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