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