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