]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
Cast alloca return value for char *
[gcc.git] / gcc / cp / pt.c
CommitLineData
8d08fdba 1/* Handle parameterized types (templates) for GNU C++.
357a4089 2 Copyright (C) 1992, 93, 94, 95, 1996 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
MS
24
25 templates for class static data don't work (methods only),
26 duplicated method templates can crash the compiler,
27 interface/impl data is taken from file defining the template,
28 all methods must be provided in header files; can't use a source
29 file that contains only the method templates and "just win". */
8d08fdba
MS
30
31#include "config.h"
32#include <stdio.h>
33#include "obstack.h"
34
35#include "tree.h"
36#include "flags.h"
37#include "cp-tree.h"
38#include "decl.h"
39#include "parse.h"
f0e01782 40#include "lex.h"
e8abc66f 41#include "output.h"
a9aedbc2 42#include "defaults.h"
49c249e1
JM
43#include "except.h"
44
45#ifdef HAVE_STDLIB_H
46#include <stdlib.h>
47#endif
8d08fdba
MS
48
49extern struct obstack permanent_obstack;
8d08fdba
MS
50
51extern int lineno;
52extern char *input_filename;
53struct pending_inline *pending_template_expansions;
54
5566b478
MS
55tree current_template_parms;
56HOST_WIDE_INT processing_template_decl;
8d08fdba 57
5566b478
MS
58tree pending_templates;
59static tree *template_tail = &pending_templates;
60
73aad9b9
JM
61tree maybe_templates;
62static tree *maybe_template_tail = &maybe_templates;
63
5566b478 64int minimal_parse_mode;
75b0bbce 65
386b8a85
JM
66static int processing_specialization;
67static int template_header_count;
68
8d08fdba
MS
69#define obstack_chunk_alloc xmalloc
70#define obstack_chunk_free free
71
49c249e1
JM
72static int unify PROTO((tree, tree *, int, tree, tree, int *, int));
73static void add_pending_template PROTO((tree));
74static int push_tinst_level PROTO((tree));
75static tree classtype_mangled_name PROTO((tree));
76static char *mangle_class_name_for_template PROTO((char *, tree, tree));
77static tree tsubst_expr_values PROTO((tree, tree));
bd6dd845 78static int comp_template_args PROTO((tree, tree));
49c249e1 79static int list_eq PROTO((tree, tree));
bd6dd845 80static tree get_class_bindings PROTO((tree, tree, tree));
49c249e1 81static tree coerce_template_parms PROTO((tree, tree, tree));
b3d5a58b 82static tree tsubst_enum PROTO((tree, tree, int, tree *));
98c1c668 83static tree add_to_template_args PROTO((tree, tree));
386b8a85
JM
84static int type_unification_real PROTO((tree, tree *, tree, tree, int*,
85 int, int, int));
86static int processing_explicit_specialization PROTO((int));
87static void note_template_header PROTO((int));
98c1c668
JM
88
89/* Restore the template parameter context. */
90
91void
786b5245
MM
92begin_member_template_processing (decl)
93 tree decl;
98c1c668 94{
786b5245 95 tree parms;
98c1c668
JM
96 int i;
97
786b5245
MM
98 parms = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
99
98c1c668
JM
100 ++processing_template_decl;
101 current_template_parms
102 = tree_cons (build_int_2 (0, processing_template_decl),
103 parms, current_template_parms);
786b5245 104 pushlevel (0);
98c1c668
JM
105 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
106 {
786b5245
MM
107 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
108 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
109
98c1c668
JM
110 switch (TREE_CODE (parm))
111 {
786b5245 112 case TYPE_DECL:
98c1c668
JM
113 pushdecl (parm);
114 break;
786b5245
MM
115
116 case PARM_DECL:
117 {
118 /* Make a CONST_DECL as is done in process_template_parm. */
119 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
120 TREE_TYPE (parm));
121 DECL_INITIAL (decl) = DECL_INITIAL (parm);
122 pushdecl (decl);
123 }
124 break;
125
98c1c668
JM
126 default:
127 my_friendly_abort (0);
128 }
129 }
130}
131
132/* Undo the effects of begin_member_template_processing. */
133
134void
135end_member_template_processing ()
136{
137 if (! processing_template_decl)
138 return;
139
140 --processing_template_decl;
141 current_template_parms = TREE_CHAIN (current_template_parms);
786b5245 142 poplevel (0, 0, 0);
98c1c668
JM
143}
144
145/* Returns non-zero iff T is a member template function. Works if T
146 is either a FUNCTION_DECL or a TEMPLATE_DECL. */
147
148int
149is_member_template (t)
150 tree t;
151{
152 int r = 0;
153
aa5f3bad
MM
154 if (TREE_CODE (t) != FUNCTION_DECL
155 && !DECL_FUNCTION_TEMPLATE_P (t))
156 /* Anything that isn't a template or a template functon is
157 certainly not a member template. */
158 return 0;
159
386b8a85
JM
160 if ((DECL_FUNCTION_MEMBER_P (t)
161 && !DECL_TEMPLATE_SPECIALIZATION (t))
162 || (TREE_CODE (t) == TEMPLATE_DECL &&
163 DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
98c1c668
JM
164 {
165 tree tmpl = NULL_TREE;
166
167 if (DECL_FUNCTION_TEMPLATE_P (t))
168 tmpl = t;
169 else if (DECL_TEMPLATE_INFO (t)
170 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
171 tmpl = DECL_TI_TEMPLATE (t);
172
173 if (tmpl)
174 {
175 tree parms = DECL_TEMPLATE_PARMS (tmpl);
176 int parm_levels = list_length (parms);
177 int template_class_levels = 0;
178 tree ctx = DECL_CLASS_CONTEXT (t);
179
180 if (CLASSTYPE_TEMPLATE_INFO (ctx))
181 {
182 tree args;
183
184 /* Here, we should really count the number of levels
185 deep ctx is, making sure not to count any levels that
186 are just specializations. Since there are no member
187 template classes yet, we don't have to do all that. */
188
189 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
190 template_class_levels = 1;
191 else
192 {
193 int i;
194
195 args = CLASSTYPE_TI_ARGS (ctx);
196
197 if (args == NULL_TREE)
198 template_class_levels = 1;
199 else
200 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
201 if (uses_template_parms (TREE_VEC_ELT (args, i)))
202 {
203 template_class_levels++;
204 break;
205 }
206 }
207 }
208
209 if (parm_levels > template_class_levels)
210 r = 1;
211 }
212 }
213
214 return r;
215}
216
217/* Return a new template argument vector which contains all of ARGS,
218 but has as its innermost set of arguments the EXTRA_ARGS. */
219
220tree
221add_to_template_args (args, extra_args)
222 tree args;
223 tree extra_args;
224{
225 tree new_args;
226
227 if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC)
228 {
229 new_args = make_tree_vec (2);
230 TREE_VEC_ELT (new_args, 0) = args;
231 }
232 else
233 {
234 int i;
235
236 new_args = make_tree_vec (TREE_VEC_LENGTH (args) - 1);
237
238 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
239 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
240 }
241
242 TREE_VEC_ELT (new_args,
243 TREE_VEC_LENGTH (new_args) - 1) = extra_args;
244
245 return new_args;
246}
5566b478
MS
247
248/* We've got a template header coming up; push to a new level for storing
249 the parms. */
8d08fdba 250
8d08fdba
MS
251void
252begin_template_parm_list ()
253{
254 pushlevel (0);
5566b478 255 declare_pseudo_global_level ();
5156628f 256 ++processing_template_decl;
386b8a85
JM
257 note_template_header (0);
258}
259
260
261/* We've just seen template <>. */
262
263void
264begin_specialization ()
265{
266 note_template_header (1);
267}
268
269
270/* Called at then end of processing a declaration preceeded by
271 template<>. */
272
273void
274end_specialization ()
275{
276 reset_specialization ();
277}
278
279
280/* Any template <>'s that we have seen thus far are not referring to a
281 function specialization. */
282
283void
284reset_specialization ()
285{
286 processing_specialization = 0;
287 template_header_count = 0;
288}
289
290
291/* We've just seen a template header. If SPECIALIZATION is non-zero,
292 it was of the form template <>. */
293
294void
295note_template_header (specialization)
296 int specialization;
297{
298 processing_specialization = specialization;
299 template_header_count++;
300}
301
302
303/* Returns non-zero iff a declarator, in which the number of template
304 types that appeared was TEMPLATE_COUNT, is an explicit
305 specialization. */
306
307int
308processing_explicit_specialization(template_count)
309 int template_count;
310{
311 /* A function declaration is an explicit specialization of a member
312 template if all of the following conditions hold:
313
314 o There was a template <...> preceeding the declaration.
315 o The last template <...> was in fact template <>.
316 o The number of template <...>'s preceeding the declaration, less
317 the number of template classes with arguments specified used to
318 qualify the function name, is 1.
319
320 For example:
321
322 template <> void S<int>::foo();
323 template <class T> template <> void S<T>::foo();
324 template <> struct S<int> { ... template <> void foo(); }
325
326 The first of these is not a specialization of S<int>::foo() (it
327 is instead a specialization of S<T>::foo), while the next two are
328 specializations of member template functions. */
329
330 return processing_specialization
331 && template_header_count > template_count;
332}
333
334/* Returns the template function specialized by TEMPLATE_ID, or
335 NULL_TREE if there is none.
336
337 The TEMPLATE_ID is a TEMPLATE_ID_EXPR. The TYPE is
338 the type it has been declared to have. Return the TEMPLATE_DECL
339 that is being specialized, and put the specialization arguments in
340 *TARGS. If no appropriate specialization can be found, NULL_TREE is
341 returned, and *TARGS is assigned NULL_TREE. If complain is
342 non-zero, error messages are printed where appropriate. */
343
344tree
345determine_explicit_specialization (template_id, type, targs_out,
346 need_member_template,
347 complain)
348 tree template_id;
349 tree type;
350 tree* targs_out;
351 int need_member_template;
352 int complain;
353{
354 int i;
355 int overloaded;
356 tree fns;
357 tree matching_fns = NULL_TREE;
358 tree name = NULL_TREE;
359 tree result;
360 tree fn;
361
362 my_friendly_assert (TREE_CODE (template_id) == TEMPLATE_ID_EXPR,
363 0);
364
365 fns = TREE_OPERAND (template_id, 0);
366
367 overloaded = fns != NULL_TREE && really_overloaded_fn (fns);
368
369 for (fn = (fns != NULL_TREE) ? get_first_fn (fns) : NULL_TREE;
370 fn != NULL_TREE;
371 fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE)
372 {
373 int dummy;
374 tree targs;
375
376 if (name == NULL_TREE)
377 name = DECL_NAME (fn);
378
379 if (TREE_CODE (fn) != TEMPLATE_DECL
380 || (need_member_template && !is_member_template (fn)))
381 continue;
382
383 if (list_length (TREE_OPERAND (template_id, 1)) > DECL_NTPARMS (fn))
384 continue;
385
386 targs = make_tree_vec (DECL_NTPARMS (fn));
387
388 /* We allow incomplete unification here, because we are going to
389 check all the functions. */
390 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
391 &TREE_VEC_ELT (targs, 0),
392 type
393 ? TYPE_ARG_TYPES (TREE_TYPE (fn)) : NULL_TREE,
394 type ? TYPE_ARG_TYPES (type) : NULL_TREE,
395 TREE_OPERAND (template_id, 1),
396 &dummy, 1, 1);
397
398 if (i == 0)
399 {
400 /* Unification was successful. See if the return types
401 match. */
402 if (type != NULL_TREE)
403 {
404 tree tmpl_return_type = tsubst (TREE_TYPE (TREE_TYPE (fn)),
405 targs,
406 DECL_NTPARMS (fn),
407 NULL_TREE);
408
409 if (tmpl_return_type != TREE_TYPE (type))
410 {
411 /* Always complain about this. With ambiguity, some
412 other context, might resolve things. But, a
413 non-matching return type will always be a
414 problem. */
415 cp_error ("Return type of explicit specialization of");
416 cp_error ("`%D' is `%T', but should be `%T'.",
417 fn, TREE_TYPE (type), tmpl_return_type);
418 *targs_out = NULL_TREE;
419 return NULL_TREE;
420 }
421 }
422
423 matching_fns = tree_cons (fn, targs, matching_fns);
424 }
425 }
426
427 if (matching_fns == NULL_TREE)
428 {
429 if (complain)
430 cp_error ("Specialization of `%s' does not match any template "
431 "declaration.", IDENTIFIER_POINTER (name));
432 *targs_out = NULL_TREE;
433 return NULL_TREE;
434 }
435
436 if (TREE_CHAIN (matching_fns) != NULL_TREE)
437 {
438 if (complain)
439 {
440 tree fn;
441
442 cp_error ("Ambiguous explicit specialization. Candidates are:");
443 for (fn = matching_fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
444 cp_error (" %D", TREE_PURPOSE (fn));
445 }
446
447 *targs_out = NULL_TREE;
448 return NULL_TREE;
449 }
450
451 /* We have one, and exactly one, match. */
452 *targs_out = TREE_VALUE (matching_fns);
453 return TREE_PURPOSE (matching_fns);
8d08fdba
MS
454}
455
386b8a85
JM
456
457/* Check to see if the function just declared, as indicated in
458 DECLARATOR, and in DECL, is a specialization. Check that the
459 specialization is OK. If FLAGS == 1, we are being called by
460 finish_struct_methods. If FLAGS == 2, we are being called by
461 grokfndecl, and the function has a definition, or is a friend. If
462 FLAGS == 3, this is a friend declaration.
463 Returns 0 if the decl is not an explicit specialization or
464 instantiation, 1 if it is an explicit specialization, and 2 if it
465 is an explicit instantiation. */
466
467int
468check_explicit_specialization(declarator, decl, template_count,
469 flags)
470 tree declarator;
471 tree decl;
472 int template_count;
473 int flags;
474{
475 int finish_member = flags == 1;
476 int have_def = flags == 2;
477 int is_friend = flags == 3;
478
479 if (processing_explicit_specialization (template_count)
480 || finish_member
481 || TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
482 {
483 tree tmpl = NULL_TREE;
484 tree dname = DECL_NAME (decl);
485 tree ctype = DECL_CLASS_CONTEXT (decl);
486 tree targs;
487
488 /* We've come across a declarator that looks like: U f<T1,
489 T2, ...>(A1, A2, ..). This is an explicit template
490 specialization. Check that:
491
492 o The explicitly specified parameters together with those
493 that can be deduced by template argument deduction
494 uniquely determine a particular specialization.
495
496 See [temp.expl.spec]. */
497
498 if (!finish_member
499 && TREE_CODE (declarator) == TEMPLATE_ID_EXPR
500 && !processing_explicit_specialization (template_count)
501 && !is_friend)
502 {
503 if (!have_def)
504 /* This is not an explicit specialization. It must be
505 an explicit instantiation. */
506 return 2;
507 else if (pedantic)
508 pedwarn ("Explicit specialization not preceeded by "
509 "`template <>'");
510 }
511
512 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
513 {
514 tree fns;
515
516 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
517 0);
518 if (!ctype)
519 fns = IDENTIFIER_GLOBAL_VALUE (dname);
520 else
521 fns = dname;
522
523 declarator = lookup_template_function (fns, NULL_TREE);
524 }
525
526 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
527 {
528 /* A friend declaration. We can't do much, because we don't
529 know what this resolves to, yet. */
530 my_friendly_assert (is_friend != 0, 0);
531 SET_DECL_IMPLICIT_INSTANTIATION (decl);
532 return 1;
533 }
534
535 if (ctype
536 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE)
537 {
538 tree fns;
539
540 if (TYPE_BEING_DEFINED (ctype) && !finish_member)
541 {
542 /* Since finish_struct_1 has not been called yet, we
543 can't call lookup_fnfields. We note that this
544 template is a specialization, and proceed, letting
545 finish_struct_methods fix this up later. */
546 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
547 DECL_TEMPLATE_INFO (decl)
548 = perm_tree_cons (NULL_TREE,
549 TREE_OPERAND (declarator, 1),
550 NULL_TREE);
551 return 1;
552 }
553
554 fns = lookup_fnfields (TYPE_BINFO (ctype),
555 TREE_OPERAND (declarator, 0),
556 1);
557
558 if (fns == NULL_TREE)
559 {
560 cp_error ("No member template `%s' declared in `%T'",
561 IDENTIFIER_POINTER (TREE_OPERAND (declarator,
562 0)),
563 ctype);
564 return 1;
565 }
566 else
567 TREE_OPERAND (declarator, 0) = fns;
568 }
569
570 tmpl =
571 determine_explicit_specialization
572 (declarator, TREE_TYPE (decl), &targs,
573 TREE_CODE (decl) == TEMPLATE_DECL, 1);
574
575 if (tmpl)
576 {
577 /* Mangle the function name appropriately. */
578 if (name_mangling_version >= 1)
579 {
580 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
581
582 if (ctype
583 && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE)
584 arg_types =
585 hash_tree_chain (build_pointer_type (ctype),
586 arg_types);
587
588 DECL_ASSEMBLER_NAME (decl)
589 = build_template_decl_overload
590 (DECL_NAME (decl),
591 arg_types,
592 TREE_TYPE (TREE_TYPE (tmpl)),
593 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
594 targs, ctype != NULL_TREE);
595 }
596
597 if (is_friend && !have_def)
598 {
599 /* This is not really a declaration of a specialization.
600 It's just the name of an instantiation. But, it's not
601 a request for an instantiation, either. */
602 SET_DECL_IMPLICIT_INSTANTIATION (decl);
603 DECL_TEMPLATE_INFO (decl)
604 = perm_tree_cons (tmpl, targs, NULL_TREE);
605 return 1;
606 }
607
608 /* This function declaration is a template specialization.
609 Record that fact. */
610 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
611 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
612 = perm_tree_cons (targs, decl,
613 DECL_TEMPLATE_SPECIALIZATIONS
614 (tmpl));
615 /* If DECL_TI_TEMPLATE (decl), the decl is an
616 instantiation of a specialization of a member template.
617 (In other words, there was a member template, in a
618 class template. That member template was specialized.
619 We then instantiated the class, so there is now an
620 instance of that specialization.)
621
622 According to the CD2,
623
624 14.7.3.13 [tmpl.expl.spec]
625
626 A specialization of a member function template or
627 member class template of a non-specialized class
628 template is itself a template.
629
630 So, we just leave the template info alone in this case.
631 */
632 if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
633 DECL_TEMPLATE_INFO (decl)
634 = perm_tree_cons (tmpl, targs, NULL_TREE);
635 return 1;
636 }
637 }
638
639 return 0;
640}
641
8d08fdba 642/* Process information from new template parameter NEXT and append it to the
5566b478 643 LIST being built. */
e92cc029 644
8d08fdba
MS
645tree
646process_template_parm (list, next)
647 tree list, next;
648{
649 tree parm;
650 tree decl = 0;
a292b002 651 tree defval;
5566b478 652 int is_type, idx;
8d08fdba
MS
653 parm = next;
654 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
a292b002
MS
655 defval = TREE_PURPOSE (parm);
656 parm = TREE_VALUE (parm);
657 is_type = TREE_PURPOSE (parm) == class_type_node;
5566b478
MS
658
659 if (list)
660 {
661 tree p = TREE_VALUE (tree_last (list));
662
663 if (TREE_CODE (p) == TYPE_DECL)
664 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
665 else
666 idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
667 ++idx;
668 }
669 else
670 idx = 0;
671
8d08fdba
MS
672 if (!is_type)
673 {
674 tree tinfo = 0;
a292b002 675 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
8d08fdba 676 /* is a const-param */
a292b002 677 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
c11b6f21 678 PARM, 0, NULL_TREE);
8d08fdba
MS
679 /* A template parameter is not modifiable. */
680 TREE_READONLY (parm) = 1;
c91a56d2
MS
681 if (IS_AGGR_TYPE (TREE_TYPE (parm))
682 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
8d08fdba 683 {
c91a56d2
MS
684 cp_error ("`%#T' is not a valid type for a template constant parameter",
685 TREE_TYPE (parm));
686 if (DECL_NAME (parm) == NULL_TREE)
687 error (" a template type parameter must begin with `class' or `typename'");
8d08fdba
MS
688 TREE_TYPE (parm) = void_type_node;
689 }
37c46b43
MS
690 else if (pedantic
691 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
692 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
eb66be0e
MS
693 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
694 TREE_TYPE (parm));
8d08fdba
MS
695 tinfo = make_node (TEMPLATE_CONST_PARM);
696 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
697 if (TREE_PERMANENT (parm) == 0)
698 {
699 parm = copy_node (parm);
700 TREE_PERMANENT (parm) = 1;
701 }
702 TREE_TYPE (tinfo) = TREE_TYPE (parm);
703 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
704 DECL_INITIAL (decl) = tinfo;
705 DECL_INITIAL (parm) = tinfo;
5156628f 706 TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl);
8d08fdba
MS
707 }
708 else
709 {
5566b478
MS
710 tree t = make_lang_type (TEMPLATE_TYPE_PARM);
711 CLASSTYPE_GOT_SEMICOLON (t) = 1;
a292b002 712 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
d2e5ee5c
MS
713 TYPE_NAME (t) = decl;
714 TYPE_STUB_DECL (t) = decl;
a292b002 715 parm = decl;
5156628f 716 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
8d08fdba 717 }
8ccc31eb 718 SET_DECL_ARTIFICIAL (decl);
8d08fdba 719 pushdecl (decl);
a292b002 720 parm = build_tree_list (defval, parm);
8d08fdba
MS
721 return chainon (list, parm);
722}
723
724/* The end of a template parameter list has been reached. Process the
725 tree list into a parameter vector, converting each parameter into a more
726 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
727 as PARM_DECLs. */
728
729tree
730end_template_parm_list (parms)
731 tree parms;
732{
5566b478 733 int nparms;
8d08fdba 734 tree parm;
5566b478
MS
735 tree saved_parmlist = make_tree_vec (list_length (parms));
736
5566b478
MS
737 current_template_parms
738 = tree_cons (build_int_2 (0, processing_template_decl),
739 saved_parmlist, current_template_parms);
8d08fdba
MS
740
741 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
5566b478 742 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
a292b002 743
8d08fdba
MS
744 return saved_parmlist;
745}
746
5566b478
MS
747/* end_template_decl is called after a template declaration is seen. */
748
8d08fdba 749void
5566b478 750end_template_decl ()
8d08fdba 751{
386b8a85
JM
752 reset_specialization ();
753
5156628f 754 if (! processing_template_decl)
73aad9b9
JM
755 return;
756
5566b478
MS
757 /* This matches the pushlevel in begin_template_parm_list. */
758 poplevel (0, 0, 0);
8d08fdba 759
5566b478
MS
760 --processing_template_decl;
761 current_template_parms = TREE_CHAIN (current_template_parms);
762 (void) get_pending_sizes (); /* Why? */
763}
8d08fdba 764
9a3b49ac
MS
765/* Generate a valid set of template args from current_template_parms. */
766
767tree
768current_template_args ()
5566b478
MS
769{
770 tree header = current_template_parms;
98c1c668
JM
771 int length = list_length (header);
772 tree args = make_tree_vec (length);
773 int l = length;
774
5566b478 775 while (header)
8d08fdba 776 {
5566b478
MS
777 tree a = copy_node (TREE_VALUE (header));
778 int i = TREE_VEC_LENGTH (a);
779 TREE_TYPE (a) = NULL_TREE;
780 while (i--)
781 {
98c1c668
JM
782 tree t = TREE_VEC_ELT (a, i);
783
784 /* t will be a list if we are called from within a
785 begin/end_template_parm_list pair, but a vector directly
786 if within a begin/end_member_template_processing pair. */
787 if (TREE_CODE (t) == TREE_LIST)
788 {
789 t = TREE_VALUE (t);
790
791 if (TREE_CODE (t) == TYPE_DECL)
792 t = TREE_TYPE (t);
793 else
794 t = DECL_INITIAL (t);
795 }
796
5566b478
MS
797 TREE_VEC_ELT (a, i) = t;
798 }
98c1c668 799 TREE_VEC_ELT (args, --l) = a;
5566b478 800 header = TREE_CHAIN (header);
8d08fdba
MS
801 }
802
9a3b49ac
MS
803 return args;
804}
805
806void
807push_template_decl (decl)
808 tree decl;
809{
810 tree tmpl;
811 tree args = NULL_TREE;
812 tree info;
813 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
814 int primary = 0;
815
816 /* Kludge! */
817 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
818 && DECL_CLASS_CONTEXT (decl))
819 ;
820 /* Note that this template is a "primary template" */
821 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
822 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
823 primary = 1;
824
73aad9b9 825 /* Partial specialization. */
824b9a4c 826 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
73aad9b9
JM
827 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
828 {
829 tree type = TREE_TYPE (decl);
830 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
831 tree mainargs = CLASSTYPE_TI_ARGS (type);
832 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
833
834 for (; spec; spec = TREE_CHAIN (spec))
835 {
836 /* purpose: args to main template
837 value: spec template */
838 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
839 return;
840 }
841
6633d636
MS
842 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
843 = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms),
844 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
73aad9b9
JM
845 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
846 return;
847 }
848
9a3b49ac
MS
849 args = current_template_args ();
850
5566b478 851 if (! ctx || TYPE_BEING_DEFINED (ctx))
8d08fdba 852 {
5566b478 853 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
98c1c668 854 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
5566b478 855 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
98c1c668 856 if (DECL_LANG_SPECIFIC (decl))
786b5245
MM
857 {
858 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
859 DECL_STATIC_FUNCTION_P (tmpl) =
860 DECL_STATIC_FUNCTION_P (decl);
386b8a85
JM
861
862 if (DECL_TEMPLATE_SPECIALIZATION (decl))
863 {
864 /* A specialization of a member template of a template
865 class. */
866 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
867 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
868 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
869 }
786b5245 870 }
8d08fdba
MS
871 }
872 else
873 {
6633d636 874 tree t;
98c1c668 875 tree a;
6633d636 876
73aad9b9
JM
877 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
878 cp_error ("must specialize `%#T' before defining member `%#D'",
879 ctx, decl);
824b9a4c 880 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
5566b478 881 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
fc378698 882 else if (! DECL_TEMPLATE_INFO (decl))
c91a56d2
MS
883 {
884 cp_error ("template definition of non-template `%#D'", decl);
885 return;
886 }
8d08fdba 887 else
5566b478 888 tmpl = DECL_TI_TEMPLATE (decl);
98c1c668
JM
889
890 if (is_member_template (tmpl))
891 {
892 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
893 t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
894 if (TREE_VEC_LENGTH (t)
895 != TREE_VEC_LENGTH (a))
896 {
897 cp_error ("got %d template parameters for `%#D'",
898 TREE_VEC_LENGTH (a), decl);
899 cp_error (" but %d required", TREE_VEC_LENGTH (t));
900 }
901 if (TREE_VEC_LENGTH (args) > 1)
902 /* Get the template parameters for the enclosing template
903 class. */
904 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2);
905 else
906 a = NULL_TREE;
907 }
908 else
909 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
910
911 t = NULL_TREE;
6633d636
MS
912
913 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
98c1c668
JM
914 {
915 /* When processing an inline member template of a
916 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
917 if (CLASSTYPE_TI_SPEC_INFO (ctx))
918 t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
919 }
920 else if (CLASSTYPE_TEMPLATE_INFO (ctx))
921 t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));
922
923 /* There should be template arguments if and only if there is a
924 template class. */
925 my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0);
6633d636 926
98c1c668
JM
927 if (t != NULL_TREE
928 && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6633d636
MS
929 {
930 cp_error ("got %d template parameters for `%#D'",
98c1c668 931 TREE_VEC_LENGTH (a), decl);
6633d636
MS
932 cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
933 }
5566b478 934 }
98c1c668
JM
935 /* Get the innermost set of template arguments. */
936 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
8d08fdba 937
5566b478
MS
938 DECL_TEMPLATE_RESULT (tmpl) = decl;
939 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 940
5566b478
MS
941 if (! ctx)
942 tmpl = pushdecl_top_level (tmpl);
8d08fdba 943
5566b478 944 if (primary)
98c1c668 945 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
5566b478
MS
946
947 info = perm_tree_cons (tmpl, args, NULL_TREE);
948
824b9a4c 949 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
8d08fdba 950 {
5566b478
MS
951 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
952 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
51c184be 953 }
ec255269
MS
954 else if (! DECL_LANG_SPECIFIC (decl))
955 cp_error ("template declaration of `%#D'", decl);
51c184be 956 else
5566b478 957 DECL_TEMPLATE_INFO (decl) = info;
8d08fdba
MS
958}
959
8d08fdba
MS
960/* Convert all template arguments to their appropriate types, and return
961 a vector containing the resulting values. If any error occurs, return
962 error_mark_node. */
e92cc029 963
8d08fdba
MS
964static tree
965coerce_template_parms (parms, arglist, in_decl)
966 tree parms, arglist;
967 tree in_decl;
968{
a292b002 969 int nparms, nargs, i, lost = 0;
8d08fdba
MS
970 tree vec;
971
a292b002
MS
972 if (arglist == NULL_TREE)
973 nargs = 0;
974 else if (TREE_CODE (arglist) == TREE_VEC)
975 nargs = TREE_VEC_LENGTH (arglist);
8d08fdba 976 else
a292b002
MS
977 nargs = list_length (arglist);
978
979 nparms = TREE_VEC_LENGTH (parms);
980
981 if (nargs > nparms
982 || (nargs < nparms
983 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
8d08fdba
MS
984 {
985 error ("incorrect number of parameters (%d, should be %d)",
a292b002 986 nargs, nparms);
8d08fdba
MS
987 if (in_decl)
988 cp_error_at ("in template expansion for decl `%D'", in_decl);
989 return error_mark_node;
990 }
991
a292b002 992 if (arglist && TREE_CODE (arglist) == TREE_VEC)
8d08fdba
MS
993 vec = copy_node (arglist);
994 else
995 {
996 vec = make_tree_vec (nparms);
997 for (i = 0; i < nparms; i++)
998 {
a292b002
MS
999 tree arg;
1000
1001 if (arglist)
1002 {
1003 arg = arglist;
1004 arglist = TREE_CHAIN (arglist);
1005
1006 if (arg == error_mark_node)
1007 lost++;
1008 else
1009 arg = TREE_VALUE (arg);
1010 }
d2e5ee5c
MS
1011 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i)))
1012 == TYPE_DECL)
5566b478 1013 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
98c1c668 1014 vec, i, in_decl);
d2e5ee5c
MS
1015 else
1016 arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
98c1c668 1017 vec, i, in_decl);
a292b002 1018
8d08fdba
MS
1019 TREE_VEC_ELT (vec, i) = arg;
1020 }
1021 }
1022 for (i = 0; i < nparms; i++)
1023 {
1024 tree arg = TREE_VEC_ELT (vec, i);
a292b002 1025 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8d08fdba
MS
1026 tree val = 0;
1027 int is_type, requires_type;
1028
1029 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
a292b002 1030 requires_type = TREE_CODE (parm) == TYPE_DECL;
5566b478
MS
1031
1032 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
1033 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
1034 {
1035 cp_pedwarn ("to refer to a type member of a template parameter,");
1036 cp_pedwarn (" use `typename %E'", arg);
1037 arg = make_typename_type (TREE_OPERAND (arg, 0),
1038 TREE_OPERAND (arg, 1));
1039 is_type = 1;
1040 }
8d08fdba
MS
1041 if (is_type != requires_type)
1042 {
1043 if (in_decl)
5566b478
MS
1044 {
1045 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1046 i, in_decl);
1047 if (is_type)
1048 cp_error (" expected a constant of type `%T', got `%T'",
1049 TREE_TYPE (parm), arg);
1050 else
1051 cp_error (" expected a type, got `%E'", arg);
1052 }
8d08fdba
MS
1053 lost++;
1054 TREE_VEC_ELT (vec, i) = error_mark_node;
1055 continue;
1056 }
1057 if (is_type)
ec255269
MS
1058 {
1059 val = groktypename (arg);
5156628f 1060 if (! processing_template_decl)
ec255269
MS
1061 {
1062 tree t = target_type (val);
85b71cf2
JM
1063 if (TREE_CODE (t) != TYPENAME_TYPE
1064 && IS_AGGR_TYPE (t)
ec255269
MS
1065 && decl_function_context (TYPE_MAIN_DECL (t)))
1066 {
1067 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
1068 return error_mark_node;
1069 }
1070 }
1071 }
8d08fdba
MS
1072 else
1073 {
98c1c668 1074 tree t = tsubst (TREE_TYPE (parm), vec,
75b0bbce 1075 TREE_VEC_LENGTH (vec), in_decl);
5156628f 1076 if (processing_template_decl)
5566b478
MS
1077 val = arg;
1078 else
1079 val = digest_init (t, arg, (tree *) 0);
a292b002 1080
5156628f 1081 if (val == error_mark_node || processing_template_decl)
8d08fdba
MS
1082 ;
1083
1084 /* 14.2: Other template-arguments must be constant-expressions,
1085 addresses of objects or functions with external linkage, or of
1086 static class members. */
bd6dd845
MS
1087 else if (IS_AGGR_TYPE (TREE_TYPE (val)))
1088 {
1089 cp_error ("object `%E' cannot be used as template argument", arg);
1090 val = error_mark_node;
1091 }
8d08fdba
MS
1092 else if (!TREE_CONSTANT (val))
1093 {
1094 cp_error ("non-const `%E' cannot be used as template argument",
1095 arg);
1096 val = error_mark_node;
1097 }
f30432d7
MS
1098 else if (POINTER_TYPE_P (TREE_TYPE (val))
1099 && ! integer_zerop (val)
1100 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
1101 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
8d08fdba 1102 {
f30432d7
MS
1103 t = val;
1104 STRIP_NOPS (t);
1105 if (TREE_CODE (t) == ADDR_EXPR)
1106 {
1107 tree a = TREE_OPERAND (t, 0);
1108 STRIP_NOPS (a);
1109 if (TREE_CODE (a) == STRING_CST)
1110 {
1111 cp_error ("string literal %E is not a valid template argument", a);
1112 error ("because it is the address of an object with static linkage");
1113 val = error_mark_node;
1114 }
1115 else if (TREE_CODE (a) != VAR_DECL
1116 && TREE_CODE (a) != FUNCTION_DECL)
1117 goto bad;
893de33c 1118 else if (! TREE_PUBLIC (a))
f30432d7
MS
1119 {
1120 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
1121 val = error_mark_node;
1122 }
1123 }
1124 else
8d08fdba 1125 {
f30432d7
MS
1126 bad:
1127 cp_error ("`%E' is not a valid template argument", t);
1128 error ("it must be %s%s with external linkage",
1129 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
1130 ? "a pointer to " : "",
1131 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
1132 ? "a function" : "an object");
8d08fdba
MS
1133 val = error_mark_node;
1134 }
1135 }
1136 }
1137
1138 if (val == error_mark_node)
1139 lost++;
1140
1141 TREE_VEC_ELT (vec, i) = val;
1142 }
1143 if (lost)
1144 return error_mark_node;
1145 return vec;
1146}
1147
bd6dd845 1148static int
5566b478
MS
1149comp_template_args (oldargs, newargs)
1150 tree oldargs, newargs;
1151{
1152 int i;
1153
386b8a85
JM
1154 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
1155 return 0;
1156
5566b478
MS
1157 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
1158 {
1159 tree nt = TREE_VEC_ELT (newargs, i);
1160 tree ot = TREE_VEC_ELT (oldargs, i);
1161
1162 if (nt == ot)
1163 continue;
1164 if (TREE_CODE (nt) != TREE_CODE (ot))
1165 return 0;
67d743fe
MS
1166 if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
1167 {
1168 if (comptypes (ot, nt, 1))
1169 continue;
1170 }
1171 else if (cp_tree_equal (ot, nt) > 0)
5566b478
MS
1172 continue;
1173 return 0;
1174 }
1175 return 1;
1176}
1177
8d08fdba
MS
1178/* Given class template name and parameter list, produce a user-friendly name
1179 for the instantiation. */
e92cc029 1180
8d08fdba
MS
1181static char *
1182mangle_class_name_for_template (name, parms, arglist)
1183 char *name;
1184 tree parms, arglist;
1185{
1186 static struct obstack scratch_obstack;
1187 static char *scratch_firstobj;
1188 int i, nparms;
8d08fdba
MS
1189
1190 if (!scratch_firstobj)
fc378698 1191 gcc_obstack_init (&scratch_obstack);
8d08fdba
MS
1192 else
1193 obstack_free (&scratch_obstack, scratch_firstobj);
fc378698 1194 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
8d08fdba
MS
1195
1196#if 0
1197#define buflen sizeof(buf)
1198#define check if (bufp >= buf+buflen-1) goto too_long
1199#define ccat(c) *bufp++=(c); check
1200#define advance bufp+=strlen(bufp); check
1201#define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1202#else
1203#define check
1204#define ccat(c) obstack_1grow (&scratch_obstack, (c));
1205#define advance
1206#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1207#endif
8d08fdba
MS
1208
1209 cat (name);
1210 ccat ('<');
1211 nparms = TREE_VEC_LENGTH (parms);
1212 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
1213 for (i = 0; i < nparms; i++)
1214 {
a292b002
MS
1215 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1216 tree arg = TREE_VEC_ELT (arglist, i);
8d08fdba
MS
1217
1218 if (i)
1219 ccat (',');
1220
a292b002 1221 if (TREE_CODE (parm) == TYPE_DECL)
8d08fdba
MS
1222 {
1223 cat (type_as_string (arg, 0));
1224 continue;
1225 }
1226 else
1227 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
1228
1229 if (TREE_CODE (arg) == TREE_LIST)
1230 {
1231 /* New list cell was built because old chain link was in
1232 use. */
1233 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
1234 arg = TREE_VALUE (arg);
1235 }
1236 /* No need to check arglist against parmlist here; we did that
1237 in coerce_template_parms, called from lookup_template_class. */
1238 cat (expr_as_string (arg, 0));
1239 }
1240 {
1241 char *bufp = obstack_next_free (&scratch_obstack);
1242 int offset = 0;
1243 while (bufp[offset - 1] == ' ')
1244 offset--;
1245 obstack_blank_fast (&scratch_obstack, offset);
1246
1247 /* B<C<char> >, not B<C<char>> */
1248 if (bufp[offset - 1] == '>')
1249 ccat (' ');
1250 }
1251 ccat ('>');
1252 ccat ('\0');
1253 return (char *) obstack_base (&scratch_obstack);
1254
8926095f 1255#if 0
8d08fdba 1256 too_long:
8926095f 1257#endif
8d08fdba
MS
1258 fatal ("out of (preallocated) string space creating template instantiation name");
1259 /* NOTREACHED */
1260 return NULL;
1261}
1262
bd6dd845 1263static tree
5566b478
MS
1264classtype_mangled_name (t)
1265 tree t;
1266{
1267 if (CLASSTYPE_TEMPLATE_INFO (t)
1268 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
1269 {
1270 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
1271 char *mangled_name = mangle_class_name_for_template
1272 (IDENTIFIER_POINTER (name),
98c1c668 1273 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
5566b478
MS
1274 CLASSTYPE_TI_ARGS (t));
1275 tree id = get_identifier (mangled_name);
1276 IDENTIFIER_TEMPLATE (id) = name;
1277 return id;
1278 }
1279 else
1280 return TYPE_IDENTIFIER (t);
1281}
1282
1283static void
1284add_pending_template (d)
1285 tree d;
1286{
e92cc029
MS
1287 tree ti;
1288
1289 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
1290 ti = CLASSTYPE_TEMPLATE_INFO (d);
1291 else
1292 ti = DECL_TEMPLATE_INFO (d);
1293
824b9a4c 1294 if (TI_PENDING_TEMPLATE_FLAG (ti))
5566b478
MS
1295 return;
1296
1297 *template_tail = perm_tree_cons
1298 (current_function_decl, d, NULL_TREE);
1299 template_tail = &TREE_CHAIN (*template_tail);
824b9a4c 1300 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5566b478
MS
1301}
1302
386b8a85
JM
1303
1304/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1305 may be either a _DECL or an overloaded function or an
1306 IDENTIFIER_NODE), and ARGLIST. */
1307
1308tree
1309lookup_template_function (fns, arglist)
1310 tree fns, arglist;
1311{
1312 if (fns == NULL_TREE)
1313 {
1314 cp_error ("non-template used as template");
1315 return error_mark_node;
1316 }
1317
1318 if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
1319 {
1320 push_obstacks (&permanent_obstack, &permanent_obstack);
1321 arglist = copy_list (arglist);
1322 pop_obstacks ();
1323 }
1324
1325 return build_min (TEMPLATE_ID_EXPR,
1326 TREE_TYPE (fns)
1327 ? TREE_TYPE (fns) : unknown_type_node,
1328 fns, arglist);
1329}
1330
1331
8d08fdba
MS
1332/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1333 parameters, find the desired type.
1334
1335 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1336 Since ARGLIST is build on the decl_obstack, we must copy it here
1337 to keep it from being reclaimed when the decl storage is reclaimed.
1338
1339 IN_DECL, if non-NULL, is the template declaration we are trying to
1340 instantiate. */
e92cc029 1341
8d08fdba
MS
1342tree
1343lookup_template_class (d1, arglist, in_decl)
1344 tree d1, arglist;
1345 tree in_decl;
1346{
1347 tree template, parmlist;
1348 char *mangled_name;
5566b478 1349 tree id, t;
5566b478
MS
1350
1351 if (TREE_CODE (d1) == IDENTIFIER_NODE)
1352 {
1353 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
1354 if (! template)
1355 template = IDENTIFIER_CLASS_VALUE (d1);
1356 }
c91a56d2
MS
1357 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
1358 {
1359 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
1360 d1 = DECL_NAME (template);
1361 }
5566b478
MS
1362 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
1363 {
1364 template = CLASSTYPE_TI_TEMPLATE (d1);
1365 d1 = DECL_NAME (template);
1366 }
1367 else
1368 my_friendly_abort (272);
8d08fdba 1369
8d08fdba
MS
1370 /* With something like `template <class T> class X class X { ... };'
1371 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1372 We don't want to do that, but we have to deal with the situation, so
1373 let's give them some syntax errors to chew on instead of a crash. */
1374 if (! template)
1375 return error_mark_node;
1376 if (TREE_CODE (template) != TEMPLATE_DECL)
1377 {
1378 cp_error ("non-template type `%T' used as a template", d1);
1379 if (in_decl)
1380 cp_error_at ("for template declaration `%D'", in_decl);
1381 return error_mark_node;
1382 }
8d08fdba 1383
5566b478 1384 if (PRIMARY_TEMPLATE_P (template))
8d08fdba 1385 {
98c1c668 1386 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
5566b478
MS
1387
1388 arglist = coerce_template_parms (parmlist, arglist, template);
1389 if (arglist == error_mark_node)
1390 return error_mark_node;
1391 if (uses_template_parms (arglist))
1392 {
1393 tree found;
1394 if (comp_template_args
1395 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
1396 found = TREE_TYPE (template);
1397 else
1398 {
1399 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
1400 found; found = TREE_CHAIN (found))
1401 {
1402 if (TI_USES_TEMPLATE_PARMS (found)
1403 && comp_template_args (TREE_PURPOSE (found), arglist))
1404 break;
1405 }
1406 if (found)
1407 found = TREE_VALUE (found);
1408 }
1409
1410 if (found)
1411 {
1412 if (can_free (&permanent_obstack, arglist))
1413 obstack_free (&permanent_obstack, arglist);
1414 return found;
1415 }
1416 }
1417
8d08fdba
MS
1418 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
1419 parmlist, arglist);
1420 id = get_identifier (mangled_name);
5566b478 1421 IDENTIFIER_TEMPLATE (id) = d1;
8d08fdba 1422
5566b478 1423 maybe_push_to_top_level (uses_template_parms (arglist));
fc378698 1424 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
5566b478 1425 pop_from_top_level ();
8926095f 1426 }
5566b478 1427 else
8d08fdba 1428 {
5566b478
MS
1429 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1430 arglist, in_decl);
1431 id = d1;
1432 arglist = CLASSTYPE_TI_ARGS (ctx);
8d08fdba 1433
5566b478 1434 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
8d08fdba 1435 {
5156628f
MS
1436 int save_temp = processing_template_decl;
1437 processing_template_decl = 0;
fc378698 1438 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
5156628f 1439 processing_template_decl = save_temp;
8d08fdba
MS
1440 }
1441 else
1442 {
5566b478
MS
1443 t = lookup_nested_type_by_name (ctx, id);
1444 my_friendly_assert (t != NULL_TREE, 42);
8d08fdba 1445 }
5566b478
MS
1446 }
1447
e92cc029 1448 /* Seems to be wanted. */
5566b478
MS
1449 CLASSTYPE_GOT_SEMICOLON (t) = 1;
1450
1451 if (! CLASSTYPE_TEMPLATE_INFO (t))
1452 {
1453 arglist = copy_to_permanent (arglist);
1454 CLASSTYPE_TEMPLATE_INFO (t)
1455 = perm_tree_cons (template, arglist, NULL_TREE);
1456 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1457 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
1458 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1459 = uses_template_parms (arglist);
1460
1461 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
1462
e92cc029 1463 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
5566b478 1464 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
386b8a85 1465 /* if (! uses_template_parms (arglist)) */
5566b478
MS
1466 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
1467 = get_identifier (build_overload_name (t, 1, 1));
1468
1469 if (flag_external_templates && ! uses_template_parms (arglist)
1470 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1471 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
e92cc029 1472 add_pending_template (t);
8d08fdba 1473 }
8d08fdba 1474
5566b478 1475 return t;
8d08fdba
MS
1476}
1477\f
51c184be 1478/* Should be defined in parse.h. */
8d08fdba
MS
1479extern int yychar;
1480
1481int
1482uses_template_parms (t)
1483 tree t;
1484{
1485 if (!t)
1486 return 0;
1487 switch (TREE_CODE (t))
1488 {
1489 case INDIRECT_REF:
1490 case COMPONENT_REF:
1491 /* We assume that the object must be instantiated in order to build
1492 the COMPONENT_REF, so we test only whether the type of the
1493 COMPONENT_REF uses template parms. */
1494 return uses_template_parms (TREE_TYPE (t));
1495
1496 case IDENTIFIER_NODE:
1497 if (!IDENTIFIER_TEMPLATE (t))
1498 return 0;
5566b478 1499 my_friendly_abort (42);
8d08fdba
MS
1500
1501 /* aggregates of tree nodes */
1502 case TREE_VEC:
1503 {
1504 int i = TREE_VEC_LENGTH (t);
1505 while (i--)
1506 if (uses_template_parms (TREE_VEC_ELT (t, i)))
1507 return 1;
1508 return 0;
1509 }
1510 case TREE_LIST:
1511 if (uses_template_parms (TREE_PURPOSE (t))
1512 || uses_template_parms (TREE_VALUE (t)))
1513 return 1;
1514 return uses_template_parms (TREE_CHAIN (t));
1515
1516 /* constructed type nodes */
1517 case POINTER_TYPE:
1518 case REFERENCE_TYPE:
1519 return uses_template_parms (TREE_TYPE (t));
1520 case RECORD_TYPE:
b7484fbe
MS
1521 if (TYPE_PTRMEMFUNC_FLAG (t))
1522 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
8d08fdba 1523 case UNION_TYPE:
5566b478 1524 if (! CLASSTYPE_TEMPLATE_INFO (t))
8d08fdba 1525 return 0;
5566b478 1526 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
8d08fdba
MS
1527 case FUNCTION_TYPE:
1528 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1529 return 1;
1530 return uses_template_parms (TREE_TYPE (t));
1531 case ARRAY_TYPE:
1532 if (uses_template_parms (TYPE_DOMAIN (t)))
1533 return 1;
1534 return uses_template_parms (TREE_TYPE (t));
1535 case OFFSET_TYPE:
1536 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
1537 return 1;
1538 return uses_template_parms (TREE_TYPE (t));
1539 case METHOD_TYPE:
75b0bbce 1540 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
8d08fdba
MS
1541 return 1;
1542 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1543 return 1;
1544 return uses_template_parms (TREE_TYPE (t));
1545
1546 /* decl nodes */
1547 case TYPE_DECL:
5566b478
MS
1548 return uses_template_parms (TREE_TYPE (t));
1549
8d08fdba 1550 case FUNCTION_DECL:
5566b478
MS
1551 case VAR_DECL:
1552 /* ??? What about FIELD_DECLs? */
1553 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
1554 && uses_template_parms (DECL_TI_ARGS (t)))
8d08fdba
MS
1555 return 1;
1556 /* fall through */
5566b478 1557 case CONST_DECL:
8d08fdba 1558 case PARM_DECL:
5566b478
MS
1559 if (uses_template_parms (TREE_TYPE (t)))
1560 return 1;
8d08fdba
MS
1561 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
1562 return 1;
8d08fdba
MS
1563 return 0;
1564
1565 case CALL_EXPR:
1566 return uses_template_parms (TREE_TYPE (t));
1567 case ADDR_EXPR:
1568 return uses_template_parms (TREE_OPERAND (t, 0));
1569
1570 /* template parm nodes */
1571 case TEMPLATE_TYPE_PARM:
1572 case TEMPLATE_CONST_PARM:
1573 return 1;
1574
1575 /* simple type nodes */
1576 case INTEGER_TYPE:
1577 if (uses_template_parms (TYPE_MIN_VALUE (t)))
1578 return 1;
1579 return uses_template_parms (TYPE_MAX_VALUE (t));
1580
1581 case REAL_TYPE:
37c46b43 1582 case COMPLEX_TYPE:
8d08fdba 1583 case VOID_TYPE:
2986ae00 1584 case BOOLEAN_TYPE:
8d08fdba
MS
1585 return 0;
1586
85b71cf2
JM
1587 case ENUMERAL_TYPE:
1588 {
1589 tree v;
1590
1591 for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
1592 if (uses_template_parms (TREE_VALUE (v)))
1593 return 1;
1594 }
1595 return 0;
1596
8d08fdba
MS
1597 /* constants */
1598 case INTEGER_CST:
1599 case REAL_CST:
1600 case STRING_CST:
1601 return 0;
1602
1603 case ERROR_MARK:
1604 /* Non-error_mark_node ERROR_MARKs are bad things. */
1605 my_friendly_assert (t == error_mark_node, 274);
1606 /* NOTREACHED */
1607 return 0;
1608
ec255269 1609 case LOOKUP_EXPR:
5566b478 1610 case TYPENAME_TYPE:
8d08fdba
MS
1611 return 1;
1612
5156628f
MS
1613 case SCOPE_REF:
1614 return uses_template_parms (TREE_OPERAND (t, 0));
1615
db5ae43f
MS
1616 case CONSTRUCTOR:
1617 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1618 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
5156628f 1619 return uses_template_parms (TREE_OPERAND (t, 1));
db5ae43f 1620
42976354
BK
1621 case MODOP_EXPR:
1622 case CAST_EXPR:
1623 case REINTERPRET_CAST_EXPR:
1624 case CONST_CAST_EXPR:
1625 case STATIC_CAST_EXPR:
1626 case DYNAMIC_CAST_EXPR:
1627 case SIZEOF_EXPR:
1628 case ARROW_EXPR:
1629 case DOTSTAR_EXPR:
1630 case TYPEID_EXPR:
1631 return 1;
1632
8d08fdba
MS
1633 default:
1634 switch (TREE_CODE_CLASS (TREE_CODE (t)))
1635 {
1636 case '1':
1637 case '2':
ec255269 1638 case 'e':
8d08fdba
MS
1639 case '<':
1640 {
1641 int i;
1642 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
1643 if (uses_template_parms (TREE_OPERAND (t, i)))
1644 return 1;
1645 return 0;
1646 }
1647 default:
1648 break;
1649 }
1650 sorry ("testing %s for template parms",
1651 tree_code_name [(int) TREE_CODE (t)]);
1652 my_friendly_abort (82);
1653 /* NOTREACHED */
1654 return 0;
1655 }
1656}
1657
7215f9a0
MS
1658static struct tinst_level *current_tinst_level = 0;
1659static struct tinst_level *free_tinst_level = 0;
1660static int tinst_depth = 0;
e9f32eb5 1661extern int max_tinst_depth;
5566b478
MS
1662#ifdef GATHER_STATISTICS
1663int depth_reached = 0;
1664#endif
8d08fdba 1665
bd6dd845 1666static int
5566b478
MS
1667push_tinst_level (d)
1668 tree d;
8d08fdba
MS
1669{
1670 struct tinst_level *new;
8d08fdba 1671
7215f9a0
MS
1672 if (tinst_depth >= max_tinst_depth)
1673 {
5566b478
MS
1674 struct tinst_level *p = current_tinst_level;
1675 int line = lineno;
1676 char *file = input_filename;
1677
7215f9a0
MS
1678 error ("template instantiation depth exceeds maximum of %d",
1679 max_tinst_depth);
e9f32eb5 1680 error (" (use -ftemplate-depth-NN to increase the maximum)");
5566b478
MS
1681 cp_error (" instantiating `%D'", d);
1682
1683 for (; p; p = p->next)
1684 {
1685 cp_error (" instantiated from `%D'", p->decl);
1686 lineno = p->line;
1687 input_filename = p->file;
1688 }
1689 error (" instantiated from here");
1690
1691 lineno = line;
1692 input_filename = file;
1693
7215f9a0
MS
1694 return 0;
1695 }
1696
8d08fdba
MS
1697 if (free_tinst_level)
1698 {
1699 new = free_tinst_level;
1700 free_tinst_level = new->next;
1701 }
1702 else
1703 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1704
5566b478
MS
1705 new->decl = d;
1706 new->line = lineno;
1707 new->file = input_filename;
8d08fdba
MS
1708 new->next = current_tinst_level;
1709 current_tinst_level = new;
5566b478 1710
7215f9a0 1711 ++tinst_depth;
5566b478
MS
1712#ifdef GATHER_STATISTICS
1713 if (tinst_depth > depth_reached)
1714 depth_reached = tinst_depth;
1715#endif
1716
7215f9a0 1717 return 1;
8d08fdba
MS
1718}
1719
1720void
1721pop_tinst_level ()
1722{
1723 struct tinst_level *old = current_tinst_level;
1724
1725 current_tinst_level = old->next;
1726 old->next = free_tinst_level;
1727 free_tinst_level = old;
7215f9a0 1728 --tinst_depth;
8d08fdba
MS
1729}
1730
1731struct tinst_level *
1732tinst_for_decl ()
1733{
1734 struct tinst_level *p = current_tinst_level;
1735
1736 if (p)
1737 for (; p->next ; p = p->next )
1738 ;
1739 return p;
1740}
1741
1742tree
5566b478
MS
1743instantiate_class_template (type)
1744 tree type;
8d08fdba 1745{
fc378698 1746 tree template, template_info, args, pattern, t, *field_chain;
8d08fdba 1747
5566b478 1748 if (type == error_mark_node)
8d08fdba
MS
1749 return error_mark_node;
1750
5566b478
MS
1751 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1752
1753 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1754 return type;
1755
1756 template = TI_TEMPLATE (template_info);
1757 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1758 args = TI_ARGS (template_info);
73aad9b9
JM
1759
1760 t = most_specialized_class
1761 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1762
1763 if (t == error_mark_node)
1764 {
1765 char *str = "candidates are:";
1766 cp_error ("ambiguous class template instantiation for `%#T'", type);
1767 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1768 {
1769 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1770 {
1771 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1772 str = " ";
1773 }
1774 }
1775 TYPE_BEING_DEFINED (type) = 1;
de22184b 1776 return error_mark_node;
73aad9b9
JM
1777 }
1778 else if (t)
1779 pattern = TREE_TYPE (t);
1780 else
1781 pattern = TREE_TYPE (template);
5566b478
MS
1782
1783 if (TYPE_SIZE (pattern) == NULL_TREE)
ec255269 1784 return type;
5566b478 1785
73aad9b9
JM
1786 if (t)
1787 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1788
5566b478
MS
1789 TYPE_BEING_DEFINED (type) = 1;
1790
1791 if (! push_tinst_level (type))
1792 return type;
8d08fdba 1793
5566b478
MS
1794 maybe_push_to_top_level (uses_template_parms (type));
1795 pushclass (type, 0);
1796
1797 if (flag_external_templates)
1798 {
1799 if (flag_alt_external_templates)
1800 {
1801 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1802 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1803 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1804 = ! CLASSTYPE_INTERFACE_ONLY (type)
1805 && CLASSTYPE_INTERFACE_KNOWN (type);
1806 }
1807 else
1808 {
1809 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1810 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1811 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1812 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1813 = ! CLASSTYPE_INTERFACE_ONLY (type)
1814 && CLASSTYPE_INTERFACE_KNOWN (type);
1815 }
1816 }
1817 else
8d08fdba 1818 {
5566b478
MS
1819 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1820 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
8d08fdba
MS
1821 }
1822
f7da6097
MS
1823 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1824 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1825 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1826 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1827 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1828 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1829 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1830 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1831 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1832 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1833 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1834 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1835 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1836 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1837 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1838 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1839 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1840 TYPE_USES_COMPLEX_INHERITANCE (type)
1841 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1842 TYPE_USES_MULTIPLE_INHERITANCE (type)
1843 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1844 TYPE_USES_VIRTUAL_BASECLASSES (type)
1845 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1846 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1847 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1848
5566b478
MS
1849 {
1850 tree binfo = TYPE_BINFO (type);
1851 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1852
1853 if (pbases)
1854 {
1855 tree bases;
1856 int i;
1857 int len = TREE_VEC_LENGTH (pbases);
de22184b 1858 bases = make_tree_vec (len);
5566b478
MS
1859 for (i = 0; i < len; ++i)
1860 {
1861 tree elt;
1862
1863 TREE_VEC_ELT (bases, i) = elt
98c1c668 1864 = tsubst (TREE_VEC_ELT (pbases, i), args,
5566b478
MS
1865 TREE_VEC_LENGTH (args), NULL_TREE);
1866 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1867
6633d636
MS
1868 if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
1869 cp_error
1870 ("base type `%T' of `%T' fails to be a struct or class type",
1871 TREE_TYPE (elt), type);
1872 else if (! uses_template_parms (type)
1873 && (TYPE_SIZE (complete_type (TREE_TYPE (elt)))
1874 == NULL_TREE))
5566b478
MS
1875 cp_error ("base class `%T' of `%T' has incomplete type",
1876 TREE_TYPE (elt), type);
1877 }
de22184b
MS
1878 /* Don't initialize this until the vector is filled out, or
1879 lookups will crash. */
1880 BINFO_BASETYPES (binfo) = bases;
5566b478
MS
1881 }
1882 }
1883
1884 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1885
1886 field_chain = &TYPE_FIELDS (type);
5566b478
MS
1887
1888 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
8d08fdba 1889 {
5566b478
MS
1890 tree name = TREE_PURPOSE (t);
1891 tree tag = TREE_VALUE (t);
5566b478 1892
fc378698 1893 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
5566b478 1894 if (TREE_CODE (tag) == ENUMERAL_TYPE)
8d08fdba 1895 {
98c1c668 1896 tree e, newtag = tsubst_enum (tag, args,
b3d5a58b 1897 TREE_VEC_LENGTH (args), field_chain);
5566b478 1898
5566b478 1899 while (*field_chain)
37c46b43
MS
1900 {
1901 DECL_FIELD_CONTEXT (*field_chain) = type;
1902 field_chain = &TREE_CHAIN (*field_chain);
1903 }
8d08fdba 1904 }
b87692e5 1905 else
98c1c668 1906 tsubst (tag, args,
b87692e5 1907 TREE_VEC_LENGTH (args), NULL_TREE);
8d08fdba
MS
1908 }
1909
5566b478
MS
1910 /* Don't replace enum constants here. */
1911 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1912 if (TREE_CODE (t) != CONST_DECL)
1913 {
98c1c668 1914 tree r = tsubst (t, args,
5566b478
MS
1915 TREE_VEC_LENGTH (args), NULL_TREE);
1916 if (TREE_CODE (r) == VAR_DECL)
1917 {
1918 if (! uses_template_parms (r))
1919 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
e92cc029 1920 /* Perhaps I should do more of grokfield here. */
5566b478
MS
1921 start_decl_1 (r);
1922 DECL_IN_AGGR_P (r) = 1;
1923 DECL_EXTERNAL (r) = 1;
1924 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1925 }
1926
1927 *field_chain = r;
1928 field_chain = &TREE_CHAIN (r);
1929 }
8d08fdba 1930
5566b478 1931 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
f7da6097
MS
1932 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
1933 {
1934 if (DECL_CONSTRUCTOR_P (t))
1935 grok_ctor_properties (type, t);
1936 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
1937 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
1938 }
8d08fdba 1939
5566b478 1940 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
fc378698 1941 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
98c1c668 1942 args, TREE_VEC_LENGTH (args), NULL_TREE);
5566b478
MS
1943
1944 {
beb53fb8 1945 tree d = CLASSTYPE_FRIEND_CLASSES (type)
98c1c668 1946 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args,
beb53fb8 1947 TREE_VEC_LENGTH (args), NULL_TREE);
fc378698
MS
1948
1949 /* This does injection for friend classes. */
1950 for (; d; d = TREE_CHAIN (d))
1951 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1952
98c1c668 1953 d = tsubst (DECL_TEMPLATE_INJECT (template), args,
056c014d 1954 TREE_VEC_LENGTH (args), NULL_TREE);
5566b478
MS
1955
1956 for (; d; d = TREE_CHAIN (d))
fc378698
MS
1957 {
1958 tree t = TREE_VALUE (d);
1959
1960 if (TREE_CODE (t) == TYPE_DECL)
1961 /* Already injected. */;
1962 else
1963 pushdecl (t);
1964 }
5566b478
MS
1965 }
1966
5566b478 1967 if (! uses_template_parms (type))
8d08fdba 1968 {
5566b478
MS
1969 tree tmp;
1970 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
ce122a86 1971 if (TREE_CODE (tmp) == FIELD_DECL)
c73964b2
MS
1972 {
1973 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1974 require_complete_type (tmp);
1975 }
5566b478 1976
6467930b 1977 type = finish_struct_1 (type, 0);
e349ee73 1978 CLASSTYPE_GOT_SEMICOLON (type) = 1;
e92cc029
MS
1979
1980 repo_template_used (type);
fe4e8851
JM
1981 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1982 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
8d08fdba
MS
1983 }
1984 else
1985 {
5566b478
MS
1986 TYPE_SIZE (type) = integer_zero_node;
1987 CLASSTYPE_METHOD_VEC (type)
1988 = finish_struct_methods (type, TYPE_METHODS (type), 1);
8d08fdba
MS
1989 }
1990
5566b478
MS
1991 TYPE_BEING_DEFINED (type) = 0;
1992 popclass (0);
1993
1994 pop_from_top_level ();
1995 pop_tinst_level ();
1996
1997 return type;
8d08fdba
MS
1998}
1999
2000static int
2001list_eq (t1, t2)
2002 tree t1, t2;
2003{
2004 if (t1 == NULL_TREE)
2005 return t2 == NULL_TREE;
2006 if (t2 == NULL_TREE)
2007 return 0;
2008 /* Don't care if one declares its arg const and the other doesn't -- the
2009 main variant of the arg type is all that matters. */
2010 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
2011 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
2012 return 0;
2013 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
2014}
2015
5566b478 2016tree
8d08fdba
MS
2017lookup_nested_type_by_name (ctype, name)
2018 tree ctype, name;
2019{
2020 tree t;
2021
5566b478
MS
2022 complete_type (ctype);
2023
db5ae43f
MS
2024 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
2025 {
f017f649
JM
2026 if (name == TREE_PURPOSE (t)
2027 /* this catches typedef enum { foo } bar; */
2028 || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
db5ae43f
MS
2029 return TREE_VALUE (t);
2030 }
8d08fdba
MS
2031 return NULL_TREE;
2032}
2033
75b0bbce 2034tree
8d08fdba 2035tsubst (t, args, nargs, in_decl)
98c1c668 2036 tree t, args;
8d08fdba
MS
2037 int nargs;
2038 tree in_decl;
2039{
2040 tree type;
2041
5566b478
MS
2042 if (t == NULL_TREE || t == error_mark_node
2043 || t == integer_type_node
2044 || t == void_type_node
2045 || t == char_type_node)
8d08fdba
MS
2046 return t;
2047
2048 type = TREE_TYPE (t);
5566b478
MS
2049 if (type == unknown_type_node)
2050 my_friendly_abort (42);
824b9a4c
MS
2051 if (type && TREE_CODE (t) != FUNCTION_DECL
2052 && TREE_CODE (t) != TYPENAME_TYPE)
b7484fbe
MS
2053 type = tsubst (type, args, nargs, in_decl);
2054
8d08fdba
MS
2055 switch (TREE_CODE (t))
2056 {
2057 case RECORD_TYPE:
2058 if (TYPE_PTRMEMFUNC_P (t))
5566b478
MS
2059 {
2060 tree r = build_ptrmemfunc_type
2061 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
2062 return cp_build_type_variant (r, TYPE_READONLY (t),
2063 TYPE_VOLATILE (t));
2064 }
2065
8d08fdba 2066 /* else fall through */
5566b478
MS
2067 case UNION_TYPE:
2068 if (uses_template_parms (t))
2069 {
2070 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
2071 tree r = lookup_template_class (t, argvec, in_decl);
2072 return cp_build_type_variant (r, TYPE_READONLY (t),
2073 TYPE_VOLATILE (t));
2074 }
8d08fdba 2075
5566b478 2076 /* else fall through */
8d08fdba
MS
2077 case ERROR_MARK:
2078 case IDENTIFIER_NODE:
2079 case OP_IDENTIFIER:
2080 case VOID_TYPE:
2081 case REAL_TYPE:
37c46b43 2082 case COMPLEX_TYPE:
2986ae00 2083 case BOOLEAN_TYPE:
8d08fdba
MS
2084 case INTEGER_CST:
2085 case REAL_CST:
2086 case STRING_CST:
8d08fdba
MS
2087 return t;
2088
5566b478
MS
2089 case ENUMERAL_TYPE:
2090 {
2091 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2092 if (ctx == NULL_TREE)
2093 return t;
b87692e5
MS
2094 else if (ctx == current_function_decl)
2095 return lookup_name (TYPE_IDENTIFIER (t), 1);
5566b478
MS
2096 else
2097 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
2098 }
2099
8d08fdba
MS
2100 case INTEGER_TYPE:
2101 if (t == integer_type_node)
2102 return t;
2103
2104 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
2105 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
2106 return t;
5566b478
MS
2107
2108 {
37c46b43
MS
2109 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
2110 max = tsubst_expr (max, args, nargs, in_decl);
5156628f 2111 if (processing_template_decl)
5566b478
MS
2112 {
2113 tree itype = make_node (INTEGER_TYPE);
2114 TYPE_MIN_VALUE (itype) = size_zero_node;
37c46b43
MS
2115 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
2116 integer_one_node);
5566b478
MS
2117 return itype;
2118 }
37c46b43
MS
2119
2120 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
5566b478
MS
2121 return build_index_2_type (size_zero_node, max);
2122 }
8d08fdba
MS
2123
2124 case TEMPLATE_TYPE_PARM:
98c1c668 2125 case TEMPLATE_CONST_PARM:
db5ae43f 2126 {
98c1c668
JM
2127 int idx;
2128 int level;
2129
2130 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2131 {
2132 idx = TEMPLATE_TYPE_IDX (t);
2133 level = TEMPLATE_TYPE_LEVEL (t);
2134 }
2135 else
2136 {
2137 idx = TEMPLATE_CONST_IDX (t);
2138 level = TEMPLATE_CONST_LEVEL (t);
2139 }
2140
2141 if (TREE_VEC_LENGTH (args) > 0)
2142 {
2143 tree arg = NULL_TREE;
2144
2145 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2146 {
2147 if (TREE_VEC_LENGTH (args) >= level - 1)
2148 arg = TREE_VEC_ELT
2149 (TREE_VEC_ELT (args, level - 1), idx);
2150 }
2151 else if (level == 1)
2152 arg = TREE_VEC_ELT (args, idx);
2153
2154 if (arg != NULL_TREE)
2155 {
2156 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2157 return cp_build_type_variant
2158 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
2159 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
2160 else
2161 return arg;
2162 }
2163 }
2164
2165 /* If we get here, we must have been looking at a parm for a
2166 more deeply nested template. */
2167 my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM
2168 && TEMPLATE_CONST_LEVEL (t) > 1)
2169 || (TREE_CODE (t) == TEMPLATE_TYPE_PARM
2170 && TEMPLATE_TYPE_LEVEL (t) > 1),
2171 0);
2172 return t;
db5ae43f 2173 }
8d08fdba 2174
98c1c668
JM
2175 case TEMPLATE_DECL:
2176 {
2177 /* We can get here when processing a member template function
2178 of a template class. */
2179 tree tmpl;
2180 tree decl = DECL_TEMPLATE_RESULT (t);
2181 tree new_decl;
2182 tree parms;
386b8a85 2183 tree spec;
98c1c668
JM
2184 int i;
2185
2186 /* We might already have an instance of this template. */
2187 tree instances = DECL_TEMPLATE_INSTANTIATIONS (t);
2188 tree ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, in_decl);
2189
2190 for (; instances; instances = TREE_CHAIN (instances))
2191 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances)) == ctx)
2192 return TREE_VALUE (instances);
2193
2194 /* Make a new template decl. It will be similar to the
2195 original, but will record the current template arguments.
2196 We also create a new function declaration, which is just
2197 like the old one, but points to this new template, rather
2198 than the old one. */
2199 tmpl = copy_node (t);
2200 copy_lang_decl (tmpl);
2201 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0);
2202 DECL_CHAIN (tmpl) = NULL_TREE;
2203 TREE_CHAIN (tmpl) = NULL_TREE;
2204 DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
2205 new_decl = tsubst (decl, args, nargs, in_decl);
2206 DECL_RESULT (tmpl) = new_decl;
2207 DECL_INITIAL (new_decl) = DECL_INITIAL (decl);
2208 DECL_TI_TEMPLATE (new_decl) = tmpl;
2209 TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
2210 DECL_TEMPLATE_INSTANTIATIONS(tmpl) = NULL_TREE;
2211
2212 /* The template parameters for this new template are all the
2213 template parameters for the old template, except the
2214 outermost level of parameters. */
2215 DECL_TEMPLATE_PARMS (tmpl)
2216 = copy_node (DECL_TEMPLATE_PARMS (tmpl));
2217 for (parms = DECL_TEMPLATE_PARMS (tmpl);
2218 TREE_CHAIN (parms) != NULL_TREE;
2219 parms = TREE_CHAIN (parms))
2220 TREE_CHAIN (parms) = copy_node (TREE_CHAIN (parms));
2221
2222 /* Record this partial instantiation. */
2223 DECL_TEMPLATE_INSTANTIATIONS (t)
2224 = perm_tree_cons (NULL_TREE, tmpl,
2225 DECL_TEMPLATE_INSTANTIATIONS (t));
386b8a85
JM
2226
2227 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
98c1c668
JM
2228 return tmpl;
2229 }
8d08fdba
MS
2230
2231 case FUNCTION_DECL:
2232 {
5566b478 2233 tree r = NULL_TREE;
386b8a85 2234 tree ctx;
5566b478
MS
2235
2236 int member;
2237
8d08fdba
MS
2238 if (DECL_CONTEXT (t) != NULL_TREE
2239 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
2240 {
5566b478
MS
2241 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
2242 member = 2;
2243 else
2244 member = 1;
2245 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
2246 type = tsubst (type, args, nargs, in_decl);
2247 }
2248 else
2249 {
2250 member = 0;
2251 ctx = NULL_TREE;
2252 type = tsubst (type, args, nargs, in_decl);
2253 }
8d08fdba 2254
5566b478
MS
2255 if (type == TREE_TYPE (t)
2256 && (! member || ctx == DECL_CLASS_CONTEXT (t)))
d22c8596
MS
2257 {
2258 t = copy_node (t);
2259 copy_lang_decl (t);
2260 return t;
2261 }
8145f082 2262
5566b478
MS
2263 /* Do we already have this instantiation? */
2264 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2265 {
98c1c668 2266 tree tmpl = DECL_TI_TEMPLATE (t);
5566b478
MS
2267 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2268
2269 for (; decls; decls = TREE_CHAIN (decls))
2270 if (TREE_TYPE (TREE_VALUE (decls)) == type
2271 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
2272 return TREE_VALUE (decls);
2273 }
2274
2275 /* We do NOT check for matching decls pushed separately at this
2276 point, as they may not represent instantiations of this
2277 template, and in any case are considered separate under the
312e7d50 2278 discrete model. Instead, see add_maybe_template. */
5566b478
MS
2279
2280 r = copy_node (t);
2281 copy_lang_decl (r);
2282 TREE_TYPE (r) = type;
2283
2284 DECL_CONTEXT (r)
2285 = tsubst (DECL_CONTEXT (t), args, nargs, t);
2286 DECL_CLASS_CONTEXT (r) = ctx;
2287
2288 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
2289 IDENTIFIER_POINTER (DECL_NAME (r)),
2290 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
2291 {
2292 /* Type-conversion operator. Reconstruct the name, in
2293 case it's the name of one of the template's parameters. */
2294 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
2295 }
2296
5566b478
MS
2297 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
2298 {
2299 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
2300 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
2301 buf = (char *) alloca (strlen (dbuf)
2302 + sizeof (DESTRUCTOR_DECL_PREFIX));
2303 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
2304 buf[len] = '\0';
2305 strcat (buf, dbuf);
2306 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
8d08fdba 2307 }
386b8a85
JM
2308 else
2309 {
2310 /* Instantiations of template functions must be mangled
2311 specially, in order to conform to 14.5.5.1
2312 [temp.over.link]. We use in_decl below rather than
2313 DECL_TI_TEMPLATE (r) because the latter is set to
2314 NULL_TREE in instantiate_decl. */
2315 tree tmpl;
2316 tree arg_types;
2317
2318 if (DECL_TEMPLATE_INFO (r))
2319 tmpl = DECL_TI_TEMPLATE (r);
2320 else
2321 tmpl = in_decl;
2322
2323 /* tmpl will be NULL if this is a specialization of a
2324 member template of a template class. */
2325 if (name_mangling_version < 1
2326 || tmpl == NULL_TREE
2327 || (member && !is_member_template (tmpl)
2328 && !DECL_TEMPLATE_INFO (tmpl)))
2329 {
2330 arg_types = TYPE_ARG_TYPES (type);
2331 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2332 arg_types = hash_tree_chain
2333 (build_pointer_type (DECL_CONTEXT (r)),
2334 arg_types);
2335
2336 DECL_ASSEMBLER_NAME (r)
2337 = build_decl_overload (DECL_NAME (r), arg_types,
2338 member);
2339 }
2340 else
2341 {
2342 /* We pass the outermost template parameters to
2343 build_template_decl_overload since the innermost
2344 template parameters are still just template
2345 parameters; there are no corresponding subsitution
2346 arguments. */
2347 tree tparms = DECL_TEMPLATE_PARMS (tmpl);
2348
2349 while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
2350 tparms = TREE_CHAIN (tparms);
2351
2352 my_friendly_assert (tparms != NULL_TREE
2353 && TREE_CODE (tparms) == TREE_LIST,
2354 0);
2355 tparms = TREE_VALUE (tparms);
2356
2357 arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
2358 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2359 arg_types = hash_tree_chain
2360 (build_pointer_type (DECL_CONTEXT (r)),
2361 arg_types);
2362
2363 DECL_ASSEMBLER_NAME (r)
2364 = build_template_decl_overload
2365 (DECL_NAME (r), arg_types,
2366 TREE_TYPE (TREE_TYPE (tmpl)),
2367 tparms,
2368 TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC
2369 ? TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1) :
2370 args,
2371 member);
2372 }
2373 }
5566b478
MS
2374 DECL_RTL (r) = 0;
2375 make_decl_rtl (r, NULL_PTR, 1);
2376
2377 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
2378 DECL_MAIN_VARIANT (r) = r;
2379 DECL_RESULT (r) = NULL_TREE;
2380 DECL_INITIAL (r) = NULL_TREE;
2381
2382 TREE_STATIC (r) = 0;
2383 TREE_PUBLIC (r) = 1;
2384 DECL_EXTERNAL (r) = 1;
2385 DECL_INTERFACE_KNOWN (r) = 0;
2386 DECL_DEFER_OUTPUT (r) = 0;
2387 TREE_CHAIN (r) = NULL_TREE;
2388 DECL_CHAIN (r) = NULL_TREE;
2389
2390 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
2391 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
2392
2393 /* Look for matching decls for the moment. */
312e7d50 2394 if (! member && ! flag_ansi_overloading)
8d08fdba 2395 {
5566b478
MS
2396 tree decls = lookup_name_nonclass (DECL_NAME (t));
2397 tree d = NULL_TREE;
2398
2399 if (decls == NULL_TREE)
2400 /* no match */;
2401 else if (is_overloaded_fn (decls))
2402 for (decls = get_first_fn (decls); decls;
2403 decls = DECL_CHAIN (decls))
8d08fdba 2404 {
5566b478
MS
2405 if (TREE_CODE (decls) == FUNCTION_DECL
2406 && TREE_TYPE (decls) == type)
8d08fdba 2407 {
5566b478
MS
2408 d = decls;
2409 break;
8d08fdba
MS
2410 }
2411 }
2412
5566b478
MS
2413 if (d)
2414 {
2415 int dcl_only = ! DECL_INITIAL (d);
2416 if (dcl_only)
2417 DECL_INITIAL (r) = error_mark_node;
2418 duplicate_decls (r, d);
2419 r = d;
2420 if (dcl_only)
2421 DECL_INITIAL (r) = 0;
2422 }
2423 }
2424
2425 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2426 {
2427 tree tmpl = DECL_TI_TEMPLATE (t);
2428 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
98c1c668
JM
2429 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2430
2431 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
2432 argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec);
5566b478
MS
2433
2434 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2435 *declsp = perm_tree_cons (argvec, r, *declsp);
2436
2437 /* If we have a preexisting version of this function, don't expand
2438 the template version, use the other instead. */
2439 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
2440 SET_DECL_TEMPLATE_SPECIALIZATION (r);
2441 else
2442 SET_DECL_IMPLICIT_INSTANTIATION (r);
2443
beb53fb8
JM
2444 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
2445 = tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
8d08fdba 2446 }
8d08fdba 2447
e92cc029
MS
2448 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2449 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2450 if (member
2451 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
2452 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
2453
8d08fdba
MS
2454 return r;
2455 }
2456
2457 case PARM_DECL:
2458 {
5566b478
MS
2459 tree r = copy_node (t);
2460 TREE_TYPE (r) = type;
8d08fdba 2461 DECL_INITIAL (r) = TREE_TYPE (r);
5566b478 2462 DECL_CONTEXT (r) = NULL_TREE;
f83b0cb6
JM
2463#ifdef PROMOTE_PROTOTYPES
2464 if ((TREE_CODE (type) == INTEGER_TYPE
2465 || TREE_CODE (type) == ENUMERAL_TYPE)
2466 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2467 DECL_ARG_TYPE (r) = integer_type_node;
2468#endif
8d08fdba
MS
2469 if (TREE_CHAIN (t))
2470 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
2471 return r;
2472 }
2473
5566b478
MS
2474 case FIELD_DECL:
2475 {
2476 tree r = copy_node (t);
2477 TREE_TYPE (r) = type;
2478 copy_lang_decl (r);
2479#if 0
2480 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
2481#endif
2482 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
2483 TREE_CHAIN (r) = NULL_TREE;
2484 return r;
2485 }
2486
2487 case USING_DECL:
2488 {
2489 tree r = copy_node (t);
2490 DECL_INITIAL (r)
2491 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
2492 TREE_CHAIN (r) = NULL_TREE;
2493 return r;
2494 }
2495
2496 case VAR_DECL:
2497 {
2498 tree r;
2499 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
2500
2501 /* Do we already have this instantiation? */
2502 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2503 {
2504 tree tmpl = DECL_TI_TEMPLATE (t);
2505 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2506
2507 for (; decls; decls = TREE_CHAIN (decls))
2508 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
2509 return TREE_VALUE (decls);
2510 }
2511
2512 r = copy_node (t);
2513 TREE_TYPE (r) = type;
2514 DECL_CONTEXT (r) = ctx;
2515 if (TREE_STATIC (r))
2516 DECL_ASSEMBLER_NAME (r)
2517 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
d11ad92e
MS
2518
2519 /* Don't try to expand the initializer until someone tries to use
2520 this variable; otherwise we run into circular dependencies. */
2521 DECL_INITIAL (r) = NULL_TREE;
5566b478
MS
2522
2523 DECL_RTL (r) = 0;
2524 DECL_SIZE (r) = 0;
2525
2526 if (DECL_LANG_SPECIFIC (r))
2527 {
2528 copy_lang_decl (r);
2529 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
2530 }
2531
2532 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2533 {
2534 tree tmpl = DECL_TI_TEMPLATE (t);
2535 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
98c1c668 2536 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
5566b478
MS
2537
2538 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2539 *declsp = perm_tree_cons (argvec, r, *declsp);
2540 SET_DECL_IMPLICIT_INSTANTIATION (r);
2541 }
2542 TREE_CHAIN (r) = NULL_TREE;
2543 return r;
2544 }
2545
2546 case TYPE_DECL:
d2e5ee5c
MS
2547 if (t == TYPE_NAME (TREE_TYPE (t)))
2548 return TYPE_NAME (type);
2549
5566b478
MS
2550 {
2551 tree r = copy_node (t);
2552 TREE_TYPE (r) = type;
909e536a 2553 DECL_CONTEXT (r) = current_class_type;
5566b478
MS
2554 TREE_CHAIN (r) = NULL_TREE;
2555 return r;
2556 }
2557
8d08fdba
MS
2558 case TREE_LIST:
2559 {
2560 tree purpose, value, chain, result;
2561 int via_public, via_virtual, via_protected;
2562
2563 if (t == void_list_node)
2564 return t;
2565
2566 via_public = TREE_VIA_PUBLIC (t);
2567 via_protected = TREE_VIA_PROTECTED (t);
2568 via_virtual = TREE_VIA_VIRTUAL (t);
2569
2570 purpose = TREE_PURPOSE (t);
2571 if (purpose)
2572 purpose = tsubst (purpose, args, nargs, in_decl);
2573 value = TREE_VALUE (t);
2574 if (value)
2575 value = tsubst (value, args, nargs, in_decl);
2576 chain = TREE_CHAIN (t);
2577 if (chain && chain != void_type_node)
2578 chain = tsubst (chain, args, nargs, in_decl);
2579 if (purpose == TREE_PURPOSE (t)
2580 && value == TREE_VALUE (t)
2581 && chain == TREE_CHAIN (t))
2582 return t;
2583 result = hash_tree_cons (via_public, via_virtual, via_protected,
2584 purpose, value, chain);
2585 TREE_PARMLIST (result) = TREE_PARMLIST (t);
2586 return result;
2587 }
2588 case TREE_VEC:
5566b478
MS
2589 if (type != NULL_TREE)
2590 {
85b71cf2
JM
2591 /* A binfo node. */
2592
5566b478
MS
2593 t = copy_node (t);
2594
2595 if (type == TREE_TYPE (t))
2596 return t;
2597
2598 TREE_TYPE (t) = complete_type (type);
6633d636
MS
2599 if (IS_AGGR_TYPE (type))
2600 {
2601 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
2602 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
2603 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
2604 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
2605 }
5566b478
MS
2606 return t;
2607 }
85b71cf2
JM
2608
2609 /* Otherwise, a vector of template arguments. */
8d08fdba
MS
2610 {
2611 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
2612 tree *elts = (tree *) alloca (len * sizeof (tree));
5566b478 2613
1daa5dd8 2614 bzero ((char *) elts, len * sizeof (tree));
8d08fdba
MS
2615
2616 for (i = 0; i < len; i++)
2617 {
ec255269 2618 elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
85b71cf2
JM
2619
2620 if (TREE_CODE_CLASS (TREE_CODE (elts[i])) != 't'
2621 && !uses_template_parms (elts[i]))
2622 {
2623 /* Sometimes, one of the args was an expression involving a
2624 template constant parameter, like N - 1. Now that we've
2625 tsubst'd, we might have something like 2 - 1. This will
2626 confuse lookup_template_class, so we do constant folding
2627 here. We have to unset processing_template_decl, to
2628 fool build_expr_from_tree() into building an actual
2629 tree. */
2630
2631 int saved_processing_template_decl = processing_template_decl;
2632 processing_template_decl = 0;
2633 elts[i] = fold (build_expr_from_tree (elts[i]));
2634 processing_template_decl = saved_processing_template_decl;
2635 }
2636
8d08fdba
MS
2637 if (elts[i] != TREE_VEC_ELT (t, i))
2638 need_new = 1;
2639 }
2640
2641 if (!need_new)
2642 return t;
2643
2644 t = make_tree_vec (len);
2645 for (i = 0; i < len; i++)
2646 TREE_VEC_ELT (t, i) = elts[i];
5566b478 2647
8d08fdba
MS
2648 return t;
2649 }
2650 case POINTER_TYPE:
2651 case REFERENCE_TYPE:
2652 {
2653 tree r;
2654 enum tree_code code;
2655 if (type == TREE_TYPE (t))
2656 return t;
2657
2658 code = TREE_CODE (t);
2659 if (code == POINTER_TYPE)
2660 r = build_pointer_type (type);
2661 else
2662 r = build_reference_type (type);
f376e137 2663 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
8d08fdba
MS
2664 /* Will this ever be needed for TYPE_..._TO values? */
2665 layout_type (r);
2666 return r;
2667 }
a4443a08
MS
2668 case OFFSET_TYPE:
2669 return build_offset_type
2670 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
8d08fdba
MS
2671 case FUNCTION_TYPE:
2672 case METHOD_TYPE:
2673 {
75b0bbce 2674 tree values = TYPE_ARG_TYPES (t);
8d08fdba 2675 tree context = TYPE_CONTEXT (t);
c11b6f21
MS
2676 tree raises = TYPE_RAISES_EXCEPTIONS (t);
2677 tree fntype;
8d08fdba
MS
2678
2679 /* Don't bother recursing if we know it won't change anything. */
2680 if (values != void_list_node)
5566b478
MS
2681 {
2682 /* This should probably be rewritten to use hash_tree_cons for
2683 the memory savings. */
2684 tree first = NULL_TREE;
2685 tree last;
2686
2687 for (; values && values != void_list_node;
2688 values = TREE_CHAIN (values))
2689 {
f7da6097
MS
2690 tree value = TYPE_MAIN_VARIANT (type_decays_to
2691 (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
de22184b
MS
2692 /* Don't instantiate default args unless they are used.
2693 Handle it in build_over_call instead. */
2694 tree purpose = TREE_PURPOSE (values);
5566b478
MS
2695 tree x = build_tree_list (purpose, value);
2696
2697 if (first)
2698 TREE_CHAIN (last) = x;
2699 else
2700 first = x;
2701 last = x;
2702 }
2703
2704 if (values == void_list_node)
2705 TREE_CHAIN (last) = void_list_node;
2706
2707 values = first;
2708 }
8d08fdba
MS
2709 if (context)
2710 context = tsubst (context, args, nargs, in_decl);
2711 /* Could also optimize cases where return value and
2712 values have common elements (e.g., T min(const &T, const T&). */
2713
2714 /* If the above parameters haven't changed, just return the type. */
2715 if (type == TREE_TYPE (t)
2716 && values == TYPE_VALUES (t)
2717 && context == TYPE_CONTEXT (t))
2718 return t;
2719
2720 /* Construct a new type node and return it. */
2721 if (TREE_CODE (t) == FUNCTION_TYPE
2722 && context == NULL_TREE)
2723 {
c11b6f21 2724 fntype = build_function_type (type, values);
8d08fdba
MS
2725 }
2726 else if (context == NULL_TREE)
2727 {
2728 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
2729 args, nargs, in_decl);
c11b6f21
MS
2730 fntype = build_cplus_method_type (base, type,
2731 TREE_CHAIN (values));
8d08fdba
MS
2732 }
2733 else
2734 {
c11b6f21
MS
2735 fntype = make_node (TREE_CODE (t));
2736 TREE_TYPE (fntype) = type;
2737 TYPE_CONTEXT (fntype) = context;
2738 TYPE_VALUES (fntype) = values;
2739 TYPE_SIZE (fntype) = TYPE_SIZE (t);
2740 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
2741 TYPE_MODE (fntype) = TYPE_MODE (t);
8d08fdba 2742 if (TYPE_METHOD_BASETYPE (t))
c11b6f21
MS
2743 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
2744 args, nargs, in_decl);
8d08fdba
MS
2745 /* Need to generate hash value. */
2746 my_friendly_abort (84);
2747 }
c11b6f21
MS
2748 fntype = build_type_variant (fntype,
2749 TYPE_READONLY (t),
2750 TYPE_VOLATILE (t));
2751 if (raises)
2752 {
2753 raises = tsubst (raises, args, nargs, in_decl);
2754 fntype = build_exception_variant (fntype, raises);
2755 }
2756 return fntype;
8d08fdba
MS
2757 }
2758 case ARRAY_TYPE:
2759 {
2760 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
2761 tree r;
2762 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
2763 return t;
2764 r = build_cplus_array_type (type, domain);
2765 return r;
2766 }
2767
8d08fdba 2768 case PLUS_EXPR:
5566b478 2769 case MINUS_EXPR:
8d08fdba
MS
2770 return fold (build (TREE_CODE (t), TREE_TYPE (t),
2771 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2772 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
2773
2774 case NEGATE_EXPR:
2775 case NOP_EXPR:
2776 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
2777 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
2778
5566b478
MS
2779 case TYPENAME_TYPE:
2780 {
2781 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2782 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
2783 return cp_build_type_variant
2784 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
2785 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
2786 }
2787
2788 case INDIRECT_REF:
2789 return make_pointer_declarator
2790 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2791
2792 case ADDR_EXPR:
2793 return make_reference_declarator
2794 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2795
2796 case ARRAY_REF:
2797 return build_parse_node
2798 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
e76a2646 2799 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
5566b478
MS
2800
2801 case CALL_EXPR:
c11b6f21
MS
2802 return make_call_declarator
2803 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2804 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2805 TREE_OPERAND (t, 2),
2806 tsubst (TREE_TYPE (t), args, nargs, in_decl));
5566b478 2807
fc378698
MS
2808 case SCOPE_REF:
2809 return build_parse_node
2810 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2811 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
2812
8d08fdba 2813 default:
5566b478 2814 sorry ("use of `%s' in template",
8d08fdba
MS
2815 tree_code_name [(int) TREE_CODE (t)]);
2816 return error_mark_node;
2817 }
2818}
2819
5566b478
MS
2820void
2821do_pushlevel ()
2822{
2823 emit_line_note (input_filename, lineno);
2824 pushlevel (0);
2825 clear_last_expr ();
2826 push_momentary ();
2827 expand_start_bindings (0);
2828}
2829
8d08fdba 2830tree
5566b478 2831do_poplevel ()
8d08fdba 2832{
5566b478 2833 tree t;
85b71cf2 2834 int saved_warn_unused;
8d08fdba 2835
85b71cf2
JM
2836 if (processing_template_decl)
2837 {
2838 saved_warn_unused = warn_unused;
2839 warn_unused = 0;
2840 }
5566b478 2841 expand_end_bindings (getdecls (), kept_level_p (), 1);
85b71cf2
JM
2842 if (processing_template_decl)
2843 warn_unused = saved_warn_unused;
5566b478
MS
2844 t = poplevel (kept_level_p (), 1, 0);
2845 pop_momentary ();
2846 return t;
2847}
8d08fdba 2848
5566b478
MS
2849tree
2850tsubst_copy (t, args, nargs, in_decl)
98c1c668 2851 tree t, args;
5566b478
MS
2852 int nargs;
2853 tree in_decl;
2854{
2855 enum tree_code code;
8d08fdba 2856
5566b478
MS
2857 if (t == NULL_TREE || t == error_mark_node)
2858 return t;
2859
2860 code = TREE_CODE (t);
b7484fbe 2861
5566b478
MS
2862 switch (code)
2863 {
2864 case PARM_DECL:
2865 return do_identifier (DECL_NAME (t), 0);
2866
2867 case CONST_DECL:
2868 case FIELD_DECL:
2869 if (DECL_CONTEXT (t))
2870 {
2871 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
b87692e5
MS
2872 if (ctx == current_function_decl)
2873 return lookup_name (DECL_NAME (t), 0);
2874 else if (ctx != DECL_CONTEXT (t))
5566b478
MS
2875 return lookup_field (ctx, DECL_NAME (t), 0, 0);
2876 }
2877 return t;
2878
2879 case VAR_DECL:
2880 case FUNCTION_DECL:
2881 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2882 t = tsubst (t, args, nargs, in_decl);
2883 mark_used (t);
2884 return t;
2885
98c1c668
JM
2886 case TEMPLATE_DECL:
2887 if (is_member_template (t))
2888 return tsubst (t, args, nargs, in_decl);
2889 else
2890 return t;
2891
5566b478
MS
2892#if 0
2893 case IDENTIFIER_NODE:
2894 return do_identifier (t, 0);
2895#endif
2896
2897 case CAST_EXPR:
2898 case REINTERPRET_CAST_EXPR:
e92cc029
MS
2899 case CONST_CAST_EXPR:
2900 case STATIC_CAST_EXPR:
2901 case DYNAMIC_CAST_EXPR:
5566b478
MS
2902 return build1
2903 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2904 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2905
2906 case INDIRECT_REF:
2907 case PREDECREMENT_EXPR:
2908 case PREINCREMENT_EXPR:
2909 case POSTDECREMENT_EXPR:
2910 case POSTINCREMENT_EXPR:
2911 case NEGATE_EXPR:
2912 case TRUTH_NOT_EXPR:
b87692e5 2913 case BIT_NOT_EXPR:
5566b478
MS
2914 case ADDR_EXPR:
2915 case CONVERT_EXPR: /* Unary + */
2916 case SIZEOF_EXPR:
2917 case ARROW_EXPR:
fc378698 2918 case THROW_EXPR:
5156628f 2919 case TYPEID_EXPR:
5566b478
MS
2920 return build1
2921 (code, NULL_TREE,
2922 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2923
2924 case PLUS_EXPR:
2925 case MINUS_EXPR:
2926 case MULT_EXPR:
2927 case TRUNC_DIV_EXPR:
2928 case CEIL_DIV_EXPR:
2929 case FLOOR_DIV_EXPR:
2930 case ROUND_DIV_EXPR:
2931 case EXACT_DIV_EXPR:
2932 case BIT_AND_EXPR:
2933 case BIT_ANDTC_EXPR:
2934 case BIT_IOR_EXPR:
2935 case BIT_XOR_EXPR:
2936 case TRUNC_MOD_EXPR:
2937 case FLOOR_MOD_EXPR:
2938 case TRUTH_ANDIF_EXPR:
2939 case TRUTH_ORIF_EXPR:
2940 case TRUTH_AND_EXPR:
2941 case TRUTH_OR_EXPR:
2942 case RSHIFT_EXPR:
2943 case LSHIFT_EXPR:
2944 case RROTATE_EXPR:
2945 case LROTATE_EXPR:
2946 case EQ_EXPR:
2947 case NE_EXPR:
2948 case MAX_EXPR:
2949 case MIN_EXPR:
2950 case LE_EXPR:
2951 case GE_EXPR:
2952 case LT_EXPR:
2953 case GT_EXPR:
2954 case COMPONENT_REF:
2955 case ARRAY_REF:
2956 case COMPOUND_EXPR:
2957 case SCOPE_REF:
2958 case DOTSTAR_EXPR:
2959 case MEMBER_REF:
2960 return build_nt
2961 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2962 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2963
2964 case CALL_EXPR:
2965 {
2966 tree fn = TREE_OPERAND (t, 0);
2967 if (really_overloaded_fn (fn))
2968 fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
2969 else
2970 fn = tsubst_copy (fn, args, nargs, in_decl);
2971 return build_nt
2972 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2973 NULL_TREE);
2974 }
2975
2976 case METHOD_CALL_EXPR:
2977 {
2978 tree name = TREE_OPERAND (t, 0);
2979 if (TREE_CODE (name) == BIT_NOT_EXPR)
2980 {
2981 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
fc378698 2982 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
5566b478
MS
2983 }
2984 else if (TREE_CODE (name) == SCOPE_REF
2985 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
2986 {
2987 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2988 name = TREE_OPERAND (name, 1);
2989 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
fc378698 2990 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
5566b478
MS
2991 name = build_nt (SCOPE_REF, base, name);
2992 }
2993 else
2994 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
2995 return build_nt
2996 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2997 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
2998 NULL_TREE);
2999 }
3000
3001 case COND_EXPR:
3002 case MODOP_EXPR:
3003 return build_nt
3004 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3005 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3006 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3007
3008 case NEW_EXPR:
3009 {
3010 tree r = build_nt
3011 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3012 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3013 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3014 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
3015 return r;
3016 }
3017
3018 case DELETE_EXPR:
3019 {
3020 tree r = build_nt
3021 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3022 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
3023 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
3024 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
3025 return r;
3026 }
3027
386b8a85
JM
3028 case TEMPLATE_ID_EXPR:
3029 {
3030 tree r = lookup_template_function
3031 (tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3032 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
3033 return r;
3034 }
3035
5566b478
MS
3036 case TREE_LIST:
3037 {
3038 tree purpose, value, chain;
3039
3040 if (t == void_list_node)
3041 return t;
3042
3043 purpose = TREE_PURPOSE (t);
3044 if (purpose)
3045 purpose = tsubst_copy (purpose, args, nargs, in_decl);
3046 value = TREE_VALUE (t);
3047 if (value)
3048 value = tsubst_copy (value, args, nargs, in_decl);
3049 chain = TREE_CHAIN (t);
3050 if (chain && chain != void_type_node)
3051 chain = tsubst_copy (chain, args, nargs, in_decl);
3052 if (purpose == TREE_PURPOSE (t)
3053 && value == TREE_VALUE (t)
3054 && chain == TREE_CHAIN (t))
3055 return t;
3056 return tree_cons (purpose, value, chain);
3057 }
3058
3059 case RECORD_TYPE:
3060 case UNION_TYPE:
3061 case ENUMERAL_TYPE:
3062 case INTEGER_TYPE:
3063 case TEMPLATE_TYPE_PARM:
3064 case TEMPLATE_CONST_PARM:
3065 case POINTER_TYPE:
3066 case REFERENCE_TYPE:
3067 case OFFSET_TYPE:
3068 case FUNCTION_TYPE:
3069 case METHOD_TYPE:
3070 case ARRAY_TYPE:
3071 case TYPENAME_TYPE:
3072 return tsubst (t, args, nargs, in_decl);
3073
e92cc029
MS
3074 case IDENTIFIER_NODE:
3075 if (IDENTIFIER_TYPENAME_P (t))
3076 return build_typename_overload
3077 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
3078 else
3079 return t;
3080
5156628f
MS
3081 case CONSTRUCTOR:
3082 return build
3083 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
3084 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
3085
5566b478
MS
3086 default:
3087 return t;
3088 }
3089}
3090
3091tree
3092tsubst_expr (t, args, nargs, in_decl)
98c1c668 3093 tree t, args;
5566b478
MS
3094 int nargs;
3095 tree in_decl;
3096{
3097 if (t == NULL_TREE || t == error_mark_node)
3098 return t;
3099
5156628f 3100 if (processing_template_decl)
5566b478
MS
3101 return tsubst_copy (t, args, nargs, in_decl);
3102
3103 switch (TREE_CODE (t))
8d08fdba 3104 {
5566b478
MS
3105 case RETURN_STMT:
3106 lineno = TREE_COMPLEXITY (t);
3107 emit_line_note (input_filename, lineno);
3108 c_expand_return
3109 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3110 finish_stmt ();
3111 break;
3112
3113 case EXPR_STMT:
3114 lineno = TREE_COMPLEXITY (t);
3115 emit_line_note (input_filename, lineno);
3116 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3117 /* Do default conversion if safe and possibly important,
3118 in case within ({...}). */
3119 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
3120 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
3121 t = default_conversion (t);
3122 cplus_expand_expr_stmt (t);
3123 clear_momentary ();
3124 finish_stmt ();
3125 break;
3126
3127 case DECL_STMT:
3128 {
3129 int i = suspend_momentary ();
67d743fe 3130 tree dcl, init;
5566b478
MS
3131
3132 lineno = TREE_COMPLEXITY (t);
3133 emit_line_note (input_filename, lineno);
3134 dcl = start_decl
3135 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
3136 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
c11b6f21
MS
3137 TREE_OPERAND (t, 2) != 0);
3138 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
5566b478 3139 cp_finish_decl
a0128b67 3140 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5566b478
MS
3141 resume_momentary (i);
3142 return dcl;
3143 }
8d08fdba 3144
5566b478
MS
3145 case FOR_STMT:
3146 {
3147 tree tmp;
3148 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
3149 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3150 int cond_scope = (TREE_OPERAND (t, 1)
3151 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
3152
3153 lineno = TREE_COMPLEXITY (t);
3154 emit_line_note (input_filename, lineno);
3155 if (init_scope)
3156 do_pushlevel ();
e76a2646
MS
3157 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
3158 tsubst_expr (tmp, args, nargs, in_decl);
5566b478
MS
3159 emit_nop ();
3160 emit_line_note (input_filename, lineno);
3161 expand_start_loop_continue_elsewhere (1);
3162
3163 if (cond_scope)
3164 do_pushlevel ();
3165 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3166 emit_line_note (input_filename, lineno);
3167 if (tmp)
3168 expand_exit_loop_if_false (0, condition_conversion (tmp));
3169
3170 if (! cond_scope)
3171 do_pushlevel ();
3172 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
3173 do_poplevel ();
3174
3175 emit_line_note (input_filename, lineno);
3176 expand_loop_continue_here ();
3177 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3178 if (tmp)
3179 cplus_expand_expr_stmt (tmp);
3180
3181 expand_end_loop ();
3182 if (init_scope)
3183 do_poplevel ();
3184 finish_stmt ();
3185 }
3186 break;
8d08fdba 3187
5566b478
MS
3188 case WHILE_STMT:
3189 {
3190 tree cond;
3191
3192 lineno = TREE_COMPLEXITY (t);
3193 emit_nop ();
3194 emit_line_note (input_filename, lineno);
3195 expand_start_loop (1);
3196
3197 cond = TREE_OPERAND (t, 0);
3198 if (TREE_CODE (cond) == DECL_STMT)
3199 do_pushlevel ();
3200 cond = tsubst_expr (cond, args, nargs, in_decl);
3201 emit_line_note (input_filename, lineno);
3202 expand_exit_loop_if_false (0, condition_conversion (cond));
3203
3204 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
3205 do_pushlevel ();
3206 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3207 do_poplevel ();
3208
3209 expand_end_loop ();
3210 finish_stmt ();
3211 }
3212 break;
8d08fdba 3213
5566b478
MS
3214 case DO_STMT:
3215 {
3216 tree cond;
8d08fdba 3217
5566b478
MS
3218 lineno = TREE_COMPLEXITY (t);
3219 emit_nop ();
3220 emit_line_note (input_filename, lineno);
3221 expand_start_loop_continue_elsewhere (1);
8d08fdba 3222
5566b478
MS
3223 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3224 expand_loop_continue_here ();
f0e01782 3225
5566b478
MS
3226 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3227 emit_line_note (input_filename, lineno);
3228 expand_exit_loop_if_false (0, condition_conversion (cond));
3229 expand_end_loop ();
28cbf42c 3230
5566b478
MS
3231 clear_momentary ();
3232 finish_stmt ();
3233 }
3234 break;
a0a33927 3235
5566b478 3236 case IF_STMT:
8d08fdba 3237 {
5566b478
MS
3238 tree tmp;
3239 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3240
3241 lineno = TREE_COMPLEXITY (t);
3242 if (cond_scope)
3243 do_pushlevel ();
3244 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3245 emit_line_note (input_filename, lineno);
3246 expand_start_cond (condition_conversion (tmp), 0);
3247
3248 if (tmp = TREE_OPERAND (t, 1), tmp)
3249 tsubst_expr (tmp, args, nargs, in_decl);
db5ae43f 3250
5566b478 3251 if (tmp = TREE_OPERAND (t, 2), tmp)
db5ae43f 3252 {
5566b478
MS
3253 expand_start_else ();
3254 tsubst_expr (tmp, args, nargs, in_decl);
db5ae43f
MS
3255 }
3256
5566b478 3257 expand_end_cond ();
8d08fdba 3258
5566b478
MS
3259 if (cond_scope)
3260 do_poplevel ();
8d08fdba 3261
5566b478 3262 finish_stmt ();
8d08fdba 3263 }
5566b478 3264 break;
8d08fdba 3265
5566b478
MS
3266 case COMPOUND_STMT:
3267 {
3268 tree substmt = TREE_OPERAND (t, 0);
8d08fdba 3269
5566b478 3270 lineno = TREE_COMPLEXITY (t);
8d08fdba 3271
5566b478
MS
3272 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3273 do_pushlevel ();
8d08fdba 3274
5566b478
MS
3275 for (; substmt; substmt = TREE_CHAIN (substmt))
3276 tsubst_expr (substmt, args, nargs, in_decl);
8d08fdba 3277
5566b478
MS
3278 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3279 do_poplevel ();
3280 }
3281 break;
8d08fdba 3282
5566b478
MS
3283 case BREAK_STMT:
3284 lineno = TREE_COMPLEXITY (t);
3285 emit_line_note (input_filename, lineno);
3286 if (! expand_exit_something ())
3287 error ("break statement not within loop or switch");
3288 break;
8d08fdba 3289
6467930b
MS
3290 case CONTINUE_STMT:
3291 lineno = TREE_COMPLEXITY (t);
3292 emit_line_note (input_filename, lineno);
3293 if (! expand_continue_loop (0))
3294 error ("continue statement not within a loop");
3295 break;
3296
5566b478
MS
3297 case SWITCH_STMT:
3298 {
3299 tree val, tmp;
3300 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3301
3302 lineno = TREE_COMPLEXITY (t);
3303 if (cond_scope)
3304 do_pushlevel ();
3305 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3306 emit_line_note (input_filename, lineno);
3307 c_expand_start_case (val);
3308 push_switch ();
3309
3310 if (tmp = TREE_OPERAND (t, 1), tmp)
3311 tsubst_expr (tmp, args, nargs, in_decl);
8d08fdba 3312
5566b478
MS
3313 expand_end_case (val);
3314 pop_switch ();
8d08fdba 3315
5566b478
MS
3316 if (cond_scope)
3317 do_poplevel ();
8d08fdba 3318
5566b478
MS
3319 finish_stmt ();
3320 }
3321 break;
3322
3323 case CASE_LABEL:
3324 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
3325 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
3326 break;
3327
3328 case LABEL_DECL:
3329 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
3330 DECL_NAME (t));
3331 if (t)
3332 expand_label (t);
3333 break;
3334
3335 case GOTO_STMT:
3336 lineno = TREE_COMPLEXITY (t);
3337 emit_line_note (input_filename, lineno);
3338 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
3339 {
3340 tree decl = lookup_label (TREE_OPERAND (t, 0));
3341 TREE_USED (decl) = 1;
3342 expand_goto (decl);
3343 }
3344 else
3345 expand_computed_goto
3346 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3347 break;
faf5394a
MS
3348
3349 case TRY_BLOCK:
3350 lineno = TREE_COMPLEXITY (t);
3351 emit_line_note (input_filename, lineno);
3352 expand_start_try_stmts ();
3353 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3354 expand_start_all_catch ();
3355 {
3356 tree handler = TREE_OPERAND (t, 1);
3357 for (; handler; handler = TREE_CHAIN (handler))
3358 tsubst_expr (handler, args, nargs, in_decl);
3359 }
3360 expand_end_all_catch ();
3361 break;
3362
3363 case HANDLER:
3364 lineno = TREE_COMPLEXITY (t);
3365 do_pushlevel ();
3366 if (TREE_OPERAND (t, 0))
3367 {
3368 tree d = TREE_OPERAND (t, 0);
3369 expand_start_catch_block
3370 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
3371 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
3372 }
3373 else
3374 expand_start_catch_block (NULL_TREE, NULL_TREE);
3375 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3376 expand_end_catch_block ();
3377 do_poplevel ();
3378 break;
3379
b87692e5
MS
3380 case TAG_DEFN:
3381 lineno = TREE_COMPLEXITY (t);
3382 t = TREE_TYPE (t);
3383 if (TREE_CODE (t) == ENUMERAL_TYPE)
b3d5a58b 3384 tsubst_enum (t, args, nargs, NULL);
b87692e5
MS
3385 break;
3386
5566b478
MS
3387 default:
3388 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
3389 }
3390 return NULL_TREE;
8d08fdba
MS
3391}
3392
5566b478
MS
3393tree
3394instantiate_template (tmpl, targ_ptr)
98c1c668 3395 tree tmpl, targ_ptr;
8d08fdba 3396{
5566b478
MS
3397 tree fndecl;
3398 int i, len;
3399 struct obstack *old_fmp_obstack;
3400 extern struct obstack *function_maybepermanent_obstack;
3401
386b8a85
JM
3402 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
3403
3404 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3405 {
3406 tree specs;
3407
3408 /* Check to see if there is a matching specialization. */
3409 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3410 specs != NULL_TREE;
3411 specs = TREE_CHAIN (specs))
3412 if (comp_template_args (TREE_PURPOSE(specs),
3413 targ_ptr))
3414 return TREE_VALUE (specs);
3415 }
3416
5566b478
MS
3417 push_obstacks (&permanent_obstack, &permanent_obstack);
3418 old_fmp_obstack = function_maybepermanent_obstack;
3419 function_maybepermanent_obstack = &permanent_obstack;
8d08fdba 3420
98c1c668 3421 len = DECL_NTPARMS (tmpl);
8d08fdba 3422
5566b478
MS
3423 i = len;
3424 while (i--)
8d08fdba 3425 {
98c1c668 3426 tree t = TREE_VEC_ELT (targ_ptr, i);
5566b478
MS
3427 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3428 {
3429 tree nt = target_type (t);
ec255269 3430 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5566b478
MS
3431 {
3432 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
3433 cp_error (" trying to instantiate `%D'", tmpl);
3434 fndecl = error_mark_node;
3435 goto out;
3436 }
3437 }
98c1c668 3438 TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
8d08fdba
MS
3439 }
3440
98c1c668
JM
3441 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
3442 targ_ptr = add_to_template_args (DECL_TI_ARGS (tmpl), targ_ptr);
3443
5566b478
MS
3444 /* substitute template parameters */
3445 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
8d08fdba 3446
824b9a4c
MS
3447 if (flag_external_templates)
3448 add_pending_template (fndecl);
3449
5566b478
MS
3450 out:
3451 function_maybepermanent_obstack = old_fmp_obstack;
3452 pop_obstacks ();
8d08fdba 3453
5566b478 3454 return fndecl;
8d08fdba 3455}
5566b478
MS
3456
3457/* Push the name of the class template into the scope of the instantiation. */
8d08fdba
MS
3458
3459void
5566b478
MS
3460overload_template_name (type)
3461 tree type;
8d08fdba 3462{
5566b478
MS
3463 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
3464 tree decl;
8d08fdba 3465
5566b478
MS
3466 if (IDENTIFIER_CLASS_VALUE (id)
3467 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
3468 return;
8d08fdba 3469
5566b478
MS
3470 decl = build_decl (TYPE_DECL, id, type);
3471 SET_DECL_ARTIFICIAL (decl);
3472 pushdecl_class_level (decl);
8d08fdba
MS
3473}
3474
98c1c668
JM
3475/* Like type_unfication but designed specially to handle conversion
3476 operators. */
3477
3478int
386b8a85
JM
3479fn_type_unification (fn, explicit_targs, targs, args, return_type, strict)
3480 tree fn, explicit_targs, targs, args, return_type;
98c1c668
JM
3481 int strict;
3482{
3483 int i, dummy = 0;
3484 tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
3485 tree decl_arg_types = args;
3486
3487 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
3488
3489 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn)))
3490 {
3491 /* This is a template conversion operator. Use the return types
3492 as well as the argument types. */
3493 fn_arg_types = tree_cons (NULL_TREE,
3494 TREE_TYPE (TREE_TYPE (fn)),
3495 fn_arg_types);
3496 decl_arg_types = tree_cons (NULL_TREE,
3497 return_type,
3498 decl_arg_types);
3499 }
3500
3501 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
3502 &TREE_VEC_ELT (targs, 0),
3503 fn_arg_types,
3504 decl_arg_types,
386b8a85
JM
3505 explicit_targs,
3506 &dummy, strict, 0);
98c1c668
JM
3507
3508 return i;
3509}
3510
3511
8d08fdba
MS
3512/* Type unification.
3513
3514 We have a function template signature with one or more references to
3515 template parameters, and a parameter list we wish to fit to this
3516 template. If possible, produce a list of parameters for the template
3517 which will cause it to fit the supplied parameter list.
3518
3519 Return zero for success, 2 for an incomplete match that doesn't resolve
3520 all the types, and 1 for complete failure. An error message will be
3521 printed only for an incomplete match.
3522
3523 TPARMS[NTPARMS] is an array of template parameter types;
3524 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3525 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3526 and ARGS is the argument list we're trying to match against it.
3527
3528 If SUBR is 1, we're being called recursively (to unify the arguments of
3529 a function or method parameter of a function template), so don't zero
6467930b
MS
3530 out targs and don't fail on an incomplete match.
3531
3532 If STRICT is 1, the match must be exact (for casts of overloaded
3533 addresses, explicit instantiation, and more_specialized). */
8d08fdba
MS
3534
3535int
386b8a85
JM
3536type_unification (tparms, targs, parms, args, targs_in, nsubsts,
3537 strict, allow_incomplete)
3538 tree tparms, *targs, parms, args, targs_in;
3539 int *nsubsts, strict, allow_incomplete;
3540{
3541 int ntparms = TREE_VEC_LENGTH (tparms);
3542 tree t;
3543 int i;
3544 int r;
3545
3546 bzero ((char *) targs, sizeof (tree) * ntparms);
3547
3548 /* Insert any explicit template arguments. They are encoded as the
3549 operands of NOP_EXPRs so that unify can tell that they are
3550 explicit arguments. */
3551 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3552 targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VALUE (t));
3553
3554 r = type_unification_real (tparms, targs, parms, args, nsubsts, 0,
3555 strict, allow_incomplete);
3556
3557 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3558 if (TREE_CODE (targs[i]) == NOP_EXPR)
3559 targs[i] = TREE_OPERAND (targs[i], 0);
3560
3561 return r;
3562}
3563
3564
3565int
3566type_unification_real (tparms, targs, parms, args, nsubsts, subr,
3567 strict, allow_incomplete)
8d08fdba 3568 tree tparms, *targs, parms, args;
386b8a85 3569 int *nsubsts, subr, strict, allow_incomplete;
8d08fdba
MS
3570{
3571 tree parm, arg;
3572 int i;
3573 int ntparms = TREE_VEC_LENGTH (tparms);
3574
3575 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
386b8a85
JM
3576 my_friendly_assert (parms == NULL_TREE
3577 || TREE_CODE (parms) == TREE_LIST, 290);
51c184be 3578 /* ARGS could be NULL (via a call from parse.y to
8d08fdba
MS
3579 build_x_function_call). */
3580 if (args)
3581 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
3582 my_friendly_assert (ntparms > 0, 292);
3583
8d08fdba
MS
3584 while (parms
3585 && parms != void_list_node
3586 && args
3587 && args != void_list_node)
3588 {
3589 parm = TREE_VALUE (parms);
3590 parms = TREE_CHAIN (parms);
3591 arg = TREE_VALUE (args);
3592 args = TREE_CHAIN (args);
3593
3594 if (arg == error_mark_node)
3595 return 1;
3596 if (arg == unknown_type_node)
3597 return 1;
b7484fbe 3598
03e70705
JM
3599 /* Conversions will be performed on a function argument that
3600 corresponds with a function parameter that contains only
3601 non-deducible template parameters and explicitly specified
3602 template parameters. */
3603 if (! uses_template_parms (parm))
b7484fbe 3604 {
03e70705
JM
3605 tree type;
3606
3607 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3608 type = TREE_TYPE (arg);
3609 else
3610 {
3611 type = arg;
3612 arg = NULL_TREE;
3613 }
3614
3615 if (strict)
3616 {
3617 if (comptypes (parm, type, 1))
3618 continue;
3619 }
3620 else if (arg)
3621 {
3622 if (can_convert_arg (parm, type, arg))
3623 continue;
3624 }
3625 else
3626 {
3627 if (can_convert (parm, type))
3628 continue;
3629 }
3630
b7484fbe
MS
3631 return 1;
3632 }
3633
8d08fdba
MS
3634#if 0
3635 if (TREE_CODE (arg) == VAR_DECL)
3636 arg = TREE_TYPE (arg);
3637 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
3638 arg = TREE_TYPE (arg);
3639#else
3640 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3641 {
3642 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
28cbf42c
MS
3643 if (TREE_CODE (arg) == TREE_LIST
3644 && TREE_TYPE (arg) == unknown_type_node
3645 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
3646 {
3647 int nsubsts, ntparms;
3648 tree *targs;
3649
3650 /* Have to back unify here */
3651 arg = TREE_VALUE (arg);
3652 nsubsts = 0;
98c1c668 3653 ntparms = DECL_NTPARMS (arg);
28cbf42c
MS
3654 targs = (tree *) alloca (sizeof (tree) * ntparms);
3655 parm = tree_cons (NULL_TREE, parm, NULL_TREE);
386b8a85
JM
3656 return
3657 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg),
3658 targs,
3659 TYPE_ARG_TYPES (TREE_TYPE (arg)),
3660 parm, NULL_TREE, &nsubsts, strict,
3661 allow_incomplete);
28cbf42c 3662 }
8d08fdba
MS
3663 arg = TREE_TYPE (arg);
3664 }
3665#endif
7834ab39 3666 if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
db5ae43f
MS
3667 arg = TREE_TYPE (arg);
3668
7834ab39 3669 if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
4cabb798
JM
3670 {
3671 if (TREE_CODE (arg) == FUNCTION_TYPE
3672 || TREE_CODE (arg) == METHOD_TYPE)
3673 arg = build_pointer_type (arg);
3674 else if (TREE_CODE (arg) == ARRAY_TYPE)
3675 arg = build_pointer_type (TREE_TYPE (arg));
3676 else
3677 arg = TYPE_MAIN_VARIANT (arg);
3678 }
8d08fdba 3679
6467930b 3680 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
8d08fdba
MS
3681 {
3682 case 0:
3683 break;
3684 case 1:
3685 return 1;
3686 }
3687 }
3688 /* Fail if we've reached the end of the parm list, and more args
3689 are present, and the parm list isn't variadic. */
3690 if (args && args != void_list_node && parms == void_list_node)
3691 return 1;
3692 /* Fail if parms are left and they don't have default values. */
3693 if (parms
3694 && parms != void_list_node
3695 && TREE_PURPOSE (parms) == NULL_TREE)
3696 return 1;
3697 if (!subr)
3698 for (i = 0; i < ntparms; i++)
3699 if (!targs[i])
3700 {
386b8a85
JM
3701 if (!allow_incomplete)
3702 error ("incomplete type unification");
8d08fdba
MS
3703 return 2;
3704 }
3705 return 0;
3706}
3707
3708/* Tail recursion is your friend. */
e92cc029 3709
8d08fdba 3710static int
6467930b 3711unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
8d08fdba 3712 tree tparms, *targs, parm, arg;
6467930b 3713 int *nsubsts, ntparms, strict;
8d08fdba
MS
3714{
3715 int idx;
3716
3717 /* I don't think this will do the right thing with respect to types.
3718 But the only case I've seen it in so far has been array bounds, where
3719 signedness is the only information lost, and I think that will be
3720 okay. */
3721 while (TREE_CODE (parm) == NOP_EXPR)
3722 parm = TREE_OPERAND (parm, 0);
3723
3724 if (arg == error_mark_node)
3725 return 1;
3726 if (arg == unknown_type_node)
3727 return 1;
3728 if (arg == parm)
3729 return 0;
3730
8d08fdba
MS
3731 switch (TREE_CODE (parm))
3732 {
2ca340ae
JM
3733 case TYPENAME_TYPE:
3734 /* In a type which contains a nested-name-specifier, template
3735 argument values cannot be deduced for template parameters used
3736 within the nested-name-specifier. */
3737 return 0;
3738
8d08fdba
MS
3739 case TEMPLATE_TYPE_PARM:
3740 (*nsubsts)++;
8d08fdba 3741 idx = TEMPLATE_TYPE_IDX (parm);
386b8a85
JM
3742 /* Check for mixed types and values. */
3743 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
3744 return 1;
3745
3746 if (!strict && targs[idx] != NULL_TREE &&
3747 TREE_CODE (targs[idx]) == NOP_EXPR)
3748 /* An explicit template argument. Don't even try to match
3749 here; the overload resolution code will manage check to
3750 see whether the call is legal. */
3751 return 0;
3752
6467930b
MS
3753 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
3754 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
3755 return 1;
db5ae43f 3756#if 0
a292b002
MS
3757 /* Template type parameters cannot contain cv-quals; i.e.
3758 template <class T> void f (T& a, T& b) will not generate
3759 void f (const int& a, const int& b). */
3760 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
3761 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
3762 return 1;
3763 arg = TYPE_MAIN_VARIANT (arg);
db5ae43f
MS
3764#else
3765 {
3766 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
3767 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
3768 arg = cp_build_type_variant (arg, constp, volatilep);
3769 }
3770#endif
8d08fdba 3771 /* Simple cases: Value already set, does match or doesn't. */
386b8a85
JM
3772 if (targs[idx] == arg
3773 || (targs[idx]
3774 && TREE_CODE (targs[idx]) == NOP_EXPR
3775 && TREE_OPERAND (targs[idx], 0) == arg))
8d08fdba
MS
3776 return 0;
3777 else if (targs[idx])
8d08fdba
MS
3778 return 1;
3779 targs[idx] = arg;
3780 return 0;
3781 case TEMPLATE_CONST_PARM:
3782 (*nsubsts)++;
3783 idx = TEMPLATE_CONST_IDX (parm);
312e7d50 3784 if (targs[idx])
8d08fdba 3785 {
312e7d50
JM
3786 int i = cp_tree_equal (targs[idx], arg);
3787 if (i == 1)
3788 return 0;
3789 else if (i == 0)
3790 return 1;
3791 else
3792 my_friendly_abort (42);
8d08fdba 3793 }
8d08fdba
MS
3794
3795 targs[idx] = copy_to_permanent (arg);
3796 return 0;
3797
3798 case POINTER_TYPE:
4ac14744
MS
3799 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
3800 return unify (tparms, targs, ntparms, parm,
6467930b 3801 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
4ac14744 3802
8d08fdba
MS
3803 if (TREE_CODE (arg) != POINTER_TYPE)
3804 return 1;
3805 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
6467930b 3806 nsubsts, strict);
8d08fdba
MS
3807
3808 case REFERENCE_TYPE:
28cbf42c
MS
3809 if (TREE_CODE (arg) == REFERENCE_TYPE)
3810 arg = TREE_TYPE (arg);
6467930b
MS
3811 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
3812 nsubsts, strict);
8d08fdba
MS
3813
3814 case ARRAY_TYPE:
3815 if (TREE_CODE (arg) != ARRAY_TYPE)
3816 return 1;
3817 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
6467930b 3818 nsubsts, strict) != 0)
8d08fdba
MS
3819 return 1;
3820 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
6467930b 3821 nsubsts, strict);
8d08fdba
MS
3822
3823 case REAL_TYPE:
37c46b43 3824 case COMPLEX_TYPE:
8d08fdba 3825 case INTEGER_TYPE:
42976354 3826 case BOOLEAN_TYPE:
f376e137
MS
3827 if (TREE_CODE (arg) != TREE_CODE (parm))
3828 return 1;
3829
3830 if (TREE_CODE (parm) == INTEGER_TYPE)
8d08fdba
MS
3831 {
3832 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
6467930b
MS
3833 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
3834 TYPE_MIN_VALUE (arg), nsubsts, strict))
8d08fdba
MS
3835 return 1;
3836 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
6467930b
MS
3837 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
3838 TYPE_MAX_VALUE (arg), nsubsts, strict))
8d08fdba
MS
3839 return 1;
3840 }
ca79f85d
JM
3841 else if (TREE_CODE (parm) == REAL_TYPE
3842 && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
3843 return 1;
3844
8d08fdba
MS
3845 /* As far as unification is concerned, this wins. Later checks
3846 will invalidate it if necessary. */
3847 return 0;
3848
3849 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 3850 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 3851 case INTEGER_CST:
bd6dd845
MS
3852 while (TREE_CODE (arg) == NOP_EXPR)
3853 arg = TREE_OPERAND (arg, 0);
3854
8d08fdba
MS
3855 if (TREE_CODE (arg) != INTEGER_CST)
3856 return 1;
3857 return !tree_int_cst_equal (parm, arg);
3858
3859 case MINUS_EXPR:
3860 {
3861 tree t1, t2;
3862 t1 = TREE_OPERAND (parm, 0);
3863 t2 = TREE_OPERAND (parm, 1);
8d08fdba
MS
3864 return unify (tparms, targs, ntparms, t1,
3865 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
6467930b 3866 nsubsts, strict);
8d08fdba
MS
3867 }
3868
3869 case TREE_VEC:
3870 {
3871 int i;
3872 if (TREE_CODE (arg) != TREE_VEC)
3873 return 1;
3874 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
3875 return 1;
3876 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
3877 if (unify (tparms, targs, ntparms,
3878 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
6467930b 3879 nsubsts, strict))
8d08fdba
MS
3880 return 1;
3881 return 0;
3882 }
3883
8d08fdba 3884 case RECORD_TYPE:
db5ae43f 3885 if (TYPE_PTRMEMFUNC_FLAG (parm))
8d08fdba 3886 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
6467930b 3887 arg, nsubsts, strict);
8d08fdba 3888
a4443a08 3889 /* Allow trivial conversions. */
5566b478 3890 if (TREE_CODE (arg) != RECORD_TYPE
a4443a08
MS
3891 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
3892 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
3893 return 1;
5566b478 3894
6467930b 3895 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
5566b478 3896 {
6467930b 3897 tree t = NULL_TREE;
c73964b2 3898 if (flag_ansi_overloading && ! strict)
6467930b 3899 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
c73964b2
MS
3900 else if
3901 (CLASSTYPE_TEMPLATE_INFO (arg)
3902 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
6467930b
MS
3903 t = arg;
3904 if (! t || t == error_mark_node)
5566b478 3905 return 1;
6467930b 3906
5566b478 3907 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
6467930b 3908 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
5566b478
MS
3909 }
3910 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
3911 return 1;
a4443a08 3912 return 0;
8d08fdba
MS
3913
3914 case METHOD_TYPE:
3915 if (TREE_CODE (arg) != METHOD_TYPE)
3916 return 1;
3917 goto check_args;
3918
3919 case FUNCTION_TYPE:
3920 if (TREE_CODE (arg) != FUNCTION_TYPE)
3921 return 1;
3922 check_args:
28cbf42c 3923 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
6467930b 3924 TREE_TYPE (arg), nsubsts, strict))
28cbf42c 3925 return 1;
386b8a85
JM
3926 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3927 TYPE_ARG_TYPES (arg), nsubsts, 1,
3928 strict, 0);
a4443a08
MS
3929
3930 case OFFSET_TYPE:
3931 if (TREE_CODE (arg) != OFFSET_TYPE)
3932 return 1;
3933 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
6467930b 3934 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
a4443a08
MS
3935 return 1;
3936 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
6467930b 3937 TREE_TYPE (arg), nsubsts, strict);
a4443a08 3938
f62dbf03
JM
3939 case CONST_DECL:
3940 if (arg != decl_constant_value (parm))
3941 return 1;
3942 return 0;
3943
8d08fdba
MS
3944 default:
3945 sorry ("use of `%s' in template type unification",
3946 tree_code_name [(int) TREE_CODE (parm)]);
3947 return 1;
3948 }
3949}
8d08fdba 3950\f
faae18ab 3951void
5566b478 3952mark_decl_instantiated (result, extern_p)
faae18ab
MS
3953 tree result;
3954 int extern_p;
3955{
3956 if (DECL_TEMPLATE_INSTANTIATION (result))
3957 SET_DECL_EXPLICIT_INSTANTIATION (result);
3958 TREE_PUBLIC (result) = 1;
3959
3960 if (! extern_p)
3961 {
3962 DECL_INTERFACE_KNOWN (result) = 1;
3963 DECL_NOT_REALLY_EXTERN (result) = 1;
3964 }
f49422da
MS
3965 else if (TREE_CODE (result) == FUNCTION_DECL)
3966 mark_inline_for_output (result);
faae18ab
MS
3967}
3968
6467930b
MS
3969/* Given two function templates PAT1 and PAT2, return:
3970
3971 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
3972 -1 if PAT2 is more specialized than PAT1.
3973 0 if neither is more specialized. */
3974
3975int
3976more_specialized (pat1, pat2)
3977 tree pat1, pat2;
3978{
98c1c668 3979 tree targs;
73aad9b9 3980 int winner = 0;
6467930b 3981
73aad9b9
JM
3982 targs = get_bindings (pat1, pat2);
3983 if (targs)
3984 {
73aad9b9
JM
3985 --winner;
3986 }
6467930b 3987
73aad9b9
JM
3988 targs = get_bindings (pat2, pat1);
3989 if (targs)
3990 {
73aad9b9
JM
3991 ++winner;
3992 }
6467930b 3993
73aad9b9
JM
3994 return winner;
3995}
6467930b 3996
73aad9b9 3997/* Given two class template specialization list nodes PAT1 and PAT2, return:
6467930b 3998
73aad9b9
JM
3999 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4000 -1 if PAT2 is more specialized than PAT1.
4001 0 if neither is more specialized. */
4002
4003int
4004more_specialized_class (pat1, pat2)
4005 tree pat1, pat2;
4006{
4007 tree targs;
4008 int winner = 0;
4009
4010 targs = get_class_bindings
4011 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
4012 if (targs)
4013 --winner;
4014
4015 targs = get_class_bindings
4016 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
4017 if (targs)
6467930b
MS
4018 ++winner;
4019
4020 return winner;
4021}
73aad9b9
JM
4022
4023/* Return the template arguments that will produce the function signature
4024 DECL from the function template FN. */
4025
98c1c668 4026tree
73aad9b9
JM
4027get_bindings (fn, decl)
4028 tree fn, decl;
4029{
98c1c668
JM
4030 int ntparms = DECL_NTPARMS (fn);
4031 tree targs = make_tree_vec (ntparms);
4032 int i;
4033
386b8a85 4034 i = fn_type_unification (fn, NULL_TREE, targs,
98c1c668
JM
4035 TYPE_ARG_TYPES (TREE_TYPE (decl)),
4036 TREE_TYPE (TREE_TYPE (decl)),
4037 1);
4038
73aad9b9
JM
4039 if (i == 0)
4040 return targs;
73aad9b9
JM
4041 return 0;
4042}
4043
bd6dd845 4044static tree
73aad9b9
JM
4045get_class_bindings (tparms, parms, args)
4046 tree tparms, parms, args;
4047{
4048 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
4049 tree vec = make_temp_vec (ntparms);
4050
4051 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
4052 {
4053 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
4054 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
4055 &dummy, 1))
4056 {
4057 case 0:
4058 break;
4059 case 1:
4060 return NULL_TREE;
4061 }
4062 }
4063
4064 for (i = 0; i < ntparms; ++i)
4065 if (! TREE_VEC_ELT (vec, i))
4066 return NULL_TREE;
4067
4068 return vec;
4069}
4070
4071/* Return the most specialized of the list of templates in FNS that can
4072 produce an instantiation matching DECL. */
4073
4074tree
4075most_specialized (fns, decl)
4076 tree fns, decl;
4077{
98c1c668 4078 tree fn, champ, args, *p;
73aad9b9
JM
4079 int fate;
4080
4081 for (p = &fns; *p; )
4082 {
4083 args = get_bindings (TREE_VALUE (*p), decl);
4084 if (args)
4085 {
73aad9b9
JM
4086 p = &TREE_CHAIN (*p);
4087 }
4088 else
4089 *p = TREE_CHAIN (*p);
4090 }
4091
4092 if (! fns)
4093 return NULL_TREE;
4094
4095 fn = fns;
4096 champ = TREE_VALUE (fn);
4097 fn = TREE_CHAIN (fn);
4098 for (; fn; fn = TREE_CHAIN (fn))
4099 {
4100 fate = more_specialized (champ, TREE_VALUE (fn));
4101 if (fate == 1)
4102 ;
4103 else
4104 {
4105 if (fate == 0)
4106 {
4107 fn = TREE_CHAIN (fn);
4108 if (! fn)
4109 return error_mark_node;
4110 }
4111 champ = TREE_VALUE (fn);
4112 }
4113 }
4114
4115 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
4116 {
4117 fate = more_specialized (champ, TREE_VALUE (fn));
4118 if (fate != 1)
4119 return error_mark_node;
4120 }
4121
4122 return champ;
4123}
4124
4125/* Return the most specialized of the class template specializations in
4126 SPECS that can produce an instantiation matching ARGS. */
4127
4128tree
4129most_specialized_class (specs, mainargs)
4130 tree specs, mainargs;
4131{
4132 tree list = NULL_TREE, t, args, champ;
4133 int fate;
4134
4135 for (t = specs; t; t = TREE_CHAIN (t))
4136 {
4137 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
4138 if (args)
4139 {
4140 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
4141 TREE_TYPE (list) = TREE_TYPE (t);
4142 }
4143 }
4144
4145 if (! list)
4146 return NULL_TREE;
4147
4148 t = list;
4149 champ = t;
4150 t = TREE_CHAIN (t);
4151 for (; t; t = TREE_CHAIN (t))
4152 {
4153 fate = more_specialized_class (champ, t);
4154 if (fate == 1)
4155 ;
4156 else
4157 {
4158 if (fate == 0)
4159 {
4160 t = TREE_CHAIN (t);
4161 if (! t)
4162 return error_mark_node;
4163 }
4164 champ = t;
4165 }
4166 }
4167
4168 for (t = list; t && t != champ; t = TREE_CHAIN (t))
4169 {
85b71cf2 4170 fate = more_specialized_class (champ, t);
73aad9b9
JM
4171 if (fate != 1)
4172 return error_mark_node;
4173 }
4174
4175 return champ;
4176}
4177
8d08fdba 4178/* called from the parser. */
e92cc029 4179
8d08fdba 4180void
6633d636 4181do_decl_instantiation (declspecs, declarator, storage)
f0e01782 4182 tree declspecs, declarator, storage;
8d08fdba 4183{
c11b6f21 4184 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
e8abc66f
MS
4185 tree name;
4186 tree fn;
8d08fdba 4187 tree result = NULL_TREE;
faae18ab 4188 int extern_p = 0;
98c1c668 4189 tree templates = NULL_TREE;
e8abc66f 4190
ec255269
MS
4191 if (! DECL_LANG_SPECIFIC (decl))
4192 {
4193 cp_error ("explicit instantiation of non-template `%#D'", decl);
4194 return;
4195 }
4196
e8abc66f 4197 /* If we've already seen this template instance, use it. */
6633d636
MS
4198 if (TREE_CODE (decl) == VAR_DECL)
4199 {
4200 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
4201 if (result && TREE_CODE (result) != VAR_DECL)
4202 result = NULL_TREE;
4203 }
4204 else if (TREE_CODE (decl) != FUNCTION_DECL)
4205 {
4206 cp_error ("explicit instantiation of `%#D'", decl);
4207 return;
4208 }
4209 else if (DECL_FUNCTION_MEMBER_P (decl))
c91a56d2
MS
4210 {
4211 if (DECL_TEMPLATE_INSTANTIATION (decl))
4212 result = decl;
4213 else if (name = DECL_ASSEMBLER_NAME (decl),
4214 fn = IDENTIFIER_GLOBAL_VALUE (name),
4215 fn && DECL_TEMPLATE_INSTANTIATION (fn))
4216 result = fn;
98c1c668
JM
4217 else
4218 {
4219 /* Maybe this is an instantiation of a member template
4220 function. */
4221 tree ctype = DECL_CONTEXT (decl);
4222
4223 name = DECL_NAME (decl);
4224 fn = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
4225 if (fn)
4226 fn = TREE_VALUE (fn);
4227
4228 for (; fn; fn = DECL_CHAIN (fn))
4229 if (decls_match (fn, decl) && DECL_DEFER_OUTPUT (fn))
4230 {
4231 result = fn;
4232 break;
4233 }
4234 else if (TREE_CODE (fn) == TEMPLATE_DECL)
4235 templates = decl_tree_cons (NULL_TREE, fn, templates);
4236 }
c91a56d2 4237 }
e8abc66f 4238 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
8d08fdba
MS
4239 {
4240 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
98c1c668 4241 if (decls_match (fn, decl) && DECL_DEFER_OUTPUT (fn))
faae18ab
MS
4242 {
4243 result = fn;
4244 break;
4245 }
4246 else if (TREE_CODE (fn) == TEMPLATE_DECL)
73aad9b9 4247 templates = decl_tree_cons (NULL_TREE, fn, templates);
98c1c668 4248 }
73aad9b9 4249
98c1c668
JM
4250 if (templates && !result)
4251 {
4252 tree args;
4253 result = most_specialized (templates, decl);
4254 if (result == error_mark_node)
73aad9b9 4255 {
98c1c668
JM
4256 char *str = "candidates are:";
4257 cp_error ("ambiguous template instantiation for `%D' requested", decl);
4258 for (fn = templates; fn; fn = TREE_CHAIN (fn))
73aad9b9 4259 {
98c1c668
JM
4260 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
4261 str = " ";
73aad9b9 4262 }
98c1c668
JM
4263 return;
4264 }
4265 else if (result)
4266 {
4267 args = get_bindings (result, decl);
4268 result = instantiate_template (result, args);
73aad9b9 4269 }
8d08fdba 4270 }
98c1c668 4271
7177d104 4272 if (! result)
faae18ab
MS
4273 {
4274 cp_error ("no matching template for `%D' found", decl);
4275 return;
4276 }
7177d104 4277
6633d636
MS
4278 if (! DECL_TEMPLATE_INFO (result))
4279 {
4280 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
4281 return;
4282 }
4283
a0a33927
MS
4284 if (flag_external_templates)
4285 return;
4286
f0e01782 4287 if (storage == NULL_TREE)
00595019 4288 ;
faae18ab
MS
4289 else if (storage == ridpointers[(int) RID_EXTERN])
4290 extern_p = 1;
f0e01782
MS
4291 else
4292 cp_error ("storage class `%D' applied to template instantiation",
4293 storage);
5566b478 4294
5566b478 4295 mark_decl_instantiated (result, extern_p);
44a8d0b3 4296 repo_template_instantiated (result, extern_p);
c91a56d2
MS
4297 if (! extern_p)
4298 instantiate_decl (result);
7177d104
MS
4299}
4300
faae18ab
MS
4301void
4302mark_class_instantiated (t, extern_p)
4303 tree t;
4304 int extern_p;
4305{
4306 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
4307 SET_CLASSTYPE_INTERFACE_KNOWN (t);
4308 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
4309 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
4310 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
4311 if (! extern_p)
4312 {
4313 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
4314 rest_of_type_compilation (t, 1);
4315 }
4316}
e8abc66f 4317
7177d104 4318void
ca79f85d
JM
4319do_type_instantiation (t, storage)
4320 tree t, storage;
7177d104 4321{
e8abc66f
MS
4322 int extern_p = 0;
4323 int nomem_p = 0;
5566b478
MS
4324 int static_p = 0;
4325
ca79f85d
JM
4326 if (TREE_CODE (t) == TYPE_DECL)
4327 t = TREE_TYPE (t);
4328
4329 if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
4330 {
4331 cp_error ("explicit instantiation of non-template type `%T'", t);
4332 return;
4333 }
4334
5566b478 4335 complete_type (t);
7177d104 4336
a292b002
MS
4337 /* With -fexternal-templates, explicit instantiations are treated the same
4338 as implicit ones. */
a0a33927
MS
4339 if (flag_external_templates)
4340 return;
4341
f0e01782
MS
4342 if (TYPE_SIZE (t) == NULL_TREE)
4343 {
4344 cp_error ("explicit instantiation of `%#T' before definition of template",
4345 t);
4346 return;
4347 }
4348
4349 if (storage == NULL_TREE)
e8abc66f
MS
4350 /* OK */;
4351 else if (storage == ridpointers[(int) RID_INLINE])
4352 nomem_p = 1;
f0e01782
MS
4353 else if (storage == ridpointers[(int) RID_EXTERN])
4354 extern_p = 1;
5566b478
MS
4355 else if (storage == ridpointers[(int) RID_STATIC])
4356 static_p = 1;
f0e01782
MS
4357 else
4358 {
4359 cp_error ("storage class `%D' applied to template instantiation",
4360 storage);
4361 extern_p = 0;
4362 }
4363
a292b002 4364 /* We've already instantiated this. */
44a8d0b3
MS
4365 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
4366 && extern_p)
4367 return;
a292b002 4368
f376e137 4369 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
44a8d0b3
MS
4370 {
4371 mark_class_instantiated (t, extern_p);
4372 repo_template_instantiated (t, extern_p);
4373 }
e8abc66f
MS
4374
4375 if (nomem_p)
4376 return;
4377
7177d104 4378 {
db5ae43f 4379 tree tmp;
5566b478
MS
4380
4381 if (! static_p)
4382 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
4383 if (DECL_TEMPLATE_INSTANTIATION (tmp))
4384 {
4385 mark_decl_instantiated (tmp, extern_p);
4386 repo_template_instantiated (tmp, extern_p);
4387 if (! extern_p)
4388 instantiate_decl (tmp);
4389 }
4390
4391 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
4392 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
863adfc0 4393 {
5566b478 4394 mark_decl_instantiated (tmp, extern_p);
863adfc0 4395 repo_template_instantiated (tmp, extern_p);
5566b478
MS
4396 if (! extern_p)
4397 instantiate_decl (tmp);
863adfc0 4398 }
7177d104 4399
a292b002 4400 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
f376e137
MS
4401 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
4402 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
a292b002 4403 }
8d08fdba 4404}
a28e3c7f
MS
4405
4406tree
5566b478
MS
4407instantiate_decl (d)
4408 tree d;
a28e3c7f 4409{
5566b478
MS
4410 tree ti = DECL_TEMPLATE_INFO (d);
4411 tree tmpl = TI_TEMPLATE (ti);
4412 tree args = TI_ARGS (ti);
4413 tree td;
4414 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
4415 tree save_ti;
4416 int nested = in_function_p ();
4417 int d_defined;
4418 int pattern_defined;
5156628f
MS
4419 int line = lineno;
4420 char *file = input_filename;
5566b478
MS
4421
4422 if (TREE_CODE (d) == FUNCTION_DECL)
4423 {
4424 d_defined = (DECL_INITIAL (d) != NULL_TREE);
4425 pattern_defined = (DECL_INITIAL (pattern) != NULL_TREE);
4426 }
4427 else
4428 {
4429 d_defined = ! DECL_IN_AGGR_P (d);
4430 pattern_defined = ! DECL_IN_AGGR_P (pattern);
4431 }
4432
4433 if (d_defined)
4434 return d;
de22184b 4435
386b8a85
JM
4436 if (TREE_CODE (d) == FUNCTION_DECL)
4437 {
4438 tree specs;
4439
4440 /* Check to see if there is a matching specialization. */
4441 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4442 specs != NULL_TREE;
4443 specs = TREE_CHAIN (specs))
4444 if (comp_template_args (TREE_PURPOSE (specs), args))
4445 return TREE_VALUE (specs);
4446 }
4447
de22184b
MS
4448 /* This needs to happen before any tsubsting. */
4449 if (! push_tinst_level (d))
4450 return d;
4451
4452 push_to_top_level ();
4453 lineno = DECL_SOURCE_LINE (d);
4454 input_filename = DECL_SOURCE_FILE (d);
4455
4456 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
4457 variable is a static const initialized in the class body. */
4458 if (TREE_CODE (d) == VAR_DECL
4459 && ! DECL_INITIAL (d) && DECL_INITIAL (pattern))
4460 {
4461 pushclass (DECL_CONTEXT (d), 2);
4462 DECL_INITIAL (d) = tsubst_expr
98c1c668 4463 (DECL_INITIAL (pattern), args,
de22184b
MS
4464 TREE_VEC_LENGTH (args), tmpl);
4465 popclass (1);
4466 }
4467
4468 /* import_export_decl has to happen after DECL_INITIAL is set up. */
4469 if (pattern_defined)
5566b478
MS
4470 {
4471 repo_template_used (d);
4472
4473 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
4474 {
4475 if (flag_alt_external_templates)
4476 {
4477 if (interface_unknown)
4478 warn_if_unknown_interface (d);
4479 }
4480 else if (DECL_INTERFACE_KNOWN (pattern))
4481 {
4482 DECL_INTERFACE_KNOWN (d) = 1;
4483 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (pattern);
4484 }
4485 else
4486 warn_if_unknown_interface (pattern);
4487 }
4488
e92cc029 4489 if (at_eof)
5566b478
MS
4490 import_export_decl (d);
4491 }
4492
4493 if (! pattern_defined
4494 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
4495 && (! DECL_INTERFACE_KNOWN (d)
909e536a
MS
4496 || ! DECL_NOT_REALLY_EXTERN (d)))
4497 /* Kludge: if we compile a constructor in the middle of processing a
4498 toplevel declaration, we blow away the declspecs in
4499 temp_decl_obstack when we call permanent_allocation in
4500 finish_function. So don't compile it yet. */
4501 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
5566b478
MS
4502 {
4503 add_pending_template (d);
de22184b 4504 goto out;
5566b478
MS
4505 }
4506
5156628f
MS
4507 lineno = DECL_SOURCE_LINE (d);
4508 input_filename = DECL_SOURCE_FILE (d);
4509
5566b478
MS
4510 /* Trick tsubst into giving us a new decl in case the template changed. */
4511 save_ti = DECL_TEMPLATE_INFO (pattern);
4512 DECL_TEMPLATE_INFO (pattern) = NULL_TREE;
98c1c668 4513 td = tsubst (pattern, args, TREE_VEC_LENGTH (args), tmpl);
5566b478
MS
4514 DECL_TEMPLATE_INFO (pattern) = save_ti;
4515
d11ad92e
MS
4516 /* And set up DECL_INITIAL, since tsubst doesn't. */
4517 if (TREE_CODE (td) == VAR_DECL)
5156628f
MS
4518 {
4519 pushclass (DECL_CONTEXT (d), 2);
4520 DECL_INITIAL (td) = tsubst_expr
98c1c668 4521 (DECL_INITIAL (pattern), args,
5156628f
MS
4522 TREE_VEC_LENGTH (args), tmpl);
4523 popclass (1);
4524 }
d11ad92e 4525
5566b478 4526 if (TREE_CODE (d) == FUNCTION_DECL)
386b8a85
JM
4527 {
4528 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4529 new decl. */
4530 DECL_INITIAL (td) = error_mark_node;
4531
4532 if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td))
4533 /* Set up the information about what is being specialized. */
4534 DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d);
4535 }
5566b478
MS
4536 duplicate_decls (td, d);
4537 if (TREE_CODE (d) == FUNCTION_DECL)
4538 DECL_INITIAL (td) = 0;
4539
4540 if (TREE_CODE (d) == VAR_DECL)
4541 {
4542 DECL_IN_AGGR_P (d) = 0;
4543 if (DECL_INTERFACE_KNOWN (d))
4544 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
4545 else
4546 {
4547 DECL_EXTERNAL (d) = 1;
4548 DECL_NOT_REALLY_EXTERN (d) = 1;
4549 }
4550 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
4551 }
4552 else if (TREE_CODE (d) == FUNCTION_DECL)
4553 {
4554 tree t = DECL_SAVED_TREE (pattern);
5566b478 4555
c11b6f21 4556 start_function (NULL_TREE, d, NULL_TREE, 1);
5566b478
MS
4557 store_parm_decls ();
4558
e76a2646
MS
4559 if (t && TREE_CODE (t) == RETURN_INIT)
4560 {
4561 store_return_init
4562 (TREE_OPERAND (t, 0),
98c1c668 4563 tsubst_expr (TREE_OPERAND (t, 1), args,
e76a2646
MS
4564 TREE_VEC_LENGTH (args), tmpl));
4565 t = TREE_CHAIN (t);
4566 }
4567
5566b478
MS
4568 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
4569 {
4570 current_member_init_list
4571 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
4572 current_base_init_list
4573 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
4574 t = TREE_CHAIN (t);
4575 }
4576
4577 setup_vtbl_ptr ();
4578 /* Always keep the BLOCK node associated with the outermost
4579 pair of curley braces of a function. These are needed
4580 for correct operation of dwarfout.c. */
4581 keep_next_level ();
4582
4583 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
98c1c668 4584 tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl);
a28e3c7f 4585
5566b478 4586 finish_function (lineno, 0, nested);
5566b478
MS
4587 }
4588
de22184b 4589out:
5156628f
MS
4590 lineno = line;
4591 input_filename = file;
4592
5566b478 4593 pop_from_top_level ();
5566b478 4594 pop_tinst_level ();
a28e3c7f 4595
a28e3c7f
MS
4596 return d;
4597}
5566b478
MS
4598
4599tree
4600tsubst_chain (t, argvec)
4601 tree t, argvec;
4602{
4603 if (t)
4604 {
98c1c668 4605 tree first = tsubst (t, argvec,
5566b478
MS
4606 TREE_VEC_LENGTH (argvec), NULL_TREE);
4607 tree last = first;
4608
4609 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
4610 {
98c1c668 4611 tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE);
5566b478
MS
4612 TREE_CHAIN (last) = x;
4613 last = x;
4614 }
4615
4616 return first;
4617 }
4618 return NULL_TREE;
4619}
4620
824b9a4c 4621static tree
5566b478
MS
4622tsubst_expr_values (t, argvec)
4623 tree t, argvec;
4624{
4625 tree first = NULL_TREE;
4626 tree *p = &first;
4627
4628 for (; t; t = TREE_CHAIN (t))
4629 {
98c1c668 4630 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
5566b478 4631 TREE_VEC_LENGTH (argvec), NULL_TREE);
98c1c668 4632 tree val = tsubst_expr (TREE_VALUE (t), argvec,
5566b478
MS
4633 TREE_VEC_LENGTH (argvec), NULL_TREE);
4634 *p = build_tree_list (pur, val);
4635 p = &TREE_CHAIN (*p);
4636 }
4637 return first;
4638}
4639
4640tree last_tree;
4641
4642void
4643add_tree (t)
4644 tree t;
4645{
4646 last_tree = TREE_CHAIN (last_tree) = t;
4647}
73aad9b9
JM
4648
4649/* D is an undefined function declaration in the presence of templates with
4650 the same name, listed in FNS. If one of them can produce D as an
4651 instantiation, remember this so we can instantiate it at EOF if D has
4652 not been defined by that time. */
4653
4654void
4655add_maybe_template (d, fns)
4656 tree d, fns;
4657{
4658 tree t;
4659
4660 if (DECL_MAYBE_TEMPLATE (d))
4661 return;
4662
4663 t = most_specialized (fns, d);
4664 if (! t)
4665 return;
4666 if (t == error_mark_node)
4667 {
4668 cp_error ("ambiguous template instantiation for `%D'", d);
4669 return;
4670 }
4671
4672 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
4673 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
4674 DECL_MAYBE_TEMPLATE (d) = 1;
4675}
b87692e5
MS
4676
4677/* Instantiate an enumerated type. Used by instantiate_class_template and
4678 tsubst_expr. */
4679
4680static tree
b3d5a58b 4681tsubst_enum (tag, args, nargs, field_chain)
98c1c668 4682 tree tag, args;
b87692e5 4683 int nargs;
b3d5a58b 4684 tree * field_chain;
b87692e5 4685{
b3d5a58b
JG
4686 extern tree current_local_enum;
4687 tree prev_local_enum = current_local_enum;
4688
b87692e5
MS
4689 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
4690 tree e, values = NULL_TREE;
4691
4692 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
4693 {
4694 tree elt = build_enumerator (TREE_PURPOSE (e),
4695 tsubst_expr (TREE_VALUE (e), args,
4696 nargs, NULL_TREE));
4697 TREE_CHAIN (elt) = values;
4698 values = elt;
4699 }
4700
4701 finish_enum (newtag, values);
4702
b3d5a58b
JG
4703 if (NULL != field_chain)
4704 *field_chain = grok_enum_decls (newtag, NULL_TREE);
4705
4706 current_local_enum = prev_local_enum;
4707
b87692e5
MS
4708 return newtag;
4709}
This page took 0.80815 seconds and 5 git commands to generate.