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