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