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;
1128 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1129 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1130 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1131 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1132 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1133 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1134 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1135 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1136 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1137 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1138 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1139 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1140 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1141 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1142 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1143 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1144 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1145 TYPE_USES_COMPLEX_INHERITANCE (type)
1146 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1147 TYPE_USES_MULTIPLE_INHERITANCE (type)
1148 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1149 TYPE_USES_VIRTUAL_BASECLASSES (type)
1150 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1151 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1152 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1155 tree binfo = TYPE_BINFO (type);
1156 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1162 int len = TREE_VEC_LENGTH (pbases);
1163 BINFO_BASETYPES (binfo) = bases = make_tree_vec (len);
1164 for (i = 0; i < len; ++i)
1168 TREE_VEC_ELT (bases, i) = elt
1169 = tsubst (TREE_VEC_ELT (pbases, i), &TREE_VEC_ELT (args, 0),
1170 TREE_VEC_LENGTH (args), NULL_TREE);
1171 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1173 if (! uses_template_parms (type) &&
1174 TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE)
1175 cp_error ("base class `%T' of `%T' has incomplete type",
1176 TREE_TYPE (elt), type);
1181 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1183 field_chain = &TYPE_FIELDS (type);
1185 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1187 tree name = TREE_PURPOSE (t);
1188 tree tag = TREE_VALUE (t);
1191 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1192 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1193 newtag = start_enum (name);
1195 newtag = tsubst (tag, &TREE_VEC_ELT (args, 0),
1196 TREE_VEC_LENGTH (args), NULL_TREE);
1198 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1200 tree e, values = NULL_TREE, *last = &values;
1202 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
1204 tree elt = build_enumerator
1206 tsubst_expr (TREE_VALUE (e), &TREE_VEC_ELT (args, 0),
1207 TREE_VEC_LENGTH (args), NULL_TREE));
1208 DECL_FIELD_CONTEXT (TREE_VALUE (elt)) = type;
1210 last = &TREE_CHAIN (elt);
1213 finish_enum (newtag, values);
1215 *field_chain = grok_enum_decls (newtag, NULL_TREE);
1216 while (*field_chain)
1217 field_chain = &TREE_CHAIN (*field_chain);
1221 /* Don't replace enum constants here. */
1222 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1223 if (TREE_CODE (t) != CONST_DECL)
1225 tree r = tsubst (t, &TREE_VEC_ELT (args, 0),
1226 TREE_VEC_LENGTH (args), NULL_TREE);
1227 if (TREE_CODE (r) == VAR_DECL)
1229 if (! uses_template_parms (r))
1230 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1231 /* Perhaps I should do more of grokfield here. */
1233 DECL_IN_AGGR_P (r) = 1;
1234 DECL_EXTERNAL (r) = 1;
1235 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1239 field_chain = &TREE_CHAIN (r);
1242 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1243 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
1245 if (DECL_CONSTRUCTOR_P (t))
1246 grok_ctor_properties (type, t);
1247 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
1248 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
1251 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1252 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1253 &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), NULL_TREE);
1256 tree d = CLASSTYPE_FRIEND_CLASSES (type) =
1257 tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), &TREE_VEC_ELT (args, 0),
1258 TREE_VEC_LENGTH (args), NULL_TREE);
1260 /* This does injection for friend classes. */
1261 for (; d; d = TREE_CHAIN (d))
1262 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1264 d = tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args, 0),
1265 TREE_VEC_LENGTH (args), NULL_TREE);
1267 for (; d; d = TREE_CHAIN (d))
1269 tree t = TREE_VALUE (d);
1271 if (TREE_CODE (t) == TYPE_DECL)
1272 /* Already injected. */;
1278 if (! uses_template_parms (type))
1281 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1282 if (TREE_CODE (tmp) == FIELD_DECL)
1284 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1285 require_complete_type (tmp);
1288 type = finish_struct_1 (type, 0);
1289 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1290 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1291 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1293 repo_template_used (type);
1297 TYPE_SIZE (type) = integer_zero_node;
1298 CLASSTYPE_METHOD_VEC (type)
1299 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1302 TYPE_BEING_DEFINED (type) = 0;
1305 pop_from_top_level ();
1315 if (t1 == NULL_TREE)
1316 return t2 == NULL_TREE;
1317 if (t2 == NULL_TREE)
1319 /* Don't care if one declares its arg const and the other doesn't -- the
1320 main variant of the arg type is all that matters. */
1321 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1322 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1324 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1328 lookup_nested_type_by_name (ctype, name)
1333 complete_type (ctype);
1335 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
1337 if (name == TREE_PURPOSE (t))
1338 return TREE_VALUE (t);
1344 tsubst (t, args, nargs, in_decl)
1351 if (t == NULL_TREE || t == error_mark_node
1352 || t == integer_type_node
1353 || t == void_type_node
1354 || t == char_type_node)
1357 type = TREE_TYPE (t);
1358 if (type == unknown_type_node)
1359 my_friendly_abort (42);
1360 if (type && TREE_CODE (t) != FUNCTION_DECL)
1361 type = tsubst (type, args, nargs, in_decl);
1363 switch (TREE_CODE (t))
1366 if (TYPE_PTRMEMFUNC_P (t))
1368 tree r = build_ptrmemfunc_type
1369 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1370 return cp_build_type_variant (r, TYPE_READONLY (t),
1374 /* else fall through */
1376 if (uses_template_parms (t))
1378 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
1379 tree r = lookup_template_class (t, argvec, in_decl);
1380 return cp_build_type_variant (r, TYPE_READONLY (t),
1384 /* else fall through */
1386 case IDENTIFIER_NODE:
1398 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1399 if (ctx == NULL_TREE)
1402 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
1406 if (t == integer_type_node)
1409 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1410 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1414 tree max = tsubst_expr (TYPE_MAX_VALUE (t), args, nargs, in_decl);
1415 if (processing_template_decl)
1417 tree itype = make_node (INTEGER_TYPE);
1418 TYPE_MIN_VALUE (itype) = size_zero_node;
1419 TYPE_MAX_VALUE (itype) = max;
1422 return build_index_2_type (size_zero_node, max);
1425 case TEMPLATE_TYPE_PARM:
1427 tree arg = args[TEMPLATE_TYPE_IDX (t)];
1428 return cp_build_type_variant
1429 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
1430 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
1433 case TEMPLATE_CONST_PARM:
1434 return args[TEMPLATE_CONST_IDX (t)];
1439 tree arg_types, ctx;
1443 if (DECL_CONTEXT (t) != NULL_TREE
1444 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1446 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
1450 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1451 type = tsubst (type, args, nargs, in_decl);
1457 type = tsubst (type, args, nargs, in_decl);
1460 if (type == TREE_TYPE (t)
1461 && (! member || ctx == DECL_CLASS_CONTEXT (t)))
1468 /* Do we already have this instantiation? */
1469 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1471 tree tmpl = TREE_PURPOSE (DECL_TEMPLATE_INFO (t));
1472 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1474 for (; decls; decls = TREE_CHAIN (decls))
1475 if (TREE_TYPE (TREE_VALUE (decls)) == type
1476 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
1477 return TREE_VALUE (decls);
1480 /* We do NOT check for matching decls pushed separately at this
1481 point, as they may not represent instantiations of this
1482 template, and in any case are considered separate under the
1487 TREE_TYPE (r) = type;
1490 = tsubst (DECL_CONTEXT (t), args, nargs, t);
1491 DECL_CLASS_CONTEXT (r) = ctx;
1493 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
1494 IDENTIFIER_POINTER (DECL_NAME (r)),
1495 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1497 /* Type-conversion operator. Reconstruct the name, in
1498 case it's the name of one of the template's parameters. */
1499 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
1502 arg_types = TYPE_VALUES (type);
1504 if (member && TREE_CODE (type) == FUNCTION_TYPE)
1505 arg_types = hash_tree_chain
1506 (build_pointer_type (DECL_CONTEXT (r)), arg_types);
1508 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
1510 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
1511 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
1512 buf = (char *) alloca (strlen (dbuf)
1513 + sizeof (DESTRUCTOR_DECL_PREFIX));
1514 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
1517 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
1520 DECL_ASSEMBLER_NAME (r)
1521 = build_decl_overload (DECL_NAME (r), arg_types, member);
1523 make_decl_rtl (r, NULL_PTR, 1);
1525 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1526 DECL_MAIN_VARIANT (r) = r;
1527 DECL_RESULT (r) = NULL_TREE;
1528 DECL_INITIAL (r) = NULL_TREE;
1530 TREE_STATIC (r) = 0;
1531 TREE_PUBLIC (r) = 1;
1532 DECL_EXTERNAL (r) = 1;
1533 DECL_INTERFACE_KNOWN (r) = 0;
1534 DECL_DEFER_OUTPUT (r) = 0;
1535 TREE_CHAIN (r) = NULL_TREE;
1536 DECL_CHAIN (r) = NULL_TREE;
1538 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
1539 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
1541 /* Look for matching decls for the moment. */
1544 tree decls = lookup_name_nonclass (DECL_NAME (t));
1547 if (decls == NULL_TREE)
1549 else if (is_overloaded_fn (decls))
1550 for (decls = get_first_fn (decls); decls;
1551 decls = DECL_CHAIN (decls))
1553 if (TREE_CODE (decls) == FUNCTION_DECL
1554 && TREE_TYPE (decls) == type)
1563 int dcl_only = ! DECL_INITIAL (d);
1565 DECL_INITIAL (r) = error_mark_node;
1566 duplicate_decls (r, d);
1569 DECL_INITIAL (r) = 0;
1573 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1575 tree tmpl = DECL_TI_TEMPLATE (t);
1576 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1577 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1578 args, nargs, in_decl);
1580 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1581 *declsp = perm_tree_cons (argvec, r, *declsp);
1583 /* If we have a preexisting version of this function, don't expand
1584 the template version, use the other instead. */
1585 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
1586 SET_DECL_TEMPLATE_SPECIALIZATION (r);
1588 SET_DECL_IMPLICIT_INSTANTIATION (r);
1590 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1591 tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1594 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
1595 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
1597 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
1598 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
1605 tree r = copy_node (t);
1606 TREE_TYPE (r) = type;
1607 DECL_INITIAL (r) = TREE_TYPE (r);
1608 DECL_CONTEXT (r) = NULL_TREE;
1609 #ifdef PROMOTE_PROTOTYPES
1610 if ((TREE_CODE (type) == INTEGER_TYPE
1611 || TREE_CODE (type) == ENUMERAL_TYPE)
1612 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
1613 DECL_ARG_TYPE (r) = integer_type_node;
1616 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1622 tree r = copy_node (t);
1623 TREE_TYPE (r) = type;
1626 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
1628 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
1629 TREE_CHAIN (r) = NULL_TREE;
1635 tree r = copy_node (t);
1637 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
1638 TREE_CHAIN (r) = NULL_TREE;
1645 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
1647 /* Do we already have this instantiation? */
1648 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1650 tree tmpl = DECL_TI_TEMPLATE (t);
1651 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1653 for (; decls; decls = TREE_CHAIN (decls))
1654 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
1655 return TREE_VALUE (decls);
1659 TREE_TYPE (r) = type;
1660 DECL_CONTEXT (r) = ctx;
1661 if (TREE_STATIC (r))
1662 DECL_ASSEMBLER_NAME (r)
1663 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
1665 /* Don't try to expand the initializer until someone tries to use
1666 this variable; otherwise we run into circular dependencies. */
1667 DECL_INITIAL (r) = NULL_TREE;
1672 if (DECL_LANG_SPECIFIC (r))
1675 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
1678 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1680 tree tmpl = DECL_TI_TEMPLATE (t);
1681 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1682 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1683 args, nargs, in_decl);
1685 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1686 *declsp = perm_tree_cons (argvec, r, *declsp);
1687 SET_DECL_IMPLICIT_INSTANTIATION (r);
1689 TREE_CHAIN (r) = NULL_TREE;
1694 if (t == TYPE_NAME (TREE_TYPE (t)))
1695 return TYPE_NAME (type);
1698 tree r = copy_node (t);
1699 TREE_TYPE (r) = type;
1700 DECL_CONTEXT (r) = current_class_type;
1701 TREE_CHAIN (r) = NULL_TREE;
1707 tree purpose, value, chain, result;
1708 int via_public, via_virtual, via_protected;
1710 if (t == void_list_node)
1713 via_public = TREE_VIA_PUBLIC (t);
1714 via_protected = TREE_VIA_PROTECTED (t);
1715 via_virtual = TREE_VIA_VIRTUAL (t);
1717 purpose = TREE_PURPOSE (t);
1719 purpose = tsubst (purpose, args, nargs, in_decl);
1720 value = TREE_VALUE (t);
1722 value = tsubst (value, args, nargs, in_decl);
1723 chain = TREE_CHAIN (t);
1724 if (chain && chain != void_type_node)
1725 chain = tsubst (chain, args, nargs, in_decl);
1726 if (purpose == TREE_PURPOSE (t)
1727 && value == TREE_VALUE (t)
1728 && chain == TREE_CHAIN (t))
1730 result = hash_tree_cons (via_public, via_virtual, via_protected,
1731 purpose, value, chain);
1732 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1736 if (type != NULL_TREE)
1740 if (type == TREE_TYPE (t))
1743 TREE_TYPE (t) = complete_type (type);
1744 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
1745 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
1746 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
1747 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
1752 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1753 tree *elts = (tree *) alloca (len * sizeof (tree));
1755 bzero ((char *) elts, len * sizeof (tree));
1757 for (i = 0; i < len; i++)
1759 elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
1760 if (elts[i] != TREE_VEC_ELT (t, i))
1767 t = make_tree_vec (len);
1768 for (i = 0; i < len; i++)
1769 TREE_VEC_ELT (t, i) = elts[i];
1774 case REFERENCE_TYPE:
1777 enum tree_code code;
1778 if (type == TREE_TYPE (t))
1781 code = TREE_CODE (t);
1782 if (code == POINTER_TYPE)
1783 r = build_pointer_type (type);
1785 r = build_reference_type (type);
1786 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
1787 /* Will this ever be needed for TYPE_..._TO values? */
1792 return build_offset_type
1793 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
1797 tree values = TYPE_ARG_TYPES (t);
1798 tree context = TYPE_CONTEXT (t);
1799 tree raises = TYPE_RAISES_EXCEPTIONS (t);
1802 /* Don't bother recursing if we know it won't change anything. */
1803 if (values != void_list_node)
1805 /* This should probably be rewritten to use hash_tree_cons for
1806 the memory savings. */
1807 tree first = NULL_TREE;
1810 for (; values && values != void_list_node;
1811 values = TREE_CHAIN (values))
1813 tree value = TYPE_MAIN_VARIANT (type_decays_to
1814 (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
1815 tree purpose = tsubst_expr (TREE_PURPOSE (values),
1816 args, nargs, in_decl);
1817 tree x = build_tree_list (purpose, value);
1820 TREE_CHAIN (last) = x;
1826 if (values == void_list_node)
1827 TREE_CHAIN (last) = void_list_node;
1832 context = tsubst (context, args, nargs, in_decl);
1833 /* Could also optimize cases where return value and
1834 values have common elements (e.g., T min(const &T, const T&). */
1836 /* If the above parameters haven't changed, just return the type. */
1837 if (type == TREE_TYPE (t)
1838 && values == TYPE_VALUES (t)
1839 && context == TYPE_CONTEXT (t))
1842 /* Construct a new type node and return it. */
1843 if (TREE_CODE (t) == FUNCTION_TYPE
1844 && context == NULL_TREE)
1846 fntype = build_function_type (type, values);
1848 else if (context == NULL_TREE)
1850 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1851 args, nargs, in_decl);
1852 fntype = build_cplus_method_type (base, type,
1853 TREE_CHAIN (values));
1857 fntype = make_node (TREE_CODE (t));
1858 TREE_TYPE (fntype) = type;
1859 TYPE_CONTEXT (fntype) = context;
1860 TYPE_VALUES (fntype) = values;
1861 TYPE_SIZE (fntype) = TYPE_SIZE (t);
1862 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
1863 TYPE_MODE (fntype) = TYPE_MODE (t);
1864 if (TYPE_METHOD_BASETYPE (t))
1865 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
1866 args, nargs, in_decl);
1867 /* Need to generate hash value. */
1868 my_friendly_abort (84);
1870 fntype = build_type_variant (fntype,
1875 raises = tsubst (raises, args, nargs, in_decl);
1876 fntype = build_exception_variant (fntype, raises);
1882 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1884 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1886 r = build_cplus_array_type (type, domain);
1892 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1893 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1894 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1898 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1899 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1903 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1904 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
1905 return cp_build_type_variant
1906 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
1907 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
1911 return make_pointer_declarator
1912 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1915 return make_reference_declarator
1916 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1919 return build_parse_node
1920 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1921 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
1924 return make_call_declarator
1925 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1926 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
1927 TREE_OPERAND (t, 2),
1928 tsubst (TREE_TYPE (t), args, nargs, in_decl));
1931 return build_parse_node
1932 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1933 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
1936 sorry ("use of `%s' in template",
1937 tree_code_name [(int) TREE_CODE (t)]);
1938 return error_mark_node;
1945 emit_line_note (input_filename, lineno);
1949 expand_start_bindings (0);
1957 expand_end_bindings (getdecls (), kept_level_p (), 1);
1958 t = poplevel (kept_level_p (), 1, 0);
1964 tsubst_copy (t, args, nargs, in_decl)
1969 enum tree_code code;
1971 if (t == NULL_TREE || t == error_mark_node)
1974 code = TREE_CODE (t);
1979 return do_identifier (DECL_NAME (t), 0);
1983 if (DECL_CONTEXT (t))
1985 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
1986 if (ctx != DECL_CONTEXT (t))
1987 return lookup_field (ctx, DECL_NAME (t), 0, 0);
1993 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1994 t = tsubst (t, args, nargs, in_decl);
1999 case IDENTIFIER_NODE:
2000 return do_identifier (t, 0);
2004 case REINTERPRET_CAST_EXPR:
2005 case CONST_CAST_EXPR:
2006 case STATIC_CAST_EXPR:
2007 case DYNAMIC_CAST_EXPR:
2009 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2010 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2013 case PREDECREMENT_EXPR:
2014 case PREINCREMENT_EXPR:
2015 case POSTDECREMENT_EXPR:
2016 case POSTINCREMENT_EXPR:
2018 case TRUTH_NOT_EXPR:
2020 case CONVERT_EXPR: /* Unary + */
2027 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2032 case TRUNC_DIV_EXPR:
2034 case FLOOR_DIV_EXPR:
2035 case ROUND_DIV_EXPR:
2036 case EXACT_DIV_EXPR:
2038 case BIT_ANDTC_EXPR:
2041 case TRUNC_MOD_EXPR:
2042 case FLOOR_MOD_EXPR:
2043 case TRUTH_ANDIF_EXPR:
2044 case TRUTH_ORIF_EXPR:
2045 case TRUTH_AND_EXPR:
2066 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2067 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2071 tree fn = TREE_OPERAND (t, 0);
2072 if (really_overloaded_fn (fn))
2073 fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
2075 fn = tsubst_copy (fn, args, nargs, in_decl);
2077 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2081 case METHOD_CALL_EXPR:
2083 tree name = TREE_OPERAND (t, 0);
2084 if (TREE_CODE (name) == BIT_NOT_EXPR)
2086 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2087 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2089 else if (TREE_CODE (name) == SCOPE_REF
2090 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
2092 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2093 name = TREE_OPERAND (name, 1);
2094 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2095 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2096 name = build_nt (SCOPE_REF, base, name);
2099 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
2101 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2102 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));
2116 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2117 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2118 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
2119 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
2126 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2127 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2128 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
2129 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
2135 tree purpose, value, chain;
2137 if (t == void_list_node)
2140 purpose = TREE_PURPOSE (t);
2142 purpose = tsubst_copy (purpose, args, nargs, in_decl);
2143 value = TREE_VALUE (t);
2145 value = tsubst_copy (value, args, nargs, in_decl);
2146 chain = TREE_CHAIN (t);
2147 if (chain && chain != void_type_node)
2148 chain = tsubst_copy (chain, args, nargs, in_decl);
2149 if (purpose == TREE_PURPOSE (t)
2150 && value == TREE_VALUE (t)
2151 && chain == TREE_CHAIN (t))
2153 return tree_cons (purpose, value, chain);
2160 case TEMPLATE_TYPE_PARM:
2161 case TEMPLATE_CONST_PARM:
2163 case REFERENCE_TYPE:
2169 return tsubst (t, args, nargs, in_decl);
2171 case IDENTIFIER_NODE:
2172 if (IDENTIFIER_TYPENAME_P (t))
2173 return build_typename_overload
2174 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
2180 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
2181 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
2189 tsubst_expr (t, args, nargs, in_decl)
2194 if (t == NULL_TREE || t == error_mark_node)
2197 if (processing_template_decl)
2198 return tsubst_copy (t, args, nargs, in_decl);
2200 switch (TREE_CODE (t))
2203 lineno = TREE_COMPLEXITY (t);
2204 emit_line_note (input_filename, lineno);
2206 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2211 lineno = TREE_COMPLEXITY (t);
2212 emit_line_note (input_filename, lineno);
2213 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2214 /* Do default conversion if safe and possibly important,
2215 in case within ({...}). */
2216 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
2217 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2218 t = default_conversion (t);
2219 cplus_expand_expr_stmt (t);
2226 int i = suspend_momentary ();
2229 lineno = TREE_COMPLEXITY (t);
2230 emit_line_note (input_filename, lineno);
2232 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2233 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2234 TREE_OPERAND (t, 2) != 0);
2235 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2237 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
2238 resume_momentary (i);
2245 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
2246 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2247 int cond_scope = (TREE_OPERAND (t, 1)
2248 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
2250 lineno = TREE_COMPLEXITY (t);
2251 emit_line_note (input_filename, lineno);
2254 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
2255 tsubst_expr (tmp, args, nargs, in_decl);
2257 emit_line_note (input_filename, lineno);
2258 expand_start_loop_continue_elsewhere (1);
2262 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2263 emit_line_note (input_filename, lineno);
2265 expand_exit_loop_if_false (0, condition_conversion (tmp));
2269 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
2272 emit_line_note (input_filename, lineno);
2273 expand_loop_continue_here ();
2274 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2276 cplus_expand_expr_stmt (tmp);
2289 lineno = TREE_COMPLEXITY (t);
2291 emit_line_note (input_filename, lineno);
2292 expand_start_loop (1);
2294 cond = TREE_OPERAND (t, 0);
2295 if (TREE_CODE (cond) == DECL_STMT)
2297 cond = tsubst_expr (cond, args, nargs, in_decl);
2298 emit_line_note (input_filename, lineno);
2299 expand_exit_loop_if_false (0, condition_conversion (cond));
2301 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
2303 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2315 lineno = TREE_COMPLEXITY (t);
2317 emit_line_note (input_filename, lineno);
2318 expand_start_loop_continue_elsewhere (1);
2320 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2321 expand_loop_continue_here ();
2323 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2324 emit_line_note (input_filename, lineno);
2325 expand_exit_loop_if_false (0, condition_conversion (cond));
2336 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2338 lineno = TREE_COMPLEXITY (t);
2341 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2342 emit_line_note (input_filename, lineno);
2343 expand_start_cond (condition_conversion (tmp), 0);
2345 if (tmp = TREE_OPERAND (t, 1), tmp)
2346 tsubst_expr (tmp, args, nargs, in_decl);
2348 if (tmp = TREE_OPERAND (t, 2), tmp)
2350 expand_start_else ();
2351 tsubst_expr (tmp, args, nargs, in_decl);
2365 tree substmt = TREE_OPERAND (t, 0);
2367 lineno = TREE_COMPLEXITY (t);
2369 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2372 for (; substmt; substmt = TREE_CHAIN (substmt))
2373 tsubst_expr (substmt, args, nargs, in_decl);
2375 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2381 lineno = TREE_COMPLEXITY (t);
2382 emit_line_note (input_filename, lineno);
2383 if (! expand_exit_something ())
2384 error ("break statement not within loop or switch");
2388 lineno = TREE_COMPLEXITY (t);
2389 emit_line_note (input_filename, lineno);
2390 if (! expand_continue_loop (0))
2391 error ("continue statement not within a loop");
2397 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2399 lineno = TREE_COMPLEXITY (t);
2402 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2403 emit_line_note (input_filename, lineno);
2404 c_expand_start_case (val);
2407 if (tmp = TREE_OPERAND (t, 1), tmp)
2408 tsubst_expr (tmp, args, nargs, in_decl);
2410 expand_end_case (val);
2421 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
2422 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2426 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
2433 lineno = TREE_COMPLEXITY (t);
2434 emit_line_note (input_filename, lineno);
2435 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
2437 tree decl = lookup_label (TREE_OPERAND (t, 0));
2438 TREE_USED (decl) = 1;
2442 expand_computed_goto
2443 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2447 lineno = TREE_COMPLEXITY (t);
2448 emit_line_note (input_filename, lineno);
2449 expand_start_try_stmts ();
2450 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2451 expand_start_all_catch ();
2453 tree handler = TREE_OPERAND (t, 1);
2454 for (; handler; handler = TREE_CHAIN (handler))
2455 tsubst_expr (handler, args, nargs, in_decl);
2457 expand_end_all_catch ();
2461 lineno = TREE_COMPLEXITY (t);
2463 if (TREE_OPERAND (t, 0))
2465 tree d = TREE_OPERAND (t, 0);
2466 expand_start_catch_block
2467 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
2468 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
2471 expand_start_catch_block (NULL_TREE, NULL_TREE);
2472 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2473 expand_end_catch_block ();
2478 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
2484 instantiate_template (tmpl, targ_ptr)
2485 tree tmpl, *targ_ptr;
2489 struct obstack *old_fmp_obstack;
2490 extern struct obstack *function_maybepermanent_obstack;
2492 push_obstacks (&permanent_obstack, &permanent_obstack);
2493 old_fmp_obstack = function_maybepermanent_obstack;
2494 function_maybepermanent_obstack = &permanent_obstack;
2496 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
2497 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
2502 tree t = targ_ptr [i];
2503 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2505 tree nt = target_type (t);
2506 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
2508 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
2509 cp_error (" trying to instantiate `%D'", tmpl);
2510 fndecl = error_mark_node;
2514 targ_ptr[i] = copy_to_permanent (t);
2517 /* substitute template parameters */
2518 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
2521 function_maybepermanent_obstack = old_fmp_obstack;
2527 /* Push the name of the class template into the scope of the instantiation. */
2530 overload_template_name (type)
2533 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
2536 if (IDENTIFIER_CLASS_VALUE (id)
2537 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
2540 decl = build_decl (TYPE_DECL, id, type);
2541 SET_DECL_ARTIFICIAL (decl);
2542 pushdecl_class_level (decl);
2545 /* Type unification.
2547 We have a function template signature with one or more references to
2548 template parameters, and a parameter list we wish to fit to this
2549 template. If possible, produce a list of parameters for the template
2550 which will cause it to fit the supplied parameter list.
2552 Return zero for success, 2 for an incomplete match that doesn't resolve
2553 all the types, and 1 for complete failure. An error message will be
2554 printed only for an incomplete match.
2556 TPARMS[NTPARMS] is an array of template parameter types;
2557 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2558 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2559 and ARGS is the argument list we're trying to match against it.
2561 If SUBR is 1, we're being called recursively (to unify the arguments of
2562 a function or method parameter of a function template), so don't zero
2563 out targs and don't fail on an incomplete match.
2565 If STRICT is 1, the match must be exact (for casts of overloaded
2566 addresses, explicit instantiation, and more_specialized). */
2569 type_unification (tparms, targs, parms, args, nsubsts, subr, strict)
2570 tree tparms, *targs, parms, args;
2571 int *nsubsts, subr, strict;
2575 int ntparms = TREE_VEC_LENGTH (tparms);
2577 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
2578 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
2579 /* ARGS could be NULL (via a call from parse.y to
2580 build_x_function_call). */
2582 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
2583 my_friendly_assert (ntparms > 0, 292);
2586 bzero ((char *) targs, sizeof (tree) * ntparms);
2589 && parms != void_list_node
2591 && args != void_list_node)
2593 parm = TREE_VALUE (parms);
2594 parms = TREE_CHAIN (parms);
2595 arg = TREE_VALUE (args);
2596 args = TREE_CHAIN (args);
2598 if (arg == error_mark_node)
2600 if (arg == unknown_type_node)
2603 if (! uses_template_parms (parm)
2604 && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2606 if (can_convert_arg (parm, TREE_TYPE (arg), arg))
2612 if (TREE_CODE (arg) == VAR_DECL)
2613 arg = TREE_TYPE (arg);
2614 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
2615 arg = TREE_TYPE (arg);
2617 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2619 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
2620 if (TREE_CODE (arg) == TREE_LIST
2621 && TREE_TYPE (arg) == unknown_type_node
2622 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
2624 int nsubsts, ntparms;
2627 /* Have to back unify here */
2628 arg = TREE_VALUE (arg);
2630 ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
2631 targs = (tree *) alloca (sizeof (tree) * ntparms);
2632 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
2633 return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
2634 TYPE_ARG_TYPES (TREE_TYPE (arg)),
2635 parm, &nsubsts, 0, strict);
2637 arg = TREE_TYPE (arg);
2640 if (TREE_CODE (arg) == REFERENCE_TYPE)
2641 arg = TREE_TYPE (arg);
2643 if (TREE_CODE (parm) != REFERENCE_TYPE)
2645 if (TREE_CODE (arg) == FUNCTION_TYPE
2646 || TREE_CODE (arg) == METHOD_TYPE)
2647 arg = build_pointer_type (arg);
2648 else if (TREE_CODE (arg) == ARRAY_TYPE)
2649 arg = build_pointer_type (TREE_TYPE (arg));
2651 arg = TYPE_MAIN_VARIANT (arg);
2654 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
2662 /* Fail if we've reached the end of the parm list, and more args
2663 are present, and the parm list isn't variadic. */
2664 if (args && args != void_list_node && parms == void_list_node)
2666 /* Fail if parms are left and they don't have default values. */
2668 && parms != void_list_node
2669 && TREE_PURPOSE (parms) == NULL_TREE)
2672 for (i = 0; i < ntparms; i++)
2675 error ("incomplete type unification");
2681 /* Tail recursion is your friend. */
2684 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
2685 tree tparms, *targs, parm, arg;
2686 int *nsubsts, ntparms, strict;
2690 /* I don't think this will do the right thing with respect to types.
2691 But the only case I've seen it in so far has been array bounds, where
2692 signedness is the only information lost, and I think that will be
2694 while (TREE_CODE (parm) == NOP_EXPR)
2695 parm = TREE_OPERAND (parm, 0);
2697 if (arg == error_mark_node)
2699 if (arg == unknown_type_node)
2704 switch (TREE_CODE (parm))
2706 case TEMPLATE_TYPE_PARM:
2708 idx = TEMPLATE_TYPE_IDX (parm);
2709 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
2710 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
2713 /* Template type parameters cannot contain cv-quals; i.e.
2714 template <class T> void f (T& a, T& b) will not generate
2715 void f (const int& a, const int& b). */
2716 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
2717 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
2719 arg = TYPE_MAIN_VARIANT (arg);
2722 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
2723 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
2724 arg = cp_build_type_variant (arg, constp, volatilep);
2727 /* Simple cases: Value already set, does match or doesn't. */
2728 if (targs[idx] == arg)
2730 else if (targs[idx])
2732 /* Check for mixed types and values. */
2733 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
2737 case TEMPLATE_CONST_PARM:
2739 idx = TEMPLATE_CONST_IDX (parm);
2740 if (targs[idx] == arg)
2742 else if (targs[idx])
2744 tree t = targs[idx];
2745 if (TREE_CODE (t) == TREE_CODE (arg))
2746 switch (TREE_CODE (arg))
2749 if (tree_int_cst_equal (t, arg))
2753 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2756 /* STRING_CST values are not valid template const parms. */
2760 my_friendly_abort (87);
2763 /* else if (typeof arg != tparms[idx])
2766 targs[idx] = copy_to_permanent (arg);
2770 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
2771 return unify (tparms, targs, ntparms, parm,
2772 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
2774 if (TREE_CODE (arg) != POINTER_TYPE)
2776 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2779 case REFERENCE_TYPE:
2780 if (TREE_CODE (arg) == REFERENCE_TYPE)
2781 arg = TREE_TYPE (arg);
2782 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
2786 if (TREE_CODE (arg) != ARRAY_TYPE)
2788 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2789 nsubsts, strict) != 0)
2791 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2796 if (TREE_CODE (arg) != TREE_CODE (parm))
2799 if (TREE_CODE (parm) == INTEGER_TYPE)
2801 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2802 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
2803 TYPE_MIN_VALUE (arg), nsubsts, strict))
2805 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2806 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
2807 TYPE_MAX_VALUE (arg), nsubsts, strict))
2810 /* As far as unification is concerned, this wins. Later checks
2811 will invalidate it if necessary. */
2814 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2816 if (TREE_CODE (arg) != INTEGER_CST)
2818 return !tree_int_cst_equal (parm, arg);
2823 t1 = TREE_OPERAND (parm, 0);
2824 t2 = TREE_OPERAND (parm, 1);
2825 return unify (tparms, targs, ntparms, t1,
2826 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2833 if (TREE_CODE (arg) != TREE_VEC)
2835 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2837 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2838 if (unify (tparms, targs, ntparms,
2839 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2846 if (TYPE_PTRMEMFUNC_FLAG (parm))
2847 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2848 arg, nsubsts, strict);
2850 /* Allow trivial conversions. */
2851 if (TREE_CODE (arg) != RECORD_TYPE
2852 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2853 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2856 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
2859 if (flag_ansi_overloading && ! strict)
2860 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
2862 (CLASSTYPE_TEMPLATE_INFO (arg)
2863 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
2865 if (! t || t == error_mark_node)
2868 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
2869 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
2871 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
2876 if (TREE_CODE (arg) != METHOD_TYPE)
2881 if (TREE_CODE (arg) != FUNCTION_TYPE)
2884 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
2885 TREE_TYPE (arg), nsubsts, strict))
2887 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2888 TYPE_ARG_TYPES (arg), nsubsts, 1, strict);
2891 if (TREE_CODE (arg) != OFFSET_TYPE)
2893 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2894 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
2896 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2897 TREE_TYPE (arg), nsubsts, strict);
2900 sorry ("use of `%s' in template type unification",
2901 tree_code_name [(int) TREE_CODE (parm)]);
2907 mark_decl_instantiated (result, extern_p)
2911 if (DECL_TEMPLATE_INSTANTIATION (result))
2912 SET_DECL_EXPLICIT_INSTANTIATION (result);
2913 TREE_PUBLIC (result) = 1;
2917 DECL_INTERFACE_KNOWN (result) = 1;
2918 DECL_NOT_REALLY_EXTERN (result) = 1;
2920 else if (TREE_CODE (result) == FUNCTION_DECL)
2921 mark_inline_for_output (result);
2924 /* Given two function templates PAT1 and PAT2, return:
2926 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
2927 -1 if PAT2 is more specialized than PAT1.
2928 0 if neither is more specialized. */
2931 more_specialized (pat1, pat2)
2937 targs = get_bindings (pat1, pat2);
2944 targs = get_bindings (pat2, pat1);
2954 /* Given two class template specialization list nodes PAT1 and PAT2, return:
2956 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
2957 -1 if PAT2 is more specialized than PAT1.
2958 0 if neither is more specialized. */
2961 more_specialized_class (pat1, pat2)
2967 targs = get_class_bindings
2968 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
2972 targs = get_class_bindings
2973 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
2980 /* Return the template arguments that will produce the function signature
2981 DECL from the function template FN. */
2984 get_bindings (fn, decl)
2987 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2988 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
2990 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2991 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2992 TYPE_ARG_TYPES (TREE_TYPE (decl)),
3001 get_class_bindings (tparms, parms, args)
3002 tree tparms, parms, args;
3004 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
3005 tree vec = make_temp_vec (ntparms);
3007 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
3009 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
3010 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
3020 for (i = 0; i < ntparms; ++i)
3021 if (! TREE_VEC_ELT (vec, i))
3027 /* Return the most specialized of the list of templates in FNS that can
3028 produce an instantiation matching DECL. */
3031 most_specialized (fns, decl)
3034 tree fn, champ, *args, *p;
3037 for (p = &fns; *p; )
3039 args = get_bindings (TREE_VALUE (*p), decl);
3043 p = &TREE_CHAIN (*p);
3046 *p = TREE_CHAIN (*p);
3053 champ = TREE_VALUE (fn);
3054 fn = TREE_CHAIN (fn);
3055 for (; fn; fn = TREE_CHAIN (fn))
3057 fate = more_specialized (champ, TREE_VALUE (fn));
3064 fn = TREE_CHAIN (fn);
3066 return error_mark_node;
3068 champ = TREE_VALUE (fn);
3072 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
3074 fate = more_specialized (champ, TREE_VALUE (fn));
3076 return error_mark_node;
3082 /* Return the most specialized of the class template specializations in
3083 SPECS that can produce an instantiation matching ARGS. */
3086 most_specialized_class (specs, mainargs)
3087 tree specs, mainargs;
3089 tree list = NULL_TREE, t, args, champ;
3092 for (t = specs; t; t = TREE_CHAIN (t))
3094 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
3097 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
3098 TREE_TYPE (list) = TREE_TYPE (t);
3108 for (; t; t = TREE_CHAIN (t))
3110 fate = more_specialized_class (champ, t);
3119 return error_mark_node;
3125 for (t = list; t && t != champ; t = TREE_CHAIN (t))
3127 fate = more_specialized (champ, t);
3129 return error_mark_node;
3135 /* called from the parser. */
3138 do_function_instantiation (declspecs, declarator, storage)
3139 tree declspecs, declarator, storage;
3141 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
3144 tree result = NULL_TREE;
3147 if (! DECL_LANG_SPECIFIC (decl))
3149 cp_error ("explicit instantiation of non-template `%#D'", decl);
3153 /* If we've already seen this template instance, use it. */
3154 if (DECL_FUNCTION_MEMBER_P (decl))
3156 if (DECL_TEMPLATE_INSTANTIATION (decl))
3158 else if (name = DECL_ASSEMBLER_NAME (decl),
3159 fn = IDENTIFIER_GLOBAL_VALUE (name),
3160 fn && DECL_TEMPLATE_INSTANTIATION (fn))
3163 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
3165 tree templates = NULL_TREE;
3166 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
3167 if (decls_match (fn, decl)
3168 && DECL_DEFER_OUTPUT (fn))
3173 else if (TREE_CODE (fn) == TEMPLATE_DECL)
3174 templates = decl_tree_cons (NULL_TREE, fn, templates);
3179 result = most_specialized (templates, decl);
3180 if (result == error_mark_node)
3182 char *str = "candidates are:";
3183 cp_error ("ambiguous template instantiation for `%D' requested", decl);
3184 for (fn = templates; fn; fn = TREE_CHAIN (fn))
3186 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
3193 args = get_bindings (result, decl);
3194 result = instantiate_template (result, args);
3201 cp_error ("no matching template for `%D' found", decl);
3205 if (flag_external_templates)
3208 if (storage == NULL_TREE)
3210 else if (storage == ridpointers[(int) RID_EXTERN])
3213 cp_error ("storage class `%D' applied to template instantiation",
3216 mark_decl_instantiated (result, extern_p);
3217 repo_template_instantiated (result, extern_p);
3219 instantiate_decl (result);
3223 mark_class_instantiated (t, extern_p)
3227 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
3228 SET_CLASSTYPE_INTERFACE_KNOWN (t);
3229 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
3230 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
3231 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
3234 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
3235 rest_of_type_compilation (t, 1);
3240 do_type_instantiation (name, storage)
3243 tree t = TREE_TYPE (name);
3250 /* With -fexternal-templates, explicit instantiations are treated the same
3251 as implicit ones. */
3252 if (flag_external_templates)
3255 if (TYPE_SIZE (t) == NULL_TREE)
3257 cp_error ("explicit instantiation of `%#T' before definition of template",
3262 if (storage == NULL_TREE)
3264 else if (storage == ridpointers[(int) RID_INLINE])
3266 else if (storage == ridpointers[(int) RID_EXTERN])
3268 else if (storage == ridpointers[(int) RID_STATIC])
3272 cp_error ("storage class `%D' applied to template instantiation",
3277 /* We've already instantiated this. */
3278 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
3282 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3284 mark_class_instantiated (t, extern_p);
3285 repo_template_instantiated (t, extern_p);
3295 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
3296 if (DECL_TEMPLATE_INSTANTIATION (tmp))
3298 mark_decl_instantiated (tmp, extern_p);
3299 repo_template_instantiated (tmp, extern_p);
3301 instantiate_decl (tmp);
3304 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
3305 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
3307 mark_decl_instantiated (tmp, extern_p);
3308 repo_template_instantiated (tmp, extern_p);
3310 instantiate_decl (tmp);
3313 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
3314 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
3315 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
3320 instantiate_decl (d)
3323 tree ti = DECL_TEMPLATE_INFO (d);
3324 tree tmpl = TI_TEMPLATE (ti);
3325 tree args = TI_ARGS (ti);
3327 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
3329 int nested = in_function_p ();
3331 int pattern_defined;
3333 char *file = input_filename;
3335 if (TREE_CODE (d) == FUNCTION_DECL)
3337 d_defined = (DECL_INITIAL (d) != NULL_TREE);
3338 pattern_defined = (DECL_INITIAL (pattern) != NULL_TREE);
3342 d_defined = ! DECL_IN_AGGR_P (d);
3343 pattern_defined = ! DECL_IN_AGGR_P (pattern);
3348 else if (pattern_defined)
3350 repo_template_used (d);
3352 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
3354 if (flag_alt_external_templates)
3356 if (interface_unknown)
3357 warn_if_unknown_interface (d);
3359 else if (DECL_INTERFACE_KNOWN (pattern))
3361 DECL_INTERFACE_KNOWN (d) = 1;
3362 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (pattern);
3365 warn_if_unknown_interface (pattern);
3369 import_export_decl (d);
3372 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
3373 variable is a static const initialized in the class body. */
3374 if (TREE_CODE (d) == VAR_DECL
3375 && ! DECL_INITIAL (d) && DECL_INITIAL (pattern))
3377 lineno = DECL_SOURCE_LINE (d);
3378 input_filename = DECL_SOURCE_FILE (d);
3380 pushclass (DECL_CONTEXT (d), 2);
3381 DECL_INITIAL (d) = tsubst_expr
3382 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3383 TREE_VEC_LENGTH (args), tmpl);
3387 input_filename = file;
3390 if (! pattern_defined
3391 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
3392 && (! DECL_INTERFACE_KNOWN (d)
3393 || ! DECL_NOT_REALLY_EXTERN (d)))
3394 /* Kludge: if we compile a constructor in the middle of processing a
3395 toplevel declaration, we blow away the declspecs in
3396 temp_decl_obstack when we call permanent_allocation in
3397 finish_function. So don't compile it yet. */
3398 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
3400 add_pending_template (d);
3404 if (! push_tinst_level (d))
3407 push_to_top_level ();
3409 lineno = DECL_SOURCE_LINE (d);
3410 input_filename = DECL_SOURCE_FILE (d);
3412 /* Trick tsubst into giving us a new decl in case the template changed. */
3413 save_ti = DECL_TEMPLATE_INFO (pattern);
3414 DECL_TEMPLATE_INFO (pattern) = NULL_TREE;
3415 td = tsubst (pattern, &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), tmpl);
3416 DECL_TEMPLATE_INFO (pattern) = save_ti;
3418 /* And set up DECL_INITIAL, since tsubst doesn't. */
3419 if (TREE_CODE (td) == VAR_DECL)
3421 pushclass (DECL_CONTEXT (d), 2);
3422 DECL_INITIAL (td) = tsubst_expr
3423 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3424 TREE_VEC_LENGTH (args), tmpl);
3428 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
3429 if (TREE_CODE (d) == FUNCTION_DECL)
3430 DECL_INITIAL (td) = error_mark_node;
3431 duplicate_decls (td, d);
3432 if (TREE_CODE (d) == FUNCTION_DECL)
3433 DECL_INITIAL (td) = 0;
3435 if (TREE_CODE (d) == VAR_DECL)
3437 DECL_IN_AGGR_P (d) = 0;
3438 if (DECL_INTERFACE_KNOWN (d))
3439 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
3442 DECL_EXTERNAL (d) = 1;
3443 DECL_NOT_REALLY_EXTERN (d) = 1;
3445 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
3447 else if (TREE_CODE (d) == FUNCTION_DECL)
3449 tree t = DECL_SAVED_TREE (pattern);
3451 start_function (NULL_TREE, d, NULL_TREE, 1);
3452 store_parm_decls ();
3454 if (t && TREE_CODE (t) == RETURN_INIT)
3457 (TREE_OPERAND (t, 0),
3458 tsubst_expr (TREE_OPERAND (t, 1), &TREE_VEC_ELT (args, 0),
3459 TREE_VEC_LENGTH (args), tmpl));
3463 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
3465 current_member_init_list
3466 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
3467 current_base_init_list
3468 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
3473 /* Always keep the BLOCK node associated with the outermost
3474 pair of curley braces of a function. These are needed
3475 for correct operation of dwarfout.c. */
3478 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
3479 tsubst_expr (t, &TREE_VEC_ELT (args, 0),
3480 TREE_VEC_LENGTH (args), tmpl);
3482 finish_function (lineno, 0, nested);
3486 input_filename = file;
3488 pop_from_top_level ();
3495 tsubst_chain (t, argvec)
3500 tree first = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3501 TREE_VEC_LENGTH (argvec), NULL_TREE);
3504 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
3506 tree x = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3507 TREE_VEC_LENGTH (argvec), NULL_TREE);
3508 TREE_CHAIN (last) = x;
3518 tsubst_expr_values (t, argvec)
3521 tree first = NULL_TREE;
3524 for (; t; t = TREE_CHAIN (t))
3526 tree pur = tsubst_copy (TREE_PURPOSE (t), &TREE_VEC_ELT (argvec, 0),
3527 TREE_VEC_LENGTH (argvec), NULL_TREE);
3528 tree val = tsubst_expr (TREE_VALUE (t), &TREE_VEC_ELT (argvec, 0),
3529 TREE_VEC_LENGTH (argvec), NULL_TREE);
3530 *p = build_tree_list (pur, val);
3531 p = &TREE_CHAIN (*p);
3542 last_tree = TREE_CHAIN (last_tree) = t;
3545 /* D is an undefined function declaration in the presence of templates with
3546 the same name, listed in FNS. If one of them can produce D as an
3547 instantiation, remember this so we can instantiate it at EOF if D has
3548 not been defined by that time. */
3551 add_maybe_template (d, fns)
3556 if (DECL_MAYBE_TEMPLATE (d))
3559 t = most_specialized (fns, d);
3562 if (t == error_mark_node)
3564 cp_error ("ambiguous template instantiation for `%D'", d);
3568 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
3569 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
3570 DECL_MAYBE_TEMPLATE (d) = 1;