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