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