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