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, 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
));
89 static int is_member_or_friend_template
PROTO((tree
, int));
91 /* Do any processing required when DECL (a member template declaration
92 using TEMPLATE_PARAMETERS as its innermost parameter list) is
93 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
94 it is a specialization, in which case the DECL itself is returned. */
97 finish_member_template_decl (template_parameters
, decl
)
98 tree template_parameters
;
101 if (template_parameters
)
104 end_specialization();
106 if (decl
&& DECL_TEMPLATE_INFO (decl
) &&
107 !DECL_TEMPLATE_SPECIALIZATION (decl
))
109 check_member_template (DECL_TI_TEMPLATE (decl
));
110 return DECL_TI_TEMPLATE (decl
);
116 /* Returns the template nesting level of the indicated class TYPE.
126 A<T>::B<U> has depth two, while A<T> has depth one. Also,
127 both A<T>::B<int> and A<int>::B<U> have depth one. */
130 template_class_depth (type
)
135 /* Note: this implementation will be broken when we have nested
136 template classes. Presumably we will have to wrap this if
138 if (CLASSTYPE_TEMPLATE_INFO (type
)
139 && uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
145 /* Restore the template parameter context for a member template or
146 a friend template defined in a class definition. */
149 maybe_begin_member_template_processing (decl
)
155 if (!is_member_or_friend_template (decl
, 1))
158 parms
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
160 ++processing_template_decl
;
161 current_template_parms
162 = tree_cons (build_int_2 (0, processing_template_decl
),
163 parms
, current_template_parms
);
165 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
167 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
168 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
170 switch (TREE_CODE (parm
))
179 /* Make a CONST_DECL as is done in process_template_parm. */
180 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
182 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
188 my_friendly_abort (0);
193 /* Undo the effects of begin_member_template_processing. */
196 maybe_end_member_template_processing (decl
)
199 if (!is_member_or_friend_template (decl
, 1))
202 if (! processing_template_decl
)
205 --processing_template_decl
;
206 current_template_parms
= TREE_CHAIN (current_template_parms
);
210 /* Returns non-zero iff T is a member template function, or, if
211 ALLOW_FRIEND is non-zero, a friend template function. We must be
214 template <class T> class C { void f(); }
216 Here, f is a template function, and a member, but not a member
217 template. This function does not concern itself with the origin of
218 T, only its present state. So if we have
220 template <class T> class C { template <class U> void f(U); }
222 then neither C<int>::f<char> nor C<T>::f<double> is considered
223 to be a member template. */
226 is_member_or_friend_template (t
, allow_friend
)
230 if (TREE_CODE (t
) != FUNCTION_DECL
231 && !DECL_FUNCTION_TEMPLATE_P (t
))
232 /* Anything that isn't a function or a template function is
233 certainly not a member template. */
236 if (((DECL_FUNCTION_MEMBER_P (t
)
237 || (allow_friend
&& DECL_FRIEND_P (t
)))
238 && !DECL_TEMPLATE_SPECIALIZATION (t
))
239 || (TREE_CODE (t
) == TEMPLATE_DECL
240 && (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))
242 && DECL_FUNCTION_TEMPLATE_P (t
)
243 && DECL_FRIEND_P (DECL_TEMPLATE_RESULT (t
))
244 && DECL_CLASS_CONTEXT (t
)))))
248 if (DECL_FUNCTION_TEMPLATE_P (t
))
250 else if (DECL_TEMPLATE_INFO (t
)
251 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
252 tmpl
= DECL_TI_TEMPLATE (t
);
257 /* If there are more levels of template parameters than
258 there are template classes surrounding the declaration,
259 then we have a member template. */
260 list_length (DECL_TEMPLATE_PARMS (tmpl
)) >
261 template_class_depth (DECL_CLASS_CONTEXT (t
)))
268 /* Returns non-zero iff T is a member template. */
271 is_member_template (t
)
274 return is_member_or_friend_template (t
, 0);
277 /* Return a new template argument vector which contains all of ARGS,
278 but has as its innermost set of arguments the EXTRA_ARGS. */
281 add_to_template_args (args
, extra_args
)
287 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
289 new_args
= make_tree_vec (2);
290 TREE_VEC_ELT (new_args
, 0) = args
;
296 new_args
= make_tree_vec (TREE_VEC_LENGTH (args
) - 1);
298 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
299 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
302 TREE_VEC_ELT (new_args
,
303 TREE_VEC_LENGTH (new_args
) - 1) = extra_args
;
308 /* We've got a template header coming up; push to a new level for storing
312 begin_template_parm_list ()
315 declare_pseudo_global_level ();
316 ++processing_template_decl
;
317 note_template_header (0);
321 /* We've just seen template <>. */
324 begin_specialization ()
326 note_template_header (1);
330 /* Called at then end of processing a declaration preceeded by
334 end_specialization ()
336 reset_specialization ();
340 /* Any template <>'s that we have seen thus far are not referring to a
341 function specialization. */
344 reset_specialization ()
346 processing_specialization
= 0;
347 template_header_count
= 0;
351 /* We've just seen a template header. If SPECIALIZATION is non-zero,
352 it was of the form template <>. */
355 note_template_header (specialization
)
358 processing_specialization
= specialization
;
359 template_header_count
++;
363 /* We're beginning an explicit instantiation. */
366 begin_explicit_instantiation ()
368 ++processing_explicit_instantiation
;
373 end_explicit_instantiation ()
375 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
376 --processing_explicit_instantiation
;
380 /* Retrieve the specialization (in the sense of [temp.spec] - a
381 specialization is either an instantiation or an explicit
382 specialization) of TMPL for the given template ARGS. If there is
383 no such specialization, return NULL_TREE. The ARGS are a vector of
384 arguments, or a vector of vectors of arguments, in the case of
385 templates with more than one level of parameters. */
388 retrieve_specialization (tmpl
, args
)
394 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
396 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
399 if (comp_template_args (TREE_PURPOSE (s
), args
))
400 return TREE_VALUE (s
);
407 /* Register the specialization SPEC as a specialization of TMPL with
408 the indicated ARGS. */
411 register_specialization (spec
, tmpl
, args
)
418 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
420 if (TREE_CODE (spec
) != TEMPLATE_DECL
421 && list_length (DECL_TEMPLATE_PARMS (tmpl
)) > 1)
422 /* Avoid registering function declarations as
423 specializations of member templates, as would otherwise
424 happen with out-of-class specializations of member
428 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
431 if (comp_template_args (TREE_PURPOSE (s
), args
))
433 tree fn
= TREE_VALUE (s
);
435 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
437 if (DECL_TEMPLATE_INSTANTIATION (fn
))
440 || DECL_EXPLICIT_INSTANTIATION (fn
))
442 cp_error ("specialization of %D after instantiation",
448 /* This situation should occur only if the first
449 specialization is an implicit instantiation,
450 the second is an explicit specialization, and
451 the implicit instantiation has not yet been
452 used. That situation can occur if we have
453 implicitly instantiated a member function of
454 class type, and then specialized it later. */
455 TREE_VALUE (s
) = spec
;
459 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
461 if (DECL_INITIAL (fn
))
462 cp_error ("duplicate specialization of %D", fn
);
464 TREE_VALUE (s
) = spec
;
470 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
471 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
475 /* Print the list of candidate FNS in an error message. */
478 print_candidates (fns
)
483 char* str
= "candidates are:";
485 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
487 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
492 /* Returns the template (one of the functions given by TEMPLATE_ID)
493 which can be specialized to match the indicated DECL with the
494 explicit template args given in TEMPLATE_ID. If
495 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
496 member template. The template args (those explicitly specified and
497 those deduced) are output in a newly created vector *TARGS_OUT. If
498 it is impossible to determine the result, an error message is
499 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
503 determine_specialization (template_id
, decl
, targs_out
,
504 need_member_template
,
509 int need_member_template
;
512 tree fns
= TREE_OPERAND (template_id
, 0);
513 tree targs_in
= TREE_OPERAND (template_id
, 1);
514 tree templates
= NULL_TREE
;
519 *targs_out
= NULL_TREE
;
521 if (is_overloaded_fn (fns
))
522 fn
= get_first_fn (fns
);
526 overloaded
= really_overloaded_fn (fns
);
527 for (; fn
!= NULL_TREE
;
528 fn
= overloaded
? DECL_CHAIN (fn
) : NULL_TREE
)
533 if (!need_member_template
534 && TREE_CODE (fn
) == FUNCTION_DECL
535 && DECL_FUNCTION_MEMBER_P (fn
)
536 && DECL_USE_TEMPLATE (fn
)
537 && DECL_TI_TEMPLATE (fn
))
538 /* We can get here when processing something like:
539 template <class T> class X { void f(); }
540 template <> void X<int>::f() {}
541 We're specializing a member function, but not a member
543 tmpl
= DECL_TI_TEMPLATE (fn
);
544 else if (TREE_CODE (fn
) != TEMPLATE_DECL
545 || (need_member_template
&& !is_member_template (fn
)))
550 if (list_length (targs_in
) > DECL_NTPARMS (tmpl
))
553 if (decl
== NULL_TREE
)
555 tree targs
= make_scratch_vec (DECL_NTPARMS (tmpl
));
557 /* We allow incomplete unification here, because we are going to
558 check all the functions. */
559 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
560 &TREE_VEC_ELT (targs
, 0),
567 /* Unification was successful. */
568 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
571 templates
= scratch_tree_cons (NULL_TREE
, tmpl
, templates
);
574 if (decl
!= NULL_TREE
)
576 tree tmpl
= most_specialized (templates
, decl
, targs_in
);
578 if (tmpl
== error_mark_node
)
580 else if (tmpl
== NULL_TREE
)
583 *targs_out
= get_bindings (tmpl
, decl
, targs_in
);
587 if (templates
== NULL_TREE
)
591 cp_error ("`%D' does not match any template declaration",
596 else if (TREE_CHAIN (templates
) != NULL_TREE
)
601 cp_error ("ambiguous template specialization `%D'",
603 print_candidates (templates
);
608 /* We have one, and exactly one, match. */
609 *targs_out
= TREE_PURPOSE (templates
);
610 return TREE_VALUE (templates
);
614 /* Check to see if the function just declared, as indicated in
615 DECLARATOR, and in DECL, is a specialization of a function
616 template. We may also discover that the declaration is an explicit
617 instantiation at this point.
619 Returns DECL, or an equivalent declaration that should be used
622 FLAGS is a bitmask consisting of the following flags:
624 1: We are being called by finish_struct. (We are unable to
625 determine what template is specialized by an in-class
626 declaration until the class definition is complete, so
627 finish_struct_methods calls this function again later to finish
629 2: The function has a definition.
630 4: The function is a friend.
631 8: The function is known to be a specialization of a member
634 The TEMPLATE_COUNT is the number of references to qualifying
635 template classes that appeared in the name of the function. For
638 template <class T> struct S { void f(); };
641 the TEMPLATE_COUNT would be 1. However, explicitly specialized
642 classes are not counted in the TEMPLATE_COUNT, so that in
644 template <class T> struct S {};
645 template <> struct S<int> { void f(); }
649 the TEMPLATE_COUNT would be 0. (Note that this declaration is
650 illegal; there should be no template <>.)
652 If the function is a specialization, it is marked as such via
653 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
654 is set up correctly, and it is added to the list of specializations
655 for that template. */
658 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
664 int finish_member
= flags
& 1;
665 int have_def
= flags
& 2;
666 int is_friend
= flags
& 4;
667 int specialization
= 0;
668 int explicit_instantiation
= 0;
669 int member_specialization
= flags
& 8;
671 tree ctype
= DECL_CLASS_CONTEXT (decl
);
672 tree dname
= DECL_NAME (decl
);
676 if (processing_specialization
)
678 /* The last template header was of the form template <>. */
680 if (template_header_count
> template_count
)
682 /* There were more template headers than qualifying template
684 if (template_header_count
- template_count
> 1)
685 /* There shouldn't be that many template parameter
686 lists. There can be at most one parameter list for
687 every qualifying class, plus one for the function
689 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
691 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
693 member_specialization
= 1;
697 else if (template_header_count
== template_count
)
699 /* The counts are equal. So, this might be a
700 specialization, but it is not a specialization of a
701 member template. It might be something like
703 template <class T> struct S {
707 void S<int>::f(int i) {} */
709 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
713 /* This cannot be an explicit specialization. There are not
714 enough headers for all of the qualifying classes. For
715 example, we might have:
718 void S<int>::T<char>::f();
720 But, we're missing another template <>. */
721 cp_error("too few template parameter lists in declaration of `%D'", decl
);
725 else if (processing_explicit_instantiation
)
727 if (template_header_count
)
728 cp_error ("template parameter list used in explicit instantiation");
731 cp_error ("definition provided for explicit instantiation");
733 explicit_instantiation
= 1;
735 else if (ctype
!= NULL_TREE
736 && !TYPE_BEING_DEFINED (ctype
)
737 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
739 /* This case catches outdated code that looks like this:
741 template <class T> struct S { void f(); };
742 void S<int>::f() {} // Missing template <>
744 We disable this check when the type is being defined to
745 avoid complaining about default compiler-generated
746 constructors, destructors, and assignment operators.
747 Since the type is an instantiation, not a specialization,
748 these are the only functions that can be defined before
749 the class is complete. */
752 template <class T> void S<int>::f() {}
754 if (template_header_count
)
756 cp_error ("template parameters specified in specialization");
762 ("explicit specialization not preceded by `template <>'");
764 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
766 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
768 /* This case handles bogus declarations like
769 template <> template <class T>
772 cp_error ("template-id `%D' in declaration of primary template",
778 if (specialization
|| member_specialization
|| explicit_instantiation
)
780 tree tmpl
= NULL_TREE
;
781 tree targs
= NULL_TREE
;
783 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
784 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
788 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
791 fns
= IDENTIFIER_GLOBAL_VALUE (dname
);
796 lookup_template_function (fns
, NULL_TREE
);
799 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
801 /* A friend declaration. We can't do much, because we don't
802 know what this resolves to, yet. */
803 my_friendly_assert (is_friend
!= 0, 0);
804 my_friendly_assert (!explicit_instantiation
, 0);
805 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
809 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
811 /* Since finish_struct_1 has not been called yet, we
812 can't call lookup_fnfields. We note that this
813 template is a specialization, and proceed, letting
814 finish_struct fix this up later. */
815 tree ti
= perm_tree_cons (NULL_TREE
,
816 TREE_OPERAND (declarator
, 1),
818 TI_PENDING_SPECIALIZATION_FLAG (ti
) = 1;
819 DECL_TEMPLATE_INFO (decl
) = ti
;
820 /* This should not be an instantiation; explicit
821 instantiation directives can only occur at the top
823 my_friendly_assert (!explicit_instantiation
, 0);
826 else if (ctype
!= NULL_TREE
827 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
830 /* Find the list of functions in ctype that have the same
831 name as the declared function. */
832 tree name
= TREE_OPERAND (declarator
, 0);
835 if (name
== constructor_name (ctype
)
836 || name
== constructor_name_full (ctype
))
838 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
840 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
841 : !TYPE_HAS_DESTRUCTOR (ctype
))
843 /* From [temp.expl.spec]:
845 If such an explicit specialization for the member
846 of a class template names an implicitly-declared
847 special member function (clause _special_), the
848 program is ill-formed.
850 Similar language is found in [temp.explicit]. */
851 cp_error ("specialization of implicitly-declared special member function");
856 fns
= TREE_VEC_ELT(CLASSTYPE_METHOD_VEC (ctype
),
857 is_constructor
? 0 : 1);
860 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
,
863 if (fns
== NULL_TREE
)
865 cp_error ("no member function `%s' declared in `%T'",
866 IDENTIFIER_POINTER (name
),
871 TREE_OPERAND (declarator
, 0) = fns
;
874 /* Figure out what exactly is being specialized at this point.
875 Note that for an explicit instantiation, even one for a
876 member function, we cannot tell apriori whether the the
877 instantiation is for a member template, or just a member
878 function of a template class. In particular, even in if the
879 instantiation is for a member template, the template
880 arguments could be deduced from the declaration. */
881 tmpl
= determine_specialization (declarator
, decl
,
883 member_specialization
,
888 if (explicit_instantiation
)
890 decl
= instantiate_template (tmpl
, targs
);
891 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
892 /* There doesn't seem to be anything in the draft to
893 prevent a specialization from being explicitly
894 instantiated. We're careful not to destroy the
895 information indicating that this is a
896 specialization here. */
897 SET_DECL_EXPLICIT_INSTANTIATION (decl
);
900 else if (DECL_STATIC_FUNCTION_P (tmpl
)
901 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
902 revert_static_member_fn (&decl
, 0, 0);
904 /* Mangle the function name appropriately. Note that we do
905 not mangle specializations of non-template member
906 functions of template classes, e.g. with
907 template <class T> struct S { void f(); }
908 and given the specialization
909 template <> void S<int>::f() {}
910 we do not mangle S<int>::f() here. That's because it's
911 just an ordinary member function and doesn't need special
913 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
914 && name_mangling_version
>= 1)
916 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
919 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
921 hash_tree_chain (build_pointer_type (ctype
),
924 DECL_ASSEMBLER_NAME (decl
)
925 = build_template_decl_overload
928 TREE_TYPE (TREE_TYPE (tmpl
)),
929 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
930 targs
, ctype
!= NULL_TREE
);
933 if (is_friend
&& !have_def
)
935 /* This is not really a declaration of a specialization.
936 It's just the name of an instantiation. But, it's not
937 a request for an instantiation, either. */
938 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
939 DECL_TEMPLATE_INFO (decl
)
940 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
944 /* If DECL_TI_TEMPLATE (decl), the decl is an
945 instantiation of a specialization of a member template.
946 (In other words, there was a member template, in a
947 class template. That member template was specialized.
948 We then instantiated the class, so there is now an
949 instance of that specialization.)
951 According to the CD2,
953 14.7.3.13 [tmpl.expl.spec]
955 A specialization of a member function template or
956 member class template of a non-specialized class
957 template is itself a template.
959 So, we just leave the template info alone in this case. */
960 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
961 DECL_TEMPLATE_INFO (decl
)
962 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
964 register_specialization (decl
, tmpl
, targs
);
974 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
975 parameters. These are represented in the same format used for
976 DECL_TEMPLATE_PARMS. */
978 int comp_template_parms (parms1
, parms2
)
985 if (parms1
== parms2
)
988 for (p1
= parms1
, p2
= parms2
;
989 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
990 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
992 tree t1
= TREE_VALUE (p1
);
993 tree t2
= TREE_VALUE (p2
);
996 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
997 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
999 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1002 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1004 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1005 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1007 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1010 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1012 else if (!comptypes (TREE_TYPE (parm1
),
1013 TREE_TYPE (parm2
), 1))
1018 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1019 /* One set of parameters has more parameters lists than the
1027 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1028 ORIG_LEVEL, DECL, and TYPE. */
1031 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1038 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1039 TEMPLATE_PARM_IDX (t
) = index
;
1040 TEMPLATE_PARM_LEVEL (t
) = level
;
1041 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1042 TEMPLATE_PARM_DECL (t
) = decl
;
1043 TREE_TYPE (t
) = type
;
1049 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1050 TEMPLATE_PARM_LEVEL has been decreased by one. If such a
1051 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1052 new one is created. */
1055 reduce_template_parm_level (index
, type
)
1059 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1060 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1061 != TEMPLATE_PARM_LEVEL (index
) - 1))
1064 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1065 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1068 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1069 TEMPLATE_PARM_LEVEL (index
) - 1,
1070 TEMPLATE_PARM_ORIG_LEVEL (index
),
1072 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1075 return TEMPLATE_PARM_DESCENDANTS (index
);
1078 /* Process information from new template parameter NEXT and append it to the
1079 LIST being built. */
1082 process_template_parm (list
, next
)
1091 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1092 defval
= TREE_PURPOSE (parm
);
1093 parm
= TREE_VALUE (parm
);
1094 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1098 tree p
= TREE_VALUE (tree_last (list
));
1100 if (TREE_CODE (p
) == TYPE_DECL
)
1101 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1102 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1103 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1105 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1113 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1114 /* is a const-param */
1115 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1116 PARM
, 0, NULL_TREE
);
1117 /* A template parameter is not modifiable. */
1118 TREE_READONLY (parm
) = 1;
1119 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1120 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
1122 cp_error ("`%#T' is not a valid type for a template constant parameter",
1124 if (DECL_NAME (parm
) == NULL_TREE
)
1125 error (" a template type parameter must begin with `class' or `typename'");
1126 TREE_TYPE (parm
) = void_type_node
;
1129 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1130 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1131 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1133 if (TREE_PERMANENT (parm
) == 0)
1135 parm
= copy_node (parm
);
1136 TREE_PERMANENT (parm
) = 1;
1138 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1139 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1140 = build_template_parm_index (idx
, processing_template_decl
,
1141 processing_template_decl
,
1142 decl
, TREE_TYPE (parm
));
1147 parm
= TREE_VALUE (parm
);
1149 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1151 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1152 /* This is for distinguishing between real templates and template
1153 template parameters */
1154 TREE_TYPE (parm
) = t
;
1155 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1160 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1161 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1162 decl
= build_decl (TYPE_DECL
, parm
, t
);
1165 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1166 TYPE_NAME (t
) = decl
;
1167 TYPE_STUB_DECL (t
) = decl
;
1169 TEMPLATE_TYPE_PARM_INDEX (t
)
1170 = build_template_parm_index (idx
, processing_template_decl
,
1171 processing_template_decl
,
1172 decl
, TREE_TYPE (parm
));
1174 SET_DECL_ARTIFICIAL (decl
);
1176 parm
= build_tree_list (defval
, parm
);
1177 return chainon (list
, parm
);
1180 /* The end of a template parameter list has been reached. Process the
1181 tree list into a parameter vector, converting each parameter into a more
1182 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1186 end_template_parm_list (parms
)
1191 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1193 current_template_parms
1194 = tree_cons (build_int_2 (0, processing_template_decl
),
1195 saved_parmlist
, current_template_parms
);
1197 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1198 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1200 return saved_parmlist
;
1203 /* end_template_decl is called after a template declaration is seen. */
1206 end_template_decl ()
1208 reset_specialization ();
1210 if (! processing_template_decl
)
1213 /* This matches the pushlevel in begin_template_parm_list. */
1216 --processing_template_decl
;
1217 current_template_parms
= TREE_CHAIN (current_template_parms
);
1218 (void) get_pending_sizes (); /* Why? */
1221 /* Generate a valid set of template args from current_template_parms. */
1224 current_template_args ()
1226 tree header
= current_template_parms
;
1227 int length
= list_length (header
);
1228 tree args
= make_tree_vec (length
);
1233 tree a
= copy_node (TREE_VALUE (header
));
1234 int i
= TREE_VEC_LENGTH (a
);
1235 TREE_TYPE (a
) = NULL_TREE
;
1238 tree t
= TREE_VEC_ELT (a
, i
);
1240 /* t will be a list if we are called from within a
1241 begin/end_template_parm_list pair, but a vector directly
1242 if within a begin/end_member_template_processing pair. */
1243 if (TREE_CODE (t
) == TREE_LIST
)
1247 if (TREE_CODE (t
) == TYPE_DECL
1248 || TREE_CODE (t
) == TEMPLATE_DECL
)
1251 t
= DECL_INITIAL (t
);
1254 TREE_VEC_ELT (a
, i
) = t
;
1256 TREE_VEC_ELT (args
, --l
) = a
;
1257 header
= TREE_CHAIN (header
);
1264 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1265 template PARMS. Used by push_template_decl below. */
1268 build_template_decl (decl
, parms
)
1272 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1273 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1274 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1275 if (DECL_LANG_SPECIFIC (decl
))
1277 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1278 DECL_STATIC_FUNCTION_P (tmpl
) =
1279 DECL_STATIC_FUNCTION_P (decl
);
1286 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
1287 parameters given by current_template_args, or reuses a
1288 previously existing one, if appropriate. Returns the DECL, or an
1289 equivalent one, if it is replaced via a call to duplicate_decls. */
1292 push_template_decl (decl
)
1300 int is_friend
= (TREE_CODE (decl
) == FUNCTION_DECL
1301 && DECL_FRIEND_P (decl
));
1304 /* For a friend, we want the context of the friend function, not
1305 the type of which it is a friend. */
1306 ctx
= DECL_CONTEXT (decl
);
1307 else if (DECL_REAL_CONTEXT (decl
))
1308 /* In the case of a virtual function, we want the class in which
1310 ctx
= DECL_REAL_CONTEXT (decl
);
1312 /* Otherwise, if we're currently definining some class, the DECL
1313 is assumed to be a member of the class. */
1314 ctx
= current_class_type
;
1317 || (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't'
1318 && template_class_depth (ctx
) == 0))
1319 /* At this point, we know that the DECL is not a member of some
1320 template class. However, a friend function declared in a
1321 template class is still not primary, since, in general it can
1322 depend on the template parameters of the enclosing class. */
1324 && DECL_CLASS_CONTEXT (decl
)
1325 && template_class_depth (DECL_CLASS_CONTEXT (decl
)) > 0))
1330 /* Partial specialization. */
1331 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
1332 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
1334 tree type
= TREE_TYPE (decl
);
1335 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1336 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
1337 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
1339 for (; spec
; spec
= TREE_CHAIN (spec
))
1341 /* purpose: args to main template
1342 value: spec template */
1343 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
1347 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
1348 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
1349 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
1350 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
1354 args
= current_template_args ();
1357 || TREE_CODE (ctx
) == FUNCTION_DECL
1358 || TYPE_BEING_DEFINED (ctx
)
1359 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
1361 if (DECL_LANG_SPECIFIC (decl
)
1362 && DECL_TEMPLATE_INFO (decl
)
1363 && DECL_TI_TEMPLATE (decl
))
1364 tmpl
= DECL_TI_TEMPLATE (decl
);
1367 tmpl
= build_template_decl (decl
, current_template_parms
);
1369 if (DECL_LANG_SPECIFIC (decl
)
1370 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1372 /* A specialization of a member template of a template
1374 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1375 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
1376 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
1385 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
1386 cp_error ("must specialize `%#T' before defining member `%#D'",
1388 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1389 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
1390 else if (! DECL_TEMPLATE_INFO (decl
))
1392 cp_error ("template definition of non-template `%#D'", decl
);
1396 tmpl
= DECL_TI_TEMPLATE (decl
);
1398 if (is_member_template (tmpl
))
1400 if (DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
1401 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1405 /* The declaration is a specialization of a member
1406 template, declared outside the class. Therefore, the
1407 innermost template arguments will be NULL, so we
1408 replace them with the arguments determined by the
1409 earlier call to check_explicit_specialization. */
1410 args
= DECL_TI_ARGS (decl
);
1413 = build_template_decl (decl
, current_template_parms
);
1414 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
1415 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
1416 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
1417 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
1418 DECL_TEMPLATE_INFO (new_tmpl
) =
1419 perm_tree_cons (tmpl
, args
, NULL_TREE
);
1421 register_specialization (new_tmpl
, tmpl
, args
);
1425 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1426 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
1427 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1429 cp_error ("got %d template parameters for `%#D'",
1430 TREE_VEC_LENGTH (a
), decl
);
1431 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
1433 if (TREE_VEC_LENGTH (args
) > 1)
1434 /* Get the template parameters for the enclosing template
1436 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
1441 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1445 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
1447 /* When processing an inline member template of a
1448 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
1449 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
1450 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
1452 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
1453 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
1455 /* There should be template arguments if and only if there is a
1457 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
1460 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1462 cp_error ("got %d template parameters for `%#D'",
1463 TREE_VEC_LENGTH (a
), decl
);
1464 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
1467 /* Get the innermost set of template arguments. */
1468 args
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1470 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1471 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
1473 if (! ctx
&& primary
)
1474 /* The check of PRIMARY ensures that we do not try to push a
1475 global template friend declared in a template class; such a
1476 thing may well depend on the template parameters of the class. */
1477 tmpl
= pushdecl_top_level (tmpl
);
1480 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
1482 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
1484 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1486 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
1487 if (!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
1488 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
1490 else if (! DECL_LANG_SPECIFIC (decl
))
1491 cp_error ("template declaration of `%#D'", decl
);
1493 DECL_TEMPLATE_INFO (decl
) = info
;
1495 return DECL_TEMPLATE_RESULT (tmpl
);
1499 /* Attempt to convert the non-type template parameter EXPR to the
1500 indicated TYPE. If the conversion is successful, return the
1501 converted value. If the conversion is unsuccesful, return
1502 NULL_TREE if we issued an error message, or error_mark_node if we
1503 did not. We issue error messages for out-and-out bad template
1504 parameters, but not simply because the conversion failed, since we
1505 might be just trying to do argument deduction. By the time this
1506 function is called, neither TYPE nor EXPR may make use of template
1510 convert_nontype_argument (type
, expr
)
1514 tree expr_type
= TREE_TYPE (expr
);
1516 /* A template-argument for a non-type, non-template
1517 template-parameter shall be one of:
1519 --an integral constant-expression of integral or enumeration
1522 --the name of a non-type template-parameter; or
1524 --the name of an object or function with external linkage,
1525 including function templates and function template-ids but
1526 excluding non-static class members, expressed as id-expression;
1529 --the address of an object or function with external linkage,
1530 including function templates and function template-ids but
1531 excluding non-static class members, expressed as & id-expression
1532 where the & is optional if the name refers to a function or
1535 --a pointer to member expressed as described in _expr.unary.op_. */
1537 /* An integral constant-expression can include const variables
1539 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
1540 expr
= decl_constant_value (expr
);
1542 if (is_overloaded_fn (expr
))
1543 /* OK for now. We'll check that it has external linkage later.
1544 Check this first since if expr_type is the unknown_type_node
1545 we would otherwise complain below. */
1547 else if (INTEGRAL_TYPE_P (expr_type
)
1548 || TYPE_PTRMEM_P (expr_type
)
1549 || TYPE_PTRMEMFUNC_P (expr_type
)
1550 /* The next two are g++ extensions. */
1551 || TREE_CODE (expr_type
) == REAL_TYPE
1552 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
1554 if (! TREE_CONSTANT (expr
))
1556 cp_error ("non-constant `%E' cannot be used as template argument",
1561 else if (TYPE_PTR_P (expr_type
)
1562 /* If expr is the address of an overloaded function, we
1563 will get the unknown_type_node at this point. */
1564 || expr_type
== unknown_type_node
)
1568 if (TREE_CODE (expr
) != ADDR_EXPR
)
1571 cp_error ("`%E' is not a valid template argument", expr
);
1572 error ("it must be %s%s with external linkage",
1573 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1574 ? "a pointer to " : "",
1575 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
1576 ? "a function" : "an object");
1580 referent
= TREE_OPERAND (expr
, 0);
1581 STRIP_NOPS (referent
);
1583 if (TREE_CODE (referent
) == STRING_CST
)
1585 cp_error ("string literal %E is not a valid template argument",
1587 error ("because it is the address of an object with static linkage");
1591 if (is_overloaded_fn (referent
))
1592 /* We'll check that it has external linkage later. */
1594 else if (TREE_CODE (referent
) != VAR_DECL
)
1596 else if (!TREE_PUBLIC (referent
))
1598 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
1599 return error_mark_node
;
1602 else if (TREE_CODE (expr
) == VAR_DECL
)
1604 if (!TREE_PUBLIC (expr
))
1609 cp_error ("object `%E' cannot be used as template argument", expr
);
1613 switch (TREE_CODE (type
))
1618 /* For a non-type template-parameter of integral or enumeration
1619 type, integral promotions (_conv.prom_) and integral
1620 conversions (_conv.integral_) are applied. */
1621 if (!INTEGRAL_TYPE_P (expr_type
))
1622 return error_mark_node
;
1624 /* It's safe to call digest_init in this case; we know we're
1625 just converting one integral constant expression to another. */
1626 return digest_init (type
, expr
, (tree
*) 0);
1630 /* These are g++ extensions. */
1631 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
1632 return error_mark_node
;
1634 return digest_init (type
, expr
, (tree
*) 0);
1638 tree type_pointed_to
= TREE_TYPE (type
);
1640 if (TYPE_PTRMEM_P (type
))
1641 /* For a non-type template-parameter of type pointer to data
1642 member, qualification conversions (_conv.qual_) are
1644 return perform_qualification_conversions (type
, expr
);
1645 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
1647 /* For a non-type template-parameter of type pointer to
1648 function, only the function-to-pointer conversion
1649 (_conv.func_) is applied. If the template-argument
1650 represents a set of overloaded functions (or a pointer to
1651 such), the matching function is selected from the set
1656 if (TREE_CODE (expr
) == ADDR_EXPR
)
1657 fns
= TREE_OPERAND (expr
, 0);
1661 fn
= instantiate_type (type_pointed_to
, fns
, 0);
1663 if (fn
== error_mark_node
)
1664 return error_mark_node
;
1666 if (!TREE_PUBLIC (fn
))
1668 if (really_overloaded_fn (fns
))
1669 return error_mark_node
;
1674 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
1676 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
1682 /* For a non-type template-parameter of type pointer to
1683 object, qualification conversions (_conv.qual_) and the
1684 array-to-pointer conversion (_conv.array_) are applied.
1685 [Note: In particular, neither the null pointer conversion
1686 (_conv.ptr_) nor the derived-to-base conversion
1687 (_conv.ptr_) are applied. Although 0 is a valid
1688 template-argument for a non-type template-parameter of
1689 integral type, it is not a valid template-argument for a
1690 non-type template-parameter of pointer type.]
1692 The call to decay_conversion performs the
1693 array-to-pointer conversion, if appropriate. */
1694 expr
= decay_conversion (expr
);
1696 if (expr
== error_mark_node
)
1697 return error_mark_node
;
1699 return perform_qualification_conversions (type
, expr
);
1704 case REFERENCE_TYPE
:
1706 tree type_referred_to
= TREE_TYPE (type
);
1708 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
1710 /* For a non-type template-parameter of type reference to
1711 function, no conversions apply. If the
1712 template-argument represents a set of overloaded
1713 functions, the matching function is selected from the
1714 set (_over.over_). */
1718 fn
= instantiate_type (type_referred_to
, fns
, 0);
1720 if (!TREE_PUBLIC (fn
))
1722 if (really_overloaded_fn (fns
))
1723 /* Don't issue an error here; we might get a different
1724 function if the overloading had worked out
1726 return error_mark_node
;
1731 if (fn
== error_mark_node
)
1732 return error_mark_node
;
1734 my_friendly_assert (comptypes (type
, TREE_TYPE (fn
), 1),
1741 /* For a non-type template-parameter of type reference to
1742 object, no conversions apply. The type referred to by the
1743 reference may be more cv-qualified than the (otherwise
1744 identical) type of the template-argument. The
1745 template-parameter is bound directly to the
1746 template-argument, which must be an lvalue. */
1747 if (!comptypes (TYPE_MAIN_VARIANT (expr_type
),
1748 TYPE_MAIN_VARIANT (type
), 1)
1749 || (TYPE_READONLY (expr_type
) >
1750 TYPE_READONLY (type_referred_to
))
1751 || (TYPE_VOLATILE (expr_type
) >
1752 TYPE_VOLATILE (type_referred_to
))
1753 || !real_lvalue_p (expr
))
1754 return error_mark_node
;
1766 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 0);
1768 /* For a non-type template-parameter of type pointer to member
1769 function, no conversions apply. If the template-argument
1770 represents a set of overloaded member functions, the
1771 matching member function is selected from the set
1774 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
1775 expr_type
!= unknown_type_node
)
1776 return error_mark_node
;
1778 if (TREE_CODE (expr
) == CONSTRUCTOR
)
1780 /* A ptr-to-member constant. */
1781 if (!comptypes (type
, expr_type
, 1))
1782 return error_mark_node
;
1787 if (TREE_CODE (expr
) != ADDR_EXPR
)
1788 return error_mark_node
;
1790 fns
= TREE_OPERAND (expr
, 0);
1792 fn
= instantiate_type (TREE_TYPE (TREE_TYPE (type
)),
1795 if (fn
== error_mark_node
)
1796 return error_mark_node
;
1798 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
1800 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
1807 /* All non-type parameters must have one of these types. */
1808 my_friendly_abort (0);
1812 return error_mark_node
;
1815 /* Convert all template arguments to their appropriate types, and return
1816 a vector containing the resulting values. If any error occurs, return
1817 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
1818 Some error messages are issued even if COMPLAIN is zero; for
1819 instance, if a template argument is composed from a local class.
1821 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
1822 provided in ARGLIST, or else trailing parameters must have default
1823 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
1824 deduction for any unspecified trailing arguments.
1826 If IS_TMPL_PARM is non-zero, we will coercing parameters of template
1827 template arguments. In this case, ARGLIST is a chain of TREE_LIST
1828 nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. */
1831 coerce_template_parms (parms
, arglist
, in_decl
,
1833 require_all_arguments
,
1835 tree parms
, arglist
;
1838 int require_all_arguments
;
1841 int nparms
, nargs
, i
, lost
= 0;
1842 tree vec
= NULL_TREE
;
1844 if (arglist
== NULL_TREE
)
1846 else if (TREE_CODE (arglist
) == TREE_VEC
)
1847 nargs
= TREE_VEC_LENGTH (arglist
);
1849 nargs
= list_length (arglist
);
1851 nparms
= TREE_VEC_LENGTH (parms
);
1855 && require_all_arguments
1856 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
1860 error ("incorrect number of parameters (%d, should be %d)",
1864 cp_error_at ("in template expansion for decl `%D'",
1868 return error_mark_node
;
1871 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
&& nargs
== nparms
)
1872 vec
= copy_node (arglist
);
1875 vec
= make_tree_vec (nparms
);
1877 for (i
= 0; i
< nparms
; i
++)
1880 tree parm
= TREE_VEC_ELT (parms
, i
);
1885 arglist
= TREE_CHAIN (arglist
);
1887 if (arg
== error_mark_node
)
1890 arg
= TREE_VALUE (arg
);
1892 else if (is_tmpl_parm
&& i
< nargs
)
1894 arg
= TREE_VEC_ELT (arglist
, i
);
1895 if (arg
== error_mark_node
)
1898 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
1900 my_friendly_assert (!require_all_arguments
, 0);
1903 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
1904 arg
= tsubst (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1906 arg
= tsubst_expr (TREE_PURPOSE (parm
), vec
, i
, in_decl
);
1908 TREE_VEC_ELT (vec
, i
) = arg
;
1911 for (i
= 0; i
< nparms
; i
++)
1913 tree arg
= TREE_VEC_ELT (vec
, i
);
1914 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1916 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
1918 if (is_tmpl_parm
&& i
< nargs
)
1920 /* In case we are checking arguments inside a template template
1921 parameter, ARG that does not come from default argument is
1922 also a TREE_LIST node. Note that ARG can also be a TREE_LIST
1923 in other cases such as overloaded functions. */
1924 if (arg
!= NULL_TREE
&& arg
!= error_mark_node
)
1925 arg
= TREE_VALUE (arg
);
1928 if (arg
== NULL_TREE
)
1929 /* We're out of arguments. */
1931 my_friendly_assert (!require_all_arguments
, 0);
1935 if (arg
== error_mark_node
)
1937 cp_error ("template argument %d is invalid", i
+ 1);
1942 if (TREE_CODE (arg
) == TREE_LIST
1943 && TREE_TYPE (arg
) != NULL_TREE
1944 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
1946 /* The template argument was the name of some
1947 member function. That's usually
1948 illegal, but static members are OK. In any
1949 case, grab the underlying fields/functions
1950 and issue an error later if required. */
1951 arg
= TREE_VALUE (arg
);
1952 TREE_TYPE (arg
) = unknown_type_node
;
1955 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
1956 requires_type
= TREE_CODE (parm
) == TYPE_DECL
1957 || requires_tmpl_type
;
1959 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
1960 we also accept implicitly created TYPE_DECL as a valid argument. */
1961 is_tmpl_type
= (TREE_CODE (arg
) == TEMPLATE_DECL
1962 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
1963 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
1964 && !CLASSTYPE_TEMPLATE_INFO (arg
))
1965 || (TREE_CODE (arg
) == RECORD_TYPE
1966 && CLASSTYPE_TEMPLATE_INFO (arg
)
1967 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
1968 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
1969 && requires_tmpl_type
);
1970 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
1971 arg
= TYPE_STUB_DECL (arg
);
1972 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
1973 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
1975 if (is_tmpl_parm
&& i
< nargs
)
1976 is_type
= TREE_CODE (arg
) == TYPE_DECL
|| is_tmpl_type
;
1978 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
1980 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
1981 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
1983 cp_pedwarn ("to refer to a type member of a template parameter,");
1984 cp_pedwarn (" use `typename %E'", arg
);
1986 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
1987 TREE_OPERAND (arg
, 1));
1990 if (is_type
!= requires_type
)
1996 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1999 cp_error (" expected a constant of type `%T', got `%T'",
2001 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
2003 cp_error (" expected a type, got `%E'", arg
);
2007 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2010 if (is_tmpl_type
^ requires_tmpl_type
)
2014 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2017 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
2019 cp_error (" expected a class template, got `%T'", arg
);
2022 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2027 if (requires_tmpl_type
)
2029 cp_error ("nested template template parameter not implemented");
2031 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2038 if (requires_tmpl_type
)
2040 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2041 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2043 /* The parameter and argument roles have to be switched
2044 here in order to handle default arguments properly.
2046 template<template <class> class TT> void f(TT<int>)
2047 should be able to accept vector<int> which comes from
2048 template <class T, class Allcator = allocator>
2051 val
= coerce_template_parms (argparm
, parmparm
, in_decl
, 1, 1, 1);
2052 if (val
!= error_mark_node
)
2055 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2057 if (val
!= error_mark_node
2058 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
2059 val
= TREE_TYPE (val
);
2063 val
= groktypename (arg
);
2064 if (! processing_template_decl
)
2066 tree t
= target_type (val
);
2067 if (TREE_CODE (t
) != TYPENAME_TYPE
2069 && decl_function_context (TYPE_MAIN_DECL (t
)))
2071 cp_error ("type `%T' composed from a local class is not a valid template-argument",
2073 return error_mark_node
;
2080 tree t
= tsubst (TREE_TYPE (parm
), vec
,
2081 TREE_VEC_LENGTH (vec
), in_decl
);
2083 if (processing_template_decl
)
2084 arg
= maybe_fold_nontype_arg (arg
);
2086 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
2087 /* We used to call digest_init here. However, digest_init
2088 will report errors, which we don't want when complain
2089 is zero. More importantly, digest_init will try too
2090 hard to convert things: for example, `0' should not be
2091 converted to pointer type at this point according to
2092 the standard. Accepting this is not merely an
2093 extension, since deciding whether or not these
2094 conversions can occur is part of determining which
2095 function template to call, or whether a given epxlicit
2096 argument specification is legal. */
2097 val
= convert_nontype_argument (t
, arg
);
2101 if (val
== NULL_TREE
)
2102 val
= error_mark_node
;
2103 else if (val
== error_mark_node
&& complain
)
2104 cp_error ("could not convert template argument `%E' to `%T'",
2108 if (val
== error_mark_node
)
2111 TREE_VEC_ELT (vec
, i
) = val
;
2114 return error_mark_node
;
2118 /* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
2119 of template arguments. Returns 0 otherwise. */
2122 comp_template_args (oldargs
, newargs
)
2123 tree oldargs
, newargs
;
2127 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
2130 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
2132 tree nt
= TREE_VEC_ELT (newargs
, i
);
2133 tree ot
= TREE_VEC_ELT (oldargs
, i
);
2137 if (TREE_CODE (nt
) != TREE_CODE (ot
))
2139 if (TREE_CODE (nt
) == TREE_VEC
)
2141 /* For member templates */
2142 if (comp_template_args (nt
, ot
))
2145 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
2147 if (comptypes (ot
, nt
, 1))
2150 else if (cp_tree_equal (ot
, nt
) > 0)
2157 /* Given class template name and parameter list, produce a user-friendly name
2158 for the instantiation. */
2161 mangle_class_name_for_template (name
, parms
, arglist
, ctx
)
2163 tree parms
, arglist
;
2166 static struct obstack scratch_obstack
;
2167 static char *scratch_firstobj
;
2170 if (!scratch_firstobj
)
2171 gcc_obstack_init (&scratch_obstack
);
2173 obstack_free (&scratch_obstack
, scratch_firstobj
);
2174 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
2177 #define buflen sizeof(buf)
2178 #define check if (bufp >= buf+buflen-1) goto too_long
2179 #define ccat(c) *bufp++=(c); check
2180 #define advance bufp+=strlen(bufp); check
2181 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2184 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2186 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2193 if (TREE_CODE (ctx
) == FUNCTION_DECL
)
2194 s
= fndecl_as_string(ctx
, 0);
2195 else if (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
2196 s
= type_as_string(ctx
, 0);
2198 my_friendly_abort (0);
2204 nparms
= TREE_VEC_LENGTH (parms
);
2205 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
2206 for (i
= 0; i
< nparms
; i
++)
2208 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2209 tree arg
= TREE_VEC_ELT (arglist
, i
);
2214 if (TREE_CODE (parm
) == TYPE_DECL
)
2216 cat (type_as_string (arg
, 0));
2219 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
2221 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
2222 /* Already substituted with real template. Just output
2223 the template name here */
2224 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
2226 /* Output the parameter declaration */
2227 cat (type_as_string (arg
, 0));
2231 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
2233 if (TREE_CODE (arg
) == TREE_LIST
)
2235 /* New list cell was built because old chain link was in
2237 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
2238 arg
= TREE_VALUE (arg
);
2240 /* No need to check arglist against parmlist here; we did that
2241 in coerce_template_parms, called from lookup_template_class. */
2242 cat (expr_as_string (arg
, 0));
2245 char *bufp
= obstack_next_free (&scratch_obstack
);
2247 while (bufp
[offset
- 1] == ' ')
2249 obstack_blank_fast (&scratch_obstack
, offset
);
2251 /* B<C<char> >, not B<C<char>> */
2252 if (bufp
[offset
- 1] == '>')
2257 return (char *) obstack_base (&scratch_obstack
);
2262 fatal ("out of (preallocated) string space creating template instantiation name");
2268 classtype_mangled_name (t
)
2271 if (CLASSTYPE_TEMPLATE_INFO (t
)
2272 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
2274 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
2275 char *mangled_name
= mangle_class_name_for_template
2276 (IDENTIFIER_POINTER (name
),
2277 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
2278 CLASSTYPE_TI_ARGS (t
), DECL_CONTEXT (t
));
2279 tree id
= get_identifier (mangled_name
);
2280 IDENTIFIER_TEMPLATE (id
) = name
;
2284 return TYPE_IDENTIFIER (t
);
2288 add_pending_template (d
)
2293 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
2294 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
2296 ti
= DECL_TEMPLATE_INFO (d
);
2298 if (TI_PENDING_TEMPLATE_FLAG (ti
))
2301 *template_tail
= perm_tree_cons
2302 (current_function_decl
, d
, NULL_TREE
);
2303 template_tail
= &TREE_CHAIN (*template_tail
);
2304 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
2308 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2309 may be either a _DECL or an overloaded function or an
2310 IDENTIFIER_NODE), and ARGLIST. */
2313 lookup_template_function (fns
, arglist
)
2316 if (fns
== NULL_TREE
)
2318 cp_error ("non-template used as template");
2319 return error_mark_node
;
2322 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
2323 copy_to_permanent (arglist
);
2325 return build_min (TEMPLATE_ID_EXPR
,
2327 ? TREE_TYPE (fns
) : unknown_type_node
,
2332 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2333 parameters, find the desired type.
2335 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2336 Since ARGLIST is build on the decl_obstack, we must copy it here
2337 to keep it from being reclaimed when the decl storage is reclaimed.
2339 IN_DECL, if non-NULL, is the template declaration we are trying to
2342 If the template class is really a local class in a template
2343 function, then the FUNCTION_CONTEXT is the function in which it is
2344 being instantiated. */
2347 lookup_template_class (d1
, arglist
, in_decl
, context
)
2352 tree
template = NULL_TREE
, parmlist
;
2356 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
2358 if (IDENTIFIER_LOCAL_VALUE (d1
)
2359 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
2360 template = IDENTIFIER_LOCAL_VALUE (d1
);
2363 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
2365 template = IDENTIFIER_CLASS_VALUE (d1
);
2368 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
2370 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
2371 return error_mark_node
;
2372 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
2373 d1
= DECL_NAME (template);
2375 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
2377 template = CLASSTYPE_TI_TEMPLATE (d1
);
2378 d1
= DECL_NAME (template);
2381 my_friendly_abort (272);
2383 /* With something like `template <class T> class X class X { ... };'
2384 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2385 We don't want to do that, but we have to deal with the situation, so
2386 let's give them some syntax errors to chew on instead of a crash. */
2388 return error_mark_node
;
2389 if (TREE_CODE (template) != TEMPLATE_DECL
)
2391 cp_error ("non-template type `%T' used as a template", d1
);
2393 cp_error_at ("for template declaration `%D'", in_decl
);
2394 return error_mark_node
;
2397 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2399 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2400 template arguments */
2402 tree parm
= copy_template_template_parm (TREE_TYPE (template));
2403 tree template2
= TYPE_STUB_DECL (parm
);
2406 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
2407 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2409 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1, 0);
2410 if (arglist2
== error_mark_node
)
2411 return error_mark_node
;
2413 arglist2
= copy_to_permanent (arglist2
);
2414 CLASSTYPE_TEMPLATE_INFO (parm
)
2415 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
2416 TYPE_SIZE (parm
) = 0;
2419 else if (PRIMARY_TEMPLATE_P (template)
2420 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2423 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2425 arglist
= coerce_template_parms (parmlist
, arglist
, template,
2427 if (arglist
== error_mark_node
)
2428 return error_mark_node
;
2429 if (uses_template_parms (arglist
))
2432 if (comp_template_args
2433 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
2434 found
= TREE_TYPE (template);
2437 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
2438 found
; found
= TREE_CHAIN (found
))
2440 if (TI_USES_TEMPLATE_PARMS (found
)
2441 && comp_template_args (TREE_PURPOSE (found
), arglist
))
2445 found
= TREE_VALUE (found
);
2450 if (can_free (&permanent_obstack
, arglist
))
2451 obstack_free (&permanent_obstack
, arglist
);
2456 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
2460 id
= get_identifier (mangled_name
);
2461 IDENTIFIER_TEMPLATE (id
) = d1
;
2463 maybe_push_to_top_level (uses_template_parms (arglist
));
2464 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
2466 if (context
!= NULL_TREE
)
2468 /* Set up the context for the type_decl correctly. Note
2469 that we must clear DECL_ASSEMBLER_NAME to fool
2470 build_overload_name into creating a new name. */
2471 tree type_decl
= TYPE_STUB_DECL (t
);
2473 TYPE_CONTEXT (t
) = context
;
2474 DECL_CONTEXT (type_decl
) = context
;
2475 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
2476 DECL_ASSEMBLER_NAME (type_decl
) =
2477 get_identifier (build_overload_name (t
, 1, 1));
2480 pop_from_top_level ();
2484 tree type_ctx
= TYPE_CONTEXT (TREE_TYPE (template));
2485 tree args
= tsubst (CLASSTYPE_TI_ARGS (type_ctx
),
2487 TREE_VEC_LENGTH (arglist
),
2489 tree ctx
= lookup_template_class (type_ctx
, args
,
2490 in_decl
, NULL_TREE
);
2492 arglist
= CLASSTYPE_TI_ARGS (ctx
);
2494 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
2496 int save_temp
= processing_template_decl
;
2497 processing_template_decl
= 0;
2498 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
2499 processing_template_decl
= save_temp
;
2503 t
= lookup_nested_type_by_name (ctx
, id
);
2504 my_friendly_assert (t
!= NULL_TREE
, 42);
2508 /* Seems to be wanted. */
2509 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
2511 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2513 arglist
= copy_to_permanent (arglist
);
2514 CLASSTYPE_TEMPLATE_INFO (t
)
2515 = perm_tree_cons (template, arglist
, NULL_TREE
);
2516 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2517 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
2518 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2519 = uses_template_parms (arglist
);
2521 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
2523 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2524 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
2525 /* if (! uses_template_parms (arglist)) */
2526 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
2527 = get_identifier (build_overload_name (t
, 1, 1));
2529 if (flag_external_templates
&& ! uses_template_parms (arglist
)
2530 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2531 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2532 add_pending_template (t
);
2538 /* Should be defined in parse.h. */
2542 uses_template_parms (t
)
2547 switch (TREE_CODE (t
))
2551 /* We assume that the object must be instantiated in order to build
2552 the COMPONENT_REF, so we test only whether the type of the
2553 COMPONENT_REF uses template parms. */
2554 return uses_template_parms (TREE_TYPE (t
));
2556 case IDENTIFIER_NODE
:
2557 if (!IDENTIFIER_TEMPLATE (t
))
2559 my_friendly_abort (42);
2561 /* aggregates of tree nodes */
2564 int i
= TREE_VEC_LENGTH (t
);
2566 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
2571 if (uses_template_parms (TREE_PURPOSE (t
))
2572 || uses_template_parms (TREE_VALUE (t
)))
2574 return uses_template_parms (TREE_CHAIN (t
));
2576 /* constructed type nodes */
2578 case REFERENCE_TYPE
:
2579 return uses_template_parms (TREE_TYPE (t
));
2581 if (TYPE_PTRMEMFUNC_FLAG (t
))
2582 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
2584 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2586 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
2588 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2590 return uses_template_parms (TREE_TYPE (t
));
2592 if (uses_template_parms (TYPE_DOMAIN (t
)))
2594 return uses_template_parms (TREE_TYPE (t
));
2596 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
2598 return uses_template_parms (TREE_TYPE (t
));
2600 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
2602 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
2604 return uses_template_parms (TREE_TYPE (t
));
2608 return uses_template_parms (TREE_TYPE (t
));
2611 /* A template template parameter is encountered */
2612 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
2613 /* We are parsing a template declaration */
2615 /* We are instantiating templates with template template
2620 if (uses_template_parms (DECL_INITIAL (t
)))
2622 goto check_type_and_context
;
2626 /* ??? What about FIELD_DECLs? */
2627 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
2628 && uses_template_parms (DECL_TI_ARGS (t
)))
2632 check_type_and_context
:
2633 if (uses_template_parms (TREE_TYPE (t
)))
2635 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
2640 return uses_template_parms (TREE_TYPE (t
));
2642 return uses_template_parms (TREE_OPERAND (t
, 0));
2644 /* template parm nodes */
2645 case TEMPLATE_TYPE_PARM
:
2646 case TEMPLATE_TEMPLATE_PARM
:
2647 case TEMPLATE_PARM_INDEX
:
2650 /* simple type nodes */
2652 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
2654 return uses_template_parms (TYPE_MAX_VALUE (t
));
2666 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
2667 if (uses_template_parms (TREE_VALUE (v
)))
2679 /* Non-error_mark_node ERROR_MARKs are bad things. */
2680 my_friendly_assert (t
== error_mark_node
, 274);
2689 return uses_template_parms (TREE_OPERAND (t
, 0));
2692 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
2693 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
2694 return uses_template_parms (TREE_OPERAND (t
, 1));
2698 case REINTERPRET_CAST_EXPR
:
2699 case CONST_CAST_EXPR
:
2700 case STATIC_CAST_EXPR
:
2701 case DYNAMIC_CAST_EXPR
:
2709 return uses_template_parms (TREE_OPERAND (t
, 0));
2712 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
2720 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
2721 if (uses_template_parms (TREE_OPERAND (t
, i
)))
2728 sorry ("testing %s for template parms",
2729 tree_code_name
[(int) TREE_CODE (t
)]);
2730 my_friendly_abort (82);
2736 static struct tinst_level
*current_tinst_level
= 0;
2737 static struct tinst_level
*free_tinst_level
= 0;
2738 static int tinst_depth
= 0;
2739 extern int max_tinst_depth
;
2740 #ifdef GATHER_STATISTICS
2741 int depth_reached
= 0;
2745 push_tinst_level (d
)
2748 struct tinst_level
*new;
2750 if (tinst_depth
>= max_tinst_depth
)
2752 struct tinst_level
*p
= current_tinst_level
;
2754 char *file
= input_filename
;
2756 error ("template instantiation depth exceeds maximum of %d",
2758 error (" (use -ftemplate-depth-NN to increase the maximum)");
2759 cp_error (" instantiating `%D'", d
);
2761 for (; p
; p
= p
->next
)
2763 cp_error (" instantiated from `%D'", p
->decl
);
2765 input_filename
= p
->file
;
2767 error (" instantiated from here");
2770 input_filename
= file
;
2775 if (free_tinst_level
)
2777 new = free_tinst_level
;
2778 free_tinst_level
= new->next
;
2781 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
2785 new->file
= input_filename
;
2786 new->next
= current_tinst_level
;
2787 current_tinst_level
= new;
2790 #ifdef GATHER_STATISTICS
2791 if (tinst_depth
> depth_reached
)
2792 depth_reached
= tinst_depth
;
2801 struct tinst_level
*old
= current_tinst_level
;
2803 current_tinst_level
= old
->next
;
2804 old
->next
= free_tinst_level
;
2805 free_tinst_level
= old
;
2809 struct tinst_level
*
2812 struct tinst_level
*p
= current_tinst_level
;
2815 for (; p
->next
; p
= p
->next
)
2821 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
2822 vector of template arguments, as for tsubst.
2824 Returns an appropriate tsbust'd friend declaration. */
2827 tsubst_friend_function (decl
, args
)
2833 if (TREE_CODE (decl
) == FUNCTION_DECL
2834 && DECL_TEMPLATE_INSTANTIATION (decl
)
2835 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
2836 /* This was a friend declared with an explicit template
2837 argument list, e.g.:
2841 to indicate that f was a template instantiation, not a new
2842 function declaration. Now, we have to figure out what
2843 instantiation of what template. */
2851 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
2853 TREE_VEC_LENGTH (args
),
2855 tsubst (DECL_TI_ARGS (decl
),
2857 TREE_VEC_LENGTH (decl
),
2860 /* Temporarily remove the DECL_TEMPLATE_INFO so as not to
2862 tinfo
= DECL_TEMPLATE_INFO (decl
);
2863 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
2864 new_friend
= tsubst (decl
, args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
2865 DECL_TEMPLATE_INFO (decl
) = tinfo
;
2867 tmpl
= determine_specialization (template_id
,
2871 return instantiate_template (tmpl
, new_args
);
2874 new_friend
= tsubst (decl
, args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
2876 /* The new_friend will look like an instantiation, to the
2877 compiler, but is not an instantiation from the point of view of
2878 the language. For example, we might have had:
2880 template <class T> struct S {
2881 template <class U> friend void f(T, U);
2884 Then, in S<int>, template <class U> void f(int, U) is not an
2885 instantiation of anything. */
2886 DECL_USE_TEMPLATE (new_friend
) = 0;
2887 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2888 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
2890 if (DECL_CONTEXT (new_friend
) == NULL_TREE
)
2892 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
2893 /* This declaration is a `primary' template. */
2894 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend
))
2897 new_friend
= pushdecl_top_level (new_friend
);
2899 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
2901 /* Check to see that the declaration is really present, and,
2902 possibly obtain an improved declaration. */
2903 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
2915 instantiate_class_template (type
)
2918 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
2921 if (type
== error_mark_node
)
2922 return error_mark_node
;
2924 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
2926 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
2929 template = TI_TEMPLATE (template_info
);
2930 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
2931 args
= TI_ARGS (template_info
);
2933 t
= most_specialized_class
2934 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
2936 if (t
== error_mark_node
)
2938 char *str
= "candidates are:";
2939 cp_error ("ambiguous class template instantiation for `%#T'", type
);
2940 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
2942 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
2944 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
2948 TYPE_BEING_DEFINED (type
) = 1;
2949 return error_mark_node
;
2952 pattern
= TREE_TYPE (t
);
2954 pattern
= TREE_TYPE (template);
2956 if (TYPE_SIZE (pattern
) == NULL_TREE
)
2960 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
2962 TYPE_BEING_DEFINED (type
) = 1;
2964 if (! push_tinst_level (type
))
2967 maybe_push_to_top_level (uses_template_parms (type
));
2968 pushclass (type
, 0);
2970 if (flag_external_templates
)
2972 if (flag_alt_external_templates
)
2974 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
2975 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
2976 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2977 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2978 && CLASSTYPE_INTERFACE_KNOWN (type
);
2982 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
2983 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
2984 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
2985 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
2986 = ! CLASSTYPE_INTERFACE_ONLY (type
)
2987 && CLASSTYPE_INTERFACE_KNOWN (type
);
2992 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
2993 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
2996 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
2997 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
2998 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
2999 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
3000 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
3001 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
3002 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
3003 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
3004 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
3005 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
3006 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
3007 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
3008 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
3009 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
3010 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
3011 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
3012 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
3013 TYPE_USES_COMPLEX_INHERITANCE (type
)
3014 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
3015 TYPE_USES_MULTIPLE_INHERITANCE (type
)
3016 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
3017 TYPE_USES_VIRTUAL_BASECLASSES (type
)
3018 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
3019 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
3020 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
3023 tree binfo
= TYPE_BINFO (type
);
3024 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
3030 int len
= TREE_VEC_LENGTH (pbases
);
3031 bases
= make_tree_vec (len
);
3032 for (i
= 0; i
< len
; ++i
)
3036 TREE_VEC_ELT (bases
, i
) = elt
3037 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
3038 TREE_VEC_LENGTH (args
), NULL_TREE
);
3039 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
3041 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
3043 ("base type `%T' of `%T' fails to be a struct or class type",
3044 TREE_TYPE (elt
), type
);
3045 else if (! uses_template_parms (type
)
3046 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
3048 cp_error ("base class `%T' of `%T' has incomplete type",
3049 TREE_TYPE (elt
), type
);
3051 /* Don't initialize this until the vector is filled out, or
3052 lookups will crash. */
3053 BINFO_BASETYPES (binfo
) = bases
;
3057 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
3059 field_chain
= &TYPE_FIELDS (type
);
3061 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
3063 tree tag
= TREE_VALUE (t
);
3065 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
3066 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
3069 tsubst_enum (tag
, args
, TREE_VEC_LENGTH (args
), field_chain
);
3071 while (*field_chain
)
3073 DECL_FIELD_CONTEXT (*field_chain
) = type
;
3074 field_chain
= &TREE_CHAIN (*field_chain
);
3079 TREE_VEC_LENGTH (args
), NULL_TREE
);
3082 /* Don't replace enum constants here. */
3083 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
3084 if (TREE_CODE (t
) != CONST_DECL
)
3086 tree r
= tsubst (t
, args
,
3087 TREE_VEC_LENGTH (args
), NULL_TREE
);
3088 if (TREE_CODE (r
) == VAR_DECL
)
3090 if (! uses_template_parms (r
))
3091 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
3092 /* Perhaps I should do more of grokfield here. */
3094 DECL_IN_AGGR_P (r
) = 1;
3095 DECL_EXTERNAL (r
) = 1;
3096 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
3100 field_chain
= &TREE_CHAIN (r
);
3103 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
3104 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
3106 if (DECL_CONSTRUCTOR_P (t
))
3107 grok_ctor_properties (type
, t
);
3108 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
3109 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
3112 /* Construct the DECL_FRIENDLIST for the new class type. */
3113 typedecl
= TYPE_MAIN_DECL (type
);
3114 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
3120 DECL_FRIENDLIST (typedecl
)
3121 = tree_cons (TREE_PURPOSE (t
), NULL_TREE
,
3122 DECL_FRIENDLIST (typedecl
));
3124 for (friends
= TREE_VALUE (t
);
3125 friends
!= NULL_TREE
;
3126 friends
= TREE_CHAIN (friends
))
3128 if (TREE_PURPOSE (friends
) == error_mark_node
)
3130 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3131 = tree_cons (error_mark_node
,
3132 tsubst_friend_function (TREE_VALUE (friends
),
3134 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3138 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3139 = tree_cons (tsubst (TREE_PURPOSE (friends
),
3140 args
, TREE_VEC_LENGTH (args
),
3143 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3150 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
3151 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
3152 TREE_VEC_LENGTH (args
), NULL_TREE
);
3154 /* This does injection for friend classes. */
3155 for (; d
; d
= TREE_CHAIN (d
))
3156 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
3158 /* This does injection for friend functions. */
3159 if (!processing_template_decl
)
3161 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
3162 TREE_VEC_LENGTH (args
), NULL_TREE
);
3164 for (; d
; d
= TREE_CHAIN (d
))
3166 tree t
= TREE_VALUE (d
);
3168 if (TREE_CODE (t
) == TYPE_DECL
)
3169 /* Already injected. */;
3176 if (! uses_template_parms (type
))
3179 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
3180 if (TREE_CODE (tmp
) == FIELD_DECL
)
3182 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
3183 require_complete_type (tmp
);
3186 type
= finish_struct_1 (type
, 0);
3187 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
3189 repo_template_used (type
);
3190 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
3191 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
3195 TYPE_SIZE (type
) = integer_zero_node
;
3196 CLASSTYPE_METHOD_VEC (type
)
3197 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
3200 TYPE_BEING_DEFINED (type
) = 0;
3203 pop_from_top_level ();
3213 if (t1
== NULL_TREE
)
3214 return t2
== NULL_TREE
;
3215 if (t2
== NULL_TREE
)
3217 /* Don't care if one declares its arg const and the other doesn't -- the
3218 main variant of the arg type is all that matters. */
3219 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
3220 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
3222 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
3226 lookup_nested_type_by_name (ctype
, name
)
3231 complete_type (ctype
);
3233 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
3235 if (name
== TREE_PURPOSE (t
)
3236 /* this catches typedef enum { foo } bar; */
3237 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
3238 return TREE_VALUE (t
);
3243 /* If arg is a non-type template parameter that does not depend on template
3244 arguments, fold it like we weren't in the body of a template. */
3247 maybe_fold_nontype_arg (arg
)
3250 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
3251 && !uses_template_parms (arg
))
3253 /* Sometimes, one of the args was an expression involving a
3254 template constant parameter, like N - 1. Now that we've
3255 tsubst'd, we might have something like 2 - 1. This will
3256 confuse lookup_template_class, so we do constant folding
3257 here. We have to unset processing_template_decl, to
3258 fool build_expr_from_tree() into building an actual
3261 int saved_processing_template_decl
= processing_template_decl
;
3262 processing_template_decl
= 0;
3263 arg
= fold (build_expr_from_tree (arg
));
3264 processing_template_decl
= saved_processing_template_decl
;
3269 /* Return the TREE_VEC with the arguments for the innermost template header,
3270 where ARGS is either that or the VEC of VECs for all the arguments.
3272 If is_spec, then we are dealing with a specialization of a member
3273 template, and want the second-innermost args, the innermost ones that
3274 are instantiated. */
3277 innermost_args (args
, is_spec
)
3281 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3282 return TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1 - is_spec
);
3286 /* Take the tree structure T and replace template parameters used therein
3287 with the argument vector ARGS. NARGS is the number of args; should
3288 be removed. IN_DECL is an associated decl for diagnostics.
3290 tsubst is used for dealing with types, decls and the like; for
3291 expressions, use tsubst_expr or tsubst_copy. */
3294 tsubst (t
, args
, nargs
, in_decl
)
3301 if (t
== NULL_TREE
|| t
== error_mark_node
3302 || t
== integer_type_node
3303 || t
== void_type_node
3304 || t
== char_type_node
)
3307 type
= TREE_TYPE (t
);
3308 if (type
== unknown_type_node
)
3309 my_friendly_abort (42);
3310 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
3311 && TREE_CODE (t
) != TYPENAME_TYPE
3312 && TREE_CODE (t
) != TEMPLATE_DECL
)
3313 type
= tsubst (type
, args
, nargs
, in_decl
);
3315 switch (TREE_CODE (t
))
3318 if (TYPE_PTRMEMFUNC_P (t
))
3320 tree r
= build_ptrmemfunc_type
3321 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
3322 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3326 /* else fall through */
3328 if (uses_template_parms (t
))
3330 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
3336 ? tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
) : NULL_TREE
;
3338 r
= lookup_template_class (t
, argvec
, in_decl
, context
);
3340 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3344 /* else fall through */
3346 case IDENTIFIER_NODE
:
3359 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
3360 if (ctx
== NULL_TREE
)
3362 else if (ctx
== current_function_decl
)
3363 return lookup_name (TYPE_IDENTIFIER (t
), 1);
3365 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
3369 if (t
== integer_type_node
)
3372 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
3373 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
3377 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
3378 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
3379 if (processing_template_decl
)
3381 tree itype
= make_node (INTEGER_TYPE
);
3382 TYPE_MIN_VALUE (itype
) = size_zero_node
;
3383 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
3388 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
3389 return build_index_2_type (size_zero_node
, max
);
3392 case TEMPLATE_TYPE_PARM
:
3393 case TEMPLATE_TEMPLATE_PARM
:
3394 case TEMPLATE_PARM_INDEX
:
3400 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3401 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3403 idx
= TEMPLATE_TYPE_IDX (t
);
3404 level
= TEMPLATE_TYPE_LEVEL (t
);
3408 idx
= TEMPLATE_PARM_IDX (t
);
3409 level
= TEMPLATE_PARM_LEVEL (t
);
3412 if (TREE_VEC_LENGTH (args
) > 0)
3414 tree arg
= NULL_TREE
;
3416 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3418 if (TREE_VEC_LENGTH (args
) >= level
- 1)
3420 (TREE_VEC_ELT (args
, level
- 1), idx
);
3422 else if (level
== 1)
3423 arg
= TREE_VEC_ELT (args
, idx
);
3425 if (arg
!= NULL_TREE
)
3427 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
3428 return cp_build_type_variant
3429 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
3430 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
3431 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3433 if (CLASSTYPE_TEMPLATE_INFO (t
))
3435 /* We are processing a type constructed from
3436 a template template parameter */
3437 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
3438 args
, nargs
, in_decl
);
3441 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3442 we are resolving nested-types in the signature of
3443 a member function templates.
3444 Otherwise ARG is a TEMPLATE_DECL and is the real
3445 template to be instantiated. */
3446 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3447 arg
= TYPE_NAME (arg
);
3449 r
= lookup_template_class (DECL_NAME (arg
),
3451 DECL_CONTEXT (arg
));
3452 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3456 /* We are processing a template argument list. */
3464 /* If we get here, we must have been looking at a parm for a
3465 more deeply nested template. */
3466 my_friendly_assert(level
> 1, 0);
3468 /* Make a new version of this template parameter, but with a
3470 switch (TREE_CODE (t
))
3472 case TEMPLATE_TYPE_PARM
:
3473 case TEMPLATE_TEMPLATE_PARM
:
3475 TEMPLATE_TYPE_PARM_INDEX (r
)
3476 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
3478 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
3479 TYPE_MAIN_VARIANT (r
) = r
;
3480 TYPE_POINTER_TO (r
) = NULL_TREE
;
3483 case TEMPLATE_PARM_INDEX
:
3484 r
= reduce_template_parm_level (t
, TREE_TYPE (t
));
3488 my_friendly_abort (0);
3496 /* We can get here when processing a member template function
3497 of a template class. */
3499 tree decl
= DECL_TEMPLATE_RESULT (t
);
3505 if (TREE_CODE (decl
) == TYPE_DECL
)
3507 if (TREE_CODE (TREE_TYPE (decl
)) == TEMPLATE_TEMPLATE_PARM
)
3508 /* There is no tsubst'ing to be done in a template template
3512 /* This must be a member template class. We don't handle
3514 sorry ("member template classes");
3518 /* We might already have an instance of this template. */
3519 spec
= retrieve_specialization (t
, args
);
3520 if (spec
!= NULL_TREE
)
3523 /* Make a new template decl. It will be similar to the
3524 original, but will record the current template arguments.
3525 We also create a new function declaration, which is just
3526 like the old one, but points to this new template, rather
3527 than the old one. */
3528 tmpl
= copy_node (t
);
3529 copy_lang_decl (tmpl
);
3530 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
3531 DECL_CHAIN (tmpl
) = NULL_TREE
;
3532 TREE_CHAIN (tmpl
) = NULL_TREE
;
3533 DECL_CONTEXT (tmpl
) = tsubst (DECL_CONTEXT (t
),
3534 args
, nargs
, in_decl
);
3535 DECL_CLASS_CONTEXT (tmpl
) = tsubst (DECL_CLASS_CONTEXT (t
),
3536 args
, nargs
, in_decl
);
3537 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
3538 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
3539 DECL_RESULT (tmpl
) = new_decl
;
3540 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
3541 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3542 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
3543 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
3545 /* The template parameters for this new template are all the
3546 template parameters for the old template, except the
3547 outermost level of parameters. */
3548 for (new_parms
= &DECL_TEMPLATE_PARMS (tmpl
),
3549 parms
= DECL_TEMPLATE_PARMS (t
);
3550 TREE_CHAIN (parms
) != NULL_TREE
;
3551 new_parms
= &(TREE_CHAIN (*new_parms
)),
3552 parms
= TREE_CHAIN (parms
))
3555 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
3558 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
3560 tree default_value
=
3561 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
3563 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
3565 TREE_VEC_ELT (new_vec
, i
)
3566 = build_tree_list (tsubst (default_value
, args
,
3568 tsubst (parm_decl
, args
, nargs
,
3574 tree_cons (build_int_2 (0,
3576 (TREE_PURPOSE (parms
)) - 1),
3581 /* What should we do with the specializations of this member
3582 template? Are they specializations of this new template,
3583 or instantiations of the templates they previously were?
3584 this new template? And where should their
3585 DECL_TI_TEMPLATES point? */
3586 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
3587 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
3589 spec
= TREE_CHAIN (spec
))
3591 /* It helps to consider example here. Consider:
3602 Now, for example, we are instantiating S<int>::f(U u).
3603 We want to make a template:
3608 It will have a specialization, for the case U = int*, of
3612 void S<int>::f<int*>(int*);
3614 This specialization will be an instantiation of
3615 the specialization given in the declaration of S, with
3616 argument list int*. */
3618 tree fn
= TREE_VALUE (spec
);
3622 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
3623 /* Instantiations are on the same list, but they're of
3624 no concern to us. */
3627 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
, nargs
,
3629 new_fn
= tsubst (DECL_RESULT (fn
), args
, nargs
,
3631 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) =
3632 perm_tree_cons (spec_args
, new_fn
,
3633 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
3636 /* Record this partial instantiation. */
3637 register_specialization (tmpl
, t
, args
);
3647 tree tmpl
= NULL_TREE
;
3650 if (DECL_CONTEXT (t
) != NULL_TREE
3651 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
3653 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
3657 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
3658 type
= tsubst (type
, args
, nargs
, in_decl
);
3664 type
= tsubst (type
, args
, nargs
, in_decl
);
3667 /* If we are instantiating a specialization, get the other args. */
3668 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
3672 tmpl
= DECL_TI_TEMPLATE (t
);
3674 /* Start by getting the innermost args. */
3675 argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
3677 /* If tmpl is an instantiation of a member template, tack on
3678 the args for the enclosing class. NOTE: this will change
3679 for member class templates. The generalized procedure
3680 is to grab the outer args, then tack on the current args,
3681 then any specialized args. */
3682 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
3684 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
3685 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
3687 /* In this case, we are instantiating a
3688 specialization. The innermost template args are
3689 already given by the specialization. */
3690 argvec
= add_to_template_args (argvec
, DECL_TI_ARGS (tmpl
));
3693 /* Do we already have this instantiation? */
3694 spec
= retrieve_specialization (tmpl
, argvec
);
3699 /* We do NOT check for matching decls pushed separately at this
3700 point, as they may not represent instantiations of this
3701 template, and in any case are considered separate under the
3702 discrete model. Instead, see add_maybe_template. */
3706 DECL_USE_TEMPLATE (r
) = 0;
3707 TREE_TYPE (r
) = type
;
3710 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
3711 DECL_CLASS_CONTEXT (r
) = ctx
;
3713 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
3714 IDENTIFIER_POINTER (DECL_NAME (r
)),
3715 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
3717 /* Type-conversion operator. Reconstruct the name, in
3718 case it's the name of one of the template's parameters. */
3719 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
3722 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
3724 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
3725 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
3726 buf
= (char *) alloca (strlen (dbuf
)
3727 + sizeof (DESTRUCTOR_DECL_PREFIX
));
3728 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
3731 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
3735 /* Instantiations of template functions must be mangled
3736 specially, in order to conform to 14.5.5.1
3737 [temp.over.link]. We use in_decl below rather than
3738 DECL_TI_TEMPLATE (r) because the latter is set to
3739 NULL_TREE in instantiate_decl. */
3743 if (DECL_TEMPLATE_INFO (r
))
3744 tmpl
= DECL_TI_TEMPLATE (r
);
3748 /* tmpl will be NULL if this is a specialization of a
3749 member function of a template class. */
3750 if (name_mangling_version
< 1
3751 || tmpl
== NULL_TREE
3752 || (member
&& !is_member_template (tmpl
)
3753 && !DECL_TEMPLATE_INFO (tmpl
)))
3755 arg_types
= TYPE_ARG_TYPES (type
);
3756 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
3757 arg_types
= hash_tree_chain
3758 (build_pointer_type (DECL_CONTEXT (r
)),
3761 DECL_ASSEMBLER_NAME (r
)
3762 = build_decl_overload (DECL_NAME (r
), arg_types
,
3770 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
3772 /* We pass the outermost template parameters to
3773 build_template_decl_overload, since the innermost
3774 template parameters are still just template
3775 parameters; there are no corresponding subsitution
3776 arguments. Levels of parms that have been bound
3777 before are not represented in DECL_TEMPLATE_PARMS. */
3778 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
3779 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
3780 tparms
= TREE_CHAIN (tparms
);
3782 targs
= innermost_args (args
, 0);
3786 /* If the template is a specialization, then it is
3787 a member template specialization. We have
3790 template <class T> struct S {
3791 template <int i> void f();
3792 template <> void f<7>();
3795 and now we are forming S<double>::f<7>.
3796 Therefore, the template parameters of interest
3797 are those that are specialized by the template
3798 (i.e., the int), not those we are using to
3799 instantiate the template, i.e. the double. */
3800 tparms
= DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl
));
3801 targs
= DECL_TI_ARGS (tmpl
);
3804 my_friendly_assert (tparms
!= NULL_TREE
3805 && TREE_CODE (tparms
) == TREE_LIST
,
3807 tparms
= TREE_VALUE (tparms
);
3809 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
3810 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
3811 arg_types
= hash_tree_chain
3812 (build_pointer_type (DECL_CONTEXT (r
)),
3815 DECL_ASSEMBLER_NAME (r
)
3816 = build_template_decl_overload
3817 (DECL_NAME (r
), arg_types
,
3818 TREE_TYPE (TREE_TYPE (tmpl
)),
3819 tparms
, targs
, member
);
3823 make_decl_rtl (r
, NULL_PTR
, 1);
3825 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
3826 DECL_MAIN_VARIANT (r
) = r
;
3827 DECL_RESULT (r
) = NULL_TREE
;
3828 DECL_INITIAL (r
) = NULL_TREE
;
3830 TREE_STATIC (r
) = 0;
3831 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
3832 DECL_EXTERNAL (r
) = 1;
3833 DECL_INTERFACE_KNOWN (r
) = 0;
3834 DECL_DEFER_OUTPUT (r
) = 0;
3835 TREE_CHAIN (r
) = NULL_TREE
;
3836 DECL_CHAIN (r
) = NULL_TREE
;
3838 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
3839 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
3841 /* Look for matching decls for the moment. */
3842 if (! member
&& ! flag_ansi_overloading
)
3844 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
3847 if (decls
== NULL_TREE
)
3849 else if (is_overloaded_fn (decls
))
3850 for (decls
= get_first_fn (decls
); decls
;
3851 decls
= DECL_CHAIN (decls
))
3853 if (TREE_CODE (decls
) == FUNCTION_DECL
3854 && TREE_TYPE (decls
) == type
)
3863 int dcl_only
= ! DECL_INITIAL (d
);
3865 DECL_INITIAL (r
) = error_mark_node
;
3866 duplicate_decls (r
, d
);
3869 DECL_INITIAL (r
) = 0;
3873 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
3875 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
3877 /* If we're not using ANSI overloading, then we might have
3878 called duplicate_decls above, and gotten back an
3879 preexisting version of this function. We treat such a
3880 function as a specialization. Otherwise, we cleared
3881 both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
3882 this condition will be false. */
3883 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
3884 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
3886 SET_DECL_IMPLICIT_INSTANTIATION (r
);
3888 register_specialization (r
, tmpl
, argvec
);
3891 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
3892 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
3894 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
3895 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
3902 tree r
= copy_node (t
);
3903 TREE_TYPE (r
) = type
;
3904 DECL_INITIAL (r
) = TREE_TYPE (r
);
3905 DECL_CONTEXT (r
) = NULL_TREE
;
3906 #ifdef PROMOTE_PROTOTYPES
3907 if ((TREE_CODE (type
) == INTEGER_TYPE
3908 || TREE_CODE (type
) == ENUMERAL_TYPE
)
3909 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
3910 DECL_ARG_TYPE (r
) = integer_type_node
;
3913 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
3919 tree r
= copy_node (t
);
3920 TREE_TYPE (r
) = type
;
3923 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
3925 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
3926 TREE_CHAIN (r
) = NULL_TREE
;
3932 tree r
= copy_node (t
);
3934 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
3935 TREE_CHAIN (r
) = NULL_TREE
;
3942 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
3944 /* Do we already have this instantiation? */
3945 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
3947 tree tmpl
= DECL_TI_TEMPLATE (t
);
3948 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
3950 for (; decls
; decls
= TREE_CHAIN (decls
))
3951 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
3952 return TREE_VALUE (decls
);
3956 TREE_TYPE (r
) = type
;
3957 DECL_CONTEXT (r
) = ctx
;
3958 if (TREE_STATIC (r
))
3959 DECL_ASSEMBLER_NAME (r
)
3960 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
3962 /* Don't try to expand the initializer until someone tries to use
3963 this variable; otherwise we run into circular dependencies. */
3964 DECL_INITIAL (r
) = NULL_TREE
;
3969 if (DECL_LANG_SPECIFIC (r
))
3972 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
3975 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
3977 tree tmpl
= DECL_TI_TEMPLATE (t
);
3978 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
3979 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
3981 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
3982 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
3983 SET_DECL_IMPLICIT_INSTANTIATION (r
);
3985 TREE_CHAIN (r
) = NULL_TREE
;
3990 if (t
== TYPE_NAME (TREE_TYPE (t
)))
3991 return TYPE_NAME (type
);
3994 tree r
= copy_node (t
);
3995 TREE_TYPE (r
) = type
;
3996 DECL_CONTEXT (r
) = current_class_type
;
3997 TREE_CHAIN (r
) = NULL_TREE
;
4003 tree purpose
, value
, chain
, result
;
4004 int via_public
, via_virtual
, via_protected
;
4006 if (t
== void_list_node
)
4009 via_public
= TREE_VIA_PUBLIC (t
);
4010 via_protected
= TREE_VIA_PROTECTED (t
);
4011 via_virtual
= TREE_VIA_VIRTUAL (t
);
4013 purpose
= TREE_PURPOSE (t
);
4015 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
4016 value
= TREE_VALUE (t
);
4018 value
= tsubst (value
, args
, nargs
, in_decl
);
4019 chain
= TREE_CHAIN (t
);
4020 if (chain
&& chain
!= void_type_node
)
4021 chain
= tsubst (chain
, args
, nargs
, in_decl
);
4022 if (purpose
== TREE_PURPOSE (t
)
4023 && value
== TREE_VALUE (t
)
4024 && chain
== TREE_CHAIN (t
))
4026 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
4027 purpose
, value
, chain
);
4028 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
4032 if (type
!= NULL_TREE
)
4038 if (type
== TREE_TYPE (t
))
4041 TREE_TYPE (t
) = complete_type (type
);
4042 if (IS_AGGR_TYPE (type
))
4044 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
4045 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
4046 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
4047 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
4052 /* Otherwise, a vector of template arguments. */
4054 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
4055 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
4057 bzero ((char *) elts
, len
* sizeof (tree
));
4059 for (i
= 0; i
< len
; i
++)
4061 elts
[i
] = maybe_fold_nontype_arg
4062 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
));
4064 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
4071 t
= make_tree_vec (len
);
4072 for (i
= 0; i
< len
; i
++)
4073 TREE_VEC_ELT (t
, i
) = elts
[i
];
4078 case REFERENCE_TYPE
:
4081 enum tree_code code
;
4083 if (type
== TREE_TYPE (t
))
4086 code
= TREE_CODE (t
);
4087 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4089 static int last_line
= 0;
4090 static char* last_file
= 0;
4092 /* We keep track of the last time we issued this error
4093 message to avoid spewing a ton of messages during a
4094 single bad template instantiation. */
4095 if (last_line
!= lineno
||
4096 last_file
!= input_filename
)
4098 cp_error ("cannot form type %s to reference type %T during template instantiation",
4099 (code
== POINTER_TYPE
) ? "pointer" : "reference",
4102 last_file
= input_filename
;
4105 /* Use the underlying type in an attempt at error
4106 recovery; maybe the user meant vector<int> and wrote
4107 vector<int&>, or some such. */
4108 if (code
== REFERENCE_TYPE
)
4111 r
= build_pointer_type (TREE_TYPE (type
));
4113 else if (code
== POINTER_TYPE
)
4114 r
= build_pointer_type (type
);
4116 r
= build_reference_type (type
);
4117 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
4119 /* Will this ever be needed for TYPE_..._TO values? */
4124 return build_offset_type
4125 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
4129 tree values
= TYPE_ARG_TYPES (t
);
4130 tree context
= TYPE_CONTEXT (t
);
4131 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
4134 /* Don't bother recursing if we know it won't change anything. */
4135 if (values
!= void_list_node
)
4137 /* This should probably be rewritten to use hash_tree_cons for
4138 the memory savings. */
4139 tree first
= NULL_TREE
;
4140 tree last
= NULL_TREE
;
4142 for (; values
&& values
!= void_list_node
;
4143 values
= TREE_CHAIN (values
))
4145 tree value
= TYPE_MAIN_VARIANT (type_decays_to
4146 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
4147 /* Don't instantiate default args unless they are used.
4148 Handle it in build_over_call instead. */
4149 tree purpose
= TREE_PURPOSE (values
);
4150 tree x
= build_tree_list (purpose
, value
);
4153 TREE_CHAIN (last
) = x
;
4159 if (values
== void_list_node
)
4160 TREE_CHAIN (last
) = void_list_node
;
4165 context
= tsubst (context
, args
, nargs
, in_decl
);
4166 /* Could also optimize cases where return value and
4167 values have common elements (e.g., T min(const &T, const T&). */
4169 /* If the above parameters haven't changed, just return the type. */
4170 if (type
== TREE_TYPE (t
)
4171 && values
== TYPE_VALUES (t
)
4172 && context
== TYPE_CONTEXT (t
))
4175 /* Construct a new type node and return it. */
4176 if (TREE_CODE (t
) == FUNCTION_TYPE
4177 && context
== NULL_TREE
)
4179 fntype
= build_function_type (type
, values
);
4181 else if (context
== NULL_TREE
)
4183 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
4184 args
, nargs
, in_decl
);
4185 fntype
= build_cplus_method_type (base
, type
,
4186 TREE_CHAIN (values
));
4190 fntype
= make_node (TREE_CODE (t
));
4191 TREE_TYPE (fntype
) = type
;
4192 TYPE_CONTEXT (fntype
) = context
;
4193 TYPE_VALUES (fntype
) = values
;
4194 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
4195 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
4196 TYPE_MODE (fntype
) = TYPE_MODE (t
);
4197 if (TYPE_METHOD_BASETYPE (t
))
4198 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
4199 args
, nargs
, in_decl
);
4200 /* Need to generate hash value. */
4201 my_friendly_abort (84);
4203 fntype
= build_type_variant (fntype
,
4208 raises
= tsubst (raises
, args
, nargs
, in_decl
);
4209 fntype
= build_exception_variant (fntype
, raises
);
4215 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
4217 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
4219 r
= build_cplus_array_type (type
, domain
);
4225 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
4226 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4227 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
4231 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
4232 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
4236 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
4237 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
4238 return cp_build_type_variant
4239 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
4240 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
4244 return make_pointer_declarator
4245 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4248 return make_reference_declarator
4249 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4252 return build_parse_node
4253 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4254 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4257 return make_call_declarator
4258 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4259 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4260 TREE_OPERAND (t
, 2),
4261 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
4264 return build_parse_node
4265 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4266 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4269 sorry ("use of `%s' in template",
4270 tree_code_name
[(int) TREE_CODE (t
)]);
4271 return error_mark_node
;
4278 emit_line_note (input_filename
, lineno
);
4282 expand_start_bindings (0);
4289 int saved_warn_unused
= 0;
4291 if (processing_template_decl
)
4293 saved_warn_unused
= warn_unused
;
4296 expand_end_bindings (getdecls (), kept_level_p (), 0);
4297 if (processing_template_decl
)
4298 warn_unused
= saved_warn_unused
;
4299 t
= poplevel (kept_level_p (), 1, 0);
4304 /* Like tsubst, but deals with expressions. This function just replaces
4305 template parms; to finish processing the resultant expression, use
4309 tsubst_copy (t
, args
, nargs
, in_decl
)
4314 enum tree_code code
;
4316 if (t
== NULL_TREE
|| t
== error_mark_node
)
4319 code
= TREE_CODE (t
);
4324 return do_identifier (DECL_NAME (t
), 0);
4328 if (DECL_CONTEXT (t
))
4330 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
4331 if (ctx
== current_function_decl
)
4332 return lookup_name (DECL_NAME (t
), 0);
4333 else if (ctx
!= DECL_CONTEXT (t
))
4334 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
4340 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4341 t
= tsubst (t
, args
, nargs
, in_decl
);
4346 if (is_member_template (t
))
4347 return tsubst (t
, args
, nargs
, in_decl
);
4352 case IDENTIFIER_NODE
:
4353 return do_identifier (t
, 0);
4357 case REINTERPRET_CAST_EXPR
:
4358 case CONST_CAST_EXPR
:
4359 case STATIC_CAST_EXPR
:
4360 case DYNAMIC_CAST_EXPR
:
4362 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
4363 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4366 case PREDECREMENT_EXPR
:
4367 case PREINCREMENT_EXPR
:
4368 case POSTDECREMENT_EXPR
:
4369 case POSTINCREMENT_EXPR
:
4371 case TRUTH_NOT_EXPR
:
4374 case CONVERT_EXPR
: /* Unary + */
4382 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4387 case TRUNC_DIV_EXPR
:
4389 case FLOOR_DIV_EXPR
:
4390 case ROUND_DIV_EXPR
:
4391 case EXACT_DIV_EXPR
:
4393 case BIT_ANDTC_EXPR
:
4396 case TRUNC_MOD_EXPR
:
4397 case FLOOR_MOD_EXPR
:
4398 case TRUTH_ANDIF_EXPR
:
4399 case TRUTH_ORIF_EXPR
:
4400 case TRUTH_AND_EXPR
:
4421 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4422 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4426 tree fn
= TREE_OPERAND (t
, 0);
4427 if (is_overloaded_fn (fn
))
4428 fn
= tsubst_copy (get_first_fn (fn
), args
, nargs
, in_decl
);
4430 /* Sometimes FN is a LOOKUP_EXPR. */
4431 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
4433 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4437 case METHOD_CALL_EXPR
:
4439 tree name
= TREE_OPERAND (t
, 0);
4440 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4442 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4443 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
4445 else if (TREE_CODE (name
) == SCOPE_REF
4446 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
4448 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4449 name
= TREE_OPERAND (name
, 1);
4450 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
4451 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
4452 name
= build_nt (SCOPE_REF
, base
, name
);
4455 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4457 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4458 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
4467 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4468 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4469 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
4471 if (code
== BIND_EXPR
&& !processing_template_decl
)
4473 /* This processing should really occur in tsubst_expr,
4474 However, tsubst_expr does not recurse into expressions,
4475 since it assumes that there aren't any statements
4476 inside them. Instead, it simply calls
4477 build_expr_from_tree. So, we need to expand the
4479 tree rtl_exp
= expand_start_stmt_expr();
4480 tsubst_expr (TREE_OPERAND (r
, 1), args
, nargs
, in_decl
);
4481 rtl_exp
= expand_end_stmt_expr (rtl_exp
);
4482 TREE_SIDE_EFFECTS (rtl_exp
) = 1;
4483 return build (BIND_EXPR
, TREE_TYPE (rtl_exp
),
4484 NULL_TREE
, rtl_exp
, TREE_OPERAND (r
, 2));
4493 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4494 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4495 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
4496 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
4503 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4504 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4505 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
4506 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
4510 case TEMPLATE_ID_EXPR
:
4512 /* Substituted template arguments */
4513 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4515 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
4516 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
4518 return lookup_template_function
4519 (tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
), targs
);
4524 tree purpose
, value
, chain
;
4526 if (t
== void_list_node
)
4529 purpose
= TREE_PURPOSE (t
);
4531 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
4532 value
= TREE_VALUE (t
);
4534 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
4535 chain
= TREE_CHAIN (t
);
4536 if (chain
&& chain
!= void_type_node
)
4537 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
4538 if (purpose
== TREE_PURPOSE (t
)
4539 && value
== TREE_VALUE (t
)
4540 && chain
== TREE_CHAIN (t
))
4542 return tree_cons (purpose
, value
, chain
);
4549 case TEMPLATE_TYPE_PARM
:
4550 case TEMPLATE_TEMPLATE_PARM
:
4551 case TEMPLATE_PARM_INDEX
:
4553 case REFERENCE_TYPE
:
4560 return tsubst (t
, args
, nargs
, in_decl
);
4562 case IDENTIFIER_NODE
:
4563 if (IDENTIFIER_TYPENAME_P (t
))
4564 return build_typename_overload
4565 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
4571 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
4572 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
4579 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
4582 tsubst_expr (t
, args
, nargs
, in_decl
)
4587 if (t
== NULL_TREE
|| t
== error_mark_node
)
4590 if (processing_template_decl
)
4591 return tsubst_copy (t
, args
, nargs
, in_decl
);
4593 switch (TREE_CODE (t
))
4596 lineno
= TREE_COMPLEXITY (t
);
4597 emit_line_note (input_filename
, lineno
);
4599 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4604 lineno
= TREE_COMPLEXITY (t
);
4605 emit_line_note (input_filename
, lineno
);
4606 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4607 /* Do default conversion if safe and possibly important,
4608 in case within ({...}). */
4609 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
4610 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
4611 t
= default_conversion (t
);
4612 cplus_expand_expr_stmt (t
);
4619 int i
= suspend_momentary ();
4622 lineno
= TREE_COMPLEXITY (t
);
4623 emit_line_note (input_filename
, lineno
);
4625 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4626 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
4627 TREE_OPERAND (t
, 2) != 0);
4628 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
4630 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
4631 resume_momentary (i
);
4638 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
4639 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4640 int cond_scope
= (TREE_OPERAND (t
, 1)
4641 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
4643 lineno
= TREE_COMPLEXITY (t
);
4644 emit_line_note (input_filename
, lineno
);
4647 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
4648 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4650 emit_line_note (input_filename
, lineno
);
4651 expand_start_loop_continue_elsewhere (1);
4655 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4656 emit_line_note (input_filename
, lineno
);
4658 expand_exit_loop_if_false (0, condition_conversion (tmp
));
4662 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
4665 emit_line_note (input_filename
, lineno
);
4666 expand_loop_continue_here ();
4667 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
4669 cplus_expand_expr_stmt (tmp
);
4682 lineno
= TREE_COMPLEXITY (t
);
4684 emit_line_note (input_filename
, lineno
);
4685 expand_start_loop (1);
4687 cond
= TREE_OPERAND (t
, 0);
4688 if (TREE_CODE (cond
) == DECL_STMT
)
4690 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
4691 emit_line_note (input_filename
, lineno
);
4692 expand_exit_loop_if_false (0, condition_conversion (cond
));
4694 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
4696 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4708 lineno
= TREE_COMPLEXITY (t
);
4710 emit_line_note (input_filename
, lineno
);
4711 expand_start_loop_continue_elsewhere (1);
4713 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4714 expand_loop_continue_here ();
4716 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4717 emit_line_note (input_filename
, lineno
);
4718 expand_exit_loop_if_false (0, condition_conversion (cond
));
4729 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4731 lineno
= TREE_COMPLEXITY (t
);
4734 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4735 emit_line_note (input_filename
, lineno
);
4736 expand_start_cond (condition_conversion (tmp
), 0);
4738 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
4739 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4741 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
4743 expand_start_else ();
4744 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4758 tree substmt
= TREE_OPERAND (t
, 0);
4760 lineno
= TREE_COMPLEXITY (t
);
4762 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
4765 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
4766 tsubst_expr (substmt
, args
, nargs
, in_decl
);
4768 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
4774 lineno
= TREE_COMPLEXITY (t
);
4775 emit_line_note (input_filename
, lineno
);
4776 if (! expand_exit_something ())
4777 error ("break statement not within loop or switch");
4781 lineno
= TREE_COMPLEXITY (t
);
4782 emit_line_note (input_filename
, lineno
);
4783 if (! expand_continue_loop (0))
4784 error ("continue statement not within a loop");
4790 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
4792 lineno
= TREE_COMPLEXITY (t
);
4795 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4796 emit_line_note (input_filename
, lineno
);
4797 c_expand_start_case (val
);
4800 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
4801 tsubst_expr (tmp
, args
, nargs
, in_decl
);
4803 expand_end_case (val
);
4814 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
4815 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
4819 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
4826 lineno
= TREE_COMPLEXITY (t
);
4827 emit_line_note (input_filename
, lineno
);
4828 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
4830 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
4831 TREE_USED (decl
) = 1;
4835 expand_computed_goto
4836 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
4840 lineno
= TREE_COMPLEXITY (t
);
4841 emit_line_note (input_filename
, lineno
);
4842 expand_start_try_stmts ();
4843 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
4844 expand_start_all_catch ();
4846 tree handler
= TREE_OPERAND (t
, 1);
4847 for (; handler
; handler
= TREE_CHAIN (handler
))
4848 tsubst_expr (handler
, args
, nargs
, in_decl
);
4850 expand_end_all_catch ();
4854 lineno
= TREE_COMPLEXITY (t
);
4856 if (TREE_OPERAND (t
, 0))
4858 tree d
= TREE_OPERAND (t
, 0);
4859 expand_start_catch_block
4860 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
4861 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
4864 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
4865 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
4866 expand_end_catch_block ();
4871 lineno
= TREE_COMPLEXITY (t
);
4873 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
4874 tsubst_enum (t
, args
, nargs
, NULL
);
4878 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
4884 instantiate_template (tmpl
, targ_ptr
)
4885 tree tmpl
, targ_ptr
;
4889 struct obstack
*old_fmp_obstack
;
4890 extern struct obstack
*function_maybepermanent_obstack
;
4892 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
4894 /* FIXME this won't work with member templates; we only have one level
4896 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
4898 /* Check to see if we already have this specialization. */
4899 tree spec
= retrieve_specialization (tmpl
, targ_ptr
);
4901 if (spec
!= NULL_TREE
)
4905 push_obstacks (&permanent_obstack
, &permanent_obstack
);
4906 old_fmp_obstack
= function_maybepermanent_obstack
;
4907 function_maybepermanent_obstack
= &permanent_obstack
;
4909 len
= DECL_NTPARMS (tmpl
);
4914 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
4915 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
4917 tree nt
= target_type (t
);
4918 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
4920 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
4921 cp_error (" trying to instantiate `%D'", tmpl
);
4922 fndecl
= error_mark_node
;
4926 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
4928 targ_ptr
= copy_to_permanent (targ_ptr
);
4930 /* substitute template parameters */
4931 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
4933 if (flag_external_templates
)
4934 add_pending_template (fndecl
);
4937 function_maybepermanent_obstack
= old_fmp_obstack
;
4943 /* Push the name of the class template into the scope of the instantiation. */
4946 overload_template_name (type
)
4949 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
4952 if (IDENTIFIER_CLASS_VALUE (id
)
4953 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
4956 decl
= build_decl (TYPE_DECL
, id
, type
);
4957 SET_DECL_ARTIFICIAL (decl
);
4958 pushdecl_class_level (decl
);
4962 /* Like type_unification but designed specially to handle conversion
4963 operators. The EXTRA_FN_ARG, if any, is the type of an additional
4964 parameter to be added to the beginning of FN's parameter list. */
4967 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
4968 strict
, extra_fn_arg
)
4969 tree fn
, explicit_targs
, targs
, args
, return_type
;
4974 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
4975 tree decl_arg_types
= args
;
4977 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
4979 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
4981 /* This is a template conversion operator. Use the return types
4982 as well as the argument types. */
4983 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
4984 TREE_TYPE (TREE_TYPE (fn
)),
4986 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
4991 if (extra_fn_arg
!= NULL_TREE
)
4992 fn_arg_types
= scratch_tree_cons (NULL_TREE
, extra_fn_arg
,
4995 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
4996 &TREE_VEC_ELT (targs
, 0),
5006 /* Type unification.
5008 We have a function template signature with one or more references to
5009 template parameters, and a parameter list we wish to fit to this
5010 template. If possible, produce a list of parameters for the template
5011 which will cause it to fit the supplied parameter list.
5013 Return zero for success, 2 for an incomplete match that doesn't resolve
5014 all the types, and 1 for complete failure. An error message will be
5015 printed only for an incomplete match.
5017 TPARMS[NTPARMS] is an array of template parameter types;
5018 TARGS[NTPARMS] is the array of template parameter values. PARMS is
5019 the function template's signature (using TEMPLATE_PARM_IDX nodes),
5020 and ARGS is the argument list we're trying to match against it.
5022 If SUBR is 1, we're being called recursively (to unify the arguments of
5023 a function or method parameter of a function template), so don't zero
5024 out targs and don't fail on an incomplete match.
5026 If STRICT is 1, the match must be exact (for casts of overloaded
5027 addresses, explicit instantiation, and more_specialized). */
5030 type_unification (tparms
, targs
, parms
, args
, targs_in
, nsubsts
,
5031 strict
, allow_incomplete
)
5032 tree tparms
, *targs
, parms
, args
, targs_in
;
5033 int *nsubsts
, strict
, allow_incomplete
;
5035 int ntparms
= TREE_VEC_LENGTH (tparms
);
5040 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
5042 if (targs_in
!= NULL_TREE
)
5045 arg_vec
= coerce_template_parms (tparms
, targs_in
, NULL_TREE
, 0,
5048 if (arg_vec
== error_mark_node
)
5052 i
< TREE_VEC_LENGTH (arg_vec
)
5053 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
5055 /* Insert the template argument. It is encoded as the operands
5056 of NOP_EXPRs so that unify can tell that it is an explicit
5058 targs
[i
] = build1 (NOP_EXPR
, NULL_TREE
, TREE_VEC_ELT (arg_vec
, i
));
5061 r
= type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, 0,
5062 strict
, allow_incomplete
);
5064 for (i
= 0, arg
= targs_in
;
5066 arg
= TREE_CHAIN (arg
), ++i
)
5067 if (TREE_CODE (targs
[i
]) == NOP_EXPR
)
5068 targs
[i
] = TREE_OPERAND (targs
[i
], 0);
5075 type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, subr
,
5076 strict
, allow_incomplete
)
5077 tree tparms
, *targs
, parms
, args
;
5078 int *nsubsts
, subr
, strict
, allow_incomplete
;
5082 int ntparms
= TREE_VEC_LENGTH (tparms
);
5084 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
5085 my_friendly_assert (parms
== NULL_TREE
5086 || TREE_CODE (parms
) == TREE_LIST
, 290);
5087 /* ARGS could be NULL (via a call from parse.y to
5088 build_x_function_call). */
5090 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
5091 my_friendly_assert (ntparms
> 0, 292);
5094 && parms
!= void_list_node
5096 && args
!= void_list_node
)
5098 parm
= TREE_VALUE (parms
);
5099 parms
= TREE_CHAIN (parms
);
5100 arg
= TREE_VALUE (args
);
5101 args
= TREE_CHAIN (args
);
5103 if (arg
== error_mark_node
)
5105 if (arg
== unknown_type_node
)
5108 /* Conversions will be performed on a function argument that
5109 corresponds with a function parameter that contains only
5110 non-deducible template parameters and explicitly specified
5111 template parameters. */
5112 if (! uses_template_parms (parm
))
5116 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5117 type
= TREE_TYPE (arg
);
5126 if (comptypes (parm
, type
, 1))
5130 /* It might work; we shouldn't check now, because we might
5131 get into infinite recursion. Overload resolution will
5139 if (TREE_CODE (arg
) == VAR_DECL
)
5140 arg
= TREE_TYPE (arg
);
5141 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
5142 arg
= TREE_TYPE (arg
);
5144 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5146 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
5147 if (TREE_CODE (arg
) == TREE_LIST
5148 && TREE_TYPE (arg
) == unknown_type_node
5149 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
5151 int nsubsts
, ntparms
;
5154 /* Have to back unify here */
5155 arg
= TREE_VALUE (arg
);
5157 ntparms
= DECL_NTPARMS (arg
);
5158 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
5159 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
5161 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
5163 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
5164 parm
, NULL_TREE
, &nsubsts
, strict
,
5167 arg
= TREE_TYPE (arg
);
5170 if (! flag_ansi
&& arg
== TREE_TYPE (null_node
))
5172 warning ("using type void* for NULL");
5173 arg
= ptr_type_node
;
5176 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
5177 arg
= TREE_TYPE (arg
);
5179 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
5181 if (TREE_CODE (arg
) == FUNCTION_TYPE
5182 || TREE_CODE (arg
) == METHOD_TYPE
)
5183 arg
= build_pointer_type (arg
);
5184 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
5185 arg
= build_pointer_type (TREE_TYPE (arg
));
5187 arg
= TYPE_MAIN_VARIANT (arg
);
5190 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
5198 /* Fail if we've reached the end of the parm list, and more args
5199 are present, and the parm list isn't variadic. */
5200 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
5202 /* Fail if parms are left and they don't have default values. */
5204 && parms
!= void_list_node
5205 && TREE_PURPOSE (parms
) == NULL_TREE
)
5208 for (i
= 0; i
< ntparms
; i
++)
5211 if (!allow_incomplete
)
5212 error ("incomplete type unification");
5218 /* Tail recursion is your friend. */
5221 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
5222 tree tparms
, *targs
, parm
, arg
;
5223 int *nsubsts
, ntparms
, strict
;
5227 /* I don't think this will do the right thing with respect to types.
5228 But the only case I've seen it in so far has been array bounds, where
5229 signedness is the only information lost, and I think that will be
5231 while (TREE_CODE (parm
) == NOP_EXPR
)
5232 parm
= TREE_OPERAND (parm
, 0);
5234 if (arg
== error_mark_node
)
5236 if (arg
== unknown_type_node
)
5241 switch (TREE_CODE (parm
))
5244 /* In a type which contains a nested-name-specifier, template
5245 argument values cannot be deduced for template parameters used
5246 within the nested-name-specifier. */
5249 case TEMPLATE_TYPE_PARM
:
5251 idx
= TEMPLATE_TYPE_IDX (parm
);
5252 /* Check for mixed types and values. */
5253 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
5256 if (!strict
&& targs
[idx
] != NULL_TREE
&&
5257 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
5258 /* An explicit template argument. Don't even try to match
5259 here; the overload resolution code will manage check to
5260 see whether the call is legal. */
5263 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
5264 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
5267 /* Template type parameters cannot contain cv-quals; i.e.
5268 template <class T> void f (T& a, T& b) will not generate
5269 void f (const int& a, const int& b). */
5270 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
5271 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
5273 arg
= TYPE_MAIN_VARIANT (arg
);
5276 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
5277 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
5278 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
5281 /* Simple cases: Value already set, does match or doesn't. */
5282 if (targs
[idx
] == arg
5284 && TREE_CODE (targs
[idx
]) == NOP_EXPR
5285 && TREE_OPERAND (targs
[idx
], 0) == arg
))
5287 else if (targs
[idx
])
5292 case TEMPLATE_TEMPLATE_PARM
:
5294 idx
= TEMPLATE_TYPE_IDX (parm
);
5295 /* Check for mixed types and values. */
5296 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TEMPLATE_DECL
)
5299 if (!strict
&& targs
[idx
] != NULL_TREE
&&
5300 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
5301 /* An explicit template argument. Don't even try to match
5302 here; the overload resolution code will manage check to
5303 see whether the call is legal. */
5306 if (CLASSTYPE_TEMPLATE_INFO (parm
))
5308 /* We arrive here when PARM does not involve template
5311 /* ARG must be constructed from a template class. */
5312 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
5316 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
5317 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
5318 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
5320 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
5323 /* The parameter and argument roles have to be switched here
5324 in order to handle default arguments properly. For example,
5325 template<template <class> class TT> void f(TT<int>)
5326 should be able to accept vector<int> which comes from
5327 template <class T, class Allcator = allocator>
5330 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1, 0)
5334 /* Deduce arguments T, i from TT<T> or TT<i>. */
5335 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
5337 tree t
= TREE_VEC_ELT (parmvec
, i
);
5338 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
5339 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
5340 && TREE_CODE (t
) != TEMPLATE_PARM_INDEX
)
5343 /* This argument can be deduced. */
5345 if (unify (tparms
, targs
, ntparms
, t
,
5346 TREE_VEC_ELT (argvec
, i
), nsubsts
, strict
))
5350 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
5353 /* Simple cases: Value already set, does match or doesn't. */
5354 if (targs
[idx
] == arg
5356 && TREE_CODE (targs
[idx
]) == NOP_EXPR
5357 && TREE_OPERAND (targs
[idx
], 0) == arg
))
5359 else if (targs
[idx
])
5364 case TEMPLATE_PARM_INDEX
:
5366 idx
= TEMPLATE_PARM_IDX (parm
);
5369 int i
= cp_tree_equal (targs
[idx
], arg
);
5375 my_friendly_abort (42);
5378 targs
[idx
] = copy_to_permanent (arg
);
5382 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
5383 return unify (tparms
, targs
, ntparms
, parm
,
5384 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
5386 if (TREE_CODE (arg
) != POINTER_TYPE
)
5388 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5391 case REFERENCE_TYPE
:
5392 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
5393 arg
= TREE_TYPE (arg
);
5394 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
5398 if (TREE_CODE (arg
) != ARRAY_TYPE
)
5400 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
5401 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
5403 if (TYPE_DOMAIN (parm
) != NULL_TREE
5404 && unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
),
5405 TYPE_DOMAIN (arg
), nsubsts
, strict
) != 0)
5407 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5415 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5418 if (TREE_CODE (parm
) == INTEGER_TYPE
)
5420 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
5421 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
5422 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
5424 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
5425 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
5426 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
5429 else if (TREE_CODE (parm
) == REAL_TYPE
5430 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
5433 /* As far as unification is concerned, this wins. Later checks
5434 will invalidate it if necessary. */
5437 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5438 /* Type INTEGER_CST can come from ordinary constant template args. */
5440 while (TREE_CODE (arg
) == NOP_EXPR
)
5441 arg
= TREE_OPERAND (arg
, 0);
5443 if (TREE_CODE (arg
) != INTEGER_CST
)
5445 return !tree_int_cst_equal (parm
, arg
);
5450 t1
= TREE_OPERAND (parm
, 0);
5451 t2
= TREE_OPERAND (parm
, 1);
5452 return unify (tparms
, targs
, ntparms
, t1
,
5453 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
5460 if (TREE_CODE (arg
) != TREE_VEC
)
5462 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
5464 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
5465 if (unify (tparms
, targs
, ntparms
,
5466 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
5473 if (TYPE_PTRMEMFUNC_FLAG (parm
))
5474 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
5475 arg
, nsubsts
, strict
);
5477 /* Allow trivial conversions. */
5478 if (TREE_CODE (arg
) != RECORD_TYPE
5479 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
5480 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
5483 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
5486 if (flag_ansi_overloading
&& ! strict
)
5487 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
5489 (CLASSTYPE_TEMPLATE_INFO (arg
)
5490 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
5492 if (! t
|| t
== error_mark_node
)
5495 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
5496 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
5498 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
5503 if (TREE_CODE (arg
) != METHOD_TYPE
)
5508 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
5511 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5512 TREE_TYPE (arg
), nsubsts
, strict
))
5514 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
5515 TYPE_ARG_TYPES (arg
), nsubsts
, 1,
5519 if (TREE_CODE (arg
) != OFFSET_TYPE
)
5521 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
5522 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
5524 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5525 TREE_TYPE (arg
), nsubsts
, strict
);
5528 if (arg
!= decl_constant_value (parm
))
5533 /* Matched cases are handled by the ARG == PARM test above. */
5537 sorry ("use of `%s' in template type unification",
5538 tree_code_name
[(int) TREE_CODE (parm
)]);
5544 mark_decl_instantiated (result
, extern_p
)
5548 if (DECL_TEMPLATE_INSTANTIATION (result
))
5549 SET_DECL_EXPLICIT_INSTANTIATION (result
);
5551 if (TREE_CODE (result
) != FUNCTION_DECL
)
5552 /* The TREE_PUBLIC flag for function declarations will have been
5553 set correctly by tsubst. */
5554 TREE_PUBLIC (result
) = 1;
5558 DECL_INTERFACE_KNOWN (result
) = 1;
5559 DECL_NOT_REALLY_EXTERN (result
) = 1;
5561 /* For WIN32 we also want to put explicit instantiations in
5562 linkonce sections. */
5563 if (supports_one_only () && ! SUPPORTS_WEAK
&& TREE_PUBLIC (result
))
5564 make_decl_one_only (result
);
5566 else if (TREE_CODE (result
) == FUNCTION_DECL
)
5567 mark_inline_for_output (result
);
5570 /* Given two function templates PAT1 and PAT2, and explicit template
5571 arguments EXPLICIT_ARGS return:
5573 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
5574 -1 if PAT2 is more specialized than PAT1.
5575 0 if neither is more specialized. */
5578 more_specialized (pat1
, pat2
, explicit_args
)
5579 tree pat1
, pat2
, explicit_args
;
5584 targs
= get_bindings (pat1
, pat2
, explicit_args
);
5590 targs
= get_bindings (pat2
, pat1
, explicit_args
);
5599 /* Given two class template specialization list nodes PAT1 and PAT2, return:
5601 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
5602 -1 if PAT2 is more specialized than PAT1.
5603 0 if neither is more specialized. */
5606 more_specialized_class (pat1
, pat2
)
5612 targs
= get_class_bindings
5613 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
5617 targs
= get_class_bindings
5618 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
5625 /* Return the template arguments that will produce the function signature
5626 DECL from the function template FN, with the explicit template
5627 arguments EXPLICIT_ARGS. */
5630 get_bindings (fn
, decl
, explicit_args
)
5631 tree fn
, decl
, explicit_args
;
5633 int ntparms
= DECL_NTPARMS (fn
);
5634 tree targs
= make_scratch_vec (ntparms
);
5635 tree decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
5636 tree extra_fn_arg
= NULL_TREE
;
5639 if (DECL_STATIC_FUNCTION_P (fn
)
5640 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
5642 /* Sometimes we are trying to figure out what's being
5643 specialized by a declaration that looks like a method, and it
5644 turns out to be a static member function. */
5645 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn
))
5646 && !is_member_template (fn
))
5647 /* The natural thing to do here seems to be to remove the
5648 spurious `this' parameter from the DECL, but that prevents
5649 unification from making use of the class type. So,
5650 instead, we have fn_type_unification add to the parameters
5652 extra_fn_arg
= build_pointer_type (DECL_REAL_CONTEXT (fn
));
5654 /* In this case, though, adding the extra_fn_arg can confuse
5655 things, so we remove from decl_arg_types instead. */
5656 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
5659 i
= fn_type_unification (fn
, explicit_args
, targs
,
5661 TREE_TYPE (TREE_TYPE (decl
)),
5667 /* Check to see that the resulting return type is also OK. */
5668 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
5673 if (!comptypes (t
, TREE_TYPE (TREE_TYPE (decl
)), 1))
5683 get_class_bindings (tparms
, parms
, args
)
5684 tree tparms
, parms
, args
;
5686 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
5687 tree vec
= make_temp_vec (ntparms
);
5689 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
5691 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
5692 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
5702 for (i
= 0; i
< ntparms
; ++i
)
5703 if (! TREE_VEC_ELT (vec
, i
))
5709 /* Return the most specialized of the list of templates in FNS that can
5710 produce an instantiation matching DECL, given the explicit template
5711 arguments EXPLICIT_ARGS. */
5714 most_specialized (fns
, decl
, explicit_args
)
5715 tree fns
, decl
, explicit_args
;
5717 tree fn
, champ
, args
, *p
;
5720 for (p
= &fns
; *p
; )
5722 args
= get_bindings (TREE_VALUE (*p
), decl
, explicit_args
);
5725 p
= &TREE_CHAIN (*p
);
5728 *p
= TREE_CHAIN (*p
);
5735 champ
= TREE_VALUE (fn
);
5736 fn
= TREE_CHAIN (fn
);
5737 for (; fn
; fn
= TREE_CHAIN (fn
))
5739 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5746 fn
= TREE_CHAIN (fn
);
5748 return error_mark_node
;
5750 champ
= TREE_VALUE (fn
);
5754 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
5756 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
5758 return error_mark_node
;
5764 /* Return the most specialized of the class template specializations in
5765 SPECS that can produce an instantiation matching ARGS. */
5768 most_specialized_class (specs
, mainargs
)
5769 tree specs
, mainargs
;
5771 tree list
= NULL_TREE
, t
, args
, champ
;
5774 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
5776 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
5779 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
5780 TREE_TYPE (list
) = TREE_TYPE (t
);
5790 for (; t
; t
= TREE_CHAIN (t
))
5792 fate
= more_specialized_class (champ
, t
);
5801 return error_mark_node
;
5807 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
5809 fate
= more_specialized_class (champ
, t
);
5811 return error_mark_node
;
5817 /* called from the parser. */
5820 do_decl_instantiation (declspecs
, declarator
, storage
)
5821 tree declspecs
, declarator
, storage
;
5823 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
5826 tree result
= NULL_TREE
;
5829 if (! DECL_LANG_SPECIFIC (decl
))
5831 cp_error ("explicit instantiation of non-template `%#D'", decl
);
5835 /* If we've already seen this template instance, use it. */
5836 if (TREE_CODE (decl
) == VAR_DECL
)
5838 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
5839 if (result
&& TREE_CODE (result
) != VAR_DECL
)
5842 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
5844 cp_error ("explicit instantiation of `%#D'", decl
);
5847 else if (DECL_TEMPLATE_INSTANTIATION (decl
))
5852 cp_error ("no matching template for `%D' found", decl
);
5856 if (! DECL_TEMPLATE_INFO (result
))
5858 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
5862 if (flag_external_templates
)
5865 if (storage
== NULL_TREE
)
5867 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5870 cp_error ("storage class `%D' applied to template instantiation",
5873 mark_decl_instantiated (result
, extern_p
);
5874 repo_template_instantiated (result
, extern_p
);
5876 instantiate_decl (result
);
5880 mark_class_instantiated (t
, extern_p
)
5884 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
5885 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
5886 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
5887 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
5888 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
5891 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
5892 rest_of_type_compilation (t
, 1);
5897 do_type_instantiation (t
, storage
)
5904 if (TREE_CODE (t
) == TYPE_DECL
)
5907 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
5909 cp_error ("explicit instantiation of non-template type `%T'", t
);
5915 /* With -fexternal-templates, explicit instantiations are treated the same
5916 as implicit ones. */
5917 if (flag_external_templates
)
5920 if (TYPE_SIZE (t
) == NULL_TREE
)
5922 cp_error ("explicit instantiation of `%#T' before definition of template",
5927 if (storage
== NULL_TREE
)
5929 else if (storage
== ridpointers
[(int) RID_INLINE
])
5931 else if (storage
== ridpointers
[(int) RID_EXTERN
])
5933 else if (storage
== ridpointers
[(int) RID_STATIC
])
5937 cp_error ("storage class `%D' applied to template instantiation",
5942 /* We've already instantiated this. */
5943 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
5947 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
5949 mark_class_instantiated (t
, extern_p
);
5950 repo_template_instantiated (t
, extern_p
);
5960 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5961 if (TREE_CODE (tmp
) == FUNCTION_DECL
5962 && DECL_TEMPLATE_INSTANTIATION (tmp
))
5964 mark_decl_instantiated (tmp
, extern_p
);
5965 repo_template_instantiated (tmp
, extern_p
);
5967 instantiate_decl (tmp
);
5970 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5971 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
5973 mark_decl_instantiated (tmp
, extern_p
);
5974 repo_template_instantiated (tmp
, extern_p
);
5976 instantiate_decl (tmp
);
5979 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5980 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
5981 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
5985 /* Produce the definition of D, a _DECL generated from a template. */
5988 instantiate_decl (d
)
5991 tree ti
= DECL_TEMPLATE_INFO (d
);
5992 tree tmpl
= TI_TEMPLATE (ti
);
5993 tree args
= TI_ARGS (ti
);
5995 tree decl_pattern
, code_pattern
;
5997 int nested
= in_function_p ();
5999 int pattern_defined
;
6001 char *file
= input_filename
;
6004 DECL_TEMPLATE_INSTANTIATION (td
)
6005 /* This next clause handles friend templates defined inside
6006 class templates. The friend templates are not really
6007 instantiations from the point of view of the language, but
6008 they are instantiations from the point of view of the
6010 || (DECL_TEMPLATE_INFO (td
) && !DECL_TEMPLATE_SPECIALIZATION (td
));
6012 td
= DECL_TI_TEMPLATE (td
);
6014 /* In the case of a member template, decl_pattern is the partially
6015 instantiated declaration (in the instantiated class), and code_pattern
6016 is the original template definition. */
6017 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
6018 code_pattern
= DECL_TEMPLATE_RESULT (td
);
6020 if (TREE_CODE (d
) == FUNCTION_DECL
)
6022 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
6023 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
6027 d_defined
= ! DECL_IN_AGGR_P (d
);
6028 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
6034 if (TREE_CODE (d
) == FUNCTION_DECL
)
6036 tree spec
= retrieve_specialization (tmpl
, args
);
6038 if (spec
!= NULL_TREE
6039 && DECL_TEMPLATE_SPECIALIZATION (spec
))
6043 /* This needs to happen before any tsubsting. */
6044 if (! push_tinst_level (d
))
6047 push_to_top_level ();
6048 lineno
= DECL_SOURCE_LINE (d
);
6049 input_filename
= DECL_SOURCE_FILE (d
);
6051 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
6052 variable is a static const initialized in the class body. */
6053 if (TREE_CODE (d
) == VAR_DECL
6054 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
6056 pushclass (DECL_CONTEXT (d
), 2);
6057 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6058 TREE_VEC_LENGTH (args
), tmpl
);
6059 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, LOOKUP_NORMAL
);
6062 if (pattern_defined
)
6064 repo_template_used (d
);
6066 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
6068 if (flag_alt_external_templates
)
6070 if (interface_unknown
)
6071 warn_if_unknown_interface (d
);
6073 else if (DECL_INTERFACE_KNOWN (code_pattern
))
6075 DECL_INTERFACE_KNOWN (d
) = 1;
6076 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
6079 warn_if_unknown_interface (code_pattern
);
6083 import_export_decl (d
);
6086 /* Reject all external templates except inline functions. */
6087 if (DECL_INTERFACE_KNOWN (d
)
6088 && ! DECL_NOT_REALLY_EXTERN (d
)
6089 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
6092 /* Defer all templates except inline functions used in another function. */
6093 if (! pattern_defined
6094 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
6097 add_pending_template (d
);
6101 lineno
= DECL_SOURCE_LINE (d
);
6102 input_filename
= DECL_SOURCE_FILE (d
);
6104 /* Trick tsubst into giving us a new decl in case the template changed. */
6105 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
6106 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
6107 temp
= innermost_args (args
, DECL_TEMPLATE_SPECIALIZATION (decl_pattern
));
6108 td
= tsubst (decl_pattern
, temp
, 0, tmpl
);
6109 SET_DECL_IMPLICIT_INSTANTIATION (td
);
6110 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
6112 /* And set up DECL_INITIAL, since tsubst doesn't. */
6113 if (TREE_CODE (td
) == VAR_DECL
)
6115 pushclass (DECL_CONTEXT (d
), 2);
6116 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6117 TREE_VEC_LENGTH (args
), tmpl
);
6121 if (TREE_CODE (d
) == FUNCTION_DECL
)
6123 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
6125 DECL_INITIAL (td
) = error_mark_node
;
6127 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
6128 /* Set up the information about what is being specialized. */
6129 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
6131 duplicate_decls (td
, d
);
6132 if (TREE_CODE (d
) == FUNCTION_DECL
)
6133 DECL_INITIAL (td
) = 0;
6135 if (TREE_CODE (d
) == VAR_DECL
)
6137 DECL_IN_AGGR_P (d
) = 0;
6138 if (DECL_INTERFACE_KNOWN (d
))
6139 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
6142 DECL_EXTERNAL (d
) = 1;
6143 DECL_NOT_REALLY_EXTERN (d
) = 1;
6145 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
6147 else if (TREE_CODE (d
) == FUNCTION_DECL
)
6149 tree t
= DECL_SAVED_TREE (code_pattern
);
6151 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
6152 store_parm_decls ();
6154 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
6157 (TREE_OPERAND (t
, 0),
6158 tsubst_expr (TREE_OPERAND (t
, 1), args
,
6159 TREE_VEC_LENGTH (args
), tmpl
));
6163 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
6165 current_member_init_list
6166 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
6167 current_base_init_list
6168 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
6173 /* Always keep the BLOCK node associated with the outermost
6174 pair of curly braces of a function. These are needed
6175 for correct operation of dwarfout.c. */
6178 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
6179 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
6181 finish_function (lineno
, 0, nested
);
6186 input_filename
= file
;
6188 pop_from_top_level ();
6195 tsubst_chain (t
, argvec
)
6200 tree first
= tsubst (t
, argvec
,
6201 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
6204 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
6206 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
6207 TREE_CHAIN (last
) = x
;
6217 tsubst_expr_values (t
, argvec
)
6220 tree first
= NULL_TREE
;
6223 for (; t
; t
= TREE_CHAIN (t
))
6225 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
6226 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
6227 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
6228 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
6229 *p
= build_tree_list (pur
, val
);
6230 p
= &TREE_CHAIN (*p
);
6241 last_tree
= TREE_CHAIN (last_tree
) = t
;
6248 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
6249 last_tree
= NULL_TREE
;
6256 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
6258 last_tree
= TREE_VALUE (saved_trees
);
6259 saved_trees
= TREE_CHAIN (saved_trees
);
6262 /* D is an undefined function declaration in the presence of templates with
6263 the same name, listed in FNS. If one of them can produce D as an
6264 instantiation, remember this so we can instantiate it at EOF if D has
6265 not been defined by that time. */
6268 add_maybe_template (d
, fns
)
6273 if (DECL_MAYBE_TEMPLATE (d
))
6276 t
= most_specialized (fns
, d
, NULL_TREE
);
6279 if (t
== error_mark_node
)
6281 cp_error ("ambiguous template instantiation for `%D'", d
);
6285 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
6286 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
6287 DECL_MAYBE_TEMPLATE (d
) = 1;
6290 /* Instantiate an enumerated type. Used by instantiate_class_template and
6294 tsubst_enum (tag
, args
, nargs
, field_chain
)
6299 extern tree current_local_enum
;
6300 tree prev_local_enum
= current_local_enum
;
6302 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
6303 tree e
, values
= NULL_TREE
;
6305 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
6307 tree elt
= build_enumerator (TREE_PURPOSE (e
),
6308 tsubst_expr (TREE_VALUE (e
), args
,
6310 TREE_CHAIN (elt
) = values
;
6314 finish_enum (newtag
, values
);
6316 if (NULL
!= field_chain
)
6317 *field_chain
= grok_enum_decls (NULL_TREE
);
6319 current_local_enum
= prev_local_enum
;