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