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