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