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