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_NAME (t) = decl;
153 TYPE_STUB_DECL (t) = decl;
155 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
157 SET_DECL_ARTIFICIAL (decl);
159 parm = build_tree_list (defval, parm);
160 return chainon (list, parm);
163 /* The end of a template parameter list has been reached. Process the
164 tree list into a parameter vector, converting each parameter into a more
165 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
169 end_template_parm_list (parms)
174 tree saved_parmlist = make_tree_vec (list_length (parms));
176 current_template_parms
177 = tree_cons (build_int_2 (0, processing_template_decl),
178 saved_parmlist, current_template_parms);
180 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
181 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
183 return saved_parmlist;
186 /* end_template_decl is called after a template declaration is seen. */
191 if (! processing_template_decl)
194 /* This matches the pushlevel in begin_template_parm_list. */
197 --processing_template_decl;
198 current_template_parms = TREE_CHAIN (current_template_parms);
199 (void) get_pending_sizes (); /* Why? */
202 /* Generate a valid set of template args from current_template_parms. */
205 current_template_args ()
207 tree header = current_template_parms;
208 tree args = NULL_TREE;
211 tree a = copy_node (TREE_VALUE (header));
212 int i = TREE_VEC_LENGTH (a);
213 TREE_TYPE (a) = NULL_TREE;
216 tree t = TREE_VALUE (TREE_VEC_ELT (a, i));
217 if (TREE_CODE (t) == TYPE_DECL)
220 t = DECL_INITIAL (t);
221 TREE_VEC_ELT (a, i) = t;
223 args = tree_cons (TREE_PURPOSE (header), a, args);
224 header = TREE_CHAIN (header);
226 args = nreverse (args);
228 /* FIXME Remove this when we support member templates. */
229 args = TREE_VALUE (args);
235 push_template_decl (decl)
239 tree args = NULL_TREE;
241 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
245 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
246 && DECL_CLASS_CONTEXT (decl))
248 /* Note that this template is a "primary template" */
249 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
250 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
253 /* Partial specialization. */
254 if (TREE_CODE (decl) == TYPE_DECL
255 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
257 tree type = TREE_TYPE (decl);
258 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
259 tree mainargs = CLASSTYPE_TI_ARGS (type);
260 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
262 for (; spec; spec = TREE_CHAIN (spec))
264 /* purpose: args to main template
265 value: spec template */
266 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
270 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = perm_tree_cons
271 (mainargs, TREE_VALUE (current_template_parms),
272 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
273 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
277 args = current_template_args ();
279 if (! ctx || TYPE_BEING_DEFINED (ctx))
281 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
282 DECL_TEMPLATE_PARMS (tmpl) = TREE_VALUE (current_template_parms);
283 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
287 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
288 cp_error ("must specialize `%#T' before defining member `%#D'",
290 if (TREE_CODE (decl) == TYPE_DECL)
291 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
292 else if (! DECL_TEMPLATE_INFO (decl))
294 cp_error ("template definition of non-template `%#D'", decl);
298 tmpl = DECL_TI_TEMPLATE (decl);
301 DECL_TEMPLATE_RESULT (tmpl) = decl;
302 TREE_TYPE (tmpl) = TREE_TYPE (decl);
305 tmpl = pushdecl_top_level (tmpl);
308 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl)) = tmpl;
310 info = perm_tree_cons (tmpl, args, NULL_TREE);
312 if (TREE_CODE (decl) == TYPE_DECL)
314 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
315 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
317 else if (! DECL_LANG_SPECIFIC (decl))
318 cp_error ("template declaration of `%#D'", decl);
320 DECL_TEMPLATE_INFO (decl) = info;
323 tree tsubst PROTO ((tree, tree*, int, tree));
325 /* Convert all template arguments to their appropriate types, and return
326 a vector containing the resulting values. If any error occurs, return
330 coerce_template_parms (parms, arglist, in_decl)
334 int nparms, nargs, i, lost = 0;
337 if (arglist == NULL_TREE)
339 else if (TREE_CODE (arglist) == TREE_VEC)
340 nargs = TREE_VEC_LENGTH (arglist);
342 nargs = list_length (arglist);
344 nparms = TREE_VEC_LENGTH (parms);
348 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
350 error ("incorrect number of parameters (%d, should be %d)",
353 cp_error_at ("in template expansion for decl `%D'", in_decl);
354 return error_mark_node;
357 if (arglist && TREE_CODE (arglist) == TREE_VEC)
358 vec = copy_node (arglist);
361 vec = make_tree_vec (nparms);
362 for (i = 0; i < nparms; i++)
369 arglist = TREE_CHAIN (arglist);
371 if (arg == error_mark_node)
374 arg = TREE_VALUE (arg);
376 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i)))
378 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
379 &TREE_VEC_ELT (vec, 0), i, in_decl);
381 arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
382 &TREE_VEC_ELT (vec, 0), i, in_decl);
384 TREE_VEC_ELT (vec, i) = arg;
387 for (i = 0; i < nparms; i++)
389 tree arg = TREE_VEC_ELT (vec, i);
390 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
392 int is_type, requires_type;
394 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
395 requires_type = TREE_CODE (parm) == TYPE_DECL;
397 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
398 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
400 cp_pedwarn ("to refer to a type member of a template parameter,");
401 cp_pedwarn (" use `typename %E'", arg);
402 arg = make_typename_type (TREE_OPERAND (arg, 0),
403 TREE_OPERAND (arg, 1));
406 if (is_type != requires_type)
410 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
413 cp_error (" expected a constant of type `%T', got `%T'",
414 TREE_TYPE (parm), arg);
416 cp_error (" expected a type, got `%E'", arg);
419 TREE_VEC_ELT (vec, i) = error_mark_node;
424 val = groktypename (arg);
425 if (! processing_template_decl)
427 tree t = target_type (val);
429 && decl_function_context (TYPE_MAIN_DECL (t)))
431 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
432 return error_mark_node;
438 tree t = tsubst (TREE_TYPE (parm), &TREE_VEC_ELT (vec, 0),
439 TREE_VEC_LENGTH (vec), in_decl);
440 if (processing_template_decl)
443 val = digest_init (t, arg, (tree *) 0);
445 if (val == error_mark_node || processing_template_decl)
448 /* 14.2: Other template-arguments must be constant-expressions,
449 addresses of objects or functions with external linkage, or of
450 static class members. */
451 else if (!TREE_CONSTANT (val))
453 cp_error ("non-const `%E' cannot be used as template argument",
455 val = error_mark_node;
457 else if (POINTER_TYPE_P (TREE_TYPE (val))
458 && ! integer_zerop (val)
459 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
460 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
464 if (TREE_CODE (t) == ADDR_EXPR)
466 tree a = TREE_OPERAND (t, 0);
468 if (TREE_CODE (a) == STRING_CST)
470 cp_error ("string literal %E is not a valid template argument", a);
471 error ("because it is the address of an object with static linkage");
472 val = error_mark_node;
474 else if (TREE_CODE (a) != VAR_DECL
475 && TREE_CODE (a) != FUNCTION_DECL)
477 else if (! DECL_PUBLIC (a))
479 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
480 val = error_mark_node;
486 cp_error ("`%E' is not a valid template argument", t);
487 error ("it must be %s%s with external linkage",
488 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
489 ? "a pointer to " : "",
490 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
491 ? "a function" : "an object");
492 val = error_mark_node;
497 if (val == error_mark_node)
500 TREE_VEC_ELT (vec, i) = val;
503 return error_mark_node;
508 comp_template_args (oldargs, newargs)
509 tree oldargs, newargs;
513 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
515 tree nt = TREE_VEC_ELT (newargs, i);
516 tree ot = TREE_VEC_ELT (oldargs, i);
520 if (TREE_CODE (nt) != TREE_CODE (ot))
522 if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
524 if (comptypes (ot, nt, 1))
527 else if (cp_tree_equal (ot, nt) > 0)
534 /* Given class template name and parameter list, produce a user-friendly name
535 for the instantiation. */
538 mangle_class_name_for_template (name, parms, arglist)
542 static struct obstack scratch_obstack;
543 static char *scratch_firstobj;
546 if (!scratch_firstobj)
547 gcc_obstack_init (&scratch_obstack);
549 obstack_free (&scratch_obstack, scratch_firstobj);
550 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
553 #define buflen sizeof(buf)
554 #define check if (bufp >= buf+buflen-1) goto too_long
555 #define ccat(c) *bufp++=(c); check
556 #define advance bufp+=strlen(bufp); check
557 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
560 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
562 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
567 nparms = TREE_VEC_LENGTH (parms);
568 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
569 for (i = 0; i < nparms; i++)
571 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
572 tree arg = TREE_VEC_ELT (arglist, i);
577 if (TREE_CODE (parm) == TYPE_DECL)
579 cat (type_as_string (arg, 0));
583 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
585 if (TREE_CODE (arg) == TREE_LIST)
587 /* New list cell was built because old chain link was in
589 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
590 arg = TREE_VALUE (arg);
592 /* No need to check arglist against parmlist here; we did that
593 in coerce_template_parms, called from lookup_template_class. */
594 cat (expr_as_string (arg, 0));
597 char *bufp = obstack_next_free (&scratch_obstack);
599 while (bufp[offset - 1] == ' ')
601 obstack_blank_fast (&scratch_obstack, offset);
603 /* B<C<char> >, not B<C<char>> */
604 if (bufp[offset - 1] == '>')
609 return (char *) obstack_base (&scratch_obstack);
614 fatal ("out of (preallocated) string space creating template instantiation name");
620 classtype_mangled_name (t)
623 if (CLASSTYPE_TEMPLATE_INFO (t)
624 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
626 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
627 char *mangled_name = mangle_class_name_for_template
628 (IDENTIFIER_POINTER (name),
629 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
630 CLASSTYPE_TI_ARGS (t));
631 tree id = get_identifier (mangled_name);
632 IDENTIFIER_TEMPLATE (id) = name;
636 return TYPE_IDENTIFIER (t);
640 add_pending_template (d)
645 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
646 ti = CLASSTYPE_TEMPLATE_INFO (d);
648 ti = DECL_TEMPLATE_INFO (d);
650 if (TREE_LANG_FLAG_0 (ti))
653 *template_tail = perm_tree_cons
654 (current_function_decl, d, NULL_TREE);
655 template_tail = &TREE_CHAIN (*template_tail);
656 TREE_LANG_FLAG_0 (ti) = 1;
659 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
660 parameters, find the desired type.
662 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
663 Since ARGLIST is build on the decl_obstack, we must copy it here
664 to keep it from being reclaimed when the decl storage is reclaimed.
666 IN_DECL, if non-NULL, is the template declaration we are trying to
670 lookup_template_class (d1, arglist, in_decl)
674 tree template, parmlist;
679 if (TREE_CODE (d1) == IDENTIFIER_NODE)
681 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
683 template = IDENTIFIER_CLASS_VALUE (d1);
685 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
687 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
688 d1 = DECL_NAME (template);
690 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
692 template = CLASSTYPE_TI_TEMPLATE (d1);
693 d1 = DECL_NAME (template);
696 my_friendly_abort (272);
698 /* With something like `template <class T> class X class X { ... };'
699 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
700 We don't want to do that, but we have to deal with the situation, so
701 let's give them some syntax errors to chew on instead of a crash. */
703 return error_mark_node;
704 if (TREE_CODE (template) != TEMPLATE_DECL)
706 cp_error ("non-template type `%T' used as a template", d1);
708 cp_error_at ("for template declaration `%D'", in_decl);
709 return error_mark_node;
712 if (PRIMARY_TEMPLATE_P (template))
714 parmlist = DECL_TEMPLATE_PARMS (template);
716 arglist = coerce_template_parms (parmlist, arglist, template);
717 if (arglist == error_mark_node)
718 return error_mark_node;
719 if (uses_template_parms (arglist))
722 if (comp_template_args
723 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
724 found = TREE_TYPE (template);
727 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
728 found; found = TREE_CHAIN (found))
730 if (TI_USES_TEMPLATE_PARMS (found)
731 && comp_template_args (TREE_PURPOSE (found), arglist))
735 found = TREE_VALUE (found);
740 if (can_free (&permanent_obstack, arglist))
741 obstack_free (&permanent_obstack, arglist);
746 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
748 id = get_identifier (mangled_name);
749 IDENTIFIER_TEMPLATE (id) = d1;
751 maybe_push_to_top_level (uses_template_parms (arglist));
752 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
753 pop_from_top_level ();
757 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
760 arglist = CLASSTYPE_TI_ARGS (ctx);
762 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
764 int save_temp = processing_template_decl;
765 processing_template_decl = 0;
766 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
767 processing_template_decl = save_temp;
771 t = lookup_nested_type_by_name (ctx, id);
772 my_friendly_assert (t != NULL_TREE, 42);
776 /* Seems to be wanted. */
777 CLASSTYPE_GOT_SEMICOLON (t) = 1;
779 if (! CLASSTYPE_TEMPLATE_INFO (t))
781 arglist = copy_to_permanent (arglist);
782 CLASSTYPE_TEMPLATE_INFO (t)
783 = perm_tree_cons (template, arglist, NULL_TREE);
784 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
785 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
786 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
787 = uses_template_parms (arglist);
789 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
791 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
792 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
793 if (! uses_template_parms (arglist))
794 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
795 = get_identifier (build_overload_name (t, 1, 1));
797 if (flag_external_templates && ! uses_template_parms (arglist)
798 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
799 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
800 add_pending_template (t);
806 /* Should be defined in parse.h. */
810 uses_template_parms (t)
815 switch (TREE_CODE (t))
819 /* We assume that the object must be instantiated in order to build
820 the COMPONENT_REF, so we test only whether the type of the
821 COMPONENT_REF uses template parms. */
822 return uses_template_parms (TREE_TYPE (t));
824 case IDENTIFIER_NODE:
825 if (!IDENTIFIER_TEMPLATE (t))
827 my_friendly_abort (42);
829 /* aggregates of tree nodes */
832 int i = TREE_VEC_LENGTH (t);
834 if (uses_template_parms (TREE_VEC_ELT (t, i)))
839 if (uses_template_parms (TREE_PURPOSE (t))
840 || uses_template_parms (TREE_VALUE (t)))
842 return uses_template_parms (TREE_CHAIN (t));
844 /* constructed type nodes */
847 return uses_template_parms (TREE_TYPE (t));
849 if (TYPE_PTRMEMFUNC_FLAG (t))
850 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
852 if (! CLASSTYPE_TEMPLATE_INFO (t))
854 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
856 if (uses_template_parms (TYPE_ARG_TYPES (t)))
858 return uses_template_parms (TREE_TYPE (t));
860 if (uses_template_parms (TYPE_DOMAIN (t)))
862 return uses_template_parms (TREE_TYPE (t));
864 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
866 return uses_template_parms (TREE_TYPE (t));
868 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
870 if (uses_template_parms (TYPE_ARG_TYPES (t)))
872 return uses_template_parms (TREE_TYPE (t));
876 return uses_template_parms (TREE_TYPE (t));
880 /* ??? What about FIELD_DECLs? */
881 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
882 && uses_template_parms (DECL_TI_ARGS (t)))
887 if (uses_template_parms (TREE_TYPE (t)))
889 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
894 return uses_template_parms (TREE_TYPE (t));
896 return uses_template_parms (TREE_OPERAND (t, 0));
898 /* template parm nodes */
899 case TEMPLATE_TYPE_PARM:
900 case TEMPLATE_CONST_PARM:
903 /* simple type nodes */
905 if (uses_template_parms (TYPE_MIN_VALUE (t)))
907 return uses_template_parms (TYPE_MAX_VALUE (t));
922 /* Non-error_mark_node ERROR_MARKs are bad things. */
923 my_friendly_assert (t == error_mark_node, 274);
932 return uses_template_parms (TREE_OPERAND (t, 0));
935 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
936 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
937 return uses_template_parms (TREE_OPERAND (t, 1));
940 switch (TREE_CODE_CLASS (TREE_CODE (t)))
948 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
949 if (uses_template_parms (TREE_OPERAND (t, i)))
956 sorry ("testing %s for template parms",
957 tree_code_name [(int) TREE_CODE (t)]);
958 my_friendly_abort (82);
964 static struct tinst_level *current_tinst_level = 0;
965 static struct tinst_level *free_tinst_level = 0;
966 static int tinst_depth = 0;
967 int max_tinst_depth = 17;
968 #ifdef GATHER_STATISTICS
969 int depth_reached = 0;
976 struct tinst_level *new;
978 if (tinst_depth >= max_tinst_depth)
980 struct tinst_level *p = current_tinst_level;
982 char *file = input_filename;
984 error ("template instantiation depth exceeds maximum of %d",
986 cp_error (" instantiating `%D'", d);
988 for (; p; p = p->next)
990 cp_error (" instantiated from `%D'", p->decl);
992 input_filename = p->file;
994 error (" instantiated from here");
997 input_filename = file;
1002 if (free_tinst_level)
1004 new = free_tinst_level;
1005 free_tinst_level = new->next;
1008 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1012 new->file = input_filename;
1013 new->next = current_tinst_level;
1014 current_tinst_level = new;
1017 #ifdef GATHER_STATISTICS
1018 if (tinst_depth > depth_reached)
1019 depth_reached = tinst_depth;
1028 struct tinst_level *old = current_tinst_level;
1030 current_tinst_level = old->next;
1031 old->next = free_tinst_level;
1032 free_tinst_level = old;
1036 struct tinst_level *
1039 struct tinst_level *p = current_tinst_level;
1042 for (; p->next ; p = p->next )
1048 instantiate_class_template (type)
1051 tree template, template_info, args, pattern, t, *field_chain;
1053 if (type == error_mark_node)
1054 return error_mark_node;
1056 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1058 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1061 template = TI_TEMPLATE (template_info);
1062 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1063 args = TI_ARGS (template_info);
1065 t = most_specialized_class
1066 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1068 if (t == error_mark_node)
1070 char *str = "candidates are:";
1071 cp_error ("ambiguous class template instantiation for `%#T'", type);
1072 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1074 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1076 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1080 TYPE_BEING_DEFINED (type) = 1;
1084 pattern = TREE_TYPE (t);
1086 pattern = TREE_TYPE (template);
1088 if (TYPE_SIZE (pattern) == NULL_TREE)
1092 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1094 TYPE_BEING_DEFINED (type) = 1;
1096 if (! push_tinst_level (type))
1099 maybe_push_to_top_level (uses_template_parms (type));
1100 pushclass (type, 0);
1102 if (flag_external_templates)
1104 if (flag_alt_external_templates)
1106 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1107 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1108 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1109 = ! CLASSTYPE_INTERFACE_ONLY (type)
1110 && CLASSTYPE_INTERFACE_KNOWN (type);
1114 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1115 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1116 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1117 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1118 = ! CLASSTYPE_INTERFACE_ONLY (type)
1119 && CLASSTYPE_INTERFACE_KNOWN (type);
1124 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1125 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
1129 tree binfo = TYPE_BINFO (type);
1130 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1136 int len = TREE_VEC_LENGTH (pbases);
1137 BINFO_BASETYPES (binfo) = bases = make_tree_vec (len);
1138 for (i = 0; i < len; ++i)
1142 TREE_VEC_ELT (bases, i) = elt
1143 = tsubst (TREE_VEC_ELT (pbases, i), &TREE_VEC_ELT (args, 0),
1144 TREE_VEC_LENGTH (args), NULL_TREE);
1145 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1147 if (! uses_template_parms (type) &&
1148 TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE)
1149 cp_error ("base class `%T' of `%T' has incomplete type",
1150 TREE_TYPE (elt), type);
1155 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1157 field_chain = &TYPE_FIELDS (type);
1159 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1161 tree name = TREE_PURPOSE (t);
1162 tree tag = TREE_VALUE (t);
1165 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1166 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1167 newtag = start_enum (name);
1169 newtag = tsubst (tag, &TREE_VEC_ELT (args, 0),
1170 TREE_VEC_LENGTH (args), NULL_TREE);
1172 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1174 tree e, values = NULL_TREE, *last = &values;
1176 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
1178 tree elt = build_enumerator
1180 tsubst_expr (TREE_VALUE (e), &TREE_VEC_ELT (args, 0),
1181 TREE_VEC_LENGTH (args), NULL_TREE));
1182 DECL_FIELD_CONTEXT (TREE_VALUE (elt)) = type;
1184 last = &TREE_CHAIN (elt);
1187 finish_enum (newtag, values);
1189 *field_chain = grok_enum_decls (newtag, NULL_TREE);
1190 while (*field_chain)
1191 field_chain = &TREE_CHAIN (*field_chain);
1195 /* Don't replace enum constants here. */
1196 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1197 if (TREE_CODE (t) != CONST_DECL)
1199 tree r = tsubst (t, &TREE_VEC_ELT (args, 0),
1200 TREE_VEC_LENGTH (args), NULL_TREE);
1201 if (TREE_CODE (r) == VAR_DECL)
1203 if (! uses_template_parms (r))
1204 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1205 /* Perhaps I should do more of grokfield here. */
1207 DECL_IN_AGGR_P (r) = 1;
1208 DECL_EXTERNAL (r) = 1;
1209 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1213 field_chain = &TREE_CHAIN (r);
1216 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1218 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1219 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1220 &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), NULL_TREE);
1223 tree d = CLASSTYPE_FRIEND_CLASSES (type) =
1224 tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), &TREE_VEC_ELT (args, 0),
1225 TREE_VEC_LENGTH (args), NULL_TREE);
1227 /* This does injection for friend classes. */
1228 for (; d; d = TREE_CHAIN (d))
1229 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1231 d = tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args, 0),
1232 TREE_VEC_LENGTH (args), NULL_TREE);
1234 for (; d; d = TREE_CHAIN (d))
1236 tree t = TREE_VALUE (d);
1238 if (TREE_CODE (t) == TYPE_DECL)
1239 /* Already injected. */;
1245 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1246 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1247 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1248 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1249 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1250 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1251 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1252 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1253 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1254 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1255 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1256 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1257 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1258 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1259 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1260 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1261 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1262 TYPE_USES_COMPLEX_INHERITANCE (type)
1263 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1264 TYPE_USES_MULTIPLE_INHERITANCE (type)
1265 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1266 TYPE_USES_VIRTUAL_BASECLASSES (type)
1267 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1268 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1269 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1271 if (! uses_template_parms (type))
1274 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1275 if (TREE_CODE (tmp) == FIELD_DECL)
1277 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1278 require_complete_type (tmp);
1281 type = finish_struct_1 (type, 0);
1282 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1283 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1284 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1286 repo_template_used (type);
1290 TYPE_SIZE (type) = integer_zero_node;
1291 CLASSTYPE_METHOD_VEC (type)
1292 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1295 TYPE_BEING_DEFINED (type) = 0;
1298 pop_from_top_level ();
1308 if (t1 == NULL_TREE)
1309 return t2 == NULL_TREE;
1310 if (t2 == NULL_TREE)
1312 /* Don't care if one declares its arg const and the other doesn't -- the
1313 main variant of the arg type is all that matters. */
1314 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1315 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1317 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1321 lookup_nested_type_by_name (ctype, name)
1326 complete_type (ctype);
1328 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
1330 if (name == TREE_PURPOSE (t))
1331 return TREE_VALUE (t);
1337 tsubst (t, args, nargs, in_decl)
1344 if (t == NULL_TREE || t == error_mark_node
1345 || t == integer_type_node
1346 || t == void_type_node
1347 || t == char_type_node)
1350 type = TREE_TYPE (t);
1351 if (type == unknown_type_node)
1352 my_friendly_abort (42);
1353 if (type && TREE_CODE (t) != FUNCTION_DECL)
1354 type = tsubst (type, args, nargs, in_decl);
1356 switch (TREE_CODE (t))
1359 if (TYPE_PTRMEMFUNC_P (t))
1361 tree r = build_ptrmemfunc_type
1362 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1363 return cp_build_type_variant (r, TYPE_READONLY (t),
1367 /* else fall through */
1369 if (uses_template_parms (t))
1371 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
1372 tree r = lookup_template_class (t, argvec, in_decl);
1373 return cp_build_type_variant (r, TYPE_READONLY (t),
1377 /* else fall through */
1379 case IDENTIFIER_NODE:
1391 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1392 if (ctx == NULL_TREE)
1395 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
1399 if (t == integer_type_node)
1402 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1403 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1407 tree max = tsubst_expr (TYPE_MAX_VALUE (t), args, nargs, in_decl);
1408 if (processing_template_decl)
1410 tree itype = make_node (INTEGER_TYPE);
1411 TYPE_MIN_VALUE (itype) = size_zero_node;
1412 TYPE_MAX_VALUE (itype) = max;
1415 return build_index_2_type (size_zero_node, max);
1418 case TEMPLATE_TYPE_PARM:
1420 tree arg = args[TEMPLATE_TYPE_IDX (t)];
1421 return cp_build_type_variant
1422 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
1423 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
1426 case TEMPLATE_CONST_PARM:
1427 return args[TEMPLATE_CONST_IDX (t)];
1432 tree arg_types, ctx;
1436 if (DECL_CONTEXT (t) != NULL_TREE
1437 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1439 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
1443 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1444 type = tsubst (type, args, nargs, in_decl);
1450 type = tsubst (type, args, nargs, in_decl);
1453 if (type == TREE_TYPE (t)
1454 && (! member || ctx == DECL_CLASS_CONTEXT (t)))
1461 /* Do we already have this instantiation? */
1462 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1464 tree tmpl = TREE_PURPOSE (DECL_TEMPLATE_INFO (t));
1465 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1467 for (; decls; decls = TREE_CHAIN (decls))
1468 if (TREE_TYPE (TREE_VALUE (decls)) == type
1469 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
1470 return TREE_VALUE (decls);
1473 /* We do NOT check for matching decls pushed separately at this
1474 point, as they may not represent instantiations of this
1475 template, and in any case are considered separate under the
1480 TREE_TYPE (r) = type;
1483 = tsubst (DECL_CONTEXT (t), args, nargs, t);
1484 DECL_CLASS_CONTEXT (r) = ctx;
1486 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
1487 IDENTIFIER_POINTER (DECL_NAME (r)),
1488 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1490 /* Type-conversion operator. Reconstruct the name, in
1491 case it's the name of one of the template's parameters. */
1492 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
1495 arg_types = TYPE_VALUES (type);
1497 if (member && TREE_CODE (type) == FUNCTION_TYPE)
1498 arg_types = hash_tree_chain
1499 (build_pointer_type (DECL_CONTEXT (r)), arg_types);
1501 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
1503 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
1504 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
1505 buf = (char *) alloca (strlen (dbuf)
1506 + sizeof (DESTRUCTOR_DECL_PREFIX));
1507 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
1510 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
1513 DECL_ASSEMBLER_NAME (r)
1514 = build_decl_overload (DECL_NAME (r), arg_types, member);
1516 make_decl_rtl (r, NULL_PTR, 1);
1518 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1519 DECL_MAIN_VARIANT (r) = r;
1520 DECL_RESULT (r) = NULL_TREE;
1521 DECL_INITIAL (r) = NULL_TREE;
1523 TREE_STATIC (r) = 0;
1524 TREE_PUBLIC (r) = 1;
1525 DECL_EXTERNAL (r) = 1;
1526 DECL_INTERFACE_KNOWN (r) = 0;
1527 DECL_DEFER_OUTPUT (r) = 0;
1528 TREE_CHAIN (r) = NULL_TREE;
1529 DECL_CHAIN (r) = NULL_TREE;
1531 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
1532 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
1534 /* Look for matching decls for the moment. */
1537 tree decls = lookup_name_nonclass (DECL_NAME (t));
1540 if (decls == NULL_TREE)
1542 else if (is_overloaded_fn (decls))
1543 for (decls = get_first_fn (decls); decls;
1544 decls = DECL_CHAIN (decls))
1546 if (TREE_CODE (decls) == FUNCTION_DECL
1547 && TREE_TYPE (decls) == type)
1556 int dcl_only = ! DECL_INITIAL (d);
1558 DECL_INITIAL (r) = error_mark_node;
1559 duplicate_decls (r, d);
1562 DECL_INITIAL (r) = 0;
1566 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1568 tree tmpl = DECL_TI_TEMPLATE (t);
1569 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1570 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1571 args, nargs, in_decl);
1573 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1574 *declsp = perm_tree_cons (argvec, r, *declsp);
1576 /* If we have a preexisting version of this function, don't expand
1577 the template version, use the other instead. */
1578 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
1579 SET_DECL_TEMPLATE_SPECIALIZATION (r);
1581 SET_DECL_IMPLICIT_INSTANTIATION (r);
1583 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1584 tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1587 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
1588 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
1590 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
1591 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
1598 tree r = copy_node (t);
1599 TREE_TYPE (r) = type;
1600 DECL_INITIAL (r) = TREE_TYPE (r);
1601 DECL_CONTEXT (r) = NULL_TREE;
1602 #ifdef PROMOTE_PROTOTYPES
1603 if ((TREE_CODE (type) == INTEGER_TYPE
1604 || TREE_CODE (type) == ENUMERAL_TYPE)
1605 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
1606 DECL_ARG_TYPE (r) = integer_type_node;
1609 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1615 tree r = copy_node (t);
1616 TREE_TYPE (r) = type;
1619 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
1621 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
1622 TREE_CHAIN (r) = NULL_TREE;
1628 tree r = copy_node (t);
1630 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
1631 TREE_CHAIN (r) = NULL_TREE;
1638 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
1640 /* Do we already have this instantiation? */
1641 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1643 tree tmpl = DECL_TI_TEMPLATE (t);
1644 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1646 for (; decls; decls = TREE_CHAIN (decls))
1647 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
1648 return TREE_VALUE (decls);
1652 TREE_TYPE (r) = type;
1653 DECL_CONTEXT (r) = ctx;
1654 if (TREE_STATIC (r))
1655 DECL_ASSEMBLER_NAME (r)
1656 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
1658 /* Don't try to expand the initializer until someone tries to use
1659 this variable; otherwise we run into circular dependencies. */
1660 DECL_INITIAL (r) = NULL_TREE;
1665 if (DECL_LANG_SPECIFIC (r))
1668 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
1671 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1673 tree tmpl = DECL_TI_TEMPLATE (t);
1674 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1675 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1676 args, nargs, in_decl);
1678 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1679 *declsp = perm_tree_cons (argvec, r, *declsp);
1680 SET_DECL_IMPLICIT_INSTANTIATION (r);
1682 TREE_CHAIN (r) = NULL_TREE;
1687 if (t == TYPE_NAME (TREE_TYPE (t)))
1688 return TYPE_NAME (type);
1691 tree r = copy_node (t);
1692 TREE_TYPE (r) = type;
1693 DECL_CONTEXT (r) = current_class_type;
1694 TREE_CHAIN (r) = NULL_TREE;
1700 tree purpose, value, chain, result;
1701 int via_public, via_virtual, via_protected;
1703 if (t == void_list_node)
1706 via_public = TREE_VIA_PUBLIC (t);
1707 via_protected = TREE_VIA_PROTECTED (t);
1708 via_virtual = TREE_VIA_VIRTUAL (t);
1710 purpose = TREE_PURPOSE (t);
1712 purpose = tsubst (purpose, args, nargs, in_decl);
1713 value = TREE_VALUE (t);
1715 value = tsubst (value, args, nargs, in_decl);
1716 chain = TREE_CHAIN (t);
1717 if (chain && chain != void_type_node)
1718 chain = tsubst (chain, args, nargs, in_decl);
1719 if (purpose == TREE_PURPOSE (t)
1720 && value == TREE_VALUE (t)
1721 && chain == TREE_CHAIN (t))
1723 result = hash_tree_cons (via_public, via_virtual, via_protected,
1724 purpose, value, chain);
1725 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1729 if (type != NULL_TREE)
1733 if (type == TREE_TYPE (t))
1736 TREE_TYPE (t) = complete_type (type);
1737 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
1738 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
1739 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
1740 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
1745 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1746 tree *elts = (tree *) alloca (len * sizeof (tree));
1748 bzero ((char *) elts, len * sizeof (tree));
1750 for (i = 0; i < len; i++)
1752 elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
1753 if (elts[i] != TREE_VEC_ELT (t, i))
1760 t = make_tree_vec (len);
1761 for (i = 0; i < len; i++)
1762 TREE_VEC_ELT (t, i) = elts[i];
1767 case REFERENCE_TYPE:
1770 enum tree_code code;
1771 if (type == TREE_TYPE (t))
1774 code = TREE_CODE (t);
1775 if (code == POINTER_TYPE)
1776 r = build_pointer_type (type);
1778 r = build_reference_type (type);
1779 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
1780 /* Will this ever be needed for TYPE_..._TO values? */
1785 return build_offset_type
1786 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
1790 tree values = TYPE_ARG_TYPES (t);
1791 tree context = TYPE_CONTEXT (t);
1792 tree raises = TYPE_RAISES_EXCEPTIONS (t);
1795 /* Don't bother recursing if we know it won't change anything. */
1796 if (values != void_list_node)
1798 /* This should probably be rewritten to use hash_tree_cons for
1799 the memory savings. */
1800 tree first = NULL_TREE;
1803 for (; values && values != void_list_node;
1804 values = TREE_CHAIN (values))
1807 = tsubst (TREE_VALUE (values), args, nargs, in_decl);
1808 tree purpose = tsubst_expr (TREE_PURPOSE (values),
1809 args, nargs, in_decl);
1810 tree x = build_tree_list (purpose, value);
1813 TREE_CHAIN (last) = x;
1819 if (values == void_list_node)
1820 TREE_CHAIN (last) = void_list_node;
1825 context = tsubst (context, args, nargs, in_decl);
1826 /* Could also optimize cases where return value and
1827 values have common elements (e.g., T min(const &T, const T&). */
1829 /* If the above parameters haven't changed, just return the type. */
1830 if (type == TREE_TYPE (t)
1831 && values == TYPE_VALUES (t)
1832 && context == TYPE_CONTEXT (t))
1835 /* Construct a new type node and return it. */
1836 if (TREE_CODE (t) == FUNCTION_TYPE
1837 && context == NULL_TREE)
1839 fntype = build_function_type (type, values);
1841 else if (context == NULL_TREE)
1843 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1844 args, nargs, in_decl);
1845 fntype = build_cplus_method_type (base, type,
1846 TREE_CHAIN (values));
1850 fntype = make_node (TREE_CODE (t));
1851 TREE_TYPE (fntype) = type;
1852 TYPE_CONTEXT (fntype) = context;
1853 TYPE_VALUES (fntype) = values;
1854 TYPE_SIZE (fntype) = TYPE_SIZE (t);
1855 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
1856 TYPE_MODE (fntype) = TYPE_MODE (t);
1857 if (TYPE_METHOD_BASETYPE (t))
1858 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
1859 args, nargs, in_decl);
1860 /* Need to generate hash value. */
1861 my_friendly_abort (84);
1863 fntype = build_type_variant (fntype,
1868 raises = tsubst (raises, args, nargs, in_decl);
1869 fntype = build_exception_variant (fntype, raises);
1875 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1877 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1879 r = build_cplus_array_type (type, domain);
1885 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1886 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1887 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1891 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1892 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1896 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1897 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
1898 return cp_build_type_variant
1899 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
1900 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
1904 return make_pointer_declarator
1905 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1908 return make_reference_declarator
1909 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1912 return build_parse_node
1913 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1914 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
1917 return make_call_declarator
1918 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1919 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
1920 TREE_OPERAND (t, 2),
1921 tsubst (TREE_TYPE (t), args, nargs, in_decl));
1924 return build_parse_node
1925 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1926 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
1929 sorry ("use of `%s' in template",
1930 tree_code_name [(int) TREE_CODE (t)]);
1931 return error_mark_node;
1938 emit_line_note (input_filename, lineno);
1942 expand_start_bindings (0);
1950 expand_end_bindings (getdecls (), kept_level_p (), 1);
1951 t = poplevel (kept_level_p (), 1, 0);
1957 tsubst_copy (t, args, nargs, in_decl)
1962 enum tree_code code;
1964 if (t == NULL_TREE || t == error_mark_node)
1967 code = TREE_CODE (t);
1972 return do_identifier (DECL_NAME (t), 0);
1976 if (DECL_CONTEXT (t))
1978 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
1979 if (ctx != DECL_CONTEXT (t))
1980 return lookup_field (ctx, DECL_NAME (t), 0, 0);
1986 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1987 t = tsubst (t, args, nargs, in_decl);
1992 case IDENTIFIER_NODE:
1993 return do_identifier (t, 0);
1997 case REINTERPRET_CAST_EXPR:
1998 case CONST_CAST_EXPR:
1999 case STATIC_CAST_EXPR:
2000 case DYNAMIC_CAST_EXPR:
2002 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2003 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2006 case PREDECREMENT_EXPR:
2007 case PREINCREMENT_EXPR:
2008 case POSTDECREMENT_EXPR:
2009 case POSTINCREMENT_EXPR:
2011 case TRUTH_NOT_EXPR:
2013 case CONVERT_EXPR: /* Unary + */
2020 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2025 case TRUNC_DIV_EXPR:
2027 case FLOOR_DIV_EXPR:
2028 case ROUND_DIV_EXPR:
2029 case EXACT_DIV_EXPR:
2031 case BIT_ANDTC_EXPR:
2034 case TRUNC_MOD_EXPR:
2035 case FLOOR_MOD_EXPR:
2036 case TRUTH_ANDIF_EXPR:
2037 case TRUTH_ORIF_EXPR:
2038 case TRUTH_AND_EXPR:
2059 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2060 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2064 tree fn = TREE_OPERAND (t, 0);
2065 if (really_overloaded_fn (fn))
2066 fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
2068 fn = tsubst_copy (fn, args, nargs, in_decl);
2070 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2074 case METHOD_CALL_EXPR:
2076 tree name = TREE_OPERAND (t, 0);
2077 if (TREE_CODE (name) == BIT_NOT_EXPR)
2079 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2080 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2082 else if (TREE_CODE (name) == SCOPE_REF
2083 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
2085 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2086 name = TREE_OPERAND (name, 1);
2087 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2088 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2089 name = build_nt (SCOPE_REF, base, name);
2092 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
2094 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2095 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
2102 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2103 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2104 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
2109 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2110 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2111 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
2112 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
2119 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2120 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2121 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
2122 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
2128 tree purpose, value, chain;
2130 if (t == void_list_node)
2133 purpose = TREE_PURPOSE (t);
2135 purpose = tsubst_copy (purpose, args, nargs, in_decl);
2136 value = TREE_VALUE (t);
2138 value = tsubst_copy (value, args, nargs, in_decl);
2139 chain = TREE_CHAIN (t);
2140 if (chain && chain != void_type_node)
2141 chain = tsubst_copy (chain, args, nargs, in_decl);
2142 if (purpose == TREE_PURPOSE (t)
2143 && value == TREE_VALUE (t)
2144 && chain == TREE_CHAIN (t))
2146 return tree_cons (purpose, value, chain);
2153 case TEMPLATE_TYPE_PARM:
2154 case TEMPLATE_CONST_PARM:
2156 case REFERENCE_TYPE:
2162 return tsubst (t, args, nargs, in_decl);
2164 case IDENTIFIER_NODE:
2165 if (IDENTIFIER_TYPENAME_P (t))
2166 return build_typename_overload
2167 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
2173 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
2174 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
2182 tsubst_expr (t, args, nargs, in_decl)
2187 if (t == NULL_TREE || t == error_mark_node)
2190 if (processing_template_decl)
2191 return tsubst_copy (t, args, nargs, in_decl);
2193 switch (TREE_CODE (t))
2196 lineno = TREE_COMPLEXITY (t);
2197 emit_line_note (input_filename, lineno);
2199 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2204 lineno = TREE_COMPLEXITY (t);
2205 emit_line_note (input_filename, lineno);
2206 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2207 /* Do default conversion if safe and possibly important,
2208 in case within ({...}). */
2209 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
2210 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2211 t = default_conversion (t);
2212 cplus_expand_expr_stmt (t);
2219 int i = suspend_momentary ();
2222 lineno = TREE_COMPLEXITY (t);
2223 emit_line_note (input_filename, lineno);
2225 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2226 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2227 TREE_OPERAND (t, 2) != 0);
2228 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2230 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
2231 resume_momentary (i);
2238 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
2239 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2240 int cond_scope = (TREE_OPERAND (t, 1)
2241 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
2243 lineno = TREE_COMPLEXITY (t);
2244 emit_line_note (input_filename, lineno);
2247 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
2248 tsubst_expr (tmp, args, nargs, in_decl);
2250 emit_line_note (input_filename, lineno);
2251 expand_start_loop_continue_elsewhere (1);
2255 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2256 emit_line_note (input_filename, lineno);
2258 expand_exit_loop_if_false (0, condition_conversion (tmp));
2262 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
2265 emit_line_note (input_filename, lineno);
2266 expand_loop_continue_here ();
2267 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2269 cplus_expand_expr_stmt (tmp);
2282 lineno = TREE_COMPLEXITY (t);
2284 emit_line_note (input_filename, lineno);
2285 expand_start_loop (1);
2287 cond = TREE_OPERAND (t, 0);
2288 if (TREE_CODE (cond) == DECL_STMT)
2290 cond = tsubst_expr (cond, args, nargs, in_decl);
2291 emit_line_note (input_filename, lineno);
2292 expand_exit_loop_if_false (0, condition_conversion (cond));
2294 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
2296 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2308 lineno = TREE_COMPLEXITY (t);
2310 emit_line_note (input_filename, lineno);
2311 expand_start_loop_continue_elsewhere (1);
2313 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2314 expand_loop_continue_here ();
2316 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2317 emit_line_note (input_filename, lineno);
2318 expand_exit_loop_if_false (0, condition_conversion (cond));
2329 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2331 lineno = TREE_COMPLEXITY (t);
2334 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2335 emit_line_note (input_filename, lineno);
2336 expand_start_cond (condition_conversion (tmp), 0);
2338 if (tmp = TREE_OPERAND (t, 1), tmp)
2339 tsubst_expr (tmp, args, nargs, in_decl);
2341 if (tmp = TREE_OPERAND (t, 2), tmp)
2343 expand_start_else ();
2344 tsubst_expr (tmp, args, nargs, in_decl);
2358 tree substmt = TREE_OPERAND (t, 0);
2360 lineno = TREE_COMPLEXITY (t);
2362 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2365 for (; substmt; substmt = TREE_CHAIN (substmt))
2366 tsubst_expr (substmt, args, nargs, in_decl);
2368 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2374 lineno = TREE_COMPLEXITY (t);
2375 emit_line_note (input_filename, lineno);
2376 if (! expand_exit_something ())
2377 error ("break statement not within loop or switch");
2381 lineno = TREE_COMPLEXITY (t);
2382 emit_line_note (input_filename, lineno);
2383 if (! expand_continue_loop (0))
2384 error ("continue statement not within a loop");
2390 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2392 lineno = TREE_COMPLEXITY (t);
2395 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2396 emit_line_note (input_filename, lineno);
2397 c_expand_start_case (val);
2400 if (tmp = TREE_OPERAND (t, 1), tmp)
2401 tsubst_expr (tmp, args, nargs, in_decl);
2403 expand_end_case (val);
2414 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
2415 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2419 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
2426 lineno = TREE_COMPLEXITY (t);
2427 emit_line_note (input_filename, lineno);
2428 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
2430 tree decl = lookup_label (TREE_OPERAND (t, 0));
2431 TREE_USED (decl) = 1;
2435 expand_computed_goto
2436 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2440 lineno = TREE_COMPLEXITY (t);
2441 emit_line_note (input_filename, lineno);
2442 expand_start_try_stmts ();
2443 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2444 expand_start_all_catch ();
2446 tree handler = TREE_OPERAND (t, 1);
2447 for (; handler; handler = TREE_CHAIN (handler))
2448 tsubst_expr (handler, args, nargs, in_decl);
2450 expand_end_all_catch ();
2454 lineno = TREE_COMPLEXITY (t);
2456 if (TREE_OPERAND (t, 0))
2458 tree d = TREE_OPERAND (t, 0);
2459 expand_start_catch_block
2460 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
2461 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
2464 expand_start_catch_block (NULL_TREE, NULL_TREE);
2465 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2466 expand_end_catch_block ();
2471 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
2477 instantiate_template (tmpl, targ_ptr)
2478 tree tmpl, *targ_ptr;
2482 struct obstack *old_fmp_obstack;
2483 extern struct obstack *function_maybepermanent_obstack;
2485 push_obstacks (&permanent_obstack, &permanent_obstack);
2486 old_fmp_obstack = function_maybepermanent_obstack;
2487 function_maybepermanent_obstack = &permanent_obstack;
2489 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
2490 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
2495 tree t = targ_ptr [i];
2496 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2498 tree nt = target_type (t);
2499 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
2501 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
2502 cp_error (" trying to instantiate `%D'", tmpl);
2503 fndecl = error_mark_node;
2507 targ_ptr[i] = copy_to_permanent (t);
2510 /* substitute template parameters */
2511 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
2514 function_maybepermanent_obstack = old_fmp_obstack;
2520 /* Push the name of the class template into the scope of the instantiation. */
2523 overload_template_name (type)
2526 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
2529 if (IDENTIFIER_CLASS_VALUE (id)
2530 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
2533 decl = build_decl (TYPE_DECL, id, type);
2534 SET_DECL_ARTIFICIAL (decl);
2535 pushdecl_class_level (decl);
2538 /* Type unification.
2540 We have a function template signature with one or more references to
2541 template parameters, and a parameter list we wish to fit to this
2542 template. If possible, produce a list of parameters for the template
2543 which will cause it to fit the supplied parameter list.
2545 Return zero for success, 2 for an incomplete match that doesn't resolve
2546 all the types, and 1 for complete failure. An error message will be
2547 printed only for an incomplete match.
2549 TPARMS[NTPARMS] is an array of template parameter types;
2550 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2551 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2552 and ARGS is the argument list we're trying to match against it.
2554 If SUBR is 1, we're being called recursively (to unify the arguments of
2555 a function or method parameter of a function template), so don't zero
2556 out targs and don't fail on an incomplete match.
2558 If STRICT is 1, the match must be exact (for casts of overloaded
2559 addresses, explicit instantiation, and more_specialized). */
2562 type_unification (tparms, targs, parms, args, nsubsts, subr, strict)
2563 tree tparms, *targs, parms, args;
2564 int *nsubsts, subr, strict;
2568 int ntparms = TREE_VEC_LENGTH (tparms);
2570 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
2571 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
2572 /* ARGS could be NULL (via a call from parse.y to
2573 build_x_function_call). */
2575 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
2576 my_friendly_assert (ntparms > 0, 292);
2579 bzero ((char *) targs, sizeof (tree) * ntparms);
2582 && parms != void_list_node
2584 && args != void_list_node)
2586 parm = TREE_VALUE (parms);
2587 parms = TREE_CHAIN (parms);
2588 arg = TREE_VALUE (args);
2589 args = TREE_CHAIN (args);
2591 if (arg == error_mark_node)
2593 if (arg == unknown_type_node)
2596 if (! uses_template_parms (parm)
2597 && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2599 if (can_convert_arg (parm, TREE_TYPE (arg), arg))
2605 if (TREE_CODE (arg) == VAR_DECL)
2606 arg = TREE_TYPE (arg);
2607 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
2608 arg = TREE_TYPE (arg);
2610 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2612 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
2613 if (TREE_CODE (arg) == TREE_LIST
2614 && TREE_TYPE (arg) == unknown_type_node
2615 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
2617 int nsubsts, ntparms;
2620 /* Have to back unify here */
2621 arg = TREE_VALUE (arg);
2623 ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
2624 targs = (tree *) alloca (sizeof (tree) * ntparms);
2625 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
2626 return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
2627 TYPE_ARG_TYPES (TREE_TYPE (arg)),
2628 parm, &nsubsts, 0, strict);
2630 arg = TREE_TYPE (arg);
2633 if (TREE_CODE (arg) == REFERENCE_TYPE)
2634 arg = TREE_TYPE (arg);
2636 if (TREE_CODE (parm) != REFERENCE_TYPE)
2638 if (TREE_CODE (arg) == FUNCTION_TYPE
2639 || TREE_CODE (arg) == METHOD_TYPE)
2640 arg = build_pointer_type (arg);
2641 else if (TREE_CODE (arg) == ARRAY_TYPE)
2642 arg = build_pointer_type (TREE_TYPE (arg));
2644 arg = TYPE_MAIN_VARIANT (arg);
2647 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
2655 /* Fail if we've reached the end of the parm list, and more args
2656 are present, and the parm list isn't variadic. */
2657 if (args && args != void_list_node && parms == void_list_node)
2659 /* Fail if parms are left and they don't have default values. */
2661 && parms != void_list_node
2662 && TREE_PURPOSE (parms) == NULL_TREE)
2665 for (i = 0; i < ntparms; i++)
2668 error ("incomplete type unification");
2674 /* Tail recursion is your friend. */
2677 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
2678 tree tparms, *targs, parm, arg;
2679 int *nsubsts, ntparms, strict;
2683 /* I don't think this will do the right thing with respect to types.
2684 But the only case I've seen it in so far has been array bounds, where
2685 signedness is the only information lost, and I think that will be
2687 while (TREE_CODE (parm) == NOP_EXPR)
2688 parm = TREE_OPERAND (parm, 0);
2690 if (arg == error_mark_node)
2692 if (arg == unknown_type_node)
2697 switch (TREE_CODE (parm))
2699 case TEMPLATE_TYPE_PARM:
2701 idx = TEMPLATE_TYPE_IDX (parm);
2702 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
2703 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
2706 /* Template type parameters cannot contain cv-quals; i.e.
2707 template <class T> void f (T& a, T& b) will not generate
2708 void f (const int& a, const int& b). */
2709 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
2710 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
2712 arg = TYPE_MAIN_VARIANT (arg);
2715 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
2716 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
2717 arg = cp_build_type_variant (arg, constp, volatilep);
2720 /* Simple cases: Value already set, does match or doesn't. */
2721 if (targs[idx] == arg)
2723 else if (targs[idx])
2725 /* Check for mixed types and values. */
2726 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
2730 case TEMPLATE_CONST_PARM:
2732 idx = TEMPLATE_CONST_IDX (parm);
2733 if (targs[idx] == arg)
2735 else if (targs[idx])
2737 tree t = targs[idx];
2738 if (TREE_CODE (t) == TREE_CODE (arg))
2739 switch (TREE_CODE (arg))
2742 if (tree_int_cst_equal (t, arg))
2746 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2749 /* STRING_CST values are not valid template const parms. */
2753 my_friendly_abort (87);
2756 /* else if (typeof arg != tparms[idx])
2759 targs[idx] = copy_to_permanent (arg);
2763 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
2764 return unify (tparms, targs, ntparms, parm,
2765 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
2767 if (TREE_CODE (arg) != POINTER_TYPE)
2769 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2772 case REFERENCE_TYPE:
2773 if (TREE_CODE (arg) == REFERENCE_TYPE)
2774 arg = TREE_TYPE (arg);
2775 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
2779 if (TREE_CODE (arg) != ARRAY_TYPE)
2781 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2782 nsubsts, strict) != 0)
2784 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2789 if (TREE_CODE (arg) != TREE_CODE (parm))
2792 if (TREE_CODE (parm) == INTEGER_TYPE)
2794 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2795 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
2796 TYPE_MIN_VALUE (arg), nsubsts, strict))
2798 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2799 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
2800 TYPE_MAX_VALUE (arg), nsubsts, strict))
2803 /* As far as unification is concerned, this wins. Later checks
2804 will invalidate it if necessary. */
2807 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2809 if (TREE_CODE (arg) != INTEGER_CST)
2811 return !tree_int_cst_equal (parm, arg);
2816 t1 = TREE_OPERAND (parm, 0);
2817 t2 = TREE_OPERAND (parm, 1);
2818 return unify (tparms, targs, ntparms, t1,
2819 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2826 if (TREE_CODE (arg) != TREE_VEC)
2828 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2830 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2831 if (unify (tparms, targs, ntparms,
2832 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2839 if (TYPE_PTRMEMFUNC_FLAG (parm))
2840 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2841 arg, nsubsts, strict);
2843 /* Allow trivial conversions. */
2844 if (TREE_CODE (arg) != RECORD_TYPE
2845 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2846 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2849 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
2852 if (flag_ansi_overloading && ! strict)
2853 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
2855 (CLASSTYPE_TEMPLATE_INFO (arg)
2856 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
2858 if (! t || t == error_mark_node)
2861 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
2862 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
2864 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
2869 if (TREE_CODE (arg) != METHOD_TYPE)
2874 if (TREE_CODE (arg) != FUNCTION_TYPE)
2877 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
2878 TREE_TYPE (arg), nsubsts, strict))
2880 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2881 TYPE_ARG_TYPES (arg), nsubsts, 1, strict);
2884 if (TREE_CODE (arg) != OFFSET_TYPE)
2886 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2887 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
2889 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2890 TREE_TYPE (arg), nsubsts, strict);
2893 sorry ("use of `%s' in template type unification",
2894 tree_code_name [(int) TREE_CODE (parm)]);
2900 mark_decl_instantiated (result, extern_p)
2904 if (DECL_TEMPLATE_INSTANTIATION (result))
2905 SET_DECL_EXPLICIT_INSTANTIATION (result);
2906 TREE_PUBLIC (result) = 1;
2910 DECL_INTERFACE_KNOWN (result) = 1;
2911 DECL_NOT_REALLY_EXTERN (result) = 1;
2913 else if (TREE_CODE (result) == FUNCTION_DECL)
2914 mark_inline_for_output (result);
2917 /* Given two function templates PAT1 and PAT2, return:
2919 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
2920 -1 if PAT2 is more specialized than PAT1.
2921 0 if neither is more specialized. */
2924 more_specialized (pat1, pat2)
2930 targs = get_bindings (pat1, pat2);
2937 targs = get_bindings (pat2, pat1);
2947 /* Given two class template specialization list nodes PAT1 and PAT2, return:
2949 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
2950 -1 if PAT2 is more specialized than PAT1.
2951 0 if neither is more specialized. */
2954 more_specialized_class (pat1, pat2)
2960 targs = get_class_bindings
2961 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
2965 targs = get_class_bindings
2966 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
2973 /* Return the template arguments that will produce the function signature
2974 DECL from the function template FN. */
2977 get_bindings (fn, decl)
2980 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2981 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
2983 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2984 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2985 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2994 get_class_bindings (tparms, parms, args)
2995 tree tparms, parms, args;
2997 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
2998 tree vec = make_temp_vec (ntparms);
3000 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
3002 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
3003 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
3013 for (i = 0; i < ntparms; ++i)
3014 if (! TREE_VEC_ELT (vec, i))
3020 /* Return the most specialized of the list of templates in FNS that can
3021 produce an instantiation matching DECL. */
3024 most_specialized (fns, decl)
3027 tree fn, champ, *args, *p;
3030 for (p = &fns; *p; )
3032 args = get_bindings (TREE_VALUE (*p), decl);
3036 p = &TREE_CHAIN (*p);
3039 *p = TREE_CHAIN (*p);
3046 champ = TREE_VALUE (fn);
3047 fn = TREE_CHAIN (fn);
3048 for (; fn; fn = TREE_CHAIN (fn))
3050 fate = more_specialized (champ, TREE_VALUE (fn));
3057 fn = TREE_CHAIN (fn);
3059 return error_mark_node;
3061 champ = TREE_VALUE (fn);
3065 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
3067 fate = more_specialized (champ, TREE_VALUE (fn));
3069 return error_mark_node;
3075 /* Return the most specialized of the class template specializations in
3076 SPECS that can produce an instantiation matching ARGS. */
3079 most_specialized_class (specs, mainargs)
3080 tree specs, mainargs;
3082 tree list = NULL_TREE, t, args, champ;
3085 for (t = specs; t; t = TREE_CHAIN (t))
3087 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
3090 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
3091 TREE_TYPE (list) = TREE_TYPE (t);
3101 for (; t; t = TREE_CHAIN (t))
3103 fate = more_specialized_class (champ, t);
3112 return error_mark_node;
3118 for (t = list; t && t != champ; t = TREE_CHAIN (t))
3120 fate = more_specialized (champ, t);
3122 return error_mark_node;
3128 /* called from the parser. */
3131 do_function_instantiation (declspecs, declarator, storage)
3132 tree declspecs, declarator, storage;
3134 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
3137 tree result = NULL_TREE;
3140 if (! DECL_LANG_SPECIFIC (decl))
3142 cp_error ("explicit instantiation of non-template `%#D'", decl);
3146 /* If we've already seen this template instance, use it. */
3147 if (DECL_FUNCTION_MEMBER_P (decl))
3149 if (DECL_TEMPLATE_INSTANTIATION (decl))
3151 else if (name = DECL_ASSEMBLER_NAME (decl),
3152 fn = IDENTIFIER_GLOBAL_VALUE (name),
3153 fn && DECL_TEMPLATE_INSTANTIATION (fn))
3156 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
3158 tree templates = NULL_TREE;
3159 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
3160 if (decls_match (fn, decl)
3161 && DECL_DEFER_OUTPUT (fn))
3166 else if (TREE_CODE (fn) == TEMPLATE_DECL)
3167 templates = decl_tree_cons (NULL_TREE, fn, templates);
3172 result = most_specialized (templates, decl);
3173 if (result == error_mark_node)
3175 char *str = "candidates are:";
3176 cp_error ("ambiguous template instantiation for `%D' requested", decl);
3177 for (fn = templates; fn; fn = TREE_CHAIN (fn))
3179 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
3186 args = get_bindings (result, decl);
3187 result = instantiate_template (result, args);
3194 cp_error ("no matching template for `%D' found", decl);
3198 if (flag_external_templates)
3201 if (storage == NULL_TREE)
3203 else if (storage == ridpointers[(int) RID_EXTERN])
3206 cp_error ("storage class `%D' applied to template instantiation",
3209 mark_decl_instantiated (result, extern_p);
3210 repo_template_instantiated (result, extern_p);
3212 instantiate_decl (result);
3216 mark_class_instantiated (t, extern_p)
3220 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
3221 SET_CLASSTYPE_INTERFACE_KNOWN (t);
3222 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
3223 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
3224 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
3227 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
3228 rest_of_type_compilation (t, 1);
3233 do_type_instantiation (name, storage)
3236 tree t = TREE_TYPE (name);
3243 /* With -fexternal-templates, explicit instantiations are treated the same
3244 as implicit ones. */
3245 if (flag_external_templates)
3248 if (TYPE_SIZE (t) == NULL_TREE)
3250 cp_error ("explicit instantiation of `%#T' before definition of template",
3255 if (storage == NULL_TREE)
3257 else if (storage == ridpointers[(int) RID_INLINE])
3259 else if (storage == ridpointers[(int) RID_EXTERN])
3261 else if (storage == ridpointers[(int) RID_STATIC])
3265 cp_error ("storage class `%D' applied to template instantiation",
3270 /* We've already instantiated this. */
3271 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
3275 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3277 mark_class_instantiated (t, extern_p);
3278 repo_template_instantiated (t, extern_p);
3288 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
3289 if (DECL_TEMPLATE_INSTANTIATION (tmp))
3291 mark_decl_instantiated (tmp, extern_p);
3292 repo_template_instantiated (tmp, extern_p);
3294 instantiate_decl (tmp);
3297 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
3298 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
3300 mark_decl_instantiated (tmp, extern_p);
3301 repo_template_instantiated (tmp, extern_p);
3303 instantiate_decl (tmp);
3306 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
3307 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
3308 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
3313 instantiate_decl (d)
3316 tree ti = DECL_TEMPLATE_INFO (d);
3317 tree tmpl = TI_TEMPLATE (ti);
3318 tree args = TI_ARGS (ti);
3320 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
3322 int nested = in_function_p ();
3324 int pattern_defined;
3326 char *file = input_filename;
3328 if (TREE_CODE (d) == FUNCTION_DECL)
3330 d_defined = (DECL_INITIAL (d) != NULL_TREE);
3331 pattern_defined = (DECL_INITIAL (pattern) != NULL_TREE);
3335 d_defined = ! DECL_IN_AGGR_P (d);
3336 pattern_defined = ! DECL_IN_AGGR_P (pattern);
3341 else if (pattern_defined)
3343 repo_template_used (d);
3345 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
3347 if (flag_alt_external_templates)
3349 if (interface_unknown)
3350 warn_if_unknown_interface (d);
3352 else if (DECL_INTERFACE_KNOWN (pattern))
3354 DECL_INTERFACE_KNOWN (d) = 1;
3355 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (pattern);
3358 warn_if_unknown_interface (pattern);
3362 import_export_decl (d);
3365 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
3366 variable is a static const initialized in the class body. */
3367 if (TREE_CODE (d) == VAR_DECL
3368 && ! DECL_INITIAL (d) && DECL_INITIAL (pattern))
3370 lineno = DECL_SOURCE_LINE (d);
3371 input_filename = DECL_SOURCE_FILE (d);
3373 pushclass (DECL_CONTEXT (d), 2);
3374 DECL_INITIAL (d) = tsubst_expr
3375 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3376 TREE_VEC_LENGTH (args), tmpl);
3380 input_filename = file;
3383 if (! pattern_defined
3384 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
3385 && (! DECL_INTERFACE_KNOWN (d)
3386 || ! DECL_NOT_REALLY_EXTERN (d)))
3387 /* Kludge: if we compile a constructor in the middle of processing a
3388 toplevel declaration, we blow away the declspecs in
3389 temp_decl_obstack when we call permanent_allocation in
3390 finish_function. So don't compile it yet. */
3391 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
3393 add_pending_template (d);
3397 if (! push_tinst_level (d))
3400 push_to_top_level ();
3402 lineno = DECL_SOURCE_LINE (d);
3403 input_filename = DECL_SOURCE_FILE (d);
3405 /* Trick tsubst into giving us a new decl in case the template changed. */
3406 save_ti = DECL_TEMPLATE_INFO (pattern);
3407 DECL_TEMPLATE_INFO (pattern) = NULL_TREE;
3408 td = tsubst (pattern, &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), tmpl);
3409 DECL_TEMPLATE_INFO (pattern) = save_ti;
3411 /* And set up DECL_INITIAL, since tsubst doesn't. */
3412 if (TREE_CODE (td) == VAR_DECL)
3414 pushclass (DECL_CONTEXT (d), 2);
3415 DECL_INITIAL (td) = tsubst_expr
3416 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3417 TREE_VEC_LENGTH (args), tmpl);
3421 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
3422 if (TREE_CODE (d) == FUNCTION_DECL)
3423 DECL_INITIAL (td) = error_mark_node;
3424 duplicate_decls (td, d);
3425 if (TREE_CODE (d) == FUNCTION_DECL)
3426 DECL_INITIAL (td) = 0;
3428 if (TREE_CODE (d) == VAR_DECL)
3430 DECL_IN_AGGR_P (d) = 0;
3431 if (DECL_INTERFACE_KNOWN (d))
3432 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
3435 DECL_EXTERNAL (d) = 1;
3436 DECL_NOT_REALLY_EXTERN (d) = 1;
3438 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
3440 else if (TREE_CODE (d) == FUNCTION_DECL)
3442 tree t = DECL_SAVED_TREE (pattern);
3444 start_function (NULL_TREE, d, NULL_TREE, 1);
3445 store_parm_decls ();
3447 if (t && TREE_CODE (t) == RETURN_INIT)
3450 (TREE_OPERAND (t, 0),
3451 tsubst_expr (TREE_OPERAND (t, 1), &TREE_VEC_ELT (args, 0),
3452 TREE_VEC_LENGTH (args), tmpl));
3456 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
3458 current_member_init_list
3459 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
3460 current_base_init_list
3461 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
3466 /* Always keep the BLOCK node associated with the outermost
3467 pair of curley braces of a function. These are needed
3468 for correct operation of dwarfout.c. */
3471 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
3472 tsubst_expr (t, &TREE_VEC_ELT (args, 0),
3473 TREE_VEC_LENGTH (args), tmpl);
3475 finish_function (lineno, 0, nested);
3479 input_filename = file;
3481 pop_from_top_level ();
3488 tsubst_chain (t, argvec)
3493 tree first = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3494 TREE_VEC_LENGTH (argvec), NULL_TREE);
3497 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
3499 tree x = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3500 TREE_VEC_LENGTH (argvec), NULL_TREE);
3501 TREE_CHAIN (last) = x;
3511 tsubst_expr_values (t, argvec)
3514 tree first = NULL_TREE;
3517 for (; t; t = TREE_CHAIN (t))
3519 tree pur = tsubst_copy (TREE_PURPOSE (t), &TREE_VEC_ELT (argvec, 0),
3520 TREE_VEC_LENGTH (argvec), NULL_TREE);
3521 tree val = tsubst_expr (TREE_VALUE (t), &TREE_VEC_ELT (argvec, 0),
3522 TREE_VEC_LENGTH (argvec), NULL_TREE);
3523 *p = build_tree_list (pur, val);
3524 p = &TREE_CHAIN (*p);
3535 last_tree = TREE_CHAIN (last_tree) = t;
3538 /* D is an undefined function declaration in the presence of templates with
3539 the same name, listed in FNS. If one of them can produce D as an
3540 instantiation, remember this so we can instantiate it at EOF if D has
3541 not been defined by that time. */
3544 add_maybe_template (d, fns)
3549 if (DECL_MAYBE_TEMPLATE (d))
3552 t = most_specialized (fns, d);
3555 if (t == error_mark_node)
3557 cp_error ("ambiguous template instantiation for `%D'", d);
3561 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
3562 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
3563 DECL_MAYBE_TEMPLATE (d) = 1;