]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
AbstractPreferences.java (isUserNode): Implemented.
[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;
dbbf88d1
NS
5149 tree pbinfo;
5150
5566b478 5151 if (type == error_mark_node)
8d08fdba
MS
5152 return error_mark_node;
5153
ca099ac8
MM
5154 if (TYPE_BEING_DEFINED (type)
5155 || COMPLETE_TYPE_P (type)
5156 || dependent_type_p (type))
5566b478
MS
5157 return type;
5158
6bdb985f 5159 /* Figure out which template is being instantiated. */
36a117a5 5160 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5566b478 5161 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
73aad9b9 5162
6bdb985f
MM
5163 /* Figure out which arguments are being used to do the
5164 instantiation. */
5165 args = CLASSTYPE_TI_ARGS (type);
4c571114
MM
5166
5167 /* Determine what specialization of the original template to
5168 instantiate. */
8fbc5ae7
MM
5169 t = most_specialized_class (template, args);
5170 if (t == error_mark_node)
73aad9b9 5171 {
8fbc5ae7
MM
5172 const char *str = "candidates are:";
5173 error ("ambiguous class template instantiation for `%#T'", type);
5174 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
5175 t = TREE_CHAIN (t))
73aad9b9 5176 {
8fbc5ae7
MM
5177 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5178 args))
73aad9b9 5179 {
8fbc5ae7
MM
5180 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
5181 str = " ";
73aad9b9
JM
5182 }
5183 }
8fbc5ae7
MM
5184 TYPE_BEING_DEFINED (type) = 1;
5185 return error_mark_node;
73aad9b9 5186 }
6bdb985f
MM
5187
5188 if (t)
73aad9b9
JM
5189 pattern = TREE_TYPE (t);
5190 else
5191 pattern = TREE_TYPE (template);
5566b478 5192
4c571114
MM
5193 /* If the template we're instantiating is incomplete, then clearly
5194 there's nothing we can do. */
d0f062fb 5195 if (!COMPLETE_TYPE_P (pattern))
f31c0a32 5196 return type;
5566b478 5197
4c571114
MM
5198 /* If we've recursively instantiated too many templates, stop. */
5199 if (! push_tinst_level (type))
f31c0a32 5200 return type;
4c571114
MM
5201
5202 /* Now we're really doing the instantiation. Mark the type as in
5203 the process of being defined. */
5204 TYPE_BEING_DEFINED (type) = 1;
5205
5206 maybe_push_to_top_level (uses_template_parms (type));
4c571114 5207
73aad9b9 5208 if (t)
36a117a5 5209 {
27631dae 5210 /* This TYPE is actually an instantiation of a partial
36a117a5
MM
5211 specialization. We replace the innermost set of ARGS with
5212 the arguments appropriate for substitution. For example,
5213 given:
5214
5215 template <class T> struct S {};
5216 template <class T> struct S<T*> {};
5217
5218 and supposing that we are instantiating S<int*>, ARGS will
5219 present be {int*} but we need {int}. */
5220 tree inner_args
5221 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
5222 args);
5223
5224 /* If there were multiple levels in ARGS, replacing the
5225 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
5226 want, so we make a copy first. */
5227 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
5228 {
5229 args = copy_node (args);
5230 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
5231 }
5232 else
5233 args = inner_args;
5234 }
8d019cef 5235
5566b478
MS
5236 if (flag_external_templates)
5237 {
5238 if (flag_alt_external_templates)
5239 {
5240 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
5241 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
5566b478
MS
5242 }
5243 else
5244 {
5245 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
5246 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
5247 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
5566b478
MS
5248 }
5249 }
5250 else
8d08fdba 5251 {
5566b478 5252 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8d08fdba
MS
5253 }
5254
f7da6097
MS
5255 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
5256 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
f7da6097
MS
5257 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
5258 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
5259 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
834c6dff
MM
5260 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5261 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
f7da6097 5262 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
f7da6097
MS
5263 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5264 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5265 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5266 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5267 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
5268 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5269 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
4c6b7393
MM
5270 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (type)
5271 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (pattern);
f7da6097
MS
5272 TYPE_USES_MULTIPLE_INHERITANCE (type)
5273 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5274 TYPE_USES_VIRTUAL_BASECLASSES (type)
5275 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5276 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5277 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
11cf4d18 5278 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
eff71ab0 5279 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6bdb8141
JM
5280 if (ANON_AGGR_TYPE_P (pattern))
5281 SET_ANON_AGGR_TYPE_P (type);
f7da6097 5282
dbbf88d1
NS
5283 pbinfo = TYPE_BINFO (pattern);
5284
5285 if (BINFO_BASETYPES (pbinfo))
3fd71a52
MM
5286 {
5287 tree base_list = NULL_TREE;
dbbf88d1
NS
5288 tree pbases = BINFO_BASETYPES (pbinfo);
5289 tree paccesses = BINFO_BASEACCESSES (pbinfo);
3fd71a52 5290 int i;
5566b478 5291
3fd71a52
MM
5292 /* Substitute into each of the bases to determine the actual
5293 basetypes. */
5294 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5295 {
5296 tree base;
5297 tree access;
5298 tree pbase;
5566b478 5299
3fd71a52 5300 pbase = TREE_VEC_ELT (pbases, i);
dbbf88d1 5301 access = TREE_VEC_ELT (paccesses, i);
711734a9 5302
dc957d14 5303 /* Substitute to figure out the base class. */
c2ea3a40 5304 base = tsubst (BINFO_TYPE (pbase), args, tf_error, NULL_TREE);
3fd71a52
MM
5305 if (base == error_mark_node)
5306 continue;
dbbf88d1 5307
3fd71a52 5308 base_list = tree_cons (access, base, base_list);
dbbf88d1 5309 TREE_VIA_VIRTUAL (base_list) = TREE_VIA_VIRTUAL (pbase);
3fd71a52 5310 }
dfbcd65a 5311
3fd71a52
MM
5312 /* The list is now in reverse order; correct that. */
5313 base_list = nreverse (base_list);
5314
5315 /* Now call xref_basetypes to set up all the base-class
5316 information. */
da15dae6 5317 xref_basetypes (type, base_list);
3fd71a52 5318 }
5566b478 5319
b74a0560
MM
5320 /* Now that our base classes are set up, enter the scope of the
5321 class, so that name lookups into base classes, etc. will work
dc957d14 5322 correctly. This is precisely analogous to what we do in
b74a0560
MM
5323 begin_class_definition when defining an ordinary non-template
5324 class. */
14d22dd6 5325 pushclass (type, true);
b74a0560 5326
7088fca9
KL
5327 /* Now members are processed in the order of declaration. */
5328 for (member = CLASSTYPE_DECL_LIST (pattern); member; member = TREE_CHAIN (member))
8d08fdba 5329 {
7088fca9 5330 tree t = TREE_VALUE (member);
5566b478 5331
7088fca9 5332 if (TREE_PURPOSE (member))
ed44da02 5333 {
7088fca9
KL
5334 if (TYPE_P (t))
5335 {
5336 /* Build new CLASSTYPE_TAGS. */
8d08fdba 5337
7088fca9
KL
5338 tree tag = t;
5339 tree name = TYPE_IDENTIFIER (tag);
5340 tree newtag;
ae58fa02 5341
7088fca9
KL
5342 newtag = tsubst (tag, args, tf_error, NULL_TREE);
5343 my_friendly_assert (newtag != error_mark_node, 20010206);
5344 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5345 {
5346 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5347 /* Unfortunately, lookup_template_class sets
5348 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5349 instantiation (i.e., for the type of a member template
5350 class nested within a template class.) This behavior is
5351 required for maybe_process_partial_specialization to work
5352 correctly, but is not accurate in this case; the TAG is not
5353 an instantiation of anything. (The corresponding
5354 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5355 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5356
5357 /* Now, we call pushtag to put this NEWTAG into the scope of
5358 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5359 pushtag calling push_template_decl. We don't have to do
5360 this for enums because it will already have been done in
5361 tsubst_enum. */
5362 if (name)
5363 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5364 pushtag (name, newtag, /*globalize=*/0);
5365 }
5366 }
5367 else if (TREE_CODE (t) == FUNCTION_DECL
5368 || DECL_FUNCTION_TEMPLATE_P (t))
5369 {
5370 /* Build new TYPE_METHODS. */
ae58fa02 5371
7088fca9
KL
5372 tree r = tsubst (t, args, tf_error, NULL_TREE);
5373 set_current_access_from_decl (r);
5374 grok_special_member_properties (r);
5375 finish_member_declaration (r);
5376 }
5377 else
5378 {
5379 /* Build new TYPE_FIELDS. */
fa8d6e85 5380
7088fca9
KL
5381 if (TREE_CODE (t) != CONST_DECL)
5382 {
5383 tree r;
fa8d6e85 5384
7088fca9
KL
5385 /* The the file and line for this declaration, to assist
5386 in error message reporting. Since we called
5387 push_tinst_level above, we don't need to restore these. */
5388 lineno = DECL_SOURCE_LINE (t);
5389 input_filename = DECL_SOURCE_FILE (t);
fa8d6e85 5390
7088fca9
KL
5391 r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
5392 if (TREE_CODE (r) == VAR_DECL)
5393 {
5394 tree init;
17f29637 5395
7088fca9
KL
5396 if (DECL_INITIALIZED_IN_CLASS_P (r))
5397 init = tsubst_expr (DECL_INITIAL (t), args,
5398 tf_error | tf_warning, NULL_TREE);
5399 else
5400 init = NULL_TREE;
8d08fdba 5401
7088fca9
KL
5402 finish_static_data_member_decl (r, init,
5403 /*asmspec_tree=*/NULL_TREE,
5404 /*flags=*/0);
8d08fdba 5405
7088fca9
KL
5406 if (DECL_INITIALIZED_IN_CLASS_P (r))
5407 check_static_variable_definition (r, TREE_TYPE (r));
5408 }
5409 else if (TREE_CODE (r) == FIELD_DECL)
5410 {
5411 /* Determine whether R has a valid type and can be
5412 completed later. If R is invalid, then it is
5413 replaced by error_mark_node so that it will not be
5414 added to TYPE_FIELDS. */
5415 tree rtype = TREE_TYPE (r);
5416 if (can_complete_type_without_circularity (rtype))
5417 complete_type (rtype);
5418
5419 if (!COMPLETE_TYPE_P (rtype))
5420 {
5421 cxx_incomplete_type_error (r, rtype);
5422 r = error_mark_node;
5423 }
5424 }
5566b478 5425
7088fca9
KL
5426 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5427 such a thing will already have been added to the field
5428 list by tsubst_enum in finish_member_declaration in the
5429 CLASSTYPE_TAGS case above. */
5430 if (!(TREE_CODE (r) == TYPE_DECL
5431 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
5432 && TYPE_CONTEXT (TREE_TYPE (r)) == type))
5433 {
5434 set_current_access_from_decl (r);
5435 finish_member_declaration (r);
5436 }
5437 }
5438 }
61fc8c9e 5439 }
7088fca9
KL
5440 else
5441 {
5442 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5443 {
5444 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5445
5446 tree friend_type = t;
5447 tree new_friend_type;
1aed5355 5448
7088fca9
KL
5449 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5450 new_friend_type = tsubst_friend_class (friend_type, args);
5451 else if (uses_template_parms (friend_type))
5452 new_friend_type = tsubst (friend_type, args,
5453 tf_error | tf_warning, NULL_TREE);
5454 else
5455 {
5456 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5457
5458 /* The call to xref_tag_from_type does injection for friend
5459 classes. */
5460 push_nested_namespace (ns);
5461 new_friend_type =
5462 xref_tag_from_type (friend_type, NULL_TREE, 1);
5463 pop_nested_namespace (ns);
5464 }
1aed5355 5465
7088fca9
KL
5466 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5467 /* Trick make_friend_class into realizing that the friend
5468 we're adding is a template, not an ordinary class. It's
5469 important that we use make_friend_class since it will
5470 perform some error-checking and output cross-reference
5471 information. */
5472 ++processing_template_decl;
fc378698 5473
7088fca9
KL
5474 if (new_friend_type != error_mark_node)
5475 make_friend_class (type, new_friend_type);
fc378698 5476
7088fca9
KL
5477 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5478 --processing_template_decl;
5479 }
5480 else
5481 {
5482 /* Build new DECL_FRIENDLIST. */
5483
5484 add_friend (type,
5485 tsubst_friend_function (t, args));
5486 }
5487 }
5488 }
5566b478 5489
61a127b3
MM
5490 /* Set the file and line number information to whatever is given for
5491 the class itself. This puts error messages involving generated
5492 implicit functions at a predictable point, and the same point
5493 that would be used for non-template classes. */
7088fca9 5494 typedecl = TYPE_MAIN_DECL (type);
61a127b3
MM
5495 lineno = DECL_SOURCE_LINE (typedecl);
5496 input_filename = DECL_SOURCE_FILE (typedecl);
5497
5498 unreverse_member_declarations (type);
9f33663b 5499 finish_struct_1 (type);
2604412d 5500 CLASSTYPE_GOT_SEMICOLON (type) = 1;
e92cc029 5501
5524676d
JM
5502 /* Clear this now so repo_template_used is happy. */
5503 TYPE_BEING_DEFINED (type) = 0;
2604412d 5504 repo_template_used (type);
8d08fdba 5505
9188c363
MM
5506 /* Now that the class is complete, instantiate default arguments for
5507 any member functions. We don't do this earlier because the
5508 default arguments may reference members of the class. */
5509 if (!PRIMARY_TEMPLATE_P (template))
5510 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5511 if (TREE_CODE (t) == FUNCTION_DECL
dc957d14 5512 /* Implicitly generated member functions will not have template
9188c363
MM
5513 information; they are not instantiations, but instead are
5514 created "fresh" for each instantiation. */
5515 && DECL_TEMPLATE_INFO (t))
5516 tsubst_default_arguments (t);
5517
b74a0560 5518 popclass ();
5566b478
MS
5519 pop_from_top_level ();
5520 pop_tinst_level ();
5521
9aad8f83
MA
5522 if (TYPE_CONTAINS_VPTR_P (type))
5523 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
5524
5566b478 5525 return type;
8d08fdba
MS
5526}
5527
5528static int
5529list_eq (t1, t2)
5530 tree t1, t2;
5531{
5532 if (t1 == NULL_TREE)
5533 return t2 == NULL_TREE;
5534 if (t2 == NULL_TREE)
5535 return 0;
5536 /* Don't care if one declares its arg const and the other doesn't -- the
5537 main variant of the arg type is all that matters. */
5538 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5539 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5540 return 0;
5541 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5542}
5543
00d3396f
JM
5544/* If arg is a non-type template parameter that does not depend on template
5545 arguments, fold it like we weren't in the body of a template. */
5546
5547static tree
5548maybe_fold_nontype_arg (arg)
5549 tree arg;
5550{
4d5f3fbd 5551 if (arg && !TYPE_P (arg) && !uses_template_parms (arg))
00d3396f
JM
5552 {
5553 /* Sometimes, one of the args was an expression involving a
5554 template constant parameter, like N - 1. Now that we've
5555 tsubst'd, we might have something like 2 - 1. This will
5556 confuse lookup_template_class, so we do constant folding
5557 here. We have to unset processing_template_decl, to
5558 fool build_expr_from_tree() into building an actual
5559 tree. */
5560
4d5f3fbd
KL
5561 /* If the TREE_TYPE of ARG is not NULL_TREE, ARG is already
5562 as simple as it's going to get, and trying to reprocess
5563 the trees will break. */
5564 if (!TREE_TYPE (arg))
5565 {
5566 int saved_processing_template_decl = processing_template_decl;
5567 processing_template_decl = 0;
5568 arg = build_expr_from_tree (arg);
5569 processing_template_decl = saved_processing_template_decl;
5570 }
5571
5572 arg = fold (arg);
00d3396f
JM
5573 }
5574 return arg;
5575}
5576
830bfa74
MM
5577/* Substitute ARGS into the vector of template arguments T. */
5578
e9659ab0 5579static tree
4393e105 5580tsubst_template_arg_vector (t, args, complain)
830bfa74
MM
5581 tree t;
5582 tree args;
c2ea3a40 5583 tsubst_flags_t complain;
830bfa74
MM
5584{
5585 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5586 tree *elts = (tree *) alloca (len * sizeof (tree));
5587
961192e1 5588 memset ((char *) elts, 0, len * sizeof (tree));
830bfa74
MM
5589
5590 for (i = 0; i < len; i++)
5591 {
5592 if (TREE_VEC_ELT (t, i) != NULL_TREE
5593 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
4393e105
MM
5594 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5595 args, complain);
830bfa74
MM
5596 else
5597 elts[i] = maybe_fold_nontype_arg
4393e105
MM
5598 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5599 NULL_TREE));
830bfa74 5600
08e72a19
JM
5601 if (elts[i] == error_mark_node)
5602 return error_mark_node;
5603
830bfa74
MM
5604 if (elts[i] != TREE_VEC_ELT (t, i))
5605 need_new = 1;
5606 }
5607
5608 if (!need_new)
5609 return t;
5610
f31c0a32 5611 t = make_tree_vec (len);
830bfa74
MM
5612 for (i = 0; i < len; i++)
5613 TREE_VEC_ELT (t, i) = elts[i];
5614
5615 return t;
5616}
5617
36a117a5
MM
5618/* Return the result of substituting ARGS into the template parameters
5619 given by PARMS. If there are m levels of ARGS and m + n levels of
5620 PARMS, then the result will contain n levels of PARMS. For
5621 example, if PARMS is `template <class T> template <class U>
5622 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5623 result will be `template <int*, double, class V>'. */
5624
e9659ab0 5625static tree
4393e105 5626tsubst_template_parms (parms, args, complain)
36a117a5
MM
5627 tree parms;
5628 tree args;
c2ea3a40 5629 tsubst_flags_t complain;
36a117a5 5630{
f71f87f9
MM
5631 tree r = NULL_TREE;
5632 tree* new_parms;
36a117a5
MM
5633
5634 for (new_parms = &r;
5635 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5636 new_parms = &(TREE_CHAIN (*new_parms)),
5637 parms = TREE_CHAIN (parms))
5638 {
5639 tree new_vec =
5640 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5641 int i;
5642
5643 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5644 {
833aa4c4
NS
5645 tree tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
5646 tree default_value = TREE_PURPOSE (tuple);
5647 tree parm_decl = TREE_VALUE (tuple);
5648
5649 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
5650 default_value = tsubst_expr (default_value, args,
5651 complain, NULL_TREE);
5652 tuple = build_tree_list (maybe_fold_nontype_arg (default_value),
5653 parm_decl);
5654 TREE_VEC_ELT (new_vec, i) = tuple;
36a117a5
MM
5655 }
5656
5657 *new_parms =
4890c2f4
MM
5658 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
5659 - TMPL_ARGS_DEPTH (args)),
36a117a5
MM
5660 new_vec, NULL_TREE);
5661 }
5662
5663 return r;
5664}
5665
ed44da02
MM
5666/* Substitute the ARGS into the indicated aggregate (or enumeration)
5667 type T. If T is not an aggregate or enumeration type, it is
5668 handled as if by tsubst. IN_DECL is as for tsubst. If
838dfd8a 5669 ENTERING_SCOPE is nonzero, T is the context for a template which
dc957d14 5670 we are presently tsubst'ing. Return the substituted value. */
36a117a5 5671
e9659ab0 5672static tree
4393e105 5673tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
36a117a5
MM
5674 tree t;
5675 tree args;
c2ea3a40 5676 tsubst_flags_t complain;
36a117a5
MM
5677 tree in_decl;
5678 int entering_scope;
5679{
5680 if (t == NULL_TREE)
5681 return NULL_TREE;
5682
5683 switch (TREE_CODE (t))
5684 {
5685 case RECORD_TYPE:
5686 if (TYPE_PTRMEMFUNC_P (t))
46c895ac 5687 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
36a117a5
MM
5688
5689 /* else fall through */
ed44da02 5690 case ENUMERAL_TYPE:
36a117a5 5691 case UNION_TYPE:
5db698f6 5692 if (TYPE_TEMPLATE_INFO (t))
36a117a5
MM
5693 {
5694 tree argvec;
5695 tree context;
5696 tree r;
5697
5698 /* First, determine the context for the type we are looking
5699 up. */
5700 if (TYPE_CONTEXT (t) != NULL_TREE)
5701 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
4393e105 5702 complain,
36a117a5
MM
5703 in_decl, /*entering_scope=*/1);
5704 else
5705 context = NULL_TREE;
5706
5707 /* Then, figure out what arguments are appropriate for the
5708 type we are trying to find. For example, given:
5709
5710 template <class T> struct S;
5711 template <class T, class U> void f(T, U) { S<U> su; }
5712
5713 and supposing that we are instantiating f<int, double>,
5714 then our ARGS will be {int, double}, but, when looking up
5715 S we only want {double}. */
4393e105
MM
5716 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5717 complain);
08e72a19
JM
5718 if (argvec == error_mark_node)
5719 return error_mark_node;
36a117a5
MM
5720
5721 r = lookup_template_class (t, argvec, in_decl, context,
f9c244b8 5722 entering_scope, complain);
36a117a5 5723
c2ea3a40 5724 return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
36a117a5
MM
5725 }
5726 else
5727 /* This is not a template type, so there's nothing to do. */
5728 return t;
5729
5730 default:
4393e105 5731 return tsubst (t, args, complain, in_decl);
36a117a5
MM
5732 }
5733}
5734
9188c363
MM
5735/* Substitute into the default argument ARG (a default argument for
5736 FN), which has the indicated TYPE. */
5737
5738tree
5739tsubst_default_argument (fn, type, arg)
5740 tree fn;
5741 tree type;
5742 tree arg;
5743{
5744 /* This default argument came from a template. Instantiate the
5745 default argument here, not in tsubst. In the case of
5746 something like:
5747
5748 template <class T>
5749 struct S {
5750 static T t();
5751 void f(T = t());
5752 };
5753
5754 we must be careful to do name lookup in the scope of S<T>,
a29e1034
JM
5755 rather than in the current class.
5756
5757 ??? current_class_type affects a lot more than name lookup. This is
5758 very fragile. Fortunately, it will go away when we do 2-phase name
5759 binding properly. */
2b59fc25
KL
5760
5761 /* FN is already the desired FUNCTION_DECL. */
5762 push_access_scope (fn);
9188c363 5763
c2ea3a40
NS
5764 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
5765 tf_error | tf_warning, NULL_TREE);
9188c363 5766
2b59fc25 5767 pop_access_scope (fn);
9188c363
MM
5768
5769 /* Make sure the default argument is reasonable. */
5770 arg = check_default_argument (type, arg);
5771
5772 return arg;
5773}
5774
5775/* Substitute into all the default arguments for FN. */
5776
5777static void
5778tsubst_default_arguments (fn)
5779 tree fn;
5780{
5781 tree arg;
5782 tree tmpl_args;
5783
5784 tmpl_args = DECL_TI_ARGS (fn);
5785
5786 /* If this function is not yet instantiated, we certainly don't need
5787 its default arguments. */
5788 if (uses_template_parms (tmpl_args))
5789 return;
5790
5791 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5792 arg;
5793 arg = TREE_CHAIN (arg))
5794 if (TREE_PURPOSE (arg))
5795 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5796 TREE_VALUE (arg),
5797 TREE_PURPOSE (arg));
5798}
5799
ae58fa02
MM
5800/* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5801 (already computed) substitution of ARGS into TREE_TYPE (T), if
c2ea3a40
NS
5802 appropriate. Return the result of the substitution. Issue error
5803 and warning messages under control of COMPLAIN. */
00d3396f 5804
e9659ab0 5805static tree
c2ea3a40 5806tsubst_decl (t, args, type, complain)
ae58fa02
MM
5807 tree t;
5808 tree args;
5809 tree type;
c2ea3a40 5810 tsubst_flags_t complain;
8d08fdba 5811{
ae58fa02 5812 int saved_lineno;
3b304f5b 5813 const char *saved_filename;
b370501f 5814 tree r = NULL_TREE;
4b2811e9 5815 tree in_decl = t;
830bfa74 5816
ae58fa02
MM
5817 /* Set the filename and linenumber to improve error-reporting. */
5818 saved_lineno = lineno;
5819 saved_filename = input_filename;
5820 lineno = DECL_SOURCE_LINE (t);
5821 input_filename = DECL_SOURCE_FILE (t);
b7484fbe 5822
8d08fdba
MS
5823 switch (TREE_CODE (t))
5824 {
98c1c668
JM
5825 case TEMPLATE_DECL:
5826 {
5827 /* We can get here when processing a member template function
5828 of a template class. */
98c1c668 5829 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 5830 tree spec;
db2767b6 5831 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
98c1c668 5832
db2767b6
MM
5833 if (!is_template_template_parm)
5834 {
36a117a5
MM
5835 /* We might already have an instance of this template.
5836 The ARGS are for the surrounding class type, so the
5837 full args contain the tsubst'd args for the context,
5838 plus the innermost args from the template decl. */
5839 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5840 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
17aec3eb 5841 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
7ddedda4
MM
5842 tree full_args;
5843
4393e105 5844 full_args = tsubst_template_arg_vector (tmpl_args, args,
c2ea3a40 5845 complain);
36a117a5
MM
5846
5847 /* tsubst_template_arg_vector doesn't copy the vector if
5848 nothing changed. But, *something* should have
5849 changed. */
5850 my_friendly_assert (full_args != tmpl_args, 0);
5851
5852 spec = retrieve_specialization (t, full_args);
db2767b6 5853 if (spec != NULL_TREE)
ae58fa02
MM
5854 {
5855 r = spec;
5856 break;
5857 }
db2767b6 5858 }
98c1c668
JM
5859
5860 /* Make a new template decl. It will be similar to the
5861 original, but will record the current template arguments.
5862 We also create a new function declaration, which is just
5863 like the old one, but points to this new template, rather
5864 than the old one. */
0acf7199 5865 r = copy_decl (t);
ae58fa02
MM
5866 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5867 TREE_CHAIN (r) = NULL_TREE;
db2767b6
MM
5868
5869 if (is_template_template_parm)
5870 {
c2ea3a40 5871 tree new_decl = tsubst (decl, args, complain, in_decl);
17aec3eb 5872 DECL_TEMPLATE_RESULT (r) = new_decl;
ae58fa02
MM
5873 TREE_TYPE (r) = TREE_TYPE (new_decl);
5874 break;
db2767b6
MM
5875 }
5876
ae58fa02 5877 DECL_CONTEXT (r)
4f1c5b7d 5878 = tsubst_aggr_type (DECL_CONTEXT (t), args,
c2ea3a40 5879 complain, in_decl,
4393e105 5880 /*entering_scope=*/1);
ae58fa02 5881 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
5882
5883 if (TREE_CODE (decl) == TYPE_DECL)
5884 {
c2ea3a40 5885 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
ae58fa02
MM
5886 TREE_TYPE (r) = new_type;
5887 CLASSTYPE_TI_TEMPLATE (new_type) = r;
17aec3eb 5888 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
ae58fa02 5889 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
93cdc044
JM
5890 }
5891 else
5892 {
c2ea3a40 5893 tree new_decl = tsubst (decl, args, complain, in_decl);
17aec3eb
RK
5894
5895 DECL_TEMPLATE_RESULT (r) = new_decl;
ae58fa02
MM
5896 DECL_TI_TEMPLATE (new_decl) = r;
5897 TREE_TYPE (r) = TREE_TYPE (new_decl);
5898 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
93cdc044
JM
5899 }
5900
ae58fa02
MM
5901 SET_DECL_IMPLICIT_INSTANTIATION (r);
5902 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5903 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
5904
5905 /* The template parameters for this new template are all the
5906 template parameters for the old template, except the
c6002625 5907 outermost level of parameters. */
ae58fa02 5908 DECL_TEMPLATE_PARMS (r)
4393e105 5909 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
c2ea3a40 5910 complain);
98c1c668 5911
93cdc044 5912 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 5913 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 5914
8d019cef 5915 /* We don't partially instantiate partial specializations. */
93cdc044 5916 if (TREE_CODE (decl) == TYPE_DECL)
ae58fa02 5917 break;
93cdc044 5918
75650646 5919 /* Record this partial instantiation. */
ae58fa02 5920 register_specialization (r, t,
17aec3eb 5921 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
75650646 5922
98c1c668 5923 }
ae58fa02 5924 break;
8d08fdba
MS
5925
5926 case FUNCTION_DECL:
5927 {
386b8a85 5928 tree ctx;
87603ed0 5929 tree argvec = NULL_TREE;
cf38f48a 5930 tree *friends;
36a117a5 5931 tree gen_tmpl;
5566b478 5932 int member;
d8c4447d
MM
5933 int args_depth;
5934 int parms_depth;
5566b478 5935
36a117a5
MM
5936 /* Nobody should be tsubst'ing into non-template functions. */
5937 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5938
5939 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5940 {
5941 tree spec;
5942
5943 /* Calculate the most general template of which R is a
5944 specialization, and the complete set of arguments used to
5945 specialize R. */
5946 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7ddedda4
MM
5947 argvec
5948 = tsubst_template_arg_vector (DECL_TI_ARGS
5949 (DECL_TEMPLATE_RESULT (gen_tmpl)),
c2ea3a40 5950 args, complain);
36a117a5
MM
5951
5952 /* Check to see if we already have this specialization. */
5953 spec = retrieve_specialization (gen_tmpl, argvec);
7ddedda4 5954
36a117a5 5955 if (spec)
ae58fa02
MM
5956 {
5957 r = spec;
5958 break;
5959 }
d8c4447d 5960
f9a7ae04
MM
5961 /* We can see more levels of arguments than parameters if
5962 there was a specialization of a member template, like
5963 this:
5964
5965 template <class T> struct S { template <class U> void f(); }
5966 template <> template <class U> void S<int>::f(U);
5967
dc957d14 5968 Here, we'll be substituting into the specialization,
f9a7ae04
MM
5969 because that's where we can find the code we actually
5970 want to generate, but we'll have enough arguments for
5971 the most general template.
5972
5973 We also deal with the peculiar case:
d8c4447d
MM
5974
5975 template <class T> struct S {
5976 template <class U> friend void f();
5977 };
74b846e0 5978 template <class U> void f() {}
d8c4447d
MM
5979 template S<int>;
5980 template void f<double>();
5981
5982 Here, the ARGS for the instantiation of will be {int,
5983 double}. But, we only need as many ARGS as there are
5984 levels of template parameters in CODE_PATTERN. We are
5985 careful not to get fooled into reducing the ARGS in
5986 situations like:
5987
5988 template <class T> struct S { template <class U> void f(U); }
5989 template <class T> template <> void S<T>::f(int) {}
5990
5991 which we can spot because the pattern will be a
5992 specialization in this case. */
5993 args_depth = TMPL_ARGS_DEPTH (args);
5994 parms_depth =
5995 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5996 if (args_depth > parms_depth
5997 && !DECL_TEMPLATE_SPECIALIZATION (t))
f9a7ae04 5998 args = get_innermost_template_args (args, parms_depth);
36a117a5
MM
5999 }
6000 else
6001 {
6002 /* This special case arises when we have something like this:
6003
6004 template <class T> struct S {
6005 friend void f<int>(int, double);
6006 };
6007
6008 Here, the DECL_TI_TEMPLATE for the friend declaration
39c01e4c
MM
6009 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
6010 being called from tsubst_friend_function, and we want
6011 only to create a new decl (R) with appropriate types so
6012 that we can call determine_specialization. */
36a117a5
MM
6013 gen_tmpl = NULL_TREE;
6014 }
6015
6eb3bb27 6016 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 6017 {
5566b478
MS
6018 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6019 member = 2;
6020 else
6021 member = 1;
4f1c5b7d 6022 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
c2ea3a40 6023 complain, t,
36a117a5 6024 /*entering_scope=*/1);
5566b478
MS
6025 }
6026 else
6027 {
6028 member = 0;
4f1c5b7d 6029 ctx = DECL_CONTEXT (t);
5566b478 6030 }
c2ea3a40 6031 type = tsubst (type, args, complain, in_decl);
b3da7bb1
MM
6032 if (type == error_mark_node)
6033 return error_mark_node;
8d08fdba 6034
5566b478
MS
6035 /* We do NOT check for matching decls pushed separately at this
6036 point, as they may not represent instantiations of this
6037 template, and in any case are considered separate under the
0a7394bc 6038 discrete model. */
0acf7199 6039 r = copy_decl (t);
e1467ff2 6040 DECL_USE_TEMPLATE (r) = 0;
5566b478 6041 TREE_TYPE (r) = type;
92643fea
MM
6042 /* Clear out the mangled name and RTL for the instantiation. */
6043 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6044 SET_DECL_RTL (r, NULL_RTX);
5566b478 6045
4f1c5b7d 6046 DECL_CONTEXT (r) = ctx;
5566b478 6047
1f6e1acc 6048 if (member && DECL_CONV_FN_P (r))
1f84ec23
MM
6049 /* Type-conversion operator. Reconstruct the name, in
6050 case it's the name of one of the template's parameters. */
6051 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5566b478 6052
4393e105 6053 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
c2ea3a40 6054 complain, t);
477f6664 6055 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
6056
6057 TREE_STATIC (r) = 0;
6058 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6059 DECL_EXTERNAL (r) = 1;
6060 DECL_INTERFACE_KNOWN (r) = 0;
6061 DECL_DEFER_OUTPUT (r) = 0;
6062 TREE_CHAIN (r) = NULL_TREE;
6063 DECL_PENDING_INLINE_INFO (r) = 0;
59026e79 6064 DECL_PENDING_INLINE_P (r) = 0;
655dc6ee 6065 DECL_SAVED_TREE (r) = NULL_TREE;
711734a9 6066 TREE_USED (r) = 0;
db9b2174
MM
6067 if (DECL_CLONED_FUNCTION (r))
6068 {
6069 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
c2ea3a40 6070 args, complain, t);
db9b2174
MM
6071 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6072 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6073 }
711734a9 6074
92643fea
MM
6075 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6076 this in the special friend case mentioned above where
6077 GEN_TMPL is NULL. */
36a117a5 6078 if (gen_tmpl)
386b8a85 6079 {
36a117a5 6080 DECL_TEMPLATE_INFO (r)
e1b3e07d 6081 = tree_cons (gen_tmpl, argvec, NULL_TREE);
36a117a5
MM
6082 SET_DECL_IMPLICIT_INSTANTIATION (r);
6083 register_specialization (r, gen_tmpl, argvec);
6084
9188c363
MM
6085 /* We're not supposed to instantiate default arguments
6086 until they are called, for a template. But, for a
6087 declaration like:
6088
6089 template <class T> void f ()
6090 { extern void g(int i = T()); }
6091
6092 we should do the substitution when the template is
6093 instantiated. We handle the member function case in
6094 instantiate_class_template since the default arguments
6095 might refer to other members of the class. */
6096 if (!member
6097 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6098 && !uses_template_parms (argvec))
6099 tsubst_default_arguments (r);
386b8a85 6100 }
f181d4ae 6101
cf38f48a
MM
6102 /* Copy the list of befriending classes. */
6103 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6104 *friends;
6105 friends = &TREE_CHAIN (*friends))
6106 {
6107 *friends = copy_node (*friends);
6108 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
c2ea3a40 6109 args, complain,
cf38f48a
MM
6110 in_decl);
6111 }
6112
212e7048 6113 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
f181d4ae
MM
6114 {
6115 maybe_retrofit_in_chrg (r);
212e7048
MM
6116 if (DECL_CONSTRUCTOR_P (r))
6117 grok_ctor_properties (ctx, r);
2be678ff
JM
6118 /* If this is an instantiation of a member template, clone it.
6119 If it isn't, that'll be handled by
6120 clone_constructors_and_destructors. */
5e818b93 6121 if (PRIMARY_TEMPLATE_P (gen_tmpl))
271e6f02 6122 clone_function_decl (r, /*update_method_vec_p=*/0);
f181d4ae 6123 }
596ea4e5 6124 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
271e6f02 6125 grok_op_properties (r, DECL_FRIEND_P (r));
8d08fdba 6126 }
ae58fa02 6127 break;
8d08fdba
MS
6128
6129 case PARM_DECL:
6130 {
ae58fa02 6131 r = copy_node (t);
833aa4c4
NS
6132 if (DECL_TEMPLATE_PARM_P (t))
6133 SET_DECL_TEMPLATE_PARM_P (r);
8e51619a 6134
5566b478 6135 TREE_TYPE (r) = type;
89d684bb 6136 c_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1 6137
db2767b6
MM
6138 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6139 DECL_INITIAL (r) = TREE_TYPE (r);
6140 else
4393e105 6141 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
c2ea3a40 6142 complain, in_decl);
db2767b6 6143
5566b478 6144 DECL_CONTEXT (r) = NULL_TREE;
8e51619a
JM
6145
6146 if (!DECL_TEMPLATE_PARM_P (r))
6147 DECL_ARG_TYPE (r) = type_passed_as (type);
8d08fdba 6148 if (TREE_CHAIN (t))
4393e105 6149 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
c2ea3a40 6150 complain, TREE_CHAIN (t));
8d08fdba 6151 }
ae58fa02 6152 break;
8d08fdba 6153
5566b478
MS
6154 case FIELD_DECL:
6155 {
0acf7199 6156 r = copy_decl (t);
1b8899d1 6157 TREE_TYPE (r) = type;
89d684bb 6158 c_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1
MM
6159
6160 /* We don't have to set DECL_CONTEXT here; it is set by
6161 finish_member_declaration. */
4393e105 6162 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
c2ea3a40 6163 complain, in_decl);
5566b478 6164 TREE_CHAIN (r) = NULL_TREE;
dfbb4f34
NS
6165 if (VOID_TYPE_P (type))
6166 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
5566b478 6167 }
ae58fa02 6168 break;
5566b478
MS
6169
6170 case USING_DECL:
6171 {
ae58fa02 6172 r = copy_node (t);
5566b478 6173 DECL_INITIAL (r)
c2ea3a40 6174 = tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
5566b478 6175 TREE_CHAIN (r) = NULL_TREE;
5566b478 6176 }
ae58fa02 6177 break;
5566b478 6178
9188c363 6179 case TYPE_DECL:
88e1b92a
JM
6180 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6181 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
9188c363 6182 {
88e1b92a
JM
6183 /* If this is the canonical decl, we don't have to mess with
6184 instantiations, and often we can't (for typename, template
6185 type parms and such). Note that TYPE_NAME is not correct for
6186 the above test if we've copied the type for a typedef. */
b9f39201 6187 r = TYPE_NAME (type);
9188c363
MM
6188 break;
6189 }
6190
6191 /* Fall through. */
6192
5566b478
MS
6193 case VAR_DECL:
6194 {
1cea0434
KG
6195 tree argvec = NULL_TREE;
6196 tree gen_tmpl = NULL_TREE;
36a117a5 6197 tree spec;
1cea0434 6198 tree tmpl = NULL_TREE;
9188c363 6199 tree ctx;
6dfbb909 6200 int local_p;
9188c363 6201
6dfbb909
MM
6202 /* Assume this is a non-local variable. */
6203 local_p = 0;
5566b478 6204
de96bf57 6205 if (TYPE_P (CP_DECL_CONTEXT (t)))
9188c363 6206 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
c2ea3a40 6207 complain,
9188c363 6208 in_decl, /*entering_scope=*/1);
2ba9c47e
MM
6209 else if (DECL_NAMESPACE_SCOPE_P (t))
6210 ctx = DECL_CONTEXT (t);
9188c363 6211 else
6dfbb909
MM
6212 {
6213 /* Subsequent calls to pushdecl will fill this in. */
6214 ctx = NULL_TREE;
2ba9c47e 6215 local_p = 1;
6dfbb909 6216 }
9188c363 6217
36a117a5 6218 /* Check to see if we already have this specialization. */
6dfbb909
MM
6219 if (!local_p)
6220 {
6221 tmpl = DECL_TI_TEMPLATE (t);
6222 gen_tmpl = most_general_template (tmpl);
c2ea3a40 6223 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6dfbb909
MM
6224 spec = retrieve_specialization (gen_tmpl, argvec);
6225 }
9188c363 6226 else
6dfbb909 6227 spec = retrieve_local_specialization (t);
9188c363 6228
36a117a5 6229 if (spec)
ae58fa02
MM
6230 {
6231 r = spec;
6232 break;
6233 }
5566b478 6234
0acf7199 6235 r = copy_decl (t);
edebf865
MM
6236 if (TREE_CODE (r) == VAR_DECL)
6237 type = complete_type (type);
01f4137f 6238 TREE_TYPE (r) = type;
89d684bb 6239 c_apply_type_quals_to_decl (cp_type_quals (type), r);
5566b478 6240 DECL_CONTEXT (r) = ctx;
92643fea
MM
6241 /* Clear out the mangled name and RTL for the instantiation. */
6242 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6243 SET_DECL_RTL (r, NULL_RTX);
d11ad92e
MS
6244
6245 /* Don't try to expand the initializer until someone tries to use
6246 this variable; otherwise we run into circular dependencies. */
6247 DECL_INITIAL (r) = NULL_TREE;
19e7881c 6248 SET_DECL_RTL (r, NULL_RTX);
06ceef4e 6249 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
5566b478 6250
e0942dcd
MM
6251 /* Even if the original location is out of scope, the newly
6252 substituted one is not. */
6253 if (TREE_CODE (r) == VAR_DECL)
17bbb839
MM
6254 {
6255 DECL_DEAD_FOR_LOCAL (r) = 0;
6256 DECL_INITIALIZED_P (r) = 0;
6257 }
e0942dcd 6258
6dfbb909
MM
6259 if (!local_p)
6260 {
6261 /* A static data member declaration is always marked
6262 external when it is declared in-class, even if an
6263 initializer is present. We mimic the non-template
6264 processing here. */
6265 DECL_EXTERNAL (r) = 1;
fa8d6e85 6266
6dfbb909
MM
6267 register_specialization (r, gen_tmpl, argvec);
6268 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6269 SET_DECL_IMPLICIT_INSTANTIATION (r);
6270 }
9188c363 6271 else
6dfbb909 6272 register_local_specialization (r, t);
5566b478 6273
5566b478 6274 TREE_CHAIN (r) = NULL_TREE;
dfbb4f34
NS
6275 if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
6276 cp_error_at ("instantiation of `%D' as type `%T'", r, type);
edebf865
MM
6277 /* Compute the size, alignment, etc. of R. */
6278 layout_decl (r, 0);
5566b478 6279 }
ae58fa02 6280 break;
5566b478 6281
ae58fa02 6282 default:
a98facb0 6283 abort ();
ae58fa02
MM
6284 }
6285
6286 /* Restore the file and line information. */
6287 lineno = saved_lineno;
6288 input_filename = saved_filename;
6289
6290 return r;
6291}
6292
cabc336a
MM
6293/* Substitue into the ARG_TYPES of a function type. */
6294
e9659ab0 6295static tree
4393e105 6296tsubst_arg_types (arg_types, args, complain, in_decl)
cabc336a
MM
6297 tree arg_types;
6298 tree args;
c2ea3a40 6299 tsubst_flags_t complain;
cabc336a
MM
6300 tree in_decl;
6301{
6302 tree remaining_arg_types;
cabc336a
MM
6303 tree type;
6304
6305 if (!arg_types || arg_types == void_list_node)
6306 return arg_types;
6307
6308 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
4393e105
MM
6309 args, complain, in_decl);
6310 if (remaining_arg_types == error_mark_node)
6311 return error_mark_node;
6312
6313 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6314 if (type == error_mark_node)
6315 return error_mark_node;
4b2811e9
NS
6316 if (VOID_TYPE_P (type))
6317 {
c2ea3a40 6318 if (complain & tf_error)
4b2811e9 6319 {
33bd39a2 6320 error ("invalid parameter type `%T'", type);
4b2811e9
NS
6321 if (in_decl)
6322 cp_error_at ("in declaration `%D'", in_decl);
6323 }
6324 return error_mark_node;
6325 }
cabc336a 6326
4393e105
MM
6327 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6328 top-level qualifiers as required. */
6329 type = TYPE_MAIN_VARIANT (type_decays_to (type));
cabc336a
MM
6330
6331 /* Note that we do not substitute into default arguments here. The
6332 standard mandates that they be instantiated only when needed,
6333 which is done in build_over_call. */
51632249
JM
6334 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6335 remaining_arg_types);
cabc336a
MM
6336
6337}
6338
4393e105
MM
6339/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6340 *not* handle the exception-specification for FNTYPE, because the
6341 initial substitution of explicitly provided template parameters
6342 during argument deduction forbids substitution into the
6343 exception-specification:
6344
6345 [temp.deduct]
6346
6347 All references in the function type of the function template to the
6348 corresponding template parameters are replaced by the specified tem-
6349 plate argument values. If a substitution in a template parameter or
6350 in the function type of the function template results in an invalid
6351 type, type deduction fails. [Note: The equivalent substitution in
6352 exception specifications is done only when the function is instanti-
6353 ated, at which point a program is ill-formed if the substitution
6354 results in an invalid type.] */
6355
6356static tree
6357tsubst_function_type (t, args, complain, in_decl)
6358 tree t;
6359 tree args;
c2ea3a40 6360 tsubst_flags_t complain;
4393e105
MM
6361 tree in_decl;
6362{
6363 tree return_type;
6364 tree arg_types;
6365 tree fntype;
6366
8dd3f57a 6367 /* The TYPE_CONTEXT is not used for function/method types. */
4393e105
MM
6368 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6369
46c895ac 6370 /* Substitute the return type. */
4393e105
MM
6371 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6372 if (return_type == error_mark_node)
6373 return error_mark_node;
6374
6375 /* Substitue the argument types. */
6376 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6377 complain, in_decl);
6378 if (arg_types == error_mark_node)
6379 return error_mark_node;
6380
6381 /* Construct a new type node and return it. */
6382 if (TREE_CODE (t) == FUNCTION_TYPE)
6383 fntype = build_function_type (return_type, arg_types);
6384 else
6385 {
6386 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6387 if (! IS_AGGR_TYPE (r))
6388 {
6389 /* [temp.deduct]
6390
6391 Type deduction may fail for any of the following
6392 reasons:
6393
6394 -- Attempting to create "pointer to member of T" when T
6395 is not a class type. */
c2ea3a40 6396 if (complain & tf_error)
33bd39a2 6397 error ("creating pointer to member function of non-class type `%T'",
4393e105
MM
6398 r);
6399 return error_mark_node;
6400 }
6401
6402 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6403 (arg_types));
6404 }
c2ea3a40 6405 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
ada846eb 6406 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
4393e105
MM
6407
6408 return fntype;
6409}
6410
297e73d8
MM
6411/* Substitute into the PARMS of a call-declarator. */
6412
e9659ab0 6413static tree
4393e105 6414tsubst_call_declarator_parms (parms, args, complain, in_decl)
297e73d8
MM
6415 tree parms;
6416 tree args;
c2ea3a40 6417 tsubst_flags_t complain;
297e73d8
MM
6418 tree in_decl;
6419{
6420 tree new_parms;
6421 tree type;
6422 tree defarg;
6423
6424 if (!parms || parms == void_list_node)
6425 return parms;
6426
6427 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
4393e105 6428 args, complain, in_decl);
297e73d8
MM
6429
6430 /* Figure out the type of this parameter. */
4393e105 6431 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
297e73d8
MM
6432
6433 /* Figure out the default argument as well. Note that we use
4393e105
MM
6434 tsubst_expr since the default argument is really an expression. */
6435 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
297e73d8
MM
6436
6437 /* Chain this parameter on to the front of those we have already
6438 processed. We don't use hash_tree_cons because that function
6439 doesn't check TREE_PARMLIST. */
6440 new_parms = tree_cons (defarg, type, new_parms);
6441
6442 /* And note that these are parameters. */
6443 TREE_PARMLIST (new_parms) = 1;
6444
6445 return new_parms;
6446}
6447
4393e105
MM
6448/* Take the tree structure T and replace template parameters used
6449 therein with the argument vector ARGS. IN_DECL is an associated
6450 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
c2ea3a40
NS
6451 Issue error and warning messages under control of COMPLAIN. Note
6452 that we must be relatively non-tolerant of extensions here, in
6453 order to preserve conformance; if we allow substitutions that
6454 should not be allowed, we may allow argument deductions that should
6455 not succeed, and therefore report ambiguous overload situations
6456 where there are none. In theory, we could allow the substitution,
6457 but indicate that it should have failed, and allow our caller to
6458 make sure that the right thing happens, but we don't try to do this
6459 yet.
4393e105
MM
6460
6461 This function is used for dealing with types, decls and the like;
6462 for expressions, use tsubst_expr or tsubst_copy. */
ae58fa02 6463
14d22dd6 6464static tree
4393e105 6465tsubst (t, args, complain, in_decl)
ae58fa02 6466 tree t, args;
c2ea3a40 6467 tsubst_flags_t complain;
ae58fa02
MM
6468 tree in_decl;
6469{
0ecfe0b4 6470 tree type, r;
ae58fa02
MM
6471
6472 if (t == NULL_TREE || t == error_mark_node
6473 || t == integer_type_node
6474 || t == void_type_node
6475 || t == char_type_node
6476 || TREE_CODE (t) == NAMESPACE_DECL)
6477 return t;
6478
6479 if (TREE_CODE (t) == IDENTIFIER_NODE)
6480 type = IDENTIFIER_TYPE_VALUE (t);
6481 else
6482 type = TREE_TYPE (t);
6483 if (type == unknown_type_node)
a98facb0 6484 abort ();
ae58fa02
MM
6485
6486 if (type && TREE_CODE (t) != FUNCTION_DECL
6487 && TREE_CODE (t) != TYPENAME_TYPE
6488 && TREE_CODE (t) != TEMPLATE_DECL
4393e105
MM
6489 && TREE_CODE (t) != IDENTIFIER_NODE
6490 && TREE_CODE (t) != FUNCTION_TYPE
6491 && TREE_CODE (t) != METHOD_TYPE)
6492 type = tsubst (type, args, complain, in_decl);
6493 if (type == error_mark_node)
6494 return error_mark_node;
ae58fa02 6495
2f939d94 6496 if (DECL_P (t))
c2ea3a40 6497 return tsubst_decl (t, args, type, complain);
ae58fa02
MM
6498
6499 switch (TREE_CODE (t))
6500 {
6501 case RECORD_TYPE:
6502 case UNION_TYPE:
6503 case ENUMERAL_TYPE:
4393e105
MM
6504 return tsubst_aggr_type (t, args, complain, in_decl,
6505 /*entering_scope=*/0);
ae58fa02
MM
6506
6507 case ERROR_MARK:
6508 case IDENTIFIER_NODE:
ae58fa02
MM
6509 case VOID_TYPE:
6510 case REAL_TYPE:
6511 case COMPLEX_TYPE:
c00996a3 6512 case VECTOR_TYPE:
ae58fa02
MM
6513 case BOOLEAN_TYPE:
6514 case INTEGER_CST:
6515 case REAL_CST:
6516 case STRING_CST:
6517 return t;
6518
6519 case INTEGER_TYPE:
6520 if (t == integer_type_node)
6521 return t;
6522
6523 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6524 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6525 return t;
d2e5ee5c 6526
5566b478 6527 {
ddce3528 6528 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 6529
ddce3528 6530 max = tsubst_expr (omax, args, complain, in_decl);
4393e105
MM
6531 if (max == error_mark_node)
6532 return error_mark_node;
6533
8dd3f57a
MM
6534 /* See if we can reduce this expression to something simpler. */
6535 max = maybe_fold_nontype_arg (max);
fc611ce0 6536 if (!processing_template_decl)
8dd3f57a
MM
6537 max = decl_constant_value (max);
6538
856216bb
MM
6539 if (processing_template_decl
6540 /* When providing explicit arguments to a template
6541 function, but leaving some arguments for subsequent
6542 deduction, MAX may be template-dependent even if we're
c95cd22e
JM
6543 not PROCESSING_TEMPLATE_DECL. We still need to check for
6544 template parms, though; MAX won't be an INTEGER_CST for
6545 dynamic arrays, either. */
6546 || (TREE_CODE (max) != INTEGER_CST
6547 && uses_template_parms (max)))
ae58fa02 6548 {
8dd3f57a
MM
6549 tree itype = make_node (INTEGER_TYPE);
6550 TYPE_MIN_VALUE (itype) = size_zero_node;
6551 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6552 integer_one_node);
6553 return itype;
ae58fa02
MM
6554 }
6555
ddce3528
JM
6556 if (integer_zerop (omax))
6557 {
6558 /* Still allow an explicit array of size zero. */
6559 if (pedantic)
6560 pedwarn ("creating array with size zero");
6561 }
07c88314
MM
6562 else if (integer_zerop (max)
6563 || (TREE_CODE (max) == INTEGER_CST
6564 && INT_CST_LT (max, integer_zero_node)))
0ecfe0b4 6565 {
4393e105
MM
6566 /* [temp.deduct]
6567
6568 Type deduction may fail for any of the following
6569 reasons:
6570
bf8f3f93
MM
6571 Attempting to create an array with a size that is
6572 zero or negative. */
c2ea3a40 6573 if (complain & tf_error)
33bd39a2 6574 error ("creating array with size zero (`%E')", max);
4393e105
MM
6575
6576 return error_mark_node;
0ecfe0b4
JM
6577 }
6578
c95cd22e 6579 return compute_array_index_type (NULL_TREE, max);
ae58fa02
MM
6580 }
6581
6582 case TEMPLATE_TYPE_PARM:
6583 case TEMPLATE_TEMPLATE_PARM:
a1281f45 6584 case BOUND_TEMPLATE_TEMPLATE_PARM:
ae58fa02
MM
6585 case TEMPLATE_PARM_INDEX:
6586 {
6587 int idx;
6588 int level;
6589 int levels;
0ecfe0b4
JM
6590
6591 r = NULL_TREE;
ae58fa02
MM
6592
6593 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
a1281f45
KL
6594 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
6595 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
ae58fa02
MM
6596 {
6597 idx = TEMPLATE_TYPE_IDX (t);
6598 level = TEMPLATE_TYPE_LEVEL (t);
6599 }
6600 else
6601 {
6602 idx = TEMPLATE_PARM_IDX (t);
6603 level = TEMPLATE_PARM_LEVEL (t);
6604 }
6605
6606 if (TREE_VEC_LENGTH (args) > 0)
6607 {
6608 tree arg = NULL_TREE;
6609
6610 levels = TMPL_ARGS_DEPTH (args);
6611 if (level <= levels)
6612 arg = TMPL_ARG (args, level, idx);
6613
8b5b8b7c
MM
6614 if (arg == error_mark_node)
6615 return error_mark_node;
6616 else if (arg != NULL_TREE)
ae58fa02
MM
6617 {
6618 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6619 {
2f939d94 6620 my_friendly_assert (TYPE_P (arg), 0);
adecb3f4 6621 return cp_build_qualified_type_real
89d684bb 6622 (arg, cp_type_quals (arg) | cp_type_quals (t),
4f2b0fb2 6623 complain | tf_ignore_bad_quals);
ae58fa02 6624 }
a1281f45 6625 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
ae58fa02 6626 {
a1281f45
KL
6627 /* We are processing a type constructed from
6628 a template template parameter */
6629 tree argvec = tsubst (TYPE_TI_ARGS (t),
6630 args, complain, in_decl);
6631 if (argvec == error_mark_node)
6632 return error_mark_node;
4393e105 6633
a1281f45
KL
6634 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6635 we are resolving nested-types in the signature of
6636 a member function templates.
6637 Otherwise ARG is a TEMPLATE_DECL and is the real
6638 template to be instantiated. */
6639 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6640 arg = TYPE_NAME (arg);
6641
6642 r = lookup_template_class (arg,
6643 argvec, in_decl,
6644 DECL_CONTEXT (arg),
f9c244b8
NS
6645 /*entering_scope=*/0,
6646 complain);
c2ea3a40
NS
6647 return cp_build_qualified_type_real
6648 (r, TYPE_QUALS (t), complain);
ae58fa02
MM
6649 }
6650 else
a1281f45 6651 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
ae58fa02
MM
6652 return arg;
6653 }
6654 }
51924768 6655 else
a98facb0 6656 abort ();
ae58fa02
MM
6657
6658 if (level == 1)
6659 /* This can happen during the attempted tsubst'ing in
6660 unify. This means that we don't yet have any information
6661 about the template parameter in question. */
6662 return t;
6663
6664 /* If we get here, we must have been looking at a parm for a
6665 more deeply nested template. Make a new version of this
6666 template parameter, but with a lower level. */
6667 switch (TREE_CODE (t))
6668 {
6669 case TEMPLATE_TYPE_PARM:
6670 case TEMPLATE_TEMPLATE_PARM:
a1281f45 6671 case BOUND_TEMPLATE_TEMPLATE_PARM:
89d684bb 6672 if (cp_type_quals (t))
ae58fa02 6673 {
9ccf6541 6674 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
4f2b0fb2
NS
6675 r = cp_build_qualified_type_real
6676 (r, cp_type_quals (t),
6677 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6678 ? tf_ignore_bad_quals : 0));
9ccf6541
MM
6679 }
6680 else
6681 {
11e74ea6 6682 r = copy_type (t);
9ccf6541
MM
6683 TEMPLATE_TYPE_PARM_INDEX (r)
6684 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6685 r, levels);
6686 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6687 TYPE_MAIN_VARIANT (r) = r;
6688 TYPE_POINTER_TO (r) = NULL_TREE;
6689 TYPE_REFERENCE_TO (r) = NULL_TREE;
6690
a1281f45 6691 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9ccf6541
MM
6692 {
6693 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6694 complain, in_decl);
6695 if (argvec == error_mark_node)
6696 return error_mark_node;
4393e105 6697
9ccf6541
MM
6698 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
6699 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
6700 }
ae58fa02
MM
6701 }
6702 break;
6703
6704 case TEMPLATE_PARM_INDEX:
6705 r = reduce_template_parm_level (t, type, levels);
6706 break;
6707
6708 default:
a98facb0 6709 abort ();
ae58fa02
MM
6710 }
6711
5566b478 6712 return r;
ae58fa02 6713 }
5566b478 6714
8d08fdba
MS
6715 case TREE_LIST:
6716 {
6717 tree purpose, value, chain, result;
8d08fdba
MS
6718
6719 if (t == void_list_node)
6720 return t;
6721
8d08fdba
MS
6722 purpose = TREE_PURPOSE (t);
6723 if (purpose)
4393e105
MM
6724 {
6725 purpose = tsubst (purpose, args, complain, in_decl);
6726 if (purpose == error_mark_node)
6727 return error_mark_node;
6728 }
8d08fdba
MS
6729 value = TREE_VALUE (t);
6730 if (value)
4393e105
MM
6731 {
6732 value = tsubst (value, args, complain, in_decl);
6733 if (value == error_mark_node)
6734 return error_mark_node;
6735 }
8d08fdba
MS
6736 chain = TREE_CHAIN (t);
6737 if (chain && chain != void_type_node)
4393e105
MM
6738 {
6739 chain = tsubst (chain, args, complain, in_decl);
6740 if (chain == error_mark_node)
6741 return error_mark_node;
6742 }
8d08fdba
MS
6743 if (purpose == TREE_PURPOSE (t)
6744 && value == TREE_VALUE (t)
6745 && chain == TREE_CHAIN (t))
6746 return t;
cbd5937a
MM
6747 if (TREE_PARMLIST (t))
6748 {
6749 result = tree_cons (purpose, value, chain);
6750 TREE_PARMLIST (result) = 1;
6751 }
6752 else
6753 result = hash_tree_cons (purpose, value, chain);
8d08fdba
MS
6754 return result;
6755 }
6756 case TREE_VEC:
5566b478
MS
6757 if (type != NULL_TREE)
6758 {
1ceaad38
JM
6759 /* A binfo node. We always need to make a copy, of the node
6760 itself and of its BINFO_BASETYPES. */
85b71cf2 6761
5566b478
MS
6762 t = copy_node (t);
6763
c7449f2b
JM
6764 /* Make sure type isn't a typedef copy. */
6765 type = BINFO_TYPE (TYPE_BINFO (type));
6766
5566b478 6767 TREE_TYPE (t) = complete_type (type);
6633d636
MS
6768 if (IS_AGGR_TYPE (type))
6769 {
6770 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6771 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6772 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6773 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6774 }
5566b478
MS
6775 return t;
6776 }
85b71cf2
JM
6777
6778 /* Otherwise, a vector of template arguments. */
4393e105 6779 return tsubst_template_arg_vector (t, args, complain);
8d08fdba 6780
8d08fdba
MS
6781 case POINTER_TYPE:
6782 case REFERENCE_TYPE:
6783 {
8d08fdba 6784 enum tree_code code;
79a7c7fa 6785
46c895ac 6786 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8d08fdba
MS
6787 return t;
6788
6789 code = TREE_CODE (t);
4393e105
MM
6790
6791
6792 /* [temp.deduct]
6793
6794 Type deduction may fail for any of the following
6795 reasons:
6796
6797 -- Attempting to create a pointer to reference type.
6798 -- Attempting to create a reference to a reference type or
6799 a reference to void. */
0ecfe0b4
JM
6800 if (TREE_CODE (type) == REFERENCE_TYPE
6801 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa
JM
6802 {
6803 static int last_line = 0;
3b304f5b 6804 static const char* last_file = 0;
79a7c7fa
JM
6805
6806 /* We keep track of the last time we issued this error
6807 message to avoid spewing a ton of messages during a
6808 single bad template instantiation. */
c2ea3a40
NS
6809 if (complain & tf_error
6810 && (last_line != lineno || last_file != input_filename))
79a7c7fa 6811 {
0ecfe0b4 6812 if (TREE_CODE (type) == VOID_TYPE)
33bd39a2 6813 error ("forming reference to void");
0ecfe0b4 6814 else
33bd39a2 6815 error ("forming %s to reference type `%T'",
0ecfe0b4
JM
6816 (code == POINTER_TYPE) ? "pointer" : "reference",
6817 type);
79a7c7fa
JM
6818 last_line = lineno;
6819 last_file = input_filename;
6820 }
6821
4393e105 6822 return error_mark_node;
79a7c7fa
JM
6823 }
6824 else if (code == POINTER_TYPE)
46c895ac
NS
6825 {
6826 r = build_pointer_type (type);
6827 if (TREE_CODE (type) == METHOD_TYPE)
6828 r = build_ptrmemfunc_type (r);
6829 }
8d08fdba
MS
6830 else
6831 r = build_reference_type (type);
adecb3f4 6832 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
79a7c7fa 6833
a5f1c5f8
NS
6834 if (r != error_mark_node)
6835 /* Will this ever be needed for TYPE_..._TO values? */
6836 layout_type (r);
6837
8d08fdba
MS
6838 return r;
6839 }
a4443a08 6840 case OFFSET_TYPE:
0ecfe0b4 6841 {
4393e105
MM
6842 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6843 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6844 {
6845 /* [temp.deduct]
6846
6847 Type deduction may fail for any of the following
6848 reasons:
6849
6850 -- Attempting to create "pointer to member of T" when T
6851 is not a class type. */
c2ea3a40
NS
6852 if (complain & tf_error)
6853 error ("creating pointer to member of non-class type `%T'", r);
4393e105
MM
6854 return error_mark_node;
6855 }
46c895ac
NS
6856 if (TREE_CODE (type) == REFERENCE_TYPE)
6857 {
4f09be91 6858 if (complain & tf_error)
46c895ac
NS
6859 error ("creating pointer to member reference type `%T'", type);
6860
6861 return error_mark_node;
6862 }
6863 my_friendly_assert (TREE_CODE (type) != METHOD_TYPE, 20011231);
6864 if (TREE_CODE (type) == FUNCTION_TYPE)
6865 /* This is really a method type. The cv qualifiers of the
6866 this pointer should _not_ be determined by the cv
6867 qualifiers of the class type. They should be held
6868 somewhere in the FUNCTION_TYPE, but we don't do that at
6869 the moment. Consider
6870 typedef void (Func) () const;
6871
6872 template <typename T1> void Foo (Func T1::*);
6873
6874 */
6875 return build_cplus_method_type (TYPE_MAIN_VARIANT (r),
6876 TREE_TYPE (type),
6877 TYPE_ARG_TYPES (type));
6878 else
6879 return build_offset_type (r, type);
0ecfe0b4 6880 }
8d08fdba
MS
6881 case FUNCTION_TYPE:
6882 case METHOD_TYPE:
6883 {
c11b6f21 6884 tree fntype;
4393e105 6885 tree raises;
8d08fdba 6886
4393e105
MM
6887 fntype = tsubst_function_type (t, args, complain, in_decl);
6888 if (fntype == error_mark_node)
6889 return error_mark_node;
cabc336a 6890
c6002625 6891 /* Substitue the exception specification. */
cabc336a 6892 raises = TYPE_RAISES_EXCEPTIONS (t);
c11b6f21
MS
6893 if (raises)
6894 {
4cc1d462
NS
6895 tree list = NULL_TREE;
6896
6897 if (! TREE_VALUE (raises))
6898 list = raises;
6899 else
6900 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6901 {
6902 tree spec = TREE_VALUE (raises);
6903
6904 spec = tsubst (spec, args, complain, in_decl);
6905 if (spec == error_mark_node)
6906 return spec;
6907 list = add_exception_specifier (list, spec, complain);
6908 }
6909 fntype = build_exception_variant (fntype, list);
c11b6f21
MS
6910 }
6911 return fntype;
8d08fdba
MS
6912 }
6913 case ARRAY_TYPE:
6914 {
4393e105
MM
6915 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6916 if (domain == error_mark_node)
6917 return error_mark_node;
6918
6919 /* As an optimization, we avoid regenerating the array type if
6920 it will obviously be the same as T. */
8d08fdba
MS
6921 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6922 return t;
0ecfe0b4 6923
4393e105
MM
6924 /* These checks should match the ones in grokdeclarator.
6925
6926 [temp.deduct]
6927
6928 The deduction may fail for any of the following reasons:
6929
6930 -- Attempting to create an array with an element type that
6931 is void, a function type, or a reference type. */
6932 if (TREE_CODE (type) == VOID_TYPE
6933 || TREE_CODE (type) == FUNCTION_TYPE
6934 || TREE_CODE (type) == REFERENCE_TYPE)
0ecfe0b4 6935 {
c2ea3a40 6936 if (complain & tf_error)
33bd39a2 6937 error ("creating array of `%T'", type);
4393e105 6938 return error_mark_node;
0ecfe0b4
JM
6939 }
6940
8d08fdba
MS
6941 r = build_cplus_array_type (type, domain);
6942 return r;
6943 }
6944
8d08fdba 6945 case PLUS_EXPR:
5566b478 6946 case MINUS_EXPR:
4393e105 6947 {
c2ea3a40
NS
6948 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
6949 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
4393e105
MM
6950
6951 if (e1 == error_mark_node || e2 == error_mark_node)
6952 return error_mark_node;
6953
6954 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6955 }
8d08fdba
MS
6956
6957 case NEGATE_EXPR:
6958 case NOP_EXPR:
4393e105 6959 {
c2ea3a40 6960 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
6961 if (e == error_mark_node)
6962 return error_mark_node;
6963
6964 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6965 }
8d08fdba 6966
5566b478
MS
6967 case TYPENAME_TYPE:
6968 {
4393e105
MM
6969 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6970 in_decl, /*entering_scope=*/1);
6971 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6972 complain, in_decl);
6973
6974 if (ctx == error_mark_node || f == error_mark_node)
6975 return error_mark_node;
ae58fa02 6976
bf8f3f93
MM
6977 if (!IS_AGGR_TYPE (ctx))
6978 {
c2ea3a40 6979 if (complain & tf_error)
33bd39a2 6980 error ("`%T' is not a class, struct, or union type",
bf8f3f93
MM
6981 ctx);
6982 return error_mark_node;
6983 }
6984 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6985 {
6986 /* Normally, make_typename_type does not require that the CTX
6987 have complete type in order to allow things like:
ae58fa02 6988
bf8f3f93 6989 template <class T> struct S { typename S<T>::X Y; };
ae58fa02 6990
bf8f3f93
MM
6991 But, such constructs have already been resolved by this
6992 point, so here CTX really should have complete type, unless
6993 it's a partial instantiation. */
4393e105 6994 ctx = complete_type (ctx);
d0f062fb 6995 if (!COMPLETE_TYPE_P (ctx))
4393e105 6996 {
c2ea3a40 6997 if (complain & tf_error)
7a228918 6998 cxx_incomplete_type_error (NULL_TREE, ctx);
4393e105
MM
6999 return error_mark_node;
7000 }
7001 }
ae58fa02 7002
4f2b0fb2
NS
7003 f = make_typename_type (ctx, f,
7004 (complain & tf_error) | tf_keep_type_decl);
f0bcd168
MM
7005 if (f == error_mark_node)
7006 return f;
4f2b0fb2
NS
7007 if (TREE_CODE (f) == TYPE_DECL)
7008 {
7009 complain |= tf_ignore_bad_quals;
7010 f = TREE_TYPE (f);
7011 }
7012
7013 return cp_build_qualified_type_real
7014 (f, cp_type_quals (f) | cp_type_quals (t), complain);
5566b478 7015 }
c2ea3a40 7016
b8c6534b
KL
7017 case UNBOUND_CLASS_TEMPLATE:
7018 {
7019 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7020 in_decl, /*entering_scope=*/1);
7021 tree name = TYPE_IDENTIFIER (t);
7022
7023 if (ctx == error_mark_node || name == error_mark_node)
7024 return error_mark_node;
7025
7026 return make_unbound_class_template (ctx, name, complain);
7027 }
7028
5566b478 7029 case INDIRECT_REF:
4393e105 7030 {
c2ea3a40 7031 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
7032 if (e == error_mark_node)
7033 return error_mark_node;
7034 return make_pointer_declarator (type, e);
7035 }
7036
5566b478 7037 case ADDR_EXPR:
4393e105 7038 {
c2ea3a40 7039 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
7040 if (e == error_mark_node)
7041 return error_mark_node;
7042 return make_reference_declarator (type, e);
7043 }
5566b478
MS
7044
7045 case ARRAY_REF:
4393e105 7046 {
c2ea3a40
NS
7047 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7048 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl);
4393e105
MM
7049 if (e1 == error_mark_node || e2 == error_mark_node)
7050 return error_mark_node;
7051
718b8ea5 7052 return build_nt (ARRAY_REF, e1, e2, tsubst_expr);
4393e105 7053 }
5566b478
MS
7054
7055 case CALL_EXPR:
4393e105 7056 {
c2ea3a40 7057 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
43f887f9
MM
7058 tree e2 = (tsubst_call_declarator_parms
7059 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
7060 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
7061 complain, in_decl);
4393e105
MM
7062
7063 if (e1 == error_mark_node || e2 == error_mark_node
7064 || e3 == error_mark_node)
7065 return error_mark_node;
7066
43f887f9 7067 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
4393e105 7068 }
5566b478 7069
fc378698 7070 case SCOPE_REF:
4393e105 7071 {
c2ea3a40 7072 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
7073 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7074 if (e1 == error_mark_node || e2 == error_mark_node)
7075 return error_mark_node;
7076
718b8ea5 7077 return build_nt (TREE_CODE (t), e1, e2);
4393e105 7078 }
fc378698 7079
b894fc05 7080 case TYPEOF_TYPE:
4393e105 7081 {
c2ea3a40 7082 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain, in_decl);
4393e105
MM
7083 if (e1 == error_mark_node)
7084 return error_mark_node;
7085
7086 return TREE_TYPE (e1);
7087 }
b894fc05 7088
8d08fdba 7089 default:
5566b478 7090 sorry ("use of `%s' in template",
8d08fdba
MS
7091 tree_code_name [(int) TREE_CODE (t)]);
7092 return error_mark_node;
7093 }
7094}
7095
00d3396f
JM
7096/* Like tsubst, but deals with expressions. This function just replaces
7097 template parms; to finish processing the resultant expression, use
7098 tsubst_expr. */
7099
14d22dd6 7100static tree
4393e105 7101tsubst_copy (t, args, complain, in_decl)
98c1c668 7102 tree t, args;
c2ea3a40 7103 tsubst_flags_t complain;
5566b478
MS
7104 tree in_decl;
7105{
7106 enum tree_code code;
8452b1d3 7107 tree r;
8d08fdba 7108
5566b478
MS
7109 if (t == NULL_TREE || t == error_mark_node)
7110 return t;
7111
7112 code = TREE_CODE (t);
b7484fbe 7113
5566b478
MS
7114 switch (code)
7115 {
7116 case PARM_DECL:
a723baf1
MM
7117 r = retrieve_local_specialization (t);
7118 my_friendly_assert (r != NULL, 20020903);
7119 return r;
5566b478
MS
7120
7121 case CONST_DECL:
ed44da02
MM
7122 {
7123 tree enum_type;
7124 tree v;
7125
a723baf1
MM
7126 if (DECL_TEMPLATE_PARM_P (t))
7127 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
ed44da02
MM
7128 if (!DECL_CONTEXT (t))
7129 /* This is a global enumeration constant. */
7130 return t;
7131
7132 /* Unfortunately, we cannot just call lookup_name here.
9188c363
MM
7133 Consider:
7134
7135 template <int I> int f() {
7136 enum E { a = I };
7137 struct S { void g() { E e = a; } };
7138 };
7139
7140 When we instantiate f<7>::S::g(), say, lookup_name is not
7141 clever enough to find f<7>::a. */
ed44da02 7142 enum_type
4393e105 7143 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
ed44da02
MM
7144 /*entering_scope=*/0);
7145
7146 for (v = TYPE_VALUES (enum_type);
7147 v != NULL_TREE;
7148 v = TREE_CHAIN (v))
7149 if (TREE_PURPOSE (v) == DECL_NAME (t))
7150 return TREE_VALUE (v);
7151
7152 /* We didn't find the name. That should never happen; if
7153 name-lookup found it during preliminary parsing, we
7154 should find it again here during instantiation. */
a98facb0 7155 abort ();
ed44da02 7156 }
db04386f 7157 return t;
ed44da02 7158
5566b478
MS
7159 case FIELD_DECL:
7160 if (DECL_CONTEXT (t))
7161 {
0978790f 7162 tree ctx;
0978790f 7163
4393e105 7164 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
36a117a5 7165 /*entering_scope=*/1);
0978790f 7166 if (ctx != DECL_CONTEXT (t))
86ac0575 7167 return lookup_field (ctx, DECL_NAME (t), 0, false);
5566b478
MS
7168 }
7169 return t;
7170
7171 case VAR_DECL:
7172 case FUNCTION_DECL:
a723baf1
MM
7173 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
7174 || local_variable_p (t))
4393e105 7175 t = tsubst (t, args, complain, in_decl);
5566b478
MS
7176 mark_used (t);
7177 return t;
7178
a723baf1
MM
7179 case BASELINK:
7180 {
7181 tree name;
7182 tree qualifying_scope;
7183 tree fns;
dd1b7476 7184 tree template_args = 0;
a723baf1
MM
7185 bool template_id_p = false;
7186
7187 /* A baselink indicates a function from a base class. The
7188 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7189 non-dependent types; otherwise, the lookup could not have
7190 succeeded. However, they may indicate bases of the template
7191 class, rather than the instantiated class.
7192
7193 In addition, lookups that were not ambiguous before may be
7194 ambiguous now. Therefore, we perform the lookup again. */
7195 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (t));
7196 fns = BASELINK_FUNCTIONS (t);
7197 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7198 {
7199 template_id_p = true;
7200 template_args = TREE_OPERAND (fns, 1);
7201 fns = TREE_OPERAND (fns, 0);
7202 }
7203 name = DECL_NAME (get_first_fn (fns));
7204 t = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
7205 if (BASELINK_P (t) && template_id_p)
7206 BASELINK_FUNCTIONS (t)
7207 = build_nt (TEMPLATE_ID_EXPR,
7208 BASELINK_FUNCTIONS (t),
7209 template_args);
7210 return adjust_result_of_qualified_name_lookup (t,
7211 qualifying_scope,
7212 current_class_type);
7213 }
7214
98c1c668 7215 case TEMPLATE_DECL:
a723baf1
MM
7216 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
7217 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
7218 args, complain, in_decl);
7219 else if (is_member_template (t))
4393e105 7220 return tsubst (t, args, complain, in_decl);
98c1c668
JM
7221 else
7222 return t;
7223
672476cb
MM
7224 case LOOKUP_EXPR:
7225 {
9da99f7d 7226 /* We must tsubst into a LOOKUP_EXPR in case the names to
672476cb
MM
7227 which it refers is a conversion operator; in that case the
7228 name will change. We avoid making unnecessary copies,
7229 however. */
7230
4393e105 7231 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
672476cb
MM
7232
7233 if (id != TREE_OPERAND (t, 0))
7234 {
8452b1d3 7235 r = build_nt (LOOKUP_EXPR, id);
672476cb
MM
7236 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
7237 t = r;
7238 }
7239
7240 return t;
7241 }
7242
5566b478
MS
7243 case CAST_EXPR:
7244 case REINTERPRET_CAST_EXPR:
e92cc029
MS
7245 case CONST_CAST_EXPR:
7246 case STATIC_CAST_EXPR:
7247 case DYNAMIC_CAST_EXPR:
51924768 7248 case NOP_EXPR:
5566b478 7249 return build1
4393e105
MM
7250 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
7251 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478
MS
7252
7253 case INDIRECT_REF:
5566b478
MS
7254 case NEGATE_EXPR:
7255 case TRUTH_NOT_EXPR:
b87692e5 7256 case BIT_NOT_EXPR:
5566b478
MS
7257 case ADDR_EXPR:
7258 case CONVERT_EXPR: /* Unary + */
7259 case SIZEOF_EXPR:
abff8e06 7260 case ALIGNOF_EXPR:
5566b478 7261 case ARROW_EXPR:
fc378698 7262 case THROW_EXPR:
5156628f 7263 case TYPEID_EXPR:
f5733617
SS
7264 case REALPART_EXPR:
7265 case IMAGPART_EXPR:
5566b478 7266 return build1
6a629cac 7267 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
4393e105 7268 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478 7269
a723baf1
MM
7270 case COMPONENT_REF:
7271 {
7272 tree object;
7273 tree name;
7274
7275 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7276 name = TREE_OPERAND (t, 1);
7277 if (TREE_CODE (name) == BIT_NOT_EXPR)
7278 {
7279 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7280 complain, in_decl);
7281 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7282 }
7283 else if (TREE_CODE (name) == SCOPE_REF
7284 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7285 {
7286 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7287 complain, in_decl);
7288 name = TREE_OPERAND (name, 1);
7289 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7290 complain, in_decl);
7291 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
7292 name = build_nt (SCOPE_REF, base, name);
7293 }
7294 else
7295 name = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
7296 return build_nt (COMPONENT_REF, object, name);
7297 }
7298
5566b478
MS
7299 case PLUS_EXPR:
7300 case MINUS_EXPR:
7301 case MULT_EXPR:
7302 case TRUNC_DIV_EXPR:
7303 case CEIL_DIV_EXPR:
7304 case FLOOR_DIV_EXPR:
7305 case ROUND_DIV_EXPR:
7306 case EXACT_DIV_EXPR:
7307 case BIT_AND_EXPR:
7308 case BIT_ANDTC_EXPR:
7309 case BIT_IOR_EXPR:
7310 case BIT_XOR_EXPR:
7311 case TRUNC_MOD_EXPR:
7312 case FLOOR_MOD_EXPR:
7313 case TRUTH_ANDIF_EXPR:
7314 case TRUTH_ORIF_EXPR:
7315 case TRUTH_AND_EXPR:
7316 case TRUTH_OR_EXPR:
7317 case RSHIFT_EXPR:
7318 case LSHIFT_EXPR:
7319 case RROTATE_EXPR:
7320 case LROTATE_EXPR:
7321 case EQ_EXPR:
7322 case NE_EXPR:
7323 case MAX_EXPR:
7324 case MIN_EXPR:
7325 case LE_EXPR:
7326 case GE_EXPR:
7327 case LT_EXPR:
7328 case GT_EXPR:
5566b478
MS
7329 case ARRAY_REF:
7330 case COMPOUND_EXPR:
7331 case SCOPE_REF:
7332 case DOTSTAR_EXPR:
7333 case MEMBER_REF:
519c9806
MM
7334 case PREDECREMENT_EXPR:
7335 case PREINCREMENT_EXPR:
7336 case POSTDECREMENT_EXPR:
7337 case POSTINCREMENT_EXPR:
5566b478 7338 return build_nt
4393e105
MM
7339 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7340 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
7341
7342 case CALL_EXPR:
a723baf1
MM
7343 return build_nt (code,
7344 tsubst_copy (TREE_OPERAND (t, 0), args,
7345 complain, in_decl),
7346 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7347 in_decl),
7348 NULL_TREE);
5566b478
MS
7349
7350 case METHOD_CALL_EXPR:
a723baf1
MM
7351 return build_nt
7352 (code,
7353 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7354 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7355 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
7356 NULL_TREE);
5566b478 7357
558475f0 7358 case STMT_EXPR:
cc23546e 7359 /* This processing should really occur in tsubst_expr. However,
558475f0 7360 tsubst_expr does not recurse into expressions, since it
cc23546e
JO
7361 assumes that there aren't any statements inside them. So, we
7362 need to expand the STMT_EXPR here. */
558475f0
MM
7363 if (!processing_template_decl)
7364 {
b5ac18ea 7365 tree stmt_expr = begin_stmt_expr ();
c2ea3a40 7366 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
b5ac18ea 7367 return finish_stmt_expr (stmt_expr);
558475f0 7368 }
9d9169de
MM
7369
7370 return t;
558475f0 7371
5566b478
MS
7372 case COND_EXPR:
7373 case MODOP_EXPR:
40242ccf 7374 case PSEUDO_DTOR_EXPR:
67da3287 7375 {
8452b1d3 7376 r = build_nt
4393e105
MM
7377 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7378 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7379 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
67da3287
MM
7380 return r;
7381 }
5566b478
MS
7382
7383 case NEW_EXPR:
7384 {
8452b1d3 7385 r = build_nt
4393e105
MM
7386 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7387 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7388 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
5566b478
MS
7389 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7390 return r;
7391 }
7392
7393 case DELETE_EXPR:
7394 {
8452b1d3 7395 r = build_nt
4393e105
MM
7396 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7397 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
7398 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7399 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7400 return r;
7401 }
7402
386b8a85
JM
7403 case TEMPLATE_ID_EXPR:
7404 {
00d3396f 7405 /* Substituted template arguments */
4393e105
MM
7406 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7407 in_decl);
856216bb
MM
7408
7409 if (targs && TREE_CODE (targs) == TREE_LIST)
7410 {
7411 tree chain;
7412 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7413 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7414 }
7415 else if (targs)
7416 {
7417 int i;
7418 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7419 TREE_VEC_ELT (targs, i)
7420 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7421 }
00d3396f
JM
7422
7423 return lookup_template_function
4393e105 7424 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
386b8a85
JM
7425 }
7426
5566b478
MS
7427 case TREE_LIST:
7428 {
7429 tree purpose, value, chain;
7430
7431 if (t == void_list_node)
7432 return t;
7433
7434 purpose = TREE_PURPOSE (t);
7435 if (purpose)
4393e105 7436 purpose = tsubst_copy (purpose, args, complain, in_decl);
5566b478
MS
7437 value = TREE_VALUE (t);
7438 if (value)
4393e105 7439 value = tsubst_copy (value, args, complain, in_decl);
5566b478
MS
7440 chain = TREE_CHAIN (t);
7441 if (chain && chain != void_type_node)
4393e105 7442 chain = tsubst_copy (chain, args, complain, in_decl);
5566b478
MS
7443 if (purpose == TREE_PURPOSE (t)
7444 && value == TREE_VALUE (t)
7445 && chain == TREE_CHAIN (t))
7446 return t;
7447 return tree_cons (purpose, value, chain);
7448 }
7449
7450 case RECORD_TYPE:
7451 case UNION_TYPE:
7452 case ENUMERAL_TYPE:
7453 case INTEGER_TYPE:
7454 case TEMPLATE_TYPE_PARM:
73b0fce8 7455 case TEMPLATE_TEMPLATE_PARM:
a1281f45 7456 case BOUND_TEMPLATE_TEMPLATE_PARM:
f84b4be9 7457 case TEMPLATE_PARM_INDEX:
5566b478
MS
7458 case POINTER_TYPE:
7459 case REFERENCE_TYPE:
7460 case OFFSET_TYPE:
7461 case FUNCTION_TYPE:
7462 case METHOD_TYPE:
7463 case ARRAY_TYPE:
7464 case TYPENAME_TYPE:
b8c6534b 7465 case UNBOUND_CLASS_TEMPLATE:
2a2b1d56 7466 case TYPEOF_TYPE:
f84b4be9 7467 case TYPE_DECL:
4393e105 7468 return tsubst (t, args, complain, in_decl);
5566b478 7469
e92cc029 7470 case IDENTIFIER_NODE:
421844e7 7471 if (IDENTIFIER_TYPENAME_P (t))
1f6e1acc
AS
7472 {
7473 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
1f84ec23 7474 return mangle_conv_op_name_for_type (new_type);
1f6e1acc 7475 }
e92cc029
MS
7476 else
7477 return t;
7478
5156628f 7479 case CONSTRUCTOR:
8452b1d3
JM
7480 {
7481 r = build
4393e105
MM
7482 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7483 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7484 complain, in_decl));
8452b1d3
JM
7485 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7486 return r;
7487 }
5156628f 7488
371534a9 7489 case VA_ARG_EXPR:
ea333e1c 7490 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
c2ea3a40
NS
7491 in_decl),
7492 tsubst (TREE_TYPE (t), args, complain, in_decl));
f9817201 7493
5566b478
MS
7494 default:
7495 return t;
7496 }
7497}
7498
cc23546e
JO
7499/* Like tsubst_copy for expressions, etc. but also does semantic
7500 processing. */
00d3396f 7501
14d22dd6 7502static tree
4393e105 7503tsubst_expr (t, args, complain, in_decl)
98c1c668 7504 tree t, args;
c2ea3a40 7505 tsubst_flags_t complain;
5566b478
MS
7506 tree in_decl;
7507{
fd10dd09 7508 tree stmt, tmp;
558475f0 7509
5566b478
MS
7510 if (t == NULL_TREE || t == error_mark_node)
7511 return t;
7512
5156628f 7513 if (processing_template_decl)
4393e105 7514 return tsubst_copy (t, args, complain, in_decl);
5566b478 7515
fd10dd09 7516 if (!statement_code_p (TREE_CODE (t)))
cc23546e 7517 return tsubst_copy_and_build (t, args, complain, in_decl);
fd10dd09 7518
5566b478 7519 switch (TREE_CODE (t))
8d08fdba 7520 {
558475f0 7521 case CTOR_INITIALIZER:
2282d28d
MM
7522 prep_stmt (t);
7523 finish_mem_initializers (tsubst_initializer_list
7524 (TREE_OPERAND (t, 0), args));
7525 break;
558475f0 7526
5566b478 7527 case RETURN_STMT:
a7e4cfa0 7528 prep_stmt (t);
d65b1d77 7529 finish_return_stmt (tsubst_expr (RETURN_STMT_EXPR (t),
4393e105 7530 args, complain, in_decl));
5566b478
MS
7531 break;
7532
7533 case EXPR_STMT:
a7e4cfa0 7534 prep_stmt (t);
ad321293 7535 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
4393e105 7536 args, complain, in_decl));
5566b478
MS
7537 break;
7538
9da99f7d
NS
7539 case USING_STMT:
7540 prep_stmt (t);
7541 do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
7542 args, complain, in_decl));
7543 break;
7544
5566b478
MS
7545 case DECL_STMT:
7546 {
e0942dcd
MM
7547 tree decl;
7548 tree init;
5566b478 7549
a7e4cfa0 7550 prep_stmt (t);
e0942dcd 7551 decl = DECL_STMT_DECL (t);
acef433b
MM
7552 if (TREE_CODE (decl) == LABEL_DECL)
7553 finish_label_decl (DECL_NAME (decl));
fbfe8c9e
NS
7554 else if (TREE_CODE (decl) == USING_DECL)
7555 {
7556 tree scope = DECL_INITIAL (decl);
7557 tree name = DECL_NAME (decl);
7558
7559 scope = tsubst_expr (scope, args, complain, in_decl);
a723baf1
MM
7560 do_local_using_decl (lookup_qualified_name (scope,
7561 name,
7562 /*is_type_p=*/0,
7563 /*flags=*/0));
fbfe8c9e 7564 }
acef433b
MM
7565 else
7566 {
7567 init = DECL_INITIAL (decl);
7568 decl = tsubst (decl, args, complain, in_decl);
ce2e5191
NS
7569 if (decl != error_mark_node)
7570 {
7571 if (init)
7572 DECL_INITIAL (decl) = error_mark_node;
7573 /* By marking the declaration as instantiated, we avoid
7574 trying to instantiate it. Since instantiate_decl can't
7575 handle local variables, and since we've already done
7576 all that needs to be done, that's the right thing to
7577 do. */
7578 if (TREE_CODE (decl) == VAR_DECL)
7579 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
edebf865
MM
7580 if (TREE_CODE (decl) == VAR_DECL
7581 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
7582 /* Anonymous aggregates are a special case. */
7583 finish_anon_union (decl);
7584 else
ed2fa432 7585 {
edebf865
MM
7586 maybe_push_decl (decl);
7587 if (DECL_PRETTY_FUNCTION_P (decl))
7588 {
7589 /* For __PRETTY_FUNCTION__ we have to adjust the
7590 initializer. */
7591 const char *const name
7592 = cxx_printable_name (current_function_decl, 2);
7593 init = cp_fname_init (name);
7594 TREE_TYPE (decl) = TREE_TYPE (init);
7595 }
7596 else
7597 init = tsubst_expr (init, args, complain, in_decl);
7598 cp_finish_decl (decl, init, NULL_TREE, 0);
ed2fa432 7599 }
ce2e5191 7600 }
acef433b 7601 }
fd10dd09
JM
7602
7603 /* A DECL_STMT can also be used as an expression, in the condition
27631dae 7604 clause of an if/for/while construct. If we aren't followed by
fd10dd09
JM
7605 another statement, return our decl. */
7606 if (TREE_CHAIN (t) == NULL_TREE)
7607 return decl;
5566b478 7608 }
fd10dd09 7609 break;
8d08fdba 7610
5566b478
MS
7611 case FOR_STMT:
7612 {
a7e4cfa0 7613 prep_stmt (t);
5566b478 7614
558475f0 7615 stmt = begin_for_stmt ();
fd10dd09 7616 tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
558475f0 7617 finish_for_init_stmt (stmt);
c2ea3a40
NS
7618 finish_for_cond (tsubst_expr (FOR_COND (t),
7619 args, complain, in_decl),
558475f0 7620 stmt);
4393e105 7621 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
558475f0 7622 finish_for_expr (tmp, stmt);
4393e105 7623 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
35b1567d 7624 finish_for_stmt (stmt);
5566b478
MS
7625 }
7626 break;
8d08fdba 7627
5566b478
MS
7628 case WHILE_STMT:
7629 {
a7e4cfa0 7630 prep_stmt (t);
558475f0 7631 stmt = begin_while_stmt ();
ad321293 7632 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
4393e105 7633 args, complain, in_decl),
558475f0 7634 stmt);
4393e105 7635 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
558475f0 7636 finish_while_stmt (stmt);
5566b478
MS
7637 }
7638 break;
8d08fdba 7639
5566b478
MS
7640 case DO_STMT:
7641 {
a7e4cfa0 7642 prep_stmt (t);
558475f0 7643 stmt = begin_do_stmt ();
4393e105 7644 tsubst_expr (DO_BODY (t), args, complain, in_decl);
558475f0 7645 finish_do_body (stmt);
c2ea3a40
NS
7646 finish_do_stmt (tsubst_expr (DO_COND (t),
7647 args, complain, in_decl),
558475f0 7648 stmt);
5566b478
MS
7649 }
7650 break;
a0a33927 7651
5566b478 7652 case IF_STMT:
8d08fdba 7653 {
a7e4cfa0 7654 prep_stmt (t);
558475f0 7655 stmt = begin_if_stmt ();
ad321293 7656 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
4393e105 7657 args, complain, in_decl),
558475f0 7658 stmt);
db5ae43f 7659
ad321293 7660 if (tmp = THEN_CLAUSE (t), tmp)
db5ae43f 7661 {
4393e105 7662 tsubst_expr (tmp, args, complain, in_decl);
558475f0 7663 finish_then_clause (stmt);
db5ae43f
MS
7664 }
7665
ad321293
MM
7666 if (tmp = ELSE_CLAUSE (t), tmp)
7667 {
7668 begin_else_clause ();
4393e105 7669 tsubst_expr (tmp, args, complain, in_decl);
558475f0 7670 finish_else_clause (stmt);
ad321293 7671 }
8d08fdba 7672
ad321293 7673 finish_if_stmt ();
8d08fdba 7674 }
5566b478 7675 break;
8d08fdba 7676
5566b478
MS
7677 case COMPOUND_STMT:
7678 {
a7e4cfa0 7679 prep_stmt (t);
ade3dc07
JM
7680 if (COMPOUND_STMT_BODY_BLOCK (t))
7681 stmt = begin_function_body ();
7682 else
7683 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
7684
fd10dd09 7685 tsubst_expr (COMPOUND_BODY (t), args, complain, in_decl);
ade3dc07
JM
7686
7687 if (COMPOUND_STMT_BODY_BLOCK (t))
7688 finish_function_body (stmt);
7689 else
7690 finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
5566b478
MS
7691 }
7692 break;
8d08fdba 7693
5566b478 7694 case BREAK_STMT:
a7e4cfa0 7695 prep_stmt (t);
ad321293 7696 finish_break_stmt ();
5566b478 7697 break;
8d08fdba 7698
6467930b 7699 case CONTINUE_STMT:
a7e4cfa0 7700 prep_stmt (t);
ad321293 7701 finish_continue_stmt ();
6467930b
MS
7702 break;
7703
5566b478
MS
7704 case SWITCH_STMT:
7705 {
558475f0 7706 tree val;
5566b478 7707
a7e4cfa0 7708 prep_stmt (t);
527f0080 7709 stmt = begin_switch_stmt ();
4393e105 7710 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
527f0080 7711 finish_switch_cond (val, stmt);
558475f0 7712 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
35b1567d 7713 finish_switch_stmt (stmt);
5566b478
MS
7714 }
7715 break;
7716
7717 case CASE_LABEL:
a7e4cfa0 7718 prep_stmt (t);
4393e105 7719 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
56cb9733
MM
7720 tsubst_expr (CASE_HIGH (t), args, complain,
7721 in_decl));
5566b478
MS
7722 break;
7723
acef433b
MM
7724 case LABEL_STMT:
7725 lineno = STMT_LINENO (t);
7726 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
5566b478
MS
7727 break;
7728
aa16c0fa
KL
7729 case FILE_STMT:
7730 input_filename = FILE_STMT_FILENAME (t);
7731 add_stmt (build_nt (FILE_STMT, FILE_STMT_FILENAME_NODE (t)));
7732 break;
7733
5566b478 7734 case GOTO_STMT:
a7e4cfa0 7735 prep_stmt (t);
fd10dd09
JM
7736 tmp = GOTO_DESTINATION (t);
7737 if (TREE_CODE (tmp) != LABEL_DECL)
aa09da44
MM
7738 /* Computed goto's must be tsubst'd into. On the other hand,
7739 non-computed gotos must not be; the identifier in question
7740 will have no binding. */
fd10dd09 7741 tmp = tsubst_expr (tmp, args, complain, in_decl);
3fa56191 7742 else
fd10dd09
JM
7743 tmp = DECL_NAME (tmp);
7744 finish_goto_stmt (tmp);
ad321293
MM
7745 break;
7746
7747 case ASM_STMT:
a7e4cfa0 7748 prep_stmt (t);
c87978aa
JM
7749 tmp = finish_asm_stmt
7750 (ASM_CV_QUAL (t),
7751 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7752 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7753 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7754 tsubst_expr (ASM_CLOBBERS (t), args, complain, in_decl));
7755 ASM_INPUT_P (tmp) = ASM_INPUT_P (t);
5566b478 7756 break;
faf5394a
MS
7757
7758 case TRY_BLOCK:
a7e4cfa0 7759 prep_stmt (t);
f1dedc31 7760 if (CLEANUP_P (t))
62409b39 7761 {
57b52417 7762 stmt = begin_try_block ();
62409b39
MM
7763 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7764 finish_cleanup_try_block (stmt);
7765 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7766 complain, in_decl),
7767 stmt);
7768 }
f1dedc31
MM
7769 else
7770 {
62409b39
MM
7771 if (FN_TRY_BLOCK_P (t))
7772 stmt = begin_function_try_block ();
7773 else
7774 stmt = begin_try_block ();
7775
7776 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
7777
7778 if (FN_TRY_BLOCK_P (t))
7779 finish_function_try_block (stmt);
7780 else
7781 finish_try_block (stmt);
7782
fd10dd09 7783 tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
84df082b
MM
7784 if (FN_TRY_BLOCK_P (t))
7785 finish_function_handler_sequence (stmt);
7786 else
7787 finish_handler_sequence (stmt);
f1dedc31 7788 }
faf5394a 7789 break;
b35d4555 7790
faf5394a 7791 case HANDLER:
b35d4555
MM
7792 {
7793 tree decl;
b35d4555
MM
7794
7795 prep_stmt (t);
7796 stmt = begin_handler ();
7797 if (HANDLER_PARMS (t))
7798 {
7799 decl = DECL_STMT_DECL (HANDLER_PARMS (t));
7800 decl = tsubst (decl, args, complain, in_decl);
f8191e64
MM
7801 /* Prevent instantiate_decl from trying to instantiate
7802 this variable. We've already done all that needs to be
7803 done. */
7804 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
b35d4555
MM
7805 }
7806 else
7807 decl = NULL_TREE;
1a6025b4 7808 finish_handler_parms (decl, stmt);
b35d4555 7809 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
1a6025b4 7810 finish_handler (stmt);
b35d4555 7811 }
faf5394a
MS
7812 break;
7813
b87692e5 7814 case TAG_DEFN:
a7e4cfa0 7815 prep_stmt (t);
fd10dd09 7816 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
b87692e5
MS
7817 break;
7818
5566b478 7819 default:
fd10dd09 7820 abort ();
5566b478 7821 }
fd10dd09
JM
7822
7823 return tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
8d08fdba
MS
7824}
7825
cc23546e
JO
7826/* Like tsubst but deals with expressions and performs semantic
7827 analysis. */
7828
14d22dd6 7829static tree
cc23546e
JO
7830tsubst_copy_and_build (t, args, complain, in_decl)
7831 tree t, args;
7832 tsubst_flags_t complain;
7833 tree in_decl;
7834{
7835 if (t == NULL_TREE || t == error_mark_node)
7836 return t;
7837
7838 switch (TREE_CODE (t))
7839 {
7840 case IDENTIFIER_NODE:
7841 if (IDENTIFIER_TYPENAME_P (t))
7842 {
7843 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7844 return do_identifier (mangle_conv_op_name_for_type (new_type),
7845 NULL_TREE);
7846 }
7847 else
7848 return do_identifier (t, NULL_TREE);
7849
7850 case LOOKUP_EXPR:
7851 {
7852 if (LOOKUP_EXPR_GLOBAL (t))
7853 {
7854 tree token
7855 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7856 return do_scoped_id (token, IDENTIFIER_GLOBAL_VALUE (token));
7857 }
7858 else
7859 {
7860 t = do_identifier
7861 (tsubst_copy
7862 (TREE_OPERAND (t, 0), args, complain, in_decl),
7863 NULL_TREE);
7864 if (TREE_CODE (t) == ALIAS_DECL)
7865 t = DECL_INITIAL (t);
7866 return t;
7867 }
7868 }
7869
7870 case TEMPLATE_ID_EXPR:
7871 {
7872 tree object;
7873 tree template
7874 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
7875
7876 if (TREE_CODE (template) == COMPONENT_REF)
7877 {
7878 object = TREE_OPERAND (template, 0);
7879 template = TREE_OPERAND (template, 1);
7880 }
7881 else
7882 object = NULL_TREE;
7883
7884 template = lookup_template_function
7885 (template,
7886 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
7887
7888 if (object)
7889 return build (COMPONENT_REF, TREE_TYPE (template),
7890 object, template);
7891 else
7892 return template;
7893 }
7894
7895 case INDIRECT_REF:
7896 return build_x_indirect_ref
7897 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
7898 "unary *");
7899
7900 case CAST_EXPR:
7901 return build_functional_cast
7902 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7903 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7904
7905 case REINTERPRET_CAST_EXPR:
7906 return build_reinterpret_cast
7907 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7908 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7909
7910 case CONST_CAST_EXPR:
7911 return build_const_cast
7912 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7913 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7914
7915 case DYNAMIC_CAST_EXPR:
7916 return build_dynamic_cast
7917 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7918 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7919
7920 case STATIC_CAST_EXPR:
7921 return build_static_cast
7922 (tsubst (TREE_TYPE (t), args, complain, in_decl),
7923 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7924
7925 case PREDECREMENT_EXPR:
7926 case PREINCREMENT_EXPR:
7927 case POSTDECREMENT_EXPR:
7928 case POSTINCREMENT_EXPR:
7929 if (TREE_TYPE (t))
7930 return tsubst_copy (t, args, complain, in_decl);
7931 else
7932 return build_x_unary_op
7933 (TREE_CODE (t),
7934 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7935 in_decl));
7936
7937 case NEGATE_EXPR:
7938 case BIT_NOT_EXPR:
7939 if (TREE_TYPE (t))
7940 return tsubst_copy (t, args, complain, in_decl);
7941 else
7942 return build_x_unary_op
7943 (TREE_CODE (t),
7944 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7945 in_decl));
7946
7947 case ABS_EXPR:
7948 if (TREE_TYPE (t))
7949 return t;
7950 return build_x_unary_op
7951 (TREE_CODE (t),
7952 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
7953
7954 case TRUTH_NOT_EXPR:
7955 case ADDR_EXPR:
7956 case CONVERT_EXPR: /* Unary + */
7957 case REALPART_EXPR:
7958 case IMAGPART_EXPR:
7959 if (TREE_TYPE (t))
7960 return tsubst_copy (t, args, complain, in_decl);
7961 else
7962 return build_x_unary_op
7963 (TREE_CODE (t),
7964 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
7965 in_decl));
7966
7967 case PLUS_EXPR:
7968 case MINUS_EXPR:
7969 case MULT_EXPR:
7970 case TRUNC_DIV_EXPR:
7971 case CEIL_DIV_EXPR:
7972 case FLOOR_DIV_EXPR:
7973 case ROUND_DIV_EXPR:
7974 case EXACT_DIV_EXPR:
7975 case BIT_AND_EXPR:
7976 case BIT_ANDTC_EXPR:
7977 case BIT_IOR_EXPR:
7978 case BIT_XOR_EXPR:
7979 case TRUNC_MOD_EXPR:
7980 case FLOOR_MOD_EXPR:
7981 case TRUTH_ANDIF_EXPR:
7982 case TRUTH_ORIF_EXPR:
7983 case TRUTH_AND_EXPR:
7984 case TRUTH_OR_EXPR:
7985 case RSHIFT_EXPR:
7986 case LSHIFT_EXPR:
7987 case RROTATE_EXPR:
7988 case LROTATE_EXPR:
7989 case EQ_EXPR:
7990 case NE_EXPR:
7991 case MAX_EXPR:
7992 case MIN_EXPR:
7993 case LE_EXPR:
7994 case GE_EXPR:
7995 case LT_EXPR:
7996 case GT_EXPR:
7997 case MEMBER_REF:
7998 return build_x_binary_op
7999 (TREE_CODE (t),
8000 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8001 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8002
8003 case DOTSTAR_EXPR:
8004 return build_m_component_ref
8005 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8006 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl));
8007
8008 case SCOPE_REF:
8009 return build_offset_ref
8010 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8011 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
8012
8013 case ARRAY_REF:
8014 {
8015 if (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl)
8016 == NULL_TREE)
8017 /* new-type-id */
8018 return build_nt
8019 (ARRAY_REF, NULL_TREE,
8020 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8021 in_decl));
8022
8023 return grok_array_decl
8024 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8025 in_decl),
8026 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
8027 in_decl));
8028 }
8029
8030 case SIZEOF_EXPR:
8031 case ALIGNOF_EXPR:
8032 {
8033 tree r =
8034 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8035 if (!TYPE_P (r))
8036 return TREE_CODE (t) == SIZEOF_EXPR ?
8037 expr_sizeof (r) : c_alignof_expr (r);
8038 else
8039 return cxx_sizeof_or_alignof_type (r, TREE_CODE (t), true);
8040 }
8041
8042 case MODOP_EXPR:
8043 return build_x_modify_expr
8044 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8045 TREE_CODE (TREE_OPERAND (t, 1)),
8046 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8047
8048 case ARROW_EXPR:
8049 return build_x_arrow
8050 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8051
8052 case NEW_EXPR:
8053 return build_new
8054 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8055 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8056 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl),
8057 NEW_EXPR_USE_GLOBAL (t));
8058
8059 case DELETE_EXPR:
8060 return delete_sanity
8061 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8062 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8063 DELETE_EXPR_USE_VEC (t),
8064 DELETE_EXPR_USE_GLOBAL (t));
8065
8066 case COMPOUND_EXPR:
8067 {
8068 if (tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl)
8069 == NULL_TREE)
8070 return build_x_compound_expr
8071 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8072 in_decl));
8073 else
8074 abort ();
8075 }
8076
8077 case METHOD_CALL_EXPR:
8078 {
8079 tree method
8080 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8081
8082 if (TREE_CODE (method) == SCOPE_REF)
8083 {
8084 tree name = TREE_OPERAND (method, 1);
8085
8086 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8087 name = build_nt (TEMPLATE_ID_EXPR,
8088 TREE_OPERAND (name, 0),
8089 TREE_OPERAND (name, 1));
8090
8091 return build_scoped_method_call
8092 (tsubst_copy_and_build
8093 (TREE_OPERAND (t, 1), args, complain, in_decl),
8094 TREE_OPERAND (method, 0),
8095 name,
8096 tsubst_copy_and_build
8097 (TREE_OPERAND (t, 2), args, complain, in_decl));
8098 }
8099 else
8100 {
8101 /* We can get a TEMPLATE_ID_EXPR here on code like:
8102
8103 x->f<2>();
8104
8105 so we must resolve that. However, we can also get things
8106 like a BIT_NOT_EXPR here, when referring to a destructor,
8107 and things like that are not correctly resolved by this
8108 function so just use it when we really need it. */
8109 if (TREE_CODE (method) == TEMPLATE_ID_EXPR)
8110 method = lookup_template_function
8111 (TREE_OPERAND (method, 0),
8112 TREE_OPERAND (method, 1));
8113
8114 return build_method_call
8115 (tsubst_copy_and_build
8116 (TREE_OPERAND (t, 1), args, complain, in_decl),
8117 method,
8118 tsubst_copy_and_build
8119 (TREE_OPERAND (t, 2), args, complain, in_decl),
8120 NULL_TREE, LOOKUP_NORMAL);
8121 }
8122 }
8123
8124 case CALL_EXPR:
8125 {
bbaab916
NS
8126 tree function, copy_args;
8127
8128 function = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8129 copy_args = tsubst_copy_and_build (TREE_OPERAND (t, 1), args,
8130 complain, in_decl);
8131
8132 if (BASELINK_P (function))
8133 return build_call_from_tree (function, copy_args, 1);
8134 else if (TREE_CODE (function) == SCOPE_REF)
cc23546e
JO
8135 {
8136 tree name = TREE_OPERAND (function, 1);
8137 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
8138 name = build_nt (TEMPLATE_ID_EXPR,
8139 TREE_OPERAND (name, 0),
8b6a662e 8140 build_expr_from_tree (TREE_OPERAND (name, 1)));
bbaab916
NS
8141
8142 function = resolve_scoped_fn_name (TREE_OPERAND (function, 0),
8143 name);
8144
8145 return build_call_from_tree (function, copy_args, 1);
cc23546e
JO
8146 }
8147 else
8148 {
8149 tree name = function;
8150 tree id;
bbaab916 8151
cc23546e
JO
8152 if (copy_args != NULL_TREE && TREE_CODE (name) == LOOKUP_EXPR
8153 && !LOOKUP_EXPR_GLOBAL (name)
8154 && (TREE_CODE ((id = TREE_OPERAND (name, 0)))
8155 == IDENTIFIER_NODE)
8156 && (!current_class_type
86ac0575 8157 || !lookup_member (current_class_type, id, 0, false)))
cc23546e
JO
8158 {
8159 /* Do Koenig lookup if there are no class members. */
8160 name = do_identifier (id, copy_args);
8161 }
8162 else if (TREE_CODE (name) == TEMPLATE_ID_EXPR
8163 || ! really_overloaded_fn (name))
8164 name = build_expr_from_tree (name);
8165
8166 if (TREE_CODE (name) == OFFSET_REF)
8167 return build_offset_ref_call_from_tree (name, copy_args);
8168 if (TREE_CODE (name) == COMPONENT_REF)
8169 return finish_object_call_expr (TREE_OPERAND (name, 1),
8170 TREE_OPERAND (name, 0),
8171 copy_args);
8172 name = convert_from_reference (name);
8173 return build_call_from_tree (name, copy_args,
8174 /*disallow_virtual=*/false);
8175 }
8176 }
8177
8178 case COND_EXPR:
8179 return build_x_conditional_expr
8180 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8181 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8182 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8183
8184 case PSEUDO_DTOR_EXPR:
8185 return finish_pseudo_destructor_expr
8186 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl),
8187 tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain, in_decl),
8188 tsubst_copy_and_build (TREE_OPERAND (t, 2), args, complain, in_decl));
8189
8190 case TREE_LIST:
8191 {
8192 tree purpose, value, chain;
8193
8194 if (t == void_list_node)
8195 return t;
8196
8197 purpose = TREE_PURPOSE (t);
8198 if (purpose)
8199 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
8200 value = TREE_VALUE (t);
8201 if (value)
8202 value = tsubst_copy_and_build (value, args, complain, in_decl);
8203 chain = TREE_CHAIN (t);
8204 if (chain && chain != void_type_node)
8205 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
8206 if (purpose == TREE_PURPOSE (t)
8207 && value == TREE_VALUE (t)
8208 && chain == TREE_CHAIN (t))
8209 return t;
8210 return tree_cons (purpose, value, chain);
8211 }
8212
8213 case COMPONENT_REF:
8214 {
8215 tree object =
8216 tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl);
8217 tree member =
8218 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
8219
8220 if (!CLASS_TYPE_P (TREE_TYPE (object)))
8221 {
8222 if (TREE_CODE (member) == BIT_NOT_EXPR)
8223 return finish_pseudo_destructor_expr (object,
8224 NULL_TREE,
8225 TREE_TYPE (object));
8226 else if (TREE_CODE (member) == SCOPE_REF
8227 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
8228 return finish_pseudo_destructor_expr (object,
8229 object,
8230 TREE_TYPE (object));
8231 }
8232 else if (TREE_CODE (member) == SCOPE_REF
8233 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
8234 {
8235 tree tmpl;
8236 tree args;
8237
8238 /* Lookup the template functions now that we know what the
8239 scope is. */
8240 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
8241 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
8242 member = lookup_qualified_name (TREE_OPERAND (member, 0),
8243 tmpl,
8244 /*is_type=*/0,
8245 /*flags=*/0);
8246 if (BASELINK_P (member))
8247 BASELINK_FUNCTIONS (member)
8248 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
8249 args);
8250 else
8251 {
8252 error ("`%D' is not a member of `%T'",
8253 tmpl, TREE_TYPE (object));
8254 return error_mark_node;
8255 }
8256 }
8257
8258 return finish_class_member_access_expr (object, member);
8259 }
8260
8261 case THROW_EXPR:
8262 return build_throw
8263 (tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain, in_decl));
8264
8265 case CONSTRUCTOR:
8266 {
8267 tree r;
8268 tree elts;
8269 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8270 bool purpose_p;
8271
8272 /* digest_init will do the wrong thing if we let it. */
8273 if (type && TYPE_PTRMEMFUNC_P (type))
8274 return t;
8275
8276 r = NULL_TREE;
8277 /* We do not want to process the purpose of aggregate
8278 initializers as they are identifier nodes which will be
8279 looked up by digest_init. */
8280 purpose_p = !(type && IS_AGGR_TYPE (type));
db77ef4c 8281 for (elts = CONSTRUCTOR_ELTS (t);
cc23546e
JO
8282 elts;
8283 elts = TREE_CHAIN (elts))
8284 {
8285 tree purpose = TREE_PURPOSE (elts);
8286 tree value = TREE_VALUE (elts);
8287
8288 if (purpose && purpose_p)
8289 purpose
8290 = tsubst_copy_and_build (purpose, args, complain, in_decl);
8291 value = tsubst_copy_and_build (value, args, complain, in_decl);
8292 r = tree_cons (purpose, value, r);
8293 }
8294
8295 r = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (r));
8296 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
8297
8298 if (type)
8299 return digest_init (type, r, 0);
8300 return r;
8301 }
8302
8303 case TYPEID_EXPR:
8304 {
8305 tree operand_0
8306 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
8307 in_decl);
8308
8309 if (TYPE_P (operand_0))
8310 return get_typeid (operand_0);
8311 return build_typeid (operand_0);
8312 }
8313
8314 case PARM_DECL:
8315 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8316
8317 case VAR_DECL:
8318 return convert_from_reference (tsubst_copy (t, args, complain, in_decl));
8319
8320 case VA_ARG_EXPR:
8321 return build_x_va_arg
8322 (tsubst_copy_and_build
8323 (TREE_OPERAND (t, 0), args, complain, in_decl),
8324 tsubst_copy (TREE_TYPE (t), args, complain, in_decl));
8325
8326 default:
8327 return tsubst_copy (t, args, complain, in_decl);
8328 }
8329}
8330
6ba89f8e 8331/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
8332 the template arguments in TARG_PTR. */
8333
5566b478
MS
8334tree
8335instantiate_template (tmpl, targ_ptr)
98c1c668 8336 tree tmpl, targ_ptr;
8d08fdba 8337{
5566b478 8338 tree fndecl;
36a117a5
MM
8339 tree gen_tmpl;
8340 tree spec;
5566b478 8341 int i, len;
36a117a5 8342 tree inner_args;
5566b478 8343
27fafc8d
JM
8344 if (tmpl == error_mark_node)
8345 return error_mark_node;
8346
386b8a85
JM
8347 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
8348
db9b2174
MM
8349 /* If this function is a clone, handle it specially. */
8350 if (DECL_CLONED_FUNCTION_P (tmpl))
3ad97789
NS
8351 {
8352 tree spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr);
8353 tree clone;
8354
c6002625 8355 /* Look for the clone. */
3ad97789
NS
8356 for (clone = TREE_CHAIN (spec);
8357 clone && DECL_CLONED_FUNCTION_P (clone);
8358 clone = TREE_CHAIN (clone))
8359 if (DECL_NAME (clone) == DECL_NAME (tmpl))
8360 return clone;
8361 /* We should always have found the clone by now. */
a98facb0 8362 abort ();
3ad97789
NS
8363 return NULL_TREE;
8364 }
8365
36a117a5
MM
8366 /* Check to see if we already have this specialization. */
8367 spec = retrieve_specialization (tmpl, targ_ptr);
8368 if (spec != NULL_TREE)
8369 return spec;
8370
f9a7ae04
MM
8371 gen_tmpl = most_general_template (tmpl);
8372 if (tmpl != gen_tmpl)
386b8a85 8373 {
36a117a5
MM
8374 /* The TMPL is a partial instantiation. To get a full set of
8375 arguments we must add the arguments used to perform the
8376 partial instantiation. */
8377 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
8378 targ_ptr);
36a117a5
MM
8379
8380 /* Check to see if we already have this specialization. */
8381 spec = retrieve_specialization (gen_tmpl, targ_ptr);
75650646
MM
8382 if (spec != NULL_TREE)
8383 return spec;
386b8a85
JM
8384 }
8385
36a117a5 8386 len = DECL_NTPARMS (gen_tmpl);
f9a7ae04 8387 inner_args = INNERMOST_TEMPLATE_ARGS (targ_ptr);
5566b478
MS
8388 i = len;
8389 while (i--)
8d08fdba 8390 {
36a117a5 8391 tree t = TREE_VEC_ELT (inner_args, i);
2f939d94 8392 if (TYPE_P (t))
5566b478
MS
8393 {
8394 tree nt = target_type (t);
ec255269 8395 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5566b478 8396 {
33bd39a2
ZW
8397 error ("type `%T' composed from a local class is not a valid template-argument", t);
8398 error (" trying to instantiate `%D'", gen_tmpl);
454fa7a7 8399 return error_mark_node;
5566b478
MS
8400 }
8401 }
8d08fdba
MS
8402 }
8403
5c74d5b0 8404 /* Make sure that we can see identifiers, and compute access
2b59fc25
KL
8405 correctly. The desired FUNCTION_DECL for FNDECL may or may not be
8406 created earlier. Let push_access_scope_real figure that out. */
8407 push_access_scope_real
8408 (gen_tmpl, targ_ptr, tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
8409 tf_error, gen_tmpl));
5c74d5b0 8410
5566b478 8411 /* substitute template parameters */
17aec3eb 8412 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
c2ea3a40 8413 targ_ptr, tf_error, gen_tmpl);
5c74d5b0 8414
2b59fc25 8415 pop_access_scope (gen_tmpl);
5c74d5b0 8416
36a117a5
MM
8417 /* The DECL_TI_TEMPLATE should always be the immediate parent
8418 template, not the most general template. */
8419 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 8420
824b9a4c
MS
8421 if (flag_external_templates)
8422 add_pending_template (fndecl);
8423
94350948 8424 /* If we've just instantiated the main entry point for a function,
3ad97789
NS
8425 instantiate all the alternate entry points as well. We do this
8426 by cloning the instantiation of the main entry point, not by
8427 instantiating the template clones. */
8428 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
8429 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
94350948 8430
5566b478 8431 return fndecl;
8d08fdba 8432}
5566b478 8433
4393e105
MM
8434/* The FN is a TEMPLATE_DECL for a function. The ARGS are the
8435 arguments that are being used when calling it. TARGS is a vector
8436 into which the deduced template arguments are placed.
8d08fdba
MS
8437
8438 Return zero for success, 2 for an incomplete match that doesn't resolve
8439 all the types, and 1 for complete failure. An error message will be
8440 printed only for an incomplete match.
8441
e5214479
JM
8442 If FN is a conversion operator, or we are trying to produce a specific
8443 specialization, RETURN_TYPE is the return type desired.
9f54c803
MM
8444
8445 The EXPLICIT_TARGS are explicit template arguments provided via a
8446 template-id.
6467930b 8447
830bfa74
MM
8448 The parameter STRICT is one of:
8449
8450 DEDUCE_CALL:
8451 We are deducing arguments for a function call, as in
8452 [temp.deduct.call].
8453
8454 DEDUCE_CONV:
8455 We are deducing arguments for a conversion function, as in
8456 [temp.deduct.conv].
8457
8458 DEDUCE_EXACT:
62e4a758
NS
8459 We are deducing arguments when doing an explicit instantiation
8460 as in [temp.explicit], when determining an explicit specialization
8461 as in [temp.expl.spec], or when taking the address of a function
8462 template, as in [temp.deduct.funcaddr].
8463
8464 DEDUCE_ORDER:
830bfa74
MM
8465 We are deducing arguments when calculating the partial
8466 ordering between specializations of function or class
62e4a758 8467 templates, as in [temp.func.order] and [temp.class.order].
4393e105 8468
e5214479
JM
8469 LEN is the number of parms to consider before returning success, or -1
8470 for all. This is used in partial ordering to avoid comparing parms for
8471 which no actual argument was passed, since they are not considered in
8472 overload resolution (and are explicitly excluded from consideration in
8473 partial ordering in [temp.func.order]/6). */
8d08fdba
MS
8474
8475int
4393e105 8476fn_type_unification (fn, explicit_targs, targs, args, return_type,
e5214479 8477 strict, len)
4393e105 8478 tree fn, explicit_targs, targs, args, return_type;
830bfa74 8479 unification_kind_t strict;
e5214479 8480 int len;
386b8a85 8481{
4393e105
MM
8482 tree parms;
8483 tree fntype;
adecb3f4 8484 int result;
386b8a85 8485
4393e105 8486 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
2b59fc25 8487
4393e105
MM
8488 fntype = TREE_TYPE (fn);
8489 if (explicit_targs)
75650646 8490 {
4393e105
MM
8491 /* [temp.deduct]
8492
8493 The specified template arguments must match the template
8494 parameters in kind (i.e., type, nontype, template), and there
8495 must not be more arguments than there are parameters;
8496 otherwise type deduction fails.
8497
8498 Nontype arguments must match the types of the corresponding
8499 nontype template parameters, or must be convertible to the
8500 types of the corresponding nontype parameters as specified in
8501 _temp.arg.nontype_, otherwise type deduction fails.
8502
8503 All references in the function type of the function template
8504 to the corresponding template parameters are replaced by the
8505 specified template argument values. If a substitution in a
8506 template parameter or in the function type of the function
8507 template results in an invalid type, type deduction fails. */
8508 int i;
8509 tree converted_args;
75650646 8510
4393e105
MM
8511 converted_args
8512 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
c2ea3a40 8513 explicit_targs, NULL_TREE, tf_none,
4393e105
MM
8514 /*require_all_arguments=*/0));
8515 if (converted_args == error_mark_node)
75650646 8516 return 1;
386b8a85 8517
c2ea3a40 8518 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
4393e105
MM
8519 if (fntype == error_mark_node)
8520 return 1;
050367a3 8521
4393e105
MM
8522 /* Place the explicitly specified arguments in TARGS. */
8523 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
8524 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
75650646 8525 }
4393e105
MM
8526
8527 parms = TYPE_ARG_TYPES (fntype);
e5214479
JM
8528 /* Never do unification on the 'this' parameter. */
8529 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
8530 parms = TREE_CHAIN (parms);
8d3631f8
NS
8531
8532 if (return_type)
8533 {
8534 /* We've been given a return type to match, prepend it. */
8535 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
8536 args = tree_cons (NULL_TREE, return_type, args);
16e42e7c
JM
8537 if (len >= 0)
8538 ++len;
4393e105
MM
8539 }
8540
4393e105
MM
8541 /* We allow incomplete unification without an error message here
8542 because the standard doesn't seem to explicitly prohibit it. Our
8543 callers must be ready to deal with unification failures in any
8544 event. */
adecb3f4
MM
8545 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
8546 targs, parms, args, /*subr=*/0,
e5214479 8547 strict, /*allow_incomplete*/1, len);
adecb3f4
MM
8548
8549 if (result == 0)
8550 /* All is well so far. Now, check:
8551
8552 [temp.deduct]
8553
8554 When all template arguments have been deduced, all uses of
8555 template parameters in nondeduced contexts are replaced with
8556 the corresponding deduced argument values. If the
8557 substitution results in an invalid type, as described above,
8558 type deduction fails. */
c2ea3a40 8559 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
adecb3f4
MM
8560 == error_mark_node)
8561 return 1;
8562
8563 return result;
830bfa74
MM
8564}
8565
8566/* Adjust types before performing type deduction, as described in
8567 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
8568 sections are symmetric. PARM is the type of a function parameter
8569 or the return type of the conversion function. ARG is the type of
8570 the argument passed to the call, or the type of the value
dc957d14 8571 initialized with the result of the conversion function. */
386b8a85 8572
62e4a758 8573static int
830bfa74
MM
8574maybe_adjust_types_for_deduction (strict, parm, arg)
8575 unification_kind_t strict;
8576 tree* parm;
8577 tree* arg;
8578{
62e4a758
NS
8579 int result = 0;
8580
830bfa74
MM
8581 switch (strict)
8582 {
8583 case DEDUCE_CALL:
8584 break;
8585
8586 case DEDUCE_CONV:
8587 {
4c7d0dff
MM
8588 /* Swap PARM and ARG throughout the remainder of this
8589 function; the handling is precisely symmetric since PARM
8590 will initialize ARG rather than vice versa. */
830bfa74
MM
8591 tree* temp = parm;
8592 parm = arg;
8593 arg = temp;
8594 break;
8595 }
8596
8597 case DEDUCE_EXACT:
8598 /* There is nothing to do in this case. */
62e4a758 8599 return 0;
830bfa74 8600
62e4a758
NS
8601 case DEDUCE_ORDER:
8602 /* DR 214. [temp.func.order] is underspecified, and leads to no
8603 ordering between things like `T *' and `T const &' for `U *'.
8604 The former has T=U and the latter T=U*. The former looks more
8605 specialized and John Spicer considers it well-formed (the EDG
8606 compiler accepts it).
8607
8608 John also confirms that deduction should proceed as in a function
996065b4 8609 call. Which implies the usual ARG and PARM conversions as DEDUCE_CALL.
62e4a758
NS
8610 However, in ordering, ARG can have REFERENCE_TYPE, but no argument
8611 to an actual call can have such a type.
8612
996065b4
NS
8613 If both ARG and PARM are REFERENCE_TYPE, we change neither.
8614 If only ARG is a REFERENCE_TYPE, we look through that and then
8615 proceed as with DEDUCE_CALL (which could further convert it). */
62e4a758
NS
8616 if (TREE_CODE (*arg) == REFERENCE_TYPE)
8617 {
8618 if (TREE_CODE (*parm) == REFERENCE_TYPE)
8619 return 0;
8620 *arg = TREE_TYPE (*arg);
62e4a758
NS
8621 }
8622 break;
830bfa74 8623 default:
a98facb0 8624 abort ();
830bfa74
MM
8625 }
8626
8627 if (TREE_CODE (*parm) != REFERENCE_TYPE)
8628 {
8629 /* [temp.deduct.call]
8630
8631 If P is not a reference type:
8632
8633 --If A is an array type, the pointer type produced by the
8634 array-to-pointer standard conversion (_conv.array_) is
8635 used in place of A for type deduction; otherwise,
8636
8637 --If A is a function type, the pointer type produced by
8638 the function-to-pointer standard conversion
8639 (_conv.func_) is used in place of A for type deduction;
8640 otherwise,
8641
8642 --If A is a cv-qualified type, the top level
8643 cv-qualifiers of A's type are ignored for type
8644 deduction. */
8645 if (TREE_CODE (*arg) == ARRAY_TYPE)
8646 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 8647 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
8648 *arg = build_pointer_type (*arg);
8649 else
8650 *arg = TYPE_MAIN_VARIANT (*arg);
8651 }
8652
8653 /* [temp.deduct.call]
8654
8655 If P is a cv-qualified type, the top level cv-qualifiers
8656 of P's type are ignored for type deduction. If P is a
8657 reference type, the type referred to by P is used for
8658 type deduction. */
8659 *parm = TYPE_MAIN_VARIANT (*parm);
8660 if (TREE_CODE (*parm) == REFERENCE_TYPE)
62e4a758
NS
8661 {
8662 *parm = TREE_TYPE (*parm);
8663 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
8664 }
8665 return result;
386b8a85
JM
8666}
8667
e5214479 8668/* Most parms like fn_type_unification.
9f54c803
MM
8669
8670 If SUBR is 1, we're being called recursively (to unify the
8671 arguments of a function or method parameter of a function
8672 template). */
386b8a85 8673
4966381a 8674static int
bd0d5d4a
JM
8675type_unification_real (tparms, targs, xparms, xargs, subr,
8676 strict, allow_incomplete, xlen)
8677 tree tparms, targs, xparms, xargs;
830bfa74
MM
8678 int subr;
8679 unification_kind_t strict;
bd0d5d4a 8680 int allow_incomplete, xlen;
8d08fdba
MS
8681{
8682 tree parm, arg;
8683 int i;
8684 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 8685 int sub_strict;
bd0d5d4a
JM
8686 int saw_undeduced = 0;
8687 tree parms, args;
8688 int len;
8d08fdba
MS
8689
8690 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
bd0d5d4a
JM
8691 my_friendly_assert (xparms == NULL_TREE
8692 || TREE_CODE (xparms) == TREE_LIST, 290);
4ba126e4 8693 /* ARGS could be NULL. */
bd0d5d4a
JM
8694 if (xargs)
8695 my_friendly_assert (TREE_CODE (xargs) == TREE_LIST, 291);
8d08fdba
MS
8696 my_friendly_assert (ntparms > 0, 292);
8697
830bfa74
MM
8698 switch (strict)
8699 {
8700 case DEDUCE_CALL:
028d1f20
NS
8701 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
8702 | UNIFY_ALLOW_DERIVED);
830bfa74
MM
8703 break;
8704
8705 case DEDUCE_CONV:
8706 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
8707 break;
8708
8709 case DEDUCE_EXACT:
8710 sub_strict = UNIFY_ALLOW_NONE;
8711 break;
62e4a758
NS
8712
8713 case DEDUCE_ORDER:
8714 sub_strict = UNIFY_ALLOW_NONE;
8715 break;
830bfa74
MM
8716
8717 default:
a98facb0 8718 abort ();
830bfa74
MM
8719 }
8720
bd0d5d4a 8721 if (xlen == 0)
e5214479
JM
8722 return 0;
8723
bd0d5d4a
JM
8724 again:
8725 parms = xparms;
8726 args = xargs;
8727 len = xlen;
8728
8d08fdba
MS
8729 while (parms
8730 && parms != void_list_node
8731 && args
8732 && args != void_list_node)
8733 {
8734 parm = TREE_VALUE (parms);
8735 parms = TREE_CHAIN (parms);
8736 arg = TREE_VALUE (args);
8737 args = TREE_CHAIN (args);
8738
8739 if (arg == error_mark_node)
8740 return 1;
8741 if (arg == unknown_type_node)
34016c81
JM
8742 /* We can't deduce anything from this, but we might get all the
8743 template args from other function args. */
8744 continue;
b7484fbe 8745
03e70705
JM
8746 /* Conversions will be performed on a function argument that
8747 corresponds with a function parameter that contains only
8748 non-deducible template parameters and explicitly specified
8749 template parameters. */
8750 if (! uses_template_parms (parm))
b7484fbe 8751 {
03e70705
JM
8752 tree type;
8753
2f939d94 8754 if (!TYPE_P (arg))
03e70705
JM
8755 type = TREE_TYPE (arg);
8756 else
8757 {
8758 type = arg;
8759 arg = NULL_TREE;
8760 }
8761
62e4a758 8762 if (strict == DEDUCE_EXACT || strict == DEDUCE_ORDER)
03e70705 8763 {
3bfdc719 8764 if (same_type_p (parm, type))
03e70705
JM
8765 continue;
8766 }
03e70705 8767 else
343c89cd
JM
8768 /* It might work; we shouldn't check now, because we might
8769 get into infinite recursion. Overload resolution will
8770 handle it. */
8771 continue;
03e70705 8772
b7484fbe
MS
8773 return 1;
8774 }
8775
2f939d94 8776 if (!TYPE_P (arg))
8d08fdba
MS
8777 {
8778 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
34016c81 8779 if (type_unknown_p (arg))
28cbf42c 8780 {
34016c81
JM
8781 /* [temp.deduct.type] A template-argument can be deduced from
8782 a pointer to function or pointer to member function
8783 argument if the set of overloaded functions does not
8784 contain function templates and at most one of a set of
8785 overloaded functions provides a unique match. */
8786
8787 if (resolve_overloaded_unification
4393e105 8788 (tparms, targs, parm, arg, strict, sub_strict)
34016c81
JM
8789 != 0)
8790 return 1;
8791 continue;
28cbf42c 8792 }
8d08fdba
MS
8793 arg = TREE_TYPE (arg);
8794 }
62e4a758
NS
8795
8796 {
8797 int arg_strict = sub_strict;
8798
8799 if (!subr)
8800 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
4393e105 8801
62e4a758
NS
8802 if (unify (tparms, targs, parm, arg, arg_strict))
8803 return 1;
8804 }
e5214479
JM
8805
8806 /* Are we done with the interesting parms? */
8807 if (--len == 0)
bd0d5d4a 8808 goto done;
8d08fdba
MS
8809 }
8810 /* Fail if we've reached the end of the parm list, and more args
8811 are present, and the parm list isn't variadic. */
8812 if (args && args != void_list_node && parms == void_list_node)
8813 return 1;
8814 /* Fail if parms are left and they don't have default values. */
8815 if (parms
8816 && parms != void_list_node
8817 && TREE_PURPOSE (parms) == NULL_TREE)
8818 return 1;
bd0d5d4a
JM
8819
8820 done:
8d08fdba
MS
8821 if (!subr)
8822 for (i = 0; i < ntparms; i++)
050367a3 8823 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8d08fdba 8824 {
bd0d5d4a
JM
8825 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8826
8827 /* If this is an undeduced nontype parameter that depends on
8828 a type parameter, try another pass; its type may have been
8829 deduced from a later argument than the one from which
8830 this parameter can be deduced. */
8831 if (TREE_CODE (tparm) == PARM_DECL
8832 && uses_template_parms (TREE_TYPE (tparm))
8833 && !saw_undeduced++)
8834 goto again;
8835
386b8a85 8836 if (!allow_incomplete)
8251199e 8837 error ("incomplete type unification");
8d08fdba
MS
8838 return 2;
8839 }
8840 return 0;
8841}
8842
34016c81
JM
8843/* Subroutine of type_unification_real. Args are like the variables at the
8844 call site. ARG is an overloaded function (or template-id); we try
8845 deducing template args from each of the overloads, and if only one
8846 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
8847
8848static int
8849resolve_overloaded_unification (tparms, targs, parm, arg, strict,
4393e105 8850 sub_strict)
34016c81
JM
8851 tree tparms, targs, parm, arg;
8852 unification_kind_t strict;
8853 int sub_strict;
34016c81
JM
8854{
8855 tree tempargs = copy_node (targs);
8856 int good = 0;
8857
8858 if (TREE_CODE (arg) == ADDR_EXPR)
8859 arg = TREE_OPERAND (arg, 0);
9f3d9e46 8860
d8f8dca1
MM
8861 if (TREE_CODE (arg) == COMPONENT_REF)
8862 /* Handle `&x' where `x' is some static or non-static member
8863 function name. */
8864 arg = TREE_OPERAND (arg, 1);
8865
05e0b2f4
JM
8866 if (TREE_CODE (arg) == OFFSET_REF)
8867 arg = TREE_OPERAND (arg, 1);
8868
9f3d9e46 8869 /* Strip baselink information. */
50ad9642
MM
8870 if (BASELINK_P (arg))
8871 arg = BASELINK_FUNCTIONS (arg);
9f3d9e46 8872
34016c81
JM
8873 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
8874 {
8875 /* If we got some explicit template args, we need to plug them into
8876 the affected templates before we try to unify, in case the
8877 explicit args will completely resolve the templates in question. */
8878
8879 tree expl_subargs = TREE_OPERAND (arg, 1);
8880 arg = TREE_OPERAND (arg, 0);
8881
8882 for (; arg; arg = OVL_NEXT (arg))
8883 {
8884 tree fn = OVL_CURRENT (arg);
8885 tree subargs, elem;
8886
8887 if (TREE_CODE (fn) != TEMPLATE_DECL)
8888 continue;
8889
17aec3eb
RK
8890 subargs = get_bindings_overload (fn, DECL_TEMPLATE_RESULT (fn),
8891 expl_subargs);
34016c81
JM
8892 if (subargs)
8893 {
c2ea3a40 8894 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
d8f8dca1
MM
8895 if (TREE_CODE (elem) == METHOD_TYPE)
8896 elem = build_ptrmemfunc_type (build_pointer_type (elem));
e97e5263 8897 good += try_one_overload (tparms, targs, tempargs, parm, elem,
4393e105 8898 strict, sub_strict);
34016c81
JM
8899 }
8900 }
8901 }
db0f613d
KL
8902 else if (TREE_CODE (arg) == OVERLOAD
8903 || TREE_CODE (arg) == FUNCTION_DECL)
34016c81
JM
8904 {
8905 for (; arg; arg = OVL_NEXT (arg))
d8f8dca1
MM
8906 {
8907 tree type = TREE_TYPE (OVL_CURRENT (arg));
8908 if (TREE_CODE (type) == METHOD_TYPE)
8909 type = build_ptrmemfunc_type (build_pointer_type (type));
8910 good += try_one_overload (tparms, targs, tempargs, parm,
8911 type,
4393e105 8912 strict, sub_strict);
d8f8dca1 8913 }
34016c81
JM
8914 }
8915 else
a98facb0 8916 abort ();
34016c81
JM
8917
8918 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8919 to function or pointer to member function argument if the set of
8920 overloaded functions does not contain function templates and at most
8921 one of a set of overloaded functions provides a unique match.
8922
8923 So if we found multiple possibilities, we return success but don't
8924 deduce anything. */
8925
8926 if (good == 1)
8927 {
8928 int i = TREE_VEC_LENGTH (targs);
8929 for (; i--; )
8930 if (TREE_VEC_ELT (tempargs, i))
8931 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8932 }
8933 if (good)
8934 return 0;
8935
8936 return 1;
8937}
8938
8939/* Subroutine of resolve_overloaded_unification; does deduction for a single
8940 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8941 different overloads deduce different arguments for a given parm.
8942 Returns 1 on success. */
8943
8944static int
e97e5263 8945try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
4393e105 8946 sub_strict)
e97e5263 8947 tree tparms, orig_targs, targs, parm, arg;
34016c81
JM
8948 unification_kind_t strict;
8949 int sub_strict;
34016c81
JM
8950{
8951 int nargs;
8952 tree tempargs;
8953 int i;
8954
8955 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8956 to function or pointer to member function argument if the set of
8957 overloaded functions does not contain function templates and at most
8958 one of a set of overloaded functions provides a unique match.
8959
8960 So if this is a template, just return success. */
8961
8962 if (uses_template_parms (arg))
8963 return 1;
8964
62e4a758 8965 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
34016c81
JM
8966
8967 /* We don't copy orig_targs for this because if we have already deduced
8968 some template args from previous args, unify would complain when we
8969 try to deduce a template parameter for the same argument, even though
8970 there isn't really a conflict. */
8971 nargs = TREE_VEC_LENGTH (targs);
f31c0a32 8972 tempargs = make_tree_vec (nargs);
34016c81 8973
4393e105 8974 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
34016c81
JM
8975 return 0;
8976
8977 /* First make sure we didn't deduce anything that conflicts with
e97e5263 8978 explicitly specified args. */
34016c81
JM
8979 for (i = nargs; i--; )
8980 {
8981 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 8982 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81
JM
8983
8984 if (elt == NULL_TREE)
8985 continue;
8986 else if (uses_template_parms (elt))
8987 {
8988 /* Since we're unifying against ourselves, we will fill in template
8989 args used in the function parm list with our own template parms.
8990 Discard them. */
8991 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8992 continue;
8993 }
8994 else if (oldelt && ! template_args_equal (oldelt, elt))
8995 return 0;
8996 }
8997
8998 for (i = nargs; i--; )
8999 {
9000 tree elt = TREE_VEC_ELT (tempargs, i);
9001
9002 if (elt)
9003 TREE_VEC_ELT (targs, i) = elt;
9004 }
9005
9006 return 1;
9007}
9008
74601d7c
KL
9009/* Verify that nondeduce template argument agrees with the type
9010 obtained from argument deduction. Return nonzero if the
9011 verification fails.
9012
9013 For example:
9014
9015 struct A { typedef int X; };
9016 template <class T, class U> struct C {};
9017 template <class T> struct C<T, typename T::X> {};
9018
9019 Then with the instantiation `C<A, int>', we can deduce that
9020 `T' is `A' but unify () does not check whether `typename T::X'
9021 is `int'. This function ensure that they agree.
9022
9023 TARGS, PARMS are the same as the arguments of unify.
9024 ARGS contains template arguments from all levels. */
9025
9026static int
9027verify_class_unification (targs, parms, args)
9028 tree targs, parms, args;
9029{
e2005c8d
KL
9030 parms = tsubst (parms, add_outermost_template_args (args, targs),
9031 tf_none, NULL_TREE);
9032 if (parms == error_mark_node)
74601d7c
KL
9033 return 1;
9034
e2005c8d 9035 return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
74601d7c
KL
9036}
9037
4393e105
MM
9038/* PARM is a template class (perhaps with unbound template
9039 parameters). ARG is a fully instantiated type. If ARG can be
9040 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
9041 TARGS are as for unify. */
fcfb9f96
MM
9042
9043static tree
4393e105 9044try_class_unification (tparms, targs, parm, arg)
fcfb9f96
MM
9045 tree tparms;
9046 tree targs;
4393e105
MM
9047 tree parm;
9048 tree arg;
9049{
4393e105
MM
9050 tree copy_of_targs;
9051
9052 if (!CLASSTYPE_TEMPLATE_INFO (arg)
9053 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
9054 return NULL_TREE;
9055
9056 /* We need to make a new template argument vector for the call to
9057 unify. If we used TARGS, we'd clutter it up with the result of
9058 the attempted unification, even if this class didn't work out.
9059 We also don't want to commit ourselves to all the unifications
9060 we've already done, since unification is supposed to be done on
9061 an argument-by-argument basis. In other words, consider the
9062 following pathological case:
9063
9064 template <int I, int J, int K>
9065 struct S {};
9066
9067 template <int I, int J>
9068 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
9069
9070 template <int I, int J, int K>
9071 void f(S<I, J, K>, S<I, I, I>);
9072
9073 void g() {
9074 S<0, 0, 0> s0;
9075 S<0, 1, 2> s2;
9076
9077 f(s0, s2);
9078 }
9079
9080 Now, by the time we consider the unification involving `s2', we
9081 already know that we must have `f<0, 0, 0>'. But, even though
0e339752 9082 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
4393e105
MM
9083 because there are two ways to unify base classes of S<0, 1, 2>
9084 with S<I, I, I>. If we kept the already deduced knowledge, we
9085 would reject the possibility I=1. */
f31c0a32 9086 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
4393e105
MM
9087
9088 /* If unification failed, we're done. */
74601d7c
KL
9089 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
9090 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
4393e105 9091 return NULL_TREE;
74601d7c
KL
9092
9093 return arg;
4393e105
MM
9094}
9095
9096/* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
dc957d14 9097 have already discovered to be satisfactory. ARG_BINFO is the binfo
4393e105
MM
9098 for the base class of ARG that we are currently examining. */
9099
9100static tree
9101get_template_base_recursive (tparms, targs, parm,
9102 arg_binfo, rval, flags)
9103 tree tparms;
9104 tree targs;
9105 tree arg_binfo;
fcfb9f96 9106 tree rval;
4393e105
MM
9107 tree parm;
9108 int flags;
fcfb9f96
MM
9109{
9110 tree binfos;
9111 int i, n_baselinks;
4393e105 9112 tree arg = BINFO_TYPE (arg_binfo);
fcfb9f96 9113
4393e105 9114 if (!(flags & GTB_IGNORE_TYPE))
fcfb9f96 9115 {
4393e105
MM
9116 tree r = try_class_unification (tparms, targs,
9117 parm, arg);
fcfb9f96 9118
4393e105 9119 /* If there is more than one satisfactory baseclass, then:
fcfb9f96 9120
4393e105
MM
9121 [temp.deduct.call]
9122
9123 If they yield more than one possible deduced A, the type
9124 deduction fails.
9125
9126 applies. */
9127 if (r && rval && !same_type_p (r, rval))
9128 return error_mark_node;
9129 else if (r)
9130 rval = r;
fcfb9f96
MM
9131 }
9132
4393e105 9133 binfos = BINFO_BASETYPES (arg_binfo);
fcfb9f96
MM
9134 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
9135
9136 /* Process base types. */
9137 for (i = 0; i < n_baselinks; i++)
9138 {
9139 tree base_binfo = TREE_VEC_ELT (binfos, i);
4393e105 9140 int this_virtual;
fcfb9f96 9141
4393e105
MM
9142 /* Skip this base, if we've already seen it. */
9143 if (BINFO_MARKED (base_binfo))
9144 continue;
9145
9146 this_virtual =
9147 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
9148
9149 /* When searching for a non-virtual, we cannot mark virtually
9150 found binfos. */
9151 if (! this_virtual)
dbbf88d1 9152 BINFO_MARKED (base_binfo) = 1;
4393e105
MM
9153
9154 rval = get_template_base_recursive (tparms, targs,
9155 parm,
9156 base_binfo,
9157 rval,
9158 GTB_VIA_VIRTUAL * this_virtual);
9159
9160 /* If we discovered more than one matching base class, we can
9161 stop now. */
9162 if (rval == error_mark_node)
9163 return error_mark_node;
fcfb9f96
MM
9164 }
9165
9166 return rval;
9167}
9168
4393e105
MM
9169/* Given a template type PARM and a class type ARG, find the unique
9170 base type in ARG that is an instance of PARM. We do not examine
9171 ARG itself; only its base-classes. If there is no appropriate base
9172 class, return NULL_TREE. If there is more than one, return
9173 error_mark_node. PARM may be the type of a partial specialization,
9174 as well as a plain template type. Used by unify. */
fcfb9f96
MM
9175
9176static tree
4393e105 9177get_template_base (tparms, targs, parm, arg)
fcfb9f96
MM
9178 tree tparms;
9179 tree targs;
4393e105
MM
9180 tree parm;
9181 tree arg;
fcfb9f96 9182{
4393e105
MM
9183 tree rval;
9184 tree arg_binfo;
fcfb9f96 9185
4393e105
MM
9186 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
9187
9188 arg_binfo = TYPE_BINFO (complete_type (arg));
9189 rval = get_template_base_recursive (tparms, targs,
9190 parm, arg_binfo,
9191 NULL_TREE,
9192 GTB_IGNORE_TYPE);
fcfb9f96 9193
4393e105
MM
9194 /* Since get_template_base_recursive marks the bases classes, we
9195 must unmark them here. */
d6479fe7 9196 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
fcfb9f96
MM
9197
9198 return rval;
9199}
9200
db2767b6
MM
9201/* Returns the level of DECL, which declares a template parameter. */
9202
e9659ab0 9203static int
db2767b6
MM
9204template_decl_level (decl)
9205 tree decl;
9206{
9207 switch (TREE_CODE (decl))
9208 {
9209 case TYPE_DECL:
9210 case TEMPLATE_DECL:
9211 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
9212
9213 case PARM_DECL:
9214 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
9215
9216 default:
a98facb0 9217 abort ();
5ffe581d 9218 return 0;
db2767b6
MM
9219 }
9220}
9221
830bfa74
MM
9222/* Decide whether ARG can be unified with PARM, considering only the
9223 cv-qualifiers of each type, given STRICT as documented for unify.
838dfd8a 9224 Returns nonzero iff the unification is OK on that basis.*/
e92cc029 9225
e9659ab0 9226static int
830bfa74
MM
9227check_cv_quals_for_unify (strict, arg, parm)
9228 int strict;
9229 tree arg;
9230 tree parm;
9231{
4f2b0fb2
NS
9232 int arg_quals = cp_type_quals (arg);
9233 int parm_quals = cp_type_quals (parm);
9234
9235 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM)
9236 {
9237 /* If the cvr quals of parm will not unify with ARG, they'll be
9238 ignored in instantiation, so we have to do the same here. */
171d2f50 9239 if (TREE_CODE (arg) == REFERENCE_TYPE)
4f2b0fb2
NS
9240 parm_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
9241 if (!POINTER_TYPE_P (arg) &&
9242 TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
9243 parm_quals &= ~TYPE_QUAL_RESTRICT;
9244 }
9245
62e4a758 9246 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 9247 && (arg_quals & parm_quals) != parm_quals)
ef637255
MM
9248 return 0;
9249
62e4a758 9250 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
4f2b0fb2 9251 && (parm_quals & arg_quals) != arg_quals)
ef637255
MM
9252 return 0;
9253
ef637255 9254 return 1;
830bfa74
MM
9255}
9256
9257/* Takes parameters as for type_unification. Returns 0 if the
dc957d14 9258 type deduction succeeds, 1 otherwise. The parameter STRICT is a
830bfa74
MM
9259 bitwise or of the following flags:
9260
9261 UNIFY_ALLOW_NONE:
9262 Require an exact match between PARM and ARG.
9263 UNIFY_ALLOW_MORE_CV_QUAL:
028d1f20
NS
9264 Allow the deduced ARG to be more cv-qualified (by qualification
9265 conversion) than ARG.
830bfa74
MM
9266 UNIFY_ALLOW_LESS_CV_QUAL:
9267 Allow the deduced ARG to be less cv-qualified than ARG.
9268 UNIFY_ALLOW_DERIVED:
9269 Allow the deduced ARG to be a template base class of ARG,
9270 or a pointer to a template base class of the type pointed to by
161c12b0
JM
9271 ARG.
9272 UNIFY_ALLOW_INTEGER:
9273 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
028d1f20
NS
9274 case for more information.
9275 UNIFY_ALLOW_OUTER_LEVEL:
9276 This is the outermost level of a deduction. Used to determine validity
9277 of qualification conversions. A valid qualification conversion must
9278 have const qualified pointers leading up to the inner type which
9279 requires additional CV quals, except at the outer level, where const
9280 is not required [conv.qual]. It would be normal to set this flag in
62e4a758
NS
9281 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
9282 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
9283 This is the outermost level of a deduction, and PARM can be more CV
9284 qualified at this point.
9285 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
9286 This is the outermost level of a deduction, and PARM can be less CV
d9f818d9
RS
9287 qualified at this point.
9288 UNIFY_ALLOW_MAX_CORRECTION:
9289 This is an INTEGER_TYPE's maximum value. Used if the range may
9290 have been derived from a size specification, such as an array size.
9291 If the size was given by a nontype template parameter N, the maximum
9292 value will have the form N-1. The flag says that we can (and indeed
9293 must) unify N with (ARG + 1), an exception to the normal rules on
9294 folding PARM. */
830bfa74 9295
e9659ab0 9296static int
4393e105 9297unify (tparms, targs, parm, arg, strict)
050367a3 9298 tree tparms, targs, parm, arg;
830bfa74 9299 int strict;
8d08fdba
MS
9300{
9301 int idx;
050367a3 9302 tree targ;
db2767b6 9303 tree tparm;
028d1f20 9304 int strict_in = strict;
8d08fdba
MS
9305
9306 /* I don't think this will do the right thing with respect to types.
9307 But the only case I've seen it in so far has been array bounds, where
9308 signedness is the only information lost, and I think that will be
9309 okay. */
9310 while (TREE_CODE (parm) == NOP_EXPR)
9311 parm = TREE_OPERAND (parm, 0);
9312
9313 if (arg == error_mark_node)
9314 return 1;
9315 if (arg == unknown_type_node)
34016c81
JM
9316 /* We can't deduce anything from this, but we might get all the
9317 template args from other function args. */
9318 return 0;
9319
db2767b6 9320 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 9321 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
9322 template parameters. We might need them if we're trying to
9323 figure out which of two things is more specialized. */
9324 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
9325 return 0;
9326
830bfa74
MM
9327 /* Immediately reject some pairs that won't unify because of
9328 cv-qualification mismatches. */
9329 if (TREE_CODE (arg) == TREE_CODE (parm)
2f939d94 9330 && TYPE_P (arg)
d0ab7624
NS
9331 /* It is the elements of the array which hold the cv quals of an array
9332 type, and the elements might be template type parms. We'll check
9333 when we recurse. */
9334 && TREE_CODE (arg) != ARRAY_TYPE
830bfa74
MM
9335 /* We check the cv-qualifiers when unifying with template type
9336 parameters below. We want to allow ARG `const T' to unify with
9337 PARM `T' for example, when computing which of two templates
9338 is more specialized, for example. */
9339 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
028d1f20 9340 && !check_cv_quals_for_unify (strict_in, arg, parm))
49432171
JM
9341 return 1;
9342
028d1f20 9343 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
3ea099f1 9344 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
028d1f20
NS
9345 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
9346 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
9347 strict &= ~UNIFY_ALLOW_DERIVED;
62e4a758
NS
9348 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9349 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
d9f818d9 9350 strict &= ~UNIFY_ALLOW_MAX_CORRECTION;
028d1f20 9351
8d08fdba
MS
9352 switch (TREE_CODE (parm))
9353 {
2ca340ae 9354 case TYPENAME_TYPE:
fccef71e 9355 case SCOPE_REF:
b8c6534b 9356 case UNBOUND_CLASS_TEMPLATE:
2ca340ae
JM
9357 /* In a type which contains a nested-name-specifier, template
9358 argument values cannot be deduced for template parameters used
9359 within the nested-name-specifier. */
9360 return 0;
9361
8d08fdba 9362 case TEMPLATE_TYPE_PARM:
73b0fce8 9363 case TEMPLATE_TEMPLATE_PARM:
a1281f45 9364 case BOUND_TEMPLATE_TEMPLATE_PARM:
db2767b6
MM
9365 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9366
9367 if (TEMPLATE_TYPE_LEVEL (parm)
9368 != template_decl_level (tparm))
9369 /* The PARM is not one we're trying to unify. Just check
9370 to see if it matches ARG. */
9371 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 9372 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 9373 idx = TEMPLATE_TYPE_IDX (parm);
050367a3 9374 targ = TREE_VEC_ELT (targs, idx);
db2767b6 9375 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 9376
73b0fce8 9377 /* Check for mixed types and values. */
db2767b6
MM
9378 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
9379 && TREE_CODE (tparm) != TYPE_DECL)
9380 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9381 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
9382 return 1;
9383
a1281f45 9384 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
73b0fce8 9385 {
b429fdf0
KL
9386 /* ARG must be constructed from a template class or a template
9387 template parameter. */
9388 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
9389 && (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg)))
a1281f45 9390 return 1;
73b0fce8 9391
a1281f45
KL
9392 {
9393 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
9394 tree parmvec = TYPE_TI_ARGS (parm);
b429fdf0 9395 tree argvec = TYPE_TI_ARGS (arg);
a1281f45 9396 tree argtmplvec
b429fdf0 9397 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
a1281f45 9398 int i;
73b0fce8 9399
a1281f45
KL
9400 /* The parameter and argument roles have to be switched here
9401 in order to handle default arguments properly. For example,
9402 template<template <class> class TT> void f(TT<int>)
9403 should be able to accept vector<int> which comes from
9404 template <class T, class Allocator = allocator>
9405 class vector. */
9406
9407 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
9408 == error_mark_node)
9409 return 1;
73b0fce8 9410
a1281f45
KL
9411 /* Deduce arguments T, i from TT<T> or TT<i>.
9412 We check each element of PARMVEC and ARGVEC individually
9413 rather than the whole TREE_VEC since they can have
9414 different number of elements. */
6b9b6b15 9415
a1281f45
KL
9416 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
9417 {
9418 tree t = TREE_VEC_ELT (parmvec, i);
73b0fce8 9419
a1281f45
KL
9420 if (unify (tparms, targs, t,
9421 TREE_VEC_ELT (argvec, i),
9422 UNIFY_ALLOW_NONE))
9423 return 1;
73b0fce8 9424 }
a1281f45 9425 }
b429fdf0 9426 arg = TYPE_TI_TEMPLATE (arg);
a1281f45
KL
9427
9428 /* Fall through to deduce template name. */
9429 }
9430
9431 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
9432 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
9433 {
9434 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
9435
9436 /* Simple cases: Value already set, does match or doesn't. */
9437 if (targ != NULL_TREE && template_args_equal (targ, arg))
9438 return 0;
9439 else if (targ)
9440 return 1;
db2767b6
MM
9441 }
9442 else
9443 {
9ae58faf
NS
9444 /* If ARG is an offset type, we're trying to unify '*T' with
9445 'U C::*', which is ill-formed. See the comment in the
9446 POINTER_TYPE case about this ugliness. */
9447 if (TREE_CODE (arg) == OFFSET_TYPE)
9448 return 1;
9449
830bfa74
MM
9450 /* If PARM is `const T' and ARG is only `int', we don't have
9451 a match unless we are allowing additional qualification.
9452 If ARG is `const int' and PARM is just `T' that's OK;
9453 that binds `const int' to `T'. */
028d1f20 9454 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
830bfa74 9455 arg, parm))
db2767b6
MM
9456 return 1;
9457
830bfa74
MM
9458 /* Consider the case where ARG is `const volatile int' and
9459 PARM is `const T'. Then, T should be `volatile int'. */
c2ea3a40
NS
9460 arg = cp_build_qualified_type_real
9461 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
adecb3f4
MM
9462 if (arg == error_mark_node)
9463 return 1;
73b0fce8 9464
a1281f45
KL
9465 /* Simple cases: Value already set, does match or doesn't. */
9466 if (targ != NULL_TREE && same_type_p (targ, arg))
9467 return 0;
9468 else if (targ)
9469 return 1;
61cd552e 9470
94fc547c
MM
9471 /* Make sure that ARG is not a variable-sized array. (Note
9472 that were talking about variable-sized arrays (like
9473 `int[n]'), rather than arrays of unknown size (like
9474 `int[]').) We'll get very confused by such a type since
9475 the bound of the array will not be computable in an
9476 instantiation. Besides, such types are not allowed in
9477 ISO C++, so we can do as we please here. */
9478 if (variably_modified_type_p (arg))
9479 return 1;
9480 }
61cd552e 9481
050367a3 9482 TREE_VEC_ELT (targs, idx) = arg;
73b0fce8
KL
9483 return 0;
9484
f84b4be9 9485 case TEMPLATE_PARM_INDEX:
db2767b6
MM
9486 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
9487
9488 if (TEMPLATE_PARM_LEVEL (parm)
9489 != template_decl_level (tparm))
9490 /* The PARM is not one we're trying to unify. Just check
9491 to see if it matches ARG. */
9492 return (TREE_CODE (arg) == TREE_CODE (parm)
67ffc812 9493 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
db2767b6 9494
f84b4be9 9495 idx = TEMPLATE_PARM_IDX (parm);
050367a3 9496 targ = TREE_VEC_ELT (targs, idx);
db2767b6 9497
050367a3 9498 if (targ)
8d08fdba 9499 {
67ffc812 9500 int i = (cp_tree_equal (targ, arg) > 0);
312e7d50
JM
9501 if (i == 1)
9502 return 0;
9503 else if (i == 0)
9504 return 1;
9505 else
a98facb0 9506 abort ();
8d08fdba 9507 }
8d08fdba 9508
161c12b0
JM
9509 /* [temp.deduct.type] If, in the declaration of a function template
9510 with a non-type template-parameter, the non-type
9511 template-parameter is used in an expression in the function
9512 parameter-list and, if the corresponding template-argument is
9513 deduced, the template-argument type shall match the type of the
9514 template-parameter exactly, except that a template-argument
d7c4edd0
NS
9515 deduced from an array bound may be of any integral type.
9516 The non-type parameter might use already deduced type parameters. */
bd0d5d4a 9517 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
e2005c8d
KL
9518 if (!TREE_TYPE (arg))
9519 /* Template-parameter dependent expression. Just accept it for now.
9520 It will later be processed in convert_template_argument. */
9521 ;
9522 else if (same_type_p (TREE_TYPE (arg), tparm))
9523 /* OK */;
161c12b0 9524 else if ((strict & UNIFY_ALLOW_INTEGER)
bd0d5d4a
JM
9525 && (TREE_CODE (tparm) == INTEGER_TYPE
9526 || TREE_CODE (tparm) == BOOLEAN_TYPE))
161c12b0 9527 /* OK */;
bd0d5d4a
JM
9528 else if (uses_template_parms (tparm))
9529 /* We haven't deduced the type of this parameter yet. Try again
9530 later. */
9531 return 0;
161c12b0
JM
9532 else
9533 return 1;
9534
2a1e9fdd 9535 TREE_VEC_ELT (targs, idx) = arg;
8d08fdba
MS
9536 return 0;
9537
9538 case POINTER_TYPE:
830bfa74 9539 {
830bfa74
MM
9540 if (TREE_CODE (arg) != POINTER_TYPE)
9541 return 1;
9542
9543 /* [temp.deduct.call]
9544
9545 A can be another pointer or pointer to member type that can
9546 be converted to the deduced A via a qualification
9547 conversion (_conv.qual_).
9548
9549 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
9550 This will allow for additional cv-qualification of the
028d1f20 9551 pointed-to types if appropriate. */
830bfa74 9552
028d1f20 9553 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
830bfa74
MM
9554 /* The derived-to-base conversion only persists through one
9555 level of pointers. */
028d1f20 9556 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
830bfa74 9557
3ea099f1
JM
9558 if (TREE_CODE (TREE_TYPE (parm)) == OFFSET_TYPE
9559 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
9560 {
9561 /* Avoid getting confused about cv-quals; don't recurse here.
9562 Pointers to members should really be just OFFSET_TYPE, not
c6002625 9563 this two-level nonsense... */
3ea099f1
JM
9564
9565 parm = TREE_TYPE (parm);
9566 arg = TREE_TYPE (arg);
9567 goto offset;
9568 }
9569
a7a64a77 9570 return unify (tparms, targs, TREE_TYPE (parm),
028d1f20 9571 TREE_TYPE (arg), strict);
830bfa74 9572 }
8d08fdba
MS
9573
9574 case REFERENCE_TYPE:
830bfa74
MM
9575 if (TREE_CODE (arg) != REFERENCE_TYPE)
9576 return 1;
9577 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
028d1f20 9578 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
9579
9580 case ARRAY_TYPE:
9581 if (TREE_CODE (arg) != ARRAY_TYPE)
9582 return 1;
3042d5be
MM
9583 if ((TYPE_DOMAIN (parm) == NULL_TREE)
9584 != (TYPE_DOMAIN (arg) == NULL_TREE))
9585 return 1;
9586 if (TYPE_DOMAIN (parm) != NULL_TREE
830bfa74 9587 && unify (tparms, targs, TYPE_DOMAIN (parm),
4393e105 9588 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8d08fdba 9589 return 1;
830bfa74 9590 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
712467a4 9591 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
9592
9593 case REAL_TYPE:
37c46b43 9594 case COMPLEX_TYPE:
c00996a3 9595 case VECTOR_TYPE:
8d08fdba 9596 case INTEGER_TYPE:
42976354 9597 case BOOLEAN_TYPE:
5ad5a526 9598 case VOID_TYPE:
f376e137
MS
9599 if (TREE_CODE (arg) != TREE_CODE (parm))
9600 return 1;
9601
514a1f18
JM
9602 if (TREE_CODE (parm) == INTEGER_TYPE
9603 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8d08fdba
MS
9604 {
9605 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
830bfa74 9606 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
4393e105 9607 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8d08fdba
MS
9608 return 1;
9609 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
830bfa74 9610 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
d9f818d9
RS
9611 TYPE_MAX_VALUE (arg),
9612 UNIFY_ALLOW_INTEGER | UNIFY_ALLOW_MAX_CORRECTION))
8d08fdba
MS
9613 return 1;
9614 }
9edc3913 9615 /* We have already checked cv-qualification at the top of the
514a1f18 9616 function. */
9edc3913 9617 else if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
ca79f85d
JM
9618 return 1;
9619
8d08fdba
MS
9620 /* As far as unification is concerned, this wins. Later checks
9621 will invalidate it if necessary. */
9622 return 0;
9623
9624 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 9625 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 9626 case INTEGER_CST:
bd6dd845
MS
9627 while (TREE_CODE (arg) == NOP_EXPR)
9628 arg = TREE_OPERAND (arg, 0);
9629
8d08fdba
MS
9630 if (TREE_CODE (arg) != INTEGER_CST)
9631 return 1;
9632 return !tree_int_cst_equal (parm, arg);
9633
8d08fdba
MS
9634 case TREE_VEC:
9635 {
9636 int i;
9637 if (TREE_CODE (arg) != TREE_VEC)
9638 return 1;
9639 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
9640 return 1;
0dc09a61 9641 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
830bfa74 9642 if (unify (tparms, targs,
8d08fdba 9643 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
4393e105 9644 UNIFY_ALLOW_NONE))
8d08fdba
MS
9645 return 1;
9646 return 0;
9647 }
9648
8d08fdba 9649 case RECORD_TYPE:
f181d4ae 9650 case UNION_TYPE:
f181d4ae 9651 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 9652 return 1;
830bfa74 9653
a7a64a77
MM
9654 if (TYPE_PTRMEMFUNC_P (parm))
9655 {
9656 if (!TYPE_PTRMEMFUNC_P (arg))
9657 return 1;
9658
9659 return unify (tparms, targs,
9660 TYPE_PTRMEMFUNC_FN_TYPE (parm),
9661 TYPE_PTRMEMFUNC_FN_TYPE (arg),
9662 strict);
9663 }
9664
5db698f6 9665 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 9666 {
6467930b 9667 tree t = NULL_TREE;
4393e105 9668
028d1f20 9669 if (strict_in & UNIFY_ALLOW_DERIVED)
4393e105
MM
9670 {
9671 /* First, we try to unify the PARM and ARG directly. */
9672 t = try_class_unification (tparms, targs,
9673 parm, arg);
9674
9675 if (!t)
9676 {
9677 /* Fallback to the special case allowed in
9678 [temp.deduct.call]:
9679
9680 If P is a class, and P has the form
9681 template-id, then A can be a derived class of
9682 the deduced A. Likewise, if P is a pointer to
9683 a class of the form template-id, A can be a
9684 pointer to a derived class pointed to by the
9685 deduced A. */
9686 t = get_template_base (tparms, targs,
9687 parm, arg);
9688
9689 if (! t || t == error_mark_node)
9690 return 1;
9691 }
9692 }
6df47b06 9693 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9fbf56f7
MM
9694 && (CLASSTYPE_TI_TEMPLATE (parm)
9695 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
9696 /* Perhaps PARM is something like S<U> and ARG is S<int>.
9697 Then, we should unify `int' and `U'. */
6467930b 9698 t = arg;
4393e105 9699 else
dc957d14 9700 /* There's no chance of unification succeeding. */
5566b478 9701 return 1;
6467930b 9702
830bfa74 9703 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
4393e105 9704 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
5566b478 9705 }
9edc3913 9706 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
5566b478 9707 return 1;
a4443a08 9708 return 0;
8d08fdba
MS
9709
9710 case METHOD_TYPE:
8d08fdba 9711 case FUNCTION_TYPE:
830bfa74 9712 if (TREE_CODE (arg) != TREE_CODE (parm))
8d08fdba 9713 return 1;
830bfa74
MM
9714
9715 if (unify (tparms, targs, TREE_TYPE (parm),
4393e105 9716 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
28cbf42c 9717 return 1;
386b8a85 9718 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3b3ba9f0 9719 TYPE_ARG_TYPES (arg), 1,
e5214479 9720 DEDUCE_EXACT, 0, -1);
a4443a08
MS
9721
9722 case OFFSET_TYPE:
3ea099f1 9723 offset:
a4443a08
MS
9724 if (TREE_CODE (arg) != OFFSET_TYPE)
9725 return 1;
830bfa74 9726 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
4393e105 9727 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
a4443a08 9728 return 1;
830bfa74 9729 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 9730 strict);
a4443a08 9731
f62dbf03 9732 case CONST_DECL:
a723baf1
MM
9733 if (DECL_TEMPLATE_PARM_P (parm))
9734 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
830bfa74 9735 if (arg != decl_constant_value (parm))
f62dbf03
JM
9736 return 1;
9737 return 0;
9738
027905b4
KL
9739 case TEMPLATE_DECL:
9740 /* Matched cases are handled by the ARG == PARM test above. */
9741 return 1;
9742
53929c47 9743 case MINUS_EXPR:
d9f818d9
RS
9744 if (tree_int_cst_equal (TREE_OPERAND (parm, 1), integer_one_node)
9745 && (strict_in & UNIFY_ALLOW_MAX_CORRECTION))
53929c47
JM
9746 {
9747 /* We handle this case specially, since it comes up with
9748 arrays. In particular, something like:
9749
9750 template <int N> void f(int (&x)[N]);
9751
9752 Here, we are trying to unify the range type, which
9753 looks like [0 ... (N - 1)]. */
9754 tree t, t1, t2;
9755 t1 = TREE_OPERAND (parm, 0);
9756 t2 = TREE_OPERAND (parm, 1);
9757
24e2e58e 9758 t = fold (build (PLUS_EXPR, integer_type_node, arg, t2));
53929c47 9759
4393e105 9760 return unify (tparms, targs, t1, t, strict);
53929c47
JM
9761 }
9762 /* else fall through */
9763
8d08fdba 9764 default:
050367a3 9765 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
74601d7c
KL
9766 {
9767
9768 /* We're looking at an expression. This can happen with
9769 something like:
4393e105 9770
74601d7c
KL
9771 template <int I>
9772 void foo(S<I>, S<I + 2>);
050367a3 9773
74601d7c 9774 This is a "nondeduced context":
050367a3 9775
74601d7c 9776 [deduct.type]
4393e105 9777
74601d7c 9778 The nondeduced contexts are:
050367a3 9779
74601d7c
KL
9780 --A type that is a template-id in which one or more of
9781 the template-arguments is an expression that references
9782 a template-parameter.
050367a3 9783
74601d7c
KL
9784 In these cases, we assume deduction succeeded, but don't
9785 actually infer any unifications. */
9786
9787 if (!uses_template_parms (parm)
9788 && !template_args_equal (parm, arg))
9789 return 1;
9790 else
9791 return 0;
9792 }
050367a3 9793 else
b4c4a9ec
MM
9794 sorry ("use of `%s' in template type unification",
9795 tree_code_name [(int) TREE_CODE (parm)]);
050367a3 9796
8d08fdba
MS
9797 return 1;
9798 }
9799}
8d08fdba 9800\f
03d0f4af
MM
9801/* Called if RESULT is explicitly instantiated, or is a member of an
9802 explicitly instantiated class, or if using -frepo and the
9803 instantiation of RESULT has been assigned to this file. */
9804
faae18ab 9805void
5566b478 9806mark_decl_instantiated (result, extern_p)
faae18ab
MS
9807 tree result;
9808 int extern_p;
9809{
75650646
MM
9810 if (TREE_CODE (result) != FUNCTION_DECL)
9811 /* The TREE_PUBLIC flag for function declarations will have been
9812 set correctly by tsubst. */
9813 TREE_PUBLIC (result) = 1;
faae18ab 9814
3ae18eaf
JM
9815 /* We used to set this unconditionally; we moved that to
9816 do_decl_instantiation so it wouldn't get set on members of
9817 explicit class template instantiations. But we still need to set
9818 it here for the 'extern template' case in order to suppress
9819 implicit instantiations. */
9820 if (extern_p)
9821 SET_DECL_EXPLICIT_INSTANTIATION (result);
9822
faae18ab
MS
9823 if (! extern_p)
9824 {
9825 DECL_INTERFACE_KNOWN (result) = 1;
9826 DECL_NOT_REALLY_EXTERN (result) = 1;
a7d87521 9827
1a408d07
JM
9828 /* Always make artificials weak. */
9829 if (DECL_ARTIFICIAL (result) && flag_weak)
9830 comdat_linkage (result);
a7d87521
JM
9831 /* For WIN32 we also want to put explicit instantiations in
9832 linkonce sections. */
1a408d07 9833 else if (TREE_PUBLIC (result))
b385c841 9834 maybe_make_one_only (result);
faae18ab 9835 }
f49422da 9836 else if (TREE_CODE (result) == FUNCTION_DECL)
56e770bf 9837 defer_fn (result);
faae18ab
MS
9838}
9839
e5214479 9840/* Given two function templates PAT1 and PAT2, return:
6467930b 9841
62e4a758
NS
9842 DEDUCE should be DEDUCE_EXACT or DEDUCE_ORDER.
9843
6467930b
MS
9844 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
9845 -1 if PAT2 is more specialized than PAT1.
e5214479
JM
9846 0 if neither is more specialized.
9847
9848 LEN is passed through to fn_type_unification. */
6467930b
MS
9849
9850int
62e4a758 9851more_specialized (pat1, pat2, deduce, len)
e5214479 9852 tree pat1, pat2;
62e4a758 9853 int deduce;
e5214479 9854 int len;
6467930b 9855{
98c1c668 9856 tree targs;
73aad9b9 9857 int winner = 0;
6467930b 9858
62e4a758
NS
9859 targs = get_bindings_real (pat1, DECL_TEMPLATE_RESULT (pat2),
9860 NULL_TREE, 0, deduce, len);
73aad9b9 9861 if (targs)
36a117a5 9862 --winner;
6467930b 9863
62e4a758
NS
9864 targs = get_bindings_real (pat2, DECL_TEMPLATE_RESULT (pat1),
9865 NULL_TREE, 0, deduce, len);
73aad9b9 9866 if (targs)
36a117a5 9867 ++winner;
6467930b 9868
73aad9b9
JM
9869 return winner;
9870}
6467930b 9871
73aad9b9 9872/* Given two class template specialization list nodes PAT1 and PAT2, return:
6467930b 9873
73aad9b9
JM
9874 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
9875 -1 if PAT2 is more specialized than PAT1.
9876 0 if neither is more specialized. */
9877
9878int
9879more_specialized_class (pat1, pat2)
9880 tree pat1, pat2;
9881{
9882 tree targs;
9883 int winner = 0;
9884
36a117a5
MM
9885 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
9886 TREE_PURPOSE (pat2));
73aad9b9
JM
9887 if (targs)
9888 --winner;
9889
36a117a5
MM
9890 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
9891 TREE_PURPOSE (pat1));
73aad9b9 9892 if (targs)
6467930b
MS
9893 ++winner;
9894
9895 return winner;
9896}
73aad9b9
JM
9897
9898/* Return the template arguments that will produce the function signature
e1467ff2 9899 DECL from the function template FN, with the explicit template
76b9a14d 9900 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
4393e105 9901 also match. Return NULL_TREE if no satisfactory arguments could be
62e4a758 9902 found. DEDUCE and LEN are passed through to fn_type_unification. */
e5214479 9903
76b9a14d 9904static tree
62e4a758 9905get_bindings_real (fn, decl, explicit_args, check_rettype, deduce, len)
e1467ff2 9906 tree fn, decl, explicit_args;
62e4a758 9907 int check_rettype, deduce, len;
73aad9b9 9908{
98c1c668 9909 int ntparms = DECL_NTPARMS (fn);
f31c0a32 9910 tree targs = make_tree_vec (ntparms);
4393e105 9911 tree decl_type;
03017874 9912 tree decl_arg_types;
98c1c668
JM
9913 int i;
9914
4393e105
MM
9915 /* Substitute the explicit template arguments into the type of DECL.
9916 The call to fn_type_unification will handle substitution into the
9917 FN. */
9918 decl_type = TREE_TYPE (decl);
9919 if (explicit_args && uses_template_parms (decl_type))
9920 {
9921 tree tmpl;
9922 tree converted_args;
9923
9924 if (DECL_TEMPLATE_INFO (decl))
9925 tmpl = DECL_TI_TEMPLATE (decl);
9926 else
0e339752 9927 /* We can get here for some invalid specializations. */
4393e105
MM
9928 return NULL_TREE;
9929
9930 converted_args
9931 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
9932 explicit_args, NULL_TREE,
c2ea3a40 9933 tf_none, /*require_all_arguments=*/0));
4393e105
MM
9934 if (converted_args == error_mark_node)
9935 return NULL_TREE;
9936
c2ea3a40 9937 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
4393e105
MM
9938 if (decl_type == error_mark_node)
9939 return NULL_TREE;
9940 }
9941
9942 decl_arg_types = TYPE_ARG_TYPES (decl_type);
e5214479
JM
9943 /* Never do unification on the 'this' parameter. */
9944 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
03017874 9945 decl_arg_types = TREE_CHAIN (decl_arg_types);
d7684f2d 9946
e1467ff2 9947 i = fn_type_unification (fn, explicit_args, targs,
03017874 9948 decl_arg_types,
8d3631f8
NS
9949 (check_rettype || DECL_CONV_FN_P (fn)
9950 ? TREE_TYPE (decl_type) : NULL_TREE),
62e4a758 9951 deduce, len);
98c1c668 9952
76b9a14d
JM
9953 if (i != 0)
9954 return NULL_TREE;
9955
76b9a14d
JM
9956 return targs;
9957}
9958
9959/* For most uses, we want to check the return type. */
9960
9961tree
9962get_bindings (fn, decl, explicit_args)
9963 tree fn, decl, explicit_args;
9964{
62e4a758 9965 return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
76b9a14d
JM
9966}
9967
e5214479
JM
9968/* But for resolve_overloaded_unification, we only care about the parameter
9969 types. */
76b9a14d
JM
9970
9971static tree
9972get_bindings_overload (fn, decl, explicit_args)
9973 tree fn, decl, explicit_args;
9974{
62e4a758 9975 return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
73aad9b9
JM
9976}
9977
36a117a5
MM
9978/* Return the innermost template arguments that, when applied to a
9979 template specialization whose innermost template parameters are
9471d3e2 9980 TPARMS, and whose specialization arguments are PARMS, yield the
36a117a5
MM
9981 ARGS.
9982
9983 For example, suppose we have:
9984
9985 template <class T, class U> struct S {};
9986 template <class T> struct S<T*, int> {};
9987
9988 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
9989 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
9990 int}. The resulting vector will be {double}, indicating that `T'
9991 is bound to `double'. */
9992
bd6dd845 9993static tree
36a117a5
MM
9994get_class_bindings (tparms, parms, args)
9995 tree tparms, parms, args;
73aad9b9 9996{
3b3ba9f0 9997 int i, ntparms = TREE_VEC_LENGTH (tparms);
f31c0a32 9998 tree vec = make_tree_vec (ntparms);
73aad9b9 9999
74601d7c
KL
10000 if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
10001 UNIFY_ALLOW_NONE))
fcfb9f96 10002 return NULL_TREE;
73aad9b9
JM
10003
10004 for (i = 0; i < ntparms; ++i)
10005 if (! TREE_VEC_ELT (vec, i))
10006 return NULL_TREE;
10007
74601d7c
KL
10008 if (verify_class_unification (vec, parms, args))
10009 return NULL_TREE;
10010
73aad9b9
JM
10011 return vec;
10012}
10013
104bf76a
MM
10014/* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
10015 Pick the most specialized template, and return the corresponding
10016 instantiation, or if there is no corresponding instantiation, the
e5214479
JM
10017 template itself. If there is no most specialized template,
10018 error_mark_node is returned. If there are no templates at all,
10019 NULL_TREE is returned. */
73aad9b9
JM
10020
10021tree
e5214479 10022most_specialized_instantiation (instantiations)
104bf76a 10023 tree instantiations;
73aad9b9 10024{
104bf76a 10025 tree fn, champ;
73aad9b9
JM
10026 int fate;
10027
104bf76a 10028 if (!instantiations)
73aad9b9
JM
10029 return NULL_TREE;
10030
104bf76a
MM
10031 champ = instantiations;
10032 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
73aad9b9 10033 {
62e4a758
NS
10034 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10035 DEDUCE_EXACT, -1);
73aad9b9
JM
10036 if (fate == 1)
10037 ;
10038 else
10039 {
10040 if (fate == 0)
10041 {
10042 fn = TREE_CHAIN (fn);
10043 if (! fn)
10044 return error_mark_node;
10045 }
104bf76a 10046 champ = fn;
73aad9b9
JM
10047 }
10048 }
10049
104bf76a 10050 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
73aad9b9 10051 {
62e4a758
NS
10052 fate = more_specialized (TREE_VALUE (champ), TREE_VALUE (fn),
10053 DEDUCE_EXACT, -1);
73aad9b9
JM
10054 if (fate != 1)
10055 return error_mark_node;
10056 }
10057
104bf76a
MM
10058 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
10059}
10060
10061/* Return the most specialized of the list of templates in FNS that can
10062 produce an instantiation matching DECL, given the explicit template
10063 arguments EXPLICIT_ARGS. */
10064
e9659ab0 10065static tree
104bf76a
MM
10066most_specialized (fns, decl, explicit_args)
10067 tree fns, decl, explicit_args;
10068{
10069 tree candidates = NULL_TREE;
10070 tree fn, args;
10071
10072 for (fn = fns; fn; fn = TREE_CHAIN (fn))
10073 {
10074 tree candidate = TREE_VALUE (fn);
10075
10076 args = get_bindings (candidate, decl, explicit_args);
10077 if (args)
e1b3e07d 10078 candidates = tree_cons (NULL_TREE, candidate, candidates);
104bf76a
MM
10079 }
10080
e5214479 10081 return most_specialized_instantiation (candidates);
73aad9b9
JM
10082}
10083
36a117a5 10084/* If DECL is a specialization of some template, return the most
f9a7ae04
MM
10085 general such template. Otherwise, returns NULL_TREE.
10086
10087 For example, given:
36a117a5
MM
10088
10089 template <class T> struct S { template <class U> void f(U); };
10090
10091 if TMPL is `template <class U> void S<int>::f(U)' this will return
10092 the full template. This function will not trace past partial
10093 specializations, however. For example, given in addition:
10094
10095 template <class T> struct S<T*> { template <class U> void f(U); };
10096
10097 if TMPL is `template <class U> void S<int*>::f(U)' this will return
10098 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9 10099
612c671a 10100tree
36a117a5
MM
10101most_general_template (decl)
10102 tree decl;
73aad9b9 10103{
f9a7ae04
MM
10104 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
10105 an immediate specialization. */
10106 if (TREE_CODE (decl) == FUNCTION_DECL)
10107 {
10108 if (DECL_TEMPLATE_INFO (decl)) {
10109 decl = DECL_TI_TEMPLATE (decl);
10110
10111 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
10112 template friend. */
10113 if (TREE_CODE (decl) != TEMPLATE_DECL)
10114 return NULL_TREE;
10115 } else
10116 return NULL_TREE;
10117 }
10118
10119 /* Look for more and more general templates. */
10120 while (DECL_TEMPLATE_INFO (decl))
10121 {
10122 /* The DECL_TI_TEMPLATE can be a LOOKUP_EXPR or IDENTIFIER_NODE
10123 in some cases. (See cp-tree.h for details.) */
10124 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10125 break;
10126
10127 /* Stop if we run into an explicitly specialized class template. */
10128 if (!DECL_NAMESPACE_SCOPE_P (decl)
10129 && DECL_CONTEXT (decl)
10130 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
10131 break;
10132
10133 decl = DECL_TI_TEMPLATE (decl);
10134 }
36a117a5
MM
10135
10136 return decl;
10137}
10138
10139/* Return the most specialized of the class template specializations
10140 of TMPL which can produce an instantiation matching ARGS, or
10141 error_mark_node if the choice is ambiguous. */
10142
e9659ab0 10143static tree
36a117a5
MM
10144most_specialized_class (tmpl, args)
10145 tree tmpl;
10146 tree args;
10147{
10148 tree list = NULL_TREE;
10149 tree t;
10150 tree champ;
73aad9b9
JM
10151 int fate;
10152
36a117a5
MM
10153 tmpl = most_general_template (tmpl);
10154 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 10155 {
36a117a5
MM
10156 tree spec_args
10157 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
10158 if (spec_args)
73aad9b9 10159 {
1f8f4a0b 10160 list = tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
73aad9b9
JM
10161 TREE_TYPE (list) = TREE_TYPE (t);
10162 }
10163 }
10164
10165 if (! list)
10166 return NULL_TREE;
10167
10168 t = list;
10169 champ = t;
10170 t = TREE_CHAIN (t);
10171 for (; t; t = TREE_CHAIN (t))
10172 {
10173 fate = more_specialized_class (champ, t);
10174 if (fate == 1)
10175 ;
10176 else
10177 {
10178 if (fate == 0)
10179 {
10180 t = TREE_CHAIN (t);
10181 if (! t)
10182 return error_mark_node;
10183 }
10184 champ = t;
10185 }
10186 }
10187
10188 for (t = list; t && t != champ; t = TREE_CHAIN (t))
10189 {
85b71cf2 10190 fate = more_specialized_class (champ, t);
73aad9b9
JM
10191 if (fate != 1)
10192 return error_mark_node;
10193 }
10194
10195 return champ;
10196}
10197
eb8845be 10198/* Explicitly instantiate DECL. */
e92cc029 10199
8d08fdba 10200void
eb8845be 10201do_decl_instantiation (tree decl, tree storage)
8d08fdba 10202{
8d08fdba 10203 tree result = NULL_TREE;
faae18ab 10204 int extern_p = 0;
e8abc66f 10205
3fa56191 10206 if (!decl)
dc957d14 10207 /* An error occurred, for which grokdeclarator has already issued
3fa56191
MM
10208 an appropriate message. */
10209 return;
10210 else if (! DECL_LANG_SPECIFIC (decl))
ec255269 10211 {
33bd39a2 10212 error ("explicit instantiation of non-template `%#D'", decl);
ec255269
MS
10213 return;
10214 }
03d0f4af 10215 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 10216 {
03d0f4af
MM
10217 /* There is an asymmetry here in the way VAR_DECLs and
10218 FUNCTION_DECLs are handled by grokdeclarator. In the case of
10219 the latter, the DECL we get back will be marked as a
10220 template instantiation, and the appropriate
10221 DECL_TEMPLATE_INFO will be set up. This does not happen for
10222 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
10223 should handle VAR_DECLs as it currently handles
10224 FUNCTION_DECLs. */
86ac0575 10225 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
e0860732 10226 if (!result || TREE_CODE (result) != VAR_DECL)
03d0f4af 10227 {
e0860732 10228 error ("no matching template for `%D' found", decl);
03d0f4af
MM
10229 return;
10230 }
6633d636
MS
10231 }
10232 else if (TREE_CODE (decl) != FUNCTION_DECL)
10233 {
33bd39a2 10234 error ("explicit instantiation of `%#D'", decl);
6633d636
MS
10235 return;
10236 }
03d0f4af
MM
10237 else
10238 result = decl;
672476cb 10239
03d0f4af 10240 /* Check for various error cases. Note that if the explicit
0e339752 10241 instantiation is valid the RESULT will currently be marked as an
03d0f4af
MM
10242 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
10243 until we get here. */
10244
10245 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 10246 {
07782718
KL
10247 /* DR 259 [temp.spec].
10248
10249 Both an explicit instantiation and a declaration of an explicit
10250 specialization shall not appear in a program unless the explicit
10251 instantiation follows a declaration of the explicit specialization.
03d0f4af 10252
07782718
KL
10253 For a given set of template parameters, if an explicit
10254 instantiation of a template appears after a declaration of an
10255 explicit specialization for that template, the explicit
10256 instantiation has no effect. */
672476cb
MM
10257 return;
10258 }
03d0f4af
MM
10259 else if (DECL_EXPLICIT_INSTANTIATION (result))
10260 {
10261 /* [temp.spec]
98c1c668 10262
03d0f4af
MM
10263 No program shall explicitly instantiate any template more
10264 than once.
10265
2aaf816d
JM
10266 We check DECL_INTERFACE_KNOWN so as not to complain when the first
10267 instantiation was `extern' and the second is not, and EXTERN_P for
10268 the opposite case. If -frepo, chances are we already got marked
dc957d14 10269 as an explicit instantiation because of the repo file. */
2aaf816d 10270 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
33bd39a2 10271 pedwarn ("duplicate explicit instantiation of `%#D'", result);
03d0f4af
MM
10272
10273 /* If we've already instantiated the template, just return now. */
10274 if (DECL_INTERFACE_KNOWN (result))
10275 return;
10276 }
10277 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 10278 {
33bd39a2 10279 error ("no matching template for `%D' found", result);
faae18ab
MS
10280 return;
10281 }
03d0f4af 10282 else if (!DECL_TEMPLATE_INFO (result))
6633d636 10283 {
33bd39a2 10284 pedwarn ("explicit instantiation of non-template `%#D'", result);
6633d636
MS
10285 return;
10286 }
10287
a0a33927
MS
10288 if (flag_external_templates)
10289 return;
10290
f0e01782 10291 if (storage == NULL_TREE)
00595019 10292 ;
faae18ab 10293 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af 10294 {
c02f5e29 10295 if (pedantic && !in_system_header)
33bd39a2 10296 pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
03d0f4af
MM
10297 extern_p = 1;
10298 }
f0e01782 10299 else
33bd39a2 10300 error ("storage class `%D' applied to template instantiation",
f0e01782 10301 storage);
5566b478 10302
03d0f4af 10303 SET_DECL_EXPLICIT_INSTANTIATION (result);
5566b478 10304 mark_decl_instantiated (result, extern_p);
44a8d0b3 10305 repo_template_instantiated (result, extern_p);
c91a56d2 10306 if (! extern_p)
16d53b64 10307 instantiate_decl (result, /*defer_ok=*/1);
7177d104
MS
10308}
10309
faae18ab
MS
10310void
10311mark_class_instantiated (t, extern_p)
10312 tree t;
10313 int extern_p;
10314{
10315 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
10316 SET_CLASSTYPE_INTERFACE_KNOWN (t);
10317 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
10318 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
10319 if (! extern_p)
10320 {
10321 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
10322 rest_of_type_compilation (t, 1);
10323 }
10324}
e8abc66f 10325
a1bcc528
JM
10326/* Perform an explicit instantiation of template class T. STORAGE, if
10327 non-null, is the RID for extern, inline or static. COMPLAIN is
838dfd8a 10328 nonzero if this is called from the parser, zero if called recursively,
a1bcc528
JM
10329 since the standard is unclear (as detailed below). */
10330
7177d104 10331void
a1bcc528 10332do_type_instantiation (t, storage, complain)
ca79f85d 10333 tree t, storage;
c2ea3a40 10334 tsubst_flags_t complain;
7177d104 10335{
e8abc66f
MS
10336 int extern_p = 0;
10337 int nomem_p = 0;
5566b478
MS
10338 int static_p = 0;
10339
ca79f85d
JM
10340 if (TREE_CODE (t) == TYPE_DECL)
10341 t = TREE_TYPE (t);
10342
7ddedda4 10343 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 10344 {
33bd39a2 10345 error ("explicit instantiation of non-template type `%T'", t);
ca79f85d
JM
10346 return;
10347 }
10348
5566b478 10349 complete_type (t);
7177d104 10350
a292b002
MS
10351 /* With -fexternal-templates, explicit instantiations are treated the same
10352 as implicit ones. */
a0a33927
MS
10353 if (flag_external_templates)
10354 return;
10355
d0f062fb 10356 if (!COMPLETE_TYPE_P (t))
f0e01782 10357 {
c2ea3a40 10358 if (complain & tf_error)
33bd39a2 10359 error ("explicit instantiation of `%#T' before definition of template",
a1bcc528 10360 t);
f0e01782
MS
10361 return;
10362 }
10363
03d0f4af 10364 if (storage != NULL_TREE)
f0e01782 10365 {
c02f5e29 10366 if (pedantic && !in_system_header)
33bd39a2 10367 pedwarn("ISO C++ forbids the use of `%s' on explicit instantiations",
8251199e 10368 IDENTIFIER_POINTER (storage));
03d0f4af
MM
10369
10370 if (storage == ridpointers[(int) RID_INLINE])
10371 nomem_p = 1;
10372 else if (storage == ridpointers[(int) RID_EXTERN])
10373 extern_p = 1;
10374 else if (storage == ridpointers[(int) RID_STATIC])
10375 static_p = 1;
10376 else
10377 {
33bd39a2 10378 error ("storage class `%D' applied to template instantiation",
03d0f4af
MM
10379 storage);
10380 extern_p = 0;
10381 }
f0e01782
MS
10382 }
10383
370af2d5 10384 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
03d0f4af 10385 {
07782718 10386 /* DR 259 [temp.spec].
a292b002 10387
07782718
KL
10388 Both an explicit instantiation and a declaration of an explicit
10389 specialization shall not appear in a program unless the explicit
10390 instantiation follows a declaration of the explicit specialization.
10391
10392 For a given set of template parameters, if an explicit
10393 instantiation of a template appears after a declaration of an
10394 explicit specialization for that template, the explicit
10395 instantiation has no effect. */
03d0f4af
MM
10396 return;
10397 }
10398 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 10399 {
03d0f4af
MM
10400 /* [temp.spec]
10401
10402 No program shall explicitly instantiate any template more
10403 than once.
10404
2aaf816d
JM
10405 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
10406 was `extern'. If EXTERN_P then the second is. If -frepo, chances
aba649ba 10407 are we already got marked as an explicit instantiation because of the
2aaf816d 10408 repo file. All these cases are OK. */
a1bcc528 10409 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository
c2ea3a40 10410 && (complain & tf_error))
33bd39a2 10411 pedwarn ("duplicate explicit instantiation of `%#T'", t);
03d0f4af
MM
10412
10413 /* If we've already instantiated the template, just return now. */
10414 if (!CLASSTYPE_INTERFACE_ONLY (t))
10415 return;
44a8d0b3 10416 }
e8abc66f 10417
03d0f4af
MM
10418 mark_class_instantiated (t, extern_p);
10419 repo_template_instantiated (t, extern_p);
10420
e8abc66f
MS
10421 if (nomem_p)
10422 return;
10423
7177d104 10424 {
db5ae43f 10425 tree tmp;
5566b478 10426
03d0f4af
MM
10427 /* In contrast to implicit instantiation, where only the
10428 declarations, and not the definitions, of members are
10429 instantiated, we have here:
10430
10431 [temp.explicit]
10432
10433 The explicit instantiation of a class template specialization
10434 implies the instantiation of all of its members not
10435 previously explicitly specialized in the translation unit
10436 containing the explicit instantiation.
10437
10438 Of course, we can't instantiate member template classes, since
10439 we don't have any arguments for them. Note that the standard
dc957d14 10440 is unclear on whether the instantiation of the members are
03d0f4af
MM
10441 *explicit* instantiations or not. We choose to be generous,
10442 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
10443 the explicit instantiation of a class where some of the members
10444 have no definition in the current translation unit. */
10445
5566b478
MS
10446 if (! static_p)
10447 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 10448 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 10449 && DECL_TEMPLATE_INSTANTIATION (tmp))
5566b478
MS
10450 {
10451 mark_decl_instantiated (tmp, extern_p);
10452 repo_template_instantiated (tmp, extern_p);
10453 if (! extern_p)
16d53b64 10454 instantiate_decl (tmp, /*defer_ok=*/1);
5566b478
MS
10455 }
10456
10457 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
10458 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
863adfc0 10459 {
5566b478 10460 mark_decl_instantiated (tmp, extern_p);
863adfc0 10461 repo_template_instantiated (tmp, extern_p);
5566b478 10462 if (! extern_p)
16d53b64 10463 instantiate_decl (tmp, /*defer_ok=*/1);
863adfc0 10464 }
7177d104 10465
a292b002 10466 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
e4a84209
MM
10467 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
10468 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
a1bcc528 10469 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage, 0);
a292b002 10470 }
8d08fdba 10471}
a28e3c7f 10472
36a117a5
MM
10473/* Given a function DECL, which is a specialization of TMPL, modify
10474 DECL to be a re-instantiation of TMPL with the same template
10475 arguments. TMPL should be the template into which tsubst'ing
10476 should occur for DECL, not the most general template.
830bfa74
MM
10477
10478 One reason for doing this is a scenario like this:
10479
10480 template <class T>
10481 void f(const T&, int i);
10482
10483 void g() { f(3, 7); }
10484
10485 template <class T>
10486 void f(const T& t, const int i) { }
10487
10488 Note that when the template is first instantiated, with
10489 instantiate_template, the resulting DECL will have no name for the
10490 first parameter, and the wrong type for the second. So, when we go
10491 to instantiate the DECL, we regenerate it. */
10492
e9659ab0 10493static void
830bfa74
MM
10494regenerate_decl_from_template (decl, tmpl)
10495 tree decl;
10496 tree tmpl;
10497{
f9a7ae04
MM
10498 /* The most general version of TMPL. */
10499 tree gen_tmpl;
10500 /* The arguments used to instantiate DECL, from the most general
10501 template. */
830bfa74 10502 tree args;
830bfa74
MM
10503 tree code_pattern;
10504 tree new_decl;
36a117a5 10505 int unregistered;
830bfa74
MM
10506
10507 args = DECL_TI_ARGS (decl);
10508 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
10509
8251199e
JM
10510 /* Unregister the specialization so that when we tsubst we will not
10511 just return DECL. We don't have to unregister DECL from TMPL
10512 because if would only be registered there if it were a partial
10513 instantiation of a specialization, which it isn't: it's a full
10514 instantiation. */
36a117a5 10515 gen_tmpl = most_general_template (tmpl);
2b59fc25 10516 push_access_scope_real (gen_tmpl, args, DECL_CONTEXT (decl));
36a117a5
MM
10517 unregistered = unregister_specialization (decl, gen_tmpl);
10518
10519 /* If the DECL was not unregistered then something peculiar is
10520 happening: we created a specialization but did not call
10521 register_specialization for it. */
10522 my_friendly_assert (unregistered, 0);
10523
d8c4447d 10524 /* Do the substitution to get the new declaration. */
c2ea3a40 10525 new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
830bfa74
MM
10526
10527 if (TREE_CODE (decl) == VAR_DECL)
10528 {
10529 /* Set up DECL_INITIAL, since tsubst doesn't. */
17bbb839
MM
10530 if (!DECL_INITIALIZED_IN_CLASS_P (decl))
10531 DECL_INITIAL (new_decl) =
10532 tsubst_expr (DECL_INITIAL (code_pattern), args,
10533 tf_error, DECL_TI_TEMPLATE (decl));
830bfa74 10534 }
2b0a63a3 10535 else if (TREE_CODE (decl) == FUNCTION_DECL)
fbf1c34b
MM
10536 {
10537 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
10538 new decl. */
10539 DECL_INITIAL (new_decl) = error_mark_node;
10540 /* And don't complain about a duplicate definition. */
10541 DECL_INITIAL (decl) = NULL_TREE;
10542 }
36a117a5 10543
2b59fc25 10544 pop_access_scope (decl);
0e902d98 10545
36a117a5
MM
10546 /* The immediate parent of the new template is still whatever it was
10547 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
10548 general template. We also reset the DECL_ASSEMBLER_NAME since
10549 tsubst always calculates the name as if the function in question
10550 were really a template instance, and sometimes, with friend
10551 functions, this is not so. See tsubst_friend_function for
10552 details. */
10553 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
92643fea 10554 COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
19e7881c 10555 COPY_DECL_RTL (decl, new_decl);
459c43ad 10556 DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
36a117a5
MM
10557
10558 /* Call duplicate decls to merge the old and new declarations. */
830bfa74
MM
10559 duplicate_decls (new_decl, decl);
10560
36a117a5
MM
10561 /* Now, re-register the specialization. */
10562 register_specialization (decl, gen_tmpl, args);
830bfa74
MM
10563}
10564
a723baf1
MM
10565/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
10566 substituted to get DECL. */
10567
10568static tree
10569template_for_substitution (tree decl)
10570{
10571 tree tmpl = DECL_TI_TEMPLATE (decl);
10572
10573 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
10574 for the instantiation. This is not always the most general
10575 template. Consider, for example:
10576
10577 template <class T>
10578 struct S { template <class U> void f();
10579 template <> void f<int>(); };
10580
10581 and an instantiation of S<double>::f<int>. We want TD to be the
10582 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
10583 while (/* An instantiation cannot have a definition, so we need a
10584 more general template. */
10585 DECL_TEMPLATE_INSTANTIATION (tmpl)
10586 /* We must also deal with friend templates. Given:
10587
10588 template <class T> struct S {
10589 template <class U> friend void f() {};
10590 };
10591
10592 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
10593 so far as the language is concerned, but that's still
10594 where we get the pattern for the instantiation from. On
10595 other hand, if the definition comes outside the class, say:
10596
10597 template <class T> struct S {
10598 template <class U> friend void f();
10599 };
10600 template <class U> friend void f() {}
10601
10602 we don't need to look any further. That's what the check for
10603 DECL_INITIAL is for. */
10604 || (TREE_CODE (decl) == FUNCTION_DECL
10605 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
10606 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
10607 {
10608 /* The present template, TD, should not be a definition. If it
10609 were a definition, we should be using it! Note that we
10610 cannot restructure the loop to just keep going until we find
10611 a template with a definition, since that might go too far if
10612 a specialization was declared, but not defined. */
10613 my_friendly_assert (!(TREE_CODE (decl) == VAR_DECL
10614 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl))),
10615 0);
10616
10617 /* Fetch the more general template. */
10618 tmpl = DECL_TI_TEMPLATE (tmpl);
10619 }
10620
10621 return tmpl;
10622}
10623
16d53b64 10624/* Produce the definition of D, a _DECL generated from a template. If
838dfd8a 10625 DEFER_OK is nonzero, then we don't have to actually do the
16d53b64 10626 instantiation now; we just have to do it sometime. */
f84b4be9 10627
a28e3c7f 10628tree
16d53b64 10629instantiate_decl (d, defer_ok)
5566b478 10630 tree d;
16d53b64 10631 int defer_ok;
a28e3c7f 10632{
36a117a5 10633 tree tmpl = DECL_TI_TEMPLATE (d);
65f8b0fb
MM
10634 tree gen_args;
10635 tree args;
830bfa74 10636 tree td;
36a117a5
MM
10637 tree code_pattern;
10638 tree spec;
10639 tree gen_tmpl;
5566b478 10640 int pattern_defined;
c27be9b9 10641 int line = lineno;
31a714f6 10642 int need_push;
3b304f5b 10643 const char *file = input_filename;
5566b478 10644
36a117a5
MM
10645 /* This function should only be used to instantiate templates for
10646 functions and static member variables. */
10647 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
10648 || TREE_CODE (d) == VAR_DECL, 0);
10649
db9b2174
MM
10650 /* Don't instantiate cloned functions. Instead, instantiate the
10651 functions they cloned. */
10652 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
10653 d = DECL_CLONED_FUNCTION (d);
10654
fbf1c34b 10655 if (DECL_TEMPLATE_INSTANTIATED (d))
03d0f4af 10656 /* D has already been instantiated. It might seem reasonable to
dc957d14 10657 check whether or not D is an explicit instantiation, and, if so,
03d0f4af
MM
10658 stop here. But when an explicit instantiation is deferred
10659 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
10660 is set, even though we still need to do the instantiation. */
36a117a5
MM
10661 return d;
10662
10663 /* If we already have a specialization of this declaration, then
10664 there's no reason to instantiate it. Note that
10665 retrieve_specialization gives us both instantiations and
10666 specializations, so we must explicitly check
10667 DECL_TEMPLATE_SPECIALIZATION. */
10668 gen_tmpl = most_general_template (tmpl);
65f8b0fb
MM
10669 gen_args = DECL_TI_ARGS (d);
10670 spec = retrieve_specialization (gen_tmpl, gen_args);
36a117a5
MM
10671 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
10672 return spec;
10673
10674 /* This needs to happen before any tsubsting. */
10675 if (! push_tinst_level (d))
10676 return d;
10677
297a5329
JM
10678 timevar_push (TV_PARSE);
10679
4d85e00e 10680 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
a723baf1
MM
10681 for the instantiation. */
10682 td = template_for_substitution (d);
fee23f54 10683 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 10684
328de7c2 10685 /* In the case of a friend template whose definition is provided
65f8b0fb
MM
10686 outside the class, we may have too many arguments. Drop the ones
10687 we don't need. */
10688 args = get_innermost_template_args (gen_args,
10689 TMPL_PARMS_DEPTH
10690 (DECL_TEMPLATE_PARMS (td)));
10691
5566b478 10692 if (TREE_CODE (d) == FUNCTION_DECL)
39c76b4f 10693 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
5566b478 10694 else
36a117a5 10695 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
de22184b 10696
de22184b
MS
10697 lineno = DECL_SOURCE_LINE (d);
10698 input_filename = DECL_SOURCE_FILE (d);
10699
de22184b 10700 if (pattern_defined)
5566b478 10701 {
7e9abee9
MM
10702 /* Let the repository code that this template definition is
10703 available.
10704
10705 The repository doesn't need to know about cloned functions
10706 because they never actually show up in the object file. It
10707 does need to know about the clones; those are the symbols
10708 that the linker will be emitting error messages about. */
10709 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (d)
10710 || DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (d))
10711 {
10712 tree t;
10713
10714 for (t = TREE_CHAIN (d);
10715 t && DECL_CLONED_FUNCTION_P (t);
10716 t = TREE_CHAIN (t))
10717 repo_template_used (t);
10718 }
10719 else
10720 repo_template_used (d);
5566b478
MS
10721
10722 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
10723 {
10724 if (flag_alt_external_templates)
10725 {
10726 if (interface_unknown)
10727 warn_if_unknown_interface (d);
10728 }
fee23f54 10729 else if (DECL_INTERFACE_KNOWN (code_pattern))
5566b478
MS
10730 {
10731 DECL_INTERFACE_KNOWN (d) = 1;
fee23f54 10732 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
5566b478
MS
10733 }
10734 else
fee23f54 10735 warn_if_unknown_interface (code_pattern);
5566b478
MS
10736 }
10737
e92cc029 10738 if (at_eof)
5566b478
MS
10739 import_export_decl (d);
10740 }
10741
4f2b0fb2
NS
10742 if (!defer_ok)
10743 {
10744 /* Recheck the substitutions to obtain any warning messages
10745 about ignoring cv qualifiers. */
10746 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
10747 tree type = TREE_TYPE (gen);
10748
0e902d98 10749 /* Make sure that we can see identifiers, and compute access
2b59fc25
KL
10750 correctly. D is already the target FUNCTION_DECL with the
10751 right context. */
10752 push_access_scope (d);
0e902d98 10753
4f2b0fb2
NS
10754 if (TREE_CODE (gen) == FUNCTION_DECL)
10755 {
65f8b0fb
MM
10756 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
10757 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
4f2b0fb2
NS
10758 tf_error | tf_warning, d);
10759 /* Don't simply tsubst the function type, as that will give
10760 duplicate warnings about poor parameter qualifications.
10761 The function arguments are the same as the decl_arguments
c6002625 10762 without the top level cv qualifiers. */
4f2b0fb2
NS
10763 type = TREE_TYPE (type);
10764 }
65f8b0fb 10765 tsubst (type, gen_args, tf_error | tf_warning, d);
0e902d98 10766
2b59fc25 10767 pop_access_scope (d);
4f2b0fb2
NS
10768 }
10769
46ccf50a
JM
10770 if (TREE_CODE (d) == VAR_DECL && DECL_INITIALIZED_IN_CLASS_P (d)
10771 && DECL_INITIAL (d) == NULL_TREE)
10772 /* We should have set up DECL_INITIAL in instantiate_class_template. */
10773 abort ();
930cd796
MM
10774 /* Reject all external templates except inline functions. */
10775 else if (DECL_INTERFACE_KNOWN (d)
10776 && ! DECL_NOT_REALLY_EXTERN (d)
4f8e1232
MM
10777 && ! (TREE_CODE (d) == FUNCTION_DECL
10778 && DECL_INLINE (d)))
930cd796 10779 goto out;
16d53b64
MM
10780 /* Defer all other templates, unless we have been explicitly
10781 forbidden from doing so. We restore the source position here
10782 because it's used by add_pending_template. */
10783 else if (! pattern_defined || defer_ok)
10784 {
c27be9b9
JM
10785 lineno = line;
10786 input_filename = file;
10787
03d0f4af
MM
10788 if (at_eof && !pattern_defined
10789 && DECL_EXPLICIT_INSTANTIATION (d))
10790 /* [temp.explicit]
10791
10792 The definition of a non-exported function template, a
10793 non-exported member function template, or a non-exported
10794 member function or static data member of a class template
10795 shall be present in every translation unit in which it is
10796 explicitly instantiated. */
33bd39a2 10797 pedwarn
46ccf50a 10798 ("explicit instantiation of `%D' but no definition available", d);
03d0f4af 10799
5566b478 10800 add_pending_template (d);
de22184b 10801 goto out;
5566b478
MS
10802 }
10803
31a714f6
MM
10804 need_push = !global_bindings_p ();
10805 if (need_push)
10806 push_to_top_level ();
414ea4aa 10807
2b0a63a3
MM
10808 /* We're now committed to instantiating this template. Mark it as
10809 instantiated so that recursive calls to instantiate_decl do not
10810 try to instantiate it again. */
fbf1c34b 10811 DECL_TEMPLATE_INSTANTIATED (d) = 1;
120722ac 10812
2b0a63a3
MM
10813 /* Regenerate the declaration in case the template has been modified
10814 by a subsequent redeclaration. */
10815 regenerate_decl_from_template (d, td);
10816
120722ac
MM
10817 /* We already set the file and line above. Reset them now in case
10818 they changed as a result of calling regenerate_decl_from_template. */
5156628f
MS
10819 lineno = DECL_SOURCE_LINE (d);
10820 input_filename = DECL_SOURCE_FILE (d);
10821
5566b478
MS
10822 if (TREE_CODE (d) == VAR_DECL)
10823 {
10824 DECL_IN_AGGR_P (d) = 0;
10825 if (DECL_INTERFACE_KNOWN (d))
10826 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
10827 else
10828 {
10829 DECL_EXTERNAL (d) = 1;
10830 DECL_NOT_REALLY_EXTERN (d) = 1;
10831 }
17bbb839
MM
10832 cp_finish_decl (d,
10833 (!DECL_INITIALIZED_IN_CLASS_P (d)
10834 ? DECL_INITIAL (d) : NULL_TREE),
10835 NULL_TREE, 0);
5566b478
MS
10836 }
10837 else if (TREE_CODE (d) == FUNCTION_DECL)
10838 {
6bbf1598 10839 htab_t saved_local_specializations;
a723baf1
MM
10840 tree subst_decl;
10841 tree tmpl_parm;
10842 tree spec_parm;
6bbf1598
MM
10843
10844 /* Save away the current list, in case we are instantiating one
10845 template from within the body of another. */
10846 saved_local_specializations = local_specializations;
10847
6dfbb909 10848 /* Set up the list of local specializations. */
6dfbb909 10849 local_specializations = htab_create (37,
69f794a7 10850 hash_local_specialization,
a723baf1 10851 eq_local_specializations,
6dfbb909
MM
10852 NULL);
10853
558475f0 10854 /* Set up context. */
a8f73d4b 10855 start_function (NULL_TREE, d, NULL_TREE, SF_PRE_PARSED);
5566b478 10856
a723baf1
MM
10857 /* Create substitution entries for the parameters. */
10858 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
10859 tmpl_parm = DECL_ARGUMENTS (subst_decl);
10860 spec_parm = DECL_ARGUMENTS (d);
10861 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
10862 {
10863 register_local_specialization (spec_parm, tmpl_parm);
10864 spec_parm = skip_artificial_parms_for (d, spec_parm);
10865 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
10866 }
10867 while (tmpl_parm)
10868 {
10869 register_local_specialization (spec_parm, tmpl_parm);
10870 tmpl_parm = TREE_CHAIN (tmpl_parm);
10871 spec_parm = TREE_CHAIN (spec_parm);
10872 }
10873 my_friendly_assert (!spec_parm, 20020813);
10874
558475f0
MM
10875 /* Substitute into the body of the function. */
10876 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
c2ea3a40 10877 tf_error | tf_warning, tmpl);
558475f0 10878
6dfbb909
MM
10879 /* We don't need the local specializations any more. */
10880 htab_delete (local_specializations);
6bbf1598 10881 local_specializations = saved_local_specializations;
6dfbb909 10882
4d6abc1c 10883 /* Finish the function. */
0acf7199 10884 expand_body (finish_function (0));
5566b478
MS
10885 }
10886
971cbc14
MM
10887 /* We're not deferring instantiation any more. */
10888 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
10889
31a714f6
MM
10890 if (need_push)
10891 pop_from_top_level ();
414ea4aa 10892
de22184b 10893out:
c27be9b9
JM
10894 lineno = line;
10895 input_filename = file;
10896
5566b478 10897 pop_tinst_level ();
a28e3c7f 10898
297a5329
JM
10899 timevar_pop (TV_PARSE);
10900
a28e3c7f
MS
10901 return d;
10902}
5566b478 10903
0aafb128
MM
10904/* Run through the list of templates that we wish we could
10905 instantiate, and instantiate any we can. */
10906
10907int
10908instantiate_pending_templates ()
10909{
10910 tree *t;
46ccf50a 10911 tree last = NULL_TREE;
0aafb128
MM
10912 int instantiated_something = 0;
10913 int reconsider;
10914
10915 do
10916 {
10917 reconsider = 0;
10918
10919 t = &pending_templates;
10920 while (*t)
10921 {
0aafb128
MM
10922 tree instantiation = TREE_VALUE (*t);
10923
3ae18eaf 10924 reopen_tinst_level (TREE_PURPOSE (*t));
0aafb128 10925
2f939d94 10926 if (TYPE_P (instantiation))
0aafb128
MM
10927 {
10928 tree fn;
10929
d0f062fb 10930 if (!COMPLETE_TYPE_P (instantiation))
0aafb128
MM
10931 {
10932 instantiate_class_template (instantiation);
10933 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
10934 for (fn = TYPE_METHODS (instantiation);
10935 fn;
10936 fn = TREE_CHAIN (fn))
10937 if (! DECL_ARTIFICIAL (fn))
16d53b64 10938 instantiate_decl (fn, /*defer_ok=*/0);
d0f062fb 10939 if (COMPLETE_TYPE_P (instantiation))
0aafb128
MM
10940 {
10941 instantiated_something = 1;
10942 reconsider = 1;
10943 }
10944 }
10945
d0f062fb 10946 if (COMPLETE_TYPE_P (instantiation))
0aafb128
MM
10947 /* If INSTANTIATION has been instantiated, then we don't
10948 need to consider it again in the future. */
10949 *t = TREE_CHAIN (*t);
46ccf50a
JM
10950 else
10951 {
10952 last = *t;
10953 t = &TREE_CHAIN (*t);
10954 }
0aafb128
MM
10955 }
10956 else
10957 {
16d53b64 10958 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
10959 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
10960 {
16d53b64
MM
10961 instantiation = instantiate_decl (instantiation,
10962 /*defer_ok=*/0);
0aafb128
MM
10963 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
10964 {
10965 instantiated_something = 1;
10966 reconsider = 1;
10967 }
10968 }
10969
16d53b64 10970 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
10971 || DECL_TEMPLATE_INSTANTIATED (instantiation))
10972 /* If INSTANTIATION has been instantiated, then we don't
10973 need to consider it again in the future. */
10974 *t = TREE_CHAIN (*t);
46ccf50a
JM
10975 else
10976 {
10977 last = *t;
10978 t = &TREE_CHAIN (*t);
10979 }
0aafb128 10980 }
84e5ca0f
NS
10981 tinst_depth = 0;
10982 current_tinst_level = NULL_TREE;
0aafb128 10983 }
46ccf50a 10984 last_pending_template = last;
0aafb128
MM
10985 }
10986 while (reconsider);
10987
10988 return instantiated_something;
10989}
10990
fd74ca0b
MM
10991/* Substitute ARGVEC into T, which is a list of initializers for
10992 either base class or a non-static data member. The TREE_PURPOSEs
10993 are DECLs, and the TREE_VALUEs are the initializer values. Used by
10994 instantiate_decl. */
4393e105 10995
824b9a4c 10996static tree
fd74ca0b 10997tsubst_initializer_list (t, argvec)
5566b478
MS
10998 tree t, argvec;
10999{
2282d28d 11000 tree inits = NULL_TREE;
5566b478
MS
11001
11002 for (; t; t = TREE_CHAIN (t))
11003 {
fd74ca0b
MM
11004 tree decl;
11005 tree init;
11006 tree val;
11007
c2ea3a40 11008 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
fd74ca0b 11009 NULL_TREE);
c2ea3a40 11010 init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
fd74ca0b
MM
11011 NULL_TREE);
11012
11013 if (!init)
11014 ;
11015 else if (TREE_CODE (init) == TREE_LIST)
11016 for (val = init; val; val = TREE_CHAIN (val))
11017 TREE_VALUE (val) = convert_from_reference (TREE_VALUE (val));
2282d28d 11018 else if (init != void_type_node)
fd74ca0b
MM
11019 init = convert_from_reference (init);
11020
2282d28d
MM
11021 init = expand_member_init (decl, init);
11022 if (init)
11023 {
11024 TREE_CHAIN (init) = inits;
11025 inits = init;
11026 }
5566b478 11027 }
2282d28d 11028 return inits;
5566b478
MS
11029}
11030
61a127b3
MM
11031/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
11032
11033static void
11034set_current_access_from_decl (decl)
11035 tree decl;
11036{
11037 if (TREE_PRIVATE (decl))
11038 current_access_specifier = access_private_node;
11039 else if (TREE_PROTECTED (decl))
11040 current_access_specifier = access_protected_node;
11041 else
11042 current_access_specifier = access_public_node;
11043}
11044
dbfe2124
MM
11045/* Instantiate an enumerated type. TAG is the template type, NEWTAG
11046 is the instantiation (which should have been created with
11047 start_enum) and ARGS are the template arguments to use. */
b87692e5 11048
dbfe2124
MM
11049static void
11050tsubst_enum (tag, newtag, args)
11051 tree tag;
11052 tree newtag;
11053 tree args;
b87692e5 11054{
dbfe2124 11055 tree e;
b87692e5
MS
11056
11057 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
11058 {
61a127b3 11059 tree value;
58595203 11060
61a127b3
MM
11061 /* Note that in a template enum, the TREE_VALUE is the
11062 CONST_DECL, not the corresponding INTEGER_CST. */
11063 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
c2ea3a40 11064 args, tf_error | tf_warning,
4393e105 11065 NULL_TREE);
61a127b3
MM
11066
11067 /* Give this enumeration constant the correct access. */
11068 set_current_access_from_decl (TREE_VALUE (e));
11069
11070 /* Actually build the enumerator itself. */
58595203 11071 build_enumerator (TREE_PURPOSE (e), value, newtag);
dbfe2124 11072 }
b3d5a58b 11073
219670f1 11074 finish_enum (newtag);
3e72ec9a
GDR
11075 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
11076 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
b87692e5 11077}
36a117a5 11078
1f6e1acc
AS
11079/* DECL is a FUNCTION_DECL that is a template specialization. Return
11080 its type -- but without substituting the innermost set of template
11081 arguments. So, innermost set of template parameters will appear in
5c74d5b0 11082 the type. */
1f6e1acc
AS
11083
11084tree
5c74d5b0 11085get_mostly_instantiated_function_type (decl)
1f6e1acc 11086 tree decl;
1f6e1acc 11087{
1f6e1acc
AS
11088 tree fn_type;
11089 tree tmpl;
11090 tree targs;
11091 tree tparms;
11092 int parm_depth;
11093
11094 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
11095 targs = DECL_TI_ARGS (decl);
11096 tparms = DECL_TEMPLATE_PARMS (tmpl);
11097 parm_depth = TMPL_PARMS_DEPTH (tparms);
11098
11099 /* There should be as many levels of arguments as there are levels
11100 of parameters. */
11101 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
11102
11103 fn_type = TREE_TYPE (tmpl);
1f6e1acc
AS
11104
11105 if (parm_depth == 1)
11106 /* No substitution is necessary. */
11107 ;
11108 else
11109 {
11110 int i;
11111 tree partial_args;
11112
11113 /* Replace the innermost level of the TARGS with NULL_TREEs to
dc957d14 11114 let tsubst know not to substitute for those parameters. */
1f6e1acc
AS
11115 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
11116 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
11117 SET_TMPL_ARGS_LEVEL (partial_args, i,
11118 TMPL_ARGS_LEVEL (targs, i));
11119 SET_TMPL_ARGS_LEVEL (partial_args,
11120 TMPL_ARGS_DEPTH (targs),
11121 make_tree_vec (DECL_NTPARMS (tmpl)));
11122
5c74d5b0
KL
11123 /* Make sure that we can see identifiers, and compute access
11124 correctly. We can just use the context of DECL for the
11125 partial substitution here. It depends only on outer template
11126 parameters, regardless of whether the innermost level is
11127 specialized or not. */
2b59fc25 11128 push_access_scope (decl);
5c74d5b0 11129
1f6e1acc
AS
11130 /* Now, do the (partial) substitution to figure out the
11131 appropriate function type. */
c2ea3a40 11132 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
1f6e1acc
AS
11133
11134 /* Substitute into the template parameters to obtain the real
11135 innermost set of parameters. This step is important if the
11136 innermost set of template parameters contains value
11137 parameters whose types depend on outer template parameters. */
11138 TREE_VEC_LENGTH (partial_args)--;
c2ea3a40 11139 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
1f6e1acc 11140
2b59fc25 11141 pop_access_scope (decl);
5c74d5b0 11142 }
1f6e1acc
AS
11143
11144 return fn_type;
11145}
669ec2b4 11146
cb753e49 11147/* Return truthvalue if we're processing a template different from
dc957d14 11148 the last one involved in diagnostics. */
cb753e49
GDR
11149int
11150problematic_instantiation_changed ()
11151{
11152 return last_template_error_tick != tinst_level_tick;
11153}
11154
11155/* Remember current template involved in diagnostics. */
11156void
11157record_last_problematic_instantiation ()
11158{
11159 last_template_error_tick = tinst_level_tick;
11160}
11161
11162tree
11163current_instantiation ()
11164{
11165 return current_tinst_level;
11166}
db3f4e4e
NS
11167
11168/* [temp.param] Check that template non-type parm TYPE is of an allowable
838dfd8a 11169 type. Return zero for ok, nonzero for disallowed. Issue error and
c2ea3a40 11170 warning messages under control of COMPLAIN. */
db3f4e4e
NS
11171
11172static int
11b810f1 11173invalid_nontype_parm_type_p (type, complain)
db3f4e4e 11174 tree type;
c2ea3a40 11175 tsubst_flags_t complain;
db3f4e4e
NS
11176{
11177 if (INTEGRAL_TYPE_P (type))
11178 return 0;
11179 else if (POINTER_TYPE_P (type))
11180 return 0;
11181 else if (TYPE_PTRMEM_P (type))
11182 return 0;
11183 else if (TYPE_PTRMEMFUNC_P (type))
11184 return 0;
db3f4e4e
NS
11185 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11186 return 0;
11187 else if (TREE_CODE (type) == TYPENAME_TYPE)
11188 return 0;
11189
c2ea3a40 11190 if (complain & tf_error)
33bd39a2 11191 error ("`%#T' is not a valid type for a template constant parameter",
db3f4e4e
NS
11192 type);
11193 return 1;
11194}
e2500fed 11195
5552b43c
MM
11196/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
11197 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
1fb3244a 11198
5552b43c
MM
11199static bool
11200dependent_type_p_r (tree type)
1fb3244a
MM
11201{
11202 tree scope;
11203
1fb3244a
MM
11204 /* [temp.dep.type]
11205
11206 A type is dependent if it is:
11207
11208 -- a template parameter. */
11209 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11210 return true;
11211 /* -- a qualified-id with a nested-name-specifier which contains a
11212 class-name that names a dependent type or whose unqualified-id
11213 names a dependent type. */
11214 if (TREE_CODE (type) == TYPENAME_TYPE)
11215 return true;
11216 /* -- a cv-qualified type where the cv-unqualified type is
11217 dependent. */
11218 type = TYPE_MAIN_VARIANT (type);
11219 /* -- a compound type constructed from any dependent type. */
11220 if (TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
11221 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
11222 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
11223 (type)));
11224 else if (TREE_CODE (type) == POINTER_TYPE
11225 || TREE_CODE (type) == REFERENCE_TYPE)
11226 return dependent_type_p (TREE_TYPE (type));
11227 else if (TREE_CODE (type) == FUNCTION_TYPE
11228 || TREE_CODE (type) == METHOD_TYPE)
11229 {
11230 tree arg_type;
11231
11232 if (dependent_type_p (TREE_TYPE (type)))
11233 return true;
11234 for (arg_type = TYPE_ARG_TYPES (type);
11235 arg_type;
11236 arg_type = TREE_CHAIN (arg_type))
11237 if (dependent_type_p (TREE_VALUE (arg_type)))
11238 return true;
11239 return false;
11240 }
11241 /* -- an array type constructed from any dependent type or whose
11242 size is specified by a constant expression that is
11243 value-dependent. */
11244 if (TREE_CODE (type) == ARRAY_TYPE)
11245 {
11246 if (TYPE_DOMAIN (type)
11247 && ((value_dependent_expression_p
11248 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
11249 || (type_dependent_expression_p
11250 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
11251 return true;
11252 return dependent_type_p (TREE_TYPE (type));
11253 }
11254 /* -- a template-id in which either the template name is a template
11255 parameter or any of the template arguments is a dependent type or
11256 an expression that is type-dependent or value-dependent.
11257
11258 This language seems somewhat confused; for example, it does not
11259 discuss template template arguments. Therefore, we use the
11260 definition for dependent template arguments in [temp.dep.temp]. */
11261 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
11262 && (dependent_template_id_p
11263 (CLASSTYPE_TI_TEMPLATE (type),
11264 CLASSTYPE_TI_ARGS (type))))
11265 return true;
11266 else if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
11267 return true;
11268 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
11269 expression is not type-dependent, then it should already been
11270 have resolved. */
11271 if (TREE_CODE (type) == TYPEOF_TYPE)
11272 return true;
11273 /* The standard does not specifically mention types that are local
11274 to template functions or local classes, but they should be
11275 considered dependent too. For example:
11276
11277 template <int I> void f() {
11278 enum E { a = I };
11279 S<sizeof (E)> s;
11280 }
11281
11282 The size of `E' cannot be known until the value of `I' has been
11283 determined. Therefore, `E' must be considered dependent. */
11284 scope = TYPE_CONTEXT (type);
11285 if (scope && TYPE_P (scope))
11286 return dependent_type_p (scope);
11287 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
11288 return type_dependent_expression_p (scope);
11289
11290 /* Other types are non-dependent. */
11291 return false;
11292}
11293
5552b43c
MM
11294/* Returns TRUE if TYPE is dependent, in the sense of
11295 [temp.dep.type]. */
11296
11297bool
11298dependent_type_p (tree type)
11299{
11300 /* If there are no template parameters in scope, then there can't be
11301 any dependent types. */
11302 if (!processing_template_decl)
11303 return false;
11304
11305 /* If the type is NULL, we have not computed a type for the entity
11306 in question; in that case, the type is dependent. */
11307 if (!type)
11308 return true;
11309
11310 /* Erroneous types can be considered non-dependent. */
11311 if (type == error_mark_node)
11312 return false;
11313
11314 /* If we have not already computed the appropriate value for TYPE,
11315 do so now. */
11316 if (!TYPE_DEPENDENT_P_VALID (type))
11317 {
11318 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
11319 TYPE_DEPENDENT_P_VALID (type) = 1;
11320 }
11321
11322 return TYPE_DEPENDENT_P (type);
11323}
11324
1fb3244a
MM
11325/* Returns TRUE if the EXPRESSION is value-dependent. */
11326
14d22dd6 11327bool
1fb3244a
MM
11328value_dependent_expression_p (tree expression)
11329{
11330 if (!processing_template_decl)
11331 return false;
11332
11333 /* A name declared with a dependent type. */
14d22dd6
MM
11334 if (TREE_CODE (expression) == LOOKUP_EXPR
11335 || (DECL_P (expression)
11336 && dependent_type_p (TREE_TYPE (expression))))
1fb3244a
MM
11337 return true;
11338 /* A non-type template parameter. */
11339 if ((TREE_CODE (expression) == CONST_DECL
11340 && DECL_TEMPLATE_PARM_P (expression))
11341 || TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
11342 return true;
11343 /* A constant with integral or enumeration type and is initialized
11344 with an expression that is value-dependent. */
11345 if (TREE_CODE (expression) == VAR_DECL
11346 && DECL_INITIAL (expression)
11347 && (CP_INTEGRAL_TYPE_P (TREE_TYPE (expression))
11348 || TREE_CODE (TREE_TYPE (expression)) == ENUMERAL_TYPE)
11349 && value_dependent_expression_p (DECL_INITIAL (expression)))
11350 return true;
11351 /* These expressions are value-dependent if the type to which the
11352 cast occurs is dependent. */
11353 if ((TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11354 || TREE_CODE (expression) == STATIC_CAST_EXPR
11355 || TREE_CODE (expression) == CONST_CAST_EXPR
11356 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11357 || TREE_CODE (expression) == CAST_EXPR)
11358 && dependent_type_p (TREE_TYPE (expression)))
11359 return true;
11360 /* A `sizeof' expression where the sizeof operand is a type is
11361 value-dependent if the type is dependent. If the type was not
11362 dependent, we would no longer have a SIZEOF_EXPR, so any
11363 SIZEOF_EXPR is dependent. */
11364 if (TREE_CODE (expression) == SIZEOF_EXPR)
11365 return true;
11366 /* A constant expression is value-dependent if any subexpression is
11367 value-dependent. */
11368 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (expression))))
11369 {
11370 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
11371 {
11372 case '1':
11373 return (value_dependent_expression_p
11374 (TREE_OPERAND (expression, 0)));
11375 case '<':
11376 case '2':
11377 return ((value_dependent_expression_p
11378 (TREE_OPERAND (expression, 0)))
11379 || (value_dependent_expression_p
11380 (TREE_OPERAND (expression, 1))));
11381 case 'e':
11382 {
11383 int i;
14d22dd6
MM
11384 for (i = 0; i < first_rtl_op (TREE_CODE (expression)); ++i)
11385 /* In some cases, some of the operands may be missing.
11386 (For example, in the case of PREDECREMENT_EXPR, the
11387 amount to increment by may be missing.) That doesn't
11388 make the expression dependent. */
11389 if (TREE_OPERAND (expression, i)
11390 && (value_dependent_expression_p
11391 (TREE_OPERAND (expression, i))))
1fb3244a
MM
11392 return true;
11393 return false;
11394 }
11395 }
11396 }
11397
11398 /* The expression is not value-dependent. */
11399 return false;
11400}
11401
11402/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
11403 [temp.dep.expr]. */
11404
11405bool
11406type_dependent_expression_p (expression)
11407 tree expression;
11408{
11409 if (!processing_template_decl)
11410 return false;
11411
11412 /* Some expression forms are never type-dependent. */
11413 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
11414 || TREE_CODE (expression) == SIZEOF_EXPR
11415 || TREE_CODE (expression) == ALIGNOF_EXPR
11416 || TREE_CODE (expression) == TYPEID_EXPR
11417 || TREE_CODE (expression) == DELETE_EXPR
11418 || TREE_CODE (expression) == VEC_DELETE_EXPR
11419 || TREE_CODE (expression) == THROW_EXPR)
11420 return false;
11421
11422 /* The types of these expressions depends only on the type to which
11423 the cast occurs. */
11424 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
11425 || TREE_CODE (expression) == STATIC_CAST_EXPR
11426 || TREE_CODE (expression) == CONST_CAST_EXPR
11427 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
11428 || TREE_CODE (expression) == CAST_EXPR)
11429 return dependent_type_p (TREE_TYPE (expression));
11430 /* The types of these expressions depends only on the type created
11431 by the expression. */
11432 else if (TREE_CODE (expression) == NEW_EXPR
11433 || TREE_CODE (expression) == VEC_NEW_EXPR)
11434 return dependent_type_p (TREE_OPERAND (expression, 1));
11435
11436 if (TREE_CODE (expression) == FUNCTION_DECL
11437 && DECL_LANG_SPECIFIC (expression)
11438 && DECL_TEMPLATE_INFO (expression)
11439 && (dependent_template_id_p
11440 (DECL_TI_TEMPLATE (expression),
11441 INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
11442 return true;
11443
11444 return (dependent_type_p (TREE_TYPE (expression)));
11445}
11446
11447/* Returns TRUE if the ARG (a template argument) is dependent. */
11448
11449bool
11450dependent_template_arg_p (tree arg)
11451{
11452 if (!processing_template_decl)
11453 return false;
11454
11455 if (TREE_CODE (arg) == TEMPLATE_DECL
11456 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
11457 return dependent_template_p (arg);
11458 else if (TYPE_P (arg))
11459 return dependent_type_p (arg);
11460 else
11461 return (type_dependent_expression_p (arg)
11462 || value_dependent_expression_p (arg));
11463}
11464
11465/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
11466
11467static bool
11468dependent_template_id_p (tree tmpl, tree args)
11469{
11470 int i;
11471
11472 if (dependent_template_p (tmpl))
11473 return true;
11474 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
11475 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
11476 return true;
11477 return false;
11478}
11479
11480/* Returns TRUE if the template TMPL is dependent. */
11481
11482bool
11483dependent_template_p (tree tmpl)
11484{
11485 /* Template template parameters are dependent. */
11486 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
11487 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
11488 return true;
11489 /* So are member templates of dependent classes. */
11490 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
11491 return dependent_type_p (DECL_CONTEXT (tmpl));
11492 return false;
11493}
11494
14d22dd6
MM
11495/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
11496 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
11497 can be found. Note that this function peers inside uninstantiated
11498 templates and therefore should be used only in extremely limited
11499 situations. */
11500
11501tree
11502resolve_typename_type (tree type, bool only_current_p)
11503{
11504 tree scope;
11505 tree name;
11506 tree decl;
11507 int quals;
11508
11509 my_friendly_assert (TREE_CODE (type) == TYPENAME_TYPE,
11510 20010702);
11511
11512 scope = TYPE_CONTEXT (type);
11513 name = TYPE_IDENTIFIER (type);
11514
11515 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
11516 it first before we can figure out what NAME refers to. */
11517 if (TREE_CODE (scope) == TYPENAME_TYPE)
11518 scope = resolve_typename_type (scope, only_current_p);
11519 /* If we don't know what SCOPE refers to, then we cannot resolve the
11520 TYPENAME_TYPE. */
11521 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
11522 return error_mark_node;
11523 /* If the SCOPE is a template type parameter, we have no way of
11524 resolving the name. */
11525 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
11526 return type;
11527 /* If the SCOPE is not the current instantiation, there's no reason
11528 to look inside it. */
11529 if (only_current_p && !currently_open_class (scope))
11530 return error_mark_node;
ca099ac8
MM
11531 /* If SCOPE is a partial instantiation, it will not have a valid
11532 TYPE_FIELDS list, so use the original template. */
11533 if (CLASSTYPE_USE_TEMPLATE (scope))
11534 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
14d22dd6
MM
11535 /* Enter the SCOPE so that name lookup will be resolved as if we
11536 were in the class definition. In particular, SCOPE will no
11537 longer be considered a dependent type. */
11538 push_scope (scope);
11539 /* Look up the declaration. */
86ac0575 11540 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
14d22dd6
MM
11541 /* Obtain the set of qualifiers applied to the TYPE. */
11542 quals = cp_type_quals (type);
11543 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
11544 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
11545 if (!decl)
11546 type = error_mark_node;
11547 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
11548 && TREE_CODE (decl) == TYPE_DECL)
11549 type = TREE_TYPE (decl);
11550 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
11551 && DECL_CLASS_TEMPLATE_P (decl))
11552 {
11553 tree tmpl;
11554 tree args;
11555 /* Obtain the template and the arguments. */
11556 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
11557 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
11558 /* Instantiate the template. */
11559 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
11560 /*entering_scope=*/0,
11561 tf_error);
11562 }
11563 else
11564 type = error_mark_node;
11565 /* Qualify the resulting type. */
11566 if (type != error_mark_node && quals)
11567 type = cp_build_qualified_type (type, quals);
11568 /* Leave the SCOPE. */
11569 pop_scope (scope);
11570
11571 return type;
11572}
11573
11574tree
11575resolve_typename_type_in_current_instantiation (tree type)
11576{
11577 tree t;
11578
11579 t = resolve_typename_type (type, /*only_current_p=*/true);
11580 return (t != error_mark_node) ? t : type;
11581}
11582
e2500fed 11583#include "gt-cp-pt.h"
This page took 3.43152 seconds and 5 git commands to generate.