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