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