1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
25 templates for class static data don't work (methods only),
26 duplicated method templates can crash the compiler,
27 interface/impl data is taken from file defining the template,
28 all methods must be provided in header files; can't use a source
29 file that contains only the method templates and "just win". */
44 extern struct obstack permanent_obstack;
47 extern char *input_filename;
48 struct pending_inline *pending_template_expansions;
50 tree current_template_parms;
51 HOST_WIDE_INT processing_template_decl;
53 tree pending_templates;
54 static tree *template_tail = &pending_templates;
57 static tree *maybe_template_tail = &maybe_templates;
59 int minimal_parse_mode;
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
66 void pop_template_decls ();
68 tree classtype_mangled_name ();
69 static char * mangle_class_name_for_template ();
70 tree tsubst_expr_values ();
71 tree most_specialized_class PROTO((tree, tree));
72 tree get_class_bindings PROTO((tree, tree, tree));
73 tree make_temp_vec PROTO((int));
75 /* We've got a template header coming up; push to a new level for storing
79 begin_template_parm_list ()
82 declare_pseudo_global_level ();
83 ++processing_template_decl;
86 /* Process information from new template parameter NEXT and append it to the
90 process_template_parm (list, next)
98 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
99 defval = TREE_PURPOSE (parm);
100 parm = TREE_VALUE (parm);
101 is_type = TREE_PURPOSE (parm) == class_type_node;
105 tree p = TREE_VALUE (tree_last (list));
107 if (TREE_CODE (p) == TYPE_DECL)
108 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
110 idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
119 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
120 /* is a const-param */
121 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
123 /* A template parameter is not modifiable. */
124 TREE_READONLY (parm) = 1;
125 if (IS_AGGR_TYPE (TREE_TYPE (parm))
126 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
128 cp_error ("`%#T' is not a valid type for a template constant parameter",
130 if (DECL_NAME (parm) == NULL_TREE)
131 error (" a template type parameter must begin with `class' or `typename'");
132 TREE_TYPE (parm) = void_type_node;
134 tinfo = make_node (TEMPLATE_CONST_PARM);
135 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
136 if (TREE_PERMANENT (parm) == 0)
138 parm = copy_node (parm);
139 TREE_PERMANENT (parm) = 1;
141 TREE_TYPE (tinfo) = TREE_TYPE (parm);
142 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
143 DECL_INITIAL (decl) = tinfo;
144 DECL_INITIAL (parm) = tinfo;
145 TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl);
149 tree t = make_lang_type (TEMPLATE_TYPE_PARM);
150 CLASSTYPE_GOT_SEMICOLON (t) = 1;
151 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
152 TYPE_MAIN_DECL (t) = decl;
154 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
156 SET_DECL_ARTIFICIAL (decl);
158 parm = build_tree_list (defval, parm);
159 return chainon (list, parm);
162 /* The end of a template parameter list has been reached. Process the
163 tree list into a parameter vector, converting each parameter into a more
164 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
168 end_template_parm_list (parms)
173 tree saved_parmlist = make_tree_vec (list_length (parms));
175 current_template_parms
176 = tree_cons (build_int_2 (0, processing_template_decl),
177 saved_parmlist, current_template_parms);
179 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
180 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
182 return saved_parmlist;
185 /* end_template_decl is called after a template declaration is seen. */
190 if (! processing_template_decl)
193 /* This matches the pushlevel in begin_template_parm_list. */
196 --processing_template_decl;
197 current_template_parms = TREE_CHAIN (current_template_parms);
198 (void) get_pending_sizes (); /* Why? */
201 /* Generate a valid set of template args from current_template_parms. */
204 current_template_args ()
206 tree header = current_template_parms;
207 tree args = NULL_TREE;
210 tree a = copy_node (TREE_VALUE (header));
211 int i = TREE_VEC_LENGTH (a);
212 TREE_TYPE (a) = NULL_TREE;
215 tree t = TREE_VALUE (TREE_VEC_ELT (a, i));
216 if (TREE_CODE (t) == TYPE_DECL)
219 t = DECL_INITIAL (t);
220 TREE_VEC_ELT (a, i) = t;
222 args = tree_cons (TREE_PURPOSE (header), a, args);
223 header = TREE_CHAIN (header);
225 args = nreverse (args);
227 /* FIXME Remove this when we support member templates. */
228 args = TREE_VALUE (args);
234 push_template_decl (decl)
238 tree args = NULL_TREE;
240 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
244 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
245 && DECL_CLASS_CONTEXT (decl))
247 /* Note that this template is a "primary template" */
248 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
249 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
252 /* Partial specialization. */
253 if (TREE_CODE (decl) == TYPE_DECL
254 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
256 tree type = TREE_TYPE (decl);
257 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
258 tree mainargs = CLASSTYPE_TI_ARGS (type);
259 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
261 for (; spec; spec = TREE_CHAIN (spec))
263 /* purpose: args to main template
264 value: spec template */
265 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
269 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = perm_tree_cons
270 (mainargs, TREE_VALUE (current_template_parms),
271 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
272 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
276 args = current_template_args ();
278 if (! ctx || TYPE_BEING_DEFINED (ctx))
280 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
281 DECL_TEMPLATE_PARMS (tmpl) = TREE_VALUE (current_template_parms);
282 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
286 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
287 cp_error ("must specialize `%#T' before defining member `%#D'",
289 if (TREE_CODE (decl) == TYPE_DECL)
290 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
291 else if (! DECL_TEMPLATE_INFO (decl))
293 cp_error ("template definition of non-template `%#D'", decl);
297 tmpl = DECL_TI_TEMPLATE (decl);
300 DECL_TEMPLATE_RESULT (tmpl) = decl;
301 TREE_TYPE (tmpl) = TREE_TYPE (decl);
304 tmpl = pushdecl_top_level (tmpl);
307 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl)) = tmpl;
309 info = perm_tree_cons (tmpl, args, NULL_TREE);
311 if (TREE_CODE (decl) == TYPE_DECL)
313 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
314 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
316 else if (! DECL_LANG_SPECIFIC (decl))
317 cp_error ("template declaration of `%#D'", decl);
319 DECL_TEMPLATE_INFO (decl) = info;
322 tree tsubst PROTO ((tree, tree*, int, tree));
324 /* Convert all template arguments to their appropriate types, and return
325 a vector containing the resulting values. If any error occurs, return
329 coerce_template_parms (parms, arglist, in_decl)
333 int nparms, nargs, i, lost = 0;
336 if (arglist == NULL_TREE)
338 else if (TREE_CODE (arglist) == TREE_VEC)
339 nargs = TREE_VEC_LENGTH (arglist);
341 nargs = list_length (arglist);
343 nparms = TREE_VEC_LENGTH (parms);
347 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
349 error ("incorrect number of parameters (%d, should be %d)",
352 cp_error_at ("in template expansion for decl `%D'", in_decl);
353 return error_mark_node;
356 if (arglist && TREE_CODE (arglist) == TREE_VEC)
357 vec = copy_node (arglist);
360 vec = make_tree_vec (nparms);
361 for (i = 0; i < nparms; i++)
368 arglist = TREE_CHAIN (arglist);
370 if (arg == error_mark_node)
373 arg = TREE_VALUE (arg);
376 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
377 &TREE_VEC_ELT (vec, 0), i, in_decl);
379 TREE_VEC_ELT (vec, i) = arg;
382 for (i = 0; i < nparms; i++)
384 tree arg = TREE_VEC_ELT (vec, i);
385 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
387 int is_type, requires_type;
389 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
390 requires_type = TREE_CODE (parm) == TYPE_DECL;
392 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
393 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
395 cp_pedwarn ("to refer to a type member of a template parameter,");
396 cp_pedwarn (" use `typename %E'", arg);
397 arg = make_typename_type (TREE_OPERAND (arg, 0),
398 TREE_OPERAND (arg, 1));
401 if (is_type != requires_type)
405 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
408 cp_error (" expected a constant of type `%T', got `%T'",
409 TREE_TYPE (parm), arg);
411 cp_error (" expected a type, got `%E'", arg);
414 TREE_VEC_ELT (vec, i) = error_mark_node;
419 val = groktypename (arg);
420 if (! processing_template_decl)
422 tree t = target_type (val);
424 && decl_function_context (TYPE_MAIN_DECL (t)))
426 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
427 return error_mark_node;
433 tree t = tsubst (TREE_TYPE (parm), &TREE_VEC_ELT (vec, 0),
434 TREE_VEC_LENGTH (vec), in_decl);
435 if (processing_template_decl)
438 val = digest_init (t, arg, (tree *) 0);
440 if (val == error_mark_node || processing_template_decl)
443 /* 14.2: Other template-arguments must be constant-expressions,
444 addresses of objects or functions with external linkage, or of
445 static class members. */
446 else if (!TREE_CONSTANT (val))
448 cp_error ("non-const `%E' cannot be used as template argument",
450 val = error_mark_node;
452 else if (POINTER_TYPE_P (TREE_TYPE (val))
453 && ! integer_zerop (val)
454 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
455 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
459 if (TREE_CODE (t) == ADDR_EXPR)
461 tree a = TREE_OPERAND (t, 0);
463 if (TREE_CODE (a) == STRING_CST)
465 cp_error ("string literal %E is not a valid template argument", a);
466 error ("because it is the address of an object with static linkage");
467 val = error_mark_node;
469 else if (TREE_CODE (a) != VAR_DECL
470 && TREE_CODE (a) != FUNCTION_DECL)
472 else if (! DECL_PUBLIC (a))
474 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
475 val = error_mark_node;
481 cp_error ("`%E' is not a valid template argument", t);
482 error ("it must be %s%s with external linkage",
483 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
484 ? "a pointer to " : "",
485 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
486 ? "a function" : "an object");
487 val = error_mark_node;
492 if (val == error_mark_node)
495 TREE_VEC_ELT (vec, i) = val;
498 return error_mark_node;
503 comp_template_args (oldargs, newargs)
504 tree oldargs, newargs;
508 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
510 tree nt = TREE_VEC_ELT (newargs, i);
511 tree ot = TREE_VEC_ELT (oldargs, i);
515 if (TREE_CODE (nt) != TREE_CODE (ot))
517 if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
519 if (comptypes (ot, nt, 1))
522 else if (cp_tree_equal (ot, nt) > 0)
529 /* Given class template name and parameter list, produce a user-friendly name
530 for the instantiation. */
533 mangle_class_name_for_template (name, parms, arglist)
537 static struct obstack scratch_obstack;
538 static char *scratch_firstobj;
541 if (!scratch_firstobj)
542 gcc_obstack_init (&scratch_obstack);
544 obstack_free (&scratch_obstack, scratch_firstobj);
545 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
548 #define buflen sizeof(buf)
549 #define check if (bufp >= buf+buflen-1) goto too_long
550 #define ccat(c) *bufp++=(c); check
551 #define advance bufp+=strlen(bufp); check
552 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
555 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
557 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
562 nparms = TREE_VEC_LENGTH (parms);
563 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
564 for (i = 0; i < nparms; i++)
566 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
567 tree arg = TREE_VEC_ELT (arglist, i);
572 if (TREE_CODE (parm) == TYPE_DECL)
574 cat (type_as_string (arg, 0));
578 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
580 if (TREE_CODE (arg) == TREE_LIST)
582 /* New list cell was built because old chain link was in
584 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
585 arg = TREE_VALUE (arg);
587 /* No need to check arglist against parmlist here; we did that
588 in coerce_template_parms, called from lookup_template_class. */
589 cat (expr_as_string (arg, 0));
592 char *bufp = obstack_next_free (&scratch_obstack);
594 while (bufp[offset - 1] == ' ')
596 obstack_blank_fast (&scratch_obstack, offset);
598 /* B<C<char> >, not B<C<char>> */
599 if (bufp[offset - 1] == '>')
604 return (char *) obstack_base (&scratch_obstack);
609 fatal ("out of (preallocated) string space creating template instantiation name");
615 classtype_mangled_name (t)
618 if (CLASSTYPE_TEMPLATE_INFO (t)
619 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
621 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
622 char *mangled_name = mangle_class_name_for_template
623 (IDENTIFIER_POINTER (name),
624 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
625 CLASSTYPE_TI_ARGS (t));
626 tree id = get_identifier (mangled_name);
627 IDENTIFIER_TEMPLATE (id) = name;
631 return TYPE_IDENTIFIER (t);
635 add_pending_template (d)
640 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
641 ti = CLASSTYPE_TEMPLATE_INFO (d);
643 ti = DECL_TEMPLATE_INFO (d);
645 if (TREE_LANG_FLAG_0 (ti))
648 *template_tail = perm_tree_cons
649 (current_function_decl, d, NULL_TREE);
650 template_tail = &TREE_CHAIN (*template_tail);
651 TREE_LANG_FLAG_0 (ti) = 1;
654 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
655 parameters, find the desired type.
657 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
658 Since ARGLIST is build on the decl_obstack, we must copy it here
659 to keep it from being reclaimed when the decl storage is reclaimed.
661 IN_DECL, if non-NULL, is the template declaration we are trying to
665 lookup_template_class (d1, arglist, in_decl)
669 tree template, parmlist;
674 if (TREE_CODE (d1) == IDENTIFIER_NODE)
676 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
678 template = IDENTIFIER_CLASS_VALUE (d1);
680 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
682 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
683 d1 = DECL_NAME (template);
685 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
687 template = CLASSTYPE_TI_TEMPLATE (d1);
688 d1 = DECL_NAME (template);
691 my_friendly_abort (272);
693 /* With something like `template <class T> class X class X { ... };'
694 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
695 We don't want to do that, but we have to deal with the situation, so
696 let's give them some syntax errors to chew on instead of a crash. */
698 return error_mark_node;
699 if (TREE_CODE (template) != TEMPLATE_DECL)
701 cp_error ("non-template type `%T' used as a template", d1);
703 cp_error_at ("for template declaration `%D'", in_decl);
704 return error_mark_node;
707 if (PRIMARY_TEMPLATE_P (template))
709 parmlist = DECL_TEMPLATE_PARMS (template);
711 arglist = coerce_template_parms (parmlist, arglist, template);
712 if (arglist == error_mark_node)
713 return error_mark_node;
714 if (uses_template_parms (arglist))
717 if (comp_template_args
718 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
719 found = TREE_TYPE (template);
722 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
723 found; found = TREE_CHAIN (found))
725 if (TI_USES_TEMPLATE_PARMS (found)
726 && comp_template_args (TREE_PURPOSE (found), arglist))
730 found = TREE_VALUE (found);
735 if (can_free (&permanent_obstack, arglist))
736 obstack_free (&permanent_obstack, arglist);
741 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
743 id = get_identifier (mangled_name);
744 IDENTIFIER_TEMPLATE (id) = d1;
746 maybe_push_to_top_level (uses_template_parms (arglist));
747 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
748 pop_from_top_level ();
752 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
755 arglist = CLASSTYPE_TI_ARGS (ctx);
757 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
759 int save_temp = processing_template_decl;
760 processing_template_decl = 0;
761 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
762 processing_template_decl = save_temp;
766 t = lookup_nested_type_by_name (ctx, id);
767 my_friendly_assert (t != NULL_TREE, 42);
771 /* Seems to be wanted. */
772 CLASSTYPE_GOT_SEMICOLON (t) = 1;
774 if (! CLASSTYPE_TEMPLATE_INFO (t))
776 arglist = copy_to_permanent (arglist);
777 CLASSTYPE_TEMPLATE_INFO (t)
778 = perm_tree_cons (template, arglist, NULL_TREE);
779 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
780 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
781 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
782 = uses_template_parms (arglist);
784 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
786 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
787 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
788 if (! uses_template_parms (arglist))
789 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
790 = get_identifier (build_overload_name (t, 1, 1));
792 if (flag_external_templates && ! uses_template_parms (arglist)
793 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
794 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
795 add_pending_template (t);
801 /* Should be defined in parse.h. */
805 uses_template_parms (t)
810 switch (TREE_CODE (t))
814 /* We assume that the object must be instantiated in order to build
815 the COMPONENT_REF, so we test only whether the type of the
816 COMPONENT_REF uses template parms. */
817 return uses_template_parms (TREE_TYPE (t));
819 case IDENTIFIER_NODE:
820 if (!IDENTIFIER_TEMPLATE (t))
822 my_friendly_abort (42);
824 /* aggregates of tree nodes */
827 int i = TREE_VEC_LENGTH (t);
829 if (uses_template_parms (TREE_VEC_ELT (t, i)))
834 if (uses_template_parms (TREE_PURPOSE (t))
835 || uses_template_parms (TREE_VALUE (t)))
837 return uses_template_parms (TREE_CHAIN (t));
839 /* constructed type nodes */
842 return uses_template_parms (TREE_TYPE (t));
844 if (TYPE_PTRMEMFUNC_FLAG (t))
845 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
847 if (! CLASSTYPE_TEMPLATE_INFO (t))
849 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
851 if (uses_template_parms (TYPE_ARG_TYPES (t)))
853 return uses_template_parms (TREE_TYPE (t));
855 if (uses_template_parms (TYPE_DOMAIN (t)))
857 return uses_template_parms (TREE_TYPE (t));
859 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
861 return uses_template_parms (TREE_TYPE (t));
863 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
865 if (uses_template_parms (TYPE_ARG_TYPES (t)))
867 return uses_template_parms (TREE_TYPE (t));
871 return uses_template_parms (TREE_TYPE (t));
875 /* ??? What about FIELD_DECLs? */
876 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
877 && uses_template_parms (DECL_TI_ARGS (t)))
882 if (uses_template_parms (TREE_TYPE (t)))
884 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
889 return uses_template_parms (TREE_TYPE (t));
891 return uses_template_parms (TREE_OPERAND (t, 0));
893 /* template parm nodes */
894 case TEMPLATE_TYPE_PARM:
895 case TEMPLATE_CONST_PARM:
898 /* simple type nodes */
900 if (uses_template_parms (TYPE_MIN_VALUE (t)))
902 return uses_template_parms (TYPE_MAX_VALUE (t));
917 /* Non-error_mark_node ERROR_MARKs are bad things. */
918 my_friendly_assert (t == error_mark_node, 274);
927 return uses_template_parms (TREE_OPERAND (t, 0));
930 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
931 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
932 return uses_template_parms (TREE_OPERAND (t, 1));
935 switch (TREE_CODE_CLASS (TREE_CODE (t)))
943 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
944 if (uses_template_parms (TREE_OPERAND (t, i)))
951 sorry ("testing %s for template parms",
952 tree_code_name [(int) TREE_CODE (t)]);
953 my_friendly_abort (82);
959 static struct tinst_level *current_tinst_level = 0;
960 static struct tinst_level *free_tinst_level = 0;
961 static int tinst_depth = 0;
962 int max_tinst_depth = 17;
963 #ifdef GATHER_STATISTICS
964 int depth_reached = 0;
971 struct tinst_level *new;
973 if (tinst_depth >= max_tinst_depth)
975 struct tinst_level *p = current_tinst_level;
977 char *file = input_filename;
979 error ("template instantiation depth exceeds maximum of %d",
981 cp_error (" instantiating `%D'", d);
983 for (; p; p = p->next)
985 cp_error (" instantiated from `%D'", p->decl);
987 input_filename = p->file;
989 error (" instantiated from here");
992 input_filename = file;
997 if (free_tinst_level)
999 new = free_tinst_level;
1000 free_tinst_level = new->next;
1003 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1007 new->file = input_filename;
1008 new->next = current_tinst_level;
1009 current_tinst_level = new;
1012 #ifdef GATHER_STATISTICS
1013 if (tinst_depth > depth_reached)
1014 depth_reached = tinst_depth;
1023 struct tinst_level *old = current_tinst_level;
1025 current_tinst_level = old->next;
1026 old->next = free_tinst_level;
1027 free_tinst_level = old;
1031 struct tinst_level *
1034 struct tinst_level *p = current_tinst_level;
1037 for (; p->next ; p = p->next )
1043 instantiate_class_template (type)
1046 tree template, template_info, args, pattern, t, *field_chain;
1048 if (type == error_mark_node)
1049 return error_mark_node;
1051 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1053 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1056 template = TI_TEMPLATE (template_info);
1057 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1058 args = TI_ARGS (template_info);
1060 t = most_specialized_class
1061 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1063 if (t == error_mark_node)
1065 char *str = "candidates are:";
1066 cp_error ("ambiguous class template instantiation for `%#T'", type);
1067 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1069 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1071 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1075 TYPE_BEING_DEFINED (type) = 1;
1079 pattern = TREE_TYPE (t);
1081 pattern = TREE_TYPE (template);
1083 if (TYPE_SIZE (pattern) == NULL_TREE)
1087 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1089 TYPE_BEING_DEFINED (type) = 1;
1091 if (! push_tinst_level (type))
1094 maybe_push_to_top_level (uses_template_parms (type));
1095 pushclass (type, 0);
1097 if (flag_external_templates)
1099 if (flag_alt_external_templates)
1101 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1102 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1103 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1104 = ! CLASSTYPE_INTERFACE_ONLY (type)
1105 && CLASSTYPE_INTERFACE_KNOWN (type);
1109 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1110 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1111 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1112 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1113 = ! CLASSTYPE_INTERFACE_ONLY (type)
1114 && CLASSTYPE_INTERFACE_KNOWN (type);
1119 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1120 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
1124 tree binfo = TYPE_BINFO (type);
1125 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1131 int len = TREE_VEC_LENGTH (pbases);
1132 BINFO_BASETYPES (binfo) = bases = make_tree_vec (len);
1133 for (i = 0; i < len; ++i)
1137 TREE_VEC_ELT (bases, i) = elt
1138 = tsubst (TREE_VEC_ELT (pbases, i), &TREE_VEC_ELT (args, 0),
1139 TREE_VEC_LENGTH (args), NULL_TREE);
1140 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1142 if (! uses_template_parms (type) &&
1143 TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE)
1144 cp_error ("base class `%T' of `%T' has incomplete type",
1145 TREE_TYPE (elt), type);
1150 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1152 field_chain = &TYPE_FIELDS (type);
1154 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1156 tree name = TREE_PURPOSE (t);
1157 tree tag = TREE_VALUE (t);
1160 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1161 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1162 newtag = start_enum (name);
1164 newtag = tsubst (tag, &TREE_VEC_ELT (args, 0),
1165 TREE_VEC_LENGTH (args), NULL_TREE);
1167 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1169 tree e, values = NULL_TREE, *last = &values;
1171 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
1173 tree elt = build_enumerator
1175 tsubst_expr (TREE_VALUE (e), &TREE_VEC_ELT (args, 0),
1176 TREE_VEC_LENGTH (args), NULL_TREE));
1177 DECL_FIELD_CONTEXT (TREE_VALUE (elt)) = type;
1179 last = &TREE_CHAIN (elt);
1182 finish_enum (newtag, values);
1184 *field_chain = grok_enum_decls (newtag, NULL_TREE);
1185 while (*field_chain)
1186 field_chain = &TREE_CHAIN (*field_chain);
1190 /* Don't replace enum constants here. */
1191 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1192 if (TREE_CODE (t) != CONST_DECL)
1194 tree r = tsubst (t, &TREE_VEC_ELT (args, 0),
1195 TREE_VEC_LENGTH (args), NULL_TREE);
1196 if (TREE_CODE (r) == VAR_DECL)
1198 if (! uses_template_parms (r))
1199 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1200 /* Perhaps I should do more of grokfield here. */
1202 DECL_IN_AGGR_P (r) = 1;
1203 DECL_EXTERNAL (r) = 1;
1204 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1208 field_chain = &TREE_CHAIN (r);
1211 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1213 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1214 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1215 &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), NULL_TREE);
1218 tree d = CLASSTYPE_FRIEND_CLASSES (type) =
1219 tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), &TREE_VEC_ELT (args, 0),
1220 TREE_VEC_LENGTH (args), NULL_TREE);
1222 /* This does injection for friend classes. */
1223 for (; d; d = TREE_CHAIN (d))
1224 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1226 d = tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args, 0),
1227 TREE_VEC_LENGTH (args), NULL_TREE);
1229 for (; d; d = TREE_CHAIN (d))
1231 tree t = TREE_VALUE (d);
1233 if (TREE_CODE (t) == TYPE_DECL)
1234 /* Already injected. */;
1240 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1241 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1242 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1243 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1244 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1245 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1246 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1247 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1248 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1249 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1250 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1251 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1252 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1253 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1254 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1255 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1256 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1257 TYPE_USES_COMPLEX_INHERITANCE (type)
1258 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1259 TYPE_USES_MULTIPLE_INHERITANCE (type)
1260 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1261 TYPE_USES_VIRTUAL_BASECLASSES (type)
1262 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1263 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1264 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1266 if (! uses_template_parms (type))
1269 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1270 if (TREE_CODE (tmp) == FIELD_DECL)
1272 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1273 require_complete_type (tmp);
1276 type = finish_struct_1 (type, 0);
1277 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1278 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1279 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1281 repo_template_used (type);
1285 TYPE_SIZE (type) = integer_zero_node;
1286 CLASSTYPE_METHOD_VEC (type)
1287 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1290 TYPE_BEING_DEFINED (type) = 0;
1293 pop_from_top_level ();
1303 if (t1 == NULL_TREE)
1304 return t2 == NULL_TREE;
1305 if (t2 == NULL_TREE)
1307 /* Don't care if one declares its arg const and the other doesn't -- the
1308 main variant of the arg type is all that matters. */
1309 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1310 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1312 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1316 lookup_nested_type_by_name (ctype, name)
1321 complete_type (ctype);
1323 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
1325 if (name == TREE_PURPOSE (t))
1326 return TREE_VALUE (t);
1332 tsubst (t, args, nargs, in_decl)
1339 if (t == NULL_TREE || t == error_mark_node
1340 || t == integer_type_node
1341 || t == void_type_node
1342 || t == char_type_node)
1345 type = TREE_TYPE (t);
1346 if (type == unknown_type_node)
1347 my_friendly_abort (42);
1348 if (type && TREE_CODE (t) != FUNCTION_DECL)
1349 type = tsubst (type, args, nargs, in_decl);
1351 switch (TREE_CODE (t))
1354 if (TYPE_PTRMEMFUNC_P (t))
1356 tree r = build_ptrmemfunc_type
1357 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1358 return cp_build_type_variant (r, TYPE_READONLY (t),
1362 /* else fall through */
1364 if (uses_template_parms (t))
1366 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
1367 tree r = lookup_template_class (t, argvec, in_decl);
1368 return cp_build_type_variant (r, TYPE_READONLY (t),
1372 /* else fall through */
1374 case IDENTIFIER_NODE:
1386 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1387 if (ctx == NULL_TREE)
1390 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
1394 if (t == integer_type_node)
1397 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1398 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1402 tree max = tsubst_expr (TYPE_MAX_VALUE (t), args, nargs, in_decl);
1403 if (processing_template_decl)
1405 tree itype = make_node (INTEGER_TYPE);
1406 TYPE_MIN_VALUE (itype) = size_zero_node;
1407 TYPE_MAX_VALUE (itype) = max;
1410 return build_index_2_type (size_zero_node, max);
1413 case TEMPLATE_TYPE_PARM:
1415 tree arg = args[TEMPLATE_TYPE_IDX (t)];
1416 return cp_build_type_variant
1417 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
1418 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
1421 case TEMPLATE_CONST_PARM:
1422 return args[TEMPLATE_CONST_IDX (t)];
1427 tree arg_types, ctx;
1431 if (DECL_CONTEXT (t) != NULL_TREE
1432 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1434 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
1438 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1439 type = tsubst (type, args, nargs, in_decl);
1445 type = tsubst (type, args, nargs, in_decl);
1448 if (type == TREE_TYPE (t)
1449 && (! member || ctx == DECL_CLASS_CONTEXT (t)))
1456 /* Do we already have this instantiation? */
1457 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1459 tree tmpl = TREE_PURPOSE (DECL_TEMPLATE_INFO (t));
1460 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1462 for (; decls; decls = TREE_CHAIN (decls))
1463 if (TREE_TYPE (TREE_VALUE (decls)) == type
1464 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
1465 return TREE_VALUE (decls);
1468 /* We do NOT check for matching decls pushed separately at this
1469 point, as they may not represent instantiations of this
1470 template, and in any case are considered separate under the
1475 TREE_TYPE (r) = type;
1478 = tsubst (DECL_CONTEXT (t), args, nargs, t);
1479 DECL_CLASS_CONTEXT (r) = ctx;
1481 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
1482 IDENTIFIER_POINTER (DECL_NAME (r)),
1483 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1485 /* Type-conversion operator. Reconstruct the name, in
1486 case it's the name of one of the template's parameters. */
1487 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
1490 arg_types = TYPE_VALUES (type);
1492 if (member && TREE_CODE (type) == FUNCTION_TYPE)
1493 arg_types = hash_tree_chain
1494 (build_pointer_type (DECL_CONTEXT (r)), arg_types);
1496 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
1498 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
1499 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
1500 buf = (char *) alloca (strlen (dbuf)
1501 + sizeof (DESTRUCTOR_DECL_PREFIX));
1502 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
1505 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
1508 DECL_ASSEMBLER_NAME (r)
1509 = build_decl_overload (DECL_NAME (r), arg_types, member);
1511 make_decl_rtl (r, NULL_PTR, 1);
1513 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1514 DECL_MAIN_VARIANT (r) = r;
1515 DECL_RESULT (r) = NULL_TREE;
1516 DECL_INITIAL (r) = NULL_TREE;
1518 TREE_STATIC (r) = 0;
1519 TREE_PUBLIC (r) = 1;
1520 DECL_EXTERNAL (r) = 1;
1521 DECL_INTERFACE_KNOWN (r) = 0;
1522 DECL_DEFER_OUTPUT (r) = 0;
1523 TREE_CHAIN (r) = NULL_TREE;
1524 DECL_CHAIN (r) = NULL_TREE;
1526 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
1527 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
1529 /* Look for matching decls for the moment. */
1532 tree decls = lookup_name_nonclass (DECL_NAME (t));
1535 if (decls == NULL_TREE)
1537 else if (is_overloaded_fn (decls))
1538 for (decls = get_first_fn (decls); decls;
1539 decls = DECL_CHAIN (decls))
1541 if (TREE_CODE (decls) == FUNCTION_DECL
1542 && TREE_TYPE (decls) == type)
1551 int dcl_only = ! DECL_INITIAL (d);
1553 DECL_INITIAL (r) = error_mark_node;
1554 duplicate_decls (r, d);
1557 DECL_INITIAL (r) = 0;
1561 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1563 tree tmpl = DECL_TI_TEMPLATE (t);
1564 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1565 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1566 args, nargs, in_decl);
1568 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1569 *declsp = perm_tree_cons (argvec, r, *declsp);
1571 /* If we have a preexisting version of this function, don't expand
1572 the template version, use the other instead. */
1573 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
1574 SET_DECL_TEMPLATE_SPECIALIZATION (r);
1576 SET_DECL_IMPLICIT_INSTANTIATION (r);
1578 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1579 tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1582 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
1583 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
1585 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
1586 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
1593 tree r = copy_node (t);
1594 TREE_TYPE (r) = type;
1595 DECL_INITIAL (r) = TREE_TYPE (r);
1596 DECL_CONTEXT (r) = NULL_TREE;
1597 #ifdef PROMOTE_PROTOTYPES
1598 if ((TREE_CODE (type) == INTEGER_TYPE
1599 || TREE_CODE (type) == ENUMERAL_TYPE)
1600 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
1601 DECL_ARG_TYPE (r) = integer_type_node;
1604 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1610 tree r = copy_node (t);
1611 TREE_TYPE (r) = type;
1614 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
1616 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
1617 TREE_CHAIN (r) = NULL_TREE;
1623 tree r = copy_node (t);
1625 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
1626 TREE_CHAIN (r) = NULL_TREE;
1633 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
1635 /* Do we already have this instantiation? */
1636 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1638 tree tmpl = DECL_TI_TEMPLATE (t);
1639 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1641 for (; decls; decls = TREE_CHAIN (decls))
1642 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
1643 return TREE_VALUE (decls);
1647 TREE_TYPE (r) = type;
1648 DECL_CONTEXT (r) = ctx;
1649 if (TREE_STATIC (r))
1650 DECL_ASSEMBLER_NAME (r)
1651 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
1653 /* Don't try to expand the initializer until someone tries to use
1654 this variable; otherwise we run into circular dependencies. */
1655 DECL_INITIAL (r) = NULL_TREE;
1660 if (DECL_LANG_SPECIFIC (r))
1663 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
1666 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1668 tree tmpl = DECL_TI_TEMPLATE (t);
1669 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1670 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1671 args, nargs, in_decl);
1673 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1674 *declsp = perm_tree_cons (argvec, r, *declsp);
1675 SET_DECL_IMPLICIT_INSTANTIATION (r);
1677 TREE_CHAIN (r) = NULL_TREE;
1683 tree r = copy_node (t);
1684 TREE_TYPE (r) = type;
1685 DECL_CONTEXT (r) = current_class_type;
1686 TREE_CHAIN (r) = NULL_TREE;
1692 tree purpose, value, chain, result;
1693 int via_public, via_virtual, via_protected;
1695 if (t == void_list_node)
1698 via_public = TREE_VIA_PUBLIC (t);
1699 via_protected = TREE_VIA_PROTECTED (t);
1700 via_virtual = TREE_VIA_VIRTUAL (t);
1702 purpose = TREE_PURPOSE (t);
1704 purpose = tsubst (purpose, args, nargs, in_decl);
1705 value = TREE_VALUE (t);
1707 value = tsubst (value, args, nargs, in_decl);
1708 chain = TREE_CHAIN (t);
1709 if (chain && chain != void_type_node)
1710 chain = tsubst (chain, args, nargs, in_decl);
1711 if (purpose == TREE_PURPOSE (t)
1712 && value == TREE_VALUE (t)
1713 && chain == TREE_CHAIN (t))
1715 result = hash_tree_cons (via_public, via_virtual, via_protected,
1716 purpose, value, chain);
1717 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1721 if (type != NULL_TREE)
1725 if (type == TREE_TYPE (t))
1728 TREE_TYPE (t) = complete_type (type);
1729 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
1730 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
1731 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
1732 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
1737 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1738 tree *elts = (tree *) alloca (len * sizeof (tree));
1740 bzero ((char *) elts, len * sizeof (tree));
1742 for (i = 0; i < len; i++)
1744 elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
1745 if (elts[i] != TREE_VEC_ELT (t, i))
1752 t = make_tree_vec (len);
1753 for (i = 0; i < len; i++)
1754 TREE_VEC_ELT (t, i) = elts[i];
1759 case REFERENCE_TYPE:
1762 enum tree_code code;
1763 if (type == TREE_TYPE (t))
1766 code = TREE_CODE (t);
1767 if (code == POINTER_TYPE)
1768 r = build_pointer_type (type);
1770 r = build_reference_type (type);
1771 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
1772 /* Will this ever be needed for TYPE_..._TO values? */
1777 return build_offset_type
1778 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
1782 tree values = TYPE_ARG_TYPES (t);
1783 tree context = TYPE_CONTEXT (t);
1784 tree raises = TYPE_RAISES_EXCEPTIONS (t);
1787 /* Don't bother recursing if we know it won't change anything. */
1788 if (values != void_list_node)
1790 /* This should probably be rewritten to use hash_tree_cons for
1791 the memory savings. */
1792 tree first = NULL_TREE;
1795 for (; values && values != void_list_node;
1796 values = TREE_CHAIN (values))
1799 = tsubst (TREE_VALUE (values), args, nargs, in_decl);
1800 tree purpose = tsubst_expr (TREE_PURPOSE (values),
1801 args, nargs, in_decl);
1802 tree x = build_tree_list (purpose, value);
1805 TREE_CHAIN (last) = x;
1811 if (values == void_list_node)
1812 TREE_CHAIN (last) = void_list_node;
1817 context = tsubst (context, args, nargs, in_decl);
1818 /* Could also optimize cases where return value and
1819 values have common elements (e.g., T min(const &T, const T&). */
1821 /* If the above parameters haven't changed, just return the type. */
1822 if (type == TREE_TYPE (t)
1823 && values == TYPE_VALUES (t)
1824 && context == TYPE_CONTEXT (t))
1827 /* Construct a new type node and return it. */
1828 if (TREE_CODE (t) == FUNCTION_TYPE
1829 && context == NULL_TREE)
1831 fntype = build_function_type (type, values);
1833 else if (context == NULL_TREE)
1835 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1836 args, nargs, in_decl);
1837 fntype = build_cplus_method_type (base, type,
1838 TREE_CHAIN (values));
1842 fntype = make_node (TREE_CODE (t));
1843 TREE_TYPE (fntype) = type;
1844 TYPE_CONTEXT (fntype) = context;
1845 TYPE_VALUES (fntype) = values;
1846 TYPE_SIZE (fntype) = TYPE_SIZE (t);
1847 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
1848 TYPE_MODE (fntype) = TYPE_MODE (t);
1849 if (TYPE_METHOD_BASETYPE (t))
1850 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
1851 args, nargs, in_decl);
1852 /* Need to generate hash value. */
1853 my_friendly_abort (84);
1855 fntype = build_type_variant (fntype,
1860 raises = tsubst (raises, args, nargs, in_decl);
1861 fntype = build_exception_variant (fntype, raises);
1867 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1869 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1871 r = build_cplus_array_type (type, domain);
1877 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1878 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1879 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1883 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1884 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1888 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1889 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
1890 return cp_build_type_variant
1891 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
1892 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
1896 return make_pointer_declarator
1897 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1900 return make_reference_declarator
1901 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1904 return build_parse_node
1905 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1906 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
1909 return make_call_declarator
1910 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1911 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
1912 TREE_OPERAND (t, 2),
1913 tsubst (TREE_TYPE (t), args, nargs, in_decl));
1916 return build_parse_node
1917 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1918 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
1921 sorry ("use of `%s' in template",
1922 tree_code_name [(int) TREE_CODE (t)]);
1923 return error_mark_node;
1930 emit_line_note (input_filename, lineno);
1934 expand_start_bindings (0);
1942 expand_end_bindings (getdecls (), kept_level_p (), 1);
1943 t = poplevel (kept_level_p (), 1, 0);
1949 tsubst_copy (t, args, nargs, in_decl)
1954 enum tree_code code;
1956 if (t == NULL_TREE || t == error_mark_node)
1959 code = TREE_CODE (t);
1964 return do_identifier (DECL_NAME (t), 0);
1968 if (DECL_CONTEXT (t))
1970 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
1971 if (ctx != DECL_CONTEXT (t))
1972 return lookup_field (ctx, DECL_NAME (t), 0, 0);
1978 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1979 t = tsubst (t, args, nargs, in_decl);
1984 case IDENTIFIER_NODE:
1985 return do_identifier (t, 0);
1989 case REINTERPRET_CAST_EXPR:
1990 case CONST_CAST_EXPR:
1991 case STATIC_CAST_EXPR:
1992 case DYNAMIC_CAST_EXPR:
1994 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
1995 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
1998 case PREDECREMENT_EXPR:
1999 case PREINCREMENT_EXPR:
2000 case POSTDECREMENT_EXPR:
2001 case POSTINCREMENT_EXPR:
2003 case TRUTH_NOT_EXPR:
2005 case CONVERT_EXPR: /* Unary + */
2012 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2017 case TRUNC_DIV_EXPR:
2019 case FLOOR_DIV_EXPR:
2020 case ROUND_DIV_EXPR:
2021 case EXACT_DIV_EXPR:
2023 case BIT_ANDTC_EXPR:
2026 case TRUNC_MOD_EXPR:
2027 case FLOOR_MOD_EXPR:
2028 case TRUTH_ANDIF_EXPR:
2029 case TRUTH_ORIF_EXPR:
2030 case TRUTH_AND_EXPR:
2051 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2052 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2056 tree fn = TREE_OPERAND (t, 0);
2057 if (really_overloaded_fn (fn))
2058 fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
2060 fn = tsubst_copy (fn, args, nargs, in_decl);
2062 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2066 case METHOD_CALL_EXPR:
2068 tree name = TREE_OPERAND (t, 0);
2069 if (TREE_CODE (name) == BIT_NOT_EXPR)
2071 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2072 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2074 else if (TREE_CODE (name) == SCOPE_REF
2075 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
2077 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2078 name = TREE_OPERAND (name, 1);
2079 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2080 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2081 name = build_nt (SCOPE_REF, base, name);
2084 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
2086 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2087 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
2094 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2095 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2096 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
2101 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2102 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2103 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
2104 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
2111 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2112 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2113 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
2114 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
2120 tree purpose, value, chain;
2122 if (t == void_list_node)
2125 purpose = TREE_PURPOSE (t);
2127 purpose = tsubst_copy (purpose, args, nargs, in_decl);
2128 value = TREE_VALUE (t);
2130 value = tsubst_copy (value, args, nargs, in_decl);
2131 chain = TREE_CHAIN (t);
2132 if (chain && chain != void_type_node)
2133 chain = tsubst_copy (chain, args, nargs, in_decl);
2134 if (purpose == TREE_PURPOSE (t)
2135 && value == TREE_VALUE (t)
2136 && chain == TREE_CHAIN (t))
2138 return tree_cons (purpose, value, chain);
2145 case TEMPLATE_TYPE_PARM:
2146 case TEMPLATE_CONST_PARM:
2148 case REFERENCE_TYPE:
2154 return tsubst (t, args, nargs, in_decl);
2156 case IDENTIFIER_NODE:
2157 if (IDENTIFIER_TYPENAME_P (t))
2158 return build_typename_overload
2159 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
2165 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
2166 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
2174 tsubst_expr (t, args, nargs, in_decl)
2179 if (t == NULL_TREE || t == error_mark_node)
2182 if (processing_template_decl)
2183 return tsubst_copy (t, args, nargs, in_decl);
2185 switch (TREE_CODE (t))
2188 lineno = TREE_COMPLEXITY (t);
2189 emit_line_note (input_filename, lineno);
2191 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2196 lineno = TREE_COMPLEXITY (t);
2197 emit_line_note (input_filename, lineno);
2198 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2199 /* Do default conversion if safe and possibly important,
2200 in case within ({...}). */
2201 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
2202 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2203 t = default_conversion (t);
2204 cplus_expand_expr_stmt (t);
2211 int i = suspend_momentary ();
2214 lineno = TREE_COMPLEXITY (t);
2215 emit_line_note (input_filename, lineno);
2217 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2218 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2219 TREE_OPERAND (t, 2) != 0);
2220 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2222 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
2223 resume_momentary (i);
2230 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
2231 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2232 int cond_scope = (TREE_OPERAND (t, 1)
2233 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
2235 lineno = TREE_COMPLEXITY (t);
2236 emit_line_note (input_filename, lineno);
2239 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
2240 tsubst_expr (tmp, args, nargs, in_decl);
2242 emit_line_note (input_filename, lineno);
2243 expand_start_loop_continue_elsewhere (1);
2247 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2248 emit_line_note (input_filename, lineno);
2250 expand_exit_loop_if_false (0, condition_conversion (tmp));
2254 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
2257 emit_line_note (input_filename, lineno);
2258 expand_loop_continue_here ();
2259 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2261 cplus_expand_expr_stmt (tmp);
2274 lineno = TREE_COMPLEXITY (t);
2276 emit_line_note (input_filename, lineno);
2277 expand_start_loop (1);
2279 cond = TREE_OPERAND (t, 0);
2280 if (TREE_CODE (cond) == DECL_STMT)
2282 cond = tsubst_expr (cond, args, nargs, in_decl);
2283 emit_line_note (input_filename, lineno);
2284 expand_exit_loop_if_false (0, condition_conversion (cond));
2286 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
2288 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2300 lineno = TREE_COMPLEXITY (t);
2302 emit_line_note (input_filename, lineno);
2303 expand_start_loop_continue_elsewhere (1);
2305 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2306 expand_loop_continue_here ();
2308 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2309 emit_line_note (input_filename, lineno);
2310 expand_exit_loop_if_false (0, condition_conversion (cond));
2321 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2323 lineno = TREE_COMPLEXITY (t);
2326 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2327 emit_line_note (input_filename, lineno);
2328 expand_start_cond (condition_conversion (tmp), 0);
2330 if (tmp = TREE_OPERAND (t, 1), tmp)
2331 tsubst_expr (tmp, args, nargs, in_decl);
2333 if (tmp = TREE_OPERAND (t, 2), tmp)
2335 expand_start_else ();
2336 tsubst_expr (tmp, args, nargs, in_decl);
2350 tree substmt = TREE_OPERAND (t, 0);
2352 lineno = TREE_COMPLEXITY (t);
2354 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2357 for (; substmt; substmt = TREE_CHAIN (substmt))
2358 tsubst_expr (substmt, args, nargs, in_decl);
2360 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2366 lineno = TREE_COMPLEXITY (t);
2367 emit_line_note (input_filename, lineno);
2368 if (! expand_exit_something ())
2369 error ("break statement not within loop or switch");
2373 lineno = TREE_COMPLEXITY (t);
2374 emit_line_note (input_filename, lineno);
2375 if (! expand_continue_loop (0))
2376 error ("continue statement not within a loop");
2382 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2384 lineno = TREE_COMPLEXITY (t);
2387 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2388 emit_line_note (input_filename, lineno);
2389 c_expand_start_case (val);
2392 if (tmp = TREE_OPERAND (t, 1), tmp)
2393 tsubst_expr (tmp, args, nargs, in_decl);
2395 expand_end_case (val);
2406 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
2407 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2411 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
2418 lineno = TREE_COMPLEXITY (t);
2419 emit_line_note (input_filename, lineno);
2420 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
2422 tree decl = lookup_label (TREE_OPERAND (t, 0));
2423 TREE_USED (decl) = 1;
2427 expand_computed_goto
2428 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2432 lineno = TREE_COMPLEXITY (t);
2433 emit_line_note (input_filename, lineno);
2434 expand_start_try_stmts ();
2435 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2436 expand_start_all_catch ();
2438 tree handler = TREE_OPERAND (t, 1);
2439 for (; handler; handler = TREE_CHAIN (handler))
2440 tsubst_expr (handler, args, nargs, in_decl);
2442 expand_end_all_catch ();
2446 lineno = TREE_COMPLEXITY (t);
2448 if (TREE_OPERAND (t, 0))
2450 tree d = TREE_OPERAND (t, 0);
2451 expand_start_catch_block
2452 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
2453 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
2456 expand_start_catch_block (NULL_TREE, NULL_TREE);
2457 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2458 expand_end_catch_block ();
2463 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
2469 instantiate_template (tmpl, targ_ptr)
2470 tree tmpl, *targ_ptr;
2474 struct obstack *old_fmp_obstack;
2475 extern struct obstack *function_maybepermanent_obstack;
2477 push_obstacks (&permanent_obstack, &permanent_obstack);
2478 old_fmp_obstack = function_maybepermanent_obstack;
2479 function_maybepermanent_obstack = &permanent_obstack;
2481 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
2482 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
2487 tree t = targ_ptr [i];
2488 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2490 tree nt = target_type (t);
2491 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
2493 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
2494 cp_error (" trying to instantiate `%D'", tmpl);
2495 fndecl = error_mark_node;
2499 targ_ptr[i] = copy_to_permanent (t);
2502 /* substitute template parameters */
2503 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
2506 function_maybepermanent_obstack = old_fmp_obstack;
2512 /* Push the name of the class template into the scope of the instantiation. */
2515 overload_template_name (type)
2518 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
2521 if (IDENTIFIER_CLASS_VALUE (id)
2522 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
2525 decl = build_decl (TYPE_DECL, id, type);
2526 SET_DECL_ARTIFICIAL (decl);
2527 pushdecl_class_level (decl);
2530 /* Type unification.
2532 We have a function template signature with one or more references to
2533 template parameters, and a parameter list we wish to fit to this
2534 template. If possible, produce a list of parameters for the template
2535 which will cause it to fit the supplied parameter list.
2537 Return zero for success, 2 for an incomplete match that doesn't resolve
2538 all the types, and 1 for complete failure. An error message will be
2539 printed only for an incomplete match.
2541 TPARMS[NTPARMS] is an array of template parameter types;
2542 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2543 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2544 and ARGS is the argument list we're trying to match against it.
2546 If SUBR is 1, we're being called recursively (to unify the arguments of
2547 a function or method parameter of a function template), so don't zero
2548 out targs and don't fail on an incomplete match.
2550 If STRICT is 1, the match must be exact (for casts of overloaded
2551 addresses, explicit instantiation, and more_specialized). */
2554 type_unification (tparms, targs, parms, args, nsubsts, subr, strict)
2555 tree tparms, *targs, parms, args;
2556 int *nsubsts, subr, strict;
2560 int ntparms = TREE_VEC_LENGTH (tparms);
2562 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
2563 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
2564 /* ARGS could be NULL (via a call from parse.y to
2565 build_x_function_call). */
2567 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
2568 my_friendly_assert (ntparms > 0, 292);
2571 bzero ((char *) targs, sizeof (tree) * ntparms);
2574 && parms != void_list_node
2576 && args != void_list_node)
2578 parm = TREE_VALUE (parms);
2579 parms = TREE_CHAIN (parms);
2580 arg = TREE_VALUE (args);
2581 args = TREE_CHAIN (args);
2583 if (arg == error_mark_node)
2585 if (arg == unknown_type_node)
2588 if (! uses_template_parms (parm)
2589 && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2591 if (can_convert_arg (parm, TREE_TYPE (arg), arg))
2597 if (TREE_CODE (arg) == VAR_DECL)
2598 arg = TREE_TYPE (arg);
2599 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
2600 arg = TREE_TYPE (arg);
2602 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2604 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
2605 if (TREE_CODE (arg) == TREE_LIST
2606 && TREE_TYPE (arg) == unknown_type_node
2607 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
2609 int nsubsts, ntparms;
2612 /* Have to back unify here */
2613 arg = TREE_VALUE (arg);
2615 ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
2616 targs = (tree *) alloca (sizeof (tree) * ntparms);
2617 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
2618 return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
2619 TYPE_ARG_TYPES (TREE_TYPE (arg)),
2620 parm, &nsubsts, 0, strict);
2622 arg = TREE_TYPE (arg);
2625 if (TREE_CODE (arg) == REFERENCE_TYPE)
2626 arg = TREE_TYPE (arg);
2628 if (TREE_CODE (parm) != REFERENCE_TYPE)
2630 if (TREE_CODE (arg) == FUNCTION_TYPE
2631 || TREE_CODE (arg) == METHOD_TYPE)
2632 arg = build_pointer_type (arg);
2633 else if (TREE_CODE (arg) == ARRAY_TYPE)
2634 arg = build_pointer_type (TREE_TYPE (arg));
2636 arg = TYPE_MAIN_VARIANT (arg);
2639 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
2647 /* Fail if we've reached the end of the parm list, and more args
2648 are present, and the parm list isn't variadic. */
2649 if (args && args != void_list_node && parms == void_list_node)
2651 /* Fail if parms are left and they don't have default values. */
2653 && parms != void_list_node
2654 && TREE_PURPOSE (parms) == NULL_TREE)
2657 for (i = 0; i < ntparms; i++)
2660 error ("incomplete type unification");
2666 /* Tail recursion is your friend. */
2669 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
2670 tree tparms, *targs, parm, arg;
2671 int *nsubsts, ntparms, strict;
2675 /* I don't think this will do the right thing with respect to types.
2676 But the only case I've seen it in so far has been array bounds, where
2677 signedness is the only information lost, and I think that will be
2679 while (TREE_CODE (parm) == NOP_EXPR)
2680 parm = TREE_OPERAND (parm, 0);
2682 if (arg == error_mark_node)
2684 if (arg == unknown_type_node)
2689 switch (TREE_CODE (parm))
2691 case TEMPLATE_TYPE_PARM:
2693 idx = TEMPLATE_TYPE_IDX (parm);
2694 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
2695 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
2698 /* Template type parameters cannot contain cv-quals; i.e.
2699 template <class T> void f (T& a, T& b) will not generate
2700 void f (const int& a, const int& b). */
2701 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
2702 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
2704 arg = TYPE_MAIN_VARIANT (arg);
2707 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
2708 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
2709 arg = cp_build_type_variant (arg, constp, volatilep);
2712 /* Simple cases: Value already set, does match or doesn't. */
2713 if (targs[idx] == arg)
2715 else if (targs[idx])
2717 /* Check for mixed types and values. */
2718 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
2722 case TEMPLATE_CONST_PARM:
2724 idx = TEMPLATE_CONST_IDX (parm);
2725 if (targs[idx] == arg)
2727 else if (targs[idx])
2729 tree t = targs[idx];
2730 if (TREE_CODE (t) == TREE_CODE (arg))
2731 switch (TREE_CODE (arg))
2734 if (tree_int_cst_equal (t, arg))
2738 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2741 /* STRING_CST values are not valid template const parms. */
2745 my_friendly_abort (87);
2748 /* else if (typeof arg != tparms[idx])
2751 targs[idx] = copy_to_permanent (arg);
2755 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
2756 return unify (tparms, targs, ntparms, parm,
2757 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
2759 if (TREE_CODE (arg) != POINTER_TYPE)
2761 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2764 case REFERENCE_TYPE:
2765 if (TREE_CODE (arg) == REFERENCE_TYPE)
2766 arg = TREE_TYPE (arg);
2767 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
2771 if (TREE_CODE (arg) != ARRAY_TYPE)
2773 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2774 nsubsts, strict) != 0)
2776 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2781 if (TREE_CODE (arg) != TREE_CODE (parm))
2784 if (TREE_CODE (parm) == INTEGER_TYPE)
2786 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2787 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
2788 TYPE_MIN_VALUE (arg), nsubsts, strict))
2790 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2791 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
2792 TYPE_MAX_VALUE (arg), nsubsts, strict))
2795 /* As far as unification is concerned, this wins. Later checks
2796 will invalidate it if necessary. */
2799 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2801 if (TREE_CODE (arg) != INTEGER_CST)
2803 return !tree_int_cst_equal (parm, arg);
2808 t1 = TREE_OPERAND (parm, 0);
2809 t2 = TREE_OPERAND (parm, 1);
2810 return unify (tparms, targs, ntparms, t1,
2811 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2818 if (TREE_CODE (arg) != TREE_VEC)
2820 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2822 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2823 if (unify (tparms, targs, ntparms,
2824 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2831 if (TYPE_PTRMEMFUNC_FLAG (parm))
2832 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2833 arg, nsubsts, strict);
2835 /* Allow trivial conversions. */
2836 if (TREE_CODE (arg) != RECORD_TYPE
2837 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2838 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2841 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
2844 if (flag_ansi_overloading && ! strict)
2845 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
2847 (CLASSTYPE_TEMPLATE_INFO (arg)
2848 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
2850 if (! t || t == error_mark_node)
2853 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
2854 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
2856 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
2861 if (TREE_CODE (arg) != METHOD_TYPE)
2866 if (TREE_CODE (arg) != FUNCTION_TYPE)
2869 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
2870 TREE_TYPE (arg), nsubsts, strict))
2872 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2873 TYPE_ARG_TYPES (arg), nsubsts, 1, strict);
2876 if (TREE_CODE (arg) != OFFSET_TYPE)
2878 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2879 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
2881 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2882 TREE_TYPE (arg), nsubsts, strict);
2885 sorry ("use of `%s' in template type unification",
2886 tree_code_name [(int) TREE_CODE (parm)]);
2892 mark_decl_instantiated (result, extern_p)
2896 if (DECL_TEMPLATE_INSTANTIATION (result))
2897 SET_DECL_EXPLICIT_INSTANTIATION (result);
2898 TREE_PUBLIC (result) = 1;
2902 DECL_INTERFACE_KNOWN (result) = 1;
2903 DECL_NOT_REALLY_EXTERN (result) = 1;
2905 else if (TREE_CODE (result) == FUNCTION_DECL)
2906 mark_inline_for_output (result);
2909 /* Given two function templates PAT1 and PAT2, return:
2911 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
2912 -1 if PAT2 is more specialized than PAT1.
2913 0 if neither is more specialized. */
2916 more_specialized (pat1, pat2)
2922 targs = get_bindings (pat1, pat2);
2929 targs = get_bindings (pat2, pat1);
2939 /* Given two class template specialization list nodes PAT1 and PAT2, return:
2941 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
2942 -1 if PAT2 is more specialized than PAT1.
2943 0 if neither is more specialized. */
2946 more_specialized_class (pat1, pat2)
2952 targs = get_class_bindings
2953 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
2957 targs = get_class_bindings
2958 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
2965 /* Return the template arguments that will produce the function signature
2966 DECL from the function template FN. */
2969 get_bindings (fn, decl)
2972 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2973 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
2975 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2976 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2977 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2986 get_class_bindings (tparms, parms, args)
2987 tree tparms, parms, args;
2989 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
2990 tree vec = make_temp_vec (ntparms);
2992 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
2994 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
2995 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
3005 for (i = 0; i < ntparms; ++i)
3006 if (! TREE_VEC_ELT (vec, i))
3012 /* Return the most specialized of the list of templates in FNS that can
3013 produce an instantiation matching DECL. */
3016 most_specialized (fns, decl)
3019 tree fn, champ, *args, *p;
3022 for (p = &fns; *p; )
3024 args = get_bindings (TREE_VALUE (*p), decl);
3028 p = &TREE_CHAIN (*p);
3031 *p = TREE_CHAIN (*p);
3038 champ = TREE_VALUE (fn);
3039 fn = TREE_CHAIN (fn);
3040 for (; fn; fn = TREE_CHAIN (fn))
3042 fate = more_specialized (champ, TREE_VALUE (fn));
3049 fn = TREE_CHAIN (fn);
3051 return error_mark_node;
3053 champ = TREE_VALUE (fn);
3057 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
3059 fate = more_specialized (champ, TREE_VALUE (fn));
3061 return error_mark_node;
3067 /* Return the most specialized of the class template specializations in
3068 SPECS that can produce an instantiation matching ARGS. */
3071 most_specialized_class (specs, mainargs)
3072 tree specs, mainargs;
3074 tree list = NULL_TREE, t, args, champ;
3077 for (t = specs; t; t = TREE_CHAIN (t))
3079 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
3082 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
3083 TREE_TYPE (list) = TREE_TYPE (t);
3093 for (; t; t = TREE_CHAIN (t))
3095 fate = more_specialized_class (champ, t);
3104 return error_mark_node;
3110 for (t = list; t && t != champ; t = TREE_CHAIN (t))
3112 fate = more_specialized (champ, t);
3114 return error_mark_node;
3120 /* called from the parser. */
3123 do_function_instantiation (declspecs, declarator, storage)
3124 tree declspecs, declarator, storage;
3126 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
3129 tree result = NULL_TREE;
3132 if (! DECL_LANG_SPECIFIC (decl))
3134 cp_error ("explicit instantiation of non-template `%#D'", decl);
3138 /* If we've already seen this template instance, use it. */
3139 if (DECL_FUNCTION_MEMBER_P (decl))
3141 if (DECL_TEMPLATE_INSTANTIATION (decl))
3143 else if (name = DECL_ASSEMBLER_NAME (decl),
3144 fn = IDENTIFIER_GLOBAL_VALUE (name),
3145 fn && DECL_TEMPLATE_INSTANTIATION (fn))
3148 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
3150 tree templates = NULL_TREE;
3151 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
3152 if (decls_match (fn, decl)
3153 && DECL_DEFER_OUTPUT (fn))
3158 else if (TREE_CODE (fn) == TEMPLATE_DECL)
3159 templates = decl_tree_cons (NULL_TREE, fn, templates);
3164 result = most_specialized (templates, decl);
3165 if (result == error_mark_node)
3167 char *str = "candidates are:";
3168 cp_error ("ambiguous template instantiation for `%D' requested", decl);
3169 for (fn = templates; fn; fn = TREE_CHAIN (fn))
3171 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
3178 args = get_bindings (result, decl);
3179 result = instantiate_template (result, args);
3186 cp_error ("no matching template for `%D' found", decl);
3190 if (flag_external_templates)
3193 if (storage == NULL_TREE)
3195 else if (storage == ridpointers[(int) RID_EXTERN])
3198 cp_error ("storage class `%D' applied to template instantiation",
3201 mark_decl_instantiated (result, extern_p);
3202 repo_template_instantiated (result, extern_p);
3204 instantiate_decl (result);
3208 mark_class_instantiated (t, extern_p)
3212 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
3213 SET_CLASSTYPE_INTERFACE_KNOWN (t);
3214 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
3215 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
3216 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
3219 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
3220 rest_of_type_compilation (t, 1);
3225 do_type_instantiation (name, storage)
3228 tree t = TREE_TYPE (name);
3235 /* With -fexternal-templates, explicit instantiations are treated the same
3236 as implicit ones. */
3237 if (flag_external_templates)
3240 if (TYPE_SIZE (t) == NULL_TREE)
3242 cp_error ("explicit instantiation of `%#T' before definition of template",
3247 if (storage == NULL_TREE)
3249 else if (storage == ridpointers[(int) RID_INLINE])
3251 else if (storage == ridpointers[(int) RID_EXTERN])
3253 else if (storage == ridpointers[(int) RID_STATIC])
3257 cp_error ("storage class `%D' applied to template instantiation",
3262 /* We've already instantiated this. */
3263 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
3267 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3269 mark_class_instantiated (t, extern_p);
3270 repo_template_instantiated (t, extern_p);
3280 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
3281 if (DECL_TEMPLATE_INSTANTIATION (tmp))
3283 mark_decl_instantiated (tmp, extern_p);
3284 repo_template_instantiated (tmp, extern_p);
3286 instantiate_decl (tmp);
3289 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
3290 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
3292 mark_decl_instantiated (tmp, extern_p);
3293 repo_template_instantiated (tmp, extern_p);
3295 instantiate_decl (tmp);
3298 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
3299 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
3300 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
3305 instantiate_decl (d)
3308 tree ti = DECL_TEMPLATE_INFO (d);
3309 tree tmpl = TI_TEMPLATE (ti);
3310 tree args = TI_ARGS (ti);
3312 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
3314 int nested = in_function_p ();
3316 int pattern_defined;
3318 char *file = input_filename;
3320 if (TREE_CODE (d) == FUNCTION_DECL)
3322 d_defined = (DECL_INITIAL (d) != NULL_TREE);
3323 pattern_defined = (DECL_INITIAL (pattern) != NULL_TREE);
3327 d_defined = ! DECL_IN_AGGR_P (d);
3328 pattern_defined = ! DECL_IN_AGGR_P (pattern);
3333 else if (pattern_defined)
3335 repo_template_used (d);
3337 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
3339 if (flag_alt_external_templates)
3341 if (interface_unknown)
3342 warn_if_unknown_interface (d);
3344 else if (DECL_INTERFACE_KNOWN (pattern))
3346 DECL_INTERFACE_KNOWN (d) = 1;
3347 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (pattern);
3350 warn_if_unknown_interface (pattern);
3354 import_export_decl (d);
3357 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
3358 variable is a static const initialized in the class body. */
3359 if (TREE_CODE (d) == VAR_DECL
3360 && ! DECL_INITIAL (d) && DECL_INITIAL (pattern))
3362 lineno = DECL_SOURCE_LINE (d);
3363 input_filename = DECL_SOURCE_FILE (d);
3365 pushclass (DECL_CONTEXT (d), 2);
3366 DECL_INITIAL (d) = tsubst_expr
3367 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3368 TREE_VEC_LENGTH (args), tmpl);
3372 input_filename = file;
3375 if (! pattern_defined
3376 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
3377 && (! DECL_INTERFACE_KNOWN (d)
3378 || ! DECL_NOT_REALLY_EXTERN (d)))
3379 /* Kludge: if we compile a constructor in the middle of processing a
3380 toplevel declaration, we blow away the declspecs in
3381 temp_decl_obstack when we call permanent_allocation in
3382 finish_function. So don't compile it yet. */
3383 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
3385 add_pending_template (d);
3389 if (! push_tinst_level (d))
3392 push_to_top_level ();
3394 lineno = DECL_SOURCE_LINE (d);
3395 input_filename = DECL_SOURCE_FILE (d);
3397 /* Trick tsubst into giving us a new decl in case the template changed. */
3398 save_ti = DECL_TEMPLATE_INFO (pattern);
3399 DECL_TEMPLATE_INFO (pattern) = NULL_TREE;
3400 td = tsubst (pattern, &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), tmpl);
3401 DECL_TEMPLATE_INFO (pattern) = save_ti;
3403 /* And set up DECL_INITIAL, since tsubst doesn't. */
3404 if (TREE_CODE (td) == VAR_DECL)
3406 pushclass (DECL_CONTEXT (d), 2);
3407 DECL_INITIAL (td) = tsubst_expr
3408 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3409 TREE_VEC_LENGTH (args), tmpl);
3413 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
3414 if (TREE_CODE (d) == FUNCTION_DECL)
3415 DECL_INITIAL (td) = error_mark_node;
3416 duplicate_decls (td, d);
3417 if (TREE_CODE (d) == FUNCTION_DECL)
3418 DECL_INITIAL (td) = 0;
3420 if (TREE_CODE (d) == VAR_DECL)
3422 DECL_IN_AGGR_P (d) = 0;
3423 if (DECL_INTERFACE_KNOWN (d))
3424 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
3427 DECL_EXTERNAL (d) = 1;
3428 DECL_NOT_REALLY_EXTERN (d) = 1;
3430 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
3432 else if (TREE_CODE (d) == FUNCTION_DECL)
3434 tree t = DECL_SAVED_TREE (pattern);
3436 start_function (NULL_TREE, d, NULL_TREE, 1);
3437 store_parm_decls ();
3439 if (t && TREE_CODE (t) == RETURN_INIT)
3442 (TREE_OPERAND (t, 0),
3443 tsubst_expr (TREE_OPERAND (t, 1), &TREE_VEC_ELT (args, 0),
3444 TREE_VEC_LENGTH (args), tmpl));
3448 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
3450 current_member_init_list
3451 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
3452 current_base_init_list
3453 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
3458 /* Always keep the BLOCK node associated with the outermost
3459 pair of curley braces of a function. These are needed
3460 for correct operation of dwarfout.c. */
3463 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
3464 tsubst_expr (t, &TREE_VEC_ELT (args, 0),
3465 TREE_VEC_LENGTH (args), tmpl);
3467 finish_function (lineno, 0, nested);
3471 input_filename = file;
3473 pop_from_top_level ();
3480 tsubst_chain (t, argvec)
3485 tree first = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3486 TREE_VEC_LENGTH (argvec), NULL_TREE);
3489 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
3491 tree x = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3492 TREE_VEC_LENGTH (argvec), NULL_TREE);
3493 TREE_CHAIN (last) = x;
3503 tsubst_expr_values (t, argvec)
3506 tree first = NULL_TREE;
3509 for (; t; t = TREE_CHAIN (t))
3511 tree pur = tsubst_copy (TREE_PURPOSE (t), &TREE_VEC_ELT (argvec, 0),
3512 TREE_VEC_LENGTH (argvec), NULL_TREE);
3513 tree val = tsubst_expr (TREE_VALUE (t), &TREE_VEC_ELT (argvec, 0),
3514 TREE_VEC_LENGTH (argvec), NULL_TREE);
3515 *p = build_tree_list (pur, val);
3516 p = &TREE_CHAIN (*p);
3527 last_tree = TREE_CHAIN (last_tree) = t;
3530 /* D is an undefined function declaration in the presence of templates with
3531 the same name, listed in FNS. If one of them can produce D as an
3532 instantiation, remember this so we can instantiate it at EOF if D has
3533 not been defined by that time. */
3536 add_maybe_template (d, fns)
3541 if (DECL_MAYBE_TEMPLATE (d))
3544 t = most_specialized (fns, d);
3547 if (t == error_mark_node)
3549 cp_error ("ambiguous template instantiation for `%D'", d);
3553 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
3554 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
3555 DECL_MAYBE_TEMPLATE (d) = 1;