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