Initial revision
[official-gcc.git] / gcc / cp / pt.c
blobf43488d62cfe24a1ac98bcc5f20c34e3ca2649a2
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)
11 any later version.
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". */
31 #include "config.h"
32 #include <stdio.h>
33 #include "obstack.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "cp-tree.h"
38 #include "decl.h"
39 #include "parse.h"
40 #include "lex.h"
41 #include "output.h"
42 #include "defaults.h"
44 extern struct obstack permanent_obstack;
46 extern int lineno;
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;
56 tree maybe_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
64 static int unify ();
66 void pop_template_decls ();
68 static tree classtype_mangled_name ();
69 static char * mangle_class_name_for_template ();
70 static tree tsubst_expr_values ();
71 static int comp_template_args PROTO((tree, tree));
72 tree most_specialized_class PROTO((tree, tree));
73 static tree get_class_bindings PROTO((tree, tree, tree));
74 tree make_temp_vec PROTO((int));
75 static tree tsubst_enum PROTO((tree, tree *, int));
77 /* We've got a template header coming up; push to a new level for storing
78 the parms. */
80 void
81 begin_template_parm_list ()
83 pushlevel (0);
84 declare_pseudo_global_level ();
85 ++processing_template_decl;
88 /* Process information from new template parameter NEXT and append it to the
89 LIST being built. */
91 tree
92 process_template_parm (list, next)
93 tree list, next;
95 tree parm;
96 tree decl = 0;
97 tree defval;
98 int is_type, idx;
99 parm = next;
100 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
101 defval = TREE_PURPOSE (parm);
102 parm = TREE_VALUE (parm);
103 is_type = TREE_PURPOSE (parm) == class_type_node;
105 if (list)
107 tree p = TREE_VALUE (tree_last (list));
109 if (TREE_CODE (p) == TYPE_DECL)
110 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
111 else
112 idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
113 ++idx;
115 else
116 idx = 0;
118 if (!is_type)
120 tree tinfo = 0;
121 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
122 /* is a const-param */
123 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
124 PARM, 0, NULL_TREE);
125 /* A template parameter is not modifiable. */
126 TREE_READONLY (parm) = 1;
127 if (IS_AGGR_TYPE (TREE_TYPE (parm))
128 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
130 cp_error ("`%#T' is not a valid type for a template constant parameter",
131 TREE_TYPE (parm));
132 if (DECL_NAME (parm) == NULL_TREE)
133 error (" a template type parameter must begin with `class' or `typename'");
134 TREE_TYPE (parm) = void_type_node;
136 else if (pedantic
137 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
138 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
139 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
140 TREE_TYPE (parm));
141 tinfo = make_node (TEMPLATE_CONST_PARM);
142 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
143 if (TREE_PERMANENT (parm) == 0)
145 parm = copy_node (parm);
146 TREE_PERMANENT (parm) = 1;
148 TREE_TYPE (tinfo) = TREE_TYPE (parm);
149 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
150 DECL_INITIAL (decl) = tinfo;
151 DECL_INITIAL (parm) = tinfo;
152 TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl);
154 else
156 tree t = make_lang_type (TEMPLATE_TYPE_PARM);
157 CLASSTYPE_GOT_SEMICOLON (t) = 1;
158 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
159 TYPE_NAME (t) = decl;
160 TYPE_STUB_DECL (t) = decl;
161 parm = decl;
162 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
164 SET_DECL_ARTIFICIAL (decl);
165 pushdecl (decl);
166 parm = build_tree_list (defval, parm);
167 return chainon (list, parm);
170 /* The end of a template parameter list has been reached. Process the
171 tree list into a parameter vector, converting each parameter into a more
172 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
173 as PARM_DECLs. */
175 tree
176 end_template_parm_list (parms)
177 tree parms;
179 int nparms;
180 tree parm;
181 tree saved_parmlist = make_tree_vec (list_length (parms));
183 current_template_parms
184 = tree_cons (build_int_2 (0, processing_template_decl),
185 saved_parmlist, current_template_parms);
187 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
188 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
190 return saved_parmlist;
193 /* end_template_decl is called after a template declaration is seen. */
195 void
196 end_template_decl ()
198 if (! processing_template_decl)
199 return;
201 /* This matches the pushlevel in begin_template_parm_list. */
202 poplevel (0, 0, 0);
204 --processing_template_decl;
205 current_template_parms = TREE_CHAIN (current_template_parms);
206 (void) get_pending_sizes (); /* Why? */
209 /* Generate a valid set of template args from current_template_parms. */
211 tree
212 current_template_args ()
214 tree header = current_template_parms;
215 tree args = NULL_TREE;
216 while (header)
218 tree a = copy_node (TREE_VALUE (header));
219 int i = TREE_VEC_LENGTH (a);
220 TREE_TYPE (a) = NULL_TREE;
221 while (i--)
223 tree t = TREE_VALUE (TREE_VEC_ELT (a, i));
224 if (TREE_CODE (t) == TYPE_DECL)
225 t = TREE_TYPE (t);
226 else
227 t = DECL_INITIAL (t);
228 TREE_VEC_ELT (a, i) = t;
230 args = tree_cons (TREE_PURPOSE (header), a, args);
231 header = TREE_CHAIN (header);
233 args = nreverse (args);
235 /* FIXME Remove this when we support member templates. */
236 args = TREE_VALUE (args);
238 return args;
241 void
242 push_template_decl (decl)
243 tree decl;
245 tree tmpl;
246 tree args = NULL_TREE;
247 tree info;
248 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
249 int primary = 0;
251 /* Kludge! */
252 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
253 && DECL_CLASS_CONTEXT (decl))
255 /* Note that this template is a "primary template" */
256 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
257 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
258 primary = 1;
260 /* Partial specialization. */
261 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
262 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
264 tree type = TREE_TYPE (decl);
265 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
266 tree mainargs = CLASSTYPE_TI_ARGS (type);
267 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
269 for (; spec; spec = TREE_CHAIN (spec))
271 /* purpose: args to main template
272 value: spec template */
273 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
274 return;
277 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
278 = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms),
279 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
280 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
281 return;
284 args = current_template_args ();
286 if (! ctx || TYPE_BEING_DEFINED (ctx))
288 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
289 DECL_TEMPLATE_PARMS (tmpl) = TREE_VALUE (current_template_parms);
290 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
292 else
294 tree t;
296 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
297 cp_error ("must specialize `%#T' before defining member `%#D'",
298 ctx, decl);
299 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
300 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
301 else if (! DECL_TEMPLATE_INFO (decl))
303 cp_error ("template definition of non-template `%#D'", decl);
304 return;
306 else
307 tmpl = DECL_TI_TEMPLATE (decl);
309 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
310 t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
311 else
312 t = DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));
314 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (args))
316 cp_error ("got %d template parameters for `%#D'",
317 TREE_VEC_LENGTH (args), decl);
318 cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
322 DECL_TEMPLATE_RESULT (tmpl) = decl;
323 TREE_TYPE (tmpl) = TREE_TYPE (decl);
325 if (! ctx)
326 tmpl = pushdecl_top_level (tmpl);
328 if (primary)
329 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl)) = tmpl;
331 info = perm_tree_cons (tmpl, args, NULL_TREE);
333 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
335 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
336 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
338 else if (! DECL_LANG_SPECIFIC (decl))
339 cp_error ("template declaration of `%#D'", decl);
340 else
341 DECL_TEMPLATE_INFO (decl) = info;
344 tree tsubst PROTO ((tree, tree*, int, tree));
346 /* Convert all template arguments to their appropriate types, and return
347 a vector containing the resulting values. If any error occurs, return
348 error_mark_node. */
350 static tree
351 coerce_template_parms (parms, arglist, in_decl)
352 tree parms, arglist;
353 tree in_decl;
355 int nparms, nargs, i, lost = 0;
356 tree vec;
358 if (arglist == NULL_TREE)
359 nargs = 0;
360 else if (TREE_CODE (arglist) == TREE_VEC)
361 nargs = TREE_VEC_LENGTH (arglist);
362 else
363 nargs = list_length (arglist);
365 nparms = TREE_VEC_LENGTH (parms);
367 if (nargs > nparms
368 || (nargs < nparms
369 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
371 error ("incorrect number of parameters (%d, should be %d)",
372 nargs, nparms);
373 if (in_decl)
374 cp_error_at ("in template expansion for decl `%D'", in_decl);
375 return error_mark_node;
378 if (arglist && TREE_CODE (arglist) == TREE_VEC)
379 vec = copy_node (arglist);
380 else
382 vec = make_tree_vec (nparms);
383 for (i = 0; i < nparms; i++)
385 tree arg;
387 if (arglist)
389 arg = arglist;
390 arglist = TREE_CHAIN (arglist);
392 if (arg == error_mark_node)
393 lost++;
394 else
395 arg = TREE_VALUE (arg);
397 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i)))
398 == TYPE_DECL)
399 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
400 &TREE_VEC_ELT (vec, 0), i, in_decl);
401 else
402 arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
403 &TREE_VEC_ELT (vec, 0), i, in_decl);
405 TREE_VEC_ELT (vec, i) = arg;
408 for (i = 0; i < nparms; i++)
410 tree arg = TREE_VEC_ELT (vec, i);
411 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
412 tree val = 0;
413 int is_type, requires_type;
415 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
416 requires_type = TREE_CODE (parm) == TYPE_DECL;
418 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
419 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
421 cp_pedwarn ("to refer to a type member of a template parameter,");
422 cp_pedwarn (" use `typename %E'", arg);
423 arg = make_typename_type (TREE_OPERAND (arg, 0),
424 TREE_OPERAND (arg, 1));
425 is_type = 1;
427 if (is_type != requires_type)
429 if (in_decl)
431 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
432 i, in_decl);
433 if (is_type)
434 cp_error (" expected a constant of type `%T', got `%T'",
435 TREE_TYPE (parm), arg);
436 else
437 cp_error (" expected a type, got `%E'", arg);
439 lost++;
440 TREE_VEC_ELT (vec, i) = error_mark_node;
441 continue;
443 if (is_type)
445 val = groktypename (arg);
446 if (! processing_template_decl)
448 tree t = target_type (val);
449 if (IS_AGGR_TYPE (t)
450 && decl_function_context (TYPE_MAIN_DECL (t)))
452 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
453 return error_mark_node;
457 else
459 tree t = tsubst (TREE_TYPE (parm), &TREE_VEC_ELT (vec, 0),
460 TREE_VEC_LENGTH (vec), in_decl);
461 if (processing_template_decl)
462 val = arg;
463 else
464 val = digest_init (t, arg, (tree *) 0);
466 if (val == error_mark_node || processing_template_decl)
469 /* 14.2: Other template-arguments must be constant-expressions,
470 addresses of objects or functions with external linkage, or of
471 static class members. */
472 else if (IS_AGGR_TYPE (TREE_TYPE (val)))
474 cp_error ("object `%E' cannot be used as template argument", arg);
475 val = error_mark_node;
477 else if (!TREE_CONSTANT (val))
479 cp_error ("non-const `%E' cannot be used as template argument",
480 arg);
481 val = error_mark_node;
483 else if (POINTER_TYPE_P (TREE_TYPE (val))
484 && ! integer_zerop (val)
485 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
486 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
488 t = val;
489 STRIP_NOPS (t);
490 if (TREE_CODE (t) == ADDR_EXPR)
492 tree a = TREE_OPERAND (t, 0);
493 STRIP_NOPS (a);
494 if (TREE_CODE (a) == STRING_CST)
496 cp_error ("string literal %E is not a valid template argument", a);
497 error ("because it is the address of an object with static linkage");
498 val = error_mark_node;
500 else if (TREE_CODE (a) != VAR_DECL
501 && TREE_CODE (a) != FUNCTION_DECL)
502 goto bad;
503 else if (! DECL_PUBLIC (a))
505 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
506 val = error_mark_node;
509 else
511 bad:
512 cp_error ("`%E' is not a valid template argument", t);
513 error ("it must be %s%s with external linkage",
514 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
515 ? "a pointer to " : "",
516 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
517 ? "a function" : "an object");
518 val = error_mark_node;
523 if (val == error_mark_node)
524 lost++;
526 TREE_VEC_ELT (vec, i) = val;
528 if (lost)
529 return error_mark_node;
530 return vec;
533 static int
534 comp_template_args (oldargs, newargs)
535 tree oldargs, newargs;
537 int i;
539 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
541 tree nt = TREE_VEC_ELT (newargs, i);
542 tree ot = TREE_VEC_ELT (oldargs, i);
544 if (nt == ot)
545 continue;
546 if (TREE_CODE (nt) != TREE_CODE (ot))
547 return 0;
548 if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
550 if (comptypes (ot, nt, 1))
551 continue;
553 else if (cp_tree_equal (ot, nt) > 0)
554 continue;
555 return 0;
557 return 1;
560 /* Given class template name and parameter list, produce a user-friendly name
561 for the instantiation. */
563 static char *
564 mangle_class_name_for_template (name, parms, arglist)
565 char *name;
566 tree parms, arglist;
568 static struct obstack scratch_obstack;
569 static char *scratch_firstobj;
570 int i, nparms;
572 if (!scratch_firstobj)
573 gcc_obstack_init (&scratch_obstack);
574 else
575 obstack_free (&scratch_obstack, scratch_firstobj);
576 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
578 #if 0
579 #define buflen sizeof(buf)
580 #define check if (bufp >= buf+buflen-1) goto too_long
581 #define ccat(c) *bufp++=(c); check
582 #define advance bufp+=strlen(bufp); check
583 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
584 #else
585 #define check
586 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
587 #define advance
588 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
589 #endif
591 cat (name);
592 ccat ('<');
593 nparms = TREE_VEC_LENGTH (parms);
594 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
595 for (i = 0; i < nparms; i++)
597 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
598 tree arg = TREE_VEC_ELT (arglist, i);
600 if (i)
601 ccat (',');
603 if (TREE_CODE (parm) == TYPE_DECL)
605 cat (type_as_string (arg, 0));
606 continue;
608 else
609 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
611 if (TREE_CODE (arg) == TREE_LIST)
613 /* New list cell was built because old chain link was in
614 use. */
615 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
616 arg = TREE_VALUE (arg);
618 /* No need to check arglist against parmlist here; we did that
619 in coerce_template_parms, called from lookup_template_class. */
620 cat (expr_as_string (arg, 0));
623 char *bufp = obstack_next_free (&scratch_obstack);
624 int offset = 0;
625 while (bufp[offset - 1] == ' ')
626 offset--;
627 obstack_blank_fast (&scratch_obstack, offset);
629 /* B<C<char> >, not B<C<char>> */
630 if (bufp[offset - 1] == '>')
631 ccat (' ');
633 ccat ('>');
634 ccat ('\0');
635 return (char *) obstack_base (&scratch_obstack);
637 #if 0
638 too_long:
639 #endif
640 fatal ("out of (preallocated) string space creating template instantiation name");
641 /* NOTREACHED */
642 return NULL;
645 static tree
646 classtype_mangled_name (t)
647 tree t;
649 if (CLASSTYPE_TEMPLATE_INFO (t)
650 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
652 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
653 char *mangled_name = mangle_class_name_for_template
654 (IDENTIFIER_POINTER (name),
655 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
656 CLASSTYPE_TI_ARGS (t));
657 tree id = get_identifier (mangled_name);
658 IDENTIFIER_TEMPLATE (id) = name;
659 return id;
661 else
662 return TYPE_IDENTIFIER (t);
665 static void
666 add_pending_template (d)
667 tree d;
669 tree ti;
671 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
672 ti = CLASSTYPE_TEMPLATE_INFO (d);
673 else
674 ti = DECL_TEMPLATE_INFO (d);
676 if (TI_PENDING_TEMPLATE_FLAG (ti))
677 return;
679 *template_tail = perm_tree_cons
680 (current_function_decl, d, NULL_TREE);
681 template_tail = &TREE_CHAIN (*template_tail);
682 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
685 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
686 parameters, find the desired type.
688 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
689 Since ARGLIST is build on the decl_obstack, we must copy it here
690 to keep it from being reclaimed when the decl storage is reclaimed.
692 IN_DECL, if non-NULL, is the template declaration we are trying to
693 instantiate. */
695 tree
696 lookup_template_class (d1, arglist, in_decl)
697 tree d1, arglist;
698 tree in_decl;
700 tree template, parmlist;
701 char *mangled_name;
702 tree id, t;
704 if (TREE_CODE (d1) == IDENTIFIER_NODE)
706 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
707 if (! template)
708 template = IDENTIFIER_CLASS_VALUE (d1);
710 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
712 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
713 d1 = DECL_NAME (template);
715 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
717 template = CLASSTYPE_TI_TEMPLATE (d1);
718 d1 = DECL_NAME (template);
720 else
721 my_friendly_abort (272);
723 /* With something like `template <class T> class X class X { ... };'
724 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
725 We don't want to do that, but we have to deal with the situation, so
726 let's give them some syntax errors to chew on instead of a crash. */
727 if (! template)
728 return error_mark_node;
729 if (TREE_CODE (template) != TEMPLATE_DECL)
731 cp_error ("non-template type `%T' used as a template", d1);
732 if (in_decl)
733 cp_error_at ("for template declaration `%D'", in_decl);
734 return error_mark_node;
737 if (PRIMARY_TEMPLATE_P (template))
739 parmlist = DECL_TEMPLATE_PARMS (template);
741 arglist = coerce_template_parms (parmlist, arglist, template);
742 if (arglist == error_mark_node)
743 return error_mark_node;
744 if (uses_template_parms (arglist))
746 tree found;
747 if (comp_template_args
748 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
749 found = TREE_TYPE (template);
750 else
752 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
753 found; found = TREE_CHAIN (found))
755 if (TI_USES_TEMPLATE_PARMS (found)
756 && comp_template_args (TREE_PURPOSE (found), arglist))
757 break;
759 if (found)
760 found = TREE_VALUE (found);
763 if (found)
765 if (can_free (&permanent_obstack, arglist))
766 obstack_free (&permanent_obstack, arglist);
767 return found;
771 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
772 parmlist, arglist);
773 id = get_identifier (mangled_name);
774 IDENTIFIER_TEMPLATE (id) = d1;
776 maybe_push_to_top_level (uses_template_parms (arglist));
777 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
778 pop_from_top_level ();
780 else
782 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
783 arglist, in_decl);
784 id = d1;
785 arglist = CLASSTYPE_TI_ARGS (ctx);
787 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
789 int save_temp = processing_template_decl;
790 processing_template_decl = 0;
791 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
792 processing_template_decl = save_temp;
794 else
796 t = lookup_nested_type_by_name (ctx, id);
797 my_friendly_assert (t != NULL_TREE, 42);
801 /* Seems to be wanted. */
802 CLASSTYPE_GOT_SEMICOLON (t) = 1;
804 if (! CLASSTYPE_TEMPLATE_INFO (t))
806 arglist = copy_to_permanent (arglist);
807 CLASSTYPE_TEMPLATE_INFO (t)
808 = perm_tree_cons (template, arglist, NULL_TREE);
809 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
810 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
811 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
812 = uses_template_parms (arglist);
814 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
816 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
817 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
818 if (! uses_template_parms (arglist))
819 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
820 = get_identifier (build_overload_name (t, 1, 1));
822 if (flag_external_templates && ! uses_template_parms (arglist)
823 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
824 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
825 add_pending_template (t);
828 return t;
831 /* Should be defined in parse.h. */
832 extern int yychar;
835 uses_template_parms (t)
836 tree t;
838 if (!t)
839 return 0;
840 switch (TREE_CODE (t))
842 case INDIRECT_REF:
843 case COMPONENT_REF:
844 /* We assume that the object must be instantiated in order to build
845 the COMPONENT_REF, so we test only whether the type of the
846 COMPONENT_REF uses template parms. */
847 return uses_template_parms (TREE_TYPE (t));
849 case IDENTIFIER_NODE:
850 if (!IDENTIFIER_TEMPLATE (t))
851 return 0;
852 my_friendly_abort (42);
854 /* aggregates of tree nodes */
855 case TREE_VEC:
857 int i = TREE_VEC_LENGTH (t);
858 while (i--)
859 if (uses_template_parms (TREE_VEC_ELT (t, i)))
860 return 1;
861 return 0;
863 case TREE_LIST:
864 if (uses_template_parms (TREE_PURPOSE (t))
865 || uses_template_parms (TREE_VALUE (t)))
866 return 1;
867 return uses_template_parms (TREE_CHAIN (t));
869 /* constructed type nodes */
870 case POINTER_TYPE:
871 case REFERENCE_TYPE:
872 return uses_template_parms (TREE_TYPE (t));
873 case RECORD_TYPE:
874 if (TYPE_PTRMEMFUNC_FLAG (t))
875 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
876 case UNION_TYPE:
877 if (! CLASSTYPE_TEMPLATE_INFO (t))
878 return 0;
879 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
880 case FUNCTION_TYPE:
881 if (uses_template_parms (TYPE_ARG_TYPES (t)))
882 return 1;
883 return uses_template_parms (TREE_TYPE (t));
884 case ARRAY_TYPE:
885 if (uses_template_parms (TYPE_DOMAIN (t)))
886 return 1;
887 return uses_template_parms (TREE_TYPE (t));
888 case OFFSET_TYPE:
889 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
890 return 1;
891 return uses_template_parms (TREE_TYPE (t));
892 case METHOD_TYPE:
893 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
894 return 1;
895 if (uses_template_parms (TYPE_ARG_TYPES (t)))
896 return 1;
897 return uses_template_parms (TREE_TYPE (t));
899 /* decl nodes */
900 case TYPE_DECL:
901 return uses_template_parms (TREE_TYPE (t));
903 case FUNCTION_DECL:
904 case VAR_DECL:
905 /* ??? What about FIELD_DECLs? */
906 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
907 && uses_template_parms (DECL_TI_ARGS (t)))
908 return 1;
909 /* fall through */
910 case CONST_DECL:
911 case PARM_DECL:
912 if (uses_template_parms (TREE_TYPE (t)))
913 return 1;
914 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
915 return 1;
916 return 0;
918 case CALL_EXPR:
919 return uses_template_parms (TREE_TYPE (t));
920 case ADDR_EXPR:
921 return uses_template_parms (TREE_OPERAND (t, 0));
923 /* template parm nodes */
924 case TEMPLATE_TYPE_PARM:
925 case TEMPLATE_CONST_PARM:
926 return 1;
928 /* simple type nodes */
929 case INTEGER_TYPE:
930 if (uses_template_parms (TYPE_MIN_VALUE (t)))
931 return 1;
932 return uses_template_parms (TYPE_MAX_VALUE (t));
934 case REAL_TYPE:
935 case COMPLEX_TYPE:
936 case VOID_TYPE:
937 case ENUMERAL_TYPE:
938 case BOOLEAN_TYPE:
939 return 0;
941 /* constants */
942 case INTEGER_CST:
943 case REAL_CST:
944 case STRING_CST:
945 return 0;
947 case ERROR_MARK:
948 /* Non-error_mark_node ERROR_MARKs are bad things. */
949 my_friendly_assert (t == error_mark_node, 274);
950 /* NOTREACHED */
951 return 0;
953 case LOOKUP_EXPR:
954 case TYPENAME_TYPE:
955 return 1;
957 case SCOPE_REF:
958 return uses_template_parms (TREE_OPERAND (t, 0));
960 case CONSTRUCTOR:
961 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
962 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
963 return uses_template_parms (TREE_OPERAND (t, 1));
965 case MODOP_EXPR:
966 case CAST_EXPR:
967 case REINTERPRET_CAST_EXPR:
968 case CONST_CAST_EXPR:
969 case STATIC_CAST_EXPR:
970 case DYNAMIC_CAST_EXPR:
971 case SIZEOF_EXPR:
972 case ARROW_EXPR:
973 case DOTSTAR_EXPR:
974 case TYPEID_EXPR:
975 return 1;
977 default:
978 switch (TREE_CODE_CLASS (TREE_CODE (t)))
980 case '1':
981 case '2':
982 case 'e':
983 case '<':
985 int i;
986 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
987 if (uses_template_parms (TREE_OPERAND (t, i)))
988 return 1;
989 return 0;
991 default:
992 break;
994 sorry ("testing %s for template parms",
995 tree_code_name [(int) TREE_CODE (t)]);
996 my_friendly_abort (82);
997 /* NOTREACHED */
998 return 0;
1002 static struct tinst_level *current_tinst_level = 0;
1003 static struct tinst_level *free_tinst_level = 0;
1004 static int tinst_depth = 0;
1005 extern int max_tinst_depth;
1006 #ifdef GATHER_STATISTICS
1007 int depth_reached = 0;
1008 #endif
1010 static int
1011 push_tinst_level (d)
1012 tree d;
1014 struct tinst_level *new;
1016 if (tinst_depth >= max_tinst_depth)
1018 struct tinst_level *p = current_tinst_level;
1019 int line = lineno;
1020 char *file = input_filename;
1022 error ("template instantiation depth exceeds maximum of %d",
1023 max_tinst_depth);
1024 error (" (use -ftemplate-depth-NN to increase the maximum)");
1025 cp_error (" instantiating `%D'", d);
1027 for (; p; p = p->next)
1029 cp_error (" instantiated from `%D'", p->decl);
1030 lineno = p->line;
1031 input_filename = p->file;
1033 error (" instantiated from here");
1035 lineno = line;
1036 input_filename = file;
1038 return 0;
1041 if (free_tinst_level)
1043 new = free_tinst_level;
1044 free_tinst_level = new->next;
1046 else
1047 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1049 new->decl = d;
1050 new->line = lineno;
1051 new->file = input_filename;
1052 new->next = current_tinst_level;
1053 current_tinst_level = new;
1055 ++tinst_depth;
1056 #ifdef GATHER_STATISTICS
1057 if (tinst_depth > depth_reached)
1058 depth_reached = tinst_depth;
1059 #endif
1061 return 1;
1064 void
1065 pop_tinst_level ()
1067 struct tinst_level *old = current_tinst_level;
1069 current_tinst_level = old->next;
1070 old->next = free_tinst_level;
1071 free_tinst_level = old;
1072 --tinst_depth;
1075 struct tinst_level *
1076 tinst_for_decl ()
1078 struct tinst_level *p = current_tinst_level;
1080 if (p)
1081 for (; p->next ; p = p->next )
1083 return p;
1086 tree
1087 instantiate_class_template (type)
1088 tree type;
1090 tree template, template_info, args, pattern, t, *field_chain;
1092 if (type == error_mark_node)
1093 return error_mark_node;
1095 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1097 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1098 return type;
1100 template = TI_TEMPLATE (template_info);
1101 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1102 args = TI_ARGS (template_info);
1104 t = most_specialized_class
1105 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1107 if (t == error_mark_node)
1109 char *str = "candidates are:";
1110 cp_error ("ambiguous class template instantiation for `%#T'", type);
1111 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1113 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1115 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1116 str = " ";
1119 TYPE_BEING_DEFINED (type) = 1;
1120 return error_mark_node;
1122 else if (t)
1123 pattern = TREE_TYPE (t);
1124 else
1125 pattern = TREE_TYPE (template);
1127 if (TYPE_SIZE (pattern) == NULL_TREE)
1128 return type;
1130 if (t)
1131 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1133 TYPE_BEING_DEFINED (type) = 1;
1135 if (! push_tinst_level (type))
1136 return type;
1138 maybe_push_to_top_level (uses_template_parms (type));
1139 pushclass (type, 0);
1141 if (flag_external_templates)
1143 if (flag_alt_external_templates)
1145 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1146 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1147 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1148 = ! CLASSTYPE_INTERFACE_ONLY (type)
1149 && CLASSTYPE_INTERFACE_KNOWN (type);
1151 else
1153 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1154 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1155 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1156 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1157 = ! CLASSTYPE_INTERFACE_ONLY (type)
1158 && CLASSTYPE_INTERFACE_KNOWN (type);
1161 else
1163 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1164 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
1167 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1168 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1169 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1170 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1171 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1172 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1173 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1174 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1175 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1176 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1177 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1178 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1179 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1180 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1181 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1182 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1183 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1184 TYPE_USES_COMPLEX_INHERITANCE (type)
1185 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1186 TYPE_USES_MULTIPLE_INHERITANCE (type)
1187 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1188 TYPE_USES_VIRTUAL_BASECLASSES (type)
1189 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1190 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1191 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1194 tree binfo = TYPE_BINFO (type);
1195 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1197 if (pbases)
1199 tree bases;
1200 int i;
1201 int len = TREE_VEC_LENGTH (pbases);
1202 bases = make_tree_vec (len);
1203 for (i = 0; i < len; ++i)
1205 tree elt;
1207 TREE_VEC_ELT (bases, i) = elt
1208 = tsubst (TREE_VEC_ELT (pbases, i), &TREE_VEC_ELT (args, 0),
1209 TREE_VEC_LENGTH (args), NULL_TREE);
1210 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1212 if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
1213 cp_error
1214 ("base type `%T' of `%T' fails to be a struct or class type",
1215 TREE_TYPE (elt), type);
1216 else if (! uses_template_parms (type)
1217 && (TYPE_SIZE (complete_type (TREE_TYPE (elt)))
1218 == NULL_TREE))
1219 cp_error ("base class `%T' of `%T' has incomplete type",
1220 TREE_TYPE (elt), type);
1222 /* Don't initialize this until the vector is filled out, or
1223 lookups will crash. */
1224 BINFO_BASETYPES (binfo) = bases;
1228 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1230 field_chain = &TYPE_FIELDS (type);
1232 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1234 tree name = TREE_PURPOSE (t);
1235 tree tag = TREE_VALUE (t);
1237 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1238 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1240 tree e, newtag = tsubst_enum (tag, &TREE_VEC_ELT (args, 0),
1241 TREE_VEC_LENGTH (args));
1243 *field_chain = grok_enum_decls (newtag, NULL_TREE);
1244 while (*field_chain)
1246 DECL_FIELD_CONTEXT (*field_chain) = type;
1247 field_chain = &TREE_CHAIN (*field_chain);
1250 else
1251 tsubst (tag, &TREE_VEC_ELT (args, 0),
1252 TREE_VEC_LENGTH (args), NULL_TREE);
1255 /* Don't replace enum constants here. */
1256 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1257 if (TREE_CODE (t) != CONST_DECL)
1259 tree r = tsubst (t, &TREE_VEC_ELT (args, 0),
1260 TREE_VEC_LENGTH (args), NULL_TREE);
1261 if (TREE_CODE (r) == VAR_DECL)
1263 if (! uses_template_parms (r))
1264 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1265 /* Perhaps I should do more of grokfield here. */
1266 start_decl_1 (r);
1267 DECL_IN_AGGR_P (r) = 1;
1268 DECL_EXTERNAL (r) = 1;
1269 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1272 *field_chain = r;
1273 field_chain = &TREE_CHAIN (r);
1276 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1277 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
1279 if (DECL_CONSTRUCTOR_P (t))
1280 grok_ctor_properties (type, t);
1281 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
1282 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
1285 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1286 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1287 &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), NULL_TREE);
1290 tree d = CLASSTYPE_FRIEND_CLASSES (type)
1291 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), &TREE_VEC_ELT (args, 0),
1292 TREE_VEC_LENGTH (args), NULL_TREE);
1294 /* This does injection for friend classes. */
1295 for (; d; d = TREE_CHAIN (d))
1296 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1298 d = tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args, 0),
1299 TREE_VEC_LENGTH (args), NULL_TREE);
1301 for (; d; d = TREE_CHAIN (d))
1303 tree t = TREE_VALUE (d);
1305 if (TREE_CODE (t) == TYPE_DECL)
1306 /* Already injected. */;
1307 else
1308 pushdecl (t);
1312 if (! uses_template_parms (type))
1314 tree tmp;
1315 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1316 if (TREE_CODE (tmp) == FIELD_DECL)
1318 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1319 require_complete_type (tmp);
1322 type = finish_struct_1 (type, 0);
1323 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1324 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1325 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1327 repo_template_used (type);
1329 else
1331 TYPE_SIZE (type) = integer_zero_node;
1332 CLASSTYPE_METHOD_VEC (type)
1333 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1336 TYPE_BEING_DEFINED (type) = 0;
1337 popclass (0);
1339 pop_from_top_level ();
1340 pop_tinst_level ();
1342 return type;
1345 static int
1346 list_eq (t1, t2)
1347 tree t1, t2;
1349 if (t1 == NULL_TREE)
1350 return t2 == NULL_TREE;
1351 if (t2 == NULL_TREE)
1352 return 0;
1353 /* Don't care if one declares its arg const and the other doesn't -- the
1354 main variant of the arg type is all that matters. */
1355 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1356 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1357 return 0;
1358 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1361 tree
1362 lookup_nested_type_by_name (ctype, name)
1363 tree ctype, name;
1365 tree t;
1367 complete_type (ctype);
1369 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
1371 if (name == TREE_PURPOSE (t))
1372 return TREE_VALUE (t);
1374 return NULL_TREE;
1377 tree
1378 tsubst (t, args, nargs, in_decl)
1379 tree t, *args;
1380 int nargs;
1381 tree in_decl;
1383 tree type;
1385 if (t == NULL_TREE || t == error_mark_node
1386 || t == integer_type_node
1387 || t == void_type_node
1388 || t == char_type_node)
1389 return t;
1391 type = TREE_TYPE (t);
1392 if (type == unknown_type_node)
1393 my_friendly_abort (42);
1394 if (type && TREE_CODE (t) != FUNCTION_DECL
1395 && TREE_CODE (t) != TYPENAME_TYPE)
1396 type = tsubst (type, args, nargs, in_decl);
1398 switch (TREE_CODE (t))
1400 case RECORD_TYPE:
1401 if (TYPE_PTRMEMFUNC_P (t))
1403 tree r = build_ptrmemfunc_type
1404 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1405 return cp_build_type_variant (r, TYPE_READONLY (t),
1406 TYPE_VOLATILE (t));
1409 /* else fall through */
1410 case UNION_TYPE:
1411 if (uses_template_parms (t))
1413 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
1414 tree r = lookup_template_class (t, argvec, in_decl);
1415 return cp_build_type_variant (r, TYPE_READONLY (t),
1416 TYPE_VOLATILE (t));
1419 /* else fall through */
1420 case ERROR_MARK:
1421 case IDENTIFIER_NODE:
1422 case OP_IDENTIFIER:
1423 case VOID_TYPE:
1424 case REAL_TYPE:
1425 case COMPLEX_TYPE:
1426 case BOOLEAN_TYPE:
1427 case INTEGER_CST:
1428 case REAL_CST:
1429 case STRING_CST:
1430 return t;
1432 case ENUMERAL_TYPE:
1434 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1435 if (ctx == NULL_TREE)
1436 return t;
1437 else if (ctx == current_function_decl)
1438 return lookup_name (TYPE_IDENTIFIER (t), 1);
1439 else
1440 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
1443 case INTEGER_TYPE:
1444 if (t == integer_type_node)
1445 return t;
1447 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1448 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1449 return t;
1452 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
1453 max = tsubst_expr (max, args, nargs, in_decl);
1454 if (processing_template_decl)
1456 tree itype = make_node (INTEGER_TYPE);
1457 TYPE_MIN_VALUE (itype) = size_zero_node;
1458 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
1459 integer_one_node);
1460 return itype;
1463 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
1464 return build_index_2_type (size_zero_node, max);
1467 case TEMPLATE_TYPE_PARM:
1469 tree arg = args[TEMPLATE_TYPE_IDX (t)];
1470 return cp_build_type_variant
1471 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
1472 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
1475 case TEMPLATE_CONST_PARM:
1476 return args[TEMPLATE_CONST_IDX (t)];
1478 case FUNCTION_DECL:
1480 tree r = NULL_TREE;
1481 tree arg_types, ctx;
1483 int member;
1485 if (DECL_CONTEXT (t) != NULL_TREE
1486 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1488 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
1489 member = 2;
1490 else
1491 member = 1;
1492 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1493 type = tsubst (type, args, nargs, in_decl);
1495 else
1497 member = 0;
1498 ctx = NULL_TREE;
1499 type = tsubst (type, args, nargs, in_decl);
1502 if (type == TREE_TYPE (t)
1503 && (! member || ctx == DECL_CLASS_CONTEXT (t)))
1505 t = copy_node (t);
1506 copy_lang_decl (t);
1507 return t;
1510 /* Do we already have this instantiation? */
1511 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1513 tree tmpl = TREE_PURPOSE (DECL_TEMPLATE_INFO (t));
1514 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1516 for (; decls; decls = TREE_CHAIN (decls))
1517 if (TREE_TYPE (TREE_VALUE (decls)) == type
1518 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
1519 return TREE_VALUE (decls);
1522 /* We do NOT check for matching decls pushed separately at this
1523 point, as they may not represent instantiations of this
1524 template, and in any case are considered separate under the
1525 discrete model. Instead, see add_maybe_template. */
1527 r = copy_node (t);
1528 copy_lang_decl (r);
1529 TREE_TYPE (r) = type;
1531 DECL_CONTEXT (r)
1532 = tsubst (DECL_CONTEXT (t), args, nargs, t);
1533 DECL_CLASS_CONTEXT (r) = ctx;
1535 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
1536 IDENTIFIER_POINTER (DECL_NAME (r)),
1537 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1539 /* Type-conversion operator. Reconstruct the name, in
1540 case it's the name of one of the template's parameters. */
1541 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
1544 arg_types = TYPE_VALUES (type);
1546 if (member && TREE_CODE (type) == FUNCTION_TYPE)
1547 arg_types = hash_tree_chain
1548 (build_pointer_type (DECL_CONTEXT (r)), arg_types);
1550 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
1552 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
1553 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
1554 buf = (char *) alloca (strlen (dbuf)
1555 + sizeof (DESTRUCTOR_DECL_PREFIX));
1556 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
1557 buf[len] = '\0';
1558 strcat (buf, dbuf);
1559 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
1561 else
1562 DECL_ASSEMBLER_NAME (r)
1563 = build_decl_overload (DECL_NAME (r), arg_types, member);
1564 DECL_RTL (r) = 0;
1565 make_decl_rtl (r, NULL_PTR, 1);
1567 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1568 DECL_MAIN_VARIANT (r) = r;
1569 DECL_RESULT (r) = NULL_TREE;
1570 DECL_INITIAL (r) = NULL_TREE;
1572 TREE_STATIC (r) = 0;
1573 TREE_PUBLIC (r) = 1;
1574 DECL_EXTERNAL (r) = 1;
1575 DECL_INTERFACE_KNOWN (r) = 0;
1576 DECL_DEFER_OUTPUT (r) = 0;
1577 TREE_CHAIN (r) = NULL_TREE;
1578 DECL_CHAIN (r) = NULL_TREE;
1580 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
1581 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
1583 /* Look for matching decls for the moment. */
1584 if (! member && ! flag_ansi_overloading)
1586 tree decls = lookup_name_nonclass (DECL_NAME (t));
1587 tree d = NULL_TREE;
1589 if (decls == NULL_TREE)
1590 /* no match */;
1591 else if (is_overloaded_fn (decls))
1592 for (decls = get_first_fn (decls); decls;
1593 decls = DECL_CHAIN (decls))
1595 if (TREE_CODE (decls) == FUNCTION_DECL
1596 && TREE_TYPE (decls) == type)
1598 d = decls;
1599 break;
1603 if (d)
1605 int dcl_only = ! DECL_INITIAL (d);
1606 if (dcl_only)
1607 DECL_INITIAL (r) = error_mark_node;
1608 duplicate_decls (r, d);
1609 r = d;
1610 if (dcl_only)
1611 DECL_INITIAL (r) = 0;
1615 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1617 tree tmpl = DECL_TI_TEMPLATE (t);
1618 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1619 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1620 args, nargs, in_decl);
1622 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1623 *declsp = perm_tree_cons (argvec, r, *declsp);
1625 /* If we have a preexisting version of this function, don't expand
1626 the template version, use the other instead. */
1627 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
1628 SET_DECL_TEMPLATE_SPECIALIZATION (r);
1629 else
1630 SET_DECL_IMPLICIT_INSTANTIATION (r);
1632 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1633 = tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1636 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
1637 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
1638 if (member
1639 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
1640 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
1642 return r;
1645 case PARM_DECL:
1647 tree r = copy_node (t);
1648 TREE_TYPE (r) = type;
1649 DECL_INITIAL (r) = TREE_TYPE (r);
1650 DECL_CONTEXT (r) = NULL_TREE;
1651 #ifdef PROMOTE_PROTOTYPES
1652 if ((TREE_CODE (type) == INTEGER_TYPE
1653 || TREE_CODE (type) == ENUMERAL_TYPE)
1654 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
1655 DECL_ARG_TYPE (r) = integer_type_node;
1656 #endif
1657 if (TREE_CHAIN (t))
1658 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1659 return r;
1662 case FIELD_DECL:
1664 tree r = copy_node (t);
1665 TREE_TYPE (r) = type;
1666 copy_lang_decl (r);
1667 #if 0
1668 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
1669 #endif
1670 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
1671 TREE_CHAIN (r) = NULL_TREE;
1672 return r;
1675 case USING_DECL:
1677 tree r = copy_node (t);
1678 DECL_INITIAL (r)
1679 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
1680 TREE_CHAIN (r) = NULL_TREE;
1681 return r;
1684 case VAR_DECL:
1686 tree r;
1687 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
1689 /* Do we already have this instantiation? */
1690 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1692 tree tmpl = DECL_TI_TEMPLATE (t);
1693 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1695 for (; decls; decls = TREE_CHAIN (decls))
1696 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
1697 return TREE_VALUE (decls);
1700 r = copy_node (t);
1701 TREE_TYPE (r) = type;
1702 DECL_CONTEXT (r) = ctx;
1703 if (TREE_STATIC (r))
1704 DECL_ASSEMBLER_NAME (r)
1705 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
1707 /* Don't try to expand the initializer until someone tries to use
1708 this variable; otherwise we run into circular dependencies. */
1709 DECL_INITIAL (r) = NULL_TREE;
1711 DECL_RTL (r) = 0;
1712 DECL_SIZE (r) = 0;
1714 if (DECL_LANG_SPECIFIC (r))
1716 copy_lang_decl (r);
1717 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
1720 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1722 tree tmpl = DECL_TI_TEMPLATE (t);
1723 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1724 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1725 args, nargs, in_decl);
1727 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1728 *declsp = perm_tree_cons (argvec, r, *declsp);
1729 SET_DECL_IMPLICIT_INSTANTIATION (r);
1731 TREE_CHAIN (r) = NULL_TREE;
1732 return r;
1735 case TYPE_DECL:
1736 if (t == TYPE_NAME (TREE_TYPE (t)))
1737 return TYPE_NAME (type);
1740 tree r = copy_node (t);
1741 TREE_TYPE (r) = type;
1742 DECL_CONTEXT (r) = current_class_type;
1743 TREE_CHAIN (r) = NULL_TREE;
1744 return r;
1747 case TREE_LIST:
1749 tree purpose, value, chain, result;
1750 int via_public, via_virtual, via_protected;
1752 if (t == void_list_node)
1753 return t;
1755 via_public = TREE_VIA_PUBLIC (t);
1756 via_protected = TREE_VIA_PROTECTED (t);
1757 via_virtual = TREE_VIA_VIRTUAL (t);
1759 purpose = TREE_PURPOSE (t);
1760 if (purpose)
1761 purpose = tsubst (purpose, args, nargs, in_decl);
1762 value = TREE_VALUE (t);
1763 if (value)
1764 value = tsubst (value, args, nargs, in_decl);
1765 chain = TREE_CHAIN (t);
1766 if (chain && chain != void_type_node)
1767 chain = tsubst (chain, args, nargs, in_decl);
1768 if (purpose == TREE_PURPOSE (t)
1769 && value == TREE_VALUE (t)
1770 && chain == TREE_CHAIN (t))
1771 return t;
1772 result = hash_tree_cons (via_public, via_virtual, via_protected,
1773 purpose, value, chain);
1774 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1775 return result;
1777 case TREE_VEC:
1778 if (type != NULL_TREE)
1780 t = copy_node (t);
1782 if (type == TREE_TYPE (t))
1783 return t;
1785 TREE_TYPE (t) = complete_type (type);
1786 if (IS_AGGR_TYPE (type))
1788 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
1789 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
1790 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
1791 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
1793 return t;
1796 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1797 tree *elts = (tree *) alloca (len * sizeof (tree));
1799 bzero ((char *) elts, len * sizeof (tree));
1801 for (i = 0; i < len; i++)
1803 elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
1804 if (elts[i] != TREE_VEC_ELT (t, i))
1805 need_new = 1;
1808 if (!need_new)
1809 return t;
1811 t = make_tree_vec (len);
1812 for (i = 0; i < len; i++)
1813 TREE_VEC_ELT (t, i) = elts[i];
1815 return t;
1817 case POINTER_TYPE:
1818 case REFERENCE_TYPE:
1820 tree r;
1821 enum tree_code code;
1822 if (type == TREE_TYPE (t))
1823 return t;
1825 code = TREE_CODE (t);
1826 if (code == POINTER_TYPE)
1827 r = build_pointer_type (type);
1828 else
1829 r = build_reference_type (type);
1830 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
1831 /* Will this ever be needed for TYPE_..._TO values? */
1832 layout_type (r);
1833 return r;
1835 case OFFSET_TYPE:
1836 return build_offset_type
1837 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
1838 case FUNCTION_TYPE:
1839 case METHOD_TYPE:
1841 tree values = TYPE_ARG_TYPES (t);
1842 tree context = TYPE_CONTEXT (t);
1843 tree raises = TYPE_RAISES_EXCEPTIONS (t);
1844 tree fntype;
1846 /* Don't bother recursing if we know it won't change anything. */
1847 if (values != void_list_node)
1849 /* This should probably be rewritten to use hash_tree_cons for
1850 the memory savings. */
1851 tree first = NULL_TREE;
1852 tree last;
1854 for (; values && values != void_list_node;
1855 values = TREE_CHAIN (values))
1857 tree value = TYPE_MAIN_VARIANT (type_decays_to
1858 (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
1859 /* Don't instantiate default args unless they are used.
1860 Handle it in build_over_call instead. */
1861 tree purpose = TREE_PURPOSE (values);
1862 tree x = build_tree_list (purpose, value);
1864 if (first)
1865 TREE_CHAIN (last) = x;
1866 else
1867 first = x;
1868 last = x;
1871 if (values == void_list_node)
1872 TREE_CHAIN (last) = void_list_node;
1874 values = first;
1876 if (context)
1877 context = tsubst (context, args, nargs, in_decl);
1878 /* Could also optimize cases where return value and
1879 values have common elements (e.g., T min(const &T, const T&). */
1881 /* If the above parameters haven't changed, just return the type. */
1882 if (type == TREE_TYPE (t)
1883 && values == TYPE_VALUES (t)
1884 && context == TYPE_CONTEXT (t))
1885 return t;
1887 /* Construct a new type node and return it. */
1888 if (TREE_CODE (t) == FUNCTION_TYPE
1889 && context == NULL_TREE)
1891 fntype = build_function_type (type, values);
1893 else if (context == NULL_TREE)
1895 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1896 args, nargs, in_decl);
1897 fntype = build_cplus_method_type (base, type,
1898 TREE_CHAIN (values));
1900 else
1902 fntype = make_node (TREE_CODE (t));
1903 TREE_TYPE (fntype) = type;
1904 TYPE_CONTEXT (fntype) = context;
1905 TYPE_VALUES (fntype) = values;
1906 TYPE_SIZE (fntype) = TYPE_SIZE (t);
1907 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
1908 TYPE_MODE (fntype) = TYPE_MODE (t);
1909 if (TYPE_METHOD_BASETYPE (t))
1910 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
1911 args, nargs, in_decl);
1912 /* Need to generate hash value. */
1913 my_friendly_abort (84);
1915 fntype = build_type_variant (fntype,
1916 TYPE_READONLY (t),
1917 TYPE_VOLATILE (t));
1918 if (raises)
1920 raises = tsubst (raises, args, nargs, in_decl);
1921 fntype = build_exception_variant (fntype, raises);
1923 return fntype;
1925 case ARRAY_TYPE:
1927 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1928 tree r;
1929 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1930 return t;
1931 r = build_cplus_array_type (type, domain);
1932 return r;
1935 case PLUS_EXPR:
1936 case MINUS_EXPR:
1937 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1938 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1939 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1941 case NEGATE_EXPR:
1942 case NOP_EXPR:
1943 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1944 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1946 case TYPENAME_TYPE:
1948 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1949 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
1950 return cp_build_type_variant
1951 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
1952 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
1955 case INDIRECT_REF:
1956 return make_pointer_declarator
1957 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1959 case ADDR_EXPR:
1960 return make_reference_declarator
1961 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1963 case ARRAY_REF:
1964 return build_parse_node
1965 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1966 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
1968 case CALL_EXPR:
1969 return make_call_declarator
1970 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1971 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
1972 TREE_OPERAND (t, 2),
1973 tsubst (TREE_TYPE (t), args, nargs, in_decl));
1975 case SCOPE_REF:
1976 return build_parse_node
1977 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1978 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
1980 default:
1981 sorry ("use of `%s' in template",
1982 tree_code_name [(int) TREE_CODE (t)]);
1983 return error_mark_node;
1987 void
1988 do_pushlevel ()
1990 emit_line_note (input_filename, lineno);
1991 pushlevel (0);
1992 clear_last_expr ();
1993 push_momentary ();
1994 expand_start_bindings (0);
1997 tree
1998 do_poplevel ()
2000 tree t;
2002 expand_end_bindings (getdecls (), kept_level_p (), 1);
2003 t = poplevel (kept_level_p (), 1, 0);
2004 pop_momentary ();
2005 return t;
2008 tree
2009 tsubst_copy (t, args, nargs, in_decl)
2010 tree t, *args;
2011 int nargs;
2012 tree in_decl;
2014 enum tree_code code;
2016 if (t == NULL_TREE || t == error_mark_node)
2017 return t;
2019 code = TREE_CODE (t);
2021 switch (code)
2023 case PARM_DECL:
2024 return do_identifier (DECL_NAME (t), 0);
2026 case CONST_DECL:
2027 case FIELD_DECL:
2028 if (DECL_CONTEXT (t))
2030 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
2031 if (ctx == current_function_decl)
2032 return lookup_name (DECL_NAME (t), 0);
2033 else if (ctx != DECL_CONTEXT (t))
2034 return lookup_field (ctx, DECL_NAME (t), 0, 0);
2036 return t;
2038 case VAR_DECL:
2039 case FUNCTION_DECL:
2040 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2041 t = tsubst (t, args, nargs, in_decl);
2042 mark_used (t);
2043 return t;
2045 #if 0
2046 case IDENTIFIER_NODE:
2047 return do_identifier (t, 0);
2048 #endif
2050 case CAST_EXPR:
2051 case REINTERPRET_CAST_EXPR:
2052 case CONST_CAST_EXPR:
2053 case STATIC_CAST_EXPR:
2054 case DYNAMIC_CAST_EXPR:
2055 return build1
2056 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2057 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2059 case INDIRECT_REF:
2060 case PREDECREMENT_EXPR:
2061 case PREINCREMENT_EXPR:
2062 case POSTDECREMENT_EXPR:
2063 case POSTINCREMENT_EXPR:
2064 case NEGATE_EXPR:
2065 case TRUTH_NOT_EXPR:
2066 case BIT_NOT_EXPR:
2067 case ADDR_EXPR:
2068 case CONVERT_EXPR: /* Unary + */
2069 case SIZEOF_EXPR:
2070 case ARROW_EXPR:
2071 case THROW_EXPR:
2072 case TYPEID_EXPR:
2073 return build1
2074 (code, NULL_TREE,
2075 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2077 case PLUS_EXPR:
2078 case MINUS_EXPR:
2079 case MULT_EXPR:
2080 case TRUNC_DIV_EXPR:
2081 case CEIL_DIV_EXPR:
2082 case FLOOR_DIV_EXPR:
2083 case ROUND_DIV_EXPR:
2084 case EXACT_DIV_EXPR:
2085 case BIT_AND_EXPR:
2086 case BIT_ANDTC_EXPR:
2087 case BIT_IOR_EXPR:
2088 case BIT_XOR_EXPR:
2089 case TRUNC_MOD_EXPR:
2090 case FLOOR_MOD_EXPR:
2091 case TRUTH_ANDIF_EXPR:
2092 case TRUTH_ORIF_EXPR:
2093 case TRUTH_AND_EXPR:
2094 case TRUTH_OR_EXPR:
2095 case RSHIFT_EXPR:
2096 case LSHIFT_EXPR:
2097 case RROTATE_EXPR:
2098 case LROTATE_EXPR:
2099 case EQ_EXPR:
2100 case NE_EXPR:
2101 case MAX_EXPR:
2102 case MIN_EXPR:
2103 case LE_EXPR:
2104 case GE_EXPR:
2105 case LT_EXPR:
2106 case GT_EXPR:
2107 case COMPONENT_REF:
2108 case ARRAY_REF:
2109 case COMPOUND_EXPR:
2110 case SCOPE_REF:
2111 case DOTSTAR_EXPR:
2112 case MEMBER_REF:
2113 return build_nt
2114 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2115 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2117 case CALL_EXPR:
2119 tree fn = TREE_OPERAND (t, 0);
2120 if (really_overloaded_fn (fn))
2121 fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
2122 else
2123 fn = tsubst_copy (fn, args, nargs, in_decl);
2124 return build_nt
2125 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2126 NULL_TREE);
2129 case METHOD_CALL_EXPR:
2131 tree name = TREE_OPERAND (t, 0);
2132 if (TREE_CODE (name) == BIT_NOT_EXPR)
2134 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2135 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2137 else if (TREE_CODE (name) == SCOPE_REF
2138 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
2140 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2141 name = TREE_OPERAND (name, 1);
2142 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2143 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2144 name = build_nt (SCOPE_REF, base, name);
2146 else
2147 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
2148 return build_nt
2149 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2150 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
2151 NULL_TREE);
2154 case COND_EXPR:
2155 case MODOP_EXPR:
2156 return build_nt
2157 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2158 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2159 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
2161 case NEW_EXPR:
2163 tree r = build_nt
2164 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2165 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2166 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
2167 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
2168 return r;
2171 case DELETE_EXPR:
2173 tree r = build_nt
2174 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2175 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2176 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
2177 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
2178 return r;
2181 case TREE_LIST:
2183 tree purpose, value, chain;
2185 if (t == void_list_node)
2186 return t;
2188 purpose = TREE_PURPOSE (t);
2189 if (purpose)
2190 purpose = tsubst_copy (purpose, args, nargs, in_decl);
2191 value = TREE_VALUE (t);
2192 if (value)
2193 value = tsubst_copy (value, args, nargs, in_decl);
2194 chain = TREE_CHAIN (t);
2195 if (chain && chain != void_type_node)
2196 chain = tsubst_copy (chain, args, nargs, in_decl);
2197 if (purpose == TREE_PURPOSE (t)
2198 && value == TREE_VALUE (t)
2199 && chain == TREE_CHAIN (t))
2200 return t;
2201 return tree_cons (purpose, value, chain);
2204 case RECORD_TYPE:
2205 case UNION_TYPE:
2206 case ENUMERAL_TYPE:
2207 case INTEGER_TYPE:
2208 case TEMPLATE_TYPE_PARM:
2209 case TEMPLATE_CONST_PARM:
2210 case POINTER_TYPE:
2211 case REFERENCE_TYPE:
2212 case OFFSET_TYPE:
2213 case FUNCTION_TYPE:
2214 case METHOD_TYPE:
2215 case ARRAY_TYPE:
2216 case TYPENAME_TYPE:
2217 return tsubst (t, args, nargs, in_decl);
2219 case IDENTIFIER_NODE:
2220 if (IDENTIFIER_TYPENAME_P (t))
2221 return build_typename_overload
2222 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
2223 else
2224 return t;
2226 case CONSTRUCTOR:
2227 return build
2228 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
2229 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
2231 default:
2232 return t;
2236 tree
2237 tsubst_expr (t, args, nargs, in_decl)
2238 tree t, *args;
2239 int nargs;
2240 tree in_decl;
2242 if (t == NULL_TREE || t == error_mark_node)
2243 return t;
2245 if (processing_template_decl)
2246 return tsubst_copy (t, args, nargs, in_decl);
2248 switch (TREE_CODE (t))
2250 case RETURN_STMT:
2251 lineno = TREE_COMPLEXITY (t);
2252 emit_line_note (input_filename, lineno);
2253 c_expand_return
2254 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2255 finish_stmt ();
2256 break;
2258 case EXPR_STMT:
2259 lineno = TREE_COMPLEXITY (t);
2260 emit_line_note (input_filename, lineno);
2261 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2262 /* Do default conversion if safe and possibly important,
2263 in case within ({...}). */
2264 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
2265 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2266 t = default_conversion (t);
2267 cplus_expand_expr_stmt (t);
2268 clear_momentary ();
2269 finish_stmt ();
2270 break;
2272 case DECL_STMT:
2274 int i = suspend_momentary ();
2275 tree dcl, init;
2277 lineno = TREE_COMPLEXITY (t);
2278 emit_line_note (input_filename, lineno);
2279 dcl = start_decl
2280 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2281 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2282 TREE_OPERAND (t, 2) != 0);
2283 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2284 cp_finish_decl
2285 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
2286 resume_momentary (i);
2287 return dcl;
2290 case FOR_STMT:
2292 tree tmp;
2293 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
2294 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2295 int cond_scope = (TREE_OPERAND (t, 1)
2296 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
2298 lineno = TREE_COMPLEXITY (t);
2299 emit_line_note (input_filename, lineno);
2300 if (init_scope)
2301 do_pushlevel ();
2302 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
2303 tsubst_expr (tmp, args, nargs, in_decl);
2304 emit_nop ();
2305 emit_line_note (input_filename, lineno);
2306 expand_start_loop_continue_elsewhere (1);
2308 if (cond_scope)
2309 do_pushlevel ();
2310 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2311 emit_line_note (input_filename, lineno);
2312 if (tmp)
2313 expand_exit_loop_if_false (0, condition_conversion (tmp));
2315 if (! cond_scope)
2316 do_pushlevel ();
2317 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
2318 do_poplevel ();
2320 emit_line_note (input_filename, lineno);
2321 expand_loop_continue_here ();
2322 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2323 if (tmp)
2324 cplus_expand_expr_stmt (tmp);
2326 expand_end_loop ();
2327 if (init_scope)
2328 do_poplevel ();
2329 finish_stmt ();
2331 break;
2333 case WHILE_STMT:
2335 tree cond;
2337 lineno = TREE_COMPLEXITY (t);
2338 emit_nop ();
2339 emit_line_note (input_filename, lineno);
2340 expand_start_loop (1);
2342 cond = TREE_OPERAND (t, 0);
2343 if (TREE_CODE (cond) == DECL_STMT)
2344 do_pushlevel ();
2345 cond = tsubst_expr (cond, args, nargs, in_decl);
2346 emit_line_note (input_filename, lineno);
2347 expand_exit_loop_if_false (0, condition_conversion (cond));
2349 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
2350 do_pushlevel ();
2351 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2352 do_poplevel ();
2354 expand_end_loop ();
2355 finish_stmt ();
2357 break;
2359 case DO_STMT:
2361 tree cond;
2363 lineno = TREE_COMPLEXITY (t);
2364 emit_nop ();
2365 emit_line_note (input_filename, lineno);
2366 expand_start_loop_continue_elsewhere (1);
2368 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2369 expand_loop_continue_here ();
2371 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2372 emit_line_note (input_filename, lineno);
2373 expand_exit_loop_if_false (0, condition_conversion (cond));
2374 expand_end_loop ();
2376 clear_momentary ();
2377 finish_stmt ();
2379 break;
2381 case IF_STMT:
2383 tree tmp;
2384 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2386 lineno = TREE_COMPLEXITY (t);
2387 if (cond_scope)
2388 do_pushlevel ();
2389 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2390 emit_line_note (input_filename, lineno);
2391 expand_start_cond (condition_conversion (tmp), 0);
2393 if (tmp = TREE_OPERAND (t, 1), tmp)
2394 tsubst_expr (tmp, args, nargs, in_decl);
2396 if (tmp = TREE_OPERAND (t, 2), tmp)
2398 expand_start_else ();
2399 tsubst_expr (tmp, args, nargs, in_decl);
2402 expand_end_cond ();
2404 if (cond_scope)
2405 do_poplevel ();
2407 finish_stmt ();
2409 break;
2411 case COMPOUND_STMT:
2413 tree substmt = TREE_OPERAND (t, 0);
2415 lineno = TREE_COMPLEXITY (t);
2417 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2418 do_pushlevel ();
2420 for (; substmt; substmt = TREE_CHAIN (substmt))
2421 tsubst_expr (substmt, args, nargs, in_decl);
2423 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2424 do_poplevel ();
2426 break;
2428 case BREAK_STMT:
2429 lineno = TREE_COMPLEXITY (t);
2430 emit_line_note (input_filename, lineno);
2431 if (! expand_exit_something ())
2432 error ("break statement not within loop or switch");
2433 break;
2435 case CONTINUE_STMT:
2436 lineno = TREE_COMPLEXITY (t);
2437 emit_line_note (input_filename, lineno);
2438 if (! expand_continue_loop (0))
2439 error ("continue statement not within a loop");
2440 break;
2442 case SWITCH_STMT:
2444 tree val, tmp;
2445 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2447 lineno = TREE_COMPLEXITY (t);
2448 if (cond_scope)
2449 do_pushlevel ();
2450 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2451 emit_line_note (input_filename, lineno);
2452 c_expand_start_case (val);
2453 push_switch ();
2455 if (tmp = TREE_OPERAND (t, 1), tmp)
2456 tsubst_expr (tmp, args, nargs, in_decl);
2458 expand_end_case (val);
2459 pop_switch ();
2461 if (cond_scope)
2462 do_poplevel ();
2464 finish_stmt ();
2466 break;
2468 case CASE_LABEL:
2469 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
2470 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2471 break;
2473 case LABEL_DECL:
2474 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
2475 DECL_NAME (t));
2476 if (t)
2477 expand_label (t);
2478 break;
2480 case GOTO_STMT:
2481 lineno = TREE_COMPLEXITY (t);
2482 emit_line_note (input_filename, lineno);
2483 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
2485 tree decl = lookup_label (TREE_OPERAND (t, 0));
2486 TREE_USED (decl) = 1;
2487 expand_goto (decl);
2489 else
2490 expand_computed_goto
2491 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2492 break;
2494 case TRY_BLOCK:
2495 lineno = TREE_COMPLEXITY (t);
2496 emit_line_note (input_filename, lineno);
2497 expand_start_try_stmts ();
2498 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2499 expand_start_all_catch ();
2501 tree handler = TREE_OPERAND (t, 1);
2502 for (; handler; handler = TREE_CHAIN (handler))
2503 tsubst_expr (handler, args, nargs, in_decl);
2505 expand_end_all_catch ();
2506 break;
2508 case HANDLER:
2509 lineno = TREE_COMPLEXITY (t);
2510 do_pushlevel ();
2511 if (TREE_OPERAND (t, 0))
2513 tree d = TREE_OPERAND (t, 0);
2514 expand_start_catch_block
2515 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
2516 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
2518 else
2519 expand_start_catch_block (NULL_TREE, NULL_TREE);
2520 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2521 expand_end_catch_block ();
2522 do_poplevel ();
2523 break;
2525 case TAG_DEFN:
2526 lineno = TREE_COMPLEXITY (t);
2527 t = TREE_TYPE (t);
2528 if (TREE_CODE (t) == ENUMERAL_TYPE)
2529 tsubst_enum (t, args, nargs);
2530 break;
2532 default:
2533 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
2535 return NULL_TREE;
2538 tree
2539 instantiate_template (tmpl, targ_ptr)
2540 tree tmpl, *targ_ptr;
2542 tree fndecl;
2543 int i, len;
2544 struct obstack *old_fmp_obstack;
2545 extern struct obstack *function_maybepermanent_obstack;
2547 push_obstacks (&permanent_obstack, &permanent_obstack);
2548 old_fmp_obstack = function_maybepermanent_obstack;
2549 function_maybepermanent_obstack = &permanent_obstack;
2551 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
2552 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
2554 i = len;
2555 while (i--)
2557 tree t = targ_ptr [i];
2558 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2560 tree nt = target_type (t);
2561 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
2563 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
2564 cp_error (" trying to instantiate `%D'", tmpl);
2565 fndecl = error_mark_node;
2566 goto out;
2569 targ_ptr[i] = copy_to_permanent (t);
2572 /* substitute template parameters */
2573 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
2575 if (flag_external_templates)
2576 add_pending_template (fndecl);
2578 out:
2579 function_maybepermanent_obstack = old_fmp_obstack;
2580 pop_obstacks ();
2582 return fndecl;
2585 /* Push the name of the class template into the scope of the instantiation. */
2587 void
2588 overload_template_name (type)
2589 tree type;
2591 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
2592 tree decl;
2594 if (IDENTIFIER_CLASS_VALUE (id)
2595 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
2596 return;
2598 decl = build_decl (TYPE_DECL, id, type);
2599 SET_DECL_ARTIFICIAL (decl);
2600 pushdecl_class_level (decl);
2603 /* Type unification.
2605 We have a function template signature with one or more references to
2606 template parameters, and a parameter list we wish to fit to this
2607 template. If possible, produce a list of parameters for the template
2608 which will cause it to fit the supplied parameter list.
2610 Return zero for success, 2 for an incomplete match that doesn't resolve
2611 all the types, and 1 for complete failure. An error message will be
2612 printed only for an incomplete match.
2614 TPARMS[NTPARMS] is an array of template parameter types;
2615 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2616 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2617 and ARGS is the argument list we're trying to match against it.
2619 If SUBR is 1, we're being called recursively (to unify the arguments of
2620 a function or method parameter of a function template), so don't zero
2621 out targs and don't fail on an incomplete match.
2623 If STRICT is 1, the match must be exact (for casts of overloaded
2624 addresses, explicit instantiation, and more_specialized). */
2627 type_unification (tparms, targs, parms, args, nsubsts, subr, strict)
2628 tree tparms, *targs, parms, args;
2629 int *nsubsts, subr, strict;
2631 tree parm, arg;
2632 int i;
2633 int ntparms = TREE_VEC_LENGTH (tparms);
2635 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
2636 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
2637 /* ARGS could be NULL (via a call from parse.y to
2638 build_x_function_call). */
2639 if (args)
2640 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
2641 my_friendly_assert (ntparms > 0, 292);
2643 if (!subr)
2644 bzero ((char *) targs, sizeof (tree) * ntparms);
2646 while (parms
2647 && parms != void_list_node
2648 && args
2649 && args != void_list_node)
2651 parm = TREE_VALUE (parms);
2652 parms = TREE_CHAIN (parms);
2653 arg = TREE_VALUE (args);
2654 args = TREE_CHAIN (args);
2656 if (arg == error_mark_node)
2657 return 1;
2658 if (arg == unknown_type_node)
2659 return 1;
2661 if (! uses_template_parms (parm)
2662 && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2664 if (can_convert_arg (parm, TREE_TYPE (arg), arg))
2665 continue;
2666 return 1;
2669 #if 0
2670 if (TREE_CODE (arg) == VAR_DECL)
2671 arg = TREE_TYPE (arg);
2672 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
2673 arg = TREE_TYPE (arg);
2674 #else
2675 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2677 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
2678 if (TREE_CODE (arg) == TREE_LIST
2679 && TREE_TYPE (arg) == unknown_type_node
2680 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
2682 int nsubsts, ntparms;
2683 tree *targs;
2685 /* Have to back unify here */
2686 arg = TREE_VALUE (arg);
2687 nsubsts = 0;
2688 ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
2689 targs = (tree *) alloca (sizeof (tree) * ntparms);
2690 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
2691 return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
2692 TYPE_ARG_TYPES (TREE_TYPE (arg)),
2693 parm, &nsubsts, 0, strict);
2695 arg = TREE_TYPE (arg);
2697 #endif
2698 if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
2699 arg = TREE_TYPE (arg);
2701 if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
2703 if (TREE_CODE (arg) == FUNCTION_TYPE
2704 || TREE_CODE (arg) == METHOD_TYPE)
2705 arg = build_pointer_type (arg);
2706 else if (TREE_CODE (arg) == ARRAY_TYPE)
2707 arg = build_pointer_type (TREE_TYPE (arg));
2708 else
2709 arg = TYPE_MAIN_VARIANT (arg);
2712 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
2714 case 0:
2715 break;
2716 case 1:
2717 return 1;
2720 /* Fail if we've reached the end of the parm list, and more args
2721 are present, and the parm list isn't variadic. */
2722 if (args && args != void_list_node && parms == void_list_node)
2723 return 1;
2724 /* Fail if parms are left and they don't have default values. */
2725 if (parms
2726 && parms != void_list_node
2727 && TREE_PURPOSE (parms) == NULL_TREE)
2728 return 1;
2729 if (!subr)
2730 for (i = 0; i < ntparms; i++)
2731 if (!targs[i])
2733 error ("incomplete type unification");
2734 return 2;
2736 return 0;
2739 /* Tail recursion is your friend. */
2741 static int
2742 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
2743 tree tparms, *targs, parm, arg;
2744 int *nsubsts, ntparms, strict;
2746 int idx;
2748 /* I don't think this will do the right thing with respect to types.
2749 But the only case I've seen it in so far has been array bounds, where
2750 signedness is the only information lost, and I think that will be
2751 okay. */
2752 while (TREE_CODE (parm) == NOP_EXPR)
2753 parm = TREE_OPERAND (parm, 0);
2755 if (arg == error_mark_node)
2756 return 1;
2757 if (arg == unknown_type_node)
2758 return 1;
2759 if (arg == parm)
2760 return 0;
2762 switch (TREE_CODE (parm))
2764 case TEMPLATE_TYPE_PARM:
2765 (*nsubsts)++;
2766 idx = TEMPLATE_TYPE_IDX (parm);
2767 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
2768 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
2769 return 1;
2770 #if 0
2771 /* Template type parameters cannot contain cv-quals; i.e.
2772 template <class T> void f (T& a, T& b) will not generate
2773 void f (const int& a, const int& b). */
2774 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
2775 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
2776 return 1;
2777 arg = TYPE_MAIN_VARIANT (arg);
2778 #else
2780 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
2781 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
2782 arg = cp_build_type_variant (arg, constp, volatilep);
2784 #endif
2785 /* Simple cases: Value already set, does match or doesn't. */
2786 if (targs[idx] == arg)
2787 return 0;
2788 else if (targs[idx])
2789 return 1;
2790 /* Check for mixed types and values. */
2791 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
2792 return 1;
2793 targs[idx] = arg;
2794 return 0;
2795 case TEMPLATE_CONST_PARM:
2796 (*nsubsts)++;
2797 idx = TEMPLATE_CONST_IDX (parm);
2798 if (targs[idx])
2800 int i = cp_tree_equal (targs[idx], arg);
2801 if (i == 1)
2802 return 0;
2803 else if (i == 0)
2804 return 1;
2805 else
2806 my_friendly_abort (42);
2809 targs[idx] = copy_to_permanent (arg);
2810 return 0;
2812 case POINTER_TYPE:
2813 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
2814 return unify (tparms, targs, ntparms, parm,
2815 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
2817 if (TREE_CODE (arg) != POINTER_TYPE)
2818 return 1;
2819 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2820 nsubsts, strict);
2822 case REFERENCE_TYPE:
2823 if (TREE_CODE (arg) == REFERENCE_TYPE)
2824 arg = TREE_TYPE (arg);
2825 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
2826 nsubsts, strict);
2828 case ARRAY_TYPE:
2829 if (TREE_CODE (arg) != ARRAY_TYPE)
2830 return 1;
2831 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2832 nsubsts, strict) != 0)
2833 return 1;
2834 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2835 nsubsts, strict);
2837 case REAL_TYPE:
2838 case COMPLEX_TYPE:
2839 case INTEGER_TYPE:
2840 case BOOLEAN_TYPE:
2841 if (TREE_CODE (arg) != TREE_CODE (parm))
2842 return 1;
2844 if (TREE_CODE (parm) == INTEGER_TYPE)
2846 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2847 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
2848 TYPE_MIN_VALUE (arg), nsubsts, strict))
2849 return 1;
2850 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2851 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
2852 TYPE_MAX_VALUE (arg), nsubsts, strict))
2853 return 1;
2855 /* As far as unification is concerned, this wins. Later checks
2856 will invalidate it if necessary. */
2857 return 0;
2859 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2860 /* Type INTEGER_CST can come from ordinary constant template args. */
2861 case INTEGER_CST:
2862 while (TREE_CODE (arg) == NOP_EXPR)
2863 arg = TREE_OPERAND (arg, 0);
2865 if (TREE_CODE (arg) != INTEGER_CST)
2866 return 1;
2867 return !tree_int_cst_equal (parm, arg);
2869 case MINUS_EXPR:
2871 tree t1, t2;
2872 t1 = TREE_OPERAND (parm, 0);
2873 t2 = TREE_OPERAND (parm, 1);
2874 return unify (tparms, targs, ntparms, t1,
2875 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2876 nsubsts, strict);
2879 case TREE_VEC:
2881 int i;
2882 if (TREE_CODE (arg) != TREE_VEC)
2883 return 1;
2884 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2885 return 1;
2886 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2887 if (unify (tparms, targs, ntparms,
2888 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2889 nsubsts, strict))
2890 return 1;
2891 return 0;
2894 case RECORD_TYPE:
2895 if (TYPE_PTRMEMFUNC_FLAG (parm))
2896 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2897 arg, nsubsts, strict);
2899 /* Allow trivial conversions. */
2900 if (TREE_CODE (arg) != RECORD_TYPE
2901 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2902 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2903 return 1;
2905 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
2907 tree t = NULL_TREE;
2908 if (flag_ansi_overloading && ! strict)
2909 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
2910 else if
2911 (CLASSTYPE_TEMPLATE_INFO (arg)
2912 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
2913 t = arg;
2914 if (! t || t == error_mark_node)
2915 return 1;
2917 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
2918 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
2920 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
2921 return 1;
2922 return 0;
2924 case METHOD_TYPE:
2925 if (TREE_CODE (arg) != METHOD_TYPE)
2926 return 1;
2927 goto check_args;
2929 case FUNCTION_TYPE:
2930 if (TREE_CODE (arg) != FUNCTION_TYPE)
2931 return 1;
2932 check_args:
2933 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
2934 TREE_TYPE (arg), nsubsts, strict))
2935 return 1;
2936 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2937 TYPE_ARG_TYPES (arg), nsubsts, 1, strict);
2939 case OFFSET_TYPE:
2940 if (TREE_CODE (arg) != OFFSET_TYPE)
2941 return 1;
2942 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2943 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
2944 return 1;
2945 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2946 TREE_TYPE (arg), nsubsts, strict);
2948 default:
2949 sorry ("use of `%s' in template type unification",
2950 tree_code_name [(int) TREE_CODE (parm)]);
2951 return 1;
2955 void
2956 mark_decl_instantiated (result, extern_p)
2957 tree result;
2958 int extern_p;
2960 if (DECL_TEMPLATE_INSTANTIATION (result))
2961 SET_DECL_EXPLICIT_INSTANTIATION (result);
2962 TREE_PUBLIC (result) = 1;
2964 if (! extern_p)
2966 DECL_INTERFACE_KNOWN (result) = 1;
2967 DECL_NOT_REALLY_EXTERN (result) = 1;
2969 else if (TREE_CODE (result) == FUNCTION_DECL)
2970 mark_inline_for_output (result);
2973 /* Given two function templates PAT1 and PAT2, return:
2975 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
2976 -1 if PAT2 is more specialized than PAT1.
2977 0 if neither is more specialized. */
2980 more_specialized (pat1, pat2)
2981 tree pat1, pat2;
2983 tree *targs;
2984 int winner = 0;
2986 targs = get_bindings (pat1, pat2);
2987 if (targs)
2989 free (targs);
2990 --winner;
2993 targs = get_bindings (pat2, pat1);
2994 if (targs)
2996 free (targs);
2997 ++winner;
3000 return winner;
3003 /* Given two class template specialization list nodes PAT1 and PAT2, return:
3005 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
3006 -1 if PAT2 is more specialized than PAT1.
3007 0 if neither is more specialized. */
3010 more_specialized_class (pat1, pat2)
3011 tree pat1, pat2;
3013 tree targs;
3014 int winner = 0;
3016 targs = get_class_bindings
3017 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
3018 if (targs)
3019 --winner;
3021 targs = get_class_bindings
3022 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
3023 if (targs)
3024 ++winner;
3026 return winner;
3029 /* Return the template arguments that will produce the function signature
3030 DECL from the function template FN. */
3032 tree *
3033 get_bindings (fn, decl)
3034 tree fn, decl;
3036 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
3037 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
3038 int i, dummy = 0;
3039 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
3040 TYPE_ARG_TYPES (TREE_TYPE (fn)),
3041 TYPE_ARG_TYPES (TREE_TYPE (decl)),
3042 &dummy, 0, 1);
3043 if (i == 0)
3044 return targs;
3045 free (targs);
3046 return 0;
3049 static tree
3050 get_class_bindings (tparms, parms, args)
3051 tree tparms, parms, args;
3053 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
3054 tree vec = make_temp_vec (ntparms);
3056 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
3058 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
3059 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
3060 &dummy, 1))
3062 case 0:
3063 break;
3064 case 1:
3065 return NULL_TREE;
3069 for (i = 0; i < ntparms; ++i)
3070 if (! TREE_VEC_ELT (vec, i))
3071 return NULL_TREE;
3073 return vec;
3076 /* Return the most specialized of the list of templates in FNS that can
3077 produce an instantiation matching DECL. */
3079 tree
3080 most_specialized (fns, decl)
3081 tree fns, decl;
3083 tree fn, champ, *args, *p;
3084 int fate;
3086 for (p = &fns; *p; )
3088 args = get_bindings (TREE_VALUE (*p), decl);
3089 if (args)
3091 free (args);
3092 p = &TREE_CHAIN (*p);
3094 else
3095 *p = TREE_CHAIN (*p);
3098 if (! fns)
3099 return NULL_TREE;
3101 fn = fns;
3102 champ = TREE_VALUE (fn);
3103 fn = TREE_CHAIN (fn);
3104 for (; fn; fn = TREE_CHAIN (fn))
3106 fate = more_specialized (champ, TREE_VALUE (fn));
3107 if (fate == 1)
3109 else
3111 if (fate == 0)
3113 fn = TREE_CHAIN (fn);
3114 if (! fn)
3115 return error_mark_node;
3117 champ = TREE_VALUE (fn);
3121 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
3123 fate = more_specialized (champ, TREE_VALUE (fn));
3124 if (fate != 1)
3125 return error_mark_node;
3128 return champ;
3131 /* Return the most specialized of the class template specializations in
3132 SPECS that can produce an instantiation matching ARGS. */
3134 tree
3135 most_specialized_class (specs, mainargs)
3136 tree specs, mainargs;
3138 tree list = NULL_TREE, t, args, champ;
3139 int fate;
3141 for (t = specs; t; t = TREE_CHAIN (t))
3143 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
3144 if (args)
3146 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
3147 TREE_TYPE (list) = TREE_TYPE (t);
3151 if (! list)
3152 return NULL_TREE;
3154 t = list;
3155 champ = t;
3156 t = TREE_CHAIN (t);
3157 for (; t; t = TREE_CHAIN (t))
3159 fate = more_specialized_class (champ, t);
3160 if (fate == 1)
3162 else
3164 if (fate == 0)
3166 t = TREE_CHAIN (t);
3167 if (! t)
3168 return error_mark_node;
3170 champ = t;
3174 for (t = list; t && t != champ; t = TREE_CHAIN (t))
3176 fate = more_specialized (champ, t);
3177 if (fate != 1)
3178 return error_mark_node;
3181 return champ;
3184 /* called from the parser. */
3186 void
3187 do_decl_instantiation (declspecs, declarator, storage)
3188 tree declspecs, declarator, storage;
3190 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
3191 tree name;
3192 tree fn;
3193 tree result = NULL_TREE;
3194 int extern_p = 0;
3196 if (! DECL_LANG_SPECIFIC (decl))
3198 cp_error ("explicit instantiation of non-template `%#D'", decl);
3199 return;
3202 /* If we've already seen this template instance, use it. */
3203 if (TREE_CODE (decl) == VAR_DECL)
3205 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
3206 if (result && TREE_CODE (result) != VAR_DECL)
3207 result = NULL_TREE;
3209 else if (TREE_CODE (decl) != FUNCTION_DECL)
3211 cp_error ("explicit instantiation of `%#D'", decl);
3212 return;
3214 else if (DECL_FUNCTION_MEMBER_P (decl))
3216 if (DECL_TEMPLATE_INSTANTIATION (decl))
3217 result = decl;
3218 else if (name = DECL_ASSEMBLER_NAME (decl),
3219 fn = IDENTIFIER_GLOBAL_VALUE (name),
3220 fn && DECL_TEMPLATE_INSTANTIATION (fn))
3221 result = fn;
3223 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
3225 tree templates = NULL_TREE;
3226 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
3227 if (decls_match (fn, decl)
3228 && DECL_DEFER_OUTPUT (fn))
3230 result = fn;
3231 break;
3233 else if (TREE_CODE (fn) == TEMPLATE_DECL)
3234 templates = decl_tree_cons (NULL_TREE, fn, templates);
3236 if (! result)
3238 tree *args;
3239 result = most_specialized (templates, decl);
3240 if (result == error_mark_node)
3242 char *str = "candidates are:";
3243 cp_error ("ambiguous template instantiation for `%D' requested", decl);
3244 for (fn = templates; fn; fn = TREE_CHAIN (fn))
3246 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
3247 str = " ";
3249 return;
3251 else if (result)
3253 args = get_bindings (result, decl);
3254 result = instantiate_template (result, args);
3255 free (args);
3259 if (! result)
3261 cp_error ("no matching template for `%D' found", decl);
3262 return;
3265 if (! DECL_TEMPLATE_INFO (result))
3267 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
3268 return;
3271 if (flag_external_templates)
3272 return;
3274 if (storage == NULL_TREE)
3276 else if (storage == ridpointers[(int) RID_EXTERN])
3277 extern_p = 1;
3278 else
3279 cp_error ("storage class `%D' applied to template instantiation",
3280 storage);
3282 mark_decl_instantiated (result, extern_p);
3283 repo_template_instantiated (result, extern_p);
3284 if (! extern_p)
3285 instantiate_decl (result);
3288 void
3289 mark_class_instantiated (t, extern_p)
3290 tree t;
3291 int extern_p;
3293 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
3294 SET_CLASSTYPE_INTERFACE_KNOWN (t);
3295 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
3296 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
3297 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
3298 if (! extern_p)
3300 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
3301 rest_of_type_compilation (t, 1);
3305 void
3306 do_type_instantiation (name, storage)
3307 tree name, storage;
3309 tree t = TREE_TYPE (name);
3310 int extern_p = 0;
3311 int nomem_p = 0;
3312 int static_p = 0;
3314 complete_type (t);
3316 /* With -fexternal-templates, explicit instantiations are treated the same
3317 as implicit ones. */
3318 if (flag_external_templates)
3319 return;
3321 if (TYPE_SIZE (t) == NULL_TREE)
3323 cp_error ("explicit instantiation of `%#T' before definition of template",
3325 return;
3328 if (storage == NULL_TREE)
3329 /* OK */;
3330 else if (storage == ridpointers[(int) RID_INLINE])
3331 nomem_p = 1;
3332 else if (storage == ridpointers[(int) RID_EXTERN])
3333 extern_p = 1;
3334 else if (storage == ridpointers[(int) RID_STATIC])
3335 static_p = 1;
3336 else
3338 cp_error ("storage class `%D' applied to template instantiation",
3339 storage);
3340 extern_p = 0;
3343 /* We've already instantiated this. */
3344 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
3345 && extern_p)
3346 return;
3348 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3350 mark_class_instantiated (t, extern_p);
3351 repo_template_instantiated (t, extern_p);
3354 if (nomem_p)
3355 return;
3358 tree tmp;
3360 if (! static_p)
3361 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
3362 if (DECL_TEMPLATE_INSTANTIATION (tmp))
3364 mark_decl_instantiated (tmp, extern_p);
3365 repo_template_instantiated (tmp, extern_p);
3366 if (! extern_p)
3367 instantiate_decl (tmp);
3370 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
3371 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
3373 mark_decl_instantiated (tmp, extern_p);
3374 repo_template_instantiated (tmp, extern_p);
3375 if (! extern_p)
3376 instantiate_decl (tmp);
3379 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
3380 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
3381 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
3385 tree
3386 instantiate_decl (d)
3387 tree d;
3389 tree ti = DECL_TEMPLATE_INFO (d);
3390 tree tmpl = TI_TEMPLATE (ti);
3391 tree args = TI_ARGS (ti);
3392 tree td;
3393 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
3394 tree save_ti;
3395 int nested = in_function_p ();
3396 int d_defined;
3397 int pattern_defined;
3398 int line = lineno;
3399 char *file = input_filename;
3401 if (TREE_CODE (d) == FUNCTION_DECL)
3403 d_defined = (DECL_INITIAL (d) != NULL_TREE);
3404 pattern_defined = (DECL_INITIAL (pattern) != NULL_TREE);
3406 else
3408 d_defined = ! DECL_IN_AGGR_P (d);
3409 pattern_defined = ! DECL_IN_AGGR_P (pattern);
3412 if (d_defined)
3413 return d;
3415 /* This needs to happen before any tsubsting. */
3416 if (! push_tinst_level (d))
3417 return d;
3419 push_to_top_level ();
3420 lineno = DECL_SOURCE_LINE (d);
3421 input_filename = DECL_SOURCE_FILE (d);
3423 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
3424 variable is a static const initialized in the class body. */
3425 if (TREE_CODE (d) == VAR_DECL
3426 && ! DECL_INITIAL (d) && DECL_INITIAL (pattern))
3428 pushclass (DECL_CONTEXT (d), 2);
3429 DECL_INITIAL (d) = tsubst_expr
3430 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3431 TREE_VEC_LENGTH (args), tmpl);
3432 popclass (1);
3435 /* import_export_decl has to happen after DECL_INITIAL is set up. */
3436 if (pattern_defined)
3438 repo_template_used (d);
3440 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
3442 if (flag_alt_external_templates)
3444 if (interface_unknown)
3445 warn_if_unknown_interface (d);
3447 else if (DECL_INTERFACE_KNOWN (pattern))
3449 DECL_INTERFACE_KNOWN (d) = 1;
3450 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (pattern);
3452 else
3453 warn_if_unknown_interface (pattern);
3456 if (at_eof)
3457 import_export_decl (d);
3460 if (! pattern_defined
3461 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
3462 && (! DECL_INTERFACE_KNOWN (d)
3463 || ! DECL_NOT_REALLY_EXTERN (d)))
3464 /* Kludge: if we compile a constructor in the middle of processing a
3465 toplevel declaration, we blow away the declspecs in
3466 temp_decl_obstack when we call permanent_allocation in
3467 finish_function. So don't compile it yet. */
3468 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
3470 add_pending_template (d);
3471 goto out;
3474 lineno = DECL_SOURCE_LINE (d);
3475 input_filename = DECL_SOURCE_FILE (d);
3477 /* Trick tsubst into giving us a new decl in case the template changed. */
3478 save_ti = DECL_TEMPLATE_INFO (pattern);
3479 DECL_TEMPLATE_INFO (pattern) = NULL_TREE;
3480 td = tsubst (pattern, &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), tmpl);
3481 DECL_TEMPLATE_INFO (pattern) = save_ti;
3483 /* And set up DECL_INITIAL, since tsubst doesn't. */
3484 if (TREE_CODE (td) == VAR_DECL)
3486 pushclass (DECL_CONTEXT (d), 2);
3487 DECL_INITIAL (td) = tsubst_expr
3488 (DECL_INITIAL (pattern), &TREE_VEC_ELT (args, 0),
3489 TREE_VEC_LENGTH (args), tmpl);
3490 popclass (1);
3493 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
3494 if (TREE_CODE (d) == FUNCTION_DECL)
3495 DECL_INITIAL (td) = error_mark_node;
3496 duplicate_decls (td, d);
3497 if (TREE_CODE (d) == FUNCTION_DECL)
3498 DECL_INITIAL (td) = 0;
3500 if (TREE_CODE (d) == VAR_DECL)
3502 DECL_IN_AGGR_P (d) = 0;
3503 if (DECL_INTERFACE_KNOWN (d))
3504 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
3505 else
3507 DECL_EXTERNAL (d) = 1;
3508 DECL_NOT_REALLY_EXTERN (d) = 1;
3510 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
3512 else if (TREE_CODE (d) == FUNCTION_DECL)
3514 tree t = DECL_SAVED_TREE (pattern);
3516 start_function (NULL_TREE, d, NULL_TREE, 1);
3517 store_parm_decls ();
3519 if (t && TREE_CODE (t) == RETURN_INIT)
3521 store_return_init
3522 (TREE_OPERAND (t, 0),
3523 tsubst_expr (TREE_OPERAND (t, 1), &TREE_VEC_ELT (args, 0),
3524 TREE_VEC_LENGTH (args), tmpl));
3525 t = TREE_CHAIN (t);
3528 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
3530 current_member_init_list
3531 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
3532 current_base_init_list
3533 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
3534 t = TREE_CHAIN (t);
3537 setup_vtbl_ptr ();
3538 /* Always keep the BLOCK node associated with the outermost
3539 pair of curley braces of a function. These are needed
3540 for correct operation of dwarfout.c. */
3541 keep_next_level ();
3543 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
3544 tsubst_expr (t, &TREE_VEC_ELT (args, 0),
3545 TREE_VEC_LENGTH (args), tmpl);
3547 finish_function (lineno, 0, nested);
3550 out:
3551 lineno = line;
3552 input_filename = file;
3554 pop_from_top_level ();
3555 pop_tinst_level ();
3557 return d;
3560 tree
3561 tsubst_chain (t, argvec)
3562 tree t, argvec;
3564 if (t)
3566 tree first = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3567 TREE_VEC_LENGTH (argvec), NULL_TREE);
3568 tree last = first;
3570 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
3572 tree x = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3573 TREE_VEC_LENGTH (argvec), NULL_TREE);
3574 TREE_CHAIN (last) = x;
3575 last = x;
3578 return first;
3580 return NULL_TREE;
3583 static tree
3584 tsubst_expr_values (t, argvec)
3585 tree t, argvec;
3587 tree first = NULL_TREE;
3588 tree *p = &first;
3590 for (; t; t = TREE_CHAIN (t))
3592 tree pur = tsubst_copy (TREE_PURPOSE (t), &TREE_VEC_ELT (argvec, 0),
3593 TREE_VEC_LENGTH (argvec), NULL_TREE);
3594 tree val = tsubst_expr (TREE_VALUE (t), &TREE_VEC_ELT (argvec, 0),
3595 TREE_VEC_LENGTH (argvec), NULL_TREE);
3596 *p = build_tree_list (pur, val);
3597 p = &TREE_CHAIN (*p);
3599 return first;
3602 tree last_tree;
3604 void
3605 add_tree (t)
3606 tree t;
3608 last_tree = TREE_CHAIN (last_tree) = t;
3611 /* D is an undefined function declaration in the presence of templates with
3612 the same name, listed in FNS. If one of them can produce D as an
3613 instantiation, remember this so we can instantiate it at EOF if D has
3614 not been defined by that time. */
3616 void
3617 add_maybe_template (d, fns)
3618 tree d, fns;
3620 tree t;
3622 if (DECL_MAYBE_TEMPLATE (d))
3623 return;
3625 t = most_specialized (fns, d);
3626 if (! t)
3627 return;
3628 if (t == error_mark_node)
3630 cp_error ("ambiguous template instantiation for `%D'", d);
3631 return;
3634 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
3635 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
3636 DECL_MAYBE_TEMPLATE (d) = 1;
3639 /* Instantiate an enumerated type. Used by instantiate_class_template and
3640 tsubst_expr. */
3642 static tree
3643 tsubst_enum (tag, args, nargs)
3644 tree tag, *args;
3645 int nargs;
3647 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
3648 tree e, values = NULL_TREE;
3650 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
3652 tree elt = build_enumerator (TREE_PURPOSE (e),
3653 tsubst_expr (TREE_VALUE (e), args,
3654 nargs, NULL_TREE));
3655 TREE_CHAIN (elt) = values;
3656 values = elt;
3659 finish_enum (newtag, values);
3661 return newtag;