]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
cp-tree.h (get_template_base): Don't declare.
[gcc.git] / gcc / cp / pt.c
CommitLineData
8d08fdba 1/* Handle parameterized types (templates) for GNU C++.
4c571114 2 Copyright (C) 1992, 93-97, 1998, 1999 Free Software Foundation, Inc.
8d08fdba 3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
fc378698 4 Rewritten by Jason Merrill (jason@cygnus.com).
8d08fdba
MS
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba
MS
22
23/* Known bugs or deficiencies include:
e92cc029 24
e92cc029
MS
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
8d08fdba
MS
27
28#include "config.h"
8d052bc7 29#include "system.h"
8d08fdba
MS
30#include "obstack.h"
31
32#include "tree.h"
33#include "flags.h"
34#include "cp-tree.h"
35#include "decl.h"
36#include "parse.h"
f0e01782 37#include "lex.h"
e8abc66f 38#include "output.h"
a9aedbc2 39#include "defaults.h"
49c249e1 40#include "except.h"
54f92bfb 41#include "toplev.h"
49c249e1 42
050367a3
MM
43/* The type of functions taking a tree, and some additional data, and
44 returning an int. */
45typedef int (*tree_fn_t) PROTO((tree, void*));
46
8d08fdba 47extern struct obstack permanent_obstack;
8d08fdba
MS
48
49extern int lineno;
50extern char *input_filename;
51struct pending_inline *pending_template_expansions;
52
5566b478
MS
53tree current_template_parms;
54HOST_WIDE_INT processing_template_decl;
8d08fdba 55
5566b478
MS
56tree pending_templates;
57static tree *template_tail = &pending_templates;
58
73aad9b9
JM
59tree maybe_templates;
60static tree *maybe_template_tail = &maybe_templates;
61
5566b478 62int minimal_parse_mode;
75b0bbce 63
92eca640 64int processing_specialization;
75650646 65int processing_explicit_instantiation;
67ffc812 66int processing_template_parmlist;
386b8a85
JM
67static int template_header_count;
68
75650646
MM
69static tree saved_trees;
70
8d08fdba
MS
71#define obstack_chunk_alloc xmalloc
72#define obstack_chunk_free free
73
830bfa74
MM
74#define UNIFY_ALLOW_NONE 0
75#define UNIFY_ALLOW_MORE_CV_QUAL 1
76#define UNIFY_ALLOW_LESS_CV_QUAL 2
77#define UNIFY_ALLOW_DERIVED 4
161c12b0 78#define UNIFY_ALLOW_INTEGER 8
830bfa74 79
34016c81
JM
80static int resolve_overloaded_unification PROTO((tree, tree, tree, tree,
81 unification_kind_t, int,
82 int*));
e97e5263 83static int try_one_overload PROTO((tree, tree, tree, tree, tree,
34016c81 84 unification_kind_t, int, int*));
830bfa74 85static int unify PROTO((tree, tree, tree, tree, int, int*));
49c249e1
JM
86static void add_pending_template PROTO((tree));
87static int push_tinst_level PROTO((tree));
88static tree classtype_mangled_name PROTO((tree));
36a117a5 89static char *mangle_class_name_for_template PROTO((char *, tree, tree));
49c249e1 90static tree tsubst_expr_values PROTO((tree, tree));
49c249e1 91static int list_eq PROTO((tree, tree));
36a117a5 92static tree get_class_bindings PROTO((tree, tree, tree));
744fac59 93static tree coerce_template_parms PROTO((tree, tree, tree, int, int));
dbfe2124 94static void tsubst_enum PROTO((tree, tree, tree));
98c1c668 95static tree add_to_template_args PROTO((tree, tree));
36a117a5 96static tree add_outermost_template_args PROTO((tree, tree));
830bfa74
MM
97static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
98 tree*));
050367a3 99static int type_unification_real PROTO((tree, tree, tree, tree,
830bfa74 100 int, unification_kind_t, int, int*));
386b8a85 101static void note_template_header PROTO((int));
840b09b4 102static tree maybe_fold_nontype_arg PROTO((tree));
e1467ff2 103static tree convert_nontype_argument PROTO((tree, tree));
8b5b8b7c
MM
104static tree convert_template_argument PROTO ((tree, tree, tree, int,
105 int , tree));
76b9a14d 106static tree get_bindings_overload PROTO((tree, tree, tree));
050367a3 107static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
4b580446 108static tree build_template_parm_index PROTO((int, int, int, tree, tree));
69ac77ce
JL
109static int inline_needs_template_parms PROTO((tree));
110static void push_inline_template_parms_recursive PROTO((tree, int));
111static tree retrieve_specialization PROTO((tree, tree));
36a117a5
MM
112static tree register_specialization PROTO((tree, tree, tree));
113static int unregister_specialization PROTO((tree, tree));
69ac77ce
JL
114static tree reduce_template_parm_level PROTO((tree, tree, int));
115static tree build_template_decl PROTO((tree, tree));
116static int mark_template_parm PROTO((tree, void *));
117static tree tsubst_friend_function PROTO((tree, tree));
6757edfe 118static tree tsubst_friend_class PROTO((tree, tree));
69ac77ce 119static tree get_bindings_real PROTO((tree, tree, tree, int));
04a81d94 120static int template_decl_level PROTO((tree));
a2b60a0e 121static tree maybe_get_template_decl_from_type_decl PROTO((tree));
830bfa74
MM
122static int check_cv_quals_for_unify PROTO((int, tree, tree));
123static tree tsubst_template_arg_vector PROTO((tree, tree));
36a117a5 124static tree tsubst_template_parms PROTO((tree, tree));
830bfa74 125static void regenerate_decl_from_template PROTO((tree, tree));
36a117a5
MM
126static tree most_specialized PROTO((tree, tree, tree));
127static tree most_specialized_class PROTO((tree, tree));
128static tree most_general_template PROTO((tree));
129static void set_mangled_name_for_template_decl PROTO((tree));
39c01e4c 130static int template_class_depth_real PROTO((tree, int));
ae58fa02
MM
131static tree tsubst_aggr_type PROTO((tree, tree, tree, int));
132static tree tsubst_decl PROTO((tree, tree, tree, tree));
cabc336a 133static tree tsubst_arg_types PROTO((tree, tree, tree));
6c30752f
MM
134static void check_specialization_scope PROTO((void));
135static tree process_partial_specialization PROTO((tree));
61a127b3 136static void set_current_access_from_decl PROTO((tree));
6ba89f8e 137static void check_default_tmpl_args PROTO((tree, tree, int, int));
297e73d8 138static tree tsubst_call_declarator_parms PROTO((tree, tree, tree));
00aeceec 139static tree get_template_base_recursive PROTO((tree, tree, tree, tree,
fcfb9f96 140 tree, int));
00aeceec 141static tree get_template_base PROTO((tree, tree, tree, tree));
36a117a5
MM
142
143/* We use TREE_VECs to hold template arguments. If there is only one
144 level of template arguments, then the TREE_VEC contains the
145 arguments directly. If there is more than one level of template
146 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
147 containing the template arguments for a single level. The first
148 entry in the outer TREE_VEC is the outermost level of template
149 parameters; the last is the innermost.
150
151 It is incorrect to ever form a template argument vector containing
152 only one level of arguments, but which is a TREE_VEC containing as
153 its only entry the TREE_VEC for that level. */
154
36a117a5
MM
155/* Non-zero if the template arguments is actually a vector of vectors,
156 rather than just a vector. */
157#define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
158 (NODE != NULL_TREE \
159 && TREE_CODE (NODE) == TREE_VEC \
160 && TREE_VEC_LENGTH (NODE) > 0 \
161 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
67ffc812
MM
162 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
163
e4a84209
MM
164/* The depth of a template argument vector. When called directly by
165 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
166 template arguments. In fact, we may even see NULL_TREE if there
167 are no template arguments. In both of those cases, there is only
168 one level of template arguments. */
169#define TMPL_ARGS_DEPTH(NODE) \
170 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
171
36a117a5
MM
172/* The LEVELth level of the template ARGS. Note that template
173 parameter levels are indexed from 1, not from 0. */
174#define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
175 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
176 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
177
178/* Set the LEVELth level of the template ARGS to VAL. This macro does
179 not work with single-level argument vectors. */
180#define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
181 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
182
183/* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
184#define TMPL_ARG(ARGS, LEVEL, IDX) \
185 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
186
187/* Set the IDXth element in the LEVELth level of ARGS to VAL. This
188 macro does not work with single-level argument vectors. */
189#define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
190 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
191
e4a84209
MM
192/* Given a single level of template arguments in NODE, return the
193 number of arguments. */
194#define NUM_TMPL_ARGS(NODE) \
195 ((NODE) == NULL_TREE ? 0 \
196 : (TREE_CODE (NODE) == TREE_VEC \
197 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
198
36a117a5
MM
199/* The number of levels of template parameters given by NODE. */
200#define TMPL_PARMS_DEPTH(NODE) \
201 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
202
e1467ff2
MM
203/* Do any processing required when DECL (a member template declaration
204 using TEMPLATE_PARAMETERS as its innermost parameter list) is
205 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
206 it is a specialization, in which case the DECL itself is returned. */
207
208tree
61a127b3 209finish_member_template_decl (decl)
e1467ff2
MM
210 tree decl;
211{
93cdc044
JM
212 if (decl == NULL_TREE || decl == void_type_node)
213 return NULL_TREE;
3ddfb0e6
MM
214 else if (decl == error_mark_node)
215 /* By returning NULL_TREE, the parser will just ignore this
216 declaration. We have already issued the error. */
217 return NULL_TREE;
93cdc044
JM
218 else if (TREE_CODE (decl) == TREE_LIST)
219 {
5f311aec 220 /* Assume that the class is the only declspec. */
93cdc044 221 decl = TREE_VALUE (decl);
8d019cef
JM
222 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
223 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
93cdc044
JM
224 {
225 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
226 check_member_template (tmpl);
227 return tmpl;
228 }
8d019cef 229 return NULL_TREE;
93cdc044 230 }
a1da6cba 231 else if (DECL_TEMPLATE_INFO (decl))
e1467ff2 232 {
a1da6cba
MM
233 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
234 {
235 check_member_template (DECL_TI_TEMPLATE (decl));
236 return DECL_TI_TEMPLATE (decl);
237 }
238 else
239 return decl;
240 }
241 else
8251199e 242 cp_error ("invalid member template declaration `%D'", decl);
e1467ff2 243
a1da6cba 244 return error_mark_node;
f84b4be9 245}
e1467ff2 246
f84b4be9
JM
247/* Returns the template nesting level of the indicated class TYPE.
248
249 For example, in:
250 template <class T>
251 struct A
252 {
253 template <class U>
254 struct B {};
255 };
256
39c01e4c
MM
257 A<T>::B<U> has depth two, while A<T> has depth one.
258 Both A<T>::B<int> and A<int>::B<U> have depth one, if
259 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
260 specializations.
261
262 This function is guaranteed to return 0 if passed NULL_TREE so
263 that, for example, `template_class_depth (current_class_type)' is
264 always safe. */
f84b4be9
JM
265
266int
39c01e4c 267template_class_depth_real (type, count_specializations)
f84b4be9 268 tree type;
39c01e4c 269 int count_specializations;
f84b4be9 270{
93cdc044 271 int depth;
f84b4be9 272
2c73f9f5 273 for (depth = 0;
ed44da02
MM
274 type && TREE_CODE (type) != NAMESPACE_DECL;
275 type = (TREE_CODE (type) == FUNCTION_DECL)
276 ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type))
277 {
278 if (TREE_CODE (type) != FUNCTION_DECL)
279 {
280 if (CLASSTYPE_TEMPLATE_INFO (type)
281 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
282 && ((count_specializations
283 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
284 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
285 ++depth;
286 }
287 else
288 {
289 if (DECL_TEMPLATE_INFO (type)
290 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
291 && ((count_specializations
292 && DECL_TEMPLATE_SPECIALIZATION (type))
293 || uses_template_parms (DECL_TI_ARGS (type))))
294 ++depth;
295 }
296 }
f84b4be9
JM
297
298 return depth;
e1467ff2 299}
98c1c668 300
39c01e4c
MM
301/* Returns the template nesting level of the indicated class TYPE.
302 Like template_class_depth_real, but instantiations do not count in
303 the depth. */
304
305int
306template_class_depth (type)
307 tree type;
308{
309 return template_class_depth_real (type, /*count_specializations=*/0);
310}
311
cae40af6
JM
312/* Returns 1 if processing DECL as part of do_pending_inlines
313 needs us to push template parms. */
314
315static int
316inline_needs_template_parms (decl)
317 tree decl;
318{
319 if (! DECL_TEMPLATE_INFO (decl))
320 return 0;
f84b4be9 321
36a117a5 322 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
cae40af6
JM
323 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
324}
325
326/* Subroutine of maybe_begin_member_template_processing.
327 Push the template parms in PARMS, starting from LEVELS steps into the
328 chain, and ending at the beginning, since template parms are listed
329 innermost first. */
330
331static void
332push_inline_template_parms_recursive (parmlist, levels)
333 tree parmlist;
334 int levels;
335{
336 tree parms = TREE_VALUE (parmlist);
337 int i;
338
339 if (levels > 1)
340 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
786b5245 341
98c1c668 342 ++processing_template_decl;
cae40af6 343 current_template_parms
98c1c668
JM
344 = tree_cons (build_int_2 (0, processing_template_decl),
345 parms, current_template_parms);
cae40af6
JM
346 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
347
786b5245 348 pushlevel (0);
98c1c668
JM
349 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
350 {
786b5245
MM
351 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
352 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
cae40af6 353
98c1c668
JM
354 switch (TREE_CODE (parm))
355 {
786b5245 356 case TYPE_DECL:
73b0fce8 357 case TEMPLATE_DECL:
98c1c668
JM
358 pushdecl (parm);
359 break;
786b5245
MM
360
361 case PARM_DECL:
362 {
fc03edb3
MM
363 /* Make a CONST_DECL as is done in process_template_parm.
364 It is ugly that we recreate this here; the original
365 version built in process_template_parm is no longer
366 available. */
786b5245
MM
367 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
368 TREE_TYPE (parm));
fc03edb3 369 SET_DECL_ARTIFICIAL (decl);
786b5245 370 DECL_INITIAL (decl) = DECL_INITIAL (parm);
fc03edb3 371 DECL_TEMPLATE_PARM_P (decl) = 1;
786b5245
MM
372 pushdecl (decl);
373 }
cae40af6 374 break;
786b5245 375
98c1c668
JM
376 default:
377 my_friendly_abort (0);
378 }
379 }
380}
381
cae40af6
JM
382/* Restore the template parameter context for a member template or
383 a friend template defined in a class definition. */
384
385void
386maybe_begin_member_template_processing (decl)
387 tree decl;
388{
389 tree parms;
390 int levels;
391
392 if (! inline_needs_template_parms (decl))
393 return;
394
36a117a5 395 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
cae40af6 396
36a117a5 397 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
cae40af6
JM
398
399 if (DECL_TEMPLATE_SPECIALIZATION (decl))
400 {
401 --levels;
402 parms = TREE_CHAIN (parms);
403 }
404
405 push_inline_template_parms_recursive (parms, levels);
406}
407
98c1c668
JM
408/* Undo the effects of begin_member_template_processing. */
409
410void
b370501f 411maybe_end_member_template_processing ()
98c1c668
JM
412{
413 if (! processing_template_decl)
414 return;
415
cae40af6
JM
416 while (current_template_parms
417 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
418 {
419 --processing_template_decl;
420 current_template_parms = TREE_CHAIN (current_template_parms);
421 poplevel (0, 0, 0);
422 }
98c1c668
JM
423}
424
cae40af6 425/* Returns non-zero iff T is a member template function. We must be
f84b4be9 426 careful as in
75650646
MM
427
428 template <class T> class C { void f(); }
429
430 Here, f is a template function, and a member, but not a member
431 template. This function does not concern itself with the origin of
432 T, only its present state. So if we have
433
434 template <class T> class C { template <class U> void f(U); }
435
436 then neither C<int>::f<char> nor C<T>::f<double> is considered
f181d4ae
MM
437 to be a member template. But, `template <class U> void
438 C<int>::f(U)' is considered a member template. */
98c1c668 439
cae40af6
JM
440int
441is_member_template (t)
98c1c668
JM
442 tree t;
443{
f181d4ae 444 if (!DECL_FUNCTION_TEMPLATE_P (t))
75650646 445 /* Anything that isn't a function or a template function is
aa5f3bad
MM
446 certainly not a member template. */
447 return 0;
448
cae40af6
JM
449 /* A local class can't have member templates. */
450 if (hack_decl_function_context (t))
451 return 0;
452
f181d4ae 453 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
f84b4be9
JM
454 /* If there are more levels of template parameters than
455 there are template classes surrounding the declaration,
456 then we have a member template. */
f181d4ae
MM
457 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
458 template_class_depth (DECL_CLASS_CONTEXT (t))));
f84b4be9 459}
98c1c668 460
ba4f4e5d 461#if 0 /* UNUSED */
2ee4e084
MM
462/* Returns non-zero iff T is a member template class. See
463 is_member_template for a description of what precisely constitutes
464 a member template. */
465
466int
467is_member_template_class (t)
468 tree t;
469{
470 if (!DECL_CLASS_TEMPLATE_P (t))
471 /* Anything that isn't a class template, is certainly not a member
472 template. */
473 return 0;
474
475 if (!DECL_CLASS_SCOPE_P (t))
476 /* Anything whose context isn't a class type is surely not a
477 member template. */
478 return 0;
479
480 /* If there are more levels of template parameters than there are
481 template classes surrounding the declaration, then we have a
482 member template. */
36a117a5 483 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
2ee4e084
MM
484 template_class_depth (DECL_CONTEXT (t)));
485}
ba4f4e5d 486#endif
2ee4e084 487
36a117a5
MM
488/* Return a new template argument vector which contains all of ARGS,
489 but has as its innermost set of arguments the EXTRA_ARGS. The
490 resulting vector will be built on a temporary obstack, and so must
491 be explicitly copied to the permanent obstack, if required. */
e6f1275f
JM
492
493static tree
36a117a5
MM
494add_to_template_args (args, extra_args)
495 tree args;
496 tree extra_args;
e6f1275f 497{
36a117a5
MM
498 tree new_args;
499 int extra_depth;
500 int i;
501 int j;
e6f1275f 502
36a117a5
MM
503 extra_depth = TMPL_ARGS_DEPTH (extra_args);
504 new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
e6f1275f 505
36a117a5
MM
506 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
507 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
e6f1275f 508
36a117a5
MM
509 for (j = 1; j <= extra_depth; ++j, ++i)
510 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
511
e6f1275f
JM
512 return new_args;
513}
514
36a117a5
MM
515/* Like add_to_template_args, but only the outermost ARGS are added to
516 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
517 (EXTRA_ARGS) levels are added. This function is used to combine
518 the template arguments from a partial instantiation with the
519 template arguments used to attain the full instantiation from the
520 partial instantiation. */
98c1c668 521
4966381a 522static tree
36a117a5 523add_outermost_template_args (args, extra_args)
98c1c668
JM
524 tree args;
525 tree extra_args;
526{
527 tree new_args;
528
e4a84209
MM
529 /* If there are more levels of EXTRA_ARGS than there are ARGS,
530 something very fishy is going on. */
531 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
532 0);
533
534 /* If *all* the new arguments will be the EXTRA_ARGS, just return
535 them. */
536 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
537 return extra_args;
538
36a117a5
MM
539 /* For the moment, we make ARGS look like it contains fewer levels. */
540 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
541
542 new_args = add_to_template_args (args, extra_args);
98c1c668 543
36a117a5
MM
544 /* Now, we restore ARGS to its full dimensions. */
545 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
98c1c668
JM
546
547 return new_args;
548}
5566b478
MS
549
550/* We've got a template header coming up; push to a new level for storing
551 the parms. */
8d08fdba 552
8d08fdba
MS
553void
554begin_template_parm_list ()
555{
6757edfe
MM
556 /* We use a non-tag-transparent scope here, which causes pushtag to
557 put tags in this scope, rather than in the enclosing class or
558 namespace scope. This is the right thing, since we want
559 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
560 global template class, push_template_decl handles putting the
561 TEMPLATE_DECL into top-level scope. For a nested template class,
562 e.g.:
563
564 template <class T> struct S1 {
565 template <class T> struct S2 {};
566 };
567
568 pushtag contains special code to call pushdecl_with_scope on the
569 TEMPLATE_DECL for S2. */
8d08fdba 570 pushlevel (0);
5566b478 571 declare_pseudo_global_level ();
5156628f 572 ++processing_template_decl;
67ffc812 573 ++processing_template_parmlist;
386b8a85
JM
574 note_template_header (0);
575}
576
6c30752f
MM
577/* This routine is called when a specialization is declared. If it is
578 illegal to declare a specialization here, an error is reported. */
579
580void
581check_specialization_scope ()
582{
583 tree scope = current_scope ();
3ddfb0e6 584
6c30752f
MM
585 /* [temp.expl.spec]
586
587 An explicit specialization shall be declared in the namespace of
588 which the template is a member, or, for member templates, in the
589 namespace of which the enclosing class or enclosing class
590 template is a member. An explicit specialization of a member
591 function, member class or static data member of a class template
592 shall be declared in the namespace of which the class template
593 is a member. */
594 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
8251199e 595 cp_error ("explicit specialization in non-namespace scope `%D'",
6c30752f 596 scope);
3ddfb0e6 597
6c30752f
MM
598 /* [temp.expl.spec]
599
600 In an explicit specialization declaration for a member of a class
601 template or a member template that appears in namespace scope,
602 the member template and some of its enclosing class templates may
603 remain unspecialized, except that the declaration shall not
604 explicitly specialize a class member template if its enclosing
605 class templates are not explicitly specialized as well. */
606 if (current_template_parms)
fd4de5ff 607 cp_error ("enclosing class templates are not explicitly specialized");
6c30752f
MM
608}
609
386b8a85
JM
610/* We've just seen template <>. */
611
612void
613begin_specialization ()
614{
615 note_template_header (1);
6c30752f 616 check_specialization_scope ();
386b8a85
JM
617}
618
386b8a85
JM
619/* Called at then end of processing a declaration preceeded by
620 template<>. */
621
622void
623end_specialization ()
624{
625 reset_specialization ();
626}
627
386b8a85
JM
628/* Any template <>'s that we have seen thus far are not referring to a
629 function specialization. */
630
631void
632reset_specialization ()
633{
634 processing_specialization = 0;
635 template_header_count = 0;
636}
637
386b8a85
JM
638/* We've just seen a template header. If SPECIALIZATION is non-zero,
639 it was of the form template <>. */
640
4966381a 641static void
386b8a85
JM
642note_template_header (specialization)
643 int specialization;
644{
645 processing_specialization = specialization;
646 template_header_count++;
647}
648
75650646 649/* We're beginning an explicit instantiation. */
386b8a85 650
75650646
MM
651void
652begin_explicit_instantiation ()
386b8a85 653{
75650646
MM
654 ++processing_explicit_instantiation;
655}
386b8a85 656
386b8a85 657
75650646
MM
658void
659end_explicit_instantiation ()
660{
661 my_friendly_assert(processing_explicit_instantiation > 0, 0);
662 --processing_explicit_instantiation;
663}
386b8a85 664
36a117a5
MM
665/* The TYPE is being declared. If it is a template type, that means it
666 is a partial specialization. Do appropriate error-checking. */
667
668void
669maybe_process_partial_specialization (type)
670 tree type;
671{
672 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
673 {
674 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
675 && TYPE_SIZE (type) == NULL_TREE)
676 {
700466c2
JM
677 if (current_namespace
678 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
679 {
680 cp_pedwarn ("specializing `%#T' in different namespace", type);
681 cp_pedwarn_at (" from definition of `%#D'",
682 CLASSTYPE_TI_TEMPLATE (type));
683 }
36a117a5
MM
684 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
685 if (processing_template_decl)
686 push_template_decl (TYPE_MAIN_DECL (type));
687 }
688 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
8251199e 689 cp_error ("specialization of `%T' after instantiation", type);
36a117a5
MM
690 }
691}
692
75650646
MM
693/* Retrieve the specialization (in the sense of [temp.spec] - a
694 specialization is either an instantiation or an explicit
695 specialization) of TMPL for the given template ARGS. If there is
696 no such specialization, return NULL_TREE. The ARGS are a vector of
697 arguments, or a vector of vectors of arguments, in the case of
698 templates with more than one level of parameters. */
699
700static tree
701retrieve_specialization (tmpl, args)
702 tree tmpl;
703 tree args;
704{
705 tree s;
706
707 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
708
36a117a5
MM
709 /* There should be as many levels of arguments as there are
710 levels of parameters. */
711 my_friendly_assert (TMPL_ARGS_DEPTH (args)
712 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
713 0);
714
75650646
MM
715 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
716 s != NULL_TREE;
717 s = TREE_CHAIN (s))
718 if (comp_template_args (TREE_PURPOSE (s), args))
719 return TREE_VALUE (s);
720
721 return NULL_TREE;
386b8a85
JM
722}
723
6757edfe
MM
724/* Returns non-zero iff DECL is a specialization of TMPL. */
725
726int
727is_specialization_of (decl, tmpl)
728 tree decl;
729 tree tmpl;
730{
731 tree t;
732
733 if (TREE_CODE (decl) == FUNCTION_DECL)
734 {
735 for (t = decl;
736 t != NULL_TREE;
737 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
738 if (t == tmpl)
739 return 1;
740 }
741 else
742 {
743 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
744
745 for (t = TREE_TYPE (decl);
746 t != NULL_TREE;
747 t = CLASSTYPE_USE_TEMPLATE (t)
748 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
3bfdc719
MM
749 if (same_type_p (TYPE_MAIN_VARIANT (t),
750 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl))))
6757edfe
MM
751 return 1;
752 }
386b8a85 753
6757edfe
MM
754 return 0;
755}
75650646
MM
756
757/* Register the specialization SPEC as a specialization of TMPL with
36a117a5
MM
758 the indicated ARGS. Returns SPEC, or an equivalent prior
759 declaration, if available. */
75650646 760
36a117a5 761static tree
75650646
MM
762register_specialization (spec, tmpl, args)
763 tree spec;
764 tree tmpl;
765 tree args;
766{
767 tree s;
768
769 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
770
36a117a5
MM
771 if (TREE_CODE (spec) == FUNCTION_DECL
772 && uses_template_parms (DECL_TI_ARGS (spec)))
773 /* This is the FUNCTION_DECL for a partial instantiation. Don't
774 register it; we want the corresponding TEMPLATE_DECL instead.
775 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
776 the more obvious `uses_template_parms (spec)' to avoid problems
777 with default function arguments. In particular, given
778 something like this:
779
780 template <class T> void f(T t1, T t = T())
781
782 the default argument expression is not substituted for in an
783 instantiation unless and until it is actually needed. */
784 return spec;
75650646 785
36a117a5
MM
786 /* There should be as many levels of arguments as there are
787 levels of parameters. */
788 my_friendly_assert (TMPL_ARGS_DEPTH (args)
789 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
790 0);
791
75650646
MM
792 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
793 s != NULL_TREE;
794 s = TREE_CHAIN (s))
795 if (comp_template_args (TREE_PURPOSE (s), args))
796 {
797 tree fn = TREE_VALUE (s);
798
799 if (DECL_TEMPLATE_SPECIALIZATION (spec))
800 {
801 if (DECL_TEMPLATE_INSTANTIATION (fn))
802 {
803 if (TREE_USED (fn)
804 || DECL_EXPLICIT_INSTANTIATION (fn))
805 {
8251199e 806 cp_error ("specialization of %D after instantiation",
75650646 807 fn);
36a117a5 808 return spec;
75650646
MM
809 }
810 else
811 {
812 /* This situation should occur only if the first
813 specialization is an implicit instantiation,
814 the second is an explicit specialization, and
815 the implicit instantiation has not yet been
816 used. That situation can occur if we have
36a117a5
MM
817 implicitly instantiated a member function and
818 then specialized it later.
819
820 We can also wind up here if a friend
821 declaration that looked like an instantiation
822 turns out to be a specialization:
823
824 template <class T> void foo(T);
825 class S { friend void foo<>(int) };
826 template <> void foo(int);
827
828 We transform the existing DECL in place so that
829 any pointers to it become pointers to the
fbf1c34b
MM
830 updated declaration.
831
832 If there was a definition for the template, but
833 not for the specialization, we want this to
834 look as if there is no definition, and vice
835 versa. */
836 DECL_INITIAL (fn) = NULL_TREE;
837 duplicate_decls (spec, fn);
838
839 return fn;
75650646
MM
840 }
841 }
842 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
843 {
fbf1c34b
MM
844 duplicate_decls (spec, fn);
845 return fn;
75650646
MM
846 }
847 }
848 }
849
850 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
851 = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
36a117a5
MM
852
853 return spec;
854}
855
856/* Unregister the specialization SPEC as a specialization of TMPL.
857 Returns nonzero if the SPEC was listed as a specialization of
858 TMPL. */
859
860static int
861unregister_specialization (spec, tmpl)
862 tree spec;
863 tree tmpl;
864{
865 tree* s;
866
867 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
868 *s != NULL_TREE;
869 s = &TREE_CHAIN (*s))
870 if (TREE_VALUE (*s) == spec)
871 {
872 *s = TREE_CHAIN (*s);
873 return 1;
874 }
875
876 return 0;
75650646
MM
877}
878
e1467ff2
MM
879/* Print the list of candidate FNS in an error message. */
880
104bf76a 881void
e1467ff2
MM
882print_candidates (fns)
883 tree fns;
884{
885 tree fn;
886
887 char* str = "candidates are:";
888
889 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
890 {
8251199e 891 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
e1467ff2
MM
892 str = " ";
893 }
894}
895
75650646 896/* Returns the template (one of the functions given by TEMPLATE_ID)
e1467ff2
MM
897 which can be specialized to match the indicated DECL with the
898 explicit template args given in TEMPLATE_ID. If
899 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
900 member template. The template args (those explicitly specified and
901 those deduced) are output in a newly created vector *TARGS_OUT. If
902 it is impossible to determine the result, an error message is
903 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
904 available. */
75650646 905
386b8a85 906tree
e1467ff2 907determine_specialization (template_id, decl, targs_out,
75650646
MM
908 need_member_template,
909 complain)
386b8a85 910 tree template_id;
e1467ff2 911 tree decl;
386b8a85
JM
912 tree* targs_out;
913 int need_member_template;
914 int complain;
915{
f2e48b67 916 tree fns, targs_in;
e1467ff2 917 tree templates = NULL_TREE;
386b8a85 918 tree fn;
386b8a85 919
e1467ff2
MM
920 *targs_out = NULL_TREE;
921
f2e48b67
BK
922 if (template_id == error_mark_node)
923 return error_mark_node;
924
925 fns = TREE_OPERAND (template_id, 0);
926 targs_in = TREE_OPERAND (template_id, 1);
927
27fafc8d
JM
928 if (fns == error_mark_node)
929 return error_mark_node;
930
2c73f9f5
ML
931 /* Check for baselinks. */
932 if (TREE_CODE (fns) == TREE_LIST)
933 fns = TREE_VALUE (fns);
386b8a85 934
2c73f9f5 935 for (; fns; fns = OVL_NEXT (fns))
386b8a85 936 {
75650646
MM
937 tree tmpl;
938
2c73f9f5 939 fn = OVL_CURRENT (fns);
75650646
MM
940 if (!need_member_template
941 && TREE_CODE (fn) == FUNCTION_DECL
e1467ff2 942 && DECL_FUNCTION_MEMBER_P (fn)
75650646
MM
943 && DECL_USE_TEMPLATE (fn)
944 && DECL_TI_TEMPLATE (fn))
e1467ff2
MM
945 /* We can get here when processing something like:
946 template <class T> class X { void f(); }
947 template <> void X<int>::f() {}
948 We're specializing a member function, but not a member
949 template. */
75650646
MM
950 tmpl = DECL_TI_TEMPLATE (fn);
951 else if (TREE_CODE (fn) != TEMPLATE_DECL
e1467ff2 952 || (need_member_template && !is_member_template (fn)))
798eed5e 953 continue;
75650646
MM
954 else
955 tmpl = fn;
386b8a85 956
75650646 957 if (list_length (targs_in) > DECL_NTPARMS (tmpl))
386b8a85
JM
958 continue;
959
e1467ff2 960 if (decl == NULL_TREE)
386b8a85 961 {
34016c81
JM
962 /* Unify against ourselves to make sure that the args we have
963 make sense and there aren't any undeducible parms. It's OK if
964 not all the parms are specified; they might be deduced
965 later. */
966 tree targs = get_bindings_overload (tmpl, DECL_RESULT (tmpl),
967 targs_in);
968
969 if (targs)
e1467ff2
MM
970 /* Unification was successful. */
971 templates = scratch_tree_cons (targs, tmpl, templates);
386b8a85 972 }
e1467ff2
MM
973 else
974 templates = scratch_tree_cons (NULL_TREE, tmpl, templates);
386b8a85 975 }
75650646 976
e1467ff2 977 if (decl != NULL_TREE)
386b8a85 978 {
e1467ff2 979 tree tmpl = most_specialized (templates, decl, targs_in);
36a117a5
MM
980 tree inner_args;
981 tree tmpl_args;
e1467ff2
MM
982
983 if (tmpl == error_mark_node)
984 goto ambiguous;
985 else if (tmpl == NULL_TREE)
986 goto no_match;
987
36a117a5
MM
988 inner_args = get_bindings (tmpl, decl, targs_in);
989 tmpl_args = DECL_TI_ARGS (DECL_RESULT (tmpl));
990 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (tmpl_args))
991 {
992 *targs_out = copy_node (tmpl_args);
993 SET_TMPL_ARGS_LEVEL (*targs_out,
994 TMPL_ARGS_DEPTH (*targs_out),
995 inner_args);
996 }
997 else
998 *targs_out = inner_args;
999
e1467ff2
MM
1000 return tmpl;
1001 }
1002
1003 if (templates == NULL_TREE)
1004 {
1005 no_match:
386b8a85 1006 if (complain)
27fafc8d 1007 {
8251199e 1008 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
27fafc8d
JM
1009 template_id, decl);
1010 return error_mark_node;
1011 }
386b8a85
JM
1012 return NULL_TREE;
1013 }
34016c81
JM
1014 else if (TREE_CHAIN (templates) != NULL_TREE
1015 || uses_template_parms (TREE_PURPOSE (templates)))
386b8a85 1016 {
e1467ff2 1017 ambiguous:
386b8a85
JM
1018 if (complain)
1019 {
8251199e 1020 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
27fafc8d 1021 template_id, decl);
e1467ff2 1022 print_candidates (templates);
27fafc8d 1023 return error_mark_node;
386b8a85 1024 }
386b8a85
JM
1025 return NULL_TREE;
1026 }
1027
1028 /* We have one, and exactly one, match. */
e1467ff2
MM
1029 *targs_out = TREE_PURPOSE (templates);
1030 return TREE_VALUE (templates);
8d08fdba 1031}
6757edfe 1032
386b8a85 1033/* Check to see if the function just declared, as indicated in
75650646
MM
1034 DECLARATOR, and in DECL, is a specialization of a function
1035 template. We may also discover that the declaration is an explicit
1036 instantiation at this point.
1037
e1467ff2
MM
1038 Returns DECL, or an equivalent declaration that should be used
1039 instead.
75650646 1040
75650646
MM
1041 FLAGS is a bitmask consisting of the following flags:
1042
75650646
MM
1043 2: The function has a definition.
1044 4: The function is a friend.
75650646
MM
1045
1046 The TEMPLATE_COUNT is the number of references to qualifying
1047 template classes that appeared in the name of the function. For
1048 example, in
1049
1050 template <class T> struct S { void f(); };
1051 void S<int>::f();
1052
1053 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1054 classes are not counted in the TEMPLATE_COUNT, so that in
1055
1056 template <class T> struct S {};
1057 template <> struct S<int> { void f(); }
36a117a5 1058 template <> void S<int>::f();
75650646
MM
1059
1060 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1061 illegal; there should be no template <>.)
1062
1063 If the function is a specialization, it is marked as such via
1064 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1065 is set up correctly, and it is added to the list of specializations
1066 for that template. */
386b8a85 1067
e1467ff2 1068tree
27bb8339 1069check_explicit_specialization (declarator, decl, template_count, flags)
386b8a85
JM
1070 tree declarator;
1071 tree decl;
1072 int template_count;
1073 int flags;
1074{
75650646
MM
1075 int have_def = flags & 2;
1076 int is_friend = flags & 4;
1077 int specialization = 0;
e1467ff2 1078 int explicit_instantiation = 0;
fd4de5ff 1079 int member_specialization = 0;
75650646
MM
1080
1081 tree ctype = DECL_CLASS_CONTEXT (decl);
1082 tree dname = DECL_NAME (decl);
386b8a85 1083
fd4de5ff 1084 if (processing_specialization)
386b8a85 1085 {
fd4de5ff 1086 /* The last template header was of the form template <>. */
75650646 1087
fd4de5ff
MM
1088 if (template_header_count > template_count)
1089 {
1090 /* There were more template headers than qualifying template
1091 classes. */
1092 if (template_header_count - template_count > 1)
1093 /* There shouldn't be that many template parameter
75650646
MM
1094 lists. There can be at most one parameter list for
1095 every qualifying class, plus one for the function
1096 itself. */
fd4de5ff 1097 cp_error ("too many template parameter lists in declaration of `%D'", decl);
75650646 1098
fd4de5ff
MM
1099 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1100 if (ctype)
1101 member_specialization = 1;
1102 else
1103 specialization = 1;
75650646 1104 }
fd4de5ff 1105 else if (template_header_count == template_count)
75650646 1106 {
fd4de5ff
MM
1107 /* The counts are equal. So, this might be a
1108 specialization, but it is not a specialization of a
1109 member template. It might be something like
1110
1111 template <class T> struct S {
1112 void f(int i);
1113 };
1114 template <>
1115 void S<int>::f(int i) {} */
1116 specialization = 1;
1117 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
75650646 1118 }
fd4de5ff 1119 else
386b8a85 1120 {
fd4de5ff
MM
1121 /* This cannot be an explicit specialization. There are not
1122 enough headers for all of the qualifying classes. For
1123 example, we might have:
1124
1125 template <>
1126 void S<int>::T<char>::f();
75650646 1127
fd4de5ff
MM
1128 But, we're missing another template <>. */
1129 cp_error("too few template parameter lists in declaration of `%D'", decl);
1130 return decl;
1131 }
1132 }
1133 else if (processing_explicit_instantiation)
1134 {
1135 if (template_header_count)
1136 cp_error ("template parameter list used in explicit instantiation");
1137
1138 if (have_def)
1139 cp_error ("definition provided for explicit instantiation");
75650646 1140
fd4de5ff
MM
1141 explicit_instantiation = 1;
1142 }
1143 else if (ctype != NULL_TREE
1144 && !TYPE_BEING_DEFINED (ctype)
1145 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1146 && !is_friend)
1147 {
1148 /* This case catches outdated code that looks like this:
1149
1150 template <class T> struct S { void f(); };
1151 void S<int>::f() {} // Missing template <>
1152
1153 We disable this check when the type is being defined to
1154 avoid complaining about default compiler-generated
1155 constructors, destructors, and assignment operators.
1156 Since the type is an instantiation, not a specialization,
1157 these are the only functions that can be defined before
1158 the class is complete. */
75650646 1159
c6f2ed0d
JM
1160 /* If they said
1161 template <class T> void S<int>::f() {}
1162 that's bogus. */
fd4de5ff
MM
1163 if (template_header_count)
1164 {
1165 cp_error ("template parameters specified in specialization");
1166 return decl;
c6f2ed0d 1167 }
fd4de5ff
MM
1168
1169 if (pedantic)
1170 cp_pedwarn
1171 ("explicit specialization not preceded by `template <>'");
1172 specialization = 1;
1173 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1174 }
1175 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1176 {
1177 if (is_friend)
1178 /* This could be something like:
1179
1180 template <class T> void f(T);
1181 class S { friend void f<>(int); } */
1182 specialization = 1;
1183 else
c6f2ed0d 1184 {
fd4de5ff
MM
1185 /* This case handles bogus declarations like template <>
1186 template <class T> void f<int>(); */
75650646 1187
fd4de5ff
MM
1188 cp_error ("template-id `%D' in declaration of primary template",
1189 declarator);
1190 return decl;
386b8a85 1191 }
75650646 1192 }
386b8a85 1193
670960ac
JM
1194 if (specialization || member_specialization)
1195 {
1196 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1197 for (; t; t = TREE_CHAIN (t))
1198 if (TREE_PURPOSE (t))
1199 {
8251199e
JM
1200 cp_pedwarn
1201 ("default argument specified in explicit specialization");
670960ac
JM
1202 break;
1203 }
f3400fe2
JM
1204 if (current_lang_name == lang_name_c)
1205 cp_error ("template specialization with C linkage");
670960ac
JM
1206 }
1207
e1467ff2 1208 if (specialization || member_specialization || explicit_instantiation)
75650646 1209 {
36a117a5 1210 tree gen_tmpl;
75650646
MM
1211 tree tmpl = NULL_TREE;
1212 tree targs = NULL_TREE;
75650646
MM
1213
1214 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
386b8a85
JM
1215 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1216 {
1217 tree fns;
1218
1219 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1220 0);
1221 if (!ctype)
30394414 1222 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
386b8a85
JM
1223 else
1224 fns = dname;
1225
75650646
MM
1226 declarator =
1227 lookup_template_function (fns, NULL_TREE);
386b8a85
JM
1228 }
1229
f2e48b67
BK
1230 if (declarator == error_mark_node)
1231 return error_mark_node;
1232
1233 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
386b8a85
JM
1234 {
1235 /* A friend declaration. We can't do much, because we don't
1236 know what this resolves to, yet. */
1237 my_friendly_assert (is_friend != 0, 0);
e1467ff2 1238 my_friendly_assert (!explicit_instantiation, 0);
386b8a85 1239 SET_DECL_IMPLICIT_INSTANTIATION (decl);
e1467ff2 1240 return decl;
386b8a85
JM
1241 }
1242
75650646
MM
1243 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1244 {
8ec2ac34 1245 if (!explicit_instantiation)
6c30752f
MM
1246 /* A specialization in class scope. This is illegal,
1247 but the error will already have been flagged by
1248 check_specialization_scope. */
1249 return error_mark_node;
8ec2ac34 1250 else
b370501f
KG
1251 {
1252 /* It's not legal to write an explicit instantiation in
1253 class scope, e.g.:
8ec2ac34 1254
b370501f 1255 class C { template void f(); }
8ec2ac34 1256
b370501f
KG
1257 This case is caught by the parser. However, on
1258 something like:
8ec2ac34 1259
b370501f 1260 template class C { void f(); };
8ec2ac34 1261
b370501f
KG
1262 (which is illegal) we can get here. The error will be
1263 issued later. */
1264 ;
1265 }
8ec2ac34 1266
e1467ff2 1267 return decl;
75650646
MM
1268 }
1269 else if (ctype != NULL_TREE
1270 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1271 IDENTIFIER_NODE))
386b8a85 1272 {
75650646
MM
1273 /* Find the list of functions in ctype that have the same
1274 name as the declared function. */
1275 tree name = TREE_OPERAND (declarator, 0);
386b8a85 1276 tree fns;
75650646
MM
1277
1278 if (name == constructor_name (ctype)
1279 || name == constructor_name_full (ctype))
386b8a85 1280 {
75650646
MM
1281 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1282
1283 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1284 : !TYPE_HAS_DESTRUCTOR (ctype))
1285 {
1286 /* From [temp.expl.spec]:
e1467ff2 1287
75650646
MM
1288 If such an explicit specialization for the member
1289 of a class template names an implicitly-declared
1290 special member function (clause _special_), the
e1467ff2
MM
1291 program is ill-formed.
1292
1293 Similar language is found in [temp.explicit]. */
8251199e 1294 cp_error ("specialization of implicitly-declared special member function");
75650646 1295
e1467ff2 1296 return decl;
75650646 1297 }
386b8a85 1298
42da2fd8 1299 name = is_constructor ? ctor_identifier : dtor_identifier;
75650646 1300 }
42da2fd8
MM
1301
1302 fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
386b8a85
JM
1303
1304 if (fns == NULL_TREE)
1305 {
8251199e 1306 cp_error ("no member function `%s' declared in `%T'",
75650646 1307 IDENTIFIER_POINTER (name),
386b8a85 1308 ctype);
e1467ff2 1309 return decl;
386b8a85
JM
1310 }
1311 else
1312 TREE_OPERAND (declarator, 0) = fns;
1313 }
75650646 1314
e1467ff2
MM
1315 /* Figure out what exactly is being specialized at this point.
1316 Note that for an explicit instantiation, even one for a
38e01259 1317 member function, we cannot tell apriori whether the
e1467ff2 1318 instantiation is for a member template, or just a member
36a117a5
MM
1319 function of a template class. Even if a member template is
1320 being instantiated, the member template arguments may be
1321 elided if they can be deduced from the rest of the
1322 declaration. */
e1467ff2
MM
1323 tmpl = determine_specialization (declarator, decl,
1324 &targs,
75650646
MM
1325 member_specialization,
1326 1);
386b8a85 1327
82a7448c 1328 if (tmpl && tmpl != error_mark_node)
386b8a85 1329 {
36a117a5
MM
1330 gen_tmpl = most_general_template (tmpl);
1331
e1467ff2
MM
1332 if (explicit_instantiation)
1333 {
03d0f4af
MM
1334 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1335 is done by do_decl_instantiation later. */
36a117a5 1336 decl = instantiate_template (tmpl, innermost_args (targs));
e1467ff2
MM
1337 return decl;
1338 }
36a117a5
MM
1339
1340 /* If we though that the DECL was a member function, but it
1341 turns out to be specializing a static member function,
1342 make DECL a static member function as well. */
1343 if (DECL_STATIC_FUNCTION_P (tmpl)
1344 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
366c0f1e
MM
1345 {
1346 revert_static_member_fn (&decl, 0, 0);
1347 last_function_parms = TREE_CHAIN (last_function_parms);
1348 }
e1467ff2 1349
36a117a5
MM
1350 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1351 DECL_TEMPLATE_INFO (decl)
1352 = perm_tree_cons (tmpl, targs, NULL_TREE);
1353
e1467ff2
MM
1354 /* Mangle the function name appropriately. Note that we do
1355 not mangle specializations of non-template member
1356 functions of template classes, e.g. with
36a117a5 1357
e1467ff2 1358 template <class T> struct S { void f(); }
36a117a5 1359
e1467ff2 1360 and given the specialization
36a117a5 1361
e1467ff2 1362 template <> void S<int>::f() {}
36a117a5 1363
e1467ff2
MM
1364 we do not mangle S<int>::f() here. That's because it's
1365 just an ordinary member function and doesn't need special
36a117a5
MM
1366 treatment. We do this here so that the ordinary,
1367 non-template, name-mangling algorith will not be used
1368 later. */
e1467ff2 1369 if ((is_member_template (tmpl) || ctype == NULL_TREE)
75650646 1370 && name_mangling_version >= 1)
36a117a5 1371 set_mangled_name_for_template_decl (decl);
386b8a85
JM
1372
1373 if (is_friend && !have_def)
36a117a5
MM
1374 /* This is not really a declaration of a specialization.
1375 It's just the name of an instantiation. But, it's not
1376 a request for an instantiation, either. */
fbf1c34b 1377 SET_DECL_IMPLICIT_INSTANTIATION (decl);
75650646 1378
36a117a5
MM
1379 /* Register this specialization so that we can find it
1380 again. */
1381 decl = register_specialization (decl, gen_tmpl, targs);
75650646 1382
e1467ff2 1383 return decl;
386b8a85
JM
1384 }
1385 }
75650646 1386
e1467ff2 1387 return decl;
386b8a85 1388}
75650646 1389
39c01e4c
MM
1390/* TYPE is being declared. Verify that the use of template headers
1391 and such is reasonable. Issue error messages if not. */
1392
1393void
1394maybe_check_template_type (type)
1395 tree type;
1396{
1397 if (template_header_count)
1398 {
1399 /* We are in the scope of some `template <...>' header. */
1400
1401 int context_depth
1402 = template_class_depth_real (TYPE_CONTEXT (type),
1403 /*count_specializations=*/1);
1404
1405 if (template_header_count <= context_depth)
1406 /* This is OK; the template headers are for the context. We
1407 are actually too lenient here; like
1408 check_explicit_specialization we should consider the number
1409 of template types included in the actual declaration. For
1410 example,
1411
1412 template <class T> struct S {
1413 template <class U> template <class V>
1414 struct I {};
1415 };
1416
1417 is illegal, but:
1418
1419 template <class T> struct S {
1420 template <class U> struct I;
1421 };
1422
1423 template <class T> template <class U.
1424 struct S<T>::I {};
1425
1426 is not. */
1427 ;
1428 else if (template_header_count > context_depth + 1)
1429 /* There are two many template parameter lists. */
8251199e 1430 cp_error ("too many template parameter lists in declaration of `%T'", type);
39c01e4c
MM
1431 }
1432}
1433
75650646
MM
1434/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1435 parameters. These are represented in the same format used for
1436 DECL_TEMPLATE_PARMS. */
1437
1438int comp_template_parms (parms1, parms2)
1439 tree parms1;
1440 tree parms2;
1441{
1442 tree p1;
1443 tree p2;
1444
1445 if (parms1 == parms2)
1446 return 1;
1447
1448 for (p1 = parms1, p2 = parms2;
1449 p1 != NULL_TREE && p2 != NULL_TREE;
1450 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1451 {
1452 tree t1 = TREE_VALUE (p1);
1453 tree t2 = TREE_VALUE (p2);
1454 int i;
1455
1456 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1457 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1458
1459 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1460 return 0;
1461
1462 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1463 {
1464 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1465 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1466
1467 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1468 return 0;
1469
1470 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1471 continue;
3bfdc719 1472 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
75650646
MM
1473 return 0;
1474 }
1475 }
1476
1477 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1478 /* One set of parameters has more parameters lists than the
1479 other. */
1480 return 0;
1481
1482 return 1;
1483}
1484
f3400fe2
JM
1485/* Complain if DECL shadows a template parameter.
1486
1487 [temp.local]: A template-parameter shall not be redeclared within its
1488 scope (including nested scopes). */
1489
1490void
1491check_template_shadow (decl)
1492 tree decl;
1493{
f181d4ae 1494 tree olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
f3400fe2 1495
f181d4ae
MM
1496 if (current_template_parms && olddecl)
1497 {
f3400fe2
JM
1498 /* We check for decl != olddecl to avoid bogus errors for using a
1499 name inside a class. We check TPFI to avoid duplicate errors for
1500 inline member templates. */
fc03edb3 1501 if (decl != olddecl && DECL_TEMPLATE_PARM_P (olddecl)
f3400fe2
JM
1502 && ! TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1503 {
1504 cp_error_at ("declaration of `%#D'", decl);
1505 cp_error_at (" shadows template parm `%#D'", olddecl);
1506 }
1507 }
1508}
22a7be53 1509
f3400fe2 1510/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
f84b4be9
JM
1511 ORIG_LEVEL, DECL, and TYPE. */
1512
1513static tree
1514build_template_parm_index (index, level, orig_level, decl, type)
1515 int index;
1516 int level;
1517 int orig_level;
1518 tree decl;
1519 tree type;
1520{
1521 tree t = make_node (TEMPLATE_PARM_INDEX);
1522 TEMPLATE_PARM_IDX (t) = index;
1523 TEMPLATE_PARM_LEVEL (t) = level;
1524 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1525 TEMPLATE_PARM_DECL (t) = decl;
1526 TREE_TYPE (t) = type;
1527
1528 return t;
1529}
1530
f84b4be9 1531/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
93cdc044 1532 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
f84b4be9
JM
1533 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1534 new one is created. */
1535
1536static tree
93cdc044 1537reduce_template_parm_level (index, type, levels)
f84b4be9
JM
1538 tree index;
1539 tree type;
93cdc044 1540 int levels;
f84b4be9
JM
1541{
1542 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1543 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
93cdc044 1544 != TEMPLATE_PARM_LEVEL (index) - levels))
f84b4be9
JM
1545 {
1546 tree decl
1547 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1548 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1549 type);
1550 tree t
1551 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
93cdc044 1552 TEMPLATE_PARM_LEVEL (index) - levels,
f84b4be9
JM
1553 TEMPLATE_PARM_ORIG_LEVEL (index),
1554 decl, type);
1555 TEMPLATE_PARM_DESCENDANTS (index) = t;
cae40af6
JM
1556
1557 /* Template template parameters need this. */
1558 DECL_TEMPLATE_PARMS (decl)
1559 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
f84b4be9
JM
1560 }
1561
1562 return TEMPLATE_PARM_DESCENDANTS (index);
1563}
1564
8d08fdba 1565/* Process information from new template parameter NEXT and append it to the
5566b478 1566 LIST being built. */
e92cc029 1567
8d08fdba
MS
1568tree
1569process_template_parm (list, next)
1570 tree list, next;
1571{
1572 tree parm;
1573 tree decl = 0;
a292b002 1574 tree defval;
5566b478 1575 int is_type, idx;
f84b4be9 1576
8d08fdba
MS
1577 parm = next;
1578 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
a292b002
MS
1579 defval = TREE_PURPOSE (parm);
1580 parm = TREE_VALUE (parm);
1581 is_type = TREE_PURPOSE (parm) == class_type_node;
5566b478
MS
1582
1583 if (list)
1584 {
1585 tree p = TREE_VALUE (tree_last (list));
1586
1587 if (TREE_CODE (p) == TYPE_DECL)
1588 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
73b0fce8
KL
1589 else if (TREE_CODE (p) == TEMPLATE_DECL)
1590 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
5566b478 1591 else
f84b4be9 1592 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
5566b478
MS
1593 ++idx;
1594 }
1595 else
1596 idx = 0;
1597
8d08fdba
MS
1598 if (!is_type)
1599 {
a292b002 1600 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
8d08fdba 1601 /* is a const-param */
a292b002 1602 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
c11b6f21 1603 PARM, 0, NULL_TREE);
d490621d
MM
1604
1605 /* [temp.param]
1606
1607 The top-level cv-qualifiers on the template-parameter are
1608 ignored when determining its type. */
1609 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
1610
8d08fdba
MS
1611 /* A template parameter is not modifiable. */
1612 TREE_READONLY (parm) = 1;
c91a56d2 1613 if (IS_AGGR_TYPE (TREE_TYPE (parm))
410c2fba
KL
1614 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
1615 && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
8d08fdba 1616 {
8251199e 1617 cp_error ("`%#T' is not a valid type for a template constant parameter",
c91a56d2
MS
1618 TREE_TYPE (parm));
1619 if (DECL_NAME (parm) == NULL_TREE)
8251199e 1620 error (" a template type parameter must begin with `class' or `typename'");
8d08fdba
MS
1621 TREE_TYPE (parm) = void_type_node;
1622 }
37c46b43
MS
1623 else if (pedantic
1624 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
1625 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
8251199e 1626 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
eb66be0e 1627 TREE_TYPE (parm));
8d08fdba
MS
1628 if (TREE_PERMANENT (parm) == 0)
1629 {
1630 parm = copy_node (parm);
1631 TREE_PERMANENT (parm) = 1;
1632 }
8d08fdba 1633 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
f84b4be9
JM
1634 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1635 = build_template_parm_index (idx, processing_template_decl,
1636 processing_template_decl,
1637 decl, TREE_TYPE (parm));
8d08fdba
MS
1638 }
1639 else
1640 {
73b0fce8
KL
1641 tree t;
1642 parm = TREE_VALUE (parm);
1643
1644 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1645 {
1646 t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1647 /* This is for distinguishing between real templates and template
1648 template parameters */
1649 TREE_TYPE (parm) = t;
1650 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1651 decl = parm;
1652 }
1653 else
1654 {
1655 t = make_lang_type (TEMPLATE_TYPE_PARM);
1656 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1657 decl = build_decl (TYPE_DECL, parm, t);
1658 }
1659
d2e5ee5c
MS
1660 TYPE_NAME (t) = decl;
1661 TYPE_STUB_DECL (t) = decl;
a292b002 1662 parm = decl;
f84b4be9
JM
1663 TEMPLATE_TYPE_PARM_INDEX (t)
1664 = build_template_parm_index (idx, processing_template_decl,
1665 processing_template_decl,
1666 decl, TREE_TYPE (parm));
8d08fdba 1667 }
8ccc31eb 1668 SET_DECL_ARTIFICIAL (decl);
fc03edb3 1669 DECL_TEMPLATE_PARM_P (decl) = 1;
8d08fdba 1670 pushdecl (decl);
a292b002 1671 parm = build_tree_list (defval, parm);
8d08fdba
MS
1672 return chainon (list, parm);
1673}
1674
1675/* The end of a template parameter list has been reached. Process the
1676 tree list into a parameter vector, converting each parameter into a more
1677 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1678 as PARM_DECLs. */
1679
1680tree
1681end_template_parm_list (parms)
1682 tree parms;
1683{
5566b478 1684 int nparms;
8d08fdba 1685 tree parm;
5566b478
MS
1686 tree saved_parmlist = make_tree_vec (list_length (parms));
1687
5566b478
MS
1688 current_template_parms
1689 = tree_cons (build_int_2 (0, processing_template_decl),
1690 saved_parmlist, current_template_parms);
8d08fdba
MS
1691
1692 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
5566b478 1693 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
a292b002 1694
67ffc812
MM
1695 --processing_template_parmlist;
1696
8d08fdba
MS
1697 return saved_parmlist;
1698}
1699
5566b478
MS
1700/* end_template_decl is called after a template declaration is seen. */
1701
8d08fdba 1702void
5566b478 1703end_template_decl ()
8d08fdba 1704{
386b8a85
JM
1705 reset_specialization ();
1706
5156628f 1707 if (! processing_template_decl)
73aad9b9
JM
1708 return;
1709
5566b478
MS
1710 /* This matches the pushlevel in begin_template_parm_list. */
1711 poplevel (0, 0, 0);
8d08fdba 1712
5566b478
MS
1713 --processing_template_decl;
1714 current_template_parms = TREE_CHAIN (current_template_parms);
1715 (void) get_pending_sizes (); /* Why? */
1716}
8d08fdba 1717
36a117a5
MM
1718/* Given a template argument vector containing the template PARMS.
1719 The innermost PARMS are given first. */
9a3b49ac
MS
1720
1721tree
1722current_template_args ()
5566b478 1723{
36a117a5 1724 tree header;
b370501f 1725 tree args = NULL_TREE;
36a117a5 1726 int length = TMPL_PARMS_DEPTH (current_template_parms);
98c1c668
JM
1727 int l = length;
1728
36a117a5
MM
1729 /* If there is only one level of template parameters, we do not
1730 create a TREE_VEC of TREE_VECs. Instead, we return a single
1731 TREE_VEC containing the arguments. */
1732 if (length > 1)
1733 args = make_tree_vec (length);
1734
1735 for (header = current_template_parms; header; header = TREE_CHAIN (header))
8d08fdba 1736 {
5566b478 1737 tree a = copy_node (TREE_VALUE (header));
36a117a5
MM
1738 int i;
1739
5566b478 1740 TREE_TYPE (a) = NULL_TREE;
36a117a5 1741 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
5566b478 1742 {
98c1c668
JM
1743 tree t = TREE_VEC_ELT (a, i);
1744
36a117a5 1745 /* T will be a list if we are called from within a
98c1c668
JM
1746 begin/end_template_parm_list pair, but a vector directly
1747 if within a begin/end_member_template_processing pair. */
1748 if (TREE_CODE (t) == TREE_LIST)
1749 {
1750 t = TREE_VALUE (t);
1751
73b0fce8
KL
1752 if (TREE_CODE (t) == TYPE_DECL
1753 || TREE_CODE (t) == TEMPLATE_DECL)
98c1c668
JM
1754 t = TREE_TYPE (t);
1755 else
1756 t = DECL_INITIAL (t);
36a117a5 1757 TREE_VEC_ELT (a, i) = t;
98c1c668 1758 }
5566b478 1759 }
36a117a5
MM
1760
1761 if (length > 1)
1762 TREE_VEC_ELT (args, --l) = a;
1763 else
1764 args = a;
8d08fdba
MS
1765 }
1766
9a3b49ac
MS
1767 return args;
1768}
75650646 1769
e1467ff2
MM
1770/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1771 template PARMS. Used by push_template_decl below. */
1772
75650646
MM
1773static tree
1774build_template_decl (decl, parms)
1775 tree decl;
1776 tree parms;
1777{
1778 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
1779 DECL_TEMPLATE_PARMS (tmpl) = parms;
1780 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
1781 if (DECL_LANG_SPECIFIC (decl))
1782 {
1783 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
1784 DECL_STATIC_FUNCTION_P (tmpl) =
1785 DECL_STATIC_FUNCTION_P (decl);
1786 }
1787
1788 return tmpl;
1789}
1790
050367a3
MM
1791struct template_parm_data
1792{
6c30752f
MM
1793 /* The level of the template parameters we are currently
1794 processing. */
050367a3 1795 int level;
6c30752f
MM
1796
1797 /* The index of the specialization argument we are currently
1798 processing. */
1799 int current_arg;
1800
1801 /* An array whose size is the number of template parameters. The
1802 elements are non-zero if the parameter has been used in any one
1803 of the arguments processed so far. */
050367a3 1804 int* parms;
6c30752f
MM
1805
1806 /* An array whose size is the number of template arguments. The
1807 elements are non-zero if the argument makes use of template
1808 parameters of this level. */
1809 int* arg_uses_template_parms;
050367a3
MM
1810};
1811
1812/* Subroutine of push_template_decl used to see if each template
1813 parameter in a partial specialization is used in the explicit
1814 argument list. If T is of the LEVEL given in DATA (which is
1815 treated as a template_parm_data*), then DATA->PARMS is marked
1816 appropriately. */
1817
1818static int
1819mark_template_parm (t, data)
1820 tree t;
1821 void* data;
1822{
1823 int level;
1824 int idx;
1825 struct template_parm_data* tpd = (struct template_parm_data*) data;
1826
1827 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
1828 {
1829 level = TEMPLATE_PARM_LEVEL (t);
1830 idx = TEMPLATE_PARM_IDX (t);
1831 }
1832 else
1833 {
1834 level = TEMPLATE_TYPE_LEVEL (t);
1835 idx = TEMPLATE_TYPE_IDX (t);
1836 }
1837
1838 if (level == tpd->level)
6c30752f
MM
1839 {
1840 tpd->parms[idx] = 1;
1841 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
1842 }
050367a3
MM
1843
1844 /* Return zero so that for_each_template_parm will continue the
1845 traversal of the tree; we want to mark *every* template parm. */
1846 return 0;
1847}
1848
6c30752f
MM
1849/* Process the partial specialization DECL. */
1850
1851tree
1852process_partial_specialization (decl)
1853 tree decl;
1854{
1855 tree type = TREE_TYPE (decl);
1856 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
1857 tree specargs = CLASSTYPE_TI_ARGS (type);
1858 tree inner_args = innermost_args (specargs);
1859 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
1860 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
1861 int nargs = TREE_VEC_LENGTH (inner_args);
1862 int ntparms = TREE_VEC_LENGTH (inner_parms);
1863 int i;
1864 int did_error_intro = 0;
6c30752f
MM
1865 struct template_parm_data tpd;
1866 struct template_parm_data tpd2;
1867
6c30752f
MM
1868 /* We check that each of the template parameters given in the
1869 partial specialization is used in the argument list to the
1870 specialization. For example:
1871
1872 template <class T> struct S;
1873 template <class T> struct S<T*>;
1874
1875 The second declaration is OK because `T*' uses the template
1876 parameter T, whereas
1877
1878 template <class T> struct S<int>;
1879
1880 is no good. Even trickier is:
1881
1882 template <class T>
1883 struct S1
1884 {
1885 template <class U>
1886 struct S2;
1887 template <class U>
1888 struct S2<T>;
1889 };
1890
1891 The S2<T> declaration is actually illegal; it is a
1892 full-specialization. Of course,
1893
1894 template <class U>
1895 struct S2<T (*)(U)>;
1896
1897 or some such would have been OK. */
1898 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
1899 tpd.parms = alloca (sizeof (int) * ntparms);
1ddb2906 1900 bzero ((PTR) tpd.parms, sizeof (int) * ntparms);
6c30752f
MM
1901
1902 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
1ddb2906 1903 bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs);
6c30752f
MM
1904 for (i = 0; i < nargs; ++i)
1905 {
1906 tpd.current_arg = i;
1907 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
1908 &mark_template_parm,
1909 &tpd);
1910 }
1911 for (i = 0; i < ntparms; ++i)
1912 if (tpd.parms[i] == 0)
1913 {
1914 /* One of the template parms was not used in the
1915 specialization. */
1916 if (!did_error_intro)
1917 {
8251199e 1918 cp_error ("template parameters not used in partial specialization:");
6c30752f
MM
1919 did_error_intro = 1;
1920 }
1921
8251199e 1922 cp_error (" `%D'",
6c30752f
MM
1923 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
1924 }
1925
1926 /* [temp.class.spec]
1927
1928 The argument list of the specialization shall not be identical to
1929 the implicit argument list of the primary template. */
1930 if (comp_template_args (inner_args,
1931 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
1932 (maintmpl)))))
8251199e 1933 cp_error ("partial specialization `%T' does not specialize any template arguments", type);
6c30752f
MM
1934
1935 /* [temp.class.spec]
1936
1937 A partially specialized non-type argument expression shall not
1938 involve template parameters of the partial specialization except
1939 when the argument expression is a simple identifier.
1940
1941 The type of a template parameter corresponding to a specialized
1942 non-type argument shall not be dependent on a parameter of the
1943 specialization. */
1944 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
1945 tpd2.parms = 0;
1946 for (i = 0; i < nargs; ++i)
1947 {
1948 tree arg = TREE_VEC_ELT (inner_args, i);
1949 if (/* These first two lines are the `non-type' bit. */
1950 TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
1951 && TREE_CODE (arg) != TEMPLATE_DECL
1952 /* This next line is the `argument expression is not just a
1953 simple identifier' condition and also the `specialized
1954 non-type argument' bit. */
1955 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
1956 {
1957 if (tpd.arg_uses_template_parms[i])
8251199e 1958 cp_error ("template argument `%E' involves template parameter(s)", arg);
6c30752f
MM
1959 else
1960 {
1961 /* Look at the corresponding template parameter,
1962 marking which template parameters its type depends
1963 upon. */
1964 tree type =
1965 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
1966 i)));
1967
1968 if (!tpd2.parms)
1969 {
1970 /* We haven't yet initialized TPD2. Do so now. */
1971 tpd2.arg_uses_template_parms
1972 = (int*) alloca (sizeof (int) * nargs);
104bf76a 1973 /* The number of parameters here is the number in the
76a83782
RH
1974 main template, which, as checked in the assertion
1975 above, is NARGS. */
6c30752f
MM
1976 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
1977 tpd2.level =
1978 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
1979 }
1980
104bf76a 1981 /* Mark the template parameters. But this time, we're
6c30752f
MM
1982 looking for the template parameters of the main
1983 template, not in the specialization. */
1984 tpd2.current_arg = i;
1985 tpd2.arg_uses_template_parms[i] = 0;
1ddb2906 1986 bzero ((PTR) tpd2.parms, sizeof (int) * nargs);
6c30752f
MM
1987 for_each_template_parm (type,
1988 &mark_template_parm,
1989 &tpd2);
1990
1991 if (tpd2.arg_uses_template_parms [i])
1992 {
1993 /* The type depended on some template parameters.
1994 If they are fully specialized in the
1995 specialization, that's OK. */
1996 int j;
1997 for (j = 0; j < nargs; ++j)
1998 if (tpd2.parms[j] != 0
1999 && tpd.arg_uses_template_parms [j])
2000 {
104bf76a 2001 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
6c30752f
MM
2002 type,
2003 arg);
2004 break;
2005 }
2006 }
2007 }
2008 }
2009 }
2010
2011 if (retrieve_specialization (maintmpl, specargs))
2012 /* We've already got this specialization. */
2013 return decl;
2014
2015 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
2016 = perm_tree_cons (inner_args, inner_parms,
2017 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2018 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2019 return decl;
2020}
2021
6ba89f8e
MM
2022/* Check that a template declaration's use of default arguments is not
2023 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2024 non-zero if DECL is the thing declared by a primary template.
2025 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2026
2027static void
2028check_default_tmpl_args (decl, parms, is_primary, is_partial)
2029 tree decl;
2030 tree parms;
2031 int is_primary;
2032 int is_partial;
2033{
2034 char* msg;
2035 int last_level_to_check;
2036
2037 /* [temp.param]
2038
2039 A default template-argument shall not be specified in a
2040 function template declaration or a function template definition, nor
2041 in the template-parameter-list of the definition of a member of a
2042 class template. */
2043
2044 if (current_class_type
2045 && !TYPE_BEING_DEFINED (current_class_type)
2046 && DECL_REAL_CONTEXT (decl) == current_class_type
2047 && DECL_DEFINED_IN_CLASS_P (decl))
2048 /* We already checked these parameters when the template was
2049 declared, so there's no need to do it again now. This is an
2050 inline member function definition. */
2051 return;
2052
2053 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2054 /* For an ordinary class template, default template arguments are
2055 allowed at the innermost level, e.g.:
2056 template <class T = int>
2057 struct S {};
2058 but, in a partial specialization, they're not allowed even
2059 there, as we have in [temp.class.spec]:
2060
2061 The template parameter list of a specialization shall not
2062 contain default template argument values.
2063
2064 So, for a partial specialization, or for a function template,
2065 we look at all of them. */
2066 ;
2067 else
2068 /* But, for a primary class template that is not a partial
2069 specialization we look at all template parameters except the
2070 innermost ones. */
2071 parms = TREE_CHAIN (parms);
2072
2073 /* Figure out what error message to issue. */
2074 if (TREE_CODE (decl) == FUNCTION_DECL)
2075 msg = "default argument for template parameter in function template `%D'";
2076 else if (is_partial)
2077 msg = "default argument in partial specialization `%D'";
2078 else
2079 msg = "default argument for template parameter for class enclosing `%D'";
2080
2081 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2082 /* If we're inside a class definition, there's no need to
104bf76a 2083 examine the parameters to the class itself. On the one
6ba89f8e
MM
2084 hand, they will be checked when the class is defined, and,
2085 on the other, default arguments are legal in things like:
2086 template <class T = double>
2087 struct S { template <class U> void f(U); };
2088 Here the default argument for `S' has no bearing on the
2089 declaration of `f'. */
2090 last_level_to_check = template_class_depth (current_class_type) + 1;
2091 else
2092 /* Check everything. */
2093 last_level_to_check = 0;
2094
2095 for (; parms && TMPL_PARMS_DEPTH (parms) >= last_level_to_check;
2096 parms = TREE_CHAIN (parms))
2097 {
2098 tree inner_parms = TREE_VALUE (parms);
2099 int i, ntparms;
2100
2101 ntparms = TREE_VEC_LENGTH (inner_parms);
2102 for (i = 0; i < ntparms; ++i)
2103 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2104 {
2105 if (msg)
2106 {
2107 cp_error (msg, decl);
2108 msg = 0;
2109 }
2110
2111 /* Clear out the default argument so that we are not
2112 confused later. */
2113 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2114 }
2115
2116 /* At this point, if we're still interested in issuing messages,
2117 they must apply to classes surrounding the object declared. */
2118 if (msg)
2119 msg = "default argument for template parameter for class enclosing `%D'";
2120 }
2121}
2122
3ac3d9ea 2123/* Creates a TEMPLATE_DECL for the indicated DECL using the template
f84b4be9
JM
2124 parameters given by current_template_args, or reuses a
2125 previously existing one, if appropriate. Returns the DECL, or an
6757edfe
MM
2126 equivalent one, if it is replaced via a call to duplicate_decls.
2127
2128 If IS_FRIEND is non-zero, DECL is a friend declaration. */
3ac3d9ea
MM
2129
2130tree
6757edfe 2131push_template_decl_real (decl, is_friend)
9a3b49ac 2132 tree decl;
6757edfe 2133 int is_friend;
9a3b49ac
MS
2134{
2135 tree tmpl;
f84b4be9 2136 tree args;
9a3b49ac 2137 tree info;
f84b4be9
JM
2138 tree ctx;
2139 int primary;
6ba89f8e
MM
2140 int is_partial;
2141
2142 /* See if this is a partial specialization. */
2143 is_partial = (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
2144 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2145 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
6757edfe
MM
2146
2147 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
f84b4be9
JM
2148
2149 if (is_friend)
2150 /* For a friend, we want the context of the friend function, not
2151 the type of which it is a friend. */
2152 ctx = DECL_CONTEXT (decl);
2c73f9f5
ML
2153 else if (DECL_REAL_CONTEXT (decl)
2154 && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
f84b4be9
JM
2155 /* In the case of a virtual function, we want the class in which
2156 it is defined. */
2157 ctx = DECL_REAL_CONTEXT (decl);
2158 else
2159 /* Otherwise, if we're currently definining some class, the DECL
2160 is assumed to be a member of the class. */
2161 ctx = current_class_type;
2162
2c73f9f5
ML
2163 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2164 ctx = NULL_TREE;
2165
2166 if (!DECL_CONTEXT (decl))
cb0dbb9a 2167 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 2168
93cdc044
JM
2169 /* For determining whether this is a primary template or not, we're really
2170 interested in the lexical context, not the true context. */
2171 if (is_friend)
36a117a5 2172 info = current_class_type;
93cdc044
JM
2173 else
2174 info = ctx;
2175
6ba89f8e 2176 /* See if this is a primary template. */
93cdc044
JM
2177 if (info && TREE_CODE (info) == FUNCTION_DECL)
2178 primary = 0;
6757edfe
MM
2179 /* Note that template_class_depth returns 0 if given NULL_TREE, so
2180 this next line works even when we are at global scope. */
2181 else if (processing_template_decl > template_class_depth (info))
9a3b49ac 2182 primary = 1;
f84b4be9
JM
2183 else
2184 primary = 0;
9a3b49ac 2185
83566abf
JM
2186 if (primary)
2187 {
2188 if (current_lang_name == lang_name_c)
8251199e 2189 cp_error ("template with C linkage");
83566abf 2190 if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl)))
8251199e 2191 cp_error ("template class without a name");
ed44da02
MM
2192 if (TREE_CODE (decl) == TYPE_DECL
2193 && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
8251199e 2194 cp_error ("template declaration of `%#T'", TREE_TYPE (decl));
83566abf
JM
2195 }
2196
6ba89f8e
MM
2197 /* Check to see that the rules regarding the use of default
2198 arguments are not being violated. */
2199 check_default_tmpl_args (decl, current_template_parms,
2200 primary, is_partial);
73aad9b9 2201
6ba89f8e
MM
2202 if (is_partial)
2203 return process_partial_specialization (decl);
d32789d8 2204
9a3b49ac
MS
2205 args = current_template_args ();
2206
f84b4be9
JM
2207 if (!ctx
2208 || TREE_CODE (ctx) == FUNCTION_DECL
2209 || TYPE_BEING_DEFINED (ctx)
2210 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
8d08fdba 2211 {
75650646 2212 if (DECL_LANG_SPECIFIC (decl)
f84b4be9
JM
2213 && DECL_TEMPLATE_INFO (decl)
2214 && DECL_TI_TEMPLATE (decl))
2215 tmpl = DECL_TI_TEMPLATE (decl);
2216 else
786b5245 2217 {
f84b4be9
JM
2218 tmpl = build_template_decl (decl, current_template_parms);
2219
2220 if (DECL_LANG_SPECIFIC (decl)
2221 && DECL_TEMPLATE_SPECIALIZATION (decl))
2222 {
2223 /* A specialization of a member template of a template
2224 class. */
2225 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2226 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2227 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2228 }
786b5245 2229 }
8d08fdba
MS
2230 }
2231 else
2232 {
e1a5ccf7 2233 tree a, t, current, parms;
ba4f4e5d 2234 int i;
6633d636 2235
73aad9b9 2236 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
8251199e 2237 cp_error ("must specialize `%#T' before defining member `%#D'",
73aad9b9 2238 ctx, decl);
6b9ab5cc
MM
2239 if (TREE_CODE (decl) == TYPE_DECL)
2240 {
ed44da02
MM
2241 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2242 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2243 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2244 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2245 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6b9ab5cc
MM
2246 else
2247 {
8251199e 2248 cp_error ("`%D' does not declare a template type", decl);
6b9ab5cc
MM
2249 return decl;
2250 }
2251 }
fc378698 2252 else if (! DECL_TEMPLATE_INFO (decl))
c91a56d2 2253 {
8251199e 2254 cp_error ("template definition of non-template `%#D'", decl);
3ac3d9ea 2255 return decl;
c91a56d2 2256 }
8d08fdba 2257 else
5566b478 2258 tmpl = DECL_TI_TEMPLATE (decl);
98c1c668 2259
e1a5ccf7
JM
2260 if (is_member_template (tmpl)
2261 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2262 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2263 && DECL_TEMPLATE_SPECIALIZATION (decl))
98c1c668 2264 {
e1a5ccf7
JM
2265 tree new_tmpl;
2266
2267 /* The declaration is a specialization of a member
2268 template, declared outside the class. Therefore, the
2269 innermost template arguments will be NULL, so we
2270 replace them with the arguments determined by the
2271 earlier call to check_explicit_specialization. */
2272 args = DECL_TI_ARGS (decl);
2273
2274 new_tmpl
2275 = build_template_decl (decl, current_template_parms);
2276 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2277 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2278 DECL_TI_TEMPLATE (decl) = new_tmpl;
2279 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2280 DECL_TEMPLATE_INFO (new_tmpl) =
2281 perm_tree_cons (tmpl, args, NULL_TREE);
2282
2283 register_specialization (new_tmpl, tmpl, args);
2284 return decl;
98c1c668 2285 }
98c1c668 2286
e1a5ccf7 2287 /* Make sure the template headers we got make sense. */
6633d636 2288
e1a5ccf7
JM
2289 parms = DECL_TEMPLATE_PARMS (tmpl);
2290 i = TMPL_PARMS_DEPTH (parms);
2291 if (TMPL_ARGS_DEPTH (args) != i)
98c1c668 2292 {
e1a5ccf7
JM
2293 cp_error ("expected %d levels of template parms for `%#D', got %d",
2294 i, decl, TMPL_ARGS_DEPTH (args));
98c1c668 2295 }
e1a5ccf7
JM
2296 else
2297 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2298 {
2299 a = TMPL_ARGS_LEVEL (args, i);
2300 t = INNERMOST_TEMPLATE_PARMS (parms);
2301
2302 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2303 {
2304 if (current == decl)
2305 cp_error ("got %d template parameters for `%#D'",
2306 TREE_VEC_LENGTH (a), decl);
2307 else
2308 cp_error ("got %d template parameters for `%#T'",
2309 TREE_VEC_LENGTH (a), current);
2310 cp_error (" but %d required", TREE_VEC_LENGTH (t));
2311 }
98c1c668 2312
e1a5ccf7
JM
2313 /* Perhaps we should also check that the parms are used in the
2314 appropriate qualifying scopes in the declarator? */
6633d636 2315
e1a5ccf7
JM
2316 if (current == decl)
2317 current = ctx;
2318 else
2319 current = TYPE_CONTEXT (current);
2320 }
5566b478 2321 }
8d08fdba 2322
5566b478
MS
2323 DECL_TEMPLATE_RESULT (tmpl) = decl;
2324 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 2325
36a117a5
MM
2326 /* Push template declarations for global functions and types. Note
2327 that we do not try to push a global template friend declared in a
2328 template class; such a thing may well depend on the template
39c01e4c 2329 parameters of the class. */
36a117a5
MM
2330 if (! ctx
2331 && !(is_friend && template_class_depth (current_class_type) > 0))
2c73f9f5 2332 tmpl = pushdecl_namespace_level (tmpl);
8d08fdba 2333
5566b478 2334 if (primary)
6757edfe 2335 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5566b478
MS
2336
2337 info = perm_tree_cons (tmpl, args, NULL_TREE);
2338
824b9a4c 2339 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
8d08fdba 2340 {
ed44da02
MM
2341 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2342 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2343 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE)
75650646 2344 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
51c184be 2345 }
ec255269 2346 else if (! DECL_LANG_SPECIFIC (decl))
8251199e 2347 cp_error ("template declaration of `%#D'", decl);
51c184be 2348 else
5566b478 2349 DECL_TEMPLATE_INFO (decl) = info;
3ac3d9ea
MM
2350
2351 return DECL_TEMPLATE_RESULT (tmpl);
8d08fdba
MS
2352}
2353
6757edfe
MM
2354tree
2355push_template_decl (decl)
2356 tree decl;
2357{
2358 return push_template_decl_real (decl, 0);
2359}
2360
2361/* Called when a class template TYPE is redeclared with the indicated
2362 template PARMS, e.g.:
7fe6899f
MM
2363
2364 template <class T> struct S;
2365 template <class T> struct S {}; */
2366
2367void
6757edfe 2368redeclare_class_template (type, parms)
7fe6899f 2369 tree type;
6757edfe 2370 tree parms;
7fe6899f
MM
2371{
2372 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
6757edfe 2373 tree tmpl_parms;
7fe6899f
MM
2374 int i;
2375
2376 if (!PRIMARY_TEMPLATE_P (tmpl))
2377 /* The type is nested in some template class. Nothing to worry
2378 about here; there are no new template parameters for the nested
2379 type. */
2380 return;
2381
6757edfe
MM
2382 parms = INNERMOST_TEMPLATE_PARMS (parms);
2383 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2384
7fe6899f
MM
2385 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2386 {
8251199e
JM
2387 cp_error_at ("previous declaration `%D'", tmpl);
2388 cp_error ("used %d template parameter%s instead of %d",
7fe6899f
MM
2389 TREE_VEC_LENGTH (tmpl_parms),
2390 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2391 TREE_VEC_LENGTH (parms));
2392 return;
2393 }
2394
2395 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2396 {
2397 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2398 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2399 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2400 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2401
2402 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2403 {
8251199e
JM
2404 cp_error_at ("template parameter `%#D'", tmpl_parm);
2405 cp_error ("redeclared here as `%#D'", parm);
7fe6899f
MM
2406 return;
2407 }
2408
2409 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2410 {
2411 /* We have in [temp.param]:
2412
2413 A template-parameter may not be given default arguments
2414 by two different declarations in the same scope. */
8251199e
JM
2415 cp_error ("redefinition of default argument for `%#D'", parm);
2416 cp_error_at (" original definition appeared here", tmpl_parm);
7fe6899f
MM
2417 return;
2418 }
2419
2420 if (parm_default != NULL_TREE)
2421 /* Update the previous template parameters (which are the ones
2422 that will really count) with the new default value. */
2423 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2424 }
2425}
75650646 2426
75650646
MM
2427/* Attempt to convert the non-type template parameter EXPR to the
2428 indicated TYPE. If the conversion is successful, return the
2429 converted value. If the conversion is unsuccesful, return
2430 NULL_TREE if we issued an error message, or error_mark_node if we
2431 did not. We issue error messages for out-and-out bad template
2432 parameters, but not simply because the conversion failed, since we
2433 might be just trying to do argument deduction. By the time this
2434 function is called, neither TYPE nor EXPR may make use of template
2435 parameters. */
2436
2437static tree
e1467ff2 2438convert_nontype_argument (type, expr)
75650646
MM
2439 tree type;
2440 tree expr;
2441{
2442 tree expr_type = TREE_TYPE (expr);
2443
2444 /* A template-argument for a non-type, non-template
2445 template-parameter shall be one of:
2446
2447 --an integral constant-expression of integral or enumeration
2448 type; or
2449
2450 --the name of a non-type template-parameter; or
2451
2452 --the name of an object or function with external linkage,
2453 including function templates and function template-ids but
86052cc3 2454 excluding non-static class members, expressed as id-expression;
75650646
MM
2455 or
2456
2457 --the address of an object or function with external linkage,
2458 including function templates and function template-ids but
2459 excluding non-static class members, expressed as & id-expression
2460 where the & is optional if the name refers to a function or
2461 array; or
2462
2463 --a pointer to member expressed as described in _expr.unary.op_. */
2464
86052cc3
JM
2465 /* An integral constant-expression can include const variables
2466 or enumerators. */
2467 if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr))
2468 expr = decl_constant_value (expr);
2469
7bf2682f
MM
2470 if (is_overloaded_fn (expr))
2471 /* OK for now. We'll check that it has external linkage later.
2472 Check this first since if expr_type is the unknown_type_node
2473 we would otherwise complain below. */
2474 ;
f3400fe2
JM
2475 else if (TYPE_PTR_P (expr_type)
2476 || TREE_CODE (expr_type) == ARRAY_TYPE
2477 || TREE_CODE (type) == REFERENCE_TYPE
75650646
MM
2478 /* If expr is the address of an overloaded function, we
2479 will get the unknown_type_node at this point. */
2480 || expr_type == unknown_type_node)
2481 {
2482 tree referent;
49bf0d6f
MM
2483 tree e = expr;
2484 STRIP_NOPS (e);
75650646 2485
f3400fe2
JM
2486 if (TREE_CODE (type) == REFERENCE_TYPE
2487 || TREE_CODE (expr_type) == ARRAY_TYPE)
2488 referent = e;
2489 else
75650646 2490 {
f3400fe2
JM
2491 if (TREE_CODE (e) != ADDR_EXPR)
2492 {
2493 bad_argument:
2494 cp_error ("`%E' is not a valid template argument", expr);
2495 error ("it must be %s%s with external linkage",
2496 TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
2497 ? "a pointer to " : "",
2498 TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE
2499 ? "a function" : "an object");
2500 return NULL_TREE;
2501 }
2502
2503 referent = TREE_OPERAND (e, 0);
2504 STRIP_NOPS (referent);
75650646
MM
2505 }
2506
75650646
MM
2507 if (TREE_CODE (referent) == STRING_CST)
2508 {
8251199e 2509 cp_error ("string literal %E is not a valid template argument",
75650646 2510 referent);
8251199e 2511 error ("because it is the address of an object with static linkage");
75650646
MM
2512 return NULL_TREE;
2513 }
2514
2515 if (is_overloaded_fn (referent))
2516 /* We'll check that it has external linkage later. */
2517 ;
2518 else if (TREE_CODE (referent) != VAR_DECL)
2519 goto bad_argument;
2520 else if (!TREE_PUBLIC (referent))
2521 {
8251199e 2522 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
75650646
MM
2523 return error_mark_node;
2524 }
2525 }
f3400fe2
JM
2526 else if (INTEGRAL_TYPE_P (expr_type)
2527 || TYPE_PTRMEM_P (expr_type)
2528 || TYPE_PTRMEMFUNC_P (expr_type)
2529 /* The next two are g++ extensions. */
2530 || TREE_CODE (expr_type) == REAL_TYPE
2531 || TREE_CODE (expr_type) == COMPLEX_TYPE)
75650646 2532 {
f3400fe2
JM
2533 if (! TREE_CONSTANT (expr))
2534 {
2535 non_constant:
2536 cp_error ("non-constant `%E' cannot be used as template argument",
2537 expr);
2538 return NULL_TREE;
2539 }
75650646 2540 }
75650646
MM
2541 else
2542 {
8251199e 2543 cp_error ("object `%E' cannot be used as template argument", expr);
75650646
MM
2544 return NULL_TREE;
2545 }
2546
2547 switch (TREE_CODE (type))
2548 {
2549 case INTEGER_TYPE:
2550 case BOOLEAN_TYPE:
2551 case ENUMERAL_TYPE:
2552 /* For a non-type template-parameter of integral or enumeration
2553 type, integral promotions (_conv.prom_) and integral
2554 conversions (_conv.integral_) are applied. */
2555 if (!INTEGRAL_TYPE_P (expr_type))
2556 return error_mark_node;
2557
2558 /* It's safe to call digest_init in this case; we know we're
2559 just converting one integral constant expression to another. */
db02b6b9 2560 expr = digest_init (type, expr, (tree*) 0);
75650646 2561
db02b6b9 2562 if (TREE_CODE (expr) != INTEGER_CST)
f3400fe2 2563 /* Curiously, some TREE_CONSTANT integral expressions do not
db02b6b9
MM
2564 simplify to integer constants. For example, `3 % 0',
2565 remains a TRUNC_MOD_EXPR. */
2566 goto non_constant;
2567
2568 return expr;
2569
abff8e06
JM
2570 case REAL_TYPE:
2571 case COMPLEX_TYPE:
2572 /* These are g++ extensions. */
2573 if (TREE_CODE (expr_type) != TREE_CODE (type))
2574 return error_mark_node;
2575
db02b6b9
MM
2576 expr = digest_init (type, expr, (tree*) 0);
2577
2578 if (TREE_CODE (expr) != REAL_CST)
2579 goto non_constant;
2580
2581 return expr;
abff8e06 2582
75650646
MM
2583 case POINTER_TYPE:
2584 {
2585 tree type_pointed_to = TREE_TYPE (type);
2586
2587 if (TYPE_PTRMEM_P (type))
2588 /* For a non-type template-parameter of type pointer to data
2589 member, qualification conversions (_conv.qual_) are
2590 applied. */
2591 return perform_qualification_conversions (type, expr);
2592 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
2593 {
2594 /* For a non-type template-parameter of type pointer to
2595 function, only the function-to-pointer conversion
2596 (_conv.func_) is applied. If the template-argument
2597 represents a set of overloaded functions (or a pointer to
2598 such), the matching function is selected from the set
2599 (_over.over_). */
2600 tree fns;
2601 tree fn;
2602
7bf2682f 2603 if (TREE_CODE (expr) == ADDR_EXPR)
75650646
MM
2604 fns = TREE_OPERAND (expr, 0);
2605 else
2606 fns = expr;
2607
a05273d4 2608 fn = instantiate_type (type_pointed_to, fns, 0);
75650646
MM
2609
2610 if (fn == error_mark_node)
2611 return error_mark_node;
2612
2613 if (!TREE_PUBLIC (fn))
2614 {
2615 if (really_overloaded_fn (fns))
2616 return error_mark_node;
2617 else
2618 goto bad_argument;
2619 }
2620
2621 expr = build_unary_op (ADDR_EXPR, fn, 0);
2622
3bfdc719 2623 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
75650646
MM
2624 0);
2625 return expr;
2626 }
2627 else
2628 {
2629 /* For a non-type template-parameter of type pointer to
2630 object, qualification conversions (_conv.qual_) and the
2631 array-to-pointer conversion (_conv.array_) are applied.
2632 [Note: In particular, neither the null pointer conversion
2633 (_conv.ptr_) nor the derived-to-base conversion
2634 (_conv.ptr_) are applied. Although 0 is a valid
2635 template-argument for a non-type template-parameter of
2636 integral type, it is not a valid template-argument for a
e1467ff2
MM
2637 non-type template-parameter of pointer type.]
2638
2639 The call to decay_conversion performs the
2640 array-to-pointer conversion, if appropriate. */
2641 expr = decay_conversion (expr);
75650646
MM
2642
2643 if (expr == error_mark_node)
2644 return error_mark_node;
2645 else
2646 return perform_qualification_conversions (type, expr);
2647 }
2648 }
2649 break;
2650
2651 case REFERENCE_TYPE:
2652 {
2653 tree type_referred_to = TREE_TYPE (type);
2654
2655 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
2656 {
2657 /* For a non-type template-parameter of type reference to
39c01e4c
MM
2658 function, no conversions apply. If the
2659 template-argument represents a set of overloaded
2660 functions, the matching function is selected from the
2661 set (_over.over_). */
75650646
MM
2662 tree fns = expr;
2663 tree fn;
2664
a05273d4 2665 fn = instantiate_type (type_referred_to, fns, 0);
e6e174e5
JM
2666
2667 if (fn == error_mark_node)
2668 return error_mark_node;
75650646
MM
2669
2670 if (!TREE_PUBLIC (fn))
2671 {
2672 if (really_overloaded_fn (fns))
2673 /* Don't issue an error here; we might get a different
2674 function if the overloading had worked out
2675 differently. */
2676 return error_mark_node;
2677 else
2678 goto bad_argument;
2679 }
2680
3bfdc719
MM
2681 my_friendly_assert (same_type_p (type_referred_to,
2682 TREE_TYPE (fn)),
75650646
MM
2683 0);
2684
2685 return fn;
2686 }
2687 else
2688 {
2689 /* For a non-type template-parameter of type reference to
2690 object, no conversions apply. The type referred to by the
2691 reference may be more cv-qualified than the (otherwise
2692 identical) type of the template-argument. The
2693 template-parameter is bound directly to the
2694 template-argument, which must be an lvalue. */
ff350acd
JM
2695 if ((TYPE_MAIN_VARIANT (expr_type)
2696 != TYPE_MAIN_VARIANT (type_referred_to))
91063b51
MM
2697 || !at_least_as_qualified_p (type_referred_to,
2698 expr_type)
75650646
MM
2699 || !real_lvalue_p (expr))
2700 return error_mark_node;
2701 else
2702 return expr;
2703 }
2704 }
2705 break;
2706
2707 case RECORD_TYPE:
2708 {
410c2fba
KL
2709 if (!TYPE_PTRMEMFUNC_P (type))
2710 /* This handles templates like
2711 template<class T, T t> void f();
2712 when T is substituted with any class. The second template
2713 parameter becomes invalid and the template candidate is
2714 rejected. */
2715 return error_mark_node;
75650646
MM
2716
2717 /* For a non-type template-parameter of type pointer to member
2718 function, no conversions apply. If the template-argument
2719 represents a set of overloaded member functions, the
2720 matching member function is selected from the set
2721 (_over.over_). */
2722
2723 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
2724 expr_type != unknown_type_node)
2725 return error_mark_node;
2726
2727 if (TREE_CODE (expr) == CONSTRUCTOR)
2728 {
2729 /* A ptr-to-member constant. */
3bfdc719 2730 if (!same_type_p (type, expr_type))
75650646
MM
2731 return error_mark_node;
2732 else
2733 return expr;
2734 }
2735
2736 if (TREE_CODE (expr) != ADDR_EXPR)
2737 return error_mark_node;
2738
d8f8dca1 2739 expr = instantiate_type (type, expr, 0);
75650646 2740
d8f8dca1 2741 if (expr == error_mark_node)
75650646
MM
2742 return error_mark_node;
2743
3bfdc719 2744 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
75650646
MM
2745 0);
2746 return expr;
2747 }
2748 break;
2749
2750 default:
2751 /* All non-type parameters must have one of these types. */
2752 my_friendly_abort (0);
2753 break;
2754 }
2755
2756 return error_mark_node;
2757}
2758
744fac59
KL
2759/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2760 template template parameters. Both PARM_PARMS and ARG_PARMS are
2761 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2762 or PARM_DECL.
2763
2764 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2765 the case, then extra parameters must have default arguments.
2766
2767 Consider the example:
2768 template <class T, class Allocator = allocator> class vector;
2769 template<template <class U> class TT> class C;
2770
2771 C<vector> is a valid instantiation. PARM_PARMS for the above code
2772 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2773 T and Allocator) and OUTER_ARGS contains the argument that is used to
2774 substitute the TT parameter. */
2775
2776static int
2777coerce_template_template_parms (parm_parms, arg_parms, in_decl, outer_args)
2778 tree parm_parms, arg_parms, in_decl, outer_args;
2779{
2780 int nparms, nargs, i;
2781 tree parm, arg;
2782
2783 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
2784 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
2785
2786 nparms = TREE_VEC_LENGTH (parm_parms);
2787 nargs = TREE_VEC_LENGTH (arg_parms);
2788
2789 /* The rule here is opposite of coerce_template_parms. */
2790 if (nargs < nparms
2791 || (nargs > nparms
2792 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
2793 return 0;
2794
2795 for (i = 0; i < nparms; ++i)
2796 {
2797 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
2798 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
2799
2800 if (arg == NULL_TREE || arg == error_mark_node
2801 || parm == NULL_TREE || parm == error_mark_node)
2802 return 0;
2803
2804 if (TREE_CODE (arg) != TREE_CODE (parm))
2805 return 0;
2806
2807 switch (TREE_CODE (parm))
2808 {
2809 case TYPE_DECL:
2810 break;
2811
2812 case TEMPLATE_DECL:
2813 /* We encounter instantiations of templates like
2814 template <template <template <class> class> class TT>
2815 class C; */
700466c2
JM
2816 {
2817 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
2818 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
2819
2820 if (!coerce_template_template_parms (parmparm, argparm,
2821 in_decl, outer_args))
2822 return 0;
2823 }
2824 break;
744fac59
KL
2825
2826 case PARM_DECL:
2827 /* The tsubst call is used to handle cases such as
2828 template <class T, template <T> class TT> class D;
2829 i.e. the parameter list of TT depends on earlier parameters. */
3bfdc719
MM
2830 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args, in_decl),
2831 TREE_TYPE (arg)))
744fac59
KL
2832 return 0;
2833 break;
2834
2835 default:
2836 my_friendly_abort (0);
2837 }
2838 }
2839 return 1;
2840}
2841
8b5b8b7c
MM
2842/* Convert the indicated template ARG as necessary to match the
2843 indicated template PARM. Returns the converted ARG, or
2844 error_mark_node if the conversion was unsuccessful. Error messages
2845 are issued if COMPLAIN is non-zero. This conversion is for the Ith
2846 parameter in the parameter list. ARGS is the full set of template
2847 arguments deduced so far. */
2848
2849static tree
2850convert_template_argument (parm, arg, args, complain, i, in_decl)
2851 tree parm;
2852 tree arg;
2853 tree args;
2854 int complain;
2855 int i;
2856 tree in_decl;
2857{
2858 tree val;
2859 tree inner_args;
2860 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
2861
2862 inner_args = innermost_args (args);
2863
2864 if (TREE_CODE (arg) == TREE_LIST
2865 && TREE_TYPE (arg) != NULL_TREE
2866 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
2867 {
2868 /* The template argument was the name of some
2869 member function. That's usually
2870 illegal, but static members are OK. In any
2871 case, grab the underlying fields/functions
2872 and issue an error later if required. */
2873 arg = TREE_VALUE (arg);
2874 TREE_TYPE (arg) = unknown_type_node;
2875 }
2876
2877 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
2878 requires_type = (TREE_CODE (parm) == TYPE_DECL
2879 || requires_tmpl_type);
2880
2881 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2882 we also accept implicitly created TYPE_DECL as a valid argument.
2883 This is necessary to handle the case where we pass a template name
2884 to a template template parameter in a scope where we've derived from
2885 in instantiation of that template, so the template name refers to that
2886 instantiation. We really ought to handle this better. */
2887 is_tmpl_type
2888 = ((TREE_CODE (arg) == TEMPLATE_DECL
2889 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
2890 || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7ddedda4 2891 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg))
8b5b8b7c
MM
2892 || (TREE_CODE (arg) == RECORD_TYPE
2893 && CLASSTYPE_TEMPLATE_INFO (arg)
2894 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
2895 && DECL_ARTIFICIAL (TYPE_NAME (arg))
2896 && requires_tmpl_type
b54ccf71 2897 && is_base_of_enclosing_class (arg, current_class_type)));
8b5b8b7c
MM
2898 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2899 arg = TYPE_STUB_DECL (arg);
2900 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
2901 arg = CLASSTYPE_TI_TEMPLATE (arg);
2902
2903 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
2904
2905 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
2906 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
2907 {
8251199e
JM
2908 cp_pedwarn ("to refer to a type member of a template parameter,");
2909 cp_pedwarn (" use `typename %E'", arg);
8b5b8b7c
MM
2910
2911 arg = make_typename_type (TREE_OPERAND (arg, 0),
2912 TREE_OPERAND (arg, 1));
2913 is_type = 1;
2914 }
2915 if (is_type != requires_type)
2916 {
2917 if (in_decl)
2918 {
2919 if (complain)
2920 {
8251199e 2921 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c
MM
2922 i + 1, in_decl);
2923 if (is_type)
8251199e 2924 cp_error (" expected a constant of type `%T', got `%T'",
8b5b8b7c
MM
2925 TREE_TYPE (parm),
2926 (is_tmpl_type ? DECL_NAME (arg) : arg));
2927 else
8251199e 2928 cp_error (" expected a type, got `%E'", arg);
8b5b8b7c
MM
2929 }
2930 }
2931 return error_mark_node;
2932 }
2933 if (is_tmpl_type ^ requires_tmpl_type)
2934 {
2935 if (in_decl && complain)
2936 {
8251199e 2937 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c
MM
2938 i + 1, in_decl);
2939 if (is_tmpl_type)
8251199e 2940 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
8b5b8b7c 2941 else
8251199e 2942 cp_error (" expected a class template, got `%T'", arg);
8b5b8b7c
MM
2943 }
2944 return error_mark_node;
2945 }
2946
2947 if (is_type)
2948 {
2949 if (requires_tmpl_type)
2950 {
2951 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
2952 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
2953
2954 if (coerce_template_template_parms (parmparm, argparm,
2955 in_decl, inner_args))
2956 {
2957 val = arg;
2958
2959 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2960 TEMPLATE_DECL. */
2961 if (val != error_mark_node
2962 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
2963 val = TREE_TYPE (val);
2964 }
2965 else
2966 {
2967 if (in_decl && complain)
2968 {
8251199e 2969 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c 2970 i + 1, in_decl);
8251199e 2971 cp_error (" expected a template of type `%D', got `%D'", parm, arg);
8b5b8b7c
MM
2972 }
2973
2974 val = error_mark_node;
2975 }
2976 }
2977 else
2978 {
2979 val = groktypename (arg);
2980 if (! processing_template_decl)
2981 {
2982 /* [basic.link]: A name with no linkage (notably, the
2983 name of a class or enumeration declared in a local
2984 scope) shall not be used to declare an entity with
2985 linkage. This implies that names with no linkage
2986 cannot be used as template arguments. */
2987 tree t = no_linkage_check (val);
2988 if (t)
2989 {
2990 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8251199e
JM
2991 cp_pedwarn
2992 ("template-argument `%T' uses anonymous type", val);
8b5b8b7c 2993 else
8251199e
JM
2994 cp_error
2995 ("template-argument `%T' uses local type `%T'",
8b5b8b7c
MM
2996 val, t);
2997 return error_mark_node;
2998 }
2999 }
3000 }
3001 }
3002 else
3003 {
3004 tree t = tsubst (TREE_TYPE (parm), args, in_decl);
3005
3006 if (processing_template_decl)
3007 arg = maybe_fold_nontype_arg (arg);
3008
3009 if (!uses_template_parms (arg) && !uses_template_parms (t))
3010 /* We used to call digest_init here. However, digest_init
3011 will report errors, which we don't want when complain
3012 is zero. More importantly, digest_init will try too
3013 hard to convert things: for example, `0' should not be
3014 converted to pointer type at this point according to
3015 the standard. Accepting this is not merely an
3016 extension, since deciding whether or not these
3017 conversions can occur is part of determining which
3018 function template to call, or whether a given epxlicit
3019 argument specification is legal. */
3020 val = convert_nontype_argument (t, arg);
3021 else
3022 val = arg;
3023
3024 if (val == NULL_TREE)
3025 val = error_mark_node;
3026 else if (val == error_mark_node && complain)
8251199e 3027 cp_error ("could not convert template argument `%E' to `%T'",
8b5b8b7c
MM
3028 arg, t);
3029 }
3030
3031 return val;
3032}
3033
3034/* Convert all template arguments to their appropriate types, and
3035 return a vector containing the innermost resulting template
3036 arguments. If any error occurs, return error_mark_node, and, if
3037 COMPLAIN is non-zero, issue an error message. Some error messages
3038 are issued even if COMPLAIN is zero; for instance, if a template
3039 argument is composed from a local class.
75650646
MM
3040
3041 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3042 provided in ARGLIST, or else trailing parameters must have default
3043 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
cabc336a
MM
3044 deduction for any unspecified trailing arguments.
3045
3046 The resulting TREE_VEC is allocated on a temporary obstack, and
3047 must be explicitly copied if it will be permanent. */
de575009 3048
8d08fdba 3049static tree
8b5b8b7c 3050coerce_template_parms (parms, args, in_decl,
75650646 3051 complain,
744fac59 3052 require_all_arguments)
8b5b8b7c 3053 tree parms, args;
8d08fdba 3054 tree in_decl;
75650646
MM
3055 int complain;
3056 int require_all_arguments;
8d08fdba 3057{
a292b002 3058 int nparms, nargs, i, lost = 0;
e4a84209 3059 tree inner_args;
8b5b8b7c
MM
3060 tree new_args;
3061 tree new_inner_args;
a292b002 3062
8b5b8b7c 3063 inner_args = innermost_args (args);
e4a84209 3064 nargs = NUM_TMPL_ARGS (inner_args);
a292b002
MS
3065 nparms = TREE_VEC_LENGTH (parms);
3066
3067 if (nargs > nparms
3068 || (nargs < nparms
75650646 3069 && require_all_arguments
a292b002 3070 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
8d08fdba 3071 {
75650646
MM
3072 if (complain)
3073 {
8251199e 3074 cp_error ("wrong number of template arguments (%d, should be %d)",
e4a84209 3075 nargs, nparms);
75650646
MM
3076
3077 if (in_decl)
8251199e 3078 cp_error_at ("provided for `%D'", in_decl);
75650646
MM
3079 }
3080
8d08fdba
MS
3081 return error_mark_node;
3082 }
3083
cabc336a 3084 new_inner_args = make_temp_vec (nparms);
8b5b8b7c
MM
3085 new_args = add_outermost_template_args (args, new_inner_args);
3086 for (i = 0; i < nparms; i++)
8d08fdba 3087 {
8b5b8b7c
MM
3088 tree arg;
3089 tree parm;
e4a84209 3090
8b5b8b7c
MM
3091 /* Get the Ith template parameter. */
3092 parm = TREE_VEC_ELT (parms, i);
75650646 3093
8b5b8b7c
MM
3094 /* Calculate the Ith argument. */
3095 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
8d08fdba 3096 {
8b5b8b7c
MM
3097 arg = TREE_VALUE (inner_args);
3098 inner_args = TREE_CHAIN (inner_args);
8d08fdba 3099 }
8b5b8b7c
MM
3100 else if (i < nargs)
3101 arg = TREE_VEC_ELT (inner_args, i);
3102 /* If no template argument was supplied, look for a default
3103 value. */
3104 else if (TREE_PURPOSE (parm) == NULL_TREE)
3105 {
3106 /* There was no default value. */
3107 my_friendly_assert (!require_all_arguments, 0);
3108 break;
3109 }
3110 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
3111 arg = tsubst (TREE_PURPOSE (parm), new_args, in_decl);
3112 else
3113 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, in_decl);
e4a84209 3114
8b5b8b7c 3115 /* Now, convert the Ith argument, as necessary. */
75650646
MM
3116 if (arg == NULL_TREE)
3117 /* We're out of arguments. */
3118 {
3119 my_friendly_assert (!require_all_arguments, 0);
3120 break;
3121 }
8b5b8b7c 3122 else if (arg == error_mark_node)
75650646 3123 {
8251199e 3124 cp_error ("template argument %d is invalid", i + 1);
8b5b8b7c 3125 arg = error_mark_node;
8d08fdba 3126 }
8b5b8b7c
MM
3127 else
3128 arg = convert_template_argument (TREE_VALUE (parm),
3129 arg, new_args, complain, i,
3130 in_decl);
3131
3132 if (arg == error_mark_node)
8d08fdba 3133 lost++;
8b5b8b7c 3134 TREE_VEC_ELT (new_inner_args, i) = arg;
8d08fdba 3135 }
8b5b8b7c 3136
8d08fdba
MS
3137 if (lost)
3138 return error_mark_node;
8b5b8b7c
MM
3139
3140 return new_inner_args;
8d08fdba
MS
3141}
3142
34016c81
JM
3143/* Returns 1 if template args OT and NT are equivalent. */
3144
3145int
3146template_args_equal (ot, nt)
3147 tree ot, nt;
3148{
3149 if (nt == ot)
3150 return 1;
3151 if (TREE_CODE (nt) != TREE_CODE (ot))
3152 return 0;
3153 if (TREE_CODE (nt) == TREE_VEC)
3154 /* For member templates */
3155 return comp_template_args (ot, nt);
3156 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
3bfdc719 3157 return same_type_p (ot, nt);
34016c81
JM
3158 else
3159 return (cp_tree_equal (ot, nt) > 0);
3160}
3161
3162/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
f84b4be9
JM
3163 of template arguments. Returns 0 otherwise. */
3164
6757edfe 3165int
5566b478
MS
3166comp_template_args (oldargs, newargs)
3167 tree oldargs, newargs;
3168{
3169 int i;
3170
386b8a85
JM
3171 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3172 return 0;
3173
5566b478
MS
3174 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3175 {
3176 tree nt = TREE_VEC_ELT (newargs, i);
3177 tree ot = TREE_VEC_ELT (oldargs, i);
3178
34016c81 3179 if (! template_args_equal (ot, nt))
61a127b3 3180 return 0;
5566b478
MS
3181 }
3182 return 1;
3183}
3184
8d08fdba
MS
3185/* Given class template name and parameter list, produce a user-friendly name
3186 for the instantiation. */
e92cc029 3187
8d08fdba 3188static char *
36a117a5 3189mangle_class_name_for_template (name, parms, arglist)
8d08fdba
MS
3190 char *name;
3191 tree parms, arglist;
3192{
3193 static struct obstack scratch_obstack;
3194 static char *scratch_firstobj;
3195 int i, nparms;
8d08fdba
MS
3196
3197 if (!scratch_firstobj)
fc378698 3198 gcc_obstack_init (&scratch_obstack);
8d08fdba
MS
3199 else
3200 obstack_free (&scratch_obstack, scratch_firstobj);
fc378698 3201 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
8d08fdba 3202
8d08fdba 3203#define ccat(c) obstack_1grow (&scratch_obstack, (c));
8d08fdba 3204#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
8d08fdba
MS
3205
3206 cat (name);
3207 ccat ('<');
3208 nparms = TREE_VEC_LENGTH (parms);
36a117a5 3209 arglist = innermost_args (arglist);
8d08fdba
MS
3210 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3211 for (i = 0; i < nparms; i++)
3212 {
a292b002
MS
3213 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3214 tree arg = TREE_VEC_ELT (arglist, i);
8d08fdba
MS
3215
3216 if (i)
3217 ccat (',');
3218
a292b002 3219 if (TREE_CODE (parm) == TYPE_DECL)
8d08fdba 3220 {
294471c3 3221 cat (type_as_string_real (arg, 0, 1));
8d08fdba
MS
3222 continue;
3223 }
73b0fce8
KL
3224 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3225 {
3226 if (TREE_CODE (arg) == TEMPLATE_DECL)
2c73f9f5
ML
3227 {
3228 /* Already substituted with real template. Just output
3229 the template name here */
cb0dbb9a
JM
3230 tree context = DECL_CONTEXT (arg);
3231 if (context)
2c73f9f5 3232 {
cb0dbb9a 3233 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422);
2c73f9f5
ML
3234 cat(decl_as_string (DECL_CONTEXT (arg), 0));
3235 cat("::");
3236 }
3237 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3238 }
73b0fce8
KL
3239 else
3240 /* Output the parameter declaration */
294471c3 3241 cat (type_as_string_real (arg, 0, 1));
73b0fce8
KL
3242 continue;
3243 }
8d08fdba
MS
3244 else
3245 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3246
3247 if (TREE_CODE (arg) == TREE_LIST)
3248 {
3249 /* New list cell was built because old chain link was in
3250 use. */
3251 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3252 arg = TREE_VALUE (arg);
3253 }
3254 /* No need to check arglist against parmlist here; we did that
3255 in coerce_template_parms, called from lookup_template_class. */
3256 cat (expr_as_string (arg, 0));
3257 }
3258 {
3259 char *bufp = obstack_next_free (&scratch_obstack);
3260 int offset = 0;
3261 while (bufp[offset - 1] == ' ')
3262 offset--;
3263 obstack_blank_fast (&scratch_obstack, offset);
3264
3265 /* B<C<char> >, not B<C<char>> */
3266 if (bufp[offset - 1] == '>')
3267 ccat (' ');
3268 }
3269 ccat ('>');
3270 ccat ('\0');
3271 return (char *) obstack_base (&scratch_obstack);
8d08fdba
MS
3272}
3273
bd6dd845 3274static tree
5566b478
MS
3275classtype_mangled_name (t)
3276 tree t;
3277{
3278 if (CLASSTYPE_TEMPLATE_INFO (t)
36a117a5
MM
3279 /* Specializations have already had their names set up in
3280 lookup_template_class. */
9fbf56f7
MM
3281 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3282 {
3283 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3284
36a117a5
MM
3285 /* For non-primary templates, the template parameters are
3286 implicit from their surrounding context. */
9fbf56f7
MM
3287 if (PRIMARY_TEMPLATE_P (tmpl))
3288 {
3289 tree name = DECL_NAME (tmpl);
3290 char *mangled_name = mangle_class_name_for_template
3291 (IDENTIFIER_POINTER (name),
3292 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3293 CLASSTYPE_TI_ARGS (t));
3294 tree id = get_identifier (mangled_name);
3295 IDENTIFIER_TEMPLATE (id) = name;
3296 return id;
3297 }
5566b478 3298 }
9fbf56f7
MM
3299
3300 return TYPE_IDENTIFIER (t);
5566b478
MS
3301}
3302
3303static void
3304add_pending_template (d)
3305 tree d;
3306{
e92cc029
MS
3307 tree ti;
3308
3309 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
3310 ti = CLASSTYPE_TEMPLATE_INFO (d);
3311 else
3312 ti = DECL_TEMPLATE_INFO (d);
3313
824b9a4c 3314 if (TI_PENDING_TEMPLATE_FLAG (ti))
5566b478
MS
3315 return;
3316
3317 *template_tail = perm_tree_cons
1139b3d8 3318 (build_srcloc_here (), d, NULL_TREE);
5566b478 3319 template_tail = &TREE_CHAIN (*template_tail);
824b9a4c 3320 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5566b478
MS
3321}
3322
386b8a85
JM
3323
3324/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3325 may be either a _DECL or an overloaded function or an
3326 IDENTIFIER_NODE), and ARGLIST. */
3327
3328tree
3329lookup_template_function (fns, arglist)
3330 tree fns, arglist;
3331{
2c73f9f5 3332 tree type;
050367a3 3333
386b8a85
JM
3334 if (fns == NULL_TREE)
3335 {
8251199e 3336 cp_error ("non-template used as template");
386b8a85
JM
3337 return error_mark_node;
3338 }
3339
2c73f9f5
ML
3340 type = TREE_TYPE (fns);
3341 if (TREE_CODE (fns) == OVERLOAD || !type)
3342 type = unknown_type_node;
3343
672476cb
MM
3344 if (processing_template_decl)
3345 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3346 else
3347 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
386b8a85
JM
3348}
3349
a2b60a0e
MM
3350/* Within the scope of a template class S<T>, the name S gets bound
3351 (in build_self_reference) to a TYPE_DECL for the class, not a
3352 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3353 or one of its enclosing classes, and that type is a template,
3354 return the associated TEMPLATE_DECL. Otherwise, the original
3355 DECL is returned. */
3356
3357tree
3358maybe_get_template_decl_from_type_decl (decl)
3359 tree decl;
3360{
3361 return (decl != NULL_TREE
3362 && TREE_CODE (decl) == TYPE_DECL
3363 && DECL_ARTIFICIAL (decl)
3364 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3365 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3366}
386b8a85 3367
8d08fdba
MS
3368/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3369 parameters, find the desired type.
3370
3371 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
36a117a5
MM
3372 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3373 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3374 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3375 copy it here to keep it from being reclaimed when the decl storage
3376 is reclaimed.
8d08fdba
MS
3377
3378 IN_DECL, if non-NULL, is the template declaration we are trying to
75650646
MM
3379 instantiate.
3380
36a117a5
MM
3381 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3382 the class we are looking up.
3383
75650646
MM
3384 If the template class is really a local class in a template
3385 function, then the FUNCTION_CONTEXT is the function in which it is
3386 being instantiated. */
e92cc029 3387
8d08fdba 3388tree
36a117a5 3389lookup_template_class (d1, arglist, in_decl, context, entering_scope)
8d08fdba
MS
3390 tree d1, arglist;
3391 tree in_decl;
e1467ff2 3392 tree context;
36a117a5 3393 int entering_scope;
8d08fdba 3394{
a703fb38 3395 tree template = NULL_TREE, parmlist;
dbfe2124 3396 tree t;
5566b478
MS
3397
3398 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3399 {
f181d4ae
MM
3400 if (IDENTIFIER_VALUE (d1)
3401 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3402 template = IDENTIFIER_VALUE (d1);
73b0fce8
KL
3403 else
3404 {
2c73f9f5
ML
3405 if (context)
3406 push_decl_namespace (context);
67ffc812
MM
3407 if (current_class_type != NULL_TREE)
3408 template =
3409 maybe_get_template_decl_from_type_decl
3410 (IDENTIFIER_CLASS_VALUE (d1));
a2b60a0e 3411 if (template == NULL_TREE)
2c73f9f5
ML
3412 template = lookup_name_nonclass (d1);
3413 if (context)
3414 pop_decl_namespace ();
73b0fce8 3415 }
8d019cef
JM
3416 if (template)
3417 context = DECL_CONTEXT (template);
5566b478 3418 }
c91a56d2
MS
3419 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3420 {
f3400fe2
JM
3421 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)))
3422 {
3423 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
3424 d1 = DECL_NAME (template);
3425 }
c91a56d2 3426 }
ed44da02
MM
3427 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3428 || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't'
3429 && IS_AGGR_TYPE (d1)))
5566b478 3430 {
ed44da02 3431 template = TYPE_TI_TEMPLATE (d1);
5566b478
MS
3432 d1 = DECL_NAME (template);
3433 }
93cdc044
JM
3434 else if (TREE_CODE (d1) == TEMPLATE_DECL
3435 && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
3436 {
3437 template = d1;
3438 d1 = DECL_NAME (template);
3439 context = DECL_CONTEXT (template);
3440 }
5566b478
MS
3441 else
3442 my_friendly_abort (272);
8d08fdba 3443
8d08fdba 3444 /* With something like `template <class T> class X class X { ... };'
f181d4ae
MM
3445 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3446 We don't want to do that, but we have to deal with the situation,
3447 so let's give them some syntax errors to chew on instead of a
3448 crash. */
8d08fdba 3449 if (! template)
f3400fe2
JM
3450 {
3451 cp_error ("`%T' is not a template", d1);
3452 return error_mark_node;
3453 }
2c73f9f5 3454
c3baf4b5
JM
3455 if (context == NULL_TREE)
3456 context = global_namespace;
2c73f9f5 3457
8d08fdba
MS
3458 if (TREE_CODE (template) != TEMPLATE_DECL)
3459 {
8251199e 3460 cp_error ("non-template type `%T' used as a template", d1);
8d08fdba 3461 if (in_decl)
8251199e 3462 cp_error_at ("for template declaration `%D'", in_decl);
8d08fdba
MS
3463 return error_mark_node;
3464 }
8d08fdba 3465
73b0fce8
KL
3466 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3467 {
3468 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3469 template arguments */
3470
3471 tree parm = copy_template_template_parm (TREE_TYPE (template));
3472 tree template2 = TYPE_STUB_DECL (parm);
3473 tree arglist2;
3474
73b0fce8
KL
3475 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3476
744fac59 3477 arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1);
73b0fce8
KL
3478 if (arglist2 == error_mark_node)
3479 return error_mark_node;
3480
3481 arglist2 = copy_to_permanent (arglist2);
7ddedda4 3482 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)
73b0fce8
KL
3483 = perm_tree_cons (template2, arglist2, NULL_TREE);
3484 TYPE_SIZE (parm) = 0;
3485 return parm;
3486 }
36a117a5 3487 else
8d08fdba 3488 {
36a117a5
MM
3489 tree template_type = TREE_TYPE (template);
3490 tree type_decl;
3491 tree found = NULL_TREE;
3492 int arg_depth;
3493 int parm_depth;
dbfe2124 3494 int is_partial_instantiation;
830bfa74 3495
36a117a5
MM
3496 template = most_general_template (template);
3497 parmlist = DECL_TEMPLATE_PARMS (template);
3498 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3499 arg_depth = TMPL_ARGS_DEPTH (arglist);
3500
7ddedda4
MM
3501 /* We build up the coerced arguments and such on the
3502 momentary_obstack. */
3503 push_momentary ();
3504
36a117a5
MM
3505 if (arg_depth == 1 && parm_depth > 1)
3506 {
39c01e4c 3507 /* We've been given an incomplete set of template arguments.
36a117a5
MM
3508 For example, given:
3509
3510 template <class T> struct S1 {
3511 template <class U> struct S2 {};
3512 template <class U> struct S2<U*> {};
3513 };
3514
3515 we will be called with an ARGLIST of `U*', but the
3516 TEMPLATE will be `template <class T> template
3517 <class U> struct S1<T>::S2'. We must fill in the missing
3518 arguments. */
3519 my_friendly_assert (context != NULL_TREE, 0);
39c01e4c
MM
3520 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context))
3521 && context != global_namespace)
36a117a5 3522 context = DECL_REAL_CONTEXT (context);
39c01e4c
MM
3523
3524 if (context == global_namespace)
3525 /* This is bad. We cannot get enough arguments, even from
3526 the surrounding context, to resolve this class. One
3527 case where this might happen is (illegal) code like:
3528
3529 template <class U>
3530 template <class T>
3531 struct S {
3532 A(const A<T>& a) {}
3533 };
3534
3535 We should catch this error sooner (at the opening curly
3536 for `S', but it is better to be safe than sorry here. */
3537 {
8251199e 3538 cp_error ("invalid use of `%D'", template);
39c01e4c
MM
3539 return error_mark_node;
3540 }
3541
ed44da02 3542 arglist = add_to_template_args (TYPE_TI_ARGS (context),
36a117a5
MM
3543 arglist);
3544 arg_depth = TMPL_ARGS_DEPTH (arglist);
3545 }
5566b478 3546
36a117a5
MM
3547 my_friendly_assert (parm_depth == arg_depth, 0);
3548
3549 /* Calculate the BOUND_ARGS. These will be the args that are
3550 actually tsubst'd into the definition to create the
3551 instantiation. */
3552 if (parm_depth > 1)
830bfa74
MM
3553 {
3554 /* We have multiple levels of arguments to coerce, at once. */
830bfa74 3555 int i;
e4a84209 3556 int saved_depth = TMPL_ARGS_DEPTH (arglist);
36a117a5 3557
cabc336a 3558 tree bound_args = make_temp_vec (parm_depth);
830bfa74 3559
e4a84209 3560 for (i = saved_depth,
830bfa74 3561 t = DECL_TEMPLATE_PARMS (template);
e4a84209 3562 i > 0 && t != NULL_TREE;
830bfa74 3563 --i, t = TREE_CHAIN (t))
e4a84209
MM
3564 {
3565 tree a = coerce_template_parms (TREE_VALUE (t),
3566 arglist, template, 1, 1);
3567 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
3568
3569 /* We temporarily reduce the length of the ARGLIST so
3570 that coerce_template_parms will see only the arguments
3571 corresponding to the template parameters it is
3572 examining. */
3573 TREE_VEC_LENGTH (arglist)--;
3574 }
3575
3576 /* Restore the ARGLIST to its full size. */
3577 TREE_VEC_LENGTH (arglist) = saved_depth;
3578
36a117a5 3579 arglist = bound_args;
830bfa74
MM
3580 }
3581 else
36a117a5
MM
3582 arglist
3583 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
3584 innermost_args (arglist),
3585 template, 1, 1);
3586
3587 if (arglist == error_mark_node)
3588 /* We were unable to bind the arguments. */
5566b478 3589 return error_mark_node;
5566b478 3590
36a117a5
MM
3591 /* In the scope of a template class, explicit references to the
3592 template class refer to the type of the template, not any
3593 instantiation of it. For example, in:
3594
3595 template <class T> class C { void f(C<T>); }
3596
3597 the `C<T>' is just the same as `C'. Outside of the
3598 class, however, such a reference is an instantiation. */
ed44da02 3599 if (comp_template_args (TYPE_TI_ARGS (template_type),
36a117a5
MM
3600 arglist))
3601 {
3602 found = template_type;
3603
3604 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5566b478 3605 {
36a117a5
MM
3606 tree ctx;
3607
3608 /* Note that we use DECL_CONTEXT, rather than
3609 CP_DECL_CONTEXT, so that the termination test is
3610 always just `ctx'. We're not interested in namepace
3611 scopes. */
3612 for (ctx = current_class_type;
3613 ctx;
3614 ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
3615 ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
3bfdc719 3616 if (same_type_p (ctx, template_type))
36a117a5
MM
3617 break;
3618
3619 if (!ctx)
3620 /* We're not in the scope of the class, so the
3621 TEMPLATE_TYPE is not the type we want after
3622 all. */
3623 found = NULL_TREE;
5566b478
MS
3624 }
3625 }
36a117a5
MM
3626
3627 if (!found)
75650646 3628 {
36a117a5
MM
3629 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3630 found; found = TREE_CHAIN (found))
3631 if (comp_template_args (TREE_PURPOSE (found), arglist))
3632 break;
75650646 3633
36a117a5
MM
3634 if (found)
3635 found = TREE_VALUE (found);
8d08fdba 3636 }
36a117a5
MM
3637
3638 if (found)
8d08fdba 3639 {
7ddedda4 3640 pop_momentary ();
36a117a5 3641 return found;
8d08fdba 3642 }
5566b478 3643
36a117a5
MM
3644 /* Since we didn't find the type, we'll have to create it.
3645 Since we'll be saving this type on the
3646 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3647 push_obstacks (&permanent_obstack, &permanent_obstack);
3648
dbfe2124 3649 /* This type is a "partial instantiation" if any of the template
486e4077
MM
3650 arguments still inolve template parameters. Note that we set
3651 IS_PARTIAL_INSTANTIATION for partial specializations as
3652 well. */
dbfe2124
MM
3653 is_partial_instantiation = uses_template_parms (arglist);
3654
36a117a5 3655 /* Create the type. */
ed44da02
MM
3656 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
3657 {
dbfe2124 3658 if (!is_partial_instantiation)
61a127b3 3659 t = start_enum (TYPE_IDENTIFIER (template_type));
ed44da02 3660 else
dbfe2124 3661 /* We don't want to call start_enum for this type, since
ed44da02
MM
3662 the values for the enumeration constants may involve
3663 template parameters. And, no one should be interested
3664 in the enumeration constants for such a type. */
3665 t = make_node (ENUMERAL_TYPE);
3666 }
3667 else
3668 {
3669 t = make_lang_type (TREE_CODE (template_type));
3670 CLASSTYPE_DECLARED_CLASS (t)
3671 = CLASSTYPE_DECLARED_CLASS (template_type);
3672 CLASSTYPE_GOT_SEMICOLON (t) = 1;
3673 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
f668f160 3674 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
ed44da02
MM
3675 }
3676
61a127b3 3677 /* If we called start_enum above, this information will already
ed44da02
MM
3678 be set up. */
3679 if (!TYPE_NAME (t))
3680 {
3681 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
36a117a5 3682
ed44da02
MM
3683 /* Create a stub TYPE_DECL for it. */
3684 type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t);
3685 SET_DECL_ARTIFICIAL (type_decl);
3686 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
3687 DECL_SOURCE_FILE (type_decl)
3688 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
3689 DECL_SOURCE_LINE (type_decl)
3690 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
3691 TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl;
3692 }
3693 else
3694 type_decl = TYPE_NAME (t);
36a117a5 3695
9fbf56f7
MM
3696 /* Set up the template information. We have to figure out which
3697 template is the immediate parent if this is a full
3698 instantiation. */
3699 if (parm_depth == 1 || is_partial_instantiation
3700 || !PRIMARY_TEMPLATE_P (template))
3701 /* This case is easy; there are no member templates involved. */
3702 found = template;
3703 else
3704 {
3705 /* This is a full instantiation of a member template. There
3706 should be some partial instantiation of which this is an
3707 instance. */
3708
3709 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3710 found; found = TREE_CHAIN (found))
3711 {
3712 int success;
3713 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
3714
3715 /* We only want partial instantiations, here, not
3716 specializations or full instantiations. */
3717 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
3718 || !uses_template_parms (TREE_VALUE (found)))
3719 continue;
3720
3721 /* Temporarily reduce by one the number of levels in the
3722 ARGLIST and in FOUND so as to avoid comparing the
3723 last set of arguments. */
3724 TREE_VEC_LENGTH (arglist)--;
3725 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
3726
3727 /* See if the arguments match. If they do, then TMPL is
3728 the partial instantiation we want. */
3729 success = comp_template_args (TREE_PURPOSE (found), arglist);
3730
3731 /* Restore the argument vectors to their full size. */
3732 TREE_VEC_LENGTH (arglist)++;
3733 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
3734
3735 if (success)
3736 {
3737 found = tmpl;
3738 break;
3739 }
3740 }
3741
3742 if (!found)
3743 my_friendly_abort (0);
3744 }
3745
5566b478 3746 arglist = copy_to_permanent (arglist);
ed44da02 3747 SET_TYPE_TEMPLATE_INFO (t,
9fbf56f7 3748 tree_cons (found, arglist, NULL_TREE));
dbfe2124
MM
3749 DECL_TEMPLATE_INSTANTIATIONS (template)
3750 = tree_cons (arglist, t,
3751 DECL_TEMPLATE_INSTANTIATIONS (template));
3752
3753 if (TREE_CODE (t) == ENUMERAL_TYPE
3754 && !is_partial_instantiation)
61a127b3
MM
3755 /* Now that the type has been registered on the instantiations
3756 list, we set up the enumerators. Because the enumeration
3757 constants may involve the enumeration type itself, we make
3758 sure to register the type first, and then create the
3759 constants. That way, doing tsubst_expr for the enumeration
3760 constants won't result in recursive calls here; we'll find
3761 the instantiation and exit above. */
3762 tsubst_enum (template_type, t, arglist);
dbfe2124
MM
3763
3764 /* We're done with the permanent obstack, now. */
3765 pop_obstacks ();
7ddedda4
MM
3766 /* We're also done with the momentary allocation we started
3767 above. */
3768 pop_momentary ();
5566b478 3769
36a117a5
MM
3770 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3771 is set up. */
ed44da02
MM
3772 if (TREE_CODE (t) != ENUMERAL_TYPE)
3773 DECL_NAME (type_decl) = classtype_mangled_name (t);
36a117a5 3774 DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
dbfe2124 3775 if (!is_partial_instantiation)
36a117a5
MM
3776 {
3777 DECL_ASSEMBLER_NAME (type_decl)
3778 = get_identifier (build_overload_name (t, 1, 1));
50a6dbd7
JM
3779
3780 /* For backwards compatibility; code that uses
3781 -fexternal-templates expects looking up a template to
3782 instantiate it. I think DDD still relies on this.
3783 (jason 8/20/1998) */
ed44da02
MM
3784 if (TREE_CODE (t) != ENUMERAL_TYPE
3785 && flag_external_templates
36a117a5
MM
3786 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3787 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3788 add_pending_template (t);
3789 }
3790 else
077e7015
MM
3791 /* If the type makes use of template parameters, the
3792 code that generates debugging information will crash. */
3793 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
8d08fdba 3794
36a117a5
MM
3795 return t;
3796 }
8d08fdba
MS
3797}
3798\f
050367a3
MM
3799/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3800 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3801 If FN returns non-zero, the iteration is terminated, and
3802 for_each_template_parm returns 1. Otherwise, the iteration
3803 continues. If FN never returns a non-zero value, the value
3804 returned by for_each_template_parm is 0. If FN is NULL, it is
3805 considered to be the function which always returns 1. */
3806
8d08fdba 3807int
050367a3 3808for_each_template_parm (t, fn, data)
8d08fdba 3809 tree t;
050367a3
MM
3810 tree_fn_t fn;
3811 void* data;
8d08fdba
MS
3812{
3813 if (!t)
3814 return 0;
581d38d0
MM
3815
3816 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
3817 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
3818 return 1;
3819
8d08fdba
MS
3820 switch (TREE_CODE (t))
3821 {
3822 case INDIRECT_REF:
3823 case COMPONENT_REF:
3824 /* We assume that the object must be instantiated in order to build
3825 the COMPONENT_REF, so we test only whether the type of the
3826 COMPONENT_REF uses template parms. */
050367a3 3827 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba 3828
297e73d8
MM
3829 case ARRAY_REF:
3830 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
3831 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
3832
8d08fdba
MS
3833 case IDENTIFIER_NODE:
3834 if (!IDENTIFIER_TEMPLATE (t))
3835 return 0;
5566b478 3836 my_friendly_abort (42);
8d08fdba
MS
3837
3838 /* aggregates of tree nodes */
3839 case TREE_VEC:
3840 {
3841 int i = TREE_VEC_LENGTH (t);
3842 while (i--)
050367a3 3843 if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
8d08fdba
MS
3844 return 1;
3845 return 0;
3846 }
3847 case TREE_LIST:
050367a3
MM
3848 if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
3849 || for_each_template_parm (TREE_VALUE (t), fn, data))
8d08fdba 3850 return 1;
050367a3 3851 return for_each_template_parm (TREE_CHAIN (t), fn, data);
8d08fdba 3852
2c73f9f5
ML
3853 case OVERLOAD:
3854 if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
3855 return 1;
3856 return for_each_template_parm (OVL_CHAIN (t), fn, data);
3857
8d08fdba
MS
3858 /* constructed type nodes */
3859 case POINTER_TYPE:
3860 case REFERENCE_TYPE:
050367a3 3861 return for_each_template_parm (TREE_TYPE (t), fn, data);
ed44da02 3862
8d08fdba 3863 case RECORD_TYPE:
b7484fbe 3864 if (TYPE_PTRMEMFUNC_FLAG (t))
050367a3
MM
3865 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
3866 fn, data);
ed44da02
MM
3867 /* Fall through. */
3868
8d08fdba 3869 case UNION_TYPE:
ed44da02
MM
3870 case ENUMERAL_TYPE:
3871 if (! TYPE_TEMPLATE_INFO (t))
8d08fdba 3872 return 0;
050367a3 3873 return for_each_template_parm (TREE_VALUE
ed44da02 3874 (TYPE_TEMPLATE_INFO (t)),
050367a3 3875 fn, data);
588c2d1c
MM
3876 case METHOD_TYPE:
3877 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
8d08fdba 3878 return 1;
588c2d1c
MM
3879 /* Fall through. */
3880
3881 case FUNCTION_TYPE:
3882 /* Check the parameter types. Since default arguments are not
3883 instantiated until they are needed, the TYPE_ARG_TYPES may
3884 contain expressions that involve template parameters. But,
3885 no-one should be looking at them yet. And, once they're
3886 instantiated, they don't contain template parameters, so
3887 there's no point in looking at them then, either. */
3888 {
3889 tree parm;
3890
3891 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
3892 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
3893 return 1;
3894 }
3895
3896 /* Check the return type, too. */
050367a3 3897 return for_each_template_parm (TREE_TYPE (t), fn, data);
588c2d1c 3898
8d08fdba 3899 case ARRAY_TYPE:
050367a3 3900 if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
8d08fdba 3901 return 1;
050367a3 3902 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba 3903 case OFFSET_TYPE:
050367a3 3904 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
8d08fdba 3905 return 1;
050367a3 3906 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba
MS
3907
3908 /* decl nodes */
3909 case TYPE_DECL:
050367a3 3910 return for_each_template_parm (TREE_TYPE (t), fn, data);
5566b478 3911
73b0fce8
KL
3912 case TEMPLATE_DECL:
3913 /* A template template parameter is encountered */
3914 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
744fac59
KL
3915 return for_each_template_parm (TREE_TYPE (t), fn, data);
3916 /* Already substituted template template parameter */
73b0fce8
KL
3917 return 0;
3918
3ac3d9ea 3919 case CONST_DECL:
050367a3 3920 if (for_each_template_parm (DECL_INITIAL (t), fn, data))
3ac3d9ea
MM
3921 return 1;
3922 goto check_type_and_context;
3923
8d08fdba 3924 case FUNCTION_DECL:
5566b478
MS
3925 case VAR_DECL:
3926 /* ??? What about FIELD_DECLs? */
3927 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
050367a3 3928 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
8d08fdba
MS
3929 return 1;
3930 /* fall through */
8d08fdba 3931 case PARM_DECL:
3ac3d9ea 3932 check_type_and_context:
050367a3 3933 if (for_each_template_parm (TREE_TYPE (t), fn, data))
5566b478 3934 return 1;
050367a3
MM
3935 if (DECL_CONTEXT (t)
3936 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
8d08fdba 3937 return 1;
8d08fdba
MS
3938 return 0;
3939
3940 case CALL_EXPR:
297e73d8
MM
3941 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
3942 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
3943
8d08fdba 3944 case ADDR_EXPR:
050367a3 3945 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
8d08fdba
MS
3946
3947 /* template parm nodes */
73b0fce8 3948 case TEMPLATE_TEMPLATE_PARM:
744fac59 3949 /* Record template parameters such as `T' inside `TT<T>'. */
7ddedda4
MM
3950 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)
3951 && for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
744fac59
KL
3952 return 1;
3953 case TEMPLATE_TYPE_PARM:
f84b4be9 3954 case TEMPLATE_PARM_INDEX:
050367a3
MM
3955 if (fn)
3956 return (*fn)(t, data);
3957 else
3958 return 1;
8d08fdba
MS
3959
3960 /* simple type nodes */
3961 case INTEGER_TYPE:
050367a3 3962 if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
8d08fdba 3963 return 1;
050367a3 3964 return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
8d08fdba
MS
3965
3966 case REAL_TYPE:
37c46b43 3967 case COMPLEX_TYPE:
8d08fdba 3968 case VOID_TYPE:
2986ae00 3969 case BOOLEAN_TYPE:
2c73f9f5 3970 case NAMESPACE_DECL:
8d08fdba
MS
3971 return 0;
3972
3973 /* constants */
3974 case INTEGER_CST:
3975 case REAL_CST:
3976 case STRING_CST:
3977 return 0;
3978
3979 case ERROR_MARK:
3980 /* Non-error_mark_node ERROR_MARKs are bad things. */
3981 my_friendly_assert (t == error_mark_node, 274);
3982 /* NOTREACHED */
3983 return 0;
3984
ec255269 3985 case LOOKUP_EXPR:
5566b478 3986 case TYPENAME_TYPE:
8d08fdba
MS
3987 return 1;
3988
61a127b3
MM
3989 case PTRMEM_CST:
3990 return for_each_template_parm (TREE_TYPE (t), fn, data);
3991
5156628f 3992 case SCOPE_REF:
050367a3 3993 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
5156628f 3994
db5ae43f
MS
3995 case CONSTRUCTOR:
3996 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
050367a3
MM
3997 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3998 (TREE_TYPE (t)), fn, data);
3999 return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
db5ae43f 4000
42976354
BK
4001 case MODOP_EXPR:
4002 case CAST_EXPR:
4003 case REINTERPRET_CAST_EXPR:
4004 case CONST_CAST_EXPR:
4005 case STATIC_CAST_EXPR:
4006 case DYNAMIC_CAST_EXPR:
42976354
BK
4007 case ARROW_EXPR:
4008 case DOTSTAR_EXPR:
4009 case TYPEID_EXPR:
4010 return 1;
4011
abff8e06
JM
4012 case SIZEOF_EXPR:
4013 case ALIGNOF_EXPR:
050367a3 4014 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
abff8e06 4015
8d08fdba
MS
4016 default:
4017 switch (TREE_CODE_CLASS (TREE_CODE (t)))
4018 {
4019 case '1':
4020 case '2':
ec255269 4021 case 'e':
8d08fdba
MS
4022 case '<':
4023 {
4024 int i;
bbc06b4b 4025 for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;)
050367a3 4026 if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
8d08fdba
MS
4027 return 1;
4028 return 0;
4029 }
4030 default:
4031 break;
4032 }
4033 sorry ("testing %s for template parms",
4034 tree_code_name [(int) TREE_CODE (t)]);
4035 my_friendly_abort (82);
4036 /* NOTREACHED */
4037 return 0;
4038 }
4039}
4040
050367a3
MM
4041int
4042uses_template_parms (t)
4043 tree t;
4044{
4045 return for_each_template_parm (t, 0, 0);
4046}
4047
27fafc8d
JM
4048static struct tinst_level *current_tinst_level;
4049static struct tinst_level *free_tinst_level;
4050static int tinst_depth;
e9f32eb5 4051extern int max_tinst_depth;
5566b478 4052#ifdef GATHER_STATISTICS
27fafc8d 4053int depth_reached;
5566b478 4054#endif
27fafc8d
JM
4055int tinst_level_tick;
4056int last_template_error_tick;
8d08fdba 4057
7bae46f4 4058/* Print out all the template instantiations that we are currently
27fafc8d
JM
4059 working on. If ERR, we are being called from cp_thing, so do
4060 the right thing for an error message. */
7bae46f4 4061
27fafc8d
JM
4062static void
4063print_template_context (err)
4064 int err;
7bae46f4
JM
4065{
4066 struct tinst_level *p = current_tinst_level;
4067 int line = lineno;
4068 char *file = input_filename;
4069
49e04385 4070 if (err && p)
27fafc8d 4071 {
49e04385
MM
4072 if (current_function_decl != p->decl
4073 && current_function_decl != NULL_TREE)
4074 /* We can get here during the processing of some synthesized
4075 method. Then, p->decl will be the function that's causing
4076 the synthesis. */
4077 ;
27fafc8d 4078 else
27fafc8d 4079 {
49e04385
MM
4080 if (current_function_decl == p->decl)
4081 /* Avoid redundancy with the the "In function" line. */;
4082 else
4083 fprintf (stderr, "%s: In instantiation of `%s':\n",
4084 file, decl_as_string (p->decl, 0));
4085
038fb86f
JM
4086 line = p->line;
4087 file = p->file;
27fafc8d
JM
4088 p = p->next;
4089 }
4090 }
4091
7bae46f4
JM
4092 for (; p; p = p->next)
4093 {
038fb86f
JM
4094 fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line,
4095 decl_as_string (p->decl, 0));
4096 line = p->line;
4097 file = p->file;
7bae46f4 4098 }
038fb86f 4099 fprintf (stderr, "%s:%d: instantiated from here\n", file, line);
7bae46f4
JM
4100}
4101
27fafc8d
JM
4102/* Called from cp_thing to print the template context for an error. */
4103
4104void
4105maybe_print_template_context ()
4106{
4107 if (last_template_error_tick == tinst_level_tick
4108 || current_tinst_level == 0)
4109 return;
4110
4111 last_template_error_tick = tinst_level_tick;
4112 print_template_context (1);
4113}
4114
bd6dd845 4115static int
5566b478
MS
4116push_tinst_level (d)
4117 tree d;
8d08fdba
MS
4118{
4119 struct tinst_level *new;
8d08fdba 4120
7215f9a0
MS
4121 if (tinst_depth >= max_tinst_depth)
4122 {
8adf5b5e
JM
4123 /* If the instantiation in question still has unbound template parms,
4124 we don't really care if we can't instantiate it, so just return.
4125 This happens with base instantiation for implicit `typename'. */
4126 if (uses_template_parms (d))
4127 return 0;
4128
1139b3d8 4129 last_template_error_tick = tinst_level_tick;
8251199e 4130 error ("template instantiation depth exceeds maximum of %d",
7215f9a0 4131 max_tinst_depth);
8251199e
JM
4132 error (" (use -ftemplate-depth-NN to increase the maximum)");
4133 cp_error (" instantiating `%D'", d);
5566b478 4134
27fafc8d 4135 print_template_context (0);
5566b478 4136
7215f9a0
MS
4137 return 0;
4138 }
4139
8d08fdba
MS
4140 if (free_tinst_level)
4141 {
4142 new = free_tinst_level;
4143 free_tinst_level = new->next;
4144 }
4145 else
4146 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
4147
5566b478
MS
4148 new->decl = d;
4149 new->line = lineno;
4150 new->file = input_filename;
8d08fdba
MS
4151 new->next = current_tinst_level;
4152 current_tinst_level = new;
5566b478 4153
7215f9a0 4154 ++tinst_depth;
5566b478
MS
4155#ifdef GATHER_STATISTICS
4156 if (tinst_depth > depth_reached)
4157 depth_reached = tinst_depth;
4158#endif
4159
27fafc8d 4160 ++tinst_level_tick;
7215f9a0 4161 return 1;
8d08fdba
MS
4162}
4163
4164void
4165pop_tinst_level ()
4166{
4167 struct tinst_level *old = current_tinst_level;
4168
ae58fa02
MM
4169 /* Restore the filename and line number stashed away when we started
4170 this instantiation. */
4171 lineno = old->line;
4172 input_filename = old->file;
5f2c99c4 4173 extract_interface_info ();
ae58fa02 4174
8d08fdba
MS
4175 current_tinst_level = old->next;
4176 old->next = free_tinst_level;
4177 free_tinst_level = old;
7215f9a0 4178 --tinst_depth;
27fafc8d 4179 ++tinst_level_tick;
8d08fdba
MS
4180}
4181
4182struct tinst_level *
4183tinst_for_decl ()
4184{
4185 struct tinst_level *p = current_tinst_level;
4186
4187 if (p)
4188 for (; p->next ; p = p->next )
4189 ;
4190 return p;
4191}
4192
f84b4be9
JM
4193/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4194 vector of template arguments, as for tsubst.
4195
4196 Returns an appropriate tsbust'd friend declaration. */
4197
4198static tree
4199tsubst_friend_function (decl, args)
4200 tree decl;
4201 tree args;
4202{
4203 tree new_friend;
27fafc8d
JM
4204 int line = lineno;
4205 char *file = input_filename;
4206
4207 lineno = DECL_SOURCE_LINE (decl);
4208 input_filename = DECL_SOURCE_FILE (decl);
4209
f84b4be9
JM
4210 if (TREE_CODE (decl) == FUNCTION_DECL
4211 && DECL_TEMPLATE_INSTANTIATION (decl)
4212 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4213 /* This was a friend declared with an explicit template
4214 argument list, e.g.:
4215
4216 friend void f<>(T);
4217
4218 to indicate that f was a template instantiation, not a new
4219 function declaration. Now, we have to figure out what
4220 instantiation of what template. */
4221 {
4222 tree template_id;
4223 tree new_args;
4224 tree tmpl;
f84b4be9
JM
4225
4226 template_id
4227 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
f7d98d58 4228 args, NULL_TREE),
f84b4be9 4229 tsubst (DECL_TI_ARGS (decl),
f7d98d58 4230 args, NULL_TREE));
39c01e4c
MM
4231 /* FIXME: The decl we create via the next tsubst could be
4232 created on a temporary obstack. */
f7d98d58 4233 new_friend = tsubst (decl, args, NULL_TREE);
36a117a5
MM
4234 tmpl = determine_specialization (template_id, new_friend,
4235 &new_args,
4236 /*need_member_template=*/0,
4237 /*complain=*/1);
27fafc8d
JM
4238 new_friend = instantiate_template (tmpl, new_args);
4239 goto done;
f84b4be9 4240 }
36a117a5
MM
4241
4242 new_friend = tsubst (decl, args, NULL_TREE);
f84b4be9 4243
36a117a5 4244 /* The NEW_FRIEND will look like an instantiation, to the
f84b4be9
JM
4245 compiler, but is not an instantiation from the point of view of
4246 the language. For example, we might have had:
4247
4248 template <class T> struct S {
4249 template <class U> friend void f(T, U);
4250 };
4251
4252 Then, in S<int>, template <class U> void f(int, U) is not an
4253 instantiation of anything. */
4254 DECL_USE_TEMPLATE (new_friend) = 0;
4255 if (TREE_CODE (decl) == TEMPLATE_DECL)
4256 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
36a117a5
MM
4257
4258 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4259 tsubst will have resulted in a call to
4260 set_mangled_name_for_template_decl. But, the function is not a
4261 template instantiation and should not be mangled like one.
4262 Therefore, we remangle the function name. We don't have to do
4263 this if the NEW_FRIEND is a template since
4264 set_mangled_name_for_template_decl doesn't do anything if the
4265 function declaration still uses template arguments. */
4266 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4267 {
4268 set_mangled_name_for_decl (new_friend);
4269 DECL_RTL (new_friend) = 0;
4270 make_decl_rtl (new_friend, NULL_PTR, 1);
4271 }
4272
6eb3bb27 4273 if (DECL_NAMESPACE_SCOPE_P (new_friend))
f84b4be9 4274 {
36a117a5 4275 tree old_decl;
fbf1c34b
MM
4276 tree new_friend_template_info;
4277 tree new_friend_result_template_info;
4278 int new_friend_is_defn;
4279
4280 /* We must save some information from NEW_FRIEND before calling
4281 duplicate decls since that function will free NEW_FRIEND if
4282 possible. */
4283 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
f84b4be9 4284 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
fbf1c34b
MM
4285 {
4286 /* This declaration is a `primary' template. */
4287 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4288
4289 new_friend_is_defn
4290 = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE;
4291 new_friend_result_template_info
4292 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend));
4293 }
4294 else
4295 {
4296 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4297 new_friend_result_template_info = NULL_TREE;
4298 }
36a117a5 4299
36a117a5
MM
4300 old_decl = pushdecl_namespace_level (new_friend);
4301
4302 if (old_decl != new_friend)
4303 {
4304 /* This new friend declaration matched an existing
4305 declaration. For example, given:
4306
4307 template <class T> void f(T);
4308 template <class U> class C {
4309 template <class T> friend void f(T) {}
4310 };
4311
4312 the friend declaration actually provides the definition
4313 of `f', once C has been instantiated for some type. So,
4314 old_decl will be the out-of-class template declaration,
4315 while new_friend is the in-class definition.
4316
4317 But, if `f' was called before this point, the
4318 instantiation of `f' will have DECL_TI_ARGS corresponding
4319 to `T' but not to `U', references to which might appear
4320 in the definition of `f'. Previously, the most general
4321 template for an instantiation of `f' was the out-of-class
4322 version; now it is the in-class version. Therefore, we
4323 run through all specialization of `f', adding to their
4324 DECL_TI_ARGS appropriately. In particular, they need a
4325 new set of outer arguments, corresponding to the
4326 arguments for this class instantiation.
4327
4328 The same situation can arise with something like this:
4329
4330 friend void f(int);
4331 template <class T> class C {
4332 friend void f(T) {}
4333 };
4334
4335 when `C<int>' is instantiated. Now, `f(int)' is defined
4336 in the class. */
4337
fbf1c34b
MM
4338 if (!new_friend_is_defn)
4339 /* On the other hand, if the in-class declaration does
4340 *not* provide a definition, then we don't want to alter
4341 existing definitions. We can just leave everything
4342 alone. */
36a117a5 4343 ;
fbf1c34b 4344 else
36a117a5 4345 {
fbf1c34b
MM
4346 /* Overwrite whatever template info was there before, if
4347 any, with the new template information pertaining to
4348 the declaration. */
4349 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4350
4351 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4352 /* duplicate_decls will take care of this case. */
4353 ;
4354 else
36a117a5 4355 {
fbf1c34b
MM
4356 tree t;
4357 tree new_friend_args;
4358
4359 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl))
4360 = new_friend_result_template_info;
4361
4362 new_friend_args = TI_ARGS (new_friend_template_info);
4363 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4364 t != NULL_TREE;
4365 t = TREE_CHAIN (t))
4366 {
4367 tree spec = TREE_VALUE (t);
36a117a5 4368
fbf1c34b
MM
4369 DECL_TI_ARGS (spec)
4370 = add_outermost_template_args (new_friend_args,
4371 DECL_TI_ARGS (spec));
4372 DECL_TI_ARGS (spec)
4373 = copy_to_permanent (DECL_TI_ARGS (spec));
4374 }
4375
4376 /* Now, since specializations are always supposed to
4377 hang off of the most general template, we must move
4378 them. */
4379 t = most_general_template (old_decl);
4380 if (t != old_decl)
4381 {
4382 DECL_TEMPLATE_SPECIALIZATIONS (t)
4383 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4384 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4385 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4386 }
36a117a5
MM
4387 }
4388 }
4389
4390 /* The information from NEW_FRIEND has been merged into OLD_DECL
4391 by duplicate_decls. */
4392 new_friend = old_decl;
4393 }
f84b4be9
JM
4394 }
4395 else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
4396 {
4397 /* Check to see that the declaration is really present, and,
4398 possibly obtain an improved declaration. */
4399 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4400 new_friend);
4401
4402 if (fn)
4403 new_friend = fn;
4404 }
4405
27fafc8d
JM
4406 done:
4407 lineno = line;
4408 input_filename = file;
f84b4be9
JM
4409 return new_friend;
4410}
4411
1aed5355
MM
4412/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4413 template arguments, as for tsubst.
6757edfe
MM
4414
4415 Returns an appropriate tsbust'd friend type. */
4416
4417static tree
1aed5355
MM
4418tsubst_friend_class (friend_tmpl, args)
4419 tree friend_tmpl;
6757edfe
MM
4420 tree args;
4421{
1aed5355 4422 tree friend_type;
f9c3243c 4423 tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1);
6757edfe
MM
4424
4425 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4426
4427 if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl))
4428 {
4429 /* The friend template has already been declared. Just
36a117a5
MM
4430 check to see that the declarations match, and install any new
4431 default parameters. We must tsubst the default parameters,
4432 of course. We only need the innermost template parameters
4433 because that is all that redeclare_class_template will look
4434 at. */
4435 tree parms
4436 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4437 args);
4438 redeclare_class_template (TREE_TYPE (tmpl), parms);
6757edfe
MM
4439 friend_type = TREE_TYPE (tmpl);
4440 }
4441 else
4442 {
4443 /* The friend template has not already been declared. In this
4444 case, the instantiation of the template class will cause the
4445 injection of this template into the global scope. */
f9c3243c 4446 tmpl = tsubst (friend_tmpl, args, NULL_TREE);
6757edfe
MM
4447
4448 /* The new TMPL is not an instantiation of anything, so we
4449 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4450 the new type because that is supposed to be the corresponding
4451 template decl, i.e., TMPL. */
4452 DECL_USE_TEMPLATE (tmpl) = 0;
4453 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4454 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4455
4456 /* Inject this template into the global scope. */
4457 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4458 }
4459
4460 return friend_type;
4461}
f84b4be9 4462
8d08fdba 4463tree
5566b478
MS
4464instantiate_class_template (type)
4465 tree type;
8d08fdba 4466{
61a127b3 4467 tree template, args, pattern, t;
36a117a5 4468 tree typedecl;
8d08fdba 4469
5566b478 4470 if (type == error_mark_node)
8d08fdba
MS
4471 return error_mark_node;
4472
5566b478
MS
4473 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
4474 return type;
4475
7ddedda4
MM
4476 /* We want to allocate temporary vectors of template arguments and
4477 template argument expressions on the momentary obstack, not on
4478 the expression obstack. Otherwise, all the space allocated in
4479 argument coercion and such is simply lost. */
4480 push_momentary ();
4481
6bdb985f 4482 /* Figure out which template is being instantiated. */
36a117a5 4483 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5566b478 4484 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
73aad9b9 4485
6bdb985f
MM
4486 /* Figure out which arguments are being used to do the
4487 instantiation. */
4488 args = CLASSTYPE_TI_ARGS (type);
4c571114 4489 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
6bdb985f 4490
4c571114
MM
4491 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4492 /* If this is a partial instantiation, then we can't instantiate
4493 the type; there's no telling whether or not one of the
4494 template parameters might eventually be instantiated to some
4495 value that results in a specialization being used. For
4496 example, consider:
4497
4498 template <class T>
4499 struct S {};
4500
4501 template <class U>
4502 void f(S<U>);
4503
4504 template <>
4505 struct S<int> {};
4506
4507 Now, the `S<U>' in `f<int>' is the specialization, not an
5db698f6 4508 instantiation of the original template. */
4c571114
MM
4509 goto end;
4510
4511 /* Determine what specialization of the original template to
4512 instantiate. */
4513 if (PARTIAL_INSTANTIATION_P (type))
6bdb985f
MM
4514 /* There's no telling which specialization is appropriate at this
4515 point. Since all peeking at the innards of this partial
4516 instantiation are extensions (like the "implicit typename"
4517 extension, which allows users to omit the keyword `typename' on
4518 names that are declared as types in template base classes), we
4519 are free to do what we please.
4520
4521 Trying to figure out which partial instantiation to use can
4522 cause a crash. (Some of the template arguments don't even have
4523 types.) So, we just use the most general version. */
4524 t = NULL_TREE;
4525 else
73aad9b9 4526 {
6bdb985f
MM
4527 t = most_specialized_class (template, args);
4528
4529 if (t == error_mark_node)
73aad9b9 4530 {
6bdb985f
MM
4531 char *str = "candidates are:";
4532 cp_error ("ambiguous class template instantiation for `%#T'", type);
4533 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4534 t = TREE_CHAIN (t))
73aad9b9 4535 {
6bdb985f
MM
4536 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4537 args))
4538 {
4539 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4540 str = " ";
4541 }
73aad9b9 4542 }
6bdb985f
MM
4543 TYPE_BEING_DEFINED (type) = 1;
4544 type = error_mark_node;
4545 goto end;
73aad9b9 4546 }
73aad9b9 4547 }
6bdb985f
MM
4548
4549 if (t)
73aad9b9
JM
4550 pattern = TREE_TYPE (t);
4551 else
4552 pattern = TREE_TYPE (template);
5566b478 4553
4c571114
MM
4554 /* If the template we're instantiating is incomplete, then clearly
4555 there's nothing we can do. */
5566b478 4556 if (TYPE_SIZE (pattern) == NULL_TREE)
7ddedda4 4557 goto end;
5566b478 4558
4c571114
MM
4559 /* If this is a partial instantiation, don't tsubst anything. We will
4560 only use this type for implicit typename, so the actual contents don't
4561 matter. All that matters is whether a particular name is a type. */
4562 if (PARTIAL_INSTANTIATION_P (type))
4563 {
4564 /* The fields set here must be kept in sync with those cleared
4565 in begin_class_definition. */
4566 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4567 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4568 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4569 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4570 /* Pretend that the type is complete, so that we will look
4571 inside it during name lookup and such. */
4572 TYPE_SIZE (type) = integer_zero_node;
4573 goto end;
4574 }
4575
4576 /* If we've recursively instantiated too many templates, stop. */
4577 if (! push_tinst_level (type))
4578 goto end;
4579
4580 /* Now we're really doing the instantiation. Mark the type as in
4581 the process of being defined. */
4582 TYPE_BEING_DEFINED (type) = 1;
4583
4584 maybe_push_to_top_level (uses_template_parms (type));
4585 pushclass (type, 0);
4586
73aad9b9 4587 if (t)
36a117a5
MM
4588 {
4589 /* This TYPE is actually a instantiation of of a partial
4590 specialization. We replace the innermost set of ARGS with
4591 the arguments appropriate for substitution. For example,
4592 given:
4593
4594 template <class T> struct S {};
4595 template <class T> struct S<T*> {};
4596
4597 and supposing that we are instantiating S<int*>, ARGS will
4598 present be {int*} but we need {int}. */
4599 tree inner_args
4600 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4601 args);
4602
4603 /* If there were multiple levels in ARGS, replacing the
4604 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4605 want, so we make a copy first. */
4606 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4607 {
4608 args = copy_node (args);
4609 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4610 }
4611 else
4612 args = inner_args;
4613 }
8d019cef 4614
5566b478
MS
4615 if (flag_external_templates)
4616 {
4617 if (flag_alt_external_templates)
4618 {
4619 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
4620 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
4621 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
2604412d
JM
4622 = (! CLASSTYPE_INTERFACE_ONLY (type)
4623 && CLASSTYPE_INTERFACE_KNOWN (type));
5566b478
MS
4624 }
4625 else
4626 {
4627 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
4628 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4629 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
4630 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
2604412d
JM
4631 = (! CLASSTYPE_INTERFACE_ONLY (type)
4632 && CLASSTYPE_INTERFACE_KNOWN (type));
5566b478
MS
4633 }
4634 }
4635 else
8d08fdba 4636 {
5566b478
MS
4637 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
4638 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
8d08fdba
MS
4639 }
4640
f7da6097
MS
4641 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
4642 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
4643 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
4644 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
4645 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
4646 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
4647 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
4648 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
4649 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
4650 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
4651 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
4652 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
4653 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
4654 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
4655 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
4656 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
4657 TYPE_USES_COMPLEX_INHERITANCE (type)
4658 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
4659 TYPE_USES_MULTIPLE_INHERITANCE (type)
4660 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
4661 TYPE_USES_VIRTUAL_BASECLASSES (type)
4662 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
4663 TYPE_PACKED (type) = TYPE_PACKED (pattern);
4664 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
eff71ab0 4665 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
f7da6097 4666
4c571114
MM
4667 /* We must copy the arguments to the permanent obstack since
4668 during the tsubst'ing below they may wind up in the
4669 DECL_TI_ARGS of some instantiated member template. */
4670 args = copy_to_permanent (args);
2604412d 4671
5566b478
MS
4672 {
4673 tree binfo = TYPE_BINFO (type);
4674 tree pbases = TYPE_BINFO_BASETYPES (pattern);
4675
4676 if (pbases)
4677 {
4678 tree bases;
4679 int i;
4680 int len = TREE_VEC_LENGTH (pbases);
de22184b 4681 bases = make_tree_vec (len);
5566b478
MS
4682 for (i = 0; i < len; ++i)
4683 {
711734a9 4684 tree elt, basetype;
5566b478
MS
4685
4686 TREE_VEC_ELT (bases, i) = elt
f7d98d58 4687 = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE);
5566b478
MS
4688 BINFO_INHERITANCE_CHAIN (elt) = binfo;
4689
711734a9
JM
4690 basetype = TREE_TYPE (elt);
4691
4692 if (! IS_AGGR_TYPE (basetype))
8251199e
JM
4693 cp_error
4694 ("base type `%T' of `%T' fails to be a struct or class type",
711734a9
JM
4695 basetype, type);
4696 else if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE)
8251199e 4697 cp_error ("base class `%T' of `%T' has incomplete type",
711734a9
JM
4698 basetype, type);
4699
4700 /* These are set up in xref_basetypes for normal classes, so
4701 we have to handle them here for template bases. */
dfbcd65a
JM
4702
4703 unshare_base_binfos (elt);
4704
711734a9
JM
4705 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4706 {
4707 TYPE_USES_VIRTUAL_BASECLASSES (type) = 1;
4708 TYPE_USES_COMPLEX_INHERITANCE (type) = 1;
4709 }
4710 TYPE_GETS_NEW (type) |= TYPE_GETS_NEW (basetype);
4711 TYPE_GETS_DELETE (type) |= TYPE_GETS_DELETE (basetype);
5566b478 4712 }
de22184b
MS
4713 /* Don't initialize this until the vector is filled out, or
4714 lookups will crash. */
4715 BINFO_BASETYPES (binfo) = bases;
5566b478
MS
4716 }
4717 }
4718
5566b478 4719 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
8d08fdba 4720 {
5566b478 4721 tree tag = TREE_VALUE (t);
36a117a5
MM
4722 tree name = TYPE_IDENTIFIER (tag);
4723 tree newtag;
5566b478 4724
ed44da02 4725 newtag = tsubst (tag, args, NULL_TREE);
61a127b3 4726 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
ed44da02 4727 {
486e4077
MM
4728 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
4729 /* Unfortunately, lookup_template_class sets
4730 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
4731 instantiation (i.e., for the type of a member template
4732 class nested within a template class.) This behavior is
4733 required for maybe_process_partial_specialization to work
4734 correctly, but is not accurate in this case; the TAG is not
4735 an instantiation of anything. (The corresponding
4736 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
4737 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
4738
ed44da02
MM
4739 /* Now, we call pushtag to put this NEWTAG into the scope of
4740 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4741 pushtag calling push_template_decl. We don't have to do
4742 this for enums because it will already have been done in
4743 tsubst_enum. */
4744 if (name)
4745 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
4746 pushtag (name, newtag, /*globalize=*/0);
4747 }
8d08fdba
MS
4748 }
4749
5566b478
MS
4750 /* Don't replace enum constants here. */
4751 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
4752 if (TREE_CODE (t) != CONST_DECL)
4753 {
ae58fa02
MM
4754 tree r;
4755
4756 /* The the file and line for this declaration, to assist in
4757 error message reporting. Since we called push_tinst_level
4758 above, we don't need to restore these. */
4759 lineno = DECL_SOURCE_LINE (t);
4760 input_filename = DECL_SOURCE_FILE (t);
4761
4762 r = tsubst (t, args, NULL_TREE);
5566b478
MS
4763 if (TREE_CODE (r) == VAR_DECL)
4764 {
2604412d
JM
4765 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
4766 /* Perhaps we should do more of grokfield here. */
94c82a77
JM
4767 if (DECL_DEFINED_IN_CLASS_P (r))
4768 /* Set up DECL_INITIAL, since tsubst doesn't. */
4769 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
4770 NULL_TREE);
5566b478
MS
4771 start_decl_1 (r);
4772 DECL_IN_AGGR_P (r) = 1;
4773 DECL_EXTERNAL (r) = 1;
4774 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
6ba89f8e
MM
4775 if (DECL_DEFINED_IN_CLASS_P (r))
4776 check_static_variable_definition (r, TREE_TYPE (r));
5566b478 4777 }
61a127b3
MM
4778
4779 /* R will have a TREE_CHAIN if and only if it has already been
4780 processed by finish_member_declaration. This can happen
4781 if, for example, it is a TYPE_DECL for a class-scoped
4782 ENUMERAL_TYPE; such a thing will already have been added to
4783 the field list by tsubst_enum above. */
4784 if (!TREE_CHAIN (r))
4785 {
4786 set_current_access_from_decl (r);
4787 finish_member_declaration (r);
4788 }
5566b478 4789 }
8d08fdba 4790
61a127b3
MM
4791 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
4792 for this instantiation. */
4793 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
4794 {
4795 tree r = tsubst (t, args, NULL_TREE);
4796 set_current_access_from_decl (r);
4797 finish_member_declaration (r);
4798 }
8d08fdba 4799
2604412d
JM
4800 /* Construct the DECL_FRIENDLIST for the new class type. */
4801 typedecl = TYPE_MAIN_DECL (type);
4802 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
4803 t != NULL_TREE;
4804 t = TREE_CHAIN (t))
f84b4be9 4805 {
2604412d 4806 tree friends;
f84b4be9 4807
2604412d
JM
4808 DECL_FRIENDLIST (typedecl)
4809 = tree_cons (TREE_PURPOSE (t), NULL_TREE,
4810 DECL_FRIENDLIST (typedecl));
f84b4be9 4811
2604412d
JM
4812 for (friends = TREE_VALUE (t);
4813 friends != NULL_TREE;
4814 friends = TREE_CHAIN (friends))
4815 {
4816 if (TREE_PURPOSE (friends) == error_mark_node)
f84b4be9 4817 {
2604412d
JM
4818 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4819 = tree_cons (error_mark_node,
4820 tsubst_friend_function (TREE_VALUE (friends),
4821 args),
4822 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
4823 }
4824 else
4825 {
4826 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4827 = tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE),
4828 NULL_TREE,
4829 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
f84b4be9
JM
4830
4831 }
4832 }
2604412d 4833 }
5566b478 4834
6757edfe
MM
4835 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
4836 t != NULL_TREE;
4837 t = TREE_CHAIN (t))
4838 {
4839 tree friend_type = TREE_VALUE (t);
1aed5355 4840 tree new_friend_type;
6757edfe 4841
36a117a5
MM
4842 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4843 new_friend_type = tsubst_friend_class (friend_type, args);
4844 else if (uses_template_parms (friend_type))
4845 new_friend_type = tsubst (friend_type, args, NULL_TREE);
4846 else
6757edfe
MM
4847 /* The call to xref_tag_from_type does injection for friend
4848 classes. */
1aed5355 4849 new_friend_type =
36a117a5
MM
4850 xref_tag_from_type (friend_type, NULL_TREE, 1);
4851
1aed5355
MM
4852
4853 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4854 /* Trick make_friend_class into realizing that the friend
4855 we're adding is a template, not an ordinary class. It's
4856 important that we use make_friend_class since it will
4857 perform some error-checking and output cross-reference
4858 information. */
4859 ++processing_template_decl;
4860
4861 make_friend_class (type, new_friend_type);
fc378698 4862
1aed5355
MM
4863 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4864 --processing_template_decl;
6757edfe 4865 }
fc378698 4866
2604412d
JM
4867 /* This does injection for friend functions. */
4868 if (!processing_template_decl)
4869 {
4870 t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE);
5566b478 4871
2604412d
JM
4872 for (; t; t = TREE_CHAIN (t))
4873 {
4874 tree d = TREE_VALUE (t);
fc378698 4875
2604412d
JM
4876 if (TREE_CODE (d) == TYPE_DECL)
4877 /* Already injected. */;
4878 else
4879 pushdecl (d);
4880 }
4881 }
5566b478 4882
2604412d
JM
4883 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
4884 if (TREE_CODE (t) == FIELD_DECL)
4885 {
4886 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
4887 require_complete_type (t);
4888 }
5566b478 4889
61a127b3
MM
4890 /* Set the file and line number information to whatever is given for
4891 the class itself. This puts error messages involving generated
4892 implicit functions at a predictable point, and the same point
4893 that would be used for non-template classes. */
4894 lineno = DECL_SOURCE_LINE (typedecl);
4895 input_filename = DECL_SOURCE_FILE (typedecl);
4896
4897 unreverse_member_declarations (type);
2604412d
JM
4898 type = finish_struct_1 (type, 0);
4899 CLASSTYPE_GOT_SEMICOLON (type) = 1;
e92cc029 4900
5524676d
JM
4901 /* Clear this now so repo_template_used is happy. */
4902 TYPE_BEING_DEFINED (type) = 0;
2604412d 4903 repo_template_used (type);
8d08fdba 4904
5566b478 4905 popclass (0);
5566b478
MS
4906 pop_from_top_level ();
4907 pop_tinst_level ();
4908
7ddedda4
MM
4909 end:
4910 pop_momentary ();
4911
5566b478 4912 return type;
8d08fdba
MS
4913}
4914
4915static int
4916list_eq (t1, t2)
4917 tree t1, t2;
4918{
4919 if (t1 == NULL_TREE)
4920 return t2 == NULL_TREE;
4921 if (t2 == NULL_TREE)
4922 return 0;
4923 /* Don't care if one declares its arg const and the other doesn't -- the
4924 main variant of the arg type is all that matters. */
4925 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
4926 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
4927 return 0;
4928 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
4929}
4930
00d3396f
JM
4931/* If arg is a non-type template parameter that does not depend on template
4932 arguments, fold it like we weren't in the body of a template. */
4933
4934static tree
4935maybe_fold_nontype_arg (arg)
4936 tree arg;
4937{
4938 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
4939 && !uses_template_parms (arg))
4940 {
4941 /* Sometimes, one of the args was an expression involving a
4942 template constant parameter, like N - 1. Now that we've
4943 tsubst'd, we might have something like 2 - 1. This will
4944 confuse lookup_template_class, so we do constant folding
4945 here. We have to unset processing_template_decl, to
4946 fool build_expr_from_tree() into building an actual
4947 tree. */
4948
4949 int saved_processing_template_decl = processing_template_decl;
4950 processing_template_decl = 0;
4951 arg = fold (build_expr_from_tree (arg));
4952 processing_template_decl = saved_processing_template_decl;
4953 }
4954 return arg;
4955}
4956
a221d52f 4957/* Return the TREE_VEC with the arguments for the innermost template header,
36a117a5
MM
4958 where ARGS is either that or the VEC of VECs for all the
4959 arguments. */
a221d52f
JM
4960
4961tree
36a117a5 4962innermost_args (args)
a221d52f 4963 tree args;
a221d52f 4964{
36a117a5 4965 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
a221d52f 4966}
f84b4be9 4967
830bfa74
MM
4968/* Substitute ARGS into the vector of template arguments T. */
4969
4970tree
4971tsubst_template_arg_vector (t, args)
4972 tree t;
4973 tree args;
4974{
4975 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
4976 tree *elts = (tree *) alloca (len * sizeof (tree));
4977
4978 bzero ((char *) elts, len * sizeof (tree));
4979
4980 for (i = 0; i < len; i++)
4981 {
4982 if (TREE_VEC_ELT (t, i) != NULL_TREE
4983 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
4984 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), args);
4985 else
4986 elts[i] = maybe_fold_nontype_arg
4987 (tsubst_expr (TREE_VEC_ELT (t, i), args, NULL_TREE));
4988
4989 if (elts[i] != TREE_VEC_ELT (t, i))
4990 need_new = 1;
4991 }
4992
4993 if (!need_new)
4994 return t;
4995
7ddedda4 4996 t = make_temp_vec (len);
830bfa74
MM
4997 for (i = 0; i < len; i++)
4998 TREE_VEC_ELT (t, i) = elts[i];
4999
5000 return t;
5001}
5002
36a117a5
MM
5003/* Return the result of substituting ARGS into the template parameters
5004 given by PARMS. If there are m levels of ARGS and m + n levels of
5005 PARMS, then the result will contain n levels of PARMS. For
5006 example, if PARMS is `template <class T> template <class U>
5007 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5008 result will be `template <int*, double, class V>'. */
5009
5010tree
5011tsubst_template_parms (parms, args)
5012 tree parms;
5013 tree args;
5014{
5015 tree r;
5016 tree* new_parms = &r;
5017
5018 for (new_parms = &r;
5019 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5020 new_parms = &(TREE_CHAIN (*new_parms)),
5021 parms = TREE_CHAIN (parms))
5022 {
5023 tree new_vec =
5024 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5025 int i;
5026
5027 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5028 {
5029 tree default_value =
5030 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5031 tree parm_decl =
5032 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5033
5034 TREE_VEC_ELT (new_vec, i)
5035 = build_tree_list (tsubst (default_value, args, NULL_TREE),
5036 tsubst (parm_decl, args, NULL_TREE));
5037
5038 }
5039
5040 *new_parms =
5041 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms)
5042 - TMPL_ARGS_DEPTH (args))),
5043 new_vec, NULL_TREE);
5044 }
5045
5046 return r;
5047}
5048
ed44da02
MM
5049/* Substitute the ARGS into the indicated aggregate (or enumeration)
5050 type T. If T is not an aggregate or enumeration type, it is
5051 handled as if by tsubst. IN_DECL is as for tsubst. If
5052 ENTERING_SCOPE is non-zero, T is the context for a template which
5053 we are presently tsubst'ing. Return the subsituted value. */
36a117a5
MM
5054
5055tree
5056tsubst_aggr_type (t, args, in_decl, entering_scope)
5057 tree t;
5058 tree args;
5059 tree in_decl;
5060 int entering_scope;
5061{
5062 if (t == NULL_TREE)
5063 return NULL_TREE;
5064
5065 switch (TREE_CODE (t))
5066 {
5067 case RECORD_TYPE:
5068 if (TYPE_PTRMEMFUNC_P (t))
5069 {
5070 tree r = build_ptrmemfunc_type
5071 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl));
91063b51 5072 return cp_build_qualified_type (r, TYPE_QUALS (t));
36a117a5
MM
5073 }
5074
5075 /* else fall through */
ed44da02 5076 case ENUMERAL_TYPE:
36a117a5 5077 case UNION_TYPE:
5db698f6 5078 if (TYPE_TEMPLATE_INFO (t))
36a117a5
MM
5079 {
5080 tree argvec;
5081 tree context;
5082 tree r;
5083
5084 /* First, determine the context for the type we are looking
5085 up. */
5086 if (TYPE_CONTEXT (t) != NULL_TREE)
5087 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
5088 in_decl, /*entering_scope=*/1);
5089 else
5090 context = NULL_TREE;
5091
5092 /* Then, figure out what arguments are appropriate for the
5093 type we are trying to find. For example, given:
5094
5095 template <class T> struct S;
5096 template <class T, class U> void f(T, U) { S<U> su; }
5097
5098 and supposing that we are instantiating f<int, double>,
5099 then our ARGS will be {int, double}, but, when looking up
5100 S we only want {double}. */
7ddedda4
MM
5101 push_momentary ();
5102 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args);
36a117a5
MM
5103
5104 r = lookup_template_class (t, argvec, in_decl, context,
5105 entering_scope);
7ddedda4 5106 pop_momentary ();
36a117a5 5107
91063b51 5108 return cp_build_qualified_type (r, TYPE_QUALS (t));
36a117a5
MM
5109 }
5110 else
5111 /* This is not a template type, so there's nothing to do. */
5112 return t;
5113
5114 default:
5115 return tsubst (t, args, in_decl);
5116 }
5117}
5118
ae58fa02
MM
5119/* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5120 (already computed) substitution of ARGS into TREE_TYPE (T), if
5121 appropriate. Return the result of the substitution. IN_DECL is as
5122 for tsubst. */
00d3396f 5123
75b0bbce 5124tree
ae58fa02
MM
5125tsubst_decl (t, args, type, in_decl)
5126 tree t;
5127 tree args;
5128 tree type;
8d08fdba
MS
5129 tree in_decl;
5130{
ae58fa02
MM
5131 int saved_lineno;
5132 char* saved_filename;
b370501f 5133 tree r = NULL_TREE;
830bfa74 5134
ae58fa02
MM
5135 /* Set the filename and linenumber to improve error-reporting. */
5136 saved_lineno = lineno;
5137 saved_filename = input_filename;
5138 lineno = DECL_SOURCE_LINE (t);
5139 input_filename = DECL_SOURCE_FILE (t);
b7484fbe 5140
8d08fdba
MS
5141 switch (TREE_CODE (t))
5142 {
98c1c668
JM
5143 case TEMPLATE_DECL:
5144 {
5145 /* We can get here when processing a member template function
5146 of a template class. */
98c1c668 5147 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 5148 tree spec;
db2767b6 5149 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
98c1c668 5150
db2767b6
MM
5151 if (!is_template_template_parm)
5152 {
36a117a5
MM
5153 /* We might already have an instance of this template.
5154 The ARGS are for the surrounding class type, so the
5155 full args contain the tsubst'd args for the context,
5156 plus the innermost args from the template decl. */
5157 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5158 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5159 : DECL_TI_ARGS (DECL_RESULT (t));
7ddedda4
MM
5160 tree full_args;
5161
5162 push_momentary ();
5163 full_args = tsubst_template_arg_vector (tmpl_args, args);
36a117a5
MM
5164
5165 /* tsubst_template_arg_vector doesn't copy the vector if
5166 nothing changed. But, *something* should have
5167 changed. */
5168 my_friendly_assert (full_args != tmpl_args, 0);
5169
5170 spec = retrieve_specialization (t, full_args);
7ddedda4 5171 pop_momentary ();
db2767b6 5172 if (spec != NULL_TREE)
ae58fa02
MM
5173 {
5174 r = spec;
5175 break;
5176 }
db2767b6 5177 }
98c1c668
JM
5178
5179 /* Make a new template decl. It will be similar to the
5180 original, but will record the current template arguments.
5181 We also create a new function declaration, which is just
5182 like the old one, but points to this new template, rather
5183 than the old one. */
ae58fa02
MM
5184 r = copy_node (t);
5185 copy_lang_decl (r);
5186 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5187 TREE_CHAIN (r) = NULL_TREE;
db2767b6
MM
5188
5189 if (is_template_template_parm)
5190 {
5191 tree new_decl = tsubst (decl, args, in_decl);
ae58fa02
MM
5192 DECL_RESULT (r) = new_decl;
5193 TREE_TYPE (r) = TREE_TYPE (new_decl);
5194 break;
db2767b6
MM
5195 }
5196
ae58fa02 5197 DECL_CONTEXT (r)
36a117a5
MM
5198 = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5199 /*entering_scope=*/1);
ae58fa02 5200 DECL_CLASS_CONTEXT (r)
36a117a5
MM
5201 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, in_decl,
5202 /*entering_scope=*/1);
ae58fa02 5203 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
5204
5205 if (TREE_CODE (decl) == TYPE_DECL)
5206 {
5207 tree new_type = tsubst (TREE_TYPE (t), args, in_decl);
ae58fa02
MM
5208 TREE_TYPE (r) = new_type;
5209 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5210 DECL_RESULT (r) = TYPE_MAIN_DECL (new_type);
5211 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
93cdc044
JM
5212 }
5213 else
5214 {
5215 tree new_decl = tsubst (decl, args, in_decl);
ae58fa02
MM
5216 DECL_RESULT (r) = new_decl;
5217 DECL_TI_TEMPLATE (new_decl) = r;
5218 TREE_TYPE (r) = TREE_TYPE (new_decl);
5219 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
93cdc044
JM
5220 }
5221
ae58fa02
MM
5222 SET_DECL_IMPLICIT_INSTANTIATION (r);
5223 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5224 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
5225
5226 /* The template parameters for this new template are all the
5227 template parameters for the old template, except the
5228 outermost level of parameters. */
ae58fa02 5229 DECL_TEMPLATE_PARMS (r)
36a117a5 5230 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args);
98c1c668 5231
93cdc044 5232 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 5233 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 5234
8d019cef 5235 /* We don't partially instantiate partial specializations. */
93cdc044 5236 if (TREE_CODE (decl) == TYPE_DECL)
ae58fa02 5237 break;
93cdc044 5238
75650646
MM
5239 for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
5240 spec != NULL_TREE;
5241 spec = TREE_CHAIN (spec))
5242 {
5243 /* It helps to consider example here. Consider:
5244
5245 template <class T>
5246 struct S {
5247 template <class U>
5248 void f(U u);
5249
5250 template <>
5251 void f(T* t) {}
5252 };
5253
5254 Now, for example, we are instantiating S<int>::f(U u).
5255 We want to make a template:
5256
5257 template <class U>
5258 void S<int>::f(U);
5259
5260 It will have a specialization, for the case U = int*, of
5261 the form:
5262
5263 template <>
5264 void S<int>::f<int*>(int*);
5265
5266 This specialization will be an instantiation of
5267 the specialization given in the declaration of S, with
5268 argument list int*. */
5269
5270 tree fn = TREE_VALUE (spec);
5271 tree spec_args;
5272 tree new_fn;
5273
5274 if (!DECL_TEMPLATE_SPECIALIZATION (fn))
5275 /* Instantiations are on the same list, but they're of
5276 no concern to us. */
5277 continue;
5278
36a117a5
MM
5279 if (TREE_CODE (fn) != TEMPLATE_DECL)
5280 /* A full specialization. There's no need to record
5281 that here. */
5282 continue;
5283
5284 spec_args = tsubst (DECL_TI_ARGS (fn), args, in_decl);
5285 new_fn = tsubst (DECL_RESULT (most_general_template (fn)),
5286 spec_args, in_decl);
5287 DECL_TI_TEMPLATE (new_fn) = fn;
ae58fa02 5288 register_specialization (new_fn, r,
36a117a5 5289 innermost_args (spec_args));
75650646
MM
5290 }
5291
5292 /* Record this partial instantiation. */
ae58fa02
MM
5293 register_specialization (r, t,
5294 DECL_TI_ARGS (DECL_RESULT (r)));
75650646 5295
98c1c668 5296 }
ae58fa02 5297 break;
8d08fdba
MS
5298
5299 case FUNCTION_DECL:
5300 {
386b8a85 5301 tree ctx;
87603ed0 5302 tree argvec = NULL_TREE;
36a117a5 5303 tree gen_tmpl;
5566b478 5304 int member;
d8c4447d
MM
5305 int args_depth;
5306 int parms_depth;
5566b478 5307
36a117a5
MM
5308 /* Nobody should be tsubst'ing into non-template functions. */
5309 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5310
5311 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5312 {
5313 tree spec;
5314
7ddedda4
MM
5315 /* Allocate template arguments on the momentary obstack,
5316 in case we don't need to keep them. */
5317 push_momentary ();
5318
36a117a5
MM
5319 /* Calculate the most general template of which R is a
5320 specialization, and the complete set of arguments used to
5321 specialize R. */
5322 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7ddedda4
MM
5323 argvec
5324 = tsubst_template_arg_vector (DECL_TI_ARGS
5325 (DECL_TEMPLATE_RESULT (gen_tmpl)),
5326 args);
36a117a5
MM
5327
5328 /* Check to see if we already have this specialization. */
5329 spec = retrieve_specialization (gen_tmpl, argvec);
7ddedda4 5330
36a117a5 5331 if (spec)
ae58fa02
MM
5332 {
5333 r = spec;
7ddedda4 5334 pop_momentary ();
ae58fa02
MM
5335 break;
5336 }
d8c4447d 5337
7ddedda4
MM
5338 /* We're going to need to keep the ARGVEC, so we copy it
5339 here. */
5340 argvec = copy_to_permanent (argvec);
5341 pop_momentary ();
5342
d8c4447d
MM
5343 /* Here, we deal with the peculiar case:
5344
5345 template <class T> struct S {
5346 template <class U> friend void f();
5347 };
5348 template <class U> friend void f() {}
5349 template S<int>;
5350 template void f<double>();
5351
5352 Here, the ARGS for the instantiation of will be {int,
5353 double}. But, we only need as many ARGS as there are
5354 levels of template parameters in CODE_PATTERN. We are
5355 careful not to get fooled into reducing the ARGS in
5356 situations like:
5357
5358 template <class T> struct S { template <class U> void f(U); }
5359 template <class T> template <> void S<T>::f(int) {}
5360
5361 which we can spot because the pattern will be a
5362 specialization in this case. */
5363 args_depth = TMPL_ARGS_DEPTH (args);
5364 parms_depth =
5365 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5366 if (args_depth > parms_depth
5367 && !DECL_TEMPLATE_SPECIALIZATION (t))
5368 {
5369 my_friendly_assert (DECL_FRIEND_P (t), 0);
5370
5371 if (parms_depth > 1)
5372 {
5373 int i;
5374
5375 args = make_temp_vec (parms_depth);
5376 for (i = 0; i < parms_depth; ++i)
5377 TREE_VEC_ELT (args, i) =
5378 TREE_VEC_ELT (args, i + (args_depth - parms_depth));
5379 }
5380 else
5381 args = TREE_VEC_ELT (args, args_depth - parms_depth);
5382 }
36a117a5
MM
5383 }
5384 else
5385 {
5386 /* This special case arises when we have something like this:
5387
5388 template <class T> struct S {
5389 friend void f<int>(int, double);
5390 };
5391
5392 Here, the DECL_TI_TEMPLATE for the friend declaration
39c01e4c
MM
5393 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5394 being called from tsubst_friend_function, and we want
5395 only to create a new decl (R) with appropriate types so
5396 that we can call determine_specialization. */
5397 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5398 == LOOKUP_EXPR)
5399 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5400 == IDENTIFIER_NODE), 0);
36a117a5
MM
5401 gen_tmpl = NULL_TREE;
5402 }
5403
6eb3bb27 5404 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 5405 {
5566b478
MS
5406 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5407 member = 2;
5408 else
5409 member = 1;
36a117a5
MM
5410 ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, t,
5411 /*entering_scope=*/1);
5566b478
MS
5412 }
5413 else
5414 {
5415 member = 0;
5416 ctx = NULL_TREE;
5566b478 5417 }
eff71ab0 5418 type = tsubst (type, args, in_decl);
8d08fdba 5419
5566b478
MS
5420 /* We do NOT check for matching decls pushed separately at this
5421 point, as they may not represent instantiations of this
5422 template, and in any case are considered separate under the
312e7d50 5423 discrete model. Instead, see add_maybe_template. */
5566b478
MS
5424
5425 r = copy_node (t);
5426 copy_lang_decl (r);
e1467ff2 5427 DECL_USE_TEMPLATE (r) = 0;
5566b478
MS
5428 TREE_TYPE (r) = type;
5429
5430 DECL_CONTEXT (r)
36a117a5 5431 = tsubst_aggr_type (DECL_CONTEXT (t), args, t, /*entering_scope=*/1);
5566b478
MS
5432 DECL_CLASS_CONTEXT (r) = ctx;
5433
4c571114 5434 if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r)))
f181d4ae
MM
5435 /* Type-conversion operator. Reconstruct the name, in
5436 case it's the name of one of the template's parameters. */
5437 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
5566b478 5438
711734a9
JM
5439 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t);
5440 DECL_MAIN_VARIANT (r) = r;
5441 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
5442
5443 TREE_STATIC (r) = 0;
5444 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5445 DECL_EXTERNAL (r) = 1;
5446 DECL_INTERFACE_KNOWN (r) = 0;
5447 DECL_DEFER_OUTPUT (r) = 0;
5448 TREE_CHAIN (r) = NULL_TREE;
5449 DECL_PENDING_INLINE_INFO (r) = 0;
5450 TREE_USED (r) = 0;
5451
36a117a5
MM
5452 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5453 name. There's no need to do this in the special friend
5454 case mentioned above where GEN_TMPL is NULL. */
5455 if (gen_tmpl)
386b8a85 5456 {
7ddedda4
MM
5457 /* The ARGVEC was built on the momentary obstack. Make it
5458 permanent now. */
5459 argvec = copy_to_permanent (argvec);
36a117a5
MM
5460 DECL_TEMPLATE_INFO (r)
5461 = perm_tree_cons (gen_tmpl, argvec, NULL_TREE);
5462 SET_DECL_IMPLICIT_INSTANTIATION (r);
5463 register_specialization (r, gen_tmpl, argvec);
5464
5465 /* Set the mangled name for R. */
5466 if (DECL_DESTRUCTOR_P (t))
5467 DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx);
5468 else
386b8a85 5469 {
36a117a5
MM
5470 /* Instantiations of template functions must be mangled
5471 specially, in order to conform to 14.5.5.1
5472 [temp.over.link]. */
5473 tree tmpl = DECL_TI_TEMPLATE (t);
386b8a85 5474
36a117a5
MM
5475 /* TMPL will be NULL if this is a specialization of a
5476 member function of a template class. */
5477 if (name_mangling_version < 1
5478 || tmpl == NULL_TREE
5479 || (member && !is_member_template (tmpl)
5480 && !DECL_TEMPLATE_INFO (tmpl)))
5481 set_mangled_name_for_decl (r);
75650646 5482 else
36a117a5 5483 set_mangled_name_for_template_decl (r);
386b8a85 5484 }
36a117a5
MM
5485
5486 DECL_RTL (r) = 0;
5487 make_decl_rtl (r, NULL_PTR, 1);
5488
5489 /* Like grokfndecl. If we don't do this, pushdecl will
5490 mess up our TREE_CHAIN because it doesn't find a
5491 previous decl. Sigh. */
5492 if (member
c36ae80f 5493 && ! uses_template_parms (r)
36a117a5
MM
5494 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r))
5495 == NULL_TREE))
5496 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
386b8a85 5497 }
f181d4ae
MM
5498
5499 if (DECL_CONSTRUCTOR_P (r))
5500 {
5501 maybe_retrofit_in_chrg (r);
5502 grok_ctor_properties (ctx, r);
5503 }
5504 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
5505 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
8d08fdba 5506 }
ae58fa02 5507 break;
8d08fdba
MS
5508
5509 case PARM_DECL:
5510 {
ae58fa02 5511 r = copy_node (t);
5566b478 5512 TREE_TYPE (r) = type;
db2767b6
MM
5513 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5514 DECL_INITIAL (r) = TREE_TYPE (r);
5515 else
5516 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl);
5517
5566b478 5518 DECL_CONTEXT (r) = NULL_TREE;
f83b0cb6
JM
5519#ifdef PROMOTE_PROTOTYPES
5520 if ((TREE_CODE (type) == INTEGER_TYPE
5521 || TREE_CODE (type) == ENUMERAL_TYPE)
5522 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5523 DECL_ARG_TYPE (r) = integer_type_node;
5524#endif
8d08fdba 5525 if (TREE_CHAIN (t))
f7d98d58 5526 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t));
8d08fdba 5527 }
ae58fa02 5528 break;
8d08fdba 5529
5566b478
MS
5530 case FIELD_DECL:
5531 {
ae58fa02 5532 r = copy_node (t);
5566b478
MS
5533 TREE_TYPE (r) = type;
5534 copy_lang_decl (r);
5535#if 0
f7d98d58 5536 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl);
5566b478 5537#endif
f7d98d58 5538 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl);
5566b478 5539 TREE_CHAIN (r) = NULL_TREE;
519ebd1e 5540 if (TREE_CODE (type) == VOID_TYPE)
8251199e 5541 cp_error_at ("instantiation of `%D' as type void", r);
5566b478 5542 }
ae58fa02 5543 break;
5566b478
MS
5544
5545 case USING_DECL:
5546 {
ae58fa02 5547 r = copy_node (t);
5566b478 5548 DECL_INITIAL (r)
f7d98d58 5549 = tsubst_copy (DECL_INITIAL (t), args, in_decl);
5566b478 5550 TREE_CHAIN (r) = NULL_TREE;
5566b478 5551 }
ae58fa02 5552 break;
5566b478
MS
5553
5554 case VAR_DECL:
5555 {
36a117a5
MM
5556 tree argvec;
5557 tree gen_tmpl;
5558 tree spec;
5559 tree tmpl;
5560 tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5561 /*entering_scope=*/1);
5562
5563 /* Nobody should be tsubst'ing into non-template variables. */
5564 my_friendly_assert (DECL_LANG_SPECIFIC (t)
5565 && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5566b478 5566
36a117a5
MM
5567 /* Check to see if we already have this specialization. */
5568 tmpl = DECL_TI_TEMPLATE (t);
5569 gen_tmpl = most_general_template (tmpl);
5570 argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
5571 spec = retrieve_specialization (gen_tmpl, argvec);
5572
5573 if (spec)
ae58fa02
MM
5574 {
5575 r = spec;
5576 break;
5577 }
5566b478
MS
5578
5579 r = copy_node (t);
5580 TREE_TYPE (r) = type;
5581 DECL_CONTEXT (r) = ctx;
5582 if (TREE_STATIC (r))
5583 DECL_ASSEMBLER_NAME (r)
5584 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
d11ad92e
MS
5585
5586 /* Don't try to expand the initializer until someone tries to use
5587 this variable; otherwise we run into circular dependencies. */
5588 DECL_INITIAL (r) = NULL_TREE;
5566b478
MS
5589 DECL_RTL (r) = 0;
5590 DECL_SIZE (r) = 0;
36a117a5
MM
5591 copy_lang_decl (r);
5592 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
5566b478 5593
36a117a5
MM
5594 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
5595 SET_DECL_IMPLICIT_INSTANTIATION (r);
5596 register_specialization (r, gen_tmpl, argvec);
5566b478 5597
5566b478 5598 TREE_CHAIN (r) = NULL_TREE;
519ebd1e 5599 if (TREE_CODE (type) == VOID_TYPE)
8251199e 5600 cp_error_at ("instantiation of `%D' as type void", r);
5566b478 5601 }
ae58fa02 5602 break;
5566b478
MS
5603
5604 case TYPE_DECL:
d2e5ee5c 5605 if (t == TYPE_NAME (TREE_TYPE (t)))
ae58fa02
MM
5606 r = TYPE_NAME (type);
5607 else
5608 {
5609 r = copy_node (t);
5610 TREE_TYPE (r) = type;
5611 DECL_CONTEXT (r) = current_class_type;
5612 TREE_CHAIN (r) = NULL_TREE;
5613 }
5614 break;
5615
5616 default:
5617 my_friendly_abort (0);
5618 }
5619
5620 /* Restore the file and line information. */
5621 lineno = saved_lineno;
5622 input_filename = saved_filename;
5623
5624 return r;
5625}
5626
cabc336a
MM
5627/* Substitue into the ARG_TYPES of a function type. */
5628
5629tree
5630tsubst_arg_types (arg_types, args, in_decl)
5631 tree arg_types;
5632 tree args;
5633 tree in_decl;
5634{
5635 tree remaining_arg_types;
cabc336a
MM
5636 tree type;
5637
5638 if (!arg_types || arg_types == void_list_node)
5639 return arg_types;
5640
5641 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
5642 args, in_decl);
5643
5644 /* We use TYPE_MAIN_VARIANT is because top-level qualifiers don't
5645 matter on function types. */
5646 type = TYPE_MAIN_VARIANT (type_decays_to
5647 (tsubst (TREE_VALUE (arg_types),
5648 args, in_decl)));
5649
5650 /* Note that we do not substitute into default arguments here. The
5651 standard mandates that they be instantiated only when needed,
5652 which is done in build_over_call. */
5653 return hash_tree_cons_simple (TREE_PURPOSE (arg_types), type,
5654 remaining_arg_types);
5655
5656}
5657
297e73d8
MM
5658/* Substitute into the PARMS of a call-declarator. */
5659
5660tree
5661tsubst_call_declarator_parms (parms, args, in_decl)
5662 tree parms;
5663 tree args;
5664 tree in_decl;
5665{
5666 tree new_parms;
5667 tree type;
5668 tree defarg;
5669
5670 if (!parms || parms == void_list_node)
5671 return parms;
5672
5673 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
5674 args, in_decl);
5675
5676 /* Figure out the type of this parameter. */
5677 type = tsubst (TREE_VALUE (parms), args, in_decl);
5678
5679 /* Figure out the default argument as well. Note that we use
5680 tsubst_copy since the default argument is really an
5681 expression. */
5682 defarg = tsubst_expr (TREE_PURPOSE (parms), args, in_decl);
5683
5684 /* Chain this parameter on to the front of those we have already
5685 processed. We don't use hash_tree_cons because that function
5686 doesn't check TREE_PARMLIST. */
5687 new_parms = tree_cons (defarg, type, new_parms);
5688
5689 /* And note that these are parameters. */
5690 TREE_PARMLIST (new_parms) = 1;
5691
5692 return new_parms;
5693}
5694
ae58fa02
MM
5695/* Take the tree structure T and replace template parameters used therein
5696 with the argument vector ARGS. IN_DECL is an associated decl for
5697 diagnostics.
5698
5699 tsubst is used for dealing with types, decls and the like; for
5700 expressions, use tsubst_expr or tsubst_copy. */
5701
5702tree
5703tsubst (t, args, in_decl)
5704 tree t, args;
5705 tree in_decl;
5706{
0ecfe0b4 5707 tree type, r;
ae58fa02
MM
5708
5709 if (t == NULL_TREE || t == error_mark_node
5710 || t == integer_type_node
5711 || t == void_type_node
5712 || t == char_type_node
5713 || TREE_CODE (t) == NAMESPACE_DECL)
5714 return t;
5715
5716 if (TREE_CODE (t) == IDENTIFIER_NODE)
5717 type = IDENTIFIER_TYPE_VALUE (t);
5718 else
5719 type = TREE_TYPE (t);
5720 if (type == unknown_type_node)
5721 my_friendly_abort (42);
5722
5723 if (type && TREE_CODE (t) != FUNCTION_DECL
5724 && TREE_CODE (t) != TYPENAME_TYPE
5725 && TREE_CODE (t) != TEMPLATE_DECL
5726 && TREE_CODE (t) != IDENTIFIER_NODE)
5727 type = tsubst (type, args, in_decl);
5728
5729 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
5730 return tsubst_decl (t, args, type, in_decl);
5731
5732 switch (TREE_CODE (t))
5733 {
5734 case RECORD_TYPE:
5735 case UNION_TYPE:
5736 case ENUMERAL_TYPE:
5737 return tsubst_aggr_type (t, args, in_decl, /*entering_scope=*/0);
5738
5739 case ERROR_MARK:
5740 case IDENTIFIER_NODE:
5741 case OP_IDENTIFIER:
5742 case VOID_TYPE:
5743 case REAL_TYPE:
5744 case COMPLEX_TYPE:
5745 case BOOLEAN_TYPE:
5746 case INTEGER_CST:
5747 case REAL_CST:
5748 case STRING_CST:
5749 return t;
5750
5751 case INTEGER_TYPE:
5752 if (t == integer_type_node)
5753 return t;
5754
5755 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
5756 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
5757 return t;
d2e5ee5c 5758
5566b478 5759 {
ae58fa02 5760 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 5761
ae58fa02
MM
5762 max = tsubst_expr (max, args, in_decl);
5763 if (processing_template_decl)
5764 {
5765 tree itype = make_node (INTEGER_TYPE);
5766 TYPE_MIN_VALUE (itype) = size_zero_node;
5767 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
5768 integer_one_node);
5769 return itype;
5770 }
5771
0ecfe0b4
JM
5772 if (pedantic && integer_zerop (max))
5773 pedwarn ("creating array with size zero");
5774 else if (INT_CST_LT (max, integer_zero_node))
5775 {
5776 cp_error ("creating array with size `%E'", max);
5777 max = integer_one_node;
5778 }
5779
ae58fa02 5780 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
7ddedda4
MM
5781 if (!TREE_PERMANENT (max) && !allocation_temporary_p ())
5782 max = copy_to_permanent (max);
120ecb8a 5783 return build_index_type (max);
ae58fa02
MM
5784 }
5785
5786 case TEMPLATE_TYPE_PARM:
5787 case TEMPLATE_TEMPLATE_PARM:
5788 case TEMPLATE_PARM_INDEX:
5789 {
5790 int idx;
5791 int level;
5792 int levels;
0ecfe0b4
JM
5793
5794 r = NULL_TREE;
ae58fa02
MM
5795
5796 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
5797 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5798 {
5799 idx = TEMPLATE_TYPE_IDX (t);
5800 level = TEMPLATE_TYPE_LEVEL (t);
5801 }
5802 else
5803 {
5804 idx = TEMPLATE_PARM_IDX (t);
5805 level = TEMPLATE_PARM_LEVEL (t);
5806 }
5807
5808 if (TREE_VEC_LENGTH (args) > 0)
5809 {
5810 tree arg = NULL_TREE;
5811
5812 levels = TMPL_ARGS_DEPTH (args);
5813 if (level <= levels)
5814 arg = TMPL_ARG (args, level, idx);
5815
8b5b8b7c
MM
5816 if (arg == error_mark_node)
5817 return error_mark_node;
5818 else if (arg != NULL_TREE)
ae58fa02
MM
5819 {
5820 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5821 {
5822 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg))
5823 == 't', 0);
91063b51
MM
5824 return cp_build_qualified_type
5825 (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t));
ae58fa02
MM
5826 }
5827 else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5828 {
7ddedda4 5829 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
ae58fa02
MM
5830 {
5831 /* We are processing a type constructed from
5832 a template template parameter */
7ddedda4 5833 tree argvec = tsubst (TYPE_TI_ARGS (t),
ae58fa02 5834 args, in_decl);
ae58fa02
MM
5835
5836 /* We can get a TEMPLATE_TEMPLATE_PARM here when
5837 we are resolving nested-types in the signature of
5838 a member function templates.
5839 Otherwise ARG is a TEMPLATE_DECL and is the real
5840 template to be instantiated. */
5841 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
5842 arg = TYPE_NAME (arg);
5843
5844 r = lookup_template_class (DECL_NAME (arg),
5845 argvec, in_decl,
5846 DECL_CONTEXT (arg),
5847 /*entering_scope=*/0);
91063b51 5848 return cp_build_qualified_type (r, TYPE_QUALS (t));
ae58fa02
MM
5849 }
5850 else
5851 /* We are processing a template argument list. */
5852 return arg;
5853 }
5854 else
5855 return arg;
5856 }
5857 }
51924768
JM
5858 else
5859 my_friendly_abort (981018);
ae58fa02
MM
5860
5861 if (level == 1)
5862 /* This can happen during the attempted tsubst'ing in
5863 unify. This means that we don't yet have any information
5864 about the template parameter in question. */
5865 return t;
5866
5867 /* If we get here, we must have been looking at a parm for a
5868 more deeply nested template. Make a new version of this
5869 template parameter, but with a lower level. */
5870 switch (TREE_CODE (t))
5871 {
5872 case TEMPLATE_TYPE_PARM:
5873 case TEMPLATE_TEMPLATE_PARM:
5874 r = copy_node (t);
5875 TEMPLATE_TYPE_PARM_INDEX (r)
5876 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
5877 r, levels);
5878 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
5879 TYPE_MAIN_VARIANT (r) = r;
5880 TYPE_POINTER_TO (r) = NULL_TREE;
5881 TYPE_REFERENCE_TO (r) = NULL_TREE;
5882
5883 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7ddedda4 5884 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
ae58fa02 5885 {
7ddedda4
MM
5886 tree argvec = tsubst (TYPE_TI_ARGS (t), args, in_decl);
5887 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
ae58fa02
MM
5888 = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
5889 }
5890 break;
5891
5892 case TEMPLATE_PARM_INDEX:
5893 r = reduce_template_parm_level (t, type, levels);
5894 break;
5895
5896 default:
5897 my_friendly_abort (0);
5898 }
5899
5566b478 5900 return r;
ae58fa02 5901 }
5566b478 5902
8d08fdba
MS
5903 case TREE_LIST:
5904 {
5905 tree purpose, value, chain, result;
5906 int via_public, via_virtual, via_protected;
5907
5908 if (t == void_list_node)
5909 return t;
5910
5911 via_public = TREE_VIA_PUBLIC (t);
5912 via_protected = TREE_VIA_PROTECTED (t);
5913 via_virtual = TREE_VIA_VIRTUAL (t);
5914
5915 purpose = TREE_PURPOSE (t);
5916 if (purpose)
f7d98d58 5917 purpose = tsubst (purpose, args, in_decl);
8d08fdba
MS
5918 value = TREE_VALUE (t);
5919 if (value)
f7d98d58 5920 value = tsubst (value, args, in_decl);
8d08fdba
MS
5921 chain = TREE_CHAIN (t);
5922 if (chain && chain != void_type_node)
f7d98d58 5923 chain = tsubst (chain, args, in_decl);
8d08fdba
MS
5924 if (purpose == TREE_PURPOSE (t)
5925 && value == TREE_VALUE (t)
5926 && chain == TREE_CHAIN (t))
5927 return t;
5928 result = hash_tree_cons (via_public, via_virtual, via_protected,
5929 purpose, value, chain);
5930 TREE_PARMLIST (result) = TREE_PARMLIST (t);
5931 return result;
5932 }
5933 case TREE_VEC:
5566b478
MS
5934 if (type != NULL_TREE)
5935 {
1ceaad38
JM
5936 /* A binfo node. We always need to make a copy, of the node
5937 itself and of its BINFO_BASETYPES. */
85b71cf2 5938
5566b478
MS
5939 t = copy_node (t);
5940
c7449f2b
JM
5941 /* Make sure type isn't a typedef copy. */
5942 type = BINFO_TYPE (TYPE_BINFO (type));
5943
5566b478 5944 TREE_TYPE (t) = complete_type (type);
6633d636
MS
5945 if (IS_AGGR_TYPE (type))
5946 {
5947 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
5948 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
5949 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
5950 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
5951 }
5566b478
MS
5952 return t;
5953 }
85b71cf2
JM
5954
5955 /* Otherwise, a vector of template arguments. */
830bfa74 5956 return tsubst_template_arg_vector (t, args);
8d08fdba 5957
8d08fdba
MS
5958 case POINTER_TYPE:
5959 case REFERENCE_TYPE:
5960 {
8d08fdba 5961 enum tree_code code;
79a7c7fa 5962
8d08fdba
MS
5963 if (type == TREE_TYPE (t))
5964 return t;
5965
5966 code = TREE_CODE (t);
0ecfe0b4
JM
5967 if (TREE_CODE (type) == REFERENCE_TYPE
5968 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa
JM
5969 {
5970 static int last_line = 0;
5971 static char* last_file = 0;
5972
5973 /* We keep track of the last time we issued this error
5974 message to avoid spewing a ton of messages during a
5975 single bad template instantiation. */
5976 if (last_line != lineno ||
5977 last_file != input_filename)
5978 {
0ecfe0b4
JM
5979 if (TREE_CODE (type) == VOID_TYPE)
5980 cp_error ("forming reference to void");
5981 else
5982 cp_error ("forming %s to reference type `%T'",
5983 (code == POINTER_TYPE) ? "pointer" : "reference",
5984 type);
79a7c7fa
JM
5985 last_line = lineno;
5986 last_file = input_filename;
5987 }
5988
5989 /* Use the underlying type in an attempt at error
5990 recovery; maybe the user meant vector<int> and wrote
5991 vector<int&>, or some such. */
5992 if (code == REFERENCE_TYPE)
5993 r = type;
5994 else
5995 r = build_pointer_type (TREE_TYPE (type));
5996 }
5997 else if (code == POINTER_TYPE)
8d08fdba
MS
5998 r = build_pointer_type (type);
5999 else
6000 r = build_reference_type (type);
91063b51 6001 r = cp_build_qualified_type (r, TYPE_QUALS (t));
79a7c7fa 6002
8d08fdba
MS
6003 /* Will this ever be needed for TYPE_..._TO values? */
6004 layout_type (r);
6005 return r;
6006 }
a4443a08 6007 case OFFSET_TYPE:
0ecfe0b4
JM
6008 {
6009 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl);
6010 if (! IS_AGGR_TYPE (r))
6011 cp_error ("creating pointer to member of non-class type `%T'", r);
6012 return build_offset_type (r, type);
6013 }
8d08fdba
MS
6014 case FUNCTION_TYPE:
6015 case METHOD_TYPE:
6016 {
cabc336a
MM
6017 tree arg_types;
6018 tree raises;
c11b6f21 6019 tree fntype;
8d08fdba 6020
cabc336a
MM
6021 /* The TYPE_CONTEXT is not used for function/method types. */
6022 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6023
6024 /* Substitue the argument types. */
6025 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, in_decl);
8d08fdba
MS
6026
6027 /* Construct a new type node and return it. */
cabc336a
MM
6028 if (TREE_CODE (t) == FUNCTION_TYPE)
6029 fntype = build_function_type (type, arg_types);
0ecfe0b4
JM
6030 else
6031 {
6032 r = TREE_TYPE (TREE_VALUE (arg_types));
6033 if (! IS_AGGR_TYPE (r))
6034 cp_error ("creating pointer to member function of non-class type `%T'",
6035 r);
6036
6037 fntype = build_cplus_method_type (r, type, TREE_CHAIN (arg_types));
6038 }
91063b51 6039 fntype = build_qualified_type (fntype, TYPE_QUALS (t));
cabc336a
MM
6040
6041 /* Substitue the exception specification. */
6042 raises = TYPE_RAISES_EXCEPTIONS (t);
c11b6f21
MS
6043 if (raises)
6044 {
f7d98d58 6045 raises = tsubst (raises, args, in_decl);
c11b6f21
MS
6046 fntype = build_exception_variant (fntype, raises);
6047 }
6048 return fntype;
8d08fdba
MS
6049 }
6050 case ARRAY_TYPE:
6051 {
f7d98d58 6052 tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl);
8d08fdba
MS
6053 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6054 return t;
0ecfe0b4
JM
6055
6056 /* These checks should match the ones in grokdeclarator. */
6057 if (TREE_CODE (type) == VOID_TYPE)
6058 {
6059 cp_error ("creating array of void");
6060 type = build_pointer_type (type);
6061 }
6062 else if (TREE_CODE (type) == FUNCTION_TYPE)
6063 {
6064 cp_error ("creating array of functions `%T'", type);
6065 type = build_pointer_type (type);
6066 }
6067 else if (TREE_CODE (type) == REFERENCE_TYPE)
6068 {
6069 cp_error ("creating array of references `%T'", type);
6070 type = TREE_TYPE (type);
6071 }
6072
8d08fdba
MS
6073 r = build_cplus_array_type (type, domain);
6074 return r;
6075 }
6076
8d08fdba 6077 case PLUS_EXPR:
5566b478 6078 case MINUS_EXPR:
8d08fdba 6079 return fold (build (TREE_CODE (t), TREE_TYPE (t),
f7d98d58
JM
6080 tsubst (TREE_OPERAND (t, 0), args, in_decl),
6081 tsubst (TREE_OPERAND (t, 1), args, in_decl)));
8d08fdba
MS
6082
6083 case NEGATE_EXPR:
6084 case NOP_EXPR:
6085 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
f7d98d58 6086 tsubst (TREE_OPERAND (t, 0), args, in_decl)));
8d08fdba 6087
5566b478
MS
6088 case TYPENAME_TYPE:
6089 {
36a117a5
MM
6090 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, in_decl,
6091 /*entering_scope=*/1);
b2b7d40a 6092 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl);
ae58fa02
MM
6093
6094 /* Normally, make_typename_type does not require that the CTX
6095 have complete type in order to allow things like:
6096
6097 template <class T> struct S { typename S<T>::X Y; };
6098
6099 But, such constructs have already been resolved by this
6100 point, so here CTX really should have complete type, unless
6101 it's a partial instantiation. */
5326f06d
MM
6102 if (!uses_template_parms (ctx)
6103 && !TYPE_BEING_DEFINED (ctx)
ae58fa02
MM
6104 && !complete_type_or_else (ctx))
6105 return error_mark_node;
6106
b2b7d40a 6107 f = make_typename_type (ctx, f);
91063b51
MM
6108 return cp_build_qualified_type (f,
6109 CP_TYPE_QUALS (f)
6110 | CP_TYPE_QUALS (t));
5566b478
MS
6111 }
6112
6113 case INDIRECT_REF:
6114 return make_pointer_declarator
f7d98d58 6115 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6116
6117 case ADDR_EXPR:
6118 return make_reference_declarator
f7d98d58 6119 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6120
6121 case ARRAY_REF:
6122 return build_parse_node
f7d98d58
JM
6123 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl),
6124 tsubst_expr (TREE_OPERAND (t, 1), args, in_decl));
5566b478
MS
6125
6126 case CALL_EXPR:
c11b6f21 6127 return make_call_declarator
f7d98d58 6128 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
297e73d8 6129 tsubst_call_declarator_parms (TREE_OPERAND (t, 1), args, in_decl),
c11b6f21 6130 TREE_OPERAND (t, 2),
f7d98d58 6131 tsubst (TREE_TYPE (t), args, in_decl));
5566b478 6132
fc378698
MS
6133 case SCOPE_REF:
6134 return build_parse_node
f7d98d58
JM
6135 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl),
6136 tsubst (TREE_OPERAND (t, 1), args, in_decl));
fc378698 6137
b894fc05
JM
6138 case TYPEOF_TYPE:
6139 return TREE_TYPE (tsubst_expr (TYPE_FIELDS (t), args, in_decl));
6140
8d08fdba 6141 default:
5566b478 6142 sorry ("use of `%s' in template",
8d08fdba
MS
6143 tree_code_name [(int) TREE_CODE (t)]);
6144 return error_mark_node;
6145 }
6146}
6147
5566b478
MS
6148void
6149do_pushlevel ()
6150{
6151 emit_line_note (input_filename, lineno);
6152 pushlevel (0);
6153 clear_last_expr ();
6154 push_momentary ();
6155 expand_start_bindings (0);
6156}
6157
8d08fdba 6158tree
5566b478 6159do_poplevel ()
8d08fdba 6160{
5566b478 6161 tree t;
a703fb38 6162 int saved_warn_unused = 0;
8d08fdba 6163
85b71cf2
JM
6164 if (processing_template_decl)
6165 {
6166 saved_warn_unused = warn_unused;
6167 warn_unused = 0;
6168 }
8baa713c 6169 expand_end_bindings (getdecls (), kept_level_p (), 0);
85b71cf2
JM
6170 if (processing_template_decl)
6171 warn_unused = saved_warn_unused;
5566b478
MS
6172 t = poplevel (kept_level_p (), 1, 0);
6173 pop_momentary ();
6174 return t;
6175}
8d08fdba 6176
00d3396f
JM
6177/* Like tsubst, but deals with expressions. This function just replaces
6178 template parms; to finish processing the resultant expression, use
6179 tsubst_expr. */
6180
5566b478 6181tree
f7d98d58 6182tsubst_copy (t, args, in_decl)
98c1c668 6183 tree t, args;
5566b478
MS
6184 tree in_decl;
6185{
6186 enum tree_code code;
8452b1d3 6187 tree r;
8d08fdba 6188
5566b478
MS
6189 if (t == NULL_TREE || t == error_mark_node)
6190 return t;
6191
6192 code = TREE_CODE (t);
b7484fbe 6193
5566b478
MS
6194 switch (code)
6195 {
6196 case PARM_DECL:
a759e627 6197 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
5566b478
MS
6198
6199 case CONST_DECL:
ed44da02
MM
6200 {
6201 tree enum_type;
6202 tree v;
6203
6204 if (!DECL_CONTEXT (t))
6205 /* This is a global enumeration constant. */
6206 return t;
6207
6208 /* Unfortunately, we cannot just call lookup_name here.
6209 Consider:
6210
6211 template <int I> int f() {
6212 enum E { a = I };
6213 struct S { void g() { E e = a; } };
6214 };
6215
6216 When we instantiate f<7>::S::g(), say, lookup_name is not
6217 clever enough to find f<7>::a. */
6218 enum_type
6219 = tsubst_aggr_type (TREE_TYPE (t), args, in_decl,
6220 /*entering_scope=*/0);
6221
6222 for (v = TYPE_VALUES (enum_type);
6223 v != NULL_TREE;
6224 v = TREE_CHAIN (v))
6225 if (TREE_PURPOSE (v) == DECL_NAME (t))
6226 return TREE_VALUE (v);
6227
6228 /* We didn't find the name. That should never happen; if
6229 name-lookup found it during preliminary parsing, we
6230 should find it again here during instantiation. */
6231 my_friendly_abort (0);
6232 }
db04386f 6233 return t;
ed44da02 6234
5566b478
MS
6235 case FIELD_DECL:
6236 if (DECL_CONTEXT (t))
6237 {
0978790f 6238 tree ctx;
0978790f 6239
36a117a5
MM
6240 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
6241 /*entering_scope=*/1);
0978790f 6242 if (ctx != DECL_CONTEXT (t))
5566b478
MS
6243 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6244 }
6245 return t;
6246
6247 case VAR_DECL:
6248 case FUNCTION_DECL:
6249 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
f7d98d58 6250 t = tsubst (t, args, in_decl);
5566b478
MS
6251 mark_used (t);
6252 return t;
6253
98c1c668
JM
6254 case TEMPLATE_DECL:
6255 if (is_member_template (t))
f7d98d58 6256 return tsubst (t, args, in_decl);
98c1c668
JM
6257 else
6258 return t;
6259
672476cb
MM
6260 case LOOKUP_EXPR:
6261 {
6262 /* We must tsbust into a LOOKUP_EXPR in case the names to
6263 which it refers is a conversion operator; in that case the
6264 name will change. We avoid making unnecessary copies,
6265 however. */
6266
6267 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
6268
6269 if (id != TREE_OPERAND (t, 0))
6270 {
8452b1d3 6271 r = build_nt (LOOKUP_EXPR, id);
672476cb
MM
6272 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6273 t = r;
6274 }
6275
6276 return t;
6277 }
6278
5566b478
MS
6279 case CAST_EXPR:
6280 case REINTERPRET_CAST_EXPR:
e92cc029
MS
6281 case CONST_CAST_EXPR:
6282 case STATIC_CAST_EXPR:
6283 case DYNAMIC_CAST_EXPR:
51924768 6284 case NOP_EXPR:
5566b478 6285 return build1
f7d98d58
JM
6286 (code, tsubst (TREE_TYPE (t), args, in_decl),
6287 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6288
6289 case INDIRECT_REF:
6290 case PREDECREMENT_EXPR:
6291 case PREINCREMENT_EXPR:
6292 case POSTDECREMENT_EXPR:
6293 case POSTINCREMENT_EXPR:
6294 case NEGATE_EXPR:
6295 case TRUTH_NOT_EXPR:
b87692e5 6296 case BIT_NOT_EXPR:
5566b478
MS
6297 case ADDR_EXPR:
6298 case CONVERT_EXPR: /* Unary + */
6299 case SIZEOF_EXPR:
abff8e06 6300 case ALIGNOF_EXPR:
5566b478 6301 case ARROW_EXPR:
fc378698 6302 case THROW_EXPR:
5156628f 6303 case TYPEID_EXPR:
5566b478
MS
6304 return build1
6305 (code, NULL_TREE,
f7d98d58 6306 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
5566b478
MS
6307
6308 case PLUS_EXPR:
6309 case MINUS_EXPR:
6310 case MULT_EXPR:
6311 case TRUNC_DIV_EXPR:
6312 case CEIL_DIV_EXPR:
6313 case FLOOR_DIV_EXPR:
6314 case ROUND_DIV_EXPR:
6315 case EXACT_DIV_EXPR:
6316 case BIT_AND_EXPR:
6317 case BIT_ANDTC_EXPR:
6318 case BIT_IOR_EXPR:
6319 case BIT_XOR_EXPR:
6320 case TRUNC_MOD_EXPR:
6321 case FLOOR_MOD_EXPR:
6322 case TRUTH_ANDIF_EXPR:
6323 case TRUTH_ORIF_EXPR:
6324 case TRUTH_AND_EXPR:
6325 case TRUTH_OR_EXPR:
6326 case RSHIFT_EXPR:
6327 case LSHIFT_EXPR:
6328 case RROTATE_EXPR:
6329 case LROTATE_EXPR:
6330 case EQ_EXPR:
6331 case NE_EXPR:
6332 case MAX_EXPR:
6333 case MIN_EXPR:
6334 case LE_EXPR:
6335 case GE_EXPR:
6336 case LT_EXPR:
6337 case GT_EXPR:
6338 case COMPONENT_REF:
6339 case ARRAY_REF:
6340 case COMPOUND_EXPR:
6341 case SCOPE_REF:
6342 case DOTSTAR_EXPR:
6343 case MEMBER_REF:
6344 return build_nt
f7d98d58
JM
6345 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6346 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
5566b478
MS
6347
6348 case CALL_EXPR:
6349 {
6350 tree fn = TREE_OPERAND (t, 0);
f84b4be9 6351 if (is_overloaded_fn (fn))
f7d98d58 6352 fn = tsubst_copy (get_first_fn (fn), args, in_decl);
5566b478 6353 else
f84b4be9 6354 /* Sometimes FN is a LOOKUP_EXPR. */
f7d98d58 6355 fn = tsubst_copy (fn, args, in_decl);
5566b478 6356 return build_nt
f7d98d58 6357 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
5566b478
MS
6358 NULL_TREE);
6359 }
6360
6361 case METHOD_CALL_EXPR:
6362 {
6363 tree name = TREE_OPERAND (t, 0);
6364 if (TREE_CODE (name) == BIT_NOT_EXPR)
6365 {
f7d98d58 6366 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
7bae46f4 6367 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
6368 }
6369 else if (TREE_CODE (name) == SCOPE_REF
6370 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
6371 {
f7d98d58 6372 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
5566b478 6373 name = TREE_OPERAND (name, 1);
f7d98d58 6374 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
11686454 6375 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
6376 name = build_nt (SCOPE_REF, base, name);
6377 }
6378 else
f7d98d58 6379 name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
5566b478 6380 return build_nt
f7d98d58
JM
6381 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6382 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl),
5566b478
MS
6383 NULL_TREE);
6384 }
6385
67da3287 6386 case BIND_EXPR:
5566b478
MS
6387 case COND_EXPR:
6388 case MODOP_EXPR:
67da3287 6389 {
8452b1d3 6390 r = build_nt
f7d98d58
JM
6391 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6392 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6393 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
67da3287
MM
6394
6395 if (code == BIND_EXPR && !processing_template_decl)
6396 {
61cd552e 6397 /* This processing should really occur in tsubst_expr,
67da3287
MM
6398 However, tsubst_expr does not recurse into expressions,
6399 since it assumes that there aren't any statements
6400 inside them. Instead, it simply calls
6401 build_expr_from_tree. So, we need to expand the
6402 BIND_EXPR here. */
b4c4a9ec 6403 tree rtl_expr = begin_stmt_expr ();
544c188d 6404 tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl);
b4c4a9ec 6405 r = finish_stmt_expr (rtl_expr, block);
67da3287
MM
6406 }
6407
6408 return r;
6409 }
5566b478
MS
6410
6411 case NEW_EXPR:
6412 {
8452b1d3 6413 r = build_nt
f7d98d58
JM
6414 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6415 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
6416 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
5566b478
MS
6417 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
6418 return r;
6419 }
6420
6421 case DELETE_EXPR:
6422 {
8452b1d3 6423 r = build_nt
f7d98d58
JM
6424 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
6425 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
5566b478
MS
6426 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
6427 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
6428 return r;
6429 }
6430
386b8a85
JM
6431 case TEMPLATE_ID_EXPR:
6432 {
00d3396f 6433 /* Substituted template arguments */
f7d98d58 6434 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl);
00d3396f
JM
6435 tree chain;
6436 for (chain = targs; chain; chain = TREE_CHAIN (chain))
6437 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
6438
6439 return lookup_template_function
f7d98d58 6440 (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs);
386b8a85
JM
6441 }
6442
5566b478
MS
6443 case TREE_LIST:
6444 {
6445 tree purpose, value, chain;
6446
6447 if (t == void_list_node)
6448 return t;
6449
6450 purpose = TREE_PURPOSE (t);
6451 if (purpose)
f7d98d58 6452 purpose = tsubst_copy (purpose, args, in_decl);
5566b478
MS
6453 value = TREE_VALUE (t);
6454 if (value)
f7d98d58 6455 value = tsubst_copy (value, args, in_decl);
5566b478
MS
6456 chain = TREE_CHAIN (t);
6457 if (chain && chain != void_type_node)
f7d98d58 6458 chain = tsubst_copy (chain, args, in_decl);
5566b478
MS
6459 if (purpose == TREE_PURPOSE (t)
6460 && value == TREE_VALUE (t)
6461 && chain == TREE_CHAIN (t))
6462 return t;
6463 return tree_cons (purpose, value, chain);
6464 }
6465
6466 case RECORD_TYPE:
6467 case UNION_TYPE:
6468 case ENUMERAL_TYPE:
6469 case INTEGER_TYPE:
6470 case TEMPLATE_TYPE_PARM:
73b0fce8 6471 case TEMPLATE_TEMPLATE_PARM:
f84b4be9 6472 case TEMPLATE_PARM_INDEX:
5566b478
MS
6473 case POINTER_TYPE:
6474 case REFERENCE_TYPE:
6475 case OFFSET_TYPE:
6476 case FUNCTION_TYPE:
6477 case METHOD_TYPE:
6478 case ARRAY_TYPE:
6479 case TYPENAME_TYPE:
f84b4be9 6480 case TYPE_DECL:
f7d98d58 6481 return tsubst (t, args, in_decl);
5566b478 6482
e92cc029 6483 case IDENTIFIER_NODE:
8e710788
BK
6484 if (IDENTIFIER_TYPENAME_P (t)
6485 /* Make sure it's not just a variable named `__opr', for instance,
6486 which can occur in some existing code. */
6487 && TREE_TYPE (t))
e92cc029 6488 return build_typename_overload
f7d98d58 6489 (tsubst (TREE_TYPE (t), args, in_decl));
e92cc029
MS
6490 else
6491 return t;
6492
5156628f 6493 case CONSTRUCTOR:
8452b1d3
JM
6494 {
6495 r = build
6496 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE,
6497 tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl));
6498 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
6499 return r;
6500 }
5156628f 6501
5566b478
MS
6502 default:
6503 return t;
6504 }
6505}
6506
00d3396f
JM
6507/* Like tsubst_copy, but also does semantic processing and RTL expansion. */
6508
5566b478 6509tree
f7d98d58 6510tsubst_expr (t, args, in_decl)
98c1c668 6511 tree t, args;
5566b478
MS
6512 tree in_decl;
6513{
6514 if (t == NULL_TREE || t == error_mark_node)
6515 return t;
6516
5156628f 6517 if (processing_template_decl)
f7d98d58 6518 return tsubst_copy (t, args, in_decl);
5566b478
MS
6519
6520 switch (TREE_CODE (t))
8d08fdba 6521 {
5566b478
MS
6522 case RETURN_STMT:
6523 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6524 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
6525 args, in_decl));
5566b478
MS
6526 break;
6527
6528 case EXPR_STMT:
6529 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6530 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
6531 args, in_decl));
5566b478
MS
6532 break;
6533
6534 case DECL_STMT:
6535 {
6536 int i = suspend_momentary ();
67d743fe 6537 tree dcl, init;
5566b478
MS
6538
6539 lineno = TREE_COMPLEXITY (t);
6540 emit_line_note (input_filename, lineno);
6541 dcl = start_decl
f7d98d58
JM
6542 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
6543 tsubst (TREE_OPERAND (t, 1), args, in_decl),
a1774733 6544 TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE);
f7d98d58 6545 init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl);
5566b478 6546 cp_finish_decl
a0128b67 6547 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5566b478
MS
6548 resume_momentary (i);
6549 return dcl;
6550 }
8d08fdba 6551
5566b478
MS
6552 case FOR_STMT:
6553 {
6554 tree tmp;
5566b478 6555 lineno = TREE_COMPLEXITY (t);
5566b478 6556
ad321293
MM
6557 begin_for_stmt ();
6558 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
6559 tsubst_expr (tmp, args, in_decl);
6560 finish_for_init_stmt (NULL_TREE);
6561 finish_for_cond (tsubst_expr (FOR_COND (t), args,
6562 in_decl),
6563 NULL_TREE);
6564 tmp = tsubst_expr (FOR_EXPR (t), args, in_decl);
6565 finish_for_expr (tmp, NULL_TREE);
6566 tsubst_expr (FOR_BODY (t), args, in_decl);
6567 finish_for_stmt (tmp, NULL_TREE);
5566b478
MS
6568 }
6569 break;
8d08fdba 6570
5566b478
MS
6571 case WHILE_STMT:
6572 {
5566b478 6573 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6574 begin_while_stmt ();
6575 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
6576 args, in_decl),
6577 NULL_TREE);
6578 tsubst_expr (WHILE_BODY (t), args, in_decl);
6579 finish_while_stmt (NULL_TREE);
5566b478
MS
6580 }
6581 break;
8d08fdba 6582
5566b478
MS
6583 case DO_STMT:
6584 {
5566b478 6585 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6586 begin_do_stmt ();
6587 tsubst_expr (DO_BODY (t), args, in_decl);
6588 finish_do_body (NULL_TREE);
6589 finish_do_stmt (tsubst_expr (DO_COND (t), args,
6590 in_decl),
6591 NULL_TREE);
5566b478
MS
6592 }
6593 break;
a0a33927 6594
5566b478 6595 case IF_STMT:
8d08fdba 6596 {
5566b478 6597 tree tmp;
5566b478
MS
6598
6599 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6600 begin_if_stmt ();
6601 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
6602 args, in_decl),
6603 NULL_TREE);
db5ae43f 6604
ad321293 6605 if (tmp = THEN_CLAUSE (t), tmp)
db5ae43f 6606 {
f7d98d58 6607 tsubst_expr (tmp, args, in_decl);
ad321293 6608 finish_then_clause (NULL_TREE);
db5ae43f
MS
6609 }
6610
ad321293
MM
6611 if (tmp = ELSE_CLAUSE (t), tmp)
6612 {
6613 begin_else_clause ();
6614 tsubst_expr (tmp, args, in_decl);
6615 finish_else_clause (NULL_TREE);
6616 }
8d08fdba 6617
ad321293 6618 finish_if_stmt ();
8d08fdba 6619 }
5566b478 6620 break;
8d08fdba 6621
5566b478
MS
6622 case COMPOUND_STMT:
6623 {
ad321293 6624 tree substmt;
8d08fdba 6625
5566b478 6626 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6627 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
6628 for (substmt = COMPOUND_BODY (t);
6629 substmt != NULL_TREE;
6630 substmt = TREE_CHAIN (substmt))
f7d98d58 6631 tsubst_expr (substmt, args, in_decl);
ad321293
MM
6632 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
6633 NULL_TREE);
5566b478
MS
6634 }
6635 break;
8d08fdba 6636
5566b478
MS
6637 case BREAK_STMT:
6638 lineno = TREE_COMPLEXITY (t);
ad321293 6639 finish_break_stmt ();
5566b478 6640 break;
8d08fdba 6641
6467930b
MS
6642 case CONTINUE_STMT:
6643 lineno = TREE_COMPLEXITY (t);
ad321293 6644 finish_continue_stmt ();
6467930b
MS
6645 break;
6646
5566b478
MS
6647 case SWITCH_STMT:
6648 {
6649 tree val, tmp;
5566b478
MS
6650
6651 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6652 begin_switch_stmt ();
6653 val = tsubst_expr (SWITCH_COND (t), args, in_decl);
6654 finish_switch_cond (val);
5566b478
MS
6655
6656 if (tmp = TREE_OPERAND (t, 1), tmp)
f7d98d58 6657 tsubst_expr (tmp, args, in_decl);
8d08fdba 6658
ad321293 6659 finish_switch_stmt (val, NULL_TREE);
5566b478
MS
6660 }
6661 break;
6662
6663 case CASE_LABEL:
ad321293
MM
6664 finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl),
6665 tsubst_expr (CASE_HIGH (t), args, in_decl));
5566b478
MS
6666 break;
6667
6668 case LABEL_DECL:
6669 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
6670 DECL_NAME (t));
6671 if (t)
6672 expand_label (t);
6673 break;
6674
6675 case GOTO_STMT:
6676 lineno = TREE_COMPLEXITY (t);
aa09da44
MM
6677 t = GOTO_DESTINATION (t);
6678 if (TREE_CODE (t) != IDENTIFIER_NODE)
6679 /* Computed goto's must be tsubst'd into. On the other hand,
6680 non-computed gotos must not be; the identifier in question
6681 will have no binding. */
6682 t = tsubst_expr (t, args, in_decl);
6683 finish_goto_stmt (t);
ad321293
MM
6684 break;
6685
6686 case ASM_STMT:
6687 lineno = TREE_COMPLEXITY (t);
6688 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl),
6689 tsubst_expr (ASM_STRING (t), args, in_decl),
6690 tsubst_expr (ASM_OUTPUTS (t), args, in_decl),
6691 tsubst_expr (ASM_INPUTS (t), args, in_decl),
6692 tsubst_expr (ASM_CLOBBERS (t), args, in_decl));
5566b478 6693 break;
faf5394a
MS
6694
6695 case TRY_BLOCK:
6696 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6697 begin_try_block ();
6698 tsubst_expr (TRY_STMTS (t), args, in_decl);
6699 finish_try_block (NULL_TREE);
faf5394a 6700 {
ad321293 6701 tree handler = TRY_HANDLERS (t);
faf5394a 6702 for (; handler; handler = TREE_CHAIN (handler))
f7d98d58 6703 tsubst_expr (handler, args, in_decl);
faf5394a 6704 }
ad321293 6705 finish_handler_sequence (NULL_TREE);
faf5394a
MS
6706 break;
6707
6708 case HANDLER:
6709 lineno = TREE_COMPLEXITY (t);
ad321293
MM
6710 begin_handler ();
6711 if (HANDLER_PARMS (t))
faf5394a 6712 {
ad321293 6713 tree d = HANDLER_PARMS (t);
faf5394a 6714 expand_start_catch_block
f7d98d58
JM
6715 (tsubst (TREE_OPERAND (d, 1), args, in_decl),
6716 tsubst (TREE_OPERAND (d, 0), args, in_decl));
faf5394a
MS
6717 }
6718 else
6719 expand_start_catch_block (NULL_TREE, NULL_TREE);
ad321293
MM
6720 finish_handler_parms (NULL_TREE);
6721 tsubst_expr (HANDLER_BODY (t), args, in_decl);
6722 finish_handler (NULL_TREE);
faf5394a
MS
6723 break;
6724
b87692e5
MS
6725 case TAG_DEFN:
6726 lineno = TREE_COMPLEXITY (t);
6727 t = TREE_TYPE (t);
6728 if (TREE_CODE (t) == ENUMERAL_TYPE)
ed44da02 6729 tsubst (t, args, NULL_TREE);
b87692e5
MS
6730 break;
6731
5566b478 6732 default:
f7d98d58 6733 return build_expr_from_tree (tsubst_copy (t, args, in_decl));
5566b478
MS
6734 }
6735 return NULL_TREE;
8d08fdba
MS
6736}
6737
6ba89f8e 6738/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
6739 the template arguments in TARG_PTR. */
6740
5566b478
MS
6741tree
6742instantiate_template (tmpl, targ_ptr)
98c1c668 6743 tree tmpl, targ_ptr;
8d08fdba 6744{
5566b478 6745 tree fndecl;
36a117a5
MM
6746 tree gen_tmpl;
6747 tree spec;
5566b478
MS
6748 int i, len;
6749 struct obstack *old_fmp_obstack;
6750 extern struct obstack *function_maybepermanent_obstack;
36a117a5 6751 tree inner_args;
5566b478 6752
27fafc8d
JM
6753 if (tmpl == error_mark_node)
6754 return error_mark_node;
6755
386b8a85
JM
6756 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
6757
36a117a5
MM
6758 /* Check to see if we already have this specialization. */
6759 spec = retrieve_specialization (tmpl, targ_ptr);
6760 if (spec != NULL_TREE)
6761 return spec;
6762
6763 if (DECL_TEMPLATE_INFO (tmpl))
386b8a85 6764 {
36a117a5
MM
6765 /* The TMPL is a partial instantiation. To get a full set of
6766 arguments we must add the arguments used to perform the
6767 partial instantiation. */
6768 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
6769 targ_ptr);
6770 gen_tmpl = most_general_template (tmpl);
6771
6772 /* Check to see if we already have this specialization. */
6773 spec = retrieve_specialization (gen_tmpl, targ_ptr);
75650646
MM
6774 if (spec != NULL_TREE)
6775 return spec;
386b8a85 6776 }
36a117a5
MM
6777 else
6778 gen_tmpl = tmpl;
386b8a85 6779
5566b478
MS
6780 push_obstacks (&permanent_obstack, &permanent_obstack);
6781 old_fmp_obstack = function_maybepermanent_obstack;
6782 function_maybepermanent_obstack = &permanent_obstack;
8d08fdba 6783
36a117a5
MM
6784 len = DECL_NTPARMS (gen_tmpl);
6785 inner_args = innermost_args (targ_ptr);
5566b478
MS
6786 i = len;
6787 while (i--)
8d08fdba 6788 {
36a117a5 6789 tree t = TREE_VEC_ELT (inner_args, i);
5566b478
MS
6790 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
6791 {
6792 tree nt = target_type (t);
ec255269 6793 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5566b478 6794 {
8251199e
JM
6795 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
6796 cp_error (" trying to instantiate `%D'", gen_tmpl);
5566b478
MS
6797 fndecl = error_mark_node;
6798 goto out;
6799 }
6800 }
8d08fdba 6801 }
e66d884e 6802 targ_ptr = copy_to_permanent (targ_ptr);
8d08fdba 6803
5566b478 6804 /* substitute template parameters */
36a117a5
MM
6805 fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, gen_tmpl);
6806 /* The DECL_TI_TEMPLATE should always be the immediate parent
6807 template, not the most general template. */
6808 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 6809
824b9a4c
MS
6810 if (flag_external_templates)
6811 add_pending_template (fndecl);
6812
5566b478
MS
6813 out:
6814 function_maybepermanent_obstack = old_fmp_obstack;
6815 pop_obstacks ();
8d08fdba 6816
5566b478 6817 return fndecl;
8d08fdba 6818}
5566b478
MS
6819
6820/* Push the name of the class template into the scope of the instantiation. */
8d08fdba
MS
6821
6822void
5566b478
MS
6823overload_template_name (type)
6824 tree type;
8d08fdba 6825{
5566b478
MS
6826 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
6827 tree decl;
8d08fdba 6828
5566b478
MS
6829 if (IDENTIFIER_CLASS_VALUE (id)
6830 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
6831 return;
8d08fdba 6832
5566b478
MS
6833 decl = build_decl (TYPE_DECL, id, type);
6834 SET_DECL_ARTIFICIAL (decl);
6835 pushdecl_class_level (decl);
8d08fdba
MS
6836}
6837
956d6950 6838/* Like type_unification but designed specially to handle conversion
9f54c803
MM
6839 operators.
6840
6841 The FN is a TEMPLATE_DECL for a function. The ARGS are the
6842 arguments that are being used when calling it.
6843
6844 If FN is a conversion operator, RETURN_TYPE is the type desired as
6845 the result of the conversion operator.
6846
6847 The EXTRA_FN_ARG, if any, is the type of an additional
6848 parameter to be added to the beginning of FN's parameter list.
6849
6850 The other arguments are as for type_unification. */
98c1c668
JM
6851
6852int
d7684f2d
MM
6853fn_type_unification (fn, explicit_targs, targs, args, return_type,
6854 strict, extra_fn_arg)
386b8a85 6855 tree fn, explicit_targs, targs, args, return_type;
830bfa74 6856 unification_kind_t strict;
d7684f2d 6857 tree extra_fn_arg;
98c1c668 6858{
9f54c803 6859 tree parms;
98c1c668
JM
6860
6861 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
6862
9f54c803
MM
6863 parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6864
aa45967f 6865 if (DECL_CONV_FN_P (fn))
98c1c668
JM
6866 {
6867 /* This is a template conversion operator. Use the return types
6868 as well as the argument types. */
9f54c803
MM
6869 parms = scratch_tree_cons (NULL_TREE,
6870 TREE_TYPE (TREE_TYPE (fn)),
6871 parms);
6872 args = scratch_tree_cons (NULL_TREE, return_type, args);
98c1c668
JM
6873 }
6874
d7684f2d 6875 if (extra_fn_arg != NULL_TREE)
9f54c803 6876 parms = scratch_tree_cons (NULL_TREE, extra_fn_arg, parms);
d7684f2d 6877
7cd55873
MM
6878 /* We allow incomplete unification without an error message here
6879 because the standard doesn't seem to explicitly prohibit it. Our
6880 callers must be ready to deal with unification failures in any
6881 event. */
9f54c803
MM
6882 return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
6883 targs,
6884 parms,
6885 args,
6886 explicit_targs,
6887 strict, 1);
98c1c668
JM
6888}
6889
6890
8d08fdba
MS
6891/* Type unification.
6892
6893 We have a function template signature with one or more references to
6894 template parameters, and a parameter list we wish to fit to this
6895 template. If possible, produce a list of parameters for the template
6896 which will cause it to fit the supplied parameter list.
6897
6898 Return zero for success, 2 for an incomplete match that doesn't resolve
6899 all the types, and 1 for complete failure. An error message will be
6900 printed only for an incomplete match.
6901
9f54c803 6902 TPARMS[NTPARMS] is an array of template parameter types.
8d08fdba 6903
9f54c803
MM
6904 TARGS[NTPARMS] is the array into which the deduced template
6905 parameter values are placed. PARMS is the function template's
6906 signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument
6907 list we're trying to match against it.
6908
6909 The EXPLICIT_TARGS are explicit template arguments provided via a
6910 template-id.
6467930b 6911
830bfa74
MM
6912 The parameter STRICT is one of:
6913
6914 DEDUCE_CALL:
6915 We are deducing arguments for a function call, as in
6916 [temp.deduct.call].
6917
6918 DEDUCE_CONV:
6919 We are deducing arguments for a conversion function, as in
6920 [temp.deduct.conv].
6921
6922 DEDUCE_EXACT:
6923 We are deducing arguments when calculating the partial
6924 ordering between specializations of function or class
6925 templates, as in [temp.func.order] and [temp.class.order],
6926 when doing an explicit instantiation as in [temp.explicit],
9f54c803 6927 when determining an explicit specialization as in
830bfa74
MM
6928 [temp.expl.spec], or when taking the address of a function
6929 template, as in [temp.deduct.funcaddr]. */
8d08fdba
MS
6930
6931int
9f54c803 6932type_unification (tparms, targs, parms, args, explicit_targs,
386b8a85 6933 strict, allow_incomplete)
9f54c803 6934 tree tparms, targs, parms, args, explicit_targs;
830bfa74
MM
6935 unification_kind_t strict;
6936 int allow_incomplete;
386b8a85 6937{
050367a3 6938 int* explicit_mask;
386b8a85 6939 int i;
386b8a85 6940
830bfa74 6941 for (i = 0; i < TREE_VEC_LENGTH (tparms); i++)
050367a3 6942 TREE_VEC_ELT (targs, i) = NULL_TREE;
386b8a85 6943
9f54c803 6944 if (explicit_targs != NULL_TREE)
75650646
MM
6945 {
6946 tree arg_vec;
9f54c803 6947 arg_vec = coerce_template_parms (tparms, explicit_targs, NULL_TREE, 0,
744fac59 6948 0);
75650646
MM
6949
6950 if (arg_vec == error_mark_node)
6951 return 1;
386b8a85 6952
050367a3 6953 explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs));
7dee3f36 6954 bzero ((char *) explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs));
050367a3 6955
75650646
MM
6956 for (i = 0;
6957 i < TREE_VEC_LENGTH (arg_vec)
050367a3 6958 && TREE_VEC_ELT (arg_vec, i) != NULL_TREE;
75650646 6959 ++i)
050367a3
MM
6960 {
6961 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i);
6962 /* Let unify know that this argument was explicit. */
6963 explicit_mask [i] = 1;
6964 }
75650646 6965 }
050367a3
MM
6966 else
6967 explicit_mask = 0;
386b8a85 6968
830bfa74
MM
6969 return
6970 type_unification_real (tparms, targs, parms, args, 0,
6971 strict, allow_incomplete, explicit_mask);
6972}
6973
6974/* Adjust types before performing type deduction, as described in
6975 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
6976 sections are symmetric. PARM is the type of a function parameter
6977 or the return type of the conversion function. ARG is the type of
6978 the argument passed to the call, or the type of the value
6979 intialized with the result of the conversion function. */
386b8a85 6980
830bfa74
MM
6981void
6982maybe_adjust_types_for_deduction (strict, parm, arg)
6983 unification_kind_t strict;
6984 tree* parm;
6985 tree* arg;
6986{
6987 switch (strict)
6988 {
6989 case DEDUCE_CALL:
6990 break;
6991
6992 case DEDUCE_CONV:
6993 {
4c7d0dff
MM
6994 /* Swap PARM and ARG throughout the remainder of this
6995 function; the handling is precisely symmetric since PARM
6996 will initialize ARG rather than vice versa. */
830bfa74
MM
6997 tree* temp = parm;
6998 parm = arg;
6999 arg = temp;
7000 break;
7001 }
7002
7003 case DEDUCE_EXACT:
7004 /* There is nothing to do in this case. */
7005 return;
7006
7007 default:
7008 my_friendly_abort (0);
7009 }
7010
7011 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7012 {
7013 /* [temp.deduct.call]
7014
7015 If P is not a reference type:
7016
7017 --If A is an array type, the pointer type produced by the
7018 array-to-pointer standard conversion (_conv.array_) is
7019 used in place of A for type deduction; otherwise,
7020
7021 --If A is a function type, the pointer type produced by
7022 the function-to-pointer standard conversion
7023 (_conv.func_) is used in place of A for type deduction;
7024 otherwise,
7025
7026 --If A is a cv-qualified type, the top level
7027 cv-qualifiers of A's type are ignored for type
7028 deduction. */
7029 if (TREE_CODE (*arg) == ARRAY_TYPE)
7030 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 7031 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
7032 *arg = build_pointer_type (*arg);
7033 else
7034 *arg = TYPE_MAIN_VARIANT (*arg);
7035 }
7036
7037 /* [temp.deduct.call]
7038
7039 If P is a cv-qualified type, the top level cv-qualifiers
7040 of P's type are ignored for type deduction. If P is a
7041 reference type, the type referred to by P is used for
7042 type deduction. */
7043 *parm = TYPE_MAIN_VARIANT (*parm);
7044 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7045 *parm = TREE_TYPE (*parm);
386b8a85
JM
7046}
7047
050367a3
MM
7048/* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
7049 integers, with ones in positions corresponding to arguments in
9f54c803
MM
7050 targs that were provided explicitly, and zeros elsewhere.
7051
7052 If SUBR is 1, we're being called recursively (to unify the
7053 arguments of a function or method parameter of a function
7054 template). */
386b8a85 7055
4966381a 7056static int
3b3ba9f0 7057type_unification_real (tparms, targs, parms, args, subr,
050367a3
MM
7058 strict, allow_incomplete, explicit_mask)
7059 tree tparms, targs, parms, args;
830bfa74
MM
7060 int subr;
7061 unification_kind_t strict;
7062 int allow_incomplete;
050367a3 7063 int* explicit_mask;
8d08fdba
MS
7064{
7065 tree parm, arg;
7066 int i;
7067 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 7068 int sub_strict;
8d08fdba
MS
7069
7070 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
386b8a85
JM
7071 my_friendly_assert (parms == NULL_TREE
7072 || TREE_CODE (parms) == TREE_LIST, 290);
51c184be 7073 /* ARGS could be NULL (via a call from parse.y to
8d08fdba
MS
7074 build_x_function_call). */
7075 if (args)
7076 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
7077 my_friendly_assert (ntparms > 0, 292);
7078
830bfa74
MM
7079 switch (strict)
7080 {
7081 case DEDUCE_CALL:
7082 sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
7083 break;
7084
7085 case DEDUCE_CONV:
7086 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7087 break;
7088
7089 case DEDUCE_EXACT:
7090 sub_strict = UNIFY_ALLOW_NONE;
7091 break;
7092
7093 default:
7094 my_friendly_abort (0);
7095 }
7096
8d08fdba
MS
7097 while (parms
7098 && parms != void_list_node
7099 && args
7100 && args != void_list_node)
7101 {
7102 parm = TREE_VALUE (parms);
7103 parms = TREE_CHAIN (parms);
7104 arg = TREE_VALUE (args);
7105 args = TREE_CHAIN (args);
7106
7107 if (arg == error_mark_node)
7108 return 1;
7109 if (arg == unknown_type_node)
34016c81
JM
7110 /* We can't deduce anything from this, but we might get all the
7111 template args from other function args. */
7112 continue;
b7484fbe 7113
03e70705
JM
7114 /* Conversions will be performed on a function argument that
7115 corresponds with a function parameter that contains only
7116 non-deducible template parameters and explicitly specified
7117 template parameters. */
7118 if (! uses_template_parms (parm))
b7484fbe 7119 {
03e70705
JM
7120 tree type;
7121
7122 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7123 type = TREE_TYPE (arg);
7124 else
7125 {
7126 type = arg;
7127 arg = NULL_TREE;
7128 }
7129
830bfa74 7130 if (strict == DEDUCE_EXACT)
03e70705 7131 {
3bfdc719 7132 if (same_type_p (parm, type))
03e70705
JM
7133 continue;
7134 }
03e70705 7135 else
343c89cd
JM
7136 /* It might work; we shouldn't check now, because we might
7137 get into infinite recursion. Overload resolution will
7138 handle it. */
7139 continue;
03e70705 7140
b7484fbe
MS
7141 return 1;
7142 }
7143
8d08fdba
MS
7144#if 0
7145 if (TREE_CODE (arg) == VAR_DECL)
7146 arg = TREE_TYPE (arg);
7147 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
7148 arg = TREE_TYPE (arg);
7149#else
7150 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7151 {
7152 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
34016c81 7153 if (type_unknown_p (arg))
28cbf42c 7154 {
34016c81
JM
7155 /* [temp.deduct.type] A template-argument can be deduced from
7156 a pointer to function or pointer to member function
7157 argument if the set of overloaded functions does not
7158 contain function templates and at most one of a set of
7159 overloaded functions provides a unique match. */
7160
7161 if (resolve_overloaded_unification
7162 (tparms, targs, parm, arg, strict, sub_strict, explicit_mask)
7163 != 0)
7164 return 1;
7165 continue;
28cbf42c 7166 }
8d08fdba
MS
7167 arg = TREE_TYPE (arg);
7168 }
7169#endif
830bfa74
MM
7170 if (!subr)
7171 maybe_adjust_types_for_deduction (strict, &parm, &arg);
db5ae43f 7172
830bfa74 7173 switch (unify (tparms, targs, parm, arg, sub_strict,
050367a3 7174 explicit_mask))
8d08fdba
MS
7175 {
7176 case 0:
7177 break;
7178 case 1:
7179 return 1;
7180 }
7181 }
7182 /* Fail if we've reached the end of the parm list, and more args
7183 are present, and the parm list isn't variadic. */
7184 if (args && args != void_list_node && parms == void_list_node)
7185 return 1;
7186 /* Fail if parms are left and they don't have default values. */
7187 if (parms
7188 && parms != void_list_node
7189 && TREE_PURPOSE (parms) == NULL_TREE)
7190 return 1;
7191 if (!subr)
7192 for (i = 0; i < ntparms; i++)
050367a3 7193 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8d08fdba 7194 {
386b8a85 7195 if (!allow_incomplete)
8251199e 7196 error ("incomplete type unification");
8d08fdba
MS
7197 return 2;
7198 }
7199 return 0;
7200}
7201
34016c81
JM
7202/* Subroutine of type_unification_real. Args are like the variables at the
7203 call site. ARG is an overloaded function (or template-id); we try
7204 deducing template args from each of the overloads, and if only one
7205 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7206
7207static int
7208resolve_overloaded_unification (tparms, targs, parm, arg, strict,
7209 sub_strict, explicit_mask)
7210 tree tparms, targs, parm, arg;
7211 unification_kind_t strict;
7212 int sub_strict;
7213 int* explicit_mask;
7214{
7215 tree tempargs = copy_node (targs);
7216 int good = 0;
7217
7218 if (TREE_CODE (arg) == ADDR_EXPR)
7219 arg = TREE_OPERAND (arg, 0);
9f3d9e46 7220
d8f8dca1
MM
7221 if (TREE_CODE (arg) == COMPONENT_REF)
7222 /* Handle `&x' where `x' is some static or non-static member
7223 function name. */
7224 arg = TREE_OPERAND (arg, 1);
7225
9f3d9e46
JM
7226 /* Strip baselink information. */
7227 while (TREE_CODE (arg) == TREE_LIST)
7228 arg = TREE_VALUE (arg);
7229
34016c81
JM
7230 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
7231 {
7232 /* If we got some explicit template args, we need to plug them into
7233 the affected templates before we try to unify, in case the
7234 explicit args will completely resolve the templates in question. */
7235
7236 tree expl_subargs = TREE_OPERAND (arg, 1);
7237 arg = TREE_OPERAND (arg, 0);
7238
7239 for (; arg; arg = OVL_NEXT (arg))
7240 {
7241 tree fn = OVL_CURRENT (arg);
7242 tree subargs, elem;
7243
7244 if (TREE_CODE (fn) != TEMPLATE_DECL)
7245 continue;
7246
7247 subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs);
7248 if (subargs)
7249 {
7250 elem = tsubst (TREE_TYPE (fn), subargs, NULL_TREE);
d8f8dca1
MM
7251 if (TREE_CODE (elem) == METHOD_TYPE)
7252 elem = build_ptrmemfunc_type (build_pointer_type (elem));
e97e5263 7253 good += try_one_overload (tparms, targs, tempargs, parm, elem,
34016c81
JM
7254 strict, sub_strict, explicit_mask);
7255 }
7256 }
7257 }
7258 else if (TREE_CODE (arg) == OVERLOAD)
7259 {
7260 for (; arg; arg = OVL_NEXT (arg))
d8f8dca1
MM
7261 {
7262 tree type = TREE_TYPE (OVL_CURRENT (arg));
7263 if (TREE_CODE (type) == METHOD_TYPE)
7264 type = build_ptrmemfunc_type (build_pointer_type (type));
7265 good += try_one_overload (tparms, targs, tempargs, parm,
7266 type,
7267 strict, sub_strict, explicit_mask);
7268 }
34016c81
JM
7269 }
7270 else
7271 my_friendly_abort (981006);
7272
7273 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7274 to function or pointer to member function argument if the set of
7275 overloaded functions does not contain function templates and at most
7276 one of a set of overloaded functions provides a unique match.
7277
7278 So if we found multiple possibilities, we return success but don't
7279 deduce anything. */
7280
7281 if (good == 1)
7282 {
7283 int i = TREE_VEC_LENGTH (targs);
7284 for (; i--; )
7285 if (TREE_VEC_ELT (tempargs, i))
7286 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
7287 }
7288 if (good)
7289 return 0;
7290
7291 return 1;
7292}
7293
7294/* Subroutine of resolve_overloaded_unification; does deduction for a single
7295 overload. Fills TARGS with any deduced arguments, or error_mark_node if
7296 different overloads deduce different arguments for a given parm.
7297 Returns 1 on success. */
7298
7299static int
e97e5263 7300try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
34016c81 7301 sub_strict, explicit_mask)
e97e5263 7302 tree tparms, orig_targs, targs, parm, arg;
34016c81
JM
7303 unification_kind_t strict;
7304 int sub_strict;
7305 int* explicit_mask;
7306{
7307 int nargs;
7308 tree tempargs;
7309 int i;
7310
7311 /* [temp.deduct.type] A template-argument can be deduced from a pointer
7312 to function or pointer to member function argument if the set of
7313 overloaded functions does not contain function templates and at most
7314 one of a set of overloaded functions provides a unique match.
7315
7316 So if this is a template, just return success. */
7317
7318 if (uses_template_parms (arg))
7319 return 1;
7320
7321 maybe_adjust_types_for_deduction (strict, &parm, &arg);
7322
7323 /* We don't copy orig_targs for this because if we have already deduced
7324 some template args from previous args, unify would complain when we
7325 try to deduce a template parameter for the same argument, even though
7326 there isn't really a conflict. */
7327 nargs = TREE_VEC_LENGTH (targs);
7328 tempargs = make_scratch_vec (nargs);
7329
7330 if (unify (tparms, tempargs, parm, arg, sub_strict, explicit_mask) != 0)
7331 return 0;
7332
7333 /* First make sure we didn't deduce anything that conflicts with
e97e5263 7334 explicitly specified args. */
34016c81
JM
7335 for (i = nargs; i--; )
7336 {
7337 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 7338 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81
JM
7339
7340 if (elt == NULL_TREE)
7341 continue;
7342 else if (uses_template_parms (elt))
7343 {
7344 /* Since we're unifying against ourselves, we will fill in template
7345 args used in the function parm list with our own template parms.
7346 Discard them. */
7347 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
7348 continue;
7349 }
7350 else if (oldelt && ! template_args_equal (oldelt, elt))
7351 return 0;
7352 }
7353
7354 for (i = nargs; i--; )
7355 {
7356 tree elt = TREE_VEC_ELT (tempargs, i);
7357
7358 if (elt)
7359 TREE_VEC_ELT (targs, i) = elt;
7360 }
7361
7362 return 1;
7363}
7364
fcfb9f96
MM
7365/* Subroutine of get_template_base. */
7366
7367static tree
00aeceec 7368get_template_base_recursive (tparms, targs,
fcfb9f96
MM
7369 binfo, rval, template,
7370 via_virtual)
7371 tree tparms;
7372 tree targs;
fcfb9f96
MM
7373 tree binfo;
7374 tree rval;
7375 tree template;
7376 int via_virtual;
7377{
7378 tree binfos;
7379 int i, n_baselinks;
7380 tree type = BINFO_TYPE (binfo);
7381 tree tmpl = CLASSTYPE_TI_TEMPLATE (template);
7382
7383 if (CLASSTYPE_TEMPLATE_INFO (type)
7384 && CLASSTYPE_TI_TEMPLATE (type) == tmpl)
7385 {
fcfb9f96 7386 push_momentary ();
fcfb9f96 7387
00aeceec
MM
7388 i = unify (tparms,
7389 /* Use a temporary vector since we're doing
7390 speculative unification here. */
7391 make_temp_vec (TREE_VEC_LENGTH (targs)),
fcfb9f96
MM
7392 CLASSTYPE_TI_ARGS (template),
7393 CLASSTYPE_TI_ARGS (type),
00aeceec 7394 UNIFY_ALLOW_NONE, 0);
fcfb9f96
MM
7395
7396 pop_momentary ();
7397
7398 if (i == 0)
7399 {
7400 if (rval == NULL_TREE || rval == type)
7401 return type;
7402 else
7403 return error_mark_node;
7404 }
7405 }
7406
7407 binfos = BINFO_BASETYPES (binfo);
7408 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
7409
7410 /* Process base types. */
7411 for (i = 0; i < n_baselinks; i++)
7412 {
7413 tree base_binfo = TREE_VEC_ELT (binfos, i);
7414
7415 /* Find any specific instance of a virtual base, when searching with
7416 a binfo... */
7417 if (BINFO_MARKED (base_binfo) == 0)
7418 {
7419 int this_virtual = via_virtual || TREE_VIA_VIRTUAL (base_binfo);
7420
7421 /* When searching for a non-virtual, we cannot mark
7422 virtually found binfos. */
7423 if (! this_virtual)
7424 SET_BINFO_MARKED (base_binfo);
7425
00aeceec 7426 rval = get_template_base_recursive (tparms, targs,
fcfb9f96
MM
7427 base_binfo, rval,
7428 template, this_virtual);
7429 if (rval == error_mark_node)
7430 return rval;
7431 }
7432 }
7433
7434 return rval;
7435}
7436
7437/* Given a template type TEMPLATE and a class type or binfo node BINFO,
7438 find the unique base type in BINFO that is an instance of TEMPLATE.
7439 If there are more than one, return error_mark_node. TEMPLATE may
7440 be the type of a partial specialization, as well as a plain
7441 template type. Used by unify. */
7442
7443static tree
00aeceec 7444get_template_base (tparms, targs, template, binfo)
fcfb9f96
MM
7445 tree tparms;
7446 tree targs;
fcfb9f96
MM
7447 tree template;
7448 tree binfo;
7449{
7450 tree type = NULL_TREE, rval;
7451
7452 if (TREE_CODE (binfo) == TREE_VEC)
7453 type = BINFO_TYPE (binfo);
7454 else if (IS_AGGR_TYPE_CODE (TREE_CODE (binfo)))
7455 {
7456 type = complete_type (binfo);
7457 binfo = TYPE_BINFO (type);
7458 }
7459 else
7460 my_friendly_abort (92);
7461
00aeceec 7462 rval = get_template_base_recursive (tparms, targs,
fcfb9f96
MM
7463 binfo, NULL_TREE,
7464 template, 0);
7465 dfs_walk (binfo, dfs_unmark, markedp);
7466
7467 return rval;
7468}
7469
db2767b6
MM
7470/* Returns the level of DECL, which declares a template parameter. */
7471
04a81d94 7472int
db2767b6
MM
7473template_decl_level (decl)
7474 tree decl;
7475{
7476 switch (TREE_CODE (decl))
7477 {
7478 case TYPE_DECL:
7479 case TEMPLATE_DECL:
7480 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
7481
7482 case PARM_DECL:
7483 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
7484
7485 default:
7486 my_friendly_abort (0);
5ffe581d 7487 return 0;
db2767b6
MM
7488 }
7489}
7490
830bfa74
MM
7491/* Decide whether ARG can be unified with PARM, considering only the
7492 cv-qualifiers of each type, given STRICT as documented for unify.
7493 Returns non-zero iff the unification is OK on that basis.*/
e92cc029 7494
830bfa74
MM
7495int
7496check_cv_quals_for_unify (strict, arg, parm)
7497 int strict;
7498 tree arg;
7499 tree parm;
7500{
7501 return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
91063b51 7502 && !at_least_as_qualified_p (arg, parm))
830bfa74 7503 || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
91063b51 7504 && (!at_least_as_qualified_p (parm, arg))));
830bfa74
MM
7505}
7506
7507/* Takes parameters as for type_unification. Returns 0 if the
7508 type deduction suceeds, 1 otherwise. The parameter STRICT is a
7509 bitwise or of the following flags:
7510
7511 UNIFY_ALLOW_NONE:
7512 Require an exact match between PARM and ARG.
7513 UNIFY_ALLOW_MORE_CV_QUAL:
7514 Allow the deduced ARG to be more cv-qualified than ARG.
7515 UNIFY_ALLOW_LESS_CV_QUAL:
7516 Allow the deduced ARG to be less cv-qualified than ARG.
7517 UNIFY_ALLOW_DERIVED:
7518 Allow the deduced ARG to be a template base class of ARG,
7519 or a pointer to a template base class of the type pointed to by
161c12b0
JM
7520 ARG.
7521 UNIFY_ALLOW_INTEGER:
7522 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
7523 case for more information. */
830bfa74
MM
7524
7525int
7526unify (tparms, targs, parm, arg, strict, explicit_mask)
050367a3 7527 tree tparms, targs, parm, arg;
830bfa74 7528 int strict;
050367a3 7529 int* explicit_mask;
8d08fdba
MS
7530{
7531 int idx;
050367a3 7532 tree targ;
db2767b6 7533 tree tparm;
8d08fdba
MS
7534
7535 /* I don't think this will do the right thing with respect to types.
7536 But the only case I've seen it in so far has been array bounds, where
7537 signedness is the only information lost, and I think that will be
7538 okay. */
7539 while (TREE_CODE (parm) == NOP_EXPR)
7540 parm = TREE_OPERAND (parm, 0);
7541
7542 if (arg == error_mark_node)
7543 return 1;
7544 if (arg == unknown_type_node)
34016c81
JM
7545 /* We can't deduce anything from this, but we might get all the
7546 template args from other function args. */
7547 return 0;
7548
db2767b6 7549 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 7550 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
7551 template parameters. We might need them if we're trying to
7552 figure out which of two things is more specialized. */
7553 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
7554 return 0;
7555
830bfa74
MM
7556 /* Immediately reject some pairs that won't unify because of
7557 cv-qualification mismatches. */
7558 if (TREE_CODE (arg) == TREE_CODE (parm)
49432171 7559 && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
830bfa74
MM
7560 /* We check the cv-qualifiers when unifying with template type
7561 parameters below. We want to allow ARG `const T' to unify with
7562 PARM `T' for example, when computing which of two templates
7563 is more specialized, for example. */
7564 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
7565 && !check_cv_quals_for_unify (strict, arg, parm))
49432171
JM
7566 return 1;
7567
8d08fdba
MS
7568 switch (TREE_CODE (parm))
7569 {
2ca340ae
JM
7570 case TYPENAME_TYPE:
7571 /* In a type which contains a nested-name-specifier, template
7572 argument values cannot be deduced for template parameters used
7573 within the nested-name-specifier. */
7574 return 0;
7575
8d08fdba 7576 case TEMPLATE_TYPE_PARM:
73b0fce8 7577 case TEMPLATE_TEMPLATE_PARM:
db2767b6
MM
7578 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
7579
7580 if (TEMPLATE_TYPE_LEVEL (parm)
7581 != template_decl_level (tparm))
7582 /* The PARM is not one we're trying to unify. Just check
7583 to see if it matches ARG. */
7584 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 7585 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 7586 idx = TEMPLATE_TYPE_IDX (parm);
050367a3 7587 targ = TREE_VEC_ELT (targs, idx);
db2767b6 7588 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 7589
73b0fce8 7590 /* Check for mixed types and values. */
db2767b6
MM
7591 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
7592 && TREE_CODE (tparm) != TYPE_DECL)
7593 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
7594 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
7595 return 1;
7596
050367a3
MM
7597 if (!strict && targ != NULL_TREE
7598 && explicit_mask && explicit_mask[idx])
73b0fce8
KL
7599 /* An explicit template argument. Don't even try to match
7600 here; the overload resolution code will manage check to
7601 see whether the call is legal. */
7602 return 0;
7603
db2767b6 7604 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
73b0fce8 7605 {
7ddedda4 7606 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm))
db2767b6
MM
7607 {
7608 /* We arrive here when PARM does not involve template
7609 specialization. */
73b0fce8 7610
db2767b6
MM
7611 /* ARG must be constructed from a template class. */
7612 if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
7613 return 1;
73b0fce8 7614
db2767b6 7615 {
7ddedda4
MM
7616 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
7617 tree parmvec = TYPE_TI_ARGS (parm);
db2767b6
MM
7618 tree argvec = CLASSTYPE_TI_ARGS (arg);
7619 tree argtmplvec
7620 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
7621 int i;
7622
7623 /* The parameter and argument roles have to be switched here
7624 in order to handle default arguments properly. For example,
7625 template<template <class> class TT> void f(TT<int>)
7626 should be able to accept vector<int> which comes from
744fac59 7627 template <class T, class Allocator = allocator>
73b0fce8
KL
7628 class vector. */
7629
fcfcdfc8 7630 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
db2767b6
MM
7631 == error_mark_node)
7632 return 1;
73b0fce8 7633
6b9b6b15
JM
7634 /* Deduce arguments T, i from TT<T> or TT<i>.
7635 We check each element of PARMVEC and ARGVEC individually
7636 rather than the whole TREE_VEC since they can have
7637 different number of elements. */
7638
db2767b6
MM
7639 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
7640 {
7641 tree t = TREE_VEC_ELT (parmvec, i);
73b0fce8 7642
830bfa74
MM
7643 if (unify (tparms, targs, t,
7644 TREE_VEC_ELT (argvec, i),
7645 UNIFY_ALLOW_NONE, explicit_mask))
db2767b6
MM
7646 return 1;
7647 }
73b0fce8 7648 }
db2767b6
MM
7649 arg = CLASSTYPE_TI_TEMPLATE (arg);
7650 }
7651 }
7652 else
7653 {
830bfa74
MM
7654 /* If PARM is `const T' and ARG is only `int', we don't have
7655 a match unless we are allowing additional qualification.
7656 If ARG is `const int' and PARM is just `T' that's OK;
7657 that binds `const int' to `T'. */
7658 if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL,
7659 arg, parm))
db2767b6
MM
7660 return 1;
7661
830bfa74
MM
7662 /* Consider the case where ARG is `const volatile int' and
7663 PARM is `const T'. Then, T should be `volatile int'. */
7664 arg =
91063b51
MM
7665 cp_build_qualified_type (arg,
7666 CP_TYPE_QUALS (arg)
7667 & ~CP_TYPE_QUALS (parm));
73b0fce8
KL
7668 }
7669
7670 /* Simple cases: Value already set, does match or doesn't. */
3a56f0ab 7671 if (targ != NULL_TREE
3bfdc719 7672 && (same_type_p (targ, arg)
3a56f0ab 7673 || (explicit_mask && explicit_mask[idx])))
73b0fce8 7674 return 0;
050367a3 7675 else if (targ)
73b0fce8 7676 return 1;
61cd552e
MM
7677
7678 /* Make sure that ARG is not a variable-sized array. (Note that
7679 were talking about variable-sized arrays (like `int[n]'),
7680 rather than arrays of unknown size (like `int[]').) We'll
7681 get very confused by such a type since the bound of the array
7682 will not be computable in an instantiation. Besides, such
7683 types are not allowed in ISO C++, so we can do as we please
7684 here. */
7685 if (TREE_CODE (arg) == ARRAY_TYPE
7686 && !uses_template_parms (arg)
7687 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
7688 != INTEGER_CST))
7689 return 1;
7690
050367a3 7691 TREE_VEC_ELT (targs, idx) = arg;
73b0fce8
KL
7692 return 0;
7693
f84b4be9 7694 case TEMPLATE_PARM_INDEX:
db2767b6
MM
7695 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
7696
7697 if (TEMPLATE_PARM_LEVEL (parm)
7698 != template_decl_level (tparm))
7699 /* The PARM is not one we're trying to unify. Just check
7700 to see if it matches ARG. */
7701 return (TREE_CODE (arg) == TREE_CODE (parm)
67ffc812 7702 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
db2767b6 7703
f84b4be9 7704 idx = TEMPLATE_PARM_IDX (parm);
050367a3 7705 targ = TREE_VEC_ELT (targs, idx);
db2767b6 7706
050367a3 7707 if (targ)
8d08fdba 7708 {
67ffc812 7709 int i = (cp_tree_equal (targ, arg) > 0);
312e7d50
JM
7710 if (i == 1)
7711 return 0;
7712 else if (i == 0)
7713 return 1;
7714 else
7715 my_friendly_abort (42);
8d08fdba 7716 }
8d08fdba 7717
161c12b0
JM
7718 /* [temp.deduct.type] If, in the declaration of a function template
7719 with a non-type template-parameter, the non-type
7720 template-parameter is used in an expression in the function
7721 parameter-list and, if the corresponding template-argument is
7722 deduced, the template-argument type shall match the type of the
7723 template-parameter exactly, except that a template-argument
7724 deduced from an array bound may be of any integral type. */
7725 if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm)))
7726 /* OK */;
7727 else if ((strict & UNIFY_ALLOW_INTEGER)
7728 && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
7729 || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE))
7730 /* OK */;
7731 else
7732 return 1;
7733
050367a3 7734 TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg);
8d08fdba
MS
7735 return 0;
7736
7737 case POINTER_TYPE:
830bfa74
MM
7738 {
7739 int sub_strict;
4ac14744 7740
830bfa74
MM
7741 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
7742 return (unify (tparms, targs, parm,
7743 TYPE_PTRMEMFUNC_FN_TYPE (arg), strict,
7744 explicit_mask));
7745
7746 if (TREE_CODE (arg) != POINTER_TYPE)
7747 return 1;
7748
7749 /* [temp.deduct.call]
7750
7751 A can be another pointer or pointer to member type that can
7752 be converted to the deduced A via a qualification
7753 conversion (_conv.qual_).
7754
7755 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
7756 This will allow for additional cv-qualification of the
7757 pointed-to types if appropriate. In general, this is a bit
7758 too generous; we are only supposed to allow qualification
7759 conversions and this method will allow an ARG of char** and
7760 a deduced ARG of const char**. However, overload
7761 resolution will subsequently invalidate the candidate, so
7762 this is probably OK. */
7763 sub_strict = strict;
7764
7765 if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE
7766 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg)))
7767 /* The derived-to-base conversion only persists through one
7768 level of pointers. */
7769 sub_strict &= ~UNIFY_ALLOW_DERIVED;
7770
7771 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE
7772 (arg), sub_strict, explicit_mask);
7773 }
8d08fdba
MS
7774
7775 case REFERENCE_TYPE:
830bfa74
MM
7776 if (TREE_CODE (arg) != REFERENCE_TYPE)
7777 return 1;
7778 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7779 UNIFY_ALLOW_NONE, explicit_mask);
8d08fdba
MS
7780
7781 case ARRAY_TYPE:
7782 if (TREE_CODE (arg) != ARRAY_TYPE)
7783 return 1;
3042d5be
MM
7784 if ((TYPE_DOMAIN (parm) == NULL_TREE)
7785 != (TYPE_DOMAIN (arg) == NULL_TREE))
7786 return 1;
7787 if (TYPE_DOMAIN (parm) != NULL_TREE
830bfa74
MM
7788 && unify (tparms, targs, TYPE_DOMAIN (parm),
7789 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE, explicit_mask) != 0)
8d08fdba 7790 return 1;
830bfa74
MM
7791 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7792 UNIFY_ALLOW_NONE, explicit_mask);
8d08fdba
MS
7793
7794 case REAL_TYPE:
37c46b43 7795 case COMPLEX_TYPE:
8d08fdba 7796 case INTEGER_TYPE:
42976354 7797 case BOOLEAN_TYPE:
5ad5a526 7798 case VOID_TYPE:
f376e137
MS
7799 if (TREE_CODE (arg) != TREE_CODE (parm))
7800 return 1;
7801
514a1f18
JM
7802 if (TREE_CODE (parm) == INTEGER_TYPE
7803 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8d08fdba
MS
7804 {
7805 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
830bfa74 7806 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
161c12b0
JM
7807 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER,
7808 explicit_mask))
8d08fdba
MS
7809 return 1;
7810 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
830bfa74 7811 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
161c12b0
JM
7812 TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER,
7813 explicit_mask))
8d08fdba
MS
7814 return 1;
7815 }
514a1f18
JM
7816 /* We use the TYPE_MAIN_VARIANT since we have already
7817 checked cv-qualification at the top of the
7818 function. */
3bfdc719
MM
7819 else if (!same_type_p (TYPE_MAIN_VARIANT (arg),
7820 TYPE_MAIN_VARIANT (parm)))
ca79f85d
JM
7821 return 1;
7822
8d08fdba
MS
7823 /* As far as unification is concerned, this wins. Later checks
7824 will invalidate it if necessary. */
7825 return 0;
7826
7827 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 7828 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 7829 case INTEGER_CST:
bd6dd845
MS
7830 while (TREE_CODE (arg) == NOP_EXPR)
7831 arg = TREE_OPERAND (arg, 0);
7832
8d08fdba
MS
7833 if (TREE_CODE (arg) != INTEGER_CST)
7834 return 1;
7835 return !tree_int_cst_equal (parm, arg);
7836
8d08fdba
MS
7837 case TREE_VEC:
7838 {
7839 int i;
7840 if (TREE_CODE (arg) != TREE_VEC)
7841 return 1;
7842 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
7843 return 1;
7844 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
830bfa74 7845 if (unify (tparms, targs,
8d08fdba 7846 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
830bfa74 7847 UNIFY_ALLOW_NONE, explicit_mask))
8d08fdba
MS
7848 return 1;
7849 return 0;
7850 }
7851
8d08fdba 7852 case RECORD_TYPE:
f181d4ae 7853 case UNION_TYPE:
db5ae43f 7854 if (TYPE_PTRMEMFUNC_FLAG (parm))
830bfa74 7855 return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm),
050367a3 7856 arg, strict, explicit_mask);
8d08fdba 7857
f181d4ae 7858 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 7859 return 1;
830bfa74 7860
5db698f6 7861 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 7862 {
6467930b 7863 tree t = NULL_TREE;
830bfa74
MM
7864 if (strict & UNIFY_ALLOW_DERIVED)
7865 /* [temp.deduct.call]
7866
7867 If P is a class, and P has the form template-id, then A
7868 can be a derived class of the deduced A. Likewise, if
7869 P is a pointer to a class of the form template-id, A
7870 can be a pointer to a derived class pointed to by the
6df47b06
MM
7871 deduced A.
7872
7873 The call to get_template_base also handles the case
7874 where PARM and ARG are the same type, i.e., where no
7875 derivation is involved. */
00aeceec 7876 t = get_template_base (tparms, targs, parm, arg);
6df47b06 7877 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9fbf56f7
MM
7878 && (CLASSTYPE_TI_TEMPLATE (parm)
7879 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
7880 /* Perhaps PARM is something like S<U> and ARG is S<int>.
7881 Then, we should unify `int' and `U'. */
6467930b 7882 t = arg;
6df47b06 7883
6467930b 7884 if (! t || t == error_mark_node)
5566b478 7885 return 1;
6467930b 7886
830bfa74
MM
7887 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
7888 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE,
7889 explicit_mask);
5566b478 7890 }
3bfdc719
MM
7891 else if (!same_type_p (TYPE_MAIN_VARIANT (parm),
7892 TYPE_MAIN_VARIANT (arg)))
5566b478 7893 return 1;
a4443a08 7894 return 0;
8d08fdba
MS
7895
7896 case METHOD_TYPE:
8d08fdba 7897 case FUNCTION_TYPE:
830bfa74 7898 if (TREE_CODE (arg) != TREE_CODE (parm))
8d08fdba 7899 return 1;
830bfa74
MM
7900
7901 if (unify (tparms, targs, TREE_TYPE (parm),
7902 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
28cbf42c 7903 return 1;
386b8a85 7904 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3b3ba9f0 7905 TYPE_ARG_TYPES (arg), 1,
830bfa74 7906 DEDUCE_EXACT, 0, explicit_mask);
a4443a08
MS
7907
7908 case OFFSET_TYPE:
7909 if (TREE_CODE (arg) != OFFSET_TYPE)
7910 return 1;
830bfa74
MM
7911 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
7912 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
a4443a08 7913 return 1;
830bfa74 7914 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
9f3d9e46 7915 strict, explicit_mask);
a4443a08 7916
f62dbf03 7917 case CONST_DECL:
830bfa74 7918 if (arg != decl_constant_value (parm))
f62dbf03
JM
7919 return 1;
7920 return 0;
7921
027905b4
KL
7922 case TEMPLATE_DECL:
7923 /* Matched cases are handled by the ARG == PARM test above. */
7924 return 1;
7925
53929c47
JM
7926 case MINUS_EXPR:
7927 if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
7928 {
7929 /* We handle this case specially, since it comes up with
7930 arrays. In particular, something like:
7931
7932 template <int N> void f(int (&x)[N]);
7933
7934 Here, we are trying to unify the range type, which
7935 looks like [0 ... (N - 1)]. */
7936 tree t, t1, t2;
7937 t1 = TREE_OPERAND (parm, 0);
7938 t2 = TREE_OPERAND (parm, 1);
7939
7940 /* Should this be a regular fold? */
7941 t = maybe_fold_nontype_arg (build (PLUS_EXPR,
7942 integer_type_node,
7943 arg, t2));
7944
161c12b0 7945 return unify (tparms, targs, t1, t, strict, explicit_mask);
53929c47
JM
7946 }
7947 /* else fall through */
7948
8d08fdba 7949 default:
050367a3
MM
7950 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
7951 {
7952 /* We're looking at an expression. This can happen with
7953 something like:
7954
7955 template <int I>
7956 void foo(S<I>, S<I + 2>);
7957
7958 If the call looked like:
7959
7960 foo(S<2>(), S<4>());
7961
7962 we would have already matched `I' with `2'. Now, we'd
7963 like to know if `4' matches `I + 2'. So, we substitute
7964 into that expression, and fold constants, in the hope of
7965 figuring it out. */
7966 tree t =
7967 maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE));
53929c47 7968 tree a = maybe_fold_nontype_arg (arg);
050367a3 7969
53929c47 7970 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t))))
050367a3 7971 /* Good, we mangaged to simplify the exression. */
53929c47 7972 return unify (tparms, targs, t, a, UNIFY_ALLOW_NONE,
050367a3
MM
7973 explicit_mask);
7974 else
7975 /* Bad, we couldn't simplify this. Assume it doesn't
7976 unify. */
7977 return 1;
7978 }
7979 else
b4c4a9ec
MM
7980 sorry ("use of `%s' in template type unification",
7981 tree_code_name [(int) TREE_CODE (parm)]);
050367a3 7982
8d08fdba
MS
7983 return 1;
7984 }
7985}
8d08fdba 7986\f
03d0f4af
MM
7987/* Called if RESULT is explicitly instantiated, or is a member of an
7988 explicitly instantiated class, or if using -frepo and the
7989 instantiation of RESULT has been assigned to this file. */
7990
faae18ab 7991void
5566b478 7992mark_decl_instantiated (result, extern_p)
faae18ab
MS
7993 tree result;
7994 int extern_p;
7995{
75650646
MM
7996 if (TREE_CODE (result) != FUNCTION_DECL)
7997 /* The TREE_PUBLIC flag for function declarations will have been
7998 set correctly by tsubst. */
7999 TREE_PUBLIC (result) = 1;
faae18ab
MS
8000
8001 if (! extern_p)
8002 {
8003 DECL_INTERFACE_KNOWN (result) = 1;
8004 DECL_NOT_REALLY_EXTERN (result) = 1;
a7d87521 8005
1a408d07
JM
8006 /* Always make artificials weak. */
8007 if (DECL_ARTIFICIAL (result) && flag_weak)
8008 comdat_linkage (result);
a7d87521
JM
8009 /* For WIN32 we also want to put explicit instantiations in
8010 linkonce sections. */
1a408d07 8011 else if (TREE_PUBLIC (result))
b385c841 8012 maybe_make_one_only (result);
faae18ab 8013 }
f49422da
MS
8014 else if (TREE_CODE (result) == FUNCTION_DECL)
8015 mark_inline_for_output (result);
faae18ab
MS
8016}
8017
e1467ff2
MM
8018/* Given two function templates PAT1 and PAT2, and explicit template
8019 arguments EXPLICIT_ARGS return:
6467930b
MS
8020
8021 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8022 -1 if PAT2 is more specialized than PAT1.
8023 0 if neither is more specialized. */
8024
8025int
e1467ff2
MM
8026more_specialized (pat1, pat2, explicit_args)
8027 tree pat1, pat2, explicit_args;
6467930b 8028{
98c1c668 8029 tree targs;
73aad9b9 8030 int winner = 0;
6467930b 8031
76b9a14d 8032 targs = get_bindings_overload (pat1, pat2, explicit_args);
73aad9b9 8033 if (targs)
36a117a5 8034 --winner;
6467930b 8035
76b9a14d 8036 targs = get_bindings_overload (pat2, pat1, explicit_args);
73aad9b9 8037 if (targs)
36a117a5 8038 ++winner;
6467930b 8039
73aad9b9
JM
8040 return winner;
8041}
6467930b 8042
73aad9b9 8043/* Given two class template specialization list nodes PAT1 and PAT2, return:
6467930b 8044
73aad9b9
JM
8045 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8046 -1 if PAT2 is more specialized than PAT1.
8047 0 if neither is more specialized. */
8048
8049int
8050more_specialized_class (pat1, pat2)
8051 tree pat1, pat2;
8052{
8053 tree targs;
8054 int winner = 0;
8055
36a117a5
MM
8056 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
8057 TREE_PURPOSE (pat2));
73aad9b9
JM
8058 if (targs)
8059 --winner;
8060
36a117a5
MM
8061 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
8062 TREE_PURPOSE (pat1));
73aad9b9 8063 if (targs)
6467930b
MS
8064 ++winner;
8065
8066 return winner;
8067}
73aad9b9
JM
8068
8069/* Return the template arguments that will produce the function signature
e1467ff2 8070 DECL from the function template FN, with the explicit template
76b9a14d
JM
8071 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
8072 also match. */
73aad9b9 8073
76b9a14d
JM
8074static tree
8075get_bindings_real (fn, decl, explicit_args, check_rettype)
e1467ff2 8076 tree fn, decl, explicit_args;
76b9a14d 8077 int check_rettype;
73aad9b9 8078{
98c1c668 8079 int ntparms = DECL_NTPARMS (fn);
e66d884e 8080 tree targs = make_scratch_vec (ntparms);
d7684f2d
MM
8081 tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
8082 tree extra_fn_arg = NULL_TREE;
98c1c668
JM
8083 int i;
8084
d7684f2d
MM
8085 if (DECL_STATIC_FUNCTION_P (fn)
8086 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
8087 {
8088 /* Sometimes we are trying to figure out what's being
8089 specialized by a declaration that looks like a method, and it
8090 turns out to be a static member function. */
8091 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn))
8092 && !is_member_template (fn))
8093 /* The natural thing to do here seems to be to remove the
8094 spurious `this' parameter from the DECL, but that prevents
8095 unification from making use of the class type. So,
8096 instead, we have fn_type_unification add to the parameters
8097 for FN. */
8098 extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn));
8099 else
8100 /* In this case, though, adding the extra_fn_arg can confuse
8101 things, so we remove from decl_arg_types instead. */
8102 decl_arg_types = TREE_CHAIN (decl_arg_types);
8103 }
8104
e1467ff2 8105 i = fn_type_unification (fn, explicit_args, targs,
d7684f2d 8106 decl_arg_types,
98c1c668 8107 TREE_TYPE (TREE_TYPE (decl)),
830bfa74 8108 DEDUCE_EXACT,
d7684f2d 8109 extra_fn_arg);
98c1c668 8110
76b9a14d
JM
8111 if (i != 0)
8112 return NULL_TREE;
8113
8114 if (check_rettype)
e1467ff2
MM
8115 {
8116 /* Check to see that the resulting return type is also OK. */
36a117a5 8117 tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs,
e6f1275f 8118 NULL_TREE);
e1467ff2 8119
3bfdc719 8120 if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl))))
e1467ff2 8121 return NULL_TREE;
e1467ff2
MM
8122 }
8123
76b9a14d
JM
8124 return targs;
8125}
8126
8127/* For most uses, we want to check the return type. */
8128
8129tree
8130get_bindings (fn, decl, explicit_args)
8131 tree fn, decl, explicit_args;
8132{
8133 return get_bindings_real (fn, decl, explicit_args, 1);
8134}
8135
8136/* But for more_specialized, we only care about the parameter types. */
8137
8138static tree
8139get_bindings_overload (fn, decl, explicit_args)
8140 tree fn, decl, explicit_args;
8141{
8142 return get_bindings_real (fn, decl, explicit_args, 0);
73aad9b9
JM
8143}
8144
36a117a5
MM
8145/* Return the innermost template arguments that, when applied to a
8146 template specialization whose innermost template parameters are
8147 TPARMS, and whose specialization arguments are ARGS, yield the
8148 ARGS.
8149
8150 For example, suppose we have:
8151
8152 template <class T, class U> struct S {};
8153 template <class T> struct S<T*, int> {};
8154
8155 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8156 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8157 int}. The resulting vector will be {double}, indicating that `T'
8158 is bound to `double'. */
8159
bd6dd845 8160static tree
36a117a5
MM
8161get_class_bindings (tparms, parms, args)
8162 tree tparms, parms, args;
73aad9b9 8163{
3b3ba9f0 8164 int i, ntparms = TREE_VEC_LENGTH (tparms);
73aad9b9
JM
8165 tree vec = make_temp_vec (ntparms);
8166
36a117a5 8167 args = innermost_args (args);
8d019cef 8168
fcfb9f96
MM
8169 if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE, 0))
8170 return NULL_TREE;
73aad9b9
JM
8171
8172 for (i = 0; i < ntparms; ++i)
8173 if (! TREE_VEC_ELT (vec, i))
8174 return NULL_TREE;
8175
8176 return vec;
8177}
8178
104bf76a
MM
8179/* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8180 Pick the most specialized template, and return the corresponding
8181 instantiation, or if there is no corresponding instantiation, the
8182 template itself. EXPLICIT_ARGS is any template arguments explicity
8183 mentioned in a template-id. If there is no most specialized
8184 tempalte, error_mark_node is returned. If there are no templates
8185 at all, NULL_TREE is returned. */
73aad9b9
JM
8186
8187tree
104bf76a
MM
8188most_specialized_instantiation (instantiations, explicit_args)
8189 tree instantiations;
8190 tree explicit_args;
73aad9b9 8191{
104bf76a 8192 tree fn, champ;
73aad9b9
JM
8193 int fate;
8194
104bf76a 8195 if (!instantiations)
73aad9b9
JM
8196 return NULL_TREE;
8197
104bf76a
MM
8198 champ = instantiations;
8199 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
73aad9b9 8200 {
104bf76a
MM
8201 fate = more_specialized (TREE_VALUE (champ),
8202 TREE_VALUE (fn), explicit_args);
73aad9b9
JM
8203 if (fate == 1)
8204 ;
8205 else
8206 {
8207 if (fate == 0)
8208 {
8209 fn = TREE_CHAIN (fn);
8210 if (! fn)
8211 return error_mark_node;
8212 }
104bf76a 8213 champ = fn;
73aad9b9
JM
8214 }
8215 }
8216
104bf76a 8217 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
73aad9b9 8218 {
104bf76a
MM
8219 fate = more_specialized (TREE_VALUE (champ),
8220 TREE_VALUE (fn), explicit_args);
73aad9b9
JM
8221 if (fate != 1)
8222 return error_mark_node;
8223 }
8224
104bf76a
MM
8225 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
8226}
8227
8228/* Return the most specialized of the list of templates in FNS that can
8229 produce an instantiation matching DECL, given the explicit template
8230 arguments EXPLICIT_ARGS. */
8231
8232tree
8233most_specialized (fns, decl, explicit_args)
8234 tree fns, decl, explicit_args;
8235{
8236 tree candidates = NULL_TREE;
8237 tree fn, args;
8238
8239 for (fn = fns; fn; fn = TREE_CHAIN (fn))
8240 {
8241 tree candidate = TREE_VALUE (fn);
8242
8243 args = get_bindings (candidate, decl, explicit_args);
8244 if (args)
8245 candidates = scratch_tree_cons (NULL_TREE, candidate,
8246 candidates);
8247 }
8248
8249 return most_specialized_instantiation (candidates, explicit_args);
73aad9b9
JM
8250}
8251
36a117a5
MM
8252/* If DECL is a specialization of some template, return the most
8253 general such template. For example, given:
8254
8255 template <class T> struct S { template <class U> void f(U); };
8256
8257 if TMPL is `template <class U> void S<int>::f(U)' this will return
8258 the full template. This function will not trace past partial
8259 specializations, however. For example, given in addition:
8260
8261 template <class T> struct S<T*> { template <class U> void f(U); };
8262
8263 if TMPL is `template <class U> void S<int*>::f(U)' this will return
8264 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9
JM
8265
8266tree
36a117a5
MM
8267most_general_template (decl)
8268 tree decl;
73aad9b9 8269{
36a117a5
MM
8270 while (DECL_TEMPLATE_INFO (decl))
8271 decl = DECL_TI_TEMPLATE (decl);
8272
8273 return decl;
8274}
8275
8276/* Return the most specialized of the class template specializations
8277 of TMPL which can produce an instantiation matching ARGS, or
8278 error_mark_node if the choice is ambiguous. */
8279
8280tree
8281most_specialized_class (tmpl, args)
8282 tree tmpl;
8283 tree args;
8284{
8285 tree list = NULL_TREE;
8286 tree t;
8287 tree champ;
73aad9b9
JM
8288 int fate;
8289
36a117a5
MM
8290 tmpl = most_general_template (tmpl);
8291 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 8292 {
36a117a5
MM
8293 tree spec_args
8294 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
8295 if (spec_args)
73aad9b9
JM
8296 {
8297 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
8298 TREE_TYPE (list) = TREE_TYPE (t);
8299 }
8300 }
8301
8302 if (! list)
8303 return NULL_TREE;
8304
8305 t = list;
8306 champ = t;
8307 t = TREE_CHAIN (t);
8308 for (; t; t = TREE_CHAIN (t))
8309 {
8310 fate = more_specialized_class (champ, t);
8311 if (fate == 1)
8312 ;
8313 else
8314 {
8315 if (fate == 0)
8316 {
8317 t = TREE_CHAIN (t);
8318 if (! t)
8319 return error_mark_node;
8320 }
8321 champ = t;
8322 }
8323 }
8324
8325 for (t = list; t && t != champ; t = TREE_CHAIN (t))
8326 {
85b71cf2 8327 fate = more_specialized_class (champ, t);
73aad9b9
JM
8328 if (fate != 1)
8329 return error_mark_node;
8330 }
8331
8332 return champ;
8333}
8334
8d08fdba 8335/* called from the parser. */
e92cc029 8336
8d08fdba 8337void
6633d636 8338do_decl_instantiation (declspecs, declarator, storage)
f0e01782 8339 tree declspecs, declarator, storage;
8d08fdba 8340{
c11b6f21 8341 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
8d08fdba 8342 tree result = NULL_TREE;
faae18ab 8343 int extern_p = 0;
e8abc66f 8344
ec255269
MS
8345 if (! DECL_LANG_SPECIFIC (decl))
8346 {
8251199e 8347 cp_error ("explicit instantiation of non-template `%#D'", decl);
ec255269
MS
8348 return;
8349 }
03d0f4af 8350 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 8351 {
03d0f4af
MM
8352 /* There is an asymmetry here in the way VAR_DECLs and
8353 FUNCTION_DECLs are handled by grokdeclarator. In the case of
8354 the latter, the DECL we get back will be marked as a
8355 template instantiation, and the appropriate
8356 DECL_TEMPLATE_INFO will be set up. This does not happen for
8357 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
8358 should handle VAR_DECLs as it currently handles
8359 FUNCTION_DECLs. */
6633d636
MS
8360 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
8361 if (result && TREE_CODE (result) != VAR_DECL)
03d0f4af 8362 {
8251199e 8363 cp_error ("no matching template for `%D' found", result);
03d0f4af
MM
8364 return;
8365 }
6633d636
MS
8366 }
8367 else if (TREE_CODE (decl) != FUNCTION_DECL)
8368 {
8251199e 8369 cp_error ("explicit instantiation of `%#D'", decl);
6633d636
MS
8370 return;
8371 }
03d0f4af
MM
8372 else
8373 result = decl;
672476cb 8374
03d0f4af
MM
8375 /* Check for various error cases. Note that if the explicit
8376 instantiation is legal the RESULT will currently be marked as an
8377 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
8378 until we get here. */
8379
8380 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 8381 {
03d0f4af
MM
8382 /* [temp.spec]
8383
8384 No program shall both explicitly instantiate and explicitly
8385 specialize a template. */
8251199e
JM
8386 cp_error ("explicit instantiation of `%#D' after", result);
8387 cp_error_at ("explicit specialization here", result);
672476cb
MM
8388 return;
8389 }
03d0f4af
MM
8390 else if (DECL_EXPLICIT_INSTANTIATION (result))
8391 {
8392 /* [temp.spec]
98c1c668 8393
03d0f4af
MM
8394 No program shall explicitly instantiate any template more
8395 than once.
8396
8397 We check DECL_INTERFACE_KNOWN so as not to complain when the
8398 first instantiation was `extern' and the second is not, and
8399 EXTERN_P for the opposite case. */
8400 if (DECL_INTERFACE_KNOWN (result) && !extern_p)
8251199e 8401 cp_error ("duplicate explicit instantiation of `%#D'", result);
03d0f4af
MM
8402
8403 /* If we've already instantiated the template, just return now. */
8404 if (DECL_INTERFACE_KNOWN (result))
8405 return;
8406 }
8407 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 8408 {
8251199e 8409 cp_error ("no matching template for `%D' found", result);
faae18ab
MS
8410 return;
8411 }
03d0f4af 8412 else if (!DECL_TEMPLATE_INFO (result))
6633d636 8413 {
8251199e 8414 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
6633d636
MS
8415 return;
8416 }
8417
a0a33927
MS
8418 if (flag_external_templates)
8419 return;
8420
f0e01782 8421 if (storage == NULL_TREE)
00595019 8422 ;
faae18ab 8423 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af
MM
8424 {
8425 if (pedantic)
8251199e 8426 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
03d0f4af
MM
8427 extern_p = 1;
8428 }
f0e01782 8429 else
8251199e 8430 cp_error ("storage class `%D' applied to template instantiation",
f0e01782 8431 storage);
5566b478 8432
03d0f4af 8433 SET_DECL_EXPLICIT_INSTANTIATION (result);
5566b478 8434 mark_decl_instantiated (result, extern_p);
44a8d0b3 8435 repo_template_instantiated (result, extern_p);
c91a56d2
MS
8436 if (! extern_p)
8437 instantiate_decl (result);
7177d104
MS
8438}
8439
faae18ab
MS
8440void
8441mark_class_instantiated (t, extern_p)
8442 tree t;
8443 int extern_p;
8444{
8445 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
8446 SET_CLASSTYPE_INTERFACE_KNOWN (t);
8447 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
8448 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
8449 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
8450 if (! extern_p)
8451 {
8452 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
8453 rest_of_type_compilation (t, 1);
8454 }
8455}
e8abc66f 8456
7177d104 8457void
ca79f85d
JM
8458do_type_instantiation (t, storage)
8459 tree t, storage;
7177d104 8460{
e8abc66f
MS
8461 int extern_p = 0;
8462 int nomem_p = 0;
5566b478
MS
8463 int static_p = 0;
8464
ca79f85d
JM
8465 if (TREE_CODE (t) == TYPE_DECL)
8466 t = TREE_TYPE (t);
8467
7ddedda4 8468 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 8469 {
8251199e 8470 cp_error ("explicit instantiation of non-template type `%T'", t);
ca79f85d
JM
8471 return;
8472 }
8473
5566b478 8474 complete_type (t);
7177d104 8475
a292b002
MS
8476 /* With -fexternal-templates, explicit instantiations are treated the same
8477 as implicit ones. */
a0a33927
MS
8478 if (flag_external_templates)
8479 return;
8480
f0e01782
MS
8481 if (TYPE_SIZE (t) == NULL_TREE)
8482 {
8251199e 8483 cp_error ("explicit instantiation of `%#T' before definition of template",
f0e01782
MS
8484 t);
8485 return;
8486 }
8487
03d0f4af 8488 if (storage != NULL_TREE)
f0e01782 8489 {
03d0f4af 8490 if (pedantic)
8251199e
JM
8491 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
8492 IDENTIFIER_POINTER (storage));
03d0f4af
MM
8493
8494 if (storage == ridpointers[(int) RID_INLINE])
8495 nomem_p = 1;
8496 else if (storage == ridpointers[(int) RID_EXTERN])
8497 extern_p = 1;
8498 else if (storage == ridpointers[(int) RID_STATIC])
8499 static_p = 1;
8500 else
8501 {
8251199e 8502 cp_error ("storage class `%D' applied to template instantiation",
03d0f4af
MM
8503 storage);
8504 extern_p = 0;
8505 }
f0e01782
MS
8506 }
8507
03d0f4af
MM
8508 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
8509 {
8510 /* [temp.spec]
a292b002 8511
03d0f4af
MM
8512 No program shall both explicitly instantiate and explicitly
8513 specialize a template. */
8251199e
JM
8514 cp_error ("explicit instantiation of `%#T' after", t);
8515 cp_error_at ("explicit specialization here", t);
03d0f4af
MM
8516 return;
8517 }
8518 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 8519 {
03d0f4af
MM
8520 /* [temp.spec]
8521
8522 No program shall explicitly instantiate any template more
8523 than once.
8524
8525 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
8526 instantiation was `extern', and if EXTERN_P then the second
8527 is. Both cases are OK. */
8528 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p)
8251199e 8529 cp_error ("duplicate explicit instantiation of `%#T'", t);
03d0f4af
MM
8530
8531 /* If we've already instantiated the template, just return now. */
8532 if (!CLASSTYPE_INTERFACE_ONLY (t))
8533 return;
44a8d0b3 8534 }
e8abc66f 8535
03d0f4af
MM
8536 mark_class_instantiated (t, extern_p);
8537 repo_template_instantiated (t, extern_p);
8538
e8abc66f
MS
8539 if (nomem_p)
8540 return;
8541
7177d104 8542 {
db5ae43f 8543 tree tmp;
5566b478 8544
03d0f4af
MM
8545 /* In contrast to implicit instantiation, where only the
8546 declarations, and not the definitions, of members are
8547 instantiated, we have here:
8548
8549 [temp.explicit]
8550
8551 The explicit instantiation of a class template specialization
8552 implies the instantiation of all of its members not
8553 previously explicitly specialized in the translation unit
8554 containing the explicit instantiation.
8555
8556 Of course, we can't instantiate member template classes, since
8557 we don't have any arguments for them. Note that the standard
8558 is unclear on whether the instatiation of the members are
8559 *explicit* instantiations or not. We choose to be generous,
8560 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
8561 the explicit instantiation of a class where some of the members
8562 have no definition in the current translation unit. */
8563
5566b478
MS
8564 if (! static_p)
8565 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 8566 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 8567 && DECL_TEMPLATE_INSTANTIATION (tmp))
5566b478
MS
8568 {
8569 mark_decl_instantiated (tmp, extern_p);
8570 repo_template_instantiated (tmp, extern_p);
8571 if (! extern_p)
8572 instantiate_decl (tmp);
8573 }
8574
8575 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
8576 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
863adfc0 8577 {
5566b478 8578 mark_decl_instantiated (tmp, extern_p);
863adfc0 8579 repo_template_instantiated (tmp, extern_p);
5566b478
MS
8580 if (! extern_p)
8581 instantiate_decl (tmp);
863adfc0 8582 }
7177d104 8583
a292b002 8584 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
e4a84209
MM
8585 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
8586 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
f376e137 8587 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
a292b002 8588 }
8d08fdba 8589}
a28e3c7f 8590
36a117a5
MM
8591/* Given a function DECL, which is a specialization of TMPL, modify
8592 DECL to be a re-instantiation of TMPL with the same template
8593 arguments. TMPL should be the template into which tsubst'ing
8594 should occur for DECL, not the most general template.
830bfa74
MM
8595
8596 One reason for doing this is a scenario like this:
8597
8598 template <class T>
8599 void f(const T&, int i);
8600
8601 void g() { f(3, 7); }
8602
8603 template <class T>
8604 void f(const T& t, const int i) { }
8605
8606 Note that when the template is first instantiated, with
8607 instantiate_template, the resulting DECL will have no name for the
8608 first parameter, and the wrong type for the second. So, when we go
8609 to instantiate the DECL, we regenerate it. */
8610
8611void
8612regenerate_decl_from_template (decl, tmpl)
8613 tree decl;
8614 tree tmpl;
8615{
8616 tree args;
830bfa74
MM
8617 tree code_pattern;
8618 tree new_decl;
36a117a5
MM
8619 tree gen_tmpl;
8620 int unregistered;
830bfa74
MM
8621
8622 args = DECL_TI_ARGS (decl);
8623 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
8624
8251199e
JM
8625 /* Unregister the specialization so that when we tsubst we will not
8626 just return DECL. We don't have to unregister DECL from TMPL
8627 because if would only be registered there if it were a partial
8628 instantiation of a specialization, which it isn't: it's a full
8629 instantiation. */
36a117a5
MM
8630 gen_tmpl = most_general_template (tmpl);
8631 unregistered = unregister_specialization (decl, gen_tmpl);
8632
8633 /* If the DECL was not unregistered then something peculiar is
8634 happening: we created a specialization but did not call
8635 register_specialization for it. */
8636 my_friendly_assert (unregistered, 0);
8637
61a127b3
MM
8638 if (TREE_CODE (decl) == VAR_DECL)
8639 /* Make sure that we can see identifiers, and compute access
8640 correctly, for the class members used in the declaration of
8641 this static variable. */
8642 pushclass (DECL_CONTEXT (decl), 2);
8643
d8c4447d
MM
8644 /* Do the substitution to get the new declaration. */
8645 new_decl = tsubst (code_pattern, args, NULL_TREE);
830bfa74
MM
8646
8647 if (TREE_CODE (decl) == VAR_DECL)
8648 {
8649 /* Set up DECL_INITIAL, since tsubst doesn't. */
830bfa74 8650 DECL_INITIAL (new_decl) =
d8c4447d 8651 tsubst_expr (DECL_INITIAL (code_pattern), args,
830bfa74 8652 DECL_TI_TEMPLATE (decl));
61a127b3 8653 /* Pop the class context we pushed above. */
830bfa74
MM
8654 popclass (1);
8655 }
8656
8657 if (TREE_CODE (decl) == FUNCTION_DECL)
fbf1c34b
MM
8658 {
8659 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
8660 new decl. */
8661 DECL_INITIAL (new_decl) = error_mark_node;
8662 /* And don't complain about a duplicate definition. */
8663 DECL_INITIAL (decl) = NULL_TREE;
8664 }
36a117a5
MM
8665
8666 /* The immediate parent of the new template is still whatever it was
8667 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
8668 general template. We also reset the DECL_ASSEMBLER_NAME since
8669 tsubst always calculates the name as if the function in question
8670 were really a template instance, and sometimes, with friend
8671 functions, this is not so. See tsubst_friend_function for
8672 details. */
8673 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
8674 DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl);
8675 DECL_RTL (new_decl) = DECL_RTL (decl);
8676
8677 /* Call duplicate decls to merge the old and new declarations. */
830bfa74
MM
8678 duplicate_decls (new_decl, decl);
8679
36a117a5
MM
8680 /* Now, re-register the specialization. */
8681 register_specialization (decl, gen_tmpl, args);
830bfa74
MM
8682}
8683
f84b4be9
JM
8684/* Produce the definition of D, a _DECL generated from a template. */
8685
a28e3c7f 8686tree
5566b478
MS
8687instantiate_decl (d)
8688 tree d;
a28e3c7f 8689{
36a117a5
MM
8690 tree tmpl = DECL_TI_TEMPLATE (d);
8691 tree args = DECL_TI_ARGS (d);
830bfa74 8692 tree td;
36a117a5
MM
8693 tree code_pattern;
8694 tree spec;
8695 tree gen_tmpl;
5566b478 8696 int nested = in_function_p ();
5566b478 8697 int pattern_defined;
c27be9b9
JM
8698 int line = lineno;
8699 char *file = input_filename;
5566b478 8700
36a117a5
MM
8701 /* This function should only be used to instantiate templates for
8702 functions and static member variables. */
8703 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
8704 || TREE_CODE (d) == VAR_DECL, 0);
8705
fbf1c34b 8706 if (DECL_TEMPLATE_INSTANTIATED (d))
03d0f4af
MM
8707 /* D has already been instantiated. It might seem reasonable to
8708 check whether or not D is an explict instantiation, and, if so,
8709 stop here. But when an explicit instantiation is deferred
8710 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
8711 is set, even though we still need to do the instantiation. */
36a117a5
MM
8712 return d;
8713
8714 /* If we already have a specialization of this declaration, then
8715 there's no reason to instantiate it. Note that
8716 retrieve_specialization gives us both instantiations and
8717 specializations, so we must explicitly check
8718 DECL_TEMPLATE_SPECIALIZATION. */
8719 gen_tmpl = most_general_template (tmpl);
8720 spec = retrieve_specialization (gen_tmpl, args);
8721 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
8722 return spec;
8723
8724 /* This needs to happen before any tsubsting. */
8725 if (! push_tinst_level (d))
8726 return d;
8727
4d85e00e
MM
8728 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
8729 for the instantiation. This is not always the most general
8730 template. Consider, for example:
8731
8732 template <class T>
8733 struct S { template <class U> void f();
8734 template <> void f<int>(); };
8735
8736 and an instantiation of S<double>::f<int>. We want TD to be the
8737 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
8738 td = tmpl;
8739 for (td = tmpl;
8740 /* An instantiation cannot have a definition, so we need a
8741 more general template. */
8742 DECL_TEMPLATE_INSTANTIATION (td)
8743 /* We must also deal with friend templates. Given:
8744
8745 template <class T> struct S {
8746 template <class U> friend void f() {};
8747 };
8748
8749 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
8750 so far as the language is concerned, but that's still
8751 where we get the pattern for the instantiation from. On
8752 ther hand, if the definition comes outside the class, say:
8753
8754 template <class T> struct S {
8755 template <class U> friend void f();
8756 };
8757 template <class U> friend void f() {}
8758
8759 we don't need to look any further. That's what the check for
8760 DECL_INITIAL is for. */
8761 || (TREE_CODE (d) == FUNCTION_DECL
61289ca3 8762 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
4d85e00e 8763 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)));
f84b4be9 8764 )
4d85e00e
MM
8765 {
8766 /* The present template, TD, should not be a definition. If it
8767 were a definition, we should be using it! Note that we
8768 cannot restructure the loop to just keep going until we find
8769 a template with a definition, since that might go too far if
8770 a specialization was declared, but not defined. */
4d85e00e
MM
8771 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
8772 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
8773 0);
8774
8775 /* Fetch the more general template. */
8776 td = DECL_TI_TEMPLATE (td);
8777 }
27bb8339 8778
fee23f54 8779 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 8780
5566b478 8781 if (TREE_CODE (d) == FUNCTION_DECL)
36a117a5 8782 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
5566b478 8783 else
36a117a5 8784 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
de22184b
MS
8785
8786 push_to_top_level ();
8787 lineno = DECL_SOURCE_LINE (d);
8788 input_filename = DECL_SOURCE_FILE (d);
8789
de22184b 8790 if (pattern_defined)
5566b478
MS
8791 {
8792 repo_template_used (d);
8793
8794 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
8795 {
8796 if (flag_alt_external_templates)
8797 {
8798 if (interface_unknown)
8799 warn_if_unknown_interface (d);
8800 }
fee23f54 8801 else if (DECL_INTERFACE_KNOWN (code_pattern))
5566b478
MS
8802 {
8803 DECL_INTERFACE_KNOWN (d) = 1;
fee23f54 8804 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
5566b478
MS
8805 }
8806 else
fee23f54 8807 warn_if_unknown_interface (code_pattern);
5566b478
MS
8808 }
8809
e92cc029 8810 if (at_eof)
5566b478
MS
8811 import_export_decl (d);
8812 }
8813
c4ae3f91
JM
8814 /* Reject all external templates except inline functions. */
8815 if (DECL_INTERFACE_KNOWN (d)
8816 && ! DECL_NOT_REALLY_EXTERN (d)
8817 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
8818 goto out;
8819
67ffc812 8820 if (TREE_CODE (d) == VAR_DECL
711734a9 8821 && TREE_READONLY (d)
67ffc812
MM
8822 && DECL_INITIAL (d) == NULL_TREE
8823 && DECL_INITIAL (code_pattern) != NULL_TREE)
8824 /* We need to set up DECL_INITIAL regardless of pattern_defined if
8825 the variable is a static const initialized in the class body. */;
8826 else if (! pattern_defined
8827 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
8828 && ! at_eof))
5566b478 8829 {
67ffc812 8830 /* Defer all templates except inline functions used in another
c27be9b9
JM
8831 function. We restore the source position here because it's used
8832 by add_pending_template. */
8833 lineno = line;
8834 input_filename = file;
8835
03d0f4af
MM
8836 if (at_eof && !pattern_defined
8837 && DECL_EXPLICIT_INSTANTIATION (d))
8838 /* [temp.explicit]
8839
8840 The definition of a non-exported function template, a
8841 non-exported member function template, or a non-exported
8842 member function or static data member of a class template
8843 shall be present in every translation unit in which it is
8844 explicitly instantiated. */
8251199e 8845 cp_error ("explicit instantiation of `%D' but no definition available",
03d0f4af
MM
8846 d);
8847
5566b478 8848 add_pending_template (d);
de22184b 8849 goto out;
5566b478
MS
8850 }
8851
120722ac 8852 regenerate_decl_from_template (d, td);
fbf1c34b 8853 DECL_TEMPLATE_INSTANTIATED (d) = 1;
120722ac
MM
8854
8855 /* We already set the file and line above. Reset them now in case
8856 they changed as a result of calling regenerate_decl_from_template. */
5156628f
MS
8857 lineno = DECL_SOURCE_LINE (d);
8858 input_filename = DECL_SOURCE_FILE (d);
8859
5566b478
MS
8860 if (TREE_CODE (d) == VAR_DECL)
8861 {
8862 DECL_IN_AGGR_P (d) = 0;
8863 if (DECL_INTERFACE_KNOWN (d))
8864 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
8865 else
8866 {
8867 DECL_EXTERNAL (d) = 1;
8868 DECL_NOT_REALLY_EXTERN (d) = 1;
8869 }
8870 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
8871 }
8872 else if (TREE_CODE (d) == FUNCTION_DECL)
8873 {
fee23f54 8874 tree t = DECL_SAVED_TREE (code_pattern);
5566b478 8875
c11b6f21 8876 start_function (NULL_TREE, d, NULL_TREE, 1);
5566b478
MS
8877 store_parm_decls ();
8878
e76a2646
MS
8879 if (t && TREE_CODE (t) == RETURN_INIT)
8880 {
8881 store_return_init
8882 (TREE_OPERAND (t, 0),
f7d98d58 8883 tsubst_expr (TREE_OPERAND (t, 1), args, tmpl));
e76a2646
MS
8884 t = TREE_CHAIN (t);
8885 }
8886
5566b478
MS
8887 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
8888 {
8889 current_member_init_list
8890 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
8891 current_base_init_list
8892 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
8893 t = TREE_CHAIN (t);
8894 }
8895
8896 setup_vtbl_ptr ();
8897 /* Always keep the BLOCK node associated with the outermost
956d6950 8898 pair of curly braces of a function. These are needed
5566b478
MS
8899 for correct operation of dwarfout.c. */
8900 keep_next_level ();
8901
8902 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
f7d98d58 8903 tsubst_expr (t, args, tmpl);
a28e3c7f 8904
5566b478 8905 finish_function (lineno, 0, nested);
5566b478
MS
8906 }
8907
de22184b 8908out:
c27be9b9
JM
8909 lineno = line;
8910 input_filename = file;
8911
5566b478 8912 pop_from_top_level ();
5566b478 8913 pop_tinst_level ();
a28e3c7f 8914
a28e3c7f
MS
8915 return d;
8916}
5566b478 8917
824b9a4c 8918static tree
5566b478
MS
8919tsubst_expr_values (t, argvec)
8920 tree t, argvec;
8921{
8922 tree first = NULL_TREE;
8923 tree *p = &first;
8924
8925 for (; t; t = TREE_CHAIN (t))
8926 {
f7d98d58
JM
8927 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE);
8928 tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE);
5566b478
MS
8929 *p = build_tree_list (pur, val);
8930 p = &TREE_CHAIN (*p);
8931 }
8932 return first;
8933}
8934
8935tree last_tree;
8936
8937void
8938add_tree (t)
8939 tree t;
8940{
8941 last_tree = TREE_CHAIN (last_tree) = t;
8942}
73aad9b9 8943
75650646
MM
8944
8945void
8946begin_tree ()
8947{
8948 saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
8949 last_tree = NULL_TREE;
8950}
8951
8952
8953void
8954end_tree ()
8955{
8956 my_friendly_assert (saved_trees != NULL_TREE, 0);
8957
8958 last_tree = TREE_VALUE (saved_trees);
8959 saved_trees = TREE_CHAIN (saved_trees);
8960}
8961
73aad9b9
JM
8962/* D is an undefined function declaration in the presence of templates with
8963 the same name, listed in FNS. If one of them can produce D as an
8964 instantiation, remember this so we can instantiate it at EOF if D has
8965 not been defined by that time. */
8966
8967void
8968add_maybe_template (d, fns)
8969 tree d, fns;
8970{
8971 tree t;
8972
8973 if (DECL_MAYBE_TEMPLATE (d))
8974 return;
8975
e1467ff2 8976 t = most_specialized (fns, d, NULL_TREE);
73aad9b9
JM
8977 if (! t)
8978 return;
8979 if (t == error_mark_node)
8980 {
8251199e 8981 cp_error ("ambiguous template instantiation for `%D'", d);
73aad9b9
JM
8982 return;
8983 }
8984
8985 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
8986 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
8987 DECL_MAYBE_TEMPLATE (d) = 1;
8988}
b87692e5 8989
61a127b3
MM
8990/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
8991
8992static void
8993set_current_access_from_decl (decl)
8994 tree decl;
8995{
8996 if (TREE_PRIVATE (decl))
8997 current_access_specifier = access_private_node;
8998 else if (TREE_PROTECTED (decl))
8999 current_access_specifier = access_protected_node;
9000 else
9001 current_access_specifier = access_public_node;
9002}
9003
dbfe2124
MM
9004/* Instantiate an enumerated type. TAG is the template type, NEWTAG
9005 is the instantiation (which should have been created with
9006 start_enum) and ARGS are the template arguments to use. */
b87692e5 9007
dbfe2124
MM
9008static void
9009tsubst_enum (tag, newtag, args)
9010 tree tag;
9011 tree newtag;
9012 tree args;
b87692e5 9013{
dbfe2124 9014 tree e;
b87692e5
MS
9015
9016 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
9017 {
61a127b3
MM
9018 tree value;
9019 tree elt;
9020
9021 /* Note that in a template enum, the TREE_VALUE is the
9022 CONST_DECL, not the corresponding INTEGER_CST. */
9023 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
72f2bd78 9024 args,
61a127b3
MM
9025 NULL_TREE);
9026
9027 /* Give this enumeration constant the correct access. */
9028 set_current_access_from_decl (TREE_VALUE (e));
9029
9030 /* Actually build the enumerator itself. */
9031 elt = build_enumerator (TREE_PURPOSE (e), value, newtag);
b87692e5 9032
dbfe2124
MM
9033 /* We save the enumerators we have built so far in the
9034 TYPE_VALUES so that if the enumeration constants for
9035 subsequent enumerators involve those for previous ones,
9036 tsubst_copy will be able to find them. */
9037 TREE_CHAIN (elt) = TYPE_VALUES (newtag);
9038 TYPE_VALUES (newtag) = elt;
9039 }
b3d5a58b 9040
dbfe2124 9041 finish_enum (newtag);
b87692e5 9042}
36a117a5
MM
9043
9044/* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9045 is either an instantiation or specialization of a template
9046 function. */
9047
9048static void
9049set_mangled_name_for_template_decl (decl)
9050 tree decl;
9051{
9052 tree saved_namespace;
b370501f 9053 tree context = NULL_TREE;
36a117a5
MM
9054 tree fn_type;
9055 tree ret_type;
9056 tree parm_types;
9057 tree tparms;
9058 tree targs;
9059 tree tmpl;
9060 int parm_depth;
9061
9062 my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0);
9063 my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0);
9064
9065 /* The names of template functions must be mangled so as to indicate
9066 what template is being specialized with what template arguments.
9067 For example, each of the following three functions must get
9068 different mangled names:
9069
9070 void f(int);
9071 template <> void f<7>(int);
9072 template <> void f<8>(int); */
9073
9074 targs = DECL_TI_ARGS (decl);
9075 if (uses_template_parms (targs))
9076 /* This DECL is for a partial instantiation. There's no need to
9077 mangle the name of such an entity. */
9078 return;
9079
9080 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9081 tparms = DECL_TEMPLATE_PARMS (tmpl);
9082 parm_depth = TMPL_PARMS_DEPTH (tparms);
9083
9084 /* There should be as many levels of arguments as there are levels
9085 of parameters. */
9086 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
9087
9088 /* We now compute the PARMS and RET_TYPE to give to
9089 build_decl_overload_real. The PARMS and RET_TYPE are the
9090 parameter and return types of the template, after all but the
9091 innermost template arguments have been substituted, not the
9092 parameter and return types of the function DECL. For example,
9093 given:
9094
9095 template <class T> T f(T);
9096
9097 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
9098 A more subtle example is:
9099
9100 template <class T> struct S { template <class U> void f(T, U); }
9101
9102 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
9103 {int, U}. Thus, the args that we want to subsitute into the
9104 return and parameter type for the function are those in TARGS,
9105 with the innermost level omitted. */
9106 fn_type = TREE_TYPE (tmpl);
9107 if (DECL_STATIC_FUNCTION_P (decl))
61a127b3 9108 context = DECL_CLASS_CONTEXT (decl);
36a117a5
MM
9109
9110 if (parm_depth == 1)
9111 /* No substitution is necessary. */
9112 ;
9113 else
9114 {
9115 int i;
9116 tree partial_args;
9117
9118 /* Replace the innermost level of the TARGS with NULL_TREEs to
9119 let tsubst know not to subsitute for those parameters. */
9120 partial_args = make_temp_vec (TREE_VEC_LENGTH (targs));
9121 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
9122 SET_TMPL_ARGS_LEVEL (partial_args, i,
9123 TMPL_ARGS_LEVEL (targs, i));
9124 SET_TMPL_ARGS_LEVEL (partial_args,
9125 TMPL_ARGS_DEPTH (targs),
9126 make_temp_vec (DECL_NTPARMS (tmpl)));
9127
9128 /* Now, do the (partial) substitution to figure out the
9129 appropriate function type. */
9130 fn_type = tsubst (fn_type, partial_args, NULL_TREE);
9131 if (DECL_STATIC_FUNCTION_P (decl))
9132 context = tsubst (context, partial_args, NULL_TREE);
9133
9134 /* Substitute into the template parameters to obtain the real
9135 innermost set of parameters. This step is important if the
9136 innermost set of template parameters contains value
9137 parameters whose types depend on outer template parameters. */
9138 TREE_VEC_LENGTH (partial_args)--;
9139 tparms = tsubst_template_parms (tparms, partial_args);
9140 }
9141
9142 /* Now, get the innermost parameters and arguments, and figure out
9143 the parameter and return types. */
9144 tparms = INNERMOST_TEMPLATE_PARMS (tparms);
9145 targs = innermost_args (targs);
9146 ret_type = TREE_TYPE (fn_type);
9147 parm_types = TYPE_ARG_TYPES (fn_type);
9148
9149 /* For a static member function, we generate a fake `this' pointer,
9150 for the purposes of mangling. This indicates of which class the
9151 function is a member. Because of:
9152
9153 [class.static]
9154
9155 There shall not be a static and a nonstatic member function
9156 with the same name and the same parameter types
9157
9158 we don't have to worry that this will result in a clash with a
9159 non-static member function. */
9160 if (DECL_STATIC_FUNCTION_P (decl))
9161 parm_types = hash_tree_chain (build_pointer_type (context), parm_types);
9162
9163 /* There should be the same number of template parameters as
9164 template arguments. */
9165 my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs),
9166 0);
9167
9168 /* If the template is in a namespace, we need to put that into the
9169 mangled name. Unfortunately, build_decl_overload_real does not
9170 get the decl to mangle, so it relies on the current
9171 namespace. Therefore, we set that here temporarily. */
9172 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702);
9173 saved_namespace = current_namespace;
9174 current_namespace = CP_DECL_CONTEXT (decl);
9175
9176 /* Actually set the DCL_ASSEMBLER_NAME. */
9177 DECL_ASSEMBLER_NAME (decl)
9178 = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type,
9179 tparms, targs,
9180 DECL_FUNCTION_MEMBER_P (decl)
9181 + DECL_CONSTRUCTOR_P (decl));
9182
9183 /* Restore the previously active namespace. */
9184 current_namespace = saved_namespace;
9185}
This page took 1.679503 seconds and 5 git commands to generate.