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