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