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