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-const `%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_CLASS (TREE_CODE (ot
)) == 't')
1162 if (comptypes (ot
, nt
, 1))
1165 else if (cp_tree_equal (ot
, nt
) > 0)
1172 /* Given class template name and parameter list, produce a user-friendly name
1173 for the instantiation. */
1176 mangle_class_name_for_template (name
, parms
, arglist
)
1178 tree parms
, arglist
;
1180 static struct obstack scratch_obstack
;
1181 static char *scratch_firstobj
;
1184 if (!scratch_firstobj
)
1185 gcc_obstack_init (&scratch_obstack
);
1187 obstack_free (&scratch_obstack
, scratch_firstobj
);
1188 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
1191 #define buflen sizeof(buf)
1192 #define check if (bufp >= buf+buflen-1) goto too_long
1193 #define ccat(c) *bufp++=(c); check
1194 #define advance bufp+=strlen(bufp); check
1195 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1198 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1200 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1205 nparms
= TREE_VEC_LENGTH (parms
);
1206 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
1207 for (i
= 0; i
< nparms
; i
++)
1209 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1210 tree arg
= TREE_VEC_ELT (arglist
, i
);
1215 if (TREE_CODE (parm
) == TYPE_DECL
)
1217 cat (type_as_string (arg
, 0));
1221 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
1223 if (TREE_CODE (arg
) == TREE_LIST
)
1225 /* New list cell was built because old chain link was in
1227 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
1228 arg
= TREE_VALUE (arg
);
1230 /* No need to check arglist against parmlist here; we did that
1231 in coerce_template_parms, called from lookup_template_class. */
1232 cat (expr_as_string (arg
, 0));
1235 char *bufp
= obstack_next_free (&scratch_obstack
);
1237 while (bufp
[offset
- 1] == ' ')
1239 obstack_blank_fast (&scratch_obstack
, offset
);
1241 /* B<C<char> >, not B<C<char>> */
1242 if (bufp
[offset
- 1] == '>')
1247 return (char *) obstack_base (&scratch_obstack
);
1252 fatal ("out of (preallocated) string space creating template instantiation name");
1258 classtype_mangled_name (t
)
1261 if (CLASSTYPE_TEMPLATE_INFO (t
)
1262 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
1264 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
1265 char *mangled_name
= mangle_class_name_for_template
1266 (IDENTIFIER_POINTER (name
),
1267 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
1268 CLASSTYPE_TI_ARGS (t
));
1269 tree id
= get_identifier (mangled_name
);
1270 IDENTIFIER_TEMPLATE (id
) = name
;
1274 return TYPE_IDENTIFIER (t
);
1278 add_pending_template (d
)
1283 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
1284 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
1286 ti
= DECL_TEMPLATE_INFO (d
);
1288 if (TI_PENDING_TEMPLATE_FLAG (ti
))
1291 *template_tail
= perm_tree_cons
1292 (current_function_decl
, d
, NULL_TREE
);
1293 template_tail
= &TREE_CHAIN (*template_tail
);
1294 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
1298 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1299 may be either a _DECL or an overloaded function or an
1300 IDENTIFIER_NODE), and ARGLIST. */
1303 lookup_template_function (fns
, arglist
)
1306 if (fns
== NULL_TREE
)
1308 cp_error ("non-template used as template");
1309 return error_mark_node
;
1312 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
1314 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1315 arglist
= copy_list (arglist
);
1319 return build_min (TEMPLATE_ID_EXPR
,
1321 ? TREE_TYPE (fns
) : unknown_type_node
,
1326 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1327 parameters, find the desired type.
1329 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1330 Since ARGLIST is build on the decl_obstack, we must copy it here
1331 to keep it from being reclaimed when the decl storage is reclaimed.
1333 IN_DECL, if non-NULL, is the template declaration we are trying to
1337 lookup_template_class (d1
, arglist
, in_decl
)
1341 tree
template, parmlist
;
1345 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
1347 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
1349 template = IDENTIFIER_CLASS_VALUE (d1
);
1351 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
1353 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
1354 d1
= DECL_NAME (template);
1356 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
1358 template = CLASSTYPE_TI_TEMPLATE (d1
);
1359 d1
= DECL_NAME (template);
1362 my_friendly_abort (272);
1364 /* With something like `template <class T> class X class X { ... };'
1365 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1366 We don't want to do that, but we have to deal with the situation, so
1367 let's give them some syntax errors to chew on instead of a crash. */
1369 return error_mark_node
;
1370 if (TREE_CODE (template) != TEMPLATE_DECL
)
1372 cp_error ("non-template type `%T' used as a template", d1
);
1374 cp_error_at ("for template declaration `%D'", in_decl
);
1375 return error_mark_node
;
1378 if (PRIMARY_TEMPLATE_P (template))
1380 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
1382 arglist
= coerce_template_parms (parmlist
, arglist
, template);
1383 if (arglist
== error_mark_node
)
1384 return error_mark_node
;
1385 if (uses_template_parms (arglist
))
1388 if (comp_template_args
1389 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
1390 found
= TREE_TYPE (template);
1393 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
1394 found
; found
= TREE_CHAIN (found
))
1396 if (TI_USES_TEMPLATE_PARMS (found
)
1397 && comp_template_args (TREE_PURPOSE (found
), arglist
))
1401 found
= TREE_VALUE (found
);
1406 if (can_free (&permanent_obstack
, arglist
))
1407 obstack_free (&permanent_obstack
, arglist
);
1412 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
1414 id
= get_identifier (mangled_name
);
1415 IDENTIFIER_TEMPLATE (id
) = d1
;
1417 maybe_push_to_top_level (uses_template_parms (arglist
));
1418 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
1419 pop_from_top_level ();
1423 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1426 arglist
= CLASSTYPE_TI_ARGS (ctx
);
1428 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
1430 int save_temp
= processing_template_decl
;
1431 processing_template_decl
= 0;
1432 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
1433 processing_template_decl
= save_temp
;
1437 t
= lookup_nested_type_by_name (ctx
, id
);
1438 my_friendly_assert (t
!= NULL_TREE
, 42);
1442 /* Seems to be wanted. */
1443 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1445 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1447 arglist
= copy_to_permanent (arglist
);
1448 CLASSTYPE_TEMPLATE_INFO (t
)
1449 = perm_tree_cons (template, arglist
, NULL_TREE
);
1450 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1451 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
1452 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1453 = uses_template_parms (arglist
);
1455 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
1457 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1458 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
1459 /* if (! uses_template_parms (arglist)) */
1460 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
1461 = get_identifier (build_overload_name (t
, 1, 1));
1463 if (flag_external_templates
&& ! uses_template_parms (arglist
)
1464 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1465 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1466 add_pending_template (t
);
1472 /* Should be defined in parse.h. */
1476 uses_template_parms (t
)
1481 switch (TREE_CODE (t
))
1485 /* We assume that the object must be instantiated in order to build
1486 the COMPONENT_REF, so we test only whether the type of the
1487 COMPONENT_REF uses template parms. */
1488 return uses_template_parms (TREE_TYPE (t
));
1490 case IDENTIFIER_NODE
:
1491 if (!IDENTIFIER_TEMPLATE (t
))
1493 my_friendly_abort (42);
1495 /* aggregates of tree nodes */
1498 int i
= TREE_VEC_LENGTH (t
);
1500 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
1505 if (uses_template_parms (TREE_PURPOSE (t
))
1506 || uses_template_parms (TREE_VALUE (t
)))
1508 return uses_template_parms (TREE_CHAIN (t
));
1510 /* constructed type nodes */
1512 case REFERENCE_TYPE
:
1513 return uses_template_parms (TREE_TYPE (t
));
1515 if (TYPE_PTRMEMFUNC_FLAG (t
))
1516 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1518 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1520 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
1522 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1524 return uses_template_parms (TREE_TYPE (t
));
1526 if (uses_template_parms (TYPE_DOMAIN (t
)))
1528 return uses_template_parms (TREE_TYPE (t
));
1530 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
1532 return uses_template_parms (TREE_TYPE (t
));
1534 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
1536 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1538 return uses_template_parms (TREE_TYPE (t
));
1542 return uses_template_parms (TREE_TYPE (t
));
1546 /* ??? What about FIELD_DECLs? */
1547 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
1548 && uses_template_parms (DECL_TI_ARGS (t
)))
1553 if (uses_template_parms (TREE_TYPE (t
)))
1555 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
1560 return uses_template_parms (TREE_TYPE (t
));
1562 return uses_template_parms (TREE_OPERAND (t
, 0));
1564 /* template parm nodes */
1565 case TEMPLATE_TYPE_PARM
:
1566 case TEMPLATE_CONST_PARM
:
1569 /* simple type nodes */
1571 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
1573 return uses_template_parms (TYPE_MAX_VALUE (t
));
1585 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
1586 if (uses_template_parms (TREE_VALUE (v
)))
1598 /* Non-error_mark_node ERROR_MARKs are bad things. */
1599 my_friendly_assert (t
== error_mark_node
, 274);
1608 return uses_template_parms (TREE_OPERAND (t
, 0));
1611 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
1612 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
1613 return uses_template_parms (TREE_OPERAND (t
, 1));
1617 case REINTERPRET_CAST_EXPR
:
1618 case CONST_CAST_EXPR
:
1619 case STATIC_CAST_EXPR
:
1620 case DYNAMIC_CAST_EXPR
:
1628 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
1636 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
1637 if (uses_template_parms (TREE_OPERAND (t
, i
)))
1644 sorry ("testing %s for template parms",
1645 tree_code_name
[(int) TREE_CODE (t
)]);
1646 my_friendly_abort (82);
1652 static struct tinst_level
*current_tinst_level
= 0;
1653 static struct tinst_level
*free_tinst_level
= 0;
1654 static int tinst_depth
= 0;
1655 extern int max_tinst_depth
;
1656 #ifdef GATHER_STATISTICS
1657 int depth_reached
= 0;
1661 push_tinst_level (d
)
1664 struct tinst_level
*new;
1666 if (tinst_depth
>= max_tinst_depth
)
1668 struct tinst_level
*p
= current_tinst_level
;
1670 char *file
= input_filename
;
1672 error ("template instantiation depth exceeds maximum of %d",
1674 error (" (use -ftemplate-depth-NN to increase the maximum)");
1675 cp_error (" instantiating `%D'", d
);
1677 for (; p
; p
= p
->next
)
1679 cp_error (" instantiated from `%D'", p
->decl
);
1681 input_filename
= p
->file
;
1683 error (" instantiated from here");
1686 input_filename
= file
;
1691 if (free_tinst_level
)
1693 new = free_tinst_level
;
1694 free_tinst_level
= new->next
;
1697 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
1701 new->file
= input_filename
;
1702 new->next
= current_tinst_level
;
1703 current_tinst_level
= new;
1706 #ifdef GATHER_STATISTICS
1707 if (tinst_depth
> depth_reached
)
1708 depth_reached
= tinst_depth
;
1717 struct tinst_level
*old
= current_tinst_level
;
1719 current_tinst_level
= old
->next
;
1720 old
->next
= free_tinst_level
;
1721 free_tinst_level
= old
;
1725 struct tinst_level
*
1728 struct tinst_level
*p
= current_tinst_level
;
1731 for (; p
->next
; p
= p
->next
)
1737 instantiate_class_template (type
)
1740 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
1742 if (type
== error_mark_node
)
1743 return error_mark_node
;
1745 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
1747 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
1750 template = TI_TEMPLATE (template_info
);
1751 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
1752 args
= TI_ARGS (template_info
);
1754 t
= most_specialized_class
1755 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
1757 if (t
== error_mark_node
)
1759 char *str
= "candidates are:";
1760 cp_error ("ambiguous class template instantiation for `%#T'", type
);
1761 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
1763 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
1765 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
1769 TYPE_BEING_DEFINED (type
) = 1;
1770 return error_mark_node
;
1773 pattern
= TREE_TYPE (t
);
1775 pattern
= TREE_TYPE (template);
1777 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1781 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
1783 TYPE_BEING_DEFINED (type
) = 1;
1785 if (! push_tinst_level (type
))
1788 maybe_push_to_top_level (uses_template_parms (type
));
1789 pushclass (type
, 0);
1791 if (flag_external_templates
)
1793 if (flag_alt_external_templates
)
1795 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1796 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1797 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1798 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1799 && CLASSTYPE_INTERFACE_KNOWN (type
);
1803 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1804 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1805 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1806 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1807 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1808 && CLASSTYPE_INTERFACE_KNOWN (type
);
1813 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1814 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1817 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1818 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1819 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1820 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1821 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1822 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1823 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1824 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1825 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1826 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1827 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1828 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1829 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1830 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1831 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1832 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1833 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1834 TYPE_USES_COMPLEX_INHERITANCE (type
)
1835 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1836 TYPE_USES_MULTIPLE_INHERITANCE (type
)
1837 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
1838 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1839 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1840 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
1841 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
1844 tree binfo
= TYPE_BINFO (type
);
1845 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1851 int len
= TREE_VEC_LENGTH (pbases
);
1852 bases
= make_tree_vec (len
);
1853 for (i
= 0; i
< len
; ++i
)
1857 TREE_VEC_ELT (bases
, i
) = elt
1858 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
1859 TREE_VEC_LENGTH (args
), NULL_TREE
);
1860 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1862 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
1864 ("base type `%T' of `%T' fails to be a struct or class type",
1865 TREE_TYPE (elt
), type
);
1866 else if (! uses_template_parms (type
)
1867 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
1869 cp_error ("base class `%T' of `%T' has incomplete type",
1870 TREE_TYPE (elt
), type
);
1872 /* Don't initialize this until the vector is filled out, or
1873 lookups will crash. */
1874 BINFO_BASETYPES (binfo
) = bases
;
1878 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1880 field_chain
= &TYPE_FIELDS (type
);
1882 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1884 tree name
= TREE_PURPOSE (t
);
1885 tree tag
= TREE_VALUE (t
);
1887 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1888 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1890 tree e
, newtag
= tsubst_enum (tag
, args
,
1891 TREE_VEC_LENGTH (args
), field_chain
);
1893 while (*field_chain
)
1895 DECL_FIELD_CONTEXT (*field_chain
) = type
;
1896 field_chain
= &TREE_CHAIN (*field_chain
);
1901 TREE_VEC_LENGTH (args
), NULL_TREE
);
1904 /* Don't replace enum constants here. */
1905 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1906 if (TREE_CODE (t
) != CONST_DECL
)
1908 tree r
= tsubst (t
, args
,
1909 TREE_VEC_LENGTH (args
), NULL_TREE
);
1910 if (TREE_CODE (r
) == VAR_DECL
)
1912 if (! uses_template_parms (r
))
1913 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1914 /* Perhaps I should do more of grokfield here. */
1916 DECL_IN_AGGR_P (r
) = 1;
1917 DECL_EXTERNAL (r
) = 1;
1918 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1922 field_chain
= &TREE_CHAIN (r
);
1925 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1926 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
1928 if (DECL_CONSTRUCTOR_P (t
))
1929 grok_ctor_properties (type
, t
);
1930 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
1931 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
1934 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1935 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1936 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
1939 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
1940 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
1941 TREE_VEC_LENGTH (args
), NULL_TREE
);
1943 /* This does injection for friend classes. */
1944 for (; d
; d
= TREE_CHAIN (d
))
1945 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1947 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
1948 TREE_VEC_LENGTH (args
), NULL_TREE
);
1950 for (; d
; d
= TREE_CHAIN (d
))
1952 tree t
= TREE_VALUE (d
);
1954 if (TREE_CODE (t
) == TYPE_DECL
)
1955 /* Already injected. */;
1961 if (! uses_template_parms (type
))
1964 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1965 if (TREE_CODE (tmp
) == FIELD_DECL
)
1967 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
1968 require_complete_type (tmp
);
1971 type
= finish_struct_1 (type
, 0);
1972 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
1974 repo_template_used (type
);
1975 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
1976 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
1980 TYPE_SIZE (type
) = integer_zero_node
;
1981 CLASSTYPE_METHOD_VEC (type
)
1982 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1985 TYPE_BEING_DEFINED (type
) = 0;
1988 pop_from_top_level ();
1998 if (t1
== NULL_TREE
)
1999 return t2
== NULL_TREE
;
2000 if (t2
== NULL_TREE
)
2002 /* Don't care if one declares its arg const and the other doesn't -- the
2003 main variant of the arg type is all that matters. */
2004 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
2005 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
2007 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
2011 lookup_nested_type_by_name (ctype
, name
)
2016 complete_type (ctype
);
2018 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
2020 if (name
== TREE_PURPOSE (t
)
2021 /* this catches typedef enum { foo } bar; */
2022 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
2023 return TREE_VALUE (t
);
2029 tsubst (t
, args
, nargs
, in_decl
)
2036 if (t
== NULL_TREE
|| t
== error_mark_node
2037 || t
== integer_type_node
2038 || t
== void_type_node
2039 || t
== char_type_node
)
2042 type
= TREE_TYPE (t
);
2043 if (type
== unknown_type_node
)
2044 my_friendly_abort (42);
2045 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
2046 && TREE_CODE (t
) != TYPENAME_TYPE
)
2047 type
= tsubst (type
, args
, nargs
, in_decl
);
2049 switch (TREE_CODE (t
))
2052 if (TYPE_PTRMEMFUNC_P (t
))
2054 tree r
= build_ptrmemfunc_type
2055 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
2056 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2060 /* else fall through */
2062 if (uses_template_parms (t
))
2064 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
2065 tree r
= lookup_template_class (t
, argvec
, in_decl
);
2066 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2070 /* else fall through */
2072 case IDENTIFIER_NODE
:
2085 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2086 if (ctx
== NULL_TREE
)
2088 else if (ctx
== current_function_decl
)
2089 return lookup_name (TYPE_IDENTIFIER (t
), 1);
2091 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
2095 if (t
== integer_type_node
)
2098 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
2099 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
2103 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
2104 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
2105 if (processing_template_decl
)
2107 tree itype
= make_node (INTEGER_TYPE
);
2108 TYPE_MIN_VALUE (itype
) = size_zero_node
;
2109 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
2114 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
2115 return build_index_2_type (size_zero_node
, max
);
2118 case TEMPLATE_TYPE_PARM
:
2119 case TEMPLATE_CONST_PARM
:
2124 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2126 idx
= TEMPLATE_TYPE_IDX (t
);
2127 level
= TEMPLATE_TYPE_LEVEL (t
);
2131 idx
= TEMPLATE_CONST_IDX (t
);
2132 level
= TEMPLATE_CONST_LEVEL (t
);
2135 if (TREE_VEC_LENGTH (args
) > 0)
2137 tree arg
= NULL_TREE
;
2139 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
2141 if (TREE_VEC_LENGTH (args
) >= level
- 1)
2143 (TREE_VEC_ELT (args
, level
- 1), idx
);
2145 else if (level
== 1)
2146 arg
= TREE_VEC_ELT (args
, idx
);
2148 if (arg
!= NULL_TREE
)
2150 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2151 return cp_build_type_variant
2152 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
2153 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
2159 /* If we get here, we must have been looking at a parm for a
2160 more deeply nested template. */
2161 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
2162 && TEMPLATE_CONST_LEVEL (t
) > 1)
2163 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
2164 && TEMPLATE_TYPE_LEVEL (t
) > 1),
2171 /* We can get here when processing a member template function
2172 of a template class. */
2174 tree decl
= DECL_TEMPLATE_RESULT (t
);
2180 /* We might already have an instance of this template. */
2181 tree instances
= DECL_TEMPLATE_INSTANTIATIONS (t
);
2182 tree ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, in_decl
);
2184 for (; instances
; instances
= TREE_CHAIN (instances
))
2185 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances
)) == ctx
)
2186 return TREE_VALUE (instances
);
2188 /* Make a new template decl. It will be similar to the
2189 original, but will record the current template arguments.
2190 We also create a new function declaration, which is just
2191 like the old one, but points to this new template, rather
2192 than the old one. */
2193 tmpl
= copy_node (t
);
2194 copy_lang_decl (tmpl
);
2195 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
2196 DECL_CHAIN (tmpl
) = NULL_TREE
;
2197 TREE_CHAIN (tmpl
) = NULL_TREE
;
2198 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
2199 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
2200 DECL_RESULT (tmpl
) = new_decl
;
2201 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
2202 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
2203 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
2204 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
2206 /* The template parameters for this new template are all the
2207 template parameters for the old template, except the
2208 outermost level of parameters. */
2209 DECL_TEMPLATE_PARMS (tmpl
)
2210 = copy_node (DECL_TEMPLATE_PARMS (tmpl
));
2211 for (parms
= DECL_TEMPLATE_PARMS (tmpl
);
2212 TREE_CHAIN (parms
) != NULL_TREE
;
2213 parms
= TREE_CHAIN (parms
))
2214 TREE_CHAIN (parms
) = copy_node (TREE_CHAIN (parms
));
2216 /* Record this partial instantiation. */
2217 DECL_TEMPLATE_INSTANTIATIONS (t
)
2218 = perm_tree_cons (NULL_TREE
, tmpl
,
2219 DECL_TEMPLATE_INSTANTIATIONS (t
));
2221 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
2232 if (DECL_CONTEXT (t
) != NULL_TREE
2233 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
2235 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
2239 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
2240 type
= tsubst (type
, args
, nargs
, in_decl
);
2246 type
= tsubst (type
, args
, nargs
, in_decl
);
2249 if (type
== TREE_TYPE (t
)
2250 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
2257 /* Do we already have this instantiation? */
2258 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2260 tree tmpl
= DECL_TI_TEMPLATE (t
);
2261 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2263 for (; decls
; decls
= TREE_CHAIN (decls
))
2264 if (TREE_TYPE (TREE_VALUE (decls
)) == type
2265 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
2266 return TREE_VALUE (decls
);
2269 /* We do NOT check for matching decls pushed separately at this
2270 point, as they may not represent instantiations of this
2271 template, and in any case are considered separate under the
2272 discrete model. Instead, see add_maybe_template. */
2276 TREE_TYPE (r
) = type
;
2279 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
2280 DECL_CLASS_CONTEXT (r
) = ctx
;
2282 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
2283 IDENTIFIER_POINTER (DECL_NAME (r
)),
2284 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
2286 /* Type-conversion operator. Reconstruct the name, in
2287 case it's the name of one of the template's parameters. */
2288 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
2291 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
2293 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
2294 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
2295 buf
= (char *) alloca (strlen (dbuf
)
2296 + sizeof (DESTRUCTOR_DECL_PREFIX
));
2297 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
2300 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
2304 /* Instantiations of template functions must be mangled
2305 specially, in order to conform to 14.5.5.1
2306 [temp.over.link]. We use in_decl below rather than
2307 DECL_TI_TEMPLATE (r) because the latter is set to
2308 NULL_TREE in instantiate_decl. */
2312 if (DECL_TEMPLATE_INFO (r
))
2313 tmpl
= DECL_TI_TEMPLATE (r
);
2317 /* tmpl will be NULL if this is a specialization of a
2318 member template of a template class. */
2319 if (name_mangling_version
< 1
2320 || tmpl
== NULL_TREE
2321 || (member
&& !is_member_template (tmpl
)
2322 && !DECL_TEMPLATE_INFO (tmpl
)))
2324 arg_types
= TYPE_ARG_TYPES (type
);
2325 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2326 arg_types
= hash_tree_chain
2327 (build_pointer_type (DECL_CONTEXT (r
)),
2330 DECL_ASSEMBLER_NAME (r
)
2331 = build_decl_overload (DECL_NAME (r
), arg_types
,
2336 /* We pass the outermost template parameters to
2337 build_template_decl_overload since the innermost
2338 template parameters are still just template
2339 parameters; there are no corresponding substitution
2341 /* FIXME The messed up thing here is that we get here with
2342 full args and only one level of parms. This is necessary
2343 because when we partially instantiate a member template,
2344 even though there's really only one level of parms left
2345 we re-use the parms from the original template, which
2346 have level 2. When this is fixed we can remove the
2347 add_to_template_args from instantiate_template. */
2348 tree tparms
= DECL_TEMPLATE_PARMS (tmpl
);
2350 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
2351 tparms
= TREE_CHAIN (tparms
);
2353 my_friendly_assert (tparms
!= NULL_TREE
2354 && TREE_CODE (tparms
) == TREE_LIST
,
2356 tparms
= TREE_VALUE (tparms
);
2358 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
2359 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2360 arg_types
= hash_tree_chain
2361 (build_pointer_type (DECL_CONTEXT (r
)),
2364 DECL_ASSEMBLER_NAME (r
)
2365 = build_template_decl_overload
2366 (DECL_NAME (r
), arg_types
,
2367 TREE_TYPE (TREE_TYPE (tmpl
)),
2369 TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
2370 ? TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1) :
2376 make_decl_rtl (r
, NULL_PTR
, 1);
2378 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
2379 DECL_MAIN_VARIANT (r
) = r
;
2380 DECL_RESULT (r
) = NULL_TREE
;
2381 DECL_INITIAL (r
) = NULL_TREE
;
2383 TREE_STATIC (r
) = 0;
2384 TREE_PUBLIC (r
) = 1;
2385 DECL_EXTERNAL (r
) = 1;
2386 DECL_INTERFACE_KNOWN (r
) = 0;
2387 DECL_DEFER_OUTPUT (r
) = 0;
2388 TREE_CHAIN (r
) = NULL_TREE
;
2389 DECL_CHAIN (r
) = NULL_TREE
;
2391 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
2392 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
2394 /* Look for matching decls for the moment. */
2395 if (! member
&& ! flag_ansi_overloading
)
2397 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
2400 if (decls
== NULL_TREE
)
2402 else if (is_overloaded_fn (decls
))
2403 for (decls
= get_first_fn (decls
); decls
;
2404 decls
= DECL_CHAIN (decls
))
2406 if (TREE_CODE (decls
) == FUNCTION_DECL
2407 && TREE_TYPE (decls
) == type
)
2416 int dcl_only
= ! DECL_INITIAL (d
);
2418 DECL_INITIAL (r
) = error_mark_node
;
2419 duplicate_decls (r
, d
);
2422 DECL_INITIAL (r
) = 0;
2426 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2428 tree tmpl
= DECL_TI_TEMPLATE (t
);
2429 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2430 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2432 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
2433 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
2435 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2436 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2438 /* If we have a preexisting version of this function, don't expand
2439 the template version, use the other instead. */
2440 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
2441 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
2443 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2445 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
2446 = tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
2449 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2450 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2452 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
2453 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
2460 tree r
= copy_node (t
);
2461 TREE_TYPE (r
) = type
;
2462 DECL_INITIAL (r
) = TREE_TYPE (r
);
2463 DECL_CONTEXT (r
) = NULL_TREE
;
2464 #ifdef PROMOTE_PROTOTYPES
2465 if ((TREE_CODE (type
) == INTEGER_TYPE
2466 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2467 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
2468 DECL_ARG_TYPE (r
) = integer_type_node
;
2471 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
2477 tree r
= copy_node (t
);
2478 TREE_TYPE (r
) = type
;
2481 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
2483 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2484 TREE_CHAIN (r
) = NULL_TREE
;
2490 tree r
= copy_node (t
);
2492 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2493 TREE_CHAIN (r
) = NULL_TREE
;
2500 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2502 /* Do we already have this instantiation? */
2503 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2505 tree tmpl
= DECL_TI_TEMPLATE (t
);
2506 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2508 for (; decls
; decls
= TREE_CHAIN (decls
))
2509 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
2510 return TREE_VALUE (decls
);
2514 TREE_TYPE (r
) = type
;
2515 DECL_CONTEXT (r
) = ctx
;
2516 if (TREE_STATIC (r
))
2517 DECL_ASSEMBLER_NAME (r
)
2518 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
2520 /* Don't try to expand the initializer until someone tries to use
2521 this variable; otherwise we run into circular dependencies. */
2522 DECL_INITIAL (r
) = NULL_TREE
;
2527 if (DECL_LANG_SPECIFIC (r
))
2530 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
2533 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2535 tree tmpl
= DECL_TI_TEMPLATE (t
);
2536 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2537 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2539 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2540 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2541 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2543 TREE_CHAIN (r
) = NULL_TREE
;
2548 if (t
== TYPE_NAME (TREE_TYPE (t
)))
2549 return TYPE_NAME (type
);
2552 tree r
= copy_node (t
);
2553 TREE_TYPE (r
) = type
;
2554 DECL_CONTEXT (r
) = current_class_type
;
2555 TREE_CHAIN (r
) = NULL_TREE
;
2561 tree purpose
, value
, chain
, result
;
2562 int via_public
, via_virtual
, via_protected
;
2564 if (t
== void_list_node
)
2567 via_public
= TREE_VIA_PUBLIC (t
);
2568 via_protected
= TREE_VIA_PROTECTED (t
);
2569 via_virtual
= TREE_VIA_VIRTUAL (t
);
2571 purpose
= TREE_PURPOSE (t
);
2573 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
2574 value
= TREE_VALUE (t
);
2576 value
= tsubst (value
, args
, nargs
, in_decl
);
2577 chain
= TREE_CHAIN (t
);
2578 if (chain
&& chain
!= void_type_node
)
2579 chain
= tsubst (chain
, args
, nargs
, in_decl
);
2580 if (purpose
== TREE_PURPOSE (t
)
2581 && value
== TREE_VALUE (t
)
2582 && chain
== TREE_CHAIN (t
))
2584 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
2585 purpose
, value
, chain
);
2586 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
2590 if (type
!= NULL_TREE
)
2596 if (type
== TREE_TYPE (t
))
2599 TREE_TYPE (t
) = complete_type (type
);
2600 if (IS_AGGR_TYPE (type
))
2602 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
2603 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
2604 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
2605 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
2610 /* Otherwise, a vector of template arguments. */
2612 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
2613 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
2615 bzero ((char *) elts
, len
* sizeof (tree
));
2617 for (i
= 0; i
< len
; i
++)
2619 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
2621 if (TREE_CODE_CLASS (TREE_CODE (elts
[i
])) != 't'
2622 && !uses_template_parms (elts
[i
]))
2624 /* Sometimes, one of the args was an expression involving a
2625 template constant parameter, like N - 1. Now that we've
2626 tsubst'd, we might have something like 2 - 1. This will
2627 confuse lookup_template_class, so we do constant folding
2628 here. We have to unset processing_template_decl, to
2629 fool build_expr_from_tree() into building an actual
2632 int saved_processing_template_decl
= processing_template_decl
;
2633 processing_template_decl
= 0;
2634 elts
[i
] = fold (build_expr_from_tree (elts
[i
]));
2635 processing_template_decl
= saved_processing_template_decl
;
2638 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
2645 t
= make_tree_vec (len
);
2646 for (i
= 0; i
< len
; i
++)
2647 TREE_VEC_ELT (t
, i
) = elts
[i
];
2652 case REFERENCE_TYPE
:
2655 enum tree_code code
;
2656 if (type
== TREE_TYPE (t
))
2659 code
= TREE_CODE (t
);
2660 if (code
== POINTER_TYPE
)
2661 r
= build_pointer_type (type
);
2663 r
= build_reference_type (type
);
2664 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
2665 /* Will this ever be needed for TYPE_..._TO values? */
2670 return build_offset_type
2671 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
2675 tree values
= TYPE_ARG_TYPES (t
);
2676 tree context
= TYPE_CONTEXT (t
);
2677 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
2680 /* Don't bother recursing if we know it won't change anything. */
2681 if (values
!= void_list_node
)
2683 /* This should probably be rewritten to use hash_tree_cons for
2684 the memory savings. */
2685 tree first
= NULL_TREE
;
2688 for (; values
&& values
!= void_list_node
;
2689 values
= TREE_CHAIN (values
))
2691 tree value
= TYPE_MAIN_VARIANT (type_decays_to
2692 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
2693 /* Don't instantiate default args unless they are used.
2694 Handle it in build_over_call instead. */
2695 tree purpose
= TREE_PURPOSE (values
);
2696 tree x
= build_tree_list (purpose
, value
);
2699 TREE_CHAIN (last
) = x
;
2705 if (values
== void_list_node
)
2706 TREE_CHAIN (last
) = void_list_node
;
2711 context
= tsubst (context
, args
, nargs
, in_decl
);
2712 /* Could also optimize cases where return value and
2713 values have common elements (e.g., T min(const &T, const T&). */
2715 /* If the above parameters haven't changed, just return the type. */
2716 if (type
== TREE_TYPE (t
)
2717 && values
== TYPE_VALUES (t
)
2718 && context
== TYPE_CONTEXT (t
))
2721 /* Construct a new type node and return it. */
2722 if (TREE_CODE (t
) == FUNCTION_TYPE
2723 && context
== NULL_TREE
)
2725 fntype
= build_function_type (type
, values
);
2727 else if (context
== NULL_TREE
)
2729 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
2730 args
, nargs
, in_decl
);
2731 fntype
= build_cplus_method_type (base
, type
,
2732 TREE_CHAIN (values
));
2736 fntype
= make_node (TREE_CODE (t
));
2737 TREE_TYPE (fntype
) = type
;
2738 TYPE_CONTEXT (fntype
) = context
;
2739 TYPE_VALUES (fntype
) = values
;
2740 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
2741 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
2742 TYPE_MODE (fntype
) = TYPE_MODE (t
);
2743 if (TYPE_METHOD_BASETYPE (t
))
2744 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
2745 args
, nargs
, in_decl
);
2746 /* Need to generate hash value. */
2747 my_friendly_abort (84);
2749 fntype
= build_type_variant (fntype
,
2754 raises
= tsubst (raises
, args
, nargs
, in_decl
);
2755 fntype
= build_exception_variant (fntype
, raises
);
2761 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
2763 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
2765 r
= build_cplus_array_type (type
, domain
);
2771 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
2772 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2773 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
2777 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
2778 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
2782 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2783 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
2784 return cp_build_type_variant
2785 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
2786 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
2790 return make_pointer_declarator
2791 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2794 return make_reference_declarator
2795 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2798 return build_parse_node
2799 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2800 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2803 return make_call_declarator
2804 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2805 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2806 TREE_OPERAND (t
, 2),
2807 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
2810 return build_parse_node
2811 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2812 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2815 sorry ("use of `%s' in template",
2816 tree_code_name
[(int) TREE_CODE (t
)]);
2817 return error_mark_node
;
2824 emit_line_note (input_filename
, lineno
);
2828 expand_start_bindings (0);
2835 int saved_warn_unused
;
2837 if (processing_template_decl
)
2839 saved_warn_unused
= warn_unused
;
2842 expand_end_bindings (getdecls (), kept_level_p (), 0);
2843 if (processing_template_decl
)
2844 warn_unused
= saved_warn_unused
;
2845 t
= poplevel (kept_level_p (), 1, 0);
2851 tsubst_copy (t
, args
, nargs
, in_decl
)
2856 enum tree_code code
;
2858 if (t
== NULL_TREE
|| t
== error_mark_node
)
2861 code
= TREE_CODE (t
);
2866 return do_identifier (DECL_NAME (t
), 0);
2870 if (DECL_CONTEXT (t
))
2872 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2873 if (ctx
== current_function_decl
)
2874 return lookup_name (DECL_NAME (t
), 0);
2875 else if (ctx
!= DECL_CONTEXT (t
))
2876 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
2882 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2883 t
= tsubst (t
, args
, nargs
, in_decl
);
2888 if (is_member_template (t
))
2889 return tsubst (t
, args
, nargs
, in_decl
);
2894 case IDENTIFIER_NODE
:
2895 return do_identifier (t
, 0);
2899 case REINTERPRET_CAST_EXPR
:
2900 case CONST_CAST_EXPR
:
2901 case STATIC_CAST_EXPR
:
2902 case DYNAMIC_CAST_EXPR
:
2904 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
2905 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2908 case PREDECREMENT_EXPR
:
2909 case PREINCREMENT_EXPR
:
2910 case POSTDECREMENT_EXPR
:
2911 case POSTINCREMENT_EXPR
:
2913 case TRUTH_NOT_EXPR
:
2916 case CONVERT_EXPR
: /* Unary + */
2923 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2928 case TRUNC_DIV_EXPR
:
2930 case FLOOR_DIV_EXPR
:
2931 case ROUND_DIV_EXPR
:
2932 case EXACT_DIV_EXPR
:
2934 case BIT_ANDTC_EXPR
:
2937 case TRUNC_MOD_EXPR
:
2938 case FLOOR_MOD_EXPR
:
2939 case TRUTH_ANDIF_EXPR
:
2940 case TRUTH_ORIF_EXPR
:
2941 case TRUTH_AND_EXPR
:
2962 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2963 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2967 tree fn
= TREE_OPERAND (t
, 0);
2968 if (really_overloaded_fn (fn
))
2969 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
2971 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
2973 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2977 case METHOD_CALL_EXPR
:
2979 tree name
= TREE_OPERAND (t
, 0);
2980 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2982 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2983 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2985 else if (TREE_CODE (name
) == SCOPE_REF
2986 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
2988 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2989 name
= TREE_OPERAND (name
, 1);
2990 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2991 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2992 name
= build_nt (SCOPE_REF
, base
, name
);
2995 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2997 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2998 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
3005 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3006 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3007 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3012 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3013 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3014 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3015 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
3022 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3023 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3024 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
3025 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
3029 case TEMPLATE_ID_EXPR
:
3031 tree r
= lookup_template_function
3032 (tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3033 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3039 tree purpose
, value
, chain
;
3041 if (t
== void_list_node
)
3044 purpose
= TREE_PURPOSE (t
);
3046 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
3047 value
= TREE_VALUE (t
);
3049 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
3050 chain
= TREE_CHAIN (t
);
3051 if (chain
&& chain
!= void_type_node
)
3052 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
3053 if (purpose
== TREE_PURPOSE (t
)
3054 && value
== TREE_VALUE (t
)
3055 && chain
== TREE_CHAIN (t
))
3057 return tree_cons (purpose
, value
, chain
);
3064 case TEMPLATE_TYPE_PARM
:
3065 case TEMPLATE_CONST_PARM
:
3067 case REFERENCE_TYPE
:
3073 return tsubst (t
, args
, nargs
, in_decl
);
3075 case IDENTIFIER_NODE
:
3076 if (IDENTIFIER_TYPENAME_P (t
))
3077 return build_typename_overload
3078 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
3084 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
3085 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
3093 tsubst_expr (t
, args
, nargs
, in_decl
)
3098 if (t
== NULL_TREE
|| t
== error_mark_node
)
3101 if (processing_template_decl
)
3102 return tsubst_copy (t
, args
, nargs
, in_decl
);
3104 switch (TREE_CODE (t
))
3107 lineno
= TREE_COMPLEXITY (t
);
3108 emit_line_note (input_filename
, lineno
);
3110 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3115 lineno
= TREE_COMPLEXITY (t
);
3116 emit_line_note (input_filename
, lineno
);
3117 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3118 /* Do default conversion if safe and possibly important,
3119 in case within ({...}). */
3120 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
3121 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
3122 t
= default_conversion (t
);
3123 cplus_expand_expr_stmt (t
);
3130 int i
= suspend_momentary ();
3133 lineno
= TREE_COMPLEXITY (t
);
3134 emit_line_note (input_filename
, lineno
);
3136 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3137 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3138 TREE_OPERAND (t
, 2) != 0);
3139 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3141 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3142 resume_momentary (i
);
3149 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
3150 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3151 int cond_scope
= (TREE_OPERAND (t
, 1)
3152 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
3154 lineno
= TREE_COMPLEXITY (t
);
3155 emit_line_note (input_filename
, lineno
);
3158 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
3159 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3161 emit_line_note (input_filename
, lineno
);
3162 expand_start_loop_continue_elsewhere (1);
3166 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3167 emit_line_note (input_filename
, lineno
);
3169 expand_exit_loop_if_false (0, condition_conversion (tmp
));
3173 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
3176 emit_line_note (input_filename
, lineno
);
3177 expand_loop_continue_here ();
3178 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3180 cplus_expand_expr_stmt (tmp
);
3193 lineno
= TREE_COMPLEXITY (t
);
3195 emit_line_note (input_filename
, lineno
);
3196 expand_start_loop (1);
3198 cond
= TREE_OPERAND (t
, 0);
3199 if (TREE_CODE (cond
) == DECL_STMT
)
3201 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
3202 emit_line_note (input_filename
, lineno
);
3203 expand_exit_loop_if_false (0, condition_conversion (cond
));
3205 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
3207 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3219 lineno
= TREE_COMPLEXITY (t
);
3221 emit_line_note (input_filename
, lineno
);
3222 expand_start_loop_continue_elsewhere (1);
3224 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3225 expand_loop_continue_here ();
3227 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3228 emit_line_note (input_filename
, lineno
);
3229 expand_exit_loop_if_false (0, condition_conversion (cond
));
3240 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3242 lineno
= TREE_COMPLEXITY (t
);
3245 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3246 emit_line_note (input_filename
, lineno
);
3247 expand_start_cond (condition_conversion (tmp
), 0);
3249 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3250 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3252 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
3254 expand_start_else ();
3255 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3269 tree substmt
= TREE_OPERAND (t
, 0);
3271 lineno
= TREE_COMPLEXITY (t
);
3273 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3276 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
3277 tsubst_expr (substmt
, args
, nargs
, in_decl
);
3279 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3285 lineno
= TREE_COMPLEXITY (t
);
3286 emit_line_note (input_filename
, lineno
);
3287 if (! expand_exit_something ())
3288 error ("break statement not within loop or switch");
3292 lineno
= TREE_COMPLEXITY (t
);
3293 emit_line_note (input_filename
, lineno
);
3294 if (! expand_continue_loop (0))
3295 error ("continue statement not within a loop");
3301 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3303 lineno
= TREE_COMPLEXITY (t
);
3306 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3307 emit_line_note (input_filename
, lineno
);
3308 c_expand_start_case (val
);
3311 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3312 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3314 expand_end_case (val
);
3325 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3326 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3330 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
3337 lineno
= TREE_COMPLEXITY (t
);
3338 emit_line_note (input_filename
, lineno
);
3339 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
3341 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
3342 TREE_USED (decl
) = 1;
3346 expand_computed_goto
3347 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3351 lineno
= TREE_COMPLEXITY (t
);
3352 emit_line_note (input_filename
, lineno
);
3353 expand_start_try_stmts ();
3354 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3355 expand_start_all_catch ();
3357 tree handler
= TREE_OPERAND (t
, 1);
3358 for (; handler
; handler
= TREE_CHAIN (handler
))
3359 tsubst_expr (handler
, args
, nargs
, in_decl
);
3361 expand_end_all_catch ();
3365 lineno
= TREE_COMPLEXITY (t
);
3367 if (TREE_OPERAND (t
, 0))
3369 tree d
= TREE_OPERAND (t
, 0);
3370 expand_start_catch_block
3371 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
3372 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
3375 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
3376 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3377 expand_end_catch_block ();
3382 lineno
= TREE_COMPLEXITY (t
);
3384 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
3385 tsubst_enum (t
, args
, nargs
, NULL
);
3389 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
3395 instantiate_template (tmpl
, targ_ptr
)
3396 tree tmpl
, targ_ptr
;
3400 struct obstack
*old_fmp_obstack
;
3401 extern struct obstack
*function_maybepermanent_obstack
;
3403 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
3405 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3409 /* Check to see if there is a matching specialization. */
3410 for (specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
3412 specs
= TREE_CHAIN (specs
))
3413 if (comp_template_args (TREE_PURPOSE (specs
), targ_ptr
))
3414 return TREE_VALUE (specs
);
3417 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3418 old_fmp_obstack
= function_maybepermanent_obstack
;
3419 function_maybepermanent_obstack
= &permanent_obstack
;
3421 len
= DECL_NTPARMS (tmpl
);
3426 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
3427 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
3429 tree nt
= target_type (t
);
3430 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
3432 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
3433 cp_error (" trying to instantiate `%D'", tmpl
);
3434 fndecl
= error_mark_node
;
3438 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
3440 targ_ptr
= copy_to_permanent (targ_ptr
);
3442 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
3443 targ_ptr
= add_to_template_args (DECL_TI_ARGS (tmpl
), targ_ptr
);
3445 /* substitute template parameters */
3446 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
3448 if (flag_external_templates
)
3449 add_pending_template (fndecl
);
3452 function_maybepermanent_obstack
= old_fmp_obstack
;
3458 /* Push the name of the class template into the scope of the instantiation. */
3461 overload_template_name (type
)
3464 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
3467 if (IDENTIFIER_CLASS_VALUE (id
)
3468 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
3471 decl
= build_decl (TYPE_DECL
, id
, type
);
3472 SET_DECL_ARTIFICIAL (decl
);
3473 pushdecl_class_level (decl
);
3476 /* Like type_unification but designed specially to handle conversion
3480 fn_type_unification (fn
, explicit_targs
, targs
, args
, return_type
, strict
)
3481 tree fn
, explicit_targs
, targs
, args
, return_type
;
3485 tree fn_arg_types
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
3486 tree decl_arg_types
= args
;
3488 my_friendly_assert (TREE_CODE (fn
) == TEMPLATE_DECL
, 0);
3490 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn
)))
3492 /* This is a template conversion operator. Use the return types
3493 as well as the argument types. */
3494 fn_arg_types
= scratch_tree_cons (NULL_TREE
,
3495 TREE_TYPE (TREE_TYPE (fn
)),
3497 decl_arg_types
= scratch_tree_cons (NULL_TREE
,
3502 i
= type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn
),
3503 &TREE_VEC_ELT (targs
, 0),
3513 /* Type unification.
3515 We have a function template signature with one or more references to
3516 template parameters, and a parameter list we wish to fit to this
3517 template. If possible, produce a list of parameters for the template
3518 which will cause it to fit the supplied parameter list.
3520 Return zero for success, 2 for an incomplete match that doesn't resolve
3521 all the types, and 1 for complete failure. An error message will be
3522 printed only for an incomplete match.
3524 TPARMS[NTPARMS] is an array of template parameter types;
3525 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3526 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3527 and ARGS is the argument list we're trying to match against it.
3529 If SUBR is 1, we're being called recursively (to unify the arguments of
3530 a function or method parameter of a function template), so don't zero
3531 out targs and don't fail on an incomplete match.
3533 If STRICT is 1, the match must be exact (for casts of overloaded
3534 addresses, explicit instantiation, and more_specialized). */
3537 type_unification (tparms
, targs
, parms
, args
, targs_in
, nsubsts
,
3538 strict
, allow_incomplete
)
3539 tree tparms
, *targs
, parms
, args
, targs_in
;
3540 int *nsubsts
, strict
, allow_incomplete
;
3542 int ntparms
= TREE_VEC_LENGTH (tparms
);
3547 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
3549 /* Insert any explicit template arguments. They are encoded as the
3550 operands of NOP_EXPRs so that unify can tell that they are
3551 explicit arguments. */
3552 for (i
= 0, t
= targs_in
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
), ++i
)
3553 targs
[i
] = build1 (NOP_EXPR
, NULL_TREE
, TREE_VALUE (t
));
3555 r
= type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, 0,
3556 strict
, allow_incomplete
);
3558 for (i
= 0, t
= targs_in
; t
!= NULL_TREE
; t
= TREE_CHAIN (t
), ++i
)
3559 if (TREE_CODE (targs
[i
]) == NOP_EXPR
)
3560 targs
[i
] = TREE_OPERAND (targs
[i
], 0);
3567 type_unification_real (tparms
, targs
, parms
, args
, nsubsts
, subr
,
3568 strict
, allow_incomplete
)
3569 tree tparms
, *targs
, parms
, args
;
3570 int *nsubsts
, subr
, strict
, allow_incomplete
;
3574 int ntparms
= TREE_VEC_LENGTH (tparms
);
3576 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
3577 my_friendly_assert (parms
== NULL_TREE
3578 || TREE_CODE (parms
) == TREE_LIST
, 290);
3579 /* ARGS could be NULL (via a call from parse.y to
3580 build_x_function_call). */
3582 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
3583 my_friendly_assert (ntparms
> 0, 292);
3586 && parms
!= void_list_node
3588 && args
!= void_list_node
)
3590 parm
= TREE_VALUE (parms
);
3591 parms
= TREE_CHAIN (parms
);
3592 arg
= TREE_VALUE (args
);
3593 args
= TREE_CHAIN (args
);
3595 if (arg
== error_mark_node
)
3597 if (arg
== unknown_type_node
)
3600 /* Conversions will be performed on a function argument that
3601 corresponds with a function parameter that contains only
3602 non-deducible template parameters and explicitly specified
3603 template parameters. */
3604 if (! uses_template_parms (parm
))
3608 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3609 type
= TREE_TYPE (arg
);
3618 if (comptypes (parm
, type
, 1))
3623 if (can_convert_arg (parm
, type
, arg
))
3628 if (can_convert (parm
, type
))
3636 if (TREE_CODE (arg
) == VAR_DECL
)
3637 arg
= TREE_TYPE (arg
);
3638 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
3639 arg
= TREE_TYPE (arg
);
3641 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
3643 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
3644 if (TREE_CODE (arg
) == TREE_LIST
3645 && TREE_TYPE (arg
) == unknown_type_node
3646 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
3648 int nsubsts
, ntparms
;
3651 /* Have to back unify here */
3652 arg
= TREE_VALUE (arg
);
3654 ntparms
= DECL_NTPARMS (arg
);
3655 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
3656 parm
= expr_tree_cons (NULL_TREE
, parm
, NULL_TREE
);
3658 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg
),
3660 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
3661 parm
, NULL_TREE
, &nsubsts
, strict
,
3664 arg
= TREE_TYPE (arg
);
3667 if (! subr
&& TREE_CODE (arg
) == REFERENCE_TYPE
)
3668 arg
= TREE_TYPE (arg
);
3670 if (! subr
&& TREE_CODE (parm
) != REFERENCE_TYPE
)
3672 if (TREE_CODE (arg
) == FUNCTION_TYPE
3673 || TREE_CODE (arg
) == METHOD_TYPE
)
3674 arg
= build_pointer_type (arg
);
3675 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
3676 arg
= build_pointer_type (TREE_TYPE (arg
));
3678 arg
= TYPE_MAIN_VARIANT (arg
);
3681 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
))
3689 /* Fail if we've reached the end of the parm list, and more args
3690 are present, and the parm list isn't variadic. */
3691 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
3693 /* Fail if parms are left and they don't have default values. */
3695 && parms
!= void_list_node
3696 && TREE_PURPOSE (parms
) == NULL_TREE
)
3699 for (i
= 0; i
< ntparms
; i
++)
3702 if (!allow_incomplete
)
3703 error ("incomplete type unification");
3709 /* Tail recursion is your friend. */
3712 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
, strict
)
3713 tree tparms
, *targs
, parm
, arg
;
3714 int *nsubsts
, ntparms
, strict
;
3718 /* I don't think this will do the right thing with respect to types.
3719 But the only case I've seen it in so far has been array bounds, where
3720 signedness is the only information lost, and I think that will be
3722 while (TREE_CODE (parm
) == NOP_EXPR
)
3723 parm
= TREE_OPERAND (parm
, 0);
3725 if (arg
== error_mark_node
)
3727 if (arg
== unknown_type_node
)
3732 switch (TREE_CODE (parm
))
3735 /* In a type which contains a nested-name-specifier, template
3736 argument values cannot be deduced for template parameters used
3737 within the nested-name-specifier. */
3740 case TEMPLATE_TYPE_PARM
:
3742 idx
= TEMPLATE_TYPE_IDX (parm
);
3743 /* Check for mixed types and values. */
3744 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
3747 if (!strict
&& targs
[idx
] != NULL_TREE
&&
3748 TREE_CODE (targs
[idx
]) == NOP_EXPR
)
3749 /* An explicit template argument. Don't even try to match
3750 here; the overload resolution code will manage check to
3751 see whether the call is legal. */
3754 if (strict
&& (TYPE_READONLY (arg
) < TYPE_READONLY (parm
)
3755 || TYPE_VOLATILE (arg
) < TYPE_VOLATILE (parm
)))
3758 /* Template type parameters cannot contain cv-quals; i.e.
3759 template <class T> void f (T& a, T& b) will not generate
3760 void f (const int& a, const int& b). */
3761 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
3762 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
3764 arg
= TYPE_MAIN_VARIANT (arg
);
3767 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
3768 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
3769 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
3772 /* Simple cases: Value already set, does match or doesn't. */
3773 if (targs
[idx
] == arg
3775 && TREE_CODE (targs
[idx
]) == NOP_EXPR
3776 && TREE_OPERAND (targs
[idx
], 0) == arg
))
3778 else if (targs
[idx
])
3782 case TEMPLATE_CONST_PARM
:
3784 idx
= TEMPLATE_CONST_IDX (parm
);
3787 int i
= cp_tree_equal (targs
[idx
], arg
);
3793 my_friendly_abort (42);
3796 targs
[idx
] = copy_to_permanent (arg
);
3800 if (TREE_CODE (arg
) == RECORD_TYPE
&& TYPE_PTRMEMFUNC_FLAG (arg
))
3801 return unify (tparms
, targs
, ntparms
, parm
,
3802 TYPE_PTRMEMFUNC_FN_TYPE (arg
), nsubsts
, strict
);
3804 if (TREE_CODE (arg
) != POINTER_TYPE
)
3806 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3809 case REFERENCE_TYPE
:
3810 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
3811 arg
= TREE_TYPE (arg
);
3812 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
,
3816 if (TREE_CODE (arg
) != ARRAY_TYPE
)
3818 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
3819 nsubsts
, strict
) != 0)
3821 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
3828 if (TREE_CODE (arg
) != TREE_CODE (parm
))
3831 if (TREE_CODE (parm
) == INTEGER_TYPE
)
3833 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
3834 && unify (tparms
, targs
, ntparms
, TYPE_MIN_VALUE (parm
),
3835 TYPE_MIN_VALUE (arg
), nsubsts
, strict
))
3837 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
3838 && unify (tparms
, targs
, ntparms
, TYPE_MAX_VALUE (parm
),
3839 TYPE_MAX_VALUE (arg
), nsubsts
, strict
))
3842 else if (TREE_CODE (parm
) == REAL_TYPE
3843 && TYPE_MAIN_VARIANT (arg
) != TYPE_MAIN_VARIANT (parm
))
3846 /* As far as unification is concerned, this wins. Later checks
3847 will invalidate it if necessary. */
3850 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3851 /* Type INTEGER_CST can come from ordinary constant template args. */
3853 while (TREE_CODE (arg
) == NOP_EXPR
)
3854 arg
= TREE_OPERAND (arg
, 0);
3856 if (TREE_CODE (arg
) != INTEGER_CST
)
3858 return !tree_int_cst_equal (parm
, arg
);
3863 t1
= TREE_OPERAND (parm
, 0);
3864 t2
= TREE_OPERAND (parm
, 1);
3865 return unify (tparms
, targs
, ntparms
, t1
,
3866 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
3873 if (TREE_CODE (arg
) != TREE_VEC
)
3875 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
3877 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
3878 if (unify (tparms
, targs
, ntparms
,
3879 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
3886 if (TYPE_PTRMEMFUNC_FLAG (parm
))
3887 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
3888 arg
, nsubsts
, strict
);
3890 /* Allow trivial conversions. */
3891 if (TREE_CODE (arg
) != RECORD_TYPE
3892 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
3893 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
3896 if (CLASSTYPE_TEMPLATE_INFO (parm
) && uses_template_parms (parm
))
3899 if (flag_ansi_overloading
&& ! strict
)
3900 t
= get_template_base (CLASSTYPE_TI_TEMPLATE (parm
), arg
);
3902 (CLASSTYPE_TEMPLATE_INFO (arg
)
3903 && CLASSTYPE_TI_TEMPLATE (parm
) == CLASSTYPE_TI_TEMPLATE (arg
))
3905 if (! t
|| t
== error_mark_node
)
3908 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
3909 CLASSTYPE_TI_ARGS (t
), nsubsts
, strict
);
3911 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
3916 if (TREE_CODE (arg
) != METHOD_TYPE
)
3921 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
3924 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3925 TREE_TYPE (arg
), nsubsts
, strict
))
3927 return type_unification_real (tparms
, targs
, TYPE_ARG_TYPES (parm
),
3928 TYPE_ARG_TYPES (arg
), nsubsts
, 1,
3932 if (TREE_CODE (arg
) != OFFSET_TYPE
)
3934 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
3935 TYPE_OFFSET_BASETYPE (arg
), nsubsts
, strict
))
3937 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
3938 TREE_TYPE (arg
), nsubsts
, strict
);
3941 if (arg
!= decl_constant_value (parm
))
3946 sorry ("use of `%s' in template type unification",
3947 tree_code_name
[(int) TREE_CODE (parm
)]);
3953 mark_decl_instantiated (result
, extern_p
)
3957 if (DECL_TEMPLATE_INSTANTIATION (result
))
3958 SET_DECL_EXPLICIT_INSTANTIATION (result
);
3959 TREE_PUBLIC (result
) = 1;
3963 DECL_INTERFACE_KNOWN (result
) = 1;
3964 DECL_NOT_REALLY_EXTERN (result
) = 1;
3966 else if (TREE_CODE (result
) == FUNCTION_DECL
)
3967 mark_inline_for_output (result
);
3970 /* Given two function templates PAT1 and PAT2, return:
3972 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
3973 -1 if PAT2 is more specialized than PAT1.
3974 0 if neither is more specialized. */
3977 more_specialized (pat1
, pat2
)
3983 targs
= get_bindings (pat1
, pat2
);
3989 targs
= get_bindings (pat2
, pat1
);
3998 /* Given two class template specialization list nodes PAT1 and PAT2, return:
4000 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4001 -1 if PAT2 is more specialized than PAT1.
4002 0 if neither is more specialized. */
4005 more_specialized_class (pat1
, pat2
)
4011 targs
= get_class_bindings
4012 (TREE_VALUE (pat1
), TREE_PURPOSE (pat1
), TREE_PURPOSE (pat2
));
4016 targs
= get_class_bindings
4017 (TREE_VALUE (pat2
), TREE_PURPOSE (pat2
), TREE_PURPOSE (pat1
));
4024 /* Return the template arguments that will produce the function signature
4025 DECL from the function template FN. */
4028 get_bindings (fn
, decl
)
4031 int ntparms
= DECL_NTPARMS (fn
);
4032 tree targs
= make_scratch_vec (ntparms
);
4035 i
= fn_type_unification (fn
, NULL_TREE
, targs
,
4036 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
4037 TREE_TYPE (TREE_TYPE (decl
)),
4046 get_class_bindings (tparms
, parms
, args
)
4047 tree tparms
, parms
, args
;
4049 int i
, dummy
, ntparms
= TREE_VEC_LENGTH (tparms
);
4050 tree vec
= make_temp_vec (ntparms
);
4052 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
4054 switch (unify (tparms
, &TREE_VEC_ELT (vec
, 0), ntparms
,
4055 TREE_VEC_ELT (parms
, i
), TREE_VEC_ELT (args
, i
),
4065 for (i
= 0; i
< ntparms
; ++i
)
4066 if (! TREE_VEC_ELT (vec
, i
))
4072 /* Return the most specialized of the list of templates in FNS that can
4073 produce an instantiation matching DECL. */
4076 most_specialized (fns
, decl
)
4079 tree fn
, champ
, args
, *p
;
4082 for (p
= &fns
; *p
; )
4084 args
= get_bindings (TREE_VALUE (*p
), decl
);
4087 p
= &TREE_CHAIN (*p
);
4090 *p
= TREE_CHAIN (*p
);
4097 champ
= TREE_VALUE (fn
);
4098 fn
= TREE_CHAIN (fn
);
4099 for (; fn
; fn
= TREE_CHAIN (fn
))
4101 fate
= more_specialized (champ
, TREE_VALUE (fn
));
4108 fn
= TREE_CHAIN (fn
);
4110 return error_mark_node
;
4112 champ
= TREE_VALUE (fn
);
4116 for (fn
= fns
; fn
&& TREE_VALUE (fn
) != champ
; fn
= TREE_CHAIN (fn
))
4118 fate
= more_specialized (champ
, TREE_VALUE (fn
));
4120 return error_mark_node
;
4126 /* Return the most specialized of the class template specializations in
4127 SPECS that can produce an instantiation matching ARGS. */
4130 most_specialized_class (specs
, mainargs
)
4131 tree specs
, mainargs
;
4133 tree list
= NULL_TREE
, t
, args
, champ
;
4136 for (t
= specs
; t
; t
= TREE_CHAIN (t
))
4138 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), mainargs
);
4141 list
= decl_tree_cons (TREE_PURPOSE (t
), TREE_VALUE (t
), list
);
4142 TREE_TYPE (list
) = TREE_TYPE (t
);
4152 for (; t
; t
= TREE_CHAIN (t
))
4154 fate
= more_specialized_class (champ
, t
);
4163 return error_mark_node
;
4169 for (t
= list
; t
&& t
!= champ
; t
= TREE_CHAIN (t
))
4171 fate
= more_specialized_class (champ
, t
);
4173 return error_mark_node
;
4179 /* called from the parser. */
4182 do_decl_instantiation (declspecs
, declarator
, storage
)
4183 tree declspecs
, declarator
, storage
;
4185 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0, NULL_TREE
);
4188 tree result
= NULL_TREE
;
4190 tree templates
= NULL_TREE
;
4192 if (! DECL_LANG_SPECIFIC (decl
))
4194 cp_error ("explicit instantiation of non-template `%#D'", decl
);
4198 /* If we've already seen this template instance, use it. */
4199 if (TREE_CODE (decl
) == VAR_DECL
)
4201 result
= lookup_field (DECL_CONTEXT (decl
), DECL_NAME (decl
), 0, 0);
4202 if (result
&& TREE_CODE (result
) != VAR_DECL
)
4205 else if (TREE_CODE (decl
) != FUNCTION_DECL
)
4207 cp_error ("explicit instantiation of `%#D'", decl
);
4210 else if (DECL_FUNCTION_MEMBER_P (decl
))
4212 if (DECL_TEMPLATE_INSTANTIATION (decl
))
4214 else if (name
= DECL_ASSEMBLER_NAME (decl
),
4215 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
4216 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
4220 /* Maybe this is an instantiation of a member template
4222 tree ctype
= DECL_CONTEXT (decl
);
4224 name
= DECL_NAME (decl
);
4225 fn
= lookup_fnfields (TYPE_BINFO (ctype
), name
, 1);
4227 fn
= TREE_VALUE (fn
);
4229 for (; fn
; fn
= DECL_CHAIN (fn
))
4230 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4231 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
4234 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
4236 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
4237 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
4238 templates
= decl_tree_cons (NULL_TREE
, fn
, templates
);
4241 if (templates
&& !result
)
4244 result
= most_specialized (templates
, decl
);
4245 if (result
== error_mark_node
)
4247 char *str
= "candidates are:";
4248 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
4249 for (fn
= templates
; fn
; fn
= TREE_CHAIN (fn
))
4251 cp_error_at ("%s %+#D", str
, TREE_VALUE (fn
));
4258 args
= get_bindings (result
, decl
);
4259 result
= instantiate_template (result
, args
);
4265 cp_error ("no matching template for `%D' found", decl
);
4269 if (! DECL_TEMPLATE_INFO (result
))
4271 cp_pedwarn ("explicit instantiation of non-template `%#D'", result
);
4275 if (flag_external_templates
)
4278 if (storage
== NULL_TREE
)
4280 else if (storage
== ridpointers
[(int) RID_EXTERN
])
4283 cp_error ("storage class `%D' applied to template instantiation",
4286 mark_decl_instantiated (result
, extern_p
);
4287 repo_template_instantiated (result
, extern_p
);
4289 instantiate_decl (result
);
4293 mark_class_instantiated (t
, extern_p
)
4297 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
4298 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
4299 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
4300 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
4301 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
4304 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
4305 rest_of_type_compilation (t
, 1);
4310 do_type_instantiation (t
, storage
)
4317 if (TREE_CODE (t
) == TYPE_DECL
)
4320 if (! IS_AGGR_TYPE (t
) || ! CLASSTYPE_TEMPLATE_INFO (t
))
4322 cp_error ("explicit instantiation of non-template type `%T'", t
);
4328 /* With -fexternal-templates, explicit instantiations are treated the same
4329 as implicit ones. */
4330 if (flag_external_templates
)
4333 if (TYPE_SIZE (t
) == NULL_TREE
)
4335 cp_error ("explicit instantiation of `%#T' before definition of template",
4340 if (storage
== NULL_TREE
)
4342 else if (storage
== ridpointers
[(int) RID_INLINE
])
4344 else if (storage
== ridpointers
[(int) RID_EXTERN
])
4346 else if (storage
== ridpointers
[(int) RID_STATIC
])
4350 cp_error ("storage class `%D' applied to template instantiation",
4355 /* We've already instantiated this. */
4356 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
4360 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
4362 mark_class_instantiated (t
, extern_p
);
4363 repo_template_instantiated (t
, extern_p
);
4373 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
4374 if (DECL_TEMPLATE_INSTANTIATION (tmp
))
4376 mark_decl_instantiated (tmp
, extern_p
);
4377 repo_template_instantiated (tmp
, extern_p
);
4379 instantiate_decl (tmp
);
4382 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
4383 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
4385 mark_decl_instantiated (tmp
, extern_p
);
4386 repo_template_instantiated (tmp
, extern_p
);
4388 instantiate_decl (tmp
);
4391 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
4392 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
4393 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
4398 instantiate_decl (d
)
4401 tree ti
= DECL_TEMPLATE_INFO (d
);
4402 tree tmpl
= TI_TEMPLATE (ti
);
4403 tree args
= TI_ARGS (ti
);
4405 tree decl_pattern
, code_pattern
;
4407 int nested
= in_function_p ();
4409 int pattern_defined
;
4411 char *file
= input_filename
;
4413 for (td
= tmpl
; DECL_TEMPLATE_INSTANTIATION (td
); )
4414 td
= DECL_TI_TEMPLATE (td
);
4416 /* In the case of a member template, decl_pattern is the partially
4417 instantiated declaration (in the instantiated class), and code_pattern
4418 is the original template definition. */
4419 decl_pattern
= DECL_TEMPLATE_RESULT (tmpl
);
4420 code_pattern
= DECL_TEMPLATE_RESULT (td
);
4422 if (TREE_CODE (d
) == FUNCTION_DECL
)
4424 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
4425 pattern_defined
= (DECL_INITIAL (code_pattern
) != NULL_TREE
);
4429 d_defined
= ! DECL_IN_AGGR_P (d
);
4430 pattern_defined
= ! DECL_IN_AGGR_P (code_pattern
);
4436 if (TREE_CODE (d
) == FUNCTION_DECL
)
4440 /* Check to see if there is a matching specialization. */
4441 for (specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
4443 specs
= TREE_CHAIN (specs
))
4444 if (comp_template_args (TREE_PURPOSE (specs
), args
))
4445 return TREE_VALUE (specs
);
4448 /* This needs to happen before any tsubsting. */
4449 if (! push_tinst_level (d
))
4452 push_to_top_level ();
4453 lineno
= DECL_SOURCE_LINE (d
);
4454 input_filename
= DECL_SOURCE_FILE (d
);
4456 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
4457 variable is a static const initialized in the class body. */
4458 if (TREE_CODE (d
) == VAR_DECL
4459 && ! DECL_INITIAL (d
) && DECL_INITIAL (code_pattern
))
4461 pushclass (DECL_CONTEXT (d
), 2);
4462 DECL_INITIAL (d
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
4463 TREE_VEC_LENGTH (args
), tmpl
);
4467 /* import_export_decl has to happen after DECL_INITIAL is set up. */
4468 if (pattern_defined
)
4470 repo_template_used (d
);
4472 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
4474 if (flag_alt_external_templates
)
4476 if (interface_unknown
)
4477 warn_if_unknown_interface (d
);
4479 else if (DECL_INTERFACE_KNOWN (code_pattern
))
4481 DECL_INTERFACE_KNOWN (d
) = 1;
4482 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (code_pattern
);
4485 warn_if_unknown_interface (code_pattern
);
4489 import_export_decl (d
);
4492 if (! pattern_defined
4493 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! DECL_INLINE (d
)
4494 && (! DECL_INTERFACE_KNOWN (d
)
4495 || ! DECL_NOT_REALLY_EXTERN (d
)))
4496 /* Kludge: if we compile a constructor in the middle of processing a
4497 toplevel declaration, we blow away the declspecs in
4498 temp_decl_obstack when we call permanent_allocation in
4499 finish_function. So don't compile it yet. */
4500 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! nested
&& ! at_eof
))
4502 add_pending_template (d
);
4506 lineno
= DECL_SOURCE_LINE (d
);
4507 input_filename
= DECL_SOURCE_FILE (d
);
4509 /* Trick tsubst into giving us a new decl in case the template changed. */
4510 save_ti
= DECL_TEMPLATE_INFO (decl_pattern
);
4511 DECL_TEMPLATE_INFO (decl_pattern
) = NULL_TREE
;
4512 td
= tsubst (decl_pattern
, args
, TREE_VEC_LENGTH (args
), tmpl
);
4513 SET_DECL_IMPLICIT_INSTANTIATION (td
);
4514 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
4516 /* And set up DECL_INITIAL, since tsubst doesn't. */
4517 if (TREE_CODE (td
) == VAR_DECL
)
4519 pushclass (DECL_CONTEXT (d
), 2);
4520 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
4521 TREE_VEC_LENGTH (args
), tmpl
);
4525 if (TREE_CODE (d
) == FUNCTION_DECL
)
4527 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4529 DECL_INITIAL (td
) = error_mark_node
;
4531 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
4532 /* Set up the information about what is being specialized. */
4533 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
4535 duplicate_decls (td
, d
);
4536 if (TREE_CODE (d
) == FUNCTION_DECL
)
4537 DECL_INITIAL (td
) = 0;
4539 if (TREE_CODE (d
) == VAR_DECL
)
4541 DECL_IN_AGGR_P (d
) = 0;
4542 if (DECL_INTERFACE_KNOWN (d
))
4543 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
4546 DECL_EXTERNAL (d
) = 1;
4547 DECL_NOT_REALLY_EXTERN (d
) = 1;
4549 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
4551 else if (TREE_CODE (d
) == FUNCTION_DECL
)
4553 tree t
= DECL_SAVED_TREE (code_pattern
);
4555 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
4556 store_parm_decls ();
4558 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
4561 (TREE_OPERAND (t
, 0),
4562 tsubst_expr (TREE_OPERAND (t
, 1), args
,
4563 TREE_VEC_LENGTH (args
), tmpl
));
4567 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
4569 current_member_init_list
4570 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
4571 current_base_init_list
4572 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
4577 /* Always keep the BLOCK node associated with the outermost
4578 pair of curly braces of a function. These are needed
4579 for correct operation of dwarfout.c. */
4582 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
4583 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
4585 finish_function (lineno
, 0, nested
);
4590 input_filename
= file
;
4592 pop_from_top_level ();
4599 tsubst_chain (t
, argvec
)
4604 tree first
= tsubst (t
, argvec
,
4605 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4608 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
4610 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4611 TREE_CHAIN (last
) = x
;
4621 tsubst_expr_values (t
, argvec
)
4624 tree first
= NULL_TREE
;
4627 for (; t
; t
= TREE_CHAIN (t
))
4629 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
4630 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4631 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
4632 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4633 *p
= build_tree_list (pur
, val
);
4634 p
= &TREE_CHAIN (*p
);
4645 last_tree
= TREE_CHAIN (last_tree
) = t
;
4648 /* D is an undefined function declaration in the presence of templates with
4649 the same name, listed in FNS. If one of them can produce D as an
4650 instantiation, remember this so we can instantiate it at EOF if D has
4651 not been defined by that time. */
4654 add_maybe_template (d
, fns
)
4659 if (DECL_MAYBE_TEMPLATE (d
))
4662 t
= most_specialized (fns
, d
);
4665 if (t
== error_mark_node
)
4667 cp_error ("ambiguous template instantiation for `%D'", d
);
4671 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
4672 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
4673 DECL_MAYBE_TEMPLATE (d
) = 1;
4676 /* Instantiate an enumerated type. Used by instantiate_class_template and
4680 tsubst_enum (tag
, args
, nargs
, field_chain
)
4685 extern tree current_local_enum
;
4686 tree prev_local_enum
= current_local_enum
;
4688 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
4689 tree e
, values
= NULL_TREE
;
4691 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
4693 tree elt
= build_enumerator (TREE_PURPOSE (e
),
4694 tsubst_expr (TREE_VALUE (e
), args
,
4696 TREE_CHAIN (elt
) = values
;
4700 finish_enum (newtag
, values
);
4702 if (NULL
!= field_chain
)
4703 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
4705 current_local_enum
= prev_local_enum
;