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