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