1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
46 extern struct obstack permanent_obstack
;
49 extern char *input_filename
;
50 struct pending_inline
*pending_template_expansions
;
52 tree current_template_parms
;
53 HOST_WIDE_INT processing_template_decl
;
55 tree pending_templates
;
56 static tree
*template_tail
= &pending_templates
;
59 static tree
*maybe_template_tail
= &maybe_templates
;
61 int minimal_parse_mode
;
63 int processing_specialization
;
64 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 function 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 preceded 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 <...> preceding the declaration.
312 o The last template <...> was in fact template <>.
313 o The number of template <...>'s preceding 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
;
355 tree name
= NULL_TREE
;
359 my_friendly_assert (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
,
362 fns
= TREE_OPERAND (template_id
, 0);
364 overloaded
= fns
!= NULL_TREE
&& really_overloaded_fn (fns
);
366 for (fn
= (fns
!= NULL_TREE
) ? get_first_fn (fns
) : NULL_TREE
;
368 fn
= overloaded
? DECL_CHAIN (fn
) : NULL_TREE
)
373 if (name
== NULL_TREE
)
374 name
= DECL_NAME (fn
);
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 ("Specialization of `%s' does not match any template declaration.",
428 IDENTIFIER_POINTER (name
));
429 *targs_out
= NULL_TREE
;
433 if (TREE_CHAIN (matching_fns
) != NULL_TREE
)
439 cp_error ("Ambiguous explicit specialization. Candidates are:");
440 for (fn
= matching_fns
; fn
!= NULL_TREE
; fn
= TREE_CHAIN (fn
))
441 cp_error (" %D", TREE_PURPOSE (fn
));
444 *targs_out
= NULL_TREE
;
448 /* We have one, and exactly one, match. */
449 *targs_out
= TREE_VALUE (matching_fns
);
450 return TREE_PURPOSE (matching_fns
);
454 /* Check to see if the function just declared, as indicated in
455 DECLARATOR, and in DECL, is a specialization. Check that the
456 specialization is OK. If FLAGS == 1, we are being called by
457 finish_struct_methods. If FLAGS == 2, we are being called by
458 grokfndecl, and the function has a definition, or is a friend. If
459 FLAGS == 3, this is a friend declaration.
460 Returns 0 if the decl is not an explicit specialization or
461 instantiation, 1 if it is an explicit specialization, and 2 if it
462 is an explicit instantiation. */
465 check_explicit_specialization (declarator
, decl
, template_count
, flags
)
471 int finish_member
= flags
== 1;
472 int have_def
= flags
== 2;
473 int is_friend
= flags
== 3;
475 if (processing_explicit_specialization (template_count
)
477 || TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
)
479 tree tmpl
= NULL_TREE
;
480 tree dname
= DECL_NAME (decl
);
481 tree ctype
= DECL_CLASS_CONTEXT (decl
);
484 /* We've come across a declarator that looks like: U f<T1,
485 T2, ...>(A1, A2, ..). This is an explicit template
486 specialization. Check that:
488 o The explicitly specified parameters together with those
489 that can be deduced by template argument deduction
490 uniquely determine a particular specialization.
492 See [temp.expl.spec]. */
495 && TREE_CODE (declarator
) == TEMPLATE_ID_EXPR
496 && !processing_explicit_specialization (template_count
)
500 /* This is not an explicit specialization. It must be
501 an explicit instantiation. */
504 pedwarn ("Explicit specialization not preceded by `template <>'");
507 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
511 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
514 fns
= IDENTIFIER_GLOBAL_VALUE (dname
);
518 declarator
= lookup_template_function (fns
, NULL_TREE
);
521 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
523 /* A friend declaration. We can't do much, because we don't
524 know what this resolves to, yet. */
525 my_friendly_assert (is_friend
!= 0, 0);
526 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
531 && TREE_CODE (TREE_OPERAND (declarator
, 0)) == IDENTIFIER_NODE
)
535 if (TYPE_BEING_DEFINED (ctype
) && !finish_member
)
537 /* Since finish_struct_1 has not been called yet, we
538 can't call lookup_fnfields. We note that this
539 template is a specialization, and proceed, letting
540 finish_struct_methods fix this up later. */
541 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
542 DECL_TEMPLATE_INFO (decl
)
543 = perm_tree_cons (NULL_TREE
,
544 TREE_OPERAND (declarator
, 1),
549 fns
= lookup_fnfields (TYPE_BINFO (ctype
),
550 TREE_OPERAND (declarator
, 0),
553 if (fns
== NULL_TREE
)
555 cp_error ("No member template `%s' declared in `%T'",
556 IDENTIFIER_POINTER (TREE_OPERAND (declarator
,
562 TREE_OPERAND (declarator
, 0) = fns
;
566 determine_explicit_specialization
567 (declarator
, TREE_TYPE (decl
), &targs
,
568 TREE_CODE (decl
) == TEMPLATE_DECL
, 1);
572 /* Mangle the function name appropriately. */
573 if (name_mangling_version
>= 1)
575 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
578 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
580 hash_tree_chain (build_pointer_type (ctype
),
583 DECL_ASSEMBLER_NAME (decl
)
584 = build_template_decl_overload
587 TREE_TYPE (TREE_TYPE (tmpl
)),
588 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
589 targs
, ctype
!= NULL_TREE
);
592 if (is_friend
&& !have_def
)
594 /* This is not really a declaration of a specialization.
595 It's just the name of an instantiation. But, it's not
596 a request for an instantiation, either. */
597 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
598 DECL_TEMPLATE_INFO (decl
)
599 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
603 /* This function declaration is a template specialization.
605 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
606 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
607 = perm_tree_cons (targs
, decl
,
608 DECL_TEMPLATE_SPECIALIZATIONS
610 /* If DECL_TI_TEMPLATE (decl), the decl is an
611 instantiation of a specialization of a member template.
612 (In other words, there was a member template, in a
613 class template. That member template was specialized.
614 We then instantiated the class, so there is now an
615 instance of that specialization.)
617 According to the CD2,
619 14.7.3.13 [tmpl.expl.spec]
621 A specialization of a member function template or
622 member class template of a non-specialized class
623 template is itself a template.
625 So, we just leave the template info alone in this case. */
626 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
627 DECL_TEMPLATE_INFO (decl
)
628 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
636 /* Process information from new template parameter NEXT and append it to the
640 process_template_parm (list
, next
)
648 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
649 defval
= TREE_PURPOSE (parm
);
650 parm
= TREE_VALUE (parm
);
651 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
655 tree p
= TREE_VALUE (tree_last (list
));
657 if (TREE_CODE (p
) == TYPE_DECL
)
658 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
660 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
669 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
670 /* is a const-param */
671 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
673 /* A template parameter is not modifiable. */
674 TREE_READONLY (parm
) = 1;
675 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
676 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
678 cp_error ("`%#T' is not a valid type for a template constant parameter",
680 if (DECL_NAME (parm
) == NULL_TREE
)
681 error (" a template type parameter must begin with `class' or `typename'");
682 TREE_TYPE (parm
) = void_type_node
;
685 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
686 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
687 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
689 tinfo
= make_node (TEMPLATE_CONST_PARM
);
690 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
691 if (TREE_PERMANENT (parm
) == 0)
693 parm
= copy_node (parm
);
694 TREE_PERMANENT (parm
) = 1;
696 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
697 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
698 DECL_INITIAL (decl
) = tinfo
;
699 DECL_INITIAL (parm
) = tinfo
;
700 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
);
704 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
705 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
706 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
707 TYPE_NAME (t
) = decl
;
708 TYPE_STUB_DECL (t
) = decl
;
710 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
);
712 SET_DECL_ARTIFICIAL (decl
);
714 parm
= build_tree_list (defval
, parm
);
715 return chainon (list
, parm
);
718 /* The end of a template parameter list has been reached. Process the
719 tree list into a parameter vector, converting each parameter into a more
720 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
724 end_template_parm_list (parms
)
729 tree saved_parmlist
= make_tree_vec (list_length (parms
));
731 current_template_parms
732 = tree_cons (build_int_2 (0, processing_template_decl
),
733 saved_parmlist
, current_template_parms
);
735 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
736 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
738 return saved_parmlist
;
741 /* end_template_decl is called after a template declaration is seen. */
746 reset_specialization ();
748 if (! processing_template_decl
)
751 /* This matches the pushlevel in begin_template_parm_list. */
754 --processing_template_decl
;
755 current_template_parms
= TREE_CHAIN (current_template_parms
);
756 (void) get_pending_sizes (); /* Why? */
759 /* Generate a valid set of template args from current_template_parms. */
762 current_template_args ()
764 tree header
= current_template_parms
;
765 int length
= list_length (header
);
766 tree args
= make_tree_vec (length
);
771 tree a
= copy_node (TREE_VALUE (header
));
772 int i
= TREE_VEC_LENGTH (a
);
773 TREE_TYPE (a
) = NULL_TREE
;
776 tree t
= TREE_VEC_ELT (a
, i
);
778 /* t will be a list if we are called from within a
779 begin/end_template_parm_list pair, but a vector directly
780 if within a begin/end_member_template_processing pair. */
781 if (TREE_CODE (t
) == TREE_LIST
)
785 if (TREE_CODE (t
) == TYPE_DECL
)
788 t
= DECL_INITIAL (t
);
791 TREE_VEC_ELT (a
, i
) = t
;
793 TREE_VEC_ELT (args
, --l
) = a
;
794 header
= TREE_CHAIN (header
);
801 push_template_decl (decl
)
805 tree args
= NULL_TREE
;
807 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
811 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
812 && DECL_CLASS_CONTEXT (decl
))
814 /* Note that this template is a "primary template" */
815 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
816 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
819 /* Partial specialization. */
820 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
821 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
823 tree type
= TREE_TYPE (decl
);
824 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
825 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
826 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
828 for (; spec
; spec
= TREE_CHAIN (spec
))
830 /* purpose: args to main template
831 value: spec template */
832 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
836 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
837 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
838 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
839 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
843 args
= current_template_args ();
845 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
847 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
848 DECL_TEMPLATE_PARMS (tmpl
) = current_template_parms
;
849 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
850 if (DECL_LANG_SPECIFIC (decl
))
852 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
853 DECL_STATIC_FUNCTION_P (tmpl
) =
854 DECL_STATIC_FUNCTION_P (decl
);
856 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
858 /* A specialization of a member template of a template
860 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
861 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
862 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
871 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
872 cp_error ("must specialize `%#T' before defining member `%#D'",
874 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
875 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
876 else if (! DECL_TEMPLATE_INFO (decl
))
878 cp_error ("template definition of non-template `%#D'", decl
);
882 tmpl
= DECL_TI_TEMPLATE (decl
);
884 if (is_member_template (tmpl
))
886 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
887 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
888 if (TREE_VEC_LENGTH (t
)
889 != TREE_VEC_LENGTH (a
))
891 cp_error ("got %d template parameters for `%#D'",
892 TREE_VEC_LENGTH (a
), decl
);
893 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
895 if (TREE_VEC_LENGTH (args
) > 1)
896 /* Get the template parameters for the enclosing template
898 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
903 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
907 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
909 /* When processing an inline member template of a
910 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
911 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
912 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
914 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
915 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
917 /* There should be template arguments if and only if there is a
919 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
922 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
924 cp_error ("got %d template parameters for `%#D'",
925 TREE_VEC_LENGTH (a
), decl
);
926 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
929 /* Get the innermost set of template arguments. */
930 args
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
932 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
933 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
936 tmpl
= pushdecl_top_level (tmpl
);
939 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
941 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
943 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
945 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
946 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
948 else if (! DECL_LANG_SPECIFIC (decl
))
949 cp_error ("template declaration of `%#D'", decl
);
951 DECL_TEMPLATE_INFO (decl
) = info
;
954 /* Convert all template arguments to their appropriate types, and return
955 a vector containing the resulting values. If any error occurs, return
959 coerce_template_parms (parms
, arglist
, in_decl
)
963 int nparms
, nargs
, i
, lost
= 0;
966 if (arglist
== NULL_TREE
)
968 else if (TREE_CODE (arglist
) == TREE_VEC
)
969 nargs
= TREE_VEC_LENGTH (arglist
);
971 nargs
= list_length (arglist
);
973 nparms
= TREE_VEC_LENGTH (parms
);
977 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
979 error ("incorrect number of parameters (%d, should be %d)",
982 cp_error_at ("in template expansion for decl `%D'", in_decl
);
983 return error_mark_node
;
986 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
987 vec
= copy_node (arglist
);
990 vec
= make_tree_vec (nparms
);
991 for (i
= 0; i
< nparms
; i
++)
998 arglist
= TREE_CHAIN (arglist
);
1000 if (arg
== error_mark_node
)
1003 arg
= TREE_VALUE (arg
);
1005 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
1007 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1010 arg
= tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1013 TREE_VEC_ELT (vec
, i
) = arg
;
1016 for (i
= 0; i
< nparms
; i
++)
1018 tree arg
= TREE_VEC_ELT (vec
, i
);
1019 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1021 int is_type
, requires_type
;
1023 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
1024 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
1026 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
1027 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
1029 cp_pedwarn ("to refer to a type member of a template parameter,");
1030 cp_pedwarn (" use `typename %E'", arg
);
1031 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
1032 TREE_OPERAND (arg
, 1));
1035 if (is_type
!= requires_type
)
1039 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1042 cp_error (" expected a constant of type `%T', got `%T'",
1043 TREE_TYPE (parm
), arg
);
1045 cp_error (" expected a type, got `%E'", arg
);
1048 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1053 val
= groktypename (arg
);
1054 if (! processing_template_decl
)
1056 tree t
= target_type (val
);
1057 if (TREE_CODE (t
) != TYPENAME_TYPE
1059 && decl_function_context (TYPE_MAIN_DECL (t
)))
1061 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
1062 return error_mark_node
;
1068 tree t
= tsubst (TREE_TYPE (parm
), vec
,
1069 TREE_VEC_LENGTH (vec
), in_decl
);
1070 if (processing_template_decl
)
1073 val
= digest_init (t
, arg
, (tree
*) 0);
1075 if (val
== error_mark_node
|| processing_template_decl
)
1078 /* 14.2: Other template-arguments must be constant-expressions,
1079 addresses of objects or functions with external linkage, or of
1080 static class members. */
1081 else if (IS_AGGR_TYPE (TREE_TYPE (val
)))
1083 cp_error ("object `%E' cannot be used as template argument", arg
);
1084 val
= error_mark_node
;
1086 else if (!TREE_CONSTANT (val
))
1088 cp_error ("non-constant `%E' cannot be used as template argument",
1090 val
= error_mark_node
;
1092 else if (POINTER_TYPE_P (TREE_TYPE (val
))
1093 && ! integer_zerop (val
)
1094 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
1095 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
1099 if (TREE_CODE (t
) == ADDR_EXPR
)
1101 tree a
= TREE_OPERAND (t
, 0);
1103 if (TREE_CODE (a
) == STRING_CST
)
1105 cp_error ("string literal %E is not a valid template argument", a
);
1106 error ("because it is the address of an object with static linkage");
1107 val
= error_mark_node
;
1109 else if (TREE_CODE (a
) != VAR_DECL
1110 && TREE_CODE (a
) != FUNCTION_DECL
)
1112 else if (! TREE_PUBLIC (a
))
1114 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
1115 val
= error_mark_node
;
1121 cp_error ("`%E' is not a valid template argument", t
);
1122 error ("it must be %s%s with external linkage",
1123 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
1124 ? "a pointer to " : "",
1125 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
1126 ? "a function" : "an object");
1127 val
= error_mark_node
;
1132 if (val
== error_mark_node
)
1135 TREE_VEC_ELT (vec
, i
) = val
;
1138 return error_mark_node
;
1143 comp_template_args (oldargs
, newargs
)
1144 tree oldargs
, newargs
;
1148 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
1151 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
1153 tree nt
= TREE_VEC_ELT (newargs
, i
);
1154 tree ot
= TREE_VEC_ELT (oldargs
, i
);
1158 if (TREE_CODE (nt
) != TREE_CODE (ot
))
1160 if (TREE_CODE (nt
) == TREE_VEC
)
1162 /* For member templates */
1163 if (comp_template_args (nt
, ot
))
1166 else if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
1168 if (comptypes (ot
, nt
, 1))
1171 else if (cp_tree_equal (ot
, nt
) > 0)
1178 /* Given class template name and parameter list, produce a user-friendly name
1179 for the instantiation. */
1182 mangle_class_name_for_template (name
, parms
, arglist
)
1184 tree parms
, arglist
;
1186 static struct obstack scratch_obstack
;
1187 static char *scratch_firstobj
;
1190 if (!scratch_firstobj
)
1191 gcc_obstack_init (&scratch_obstack
);
1193 obstack_free (&scratch_obstack
, scratch_firstobj
);
1194 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
1197 #define buflen sizeof(buf)
1198 #define check if (bufp >= buf+buflen-1) goto too_long
1199 #define ccat(c) *bufp++=(c); check
1200 #define advance bufp+=strlen(bufp); check
1201 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1204 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1206 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1211 nparms
= TREE_VEC_LENGTH (parms
);
1212 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
1213 for (i
= 0; i
< nparms
; i
++)
1215 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1216 tree arg
= TREE_VEC_ELT (arglist
, i
);
1221 if (TREE_CODE (parm
) == TYPE_DECL
)
1223 cat (type_as_string (arg
, 0));
1227 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
1229 if (TREE_CODE (arg
) == TREE_LIST
)
1231 /* New list cell was built because old chain link was in
1233 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
1234 arg
= TREE_VALUE (arg
);
1236 /* No need to check arglist against parmlist here; we did that
1237 in coerce_template_parms, called from lookup_template_class. */
1238 cat (expr_as_string (arg
, 0));
1241 char *bufp
= obstack_next_free (&scratch_obstack
);
1243 while (bufp
[offset
- 1] == ' ')
1245 obstack_blank_fast (&scratch_obstack
, offset
);
1247 /* B<C<char> >, not B<C<char>> */
1248 if (bufp
[offset
- 1] == '>')
1253 return (char *) obstack_base (&scratch_obstack
);
1258 fatal ("out of (preallocated) string space creating template instantiation name");
1264 classtype_mangled_name (t
)
1267 if (CLASSTYPE_TEMPLATE_INFO (t
)
1268 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
1270 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
1271 char *mangled_name
= mangle_class_name_for_template
1272 (IDENTIFIER_POINTER (name
),
1273 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
1274 CLASSTYPE_TI_ARGS (t
));
1275 tree id
= get_identifier (mangled_name
);
1276 IDENTIFIER_TEMPLATE (id
) = name
;
1280 return TYPE_IDENTIFIER (t
);
1284 add_pending_template (d
)
1289 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
1290 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
1292 ti
= DECL_TEMPLATE_INFO (d
);
1294 if (TI_PENDING_TEMPLATE_FLAG (ti
))
1297 *template_tail
= perm_tree_cons
1298 (current_function_decl
, d
, NULL_TREE
);
1299 template_tail
= &TREE_CHAIN (*template_tail
);
1300 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
1304 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1305 may be either a _DECL or an overloaded function or an
1306 IDENTIFIER_NODE), and ARGLIST. */
1309 lookup_template_function (fns
, arglist
)
1312 if (fns
== NULL_TREE
)
1314 cp_error ("non-template used as template");
1315 return error_mark_node
;
1318 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
1320 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1321 arglist
= copy_list (arglist
);
1325 return build_min (TEMPLATE_ID_EXPR
,
1327 ? TREE_TYPE (fns
) : unknown_type_node
,
1332 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1333 parameters, find the desired type.
1335 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1336 Since ARGLIST is build on the decl_obstack, we must copy it here
1337 to keep it from being reclaimed when the decl storage is reclaimed.
1339 IN_DECL, if non-NULL, is the template declaration we are trying to
1343 lookup_template_class (d1
, arglist
, in_decl
)
1347 tree
template, parmlist
;
1351 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
1353 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
1355 template = IDENTIFIER_CLASS_VALUE (d1
);
1357 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
1359 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
1360 d1
= DECL_NAME (template);
1362 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
1364 template = CLASSTYPE_TI_TEMPLATE (d1
);
1365 d1
= DECL_NAME (template);
1368 my_friendly_abort (272);
1370 /* With something like `template <class T> class X class X { ... };'
1371 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1372 We don't want to do that, but we have to deal with the situation, so
1373 let's give them some syntax errors to chew on instead of a crash. */
1375 return error_mark_node
;
1376 if (TREE_CODE (template) != TEMPLATE_DECL
)
1378 cp_error ("non-template type `%T' used as a template", d1
);
1380 cp_error_at ("for template declaration `%D'", in_decl
);
1381 return error_mark_node
;
1384 if (PRIMARY_TEMPLATE_P (template))
1386 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
1388 arglist
= coerce_template_parms (parmlist
, arglist
, template);
1389 if (arglist
== error_mark_node
)
1390 return error_mark_node
;
1391 if (uses_template_parms (arglist
))
1394 if (comp_template_args
1395 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
1396 found
= TREE_TYPE (template);
1399 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
1400 found
; found
= TREE_CHAIN (found
))
1402 if (TI_USES_TEMPLATE_PARMS (found
)
1403 && comp_template_args (TREE_PURPOSE (found
), arglist
))
1407 found
= TREE_VALUE (found
);
1412 if (can_free (&permanent_obstack
, arglist
))
1413 obstack_free (&permanent_obstack
, arglist
);
1418 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
1420 id
= get_identifier (mangled_name
);
1421 IDENTIFIER_TEMPLATE (id
) = d1
;
1423 maybe_push_to_top_level (uses_template_parms (arglist
));
1424 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
1425 pop_from_top_level ();
1429 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1432 arglist
= CLASSTYPE_TI_ARGS (ctx
);
1434 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
1436 int save_temp
= processing_template_decl
;
1437 processing_template_decl
= 0;
1438 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
1439 processing_template_decl
= save_temp
;
1443 t
= lookup_nested_type_by_name (ctx
, id
);
1444 my_friendly_assert (t
!= NULL_TREE
, 42);
1448 /* Seems to be wanted. */
1449 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1451 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1453 arglist
= copy_to_permanent (arglist
);
1454 CLASSTYPE_TEMPLATE_INFO (t
)
1455 = perm_tree_cons (template, arglist
, NULL_TREE
);
1456 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1457 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
1458 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1459 = uses_template_parms (arglist
);
1461 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
1463 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1464 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
1465 /* if (! uses_template_parms (arglist)) */
1466 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
1467 = get_identifier (build_overload_name (t
, 1, 1));
1469 if (flag_external_templates
&& ! uses_template_parms (arglist
)
1470 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1471 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1472 add_pending_template (t
);
1478 /* Should be defined in parse.h. */
1482 uses_template_parms (t
)
1487 switch (TREE_CODE (t
))
1491 /* We assume that the object must be instantiated in order to build
1492 the COMPONENT_REF, so we test only whether the type of the
1493 COMPONENT_REF uses template parms. */
1494 return uses_template_parms (TREE_TYPE (t
));
1496 case IDENTIFIER_NODE
:
1497 if (!IDENTIFIER_TEMPLATE (t
))
1499 my_friendly_abort (42);
1501 /* aggregates of tree nodes */
1504 int i
= TREE_VEC_LENGTH (t
);
1506 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
1511 if (uses_template_parms (TREE_PURPOSE (t
))
1512 || uses_template_parms (TREE_VALUE (t
)))
1514 return uses_template_parms (TREE_CHAIN (t
));
1516 /* constructed type nodes */
1518 case REFERENCE_TYPE
:
1519 return uses_template_parms (TREE_TYPE (t
));
1521 if (TYPE_PTRMEMFUNC_FLAG (t
))
1522 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1524 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1526 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
1528 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1530 return uses_template_parms (TREE_TYPE (t
));
1532 if (uses_template_parms (TYPE_DOMAIN (t
)))
1534 return uses_template_parms (TREE_TYPE (t
));
1536 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
1538 return uses_template_parms (TREE_TYPE (t
));
1540 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
1542 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1544 return uses_template_parms (TREE_TYPE (t
));
1548 return uses_template_parms (TREE_TYPE (t
));
1552 /* ??? What about FIELD_DECLs? */
1553 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
1554 && uses_template_parms (DECL_TI_ARGS (t
)))
1559 if (uses_template_parms (TREE_TYPE (t
)))
1561 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
1566 return uses_template_parms (TREE_TYPE (t
));
1568 return uses_template_parms (TREE_OPERAND (t
, 0));
1570 /* template parm nodes */
1571 case TEMPLATE_TYPE_PARM
:
1572 case TEMPLATE_CONST_PARM
:
1575 /* simple type nodes */
1577 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
1579 return uses_template_parms (TYPE_MAX_VALUE (t
));
1591 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
1592 if (uses_template_parms (TREE_VALUE (v
)))
1604 /* Non-error_mark_node ERROR_MARKs are bad things. */
1605 my_friendly_assert (t
== error_mark_node
, 274);
1614 return uses_template_parms (TREE_OPERAND (t
, 0));
1617 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
1618 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
1619 return uses_template_parms (TREE_OPERAND (t
, 1));
1623 case REINTERPRET_CAST_EXPR
:
1624 case CONST_CAST_EXPR
:
1625 case STATIC_CAST_EXPR
:
1626 case DYNAMIC_CAST_EXPR
:
1634 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
1642 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
1643 if (uses_template_parms (TREE_OPERAND (t
, i
)))
1650 sorry ("testing %s for template parms",
1651 tree_code_name
[(int) TREE_CODE (t
)]);
1652 my_friendly_abort (82);
1658 static struct tinst_level
*current_tinst_level
= 0;
1659 static struct tinst_level
*free_tinst_level
= 0;
1660 static int tinst_depth
= 0;
1661 extern int max_tinst_depth
;
1662 #ifdef GATHER_STATISTICS
1663 int depth_reached
= 0;
1667 push_tinst_level (d
)
1670 struct tinst_level
*new;
1672 if (tinst_depth
>= max_tinst_depth
)
1674 struct tinst_level
*p
= current_tinst_level
;
1676 char *file
= input_filename
;
1678 error ("template instantiation depth exceeds maximum of %d",
1680 error (" (use -ftemplate-depth-NN to increase the maximum)");
1681 cp_error (" instantiating `%D'", d
);
1683 for (; p
; p
= p
->next
)
1685 cp_error (" instantiated from `%D'", p
->decl
);
1687 input_filename
= p
->file
;
1689 error (" instantiated from here");
1692 input_filename
= file
;
1697 if (free_tinst_level
)
1699 new = free_tinst_level
;
1700 free_tinst_level
= new->next
;
1703 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
1707 new->file
= input_filename
;
1708 new->next
= current_tinst_level
;
1709 current_tinst_level
= new;
1712 #ifdef GATHER_STATISTICS
1713 if (tinst_depth
> depth_reached
)
1714 depth_reached
= tinst_depth
;
1723 struct tinst_level
*old
= current_tinst_level
;
1725 current_tinst_level
= old
->next
;
1726 old
->next
= free_tinst_level
;
1727 free_tinst_level
= old
;
1731 struct tinst_level
*
1734 struct tinst_level
*p
= current_tinst_level
;
1737 for (; p
->next
; p
= p
->next
)
1743 instantiate_class_template (type
)
1746 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
1748 if (type
== error_mark_node
)
1749 return error_mark_node
;
1751 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
1753 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
1756 template = TI_TEMPLATE (template_info
);
1757 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
1758 args
= TI_ARGS (template_info
);
1760 t
= most_specialized_class
1761 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
1763 if (t
== error_mark_node
)
1765 char *str
= "candidates are:";
1766 cp_error ("ambiguous class template instantiation for `%#T'", type
);
1767 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
1769 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
1771 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
1775 TYPE_BEING_DEFINED (type
) = 1;
1776 return error_mark_node
;
1779 pattern
= TREE_TYPE (t
);
1781 pattern
= TREE_TYPE (template);
1783 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1787 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
1789 TYPE_BEING_DEFINED (type
) = 1;
1791 if (! push_tinst_level (type
))
1794 maybe_push_to_top_level (uses_template_parms (type
));
1795 pushclass (type
, 0);
1797 if (flag_external_templates
)
1799 if (flag_alt_external_templates
)
1801 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1802 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1803 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1804 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1805 && CLASSTYPE_INTERFACE_KNOWN (type
);
1809 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1810 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1811 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1812 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1813 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1814 && CLASSTYPE_INTERFACE_KNOWN (type
);
1819 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1820 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1823 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1824 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1825 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1826 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1827 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1828 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1829 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1830 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1831 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1832 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1833 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1834 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1835 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1836 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1837 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1838 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1839 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1840 TYPE_USES_COMPLEX_INHERITANCE (type
)
1841 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1842 TYPE_USES_MULTIPLE_INHERITANCE (type
)
1843 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
1844 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1845 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1846 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
1847 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
1850 tree binfo
= TYPE_BINFO (type
);
1851 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1857 int len
= TREE_VEC_LENGTH (pbases
);
1858 bases
= make_tree_vec (len
);
1859 for (i
= 0; i
< len
; ++i
)
1863 TREE_VEC_ELT (bases
, i
) = elt
1864 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
1865 TREE_VEC_LENGTH (args
), NULL_TREE
);
1866 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1868 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
1870 ("base type `%T' of `%T' fails to be a struct or class type",
1871 TREE_TYPE (elt
), type
);
1872 else if (! uses_template_parms (type
)
1873 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
1875 cp_error ("base class `%T' of `%T' has incomplete type",
1876 TREE_TYPE (elt
), type
);
1878 /* Don't initialize this until the vector is filled out, or
1879 lookups will crash. */
1880 BINFO_BASETYPES (binfo
) = bases
;
1884 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1886 field_chain
= &TYPE_FIELDS (type
);
1888 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1890 tree name
= TREE_PURPOSE (t
);
1891 tree tag
= TREE_VALUE (t
);
1893 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1894 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1896 tree e
, newtag
= tsubst_enum (tag
, args
,
1897 TREE_VEC_LENGTH (args
), field_chain
);
1899 while (*field_chain
)
1901 DECL_FIELD_CONTEXT (*field_chain
) = type
;
1902 field_chain
= &TREE_CHAIN (*field_chain
);
1907 TREE_VEC_LENGTH (args
), NULL_TREE
);
1910 /* Don't replace enum constants here. */
1911 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1912 if (TREE_CODE (t
) != CONST_DECL
)
1914 tree r
= tsubst (t
, args
,
1915 TREE_VEC_LENGTH (args
), NULL_TREE
);
1916 if (TREE_CODE (r
) == VAR_DECL
)
1918 if (! uses_template_parms (r
))
1919 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1920 /* Perhaps I should do more of grokfield here. */
1922 DECL_IN_AGGR_P (r
) = 1;
1923 DECL_EXTERNAL (r
) = 1;
1924 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1928 field_chain
= &TREE_CHAIN (r
);
1931 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1932 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
1934 if (DECL_CONSTRUCTOR_P (t
))
1935 grok_ctor_properties (type
, t
);
1936 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
1937 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
1940 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1941 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1942 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
1945 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
1946 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
1947 TREE_VEC_LENGTH (args
), NULL_TREE
);
1949 /* This does injection for friend classes. */
1950 for (; d
; d
= TREE_CHAIN (d
))
1951 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1953 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
1954 TREE_VEC_LENGTH (args
), NULL_TREE
);
1956 for (; d
; d
= TREE_CHAIN (d
))
1958 tree t
= TREE_VALUE (d
);
1960 if (TREE_CODE (t
) == TYPE_DECL
)
1961 /* Already injected. */;
1967 if (! uses_template_parms (type
))
1970 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1971 if (TREE_CODE (tmp
) == FIELD_DECL
)
1973 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
1974 require_complete_type (tmp
);
1977 type
= finish_struct_1 (type
, 0);
1978 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
1980 repo_template_used (type
);
1981 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
1982 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
1986 TYPE_SIZE (type
) = integer_zero_node
;
1987 CLASSTYPE_METHOD_VEC (type
)
1988 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1991 TYPE_BEING_DEFINED (type
) = 0;
1994 pop_from_top_level ();
2004 if (t1
== NULL_TREE
)
2005 return t2
== NULL_TREE
;
2006 if (t2
== NULL_TREE
)
2008 /* Don't care if one declares its arg const and the other doesn't -- the
2009 main variant of the arg type is all that matters. */
2010 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
2011 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
2013 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
2017 lookup_nested_type_by_name (ctype
, name
)
2022 complete_type (ctype
);
2024 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
2026 if (name
== TREE_PURPOSE (t
)
2027 /* this catches typedef enum { foo } bar; */
2028 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
2029 return TREE_VALUE (t
);
2034 /* If arg is a non-type template parameter that does not depend on template
2035 arguments, fold it like we weren't in the body of a template. */
2038 maybe_fold_nontype_arg (arg
)
2041 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't'
2042 && !uses_template_parms (arg
))
2044 /* Sometimes, one of the args was an expression involving a
2045 template constant parameter, like N - 1. Now that we've
2046 tsubst'd, we might have something like 2 - 1. This will
2047 confuse lookup_template_class, so we do constant folding
2048 here. We have to unset processing_template_decl, to
2049 fool build_expr_from_tree() into building an actual
2052 int saved_processing_template_decl
= processing_template_decl
;
2053 processing_template_decl
= 0;
2054 arg
= fold (build_expr_from_tree (arg
));
2055 processing_template_decl
= saved_processing_template_decl
;
2060 /* Take the tree structure T and replace template parameters used therein
2061 with the argument vector ARGS. NARGS is the number of args; should
2062 be removed. IN_DECL is an associated decl for diagnostics.
2064 tsubst is used for dealing with types, decls and the like; for
2065 expressions, use tsubst_expr or tsubst_copy. */
2068 tsubst (t
, args
, nargs
, in_decl
)
2075 if (t
== NULL_TREE
|| t
== error_mark_node
2076 || t
== integer_type_node
2077 || t
== void_type_node
2078 || t
== char_type_node
)
2081 type
= TREE_TYPE (t
);
2082 if (type
== unknown_type_node
)
2083 my_friendly_abort (42);
2084 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
2085 && TREE_CODE (t
) != TYPENAME_TYPE
)
2086 type
= tsubst (type
, args
, nargs
, in_decl
);
2088 switch (TREE_CODE (t
))
2091 if (TYPE_PTRMEMFUNC_P (t
))
2093 tree r
= build_ptrmemfunc_type
2094 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
2095 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2099 /* else fall through */
2101 if (uses_template_parms (t
))
2103 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
2104 tree r
= lookup_template_class (t
, argvec
, in_decl
);
2105 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2109 /* else fall through */
2111 case IDENTIFIER_NODE
:
2124 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2125 if (ctx
== NULL_TREE
)
2127 else if (ctx
== current_function_decl
)
2128 return lookup_name (TYPE_IDENTIFIER (t
), 1);
2130 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
2134 if (t
== integer_type_node
)
2137 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
2138 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
2142 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
2143 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
2144 if (processing_template_decl
)
2146 tree itype
= make_node (INTEGER_TYPE
);
2147 TYPE_MIN_VALUE (itype
) = size_zero_node
;
2148 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
2153 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
2154 return build_index_2_type (size_zero_node
, max
);
2157 case TEMPLATE_TYPE_PARM
:
2158 case TEMPLATE_CONST_PARM
:
2163 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2165 idx
= TEMPLATE_TYPE_IDX (t
);
2166 level
= TEMPLATE_TYPE_LEVEL (t
);
2170 idx
= TEMPLATE_CONST_IDX (t
);
2171 level
= TEMPLATE_CONST_LEVEL (t
);
2174 if (TREE_VEC_LENGTH (args
) > 0)
2176 tree arg
= NULL_TREE
;
2178 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
2180 if (TREE_VEC_LENGTH (args
) >= level
- 1)
2182 (TREE_VEC_ELT (args
, level
- 1), idx
);
2184 else if (level
== 1)
2185 arg
= TREE_VEC_ELT (args
, idx
);
2187 if (arg
!= NULL_TREE
)
2189 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2190 return cp_build_type_variant
2191 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
2192 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
2198 /* If we get here, we must have been looking at a parm for a
2199 more deeply nested template. */
2200 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
2201 && TEMPLATE_CONST_LEVEL (t
) > 1)
2202 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
2203 && TEMPLATE_TYPE_LEVEL (t
) > 1),
2210 /* We can get here when processing a member template function
2211 of a template class. */
2213 tree decl
= DECL_TEMPLATE_RESULT (t
);
2219 /* We might already have an instance of this template. */
2220 tree instances
= DECL_TEMPLATE_INSTANTIATIONS (t
);
2221 tree ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, in_decl
);
2223 for (; instances
; instances
= TREE_CHAIN (instances
))
2224 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances
)) == ctx
)
2225 return TREE_VALUE (instances
);
2227 /* Make a new template decl. It will be similar to the
2228 original, but will record the current template arguments.
2229 We also create a new function declaration, which is just
2230 like the old one, but points to this new template, rather
2231 than the old one. */
2232 tmpl
= copy_node (t
);
2233 copy_lang_decl (tmpl
);
2234 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
2235 DECL_CHAIN (tmpl
) = NULL_TREE
;
2236 TREE_CHAIN (tmpl
) = NULL_TREE
;
2237 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
2238 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
2239 DECL_RESULT (tmpl
) = new_decl
;
2240 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
2241 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
2242 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
2243 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
2245 /* The template parameters for this new template are all the
2246 template parameters for the old template, except the
2247 outermost level of parameters. */
2248 DECL_TEMPLATE_PARMS (tmpl
)
2249 = copy_node (DECL_TEMPLATE_PARMS (tmpl
));
2250 for (parms
= DECL_TEMPLATE_PARMS (tmpl
);
2251 TREE_CHAIN (parms
) != NULL_TREE
;
2252 parms
= TREE_CHAIN (parms
))
2253 TREE_CHAIN (parms
) = copy_node (TREE_CHAIN (parms
));
2255 /* Record this partial instantiation. */
2256 DECL_TEMPLATE_INSTANTIATIONS (t
)
2257 = perm_tree_cons (NULL_TREE
, tmpl
,
2258 DECL_TEMPLATE_INSTANTIATIONS (t
));
2260 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
2271 if (DECL_CONTEXT (t
) != NULL_TREE
2272 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
2274 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
2278 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
2279 type
= tsubst (type
, args
, nargs
, in_decl
);
2285 type
= tsubst (type
, args
, nargs
, in_decl
);
2288 /* Do we already have this instantiation? */
2289 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2291 tree tmpl
= DECL_TI_TEMPLATE (t
);
2292 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2294 for (; decls
; decls
= TREE_CHAIN (decls
))
2295 if (TREE_TYPE (TREE_VALUE (decls
)) == type
2296 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
2297 && comp_template_args (TREE_PURPOSE (decls
), args
))
2298 return TREE_VALUE (decls
);
2301 /* We do NOT check for matching decls pushed separately at this
2302 point, as they may not represent instantiations of this
2303 template, and in any case are considered separate under the
2304 discrete model. Instead, see add_maybe_template. */
2308 TREE_TYPE (r
) = type
;
2311 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
2312 DECL_CLASS_CONTEXT (r
) = ctx
;
2314 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
2315 IDENTIFIER_POINTER (DECL_NAME (r
)),
2316 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
2318 /* Type-conversion operator. Reconstruct the name, in
2319 case it's the name of one of the template's parameters. */
2320 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
2323 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
2325 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
2326 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
2327 buf
= (char *) alloca (strlen (dbuf
)
2328 + sizeof (DESTRUCTOR_DECL_PREFIX
));
2329 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
2332 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
2336 /* Instantiations of template functions must be mangled
2337 specially, in order to conform to 14.5.5.1
2338 [temp.over.link]. We use in_decl below rather than
2339 DECL_TI_TEMPLATE (r) because the latter is set to
2340 NULL_TREE in instantiate_decl. */
2344 if (DECL_TEMPLATE_INFO (r
))
2345 tmpl
= DECL_TI_TEMPLATE (r
);
2349 /* tmpl will be NULL if this is a specialization of a
2350 member template of a template class. */
2351 if (name_mangling_version
< 1
2352 || tmpl
== NULL_TREE
2353 || (member
&& !is_member_template (tmpl
)
2354 && !DECL_TEMPLATE_INFO (tmpl
)))
2356 arg_types
= TYPE_ARG_TYPES (type
);
2357 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2358 arg_types
= hash_tree_chain
2359 (build_pointer_type (DECL_CONTEXT (r
)),
2362 DECL_ASSEMBLER_NAME (r
)
2363 = build_decl_overload (DECL_NAME (r
), arg_types
,
2368 /* We pass the outermost template parameters to
2369 build_template_decl_overload since the innermost
2370 template parameters are still just template
2371 parameters; there are no corresponding substitution
2373 /* FIXME The messed up thing here is that we get here with
2374 full args and only one level of parms. This is necessary
2375 because when we partially instantiate a member template,
2376 even though there's really only one level of parms left
2377 we re-use the parms from the original template, which
2378 have level 2. When this is fixed we can remove the
2379 add_to_template_args from instantiate_template. */
2380 tree tparms
= DECL_TEMPLATE_PARMS (tmpl
);
2382 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
2383 tparms
= TREE_CHAIN (tparms
);
2385 my_friendly_assert (tparms
!= NULL_TREE
2386 && TREE_CODE (tparms
) == TREE_LIST
,
2388 tparms
= TREE_VALUE (tparms
);
2390 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
2391 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2392 arg_types
= hash_tree_chain
2393 (build_pointer_type (DECL_CONTEXT (r
)),
2396 DECL_ASSEMBLER_NAME (r
)
2397 = build_template_decl_overload
2398 (DECL_NAME (r
), arg_types
,
2399 TREE_TYPE (TREE_TYPE (tmpl
)),
2401 TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
2402 ? TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1) :
2408 make_decl_rtl (r
, NULL_PTR
, 1);
2410 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
2411 DECL_MAIN_VARIANT (r
) = r
;
2412 DECL_RESULT (r
) = NULL_TREE
;
2413 DECL_INITIAL (r
) = NULL_TREE
;
2415 TREE_STATIC (r
) = 0;
2416 TREE_PUBLIC (r
) = 1;
2417 DECL_EXTERNAL (r
) = 1;
2418 DECL_INTERFACE_KNOWN (r
) = 0;
2419 DECL_DEFER_OUTPUT (r
) = 0;
2420 TREE_CHAIN (r
) = NULL_TREE
;
2421 DECL_CHAIN (r
) = NULL_TREE
;
2423 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
2424 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
2426 /* Look for matching decls for the moment. */
2427 if (! member
&& ! flag_ansi_overloading
)
2429 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
2432 if (decls
== NULL_TREE
)
2434 else if (is_overloaded_fn (decls
))
2435 for (decls
= get_first_fn (decls
); decls
;
2436 decls
= DECL_CHAIN (decls
))
2438 if (TREE_CODE (decls
) == FUNCTION_DECL
2439 && TREE_TYPE (decls
) == type
)
2448 int dcl_only
= ! DECL_INITIAL (d
);
2450 DECL_INITIAL (r
) = error_mark_node
;
2451 duplicate_decls (r
, d
);
2454 DECL_INITIAL (r
) = 0;
2458 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2460 tree tmpl
= DECL_TI_TEMPLATE (t
);
2461 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2462 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2464 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
2465 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
2467 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2468 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2470 /* If we have a preexisting version of this function, don't expand
2471 the template version, use the other instead. */
2472 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
2473 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
2475 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2477 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
2478 = tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
2481 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2482 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2484 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
2485 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
2492 tree r
= copy_node (t
);
2493 TREE_TYPE (r
) = type
;
2494 DECL_INITIAL (r
) = TREE_TYPE (r
);
2495 DECL_CONTEXT (r
) = NULL_TREE
;
2496 #ifdef PROMOTE_PROTOTYPES
2497 if ((TREE_CODE (type
) == INTEGER_TYPE
2498 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2499 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
2500 DECL_ARG_TYPE (r
) = integer_type_node
;
2503 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
2509 tree r
= copy_node (t
);
2510 TREE_TYPE (r
) = type
;
2513 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
2515 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2516 TREE_CHAIN (r
) = NULL_TREE
;
2522 tree r
= copy_node (t
);
2524 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2525 TREE_CHAIN (r
) = NULL_TREE
;
2532 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2534 /* Do we already have this instantiation? */
2535 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2537 tree tmpl
= DECL_TI_TEMPLATE (t
);
2538 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2540 for (; decls
; decls
= TREE_CHAIN (decls
))
2541 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
2542 return TREE_VALUE (decls
);
2546 TREE_TYPE (r
) = type
;
2547 DECL_CONTEXT (r
) = ctx
;
2548 if (TREE_STATIC (r
))
2549 DECL_ASSEMBLER_NAME (r
)
2550 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
2552 /* Don't try to expand the initializer until someone tries to use
2553 this variable; otherwise we run into circular dependencies. */
2554 DECL_INITIAL (r
) = NULL_TREE
;
2559 if (DECL_LANG_SPECIFIC (r
))
2562 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
2565 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2567 tree tmpl
= DECL_TI_TEMPLATE (t
);
2568 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2569 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2571 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2572 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2573 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2575 TREE_CHAIN (r
) = NULL_TREE
;
2580 if (t
== TYPE_NAME (TREE_TYPE (t
)))
2581 return TYPE_NAME (type
);
2584 tree r
= copy_node (t
);
2585 TREE_TYPE (r
) = type
;
2586 DECL_CONTEXT (r
) = current_class_type
;
2587 TREE_CHAIN (r
) = NULL_TREE
;
2593 tree purpose
, value
, chain
, result
;
2594 int via_public
, via_virtual
, via_protected
;
2596 if (t
== void_list_node
)
2599 via_public
= TREE_VIA_PUBLIC (t
);
2600 via_protected
= TREE_VIA_PROTECTED (t
);
2601 via_virtual
= TREE_VIA_VIRTUAL (t
);
2603 purpose
= TREE_PURPOSE (t
);
2605 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
2606 value
= TREE_VALUE (t
);
2608 value
= tsubst (value
, args
, nargs
, in_decl
);
2609 chain
= TREE_CHAIN (t
);
2610 if (chain
&& chain
!= void_type_node
)
2611 chain
= tsubst (chain
, args
, nargs
, in_decl
);
2612 if (purpose
== TREE_PURPOSE (t
)
2613 && value
== TREE_VALUE (t
)
2614 && chain
== TREE_CHAIN (t
))
2616 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
2617 purpose
, value
, chain
);
2618 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
2622 if (type
!= NULL_TREE
)
2628 if (type
== TREE_TYPE (t
))
2631 TREE_TYPE (t
) = complete_type (type
);
2632 if (IS_AGGR_TYPE (type
))
2634 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
2635 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
2636 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
2637 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
2642 /* Otherwise, a vector of template arguments. */
2644 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
2645 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
2647 bzero ((char *) elts
, len
* sizeof (tree
));
2649 for (i
= 0; i
< len
; i
++)
2651 elts
[i
] = maybe_fold_nontype_arg
2652 (tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
));
2654 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
2661 t
= make_tree_vec (len
);
2662 for (i
= 0; i
< len
; i
++)
2663 TREE_VEC_ELT (t
, i
) = elts
[i
];
2668 case REFERENCE_TYPE
:
2671 enum tree_code code
;
2672 if (type
== TREE_TYPE (t
))
2675 code
= TREE_CODE (t
);
2676 if (code
== POINTER_TYPE
)
2677 r
= build_pointer_type (type
);
2679 r
= build_reference_type (type
);
2680 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
2681 /* Will this ever be needed for TYPE_..._TO values? */
2686 return build_offset_type
2687 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
2691 tree values
= TYPE_ARG_TYPES (t
);
2692 tree context
= TYPE_CONTEXT (t
);
2693 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
2696 /* Don't bother recursing if we know it won't change anything. */
2697 if (values
!= void_list_node
)
2699 /* This should probably be rewritten to use hash_tree_cons for
2700 the memory savings. */
2701 tree first
= NULL_TREE
;
2704 for (; values
&& values
!= void_list_node
;
2705 values
= TREE_CHAIN (values
))
2707 tree value
= TYPE_MAIN_VARIANT (type_decays_to
2708 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
2709 /* Don't instantiate default args unless they are used.
2710 Handle it in build_over_call instead. */
2711 tree purpose
= TREE_PURPOSE (values
);
2712 tree x
= build_tree_list (purpose
, value
);
2715 TREE_CHAIN (last
) = x
;
2721 if (values
== void_list_node
)
2722 TREE_CHAIN (last
) = void_list_node
;
2727 context
= tsubst (context
, args
, nargs
, in_decl
);
2728 /* Could also optimize cases where return value and
2729 values have common elements (e.g., T min(const &T, const T&). */
2731 /* If the above parameters haven't changed, just return the type. */
2732 if (type
== TREE_TYPE (t
)
2733 && values
== TYPE_VALUES (t
)
2734 && context
== TYPE_CONTEXT (t
))
2737 /* Construct a new type node and return it. */
2738 if (TREE_CODE (t
) == FUNCTION_TYPE
2739 && context
== NULL_TREE
)
2741 fntype
= build_function_type (type
, values
);
2743 else if (context
== NULL_TREE
)
2745 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
2746 args
, nargs
, in_decl
);
2747 fntype
= build_cplus_method_type (base
, type
,
2748 TREE_CHAIN (values
));
2752 fntype
= make_node (TREE_CODE (t
));
2753 TREE_TYPE (fntype
) = type
;
2754 TYPE_CONTEXT (fntype
) = context
;
2755 TYPE_VALUES (fntype
) = values
;
2756 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
2757 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
2758 TYPE_MODE (fntype
) = TYPE_MODE (t
);
2759 if (TYPE_METHOD_BASETYPE (t
))
2760 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
2761 args
, nargs
, in_decl
);
2762 /* Need to generate hash value. */
2763 my_friendly_abort (84);
2765 fntype
= build_type_variant (fntype
,
2770 raises
= tsubst (raises
, args
, nargs
, in_decl
);
2771 fntype
= build_exception_variant (fntype
, raises
);
2777 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
2779 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
2781 r
= build_cplus_array_type (type
, domain
);
2787 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
2788 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2789 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
2793 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
2794 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
2798 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2799 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
2800 return cp_build_type_variant
2801 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
2802 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
2806 return make_pointer_declarator
2807 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2810 return make_reference_declarator
2811 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2814 return build_parse_node
2815 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2816 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2819 return make_call_declarator
2820 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2821 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2822 TREE_OPERAND (t
, 2),
2823 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
2826 return build_parse_node
2827 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2828 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2831 sorry ("use of `%s' in template",
2832 tree_code_name
[(int) TREE_CODE (t
)]);
2833 return error_mark_node
;
2840 emit_line_note (input_filename
, lineno
);
2844 expand_start_bindings (0);
2851 int saved_warn_unused
;
2853 if (processing_template_decl
)
2855 saved_warn_unused
= warn_unused
;
2858 expand_end_bindings (getdecls (), kept_level_p (), 0);
2859 if (processing_template_decl
)
2860 warn_unused
= saved_warn_unused
;
2861 t
= poplevel (kept_level_p (), 1, 0);
2866 /* Like tsubst, but deals with expressions. This function just replaces
2867 template parms; to finish processing the resultant expression, use
2871 tsubst_copy (t
, args
, nargs
, in_decl
)
2876 enum tree_code code
;
2878 if (t
== NULL_TREE
|| t
== error_mark_node
)
2881 code
= TREE_CODE (t
);
2886 return do_identifier (DECL_NAME (t
), 0);
2890 if (DECL_CONTEXT (t
))
2892 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2893 if (ctx
== current_function_decl
)
2894 return lookup_name (DECL_NAME (t
), 0);
2895 else if (ctx
!= DECL_CONTEXT (t
))
2896 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
2902 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2903 t
= tsubst (t
, args
, nargs
, in_decl
);
2908 if (is_member_template (t
))
2909 return tsubst (t
, args
, nargs
, in_decl
);
2914 case IDENTIFIER_NODE
:
2915 return do_identifier (t
, 0);
2919 case REINTERPRET_CAST_EXPR
:
2920 case CONST_CAST_EXPR
:
2921 case STATIC_CAST_EXPR
:
2922 case DYNAMIC_CAST_EXPR
:
2924 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
2925 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2928 case PREDECREMENT_EXPR
:
2929 case PREINCREMENT_EXPR
:
2930 case POSTDECREMENT_EXPR
:
2931 case POSTINCREMENT_EXPR
:
2933 case TRUTH_NOT_EXPR
:
2936 case CONVERT_EXPR
: /* Unary + */
2943 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2948 case TRUNC_DIV_EXPR
:
2950 case FLOOR_DIV_EXPR
:
2951 case ROUND_DIV_EXPR
:
2952 case EXACT_DIV_EXPR
:
2954 case BIT_ANDTC_EXPR
:
2957 case TRUNC_MOD_EXPR
:
2958 case FLOOR_MOD_EXPR
:
2959 case TRUTH_ANDIF_EXPR
:
2960 case TRUTH_ORIF_EXPR
:
2961 case TRUTH_AND_EXPR
:
2982 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2983 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2987 tree fn
= TREE_OPERAND (t
, 0);
2988 if (really_overloaded_fn (fn
))
2989 fn
= tsubst_copy (get_first_fn (fn
), args
, nargs
, in_decl
);
2991 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
2993 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2997 case METHOD_CALL_EXPR
:
2999 tree name
= TREE_OPERAND (t
, 0);
3000 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
3002 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
3003 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
3005 else if (TREE_CODE (name
) == SCOPE_REF
3006 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
3008 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
3009 name
= TREE_OPERAND (name
, 1);
3010 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
3011 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
3012 name
= build_nt (SCOPE_REF
, base
, name
);
3015 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3017 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3018 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
3025 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3026 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3027 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3032 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3033 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3034 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3035 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
3042 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3043 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3044 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
3045 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
3049 case TEMPLATE_ID_EXPR
:
3051 /* Substituted template arguments */
3052 tree targs
= tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3054 for (chain
= targs
; chain
; chain
= TREE_CHAIN (chain
))
3055 TREE_VALUE (chain
) = maybe_fold_nontype_arg (TREE_VALUE (chain
));
3057 return lookup_template_function
3058 (tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
), targs
);
3063 tree purpose
, value
, chain
;
3065 if (t
== void_list_node
)
3068 purpose
= TREE_PURPOSE (t
);
3070 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
3071 value
= TREE_VALUE (t
);
3073 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
3074 chain
= TREE_CHAIN (t
);
3075 if (chain
&& chain
!= void_type_node
)
3076 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
3077 if (purpose
== TREE_PURPOSE (t
)
3078 && value
== TREE_VALUE (t
)
3079 && chain
== TREE_CHAIN (t
))
3081 return tree_cons (purpose
, value
, chain
);
3088 case TEMPLATE_TYPE_PARM
:
3089 case TEMPLATE_CONST_PARM
:
3091 case REFERENCE_TYPE
:
3097 return tsubst (t
, args
, nargs
, in_decl
);
3099 case IDENTIFIER_NODE
:
3100 if (IDENTIFIER_TYPENAME_P (t
))
3101 return build_typename_overload
3102 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
3108 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
3109 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
3116 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
3119 tsubst_expr (t
, args
, nargs
, in_decl
)
3124 if (t
== NULL_TREE
|| t
== error_mark_node
)
3127 if (processing_template_decl
)
3128 return tsubst_copy (t
, args
, nargs
, in_decl
);
3130 switch (TREE_CODE (t
))
3133 lineno
= TREE_COMPLEXITY (t
);
3134 emit_line_note (input_filename
, lineno
);
3136 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3141 lineno
= TREE_COMPLEXITY (t
);
3142 emit_line_note (input_filename
, lineno
);
3143 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3144 /* Do default conversion if safe and possibly important,
3145 in case within ({...}). */
3146 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
3147 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
3148 t
= default_conversion (t
);
3149 cplus_expand_expr_stmt (t
);
3156 int i
= suspend_momentary ();
3159 lineno
= TREE_COMPLEXITY (t
);
3160 emit_line_note (input_filename
, lineno
);
3162 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3163 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3164 TREE_OPERAND (t
, 2) != 0);
3165 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3167 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3168 resume_momentary (i
);
3175 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
3176 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3177 int cond_scope
= (TREE_OPERAND (t
, 1)
3178 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
3180 lineno
= TREE_COMPLEXITY (t
);
3181 emit_line_note (input_filename
, lineno
);
3184 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
3185 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3187 emit_line_note (input_filename
, lineno
);
3188 expand_start_loop_continue_elsewhere (1);
3192 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3193 emit_line_note (input_filename
, lineno
);
3195 expand_exit_loop_if_false (0, condition_conversion (tmp
));
3199 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
3202 emit_line_note (input_filename
, lineno
);
3203 expand_loop_continue_here ();
3204 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3206 cplus_expand_expr_stmt (tmp
);
3219 lineno
= TREE_COMPLEXITY (t
);
3221 emit_line_note (input_filename
, lineno
);
3222 expand_start_loop (1);
3224 cond
= TREE_OPERAND (t
, 0);
3225 if (TREE_CODE (cond
) == DECL_STMT
)
3227 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
3228 emit_line_note (input_filename
, lineno
);
3229 expand_exit_loop_if_false (0, condition_conversion (cond
));
3231 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
3233 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3245 lineno
= TREE_COMPLEXITY (t
);
3247 emit_line_note (input_filename
, lineno
);
3248 expand_start_loop_continue_elsewhere (1);
3250 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3251 expand_loop_continue_here ();
3253 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3254 emit_line_note (input_filename
, lineno
);
3255 expand_exit_loop_if_false (0, condition_conversion (cond
));
3266 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3268 lineno
= TREE_COMPLEXITY (t
);
3271 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3272 emit_line_note (input_filename
, lineno
);
3273 expand_start_cond (condition_conversion (tmp
), 0);
3275 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3276 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3278 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
3280 expand_start_else ();
3281 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3295 tree substmt
= TREE_OPERAND (t
, 0);
3297 lineno
= TREE_COMPLEXITY (t
);
3299 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3302 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
3303 tsubst_expr (substmt
, args
, nargs
, in_decl
);
3305 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3311 lineno
= TREE_COMPLEXITY (t
);
3312 emit_line_note (input_filename
, lineno
);
3313 if (! expand_exit_something ())
3314 error ("break statement not within loop or switch");
3318 lineno
= TREE_COMPLEXITY (t
);
3319 emit_line_note (input_filename
, lineno
);
3320 if (! expand_continue_loop (0))
3321 error ("continue statement not within a loop");
3327 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3329 lineno
= TREE_COMPLEXITY (t
);
3332 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3333 emit_line_note (input_filename
, lineno
);
3334 c_expand_start_case (val
);
3337 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3338 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3340 expand_end_case (val
);
3351 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3352 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3356 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
3363 lineno
= TREE_COMPLEXITY (t
);
3364 emit_line_note (input_filename
, lineno
);
3365 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
3367 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
3368 TREE_USED (decl
) = 1;
3372 expand_computed_goto
3373 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3377 lineno
= TREE_COMPLEXITY (t
);
3378 emit_line_note (input_filename
, lineno
);
3379 expand_start_try_stmts ();
3380 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3381 expand_start_all_catch ();
3383 tree handler
= TREE_OPERAND (t
, 1);
3384 for (; handler
; handler
= TREE_CHAIN (handler
))
3385 tsubst_expr (handler
, args
, nargs
, in_decl
);
3387 expand_end_all_catch ();
3391 lineno
= TREE_COMPLEXITY (t
);
3393 if (TREE_OPERAND (t
, 0))
3395 tree d
= TREE_OPERAND (t
, 0);
3396 expand_start_catch_block
3397 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
3398 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
3401 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
3402 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3403 expand_end_catch_block ();
3408 lineno
= TREE_COMPLEXITY (t
);
3410 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
3411 tsubst_enum (t
, args
, nargs
, NULL
);
3415 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
3421 instantiate_template (tmpl
, targ_ptr
)
3422 tree tmpl
, targ_ptr
;
3426 struct obstack
*old_fmp_obstack
;
3427 extern struct obstack
*function_maybepermanent_obstack
;
3429 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
3431 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3435 /* Check to see if there is a matching specialization. */
3436 for (specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
3438 specs
= TREE_CHAIN (specs
))
3439 if (comp_template_args (TREE_PURPOSE (specs
), targ_ptr
))
3440 return TREE_VALUE (specs
);
3443 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3444 old_fmp_obstack
= function_maybepermanent_obstack
;
3445 function_maybepermanent_obstack
= &permanent_obstack
;
3447 len
= DECL_NTPARMS (tmpl
);
3452 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
3453 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
3455 tree nt
= target_type (t
);
3456 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
3458 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
3459 cp_error (" trying to instantiate `%D'", tmpl
);
3460 fndecl
= error_mark_node
;
3464 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
3466 targ_ptr
= copy_to_permanent (targ_ptr
);
3468 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
3469 targ_ptr
= add_to_template_args (DECL_TI_ARGS (tmpl
), targ_ptr
);
3471 /* substitute template parameters */
3472 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
3474 if (flag_external_templates
)
3475 add_pending_template (fndecl
);
3478 function_maybepermanent_obstack
= old_fmp_obstack
;
3484 /* Push the name of the class template into the scope of the instantiation. */
3487 overload_template_name (type
)
3490 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
3493 if (IDENTIFIER_CLASS_VALUE (id
)
3494 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
3497 decl
= build_decl (TYPE_DECL
, id
, type
);
3498 SET_DECL_ARTIFICIAL (decl
);
3499 pushdecl_class_level (decl
);
3502 /* Like type_unification but designed specially to handle conversion
3506 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
, strict
)
3507 tree fn
, explicit_targs
, targs
, args
, return_type
;
3511 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3512 tree decl_arg_types
= args
;
3514 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
3516 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
3518 /* This is a template conversion operator. Use the return types
3519 as well as the argument types. */
3520 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
3521 TREE_TYPE (TREE_TYPE (fn
)),
3523 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
3528 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
3529 &TREE_VEC_ELT (targs
, 0),
3539 /* Type unification.
3541 We have a function template signature with one or more references to
3542 template parameters, and a parameter list we wish to fit to this
3543 template. If possible, produce a list of parameters for the template
3544 which will cause it to fit the supplied parameter list.
3546 Return zero for success, 2 for an incomplete match that doesn't resolve
3547 all the types, and 1 for complete failure. An error message will be
3548 printed only for an incomplete match.
3550 TPARMS[NTPARMS] is an array of template parameter types;
3551 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3552 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3553 and ARGS is the argument list we're trying to match against it.
3555 If SUBR is 1, we're being called recursively (to unify the arguments of
3556 a function or method parameter of a function template), so don't zero
3557 out targs and don't fail on an incomplete match.
3559 If STRICT is 1, the match must be exact (for casts of overloaded
3560 addresses, explicit instantiation, and more_specialized). */
3563 type_unification (tparms
, targs
, parms
, args
, targs_in
, nsubsts
,
3564 strict
, allow_incomplete
)
3565 tree tparms
, *targs
, parms
, args
, targs_in
;
3566 int *nsubsts
, strict
, allow_incomplete
;
3568 int ntparms
= TREE_VEC_LENGTH (tparms
);
3573 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
3575 /* Insert any explicit template arguments. They are encoded as the
3576 operands of NOP_EXPRs so that unify can tell that they are
3577 explicit arguments. */
3578 for (i
= 0, t
= targs_in
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
), ++i
)
3579 targs
[i
] = build1 (NOP_EXPR
, NULL_TREE
, TREE_VALUE (t
));
3581 r
= type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, 0,
3582 strict
, allow_incomplete
);
3584 for (i
= 0, t
= targs_in
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
), ++i
)
3585 if (TREE_CODE (targs
[i
]) == NOP_EXPR
)
3586 targs
[i
] = TREE_OPERAND (targs
[i
], 0);
3593 type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, subr
,
3594 strict
, allow_incomplete
)
3595 tree tparms
, *targs
, parms
, args
;
3596 int *nsubsts
, subr
, strict
, allow_incomplete
;
3600 int ntparms
= TREE_VEC_LENGTH (tparms
);
3602 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
3603 my_friendly_assert (parms
== NULL_TREE
3604 || TREE_CODE (parms
) == TREE_LIST
, 290);
3605 /* ARGS could be NULL (via a call from parse.y to
3606 build_x_function_call). */
3608 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
3609 my_friendly_assert (ntparms
> 0, 292);
3612 && parms
!= void_list_node
3614 && args
!= void_list_node
)
3616 parm
= TREE_VALUE (parms
);
3617 parms
= TREE_CHAIN (parms
);
3618 arg
= TREE_VALUE (args
);
3619 args
= TREE_CHAIN (args
);
3621 if (arg
== error_mark_node
)
3623 if (arg
== unknown_type_node
)
3626 /* Conversions will be performed on a function argument that
3627 corresponds with a function parameter that contains only
3628 non-deducible template parameters and explicitly specified
3629 template parameters. */
3630 if (! uses_template_parms (parm
))
3634 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3635 type
= TREE_TYPE (arg
);
3644 if (comptypes (parm
, type
, 1))
3649 if (can_convert_arg (parm
, type
, arg
))
3654 if (can_convert (parm
, type
))
3662 if (TREE_CODE (arg
) == VAR_DECL
)
3663 arg
= TREE_TYPE (arg
);
3664 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
3665 arg
= TREE_TYPE (arg
);
3667 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3669 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
3670 if (TREE_CODE (arg
) == TREE_LIST
3671 && TREE_TYPE (arg
) == unknown_type_node
3672 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
3674 int nsubsts
, ntparms
;
3677 /* Have to back unify here */
3678 arg
= TREE_VALUE (arg
);
3680 ntparms
= DECL_NTPARMS (arg
);
3681 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
3682 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
3684 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
3686 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
3687 parm
, NULL_TREE
, &nsubsts
, strict
,
3690 arg
= TREE_TYPE (arg
);
3693 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
3694 arg
= TREE_TYPE (arg
);
3696 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
3698 if (TREE_CODE (arg
) == FUNCTION_TYPE
3699 || TREE_CODE (arg
) == METHOD_TYPE
)
3700 arg
= build_pointer_type (arg
);
3701 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
3702 arg
= build_pointer_type (TREE_TYPE (arg
));
3704 arg
= TYPE_MAIN_VARIANT (arg
);
3707 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
3715 /* Fail if we've reached the end of the parm list, and more args
3716 are present, and the parm list isn't variadic. */
3717 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
3719 /* Fail if parms are left and they don't have default values. */
3721 && parms
!= void_list_node
3722 && TREE_PURPOSE (parms
) == NULL_TREE
)
3725 for (i
= 0; i
< ntparms
; i
++)
3728 if (!allow_incomplete
)
3729 error ("incomplete type unification");
3735 /* Tail recursion is your friend. */
3738 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
3739 tree tparms
, *targs
, parm
, arg
;
3740 int *nsubsts
, ntparms
, strict
;
3744 /* I don't think this will do the right thing with respect to types.
3745 But the only case I've seen it in so far has been array bounds, where
3746 signedness is the only information lost, and I think that will be
3748 while (TREE_CODE (parm
) == NOP_EXPR
)
3749 parm
= TREE_OPERAND (parm
, 0);
3751 if (arg
== error_mark_node
)
3753 if (arg
== unknown_type_node
)
3758 switch (TREE_CODE (parm
))
3761 /* In a type which contains a nested-name-specifier, template
3762 argument values cannot be deduced for template parameters used
3763 within the nested-name-specifier. */
3766 case TEMPLATE_TYPE_PARM
:
3768 idx
= TEMPLATE_TYPE_IDX (parm
);
3769 /* Check for mixed types and values. */
3770 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
3773 if (!strict
&& targs
[idx
] != NULL_TREE
&&
3774 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
3775 /* An explicit template argument. Don't even try to match
3776 here; the overload resolution code will manage check to
3777 see whether the call is legal. */
3780 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
3781 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
3784 /* Template type parameters cannot contain cv-quals; i.e.
3785 template <class T> void f (T& a, T& b) will not generate
3786 void f (const int& a, const int& b). */
3787 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
3788 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
3790 arg
= TYPE_MAIN_VARIANT (arg
);
3793 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
3794 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
3795 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
3798 /* Simple cases: Value already set, does match or doesn't. */
3799 if (targs
[idx
] == arg
3801 && TREE_CODE (targs
[idx
]) == NOP_EXPR
3802 && TREE_OPERAND (targs
[idx
], 0) == arg
))
3804 else if (targs
[idx
])
3808 case TEMPLATE_CONST_PARM
:
3810 idx
= TEMPLATE_CONST_IDX (parm
);
3813 int i
= cp_tree_equal (targs
[idx
], arg
);
3819 my_friendly_abort (42);
3822 targs
[idx
] = copy_to_permanent (arg
);
3826 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
3827 return unify (tparms
, targs
, ntparms
, parm
,
3828 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
3830 if (TREE_CODE (arg
) != POINTER_TYPE
)
3832 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3835 case REFERENCE_TYPE
:
3836 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
3837 arg
= TREE_TYPE (arg
);
3838 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
3842 if (TREE_CODE (arg
) != ARRAY_TYPE
)
3844 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
3845 nsubsts
, strict
) != 0)
3847 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3855 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3858 if (TREE_CODE (parm
) == INTEGER_TYPE
)
3860 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
3861 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
3862 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
3864 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
3865 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
3866 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
3869 else if (TREE_CODE (parm
) == REAL_TYPE
3870 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
3873 /* As far as unification is concerned, this wins. Later checks
3874 will invalidate it if necessary. */
3877 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3878 /* Type INTEGER_CST can come from ordinary constant template args. */
3880 while (TREE_CODE (arg
) == NOP_EXPR
)
3881 arg
= TREE_OPERAND (arg
, 0);
3883 if (TREE_CODE (arg
) != INTEGER_CST
)
3885 return !tree_int_cst_equal (parm
, arg
);
3890 t1
= TREE_OPERAND (parm
, 0);
3891 t2
= TREE_OPERAND (parm
, 1);
3892 return unify (tparms
, targs
, ntparms
, t1
,
3893 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
3900 if (TREE_CODE (arg
) != TREE_VEC
)
3902 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
3904 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
3905 if (unify (tparms
, targs
, ntparms
,
3906 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
3913 if (TYPE_PTRMEMFUNC_FLAG (parm
))
3914 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
3915 arg
, nsubsts
, strict
);
3917 /* Allow trivial conversions. */
3918 if (TREE_CODE (arg
) != RECORD_TYPE
3919 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
3920 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
3923 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
3926 if (flag_ansi_overloading
&& ! strict
)
3927 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
3929 (CLASSTYPE_TEMPLATE_INFO (arg
)
3930 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
3932 if (! t
|| t
== error_mark_node
)
3935 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
3936 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
3938 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
3943 if (TREE_CODE (arg
) != METHOD_TYPE
)
3948 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
3951 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3952 TREE_TYPE (arg
), nsubsts
, strict
))
3954 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
3955 TYPE_ARG_TYPES (arg
), nsubsts
, 1,
3959 if (TREE_CODE (arg
) != OFFSET_TYPE
)
3961 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
3962 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
3964 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3965 TREE_TYPE (arg
), nsubsts
, strict
);
3968 if (arg
!= decl_constant_value (parm
))
3973 sorry ("use of `%s' in template type unification",
3974 tree_code_name
[(int) TREE_CODE (parm
)]);
3980 mark_decl_instantiated (result
, extern_p
)
3984 if (DECL_TEMPLATE_INSTANTIATION (result
))
3985 SET_DECL_EXPLICIT_INSTANTIATION (result
);
3986 TREE_PUBLIC (result
) = 1;
3990 DECL_INTERFACE_KNOWN (result
) = 1;
3991 DECL_NOT_REALLY_EXTERN (result
) = 1;
3993 /* For WIN32 we also want to put explicit instantiations in
3994 linkonce sections. */
3995 if (supports_one_only () && ! SUPPORTS_WEAK
)
3996 comdat_linkage (result
);
3998 else if (TREE_CODE (result
) == FUNCTION_DECL
)
3999 mark_inline_for_output (result
);
4002 /* Given two function templates PAT1 and PAT2, return:
4004 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
4005 -1 if PAT2 is more specialized than PAT1.
4006 0 if neither is more specialized. */
4009 more_specialized (pat1
, pat2
)
4015 targs
= get_bindings (pat1
, pat2
);
4021 targs
= get_bindings (pat2
, pat1
);
4030 /* Given two class template specialization list nodes PAT1 and PAT2, return:
4032 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4033 -1 if PAT2 is more specialized than PAT1.
4034 0 if neither is more specialized. */
4037 more_specialized_class (pat1
, pat2
)
4043 targs
= get_class_bindings
4044 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
4048 targs
= get_class_bindings
4049 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
4056 /* Return the template arguments that will produce the function signature
4057 DECL from the function template FN. */
4060 get_bindings (fn
, decl
)
4063 int ntparms
= DECL_NTPARMS (fn
);
4064 tree targs
= make_scratch_vec (ntparms
);
4067 i
= fn_type_unification (fn
, NULL_TREE
, targs
,
4068 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
4069 TREE_TYPE (TREE_TYPE (decl
)),
4078 get_class_bindings (tparms
, parms
, args
)
4079 tree tparms
, parms
, args
;
4081 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
4082 tree vec
= make_temp_vec (ntparms
);
4084 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
4086 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
4087 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
4097 for (i
= 0; i
< ntparms
; ++i
)
4098 if (! TREE_VEC_ELT (vec
, i
))
4104 /* Return the most specialized of the list of templates in FNS that can
4105 produce an instantiation matching DECL. */
4108 most_specialized (fns
, decl
)
4111 tree fn
, champ
, args
, *p
;
4114 for (p
= &fns
; *p
; )
4116 args
= get_bindings (TREE_VALUE (*p
), decl
);
4119 p
= &TREE_CHAIN (*p
);
4122 *p
= TREE_CHAIN (*p
);
4129 champ
= TREE_VALUE (fn
);
4130 fn
= TREE_CHAIN (fn
);
4131 for (; fn
; fn
= TREE_CHAIN (fn
))
4133 fate
= more_specialized (champ
, TREE_VALUE (fn
));
4140 fn
= TREE_CHAIN (fn
);
4142 return error_mark_node
;
4144 champ
= TREE_VALUE (fn
);
4148 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
4150 fate
= more_specialized (champ
, TREE_VALUE (fn
));
4152 return error_mark_node
;
4158 /* Return the most specialized of the class template specializations in
4159 SPECS that can produce an instantiation matching ARGS. */
4162 most_specialized_class (specs
, mainargs
)
4163 tree specs
, mainargs
;
4165 tree list
= NULL_TREE
, t
, args
, champ
;
4168 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
4170 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
4173 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
4174 TREE_TYPE (list
) = TREE_TYPE (t
);
4184 for (; t
; t
= TREE_CHAIN (t
))
4186 fate
= more_specialized_class (champ
, t
);
4195 return error_mark_node
;
4201 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
4203 fate
= more_specialized_class (champ
, t
);
4205 return error_mark_node
;
4211 /* called from the parser. */
4214 do_decl_instantiation (declspecs
, declarator
, storage
)
4215 tree declspecs
, declarator
, storage
;
4217 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
4220 tree result
= NULL_TREE
;
4222 tree templates
= NULL_TREE
;
4224 if (! DECL_LANG_SPECIFIC (decl
))
4226 cp_error ("explicit instantiation of non-template `%#D'", decl
);
4230 /* If we've already seen this template instance, use it. */
4231 if (TREE_CODE (decl
) == VAR_DECL
)
4233 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
4234 if (result
&& TREE_CODE (result
) != VAR_DECL
)
4237 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
4239 cp_error ("explicit instantiation of `%#D'", decl
);
4242 else if (DECL_FUNCTION_MEMBER_P (decl
))
4244 if (DECL_TEMPLATE_INSTANTIATION (decl
))
4246 else if (name
= DECL_ASSEMBLER_NAME (decl
),
4247 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
4248 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
4252 /* Maybe this is an instantiation of a member template
4254 tree ctype
= DECL_CONTEXT (decl
);
4256 name
= DECL_NAME (decl
);
4257 fn
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
4259 fn
= TREE_VALUE (fn
);
4261 for (; fn
; fn
= DECL_CHAIN (fn
))
4262 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4263 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
4266 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
4268 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
4269 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4270 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
4273 if (templates
&& !result
)
4276 result
= most_specialized (templates
, decl
);
4277 if (result
== error_mark_node
)
4279 char *str
= "candidates are:";
4280 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
4281 for (fn
= templates
; fn
; fn
= TREE_CHAIN (fn
))
4283 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
4290 args
= get_bindings (result
, decl
);
4291 result
= instantiate_template (result
, args
);
4297 cp_error ("no matching template for `%D' found", decl
);
4301 if (! DECL_TEMPLATE_INFO (result
))
4303 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
4307 if (flag_external_templates
)
4310 if (storage
== NULL_TREE
)
4312 else if (storage
== ridpointers
[(int) RID_EXTERN
])
4315 cp_error ("storage class `%D' applied to template instantiation",
4318 mark_decl_instantiated (result
, extern_p
);
4319 repo_template_instantiated (result
, extern_p
);
4321 instantiate_decl (result
);
4325 mark_class_instantiated (t
, extern_p
)
4329 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
4331 if (supports_one_only () && ! SUPPORTS_WEAK
)
4332 /* For WIN32 we also want to put explicit instantiations in
4333 linkonce sections. */;
4336 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
4337 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 /* import_export_decl has to happen after DECL_INITIAL is set up. */
4509 if (pattern_defined
)
4511 repo_template_used (d
);
4513 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
4515 if (flag_alt_external_templates
)
4517 if (interface_unknown
)
4518 warn_if_unknown_interface (d
);
4520 else if (DECL_INTERFACE_KNOWN (code_pattern
))
4522 DECL_INTERFACE_KNOWN (d
) = 1;
4523 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
4526 warn_if_unknown_interface (code_pattern
);
4530 import_export_decl (d
);
4533 /* Reject all external templates except inline functions. */
4534 if (DECL_INTERFACE_KNOWN (d
)
4535 && ! DECL_NOT_REALLY_EXTERN (d
)
4536 && ! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
)))
4539 /* Defer all templates except inline functions used in another function. */
4540 if (! pattern_defined
4541 || (! (TREE_CODE (d
) == FUNCTION_DECL
&& DECL_INLINE (d
) && nested
)
4544 add_pending_template (d
);
4548 lineno
= DECL_SOURCE_LINE (d
);
4549 input_filename
= DECL_SOURCE_FILE (d
);
4551 /* Trick tsubst into giving us a new decl in case the template changed. */
4552 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
4553 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
4554 td
= tsubst (decl_pattern
, args
, TREE_VEC_LENGTH (args
), tmpl
);
4555 SET_DECL_IMPLICIT_INSTANTIATION (td
);
4556 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
4558 /* And set up DECL_INITIAL, since tsubst doesn't. */
4559 if (TREE_CODE (td
) == VAR_DECL
)
4561 pushclass (DECL_CONTEXT (d
), 2);
4562 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
4563 TREE_VEC_LENGTH (args
), tmpl
);
4567 if (TREE_CODE (d
) == FUNCTION_DECL
)
4569 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4571 DECL_INITIAL (td
) = error_mark_node
;
4573 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
4574 /* Set up the information about what is being specialized. */
4575 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
4577 duplicate_decls (td
, d
);
4578 if (TREE_CODE (d
) == FUNCTION_DECL
)
4579 DECL_INITIAL (td
) = 0;
4581 if (TREE_CODE (d
) == VAR_DECL
)
4583 DECL_IN_AGGR_P (d
) = 0;
4584 if (DECL_INTERFACE_KNOWN (d
))
4585 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
4588 DECL_EXTERNAL (d
) = 1;
4589 DECL_NOT_REALLY_EXTERN (d
) = 1;
4591 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
4593 else if (TREE_CODE (d
) == FUNCTION_DECL
)
4595 tree t
= DECL_SAVED_TREE (code_pattern
);
4597 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
4598 store_parm_decls ();
4600 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
4603 (TREE_OPERAND (t
, 0),
4604 tsubst_expr (TREE_OPERAND (t
, 1), args
,
4605 TREE_VEC_LENGTH (args
), tmpl
));
4609 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
4611 current_member_init_list
4612 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
4613 current_base_init_list
4614 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
4619 /* Always keep the BLOCK node associated with the outermost
4620 pair of curly braces of a function. These are needed
4621 for correct operation of dwarfout.c. */
4624 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
4625 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
4627 finish_function (lineno
, 0, nested
);
4632 input_filename
= file
;
4634 pop_from_top_level ();
4641 tsubst_chain (t
, argvec
)
4646 tree first
= tsubst (t
, argvec
,
4647 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4650 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
4652 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4653 TREE_CHAIN (last
) = x
;
4663 tsubst_expr_values (t
, argvec
)
4666 tree first
= NULL_TREE
;
4669 for (; t
; t
= TREE_CHAIN (t
))
4671 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
4672 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4673 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
4674 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4675 *p
= build_tree_list (pur
, val
);
4676 p
= &TREE_CHAIN (*p
);
4687 last_tree
= TREE_CHAIN (last_tree
) = t
;
4690 /* D is an undefined function declaration in the presence of templates with
4691 the same name, listed in FNS. If one of them can produce D as an
4692 instantiation, remember this so we can instantiate it at EOF if D has
4693 not been defined by that time. */
4696 add_maybe_template (d
, fns
)
4701 if (DECL_MAYBE_TEMPLATE (d
))
4704 t
= most_specialized (fns
, d
);
4707 if (t
== error_mark_node
)
4709 cp_error ("ambiguous template instantiation for `%D'", d
);
4713 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
4714 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
4715 DECL_MAYBE_TEMPLATE (d
) = 1;
4718 /* Instantiate an enumerated type. Used by instantiate_class_template and
4722 tsubst_enum (tag
, args
, nargs
, field_chain
)
4727 extern tree current_local_enum
;
4728 tree prev_local_enum
= current_local_enum
;
4730 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
4731 tree e
, values
= NULL_TREE
;
4733 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
4735 tree elt
= build_enumerator (TREE_PURPOSE (e
),
4736 tsubst_expr (TREE_VALUE (e
), args
,
4738 TREE_CHAIN (elt
) = values
;
4742 finish_enum (newtag
, values
);
4744 if (NULL
!= field_chain
)
4745 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
4747 current_local_enum
= prev_local_enum
;