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