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