1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
46 extern struct obstack permanent_obstack
;
49 extern char *input_filename
;
50 struct pending_inline
*pending_template_expansions
;
52 tree current_template_parms
;
53 HOST_WIDE_INT processing_template_decl
;
55 tree pending_templates
;
56 static tree
*template_tail
= &pending_templates
;
59 static tree
*maybe_template_tail
= &maybe_templates
;
61 int minimal_parse_mode
;
63 int processing_specialization
;
64 static int template_header_count
;
66 #define obstack_chunk_alloc xmalloc
67 #define obstack_chunk_free free
69 static int unify
PROTO((tree
, tree
*, int, tree
, tree
, int *, int));
70 static void add_pending_template
PROTO((tree
));
71 static int push_tinst_level
PROTO((tree
));
72 static tree classtype_mangled_name
PROTO((tree
));
73 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
));
74 static tree tsubst_expr_values
PROTO((tree
, tree
));
75 static int comp_template_args
PROTO((tree
, tree
));
76 static int list_eq
PROTO((tree
, tree
));
77 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
78 static tree coerce_template_parms
PROTO((tree
, tree
, tree
));
79 static tree tsubst_enum
PROTO((tree
, tree
, int, tree
*));
80 static tree add_to_template_args
PROTO((tree
, tree
));
81 static int type_unification_real
PROTO((tree
, tree
*, tree
, tree
, int*,
83 static int processing_explicit_specialization
PROTO((int));
84 static void note_template_header
PROTO((int));
86 /* Restore the template parameter context. */
89 begin_member_template_processing (decl
)
95 parms
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
97 ++processing_template_decl
;
98 current_template_parms
99 = tree_cons (build_int_2 (0, processing_template_decl
),
100 parms
, current_template_parms
);
102 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
104 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
105 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
107 switch (TREE_CODE (parm
))
115 /* Make a CONST_DECL as is done in process_template_parm. */
116 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
118 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
124 my_friendly_abort (0);
129 /* Undo the effects of begin_member_template_processing. */
132 end_member_template_processing ()
134 if (! processing_template_decl
)
137 --processing_template_decl
;
138 current_template_parms
= TREE_CHAIN (current_template_parms
);
142 /* Returns non-zero iff T is a member template function. Works if T
143 is either a FUNCTION_DECL or a TEMPLATE_DECL. */
146 is_member_template (t
)
151 if (TREE_CODE (t
) != FUNCTION_DECL
152 && !DECL_FUNCTION_TEMPLATE_P (t
))
153 /* Anything that isn't a template or a template functon is
154 certainly not a member template. */
157 if ((DECL_FUNCTION_MEMBER_P (t
)
158 && !DECL_TEMPLATE_SPECIALIZATION (t
))
159 || (TREE_CODE (t
) == TEMPLATE_DECL
&&
160 DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))))
162 tree tmpl
= NULL_TREE
;
164 if (DECL_FUNCTION_TEMPLATE_P (t
))
166 else if (DECL_TEMPLATE_INFO (t
)
167 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
168 tmpl
= DECL_TI_TEMPLATE (t
);
172 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
173 int parm_levels
= list_length (parms
);
174 int template_class_levels
= 0;
175 tree ctx
= DECL_CLASS_CONTEXT (t
);
177 if (CLASSTYPE_TEMPLATE_INFO (ctx
))
181 /* Here, we should really count the number of levels
182 deep ctx is, making sure not to count any levels that
183 are just specializations. Since there are no member
184 template classes yet, we don't have to do all that. */
186 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
187 template_class_levels
= 1;
192 args
= CLASSTYPE_TI_ARGS (ctx
);
194 if (args
== NULL_TREE
)
195 template_class_levels
= 1;
197 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
198 if (uses_template_parms (TREE_VEC_ELT (args
, i
)))
200 template_class_levels
++;
206 if (parm_levels
> template_class_levels
)
214 /* Return a new template argument vector which contains all of ARGS,
215 but has as its innermost set of arguments the EXTRA_ARGS. */
218 add_to_template_args (args
, extra_args
)
224 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
226 new_args
= make_tree_vec (2);
227 TREE_VEC_ELT (new_args
, 0) = args
;
233 new_args
= make_tree_vec (TREE_VEC_LENGTH (args
) - 1);
235 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
236 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
239 TREE_VEC_ELT (new_args
,
240 TREE_VEC_LENGTH (new_args
) - 1) = extra_args
;
245 /* We've got a template header coming up; push to a new level for storing
249 begin_template_parm_list ()
252 declare_pseudo_global_level ();
253 ++processing_template_decl
;
254 note_template_header (0);
258 /* We've just seen template <>. */
261 begin_specialization ()
263 note_template_header (1);
267 /* Called at then end of processing a declaration preceeded by
271 end_specialization ()
273 reset_specialization ();
277 /* Any template <>'s that we have seen thus far are not referring to a
278 function specialization. */
281 reset_specialization ()
283 processing_specialization
= 0;
284 template_header_count
= 0;
288 /* We've just seen a template header. If SPECIALIZATION is non-zero,
289 it was of the form template <>. */
292 note_template_header (specialization
)
295 processing_specialization
= specialization
;
296 template_header_count
++;
300 /* Returns non-zero iff a declarator, in which the number of template
301 types that appeared was TEMPLATE_COUNT, is an explicit
305 processing_explicit_specialization (template_count
)
308 /* A function declaration is an explicit specialization of a member
309 template if all of the following conditions hold:
311 o There was a template <...> preceeding the declaration.
312 o The last template <...> was in fact template <>.
313 o The number of template <...>'s preceeding the declaration, less
314 the number of template classes with arguments specified used to
315 qualify the function name, is 1.
319 template <> void S<int>::foo();
320 template <class T> template <> void S<T>::foo();
321 template <> struct S<int> { ... template <> void foo(); }
323 The first of these is not a specialization of S<int>::foo() (it
324 is instead a specialization of S<T>::foo), while the next two are
325 specializations of member template functions. */
327 return processing_specialization
328 && template_header_count
> template_count
;
331 /* Returns the template function specialized by TEMPLATE_ID, or
332 NULL_TREE if there is none.
334 The TEMPLATE_ID is a TEMPLATE_ID_EXPR. The TYPE is
335 the type it has been declared to have. Return the TEMPLATE_DECL
336 that is being specialized, and put the specialization arguments in
337 *TARGS. If no appropriate specialization can be found, NULL_TREE is
338 returned, and *TARGS is assigned NULL_TREE. If complain is
339 non-zero, error messages are printed where appropriate. */
342 determine_explicit_specialization (template_id
, type
, targs_out
,
343 need_member_template
,
348 int need_member_template
;
354 tree matching_fns
= NULL_TREE
;
358 my_friendly_assert (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
359 && TREE_OPERAND (template_id
, 0), 0);
361 fns
= TREE_OPERAND (template_id
, 0);
363 if (is_overloaded_fn (fns
))
364 fn
= get_first_fn (fns
);
368 overloaded
= really_overloaded_fn (fns
);
370 for (; fn
!= NULL_TREE
;
371 fn
= overloaded
? DECL_CHAIN (fn
) : NULL_TREE
)
376 if (TREE_CODE (fn
) != TEMPLATE_DECL
377 || (need_member_template
&& !is_member_template (fn
)))
380 if (list_length (TREE_OPERAND (template_id
, 1)) > DECL_NTPARMS (fn
))
383 targs
= make_scratch_vec (DECL_NTPARMS (fn
));
385 /* We allow incomplete unification here, because we are going to
386 check all the functions. */
387 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
388 &TREE_VEC_ELT (targs
, 0),
390 ? TYPE_ARG_TYPES (TREE_TYPE (fn
)) : NULL_TREE
,
391 type
? TYPE_ARG_TYPES (type
) : NULL_TREE
,
392 TREE_OPERAND (template_id
, 1),
397 /* Unification was successful. See if the return types
399 if (type
!= NULL_TREE
)
401 tree tmpl_return_type
= tsubst (TREE_TYPE (TREE_TYPE (fn
)),
406 if (tmpl_return_type
!= TREE_TYPE (type
))
408 /* Always complain about this. With ambiguity, some
409 other context, might resolve things. But, a
410 non-matching return type will always be a
412 cp_error ("Return type of explicit specialization of");
413 cp_error ("`%D' is `%T', but should be `%T'.",
414 fn
, TREE_TYPE (type
), tmpl_return_type
);
415 *targs_out
= NULL_TREE
;
420 matching_fns
= scratch_tree_cons (fn
, targs
, matching_fns
);
424 if (matching_fns
== NULL_TREE
)
427 cp_error ("`%D' does not match any template declaration.",
430 *targs_out
= NULL_TREE
;
434 if (TREE_CHAIN (matching_fns
) != NULL_TREE
)
440 cp_error ("Ambiguous explicit specialization. Candidates are:");
441 for (fn
= matching_fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
442 cp_error (" %D", TREE_PURPOSE (fn
));
445 *targs_out
= NULL_TREE
;
449 /* We have one, and exactly one, match. */
450 *targs_out
= TREE_VALUE (matching_fns
);
451 return TREE_PURPOSE (matching_fns
);
455 /* Check to see if the function just declared, as indicated in
456 DECLARATOR, and in DECL, is a specialization. Check that the
457 specialization is OK. If FLAGS == 1, we are being called by
458 finish_struct_methods. If FLAGS == 2, we are being called by
459 grokfndecl, and the function has a definition, or is a friend. If
460 FLAGS == 3, this is a friend declaration.
461 Returns 0 if the decl is not an explicit specialization or
462 instantiation, 1 if it is an explicit specialization, and 2 if it
463 is an explicit instantiation. */
466 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
472 int finish_member
= flags
== 1;
473 int have_def
= flags
== 2;
474 int is_friend
= flags
== 3;
476 if (processing_explicit_specialization (template_count
)
478 || TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
480 tree tmpl
= NULL_TREE
;
481 tree dname
= DECL_NAME (decl
);
482 tree ctype
= DECL_CLASS_CONTEXT (decl
);
485 /* We've come across a declarator that looks like: U f<T1,
486 T2, ...>(A1, A2, ..). This is an explicit template
487 specialization. Check that:
489 o The explicitly specified parameters together with those
490 that can be deduced by template argument deduction
491 uniquely determine a particular specialization.
493 See [temp.expl.spec]. */
496 && TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
497 && !processing_explicit_specialization (template_count
)
500 if (! have_def
&& ! template_header_count
&& ! ctype
)
501 /* This is not an explict specialization. It must be
502 an explicit instantiation. */
504 else if (template_header_count
> template_count
505 && !processing_specialization
)
507 cp_error ("template-id `%D' in declaration of primary template",
511 else if (pedantic
|| uses_template_parms (decl
))
512 pedwarn ("explicit specialization not preceeded by `template <>'");
515 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
519 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
522 fns
= IDENTIFIER_GLOBAL_VALUE (dname
);
526 declarator
= lookup_template_function (fns
, NULL_TREE
);
529 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
531 /* A friend declaration. We can't do much, because we don't
532 know what this resolves to, yet. */
533 my_friendly_assert (is_friend
!= 0, 0);
534 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
539 && TREE_CODE (TREE_OPERAND (declarator
, 0)) == IDENTIFIER_NODE
)
543 if (TYPE_BEING_DEFINED (ctype
) && !finish_member
)
545 /* Since finish_struct_1 has not been called yet, we
546 can't call lookup_fnfields. We note that this
547 template is a specialization, and proceed, letting
548 finish_struct_methods fix this up later. */
549 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
550 DECL_TEMPLATE_INFO (decl
)
551 = perm_tree_cons (NULL_TREE
,
552 TREE_OPERAND (declarator
, 1),
557 fns
= lookup_fnfields (TYPE_BINFO (ctype
),
558 TREE_OPERAND (declarator
, 0),
561 if (fns
== NULL_TREE
)
563 cp_error ("No member template `%s' declared in `%T'",
564 IDENTIFIER_POINTER (TREE_OPERAND (declarator
,
570 TREE_OPERAND (declarator
, 0) = fns
;
574 determine_explicit_specialization
575 (declarator
, TREE_TYPE (decl
), &targs
,
576 TREE_CODE (decl
) == TEMPLATE_DECL
, 1);
580 /* Mangle the function name appropriately. */
581 if (name_mangling_version
>= 1)
583 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
586 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
588 hash_tree_chain (build_pointer_type (ctype
),
591 DECL_ASSEMBLER_NAME (decl
)
592 = build_template_decl_overload
595 TREE_TYPE (TREE_TYPE (tmpl
)),
596 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
597 targs
, ctype
!= NULL_TREE
);
600 if (is_friend
&& !have_def
)
602 /* This is not really a declaration of a specialization.
603 It's just the name of an instantiation. But, it's not
604 a request for an instantiation, either. */
605 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
606 DECL_TEMPLATE_INFO (decl
)
607 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
611 /* This function declaration is a template specialization.
613 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
614 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
615 = perm_tree_cons (targs
, decl
,
616 DECL_TEMPLATE_SPECIALIZATIONS
618 /* If DECL_TI_TEMPLATE (decl), the decl is an
619 instantiation of a specialization of a member template.
620 (In other words, there was a member template, in a
621 class template. That member template was specialized.
622 We then instantiated the class, so there is now an
623 instance of that specialization.)
625 According to the CD2,
627 14.7.3.13 [tmpl.expl.spec]
629 A specialization of a member function template or
630 member class template of a non-specialized class
631 template is itself a template.
633 So, we just leave the template info alone in this case. */
634 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
635 DECL_TEMPLATE_INFO (decl
)
636 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
644 /* Process information from new template parameter NEXT and append it to the
648 process_template_parm (list
, next
)
656 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
657 defval
= TREE_PURPOSE (parm
);
658 parm
= TREE_VALUE (parm
);
659 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
663 tree p
= TREE_VALUE (tree_last (list
));
665 if (TREE_CODE (p
) == TYPE_DECL
)
666 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
668 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
677 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
678 /* is a const-param */
679 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
681 /* A template parameter is not modifiable. */
682 TREE_READONLY (parm
) = 1;
683 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
684 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
686 cp_error ("`%#T' is not a valid type for a template constant parameter",
688 if (DECL_NAME (parm
) == NULL_TREE
)
689 error (" a template type parameter must begin with `class' or `typename'");
690 TREE_TYPE (parm
) = void_type_node
;
693 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
694 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
695 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
697 tinfo
= make_node (TEMPLATE_CONST_PARM
);
698 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
699 if (TREE_PERMANENT (parm
) == 0)
701 parm
= copy_node (parm
);
702 TREE_PERMANENT (parm
) = 1;
704 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
705 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
706 DECL_INITIAL (decl
) = tinfo
;
707 DECL_INITIAL (parm
) = tinfo
;
708 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
);
712 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
713 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
714 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
715 TYPE_NAME (t
) = decl
;
716 TYPE_STUB_DECL (t
) = decl
;
718 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
);
720 SET_DECL_ARTIFICIAL (decl
);
722 parm
= build_tree_list (defval
, parm
);
723 return chainon (list
, parm
);
726 /* The end of a template parameter list has been reached. Process the
727 tree list into a parameter vector, converting each parameter into a more
728 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
732 end_template_parm_list (parms
)
737 tree saved_parmlist
= make_tree_vec (list_length (parms
));
739 current_template_parms
740 = tree_cons (build_int_2 (0, processing_template_decl
),
741 saved_parmlist
, current_template_parms
);
743 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
744 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
746 return saved_parmlist
;
749 /* end_template_decl is called after a template declaration is seen. */
754 reset_specialization ();
756 if (! processing_template_decl
)
759 /* This matches the pushlevel in begin_template_parm_list. */
762 --processing_template_decl
;
763 current_template_parms
= TREE_CHAIN (current_template_parms
);
764 (void) get_pending_sizes (); /* Why? */
767 /* Generate a valid set of template args from current_template_parms. */
770 current_template_args ()
772 tree header
= current_template_parms
;
773 int length
= list_length (header
);
774 tree args
= make_tree_vec (length
);
779 tree a
= copy_node (TREE_VALUE (header
));
780 int i
= TREE_VEC_LENGTH (a
);
781 TREE_TYPE (a
) = NULL_TREE
;
784 tree t
= TREE_VEC_ELT (a
, i
);
786 /* t will be a list if we are called from within a
787 begin/end_template_parm_list pair, but a vector directly
788 if within a begin/end_member_template_processing pair. */
789 if (TREE_CODE (t
) == TREE_LIST
)
793 if (TREE_CODE (t
) == TYPE_DECL
)
796 t
= DECL_INITIAL (t
);
799 TREE_VEC_ELT (a
, i
) = t
;
801 TREE_VEC_ELT (args
, --l
) = a
;
802 header
= TREE_CHAIN (header
);
809 push_template_decl (decl
)
813 tree args
= NULL_TREE
;
815 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
819 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
820 && DECL_CLASS_CONTEXT (decl
))
822 /* Note that this template is a "primary template" */
823 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
824 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
827 /* Partial specialization. */
828 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
829 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
831 tree type
= TREE_TYPE (decl
);
832 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
833 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
834 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
836 for (; spec
; spec
= TREE_CHAIN (spec
))
838 /* purpose: args to main template
839 value: spec template */
840 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
844 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
845 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
846 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
847 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
851 args
= current_template_args ();
853 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
855 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
856 DECL_TEMPLATE_PARMS (tmpl
) = current_template_parms
;
857 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
858 if (DECL_LANG_SPECIFIC (decl
))
860 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
861 DECL_STATIC_FUNCTION_P (tmpl
) =
862 DECL_STATIC_FUNCTION_P (decl
);
864 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
866 /* A specialization of a member template of a template
868 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
869 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
870 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
879 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
880 cp_error ("must specialize `%#T' before defining member `%#D'",
882 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
883 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
884 else if (! DECL_TEMPLATE_INFO (decl
))
886 cp_error ("template definition of non-template `%#D'", decl
);
890 tmpl
= DECL_TI_TEMPLATE (decl
);
892 if (is_member_template (tmpl
))
894 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
895 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
896 if (TREE_VEC_LENGTH (t
)
897 != TREE_VEC_LENGTH (a
))
899 cp_error ("got %d template parameters for `%#D'",
900 TREE_VEC_LENGTH (a
), decl
);
901 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
903 if (TREE_VEC_LENGTH (args
) > 1)
904 /* Get the template parameters for the enclosing template
906 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
911 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
915 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
917 /* When processing an inline member template of a
918 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
919 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
920 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
922 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
923 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
925 /* There should be template arguments if and only if there is a
927 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
930 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
932 cp_error ("got %d template parameters for `%#D'",
933 TREE_VEC_LENGTH (a
), decl
);
934 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
937 /* Get the innermost set of template arguments. */
938 args
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
940 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
941 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
944 tmpl
= pushdecl_top_level (tmpl
);
947 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
949 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
951 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
953 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
954 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
956 else if (! DECL_LANG_SPECIFIC (decl
))
957 cp_error ("template declaration of `%#D'", decl
);
959 DECL_TEMPLATE_INFO (decl
) = info
;
962 /* Convert all template arguments to their appropriate types, and return
963 a vector containing the resulting values. If any error occurs, return
967 coerce_template_parms (parms
, arglist
, in_decl
)
971 int nparms
, nargs
, i
, lost
= 0;
974 if (arglist
== NULL_TREE
)
976 else if (TREE_CODE (arglist
) == TREE_VEC
)
977 nargs
= TREE_VEC_LENGTH (arglist
);
979 nargs
= list_length (arglist
);
981 nparms
= TREE_VEC_LENGTH (parms
);
985 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
987 error ("incorrect number of parameters (%d, should be %d)",
990 cp_error_at ("in template expansion for decl `%D'", in_decl
);
991 return error_mark_node
;
994 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
995 vec
= copy_node (arglist
);
998 vec
= make_tree_vec (nparms
);
999 for (i
= 0; i
< nparms
; i
++)
1006 arglist
= TREE_CHAIN (arglist
);
1008 if (arg
== error_mark_node
)
1011 arg
= TREE_VALUE (arg
);
1013 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
1015 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1018 arg
= tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1021 TREE_VEC_ELT (vec
, i
) = arg
;
1024 for (i
= 0; i
< nparms
; i
++)
1026 tree arg
= TREE_VEC_ELT (vec
, i
);
1027 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1029 int is_type
, requires_type
;
1031 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
1032 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
1034 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
1035 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
1037 cp_pedwarn ("to refer to a type member of a template parameter,");
1038 cp_pedwarn (" use `typename %E'", arg
);
1039 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
1040 TREE_OPERAND (arg
, 1));
1043 if (is_type
!= requires_type
)
1047 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1050 cp_error (" expected a constant of type `%T', got `%T'",
1051 TREE_TYPE (parm
), arg
);
1053 cp_error (" expected a type, got `%E'", arg
);
1056 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1061 val
= groktypename (arg
);
1062 if (! processing_template_decl
)
1064 tree t
= target_type (val
);
1065 if (TREE_CODE (t
) != TYPENAME_TYPE
1067 && decl_function_context (TYPE_MAIN_DECL (t
)))
1069 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
1070 return error_mark_node
;
1076 tree t
= tsubst (TREE_TYPE (parm
), vec
,
1077 TREE_VEC_LENGTH (vec
), in_decl
);
1078 if (processing_template_decl
)
1081 val
= digest_init (t
, arg
, (tree
*) 0);
1083 if (val
== error_mark_node
|| processing_template_decl
)
1086 /* 14.2: Other template-arguments must be constant-expressions,
1087 addresses of objects or functions with external linkage, or of
1088 static class members. */
1089 else if (IS_AGGR_TYPE (TREE_TYPE (val
)))
1091 cp_error ("object `%E' cannot be used as template argument", arg
);
1092 val
= error_mark_node
;
1094 else if (!TREE_CONSTANT (val
))
1096 cp_error ("non-constant `%E' cannot be used as template argument",
1098 val
= error_mark_node
;
1100 else if (POINTER_TYPE_P (TREE_TYPE (val
))
1101 && ! integer_zerop (val
)
1102 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
1103 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
1107 if (TREE_CODE (t
) == ADDR_EXPR
)
1109 tree a
= TREE_OPERAND (t
, 0);
1111 if (TREE_CODE (a
) == STRING_CST
)
1113 cp_error ("string literal %E is not a valid template argument", a
);
1114 error ("because it is the address of an object with static linkage");
1115 val
= error_mark_node
;
1117 else if (TREE_CODE (a
) != VAR_DECL
1118 && TREE_CODE (a
) != FUNCTION_DECL
)
1120 else if (! TREE_PUBLIC (a
))
1122 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
1123 val
= error_mark_node
;
1129 cp_error ("`%E' is not a valid template argument", t
);
1130 error ("it must be %s%s with external linkage",
1131 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
1132 ? "a pointer to " : "",
1133 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
1134 ? "a function" : "an object");
1135 val
= error_mark_node
;
1140 if (val
== error_mark_node
)
1143 TREE_VEC_ELT (vec
, i
) = val
;
1146 return error_mark_node
;
1151 comp_template_args (oldargs
, newargs
)
1152 tree oldargs
, newargs
;
1156 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
1159 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
1161 tree nt
= TREE_VEC_ELT (newargs
, i
);
1162 tree ot
= TREE_VEC_ELT (oldargs
, i
);
1166 if (TREE_CODE (nt
) != TREE_CODE (ot
))
1168 if (TREE_CODE (nt
) == TREE_VEC
)
1170 /* For member templates */
1171 if (comp_template_args (nt
, ot
))
1174 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
1176 if (comptypes (ot
, nt
, 1))
1179 else if (cp_tree_equal (ot
, nt
) > 0)
1186 /* Given class template name and parameter list, produce a user-friendly name
1187 for the instantiation. */
1190 mangle_class_name_for_template (name
, parms
, arglist
)
1192 tree parms
, arglist
;
1194 static struct obstack scratch_obstack
;
1195 static char *scratch_firstobj
;
1198 if (!scratch_firstobj
)
1199 gcc_obstack_init (&scratch_obstack
);
1201 obstack_free (&scratch_obstack
, scratch_firstobj
);
1202 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
1205 #define buflen sizeof(buf)
1206 #define check if (bufp >= buf+buflen-1) goto too_long
1207 #define ccat(c) *bufp++=(c); check
1208 #define advance bufp+=strlen(bufp); check
1209 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1212 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1214 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1219 nparms
= TREE_VEC_LENGTH (parms
);
1220 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
1221 for (i
= 0; i
< nparms
; i
++)
1223 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1224 tree arg
= TREE_VEC_ELT (arglist
, i
);
1229 if (TREE_CODE (parm
) == TYPE_DECL
)
1231 cat (type_as_string (arg
, 0));
1235 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
1237 if (TREE_CODE (arg
) == TREE_LIST
)
1239 /* New list cell was built because old chain link was in
1241 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
1242 arg
= TREE_VALUE (arg
);
1244 /* No need to check arglist against parmlist here; we did that
1245 in coerce_template_parms, called from lookup_template_class. */
1246 cat (expr_as_string (arg
, 0));
1249 char *bufp
= obstack_next_free (&scratch_obstack
);
1251 while (bufp
[offset
- 1] == ' ')
1253 obstack_blank_fast (&scratch_obstack
, offset
);
1255 /* B<C<char> >, not B<C<char>> */
1256 if (bufp
[offset
- 1] == '>')
1261 return (char *) obstack_base (&scratch_obstack
);
1266 fatal ("out of (preallocated) string space creating template instantiation name");
1272 classtype_mangled_name (t
)
1275 if (CLASSTYPE_TEMPLATE_INFO (t
)
1276 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
1278 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
1279 char *mangled_name
= mangle_class_name_for_template
1280 (IDENTIFIER_POINTER (name
),
1281 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
1282 CLASSTYPE_TI_ARGS (t
));
1283 tree id
= get_identifier (mangled_name
);
1284 IDENTIFIER_TEMPLATE (id
) = name
;
1288 return TYPE_IDENTIFIER (t
);
1292 add_pending_template (d
)
1297 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
1298 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
1300 ti
= DECL_TEMPLATE_INFO (d
);
1302 if (TI_PENDING_TEMPLATE_FLAG (ti
))
1305 *template_tail
= perm_tree_cons
1306 (current_function_decl
, d
, NULL_TREE
);
1307 template_tail
= &TREE_CHAIN (*template_tail
);
1308 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
1312 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1313 may be either a _DECL or an overloaded function or an
1314 IDENTIFIER_NODE), and ARGLIST. */
1317 lookup_template_function (fns
, arglist
)
1320 if (fns
== NULL_TREE
)
1322 cp_error ("non-template used as template");
1323 return error_mark_node
;
1326 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
1328 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1329 arglist
= copy_list (arglist
);
1333 return build_min (TEMPLATE_ID_EXPR
,
1335 ? TREE_TYPE (fns
) : unknown_type_node
,
1340 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1341 parameters, find the desired type.
1343 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1344 Since ARGLIST is build on the decl_obstack, we must copy it here
1345 to keep it from being reclaimed when the decl storage is reclaimed.
1347 IN_DECL, if non-NULL, is the template declaration we are trying to
1351 lookup_template_class (d1
, arglist
, in_decl
)
1355 tree
template, parmlist
;
1359 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
1361 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
1363 template = IDENTIFIER_CLASS_VALUE (d1
);
1365 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
1367 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
1368 d1
= DECL_NAME (template);
1370 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
1372 template = CLASSTYPE_TI_TEMPLATE (d1
);
1373 d1
= DECL_NAME (template);
1376 my_friendly_abort (272);
1378 /* With something like `template <class T> class X class X { ... };'
1379 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1380 We don't want to do that, but we have to deal with the situation, so
1381 let's give them some syntax errors to chew on instead of a crash. */
1383 return error_mark_node
;
1384 if (TREE_CODE (template) != TEMPLATE_DECL
)
1386 cp_error ("non-template type `%T' used as a template", d1
);
1388 cp_error_at ("for template declaration `%D'", in_decl
);
1389 return error_mark_node
;
1392 if (PRIMARY_TEMPLATE_P (template))
1394 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
1396 arglist
= coerce_template_parms (parmlist
, arglist
, template);
1397 if (arglist
== error_mark_node
)
1398 return error_mark_node
;
1399 if (uses_template_parms (arglist
))
1402 if (comp_template_args
1403 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
1404 found
= TREE_TYPE (template);
1407 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
1408 found
; found
= TREE_CHAIN (found
))
1410 if (TI_USES_TEMPLATE_PARMS (found
)
1411 && comp_template_args (TREE_PURPOSE (found
), arglist
))
1415 found
= TREE_VALUE (found
);
1420 if (can_free (&permanent_obstack
, arglist
))
1421 obstack_free (&permanent_obstack
, arglist
);
1426 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
1428 id
= get_identifier (mangled_name
);
1429 IDENTIFIER_TEMPLATE (id
) = d1
;
1431 maybe_push_to_top_level (uses_template_parms (arglist
));
1432 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
1433 pop_from_top_level ();
1437 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1440 arglist
= CLASSTYPE_TI_ARGS (ctx
);
1442 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
1444 int save_temp
= processing_template_decl
;
1445 processing_template_decl
= 0;
1446 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
1447 processing_template_decl
= save_temp
;
1451 t
= lookup_nested_type_by_name (ctx
, id
);
1452 my_friendly_assert (t
!= NULL_TREE
, 42);
1456 /* Seems to be wanted. */
1457 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1459 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1461 arglist
= copy_to_permanent (arglist
);
1462 CLASSTYPE_TEMPLATE_INFO (t
)
1463 = perm_tree_cons (template, arglist
, NULL_TREE
);
1464 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1465 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
1466 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1467 = uses_template_parms (arglist
);
1469 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
1471 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1472 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
1473 /* if (! uses_template_parms (arglist)) */
1474 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
1475 = get_identifier (build_overload_name (t
, 1, 1));
1477 if (flag_external_templates
&& ! uses_template_parms (arglist
)
1478 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1479 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1480 add_pending_template (t
);
1486 /* Should be defined in parse.h. */
1490 uses_template_parms (t
)
1495 switch (TREE_CODE (t
))
1499 /* We assume that the object must be instantiated in order to build
1500 the COMPONENT_REF, so we test only whether the type of the
1501 COMPONENT_REF uses template parms. */
1502 return uses_template_parms (TREE_TYPE (t
));
1504 case IDENTIFIER_NODE
:
1505 if (!IDENTIFIER_TEMPLATE (t
))
1507 my_friendly_abort (42);
1509 /* aggregates of tree nodes */
1512 int i
= TREE_VEC_LENGTH (t
);
1514 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
1519 if (uses_template_parms (TREE_PURPOSE (t
))
1520 || uses_template_parms (TREE_VALUE (t
)))
1522 return uses_template_parms (TREE_CHAIN (t
));
1524 /* constructed type nodes */
1526 case REFERENCE_TYPE
:
1527 return uses_template_parms (TREE_TYPE (t
));
1529 if (TYPE_PTRMEMFUNC_FLAG (t
))
1530 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1532 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1534 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
1536 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1538 return uses_template_parms (TREE_TYPE (t
));
1540 if (uses_template_parms (TYPE_DOMAIN (t
)))
1542 return uses_template_parms (TREE_TYPE (t
));
1544 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
1546 return uses_template_parms (TREE_TYPE (t
));
1548 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
1550 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1552 return uses_template_parms (TREE_TYPE (t
));
1556 return uses_template_parms (TREE_TYPE (t
));
1560 /* ??? What about FIELD_DECLs? */
1561 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
1562 && uses_template_parms (DECL_TI_ARGS (t
)))
1567 if (uses_template_parms (TREE_TYPE (t
)))
1569 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
1574 return uses_template_parms (TREE_TYPE (t
));
1576 return uses_template_parms (TREE_OPERAND (t
, 0));
1578 /* template parm nodes */
1579 case TEMPLATE_TYPE_PARM
:
1580 case TEMPLATE_CONST_PARM
:
1583 /* simple type nodes */
1585 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
1587 return uses_template_parms (TYPE_MAX_VALUE (t
));
1599 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
1600 if (uses_template_parms (TREE_VALUE (v
)))
1612 /* Non-error_mark_node ERROR_MARKs are bad things. */
1613 my_friendly_assert (t
== error_mark_node
, 274);
1622 return uses_template_parms (TREE_OPERAND (t
, 0));
1625 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
1626 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
1627 return uses_template_parms (TREE_OPERAND (t
, 1));
1631 case REINTERPRET_CAST_EXPR
:
1632 case CONST_CAST_EXPR
:
1633 case STATIC_CAST_EXPR
:
1634 case DYNAMIC_CAST_EXPR
:
1642 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
1650 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
1651 if (uses_template_parms (TREE_OPERAND (t
, i
)))
1658 sorry ("testing %s for template parms",
1659 tree_code_name
[(int) TREE_CODE (t
)]);
1660 my_friendly_abort (82);
1666 static struct tinst_level
*current_tinst_level
= 0;
1667 static struct tinst_level
*free_tinst_level
= 0;
1668 static int tinst_depth
= 0;
1669 extern int max_tinst_depth
;
1670 #ifdef GATHER_STATISTICS
1671 int depth_reached
= 0;
1675 push_tinst_level (d
)
1678 struct tinst_level
*new;
1680 if (tinst_depth
>= max_tinst_depth
)
1682 struct tinst_level
*p
= current_tinst_level
;
1684 char *file
= input_filename
;
1686 error ("template instantiation depth exceeds maximum of %d",
1688 error (" (use -ftemplate-depth-NN to increase the maximum)");
1689 cp_error (" instantiating `%D'", d
);
1691 for (; p
; p
= p
->next
)
1693 cp_error (" instantiated from `%D'", p
->decl
);
1695 input_filename
= p
->file
;
1697 error (" instantiated from here");
1700 input_filename
= file
;
1705 if (free_tinst_level
)
1707 new = free_tinst_level
;
1708 free_tinst_level
= new->next
;
1711 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
1715 new->file
= input_filename
;
1716 new->next
= current_tinst_level
;
1717 current_tinst_level
= new;
1720 #ifdef GATHER_STATISTICS
1721 if (tinst_depth
> depth_reached
)
1722 depth_reached
= tinst_depth
;
1731 struct tinst_level
*old
= current_tinst_level
;
1733 current_tinst_level
= old
->next
;
1734 old
->next
= free_tinst_level
;
1735 free_tinst_level
= old
;
1739 struct tinst_level
*
1742 struct tinst_level
*p
= current_tinst_level
;
1745 for (; p
->next
; p
= p
->next
)
1751 instantiate_class_template (type
)
1754 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
1756 if (type
== error_mark_node
)
1757 return error_mark_node
;
1759 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
1761 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
1764 template = TI_TEMPLATE (template_info
);
1765 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
1766 args
= TI_ARGS (template_info
);
1768 t
= most_specialized_class
1769 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
1771 if (t
== error_mark_node
)
1773 char *str
= "candidates are:";
1774 cp_error ("ambiguous class template instantiation for `%#T'", type
);
1775 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
1777 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
1779 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
1783 TYPE_BEING_DEFINED (type
) = 1;
1784 return error_mark_node
;
1787 pattern
= TREE_TYPE (t
);
1789 pattern
= TREE_TYPE (template);
1791 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1795 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
1797 TYPE_BEING_DEFINED (type
) = 1;
1799 if (! push_tinst_level (type
))
1802 maybe_push_to_top_level (uses_template_parms (type
));
1803 pushclass (type
, 0);
1805 if (flag_external_templates
)
1807 if (flag_alt_external_templates
)
1809 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1810 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1811 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1812 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1813 && CLASSTYPE_INTERFACE_KNOWN (type
);
1817 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1818 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1819 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1820 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1821 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1822 && CLASSTYPE_INTERFACE_KNOWN (type
);
1827 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1828 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1831 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1832 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1833 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1834 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1835 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1836 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1837 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1838 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1839 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1840 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1841 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1842 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1843 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1844 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1845 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1846 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1847 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1848 TYPE_USES_COMPLEX_INHERITANCE (type
)
1849 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1850 TYPE_USES_MULTIPLE_INHERITANCE (type
)
1851 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
1852 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1853 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1854 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
1855 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
1858 tree binfo
= TYPE_BINFO (type
);
1859 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1865 int len
= TREE_VEC_LENGTH (pbases
);
1866 bases
= make_tree_vec (len
);
1867 for (i
= 0; i
< len
; ++i
)
1871 TREE_VEC_ELT (bases
, i
) = elt
1872 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
1873 TREE_VEC_LENGTH (args
), NULL_TREE
);
1874 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1876 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
1878 ("base type `%T' of `%T' fails to be a struct or class type",
1879 TREE_TYPE (elt
), type
);
1880 else if (! uses_template_parms (type
)
1881 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
1883 cp_error ("base class `%T' of `%T' has incomplete type",
1884 TREE_TYPE (elt
), type
);
1886 /* Don't initialize this until the vector is filled out, or
1887 lookups will crash. */
1888 BINFO_BASETYPES (binfo
) = bases
;
1892 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1894 field_chain
= &TYPE_FIELDS (type
);
1896 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1898 tree name
= TREE_PURPOSE (t
);
1899 tree tag
= TREE_VALUE (t
);
1901 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1902 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1904 tree e
, newtag
= tsubst_enum (tag
, args
,
1905 TREE_VEC_LENGTH (args
), field_chain
);
1907 while (*field_chain
)
1909 DECL_FIELD_CONTEXT (*field_chain
) = type
;
1910 field_chain
= &TREE_CHAIN (*field_chain
);
1915 TREE_VEC_LENGTH (args
), NULL_TREE
);
1918 /* Don't replace enum constants here. */
1919 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1920 if (TREE_CODE (t
) != CONST_DECL
)
1922 tree r
= tsubst (t
, args
,
1923 TREE_VEC_LENGTH (args
), NULL_TREE
);
1924 if (TREE_CODE (r
) == VAR_DECL
)
1926 if (! uses_template_parms (r
))
1927 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1928 /* Perhaps I should do more of grokfield here. */
1930 DECL_IN_AGGR_P (r
) = 1;
1931 DECL_EXTERNAL (r
) = 1;
1932 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1936 field_chain
= &TREE_CHAIN (r
);
1939 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1940 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
1942 if (DECL_CONSTRUCTOR_P (t
))
1943 grok_ctor_properties (type
, t
);
1944 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
1945 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
1948 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1949 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1950 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
1953 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
1954 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
1955 TREE_VEC_LENGTH (args
), NULL_TREE
);
1957 /* This does injection for friend classes. */
1958 for (; d
; d
= TREE_CHAIN (d
))
1959 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1961 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
1962 TREE_VEC_LENGTH (args
), NULL_TREE
);
1964 for (; d
; d
= TREE_CHAIN (d
))
1966 tree t
= TREE_VALUE (d
);
1968 if (TREE_CODE (t
) == TYPE_DECL
)
1969 /* Already injected. */;
1975 if (! uses_template_parms (type
))
1978 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1979 if (TREE_CODE (tmp
) == FIELD_DECL
)
1981 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
1982 require_complete_type (tmp
);
1985 type
= finish_struct_1 (type
, 0);
1986 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
1988 repo_template_used (type
);
1989 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
1990 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
1994 TYPE_SIZE (type
) = integer_zero_node
;
1995 CLASSTYPE_METHOD_VEC (type
)
1996 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1999 TYPE_BEING_DEFINED (type
) = 0;
2002 pop_from_top_level ();
2012 if (t1
== NULL_TREE
)
2013 return t2
== NULL_TREE
;
2014 if (t2
== NULL_TREE
)
2016 /* Don't care if one declares its arg const and the other doesn't -- the
2017 main variant of the arg type is all that matters. */
2018 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
2019 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
2021 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
2025 lookup_nested_type_by_name (ctype
, name
)
2030 complete_type (ctype
);
2032 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
2034 if (name
== TREE_PURPOSE (t
)
2035 /* this catches typedef enum { foo } bar; */
2036 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
2037 return TREE_VALUE (t
);
2042 /* If arg is a non-type template parameter that does not depend on template
2043 arguments, fold it like we weren't in the body of a template. */
2046 maybe_fold_nontype_arg (arg
)
2049 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2050 && !uses_template_parms (arg
))
2052 /* Sometimes, one of the args was an expression involving a
2053 template constant parameter, like N - 1. Now that we've
2054 tsubst'd, we might have something like 2 - 1. This will
2055 confuse lookup_template_class, so we do constant folding
2056 here. We have to unset processing_template_decl, to
2057 fool build_expr_from_tree() into building an actual
2060 int saved_processing_template_decl
= processing_template_decl
;
2061 processing_template_decl
= 0;
2062 arg
= fold (build_expr_from_tree (arg
));
2063 processing_template_decl
= saved_processing_template_decl
;
2068 /* Take the tree structure T and replace template parameters used therein
2069 with the argument vector ARGS. NARGS is the number of args; should
2070 be removed. IN_DECL is an associated decl for diagnostics.
2072 tsubst is used for dealing with types, decls and the like; for
2073 expressions, use tsubst_expr or tsubst_copy. */
2076 tsubst (t
, args
, nargs
, in_decl
)
2083 if (t
== NULL_TREE
|| t
== error_mark_node
2084 || t
== integer_type_node
2085 || t
== void_type_node
2086 || t
== char_type_node
)
2089 type
= TREE_TYPE (t
);
2090 if (type
== unknown_type_node
)
2091 my_friendly_abort (42);
2092 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
2093 && TREE_CODE (t
) != TYPENAME_TYPE
)
2094 type
= tsubst (type
, args
, nargs
, in_decl
);
2096 switch (TREE_CODE (t
))
2099 if (TYPE_PTRMEMFUNC_P (t
))
2101 tree r
= build_ptrmemfunc_type
2102 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
2103 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2107 /* else fall through */
2109 if (uses_template_parms (t
))
2111 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
2112 tree r
= lookup_template_class (t
, argvec
, in_decl
);
2113 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2117 /* else fall through */
2119 case IDENTIFIER_NODE
:
2132 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2133 if (ctx
== NULL_TREE
)
2135 else if (ctx
== current_function_decl
)
2136 return lookup_name (TYPE_IDENTIFIER (t
), 1);
2138 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
2142 if (t
== integer_type_node
)
2145 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
2146 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
2150 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
2151 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
2152 if (processing_template_decl
)
2154 tree itype
= make_node (INTEGER_TYPE
);
2155 TYPE_MIN_VALUE (itype
) = size_zero_node
;
2156 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
2161 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
2162 return build_index_2_type (size_zero_node
, max
);
2165 case TEMPLATE_TYPE_PARM
:
2166 case TEMPLATE_CONST_PARM
:
2171 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2173 idx
= TEMPLATE_TYPE_IDX (t
);
2174 level
= TEMPLATE_TYPE_LEVEL (t
);
2178 idx
= TEMPLATE_CONST_IDX (t
);
2179 level
= TEMPLATE_CONST_LEVEL (t
);
2182 if (TREE_VEC_LENGTH (args
) > 0)
2184 tree arg
= NULL_TREE
;
2186 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
2188 if (TREE_VEC_LENGTH (args
) >= level
- 1)
2190 (TREE_VEC_ELT (args
, level
- 1), idx
);
2192 else if (level
== 1)
2193 arg
= TREE_VEC_ELT (args
, idx
);
2195 if (arg
!= NULL_TREE
)
2197 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2198 return cp_build_type_variant
2199 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
2200 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
2206 /* If we get here, we must have been looking at a parm for a
2207 more deeply nested template. */
2208 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
2209 && TEMPLATE_CONST_LEVEL (t
) > 1)
2210 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
2211 && TEMPLATE_TYPE_LEVEL (t
) > 1),
2218 /* We can get here when processing a member template function
2219 of a template class. */
2221 tree decl
= DECL_TEMPLATE_RESULT (t
);
2227 /* We might already have an instance of this template. */
2228 tree instances
= DECL_TEMPLATE_INSTANTIATIONS (t
);
2229 tree ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, in_decl
);
2231 for (; instances
; instances
= TREE_CHAIN (instances
))
2232 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances
)) == ctx
)
2233 return TREE_VALUE (instances
);
2235 /* Make a new template decl. It will be similar to the
2236 original, but will record the current template arguments.
2237 We also create a new function declaration, which is just
2238 like the old one, but points to this new template, rather
2239 than the old one. */
2240 tmpl
= copy_node (t
);
2241 copy_lang_decl (tmpl
);
2242 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
2243 DECL_CHAIN (tmpl
) = NULL_TREE
;
2244 TREE_CHAIN (tmpl
) = NULL_TREE
;
2245 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
2246 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
2247 DECL_RESULT (tmpl
) = new_decl
;
2248 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
2249 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
2250 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
2251 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
2253 /* The template parameters for this new template are all the
2254 template parameters for the old template, except the
2255 outermost level of parameters. */
2256 DECL_TEMPLATE_PARMS (tmpl
)
2257 = copy_node (DECL_TEMPLATE_PARMS (tmpl
));
2258 for (parms
= DECL_TEMPLATE_PARMS (tmpl
);
2259 TREE_CHAIN (parms
) != NULL_TREE
;
2260 parms
= TREE_CHAIN (parms
))
2261 TREE_CHAIN (parms
) = copy_node (TREE_CHAIN (parms
));
2263 /* Record this partial instantiation. */
2264 DECL_TEMPLATE_INSTANTIATIONS (t
)
2265 = perm_tree_cons (NULL_TREE
, tmpl
,
2266 DECL_TEMPLATE_INSTANTIATIONS (t
));
2268 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
2279 if (DECL_CONTEXT (t
) != NULL_TREE
2280 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
2282 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
2286 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
2287 type
= tsubst (type
, args
, nargs
, in_decl
);
2293 type
= tsubst (type
, args
, nargs
, in_decl
);
2296 /* Do we already have this instantiation? */
2297 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2299 tree tmpl
= DECL_TI_TEMPLATE (t
);
2300 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2302 for (; decls
; decls
= TREE_CHAIN (decls
))
2303 if (TREE_TYPE (TREE_VALUE (decls
)) == type
2304 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
2305 && comp_template_args (TREE_PURPOSE (decls
), args
))
2306 return TREE_VALUE (decls
);
2309 /* We do NOT check for matching decls pushed separately at this
2310 point, as they may not represent instantiations of this
2311 template, and in any case are considered separate under the
2312 discrete model. Instead, see add_maybe_template. */
2316 TREE_TYPE (r
) = type
;
2319 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
2320 DECL_CLASS_CONTEXT (r
) = ctx
;
2322 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
2323 IDENTIFIER_POINTER (DECL_NAME (r
)),
2324 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
2326 /* Type-conversion operator. Reconstruct the name, in
2327 case it's the name of one of the template's parameters. */
2328 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
2331 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
2333 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
2334 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
2335 buf
= (char *) alloca (strlen (dbuf
)
2336 + sizeof (DESTRUCTOR_DECL_PREFIX
));
2337 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
2340 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
2344 /* Instantiations of template functions must be mangled
2345 specially, in order to conform to 14.5.5.1
2346 [temp.over.link]. We use in_decl below rather than
2347 DECL_TI_TEMPLATE (r) because the latter is set to
2348 NULL_TREE in instantiate_decl. */
2352 if (DECL_TEMPLATE_INFO (r
))
2353 tmpl
= DECL_TI_TEMPLATE (r
);
2357 /* tmpl will be NULL if this is a specialization of a
2358 member template of a template class. */
2359 if (name_mangling_version
< 1
2360 || tmpl
== NULL_TREE
2361 || (member
&& !is_member_template (tmpl
)
2362 && !DECL_TEMPLATE_INFO (tmpl
)))
2364 arg_types
= TYPE_ARG_TYPES (type
);
2365 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2366 arg_types
= hash_tree_chain
2367 (build_pointer_type (DECL_CONTEXT (r
)),
2370 DECL_ASSEMBLER_NAME (r
)
2371 = build_decl_overload (DECL_NAME (r
), arg_types
,
2376 /* We pass the outermost template parameters to
2377 build_template_decl_overload since the innermost
2378 template parameters are still just template
2379 parameters; there are no corresponding subsitution
2381 /* FIXME The messed up thing here is that we get here with
2382 full args and only one level of parms. This is necessary
2383 because when we partially instantiate a member template,
2384 even though there's really only one level of parms left
2385 we re-use the parms from the original template, which
2386 have level 2. When this is fixed we can remove the
2387 add_to_template_args from instantiate_template. */
2388 tree tparms
= DECL_TEMPLATE_PARMS (tmpl
);
2390 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
2391 tparms
= TREE_CHAIN (tparms
);
2393 my_friendly_assert (tparms
!= NULL_TREE
2394 && TREE_CODE (tparms
) == TREE_LIST
,
2396 tparms
= TREE_VALUE (tparms
);
2398 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
2399 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2400 arg_types
= hash_tree_chain
2401 (build_pointer_type (DECL_CONTEXT (r
)),
2404 DECL_ASSEMBLER_NAME (r
)
2405 = build_template_decl_overload
2406 (DECL_NAME (r
), arg_types
,
2407 TREE_TYPE (TREE_TYPE (tmpl
)),
2409 TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
2410 ? TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1) :
2416 make_decl_rtl (r
, NULL_PTR
, 1);
2418 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
2419 DECL_MAIN_VARIANT (r
) = r
;
2420 DECL_RESULT (r
) = NULL_TREE
;
2421 DECL_INITIAL (r
) = NULL_TREE
;
2423 TREE_STATIC (r
) = 0;
2424 TREE_PUBLIC (r
) = 1;
2425 DECL_EXTERNAL (r
) = 1;
2426 DECL_INTERFACE_KNOWN (r
) = 0;
2427 DECL_DEFER_OUTPUT (r
) = 0;
2428 TREE_CHAIN (r
) = NULL_TREE
;
2429 DECL_CHAIN (r
) = NULL_TREE
;
2431 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
2432 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
2434 /* Look for matching decls for the moment. */
2435 if (! member
&& ! flag_ansi_overloading
)
2437 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
2440 if (decls
== NULL_TREE
)
2442 else if (is_overloaded_fn (decls
))
2443 for (decls
= get_first_fn (decls
); decls
;
2444 decls
= DECL_CHAIN (decls
))
2446 if (TREE_CODE (decls
) == FUNCTION_DECL
2447 && TREE_TYPE (decls
) == type
)
2456 int dcl_only
= ! DECL_INITIAL (d
);
2458 DECL_INITIAL (r
) = error_mark_node
;
2459 duplicate_decls (r
, d
);
2462 DECL_INITIAL (r
) = 0;
2466 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2468 tree tmpl
= DECL_TI_TEMPLATE (t
);
2469 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2470 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2472 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
2473 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
2475 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2476 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2478 /* If we have a preexisting version of this function, don't expand
2479 the template version, use the other instead. */
2480 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
2481 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
2483 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2485 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
2486 = tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
2489 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2490 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2492 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
2493 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
2500 tree r
= copy_node (t
);
2501 TREE_TYPE (r
) = type
;
2502 DECL_INITIAL (r
) = TREE_TYPE (r
);
2503 DECL_CONTEXT (r
) = NULL_TREE
;
2504 #ifdef PROMOTE_PROTOTYPES
2505 if ((TREE_CODE (type
) == INTEGER_TYPE
2506 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2507 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
2508 DECL_ARG_TYPE (r
) = integer_type_node
;
2511 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
2517 tree r
= copy_node (t
);
2518 TREE_TYPE (r
) = type
;
2521 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
2523 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2524 TREE_CHAIN (r
) = NULL_TREE
;
2530 tree r
= copy_node (t
);
2532 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2533 TREE_CHAIN (r
) = NULL_TREE
;
2540 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2542 /* Do we already have this instantiation? */
2543 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2545 tree tmpl
= DECL_TI_TEMPLATE (t
);
2546 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2548 for (; decls
; decls
= TREE_CHAIN (decls
))
2549 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
2550 return TREE_VALUE (decls
);
2554 TREE_TYPE (r
) = type
;
2555 DECL_CONTEXT (r
) = ctx
;
2556 if (TREE_STATIC (r
))
2557 DECL_ASSEMBLER_NAME (r
)
2558 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
2560 /* Don't try to expand the initializer until someone tries to use
2561 this variable; otherwise we run into circular dependencies. */
2562 DECL_INITIAL (r
) = NULL_TREE
;
2567 if (DECL_LANG_SPECIFIC (r
))
2570 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
2573 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2575 tree tmpl
= DECL_TI_TEMPLATE (t
);
2576 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2577 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2579 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2580 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2581 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2583 TREE_CHAIN (r
) = NULL_TREE
;
2588 if (t
== TYPE_NAME (TREE_TYPE (t
)))
2589 return TYPE_NAME (type
);
2592 tree r
= copy_node (t
);
2593 TREE_TYPE (r
) = type
;
2594 DECL_CONTEXT (r
) = current_class_type
;
2595 TREE_CHAIN (r
) = NULL_TREE
;
2601 tree purpose
, value
, chain
, result
;
2602 int via_public
, via_virtual
, via_protected
;
2604 if (t
== void_list_node
)
2607 via_public
= TREE_VIA_PUBLIC (t
);
2608 via_protected
= TREE_VIA_PROTECTED (t
);
2609 via_virtual
= TREE_VIA_VIRTUAL (t
);
2611 purpose
= TREE_PURPOSE (t
);
2613 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
2614 value
= TREE_VALUE (t
);
2616 value
= tsubst (value
, args
, nargs
, in_decl
);
2617 chain
= TREE_CHAIN (t
);
2618 if (chain
&& chain
!= void_type_node
)
2619 chain
= tsubst (chain
, args
, nargs
, in_decl
);
2620 if (purpose
== TREE_PURPOSE (t
)
2621 && value
== TREE_VALUE (t
)
2622 && chain
== TREE_CHAIN (t
))
2624 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
2625 purpose
, value
, chain
);
2626 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
2630 if (type
!= NULL_TREE
)
2636 if (type
== TREE_TYPE (t
))
2639 TREE_TYPE (t
) = complete_type (type
);
2640 if (IS_AGGR_TYPE (type
))
2642 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
2643 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
2644 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
2645 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
2650 /* Otherwise, a vector of template arguments. */
2652 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
2653 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
2655 bzero ((char *) elts
, len
* sizeof (tree
));
2657 for (i
= 0; i
< len
; i
++)
2659 elts
[i
] = maybe_fold_nontype_arg
2660 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
));
2662 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
2669 t
= make_tree_vec (len
);
2670 for (i
= 0; i
< len
; i
++)
2671 TREE_VEC_ELT (t
, i
) = elts
[i
];
2676 case REFERENCE_TYPE
:
2679 enum tree_code code
;
2680 if (type
== TREE_TYPE (t
))
2683 code
= TREE_CODE (t
);
2684 if (code
== POINTER_TYPE
)
2685 r
= build_pointer_type (type
);
2687 r
= build_reference_type (type
);
2688 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
2689 /* Will this ever be needed for TYPE_..._TO values? */
2694 return build_offset_type
2695 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
2699 tree values
= TYPE_ARG_TYPES (t
);
2700 tree context
= TYPE_CONTEXT (t
);
2701 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
2704 /* Don't bother recursing if we know it won't change anything. */
2705 if (values
!= void_list_node
)
2707 /* This should probably be rewritten to use hash_tree_cons for
2708 the memory savings. */
2709 tree first
= NULL_TREE
;
2712 for (; values
&& values
!= void_list_node
;
2713 values
= TREE_CHAIN (values
))
2715 tree value
= TYPE_MAIN_VARIANT (type_decays_to
2716 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
2717 /* Don't instantiate default args unless they are used.
2718 Handle it in build_over_call instead. */
2719 tree purpose
= TREE_PURPOSE (values
);
2720 tree x
= build_tree_list (purpose
, value
);
2723 TREE_CHAIN (last
) = x
;
2729 if (values
== void_list_node
)
2730 TREE_CHAIN (last
) = void_list_node
;
2735 context
= tsubst (context
, args
, nargs
, in_decl
);
2736 /* Could also optimize cases where return value and
2737 values have common elements (e.g., T min(const &T, const T&). */
2739 /* If the above parameters haven't changed, just return the type. */
2740 if (type
== TREE_TYPE (t
)
2741 && values
== TYPE_VALUES (t
)
2742 && context
== TYPE_CONTEXT (t
))
2745 /* Construct a new type node and return it. */
2746 if (TREE_CODE (t
) == FUNCTION_TYPE
2747 && context
== NULL_TREE
)
2749 fntype
= build_function_type (type
, values
);
2751 else if (context
== NULL_TREE
)
2753 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
2754 args
, nargs
, in_decl
);
2755 fntype
= build_cplus_method_type (base
, type
,
2756 TREE_CHAIN (values
));
2760 fntype
= make_node (TREE_CODE (t
));
2761 TREE_TYPE (fntype
) = type
;
2762 TYPE_CONTEXT (fntype
) = context
;
2763 TYPE_VALUES (fntype
) = values
;
2764 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
2765 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
2766 TYPE_MODE (fntype
) = TYPE_MODE (t
);
2767 if (TYPE_METHOD_BASETYPE (t
))
2768 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
2769 args
, nargs
, in_decl
);
2770 /* Need to generate hash value. */
2771 my_friendly_abort (84);
2773 fntype
= build_type_variant (fntype
,
2778 raises
= tsubst (raises
, args
, nargs
, in_decl
);
2779 fntype
= build_exception_variant (fntype
, raises
);
2785 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
2787 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
2789 r
= build_cplus_array_type (type
, domain
);
2795 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
2796 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2797 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
2801 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
2802 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
2806 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2807 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
2808 return cp_build_type_variant
2809 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
2810 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
2814 return make_pointer_declarator
2815 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2818 return make_reference_declarator
2819 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2822 return build_parse_node
2823 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2824 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2827 return make_call_declarator
2828 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2829 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2830 TREE_OPERAND (t
, 2),
2831 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
2834 return build_parse_node
2835 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2836 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2839 sorry ("use of `%s' in template",
2840 tree_code_name
[(int) TREE_CODE (t
)]);
2841 return error_mark_node
;
2848 emit_line_note (input_filename
, lineno
);
2852 expand_start_bindings (0);
2859 int saved_warn_unused
;
2861 if (processing_template_decl
)
2863 saved_warn_unused
= warn_unused
;
2866 expand_end_bindings (getdecls (), kept_level_p (), 0);
2867 if (processing_template_decl
)
2868 warn_unused
= saved_warn_unused
;
2869 t
= poplevel (kept_level_p (), 1, 0);
2874 /* Like tsubst, but deals with expressions. This function just replaces
2875 template parms; to finish processing the resultant expression, use
2879 tsubst_copy (t
, args
, nargs
, in_decl
)
2884 enum tree_code code
;
2886 if (t
== NULL_TREE
|| t
== error_mark_node
)
2889 code
= TREE_CODE (t
);
2894 return do_identifier (DECL_NAME (t
), 0);
2898 if (DECL_CONTEXT (t
))
2900 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2901 if (ctx
== current_function_decl
)
2902 return lookup_name (DECL_NAME (t
), 0);
2903 else if (ctx
!= DECL_CONTEXT (t
))
2904 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
2910 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2911 t
= tsubst (t
, args
, nargs
, in_decl
);
2916 if (is_member_template (t
))
2917 return tsubst (t
, args
, nargs
, in_decl
);
2922 case IDENTIFIER_NODE
:
2923 return do_identifier (t
, 0);
2927 case REINTERPRET_CAST_EXPR
:
2928 case CONST_CAST_EXPR
:
2929 case STATIC_CAST_EXPR
:
2930 case DYNAMIC_CAST_EXPR
:
2932 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
2933 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2936 case PREDECREMENT_EXPR
:
2937 case PREINCREMENT_EXPR
:
2938 case POSTDECREMENT_EXPR
:
2939 case POSTINCREMENT_EXPR
:
2941 case TRUTH_NOT_EXPR
:
2944 case CONVERT_EXPR
: /* Unary + */
2951 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2956 case TRUNC_DIV_EXPR
:
2958 case FLOOR_DIV_EXPR
:
2959 case ROUND_DIV_EXPR
:
2960 case EXACT_DIV_EXPR
:
2962 case BIT_ANDTC_EXPR
:
2965 case TRUNC_MOD_EXPR
:
2966 case FLOOR_MOD_EXPR
:
2967 case TRUTH_ANDIF_EXPR
:
2968 case TRUTH_ORIF_EXPR
:
2969 case TRUTH_AND_EXPR
:
2990 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2991 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2995 tree fn
= TREE_OPERAND (t
, 0);
2996 if (really_overloaded_fn (fn
))
2997 fn
= tsubst_copy (get_first_fn (fn
), args
, nargs
, in_decl
);
2999 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
3001 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3005 case METHOD_CALL_EXPR
:
3007 tree name
= TREE_OPERAND (t
, 0);
3008 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3010 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
3011 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
3013 else if (TREE_CODE (name
) == SCOPE_REF
3014 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
3016 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
3017 name
= TREE_OPERAND (name
, 1);
3018 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
3019 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
3020 name
= build_nt (SCOPE_REF
, base
, name
);
3023 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3025 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3026 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
3033 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3034 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3035 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3040 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3041 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3042 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3043 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
3050 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3051 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3052 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
3053 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
3057 case TEMPLATE_ID_EXPR
:
3059 /* Substituted template arguments */
3060 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3062 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
3063 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
3065 return lookup_template_function
3066 (tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
), targs
);
3071 tree purpose
, value
, chain
;
3073 if (t
== void_list_node
)
3076 purpose
= TREE_PURPOSE (t
);
3078 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
3079 value
= TREE_VALUE (t
);
3081 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
3082 chain
= TREE_CHAIN (t
);
3083 if (chain
&& chain
!= void_type_node
)
3084 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
3085 if (purpose
== TREE_PURPOSE (t
)
3086 && value
== TREE_VALUE (t
)
3087 && chain
== TREE_CHAIN (t
))
3089 return tree_cons (purpose
, value
, chain
);
3096 case TEMPLATE_TYPE_PARM
:
3097 case TEMPLATE_CONST_PARM
:
3099 case REFERENCE_TYPE
:
3105 return tsubst (t
, args
, nargs
, in_decl
);
3107 case IDENTIFIER_NODE
:
3108 if (IDENTIFIER_TYPENAME_P (t
))
3109 return build_typename_overload
3110 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
3116 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
3117 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
3124 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
3127 tsubst_expr (t
, args
, nargs
, in_decl
)
3132 if (t
== NULL_TREE
|| t
== error_mark_node
)
3135 if (processing_template_decl
)
3136 return tsubst_copy (t
, args
, nargs
, in_decl
);
3138 switch (TREE_CODE (t
))
3141 lineno
= TREE_COMPLEXITY (t
);
3142 emit_line_note (input_filename
, lineno
);
3144 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3149 lineno
= TREE_COMPLEXITY (t
);
3150 emit_line_note (input_filename
, lineno
);
3151 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3152 /* Do default conversion if safe and possibly important,
3153 in case within ({...}). */
3154 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
3155 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
3156 t
= default_conversion (t
);
3157 cplus_expand_expr_stmt (t
);
3164 int i
= suspend_momentary ();
3167 lineno
= TREE_COMPLEXITY (t
);
3168 emit_line_note (input_filename
, lineno
);
3170 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3171 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3172 TREE_OPERAND (t
, 2) != 0);
3173 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3175 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3176 resume_momentary (i
);
3183 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
3184 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3185 int cond_scope
= (TREE_OPERAND (t
, 1)
3186 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
3188 lineno
= TREE_COMPLEXITY (t
);
3189 emit_line_note (input_filename
, lineno
);
3192 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
3193 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3195 emit_line_note (input_filename
, lineno
);
3196 expand_start_loop_continue_elsewhere (1);
3200 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3201 emit_line_note (input_filename
, lineno
);
3203 expand_exit_loop_if_false (0, condition_conversion (tmp
));
3207 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
3210 emit_line_note (input_filename
, lineno
);
3211 expand_loop_continue_here ();
3212 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3214 cplus_expand_expr_stmt (tmp
);
3227 lineno
= TREE_COMPLEXITY (t
);
3229 emit_line_note (input_filename
, lineno
);
3230 expand_start_loop (1);
3232 cond
= TREE_OPERAND (t
, 0);
3233 if (TREE_CODE (cond
) == DECL_STMT
)
3235 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
3236 emit_line_note (input_filename
, lineno
);
3237 expand_exit_loop_if_false (0, condition_conversion (cond
));
3239 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
3241 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3253 lineno
= TREE_COMPLEXITY (t
);
3255 emit_line_note (input_filename
, lineno
);
3256 expand_start_loop_continue_elsewhere (1);
3258 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3259 expand_loop_continue_here ();
3261 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3262 emit_line_note (input_filename
, lineno
);
3263 expand_exit_loop_if_false (0, condition_conversion (cond
));
3274 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3276 lineno
= TREE_COMPLEXITY (t
);
3279 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3280 emit_line_note (input_filename
, lineno
);
3281 expand_start_cond (condition_conversion (tmp
), 0);
3283 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3284 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3286 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
3288 expand_start_else ();
3289 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3303 tree substmt
= TREE_OPERAND (t
, 0);
3305 lineno
= TREE_COMPLEXITY (t
);
3307 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3310 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
3311 tsubst_expr (substmt
, args
, nargs
, in_decl
);
3313 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3319 lineno
= TREE_COMPLEXITY (t
);
3320 emit_line_note (input_filename
, lineno
);
3321 if (! expand_exit_something ())
3322 error ("break statement not within loop or switch");
3326 lineno
= TREE_COMPLEXITY (t
);
3327 emit_line_note (input_filename
, lineno
);
3328 if (! expand_continue_loop (0))
3329 error ("continue statement not within a loop");
3335 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3337 lineno
= TREE_COMPLEXITY (t
);
3340 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3341 emit_line_note (input_filename
, lineno
);
3342 c_expand_start_case (val
);
3345 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3346 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3348 expand_end_case (val
);
3359 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3360 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3364 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
3371 lineno
= TREE_COMPLEXITY (t
);
3372 emit_line_note (input_filename
, lineno
);
3373 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
3375 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
3376 TREE_USED (decl
) = 1;
3380 expand_computed_goto
3381 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3385 lineno
= TREE_COMPLEXITY (t
);
3386 emit_line_note (input_filename
, lineno
);
3387 expand_start_try_stmts ();
3388 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3389 expand_start_all_catch ();
3391 tree handler
= TREE_OPERAND (t
, 1);
3392 for (; handler
; handler
= TREE_CHAIN (handler
))
3393 tsubst_expr (handler
, args
, nargs
, in_decl
);
3395 expand_end_all_catch ();
3399 lineno
= TREE_COMPLEXITY (t
);
3401 if (TREE_OPERAND (t
, 0))
3403 tree d
= TREE_OPERAND (t
, 0);
3404 expand_start_catch_block
3405 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
3406 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
3409 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
3410 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3411 expand_end_catch_block ();
3416 lineno
= TREE_COMPLEXITY (t
);
3418 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
3419 tsubst_enum (t
, args
, nargs
, NULL
);
3423 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
3429 instantiate_template (tmpl
, targ_ptr
)
3430 tree tmpl
, targ_ptr
;
3434 struct obstack
*old_fmp_obstack
;
3435 extern struct obstack
*function_maybepermanent_obstack
;
3437 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
3439 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3443 /* Check to see if there is a matching specialization. */
3444 for (specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
3446 specs
= TREE_CHAIN (specs
))
3447 if (comp_template_args (TREE_PURPOSE (specs
), targ_ptr
))
3448 return TREE_VALUE (specs
);
3451 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3452 old_fmp_obstack
= function_maybepermanent_obstack
;
3453 function_maybepermanent_obstack
= &permanent_obstack
;
3455 len
= DECL_NTPARMS (tmpl
);
3460 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
3461 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
3463 tree nt
= target_type (t
);
3464 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
3466 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
3467 cp_error (" trying to instantiate `%D'", tmpl
);
3468 fndecl
= error_mark_node
;
3472 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
3474 targ_ptr
= copy_to_permanent (targ_ptr
);
3476 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
3477 targ_ptr
= add_to_template_args (DECL_TI_ARGS (tmpl
), targ_ptr
);
3479 /* substitute template parameters */
3480 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
3482 if (flag_external_templates
)
3483 add_pending_template (fndecl
);
3486 function_maybepermanent_obstack
= old_fmp_obstack
;
3492 /* Push the name of the class template into the scope of the instantiation. */
3495 overload_template_name (type
)
3498 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
3501 if (IDENTIFIER_CLASS_VALUE (id
)
3502 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
3505 decl
= build_decl (TYPE_DECL
, id
, type
);
3506 SET_DECL_ARTIFICIAL (decl
);
3507 pushdecl_class_level (decl
);
3510 /* Like type_unfication but designed specially to handle conversion
3514 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
, strict
)
3515 tree fn
, explicit_targs
, targs
, args
, return_type
;
3519 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3520 tree decl_arg_types
= args
;
3522 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
3524 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
3526 /* This is a template conversion operator. Use the return types
3527 as well as the argument types. */
3528 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
3529 TREE_TYPE (TREE_TYPE (fn
)),
3531 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
3536 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
3537 &TREE_VEC_ELT (targs
, 0),
3547 /* Type unification.
3549 We have a function template signature with one or more references to
3550 template parameters, and a parameter list we wish to fit to this
3551 template. If possible, produce a list of parameters for the template
3552 which will cause it to fit the supplied parameter list.
3554 Return zero for success, 2 for an incomplete match that doesn't resolve
3555 all the types, and 1 for complete failure. An error message will be
3556 printed only for an incomplete match.
3558 TPARMS[NTPARMS] is an array of template parameter types;
3559 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3560 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3561 and ARGS is the argument list we're trying to match against it.
3563 If SUBR is 1, we're being called recursively (to unify the arguments of
3564 a function or method parameter of a function template), so don't zero
3565 out targs and don't fail on an incomplete match.
3567 If STRICT is 1, the match must be exact (for casts of overloaded
3568 addresses, explicit instantiation, and more_specialized). */
3571 type_unification (tparms
, targs
, parms
, args
, targs_in
, nsubsts
,
3572 strict
, allow_incomplete
)
3573 tree tparms
, *targs
, parms
, args
, targs_in
;
3574 int *nsubsts
, strict
, allow_incomplete
;
3576 int ntparms
= TREE_VEC_LENGTH (tparms
);
3581 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
3583 /* Insert any explicit template arguments. They are encoded as the
3584 operands of NOP_EXPRs so that unify can tell that they are
3585 explicit arguments. */
3586 for (i
= 0, t
= targs_in
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
), ++i
)
3587 targs
[i
] = build1 (NOP_EXPR
, NULL_TREE
, TREE_VALUE (t
));
3589 r
= type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, 0,
3590 strict
, allow_incomplete
);
3592 for (i
= 0, t
= targs_in
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
), ++i
)
3593 if (TREE_CODE (targs
[i
]) == NOP_EXPR
)
3594 targs
[i
] = TREE_OPERAND (targs
[i
], 0);
3601 type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, subr
,
3602 strict
, allow_incomplete
)
3603 tree tparms
, *targs
, parms
, args
;
3604 int *nsubsts
, subr
, strict
, allow_incomplete
;
3608 int ntparms
= TREE_VEC_LENGTH (tparms
);
3610 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
3611 my_friendly_assert (parms
== NULL_TREE
3612 || TREE_CODE (parms
) == TREE_LIST
, 290);
3613 /* ARGS could be NULL (via a call from parse.y to
3614 build_x_function_call). */
3616 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
3617 my_friendly_assert (ntparms
> 0, 292);
3620 && parms
!= void_list_node
3622 && args
!= void_list_node
)
3624 parm
= TREE_VALUE (parms
);
3625 parms
= TREE_CHAIN (parms
);
3626 arg
= TREE_VALUE (args
);
3627 args
= TREE_CHAIN (args
);
3629 if (arg
== error_mark_node
)
3631 if (arg
== unknown_type_node
)
3634 /* Conversions will be performed on a function argument that
3635 corresponds with a function parameter that contains only
3636 non-deducible template parameters and explicitly specified
3637 template parameters. */
3638 if (! uses_template_parms (parm
))
3642 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3643 type
= TREE_TYPE (arg
);
3652 if (comptypes (parm
, type
, 1))
3657 if (can_convert_arg (parm
, type
, arg
))
3662 if (can_convert (parm
, type
))
3670 if (TREE_CODE (arg
) == VAR_DECL
)
3671 arg
= TREE_TYPE (arg
);
3672 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
3673 arg
= TREE_TYPE (arg
);
3675 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3677 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
3678 if (TREE_CODE (arg
) == TREE_LIST
3679 && TREE_TYPE (arg
) == unknown_type_node
3680 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
3682 int nsubsts
, ntparms
;
3685 /* Have to back unify here */
3686 arg
= TREE_VALUE (arg
);
3688 ntparms
= DECL_NTPARMS (arg
);
3689 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
3690 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
3692 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
3694 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
3695 parm
, NULL_TREE
, &nsubsts
, strict
,
3698 arg
= TREE_TYPE (arg
);
3701 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
3702 arg
= TREE_TYPE (arg
);
3704 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
3706 if (TREE_CODE (arg
) == FUNCTION_TYPE
3707 || TREE_CODE (arg
) == METHOD_TYPE
)
3708 arg
= build_pointer_type (arg
);
3709 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
3710 arg
= build_pointer_type (TREE_TYPE (arg
));
3712 arg
= TYPE_MAIN_VARIANT (arg
);
3715 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
3723 /* Fail if we've reached the end of the parm list, and more args
3724 are present, and the parm list isn't variadic. */
3725 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
3727 /* Fail if parms are left and they don't have default values. */
3729 && parms
!= void_list_node
3730 && TREE_PURPOSE (parms
) == NULL_TREE
)
3733 for (i
= 0; i
< ntparms
; i
++)
3736 if (!allow_incomplete
)
3737 error ("incomplete type unification");
3743 /* Tail recursion is your friend. */
3746 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
3747 tree tparms
, *targs
, parm
, arg
;
3748 int *nsubsts
, ntparms
, strict
;
3752 /* I don't think this will do the right thing with respect to types.
3753 But the only case I've seen it in so far has been array bounds, where
3754 signedness is the only information lost, and I think that will be
3756 while (TREE_CODE (parm
) == NOP_EXPR
)
3757 parm
= TREE_OPERAND (parm
, 0);
3759 if (arg
== error_mark_node
)
3761 if (arg
== unknown_type_node
)
3766 switch (TREE_CODE (parm
))
3769 /* In a type which contains a nested-name-specifier, template
3770 argument values cannot be deduced for template parameters used
3771 within the nested-name-specifier. */
3774 case TEMPLATE_TYPE_PARM
:
3776 idx
= TEMPLATE_TYPE_IDX (parm
);
3777 /* Check for mixed types and values. */
3778 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
3781 if (!strict
&& targs
[idx
] != NULL_TREE
&&
3782 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
3783 /* An explicit template argument. Don't even try to match
3784 here; the overload resolution code will manage check to
3785 see whether the call is legal. */
3788 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
3789 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
3792 /* Template type parameters cannot contain cv-quals; i.e.
3793 template <class T> void f (T& a, T& b) will not generate
3794 void f (const int& a, const int& b). */
3795 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
3796 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
3798 arg
= TYPE_MAIN_VARIANT (arg
);
3801 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
3802 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
3803 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
3806 /* Simple cases: Value already set, does match or doesn't. */
3807 if (targs
[idx
] == arg
3809 && TREE_CODE (targs
[idx
]) == NOP_EXPR
3810 && TREE_OPERAND (targs
[idx
], 0) == arg
))
3812 else if (targs
[idx
])
3816 case TEMPLATE_CONST_PARM
:
3818 idx
= TEMPLATE_CONST_IDX (parm
);
3821 int i
= cp_tree_equal (targs
[idx
], arg
);
3827 my_friendly_abort (42);
3830 targs
[idx
] = copy_to_permanent (arg
);
3834 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
3835 return unify (tparms
, targs
, ntparms
, parm
,
3836 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
3838 if (TREE_CODE (arg
) != POINTER_TYPE
)
3840 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3843 case REFERENCE_TYPE
:
3844 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
3845 arg
= TREE_TYPE (arg
);
3846 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
3850 if (TREE_CODE (arg
) != ARRAY_TYPE
)
3852 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
3853 nsubsts
, strict
) != 0)
3855 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3863 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3866 if (TREE_CODE (parm
) == INTEGER_TYPE
)
3868 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
3869 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
3870 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
3872 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
3873 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
3874 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
3877 else if (TREE_CODE (parm
) == REAL_TYPE
3878 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
3881 /* As far as unification is concerned, this wins. Later checks
3882 will invalidate it if necessary. */
3885 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3886 /* Type INTEGER_CST can come from ordinary constant template args. */
3888 while (TREE_CODE (arg
) == NOP_EXPR
)
3889 arg
= TREE_OPERAND (arg
, 0);
3891 if (TREE_CODE (arg
) != INTEGER_CST
)
3893 return !tree_int_cst_equal (parm
, arg
);
3898 t1
= TREE_OPERAND (parm
, 0);
3899 t2
= TREE_OPERAND (parm
, 1);
3900 return unify (tparms
, targs
, ntparms
, t1
,
3901 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
3908 if (TREE_CODE (arg
) != TREE_VEC
)
3910 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
3912 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
3913 if (unify (tparms
, targs
, ntparms
,
3914 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
3921 if (TYPE_PTRMEMFUNC_FLAG (parm
))
3922 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
3923 arg
, nsubsts
, strict
);
3925 /* Allow trivial conversions. */
3926 if (TREE_CODE (arg
) != RECORD_TYPE
3927 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
3928 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
3931 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
3934 if (flag_ansi_overloading
&& ! strict
)
3935 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
3937 (CLASSTYPE_TEMPLATE_INFO (arg
)
3938 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
3940 if (! t
|| t
== error_mark_node
)
3943 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
3944 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
3946 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
3951 if (TREE_CODE (arg
) != METHOD_TYPE
)
3956 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
3959 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3960 TREE_TYPE (arg
), nsubsts
, strict
))
3962 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
3963 TYPE_ARG_TYPES (arg
), nsubsts
, 1,
3967 if (TREE_CODE (arg
) != OFFSET_TYPE
)
3969 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
3970 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
3972 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3973 TREE_TYPE (arg
), nsubsts
, strict
);
3976 if (arg
!= decl_constant_value (parm
))
3981 sorry ("use of `%s' in template type unification",
3982 tree_code_name
[(int) TREE_CODE (parm
)]);
3988 mark_decl_instantiated (result
, extern_p
)
3992 if (DECL_TEMPLATE_INSTANTIATION (result
))
3993 SET_DECL_EXPLICIT_INSTANTIATION (result
);
3994 TREE_PUBLIC (result
) = 1;
3998 DECL_INTERFACE_KNOWN (result
) = 1;
3999 DECL_NOT_REALLY_EXTERN (result
) = 1;
4001 /* For WIN32 we also want to put explicit instantiations in
4002 linkonce sections. */
4003 if (supports_one_only () && ! SUPPORTS_WEAK
)
4004 comdat_linkage (result
);
4006 else if (TREE_CODE (result
) == FUNCTION_DECL
)
4007 mark_inline_for_output (result
);
4010 /* Given two function templates PAT1 and PAT2, return:
4012 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
4013 -1 if PAT2 is more specialized than PAT1.
4014 0 if neither is more specialized. */
4017 more_specialized (pat1
, pat2
)
4023 targs
= get_bindings (pat1
, pat2
);
4029 targs
= get_bindings (pat2
, pat1
);
4038 /* Given two class template specialization list nodes PAT1 and PAT2, return:
4040 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4041 -1 if PAT2 is more specialized than PAT1.
4042 0 if neither is more specialized. */
4045 more_specialized_class (pat1
, pat2
)
4051 targs
= get_class_bindings
4052 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
4056 targs
= get_class_bindings
4057 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
4064 /* Return the template arguments that will produce the function signature
4065 DECL from the function template FN. */
4068 get_bindings (fn
, decl
)
4071 int ntparms
= DECL_NTPARMS (fn
);
4072 tree targs
= make_scratch_vec (ntparms
);
4075 i
= fn_type_unification (fn
, NULL_TREE
, targs
,
4076 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
4077 TREE_TYPE (TREE_TYPE (decl
)),
4086 get_class_bindings (tparms
, parms
, args
)
4087 tree tparms
, parms
, args
;
4089 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
4090 tree vec
= make_temp_vec (ntparms
);
4092 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
4094 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
4095 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
4105 for (i
= 0; i
< ntparms
; ++i
)
4106 if (! TREE_VEC_ELT (vec
, i
))
4112 /* Return the most specialized of the list of templates in FNS that can
4113 produce an instantiation matching DECL. */
4116 most_specialized (fns
, decl
)
4119 tree fn
, champ
, args
, *p
;
4122 for (p
= &fns
; *p
; )
4124 args
= get_bindings (TREE_VALUE (*p
), decl
);
4127 p
= &TREE_CHAIN (*p
);
4130 *p
= TREE_CHAIN (*p
);
4137 champ
= TREE_VALUE (fn
);
4138 fn
= TREE_CHAIN (fn
);
4139 for (; fn
; fn
= TREE_CHAIN (fn
))
4141 fate
= more_specialized (champ
, TREE_VALUE (fn
));
4148 fn
= TREE_CHAIN (fn
);
4150 return error_mark_node
;
4152 champ
= TREE_VALUE (fn
);
4156 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
4158 fate
= more_specialized (champ
, TREE_VALUE (fn
));
4160 return error_mark_node
;
4166 /* Return the most specialized of the class template specializations in
4167 SPECS that can produce an instantiation matching ARGS. */
4170 most_specialized_class (specs
, mainargs
)
4171 tree specs
, mainargs
;
4173 tree list
= NULL_TREE
, t
, args
, champ
;
4176 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
4178 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
4181 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
4182 TREE_TYPE (list
) = TREE_TYPE (t
);
4192 for (; t
; t
= TREE_CHAIN (t
))
4194 fate
= more_specialized_class (champ
, t
);
4203 return error_mark_node
;
4209 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
4211 fate
= more_specialized_class (champ
, t
);
4213 return error_mark_node
;
4219 /* called from the parser. */
4222 do_decl_instantiation (declspecs
, declarator
, storage
)
4223 tree declspecs
, declarator
, storage
;
4225 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
4228 tree result
= NULL_TREE
;
4230 tree templates
= NULL_TREE
;
4232 if (! DECL_LANG_SPECIFIC (decl
))
4234 cp_error ("explicit instantiation of non-template `%#D'", decl
);
4238 /* If we've already seen this template instance, use it. */
4239 if (TREE_CODE (decl
) == VAR_DECL
)
4241 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
4242 if (result
&& TREE_CODE (result
) != VAR_DECL
)
4245 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
4247 cp_error ("explicit instantiation of `%#D'", decl
);
4250 else if (DECL_FUNCTION_MEMBER_P (decl
))
4252 if (DECL_TEMPLATE_INSTANTIATION (decl
))
4254 else if (name
= DECL_ASSEMBLER_NAME (decl
),
4255 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
4256 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
4260 /* Maybe this is an instantiation of a member template
4262 tree ctype
= DECL_CONTEXT (decl
);
4264 name
= DECL_NAME (decl
);
4265 fn
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
4267 fn
= TREE_VALUE (fn
);
4269 for (; fn
; fn
= DECL_CHAIN (fn
))
4270 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4271 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
4274 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
4276 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
4277 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4278 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
4281 if (templates
&& !result
)
4284 result
= most_specialized (templates
, decl
);
4285 if (result
== error_mark_node
)
4287 char *str
= "candidates are:";
4288 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
4289 for (fn
= templates
; fn
; fn
= TREE_CHAIN (fn
))
4291 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
4298 args
= get_bindings (result
, decl
);
4299 result
= instantiate_template (result
, args
);
4305 cp_error ("no matching template for `%D' found", decl
);
4309 if (! DECL_TEMPLATE_INFO (result
))
4311 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
4315 if (flag_external_templates
)
4318 if (storage
== NULL_TREE
)
4320 else if (storage
== ridpointers
[(int) RID_EXTERN
])
4323 cp_error ("storage class `%D' applied to template instantiation",
4326 mark_decl_instantiated (result
, extern_p
);
4327 repo_template_instantiated (result
, extern_p
);
4329 instantiate_decl (result
);
4333 mark_class_instantiated (t
, extern_p
)
4337 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
4338 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
4339 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
4340 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
4341 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
4344 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
4345 rest_of_type_compilation (t
, 1);
4350 do_type_instantiation (t
, storage
)
4357 if (TREE_CODE (t
) == TYPE_DECL
)
4360 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
4362 cp_error ("explicit instantiation of non-template type `%T'", t
);
4368 /* With -fexternal-templates, explicit instantiations are treated the same
4369 as implicit ones. */
4370 if (flag_external_templates
)
4373 if (TYPE_SIZE (t
) == NULL_TREE
)
4375 cp_error ("explicit instantiation of `%#T' before definition of template",
4380 if (storage
== NULL_TREE
)
4382 else if (storage
== ridpointers
[(int) RID_INLINE
])
4384 else if (storage
== ridpointers
[(int) RID_EXTERN
])
4386 else if (storage
== ridpointers
[(int) RID_STATIC
])
4390 cp_error ("storage class `%D' applied to template instantiation",
4395 /* We've already instantiated this. */
4396 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
4400 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
4402 mark_class_instantiated (t
, extern_p
);
4403 repo_template_instantiated (t
, extern_p
);
4413 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
4414 if (TREE_CODE (tmp
) == FUNCTION_DECL
4415 && DECL_TEMPLATE_INSTANTIATION (tmp
))
4417 mark_decl_instantiated (tmp
, extern_p
);
4418 repo_template_instantiated (tmp
, extern_p
);
4420 instantiate_decl (tmp
);
4423 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
4424 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
4426 mark_decl_instantiated (tmp
, extern_p
);
4427 repo_template_instantiated (tmp
, extern_p
);
4429 instantiate_decl (tmp
);
4432 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
4433 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
4434 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
4439 instantiate_decl (d
)
4442 tree ti
= DECL_TEMPLATE_INFO (d
);
4443 tree tmpl
= TI_TEMPLATE (ti
);
4444 tree args
= TI_ARGS (ti
);
4446 tree decl_pattern
, code_pattern
;
4448 int nested
= in_function_p ();
4450 int pattern_defined
;
4452 char *file
= input_filename
;
4454 for (td
= tmpl
; DECL_TEMPLATE_INSTANTIATION (td
); )
4455 td
= DECL_TI_TEMPLATE (td
);
4457 /* In the case of a member template, decl_pattern is the partially
4458 instantiated declaration (in the instantiated class), and code_pattern
4459 is the original template definition. */
4460 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
4461 code_pattern
= DECL_TEMPLATE_RESULT (td
);
4463 if (TREE_CODE (d
) == FUNCTION_DECL
)
4465 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
4466 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
4470 d_defined
= ! DECL_IN_AGGR_P (d
);
4471 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
4477 if (TREE_CODE (d
) == FUNCTION_DECL
)
4481 /* Check to see if there is a matching specialization. */
4482 for (specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
4484 specs
= TREE_CHAIN (specs
))
4485 if (comp_template_args (TREE_PURPOSE (specs
), args
))
4486 return TREE_VALUE (specs
);
4489 /* This needs to happen before any tsubsting. */
4490 if (! push_tinst_level (d
))
4493 push_to_top_level ();
4494 lineno
= DECL_SOURCE_LINE (d
);
4495 input_filename
= DECL_SOURCE_FILE (d
);
4497 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
4498 variable is a static const initialized in the class body. */
4499 if (TREE_CODE (d
) == VAR_DECL
4500 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
4502 pushclass (DECL_CONTEXT (d
), 2);
4503 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
4504 TREE_VEC_LENGTH (args
), tmpl
);
4508 if (pattern_defined
)
4510 repo_template_used (d
);
4512 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
4514 if (flag_alt_external_templates
)
4516 if (interface_unknown
)
4517 warn_if_unknown_interface (d
);
4519 else if (DECL_INTERFACE_KNOWN (code_pattern
))
4521 DECL_INTERFACE_KNOWN (d
) = 1;
4522 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
4525 warn_if_unknown_interface (code_pattern
);
4529 import_export_decl (d
);
4532 if (! pattern_defined
4533 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! DECL_INLINE (d
)
4534 && (! DECL_INTERFACE_KNOWN (d
)
4535 || ! DECL_NOT_REALLY_EXTERN (d
)))
4536 /* Kludge: if we compile a constructor in the middle of processing a
4537 toplevel declaration, we blow away the declspecs in
4538 temp_decl_obstack when we call permanent_allocation in
4539 finish_function. So don't compile it yet. */
4540 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! nested
&& ! at_eof
))
4542 add_pending_template (d
);
4546 lineno
= DECL_SOURCE_LINE (d
);
4547 input_filename
= DECL_SOURCE_FILE (d
);
4549 /* Trick tsubst into giving us a new decl in case the template changed. */
4550 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
4551 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
4552 td
= tsubst (decl_pattern
, args
, TREE_VEC_LENGTH (args
), tmpl
);
4553 SET_DECL_IMPLICIT_INSTANTIATION (td
);
4554 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
4556 /* And set up DECL_INITIAL, since tsubst doesn't. */
4557 if (TREE_CODE (td
) == VAR_DECL
)
4559 pushclass (DECL_CONTEXT (d
), 2);
4560 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
4561 TREE_VEC_LENGTH (args
), tmpl
);
4565 if (TREE_CODE (d
) == FUNCTION_DECL
)
4567 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4569 DECL_INITIAL (td
) = error_mark_node
;
4571 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
4572 /* Set up the information about what is being specialized. */
4573 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
4575 duplicate_decls (td
, d
);
4576 if (TREE_CODE (d
) == FUNCTION_DECL
)
4577 DECL_INITIAL (td
) = 0;
4579 if (TREE_CODE (d
) == VAR_DECL
)
4581 DECL_IN_AGGR_P (d
) = 0;
4582 if (DECL_INTERFACE_KNOWN (d
))
4583 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
4586 DECL_EXTERNAL (d
) = 1;
4587 DECL_NOT_REALLY_EXTERN (d
) = 1;
4589 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
4591 else if (TREE_CODE (d
) == FUNCTION_DECL
)
4593 tree t
= DECL_SAVED_TREE (code_pattern
);
4595 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
4596 store_parm_decls ();
4598 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
4601 (TREE_OPERAND (t
, 0),
4602 tsubst_expr (TREE_OPERAND (t
, 1), args
,
4603 TREE_VEC_LENGTH (args
), tmpl
));
4607 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
4609 current_member_init_list
4610 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
4611 current_base_init_list
4612 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
4617 /* Always keep the BLOCK node associated with the outermost
4618 pair of curley braces of a function. These are needed
4619 for correct operation of dwarfout.c. */
4622 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
4623 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
4625 finish_function (lineno
, 0, nested
);
4630 input_filename
= file
;
4632 pop_from_top_level ();
4639 tsubst_chain (t
, argvec
)
4644 tree first
= tsubst (t
, argvec
,
4645 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4648 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
4650 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4651 TREE_CHAIN (last
) = x
;
4661 tsubst_expr_values (t
, argvec
)
4664 tree first
= NULL_TREE
;
4667 for (; t
; t
= TREE_CHAIN (t
))
4669 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
4670 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4671 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
4672 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4673 *p
= build_tree_list (pur
, val
);
4674 p
= &TREE_CHAIN (*p
);
4685 last_tree
= TREE_CHAIN (last_tree
) = t
;
4688 /* D is an undefined function declaration in the presence of templates with
4689 the same name, listed in FNS. If one of them can produce D as an
4690 instantiation, remember this so we can instantiate it at EOF if D has
4691 not been defined by that time. */
4694 add_maybe_template (d
, fns
)
4699 if (DECL_MAYBE_TEMPLATE (d
))
4702 t
= most_specialized (fns
, d
);
4705 if (t
== error_mark_node
)
4707 cp_error ("ambiguous template instantiation for `%D'", d
);
4711 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
4712 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
4713 DECL_MAYBE_TEMPLATE (d
) = 1;
4716 /* Instantiate an enumerated type. Used by instantiate_class_template and
4720 tsubst_enum (tag
, args
, nargs
, field_chain
)
4725 extern tree current_local_enum
;
4726 tree prev_local_enum
= current_local_enum
;
4728 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
4729 tree e
, values
= NULL_TREE
;
4731 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
4733 tree elt
= build_enumerator (TREE_PURPOSE (e
),
4734 tsubst_expr (TREE_VALUE (e
), args
,
4736 TREE_CHAIN (elt
) = values
;
4740 finish_enum (newtag
, values
);
4742 if (NULL
!= field_chain
)
4743 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
4745 current_local_enum
= prev_local_enum
;