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