1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
46 extern struct obstack permanent_obstack
;
49 extern char *input_filename
;
50 struct pending_inline
*pending_template_expansions
;
52 tree current_template_parms
;
53 HOST_WIDE_INT processing_template_decl
;
55 tree pending_templates
;
56 static tree
*template_tail
= &pending_templates
;
59 static tree
*maybe_template_tail
= &maybe_templates
;
61 int minimal_parse_mode
;
63 static int processing_specialization
;
64 static int template_header_count
;
66 #define obstack_chunk_alloc xmalloc
67 #define obstack_chunk_free free
69 static int unify
PROTO((tree
, tree
*, int, tree
, tree
, int *, int));
70 static void add_pending_template
PROTO((tree
));
71 static int push_tinst_level
PROTO((tree
));
72 static tree classtype_mangled_name
PROTO((tree
));
73 static char *mangle_class_name_for_template
PROTO((char *, tree
, tree
));
74 static tree tsubst_expr_values
PROTO((tree
, tree
));
75 static int comp_template_args
PROTO((tree
, tree
));
76 static int list_eq
PROTO((tree
, tree
));
77 static tree get_class_bindings
PROTO((tree
, tree
, tree
));
78 static tree coerce_template_parms
PROTO((tree
, tree
, tree
));
79 static tree tsubst_enum
PROTO((tree
, tree
, int, tree
*));
80 static tree add_to_template_args
PROTO((tree
, tree
));
81 static int type_unification_real
PROTO((tree
, tree
*, tree
, tree
, int*,
83 static int processing_explicit_specialization
PROTO((int));
84 static void note_template_header
PROTO((int));
86 /* Restore the template parameter context. */
89 begin_member_template_processing (decl
)
95 parms
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
97 ++processing_template_decl
;
98 current_template_parms
99 = tree_cons (build_int_2 (0, processing_template_decl
),
100 parms
, current_template_parms
);
102 for (i
= 0; i
< TREE_VEC_LENGTH (parms
); ++i
)
104 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
105 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm
)) == 'd', 0);
107 switch (TREE_CODE (parm
))
115 /* Make a CONST_DECL as is done in process_template_parm. */
116 tree decl
= build_decl (CONST_DECL
, DECL_NAME (parm
),
118 DECL_INITIAL (decl
) = DECL_INITIAL (parm
);
124 my_friendly_abort (0);
129 /* Undo the effects of begin_member_template_processing. */
132 end_member_template_processing ()
134 if (! processing_template_decl
)
137 --processing_template_decl
;
138 current_template_parms
= TREE_CHAIN (current_template_parms
);
142 /* Returns non-zero iff T is a member template function. Works if T
143 is either a FUNCTION_DECL or a TEMPLATE_DECL. */
146 is_member_template (t
)
151 if (TREE_CODE (t
) != FUNCTION_DECL
152 && !DECL_FUNCTION_TEMPLATE_P (t
))
153 /* Anything that isn't a template or a template functon is
154 certainly not a member template. */
157 if ((DECL_FUNCTION_MEMBER_P (t
)
158 && !DECL_TEMPLATE_SPECIALIZATION (t
))
159 || (TREE_CODE (t
) == TEMPLATE_DECL
&&
160 DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t
))))
162 tree tmpl
= NULL_TREE
;
164 if (DECL_FUNCTION_TEMPLATE_P (t
))
166 else if (DECL_TEMPLATE_INFO (t
)
167 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t
)))
168 tmpl
= DECL_TI_TEMPLATE (t
);
172 tree parms
= DECL_TEMPLATE_PARMS (tmpl
);
173 int parm_levels
= list_length (parms
);
174 int template_class_levels
= 0;
175 tree ctx
= DECL_CLASS_CONTEXT (t
);
177 if (CLASSTYPE_TEMPLATE_INFO (ctx
))
181 /* Here, we should really count the number of levels
182 deep ctx is, making sure not to count any levels that
183 are just specializations. Since there are no member
184 template classes yet, we don't have to do all that. */
186 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
187 template_class_levels
= 1;
192 args
= CLASSTYPE_TI_ARGS (ctx
);
194 if (args
== NULL_TREE
)
195 template_class_levels
= 1;
197 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
198 if (uses_template_parms (TREE_VEC_ELT (args
, i
)))
200 template_class_levels
++;
206 if (parm_levels
> template_class_levels
)
214 /* Return a new template argument vector which contains all of ARGS,
215 but has as its innermost set of arguments the EXTRA_ARGS. */
218 add_to_template_args (args
, extra_args
)
224 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) != TREE_VEC
)
226 new_args
= make_tree_vec (2);
227 TREE_VEC_ELT (new_args
, 0) = args
;
233 new_args
= make_tree_vec (TREE_VEC_LENGTH (args
) - 1);
235 for (i
= 0; i
< TREE_VEC_LENGTH (args
); ++i
)
236 TREE_VEC_ELT (new_args
, i
) = TREE_VEC_ELT (args
, i
);
239 TREE_VEC_ELT (new_args
,
240 TREE_VEC_LENGTH (new_args
) - 1) = extra_args
;
245 /* We've got a template header coming up; push to a new level for storing
249 begin_template_parm_list ()
252 declare_pseudo_global_level ();
253 ++processing_template_decl
;
254 note_template_header (0);
258 /* We've just seen template <>. */
261 begin_specialization ()
263 note_template_header (1);
267 /* Called at then end of processing a declaration preceeded by
271 end_specialization ()
273 reset_specialization ();
277 /* Any template <>'s that we have seen thus far are not referring to a
278 function specialization. */
281 reset_specialization ()
283 processing_specialization
= 0;
284 template_header_count
= 0;
288 /* We've just seen a template header. If SPECIALIZATION is non-zero,
289 it was of the form template <>. */
292 note_template_header (specialization
)
295 processing_specialization
= specialization
;
296 template_header_count
++;
300 /* Returns non-zero iff a declarator, in which the number of template
301 types that appeared was TEMPLATE_COUNT, is an explicit
305 processing_explicit_specialization(template_count
)
308 /* A function declaration is an explicit specialization of a member
309 template if all of the following conditions hold:
311 o There was a template <...> preceeding the declaration.
312 o The last template <...> was in fact template <>.
313 o The number of template <...>'s preceeding the declaration, less
314 the number of template classes with arguments specified used to
315 qualify the function name, is 1.
319 template <> void S<int>::foo();
320 template <class T> template <> void S<T>::foo();
321 template <> struct S<int> { ... template <> void foo(); }
323 The first of these is not a specialization of S<int>::foo() (it
324 is instead a specialization of S<T>::foo), while the next two are
325 specializations of member template functions. */
327 return processing_specialization
328 && template_header_count
> template_count
;
331 /* Returns the template function specialized by TEMPLATE_ID, or
332 NULL_TREE if there is none.
334 The TEMPLATE_ID is a TEMPLATE_ID_EXPR. The TYPE is
335 the type it has been declared to have. Return the TEMPLATE_DECL
336 that is being specialized, and put the specialization arguments in
337 *TARGS. If no appropriate specialization can be found, NULL_TREE is
338 returned, and *TARGS is assigned NULL_TREE. If complain is
339 non-zero, error messages are printed where appropriate. */
342 determine_explicit_specialization (template_id
, type
, targs_out
,
343 need_member_template
,
348 int need_member_template
;
354 tree matching_fns
= NULL_TREE
;
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_tree_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
= tree_cons (fn
, targs
, matching_fns
);
424 if (matching_fns
== NULL_TREE
)
427 cp_error ("Specialization of `%s' does not match any template "
428 "declaration.", 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 preceeded by "
508 if (TREE_CODE (declarator
) != TEMPLATE_ID_EXPR
)
512 my_friendly_assert (TREE_CODE (declarator
) == IDENTIFIER_NODE
,
515 fns
= IDENTIFIER_GLOBAL_VALUE (dname
);
519 declarator
= lookup_template_function (fns
, NULL_TREE
);
522 if (TREE_CODE (TREE_OPERAND (declarator
, 0)) == LOOKUP_EXPR
)
524 /* A friend declaration. We can't do much, because we don't
525 know what this resolves to, yet. */
526 my_friendly_assert (is_friend
!= 0, 0);
527 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
532 && TREE_CODE (TREE_OPERAND (declarator
, 0)) == IDENTIFIER_NODE
)
536 if (TYPE_BEING_DEFINED (ctype
) && !finish_member
)
538 /* Since finish_struct_1 has not been called yet, we
539 can't call lookup_fnfields. We note that this
540 template is a specialization, and proceed, letting
541 finish_struct_methods fix this up later. */
542 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
543 DECL_TEMPLATE_INFO (decl
)
544 = perm_tree_cons (NULL_TREE
,
545 TREE_OPERAND (declarator
, 1),
550 fns
= lookup_fnfields (TYPE_BINFO (ctype
),
551 TREE_OPERAND (declarator
, 0),
554 if (fns
== NULL_TREE
)
556 cp_error ("No member template `%s' declared in `%T'",
557 IDENTIFIER_POINTER (TREE_OPERAND (declarator
,
563 TREE_OPERAND (declarator
, 0) = fns
;
567 determine_explicit_specialization
568 (declarator
, TREE_TYPE (decl
), &targs
,
569 TREE_CODE (decl
) == TEMPLATE_DECL
, 1);
573 /* Mangle the function name appropriately. */
574 if (name_mangling_version
>= 1)
576 tree arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
579 && TREE_CODE (TREE_TYPE (tmpl
)) == FUNCTION_TYPE
)
581 hash_tree_chain (build_pointer_type (ctype
),
584 DECL_ASSEMBLER_NAME (decl
)
585 = build_template_decl_overload
588 TREE_TYPE (TREE_TYPE (tmpl
)),
589 DECL_INNERMOST_TEMPLATE_PARMS (tmpl
),
590 targs
, ctype
!= NULL_TREE
);
593 if (is_friend
&& !have_def
)
595 /* This is not really a declaration of a specialization.
596 It's just the name of an instantiation. But, it's not
597 a request for an instantiation, either. */
598 SET_DECL_IMPLICIT_INSTANTIATION (decl
);
599 DECL_TEMPLATE_INFO (decl
)
600 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
604 /* This function declaration is a template specialization.
606 SET_DECL_TEMPLATE_SPECIALIZATION (decl
);
607 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
)
608 = perm_tree_cons (targs
, decl
,
609 DECL_TEMPLATE_SPECIALIZATIONS
611 /* If DECL_TI_TEMPLATE (decl), the decl is an
612 instantiation of a specialization of a member template.
613 (In other words, there was a member template, in a
614 class template. That member template was specialized.
615 We then instantiated the class, so there is now an
616 instance of that specialization.)
618 According to the CD2,
620 14.7.3.13 [tmpl.expl.spec]
622 A specialization of a member function template or
623 member class template of a non-specialized class
624 template is itself a template.
626 So, we just leave the template info alone in this case. */
627 if (!(DECL_TEMPLATE_INFO (decl
) && DECL_TI_TEMPLATE (decl
)))
628 DECL_TEMPLATE_INFO (decl
)
629 = perm_tree_cons (tmpl
, targs
, NULL_TREE
);
637 /* Process information from new template parameter NEXT and append it to the
641 process_template_parm (list
, next
)
649 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
650 defval
= TREE_PURPOSE (parm
);
651 parm
= TREE_VALUE (parm
);
652 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
656 tree p
= TREE_VALUE (tree_last (list
));
658 if (TREE_CODE (p
) == TYPE_DECL
)
659 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
661 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
670 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
671 /* is a const-param */
672 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
674 /* A template parameter is not modifiable. */
675 TREE_READONLY (parm
) = 1;
676 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
677 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
679 cp_error ("`%#T' is not a valid type for a template constant parameter",
681 if (DECL_NAME (parm
) == NULL_TREE
)
682 error (" a template type parameter must begin with `class' or `typename'");
683 TREE_TYPE (parm
) = void_type_node
;
686 && (TREE_CODE (TREE_TYPE (parm
)) == REAL_TYPE
687 || TREE_CODE (TREE_TYPE (parm
)) == COMPLEX_TYPE
))
688 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
690 tinfo
= make_node (TEMPLATE_CONST_PARM
);
691 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
692 if (TREE_PERMANENT (parm
) == 0)
694 parm
= copy_node (parm
);
695 TREE_PERMANENT (parm
) = 1;
697 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
698 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
699 DECL_INITIAL (decl
) = tinfo
;
700 DECL_INITIAL (parm
) = tinfo
;
701 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
);
705 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
706 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
707 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
708 TYPE_NAME (t
) = decl
;
709 TYPE_STUB_DECL (t
) = decl
;
711 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
);
713 SET_DECL_ARTIFICIAL (decl
);
715 parm
= build_tree_list (defval
, parm
);
716 return chainon (list
, parm
);
719 /* The end of a template parameter list has been reached. Process the
720 tree list into a parameter vector, converting each parameter into a more
721 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
725 end_template_parm_list (parms
)
730 tree saved_parmlist
= make_tree_vec (list_length (parms
));
732 current_template_parms
733 = tree_cons (build_int_2 (0, processing_template_decl
),
734 saved_parmlist
, current_template_parms
);
736 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
737 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
739 return saved_parmlist
;
742 /* end_template_decl is called after a template declaration is seen. */
747 reset_specialization ();
749 if (! processing_template_decl
)
752 /* This matches the pushlevel in begin_template_parm_list. */
755 --processing_template_decl
;
756 current_template_parms
= TREE_CHAIN (current_template_parms
);
757 (void) get_pending_sizes (); /* Why? */
760 /* Generate a valid set of template args from current_template_parms. */
763 current_template_args ()
765 tree header
= current_template_parms
;
766 int length
= list_length (header
);
767 tree args
= make_tree_vec (length
);
772 tree a
= copy_node (TREE_VALUE (header
));
773 int i
= TREE_VEC_LENGTH (a
);
774 TREE_TYPE (a
) = NULL_TREE
;
777 tree t
= TREE_VEC_ELT (a
, i
);
779 /* t will be a list if we are called from within a
780 begin/end_template_parm_list pair, but a vector directly
781 if within a begin/end_member_template_processing pair. */
782 if (TREE_CODE (t
) == TREE_LIST
)
786 if (TREE_CODE (t
) == TYPE_DECL
)
789 t
= DECL_INITIAL (t
);
792 TREE_VEC_ELT (a
, i
) = t
;
794 TREE_VEC_ELT (args
, --l
) = a
;
795 header
= TREE_CHAIN (header
);
802 push_template_decl (decl
)
806 tree args
= NULL_TREE
;
808 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
812 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
813 && DECL_CLASS_CONTEXT (decl
))
815 /* Note that this template is a "primary template" */
816 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
817 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
820 /* Partial specialization. */
821 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
)
822 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl
)))
824 tree type
= TREE_TYPE (decl
);
825 tree maintmpl
= CLASSTYPE_TI_TEMPLATE (type
);
826 tree mainargs
= CLASSTYPE_TI_ARGS (type
);
827 tree spec
= DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
);
829 for (; spec
; spec
= TREE_CHAIN (spec
))
831 /* purpose: args to main template
832 value: spec template */
833 if (comp_template_args (TREE_PURPOSE (spec
), mainargs
))
837 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
) = CLASSTYPE_TI_SPEC_INFO (type
)
838 = perm_tree_cons (mainargs
, TREE_VALUE (current_template_parms
),
839 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
));
840 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl
)) = type
;
844 args
= current_template_args ();
846 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
848 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
849 DECL_TEMPLATE_PARMS (tmpl
) = current_template_parms
;
850 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
851 if (DECL_LANG_SPECIFIC (decl
))
853 DECL_CLASS_CONTEXT (tmpl
) = DECL_CLASS_CONTEXT (decl
);
854 DECL_STATIC_FUNCTION_P (tmpl
) =
855 DECL_STATIC_FUNCTION_P (decl
);
857 if (DECL_TEMPLATE_SPECIALIZATION (decl
))
859 /* A specialization of a member template of a template
861 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl
);
862 DECL_TEMPLATE_INFO (tmpl
) = DECL_TEMPLATE_INFO (decl
);
863 DECL_TEMPLATE_INFO (decl
) = NULL_TREE
;
872 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx
))
873 cp_error ("must specialize `%#T' before defining member `%#D'",
875 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
876 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
877 else if (! DECL_TEMPLATE_INFO (decl
))
879 cp_error ("template definition of non-template `%#D'", decl
);
883 tmpl
= DECL_TI_TEMPLATE (decl
);
885 if (is_member_template (tmpl
))
887 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
888 t
= DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl
));
889 if (TREE_VEC_LENGTH (t
)
890 != TREE_VEC_LENGTH (a
))
892 cp_error ("got %d template parameters for `%#D'",
893 TREE_VEC_LENGTH (a
), decl
);
894 cp_error (" but %d required", TREE_VEC_LENGTH (t
));
896 if (TREE_VEC_LENGTH (args
) > 1)
897 /* Get the template parameters for the enclosing template
899 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 2);
904 a
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
908 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx
))
910 /* When processing an inline member template of a
911 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
912 if (CLASSTYPE_TI_SPEC_INFO (ctx
))
913 t
= TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx
));
915 else if (CLASSTYPE_TEMPLATE_INFO (ctx
))
916 t
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx
));
918 /* There should be template arguments if and only if there is a
920 my_friendly_assert((a
!= NULL_TREE
) == (t
!= NULL_TREE
), 0);
923 && TREE_VEC_LENGTH (t
) != TREE_VEC_LENGTH (a
))
925 cp_error ("got %d template parameters for `%#D'",
926 TREE_VEC_LENGTH (a
), decl
);
927 cp_error (" but `%#T' has %d", ctx
, TREE_VEC_LENGTH (t
));
930 /* Get the innermost set of template arguments. */
931 args
= TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1);
933 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
934 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
937 tmpl
= pushdecl_top_level (tmpl
);
940 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl
)) = tmpl
;
942 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
944 if (TREE_CODE (decl
) == TYPE_DECL
&& DECL_ARTIFICIAL (decl
))
946 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
947 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
949 else if (! DECL_LANG_SPECIFIC (decl
))
950 cp_error ("template declaration of `%#D'", decl
);
952 DECL_TEMPLATE_INFO (decl
) = info
;
955 /* Convert all template arguments to their appropriate types, and return
956 a vector containing the resulting values. If any error occurs, return
960 coerce_template_parms (parms
, arglist
, in_decl
)
964 int nparms
, nargs
, i
, lost
= 0;
967 if (arglist
== NULL_TREE
)
969 else if (TREE_CODE (arglist
) == TREE_VEC
)
970 nargs
= TREE_VEC_LENGTH (arglist
);
972 nargs
= list_length (arglist
);
974 nparms
= TREE_VEC_LENGTH (parms
);
978 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
980 error ("incorrect number of parameters (%d, should be %d)",
983 cp_error_at ("in template expansion for decl `%D'", in_decl
);
984 return error_mark_node
;
987 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
988 vec
= copy_node (arglist
);
991 vec
= make_tree_vec (nparms
);
992 for (i
= 0; i
< nparms
; i
++)
999 arglist
= TREE_CHAIN (arglist
);
1001 if (arg
== error_mark_node
)
1004 arg
= TREE_VALUE (arg
);
1006 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms
, i
)))
1008 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1011 arg
= tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
1014 TREE_VEC_ELT (vec
, i
) = arg
;
1017 for (i
= 0; i
< nparms
; i
++)
1019 tree arg
= TREE_VEC_ELT (vec
, i
);
1020 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1022 int is_type
, requires_type
;
1024 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
1025 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
1027 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
1028 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
1030 cp_pedwarn ("to refer to a type member of a template parameter,");
1031 cp_pedwarn (" use `typename %E'", arg
);
1032 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
1033 TREE_OPERAND (arg
, 1));
1036 if (is_type
!= requires_type
)
1040 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1043 cp_error (" expected a constant of type `%T', got `%T'",
1044 TREE_TYPE (parm
), arg
);
1046 cp_error (" expected a type, got `%E'", arg
);
1049 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
1054 val
= groktypename (arg
);
1055 if (! processing_template_decl
)
1057 tree t
= target_type (val
);
1058 if (TREE_CODE (t
) != TYPENAME_TYPE
1060 && decl_function_context (TYPE_MAIN_DECL (t
)))
1062 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
1063 return error_mark_node
;
1069 tree t
= tsubst (TREE_TYPE (parm
), vec
,
1070 TREE_VEC_LENGTH (vec
), in_decl
);
1071 if (processing_template_decl
)
1074 val
= digest_init (t
, arg
, (tree
*) 0);
1076 if (val
== error_mark_node
|| processing_template_decl
)
1079 /* 14.2: Other template-arguments must be constant-expressions,
1080 addresses of objects or functions with external linkage, or of
1081 static class members. */
1082 else if (IS_AGGR_TYPE (TREE_TYPE (val
)))
1084 cp_error ("object `%E' cannot be used as template argument", arg
);
1085 val
= error_mark_node
;
1087 else if (!TREE_CONSTANT (val
))
1089 cp_error ("non-const `%E' cannot be used as template argument",
1091 val
= error_mark_node
;
1093 else if (POINTER_TYPE_P (TREE_TYPE (val
))
1094 && ! integer_zerop (val
)
1095 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
1096 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
1100 if (TREE_CODE (t
) == ADDR_EXPR
)
1102 tree a
= TREE_OPERAND (t
, 0);
1104 if (TREE_CODE (a
) == STRING_CST
)
1106 cp_error ("string literal %E is not a valid template argument", a
);
1107 error ("because it is the address of an object with static linkage");
1108 val
= error_mark_node
;
1110 else if (TREE_CODE (a
) != VAR_DECL
1111 && TREE_CODE (a
) != FUNCTION_DECL
)
1113 else if (! TREE_PUBLIC (a
))
1115 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
1116 val
= error_mark_node
;
1122 cp_error ("`%E' is not a valid template argument", t
);
1123 error ("it must be %s%s with external linkage",
1124 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
1125 ? "a pointer to " : "",
1126 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
1127 ? "a function" : "an object");
1128 val
= error_mark_node
;
1133 if (val
== error_mark_node
)
1136 TREE_VEC_ELT (vec
, i
) = val
;
1139 return error_mark_node
;
1144 comp_template_args (oldargs
, newargs
)
1145 tree oldargs
, newargs
;
1149 if (TREE_VEC_LENGTH (oldargs
) != TREE_VEC_LENGTH (newargs
))
1152 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
1154 tree nt
= TREE_VEC_ELT (newargs
, i
);
1155 tree ot
= TREE_VEC_ELT (oldargs
, i
);
1159 if (TREE_CODE (nt
) != TREE_CODE (ot
))
1161 if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't')
1163 if (comptypes (ot
, nt
, 1))
1166 else if (cp_tree_equal (ot
, nt
) > 0)
1173 /* Given class template name and parameter list, produce a user-friendly name
1174 for the instantiation. */
1177 mangle_class_name_for_template (name
, parms
, arglist
)
1179 tree parms
, arglist
;
1181 static struct obstack scratch_obstack
;
1182 static char *scratch_firstobj
;
1185 if (!scratch_firstobj
)
1186 gcc_obstack_init (&scratch_obstack
);
1188 obstack_free (&scratch_obstack
, scratch_firstobj
);
1189 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
1192 #define buflen sizeof(buf)
1193 #define check if (bufp >= buf+buflen-1) goto too_long
1194 #define ccat(c) *bufp++=(c); check
1195 #define advance bufp+=strlen(bufp); check
1196 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1199 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1201 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1206 nparms
= TREE_VEC_LENGTH (parms
);
1207 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
1208 for (i
= 0; i
< nparms
; i
++)
1210 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
1211 tree arg
= TREE_VEC_ELT (arglist
, i
);
1216 if (TREE_CODE (parm
) == TYPE_DECL
)
1218 cat (type_as_string (arg
, 0));
1222 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
1224 if (TREE_CODE (arg
) == TREE_LIST
)
1226 /* New list cell was built because old chain link was in
1228 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
1229 arg
= TREE_VALUE (arg
);
1231 /* No need to check arglist against parmlist here; we did that
1232 in coerce_template_parms, called from lookup_template_class. */
1233 cat (expr_as_string (arg
, 0));
1236 char *bufp
= obstack_next_free (&scratch_obstack
);
1238 while (bufp
[offset
- 1] == ' ')
1240 obstack_blank_fast (&scratch_obstack
, offset
);
1242 /* B<C<char> >, not B<C<char>> */
1243 if (bufp
[offset
- 1] == '>')
1248 return (char *) obstack_base (&scratch_obstack
);
1253 fatal ("out of (preallocated) string space creating template instantiation name");
1259 classtype_mangled_name (t
)
1262 if (CLASSTYPE_TEMPLATE_INFO (t
)
1263 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
1265 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
1266 char *mangled_name
= mangle_class_name_for_template
1267 (IDENTIFIER_POINTER (name
),
1268 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
1269 CLASSTYPE_TI_ARGS (t
));
1270 tree id
= get_identifier (mangled_name
);
1271 IDENTIFIER_TEMPLATE (id
) = name
;
1275 return TYPE_IDENTIFIER (t
);
1279 add_pending_template (d
)
1284 if (TREE_CODE_CLASS (TREE_CODE (d
)) == 't')
1285 ti
= CLASSTYPE_TEMPLATE_INFO (d
);
1287 ti
= DECL_TEMPLATE_INFO (d
);
1289 if (TI_PENDING_TEMPLATE_FLAG (ti
))
1292 *template_tail
= perm_tree_cons
1293 (current_function_decl
, d
, NULL_TREE
);
1294 template_tail
= &TREE_CHAIN (*template_tail
);
1295 TI_PENDING_TEMPLATE_FLAG (ti
) = 1;
1299 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1300 may be either a _DECL or an overloaded function or an
1301 IDENTIFIER_NODE), and ARGLIST. */
1304 lookup_template_function (fns
, arglist
)
1307 if (fns
== NULL_TREE
)
1309 cp_error ("non-template used as template");
1310 return error_mark_node
;
1313 if (arglist
!= NULL_TREE
&& !TREE_PERMANENT (arglist
))
1315 push_obstacks (&permanent_obstack
, &permanent_obstack
);
1316 arglist
= copy_list (arglist
);
1320 return build_min (TEMPLATE_ID_EXPR
,
1322 ? TREE_TYPE (fns
) : unknown_type_node
,
1327 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1328 parameters, find the desired type.
1330 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1331 Since ARGLIST is build on the decl_obstack, we must copy it here
1332 to keep it from being reclaimed when the decl storage is reclaimed.
1334 IN_DECL, if non-NULL, is the template declaration we are trying to
1338 lookup_template_class (d1
, arglist
, in_decl
)
1342 tree
template, parmlist
;
1346 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
1348 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
1350 template = IDENTIFIER_CLASS_VALUE (d1
);
1352 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
1354 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
1355 d1
= DECL_NAME (template);
1357 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
1359 template = CLASSTYPE_TI_TEMPLATE (d1
);
1360 d1
= DECL_NAME (template);
1363 my_friendly_abort (272);
1365 /* With something like `template <class T> class X class X { ... };'
1366 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1367 We don't want to do that, but we have to deal with the situation, so
1368 let's give them some syntax errors to chew on instead of a crash. */
1370 return error_mark_node
;
1371 if (TREE_CODE (template) != TEMPLATE_DECL
)
1373 cp_error ("non-template type `%T' used as a template", d1
);
1375 cp_error_at ("for template declaration `%D'", in_decl
);
1376 return error_mark_node
;
1379 if (PRIMARY_TEMPLATE_P (template))
1381 parmlist
= DECL_INNERMOST_TEMPLATE_PARMS (template);
1383 arglist
= coerce_template_parms (parmlist
, arglist
, template);
1384 if (arglist
== error_mark_node
)
1385 return error_mark_node
;
1386 if (uses_template_parms (arglist
))
1389 if (comp_template_args
1390 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
1391 found
= TREE_TYPE (template);
1394 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
1395 found
; found
= TREE_CHAIN (found
))
1397 if (TI_USES_TEMPLATE_PARMS (found
)
1398 && comp_template_args (TREE_PURPOSE (found
), arglist
))
1402 found
= TREE_VALUE (found
);
1407 if (can_free (&permanent_obstack
, arglist
))
1408 obstack_free (&permanent_obstack
, arglist
);
1413 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
1415 id
= get_identifier (mangled_name
);
1416 IDENTIFIER_TEMPLATE (id
) = d1
;
1418 maybe_push_to_top_level (uses_template_parms (arglist
));
1419 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
1420 pop_from_top_level ();
1424 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1427 arglist
= CLASSTYPE_TI_ARGS (ctx
);
1429 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
1431 int save_temp
= processing_template_decl
;
1432 processing_template_decl
= 0;
1433 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
1434 processing_template_decl
= save_temp
;
1438 t
= lookup_nested_type_by_name (ctx
, id
);
1439 my_friendly_assert (t
!= NULL_TREE
, 42);
1443 /* Seems to be wanted. */
1444 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
1446 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1448 arglist
= copy_to_permanent (arglist
);
1449 CLASSTYPE_TEMPLATE_INFO (t
)
1450 = perm_tree_cons (template, arglist
, NULL_TREE
);
1451 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1452 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
1453 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1454 = uses_template_parms (arglist
);
1456 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
1458 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1459 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
1460 /* if (! uses_template_parms (arglist)) */
1461 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
1462 = get_identifier (build_overload_name (t
, 1, 1));
1464 if (flag_external_templates
&& ! uses_template_parms (arglist
)
1465 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1466 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1467 add_pending_template (t
);
1473 /* Should be defined in parse.h. */
1477 uses_template_parms (t
)
1482 switch (TREE_CODE (t
))
1486 /* We assume that the object must be instantiated in order to build
1487 the COMPONENT_REF, so we test only whether the type of the
1488 COMPONENT_REF uses template parms. */
1489 return uses_template_parms (TREE_TYPE (t
));
1491 case IDENTIFIER_NODE
:
1492 if (!IDENTIFIER_TEMPLATE (t
))
1494 my_friendly_abort (42);
1496 /* aggregates of tree nodes */
1499 int i
= TREE_VEC_LENGTH (t
);
1501 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
1506 if (uses_template_parms (TREE_PURPOSE (t
))
1507 || uses_template_parms (TREE_VALUE (t
)))
1509 return uses_template_parms (TREE_CHAIN (t
));
1511 /* constructed type nodes */
1513 case REFERENCE_TYPE
:
1514 return uses_template_parms (TREE_TYPE (t
));
1516 if (TYPE_PTRMEMFUNC_FLAG (t
))
1517 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
1519 if (! CLASSTYPE_TEMPLATE_INFO (t
))
1521 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
1523 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1525 return uses_template_parms (TREE_TYPE (t
));
1527 if (uses_template_parms (TYPE_DOMAIN (t
)))
1529 return uses_template_parms (TREE_TYPE (t
));
1531 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
1533 return uses_template_parms (TREE_TYPE (t
));
1535 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
1537 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
1539 return uses_template_parms (TREE_TYPE (t
));
1543 return uses_template_parms (TREE_TYPE (t
));
1547 /* ??? What about FIELD_DECLs? */
1548 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
1549 && uses_template_parms (DECL_TI_ARGS (t
)))
1554 if (uses_template_parms (TREE_TYPE (t
)))
1556 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
1561 return uses_template_parms (TREE_TYPE (t
));
1563 return uses_template_parms (TREE_OPERAND (t
, 0));
1565 /* template parm nodes */
1566 case TEMPLATE_TYPE_PARM
:
1567 case TEMPLATE_CONST_PARM
:
1570 /* simple type nodes */
1572 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
1574 return uses_template_parms (TYPE_MAX_VALUE (t
));
1586 for (v
= TYPE_VALUES (t
); v
!= NULL_TREE
; v
= TREE_CHAIN (v
))
1587 if (uses_template_parms (TREE_VALUE (v
)))
1599 /* Non-error_mark_node ERROR_MARKs are bad things. */
1600 my_friendly_assert (t
== error_mark_node
, 274);
1609 return uses_template_parms (TREE_OPERAND (t
, 0));
1612 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
1613 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
1614 return uses_template_parms (TREE_OPERAND (t
, 1));
1618 case REINTERPRET_CAST_EXPR
:
1619 case CONST_CAST_EXPR
:
1620 case STATIC_CAST_EXPR
:
1621 case DYNAMIC_CAST_EXPR
:
1629 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
1637 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
1638 if (uses_template_parms (TREE_OPERAND (t
, i
)))
1645 sorry ("testing %s for template parms",
1646 tree_code_name
[(int) TREE_CODE (t
)]);
1647 my_friendly_abort (82);
1653 static struct tinst_level
*current_tinst_level
= 0;
1654 static struct tinst_level
*free_tinst_level
= 0;
1655 static int tinst_depth
= 0;
1656 extern int max_tinst_depth
;
1657 #ifdef GATHER_STATISTICS
1658 int depth_reached
= 0;
1662 push_tinst_level (d
)
1665 struct tinst_level
*new;
1667 if (tinst_depth
>= max_tinst_depth
)
1669 struct tinst_level
*p
= current_tinst_level
;
1671 char *file
= input_filename
;
1673 error ("template instantiation depth exceeds maximum of %d",
1675 error (" (use -ftemplate-depth-NN to increase the maximum)");
1676 cp_error (" instantiating `%D'", d
);
1678 for (; p
; p
= p
->next
)
1680 cp_error (" instantiated from `%D'", p
->decl
);
1682 input_filename
= p
->file
;
1684 error (" instantiated from here");
1687 input_filename
= file
;
1692 if (free_tinst_level
)
1694 new = free_tinst_level
;
1695 free_tinst_level
= new->next
;
1698 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
1702 new->file
= input_filename
;
1703 new->next
= current_tinst_level
;
1704 current_tinst_level
= new;
1707 #ifdef GATHER_STATISTICS
1708 if (tinst_depth
> depth_reached
)
1709 depth_reached
= tinst_depth
;
1718 struct tinst_level
*old
= current_tinst_level
;
1720 current_tinst_level
= old
->next
;
1721 old
->next
= free_tinst_level
;
1722 free_tinst_level
= old
;
1726 struct tinst_level
*
1729 struct tinst_level
*p
= current_tinst_level
;
1732 for (; p
->next
; p
= p
->next
)
1738 instantiate_class_template (type
)
1741 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
1743 if (type
== error_mark_node
)
1744 return error_mark_node
;
1746 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
1748 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
1751 template = TI_TEMPLATE (template_info
);
1752 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
1753 args
= TI_ARGS (template_info
);
1755 t
= most_specialized_class
1756 (DECL_TEMPLATE_SPECIALIZATIONS (template), args
);
1758 if (t
== error_mark_node
)
1760 char *str
= "candidates are:";
1761 cp_error ("ambiguous class template instantiation for `%#T'", type
);
1762 for (t
= DECL_TEMPLATE_SPECIALIZATIONS (template); t
; t
= TREE_CHAIN (t
))
1764 if (get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
))
1766 cp_error_at ("%s %+#T", str
, TREE_TYPE (t
));
1770 TYPE_BEING_DEFINED (type
) = 1;
1771 return error_mark_node
;
1774 pattern
= TREE_TYPE (t
);
1776 pattern
= TREE_TYPE (template);
1778 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1782 args
= get_class_bindings (TREE_VALUE (t
), TREE_PURPOSE (t
), args
);
1784 TYPE_BEING_DEFINED (type
) = 1;
1786 if (! push_tinst_level (type
))
1789 maybe_push_to_top_level (uses_template_parms (type
));
1790 pushclass (type
, 0);
1792 if (flag_external_templates
)
1794 if (flag_alt_external_templates
)
1796 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1797 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1798 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1799 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1800 && CLASSTYPE_INTERFACE_KNOWN (type
);
1804 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1805 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1806 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1807 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1808 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1809 && CLASSTYPE_INTERFACE_KNOWN (type
);
1814 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1815 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1818 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1819 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1820 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1821 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1822 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1823 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1824 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1825 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1826 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1827 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1828 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1829 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1830 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1831 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1832 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1833 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1834 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1835 TYPE_USES_COMPLEX_INHERITANCE (type
)
1836 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1837 TYPE_USES_MULTIPLE_INHERITANCE (type
)
1838 = TYPE_USES_MULTIPLE_INHERITANCE (pattern
);
1839 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1840 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1841 TYPE_PACKED (type
) = TYPE_PACKED (pattern
);
1842 TYPE_ALIGN (type
) = TYPE_ALIGN (pattern
);
1845 tree binfo
= TYPE_BINFO (type
);
1846 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1852 int len
= TREE_VEC_LENGTH (pbases
);
1853 bases
= make_tree_vec (len
);
1854 for (i
= 0; i
< len
; ++i
)
1858 TREE_VEC_ELT (bases
, i
) = elt
1859 = tsubst (TREE_VEC_ELT (pbases
, i
), args
,
1860 TREE_VEC_LENGTH (args
), NULL_TREE
);
1861 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1863 if (! IS_AGGR_TYPE (TREE_TYPE (elt
)))
1865 ("base type `%T' of `%T' fails to be a struct or class type",
1866 TREE_TYPE (elt
), type
);
1867 else if (! uses_template_parms (type
)
1868 && (TYPE_SIZE (complete_type (TREE_TYPE (elt
)))
1870 cp_error ("base class `%T' of `%T' has incomplete type",
1871 TREE_TYPE (elt
), type
);
1873 /* Don't initialize this until the vector is filled out, or
1874 lookups will crash. */
1875 BINFO_BASETYPES (binfo
) = bases
;
1879 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1881 field_chain
= &TYPE_FIELDS (type
);
1883 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1885 tree name
= TREE_PURPOSE (t
);
1886 tree tag
= TREE_VALUE (t
);
1888 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1889 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1891 tree e
, newtag
= tsubst_enum (tag
, args
,
1892 TREE_VEC_LENGTH (args
), field_chain
);
1894 while (*field_chain
)
1896 DECL_FIELD_CONTEXT (*field_chain
) = type
;
1897 field_chain
= &TREE_CHAIN (*field_chain
);
1902 TREE_VEC_LENGTH (args
), NULL_TREE
);
1905 /* Don't replace enum constants here. */
1906 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1907 if (TREE_CODE (t
) != CONST_DECL
)
1909 tree r
= tsubst (t
, args
,
1910 TREE_VEC_LENGTH (args
), NULL_TREE
);
1911 if (TREE_CODE (r
) == VAR_DECL
)
1913 if (! uses_template_parms (r
))
1914 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1915 /* Perhaps I should do more of grokfield here. */
1917 DECL_IN_AGGR_P (r
) = 1;
1918 DECL_EXTERNAL (r
) = 1;
1919 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1923 field_chain
= &TREE_CHAIN (r
);
1926 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1927 for (t
= TYPE_METHODS (type
); t
; t
= TREE_CHAIN (t
))
1929 if (DECL_CONSTRUCTOR_P (t
))
1930 grok_ctor_properties (type
, t
);
1931 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t
)))
1932 grok_op_properties (t
, DECL_VIRTUAL_P (t
), 0);
1935 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1936 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1937 args
, TREE_VEC_LENGTH (args
), NULL_TREE
);
1940 tree d
= CLASSTYPE_FRIEND_CLASSES (type
)
1941 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), args
,
1942 TREE_VEC_LENGTH (args
), NULL_TREE
);
1944 /* This does injection for friend classes. */
1945 for (; d
; d
= TREE_CHAIN (d
))
1946 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1948 d
= tsubst (DECL_TEMPLATE_INJECT (template), args
,
1949 TREE_VEC_LENGTH (args
), NULL_TREE
);
1951 for (; d
; d
= TREE_CHAIN (d
))
1953 tree t
= TREE_VALUE (d
);
1955 if (TREE_CODE (t
) == TYPE_DECL
)
1956 /* Already injected. */;
1962 if (! uses_template_parms (type
))
1965 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1966 if (TREE_CODE (tmp
) == FIELD_DECL
)
1968 TREE_TYPE (tmp
) = complete_type (TREE_TYPE (tmp
));
1969 require_complete_type (tmp
);
1972 type
= finish_struct_1 (type
, 0);
1973 CLASSTYPE_GOT_SEMICOLON (type
) = 1;
1975 repo_template_used (type
);
1976 if (at_eof
&& TYPE_BINFO_VTABLE (type
) != NULL_TREE
)
1977 finish_prevtable_vardecl (NULL
, TYPE_BINFO_VTABLE (type
));
1981 TYPE_SIZE (type
) = integer_zero_node
;
1982 CLASSTYPE_METHOD_VEC (type
)
1983 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1986 TYPE_BEING_DEFINED (type
) = 0;
1989 pop_from_top_level ();
1999 if (t1
== NULL_TREE
)
2000 return t2
== NULL_TREE
;
2001 if (t2
== NULL_TREE
)
2003 /* Don't care if one declares its arg const and the other doesn't -- the
2004 main variant of the arg type is all that matters. */
2005 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
2006 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
2008 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
2012 lookup_nested_type_by_name (ctype
, name
)
2017 complete_type (ctype
);
2019 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
2021 if (name
== TREE_PURPOSE (t
)
2022 /* this catches typedef enum { foo } bar; */
2023 || name
== TYPE_IDENTIFIER (TREE_VALUE (t
)))
2024 return TREE_VALUE (t
);
2030 tsubst (t
, args
, nargs
, in_decl
)
2037 if (t
== NULL_TREE
|| t
== error_mark_node
2038 || t
== integer_type_node
2039 || t
== void_type_node
2040 || t
== char_type_node
)
2043 type
= TREE_TYPE (t
);
2044 if (type
== unknown_type_node
)
2045 my_friendly_abort (42);
2046 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
2047 && TREE_CODE (t
) != TYPENAME_TYPE
)
2048 type
= tsubst (type
, args
, nargs
, in_decl
);
2050 switch (TREE_CODE (t
))
2053 if (TYPE_PTRMEMFUNC_P (t
))
2055 tree r
= build_ptrmemfunc_type
2056 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
2057 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2061 /* else fall through */
2063 if (uses_template_parms (t
))
2065 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
2066 tree r
= lookup_template_class (t
, argvec
, in_decl
);
2067 return cp_build_type_variant (r
, TYPE_READONLY (t
),
2071 /* else fall through */
2073 case IDENTIFIER_NODE
:
2086 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2087 if (ctx
== NULL_TREE
)
2089 else if (ctx
== current_function_decl
)
2090 return lookup_name (TYPE_IDENTIFIER (t
), 1);
2092 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
2096 if (t
== integer_type_node
)
2099 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
2100 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
2104 tree max
= TREE_OPERAND (TYPE_MAX_VALUE (t
), 0);
2105 max
= tsubst_expr (max
, args
, nargs
, in_decl
);
2106 if (processing_template_decl
)
2108 tree itype
= make_node (INTEGER_TYPE
);
2109 TYPE_MIN_VALUE (itype
) = size_zero_node
;
2110 TYPE_MAX_VALUE (itype
) = build_min (MINUS_EXPR
, sizetype
, max
,
2115 max
= fold (build_binary_op (MINUS_EXPR
, max
, integer_one_node
, 1));
2116 return build_index_2_type (size_zero_node
, max
);
2119 case TEMPLATE_TYPE_PARM
:
2120 case TEMPLATE_CONST_PARM
:
2125 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2127 idx
= TEMPLATE_TYPE_IDX (t
);
2128 level
= TEMPLATE_TYPE_LEVEL (t
);
2132 idx
= TEMPLATE_CONST_IDX (t
);
2133 level
= TEMPLATE_CONST_LEVEL (t
);
2136 if (TREE_VEC_LENGTH (args
) > 0)
2138 tree arg
= NULL_TREE
;
2140 if (TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
)
2142 if (TREE_VEC_LENGTH (args
) >= level
- 1)
2144 (TREE_VEC_ELT (args
, level
- 1), idx
);
2146 else if (level
== 1)
2147 arg
= TREE_VEC_ELT (args
, idx
);
2149 if (arg
!= NULL_TREE
)
2151 if (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
)
2152 return cp_build_type_variant
2153 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
2154 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
2160 /* If we get here, we must have been looking at a parm for a
2161 more deeply nested template. */
2162 my_friendly_assert((TREE_CODE (t
) == TEMPLATE_CONST_PARM
2163 && TEMPLATE_CONST_LEVEL (t
) > 1)
2164 || (TREE_CODE (t
) == TEMPLATE_TYPE_PARM
2165 && TEMPLATE_TYPE_LEVEL (t
) > 1),
2172 /* We can get here when processing a member template function
2173 of a template class. */
2175 tree decl
= DECL_TEMPLATE_RESULT (t
);
2181 /* We might already have an instance of this template. */
2182 tree instances
= DECL_TEMPLATE_INSTANTIATIONS (t
);
2183 tree ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, in_decl
);
2185 for (; instances
; instances
= TREE_CHAIN (instances
))
2186 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances
)) == ctx
)
2187 return TREE_VALUE (instances
);
2189 /* Make a new template decl. It will be similar to the
2190 original, but will record the current template arguments.
2191 We also create a new function declaration, which is just
2192 like the old one, but points to this new template, rather
2193 than the old one. */
2194 tmpl
= copy_node (t
);
2195 copy_lang_decl (tmpl
);
2196 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl
) != 0, 0);
2197 DECL_CHAIN (tmpl
) = NULL_TREE
;
2198 TREE_CHAIN (tmpl
) = NULL_TREE
;
2199 DECL_TEMPLATE_INFO (tmpl
) = build_tree_list (t
, args
);
2200 new_decl
= tsubst (decl
, args
, nargs
, in_decl
);
2201 DECL_RESULT (tmpl
) = new_decl
;
2202 DECL_TI_TEMPLATE (new_decl
) = tmpl
;
2203 TREE_TYPE (tmpl
) = TREE_TYPE (new_decl
);
2204 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) = NULL_TREE
;
2205 SET_DECL_IMPLICIT_INSTANTIATION (tmpl
);
2207 /* The template parameters for this new template are all the
2208 template parameters for the old template, except the
2209 outermost level of parameters. */
2210 DECL_TEMPLATE_PARMS (tmpl
)
2211 = copy_node (DECL_TEMPLATE_PARMS (tmpl
));
2212 for (parms
= DECL_TEMPLATE_PARMS (tmpl
);
2213 TREE_CHAIN (parms
) != NULL_TREE
;
2214 parms
= TREE_CHAIN (parms
))
2215 TREE_CHAIN (parms
) = copy_node (TREE_CHAIN (parms
));
2217 /* Record this partial instantiation. */
2218 DECL_TEMPLATE_INSTANTIATIONS (t
)
2219 = perm_tree_cons (NULL_TREE
, tmpl
,
2220 DECL_TEMPLATE_INSTANTIATIONS (t
));
2222 DECL_TEMPLATE_SPECIALIZATIONS (tmpl
) = NULL_TREE
;
2233 if (DECL_CONTEXT (t
) != NULL_TREE
2234 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
2236 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
2240 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
2241 type
= tsubst (type
, args
, nargs
, in_decl
);
2247 type
= tsubst (type
, args
, nargs
, in_decl
);
2250 if (type
== TREE_TYPE (t
)
2251 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
2258 /* Do we already have this instantiation? */
2259 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2261 tree tmpl
= DECL_TI_TEMPLATE (t
);
2262 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2264 for (; decls
; decls
= TREE_CHAIN (decls
))
2265 if (TREE_TYPE (TREE_VALUE (decls
)) == type
2266 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
2267 return TREE_VALUE (decls
);
2270 /* We do NOT check for matching decls pushed separately at this
2271 point, as they may not represent instantiations of this
2272 template, and in any case are considered separate under the
2273 discrete model. Instead, see add_maybe_template. */
2277 TREE_TYPE (r
) = type
;
2280 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
2281 DECL_CLASS_CONTEXT (r
) = ctx
;
2283 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
2284 IDENTIFIER_POINTER (DECL_NAME (r
)),
2285 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
2287 /* Type-conversion operator. Reconstruct the name, in
2288 case it's the name of one of the template's parameters. */
2289 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
2292 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
2294 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
2295 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
2296 buf
= (char *) alloca (strlen (dbuf
)
2297 + sizeof (DESTRUCTOR_DECL_PREFIX
));
2298 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
2301 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
2305 /* Instantiations of template functions must be mangled
2306 specially, in order to conform to 14.5.5.1
2307 [temp.over.link]. We use in_decl below rather than
2308 DECL_TI_TEMPLATE (r) because the latter is set to
2309 NULL_TREE in instantiate_decl. */
2313 if (DECL_TEMPLATE_INFO (r
))
2314 tmpl
= DECL_TI_TEMPLATE (r
);
2318 /* tmpl will be NULL if this is a specialization of a
2319 member template of a template class. */
2320 if (name_mangling_version
< 1
2321 || tmpl
== NULL_TREE
2322 || (member
&& !is_member_template (tmpl
)
2323 && !DECL_TEMPLATE_INFO (tmpl
)))
2325 arg_types
= TYPE_ARG_TYPES (type
);
2326 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2327 arg_types
= hash_tree_chain
2328 (build_pointer_type (DECL_CONTEXT (r
)),
2331 DECL_ASSEMBLER_NAME (r
)
2332 = build_decl_overload (DECL_NAME (r
), arg_types
,
2337 /* We pass the outermost template parameters to
2338 build_template_decl_overload since the innermost
2339 template parameters are still just template
2340 parameters; there are no corresponding subsitution
2342 /* FIXME The messed up thing here is that we get here with
2343 full args and only one level of parms. This is necessary
2344 because when we partially instantiate a member template,
2345 even though there's really only one level of parms left
2346 we re-use the parms from the original template, which
2347 have level 2. When this is fixed we can remove the
2348 add_to_template_args from instantiate_template. */
2349 tree tparms
= DECL_TEMPLATE_PARMS (tmpl
);
2351 while (tparms
&& TREE_CHAIN (tparms
) != NULL_TREE
)
2352 tparms
= TREE_CHAIN (tparms
);
2354 my_friendly_assert (tparms
!= NULL_TREE
2355 && TREE_CODE (tparms
) == TREE_LIST
,
2357 tparms
= TREE_VALUE (tparms
);
2359 arg_types
= TYPE_ARG_TYPES (TREE_TYPE (tmpl
));
2360 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
2361 arg_types
= hash_tree_chain
2362 (build_pointer_type (DECL_CONTEXT (r
)),
2365 DECL_ASSEMBLER_NAME (r
)
2366 = build_template_decl_overload
2367 (DECL_NAME (r
), arg_types
,
2368 TREE_TYPE (TREE_TYPE (tmpl
)),
2370 TREE_CODE (TREE_VEC_ELT (args
, 0)) == TREE_VEC
2371 ? TREE_VEC_ELT (args
, TREE_VEC_LENGTH (args
) - 1) :
2377 make_decl_rtl (r
, NULL_PTR
, 1);
2379 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
2380 DECL_MAIN_VARIANT (r
) = r
;
2381 DECL_RESULT (r
) = NULL_TREE
;
2382 DECL_INITIAL (r
) = NULL_TREE
;
2384 TREE_STATIC (r
) = 0;
2385 TREE_PUBLIC (r
) = 1;
2386 DECL_EXTERNAL (r
) = 1;
2387 DECL_INTERFACE_KNOWN (r
) = 0;
2388 DECL_DEFER_OUTPUT (r
) = 0;
2389 TREE_CHAIN (r
) = NULL_TREE
;
2390 DECL_CHAIN (r
) = NULL_TREE
;
2392 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
2393 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
2395 /* Look for matching decls for the moment. */
2396 if (! member
&& ! flag_ansi_overloading
)
2398 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
2401 if (decls
== NULL_TREE
)
2403 else if (is_overloaded_fn (decls
))
2404 for (decls
= get_first_fn (decls
); decls
;
2405 decls
= DECL_CHAIN (decls
))
2407 if (TREE_CODE (decls
) == FUNCTION_DECL
2408 && TREE_TYPE (decls
) == type
)
2417 int dcl_only
= ! DECL_INITIAL (d
);
2419 DECL_INITIAL (r
) = error_mark_node
;
2420 duplicate_decls (r
, d
);
2423 DECL_INITIAL (r
) = 0;
2427 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
2429 tree tmpl
= DECL_TI_TEMPLATE (t
);
2430 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2431 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2433 if (DECL_TEMPLATE_INFO (tmpl
) && DECL_TI_ARGS (tmpl
))
2434 argvec
= add_to_template_args (DECL_TI_ARGS (tmpl
), argvec
);
2436 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2437 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2439 /* If we have a preexisting version of this function, don't expand
2440 the template version, use the other instead. */
2441 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
2442 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
2444 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2446 DECL_TEMPLATE_INSTANTIATIONS (tmpl
)
2447 = tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
2450 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2451 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2453 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) == NULL_TREE
)
2454 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r
)) = r
;
2461 tree r
= copy_node (t
);
2462 TREE_TYPE (r
) = type
;
2463 DECL_INITIAL (r
) = TREE_TYPE (r
);
2464 DECL_CONTEXT (r
) = NULL_TREE
;
2465 #ifdef PROMOTE_PROTOTYPES
2466 if ((TREE_CODE (type
) == INTEGER_TYPE
2467 || TREE_CODE (type
) == ENUMERAL_TYPE
)
2468 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
2469 DECL_ARG_TYPE (r
) = integer_type_node
;
2472 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
2478 tree r
= copy_node (t
);
2479 TREE_TYPE (r
) = type
;
2482 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
2484 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2485 TREE_CHAIN (r
) = NULL_TREE
;
2491 tree r
= copy_node (t
);
2493 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
2494 TREE_CHAIN (r
) = NULL_TREE
;
2501 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2503 /* Do we already have this instantiation? */
2504 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2506 tree tmpl
= DECL_TI_TEMPLATE (t
);
2507 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2509 for (; decls
; decls
= TREE_CHAIN (decls
))
2510 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
2511 return TREE_VALUE (decls
);
2515 TREE_TYPE (r
) = type
;
2516 DECL_CONTEXT (r
) = ctx
;
2517 if (TREE_STATIC (r
))
2518 DECL_ASSEMBLER_NAME (r
)
2519 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
2521 /* Don't try to expand the initializer until someone tries to use
2522 this variable; otherwise we run into circular dependencies. */
2523 DECL_INITIAL (r
) = NULL_TREE
;
2528 if (DECL_LANG_SPECIFIC (r
))
2531 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
2534 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2536 tree tmpl
= DECL_TI_TEMPLATE (t
);
2537 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
2538 tree argvec
= tsubst (DECL_TI_ARGS (t
), args
, nargs
, in_decl
);
2540 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
2541 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
2542 SET_DECL_IMPLICIT_INSTANTIATION (r
);
2544 TREE_CHAIN (r
) = NULL_TREE
;
2549 if (t
== TYPE_NAME (TREE_TYPE (t
)))
2550 return TYPE_NAME (type
);
2553 tree r
= copy_node (t
);
2554 TREE_TYPE (r
) = type
;
2555 DECL_CONTEXT (r
) = current_class_type
;
2556 TREE_CHAIN (r
) = NULL_TREE
;
2562 tree purpose
, value
, chain
, result
;
2563 int via_public
, via_virtual
, via_protected
;
2565 if (t
== void_list_node
)
2568 via_public
= TREE_VIA_PUBLIC (t
);
2569 via_protected
= TREE_VIA_PROTECTED (t
);
2570 via_virtual
= TREE_VIA_VIRTUAL (t
);
2572 purpose
= TREE_PURPOSE (t
);
2574 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
2575 value
= TREE_VALUE (t
);
2577 value
= tsubst (value
, args
, nargs
, in_decl
);
2578 chain
= TREE_CHAIN (t
);
2579 if (chain
&& chain
!= void_type_node
)
2580 chain
= tsubst (chain
, args
, nargs
, in_decl
);
2581 if (purpose
== TREE_PURPOSE (t
)
2582 && value
== TREE_VALUE (t
)
2583 && chain
== TREE_CHAIN (t
))
2585 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
2586 purpose
, value
, chain
);
2587 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
2591 if (type
!= NULL_TREE
)
2597 if (type
== TREE_TYPE (t
))
2600 TREE_TYPE (t
) = complete_type (type
);
2601 if (IS_AGGR_TYPE (type
))
2603 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
2604 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
2605 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
2606 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
2611 /* Otherwise, a vector of template arguments. */
2613 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
2614 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
2616 bzero ((char *) elts
, len
* sizeof (tree
));
2618 for (i
= 0; i
< len
; i
++)
2620 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
2622 if (TREE_CODE_CLASS (TREE_CODE (elts
[i
])) != 't'
2623 && !uses_template_parms (elts
[i
]))
2625 /* Sometimes, one of the args was an expression involving a
2626 template constant parameter, like N - 1. Now that we've
2627 tsubst'd, we might have something like 2 - 1. This will
2628 confuse lookup_template_class, so we do constant folding
2629 here. We have to unset processing_template_decl, to
2630 fool build_expr_from_tree() into building an actual
2633 int saved_processing_template_decl
= processing_template_decl
;
2634 processing_template_decl
= 0;
2635 elts
[i
] = fold (build_expr_from_tree (elts
[i
]));
2636 processing_template_decl
= saved_processing_template_decl
;
2639 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
2646 t
= make_tree_vec (len
);
2647 for (i
= 0; i
< len
; i
++)
2648 TREE_VEC_ELT (t
, i
) = elts
[i
];
2653 case REFERENCE_TYPE
:
2656 enum tree_code code
;
2657 if (type
== TREE_TYPE (t
))
2660 code
= TREE_CODE (t
);
2661 if (code
== POINTER_TYPE
)
2662 r
= build_pointer_type (type
);
2664 r
= build_reference_type (type
);
2665 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
2666 /* Will this ever be needed for TYPE_..._TO values? */
2671 return build_offset_type
2672 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
2676 tree values
= TYPE_ARG_TYPES (t
);
2677 tree context
= TYPE_CONTEXT (t
);
2678 tree raises
= TYPE_RAISES_EXCEPTIONS (t
);
2681 /* Don't bother recursing if we know it won't change anything. */
2682 if (values
!= void_list_node
)
2684 /* This should probably be rewritten to use hash_tree_cons for
2685 the memory savings. */
2686 tree first
= NULL_TREE
;
2689 for (; values
&& values
!= void_list_node
;
2690 values
= TREE_CHAIN (values
))
2692 tree value
= TYPE_MAIN_VARIANT (type_decays_to
2693 (tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
)));
2694 /* Don't instantiate default args unless they are used.
2695 Handle it in build_over_call instead. */
2696 tree purpose
= TREE_PURPOSE (values
);
2697 tree x
= build_tree_list (purpose
, value
);
2700 TREE_CHAIN (last
) = x
;
2706 if (values
== void_list_node
)
2707 TREE_CHAIN (last
) = void_list_node
;
2712 context
= tsubst (context
, args
, nargs
, in_decl
);
2713 /* Could also optimize cases where return value and
2714 values have common elements (e.g., T min(const &T, const T&). */
2716 /* If the above parameters haven't changed, just return the type. */
2717 if (type
== TREE_TYPE (t
)
2718 && values
== TYPE_VALUES (t
)
2719 && context
== TYPE_CONTEXT (t
))
2722 /* Construct a new type node and return it. */
2723 if (TREE_CODE (t
) == FUNCTION_TYPE
2724 && context
== NULL_TREE
)
2726 fntype
= build_function_type (type
, values
);
2728 else if (context
== NULL_TREE
)
2730 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
2731 args
, nargs
, in_decl
);
2732 fntype
= build_cplus_method_type (base
, type
,
2733 TREE_CHAIN (values
));
2737 fntype
= make_node (TREE_CODE (t
));
2738 TREE_TYPE (fntype
) = type
;
2739 TYPE_CONTEXT (fntype
) = context
;
2740 TYPE_VALUES (fntype
) = values
;
2741 TYPE_SIZE (fntype
) = TYPE_SIZE (t
);
2742 TYPE_ALIGN (fntype
) = TYPE_ALIGN (t
);
2743 TYPE_MODE (fntype
) = TYPE_MODE (t
);
2744 if (TYPE_METHOD_BASETYPE (t
))
2745 TYPE_METHOD_BASETYPE (fntype
) = tsubst (TYPE_METHOD_BASETYPE (t
),
2746 args
, nargs
, in_decl
);
2747 /* Need to generate hash value. */
2748 my_friendly_abort (84);
2750 fntype
= build_type_variant (fntype
,
2755 raises
= tsubst (raises
, args
, nargs
, in_decl
);
2756 fntype
= build_exception_variant (fntype
, raises
);
2762 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
2764 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
2766 r
= build_cplus_array_type (type
, domain
);
2772 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
2773 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2774 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
2778 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
2779 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
2783 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
2784 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
2785 return cp_build_type_variant
2786 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
2787 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
2791 return make_pointer_declarator
2792 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2795 return make_reference_declarator
2796 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2799 return build_parse_node
2800 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2801 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2804 return make_call_declarator
2805 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2806 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2807 TREE_OPERAND (t
, 2),
2808 tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
2811 return build_parse_node
2812 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2813 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2816 sorry ("use of `%s' in template",
2817 tree_code_name
[(int) TREE_CODE (t
)]);
2818 return error_mark_node
;
2825 emit_line_note (input_filename
, lineno
);
2829 expand_start_bindings (0);
2836 int saved_warn_unused
;
2838 if (processing_template_decl
)
2840 saved_warn_unused
= warn_unused
;
2843 expand_end_bindings (getdecls (), kept_level_p (), 1);
2844 if (processing_template_decl
)
2845 warn_unused
= saved_warn_unused
;
2846 t
= poplevel (kept_level_p (), 1, 0);
2852 tsubst_copy (t
, args
, nargs
, in_decl
)
2857 enum tree_code code
;
2859 if (t
== NULL_TREE
|| t
== error_mark_node
)
2862 code
= TREE_CODE (t
);
2867 return do_identifier (DECL_NAME (t
), 0);
2871 if (DECL_CONTEXT (t
))
2873 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
2874 if (ctx
== current_function_decl
)
2875 return lookup_name (DECL_NAME (t
), 0);
2876 else if (ctx
!= DECL_CONTEXT (t
))
2877 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
2883 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
2884 t
= tsubst (t
, args
, nargs
, in_decl
);
2889 if (is_member_template (t
))
2890 return tsubst (t
, args
, nargs
, in_decl
);
2895 case IDENTIFIER_NODE
:
2896 return do_identifier (t
, 0);
2900 case REINTERPRET_CAST_EXPR
:
2901 case CONST_CAST_EXPR
:
2902 case STATIC_CAST_EXPR
:
2903 case DYNAMIC_CAST_EXPR
:
2905 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
2906 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2909 case PREDECREMENT_EXPR
:
2910 case PREINCREMENT_EXPR
:
2911 case POSTDECREMENT_EXPR
:
2912 case POSTINCREMENT_EXPR
:
2914 case TRUTH_NOT_EXPR
:
2917 case CONVERT_EXPR
: /* Unary + */
2924 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2929 case TRUNC_DIV_EXPR
:
2931 case FLOOR_DIV_EXPR
:
2932 case ROUND_DIV_EXPR
:
2933 case EXACT_DIV_EXPR
:
2935 case BIT_ANDTC_EXPR
:
2938 case TRUNC_MOD_EXPR
:
2939 case FLOOR_MOD_EXPR
:
2940 case TRUTH_ANDIF_EXPR
:
2941 case TRUTH_ORIF_EXPR
:
2942 case TRUTH_AND_EXPR
:
2963 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2964 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2968 tree fn
= TREE_OPERAND (t
, 0);
2969 if (really_overloaded_fn (fn
))
2970 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
2972 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
2974 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2978 case METHOD_CALL_EXPR
:
2980 tree name
= TREE_OPERAND (t
, 0);
2981 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
2983 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2984 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2986 else if (TREE_CODE (name
) == SCOPE_REF
2987 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
2989 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2990 name
= TREE_OPERAND (name
, 1);
2991 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
2992 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
2993 name
= build_nt (SCOPE_REF
, base
, name
);
2996 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2998 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2999 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
3006 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3007 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3008 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3013 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3014 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3015 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
3016 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
3023 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3024 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3025 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
3026 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
3030 case TEMPLATE_ID_EXPR
:
3032 tree r
= lookup_template_function
3033 (tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3034 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3040 tree purpose
, value
, chain
;
3042 if (t
== void_list_node
)
3045 purpose
= TREE_PURPOSE (t
);
3047 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
3048 value
= TREE_VALUE (t
);
3050 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
3051 chain
= TREE_CHAIN (t
);
3052 if (chain
&& chain
!= void_type_node
)
3053 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
3054 if (purpose
== TREE_PURPOSE (t
)
3055 && value
== TREE_VALUE (t
)
3056 && chain
== TREE_CHAIN (t
))
3058 return tree_cons (purpose
, value
, chain
);
3065 case TEMPLATE_TYPE_PARM
:
3066 case TEMPLATE_CONST_PARM
:
3068 case REFERENCE_TYPE
:
3074 return tsubst (t
, args
, nargs
, in_decl
);
3076 case IDENTIFIER_NODE
:
3077 if (IDENTIFIER_TYPENAME_P (t
))
3078 return build_typename_overload
3079 (tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
));
3085 (CONSTRUCTOR
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
), NULL_TREE
,
3086 tsubst_copy (CONSTRUCTOR_ELTS (t
), args
, nargs
, in_decl
));
3094 tsubst_expr (t
, args
, nargs
, in_decl
)
3099 if (t
== NULL_TREE
|| t
== error_mark_node
)
3102 if (processing_template_decl
)
3103 return tsubst_copy (t
, args
, nargs
, in_decl
);
3105 switch (TREE_CODE (t
))
3108 lineno
= TREE_COMPLEXITY (t
);
3109 emit_line_note (input_filename
, lineno
);
3111 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3116 lineno
= TREE_COMPLEXITY (t
);
3117 emit_line_note (input_filename
, lineno
);
3118 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3119 /* Do default conversion if safe and possibly important,
3120 in case within ({...}). */
3121 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
3122 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
3123 t
= default_conversion (t
);
3124 cplus_expand_expr_stmt (t
);
3131 int i
= suspend_momentary ();
3134 lineno
= TREE_COMPLEXITY (t
);
3135 emit_line_note (input_filename
, lineno
);
3137 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3138 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
3139 TREE_OPERAND (t
, 2) != 0);
3140 init
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3142 (dcl
, init
, NULL_TREE
, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3143 resume_momentary (i
);
3150 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
3151 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3152 int cond_scope
= (TREE_OPERAND (t
, 1)
3153 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
3155 lineno
= TREE_COMPLEXITY (t
);
3156 emit_line_note (input_filename
, lineno
);
3159 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
3160 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3162 emit_line_note (input_filename
, lineno
);
3163 expand_start_loop_continue_elsewhere (1);
3167 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3168 emit_line_note (input_filename
, lineno
);
3170 expand_exit_loop_if_false (0, condition_conversion (tmp
));
3174 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
3177 emit_line_note (input_filename
, lineno
);
3178 expand_loop_continue_here ();
3179 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
3181 cplus_expand_expr_stmt (tmp
);
3194 lineno
= TREE_COMPLEXITY (t
);
3196 emit_line_note (input_filename
, lineno
);
3197 expand_start_loop (1);
3199 cond
= TREE_OPERAND (t
, 0);
3200 if (TREE_CODE (cond
) == DECL_STMT
)
3202 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
3203 emit_line_note (input_filename
, lineno
);
3204 expand_exit_loop_if_false (0, condition_conversion (cond
));
3206 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
3208 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3220 lineno
= TREE_COMPLEXITY (t
);
3222 emit_line_note (input_filename
, lineno
);
3223 expand_start_loop_continue_elsewhere (1);
3225 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3226 expand_loop_continue_here ();
3228 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3229 emit_line_note (input_filename
, lineno
);
3230 expand_exit_loop_if_false (0, condition_conversion (cond
));
3241 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3243 lineno
= TREE_COMPLEXITY (t
);
3246 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3247 emit_line_note (input_filename
, lineno
);
3248 expand_start_cond (condition_conversion (tmp
), 0);
3250 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3251 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3253 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
3255 expand_start_else ();
3256 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3270 tree substmt
= TREE_OPERAND (t
, 0);
3272 lineno
= TREE_COMPLEXITY (t
);
3274 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3277 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
3278 tsubst_expr (substmt
, args
, nargs
, in_decl
);
3280 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
3286 lineno
= TREE_COMPLEXITY (t
);
3287 emit_line_note (input_filename
, lineno
);
3288 if (! expand_exit_something ())
3289 error ("break statement not within loop or switch");
3293 lineno
= TREE_COMPLEXITY (t
);
3294 emit_line_note (input_filename
, lineno
);
3295 if (! expand_continue_loop (0))
3296 error ("continue statement not within a loop");
3302 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
3304 lineno
= TREE_COMPLEXITY (t
);
3307 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3308 emit_line_note (input_filename
, lineno
);
3309 c_expand_start_case (val
);
3312 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
3313 tsubst_expr (tmp
, args
, nargs
, in_decl
);
3315 expand_end_case (val
);
3326 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
3327 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
3331 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
3338 lineno
= TREE_COMPLEXITY (t
);
3339 emit_line_note (input_filename
, lineno
);
3340 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
3342 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
3343 TREE_USED (decl
) = 1;
3347 expand_computed_goto
3348 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
3352 lineno
= TREE_COMPLEXITY (t
);
3353 emit_line_note (input_filename
, lineno
);
3354 expand_start_try_stmts ();
3355 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
3356 expand_start_all_catch ();
3358 tree handler
= TREE_OPERAND (t
, 1);
3359 for (; handler
; handler
= TREE_CHAIN (handler
))
3360 tsubst_expr (handler
, args
, nargs
, in_decl
);
3362 expand_end_all_catch ();
3366 lineno
= TREE_COMPLEXITY (t
);
3368 if (TREE_OPERAND (t
, 0))
3370 tree d
= TREE_OPERAND (t
, 0);
3371 expand_start_catch_block
3372 (tsubst (TREE_OPERAND (d
, 1), args
, nargs
, in_decl
),
3373 tsubst (TREE_OPERAND (d
, 0), args
, nargs
, in_decl
));
3376 expand_start_catch_block (NULL_TREE
, NULL_TREE
);
3377 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
3378 expand_end_catch_block ();
3383 lineno
= TREE_COMPLEXITY (t
);
3385 if (TREE_CODE (t
) == ENUMERAL_TYPE
)
3386 tsubst_enum (t
, args
, nargs
, NULL
);
3390 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
3396 instantiate_template (tmpl
, targ_ptr
)
3397 tree tmpl
, targ_ptr
;
3401 struct obstack
*old_fmp_obstack
;
3402 extern struct obstack
*function_maybepermanent_obstack
;
3404 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
3406 if (DECL_FUNCTION_TEMPLATE_P (tmpl
))
3410 /* Check to see if there is a matching specialization. */
3411 for (specs
= DECL_TEMPLATE_SPECIALIZATIONS (tmpl
);
3413 specs
= TREE_CHAIN (specs
))
3414 if (comp_template_args (TREE_PURPOSE (specs
), targ_ptr
))
3415 return TREE_VALUE (specs
);
3418 push_obstacks (&permanent_obstack
, &permanent_obstack
);
3419 old_fmp_obstack
= function_maybepermanent_obstack
;
3420 function_maybepermanent_obstack
= &permanent_obstack
;
3422 len
= DECL_NTPARMS (tmpl
);
3427 tree t
= TREE_VEC_ELT (targ_ptr
, i
);
3428 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
3430 tree nt
= target_type (t
);
3431 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
3433 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
3434 cp_error (" trying to instantiate `%D'", tmpl
);
3435 fndecl
= error_mark_node
;
3439 TREE_VEC_ELT (targ_ptr
, i
) = copy_to_permanent (t
);
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_unfication 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
= tree_cons (NULL_TREE
,
3495 TREE_TYPE (TREE_TYPE (fn
)),
3497 decl_arg_types
= 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
= 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_tree_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 DECL_TEMPLATE_INFO (decl_pattern
) = save_ti
;
4515 /* And set up DECL_INITIAL, since tsubst doesn't. */
4516 if (TREE_CODE (td
) == VAR_DECL
)
4518 pushclass (DECL_CONTEXT (d
), 2);
4519 DECL_INITIAL (td
) = tsubst_expr (DECL_INITIAL (code_pattern
), args
,
4520 TREE_VEC_LENGTH (args
), tmpl
);
4524 if (TREE_CODE (d
) == FUNCTION_DECL
)
4526 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4528 DECL_INITIAL (td
) = error_mark_node
;
4530 if (DECL_TEMPLATE_SPECIALIZATION (td
) && !DECL_TEMPLATE_INFO (td
))
4531 /* Set up the information about what is being specialized. */
4532 DECL_TEMPLATE_INFO (td
) = DECL_TEMPLATE_INFO (d
);
4534 duplicate_decls (td
, d
);
4535 if (TREE_CODE (d
) == FUNCTION_DECL
)
4536 DECL_INITIAL (td
) = 0;
4538 if (TREE_CODE (d
) == VAR_DECL
)
4540 DECL_IN_AGGR_P (d
) = 0;
4541 if (DECL_INTERFACE_KNOWN (d
))
4542 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
4545 DECL_EXTERNAL (d
) = 1;
4546 DECL_NOT_REALLY_EXTERN (d
) = 1;
4548 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
4550 else if (TREE_CODE (d
) == FUNCTION_DECL
)
4552 tree t
= DECL_SAVED_TREE (code_pattern
);
4554 start_function (NULL_TREE
, d
, NULL_TREE
, 1);
4555 store_parm_decls ();
4557 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
4560 (TREE_OPERAND (t
, 0),
4561 tsubst_expr (TREE_OPERAND (t
, 1), args
,
4562 TREE_VEC_LENGTH (args
), tmpl
));
4566 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
4568 current_member_init_list
4569 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
4570 current_base_init_list
4571 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
4576 /* Always keep the BLOCK node associated with the outermost
4577 pair of curley braces of a function. These are needed
4578 for correct operation of dwarfout.c. */
4581 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
4582 tsubst_expr (t
, args
, TREE_VEC_LENGTH (args
), tmpl
);
4584 finish_function (lineno
, 0, nested
);
4589 input_filename
= file
;
4591 pop_from_top_level ();
4598 tsubst_chain (t
, argvec
)
4603 tree first
= tsubst (t
, argvec
,
4604 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4607 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
4609 tree x
= tsubst (t
, argvec
, TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4610 TREE_CHAIN (last
) = x
;
4620 tsubst_expr_values (t
, argvec
)
4623 tree first
= NULL_TREE
;
4626 for (; t
; t
= TREE_CHAIN (t
))
4628 tree pur
= tsubst_copy (TREE_PURPOSE (t
), argvec
,
4629 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4630 tree val
= tsubst_expr (TREE_VALUE (t
), argvec
,
4631 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
4632 *p
= build_tree_list (pur
, val
);
4633 p
= &TREE_CHAIN (*p
);
4644 last_tree
= TREE_CHAIN (last_tree
) = t
;
4647 /* D is an undefined function declaration in the presence of templates with
4648 the same name, listed in FNS. If one of them can produce D as an
4649 instantiation, remember this so we can instantiate it at EOF if D has
4650 not been defined by that time. */
4653 add_maybe_template (d
, fns
)
4658 if (DECL_MAYBE_TEMPLATE (d
))
4661 t
= most_specialized (fns
, d
);
4664 if (t
== error_mark_node
)
4666 cp_error ("ambiguous template instantiation for `%D'", d
);
4670 *maybe_template_tail
= perm_tree_cons (t
, d
, NULL_TREE
);
4671 maybe_template_tail
= &TREE_CHAIN (*maybe_template_tail
);
4672 DECL_MAYBE_TEMPLATE (d
) = 1;
4675 /* Instantiate an enumerated type. Used by instantiate_class_template and
4679 tsubst_enum (tag
, args
, nargs
, field_chain
)
4684 extern tree current_local_enum
;
4685 tree prev_local_enum
= current_local_enum
;
4687 tree newtag
= start_enum (TYPE_IDENTIFIER (tag
));
4688 tree e
, values
= NULL_TREE
;
4690 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
4692 tree elt
= build_enumerator (TREE_PURPOSE (e
),
4693 tsubst_expr (TREE_VALUE (e
), args
,
4695 TREE_CHAIN (elt
) = values
;
4699 finish_enum (newtag
, values
);
4701 if (NULL
!= field_chain
)
4702 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
4704 current_local_enum
= prev_local_enum
;