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