1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
46 extern struct obstack permanent_obstack;
49 extern char *input_filename;
50 struct pending_inline *pending_template_expansions;
52 tree current_template_parms;
53 HOST_WIDE_INT processing_template_decl;
55 tree pending_templates;
56 static tree *template_tail = &pending_templates;
59 static tree *maybe_template_tail = &maybe_templates;
61 int minimal_parse_mode;
63 int processing_specialization;
64 static int template_header_count;
66 #define obstack_chunk_alloc xmalloc
67 #define obstack_chunk_free free
69 static int unify PROTO((tree, tree *, int, tree, tree, int *, int));
70 static void add_pending_template PROTO((tree));
71 static int push_tinst_level PROTO((tree));
72 static tree classtype_mangled_name PROTO((tree));
73 static char *mangle_class_name_for_template PROTO((char *, tree, tree));
74 static tree tsubst_expr_values PROTO((tree, tree));
75 static int comp_template_args PROTO((tree, tree));
76 static int list_eq PROTO((tree, tree));
77 static tree get_class_bindings PROTO((tree, tree, tree));
78 static tree coerce_template_parms PROTO((tree, tree, tree));
79 static tree tsubst_enum PROTO((tree, tree, int, tree *));
80 static tree add_to_template_args PROTO((tree, tree));
81 static int type_unification_real PROTO((tree, tree *, tree, tree, int*,
83 static int processing_explicit_specialization PROTO((int));
84 static void note_template_header PROTO((int));
86 /* Restore the template parameter context. */
89 begin_member_template_processing (decl)
95 parms = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
97 ++processing_template_decl;
98 current_template_parms
99 = tree_cons (build_int_2 (0, processing_template_decl),
100 parms, current_template_parms);
102 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
104 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
105 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
107 switch (TREE_CODE (parm))
115 /* Make a CONST_DECL as is done in process_template_parm. */
116 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
118 DECL_INITIAL (decl) = DECL_INITIAL (parm);
124 my_friendly_abort (0);
129 /* Undo the effects of begin_member_template_processing. */
132 end_member_template_processing ()
134 if (! processing_template_decl)
137 --processing_template_decl;
138 current_template_parms = TREE_CHAIN (current_template_parms);
142 /* Returns non-zero iff T is a member template function. Works if T
143 is either a FUNCTION_DECL or a TEMPLATE_DECL. */
146 is_member_template (t)
151 if (TREE_CODE (t) != FUNCTION_DECL
152 && !DECL_FUNCTION_TEMPLATE_P (t))
153 /* Anything that isn't a template or a template function is
154 certainly not a member template. */
157 if ((DECL_FUNCTION_MEMBER_P (t)
158 && !DECL_TEMPLATE_SPECIALIZATION (t))
159 || (TREE_CODE (t) == TEMPLATE_DECL &&
160 DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
162 tree tmpl = NULL_TREE;
164 if (DECL_FUNCTION_TEMPLATE_P (t))
166 else if (DECL_TEMPLATE_INFO (t)
167 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
168 tmpl = DECL_TI_TEMPLATE (t);
172 tree parms = DECL_TEMPLATE_PARMS (tmpl);
173 int parm_levels = list_length (parms);
174 int template_class_levels = 0;
175 tree ctx = DECL_CLASS_CONTEXT (t);
177 if (CLASSTYPE_TEMPLATE_INFO (ctx))
181 /* Here, we should really count the number of levels
182 deep ctx is, making sure not to count any levels that
183 are just specializations. Since there are no member
184 template classes yet, we don't have to do all that. */
186 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
187 template_class_levels = 1;
192 args = CLASSTYPE_TI_ARGS (ctx);
194 if (args == NULL_TREE)
195 template_class_levels = 1;
197 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
198 if (uses_template_parms (TREE_VEC_ELT (args, i)))
200 template_class_levels++;
206 if (parm_levels > template_class_levels)
214 /* Return a new template argument vector which contains all of ARGS,
215 but has as its innermost set of arguments the EXTRA_ARGS. */
218 add_to_template_args (args, extra_args)
224 if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC)
226 new_args = make_tree_vec (2);
227 TREE_VEC_ELT (new_args, 0) = args;
233 new_args = make_tree_vec (TREE_VEC_LENGTH (args) - 1);
235 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
236 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
239 TREE_VEC_ELT (new_args,
240 TREE_VEC_LENGTH (new_args) - 1) = extra_args;
245 /* We've got a template header coming up; push to a new level for storing
249 begin_template_parm_list ()
252 declare_pseudo_global_level ();
253 ++processing_template_decl;
254 note_template_header (0);
258 /* We've just seen template <>. */
261 begin_specialization ()
263 note_template_header (1);
267 /* Called at then end of processing a declaration preceeded by
271 end_specialization ()
273 reset_specialization ();
277 /* Any template <>'s that we have seen thus far are not referring to a
278 function specialization. */
281 reset_specialization ()
283 processing_specialization = 0;
284 template_header_count = 0;
288 /* We've just seen a template header. If SPECIALIZATION is non-zero,
289 it was of the form template <>. */
292 note_template_header (specialization)
295 processing_specialization = specialization;
296 template_header_count++;
300 /* Returns non-zero iff a declarator, in which the number of template
301 types that appeared was TEMPLATE_COUNT, is an explicit
305 processing_explicit_specialization (template_count)
308 /* A function declaration is an explicit specialization of a member
309 template if all of the following conditions hold:
311 o There was a template <...> preceeding the declaration.
312 o The last template <...> was in fact template <>.
313 o The number of template <...>'s preceeding the declaration, less
314 the number of template classes with arguments specified used to
315 qualify the function name, is 1.
319 template <> void S<int>::foo();
320 template <class T> template <> void S<T>::foo();
321 template <> struct S<int> { ... template <> void foo(); }
323 The first of these is not a specialization of S<int>::foo() (it
324 is instead a specialization of S<T>::foo), while the next two are
325 specializations of member template functions. */
327 return processing_specialization
328 && template_header_count > template_count;
331 /* Returns the template function specialized by TEMPLATE_ID, or
332 NULL_TREE if there is none.
334 The TEMPLATE_ID is a TEMPLATE_ID_EXPR. The TYPE is
335 the type it has been declared to have. Return the TEMPLATE_DECL
336 that is being specialized, and put the specialization arguments in
337 *TARGS. If no appropriate specialization can be found, NULL_TREE is
338 returned, and *TARGS is assigned NULL_TREE. If complain is
339 non-zero, error messages are printed where appropriate. */
342 determine_explicit_specialization (template_id, type, targs_out,
343 need_member_template,
348 int need_member_template;
354 tree matching_fns = NULL_TREE;
358 my_friendly_assert (TREE_CODE (template_id) == TEMPLATE_ID_EXPR
359 && TREE_OPERAND (template_id, 0), 0);
361 fns = TREE_OPERAND (template_id, 0);
363 if (is_overloaded_fn (fns))
364 fn = get_first_fn (fns);
368 overloaded = really_overloaded_fn (fns);
370 for (; fn != NULL_TREE;
371 fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE)
376 if (TREE_CODE (fn) != TEMPLATE_DECL
377 || (need_member_template && !is_member_template (fn)))
380 if (list_length (TREE_OPERAND (template_id, 1)) > DECL_NTPARMS (fn))
383 targs = make_scratch_vec (DECL_NTPARMS (fn));
385 /* We allow incomplete unification here, because we are going to
386 check all the functions. */
387 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
388 &TREE_VEC_ELT (targs, 0),
390 ? TYPE_ARG_TYPES (TREE_TYPE (fn)) : NULL_TREE,
391 type ? TYPE_ARG_TYPES (type) : NULL_TREE,
392 TREE_OPERAND (template_id, 1),
397 /* Unification was successful. See if the return types
399 if (type != NULL_TREE)
401 tree tmpl_return_type = tsubst (TREE_TYPE (TREE_TYPE (fn)),
406 if (tmpl_return_type != TREE_TYPE (type))
408 /* Always complain about this. With ambiguity, some
409 other context, might resolve things. But, a
410 non-matching return type will always be a
412 cp_error ("Return type of explicit specialization of");
413 cp_error ("`%D' is `%T', but should be `%T'.",
414 fn, TREE_TYPE (type), tmpl_return_type);
415 *targs_out = NULL_TREE;
420 matching_fns = scratch_tree_cons (fn, targs, matching_fns);
424 if (matching_fns == NULL_TREE)
427 cp_error ("`%D' does not match any template declaration.",
430 *targs_out = NULL_TREE;
434 if (TREE_CHAIN (matching_fns) != NULL_TREE)
440 cp_error ("Ambiguous explicit specialization. Candidates are:");
441 for (fn = matching_fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
442 cp_error (" %D", TREE_PURPOSE (fn));
445 *targs_out = NULL_TREE;
449 /* We have one, and exactly one, match. */
450 *targs_out = TREE_VALUE (matching_fns);
451 return TREE_PURPOSE (matching_fns);
455 /* Check to see if the function just declared, as indicated in
456 DECLARATOR, and in DECL, is a specialization. Check that the
457 specialization is OK. If FLAGS == 1, we are being called by
458 finish_struct_methods. If FLAGS == 2, we are being called by
459 grokfndecl, and the function has a definition, or is a friend. If
460 FLAGS == 3, this is a friend declaration.
461 Returns 0 if the decl is not an explicit specialization or
462 instantiation, 1 if it is an explicit specialization, and 2 if it
463 is an explicit instantiation. */
466 check_explicit_specialization (declarator, decl, template_count, flags)
472 int finish_member = flags == 1;
473 int have_def = flags == 2;
474 int is_friend = flags == 3;
476 if (processing_explicit_specialization (template_count)
478 || TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
480 tree tmpl = NULL_TREE;
481 tree dname = DECL_NAME (decl);
482 tree ctype = DECL_CLASS_CONTEXT (decl);
485 /* We've come across a declarator that looks like: U f<T1,
486 T2, ...>(A1, A2, ..). This is an explicit template
487 specialization. Check that:
489 o The explicitly specified parameters together with those
490 that can be deduced by template argument deduction
491 uniquely determine a particular specialization.
493 See [temp.expl.spec]. */
496 && TREE_CODE (declarator) == TEMPLATE_ID_EXPR
497 && !processing_explicit_specialization (template_count)
500 if (! have_def && ! template_header_count && ! ctype)
501 /* This is not an explict specialization. It must be
502 an explicit instantiation. */
504 else if (template_header_count > template_count
505 && !processing_specialization)
507 cp_error ("template-id `%D' in declaration of primary template",
511 else if (pedantic || uses_template_parms (decl))
512 pedwarn ("explicit specialization not preceded by `template <>'");
515 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
519 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
522 fns = IDENTIFIER_GLOBAL_VALUE (dname);
526 declarator = lookup_template_function (fns, NULL_TREE);
529 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
531 /* A friend declaration. We can't do much, because we don't
532 know what this resolves to, yet. */
533 my_friendly_assert (is_friend != 0, 0);
534 SET_DECL_IMPLICIT_INSTANTIATION (decl);
539 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE)
543 if (TYPE_BEING_DEFINED (ctype) && !finish_member)
545 /* Since finish_struct_1 has not been called yet, we
546 can't call lookup_fnfields. We note that this
547 template is a specialization, and proceed, letting
548 finish_struct_methods fix this up later. */
549 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
550 DECL_TEMPLATE_INFO (decl)
551 = perm_tree_cons (NULL_TREE,
552 TREE_OPERAND (declarator, 1),
557 fns = lookup_fnfields (TYPE_BINFO (ctype),
558 TREE_OPERAND (declarator, 0),
561 if (fns == NULL_TREE)
563 cp_error ("No member template `%s' declared in `%T'",
564 IDENTIFIER_POINTER (TREE_OPERAND (declarator,
570 TREE_OPERAND (declarator, 0) = fns;
574 determine_explicit_specialization
575 (declarator, TREE_TYPE (decl), &targs,
576 TREE_CODE (decl) == TEMPLATE_DECL, 1);
580 /* Mangle the function name appropriately. */
581 if (name_mangling_version >= 1)
583 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
586 && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE)
588 hash_tree_chain (build_pointer_type (ctype),
591 DECL_ASSEMBLER_NAME (decl)
592 = build_template_decl_overload
595 TREE_TYPE (TREE_TYPE (tmpl)),
596 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
597 targs, ctype != NULL_TREE);
600 if (is_friend && !have_def)
602 /* This is not really a declaration of a specialization.
603 It's just the name of an instantiation. But, it's not
604 a request for an instantiation, either. */
605 SET_DECL_IMPLICIT_INSTANTIATION (decl);
606 DECL_TEMPLATE_INFO (decl)
607 = perm_tree_cons (tmpl, targs, NULL_TREE);
611 /* This function declaration is a template specialization.
613 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
614 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
615 = perm_tree_cons (targs, decl,
616 DECL_TEMPLATE_SPECIALIZATIONS
618 /* If DECL_TI_TEMPLATE (decl), the decl is an
619 instantiation of a specialization of a member template.
620 (In other words, there was a member template, in a
621 class template. That member template was specialized.
622 We then instantiated the class, so there is now an
623 instance of that specialization.)
625 According to the CD2,
627 14.7.3.13 [tmpl.expl.spec]
629 A specialization of a member function template or
630 member class template of a non-specialized class
631 template is itself a template.
633 So, we just leave the template info alone in this case. */
634 if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
635 DECL_TEMPLATE_INFO (decl)
636 = perm_tree_cons (tmpl, targs, NULL_TREE);
644 /* Process information from new template parameter NEXT and append it to the
648 process_template_parm (list, next)
656 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
657 defval = TREE_PURPOSE (parm);
658 parm = TREE_VALUE (parm);
659 is_type = TREE_PURPOSE (parm) == class_type_node;
663 tree p = TREE_VALUE (tree_last (list));
665 if (TREE_CODE (p) == TYPE_DECL)
666 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
668 idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
677 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
678 /* is a const-param */
679 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
681 /* A template parameter is not modifiable. */
682 TREE_READONLY (parm) = 1;
683 if (IS_AGGR_TYPE (TREE_TYPE (parm))
684 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
686 cp_error ("`%#T' is not a valid type for a template constant parameter",
688 if (DECL_NAME (parm) == NULL_TREE)
689 error (" a template type parameter must begin with `class' or `typename'");
690 TREE_TYPE (parm) = void_type_node;
693 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
694 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
695 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
697 tinfo = make_node (TEMPLATE_CONST_PARM);
698 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
699 if (TREE_PERMANENT (parm) == 0)
701 parm = copy_node (parm);
702 TREE_PERMANENT (parm) = 1;
704 TREE_TYPE (tinfo) = TREE_TYPE (parm);
705 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
706 DECL_INITIAL (decl) = tinfo;
707 DECL_INITIAL (parm) = tinfo;
708 TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl);
712 tree t = make_lang_type (TEMPLATE_TYPE_PARM);
713 CLASSTYPE_GOT_SEMICOLON (t) = 1;
714 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
715 TYPE_NAME (t) = decl;
716 TYPE_STUB_DECL (t) = decl;
718 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
720 SET_DECL_ARTIFICIAL (decl);
722 parm = build_tree_list (defval, parm);
723 return chainon (list, parm);
726 /* The end of a template parameter list has been reached. Process the
727 tree list into a parameter vector, converting each parameter into a more
728 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
732 end_template_parm_list (parms)
737 tree saved_parmlist = make_tree_vec (list_length (parms));
739 current_template_parms
740 = tree_cons (build_int_2 (0, processing_template_decl),
741 saved_parmlist, current_template_parms);
743 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
744 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
746 return saved_parmlist;
749 /* end_template_decl is called after a template declaration is seen. */
754 reset_specialization ();
756 if (! processing_template_decl)
759 /* This matches the pushlevel in begin_template_parm_list. */
762 --processing_template_decl;
763 current_template_parms = TREE_CHAIN (current_template_parms);
764 (void) get_pending_sizes (); /* Why? */
767 /* Generate a valid set of template args from current_template_parms. */
770 current_template_args ()
772 tree header = current_template_parms;
773 int length = list_length (header);
774 tree args = make_tree_vec (length);
779 tree a = copy_node (TREE_VALUE (header));
780 int i = TREE_VEC_LENGTH (a);
781 TREE_TYPE (a) = NULL_TREE;
784 tree t = TREE_VEC_ELT (a, i);
786 /* t will be a list if we are called from within a
787 begin/end_template_parm_list pair, but a vector directly
788 if within a begin/end_member_template_processing pair. */
789 if (TREE_CODE (t) == TREE_LIST)
793 if (TREE_CODE (t) == TYPE_DECL)
796 t = DECL_INITIAL (t);
799 TREE_VEC_ELT (a, i) = t;
801 TREE_VEC_ELT (args, --l) = a;
802 header = TREE_CHAIN (header);
809 push_template_decl (decl)
813 tree args = NULL_TREE;
815 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
819 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
820 && DECL_CLASS_CONTEXT (decl))
822 /* Note that this template is a "primary template" */
823 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
824 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
827 /* Partial specialization. */
828 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
829 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
831 tree type = TREE_TYPE (decl);
832 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
833 tree mainargs = CLASSTYPE_TI_ARGS (type);
834 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
836 for (; spec; spec = TREE_CHAIN (spec))
838 /* purpose: args to main template
839 value: spec template */
840 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
844 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
845 = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms),
846 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
847 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
851 args = current_template_args ();
853 if (! ctx || TYPE_BEING_DEFINED (ctx))
855 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
856 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
857 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
858 if (DECL_LANG_SPECIFIC (decl))
860 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
861 DECL_STATIC_FUNCTION_P (tmpl) =
862 DECL_STATIC_FUNCTION_P (decl);
864 if (DECL_TEMPLATE_SPECIALIZATION (decl))
866 /* A specialization of a member template of a template
868 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
869 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
870 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
879 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
880 cp_error ("must specialize `%#T' before defining member `%#D'",
882 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
883 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
884 else if (! DECL_TEMPLATE_INFO (decl))
886 cp_error ("template definition of non-template `%#D'", decl);
890 tmpl = DECL_TI_TEMPLATE (decl);
892 if (is_member_template (tmpl))
894 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
895 t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
896 if (TREE_VEC_LENGTH (t)
897 != TREE_VEC_LENGTH (a))
899 cp_error ("got %d template parameters for `%#D'",
900 TREE_VEC_LENGTH (a), decl);
901 cp_error (" but %d required", TREE_VEC_LENGTH (t));
903 if (TREE_VEC_LENGTH (args) > 1)
904 /* Get the template parameters for the enclosing template
906 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2);
911 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
915 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
917 /* When processing an inline member template of a
918 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
919 if (CLASSTYPE_TI_SPEC_INFO (ctx))
920 t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
922 else if (CLASSTYPE_TEMPLATE_INFO (ctx))
923 t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));
925 /* There should be template arguments if and only if there is a
927 my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0);
930 && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
932 cp_error ("got %d template parameters for `%#D'",
933 TREE_VEC_LENGTH (a), decl);
934 cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
937 /* Get the innermost set of template arguments. */
938 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
940 DECL_TEMPLATE_RESULT (tmpl) = decl;
941 TREE_TYPE (tmpl) = TREE_TYPE (decl);
944 tmpl = pushdecl_top_level (tmpl);
947 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
949 info = perm_tree_cons (tmpl, args, NULL_TREE);
951 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
953 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
954 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
956 else if (! DECL_LANG_SPECIFIC (decl))
957 cp_error ("template declaration of `%#D'", decl);
959 DECL_TEMPLATE_INFO (decl) = info;
962 /* Convert all template arguments to their appropriate types, and return
963 a vector containing the resulting values. If any error occurs, return
967 coerce_template_parms (parms, arglist, in_decl)
971 int nparms, nargs, i, lost = 0;
974 if (arglist == NULL_TREE)
976 else if (TREE_CODE (arglist) == TREE_VEC)
977 nargs = TREE_VEC_LENGTH (arglist);
979 nargs = list_length (arglist);
981 nparms = TREE_VEC_LENGTH (parms);
985 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
987 error ("incorrect number of parameters (%d, should be %d)",
990 cp_error_at ("in template expansion for decl `%D'", in_decl);
991 return error_mark_node;
994 if (arglist && TREE_CODE (arglist) == TREE_VEC)
995 vec = copy_node (arglist);
998 vec = make_tree_vec (nparms);
999 for (i = 0; i < nparms; i++)
1006 arglist = TREE_CHAIN (arglist);
1008 if (arg == error_mark_node)
1011 arg = TREE_VALUE (arg);
1013 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i)))
1015 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1018 arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1021 TREE_VEC_ELT (vec, i) = arg;
1024 for (i = 0; i < nparms; i++)
1026 tree arg = TREE_VEC_ELT (vec, i);
1027 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1029 int is_type, requires_type;
1031 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
1032 requires_type = TREE_CODE (parm) == TYPE_DECL;
1034 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
1035 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
1037 cp_pedwarn ("to refer to a type member of a template parameter,");
1038 cp_pedwarn (" use `typename %E'", arg);
1039 arg = make_typename_type (TREE_OPERAND (arg, 0),
1040 TREE_OPERAND (arg, 1));
1043 if (is_type != requires_type)
1047 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1050 cp_error (" expected a constant of type `%T', got `%T'",
1051 TREE_TYPE (parm), arg);
1053 cp_error (" expected a type, got `%E'", arg);
1056 TREE_VEC_ELT (vec, i) = error_mark_node;
1061 val = groktypename (arg);
1062 if (! processing_template_decl)
1064 tree t = target_type (val);
1065 if (TREE_CODE (t) != TYPENAME_TYPE
1067 && decl_function_context (TYPE_MAIN_DECL (t)))
1069 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
1070 return error_mark_node;
1076 tree t = tsubst (TREE_TYPE (parm), vec,
1077 TREE_VEC_LENGTH (vec), in_decl);
1078 if (processing_template_decl)
1081 val = digest_init (t, arg, (tree *) 0);
1083 if (val == error_mark_node || processing_template_decl)
1086 /* 14.2: Other template-arguments must be constant-expressions,
1087 addresses of objects or functions with external linkage, or of
1088 static class members. */
1089 else if (IS_AGGR_TYPE (TREE_TYPE (val)))
1091 cp_error ("object `%E' cannot be used as template argument", arg);
1092 val = error_mark_node;
1094 else if (!TREE_CONSTANT (val))
1096 cp_error ("non-constant `%E' cannot be used as template argument",
1098 val = error_mark_node;
1100 else if (POINTER_TYPE_P (TREE_TYPE (val))
1101 && ! integer_zerop (val)
1102 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
1103 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
1107 if (TREE_CODE (t) == ADDR_EXPR)
1109 tree a = TREE_OPERAND (t, 0);
1111 if (TREE_CODE (a) == STRING_CST)
1113 cp_error ("string literal %E is not a valid template argument", a);
1114 error ("because it is the address of an object with static linkage");
1115 val = error_mark_node;
1117 else if (TREE_CODE (a) != VAR_DECL
1118 && TREE_CODE (a) != FUNCTION_DECL)
1120 else if (! TREE_PUBLIC (a))
1122 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
1123 val = error_mark_node;
1129 cp_error ("`%E' is not a valid template argument", t);
1130 error ("it must be %s%s with external linkage",
1131 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
1132 ? "a pointer to " : "",
1133 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
1134 ? "a function" : "an object");
1135 val = error_mark_node;
1140 if (val == error_mark_node)
1143 TREE_VEC_ELT (vec, i) = val;
1146 return error_mark_node;
1151 comp_template_args (oldargs, newargs)
1152 tree oldargs, newargs;
1156 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
1159 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
1161 tree nt = TREE_VEC_ELT (newargs, i);
1162 tree ot = TREE_VEC_ELT (oldargs, i);
1166 if (TREE_CODE (nt) != TREE_CODE (ot))
1168 if (TREE_CODE (nt) == TREE_VEC)
1170 /* For member templates */
1171 if (comp_template_args (nt, ot))
1174 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
1176 if (comptypes (ot, nt, 1))
1179 else if (cp_tree_equal (ot, nt) > 0)
1186 /* Given class template name and parameter list, produce a user-friendly name
1187 for the instantiation. */
1190 mangle_class_name_for_template (name, parms, arglist)
1192 tree parms, arglist;
1194 static struct obstack scratch_obstack;
1195 static char *scratch_firstobj;
1198 if (!scratch_firstobj)
1199 gcc_obstack_init (&scratch_obstack);
1201 obstack_free (&scratch_obstack, scratch_firstobj);
1202 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
1205 #define buflen sizeof(buf)
1206 #define check if (bufp >= buf+buflen-1) goto too_long
1207 #define ccat(c) *bufp++=(c); check
1208 #define advance bufp+=strlen(bufp); check
1209 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1212 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1214 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1219 nparms = TREE_VEC_LENGTH (parms);
1220 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
1221 for (i = 0; i < nparms; i++)
1223 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1224 tree arg = TREE_VEC_ELT (arglist, i);
1229 if (TREE_CODE (parm) == TYPE_DECL)
1231 cat (type_as_string (arg, 0));
1235 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
1237 if (TREE_CODE (arg) == TREE_LIST)
1239 /* New list cell was built because old chain link was in
1241 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
1242 arg = TREE_VALUE (arg);
1244 /* No need to check arglist against parmlist here; we did that
1245 in coerce_template_parms, called from lookup_template_class. */
1246 cat (expr_as_string (arg, 0));
1249 char *bufp = obstack_next_free (&scratch_obstack);
1251 while (bufp[offset - 1] == ' ')
1253 obstack_blank_fast (&scratch_obstack, offset);
1255 /* B<C<char> >, not B<C<char>> */
1256 if (bufp[offset - 1] == '>')
1261 return (char *) obstack_base (&scratch_obstack);
1266 fatal ("out of (preallocated) string space creating template instantiation name");
1272 classtype_mangled_name (t)
1275 if (CLASSTYPE_TEMPLATE_INFO (t)
1276 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
1278 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
1279 char *mangled_name = mangle_class_name_for_template
1280 (IDENTIFIER_POINTER (name),
1281 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
1282 CLASSTYPE_TI_ARGS (t));
1283 tree id = get_identifier (mangled_name);
1284 IDENTIFIER_TEMPLATE (id) = name;
1288 return TYPE_IDENTIFIER (t);
1292 add_pending_template (d)
1297 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
1298 ti = CLASSTYPE_TEMPLATE_INFO (d);
1300 ti = DECL_TEMPLATE_INFO (d);
1302 if (TI_PENDING_TEMPLATE_FLAG (ti))
1305 *template_tail = perm_tree_cons
1306 (current_function_decl, d, NULL_TREE);
1307 template_tail = &TREE_CHAIN (*template_tail);
1308 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
1312 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1313 may be either a _DECL or an overloaded function or an
1314 IDENTIFIER_NODE), and ARGLIST. */
1317 lookup_template_function (fns, arglist)
1320 if (fns == NULL_TREE)
1322 cp_error ("non-template used as template");
1323 return error_mark_node;
1326 if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
1328 push_obstacks (&permanent_obstack, &permanent_obstack);
1329 arglist = copy_list (arglist);
1333 return build_min (TEMPLATE_ID_EXPR,
1335 ? TREE_TYPE (fns) : unknown_type_node,
1340 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1341 parameters, find the desired type.
1343 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1344 Since ARGLIST is build on the decl_obstack, we must copy it here
1345 to keep it from being reclaimed when the decl storage is reclaimed.
1347 IN_DECL, if non-NULL, is the template declaration we are trying to
1351 lookup_template_class (d1, arglist, in_decl)
1355 tree template, parmlist;
1359 if (TREE_CODE (d1) == IDENTIFIER_NODE)
1361 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
1363 template = IDENTIFIER_CLASS_VALUE (d1);
1365 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
1367 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
1368 d1 = DECL_NAME (template);
1370 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
1372 template = CLASSTYPE_TI_TEMPLATE (d1);
1373 d1 = DECL_NAME (template);
1376 my_friendly_abort (272);
1378 /* With something like `template <class T> class X class X { ... };'
1379 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1380 We don't want to do that, but we have to deal with the situation, so
1381 let's give them some syntax errors to chew on instead of a crash. */
1383 return error_mark_node;
1384 if (TREE_CODE (template) != TEMPLATE_DECL)
1386 cp_error ("non-template type `%T' used as a template", d1);
1388 cp_error_at ("for template declaration `%D'", in_decl);
1389 return error_mark_node;
1392 if (PRIMARY_TEMPLATE_P (template))
1394 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
1396 arglist = coerce_template_parms (parmlist, arglist, template);
1397 if (arglist == error_mark_node)
1398 return error_mark_node;
1399 if (uses_template_parms (arglist))
1402 if (comp_template_args
1403 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
1404 found = TREE_TYPE (template);
1407 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
1408 found; found = TREE_CHAIN (found))
1410 if (TI_USES_TEMPLATE_PARMS (found)
1411 && comp_template_args (TREE_PURPOSE (found), arglist))
1415 found = TREE_VALUE (found);
1420 if (can_free (&permanent_obstack, arglist))
1421 obstack_free (&permanent_obstack, arglist);
1426 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
1428 id = get_identifier (mangled_name);
1429 IDENTIFIER_TEMPLATE (id) = d1;
1431 maybe_push_to_top_level (uses_template_parms (arglist));
1432 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
1433 pop_from_top_level ();
1437 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1440 arglist = CLASSTYPE_TI_ARGS (ctx);
1442 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
1444 int save_temp = processing_template_decl;
1445 processing_template_decl = 0;
1446 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
1447 processing_template_decl = save_temp;
1451 t = lookup_nested_type_by_name (ctx, id);
1452 my_friendly_assert (t != NULL_TREE, 42);
1456 /* Seems to be wanted. */
1457 CLASSTYPE_GOT_SEMICOLON (t) = 1;
1459 if (! CLASSTYPE_TEMPLATE_INFO (t))
1461 arglist = copy_to_permanent (arglist);
1462 CLASSTYPE_TEMPLATE_INFO (t)
1463 = perm_tree_cons (template, arglist, NULL_TREE);
1464 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1465 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
1466 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1467 = uses_template_parms (arglist);
1469 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
1471 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1472 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
1473 /* if (! uses_template_parms (arglist)) */
1474 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
1475 = get_identifier (build_overload_name (t, 1, 1));
1477 if (flag_external_templates && ! uses_template_parms (arglist)
1478 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1479 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1480 add_pending_template (t);
1486 /* Should be defined in parse.h. */
1490 uses_template_parms (t)
1495 switch (TREE_CODE (t))
1499 /* We assume that the object must be instantiated in order to build
1500 the COMPONENT_REF, so we test only whether the type of the
1501 COMPONENT_REF uses template parms. */
1502 return uses_template_parms (TREE_TYPE (t));
1504 case IDENTIFIER_NODE:
1505 if (!IDENTIFIER_TEMPLATE (t))
1507 my_friendly_abort (42);
1509 /* aggregates of tree nodes */
1512 int i = TREE_VEC_LENGTH (t);
1514 if (uses_template_parms (TREE_VEC_ELT (t, i)))
1519 if (uses_template_parms (TREE_PURPOSE (t))
1520 || uses_template_parms (TREE_VALUE (t)))
1522 return uses_template_parms (TREE_CHAIN (t));
1524 /* constructed type nodes */
1526 case REFERENCE_TYPE:
1527 return uses_template_parms (TREE_TYPE (t));
1529 if (TYPE_PTRMEMFUNC_FLAG (t))
1530 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
1532 if (! CLASSTYPE_TEMPLATE_INFO (t))
1534 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
1536 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1538 return uses_template_parms (TREE_TYPE (t));
1540 if (uses_template_parms (TYPE_DOMAIN (t)))
1542 return uses_template_parms (TREE_TYPE (t));
1544 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
1546 return uses_template_parms (TREE_TYPE (t));
1548 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
1550 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1552 return uses_template_parms (TREE_TYPE (t));
1556 return uses_template_parms (TREE_TYPE (t));
1560 /* ??? What about FIELD_DECLs? */
1561 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
1562 && uses_template_parms (DECL_TI_ARGS (t)))
1567 if (uses_template_parms (TREE_TYPE (t)))
1569 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
1574 return uses_template_parms (TREE_TYPE (t));
1576 return uses_template_parms (TREE_OPERAND (t, 0));
1578 /* template parm nodes */
1579 case TEMPLATE_TYPE_PARM:
1580 case TEMPLATE_CONST_PARM:
1583 /* simple type nodes */
1585 if (uses_template_parms (TYPE_MIN_VALUE (t)))
1587 return uses_template_parms (TYPE_MAX_VALUE (t));
1599 for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
1600 if (uses_template_parms (TREE_VALUE (v)))
1612 /* Non-error_mark_node ERROR_MARKs are bad things. */
1613 my_friendly_assert (t == error_mark_node, 274);
1622 return uses_template_parms (TREE_OPERAND (t, 0));
1625 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1626 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1627 return uses_template_parms (TREE_OPERAND (t, 1));
1631 case REINTERPRET_CAST_EXPR:
1632 case CONST_CAST_EXPR:
1633 case STATIC_CAST_EXPR:
1634 case DYNAMIC_CAST_EXPR:
1642 switch (TREE_CODE_CLASS (TREE_CODE (t)))
1650 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
1651 if (uses_template_parms (TREE_OPERAND (t, i)))
1658 sorry ("testing %s for template parms",
1659 tree_code_name [(int) TREE_CODE (t)]);
1660 my_friendly_abort (82);
1666 static struct tinst_level *current_tinst_level = 0;
1667 static struct tinst_level *free_tinst_level = 0;
1668 static int tinst_depth = 0;
1669 extern int max_tinst_depth;
1670 #ifdef GATHER_STATISTICS
1671 int depth_reached = 0;
1675 push_tinst_level (d)
1678 struct tinst_level *new;
1680 if (tinst_depth >= max_tinst_depth)
1682 struct tinst_level *p = current_tinst_level;
1684 char *file = input_filename;
1686 error ("template instantiation depth exceeds maximum of %d",
1688 error (" (use -ftemplate-depth-NN to increase the maximum)");
1689 cp_error (" instantiating `%D'", d);
1691 for (; p; p = p->next)
1693 cp_error (" instantiated from `%D'", p->decl);
1695 input_filename = p->file;
1697 error (" instantiated from here");
1700 input_filename = file;
1705 if (free_tinst_level)
1707 new = free_tinst_level;
1708 free_tinst_level = new->next;
1711 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1715 new->file = input_filename;
1716 new->next = current_tinst_level;
1717 current_tinst_level = new;
1720 #ifdef GATHER_STATISTICS
1721 if (tinst_depth > depth_reached)
1722 depth_reached = tinst_depth;
1731 struct tinst_level *old = current_tinst_level;
1733 current_tinst_level = old->next;
1734 old->next = free_tinst_level;
1735 free_tinst_level = old;
1739 struct tinst_level *
1742 struct tinst_level *p = current_tinst_level;
1745 for (; p->next ; p = p->next )
1751 instantiate_class_template (type)
1754 tree template, template_info, args, pattern, t, *field_chain;
1756 if (type == error_mark_node)
1757 return error_mark_node;
1759 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1761 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1764 template = TI_TEMPLATE (template_info);
1765 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1766 args = TI_ARGS (template_info);
1768 t = most_specialized_class
1769 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1771 if (t == error_mark_node)
1773 char *str = "candidates are:";
1774 cp_error ("ambiguous class template instantiation for `%#T'", type);
1775 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1777 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1779 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1783 TYPE_BEING_DEFINED (type) = 1;
1784 return error_mark_node;
1787 pattern = TREE_TYPE (t);
1789 pattern = TREE_TYPE (template);
1791 if (TYPE_SIZE (pattern) == NULL_TREE)
1795 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1797 TYPE_BEING_DEFINED (type) = 1;
1799 if (! push_tinst_level (type))
1802 maybe_push_to_top_level (uses_template_parms (type));
1803 pushclass (type, 0);
1805 if (flag_external_templates)
1807 if (flag_alt_external_templates)
1809 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1810 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1811 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1812 = ! CLASSTYPE_INTERFACE_ONLY (type)
1813 && CLASSTYPE_INTERFACE_KNOWN (type);
1817 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1818 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1819 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1820 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1821 = ! CLASSTYPE_INTERFACE_ONLY (type)
1822 && CLASSTYPE_INTERFACE_KNOWN (type);
1827 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1828 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
1831 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1832 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1833 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1834 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1835 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1836 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1837 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1838 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1839 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1840 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1841 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1842 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1843 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1844 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1845 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1846 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1847 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1848 TYPE_USES_COMPLEX_INHERITANCE (type)
1849 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1850 TYPE_USES_MULTIPLE_INHERITANCE (type)
1851 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1852 TYPE_USES_VIRTUAL_BASECLASSES (type)
1853 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1854 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1855 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1858 tree binfo = TYPE_BINFO (type);
1859 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1865 int len = TREE_VEC_LENGTH (pbases);
1866 bases = make_tree_vec (len);
1867 for (i = 0; i < len; ++i)
1871 TREE_VEC_ELT (bases, i) = elt
1872 = tsubst (TREE_VEC_ELT (pbases, i), args,
1873 TREE_VEC_LENGTH (args), NULL_TREE);
1874 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1876 if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
1878 ("base type `%T' of `%T' fails to be a struct or class type",
1879 TREE_TYPE (elt), type);
1880 else if (! uses_template_parms (type)
1881 && (TYPE_SIZE (complete_type (TREE_TYPE (elt)))
1883 cp_error ("base class `%T' of `%T' has incomplete type",
1884 TREE_TYPE (elt), type);
1886 /* Don't initialize this until the vector is filled out, or
1887 lookups will crash. */
1888 BINFO_BASETYPES (binfo) = bases;
1892 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1894 field_chain = &TYPE_FIELDS (type);
1896 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1898 tree name = TREE_PURPOSE (t);
1899 tree tag = TREE_VALUE (t);
1901 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1902 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1904 tree e, newtag = tsubst_enum (tag, args,
1905 TREE_VEC_LENGTH (args), field_chain);
1907 while (*field_chain)
1909 DECL_FIELD_CONTEXT (*field_chain) = type;
1910 field_chain = &TREE_CHAIN (*field_chain);
1915 TREE_VEC_LENGTH (args), NULL_TREE);
1918 /* Don't replace enum constants here. */
1919 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1920 if (TREE_CODE (t) != CONST_DECL)
1922 tree r = tsubst (t, args,
1923 TREE_VEC_LENGTH (args), NULL_TREE);
1924 if (TREE_CODE (r) == VAR_DECL)
1926 if (! uses_template_parms (r))
1927 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1928 /* Perhaps I should do more of grokfield here. */
1930 DECL_IN_AGGR_P (r) = 1;
1931 DECL_EXTERNAL (r) = 1;
1932 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1936 field_chain = &TREE_CHAIN (r);
1939 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1940 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
1942 if (DECL_CONSTRUCTOR_P (t))
1943 grok_ctor_properties (type, t);
1944 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
1945 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
1948 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1949 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1950 args, TREE_VEC_LENGTH (args), NULL_TREE);
1953 tree d = CLASSTYPE_FRIEND_CLASSES (type)
1954 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args,
1955 TREE_VEC_LENGTH (args), NULL_TREE);
1957 /* This does injection for friend classes. */
1958 for (; d; d = TREE_CHAIN (d))
1959 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1961 d = tsubst (DECL_TEMPLATE_INJECT (template), args,
1962 TREE_VEC_LENGTH (args), NULL_TREE);
1964 for (; d; d = TREE_CHAIN (d))
1966 tree t = TREE_VALUE (d);
1968 if (TREE_CODE (t) == TYPE_DECL)
1969 /* Already injected. */;
1975 if (! uses_template_parms (type))
1978 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1979 if (TREE_CODE (tmp) == FIELD_DECL)
1981 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1982 require_complete_type (tmp);
1985 type = finish_struct_1 (type, 0);
1986 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1988 repo_template_used (type);
1989 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1990 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1994 TYPE_SIZE (type) = integer_zero_node;
1995 CLASSTYPE_METHOD_VEC (type)
1996 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1999 TYPE_BEING_DEFINED (type) = 0;
2002 pop_from_top_level ();
2012 if (t1 == NULL_TREE)
2013 return t2 == NULL_TREE;
2014 if (t2 == NULL_TREE)
2016 /* Don't care if one declares its arg const and the other doesn't -- the
2017 main variant of the arg type is all that matters. */
2018 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
2019 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
2021 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
2025 lookup_nested_type_by_name (ctype, name)
2030 complete_type (ctype);
2032 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
2034 if (name == TREE_PURPOSE (t)
2035 /* this catches typedef enum { foo } bar; */
2036 || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
2037 return TREE_VALUE (t);
2042 /* If arg is a non-type template parameter that does not depend on template
2043 arguments, fold it like we weren't in the body of a template. */
2046 maybe_fold_nontype_arg (arg)
2049 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
2050 && !uses_template_parms (arg))
2052 /* Sometimes, one of the args was an expression involving a
2053 template constant parameter, like N - 1. Now that we've
2054 tsubst'd, we might have something like 2 - 1. This will
2055 confuse lookup_template_class, so we do constant folding
2056 here. We have to unset processing_template_decl, to
2057 fool build_expr_from_tree() into building an actual
2060 int saved_processing_template_decl = processing_template_decl;
2061 processing_template_decl = 0;
2062 arg = fold (build_expr_from_tree (arg));
2063 processing_template_decl = saved_processing_template_decl;
2068 /* Take the tree structure T and replace template parameters used therein
2069 with the argument vector ARGS. NARGS is the number of args; should
2070 be removed. IN_DECL is an associated decl for diagnostics.
2072 tsubst is used for dealing with types, decls and the like; for
2073 expressions, use tsubst_expr or tsubst_copy. */
2076 tsubst (t, args, nargs, in_decl)
2083 if (t == NULL_TREE || t == error_mark_node
2084 || t == integer_type_node
2085 || t == void_type_node
2086 || t == char_type_node)
2089 type = TREE_TYPE (t);
2090 if (type == unknown_type_node)
2091 my_friendly_abort (42);
2092 if (type && TREE_CODE (t) != FUNCTION_DECL
2093 && TREE_CODE (t) != TYPENAME_TYPE)
2094 type = tsubst (type, args, nargs, in_decl);
2096 switch (TREE_CODE (t))
2099 if (TYPE_PTRMEMFUNC_P (t))
2101 tree r = build_ptrmemfunc_type
2102 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
2103 return cp_build_type_variant (r, TYPE_READONLY (t),
2107 /* else fall through */
2109 if (uses_template_parms (t))
2111 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
2112 tree r = lookup_template_class (t, argvec, in_decl);
2113 return cp_build_type_variant (r, TYPE_READONLY (t),
2117 /* else fall through */
2119 case IDENTIFIER_NODE:
2132 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2133 if (ctx == NULL_TREE)
2135 else if (ctx == current_function_decl)
2136 return lookup_name (TYPE_IDENTIFIER (t), 1);
2138 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
2142 if (t == integer_type_node)
2145 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
2146 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
2150 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
2151 max = tsubst_expr (max, args, nargs, in_decl);
2152 if (processing_template_decl)
2154 tree itype = make_node (INTEGER_TYPE);
2155 TYPE_MIN_VALUE (itype) = size_zero_node;
2156 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
2161 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
2162 return build_index_2_type (size_zero_node, max);
2165 case TEMPLATE_TYPE_PARM:
2166 case TEMPLATE_CONST_PARM:
2171 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2173 idx = TEMPLATE_TYPE_IDX (t);
2174 level = TEMPLATE_TYPE_LEVEL (t);
2178 idx = TEMPLATE_CONST_IDX (t);
2179 level = TEMPLATE_CONST_LEVEL (t);
2182 if (TREE_VEC_LENGTH (args) > 0)
2184 tree arg = NULL_TREE;
2186 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2188 if (TREE_VEC_LENGTH (args) >= level - 1)
2190 (TREE_VEC_ELT (args, level - 1), idx);
2192 else if (level == 1)
2193 arg = TREE_VEC_ELT (args, idx);
2195 if (arg != NULL_TREE)
2197 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2198 return cp_build_type_variant
2199 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
2200 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
2206 /* If we get here, we must have been looking at a parm for a
2207 more deeply nested template. */
2208 my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM
2209 && TEMPLATE_CONST_LEVEL (t) > 1)
2210 || (TREE_CODE (t) == TEMPLATE_TYPE_PARM
2211 && TEMPLATE_TYPE_LEVEL (t) > 1),
2218 /* We can get here when processing a member template function
2219 of a template class. */
2221 tree decl = DECL_TEMPLATE_RESULT (t);
2227 /* We might already have an instance of this template. */
2228 tree instances = DECL_TEMPLATE_INSTANTIATIONS (t);
2229 tree ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, in_decl);
2231 for (; instances; instances = TREE_CHAIN (instances))
2232 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances)) == ctx)
2233 return TREE_VALUE (instances);
2235 /* Make a new template decl. It will be similar to the
2236 original, but will record the current template arguments.
2237 We also create a new function declaration, which is just
2238 like the old one, but points to this new template, rather
2239 than the old one. */
2240 tmpl = copy_node (t);
2241 copy_lang_decl (tmpl);
2242 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0);
2243 DECL_CHAIN (tmpl) = NULL_TREE;
2244 TREE_CHAIN (tmpl) = NULL_TREE;
2245 DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
2246 new_decl = tsubst (decl, args, nargs, in_decl);
2247 DECL_RESULT (tmpl) = new_decl;
2248 DECL_TI_TEMPLATE (new_decl) = tmpl;
2249 TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
2250 DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
2251 SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
2253 /* The template parameters for this new template are all the
2254 template parameters for the old template, except the
2255 outermost level of parameters. */
2256 DECL_TEMPLATE_PARMS (tmpl)
2257 = copy_node (DECL_TEMPLATE_PARMS (tmpl));
2258 for (parms = DECL_TEMPLATE_PARMS (tmpl);
2259 TREE_CHAIN (parms) != NULL_TREE;
2260 parms = TREE_CHAIN (parms))
2261 TREE_CHAIN (parms) = copy_node (TREE_CHAIN (parms));
2263 /* Record this partial instantiation. */
2264 DECL_TEMPLATE_INSTANTIATIONS (t)
2265 = perm_tree_cons (NULL_TREE, tmpl,
2266 DECL_TEMPLATE_INSTANTIATIONS (t));
2268 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
2279 if (DECL_CONTEXT (t) != NULL_TREE
2280 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
2282 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
2286 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
2287 type = tsubst (type, args, nargs, in_decl);
2293 type = tsubst (type, args, nargs, in_decl);
2296 /* Do we already have this instantiation? */
2297 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2299 tree tmpl = DECL_TI_TEMPLATE (t);
2300 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2302 for (; decls; decls = TREE_CHAIN (decls))
2303 if (TREE_TYPE (TREE_VALUE (decls)) == type
2304 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx
2305 && comp_template_args (TREE_PURPOSE (decls), args))
2306 return TREE_VALUE (decls);
2309 /* We do NOT check for matching decls pushed separately at this
2310 point, as they may not represent instantiations of this
2311 template, and in any case are considered separate under the
2312 discrete model. Instead, see add_maybe_template. */
2316 TREE_TYPE (r) = type;
2319 = tsubst (DECL_CONTEXT (t), args, nargs, t);
2320 DECL_CLASS_CONTEXT (r) = ctx;
2322 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
2323 IDENTIFIER_POINTER (DECL_NAME (r)),
2324 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
2326 /* Type-conversion operator. Reconstruct the name, in
2327 case it's the name of one of the template's parameters. */
2328 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
2331 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
2333 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
2334 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
2335 buf = (char *) alloca (strlen (dbuf)
2336 + sizeof (DESTRUCTOR_DECL_PREFIX));
2337 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
2340 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
2344 /* Instantiations of template functions must be mangled
2345 specially, in order to conform to 14.5.5.1
2346 [temp.over.link]. We use in_decl below rather than
2347 DECL_TI_TEMPLATE (r) because the latter is set to
2348 NULL_TREE in instantiate_decl. */
2352 if (DECL_TEMPLATE_INFO (r))
2353 tmpl = DECL_TI_TEMPLATE (r);
2357 /* tmpl will be NULL if this is a specialization of a
2358 member template of a template class. */
2359 if (name_mangling_version < 1
2360 || tmpl == NULL_TREE
2361 || (member && !is_member_template (tmpl)
2362 && !DECL_TEMPLATE_INFO (tmpl)))
2364 arg_types = TYPE_ARG_TYPES (type);
2365 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2366 arg_types = hash_tree_chain
2367 (build_pointer_type (DECL_CONTEXT (r)),
2370 DECL_ASSEMBLER_NAME (r)
2371 = build_decl_overload (DECL_NAME (r), arg_types,
2376 /* We pass the outermost template parameters to
2377 build_template_decl_overload since the innermost
2378 template parameters are still just template
2379 parameters; there are no corresponding subsitution
2381 /* FIXME The messed up thing here is that we get here with
2382 full args and only one level of parms. This is necessary
2383 because when we partially instantiate a member template,
2384 even though there's really only one level of parms left
2385 we re-use the parms from the original template, which
2386 have level 2. When this is fixed we can remove the
2387 add_to_template_args from instantiate_template. */
2388 tree tparms = DECL_TEMPLATE_PARMS (tmpl);
2390 while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
2391 tparms = TREE_CHAIN (tparms);
2393 my_friendly_assert (tparms != NULL_TREE
2394 && TREE_CODE (tparms) == TREE_LIST,
2396 tparms = TREE_VALUE (tparms);
2398 arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
2399 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2400 arg_types = hash_tree_chain
2401 (build_pointer_type (DECL_CONTEXT (r)),
2404 DECL_ASSEMBLER_NAME (r)
2405 = build_template_decl_overload
2406 (DECL_NAME (r), arg_types,
2407 TREE_TYPE (TREE_TYPE (tmpl)),
2409 TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC
2410 ? TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1) :
2416 make_decl_rtl (r, NULL_PTR, 1);
2418 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
2419 DECL_MAIN_VARIANT (r) = r;
2420 DECL_RESULT (r) = NULL_TREE;
2421 DECL_INITIAL (r) = NULL_TREE;
2423 TREE_STATIC (r) = 0;
2424 TREE_PUBLIC (r) = 1;
2425 DECL_EXTERNAL (r) = 1;
2426 DECL_INTERFACE_KNOWN (r) = 0;
2427 DECL_DEFER_OUTPUT (r) = 0;
2428 TREE_CHAIN (r) = NULL_TREE;
2429 DECL_CHAIN (r) = NULL_TREE;
2431 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
2432 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
2434 /* Look for matching decls for the moment. */
2435 if (! member && ! flag_ansi_overloading)
2437 tree decls = lookup_name_nonclass (DECL_NAME (t));
2440 if (decls == NULL_TREE)
2442 else if (is_overloaded_fn (decls))
2443 for (decls = get_first_fn (decls); decls;
2444 decls = DECL_CHAIN (decls))
2446 if (TREE_CODE (decls) == FUNCTION_DECL
2447 && TREE_TYPE (decls) == type)
2456 int dcl_only = ! DECL_INITIAL (d);
2458 DECL_INITIAL (r) = error_mark_node;
2459 duplicate_decls (r, d);
2462 DECL_INITIAL (r) = 0;
2466 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2468 tree tmpl = DECL_TI_TEMPLATE (t);
2469 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2470 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2472 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
2473 argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec);
2475 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2476 *declsp = perm_tree_cons (argvec, r, *declsp);
2478 /* If we have a preexisting version of this function, don't expand
2479 the template version, use the other instead. */
2480 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
2481 SET_DECL_TEMPLATE_SPECIALIZATION (r);
2483 SET_DECL_IMPLICIT_INSTANTIATION (r);
2487 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2488 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2490 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
2491 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
2498 tree r = copy_node (t);
2499 TREE_TYPE (r) = type;
2500 DECL_INITIAL (r) = TREE_TYPE (r);
2501 DECL_CONTEXT (r) = NULL_TREE;
2502 #ifdef PROMOTE_PROTOTYPES
2503 if ((TREE_CODE (type) == INTEGER_TYPE
2504 || TREE_CODE (type) == ENUMERAL_TYPE)
2505 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2506 DECL_ARG_TYPE (r) = integer_type_node;
2509 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
2515 tree r = copy_node (t);
2516 TREE_TYPE (r) = type;
2519 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
2521 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
2522 TREE_CHAIN (r) = NULL_TREE;
2528 tree r = copy_node (t);
2530 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
2531 TREE_CHAIN (r) = NULL_TREE;
2538 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
2540 /* Do we already have this instantiation? */
2541 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2543 tree tmpl = DECL_TI_TEMPLATE (t);
2544 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2546 for (; decls; decls = TREE_CHAIN (decls))
2547 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
2548 return TREE_VALUE (decls);
2552 TREE_TYPE (r) = type;
2553 DECL_CONTEXT (r) = ctx;
2554 if (TREE_STATIC (r))
2555 DECL_ASSEMBLER_NAME (r)
2556 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
2558 /* Don't try to expand the initializer until someone tries to use
2559 this variable; otherwise we run into circular dependencies. */
2560 DECL_INITIAL (r) = NULL_TREE;
2565 if (DECL_LANG_SPECIFIC (r))
2568 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
2571 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2573 tree tmpl = DECL_TI_TEMPLATE (t);
2574 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2575 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2577 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2578 *declsp = perm_tree_cons (argvec, r, *declsp);
2579 SET_DECL_IMPLICIT_INSTANTIATION (r);
2581 TREE_CHAIN (r) = NULL_TREE;
2586 if (t == TYPE_NAME (TREE_TYPE (t)))
2587 return TYPE_NAME (type);
2590 tree r = copy_node (t);
2591 TREE_TYPE (r) = type;
2592 DECL_CONTEXT (r) = current_class_type;
2593 TREE_CHAIN (r) = NULL_TREE;
2599 tree purpose, value, chain, result;
2600 int via_public, via_virtual, via_protected;
2602 if (t == void_list_node)
2605 via_public = TREE_VIA_PUBLIC (t);
2606 via_protected = TREE_VIA_PROTECTED (t);
2607 via_virtual = TREE_VIA_VIRTUAL (t);
2609 purpose = TREE_PURPOSE (t);
2611 purpose = tsubst (purpose, args, nargs, in_decl);
2612 value = TREE_VALUE (t);
2614 value = tsubst (value, args, nargs, in_decl);
2615 chain = TREE_CHAIN (t);
2616 if (chain && chain != void_type_node)
2617 chain = tsubst (chain, args, nargs, in_decl);
2618 if (purpose == TREE_PURPOSE (t)
2619 && value == TREE_VALUE (t)
2620 && chain == TREE_CHAIN (t))
2622 result = hash_tree_cons (via_public, via_virtual, via_protected,
2623 purpose, value, chain);
2624 TREE_PARMLIST (result) = TREE_PARMLIST (t);
2628 if (type != NULL_TREE)
2634 if (type == TREE_TYPE (t))
2637 TREE_TYPE (t) = complete_type (type);
2638 if (IS_AGGR_TYPE (type))
2640 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
2641 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
2642 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
2643 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
2648 /* Otherwise, a vector of template arguments. */
2650 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
2651 tree *elts = (tree *) alloca (len * sizeof (tree));
2653 bzero ((char *) elts, len * sizeof (tree));
2655 for (i = 0; i < len; i++)
2657 elts[i] = maybe_fold_nontype_arg
2658 (tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl));
2660 if (elts[i] != TREE_VEC_ELT (t, i))
2667 t = make_tree_vec (len);
2668 for (i = 0; i < len; i++)
2669 TREE_VEC_ELT (t, i) = elts[i];
2674 case REFERENCE_TYPE:
2677 enum tree_code code;
2678 if (type == TREE_TYPE (t))
2681 code = TREE_CODE (t);
2682 if (code == POINTER_TYPE)
2683 r = build_pointer_type (type);
2685 r = build_reference_type (type);
2686 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
2687 /* Will this ever be needed for TYPE_..._TO values? */
2692 return build_offset_type
2693 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
2697 tree values = TYPE_ARG_TYPES (t);
2698 tree context = TYPE_CONTEXT (t);
2699 tree raises = TYPE_RAISES_EXCEPTIONS (t);
2702 /* Don't bother recursing if we know it won't change anything. */
2703 if (values != void_list_node)
2705 /* This should probably be rewritten to use hash_tree_cons for
2706 the memory savings. */
2707 tree first = NULL_TREE;
2710 for (; values && values != void_list_node;
2711 values = TREE_CHAIN (values))
2713 tree value = TYPE_MAIN_VARIANT (type_decays_to
2714 (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
2715 /* Don't instantiate default args unless they are used.
2716 Handle it in build_over_call instead. */
2717 tree purpose = TREE_PURPOSE (values);
2718 tree x = build_tree_list (purpose, value);
2721 TREE_CHAIN (last) = x;
2727 if (values == void_list_node)
2728 TREE_CHAIN (last) = void_list_node;
2733 context = tsubst (context, args, nargs, in_decl);
2734 /* Could also optimize cases where return value and
2735 values have common elements (e.g., T min(const &T, const T&). */
2737 /* If the above parameters haven't changed, just return the type. */
2738 if (type == TREE_TYPE (t)
2739 && values == TYPE_VALUES (t)
2740 && context == TYPE_CONTEXT (t))
2743 /* Construct a new type node and return it. */
2744 if (TREE_CODE (t) == FUNCTION_TYPE
2745 && context == NULL_TREE)
2747 fntype = build_function_type (type, values);
2749 else if (context == NULL_TREE)
2751 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
2752 args, nargs, in_decl);
2753 fntype = build_cplus_method_type (base, type,
2754 TREE_CHAIN (values));
2758 fntype = make_node (TREE_CODE (t));
2759 TREE_TYPE (fntype) = type;
2760 TYPE_CONTEXT (fntype) = context;
2761 TYPE_VALUES (fntype) = values;
2762 TYPE_SIZE (fntype) = TYPE_SIZE (t);
2763 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
2764 TYPE_MODE (fntype) = TYPE_MODE (t);
2765 if (TYPE_METHOD_BASETYPE (t))
2766 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
2767 args, nargs, in_decl);
2768 /* Need to generate hash value. */
2769 my_friendly_abort (84);
2771 fntype = build_type_variant (fntype,
2776 raises = tsubst (raises, args, nargs, in_decl);
2777 fntype = build_exception_variant (fntype, raises);
2783 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
2785 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
2787 r = build_cplus_array_type (type, domain);
2793 return fold (build (TREE_CODE (t), TREE_TYPE (t),
2794 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2795 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
2799 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
2800 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
2804 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2805 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
2806 return cp_build_type_variant
2807 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
2808 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
2812 return make_pointer_declarator
2813 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2816 return make_reference_declarator
2817 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2820 return build_parse_node
2821 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2822 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2825 return make_call_declarator
2826 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2827 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2828 TREE_OPERAND (t, 2),
2829 tsubst (TREE_TYPE (t), args, nargs, in_decl));
2832 return build_parse_node
2833 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2834 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
2837 sorry ("use of `%s' in template",
2838 tree_code_name [(int) TREE_CODE (t)]);
2839 return error_mark_node;
2846 emit_line_note (input_filename, lineno);
2850 expand_start_bindings (0);
2857 int saved_warn_unused;
2859 if (processing_template_decl)
2861 saved_warn_unused = warn_unused;
2864 expand_end_bindings (getdecls (), kept_level_p (), 0);
2865 if (processing_template_decl)
2866 warn_unused = saved_warn_unused;
2867 t = poplevel (kept_level_p (), 1, 0);
2872 /* Like tsubst, but deals with expressions. This function just replaces
2873 template parms; to finish processing the resultant expression, use
2877 tsubst_copy (t, args, nargs, in_decl)
2882 enum tree_code code;
2884 if (t == NULL_TREE || t == error_mark_node)
2887 code = TREE_CODE (t);
2892 return do_identifier (DECL_NAME (t), 0);
2896 if (DECL_CONTEXT (t))
2898 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
2899 if (ctx == current_function_decl)
2900 return lookup_name (DECL_NAME (t), 0);
2901 else if (ctx != DECL_CONTEXT (t))
2902 return lookup_field (ctx, DECL_NAME (t), 0, 0);
2908 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2909 t = tsubst (t, args, nargs, in_decl);
2914 if (is_member_template (t))
2915 return tsubst (t, args, nargs, in_decl);
2920 case IDENTIFIER_NODE:
2921 return do_identifier (t, 0);
2925 case REINTERPRET_CAST_EXPR:
2926 case CONST_CAST_EXPR:
2927 case STATIC_CAST_EXPR:
2928 case DYNAMIC_CAST_EXPR:
2930 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2931 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2934 case PREDECREMENT_EXPR:
2935 case PREINCREMENT_EXPR:
2936 case POSTDECREMENT_EXPR:
2937 case POSTINCREMENT_EXPR:
2939 case TRUTH_NOT_EXPR:
2942 case CONVERT_EXPR: /* Unary + */
2949 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2954 case TRUNC_DIV_EXPR:
2956 case FLOOR_DIV_EXPR:
2957 case ROUND_DIV_EXPR:
2958 case EXACT_DIV_EXPR:
2960 case BIT_ANDTC_EXPR:
2963 case TRUNC_MOD_EXPR:
2964 case FLOOR_MOD_EXPR:
2965 case TRUTH_ANDIF_EXPR:
2966 case TRUTH_ORIF_EXPR:
2967 case TRUTH_AND_EXPR:
2988 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2989 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2993 tree fn = TREE_OPERAND (t, 0);
2994 if (really_overloaded_fn (fn))
2995 fn = tsubst_copy (get_first_fn (fn), args, nargs, in_decl);
2997 fn = tsubst_copy (fn, args, nargs, in_decl);
2999 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3003 case METHOD_CALL_EXPR:
3005 tree name = TREE_OPERAND (t, 0);
3006 if (TREE_CODE (name) == BIT_NOT_EXPR)
3008 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3009 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
3011 else if (TREE_CODE (name) == SCOPE_REF
3012 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
3014 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3015 name = TREE_OPERAND (name, 1);
3016 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3017 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
3018 name = build_nt (SCOPE_REF, base, name);
3021 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
3023 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3024 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
3031 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3032 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3033 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3038 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3039 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3040 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3041 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
3048 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3049 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
3050 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
3051 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
3055 case TEMPLATE_ID_EXPR:
3057 /* Substituted template arguments */
3058 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl);
3060 for (chain = targs; chain; chain = TREE_CHAIN (chain))
3061 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
3063 return lookup_template_function
3064 (tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), targs);
3069 tree purpose, value, chain;
3071 if (t == void_list_node)
3074 purpose = TREE_PURPOSE (t);
3076 purpose = tsubst_copy (purpose, args, nargs, in_decl);
3077 value = TREE_VALUE (t);
3079 value = tsubst_copy (value, args, nargs, in_decl);
3080 chain = TREE_CHAIN (t);
3081 if (chain && chain != void_type_node)
3082 chain = tsubst_copy (chain, args, nargs, in_decl);
3083 if (purpose == TREE_PURPOSE (t)
3084 && value == TREE_VALUE (t)
3085 && chain == TREE_CHAIN (t))
3087 return tree_cons (purpose, value, chain);
3094 case TEMPLATE_TYPE_PARM:
3095 case TEMPLATE_CONST_PARM:
3097 case REFERENCE_TYPE:
3103 return tsubst (t, args, nargs, in_decl);
3105 case IDENTIFIER_NODE:
3106 if (IDENTIFIER_TYPENAME_P (t))
3107 return build_typename_overload
3108 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
3114 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
3115 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
3122 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
3125 tsubst_expr (t, args, nargs, in_decl)
3130 if (t == NULL_TREE || t == error_mark_node)
3133 if (processing_template_decl)
3134 return tsubst_copy (t, args, nargs, in_decl);
3136 switch (TREE_CODE (t))
3139 lineno = TREE_COMPLEXITY (t);
3140 emit_line_note (input_filename, lineno);
3142 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3147 lineno = TREE_COMPLEXITY (t);
3148 emit_line_note (input_filename, lineno);
3149 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3150 /* Do default conversion if safe and possibly important,
3151 in case within ({...}). */
3152 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
3153 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
3154 t = default_conversion (t);
3155 cplus_expand_expr_stmt (t);
3162 int i = suspend_momentary ();
3165 lineno = TREE_COMPLEXITY (t);
3166 emit_line_note (input_filename, lineno);
3168 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
3169 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
3170 TREE_OPERAND (t, 2) != 0);
3171 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3173 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3174 resume_momentary (i);
3181 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
3182 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3183 int cond_scope = (TREE_OPERAND (t, 1)
3184 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
3186 lineno = TREE_COMPLEXITY (t);
3187 emit_line_note (input_filename, lineno);
3190 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
3191 tsubst_expr (tmp, args, nargs, in_decl);
3193 emit_line_note (input_filename, lineno);
3194 expand_start_loop_continue_elsewhere (1);
3198 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3199 emit_line_note (input_filename, lineno);
3201 expand_exit_loop_if_false (0, condition_conversion (tmp));
3205 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
3208 emit_line_note (input_filename, lineno);
3209 expand_loop_continue_here ();
3210 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3212 cplus_expand_expr_stmt (tmp);
3225 lineno = TREE_COMPLEXITY (t);
3227 emit_line_note (input_filename, lineno);
3228 expand_start_loop (1);
3230 cond = TREE_OPERAND (t, 0);
3231 if (TREE_CODE (cond) == DECL_STMT)
3233 cond = tsubst_expr (cond, args, nargs, in_decl);
3234 emit_line_note (input_filename, lineno);
3235 expand_exit_loop_if_false (0, condition_conversion (cond));
3237 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
3239 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3251 lineno = TREE_COMPLEXITY (t);
3253 emit_line_note (input_filename, lineno);
3254 expand_start_loop_continue_elsewhere (1);
3256 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3257 expand_loop_continue_here ();
3259 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3260 emit_line_note (input_filename, lineno);
3261 expand_exit_loop_if_false (0, condition_conversion (cond));
3272 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3274 lineno = TREE_COMPLEXITY (t);
3277 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3278 emit_line_note (input_filename, lineno);
3279 expand_start_cond (condition_conversion (tmp), 0);
3281 if (tmp = TREE_OPERAND (t, 1), tmp)
3282 tsubst_expr (tmp, args, nargs, in_decl);
3284 if (tmp = TREE_OPERAND (t, 2), tmp)
3286 expand_start_else ();
3287 tsubst_expr (tmp, args, nargs, in_decl);
3301 tree substmt = TREE_OPERAND (t, 0);
3303 lineno = TREE_COMPLEXITY (t);
3305 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3308 for (; substmt; substmt = TREE_CHAIN (substmt))
3309 tsubst_expr (substmt, args, nargs, in_decl);
3311 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3317 lineno = TREE_COMPLEXITY (t);
3318 emit_line_note (input_filename, lineno);
3319 if (! expand_exit_something ())
3320 error ("break statement not within loop or switch");
3324 lineno = TREE_COMPLEXITY (t);
3325 emit_line_note (input_filename, lineno);
3326 if (! expand_continue_loop (0))
3327 error ("continue statement not within a loop");
3333 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3335 lineno = TREE_COMPLEXITY (t);
3338 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3339 emit_line_note (input_filename, lineno);
3340 c_expand_start_case (val);
3343 if (tmp = TREE_OPERAND (t, 1), tmp)
3344 tsubst_expr (tmp, args, nargs, in_decl);
3346 expand_end_case (val);
3357 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
3358 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
3362 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
3369 lineno = TREE_COMPLEXITY (t);
3370 emit_line_note (input_filename, lineno);
3371 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
3373 tree decl = lookup_label (TREE_OPERAND (t, 0));
3374 TREE_USED (decl) = 1;
3378 expand_computed_goto
3379 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3383 lineno = TREE_COMPLEXITY (t);
3384 emit_line_note (input_filename, lineno);
3385 expand_start_try_stmts ();
3386 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3387 expand_start_all_catch ();
3389 tree handler = TREE_OPERAND (t, 1);
3390 for (; handler; handler = TREE_CHAIN (handler))
3391 tsubst_expr (handler, args, nargs, in_decl);
3393 expand_end_all_catch ();
3397 lineno = TREE_COMPLEXITY (t);
3399 if (TREE_OPERAND (t, 0))
3401 tree d = TREE_OPERAND (t, 0);
3402 expand_start_catch_block
3403 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
3404 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
3407 expand_start_catch_block (NULL_TREE, NULL_TREE);
3408 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3409 expand_end_catch_block ();
3414 lineno = TREE_COMPLEXITY (t);
3416 if (TREE_CODE (t) == ENUMERAL_TYPE)
3417 tsubst_enum (t, args, nargs, NULL);
3421 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
3427 instantiate_template (tmpl, targ_ptr)
3428 tree tmpl, targ_ptr;
3432 struct obstack *old_fmp_obstack;
3433 extern struct obstack *function_maybepermanent_obstack;
3435 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
3437 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3441 /* Check to see if there is a matching specialization. */
3442 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3444 specs = TREE_CHAIN (specs))
3445 if (comp_template_args (TREE_PURPOSE (specs), targ_ptr))
3446 return TREE_VALUE (specs);
3449 push_obstacks (&permanent_obstack, &permanent_obstack);
3450 old_fmp_obstack = function_maybepermanent_obstack;
3451 function_maybepermanent_obstack = &permanent_obstack;
3453 len = DECL_NTPARMS (tmpl);
3458 tree t = TREE_VEC_ELT (targ_ptr, i);
3459 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3461 tree nt = target_type (t);
3462 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
3464 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
3465 cp_error (" trying to instantiate `%D'", tmpl);
3466 fndecl = error_mark_node;
3470 TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
3472 targ_ptr = copy_to_permanent (targ_ptr);
3474 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
3475 targ_ptr = add_to_template_args (DECL_TI_ARGS (tmpl), targ_ptr);
3477 /* substitute template parameters */
3478 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
3480 if (flag_external_templates)
3481 add_pending_template (fndecl);
3484 function_maybepermanent_obstack = old_fmp_obstack;
3490 /* Push the name of the class template into the scope of the instantiation. */
3493 overload_template_name (type)
3496 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
3499 if (IDENTIFIER_CLASS_VALUE (id)
3500 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
3503 decl = build_decl (TYPE_DECL, id, type);
3504 SET_DECL_ARTIFICIAL (decl);
3505 pushdecl_class_level (decl);
3508 /* Like type_unification but designed specially to handle conversion
3512 fn_type_unification (fn, explicit_targs, targs, args, return_type, strict)
3513 tree fn, explicit_targs, targs, args, return_type;
3517 tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
3518 tree decl_arg_types = args;
3520 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
3522 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn)))
3524 /* This is a template conversion operator. Use the return types
3525 as well as the argument types. */
3526 fn_arg_types = scratch_tree_cons (NULL_TREE,
3527 TREE_TYPE (TREE_TYPE (fn)),
3529 decl_arg_types = scratch_tree_cons (NULL_TREE,
3534 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
3535 &TREE_VEC_ELT (targs, 0),
3545 /* Type unification.
3547 We have a function template signature with one or more references to
3548 template parameters, and a parameter list we wish to fit to this
3549 template. If possible, produce a list of parameters for the template
3550 which will cause it to fit the supplied parameter list.
3552 Return zero for success, 2 for an incomplete match that doesn't resolve
3553 all the types, and 1 for complete failure. An error message will be
3554 printed only for an incomplete match.
3556 TPARMS[NTPARMS] is an array of template parameter types;
3557 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3558 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3559 and ARGS is the argument list we're trying to match against it.
3561 If SUBR is 1, we're being called recursively (to unify the arguments of
3562 a function or method parameter of a function template), so don't zero
3563 out targs and don't fail on an incomplete match.
3565 If STRICT is 1, the match must be exact (for casts of overloaded
3566 addresses, explicit instantiation, and more_specialized). */
3569 type_unification (tparms, targs, parms, args, targs_in, nsubsts,
3570 strict, allow_incomplete)
3571 tree tparms, *targs, parms, args, targs_in;
3572 int *nsubsts, strict, allow_incomplete;
3574 int ntparms = TREE_VEC_LENGTH (tparms);
3579 bzero ((char *) targs, sizeof (tree) * ntparms);
3581 /* Insert any explicit template arguments. They are encoded as the
3582 operands of NOP_EXPRs so that unify can tell that they are
3583 explicit arguments. */
3584 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3585 targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VALUE (t));
3587 r = type_unification_real (tparms, targs, parms, args, nsubsts, 0,
3588 strict, allow_incomplete);
3590 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3591 if (TREE_CODE (targs[i]) == NOP_EXPR)
3592 targs[i] = TREE_OPERAND (targs[i], 0);
3599 type_unification_real (tparms, targs, parms, args, nsubsts, subr,
3600 strict, allow_incomplete)
3601 tree tparms, *targs, parms, args;
3602 int *nsubsts, subr, strict, allow_incomplete;
3606 int ntparms = TREE_VEC_LENGTH (tparms);
3608 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
3609 my_friendly_assert (parms == NULL_TREE
3610 || TREE_CODE (parms) == TREE_LIST, 290);
3611 /* ARGS could be NULL (via a call from parse.y to
3612 build_x_function_call). */
3614 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
3615 my_friendly_assert (ntparms > 0, 292);
3618 && parms != void_list_node
3620 && args != void_list_node)
3622 parm = TREE_VALUE (parms);
3623 parms = TREE_CHAIN (parms);
3624 arg = TREE_VALUE (args);
3625 args = TREE_CHAIN (args);
3627 if (arg == error_mark_node)
3629 if (arg == unknown_type_node)
3632 /* Conversions will be performed on a function argument that
3633 corresponds with a function parameter that contains only
3634 non-deducible template parameters and explicitly specified
3635 template parameters. */
3636 if (! uses_template_parms (parm))
3640 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3641 type = TREE_TYPE (arg);
3650 if (comptypes (parm, type, 1))
3655 if (can_convert_arg (parm, type, arg))
3660 if (can_convert (parm, type))
3668 if (TREE_CODE (arg) == VAR_DECL)
3669 arg = TREE_TYPE (arg);
3670 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
3671 arg = TREE_TYPE (arg);
3673 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3675 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
3676 if (TREE_CODE (arg) == TREE_LIST
3677 && TREE_TYPE (arg) == unknown_type_node
3678 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
3680 int nsubsts, ntparms;
3683 /* Have to back unify here */
3684 arg = TREE_VALUE (arg);
3686 ntparms = DECL_NTPARMS (arg);
3687 targs = (tree *) alloca (sizeof (tree) * ntparms);
3688 parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
3690 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg),
3692 TYPE_ARG_TYPES (TREE_TYPE (arg)),
3693 parm, NULL_TREE, &nsubsts, strict,
3696 arg = TREE_TYPE (arg);
3699 if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
3700 arg = TREE_TYPE (arg);
3702 if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
3704 if (TREE_CODE (arg) == FUNCTION_TYPE
3705 || TREE_CODE (arg) == METHOD_TYPE)
3706 arg = build_pointer_type (arg);
3707 else if (TREE_CODE (arg) == ARRAY_TYPE)
3708 arg = build_pointer_type (TREE_TYPE (arg));
3710 arg = TYPE_MAIN_VARIANT (arg);
3713 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
3721 /* Fail if we've reached the end of the parm list, and more args
3722 are present, and the parm list isn't variadic. */
3723 if (args && args != void_list_node && parms == void_list_node)
3725 /* Fail if parms are left and they don't have default values. */
3727 && parms != void_list_node
3728 && TREE_PURPOSE (parms) == NULL_TREE)
3731 for (i = 0; i < ntparms; i++)
3734 if (!allow_incomplete)
3735 error ("incomplete type unification");
3741 /* Tail recursion is your friend. */
3744 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
3745 tree tparms, *targs, parm, arg;
3746 int *nsubsts, ntparms, strict;
3750 /* I don't think this will do the right thing with respect to types.
3751 But the only case I've seen it in so far has been array bounds, where
3752 signedness is the only information lost, and I think that will be
3754 while (TREE_CODE (parm) == NOP_EXPR)
3755 parm = TREE_OPERAND (parm, 0);
3757 if (arg == error_mark_node)
3759 if (arg == unknown_type_node)
3764 switch (TREE_CODE (parm))
3767 /* In a type which contains a nested-name-specifier, template
3768 argument values cannot be deduced for template parameters used
3769 within the nested-name-specifier. */
3772 case TEMPLATE_TYPE_PARM:
3774 idx = TEMPLATE_TYPE_IDX (parm);
3775 /* Check for mixed types and values. */
3776 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
3779 if (!strict && targs[idx] != NULL_TREE &&
3780 TREE_CODE (targs[idx]) == NOP_EXPR)
3781 /* An explicit template argument. Don't even try to match
3782 here; the overload resolution code will manage check to
3783 see whether the call is legal. */
3786 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
3787 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
3790 /* Template type parameters cannot contain cv-quals; i.e.
3791 template <class T> void f (T& a, T& b) will not generate
3792 void f (const int& a, const int& b). */
3793 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
3794 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
3796 arg = TYPE_MAIN_VARIANT (arg);
3799 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
3800 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
3801 arg = cp_build_type_variant (arg, constp, volatilep);
3804 /* Simple cases: Value already set, does match or doesn't. */
3805 if (targs[idx] == arg
3807 && TREE_CODE (targs[idx]) == NOP_EXPR
3808 && TREE_OPERAND (targs[idx], 0) == arg))
3810 else if (targs[idx])
3814 case TEMPLATE_CONST_PARM:
3816 idx = TEMPLATE_CONST_IDX (parm);
3819 int i = cp_tree_equal (targs[idx], arg);
3825 my_friendly_abort (42);
3828 targs[idx] = copy_to_permanent (arg);
3832 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
3833 return unify (tparms, targs, ntparms, parm,
3834 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
3836 if (TREE_CODE (arg) != POINTER_TYPE)
3838 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3841 case REFERENCE_TYPE:
3842 if (TREE_CODE (arg) == REFERENCE_TYPE)
3843 arg = TREE_TYPE (arg);
3844 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
3848 if (TREE_CODE (arg) != ARRAY_TYPE)
3850 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
3851 nsubsts, strict) != 0)
3853 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3861 if (TREE_CODE (arg) != TREE_CODE (parm))
3864 if (TREE_CODE (parm) == INTEGER_TYPE)
3866 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
3867 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
3868 TYPE_MIN_VALUE (arg), nsubsts, strict))
3870 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
3871 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
3872 TYPE_MAX_VALUE (arg), nsubsts, strict))
3875 else if (TREE_CODE (parm) == REAL_TYPE
3876 && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
3879 /* As far as unification is concerned, this wins. Later checks
3880 will invalidate it if necessary. */
3883 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3884 /* Type INTEGER_CST can come from ordinary constant template args. */
3886 while (TREE_CODE (arg) == NOP_EXPR)
3887 arg = TREE_OPERAND (arg, 0);
3889 if (TREE_CODE (arg) != INTEGER_CST)
3891 return !tree_int_cst_equal (parm, arg);
3896 t1 = TREE_OPERAND (parm, 0);
3897 t2 = TREE_OPERAND (parm, 1);
3898 return unify (tparms, targs, ntparms, t1,
3899 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
3906 if (TREE_CODE (arg) != TREE_VEC)
3908 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
3910 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
3911 if (unify (tparms, targs, ntparms,
3912 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
3919 if (TYPE_PTRMEMFUNC_FLAG (parm))
3920 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
3921 arg, nsubsts, strict);
3923 /* Allow trivial conversions. */
3924 if (TREE_CODE (arg) != RECORD_TYPE
3925 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
3926 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
3929 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
3932 if (flag_ansi_overloading && ! strict)
3933 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
3935 (CLASSTYPE_TEMPLATE_INFO (arg)
3936 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
3938 if (! t || t == error_mark_node)
3941 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
3942 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
3944 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
3949 if (TREE_CODE (arg) != METHOD_TYPE)
3954 if (TREE_CODE (arg) != FUNCTION_TYPE)
3957 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
3958 TREE_TYPE (arg), nsubsts, strict))
3960 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3961 TYPE_ARG_TYPES (arg), nsubsts, 1,
3965 if (TREE_CODE (arg) != OFFSET_TYPE)
3967 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
3968 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
3970 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
3971 TREE_TYPE (arg), nsubsts, strict);
3974 if (arg != decl_constant_value (parm))
3979 sorry ("use of `%s' in template type unification",
3980 tree_code_name [(int) TREE_CODE (parm)]);
3986 mark_decl_instantiated (result, extern_p)
3990 if (DECL_TEMPLATE_INSTANTIATION (result))
3991 SET_DECL_EXPLICIT_INSTANTIATION (result);
3992 TREE_PUBLIC (result) = 1;
3996 DECL_INTERFACE_KNOWN (result) = 1;
3997 DECL_NOT_REALLY_EXTERN (result) = 1;
3999 /* For WIN32 we also want to put explicit instantiations in
4000 linkonce sections. */
4001 if (supports_one_only () && ! SUPPORTS_WEAK)
4002 comdat_linkage (result);
4004 else if (TREE_CODE (result) == FUNCTION_DECL)
4005 mark_inline_for_output (result);
4008 /* Given two function templates PAT1 and PAT2, return:
4010 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
4011 -1 if PAT2 is more specialized than PAT1.
4012 0 if neither is more specialized. */
4015 more_specialized (pat1, pat2)
4021 targs = get_bindings (pat1, pat2);
4027 targs = get_bindings (pat2, pat1);
4036 /* Given two class template specialization list nodes PAT1 and PAT2, return:
4038 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4039 -1 if PAT2 is more specialized than PAT1.
4040 0 if neither is more specialized. */
4043 more_specialized_class (pat1, pat2)
4049 targs = get_class_bindings
4050 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
4054 targs = get_class_bindings
4055 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
4062 /* Return the template arguments that will produce the function signature
4063 DECL from the function template FN. */
4066 get_bindings (fn, decl)
4069 int ntparms = DECL_NTPARMS (fn);
4070 tree targs = make_scratch_vec (ntparms);
4073 i = fn_type_unification (fn, NULL_TREE, targs,
4074 TYPE_ARG_TYPES (TREE_TYPE (decl)),
4075 TREE_TYPE (TREE_TYPE (decl)),
4084 get_class_bindings (tparms, parms, args)
4085 tree tparms, parms, args;
4087 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
4088 tree vec = make_temp_vec (ntparms);
4090 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
4092 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
4093 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
4103 for (i = 0; i < ntparms; ++i)
4104 if (! TREE_VEC_ELT (vec, i))
4110 /* Return the most specialized of the list of templates in FNS that can
4111 produce an instantiation matching DECL. */
4114 most_specialized (fns, decl)
4117 tree fn, champ, args, *p;
4120 for (p = &fns; *p; )
4122 args = get_bindings (TREE_VALUE (*p), decl);
4125 p = &TREE_CHAIN (*p);
4128 *p = TREE_CHAIN (*p);
4135 champ = TREE_VALUE (fn);
4136 fn = TREE_CHAIN (fn);
4137 for (; fn; fn = TREE_CHAIN (fn))
4139 fate = more_specialized (champ, TREE_VALUE (fn));
4146 fn = TREE_CHAIN (fn);
4148 return error_mark_node;
4150 champ = TREE_VALUE (fn);
4154 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
4156 fate = more_specialized (champ, TREE_VALUE (fn));
4158 return error_mark_node;
4164 /* Return the most specialized of the class template specializations in
4165 SPECS that can produce an instantiation matching ARGS. */
4168 most_specialized_class (specs, mainargs)
4169 tree specs, mainargs;
4171 tree list = NULL_TREE, t, args, champ;
4174 for (t = specs; t; t = TREE_CHAIN (t))
4176 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
4179 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
4180 TREE_TYPE (list) = TREE_TYPE (t);
4190 for (; t; t = TREE_CHAIN (t))
4192 fate = more_specialized_class (champ, t);
4201 return error_mark_node;
4207 for (t = list; t && t != champ; t = TREE_CHAIN (t))
4209 fate = more_specialized_class (champ, t);
4211 return error_mark_node;
4217 /* called from the parser. */
4220 do_decl_instantiation (declspecs, declarator, storage)
4221 tree declspecs, declarator, storage;
4223 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
4226 tree result = NULL_TREE;
4228 tree templates = NULL_TREE;
4230 if (! DECL_LANG_SPECIFIC (decl))
4232 cp_error ("explicit instantiation of non-template `%#D'", decl);
4236 /* If we've already seen this template instance, use it. */
4237 if (TREE_CODE (decl) == VAR_DECL)
4239 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
4240 if (result && TREE_CODE (result) != VAR_DECL)
4243 else if (TREE_CODE (decl) != FUNCTION_DECL)
4245 cp_error ("explicit instantiation of `%#D'", decl);
4248 else if (DECL_FUNCTION_MEMBER_P (decl))
4250 if (DECL_TEMPLATE_INSTANTIATION (decl))
4252 else if (name = DECL_ASSEMBLER_NAME (decl),
4253 fn = IDENTIFIER_GLOBAL_VALUE (name),
4254 fn && DECL_TEMPLATE_INSTANTIATION (fn))
4258 /* Maybe this is an instantiation of a member template
4260 tree ctype = DECL_CONTEXT (decl);
4262 name = DECL_NAME (decl);
4263 fn = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
4265 fn = TREE_VALUE (fn);
4267 for (; fn; fn = DECL_CHAIN (fn))
4268 if (TREE_CODE (fn) == TEMPLATE_DECL)
4269 templates = decl_tree_cons (NULL_TREE, fn, templates);
4272 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
4274 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
4275 if (TREE_CODE (fn) == TEMPLATE_DECL)
4276 templates = decl_tree_cons (NULL_TREE, fn, templates);
4279 if (templates && !result)
4282 result = most_specialized (templates, decl);
4283 if (result == error_mark_node)
4285 char *str = "candidates are:";
4286 cp_error ("ambiguous template instantiation for `%D' requested", decl);
4287 for (fn = templates; fn; fn = TREE_CHAIN (fn))
4289 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
4296 args = get_bindings (result, decl);
4297 result = instantiate_template (result, args);
4303 cp_error ("no matching template for `%D' found", decl);
4307 if (! DECL_TEMPLATE_INFO (result))
4309 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
4313 if (flag_external_templates)
4316 if (storage == NULL_TREE)
4318 else if (storage == ridpointers[(int) RID_EXTERN])
4321 cp_error ("storage class `%D' applied to template instantiation",
4324 mark_decl_instantiated (result, extern_p);
4325 repo_template_instantiated (result, extern_p);
4327 instantiate_decl (result);
4331 mark_class_instantiated (t, extern_p)
4335 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
4336 SET_CLASSTYPE_INTERFACE_KNOWN (t);
4337 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
4338 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
4339 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
4342 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
4343 rest_of_type_compilation (t, 1);
4348 do_type_instantiation (t, storage)
4355 if (TREE_CODE (t) == TYPE_DECL)
4358 if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
4360 cp_error ("explicit instantiation of non-template type `%T'", t);
4366 /* With -fexternal-templates, explicit instantiations are treated the same
4367 as implicit ones. */
4368 if (flag_external_templates)
4371 if (TYPE_SIZE (t) == NULL_TREE)
4373 cp_error ("explicit instantiation of `%#T' before definition of template",
4378 if (storage == NULL_TREE)
4380 else if (storage == ridpointers[(int) RID_INLINE])
4382 else if (storage == ridpointers[(int) RID_EXTERN])
4384 else if (storage == ridpointers[(int) RID_STATIC])
4388 cp_error ("storage class `%D' applied to template instantiation",
4393 /* We've already instantiated this. */
4394 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
4398 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4400 mark_class_instantiated (t, extern_p);
4401 repo_template_instantiated (t, extern_p);
4411 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
4412 if (TREE_CODE (tmp) == FUNCTION_DECL
4413 && DECL_TEMPLATE_INSTANTIATION (tmp))
4415 mark_decl_instantiated (tmp, extern_p);
4416 repo_template_instantiated (tmp, extern_p);
4418 instantiate_decl (tmp);
4421 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
4422 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
4424 mark_decl_instantiated (tmp, extern_p);
4425 repo_template_instantiated (tmp, extern_p);
4427 instantiate_decl (tmp);
4430 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
4431 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
4432 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
4437 instantiate_decl (d)
4440 tree ti = DECL_TEMPLATE_INFO (d);
4441 tree tmpl = TI_TEMPLATE (ti);
4442 tree args = TI_ARGS (ti);
4444 tree decl_pattern, code_pattern;
4446 int nested = in_function_p ();
4448 int pattern_defined;
4450 char *file = input_filename;
4452 for (td = tmpl; DECL_TEMPLATE_INSTANTIATION (td); )
4453 td = DECL_TI_TEMPLATE (td);
4455 /* In the case of a member template, decl_pattern is the partially
4456 instantiated declaration (in the instantiated class), and code_pattern
4457 is the original template definition. */
4458 decl_pattern = DECL_TEMPLATE_RESULT (tmpl);
4459 code_pattern = DECL_TEMPLATE_RESULT (td);
4461 if (TREE_CODE (d) == FUNCTION_DECL)
4463 d_defined = (DECL_INITIAL (d) != NULL_TREE);
4464 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
4468 d_defined = ! DECL_IN_AGGR_P (d);
4469 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
4475 if (TREE_CODE (d) == FUNCTION_DECL)
4479 /* Check to see if there is a matching specialization. */
4480 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4482 specs = TREE_CHAIN (specs))
4483 if (comp_template_args (TREE_PURPOSE (specs), args))
4484 return TREE_VALUE (specs);
4487 /* This needs to happen before any tsubsting. */
4488 if (! push_tinst_level (d))
4491 push_to_top_level ();
4492 lineno = DECL_SOURCE_LINE (d);
4493 input_filename = DECL_SOURCE_FILE (d);
4495 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
4496 variable is a static const initialized in the class body. */
4497 if (TREE_CODE (d) == VAR_DECL
4498 && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern))
4500 pushclass (DECL_CONTEXT (d), 2);
4501 DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args,
4502 TREE_VEC_LENGTH (args), tmpl);
4506 if (pattern_defined)
4508 repo_template_used (d);
4510 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
4512 if (flag_alt_external_templates)
4514 if (interface_unknown)
4515 warn_if_unknown_interface (d);
4517 else if (DECL_INTERFACE_KNOWN (code_pattern))
4519 DECL_INTERFACE_KNOWN (d) = 1;
4520 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
4523 warn_if_unknown_interface (code_pattern);
4527 import_export_decl (d);
4530 if (! pattern_defined
4531 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
4532 && (! DECL_INTERFACE_KNOWN (d)
4533 || ! DECL_NOT_REALLY_EXTERN (d)))
4534 /* Kludge: if we compile a constructor in the middle of processing a
4535 toplevel declaration, we blow away the declspecs in
4536 temp_decl_obstack when we call permanent_allocation in
4537 finish_function. So don't compile it yet. */
4538 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
4540 add_pending_template (d);
4544 lineno = DECL_SOURCE_LINE (d);
4545 input_filename = DECL_SOURCE_FILE (d);
4547 /* Trick tsubst into giving us a new decl in case the template changed. */
4548 save_ti = DECL_TEMPLATE_INFO (decl_pattern);
4549 DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE;
4550 td = tsubst (decl_pattern, args, TREE_VEC_LENGTH (args), tmpl);
4551 SET_DECL_IMPLICIT_INSTANTIATION (td);
4552 DECL_TEMPLATE_INFO (decl_pattern) = save_ti;
4554 /* And set up DECL_INITIAL, since tsubst doesn't. */
4555 if (TREE_CODE (td) == VAR_DECL)
4557 pushclass (DECL_CONTEXT (d), 2);
4558 DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args,
4559 TREE_VEC_LENGTH (args), tmpl);
4563 if (TREE_CODE (d) == FUNCTION_DECL)
4565 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4567 DECL_INITIAL (td) = error_mark_node;
4569 if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td))
4570 /* Set up the information about what is being specialized. */
4571 DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d);
4573 duplicate_decls (td, d);
4574 if (TREE_CODE (d) == FUNCTION_DECL)
4575 DECL_INITIAL (td) = 0;
4577 if (TREE_CODE (d) == VAR_DECL)
4579 DECL_IN_AGGR_P (d) = 0;
4580 if (DECL_INTERFACE_KNOWN (d))
4581 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
4584 DECL_EXTERNAL (d) = 1;
4585 DECL_NOT_REALLY_EXTERN (d) = 1;
4587 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
4589 else if (TREE_CODE (d) == FUNCTION_DECL)
4591 tree t = DECL_SAVED_TREE (code_pattern);
4593 start_function (NULL_TREE, d, NULL_TREE, 1);
4594 store_parm_decls ();
4596 if (t && TREE_CODE (t) == RETURN_INIT)
4599 (TREE_OPERAND (t, 0),
4600 tsubst_expr (TREE_OPERAND (t, 1), args,
4601 TREE_VEC_LENGTH (args), tmpl));
4605 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
4607 current_member_init_list
4608 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
4609 current_base_init_list
4610 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
4615 /* Always keep the BLOCK node associated with the outermost
4616 pair of curly braces of a function. These are needed
4617 for correct operation of dwarfout.c. */
4620 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
4621 tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl);
4623 finish_function (lineno, 0, nested);
4628 input_filename = file;
4630 pop_from_top_level ();
4637 tsubst_chain (t, argvec)
4642 tree first = tsubst (t, argvec,
4643 TREE_VEC_LENGTH (argvec), NULL_TREE);
4646 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
4648 tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE);
4649 TREE_CHAIN (last) = x;
4659 tsubst_expr_values (t, argvec)
4662 tree first = NULL_TREE;
4665 for (; t; t = TREE_CHAIN (t))
4667 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
4668 TREE_VEC_LENGTH (argvec), NULL_TREE);
4669 tree val = tsubst_expr (TREE_VALUE (t), argvec,
4670 TREE_VEC_LENGTH (argvec), NULL_TREE);
4671 *p = build_tree_list (pur, val);
4672 p = &TREE_CHAIN (*p);
4683 last_tree = TREE_CHAIN (last_tree) = t;
4686 /* D is an undefined function declaration in the presence of templates with
4687 the same name, listed in FNS. If one of them can produce D as an
4688 instantiation, remember this so we can instantiate it at EOF if D has
4689 not been defined by that time. */
4692 add_maybe_template (d, fns)
4697 if (DECL_MAYBE_TEMPLATE (d))
4700 t = most_specialized (fns, d);
4703 if (t == error_mark_node)
4705 cp_error ("ambiguous template instantiation for `%D'", d);
4709 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
4710 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
4711 DECL_MAYBE_TEMPLATE (d) = 1;
4714 /* Instantiate an enumerated type. Used by instantiate_class_template and
4718 tsubst_enum (tag, args, nargs, field_chain)
4723 extern tree current_local_enum;
4724 tree prev_local_enum = current_local_enum;
4726 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
4727 tree e, values = NULL_TREE;
4729 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
4731 tree elt = build_enumerator (TREE_PURPOSE (e),
4732 tsubst_expr (TREE_VALUE (e), args,
4734 TREE_CHAIN (elt) = values;
4738 finish_enum (newtag, values);
4740 if (NULL != field_chain)
4741 *field_chain = grok_enum_decls (newtag, NULL_TREE);
4743 current_local_enum = prev_local_enum;