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