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