]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
84th Cygnus<->FSF merge
[gcc.git] / gcc / cp / pt.c
CommitLineData
8d08fdba 1/* Handle parameterized types (templates) for GNU C++.
75b0bbce 2 Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
8d08fdba
MS
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22/* Known bugs or deficiencies include:
23 * templates for class static data don't work (methods only)
24 * duplicated method templates can crash the compiler
25 * interface/impl data is taken from file defining the template
26 * all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win"
8d08fdba
MS
28 */
29
30#include "config.h"
31#include <stdio.h>
32#include "obstack.h"
33
34#include "tree.h"
35#include "flags.h"
36#include "cp-tree.h"
37#include "decl.h"
38#include "parse.h"
f0e01782 39#include "lex.h"
e8abc66f 40#include "output.h"
a9aedbc2 41#include "defaults.h"
8d08fdba
MS
42
43extern struct obstack permanent_obstack;
8d08fdba
MS
44
45extern int lineno;
46extern char *input_filename;
47struct pending_inline *pending_template_expansions;
48
5566b478
MS
49tree current_template_parms;
50HOST_WIDE_INT processing_template_decl;
8d08fdba 51
5566b478
MS
52tree pending_templates;
53static tree *template_tail = &pending_templates;
54
55int minimal_parse_mode;
75b0bbce 56
8d08fdba
MS
57#define obstack_chunk_alloc xmalloc
58#define obstack_chunk_free free
59
60static int unify ();
8d08fdba 61
5566b478
MS
62void pop_template_decls ();
63
64tree classtype_mangled_name ();
65static char * mangle_class_name_for_template ();
66tree tsubst_expr_values ();
67
68/* We've got a template header coming up; push to a new level for storing
69 the parms. */
8d08fdba 70
8d08fdba
MS
71void
72begin_template_parm_list ()
73{
74 pushlevel (0);
5566b478 75 declare_pseudo_global_level ();
8d08fdba
MS
76}
77
78/* Process information from new template parameter NEXT and append it to the
5566b478 79 LIST being built. */
8d08fdba
MS
80tree
81process_template_parm (list, next)
82 tree list, next;
83{
84 tree parm;
85 tree decl = 0;
a292b002 86 tree defval;
5566b478 87 int is_type, idx;
8d08fdba
MS
88 parm = next;
89 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
a292b002
MS
90 defval = TREE_PURPOSE (parm);
91 parm = TREE_VALUE (parm);
92 is_type = TREE_PURPOSE (parm) == class_type_node;
5566b478
MS
93
94 if (list)
95 {
96 tree p = TREE_VALUE (tree_last (list));
97
98 if (TREE_CODE (p) == TYPE_DECL)
99 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
100 else
101 idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
102 ++idx;
103 }
104 else
105 idx = 0;
106
8d08fdba
MS
107 if (!is_type)
108 {
109 tree tinfo = 0;
a292b002 110 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
8d08fdba 111 /* is a const-param */
a292b002 112 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
f30432d7 113 PARM, 0, NULL_TREE, NULL_TREE);
8d08fdba
MS
114 /* A template parameter is not modifiable. */
115 TREE_READONLY (parm) = 1;
db5ae43f 116 if (IS_AGGR_TYPE (TREE_TYPE (parm)))
8d08fdba
MS
117 {
118 sorry ("aggregate template parameter types");
119 TREE_TYPE (parm) = void_type_node;
120 }
121 tinfo = make_node (TEMPLATE_CONST_PARM);
122 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
123 if (TREE_PERMANENT (parm) == 0)
124 {
125 parm = copy_node (parm);
126 TREE_PERMANENT (parm) = 1;
127 }
128 TREE_TYPE (tinfo) = TREE_TYPE (parm);
129 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
130 DECL_INITIAL (decl) = tinfo;
131 DECL_INITIAL (parm) = tinfo;
5566b478 132 TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl + 1);
8d08fdba
MS
133 }
134 else
135 {
5566b478
MS
136 tree t = make_lang_type (TEMPLATE_TYPE_PARM);
137 CLASSTYPE_GOT_SEMICOLON (t) = 1;
a292b002
MS
138 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
139 TYPE_MAIN_DECL (t) = decl;
140 parm = decl;
5566b478 141 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl + 1);
8d08fdba 142 }
8ccc31eb 143 SET_DECL_ARTIFICIAL (decl);
8d08fdba 144 pushdecl (decl);
a292b002 145 parm = build_tree_list (defval, parm);
8d08fdba
MS
146 return chainon (list, parm);
147}
148
149/* The end of a template parameter list has been reached. Process the
150 tree list into a parameter vector, converting each parameter into a more
151 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
152 as PARM_DECLs. */
153
154tree
155end_template_parm_list (parms)
156 tree parms;
157{
5566b478 158 int nparms;
8d08fdba 159 tree parm;
5566b478
MS
160 tree saved_parmlist = make_tree_vec (list_length (parms));
161
162 ++processing_template_decl;
163 current_template_parms
164 = tree_cons (build_int_2 (0, processing_template_decl),
165 saved_parmlist, current_template_parms);
8d08fdba
MS
166
167 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
5566b478 168 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
a292b002 169
8d08fdba
MS
170 return saved_parmlist;
171}
172
5566b478
MS
173/* end_template_decl is called after a template declaration is seen. */
174
8d08fdba 175void
5566b478 176end_template_decl ()
8d08fdba 177{
5566b478
MS
178 /* This matches the pushlevel in begin_template_parm_list. */
179 poplevel (0, 0, 0);
8d08fdba 180
5566b478
MS
181 --processing_template_decl;
182 current_template_parms = TREE_CHAIN (current_template_parms);
183 (void) get_pending_sizes (); /* Why? */
184}
8d08fdba 185
5566b478
MS
186void
187push_template_decl (decl)
188 tree decl;
189{
190 tree header = current_template_parms;
191 tree tmpl;
192 tree args = NULL_TREE;
193 tree info;
194 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
195 int primary = 0;
196
197 /* Kludge! */
198 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
199 && DECL_CLASS_CONTEXT (decl))
200 ;
201 /* Note that this template is a "primary template" */
202 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
203 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
204 primary = 1;
8d08fdba 205
5566b478 206 while (header)
8d08fdba 207 {
5566b478
MS
208 tree a = copy_node (TREE_VALUE (header));
209 int i = TREE_VEC_LENGTH (a);
210 TREE_TYPE (a) = NULL_TREE;
211 while (i--)
212 {
213 tree t = TREE_VALUE (TREE_VEC_ELT (a, i));
214 if (TREE_CODE (t) == TYPE_DECL)
215 t = TREE_TYPE (t);
216 else
217 t = DECL_INITIAL (t);
218 TREE_VEC_ELT (a, i) = t;
219 }
220 args = tree_cons (TREE_PURPOSE (header), a, args);
221 header = TREE_CHAIN (header);
8d08fdba 222 }
5566b478
MS
223 args = nreverse (args);
224 args = TREE_VALUE (args);
8d08fdba 225
5566b478 226 if (! ctx || TYPE_BEING_DEFINED (ctx))
8d08fdba 227 {
5566b478
MS
228 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
229 DECL_TEMPLATE_PARMS (tmpl) = TREE_VALUE (current_template_parms);
230 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
8d08fdba
MS
231 }
232 else
233 {
5566b478
MS
234 if (TREE_CODE (decl) == TYPE_DECL)
235 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
8d08fdba 236 else
5566b478
MS
237 tmpl = DECL_TI_TEMPLATE (decl);
238 }
8d08fdba 239
5566b478
MS
240 DECL_TEMPLATE_RESULT (tmpl) = decl;
241 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 242
5566b478
MS
243 if (! ctx)
244 tmpl = pushdecl_top_level (tmpl);
8d08fdba 245
5566b478
MS
246 if (primary)
247 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl)) = tmpl;
248
249 info = perm_tree_cons (tmpl, args, NULL_TREE);
250
251 if (TREE_CODE (decl) == TYPE_DECL)
8d08fdba 252 {
5566b478
MS
253 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
254 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
51c184be 255 }
ec255269
MS
256 else if (! DECL_LANG_SPECIFIC (decl))
257 cp_error ("template declaration of `%#D'", decl);
51c184be 258 else
5566b478 259 DECL_TEMPLATE_INFO (decl) = info;
8d08fdba
MS
260}
261
75b0bbce 262tree tsubst PROTO ((tree, tree*, int, tree));
8d08fdba
MS
263
264/* Convert all template arguments to their appropriate types, and return
265 a vector containing the resulting values. If any error occurs, return
266 error_mark_node. */
267static tree
268coerce_template_parms (parms, arglist, in_decl)
269 tree parms, arglist;
270 tree in_decl;
271{
a292b002 272 int nparms, nargs, i, lost = 0;
8d08fdba
MS
273 tree vec;
274
a292b002
MS
275 if (arglist == NULL_TREE)
276 nargs = 0;
277 else if (TREE_CODE (arglist) == TREE_VEC)
278 nargs = TREE_VEC_LENGTH (arglist);
8d08fdba 279 else
a292b002
MS
280 nargs = list_length (arglist);
281
282 nparms = TREE_VEC_LENGTH (parms);
283
284 if (nargs > nparms
285 || (nargs < nparms
286 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
8d08fdba
MS
287 {
288 error ("incorrect number of parameters (%d, should be %d)",
a292b002 289 nargs, nparms);
8d08fdba
MS
290 if (in_decl)
291 cp_error_at ("in template expansion for decl `%D'", in_decl);
292 return error_mark_node;
293 }
294
a292b002 295 if (arglist && TREE_CODE (arglist) == TREE_VEC)
8d08fdba
MS
296 vec = copy_node (arglist);
297 else
298 {
299 vec = make_tree_vec (nparms);
300 for (i = 0; i < nparms; i++)
301 {
a292b002
MS
302 tree arg;
303
304 if (arglist)
305 {
306 arg = arglist;
307 arglist = TREE_CHAIN (arglist);
308
309 if (arg == error_mark_node)
310 lost++;
311 else
312 arg = TREE_VALUE (arg);
313 }
8d08fdba 314 else
5566b478
MS
315 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
316 &TREE_VEC_ELT (vec, 0), i, in_decl);
a292b002 317
8d08fdba
MS
318 TREE_VEC_ELT (vec, i) = arg;
319 }
320 }
321 for (i = 0; i < nparms; i++)
322 {
323 tree arg = TREE_VEC_ELT (vec, i);
a292b002 324 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8d08fdba
MS
325 tree val = 0;
326 int is_type, requires_type;
327
328 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
a292b002 329 requires_type = TREE_CODE (parm) == TYPE_DECL;
5566b478
MS
330
331 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
332 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
333 {
334 cp_pedwarn ("to refer to a type member of a template parameter,");
335 cp_pedwarn (" use `typename %E'", arg);
336 arg = make_typename_type (TREE_OPERAND (arg, 0),
337 TREE_OPERAND (arg, 1));
338 is_type = 1;
339 }
8d08fdba
MS
340 if (is_type != requires_type)
341 {
342 if (in_decl)
5566b478
MS
343 {
344 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
345 i, in_decl);
346 if (is_type)
347 cp_error (" expected a constant of type `%T', got `%T'",
348 TREE_TYPE (parm), arg);
349 else
350 cp_error (" expected a type, got `%E'", arg);
351 }
8d08fdba
MS
352 lost++;
353 TREE_VEC_ELT (vec, i) = error_mark_node;
354 continue;
355 }
356 if (is_type)
ec255269
MS
357 {
358 val = groktypename (arg);
359 if (! current_template_parms)
360 {
361 tree t = target_type (val);
362 if (IS_AGGR_TYPE (t)
363 && decl_function_context (TYPE_MAIN_DECL (t)))
364 {
365 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
366 return error_mark_node;
367 }
368 }
369 }
8d08fdba
MS
370 else
371 {
75b0bbce
RK
372 tree t = tsubst (TREE_TYPE (parm), &TREE_VEC_ELT (vec, 0),
373 TREE_VEC_LENGTH (vec), in_decl);
ec255269 374 if (current_template_parms)
5566b478
MS
375 val = arg;
376 else
377 val = digest_init (t, arg, (tree *) 0);
a292b002 378
5566b478 379 if (val == error_mark_node || current_template_parms)
8d08fdba
MS
380 ;
381
382 /* 14.2: Other template-arguments must be constant-expressions,
383 addresses of objects or functions with external linkage, or of
384 static class members. */
385 else if (!TREE_CONSTANT (val))
386 {
387 cp_error ("non-const `%E' cannot be used as template argument",
388 arg);
389 val = error_mark_node;
390 }
f30432d7
MS
391 else if (POINTER_TYPE_P (TREE_TYPE (val))
392 && ! integer_zerop (val)
393 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
394 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
8d08fdba 395 {
f30432d7
MS
396 t = val;
397 STRIP_NOPS (t);
398 if (TREE_CODE (t) == ADDR_EXPR)
399 {
400 tree a = TREE_OPERAND (t, 0);
401 STRIP_NOPS (a);
402 if (TREE_CODE (a) == STRING_CST)
403 {
404 cp_error ("string literal %E is not a valid template argument", a);
405 error ("because it is the address of an object with static linkage");
406 val = error_mark_node;
407 }
408 else if (TREE_CODE (a) != VAR_DECL
409 && TREE_CODE (a) != FUNCTION_DECL)
410 goto bad;
411 else if (! DECL_PUBLIC (a))
412 {
413 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
414 val = error_mark_node;
415 }
416 }
417 else
8d08fdba 418 {
f30432d7
MS
419 bad:
420 cp_error ("`%E' is not a valid template argument", t);
421 error ("it must be %s%s with external linkage",
422 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
423 ? "a pointer to " : "",
424 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
425 ? "a function" : "an object");
8d08fdba
MS
426 val = error_mark_node;
427 }
428 }
429 }
430
431 if (val == error_mark_node)
432 lost++;
433
434 TREE_VEC_ELT (vec, i) = val;
435 }
436 if (lost)
437 return error_mark_node;
438 return vec;
439}
440
5566b478
MS
441int
442comp_template_args (oldargs, newargs)
443 tree oldargs, newargs;
444{
445 int i;
446
447 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
448 {
449 tree nt = TREE_VEC_ELT (newargs, i);
450 tree ot = TREE_VEC_ELT (oldargs, i);
451
452 if (nt == ot)
453 continue;
454 if (TREE_CODE (nt) != TREE_CODE (ot))
455 return 0;
456 if (TREE_CODE (ot) == TEMPLATE_TYPE_PARM
457 && comptypes (ot, nt, 1))
458 continue;
459 if (TREE_CODE (ot) == TEMPLATE_CONST_PARM
460 && TEMPLATE_CONST_IDX (nt) == TEMPLATE_CONST_IDX (ot))
461 continue;
462 return 0;
463 }
464 return 1;
465}
466
8d08fdba
MS
467/* Given class template name and parameter list, produce a user-friendly name
468 for the instantiation. */
469static char *
470mangle_class_name_for_template (name, parms, arglist)
471 char *name;
472 tree parms, arglist;
473{
474 static struct obstack scratch_obstack;
475 static char *scratch_firstobj;
476 int i, nparms;
8d08fdba
MS
477
478 if (!scratch_firstobj)
479 {
480 gcc_obstack_init (&scratch_obstack);
481 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
482 }
483 else
484 obstack_free (&scratch_obstack, scratch_firstobj);
485
486#if 0
487#define buflen sizeof(buf)
488#define check if (bufp >= buf+buflen-1) goto too_long
489#define ccat(c) *bufp++=(c); check
490#define advance bufp+=strlen(bufp); check
491#define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
492#else
493#define check
494#define ccat(c) obstack_1grow (&scratch_obstack, (c));
495#define advance
496#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
497#endif
8d08fdba
MS
498
499 cat (name);
500 ccat ('<');
501 nparms = TREE_VEC_LENGTH (parms);
502 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
503 for (i = 0; i < nparms; i++)
504 {
a292b002
MS
505 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
506 tree arg = TREE_VEC_ELT (arglist, i);
8d08fdba
MS
507
508 if (i)
509 ccat (',');
510
a292b002 511 if (TREE_CODE (parm) == TYPE_DECL)
8d08fdba
MS
512 {
513 cat (type_as_string (arg, 0));
514 continue;
515 }
516 else
517 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
518
519 if (TREE_CODE (arg) == TREE_LIST)
520 {
521 /* New list cell was built because old chain link was in
522 use. */
523 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
524 arg = TREE_VALUE (arg);
525 }
526 /* No need to check arglist against parmlist here; we did that
527 in coerce_template_parms, called from lookup_template_class. */
528 cat (expr_as_string (arg, 0));
529 }
530 {
531 char *bufp = obstack_next_free (&scratch_obstack);
532 int offset = 0;
533 while (bufp[offset - 1] == ' ')
534 offset--;
535 obstack_blank_fast (&scratch_obstack, offset);
536
537 /* B<C<char> >, not B<C<char>> */
538 if (bufp[offset - 1] == '>')
539 ccat (' ');
540 }
541 ccat ('>');
542 ccat ('\0');
543 return (char *) obstack_base (&scratch_obstack);
544
8926095f 545#if 0
8d08fdba 546 too_long:
8926095f 547#endif
8d08fdba
MS
548 fatal ("out of (preallocated) string space creating template instantiation name");
549 /* NOTREACHED */
550 return NULL;
551}
552
5566b478
MS
553tree
554classtype_mangled_name (t)
555 tree t;
556{
557 if (CLASSTYPE_TEMPLATE_INFO (t)
558 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
559 {
560 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
561 char *mangled_name = mangle_class_name_for_template
562 (IDENTIFIER_POINTER (name),
563 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
564 CLASSTYPE_TI_ARGS (t));
565 tree id = get_identifier (mangled_name);
566 IDENTIFIER_TEMPLATE (id) = name;
567 return id;
568 }
569 else
570 return TYPE_IDENTIFIER (t);
571}
572
573static void
574add_pending_template (d)
575 tree d;
576{
577 if (TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d)))
578 return;
579
580 *template_tail = perm_tree_cons
581 (current_function_decl, d, NULL_TREE);
582 template_tail = &TREE_CHAIN (*template_tail);
583 TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d)) = 1;
584}
585
8d08fdba
MS
586/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
587 parameters, find the desired type.
588
589 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
590 Since ARGLIST is build on the decl_obstack, we must copy it here
591 to keep it from being reclaimed when the decl storage is reclaimed.
592
593 IN_DECL, if non-NULL, is the template declaration we are trying to
594 instantiate. */
595tree
596lookup_template_class (d1, arglist, in_decl)
597 tree d1, arglist;
598 tree in_decl;
599{
600 tree template, parmlist;
601 char *mangled_name;
5566b478
MS
602 tree id, t;
603 tree code_type_node;
604
605 if (TREE_CODE (d1) == IDENTIFIER_NODE)
606 {
607 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
608 if (! template)
609 template = IDENTIFIER_CLASS_VALUE (d1);
610 }
611 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
612 {
613 template = CLASSTYPE_TI_TEMPLATE (d1);
614 d1 = DECL_NAME (template);
615 }
616 else
617 my_friendly_abort (272);
8d08fdba 618
8d08fdba
MS
619 /* With something like `template <class T> class X class X { ... };'
620 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
621 We don't want to do that, but we have to deal with the situation, so
622 let's give them some syntax errors to chew on instead of a crash. */
623 if (! template)
624 return error_mark_node;
625 if (TREE_CODE (template) != TEMPLATE_DECL)
626 {
627 cp_error ("non-template type `%T' used as a template", d1);
628 if (in_decl)
629 cp_error_at ("for template declaration `%D'", in_decl);
630 return error_mark_node;
631 }
8d08fdba 632
5566b478
MS
633 if (TREE_CODE (TREE_TYPE (template)) == RECORD_TYPE)
634 code_type_node = (CLASSTYPE_DECLARED_CLASS (TREE_TYPE (template))
635 ? class_type_node : record_type_node);
8d08fdba 636 else
5566b478
MS
637 code_type_node = union_type_node;
638
639 if (PRIMARY_TEMPLATE_P (template))
8d08fdba 640 {
5566b478
MS
641 parmlist = DECL_TEMPLATE_PARMS (template);
642
643 arglist = coerce_template_parms (parmlist, arglist, template);
644 if (arglist == error_mark_node)
645 return error_mark_node;
646 if (uses_template_parms (arglist))
647 {
648 tree found;
649 if (comp_template_args
650 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
651 found = TREE_TYPE (template);
652 else
653 {
654 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
655 found; found = TREE_CHAIN (found))
656 {
657 if (TI_USES_TEMPLATE_PARMS (found)
658 && comp_template_args (TREE_PURPOSE (found), arglist))
659 break;
660 }
661 if (found)
662 found = TREE_VALUE (found);
663 }
664
665 if (found)
666 {
667 if (can_free (&permanent_obstack, arglist))
668 obstack_free (&permanent_obstack, arglist);
669 return found;
670 }
671 }
672
8d08fdba
MS
673 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
674 parmlist, arglist);
675 id = get_identifier (mangled_name);
5566b478 676 IDENTIFIER_TEMPLATE (id) = d1;
8d08fdba 677
5566b478
MS
678 maybe_push_to_top_level (uses_template_parms (arglist));
679 t = xref_tag (code_type_node, id, NULL_TREE, 1);
680 pop_from_top_level ();
8926095f 681 }
5566b478 682 else
8d08fdba 683 {
5566b478
MS
684 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
685 arglist, in_decl);
686 id = d1;
687 arglist = CLASSTYPE_TI_ARGS (ctx);
8d08fdba 688
5566b478 689 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
8d08fdba 690 {
5566b478
MS
691 tree save_parms = current_template_parms;
692 current_template_parms = NULL_TREE;
693 t = xref_tag (code_type_node, id, NULL_TREE, 0);
694 current_template_parms = save_parms;
8d08fdba
MS
695 }
696 else
697 {
5566b478
MS
698 t = lookup_nested_type_by_name (ctx, id);
699 my_friendly_assert (t != NULL_TREE, 42);
8d08fdba 700 }
5566b478
MS
701 }
702
703 /* Seems to be wanted. */
704 CLASSTYPE_GOT_SEMICOLON (t) = 1;
705
706 if (! CLASSTYPE_TEMPLATE_INFO (t))
707 {
708 arglist = copy_to_permanent (arglist);
709 CLASSTYPE_TEMPLATE_INFO (t)
710 = perm_tree_cons (template, arglist, NULL_TREE);
711 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
712 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
713 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
714 = uses_template_parms (arglist);
715
716 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
717
718 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
719 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
720 if (! uses_template_parms (arglist))
721 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
722 = get_identifier (build_overload_name (t, 1, 1));
723
724 if (flag_external_templates && ! uses_template_parms (arglist)
725 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
726 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
8d08fdba 727 {
5566b478
MS
728 tree d;
729
730 instantiate_class_template (t);
731
732 for (d = TYPE_METHODS (t); d; d = TREE_CHAIN (d))
733 add_pending_template (d);
8d08fdba
MS
734 }
735 }
8d08fdba 736
5566b478 737 return t;
8d08fdba
MS
738}
739\f
51c184be 740/* Should be defined in parse.h. */
8d08fdba
MS
741extern int yychar;
742
743int
744uses_template_parms (t)
745 tree t;
746{
747 if (!t)
748 return 0;
749 switch (TREE_CODE (t))
750 {
751 case INDIRECT_REF:
752 case COMPONENT_REF:
753 /* We assume that the object must be instantiated in order to build
754 the COMPONENT_REF, so we test only whether the type of the
755 COMPONENT_REF uses template parms. */
756 return uses_template_parms (TREE_TYPE (t));
757
758 case IDENTIFIER_NODE:
759 if (!IDENTIFIER_TEMPLATE (t))
760 return 0;
5566b478 761 my_friendly_abort (42);
8d08fdba
MS
762
763 /* aggregates of tree nodes */
764 case TREE_VEC:
765 {
766 int i = TREE_VEC_LENGTH (t);
767 while (i--)
768 if (uses_template_parms (TREE_VEC_ELT (t, i)))
769 return 1;
770 return 0;
771 }
772 case TREE_LIST:
773 if (uses_template_parms (TREE_PURPOSE (t))
774 || uses_template_parms (TREE_VALUE (t)))
775 return 1;
776 return uses_template_parms (TREE_CHAIN (t));
777
778 /* constructed type nodes */
779 case POINTER_TYPE:
780 case REFERENCE_TYPE:
781 return uses_template_parms (TREE_TYPE (t));
782 case RECORD_TYPE:
b7484fbe
MS
783 if (TYPE_PTRMEMFUNC_FLAG (t))
784 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
8d08fdba 785 case UNION_TYPE:
5566b478 786 if (! CLASSTYPE_TEMPLATE_INFO (t))
8d08fdba 787 return 0;
5566b478 788 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
8d08fdba
MS
789 case FUNCTION_TYPE:
790 if (uses_template_parms (TYPE_ARG_TYPES (t)))
791 return 1;
792 return uses_template_parms (TREE_TYPE (t));
793 case ARRAY_TYPE:
794 if (uses_template_parms (TYPE_DOMAIN (t)))
795 return 1;
796 return uses_template_parms (TREE_TYPE (t));
797 case OFFSET_TYPE:
798 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
799 return 1;
800 return uses_template_parms (TREE_TYPE (t));
801 case METHOD_TYPE:
75b0bbce 802 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
8d08fdba
MS
803 return 1;
804 if (uses_template_parms (TYPE_ARG_TYPES (t)))
805 return 1;
806 return uses_template_parms (TREE_TYPE (t));
807
808 /* decl nodes */
809 case TYPE_DECL:
5566b478
MS
810 return uses_template_parms (TREE_TYPE (t));
811
8d08fdba 812 case FUNCTION_DECL:
5566b478
MS
813 case VAR_DECL:
814 /* ??? What about FIELD_DECLs? */
815 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
816 && uses_template_parms (DECL_TI_ARGS (t)))
8d08fdba
MS
817 return 1;
818 /* fall through */
5566b478 819 case CONST_DECL:
8d08fdba 820 case PARM_DECL:
5566b478
MS
821 if (uses_template_parms (TREE_TYPE (t)))
822 return 1;
8d08fdba
MS
823 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
824 return 1;
8d08fdba
MS
825 return 0;
826
827 case CALL_EXPR:
828 return uses_template_parms (TREE_TYPE (t));
829 case ADDR_EXPR:
830 return uses_template_parms (TREE_OPERAND (t, 0));
831
832 /* template parm nodes */
833 case TEMPLATE_TYPE_PARM:
834 case TEMPLATE_CONST_PARM:
835 return 1;
836
837 /* simple type nodes */
838 case INTEGER_TYPE:
839 if (uses_template_parms (TYPE_MIN_VALUE (t)))
840 return 1;
841 return uses_template_parms (TYPE_MAX_VALUE (t));
842
843 case REAL_TYPE:
844 case VOID_TYPE:
845 case ENUMERAL_TYPE:
2986ae00 846 case BOOLEAN_TYPE:
8d08fdba
MS
847 return 0;
848
849 /* constants */
850 case INTEGER_CST:
851 case REAL_CST:
852 case STRING_CST:
853 return 0;
854
855 case ERROR_MARK:
856 /* Non-error_mark_node ERROR_MARKs are bad things. */
857 my_friendly_assert (t == error_mark_node, 274);
858 /* NOTREACHED */
859 return 0;
860
ec255269 861 case LOOKUP_EXPR:
5566b478 862 case TYPENAME_TYPE:
8d08fdba
MS
863 return 1;
864
db5ae43f
MS
865 case CONSTRUCTOR:
866 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
867 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
868 /* else fall through */
869
8d08fdba
MS
870 default:
871 switch (TREE_CODE_CLASS (TREE_CODE (t)))
872 {
873 case '1':
874 case '2':
ec255269 875 case 'e':
8d08fdba
MS
876 case '<':
877 {
878 int i;
879 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
880 if (uses_template_parms (TREE_OPERAND (t, i)))
881 return 1;
882 return 0;
883 }
884 default:
885 break;
886 }
887 sorry ("testing %s for template parms",
888 tree_code_name [(int) TREE_CODE (t)]);
889 my_friendly_abort (82);
890 /* NOTREACHED */
891 return 0;
892 }
893}
894
7215f9a0
MS
895static struct tinst_level *current_tinst_level = 0;
896static struct tinst_level *free_tinst_level = 0;
897static int tinst_depth = 0;
898int max_tinst_depth = 17;
5566b478
MS
899#ifdef GATHER_STATISTICS
900int depth_reached = 0;
901#endif
8d08fdba 902
7215f9a0 903int
5566b478
MS
904push_tinst_level (d)
905 tree d;
8d08fdba
MS
906{
907 struct tinst_level *new;
8d08fdba 908
7215f9a0
MS
909 if (tinst_depth >= max_tinst_depth)
910 {
5566b478
MS
911 struct tinst_level *p = current_tinst_level;
912 int line = lineno;
913 char *file = input_filename;
914
7215f9a0
MS
915 error ("template instantiation depth exceeds maximum of %d",
916 max_tinst_depth);
5566b478
MS
917 cp_error (" instantiating `%D'", d);
918
919 for (; p; p = p->next)
920 {
921 cp_error (" instantiated from `%D'", p->decl);
922 lineno = p->line;
923 input_filename = p->file;
924 }
925 error (" instantiated from here");
926
927 lineno = line;
928 input_filename = file;
929
7215f9a0
MS
930 return 0;
931 }
932
8d08fdba
MS
933 if (free_tinst_level)
934 {
935 new = free_tinst_level;
936 free_tinst_level = new->next;
937 }
938 else
939 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
940
5566b478
MS
941 new->decl = d;
942 new->line = lineno;
943 new->file = input_filename;
8d08fdba
MS
944 new->next = current_tinst_level;
945 current_tinst_level = new;
5566b478 946
7215f9a0 947 ++tinst_depth;
5566b478
MS
948#ifdef GATHER_STATISTICS
949 if (tinst_depth > depth_reached)
950 depth_reached = tinst_depth;
951#endif
952
7215f9a0 953 return 1;
8d08fdba
MS
954}
955
956void
957pop_tinst_level ()
958{
959 struct tinst_level *old = current_tinst_level;
960
961 current_tinst_level = old->next;
962 old->next = free_tinst_level;
963 free_tinst_level = old;
7215f9a0 964 --tinst_depth;
8d08fdba
MS
965}
966
967struct tinst_level *
968tinst_for_decl ()
969{
970 struct tinst_level *p = current_tinst_level;
971
972 if (p)
973 for (; p->next ; p = p->next )
974 ;
975 return p;
976}
977
978tree
5566b478
MS
979instantiate_class_template (type)
980 tree type;
8d08fdba 981{
5566b478 982 tree template, template_info, args, pattern, t, *field_chain, *tag_chain;
8d08fdba 983
5566b478 984 if (type == error_mark_node)
8d08fdba
MS
985 return error_mark_node;
986
5566b478
MS
987 template_info = CLASSTYPE_TEMPLATE_INFO (type);
988
989 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
990 return type;
991
992 template = TI_TEMPLATE (template_info);
993 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
994 args = TI_ARGS (template_info);
995 pattern = TREE_TYPE (template);
996
997 if (TYPE_SIZE (pattern) == NULL_TREE)
ec255269 998 return type;
5566b478
MS
999
1000 TYPE_BEING_DEFINED (type) = 1;
1001
1002 if (! push_tinst_level (type))
1003 return type;
8d08fdba 1004
5566b478
MS
1005 maybe_push_to_top_level (uses_template_parms (type));
1006 pushclass (type, 0);
1007
1008 if (flag_external_templates)
1009 {
1010 if (flag_alt_external_templates)
1011 {
1012 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1013 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1014 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1015 = ! CLASSTYPE_INTERFACE_ONLY (type)
1016 && CLASSTYPE_INTERFACE_KNOWN (type);
1017 }
1018 else
1019 {
1020 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1021 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1022 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1023 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1024 = ! CLASSTYPE_INTERFACE_ONLY (type)
1025 && CLASSTYPE_INTERFACE_KNOWN (type);
1026 }
1027 }
1028 else
8d08fdba 1029 {
5566b478
MS
1030 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1031 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
8d08fdba
MS
1032 }
1033
5566b478
MS
1034 {
1035 tree binfo = TYPE_BINFO (type);
1036 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1037
1038 if (pbases)
1039 {
1040 tree bases;
1041 int i;
1042 int len = TREE_VEC_LENGTH (pbases);
1043 BINFO_BASETYPES (binfo) = bases = make_tree_vec (len);
1044 for (i = 0; i < len; ++i)
1045 {
1046 tree elt;
1047
1048 TREE_VEC_ELT (bases, i) = elt
1049 = tsubst (TREE_VEC_ELT (pbases, i), &TREE_VEC_ELT (args, 0),
1050 TREE_VEC_LENGTH (args), NULL_TREE);
1051 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1052
1053 if (! uses_template_parms (type) &&
1054 TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE)
1055 cp_error ("base class `%T' of `%T' has incomplete type",
1056 TREE_TYPE (elt), type);
1057 }
1058 }
1059 }
1060
1061 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1062
1063 field_chain = &TYPE_FIELDS (type);
1064 tag_chain = &CLASSTYPE_TAGS (type);
1065
1066 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
8d08fdba 1067 {
5566b478
MS
1068 tree name = TREE_PURPOSE (t);
1069 tree tag = TREE_VALUE (t);
1070 tree newtag;
1071
1072 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1073 newtag = start_enum (name);
1074 else
1075 newtag = tsubst (tag, &TREE_VEC_ELT (args, 0),
1076 TREE_VEC_LENGTH (args), NULL_TREE);
1077
1078 *tag_chain = build_tree_list (name, newtag);
1079 tag_chain = &TREE_CHAIN (*tag_chain);
1080
1081 if (TREE_CODE (tag) == ENUMERAL_TYPE)
8d08fdba 1082 {
5566b478
MS
1083 tree e, values = NULL_TREE, *last = &values;
1084
1085 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
1086 {
1087 tree elt = build_enumerator
1088 (TREE_PURPOSE (e),
1089 tsubst_expr (TREE_VALUE (e), &TREE_VEC_ELT (args, 0),
1090 TREE_VEC_LENGTH (args), NULL_TREE));
1091 DECL_FIELD_CONTEXT (TREE_VALUE (elt)) = type;
1092 *last = elt;
1093 last = &TREE_CHAIN (elt);
1094 }
1095
1096 finish_enum (newtag, values);
1097
1098 *field_chain = grok_enum_decls (newtag, NULL_TREE);
1099 while (*field_chain)
1100 field_chain = &TREE_CHAIN (*field_chain);
8d08fdba 1101 }
8d08fdba
MS
1102 }
1103
5566b478
MS
1104 /* Don't replace enum constants here. */
1105 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1106 if (TREE_CODE (t) != CONST_DECL)
1107 {
1108 tree r = tsubst (t, &TREE_VEC_ELT (args, 0),
1109 TREE_VEC_LENGTH (args), NULL_TREE);
1110 if (TREE_CODE (r) == VAR_DECL)
1111 {
1112 if (! uses_template_parms (r))
1113 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1114 /* Perhaps I should do more of grokfield here. */
1115 start_decl_1 (r);
1116 DECL_IN_AGGR_P (r) = 1;
1117 DECL_EXTERNAL (r) = 1;
1118 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1119 }
1120
1121 *field_chain = r;
1122 field_chain = &TREE_CHAIN (r);
1123 }
8d08fdba 1124
5566b478 1125 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
8d08fdba 1126
5566b478
MS
1127 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1128 = tsubst_chain (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)), args);
1129 CLASSTYPE_FRIEND_CLASSES (type)
1130 = tsubst_chain (CLASSTYPE_FRIEND_CLASSES (pattern), args);
1131
1132 {
1133 tree d = tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args, 0),
1134 TREE_VEC_LENGTH (args), NULL_TREE);
1135
1136 for (; d; d = TREE_CHAIN (d))
1137 pushdecl (TREE_VALUE (d));
1138 }
1139
1140 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1141 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1142 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1143 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1144 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1145 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1146 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1147 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1148 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1149 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1150 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1151 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1152 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1153 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1154 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1155 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1156 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1157 TYPE_USES_COMPLEX_INHERITANCE (type)
1158 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1159 TYPE_USES_VIRTUAL_BASECLASSES (type)
1160 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1161
1162 if (! uses_template_parms (type))
8d08fdba 1163 {
5566b478
MS
1164 tree tmp;
1165 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1166 if (TREE_CODE (tmp) == FIELD_DECL
1167 && TREE_CODE (DECL_NAME (tmp)) != SCOPE_REF)
1168 require_complete_type (tmp);
1169
1170 /* XXX handle attributes */
1171 type = finish_struct_1 (type, 0, 0);
8d08fdba
MS
1172 }
1173 else
1174 {
5566b478
MS
1175 TYPE_SIZE (type) = integer_zero_node;
1176 CLASSTYPE_METHOD_VEC (type)
1177 = finish_struct_methods (type, TYPE_METHODS (type), 1);
8d08fdba
MS
1178 }
1179
5566b478
MS
1180 TYPE_BEING_DEFINED (type) = 0;
1181 popclass (0);
1182
1183 pop_from_top_level ();
1184 pop_tinst_level ();
1185
1186 return type;
8d08fdba
MS
1187}
1188
1189static int
1190list_eq (t1, t2)
1191 tree t1, t2;
1192{
1193 if (t1 == NULL_TREE)
1194 return t2 == NULL_TREE;
1195 if (t2 == NULL_TREE)
1196 return 0;
1197 /* Don't care if one declares its arg const and the other doesn't -- the
1198 main variant of the arg type is all that matters. */
1199 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
1200 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
1201 return 0;
1202 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
1203}
1204
5566b478 1205tree
8d08fdba
MS
1206lookup_nested_type_by_name (ctype, name)
1207 tree ctype, name;
1208{
1209 tree t;
1210
5566b478
MS
1211 complete_type (ctype);
1212
db5ae43f
MS
1213 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
1214 {
1215 if (name == TREE_PURPOSE (t))
1216 return TREE_VALUE (t);
1217 }
8d08fdba
MS
1218 return NULL_TREE;
1219}
1220
75b0bbce 1221tree
8d08fdba
MS
1222tsubst (t, args, nargs, in_decl)
1223 tree t, *args;
1224 int nargs;
1225 tree in_decl;
1226{
1227 tree type;
1228
5566b478
MS
1229 if (t == NULL_TREE || t == error_mark_node
1230 || t == integer_type_node
1231 || t == void_type_node
1232 || t == char_type_node)
8d08fdba
MS
1233 return t;
1234
1235 type = TREE_TYPE (t);
5566b478
MS
1236 if (type == unknown_type_node)
1237 my_friendly_abort (42);
1238 if (type && TREE_CODE (t) != FUNCTION_DECL)
b7484fbe
MS
1239 type = tsubst (type, args, nargs, in_decl);
1240
8d08fdba
MS
1241 switch (TREE_CODE (t))
1242 {
1243 case RECORD_TYPE:
1244 if (TYPE_PTRMEMFUNC_P (t))
5566b478
MS
1245 {
1246 tree r = build_ptrmemfunc_type
1247 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
1248 return cp_build_type_variant (r, TYPE_READONLY (t),
1249 TYPE_VOLATILE (t));
1250 }
1251
8d08fdba 1252 /* else fall through */
5566b478
MS
1253 case UNION_TYPE:
1254 if (uses_template_parms (t))
1255 {
1256 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
1257 tree r = lookup_template_class (t, argvec, in_decl);
1258 return cp_build_type_variant (r, TYPE_READONLY (t),
1259 TYPE_VOLATILE (t));
1260 }
8d08fdba 1261
5566b478 1262 /* else fall through */
8d08fdba
MS
1263 case ERROR_MARK:
1264 case IDENTIFIER_NODE:
1265 case OP_IDENTIFIER:
1266 case VOID_TYPE:
1267 case REAL_TYPE:
2986ae00 1268 case BOOLEAN_TYPE:
8d08fdba
MS
1269 case INTEGER_CST:
1270 case REAL_CST:
1271 case STRING_CST:
8d08fdba
MS
1272 return t;
1273
5566b478
MS
1274 case ENUMERAL_TYPE:
1275 {
1276 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1277 if (ctx == NULL_TREE)
1278 return t;
1279 else
1280 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
1281 }
1282
8d08fdba
MS
1283 case INTEGER_TYPE:
1284 if (t == integer_type_node)
1285 return t;
1286
1287 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
1288 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
1289 return t;
5566b478
MS
1290
1291 {
1292 tree max = tsubst_expr (TYPE_MAX_VALUE (t), args, nargs, in_decl);
1293 if (current_template_parms)
1294 {
1295 tree itype = make_node (INTEGER_TYPE);
1296 TYPE_MIN_VALUE (itype) = size_zero_node;
1297 TYPE_MAX_VALUE (itype) = max;
1298 return itype;
1299 }
1300 return build_index_2_type (size_zero_node, max);
1301 }
8d08fdba
MS
1302
1303 case TEMPLATE_TYPE_PARM:
db5ae43f
MS
1304 {
1305 tree arg = args[TEMPLATE_TYPE_IDX (t)];
1306 return cp_build_type_variant
1307 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
1308 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
1309 }
8d08fdba
MS
1310
1311 case TEMPLATE_CONST_PARM:
1312 return args[TEMPLATE_CONST_IDX (t)];
1313
1314 case FUNCTION_DECL:
1315 {
5566b478
MS
1316 tree r = NULL_TREE;
1317 tree arg_types, ctx;
1318
1319 int member;
1320
8d08fdba
MS
1321 if (DECL_CONTEXT (t) != NULL_TREE
1322 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
1323 {
5566b478
MS
1324 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
1325 member = 2;
1326 else
1327 member = 1;
1328 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
1329 type = tsubst (type, args, nargs, in_decl);
1330 }
1331 else
1332 {
1333 member = 0;
1334 ctx = NULL_TREE;
1335 type = tsubst (type, args, nargs, in_decl);
1336 }
8d08fdba 1337
5566b478
MS
1338 if (type == TREE_TYPE (t)
1339 && (! member || ctx == DECL_CLASS_CONTEXT (t)))
1340 return t;
8145f082 1341
5566b478
MS
1342 /* Do we already have this instantiation? */
1343 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1344 {
1345 tree tmpl = TREE_PURPOSE (DECL_TEMPLATE_INFO (t));
1346 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1347
1348 for (; decls; decls = TREE_CHAIN (decls))
1349 if (TREE_TYPE (TREE_VALUE (decls)) == type
1350 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
1351 return TREE_VALUE (decls);
1352 }
1353
1354 /* We do NOT check for matching decls pushed separately at this
1355 point, as they may not represent instantiations of this
1356 template, and in any case are considered separate under the
1357 discrete model. */
1358
1359 r = copy_node (t);
1360 copy_lang_decl (r);
1361 TREE_TYPE (r) = type;
1362
1363 DECL_CONTEXT (r)
1364 = tsubst (DECL_CONTEXT (t), args, nargs, t);
1365 DECL_CLASS_CONTEXT (r) = ctx;
1366
1367 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
1368 IDENTIFIER_POINTER (DECL_NAME (r)),
1369 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
1370 {
1371 /* Type-conversion operator. Reconstruct the name, in
1372 case it's the name of one of the template's parameters. */
1373 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
1374 }
1375
1376 arg_types = TYPE_VALUES (type);
1377
1378 if (member && TREE_CODE (type) == FUNCTION_TYPE)
1379 arg_types = hash_tree_chain
1380 (build_pointer_type (DECL_CONTEXT (r)), arg_types);
1381
1382 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
1383 {
1384 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
1385 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
1386 buf = (char *) alloca (strlen (dbuf)
1387 + sizeof (DESTRUCTOR_DECL_PREFIX));
1388 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
1389 buf[len] = '\0';
1390 strcat (buf, dbuf);
1391 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
8d08fdba
MS
1392 }
1393 else
5566b478
MS
1394 DECL_ASSEMBLER_NAME (r)
1395 = build_decl_overload (DECL_NAME (r), arg_types, member);
1396 DECL_RTL (r) = 0;
1397 make_decl_rtl (r, NULL_PTR, 1);
1398
1399 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
1400 DECL_MAIN_VARIANT (r) = r;
1401 DECL_RESULT (r) = NULL_TREE;
1402 DECL_INITIAL (r) = NULL_TREE;
1403
1404 TREE_STATIC (r) = 0;
1405 TREE_PUBLIC (r) = 1;
1406 DECL_EXTERNAL (r) = 1;
1407 DECL_INTERFACE_KNOWN (r) = 0;
1408 DECL_DEFER_OUTPUT (r) = 0;
1409 TREE_CHAIN (r) = NULL_TREE;
1410 DECL_CHAIN (r) = NULL_TREE;
1411
1412 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
1413 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
1414
1415 /* Look for matching decls for the moment. */
1416 if (! member)
8d08fdba 1417 {
5566b478
MS
1418 tree decls = lookup_name_nonclass (DECL_NAME (t));
1419 tree d = NULL_TREE;
1420
1421 if (decls == NULL_TREE)
1422 /* no match */;
1423 else if (is_overloaded_fn (decls))
1424 for (decls = get_first_fn (decls); decls;
1425 decls = DECL_CHAIN (decls))
8d08fdba 1426 {
5566b478
MS
1427 if (TREE_CODE (decls) == FUNCTION_DECL
1428 && TREE_TYPE (decls) == type)
8d08fdba 1429 {
5566b478
MS
1430 d = decls;
1431 break;
8d08fdba
MS
1432 }
1433 }
1434
5566b478
MS
1435 if (d)
1436 {
1437 int dcl_only = ! DECL_INITIAL (d);
1438 if (dcl_only)
1439 DECL_INITIAL (r) = error_mark_node;
1440 duplicate_decls (r, d);
1441 r = d;
1442 if (dcl_only)
1443 DECL_INITIAL (r) = 0;
1444 }
1445 }
1446
1447 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
1448 {
1449 tree tmpl = DECL_TI_TEMPLATE (t);
1450 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1451 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1452 args, nargs, in_decl);
1453
1454 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1455 *declsp = perm_tree_cons (argvec, r, *declsp);
1456
1457 /* If we have a preexisting version of this function, don't expand
1458 the template version, use the other instead. */
1459 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
1460 SET_DECL_TEMPLATE_SPECIALIZATION (r);
1461 else
1462 SET_DECL_IMPLICIT_INSTANTIATION (r);
1463
1464 DECL_TEMPLATE_INSTANTIATIONS (tmpl) =
1465 tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
8d08fdba 1466 }
8d08fdba 1467
8d08fdba
MS
1468 return r;
1469 }
1470
1471 case PARM_DECL:
1472 {
5566b478
MS
1473 tree r = copy_node (t);
1474 TREE_TYPE (r) = type;
8d08fdba 1475 DECL_INITIAL (r) = TREE_TYPE (r);
5566b478 1476 DECL_CONTEXT (r) = NULL_TREE;
f83b0cb6
JM
1477#ifdef PROMOTE_PROTOTYPES
1478 if ((TREE_CODE (type) == INTEGER_TYPE
1479 || TREE_CODE (type) == ENUMERAL_TYPE)
1480 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
1481 DECL_ARG_TYPE (r) = integer_type_node;
1482#endif
8d08fdba
MS
1483 if (TREE_CHAIN (t))
1484 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
1485 return r;
1486 }
1487
5566b478
MS
1488 case FIELD_DECL:
1489 {
1490 tree r = copy_node (t);
1491 TREE_TYPE (r) = type;
1492 copy_lang_decl (r);
1493#if 0
1494 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
1495#endif
1496 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
1497 TREE_CHAIN (r) = NULL_TREE;
1498 return r;
1499 }
1500
1501 case USING_DECL:
1502 {
1503 tree r = copy_node (t);
1504 DECL_INITIAL (r)
1505 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
1506 TREE_CHAIN (r) = NULL_TREE;
1507 return r;
1508 }
1509
1510 case VAR_DECL:
1511 {
1512 tree r;
1513 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
1514
1515 /* Do we already have this instantiation? */
1516 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1517 {
1518 tree tmpl = DECL_TI_TEMPLATE (t);
1519 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1520
1521 for (; decls; decls = TREE_CHAIN (decls))
1522 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
1523 return TREE_VALUE (decls);
1524 }
1525
1526 r = copy_node (t);
1527 TREE_TYPE (r) = type;
1528 DECL_CONTEXT (r) = ctx;
1529 if (TREE_STATIC (r))
1530 DECL_ASSEMBLER_NAME (r)
1531 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
1532 DECL_INITIAL (r) = tsubst_expr
1533 (DECL_INITIAL (t), args, nargs, in_decl);
1534
1535 DECL_RTL (r) = 0;
1536 DECL_SIZE (r) = 0;
1537
1538 if (DECL_LANG_SPECIFIC (r))
1539 {
1540 copy_lang_decl (r);
1541 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
1542 }
1543
1544 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1545 {
1546 tree tmpl = DECL_TI_TEMPLATE (t);
1547 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1548 tree argvec = tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t)),
1549 args, nargs, in_decl);
1550
1551 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
1552 *declsp = perm_tree_cons (argvec, r, *declsp);
1553 SET_DECL_IMPLICIT_INSTANTIATION (r);
1554 }
1555 TREE_CHAIN (r) = NULL_TREE;
1556 return r;
1557 }
1558
1559 case TYPE_DECL:
1560 {
1561 tree r = copy_node (t);
1562 TREE_TYPE (r) = type;
1563 set_nested_typename (r, current_class_name, DECL_NAME (r), type);
1564 TREE_CHAIN (r) = NULL_TREE;
1565 return r;
1566 }
1567
8d08fdba
MS
1568 case TREE_LIST:
1569 {
1570 tree purpose, value, chain, result;
1571 int via_public, via_virtual, via_protected;
1572
1573 if (t == void_list_node)
1574 return t;
1575
1576 via_public = TREE_VIA_PUBLIC (t);
1577 via_protected = TREE_VIA_PROTECTED (t);
1578 via_virtual = TREE_VIA_VIRTUAL (t);
1579
1580 purpose = TREE_PURPOSE (t);
1581 if (purpose)
1582 purpose = tsubst (purpose, args, nargs, in_decl);
1583 value = TREE_VALUE (t);
1584 if (value)
1585 value = tsubst (value, args, nargs, in_decl);
1586 chain = TREE_CHAIN (t);
1587 if (chain && chain != void_type_node)
1588 chain = tsubst (chain, args, nargs, in_decl);
1589 if (purpose == TREE_PURPOSE (t)
1590 && value == TREE_VALUE (t)
1591 && chain == TREE_CHAIN (t))
1592 return t;
1593 result = hash_tree_cons (via_public, via_virtual, via_protected,
1594 purpose, value, chain);
1595 TREE_PARMLIST (result) = TREE_PARMLIST (t);
1596 return result;
1597 }
1598 case TREE_VEC:
5566b478
MS
1599 if (type != NULL_TREE)
1600 {
1601 t = copy_node (t);
1602
1603 if (type == TREE_TYPE (t))
1604 return t;
1605
1606 TREE_TYPE (t) = complete_type (type);
1607 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
1608 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
1609 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
1610 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
1611
1612 return t;
1613 }
8d08fdba
MS
1614 {
1615 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
1616 tree *elts = (tree *) alloca (len * sizeof (tree));
5566b478 1617
1daa5dd8 1618 bzero ((char *) elts, len * sizeof (tree));
8d08fdba
MS
1619
1620 for (i = 0; i < len; i++)
1621 {
ec255269 1622 elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
8d08fdba
MS
1623 if (elts[i] != TREE_VEC_ELT (t, i))
1624 need_new = 1;
1625 }
1626
1627 if (!need_new)
1628 return t;
1629
1630 t = make_tree_vec (len);
1631 for (i = 0; i < len; i++)
1632 TREE_VEC_ELT (t, i) = elts[i];
5566b478 1633
8d08fdba
MS
1634 return t;
1635 }
1636 case POINTER_TYPE:
1637 case REFERENCE_TYPE:
1638 {
1639 tree r;
1640 enum tree_code code;
1641 if (type == TREE_TYPE (t))
1642 return t;
1643
1644 code = TREE_CODE (t);
1645 if (code == POINTER_TYPE)
1646 r = build_pointer_type (type);
1647 else
1648 r = build_reference_type (type);
f376e137 1649 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
8d08fdba
MS
1650 /* Will this ever be needed for TYPE_..._TO values? */
1651 layout_type (r);
1652 return r;
1653 }
a4443a08
MS
1654 case OFFSET_TYPE:
1655 return build_offset_type
1656 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
8d08fdba
MS
1657 case FUNCTION_TYPE:
1658 case METHOD_TYPE:
1659 {
75b0bbce 1660 tree values = TYPE_ARG_TYPES (t);
8d08fdba
MS
1661 tree context = TYPE_CONTEXT (t);
1662 tree new_value;
1663
1664 /* Don't bother recursing if we know it won't change anything. */
1665 if (values != void_list_node)
5566b478
MS
1666 {
1667 /* This should probably be rewritten to use hash_tree_cons for
1668 the memory savings. */
1669 tree first = NULL_TREE;
1670 tree last;
1671
1672 for (; values && values != void_list_node;
1673 values = TREE_CHAIN (values))
1674 {
1675 tree value
1676 = tsubst (TREE_VALUE (values), args, nargs, in_decl);
1677 tree purpose = tsubst_expr (TREE_PURPOSE (values),
1678 args, nargs, in_decl);
1679 tree x = build_tree_list (purpose, value);
1680
1681 if (first)
1682 TREE_CHAIN (last) = x;
1683 else
1684 first = x;
1685 last = x;
1686 }
1687
1688 if (values == void_list_node)
1689 TREE_CHAIN (last) = void_list_node;
1690
1691 values = first;
1692 }
8d08fdba
MS
1693 if (context)
1694 context = tsubst (context, args, nargs, in_decl);
1695 /* Could also optimize cases where return value and
1696 values have common elements (e.g., T min(const &T, const T&). */
1697
1698 /* If the above parameters haven't changed, just return the type. */
1699 if (type == TREE_TYPE (t)
1700 && values == TYPE_VALUES (t)
1701 && context == TYPE_CONTEXT (t))
1702 return t;
1703
1704 /* Construct a new type node and return it. */
1705 if (TREE_CODE (t) == FUNCTION_TYPE
1706 && context == NULL_TREE)
1707 {
1708 new_value = build_function_type (type, values);
1709 }
1710 else if (context == NULL_TREE)
1711 {
1712 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
1713 args, nargs, in_decl);
1714 new_value = build_cplus_method_type (base, type,
1715 TREE_CHAIN (values));
1716 }
1717 else
1718 {
1719 new_value = make_node (TREE_CODE (t));
1720 TREE_TYPE (new_value) = type;
1721 TYPE_CONTEXT (new_value) = context;
1722 TYPE_VALUES (new_value) = values;
1723 TYPE_SIZE (new_value) = TYPE_SIZE (t);
1724 TYPE_ALIGN (new_value) = TYPE_ALIGN (t);
1725 TYPE_MODE (new_value) = TYPE_MODE (t);
1726 if (TYPE_METHOD_BASETYPE (t))
1727 TYPE_METHOD_BASETYPE (new_value) = tsubst (TYPE_METHOD_BASETYPE (t),
1728 args, nargs, in_decl);
1729 /* Need to generate hash value. */
1730 my_friendly_abort (84);
1731 }
1732 new_value = build_type_variant (new_value,
1733 TYPE_READONLY (t),
1734 TYPE_VOLATILE (t));
1735 return new_value;
1736 }
1737 case ARRAY_TYPE:
1738 {
1739 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
1740 tree r;
1741 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
1742 return t;
1743 r = build_cplus_array_type (type, domain);
1744 return r;
1745 }
1746
8d08fdba 1747 case PLUS_EXPR:
5566b478 1748 case MINUS_EXPR:
8d08fdba
MS
1749 return fold (build (TREE_CODE (t), TREE_TYPE (t),
1750 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1751 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
1752
1753 case NEGATE_EXPR:
1754 case NOP_EXPR:
1755 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
1756 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
1757
5566b478
MS
1758 case TYPENAME_TYPE:
1759 {
1760 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
1761 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
1762 return cp_build_type_variant
1763 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
1764 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
1765 }
1766
1767 case INDIRECT_REF:
1768 return make_pointer_declarator
1769 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1770
1771 case ADDR_EXPR:
1772 return make_reference_declarator
1773 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
1774
1775 case ARRAY_REF:
1776 return build_parse_node
1777 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1778 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
1779
1780 case CALL_EXPR:
1781 return build_parse_node
1782 (CALL_EXPR, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
1783 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl), 0);
1784
8d08fdba 1785 default:
5566b478 1786 sorry ("use of `%s' in template",
8d08fdba
MS
1787 tree_code_name [(int) TREE_CODE (t)]);
1788 return error_mark_node;
1789 }
1790}
1791
5566b478
MS
1792void
1793do_pushlevel ()
1794{
1795 emit_line_note (input_filename, lineno);
1796 pushlevel (0);
1797 clear_last_expr ();
1798 push_momentary ();
1799 expand_start_bindings (0);
1800}
1801
8d08fdba 1802tree
5566b478 1803do_poplevel ()
8d08fdba 1804{
5566b478 1805 tree t;
8d08fdba 1806
5566b478
MS
1807 expand_end_bindings (getdecls (), kept_level_p (), 1);
1808 t = poplevel (kept_level_p (), 1, 0);
1809 pop_momentary ();
1810 return t;
1811}
8d08fdba 1812
5566b478
MS
1813tree
1814tsubst_copy (t, args, nargs, in_decl)
1815 tree t, *args;
1816 int nargs;
1817 tree in_decl;
1818{
1819 enum tree_code code;
8d08fdba 1820
5566b478
MS
1821 if (t == NULL_TREE || t == error_mark_node)
1822 return t;
1823
1824 code = TREE_CODE (t);
b7484fbe 1825
5566b478
MS
1826 switch (code)
1827 {
1828 case PARM_DECL:
1829 return do_identifier (DECL_NAME (t), 0);
1830
1831 case CONST_DECL:
1832 case FIELD_DECL:
1833 if (DECL_CONTEXT (t))
1834 {
1835 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
1836 if (ctx != DECL_CONTEXT (t))
1837 return lookup_field (ctx, DECL_NAME (t), 0, 0);
1838 }
1839 return t;
1840
1841 case VAR_DECL:
1842 case FUNCTION_DECL:
1843 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
1844 t = tsubst (t, args, nargs, in_decl);
1845 mark_used (t);
1846 return t;
1847
1848#if 0
1849 case IDENTIFIER_NODE:
1850 return do_identifier (t, 0);
1851#endif
1852
1853 case CAST_EXPR:
1854 case REINTERPRET_CAST_EXPR:
1855 return build1
1856 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
1857 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
1858
1859 case INDIRECT_REF:
1860 case PREDECREMENT_EXPR:
1861 case PREINCREMENT_EXPR:
1862 case POSTDECREMENT_EXPR:
1863 case POSTINCREMENT_EXPR:
1864 case NEGATE_EXPR:
1865 case TRUTH_NOT_EXPR:
1866 case ADDR_EXPR:
1867 case CONVERT_EXPR: /* Unary + */
1868 case SIZEOF_EXPR:
1869 case ARROW_EXPR:
1870 return build1
1871 (code, NULL_TREE,
1872 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
1873
1874 case PLUS_EXPR:
1875 case MINUS_EXPR:
1876 case MULT_EXPR:
1877 case TRUNC_DIV_EXPR:
1878 case CEIL_DIV_EXPR:
1879 case FLOOR_DIV_EXPR:
1880 case ROUND_DIV_EXPR:
1881 case EXACT_DIV_EXPR:
1882 case BIT_AND_EXPR:
1883 case BIT_ANDTC_EXPR:
1884 case BIT_IOR_EXPR:
1885 case BIT_XOR_EXPR:
1886 case TRUNC_MOD_EXPR:
1887 case FLOOR_MOD_EXPR:
1888 case TRUTH_ANDIF_EXPR:
1889 case TRUTH_ORIF_EXPR:
1890 case TRUTH_AND_EXPR:
1891 case TRUTH_OR_EXPR:
1892 case RSHIFT_EXPR:
1893 case LSHIFT_EXPR:
1894 case RROTATE_EXPR:
1895 case LROTATE_EXPR:
1896 case EQ_EXPR:
1897 case NE_EXPR:
1898 case MAX_EXPR:
1899 case MIN_EXPR:
1900 case LE_EXPR:
1901 case GE_EXPR:
1902 case LT_EXPR:
1903 case GT_EXPR:
1904 case COMPONENT_REF:
1905 case ARRAY_REF:
1906 case COMPOUND_EXPR:
1907 case SCOPE_REF:
1908 case DOTSTAR_EXPR:
1909 case MEMBER_REF:
1910 return build_nt
1911 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
1912 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
1913
1914 case CALL_EXPR:
1915 {
1916 tree fn = TREE_OPERAND (t, 0);
1917 if (really_overloaded_fn (fn))
1918 fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
1919 else
1920 fn = tsubst_copy (fn, args, nargs, in_decl);
1921 return build_nt
1922 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
1923 NULL_TREE);
1924 }
1925
1926 case METHOD_CALL_EXPR:
1927 {
1928 tree name = TREE_OPERAND (t, 0);
1929 if (TREE_CODE (name) == BIT_NOT_EXPR)
1930 {
1931 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
1932 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
1933 }
1934 else if (TREE_CODE (name) == SCOPE_REF
1935 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
1936 {
1937 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
1938 name = TREE_OPERAND (name, 1);
1939 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
1940 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
1941 name = build_nt (SCOPE_REF, base, name);
1942 }
1943 else
1944 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
1945 return build_nt
1946 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
1947 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
1948 NULL_TREE);
1949 }
1950
1951 case COND_EXPR:
1952 case MODOP_EXPR:
1953 return build_nt
1954 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
1955 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
1956 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
1957
1958 case NEW_EXPR:
1959 {
1960 tree r = build_nt
1961 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
1962 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
1963 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
1964 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
1965 return r;
1966 }
1967
1968 case DELETE_EXPR:
1969 {
1970 tree r = build_nt
1971 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
1972 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
1973 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
1974 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
1975 return r;
1976 }
1977
1978 case TREE_LIST:
1979 {
1980 tree purpose, value, chain;
1981
1982 if (t == void_list_node)
1983 return t;
1984
1985 purpose = TREE_PURPOSE (t);
1986 if (purpose)
1987 purpose = tsubst_copy (purpose, args, nargs, in_decl);
1988 value = TREE_VALUE (t);
1989 if (value)
1990 value = tsubst_copy (value, args, nargs, in_decl);
1991 chain = TREE_CHAIN (t);
1992 if (chain && chain != void_type_node)
1993 chain = tsubst_copy (chain, args, nargs, in_decl);
1994 if (purpose == TREE_PURPOSE (t)
1995 && value == TREE_VALUE (t)
1996 && chain == TREE_CHAIN (t))
1997 return t;
1998 return tree_cons (purpose, value, chain);
1999 }
2000
2001 case RECORD_TYPE:
2002 case UNION_TYPE:
2003 case ENUMERAL_TYPE:
2004 case INTEGER_TYPE:
2005 case TEMPLATE_TYPE_PARM:
2006 case TEMPLATE_CONST_PARM:
2007 case POINTER_TYPE:
2008 case REFERENCE_TYPE:
2009 case OFFSET_TYPE:
2010 case FUNCTION_TYPE:
2011 case METHOD_TYPE:
2012 case ARRAY_TYPE:
2013 case TYPENAME_TYPE:
2014 return tsubst (t, args, nargs, in_decl);
2015
2016 default:
2017 return t;
2018 }
2019}
2020
2021tree
2022tsubst_expr (t, args, nargs, in_decl)
2023 tree t, *args;
2024 int nargs;
2025 tree in_decl;
2026{
2027 if (t == NULL_TREE || t == error_mark_node)
2028 return t;
2029
2030 if (current_template_parms)
2031 return tsubst_copy (t, args, nargs, in_decl);
2032
2033 switch (TREE_CODE (t))
8d08fdba 2034 {
5566b478
MS
2035 case RETURN_STMT:
2036 lineno = TREE_COMPLEXITY (t);
2037 emit_line_note (input_filename, lineno);
2038 c_expand_return
2039 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2040 finish_stmt ();
2041 break;
2042
2043 case EXPR_STMT:
2044 lineno = TREE_COMPLEXITY (t);
2045 emit_line_note (input_filename, lineno);
2046 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2047 /* Do default conversion if safe and possibly important,
2048 in case within ({...}). */
2049 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
2050 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2051 t = default_conversion (t);
2052 cplus_expand_expr_stmt (t);
2053 clear_momentary ();
2054 finish_stmt ();
2055 break;
2056
2057 case DECL_STMT:
2058 {
2059 int i = suspend_momentary ();
2060 tree dcl;
2061
2062 lineno = TREE_COMPLEXITY (t);
2063 emit_line_note (input_filename, lineno);
2064 dcl = start_decl
2065 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2066 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2067 TREE_OPERAND (t, 3) != 0,
2068 tsubst (TREE_OPERAND (t, 2), args, nargs, in_decl));
2069 cp_finish_decl
2070 (dcl, tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl),
2071 NULL_TREE, 1, LOOKUP_ONLYCONVERTING);
2072 resume_momentary (i);
2073 return dcl;
2074 }
8d08fdba 2075
5566b478
MS
2076 case FOR_STMT:
2077 {
2078 tree tmp;
2079 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
2080 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2081 int cond_scope = (TREE_OPERAND (t, 1)
2082 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
2083
2084 lineno = TREE_COMPLEXITY (t);
2085 emit_line_note (input_filename, lineno);
2086 if (init_scope)
2087 do_pushlevel ();
2088 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2089 emit_nop ();
2090 emit_line_note (input_filename, lineno);
2091 expand_start_loop_continue_elsewhere (1);
2092
2093 if (cond_scope)
2094 do_pushlevel ();
2095 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2096 emit_line_note (input_filename, lineno);
2097 if (tmp)
2098 expand_exit_loop_if_false (0, condition_conversion (tmp));
2099
2100 if (! cond_scope)
2101 do_pushlevel ();
2102 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
2103 do_poplevel ();
2104
2105 emit_line_note (input_filename, lineno);
2106 expand_loop_continue_here ();
2107 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
2108 if (tmp)
2109 cplus_expand_expr_stmt (tmp);
2110
2111 expand_end_loop ();
2112 if (init_scope)
2113 do_poplevel ();
2114 finish_stmt ();
2115 }
2116 break;
8d08fdba 2117
5566b478
MS
2118 case WHILE_STMT:
2119 {
2120 tree cond;
2121
2122 lineno = TREE_COMPLEXITY (t);
2123 emit_nop ();
2124 emit_line_note (input_filename, lineno);
2125 expand_start_loop (1);
2126
2127 cond = TREE_OPERAND (t, 0);
2128 if (TREE_CODE (cond) == DECL_STMT)
2129 do_pushlevel ();
2130 cond = tsubst_expr (cond, args, nargs, in_decl);
2131 emit_line_note (input_filename, lineno);
2132 expand_exit_loop_if_false (0, condition_conversion (cond));
2133
2134 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
2135 do_pushlevel ();
2136 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2137 do_poplevel ();
2138
2139 expand_end_loop ();
2140 finish_stmt ();
2141 }
2142 break;
8d08fdba 2143
5566b478
MS
2144 case DO_STMT:
2145 {
2146 tree cond;
8d08fdba 2147
5566b478
MS
2148 lineno = TREE_COMPLEXITY (t);
2149 emit_nop ();
2150 emit_line_note (input_filename, lineno);
2151 expand_start_loop_continue_elsewhere (1);
8d08fdba 2152
5566b478
MS
2153 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2154 expand_loop_continue_here ();
f0e01782 2155
5566b478
MS
2156 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
2157 emit_line_note (input_filename, lineno);
2158 expand_exit_loop_if_false (0, condition_conversion (cond));
2159 expand_end_loop ();
28cbf42c 2160
5566b478
MS
2161 clear_momentary ();
2162 finish_stmt ();
2163 }
2164 break;
a0a33927 2165
5566b478 2166 case IF_STMT:
8d08fdba 2167 {
5566b478
MS
2168 tree tmp;
2169 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2170
2171 lineno = TREE_COMPLEXITY (t);
2172 if (cond_scope)
2173 do_pushlevel ();
2174 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2175 emit_line_note (input_filename, lineno);
2176 expand_start_cond (condition_conversion (tmp), 0);
2177
2178 if (tmp = TREE_OPERAND (t, 1), tmp)
2179 tsubst_expr (tmp, args, nargs, in_decl);
db5ae43f 2180
5566b478 2181 if (tmp = TREE_OPERAND (t, 2), tmp)
db5ae43f 2182 {
5566b478
MS
2183 expand_start_else ();
2184 tsubst_expr (tmp, args, nargs, in_decl);
db5ae43f
MS
2185 }
2186
5566b478 2187 expand_end_cond ();
8d08fdba 2188
5566b478
MS
2189 if (cond_scope)
2190 do_poplevel ();
8d08fdba 2191
5566b478 2192 finish_stmt ();
8d08fdba 2193 }
5566b478 2194 break;
8d08fdba 2195
5566b478
MS
2196 case COMPOUND_STMT:
2197 {
2198 tree substmt = TREE_OPERAND (t, 0);
8d08fdba 2199
5566b478 2200 lineno = TREE_COMPLEXITY (t);
8d08fdba 2201
5566b478
MS
2202 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2203 do_pushlevel ();
8d08fdba 2204
5566b478
MS
2205 for (; substmt; substmt = TREE_CHAIN (substmt))
2206 tsubst_expr (substmt, args, nargs, in_decl);
8d08fdba 2207
5566b478
MS
2208 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
2209 do_poplevel ();
2210 }
2211 break;
8d08fdba 2212
5566b478
MS
2213 case BREAK_STMT:
2214 lineno = TREE_COMPLEXITY (t);
2215 emit_line_note (input_filename, lineno);
2216 if (! expand_exit_something ())
2217 error ("break statement not within loop or switch");
2218 break;
8d08fdba 2219
5566b478
MS
2220 case SWITCH_STMT:
2221 {
2222 tree val, tmp;
2223 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
2224
2225 lineno = TREE_COMPLEXITY (t);
2226 if (cond_scope)
2227 do_pushlevel ();
2228 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
2229 emit_line_note (input_filename, lineno);
2230 c_expand_start_case (val);
2231 push_switch ();
2232
2233 if (tmp = TREE_OPERAND (t, 1), tmp)
2234 tsubst_expr (tmp, args, nargs, in_decl);
8d08fdba 2235
5566b478
MS
2236 expand_end_case (val);
2237 pop_switch ();
8d08fdba 2238
5566b478
MS
2239 if (cond_scope)
2240 do_poplevel ();
8d08fdba 2241
5566b478
MS
2242 finish_stmt ();
2243 }
2244 break;
2245
2246 case CASE_LABEL:
2247 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
2248 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2249 break;
2250
2251 case LABEL_DECL:
2252 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
2253 DECL_NAME (t));
2254 if (t)
2255 expand_label (t);
2256 break;
2257
2258 case GOTO_STMT:
2259 lineno = TREE_COMPLEXITY (t);
2260 emit_line_note (input_filename, lineno);
2261 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
2262 {
2263 tree decl = lookup_label (TREE_OPERAND (t, 0));
2264 TREE_USED (decl) = 1;
2265 expand_goto (decl);
2266 }
2267 else
2268 expand_computed_goto
2269 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
2270 break;
2271
2272 default:
2273 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
2274 }
2275 return NULL_TREE;
8d08fdba
MS
2276}
2277
5566b478
MS
2278tree
2279instantiate_template (tmpl, targ_ptr)
2280 tree tmpl, *targ_ptr;
8d08fdba 2281{
5566b478
MS
2282 tree fndecl;
2283 int i, len;
2284 struct obstack *old_fmp_obstack;
2285 extern struct obstack *function_maybepermanent_obstack;
2286
2287 push_obstacks (&permanent_obstack, &permanent_obstack);
2288 old_fmp_obstack = function_maybepermanent_obstack;
2289 function_maybepermanent_obstack = &permanent_obstack;
8d08fdba 2290
5566b478
MS
2291 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
2292 len = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl));
8d08fdba 2293
5566b478
MS
2294 i = len;
2295 while (i--)
8d08fdba 2296 {
5566b478
MS
2297 tree t = targ_ptr [i];
2298 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2299 {
2300 tree nt = target_type (t);
ec255269 2301 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5566b478
MS
2302 {
2303 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
2304 cp_error (" trying to instantiate `%D'", tmpl);
2305 fndecl = error_mark_node;
2306 goto out;
2307 }
2308 }
2309 targ_ptr[i] = copy_to_permanent (t);
8d08fdba
MS
2310 }
2311
5566b478
MS
2312 /* substitute template parameters */
2313 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
8d08fdba 2314
5566b478
MS
2315 out:
2316 function_maybepermanent_obstack = old_fmp_obstack;
2317 pop_obstacks ();
8d08fdba 2318
5566b478 2319 return fndecl;
8d08fdba 2320}
5566b478
MS
2321
2322/* Push the name of the class template into the scope of the instantiation. */
8d08fdba
MS
2323
2324void
5566b478
MS
2325overload_template_name (type)
2326 tree type;
8d08fdba 2327{
5566b478
MS
2328 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
2329 tree decl;
8d08fdba 2330
5566b478
MS
2331 if (IDENTIFIER_CLASS_VALUE (id)
2332 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
2333 return;
8d08fdba 2334
5566b478
MS
2335 decl = build_decl (TYPE_DECL, id, type);
2336 SET_DECL_ARTIFICIAL (decl);
2337 pushdecl_class_level (decl);
8d08fdba
MS
2338}
2339
2340/* Type unification.
2341
2342 We have a function template signature with one or more references to
2343 template parameters, and a parameter list we wish to fit to this
2344 template. If possible, produce a list of parameters for the template
2345 which will cause it to fit the supplied parameter list.
2346
2347 Return zero for success, 2 for an incomplete match that doesn't resolve
2348 all the types, and 1 for complete failure. An error message will be
2349 printed only for an incomplete match.
2350
2351 TPARMS[NTPARMS] is an array of template parameter types;
2352 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2353 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2354 and ARGS is the argument list we're trying to match against it.
2355
2356 If SUBR is 1, we're being called recursively (to unify the arguments of
2357 a function or method parameter of a function template), so don't zero
2358 out targs and don't fail on an incomplete match. */
2359
2360int
2361type_unification (tparms, targs, parms, args, nsubsts, subr)
2362 tree tparms, *targs, parms, args;
2363 int *nsubsts, subr;
2364{
2365 tree parm, arg;
2366 int i;
2367 int ntparms = TREE_VEC_LENGTH (tparms);
2368
2369 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
2370 my_friendly_assert (TREE_CODE (parms) == TREE_LIST, 290);
51c184be 2371 /* ARGS could be NULL (via a call from parse.y to
8d08fdba
MS
2372 build_x_function_call). */
2373 if (args)
2374 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
2375 my_friendly_assert (ntparms > 0, 292);
2376
2377 if (!subr)
1daa5dd8 2378 bzero ((char *) targs, sizeof (tree) * ntparms);
8d08fdba
MS
2379
2380 while (parms
2381 && parms != void_list_node
2382 && args
2383 && args != void_list_node)
2384 {
2385 parm = TREE_VALUE (parms);
2386 parms = TREE_CHAIN (parms);
2387 arg = TREE_VALUE (args);
2388 args = TREE_CHAIN (args);
2389
2390 if (arg == error_mark_node)
2391 return 1;
2392 if (arg == unknown_type_node)
2393 return 1;
b7484fbe
MS
2394
2395 if (! uses_template_parms (parm)
2396 && TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2397 {
2398 if (can_convert_arg (parm, TREE_TYPE (arg), arg))
2399 continue;
2400 return 1;
2401 }
2402
8d08fdba
MS
2403#if 0
2404 if (TREE_CODE (arg) == VAR_DECL)
2405 arg = TREE_TYPE (arg);
2406 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
2407 arg = TREE_TYPE (arg);
2408#else
2409 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
2410 {
2411 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
28cbf42c
MS
2412 if (TREE_CODE (arg) == TREE_LIST
2413 && TREE_TYPE (arg) == unknown_type_node
2414 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
2415 {
2416 int nsubsts, ntparms;
2417 tree *targs;
2418
2419 /* Have to back unify here */
2420 arg = TREE_VALUE (arg);
2421 nsubsts = 0;
2422 ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg));
2423 targs = (tree *) alloca (sizeof (tree) * ntparms);
2424 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
2425 return type_unification (DECL_TEMPLATE_PARMS (arg), targs,
2426 TYPE_ARG_TYPES (TREE_TYPE (arg)),
2427 parm, &nsubsts, 0);
2428 }
8d08fdba
MS
2429 arg = TREE_TYPE (arg);
2430 }
2431#endif
db5ae43f
MS
2432 if (TREE_CODE (arg) == REFERENCE_TYPE)
2433 arg = TREE_TYPE (arg);
2434
4cabb798
JM
2435 if (TREE_CODE (parm) != REFERENCE_TYPE)
2436 {
2437 if (TREE_CODE (arg) == FUNCTION_TYPE
2438 || TREE_CODE (arg) == METHOD_TYPE)
2439 arg = build_pointer_type (arg);
2440 else if (TREE_CODE (arg) == ARRAY_TYPE)
2441 arg = build_pointer_type (TREE_TYPE (arg));
2442 else
2443 arg = TYPE_MAIN_VARIANT (arg);
2444 }
8d08fdba
MS
2445
2446 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts))
2447 {
2448 case 0:
2449 break;
2450 case 1:
2451 return 1;
2452 }
2453 }
2454 /* Fail if we've reached the end of the parm list, and more args
2455 are present, and the parm list isn't variadic. */
2456 if (args && args != void_list_node && parms == void_list_node)
2457 return 1;
2458 /* Fail if parms are left and they don't have default values. */
2459 if (parms
2460 && parms != void_list_node
2461 && TREE_PURPOSE (parms) == NULL_TREE)
2462 return 1;
2463 if (!subr)
2464 for (i = 0; i < ntparms; i++)
2465 if (!targs[i])
2466 {
2467 error ("incomplete type unification");
2468 return 2;
2469 }
2470 return 0;
2471}
2472
2473/* Tail recursion is your friend. */
2474static int
2475unify (tparms, targs, ntparms, parm, arg, nsubsts)
2476 tree tparms, *targs, parm, arg;
2477 int *nsubsts, ntparms;
2478{
2479 int idx;
2480
2481 /* I don't think this will do the right thing with respect to types.
2482 But the only case I've seen it in so far has been array bounds, where
2483 signedness is the only information lost, and I think that will be
2484 okay. */
2485 while (TREE_CODE (parm) == NOP_EXPR)
2486 parm = TREE_OPERAND (parm, 0);
2487
2488 if (arg == error_mark_node)
2489 return 1;
2490 if (arg == unknown_type_node)
2491 return 1;
2492 if (arg == parm)
2493 return 0;
2494
8d08fdba
MS
2495 switch (TREE_CODE (parm))
2496 {
2497 case TEMPLATE_TYPE_PARM:
2498 (*nsubsts)++;
8d08fdba 2499 idx = TEMPLATE_TYPE_IDX (parm);
db5ae43f 2500#if 0
a292b002
MS
2501 /* Template type parameters cannot contain cv-quals; i.e.
2502 template <class T> void f (T& a, T& b) will not generate
2503 void f (const int& a, const int& b). */
2504 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
2505 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
2506 return 1;
2507 arg = TYPE_MAIN_VARIANT (arg);
db5ae43f
MS
2508#else
2509 {
2510 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
2511 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
2512 arg = cp_build_type_variant (arg, constp, volatilep);
2513 }
2514#endif
8d08fdba
MS
2515 /* Simple cases: Value already set, does match or doesn't. */
2516 if (targs[idx] == arg)
2517 return 0;
2518 else if (targs[idx])
8d08fdba 2519 return 1;
a292b002
MS
2520 /* Check for mixed types and values. */
2521 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
a4443a08 2522 return 1;
8d08fdba
MS
2523 targs[idx] = arg;
2524 return 0;
2525 case TEMPLATE_CONST_PARM:
2526 (*nsubsts)++;
2527 idx = TEMPLATE_CONST_IDX (parm);
2528 if (targs[idx] == arg)
2529 return 0;
2530 else if (targs[idx])
2531 {
a28e3c7f
MS
2532 tree t = targs[idx];
2533 if (TREE_CODE (t) == TREE_CODE (arg))
2534 switch (TREE_CODE (arg))
2535 {
2536 case INTEGER_CST:
2537 if (tree_int_cst_equal (t, arg))
2538 return 0;
2539 break;
2540 case REAL_CST:
2541 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t), TREE_REAL_CST (arg)))
2542 return 0;
2543 break;
2544 /* STRING_CST values are not valid template const parms. */
2545 default:
2546 ;
2547 }
8d08fdba
MS
2548 my_friendly_abort (87);
2549 return 1;
2550 }
2551/* else if (typeof arg != tparms[idx])
2552 return 1;*/
2553
2554 targs[idx] = copy_to_permanent (arg);
2555 return 0;
2556
2557 case POINTER_TYPE:
2558 if (TREE_CODE (arg) != POINTER_TYPE)
2559 return 1;
2560 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2561 nsubsts);
2562
2563 case REFERENCE_TYPE:
28cbf42c
MS
2564 if (TREE_CODE (arg) == REFERENCE_TYPE)
2565 arg = TREE_TYPE (arg);
8d08fdba
MS
2566 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg, nsubsts);
2567
2568 case ARRAY_TYPE:
2569 if (TREE_CODE (arg) != ARRAY_TYPE)
2570 return 1;
2571 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
2572 nsubsts) != 0)
2573 return 1;
2574 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
2575 nsubsts);
2576
2577 case REAL_TYPE:
2578 case INTEGER_TYPE:
f376e137
MS
2579 if (TREE_CODE (arg) != TREE_CODE (parm))
2580 return 1;
2581
2582 if (TREE_CODE (parm) == INTEGER_TYPE)
8d08fdba
MS
2583 {
2584 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
2585 && unify (tparms, targs, ntparms,
2586 TYPE_MIN_VALUE (parm), TYPE_MIN_VALUE (arg), nsubsts))
2587 return 1;
2588 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
2589 && unify (tparms, targs, ntparms,
2590 TYPE_MAX_VALUE (parm), TYPE_MAX_VALUE (arg), nsubsts))
2591 return 1;
2592 }
2593 /* As far as unification is concerned, this wins. Later checks
2594 will invalidate it if necessary. */
2595 return 0;
2596
2597 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2598 case INTEGER_CST:
2599 if (TREE_CODE (arg) != INTEGER_CST)
2600 return 1;
2601 return !tree_int_cst_equal (parm, arg);
2602
2603 case MINUS_EXPR:
2604 {
2605 tree t1, t2;
2606 t1 = TREE_OPERAND (parm, 0);
2607 t2 = TREE_OPERAND (parm, 1);
8d08fdba
MS
2608 return unify (tparms, targs, ntparms, t1,
2609 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
2610 nsubsts);
2611 }
2612
2613 case TREE_VEC:
2614 {
2615 int i;
2616 if (TREE_CODE (arg) != TREE_VEC)
2617 return 1;
2618 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
2619 return 1;
2620 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
2621 if (unify (tparms, targs, ntparms,
2622 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
2623 nsubsts))
2624 return 1;
2625 return 0;
2626 }
2627
8d08fdba 2628 case RECORD_TYPE:
db5ae43f 2629 if (TYPE_PTRMEMFUNC_FLAG (parm))
8d08fdba
MS
2630 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
2631 arg, nsubsts);
2632
a4443a08 2633 /* Allow trivial conversions. */
5566b478 2634 if (TREE_CODE (arg) != RECORD_TYPE
a4443a08
MS
2635 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
2636 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
2637 return 1;
5566b478
MS
2638
2639 if (CLASSTYPE_TEMPLATE_INFO (parm) && CLASSTYPE_TEMPLATE_INFO (arg)
2640 && uses_template_parms (parm))
2641 {
2642 if (CLASSTYPE_TI_TEMPLATE (parm) != CLASSTYPE_TI_TEMPLATE (arg))
2643 return 1;
2644 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
2645 CLASSTYPE_TI_ARGS (arg), nsubsts);
2646 }
2647 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
2648 return 1;
a4443a08 2649 return 0;
8d08fdba
MS
2650
2651 case METHOD_TYPE:
2652 if (TREE_CODE (arg) != METHOD_TYPE)
2653 return 1;
2654 goto check_args;
2655
2656 case FUNCTION_TYPE:
2657 if (TREE_CODE (arg) != FUNCTION_TYPE)
2658 return 1;
2659 check_args:
28cbf42c
MS
2660 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
2661 TREE_TYPE (arg), nsubsts))
2662 return 1;
8d08fdba
MS
2663 return type_unification (tparms, targs, TYPE_ARG_TYPES (parm),
2664 TYPE_ARG_TYPES (arg), nsubsts, 1);
a4443a08
MS
2665
2666 case OFFSET_TYPE:
2667 if (TREE_CODE (arg) != OFFSET_TYPE)
2668 return 1;
2669 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
2670 TYPE_OFFSET_BASETYPE (arg), nsubsts))
2671 return 1;
2672 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
2673 TREE_TYPE (arg), nsubsts);
2674
8d08fdba
MS
2675 default:
2676 sorry ("use of `%s' in template type unification",
2677 tree_code_name [(int) TREE_CODE (parm)]);
2678 return 1;
2679 }
2680}
8d08fdba 2681\f
faae18ab 2682void
5566b478 2683mark_decl_instantiated (result, extern_p)
faae18ab
MS
2684 tree result;
2685 int extern_p;
2686{
2687 if (DECL_TEMPLATE_INSTANTIATION (result))
2688 SET_DECL_EXPLICIT_INSTANTIATION (result);
2689 TREE_PUBLIC (result) = 1;
2690
2691 if (! extern_p)
2692 {
2693 DECL_INTERFACE_KNOWN (result) = 1;
2694 DECL_NOT_REALLY_EXTERN (result) = 1;
2695 }
2696}
2697
8d08fdba
MS
2698/* called from the parser. */
2699void
f0e01782
MS
2700do_function_instantiation (declspecs, declarator, storage)
2701 tree declspecs, declarator, storage;
8d08fdba 2702{
f30432d7
MS
2703 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0,
2704 NULL_TREE, NULL_TREE);
e8abc66f
MS
2705 tree name;
2706 tree fn;
8d08fdba 2707 tree result = NULL_TREE;
faae18ab 2708 int extern_p = 0;
e8abc66f 2709
ec255269
MS
2710 if (! DECL_LANG_SPECIFIC (decl))
2711 {
2712 cp_error ("explicit instantiation of non-template `%#D'", decl);
2713 return;
2714 }
2715
e8abc66f
MS
2716 /* If we've already seen this template instance, use it. */
2717 if (name = DECL_ASSEMBLER_NAME (decl),
2718 fn = IDENTIFIER_GLOBAL_VALUE (name),
2719 fn && DECL_TEMPLATE_INSTANTIATION (fn))
2720 result = fn;
72b7eeff
MS
2721 else if (fn && DECL_CONTEXT (fn))
2722 ;
e8abc66f 2723 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
8d08fdba
MS
2724 {
2725 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
faae18ab
MS
2726 if (decls_match (fn, decl)
2727 && DECL_DEFER_OUTPUT (fn))
2728 {
2729 result = fn;
2730 break;
2731 }
2732 else if (TREE_CODE (fn) == TEMPLATE_DECL)
8d08fdba
MS
2733 {
2734 int ntparms = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn));
2735 tree *targs = (tree *) malloc (sizeof (tree) * ntparms);
e1cd6e56 2736 int i, dummy = 0;
8d08fdba
MS
2737 i = type_unification (DECL_TEMPLATE_PARMS (fn), targs,
2738 TYPE_ARG_TYPES (TREE_TYPE (fn)),
2739 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2740 &dummy, 0);
2741 if (i == 0)
2742 {
2743 if (result)
2744 cp_error ("ambiguous template instantiation for `%D' requested", decl);
2745 else
2746 result = instantiate_template (fn, targs);
2747 }
e1cd6e56 2748 free (targs);
8d08fdba
MS
2749 }
2750 }
7177d104 2751 if (! result)
faae18ab
MS
2752 {
2753 cp_error ("no matching template for `%D' found", decl);
2754 return;
2755 }
7177d104 2756
a0a33927
MS
2757 if (flag_external_templates)
2758 return;
2759
f0e01782 2760 if (storage == NULL_TREE)
00595019 2761 ;
faae18ab
MS
2762 else if (storage == ridpointers[(int) RID_EXTERN])
2763 extern_p = 1;
f0e01782
MS
2764 else
2765 cp_error ("storage class `%D' applied to template instantiation",
2766 storage);
5566b478
MS
2767
2768 if (! extern_p)
2769 instantiate_decl (result);
2770 mark_decl_instantiated (result, extern_p);
44a8d0b3 2771 repo_template_instantiated (result, extern_p);
7177d104
MS
2772}
2773
faae18ab
MS
2774void
2775mark_class_instantiated (t, extern_p)
2776 tree t;
2777 int extern_p;
2778{
2779 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
2780 SET_CLASSTYPE_INTERFACE_KNOWN (t);
2781 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
2782 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
2783 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
2784 if (! extern_p)
2785 {
2786 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
2787 rest_of_type_compilation (t, 1);
2788 }
2789}
e8abc66f 2790
7177d104 2791void
f0e01782
MS
2792do_type_instantiation (name, storage)
2793 tree name, storage;
7177d104
MS
2794{
2795 tree t = TREE_TYPE (name);
e8abc66f
MS
2796 int extern_p = 0;
2797 int nomem_p = 0;
5566b478
MS
2798 int static_p = 0;
2799
2800 complete_type (t);
7177d104 2801
a292b002
MS
2802 /* With -fexternal-templates, explicit instantiations are treated the same
2803 as implicit ones. */
a0a33927
MS
2804 if (flag_external_templates)
2805 return;
2806
f0e01782
MS
2807 if (TYPE_SIZE (t) == NULL_TREE)
2808 {
2809 cp_error ("explicit instantiation of `%#T' before definition of template",
2810 t);
2811 return;
2812 }
2813
2814 if (storage == NULL_TREE)
e8abc66f
MS
2815 /* OK */;
2816 else if (storage == ridpointers[(int) RID_INLINE])
2817 nomem_p = 1;
f0e01782
MS
2818 else if (storage == ridpointers[(int) RID_EXTERN])
2819 extern_p = 1;
5566b478
MS
2820 else if (storage == ridpointers[(int) RID_STATIC])
2821 static_p = 1;
f0e01782
MS
2822 else
2823 {
2824 cp_error ("storage class `%D' applied to template instantiation",
2825 storage);
2826 extern_p = 0;
2827 }
2828
a292b002 2829 /* We've already instantiated this. */
44a8d0b3
MS
2830 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
2831 && extern_p)
2832 return;
a292b002 2833
f376e137 2834 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
44a8d0b3
MS
2835 {
2836 mark_class_instantiated (t, extern_p);
2837 repo_template_instantiated (t, extern_p);
2838 }
e8abc66f
MS
2839
2840 if (nomem_p)
2841 return;
2842
7177d104 2843 {
db5ae43f 2844 tree tmp;
5566b478
MS
2845
2846 if (! static_p)
2847 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
2848 if (DECL_TEMPLATE_INSTANTIATION (tmp))
2849 {
2850 mark_decl_instantiated (tmp, extern_p);
2851 repo_template_instantiated (tmp, extern_p);
2852 if (! extern_p)
2853 instantiate_decl (tmp);
2854 }
2855
2856 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
2857 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
863adfc0 2858 {
5566b478 2859 mark_decl_instantiated (tmp, extern_p);
863adfc0 2860 repo_template_instantiated (tmp, extern_p);
5566b478
MS
2861 if (! extern_p)
2862 instantiate_decl (tmp);
863adfc0 2863 }
7177d104 2864
a292b002 2865 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
f376e137
MS
2866 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
2867 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
a292b002 2868 }
8d08fdba 2869}
a28e3c7f
MS
2870
2871tree
5566b478
MS
2872instantiate_decl (d)
2873 tree d;
a28e3c7f 2874{
5566b478
MS
2875 tree ti = DECL_TEMPLATE_INFO (d);
2876 tree tmpl = TI_TEMPLATE (ti);
2877 tree args = TI_ARGS (ti);
2878 tree td;
2879 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
2880 tree save_ti;
2881 int nested = in_function_p ();
2882 int d_defined;
2883 int pattern_defined;
2884
2885 if (TREE_CODE (d) == FUNCTION_DECL)
2886 {
2887 d_defined = (DECL_INITIAL (d) != NULL_TREE);
2888 pattern_defined = (DECL_INITIAL (pattern) != NULL_TREE);
2889 }
2890 else
2891 {
2892 d_defined = ! DECL_IN_AGGR_P (d);
2893 pattern_defined = ! DECL_IN_AGGR_P (pattern);
2894 }
2895
2896 if (d_defined)
2897 return d;
2898 else if (pattern_defined)
2899 {
2900 repo_template_used (d);
2901
2902 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
2903 {
2904 if (flag_alt_external_templates)
2905 {
2906 if (interface_unknown)
2907 warn_if_unknown_interface (d);
2908 }
2909 else if (DECL_INTERFACE_KNOWN (pattern))
2910 {
2911 DECL_INTERFACE_KNOWN (d) = 1;
2912 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (pattern);
2913 }
2914 else
2915 warn_if_unknown_interface (pattern);
2916 }
2917
2918 if (at_eof)
2919 import_export_decl (d);
2920 }
2921
2922 if (! pattern_defined
2923 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
2924 && (! DECL_INTERFACE_KNOWN (d)
2925 || ! DECL_NOT_REALLY_EXTERN (d))))
2926 {
2927 add_pending_template (d);
2928 return d;
2929 }
2930
2931 if (! push_tinst_level (d))
2932 return d;
2933
2934 if (TREE_CODE (d) == FUNCTION_DECL && nested)
2935 push_cp_function_context (NULL_TREE);
2936 push_to_top_level ();
2937
2938 /* Trick tsubst into giving us a new decl in case the template changed. */
2939 save_ti = DECL_TEMPLATE_INFO (pattern);
2940 DECL_TEMPLATE_INFO (pattern) = NULL_TREE;
2941 td = tsubst (pattern, &TREE_VEC_ELT (args, 0), TREE_VEC_LENGTH (args), tmpl);
2942 DECL_TEMPLATE_INFO (pattern) = save_ti;
2943
2944 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
2945 if (TREE_CODE (d) == FUNCTION_DECL)
2946 DECL_INITIAL (td) = error_mark_node;
2947 duplicate_decls (td, d);
2948 if (TREE_CODE (d) == FUNCTION_DECL)
2949 DECL_INITIAL (td) = 0;
2950
2951 if (TREE_CODE (d) == VAR_DECL)
2952 {
2953 DECL_IN_AGGR_P (d) = 0;
2954 if (DECL_INTERFACE_KNOWN (d))
2955 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
2956 else
2957 {
2958 DECL_EXTERNAL (d) = 1;
2959 DECL_NOT_REALLY_EXTERN (d) = 1;
2960 }
2961 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
2962 }
2963 else if (TREE_CODE (d) == FUNCTION_DECL)
2964 {
2965 tree t = DECL_SAVED_TREE (pattern);
2966 int line = lineno;
2967 char *file = input_filename;
2968
2969 lineno = DECL_SOURCE_LINE (d);
2970 input_filename = DECL_SOURCE_FILE (d);
2971
2972 start_function (NULL_TREE, d, NULL_TREE, NULL_TREE, 1);
2973 store_parm_decls ();
2974
2975 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
2976 {
2977 current_member_init_list
2978 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
2979 current_base_init_list
2980 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
2981 t = TREE_CHAIN (t);
2982 }
2983
2984 setup_vtbl_ptr ();
2985 /* Always keep the BLOCK node associated with the outermost
2986 pair of curley braces of a function. These are needed
2987 for correct operation of dwarfout.c. */
2988 keep_next_level ();
2989
2990 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
2991 tsubst_expr (t, &TREE_VEC_ELT (args, 0),
2992 TREE_VEC_LENGTH (args), tmpl);
a28e3c7f 2993
5566b478
MS
2994 finish_function (lineno, 0, nested);
2995
2996 lineno = line;
2997 input_filename = file;
2998 }
2999
3000 pop_from_top_level ();
3001 if (TREE_CODE (d) == FUNCTION_DECL && nested)
3002 pop_cp_function_context (NULL_TREE);
3003 pop_tinst_level ();
a28e3c7f 3004
a28e3c7f
MS
3005 return d;
3006}
5566b478
MS
3007
3008tree
3009tsubst_chain (t, argvec)
3010 tree t, argvec;
3011{
3012 if (t)
3013 {
3014 tree first = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3015 TREE_VEC_LENGTH (argvec), NULL_TREE);
3016 tree last = first;
3017
3018 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
3019 {
3020 tree x = tsubst (t, &TREE_VEC_ELT (argvec, 0),
3021 TREE_VEC_LENGTH (argvec), NULL_TREE);
3022 TREE_CHAIN (last) = x;
3023 last = x;
3024 }
3025
3026 return first;
3027 }
3028 return NULL_TREE;
3029}
3030
3031tree
3032tsubst_expr_values (t, argvec)
3033 tree t, argvec;
3034{
3035 tree first = NULL_TREE;
3036 tree *p = &first;
3037
3038 for (; t; t = TREE_CHAIN (t))
3039 {
3040 tree pur = tsubst_copy (TREE_PURPOSE (t), &TREE_VEC_ELT (argvec, 0),
3041 TREE_VEC_LENGTH (argvec), NULL_TREE);
3042 tree val = tsubst_expr (TREE_VALUE (t), &TREE_VEC_ELT (argvec, 0),
3043 TREE_VEC_LENGTH (argvec), NULL_TREE);
3044 *p = build_tree_list (pur, val);
3045 p = &TREE_CHAIN (*p);
3046 }
3047 return first;
3048}
3049
3050tree last_tree;
3051
3052void
3053add_tree (t)
3054 tree t;
3055{
3056 last_tree = TREE_CHAIN (last_tree) = t;
3057}
This page took 0.468796 seconds and 5 git commands to generate.