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