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