1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
42 /* The type of functions taking a tree, and some additional data, and
44 typedef int (*tree_fn_t
) PROTO((tree
, void*));
46 extern struct obstack permanent_obstack
;
49 extern char *input_filename
;
50 struct pending_inline
*pending_template_expansions
;
52 tree current_template_parms
;
53 HOST_WIDE_INT processing_template_decl
;
55 tree pending_templates
;
56 static tree
*template_tail
= &pending_templates
;
59 static tree
*maybe_template_tail
= &maybe_templates
;
61 int minimal_parse_mode
;
63 int processing_specialization
;
64 int processing_explicit_instantiation
;
65 static int template_header_count
;
67 static tree saved_trees
;
69 #define obstack_chunk_alloc xmalloc
70 #define obstack_chunk_free free
72 static int unify
PROTO((tree
, tree
, int, tree
, tree
, int, int*));
73 static void add_pending_template
PROTO((tree
));
74 static int push_tinst_level
PROTO((tree
));
75 static tree classtype_mangled_name
PROTO((tree
));
76 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
, tree
));
77 static tree tsubst_expr_values
PROTO((tree
, tree
));
78 static int comp_template_args
PROTO((tree
, tree
));
79 static int list_eq
PROTO((tree
, tree
));
80 static tree get_class_bindings
PROTO((tree
, tree
, tree
, tree
));
81 static tree coerce_template_parms
PROTO((tree
, tree
, tree
, int, int, int));
82 static tree tsubst_enum
PROTO((tree
, tree
, tree
*));
83 static tree add_to_template_args
PROTO((tree
, tree
));
84 static int type_unification_real
PROTO((tree
, tree
, tree
, tree
,
85 int, int, int, int*));
86 static tree complete_template_args
PROTO((tree
, tree
, int));
87 static void note_template_header
PROTO((int));
88 static tree maybe_fold_nontype_arg
PROTO((tree
));
89 static tree convert_nontype_argument
PROTO((tree
, tree
));
90 static tree get_bindings_overload
PROTO((tree
, tree
, tree
));
91 static int for_each_template_parm
PROTO((tree
, tree_fn_t
, void*));
92 static tree build_template_parm_index
PROTO((int, int, int, tree
, tree
));
93 static tree original_template
PROTO((tree
));
94 static int inline_needs_template_parms
PROTO((tree
));
95 static void push_inline_template_parms_recursive
PROTO((tree
, int));
96 static tree retrieve_specialization
PROTO((tree
, tree
));
97 static void register_specialization
PROTO((tree
, tree
, tree
));
98 static void print_candidates
PROTO((tree
));
99 static tree reduce_template_parm_level
PROTO((tree
, tree
, int));
100 static tree build_template_decl
PROTO((tree
, tree
));
101 static int mark_template_parm
PROTO((tree
, void *));
102 static tree tsubst_friend_function
PROTO((tree
, tree
));
103 static tree get_bindings_real
PROTO((tree
, tree
, tree
, int));
104 static int template_decl_level
PROTO((tree
));
105 static tree maybe_get_template_decl_from_type_decl
PROTO((tree
));
107 /* Do any processing required when DECL (a member template declaration
108 using TEMPLATE_PARAMETERS as its innermost parameter list) is
109 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
110 it is a specialization, in which case the DECL itself is returned. */
113 finish_member_template_decl (template_parameters
, decl
)
114 tree template_parameters
;
117 if (template_parameters
)
118 end_template_decl ();
120 end_specialization ();
122 if (decl
== NULL_TREE
|| decl
== void_type_node
)
124 else if (TREE_CODE (decl
) == TREE_LIST
)
126 /* Assume that the class is the only declspec. */
127 decl
= TREE_VALUE (decl
);
128 if (IS_AGGR_TYPE (decl
) && CLASSTYPE_TEMPLATE_INFO (decl
)
129 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl
))
131 tree tmpl
= CLASSTYPE_TI_TEMPLATE (decl
);
132 check_member_template (tmpl
);
137 else if (DECL_TEMPLATE_INFO (decl
))
139 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
141 check_member_template (DECL_TI_TEMPLATE (decl
));
142 return DECL_TI_TEMPLATE (decl
);
148 cp_error ("invalid member template declaration `%D'", decl
);
151 return error_mark_node
;
154 /* Returns the template nesting level of the indicated class TYPE.
164 A<T>::B<U> has depth two, while A<T> has depth one. Also,
165 both A<T>::B<int> and A<int>::B<U> have depth one. */
168 template_class_depth (type
)
173 for (depth
= 0; type
&& TREE_CODE (type
) != FUNCTION_DECL
;
174 type
= TYPE_CONTEXT (type
))
175 if (CLASSTYPE_TEMPLATE_INFO (type
)
176 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
))
177 && uses_template_parms (CLASSTYPE_TI_ARGS (type
)))
183 /* Return the original template for this decl, disregarding any
187 original_template (decl
)
190 while (DECL_TEMPLATE_INFO (decl
))
191 decl
= DECL_TI_TEMPLATE (decl
);
195 /* Returns 1 if processing DECL as part of do_pending_inlines
196 needs us to push template parms. */
199 inline_needs_template_parms (decl
)
202 if (! DECL_TEMPLATE_INFO (decl
))
205 return (list_length (DECL_TEMPLATE_PARMS (original_template (decl
)))
206 > (processing_template_decl
+ DECL_TEMPLATE_SPECIALIZATION (decl
)));
209 /* Subroutine of maybe_begin_member_template_processing.
210 Push the template parms in PARMS, starting from LEVELS steps into the
211 chain, and ending at the beginning, since template parms are listed
215 push_inline_template_parms_recursive (parmlist
, levels
)
219 tree parms
= TREE_VALUE (parmlist
);
223 push_inline_template_parms_recursive (TREE_CHAIN (parmlist
), levels
- 1);
225 ++processing_template_decl
;
226 current_template_parms
227 = tree_cons (build_int_2 (0, processing_template_decl
),
228 parms
, current_template_parms
);
229 TEMPLATE_PARMS_FOR_INLINE (current_template_parms
) = 1;
232 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
234 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
235 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
237 switch (TREE_CODE (parm
))
246 /* Make a CONST_DECL as is done in process_template_parm. */
247 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
249 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
255 my_friendly_abort (0);
260 /* Restore the template parameter context for a member template or
261 a friend template defined in a class definition. */
264 maybe_begin_member_template_processing (decl
)
270 if (! inline_needs_template_parms (decl
))
273 parms
= DECL_TEMPLATE_PARMS (original_template (decl
));
275 levels
= list_length (parms
) - processing_template_decl
;
277 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
280 parms
= TREE_CHAIN (parms
);
283 push_inline_template_parms_recursive (parms
, levels
);
286 /* Undo the effects of begin_member_template_processing. */
289 maybe_end_member_template_processing (decl
)
292 if (! processing_template_decl
)
295 while (current_template_parms
296 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms
))
298 --processing_template_decl
;
299 current_template_parms
= TREE_CHAIN (current_template_parms
);
304 /* Returns non-zero iff T is a member template function. We must be
307 template <class T> class C { void f(); }
309 Here, f is a template function, and a member, but not a member
310 template. This function does not concern itself with the origin of
311 T, only its present state. So if we have
313 template <class T> class C { template <class U> void f(U); }
315 then neither C<int>::f<char> nor C<T>::f<double> is considered
316 to be a member template. */
319 is_member_template (t
)
322 if (TREE_CODE (t
) != FUNCTION_DECL
323 && !DECL_FUNCTION_TEMPLATE_P (t
))
324 /* Anything that isn't a function or a template function is
325 certainly not a member template. */
328 /* A local class can't have member templates. */
329 if (hack_decl_function_context (t
))
332 if ((DECL_FUNCTION_MEMBER_P (t
)
333 && !DECL_TEMPLATE_SPECIALIZATION (t
))
334 || (TREE_CODE (t
) == TEMPLATE_DECL
335 && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))))
339 if (DECL_FUNCTION_TEMPLATE_P (t
))
341 else if (DECL_TEMPLATE_INFO (t
)
342 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
343 tmpl
= DECL_TI_TEMPLATE (t
);
348 /* If there are more levels of template parameters than
349 there are template classes surrounding the declaration,
350 then we have a member template. */
351 && (list_length (DECL_TEMPLATE_PARMS (tmpl
)) >
352 template_class_depth (DECL_CLASS_CONTEXT (t
))))
359 /* Return a new template argument vector which contains all of ARGS
360 for all outer templates TYPE is contained in, but has as its
361 innermost set of arguments the EXTRA_ARGS. If UNBOUND_ONLY, we
362 are only interested in unbound template arguments, not arguments from
363 enclosing templates that have been instantiated already. */
366 complete_template_args (tmpl
, extra_args
, unbound_only
)
367 tree tmpl
, extra_args
;
370 /* depth is the number of levels of enclosing args we're adding. */
372 tree args
, new_args
, spec_args
= NULL_TREE
;
374 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
375 my_friendly_assert (TREE_CODE (extra_args
) == TREE_VEC
, 0);
377 if (DECL_TEMPLATE_INFO (tmpl
) && !unbound_only
)
379 /* A specialization of a member template of a template class shows up
380 as a TEMPLATE_DECL with DECL_TEMPLATE_SPECIALIZATION set.
381 DECL_TI_ARGS is the specialization args, and DECL_TI_TEMPLATE
382 is the template being specialized. */
383 if (DECL_TEMPLATE_SPECIALIZATION (tmpl
))
385 spec_args
= DECL_TI_ARGS (tmpl
);
386 tmpl
= DECL_TI_TEMPLATE (tmpl
);
389 if (DECL_TEMPLATE_INFO (tmpl
))
391 /* A partial instantiation of a member template shows up as a
392 TEMPLATE_DECL with DECL_TEMPLATE_INFO. DECL_TI_ARGS is
393 all the bound template arguments. */
394 args
= DECL_TI_ARGS (tmpl
);
395 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
398 depth
= TREE_VEC_LENGTH (args
);
401 /* If we are a specialization, we might have no previously bound
405 new_args
= make_tree_vec (depth
+ 1 + (!!spec_args
));
408 TREE_VEC_ELT (new_args
, 0) = args
;
410 for (i
= 0; i
< depth
; ++i
)
411 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
418 /* For unbound args, we have to do more work. We are getting bindings
419 for the innermost args from extra_args, so we start from our
420 context and work out until we've seen all the args. We need to
421 do it this way to handle partial specialization. */
423 depth
= list_length (DECL_TEMPLATE_PARMS (tmpl
)) - 1;
427 new_args
= make_tree_vec (depth
+ 1);
429 /* If this isn't a member template, extra_args is for the innermost
430 template class, so skip over it. */
431 skip
= (! is_member_template (tmpl
));
433 type
= DECL_REAL_CONTEXT (tmpl
);
434 for (i
= depth
; i
; type
= TYPE_CONTEXT (type
))
435 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type
)))
442 TREE_VEC_ELT (new_args
, i
) = CLASSTYPE_TI_ARGS (type
);
447 TREE_VEC_ELT (new_args
, depth
) = extra_args
;
450 TREE_VEC_ELT (new_args
, depth
+ 1) = spec_args
;
455 /* Return a new template argument vector which contains all of ARGS,
456 but has as its innermost set of arguments the EXTRA_ARGS. */
459 add_to_template_args (args
, extra_args
)
465 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
467 new_args
= make_tree_vec (2);
468 TREE_VEC_ELT (new_args
, 0) = args
;
474 new_args
= make_tree_vec (TREE_VEC_LENGTH (args
) + 1);
476 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
477 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
480 TREE_VEC_ELT (new_args
,
481 TREE_VEC_LENGTH (new_args
) - 1) = extra_args
;
486 /* We've got a template header coming up; push to a new level for storing
490 begin_template_parm_list ()
493 declare_pseudo_global_level ();
494 ++processing_template_decl
;
495 note_template_header (0);
499 /* We've just seen template <>. */
502 begin_specialization ()
504 note_template_header (1);
508 /* Called at then end of processing a declaration preceeded by
512 end_specialization ()
514 reset_specialization ();
518 /* Any template <>'s that we have seen thus far are not referring to a
519 function specialization. */
522 reset_specialization ()
524 processing_specialization
= 0;
525 template_header_count
= 0;
529 /* We've just seen a template header. If SPECIALIZATION is non-zero,
530 it was of the form template <>. */
533 note_template_header (specialization
)
536 processing_specialization
= specialization
;
537 template_header_count
++;
541 /* We're beginning an explicit instantiation. */
544 begin_explicit_instantiation ()
546 ++processing_explicit_instantiation
;
551 end_explicit_instantiation ()
553 my_friendly_assert(processing_explicit_instantiation
> 0, 0);
554 --processing_explicit_instantiation
;
558 /* Retrieve the specialization (in the sense of [temp.spec] - a
559 specialization is either an instantiation or an explicit
560 specialization) of TMPL for the given template ARGS. If there is
561 no such specialization, return NULL_TREE. The ARGS are a vector of
562 arguments, or a vector of vectors of arguments, in the case of
563 templates with more than one level of parameters. */
566 retrieve_specialization (tmpl
, args
)
572 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
574 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
577 if (comp_template_args (TREE_PURPOSE (s
), args
))
578 return TREE_VALUE (s
);
585 /* Register the specialization SPEC as a specialization of TMPL with
586 the indicated ARGS. */
589 register_specialization (spec
, tmpl
, args
)
596 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 0);
598 if (TREE_CODE (spec
) != TEMPLATE_DECL
599 && list_length (DECL_TEMPLATE_PARMS (tmpl
)) > 1)
600 /* Avoid registering function declarations as
601 specializations of member templates, as would otherwise
602 happen with out-of-class specializations of member
606 for (s
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
609 if (comp_template_args (TREE_PURPOSE (s
), args
))
611 tree fn
= TREE_VALUE (s
);
613 if (DECL_TEMPLATE_SPECIALIZATION (spec
))
615 if (DECL_TEMPLATE_INSTANTIATION (fn
))
618 || DECL_EXPLICIT_INSTANTIATION (fn
))
620 cp_error ("specialization of %D after instantiation",
626 /* This situation should occur only if the first
627 specialization is an implicit instantiation,
628 the second is an explicit specialization, and
629 the implicit instantiation has not yet been
630 used. That situation can occur if we have
631 implicitly instantiated a member function of
632 class type, and then specialized it later. */
633 TREE_VALUE (s
) = spec
;
637 else if (DECL_TEMPLATE_SPECIALIZATION (fn
))
639 if (DECL_INITIAL (fn
))
640 cp_error ("duplicate specialization of %D", fn
);
642 TREE_VALUE (s
) = spec
;
648 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
649 = perm_tree_cons (args
, spec
, DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
653 /* Print the list of candidate FNS in an error message. */
656 print_candidates (fns
)
661 char* str
= "candidates are:";
663 for (fn
= fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
665 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
670 /* Returns the template (one of the functions given by TEMPLATE_ID)
671 which can be specialized to match the indicated DECL with the
672 explicit template args given in TEMPLATE_ID. If
673 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
674 member template. The template args (those explicitly specified and
675 those deduced) are output in a newly created vector *TARGS_OUT. If
676 it is impossible to determine the result, an error message is
677 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
681 determine_specialization (template_id
, decl
, targs_out
,
682 need_member_template
,
687 int need_member_template
;
690 tree fns
= TREE_OPERAND (template_id
, 0);
691 tree targs_in
= TREE_OPERAND (template_id
, 1);
692 tree templates
= NULL_TREE
;
697 *targs_out
= NULL_TREE
;
699 if (is_overloaded_fn (fns
))
700 fn
= get_first_fn (fns
);
704 overloaded
= really_overloaded_fn (fns
);
705 for (; fn
!= NULL_TREE
;
706 fn
= overloaded
? DECL_CHAIN (fn
) : NULL_TREE
)
710 if (!need_member_template
711 && TREE_CODE (fn
) == FUNCTION_DECL
712 && DECL_FUNCTION_MEMBER_P (fn
)
713 && DECL_USE_TEMPLATE (fn
)
714 && DECL_TI_TEMPLATE (fn
))
715 /* We can get here when processing something like:
716 template <class T> class X { void f(); }
717 template <> void X<int>::f() {}
718 We're specializing a member function, but not a member
720 tmpl
= DECL_TI_TEMPLATE (fn
);
721 else if (TREE_CODE (fn
) != TEMPLATE_DECL
722 || (need_member_template
&& !is_member_template (fn
)))
727 if (list_length (targs_in
) > DECL_NTPARMS (tmpl
))
730 if (decl
== NULL_TREE
)
732 tree targs
= make_scratch_vec (DECL_NTPARMS (tmpl
));
734 /* We allow incomplete unification here, because we are going to
735 check all the functions. */
736 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
744 /* Unification was successful. */
745 templates
= scratch_tree_cons (targs
, tmpl
, templates
);
748 templates
= scratch_tree_cons (NULL_TREE
, tmpl
, templates
);
751 if (decl
!= NULL_TREE
)
753 tree tmpl
= most_specialized (templates
, decl
, targs_in
);
755 if (tmpl
== error_mark_node
)
757 else if (tmpl
== NULL_TREE
)
760 *targs_out
= get_bindings (tmpl
, decl
, targs_in
);
764 if (templates
== NULL_TREE
)
768 cp_error ("`%D' does not match any template declaration",
773 else if (TREE_CHAIN (templates
) != NULL_TREE
)
778 cp_error ("ambiguous template specialization `%D'",
780 print_candidates (templates
);
785 /* We have one, and exactly one, match. */
786 *targs_out
= TREE_PURPOSE (templates
);
787 return TREE_VALUE (templates
);
791 /* Check to see if the function just declared, as indicated in
792 DECLARATOR, and in DECL, is a specialization of a function
793 template. We may also discover that the declaration is an explicit
794 instantiation at this point.
796 Returns DECL, or an equivalent declaration that should be used
799 FLAGS is a bitmask consisting of the following flags:
801 1: We are being called by finish_struct. (We are unable to
802 determine what template is specialized by an in-class
803 declaration until the class definition is complete, so
804 finish_struct_methods calls this function again later to finish
806 2: The function has a definition.
807 4: The function is a friend.
808 8: The function is known to be a specialization of a member
811 The TEMPLATE_COUNT is the number of references to qualifying
812 template classes that appeared in the name of the function. For
815 template <class T> struct S { void f(); };
818 the TEMPLATE_COUNT would be 1. However, explicitly specialized
819 classes are not counted in the TEMPLATE_COUNT, so that in
821 template <class T> struct S {};
822 template <> struct S<int> { void f(); }
826 the TEMPLATE_COUNT would be 0. (Note that this declaration is
827 illegal; there should be no template <>.)
829 If the function is a specialization, it is marked as such via
830 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
831 is set up correctly, and it is added to the list of specializations
832 for that template. */
835 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
841 int finish_member
= flags
& 1;
842 int have_def
= flags
& 2;
843 int is_friend
= flags
& 4;
844 int specialization
= 0;
845 int explicit_instantiation
= 0;
846 int member_specialization
= flags
& 8;
848 tree ctype
= DECL_CLASS_CONTEXT (decl
);
849 tree dname
= DECL_NAME (decl
);
853 if (processing_specialization
)
855 /* The last template header was of the form template <>. */
857 if (template_header_count
> template_count
)
859 /* There were more template headers than qualifying template
861 if (template_header_count
- template_count
> 1)
862 /* There shouldn't be that many template parameter
863 lists. There can be at most one parameter list for
864 every qualifying class, plus one for the function
866 cp_error ("too many template parameter lists in declaration of `%D'", decl
);
868 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
870 member_specialization
= 1;
874 else if (template_header_count
== template_count
)
876 /* The counts are equal. So, this might be a
877 specialization, but it is not a specialization of a
878 member template. It might be something like
880 template <class T> struct S {
884 void S<int>::f(int i) {} */
886 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
890 /* This cannot be an explicit specialization. There are not
891 enough headers for all of the qualifying classes. For
892 example, we might have:
895 void S<int>::T<char>::f();
897 But, we're missing another template <>. */
898 cp_error("too few template parameter lists in declaration of `%D'", decl
);
902 else if (processing_explicit_instantiation
)
904 if (template_header_count
)
905 cp_error ("template parameter list used in explicit instantiation");
908 cp_error ("definition provided for explicit instantiation");
910 explicit_instantiation
= 1;
912 else if (ctype
!= NULL_TREE
913 && !TYPE_BEING_DEFINED (ctype
)
914 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype
))
916 /* This case catches outdated code that looks like this:
918 template <class T> struct S { void f(); };
919 void S<int>::f() {} // Missing template <>
921 We disable this check when the type is being defined to
922 avoid complaining about default compiler-generated
923 constructors, destructors, and assignment operators.
924 Since the type is an instantiation, not a specialization,
925 these are the only functions that can be defined before
926 the class is complete. */
929 template <class T> void S<int>::f() {}
931 if (template_header_count
)
933 cp_error ("template parameters specified in specialization");
939 ("explicit specialization not preceded by `template <>'");
941 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
943 else if (TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
945 /* This case handles bogus declarations like
946 template <> template <class T>
949 cp_error ("template-id `%D' in declaration of primary template",
955 if (specialization
|| member_specialization
)
957 tree t
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
958 for (; t
; t
= TREE_CHAIN (t
))
959 if (TREE_PURPOSE (t
))
962 ("default argument specified in explicit specialization");
967 if (specialization
|| member_specialization
|| explicit_instantiation
)
969 tree tmpl
= NULL_TREE
;
970 tree targs
= NULL_TREE
;
972 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
973 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
977 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
980 fns
= IDENTIFIER_NAMESPACE_VALUE (dname
);
985 lookup_template_function (fns
, NULL_TREE
);
988 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
990 /* A friend declaration. We can't do much, because we don't
991 know what this resolves to, yet. */
992 my_friendly_assert (is_friend
!= 0, 0);
993 my_friendly_assert (!explicit_instantiation
, 0);
994 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
998 if (ctype
!= NULL_TREE
&& TYPE_BEING_DEFINED (ctype
))
1000 /* Since finish_struct_1 has not been called yet, we
1001 can't call lookup_fnfields. We note that this
1002 template is a specialization, and proceed, letting
1003 finish_struct fix this up later. */
1004 tree ti
= perm_tree_cons (NULL_TREE
,
1005 TREE_OPERAND (declarator
, 1),
1007 TI_PENDING_SPECIALIZATION_FLAG (ti
) = 1;
1008 DECL_TEMPLATE_INFO (decl
) = ti
;
1009 /* This should not be an instantiation; explicit
1010 instantiation directives can only occur at the top
1012 my_friendly_assert (!explicit_instantiation
, 0);
1015 else if (ctype
!= NULL_TREE
1016 && (TREE_CODE (TREE_OPERAND (declarator
, 0)) ==
1019 /* Find the list of functions in ctype that have the same
1020 name as the declared function. */
1021 tree name
= TREE_OPERAND (declarator
, 0);
1024 if (name
== constructor_name (ctype
)
1025 || name
== constructor_name_full (ctype
))
1027 int is_constructor
= DECL_CONSTRUCTOR_P (decl
);
1029 if (is_constructor
? !TYPE_HAS_CONSTRUCTOR (ctype
)
1030 : !TYPE_HAS_DESTRUCTOR (ctype
))
1032 /* From [temp.expl.spec]:
1034 If such an explicit specialization for the member
1035 of a class template names an implicitly-declared
1036 special member function (clause _special_), the
1037 program is ill-formed.
1039 Similar language is found in [temp.explicit]. */
1040 cp_error ("specialization of implicitly-declared special member function");
1045 name
= is_constructor
? ctor_identifier
: dtor_identifier
;
1048 fns
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
1050 if (fns
== NULL_TREE
)
1052 cp_error ("no member function `%s' declared in `%T'",
1053 IDENTIFIER_POINTER (name
),
1058 TREE_OPERAND (declarator
, 0) = fns
;
1061 /* Figure out what exactly is being specialized at this point.
1062 Note that for an explicit instantiation, even one for a
1063 member function, we cannot tell apriori whether the the
1064 instantiation is for a member template, or just a member
1065 function of a template class. In particular, even in if the
1066 instantiation is for a member template, the template
1067 arguments could be deduced from the declaration. */
1068 tmpl
= determine_specialization (declarator
, decl
,
1070 member_specialization
,
1075 if (explicit_instantiation
)
1077 decl
= instantiate_template (tmpl
, targs
);
1078 if (!DECL_TEMPLATE_SPECIALIZATION (decl
))
1079 /* There doesn't seem to be anything in the draft to
1080 prevent a specialization from being explicitly
1081 instantiated. We're careful not to destroy the
1082 information indicating that this is a
1083 specialization here. */
1084 SET_DECL_EXPLICIT_INSTANTIATION (decl
);
1087 else if (DECL_STATIC_FUNCTION_P (tmpl
)
1088 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
1090 revert_static_member_fn (&decl
, 0, 0);
1091 last_function_parms
= TREE_CHAIN (last_function_parms
);
1094 /* Mangle the function name appropriately. Note that we do
1095 not mangle specializations of non-template member
1096 functions of template classes, e.g. with
1097 template <class T> struct S { void f(); }
1098 and given the specialization
1099 template <> void S<int>::f() {}
1100 we do not mangle S<int>::f() here. That's because it's
1101 just an ordinary member function and doesn't need special
1103 if ((is_member_template (tmpl
) || ctype
== NULL_TREE
)
1104 && name_mangling_version
>= 1)
1106 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
1109 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
1111 hash_tree_chain (build_pointer_type (ctype
),
1114 DECL_ASSEMBLER_NAME (decl
)
1115 = build_template_decl_overload
1118 TREE_TYPE (TREE_TYPE (tmpl
)),
1119 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
1120 targs
, ctype
!= NULL_TREE
);
1123 if (is_friend
&& !have_def
)
1125 /* This is not really a declaration of a specialization.
1126 It's just the name of an instantiation. But, it's not
1127 a request for an instantiation, either. */
1128 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
1129 DECL_TEMPLATE_INFO (decl
)
1130 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1134 /* If DECL_TI_TEMPLATE (decl), the decl is an
1135 instantiation of a specialization of a member template.
1136 (In other words, there was a member template, in a
1137 class template. That member template was specialized.
1138 We then instantiated the class, so there is now an
1139 instance of that specialization.)
1141 According to the CD2,
1143 14.7.3.13 [tmpl.expl.spec]
1145 A specialization of a member function template or
1146 member class template of a non-specialized class
1147 template is itself a template.
1149 So, we just leave the template info alone in this case. */
1150 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
1151 DECL_TEMPLATE_INFO (decl
)
1152 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
1154 register_specialization (decl
, tmpl
, targs
);
1164 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1165 parameters. These are represented in the same format used for
1166 DECL_TEMPLATE_PARMS. */
1168 int comp_template_parms (parms1
, parms2
)
1175 if (parms1
== parms2
)
1178 for (p1
= parms1
, p2
= parms2
;
1179 p1
!= NULL_TREE
&& p2
!= NULL_TREE
;
1180 p1
= TREE_CHAIN (p1
), p2
= TREE_CHAIN (p2
))
1182 tree t1
= TREE_VALUE (p1
);
1183 tree t2
= TREE_VALUE (p2
);
1186 my_friendly_assert (TREE_CODE (t1
) == TREE_VEC
, 0);
1187 my_friendly_assert (TREE_CODE (t2
) == TREE_VEC
, 0);
1189 if (TREE_VEC_LENGTH (t1
) != TREE_VEC_LENGTH (t2
))
1192 for (i
= 0; i
< TREE_VEC_LENGTH (t2
); ++i
)
1194 tree parm1
= TREE_VALUE (TREE_VEC_ELT (t1
, i
));
1195 tree parm2
= TREE_VALUE (TREE_VEC_ELT (t2
, i
));
1197 if (TREE_CODE (parm1
) != TREE_CODE (parm2
))
1200 if (TREE_CODE (parm1
) == TEMPLATE_TYPE_PARM
)
1202 else if (!comptypes (TREE_TYPE (parm1
),
1203 TREE_TYPE (parm2
), 1))
1208 if ((p1
!= NULL_TREE
) != (p2
!= NULL_TREE
))
1209 /* One set of parameters has more parameters lists than the
1217 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1218 ORIG_LEVEL, DECL, and TYPE. */
1221 build_template_parm_index (index
, level
, orig_level
, decl
, type
)
1228 tree t
= make_node (TEMPLATE_PARM_INDEX
);
1229 TEMPLATE_PARM_IDX (t
) = index
;
1230 TEMPLATE_PARM_LEVEL (t
) = level
;
1231 TEMPLATE_PARM_ORIG_LEVEL (t
) = orig_level
;
1232 TEMPLATE_PARM_DECL (t
) = decl
;
1233 TREE_TYPE (t
) = type
;
1239 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1240 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1241 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1242 new one is created. */
1245 reduce_template_parm_level (index
, type
, levels
)
1250 if (TEMPLATE_PARM_DESCENDANTS (index
) == NULL_TREE
1251 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index
))
1252 != TEMPLATE_PARM_LEVEL (index
) - levels
))
1255 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index
)),
1256 DECL_NAME (TEMPLATE_PARM_DECL (index
)),
1259 = build_template_parm_index (TEMPLATE_PARM_IDX (index
),
1260 TEMPLATE_PARM_LEVEL (index
) - levels
,
1261 TEMPLATE_PARM_ORIG_LEVEL (index
),
1263 TEMPLATE_PARM_DESCENDANTS (index
) = t
;
1265 /* Template template parameters need this. */
1266 DECL_TEMPLATE_PARMS (decl
)
1267 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index
));
1270 return TEMPLATE_PARM_DESCENDANTS (index
);
1273 /* Process information from new template parameter NEXT and append it to the
1274 LIST being built. */
1277 process_template_parm (list
, next
)
1286 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
1287 defval
= TREE_PURPOSE (parm
);
1288 parm
= TREE_VALUE (parm
);
1289 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
1293 tree p
= TREE_VALUE (tree_last (list
));
1295 if (TREE_CODE (p
) == TYPE_DECL
)
1296 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
1297 else if (TREE_CODE (p
) == TEMPLATE_DECL
)
1298 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p
)));
1300 idx
= TEMPLATE_PARM_IDX (DECL_INITIAL (p
));
1308 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
1309 /* is a const-param */
1310 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
1311 PARM
, 0, NULL_TREE
);
1312 /* A template parameter is not modifiable. */
1313 TREE_READONLY (parm
) = 1;
1314 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
1315 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
1317 cp_error ("`%#T' is not a valid type for a template constant parameter",
1319 if (DECL_NAME (parm
) == NULL_TREE
)
1320 error (" a template type parameter must begin with `class' or `typename'");
1321 TREE_TYPE (parm
) = void_type_node
;
1324 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
1325 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
1326 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1328 if (TREE_PERMANENT (parm
) == 0)
1330 parm
= copy_node (parm
);
1331 TREE_PERMANENT (parm
) = 1;
1333 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
1334 DECL_INITIAL (parm
) = DECL_INITIAL (decl
)
1335 = build_template_parm_index (idx
, processing_template_decl
,
1336 processing_template_decl
,
1337 decl
, TREE_TYPE (parm
));
1342 parm
= TREE_VALUE (parm
);
1344 if (parm
&& TREE_CODE (parm
) == TEMPLATE_DECL
)
1346 t
= make_lang_type (TEMPLATE_TEMPLATE_PARM
);
1347 /* This is for distinguishing between real templates and template
1348 template parameters */
1349 TREE_TYPE (parm
) = t
;
1350 TREE_TYPE (DECL_TEMPLATE_RESULT (parm
)) = t
;
1355 t
= make_lang_type (TEMPLATE_TYPE_PARM
);
1356 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1357 decl
= build_decl (TYPE_DECL
, parm
, t
);
1360 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1361 TYPE_NAME (t
) = decl
;
1362 TYPE_STUB_DECL (t
) = decl
;
1364 TEMPLATE_TYPE_PARM_INDEX (t
)
1365 = build_template_parm_index (idx
, processing_template_decl
,
1366 processing_template_decl
,
1367 decl
, TREE_TYPE (parm
));
1369 SET_DECL_ARTIFICIAL (decl
);
1371 parm
= build_tree_list (defval
, parm
);
1372 return chainon (list
, parm
);
1375 /* The end of a template parameter list has been reached. Process the
1376 tree list into a parameter vector, converting each parameter into a more
1377 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1381 end_template_parm_list (parms
)
1386 tree saved_parmlist
= make_tree_vec (list_length (parms
));
1388 current_template_parms
1389 = tree_cons (build_int_2 (0, processing_template_decl
),
1390 saved_parmlist
, current_template_parms
);
1392 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
1393 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
1395 return saved_parmlist
;
1398 /* end_template_decl is called after a template declaration is seen. */
1401 end_template_decl ()
1403 reset_specialization ();
1405 if (! processing_template_decl
)
1408 /* This matches the pushlevel in begin_template_parm_list. */
1411 --processing_template_decl
;
1412 current_template_parms
= TREE_CHAIN (current_template_parms
);
1413 (void) get_pending_sizes (); /* Why? */
1416 /* Generate a valid set of template args from current_template_parms. */
1419 current_template_args ()
1421 tree header
= current_template_parms
;
1422 int length
= list_length (header
);
1423 tree args
= make_tree_vec (length
);
1428 tree a
= copy_node (TREE_VALUE (header
));
1429 int i
= TREE_VEC_LENGTH (a
);
1430 TREE_TYPE (a
) = NULL_TREE
;
1433 tree t
= TREE_VEC_ELT (a
, i
);
1435 /* t will be a list if we are called from within a
1436 begin/end_template_parm_list pair, but a vector directly
1437 if within a begin/end_member_template_processing pair. */
1438 if (TREE_CODE (t
) == TREE_LIST
)
1442 if (TREE_CODE (t
) == TYPE_DECL
1443 || TREE_CODE (t
) == TEMPLATE_DECL
)
1446 t
= DECL_INITIAL (t
);
1449 TREE_VEC_ELT (a
, i
) = t
;
1451 TREE_VEC_ELT (args
, --l
) = a
;
1452 header
= TREE_CHAIN (header
);
1459 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1460 template PARMS. Used by push_template_decl below. */
1463 build_template_decl (decl
, parms
)
1467 tree tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
1468 DECL_TEMPLATE_PARMS (tmpl
) = parms
;
1469 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
1470 if (DECL_LANG_SPECIFIC (decl
))
1472 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
1473 DECL_STATIC_FUNCTION_P (tmpl
) =
1474 DECL_STATIC_FUNCTION_P (decl
);
1480 struct template_parm_data
1486 /* Subroutine of push_template_decl used to see if each template
1487 parameter in a partial specialization is used in the explicit
1488 argument list. If T is of the LEVEL given in DATA (which is
1489 treated as a template_parm_data*), then DATA->PARMS is marked
1493 mark_template_parm (t
, data
)
1499 struct template_parm_data
* tpd
= (struct template_parm_data
*) data
;
1501 if (TREE_CODE (t
) == TEMPLATE_PARM_INDEX
)
1503 level
= TEMPLATE_PARM_LEVEL (t
);
1504 idx
= TEMPLATE_PARM_IDX (t
);
1508 level
= TEMPLATE_TYPE_LEVEL (t
);
1509 idx
= TEMPLATE_TYPE_IDX (t
);
1512 if (level
== tpd
->level
)
1513 tpd
->parms
[idx
] = 1;
1515 /* Return zero so that for_each_template_parm will continue the
1516 traversal of the tree; we want to mark *every* template parm. */
1520 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
1521 parameters given by current_template_args, or reuses a
1522 previously existing one, if appropriate. Returns the DECL, or an
1523 equivalent one, if it is replaced via a call to duplicate_decls. */
1526 push_template_decl (decl
)
1534 int is_friend
= (TREE_CODE (decl
) == FUNCTION_DECL
1535 && DECL_FRIEND_P (decl
));
1538 /* For a friend, we want the context of the friend function, not
1539 the type of which it is a friend. */
1540 ctx
= DECL_CONTEXT (decl
);
1541 else if (DECL_REAL_CONTEXT (decl
))
1542 /* In the case of a virtual function, we want the class in which
1544 ctx
= DECL_REAL_CONTEXT (decl
);
1546 /* Otherwise, if we're currently definining some class, the DECL
1547 is assumed to be a member of the class. */
1548 ctx
= current_class_type
;
1550 /* For determining whether this is a primary template or not, we're really
1551 interested in the lexical context, not the true context. */
1553 info
= DECL_CLASS_CONTEXT (decl
);
1557 if (info
&& TREE_CODE (info
) == FUNCTION_DECL
)
1560 || (TYPE_BEING_DEFINED (info
) && template_header_count
1561 && ! processing_specialization
)
1562 || (template_header_count
> template_class_depth (info
)))
1569 if (current_lang_name
== lang_name_c
)
1570 cp_error ("template with C linkage");
1571 if (TREE_CODE (decl
) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl
)))
1572 cp_error ("template class without a name");
1575 /* Partial specialization. */
1576 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
1577 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
1579 tree type
= TREE_TYPE (decl
);
1580 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1581 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
1582 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
1584 /* We check that each of the template parameters given in the
1585 partial specialization is used in the argument list to the
1586 specialization. For example:
1588 template <class T> struct S;
1589 template <class T> struct S<T*>;
1591 The second declaration is OK because `T*' uses the template
1592 parameter T, whereas
1594 template <class T> struct S<int>;
1596 is no good. Even trickier is:
1607 The S2<T> declaration is actually illegal; it is a
1608 full-specialization. Of course,
1611 struct S2<T (*)(U)>;
1613 or some such would have been OK. */
1615 struct template_parm_data tpd
;
1616 int ntparms
= TREE_VEC_LENGTH (TREE_VALUE (current_template_parms
));
1617 int did_error_intro
= 0;
1619 tpd
.level
= TREE_INT_CST_HIGH (TREE_PURPOSE (current_template_parms
));
1620 tpd
.parms
= alloca (sizeof (int) * ntparms
);
1621 for (i
= 0; i
< ntparms
; ++i
)
1623 for (i
= 0; i
< TREE_VEC_LENGTH (mainargs
); ++i
)
1624 for_each_template_parm (TREE_VEC_ELT (mainargs
, i
),
1625 &mark_template_parm
,
1627 for (i
= 0; i
< ntparms
; ++i
)
1628 if (tpd
.parms
[i
] == 0)
1630 /* One of the template parms was not used in the
1632 if (!did_error_intro
)
1634 cp_error ("template parameters not used in partial specialization:");
1635 did_error_intro
= 1;
1639 TREE_VALUE (TREE_VEC_ELT
1640 (TREE_VALUE (current_template_parms
),
1644 for (; spec
; spec
= TREE_CHAIN (spec
))
1646 /* purpose: args to main template
1647 value: spec template */
1648 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
1652 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
1653 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
1654 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
1655 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
1659 args
= current_template_args ();
1662 || TREE_CODE (ctx
) == FUNCTION_DECL
1663 || TYPE_BEING_DEFINED (ctx
)
1664 || (is_friend
&& !DECL_TEMPLATE_INFO (decl
)))
1666 if (DECL_LANG_SPECIFIC (decl
)
1667 && DECL_TEMPLATE_INFO (decl
)
1668 && DECL_TI_TEMPLATE (decl
))
1669 tmpl
= DECL_TI_TEMPLATE (decl
);
1672 tmpl
= build_template_decl (decl
, current_template_parms
);
1674 if (DECL_LANG_SPECIFIC (decl
)
1675 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1677 /* A specialization of a member template of a template
1679 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
1680 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
1681 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
1690 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
1691 cp_error ("must specialize `%#T' before defining member `%#D'",
1693 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1694 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
1695 else if (! DECL_TEMPLATE_INFO (decl
))
1697 cp_error ("template definition of non-template `%#D'", decl
);
1701 tmpl
= DECL_TI_TEMPLATE (decl
);
1703 if (is_member_template (tmpl
))
1705 if (DECL_TEMPLATE_INFO (decl
) && DECL_TI_ARGS (decl
)
1706 && DECL_TEMPLATE_SPECIALIZATION (decl
))
1710 /* The declaration is a specialization of a member
1711 template, declared outside the class. Therefore, the
1712 innermost template arguments will be NULL, so we
1713 replace them with the arguments determined by the
1714 earlier call to check_explicit_specialization. */
1715 args
= DECL_TI_ARGS (decl
);
1718 = build_template_decl (decl
, current_template_parms
);
1719 DECL_TEMPLATE_RESULT (new_tmpl
) = decl
;
1720 TREE_TYPE (new_tmpl
) = TREE_TYPE (decl
);
1721 DECL_TI_TEMPLATE (decl
) = new_tmpl
;
1722 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl
);
1723 DECL_TEMPLATE_INFO (new_tmpl
) =
1724 perm_tree_cons (tmpl
, args
, NULL_TREE
);
1726 register_specialization (new_tmpl
, tmpl
, args
);
1730 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1731 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
1732 if (TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1734 cp_error ("got %d template parameters for `%#D'",
1735 TREE_VEC_LENGTH (a
), decl
);
1736 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
1738 if (TREE_VEC_LENGTH (args
) > 1)
1739 /* Get the template parameters for the enclosing template
1741 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
1746 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
1750 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
1752 /* When processing an inline member template of a
1753 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
1754 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
1755 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
1757 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
1758 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
1760 /* There should be template arguments if and only if there is a
1762 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
1765 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
1767 cp_error ("got %d template parameters for `%#D'",
1768 TREE_VEC_LENGTH (a
), decl
);
1769 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
1772 /* Get the innermost set of template arguments. */
1773 args
= innermost_args (args
, 0);
1775 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
1776 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
1778 if (! ctx
&& primary
)
1779 /* The check of PRIMARY ensures that we do not try to push a
1780 global template friend declared in a template class; such a
1781 thing may well depend on the template parameters of the class. */
1782 tmpl
= pushdecl_top_level (tmpl
);
1785 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
1787 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
1789 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
1791 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
1792 if (!ctx
|| TREE_CODE (ctx
) != FUNCTION_DECL
)
1793 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
1795 else if (! DECL_LANG_SPECIFIC (decl
))
1796 cp_error ("template declaration of `%#D'", decl
);
1798 DECL_TEMPLATE_INFO (decl
) = info
;
1800 return DECL_TEMPLATE_RESULT (tmpl
);
1803 /* Called when a class template TYPE is redeclared, e.g.:
1805 template <class T> struct S;
1806 template <class T> struct S {}; */
1809 redeclare_class_template (type
)
1812 tree tmpl
= CLASSTYPE_TI_TEMPLATE (type
);
1813 tree tmpl_parms
= DECL_INNERMOST_TEMPLATE_PARMS (tmpl
);
1814 tree parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
1817 if (!PRIMARY_TEMPLATE_P (tmpl
))
1818 /* The type is nested in some template class. Nothing to worry
1819 about here; there are no new template parameters for the nested
1823 if (TREE_VEC_LENGTH (parms
) != TREE_VEC_LENGTH (tmpl_parms
))
1825 cp_error_at ("previous declaration `%D'", tmpl
);
1826 cp_error ("used %d template parameter%s instead of %d",
1827 TREE_VEC_LENGTH (tmpl_parms
),
1828 TREE_VEC_LENGTH (tmpl_parms
) == 1 ? "" : "s",
1829 TREE_VEC_LENGTH (parms
));
1833 for (i
= 0; i
< TREE_VEC_LENGTH (tmpl_parms
); ++i
)
1835 tree tmpl_parm
= TREE_VALUE (TREE_VEC_ELT (tmpl_parms
, i
));
1836 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1837 tree tmpl_default
= TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
));
1838 tree parm_default
= TREE_PURPOSE (TREE_VEC_ELT (parms
, i
));
1840 if (TREE_CODE (tmpl_parm
) != TREE_CODE (parm
))
1842 cp_error_at ("template parameter `%#D'", tmpl_parm
);
1843 cp_error ("redeclared here as `%#D'", parm
);
1847 if (tmpl_default
!= NULL_TREE
&& parm_default
!= NULL_TREE
)
1849 /* We have in [temp.param]:
1851 A template-parameter may not be given default arguments
1852 by two different declarations in the same scope. */
1853 cp_error ("redefinition of default argument for `%#D'", parm
);
1857 if (parm_default
!= NULL_TREE
)
1858 /* Update the previous template parameters (which are the ones
1859 that will really count) with the new default value. */
1860 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms
, i
)) = parm_default
;
1864 /* Attempt to convert the non-type template parameter EXPR to the
1865 indicated TYPE. If the conversion is successful, return the
1866 converted value. If the conversion is unsuccesful, return
1867 NULL_TREE if we issued an error message, or error_mark_node if we
1868 did not. We issue error messages for out-and-out bad template
1869 parameters, but not simply because the conversion failed, since we
1870 might be just trying to do argument deduction. By the time this
1871 function is called, neither TYPE nor EXPR may make use of template
1875 convert_nontype_argument (type
, expr
)
1879 tree expr_type
= TREE_TYPE (expr
);
1881 /* A template-argument for a non-type, non-template
1882 template-parameter shall be one of:
1884 --an integral constant-expression of integral or enumeration
1887 --the name of a non-type template-parameter; or
1889 --the name of an object or function with external linkage,
1890 including function templates and function template-ids but
1891 excluding non-static class members, expressed as id-expression;
1894 --the address of an object or function with external linkage,
1895 including function templates and function template-ids but
1896 excluding non-static class members, expressed as & id-expression
1897 where the & is optional if the name refers to a function or
1900 --a pointer to member expressed as described in _expr.unary.op_. */
1902 /* An integral constant-expression can include const variables
1904 if (INTEGRAL_TYPE_P (expr_type
) && TREE_READONLY_DECL_P (expr
))
1905 expr
= decl_constant_value (expr
);
1907 if (is_overloaded_fn (expr
))
1908 /* OK for now. We'll check that it has external linkage later.
1909 Check this first since if expr_type is the unknown_type_node
1910 we would otherwise complain below. */
1912 else if (INTEGRAL_TYPE_P (expr_type
)
1913 || TYPE_PTRMEM_P (expr_type
)
1914 || TYPE_PTRMEMFUNC_P (expr_type
)
1915 /* The next two are g++ extensions. */
1916 || TREE_CODE (expr_type
) == REAL_TYPE
1917 || TREE_CODE (expr_type
) == COMPLEX_TYPE
)
1919 if (! TREE_CONSTANT (expr
))
1921 cp_error ("non-constant `%E' cannot be used as template argument",
1926 else if (TYPE_PTR_P (expr_type
)
1927 /* If expr is the address of an overloaded function, we
1928 will get the unknown_type_node at this point. */
1929 || expr_type
== unknown_type_node
)
1935 if (TREE_CODE (e
) != ADDR_EXPR
)
1938 cp_error ("`%E' is not a valid template argument", expr
);
1939 error ("it must be %s%s with external linkage",
1940 TREE_CODE (TREE_TYPE (expr
)) == POINTER_TYPE
1941 ? "a pointer to " : "",
1942 TREE_CODE (TREE_TYPE (TREE_TYPE (expr
))) == FUNCTION_TYPE
1943 ? "a function" : "an object");
1947 referent
= TREE_OPERAND (e
, 0);
1948 STRIP_NOPS (referent
);
1950 if (TREE_CODE (referent
) == STRING_CST
)
1952 cp_error ("string literal %E is not a valid template argument",
1954 error ("because it is the address of an object with static linkage");
1958 if (is_overloaded_fn (referent
))
1959 /* We'll check that it has external linkage later. */
1961 else if (TREE_CODE (referent
) != VAR_DECL
)
1963 else if (!TREE_PUBLIC (referent
))
1965 cp_error ("address of non-extern `%E' cannot be used as template argument", referent
);
1966 return error_mark_node
;
1969 else if (TREE_CODE (expr
) == VAR_DECL
)
1971 if (!TREE_PUBLIC (expr
))
1976 cp_error ("object `%E' cannot be used as template argument", expr
);
1980 switch (TREE_CODE (type
))
1985 /* For a non-type template-parameter of integral or enumeration
1986 type, integral promotions (_conv.prom_) and integral
1987 conversions (_conv.integral_) are applied. */
1988 if (!INTEGRAL_TYPE_P (expr_type
))
1989 return error_mark_node
;
1991 /* It's safe to call digest_init in this case; we know we're
1992 just converting one integral constant expression to another. */
1993 return digest_init (type
, expr
, (tree
*) 0);
1997 /* These are g++ extensions. */
1998 if (TREE_CODE (expr_type
) != TREE_CODE (type
))
1999 return error_mark_node
;
2001 return digest_init (type
, expr
, (tree
*) 0);
2005 tree type_pointed_to
= TREE_TYPE (type
);
2007 if (TYPE_PTRMEM_P (type
))
2008 /* For a non-type template-parameter of type pointer to data
2009 member, qualification conversions (_conv.qual_) are
2011 return perform_qualification_conversions (type
, expr
);
2012 else if (TREE_CODE (type_pointed_to
) == FUNCTION_TYPE
)
2014 /* For a non-type template-parameter of type pointer to
2015 function, only the function-to-pointer conversion
2016 (_conv.func_) is applied. If the template-argument
2017 represents a set of overloaded functions (or a pointer to
2018 such), the matching function is selected from the set
2023 if (TREE_CODE (expr
) == ADDR_EXPR
)
2024 fns
= TREE_OPERAND (expr
, 0);
2028 fn
= instantiate_type (type_pointed_to
, fns
, 0);
2030 if (fn
== error_mark_node
)
2031 return error_mark_node
;
2033 if (!TREE_PUBLIC (fn
))
2035 if (really_overloaded_fn (fns
))
2036 return error_mark_node
;
2041 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2043 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
2049 /* For a non-type template-parameter of type pointer to
2050 object, qualification conversions (_conv.qual_) and the
2051 array-to-pointer conversion (_conv.array_) are applied.
2052 [Note: In particular, neither the null pointer conversion
2053 (_conv.ptr_) nor the derived-to-base conversion
2054 (_conv.ptr_) are applied. Although 0 is a valid
2055 template-argument for a non-type template-parameter of
2056 integral type, it is not a valid template-argument for a
2057 non-type template-parameter of pointer type.]
2059 The call to decay_conversion performs the
2060 array-to-pointer conversion, if appropriate. */
2061 expr
= decay_conversion (expr
);
2063 if (expr
== error_mark_node
)
2064 return error_mark_node
;
2066 return perform_qualification_conversions (type
, expr
);
2071 case REFERENCE_TYPE
:
2073 tree type_referred_to
= TREE_TYPE (type
);
2075 if (TREE_CODE (type_referred_to
) == FUNCTION_TYPE
)
2077 /* For a non-type template-parameter of type reference to
2078 function, no conversions apply. If the
2079 template-argument represents a set of overloaded
2080 functions, the matching function is selected from the
2081 set (_over.over_). */
2085 fn
= instantiate_type (type_referred_to
, fns
, 0);
2087 if (!TREE_PUBLIC (fn
))
2089 if (really_overloaded_fn (fns
))
2090 /* Don't issue an error here; we might get a different
2091 function if the overloading had worked out
2093 return error_mark_node
;
2098 if (fn
== error_mark_node
)
2099 return error_mark_node
;
2101 my_friendly_assert (comptypes (type
, TREE_TYPE (fn
), 1),
2108 /* For a non-type template-parameter of type reference to
2109 object, no conversions apply. The type referred to by the
2110 reference may be more cv-qualified than the (otherwise
2111 identical) type of the template-argument. The
2112 template-parameter is bound directly to the
2113 template-argument, which must be an lvalue. */
2114 if (!comptypes (TYPE_MAIN_VARIANT (expr_type
),
2115 TYPE_MAIN_VARIANT (type
), 1)
2116 || (TYPE_READONLY (expr_type
) >
2117 TYPE_READONLY (type_referred_to
))
2118 || (TYPE_VOLATILE (expr_type
) >
2119 TYPE_VOLATILE (type_referred_to
))
2120 || !real_lvalue_p (expr
))
2121 return error_mark_node
;
2133 my_friendly_assert (TYPE_PTRMEMFUNC_P (type
), 0);
2135 /* For a non-type template-parameter of type pointer to member
2136 function, no conversions apply. If the template-argument
2137 represents a set of overloaded member functions, the
2138 matching member function is selected from the set
2141 if (!TYPE_PTRMEMFUNC_P (expr_type
) &&
2142 expr_type
!= unknown_type_node
)
2143 return error_mark_node
;
2145 if (TREE_CODE (expr
) == CONSTRUCTOR
)
2147 /* A ptr-to-member constant. */
2148 if (!comptypes (type
, expr_type
, 1))
2149 return error_mark_node
;
2154 if (TREE_CODE (expr
) != ADDR_EXPR
)
2155 return error_mark_node
;
2157 fns
= TREE_OPERAND (expr
, 0);
2159 fn
= instantiate_type (TREE_TYPE (TREE_TYPE (type
)),
2162 if (fn
== error_mark_node
)
2163 return error_mark_node
;
2165 expr
= build_unary_op (ADDR_EXPR
, fn
, 0);
2167 my_friendly_assert (comptypes (type
, TREE_TYPE (expr
), 1),
2174 /* All non-type parameters must have one of these types. */
2175 my_friendly_abort (0);
2179 return error_mark_node
;
2182 /* Convert all template arguments to their appropriate types, and return
2183 a vector containing the resulting values. If any error occurs, return
2184 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
2185 Some error messages are issued even if COMPLAIN is zero; for
2186 instance, if a template argument is composed from a local class.
2188 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
2189 provided in ARGLIST, or else trailing parameters must have default
2190 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
2191 deduction for any unspecified trailing arguments.
2193 If IS_TMPL_PARM is non-zero, we will coercing parameters of template
2194 template arguments. In this case, ARGLIST is a chain of TREE_LIST
2195 nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. */
2198 coerce_template_parms (parms
, arglist
, in_decl
,
2200 require_all_arguments
,
2202 tree parms
, arglist
;
2205 int require_all_arguments
;
2208 int nparms
, nargs
, i
, lost
= 0;
2209 tree vec
= NULL_TREE
;
2211 if (arglist
== NULL_TREE
)
2213 else if (TREE_CODE (arglist
) == TREE_VEC
)
2214 nargs
= TREE_VEC_LENGTH (arglist
);
2216 nargs
= list_length (arglist
);
2218 nparms
= TREE_VEC_LENGTH (parms
);
2222 && require_all_arguments
2223 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
2227 error ("incorrect number of parameters (%d, should be %d)",
2231 cp_error_at ("in template expansion for decl `%D'",
2235 return error_mark_node
;
2238 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
&& nargs
== nparms
)
2239 vec
= copy_node (arglist
);
2242 vec
= make_tree_vec (nparms
);
2244 for (i
= 0; i
< nparms
; i
++)
2247 tree parm
= TREE_VEC_ELT (parms
, i
);
2252 arglist
= TREE_CHAIN (arglist
);
2254 if (arg
== error_mark_node
)
2257 arg
= TREE_VALUE (arg
);
2259 else if (is_tmpl_parm
&& i
< nargs
)
2261 arg
= TREE_VEC_ELT (arglist
, i
);
2262 if (arg
== error_mark_node
)
2265 else if (TREE_PURPOSE (parm
) == NULL_TREE
)
2267 my_friendly_assert (!require_all_arguments
, 0);
2270 else if (TREE_CODE (TREE_VALUE (parm
)) == TYPE_DECL
)
2271 arg
= tsubst (TREE_PURPOSE (parm
), vec
, in_decl
);
2273 arg
= tsubst_expr (TREE_PURPOSE (parm
), vec
, in_decl
);
2275 TREE_VEC_ELT (vec
, i
) = arg
;
2278 for (i
= 0; i
< nparms
; i
++)
2280 tree arg
= TREE_VEC_ELT (vec
, i
);
2281 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2283 int is_type
, requires_type
, is_tmpl_type
, requires_tmpl_type
;
2285 if (is_tmpl_parm
&& i
< nargs
)
2287 /* In case we are checking arguments inside a template template
2288 parameter, ARG that does not come from default argument is
2289 also a TREE_LIST node. Note that ARG can also be a TREE_LIST
2290 in other cases such as overloaded functions. */
2291 if (arg
!= NULL_TREE
&& arg
!= error_mark_node
)
2292 arg
= TREE_VALUE (arg
);
2295 if (arg
== NULL_TREE
)
2296 /* We're out of arguments. */
2298 my_friendly_assert (!require_all_arguments
, 0);
2302 if (arg
== error_mark_node
)
2304 cp_error ("template argument %d is invalid", i
+ 1);
2309 if (TREE_CODE (arg
) == TREE_LIST
2310 && TREE_TYPE (arg
) != NULL_TREE
2311 && TREE_CODE (TREE_TYPE (arg
)) == OFFSET_TYPE
)
2313 /* The template argument was the name of some
2314 member function. That's usually
2315 illegal, but static members are OK. In any
2316 case, grab the underlying fields/functions
2317 and issue an error later if required. */
2318 arg
= TREE_VALUE (arg
);
2319 TREE_TYPE (arg
) = unknown_type_node
;
2322 requires_tmpl_type
= TREE_CODE (parm
) == TEMPLATE_DECL
;
2323 requires_type
= TREE_CODE (parm
) == TYPE_DECL
2324 || requires_tmpl_type
;
2326 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2327 we also accept implicitly created TYPE_DECL as a valid argument. */
2328 is_tmpl_type
= (TREE_CODE (arg
) == TEMPLATE_DECL
2329 && TREE_CODE (DECL_TEMPLATE_RESULT (arg
)) == TYPE_DECL
)
2330 || (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
2331 && !CLASSTYPE_TEMPLATE_INFO (arg
))
2332 || (TREE_CODE (arg
) == RECORD_TYPE
2333 && CLASSTYPE_TEMPLATE_INFO (arg
)
2334 && TREE_CODE (TYPE_NAME (arg
)) == TYPE_DECL
2335 && DECL_ARTIFICIAL (TYPE_NAME (arg
))
2336 && requires_tmpl_type
);
2337 if (is_tmpl_type
&& TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
2338 arg
= TYPE_STUB_DECL (arg
);
2339 else if (is_tmpl_type
&& TREE_CODE (arg
) == RECORD_TYPE
)
2340 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
2342 if (is_tmpl_parm
&& i
< nargs
)
2343 is_type
= TREE_CODE (arg
) == TYPE_DECL
|| is_tmpl_type
;
2345 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't' || is_tmpl_type
;
2347 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
2348 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
2350 cp_pedwarn ("to refer to a type member of a template parameter,");
2351 cp_pedwarn (" use `typename %E'", arg
);
2353 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
2354 TREE_OPERAND (arg
, 1));
2357 if (is_type
!= requires_type
)
2363 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2366 cp_error (" expected a constant of type `%T', got `%T'",
2368 (is_tmpl_type
? DECL_NAME (arg
) : arg
));
2370 cp_error (" expected a type, got `%E'", arg
);
2374 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2377 if (is_tmpl_type
^ requires_tmpl_type
)
2381 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2384 cp_error (" expected a type, got `%T'", DECL_NAME (arg
));
2386 cp_error (" expected a class template, got `%T'", arg
);
2389 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2394 if (requires_tmpl_type
)
2396 cp_error ("nested template template parameter not implemented");
2398 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
2405 if (requires_tmpl_type
)
2407 tree parmparm
= DECL_INNERMOST_TEMPLATE_PARMS (parm
);
2408 tree argparm
= DECL_INNERMOST_TEMPLATE_PARMS (arg
);
2410 /* The parameter and argument roles have to be switched
2411 here in order to handle default arguments properly.
2413 template<template <class> class TT> void f(TT<int>)
2414 should be able to accept vector<int> which comes from
2415 template <class T, class Allcator = allocator>
2418 val
= coerce_template_parms (argparm
, parmparm
, in_decl
, 1, 1, 1);
2419 if (val
!= error_mark_node
)
2422 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2424 if (val
!= error_mark_node
2425 && DECL_TEMPLATE_TEMPLATE_PARM_P (val
))
2426 val
= TREE_TYPE (val
);
2430 val
= groktypename (arg
);
2431 if (! processing_template_decl
)
2433 tree t
= target_type (val
);
2434 if (TREE_CODE (t
) != TYPENAME_TYPE
2436 && decl_function_context (TYPE_MAIN_DECL (t
)))
2438 cp_error ("type `%T' composed from a local class is not a valid template-argument",
2440 return error_mark_node
;
2447 tree t
= tsubst (TREE_TYPE (parm
), vec
, in_decl
);
2449 if (processing_template_decl
)
2450 arg
= maybe_fold_nontype_arg (arg
);
2452 if (!uses_template_parms (arg
) && !uses_template_parms (t
))
2453 /* We used to call digest_init here. However, digest_init
2454 will report errors, which we don't want when complain
2455 is zero. More importantly, digest_init will try too
2456 hard to convert things: for example, `0' should not be
2457 converted to pointer type at this point according to
2458 the standard. Accepting this is not merely an
2459 extension, since deciding whether or not these
2460 conversions can occur is part of determining which
2461 function template to call, or whether a given epxlicit
2462 argument specification is legal. */
2463 val
= convert_nontype_argument (t
, arg
);
2467 if (val
== NULL_TREE
)
2468 val
= error_mark_node
;
2469 else if (val
== error_mark_node
&& complain
)
2470 cp_error ("could not convert template argument `%E' to `%T'",
2474 if (val
== error_mark_node
)
2477 TREE_VEC_ELT (vec
, i
) = val
;
2480 return error_mark_node
;
2484 /* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
2485 of template arguments. Returns 0 otherwise. */
2488 comp_template_args (oldargs
, newargs
)
2489 tree oldargs
, newargs
;
2493 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
2496 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
2498 tree nt
= TREE_VEC_ELT (newargs
, i
);
2499 tree ot
= TREE_VEC_ELT (oldargs
, i
);
2503 if (TREE_CODE (nt
) != TREE_CODE (ot
))
2505 if (TREE_CODE (nt
) == TREE_VEC
)
2507 /* For member templates */
2508 if (comp_template_args (nt
, ot
))
2511 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
2513 if (comptypes (ot
, nt
, 1))
2516 else if (cp_tree_equal (ot
, nt
) > 0)
2523 /* Given class template name and parameter list, produce a user-friendly name
2524 for the instantiation. */
2527 mangle_class_name_for_template (name
, parms
, arglist
, ctx
)
2529 tree parms
, arglist
;
2532 static struct obstack scratch_obstack
;
2533 static char *scratch_firstobj
;
2536 if (!scratch_firstobj
)
2537 gcc_obstack_init (&scratch_obstack
);
2539 obstack_free (&scratch_obstack
, scratch_firstobj
);
2540 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
2543 #define buflen sizeof(buf)
2544 #define check if (bufp >= buf+buflen-1) goto too_long
2545 #define ccat(c) *bufp++=(c); check
2546 #define advance bufp+=strlen(bufp); check
2547 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2550 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2552 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2559 if (TREE_CODE (ctx
) == FUNCTION_DECL
)
2560 s
= fndecl_as_string (ctx
, 0);
2561 else if (TREE_CODE_CLASS (TREE_CODE (ctx
)) == 't')
2562 s
= type_as_string_real (ctx
, 0, 1);
2564 my_friendly_abort (0);
2570 nparms
= TREE_VEC_LENGTH (parms
);
2571 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
2572 for (i
= 0; i
< nparms
; i
++)
2574 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
2575 tree arg
= TREE_VEC_ELT (arglist
, i
);
2580 if (TREE_CODE (parm
) == TYPE_DECL
)
2582 cat (type_as_string_real (arg
, 0, 1));
2585 else if (TREE_CODE (parm
) == TEMPLATE_DECL
)
2587 if (TREE_CODE (arg
) == TEMPLATE_DECL
)
2588 /* Already substituted with real template. Just output
2589 the template name here */
2590 cat (IDENTIFIER_POINTER (DECL_NAME (arg
)));
2592 /* Output the parameter declaration */
2593 cat (type_as_string_real (arg
, 0, 1));
2597 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
2599 if (TREE_CODE (arg
) == TREE_LIST
)
2601 /* New list cell was built because old chain link was in
2603 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
2604 arg
= TREE_VALUE (arg
);
2606 /* No need to check arglist against parmlist here; we did that
2607 in coerce_template_parms, called from lookup_template_class. */
2608 cat (expr_as_string (arg
, 0));
2611 char *bufp
= obstack_next_free (&scratch_obstack
);
2613 while (bufp
[offset
- 1] == ' ')
2615 obstack_blank_fast (&scratch_obstack
, offset
);
2617 /* B<C<char> >, not B<C<char>> */
2618 if (bufp
[offset
- 1] == '>')
2623 return (char *) obstack_base (&scratch_obstack
);
2628 fatal ("out of (preallocated) string space creating template instantiation name");
2634 classtype_mangled_name (t
)
2637 if (CLASSTYPE_TEMPLATE_INFO (t
)
2638 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
2640 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
2641 char *mangled_name
= mangle_class_name_for_template
2642 (IDENTIFIER_POINTER (name
),
2643 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
2644 CLASSTYPE_TI_ARGS (t
), DECL_CONTEXT (t
));
2645 tree id
= get_identifier (mangled_name
);
2646 IDENTIFIER_TEMPLATE (id
) = name
;
2650 return TYPE_IDENTIFIER (t
);
2654 add_pending_template (d
)
2659 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
2660 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
2662 ti
= DECL_TEMPLATE_INFO (d
);
2664 if (TI_PENDING_TEMPLATE_FLAG (ti
))
2667 *template_tail
= perm_tree_cons
2668 (current_function_decl
, d
, NULL_TREE
);
2669 template_tail
= &TREE_CHAIN (*template_tail
);
2670 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
2674 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2675 may be either a _DECL or an overloaded function or an
2676 IDENTIFIER_NODE), and ARGLIST. */
2679 lookup_template_function (fns
, arglist
)
2684 if (fns
== NULL_TREE
)
2686 cp_error ("non-template used as template");
2687 return error_mark_node
;
2690 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
2691 copy_to_permanent (arglist
);
2693 return build_min (TEMPLATE_ID_EXPR
,
2695 ? TREE_TYPE (fns
) : unknown_type_node
,
2699 /* Within the scope of a template class S<T>, the name S gets bound
2700 (in build_self_reference) to a TYPE_DECL for the class, not a
2701 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
2702 or one of its enclosing classes, and that type is a template,
2703 return the associated TEMPLATE_DECL. Otherwise, the original
2704 DECL is returned. */
2707 maybe_get_template_decl_from_type_decl (decl
)
2710 return (decl
!= NULL_TREE
2711 && TREE_CODE (decl
) == TYPE_DECL
2712 && DECL_ARTIFICIAL (decl
)
2713 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl
)))
2714 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
)) : decl
;
2717 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2718 parameters, find the desired type.
2720 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2721 Since ARGLIST is build on the decl_obstack, we must copy it here
2722 to keep it from being reclaimed when the decl storage is reclaimed.
2724 IN_DECL, if non-NULL, is the template declaration we are trying to
2727 If the template class is really a local class in a template
2728 function, then the FUNCTION_CONTEXT is the function in which it is
2729 being instantiated. */
2732 lookup_template_class (d1
, arglist
, in_decl
, context
)
2737 tree
template = NULL_TREE
, parmlist
;
2741 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
2743 if (IDENTIFIER_LOCAL_VALUE (d1
)
2744 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1
)))
2745 template = IDENTIFIER_LOCAL_VALUE (d1
);
2749 maybe_get_template_decl_from_type_decl
2750 (IDENTIFIER_CLASS_VALUE (d1
));
2751 if (template == NULL_TREE
)
2752 template = IDENTIFIER_NAMESPACE_VALUE (d1
);
2755 context
= DECL_CONTEXT (template);
2757 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
2759 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1
)) == NULL_TREE
)
2760 return error_mark_node
;
2761 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
2762 d1
= DECL_NAME (template);
2764 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
2766 template = CLASSTYPE_TI_TEMPLATE (d1
);
2767 d1
= DECL_NAME (template);
2769 else if (TREE_CODE (d1
) == TEMPLATE_DECL
2770 && TREE_CODE (DECL_RESULT (d1
)) == TYPE_DECL
)
2773 d1
= DECL_NAME (template);
2774 context
= DECL_CONTEXT (template);
2777 my_friendly_abort (272);
2779 /* With something like `template <class T> class X class X { ... };'
2780 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2781 We don't want to do that, but we have to deal with the situation, so
2782 let's give them some syntax errors to chew on instead of a crash. */
2784 return error_mark_node
;
2785 if (TREE_CODE (template) != TEMPLATE_DECL
)
2787 cp_error ("non-template type `%T' used as a template", d1
);
2789 cp_error_at ("for template declaration `%D'", in_decl
);
2790 return error_mark_node
;
2793 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2795 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2796 template arguments */
2798 tree parm
= copy_template_template_parm (TREE_TYPE (template));
2799 tree template2
= TYPE_STUB_DECL (parm
);
2802 CLASSTYPE_GOT_SEMICOLON (parm
) = 1;
2803 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2805 arglist2
= coerce_template_parms (parmlist
, arglist
, template, 1, 1, 0);
2806 if (arglist2
== error_mark_node
)
2807 return error_mark_node
;
2809 arglist2
= copy_to_permanent (arglist2
);
2810 CLASSTYPE_TEMPLATE_INFO (parm
)
2811 = perm_tree_cons (template2
, arglist2
, NULL_TREE
);
2812 TYPE_SIZE (parm
) = 0;
2815 else if (PRIMARY_TEMPLATE_P (template)
2816 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2819 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
2821 arglist
= coerce_template_parms (parmlist
, arglist
, template,
2823 if (arglist
== error_mark_node
)
2824 return error_mark_node
;
2825 if (uses_template_parms (arglist
))
2828 if (comp_template_args
2829 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
2830 found
= TREE_TYPE (template);
2833 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
2834 found
; found
= TREE_CHAIN (found
))
2836 if (TI_USES_TEMPLATE_PARMS (found
)
2837 && comp_template_args (TREE_PURPOSE (found
), arglist
))
2841 found
= TREE_VALUE (found
);
2846 if (can_free (&permanent_obstack
, arglist
))
2847 obstack_free (&permanent_obstack
, arglist
);
2852 /* FIXME avoid duplication. */
2853 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
2857 id
= get_identifier (mangled_name
);
2858 IDENTIFIER_TEMPLATE (id
) = d1
;
2860 maybe_push_to_top_level (uses_template_parms (arglist
));
2861 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
2863 if (context
!= NULL_TREE
)
2865 /* Set up the context for the type_decl correctly. Note
2866 that we must clear DECL_ASSEMBLER_NAME to fool
2867 build_overload_name into creating a new name. */
2868 tree type_decl
= TYPE_STUB_DECL (t
);
2870 TYPE_CONTEXT (t
) = context
;
2871 DECL_CONTEXT (type_decl
) = context
;
2872 DECL_ASSEMBLER_NAME (type_decl
) = DECL_NAME (type_decl
);
2873 DECL_ASSEMBLER_NAME (type_decl
) =
2874 get_identifier (build_overload_name (t
, 1, 1));
2877 pop_from_top_level ();
2881 tree type_ctx
= TYPE_CONTEXT (TREE_TYPE (template));
2882 tree args
= tsubst (CLASSTYPE_TI_ARGS (type_ctx
), arglist
, in_decl
);
2883 tree ctx
= lookup_template_class (type_ctx
, args
,
2884 in_decl
, NULL_TREE
);
2886 arglist
= CLASSTYPE_TI_ARGS (ctx
);
2888 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
2890 int save_temp
= processing_template_decl
;
2891 processing_template_decl
= 0;
2892 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
2893 processing_template_decl
= save_temp
;
2897 t
= lookup_nested_type_by_name (ctx
, id
);
2898 my_friendly_assert (t
!= NULL_TREE
, 42);
2902 /* Seems to be wanted. */
2903 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
2905 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2907 arglist
= copy_to_permanent (arglist
);
2908 CLASSTYPE_TEMPLATE_INFO (t
)
2909 = perm_tree_cons (template, arglist
, NULL_TREE
);
2910 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2911 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
2912 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2913 = uses_template_parms (arglist
);
2915 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
2917 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2918 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
2919 if (! uses_template_parms (arglist
))
2920 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
2921 = get_identifier (build_overload_name (t
, 1, 1));
2923 if (flag_external_templates
&& ! uses_template_parms (arglist
)
2924 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2925 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2926 add_pending_template (t
);
2932 /* Should be defined in parse.h. */
2935 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
2936 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
2937 If FN returns non-zero, the iteration is terminated, and
2938 for_each_template_parm returns 1. Otherwise, the iteration
2939 continues. If FN never returns a non-zero value, the value
2940 returned by for_each_template_parm is 0. If FN is NULL, it is
2941 considered to be the function which always returns 1. */
2944 for_each_template_parm (t
, fn
, data
)
2951 switch (TREE_CODE (t
))
2955 /* We assume that the object must be instantiated in order to build
2956 the COMPONENT_REF, so we test only whether the type of the
2957 COMPONENT_REF uses template parms. */
2958 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2960 case IDENTIFIER_NODE
:
2961 if (!IDENTIFIER_TEMPLATE (t
))
2963 my_friendly_abort (42);
2965 /* aggregates of tree nodes */
2968 int i
= TREE_VEC_LENGTH (t
);
2970 if (for_each_template_parm (TREE_VEC_ELT (t
, i
), fn
, data
))
2975 if (for_each_template_parm (TREE_PURPOSE (t
), fn
, data
)
2976 || for_each_template_parm (TREE_VALUE (t
), fn
, data
))
2978 return for_each_template_parm (TREE_CHAIN (t
), fn
, data
);
2980 /* constructed type nodes */
2982 case REFERENCE_TYPE
:
2983 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2985 if (TYPE_PTRMEMFUNC_FLAG (t
))
2986 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t
),
2989 if (! CLASSTYPE_TEMPLATE_INFO (t
))
2991 return for_each_template_parm (TREE_VALUE
2992 (CLASSTYPE_TEMPLATE_INFO (t
)),
2995 if (for_each_template_parm (TYPE_ARG_TYPES (t
), fn
, data
))
2997 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
2999 if (for_each_template_parm (TYPE_DOMAIN (t
), fn
, data
))
3001 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3003 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t
), fn
, data
))
3005 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3007 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t
), fn
, data
))
3009 if (for_each_template_parm (TYPE_ARG_TYPES (t
), fn
, data
))
3011 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3015 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3018 /* A template template parameter is encountered */
3019 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t
))
3020 /* We are parsing a template declaration */
3022 /* We are instantiating templates with template template
3027 if (for_each_template_parm (DECL_INITIAL (t
), fn
, data
))
3029 goto check_type_and_context
;
3033 /* ??? What about FIELD_DECLs? */
3034 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
3035 && for_each_template_parm (DECL_TI_ARGS (t
), fn
, data
))
3039 check_type_and_context
:
3040 if (for_each_template_parm (TREE_TYPE (t
), fn
, data
))
3042 if (DECL_CONTEXT (t
)
3043 && for_each_template_parm (DECL_CONTEXT (t
), fn
, data
))
3048 return for_each_template_parm (TREE_TYPE (t
), fn
, data
);
3050 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3052 /* template parm nodes */
3053 case TEMPLATE_TYPE_PARM
:
3054 case TEMPLATE_TEMPLATE_PARM
:
3055 case TEMPLATE_PARM_INDEX
:
3057 return (*fn
)(t
, data
);
3061 /* simple type nodes */
3063 if (for_each_template_parm (TYPE_MIN_VALUE (t
), fn
, data
))
3065 return for_each_template_parm (TYPE_MAX_VALUE (t
), fn
, data
);
3077 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
3078 if (for_each_template_parm (TREE_VALUE (v
), fn
, data
))
3090 /* Non-error_mark_node ERROR_MARKs are bad things. */
3091 my_friendly_assert (t
== error_mark_node
, 274);
3100 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3103 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
3104 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3105 (TREE_TYPE (t
)), fn
, data
);
3106 return for_each_template_parm (TREE_OPERAND (t
, 1), fn
, data
);
3110 case REINTERPRET_CAST_EXPR
:
3111 case CONST_CAST_EXPR
:
3112 case STATIC_CAST_EXPR
:
3113 case DYNAMIC_CAST_EXPR
:
3121 return for_each_template_parm (TREE_OPERAND (t
, 0), fn
, data
);
3124 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3132 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
3133 if (for_each_template_parm (TREE_OPERAND (t
, i
), fn
, data
))
3140 sorry ("testing %s for template parms",
3141 tree_code_name
[(int) TREE_CODE (t
)]);
3142 my_friendly_abort (82);
3149 uses_template_parms (t
)
3152 return for_each_template_parm (t
, 0, 0);
3155 static struct tinst_level
*current_tinst_level
= 0;
3156 static struct tinst_level
*free_tinst_level
= 0;
3157 static int tinst_depth
= 0;
3158 extern int max_tinst_depth
;
3159 #ifdef GATHER_STATISTICS
3160 int depth_reached
= 0;
3163 /* Print out all the template instantiations that we are currently
3167 print_template_context ()
3169 struct tinst_level
*p
= current_tinst_level
;
3171 char *file
= input_filename
;
3173 for (; p
; p
= p
->next
)
3175 cp_error (" instantiated from `%D'", p
->decl
);
3177 input_filename
= p
->file
;
3179 error (" instantiated from here");
3182 input_filename
= file
;
3186 push_tinst_level (d
)
3189 struct tinst_level
*new;
3191 if (tinst_depth
>= max_tinst_depth
)
3193 /* If the instantiation in question still has unbound template parms,
3194 we don't really care if we can't instantiate it, so just return.
3195 This happens with base instantiation for implicit `typename'. */
3196 if (uses_template_parms (d
))
3199 error ("template instantiation depth exceeds maximum of %d",
3201 error (" (use -ftemplate-depth-NN to increase the maximum)");
3202 cp_error (" instantiating `%D'", d
);
3204 print_template_context ();
3209 if (free_tinst_level
)
3211 new = free_tinst_level
;
3212 free_tinst_level
= new->next
;
3215 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
3219 new->file
= input_filename
;
3220 new->next
= current_tinst_level
;
3221 current_tinst_level
= new;
3224 #ifdef GATHER_STATISTICS
3225 if (tinst_depth
> depth_reached
)
3226 depth_reached
= tinst_depth
;
3235 struct tinst_level
*old
= current_tinst_level
;
3237 current_tinst_level
= old
->next
;
3238 old
->next
= free_tinst_level
;
3239 free_tinst_level
= old
;
3243 struct tinst_level
*
3246 struct tinst_level
*p
= current_tinst_level
;
3249 for (; p
->next
; p
= p
->next
)
3255 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
3256 vector of template arguments, as for tsubst.
3258 Returns an appropriate tsbust'd friend declaration. */
3261 tsubst_friend_function (decl
, args
)
3267 if (TREE_CODE (decl
) == FUNCTION_DECL
3268 && DECL_TEMPLATE_INSTANTIATION (decl
)
3269 && TREE_CODE (DECL_TI_TEMPLATE (decl
)) != TEMPLATE_DECL
)
3270 /* This was a friend declared with an explicit template
3271 argument list, e.g.:
3275 to indicate that f was a template instantiation, not a new
3276 function declaration. Now, we have to figure out what
3277 instantiation of what template. */
3285 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl
),
3287 tsubst (DECL_TI_ARGS (decl
),
3290 /* Temporarily remove the DECL_TEMPLATE_INFO so as not to
3292 tinfo
= DECL_TEMPLATE_INFO (decl
);
3293 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
3294 new_friend
= tsubst (decl
, args
, NULL_TREE
);
3295 DECL_TEMPLATE_INFO (decl
) = tinfo
;
3297 tmpl
= determine_specialization (template_id
,
3301 return instantiate_template (tmpl
, new_args
);
3304 new_friend
= tsubst (decl
, args
, NULL_TREE
);
3306 /* The new_friend will look like an instantiation, to the
3307 compiler, but is not an instantiation from the point of view of
3308 the language. For example, we might have had:
3310 template <class T> struct S {
3311 template <class U> friend void f(T, U);
3314 Then, in S<int>, template <class U> void f(int, U) is not an
3315 instantiation of anything. */
3316 DECL_USE_TEMPLATE (new_friend
) = 0;
3317 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
3318 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend
)) = 0;
3320 if (DECL_CONTEXT (new_friend
) == NULL_TREE
)
3322 if (TREE_CODE (new_friend
) == TEMPLATE_DECL
)
3323 /* This declaration is a `primary' template. */
3324 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend
))
3327 new_friend
= pushdecl_top_level (new_friend
);
3329 else if (TYPE_SIZE (DECL_CONTEXT (new_friend
)))
3331 /* Check to see that the declaration is really present, and,
3332 possibly obtain an improved declaration. */
3333 tree fn
= check_classfn (DECL_CONTEXT (new_friend
),
3345 instantiate_class_template (type
)
3348 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
3349 tree typedecl
, outer_args
;
3351 if (type
== error_mark_node
)
3352 return error_mark_node
;
3354 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
3356 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
3359 template = TI_TEMPLATE (template_info
);
3360 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
3361 args
= TI_ARGS (template_info
);
3363 if (DECL_TEMPLATE_INFO (template))
3365 outer_args
= DECL_TI_ARGS (template);
3366 while (DECL_TEMPLATE_INFO (template))
3367 template = DECL_TI_TEMPLATE (template);
3370 outer_args
= NULL_TREE
;
3372 t
= most_specialized_class
3373 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
, outer_args
);
3375 if (t
== error_mark_node
)
3377 char *str
= "candidates are:";
3378 cp_error ("ambiguous class template instantiation for `%#T'", type
);
3379 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
3381 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
3384 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
3388 TYPE_BEING_DEFINED (type
) = 1;
3389 return error_mark_node
;
3392 pattern
= TREE_TYPE (t
);
3394 pattern
= TREE_TYPE (template);
3396 if (TYPE_SIZE (pattern
) == NULL_TREE
)
3400 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
3403 if (pedantic
&& uses_template_parms (args
))
3404 /* If there are still template parameters amongst the args, then
3405 we can't instantiate the type; there's no telling whether or not one
3406 of the template parameters might eventually be instantiated to some
3407 value that results in a specialization being used. */
3410 /* We must copy the arguments to the permanent obstack since
3411 during the tsubst'ing below they may wind up in the
3412 DECL_TI_ARGS of some instantiated member template. */
3413 args
= copy_to_permanent (args
);
3415 TYPE_BEING_DEFINED (type
) = 1;
3417 if (! push_tinst_level (type
))
3420 maybe_push_to_top_level (uses_template_parms (type
));
3421 pushclass (type
, 0);
3424 args
= add_to_template_args (outer_args
, args
);
3426 if (flag_external_templates
)
3428 if (flag_alt_external_templates
)
3430 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
3431 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
3432 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
3433 = (! CLASSTYPE_INTERFACE_ONLY (type
)
3434 && CLASSTYPE_INTERFACE_KNOWN (type
));
3438 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
3439 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3440 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
3441 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
3442 = (! CLASSTYPE_INTERFACE_ONLY (type
)
3443 && CLASSTYPE_INTERFACE_KNOWN (type
));
3448 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
3449 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
3452 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
3453 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
3454 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
3455 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
3456 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
3457 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
3458 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
3459 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
3460 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
3461 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
3462 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
3463 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
3464 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
3465 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
3466 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
3467 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
3468 TYPE_USES_COMPLEX_INHERITANCE (type
)
3469 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
3470 TYPE_USES_MULTIPLE_INHERITANCE (type
)
3471 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
3472 TYPE_USES_VIRTUAL_BASECLASSES (type
)
3473 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
3474 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
3475 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
3477 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
3479 /* If this is a partial instantiation, don't tsubst anything. We will
3480 only use this type for implicit typename, so the actual contents don't
3481 matter. All that matters is whether a particular name is a type. */
3482 if (uses_template_parms (type
))
3484 TYPE_BINFO_BASETYPES (type
) = TYPE_BINFO_BASETYPES (pattern
);
3485 TYPE_FIELDS (type
) = TYPE_FIELDS (pattern
);
3486 TYPE_METHODS (type
) = TYPE_METHODS (pattern
);
3487 CLASSTYPE_TAGS (type
) = CLASSTYPE_TAGS (pattern
);
3488 TYPE_SIZE (type
) = integer_zero_node
;
3493 tree binfo
= TYPE_BINFO (type
);
3494 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
3500 int len
= TREE_VEC_LENGTH (pbases
);
3501 bases
= make_tree_vec (len
);
3502 for (i
= 0; i
< len
; ++i
)
3506 TREE_VEC_ELT (bases
, i
) = elt
3507 = tsubst (TREE_VEC_ELT (pbases
, i
), args
, NULL_TREE
);
3508 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
3510 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
3512 ("base type `%T' of `%T' fails to be a struct or class type",
3513 TREE_TYPE (elt
), type
);
3514 else if (TYPE_SIZE (complete_type (TREE_TYPE (elt
))) == NULL_TREE
)
3515 cp_error ("base class `%T' of `%T' has incomplete type",
3516 TREE_TYPE (elt
), type
);
3518 /* Don't initialize this until the vector is filled out, or
3519 lookups will crash. */
3520 BINFO_BASETYPES (binfo
) = bases
;
3524 field_chain
= &TYPE_FIELDS (type
);
3526 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
3528 tree tag
= TREE_VALUE (t
);
3530 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
3531 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
3533 (void) tsubst_enum (tag
, args
, field_chain
);
3534 while (*field_chain
)
3536 DECL_FIELD_CONTEXT (*field_chain
) = type
;
3537 field_chain
= &TREE_CHAIN (*field_chain
);
3541 tsubst (tag
, args
, NULL_TREE
);
3544 /* Don't replace enum constants here. */
3545 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
3546 if (TREE_CODE (t
) != CONST_DECL
)
3548 tree r
= tsubst (t
, args
, NULL_TREE
);
3549 if (TREE_CODE (r
) == VAR_DECL
)
3551 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
3552 /* Perhaps we should do more of grokfield here. */
3554 DECL_IN_AGGR_P (r
) = 1;
3555 DECL_EXTERNAL (r
) = 1;
3556 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
3560 field_chain
= &TREE_CHAIN (r
);
3563 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
3564 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
3566 if (DECL_CONSTRUCTOR_P (t
))
3567 grok_ctor_properties (type
, t
);
3568 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
3569 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
3572 /* Construct the DECL_FRIENDLIST for the new class type. */
3573 typedecl
= TYPE_MAIN_DECL (type
);
3574 for (t
= DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
));
3580 DECL_FRIENDLIST (typedecl
)
3581 = tree_cons (TREE_PURPOSE (t
), NULL_TREE
,
3582 DECL_FRIENDLIST (typedecl
));
3584 for (friends
= TREE_VALUE (t
);
3585 friends
!= NULL_TREE
;
3586 friends
= TREE_CHAIN (friends
))
3588 if (TREE_PURPOSE (friends
) == error_mark_node
)
3590 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3591 = tree_cons (error_mark_node
,
3592 tsubst_friend_function (TREE_VALUE (friends
),
3594 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3598 TREE_VALUE (DECL_FRIENDLIST (typedecl
))
3599 = tree_cons (tsubst (TREE_PURPOSE (friends
), args
, NULL_TREE
),
3601 TREE_VALUE (DECL_FRIENDLIST (typedecl
)));
3607 t
= CLASSTYPE_FRIEND_CLASSES (type
)
3608 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
, NULL_TREE
);
3610 /* This does injection for friend classes. */
3611 for (; t
; t
= TREE_CHAIN (t
))
3612 TREE_VALUE (t
) = xref_tag_from_type (TREE_VALUE (t
), NULL_TREE
, 1);
3614 /* This does injection for friend functions. */
3615 if (!processing_template_decl
)
3617 t
= tsubst (DECL_TEMPLATE_INJECT (template), args
, NULL_TREE
);
3619 for (; t
; t
= TREE_CHAIN (t
))
3621 tree d
= TREE_VALUE (t
);
3623 if (TREE_CODE (d
) == TYPE_DECL
)
3624 /* Already injected. */;
3630 for (t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
3631 if (TREE_CODE (t
) == FIELD_DECL
)
3633 TREE_TYPE (t
) = complete_type (TREE_TYPE (t
));
3634 require_complete_type (t
);
3637 type
= finish_struct_1 (type
, 0);
3638 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
3640 repo_template_used (type
);
3641 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
3642 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
3645 TYPE_BEING_DEFINED (type
) = 0;
3648 pop_from_top_level ();
3658 if (t1
== NULL_TREE
)
3659 return t2
== NULL_TREE
;
3660 if (t2
== NULL_TREE
)
3662 /* Don't care if one declares its arg const and the other doesn't -- the
3663 main variant of the arg type is all that matters. */
3664 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
3665 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
3667 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
3671 lookup_nested_type_by_name (ctype
, name
)
3676 complete_type (ctype
);
3678 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
3680 if (name
== TREE_PURPOSE (t
)
3681 /* this catches typedef enum { foo } bar; */
3682 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
3683 return TREE_VALUE (t
);
3688 /* If arg is a non-type template parameter that does not depend on template
3689 arguments, fold it like we weren't in the body of a template. */
3692 maybe_fold_nontype_arg (arg
)
3695 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
3696 && !uses_template_parms (arg
))
3698 /* Sometimes, one of the args was an expression involving a
3699 template constant parameter, like N - 1. Now that we've
3700 tsubst'd, we might have something like 2 - 1. This will
3701 confuse lookup_template_class, so we do constant folding
3702 here. We have to unset processing_template_decl, to
3703 fool build_expr_from_tree() into building an actual
3706 int saved_processing_template_decl
= processing_template_decl
;
3707 processing_template_decl
= 0;
3708 arg
= fold (build_expr_from_tree (arg
));
3709 processing_template_decl
= saved_processing_template_decl
;
3714 /* Return the TREE_VEC with the arguments for the innermost template header,
3715 where ARGS is either that or the VEC of VECs for all the arguments.
3717 If is_spec, then we are dealing with a specialization of a member
3718 template, and want the second-innermost args, the innermost ones that
3719 are instantiated. */
3722 innermost_args (args
, is_spec
)
3726 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3727 return TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1 - is_spec
);
3731 /* Take the tree structure T and replace template parameters used therein
3732 with the argument vector ARGS. IN_DECL is an associated decl for
3735 tsubst is used for dealing with types, decls and the like; for
3736 expressions, use tsubst_expr or tsubst_copy. */
3739 tsubst (t
, args
, in_decl
)
3745 if (t
== NULL_TREE
|| t
== error_mark_node
3746 || t
== integer_type_node
3747 || t
== void_type_node
3748 || t
== char_type_node
)
3751 type
= TREE_TYPE (t
);
3752 if (type
== unknown_type_node
)
3753 my_friendly_abort (42);
3754 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
3755 && TREE_CODE (t
) != TYPENAME_TYPE
3756 && TREE_CODE (t
) != TEMPLATE_DECL
)
3757 type
= tsubst (type
, args
, in_decl
);
3759 switch (TREE_CODE (t
))
3762 if (TYPE_PTRMEMFUNC_P (t
))
3764 tree r
= build_ptrmemfunc_type
3765 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, in_decl
));
3766 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3770 /* else fall through */
3772 if (uses_template_parms (t
))
3774 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, in_decl
);
3780 ? tsubst (TYPE_CONTEXT (t
), args
, in_decl
) : NULL_TREE
;
3782 r
= lookup_template_class (t
, argvec
, in_decl
, context
);
3784 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3788 /* else fall through */
3790 case IDENTIFIER_NODE
:
3803 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, in_decl
);
3804 if (ctx
== NULL_TREE
)
3806 else if (ctx
== current_function_decl
)
3807 return lookup_name (TYPE_IDENTIFIER (t
), 1);
3809 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
3813 if (t
== integer_type_node
)
3816 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
3817 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
3821 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
3822 max
= tsubst_expr (max
, args
, in_decl
);
3823 if (processing_template_decl
)
3825 tree itype
= make_node (INTEGER_TYPE
);
3826 TYPE_MIN_VALUE (itype
) = size_zero_node
;
3827 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
3832 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
3833 return build_index_2_type (size_zero_node
, max
);
3836 case TEMPLATE_TYPE_PARM
:
3837 case TEMPLATE_TEMPLATE_PARM
:
3838 case TEMPLATE_PARM_INDEX
:
3845 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
3846 || TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3848 idx
= TEMPLATE_TYPE_IDX (t
);
3849 level
= TEMPLATE_TYPE_LEVEL (t
);
3853 idx
= TEMPLATE_PARM_IDX (t
);
3854 level
= TEMPLATE_PARM_LEVEL (t
);
3857 if (TREE_VEC_LENGTH (args
) > 0)
3859 tree arg
= NULL_TREE
;
3861 if (TREE_VEC_ELT (args
, 0) != NULL_TREE
3862 && TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
3864 levels
= TREE_VEC_LENGTH (args
);
3865 if (level
<= levels
)
3867 (TREE_VEC_ELT (args
, level
- 1), idx
);
3873 arg
= TREE_VEC_ELT (args
, idx
);
3876 if (arg
!= NULL_TREE
)
3878 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
3879 return cp_build_type_variant
3880 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
3881 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
3882 else if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
)
3884 if (CLASSTYPE_TEMPLATE_INFO (t
))
3886 /* We are processing a type constructed from
3887 a template template parameter */
3888 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
),
3892 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3893 we are resolving nested-types in the signature of
3894 a member function templates.
3895 Otherwise ARG is a TEMPLATE_DECL and is the real
3896 template to be instantiated. */
3897 if (TREE_CODE (arg
) == TEMPLATE_TEMPLATE_PARM
)
3898 arg
= TYPE_NAME (arg
);
3900 r
= lookup_template_class (DECL_NAME (arg
),
3902 DECL_CONTEXT (arg
));
3903 return cp_build_type_variant (r
, TYPE_READONLY (t
),
3907 /* We are processing a template argument list. */
3916 /* This can happen during the attempted tsubst'ing in
3917 unify. This means that we don't yet have any information
3918 about the template parameter in question. */
3921 /* If we get here, we must have been looking at a parm for a
3922 more deeply nested template. Make a new version of this
3923 template parameter, but with a lower level. */
3924 switch (TREE_CODE (t
))
3926 case TEMPLATE_TYPE_PARM
:
3927 case TEMPLATE_TEMPLATE_PARM
:
3929 TEMPLATE_TYPE_PARM_INDEX (r
)
3930 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t
),
3932 TYPE_STUB_DECL (r
) = TYPE_NAME (r
) = TEMPLATE_TYPE_DECL (r
);
3933 TYPE_MAIN_VARIANT (r
) = r
;
3934 TYPE_POINTER_TO (r
) = NULL_TREE
;
3935 TYPE_REFERENCE_TO (r
) = NULL_TREE
;
3937 if (TREE_CODE (t
) == TEMPLATE_TEMPLATE_PARM
3938 && CLASSTYPE_TEMPLATE_INFO (t
))
3940 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, in_decl
);
3941 CLASSTYPE_TEMPLATE_INFO (r
)
3942 = perm_tree_cons (TYPE_NAME (t
), argvec
, NULL_TREE
);
3946 case TEMPLATE_PARM_INDEX
:
3947 r
= reduce_template_parm_level (t
, TREE_TYPE (t
), levels
);
3951 my_friendly_abort (0);
3959 /* We can get here when processing a member template function
3960 of a template class. */
3962 tree decl
= DECL_TEMPLATE_RESULT (t
);
3966 int is_template_template_parm
= DECL_TEMPLATE_TEMPLATE_PARM_P (t
);
3968 if (!is_template_template_parm
)
3970 /* We might already have an instance of this template. */
3971 spec
= retrieve_specialization (t
, args
);
3972 if (spec
!= NULL_TREE
)
3976 /* Make a new template decl. It will be similar to the
3977 original, but will record the current template arguments.
3978 We also create a new function declaration, which is just
3979 like the old one, but points to this new template, rather
3980 than the old one. */
3981 tmpl
= copy_node (t
);
3982 copy_lang_decl (tmpl
);
3983 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
3984 DECL_CHAIN (tmpl
) = NULL_TREE
;
3985 TREE_CHAIN (tmpl
) = NULL_TREE
;
3987 if (is_template_template_parm
)
3989 tree new_decl
= tsubst (decl
, args
, in_decl
);
3990 DECL_RESULT (tmpl
) = new_decl
;
3991 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
3995 DECL_CONTEXT (tmpl
) = tsubst (DECL_CONTEXT (t
),
3997 DECL_CLASS_CONTEXT (tmpl
) = tsubst (DECL_CLASS_CONTEXT (t
),
3999 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
4001 if (TREE_CODE (decl
) == TYPE_DECL
)
4003 tree new_type
= tsubst (TREE_TYPE (t
), args
, in_decl
);
4004 TREE_TYPE (tmpl
) = new_type
;
4005 CLASSTYPE_TI_TEMPLATE (new_type
) = tmpl
;
4006 DECL_RESULT (tmpl
) = TYPE_MAIN_DECL (new_type
);
4010 tree new_decl
= tsubst (decl
, args
, in_decl
);
4011 DECL_RESULT (tmpl
) = new_decl
;
4012 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
4013 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
4016 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
4017 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
4019 /* The template parameters for this new template are all the
4020 template parameters for the old template, except the
4021 outermost level of parameters. */
4022 for (new_parms
= &DECL_TEMPLATE_PARMS (tmpl
),
4023 parms
= DECL_TEMPLATE_PARMS (t
);
4024 TREE_CHAIN (parms
) != NULL_TREE
;
4025 new_parms
= &(TREE_CHAIN (*new_parms
)),
4026 parms
= TREE_CHAIN (parms
))
4029 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms
)));
4032 for (i
= 0; i
< TREE_VEC_LENGTH (new_vec
); ++i
)
4034 tree default_value
=
4035 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4037 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms
), i
));
4039 TREE_VEC_ELT (new_vec
, i
)
4040 = build_tree_list (tsubst (default_value
, args
, in_decl
),
4041 tsubst (parm_decl
, args
, in_decl
));
4046 tree_cons (build_int_2 (0,
4048 (TREE_PURPOSE (parms
)) - 1),
4053 if (PRIMARY_TEMPLATE_P (t
))
4054 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
4056 /* We don't partially instantiate partial specializations. */
4057 if (TREE_CODE (decl
) == TYPE_DECL
)
4060 /* What should we do with the specializations of this member
4061 template? Are they specializations of this new template,
4062 or instantiations of the templates they previously were?
4063 this new template? And where should their
4064 DECL_TI_TEMPLATES point? */
4065 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
4066 for (spec
= DECL_TEMPLATE_SPECIALIZATIONS (t
);
4068 spec
= TREE_CHAIN (spec
))
4070 /* It helps to consider example here. Consider:
4081 Now, for example, we are instantiating S<int>::f(U u).
4082 We want to make a template:
4087 It will have a specialization, for the case U = int*, of
4091 void S<int>::f<int*>(int*);
4093 This specialization will be an instantiation of
4094 the specialization given in the declaration of S, with
4095 argument list int*. */
4097 tree fn
= TREE_VALUE (spec
);
4101 if (!DECL_TEMPLATE_SPECIALIZATION (fn
))
4102 /* Instantiations are on the same list, but they're of
4103 no concern to us. */
4106 spec_args
= tsubst (DECL_TI_ARGS (fn
), args
,
4108 new_fn
= tsubst (DECL_RESULT (fn
), args
,
4110 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) =
4111 perm_tree_cons (spec_args
, new_fn
,
4112 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
));
4115 /* Record this partial instantiation. */
4116 register_specialization (tmpl
, t
, args
);
4126 tree tmpl
= NULL_TREE
;
4129 if (DECL_CONTEXT (t
) != NULL_TREE
4130 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
4132 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
4136 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, t
);
4137 type
= tsubst (type
, args
, in_decl
);
4143 type
= tsubst (type
, args
, in_decl
);
4146 /* If we are instantiating a specialization, get the other args. */
4147 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
4151 tmpl
= DECL_TI_TEMPLATE (t
);
4153 /* Start by getting the innermost args. */
4154 argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
4156 if (DECL_TEMPLATE_INFO (tmpl
))
4157 argvec
= complete_template_args (tmpl
, argvec
, 0);
4159 /* Do we already have this instantiation? */
4160 spec
= retrieve_specialization (tmpl
, argvec
);
4165 /* We do NOT check for matching decls pushed separately at this
4166 point, as they may not represent instantiations of this
4167 template, and in any case are considered separate under the
4168 discrete model. Instead, see add_maybe_template. */
4172 DECL_USE_TEMPLATE (r
) = 0;
4173 TREE_TYPE (r
) = type
;
4176 = tsubst (DECL_CONTEXT (t
), args
, t
);
4177 DECL_CLASS_CONTEXT (r
) = ctx
;
4179 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
4180 IDENTIFIER_POINTER (DECL_NAME (r
)),
4181 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
4183 /* Type-conversion operator. Reconstruct the name, in
4184 case it's the name of one of the template's parameters. */
4185 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
4188 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
4190 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
4191 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
4192 buf
= (char *) alloca (strlen (dbuf
)
4193 + sizeof (DESTRUCTOR_DECL_PREFIX
));
4194 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
4197 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
4201 /* Instantiations of template functions must be mangled
4202 specially, in order to conform to 14.5.5.1
4203 [temp.over.link]. We use in_decl below rather than
4204 DECL_TI_TEMPLATE (r) because the latter is set to
4205 NULL_TREE in instantiate_decl. */
4209 if (DECL_TEMPLATE_INFO (r
))
4210 tmpl
= DECL_TI_TEMPLATE (r
);
4214 /* tmpl will be NULL if this is a specialization of a
4215 member function of a template class. */
4216 if (name_mangling_version
< 1
4217 || tmpl
== NULL_TREE
4218 || (member
&& !is_member_template (tmpl
)
4219 && !DECL_TEMPLATE_INFO (tmpl
)))
4221 arg_types
= TYPE_ARG_TYPES (type
);
4222 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
4223 arg_types
= hash_tree_chain
4224 (build_pointer_type (DECL_CONTEXT (r
)),
4227 DECL_ASSEMBLER_NAME (r
)
4228 = build_decl_overload (DECL_NAME (r
), arg_types
,
4236 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl
))
4238 /* We pass the outermost template parameters to
4239 build_template_decl_overload, since the innermost
4240 template parameters are still just template
4241 parameters; there are no corresponding subsitution
4242 arguments. Levels of parms that have been bound
4243 before are not represented in DECL_TEMPLATE_PARMS. */
4244 tparms
= DECL_TEMPLATE_PARMS (tmpl
);
4245 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
4246 tparms
= TREE_CHAIN (tparms
);
4248 targs
= innermost_args (args
, 0);
4252 /* If the template is a specialization, then it is
4253 a member template specialization. We have
4256 template <class T> struct S {
4257 template <int i> void f();
4258 template <> void f<7>();
4261 and now we are forming S<double>::f<7>.
4262 Therefore, the template parameters of interest
4263 are those that are specialized by the template
4264 (i.e., the int), not those we are using to
4265 instantiate the template, i.e. the double. */
4266 tparms
= DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl
));
4267 targs
= DECL_TI_ARGS (tmpl
);
4270 my_friendly_assert (tparms
!= NULL_TREE
4271 && TREE_CODE (tparms
) == TREE_LIST
,
4273 tparms
= TREE_VALUE (tparms
);
4275 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
4276 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
4277 arg_types
= hash_tree_chain
4278 (build_pointer_type (DECL_CONTEXT (r
)),
4281 DECL_ASSEMBLER_NAME (r
)
4282 = build_template_decl_overload
4283 (DECL_NAME (r
), arg_types
,
4284 TREE_TYPE (TREE_TYPE (tmpl
)),
4285 tparms
, targs
, member
);
4289 make_decl_rtl (r
, NULL_PTR
, 1);
4291 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, t
);
4292 DECL_MAIN_VARIANT (r
) = r
;
4293 DECL_RESULT (r
) = NULL_TREE
;
4294 DECL_INITIAL (r
) = NULL_TREE
;
4296 TREE_STATIC (r
) = 0;
4297 TREE_PUBLIC (r
) = TREE_PUBLIC (t
);
4298 DECL_EXTERNAL (r
) = 1;
4299 DECL_INTERFACE_KNOWN (r
) = 0;
4300 DECL_DEFER_OUTPUT (r
) = 0;
4301 TREE_CHAIN (r
) = NULL_TREE
;
4302 DECL_CHAIN (r
) = NULL_TREE
;
4303 DECL_PENDING_INLINE_INFO (r
) = 0;
4306 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
4307 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
4309 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
4311 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
4313 /* If we're not using ANSI overloading, then we might have
4314 called duplicate_decls above, and gotten back an
4315 preexisting version of this function. We treat such a
4316 function as a specialization. Otherwise, we cleared
4317 both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
4318 this condition will be false. */
4319 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
4320 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
4322 SET_DECL_IMPLICIT_INSTANTIATION (r
);
4324 register_specialization (r
, tmpl
, argvec
);
4327 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
4328 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
4330 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
4331 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
4338 tree r
= copy_node (t
);
4339 TREE_TYPE (r
) = type
;
4340 if (TREE_CODE (DECL_INITIAL (r
)) != TEMPLATE_PARM_INDEX
)
4341 DECL_INITIAL (r
) = TREE_TYPE (r
);
4343 DECL_INITIAL (r
) = tsubst (DECL_INITIAL (r
), args
, in_decl
);
4345 DECL_CONTEXT (r
) = NULL_TREE
;
4346 #ifdef PROMOTE_PROTOTYPES
4347 if ((TREE_CODE (type
) == INTEGER_TYPE
4348 || TREE_CODE (type
) == ENUMERAL_TYPE
)
4349 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
4350 DECL_ARG_TYPE (r
) = integer_type_node
;
4353 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, TREE_CHAIN (t
));
4359 tree r
= copy_node (t
);
4360 TREE_TYPE (r
) = type
;
4363 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, in_decl
);
4365 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, in_decl
);
4366 TREE_CHAIN (r
) = NULL_TREE
;
4372 tree r
= copy_node (t
);
4374 = tsubst_copy (DECL_INITIAL (t
), args
, in_decl
);
4375 TREE_CHAIN (r
) = NULL_TREE
;
4382 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, in_decl
);
4384 /* Do we already have this instantiation? */
4385 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4387 tree tmpl
= DECL_TI_TEMPLATE (t
);
4388 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
4390 for (; decls
; decls
= TREE_CHAIN (decls
))
4391 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
4392 return TREE_VALUE (decls
);
4396 TREE_TYPE (r
) = type
;
4397 DECL_CONTEXT (r
) = ctx
;
4398 if (TREE_STATIC (r
))
4399 DECL_ASSEMBLER_NAME (r
)
4400 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
4402 /* Don't try to expand the initializer until someone tries to use
4403 this variable; otherwise we run into circular dependencies. */
4404 DECL_INITIAL (r
) = NULL_TREE
;
4409 if (DECL_LANG_SPECIFIC (r
))
4412 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
4415 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4417 tree tmpl
= DECL_TI_TEMPLATE (t
);
4418 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
4419 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, in_decl
);
4421 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
4422 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
4423 SET_DECL_IMPLICIT_INSTANTIATION (r
);
4425 TREE_CHAIN (r
) = NULL_TREE
;
4430 if (t
== TYPE_NAME (TREE_TYPE (t
)))
4431 return TYPE_NAME (type
);
4434 tree r
= copy_node (t
);
4435 TREE_TYPE (r
) = type
;
4436 DECL_CONTEXT (r
) = current_class_type
;
4437 TREE_CHAIN (r
) = NULL_TREE
;
4443 tree purpose
, value
, chain
, result
;
4444 int via_public
, via_virtual
, via_protected
;
4446 if (t
== void_list_node
)
4449 via_public
= TREE_VIA_PUBLIC (t
);
4450 via_protected
= TREE_VIA_PROTECTED (t
);
4451 via_virtual
= TREE_VIA_VIRTUAL (t
);
4453 purpose
= TREE_PURPOSE (t
);
4455 purpose
= tsubst (purpose
, args
, in_decl
);
4456 value
= TREE_VALUE (t
);
4458 value
= tsubst (value
, args
, in_decl
);
4459 chain
= TREE_CHAIN (t
);
4460 if (chain
&& chain
!= void_type_node
)
4461 chain
= tsubst (chain
, args
, in_decl
);
4462 if (purpose
== TREE_PURPOSE (t
)
4463 && value
== TREE_VALUE (t
)
4464 && chain
== TREE_CHAIN (t
))
4466 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
4467 purpose
, value
, chain
);
4468 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
4472 if (type
!= NULL_TREE
)
4478 if (type
== TREE_TYPE (t
))
4481 TREE_TYPE (t
) = complete_type (type
);
4482 if (IS_AGGR_TYPE (type
))
4484 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
4485 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
4486 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
4487 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
4492 /* Otherwise, a vector of template arguments. */
4494 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
4495 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
4497 bzero ((char *) elts
, len
* sizeof (tree
));
4499 for (i
= 0; i
< len
; i
++)
4501 elts
[i
] = maybe_fold_nontype_arg
4502 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, in_decl
));
4504 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
4511 t
= make_tree_vec (len
);
4512 for (i
= 0; i
< len
; i
++)
4513 TREE_VEC_ELT (t
, i
) = elts
[i
];
4518 case REFERENCE_TYPE
:
4521 enum tree_code code
;
4523 if (type
== TREE_TYPE (t
))
4526 code
= TREE_CODE (t
);
4527 if (TREE_CODE (type
) == REFERENCE_TYPE
)
4529 static int last_line
= 0;
4530 static char* last_file
= 0;
4532 /* We keep track of the last time we issued this error
4533 message to avoid spewing a ton of messages during a
4534 single bad template instantiation. */
4535 if (last_line
!= lineno
||
4536 last_file
!= input_filename
)
4538 cp_error ("cannot form type %s to reference type %T during template instantiation",
4539 (code
== POINTER_TYPE
) ? "pointer" : "reference",
4542 last_file
= input_filename
;
4545 /* Use the underlying type in an attempt at error
4546 recovery; maybe the user meant vector<int> and wrote
4547 vector<int&>, or some such. */
4548 if (code
== REFERENCE_TYPE
)
4551 r
= build_pointer_type (TREE_TYPE (type
));
4553 else if (code
== POINTER_TYPE
)
4554 r
= build_pointer_type (type
);
4556 r
= build_reference_type (type
);
4557 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
4559 /* Will this ever be needed for TYPE_..._TO values? */
4564 return build_offset_type
4565 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, in_decl
), type
);
4569 tree values
= TYPE_ARG_TYPES (t
);
4570 tree context
= TYPE_CONTEXT (t
);
4571 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
4574 /* Don't bother recursing if we know it won't change anything. */
4575 if (values
!= void_list_node
)
4577 /* This should probably be rewritten to use hash_tree_cons for
4578 the memory savings. */
4579 tree first
= NULL_TREE
;
4580 tree last
= NULL_TREE
;
4582 for (; values
&& values
!= void_list_node
;
4583 values
= TREE_CHAIN (values
))
4585 tree value
= TYPE_MAIN_VARIANT (type_decays_to
4586 (tsubst (TREE_VALUE (values
), args
, in_decl
)));
4587 /* Don't instantiate default args unless they are used.
4588 Handle it in build_over_call instead. */
4589 tree purpose
= TREE_PURPOSE (values
);
4590 tree x
= build_tree_list (purpose
, value
);
4593 TREE_CHAIN (last
) = x
;
4599 if (values
== void_list_node
)
4600 TREE_CHAIN (last
) = void_list_node
;
4605 context
= tsubst (context
, args
, in_decl
);
4606 /* Could also optimize cases where return value and
4607 values have common elements (e.g., T min(const &T, const T&). */
4609 /* If the above parameters haven't changed, just return the type. */
4610 if (type
== TREE_TYPE (t
)
4611 && values
== TYPE_VALUES (t
)
4612 && context
== TYPE_CONTEXT (t
))
4615 /* Construct a new type node and return it. */
4616 if (TREE_CODE (t
) == FUNCTION_TYPE
4617 && context
== NULL_TREE
)
4619 fntype
= build_function_type (type
, values
);
4621 else if (context
== NULL_TREE
)
4623 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
4625 fntype
= build_cplus_method_type (base
, type
,
4626 TREE_CHAIN (values
));
4630 fntype
= make_node (TREE_CODE (t
));
4631 TREE_TYPE (fntype
) = type
;
4632 TYPE_CONTEXT (fntype
) = context
;
4633 TYPE_VALUES (fntype
) = values
;
4634 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
4635 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
4636 TYPE_MODE (fntype
) = TYPE_MODE (t
);
4637 if (TYPE_METHOD_BASETYPE (t
))
4638 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
4640 /* Need to generate hash value. */
4641 my_friendly_abort (84);
4643 fntype
= build_type_variant (fntype
,
4648 raises
= tsubst (raises
, args
, in_decl
);
4649 fntype
= build_exception_variant (fntype
, raises
);
4655 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, in_decl
);
4657 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
4659 r
= build_cplus_array_type (type
, domain
);
4665 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
4666 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4667 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
)));
4671 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
4672 tsubst (TREE_OPERAND (t
, 0), args
, in_decl
)));
4676 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, in_decl
);
4677 tree f
= tsubst_copy (TYPENAME_TYPE_FULLNAME (t
), args
, in_decl
);
4678 f
= make_typename_type (ctx
, f
);
4679 return cp_build_type_variant
4680 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
4681 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
4685 return make_pointer_declarator
4686 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
4689 return make_reference_declarator
4690 (type
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
));
4693 return build_parse_node
4694 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4695 tsubst_expr (TREE_OPERAND (t
, 1), args
, in_decl
));
4698 return make_call_declarator
4699 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4700 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
4701 TREE_OPERAND (t
, 2),
4702 tsubst (TREE_TYPE (t
), args
, in_decl
));
4705 return build_parse_node
4706 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
4707 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
));
4710 sorry ("use of `%s' in template",
4711 tree_code_name
[(int) TREE_CODE (t
)]);
4712 return error_mark_node
;
4719 emit_line_note (input_filename
, lineno
);
4723 expand_start_bindings (0);
4730 int saved_warn_unused
= 0;
4732 if (processing_template_decl
)
4734 saved_warn_unused
= warn_unused
;
4737 expand_end_bindings (getdecls (), kept_level_p (), 0);
4738 if (processing_template_decl
)
4739 warn_unused
= saved_warn_unused
;
4740 t
= poplevel (kept_level_p (), 1, 0);
4745 /* Like tsubst, but deals with expressions. This function just replaces
4746 template parms; to finish processing the resultant expression, use
4750 tsubst_copy (t
, args
, in_decl
)
4754 enum tree_code code
;
4756 if (t
== NULL_TREE
|| t
== error_mark_node
)
4759 code
= TREE_CODE (t
);
4764 return do_identifier (DECL_NAME (t
), 0);
4768 if (DECL_CONTEXT (t
))
4771 if (TREE_CODE (DECL_CONTEXT (t
)) == FUNCTION_DECL
)
4772 return lookup_name (DECL_NAME (t
), 0);
4774 ctx
= tsubst (DECL_CONTEXT (t
), args
, in_decl
);
4775 if (ctx
!= DECL_CONTEXT (t
))
4776 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
4782 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
4783 t
= tsubst (t
, args
, in_decl
);
4788 if (is_member_template (t
))
4789 return tsubst (t
, args
, in_decl
);
4794 case IDENTIFIER_NODE
:
4795 return do_identifier (t
, 0);
4799 case REINTERPRET_CAST_EXPR
:
4800 case CONST_CAST_EXPR
:
4801 case STATIC_CAST_EXPR
:
4802 case DYNAMIC_CAST_EXPR
:
4804 (code
, tsubst (TREE_TYPE (t
), args
, in_decl
),
4805 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
4808 case PREDECREMENT_EXPR
:
4809 case PREINCREMENT_EXPR
:
4810 case POSTDECREMENT_EXPR
:
4811 case POSTINCREMENT_EXPR
:
4813 case TRUTH_NOT_EXPR
:
4816 case CONVERT_EXPR
: /* Unary + */
4824 tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
));
4829 case TRUNC_DIV_EXPR
:
4831 case FLOOR_DIV_EXPR
:
4832 case ROUND_DIV_EXPR
:
4833 case EXACT_DIV_EXPR
:
4835 case BIT_ANDTC_EXPR
:
4838 case TRUNC_MOD_EXPR
:
4839 case FLOOR_MOD_EXPR
:
4840 case TRUTH_ANDIF_EXPR
:
4841 case TRUTH_ORIF_EXPR
:
4842 case TRUTH_AND_EXPR
:
4863 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4864 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
4868 tree fn
= TREE_OPERAND (t
, 0);
4869 if (is_overloaded_fn (fn
))
4870 fn
= tsubst_copy (get_first_fn (fn
), args
, in_decl
);
4872 /* Sometimes FN is a LOOKUP_EXPR. */
4873 fn
= tsubst_copy (fn
, args
, in_decl
);
4875 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4879 case METHOD_CALL_EXPR
:
4881 tree name
= TREE_OPERAND (t
, 0);
4882 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
4884 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4885 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4886 name
= TYPE_MAIN_VARIANT (name
);
4887 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
4889 else if (TREE_CODE (name
) == SCOPE_REF
4890 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
4892 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4893 name
= TREE_OPERAND (name
, 1);
4894 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, in_decl
);
4895 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
4896 name
= TYPE_MAIN_VARIANT (name
);
4897 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
4898 name
= build_nt (SCOPE_REF
, base
, name
);
4901 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
);
4903 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4904 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
),
4913 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4914 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4915 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
4917 if (code
== BIND_EXPR
&& !processing_template_decl
)
4919 /* This processing should really occur in tsubst_expr,
4920 However, tsubst_expr does not recurse into expressions,
4921 since it assumes that there aren't any statements
4922 inside them. Instead, it simply calls
4923 build_expr_from_tree. So, we need to expand the
4925 tree rtl_expr
= begin_stmt_expr ();
4926 tree block
= tsubst_expr (TREE_OPERAND (r
, 1), args
, in_decl
);
4927 r
= finish_stmt_expr (rtl_expr
, block
);
4936 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4937 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
),
4938 tsubst_copy (TREE_OPERAND (t
, 2), args
, in_decl
));
4939 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
4946 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
),
4947 tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
));
4948 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
4949 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
4953 case TEMPLATE_ID_EXPR
:
4955 /* Substituted template arguments */
4956 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, in_decl
);
4958 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
4959 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
4961 return lookup_template_function
4962 (tsubst_copy (TREE_OPERAND (t
, 0), args
, in_decl
), targs
);
4967 tree purpose
, value
, chain
;
4969 if (t
== void_list_node
)
4972 purpose
= TREE_PURPOSE (t
);
4974 purpose
= tsubst_copy (purpose
, args
, in_decl
);
4975 value
= TREE_VALUE (t
);
4977 value
= tsubst_copy (value
, args
, in_decl
);
4978 chain
= TREE_CHAIN (t
);
4979 if (chain
&& chain
!= void_type_node
)
4980 chain
= tsubst_copy (chain
, args
, in_decl
);
4981 if (purpose
== TREE_PURPOSE (t
)
4982 && value
== TREE_VALUE (t
)
4983 && chain
== TREE_CHAIN (t
))
4985 return tree_cons (purpose
, value
, chain
);
4992 case TEMPLATE_TYPE_PARM
:
4993 case TEMPLATE_TEMPLATE_PARM
:
4994 case TEMPLATE_PARM_INDEX
:
4996 case REFERENCE_TYPE
:
5003 return tsubst (t
, args
, in_decl
);
5005 case IDENTIFIER_NODE
:
5006 if (IDENTIFIER_TYPENAME_P (t
))
5007 return build_typename_overload
5008 (tsubst (TREE_TYPE (t
), args
, in_decl
));
5014 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, in_decl
), NULL_TREE
,
5015 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, in_decl
));
5022 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
5025 tsubst_expr (t
, args
, in_decl
)
5029 if (t
== NULL_TREE
|| t
== error_mark_node
)
5032 if (processing_template_decl
)
5033 return tsubst_copy (t
, args
, in_decl
);
5035 switch (TREE_CODE (t
))
5038 lineno
= TREE_COMPLEXITY (t
);
5039 finish_return_stmt (tsubst_expr (RETURN_EXPR (t
),
5044 lineno
= TREE_COMPLEXITY (t
);
5045 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t
),
5051 int i
= suspend_momentary ();
5054 lineno
= TREE_COMPLEXITY (t
);
5055 emit_line_note (input_filename
, lineno
);
5057 (tsubst (TREE_OPERAND (t
, 0), args
, in_decl
),
5058 tsubst (TREE_OPERAND (t
, 1), args
, in_decl
),
5059 TREE_OPERAND (t
, 2) != 0, NULL_TREE
, NULL_TREE
);
5060 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, in_decl
);
5062 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5063 resume_momentary (i
);
5070 lineno
= TREE_COMPLEXITY (t
);
5073 for (tmp
= FOR_INIT_STMT (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
5074 tsubst_expr (tmp
, args
, in_decl
);
5075 finish_for_init_stmt (NULL_TREE
);
5076 finish_for_cond (tsubst_expr (FOR_COND (t
), args
,
5079 tmp
= tsubst_expr (FOR_EXPR (t
), args
, in_decl
);
5080 finish_for_expr (tmp
, NULL_TREE
);
5081 tsubst_expr (FOR_BODY (t
), args
, in_decl
);
5082 finish_for_stmt (tmp
, NULL_TREE
);
5088 lineno
= TREE_COMPLEXITY (t
);
5089 begin_while_stmt ();
5090 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t
),
5093 tsubst_expr (WHILE_BODY (t
), args
, in_decl
);
5094 finish_while_stmt (NULL_TREE
);
5100 lineno
= TREE_COMPLEXITY (t
);
5102 tsubst_expr (DO_BODY (t
), args
, in_decl
);
5103 finish_do_body (NULL_TREE
);
5104 finish_do_stmt (tsubst_expr (DO_COND (t
), args
,
5114 lineno
= TREE_COMPLEXITY (t
);
5116 finish_if_stmt_cond (tsubst_expr (IF_COND (t
),
5120 if (tmp
= THEN_CLAUSE (t
), tmp
)
5122 tsubst_expr (tmp
, args
, in_decl
);
5123 finish_then_clause (NULL_TREE
);
5126 if (tmp
= ELSE_CLAUSE (t
), tmp
)
5128 begin_else_clause ();
5129 tsubst_expr (tmp
, args
, in_decl
);
5130 finish_else_clause (NULL_TREE
);
5141 lineno
= TREE_COMPLEXITY (t
);
5142 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
));
5143 for (substmt
= COMPOUND_BODY (t
);
5144 substmt
!= NULL_TREE
;
5145 substmt
= TREE_CHAIN (substmt
))
5146 tsubst_expr (substmt
, args
, in_decl
);
5147 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t
),
5153 lineno
= TREE_COMPLEXITY (t
);
5154 finish_break_stmt ();
5158 lineno
= TREE_COMPLEXITY (t
);
5159 finish_continue_stmt ();
5166 lineno
= TREE_COMPLEXITY (t
);
5167 begin_switch_stmt ();
5168 val
= tsubst_expr (SWITCH_COND (t
), args
, in_decl
);
5169 finish_switch_cond (val
);
5171 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
5172 tsubst_expr (tmp
, args
, in_decl
);
5174 finish_switch_stmt (val
, NULL_TREE
);
5179 finish_case_label (tsubst_expr (CASE_LOW (t
), args
, in_decl
),
5180 tsubst_expr (CASE_HIGH (t
), args
, in_decl
));
5184 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
5191 lineno
= TREE_COMPLEXITY (t
);
5192 t
= GOTO_DESTINATION (t
);
5193 if (TREE_CODE (t
) != IDENTIFIER_NODE
)
5194 /* Computed goto's must be tsubst'd into. On the other hand,
5195 non-computed gotos must not be; the identifier in question
5196 will have no binding. */
5197 t
= tsubst_expr (t
, args
, in_decl
);
5198 finish_goto_stmt (t
);
5202 lineno
= TREE_COMPLEXITY (t
);
5203 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t
), args
, in_decl
),
5204 tsubst_expr (ASM_STRING (t
), args
, in_decl
),
5205 tsubst_expr (ASM_OUTPUTS (t
), args
, in_decl
),
5206 tsubst_expr (ASM_INPUTS (t
), args
, in_decl
),
5207 tsubst_expr (ASM_CLOBBERS (t
), args
, in_decl
));
5211 lineno
= TREE_COMPLEXITY (t
);
5213 tsubst_expr (TRY_STMTS (t
), args
, in_decl
);
5214 finish_try_block (NULL_TREE
);
5216 tree handler
= TRY_HANDLERS (t
);
5217 for (; handler
; handler
= TREE_CHAIN (handler
))
5218 tsubst_expr (handler
, args
, in_decl
);
5220 finish_handler_sequence (NULL_TREE
);
5224 lineno
= TREE_COMPLEXITY (t
);
5226 if (HANDLER_PARMS (t
))
5228 tree d
= HANDLER_PARMS (t
);
5229 expand_start_catch_block
5230 (tsubst (TREE_OPERAND (d
, 1), args
, in_decl
),
5231 tsubst (TREE_OPERAND (d
, 0), args
, in_decl
));
5234 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
5235 finish_handler_parms (NULL_TREE
);
5236 tsubst_expr (HANDLER_BODY (t
), args
, in_decl
);
5237 finish_handler (NULL_TREE
);
5241 lineno
= TREE_COMPLEXITY (t
);
5243 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
5244 tsubst_enum (t
, args
, NULL
);
5248 return build_expr_from_tree (tsubst_copy (t
, args
, in_decl
));
5254 instantiate_template (tmpl
, targ_ptr
)
5255 tree tmpl
, targ_ptr
;
5259 struct obstack
*old_fmp_obstack
;
5260 extern struct obstack
*function_maybepermanent_obstack
;
5262 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
5264 /* FIXME this won't work with member templates; we only have one level
5266 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
5268 /* Check to see if we already have this specialization. */
5269 tree spec
= retrieve_specialization (tmpl
, targ_ptr
);
5271 if (spec
!= NULL_TREE
)
5275 push_obstacks (&permanent_obstack
, &permanent_obstack
);
5276 old_fmp_obstack
= function_maybepermanent_obstack
;
5277 function_maybepermanent_obstack
= &permanent_obstack
;
5279 len
= DECL_NTPARMS (tmpl
);
5284 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
5285 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
5287 tree nt
= target_type (t
);
5288 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
5290 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
5291 cp_error (" trying to instantiate `%D'", tmpl
);
5292 fndecl
= error_mark_node
;
5296 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
5298 targ_ptr
= copy_to_permanent (targ_ptr
);
5300 /* substitute template parameters */
5301 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, tmpl
);
5303 if (flag_external_templates
)
5304 add_pending_template (fndecl
);
5307 function_maybepermanent_obstack
= old_fmp_obstack
;
5313 /* Push the name of the class template into the scope of the instantiation. */
5316 overload_template_name (type
)
5319 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
5322 if (IDENTIFIER_CLASS_VALUE (id
)
5323 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
5326 decl
= build_decl (TYPE_DECL
, id
, type
);
5327 SET_DECL_ARTIFICIAL (decl
);
5328 pushdecl_class_level (decl
);
5332 /* Like type_unification but designed specially to handle conversion
5333 operators. The EXTRA_FN_ARG, if any, is the type of an additional
5334 parameter to be added to the beginning of FN's parameter list. */
5337 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
,
5338 strict
, extra_fn_arg
)
5339 tree fn
, explicit_targs
, targs
, args
, return_type
;
5344 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
5345 tree decl_arg_types
= args
;
5347 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
5349 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
5351 /* This is a template conversion operator. Use the return types
5352 as well as the argument types. */
5353 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
5354 TREE_TYPE (TREE_TYPE (fn
)),
5356 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
5361 if (extra_fn_arg
!= NULL_TREE
)
5362 fn_arg_types
= scratch_tree_cons (NULL_TREE
, extra_fn_arg
,
5365 /* We allow incomplete unification without an error message here
5366 because the standard doesn't seem to explicitly prohibit it. Our
5367 callers must be ready to deal with unification failures in any
5369 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
5380 /* Type unification.
5382 We have a function template signature with one or more references to
5383 template parameters, and a parameter list we wish to fit to this
5384 template. If possible, produce a list of parameters for the template
5385 which will cause it to fit the supplied parameter list.
5387 Return zero for success, 2 for an incomplete match that doesn't resolve
5388 all the types, and 1 for complete failure. An error message will be
5389 printed only for an incomplete match.
5391 TPARMS[NTPARMS] is an array of template parameter types;
5392 TARGS[NTPARMS] is the array of template parameter values. PARMS is
5393 the function template's signature (using TEMPLATE_PARM_IDX nodes),
5394 and ARGS is the argument list we're trying to match against it.
5396 If SUBR is 1, we're being called recursively (to unify the arguments of
5397 a function or method parameter of a function template), so don't zero
5398 out targs and don't fail on an incomplete match.
5400 If STRICT is 1, the match must be exact (for casts of overloaded
5401 addresses, explicit instantiation, and more_specialized). */
5404 type_unification (tparms
, targs
, parms
, args
, targs_in
,
5405 strict
, allow_incomplete
)
5406 tree tparms
, targs
, parms
, args
, targs_in
;
5407 int strict
, allow_incomplete
;
5409 int ntparms
= TREE_VEC_LENGTH (tparms
);
5415 for (i
= 0; i
< ntparms
; i
++)
5416 TREE_VEC_ELT (targs
, i
) = NULL_TREE
;
5418 if (targs_in
!= NULL_TREE
)
5421 arg_vec
= coerce_template_parms (tparms
, targs_in
, NULL_TREE
, 0,
5424 if (arg_vec
== error_mark_node
)
5427 explicit_mask
= alloca (sizeof (int) * TREE_VEC_LENGTH (targs
));
5428 bzero (explicit_mask
, sizeof(int) * TREE_VEC_LENGTH (targs
));
5431 i
< TREE_VEC_LENGTH (arg_vec
)
5432 && TREE_VEC_ELT (arg_vec
, i
) != NULL_TREE
;
5435 TREE_VEC_ELT (targs
, i
) = TREE_VEC_ELT (arg_vec
, i
);
5436 /* Let unify know that this argument was explicit. */
5437 explicit_mask
[i
] = 1;
5443 r
= type_unification_real (tparms
, targs
, parms
, args
, 0,
5444 strict
, allow_incomplete
, explicit_mask
);
5449 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
5450 integers, with ones in positions corresponding to arguments in
5451 targs that were provided explicitly, and zeros elsewhere. */
5454 type_unification_real (tparms
, targs
, parms
, args
, subr
,
5455 strict
, allow_incomplete
, explicit_mask
)
5456 tree tparms
, targs
, parms
, args
;
5457 int subr
, strict
, allow_incomplete
;
5462 int ntparms
= TREE_VEC_LENGTH (tparms
);
5464 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
5465 my_friendly_assert (parms
== NULL_TREE
5466 || TREE_CODE (parms
) == TREE_LIST
, 290);
5467 /* ARGS could be NULL (via a call from parse.y to
5468 build_x_function_call). */
5470 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
5471 my_friendly_assert (ntparms
> 0, 292);
5474 && parms
!= void_list_node
5476 && args
!= void_list_node
)
5478 parm
= TREE_VALUE (parms
);
5479 parms
= TREE_CHAIN (parms
);
5480 arg
= TREE_VALUE (args
);
5481 args
= TREE_CHAIN (args
);
5483 if (arg
== error_mark_node
)
5485 if (arg
== unknown_type_node
)
5488 /* Conversions will be performed on a function argument that
5489 corresponds with a function parameter that contains only
5490 non-deducible template parameters and explicitly specified
5491 template parameters. */
5492 if (! uses_template_parms (parm
))
5496 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5497 type
= TREE_TYPE (arg
);
5506 if (comptypes (parm
, type
, 1))
5510 /* It might work; we shouldn't check now, because we might
5511 get into infinite recursion. Overload resolution will
5519 if (TREE_CODE (arg
) == VAR_DECL
)
5520 arg
= TREE_TYPE (arg
);
5521 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
5522 arg
= TREE_TYPE (arg
);
5524 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
5526 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
5527 if (TREE_CODE (arg
) == TREE_LIST
5528 && TREE_TYPE (arg
) == unknown_type_node
5529 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
5534 /* Have to back unify here */
5535 arg
= TREE_VALUE (arg
);
5536 ntparms
= DECL_NTPARMS (arg
);
5537 targs
= make_scratch_vec (ntparms
);
5538 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
5540 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
5542 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
5543 parm
, NULL_TREE
, strict
,
5546 arg
= TREE_TYPE (arg
);
5549 if (! flag_ansi
&& arg
== TREE_TYPE (null_node
))
5551 warning ("using type void* for NULL");
5552 arg
= ptr_type_node
;
5555 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
5556 arg
= TREE_TYPE (arg
);
5558 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
5560 if (TREE_CODE (arg
) == FUNCTION_TYPE
5561 || TREE_CODE (arg
) == METHOD_TYPE
)
5562 arg
= build_pointer_type (arg
);
5563 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
5564 arg
= build_pointer_type (TREE_TYPE (arg
));
5566 arg
= TYPE_MAIN_VARIANT (arg
);
5569 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, strict
,
5578 /* Fail if we've reached the end of the parm list, and more args
5579 are present, and the parm list isn't variadic. */
5580 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
5582 /* Fail if parms are left and they don't have default values. */
5584 && parms
!= void_list_node
5585 && TREE_PURPOSE (parms
) == NULL_TREE
)
5588 for (i
= 0; i
< ntparms
; i
++)
5589 if (TREE_VEC_ELT (targs
, i
) == NULL_TREE
)
5591 if (!allow_incomplete
)
5592 error ("incomplete type unification");
5598 /* Returns the level of DECL, which declares a template parameter. */
5601 template_decl_level (decl
)
5604 switch (TREE_CODE (decl
))
5608 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl
));
5611 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl
));
5614 my_friendly_abort (0);
5619 /* Tail recursion is your friend. */
5622 unify (tparms
, targs
, ntparms
, parm
, arg
, strict
, explicit_mask
)
5623 tree tparms
, targs
, parm
, arg
;
5624 int ntparms
, strict
;
5631 /* I don't think this will do the right thing with respect to types.
5632 But the only case I've seen it in so far has been array bounds, where
5633 signedness is the only information lost, and I think that will be
5635 while (TREE_CODE (parm
) == NOP_EXPR
)
5636 parm
= TREE_OPERAND (parm
, 0);
5638 if (arg
== error_mark_node
)
5640 if (arg
== unknown_type_node
)
5642 /* If PARM uses template parameters, then we can't bail out here,
5643 even in ARG == PARM, since we won't record unifications for the
5644 template parameters. We might need them if we're trying to
5645 figure out which of two things is more specialized. */
5646 if (arg
== parm
&& !uses_template_parms (parm
))
5649 /* We can't remove cv-quals when strict. */
5650 if (strict
&& TREE_CODE (arg
) == TREE_CODE (parm
)
5651 && TREE_CODE_CLASS (TREE_CODE (arg
)) == 't'
5652 && (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
5653 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
5656 switch (TREE_CODE (parm
))
5659 /* In a type which contains a nested-name-specifier, template
5660 argument values cannot be deduced for template parameters used
5661 within the nested-name-specifier. */
5664 case TEMPLATE_TYPE_PARM
:
5665 case TEMPLATE_TEMPLATE_PARM
:
5666 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
5668 if (TEMPLATE_TYPE_LEVEL (parm
)
5669 != template_decl_level (tparm
))
5670 /* The PARM is not one we're trying to unify. Just check
5671 to see if it matches ARG. */
5672 return (TREE_CODE (arg
) == TREE_CODE (parm
)
5673 && comptypes (parm
, arg
, 1)) ? 0 : 1;
5674 idx
= TEMPLATE_TYPE_IDX (parm
);
5675 targ
= TREE_VEC_ELT (targs
, idx
);
5676 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, idx
));
5678 /* Check for mixed types and values. */
5679 if ((TREE_CODE (parm
) == TEMPLATE_TYPE_PARM
5680 && TREE_CODE (tparm
) != TYPE_DECL
)
5681 || (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
5682 && TREE_CODE (tparm
) != TEMPLATE_DECL
))
5685 if (!strict
&& targ
!= NULL_TREE
5686 && explicit_mask
&& explicit_mask
[idx
])
5687 /* An explicit template argument. Don't even try to match
5688 here; the overload resolution code will manage check to
5689 see whether the call is legal. */
5692 if (TREE_CODE (parm
) == TEMPLATE_TEMPLATE_PARM
)
5694 if (CLASSTYPE_TEMPLATE_INFO (parm
))
5696 /* We arrive here when PARM does not involve template
5699 /* ARG must be constructed from a template class. */
5700 if (TREE_CODE (arg
) != RECORD_TYPE
|| !CLASSTYPE_TEMPLATE_INFO (arg
))
5704 tree parmtmpl
= CLASSTYPE_TI_TEMPLATE (parm
);
5705 tree parmvec
= CLASSTYPE_TI_ARGS (parm
);
5706 tree argvec
= CLASSTYPE_TI_ARGS (arg
);
5708 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg
));
5711 /* The parameter and argument roles have to be switched here
5712 in order to handle default arguments properly. For example,
5713 template<template <class> class TT> void f(TT<int>)
5714 should be able to accept vector<int> which comes from
5715 template <class T, class Allcator = allocator>
5718 if (coerce_template_parms (argtmplvec
, parmvec
, parmtmpl
, 1, 1, 0)
5722 /* Deduce arguments T, i from TT<T> or TT<i>. */
5723 for (i
= 0; i
< TREE_VEC_LENGTH (parmvec
); ++i
)
5725 tree t
= TREE_VEC_ELT (parmvec
, i
);
5726 if (TREE_CODE (t
) != TEMPLATE_TYPE_PARM
5727 && TREE_CODE (t
) != TEMPLATE_TEMPLATE_PARM
5728 && TREE_CODE (t
) != TEMPLATE_PARM_INDEX
)
5731 /* This argument can be deduced. */
5733 if (unify (tparms
, targs
, ntparms
, t
,
5734 TREE_VEC_ELT (argvec
, i
), strict
, explicit_mask
))
5738 arg
= CLASSTYPE_TI_TEMPLATE (arg
);
5743 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
5744 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
5748 /* Template type parameters cannot contain cv-quals; i.e.
5749 template <class T> void f (T& a, T& b) will not generate
5750 void f (const int& a, const int& b). */
5751 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
5752 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
5754 arg
= TYPE_MAIN_VARIANT (arg
);
5757 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
5758 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
5759 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
5764 /* Simple cases: Value already set, does match or doesn't. */
5765 if (targ
!= NULL_TREE
5766 && (comptypes (targ
, arg
, 1)
5767 || (explicit_mask
&& explicit_mask
[idx
])))
5771 TREE_VEC_ELT (targs
, idx
) = arg
;
5774 case TEMPLATE_PARM_INDEX
:
5775 tparm
= TREE_VALUE (TREE_VEC_ELT (tparms
, 0));
5777 if (TEMPLATE_PARM_LEVEL (parm
)
5778 != template_decl_level (tparm
))
5779 /* The PARM is not one we're trying to unify. Just check
5780 to see if it matches ARG. */
5781 return (TREE_CODE (arg
) == TREE_CODE (parm
)
5782 && cp_tree_equal (parm
, arg
)) ? 0 : 1;
5784 idx
= TEMPLATE_PARM_IDX (parm
);
5785 targ
= TREE_VEC_ELT (targs
, idx
);
5789 int i
= cp_tree_equal (targ
, arg
);
5795 my_friendly_abort (42);
5798 TREE_VEC_ELT (targs
, idx
) = copy_to_permanent (arg
);
5802 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
5803 return unify (tparms
, targs
, ntparms
, parm
,
5804 TYPE_PTRMEMFUNC_FN_TYPE (arg
), strict
, explicit_mask
);
5806 if (TREE_CODE (arg
) != POINTER_TYPE
)
5808 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5809 strict
, explicit_mask
);
5811 case REFERENCE_TYPE
:
5812 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
5813 arg
= TREE_TYPE (arg
);
5814 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
5815 strict
, explicit_mask
);
5818 if (TREE_CODE (arg
) != ARRAY_TYPE
)
5820 if ((TYPE_DOMAIN (parm
) == NULL_TREE
)
5821 != (TYPE_DOMAIN (arg
) == NULL_TREE
))
5823 if (TYPE_DOMAIN (parm
) != NULL_TREE
5824 && unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
),
5825 TYPE_DOMAIN (arg
), strict
, explicit_mask
) != 0)
5827 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
5828 strict
, explicit_mask
);
5835 if (TREE_CODE (arg
) != TREE_CODE (parm
))
5838 if (TREE_CODE (parm
) == INTEGER_TYPE
)
5840 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
5841 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
5842 TYPE_MIN_VALUE (arg
), strict
, explicit_mask
))
5844 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
5845 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
5846 TYPE_MAX_VALUE (arg
), strict
, explicit_mask
))
5849 else if (TREE_CODE (parm
) == REAL_TYPE
5850 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
5853 /* As far as unification is concerned, this wins. Later checks
5854 will invalidate it if necessary. */
5857 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5858 /* Type INTEGER_CST can come from ordinary constant template args. */
5860 while (TREE_CODE (arg
) == NOP_EXPR
)
5861 arg
= TREE_OPERAND (arg
, 0);
5863 if (TREE_CODE (arg
) != INTEGER_CST
)
5865 return !tree_int_cst_equal (parm
, arg
);
5870 if (TREE_CODE (arg
) != TREE_VEC
)
5872 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
5874 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
5875 if (unify (tparms
, targs
, ntparms
,
5876 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
5877 strict
, explicit_mask
))
5883 if (TYPE_PTRMEMFUNC_FLAG (parm
))
5884 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
5885 arg
, strict
, explicit_mask
);
5887 /* Allow trivial conversions. */
5888 if (TREE_CODE (arg
) != RECORD_TYPE
5889 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
5890 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
5893 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
5897 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
5899 (CLASSTYPE_TEMPLATE_INFO (arg
)
5900 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
5902 if (! t
|| t
== error_mark_node
)
5905 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
5906 CLASSTYPE_TI_ARGS (t
), strict
, explicit_mask
);
5908 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
5913 if (TREE_CODE (arg
) != METHOD_TYPE
)
5918 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
5921 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5922 TREE_TYPE (arg
), strict
, explicit_mask
))
5924 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
5925 TYPE_ARG_TYPES (arg
), 1,
5926 strict
, 0, explicit_mask
);
5929 if (TREE_CODE (arg
) != OFFSET_TYPE
)
5931 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
5932 TYPE_OFFSET_BASETYPE (arg
), strict
, explicit_mask
))
5934 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
5935 TREE_TYPE (arg
), strict
, explicit_mask
);
5938 if (arg
!= decl_constant_value (parm
))
5943 /* Matched cases are handled by the ARG == PARM test above. */
5947 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm
))))
5949 /* We're looking at an expression. This can happen with
5953 void foo(S<I>, S<I + 2>);
5955 If the call looked like:
5957 foo(S<2>(), S<4>());
5959 we would have already matched `I' with `2'. Now, we'd
5960 like to know if `4' matches `I + 2'. So, we substitute
5961 into that expression, and fold constants, in the hope of
5964 maybe_fold_nontype_arg (tsubst_expr (parm
, targs
, NULL_TREE
));
5965 enum tree_code tc
= TREE_CODE (t
);
5967 if (tc
== MINUS_EXPR
5968 && TREE_CODE (TREE_OPERAND (t
, 0)) == TEMPLATE_PARM_INDEX
5969 && TREE_CODE (TREE_OPERAND (t
, 1)) == INTEGER_CST
)
5971 /* We handle this case specially, since it comes up with
5972 arrays. In particular, something like:
5974 template <int N> void f(int (&x)[N]);
5976 Here, we are trying to unify the range type, which
5977 looks like [0 ... (N - 1)]. */
5979 t1
= TREE_OPERAND (parm
, 0);
5980 t2
= TREE_OPERAND (parm
, 1);
5982 t
= maybe_fold_nontype_arg (build (PLUS_EXPR
,
5986 return unify (tparms
, targs
, ntparms
, t1
, t
,
5987 strict
, explicit_mask
);
5990 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (tc
)))
5991 /* Good, we mangaged to simplify the exression. */
5992 return unify (tparms
, targs
, ntparms
, t
, arg
, strict
,
5995 /* Bad, we couldn't simplify this. Assume it doesn't
6000 sorry ("use of `%s' in template type unification",
6001 tree_code_name
[(int) TREE_CODE (parm
)]);
6008 mark_decl_instantiated (result
, extern_p
)
6012 if (DECL_TEMPLATE_INSTANTIATION (result
))
6013 SET_DECL_EXPLICIT_INSTANTIATION (result
);
6015 if (TREE_CODE (result
) != FUNCTION_DECL
)
6016 /* The TREE_PUBLIC flag for function declarations will have been
6017 set correctly by tsubst. */
6018 TREE_PUBLIC (result
) = 1;
6022 DECL_INTERFACE_KNOWN (result
) = 1;
6023 DECL_NOT_REALLY_EXTERN (result
) = 1;
6025 /* For WIN32 we also want to put explicit instantiations in
6026 linkonce sections. */
6027 if (supports_one_only () && ! SUPPORTS_WEAK
&& TREE_PUBLIC (result
))
6028 make_decl_one_only (result
);
6030 else if (TREE_CODE (result
) == FUNCTION_DECL
)
6031 mark_inline_for_output (result
);
6034 /* Given two function templates PAT1 and PAT2, and explicit template
6035 arguments EXPLICIT_ARGS return:
6037 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
6038 -1 if PAT2 is more specialized than PAT1.
6039 0 if neither is more specialized. */
6042 more_specialized (pat1
, pat2
, explicit_args
)
6043 tree pat1
, pat2
, explicit_args
;
6048 targs
= get_bindings_overload (pat1
, pat2
, explicit_args
);
6054 targs
= get_bindings_overload (pat2
, pat1
, explicit_args
);
6063 /* Given two class template specialization list nodes PAT1 and PAT2, return:
6065 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
6066 -1 if PAT2 is more specialized than PAT1.
6067 0 if neither is more specialized. */
6070 more_specialized_class (pat1
, pat2
)
6076 targs
= get_class_bindings
6077 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
),
6078 TREE_PURPOSE (pat2
), NULL_TREE
);
6082 targs
= get_class_bindings
6083 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
),
6084 TREE_PURPOSE (pat1
), NULL_TREE
);
6091 /* Return the template arguments that will produce the function signature
6092 DECL from the function template FN, with the explicit template
6093 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
6097 get_bindings_real (fn
, decl
, explicit_args
, check_rettype
)
6098 tree fn
, decl
, explicit_args
;
6101 int ntparms
= DECL_NTPARMS (fn
);
6102 tree targs
= make_scratch_vec (ntparms
);
6103 tree decl_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
6104 tree extra_fn_arg
= NULL_TREE
;
6107 if (DECL_STATIC_FUNCTION_P (fn
)
6108 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl
))
6110 /* Sometimes we are trying to figure out what's being
6111 specialized by a declaration that looks like a method, and it
6112 turns out to be a static member function. */
6113 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn
))
6114 && !is_member_template (fn
))
6115 /* The natural thing to do here seems to be to remove the
6116 spurious `this' parameter from the DECL, but that prevents
6117 unification from making use of the class type. So,
6118 instead, we have fn_type_unification add to the parameters
6120 extra_fn_arg
= build_pointer_type (DECL_REAL_CONTEXT (fn
));
6122 /* In this case, though, adding the extra_fn_arg can confuse
6123 things, so we remove from decl_arg_types instead. */
6124 decl_arg_types
= TREE_CHAIN (decl_arg_types
);
6127 i
= fn_type_unification (fn
, explicit_args
, targs
,
6129 TREE_TYPE (TREE_TYPE (decl
)),
6138 /* Check to see that the resulting return type is also OK. */
6139 tree t
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
6140 complete_template_args (fn
, targs
, 1),
6143 if (!comptypes (t
, TREE_TYPE (TREE_TYPE (decl
)), 1))
6150 /* For most uses, we want to check the return type. */
6153 get_bindings (fn
, decl
, explicit_args
)
6154 tree fn
, decl
, explicit_args
;
6156 return get_bindings_real (fn
, decl
, explicit_args
, 1);
6159 /* But for more_specialized, we only care about the parameter types. */
6162 get_bindings_overload (fn
, decl
, explicit_args
)
6163 tree fn
, decl
, explicit_args
;
6165 return get_bindings_real (fn
, decl
, explicit_args
, 0);
6169 get_class_bindings (tparms
, parms
, args
, outer_args
)
6170 tree tparms
, parms
, args
, outer_args
;
6172 int i
, ntparms
= TREE_VEC_LENGTH (tparms
);
6173 tree vec
= make_temp_vec (ntparms
);
6177 tparms
= tsubst (tparms
, outer_args
, NULL_TREE
);
6178 parms
= tsubst (parms
, outer_args
, NULL_TREE
);
6181 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
6183 switch (unify (tparms
, vec
, ntparms
,
6184 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
6194 for (i
= 0; i
< ntparms
; ++i
)
6195 if (! TREE_VEC_ELT (vec
, i
))
6201 /* Return the most specialized of the list of templates in FNS that can
6202 produce an instantiation matching DECL, given the explicit template
6203 arguments EXPLICIT_ARGS. */
6206 most_specialized (fns
, decl
, explicit_args
)
6207 tree fns
, decl
, explicit_args
;
6209 tree fn
, champ
, args
, *p
;
6212 for (p
= &fns
; *p
; )
6214 args
= get_bindings (TREE_VALUE (*p
), decl
, explicit_args
);
6217 p
= &TREE_CHAIN (*p
);
6220 *p
= TREE_CHAIN (*p
);
6227 champ
= TREE_VALUE (fn
);
6228 fn
= TREE_CHAIN (fn
);
6229 for (; fn
; fn
= TREE_CHAIN (fn
))
6231 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
6238 fn
= TREE_CHAIN (fn
);
6240 return error_mark_node
;
6242 champ
= TREE_VALUE (fn
);
6246 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
6248 fate
= more_specialized (champ
, TREE_VALUE (fn
), explicit_args
);
6250 return error_mark_node
;
6256 /* Return the most specialized of the class template specializations in
6257 SPECS that can produce an instantiation matching ARGS. */
6260 most_specialized_class (specs
, mainargs
, outer_args
)
6261 tree specs
, mainargs
, outer_args
;
6263 tree list
= NULL_TREE
, t
, args
, champ
;
6266 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
6268 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
),
6269 mainargs
, outer_args
);
6272 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
6273 TREE_TYPE (list
) = TREE_TYPE (t
);
6283 for (; t
; t
= TREE_CHAIN (t
))
6285 fate
= more_specialized_class (champ
, t
);
6294 return error_mark_node
;
6300 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
6302 fate
= more_specialized_class (champ
, t
);
6304 return error_mark_node
;
6310 /* called from the parser. */
6313 do_decl_instantiation (declspecs
, declarator
, storage
)
6314 tree declspecs
, declarator
, storage
;
6316 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
6317 tree result
= NULL_TREE
;
6320 if (! DECL_LANG_SPECIFIC (decl
))
6322 cp_error ("explicit instantiation of non-template `%#D'", decl
);
6326 /* If we've already seen this template instance, use it. */
6327 if (TREE_CODE (decl
) == VAR_DECL
)
6329 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
6330 if (result
&& TREE_CODE (result
) != VAR_DECL
)
6333 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
6335 cp_error ("explicit instantiation of `%#D'", decl
);
6338 else if (DECL_TEMPLATE_INSTANTIATION (decl
))
6343 cp_error ("no matching template for `%D' found", decl
);
6347 if (! DECL_TEMPLATE_INFO (result
))
6349 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
6353 if (flag_external_templates
)
6356 if (storage
== NULL_TREE
)
6358 else if (storage
== ridpointers
[(int) RID_EXTERN
])
6361 cp_error ("storage class `%D' applied to template instantiation",
6364 mark_decl_instantiated (result
, extern_p
);
6365 repo_template_instantiated (result
, extern_p
);
6367 instantiate_decl (result
);
6371 mark_class_instantiated (t
, extern_p
)
6375 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
6376 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
6377 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
6378 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
6379 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
6382 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
6383 rest_of_type_compilation (t
, 1);
6388 do_type_instantiation (t
, storage
)
6395 if (TREE_CODE (t
) == TYPE_DECL
)
6398 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
6400 cp_error ("explicit instantiation of non-template type `%T'", t
);
6406 /* With -fexternal-templates, explicit instantiations are treated the same
6407 as implicit ones. */
6408 if (flag_external_templates
)
6411 if (TYPE_SIZE (t
) == NULL_TREE
)
6413 cp_error ("explicit instantiation of `%#T' before definition of template",
6418 if (storage
== NULL_TREE
)
6420 else if (storage
== ridpointers
[(int) RID_INLINE
])
6422 else if (storage
== ridpointers
[(int) RID_EXTERN
])
6424 else if (storage
== ridpointers
[(int) RID_STATIC
])
6428 cp_error ("storage class `%D' applied to template instantiation",
6433 /* We've already instantiated this. */
6434 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
6438 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
6440 mark_class_instantiated (t
, extern_p
);
6441 repo_template_instantiated (t
, extern_p
);
6451 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6452 if (TREE_CODE (tmp
) == FUNCTION_DECL
6453 && DECL_TEMPLATE_INSTANTIATION (tmp
))
6455 mark_decl_instantiated (tmp
, extern_p
);
6456 repo_template_instantiated (tmp
, extern_p
);
6458 instantiate_decl (tmp
);
6461 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6462 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
6464 mark_decl_instantiated (tmp
, extern_p
);
6465 repo_template_instantiated (tmp
, extern_p
);
6467 instantiate_decl (tmp
);
6470 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
6471 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
6472 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
6476 /* Produce the definition of D, a _DECL generated from a template. */
6479 instantiate_decl (d
)
6482 tree ti
= DECL_TEMPLATE_INFO (d
);
6483 tree tmpl
= TI_TEMPLATE (ti
);
6484 tree args
= TI_ARGS (ti
);
6486 tree decl_pattern
, code_pattern
;
6488 int nested
= in_function_p ();
6490 int pattern_defined
;
6492 char *file
= input_filename
;
6495 DECL_TEMPLATE_INSTANTIATION (td
)
6496 /* This next clause handles friend templates defined inside
6497 class templates. The friend templates are not really
6498 instantiations from the point of view of the language, but
6499 they are instantiations from the point of view of the
6501 || (DECL_TEMPLATE_INFO (td
) && !DECL_TEMPLATE_SPECIALIZATION (td
));
6503 td
= DECL_TI_TEMPLATE (td
);
6505 /* In the case of a member template, decl_pattern is the partially
6506 instantiated declaration (in the instantiated class), and code_pattern
6507 is the original template definition. */
6508 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
6509 code_pattern
= DECL_TEMPLATE_RESULT (td
);
6511 if (TREE_CODE (d
) == FUNCTION_DECL
)
6513 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
6514 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
6518 d_defined
= ! DECL_IN_AGGR_P (d
);
6519 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
6525 if (TREE_CODE (d
) == FUNCTION_DECL
)
6527 tree spec
= retrieve_specialization (tmpl
, args
);
6529 if (spec
!= NULL_TREE
6530 && DECL_TEMPLATE_SPECIALIZATION (spec
))
6534 /* This needs to happen before any tsubsting. */
6535 if (! push_tinst_level (d
))
6538 push_to_top_level ();
6539 lineno
= DECL_SOURCE_LINE (d
);
6540 input_filename
= DECL_SOURCE_FILE (d
);
6542 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
6543 variable is a static const initialized in the class body. */
6544 if (TREE_CODE (d
) == VAR_DECL
6545 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
6547 pushclass (DECL_CONTEXT (d
), 2);
6548 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6550 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, LOOKUP_NORMAL
);
6553 if (pattern_defined
)
6555 repo_template_used (d
);
6557 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
6559 if (flag_alt_external_templates
)
6561 if (interface_unknown
)
6562 warn_if_unknown_interface (d
);
6564 else if (DECL_INTERFACE_KNOWN (code_pattern
))
6566 DECL_INTERFACE_KNOWN (d
) = 1;
6567 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
6570 warn_if_unknown_interface (code_pattern
);
6574 import_export_decl (d
);
6577 /* Reject all external templates except inline functions. */
6578 if (DECL_INTERFACE_KNOWN (d
)
6579 && ! DECL_NOT_REALLY_EXTERN (d
)
6580 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
6583 /* Defer all templates except inline functions used in another function. */
6584 if (! pattern_defined
6585 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
6588 add_pending_template (d
);
6592 lineno
= DECL_SOURCE_LINE (d
);
6593 input_filename
= DECL_SOURCE_FILE (d
);
6595 /* Trick tsubst into giving us a new decl in case the template changed. */
6596 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
6597 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
6598 /* decl_pattern has all but one level of template parms bound. Only pass
6599 in that one level of args. */
6600 temp
= innermost_args (args
, DECL_TEMPLATE_SPECIALIZATION (decl_pattern
));
6601 td
= tsubst (decl_pattern
, temp
, tmpl
);
6602 SET_DECL_IMPLICIT_INSTANTIATION (td
);
6603 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
6605 /* And set up DECL_INITIAL, since tsubst doesn't. */
6606 if (TREE_CODE (td
) == VAR_DECL
)
6608 pushclass (DECL_CONTEXT (d
), 2);
6609 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
6614 if (TREE_CODE (d
) == FUNCTION_DECL
)
6616 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
6618 DECL_INITIAL (td
) = error_mark_node
;
6620 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
6621 /* Set up the information about what is being specialized. */
6622 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
6624 duplicate_decls (td
, d
);
6625 if (TREE_CODE (d
) == FUNCTION_DECL
)
6626 DECL_INITIAL (td
) = 0;
6628 if (TREE_CODE (d
) == VAR_DECL
)
6630 DECL_IN_AGGR_P (d
) = 0;
6631 if (DECL_INTERFACE_KNOWN (d
))
6632 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
6635 DECL_EXTERNAL (d
) = 1;
6636 DECL_NOT_REALLY_EXTERN (d
) = 1;
6638 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
6640 else if (TREE_CODE (d
) == FUNCTION_DECL
)
6642 tree t
= DECL_SAVED_TREE (code_pattern
);
6644 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
6645 store_parm_decls ();
6647 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
6650 (TREE_OPERAND (t
, 0),
6651 tsubst_expr (TREE_OPERAND (t
, 1), args
, tmpl
));
6655 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
6657 current_member_init_list
6658 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
6659 current_base_init_list
6660 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
6665 /* Always keep the BLOCK node associated with the outermost
6666 pair of curly braces of a function. These are needed
6667 for correct operation of dwarfout.c. */
6670 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
6671 tsubst_expr (t
, args
, tmpl
);
6673 finish_function (lineno
, 0, nested
);
6678 input_filename
= file
;
6680 pop_from_top_level ();
6687 tsubst_chain (t
, argvec
)
6692 tree first
= tsubst (t
, argvec
, NULL_TREE
);
6695 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
6697 tree x
= tsubst (t
, argvec
, NULL_TREE
);
6698 TREE_CHAIN (last
) = x
;
6708 tsubst_expr_values (t
, argvec
)
6711 tree first
= NULL_TREE
;
6714 for (; t
; t
= TREE_CHAIN (t
))
6716 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
, NULL_TREE
);
6717 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
, NULL_TREE
);
6718 *p
= build_tree_list (pur
, val
);
6719 p
= &TREE_CHAIN (*p
);
6730 last_tree
= TREE_CHAIN (last_tree
) = t
;
6737 saved_trees
= tree_cons (NULL_TREE
, last_tree
, saved_trees
);
6738 last_tree
= NULL_TREE
;
6745 my_friendly_assert (saved_trees
!= NULL_TREE
, 0);
6747 last_tree
= TREE_VALUE (saved_trees
);
6748 saved_trees
= TREE_CHAIN (saved_trees
);
6751 /* D is an undefined function declaration in the presence of templates with
6752 the same name, listed in FNS. If one of them can produce D as an
6753 instantiation, remember this so we can instantiate it at EOF if D has
6754 not been defined by that time. */
6757 add_maybe_template (d
, fns
)
6762 if (DECL_MAYBE_TEMPLATE (d
))
6765 t
= most_specialized (fns
, d
, NULL_TREE
);
6768 if (t
== error_mark_node
)
6770 cp_error ("ambiguous template instantiation for `%D'", d
);
6774 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
6775 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
6776 DECL_MAYBE_TEMPLATE (d
) = 1;
6779 /* Instantiate an enumerated type. Used by instantiate_class_template and
6783 tsubst_enum (tag
, args
, field_chain
)
6787 extern tree current_local_enum
;
6788 tree prev_local_enum
= current_local_enum
;
6790 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
6791 tree e
, values
= NULL_TREE
;
6793 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
6795 tree elt
= build_enumerator (TREE_PURPOSE (e
),
6796 tsubst_expr (TREE_VALUE (e
), args
,
6798 TREE_CHAIN (elt
) = values
;
6802 finish_enum (newtag
, values
);
6804 if (NULL
!= field_chain
)
6805 *field_chain
= grok_enum_decls (NULL_TREE
);
6807 current_local_enum
= prev_local_enum
;