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