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