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