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