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