]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/pt.c
invoke.texi (-fvar-tracking-assignments): New.
[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,
493e377c
PC
3 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
8d08fdba 5 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
fc378698 6 Rewritten by Jason Merrill (jason@cygnus.com).
8d08fdba 7
f5adbb8d 8This file is part of GCC.
8d08fdba 9
f5adbb8d 10GCC is free software; you can redistribute it and/or modify
8d08fdba 11it under the terms of the GNU General Public License as published by
e77f031d 12the Free Software Foundation; either version 3, or (at your option)
8d08fdba
MS
13any later version.
14
f5adbb8d 15GCC is distributed in the hope that it will be useful,
8d08fdba
MS
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
e77f031d
NC
21along with GCC; see the file COPYING3. If not see
22<http://www.gnu.org/licenses/>. */
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 34#include "tree.h"
0c58f841 35#include "pointer-set.h"
8d08fdba 36#include "flags.h"
e58a9aa1 37#include "c-common.h"
8d08fdba 38#include "cp-tree.h"
e58a9aa1 39#include "cp-objcp-common.h"
25af8512 40#include "tree-inline.h"
8d08fdba 41#include "decl.h"
e8abc66f 42#include "output.h"
49c249e1 43#include "except.h"
54f92bfb 44#include "toplev.h"
3dcaad8b 45#include "rtl.h"
297a5329 46#include "timevar.h"
325c3691 47#include "tree-iterator.h"
53824026 48#include "vecprim.h"
49c249e1 49
050367a3
MM
50/* The type of functions taking a tree, and some additional data, and
51 returning an int. */
3a978d72 52typedef int (*tree_fn_t) (tree, void*);
050367a3 53
0aafb128
MM
54/* The PENDING_TEMPLATES is a TREE_LIST of templates whose
55 instantiations have been deferred, either because their definitions
e2c3721c 56 were not yet available, or because we were putting off doing the work. */
d1b38208 57struct GTY (()) pending_template {
e2c3721c
PB
58 struct pending_template *next;
59 struct tinst_level *tinst;
60};
61
62static GTY(()) struct pending_template *pending_templates;
63static GTY(()) struct pending_template *last_pending_template;
73aad9b9 64
67ffc812 65int processing_template_parmlist;
386b8a85
JM
66static int template_header_count;
67
e2500fed 68static GTY(()) tree saved_trees;
53824026 69static VEC(int,heap) *inline_parm_levels;
75650646 70
e2c3721c 71static GTY(()) struct tinst_level *current_tinst_level;
3ae18eaf 72
2b59fc25
KL
73static GTY(()) tree saved_access_scope;
74
0fe0caa6
RH
75/* Live only within one (recursive) call to tsubst_expr. We use
76 this to pass the statement expression node from the STMT_EXPR
77 to the EXPR_STMT that is its result. */
78static tree cur_stmt_expr;
79
6dfbb909
MM
80/* A map from local variable declarations in the body of the template
81 presently being instantiated to the corresponding instantiated
82 local variables. */
83static htab_t local_specializations;
84
2b110bfc
JM
85typedef struct GTY(()) spec_entry
86{
87 tree tmpl;
88 tree args;
89 tree spec;
90} spec_entry;
91
92static GTY ((param_is (spec_entry)))
93 htab_t decl_specializations;
94
95static GTY ((param_is (spec_entry)))
96 htab_t type_specializations;
97
410cf6e6 98/* Contains canonical template parameter types. The vector is indexed by
06d40de8
DG
99 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
100 TREE_LIST, whose TREE_VALUEs contain the canonical template
101 parameters of various types and levels. */
102static GTY(()) VEC(tree,gc) *canonical_template_parms;
103
830bfa74
MM
104#define UNIFY_ALLOW_NONE 0
105#define UNIFY_ALLOW_MORE_CV_QUAL 1
106#define UNIFY_ALLOW_LESS_CV_QUAL 2
107#define UNIFY_ALLOW_DERIVED 4
161c12b0 108#define UNIFY_ALLOW_INTEGER 8
028d1f20 109#define UNIFY_ALLOW_OUTER_LEVEL 16
62e4a758
NS
110#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
111#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
830bfa74 112
3a978d72
NN
113static void push_access_scope (tree);
114static void pop_access_scope (tree);
acde59b4
MM
115static bool resolve_overloaded_unification (tree, tree, tree, tree,
116 unification_kind_t, int);
3a978d72 117static int try_one_overload (tree, tree, tree, tree, tree,
f23fb7f5 118 unification_kind_t, int, bool);
3a978d72
NN
119static int unify (tree, tree, tree, tree, int);
120static void add_pending_template (tree);
aa9d8196
VR
121static int push_tinst_level (tree);
122static void pop_tinst_level (void);
e2c3721c 123static tree reopen_tinst_level (struct tinst_level *);
3a978d72 124static tree tsubst_initializer_list (tree, tree);
3a978d72 125static tree get_class_bindings (tree, tree, tree);
3db45ab5 126static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
e7e93965 127 bool, bool);
3a978d72
NN
128static void tsubst_enum (tree, tree, tree);
129static tree add_to_template_args (tree, tree);
130static tree add_outermost_template_args (tree, tree);
131static bool check_instantiated_args (tree, tree, tsubst_flags_t);
8af2fec4
RY
132static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
133 tree);
c166b898
ILT
134static int type_unification_real (tree, tree, tree, const tree *,
135 unsigned int, int, unification_kind_t, int);
3a978d72 136static void note_template_header (int);
b6ab6892 137static tree convert_nontype_argument_function (tree, tree);
3a978d72
NN
138static tree convert_nontype_argument (tree, tree);
139static tree convert_template_argument (tree, tree, tree,
140 tsubst_flags_t, int, tree);
0c58f841 141static int for_each_template_parm (tree, tree_fn_t, void*,
c095a4f8 142 struct pointer_set_t*, bool);
5d80a306 143static tree expand_template_argument_pack (tree);
3a978d72 144static tree build_template_parm_index (int, int, int, tree, tree);
ae95e46e 145static bool inline_needs_template_parms (tree);
3a978d72 146static void push_inline_template_parms_recursive (tree, int);
3a978d72 147static tree retrieve_local_specialization (tree);
3a978d72 148static void register_local_specialization (tree, tree);
2b110bfc 149static hashval_t hash_specialization (const void *p);
ef3b7b17 150static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
3a978d72
NN
151static int mark_template_parm (tree, void *);
152static int template_parm_this_level_p (tree, void *);
153static tree tsubst_friend_function (tree, tree);
154static tree tsubst_friend_class (tree, tree);
155static int can_complete_type_without_circularity (tree);
a34d3336 156static tree get_bindings (tree, tree, tree, bool);
3a978d72
NN
157static int template_decl_level (tree);
158static int check_cv_quals_for_unify (int, tree, tree);
5d80a306
DG
159static void template_parm_level_and_index (tree, int*, int*);
160static int unify_pack_expansion (tree, tree, tree, tree, int, bool, bool);
a91db711
NS
161static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
162static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
3a978d72
NN
163static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
164static void regenerate_decl_from_template (tree, tree);
3a978d72 165static tree most_specialized_class (tree, tree);
3a978d72 166static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
3a978d72
NN
167static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
168static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
9b910171 169static bool check_specialization_scope (void);
3a978d72
NN
170static tree process_partial_specialization (tree);
171static void set_current_access_from_decl (tree);
3a978d72 172static tree get_template_base (tree, tree, tree, tree);
3a978d72
NN
173static tree try_class_unification (tree, tree, tree, tree);
174static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
175 tree, tree);
4cf36211 176static bool template_template_parm_bindings_ok_p (tree, tree);
3a978d72
NN
177static int template_args_equal (tree, tree);
178static void tsubst_default_arguments (tree);
179static tree for_each_template_parm_r (tree *, int *, void *);
180static tree copy_default_args_to_explicit_spec_1 (tree, tree);
181static void copy_default_args_to_explicit_spec (tree);
182static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
a723baf1 183static int eq_local_specializations (const void *, const void *);
5d80a306 184static bool dependent_template_arg_p (tree);
06d40de8 185static bool any_template_arguments_need_structural_equality_p (tree);
5552b43c 186static bool dependent_type_p_r (tree);
015c2c66 187static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
14d22dd6 188static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
5d80a306 189static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
6afcfe0a 190static tree tsubst_decl (tree, tree, tsubst_flags_t);
d0940d56 191static void perform_typedefs_access_check (tree tmpl, tree targs);
2eb25c98 192static void append_type_to_template_for_access_check_1 (tree, tree, tree);
2b110bfc 193static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
f9329c35 194static bool primary_template_instantiation_p (const_tree);
36a117a5 195
2b59fc25
KL
196/* Make the current scope suitable for access checking when we are
197 processing T. T can be FUNCTION_DECL for instantiated function
2b907f5c
KL
198 template, or VAR_DECL for static member variable (need by
199 instantiate_decl). */
2b59fc25 200
8ce33230 201static void
2b907f5c 202push_access_scope (tree t)
2b59fc25 203{
50bc768d
NS
204 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
205 || TREE_CODE (t) == VAR_DECL);
2b59fc25 206
23ff7e2d
KL
207 if (DECL_FRIEND_CONTEXT (t))
208 push_nested_class (DECL_FRIEND_CONTEXT (t));
209 else if (DECL_CLASS_SCOPE_P (t))
2b907f5c 210 push_nested_class (DECL_CONTEXT (t));
0f399e5f
KL
211 else
212 push_to_top_level ();
c8094d83 213
2b907f5c 214 if (TREE_CODE (t) == FUNCTION_DECL)
0f399e5f
KL
215 {
216 saved_access_scope = tree_cons
217 (NULL_TREE, current_function_decl, saved_access_scope);
218 current_function_decl = t;
219 }
2b59fc25
KL
220}
221
2b59fc25
KL
222/* Restore the scope set up by push_access_scope. T is the node we
223 are processing. */
224
8ce33230 225static void
3a978d72 226pop_access_scope (tree t)
2b59fc25 227{
2b907f5c 228 if (TREE_CODE (t) == FUNCTION_DECL)
2b59fc25
KL
229 {
230 current_function_decl = TREE_VALUE (saved_access_scope);
231 saved_access_scope = TREE_CHAIN (saved_access_scope);
232 }
0f399e5f 233
23ff7e2d 234 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
0f399e5f
KL
235 pop_nested_class ();
236 else
237 pop_from_top_level ();
2b59fc25
KL
238}
239
a723baf1
MM
240/* Do any processing required when DECL (a member template
241 declaration) is finished. Returns the TEMPLATE_DECL corresponding
242 to DECL, unless it is a specialization, in which case the DECL
243 itself is returned. */
e1467ff2
MM
244
245tree
3a978d72 246finish_member_template_decl (tree decl)
e1467ff2 247{
a723baf1
MM
248 if (decl == error_mark_node)
249 return error_mark_node;
250
50bc768d 251 gcc_assert (DECL_P (decl));
a723baf1
MM
252
253 if (TREE_CODE (decl) == TYPE_DECL)
93cdc044 254 {
a723baf1
MM
255 tree type;
256
257 type = TREE_TYPE (decl);
92fab505
DG
258 if (type == error_mark_node)
259 return error_mark_node;
9e1e64ec 260 if (MAYBE_CLASS_TYPE_P (type)
a723baf1
MM
261 && CLASSTYPE_TEMPLATE_INFO (type)
262 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
93cdc044 263 {
a723baf1 264 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
93cdc044
JM
265 check_member_template (tmpl);
266 return tmpl;
267 }
8d019cef 268 return NULL_TREE;
93cdc044 269 }
07c88314 270 else if (TREE_CODE (decl) == FIELD_DECL)
0f51ccfc 271 error ("data member %qD cannot be a member template", decl);
a1da6cba 272 else if (DECL_TEMPLATE_INFO (decl))
e1467ff2 273 {
a1da6cba
MM
274 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
275 {
276 check_member_template (DECL_TI_TEMPLATE (decl));
277 return DECL_TI_TEMPLATE (decl);
278 }
279 else
280 return decl;
c8094d83 281 }
a1da6cba 282 else
0f51ccfc 283 error ("invalid member template declaration %qD", decl);
e1467ff2 284
a1da6cba 285 return error_mark_node;
f84b4be9 286}
e1467ff2 287
91a77d68
JM
288/* Return the template info node corresponding to T, whatever T is. */
289
290tree
f9329c35 291get_template_info (const_tree t)
91a77d68
JM
292{
293 tree tinfo = NULL_TREE;
294
295 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
296 tinfo = DECL_TEMPLATE_INFO (t);
297
298 if (!tinfo && TREE_CODE (t) == TYPE_DECL)
299 t = TREE_TYPE (t);
300
301 if (TAGGED_TYPE_P (t))
302 tinfo = TYPE_TEMPLATE_INFO (t);
303
304 return tinfo;
305}
306
f84b4be9 307/* Returns the template nesting level of the indicated class TYPE.
c8094d83 308
f84b4be9
JM
309 For example, in:
310 template <class T>
311 struct A
312 {
313 template <class U>
314 struct B {};
315 };
316
c8094d83 317 A<T>::B<U> has depth two, while A<T> has depth one.
39c01e4c 318 Both A<T>::B<int> and A<int>::B<U> have depth one, if
260cd73f 319 they are instantiations, not specializations.
39c01e4c
MM
320
321 This function is guaranteed to return 0 if passed NULL_TREE so
322 that, for example, `template_class_depth (current_class_type)' is
323 always safe. */
f84b4be9 324
260cd73f
VR
325int
326template_class_depth (tree type)
f84b4be9 327{
93cdc044 328 int depth;
f84b4be9 329
c8094d83 330 for (depth = 0;
ed44da02 331 type && TREE_CODE (type) != NAMESPACE_DECL;
c8094d83 332 type = (TREE_CODE (type) == FUNCTION_DECL)
4f1c5b7d 333 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
ed44da02 334 {
91a77d68
JM
335 tree tinfo = get_template_info (type);
336
337 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
338 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
339 ++depth;
ed44da02 340 }
f84b4be9
JM
341
342 return depth;
e1467ff2 343}
98c1c668 344
ae95e46e
PC
345/* Subroutine of maybe_begin_member_template_processing.
346 Returns true if processing DECL needs us to push template parms. */
cae40af6 347
ae95e46e 348static bool
3a978d72 349inline_needs_template_parms (tree decl)
cae40af6
JM
350{
351 if (! DECL_TEMPLATE_INFO (decl))
ae95e46e 352 return false;
f84b4be9 353
36a117a5 354 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
cae40af6
JM
355 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
356}
357
358/* Subroutine of maybe_begin_member_template_processing.
359 Push the template parms in PARMS, starting from LEVELS steps into the
360 chain, and ending at the beginning, since template parms are listed
361 innermost first. */
362
363static void
3a978d72 364push_inline_template_parms_recursive (tree parmlist, int levels)
cae40af6
JM
365{
366 tree parms = TREE_VALUE (parmlist);
367 int i;
368
369 if (levels > 1)
370 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
786b5245 371
98c1c668 372 ++processing_template_decl;
cae40af6 373 current_template_parms
4890c2f4 374 = tree_cons (size_int (processing_template_decl),
98c1c668 375 parms, current_template_parms);
cae40af6
JM
376 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
377
ac20c67a 378 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
0cbd7506 379 NULL);
c8094d83 380 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
98c1c668 381 {
0f67a82f 382 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
42b304f1 383
0f67a82f
LM
384 if (parm == error_mark_node)
385 continue;
42b304f1 386
50bc768d 387 gcc_assert (DECL_P (parm));
cae40af6 388
98c1c668
JM
389 switch (TREE_CODE (parm))
390 {
786b5245 391 case TYPE_DECL:
73b0fce8 392 case TEMPLATE_DECL:
98c1c668
JM
393 pushdecl (parm);
394 break;
786b5245
MM
395
396 case PARM_DECL:
397 {
fc03edb3
MM
398 /* Make a CONST_DECL as is done in process_template_parm.
399 It is ugly that we recreate this here; the original
400 version built in process_template_parm is no longer
401 available. */
c2255bc4
AH
402 tree decl = build_decl (DECL_SOURCE_LOCATION (parm),
403 CONST_DECL, DECL_NAME (parm),
786b5245 404 TREE_TYPE (parm));
c727aa5e 405 DECL_ARTIFICIAL (decl) = 1;
6de9cd9a 406 TREE_CONSTANT (decl) = 1;
6de9cd9a 407 TREE_READONLY (decl) = 1;
786b5245 408 DECL_INITIAL (decl) = DECL_INITIAL (parm);
cd9f6678 409 SET_DECL_TEMPLATE_PARM_P (decl);
786b5245
MM
410 pushdecl (decl);
411 }
cae40af6 412 break;
786b5245 413
98c1c668 414 default:
315fb5db 415 gcc_unreachable ();
98c1c668
JM
416 }
417 }
418}
419
cae40af6
JM
420/* Restore the template parameter context for a member template or
421 a friend template defined in a class definition. */
422
423void
3a978d72 424maybe_begin_member_template_processing (tree decl)
cae40af6
JM
425{
426 tree parms;
3dcaad8b 427 int levels = 0;
cae40af6 428
3dcaad8b
MM
429 if (inline_needs_template_parms (decl))
430 {
431 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
432 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
cae40af6 433
3dcaad8b
MM
434 if (DECL_TEMPLATE_SPECIALIZATION (decl))
435 {
436 --levels;
437 parms = TREE_CHAIN (parms);
438 }
cae40af6 439
3dcaad8b 440 push_inline_template_parms_recursive (parms, levels);
cae40af6
JM
441 }
442
3dcaad8b
MM
443 /* Remember how many levels of template parameters we pushed so that
444 we can pop them later. */
53824026 445 VEC_safe_push (int, heap, inline_parm_levels, levels);
cae40af6
JM
446}
447
1875c2b7 448/* Undo the effects of maybe_begin_member_template_processing. */
98c1c668 449
c8094d83 450void
3a978d72 451maybe_end_member_template_processing (void)
98c1c668 452{
3dcaad8b 453 int i;
53824026 454 int last;
3dcaad8b 455
53824026 456 if (VEC_length (int, inline_parm_levels) == 0)
98c1c668
JM
457 return;
458
53824026
KH
459 last = VEC_pop (int, inline_parm_levels);
460 for (i = 0; i < last; ++i)
cae40af6
JM
461 {
462 --processing_template_decl;
463 current_template_parms = TREE_CHAIN (current_template_parms);
464 poplevel (0, 0, 0);
465 }
98c1c668
JM
466}
467
36a117a5 468/* Return a new template argument vector which contains all of ARGS,
f9a7ae04 469 but has as its innermost set of arguments the EXTRA_ARGS. */
e6f1275f
JM
470
471static tree
3a978d72 472add_to_template_args (tree args, tree extra_args)
e6f1275f 473{
36a117a5
MM
474 tree new_args;
475 int extra_depth;
476 int i;
477 int j;
e6f1275f 478
36a117a5 479 extra_depth = TMPL_ARGS_DEPTH (extra_args);
f31c0a32 480 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
e6f1275f 481
36a117a5
MM
482 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
483 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
e6f1275f 484
36a117a5
MM
485 for (j = 1; j <= extra_depth; ++j, ++i)
486 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
c8094d83 487
e6f1275f
JM
488 return new_args;
489}
490
36a117a5
MM
491/* Like add_to_template_args, but only the outermost ARGS are added to
492 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
493 (EXTRA_ARGS) levels are added. This function is used to combine
494 the template arguments from a partial instantiation with the
495 template arguments used to attain the full instantiation from the
496 partial instantiation. */
98c1c668 497
4966381a 498static tree
3a978d72 499add_outermost_template_args (tree args, tree extra_args)
98c1c668
JM
500{
501 tree new_args;
502
e4a84209
MM
503 /* If there are more levels of EXTRA_ARGS than there are ARGS,
504 something very fishy is going on. */
50bc768d 505 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
e4a84209
MM
506
507 /* If *all* the new arguments will be the EXTRA_ARGS, just return
508 them. */
509 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
510 return extra_args;
511
36a117a5
MM
512 /* For the moment, we make ARGS look like it contains fewer levels. */
513 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
c8094d83 514
36a117a5 515 new_args = add_to_template_args (args, extra_args);
98c1c668 516
36a117a5
MM
517 /* Now, we restore ARGS to its full dimensions. */
518 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
98c1c668
JM
519
520 return new_args;
521}
5566b478 522
f9a7ae04
MM
523/* Return the N levels of innermost template arguments from the ARGS. */
524
525tree
3a978d72 526get_innermost_template_args (tree args, int n)
f9a7ae04
MM
527{
528 tree new_args;
529 int extra_levels;
530 int i;
531
50bc768d 532 gcc_assert (n >= 0);
f9a7ae04
MM
533
534 /* If N is 1, just return the innermost set of template arguments. */
535 if (n == 1)
536 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
c8094d83 537
f9a7ae04
MM
538 /* If we're not removing anything, just return the arguments we were
539 given. */
540 extra_levels = TMPL_ARGS_DEPTH (args) - n;
50bc768d 541 gcc_assert (extra_levels >= 0);
f9a7ae04
MM
542 if (extra_levels == 0)
543 return args;
544
545 /* Make a new set of arguments, not containing the outer arguments. */
546 new_args = make_tree_vec (n);
547 for (i = 1; i <= n; ++i)
c8094d83 548 SET_TMPL_ARGS_LEVEL (new_args, i,
f9a7ae04
MM
549 TMPL_ARGS_LEVEL (args, i + extra_levels));
550
551 return new_args;
552}
553
dc28490d
JM
554/* The inverse of get_innermost_template_args: Return all but the innermost
555 EXTRA_LEVELS levels of template arguments from the ARGS. */
556
557static tree
558strip_innermost_template_args (tree args, int extra_levels)
559{
560 tree new_args;
561 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
562 int i;
563
564 gcc_assert (n >= 0);
565
566 /* If N is 1, just return the outermost set of template arguments. */
567 if (n == 1)
568 return TMPL_ARGS_LEVEL (args, 1);
569
570 /* If we're not removing anything, just return the arguments we were
571 given. */
572 gcc_assert (extra_levels >= 0);
573 if (extra_levels == 0)
574 return args;
575
576 /* Make a new set of arguments, not containing the inner arguments. */
577 new_args = make_tree_vec (n);
578 for (i = 1; i <= n; ++i)
579 SET_TMPL_ARGS_LEVEL (new_args, i,
580 TMPL_ARGS_LEVEL (args, i));
581
582 return new_args;
583}
584
5566b478
MS
585/* We've got a template header coming up; push to a new level for storing
586 the parms. */
8d08fdba 587
8d08fdba 588void
3a978d72 589begin_template_parm_list (void)
8d08fdba 590{
6757edfe
MM
591 /* We use a non-tag-transparent scope here, which causes pushtag to
592 put tags in this scope, rather than in the enclosing class or
593 namespace scope. This is the right thing, since we want
594 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
595 global template class, push_template_decl handles putting the
596 TEMPLATE_DECL into top-level scope. For a nested template class,
597 e.g.:
598
599 template <class T> struct S1 {
0cbd7506 600 template <class T> struct S2 {};
6757edfe
MM
601 };
602
603 pushtag contains special code to call pushdecl_with_scope on the
604 TEMPLATE_DECL for S2. */
ac20c67a 605 begin_scope (sk_template_parms, NULL);
5156628f 606 ++processing_template_decl;
67ffc812 607 ++processing_template_parmlist;
386b8a85
JM
608 note_template_header (0);
609}
610
6c30752f 611/* This routine is called when a specialization is declared. If it is
9b910171
LM
612 invalid to declare a specialization here, an error is reported and
613 false is returned, otherwise this routine will return true. */
6c30752f 614
9b910171 615static bool
3a978d72 616check_specialization_scope (void)
6c30752f
MM
617{
618 tree scope = current_scope ();
3ddfb0e6 619
c8094d83
MS
620 /* [temp.expl.spec]
621
6c30752f
MM
622 An explicit specialization shall be declared in the namespace of
623 which the template is a member, or, for member templates, in the
624 namespace of which the enclosing class or enclosing class
625 template is a member. An explicit specialization of a member
626 function, member class or static data member of a class template
627 shall be declared in the namespace of which the class template
628 is a member. */
629 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
9b910171
LM
630 {
631 error ("explicit specialization in non-namespace scope %qD", scope);
632 return false;
633 }
3ddfb0e6 634
c8094d83 635 /* [temp.expl.spec]
6c30752f
MM
636
637 In an explicit specialization declaration for a member of a class
638 template or a member template that appears in namespace scope,
639 the member template and some of its enclosing class templates may
640 remain unspecialized, except that the declaration shall not
641 explicitly specialize a class member template if its enclosing
642 class templates are not explicitly specialized as well. */
c8094d83 643 if (current_template_parms)
9b910171
LM
644 {
645 error ("enclosing class templates are not explicitly specialized");
646 return false;
647 }
648
649 return true;
6c30752f
MM
650}
651
c6002625 652/* We've just seen template <>. */
386b8a85 653
9b910171 654bool
3a978d72 655begin_specialization (void)
386b8a85 656{
ac20c67a 657 begin_scope (sk_template_spec, NULL);
386b8a85 658 note_template_header (1);
9b910171 659 return check_specialization_scope ();
386b8a85
JM
660}
661
dc957d14 662/* Called at then end of processing a declaration preceded by
386b8a85
JM
663 template<>. */
664
c8094d83 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
c8094d83 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{
50bc768d 697 gcc_assert (!processing_explicit_instantiation);
a723baf1 698 processing_explicit_instantiation = true;
75650646 699}
386b8a85 700
386b8a85 701
75650646 702void
3a978d72 703end_explicit_instantiation (void)
75650646 704{
50bc768d 705 gcc_assert (processing_explicit_instantiation);
a723baf1 706 processing_explicit_instantiation = false;
75650646 707}
386b8a85 708
13a44ee0 709/* An explicit specialization or partial specialization TMPL is being
b1cc95ce
MM
710 declared. Check that the namespace in which the specialization is
711 occurring is permissible. Returns false iff it is invalid to
712 specialize TMPL in the current namespace. */
c8094d83 713
b1cc95ce
MM
714static bool
715check_specialization_namespace (tree tmpl)
716{
717 tree tpl_ns = decl_namespace_context (tmpl);
718
719 /* [tmpl.expl.spec]
c8094d83 720
b1cc95ce
MM
721 An explicit specialization shall be declared in the namespace of
722 which the template is a member, or, for member templates, in the
723 namespace of which the enclosing class or enclosing class
724 template is a member. An explicit specialization of a member
725 function, member class or static data member of a class template
726 shall be declared in the namespace of which the class template is
727 a member. */
728 if (is_associated_namespace (current_namespace, tpl_ns))
729 /* Same or super-using namespace. */
730 return true;
731 else
732 {
cbe5f3b3
MLI
733 permerror (input_location, "specialization of %qD in different namespace", tmpl);
734 permerror (input_location, " from definition of %q+#D", tmpl);
b1cc95ce
MM
735 return false;
736 }
737}
738
9c12301f
MM
739/* SPEC is an explicit instantiation. Check that it is valid to
740 perform this explicit instantiation in the current namespace. */
741
742static void
743check_explicit_instantiation_namespace (tree spec)
744{
745 tree ns;
746
747 /* DR 275: An explicit instantiation shall appear in an enclosing
3db45ab5 748 namespace of its template. */
9c12301f
MM
749 ns = decl_namespace_context (spec);
750 if (!is_ancestor (current_namespace, ns))
cbe5f3b3 751 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
37ec60ed
JW
752 "(which does not enclose namespace %qD)",
753 spec, current_namespace, ns);
9c12301f
MM
754}
755
36a117a5
MM
756/* The TYPE is being declared. If it is a template type, that means it
757 is a partial specialization. Do appropriate error-checking. */
758
9ce387d9 759tree
3a978d72 760maybe_process_partial_specialization (tree type)
36a117a5 761{
c9043d03
VR
762 tree context;
763
764 if (type == error_mark_node)
9ce387d9
VR
765 return error_mark_node;
766
767 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
768 {
769 error ("name of class shadows template template parameter %qD",
770 TYPE_NAME (type));
771 return error_mark_node;
772 }
c9043d03
VR
773
774 context = TYPE_CONTEXT (type);
6e049fcd 775
a723baf1 776 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
36a117a5 777 {
6e049fcd
KL
778 /* This is for ordinary explicit specialization and partial
779 specialization of a template class such as:
780
781 template <> class C<int>;
782
783 or:
784
785 template <class T> class C<T*>;
786
787 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
788
36a117a5 789 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
d0f062fb 790 && !COMPLETE_TYPE_P (type))
36a117a5 791 {
b1cc95ce 792 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
370af2d5 793 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
36a117a5 794 if (processing_template_decl)
c5285718
PC
795 {
796 if (push_template_decl (TYPE_MAIN_DECL (type))
797 == error_mark_node)
798 return error_mark_node;
799 }
36a117a5
MM
800 }
801 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
0f51ccfc 802 error ("specialization of %qT after instantiation", type);
36a117a5 803 }
6e049fcd
KL
804 else if (CLASS_TYPE_P (type)
805 && !CLASSTYPE_USE_TEMPLATE (type)
806 && CLASSTYPE_TEMPLATE_INFO (type)
807 && context && CLASS_TYPE_P (context)
808 && CLASSTYPE_TEMPLATE_INFO (context))
809 {
810 /* This is for an explicit specialization of member class
811 template according to [temp.expl.spec/18]:
812
813 template <> template <class U> class C<int>::D;
814
815 The context `C<int>' must be an implicit instantiation.
816 Otherwise this is just a member class template declared
817 earlier like:
818
819 template <> class C<int> { template <class U> class D; };
820 template <> template <class U> class C<int>::D;
821
822 In the first case, `C<int>::D' is a specialization of `C<T>::D'
823 while in the second case, `C<int>::D' is a primary template
824 and `C<T>::D' may not exist. */
825
826 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
827 && !COMPLETE_TYPE_P (type))
828 {
829 tree t;
2b110bfc 830 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
6e049fcd
KL
831
832 if (current_namespace
2b110bfc 833 != decl_namespace_context (tmpl))
6e049fcd 834 {
cbe5f3b3 835 permerror (input_location, "specializing %q#T in different namespace", type);
2b110bfc 836 permerror (input_location, " from definition of %q+#D", tmpl);
6e049fcd
KL
837 }
838
839 /* Check for invalid specialization after instantiation:
840
841 template <> template <> class C<int>::D<int>;
842 template <> template <class U> class C<int>::D; */
843
2b110bfc 844 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
6e049fcd 845 t; t = TREE_CHAIN (t))
2b110bfc
JM
846 {
847 tree inst = TREE_VALUE (t);
848 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst))
849 {
850 /* We already have a full specialization of this partial
851 instantiation. Reassign it to the new member
852 specialization template. */
853 spec_entry elt;
854 spec_entry **slot;
855
856 elt.tmpl = most_general_template (tmpl);
857 elt.args = CLASSTYPE_TI_ARGS (inst);
858 elt.spec = inst;
859
860 htab_remove_elt (type_specializations, &elt);
861
862 elt.tmpl = tmpl;
863 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
864
865 slot = (spec_entry **)
866 htab_find_slot (type_specializations, &elt, INSERT);
867 *slot = GGC_NEW (spec_entry);
868 **slot = elt;
869 }
870 else if (COMPLETE_TYPE_P (inst) || TYPE_BEING_DEFINED (inst))
871 /* But if we've had an implicit instantiation, that's a
872 problem ([temp.expl.spec]/6). */
873 error ("specialization %qT after instantiation %qT",
874 type, inst);
875 }
6e049fcd
KL
876
877 /* Mark TYPE as a specialization. And as a result, we only
878 have one level of template argument for the innermost
879 class template. */
880 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
881 CLASSTYPE_TI_ARGS (type)
882 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
883 }
884 }
20496fa2 885 else if (processing_specialization)
9ce387d9
VR
886 {
887 error ("explicit specialization of non-template %qT", type);
888 return error_mark_node;
889 }
890
891 return type;
36a117a5
MM
892}
893
ee81147e 894/* Returns nonzero if we can optimize the retrieval of specializations
c7222c02
MM
895 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
896 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
897
c8094d83 898static inline bool
c7222c02
MM
899optimize_specialization_lookup_p (tree tmpl)
900{
901 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
902 && DECL_CLASS_SCOPE_P (tmpl)
903 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
904 parameter. */
905 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1ca939e5
MM
906 /* The optimized lookup depends on the fact that the
907 template arguments for the member function template apply
908 purely to the containing class, which is not true if the
909 containing class is an explicit or partial
910 specialization. */
911 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
c7222c02
MM
912 && !DECL_MEMBER_TEMPLATE_P (tmpl)
913 && !DECL_CONV_FN_P (tmpl)
914 /* It is possible to have a template that is not a member
915 template and is not a member of a template class:
c8094d83
MS
916
917 template <typename T>
c7222c02 918 struct S { friend A::f(); };
c8094d83 919
c7222c02
MM
920 Here, the friend function is a template, but the context does
921 not have template information. The optimized lookup relies
922 on having ARGS be the template arguments for both the class
923 and the function template. */
924 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
925}
926
75650646
MM
927/* Retrieve the specialization (in the sense of [temp.spec] - a
928 specialization is either an instantiation or an explicit
929 specialization) of TMPL for the given template ARGS. If there is
930 no such specialization, return NULL_TREE. The ARGS are a vector of
931 arguments, or a vector of vectors of arguments, in the case of
c8094d83 932 templates with more than one level of parameters.
c7222c02
MM
933
934 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
935 then we search for a partial specialization matching ARGS. This
936 parameter is ignored if TMPL is not a class template. */
c8094d83 937
75650646 938static tree
2b110bfc 939retrieve_specialization (tree tmpl, tree args, hashval_t hash)
75650646 940{
2d8ba2c7
LM
941 if (args == error_mark_node)
942 return NULL_TREE;
943
50bc768d 944 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
75650646 945
36a117a5
MM
946 /* There should be as many levels of arguments as there are
947 levels of parameters. */
c8094d83 948 gcc_assert (TMPL_ARGS_DEPTH (args)
50bc768d 949 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
c8094d83 950
c7222c02
MM
951 if (optimize_specialization_lookup_p (tmpl))
952 {
953 tree class_template;
954 tree class_specialization;
d4e6fecb 955 VEC(tree,gc) *methods;
c7222c02
MM
956 tree fns;
957 int idx;
958
959 /* The template arguments actually apply to the containing
960 class. Find the class specialization with those
961 arguments. */
962 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
c8094d83 963 class_specialization
2b110bfc 964 = retrieve_specialization (class_template, args, 0);
c7222c02
MM
965 if (!class_specialization)
966 return NULL_TREE;
967 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
968 for the specialization. */
969 idx = class_method_index_for_fn (class_specialization, tmpl);
970 if (idx == -1)
971 return NULL_TREE;
972 /* Iterate through the methods with the indicated name, looking
973 for the one that has an instance of TMPL. */
974 methods = CLASSTYPE_METHOD_VEC (class_specialization);
975 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
976 {
977 tree fn = OVL_CURRENT (fns);
eb5c3f05
JM
978 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
979 /* using-declarations can add base methods to the method vec,
980 and we don't want those here. */
981 && DECL_CONTEXT (fn) == class_specialization)
c7222c02
MM
982 return fn;
983 }
984 return NULL_TREE;
985 }
986 else
987 {
2b110bfc
JM
988 spec_entry *found;
989 spec_entry elt;
990 htab_t specializations;
991
992 elt.tmpl = tmpl;
993 elt.args = args;
994 elt.spec = NULL_TREE;
995
996 if (DECL_CLASS_TEMPLATE_P (tmpl))
997 specializations = type_specializations;
c7222c02 998 else
2b110bfc 999 specializations = decl_specializations;
c8094d83 1000
2b110bfc
JM
1001 if (hash == 0)
1002 hash = hash_specialization (&elt);
1003 found = (spec_entry *) htab_find_with_hash (specializations, &elt, hash);
1004 if (found)
1005 return found->spec;
c7222c02 1006 }
75650646
MM
1007
1008 return NULL_TREE;
386b8a85
JM
1009}
1010
dc957d14 1011/* Like retrieve_specialization, but for local declarations. */
9188c363
MM
1012
1013static tree
3a978d72 1014retrieve_local_specialization (tree tmpl)
9188c363 1015{
29b0d1fd
JM
1016 tree spec;
1017
1018 if (local_specializations == NULL)
1019 return NULL_TREE;
1020
1021 spec = (tree) htab_find_with_hash (local_specializations, tmpl,
1022 htab_hash_pointer (tmpl));
a723baf1 1023 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
9188c363
MM
1024}
1025
838dfd8a 1026/* Returns nonzero iff DECL is a specialization of TMPL. */
6757edfe
MM
1027
1028int
3a978d72 1029is_specialization_of (tree decl, tree tmpl)
6757edfe
MM
1030{
1031 tree t;
1032
1033 if (TREE_CODE (decl) == FUNCTION_DECL)
1034 {
c8094d83 1035 for (t = decl;
6757edfe
MM
1036 t != NULL_TREE;
1037 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1038 if (t == tmpl)
1039 return 1;
1040 }
c8094d83 1041 else
6757edfe 1042 {
50bc768d 1043 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6757edfe
MM
1044
1045 for (t = TREE_TYPE (decl);
1046 t != NULL_TREE;
1047 t = CLASSTYPE_USE_TEMPLATE (t)
1048 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
9edc3913 1049 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
6757edfe 1050 return 1;
c8094d83 1051 }
386b8a85 1052
6757edfe
MM
1053 return 0;
1054}
75650646 1055
d43f603d 1056/* Returns nonzero iff DECL is a specialization of friend declaration
be93747e 1057 FRIEND_DECL according to [temp.friend]. */
d43f603d
KL
1058
1059bool
be93747e 1060is_specialization_of_friend (tree decl, tree friend_decl)
d43f603d
KL
1061{
1062 bool need_template = true;
1063 int template_depth;
1064
b939a023
KL
1065 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1066 || TREE_CODE (decl) == TYPE_DECL);
d43f603d 1067
be93747e 1068 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
d43f603d
KL
1069 of a template class, we want to check if DECL is a specialization
1070 if this. */
be93747e
KG
1071 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1072 && DECL_TEMPLATE_INFO (friend_decl)
1073 && !DECL_USE_TEMPLATE (friend_decl))
d43f603d 1074 {
b939a023 1075 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
be93747e 1076 friend_decl = DECL_TI_TEMPLATE (friend_decl);
d43f603d
KL
1077 need_template = false;
1078 }
be93747e
KG
1079 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1080 && !PRIMARY_TEMPLATE_P (friend_decl))
b939a023 1081 need_template = false;
d43f603d
KL
1082
1083 /* There is nothing to do if this is not a template friend. */
be93747e 1084 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
b939a023 1085 return false;
d43f603d 1086
be93747e 1087 if (is_specialization_of (decl, friend_decl))
b939a023 1088 return true;
d43f603d
KL
1089
1090 /* [temp.friend/6]
1091 A member of a class template may be declared to be a friend of a
1092 non-template class. In this case, the corresponding member of
1093 every specialization of the class template is a friend of the
1094 class granting friendship.
c8094d83 1095
d43f603d
KL
1096 For example, given a template friend declaration
1097
1098 template <class T> friend void A<T>::f();
1099
1100 the member function below is considered a friend
1101
1102 template <> struct A<int> {
1103 void f();
1104 };
1105
1106 For this type of template friend, TEMPLATE_DEPTH below will be
5c425df5 1107 nonzero. To determine if DECL is a friend of FRIEND, we first
d43f603d
KL
1108 check if the enclosing class is a specialization of another. */
1109
be93747e 1110 template_depth = template_class_depth (DECL_CONTEXT (friend_decl));
d43f603d
KL
1111 if (template_depth
1112 && DECL_CLASS_SCOPE_P (decl)
c8094d83 1113 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
be93747e 1114 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
d43f603d
KL
1115 {
1116 /* Next, we check the members themselves. In order to handle
be93747e 1117 a few tricky cases, such as when FRIEND_DECL's are
d43f603d
KL
1118
1119 template <class T> friend void A<T>::g(T t);
1120 template <class T> template <T t> friend void A<T>::h();
1121
b939a023
KL
1122 and DECL's are
1123
1124 void A<int>::g(int);
1125 template <int> void A<int>::h();
1126
1127 we need to figure out ARGS, the template arguments from
1128 the context of DECL. This is required for template substitution
1129 of `T' in the function parameter of `g' and template parameter
1130 of `h' in the above examples. Here ARGS corresponds to `int'. */
d43f603d
KL
1131
1132 tree context = DECL_CONTEXT (decl);
1133 tree args = NULL_TREE;
1134 int current_depth = 0;
b939a023 1135
d43f603d
KL
1136 while (current_depth < template_depth)
1137 {
1138 if (CLASSTYPE_TEMPLATE_INFO (context))
1139 {
1140 if (current_depth == 0)
1141 args = TYPE_TI_ARGS (context);
1142 else
1143 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1144 current_depth++;
1145 }
1146 context = TYPE_CONTEXT (context);
1147 }
1148
1149 if (TREE_CODE (decl) == FUNCTION_DECL)
1150 {
1151 bool is_template;
1152 tree friend_type;
1153 tree decl_type;
1154 tree friend_args_type;
1155 tree decl_args_type;
1156
be93747e 1157 /* Make sure that both DECL and FRIEND_DECL are templates or
d43f603d
KL
1158 non-templates. */
1159 is_template = DECL_TEMPLATE_INFO (decl)
1160 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1161 if (need_template ^ is_template)
b939a023 1162 return false;
d43f603d
KL
1163 else if (is_template)
1164 {
da1d7781 1165 /* If both are templates, check template parameter list. */
d43f603d 1166 tree friend_parms
be93747e 1167 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
d43f603d
KL
1168 args, tf_none);
1169 if (!comp_template_parms
1170 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1171 friend_parms))
b939a023 1172 return false;
d43f603d
KL
1173
1174 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1175 }
1176 else
1177 decl_type = TREE_TYPE (decl);
1178
be93747e 1179 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
d43f603d
KL
1180 tf_none, NULL_TREE);
1181 if (friend_type == error_mark_node)
b939a023 1182 return false;
d43f603d
KL
1183
1184 /* Check if return types match. */
1185 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
b939a023 1186 return false;
d43f603d
KL
1187
1188 /* Check if function parameter types match, ignoring the
1189 `this' parameter. */
1190 friend_args_type = TYPE_ARG_TYPES (friend_type);
1191 decl_args_type = TYPE_ARG_TYPES (decl_type);
be93747e 1192 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
d43f603d
KL
1193 friend_args_type = TREE_CHAIN (friend_args_type);
1194 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1195 decl_args_type = TREE_CHAIN (decl_args_type);
b939a023
KL
1196
1197 return compparms (decl_args_type, friend_args_type);
1198 }
1199 else
1200 {
1201 /* DECL is a TYPE_DECL */
1202 bool is_template;
1203 tree decl_type = TREE_TYPE (decl);
1204
be93747e 1205 /* Make sure that both DECL and FRIEND_DECL are templates or
b939a023
KL
1206 non-templates. */
1207 is_template
1208 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1209 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1210
1211 if (need_template ^ is_template)
1212 return false;
1213 else if (is_template)
1214 {
1215 tree friend_parms;
1216 /* If both are templates, check the name of the two
1217 TEMPLATE_DECL's first because is_friend didn't. */
1218 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
be93747e 1219 != DECL_NAME (friend_decl))
b939a023
KL
1220 return false;
1221
1222 /* Now check template parameter list. */
1223 friend_parms
be93747e 1224 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
b939a023
KL
1225 args, tf_none);
1226 return comp_template_parms
1227 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1228 friend_parms);
1229 }
1230 else
1231 return (DECL_NAME (decl)
be93747e 1232 == DECL_NAME (friend_decl));
d43f603d
KL
1233 }
1234 }
b939a023 1235 return false;
d43f603d
KL
1236}
1237
75650646 1238/* Register the specialization SPEC as a specialization of TMPL with
d63d5d0c
ILT
1239 the indicated ARGS. IS_FRIEND indicates whether the specialization
1240 is actually just a friend declaration. Returns SPEC, or an
1241 equivalent prior declaration, if available. */
75650646 1242
36a117a5 1243static tree
2b110bfc
JM
1244register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1245 hashval_t hash)
75650646 1246{
c7222c02 1247 tree fn;
2b110bfc
JM
1248 spec_entry **slot = NULL;
1249 spec_entry elt;
75650646 1250
2b110bfc 1251 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec));
75650646 1252
c8094d83 1253 if (TREE_CODE (spec) == FUNCTION_DECL
36a117a5
MM
1254 && uses_template_parms (DECL_TI_ARGS (spec)))
1255 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1256 register it; we want the corresponding TEMPLATE_DECL instead.
1257 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1258 the more obvious `uses_template_parms (spec)' to avoid problems
1259 with default function arguments. In particular, given
1260 something like this:
1261
0cbd7506 1262 template <class T> void f(T t1, T t = T())
36a117a5
MM
1263
1264 the default argument expression is not substituted for in an
1265 instantiation unless and until it is actually needed. */
1266 return spec;
28ba38e3 1267
2b110bfc
JM
1268 if (optimize_specialization_lookup_p (tmpl))
1269 /* We don't put these specializations in the hash table, but we might
1270 want to give an error about a mismatch. */
1271 fn = retrieve_specialization (tmpl, args, 0);
1272 else
1273 {
1274 elt.tmpl = tmpl;
1275 elt.args = args;
1276 elt.spec = spec;
1277
1278 if (hash == 0)
1279 hash = hash_specialization (&elt);
1280
1281 slot = (spec_entry **)
1282 htab_find_slot_with_hash (decl_specializations, &elt, hash, INSERT);
1283 if (*slot)
1284 fn = (*slot)->spec;
1285 else
1286 fn = NULL_TREE;
1287 }
1288
c7222c02
MM
1289 /* We can sometimes try to re-register a specialization that we've
1290 already got. In particular, regenerate_decl_from_template calls
1291 duplicate_decls which will update the specialization list. But,
1292 we'll still get called again here anyhow. It's more convenient
1293 to simply allow this than to try to prevent it. */
1294 if (fn == spec)
1295 return spec;
1296 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
22e9174f 1297 {
c7222c02 1298 if (DECL_TEMPLATE_INSTANTIATION (fn))
22e9174f 1299 {
3146f36f 1300 if (DECL_ODR_USED (fn)
c7222c02 1301 || DECL_EXPLICIT_INSTANTIATION (fn))
22e9174f 1302 {
c7222c02
MM
1303 error ("specialization of %qD after instantiation",
1304 fn);
99f4234a 1305 return error_mark_node;
22e9174f 1306 }
c7222c02 1307 else
b1cc95ce 1308 {
a39041fa 1309 tree clone;
c7222c02
MM
1310 /* This situation should occur only if the first
1311 specialization is an implicit instantiation, the
1312 second is an explicit specialization, and the
1313 implicit instantiation has not yet been used. That
1314 situation can occur if we have implicitly
1315 instantiated a member function and then specialized
1316 it later.
c8094d83 1317
c7222c02
MM
1318 We can also wind up here if a friend declaration that
1319 looked like an instantiation turns out to be a
1320 specialization:
c8094d83 1321
c7222c02
MM
1322 template <class T> void foo(T);
1323 class S { friend void foo<>(int) };
c8094d83
MS
1324 template <> void foo(int);
1325
c7222c02
MM
1326 We transform the existing DECL in place so that any
1327 pointers to it become pointers to the updated
1328 declaration.
1329
1330 If there was a definition for the template, but not
1331 for the specialization, we want this to look as if
1332 there were no definition, and vice versa. */
1333 DECL_INITIAL (fn) = NULL_TREE;
d63d5d0c 1334 duplicate_decls (spec, fn, is_friend);
a39041fa 1335 /* The call to duplicate_decls will have applied
3db45ab5 1336 [temp.expl.spec]:
a39041fa 1337
3db45ab5 1338 An explicit specialization of a function template
a39041fa 1339 is inline only if it is explicitly declared to be,
bcf51da2 1340 and independently of whether its function template
a39041fa
MM
1341 is.
1342
1343 to the primary function; now copy the inline bits to
3db45ab5 1344 the various clones. */
a39041fa 1345 FOR_EACH_CLONE (clone, fn)
99355518
JH
1346 DECL_DECLARED_INLINE_P (clone)
1347 = DECL_DECLARED_INLINE_P (fn);
9c12301f 1348 check_specialization_namespace (fn);
c8094d83 1349
b1cc95ce
MM
1350 return fn;
1351 }
22e9174f 1352 }
c7222c02
MM
1353 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1354 {
d63d5d0c 1355 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
c7222c02
MM
1356 /* Dup decl failed, but this is a new definition. Set the
1357 line number so any errors match this new
1358 definition. */
1359 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
c8094d83 1360
c7222c02
MM
1361 return fn;
1362 }
b1cc95ce 1363 }
62e3e13a
JM
1364 else if (fn)
1365 return duplicate_decls (spec, fn, is_friend);
b1cc95ce
MM
1366
1367 /* A specialization must be declared in the same namespace as the
1368 template it is specializing. */
1369 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1370 && !check_specialization_namespace (tmpl))
db10c6ec 1371 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
75650646 1372
c7222c02 1373 if (!optimize_specialization_lookup_p (tmpl))
2b110bfc
JM
1374 {
1375 gcc_assert (tmpl && args && spec);
1376 *slot = GGC_NEW (spec_entry);
1377 **slot = elt;
1378 if (TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1379 && PRIMARY_TEMPLATE_P (tmpl)
1380 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1381 /* TMPL is a forward declaration of a template function; keep a list
1382 of all specializations in case we need to reassign them to a friend
1383 template later in tsubst_friend_function. */
1384 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1385 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1386 }
36a117a5
MM
1387
1388 return spec;
1389}
1390
2b110bfc
JM
1391/* Returns true iff two spec_entry nodes are equivalent. Only compares the
1392 TMPL and ARGS members, ignores SPEC. */
1393
1394static int
1395eq_specializations (const void *p1, const void *p2)
1396{
1397 const spec_entry *e1 = (const spec_entry *)p1;
1398 const spec_entry *e2 = (const spec_entry *)p2;
1399
1400 return (e1->tmpl == e2->tmpl
1401 && comp_template_args (e1->args, e2->args));
1402}
1403
1404/* Returns a hash for a template TMPL and template arguments ARGS. */
1405
1406static hashval_t
1407hash_tmpl_and_args (tree tmpl, tree args)
1408{
1409 hashval_t val = DECL_UID (tmpl);
1410 return iterative_hash_template_arg (args, val);
1411}
1412
1413/* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1414 ignoring SPEC. */
1415
1416static hashval_t
1417hash_specialization (const void *p)
1418{
1419 const spec_entry *e = (const spec_entry *)p;
1420 return hash_tmpl_and_args (e->tmpl, e->args);
1421}
1422
1423/* Recursively calculate a hash value for a template argument ARG, for use
1424 in the hash tables of template specializations. */
1425
1426static hashval_t
1427iterative_hash_template_arg (tree arg, hashval_t val)
1428{
1429 unsigned HOST_WIDE_INT i;
1430 enum tree_code code;
1431 char tclass;
1432
1433 if (arg == NULL_TREE)
1434 return iterative_hash_object (arg, val);
1435
1436 if (!TYPE_P (arg))
1437 STRIP_NOPS (arg);
1438
1439 code = TREE_CODE (arg);
1440 tclass = TREE_CODE_CLASS (code);
1441
1442 val = iterative_hash_object (code, val);
1443
1444 switch (code)
1445 {
1446 case ERROR_MARK:
1447 return val;
1448
1449 case IDENTIFIER_NODE:
1450 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1451
1452 case TREE_VEC:
1453 {
1454 int i, len = TREE_VEC_LENGTH (arg);
1455 for (i = 0; i < len; ++i)
1456 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1457 return val;
1458 }
1459
1460 case TYPE_PACK_EXPANSION:
1461 case EXPR_PACK_EXPANSION:
1462 return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1463
1464 case ARGUMENT_PACK_SELECT:
1465 /* We can get one of these when re-hashing a previous entry in the middle
1466 of substituting into a pack expansion. Just look through it... */
1467 arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
1468 /* ...and fall through. */
1469 case TYPE_ARGUMENT_PACK:
1470 case NONTYPE_ARGUMENT_PACK:
1471 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1472
1473 case TREE_LIST:
1474 for (; arg; arg = TREE_CHAIN (arg))
1475 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1476 return val;
1477
1478 case OVERLOAD:
1479 for (; arg; arg = OVL_CHAIN (arg))
1480 val = iterative_hash_template_arg (OVL_FUNCTION (arg), val);
1481 return val;
1482
1483 case CONSTRUCTOR:
1484 {
1485 tree field, value;
1486 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1487 {
1488 val = iterative_hash_template_arg (field, val);
1489 val = iterative_hash_template_arg (value, val);
1490 }
1491 return val;
1492 }
1493
1494 case PARM_DECL:
ad909c97 1495 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
2b110bfc
JM
1496 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1497
1498 case TARGET_EXPR:
1499 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1500
1501 case PTRMEM_CST:
1502 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1503 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1504
1505 case TEMPLATE_PARM_INDEX:
1506 val = iterative_hash_template_arg
1507 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1508 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1509 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1510
1511 case TRAIT_EXPR:
1512 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1513 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1514 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1515
1516 case BASELINK:
1517 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1518 val);
1519 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1520 val);
1521
1522 case MODOP_EXPR:
1523 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1524 code = TREE_CODE (TREE_OPERAND (arg, 1));
1525 val = iterative_hash_object (code, val);
1526 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1527
1528 default:
1529 switch (tclass)
1530 {
1531 case tcc_type:
1532 if (TYPE_CANONICAL (arg))
1533 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1534 val);
1535 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1536 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1537 /* Otherwise just compare the types during lookup. */
1538 return val;
1539
1540 case tcc_declaration:
1541 case tcc_constant:
1542 return iterative_hash_expr (arg, val);
1543
1544 default:
1545 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1546 {
1547 unsigned n = TREE_OPERAND_LENGTH (arg);
1548 for (i = 0; i < n; ++i)
1549 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1550 return val;
1551 }
1552 }
1553 }
1554 gcc_unreachable ();
1555 return 0;
1556}
1557
36a117a5 1558/* Unregister the specialization SPEC as a specialization of TMPL.
b3445994 1559 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
2b110bfc
JM
1560 if the SPEC was listed as a specialization of TMPL.
1561
1562 Note that SPEC has been ggc_freed, so we can't look inside it. */
36a117a5 1563
b3445994 1564bool
2b110bfc 1565reregister_specialization (tree spec, tree tinfo, tree new_spec)
36a117a5 1566{
2b110bfc
JM
1567 spec_entry **slot;
1568 spec_entry elt;
36a117a5 1569
2b110bfc
JM
1570 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1571 elt.args = TI_ARGS (tinfo);
1572 elt.spec = NULL_TREE;
1573
1574 slot = (spec_entry **) htab_find_slot (decl_specializations, &elt, INSERT);
1575 if (*slot)
1576 {
1577 gcc_assert ((*slot)->spec == spec || (*slot)->spec == new_spec);
1578 gcc_assert (new_spec != NULL_TREE);
1579 (*slot)->spec = new_spec;
1580 return 1;
1581 }
36a117a5
MM
1582
1583 return 0;
75650646
MM
1584}
1585
a723baf1
MM
1586/* Compare an entry in the local specializations hash table P1 (which
1587 is really a pointer to a TREE_LIST) with P2 (which is really a
1588 DECL). */
1589
1590static int
1591eq_local_specializations (const void *p1, const void *p2)
1592{
741ac903 1593 return TREE_VALUE ((const_tree) p1) == (const_tree) p2;
a723baf1
MM
1594}
1595
69f794a7
MM
1596/* Hash P1, an entry in the local specializations table. */
1597
1598static hashval_t
1599hash_local_specialization (const void* p1)
1600{
741ac903 1601 return htab_hash_pointer (TREE_VALUE ((const_tree) p1));
69f794a7
MM
1602}
1603
6dfbb909
MM
1604/* Like register_specialization, but for local declarations. We are
1605 registering SPEC, an instantiation of TMPL. */
9188c363 1606
414ea4aa 1607static void
3a978d72 1608register_local_specialization (tree spec, tree tmpl)
9188c363 1609{
6dfbb909
MM
1610 void **slot;
1611
c8094d83 1612 slot = htab_find_slot_with_hash (local_specializations, tmpl,
69f794a7 1613 htab_hash_pointer (tmpl), INSERT);
a723baf1 1614 *slot = build_tree_list (spec, tmpl);
9188c363
MM
1615}
1616
9ba7a2f2
MM
1617/* TYPE is a class type. Returns true if TYPE is an explicitly
1618 specialized class. */
1619
1620bool
1621explicit_class_specialization_p (tree type)
1622{
1623 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1624 return false;
1625 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1626}
1627
e1467ff2
MM
1628/* Print the list of candidate FNS in an error message. */
1629
104bf76a 1630void
3a978d72 1631print_candidates (tree fns)
e1467ff2
MM
1632{
1633 tree fn;
1634
d8e178a0 1635 const char *str = "candidates are:";
e1467ff2
MM
1636
1637 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1638 {
d6479fe7
MM
1639 tree f;
1640
1641 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
dee15844 1642 error ("%s %+#D", str, OVL_CURRENT (f));
e1467ff2
MM
1643 str = " ";
1644 }
1645}
1646
75650646 1647/* Returns the template (one of the functions given by TEMPLATE_ID)
e1467ff2 1648 which can be specialized to match the indicated DECL with the
03017874
MM
1649 explicit template args given in TEMPLATE_ID. The DECL may be
1650 NULL_TREE if none is available. In that case, the functions in
1651 TEMPLATE_ID are non-members.
1652
838dfd8a 1653 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
03017874
MM
1654 specialization of a member template.
1655
5fe7b654
GB
1656 The TEMPLATE_COUNT is the number of references to qualifying
1657 template classes that appeared in the name of the function. See
1658 check_explicit_specialization for a more accurate description.
1659
29a1da1c
MM
1660 TSK indicates what kind of template declaration (if any) is being
1661 declared. TSK_TEMPLATE indicates that the declaration given by
1662 DECL, though a FUNCTION_DECL, has template parameters, and is
1663 therefore a template function.
1664
03017874
MM
1665 The template args (those explicitly specified and those deduced)
1666 are output in a newly created vector *TARGS_OUT.
1667
1668 If it is impossible to determine the result, an error message is
bf8f3f93 1669 issued. The error_mark_node is returned to indicate failure. */
75650646 1670
e9659ab0 1671static tree
c8094d83 1672determine_specialization (tree template_id,
0cbd7506
MS
1673 tree decl,
1674 tree* targs_out,
5fe7b654 1675 int need_member_template,
29a1da1c
MM
1676 int template_count,
1677 tmpl_spec_kind tsk)
386b8a85 1678{
03017874
MM
1679 tree fns;
1680 tree targs;
1681 tree explicit_targs;
1682 tree candidates = NULL_TREE;
7ca383e6
MM
1683 /* A TREE_LIST of templates of which DECL may be a specialization.
1684 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1685 corresponding TREE_PURPOSE is the set of template arguments that,
1686 when used to instantiate the template, would produce a function
1687 with the signature of DECL. */
03017874 1688 tree templates = NULL_TREE;
5fe7b654
GB
1689 int header_count;
1690 struct cp_binding_level *b;
386b8a85 1691
e1467ff2
MM
1692 *targs_out = NULL_TREE;
1693
728da672 1694 if (template_id == error_mark_node || decl == error_mark_node)
f2e48b67
BK
1695 return error_mark_node;
1696
1697 fns = TREE_OPERAND (template_id, 0);
03017874 1698 explicit_targs = TREE_OPERAND (template_id, 1);
f2e48b67 1699
27fafc8d
JM
1700 if (fns == error_mark_node)
1701 return error_mark_node;
1702
c6002625 1703 /* Check for baselinks. */
91e490ab 1704 if (BASELINK_P (fns))
da15dae6 1705 fns = BASELINK_FUNCTIONS (fns);
386b8a85 1706
91e490ab
MM
1707 if (!is_overloaded_fn (fns))
1708 {
0f51ccfc 1709 error ("%qD is not a function template", fns);
91e490ab
MM
1710 return error_mark_node;
1711 }
1712
5fe7b654
GB
1713 /* Count the number of template headers specified for this
1714 specialization. */
1715 header_count = 0;
c8094d83 1716 for (b = current_binding_level;
98f99d7f 1717 b->kind == sk_template_parms;
5fe7b654
GB
1718 b = b->level_chain)
1719 ++header_count;
1720
2c73f9f5 1721 for (; fns; fns = OVL_NEXT (fns))
386b8a85 1722 {
9dfce8fd 1723 tree fn = OVL_CURRENT (fns);
03017874
MM
1724
1725 if (TREE_CODE (fn) == TEMPLATE_DECL)
d955f6ea
KL
1726 {
1727 tree decl_arg_types;
0da99d4e 1728 tree fn_arg_types;
d955f6ea 1729
5fe7b654
GB
1730 /* In case of explicit specialization, we need to check if
1731 the number of template headers appearing in the specialization
1732 is correct. This is usually done in check_explicit_specialization,
1733 but the check done there cannot be exhaustive when specializing
1734 member functions. Consider the following code:
1735
1736 template <> void A<int>::f(int);
1737 template <> template <> void A<int>::f(int);
1738
1739 Assuming that A<int> is not itself an explicit specialization
1740 already, the first line specializes "f" which is a non-template
1741 member function, whilst the second line specializes "f" which
1742 is a template member function. So both lines are syntactically
1743 correct, and check_explicit_specialization does not reject
1744 them.
c8094d83 1745
5fe7b654
GB
1746 Here, we can do better, as we are matching the specialization
1747 against the declarations. We count the number of template
1748 headers, and we check if they match TEMPLATE_COUNT + 1
1749 (TEMPLATE_COUNT is the number of qualifying template classes,
1750 plus there must be another header for the member template
1751 itself).
c8094d83 1752
5fe7b654
GB
1753 Notice that if header_count is zero, this is not a
1754 specialization but rather a template instantiation, so there
1755 is no check we can perform here. */
1756 if (header_count && header_count != template_count + 1)
1757 continue;
1758
98f99d7f
MM
1759 /* Check that the number of template arguments at the
1760 innermost level for DECL is the same as for FN. */
1761 if (current_binding_level->kind == sk_template_parms
1762 && !current_binding_level->explicit_spec_p
1763 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
3db45ab5 1764 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
916b63c3 1765 (current_template_parms))))
98f99d7f 1766 continue;
c8094d83 1767
ba139ba8
MM
1768 /* DECL might be a specialization of FN. */
1769 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1770 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1771
1772 /* For a non-static member function, we need to make sure
1773 that the const qualification is the same. Since
1774 get_bindings does not try to merge the "this" parameter,
1775 we must do the comparison explicitly. */
1776 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
1777 && !same_type_p (TREE_VALUE (fn_arg_types),
1778 TREE_VALUE (decl_arg_types)))
1779 continue;
1780
1781 /* Skip the "this" parameter and, for constructors of
1782 classes with virtual bases, the VTT parameter. A
1783 full specialization of a constructor will have a VTT
1784 parameter, but a template never will. */
1785 decl_arg_types
1786 = skip_artificial_parms_for (decl, decl_arg_types);
1787 fn_arg_types
1788 = skip_artificial_parms_for (fn, fn_arg_types);
1789
1790 /* Check that the number of function parameters matches.
1791 For example,
1792 template <class T> void f(int i = 0);
1793 template <> void f<int>();
1794 The specialization f<int> is invalid but is not caught
1795 by get_bindings below. */
1796 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1797 continue;
1798
29a1da1c
MM
1799 /* Function templates cannot be specializations; there are
1800 no partial specializations of functions. Therefore, if
1801 the type of DECL does not match FN, there is no
1802 match. */
54062fc0
PC
1803 if (tsk == tsk_template)
1804 {
1805 if (compparms (fn_arg_types, decl_arg_types))
1806 candidates = tree_cons (NULL_TREE, fn, candidates);
1807 continue;
1808 }
29a1da1c 1809
d955f6ea
KL
1810 /* See whether this function might be a specialization of this
1811 template. */
a34d3336 1812 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
d955f6ea
KL
1813
1814 if (!targs)
1815 /* We cannot deduce template arguments that when used to
1816 specialize TMPL will produce DECL. */
1817 continue;
1818
1819 /* Save this template, and the arguments deduced. */
1820 templates = tree_cons (targs, fn, templates);
1821 }
03017874
MM
1822 else if (need_member_template)
1823 /* FN is an ordinary member function, and we need a
1824 specialization of a member template. */
d955f6ea 1825 ;
03017874
MM
1826 else if (TREE_CODE (fn) != FUNCTION_DECL)
1827 /* We can get IDENTIFIER_NODEs here in certain erroneous
1828 cases. */
d955f6ea 1829 ;
03017874
MM
1830 else if (!DECL_FUNCTION_MEMBER_P (fn))
1831 /* This is just an ordinary non-member function. Nothing can
1832 be a specialization of that. */
d955f6ea 1833 ;
3b82c249
KL
1834 else if (DECL_ARTIFICIAL (fn))
1835 /* Cannot specialize functions that are created implicitly. */
d955f6ea 1836 ;
75650646 1837 else
03017874
MM
1838 {
1839 tree decl_arg_types;
386b8a85 1840
03017874
MM
1841 /* This is an ordinary member function. However, since
1842 we're here, we can assume it's enclosing class is a
1843 template class. For example,
c8094d83 1844
03017874
MM
1845 template <typename T> struct S { void f(); };
1846 template <> void S<int>::f() {}
1847
1848 Here, S<int>::f is a non-template, but S<int> is a
1849 template class. If FN has the same type as DECL, we
1850 might be in business. */
f5d47abd
KL
1851
1852 if (!DECL_TEMPLATE_INFO (fn))
1853 /* Its enclosing class is an explicit specialization
1854 of a template class. This is not a candidate. */
1855 continue;
1856
03017874
MM
1857 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1858 TREE_TYPE (TREE_TYPE (fn))))
1859 /* The return types differ. */
1860 continue;
1861
1862 /* Adjust the type of DECL in case FN is a static member. */
1863 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
c8094d83 1864 if (DECL_STATIC_FUNCTION_P (fn)
03017874
MM
1865 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1866 decl_arg_types = TREE_CHAIN (decl_arg_types);
1867
c8094d83 1868 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
03017874
MM
1869 decl_arg_types))
1870 /* They match! */
1871 candidates = tree_cons (NULL_TREE, fn, candidates);
03017874 1872 }
386b8a85 1873 }
03017874 1874
bf8f3f93 1875 if (templates && TREE_CHAIN (templates))
386b8a85 1876 {
03017874 1877 /* We have:
c8094d83 1878
03017874
MM
1879 [temp.expl.spec]
1880
1881 It is possible for a specialization with a given function
1882 signature to be instantiated from more than one function
1883 template. In such cases, explicit specification of the
1884 template arguments must be used to uniquely identify the
1885 function template specialization being specialized.
1886
1887 Note that here, there's no suggestion that we're supposed to
1888 determine which of the candidate templates is most
1889 specialized. However, we, also have:
1890
1891 [temp.func.order]
1892
1893 Partial ordering of overloaded function template
1894 declarations is used in the following contexts to select
1895 the function template to which a function template
c8094d83 1896 specialization refers:
03017874 1897
0cbd7506 1898 -- when an explicit specialization refers to a function
c8094d83 1899 template.
03017874
MM
1900
1901 So, we do use the partial ordering rules, at least for now.
0e339752 1902 This extension can only serve to make invalid programs valid,
03017874
MM
1903 so it's safe. And, there is strong anecdotal evidence that
1904 the committee intended the partial ordering rules to apply;
3b426391 1905 the EDG front end has that behavior, and John Spicer claims
03017874
MM
1906 that the committee simply forgot to delete the wording in
1907 [temp.expl.spec]. */
7ca383e6
MM
1908 tree tmpl = most_specialized_instantiation (templates);
1909 if (tmpl != error_mark_node)
1910 {
1911 templates = tmpl;
1912 TREE_CHAIN (templates) = NULL_TREE;
1913 }
e1467ff2
MM
1914 }
1915
03017874 1916 if (templates == NULL_TREE && candidates == NULL_TREE)
e1467ff2 1917 {
dee15844
JM
1918 error ("template-id %qD for %q+D does not match any template "
1919 "declaration", template_id, decl);
03017874 1920 return error_mark_node;
386b8a85 1921 }
03017874 1922 else if ((templates && TREE_CHAIN (templates))
bf8f3f93
MM
1923 || (candidates && TREE_CHAIN (candidates))
1924 || (templates && candidates))
386b8a85 1925 {
dee15844
JM
1926 error ("ambiguous template specialization %qD for %q+D",
1927 template_id, decl);
bf8f3f93
MM
1928 chainon (candidates, templates);
1929 print_candidates (candidates);
03017874 1930 return error_mark_node;
386b8a85
JM
1931 }
1932
c6002625 1933 /* We have one, and exactly one, match. */
03017874
MM
1934 if (candidates)
1935 {
29a1da1c 1936 tree fn = TREE_VALUE (candidates);
62e3e13a
JM
1937 *targs_out = copy_node (DECL_TI_ARGS (fn));
1938 /* DECL is a re-declaration or partial instantiation of a template
1939 function. */
29a1da1c
MM
1940 if (TREE_CODE (fn) == TEMPLATE_DECL)
1941 return fn;
03017874
MM
1942 /* It was a specialization of an ordinary member function in a
1943 template class. */
29a1da1c 1944 return DECL_TI_TEMPLATE (fn);
03017874
MM
1945 }
1946
1947 /* It was a specialization of a template. */
17aec3eb 1948 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
03017874
MM
1949 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1950 {
1951 *targs_out = copy_node (targs);
c8094d83 1952 SET_TMPL_ARGS_LEVEL (*targs_out,
03017874
MM
1953 TMPL_ARGS_DEPTH (*targs_out),
1954 TREE_PURPOSE (templates));
1955 }
1956 else
1957 *targs_out = TREE_PURPOSE (templates);
e1467ff2 1958 return TREE_VALUE (templates);
8d08fdba 1959}
8afa707f
MM
1960
1961/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1962 but with the default argument values filled in from those in the
1963 TMPL_TYPES. */
c8094d83 1964
8afa707f 1965static tree
3a978d72
NN
1966copy_default_args_to_explicit_spec_1 (tree spec_types,
1967 tree tmpl_types)
8afa707f
MM
1968{
1969 tree new_spec_types;
1970
1971 if (!spec_types)
1972 return NULL_TREE;
1973
1974 if (spec_types == void_list_node)
1975 return void_list_node;
1976
1977 /* Substitute into the rest of the list. */
1978 new_spec_types =
1979 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1980 TREE_CHAIN (tmpl_types));
c8094d83 1981
8afa707f
MM
1982 /* Add the default argument for this parameter. */
1983 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1984 TREE_VALUE (spec_types),
1985 new_spec_types);
1986}
1987
1988/* DECL is an explicit specialization. Replicate default arguments
1989 from the template it specializes. (That way, code like:
1990
1991 template <class T> void f(T = 3);
1992 template <> void f(double);
c8094d83 1993 void g () { f (); }
8afa707f
MM
1994
1995 works, as required.) An alternative approach would be to look up
1996 the correct default arguments at the call-site, but this approach
1997 is consistent with how implicit instantiations are handled. */
1998
1999static void
3a978d72 2000copy_default_args_to_explicit_spec (tree decl)
8afa707f
MM
2001{
2002 tree tmpl;
2003 tree spec_types;
2004 tree tmpl_types;
2005 tree new_spec_types;
2006 tree old_type;
2007 tree new_type;
2008 tree t;
08c2df0f
NS
2009 tree object_type = NULL_TREE;
2010 tree in_charge = NULL_TREE;
e0fff4b3 2011 tree vtt = NULL_TREE;
8afa707f
MM
2012
2013 /* See if there's anything we need to do. */
2014 tmpl = DECL_TI_TEMPLATE (decl);
2015 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2016 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2017 if (TREE_PURPOSE (t))
2018 break;
2019 if (!t)
2020 return;
2021
2022 old_type = TREE_TYPE (decl);
2023 spec_types = TYPE_ARG_TYPES (old_type);
c8094d83 2024
8afa707f
MM
2025 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2026 {
08c2df0f 2027 /* Remove the this pointer, but remember the object's type for
0cbd7506 2028 CV quals. */
08c2df0f 2029 object_type = TREE_TYPE (TREE_VALUE (spec_types));
8afa707f
MM
2030 spec_types = TREE_CHAIN (spec_types);
2031 tmpl_types = TREE_CHAIN (tmpl_types);
c8094d83 2032
8afa707f 2033 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
0cbd7506
MS
2034 {
2035 /* DECL may contain more parameters than TMPL due to the extra
2036 in-charge parameter in constructors and destructors. */
2037 in_charge = spec_types;
08c2df0f
NS
2038 spec_types = TREE_CHAIN (spec_types);
2039 }
e0fff4b3
JM
2040 if (DECL_HAS_VTT_PARM_P (decl))
2041 {
2042 vtt = spec_types;
2043 spec_types = TREE_CHAIN (spec_types);
2044 }
8afa707f
MM
2045 }
2046
2047 /* Compute the merged default arguments. */
c8094d83 2048 new_spec_types =
8afa707f
MM
2049 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2050
08c2df0f
NS
2051 /* Compute the new FUNCTION_TYPE. */
2052 if (object_type)
8afa707f 2053 {
e0fff4b3 2054 if (vtt)
0cbd7506
MS
2055 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2056 TREE_VALUE (vtt),
2057 new_spec_types);
e0fff4b3 2058
08c2df0f 2059 if (in_charge)
0cbd7506
MS
2060 /* Put the in-charge parameter back. */
2061 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2062 TREE_VALUE (in_charge),
2063 new_spec_types);
08c2df0f 2064
43dc123f
MM
2065 new_type = build_method_type_directly (object_type,
2066 TREE_TYPE (old_type),
2067 new_spec_types);
8afa707f 2068 }
8afa707f
MM
2069 else
2070 new_type = build_function_type (TREE_TYPE (old_type),
08c2df0f 2071 new_spec_types);
e9525111
MM
2072 new_type = cp_build_type_attribute_variant (new_type,
2073 TYPE_ATTRIBUTES (old_type));
8afa707f
MM
2074 new_type = build_exception_variant (new_type,
2075 TYPE_RAISES_EXCEPTIONS (old_type));
2076 TREE_TYPE (decl) = new_type;
2077}
2078
386b8a85 2079/* Check to see if the function just declared, as indicated in
75650646
MM
2080 DECLARATOR, and in DECL, is a specialization of a function
2081 template. We may also discover that the declaration is an explicit
2082 instantiation at this point.
2083
e1467ff2 2084 Returns DECL, or an equivalent declaration that should be used
03017874
MM
2085 instead if all goes well. Issues an error message if something is
2086 amiss. Returns error_mark_node if the error is not easily
2087 recoverable.
c8094d83
MS
2088
2089 FLAGS is a bitmask consisting of the following flags:
75650646 2090
75650646
MM
2091 2: The function has a definition.
2092 4: The function is a friend.
75650646
MM
2093
2094 The TEMPLATE_COUNT is the number of references to qualifying
2095 template classes that appeared in the name of the function. For
2096 example, in
2097
2098 template <class T> struct S { void f(); };
2099 void S<int>::f();
c8094d83 2100
75650646
MM
2101 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2102 classes are not counted in the TEMPLATE_COUNT, so that in
2103
2104 template <class T> struct S {};
2105 template <> struct S<int> { void f(); }
36a117a5 2106 template <> void S<int>::f();
75650646
MM
2107
2108 the TEMPLATE_COUNT would be 0. (Note that this declaration is
0e339752 2109 invalid; there should be no template <>.)
75650646
MM
2110
2111 If the function is a specialization, it is marked as such via
2112 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
c8094d83 2113 is set up correctly, and it is added to the list of specializations
75650646 2114 for that template. */
386b8a85 2115
e1467ff2 2116tree
c8094d83 2117check_explicit_specialization (tree declarator,
0cbd7506
MS
2118 tree decl,
2119 int template_count,
2120 int flags)
386b8a85 2121{
75650646
MM
2122 int have_def = flags & 2;
2123 int is_friend = flags & 4;
2124 int specialization = 0;
e1467ff2 2125 int explicit_instantiation = 0;
fd4de5ff 2126 int member_specialization = 0;
75650646
MM
2127 tree ctype = DECL_CLASS_CONTEXT (decl);
2128 tree dname = DECL_NAME (decl);
74b846e0 2129 tmpl_spec_kind tsk;
386b8a85 2130
f65b7de3
GB
2131 if (is_friend)
2132 {
2133 if (!processing_specialization)
2134 tsk = tsk_none;
2135 else
2136 tsk = tsk_excessive_parms;
2137 }
2138 else
2139 tsk = current_tmpl_spec_kind (template_count);
75650646 2140
74b846e0
MM
2141 switch (tsk)
2142 {
2143 case tsk_none:
c8094d83 2144 if (processing_specialization)
75650646 2145 {
fd4de5ff
MM
2146 specialization = 1;
2147 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
75650646 2148 }
74b846e0 2149 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
386b8a85 2150 {
74b846e0
MM
2151 if (is_friend)
2152 /* This could be something like:
75650646 2153
74b846e0
MM
2154 template <class T> void f(T);
2155 class S { friend void f<>(int); } */
2156 specialization = 1;
2157 else
2158 {
2159 /* This case handles bogus declarations like template <>
2160 template <class T> void f<int>(); */
2161
0f51ccfc 2162 error ("template-id %qD in declaration of primary template",
0cbd7506 2163 declarator);
74b846e0
MM
2164 return decl;
2165 }
2166 }
2167 break;
2168
2169 case tsk_invalid_member_spec:
2170 /* The error has already been reported in
2171 check_specialization_scope. */
2172 return error_mark_node;
2173
2174 case tsk_invalid_expl_inst:
33bd39a2 2175 error ("template parameter list used in explicit instantiation");
74b846e0
MM
2176
2177 /* Fall through. */
2178
2179 case tsk_expl_inst:
fd4de5ff 2180 if (have_def)
33bd39a2 2181 error ("definition provided for explicit instantiation");
c8094d83 2182
fd4de5ff 2183 explicit_instantiation = 1;
74b846e0 2184 break;
fd4de5ff 2185
74b846e0 2186 case tsk_excessive_parms:
f03adc6b
GB
2187 case tsk_insufficient_parms:
2188 if (tsk == tsk_excessive_parms)
0cbd7506 2189 error ("too many template parameter lists in declaration of %qD",
f03adc6b
GB
2190 decl);
2191 else if (template_header_count)
0f51ccfc 2192 error("too few template parameter lists in declaration of %qD", decl);
f03adc6b 2193 else
0f51ccfc 2194 error("explicit specialization of %qD must be introduced by "
9e637a26 2195 "%<template <>%>", decl);
75650646 2196
74b846e0
MM
2197 /* Fall through. */
2198 case tsk_expl_spec:
2199 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2200 if (ctype)
2201 member_specialization = 1;
2202 else
2203 specialization = 1;
2204 break;
74b846e0
MM
2205
2206 case tsk_template:
2207 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
c6f2ed0d 2208 {
fd4de5ff
MM
2209 /* This case handles bogus declarations like template <>
2210 template <class T> void f<int>(); */
75650646 2211
655dc6ee 2212 if (uses_template_parms (declarator))
0f51ccfc 2213 error ("function template partial specialization %qD "
e1e93ad8 2214 "is not allowed", declarator);
655dc6ee 2215 else
0f51ccfc 2216 error ("template-id %qD in declaration of primary template",
0cbd7506 2217 declarator);
fd4de5ff 2218 return decl;
386b8a85 2219 }
74b846e0
MM
2220
2221 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2222 /* This is a specialization of a member template, without
2223 specialization the containing class. Something like:
2224
2225 template <class T> struct S {
c8094d83 2226 template <class U> void f (U);
0cbd7506 2227 };
74b846e0 2228 template <> template <class U> void S<int>::f(U) {}
c8094d83 2229
74b846e0
MM
2230 That's a specialization -- but of the entire template. */
2231 specialization = 1;
2232 break;
2233
2234 default:
315fb5db 2235 gcc_unreachable ();
75650646 2236 }
386b8a85 2237
670960ac
JM
2238 if (specialization || member_specialization)
2239 {
2240 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2241 for (; t; t = TREE_CHAIN (t))
2242 if (TREE_PURPOSE (t))
2243 {
cbe5f3b3
MLI
2244 permerror (input_location,
2245 "default argument specified in explicit specialization");
670960ac
JM
2246 break;
2247 }
2248 }
2249
e1467ff2 2250 if (specialization || member_specialization || explicit_instantiation)
75650646
MM
2251 {
2252 tree tmpl = NULL_TREE;
2253 tree targs = NULL_TREE;
75650646
MM
2254
2255 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
386b8a85
JM
2256 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2257 {
2258 tree fns;
2259
50bc768d 2260 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2f54a1db 2261 if (ctype)
386b8a85 2262 fns = dname;
2f54a1db
GB
2263 else
2264 {
2265 /* If there is no class context, the explicit instantiation
0cbd7506 2266 must be at namespace scope. */
50bc768d 2267 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2f54a1db
GB
2268
2269 /* Find the namespace binding, using the declaration
0cbd7506 2270 context. */
ddf74938 2271 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
664a90c0 2272 false, true);
97dc8e5b 2273 if (fns == error_mark_node || !is_overloaded_fn (fns))
4230cec2
NS
2274 {
2275 error ("%qD is not a template function", dname);
2276 fns = error_mark_node;
2277 }
664a90c0
JM
2278 else
2279 {
2280 tree fn = OVL_CURRENT (fns);
ddf74938 2281 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
08d295c5
JM
2282 CP_DECL_CONTEXT (fn)))
2283 error ("%qD is not declared in %qD",
2284 decl, current_namespace);
664a90c0 2285 }
2f54a1db 2286 }
386b8a85 2287
2f54a1db 2288 declarator = lookup_template_function (fns, NULL_TREE);
386b8a85
JM
2289 }
2290
f2e48b67
BK
2291 if (declarator == error_mark_node)
2292 return error_mark_node;
2293
75650646
MM
2294 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2295 {
8ec2ac34 2296 if (!explicit_instantiation)
0e339752 2297 /* A specialization in class scope. This is invalid,
6c30752f
MM
2298 but the error will already have been flagged by
2299 check_specialization_scope. */
2300 return error_mark_node;
8ec2ac34 2301 else
b370501f 2302 {
0e339752 2303 /* It's not valid to write an explicit instantiation in
b370501f 2304 class scope, e.g.:
8ec2ac34 2305
0cbd7506 2306 class C { template void f(); }
8ec2ac34 2307
b370501f
KG
2308 This case is caught by the parser. However, on
2309 something like:
c8094d83 2310
b370501f 2311 template class C { void f(); };
8ec2ac34 2312
0e339752 2313 (which is invalid) we can get here. The error will be
b370501f
KG
2314 issued later. */
2315 ;
2316 }
8ec2ac34 2317
e1467ff2 2318 return decl;
75650646 2319 }
c8094d83 2320 else if (ctype != NULL_TREE
75650646
MM
2321 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2322 IDENTIFIER_NODE))
386b8a85 2323 {
75650646
MM
2324 /* Find the list of functions in ctype that have the same
2325 name as the declared function. */
2326 tree name = TREE_OPERAND (declarator, 0);
03017874
MM
2327 tree fns = NULL_TREE;
2328 int idx;
2329
8ba658ee 2330 if (constructor_name_p (name, ctype))
386b8a85 2331 {
75650646 2332 int is_constructor = DECL_CONSTRUCTOR_P (decl);
c8094d83 2333
0fcedd9c 2334 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
9f4faeae 2335 : !CLASSTYPE_DESTRUCTORS (ctype))
75650646
MM
2336 {
2337 /* From [temp.expl.spec]:
c8094d83 2338
75650646
MM
2339 If such an explicit specialization for the member
2340 of a class template names an implicitly-declared
2341 special member function (clause _special_), the
c8094d83 2342 program is ill-formed.
e1467ff2
MM
2343
2344 Similar language is found in [temp.explicit]. */
33bd39a2 2345 error ("specialization of implicitly-declared special member function");
03017874 2346 return error_mark_node;
75650646 2347 }
386b8a85 2348
42da2fd8 2349 name = is_constructor ? ctor_identifier : dtor_identifier;
75650646 2350 }
42da2fd8 2351
421844e7 2352 if (!DECL_CONV_FN_P (decl))
03017874
MM
2353 {
2354 idx = lookup_fnfields_1 (ctype, name);
2355 if (idx >= 0)
aaaa46d2 2356 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
03017874
MM
2357 }
2358 else
2359 {
d4e6fecb 2360 VEC(tree,gc) *methods;
aaaa46d2 2361 tree ovl;
03017874
MM
2362
2363 /* For a type-conversion operator, we cannot do a
2364 name-based lookup. We might be looking for `operator
2365 int' which will be a specialization of `operator T'.
2366 So, we find *all* the conversion operators, and then
2367 select from them. */
2368 fns = NULL_TREE;
2369
2370 methods = CLASSTYPE_METHOD_VEC (ctype);
2371 if (methods)
5dd236e2 2372 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
9ba5ff0f 2373 VEC_iterate (tree, methods, idx, ovl);
aaaa46d2 2374 ++idx)
03017874 2375 {
aaaa46d2 2376 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
03017874
MM
2377 /* There are no more conversion functions. */
2378 break;
2379
2380 /* Glue all these conversion functions together
2381 with those we already have. */
2382 for (; ovl; ovl = OVL_NEXT (ovl))
2383 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2384 }
2385 }
c8094d83
MS
2386
2387 if (fns == NULL_TREE)
386b8a85 2388 {
0f51ccfc 2389 error ("no member function %qD declared in %qT", name, ctype);
03017874 2390 return error_mark_node;
386b8a85
JM
2391 }
2392 else
2393 TREE_OPERAND (declarator, 0) = fns;
2394 }
c8094d83 2395
e1467ff2
MM
2396 /* Figure out what exactly is being specialized at this point.
2397 Note that for an explicit instantiation, even one for a
38e01259 2398 member function, we cannot tell apriori whether the
e1467ff2 2399 instantiation is for a member template, or just a member
36a117a5
MM
2400 function of a template class. Even if a member template is
2401 being instantiated, the member template arguments may be
2402 elided if they can be deduced from the rest of the
2403 declaration. */
e1467ff2 2404 tmpl = determine_specialization (declarator, decl,
c8094d83 2405 &targs,
5fe7b654 2406 member_specialization,
29a1da1c
MM
2407 template_count,
2408 tsk);
c8094d83 2409
03017874
MM
2410 if (!tmpl || tmpl == error_mark_node)
2411 /* We couldn't figure out what this declaration was
2412 specializing. */
2413 return error_mark_node;
2414 else
386b8a85 2415 {
25aab5d0 2416 tree gen_tmpl = most_general_template (tmpl);
36a117a5 2417
e1467ff2
MM
2418 if (explicit_instantiation)
2419 {
03d0f4af 2420 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
c8094d83 2421 is done by do_decl_instantiation later. */
25aab5d0
MM
2422
2423 int arg_depth = TMPL_ARGS_DEPTH (targs);
2424 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2425
2426 if (arg_depth > parm_depth)
2427 {
2428 /* If TMPL is not the most general template (for
2429 example, if TMPL is a friend template that is
2430 injected into namespace scope), then there will
dc957d14 2431 be too many levels of TARGS. Remove some of them
25aab5d0
MM
2432 here. */
2433 int i;
2434 tree new_targs;
2435
f31c0a32 2436 new_targs = make_tree_vec (parm_depth);
25aab5d0
MM
2437 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2438 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2439 = TREE_VEC_ELT (targs, i);
2440 targs = new_targs;
2441 }
c8094d83 2442
3e4a3562 2443 return instantiate_template (tmpl, targs, tf_error);
e1467ff2 2444 }
74b846e0 2445
6c07f448
KL
2446 /* If we thought that the DECL was a member function, but it
2447 turns out to be specializing a static member function,
4546865e 2448 make DECL a static member function as well. */
6c07f448
KL
2449 if (DECL_STATIC_FUNCTION_P (tmpl)
2450 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
4546865e 2451 revert_static_member_fn (decl);
6c07f448 2452
f9a7ae04 2453 /* If this is a specialization of a member template of a
29a1da1c
MM
2454 template class, we want to return the TEMPLATE_DECL, not
2455 the specialization of it. */
74b846e0
MM
2456 if (tsk == tsk_template)
2457 {
448545cb 2458 tree result = DECL_TEMPLATE_RESULT (tmpl);
74b846e0 2459 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
448545cb 2460 DECL_INITIAL (result) = NULL_TREE;
b190f239
NS
2461 if (have_def)
2462 {
448545cb 2463 tree parm;
f31686a3 2464 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
448545cb 2465 DECL_SOURCE_LOCATION (result)
f31686a3 2466 = DECL_SOURCE_LOCATION (decl);
08167d1c
AO
2467 /* We want to use the argument list specified in the
2468 definition, not in the original declaration. */
448545cb
JM
2469 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
2470 for (parm = DECL_ARGUMENTS (result); parm;
2471 parm = TREE_CHAIN (parm))
2472 DECL_CONTEXT (parm) = result;
b190f239 2473 }
62e3e13a 2474 return register_specialization (tmpl, gen_tmpl, targs,
2b110bfc 2475 is_friend, 0);
74b846e0
MM
2476 }
2477
36a117a5 2478 /* Set up the DECL_TEMPLATE_INFO for DECL. */
e1b3e07d 2479 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
36a117a5 2480
8afa707f
MM
2481 /* Inherit default function arguments from the template
2482 DECL is specializing. */
2483 copy_default_args_to_explicit_spec (decl);
2484
c750255c
MM
2485 /* This specialization has the same protection as the
2486 template it specializes. */
2487 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2488 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
b9e75696 2489
736b8100
SB
2490 /* 7.1.1-1 [dcl.stc]
2491
2492 A storage-class-specifier shall not be specified in an
2493 explicit specialization...
2494
2495 The parser rejects these, so unless action is taken here,
2496 explicit function specializations will always appear with
2497 global linkage.
2498
2499 The action recommended by the C++ CWG in response to C++
2500 defect report 605 is to make the storage class and linkage
2501 of the explicit specialization match the templated function:
2502
2503 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
2504 */
2505 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
2506 {
2507 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
2508 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
2509
d732e98f 2510 /* This specialization has the same linkage and visibility as
736b8100
SB
2511 the function template it specializes. */
2512 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
92d0af97
JJ
2513 if (! TREE_PUBLIC (decl))
2514 {
2515 DECL_INTERFACE_KNOWN (decl) = 1;
2516 DECL_NOT_REALLY_EXTERN (decl) = 1;
2517 }
736b8100
SB
2518 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
2519 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
2520 {
2521 DECL_VISIBILITY_SPECIFIED (decl) = 1;
2522 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
2523 }
2524 }
2525
9c12301f
MM
2526 /* If DECL is a friend declaration, declared using an
2527 unqualified name, the namespace associated with DECL may
2528 have been set incorrectly. For example, in:
3db45ab5
MS
2529
2530 template <typename T> void f(T);
2531 namespace N {
2532 struct S { friend void f<int>(int); }
2533 }
2534
2535 we will have set the DECL_CONTEXT for the friend
2536 declaration to N, rather than to the global namespace. */
9c12301f
MM
2537 if (DECL_NAMESPACE_SCOPE_P (decl))
2538 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
8d039470 2539
386b8a85 2540 if (is_friend && !have_def)
36a117a5
MM
2541 /* This is not really a declaration of a specialization.
2542 It's just the name of an instantiation. But, it's not
2543 a request for an instantiation, either. */
fbf1c34b 2544 SET_DECL_IMPLICIT_INSTANTIATION (decl);
08511114
KL
2545 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2546 /* This is indeed a specialization. In case of constructors
2547 and destructors, we need in-charge and not-in-charge
2548 versions in V3 ABI. */
2549 clone_function_decl (decl, /*update_method_vec_p=*/0);
75650646 2550
36a117a5
MM
2551 /* Register this specialization so that we can find it
2552 again. */
2b110bfc 2553 decl = register_specialization (decl, gen_tmpl, targs, is_friend, 0);
386b8a85
JM
2554 }
2555 }
c8094d83 2556
e1467ff2 2557 return decl;
386b8a85 2558}
75650646 2559
75650646
MM
2560/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2561 parameters. These are represented in the same format used for
2562 DECL_TEMPLATE_PARMS. */
2563
50bc768d 2564int
58f9752a 2565comp_template_parms (const_tree parms1, const_tree parms2)
75650646 2566{
58f9752a
KG
2567 const_tree p1;
2568 const_tree p2;
75650646
MM
2569
2570 if (parms1 == parms2)
2571 return 1;
2572
c8094d83 2573 for (p1 = parms1, p2 = parms2;
75650646
MM
2574 p1 != NULL_TREE && p2 != NULL_TREE;
2575 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2576 {
2577 tree t1 = TREE_VALUE (p1);
2578 tree t2 = TREE_VALUE (p2);
2579 int i;
2580
50bc768d
NS
2581 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2582 gcc_assert (TREE_CODE (t2) == TREE_VEC);
75650646
MM
2583
2584 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2585 return 0;
2586
c8094d83 2587 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
75650646 2588 {
0f67a82f
LM
2589 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2590 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
42b304f1 2591
0f67a82f
LM
2592 /* If either of the template parameters are invalid, assume
2593 they match for the sake of error recovery. */
2594 if (parm1 == error_mark_node || parm2 == error_mark_node)
2595 return 1;
75650646
MM
2596
2597 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2598 return 0;
2599
5d80a306
DG
2600 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
2601 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
2602 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
75650646 2603 continue;
3bfdc719 2604 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
75650646
MM
2605 return 0;
2606 }
2607 }
2608
2609 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2610 /* One set of parameters has more parameters lists than the
2611 other. */
2612 return 0;
2613
2614 return 1;
2615}
2616
5d80a306
DG
2617/* Determine whether PARM is a parameter pack. */
2618bool
58f9752a 2619template_parameter_pack_p (const_tree parm)
5d80a306
DG
2620{
2621 /* Determine if we have a non-type template parameter pack. */
2622 if (TREE_CODE (parm) == PARM_DECL)
2623 return (DECL_TEMPLATE_PARM_P (parm)
2624 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
2625
2626 /* If this is a list of template parameters, we could get a
2627 TYPE_DECL or a TEMPLATE_DECL. */
2628 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
2629 parm = TREE_TYPE (parm);
2630
2631 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
2632 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
2633 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
2634}
2635
5d80a306
DG
2636/* Determine whether ARGS describes a variadic template args list,
2637 i.e., one that is terminated by a template argument pack. */
2638static bool
2639template_args_variadic_p (tree args)
2640{
2641 int nargs;
2642 tree last_parm;
2643
2644 if (args == NULL_TREE)
2645 return false;
2646
2647 args = INNERMOST_TEMPLATE_ARGS (args);
2648 nargs = TREE_VEC_LENGTH (args);
2649
2650 if (nargs == 0)
2651 return false;
2652
2653 last_parm = TREE_VEC_ELT (args, nargs - 1);
2654
2655 return ARGUMENT_PACK_P (last_parm);
2656}
2657
2658/* Generate a new name for the parameter pack name NAME (an
2659 IDENTIFIER_NODE) that incorporates its */
2660static tree
2661make_ith_pack_parameter_name (tree name, int i)
2662{
2663 /* Munge the name to include the parameter index. */
f9329c35
DS
2664#define NUMBUF_LEN 128
2665 char numbuf[NUMBUF_LEN];
5d80a306 2666 char* newname;
f9329c35
DS
2667 int newname_len;
2668
2669 snprintf (numbuf, NUMBUF_LEN, "%i", i);
2670 newname_len = IDENTIFIER_LENGTH (name)
d3bc57f2 2671 + strlen (numbuf) + 2;
f9329c35
DS
2672 newname = (char*)alloca (newname_len);
2673 snprintf (newname, newname_len,
2674 "%s#%i", IDENTIFIER_POINTER (name), i);
5d80a306
DG
2675 return get_identifier (newname);
2676}
2677
f9329c35
DS
2678/* Return true if T is a primary function
2679 or class template instantiation. */
2680
2681static bool
2682primary_template_instantiation_p (const_tree t)
2683{
2684 if (!t)
2685 return false;
2686
2687 if (TREE_CODE (t) == FUNCTION_DECL)
2688 return DECL_LANG_SPECIFIC (t)
2689 && DECL_TEMPLATE_INSTANTIATION (t)
2690 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
2691 else if (CLASS_TYPE_P (t))
2692 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
2693 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
2694 return false;
2695}
2696
2697/* Return true if PARM is a template template parameter. */
2698
2699bool
2700template_template_parameter_p (const_tree parm)
2701{
2702 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
2703}
2704
2705/* Return the template parameters of T if T is a
2706 primary template instantiation, NULL otherwise. */
2707
2708tree
2709get_primary_template_innermost_parameters (const_tree t)
2710{
2711 tree parms = NULL, template_info = NULL;
2712
2713 if ((template_info = get_template_info (t))
2714 && primary_template_instantiation_p (t))
2715 parms = INNERMOST_TEMPLATE_PARMS
2716 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
2717
2718 return parms;
2719}
2720
2721/* Returns the template arguments of T if T is a template instantiation,
2722 NULL otherwise. */
2723
2724tree
2725get_template_innermost_arguments (const_tree t)
2726{
2727 tree args = NULL, template_info = NULL;
2728
2729 if ((template_info = get_template_info (t))
2730 && TI_ARGS (template_info))
2731 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
2732
2733 return args;
2734}
2735
2736/* Return the arguments pack of T if T is a template, NULL otherwise. */
2737
2738tree
2739get_template_argument_pack_elems (const_tree t)
2740{
2741 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
2742 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
2743 return NULL;
2744
2745 return ARGUMENT_PACK_ARGS (t);
2746}
2747
1ad8aeeb 2748/* Structure used to track the progress of find_parameter_packs_r. */
5d80a306
DG
2749struct find_parameter_pack_data
2750{
1ad8aeeb
DG
2751 /* TREE_LIST that will contain all of the parameter packs found by
2752 the traversal. */
5d80a306 2753 tree* parameter_packs;
1ad8aeeb
DG
2754
2755 /* Set of AST nodes that have been visited by the traversal. */
5d80a306
DG
2756 struct pointer_set_t *visited;
2757};
2758
88b82314 2759/* Identifies all of the argument packs that occur in a template
5d80a306 2760 argument and appends them to the TREE_LIST inside DATA, which is a
88b82314 2761 find_parameter_pack_data structure. This is a subroutine of
5d80a306
DG
2762 make_pack_expansion and uses_parameter_packs. */
2763static tree
2764find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
2765{
2766 tree t = *tp;
2767 struct find_parameter_pack_data* ppd =
2768 (struct find_parameter_pack_data*)data;
1ad8aeeb 2769 bool parameter_pack_p = false;
5d80a306 2770
1ad8aeeb
DG
2771 /* Identify whether this is a parameter pack or not. */
2772 switch (TREE_CODE (t))
2773 {
2774 case TEMPLATE_PARM_INDEX:
2775 if (TEMPLATE_PARM_PARAMETER_PACK (t))
2776 parameter_pack_p = true;
2777 break;
2778
2779 case TEMPLATE_TYPE_PARM:
2780 case TEMPLATE_TEMPLATE_PARM:
2781 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
2782 parameter_pack_p = true;
2783 break;
2784
2785 case PARM_DECL:
2786 if (FUNCTION_PARAMETER_PACK_P (t))
2787 {
2788 /* We don't want to walk into the type of a PARM_DECL,
2789 because we don't want to see the type parameter pack. */
2790 *walk_subtrees = 0;
2791 parameter_pack_p = true;
2792 }
2793 break;
2794
2795 default:
2796 /* Not a parameter pack. */
2797 break;
2798 }
2799
2800 if (parameter_pack_p)
2801 {
2802 /* Add this parameter pack to the list. */
2803 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
1ad8aeeb 2804 }
7b3e2d46 2805
1ad8aeeb
DG
2806 if (TYPE_P (t))
2807 cp_walk_tree (&TYPE_CONTEXT (t),
92fab505 2808 &find_parameter_packs_r, ppd, ppd->visited);
1ad8aeeb 2809
5d80a306
DG
2810 /* This switch statement will return immediately if we don't find a
2811 parameter pack. */
2812 switch (TREE_CODE (t))
2813 {
2814 case TEMPLATE_PARM_INDEX:
5d80a306
DG
2815 return NULL_TREE;
2816
2817 case BOUND_TEMPLATE_TEMPLATE_PARM:
1ad8aeeb
DG
2818 /* Check the template itself. */
2819 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
92fab505 2820 &find_parameter_packs_r, ppd, ppd->visited);
5d80a306 2821 /* Check the template arguments. */
14588106 2822 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
92fab505 2823 ppd->visited);
5d80a306 2824 *walk_subtrees = 0;
1ad8aeeb 2825 return NULL_TREE;
5d80a306
DG
2826
2827 case TEMPLATE_TYPE_PARM:
2828 case TEMPLATE_TEMPLATE_PARM:
5d80a306
DG
2829 return NULL_TREE;
2830
2831 case PARM_DECL:
5d80a306
DG
2832 return NULL_TREE;
2833
2834 case RECORD_TYPE:
2835 if (TYPE_PTRMEMFUNC_P (t))
2836 return NULL_TREE;
2837 /* Fall through. */
2838
2839 case UNION_TYPE:
2840 case ENUMERAL_TYPE:
2841 if (TYPE_TEMPLATE_INFO (t))
1ad8aeeb 2842 cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
92fab505 2843 &find_parameter_packs_r, ppd, ppd->visited);
5d80a306
DG
2844
2845 *walk_subtrees = 0;
2846 return NULL_TREE;
2847
2848 case TEMPLATE_DECL:
1ad8aeeb 2849 cp_walk_tree (&TREE_TYPE (t),
92fab505 2850 &find_parameter_packs_r, ppd, ppd->visited);
5d80a306 2851 return NULL_TREE;
e1a18c68
DG
2852
2853 case TYPENAME_TYPE:
2854 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
92fab505 2855 ppd, ppd->visited);
e1a18c68
DG
2856 *walk_subtrees = 0;
2857 return NULL_TREE;
e1a18c68 2858
5d80a306
DG
2859 case TYPE_PACK_EXPANSION:
2860 case EXPR_PACK_EXPANSION:
2861 *walk_subtrees = 0;
2862 return NULL_TREE;
2863
d393153e 2864 case INTEGER_TYPE:
14588106 2865 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
92fab505 2866 ppd, ppd->visited);
d393153e
DG
2867 *walk_subtrees = 0;
2868 return NULL_TREE;
2869
4439d02f 2870 case IDENTIFIER_NODE:
92fab505
DG
2871 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
2872 ppd->visited);
4439d02f
DG
2873 *walk_subtrees = 0;
2874 return NULL_TREE;
2875
5d80a306
DG
2876 default:
2877 return NULL_TREE;
2878 }
5d80a306
DG
2879
2880 return NULL_TREE;
2881}
2882
2883/* Determines if the expression or type T uses any parameter packs. */
2884bool
2885uses_parameter_packs (tree t)
2886{
2887 tree parameter_packs = NULL_TREE;
2888 struct find_parameter_pack_data ppd;
2889 ppd.parameter_packs = &parameter_packs;
2890 ppd.visited = pointer_set_create ();
92fab505 2891 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
30bcc028 2892 pointer_set_destroy (ppd.visited);
5d80a306
DG
2893 return parameter_packs != NULL_TREE;
2894}
2895
2896/* Turn ARG, which may be an expression, type, or a TREE_LIST
2897 representation a base-class initializer into a parameter pack
2898 expansion. If all goes well, the resulting node will be an
2899 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
2900 respectively. */
2901tree
2902make_pack_expansion (tree arg)
2903{
2904 tree result;
2905 tree parameter_packs = NULL_TREE;
2906 bool for_types = false;
2907 struct find_parameter_pack_data ppd;
2908
2909 if (!arg || arg == error_mark_node)
2910 return arg;
2911
2912 if (TREE_CODE (arg) == TREE_LIST)
2913 {
2914 /* The only time we will see a TREE_LIST here is for a base
2915 class initializer. In this case, the TREE_PURPOSE will be a
2916 _TYPE node (representing the base class expansion we're
2917 initializing) and the TREE_VALUE will be a TREE_LIST
2918 containing the initialization arguments.
2919
2920 The resulting expansion looks somewhat different from most
2921 expansions. Rather than returning just one _EXPANSION, we
2922 return a TREE_LIST whose TREE_PURPOSE is a
2923 TYPE_PACK_EXPANSION containing the bases that will be
2924 initialized. The TREE_VALUE will be identical to the
2925 original TREE_VALUE, which is a list of arguments that will
2926 be passed to each base. We do not introduce any new pack
2927 expansion nodes into the TREE_VALUE (although it is possible
2928 that some already exist), because the TREE_PURPOSE and
2929 TREE_VALUE all need to be expanded together with the same
2930 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
2931 resulting TREE_PURPOSE will mention the parameter packs in
2932 both the bases and the arguments to the bases. */
2933 tree purpose;
2934 tree value;
2935 tree parameter_packs = NULL_TREE;
2936
2937 /* Determine which parameter packs will be used by the base
2938 class expansion. */
2939 ppd.visited = pointer_set_create ();
2940 ppd.parameter_packs = &parameter_packs;
14588106 2941 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
92fab505 2942 &ppd, ppd.visited);
5d80a306
DG
2943
2944 if (parameter_packs == NULL_TREE)
2945 {
2946 error ("base initializer expansion %<%T%> contains no parameter packs", arg);
30bcc028 2947 pointer_set_destroy (ppd.visited);
5d80a306
DG
2948 return error_mark_node;
2949 }
2950
2951 if (TREE_VALUE (arg) != void_type_node)
2952 {
2953 /* Collect the sets of parameter packs used in each of the
2954 initialization arguments. */
2955 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
2956 {
2957 /* Determine which parameter packs will be expanded in this
2958 argument. */
14588106 2959 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
92fab505 2960 &ppd, ppd.visited);
5d80a306
DG
2961 }
2962 }
2963
30bcc028
DG
2964 pointer_set_destroy (ppd.visited);
2965
5d80a306
DG
2966 /* Create the pack expansion type for the base type. */
2967 purpose = make_node (TYPE_PACK_EXPANSION);
2968 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
2969 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
2970
2971 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2972 they will rarely be compared to anything. */
2973 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
2974
2975 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
2976 }
2977
2978 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
2979 for_types = true;
2980
2981 /* Build the PACK_EXPANSION_* node. */
2982 result = make_node (for_types ? TYPE_PACK_EXPANSION : EXPR_PACK_EXPANSION);
2983 SET_PACK_EXPANSION_PATTERN (result, arg);
2984 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
2985 {
2986 /* Propagate type and const-expression information. */
2987 TREE_TYPE (result) = TREE_TYPE (arg);
2988 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
2989 }
2990 else
2991 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
2992 they will rarely be compared to anything. */
2993 SET_TYPE_STRUCTURAL_EQUALITY (result);
2994
2995 /* Determine which parameter packs will be expanded. */
2996 ppd.parameter_packs = &parameter_packs;
2997 ppd.visited = pointer_set_create ();
92fab505 2998 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
30bcc028 2999 pointer_set_destroy (ppd.visited);
5d80a306
DG
3000
3001 /* Make sure we found some parameter packs. */
3002 if (parameter_packs == NULL_TREE)
3003 {
3004 if (TYPE_P (arg))
3005 error ("expansion pattern %<%T%> contains no argument packs", arg);
3006 else
3007 error ("expansion pattern %<%E%> contains no argument packs", arg);
3008 return error_mark_node;
3009 }
3010 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3011
3012 return result;
3013}
3014
3015/* Checks T for any "bare" parameter packs, which have not yet been
3016 expanded, and issues an error if any are found. This operation can
3017 only be done on full expressions or types (e.g., an expression
3018 statement, "if" condition, etc.), because we could have expressions like:
3019
3020 foo(f(g(h(args)))...)
3021
3022 where "args" is a parameter pack. check_for_bare_parameter_packs
3023 should not be called for the subexpressions args, h(args),
3024 g(h(args)), or f(g(h(args))), because we would produce erroneous
b1d7b1c0
DG
3025 error messages.
3026
4439d02f
DG
3027 Returns TRUE and emits an error if there were bare parameter packs,
3028 returns FALSE otherwise. */
b1d7b1c0 3029bool
7b3e2d46 3030check_for_bare_parameter_packs (tree t)
5d80a306
DG
3031{
3032 tree parameter_packs = NULL_TREE;
3033 struct find_parameter_pack_data ppd;
3034
7b3e2d46 3035 if (!processing_template_decl || !t || t == error_mark_node)
4439d02f 3036 return false;
5d80a306 3037
7b3e2d46
DG
3038 if (TREE_CODE (t) == TYPE_DECL)
3039 t = TREE_TYPE (t);
5d80a306
DG
3040
3041 ppd.parameter_packs = &parameter_packs;
3042 ppd.visited = pointer_set_create ();
92fab505 3043 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
30bcc028 3044 pointer_set_destroy (ppd.visited);
5d80a306 3045
b1d7b1c0
DG
3046 if (parameter_packs)
3047 {
bcac2b89 3048 error ("parameter packs not expanded with %<...%>:");
b1d7b1c0
DG
3049 while (parameter_packs)
3050 {
3051 tree pack = TREE_VALUE (parameter_packs);
3052 tree name = NULL_TREE;
3053
3054 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3055 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3056 name = TYPE_NAME (pack);
3057 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3058 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3059 else
3060 name = DECL_NAME (pack);
1ad8aeeb
DG
3061
3062 if (name)
1f5b3869 3063 inform (input_location, " %qD", name);
1ad8aeeb 3064 else
1f5b3869 3065 inform (input_location, " <anonymous>");
b1d7b1c0
DG
3066
3067 parameter_packs = TREE_CHAIN (parameter_packs);
3068 }
3069
4439d02f 3070 return true;
b1d7b1c0
DG
3071 }
3072
4439d02f 3073 return false;
5d80a306
DG
3074}
3075
3076/* Expand any parameter packs that occur in the template arguments in
3077 ARGS. */
3078tree
3079expand_template_argument_pack (tree args)
3080{
3081 tree result_args = NULL_TREE;
3082 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3083 int num_result_args = -1;
3084
3085 /* First, determine if we need to expand anything, and the number of
3086 slots we'll need. */
3087 for (in_arg = 0; in_arg < nargs; ++in_arg)
3088 {
3089 tree arg = TREE_VEC_ELT (args, in_arg);
3090 if (ARGUMENT_PACK_P (arg))
3091 {
3092 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3093 if (num_result_args < 0)
3094 num_result_args = in_arg + num_packed;
3095 else
3096 num_result_args += num_packed;
3097 }
3098 else
3099 {
3100 if (num_result_args >= 0)
3101 num_result_args++;
3102 }
3103 }
3104
3105 /* If no expansion is necessary, we're done. */
3106 if (num_result_args < 0)
3107 return args;
3108
3109 /* Expand arguments. */
3110 result_args = make_tree_vec (num_result_args);
3111 for (in_arg = 0; in_arg < nargs; ++in_arg)
3112 {
3113 tree arg = TREE_VEC_ELT (args, in_arg);
3114 if (ARGUMENT_PACK_P (arg))
3115 {
3116 tree packed = ARGUMENT_PACK_ARGS (arg);
3117 int i, num_packed = TREE_VEC_LENGTH (packed);
3118 for (i = 0; i < num_packed; ++i, ++out_arg)
3119 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3120 }
3121 else
3122 {
3123 TREE_VEC_ELT (result_args, out_arg) = arg;
3124 ++out_arg;
3125 }
3126 }
3127
3128 return result_args;
3129}
3130
157b0647 3131/* Checks if DECL shadows a template parameter.
f3400fe2
JM
3132
3133 [temp.local]: A template-parameter shall not be redeclared within its
157b0647 3134 scope (including nested scopes).
f3400fe2 3135
157b0647
PC
3136 Emits an error and returns TRUE if the DECL shadows a parameter,
3137 returns FALSE otherwise. */
3138
3139bool
3a978d72 3140check_template_shadow (tree decl)
f3400fe2 3141{
8f032717
MM
3142 tree olddecl;
3143
b5d9b9ab
MM
3144 /* If we're not in a template, we can't possibly shadow a template
3145 parameter. */
3146 if (!current_template_parms)
157b0647 3147 return true;
b5d9b9ab
MM
3148
3149 /* Figure out what we're shadowing. */
8f032717
MM
3150 if (TREE_CODE (decl) == OVERLOAD)
3151 decl = OVL_CURRENT (decl);
90ea9897 3152 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
f3400fe2 3153
b5d9b9ab
MM
3154 /* If there's no previous binding for this name, we're not shadowing
3155 anything, let alone a template parameter. */
3156 if (!olddecl)
157b0647 3157 return true;
b5d9b9ab
MM
3158
3159 /* If we're not shadowing a template parameter, we're done. Note
3160 that OLDDECL might be an OVERLOAD (or perhaps even an
3161 ERROR_MARK), so we can't just blithely assume it to be a _DECL
3162 node. */
2f939d94 3163 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
157b0647 3164 return true;
b5d9b9ab
MM
3165
3166 /* We check for decl != olddecl to avoid bogus errors for using a
3167 name inside a class. We check TPFI to avoid duplicate errors for
3168 inline member templates. */
c8094d83 3169 if (decl == olddecl
b5d9b9ab 3170 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
157b0647 3171 return true;
b5d9b9ab 3172
dee15844
JM
3173 error ("declaration of %q+#D", decl);
3174 error (" shadows template parm %q+#D", olddecl);
157b0647 3175 return false;
f3400fe2 3176}
22a7be53 3177
f3400fe2 3178/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
f84b4be9
JM
3179 ORIG_LEVEL, DECL, and TYPE. */
3180
3181static tree
c8094d83 3182build_template_parm_index (int index,
0cbd7506
MS
3183 int level,
3184 int orig_level,
3185 tree decl,
3186 tree type)
f84b4be9
JM
3187{
3188 tree t = make_node (TEMPLATE_PARM_INDEX);
3189 TEMPLATE_PARM_IDX (t) = index;
3190 TEMPLATE_PARM_LEVEL (t) = level;
3191 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
3192 TEMPLATE_PARM_DECL (t) = decl;
3193 TREE_TYPE (t) = type;
3e4a3562
NS
3194 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
3195 TREE_READONLY (t) = TREE_READONLY (decl);
f84b4be9
JM
3196
3197 return t;
3198}
3199
06d40de8 3200/* Find the canonical type parameter for the given template type
c3e188fc
KH
3201 parameter. Returns the canonical type parameter, which may be TYPE
3202 if no such parameter existed. */
06d40de8
DG
3203static tree
3204canonical_type_parameter (tree type)
3205{
3206 tree list;
3207 int idx = TEMPLATE_TYPE_IDX (type);
3208 if (!canonical_template_parms)
3209 canonical_template_parms = VEC_alloc (tree, gc, idx+1);
3210
3211 while (VEC_length (tree, canonical_template_parms) <= (unsigned)idx)
3212 VEC_safe_push (tree, gc, canonical_template_parms, NULL_TREE);
3213
3214 list = VEC_index (tree, canonical_template_parms, idx);
96d84882 3215 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
06d40de8
DG
3216 list = TREE_CHAIN (list);
3217
3218 if (list)
3219 return TREE_VALUE (list);
3220 else
3221 {
3222 VEC_replace(tree, canonical_template_parms, idx,
3223 tree_cons (NULL_TREE, type,
3224 VEC_index (tree, canonical_template_parms, idx)));
3225 return type;
3226 }
3227}
3228
f84b4be9 3229/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
93cdc044 3230 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
f84b4be9
JM
3231 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
3232 new one is created. */
3233
c8094d83 3234static tree
ef3b7b17
JM
3235reduce_template_parm_level (tree index, tree type, int levels, tree args,
3236 tsubst_flags_t complain)
f84b4be9
JM
3237{
3238 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
3239 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
93cdc044 3240 != TEMPLATE_PARM_LEVEL (index) - levels))
f84b4be9 3241 {
3e4a3562
NS
3242 tree orig_decl = TEMPLATE_PARM_DECL (index);
3243 tree decl, t;
c8094d83 3244
c2255bc4
AH
3245 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
3246 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
3e4a3562
NS
3247 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
3248 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
3249 DECL_ARTIFICIAL (decl) = 1;
3250 SET_DECL_TEMPLATE_PARM_P (decl);
c8094d83 3251
3e4a3562 3252 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
93cdc044 3253 TEMPLATE_PARM_LEVEL (index) - levels,
f84b4be9
JM
3254 TEMPLATE_PARM_ORIG_LEVEL (index),
3255 decl, type);
3256 TEMPLATE_PARM_DESCENDANTS (index) = t;
5d80a306
DG
3257 TEMPLATE_PARM_PARAMETER_PACK (t)
3258 = TEMPLATE_PARM_PARAMETER_PACK (index);
cae40af6 3259
820cc88f 3260 /* Template template parameters need this. */
ef3b7b17
JM
3261 if (TREE_CODE (decl) == TEMPLATE_DECL)
3262 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
3263 (DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index)),
3264 args, complain);
f84b4be9
JM
3265 }
3266
3267 return TEMPLATE_PARM_DESCENDANTS (index);
3268}
3269
a14de1af 3270/* Process information from new template parameter PARM and append it to the
058b15c1 3271 LIST being built. This new parameter is a non-type parameter iff
5d80a306 3272 IS_NON_TYPE is true. This new parameter is a parameter
c2255bc4
AH
3273 pack iff IS_PARAMETER_PACK is true. The location of PARM is in
3274 PARM_LOC. */
e92cc029 3275
8d08fdba 3276tree
c2255bc4 3277process_template_parm (tree list, location_t parm_loc, tree parm, bool is_non_type,
5d80a306 3278 bool is_parameter_pack)
8d08fdba 3279{
8d08fdba 3280 tree decl = 0;
a292b002 3281 tree defval;
0f67a82f 3282 tree err_parm_list;
d47e3adf 3283 int idx = 0;
f84b4be9 3284
50bc768d 3285 gcc_assert (TREE_CODE (parm) == TREE_LIST);
a292b002 3286 defval = TREE_PURPOSE (parm);
5566b478
MS
3287
3288 if (list)
3289 {
d47e3adf
LM
3290 tree p = tree_last (list);
3291
0f67a82f 3292 if (p && TREE_VALUE (p) != error_mark_node)
d47e3adf
LM
3293 {
3294 p = TREE_VALUE (p);
3295 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
3296 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
3297 else
3298 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
3299 }
5566b478 3300
5566b478
MS
3301 ++idx;
3302 }
3303 else
3304 idx = 0;
3305
058b15c1 3306 if (is_non_type)
8d08fdba 3307 {
058b15c1
MM
3308 parm = TREE_VALUE (parm);
3309
833aa4c4 3310 SET_DECL_TEMPLATE_PARM_P (parm);
d490621d 3311
620188c9 3312 if (TREE_TYPE (parm) == error_mark_node)
0f67a82f
LM
3313 {
3314 err_parm_list = build_tree_list (defval, parm);
3315 TREE_VALUE (err_parm_list) = error_mark_node;
3316 return chainon (list, err_parm_list);
3317 }
620188c9
VR
3318 else
3319 {
3320 /* [temp.param]
d490621d 3321
620188c9
VR
3322 The top-level cv-qualifiers on the template-parameter are
3323 ignored when determining its type. */
3324 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
3325 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
0f67a82f
LM
3326 {
3327 err_parm_list = build_tree_list (defval, parm);
3328 TREE_VALUE (err_parm_list) = error_mark_node;
3329 return chainon (list, err_parm_list);
3330 }
5d80a306
DG
3331
3332 if (uses_parameter_packs (TREE_TYPE (parm)) && !is_parameter_pack)
3333 {
3334 /* This template parameter is not a parameter pack, but it
3335 should be. Complain about "bare" parameter packs. */
7b3e2d46 3336 check_for_bare_parameter_packs (TREE_TYPE (parm));
5d80a306
DG
3337
3338 /* Recover by calling this a parameter pack. */
3339 is_parameter_pack = true;
3340 }
620188c9 3341 }
d490621d 3342
8d08fdba 3343 /* A template parameter is not modifiable. */
6de9cd9a 3344 TREE_CONSTANT (parm) = 1;
6de9cd9a 3345 TREE_READONLY (parm) = 1;
c2255bc4
AH
3346 decl = build_decl (parm_loc,
3347 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
6de9cd9a 3348 TREE_CONSTANT (decl) = 1;
6de9cd9a 3349 TREE_READONLY (decl) = 1;
c8094d83 3350 DECL_INITIAL (parm) = DECL_INITIAL (decl)
f84b4be9
JM
3351 = build_template_parm_index (idx, processing_template_decl,
3352 processing_template_decl,
3353 decl, TREE_TYPE (parm));
5d80a306
DG
3354
3355 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
3356 = is_parameter_pack;
8d08fdba
MS
3357 }
3358 else
3359 {
73b0fce8 3360 tree t;
058b15c1 3361 parm = TREE_VALUE (TREE_VALUE (parm));
c8094d83 3362
73b0fce8
KL
3363 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
3364 {
9e1e64ec 3365 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
c8094d83 3366 /* This is for distinguishing between real templates and template
73b0fce8
KL
3367 template parameters */
3368 TREE_TYPE (parm) = t;
3369 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
3370 decl = parm;
3371 }
3372 else
3373 {
9e1e64ec 3374 t = cxx_make_type (TEMPLATE_TYPE_PARM);
f4f206f4 3375 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
c2255bc4
AH
3376 decl = build_decl (parm_loc,
3377 TYPE_DECL, parm, t);
73b0fce8 3378 }
c8094d83 3379
d2e5ee5c
MS
3380 TYPE_NAME (t) = decl;
3381 TYPE_STUB_DECL (t) = decl;
a292b002 3382 parm = decl;
f84b4be9 3383 TEMPLATE_TYPE_PARM_INDEX (t)
c8094d83 3384 = build_template_parm_index (idx, processing_template_decl,
f84b4be9
JM
3385 processing_template_decl,
3386 decl, TREE_TYPE (parm));
5d80a306 3387 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
06d40de8 3388 TYPE_CANONICAL (t) = canonical_type_parameter (t);
8d08fdba 3389 }
c727aa5e 3390 DECL_ARTIFICIAL (decl) = 1;
cd9f6678 3391 SET_DECL_TEMPLATE_PARM_P (decl);
8d08fdba 3392 pushdecl (decl);
a292b002 3393 parm = build_tree_list (defval, parm);
8d08fdba
MS
3394 return chainon (list, parm);
3395}
3396
3397/* The end of a template parameter list has been reached. Process the
3398 tree list into a parameter vector, converting each parameter into a more
3399 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
3400 as PARM_DECLs. */
3401
3402tree
3a978d72 3403end_template_parm_list (tree parms)
8d08fdba 3404{
5566b478 3405 int nparms;
9471d3e2 3406 tree parm, next;
5566b478
MS
3407 tree saved_parmlist = make_tree_vec (list_length (parms));
3408
5566b478 3409 current_template_parms
4890c2f4 3410 = tree_cons (size_int (processing_template_decl),
5566b478 3411 saved_parmlist, current_template_parms);
8d08fdba 3412
9471d3e2
NS
3413 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
3414 {
3415 next = TREE_CHAIN (parm);
3416 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
3417 TREE_CHAIN (parm) = NULL_TREE;
3418 }
a292b002 3419
67ffc812
MM
3420 --processing_template_parmlist;
3421
8d08fdba
MS
3422 return saved_parmlist;
3423}
3424
5566b478
MS
3425/* end_template_decl is called after a template declaration is seen. */
3426
8d08fdba 3427void
3a978d72 3428end_template_decl (void)
8d08fdba 3429{
386b8a85
JM
3430 reset_specialization ();
3431
5156628f 3432 if (! processing_template_decl)
73aad9b9
JM
3433 return;
3434
5566b478 3435 /* This matches the pushlevel in begin_template_parm_list. */
74b846e0 3436 finish_scope ();
8d08fdba 3437
5566b478
MS
3438 --processing_template_decl;
3439 current_template_parms = TREE_CHAIN (current_template_parms);
5566b478 3440}
8d08fdba 3441
03c17ccd
MM
3442/* Within the declaration of a template, return all levels of template
3443 parameters that apply. The template parameters are represented as
3444 a TREE_VEC, in the form documented in cp-tree.h for template
3445 arguments. */
9a3b49ac 3446
b5791fdc 3447static tree
3a978d72 3448current_template_args (void)
5566b478 3449{
36a117a5 3450 tree header;
b370501f 3451 tree args = NULL_TREE;
36a117a5 3452 int length = TMPL_PARMS_DEPTH (current_template_parms);
98c1c668
JM
3453 int l = length;
3454
36a117a5
MM
3455 /* If there is only one level of template parameters, we do not
3456 create a TREE_VEC of TREE_VECs. Instead, we return a single
3457 TREE_VEC containing the arguments. */
3458 if (length > 1)
3459 args = make_tree_vec (length);
3460
3461 for (header = current_template_parms; header; header = TREE_CHAIN (header))
8d08fdba 3462 {
5566b478 3463 tree a = copy_node (TREE_VALUE (header));
36a117a5
MM
3464 int i;
3465
5566b478 3466 TREE_TYPE (a) = NULL_TREE;
36a117a5 3467 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
5566b478 3468 {
98c1c668
JM
3469 tree t = TREE_VEC_ELT (a, i);
3470
36a117a5 3471 /* T will be a list if we are called from within a
98c1c668
JM
3472 begin/end_template_parm_list pair, but a vector directly
3473 if within a begin/end_member_template_processing pair. */
c8094d83 3474 if (TREE_CODE (t) == TREE_LIST)
98c1c668
JM
3475 {
3476 t = TREE_VALUE (t);
c8094d83 3477
0b93f014 3478 if (!error_operand_p (t))
0f67a82f
LM
3479 {
3480 if (TREE_CODE (t) == TYPE_DECL
3481 || TREE_CODE (t) == TEMPLATE_DECL)
5d80a306
DG
3482 {
3483 t = TREE_TYPE (t);
3484
3485 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3486 {
3487 /* Turn this argument into a TYPE_ARGUMENT_PACK
3488 with a single element, which expands T. */
3489 tree vec = make_tree_vec (1);
3490 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3491
3492 t = make_node (TYPE_ARGUMENT_PACK);
3493 SET_ARGUMENT_PACK_ARGS (t, vec);
3494 }
3495 }
3496 else
3497 {
3498 t = DECL_INITIAL (t);
3499
3500 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3501 {
3502 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
3503 with a single element, which expands T. */
3504 tree vec = make_tree_vec (1);
3505 tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
3506 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
3507
3508 t = make_node (NONTYPE_ARGUMENT_PACK);
3509 SET_ARGUMENT_PACK_ARGS (t, vec);
3510 TREE_TYPE (t) = type;
3511 }
3512 }
bf98d3b6 3513 TREE_VEC_ELT (a, i) = t;
5d80a306 3514 }
98c1c668 3515 }
5566b478 3516 }
36a117a5
MM
3517
3518 if (length > 1)
3519 TREE_VEC_ELT (args, --l) = a;
3520 else
3521 args = a;
8d08fdba
MS
3522 }
3523
9a3b49ac
MS
3524 return args;
3525}
75650646 3526
e1467ff2 3527/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
c7222c02 3528 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
ee81147e 3529 a member template. Used by push_template_decl below. */
e1467ff2 3530
75650646 3531static tree
c7222c02 3532build_template_decl (tree decl, tree parms, bool member_template_p)
75650646
MM
3533{
3534 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
3535 DECL_TEMPLATE_PARMS (tmpl) = parms;
3536 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
c7222c02 3537 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
75650646
MM
3538
3539 return tmpl;
3540}
3541
050367a3
MM
3542struct template_parm_data
3543{
6c30752f
MM
3544 /* The level of the template parameters we are currently
3545 processing. */
050367a3 3546 int level;
6c30752f
MM
3547
3548 /* The index of the specialization argument we are currently
3549 processing. */
3550 int current_arg;
3551
3552 /* An array whose size is the number of template parameters. The
838dfd8a 3553 elements are nonzero if the parameter has been used in any one
6c30752f 3554 of the arguments processed so far. */
050367a3 3555 int* parms;
6c30752f
MM
3556
3557 /* An array whose size is the number of template arguments. The
838dfd8a 3558 elements are nonzero if the argument makes use of template
6c30752f
MM
3559 parameters of this level. */
3560 int* arg_uses_template_parms;
050367a3
MM
3561};
3562
3563/* Subroutine of push_template_decl used to see if each template
3564 parameter in a partial specialization is used in the explicit
3565 argument list. If T is of the LEVEL given in DATA (which is
3566 treated as a template_parm_data*), then DATA->PARMS is marked
3567 appropriately. */
3568
3569static int
3a978d72 3570mark_template_parm (tree t, void* data)
050367a3
MM
3571{
3572 int level;
3573 int idx;
3574 struct template_parm_data* tpd = (struct template_parm_data*) data;
3575
3576 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
3577 {
3578 level = TEMPLATE_PARM_LEVEL (t);
3579 idx = TEMPLATE_PARM_IDX (t);
3580 }
3581 else
3582 {
3583 level = TEMPLATE_TYPE_LEVEL (t);
3584 idx = TEMPLATE_TYPE_IDX (t);
3585 }
3586
3587 if (level == tpd->level)
6c30752f
MM
3588 {
3589 tpd->parms[idx] = 1;
3590 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
3591 }
050367a3
MM
3592
3593 /* Return zero so that for_each_template_parm will continue the
3594 traversal of the tree; we want to mark *every* template parm. */
3595 return 0;
3596}
3597
6c30752f
MM
3598/* Process the partial specialization DECL. */
3599
e9659ab0 3600static tree
3a978d72 3601process_partial_specialization (tree decl)
6c30752f
MM
3602{
3603 tree type = TREE_TYPE (decl);
3604 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
3605 tree specargs = CLASSTYPE_TI_ARGS (type);
f9a7ae04 3606 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
6c30752f 3607 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
b17c40be 3608 tree inner_parms;
6c30752f 3609 int nargs = TREE_VEC_LENGTH (inner_args);
b17c40be 3610 int ntparms;
6c30752f
MM
3611 int i;
3612 int did_error_intro = 0;
6c30752f
MM
3613 struct template_parm_data tpd;
3614 struct template_parm_data tpd2;
3615
b17c40be
PC
3616 gcc_assert (current_template_parms);
3617
3618 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
3619 ntparms = TREE_VEC_LENGTH (inner_parms);
3620
6c30752f
MM
3621 /* We check that each of the template parameters given in the
3622 partial specialization is used in the argument list to the
3623 specialization. For example:
3624
3625 template <class T> struct S;
3626 template <class T> struct S<T*>;
3627
3628 The second declaration is OK because `T*' uses the template
3629 parameter T, whereas
3630
3631 template <class T> struct S<int>;
3632
3633 is no good. Even trickier is:
3634
3635 template <class T>
3636 struct S1
3637 {
3638 template <class U>
3639 struct S2;
3640 template <class U>
3641 struct S2<T>;
3642 };
3643
0e339752 3644 The S2<T> declaration is actually invalid; it is a
c8094d83 3645 full-specialization. Of course,
6c30752f
MM
3646
3647 template <class U>
3648 struct S2<T (*)(U)>;
3649
3650 or some such would have been OK. */
3651 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
67f5655f 3652 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
fad205ff 3653 memset (tpd.parms, 0, sizeof (int) * ntparms);
6c30752f 3654
67f5655f 3655 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
fad205ff 3656 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
6c30752f
MM
3657 for (i = 0; i < nargs; ++i)
3658 {
3659 tpd.current_arg = i;
3660 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
3661 &mark_template_parm,
4f2c9d7e 3662 &tpd,
c095a4f8
DG
3663 NULL,
3664 /*include_nondeduced_p=*/false);
6c30752f
MM
3665 }
3666 for (i = 0; i < ntparms; ++i)
3667 if (tpd.parms[i] == 0)
3668 {
3669 /* One of the template parms was not used in the
f9a7ae04 3670 specialization. */
6c30752f
MM
3671 if (!did_error_intro)
3672 {
33bd39a2 3673 error ("template parameters not used in partial specialization:");
6c30752f
MM
3674 did_error_intro = 1;
3675 }
3676
0f51ccfc 3677 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
6c30752f
MM
3678 }
3679
3680 /* [temp.class.spec]
3681
3682 The argument list of the specialization shall not be identical to
3683 the implicit argument list of the primary template. */
c8094d83
MS
3684 if (comp_template_args
3685 (inner_args,
f9a7ae04
MM
3686 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
3687 (maintmpl)))))
0f51ccfc 3688 error ("partial specialization %qT does not specialize any template arguments", type);
6c30752f
MM
3689
3690 /* [temp.class.spec]
3691
3692 A partially specialized non-type argument expression shall not
3693 involve template parameters of the partial specialization except
3694 when the argument expression is a simple identifier.
3695
3696 The type of a template parameter corresponding to a specialized
3697 non-type argument shall not be dependent on a parameter of the
5d80a306
DG
3698 specialization.
3699
3700 Also, we verify that pack expansions only occur at the
3701 end of the argument list. */
50bc768d 3702 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
6c30752f
MM
3703 tpd2.parms = 0;
3704 for (i = 0; i < nargs; ++i)
3705 {
5d80a306 3706 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
6c30752f 3707 tree arg = TREE_VEC_ELT (inner_args, i);
5d80a306
DG
3708 tree packed_args = NULL_TREE;
3709 int j, len = 1;
6c30752f 3710
5d80a306
DG
3711 if (ARGUMENT_PACK_P (arg))
3712 {
3713 /* Extract the arguments from the argument pack. We'll be
3714 iterating over these in the following loop. */
3715 packed_args = ARGUMENT_PACK_ARGS (arg);
3716 len = TREE_VEC_LENGTH (packed_args);
3717 }
3718
3719 for (j = 0; j < len; j++)
3720 {
3721 if (packed_args)
3722 /* Get the Jth argument in the parameter pack. */
3723 arg = TREE_VEC_ELT (packed_args, j);
3724
3725 if (PACK_EXPANSION_P (arg))
3726 {
3727 /* Pack expansions must come at the end of the
3728 argument list. */
3729 if ((packed_args && j < len - 1)
3730 || (!packed_args && i < nargs - 1))
3731 {
3732 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3733 error ("parameter pack argument %qE must be at the end of the template argument list", arg);
3734 else
1ad8aeeb
DG
3735 error ("parameter pack argument %qT must be at the end of the template argument list", arg);
3736
3737 if (packed_args)
3738 TREE_VEC_ELT (packed_args, j) = error_mark_node;
5d80a306
DG
3739 }
3740 }
3741
3742 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
3743 /* We only care about the pattern. */
3744 arg = PACK_EXPANSION_PATTERN (arg);
3745
3746 if (/* These first two lines are the `non-type' bit. */
3747 !TYPE_P (arg)
3748 && TREE_CODE (arg) != TEMPLATE_DECL
3749 /* This next line is the `argument expression is not just a
3750 simple identifier' condition and also the `specialized
3751 non-type argument' bit. */
3752 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
3753 {
3754 if ((!packed_args && tpd.arg_uses_template_parms[i])
3755 || (packed_args && uses_template_parms (arg)))
3756 error ("template argument %qE involves template parameter(s)",
3757 arg);
3758 else
3759 {
3760 /* Look at the corresponding template parameter,
3761 marking which template parameters its type depends
3762 upon. */
3763 tree type = TREE_TYPE (parm);
3764
3765 if (!tpd2.parms)
3766 {
3767 /* We haven't yet initialized TPD2. Do so now. */
3768 tpd2.arg_uses_template_parms
3769 = (int *) alloca (sizeof (int) * nargs);
3770 /* The number of parameters here is the number in the
3771 main template, which, as checked in the assertion
3772 above, is NARGS. */
3773 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
3774 tpd2.level =
3775 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
3776 }
3777
3778 /* Mark the template parameters. But this time, we're
3779 looking for the template parameters of the main
3780 template, not in the specialization. */
3781 tpd2.current_arg = i;
3782 tpd2.arg_uses_template_parms[i] = 0;
3783 memset (tpd2.parms, 0, sizeof (int) * nargs);
3784 for_each_template_parm (type,
3785 &mark_template_parm,
3786 &tpd2,
c095a4f8
DG
3787 NULL,
3788 /*include_nondeduced_p=*/false);
5d80a306
DG
3789
3790 if (tpd2.arg_uses_template_parms [i])
3791 {
3792 /* The type depended on some template parameters.
3793 If they are fully specialized in the
3794 specialization, that's OK. */
3795 int j;
3796 for (j = 0; j < nargs; ++j)
3797 if (tpd2.parms[j] != 0
3798 && tpd.arg_uses_template_parms [j])
3799 {
3800 error ("type %qT of template argument %qE depends "
3801 "on template parameter(s)",
3802 type,
3803 arg);
3804 break;
3805 }
3806 }
3807 }
3808 }
3809 }
6c30752f
MM
3810 }
3811
2b110bfc
JM
3812 /* We should only get here once. */
3813 gcc_assert (!COMPLETE_TYPE_P (type));
6c30752f 3814
d8b64f80 3815 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
916b63c3 3816 = tree_cons (specargs, inner_parms,
b1d7b1c0 3817 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
6c30752f
MM
3818 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
3819 return decl;
3820}
3821
85d85234
DG
3822/* Check that a template declaration's use of default arguments and
3823 parameter packs is not invalid. Here, PARMS are the template
3824 parameters. IS_PRIMARY is nonzero if DECL is the thing declared by
3825 a primary template. IS_PARTIAL is nonzero if DECL is a partial
3826 specialization.
9b7dd5e8 3827
6ba89f8e 3828
9b7dd5e8
DG
3829 IS_FRIEND_DECL is nonzero if DECL is a friend function template
3830 declaration (but not a definition); 1 indicates a declaration, 2
3831 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
3832 emitted for extraneous default arguments.
3833
3834 Returns TRUE if there were no errors found, FALSE otherwise. */
3835
3836bool
3837check_default_tmpl_args (tree decl, tree parms, int is_primary,
3838 int is_partial, int is_friend_decl)
6ba89f8e 3839{
d8e178a0 3840 const char *msg;
66191c20
MM
3841 int last_level_to_check;
3842 tree parm_level;
9b7dd5e8 3843 bool no_errors = true;
6ba89f8e 3844
c8094d83 3845 /* [temp.param]
6ba89f8e
MM
3846
3847 A default template-argument shall not be specified in a
3848 function template declaration or a function template definition, nor
3849 in the template-parameter-list of the definition of a member of a
3850 class template. */
3851
4f1c5b7d 3852 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
e0942dcd
MM
3853 /* You can't have a function template declaration in a local
3854 scope, nor you can you define a member of a class template in a
3855 local scope. */
9b7dd5e8 3856 return true;
e0942dcd 3857
6ba89f8e
MM
3858 if (current_class_type
3859 && !TYPE_BEING_DEFINED (current_class_type)
3d7de1fa 3860 && DECL_LANG_SPECIFIC (decl)
b97e8a14 3861 && DECL_DECLARES_FUNCTION_P (decl)
5937a6f9
MM
3862 /* If this is either a friend defined in the scope of the class
3863 or a member function. */
6df5158a
NS
3864 && (DECL_FUNCTION_MEMBER_P (decl)
3865 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
3866 : DECL_FRIEND_CONTEXT (decl)
3867 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
3868 : false)
5937a6f9
MM
3869 /* And, if it was a member function, it really was defined in
3870 the scope of the class. */
6df5158a
NS
3871 && (!DECL_FUNCTION_MEMBER_P (decl)
3872 || DECL_INITIALIZED_IN_CLASS_P (decl)))
6ba89f8e 3873 /* We already checked these parameters when the template was
5937a6f9
MM
3874 declared, so there's no need to do it again now. This function
3875 was defined in class scope, but we're processing it's body now
3876 that the class is complete. */
9b7dd5e8 3877 return true;
c8094d83 3878
9b7dd5e8
DG
3879 /* Core issue 226 (C++0x only): the following only applies to class
3880 templates. */
c1ae8be5 3881 if ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL)
66191c20 3882 {
9b7dd5e8 3883 /* [temp.param]
66191c20 3884
9b7dd5e8
DG
3885 If a template-parameter has a default template-argument, all
3886 subsequent template-parameters shall have a default
3887 template-argument supplied. */
3888 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
3889 {
3890 tree inner_parms = TREE_VALUE (parm_level);
3891 int ntparms = TREE_VEC_LENGTH (inner_parms);
3892 int seen_def_arg_p = 0;
3893 int i;
42b304f1 3894
9b7dd5e8
DG
3895 for (i = 0; i < ntparms; ++i)
3896 {
3897 tree parm = TREE_VEC_ELT (inner_parms, i);
42b304f1 3898
9b7dd5e8
DG
3899 if (parm == error_mark_node)
3900 continue;
3901
3902 if (TREE_PURPOSE (parm))
3903 seen_def_arg_p = 1;
ffaf1e05
JM
3904 else if (seen_def_arg_p
3905 && !template_parameter_pack_p (TREE_VALUE (parm)))
9b7dd5e8
DG
3906 {
3907 error ("no default argument for %qD", TREE_VALUE (parm));
3908 /* For better subsequent error-recovery, we indicate that
3909 there should have been a default argument. */
3910 TREE_PURPOSE (parm) = error_mark_node;
3911 no_errors = false;
3912 }
85d85234
DG
3913 else if (is_primary
3914 && !is_partial
3915 && !is_friend_decl
ffaf1e05
JM
3916 /* Don't complain about an enclosing partial
3917 specialization. */
3918 && parm_level == parms
85d85234
DG
3919 && TREE_CODE (decl) == TYPE_DECL
3920 && i < ntparms - 1
3921 && template_parameter_pack_p (TREE_VALUE (parm)))
3922 {
3923 /* A primary class template can only have one
3924 parameter pack, at the end of the template
3925 parameter list. */
3926
3927 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL)
3928 error ("parameter pack %qE must be at the end of the"
3929 " template parameter list", TREE_VALUE (parm));
3930 else
3931 error ("parameter pack %qT must be at the end of the"
3932 " template parameter list",
3933 TREE_TYPE (TREE_VALUE (parm)));
3934
3935 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
3936 = error_mark_node;
3937 no_errors = false;
3938 }
9b7dd5e8
DG
3939 }
3940 }
66191c20
MM
3941 }
3942
c1ae8be5 3943 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
9b7dd5e8
DG
3944 || is_partial
3945 || !is_primary
3946 || is_friend_decl)
6ba89f8e
MM
3947 /* For an ordinary class template, default template arguments are
3948 allowed at the innermost level, e.g.:
0cbd7506 3949 template <class T = int>
6ba89f8e
MM
3950 struct S {};
3951 but, in a partial specialization, they're not allowed even
3952 there, as we have in [temp.class.spec]:
c8094d83 3953
6ba89f8e 3954 The template parameter list of a specialization shall not
c8094d83 3955 contain default template argument values.
6ba89f8e 3956
9b7dd5e8
DG
3957 So, for a partial specialization, or for a function template
3958 (in C++98/C++03), we look at all of them. */
6ba89f8e
MM
3959 ;
3960 else
3961 /* But, for a primary class template that is not a partial
3962 specialization we look at all template parameters except the
3963 innermost ones. */
3964 parms = TREE_CHAIN (parms);
3965
3966 /* Figure out what error message to issue. */
9b7dd5e8
DG
3967 if (is_friend_decl == 2)
3968 msg = "default template arguments may not be used in function template friend re-declaration";
3969 else if (is_friend_decl)
3970 msg = "default template arguments may not be used in function template friend declarations";
c1ae8be5 3971 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
8653a2c3 3972 msg = "default template arguments may not be used in function templates";
6ba89f8e 3973 else if (is_partial)
8653a2c3 3974 msg = "default template arguments may not be used in partial specializations";
6ba89f8e 3975 else
0f51ccfc 3976 msg = "default argument for template parameter for class enclosing %qD";
6ba89f8e
MM
3977
3978 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
3979 /* If we're inside a class definition, there's no need to
104bf76a 3980 examine the parameters to the class itself. On the one
6ba89f8e 3981 hand, they will be checked when the class is defined, and,
0e339752 3982 on the other, default arguments are valid in things like:
0cbd7506
MS
3983 template <class T = double>
3984 struct S { template <class U> void f(U); };
6ba89f8e
MM
3985 Here the default argument for `S' has no bearing on the
3986 declaration of `f'. */
3987 last_level_to_check = template_class_depth (current_class_type) + 1;
3988 else
3989 /* Check everything. */
3990 last_level_to_check = 0;
3991
c8094d83
MS
3992 for (parm_level = parms;
3993 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
66191c20 3994 parm_level = TREE_CHAIN (parm_level))
6ba89f8e 3995 {
66191c20
MM
3996 tree inner_parms = TREE_VALUE (parm_level);
3997 int i;
3998 int ntparms;
6ba89f8e
MM
3999
4000 ntparms = TREE_VEC_LENGTH (inner_parms);
c8094d83 4001 for (i = 0; i < ntparms; ++i)
42b304f1
LM
4002 {
4003 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
4004 continue;
6ba89f8e 4005
42b304f1
LM
4006 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
4007 {
4008 if (msg)
4009 {
9b7dd5e8
DG
4010 no_errors = false;
4011 if (is_friend_decl == 2)
4012 return no_errors;
4013
42b304f1
LM
4014 error (msg, decl);
4015 msg = 0;
4016 }
4017
4018 /* Clear out the default argument so that we are not
4019 confused later. */
4020 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
4021 }
4022 }
6ba89f8e
MM
4023
4024 /* At this point, if we're still interested in issuing messages,
4025 they must apply to classes surrounding the object declared. */
4026 if (msg)
c8094d83 4027 msg = "default argument for template parameter for class enclosing %qD";
6ba89f8e 4028 }
9b7dd5e8
DG
4029
4030 return no_errors;
6ba89f8e
MM
4031}
4032
5dd236e2
NS
4033/* Worker for push_template_decl_real, called via
4034 for_each_template_parm. DATA is really an int, indicating the
4035 level of the parameters we are interested in. If T is a template
838dfd8a 4036 parameter of that level, return nonzero. */
5dd236e2
NS
4037
4038static int
3a978d72 4039template_parm_this_level_p (tree t, void* data)
5dd236e2 4040{
6e04241f 4041 int this_level = *(int *)data;
5dd236e2
NS
4042 int level;
4043
4044 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4045 level = TEMPLATE_PARM_LEVEL (t);
4046 else
4047 level = TEMPLATE_TYPE_LEVEL (t);
4048 return level == this_level;
4049}
4050
3ac3d9ea 4051/* Creates a TEMPLATE_DECL for the indicated DECL using the template
f84b4be9
JM
4052 parameters given by current_template_args, or reuses a
4053 previously existing one, if appropriate. Returns the DECL, or an
c8094d83 4054 equivalent one, if it is replaced via a call to duplicate_decls.
6757edfe 4055
d63d5d0c 4056 If IS_FRIEND is true, DECL is a friend declaration. */
3ac3d9ea
MM
4057
4058tree
d63d5d0c 4059push_template_decl_real (tree decl, bool is_friend)
9a3b49ac
MS
4060{
4061 tree tmpl;
f84b4be9 4062 tree args;
9a3b49ac 4063 tree info;
f84b4be9
JM
4064 tree ctx;
4065 int primary;
6ba89f8e 4066 int is_partial;
cfe507be 4067 int new_template_p = 0;
c7222c02
MM
4068 /* True if the template is a member template, in the sense of
4069 [temp.mem]. */
4070 bool member_template_p = false;
6ba89f8e 4071
b17c40be
PC
4072 if (decl == error_mark_node || !current_template_parms)
4073 return error_mark_node;
c0694c4b 4074
6ba89f8e 4075 /* See if this is a partial specialization. */
9188c363 4076 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
6ba89f8e 4077 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
370af2d5 4078 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
6757edfe 4079
d63d5d0c
ILT
4080 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
4081 is_friend = true;
f84b4be9
JM
4082
4083 if (is_friend)
4084 /* For a friend, we want the context of the friend function, not
4085 the type of which it is a friend. */
4086 ctx = DECL_CONTEXT (decl);
4f1c5b7d
MM
4087 else if (CP_DECL_CONTEXT (decl)
4088 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
f84b4be9
JM
4089 /* In the case of a virtual function, we want the class in which
4090 it is defined. */
4f1c5b7d 4091 ctx = CP_DECL_CONTEXT (decl);
f84b4be9 4092 else
dc957d14 4093 /* Otherwise, if we're currently defining some class, the DECL
f84b4be9 4094 is assumed to be a member of the class. */
9188c363 4095 ctx = current_scope ();
f84b4be9 4096
2c73f9f5
ML
4097 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
4098 ctx = NULL_TREE;
4099
4100 if (!DECL_CONTEXT (decl))
cb0dbb9a 4101 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 4102
6ba89f8e 4103 /* See if this is a primary template. */
c9cbfca6
JM
4104 if (is_friend && ctx)
4105 /* A friend template that specifies a class context, i.e.
4106 template <typename T> friend void A<T>::f();
4107 is not primary. */
4108 primary = 0;
4109 else
4110 primary = template_parm_scope_p ();
9a3b49ac 4111
83566abf
JM
4112 if (primary)
4113 {
c7222c02
MM
4114 if (DECL_CLASS_SCOPE_P (decl))
4115 member_template_p = true;
2f1b1731
MM
4116 if (TREE_CODE (decl) == TYPE_DECL
4117 && ANON_AGGRNAME_P (DECL_NAME (decl)))
80d7287f
PC
4118 {
4119 error ("template class without a name");
4120 return error_mark_node;
4121 }
717a7d5d 4122 else if (TREE_CODE (decl) == FUNCTION_DECL)
4b0d3cbe 4123 {
717a7d5d
MM
4124 if (DECL_DESTRUCTOR_P (decl))
4125 {
4126 /* [temp.mem]
c8094d83 4127
0cbd7506 4128 A destructor shall not be a member template. */
0f51ccfc 4129 error ("destructor %qD declared as member template", decl);
717a7d5d
MM
4130 return error_mark_node;
4131 }
4132 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
4133 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
4134 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
4135 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
4136 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
4137 == void_list_node)))
4138 {
c8094d83 4139 /* [basic.stc.dynamic.allocation]
717a7d5d 4140
0cbd7506 4141 An allocation function can be a function
717a7d5d
MM
4142 template. ... Template allocation functions shall
4143 have two or more parameters. */
0f51ccfc 4144 error ("invalid template declaration of %qD", decl);
7c60008e 4145 return error_mark_node;
717a7d5d 4146 }
4b0d3cbe 4147 }
8ca4bf25
MM
4148 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
4149 && CLASS_TYPE_P (TREE_TYPE (decl)))
2aaf816d
JM
4150 /* OK */;
4151 else
11325dcd 4152 {
0f51ccfc 4153 error ("template declaration of %q#D", decl);
11325dcd
KL
4154 return error_mark_node;
4155 }
83566abf
JM
4156 }
4157
6ba89f8e
MM
4158 /* Check to see that the rules regarding the use of default
4159 arguments are not being violated. */
c8094d83 4160 check_default_tmpl_args (decl, current_template_parms,
9b7dd5e8 4161 primary, is_partial, /*is_friend_decl=*/0);
73aad9b9 4162
5d80a306
DG
4163 /* Ensure that there are no parameter packs in the type of this
4164 declaration that have not been expanded. */
b1d7b1c0
DG
4165 if (TREE_CODE (decl) == FUNCTION_DECL)
4166 {
4167 /* Check each of the arguments individually to see if there are
4168 any bare parameter packs. */
4169 tree type = TREE_TYPE (decl);
4170 tree arg = DECL_ARGUMENTS (decl);
4171 tree argtype = TYPE_ARG_TYPES (type);
4172
4173 while (arg && argtype)
4174 {
4175 if (!FUNCTION_PARAMETER_PACK_P (arg)
7b3e2d46 4176 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
b1d7b1c0
DG
4177 {
4178 /* This is a PARM_DECL that contains unexpanded parameter
4179 packs. We have already complained about this in the
4180 check_for_bare_parameter_packs call, so just replace
4181 these types with ERROR_MARK_NODE. */
4182 TREE_TYPE (arg) = error_mark_node;
4183 TREE_VALUE (argtype) = error_mark_node;
4184 }
4185
4186 arg = TREE_CHAIN (arg);
4187 argtype = TREE_CHAIN (argtype);
4188 }
4189
4190 /* Check for bare parameter packs in the return type and the
4191 exception specifiers. */
7b3e2d46 4192 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
046e4071
JJ
4193 /* Errors were already issued, set return type to int
4194 as the frontend doesn't expect error_mark_node as
4195 the return type. */
4196 TREE_TYPE (type) = integer_type_node;
7b3e2d46
DG
4197 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
4198 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
b1d7b1c0 4199 }
7b3e2d46 4200 else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
92fab505
DG
4201 {
4202 TREE_TYPE (decl) = error_mark_node;
4203 return error_mark_node;
4204 }
5d80a306 4205
6ba89f8e
MM
4206 if (is_partial)
4207 return process_partial_specialization (decl);
d32789d8 4208
9a3b49ac
MS
4209 args = current_template_args ();
4210
c8094d83 4211 if (!ctx
f84b4be9 4212 || TREE_CODE (ctx) == FUNCTION_DECL
a723baf1 4213 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
f84b4be9 4214 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
8d08fdba 4215 {
75650646 4216 if (DECL_LANG_SPECIFIC (decl)
f84b4be9
JM
4217 && DECL_TEMPLATE_INFO (decl)
4218 && DECL_TI_TEMPLATE (decl))
4219 tmpl = DECL_TI_TEMPLATE (decl);
1c10870d
AS
4220 /* If DECL is a TYPE_DECL for a class-template, then there won't
4221 be DECL_LANG_SPECIFIC. The information equivalent to
4222 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
c8094d83 4223 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
1c10870d
AS
4224 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
4225 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
4226 {
4227 /* Since a template declaration already existed for this
4228 class-type, we must be redeclaring it here. Make sure
0e339752 4229 that the redeclaration is valid. */
1c10870d
AS
4230 redeclare_class_template (TREE_TYPE (decl),
4231 current_template_parms);
4232 /* We don't need to create a new TEMPLATE_DECL; just use the
4233 one we already had. */
4234 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
4235 }
f84b4be9 4236 else
786b5245 4237 {
c7222c02
MM
4238 tmpl = build_template_decl (decl, current_template_parms,
4239 member_template_p);
cfe507be
MM
4240 new_template_p = 1;
4241
f84b4be9
JM
4242 if (DECL_LANG_SPECIFIC (decl)
4243 && DECL_TEMPLATE_SPECIALIZATION (decl))
4244 {
4245 /* A specialization of a member template of a template
c6002625 4246 class. */
f84b4be9
JM
4247 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4248 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
4249 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
4250 }
786b5245 4251 }
8d08fdba
MS
4252 }
4253 else
4254 {
e1a5ccf7 4255 tree a, t, current, parms;
ba4f4e5d 4256 int i;
91a77d68 4257 tree tinfo = get_template_info (decl);
6633d636 4258
91a77d68 4259 if (!tinfo)
c91a56d2 4260 {
0f51ccfc 4261 error ("template definition of non-template %q#D", decl);
3fe82414 4262 return error_mark_node;
c91a56d2 4263 }
91a77d68
JM
4264
4265 tmpl = TI_TEMPLATE (tinfo);
c8094d83 4266
c353b8e3 4267 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
c8094d83 4268 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
c353b8e3 4269 && DECL_TEMPLATE_SPECIALIZATION (decl)
c7222c02 4270 && DECL_MEMBER_TEMPLATE_P (tmpl))
98c1c668 4271 {
e1a5ccf7
JM
4272 tree new_tmpl;
4273
4274 /* The declaration is a specialization of a member
4275 template, declared outside the class. Therefore, the
4276 innermost template arguments will be NULL, so we
4277 replace them with the arguments determined by the
4278 earlier call to check_explicit_specialization. */
4279 args = DECL_TI_ARGS (decl);
4280
c8094d83 4281 new_tmpl
c7222c02
MM
4282 = build_template_decl (decl, current_template_parms,
4283 member_template_p);
e1a5ccf7
JM
4284 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
4285 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
4286 DECL_TI_TEMPLATE (decl) = new_tmpl;
4287 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
c8094d83 4288 DECL_TEMPLATE_INFO (new_tmpl)
e1b3e07d 4289 = tree_cons (tmpl, args, NULL_TREE);
e1a5ccf7 4290
c8094d83
MS
4291 register_specialization (new_tmpl,
4292 most_general_template (tmpl),
d63d5d0c 4293 args,
2b110bfc 4294 is_friend, 0);
e1a5ccf7 4295 return decl;
98c1c668 4296 }
98c1c668 4297
e1a5ccf7 4298 /* Make sure the template headers we got make sense. */
6633d636 4299
e1a5ccf7
JM
4300 parms = DECL_TEMPLATE_PARMS (tmpl);
4301 i = TMPL_PARMS_DEPTH (parms);
4302 if (TMPL_ARGS_DEPTH (args) != i)
98c1c668 4303 {
0f51ccfc 4304 error ("expected %d levels of template parms for %q#D, got %d",
0cbd7506 4305 i, decl, TMPL_ARGS_DEPTH (args));
98c1c668 4306 }
e1a5ccf7
JM
4307 else
4308 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
4309 {
4310 a = TMPL_ARGS_LEVEL (args, i);
4311 t = INNERMOST_TEMPLATE_PARMS (parms);
4312
4313 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
4314 {
4315 if (current == decl)
0f51ccfc 4316 error ("got %d template parameters for %q#D",
0cbd7506 4317 TREE_VEC_LENGTH (a), decl);
e1a5ccf7 4318 else
0f51ccfc 4319 error ("got %d template parameters for %q#T",
0cbd7506 4320 TREE_VEC_LENGTH (a), current);
33bd39a2 4321 error (" but %d required", TREE_VEC_LENGTH (t));
f1cc0515 4322 return error_mark_node;
e1a5ccf7 4323 }
98c1c668 4324
e1a5ccf7
JM
4325 if (current == decl)
4326 current = ctx;
4327 else
ba75df2b
PC
4328 current = (TYPE_P (current)
4329 ? TYPE_CONTEXT (current)
4330 : DECL_CONTEXT (current));
e1a5ccf7 4331 }
91a77d68
JM
4332
4333 /* Check that the parms are used in the appropriate qualifying scopes
4334 in the declarator. */
4335 if (!comp_template_args
4336 (TI_ARGS (tinfo),
4337 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
4338 {
4339 error ("\
4340template arguments to %qD do not match original template %qD",
4341 decl, DECL_TEMPLATE_RESULT (tmpl));
4342 if (!uses_template_parms (TI_ARGS (tinfo)))
1f5b3869 4343 inform (input_location, "use template<> for an explicit specialization");
91a77d68
JM
4344 /* Avoid crash in import_export_decl. */
4345 DECL_INTERFACE_KNOWN (decl) = 1;
4346 return error_mark_node;
4347 }
5566b478 4348 }
8d08fdba 4349
5566b478
MS
4350 DECL_TEMPLATE_RESULT (tmpl) = decl;
4351 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 4352
36a117a5
MM
4353 /* Push template declarations for global functions and types. Note
4354 that we do not try to push a global template friend declared in a
4355 template class; such a thing may well depend on the template
39c01e4c 4356 parameters of the class. */
c8094d83 4357 if (new_template_p && !ctx
36a117a5 4358 && !(is_friend && template_class_depth (current_class_type) > 0))
c6f9f83b 4359 {
d63d5d0c 4360 tmpl = pushdecl_namespace_level (tmpl, is_friend);
c6f9f83b
KL
4361 if (tmpl == error_mark_node)
4362 return error_mark_node;
bd3d082e
KL
4363
4364 /* Hide template friend classes that haven't been declared yet. */
4365 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
4366 {
4367 DECL_ANTICIPATED (tmpl) = 1;
4368 DECL_FRIEND_P (tmpl) = 1;
4369 }
c6f9f83b 4370 }
8d08fdba 4371
5566b478 4372 if (primary)
5dd236e2 4373 {
ef3b7b17
JM
4374 tree parms = DECL_TEMPLATE_PARMS (tmpl);
4375 int i;
4376
5dd236e2
NS
4377 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
4378 if (DECL_CONV_FN_P (tmpl))
4379 {
ef3b7b17 4380 int depth = TMPL_PARMS_DEPTH (parms);
6e04241f 4381
5dd236e2
NS
4382 /* It is a conversion operator. See if the type converted to
4383 depends on innermost template operands. */
c8094d83 4384
d43f603d
KL
4385 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
4386 depth))
5dd236e2
NS
4387 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
4388 }
ef3b7b17
JM
4389
4390 /* Give template template parms a DECL_CONTEXT of the template
4391 for which they are a parameter. */
4392 parms = INNERMOST_TEMPLATE_PARMS (parms);
4393 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
4394 {
4395 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4396 if (TREE_CODE (parm) == TEMPLATE_DECL)
4397 DECL_CONTEXT (parm) = tmpl;
4398 }
5dd236e2 4399 }
5566b478 4400
a692ad2e 4401 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
28ba38e3
KL
4402 back to its most general template. If TMPL is a specialization,
4403 ARGS may only have the innermost set of arguments. Add the missing
4404 argument levels if necessary. */
4405 if (DECL_TEMPLATE_INFO (tmpl))
4406 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
4407
e1b3e07d 4408 info = tree_cons (tmpl, args, NULL_TREE);
5566b478 4409
9188c363 4410 if (DECL_IMPLICIT_TYPEDEF_P (decl))
a0ad3539 4411 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2aaf816d 4412 else if (DECL_LANG_SPECIFIC (decl))
5566b478 4413 DECL_TEMPLATE_INFO (decl) = info;
3ac3d9ea
MM
4414
4415 return DECL_TEMPLATE_RESULT (tmpl);
8d08fdba
MS
4416}
4417
6757edfe 4418tree
3a978d72 4419push_template_decl (tree decl)
6757edfe 4420{
d63d5d0c 4421 return push_template_decl_real (decl, false);
6757edfe
MM
4422}
4423
4424/* Called when a class template TYPE is redeclared with the indicated
4425 template PARMS, e.g.:
7fe6899f
MM
4426
4427 template <class T> struct S;
4428 template <class T> struct S {}; */
4429
60feef2c 4430bool
3a978d72 4431redeclare_class_template (tree type, tree parms)
7fe6899f 4432{
3d7de1fa 4433 tree tmpl;
6757edfe 4434 tree tmpl_parms;
7fe6899f
MM
4435 int i;
4436
3d7de1fa
MM
4437 if (!TYPE_TEMPLATE_INFO (type))
4438 {
0f51ccfc 4439 error ("%qT is not a template type", type);
60feef2c 4440 return false;
3d7de1fa
MM
4441 }
4442
4443 tmpl = TYPE_TI_TEMPLATE (type);
7fe6899f
MM
4444 if (!PRIMARY_TEMPLATE_P (tmpl))
4445 /* The type is nested in some template class. Nothing to worry
4446 about here; there are no new template parameters for the nested
4447 type. */
60feef2c 4448 return true;
7fe6899f 4449
ee921ddf
MM
4450 if (!parms)
4451 {
4452 error ("template specifiers not specified in declaration of %qD",
4453 tmpl);
60feef2c 4454 return false;
ee921ddf
MM
4455 }
4456
6757edfe
MM
4457 parms = INNERMOST_TEMPLATE_PARMS (parms);
4458 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
4459
7fe6899f
MM
4460 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
4461 {
b131ad7c
MLI
4462 error ("redeclared with %d template parameter(s)",
4463 TREE_VEC_LENGTH (parms));
1f5b3869 4464 inform (input_location, "previous declaration %q+D used %d template parameter(s)",
b131ad7c 4465 tmpl, TREE_VEC_LENGTH (tmpl_parms));
60feef2c 4466 return false;
7fe6899f
MM
4467 }
4468
4469 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
4470 {
2d8ba2c7
LM
4471 tree tmpl_parm;
4472 tree parm;
4473 tree tmpl_default;
4474 tree parm_default;
4475
4476 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
4477 || TREE_VEC_ELT (parms, i) == error_mark_node)
4478 continue;
4479
4480 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
4481 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4482 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
4483 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
7fe6899f 4484
2649701f
KL
4485 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
4486 TEMPLATE_DECL. */
0f67a82f 4487 if (tmpl_parm != error_mark_node
a022041e
DG
4488 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
4489 || (TREE_CODE (tmpl_parm) != TYPE_DECL
4490 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
4491 || (TREE_CODE (tmpl_parm) != PARM_DECL
4492 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
4493 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
4494 || (TREE_CODE (tmpl_parm) == PARM_DECL
4495 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
4496 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
7fe6899f 4497 {
dee15844 4498 error ("template parameter %q+#D", tmpl_parm);
0f51ccfc 4499 error ("redeclared here as %q#D", parm);
60feef2c 4500 return false;
7fe6899f
MM
4501 }
4502
4503 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
4504 {
4505 /* We have in [temp.param]:
4506
4507 A template-parameter may not be given default arguments
4508 by two different declarations in the same scope. */
c5d75364
MLI
4509 error_at (input_location, "redefinition of default argument for %q#D", parm);
4510 inform (DECL_SOURCE_LOCATION (tmpl_parm),
4511 "original definition appeared here");
60feef2c 4512 return false;
7fe6899f
MM
4513 }
4514
4515 if (parm_default != NULL_TREE)
4516 /* Update the previous template parameters (which are the ones
4517 that will really count) with the new default value. */
4518 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
7ce74c5e
MM
4519 else if (tmpl_default != NULL_TREE)
4520 /* Update the new parameters, too; they'll be used as the
4521 parameters for any members. */
4522 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
7fe6899f 4523 }
60feef2c
LM
4524
4525 return true;
7fe6899f 4526}
75650646 4527
9baa27a9
MM
4528/* Simplify EXPR if it is a non-dependent expression. Returns the
4529 (possibly simplified) expression. */
4530
4531tree
4532fold_non_dependent_expr (tree expr)
4533{
d4a200d3
SM
4534 if (expr == NULL_TREE)
4535 return NULL_TREE;
4536
9baa27a9
MM
4537 /* If we're in a template, but EXPR isn't value dependent, simplify
4538 it. We're supposed to treat:
c8094d83 4539
9baa27a9
MM
4540 template <typename T> void f(T[1 + 1]);
4541 template <typename T> void f(T[2]);
c8094d83 4542
9baa27a9
MM
4543 as two declarations of the same function, for example. */
4544 if (processing_template_decl
4545 && !type_dependent_expression_p (expr)
7416ab02 4546 && !value_dependent_expression_p (expr))
9baa27a9
MM
4547 {
4548 HOST_WIDE_INT saved_processing_template_decl;
4549
4550 saved_processing_template_decl = processing_template_decl;
4551 processing_template_decl = 0;
4552 expr = tsubst_copy_and_build (expr,
4553 /*args=*/NULL_TREE,
4554 tf_error,
4555 /*in_decl=*/NULL_TREE,
015c2c66
MM
4556 /*function_p=*/false,
4557 /*integral_constant_expression_p=*/true);
9baa27a9
MM
4558 processing_template_decl = saved_processing_template_decl;
4559 }
4560 return expr;
4561}
4562
b6ab6892
GB
4563/* EXPR is an expression which is used in a constant-expression context.
4564 For instance, it could be a VAR_DECL with a constant initializer.
39a13be5 4565 Extract the innermost constant expression.
c8094d83 4566
8a784e4a
NS
4567 This is basically a more powerful version of
4568 integral_constant_value, which can be used also in templates where
4569 initializers can maintain a syntactic rather than semantic form
4570 (even if they are non-dependent, for access-checking purposes). */
b6ab6892 4571
993acaec 4572static tree
b6ab6892
GB
4573fold_decl_constant_value (tree expr)
4574{
4ef69b83
GB
4575 tree const_expr = expr;
4576 do
b6ab6892 4577 {
b6ab6892 4578 expr = fold_non_dependent_expr (const_expr);
4ef69b83 4579 const_expr = integral_constant_value (expr);
b6ab6892 4580 }
4ef69b83 4581 while (expr != const_expr);
b6ab6892 4582
8a784e4a 4583 return expr;
b6ab6892
GB
4584}
4585
4586/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
4587 must be a function or a pointer-to-function type, as specified
4588 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
4589 and check that the resulting function has external linkage. */
4590
4591static tree
4592convert_nontype_argument_function (tree type, tree expr)
4593{
4594 tree fns = expr;
4595 tree fn, fn_no_ptr;
4596
4597 fn = instantiate_type (type, fns, tf_none);
4598 if (fn == error_mark_node)
4599 return error_mark_node;
4600
4601 fn_no_ptr = fn;
4602 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
4603 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
eff3a276
MM
4604 if (TREE_CODE (fn_no_ptr) == BASELINK)
4605 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
4606
b6ab6892
GB
4607 /* [temp.arg.nontype]/1
4608
4609 A template-argument for a non-type, non-template template-parameter
4610 shall be one of:
4611 [...]
4612 -- the address of an object or function with external linkage. */
4613 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
4614 {
4615 error ("%qE is not a valid template argument for type %qT "
4616 "because function %qD has not external linkage",
4617 expr, type, fn_no_ptr);
4618 return NULL_TREE;
4619 }
4620
4621 return fn;
4622}
4623
75650646
MM
4624/* Attempt to convert the non-type template parameter EXPR to the
4625 indicated TYPE. If the conversion is successful, return the
dc957d14 4626 converted value. If the conversion is unsuccessful, return
75650646
MM
4627 NULL_TREE if we issued an error message, or error_mark_node if we
4628 did not. We issue error messages for out-and-out bad template
4629 parameters, but not simply because the conversion failed, since we
9baa27a9 4630 might be just trying to do argument deduction. Both TYPE and EXPR
b6ab6892
GB
4631 must be non-dependent.
4632
4633 The conversion follows the special rules described in
4634 [temp.arg.nontype], and it is much more strict than an implicit
4635 conversion.
4636
4637 This function is called twice for each template argument (see
4638 lookup_template_class for a more accurate description of this
4639 problem). This means that we need to handle expressions which
4640 are not valid in a C++ source, but can be created from the
4641 first call (for instance, casts to perform conversions). These
4642 hacks can go away after we fix the double coercion problem. */
75650646
MM
4643
4644static tree
3a978d72 4645convert_nontype_argument (tree type, tree expr)
75650646 4646{
9baa27a9
MM
4647 tree expr_type;
4648
b6ab6892
GB
4649 /* Detect immediately string literals as invalid non-type argument.
4650 This special-case is not needed for correctness (we would easily
4651 catch this later), but only to provide better diagnostic for this
4652 common user mistake. As suggested by DR 100, we do not mention
4653 linkage issues in the diagnostic as this is not the point. */
4654 if (TREE_CODE (expr) == STRING_CST)
4655 {
4656 error ("%qE is not a valid template argument for type %qT "
4657 "because string literals can never be used in this context",
4658 expr, type);
4659 return NULL_TREE;
4660 }
4661
9baa27a9
MM
4662 /* If we are in a template, EXPR may be non-dependent, but still
4663 have a syntactic, rather than semantic, form. For example, EXPR
4664 might be a SCOPE_REF, rather than the VAR_DECL to which the
4665 SCOPE_REF refers. Preserving the qualifying scope is necessary
4666 so that access checking can be performed when the template is
4667 instantiated -- but here we need the resolved form so that we can
4668 convert the argument. */
4669 expr = fold_non_dependent_expr (expr);
b166a559
VR
4670 if (error_operand_p (expr))
4671 return error_mark_node;
9baa27a9 4672 expr_type = TREE_TYPE (expr);
75650646 4673
b6ab6892
GB
4674 /* HACK: Due to double coercion, we can get a
4675 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
4676 which is the tree that we built on the first call (see
4677 below when coercing to reference to object or to reference to
4678 function). We just strip everything and get to the arg.
4679 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
4680 for examples. */
4681 if (TREE_CODE (expr) == NOP_EXPR)
75650646 4682 {
b6ab6892 4683 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
75650646 4684 {
b6ab6892
GB
4685 /* ??? Maybe we could use convert_from_reference here, but we
4686 would need to relax its constraints because the NOP_EXPR
4687 could actually change the type to something more cv-qualified,
4688 and this is not folded by convert_from_reference. */
4689 tree addr = TREE_OPERAND (expr, 0);
4690 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
4691 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
4692 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
4693 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4694 (TREE_TYPE (expr_type),
4695 TREE_TYPE (TREE_TYPE (addr))));
4696
4697 expr = TREE_OPERAND (addr, 0);
4698 expr_type = TREE_TYPE (expr);
75650646
MM
4699 }
4700
b6ab6892
GB
4701 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
4702 parameter is a pointer to object, through decay and
4703 qualification conversion. Let's strip everything. */
4704 else if (TYPE_PTROBV_P (type))
75650646 4705 {
b6ab6892
GB
4706 STRIP_NOPS (expr);
4707 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
4708 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
4709 /* Skip the ADDR_EXPR only if it is part of the decay for
4710 an array. Otherwise, it is part of the original argument
4711 in the source code. */
4712 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
4713 expr = TREE_OPERAND (expr, 0);
4714 expr_type = TREE_TYPE (expr);
f3400fe2 4715 }
75650646 4716 }
c61dce3a 4717
b6ab6892 4718 /* [temp.arg.nontype]/5, bullet 1
75650646 4719
b6ab6892
GB
4720 For a non-type template-parameter of integral or enumeration type,
4721 integral promotions (_conv.prom_) and integral conversions
4722 (_conv.integral_) are applied. */
550a799d 4723 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
75650646 4724 {
550a799d 4725 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (expr_type))
75650646 4726 return error_mark_node;
fddabb2c 4727
b6ab6892
GB
4728 expr = fold_decl_constant_value (expr);
4729 /* Notice that there are constant expressions like '4 % 0' which
4730 do not fold into integer constants. */
db02b6b9 4731 if (TREE_CODE (expr) != INTEGER_CST)
b6ab6892
GB
4732 {
4733 error ("%qE is not a valid template argument for type %qT "
4734 "because it is a non-constant expression", expr, type);
4735 return NULL_TREE;
4736 }
75650646 4737
b6ab6892
GB
4738 /* At this point, an implicit conversion does what we want,
4739 because we already know that the expression is of integral
4740 type. */
4741 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
4742 if (expr == error_mark_node)
4743 return error_mark_node;
75650646 4744
b6ab6892
GB
4745 /* Conversion was allowed: fold it to a bare integer constant. */
4746 expr = fold (expr);
4747 }
4748 /* [temp.arg.nontype]/5, bullet 2
75650646 4749
b6ab6892
GB
4750 For a non-type template-parameter of type pointer to object,
4751 qualification conversions (_conv.qual_) and the array-to-pointer
4752 conversion (_conv.array_) are applied. */
4753 else if (TYPE_PTROBV_P (type))
4754 {
4755 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
75650646 4756
b6ab6892
GB
4757 A template-argument for a non-type, non-template template-parameter
4758 shall be one of: [...]
75650646 4759
b6ab6892
GB
4760 -- the name of a non-type template-parameter;
4761 -- the address of an object or function with external linkage, [...]
4762 expressed as "& id-expression" where the & is optional if the name
4763 refers to a function or array, or if the corresponding
4764 template-parameter is a reference.
c8094d83 4765
b6ab6892
GB
4766 Here, we do not care about functions, as they are invalid anyway
4767 for a parameter of type pointer-to-object. */
1082fd10
MM
4768
4769 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
4770 /* Non-type template parameters are OK. */
4771 ;
4772 else if (TREE_CODE (expr) != ADDR_EXPR
4773 && TREE_CODE (expr_type) != ARRAY_TYPE)
4774 {
4775 if (TREE_CODE (expr) == VAR_DECL)
4776 {
4777 error ("%qD is not a valid template argument "
4778 "because %qD is a variable, not the address of "
4779 "a variable",
4780 expr, expr);
4781 return NULL_TREE;
4782 }
4783 /* Other values, like integer constants, might be valid
4784 non-type arguments of some other type. */
4785 return error_mark_node;
4786 }
4787 else
4788 {
4789 tree decl;
4790
4791 decl = ((TREE_CODE (expr) == ADDR_EXPR)
4792 ? TREE_OPERAND (expr, 0) : expr);
4793 if (TREE_CODE (decl) != VAR_DECL)
4794 {
4795 error ("%qE is not a valid template argument of type %qT "
4796 "because %qE is not a variable",
4797 expr, type, decl);
4798 return NULL_TREE;
4799 }
4800 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
4801 {
4802 error ("%qE is not a valid template argument of type %qT "
4803 "because %qD does not have external linkage",
4804 expr, type, decl);
4805 return NULL_TREE;
4806 }
4807 }
b6ab6892
GB
4808
4809 expr = decay_conversion (expr);
4810 if (expr == error_mark_node)
4811 return error_mark_node;
75650646 4812
b6ab6892
GB
4813 expr = perform_qualification_conversions (type, expr);
4814 if (expr == error_mark_node)
4815 return error_mark_node;
b6ab6892
GB
4816 }
4817 /* [temp.arg.nontype]/5, bullet 3
4818
4819 For a non-type template-parameter of type reference to object, no
4820 conversions apply. The type referred to by the reference may be more
4821 cv-qualified than the (otherwise identical) type of the
4822 template-argument. The template-parameter is bound directly to the
4823 template-argument, which must be an lvalue. */
4824 else if (TYPE_REF_OBJ_P (type))
4825 {
4826 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
4827 expr_type))
4828 return error_mark_node;
75650646 4829
b6ab6892
GB
4830 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
4831 {
4832 error ("%qE is not a valid template argument for type %qT "
4833 "because of conflicts in cv-qualification", expr, type);
4834 return NULL_TREE;
4835 }
c8094d83 4836
b6ab6892
GB
4837 if (!real_lvalue_p (expr))
4838 {
4839 error ("%qE is not a valid template argument for type %qT "
944fd207 4840 "because it is not an lvalue", expr, type);
b6ab6892
GB
4841 return NULL_TREE;
4842 }
e6e174e5 4843
b6ab6892 4844 /* [temp.arg.nontype]/1
75650646 4845
b6ab6892
GB
4846 A template-argument for a non-type, non-template template-parameter
4847 shall be one of: [...]
75650646 4848
03fd3f84 4849 -- the address of an object or function with external linkage. */
b6ab6892
GB
4850 if (!DECL_EXTERNAL_LINKAGE_P (expr))
4851 {
4852 error ("%qE is not a valid template argument for type %qT "
4853 "because object %qD has not external linkage",
4854 expr, type, expr);
4855 return NULL_TREE;
4856 }
0dc09a61 4857
b6ab6892
GB
4858 expr = build_nop (type, build_address (expr));
4859 }
4860 /* [temp.arg.nontype]/5, bullet 4
4861
4862 For a non-type template-parameter of type pointer to function, only
4863 the function-to-pointer conversion (_conv.func_) is applied. If the
4864 template-argument represents a set of overloaded functions (or a
4865 pointer to such), the matching function is selected from the set
4866 (_over.over_). */
4867 else if (TYPE_PTRFN_P (type))
4868 {
4869 /* If the argument is a template-id, we might not have enough
2c164de6 4870 context information to decay the pointer. */
b6ab6892
GB
4871 if (!type_unknown_p (expr_type))
4872 {
4873 expr = decay_conversion (expr);
4874 if (expr == error_mark_node)
4875 return error_mark_node;
4876 }
75650646 4877
b6ab6892
GB
4878 expr = convert_nontype_argument_function (type, expr);
4879 if (!expr || expr == error_mark_node)
4880 return expr;
e350dbbd
PB
4881
4882 if (TREE_CODE (expr) != ADDR_EXPR)
4883 {
4884 error ("%qE is not a valid template argument for type %qT", expr, type);
4885 error ("it must be the address of a function with external linkage");
4886 return NULL_TREE;
4887 }
b6ab6892
GB
4888 }
4889 /* [temp.arg.nontype]/5, bullet 5
75650646 4890
b6ab6892
GB
4891 For a non-type template-parameter of type reference to function, no
4892 conversions apply. If the template-argument represents a set of
4893 overloaded functions, the matching function is selected from the set
4894 (_over.over_). */
4895 else if (TYPE_REFFN_P (type))
4896 {
4897 if (TREE_CODE (expr) == ADDR_EXPR)
4898 {
4899 error ("%qE is not a valid template argument for type %qT "
4900 "because it is a pointer", expr, type);
1f5b3869 4901 inform (input_location, "try using %qE instead", TREE_OPERAND (expr, 0));
b6ab6892
GB
4902 return NULL_TREE;
4903 }
75650646 4904
b6ab6892
GB
4905 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
4906 if (!expr || expr == error_mark_node)
4907 return expr;
75650646 4908
7866705a 4909 expr = build_nop (type, build_address (expr));
b6ab6892
GB
4910 }
4911 /* [temp.arg.nontype]/5, bullet 6
75650646 4912
b6ab6892
GB
4913 For a non-type template-parameter of type pointer to member function,
4914 no conversions apply. If the template-argument represents a set of
4915 overloaded member functions, the matching member function is selected
4916 from the set (_over.over_). */
4917 else if (TYPE_PTRMEMFUNC_P (type))
4918 {
4919 expr = instantiate_type (type, expr, tf_none);
4920 if (expr == error_mark_node)
4921 return error_mark_node;
75650646 4922
b6ab6892
GB
4923 /* There is no way to disable standard conversions in
4924 resolve_address_of_overloaded_function (called by
4925 instantiate_type). It is possible that the call succeeded by
4926 converting &B::I to &D::I (where B is a base of D), so we need
4927 to reject this conversion here.
75650646 4928
b6ab6892
GB
4929 Actually, even if there was a way to disable standard conversions,
4930 it would still be better to reject them here so that we can
4931 provide a superior diagnostic. */
4932 if (!same_type_p (TREE_TYPE (expr), type))
4933 {
4934 /* Make sure we are just one standard conversion off. */
4935 gcc_assert (can_convert (type, TREE_TYPE (expr)));
4936 error ("%qE is not a valid template argument for type %qT "
4937 "because it is of type %qT", expr, type,
4938 TREE_TYPE (expr));
1f5b3869 4939 inform (input_location, "standard conversions are not allowed in this context");
b6ab6892
GB
4940 return NULL_TREE;
4941 }
4942 }
4943 /* [temp.arg.nontype]/5, bullet 7
59e7a901 4944
b6ab6892
GB
4945 For a non-type template-parameter of type pointer to data member,
4946 qualification conversions (_conv.qual_) are applied. */
4947 else if (TYPE_PTRMEM_P (type))
4948 {
4949 expr = perform_qualification_conversions (type, expr);
4950 if (expr == error_mark_node)
75650646 4951 return expr;
75650646 4952 }
b6ab6892
GB
4953 /* A template non-type parameter must be one of the above. */
4954 else
4955 gcc_unreachable ();
75650646 4956
b6ab6892
GB
4957 /* Sanity check: did we actually convert the argument to the
4958 right type? */
4959 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
4960 return expr;
75650646
MM
4961}
4962
a0a5f30f
DG
4963/* Subroutine of coerce_template_template_parms, which returns 1 if
4964 PARM_PARM and ARG_PARM match using the rule for the template
4965 parameters of template template parameters. Both PARM and ARG are
4966 template parameters; the rest of the arguments are the same as for
4967 coerce_template_template_parms.
4968 */
4969static int
4970coerce_template_template_parm (tree parm,
4971 tree arg,
4972 tsubst_flags_t complain,
4973 tree in_decl,
4974 tree outer_args)
4975{
4976 if (arg == NULL_TREE || arg == error_mark_node
4977 || parm == NULL_TREE || parm == error_mark_node)
4978 return 0;
4979
4980 if (TREE_CODE (arg) != TREE_CODE (parm))
4981 return 0;
4982
4983 switch (TREE_CODE (parm))
4984 {
4985 case TEMPLATE_DECL:
4986 /* We encounter instantiations of templates like
4987 template <template <template <class> class> class TT>
4988 class C; */
4989 {
4990 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4991 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4992
4993 if (!coerce_template_template_parms
4994 (parmparm, argparm, complain, in_decl, outer_args))
4995 return 0;
4996 }
4997 /* Fall through. */
4998
4999 case TYPE_DECL:
5000 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
5001 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5002 /* Argument is a parameter pack but parameter is not. */
5003 return 0;
5004 break;
5005
5006 case PARM_DECL:
5007 /* The tsubst call is used to handle cases such as
5008
5009 template <int> class C {};
5010 template <class T, template <T> class TT> class D {};
5011 D<int, C> d;
5012
5013 i.e. the parameter list of TT depends on earlier parameters. */
8320114a 5014 if (!uses_template_parms (TREE_TYPE (arg))
a0a5f30f
DG
5015 && !same_type_p
5016 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
5017 TREE_TYPE (arg)))
5018 return 0;
5019
5020 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
5021 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5022 /* Argument is a parameter pack but parameter is not. */
5023 return 0;
5024
5025 break;
5026
5027 default:
5028 gcc_unreachable ();
5029 }
5030
5031 return 1;
5032}
5033
b6ab6892 5034
c8094d83
MS
5035/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
5036 template template parameters. Both PARM_PARMS and ARG_PARMS are
5037 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
744fac59 5038 or PARM_DECL.
c8094d83 5039
744fac59 5040 Consider the example:
e7e93965
MM
5041 template <class T> class A;
5042 template<template <class U> class TT> class B;
744fac59 5043
e7e93965
MM
5044 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
5045 the parameters to A, and OUTER_ARGS contains A. */
744fac59
KL
5046
5047static int
c8094d83 5048coerce_template_template_parms (tree parm_parms,
0cbd7506
MS
5049 tree arg_parms,
5050 tsubst_flags_t complain,
3a978d72 5051 tree in_decl,
0cbd7506 5052 tree outer_args)
744fac59
KL
5053{
5054 int nparms, nargs, i;
5055 tree parm, arg;
a0a5f30f 5056 int variadic_p = 0;
744fac59 5057
50bc768d
NS
5058 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
5059 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
744fac59
KL
5060
5061 nparms = TREE_VEC_LENGTH (parm_parms);
5062 nargs = TREE_VEC_LENGTH (arg_parms);
5063
a0a5f30f
DG
5064 /* Determine whether we have a parameter pack at the end of the
5065 template template parameter's template parameter list. */
5066 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
5067 {
5068 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
5069
8c8d4a46
L
5070 if (parm == error_mark_node)
5071 return 0;
5072
a0a5f30f
DG
5073 switch (TREE_CODE (parm))
5074 {
5075 case TEMPLATE_DECL:
5076 case TYPE_DECL:
5077 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
5078 variadic_p = 1;
5079 break;
5080
5081 case PARM_DECL:
5082 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
5083 variadic_p = 1;
5084 break;
5085
5086 default:
5087 gcc_unreachable ();
5088 }
5089 }
5090
5091 if (nargs != nparms
5092 && !(variadic_p && nargs >= nparms - 1))
744fac59
KL
5093 return 0;
5094
a0a5f30f
DG
5095 /* Check all of the template parameters except the parameter pack at
5096 the end (if any). */
5097 for (i = 0; i < nparms - variadic_p; ++i)
744fac59 5098 {
2d8ba2c7
LM
5099 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
5100 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
42b304f1
LM
5101 continue;
5102
744fac59
KL
5103 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
5104 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5105
a0a5f30f
DG
5106 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5107 outer_args))
744fac59
KL
5108 return 0;
5109
a0a5f30f 5110 }
00bdb87f 5111
a0a5f30f
DG
5112 if (variadic_p)
5113 {
5114 /* Check each of the template parameters in the template
5115 argument against the template parameter pack at the end of
5116 the template template parameter. */
5117 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
5118 return 0;
5d80a306 5119
a0a5f30f 5120 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
c8094d83 5121
a0a5f30f
DG
5122 for (; i < nargs; ++i)
5123 {
5124 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
5125 continue;
5126
5127 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
5128
5129 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
5130 outer_args))
5131 return 0;
5132 }
744fac59 5133 }
a0a5f30f 5134
744fac59
KL
5135 return 1;
5136}
5137
4cf36211
DG
5138/* Verifies that the deduced template arguments (in TARGS) for the
5139 template template parameters (in TPARMS) represent valid bindings,
5140 by comparing the template parameter list of each template argument
5141 to the template parameter list of its corresponding template
5142 template parameter, in accordance with DR150. This
5143 routine can only be called after all template arguments have been
5144 deduced. It will return TRUE if all of the template template
5145 parameter bindings are okay, FALSE otherwise. */
5146bool
5147template_template_parm_bindings_ok_p (tree tparms, tree targs)
5148{
5149 int i, ntparms = TREE_VEC_LENGTH (tparms);
ee34d21a
JM
5150 bool ret = true;
5151
5152 /* We're dealing with template parms in this process. */
5153 ++processing_template_decl;
4cf36211
DG
5154
5155 targs = INNERMOST_TEMPLATE_ARGS (targs);
5156
5157 for (i = 0; i < ntparms; ++i)
5158 {
5159 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
5160 tree targ = TREE_VEC_ELT (targs, i);
5161
5162 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
5163 {
5164 tree packed_args = NULL_TREE;
5165 int idx, len = 1;
5166
5167 if (ARGUMENT_PACK_P (targ))
5168 {
5169 /* Look inside the argument pack. */
5170 packed_args = ARGUMENT_PACK_ARGS (targ);
5171 len = TREE_VEC_LENGTH (packed_args);
5172 }
5173
5174 for (idx = 0; idx < len; ++idx)
5175 {
5176 tree targ_parms = NULL_TREE;
5177
5178 if (packed_args)
5179 /* Extract the next argument from the argument
5180 pack. */
5181 targ = TREE_VEC_ELT (packed_args, idx);
5182
5183 if (PACK_EXPANSION_P (targ))
5184 /* Look at the pattern of the pack expansion. */
5185 targ = PACK_EXPANSION_PATTERN (targ);
5186
5187 /* Extract the template parameters from the template
5188 argument. */
5189 if (TREE_CODE (targ) == TEMPLATE_DECL)
5190 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
5191 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
5192 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
5193
5194 /* Verify that we can coerce the template template
5195 parameters from the template argument to the template
5196 parameter. This requires an exact match. */
5197 if (targ_parms
5198 && !coerce_template_template_parms
5199 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
5200 targ_parms,
5201 tf_none,
5202 tparm,
5203 targs))
ee34d21a
JM
5204 {
5205 ret = false;
5206 goto out;
5207 }
4cf36211
DG
5208 }
5209 }
5210 }
5211
ee34d21a
JM
5212 out:
5213
5214 --processing_template_decl;
5215 return ret;
4cf36211
DG
5216}
5217
8b5b8b7c
MM
5218/* Convert the indicated template ARG as necessary to match the
5219 indicated template PARM. Returns the converted ARG, or
c2ea3a40
NS
5220 error_mark_node if the conversion was unsuccessful. Error and
5221 warning messages are issued under control of COMPLAIN. This
5222 conversion is for the Ith parameter in the parameter list. ARGS is
5223 the full set of template arguments deduced so far. */
8b5b8b7c
MM
5224
5225static tree
c8094d83 5226convert_template_argument (tree parm,
0cbd7506
MS
5227 tree arg,
5228 tree args,
5229 tsubst_flags_t complain,
5230 int i,
5231 tree in_decl)
8b5b8b7c 5232{
88b82314 5233 tree orig_arg;
8b5b8b7c 5234 tree val;
8b5b8b7c 5235 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
c8094d83 5236
c8094d83 5237 if (TREE_CODE (arg) == TREE_LIST
a5ac359a 5238 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
c8094d83 5239 {
8b5b8b7c
MM
5240 /* The template argument was the name of some
5241 member function. That's usually
0e339752 5242 invalid, but static members are OK. In any
8b5b8b7c
MM
5243 case, grab the underlying fields/functions
5244 and issue an error later if required. */
88b82314 5245 orig_arg = TREE_VALUE (arg);
8b5b8b7c
MM
5246 TREE_TYPE (arg) = unknown_type_node;
5247 }
5248
88b82314
DG
5249 orig_arg = arg;
5250
8b5b8b7c
MM
5251 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
5252 requires_type = (TREE_CODE (parm) == TYPE_DECL
5253 || requires_tmpl_type);
5254
d732e98f 5255 /* When determining whether an argument pack expansion is a template,
5d80a306 5256 look at the pattern. */
88b82314
DG
5257 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
5258 arg = PACK_EXPANSION_PATTERN (arg);
5d80a306
DG
5259
5260 is_tmpl_type =
88b82314
DG
5261 ((TREE_CODE (arg) == TEMPLATE_DECL
5262 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
5263 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5264 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
c8094d83 5265
b8c6534b
KL
5266 if (is_tmpl_type
5267 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
5268 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8b5b8b7c 5269 arg = TYPE_STUB_DECL (arg);
8b5b8b7c 5270
2f939d94 5271 is_type = TYPE_P (arg) || is_tmpl_type;
8b5b8b7c
MM
5272
5273 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
5274 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
5275 {
cbe5f3b3 5276 permerror (input_location, "to refer to a type member of a template parameter, "
37ec60ed 5277 "use %<typename %E%>", orig_arg);
c8094d83 5278
88b82314
DG
5279 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
5280 TREE_OPERAND (arg, 1),
5281 typename_type,
5282 complain & tf_error);
5283 arg = orig_arg;
8b5b8b7c
MM
5284 is_type = 1;
5285 }
5286 if (is_type != requires_type)
5287 {
5288 if (in_decl)
5289 {
c2ea3a40 5290 if (complain & tf_error)
8b5b8b7c 5291 {
0f51ccfc 5292 error ("type/value mismatch at argument %d in template "
0cbd7506
MS
5293 "parameter list for %qD",
5294 i + 1, in_decl);
8b5b8b7c 5295 if (is_type)
0f51ccfc 5296 error (" expected a constant of type %qT, got %qT",
0cbd7506 5297 TREE_TYPE (parm),
1ad8aeeb 5298 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
d12a7283 5299 else if (requires_tmpl_type)
88b82314 5300 error (" expected a class template, got %qE", orig_arg);
8b5b8b7c 5301 else
88b82314 5302 error (" expected a type, got %qE", orig_arg);
8b5b8b7c
MM
5303 }
5304 }
5305 return error_mark_node;
5306 }
5307 if (is_tmpl_type ^ requires_tmpl_type)
5308 {
c2ea3a40 5309 if (in_decl && (complain & tf_error))
8b5b8b7c 5310 {
0f51ccfc 5311 error ("type/value mismatch at argument %d in template "
0cbd7506
MS
5312 "parameter list for %qD",
5313 i + 1, in_decl);
8b5b8b7c 5314 if (is_tmpl_type)
0f51ccfc 5315 error (" expected a type, got %qT", DECL_NAME (arg));
8b5b8b7c 5316 else
88b82314 5317 error (" expected a class template, got %qT", orig_arg);
8b5b8b7c
MM
5318 }
5319 return error_mark_node;
5320 }
c8094d83 5321
8b5b8b7c
MM
5322 if (is_type)
5323 {
5324 if (requires_tmpl_type)
5325 {
b8c6534b
KL
5326 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
5327 /* The number of argument required is not known yet.
5328 Just accept it for now. */
5329 val = TREE_TYPE (arg);
8b5b8b7c
MM
5330 else
5331 {
b8c6534b 5332 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
5d80a306
DG
5333 tree argparm;
5334
88b82314 5335 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
b8c6534b
KL
5336
5337 if (coerce_template_template_parms (parmparm, argparm,
5338 complain, in_decl,
6150b602 5339 args))
8b5b8b7c 5340 {
88b82314 5341 val = orig_arg;
c8094d83
MS
5342
5343 /* TEMPLATE_TEMPLATE_PARM node is preferred over
b8c6534b 5344 TEMPLATE_DECL. */
5d80a306
DG
5345 if (val != error_mark_node)
5346 {
5347 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
5348 val = TREE_TYPE (val);
5349 else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
88b82314 5350 && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
5d80a306 5351 {
88b82314 5352 val = TREE_TYPE (arg);
5d80a306
DG
5353 val = make_pack_expansion (val);
5354 }
5355 }
8b5b8b7c 5356 }
b8c6534b
KL
5357 else
5358 {
c2ea3a40 5359 if (in_decl && (complain & tf_error))
b8c6534b 5360 {
0f51ccfc 5361 error ("type/value mismatch at argument %d in "
0cbd7506
MS
5362 "template parameter list for %qD",
5363 i + 1, in_decl);
0f51ccfc 5364 error (" expected a template of type %qD, got %qD",
88b82314 5365 parm, orig_arg);
b8c6534b 5366 }
c8094d83 5367
b8c6534b
KL
5368 val = error_mark_node;
5369 }
8b5b8b7c
MM
5370 }
5371 }
5372 else
88b82314 5373 val = orig_arg;
685e39c2
MM
5374 /* We only form one instance of each template specialization.
5375 Therefore, if we use a non-canonical variant (i.e., a
3db45ab5 5376 typedef), any future messages referring to the type will use
685e39c2
MM
5377 the typedef, which is confusing if those future uses do not
5378 themselves also use the typedef. */
5379 if (TYPE_P (val))
cd41d410 5380 val = strip_typedefs (val);
8b5b8b7c
MM
5381 }
5382 else
5383 {
4393e105 5384 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
8b5b8b7c 5385
11b810f1 5386 if (invalid_nontype_parm_type_p (t, complain))
0cbd7506 5387 return error_mark_node;
c8094d83 5388
ffaf1e05
JM
5389 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
5390 {
5391 if (same_type_p (t, TREE_TYPE (orig_arg)))
5392 val = orig_arg;
5393 else
5394 {
5395 /* Not sure if this is reachable, but it doesn't hurt
5396 to be robust. */
5397 error ("type mismatch in nontype parameter pack");
5398 val = error_mark_node;
5399 }
5400 }
5401 else if (!uses_template_parms (orig_arg) && !uses_template_parms (t))
8b5b8b7c
MM
5402 /* We used to call digest_init here. However, digest_init
5403 will report errors, which we don't want when complain
5404 is zero. More importantly, digest_init will try too
5405 hard to convert things: for example, `0' should not be
5406 converted to pointer type at this point according to
5407 the standard. Accepting this is not merely an
5408 extension, since deciding whether or not these
5409 conversions can occur is part of determining which
dc957d14 5410 function template to call, or whether a given explicit
0e339752 5411 argument specification is valid. */
88b82314 5412 val = convert_nontype_argument (t, orig_arg);
8b5b8b7c 5413 else
88b82314 5414 val = orig_arg;
8b5b8b7c
MM
5415
5416 if (val == NULL_TREE)
5417 val = error_mark_node;
c2ea3a40 5418 else if (val == error_mark_node && (complain & tf_error))
88b82314 5419 error ("could not convert template argument %qE to %qT", orig_arg, t);
8b5b8b7c
MM
5420 }
5421
5422 return val;
5423}
5424
b1d7b1c0
DG
5425/* Coerces the remaining template arguments in INNER_ARGS (from
5426 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
5427 Returns the coerced argument pack. PARM_IDX is the position of this
5428 parameter in the template parameter list. ARGS is the original
5429 template argument list. */
5430static tree
5431coerce_template_parameter_pack (tree parms,
5432 int parm_idx,
5433 tree args,
5434 tree inner_args,
5435 int arg_idx,
5436 tree new_args,
5437 int* lost,
5438 tree in_decl,
5439 tsubst_flags_t complain)
5440{
5441 tree parm = TREE_VEC_ELT (parms, parm_idx);
5442 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5443 tree packed_args;
5444 tree argument_pack;
5445 tree packed_types = NULL_TREE;
5446
5447 if (arg_idx > nargs)
5448 arg_idx = nargs;
5449
5450 packed_args = make_tree_vec (nargs - arg_idx);
5451
5452 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL
5453 && uses_parameter_packs (TREE_TYPE (TREE_VALUE (parm))))
5454 {
5455 /* When the template parameter is a non-type template
5456 parameter pack whose type uses parameter packs, we need
5457 to look at each of the template arguments
5458 separately. Build a vector of the types for these
5459 non-type template parameters in PACKED_TYPES. */
5460 tree expansion
5461 = make_pack_expansion (TREE_TYPE (TREE_VALUE (parm)));
5462 packed_types = tsubst_pack_expansion (expansion, args,
5463 complain, in_decl);
5464
5465 if (packed_types == error_mark_node)
5466 return error_mark_node;
5467
5468 /* Check that we have the right number of arguments. */
5469 if (arg_idx < nargs
5470 && !PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx))
5471 && nargs - arg_idx != TREE_VEC_LENGTH (packed_types))
5472 {
5473 int needed_parms
5474 = TREE_VEC_LENGTH (parms) - 1 + TREE_VEC_LENGTH (packed_types);
5475 error ("wrong number of template arguments (%d, should be %d)",
5476 nargs, needed_parms);
5477 return error_mark_node;
5478 }
5479
5480 /* If we aren't able to check the actual arguments now
5481 (because they haven't been expanded yet), we can at least
5482 verify that all of the types used for the non-type
5483 template parameter pack are, in fact, valid for non-type
5484 template parameters. */
5485 if (arg_idx < nargs
5486 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
5487 {
5488 int j, len = TREE_VEC_LENGTH (packed_types);
5489 for (j = 0; j < len; ++j)
5490 {
5491 tree t = TREE_VEC_ELT (packed_types, j);
5492 if (invalid_nontype_parm_type_p (t, complain))
5493 return error_mark_node;
5494 }
5495 }
5496 }
5497
5498 /* Convert the remaining arguments, which will be a part of the
5499 parameter pack "parm". */
5500 for (; arg_idx < nargs; ++arg_idx)
5501 {
5502 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
5503 tree actual_parm = TREE_VALUE (parm);
5504
5505 if (packed_types && !PACK_EXPANSION_P (arg))
5506 {
5507 /* When we have a vector of types (corresponding to the
5508 non-type template parameter pack that uses parameter
5509 packs in its type, as mention above), and the
5510 argument is not an expansion (which expands to a
5511 currently unknown number of arguments), clone the
5512 parm and give it the next type in PACKED_TYPES. */
5513 actual_parm = copy_node (actual_parm);
5514 TREE_TYPE (actual_parm) =
5515 TREE_VEC_ELT (packed_types, arg_idx - parm_idx);
5516 }
5517
c343d5a7
PC
5518 if (arg != error_mark_node)
5519 arg = convert_template_argument (actual_parm,
5520 arg, new_args, complain, parm_idx,
5521 in_decl);
b1d7b1c0
DG
5522 if (arg == error_mark_node)
5523 (*lost)++;
5524 TREE_VEC_ELT (packed_args, arg_idx - parm_idx) = arg;
5525 }
5526
5527 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
5528 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
5529 argument_pack = make_node (TYPE_ARGUMENT_PACK);
5530 else
5531 {
5532 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
88b82314 5533 TREE_TYPE (argument_pack)
cdcae745 5534 = tsubst (TREE_TYPE (TREE_VALUE (parm)), new_args, complain, in_decl);
b1d7b1c0
DG
5535 TREE_CONSTANT (argument_pack) = 1;
5536 }
5537
5538 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
5539 return argument_pack;
5540}
5541
8b5b8b7c
MM
5542/* Convert all template arguments to their appropriate types, and
5543 return a vector containing the innermost resulting template
c2ea3a40 5544 arguments. If any error occurs, return error_mark_node. Error and
3e4a3562 5545 warning messages are issued under control of COMPLAIN.
75650646 5546
e7e93965 5547 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
84dc00e8 5548 for arguments not specified in ARGS. Otherwise, if
e7e93965
MM
5549 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
5550 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
5551 USE_DEFAULT_ARGS is false, then all arguments must be specified in
5552 ARGS. */
c8094d83 5553
8d08fdba 5554static tree
c8094d83 5555coerce_template_parms (tree parms,
0cbd7506
MS
5556 tree args,
5557 tree in_decl,
3a978d72 5558 tsubst_flags_t complain,
e7e93965
MM
5559 bool require_all_args,
5560 bool use_default_args)
8d08fdba 5561{
b1d7b1c0 5562 int nparms, nargs, parm_idx, arg_idx, lost = 0;
e4a84209 5563 tree inner_args;
8b5b8b7c
MM
5564 tree new_args;
5565 tree new_inner_args;
7d882b83
ILT
5566 int saved_unevaluated_operand;
5567 int saved_inhibit_evaluation_warnings;
a292b002 5568
5d80a306
DG
5569 /* When used as a boolean value, indicates whether this is a
5570 variadic template parameter list. Since it's an int, we can also
5571 subtract it from nparms to get the number of non-variadic
5572 parameters. */
b1d7b1c0 5573 int variadic_p = 0;
5d80a306 5574
a292b002
MS
5575 nparms = TREE_VEC_LENGTH (parms);
5576
b1d7b1c0
DG
5577 /* Determine if there are any parameter packs. */
5578 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
5579 {
5580 tree tparm = TREE_VALUE (TREE_VEC_ELT (parms, parm_idx));
5581 if (template_parameter_pack_p (tparm))
ffaf1e05 5582 ++variadic_p;
b1d7b1c0
DG
5583 }
5584
ffaf1e05
JM
5585 inner_args = INNERMOST_TEMPLATE_ARGS (args);
5586 /* If there are 0 or 1 parameter packs, we need to expand any argument
5587 packs so that we can deduce a parameter pack from some non-packed args
5588 followed by an argument pack, as in variadic85.C. If there are more
5589 than that, we need to leave argument packs intact so the arguments are
5590 assigned to the right parameter packs. This should only happen when
5591 dealing with a nested class inside a partial specialization of a class
5592 template, as in variadic92.C. */
5593 if (variadic_p <= 1)
5594 inner_args = expand_template_argument_pack (inner_args);
5595
5596 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
5597 if ((nargs > nparms && !variadic_p)
5d80a306 5598 || (nargs < nparms - variadic_p
e7e93965
MM
5599 && require_all_args
5600 && (!use_default_args
42b304f1
LM
5601 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
5602 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8d08fdba 5603 {
c8094d83 5604 if (complain & tf_error)
75650646 5605 {
5d80a306
DG
5606 const char *or_more = "";
5607 if (variadic_p)
5608 {
5609 or_more = " or more";
5610 --nparms;
5611 }
5612
5613 error ("wrong number of template arguments (%d, should be %d%s)",
5614 nargs, nparms, or_more);
c8094d83 5615
75650646 5616 if (in_decl)
dee15844 5617 error ("provided for %q+D", in_decl);
75650646
MM
5618 }
5619
8d08fdba
MS
5620 return error_mark_node;
5621 }
5622
a2c5ed87
MM
5623 /* We need to evaluate the template arguments, even though this
5624 template-id may be nested within a "sizeof". */
7d882b83
ILT
5625 saved_unevaluated_operand = cp_unevaluated_operand;
5626 cp_unevaluated_operand = 0;
5627 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
5628 c_inhibit_evaluation_warnings = 0;
f31c0a32 5629 new_inner_args = make_tree_vec (nparms);
8b5b8b7c 5630 new_args = add_outermost_template_args (args, new_inner_args);
b1d7b1c0 5631 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8d08fdba 5632 {
8b5b8b7c
MM
5633 tree arg;
5634 tree parm;
e4a84209 5635
8b5b8b7c 5636 /* Get the Ith template parameter. */
b1d7b1c0 5637 parm = TREE_VEC_ELT (parms, parm_idx);
42b304f1
LM
5638
5639 if (parm == error_mark_node)
2d8ba2c7 5640 {
b1d7b1c0 5641 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
42b304f1 5642 continue;
2d8ba2c7 5643 }
75650646 5644
b1d7b1c0 5645 /* Calculate the next argument. */
ffaf1e05
JM
5646 if (arg_idx < nargs)
5647 arg = TREE_VEC_ELT (inner_args, arg_idx);
5648 else
5649 arg = NULL_TREE;
5650
5651 if (template_parameter_pack_p (TREE_VALUE (parm))
5652 && !(arg && ARGUMENT_PACK_P (arg)))
5d80a306 5653 {
ffaf1e05
JM
5654 /* All remaining arguments will be placed in the
5655 template parameter pack PARM. */
5656 arg = coerce_template_parameter_pack (parms, parm_idx, args,
5657 inner_args, arg_idx,
5658 new_args, &lost,
5659 in_decl, complain);
5660
b1d7b1c0
DG
5661 /* Store this argument. */
5662 if (arg == error_mark_node)
5663 lost++;
5664 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
5665
ffaf1e05
JM
5666 /* We are done with all of the arguments. */
5667 arg_idx = nargs;
5668
b1d7b1c0
DG
5669 continue;
5670 }
ffaf1e05
JM
5671 else if (arg)
5672 {
5673 if (PACK_EXPANSION_P (arg))
5d80a306 5674 {
4cf36211
DG
5675 if (complain & tf_error)
5676 {
ffaf1e05
JM
5677 /* FIXME this restriction was removed by N2555; see
5678 bug 35722. */
4cf36211
DG
5679 /* If ARG is a pack expansion, but PARM is not a
5680 template parameter pack (if it were, we would have
5681 handled it above), we're trying to expand into a
5682 fixed-length argument list. */
5683 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
ffaf1e05 5684 sorry ("cannot expand %<%E%> into a fixed-length "
4cf36211
DG
5685 "argument list", arg);
5686 else
ffaf1e05 5687 sorry ("cannot expand %<%T%> into a fixed-length "
4cf36211
DG
5688 "argument list", arg);
5689 }
c343d5a7 5690 return error_mark_node;
5d80a306
DG
5691 }
5692 }
e7e93965 5693 else if (require_all_args)
b1d7b1c0
DG
5694 /* There must be a default arg in this case. */
5695 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
5696 complain, in_decl);
ffd49b19
NS
5697 else
5698 break;
c8094d83 5699
ffd49b19 5700 if (arg == error_mark_node)
e34b0922
KL
5701 {
5702 if (complain & tf_error)
b1d7b1c0 5703 error ("template argument %d is invalid", arg_idx + 1);
e34b0922 5704 }
b1d7b1c0
DG
5705 else if (!arg)
5706 /* This only occurs if there was an error in the template
5707 parameter list itself (which we would already have
5708 reported) that we are trying to recover from, e.g., a class
5709 template with a parameter list such as
5710 template<typename..., typename>. */
5711 return error_mark_node;
c8094d83
MS
5712 else
5713 arg = convert_template_argument (TREE_VALUE (parm),
b1d7b1c0
DG
5714 arg, new_args, complain,
5715 parm_idx, in_decl);
c8094d83 5716
8b5b8b7c 5717 if (arg == error_mark_node)
8d08fdba 5718 lost++;
b1d7b1c0 5719 TREE_VEC_ELT (new_inner_args, arg_idx) = arg;
8d08fdba 5720 }
7d882b83
ILT
5721 cp_unevaluated_operand = saved_unevaluated_operand;
5722 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8b5b8b7c 5723
8d08fdba
MS
5724 if (lost)
5725 return error_mark_node;
8b5b8b7c
MM
5726
5727 return new_inner_args;
8d08fdba
MS
5728}
5729
34016c81
JM
5730/* Returns 1 if template args OT and NT are equivalent. */
5731
d8e178a0 5732static int
3a978d72 5733template_args_equal (tree ot, tree nt)
34016c81
JM
5734{
5735 if (nt == ot)
5736 return 1;
74601d7c 5737
34016c81
JM
5738 if (TREE_CODE (nt) == TREE_VEC)
5739 /* For member templates */
74601d7c 5740 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
5d80a306
DG
5741 else if (PACK_EXPANSION_P (ot))
5742 return PACK_EXPANSION_P (nt)
5743 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
5744 PACK_EXPANSION_PATTERN (nt));
ffaf1e05
JM
5745 else if (ARGUMENT_PACK_P (ot))
5746 {
5747 int i, len;
5748 tree opack, npack;
5749
5750 if (!ARGUMENT_PACK_P (nt))
5751 return 0;
5752
5753 opack = ARGUMENT_PACK_ARGS (ot);
5754 npack = ARGUMENT_PACK_ARGS (nt);
5755 len = TREE_VEC_LENGTH (opack);
5756 if (TREE_VEC_LENGTH (npack) != len)
5757 return 0;
5758 for (i = 0; i < len; ++i)
5759 if (!template_args_equal (TREE_VEC_ELT (opack, i),
5760 TREE_VEC_ELT (npack, i)))
5761 return 0;
5762 return 1;
5763 }
74601d7c 5764 else if (TYPE_P (nt))
660845bf 5765 return TYPE_P (ot) && same_type_p (ot, nt);
74601d7c
KL
5766 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
5767 return 0;
34016c81 5768 else
c8a209ca 5769 return cp_tree_equal (ot, nt);
34016c81
JM
5770}
5771
5772/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
f84b4be9
JM
5773 of template arguments. Returns 0 otherwise. */
5774
6757edfe 5775int
3a978d72 5776comp_template_args (tree oldargs, tree newargs)
5566b478
MS
5777{
5778 int i;
5779
386b8a85
JM
5780 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
5781 return 0;
5782
5566b478
MS
5783 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
5784 {
5785 tree nt = TREE_VEC_ELT (newargs, i);
5786 tree ot = TREE_VEC_ELT (oldargs, i);
5787
34016c81 5788 if (! template_args_equal (ot, nt))
61a127b3 5789 return 0;
5566b478
MS
5790 }
5791 return 1;
5792}
5793
5566b478 5794static void
3a978d72 5795add_pending_template (tree d)
5566b478 5796{
3ae18eaf
JM
5797 tree ti = (TYPE_P (d)
5798 ? CLASSTYPE_TEMPLATE_INFO (d)
5799 : DECL_TEMPLATE_INFO (d));
e2c3721c 5800 struct pending_template *pt;
3ae18eaf 5801 int level;
e92cc029 5802
824b9a4c 5803 if (TI_PENDING_TEMPLATE_FLAG (ti))
5566b478
MS
5804 return;
5805
3ae18eaf
JM
5806 /* We are called both from instantiate_decl, where we've already had a
5807 tinst_level pushed, and instantiate_template, where we haven't.
5808 Compensate. */
e2c3721c 5809 level = !current_tinst_level || current_tinst_level->decl != d;
3ae18eaf
JM
5810
5811 if (level)
5812 push_tinst_level (d);
5813
e2c3721c
PB
5814 pt = GGC_NEW (struct pending_template);
5815 pt->next = NULL;
5816 pt->tinst = current_tinst_level;
46ccf50a 5817 if (last_pending_template)
e2c3721c 5818 last_pending_template->next = pt;
46ccf50a
JM
5819 else
5820 pending_templates = pt;
5821
5822 last_pending_template = pt;
5823
824b9a4c 5824 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3ae18eaf
JM
5825
5826 if (level)
5827 pop_tinst_level ();
5566b478
MS
5828}
5829
386b8a85 5830
4ba126e4
MM
5831/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
5832 ARGLIST. Valid choices for FNS are given in the cp-tree.def
5833 documentation for TEMPLATE_ID_EXPR. */
386b8a85
JM
5834
5835tree
3a978d72 5836lookup_template_function (tree fns, tree arglist)
386b8a85 5837{
2c73f9f5 5838 tree type;
050367a3 5839
4ba126e4
MM
5840 if (fns == error_mark_node || arglist == error_mark_node)
5841 return error_mark_node;
5842
50bc768d 5843 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4230cec2
NS
5844 gcc_assert (fns && (is_overloaded_fn (fns)
5845 || TREE_CODE (fns) == IDENTIFIER_NODE));
4ba126e4 5846
50ad9642
MM
5847 if (BASELINK_P (fns))
5848 {
f293ce4b
RS
5849 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
5850 unknown_type_node,
5851 BASELINK_FUNCTIONS (fns),
5852 arglist);
50ad9642
MM
5853 return fns;
5854 }
5855
2c73f9f5
ML
5856 type = TREE_TYPE (fns);
5857 if (TREE_CODE (fns) == OVERLOAD || !type)
5858 type = unknown_type_node;
c8094d83 5859
f293ce4b 5860 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
386b8a85
JM
5861}
5862
a2b60a0e
MM
5863/* Within the scope of a template class S<T>, the name S gets bound
5864 (in build_self_reference) to a TYPE_DECL for the class, not a
5865 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
5866 or one of its enclosing classes, and that type is a template,
5867 return the associated TEMPLATE_DECL. Otherwise, the original
5868 DECL is returned. */
5869
a723baf1 5870tree
3a978d72 5871maybe_get_template_decl_from_type_decl (tree decl)
a2b60a0e
MM
5872{
5873 return (decl != NULL_TREE
c8094d83 5874 && TREE_CODE (decl) == TYPE_DECL
a2b60a0e 5875 && DECL_ARTIFICIAL (decl)
511b60ff 5876 && CLASS_TYPE_P (TREE_TYPE (decl))
c8094d83 5877 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
a2b60a0e
MM
5878 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
5879}
386b8a85 5880
8d08fdba
MS
5881/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
5882 parameters, find the desired type.
5883
5884 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8d08fdba
MS
5885
5886 IN_DECL, if non-NULL, is the template declaration we are trying to
c8094d83 5887 instantiate.
75650646 5888
838dfd8a 5889 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
36a117a5 5890 the class we are looking up.
c8094d83 5891
c2ea3a40 5892 Issue error and warning messages under control of COMPLAIN.
36a117a5 5893
75650646
MM
5894 If the template class is really a local class in a template
5895 function, then the FUNCTION_CONTEXT is the function in which it is
c8094d83 5896 being instantiated.
b6ab6892
GB
5897
5898 ??? Note that this function is currently called *twice* for each
5899 template-id: the first time from the parser, while creating the
5900 incomplete type (finish_template_type), and the second type during the
5901 real instantiation (instantiate_template_class). This is surely something
5902 that we want to avoid. It also causes some problems with argument
5903 coercion (see convert_nontype_argument for more information on this). */
e92cc029 5904
8d08fdba 5905tree
c8094d83 5906lookup_template_class (tree d1,
0cbd7506
MS
5907 tree arglist,
5908 tree in_decl,
5909 tree context,
5910 int entering_scope,
5911 tsubst_flags_t complain)
8d08fdba 5912{
be93747e 5913 tree templ = NULL_TREE, parmlist;
dbfe2124 5914 tree t;
2b110bfc
JM
5915 spec_entry **slot;
5916 spec_entry *entry;
5917 spec_entry elt;
5918 hashval_t hash;
c8094d83 5919
fd295cb2 5920 timevar_push (TV_NAME_LOOKUP);
c8094d83 5921
5566b478
MS
5922 if (TREE_CODE (d1) == IDENTIFIER_NODE)
5923 {
90ea9897
MM
5924 tree value = innermost_non_namespace_value (d1);
5925 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
be93747e 5926 templ = value;
73b0fce8
KL
5927 else
5928 {
2c73f9f5
ML
5929 if (context)
5930 push_decl_namespace (context);
be93747e
KG
5931 templ = lookup_name (d1);
5932 templ = maybe_get_template_decl_from_type_decl (templ);
2c73f9f5
ML
5933 if (context)
5934 pop_decl_namespace ();
73b0fce8 5935 }
be93747e
KG
5936 if (templ)
5937 context = DECL_CONTEXT (templ);
5566b478 5938 }
9e1e64ec 5939 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
c91a56d2 5940 {
802dbc34
JM
5941 tree type = TREE_TYPE (d1);
5942
5943 /* If we are declaring a constructor, say A<T>::A<T>, we will get
5944 an implicit typename for the second A. Deal with it. */
5945 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5946 type = TREE_TYPE (type);
c8094d83 5947
802dbc34 5948 if (CLASSTYPE_TEMPLATE_INFO (type))
f3400fe2 5949 {
be93747e
KG
5950 templ = CLASSTYPE_TI_TEMPLATE (type);
5951 d1 = DECL_NAME (templ);
f3400fe2 5952 }
c91a56d2 5953 }
c8094d83 5954 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9e1e64ec 5955 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
5566b478 5956 {
be93747e
KG
5957 templ = TYPE_TI_TEMPLATE (d1);
5958 d1 = DECL_NAME (templ);
5566b478 5959 }
93cdc044 5960 else if (TREE_CODE (d1) == TEMPLATE_DECL
b55276c8 5961 && DECL_TEMPLATE_RESULT (d1)
17aec3eb 5962 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
93cdc044 5963 {
be93747e
KG
5964 templ = d1;
5965 d1 = DECL_NAME (templ);
5966 context = DECL_CONTEXT (templ);
93cdc044 5967 }
8d08fdba 5968
90ea9897 5969 /* Issue an error message if we didn't find a template. */
be93747e 5970 if (! templ)
f3400fe2 5971 {
c2ea3a40 5972 if (complain & tf_error)
0cbd7506 5973 error ("%qT is not a template", d1);
fd295cb2 5974 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
f3400fe2 5975 }
2c73f9f5 5976
be93747e 5977 if (TREE_CODE (templ) != TEMPLATE_DECL
0cbd7506 5978 /* Make sure it's a user visible template, if it was named by
42eaed49 5979 the user. */
be93747e
KG
5980 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
5981 && !PRIMARY_TEMPLATE_P (templ)))
8d08fdba 5982 {
c2ea3a40 5983 if (complain & tf_error)
0cbd7506
MS
5984 {
5985 error ("non-template type %qT used as a template", d1);
5986 if (in_decl)
dee15844 5987 error ("for template declaration %q+D", in_decl);
f9c244b8 5988 }
fd295cb2 5989 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
8d08fdba 5990 }
8d08fdba 5991
42eaed49 5992 complain &= ~tf_user;
c8094d83 5993
be93747e 5994 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
73b0fce8
KL
5995 {
5996 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
0cbd7506 5997 template arguments */
73b0fce8 5998
1899c3a4 5999 tree parm;
73b0fce8 6000 tree arglist2;
ef3b7b17 6001 tree outer;
73b0fce8 6002
be93747e 6003 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
73b0fce8 6004
4f96ff63
KL
6005 /* Consider an example where a template template parameter declared as
6006
6007 template <class T, class U = std::allocator<T> > class TT
6008
c8094d83
MS
6009 The template parameter level of T and U are one level larger than
6010 of TT. To proper process the default argument of U, say when an
4f96ff63 6011 instantiation `TT<int>' is seen, we need to build the full
342cea95
KL
6012 arguments containing {int} as the innermost level. Outer levels,
6013 available when not appearing as default template argument, can be
ef3b7b17 6014 obtained from the arguments of the enclosing template.
4f96ff63 6015
342cea95
KL
6016 Suppose that TT is later substituted with std::vector. The above
6017 instantiation is `TT<int, std::allocator<T> >' with TT at
6018 level 1, and T at level 2, while the template arguments at level 1
6019 becomes {std::vector} and the inner level 2 is {int}. */
6020
be93747e 6021 outer = DECL_CONTEXT (templ);
ef3b7b17
JM
6022 if (outer)
6023 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
6024 else if (current_template_parms)
6025 /* This is an argument of the current template, so we haven't set
6026 DECL_CONTEXT yet. */
6027 outer = current_template_args ();
6028
6029 if (outer)
6030 arglist = add_to_template_args (outer, arglist);
4f96ff63 6031
be93747e 6032 arglist2 = coerce_template_parms (parmlist, arglist, templ,
3db45ab5 6033 complain,
e7e93965
MM
6034 /*require_all_args=*/true,
6035 /*use_default_args=*/true);
3e4a3562 6036 if (arglist2 == error_mark_node
544aef8c 6037 || (!uses_template_parms (arglist2)
be93747e 6038 && check_instantiated_args (templ, arglist2, complain)))
0cbd7506 6039 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
73b0fce8 6040
be93747e 6041 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
fd295cb2 6042 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
73b0fce8 6043 }
c8094d83 6044 else
8d08fdba 6045 {
be93747e 6046 tree template_type = TREE_TYPE (templ);
7ac7b28f 6047 tree gen_tmpl;
36a117a5
MM
6048 tree type_decl;
6049 tree found = NULL_TREE;
6050 int arg_depth;
6051 int parm_depth;
dbfe2124 6052 int is_partial_instantiation;
830bfa74 6053
be93747e 6054 gen_tmpl = most_general_template (templ);
7ac7b28f 6055 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
36a117a5
MM
6056 parm_depth = TMPL_PARMS_DEPTH (parmlist);
6057 arg_depth = TMPL_ARGS_DEPTH (arglist);
6058
6059 if (arg_depth == 1 && parm_depth > 1)
6060 {
39c01e4c 6061 /* We've been given an incomplete set of template arguments.
36a117a5
MM
6062 For example, given:
6063
6064 template <class T> struct S1 {
0cbd7506 6065 template <class U> struct S2 {};
36a117a5 6066 template <class U> struct S2<U*> {};
0cbd7506 6067 };
c8094d83 6068
36a117a5
MM
6069 we will be called with an ARGLIST of `U*', but the
6070 TEMPLATE will be `template <class T> template
6071 <class U> struct S1<T>::S2'. We must fill in the missing
6072 arguments. */
c8094d83 6073 arglist
be93747e 6074 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (templ)),
7ac7b28f 6075 arglist);
36a117a5
MM
6076 arg_depth = TMPL_ARGS_DEPTH (arglist);
6077 }
5566b478 6078
41f5d4b1 6079 /* Now we should have enough arguments. */
50bc768d 6080 gcc_assert (parm_depth == arg_depth);
c8094d83 6081
7ac7b28f
MM
6082 /* From here on, we're only interested in the most general
6083 template. */
7ac7b28f 6084
36a117a5
MM
6085 /* Calculate the BOUND_ARGS. These will be the args that are
6086 actually tsubst'd into the definition to create the
6087 instantiation. */
6088 if (parm_depth > 1)
830bfa74
MM
6089 {
6090 /* We have multiple levels of arguments to coerce, at once. */
830bfa74 6091 int i;
e4a84209 6092 int saved_depth = TMPL_ARGS_DEPTH (arglist);
36a117a5 6093
f31c0a32 6094 tree bound_args = make_tree_vec (parm_depth);
c8094d83 6095
e4a84209 6096 for (i = saved_depth,
2b110bfc 6097 t = DECL_TEMPLATE_PARMS (gen_tmpl);
e4a84209 6098 i > 0 && t != NULL_TREE;
830bfa74 6099 --i, t = TREE_CHAIN (t))
e4a84209 6100 {
ee3071ef 6101 tree a = coerce_template_parms (TREE_VALUE (t),
2b110bfc 6102 arglist, gen_tmpl,
3db45ab5 6103 complain,
e7e93965
MM
6104 /*require_all_args=*/true,
6105 /*use_default_args=*/true);
88e98cfe
KL
6106
6107 /* Don't process further if one of the levels fails. */
6108 if (a == error_mark_node)
6109 {
6110 /* Restore the ARGLIST to its full size. */
6111 TREE_VEC_LENGTH (arglist) = saved_depth;
6112 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6113 }
c8094d83 6114
e4a84209
MM
6115 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
6116
6117 /* We temporarily reduce the length of the ARGLIST so
6118 that coerce_template_parms will see only the arguments
6119 corresponding to the template parameters it is
6120 examining. */
6121 TREE_VEC_LENGTH (arglist)--;
6122 }
6123
6124 /* Restore the ARGLIST to its full size. */
6125 TREE_VEC_LENGTH (arglist) = saved_depth;
6126
36a117a5 6127 arglist = bound_args;
830bfa74
MM
6128 }
6129 else
36a117a5
MM
6130 arglist
6131 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
f9a7ae04 6132 INNERMOST_TEMPLATE_ARGS (arglist),
2b110bfc 6133 gen_tmpl,
3db45ab5 6134 complain,
e7e93965
MM
6135 /*require_all_args=*/true,
6136 /*use_default_args=*/true);
36a117a5 6137
c353b8e3 6138 if (arglist == error_mark_node)
36a117a5 6139 /* We were unable to bind the arguments. */
fd295cb2 6140 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5566b478 6141
36a117a5
MM
6142 /* In the scope of a template class, explicit references to the
6143 template class refer to the type of the template, not any
6144 instantiation of it. For example, in:
c8094d83 6145
36a117a5
MM
6146 template <class T> class C { void f(C<T>); }
6147
6148 the `C<T>' is just the same as `C'. Outside of the
6149 class, however, such a reference is an instantiation. */
fdfacfa1 6150 if ((entering_scope
2b110bfc 6151 || !PRIMARY_TEMPLATE_P (gen_tmpl)
fdfacfa1
JM
6152 || currently_open_class (template_type))
6153 /* comp_template_args is expensive, check it last. */
6154 && comp_template_args (TYPE_TI_ARGS (template_type),
6155 arglist))
6156 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, template_type);
414ea4aa 6157
c7222c02 6158 /* If we already have this specialization, return it. */
2b110bfc
JM
6159 elt.tmpl = gen_tmpl;
6160 elt.args = arglist;
6161 hash = hash_specialization (&elt);
6162 entry = (spec_entry *) htab_find_with_hash (type_specializations,
6163 &elt, hash);
6164
6165 if (entry)
6166 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
5566b478 6167
dbfe2124 6168 /* This type is a "partial instantiation" if any of the template
ab097535 6169 arguments still involve template parameters. Note that we set
486e4077
MM
6170 IS_PARTIAL_INSTANTIATION for partial specializations as
6171 well. */
dbfe2124
MM
6172 is_partial_instantiation = uses_template_parms (arglist);
6173
c353b8e3
MM
6174 /* If the deduced arguments are invalid, then the binding
6175 failed. */
6176 if (!is_partial_instantiation
2b110bfc 6177 && check_instantiated_args (gen_tmpl,
c353b8e3
MM
6178 INNERMOST_TEMPLATE_ARGS (arglist),
6179 complain))
6180 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
c8094d83
MS
6181
6182 if (!is_partial_instantiation
2b110bfc
JM
6183 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6184 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
3ebc5c52 6185 {
2b110bfc
JM
6186 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
6187 DECL_NAME (gen_tmpl),
29ef83de 6188 /*tag_scope=*/ts_global);
fd295cb2 6189 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
3ebc5c52 6190 }
c8094d83 6191
2b110bfc 6192 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
c2ea3a40 6193 complain, in_decl);
95ee998c
MM
6194 if (!context)
6195 context = global_namespace;
6196
36a117a5 6197 /* Create the type. */
ed44da02
MM
6198 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
6199 {
dbfe2124 6200 if (!is_partial_instantiation)
92777ce4
NS
6201 {
6202 set_current_access_from_decl (TYPE_NAME (template_type));
adf2edec
DG
6203 t = start_enum (TYPE_IDENTIFIER (template_type),
6204 tsubst (ENUM_UNDERLYING_TYPE (template_type),
6205 arglist, complain, in_decl),
6206 SCOPED_ENUM_P (template_type));
92777ce4 6207 }
ed44da02 6208 else
adf2edec
DG
6209 {
6210 /* We don't want to call start_enum for this type, since
6211 the values for the enumeration constants may involve
6212 template parameters. And, no one should be interested
6213 in the enumeration constants for such a type. */
6214 t = make_node (ENUMERAL_TYPE);
6215 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
6216 }
ed44da02
MM
6217 }
6218 else
6219 {
9e1e64ec 6220 t = make_class_type (TREE_CODE (template_type));
c8094d83 6221 CLASSTYPE_DECLARED_CLASS (t)
ed44da02 6222 = CLASSTYPE_DECLARED_CLASS (template_type);
ed44da02 6223 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
f668f160 6224 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
ae673f14
JM
6225
6226 /* A local class. Make sure the decl gets registered properly. */
6227 if (context == current_function_decl)
2b110bfc 6228 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
06d40de8
DG
6229
6230 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
6231 /* This instantiation is another name for the primary
6232 template type. Set the TYPE_CANONICAL field
6233 appropriately. */
6234 TYPE_CANONICAL (t) = template_type;
6235 else if (any_template_arguments_need_structural_equality_p (arglist))
6236 /* Some of the template arguments require structural
6237 equality testing, so this template class requires
6238 structural equality testing. */
6239 SET_TYPE_STRUCTURAL_EQUALITY (t);
ed44da02
MM
6240 }
6241
ae673f14
JM
6242 /* If we called start_enum or pushtag above, this information
6243 will already be set up. */
ed44da02
MM
6244 if (!TYPE_NAME (t))
6245 {
6246 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
c8094d83 6247
2b110bfc 6248 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
ed44da02 6249 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9188c363 6250 TYPE_STUB_DECL (t) = type_decl;
c8094d83 6251 DECL_SOURCE_LOCATION (type_decl)
f31686a3 6252 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
ed44da02
MM
6253 }
6254 else
6255 type_decl = TYPE_NAME (t);
36a117a5 6256
cab7a9a3
KL
6257 TREE_PRIVATE (type_decl)
6258 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
6259 TREE_PROTECTED (type_decl)
6260 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
8d039470
MS
6261 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
6262 {
6263 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
6264 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
6265 }
cab7a9a3 6266
9fbf56f7
MM
6267 /* Set up the template information. We have to figure out which
6268 template is the immediate parent if this is a full
6269 instantiation. */
6270 if (parm_depth == 1 || is_partial_instantiation
2b110bfc 6271 || !PRIMARY_TEMPLATE_P (gen_tmpl))
9fbf56f7 6272 /* This case is easy; there are no member templates involved. */
2b110bfc 6273 found = gen_tmpl;
9fbf56f7
MM
6274 else
6275 {
2b110bfc
JM
6276 /* This is a full instantiation of a member template. Find
6277 the partial instantiation of which this is an instance. */
6278
6279 /* Temporarily reduce by one the number of levels in the ARGLIST
6280 so as to avoid comparing the last set of arguments. */
6281 TREE_VEC_LENGTH (arglist)--;
6282 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
6283 TREE_VEC_LENGTH (arglist)++;
6284 found = CLASSTYPE_TI_TEMPLATE (found);
9fbf56f7
MM
6285 }
6286
c8094d83 6287 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
2b110bfc
JM
6288
6289 elt.spec = t;
6290 slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
6291 &elt, hash, INSERT);
6292 *slot = GGC_NEW (spec_entry);
6293 **slot = elt;
6294
6295 /* Note this use of the partial instantiation so we can check it
6296 later in maybe_process_partial_specialization. */
be93747e 6297 DECL_TEMPLATE_INSTANTIATIONS (templ)
c8094d83 6298 = tree_cons (arglist, t,
be93747e 6299 DECL_TEMPLATE_INSTANTIATIONS (templ));
dbfe2124 6300
c8094d83 6301 if (TREE_CODE (t) == ENUMERAL_TYPE
dbfe2124 6302 && !is_partial_instantiation)
61a127b3
MM
6303 /* Now that the type has been registered on the instantiations
6304 list, we set up the enumerators. Because the enumeration
6305 constants may involve the enumeration type itself, we make
6306 sure to register the type first, and then create the
6307 constants. That way, doing tsubst_expr for the enumeration
6308 constants won't result in recursive calls here; we'll find
6309 the instantiation and exit above. */
6310 tsubst_enum (template_type, t, arglist);
dbfe2124 6311
7813d14c 6312 if (is_partial_instantiation)
077e7015
MM
6313 /* If the type makes use of template parameters, the
6314 code that generates debugging information will crash. */
6315 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
8d08fdba 6316
b9e75696
JM
6317 /* Possibly limit visibility based on template args. */
6318 TREE_PUBLIC (type_decl) = 1;
6319 determine_visibility (type_decl);
6320
fd295cb2 6321 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
36a117a5 6322 }
fd295cb2 6323 timevar_pop (TV_NAME_LOOKUP);
8d08fdba
MS
6324}
6325\f
c8094d83 6326struct pair_fn_data
8d08fdba 6327{
8dfaeb63
MM
6328 tree_fn_t fn;
6329 void *data;
c095a4f8
DG
6330 /* True when we should also visit template parameters that occur in
6331 non-deduced contexts. */
6332 bool include_nondeduced_p;
0c58f841 6333 struct pointer_set_t *visited;
8dfaeb63
MM
6334};
6335
6336/* Called from for_each_template_parm via walk_tree. */
581d38d0 6337
8dfaeb63 6338static tree
350fae66 6339for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8dfaeb63
MM
6340{
6341 tree t = *tp;
6342 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
6343 tree_fn_t fn = pfd->fn;
6344 void *data = pfd->data;
4f2c9d7e 6345
2f939d94 6346 if (TYPE_P (t)
c095a4f8
DG
6347 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE)
6348 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited,
6349 pfd->include_nondeduced_p))
8dfaeb63 6350 return error_mark_node;
581d38d0 6351
8d08fdba
MS
6352 switch (TREE_CODE (t))
6353 {
8d08fdba 6354 case RECORD_TYPE:
9076e292 6355 if (TYPE_PTRMEMFUNC_P (t))
8dfaeb63 6356 break;
ed44da02
MM
6357 /* Fall through. */
6358
8d08fdba 6359 case UNION_TYPE:
ed44da02 6360 case ENUMERAL_TYPE:
8dfaeb63
MM
6361 if (!TYPE_TEMPLATE_INFO (t))
6362 *walk_subtrees = 0;
6363 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
c095a4f8
DG
6364 fn, data, pfd->visited,
6365 pfd->include_nondeduced_p))
8dfaeb63
MM
6366 return error_mark_node;
6367 break;
6368
8f6e6bf3
EB
6369 case INTEGER_TYPE:
6370 if (for_each_template_parm (TYPE_MIN_VALUE (t),
c095a4f8
DG
6371 fn, data, pfd->visited,
6372 pfd->include_nondeduced_p)
8f6e6bf3 6373 || for_each_template_parm (TYPE_MAX_VALUE (t),
c095a4f8
DG
6374 fn, data, pfd->visited,
6375 pfd->include_nondeduced_p))
8f6e6bf3
EB
6376 return error_mark_node;
6377 break;
6378
588c2d1c 6379 case METHOD_TYPE:
8dfaeb63
MM
6380 /* Since we're not going to walk subtrees, we have to do this
6381 explicitly here. */
4f2c9d7e 6382 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
c095a4f8 6383 pfd->visited, pfd->include_nondeduced_p))
8dfaeb63 6384 return error_mark_node;
4890c2f4 6385 /* Fall through. */
588c2d1c
MM
6386
6387 case FUNCTION_TYPE:
8dfaeb63 6388 /* Check the return type. */
c095a4f8
DG
6389 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6390 pfd->include_nondeduced_p))
8dfaeb63
MM
6391 return error_mark_node;
6392
588c2d1c
MM
6393 /* Check the parameter types. Since default arguments are not
6394 instantiated until they are needed, the TYPE_ARG_TYPES may
6395 contain expressions that involve template parameters. But,
6396 no-one should be looking at them yet. And, once they're
6397 instantiated, they don't contain template parameters, so
6398 there's no point in looking at them then, either. */
6399 {
6400 tree parm;
6401
6402 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4f2c9d7e 6403 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
c095a4f8 6404 pfd->visited, pfd->include_nondeduced_p))
8dfaeb63 6405 return error_mark_node;
5566b478 6406
8dfaeb63
MM
6407 /* Since we've already handled the TYPE_ARG_TYPES, we don't
6408 want walk_tree walking into them itself. */
6409 *walk_subtrees = 0;
6410 }
6411 break;
3ac3d9ea 6412
a723baf1 6413 case TYPEOF_TYPE:
c095a4f8
DG
6414 if (pfd->include_nondeduced_p
6415 && for_each_template_parm (TYPE_FIELDS (t), fn, data,
6416 pfd->visited,
6417 pfd->include_nondeduced_p))
a723baf1
MM
6418 return error_mark_node;
6419 break;
6420
8d08fdba 6421 case FUNCTION_DECL:
5566b478 6422 case VAR_DECL:
5566b478 6423 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4f2c9d7e 6424 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
c095a4f8 6425 pfd->visited, pfd->include_nondeduced_p))
8dfaeb63
MM
6426 return error_mark_node;
6427 /* Fall through. */
6428
8d08fdba 6429 case PARM_DECL:
a723baf1
MM
6430 case CONST_DECL:
6431 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
6432 && for_each_template_parm (DECL_INITIAL (t), fn, data,
c095a4f8 6433 pfd->visited, pfd->include_nondeduced_p))
a723baf1 6434 return error_mark_node;
c8094d83 6435 if (DECL_CONTEXT (t)
c095a4f8 6436 && pfd->include_nondeduced_p
4f2c9d7e 6437 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
c095a4f8 6438 pfd->visited, pfd->include_nondeduced_p))
8dfaeb63
MM
6439 return error_mark_node;
6440 break;
8d08fdba 6441
a1281f45 6442 case BOUND_TEMPLATE_TEMPLATE_PARM:
744fac59 6443 /* Record template parameters such as `T' inside `TT<T>'. */
c095a4f8
DG
6444 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited,
6445 pfd->include_nondeduced_p))
8dfaeb63
MM
6446 return error_mark_node;
6447 /* Fall through. */
6448
a1281f45 6449 case TEMPLATE_TEMPLATE_PARM:
744fac59 6450 case TEMPLATE_TYPE_PARM:
f84b4be9 6451 case TEMPLATE_PARM_INDEX:
8dfaeb63
MM
6452 if (fn && (*fn)(t, data))
6453 return error_mark_node;
6454 else if (!fn)
6455 return error_mark_node;
6456 break;
5156628f 6457
8dfaeb63 6458 case TEMPLATE_DECL:
f4f206f4 6459 /* A template template parameter is encountered. */
8dfaeb63 6460 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
c095a4f8
DG
6461 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited,
6462 pfd->include_nondeduced_p))
8dfaeb63 6463 return error_mark_node;
db5ae43f 6464
8dfaeb63
MM
6465 /* Already substituted template template parameter */
6466 *walk_subtrees = 0;
6467 break;
b8865407 6468
4699c561 6469 case TYPENAME_TYPE:
c8094d83 6470 if (!fn
4f2c9d7e 6471 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
c095a4f8
DG
6472 data, pfd->visited,
6473 pfd->include_nondeduced_p))
8dfaeb63
MM
6474 return error_mark_node;
6475 break;
4699c561 6476
8dfaeb63
MM
6477 case CONSTRUCTOR:
6478 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
c095a4f8 6479 && pfd->include_nondeduced_p
8dfaeb63 6480 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4f2c9d7e 6481 (TREE_TYPE (t)), fn, data,
c095a4f8 6482 pfd->visited, pfd->include_nondeduced_p))
8dfaeb63
MM
6483 return error_mark_node;
6484 break;
c8094d83 6485
b8865407
MM
6486 case INDIRECT_REF:
6487 case COMPONENT_REF:
4699c561 6488 /* If there's no type, then this thing must be some expression
b8865407 6489 involving template parameters. */
4699c561 6490 if (!fn && !TREE_TYPE (t))
8dfaeb63
MM
6491 return error_mark_node;
6492 break;
b8865407 6493
42976354
BK
6494 case MODOP_EXPR:
6495 case CAST_EXPR:
6496 case REINTERPRET_CAST_EXPR:
6497 case CONST_CAST_EXPR:
6498 case STATIC_CAST_EXPR:
6499 case DYNAMIC_CAST_EXPR:
42976354
BK
6500 case ARROW_EXPR:
6501 case DOTSTAR_EXPR:
6502 case TYPEID_EXPR:
40242ccf 6503 case PSEUDO_DTOR_EXPR:
b8865407 6504 if (!fn)
8dfaeb63
MM
6505 return error_mark_node;
6506 break;
abff8e06 6507
8d08fdba 6508 default:
8dfaeb63 6509 break;
8d08fdba 6510 }
8dfaeb63
MM
6511
6512 /* We didn't find any template parameters we liked. */
6513 return NULL_TREE;
6514}
6515
c8094d83
MS
6516/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
6517 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
a1281f45 6518 call FN with the parameter and the DATA.
838dfd8a 6519 If FN returns nonzero, the iteration is terminated, and
8dfaeb63 6520 for_each_template_parm returns 1. Otherwise, the iteration
838dfd8a 6521 continues. If FN never returns a nonzero value, the value
8dfaeb63 6522 returned by for_each_template_parm is 0. If FN is NULL, it is
c095a4f8
DG
6523 considered to be the function which always returns 1.
6524
6525 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
6526 parameters that occur in non-deduced contexts. When false, only
6527 visits those template parameters that can be deduced. */
8dfaeb63
MM
6528
6529static int
0c58f841 6530for_each_template_parm (tree t, tree_fn_t fn, void* data,
c095a4f8
DG
6531 struct pointer_set_t *visited,
6532 bool include_nondeduced_p)
8dfaeb63
MM
6533{
6534 struct pair_fn_data pfd;
ad2ae3b2 6535 int result;
8dfaeb63
MM
6536
6537 /* Set up. */
6538 pfd.fn = fn;
6539 pfd.data = data;
c095a4f8 6540 pfd.include_nondeduced_p = include_nondeduced_p;
8dfaeb63 6541
4890c2f4
MM
6542 /* Walk the tree. (Conceptually, we would like to walk without
6543 duplicates, but for_each_template_parm_r recursively calls
6544 for_each_template_parm, so we would need to reorganize a fair
4f2c9d7e
MM
6545 bit to use walk_tree_without_duplicates, so we keep our own
6546 visited list.) */
6547 if (visited)
6548 pfd.visited = visited;
6549 else
0c58f841 6550 pfd.visited = pointer_set_create ();
14588106
RG
6551 result = cp_walk_tree (&t,
6552 for_each_template_parm_r,
6553 &pfd,
6554 pfd.visited) != NULL_TREE;
ad2ae3b2
MM
6555
6556 /* Clean up. */
6557 if (!visited)
0c58f841
MA
6558 {
6559 pointer_set_destroy (pfd.visited);
6560 pfd.visited = 0;
6561 }
ad2ae3b2
MM
6562
6563 return result;
8d08fdba
MS
6564}
6565
d43f603d
KL
6566/* Returns true if T depends on any template parameter. */
6567
050367a3 6568int
3a978d72 6569uses_template_parms (tree t)
050367a3 6570{
c353b8e3
MM
6571 bool dependent_p;
6572 int saved_processing_template_decl;
6573
6574 saved_processing_template_decl = processing_template_decl;
6575 if (!saved_processing_template_decl)
6576 processing_template_decl = 1;
6577 if (TYPE_P (t))
6578 dependent_p = dependent_type_p (t);
6579 else if (TREE_CODE (t) == TREE_VEC)
6580 dependent_p = any_dependent_template_arguments_p (t);
6581 else if (TREE_CODE (t) == TREE_LIST)
6582 dependent_p = (uses_template_parms (TREE_VALUE (t))
6583 || uses_template_parms (TREE_CHAIN (t)));
3ce5fa4f
NS
6584 else if (TREE_CODE (t) == TYPE_DECL)
6585 dependent_p = dependent_type_p (TREE_TYPE (t));
c8094d83
MS
6586 else if (DECL_P (t)
6587 || EXPR_P (t)
c353b8e3
MM
6588 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
6589 || TREE_CODE (t) == OVERLOAD
6590 || TREE_CODE (t) == BASELINK
b207d6e2 6591 || TREE_CODE (t) == IDENTIFIER_NODE
cb68ec50 6592 || TREE_CODE (t) == TRAIT_EXPR
c1b93f2b 6593 || TREE_CODE (t) == CONSTRUCTOR
6615c446 6594 || CONSTANT_CLASS_P (t))
c353b8e3
MM
6595 dependent_p = (type_dependent_expression_p (t)
6596 || value_dependent_expression_p (t));
315fb5db
NS
6597 else
6598 {
6599 gcc_assert (t == error_mark_node);
6600 dependent_p = false;
6601 }
c8094d83 6602
c353b8e3
MM
6603 processing_template_decl = saved_processing_template_decl;
6604
6605 return dependent_p;
050367a3
MM
6606}
6607
d43f603d
KL
6608/* Returns true if T depends on any template parameter with level LEVEL. */
6609
6610int
6611uses_template_parms_level (tree t, int level)
6612{
c095a4f8
DG
6613 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
6614 /*include_nondeduced_p=*/true);
d43f603d
KL
6615}
6616
27fafc8d 6617static int tinst_depth;
e9f32eb5 6618extern int max_tinst_depth;
5566b478 6619#ifdef GATHER_STATISTICS
27fafc8d 6620int depth_reached;
5566b478 6621#endif
8dfaeb63
MM
6622static int tinst_level_tick;
6623static int last_template_error_tick;
8d08fdba 6624
3ae18eaf
JM
6625/* We're starting to instantiate D; record the template instantiation context
6626 for diagnostics and to restore it later. */
6627
aa9d8196 6628static int
3a978d72 6629push_tinst_level (tree d)
8d08fdba 6630{
be93747e 6631 struct tinst_level *new_level;
8d08fdba 6632
7215f9a0
MS
6633 if (tinst_depth >= max_tinst_depth)
6634 {
8adf5b5e
JM
6635 /* If the instantiation in question still has unbound template parms,
6636 we don't really care if we can't instantiate it, so just return.
0cbd7506 6637 This happens with base instantiation for implicit `typename'. */
8adf5b5e
JM
6638 if (uses_template_parms (d))
6639 return 0;
6640
1139b3d8 6641 last_template_error_tick = tinst_level_tick;
0f51ccfc 6642 error ("template instantiation depth exceeds maximum of %d (use "
0cbd7506 6643 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
b4f4233d 6644 max_tinst_depth, d);
5566b478 6645
cb753e49 6646 print_instantiation_context ();
5566b478 6647
7215f9a0
MS
6648 return 0;
6649 }
6650
be93747e
KG
6651 new_level = GGC_NEW (struct tinst_level);
6652 new_level->decl = d;
6653 new_level->locus = input_location;
6654 new_level->in_system_header_p = in_system_header;
6655 new_level->next = current_tinst_level;
6656 current_tinst_level = new_level;
5566b478 6657
7215f9a0 6658 ++tinst_depth;
5566b478
MS
6659#ifdef GATHER_STATISTICS
6660 if (tinst_depth > depth_reached)
6661 depth_reached = tinst_depth;
6662#endif
6663
27fafc8d 6664 ++tinst_level_tick;
7215f9a0 6665 return 1;
8d08fdba
MS
6666}
6667
3ae18eaf
JM
6668/* We're done instantiating this template; return to the instantiation
6669 context. */
6670
aa9d8196 6671static void
3a978d72 6672pop_tinst_level (void)
8d08fdba 6673{
ae58fa02
MM
6674 /* Restore the filename and line number stashed away when we started
6675 this instantiation. */
e2c3721c 6676 input_location = current_tinst_level->locus;
e2c3721c 6677 current_tinst_level = current_tinst_level->next;
7215f9a0 6678 --tinst_depth;
27fafc8d 6679 ++tinst_level_tick;
8d08fdba
MS
6680}
6681
3ae18eaf
JM
6682/* We're instantiating a deferred template; restore the template
6683 instantiation context in which the instantiation was requested, which
e2c3721c 6684 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
3ae18eaf 6685
e2c3721c
PB
6686static tree
6687reopen_tinst_level (struct tinst_level *level)
3ae18eaf 6688{
e2c3721c 6689 struct tinst_level *t;
3ae18eaf
JM
6690
6691 tinst_depth = 0;
e2c3721c 6692 for (t = level; t; t = t->next)
3ae18eaf
JM
6693 ++tinst_depth;
6694
6695 current_tinst_level = level;
6696 pop_tinst_level ();
e2c3721c 6697 return level->decl;
3ae18eaf
JM
6698}
6699
61172206
JM
6700/* Returns the TINST_LEVEL which gives the original instantiation
6701 context. */
6702
e2c3721c 6703struct tinst_level *
61172206
JM
6704outermost_tinst_level (void)
6705{
e2c3721c
PB
6706 struct tinst_level *level = current_tinst_level;
6707 if (level)
6708 while (level->next)
6709 level = level->next;
6710 return level;
61172206
JM
6711}
6712
172a4594
DS
6713/* Returns TRUE if PARM is a parameter of the template TEMPL. */
6714
6715bool
6716parameter_of_template_p (tree parm, tree templ)
6717{
6718 tree parms;
6719 int i;
6720
6721 if (!parm || !templ)
6722 return false;
6723
6724 gcc_assert (DECL_TEMPLATE_PARM_P (parm));
6725 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
6726
6727 parms = DECL_TEMPLATE_PARMS (templ);
6728 parms = INNERMOST_TEMPLATE_PARMS (parms);
6729
6730 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6731 if (parm == TREE_VALUE (TREE_VEC_ELT (parms, i)))
6732 return true;
6733
6734 return false;
6735}
6736
f84b4be9
JM
6737/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
6738 vector of template arguments, as for tsubst.
6739
dc957d14 6740 Returns an appropriate tsubst'd friend declaration. */
f84b4be9
JM
6741
6742static tree
3a978d72 6743tsubst_friend_function (tree decl, tree args)
f84b4be9
JM
6744{
6745 tree new_friend;
27fafc8d 6746
c8094d83 6747 if (TREE_CODE (decl) == FUNCTION_DECL
f84b4be9
JM
6748 && DECL_TEMPLATE_INSTANTIATION (decl)
6749 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
6750 /* This was a friend declared with an explicit template
6751 argument list, e.g.:
c8094d83 6752
f84b4be9 6753 friend void f<>(T);
c8094d83 6754
f84b4be9
JM
6755 to indicate that f was a template instantiation, not a new
6756 function declaration. Now, we have to figure out what
6757 instantiation of what template. */
6758 {
76e57b45 6759 tree template_id, arglist, fns;
f84b4be9
JM
6760 tree new_args;
6761 tree tmpl;
ed2fa432 6762 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
c8094d83 6763
76e57b45 6764 /* Friend functions are looked up in the containing namespace scope.
0cbd7506 6765 We must enter that scope, to avoid finding member functions of the
39a13be5 6766 current class with same name. */
76e57b45
NS
6767 push_nested_namespace (ns);
6768 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
015c2c66
MM
6769 tf_warning_or_error, NULL_TREE,
6770 /*integral_constant_expression_p=*/false);
76e57b45
NS
6771 pop_nested_namespace (ns);
6772 arglist = tsubst (DECL_TI_ARGS (decl), args,
23fca1f5 6773 tf_warning_or_error, NULL_TREE);
76e57b45 6774 template_id = lookup_template_function (fns, arglist);
c8094d83 6775
23fca1f5 6776 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
36a117a5 6777 tmpl = determine_specialization (template_id, new_friend,
c8094d83 6778 &new_args,
5fe7b654 6779 /*need_member_template=*/0,
29a1da1c
MM
6780 TREE_VEC_LENGTH (args),
6781 tsk_none);
6e432b31 6782 return instantiate_template (tmpl, new_args, tf_error);
f84b4be9 6783 }
36a117a5 6784
23fca1f5 6785 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
c8094d83 6786
36a117a5 6787 /* The NEW_FRIEND will look like an instantiation, to the
f84b4be9
JM
6788 compiler, but is not an instantiation from the point of view of
6789 the language. For example, we might have had:
c8094d83 6790
f84b4be9
JM
6791 template <class T> struct S {
6792 template <class U> friend void f(T, U);
6793 };
c8094d83 6794
f84b4be9
JM
6795 Then, in S<int>, template <class U> void f(int, U) is not an
6796 instantiation of anything. */
ac2b3222
AP
6797 if (new_friend == error_mark_node)
6798 return error_mark_node;
c8094d83 6799
f84b4be9
JM
6800 DECL_USE_TEMPLATE (new_friend) = 0;
6801 if (TREE_CODE (decl) == TEMPLATE_DECL)
655dc6ee
JM
6802 {
6803 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
6804 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
6805 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
6806 }
36a117a5 6807
92643fea
MM
6808 /* The mangled name for the NEW_FRIEND is incorrect. The function
6809 is not a template instantiation and should not be mangled like
6810 one. Therefore, we forget the mangling here; we'll recompute it
6811 later if we need it. */
36a117a5
MM
6812 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
6813 {
19e7881c 6814 SET_DECL_RTL (new_friend, NULL_RTX);
92643fea 6815 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
36a117a5 6816 }
c8094d83 6817
6eb3bb27 6818 if (DECL_NAMESPACE_SCOPE_P (new_friend))
f84b4be9 6819 {
36a117a5 6820 tree old_decl;
fbf1c34b
MM
6821 tree new_friend_template_info;
6822 tree new_friend_result_template_info;
92da7074 6823 tree ns;
fbf1c34b
MM
6824 int new_friend_is_defn;
6825
6826 /* We must save some information from NEW_FRIEND before calling
6827 duplicate decls since that function will free NEW_FRIEND if
6828 possible. */
6829 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5bd61841 6830 new_friend_is_defn =
c8094d83 6831 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5bd61841
MM
6832 (template_for_substitution (new_friend)))
6833 != NULL_TREE);
f84b4be9 6834 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
fbf1c34b
MM
6835 {
6836 /* This declaration is a `primary' template. */
6837 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
c8094d83 6838
fbf1c34b 6839 new_friend_result_template_info
17aec3eb 6840 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
fbf1c34b
MM
6841 }
6842 else
5bd61841 6843 new_friend_result_template_info = NULL_TREE;
36a117a5 6844
b01e6d2b
JM
6845 /* Make the init_value nonzero so pushdecl knows this is a defn. */
6846 if (new_friend_is_defn)
6847 DECL_INITIAL (new_friend) = error_mark_node;
6848
1c227897
MM
6849 /* Inside pushdecl_namespace_level, we will push into the
6850 current namespace. However, the friend function should go
c6002625 6851 into the namespace of the template. */
92da7074
ML
6852 ns = decl_namespace_context (new_friend);
6853 push_nested_namespace (ns);
d63d5d0c 6854 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
92da7074 6855 pop_nested_namespace (ns);
36a117a5 6856
0014c247
VR
6857 if (old_decl == error_mark_node)
6858 return error_mark_node;
6859
36a117a5
MM
6860 if (old_decl != new_friend)
6861 {
6862 /* This new friend declaration matched an existing
6863 declaration. For example, given:
6864
6865 template <class T> void f(T);
c8094d83
MS
6866 template <class U> class C {
6867 template <class T> friend void f(T) {}
36a117a5
MM
6868 };
6869
6870 the friend declaration actually provides the definition
6871 of `f', once C has been instantiated for some type. So,
6872 old_decl will be the out-of-class template declaration,
6873 while new_friend is the in-class definition.
6874
6875 But, if `f' was called before this point, the
6876 instantiation of `f' will have DECL_TI_ARGS corresponding
6877 to `T' but not to `U', references to which might appear
6878 in the definition of `f'. Previously, the most general
6879 template for an instantiation of `f' was the out-of-class
6880 version; now it is the in-class version. Therefore, we
6881 run through all specialization of `f', adding to their
6882 DECL_TI_ARGS appropriately. In particular, they need a
6883 new set of outer arguments, corresponding to the
c8094d83 6884 arguments for this class instantiation.
36a117a5
MM
6885
6886 The same situation can arise with something like this:
6887
6888 friend void f(int);
c8094d83 6889 template <class T> class C {
0cbd7506
MS
6890 friend void f(T) {}
6891 };
36a117a5
MM
6892
6893 when `C<int>' is instantiated. Now, `f(int)' is defined
6894 in the class. */
6895
fbf1c34b
MM
6896 if (!new_friend_is_defn)
6897 /* On the other hand, if the in-class declaration does
6898 *not* provide a definition, then we don't want to alter
6899 existing definitions. We can just leave everything
6900 alone. */
36a117a5 6901 ;
fbf1c34b 6902 else
36a117a5 6903 {
2b110bfc
JM
6904 tree new_template = TI_TEMPLATE (new_friend_template_info);
6905 tree new_args = TI_ARGS (new_friend_template_info);
6906
fbf1c34b
MM
6907 /* Overwrite whatever template info was there before, if
6908 any, with the new template information pertaining to
6909 the declaration. */
6910 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
6911
6912 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
2b110bfc
JM
6913 /* We should have called reregister_specialization in
6914 duplicate_decls. */
6915 gcc_assert (retrieve_specialization (new_template,
6916 new_args, 0)
6917 == old_decl);
c8094d83 6918 else
36a117a5 6919 {
fbf1c34b 6920 tree t;
fbf1c34b 6921
2b110bfc
JM
6922 /* Indicate that the old function template is a partial
6923 instantiation. */
c8094d83 6924 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
fbf1c34b 6925 = new_friend_result_template_info;
c8094d83 6926
2b110bfc
JM
6927 gcc_assert (new_template
6928 == most_general_template (new_template));
6929 gcc_assert (new_template != old_decl);
6930
6931 /* Reassign any specializations already in the hash table
6932 to the new more general template, and add the
6933 additional template args. */
6934 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
fbf1c34b
MM
6935 t != NULL_TREE;
6936 t = TREE_CHAIN (t))
6937 {
6938 tree spec = TREE_VALUE (t);
2b110bfc
JM
6939 spec_entry elt;
6940
6941 elt.tmpl = old_decl;
6942 elt.args = DECL_TI_ARGS (spec);
6943 elt.spec = NULL_TREE;
6944
6945 htab_remove_elt (decl_specializations, &elt);
c8094d83
MS
6946
6947 DECL_TI_ARGS (spec)
2b110bfc 6948 = add_outermost_template_args (new_args,
fbf1c34b 6949 DECL_TI_ARGS (spec));
fbf1c34b 6950
2b110bfc
JM
6951 register_specialization
6952 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
6953
fbf1c34b 6954 }
2b110bfc 6955 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
36a117a5
MM
6956 }
6957 }
6958
6959 /* The information from NEW_FRIEND has been merged into OLD_DECL
6960 by duplicate_decls. */
6961 new_friend = old_decl;
6962 }
f84b4be9 6963 }
6e432b31 6964 else
f84b4be9 6965 {
6e432b31
KL
6966 tree context = DECL_CONTEXT (new_friend);
6967 bool dependent_p;
6968
6969 /* In the code
6970 template <class T> class C {
6971 template <class U> friend void C1<U>::f (); // case 1
6972 friend void C2<T>::f (); // case 2
6973 };
6974 we only need to make sure CONTEXT is a complete type for
6975 case 2. To distinguish between the two cases, we note that
6976 CONTEXT of case 1 remains dependent type after tsubst while
6977 this isn't true for case 2. */
6978 ++processing_template_decl;
6979 dependent_p = dependent_type_p (context);
6980 --processing_template_decl;
6981
6982 if (!dependent_p
6983 && !complete_type_or_else (context, NULL_TREE))
6984 return error_mark_node;
6985
6986 if (COMPLETE_TYPE_P (context))
6987 {
6988 /* Check to see that the declaration is really present, and,
6989 possibly obtain an improved declaration. */
6990 tree fn = check_classfn (context,
6991 new_friend, NULL_TREE);
6992
684939ce 6993 if (fn)
6e432b31
KL
6994 new_friend = fn;
6995 }
f84b4be9
JM
6996 }
6997
6998 return new_friend;
6999}
7000
1aed5355
MM
7001/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
7002 template arguments, as for tsubst.
6757edfe 7003
dc957d14 7004 Returns an appropriate tsubst'd friend type or error_mark_node on
4b054b80 7005 failure. */
6757edfe
MM
7006
7007static tree
3a978d72 7008tsubst_friend_class (tree friend_tmpl, tree args)
6757edfe 7009{
1aed5355 7010 tree friend_type;
25aab5d0 7011 tree tmpl;
3e0ec82f 7012 tree context;
6757edfe 7013
3e0ec82f
MM
7014 context = DECL_CONTEXT (friend_tmpl);
7015
7016 if (context)
77adef84 7017 {
3e0ec82f
MM
7018 if (TREE_CODE (context) == NAMESPACE_DECL)
7019 push_nested_namespace (context);
7020 else
c8094d83 7021 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
3e0ec82f 7022 }
25aab5d0 7023
105d72c5
MM
7024 /* Look for a class template declaration. We look for hidden names
7025 because two friend declarations of the same template are the
7026 same. For example, in:
7027
7028 struct A {
7029 template <typename> friend class F;
7030 };
7031 template <typename> struct B {
7032 template <typename> friend class F;
7033 };
7034
7035 both F templates are the same. */
7036 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
7037 /*block_p=*/true, 0,
7038 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
25aab5d0 7039
3e0ec82f
MM
7040 /* But, if we don't find one, it might be because we're in a
7041 situation like this:
77adef84 7042
3e0ec82f
MM
7043 template <class T>
7044 struct S {
7045 template <class U>
7046 friend struct S;
7047 };
7048
7049 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
7050 for `S<int>', not the TEMPLATE_DECL. */
7051 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
7052 {
10e6657a 7053 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
3e0ec82f 7054 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
25aab5d0 7055 }
6757edfe 7056
25aab5d0 7057 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6757edfe
MM
7058 {
7059 /* The friend template has already been declared. Just
36a117a5
MM
7060 check to see that the declarations match, and install any new
7061 default parameters. We must tsubst the default parameters,
7062 of course. We only need the innermost template parameters
7063 because that is all that redeclare_class_template will look
7064 at. */
3e0ec82f
MM
7065 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
7066 > TMPL_ARGS_DEPTH (args))
7067 {
7068 tree parms;
b131ad7c 7069 location_t saved_input_location;
3e0ec82f 7070 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
23fca1f5 7071 args, tf_warning_or_error);
b131ad7c
MLI
7072
7073 saved_input_location = input_location;
7074 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
3e0ec82f 7075 redeclare_class_template (TREE_TYPE (tmpl), parms);
b131ad7c
MLI
7076 input_location = saved_input_location;
7077
3e0ec82f
MM
7078 }
7079
6757edfe
MM
7080 friend_type = TREE_TYPE (tmpl);
7081 }
7082 else
7083 {
7084 /* The friend template has not already been declared. In this
7085 case, the instantiation of the template class will cause the
7086 injection of this template into the global scope. */
23fca1f5 7087 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
dca56f77
VR
7088 if (tmpl == error_mark_node)
7089 return error_mark_node;
6757edfe
MM
7090
7091 /* The new TMPL is not an instantiation of anything, so we
0cbd7506 7092 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6757edfe
MM
7093 the new type because that is supposed to be the corresponding
7094 template decl, i.e., TMPL. */
7095 DECL_USE_TEMPLATE (tmpl) = 0;
7096 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
7097 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
68c78847
KL
7098 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
7099 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6757edfe
MM
7100
7101 /* Inject this template into the global scope. */
d63d5d0c 7102 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6757edfe
MM
7103 }
7104
c8094d83 7105 if (context)
3e0ec82f
MM
7106 {
7107 if (TREE_CODE (context) == NAMESPACE_DECL)
7108 pop_nested_namespace (context);
7109 else
7110 pop_nested_class ();
7111 }
7112
6757edfe
MM
7113 return friend_type;
7114}
f84b4be9 7115
17f29637
KL
7116/* Returns zero if TYPE cannot be completed later due to circularity.
7117 Otherwise returns one. */
7118
d5372501 7119static int
3a978d72 7120can_complete_type_without_circularity (tree type)
17f29637
KL
7121{
7122 if (type == NULL_TREE || type == error_mark_node)
7123 return 0;
7124 else if (COMPLETE_TYPE_P (type))
7125 return 1;
7126 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
7127 return can_complete_type_without_circularity (TREE_TYPE (type));
8c6ab2db
NS
7128 else if (CLASS_TYPE_P (type)
7129 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
17f29637
KL
7130 return 0;
7131 else
7132 return 1;
7133}
7134
4f4141ff
JM
7135/* Apply any attributes which had to be deferred until instantiation
7136 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
7137 ARGS, COMPLAIN, IN_DECL are as tsubst. */
7138
7139static void
7140apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
7141 tree args, tsubst_flags_t complain, tree in_decl)
7142{
5818c8e4 7143 tree last_dep = NULL_TREE;
4f4141ff 7144 tree t;
5818c8e4
JM
7145 tree *p;
7146
7147 for (t = attributes; t; t = TREE_CHAIN (t))
7148 if (ATTR_IS_DEPENDENT (t))
7149 {
7150 last_dep = t;
7151 attributes = copy_list (attributes);
7152 break;
7153 }
4f4141ff
JM
7154
7155 if (DECL_P (*decl_p))
823e5f7f
JJ
7156 {
7157 if (TREE_TYPE (*decl_p) == error_mark_node)
7158 return;
7159 p = &DECL_ATTRIBUTES (*decl_p);
7160 }
4f4141ff 7161 else
5818c8e4 7162 p = &TYPE_ATTRIBUTES (*decl_p);
4f4141ff 7163
5818c8e4
JM
7164 if (last_dep)
7165 {
7166 tree late_attrs = NULL_TREE;
7167 tree *q = &late_attrs;
7168
7169 for (*p = attributes; *p; )
7170 {
7171 t = *p;
7172 if (ATTR_IS_DEPENDENT (t))
7173 {
7174 *p = TREE_CHAIN (t);
7175 TREE_CHAIN (t) = NULL_TREE;
ff2f1c5f
JJ
7176 /* If the first attribute argument is an identifier, don't
7177 pass it through tsubst. Attributes like mode, format,
7178 cleanup and several target specific attributes expect it
7179 unmodified. */
7180 if (TREE_VALUE (t)
7181 && TREE_CODE (TREE_VALUE (t)) == TREE_LIST
7182 && TREE_VALUE (TREE_VALUE (t))
7183 && (TREE_CODE (TREE_VALUE (TREE_VALUE (t)))
7184 == IDENTIFIER_NODE))
7185 {
7186 tree chain
7187 = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
7188 in_decl,
7189 /*integral_constant_expression_p=*/false);
7190 if (chain != TREE_CHAIN (TREE_VALUE (t)))
7191 TREE_VALUE (t)
7192 = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
7193 chain);
7194 }
7195 else
7196 TREE_VALUE (t)
7197 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
7198 /*integral_constant_expression_p=*/false);
5818c8e4
JM
7199 *q = t;
7200 q = &TREE_CHAIN (t);
7201 }
7202 else
7203 p = &TREE_CHAIN (t);
7204 }
4f4141ff 7205
5818c8e4
JM
7206 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
7207 }
4f4141ff
JM
7208}
7209
d0940d56
DS
7210/* Perform (or defer) access check for typedefs that were referenced
7211 from within the template TMPL code.
7212 This is a subroutine of instantiate_template and instantiate_class_template.
7213 TMPL is the template to consider and TARGS is the list of arguments of
7214 that template. */
7215
7216static void
7217perform_typedefs_access_check (tree tmpl, tree targs)
7218{
7219 tree t;
7220
2eb25c98 7221 if (!tmpl
9dbed529 7222 || (!CLASS_TYPE_P (tmpl)
2eb25c98 7223 && TREE_CODE (tmpl) != FUNCTION_DECL))
d0940d56
DS
7224 return;
7225
2eb25c98 7226 for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t))
d0940d56
DS
7227 {
7228 tree type_decl = TREE_PURPOSE (t);
7229 tree type_scope = TREE_VALUE (t);
7230
7231 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
7232 continue;
7233
7234 if (uses_template_parms (type_decl))
7235 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
7236 if (uses_template_parms (type_scope))
7237 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
7238
2eb25c98
DS
7239 perform_or_defer_access_check (TYPE_BINFO (type_scope),
7240 type_decl, type_decl);
d0940d56
DS
7241 }
7242}
7243
8d08fdba 7244tree
3a978d72 7245instantiate_class_template (tree type)
8d08fdba 7246{
be93747e 7247 tree templ, args, pattern, t, member;
36a117a5 7248 tree typedecl;
dbbf88d1 7249 tree pbinfo;
cad7e87b 7250 tree base_list;
c8094d83 7251
5566b478 7252 if (type == error_mark_node)
8d08fdba
MS
7253 return error_mark_node;
7254
c8094d83 7255 if (TYPE_BEING_DEFINED (type)
ca099ac8
MM
7256 || COMPLETE_TYPE_P (type)
7257 || dependent_type_p (type))
5566b478
MS
7258 return type;
7259
6bdb985f 7260 /* Figure out which template is being instantiated. */
be93747e
KG
7261 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
7262 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
73aad9b9 7263
4c571114
MM
7264 /* Determine what specialization of the original template to
7265 instantiate. */
be93747e 7266 t = most_specialized_class (type, templ);
8fbc5ae7 7267 if (t == error_mark_node)
73aad9b9 7268 {
8fbc5ae7
MM
7269 TYPE_BEING_DEFINED (type) = 1;
7270 return error_mark_node;
73aad9b9 7271 }
916b63c3
MM
7272 else if (t)
7273 {
7274 /* This TYPE is actually an instantiation of a partial
7275 specialization. We replace the innermost set of ARGS with
7276 the arguments appropriate for substitution. For example,
7277 given:
3db45ab5
MS
7278
7279 template <class T> struct S {};
916b63c3 7280 template <class T> struct S<T*> {};
6bdb985f 7281
916b63c3 7282 and supposing that we are instantiating S<int*>, ARGS will
3db45ab5 7283 presently be {int*} -- but we need {int}. */
916b63c3
MM
7284 pattern = TREE_TYPE (t);
7285 args = TREE_PURPOSE (t);
7286 }
73aad9b9 7287 else
916b63c3 7288 {
be93747e 7289 pattern = TREE_TYPE (templ);
916b63c3
MM
7290 args = CLASSTYPE_TI_ARGS (type);
7291 }
5566b478 7292
4c571114
MM
7293 /* If the template we're instantiating is incomplete, then clearly
7294 there's nothing we can do. */
d0f062fb 7295 if (!COMPLETE_TYPE_P (pattern))
f31c0a32 7296 return type;
5566b478 7297
4c571114
MM
7298 /* If we've recursively instantiated too many templates, stop. */
7299 if (! push_tinst_level (type))
f31c0a32 7300 return type;
4c571114
MM
7301
7302 /* Now we're really doing the instantiation. Mark the type as in
7303 the process of being defined. */
7304 TYPE_BEING_DEFINED (type) = 1;
7305
78757caa
KL
7306 /* We may be in the middle of deferred access check. Disable
7307 it now. */
7308 push_deferring_access_checks (dk_no_deferred);
7309
c353b8e3 7310 push_to_top_level ();
4c571114 7311
7813d14c 7312 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8d08fdba 7313
2eb25c98
DS
7314 /* Set the input location to the most specialized template definition.
7315 This is needed if tsubsting causes an error. */
7316 typedecl = TYPE_MAIN_DECL (pattern);
12af7ba3 7317 input_location = DECL_SOURCE_LOCATION (typedecl);
68ea098a 7318
0fcedd9c 7319 TYPE_HAS_USER_CONSTRUCTOR (type) = TYPE_HAS_USER_CONSTRUCTOR (pattern);
834c6dff
MM
7320 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
7321 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
f7da6097 7322 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
f7da6097
MS
7323 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
7324 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
f7da6097
MS
7325 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
7326 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
7327 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
7328 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
f7da6097
MS
7329 TYPE_PACKED (type) = TYPE_PACKED (pattern);
7330 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
11cf4d18 7331 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
eff71ab0 7332 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6bdb8141
JM
7333 if (ANON_AGGR_TYPE_P (pattern))
7334 SET_ANON_AGGR_TYPE_P (type);
8d039470
MS
7335 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
7336 {
7337 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
7338 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
7339 }
f7da6097 7340
dbbf88d1 7341 pbinfo = TYPE_BINFO (pattern);
1456deaf 7342
315fb5db
NS
7343 /* We should never instantiate a nested class before its enclosing
7344 class; we need to look up the nested class by name before we can
7345 instantiate it, and that lookup should instantiate the enclosing
7346 class. */
7347 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
7348 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
7349 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
1456deaf 7350
cad7e87b 7351 base_list = NULL_TREE;
fa743e8c 7352 if (BINFO_N_BASE_BINFOS (pbinfo))
3fd71a52 7353 {
fa743e8c 7354 tree pbase_binfo;
a2507277 7355 tree context = TYPE_CONTEXT (type);
4514aa8c 7356 tree pushed_scope;
3fd71a52 7357 int i;
5566b478 7358
a2507277
NS
7359 /* We must enter the scope containing the type, as that is where
7360 the accessibility of types named in dependent bases are
7361 looked up from. */
4514aa8c 7362 pushed_scope = push_scope (context ? context : global_namespace);
c8094d83 7363
3fd71a52
MM
7364 /* Substitute into each of the bases to determine the actual
7365 basetypes. */
fa743e8c 7366 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
3fd71a52
MM
7367 {
7368 tree base;
fa743e8c 7369 tree access = BINFO_BASE_ACCESS (pbinfo, i);
5d80a306
DG
7370 tree expanded_bases = NULL_TREE;
7371 int idx, len = 1;
7372
7373 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
7374 {
7375 expanded_bases =
7376 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
7377 args, tf_error, NULL_TREE);
7378 if (expanded_bases == error_mark_node)
7379 continue;
7380
7381 len = TREE_VEC_LENGTH (expanded_bases);
7382 }
7383
7384 for (idx = 0; idx < len; idx++)
7385 {
7386 if (expanded_bases)
7387 /* Extract the already-expanded base class. */
7388 base = TREE_VEC_ELT (expanded_bases, idx);
7389 else
7390 /* Substitute to figure out the base class. */
7391 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
7392 NULL_TREE);
7393
7394 if (base == error_mark_node)
7395 continue;
7396
7397 base_list = tree_cons (access, base, base_list);
7398 if (BINFO_VIRTUAL_P (pbase_binfo))
7399 TREE_TYPE (base_list) = integer_type_node;
7400 }
3fd71a52 7401 }
dfbcd65a 7402
3fd71a52
MM
7403 /* The list is now in reverse order; correct that. */
7404 base_list = nreverse (base_list);
7405
4514aa8c
NS
7406 if (pushed_scope)
7407 pop_scope (pushed_scope);
3fd71a52 7408 }
cad7e87b
NS
7409 /* Now call xref_basetypes to set up all the base-class
7410 information. */
7411 xref_basetypes (type, base_list);
7412
4f4141ff
JM
7413 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
7414 (int) ATTR_FLAG_TYPE_IN_PLACE,
7415 args, tf_error, NULL_TREE);
5566b478 7416
b74a0560
MM
7417 /* Now that our base classes are set up, enter the scope of the
7418 class, so that name lookups into base classes, etc. will work
dc957d14 7419 correctly. This is precisely analogous to what we do in
b74a0560 7420 begin_class_definition when defining an ordinary non-template
56d0c6e3
JM
7421 class, except we also need to push the enclosing classes. */
7422 push_nested_class (type);
b74a0560 7423
7088fca9 7424 /* Now members are processed in the order of declaration. */
8c6ab2db
NS
7425 for (member = CLASSTYPE_DECL_LIST (pattern);
7426 member; member = TREE_CHAIN (member))
8d08fdba 7427 {
7088fca9 7428 tree t = TREE_VALUE (member);
5566b478 7429
7088fca9 7430 if (TREE_PURPOSE (member))
ed44da02 7431 {
7088fca9
KL
7432 if (TYPE_P (t))
7433 {
5e0c54e5 7434 /* Build new CLASSTYPE_NESTED_UTDS. */
8d08fdba 7435
7088fca9 7436 tree newtag;
883a2bff
MM
7437 bool class_template_p;
7438
2678bae8
VR
7439 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
7440 && TYPE_LANG_SPECIFIC (t)
7441 && CLASSTYPE_IS_TEMPLATE (t));
883a2bff 7442 /* If the member is a class template, then -- even after
6c745393 7443 substitution -- there may be dependent types in the
883a2bff
MM
7444 template argument list for the class. We increment
7445 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
7446 that function will assume that no types are dependent
7447 when outside of a template. */
7448 if (class_template_p)
7449 ++processing_template_decl;
2678bae8 7450 newtag = tsubst (t, args, tf_error, NULL_TREE);
883a2bff
MM
7451 if (class_template_p)
7452 --processing_template_decl;
2620d095
KL
7453 if (newtag == error_mark_node)
7454 continue;
7455
7088fca9
KL
7456 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
7457 {
2678bae8
VR
7458 tree name = TYPE_IDENTIFIER (t);
7459
883a2bff 7460 if (class_template_p)
7088fca9
KL
7461 /* Unfortunately, lookup_template_class sets
7462 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
3afd2e20
NS
7463 instantiation (i.e., for the type of a member
7464 template class nested within a template class.)
7465 This behavior is required for
7466 maybe_process_partial_specialization to work
7467 correctly, but is not accurate in this case;
7468 the TAG is not an instantiation of anything.
7469 (The corresponding TEMPLATE_DECL is an
7470 instantiation, but the TYPE is not.) */
7088fca9
KL
7471 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
7472
7473 /* Now, we call pushtag to put this NEWTAG into the scope of
7474 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
7475 pushtag calling push_template_decl. We don't have to do
7476 this for enums because it will already have been done in
7477 tsubst_enum. */
7478 if (name)
7479 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
bd3d082e 7480 pushtag (name, newtag, /*tag_scope=*/ts_current);
7088fca9
KL
7481 }
7482 }
c8094d83 7483 else if (TREE_CODE (t) == FUNCTION_DECL
7088fca9
KL
7484 || DECL_FUNCTION_TEMPLATE_P (t))
7485 {
7486 /* Build new TYPE_METHODS. */
fecafe5e 7487 tree r;
c8094d83 7488
fecafe5e 7489 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 7490 ++processing_template_decl;
fecafe5e
NS
7491 r = tsubst (t, args, tf_error, NULL_TREE);
7492 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 7493 --processing_template_decl;
7088fca9 7494 set_current_access_from_decl (r);
7088fca9
KL
7495 finish_member_declaration (r);
7496 }
7497 else
7498 {
7499 /* Build new TYPE_FIELDS. */
55a3debe
DG
7500 if (TREE_CODE (t) == STATIC_ASSERT)
7501 {
7502 tree condition =
7503 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
7504 tf_warning_or_error, NULL_TREE,
7505 /*integral_constant_expression_p=*/true);
7506 finish_static_assert (condition,
7507 STATIC_ASSERT_MESSAGE (t),
7508 STATIC_ASSERT_SOURCE_LOCATION (t),
7509 /*member_p=*/true);
7510 }
7511 else if (TREE_CODE (t) != CONST_DECL)
7088fca9
KL
7512 {
7513 tree r;
fa8d6e85 7514
39a13be5 7515 /* The file and line for this declaration, to
d479d37f
NS
7516 assist in error message reporting. Since we
7517 called push_tinst_level above, we don't need to
7518 restore these. */
f31686a3 7519 input_location = DECL_SOURCE_LOCATION (t);
fa8d6e85 7520
fb5ce3c9 7521 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 7522 ++processing_template_decl;
23fca1f5 7523 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
fb5ce3c9 7524 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 7525 --processing_template_decl;
7088fca9
KL
7526 if (TREE_CODE (r) == VAR_DECL)
7527 {
b794e321
MM
7528 /* In [temp.inst]:
7529
7530 [t]he initialization (and any associated
7531 side-effects) of a static data member does
7532 not occur unless the static data member is
7533 itself used in a way that requires the
7534 definition of the static data member to
3db45ab5 7535 exist.
b794e321
MM
7536
7537 Therefore, we do not substitute into the
3db45ab5
MS
7538 initialized for the static data member here. */
7539 finish_static_data_member_decl
7540 (r,
7541 /*init=*/NULL_TREE,
d174af6c 7542 /*init_const_expr_p=*/false,
3db45ab5 7543 /*asmspec_tree=*/NULL_TREE,
b794e321 7544 /*flags=*/0);
7088fca9
KL
7545 if (DECL_INITIALIZED_IN_CLASS_P (r))
7546 check_static_variable_definition (r, TREE_TYPE (r));
7547 }
7548 else if (TREE_CODE (r) == FIELD_DECL)
7549 {
7550 /* Determine whether R has a valid type and can be
7551 completed later. If R is invalid, then it is
7552 replaced by error_mark_node so that it will not be
7553 added to TYPE_FIELDS. */
7554 tree rtype = TREE_TYPE (r);
7555 if (can_complete_type_without_circularity (rtype))
7556 complete_type (rtype);
7557
7558 if (!COMPLETE_TYPE_P (rtype))
7559 {
7560 cxx_incomplete_type_error (r, rtype);
0cbd7506 7561 r = error_mark_node;
7088fca9
KL
7562 }
7563 }
5566b478 7564
7088fca9
KL
7565 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
7566 such a thing will already have been added to the field
7567 list by tsubst_enum in finish_member_declaration in the
5e0c54e5 7568 CLASSTYPE_NESTED_UTDS case above. */
7088fca9
KL
7569 if (!(TREE_CODE (r) == TYPE_DECL
7570 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
43b26a01 7571 && DECL_ARTIFICIAL (r)))
7088fca9
KL
7572 {
7573 set_current_access_from_decl (r);
7574 finish_member_declaration (r);
7575 }
0cbd7506 7576 }
7088fca9 7577 }
61fc8c9e 7578 }
7088fca9
KL
7579 else
7580 {
7581 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
7582 {
7583 /* Build new CLASSTYPE_FRIEND_CLASSES. */
7584
7585 tree friend_type = t;
b939a023 7586 bool adjust_processing_template_decl = false;
1aed5355 7587
7088fca9 7588 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
b939a023 7589 {
5a24482e 7590 /* template <class T> friend class C; */
b939a023 7591 friend_type = tsubst_friend_class (friend_type, args);
0cbd7506 7592 adjust_processing_template_decl = true;
b939a023
KL
7593 }
7594 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
7595 {
5a24482e 7596 /* template <class T> friend class C::D; */
b939a023 7597 friend_type = tsubst (friend_type, args,
23fca1f5 7598 tf_warning_or_error, NULL_TREE);
b939a023
KL
7599 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
7600 friend_type = TREE_TYPE (friend_type);
0cbd7506 7601 adjust_processing_template_decl = true;
b939a023
KL
7602 }
7603 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
7604 {
5a24482e
KL
7605 /* This could be either
7606
7607 friend class T::C;
7608
7609 when dependent_type_p is false or
7610
7611 template <class U> friend class T::C;
7612
7613 otherwise. */
b939a023 7614 friend_type = tsubst (friend_type, args,
23fca1f5 7615 tf_warning_or_error, NULL_TREE);
b939a023
KL
7616 /* Bump processing_template_decl for correct
7617 dependent_type_p calculation. */
7618 ++processing_template_decl;
7619 if (dependent_type_p (friend_type))
7620 adjust_processing_template_decl = true;
7621 --processing_template_decl;
7622 }
5a24482e
KL
7623 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
7624 && hidden_name_p (TYPE_NAME (friend_type)))
7088fca9 7625 {
5a24482e
KL
7626 /* friend class C;
7627
7628 where C hasn't been declared yet. Let's lookup name
7629 from namespace scope directly, bypassing any name that
7630 come from dependent base class. */
7088fca9
KL
7631 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
7632
7633 /* The call to xref_tag_from_type does injection for friend
7634 classes. */
7635 push_nested_namespace (ns);
c8094d83
MS
7636 friend_type =
7637 xref_tag_from_type (friend_type, NULL_TREE,
5a24482e 7638 /*tag_scope=*/ts_current);
7088fca9
KL
7639 pop_nested_namespace (ns);
7640 }
5a24482e
KL
7641 else if (uses_template_parms (friend_type))
7642 /* friend class C<T>; */
7643 friend_type = tsubst (friend_type, args,
23fca1f5 7644 tf_warning_or_error, NULL_TREE);
5a24482e
KL
7645 /* Otherwise it's
7646
7647 friend class C;
7648
7649 where C is already declared or
7650
7651 friend class C<int>;
7652
0cbd7506 7653 We don't have to do anything in these cases. */
1aed5355 7654
b939a023 7655 if (adjust_processing_template_decl)
7088fca9
KL
7656 /* Trick make_friend_class into realizing that the friend
7657 we're adding is a template, not an ordinary class. It's
7658 important that we use make_friend_class since it will
7659 perform some error-checking and output cross-reference
7660 information. */
7661 ++processing_template_decl;
fc378698 7662
b939a023 7663 if (friend_type != error_mark_node)
0cbd7506 7664 make_friend_class (type, friend_type, /*complain=*/false);
fc378698 7665
b939a023 7666 if (adjust_processing_template_decl)
7088fca9
KL
7667 --processing_template_decl;
7668 }
7669 else
9579624e
KL
7670 {
7671 /* Build new DECL_FRIENDLIST. */
7672 tree r;
7673
39a13be5 7674 /* The file and line for this declaration, to
6e432b31
KL
7675 assist in error message reporting. Since we
7676 called push_tinst_level above, we don't need to
7677 restore these. */
7678 input_location = DECL_SOURCE_LOCATION (t);
7679
9579624e 7680 if (TREE_CODE (t) == TEMPLATE_DECL)
cad7e87b
NS
7681 {
7682 ++processing_template_decl;
7683 push_deferring_access_checks (dk_no_check);
7684 }
c8094d83 7685
9579624e 7686 r = tsubst_friend_function (t, args);
9579624e 7687 add_friend (type, r, /*complain=*/false);
cad7e87b
NS
7688 if (TREE_CODE (t) == TEMPLATE_DECL)
7689 {
7690 pop_deferring_access_checks ();
7691 --processing_template_decl;
7692 }
9579624e 7693 }
7088fca9
KL
7694 }
7695 }
5566b478 7696
61a127b3
MM
7697 /* Set the file and line number information to whatever is given for
7698 the class itself. This puts error messages involving generated
7699 implicit functions at a predictable point, and the same point
7700 that would be used for non-template classes. */
f31686a3 7701 input_location = DECL_SOURCE_LOCATION (typedecl);
6de9cd9a 7702
61a127b3 7703 unreverse_member_declarations (type);
9f33663b 7704 finish_struct_1 (type);
5524676d 7705 TYPE_BEING_DEFINED (type) = 0;
8d08fdba 7706
9188c363
MM
7707 /* Now that the class is complete, instantiate default arguments for
7708 any member functions. We don't do this earlier because the
7709 default arguments may reference members of the class. */
be93747e 7710 if (!PRIMARY_TEMPLATE_P (templ))
9188c363 7711 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
c8094d83 7712 if (TREE_CODE (t) == FUNCTION_DECL
dc957d14 7713 /* Implicitly generated member functions will not have template
9188c363
MM
7714 information; they are not instantiations, but instead are
7715 created "fresh" for each instantiation. */
7716 && DECL_TEMPLATE_INFO (t))
7717 tsubst_default_arguments (t);
7718
d0940d56
DS
7719 /* Some typedefs referenced from within the template code need to be access
7720 checked at template instantiation time, i.e now. These types were
7721 added to the template at parsing time. Let's get those and perform
2eb25c98
DS
7722 the access checks then. */
7723 perform_typedefs_access_check (pattern, args);
d0940d56 7724 perform_deferred_access_checks ();
56d0c6e3 7725 pop_nested_class ();
5566b478 7726 pop_from_top_level ();
78757caa 7727 pop_deferring_access_checks ();
5566b478
MS
7728 pop_tinst_level ();
7729
4684cd27
MM
7730 /* The vtable for a template class can be emitted in any translation
7731 unit in which the class is instantiated. When there is no key
7732 method, however, finish_struct_1 will already have added TYPE to
7733 the keyed_classes list. */
7734 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9aad8f83
MA
7735 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
7736
5566b478 7737 return type;
8d08fdba
MS
7738}
7739
00d3396f 7740static tree
a91db711 7741tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
00d3396f 7742{
a91db711 7743 tree r;
c8094d83 7744
a91db711
NS
7745 if (!t)
7746 r = t;
7747 else if (TYPE_P (t))
7748 r = tsubst (t, args, complain, in_decl);
7749 else
00d3396f 7750 {
015c2c66
MM
7751 r = tsubst_expr (t, args, complain, in_decl,
7752 /*integral_constant_expression_p=*/true);
6f25cb35 7753 r = fold_non_dependent_expr (r);
bd83b409 7754 }
5d80a306
DG
7755 return r;
7756}
7757
6afcfe0a
JM
7758/* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
7759 NONTYPE_ARGUMENT_PACK. */
7760
7761static tree
7762make_fnparm_pack (tree spec_parm)
7763{
7764 /* Collect all of the extra "packed" parameters into an
7765 argument pack. */
7766 tree parmvec;
7767 tree parmtypevec;
7768 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
7769 tree argtypepack = make_node (TYPE_ARGUMENT_PACK);
7770 int i, len = list_length (spec_parm);
7771
7772 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
7773 parmvec = make_tree_vec (len);
7774 parmtypevec = make_tree_vec (len);
7775 for (i = 0; i < len; i++, spec_parm = TREE_CHAIN (spec_parm))
7776 {
7777 TREE_VEC_ELT (parmvec, i) = spec_parm;
7778 TREE_VEC_ELT (parmtypevec, i) = TREE_TYPE (spec_parm);
7779 }
7780
7781 /* Build the argument packs. */
7782 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
7783 SET_ARGUMENT_PACK_ARGS (argtypepack, parmtypevec);
7784 TREE_TYPE (argpack) = argtypepack;
7785
7786 return argpack;
7787}
7788
5d80a306
DG
7789/* Substitute ARGS into T, which is an pack expansion
7790 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
7791 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
7792 (if only a partial substitution could be performed) or
7793 ERROR_MARK_NODE if there was an error. */
7794tree
7795tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
7796 tree in_decl)
7797{
7798 tree pattern;
7799 tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
7800 tree first_arg_pack; int i, len = -1;
7801 tree result;
7802 int incomplete = 0;
6afcfe0a 7803 bool very_local_specializations = false;
5d80a306
DG
7804
7805 gcc_assert (PACK_EXPANSION_P (t));
7806 pattern = PACK_EXPANSION_PATTERN (t);
7807
7808 /* Determine the argument packs that will instantiate the parameter
7809 packs used in the expansion expression. While we're at it,
7810 compute the number of arguments to be expanded and make sure it
7811 is consistent. */
7812 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
7813 pack = TREE_CHAIN (pack))
7814 {
7815 tree parm_pack = TREE_VALUE (pack);
7816 tree arg_pack = NULL_TREE;
7817 tree orig_arg = NULL_TREE;
7818
7819 if (TREE_CODE (parm_pack) == PARM_DECL)
6afcfe0a
JM
7820 {
7821 arg_pack = retrieve_local_specialization (parm_pack);
7822 if (arg_pack == NULL_TREE)
7823 {
7824 /* This can happen for a parameter name used later in a function
7825 declaration (such as in a late-specified return type). Just
7826 make a dummy decl, since it's only used for its type. */
7d882b83 7827 gcc_assert (cp_unevaluated_operand != 0);
6afcfe0a
JM
7828 arg_pack = tsubst_decl (parm_pack, args, complain);
7829 arg_pack = make_fnparm_pack (arg_pack);
7830 }
7831 }
5d80a306
DG
7832 else
7833 {
7834 int level, idx, levels;
7835 template_parm_level_and_index (parm_pack, &level, &idx);
7836
7837 levels = TMPL_ARGS_DEPTH (args);
7838 if (level <= levels)
7839 arg_pack = TMPL_ARG (args, level, idx);
7840 }
7841
7842 orig_arg = arg_pack;
7843 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
7844 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
7845
d393153e
DG
7846 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
7847 /* This can only happen if we forget to expand an argument
7848 pack somewhere else. Just return an error, silently. */
7849 {
7850 result = make_tree_vec (1);
7851 TREE_VEC_ELT (result, 0) = error_mark_node;
7852 return result;
7853 }
7854
88b82314
DG
7855 if (arg_pack
7856 && TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
7857 && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0)))
7858 {
7859 tree expansion = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack), 0);
7860 tree pattern = PACK_EXPANSION_PATTERN (expansion);
7861 if ((TYPE_P (pattern) && same_type_p (pattern, parm_pack))
7862 || (!TYPE_P (pattern) && cp_tree_equal (parm_pack, pattern)))
7863 /* The argument pack that the parameter maps to is just an
7864 expansion of the parameter itself, such as one would
7865 find in the implicit typedef of a class inside the
7866 class itself. Consider this parameter "unsubstituted",
7867 so that we will maintain the outer pack expansion. */
7868 arg_pack = NULL_TREE;
7869 }
7870
5d80a306
DG
7871 if (arg_pack)
7872 {
7873 int my_len =
7874 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
7875
7876 /* It's all-or-nothing with incomplete argument packs. */
7877 if (incomplete && !ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7878 return error_mark_node;
7879
7880 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
7881 incomplete = 1;
7882
7883 if (len < 0)
7884 {
7885 len = my_len;
7886 first_arg_pack = arg_pack;
7887 }
7888 else if (len != my_len)
7889 {
7890 if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
7891 error ("mismatched argument pack lengths while expanding "
7892 "%<%T%>",
7893 pattern);
7894 else
7895 error ("mismatched argument pack lengths while expanding "
7896 "%<%E%>",
7897 pattern);
7898 return error_mark_node;
7899 }
7900
7901 /* Keep track of the parameter packs and their corresponding
7902 argument packs. */
7903 packs = tree_cons (parm_pack, arg_pack, packs);
7904 TREE_TYPE (packs) = orig_arg;
7905 }
7906 else
7907 /* We can't substitute for this parameter pack. */
7908 unsubstituted_packs = tree_cons (TREE_PURPOSE (pack),
7909 TREE_VALUE (pack),
7910 unsubstituted_packs);
7911 }
7912
7913 /* We cannot expand this expansion expression, because we don't have
7914 all of the argument packs we need. Substitute into the pattern
7915 and return a PACK_EXPANSION_*. The caller will need to deal with
7916 that. */
7917 if (unsubstituted_packs)
0db906f1
JM
7918 {
7919 tree new_pat;
7920 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7921 new_pat = tsubst_expr (pattern, args, complain, in_decl,
7922 /*integral_constant_expression_p=*/false);
7923 else
7924 new_pat = tsubst (pattern, args, complain, in_decl);
7925 return make_pack_expansion (new_pat);
7926 }
5d80a306
DG
7927
7928 /* We could not find any argument packs that work. */
7929 if (len < 0)
7930 return error_mark_node;
7931
6afcfe0a
JM
7932 if (!local_specializations)
7933 {
7934 /* We're in a late-specified return type, so we don't have a local
7935 specializations table. Create one for doing this expansion. */
7936 very_local_specializations = true;
7937 local_specializations = htab_create (37,
7938 hash_local_specialization,
7939 eq_local_specializations,
7940 NULL);
7941 }
7942
5d80a306
DG
7943 /* For each argument in each argument pack, substitute into the
7944 pattern. */
7945 result = make_tree_vec (len + incomplete);
7946 for (i = 0; i < len + incomplete; ++i)
7947 {
7948 /* For parameter pack, change the substitution of the parameter
7949 pack to the ith argument in its argument pack, then expand
7950 the pattern. */
7951 for (pack = packs; pack; pack = TREE_CHAIN (pack))
7952 {
7953 tree parm = TREE_PURPOSE (pack);
7954
7955 if (TREE_CODE (parm) == PARM_DECL)
7956 {
7957 /* Select the Ith argument from the pack. */
7958 tree arg = make_node (ARGUMENT_PACK_SELECT);
7959 ARGUMENT_PACK_SELECT_FROM_PACK (arg) = TREE_VALUE (pack);
7960 ARGUMENT_PACK_SELECT_INDEX (arg) = i;
7961 mark_used (parm);
7962 register_local_specialization (arg, parm);
7963 }
7964 else
7965 {
7966 tree value = parm;
7967 int idx, level;
7968 template_parm_level_and_index (parm, &level, &idx);
7969
7970 if (i < len)
7971 {
7972 /* Select the Ith argument from the pack. */
7973 value = make_node (ARGUMENT_PACK_SELECT);
7974 ARGUMENT_PACK_SELECT_FROM_PACK (value) = TREE_VALUE (pack);
7975 ARGUMENT_PACK_SELECT_INDEX (value) = i;
7976 }
7977
7978 /* Update the corresponding argument. */
7979 TMPL_ARG (args, level, idx) = value;
7980 }
7981 }
7982
7983 /* Substitute into the PATTERN with the altered arguments. */
7984 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
7985 TREE_VEC_ELT (result, i) =
7986 tsubst_expr (pattern, args, complain, in_decl,
7987 /*integral_constant_expression_p=*/false);
7988 else
7989 TREE_VEC_ELT (result, i) = tsubst (pattern, args, complain, in_decl);
7990
7991 if (i == len)
7992 /* When we have incomplete argument packs, the last "expanded"
7993 result is itself a pack expansion, which allows us
7994 to deduce more arguments. */
7995 TREE_VEC_ELT (result, i) =
7996 make_pack_expansion (TREE_VEC_ELT (result, i));
7997
7998 if (TREE_VEC_ELT (result, i) == error_mark_node)
7999 {
8000 result = error_mark_node;
8001 break;
8002 }
8003 }
6afcfe0a 8004
5d80a306
DG
8005 /* Update ARGS to restore the substitution from parameter packs to
8006 their argument packs. */
8007 for (pack = packs; pack; pack = TREE_CHAIN (pack))
8008 {
8009 tree parm = TREE_PURPOSE (pack);
8010
8011 if (TREE_CODE (parm) == PARM_DECL)
8012 register_local_specialization (TREE_TYPE (pack), parm);
8013 else
8014 {
8015 int idx, level;
8016 template_parm_level_and_index (parm, &level, &idx);
8017
8018 /* Update the corresponding argument. */
8019 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
8020 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
8021 TREE_TYPE (pack);
8022 else
8023 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
8024 }
8025 }
8026
6afcfe0a
JM
8027 if (very_local_specializations)
8028 {
8029 htab_delete (local_specializations);
8030 local_specializations = NULL;
8031 }
8032
5d80a306 8033 return result;
bd83b409
NS
8034}
8035
a91db711 8036/* Substitute ARGS into the vector or list of template arguments T. */
830bfa74 8037
e9659ab0 8038static tree
a91db711 8039tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
830bfa74 8040{
5d80a306 8041 tree orig_t = t;
bf12d54d 8042 int len = TREE_VEC_LENGTH (t);
5d80a306 8043 int need_new = 0, i, expanded_len_adjust = 0, out;
67f5655f 8044 tree *elts = (tree *) alloca (len * sizeof (tree));
c8094d83 8045
830bfa74
MM
8046 for (i = 0; i < len; i++)
8047 {
bf12d54d
NS
8048 tree orig_arg = TREE_VEC_ELT (t, i);
8049 tree new_arg;
a91db711 8050
bf12d54d
NS
8051 if (TREE_CODE (orig_arg) == TREE_VEC)
8052 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
5d80a306
DG
8053 else if (PACK_EXPANSION_P (orig_arg))
8054 {
8055 /* Substitute into an expansion expression. */
8056 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
8057
8058 if (TREE_CODE (new_arg) == TREE_VEC)
8059 /* Add to the expanded length adjustment the number of
8060 expanded arguments. We subtract one from this
8061 measurement, because the argument pack expression
8062 itself is already counted as 1 in
8063 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
8064 the argument pack is empty. */
8065 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
8066 }
8067 else if (ARGUMENT_PACK_P (orig_arg))
8068 {
8069 /* Substitute into each of the arguments. */
8070 new_arg = make_node (TREE_CODE (orig_arg));
8071
8072 SET_ARGUMENT_PACK_ARGS (
8073 new_arg,
8074 tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
8075 args, complain, in_decl));
8076
8077 if (ARGUMENT_PACK_ARGS (new_arg) == error_mark_node)
8078 new_arg = error_mark_node;
8079
8080 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK) {
8081 TREE_TYPE (new_arg) = tsubst (TREE_TYPE (orig_arg), args,
8082 complain, in_decl);
8083 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
8084
8085 if (TREE_TYPE (new_arg) == error_mark_node)
8086 new_arg = error_mark_node;
8087 }
8088 }
830bfa74 8089 else
a91db711 8090 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
c8094d83 8091
a91db711 8092 if (new_arg == error_mark_node)
08e72a19
JM
8093 return error_mark_node;
8094
a91db711
NS
8095 elts[i] = new_arg;
8096 if (new_arg != orig_arg)
830bfa74
MM
8097 need_new = 1;
8098 }
c8094d83 8099
830bfa74
MM
8100 if (!need_new)
8101 return t;
a91db711 8102
5d80a306
DG
8103 /* Make space for the expanded arguments coming from template
8104 argument packs. */
8105 t = make_tree_vec (len + expanded_len_adjust);
8106 for (i = 0, out = 0; i < len; i++)
8107 {
8108 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
8109 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
8110 && TREE_CODE (elts[i]) == TREE_VEC)
8111 {
8112 int idx;
8113
8114 /* Now expand the template argument pack "in place". */
8115 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
8116 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
8117 }
8118 else
8119 {
8120 TREE_VEC_ELT (t, out) = elts[i];
8121 out++;
8122 }
8123 }
c8094d83 8124
830bfa74
MM
8125 return t;
8126}
8127
36a117a5
MM
8128/* Return the result of substituting ARGS into the template parameters
8129 given by PARMS. If there are m levels of ARGS and m + n levels of
8130 PARMS, then the result will contain n levels of PARMS. For
8131 example, if PARMS is `template <class T> template <class U>
8132 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
8133 result will be `template <int*, double, class V>'. */
8134
e9659ab0 8135static tree
3a978d72 8136tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
36a117a5 8137{
f71f87f9
MM
8138 tree r = NULL_TREE;
8139 tree* new_parms;
36a117a5 8140
8ca4bf25
MM
8141 /* When substituting into a template, we must set
8142 PROCESSING_TEMPLATE_DECL as the template parameters may be
8143 dependent if they are based on one-another, and the dependency
8144 predicates are short-circuit outside of templates. */
8145 ++processing_template_decl;
8146
36a117a5
MM
8147 for (new_parms = &r;
8148 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
8149 new_parms = &(TREE_CHAIN (*new_parms)),
8150 parms = TREE_CHAIN (parms))
8151 {
c8094d83 8152 tree new_vec =
36a117a5
MM
8153 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
8154 int i;
c8094d83 8155
36a117a5
MM
8156 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
8157 {
42b304f1
LM
8158 tree tuple;
8159 tree default_value;
8160 tree parm_decl;
8161
8162 if (parms == error_mark_node)
8163 continue;
8164
8165 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
8166
8167 if (tuple == error_mark_node)
8168 continue;
8169
8170 default_value = TREE_PURPOSE (tuple);
8171 parm_decl = TREE_VALUE (tuple);
833aa4c4
NS
8172
8173 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
a207780f
VR
8174 if (TREE_CODE (parm_decl) == PARM_DECL
8175 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
8176 parm_decl = error_mark_node;
a91db711
NS
8177 default_value = tsubst_template_arg (default_value, args,
8178 complain, NULL_TREE);
c8094d83 8179
a91db711 8180 tuple = build_tree_list (default_value, parm_decl);
833aa4c4 8181 TREE_VEC_ELT (new_vec, i) = tuple;
36a117a5 8182 }
c8094d83
MS
8183
8184 *new_parms =
8185 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
4890c2f4 8186 - TMPL_ARGS_DEPTH (args)),
36a117a5
MM
8187 new_vec, NULL_TREE);
8188 }
8189
8ca4bf25
MM
8190 --processing_template_decl;
8191
36a117a5
MM
8192 return r;
8193}
8194
ed44da02
MM
8195/* Substitute the ARGS into the indicated aggregate (or enumeration)
8196 type T. If T is not an aggregate or enumeration type, it is
8197 handled as if by tsubst. IN_DECL is as for tsubst. If
838dfd8a 8198 ENTERING_SCOPE is nonzero, T is the context for a template which
dc957d14 8199 we are presently tsubst'ing. Return the substituted value. */
36a117a5 8200
e9659ab0 8201static tree
c8094d83 8202tsubst_aggr_type (tree t,
0cbd7506
MS
8203 tree args,
8204 tsubst_flags_t complain,
8205 tree in_decl,
8206 int entering_scope)
36a117a5
MM
8207{
8208 if (t == NULL_TREE)
8209 return NULL_TREE;
8210
8211 switch (TREE_CODE (t))
8212 {
8213 case RECORD_TYPE:
8214 if (TYPE_PTRMEMFUNC_P (t))
46c895ac 8215 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
36a117a5 8216
f4f206f4 8217 /* Else fall through. */
ed44da02 8218 case ENUMERAL_TYPE:
36a117a5 8219 case UNION_TYPE:
82390eb6 8220 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
36a117a5
MM
8221 {
8222 tree argvec;
8223 tree context;
8224 tree r;
7d882b83
ILT
8225 int saved_unevaluated_operand;
8226 int saved_inhibit_evaluation_warnings;
3489ea0c
MM
8227
8228 /* In "sizeof(X<I>)" we need to evaluate "I". */
7d882b83
ILT
8229 saved_unevaluated_operand = cp_unevaluated_operand;
8230 cp_unevaluated_operand = 0;
8231 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8232 c_inhibit_evaluation_warnings = 0;
36a117a5
MM
8233
8234 /* First, determine the context for the type we are looking
8235 up. */
4f7847ca
NS
8236 context = TYPE_CONTEXT (t);
8237 if (context)
29b0d1fd
JM
8238 {
8239 context = tsubst_aggr_type (context, args, complain,
8240 in_decl, /*entering_scope=*/1);
8241 /* If context is a nested class inside a class template,
8242 it may still need to be instantiated (c++/33959). */
8243 if (TYPE_P (context))
8244 context = complete_type (context);
8245 }
36a117a5
MM
8246
8247 /* Then, figure out what arguments are appropriate for the
8248 type we are trying to find. For example, given:
8249
8250 template <class T> struct S;
8251 template <class T, class U> void f(T, U) { S<U> su; }
8252
8253 and supposing that we are instantiating f<int, double>,
8254 then our ARGS will be {int, double}, but, when looking up
8255 S we only want {double}. */
a91db711
NS
8256 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
8257 complain, in_decl);
08e72a19 8258 if (argvec == error_mark_node)
3489ea0c
MM
8259 r = error_mark_node;
8260 else
8261 {
8262 r = lookup_template_class (t, argvec, in_decl, context,
8263 entering_scope, complain);
8264 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
8265 }
3db45ab5 8266
7d882b83
ILT
8267 cp_unevaluated_operand = saved_unevaluated_operand;
8268 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
36a117a5 8269
3489ea0c 8270 return r;
36a117a5 8271 }
c8094d83 8272 else
36a117a5
MM
8273 /* This is not a template type, so there's nothing to do. */
8274 return t;
8275
8276 default:
4393e105 8277 return tsubst (t, args, complain, in_decl);
36a117a5
MM
8278 }
8279}
8280
9188c363
MM
8281/* Substitute into the default argument ARG (a default argument for
8282 FN), which has the indicated TYPE. */
8283
8284tree
3a978d72 8285tsubst_default_argument (tree fn, tree type, tree arg)
9188c363 8286{
2436b51f
MM
8287 tree saved_class_ptr = NULL_TREE;
8288 tree saved_class_ref = NULL_TREE;
8289
9188c363
MM
8290 /* This default argument came from a template. Instantiate the
8291 default argument here, not in tsubst. In the case of
c8094d83
MS
8292 something like:
8293
9188c363
MM
8294 template <class T>
8295 struct S {
8296 static T t();
8297 void f(T = t());
8298 };
c8094d83 8299
9188c363 8300 we must be careful to do name lookup in the scope of S<T>,
d5a10cf0 8301 rather than in the current class. */
2b59fc25 8302 push_access_scope (fn);
2436b51f
MM
8303 /* The "this" pointer is not valid in a default argument. */
8304 if (cfun)
8305 {
8306 saved_class_ptr = current_class_ptr;
8307 cp_function_chain->x_current_class_ptr = NULL_TREE;
8308 saved_class_ref = current_class_ref;
8309 cp_function_chain->x_current_class_ref = NULL_TREE;
8310 }
9188c363 8311
d5a10cf0 8312 push_deferring_access_checks(dk_no_deferred);
5a8613b2
MM
8313 /* The default argument expression may cause implicitly defined
8314 member functions to be synthesized, which will result in garbage
8315 collection. We must treat this situation as if we were within
8316 the body of function so as to avoid collecting live data on the
8317 stack. */
8318 ++function_depth;
c2ea3a40 8319 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
015c2c66
MM
8320 tf_warning_or_error, NULL_TREE,
8321 /*integral_constant_expression_p=*/false);
5a8613b2 8322 --function_depth;
d5a10cf0
MM
8323 pop_deferring_access_checks();
8324
2436b51f
MM
8325 /* Restore the "this" pointer. */
8326 if (cfun)
8327 {
8328 cp_function_chain->x_current_class_ptr = saved_class_ptr;
8329 cp_function_chain->x_current_class_ref = saved_class_ref;
8330 }
8331
9188c363
MM
8332 /* Make sure the default argument is reasonable. */
8333 arg = check_default_argument (type, arg);
8334
3693f266
DS
8335 pop_access_scope (fn);
8336
9188c363
MM
8337 return arg;
8338}
8339
8340/* Substitute into all the default arguments for FN. */
8341
8342static void
3a978d72 8343tsubst_default_arguments (tree fn)
9188c363
MM
8344{
8345 tree arg;
8346 tree tmpl_args;
8347
8348 tmpl_args = DECL_TI_ARGS (fn);
8349
8350 /* If this function is not yet instantiated, we certainly don't need
8351 its default arguments. */
8352 if (uses_template_parms (tmpl_args))
8353 return;
8354
c8094d83
MS
8355 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
8356 arg;
9188c363
MM
8357 arg = TREE_CHAIN (arg))
8358 if (TREE_PURPOSE (arg))
c8094d83 8359 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9188c363
MM
8360 TREE_VALUE (arg),
8361 TREE_PURPOSE (arg));
8362}
8363
fc6a28d7
MM
8364/* Substitute the ARGS into the T, which is a _DECL. Return the
8365 result of the substitution. Issue error and warning messages under
8366 control of COMPLAIN. */
00d3396f 8367
e9659ab0 8368static tree
fc6a28d7 8369tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8d08fdba 8370{
82a98427 8371 location_t saved_loc;
b370501f 8372 tree r = NULL_TREE;
4b2811e9 8373 tree in_decl = t;
2b110bfc 8374 hashval_t hash = 0;
830bfa74 8375
ae58fa02 8376 /* Set the filename and linenumber to improve error-reporting. */
82a98427 8377 saved_loc = input_location;
f31686a3 8378 input_location = DECL_SOURCE_LOCATION (t);
b7484fbe 8379
8d08fdba
MS
8380 switch (TREE_CODE (t))
8381 {
98c1c668
JM
8382 case TEMPLATE_DECL:
8383 {
28e42b7e 8384 /* We can get here when processing a member function template,
438c0fa8 8385 member class template, or template template parameter. */
98c1c668 8386 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 8387 tree spec;
28e42b7e
KL
8388 tree tmpl_args;
8389 tree full_args;
98c1c668 8390
28e42b7e 8391 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
db2767b6 8392 {
28e42b7e
KL
8393 /* Template template parameter is treated here. */
8394 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8395 if (new_type == error_mark_node)
8396 return error_mark_node;
36a117a5 8397
28e42b7e
KL
8398 r = copy_decl (t);
8399 TREE_CHAIN (r) = NULL_TREE;
8400 TREE_TYPE (r) = new_type;
8401 DECL_TEMPLATE_RESULT (r)
c2255bc4
AH
8402 = build_decl (DECL_SOURCE_LOCATION (decl),
8403 TYPE_DECL, DECL_NAME (decl), new_type);
c8094d83 8404 DECL_TEMPLATE_PARMS (r)
28e42b7e
KL
8405 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
8406 complain);
8407 TYPE_NAME (new_type) = r;
8408 break;
8409 }
36a117a5 8410
28e42b7e
KL
8411 /* We might already have an instance of this template.
8412 The ARGS are for the surrounding class type, so the
8413 full args contain the tsubst'd args for the context,
8414 plus the innermost args from the template decl. */
c8094d83 8415 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
28e42b7e
KL
8416 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
8417 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8ca4bf25
MM
8418 /* Because this is a template, the arguments will still be
8419 dependent, even after substitution. If
8420 PROCESSING_TEMPLATE_DECL is not set, the dependency
8421 predicates will short-circuit. */
8422 ++processing_template_decl;
28e42b7e
KL
8423 full_args = tsubst_template_args (tmpl_args, args,
8424 complain, in_decl);
8ca4bf25 8425 --processing_template_decl;
bf2f7328
VR
8426 if (full_args == error_mark_node)
8427 return error_mark_node;
28e42b7e 8428
438c0fa8
JM
8429 /* If this is a default template template argument,
8430 tsubst might not have changed anything. */
8431 if (full_args == tmpl_args)
8432 return t;
28e42b7e 8433
2b110bfc
JM
8434 hash = hash_tmpl_and_args (t, full_args);
8435 spec = retrieve_specialization (t, full_args, hash);
28e42b7e
KL
8436 if (spec != NULL_TREE)
8437 {
8438 r = spec;
8439 break;
db2767b6 8440 }
98c1c668
JM
8441
8442 /* Make a new template decl. It will be similar to the
c8094d83 8443 original, but will record the current template arguments.
98c1c668
JM
8444 We also create a new function declaration, which is just
8445 like the old one, but points to this new template, rather
8446 than the old one. */
0acf7199 8447 r = copy_decl (t);
50bc768d 8448 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
ae58fa02 8449 TREE_CHAIN (r) = NULL_TREE;
db2767b6 8450
ae58fa02 8451 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
8452
8453 if (TREE_CODE (decl) == TYPE_DECL)
8454 {
8ca4bf25
MM
8455 tree new_type;
8456 ++processing_template_decl;
8457 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
3db45ab5 8458 --processing_template_decl;
2620d095
KL
8459 if (new_type == error_mark_node)
8460 return error_mark_node;
8461
ae58fa02
MM
8462 TREE_TYPE (r) = new_type;
8463 CLASSTYPE_TI_TEMPLATE (new_type) = r;
17aec3eb 8464 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
ae58fa02 8465 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8ca4bf25 8466 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
93cdc044
JM
8467 }
8468 else
8469 {
8ca4bf25
MM
8470 tree new_decl;
8471 ++processing_template_decl;
8472 new_decl = tsubst (decl, args, complain, in_decl);
8473 --processing_template_decl;
caec1dc0
KL
8474 if (new_decl == error_mark_node)
8475 return error_mark_node;
17aec3eb
RK
8476
8477 DECL_TEMPLATE_RESULT (r) = new_decl;
ae58fa02
MM
8478 DECL_TI_TEMPLATE (new_decl) = r;
8479 TREE_TYPE (r) = TREE_TYPE (new_decl);
8480 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
3db45ab5 8481 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
93cdc044
JM
8482 }
8483
ae58fa02
MM
8484 SET_DECL_IMPLICIT_INSTANTIATION (r);
8485 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
8486 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
8487
8488 /* The template parameters for this new template are all the
8489 template parameters for the old template, except the
c6002625 8490 outermost level of parameters. */
c8094d83 8491 DECL_TEMPLATE_PARMS (r)
4393e105 8492 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
c2ea3a40 8493 complain);
98c1c668 8494
93cdc044 8495 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 8496 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 8497
8c6ab2db
NS
8498 if (TREE_CODE (decl) != TYPE_DECL)
8499 /* Record this non-type partial instantiation. */
c8094d83 8500 register_specialization (r, t,
d63d5d0c 8501 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
2b110bfc 8502 false, hash);
98c1c668 8503 }
ae58fa02 8504 break;
8d08fdba
MS
8505
8506 case FUNCTION_DECL:
8507 {
386b8a85 8508 tree ctx;
87603ed0 8509 tree argvec = NULL_TREE;
cf38f48a 8510 tree *friends;
36a117a5 8511 tree gen_tmpl;
fc6a28d7 8512 tree type;
5566b478 8513 int member;
d8c4447d
MM
8514 int args_depth;
8515 int parms_depth;
5566b478 8516
36a117a5 8517 /* Nobody should be tsubst'ing into non-template functions. */
50bc768d 8518 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
36a117a5
MM
8519
8520 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
8521 {
8522 tree spec;
00cf3e31
MM
8523 bool dependent_p;
8524
8525 /* If T is not dependent, just return it. We have to
8526 increment PROCESSING_TEMPLATE_DECL because
8527 value_dependent_expression_p assumes that nothing is
8528 dependent when PROCESSING_TEMPLATE_DECL is zero. */
8529 ++processing_template_decl;
8530 dependent_p = value_dependent_expression_p (t);
8531 --processing_template_decl;
8532 if (!dependent_p)
8533 return t;
36a117a5
MM
8534
8535 /* Calculate the most general template of which R is a
8536 specialization, and the complete set of arguments used to
8537 specialize R. */
8538 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
c8094d83 8539 argvec = tsubst_template_args (DECL_TI_ARGS
a91db711 8540 (DECL_TEMPLATE_RESULT (gen_tmpl)),
c8094d83 8541 args, complain, in_decl);
36a117a5
MM
8542
8543 /* Check to see if we already have this specialization. */
2b110bfc
JM
8544 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8545 spec = retrieve_specialization (gen_tmpl, argvec, hash);
7ddedda4 8546
36a117a5 8547 if (spec)
ae58fa02
MM
8548 {
8549 r = spec;
8550 break;
8551 }
d8c4447d 8552
f9a7ae04
MM
8553 /* We can see more levels of arguments than parameters if
8554 there was a specialization of a member template, like
8555 this:
8556
0cbd7506 8557 template <class T> struct S { template <class U> void f(); }
c8094d83 8558 template <> template <class U> void S<int>::f(U);
f9a7ae04 8559
dc957d14 8560 Here, we'll be substituting into the specialization,
f9a7ae04
MM
8561 because that's where we can find the code we actually
8562 want to generate, but we'll have enough arguments for
c8094d83 8563 the most general template.
f9a7ae04
MM
8564
8565 We also deal with the peculiar case:
d8c4447d 8566
c8094d83 8567 template <class T> struct S {
d8c4447d
MM
8568 template <class U> friend void f();
8569 };
74b846e0 8570 template <class U> void f() {}
d8c4447d
MM
8571 template S<int>;
8572 template void f<double>();
8573
8574 Here, the ARGS for the instantiation of will be {int,
8575 double}. But, we only need as many ARGS as there are
8576 levels of template parameters in CODE_PATTERN. We are
8577 careful not to get fooled into reducing the ARGS in
8578 situations like:
8579
8580 template <class T> struct S { template <class U> void f(U); }
8581 template <class T> template <> void S<T>::f(int) {}
8582
8583 which we can spot because the pattern will be a
8584 specialization in this case. */
8585 args_depth = TMPL_ARGS_DEPTH (args);
c8094d83
MS
8586 parms_depth =
8587 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
d8c4447d
MM
8588 if (args_depth > parms_depth
8589 && !DECL_TEMPLATE_SPECIALIZATION (t))
f9a7ae04 8590 args = get_innermost_template_args (args, parms_depth);
36a117a5
MM
8591 }
8592 else
8593 {
8594 /* This special case arises when we have something like this:
8595
0cbd7506 8596 template <class T> struct S {
c8094d83 8597 friend void f<int>(int, double);
36a117a5
MM
8598 };
8599
8600 Here, the DECL_TI_TEMPLATE for the friend declaration
10b1d5e7
MM
8601 will be an IDENTIFIER_NODE. We are being called from
8602 tsubst_friend_function, and we want only to create a
8603 new decl (R) with appropriate types so that we can call
8604 determine_specialization. */
36a117a5
MM
8605 gen_tmpl = NULL_TREE;
8606 }
8607
6eb3bb27 8608 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 8609 {
5566b478
MS
8610 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
8611 member = 2;
8612 else
8613 member = 1;
c8094d83 8614 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8c6ab2db 8615 complain, t, /*entering_scope=*/1);
5566b478
MS
8616 }
8617 else
8618 {
8619 member = 0;
4f1c5b7d 8620 ctx = DECL_CONTEXT (t);
5566b478 8621 }
fc6a28d7 8622 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
b3da7bb1
MM
8623 if (type == error_mark_node)
8624 return error_mark_node;
8d08fdba 8625
5566b478 8626 /* We do NOT check for matching decls pushed separately at this
0cbd7506
MS
8627 point, as they may not represent instantiations of this
8628 template, and in any case are considered separate under the
8629 discrete model. */
0acf7199 8630 r = copy_decl (t);
e1467ff2 8631 DECL_USE_TEMPLATE (r) = 0;
5566b478 8632 TREE_TYPE (r) = type;
92643fea
MM
8633 /* Clear out the mangled name and RTL for the instantiation. */
8634 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
8635 SET_DECL_RTL (r, NULL_RTX);
9b26c96e
JM
8636 /* Leave DECL_INITIAL set on deleted instantiations. */
8637 if (!DECL_DELETED_FN (r))
8638 DECL_INITIAL (r) = NULL_TREE;
4f1c5b7d 8639 DECL_CONTEXT (r) = ctx;
5566b478 8640
c8094d83 8641 if (member && DECL_CONV_FN_P (r))
1f84ec23
MM
8642 /* Type-conversion operator. Reconstruct the name, in
8643 case it's the name of one of the template's parameters. */
8644 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5566b478 8645
4393e105 8646 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
c2ea3a40 8647 complain, t);
477f6664 8648 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
8649
8650 TREE_STATIC (r) = 0;
8651 TREE_PUBLIC (r) = TREE_PUBLIC (t);
8652 DECL_EXTERNAL (r) = 1;
4684cd27
MM
8653 /* If this is an instantiation of a function with internal
8654 linkage, we already know what object file linkage will be
8655 assigned to the instantiation. */
8656 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
711734a9
JM
8657 DECL_DEFER_OUTPUT (r) = 0;
8658 TREE_CHAIN (r) = NULL_TREE;
8659 DECL_PENDING_INLINE_INFO (r) = 0;
59026e79 8660 DECL_PENDING_INLINE_P (r) = 0;
655dc6ee 8661 DECL_SAVED_TREE (r) = NULL_TREE;
29d7f409 8662 DECL_STRUCT_FUNCTION (r) = NULL;
711734a9 8663 TREE_USED (r) = 0;
b97e8a14
JM
8664 /* We'll re-clone as appropriate in instantiate_template. */
8665 DECL_CLONED_FUNCTION (r) = NULL_TREE;
711734a9 8666
92643fea
MM
8667 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
8668 this in the special friend case mentioned above where
8669 GEN_TMPL is NULL. */
36a117a5 8670 if (gen_tmpl)
386b8a85 8671 {
c8094d83 8672 DECL_TEMPLATE_INFO (r)
e1b3e07d 8673 = tree_cons (gen_tmpl, argvec, NULL_TREE);
36a117a5 8674 SET_DECL_IMPLICIT_INSTANTIATION (r);
2b110bfc 8675 register_specialization (r, gen_tmpl, argvec, false, hash);
36a117a5 8676
9188c363
MM
8677 /* We're not supposed to instantiate default arguments
8678 until they are called, for a template. But, for a
8679 declaration like:
8680
0cbd7506
MS
8681 template <class T> void f ()
8682 { extern void g(int i = T()); }
c8094d83 8683
9188c363
MM
8684 we should do the substitution when the template is
8685 instantiated. We handle the member function case in
8686 instantiate_class_template since the default arguments
8687 might refer to other members of the class. */
8688 if (!member
8689 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8690 && !uses_template_parms (argvec))
8691 tsubst_default_arguments (r);
386b8a85 8692 }
c43e95f8
MM
8693 else
8694 DECL_TEMPLATE_INFO (r) = NULL_TREE;
f181d4ae 8695
cf38f48a
MM
8696 /* Copy the list of befriending classes. */
8697 for (friends = &DECL_BEFRIENDING_CLASSES (r);
8698 *friends;
c8094d83 8699 friends = &TREE_CHAIN (*friends))
cf38f48a
MM
8700 {
8701 *friends = copy_node (*friends);
8702 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
c2ea3a40 8703 args, complain,
cf38f48a
MM
8704 in_decl);
8705 }
8706
212e7048 8707 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
f181d4ae
MM
8708 {
8709 maybe_retrofit_in_chrg (r);
212e7048
MM
8710 if (DECL_CONSTRUCTOR_P (r))
8711 grok_ctor_properties (ctx, r);
2be678ff
JM
8712 /* If this is an instantiation of a member template, clone it.
8713 If it isn't, that'll be handled by
8714 clone_constructors_and_destructors. */
5e818b93 8715 if (PRIMARY_TEMPLATE_P (gen_tmpl))
271e6f02 8716 clone_function_decl (r, /*update_method_vec_p=*/0);
f181d4ae 8717 }
398cd199
VR
8718 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
8719 && !grok_op_properties (r, (complain & tf_error) != 0))
8720 return error_mark_node;
c8b2e872
MM
8721
8722 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
8723 SET_DECL_FRIEND_CONTEXT (r,
8724 tsubst (DECL_FRIEND_CONTEXT (t),
8725 args, complain, in_decl));
b9e75696
JM
8726
8727 /* Possibly limit visibility based on template args. */
8728 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
b70f0f48
JM
8729 if (DECL_VISIBILITY_SPECIFIED (t))
8730 {
8731 DECL_VISIBILITY_SPECIFIED (r) = 0;
8732 DECL_ATTRIBUTES (r)
8733 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
8734 }
b9e75696 8735 determine_visibility (r);
4f4141ff
JM
8736
8737 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8738 args, complain, in_decl);
8d08fdba 8739 }
ae58fa02 8740 break;
8d08fdba
MS
8741
8742 case PARM_DECL:
8743 {
5d80a306
DG
8744 tree type = NULL_TREE;
8745 int i, len = 1;
8746 tree expanded_types = NULL_TREE;
8747 tree prev_r = NULL_TREE;
8748 tree first_r = NULL_TREE;
1b8899d1 8749
5d80a306
DG
8750 if (FUNCTION_PARAMETER_PACK_P (t))
8751 {
8752 /* If there is a local specialization that isn't a
8753 parameter pack, it means that we're doing a "simple"
8754 substitution from inside tsubst_pack_expansion. Just
a757b5c5 8755 return the local specialization (which will be a single
5d80a306 8756 parm). */
29b0d1fd 8757 tree spec = retrieve_local_specialization (t);
5d80a306
DG
8758 if (spec
8759 && TREE_CODE (spec) == PARM_DECL
8760 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
8761 return spec;
8762
8763 /* Expand the TYPE_PACK_EXPANSION that provides the types for
8764 the parameters in this function parameter pack. */
8765 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
8766 complain, in_decl);
8767 if (TREE_CODE (expanded_types) == TREE_VEC)
8768 {
8769 len = TREE_VEC_LENGTH (expanded_types);
8770
8771 /* Zero-length parameter packs are boring. Just substitute
8772 into the chain. */
8773 if (len == 0)
8774 return tsubst (TREE_CHAIN (t), args, complain,
8775 TREE_CHAIN (t));
8776 }
8777 else
8778 {
8779 /* All we did was update the type. Make a note of that. */
8780 type = expanded_types;
8781 expanded_types = NULL_TREE;
8782 }
8783 }
db2767b6 8784
5d80a306
DG
8785 /* Loop through all of the parameter's we'll build. When T is
8786 a function parameter pack, LEN is the number of expanded
8787 types in EXPANDED_TYPES; otherwise, LEN is 1. */
8788 r = NULL_TREE;
8789 for (i = 0; i < len; ++i)
8790 {
8791 prev_r = r;
8792 r = copy_node (t);
8793 if (DECL_TEMPLATE_PARM_P (t))
8794 SET_DECL_TEMPLATE_PARM_P (r);
8795
8796 if (expanded_types)
8797 /* We're on the Ith parameter of the function parameter
8798 pack. */
8799 {
8800 /* Get the Ith type. */
8801 type = TREE_VEC_ELT (expanded_types, i);
8802
8803 if (DECL_NAME (r))
8804 /* Rename the parameter to include the index. */
8805 DECL_NAME (r) =
8806 make_ith_pack_parameter_name (DECL_NAME (r), i);
8807 }
8808 else if (!type)
8809 /* We're dealing with a normal parameter. */
8810 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8811
8812 type = type_decays_to (type);
8813 TREE_TYPE (r) = type;
8814 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
8815
8816 if (DECL_INITIAL (r))
8817 {
8818 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
8819 DECL_INITIAL (r) = TREE_TYPE (r);
8820 else
8821 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
8822 complain, in_decl);
8823 }
8824
8825 DECL_CONTEXT (r) = NULL_TREE;
8826
8827 if (!DECL_TEMPLATE_PARM_P (r))
8828 DECL_ARG_TYPE (r) = type_passed_as (type);
8829
4f4141ff
JM
8830 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8831 args, complain, in_decl);
8832
5d80a306
DG
8833 /* Keep track of the first new parameter we
8834 generate. That's what will be returned to the
8835 caller. */
8836 if (!first_r)
8837 first_r = r;
8838
8839 /* Build a proper chain of parameters when substituting
8840 into a function parameter pack. */
8841 if (prev_r)
8842 TREE_CHAIN (prev_r) = r;
8843 }
8e51619a 8844
8d08fdba 8845 if (TREE_CHAIN (t))
4393e105 8846 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
c2ea3a40 8847 complain, TREE_CHAIN (t));
5d80a306
DG
8848
8849 /* FIRST_R contains the start of the chain we've built. */
8850 r = first_r;
8d08fdba 8851 }
ae58fa02 8852 break;
8d08fdba 8853
5566b478
MS
8854 case FIELD_DECL:
8855 {
fc6a28d7
MM
8856 tree type;
8857
0acf7199 8858 r = copy_decl (t);
fc6a28d7
MM
8859 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8860 if (type == error_mark_node)
8861 return error_mark_node;
1b8899d1 8862 TREE_TYPE (r) = type;
9804209d 8863 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1 8864
015c2c66
MM
8865 /* DECL_INITIAL gives the number of bits in a bit-field. */
8866 DECL_INITIAL (r)
8867 = tsubst_expr (DECL_INITIAL (t), args,
8868 complain, in_decl,
8869 /*integral_constant_expression_p=*/true);
1b8899d1
MM
8870 /* We don't have to set DECL_CONTEXT here; it is set by
8871 finish_member_declaration. */
5566b478 8872 TREE_CHAIN (r) = NULL_TREE;
c8094d83 8873 if (VOID_TYPE_P (type))
dee15844 8874 error ("instantiation of %q+D as type %qT", r, type);
4f4141ff
JM
8875
8876 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
8877 args, complain, in_decl);
5566b478 8878 }
ae58fa02 8879 break;
5566b478
MS
8880
8881 case USING_DECL:
98ed9dae
NS
8882 /* We reach here only for member using decls. */
8883 if (DECL_DEPENDENT_P (t))
8884 {
8885 r = do_class_using_decl
8886 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
8887 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
8888 if (!r)
8889 r = error_mark_node;
db8470f6
JM
8890 else
8891 {
8892 TREE_PROTECTED (r) = TREE_PROTECTED (t);
8893 TREE_PRIVATE (r) = TREE_PRIVATE (t);
8894 }
98ed9dae
NS
8895 }
8896 else
8897 {
8898 r = copy_node (t);
8899 TREE_CHAIN (r) = NULL_TREE;
8900 }
ae58fa02 8901 break;
5566b478 8902
9188c363 8903 case TYPE_DECL:
5566b478
MS
8904 case VAR_DECL:
8905 {
1cea0434
KG
8906 tree argvec = NULL_TREE;
8907 tree gen_tmpl = NULL_TREE;
36a117a5 8908 tree spec;
1cea0434 8909 tree tmpl = NULL_TREE;
9188c363 8910 tree ctx;
fc6a28d7 8911 tree type = NULL_TREE;
f604fc3b 8912 bool local_p;
9188c363 8913
56d0c6e3 8914 if (TREE_CODE (t) == TYPE_DECL
a7f6bc8c 8915 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
fc6a28d7 8916 {
a7f6bc8c
JM
8917 /* If this is the canonical decl, we don't have to
8918 mess with instantiations, and often we can't (for
8919 typename, template type parms and such). Note that
8920 TYPE_NAME is not correct for the above test if
8921 we've copied the type for a typedef. */
fc6a28d7 8922 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
c343d5a7
PC
8923 if (type == error_mark_node)
8924 return error_mark_node;
a7f6bc8c
JM
8925 r = TYPE_NAME (type);
8926 break;
fc6a28d7 8927 }
c8094d83 8928
f604fc3b
MM
8929 /* Check to see if we already have the specialization we
8930 need. */
8931 spec = NULL_TREE;
8932 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
8933 {
8934 /* T is a static data member or namespace-scope entity.
8935 We have to substitute into namespace-scope variables
8936 (even though such entities are never templates) because
8937 of cases like:
8938
8939 template <class T> void f() { extern T t; }
8940
8941 where the entity referenced is not known until
8942 instantiation time. */
8943 local_p = false;
8944 ctx = DECL_CONTEXT (t);
8945 if (DECL_CLASS_SCOPE_P (t))
8946 {
8947 ctx = tsubst_aggr_type (ctx, args,
8948 complain,
8949 in_decl, /*entering_scope=*/1);
8950 /* If CTX is unchanged, then T is in fact the
8951 specialization we want. That situation occurs when
8952 referencing a static data member within in its own
8953 class. We can use pointer equality, rather than
8954 same_type_p, because DECL_CONTEXT is always
8955 canonical. */
8956 if (ctx == DECL_CONTEXT (t))
8957 spec = t;
8958 }
5566b478 8959
f604fc3b
MM
8960 if (!spec)
8961 {
8962 tmpl = DECL_TI_TEMPLATE (t);
8963 gen_tmpl = most_general_template (tmpl);
8964 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
2b110bfc
JM
8965 hash = hash_tmpl_and_args (gen_tmpl, argvec);
8966 spec = retrieve_specialization (gen_tmpl, argvec, hash);
f604fc3b
MM
8967 }
8968 }
9188c363 8969 else
6dfbb909 8970 {
f604fc3b
MM
8971 /* A local variable. */
8972 local_p = true;
6dfbb909
MM
8973 /* Subsequent calls to pushdecl will fill this in. */
8974 ctx = NULL_TREE;
f604fc3b 8975 spec = retrieve_local_specialization (t);
6dfbb909 8976 }
f604fc3b
MM
8977 /* If we already have the specialization we need, there is
8978 nothing more to do. */
36a117a5 8979 if (spec)
ae58fa02
MM
8980 {
8981 r = spec;
8982 break;
8983 }
5566b478 8984
f604fc3b 8985 /* Create a new node for the specialization we need. */
0acf7199 8986 r = copy_decl (t);
56d0c6e3
JM
8987 if (type == NULL_TREE)
8988 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
edebf865 8989 if (TREE_CODE (r) == VAR_DECL)
39703eb9 8990 {
8b0a8500
MM
8991 /* Even if the original location is out of scope, the
8992 newly substituted one is not. */
8993 DECL_DEAD_FOR_LOCAL (r) = 0;
8994 DECL_INITIALIZED_P (r) = 0;
8995 DECL_TEMPLATE_INSTANTIATED (r) = 0;
fc6a28d7
MM
8996 if (type == error_mark_node)
8997 return error_mark_node;
2c05d05e
MM
8998 if (TREE_CODE (type) == FUNCTION_TYPE)
8999 {
9000 /* It may seem that this case cannot occur, since:
9001
9002 typedef void f();
9003 void g() { f x; }
9004
9005 declares a function, not a variable. However:
9006
9007 typedef void f();
9008 template <typename T> void g() { T t; }
9009 template void g<f>();
9010
9011 is an attempt to declare a variable with function
9012 type. */
9013 error ("variable %qD has function type",
9014 /* R is not yet sufficiently initialized, so we
9015 just use its name. */
9016 DECL_NAME (r));
9017 return error_mark_node;
9018 }
39703eb9
MM
9019 type = complete_type (type);
9020 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
9021 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
62e19030 9022 type = check_var_type (DECL_NAME (r), type);
56b4ea3d
RH
9023
9024 if (DECL_HAS_VALUE_EXPR_P (t))
9025 {
9026 tree ve = DECL_VALUE_EXPR (t);
015c2c66
MM
9027 ve = tsubst_expr (ve, args, complain, in_decl,
9028 /*constant_expression_p=*/false);
3db45ab5 9029 SET_DECL_VALUE_EXPR (r, ve);
56b4ea3d 9030 }
39703eb9 9031 }
a3d87771
MM
9032 else if (DECL_SELF_REFERENCE_P (t))
9033 SET_DECL_SELF_REFERENCE_P (r);
01f4137f 9034 TREE_TYPE (r) = type;
9804209d 9035 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
5566b478 9036 DECL_CONTEXT (r) = ctx;
92643fea
MM
9037 /* Clear out the mangled name and RTL for the instantiation. */
9038 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
820cc88f
DB
9039 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9040 SET_DECL_RTL (r, NULL_RTX);
8b0a8500
MM
9041 /* The initializer must not be expanded until it is required;
9042 see [temp.inst]. */
d11ad92e 9043 DECL_INITIAL (r) = NULL_TREE;
820cc88f
DB
9044 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
9045 SET_DECL_RTL (r, NULL_RTX);
06ceef4e 9046 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
b9e75696
JM
9047 if (TREE_CODE (r) == VAR_DECL)
9048 {
9049 /* Possibly limit visibility based on template args. */
9050 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
b70f0f48
JM
9051 if (DECL_VISIBILITY_SPECIFIED (t))
9052 {
9053 DECL_VISIBILITY_SPECIFIED (r) = 0;
9054 DECL_ATTRIBUTES (r)
9055 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
9056 }
b9e75696
JM
9057 determine_visibility (r);
9058 }
5044ab0e
JM
9059 /* Preserve a typedef that names a type. */
9060 else if (TREE_CODE (r) == TYPE_DECL
9061 && DECL_ORIGINAL_TYPE (t)
9062 && type != error_mark_node)
9063 {
9064 DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
9065 args, complain, in_decl);
9066 TREE_TYPE (r) = type = build_variant_type_copy (type);
9067 TYPE_NAME (type) = r;
9068 }
5566b478 9069
6dfbb909
MM
9070 if (!local_p)
9071 {
9072 /* A static data member declaration is always marked
9073 external when it is declared in-class, even if an
9074 initializer is present. We mimic the non-template
9075 processing here. */
9076 DECL_EXTERNAL (r) = 1;
fa8d6e85 9077
2b110bfc 9078 register_specialization (r, gen_tmpl, argvec, false, hash);
6dfbb909
MM
9079 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
9080 SET_DECL_IMPLICIT_INSTANTIATION (r);
9081 }
9188c363 9082 else
6dfbb909 9083 register_local_specialization (r, t);
5566b478 9084
5566b478 9085 TREE_CHAIN (r) = NULL_TREE;
4f4141ff 9086
5044ab0e
JM
9087 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
9088 (int) ATTR_FLAG_TYPE_IN_PLACE,
4f4141ff 9089 args, complain, in_decl);
edebf865 9090 layout_decl (r, 0);
5566b478 9091 }
ae58fa02 9092 break;
5566b478 9093
ae58fa02 9094 default:
315fb5db 9095 gcc_unreachable ();
c8094d83 9096 }
ae58fa02
MM
9097
9098 /* Restore the file and line information. */
82a98427 9099 input_location = saved_loc;
ae58fa02
MM
9100
9101 return r;
9102}
9103
34cd5ae7 9104/* Substitute into the ARG_TYPES of a function type. */
cabc336a 9105
e9659ab0 9106static tree
c8094d83 9107tsubst_arg_types (tree arg_types,
0cbd7506
MS
9108 tree args,
9109 tsubst_flags_t complain,
9110 tree in_decl)
cabc336a
MM
9111{
9112 tree remaining_arg_types;
5d80a306
DG
9113 tree type = NULL_TREE;
9114 int i = 1;
9115 tree expanded_args = NULL_TREE;
5e97d404 9116 tree default_arg;
cabc336a
MM
9117
9118 if (!arg_types || arg_types == void_list_node)
9119 return arg_types;
c8094d83 9120
cabc336a 9121 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
4393e105
MM
9122 args, complain, in_decl);
9123 if (remaining_arg_types == error_mark_node)
9124 return error_mark_node;
9125
5d80a306 9126 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
4b2811e9 9127 {
5d80a306
DG
9128 /* For a pack expansion, perform substitution on the
9129 entire expression. Later on, we'll handle the arguments
9130 one-by-one. */
9131 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
9132 args, complain, in_decl);
cabc336a 9133
5d80a306
DG
9134 if (TREE_CODE (expanded_args) == TREE_VEC)
9135 /* So that we'll spin through the parameters, one by one. */
9136 i = TREE_VEC_LENGTH (expanded_args);
9137 else
9138 {
9139 /* We only partially substituted into the parameter
9140 pack. Our type is TYPE_PACK_EXPANSION. */
9141 type = expanded_args;
9142 expanded_args = NULL_TREE;
9143 }
9144 }
cabc336a 9145
5d80a306
DG
9146 while (i > 0) {
9147 --i;
9148
9149 if (expanded_args)
9150 type = TREE_VEC_ELT (expanded_args, i);
9151 else if (!type)
9152 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
c8094d83 9153
5d80a306
DG
9154 if (type == error_mark_node)
9155 return error_mark_node;
9156 if (VOID_TYPE_P (type))
9157 {
9158 if (complain & tf_error)
9159 {
9160 error ("invalid parameter type %qT", type);
9161 if (in_decl)
9162 error ("in declaration %q+D", in_decl);
9163 }
9164 return error_mark_node;
5e97d404 9165 }
5d80a306
DG
9166
9167 /* Do array-to-pointer, function-to-pointer conversion, and ignore
9168 top-level qualifiers as required. */
9169 type = TYPE_MAIN_VARIANT (type_decays_to (type));
c8094d83 9170
5d80a306
DG
9171 /* We do not substitute into default arguments here. The standard
9172 mandates that they be instantiated only when needed, which is
9173 done in build_over_call. */
9174 default_arg = TREE_PURPOSE (arg_types);
9175
9176 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
9177 {
9178 /* We've instantiated a template before its default arguments
9179 have been parsed. This can happen for a nested template
9180 class, and is not an error unless we require the default
9181 argument in a call of this function. */
9182 remaining_arg_types =
9183 tree_cons (default_arg, type, remaining_arg_types);
9184 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg),
9185 remaining_arg_types);
9186 }
9187 else
9188 remaining_arg_types =
9189 hash_tree_cons (default_arg, type, remaining_arg_types);
9190 }
9191
9192 return remaining_arg_types;
cabc336a
MM
9193}
9194
4393e105
MM
9195/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
9196 *not* handle the exception-specification for FNTYPE, because the
9197 initial substitution of explicitly provided template parameters
9198 during argument deduction forbids substitution into the
9199 exception-specification:
9200
9201 [temp.deduct]
9202
9203 All references in the function type of the function template to the
9204 corresponding template parameters are replaced by the specified tem-
9205 plate argument values. If a substitution in a template parameter or
9206 in the function type of the function template results in an invalid
9207 type, type deduction fails. [Note: The equivalent substitution in
9208 exception specifications is done only when the function is instanti-
9209 ated, at which point a program is ill-formed if the substitution
9210 results in an invalid type.] */
9211
9212static tree
c8094d83 9213tsubst_function_type (tree t,
0cbd7506
MS
9214 tree args,
9215 tsubst_flags_t complain,
9216 tree in_decl)
4393e105
MM
9217{
9218 tree return_type;
9219 tree arg_types;
9220 tree fntype;
9221
8dd3f57a 9222 /* The TYPE_CONTEXT is not used for function/method types. */
50bc768d 9223 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
4393e105 9224
46c895ac 9225 /* Substitute the return type. */
4393e105
MM
9226 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
9227 if (return_type == error_mark_node)
9228 return error_mark_node;
6e2993bf
MM
9229 /* The standard does not presently indicate that creation of a
9230 function type with an invalid return type is a deduction failure.
270af55d 9231 However, that is clearly analogous to creating an array of "void"
c8094d83 9232 or a reference to a reference. This is core issue #486. */
6e2993bf
MM
9233 if (TREE_CODE (return_type) == ARRAY_TYPE
9234 || TREE_CODE (return_type) == FUNCTION_TYPE)
9235 {
9236 if (complain & tf_error)
9237 {
9238 if (TREE_CODE (return_type) == ARRAY_TYPE)
9239 error ("function returning an array");
9240 else
9241 error ("function returning a function");
9242 }
9243 return error_mark_node;
9244 }
4393e105 9245
34cd5ae7 9246 /* Substitute the argument types. */
4393e105 9247 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
c8094d83 9248 complain, in_decl);
4393e105
MM
9249 if (arg_types == error_mark_node)
9250 return error_mark_node;
c8094d83 9251
4393e105
MM
9252 /* Construct a new type node and return it. */
9253 if (TREE_CODE (t) == FUNCTION_TYPE)
9254 fntype = build_function_type (return_type, arg_types);
9255 else
9256 {
9257 tree r = TREE_TYPE (TREE_VALUE (arg_types));
9e1e64ec 9258 if (! MAYBE_CLASS_TYPE_P (r))
4393e105
MM
9259 {
9260 /* [temp.deduct]
c8094d83 9261
4393e105
MM
9262 Type deduction may fail for any of the following
9263 reasons:
c8094d83 9264
4393e105
MM
9265 -- Attempting to create "pointer to member of T" when T
9266 is not a class type. */
c2ea3a40 9267 if (complain & tf_error)
0f51ccfc 9268 error ("creating pointer to member function of non-class type %qT",
4393e105
MM
9269 r);
9270 return error_mark_node;
9271 }
c8094d83
MS
9272
9273 fntype = build_method_type_directly (r, return_type,
43dc123f 9274 TREE_CHAIN (arg_types));
4393e105 9275 }
c2ea3a40 9276 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
e9525111 9277 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
c8094d83
MS
9278
9279 return fntype;
4393e105
MM
9280}
9281
c7222c02
MM
9282/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
9283 ARGS into that specification, and return the substituted
9284 specification. If there is no specification, return NULL_TREE. */
9285
9286static tree
c8094d83
MS
9287tsubst_exception_specification (tree fntype,
9288 tree args,
c7222c02
MM
9289 tsubst_flags_t complain,
9290 tree in_decl)
9291{
9292 tree specs;
9293 tree new_specs;
9294
9295 specs = TYPE_RAISES_EXCEPTIONS (fntype);
9296 new_specs = NULL_TREE;
9297 if (specs)
9298 {
9299 if (! TREE_VALUE (specs))
9300 new_specs = specs;
9301 else
9302 while (specs)
9303 {
9304 tree spec;
5d80a306
DG
9305 int i, len = 1;
9306 tree expanded_specs = NULL_TREE;
9307
9308 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
9309 {
9310 /* Expand the pack expansion type. */
9311 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
9312 args, complain,
9313 in_decl);
2f93f02c
DG
9314
9315 if (expanded_specs == error_mark_node)
9316 return error_mark_node;
9317 else if (TREE_CODE (expanded_specs) == TREE_VEC)
9318 len = TREE_VEC_LENGTH (expanded_specs);
9319 else
9320 {
9321 /* We're substituting into a member template, so
9322 we got a TYPE_PACK_EXPANSION back. Add that
9323 expansion and move on. */
9324 gcc_assert (TREE_CODE (expanded_specs)
9325 == TYPE_PACK_EXPANSION);
9326 new_specs = add_exception_specifier (new_specs,
9327 expanded_specs,
9328 complain);
9329 specs = TREE_CHAIN (specs);
9330 continue;
9331 }
5d80a306
DG
9332 }
9333
9334 for (i = 0; i < len; ++i)
9335 {
9336 if (expanded_specs)
9337 spec = TREE_VEC_ELT (expanded_specs, i);
9338 else
9339 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
9340 if (spec == error_mark_node)
9341 return spec;
9342 new_specs = add_exception_specifier (new_specs, spec,
9343 complain);
9344 }
9345
9346 specs = TREE_CHAIN (specs);
c7222c02
MM
9347 }
9348 }
9349 return new_specs;
9350}
9351
4393e105
MM
9352/* Take the tree structure T and replace template parameters used
9353 therein with the argument vector ARGS. IN_DECL is an associated
9354 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
c2ea3a40
NS
9355 Issue error and warning messages under control of COMPLAIN. Note
9356 that we must be relatively non-tolerant of extensions here, in
9357 order to preserve conformance; if we allow substitutions that
9358 should not be allowed, we may allow argument deductions that should
9359 not succeed, and therefore report ambiguous overload situations
9360 where there are none. In theory, we could allow the substitution,
9361 but indicate that it should have failed, and allow our caller to
9362 make sure that the right thing happens, but we don't try to do this
9363 yet.
4393e105
MM
9364
9365 This function is used for dealing with types, decls and the like;
9366 for expressions, use tsubst_expr or tsubst_copy. */
ae58fa02 9367
d5c8be27 9368tree
3a978d72 9369tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
ae58fa02 9370{
0ecfe0b4 9371 tree type, r;
ae58fa02
MM
9372
9373 if (t == NULL_TREE || t == error_mark_node
9374 || t == integer_type_node
9375 || t == void_type_node
9376 || t == char_type_node
539599c1 9377 || t == unknown_type_node
ae58fa02
MM
9378 || TREE_CODE (t) == NAMESPACE_DECL)
9379 return t;
9380
fc6a28d7
MM
9381 if (DECL_P (t))
9382 return tsubst_decl (t, args, complain);
9383
5fa0e853
SE
9384 if (args == NULL_TREE)
9385 return t;
9386
ae58fa02
MM
9387 if (TREE_CODE (t) == IDENTIFIER_NODE)
9388 type = IDENTIFIER_TYPE_VALUE (t);
9389 else
9390 type = TREE_TYPE (t);
399dedb9 9391
50bc768d 9392 gcc_assert (type != unknown_type_node);
ae58fa02 9393
56d0c6e3 9394 /* Reuse typedefs. We need to do this to handle dependent attributes,
a7f6bc8c 9395 such as attribute aligned. */
5044ab0e
JM
9396 if (TYPE_P (t)
9397 && TYPE_NAME (t)
a7f6bc8c 9398 && TYPE_NAME (t) != TYPE_MAIN_DECL (t))
5044ab0e
JM
9399 {
9400 tree decl = TYPE_NAME (t);
56d0c6e3 9401
a7f6bc8c 9402 if (DECL_CLASS_SCOPE_P (decl)
02e52ae5
JM
9403 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
9404 && uses_template_parms (DECL_CONTEXT (decl)))
56d0c6e3
JM
9405 {
9406 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
9407 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
2b110bfc 9408 r = retrieve_specialization (tmpl, gen_args, 0);
56d0c6e3 9409 }
29b0d1fd 9410 else if (DECL_FUNCTION_SCOPE_P (decl)
02e52ae5
JM
9411 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
9412 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
56d0c6e3
JM
9413 r = retrieve_local_specialization (decl);
9414 else
29b0d1fd
JM
9415 /* The typedef is from a non-template context. */
9416 return t;
9417
56d0c6e3
JM
9418 if (r)
9419 {
9420 r = TREE_TYPE (r);
9421 r = cp_build_qualified_type_real
9422 (r, cp_type_quals (t) | cp_type_quals (r),
9423 complain | tf_ignore_bad_quals);
9424 return r;
9425 }
9426 /* Else we must be instantiating the typedef, so fall through. */
5044ab0e
JM
9427 }
9428
fc6a28d7 9429 if (type
ae58fa02 9430 && TREE_CODE (t) != TYPENAME_TYPE
4393e105
MM
9431 && TREE_CODE (t) != IDENTIFIER_NODE
9432 && TREE_CODE (t) != FUNCTION_TYPE
9433 && TREE_CODE (t) != METHOD_TYPE)
9434 type = tsubst (type, args, complain, in_decl);
9435 if (type == error_mark_node)
9436 return error_mark_node;
ae58fa02 9437
ae58fa02
MM
9438 switch (TREE_CODE (t))
9439 {
9440 case RECORD_TYPE:
9441 case UNION_TYPE:
9442 case ENUMERAL_TYPE:
4393e105
MM
9443 return tsubst_aggr_type (t, args, complain, in_decl,
9444 /*entering_scope=*/0);
ae58fa02
MM
9445
9446 case ERROR_MARK:
9447 case IDENTIFIER_NODE:
ae58fa02
MM
9448 case VOID_TYPE:
9449 case REAL_TYPE:
9450 case COMPLEX_TYPE:
c00996a3 9451 case VECTOR_TYPE:
ae58fa02
MM
9452 case BOOLEAN_TYPE:
9453 case INTEGER_CST:
9454 case REAL_CST:
9455 case STRING_CST:
9456 return t;
9457
9458 case INTEGER_TYPE:
9459 if (t == integer_type_node)
9460 return t;
9461
9462 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
9463 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
9464 return t;
d2e5ee5c 9465
5566b478 9466 {
ddce3528 9467 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 9468
6ee3ffe8
MM
9469 max = tsubst_expr (omax, args, complain, in_decl,
9470 /*integral_constant_expression_p=*/false);
5bbca039
JJ
9471
9472 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
9473 needed. */
9474 if (TREE_CODE (max) == NOP_EXPR
9475 && TREE_SIDE_EFFECTS (omax)
9476 && !TREE_TYPE (max))
9477 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
9478
4ef69b83 9479 max = fold_decl_constant_value (max);
8dd3f57a 9480
c1165535
JM
9481 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
9482 with TREE_SIDE_EFFECTS that indicates this is not an integral
9483 constant expression. */
9484 if (processing_template_decl
9485 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
9486 {
9487 gcc_assert (TREE_CODE (max) == NOP_EXPR);
9488 TREE_SIDE_EFFECTS (max) = 1;
9489 }
9490
6a279e92
JJ
9491 if (TREE_CODE (max) != INTEGER_CST
9492 && !at_function_scope_p ()
c1165535 9493 && !TREE_SIDE_EFFECTS (max)
6a279e92 9494 && !value_dependent_expression_p (max))
eff3a276
MM
9495 {
9496 if (complain & tf_error)
9497 error ("array bound is not an integer constant");
9498 return error_mark_node;
9499 }
9500
95cd6f6f 9501 /* [temp.deduct]
4393e105 9502
95cd6f6f
JC
9503 Type deduction may fail for any of the following
9504 reasons:
4393e105 9505
95cd6f6f
JC
9506 Attempting to create an array with a size that is
9507 zero or negative. */
9508 if (integer_zerop (max) && !(complain & tf_error))
9509 /* We must fail if performing argument deduction (as
9510 indicated by the state of complain), so that
9511 another substitution can be found. */
9512 return error_mark_node;
95cd6f6f
JC
9513 else if (TREE_CODE (max) == INTEGER_CST
9514 && INT_CST_LT (max, integer_zero_node))
9515 {
c2ea3a40 9516 if (complain & tf_error)
95cd6f6f 9517 error ("creating array with negative size (%qE)", max);
4393e105
MM
9518
9519 return error_mark_node;
0ecfe0b4
JM
9520 }
9521
c95cd22e 9522 return compute_array_index_type (NULL_TREE, max);
ae58fa02
MM
9523 }
9524
9525 case TEMPLATE_TYPE_PARM:
9526 case TEMPLATE_TEMPLATE_PARM:
a1281f45 9527 case BOUND_TEMPLATE_TEMPLATE_PARM:
ae58fa02
MM
9528 case TEMPLATE_PARM_INDEX:
9529 {
9530 int idx;
9531 int level;
9532 int levels;
315fb5db 9533 tree arg = NULL_TREE;
0ecfe0b4
JM
9534
9535 r = NULL_TREE;
ae58fa02 9536
315fb5db 9537 gcc_assert (TREE_VEC_LENGTH (args) > 0);
f4205442 9538 template_parm_level_and_index (t, &level, &idx);
ae58fa02 9539
315fb5db
NS
9540 levels = TMPL_ARGS_DEPTH (args);
9541 if (level <= levels)
5d80a306
DG
9542 {
9543 arg = TMPL_ARG (args, level, idx);
9544
9545 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
9546 /* See through ARGUMENT_PACK_SELECT arguments. */
9547 arg = ARGUMENT_PACK_SELECT_ARG (arg);
9548 }
ae58fa02 9549
315fb5db
NS
9550 if (arg == error_mark_node)
9551 return error_mark_node;
9552 else if (arg != NULL_TREE)
9553 {
5d80a306
DG
9554 if (ARGUMENT_PACK_P (arg))
9555 /* If ARG is an argument pack, we don't actually want to
9556 perform a substitution here, because substitutions
9557 for argument packs are only done
9558 element-by-element. We can get to this point when
9559 substituting the type of a non-type template
9560 parameter pack, when that type actually contains
9561 template parameter packs from an outer template, e.g.,
9562
9563 template<typename... Types> struct A {
9564 template<Types... Values> struct B { };
9565 }; */
9566 return t;
9567
315fb5db 9568 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
ae58fa02 9569 {
9180c238 9570 int quals;
315fb5db 9571 gcc_assert (TYPE_P (arg));
9180c238
JM
9572
9573 /* cv-quals from the template are discarded when
9574 substituting in a function or reference type. */
9575 if (TREE_CODE (arg) == FUNCTION_TYPE
9576 || TREE_CODE (arg) == METHOD_TYPE
9577 || TREE_CODE (arg) == REFERENCE_TYPE)
9578 quals = cp_type_quals (arg);
9579 else
9580 quals = cp_type_quals (arg) | cp_type_quals (t);
9581
315fb5db 9582 return cp_build_qualified_type_real
9180c238 9583 (arg, quals, complain | tf_ignore_bad_quals);
315fb5db
NS
9584 }
9585 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9586 {
9587 /* We are processing a type constructed from a
9588 template template parameter. */
9589 tree argvec = tsubst (TYPE_TI_ARGS (t),
9590 args, complain, in_decl);
9591 if (argvec == error_mark_node)
9592 return error_mark_node;
c8094d83 9593
315fb5db
NS
9594 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
9595 are resolving nested-types in the signature of a
9596 member function templates. Otherwise ARG is a
9597 TEMPLATE_DECL and is the real template to be
9598 instantiated. */
9599 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
9600 arg = TYPE_NAME (arg);
c8094d83
MS
9601
9602 r = lookup_template_class (arg,
9603 argvec, in_decl,
315fb5db
NS
9604 DECL_CONTEXT (arg),
9605 /*entering_scope=*/0,
9606 complain);
9607 return cp_build_qualified_type_real
9608 (r, TYPE_QUALS (t), complain);
ae58fa02 9609 }
315fb5db
NS
9610 else
9611 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
9612 return arg;
ae58fa02
MM
9613 }
9614
9615 if (level == 1)
9616 /* This can happen during the attempted tsubst'ing in
9617 unify. This means that we don't yet have any information
9618 about the template parameter in question. */
9619 return t;
9620
9621 /* If we get here, we must have been looking at a parm for a
9622 more deeply nested template. Make a new version of this
9623 template parameter, but with a lower level. */
9624 switch (TREE_CODE (t))
9625 {
9626 case TEMPLATE_TYPE_PARM:
9627 case TEMPLATE_TEMPLATE_PARM:
a1281f45 9628 case BOUND_TEMPLATE_TEMPLATE_PARM:
89d684bb 9629 if (cp_type_quals (t))
ae58fa02 9630 {
9ccf6541 9631 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
0cbd7506
MS
9632 r = cp_build_qualified_type_real
9633 (r, cp_type_quals (t),
4f2b0fb2
NS
9634 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
9635 ? tf_ignore_bad_quals : 0));
9ccf6541
MM
9636 }
9637 else
9638 {
11e74ea6 9639 r = copy_type (t);
9ccf6541
MM
9640 TEMPLATE_TYPE_PARM_INDEX (r)
9641 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
ef3b7b17 9642 r, levels, args, complain);
9ccf6541
MM
9643 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
9644 TYPE_MAIN_VARIANT (r) = r;
9645 TYPE_POINTER_TO (r) = NULL_TREE;
9646 TYPE_REFERENCE_TO (r) = NULL_TREE;
9647
06d40de8
DG
9648 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
9649 /* We have reduced the level of the template
9650 template parameter, but not the levels of its
9651 template parameters, so canonical_type_parameter
9652 will not be able to find the canonical template
9653 template parameter for this level. Thus, we
9654 require structural equality checking to compare
9655 TEMPLATE_TEMPLATE_PARMs. */
9656 SET_TYPE_STRUCTURAL_EQUALITY (r);
3d761c46
DG
9657 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
9658 SET_TYPE_STRUCTURAL_EQUALITY (r);
06d40de8
DG
9659 else
9660 TYPE_CANONICAL (r) = canonical_type_parameter (r);
9661
a1281f45 9662 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9ccf6541
MM
9663 {
9664 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
c8094d83 9665 complain, in_decl);
9ccf6541
MM
9666 if (argvec == error_mark_node)
9667 return error_mark_node;
4393e105 9668
9ccf6541
MM
9669 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
9670 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
9671 }
ae58fa02
MM
9672 }
9673 break;
9674
9675 case TEMPLATE_PARM_INDEX:
ef3b7b17 9676 r = reduce_template_parm_level (t, type, levels, args, complain);
ae58fa02 9677 break;
c8094d83 9678
ae58fa02 9679 default:
315fb5db 9680 gcc_unreachable ();
ae58fa02
MM
9681 }
9682
5566b478 9683 return r;
ae58fa02 9684 }
5566b478 9685
8d08fdba
MS
9686 case TREE_LIST:
9687 {
058b15c1 9688 tree purpose, value, chain;
8d08fdba
MS
9689
9690 if (t == void_list_node)
9691 return t;
9692
8d08fdba
MS
9693 purpose = TREE_PURPOSE (t);
9694 if (purpose)
4393e105
MM
9695 {
9696 purpose = tsubst (purpose, args, complain, in_decl);
9697 if (purpose == error_mark_node)
9698 return error_mark_node;
9699 }
8d08fdba
MS
9700 value = TREE_VALUE (t);
9701 if (value)
4393e105
MM
9702 {
9703 value = tsubst (value, args, complain, in_decl);
9704 if (value == error_mark_node)
9705 return error_mark_node;
9706 }
8d08fdba
MS
9707 chain = TREE_CHAIN (t);
9708 if (chain && chain != void_type_node)
4393e105
MM
9709 {
9710 chain = tsubst (chain, args, complain, in_decl);
9711 if (chain == error_mark_node)
9712 return error_mark_node;
9713 }
8d08fdba
MS
9714 if (purpose == TREE_PURPOSE (t)
9715 && value == TREE_VALUE (t)
9716 && chain == TREE_CHAIN (t))
9717 return t;
058b15c1 9718 return hash_tree_cons (purpose, value, chain);
8d08fdba 9719 }
c8094d83 9720
95b4aca6 9721 case TREE_BINFO:
bd7eccc4 9722 /* We should never be tsubsting a binfo. */
315fb5db 9723 gcc_unreachable ();
85b71cf2 9724
95b4aca6
NS
9725 case TREE_VEC:
9726 /* A vector of template arguments. */
50bc768d 9727 gcc_assert (!type);
a91db711 9728 return tsubst_template_args (t, args, complain, in_decl);
8d08fdba 9729
8d08fdba
MS
9730 case POINTER_TYPE:
9731 case REFERENCE_TYPE:
9732 {
8d08fdba 9733 enum tree_code code;
79a7c7fa 9734
46c895ac 9735 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8d08fdba
MS
9736 return t;
9737
9738 code = TREE_CODE (t);
4393e105
MM
9739
9740
9741 /* [temp.deduct]
c8094d83 9742
4393e105 9743 Type deduction may fail for any of the following
c8094d83 9744 reasons:
4393e105
MM
9745
9746 -- Attempting to create a pointer to reference type.
9747 -- Attempting to create a reference to a reference type or
8af2fec4
RY
9748 a reference to void.
9749
9750 Core issue 106 says that creating a reference to a reference
9751 during instantiation is no longer a cause for failure. We
9752 only enforce this check in strict C++98 mode. */
9753 if ((TREE_CODE (type) == REFERENCE_TYPE
c1ae8be5 9754 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
0ecfe0b4 9755 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa 9756 {
82a98427 9757 static location_t last_loc;
79a7c7fa
JM
9758
9759 /* We keep track of the last time we issued this error
9760 message to avoid spewing a ton of messages during a
9761 single bad template instantiation. */
c2ea3a40 9762 if (complain & tf_error
2d593c86 9763 && last_loc != input_location)
79a7c7fa 9764 {
0ecfe0b4 9765 if (TREE_CODE (type) == VOID_TYPE)
33bd39a2 9766 error ("forming reference to void");
0ecfe0b4 9767 else
0f51ccfc 9768 error ("forming %s to reference type %qT",
0cbd7506
MS
9769 (code == POINTER_TYPE) ? "pointer" : "reference",
9770 type);
82a98427 9771 last_loc = input_location;
79a7c7fa
JM
9772 }
9773
4393e105 9774 return error_mark_node;
79a7c7fa
JM
9775 }
9776 else if (code == POINTER_TYPE)
46c895ac
NS
9777 {
9778 r = build_pointer_type (type);
9779 if (TREE_CODE (type) == METHOD_TYPE)
9780 r = build_ptrmemfunc_type (r);
9781 }
8af2fec4
RY
9782 else if (TREE_CODE (type) == REFERENCE_TYPE)
9783 /* In C++0x, during template argument substitution, when there is an
9784 attempt to create a reference to a reference type, reference
9785 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
9786
9787 "If a template-argument for a template-parameter T names a type
9788 that is a reference to a type A, an attempt to create the type
9789 'lvalue reference to cv T' creates the type 'lvalue reference to
9790 A,' while an attempt to create the type type rvalue reference to
9791 cv T' creates the type T"
9792 */
9793 r = cp_build_reference_type
9794 (TREE_TYPE (type),
9795 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
8d08fdba 9796 else
8af2fec4 9797 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
adecb3f4 9798 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
79a7c7fa 9799
a5f1c5f8
NS
9800 if (r != error_mark_node)
9801 /* Will this ever be needed for TYPE_..._TO values? */
9802 layout_type (r);
c8094d83 9803
8d08fdba
MS
9804 return r;
9805 }
a4443a08 9806 case OFFSET_TYPE:
0ecfe0b4 9807 {
4393e105 9808 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
9e1e64ec 9809 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
4393e105
MM
9810 {
9811 /* [temp.deduct]
9812
9813 Type deduction may fail for any of the following
9814 reasons:
c8094d83 9815
4393e105 9816 -- Attempting to create "pointer to member of T" when T
0cbd7506 9817 is not a class type. */
c2ea3a40 9818 if (complain & tf_error)
0f51ccfc 9819 error ("creating pointer to member of non-class type %qT", r);
4393e105
MM
9820 return error_mark_node;
9821 }
46c895ac
NS
9822 if (TREE_CODE (type) == REFERENCE_TYPE)
9823 {
4f09be91 9824 if (complain & tf_error)
0f51ccfc 9825 error ("creating pointer to member reference type %qT", type);
cb6d4a9f
VR
9826 return error_mark_node;
9827 }
9828 if (TREE_CODE (type) == VOID_TYPE)
9829 {
9830 if (complain & tf_error)
9831 error ("creating pointer to member of type void");
46c895ac
NS
9832 return error_mark_node;
9833 }
50bc768d 9834 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
46c895ac 9835 if (TREE_CODE (type) == FUNCTION_TYPE)
a5ac359a 9836 {
0cbd7506
MS
9837 /* The type of the implicit object parameter gets its
9838 cv-qualifiers from the FUNCTION_TYPE. */
a5ac359a 9839 tree method_type;
0cbd7506
MS
9840 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
9841 cp_type_quals (type));
9842 tree memptr;
9843 method_type = build_method_type_directly (this_type,
43dc123f
MM
9844 TREE_TYPE (type),
9845 TYPE_ARG_TYPES (type));
0cbd7506
MS
9846 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
9847 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
9848 complain);
a5ac359a 9849 }
46c895ac 9850 else
b7a78333
MM
9851 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
9852 TYPE_QUALS (t),
9853 complain);
0ecfe0b4 9854 }
8d08fdba
MS
9855 case FUNCTION_TYPE:
9856 case METHOD_TYPE:
9857 {
c11b6f21 9858 tree fntype;
c7222c02 9859 tree specs;
4393e105
MM
9860 fntype = tsubst_function_type (t, args, complain, in_decl);
9861 if (fntype == error_mark_node)
9862 return error_mark_node;
cabc336a 9863
34cd5ae7 9864 /* Substitute the exception specification. */
c8094d83 9865 specs = tsubst_exception_specification (t, args, complain,
c7222c02 9866 in_decl);
9f6206d9
VR
9867 if (specs == error_mark_node)
9868 return error_mark_node;
c7222c02
MM
9869 if (specs)
9870 fntype = build_exception_variant (fntype, specs);
c11b6f21 9871 return fntype;
8d08fdba
MS
9872 }
9873 case ARRAY_TYPE:
9874 {
4393e105
MM
9875 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
9876 if (domain == error_mark_node)
9877 return error_mark_node;
9878
9879 /* As an optimization, we avoid regenerating the array type if
9880 it will obviously be the same as T. */
8d08fdba
MS
9881 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
9882 return t;
0ecfe0b4 9883
c8094d83 9884 /* These checks should match the ones in grokdeclarator.
4393e105 9885
c8094d83
MS
9886 [temp.deduct]
9887
9888 The deduction may fail for any of the following reasons:
4393e105
MM
9889
9890 -- Attempting to create an array with an element type that
c8094d83 9891 is void, a function type, or a reference type, or [DR337]
cfb91b67 9892 an abstract class type. */
c8094d83 9893 if (TREE_CODE (type) == VOID_TYPE
4393e105
MM
9894 || TREE_CODE (type) == FUNCTION_TYPE
9895 || TREE_CODE (type) == REFERENCE_TYPE)
0ecfe0b4 9896 {
c2ea3a40 9897 if (complain & tf_error)
0f51ccfc 9898 error ("creating array of %qT", type);
4393e105 9899 return error_mark_node;
0ecfe0b4 9900 }
cfb91b67
GB
9901 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
9902 {
9903 if (complain & tf_error)
c8094d83 9904 error ("creating array of %qT, which is an abstract class type",
cfb91b67 9905 type);
c8094d83 9906 return error_mark_node;
cfb91b67 9907 }
0ecfe0b4 9908
8d08fdba 9909 r = build_cplus_array_type (type, domain);
5044ab0e
JM
9910
9911 if (TYPE_USER_ALIGN (t))
9912 {
9913 TYPE_ALIGN (r) = TYPE_ALIGN (t);
9914 TYPE_USER_ALIGN (r) = 1;
9915 }
9916
8d08fdba
MS
9917 return r;
9918 }
9919
8d08fdba 9920 case PLUS_EXPR:
5566b478 9921 case MINUS_EXPR:
4393e105 9922 {
c2ea3a40
NS
9923 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
9924 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
4393e105
MM
9925
9926 if (e1 == error_mark_node || e2 == error_mark_node)
9927 return error_mark_node;
9928
db3927fb
AH
9929 return fold_build2_loc (input_location,
9930 TREE_CODE (t), TREE_TYPE (t), e1, e2);
4393e105 9931 }
8d08fdba
MS
9932
9933 case NEGATE_EXPR:
9934 case NOP_EXPR:
4393e105 9935 {
c2ea3a40 9936 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
9937 if (e == error_mark_node)
9938 return error_mark_node;
9939
db3927fb 9940 return fold_build1_loc (input_location, TREE_CODE (t), TREE_TYPE (t), e);
4393e105 9941 }
8d08fdba 9942
5566b478
MS
9943 case TYPENAME_TYPE:
9944 {
4393e105
MM
9945 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
9946 in_decl, /*entering_scope=*/1);
9947 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
c8094d83 9948 complain, in_decl);
4393e105
MM
9949
9950 if (ctx == error_mark_node || f == error_mark_node)
9951 return error_mark_node;
ae58fa02 9952
9e1e64ec 9953 if (!MAYBE_CLASS_TYPE_P (ctx))
bf8f3f93 9954 {
c2ea3a40 9955 if (complain & tf_error)
0f51ccfc 9956 error ("%qT is not a class, struct, or union type", ctx);
bf8f3f93
MM
9957 return error_mark_node;
9958 }
9959 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
9960 {
9961 /* Normally, make_typename_type does not require that the CTX
9962 have complete type in order to allow things like:
c8094d83 9963
0cbd7506 9964 template <class T> struct S { typename S<T>::X Y; };
ae58fa02 9965
bf8f3f93
MM
9966 But, such constructs have already been resolved by this
9967 point, so here CTX really should have complete type, unless
9968 it's a partial instantiation. */
8b334f7b
JM
9969 if (!(complain & tf_no_class_instantiations))
9970 ctx = complete_type (ctx);
d0f062fb 9971 if (!COMPLETE_TYPE_P (ctx))
4393e105 9972 {
c2ea3a40 9973 if (complain & tf_error)
7a228918 9974 cxx_incomplete_type_error (NULL_TREE, ctx);
4393e105
MM
9975 return error_mark_node;
9976 }
9977 }
ae58fa02 9978
fc6a28d7 9979 f = make_typename_type (ctx, f, typename_type,
4f2b0fb2 9980 (complain & tf_error) | tf_keep_type_decl);
f0bcd168
MM
9981 if (f == error_mark_node)
9982 return f;
0cbd7506
MS
9983 if (TREE_CODE (f) == TYPE_DECL)
9984 {
4f2b0fb2 9985 complain |= tf_ignore_bad_quals;
0cbd7506
MS
9986 f = TREE_TYPE (f);
9987 }
c8094d83 9988
fc6a28d7
MM
9989 if (TREE_CODE (f) != TYPENAME_TYPE)
9990 {
9991 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
c8094d83 9992 error ("%qT resolves to %qT, which is not an enumeration type",
fc6a28d7
MM
9993 t, f);
9994 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
c8094d83 9995 error ("%qT resolves to %qT, which is is not a class type",
fc6a28d7
MM
9996 t, f);
9997 }
9998
0cbd7506
MS
9999 return cp_build_qualified_type_real
10000 (f, cp_type_quals (f) | cp_type_quals (t), complain);
5566b478 10001 }
c8094d83 10002
b8c6534b
KL
10003 case UNBOUND_CLASS_TEMPLATE:
10004 {
10005 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
10006 in_decl, /*entering_scope=*/1);
10007 tree name = TYPE_IDENTIFIER (t);
b939a023 10008 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
b8c6534b
KL
10009
10010 if (ctx == error_mark_node || name == error_mark_node)
10011 return error_mark_node;
10012
b939a023
KL
10013 if (parm_list)
10014 parm_list = tsubst_template_parms (parm_list, args, complain);
10015 return make_unbound_class_template (ctx, name, parm_list, complain);
b8c6534b
KL
10016 }
10017
5566b478 10018 case INDIRECT_REF:
5566b478 10019 case ADDR_EXPR:
058b15c1 10020 case CALL_EXPR:
315fb5db 10021 gcc_unreachable ();
5566b478
MS
10022
10023 case ARRAY_REF:
4393e105 10024 {
c2ea3a40 10025 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
015c2c66
MM
10026 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
10027 /*integral_constant_expression_p=*/false);
4393e105
MM
10028 if (e1 == error_mark_node || e2 == error_mark_node)
10029 return error_mark_node;
10030
44de5aeb 10031 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
4393e105 10032 }
5566b478 10033
fc378698 10034 case SCOPE_REF:
4393e105 10035 {
c2ea3a40 10036 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
10037 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
10038 if (e1 == error_mark_node || e2 == error_mark_node)
10039 return error_mark_node;
10040
02ed62dd
MM
10041 return build_qualified_name (/*type=*/NULL_TREE,
10042 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
4393e105 10043 }
fc378698 10044
b894fc05 10045 case TYPEOF_TYPE:
4393e105 10046 {
b830b74c 10047 tree type;
4393e105 10048
015c2c66
MM
10049 type = finish_typeof (tsubst_expr
10050 (TYPEOF_TYPE_EXPR (t), args,
10051 complain, in_decl,
10052 /*integral_constant_expression_p=*/false));
b830b74c 10053 return cp_build_qualified_type_real (type,
dce50630 10054 cp_type_quals (t)
b830b74c 10055 | cp_type_quals (type),
dce50630 10056 complain);
4393e105 10057 }
b894fc05 10058
3ad6a8e1
DG
10059 case DECLTYPE_TYPE:
10060 {
10061 tree type;
10062
7d882b83
ILT
10063 ++cp_unevaluated_operand;
10064 ++c_inhibit_evaluation_warnings;
b344d949
JM
10065
10066 type = tsubst_expr (DECLTYPE_TYPE_EXPR (t), args,
10067 complain, in_decl,
10068 /*integral_constant_expression_p=*/false);
10069
7d882b83
ILT
10070 --cp_unevaluated_operand;
10071 --c_inhibit_evaluation_warnings;
b344d949
JM
10072
10073 type =
10074 finish_decltype_type (type,
3ad6a8e1
DG
10075 DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t));
10076 return cp_build_qualified_type_real (type,
10077 cp_type_quals (t)
10078 | cp_type_quals (type),
10079 complain);
10080 }
10081
5d80a306
DG
10082 case TYPE_ARGUMENT_PACK:
10083 case NONTYPE_ARGUMENT_PACK:
10084 {
10085 tree r = make_node (TREE_CODE (t));
10086 tree packed_out =
10087 tsubst_template_args (ARGUMENT_PACK_ARGS (t),
10088 args,
10089 complain,
10090 in_decl);
10091 SET_ARGUMENT_PACK_ARGS (r, packed_out);
10092
10093 /* For template nontype argument packs, also substitute into
10094 the type. */
10095 if (TREE_CODE (t) == NONTYPE_ARGUMENT_PACK)
10096 TREE_TYPE (r) = tsubst (TREE_TYPE (t), args, complain, in_decl);
10097
10098 return r;
10099 }
10100 break;
10101
8d08fdba 10102 default:
9e637a26 10103 sorry ("use of %qs in template",
8d08fdba
MS
10104 tree_code_name [(int) TREE_CODE (t)]);
10105 return error_mark_node;
10106 }
10107}
10108
ee76b931
MM
10109/* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
10110 type of the expression on the left-hand side of the "." or "->"
10111 operator. */
10112
10113static tree
10114tsubst_baselink (tree baselink, tree object_type,
10115 tree args, tsubst_flags_t complain, tree in_decl)
10116{
10117 tree name;
10118 tree qualifying_scope;
10119 tree fns;
6f67f000 10120 tree optype;
ee76b931
MM
10121 tree template_args = 0;
10122 bool template_id_p = false;
10123
51a203d9
MM
10124 /* A baselink indicates a function from a base class. Both the
10125 BASELINK_ACCESS_BINFO and the base class referenced may
10126 indicate bases of the template class, rather than the
10127 instantiated class. In addition, lookups that were not
10128 ambiguous before may be ambiguous now. Therefore, we perform
10129 the lookup again. */
ee76b931 10130 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
51a203d9
MM
10131 qualifying_scope = tsubst (qualifying_scope, args,
10132 complain, in_decl);
ee76b931 10133 fns = BASELINK_FUNCTIONS (baselink);
6f67f000 10134 optype = BASELINK_OPTYPE (baselink);
ee76b931
MM
10135 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
10136 {
10137 template_id_p = true;
10138 template_args = TREE_OPERAND (fns, 1);
10139 fns = TREE_OPERAND (fns, 0);
bf12d54d
NS
10140 if (template_args)
10141 template_args = tsubst_template_args (template_args, args,
10142 complain, in_decl);
ee76b931
MM
10143 }
10144 name = DECL_NAME (get_first_fn (fns));
10145 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
c8094d83 10146
9b60dfd7
MM
10147 /* If lookup found a single function, mark it as used at this
10148 point. (If it lookup found multiple functions the one selected
10149 later by overload resolution will be marked as used at that
10150 point.) */
10151 if (BASELINK_P (baselink))
10152 fns = BASELINK_FUNCTIONS (baselink);
10153 if (!template_id_p && !really_overloaded_fn (fns))
10154 mark_used (OVL_CURRENT (fns));
10155
10156 /* Add back the template arguments, if present. */
ee76b931 10157 if (BASELINK_P (baselink) && template_id_p)
c8094d83 10158 BASELINK_FUNCTIONS (baselink)
ee76b931
MM
10159 = build_nt (TEMPLATE_ID_EXPR,
10160 BASELINK_FUNCTIONS (baselink),
10161 template_args);
6f67f000
MM
10162 /* Update the conversion operator type. */
10163 BASELINK_OPTYPE (baselink)
10164 = tsubst (optype, args, complain, in_decl);
9b60dfd7 10165
ee76b931
MM
10166 if (!object_type)
10167 object_type = current_class_type;
c8094d83 10168 return adjust_result_of_qualified_name_lookup (baselink,
ee76b931
MM
10169 qualifying_scope,
10170 object_type);
10171}
10172
10173/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
10174 true if the qualified-id will be a postfix-expression in-and-of
10175 itself; false if more of the postfix-expression follows the
10176 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
10177 of "&". */
10178
10179static tree
c8094d83 10180tsubst_qualified_id (tree qualified_id, tree args,
ee76b931
MM
10181 tsubst_flags_t complain, tree in_decl,
10182 bool done, bool address_p)
10183{
10184 tree expr;
10185 tree scope;
10186 tree name;
10187 bool is_template;
10188 tree template_args;
10189
50bc768d 10190 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
ee76b931 10191
ee76b931
MM
10192 /* Figure out what name to look up. */
10193 name = TREE_OPERAND (qualified_id, 1);
10194 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
10195 {
10196 is_template = true;
bf12d54d
NS
10197 template_args = TREE_OPERAND (name, 1);
10198 if (template_args)
10199 template_args = tsubst_template_args (template_args, args,
10200 complain, in_decl);
ee76b931
MM
10201 name = TREE_OPERAND (name, 0);
10202 }
10203 else
10204 {
10205 is_template = false;
10206 template_args = NULL_TREE;
10207 }
10208
6eeba0cc
MM
10209 /* Substitute into the qualifying scope. When there are no ARGS, we
10210 are just trying to simplify a non-dependent expression. In that
10211 case the qualifying scope may be dependent, and, in any case,
10212 substituting will not help. */
10213 scope = TREE_OPERAND (qualified_id, 0);
10214 if (args)
10215 {
10216 scope = tsubst (scope, args, complain, in_decl);
10217 expr = tsubst_copy (name, args, complain, in_decl);
10218 }
10219 else
10220 expr = name;
10b1d5e7 10221
ab73670a 10222 if (dependent_type_p (scope))
7e361ae6
JM
10223 {
10224 tree type = NULL_TREE;
10225 if (DECL_P (expr) && !dependent_scope_p (scope))
10226 type = TREE_TYPE (expr);
10227 return build_qualified_name (type, scope, expr,
10228 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
10229 }
c8094d83 10230
5e08432e 10231 if (!BASELINK_P (name) && !DECL_P (expr))
12483c9f 10232 {
8ca4bf25 10233 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7e361ae6
JM
10234 {
10235 /* A BIT_NOT_EXPR is used to represent a destructor. */
10236 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
10237 {
10238 error ("qualifying type %qT does not match destructor name ~%qT",
10239 scope, TREE_OPERAND (expr, 0));
10240 expr = error_mark_node;
10241 }
10242 else
10243 expr = lookup_qualified_name (scope, complete_dtor_identifier,
10244 /*is_type_p=*/0, false);
10245 }
8ca4bf25
MM
10246 else
10247 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12483c9f
NS
10248 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
10249 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
10250 {
10251 if (complain & tf_error)
b2693faf 10252 {
0f51ccfc 10253 error ("dependent-name %qE is parsed as a non-type, but "
b2693faf 10254 "instantiation yields a type", qualified_id);
1f5b3869 10255 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
b2693faf 10256 }
12483c9f
NS
10257 return error_mark_node;
10258 }
10259 }
c8094d83 10260
279b8466 10261 if (DECL_P (expr))
8f78f01f
MM
10262 {
10263 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
10264 scope);
10265 /* Remember that there was a reference to this entity. */
10266 mark_used (expr);
10267 }
10268
10269 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
10270 {
10271 if (complain & tf_error)
c8094d83 10272 qualified_name_lookup_error (scope,
8f78f01f 10273 TREE_OPERAND (qualified_id, 1),
2b7a3abf 10274 expr, input_location);
8f78f01f
MM
10275 return error_mark_node;
10276 }
ee76b931
MM
10277
10278 if (is_template)
10b1d5e7 10279 expr = lookup_template_function (expr, template_args);
ee76b931 10280
22038b2c 10281 if (expr == error_mark_node && complain & tf_error)
8f78f01f 10282 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
2b7a3abf 10283 expr, input_location);
22038b2c 10284 else if (TYPE_P (scope))
ee76b931 10285 {
c8094d83 10286 expr = (adjust_result_of_qualified_name_lookup
ee76b931 10287 (expr, scope, current_class_type));
3db45ab5 10288 expr = (finish_qualified_id_expr
02ed62dd
MM
10289 (scope, expr, done, address_p,
10290 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
10291 /*template_arg_p=*/false));
ee76b931 10292 }
c8094d83 10293
015c2c66
MM
10294 /* Expressions do not generally have reference type. */
10295 if (TREE_CODE (expr) != SCOPE_REF
10296 /* However, if we're about to form a pointer-to-member, we just
10297 want the referenced member referenced. */
10298 && TREE_CODE (expr) != OFFSET_REF)
aec5ba60 10299 expr = convert_from_reference (expr);
ee76b931
MM
10300
10301 return expr;
10302}
10303
00d3396f
JM
10304/* Like tsubst, but deals with expressions. This function just replaces
10305 template parms; to finish processing the resultant expression, use
10306 tsubst_expr. */
10307
14d22dd6 10308static tree
3a978d72 10309tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5566b478
MS
10310{
10311 enum tree_code code;
8452b1d3 10312 tree r;
8d08fdba 10313
b77ba909 10314 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
5566b478
MS
10315 return t;
10316
10317 code = TREE_CODE (t);
b7484fbe 10318
5566b478
MS
10319 switch (code)
10320 {
10321 case PARM_DECL:
a723baf1 10322 r = retrieve_local_specialization (t);
b344d949
JM
10323
10324 if (r == NULL)
10325 {
c7cb9f42 10326 tree c;
b344d949 10327 /* This can happen for a parameter name used later in a function
448545cb
JM
10328 declaration (such as in a late-specified return type). Just
10329 make a dummy decl, since it's only used for its type. */
7d882b83 10330 gcc_assert (cp_unevaluated_operand != 0);
c7cb9f42
DS
10331 /* We copy T because want to tsubst the PARM_DECL only,
10332 not the following PARM_DECLs that are chained to T. */
10333 c = copy_node (t);
10334 r = tsubst_decl (c, args, complain);
448545cb
JM
10335 /* Give it the template pattern as its context; its true context
10336 hasn't been instantiated yet and this is good enough for
10337 mangling. */
10338 DECL_CONTEXT (r) = DECL_CONTEXT (t);
b344d949
JM
10339 }
10340
5d80a306
DG
10341 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
10342 r = ARGUMENT_PACK_SELECT_ARG (r);
c0694c4b 10343 mark_used (r);
a723baf1 10344 return r;
5566b478
MS
10345
10346 case CONST_DECL:
ed44da02
MM
10347 {
10348 tree enum_type;
10349 tree v;
10350
a723baf1
MM
10351 if (DECL_TEMPLATE_PARM_P (t))
10352 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7b6d72fc
MM
10353 /* There is no need to substitute into namespace-scope
10354 enumerators. */
10355 if (DECL_NAMESPACE_SCOPE_P (t))
ed44da02 10356 return t;
d5a10cf0
MM
10357 /* If ARGS is NULL, then T is known to be non-dependent. */
10358 if (args == NULL_TREE)
8a784e4a 10359 return integral_constant_value (t);
ed44da02
MM
10360
10361 /* Unfortunately, we cannot just call lookup_name here.
9188c363 10362 Consider:
c8094d83 10363
9188c363
MM
10364 template <int I> int f() {
10365 enum E { a = I };
10366 struct S { void g() { E e = a; } };
10367 };
c8094d83 10368
9188c363
MM
10369 When we instantiate f<7>::S::g(), say, lookup_name is not
10370 clever enough to find f<7>::a. */
c8094d83
MS
10371 enum_type
10372 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
ed44da02
MM
10373 /*entering_scope=*/0);
10374
c8094d83
MS
10375 for (v = TYPE_VALUES (enum_type);
10376 v != NULL_TREE;
ed44da02
MM
10377 v = TREE_CHAIN (v))
10378 if (TREE_PURPOSE (v) == DECL_NAME (t))
10379 return TREE_VALUE (v);
10380
10381 /* We didn't find the name. That should never happen; if
10382 name-lookup found it during preliminary parsing, we
10383 should find it again here during instantiation. */
315fb5db 10384 gcc_unreachable ();
ed44da02 10385 }
db04386f 10386 return t;
ed44da02 10387
5566b478
MS
10388 case FIELD_DECL:
10389 if (DECL_CONTEXT (t))
10390 {
0978790f 10391 tree ctx;
0978790f 10392
4393e105 10393 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
36a117a5 10394 /*entering_scope=*/1);
0978790f 10395 if (ctx != DECL_CONTEXT (t))
bad1f462
KL
10396 {
10397 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
10398 if (!r)
10399 {
10400 if (complain & tf_error)
10401 error ("using invalid field %qD", t);
10402 return error_mark_node;
10403 }
10404 return r;
10405 }
5566b478 10406 }
c8094d83 10407
5566b478
MS
10408 return t;
10409
10410 case VAR_DECL:
10411 case FUNCTION_DECL:
a723baf1
MM
10412 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10413 || local_variable_p (t))
4393e105 10414 t = tsubst (t, args, complain, in_decl);
5566b478
MS
10415 mark_used (t);
10416 return t;
10417
a723baf1 10418 case BASELINK:
ee76b931 10419 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
a723baf1 10420
98c1c668 10421 case TEMPLATE_DECL:
a723baf1 10422 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
c8094d83 10423 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
a723baf1 10424 args, complain, in_decl);
c7222c02 10425 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
4393e105 10426 return tsubst (t, args, complain, in_decl);
fcea7401
KL
10427 else if (DECL_CLASS_SCOPE_P (t)
10428 && uses_template_parms (DECL_CONTEXT (t)))
10429 {
10430 /* Template template argument like the following example need
10431 special treatment:
10432
10433 template <template <class> class TT> struct C {};
10434 template <class T> struct D {
10435 template <class U> struct E {};
0cbd7506 10436 C<E> c; // #1
fcea7401
KL
10437 };
10438 D<int> d; // #2
10439
10440 We are processing the template argument `E' in #1 for
10441 the template instantiation #2. Originally, `E' is a
10442 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
10443 have to substitute this with one having context `D<int>'. */
10444
10445 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
10446 return lookup_field (context, DECL_NAME(t), 0, false);
10447 }
98c1c668 10448 else
fcea7401 10449 /* Ordinary template template argument. */
98c1c668
JM
10450 return t;
10451
5566b478
MS
10452 case CAST_EXPR:
10453 case REINTERPRET_CAST_EXPR:
e92cc029
MS
10454 case CONST_CAST_EXPR:
10455 case STATIC_CAST_EXPR:
10456 case DYNAMIC_CAST_EXPR:
51924768 10457 case NOP_EXPR:
5566b478 10458 return build1
4393e105
MM
10459 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
10460 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478 10461
5d80a306
DG
10462 case SIZEOF_EXPR:
10463 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
10464 {
10465 /* We only want to compute the number of arguments. */
10466 tree expanded = tsubst_pack_expansion (TREE_OPERAND (t, 0), args,
10467 complain, in_decl);
e68c63e3 10468 int len = 0;
063cc99c
DG
10469
10470 if (TREE_CODE (expanded) == TREE_VEC)
10471 len = TREE_VEC_LENGTH (expanded);
10472
4745e4eb
JJ
10473 if (expanded == error_mark_node)
10474 return error_mark_node;
063cc99c
DG
10475 else if (PACK_EXPANSION_P (expanded)
10476 || (TREE_CODE (expanded) == TREE_VEC
10477 && len > 0
10478 && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
10479 {
10480 if (TREE_CODE (expanded) == TREE_VEC)
10481 expanded = TREE_VEC_ELT (expanded, len - 1);
10482
10483 if (TYPE_P (expanded))
10484 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
10485 complain & tf_error);
10486 else
10487 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
10488 complain & tf_error);
10489 }
10490 else
10491 return build_int_cst (size_type_node, len);
5d80a306
DG
10492 }
10493 /* Fall through */
10494
5566b478 10495 case INDIRECT_REF:
5566b478
MS
10496 case NEGATE_EXPR:
10497 case TRUTH_NOT_EXPR:
b87692e5 10498 case BIT_NOT_EXPR:
5566b478 10499 case ADDR_EXPR:
392e3d51 10500 case UNARY_PLUS_EXPR: /* Unary + */
abff8e06 10501 case ALIGNOF_EXPR:
5566b478 10502 case ARROW_EXPR:
fc378698 10503 case THROW_EXPR:
5156628f 10504 case TYPEID_EXPR:
f5733617
SS
10505 case REALPART_EXPR:
10506 case IMAGPART_EXPR:
5566b478 10507 return build1
6a629cac 10508 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
4393e105 10509 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478 10510
a723baf1
MM
10511 case COMPONENT_REF:
10512 {
10513 tree object;
10514 tree name;
10515
10516 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
10517 name = TREE_OPERAND (t, 1);
c8094d83 10518 if (TREE_CODE (name) == BIT_NOT_EXPR)
a723baf1
MM
10519 {
10520 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10521 complain, in_decl);
10522 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
10523 }
10524 else if (TREE_CODE (name) == SCOPE_REF
10525 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
10526 {
10527 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
10528 complain, in_decl);
10529 name = TREE_OPERAND (name, 1);
10530 name = tsubst_copy (TREE_OPERAND (name, 0), args,
10531 complain, in_decl);
10532 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
02ed62dd 10533 name = build_qualified_name (/*type=*/NULL_TREE,
3db45ab5 10534 base, name,
02ed62dd 10535 /*template_p=*/false);
a723baf1 10536 }
ee76b931 10537 else if (TREE_CODE (name) == BASELINK)
c8094d83
MS
10538 name = tsubst_baselink (name,
10539 non_reference (TREE_TYPE (object)),
10540 args, complain,
ee76b931 10541 in_decl);
a723baf1 10542 else
ee76b931 10543 name = tsubst_copy (name, args, complain, in_decl);
44de5aeb 10544 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
a723baf1
MM
10545 }
10546
5566b478
MS
10547 case PLUS_EXPR:
10548 case MINUS_EXPR:
10549 case MULT_EXPR:
10550 case TRUNC_DIV_EXPR:
10551 case CEIL_DIV_EXPR:
10552 case FLOOR_DIV_EXPR:
10553 case ROUND_DIV_EXPR:
10554 case EXACT_DIV_EXPR:
10555 case BIT_AND_EXPR:
5566b478
MS
10556 case BIT_IOR_EXPR:
10557 case BIT_XOR_EXPR:
10558 case TRUNC_MOD_EXPR:
10559 case FLOOR_MOD_EXPR:
10560 case TRUTH_ANDIF_EXPR:
10561 case TRUTH_ORIF_EXPR:
10562 case TRUTH_AND_EXPR:
10563 case TRUTH_OR_EXPR:
10564 case RSHIFT_EXPR:
10565 case LSHIFT_EXPR:
10566 case RROTATE_EXPR:
10567 case LROTATE_EXPR:
10568 case EQ_EXPR:
10569 case NE_EXPR:
10570 case MAX_EXPR:
10571 case MIN_EXPR:
10572 case LE_EXPR:
10573 case GE_EXPR:
10574 case LT_EXPR:
10575 case GT_EXPR:
5566b478 10576 case COMPOUND_EXPR:
5566b478
MS
10577 case DOTSTAR_EXPR:
10578 case MEMBER_REF:
519c9806
MM
10579 case PREDECREMENT_EXPR:
10580 case PREINCREMENT_EXPR:
10581 case POSTDECREMENT_EXPR:
10582 case POSTINCREMENT_EXPR:
5566b478 10583 return build_nt
4393e105
MM
10584 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10585 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478 10586
02ed62dd
MM
10587 case SCOPE_REF:
10588 return build_qualified_name (/*type=*/NULL_TREE,
10589 tsubst_copy (TREE_OPERAND (t, 0),
10590 args, complain, in_decl),
10591 tsubst_copy (TREE_OPERAND (t, 1),
10592 args, complain, in_decl),
10593 QUALIFIED_NAME_IS_TEMPLATE (t));
10594
d8987adb
NS
10595 case ARRAY_REF:
10596 return build_nt
10597 (ARRAY_REF,
10598 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10599 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10600 NULL_TREE, NULL_TREE);
10601
5566b478 10602 case CALL_EXPR:
5039610b
SL
10603 {
10604 int n = VL_EXP_OPERAND_LENGTH (t);
10605 tree result = build_vl_exp (CALL_EXPR, n);
10606 int i;
10607 for (i = 0; i < n; i++)
10608 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
10609 complain, in_decl);
10610 return result;
10611 }
5566b478 10612
5566b478
MS
10613 case COND_EXPR:
10614 case MODOP_EXPR:
40242ccf 10615 case PSEUDO_DTOR_EXPR:
67da3287 10616 {
8452b1d3 10617 r = build_nt
4393e105
MM
10618 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10619 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10620 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
e4c2c34b 10621 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
67da3287
MM
10622 return r;
10623 }
5566b478
MS
10624
10625 case NEW_EXPR:
10626 {
8452b1d3 10627 r = build_nt
4393e105
MM
10628 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10629 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
10630 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
5566b478
MS
10631 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
10632 return r;
10633 }
10634
10635 case DELETE_EXPR:
10636 {
8452b1d3 10637 r = build_nt
4393e105
MM
10638 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
10639 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
10640 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
10641 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
10642 return r;
10643 }
10644
386b8a85
JM
10645 case TEMPLATE_ID_EXPR:
10646 {
0cbd7506 10647 /* Substituted template arguments */
a91db711
NS
10648 tree fn = TREE_OPERAND (t, 0);
10649 tree targs = TREE_OPERAND (t, 1);
856216bb 10650
a91db711 10651 fn = tsubst_copy (fn, args, complain, in_decl);
bf12d54d
NS
10652 if (targs)
10653 targs = tsubst_template_args (targs, args, complain, in_decl);
c8094d83 10654
a91db711 10655 return lookup_template_function (fn, targs);
386b8a85
JM
10656 }
10657
5566b478
MS
10658 case TREE_LIST:
10659 {
10660 tree purpose, value, chain;
10661
10662 if (t == void_list_node)
10663 return t;
10664
10665 purpose = TREE_PURPOSE (t);
10666 if (purpose)
4393e105 10667 purpose = tsubst_copy (purpose, args, complain, in_decl);
5566b478
MS
10668 value = TREE_VALUE (t);
10669 if (value)
4393e105 10670 value = tsubst_copy (value, args, complain, in_decl);
5566b478
MS
10671 chain = TREE_CHAIN (t);
10672 if (chain && chain != void_type_node)
4393e105 10673 chain = tsubst_copy (chain, args, complain, in_decl);
5566b478
MS
10674 if (purpose == TREE_PURPOSE (t)
10675 && value == TREE_VALUE (t)
10676 && chain == TREE_CHAIN (t))
10677 return t;
10678 return tree_cons (purpose, value, chain);
10679 }
10680
10681 case RECORD_TYPE:
10682 case UNION_TYPE:
10683 case ENUMERAL_TYPE:
10684 case INTEGER_TYPE:
10685 case TEMPLATE_TYPE_PARM:
73b0fce8 10686 case TEMPLATE_TEMPLATE_PARM:
a1281f45 10687 case BOUND_TEMPLATE_TEMPLATE_PARM:
f84b4be9 10688 case TEMPLATE_PARM_INDEX:
5566b478
MS
10689 case POINTER_TYPE:
10690 case REFERENCE_TYPE:
10691 case OFFSET_TYPE:
10692 case FUNCTION_TYPE:
10693 case METHOD_TYPE:
10694 case ARRAY_TYPE:
10695 case TYPENAME_TYPE:
b8c6534b 10696 case UNBOUND_CLASS_TEMPLATE:
2a2b1d56 10697 case TYPEOF_TYPE:
3ad6a8e1 10698 case DECLTYPE_TYPE:
f84b4be9 10699 case TYPE_DECL:
4393e105 10700 return tsubst (t, args, complain, in_decl);
5566b478 10701
e92cc029 10702 case IDENTIFIER_NODE:
421844e7 10703 if (IDENTIFIER_TYPENAME_P (t))
1f6e1acc
AS
10704 {
10705 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
1f84ec23 10706 return mangle_conv_op_name_for_type (new_type);
1f6e1acc 10707 }
e92cc029
MS
10708 else
10709 return t;
10710
5156628f 10711 case CONSTRUCTOR:
4038c495
GB
10712 /* This is handled by tsubst_copy_and_build. */
10713 gcc_unreachable ();
5156628f 10714
371534a9 10715 case VA_ARG_EXPR:
ea333e1c 10716 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
c2ea3a40
NS
10717 in_decl),
10718 tsubst (TREE_TYPE (t), args, complain, in_decl));
f9817201 10719
543a0daa
RH
10720 case CLEANUP_POINT_EXPR:
10721 /* We shouldn't have built any of these during initial template
10722 generation. Instead, they should be built during instantiation
10723 in response to the saved STMT_IS_FULL_EXPR_P setting. */
315fb5db 10724 gcc_unreachable ();
543a0daa 10725
7eab6e7b
MM
10726 case OFFSET_REF:
10727 mark_used (TREE_OPERAND (t, 1));
10728 return t;
10729
5d80a306
DG
10730 case EXPR_PACK_EXPANSION:
10731 error ("invalid use of pack expansion expression");
10732 return error_mark_node;
10733
10734 case NONTYPE_ARGUMENT_PACK:
10735 error ("use %<...%> to expand argument pack");
10736 return error_mark_node;
10737
5566b478
MS
10738 default:
10739 return t;
10740 }
10741}
10742
1799e5d5
RH
10743/* Like tsubst_copy, but specifically for OpenMP clauses. */
10744
10745static tree
10746tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
10747 tree in_decl)
10748{
10749 tree new_clauses = NULL, nc, oc;
10750
10751 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
10752 {
10753 nc = copy_node (oc);
10754 OMP_CLAUSE_CHAIN (nc) = new_clauses;
10755 new_clauses = nc;
10756
10757 switch (OMP_CLAUSE_CODE (nc))
10758 {
a68ab351
JJ
10759 case OMP_CLAUSE_LASTPRIVATE:
10760 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
10761 {
10762 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
10763 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
10764 in_decl, /*integral_constant_expression_p=*/false);
10765 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
10766 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
10767 }
10768 /* FALLTHRU */
1799e5d5
RH
10769 case OMP_CLAUSE_PRIVATE:
10770 case OMP_CLAUSE_SHARED:
10771 case OMP_CLAUSE_FIRSTPRIVATE:
1799e5d5
RH
10772 case OMP_CLAUSE_REDUCTION:
10773 case OMP_CLAUSE_COPYIN:
10774 case OMP_CLAUSE_COPYPRIVATE:
10775 case OMP_CLAUSE_IF:
10776 case OMP_CLAUSE_NUM_THREADS:
10777 case OMP_CLAUSE_SCHEDULE:
a68ab351 10778 case OMP_CLAUSE_COLLAPSE:
1799e5d5 10779 OMP_CLAUSE_OPERAND (nc, 0)
015c2c66
MM
10780 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
10781 in_decl, /*integral_constant_expression_p=*/false);
1799e5d5
RH
10782 break;
10783 case OMP_CLAUSE_NOWAIT:
10784 case OMP_CLAUSE_ORDERED:
10785 case OMP_CLAUSE_DEFAULT:
a68ab351 10786 case OMP_CLAUSE_UNTIED:
1799e5d5
RH
10787 break;
10788 default:
10789 gcc_unreachable ();
10790 }
10791 }
10792
10793 return finish_omp_clauses (nreverse (new_clauses));
10794}
10795
f74dcfb7
JJ
10796/* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
10797
10798static tree
10799tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
10800 tree in_decl)
10801{
10802#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
10803
10804 tree purpose, value, chain;
10805
10806 if (t == NULL)
10807 return t;
10808
10809 if (TREE_CODE (t) != TREE_LIST)
10810 return tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
10811 /*function_p=*/false,
10812 /*integral_constant_expression_p=*/false);
f74dcfb7
JJ
10813
10814 if (t == void_list_node)
10815 return t;
10816
10817 purpose = TREE_PURPOSE (t);
10818 if (purpose)
10819 purpose = RECUR (purpose);
10820 value = TREE_VALUE (t);
10821 if (value)
10822 value = RECUR (value);
10823 chain = TREE_CHAIN (t);
10824 if (chain && chain != void_type_node)
10825 chain = RECUR (chain);
10826 return tree_cons (purpose, value, chain);
10827#undef RECUR
10828}
10829
a68ab351
JJ
10830/* Substitute one OMP_FOR iterator. */
10831
10832static void
10833tsubst_omp_for_iterator (tree t, int i, tree declv, tree initv,
10834 tree condv, tree incrv, tree *clauses,
10835 tree args, tsubst_flags_t complain, tree in_decl,
10836 bool integral_constant_expression_p)
10837{
10838#define RECUR(NODE) \
10839 tsubst_expr ((NODE), args, complain, in_decl, \
10840 integral_constant_expression_p)
a2d4cdc9 10841 tree decl, init, cond, incr, auto_node;
a68ab351
JJ
10842
10843 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
10844 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
10845 decl = RECUR (TREE_OPERAND (init, 0));
10846 init = TREE_OPERAND (init, 1);
a2d4cdc9
JJ
10847 auto_node = type_uses_auto (TREE_TYPE (decl));
10848 if (auto_node && init)
10849 {
10850 tree init_expr = init;
a2d4cdc9
JJ
10851 if (TREE_CODE (init_expr) == DECL_EXPR)
10852 init_expr = DECL_INITIAL (DECL_EXPR_DECL (init_expr));
749db154 10853 init_expr = RECUR (init_expr);
a2d4cdc9
JJ
10854 TREE_TYPE (decl)
10855 = do_auto_deduction (TREE_TYPE (decl), init_expr, auto_node);
a2d4cdc9 10856 }
a68ab351
JJ
10857 gcc_assert (!type_dependent_expression_p (decl));
10858
10859 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
10860 {
10861 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
10862 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10863 if (TREE_CODE (incr) == MODIFY_EXPR)
10864 incr = build_x_modify_expr (RECUR (TREE_OPERAND (incr, 0)), NOP_EXPR,
10865 RECUR (TREE_OPERAND (incr, 1)),
10866 complain);
10867 else
10868 incr = RECUR (incr);
10869 TREE_VEC_ELT (declv, i) = decl;
10870 TREE_VEC_ELT (initv, i) = init;
10871 TREE_VEC_ELT (condv, i) = cond;
10872 TREE_VEC_ELT (incrv, i) = incr;
10873 return;
10874 }
10875
10876 if (init && TREE_CODE (init) != DECL_EXPR)
10877 {
10878 tree c;
10879 for (c = *clauses; c ; c = OMP_CLAUSE_CHAIN (c))
10880 {
10881 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
10882 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
10883 && OMP_CLAUSE_DECL (c) == decl)
10884 break;
10885 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10886 && OMP_CLAUSE_DECL (c) == decl)
10887 error ("iteration variable %qD should not be firstprivate", decl);
10888 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
10889 && OMP_CLAUSE_DECL (c) == decl)
10890 error ("iteration variable %qD should not be reduction", decl);
10891 }
10892 if (c == NULL)
10893 {
c2255bc4 10894 c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
a68ab351
JJ
10895 OMP_CLAUSE_DECL (c) = decl;
10896 c = finish_omp_clauses (c);
10897 if (c)
10898 {
10899 OMP_CLAUSE_CHAIN (c) = *clauses;
10900 *clauses = c;
10901 }
10902 }
10903 }
10904 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
10905 if (COMPARISON_CLASS_P (cond))
10906 cond = build2 (TREE_CODE (cond), boolean_type_node,
10907 RECUR (TREE_OPERAND (cond, 0)),
10908 RECUR (TREE_OPERAND (cond, 1)));
10909 else
10910 cond = RECUR (cond);
10911 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
10912 switch (TREE_CODE (incr))
10913 {
10914 case PREINCREMENT_EXPR:
10915 case PREDECREMENT_EXPR:
10916 case POSTINCREMENT_EXPR:
10917 case POSTDECREMENT_EXPR:
10918 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
10919 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
10920 break;
10921 case MODIFY_EXPR:
10922 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10923 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10924 {
10925 tree rhs = TREE_OPERAND (incr, 1);
10926 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10927 RECUR (TREE_OPERAND (incr, 0)),
10928 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10929 RECUR (TREE_OPERAND (rhs, 0)),
10930 RECUR (TREE_OPERAND (rhs, 1))));
10931 }
10932 else
10933 incr = RECUR (incr);
10934 break;
10935 case MODOP_EXPR:
10936 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
10937 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
10938 {
10939 tree lhs = RECUR (TREE_OPERAND (incr, 0));
10940 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
10941 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
10942 TREE_TYPE (decl), lhs,
10943 RECUR (TREE_OPERAND (incr, 2))));
10944 }
10945 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
10946 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
10947 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
10948 {
10949 tree rhs = TREE_OPERAND (incr, 2);
10950 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl),
10951 RECUR (TREE_OPERAND (incr, 0)),
10952 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
10953 RECUR (TREE_OPERAND (rhs, 0)),
10954 RECUR (TREE_OPERAND (rhs, 1))));
10955 }
10956 else
10957 incr = RECUR (incr);
10958 break;
10959 default:
10960 incr = RECUR (incr);
10961 break;
10962 }
10963
10964 TREE_VEC_ELT (declv, i) = decl;
10965 TREE_VEC_ELT (initv, i) = init;
10966 TREE_VEC_ELT (condv, i) = cond;
10967 TREE_VEC_ELT (incrv, i) = incr;
10968#undef RECUR
10969}
10970
cc23546e
JO
10971/* Like tsubst_copy for expressions, etc. but also does semantic
10972 processing. */
00d3396f 10973
14d22dd6 10974static tree
015c2c66
MM
10975tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
10976 bool integral_constant_expression_p)
5566b478 10977{
015c2c66
MM
10978#define RECUR(NODE) \
10979 tsubst_expr ((NODE), args, complain, in_decl, \
10980 integral_constant_expression_p)
10981
fd10dd09 10982 tree stmt, tmp;
558475f0 10983
5566b478
MS
10984 if (t == NULL_TREE || t == error_mark_node)
10985 return t;
10986
93409b8c
PB
10987 if (EXPR_HAS_LOCATION (t))
10988 input_location = EXPR_LOCATION (t);
7c34ced1
RH
10989 if (STATEMENT_CODE_P (TREE_CODE (t)))
10990 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
10991
5566b478 10992 switch (TREE_CODE (t))
8d08fdba 10993 {
325c3691
RH
10994 case STATEMENT_LIST:
10995 {
10996 tree_stmt_iterator i;
10997 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
015c2c66 10998 RECUR (tsi_stmt (i));
325c3691
RH
10999 break;
11000 }
11001
558475f0 11002 case CTOR_INITIALIZER:
c8094d83 11003 finish_mem_initializers (tsubst_initializer_list
2282d28d
MM
11004 (TREE_OPERAND (t, 0), args));
11005 break;
558475f0 11006
5088b058 11007 case RETURN_EXPR:
015c2c66 11008 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
5566b478
MS
11009 break;
11010
11011 case EXPR_STMT:
015c2c66 11012 tmp = RECUR (EXPR_STMT_EXPR (t));
7c34ced1
RH
11013 if (EXPR_STMT_STMT_EXPR_RESULT (t))
11014 finish_stmt_expr_expr (tmp, cur_stmt_expr);
11015 else
11016 finish_expr_stmt (tmp);
11017 break;
5566b478 11018
9da99f7d 11019 case USING_STMT:
015c2c66 11020 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
9da99f7d 11021 break;
c8094d83 11022
350fae66 11023 case DECL_EXPR:
5566b478 11024 {
e0942dcd
MM
11025 tree decl;
11026 tree init;
5566b478 11027
350fae66 11028 decl = DECL_EXPR_DECL (t);
acef433b
MM
11029 if (TREE_CODE (decl) == LABEL_DECL)
11030 finish_label_decl (DECL_NAME (decl));
fbfe8c9e
NS
11031 else if (TREE_CODE (decl) == USING_DECL)
11032 {
98ed9dae 11033 tree scope = USING_DECL_SCOPE (decl);
fbfe8c9e 11034 tree name = DECL_NAME (decl);
22038b2c 11035 tree decl;
c8094d83 11036
015c2c66 11037 scope = RECUR (scope);
22038b2c 11038 decl = lookup_qualified_name (scope, name,
12483c9f
NS
11039 /*is_type_p=*/false,
11040 /*complain=*/false);
8f78f01f 11041 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
2b7a3abf 11042 qualified_name_lookup_error (scope, name, decl, input_location);
22038b2c 11043 else
ed5f054f 11044 do_local_using_decl (decl, scope, name);
fbfe8c9e 11045 }
acef433b
MM
11046 else
11047 {
11048 init = DECL_INITIAL (decl);
11049 decl = tsubst (decl, args, complain, in_decl);
ce2e5191
NS
11050 if (decl != error_mark_node)
11051 {
0cbd7506
MS
11052 /* By marking the declaration as instantiated, we avoid
11053 trying to instantiate it. Since instantiate_decl can't
11054 handle local variables, and since we've already done
11055 all that needs to be done, that's the right thing to
11056 do. */
11057 if (TREE_CODE (decl) == VAR_DECL)
11058 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
edebf865
MM
11059 if (TREE_CODE (decl) == VAR_DECL
11060 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
11061 /* Anonymous aggregates are a special case. */
11062 finish_anon_union (decl);
c8094d83 11063 else
ed2fa432 11064 {
edebf865 11065 maybe_push_decl (decl);
39703eb9
MM
11066 if (TREE_CODE (decl) == VAR_DECL
11067 && DECL_PRETTY_FUNCTION_P (decl))
edebf865
MM
11068 {
11069 /* For __PRETTY_FUNCTION__ we have to adjust the
11070 initializer. */
11071 const char *const name
11072 = cxx_printable_name (current_function_decl, 2);
bb885938 11073 init = cp_fname_init (name, &TREE_TYPE (decl));
edebf865
MM
11074 }
11075 else
268127ce
DG
11076 {
11077 tree t = RECUR (init);
11078
11079 if (init && !t)
11080 /* If we had an initializer but it
11081 instantiated to nothing,
11082 value-initialize the object. This will
11083 only occur when the initializer was a
11084 pack expansion where the parameter packs
11085 used in that expansion were of length
11086 zero. */
9dbd4406 11087 init = build_value_init (TREE_TYPE (decl));
268127ce
DG
11088 else
11089 init = t;
11090 }
11091
3600f678 11092 cp_finish_decl (decl, init, false, NULL_TREE, 0);
ed2fa432 11093 }
ce2e5191 11094 }
acef433b 11095 }
fd10dd09 11096
350fae66 11097 /* A DECL_EXPR can also be used as an expression, in the condition
325c3691
RH
11098 clause of an if/for/while construct. */
11099 return decl;
5566b478 11100 }
8d08fdba 11101
5566b478 11102 case FOR_STMT:
7c34ced1 11103 stmt = begin_for_stmt ();
015c2c66 11104 RECUR (FOR_INIT_STMT (t));
7c34ced1 11105 finish_for_init_stmt (stmt);
015c2c66 11106 tmp = RECUR (FOR_COND (t));
7c34ced1 11107 finish_for_cond (tmp, stmt);
015c2c66 11108 tmp = RECUR (FOR_EXPR (t));
7c34ced1 11109 finish_for_expr (tmp, stmt);
015c2c66 11110 RECUR (FOR_BODY (t));
7c34ced1 11111 finish_for_stmt (stmt);
5566b478 11112 break;
8d08fdba 11113
5566b478 11114 case WHILE_STMT:
7c34ced1 11115 stmt = begin_while_stmt ();
015c2c66 11116 tmp = RECUR (WHILE_COND (t));
7c34ced1 11117 finish_while_stmt_cond (tmp, stmt);
015c2c66 11118 RECUR (WHILE_BODY (t));
7c34ced1 11119 finish_while_stmt (stmt);
5566b478 11120 break;
8d08fdba 11121
5566b478 11122 case DO_STMT:
7c34ced1 11123 stmt = begin_do_stmt ();
015c2c66 11124 RECUR (DO_BODY (t));
7c34ced1 11125 finish_do_body (stmt);
015c2c66 11126 tmp = RECUR (DO_COND (t));
7c34ced1 11127 finish_do_stmt (tmp, stmt);
5566b478 11128 break;
a0a33927 11129
5566b478 11130 case IF_STMT:
7c34ced1 11131 stmt = begin_if_stmt ();
015c2c66 11132 tmp = RECUR (IF_COND (t));
7c34ced1 11133 finish_if_stmt_cond (tmp, stmt);
015c2c66 11134 RECUR (THEN_CLAUSE (t));
7c34ced1 11135 finish_then_clause (stmt);
8d08fdba 11136
7c34ced1
RH
11137 if (ELSE_CLAUSE (t))
11138 {
11139 begin_else_clause (stmt);
015c2c66 11140 RECUR (ELSE_CLAUSE (t));
7c34ced1
RH
11141 finish_else_clause (stmt);
11142 }
11143
11144 finish_if_stmt (stmt);
5566b478 11145 break;
8d08fdba 11146
5882f0f3 11147 case BIND_EXPR:
7c34ced1
RH
11148 if (BIND_EXPR_BODY_BLOCK (t))
11149 stmt = begin_function_body ();
11150 else
11151 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
11152 ? BCS_TRY_BLOCK : 0);
ade3dc07 11153
015c2c66 11154 RECUR (BIND_EXPR_BODY (t));
ade3dc07 11155
7c34ced1
RH
11156 if (BIND_EXPR_BODY_BLOCK (t))
11157 finish_function_body (stmt);
11158 else
11159 finish_compound_stmt (stmt);
5566b478 11160 break;
8d08fdba 11161
5566b478 11162 case BREAK_STMT:
ad321293 11163 finish_break_stmt ();
5566b478 11164 break;
8d08fdba 11165
6467930b 11166 case CONTINUE_STMT:
ad321293 11167 finish_continue_stmt ();
6467930b
MS
11168 break;
11169
5566b478 11170 case SWITCH_STMT:
7c34ced1 11171 stmt = begin_switch_stmt ();
015c2c66 11172 tmp = RECUR (SWITCH_STMT_COND (t));
7c34ced1 11173 finish_switch_cond (tmp, stmt);
015c2c66 11174 RECUR (SWITCH_STMT_BODY (t));
7c34ced1 11175 finish_switch_stmt (stmt);
5566b478
MS
11176 break;
11177
8c161995 11178 case CASE_LABEL_EXPR:
c2255bc4
AH
11179 finish_case_label (EXPR_LOCATION (t),
11180 RECUR (CASE_LOW (t)),
015c2c66 11181 RECUR (CASE_HIGH (t)));
5566b478
MS
11182 break;
11183
9e14e18f 11184 case LABEL_EXPR:
5bca4e80
ILT
11185 {
11186 tree decl = LABEL_EXPR_LABEL (t);
11187 tree label;
11188
11189 label = finish_label_stmt (DECL_NAME (decl));
11190 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
11191 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
11192 }
5566b478
MS
11193 break;
11194
9e14e18f 11195 case GOTO_EXPR:
fd10dd09
JM
11196 tmp = GOTO_DESTINATION (t);
11197 if (TREE_CODE (tmp) != LABEL_DECL)
aa09da44
MM
11198 /* Computed goto's must be tsubst'd into. On the other hand,
11199 non-computed gotos must not be; the identifier in question
11200 will have no binding. */
015c2c66 11201 tmp = RECUR (tmp);
3fa56191 11202 else
fd10dd09
JM
11203 tmp = DECL_NAME (tmp);
11204 finish_goto_stmt (tmp);
ad321293
MM
11205 break;
11206
e130a54b 11207 case ASM_EXPR:
c87978aa 11208 tmp = finish_asm_stmt
6de9cd9a 11209 (ASM_VOLATILE_P (t),
015c2c66 11210 RECUR (ASM_STRING (t)),
f74dcfb7
JJ
11211 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
11212 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
11213 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
131263fa
AP
11214 {
11215 tree asm_expr = tmp;
11216 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
11217 asm_expr = TREE_OPERAND (asm_expr, 0);
11218 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
11219 }
5566b478 11220 break;
faf5394a
MS
11221
11222 case TRY_BLOCK:
f1dedc31 11223 if (CLEANUP_P (t))
62409b39 11224 {
57b52417 11225 stmt = begin_try_block ();
015c2c66 11226 RECUR (TRY_STMTS (t));
62409b39 11227 finish_cleanup_try_block (stmt);
015c2c66 11228 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
62409b39 11229 }
f1dedc31
MM
11230 else
11231 {
eaf6fb90
MM
11232 tree compound_stmt = NULL_TREE;
11233
62409b39 11234 if (FN_TRY_BLOCK_P (t))
eaf6fb90 11235 stmt = begin_function_try_block (&compound_stmt);
62409b39
MM
11236 else
11237 stmt = begin_try_block ();
11238
015c2c66 11239 RECUR (TRY_STMTS (t));
62409b39
MM
11240
11241 if (FN_TRY_BLOCK_P (t))
11242 finish_function_try_block (stmt);
11243 else
11244 finish_try_block (stmt);
11245
015c2c66 11246 RECUR (TRY_HANDLERS (t));
84df082b 11247 if (FN_TRY_BLOCK_P (t))
eaf6fb90 11248 finish_function_handler_sequence (stmt, compound_stmt);
84df082b
MM
11249 else
11250 finish_handler_sequence (stmt);
f1dedc31 11251 }
faf5394a 11252 break;
c8094d83 11253
faf5394a 11254 case HANDLER:
b35d4555 11255 {
3a2419a7 11256 tree decl = HANDLER_PARMS (t);
b35d4555 11257
3a2419a7 11258 if (decl)
b35d4555 11259 {
b35d4555 11260 decl = tsubst (decl, args, complain, in_decl);
f8191e64
MM
11261 /* Prevent instantiate_decl from trying to instantiate
11262 this variable. We've already done all that needs to be
11263 done. */
3a2419a7
VR
11264 if (decl != error_mark_node)
11265 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
b35d4555 11266 }
3a2419a7 11267 stmt = begin_handler ();
1a6025b4 11268 finish_handler_parms (decl, stmt);
015c2c66 11269 RECUR (HANDLER_BODY (t));
1a6025b4 11270 finish_handler (stmt);
b35d4555 11271 }
faf5394a
MS
11272 break;
11273
b87692e5 11274 case TAG_DEFN:
fd10dd09 11275 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
b87692e5 11276 break;
55a3debe
DG
11277
11278 case STATIC_ASSERT:
11279 {
11280 tree condition =
11281 tsubst_expr (STATIC_ASSERT_CONDITION (t),
11282 args,
11283 complain, in_decl,
11284 /*integral_constant_expression_p=*/true);
11285 finish_static_assert (condition,
11286 STATIC_ASSERT_MESSAGE (t),
11287 STATIC_ASSERT_SOURCE_LOCATION (t),
11288 /*member_p=*/false);
11289 }
11290 break;
b87692e5 11291
1799e5d5
RH
11292 case OMP_PARALLEL:
11293 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
11294 args, complain, in_decl);
11295 stmt = begin_omp_parallel ();
015c2c66 11296 RECUR (OMP_PARALLEL_BODY (t));
761041be
JJ
11297 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
11298 = OMP_PARALLEL_COMBINED (t);
1799e5d5
RH
11299 break;
11300
a68ab351
JJ
11301 case OMP_TASK:
11302 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t),
11303 args, complain, in_decl);
11304 stmt = begin_omp_task ();
11305 RECUR (OMP_TASK_BODY (t));
11306 finish_omp_task (tmp, stmt);
11307 break;
11308
1799e5d5
RH
11309 case OMP_FOR:
11310 {
a68ab351
JJ
11311 tree clauses, body, pre_body;
11312 tree declv, initv, condv, incrv;
11313 int i;
1799e5d5
RH
11314
11315 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
11316 args, complain, in_decl);
a68ab351
JJ
11317 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11318 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11319 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11320 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
11321
11322 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
11323 tsubst_omp_for_iterator (t, i, declv, initv, condv, incrv,
11324 &clauses, args, complain, in_decl,
11325 integral_constant_expression_p);
1799e5d5
RH
11326
11327 stmt = begin_omp_structured_block ();
11328
a68ab351
JJ
11329 for (i = 0; i < TREE_VEC_LENGTH (initv); i++)
11330 if (TREE_VEC_ELT (initv, i) == NULL
11331 || TREE_CODE (TREE_VEC_ELT (initv, i)) != DECL_EXPR)
11332 TREE_VEC_ELT (initv, i) = RECUR (TREE_VEC_ELT (initv, i));
11333 else if (CLASS_TYPE_P (TREE_TYPE (TREE_VEC_ELT (initv, i))))
11334 {
11335 tree init = RECUR (TREE_VEC_ELT (initv, i));
11336 gcc_assert (init == TREE_VEC_ELT (declv, i));
11337 TREE_VEC_ELT (initv, i) = NULL_TREE;
11338 }
11339 else
11340 {
11341 tree decl_expr = TREE_VEC_ELT (initv, i);
11342 tree init = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
11343 gcc_assert (init != NULL);
11344 TREE_VEC_ELT (initv, i) = RECUR (init);
11345 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL;
11346 RECUR (decl_expr);
11347 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init;
11348 }
11349
1799e5d5 11350 pre_body = push_stmt_list ();
015c2c66 11351 RECUR (OMP_FOR_PRE_BODY (t));
1799e5d5
RH
11352 pre_body = pop_stmt_list (pre_body);
11353
11354 body = push_stmt_list ();
015c2c66 11355 RECUR (OMP_FOR_BODY (t));
1799e5d5
RH
11356 body = pop_stmt_list (body);
11357
a68ab351
JJ
11358 t = finish_omp_for (EXPR_LOCATION (t), declv, initv, condv, incrv,
11359 body, pre_body, clauses);
1799e5d5
RH
11360
11361 add_stmt (finish_omp_structured_block (stmt));
11362 }
11363 break;
11364
11365 case OMP_SECTIONS:
11366 case OMP_SINGLE:
11367 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
11368 stmt = push_stmt_list ();
015c2c66 11369 RECUR (OMP_BODY (t));
1799e5d5
RH
11370 stmt = pop_stmt_list (stmt);
11371
11372 t = copy_node (t);
11373 OMP_BODY (t) = stmt;
11374 OMP_CLAUSES (t) = tmp;
11375 add_stmt (t);
11376 break;
11377
11378 case OMP_SECTION:
11379 case OMP_CRITICAL:
11380 case OMP_MASTER:
11381 case OMP_ORDERED:
11382 stmt = push_stmt_list ();
015c2c66 11383 RECUR (OMP_BODY (t));
1799e5d5
RH
11384 stmt = pop_stmt_list (stmt);
11385
11386 t = copy_node (t);
11387 OMP_BODY (t) = stmt;
11388 add_stmt (t);
11389 break;
11390
11391 case OMP_ATOMIC:
239371f9
JJ
11392 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
11393 {
11394 tree op1 = TREE_OPERAND (t, 1);
11395 tree lhs = RECUR (TREE_OPERAND (op1, 0));
11396 tree rhs = RECUR (TREE_OPERAND (op1, 1));
11397 finish_omp_atomic (TREE_CODE (op1), lhs, rhs);
11398 }
1799e5d5
RH
11399 break;
11400
5d80a306
DG
11401 case EXPR_PACK_EXPANSION:
11402 error ("invalid use of pack expansion expression");
11403 return error_mark_node;
11404
11405 case NONTYPE_ARGUMENT_PACK:
11406 error ("use %<...%> to expand argument pack");
11407 return error_mark_node;
11408
5566b478 11409 default:
315fb5db 11410 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
c8094d83 11411
315fb5db 11412 return tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
11413 /*function_p=*/false,
11414 integral_constant_expression_p);
5566b478 11415 }
fd10dd09 11416
325c3691 11417 return NULL_TREE;
015c2c66 11418#undef RECUR
8d08fdba
MS
11419}
11420
b3445994
MM
11421/* T is a postfix-expression that is not being used in a function
11422 call. Return the substituted version of T. */
11423
11424static tree
c8094d83 11425tsubst_non_call_postfix_expression (tree t, tree args,
b3445994
MM
11426 tsubst_flags_t complain,
11427 tree in_decl)
11428{
11429 if (TREE_CODE (t) == SCOPE_REF)
11430 t = tsubst_qualified_id (t, args, complain, in_decl,
11431 /*done=*/false, /*address_p=*/false);
11432 else
11433 t = tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
11434 /*function_p=*/false,
11435 /*integral_constant_expression_p=*/false);
b3445994
MM
11436
11437 return t;
11438}
11439
cc23546e 11440/* Like tsubst but deals with expressions and performs semantic
b3445994 11441 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
cc23546e 11442
ee76b931 11443tree
c8094d83 11444tsubst_copy_and_build (tree t,
0cbd7506
MS
11445 tree args,
11446 tsubst_flags_t complain,
11447 tree in_decl,
015c2c66
MM
11448 bool function_p,
11449 bool integral_constant_expression_p)
cc23546e 11450{
015c2c66
MM
11451#define RECUR(NODE) \
11452 tsubst_copy_and_build (NODE, args, complain, in_decl, \
11453 /*function_p=*/false, \
11454 integral_constant_expression_p)
b3445994 11455
ee76b931
MM
11456 tree op1;
11457
cc23546e
JO
11458 if (t == NULL_TREE || t == error_mark_node)
11459 return t;
11460
11461 switch (TREE_CODE (t))
11462 {
399dedb9
NS
11463 case USING_DECL:
11464 t = DECL_NAME (t);
852dcbdd 11465 /* Fall through. */
b3445994 11466 case IDENTIFIER_NODE:
cc23546e 11467 {
b3445994 11468 tree decl;
b3445994 11469 cp_id_kind idk;
67c03833 11470 bool non_integral_constant_expression_p;
b3445994
MM
11471 const char *error_msg;
11472
b3445994 11473 if (IDENTIFIER_TYPENAME_P (t))
cc23546e 11474 {
b3445994
MM
11475 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11476 t = mangle_conv_op_name_for_type (new_type);
cc23546e 11477 }
b3445994
MM
11478
11479 /* Look up the name. */
10e6657a 11480 decl = lookup_name (t);
b3445994
MM
11481
11482 /* By convention, expressions use ERROR_MARK_NODE to indicate
11483 failure, not NULL_TREE. */
11484 if (decl == NULL_TREE)
11485 decl = error_mark_node;
11486
10b1d5e7 11487 decl = finish_id_expression (t, decl, NULL_TREE,
b3445994 11488 &idk,
015c2c66 11489 integral_constant_expression_p,
67c03833
JM
11490 /*allow_non_integral_constant_expression_p=*/false,
11491 &non_integral_constant_expression_p,
02ed62dd
MM
11492 /*template_p=*/false,
11493 /*done=*/true,
11494 /*address_p=*/false,
11495 /*template_arg_p=*/false,
2b7a3abf
DS
11496 &error_msg,
11497 input_location);
b3445994
MM
11498 if (error_msg)
11499 error (error_msg);
11500 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
11501 decl = unqualified_name_lookup_error (decl);
11502 return decl;
cc23546e
JO
11503 }
11504
11505 case TEMPLATE_ID_EXPR:
11506 {
11507 tree object;
be93747e 11508 tree templ = RECUR (TREE_OPERAND (t, 0));
bf12d54d
NS
11509 tree targs = TREE_OPERAND (t, 1);
11510
11511 if (targs)
11512 targs = tsubst_template_args (targs, args, complain, in_decl);
c8094d83 11513
be93747e 11514 if (TREE_CODE (templ) == COMPONENT_REF)
cc23546e 11515 {
be93747e
KG
11516 object = TREE_OPERAND (templ, 0);
11517 templ = TREE_OPERAND (templ, 1);
cc23546e
JO
11518 }
11519 else
11520 object = NULL_TREE;
be93747e 11521 templ = lookup_template_function (templ, targs);
c8094d83 11522
cc23546e 11523 if (object)
be93747e
KG
11524 return build3 (COMPONENT_REF, TREE_TYPE (templ),
11525 object, templ, NULL_TREE);
cc23546e 11526 else
be93747e 11527 return baselink_for_fns (templ);
cc23546e
JO
11528 }
11529
11530 case INDIRECT_REF:
db24eb1f
NS
11531 {
11532 tree r = RECUR (TREE_OPERAND (t, 0));
11533
11534 if (REFERENCE_REF_P (t))
11535 {
e8c66fe0
NS
11536 /* A type conversion to reference type will be enclosed in
11537 such an indirect ref, but the substitution of the cast
11538 will have also added such an indirect ref. */
11539 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
11540 r = convert_from_reference (r);
db24eb1f
NS
11541 }
11542 else
5ade1ed2 11543 r = build_x_indirect_ref (r, "unary *", complain);
db24eb1f
NS
11544 return r;
11545 }
cc23546e 11546
0da99d4e
GB
11547 case NOP_EXPR:
11548 return build_nop
11549 (tsubst (TREE_TYPE (t), args, complain, in_decl),
11550 RECUR (TREE_OPERAND (t, 0)));
11551
cc23546e 11552 case CAST_EXPR:
cc23546e 11553 case REINTERPRET_CAST_EXPR:
cc23546e 11554 case CONST_CAST_EXPR:
cc23546e 11555 case DYNAMIC_CAST_EXPR:
cc23546e 11556 case STATIC_CAST_EXPR:
015c2c66
MM
11557 {
11558 tree type;
11559 tree op;
11560
11561 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
11562 if (integral_constant_expression_p
11563 && !cast_valid_in_integral_constant_expression_p (type))
11564 {
5ade1ed2
DG
11565 if (complain & tf_error)
11566 error ("a cast to a type other than an integral or "
11567 "enumeration type cannot appear in a constant-expression");
015c2c66
MM
11568 return error_mark_node;
11569 }
11570
11571 op = RECUR (TREE_OPERAND (t, 0));
11572
11573 switch (TREE_CODE (t))
11574 {
11575 case CAST_EXPR:
5ade1ed2 11576 return build_functional_cast (type, op, complain);
015c2c66 11577 case REINTERPRET_CAST_EXPR:
5ade1ed2 11578 return build_reinterpret_cast (type, op, complain);
015c2c66 11579 case CONST_CAST_EXPR:
5ade1ed2 11580 return build_const_cast (type, op, complain);
015c2c66 11581 case DYNAMIC_CAST_EXPR:
5ade1ed2 11582 return build_dynamic_cast (type, op, complain);
015c2c66 11583 case STATIC_CAST_EXPR:
5ade1ed2 11584 return build_static_cast (type, op, complain);
015c2c66
MM
11585 default:
11586 gcc_unreachable ();
11587 }
11588 }
cc23546e 11589
cc23546e
JO
11590 case POSTDECREMENT_EXPR:
11591 case POSTINCREMENT_EXPR:
b3445994
MM
11592 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11593 args, complain, in_decl);
5ade1ed2 11594 return build_x_unary_op (TREE_CODE (t), op1, complain);
ee76b931
MM
11595
11596 case PREDECREMENT_EXPR:
11597 case PREINCREMENT_EXPR:
cc23546e
JO
11598 case NEGATE_EXPR:
11599 case BIT_NOT_EXPR:
cc23546e 11600 case ABS_EXPR:
d17811fd 11601 case TRUTH_NOT_EXPR:
392e3d51 11602 case UNARY_PLUS_EXPR: /* Unary + */
d17811fd
MM
11603 case REALPART_EXPR:
11604 case IMAGPART_EXPR:
5ade1ed2
DG
11605 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
11606 complain);
cc23546e 11607
cc23546e 11608 case ADDR_EXPR:
ee76b931
MM
11609 op1 = TREE_OPERAND (t, 0);
11610 if (TREE_CODE (op1) == SCOPE_REF)
c8094d83 11611 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
ee76b931
MM
11612 /*done=*/true, /*address_p=*/true);
11613 else
c8094d83 11614 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
b3445994 11615 in_decl);
fc2b8477 11616 if (TREE_CODE (op1) == LABEL_DECL)
6a3799eb
AH
11617 return finish_label_address_expr (DECL_NAME (op1),
11618 EXPR_LOCATION (op1));
5ade1ed2 11619 return build_x_unary_op (ADDR_EXPR, op1, complain);
ee76b931 11620
cc23546e
JO
11621 case PLUS_EXPR:
11622 case MINUS_EXPR:
11623 case MULT_EXPR:
11624 case TRUNC_DIV_EXPR:
11625 case CEIL_DIV_EXPR:
11626 case FLOOR_DIV_EXPR:
11627 case ROUND_DIV_EXPR:
11628 case EXACT_DIV_EXPR:
11629 case BIT_AND_EXPR:
cc23546e
JO
11630 case BIT_IOR_EXPR:
11631 case BIT_XOR_EXPR:
11632 case TRUNC_MOD_EXPR:
11633 case FLOOR_MOD_EXPR:
11634 case TRUTH_ANDIF_EXPR:
11635 case TRUTH_ORIF_EXPR:
11636 case TRUTH_AND_EXPR:
11637 case TRUTH_OR_EXPR:
11638 case RSHIFT_EXPR:
11639 case LSHIFT_EXPR:
11640 case RROTATE_EXPR:
11641 case LROTATE_EXPR:
11642 case EQ_EXPR:
11643 case NE_EXPR:
11644 case MAX_EXPR:
11645 case MIN_EXPR:
11646 case LE_EXPR:
11647 case GE_EXPR:
11648 case LT_EXPR:
11649 case GT_EXPR:
11650 case MEMBER_REF:
b3445994 11651 case DOTSTAR_EXPR:
cc23546e 11652 return build_x_binary_op
c8094d83 11653 (TREE_CODE (t),
b3445994 11654 RECUR (TREE_OPERAND (t, 0)),
2a67bec2
ILT
11655 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
11656 ? ERROR_MARK
11657 : TREE_CODE (TREE_OPERAND (t, 0))),
ec835fb2 11658 RECUR (TREE_OPERAND (t, 1)),
2a67bec2
ILT
11659 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
11660 ? ERROR_MARK
11661 : TREE_CODE (TREE_OPERAND (t, 1))),
5ade1ed2
DG
11662 /*overloaded_p=*/NULL,
11663 complain);
cc23546e
JO
11664
11665 case SCOPE_REF:
ee76b931
MM
11666 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
11667 /*address_p=*/false);
cc23546e 11668 case ARRAY_REF:
b3445994
MM
11669 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11670 args, complain, in_decl);
493e377c 11671 return build_x_array_ref (op1, RECUR (TREE_OPERAND (t, 1)), complain);
c8094d83 11672
cc23546e 11673 case SIZEOF_EXPR:
5d80a306 11674 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
063cc99c 11675 return tsubst_copy (t, args, complain, in_decl);
5d80a306
DG
11676 /* Fall through */
11677
cc23546e 11678 case ALIGNOF_EXPR:
d17811fd
MM
11679 op1 = TREE_OPERAND (t, 0);
11680 if (!args)
11681 {
11682 /* When there are no ARGS, we are trying to evaluate a
11683 non-dependent expression from the parser. Trying to do
11684 the substitutions may not work. */
11685 if (!TYPE_P (op1))
11686 op1 = TREE_TYPE (op1);
11687 }
11688 else
11689 {
7d882b83
ILT
11690 ++cp_unevaluated_operand;
11691 ++c_inhibit_evaluation_warnings;
015c2c66
MM
11692 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
11693 /*function_p=*/false,
11694 /*integral_constant_expression_p=*/false);
7d882b83
ILT
11695 --cp_unevaluated_operand;
11696 --c_inhibit_evaluation_warnings;
d17811fd 11697 }
7a18b933 11698 if (TYPE_P (op1))
5ade1ed2
DG
11699 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
11700 complain & tf_error);
d17811fd 11701 else
5ade1ed2
DG
11702 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
11703 complain & tf_error);
cc23546e
JO
11704
11705 case MODOP_EXPR:
e4c2c34b
JM
11706 {
11707 tree r = build_x_modify_expr
11708 (RECUR (TREE_OPERAND (t, 0)),
11709 TREE_CODE (TREE_OPERAND (t, 1)),
5ade1ed2
DG
11710 RECUR (TREE_OPERAND (t, 2)),
11711 complain);
bcf9a914
JM
11712 /* TREE_NO_WARNING must be set if either the expression was
11713 parenthesized or it uses an operator such as >>= rather
11714 than plain assignment. In the former case, it was already
11715 set and must be copied. In the latter case,
11716 build_x_modify_expr sets it and it must not be reset
11717 here. */
11718 if (TREE_NO_WARNING (t))
11719 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
e4c2c34b
JM
11720 return r;
11721 }
cc23546e
JO
11722
11723 case ARROW_EXPR:
b3445994
MM
11724 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
11725 args, complain, in_decl);
ee76b931
MM
11726 /* Remember that there was a reference to this entity. */
11727 if (DECL_P (op1))
11728 mark_used (op1);
11729 return build_x_arrow (op1);
cc23546e
JO
11730
11731 case NEW_EXPR:
268127ce 11732 {
c166b898 11733 tree placement = RECUR (TREE_OPERAND (t, 0));
268127ce 11734 tree init = RECUR (TREE_OPERAND (t, 3));
c166b898
ILT
11735 VEC(tree,gc) *placement_vec;
11736 VEC(tree,gc) *init_vec;
11737 tree ret;
268127ce 11738
c166b898
ILT
11739 if (placement == NULL_TREE)
11740 placement_vec = NULL;
11741 else
11742 {
11743 placement_vec = make_tree_vector ();
11744 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
11745 VEC_safe_push (tree, gc, placement_vec, TREE_VALUE (placement));
11746 }
268127ce 11747
c166b898
ILT
11748 /* If there was an initializer in the original tree, but it
11749 instantiated to an empty list, then we should pass a
11750 non-NULL empty vector to tell build_new that it was an
11751 empty initializer() rather than no initializer. This can
11752 only happen when the initializer is a pack expansion whose
11753 parameter packs are of length zero. */
11754 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
11755 init_vec = NULL;
11756 else
11757 {
11758 init_vec = make_tree_vector ();
11759 if (init == void_zero_node)
11760 gcc_assert (init_vec != NULL);
11761 else
11762 {
11763 for (; init != NULL_TREE; init = TREE_CHAIN (init))
11764 VEC_safe_push (tree, gc, init_vec, TREE_VALUE (init));
11765 }
11766 }
11767
11768 ret = build_new (&placement_vec,
11769 RECUR (TREE_OPERAND (t, 1)),
11770 RECUR (TREE_OPERAND (t, 2)),
11771 &init_vec,
11772 NEW_EXPR_USE_GLOBAL (t),
11773 complain);
11774
11775 if (placement_vec != NULL)
11776 release_tree_vector (placement_vec);
11777 if (init_vec != NULL)
11778 release_tree_vector (init_vec);
11779
11780 return ret;
268127ce 11781 }
cc23546e
JO
11782
11783 case DELETE_EXPR:
11784 return delete_sanity
b3445994
MM
11785 (RECUR (TREE_OPERAND (t, 0)),
11786 RECUR (TREE_OPERAND (t, 1)),
cc23546e
JO
11787 DELETE_EXPR_USE_VEC (t),
11788 DELETE_EXPR_USE_GLOBAL (t));
11789
11790 case COMPOUND_EXPR:
b3445994 11791 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
5ade1ed2
DG
11792 RECUR (TREE_OPERAND (t, 1)),
11793 complain);
cc23546e 11794
ee76b931 11795 case CALL_EXPR:
cc23546e 11796 {
ee76b931 11797 tree function;
c166b898
ILT
11798 VEC(tree,gc) *call_args;
11799 unsigned int nargs, i;
ee76b931 11800 bool qualified_p;
ee935db4 11801 bool koenig_p;
c166b898 11802 tree ret;
ee76b931 11803
5039610b 11804 function = CALL_EXPR_FN (t);
6d80c4b9
MM
11805 /* When we parsed the expression, we determined whether or
11806 not Koenig lookup should be performed. */
11807 koenig_p = KOENIG_LOOKUP_P (t);
ee76b931 11808 if (TREE_CODE (function) == SCOPE_REF)
cc23546e 11809 {
ee76b931
MM
11810 qualified_p = true;
11811 function = tsubst_qualified_id (function, args, complain, in_decl,
c8094d83 11812 /*done=*/false,
ee76b931 11813 /*address_p=*/false);
cc23546e 11814 }
ee76b931 11815 else
cc23546e 11816 {
61e71a9e
NS
11817 if (TREE_CODE (function) == COMPONENT_REF)
11818 {
11819 tree op = TREE_OPERAND (function, 1);
11820
11821 qualified_p = (TREE_CODE (op) == SCOPE_REF
11822 || (BASELINK_P (op)
11823 && BASELINK_QUALIFIED_P (op)));
11824 }
11825 else
11826 qualified_p = false;
3db45ab5 11827
c8094d83 11828 function = tsubst_copy_and_build (function, args, complain,
b3445994 11829 in_decl,
015c2c66
MM
11830 !qualified_p,
11831 integral_constant_expression_p);
11832
6d80c4b9
MM
11833 if (BASELINK_P (function))
11834 qualified_p = true;
cc23546e 11835 }
cc23546e 11836
c166b898
ILT
11837 nargs = call_expr_nargs (t);
11838 call_args = make_tree_vector ();
11839 for (i = 0; i < nargs; ++i)
11840 {
11841 tree arg = CALL_EXPR_ARG (t, i);
11842
11843 if (!PACK_EXPANSION_P (arg))
11844 VEC_safe_push (tree, gc, call_args,
11845 RECUR (CALL_EXPR_ARG (t, i)));
11846 else
11847 {
11848 /* Expand the pack expansion and push each entry onto
11849 CALL_ARGS. */
11850 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
11851 if (TREE_CODE (arg) == TREE_VEC)
11852 {
11853 unsigned int len, j;
11854
11855 len = TREE_VEC_LENGTH (arg);
11856 for (j = 0; j < len; ++j)
11857 {
11858 tree value = TREE_VEC_ELT (arg, j);
11859 if (value != NULL_TREE)
11860 value = convert_from_reference (value);
11861 VEC_safe_push (tree, gc, call_args, value);
11862 }
11863 }
11864 else
11865 {
11866 /* A partial substitution. Add one entry. */
11867 VEC_safe_push (tree, gc, call_args, arg);
11868 }
11869 }
11870 }
676e33ca
MM
11871
11872 /* We do not perform argument-dependent lookup if normal
11873 lookup finds a non-function, in accordance with the
11874 expected resolution of DR 218. */
ee935db4 11875 if (koenig_p
44370687
MM
11876 && ((is_overloaded_fn (function)
11877 /* If lookup found a member function, the Koenig lookup is
11878 not appropriate, even if an unqualified-name was used
11879 to denote the function. */
11880 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
5094a795
JM
11881 || TREE_CODE (function) == IDENTIFIER_NODE)
11882 /* Only do this when substitution turns a dependent call
11883 into a non-dependent call. */
11884 && type_dependent_expression_p_push (t)
11885 && !any_type_dependent_arguments_p (call_args))
6d80c4b9
MM
11886 function = perform_koenig_lookup (function, call_args);
11887
11888 if (TREE_CODE (function) == IDENTIFIER_NODE)
b3445994 11889 {
6d80c4b9 11890 unqualified_name_lookup_error (function);
c166b898 11891 release_tree_vector (call_args);
6d80c4b9 11892 return error_mark_node;
b3445994
MM
11893 }
11894
11895 /* Remember that there was a reference to this entity. */
11896 if (DECL_P (function))
11897 mark_used (function);
11898
d17811fd 11899 if (TREE_CODE (function) == OFFSET_REF)
c166b898
ILT
11900 ret = build_offset_ref_call_from_tree (function, &call_args);
11901 else if (TREE_CODE (function) == COMPONENT_REF)
9f880ef9
MM
11902 {
11903 if (!BASELINK_P (TREE_OPERAND (function, 1)))
c166b898 11904 ret = finish_call_expr (function, &call_args,
9f880ef9 11905 /*disallow_virtual=*/false,
5ade1ed2
DG
11906 /*koenig_p=*/false,
11907 complain);
9f880ef9 11908 else
c166b898 11909 ret = (build_new_method_call
9f880ef9
MM
11910 (TREE_OPERAND (function, 0),
11911 TREE_OPERAND (function, 1),
c166b898 11912 &call_args, NULL_TREE,
63c9a190 11913 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
5ade1ed2
DG
11914 /*fn_p=*/NULL,
11915 complain));
9f880ef9 11916 }
c166b898
ILT
11917 else
11918 ret = finish_call_expr (function, &call_args,
11919 /*disallow_virtual=*/qualified_p,
11920 koenig_p,
11921 complain);
11922
11923 release_tree_vector (call_args);
11924
11925 return ret;
cc23546e
JO
11926 }
11927
11928 case COND_EXPR:
11929 return build_x_conditional_expr
b3445994
MM
11930 (RECUR (TREE_OPERAND (t, 0)),
11931 RECUR (TREE_OPERAND (t, 1)),
5ade1ed2
DG
11932 RECUR (TREE_OPERAND (t, 2)),
11933 complain);
cc23546e
JO
11934
11935 case PSEUDO_DTOR_EXPR:
c8094d83 11936 return finish_pseudo_destructor_expr
b3445994
MM
11937 (RECUR (TREE_OPERAND (t, 0)),
11938 RECUR (TREE_OPERAND (t, 1)),
11939 RECUR (TREE_OPERAND (t, 2)));
cc23546e
JO
11940
11941 case TREE_LIST:
11942 {
11943 tree purpose, value, chain;
11944
11945 if (t == void_list_node)
11946 return t;
11947
5d80a306
DG
11948 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
11949 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
11950 {
11951 /* We have pack expansions, so expand those and
11952 create a new list out of it. */
11953 tree purposevec = NULL_TREE;
11954 tree valuevec = NULL_TREE;
11955 tree chain;
11956 int i, len = -1;
11957
11958 /* Expand the argument expressions. */
11959 if (TREE_PURPOSE (t))
11960 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
11961 complain, in_decl);
11962 if (TREE_VALUE (t))
11963 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
11964 complain, in_decl);
11965
11966 /* Build the rest of the list. */
11967 chain = TREE_CHAIN (t);
11968 if (chain && chain != void_type_node)
11969 chain = RECUR (chain);
11970
11971 /* Determine the number of arguments. */
11972 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
11973 {
11974 len = TREE_VEC_LENGTH (purposevec);
11975 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
11976 }
11977 else if (TREE_CODE (valuevec) == TREE_VEC)
11978 len = TREE_VEC_LENGTH (valuevec);
11979 else
11980 {
11981 /* Since we only performed a partial substitution into
11982 the argument pack, we only return a single list
11983 node. */
11984 if (purposevec == TREE_PURPOSE (t)
11985 && valuevec == TREE_VALUE (t)
11986 && chain == TREE_CHAIN (t))
11987 return t;
11988
11989 return tree_cons (purposevec, valuevec, chain);
11990 }
11991
11992 /* Convert the argument vectors into a TREE_LIST */
11993 i = len;
11994 while (i > 0)
11995 {
11996 /* Grab the Ith values. */
11997 i--;
11998 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
11999 : NULL_TREE;
12000 value
12001 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
12002 : NULL_TREE;
12003
12004 /* Build the list (backwards). */
12005 chain = tree_cons (purpose, value, chain);
12006 }
12007
12008 return chain;
12009 }
12010
cc23546e
JO
12011 purpose = TREE_PURPOSE (t);
12012 if (purpose)
b3445994 12013 purpose = RECUR (purpose);
cc23546e
JO
12014 value = TREE_VALUE (t);
12015 if (value)
b3445994 12016 value = RECUR (value);
cc23546e
JO
12017 chain = TREE_CHAIN (t);
12018 if (chain && chain != void_type_node)
b3445994 12019 chain = RECUR (chain);
cc23546e
JO
12020 if (purpose == TREE_PURPOSE (t)
12021 && value == TREE_VALUE (t)
12022 && chain == TREE_CHAIN (t))
12023 return t;
12024 return tree_cons (purpose, value, chain);
12025 }
12026
12027 case COMPONENT_REF:
12028 {
ee76b931 12029 tree object;
3897c0aa 12030 tree object_type;
ee76b931
MM
12031 tree member;
12032
b3445994
MM
12033 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
12034 args, complain, in_decl);
ee76b931
MM
12035 /* Remember that there was a reference to this entity. */
12036 if (DECL_P (object))
12037 mark_used (object);
3897c0aa 12038 object_type = TREE_TYPE (object);
ee76b931
MM
12039
12040 member = TREE_OPERAND (t, 1);
12041 if (BASELINK_P (member))
c8094d83 12042 member = tsubst_baselink (member,
ee76b931
MM
12043 non_reference (TREE_TYPE (object)),
12044 args, complain, in_decl);
12045 else
12046 member = tsubst_copy (member, args, complain, in_decl);
bad1f462
KL
12047 if (member == error_mark_node)
12048 return error_mark_node;
3897c0aa
MM
12049
12050 if (object_type && !CLASS_TYPE_P (object_type))
cc23546e 12051 {
3d9b2eb6
JJ
12052 if (SCALAR_TYPE_P (object_type))
12053 {
12054 tree s = NULL_TREE;
12055 tree dtor = member;
12056
12057 if (TREE_CODE (dtor) == SCOPE_REF)
12058 {
12059 s = TREE_OPERAND (dtor, 0);
12060 dtor = TREE_OPERAND (dtor, 1);
12061 }
12062 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
12063 {
12064 dtor = TREE_OPERAND (dtor, 0);
12065 if (TYPE_P (dtor))
12066 return finish_pseudo_destructor_expr (object, s, dtor);
12067 }
12068 }
cc23546e
JO
12069 }
12070 else if (TREE_CODE (member) == SCOPE_REF
12071 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
12072 {
12073 tree tmpl;
12074 tree args;
c8094d83 12075
cc23546e
JO
12076 /* Lookup the template functions now that we know what the
12077 scope is. */
12078 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
12079 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
c8094d83 12080 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12483c9f
NS
12081 /*is_type_p=*/false,
12082 /*complain=*/false);
cc23546e 12083 if (BASELINK_P (member))
44370687 12084 {
c8094d83 12085 BASELINK_FUNCTIONS (member)
44370687
MM
12086 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
12087 args);
c8094d83
MS
12088 member = (adjust_result_of_qualified_name_lookup
12089 (member, BINFO_TYPE (BASELINK_BINFO (member)),
3897c0aa 12090 object_type));
44370687 12091 }
cc23546e
JO
12092 else
12093 {
2b7a3abf
DS
12094 qualified_name_lookup_error (object_type, tmpl, member,
12095 input_location);
cc23546e
JO
12096 return error_mark_node;
12097 }
12098 }
2436b51f
MM
12099 else if (TREE_CODE (member) == SCOPE_REF
12100 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
12101 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
12102 {
12103 if (complain & tf_error)
12104 {
12105 if (TYPE_P (TREE_OPERAND (member, 0)))
c8094d83 12106 error ("%qT is not a class or namespace",
2436b51f
MM
12107 TREE_OPERAND (member, 0));
12108 else
c8094d83 12109 error ("%qD is not a class or namespace",
2436b51f
MM
12110 TREE_OPERAND (member, 0));
12111 }
12112 return error_mark_node;
12113 }
a3f10e50
NS
12114 else if (TREE_CODE (member) == FIELD_DECL)
12115 return finish_non_static_data_member (member, object, NULL_TREE);
cc23546e 12116
02ed62dd 12117 return finish_class_member_access_expr (object, member,
5ade1ed2
DG
12118 /*template_p=*/false,
12119 complain);
cc23546e
JO
12120 }
12121
12122 case THROW_EXPR:
12123 return build_throw
b3445994 12124 (RECUR (TREE_OPERAND (t, 0)));
cc23546e
JO
12125
12126 case CONSTRUCTOR:
12127 {
4038c495
GB
12128 VEC(constructor_elt,gc) *n;
12129 constructor_elt *ce;
12130 unsigned HOST_WIDE_INT idx;
cc23546e 12131 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
4038c495 12132 bool process_index_p;
5d80a306
DG
12133 int newlen;
12134 bool need_copy_p = false;
09357846 12135 tree r;
cc23546e 12136
a97728cf
VR
12137 if (type == error_mark_node)
12138 return error_mark_node;
12139
cc23546e
JO
12140 /* digest_init will do the wrong thing if we let it. */
12141 if (type && TYPE_PTRMEMFUNC_P (type))
12142 return t;
12143
4038c495 12144 /* We do not want to process the index of aggregate
cc23546e
JO
12145 initializers as they are identifier nodes which will be
12146 looked up by digest_init. */
9e1e64ec 12147 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
c8094d83 12148
4038c495 12149 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
5d80a306 12150 newlen = VEC_length (constructor_elt, n);
4038c495
GB
12151 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
12152 {
12153 if (ce->index && process_index_p)
12154 ce->index = RECUR (ce->index);
5d80a306
DG
12155
12156 if (PACK_EXPANSION_P (ce->value))
12157 {
12158 /* Substitute into the pack expansion. */
12159 ce->value = tsubst_pack_expansion (ce->value, args, complain,
12160 in_decl);
12161
4745e4eb
JJ
12162 if (ce->value == error_mark_node)
12163 ;
12164 else if (TREE_VEC_LENGTH (ce->value) == 1)
5d80a306
DG
12165 /* Just move the argument into place. */
12166 ce->value = TREE_VEC_ELT (ce->value, 0);
12167 else
12168 {
12169 /* Update the length of the final CONSTRUCTOR
12170 arguments vector, and note that we will need to
12171 copy.*/
12172 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
12173 need_copy_p = true;
12174 }
12175 }
12176 else
12177 ce->value = RECUR (ce->value);
cc23546e 12178 }
c8094d83 12179
5d80a306
DG
12180 if (need_copy_p)
12181 {
12182 VEC(constructor_elt,gc) *old_n = n;
12183
12184 n = VEC_alloc (constructor_elt, gc, newlen);
12185 for (idx = 0; VEC_iterate (constructor_elt, old_n, idx, ce);
12186 idx++)
12187 {
12188 if (TREE_CODE (ce->value) == TREE_VEC)
12189 {
12190 int i, len = TREE_VEC_LENGTH (ce->value);
12191 for (i = 0; i < len; ++i)
12192 CONSTRUCTOR_APPEND_ELT (n, 0,
12193 TREE_VEC_ELT (ce->value, i));
12194 }
12195 else
12196 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
12197 }
12198 }
12199
09357846 12200 r = build_constructor (init_list_type_node, n);
27fb09b7 12201 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
09357846 12202
05b22df9 12203 if (TREE_HAS_CONSTRUCTOR (t))
09357846 12204 return finish_compound_literal (type, r);
cc23546e 12205
09357846 12206 return r;
cc23546e
JO
12207 }
12208
12209 case TYPEID_EXPR:
12210 {
b3445994 12211 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
cc23546e
JO
12212 if (TYPE_P (operand_0))
12213 return get_typeid (operand_0);
12214 return build_typeid (operand_0);
12215 }
12216
cc23546e 12217 case VAR_DECL:
db24eb1f
NS
12218 if (!args)
12219 return t;
12220 /* Fall through */
c8094d83 12221
db24eb1f
NS
12222 case PARM_DECL:
12223 {
12224 tree r = tsubst_copy (t, args, complain, in_decl);
c8094d83 12225
db24eb1f
NS
12226 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
12227 /* If the original type was a reference, we'll be wrapped in
12228 the appropriate INDIRECT_REF. */
12229 r = convert_from_reference (r);
12230 return r;
12231 }
cc23546e
JO
12232
12233 case VA_ARG_EXPR:
b3445994 12234 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
c8094d83 12235 tsubst_copy (TREE_TYPE (t), args, complain,
b3445994 12236 in_decl));
cc23546e 12237
4bceb077 12238 case OFFSETOF_EXPR:
c291f8b1 12239 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
4bceb077 12240
cb68ec50
PC
12241 case TRAIT_EXPR:
12242 {
12243 tree type1 = tsubst_copy (TRAIT_EXPR_TYPE1 (t), args,
12244 complain, in_decl);
12245
12246 tree type2 = TRAIT_EXPR_TYPE2 (t);
12247 if (type2)
12248 type2 = tsubst_copy (type2, args, complain, in_decl);
12249
12250 return finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2);
12251 }
12252
0fe0caa6
RH
12253 case STMT_EXPR:
12254 {
12255 tree old_stmt_expr = cur_stmt_expr;
12256 tree stmt_expr = begin_stmt_expr ();
12257
12258 cur_stmt_expr = stmt_expr;
015c2c66
MM
12259 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
12260 integral_constant_expression_p);
0fe0caa6
RH
12261 stmt_expr = finish_stmt_expr (stmt_expr, false);
12262 cur_stmt_expr = old_stmt_expr;
12263
12264 return stmt_expr;
12265 }
12266
a5201a91
MM
12267 case CONST_DECL:
12268 t = tsubst_copy (t, args, complain, in_decl);
12269 /* As in finish_id_expression, we resolve enumeration constants
12270 to their underlying values. */
12271 if (TREE_CODE (t) == CONST_DECL)
6193b8b7
DJ
12272 {
12273 used_types_insert (TREE_TYPE (t));
12274 return DECL_INITIAL (t);
12275 }
a5201a91
MM
12276 return t;
12277
cc23546e 12278 default:
e58a9aa1
ZL
12279 /* Handle Objective-C++ constructs, if appropriate. */
12280 {
12281 tree subst
12282 = objcp_tsubst_copy_and_build (t, args, complain,
12283 in_decl, /*function_p=*/false);
12284 if (subst)
12285 return subst;
12286 }
cc23546e
JO
12287 return tsubst_copy (t, args, complain, in_decl);
12288 }
b3445994
MM
12289
12290#undef RECUR
cc23546e
JO
12291}
12292
3e4a3562 12293/* Verify that the instantiated ARGS are valid. For type arguments,
e8f43da6 12294 make sure that the type is not variably modified. For non-type arguments,
34cd5ae7 12295 make sure they are constants if they are integral or enumerations.
9bcb9aae 12296 Emit an error under control of COMPLAIN, and return TRUE on error. */
3e4a3562
NS
12297
12298static bool
ffaf1e05 12299check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
3e4a3562 12300{
ffaf1e05
JM
12301 if (ARGUMENT_PACK_P (t))
12302 {
12303 tree vec = ARGUMENT_PACK_ARGS (t);
12304 int len = TREE_VEC_LENGTH (vec);
12305 bool result = false;
12306 int i;
3e4a3562 12307
ffaf1e05
JM
12308 for (i = 0; i < len; ++i)
12309 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
12310 result = true;
12311 return result;
12312 }
12313 else if (TYPE_P (t))
3e4a3562 12314 {
e8f43da6 12315 if (variably_modified_type_p (t, NULL_TREE))
3e4a3562
NS
12316 {
12317 if (complain & tf_error)
ffaf1e05
JM
12318 error ("%qT is a variably modified type", t);
12319 return true;
3e4a3562
NS
12320 }
12321 }
ffaf1e05
JM
12322 /* A non-type argument of integral or enumerated type must be a
12323 constant. */
12324 else if (TREE_TYPE (t)
12325 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
12326 && !TREE_CONSTANT (t))
12327 {
12328 if (complain & tf_error)
12329 error ("integral expression %qE is not constant", t);
12330 return true;
12331 }
12332 return false;
12333}
12334
12335static bool
12336check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
12337{
12338 int ix, len = DECL_NTPARMS (tmpl);
12339 bool result = false;
12340
12341 for (ix = 0; ix != len; ix++)
12342 {
12343 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
12344 result = true;
12345 }
2010cdcd 12346 if (result && (complain & tf_error))
0f51ccfc 12347 error (" trying to instantiate %qD", tmpl);
3e4a3562
NS
12348 return result;
12349}
12350
6ba89f8e 12351/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
12352 the template arguments in TARG_PTR. */
12353
5566b478 12354tree
2b110bfc 12355instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
8d08fdba 12356{
2b110bfc 12357 tree targ_ptr = orig_args;
5566b478 12358 tree fndecl;
36a117a5
MM
12359 tree gen_tmpl;
12360 tree spec;
a95799ec 12361 HOST_WIDE_INT saved_processing_template_decl;
5566b478 12362
27fafc8d
JM
12363 if (tmpl == error_mark_node)
12364 return error_mark_node;
12365
50bc768d 12366 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
386b8a85 12367
db9b2174
MM
12368 /* If this function is a clone, handle it specially. */
12369 if (DECL_CLONED_FUNCTION_P (tmpl))
3ad97789 12370 {
a30f62e0 12371 tree spec;
3ad97789 12372 tree clone;
c8094d83 12373
b97e8a14
JM
12374 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
12375 DECL_CLONED_FUNCTION. */
12376 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
12377 targ_ptr, complain);
a30f62e0
MM
12378 if (spec == error_mark_node)
12379 return error_mark_node;
12380
c6002625 12381 /* Look for the clone. */
4684cd27 12382 FOR_EACH_CLONE (clone, spec)
3ad97789
NS
12383 if (DECL_NAME (clone) == DECL_NAME (tmpl))
12384 return clone;
12385 /* We should always have found the clone by now. */
315fb5db 12386 gcc_unreachable ();
3ad97789
NS
12387 return NULL_TREE;
12388 }
c8094d83 12389
36a117a5 12390 /* Check to see if we already have this specialization. */
f9a7ae04
MM
12391 gen_tmpl = most_general_template (tmpl);
12392 if (tmpl != gen_tmpl)
2b110bfc
JM
12393 /* The TMPL is a partial instantiation. To get a full set of
12394 arguments we must add the arguments used to perform the
12395 partial instantiation. */
12396 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
12397 targ_ptr);
36a117a5 12398
2b110bfc
JM
12399 /* It would be nice to avoid hashing here and then again in tsubst_decl,
12400 but it doesn't seem to be on the hot path. */
12401 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
12402
12403 gcc_assert (tmpl == gen_tmpl
12404 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
12405 == spec)
12406 || fndecl == NULL_TREE);
12407
12408 if (spec != NULL_TREE)
12409 return spec;
386b8a85 12410
3e4a3562
NS
12411 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
12412 complain))
12413 return error_mark_node;
c8094d83 12414
2b907f5c
KL
12415 /* We are building a FUNCTION_DECL, during which the access of its
12416 parameters and return types have to be checked. However this
12417 FUNCTION_DECL which is the desired context for access checking
12418 is not built yet. We solve this chicken-and-egg problem by
12419 deferring all checks until we have the FUNCTION_DECL. */
12420 push_deferring_access_checks (dk_deferred);
5c74d5b0 12421
a95799ec
MM
12422 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
12423 (because, for example, we have encountered a non-dependent
a1d08991
MM
12424 function call in the body of a template function and must now
12425 determine which of several overloaded functions will be called),
12426 within the instantiation itself we are not processing a
12427 template. */
a95799ec
MM
12428 saved_processing_template_decl = processing_template_decl;
12429 processing_template_decl = 0;
12430 /* Substitute template parameters to obtain the specialization. */
17aec3eb 12431 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
3e4a3562 12432 targ_ptr, complain, gen_tmpl);
a95799ec 12433 processing_template_decl = saved_processing_template_decl;
6b6b60af
MM
12434 if (fndecl == error_mark_node)
12435 return error_mark_node;
5c74d5b0 12436
2b907f5c
KL
12437 /* Now we know the specialization, compute access previously
12438 deferred. */
12439 push_access_scope (fndecl);
d0940d56
DS
12440
12441 /* Some typedefs referenced from within the template code need to be access
12442 checked at template instantiation time, i.e now. These types were
12443 added to the template at parsing time. Let's get those and perfom
12444 the acces checks then. */
2eb25c98 12445 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (tmpl), targ_ptr);
2b907f5c
KL
12446 perform_deferred_access_checks ();
12447 pop_access_scope (fndecl);
12448 pop_deferring_access_checks ();
5c74d5b0 12449
36a117a5
MM
12450 /* The DECL_TI_TEMPLATE should always be the immediate parent
12451 template, not the most general template. */
12452 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 12453
94350948 12454 /* If we've just instantiated the main entry point for a function,
3ad97789
NS
12455 instantiate all the alternate entry points as well. We do this
12456 by cloning the instantiation of the main entry point, not by
12457 instantiating the template clones. */
12458 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
12459 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
94350948 12460
5566b478 12461 return fndecl;
8d08fdba 12462}
5566b478 12463
c166b898
ILT
12464/* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
12465 NARGS elements of the arguments that are being used when calling
12466 it. TARGS is a vector into which the deduced template arguments
12467 are placed.
8d08fdba
MS
12468
12469 Return zero for success, 2 for an incomplete match that doesn't resolve
12470 all the types, and 1 for complete failure. An error message will be
12471 printed only for an incomplete match.
12472
e5214479
JM
12473 If FN is a conversion operator, or we are trying to produce a specific
12474 specialization, RETURN_TYPE is the return type desired.
9f54c803
MM
12475
12476 The EXPLICIT_TARGS are explicit template arguments provided via a
12477 template-id.
6467930b 12478
830bfa74
MM
12479 The parameter STRICT is one of:
12480
c8094d83 12481 DEDUCE_CALL:
830bfa74
MM
12482 We are deducing arguments for a function call, as in
12483 [temp.deduct.call].
12484
12485 DEDUCE_CONV:
c8094d83 12486 We are deducing arguments for a conversion function, as in
830bfa74
MM
12487 [temp.deduct.conv].
12488
12489 DEDUCE_EXACT:
62e4a758
NS
12490 We are deducing arguments when doing an explicit instantiation
12491 as in [temp.explicit], when determining an explicit specialization
12492 as in [temp.expl.spec], or when taking the address of a function
a34d3336 12493 template, as in [temp.deduct.funcaddr]. */
8d08fdba
MS
12494
12495int
c8094d83 12496fn_type_unification (tree fn,
0cbd7506
MS
12497 tree explicit_targs,
12498 tree targs,
c166b898
ILT
12499 const tree *args,
12500 unsigned int nargs,
0cbd7506 12501 tree return_type,
30f86ec3
FJ
12502 unification_kind_t strict,
12503 int flags)
386b8a85 12504{
4393e105
MM
12505 tree parms;
12506 tree fntype;
adecb3f4 12507 int result;
5d80a306 12508 bool incomplete_argument_packs_p = false;
386b8a85 12509
50bc768d 12510 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
2b59fc25 12511
4393e105
MM
12512 fntype = TREE_TYPE (fn);
12513 if (explicit_targs)
75650646 12514 {
4393e105 12515 /* [temp.deduct]
c8094d83 12516
4393e105
MM
12517 The specified template arguments must match the template
12518 parameters in kind (i.e., type, nontype, template), and there
12519 must not be more arguments than there are parameters;
12520 otherwise type deduction fails.
12521
12522 Nontype arguments must match the types of the corresponding
12523 nontype template parameters, or must be convertible to the
12524 types of the corresponding nontype parameters as specified in
12525 _temp.arg.nontype_, otherwise type deduction fails.
12526
12527 All references in the function type of the function template
12528 to the corresponding template parameters are replaced by the
12529 specified template argument values. If a substitution in a
12530 template parameter or in the function type of the function
12531 template results in an invalid type, type deduction fails. */
5d80a306
DG
12532 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
12533 int i, len = TREE_VEC_LENGTH (tparms);
4393e105 12534 tree converted_args;
5d80a306 12535 bool incomplete = false;
75650646 12536
c8a7ed43
AO
12537 if (explicit_targs == error_mark_node)
12538 return 1;
12539
4393e105 12540 converted_args
5d80a306 12541 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE, tf_none,
e7e93965
MM
12542 /*require_all_args=*/false,
12543 /*use_default_args=*/false));
4393e105 12544 if (converted_args == error_mark_node)
75650646 12545 return 1;
386b8a85 12546
ffd49b19 12547 /* Substitute the explicit args into the function type. This is
0cbd7506
MS
12548 necessary so that, for instance, explicitly declared function
12549 arguments can match null pointed constants. If we were given
12550 an incomplete set of explicit args, we must not do semantic
12551 processing during substitution as we could create partial
12552 instantiations. */
5d80a306
DG
12553 for (i = 0; i < len; i++)
12554 {
12555 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
12556 bool parameter_pack = false;
12557
12558 /* Dig out the actual parm. */
12559 if (TREE_CODE (parm) == TYPE_DECL
12560 || TREE_CODE (parm) == TEMPLATE_DECL)
12561 {
12562 parm = TREE_TYPE (parm);
12563 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
12564 }
12565 else if (TREE_CODE (parm) == PARM_DECL)
12566 {
12567 parm = DECL_INITIAL (parm);
12568 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
12569 }
12570
12571 if (parameter_pack)
12572 {
12573 int level, idx;
12574 tree targ;
12575 template_parm_level_and_index (parm, &level, &idx);
12576
12577 /* Mark the argument pack as "incomplete". We could
12578 still deduce more arguments during unification. */
12579 targ = TMPL_ARG (converted_args, level, idx);
b1d7b1c0
DG
12580 if (targ)
12581 {
12582 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
12583 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
12584 = ARGUMENT_PACK_ARGS (targ);
12585 }
5d80a306
DG
12586
12587 /* We have some incomplete argument packs. */
12588 incomplete_argument_packs_p = true;
12589 }
12590 }
12591
12592 if (incomplete_argument_packs_p)
12593 /* Any substitution is guaranteed to be incomplete if there
12594 are incomplete argument packs, because we can still deduce
12595 more arguments. */
12596 incomplete = 1;
12597 else
12598 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
12599
ffd49b19 12600 processing_template_decl += incomplete;
c2ea3a40 12601 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
ffd49b19 12602 processing_template_decl -= incomplete;
c8094d83 12603
4393e105
MM
12604 if (fntype == error_mark_node)
12605 return 1;
050367a3 12606
4393e105 12607 /* Place the explicitly specified arguments in TARGS. */
ffd49b19 12608 for (i = NUM_TMPL_ARGS (converted_args); i--;)
4393e105 12609 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
75650646 12610 }
c8094d83 12611
e5214479 12612 /* Never do unification on the 'this' parameter. */
ba139ba8 12613 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
c8094d83 12614
8d3631f8
NS
12615 if (return_type)
12616 {
c166b898
ILT
12617 tree *new_args;
12618
8d3631f8 12619 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
c166b898
ILT
12620 new_args = XALLOCAVEC (tree, nargs + 1);
12621 new_args[0] = return_type;
12622 memcpy (new_args + 1, args, nargs * sizeof (tree));
12623 args = new_args;
12624 ++nargs;
4393e105
MM
12625 }
12626
4393e105
MM
12627 /* We allow incomplete unification without an error message here
12628 because the standard doesn't seem to explicitly prohibit it. Our
12629 callers must be ready to deal with unification failures in any
12630 event. */
c8094d83 12631 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
c166b898 12632 targs, parms, args, nargs, /*subr=*/0,
30f86ec3 12633 strict, flags);
adecb3f4 12634
5d80a306
DG
12635 if (result == 0 && incomplete_argument_packs_p)
12636 {
12637 int i, len = NUM_TMPL_ARGS (targs);
12638
12639 /* Clear the "incomplete" flags on all argument packs. */
12640 for (i = 0; i < len; i++)
12641 {
12642 tree arg = TREE_VEC_ELT (targs, i);
12643 if (ARGUMENT_PACK_P (arg))
12644 {
12645 ARGUMENT_PACK_INCOMPLETE_P (arg) = 0;
12646 ARGUMENT_PACK_EXPLICIT_ARGS (arg) = NULL_TREE;
12647 }
12648 }
12649 }
12650
4cf36211
DG
12651 /* Now that we have bindings for all of the template arguments,
12652 ensure that the arguments deduced for the template template
12653 parameters have compatible template parameter lists. We cannot
12654 check this property before we have deduced all template
12655 arguments, because the template parameter types of a template
12656 template parameter might depend on prior template parameters
12657 deduced after the template template parameter. The following
12658 ill-formed example illustrates this issue:
12659
12660 template<typename T, template<T> class C> void f(C<5>, T);
12661
12662 template<int N> struct X {};
12663
12664 void g() {
12665 f(X<5>(), 5l); // error: template argument deduction fails
12666 }
12667
12668 The template parameter list of 'C' depends on the template type
12669 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
12670 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
12671 time that we deduce 'C'. */
12672 if (result == 0
12673 && !template_template_parm_bindings_ok_p
12674 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
12675 return 1;
12676
c8094d83 12677 if (result == 0)
adecb3f4 12678 /* All is well so far. Now, check:
c8094d83
MS
12679
12680 [temp.deduct]
12681
adecb3f4
MM
12682 When all template arguments have been deduced, all uses of
12683 template parameters in nondeduced contexts are replaced with
12684 the corresponding deduced argument values. If the
12685 substitution results in an invalid type, as described above,
12686 type deduction fails. */
463ecaca
JM
12687 {
12688 tree substed = tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE);
12689 if (substed == error_mark_node)
12690 return 1;
12691
12692 /* If we're looking for an exact match, check that what we got
12693 is indeed an exact match. It might not be if some template
12694 parameters are used in non-deduced contexts. */
12695 if (strict == DEDUCE_EXACT)
12696 {
c166b898
ILT
12697 unsigned int i;
12698
463ecaca
JM
12699 tree sarg
12700 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (substed));
463ecaca
JM
12701 if (return_type)
12702 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
c166b898
ILT
12703 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
12704 if (!same_type_p (args[i], TREE_VALUE (sarg)))
463ecaca
JM
12705 return 1;
12706 }
12707 }
adecb3f4
MM
12708
12709 return result;
830bfa74
MM
12710}
12711
12712/* Adjust types before performing type deduction, as described in
12713 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
12714 sections are symmetric. PARM is the type of a function parameter
12715 or the return type of the conversion function. ARG is the type of
12716 the argument passed to the call, or the type of the value
8af2fec4
RY
12717 initialized with the result of the conversion function.
12718 ARG_EXPR is the original argument expression, which may be null. */
386b8a85 12719
62e4a758 12720static int
c8094d83 12721maybe_adjust_types_for_deduction (unification_kind_t strict,
0cbd7506 12722 tree* parm,
8af2fec4
RY
12723 tree* arg,
12724 tree arg_expr)
830bfa74 12725{
62e4a758 12726 int result = 0;
c8094d83 12727
830bfa74
MM
12728 switch (strict)
12729 {
12730 case DEDUCE_CALL:
12731 break;
12732
12733 case DEDUCE_CONV:
12734 {
4c7d0dff
MM
12735 /* Swap PARM and ARG throughout the remainder of this
12736 function; the handling is precisely symmetric since PARM
12737 will initialize ARG rather than vice versa. */
830bfa74
MM
12738 tree* temp = parm;
12739 parm = arg;
12740 arg = temp;
12741 break;
12742 }
12743
12744 case DEDUCE_EXACT:
12745 /* There is nothing to do in this case. */
62e4a758 12746 return 0;
830bfa74
MM
12747
12748 default:
315fb5db 12749 gcc_unreachable ();
830bfa74
MM
12750 }
12751
12752 if (TREE_CODE (*parm) != REFERENCE_TYPE)
12753 {
12754 /* [temp.deduct.call]
c8094d83 12755
830bfa74 12756 If P is not a reference type:
c8094d83 12757
830bfa74
MM
12758 --If A is an array type, the pointer type produced by the
12759 array-to-pointer standard conversion (_conv.array_) is
12760 used in place of A for type deduction; otherwise,
c8094d83 12761
830bfa74
MM
12762 --If A is a function type, the pointer type produced by
12763 the function-to-pointer standard conversion
12764 (_conv.func_) is used in place of A for type deduction;
12765 otherwise,
c8094d83 12766
830bfa74
MM
12767 --If A is a cv-qualified type, the top level
12768 cv-qualifiers of A's type are ignored for type
12769 deduction. */
12770 if (TREE_CODE (*arg) == ARRAY_TYPE)
12771 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 12772 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
12773 *arg = build_pointer_type (*arg);
12774 else
12775 *arg = TYPE_MAIN_VARIANT (*arg);
12776 }
c8094d83 12777
8af2fec4
RY
12778 /* From C++0x [14.8.2.1/3 temp.deduct.call] (after DR606), "If P is
12779 of the form T&&, where T is a template parameter, and the argument
12780 is an lvalue, T is deduced as A& */
12781 if (TREE_CODE (*parm) == REFERENCE_TYPE
12782 && TYPE_REF_IS_RVALUE (*parm)
12783 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
12784 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
12785 && arg_expr && real_lvalue_p (arg_expr))
12786 *arg = build_reference_type (*arg);
12787
830bfa74 12788 /* [temp.deduct.call]
c8094d83 12789
830bfa74
MM
12790 If P is a cv-qualified type, the top level cv-qualifiers
12791 of P's type are ignored for type deduction. If P is a
12792 reference type, the type referred to by P is used for
12793 type deduction. */
12794 *parm = TYPE_MAIN_VARIANT (*parm);
12795 if (TREE_CODE (*parm) == REFERENCE_TYPE)
62e4a758
NS
12796 {
12797 *parm = TREE_TYPE (*parm);
12798 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
12799 }
1c82cc90
NS
12800
12801 /* DR 322. For conversion deduction, remove a reference type on parm
12802 too (which has been swapped into ARG). */
12803 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
12804 *arg = TREE_TYPE (*arg);
c8094d83 12805
62e4a758 12806 return result;
386b8a85
JM
12807}
12808
e5214479 12809/* Most parms like fn_type_unification.
9f54c803
MM
12810
12811 If SUBR is 1, we're being called recursively (to unify the
12812 arguments of a function or method parameter of a function
38d18b1a 12813 template). */
386b8a85 12814
4966381a 12815static int
c8094d83 12816type_unification_real (tree tparms,
0cbd7506
MS
12817 tree targs,
12818 tree xparms,
c166b898
ILT
12819 const tree *xargs,
12820 unsigned int xnargs,
0cbd7506 12821 int subr,
30f86ec3
FJ
12822 unification_kind_t strict,
12823 int flags)
8d08fdba 12824{
8af2fec4 12825 tree parm, arg, arg_expr;
8d08fdba
MS
12826 int i;
12827 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 12828 int sub_strict;
bd0d5d4a 12829 int saw_undeduced = 0;
c166b898
ILT
12830 tree parms;
12831 const tree *args;
12832 unsigned int nargs;
12833 unsigned int ia;
8d08fdba 12834
50bc768d
NS
12835 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
12836 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
50bc768d 12837 gcc_assert (ntparms > 0);
8d08fdba 12838
830bfa74
MM
12839 switch (strict)
12840 {
12841 case DEDUCE_CALL:
028d1f20 12842 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
0cbd7506 12843 | UNIFY_ALLOW_DERIVED);
830bfa74 12844 break;
c8094d83 12845
830bfa74
MM
12846 case DEDUCE_CONV:
12847 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
12848 break;
12849
12850 case DEDUCE_EXACT:
12851 sub_strict = UNIFY_ALLOW_NONE;
12852 break;
c8094d83 12853
830bfa74 12854 default:
315fb5db 12855 gcc_unreachable ();
830bfa74
MM
12856 }
12857
bd0d5d4a
JM
12858 again:
12859 parms = xparms;
12860 args = xargs;
c166b898 12861 nargs = xnargs;
bd0d5d4a 12862
c166b898 12863 ia = 0;
a34d3336 12864 while (parms && parms != void_list_node
c166b898 12865 && ia < nargs)
8d08fdba 12866 {
5d80a306
DG
12867 if (TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12868 break;
12869
8d08fdba
MS
12870 parm = TREE_VALUE (parms);
12871 parms = TREE_CHAIN (parms);
c166b898
ILT
12872 arg = args[ia];
12873 ++ia;
8af2fec4 12874 arg_expr = NULL;
8d08fdba
MS
12875
12876 if (arg == error_mark_node)
12877 return 1;
12878 if (arg == unknown_type_node)
34016c81
JM
12879 /* We can't deduce anything from this, but we might get all the
12880 template args from other function args. */
12881 continue;
b7484fbe 12882
03e70705
JM
12883 /* Conversions will be performed on a function argument that
12884 corresponds with a function parameter that contains only
12885 non-deducible template parameters and explicitly specified
12886 template parameters. */
c353b8e3 12887 if (!uses_template_parms (parm))
b7484fbe 12888 {
03e70705
JM
12889 tree type;
12890
2f939d94 12891 if (!TYPE_P (arg))
03e70705
JM
12892 type = TREE_TYPE (arg);
12893 else
c353b8e3 12894 type = arg;
03e70705 12895
fad86f7a 12896 if (same_type_p (parm, type))
343c89cd 12897 continue;
fad86f7a 12898 if (strict != DEDUCE_EXACT
3db45ab5 12899 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
30f86ec3 12900 flags))
fad86f7a 12901 continue;
3db45ab5 12902
b7484fbe
MS
12903 return 1;
12904 }
c8094d83 12905
2f939d94 12906 if (!TYPE_P (arg))
8d08fdba 12907 {
50bc768d 12908 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
34016c81 12909 if (type_unknown_p (arg))
28cbf42c 12910 {
acde59b4 12911 /* [temp.deduct.type]
34016c81 12912
acde59b4
MM
12913 A template-argument can be deduced from a pointer to
12914 function or pointer to member function argument if
12915 the set of overloaded functions does not contain
12916 function templates and at most one of a set of
12917 overloaded functions provides a unique match. */
34016c81 12918 if (resolve_overloaded_unification
acde59b4
MM
12919 (tparms, targs, parm, arg, strict, sub_strict))
12920 continue;
12921
12922 return 1;
28cbf42c 12923 }
8af2fec4 12924 arg_expr = arg;
f9aa54d3 12925 arg = unlowered_expr_type (arg);
08476342
NS
12926 if (arg == error_mark_node)
12927 return 1;
8d08fdba 12928 }
c8094d83 12929
62e4a758 12930 {
0cbd7506 12931 int arg_strict = sub_strict;
c8094d83 12932
0cbd7506 12933 if (!subr)
8af2fec4
RY
12934 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg,
12935 arg_expr);
4393e105 12936
09357846
JM
12937 if (arg == init_list_type_node && arg_expr)
12938 arg = arg_expr;
0cbd7506
MS
12939 if (unify (tparms, targs, parm, arg, arg_strict))
12940 return 1;
62e4a758 12941 }
8d08fdba 12942 }
c8094d83 12943
5d80a306
DG
12944
12945 if (parms
12946 && parms != void_list_node
12947 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
12948 {
12949 /* Unify the remaining arguments with the pack expansion type. */
12950 tree argvec;
12951 tree parmvec = make_tree_vec (1);
5d80a306 12952
5d80a306 12953 /* Allocate a TREE_VEC and copy in all of the arguments */
c166b898
ILT
12954 argvec = make_tree_vec (nargs - ia);
12955 for (i = 0; ia < nargs; ++ia, ++i)
12956 TREE_VEC_ELT (argvec, i) = args[ia];
5d80a306
DG
12957
12958 /* Copy the parameter into parmvec. */
12959 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
12960 if (unify_pack_expansion (tparms, targs, parmvec, argvec, strict,
12961 /*call_args_p=*/true, /*subr=*/subr))
12962 return 1;
12963
12964 /* Advance to the end of the list of parameters. */
12965 parms = TREE_CHAIN (parms);
12966 }
12967
8d08fdba
MS
12968 /* Fail if we've reached the end of the parm list, and more args
12969 are present, and the parm list isn't variadic. */
c166b898 12970 if (ia < nargs && parms == void_list_node)
8d08fdba 12971 return 1;
f4f206f4 12972 /* Fail if parms are left and they don't have default values. */
a34d3336 12973 if (parms && parms != void_list_node
8d08fdba
MS
12974 && TREE_PURPOSE (parms) == NULL_TREE)
12975 return 1;
bd0d5d4a 12976
8d08fdba
MS
12977 if (!subr)
12978 for (i = 0; i < ntparms; i++)
a34d3336 12979 if (!TREE_VEC_ELT (targs, i))
8d08fdba 12980 {
2d8ba2c7
LM
12981 tree tparm;
12982
12983 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
12984 continue;
12985
12986 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
bd0d5d4a
JM
12987
12988 /* If this is an undeduced nontype parameter that depends on
12989 a type parameter, try another pass; its type may have been
12990 deduced from a later argument than the one from which
12991 this parameter can be deduced. */
12992 if (TREE_CODE (tparm) == PARM_DECL
12993 && uses_template_parms (TREE_TYPE (tparm))
12994 && !saw_undeduced++)
12995 goto again;
12996
9b7dd5e8
DG
12997 /* Core issue #226 (C++0x) [temp.deduct]:
12998
12999 If a template argument has not been deduced, its
13000 default template argument, if any, is used.
13001
c1ae8be5
SM
13002 When we are in C++98 mode, TREE_PURPOSE will either
13003 be NULL_TREE or ERROR_MARK_NODE, so we do not need
13004 to explicitly check cxx_dialect here. */
9b7dd5e8
DG
13005 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
13006 {
8be2c871
DS
13007 tree arg = tsubst_template_arg
13008 (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)),
13009 targs, tf_none, NULL_TREE);
9b7dd5e8
DG
13010 if (arg == error_mark_node)
13011 return 1;
13012 else
13013 {
13014 TREE_VEC_ELT (targs, i) = arg;
13015 continue;
13016 }
13017 }
13018
b1d7b1c0
DG
13019 /* If the type parameter is a parameter pack, then it will
13020 be deduced to an empty parameter pack. */
13021 if (template_parameter_pack_p (tparm))
13022 {
13023 tree arg;
13024
13025 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
13026 {
13027 arg = make_node (NONTYPE_ARGUMENT_PACK);
13028 TREE_TYPE (arg) = TREE_TYPE (TEMPLATE_PARM_DECL (tparm));
13029 TREE_CONSTANT (arg) = 1;
13030 }
13031 else
13032 arg = make_node (TYPE_ARGUMENT_PACK);
13033
13034 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
13035
13036 TREE_VEC_ELT (targs, i) = arg;
13037 continue;
13038 }
13039
8d08fdba
MS
13040 return 2;
13041 }
c8094d83 13042
8d08fdba
MS
13043 return 0;
13044}
13045
acde59b4
MM
13046/* Subroutine of type_unification_real. Args are like the variables
13047 at the call site. ARG is an overloaded function (or template-id);
13048 we try deducing template args from each of the overloads, and if
13049 only one succeeds, we go with that. Modifies TARGS and returns
13050 true on success. */
34016c81 13051
acde59b4 13052static bool
c8094d83 13053resolve_overloaded_unification (tree tparms,
0cbd7506
MS
13054 tree targs,
13055 tree parm,
13056 tree arg,
13057 unification_kind_t strict,
3a978d72 13058 int sub_strict)
34016c81
JM
13059{
13060 tree tempargs = copy_node (targs);
13061 int good = 0;
e04c614e 13062 tree goodfn = NULL_TREE;
f23fb7f5 13063 bool addr_p;
34016c81
JM
13064
13065 if (TREE_CODE (arg) == ADDR_EXPR)
f23fb7f5
MM
13066 {
13067 arg = TREE_OPERAND (arg, 0);
13068 addr_p = true;
13069 }
13070 else
13071 addr_p = false;
9f3d9e46 13072
d8f8dca1
MM
13073 if (TREE_CODE (arg) == COMPONENT_REF)
13074 /* Handle `&x' where `x' is some static or non-static member
13075 function name. */
13076 arg = TREE_OPERAND (arg, 1);
13077
05e0b2f4
JM
13078 if (TREE_CODE (arg) == OFFSET_REF)
13079 arg = TREE_OPERAND (arg, 1);
13080
9f3d9e46 13081 /* Strip baselink information. */
50ad9642
MM
13082 if (BASELINK_P (arg))
13083 arg = BASELINK_FUNCTIONS (arg);
9f3d9e46 13084
34016c81
JM
13085 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
13086 {
13087 /* If we got some explicit template args, we need to plug them into
13088 the affected templates before we try to unify, in case the
13089 explicit args will completely resolve the templates in question. */
13090
13091 tree expl_subargs = TREE_OPERAND (arg, 1);
13092 arg = TREE_OPERAND (arg, 0);
13093
13094 for (; arg; arg = OVL_NEXT (arg))
13095 {
13096 tree fn = OVL_CURRENT (arg);
13097 tree subargs, elem;
13098
13099 if (TREE_CODE (fn) != TEMPLATE_DECL)
13100 continue;
13101
cc85b4c5 13102 ++processing_template_decl;
a34d3336
NS
13103 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
13104 expl_subargs, /*check_ret=*/false);
34016c81
JM
13105 if (subargs)
13106 {
c2ea3a40 13107 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
e04c614e
JM
13108 if (try_one_overload (tparms, targs, tempargs, parm,
13109 elem, strict, sub_strict, addr_p)
13110 && (!goodfn || !decls_match (goodfn, elem)))
13111 {
13112 goodfn = elem;
13113 ++good;
13114 }
34016c81 13115 }
cc85b4c5 13116 --processing_template_decl;
34016c81
JM
13117 }
13118 }
acde59b4
MM
13119 else if (TREE_CODE (arg) != OVERLOAD
13120 && TREE_CODE (arg) != FUNCTION_DECL)
13121 /* If ARG is, for example, "(0, &f)" then its type will be unknown
13122 -- but the deduction does not succeed because the expression is
13123 not just the function on its own. */
13124 return false;
315fb5db 13125 else
acde59b4 13126 for (; arg; arg = OVL_NEXT (arg))
e04c614e
JM
13127 if (try_one_overload (tparms, targs, tempargs, parm,
13128 TREE_TYPE (OVL_CURRENT (arg)),
13129 strict, sub_strict, addr_p)
13130 && (!goodfn || !decls_match (goodfn, OVL_CURRENT (arg))))
13131 {
13132 goodfn = OVL_CURRENT (arg);
13133 ++good;
13134 }
34016c81
JM
13135
13136 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13137 to function or pointer to member function argument if the set of
13138 overloaded functions does not contain function templates and at most
13139 one of a set of overloaded functions provides a unique match.
13140
13141 So if we found multiple possibilities, we return success but don't
13142 deduce anything. */
13143
13144 if (good == 1)
13145 {
13146 int i = TREE_VEC_LENGTH (targs);
13147 for (; i--; )
13148 if (TREE_VEC_ELT (tempargs, i))
13149 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
13150 }
13151 if (good)
acde59b4 13152 return true;
34016c81 13153
acde59b4 13154 return false;
34016c81
JM
13155}
13156
13157/* Subroutine of resolve_overloaded_unification; does deduction for a single
13158 overload. Fills TARGS with any deduced arguments, or error_mark_node if
13159 different overloads deduce different arguments for a given parm.
f23fb7f5
MM
13160 ADDR_P is true if the expression for which deduction is being
13161 performed was of the form "& fn" rather than simply "fn".
13162
34016c81
JM
13163 Returns 1 on success. */
13164
13165static int
3a978d72 13166try_one_overload (tree tparms,
0cbd7506
MS
13167 tree orig_targs,
13168 tree targs,
13169 tree parm,
13170 tree arg,
13171 unification_kind_t strict,
f23fb7f5
MM
13172 int sub_strict,
13173 bool addr_p)
34016c81
JM
13174{
13175 int nargs;
13176 tree tempargs;
13177 int i;
13178
13179 /* [temp.deduct.type] A template-argument can be deduced from a pointer
13180 to function or pointer to member function argument if the set of
13181 overloaded functions does not contain function templates and at most
13182 one of a set of overloaded functions provides a unique match.
13183
13184 So if this is a template, just return success. */
13185
13186 if (uses_template_parms (arg))
13187 return 1;
13188
f23fb7f5
MM
13189 if (TREE_CODE (arg) == METHOD_TYPE)
13190 arg = build_ptrmemfunc_type (build_pointer_type (arg));
13191 else if (addr_p)
13192 arg = build_pointer_type (arg);
13193
8af2fec4 13194 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
34016c81
JM
13195
13196 /* We don't copy orig_targs for this because if we have already deduced
13197 some template args from previous args, unify would complain when we
13198 try to deduce a template parameter for the same argument, even though
13199 there isn't really a conflict. */
13200 nargs = TREE_VEC_LENGTH (targs);
f31c0a32 13201 tempargs = make_tree_vec (nargs);
34016c81 13202
4393e105 13203 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
34016c81
JM
13204 return 0;
13205
13206 /* First make sure we didn't deduce anything that conflicts with
e97e5263 13207 explicitly specified args. */
34016c81
JM
13208 for (i = nargs; i--; )
13209 {
13210 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 13211 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81 13212
a34d3336
NS
13213 if (!elt)
13214 /*NOP*/;
34016c81 13215 else if (uses_template_parms (elt))
a34d3336
NS
13216 /* Since we're unifying against ourselves, we will fill in
13217 template args used in the function parm list with our own
13218 template parms. Discard them. */
13219 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
13220 else if (oldelt && !template_args_equal (oldelt, elt))
34016c81
JM
13221 return 0;
13222 }
13223
13224 for (i = nargs; i--; )
13225 {
13226 tree elt = TREE_VEC_ELT (tempargs, i);
13227
13228 if (elt)
13229 TREE_VEC_ELT (targs, i) = elt;
13230 }
13231
13232 return 1;
13233}
13234
4393e105
MM
13235/* PARM is a template class (perhaps with unbound template
13236 parameters). ARG is a fully instantiated type. If ARG can be
13237 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
13238 TARGS are as for unify. */
fcfb9f96
MM
13239
13240static tree
3a978d72 13241try_class_unification (tree tparms, tree targs, tree parm, tree arg)
4393e105 13242{
4393e105
MM
13243 tree copy_of_targs;
13244
13245 if (!CLASSTYPE_TEMPLATE_INFO (arg)
c8094d83 13246 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
68361a03 13247 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
4393e105
MM
13248 return NULL_TREE;
13249
13250 /* We need to make a new template argument vector for the call to
13251 unify. If we used TARGS, we'd clutter it up with the result of
13252 the attempted unification, even if this class didn't work out.
13253 We also don't want to commit ourselves to all the unifications
13254 we've already done, since unification is supposed to be done on
13255 an argument-by-argument basis. In other words, consider the
13256 following pathological case:
13257
13258 template <int I, int J, int K>
13259 struct S {};
c8094d83 13260
4393e105
MM
13261 template <int I, int J>
13262 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
c8094d83 13263
4393e105
MM
13264 template <int I, int J, int K>
13265 void f(S<I, J, K>, S<I, I, I>);
c8094d83 13266
4393e105 13267 void g() {
0cbd7506
MS
13268 S<0, 0, 0> s0;
13269 S<0, 1, 2> s2;
c8094d83 13270
0cbd7506 13271 f(s0, s2);
4393e105
MM
13272 }
13273
13274 Now, by the time we consider the unification involving `s2', we
13275 already know that we must have `f<0, 0, 0>'. But, even though
0e339752 13276 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
4393e105
MM
13277 because there are two ways to unify base classes of S<0, 1, 2>
13278 with S<I, I, I>. If we kept the already deduced knowledge, we
13279 would reject the possibility I=1. */
f31c0a32 13280 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
c8094d83 13281
4393e105 13282 /* If unification failed, we're done. */
74601d7c
KL
13283 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
13284 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
4393e105 13285 return NULL_TREE;
74601d7c
KL
13286
13287 return arg;
4393e105
MM
13288}
13289
a3a0fc7f
NS
13290/* Given a template type PARM and a class type ARG, find the unique
13291 base type in ARG that is an instance of PARM. We do not examine
13292 ARG itself; only its base-classes. If there is not exactly one
13293 appropriate base class, return NULL_TREE. PARM may be the type of
13294 a partial specialization, as well as a plain template type. Used
13295 by unify. */
4393e105
MM
13296
13297static tree
a3a0fc7f 13298get_template_base (tree tparms, tree targs, tree parm, tree arg)
fcfb9f96 13299{
a3a0fc7f
NS
13300 tree rval = NULL_TREE;
13301 tree binfo;
13302
9e1e64ec 13303 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
c8094d83 13304
a3a0fc7f
NS
13305 binfo = TYPE_BINFO (complete_type (arg));
13306 if (!binfo)
13307 /* The type could not be completed. */
13308 return NULL_TREE;
fcfb9f96 13309
a3a0fc7f
NS
13310 /* Walk in inheritance graph order. The search order is not
13311 important, and this avoids multiple walks of virtual bases. */
13312 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
fcfb9f96 13313 {
a3a0fc7f 13314 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
fcfb9f96 13315
8d83768f
NS
13316 if (r)
13317 {
13318 /* If there is more than one satisfactory baseclass, then:
fcfb9f96 13319
8d83768f 13320 [temp.deduct.call]
fcfb9f96 13321
8d83768f
NS
13322 If they yield more than one possible deduced A, the type
13323 deduction fails.
4393e105 13324
8d83768f 13325 applies. */
a3a0fc7f
NS
13326 if (rval && !same_type_p (r, rval))
13327 return NULL_TREE;
c8094d83 13328
a3a0fc7f 13329 rval = r;
8d83768f 13330 }
fcfb9f96
MM
13331 }
13332
a3a0fc7f 13333 return rval;
fcfb9f96
MM
13334}
13335
db2767b6
MM
13336/* Returns the level of DECL, which declares a template parameter. */
13337
e9659ab0 13338static int
3a978d72 13339template_decl_level (tree decl)
db2767b6
MM
13340{
13341 switch (TREE_CODE (decl))
13342 {
13343 case TYPE_DECL:
13344 case TEMPLATE_DECL:
13345 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
13346
13347 case PARM_DECL:
13348 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
13349
13350 default:
315fb5db 13351 gcc_unreachable ();
db2767b6 13352 }
315fb5db 13353 return 0;
db2767b6
MM
13354}
13355
830bfa74
MM
13356/* Decide whether ARG can be unified with PARM, considering only the
13357 cv-qualifiers of each type, given STRICT as documented for unify.
324f9dfb 13358 Returns nonzero iff the unification is OK on that basis. */
e92cc029 13359
e9659ab0 13360static int
3a978d72 13361check_cv_quals_for_unify (int strict, tree arg, tree parm)
830bfa74 13362{
4f2b0fb2
NS
13363 int arg_quals = cp_type_quals (arg);
13364 int parm_quals = cp_type_quals (parm);
13365
355f774d
NS
13366 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13367 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 13368 {
2e9ceb77 13369 /* Although a CVR qualifier is ignored when being applied to a
0cbd7506
MS
13370 substituted template parameter ([8.3.2]/1 for example), that
13371 does not apply during deduction [14.8.2.4]/1, (even though
13372 that is not explicitly mentioned, [14.8.2.4]/9 indicates
13373 this). Except when we're allowing additional CV qualifiers
13374 at the outer level [14.8.2.1]/3,1st bullet. */
2e9ceb77
NS
13375 if ((TREE_CODE (arg) == REFERENCE_TYPE
13376 || TREE_CODE (arg) == FUNCTION_TYPE
13377 || TREE_CODE (arg) == METHOD_TYPE)
13378 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
13379 return 0;
13380
13381 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
13382 && (parm_quals & TYPE_QUAL_RESTRICT))
13383 return 0;
4f2b0fb2 13384 }
2e9ceb77 13385
62e4a758 13386 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 13387 && (arg_quals & parm_quals) != parm_quals)
ef637255
MM
13388 return 0;
13389
62e4a758 13390 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
4f2b0fb2 13391 && (parm_quals & arg_quals) != arg_quals)
ef637255
MM
13392 return 0;
13393
ef637255 13394 return 1;
830bfa74
MM
13395}
13396
5d80a306
DG
13397/* Determines the LEVEL and INDEX for the template parameter PARM. */
13398void
13399template_parm_level_and_index (tree parm, int* level, int* index)
13400{
13401 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13402 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13403 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13404 {
13405 *index = TEMPLATE_TYPE_IDX (parm);
13406 *level = TEMPLATE_TYPE_LEVEL (parm);
13407 }
13408 else
13409 {
13410 *index = TEMPLATE_PARM_IDX (parm);
13411 *level = TEMPLATE_PARM_LEVEL (parm);
13412 }
13413}
13414
13415/* Unifies the remaining arguments in PACKED_ARGS with the pack
13416 expansion at the end of PACKED_PARMS. Returns 0 if the type
13417 deduction succeeds, 1 otherwise. STRICT is the same as in
13418 unify. CALL_ARGS_P is true iff PACKED_ARGS is actually a function
13419 call argument list. We'll need to adjust the arguments to make them
13420 types. SUBR tells us if this is from a recursive call to
13421 type_unification_real. */
13422int
13423unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
13424 tree packed_args, int strict, bool call_args_p,
13425 bool subr)
13426{
13427 tree parm
13428 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
13429 tree pattern = PACK_EXPANSION_PATTERN (parm);
13430 tree pack, packs = NULL_TREE;
13431 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
13432 int len = TREE_VEC_LENGTH (packed_args);
13433
13434 /* Determine the parameter packs we will be deducing from the
13435 pattern, and record their current deductions. */
13436 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
13437 pack; pack = TREE_CHAIN (pack))
13438 {
13439 tree parm_pack = TREE_VALUE (pack);
13440 int idx, level;
13441
13442 /* Determine the index and level of this parameter pack. */
13443 template_parm_level_and_index (parm_pack, &level, &idx);
13444
13445 /* Keep track of the parameter packs and their corresponding
13446 argument packs. */
13447 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
13448 TREE_TYPE (packs) = make_tree_vec (len - start);
13449 }
13450
13451 /* Loop through all of the arguments that have not yet been
13452 unified and unify each with the pattern. */
13453 for (i = start; i < len; i++)
13454 {
13455 tree parm = pattern;
13456
13457 /* For each parameter pack, clear out the deduced value so that
13458 we can deduce it again. */
13459 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13460 {
13461 int idx, level;
13462 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13463
13464 TMPL_ARG (targs, level, idx) = NULL_TREE;
13465 }
13466
13467 /* Unify the pattern with the current argument. */
13468 {
13469 tree arg = TREE_VEC_ELT (packed_args, i);
2afad0f6 13470 tree arg_expr = NULL_TREE;
5d80a306
DG
13471 int arg_strict = strict;
13472 bool skip_arg_p = false;
13473
13474 if (call_args_p)
13475 {
13476 int sub_strict;
13477
13478 /* This mirrors what we do in type_unification_real. */
13479 switch (strict)
13480 {
13481 case DEDUCE_CALL:
13482 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL
13483 | UNIFY_ALLOW_MORE_CV_QUAL
13484 | UNIFY_ALLOW_DERIVED);
13485 break;
13486
13487 case DEDUCE_CONV:
13488 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
13489 break;
13490
13491 case DEDUCE_EXACT:
13492 sub_strict = UNIFY_ALLOW_NONE;
13493 break;
13494
13495 default:
13496 gcc_unreachable ();
13497 }
13498
13499 if (!TYPE_P (arg))
13500 {
13501 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
13502 if (type_unknown_p (arg))
13503 {
13504 /* [temp.deduct.type] A template-argument can be
13505 deduced from a pointer to function or pointer
13506 to member function argument if the set of
13507 overloaded functions does not contain function
13508 templates and at most one of a set of
13509 overloaded functions provides a unique
13510 match. */
13511
13512 if (resolve_overloaded_unification
a916d97f
ILT
13513 (tparms, targs, parm, arg,
13514 (unification_kind_t) strict,
13515 sub_strict)
5d80a306
DG
13516 != 0)
13517 return 1;
13518 skip_arg_p = true;
13519 }
13520
13521 if (!skip_arg_p)
13522 {
2afad0f6
DG
13523 arg_expr = arg;
13524 arg = unlowered_expr_type (arg);
5d80a306
DG
13525 if (arg == error_mark_node)
13526 return 1;
13527 }
13528 }
13529
13530 arg_strict = sub_strict;
13531
13532 if (!subr)
13533 arg_strict |=
a916d97f
ILT
13534 maybe_adjust_types_for_deduction ((unification_kind_t) strict,
13535 &parm, &arg, arg_expr);
5d80a306
DG
13536 }
13537
13538 if (!skip_arg_p)
13539 {
13540 if (unify (tparms, targs, parm, arg, arg_strict))
13541 return 1;
13542 }
13543 }
13544
13545 /* For each parameter pack, collect the deduced value. */
13546 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13547 {
13548 int idx, level;
13549 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13550
13551 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
13552 TMPL_ARG (targs, level, idx);
13553 }
13554 }
13555
13556 /* Verify that the results of unification with the parameter packs
13557 produce results consistent with what we've seen before, and make
13558 the deduced argument packs available. */
13559 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13560 {
13561 tree old_pack = TREE_VALUE (pack);
13562 tree new_args = TREE_TYPE (pack);
db60ff18
DG
13563 int i, len = TREE_VEC_LENGTH (new_args);
13564 bool nondeduced_p = false;
13565
13566 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
13567 actually deduce anything. */
13568 for (i = 0; i < len && !nondeduced_p; ++i)
13569 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
13570 nondeduced_p = true;
13571 if (nondeduced_p)
13572 continue;
5d80a306
DG
13573
13574 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
13575 {
13576 /* Prepend the explicit arguments onto NEW_ARGS. */
13577 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13578 tree old_args = new_args;
13579 int i, explicit_len = TREE_VEC_LENGTH (explicit_args);
13580 int len = explicit_len + TREE_VEC_LENGTH (old_args);
13581
13582 /* Copy the explicit arguments. */
13583 new_args = make_tree_vec (len);
13584 for (i = 0; i < explicit_len; i++)
13585 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (explicit_args, i);
13586
13587 /* Copy the deduced arguments. */
13588 for (; i < len; i++)
13589 TREE_VEC_ELT (new_args, i) =
13590 TREE_VEC_ELT (old_args, i - explicit_len);
13591 }
13592
13593 if (!old_pack)
13594 {
13595 tree result;
13596 int idx, level;
13597
13598 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13599
13600 /* Build the deduced *_ARGUMENT_PACK. */
13601 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
13602 {
13603 result = make_node (NONTYPE_ARGUMENT_PACK);
13604 TREE_TYPE (result) =
13605 TREE_TYPE (TEMPLATE_PARM_DECL (TREE_PURPOSE (pack)));
13606 TREE_CONSTANT (result) = 1;
13607 }
13608 else
13609 result = make_node (TYPE_ARGUMENT_PACK);
13610
13611 SET_ARGUMENT_PACK_ARGS (result, new_args);
13612
13613 /* Note the deduced argument packs for this parameter
13614 pack. */
13615 TMPL_ARG (targs, level, idx) = result;
13616 }
13617 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
13618 && (ARGUMENT_PACK_ARGS (old_pack)
13619 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
13620 {
13621 /* We only had the explicitly-provided arguments before, but
13622 now we have a complete set of arguments. */
13623 int idx, level;
13624 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
13625 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13626
13627 /* Keep the original deduced argument pack. */
13628 TMPL_ARG (targs, level, idx) = old_pack;
13629
13630 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
13631 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
13632 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
13633 }
13634 else if (!comp_template_args (ARGUMENT_PACK_ARGS (old_pack),
13635 new_args))
13636 /* Inconsistent unification of this parameter pack. */
13637 return 1;
13638 else
13639 {
13640 int idx, level;
13641
13642 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
13643
13644 /* Keep the original deduced argument pack. */
13645 TMPL_ARG (targs, level, idx) = old_pack;
13646 }
13647 }
13648
13649 return 0;
13650}
13651
916b63c3
MM
13652/* Deduce the value of template parameters. TPARMS is the (innermost)
13653 set of template parameters to a template. TARGS is the bindings
13654 for those template parameters, as determined thus far; TARGS may
13655 include template arguments for outer levels of template parameters
13656 as well. PARM is a parameter to a template function, or a
13657 subcomponent of that parameter; ARG is the corresponding argument.
13658 This function attempts to match PARM with ARG in a manner
13659 consistent with the existing assignments in TARGS. If more values
13660 are deduced, then TARGS is updated.
13661
13662 Returns 0 if the type deduction succeeds, 1 otherwise. The
13663 parameter STRICT is a bitwise or of the following flags:
830bfa74
MM
13664
13665 UNIFY_ALLOW_NONE:
13666 Require an exact match between PARM and ARG.
13667 UNIFY_ALLOW_MORE_CV_QUAL:
028d1f20
NS
13668 Allow the deduced ARG to be more cv-qualified (by qualification
13669 conversion) than ARG.
830bfa74
MM
13670 UNIFY_ALLOW_LESS_CV_QUAL:
13671 Allow the deduced ARG to be less cv-qualified than ARG.
13672 UNIFY_ALLOW_DERIVED:
13673 Allow the deduced ARG to be a template base class of ARG,
13674 or a pointer to a template base class of the type pointed to by
161c12b0
JM
13675 ARG.
13676 UNIFY_ALLOW_INTEGER:
13677 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
c8094d83 13678 case for more information.
028d1f20
NS
13679 UNIFY_ALLOW_OUTER_LEVEL:
13680 This is the outermost level of a deduction. Used to determine validity
13681 of qualification conversions. A valid qualification conversion must
13682 have const qualified pointers leading up to the inner type which
13683 requires additional CV quals, except at the outer level, where const
13684 is not required [conv.qual]. It would be normal to set this flag in
62e4a758
NS
13685 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
13686 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
13687 This is the outermost level of a deduction, and PARM can be more CV
13688 qualified at this point.
13689 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
13690 This is the outermost level of a deduction, and PARM can be less CV
8baddbf1 13691 qualified at this point. */
830bfa74 13692
e9659ab0 13693static int
3a978d72 13694unify (tree tparms, tree targs, tree parm, tree arg, int strict)
8d08fdba
MS
13695{
13696 int idx;
050367a3 13697 tree targ;
db2767b6 13698 tree tparm;
028d1f20 13699 int strict_in = strict;
8d08fdba
MS
13700
13701 /* I don't think this will do the right thing with respect to types.
13702 But the only case I've seen it in so far has been array bounds, where
13703 signedness is the only information lost, and I think that will be
13704 okay. */
13705 while (TREE_CODE (parm) == NOP_EXPR)
13706 parm = TREE_OPERAND (parm, 0);
13707
13708 if (arg == error_mark_node)
13709 return 1;
09357846
JM
13710 if (arg == unknown_type_node
13711 || arg == init_list_type_node)
34016c81
JM
13712 /* We can't deduce anything from this, but we might get all the
13713 template args from other function args. */
13714 return 0;
13715
db2767b6 13716 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 13717 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
13718 template parameters. We might need them if we're trying to
13719 figure out which of two things is more specialized. */
13720 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
13721 return 0;
13722
09357846
JM
13723 /* Handle init lists early, so the rest of the function can assume
13724 we're dealing with a type. */
13725 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
13726 {
13727 tree elt, elttype;
13728 unsigned i;
13729
13730 if (!is_std_init_list (parm))
13731 /* We can only deduce from an initializer list argument if the
13732 parameter is std::initializer_list; otherwise this is a
13733 non-deduced context. */
13734 return 0;
13735
13736 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
13737
13738 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
13739 {
2604de9d 13740 int elt_strict = strict;
09357846 13741 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
2604de9d
JM
13742 {
13743 tree type = TREE_TYPE (elt);
13744 /* It should only be possible to get here for a call. */
13745 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
13746 elt_strict |= maybe_adjust_types_for_deduction
13747 (DEDUCE_CALL, &elttype, &type, elt);
13748 elt = type;
13749 }
13750
13751 if (unify (tparms, targs, elttype, elt, elt_strict))
09357846
JM
13752 return 1;
13753 }
13754 return 0;
13755 }
13756
830bfa74
MM
13757 /* Immediately reject some pairs that won't unify because of
13758 cv-qualification mismatches. */
13759 if (TREE_CODE (arg) == TREE_CODE (parm)
2f939d94 13760 && TYPE_P (arg)
d0ab7624 13761 /* It is the elements of the array which hold the cv quals of an array
0cbd7506
MS
13762 type, and the elements might be template type parms. We'll check
13763 when we recurse. */
d0ab7624 13764 && TREE_CODE (arg) != ARRAY_TYPE
830bfa74
MM
13765 /* We check the cv-qualifiers when unifying with template type
13766 parameters below. We want to allow ARG `const T' to unify with
13767 PARM `T' for example, when computing which of two templates
13768 is more specialized, for example. */
13769 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
028d1f20 13770 && !check_cv_quals_for_unify (strict_in, arg, parm))
49432171
JM
13771 return 1;
13772
028d1f20 13773 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
3ea099f1 13774 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
028d1f20
NS
13775 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
13776 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
13777 strict &= ~UNIFY_ALLOW_DERIVED;
62e4a758
NS
13778 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
13779 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
c8094d83 13780
8d08fdba
MS
13781 switch (TREE_CODE (parm))
13782 {
2ca340ae 13783 case TYPENAME_TYPE:
fccef71e 13784 case SCOPE_REF:
b8c6534b 13785 case UNBOUND_CLASS_TEMPLATE:
2ca340ae
JM
13786 /* In a type which contains a nested-name-specifier, template
13787 argument values cannot be deduced for template parameters used
13788 within the nested-name-specifier. */
13789 return 0;
13790
8d08fdba 13791 case TEMPLATE_TYPE_PARM:
73b0fce8 13792 case TEMPLATE_TEMPLATE_PARM:
a1281f45 13793 case BOUND_TEMPLATE_TEMPLATE_PARM:
db2767b6 13794 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
833be229
PC
13795 if (tparm == error_mark_node)
13796 return 1;
db2767b6
MM
13797
13798 if (TEMPLATE_TYPE_LEVEL (parm)
13799 != template_decl_level (tparm))
13800 /* The PARM is not one we're trying to unify. Just check
13801 to see if it matches ARG. */
13802 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 13803 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 13804 idx = TEMPLATE_TYPE_IDX (parm);
916b63c3 13805 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
db2767b6 13806 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 13807
73b0fce8 13808 /* Check for mixed types and values. */
db2767b6
MM
13809 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
13810 && TREE_CODE (tparm) != TYPE_DECL)
c8094d83 13811 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
db2767b6 13812 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
13813 return 1;
13814
a1281f45 13815 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
73b0fce8 13816 {
b429fdf0
KL
13817 /* ARG must be constructed from a template class or a template
13818 template parameter. */
13819 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
4e95268d 13820 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
a1281f45 13821 return 1;
73b0fce8 13822
a1281f45 13823 {
a1281f45 13824 tree parmvec = TYPE_TI_ARGS (parm);
6df91b00 13825 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
a0a5f30f 13826 tree parm_parms
063cc99c 13827 = DECL_INNERMOST_TEMPLATE_PARMS
a0a5f30f 13828 (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (parm));
4cf36211
DG
13829 int i, len;
13830 int parm_variadic_p = 0;
73b0fce8 13831
e7e93965
MM
13832 /* The resolution to DR150 makes clear that default
13833 arguments for an N-argument may not be used to bind T
13834 to a template template parameter with fewer than N
13835 parameters. It is not safe to permit the binding of
13836 default arguments as an extension, as that may change
13837 the meaning of a conforming program. Consider:
13838
13839 struct Dense { static const unsigned int dim = 1; };
13840
13841 template <template <typename> class View,
13842 typename Block>
13843 void operator+(float, View<Block> const&);
13844
3db45ab5
MS
13845 template <typename Block,
13846 unsigned int Dim = Block::dim>
e7e93965
MM
13847 struct Lvalue_proxy { operator float() const; };
13848
13849 void
13850 test_1d (void) {
13851 Lvalue_proxy<Dense> p;
13852 float b;
13853 b + p;
13854 }
a1281f45 13855
e7e93965
MM
13856 Here, if Lvalue_proxy is permitted to bind to View, then
13857 the global operator+ will be used; if they are not, the
3db45ab5 13858 Lvalue_proxy will be converted to float. */
a0a5f30f
DG
13859 if (coerce_template_parms (parm_parms,
13860 argvec,
e7e93965
MM
13861 TYPE_TI_TEMPLATE (parm),
13862 tf_none,
13863 /*require_all_args=*/true,
13864 /*use_default_args=*/false)
0cbd7506 13865 == error_mark_node)
a1281f45 13866 return 1;
c8094d83
MS
13867
13868 /* Deduce arguments T, i from TT<T> or TT<i>.
a1281f45
KL
13869 We check each element of PARMVEC and ARGVEC individually
13870 rather than the whole TREE_VEC since they can have
13871 different number of elements. */
6b9b6b15 13872
4cf36211
DG
13873 parmvec = expand_template_argument_pack (parmvec);
13874 argvec = expand_template_argument_pack (argvec);
13875
13876 len = TREE_VEC_LENGTH (parmvec);
13877
13878 /* Check if the parameters end in a pack, making them
13879 variadic. */
13880 if (len > 0
13881 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
13882 parm_variadic_p = 1;
13883
13884 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
13885 return 1;
13886
13887 for (i = 0; i < len - parm_variadic_p; ++i)
a1281f45 13888 {
0cbd7506 13889 if (unify (tparms, targs,
c8094d83
MS
13890 TREE_VEC_ELT (parmvec, i),
13891 TREE_VEC_ELT (argvec, i),
a1281f45
KL
13892 UNIFY_ALLOW_NONE))
13893 return 1;
73b0fce8 13894 }
4cf36211
DG
13895
13896 if (parm_variadic_p
13897 && unify_pack_expansion (tparms, targs,
13898 parmvec, argvec,
13899 UNIFY_ALLOW_NONE,
13900 /*call_args_p=*/false,
13901 /*subr=*/false))
13902 return 1;
a1281f45 13903 }
b429fdf0 13904 arg = TYPE_TI_TEMPLATE (arg);
a1281f45
KL
13905
13906 /* Fall through to deduce template name. */
13907 }
13908
13909 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
13910 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
13911 {
13912 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
13913
13914 /* Simple cases: Value already set, does match or doesn't. */
13915 if (targ != NULL_TREE && template_args_equal (targ, arg))
13916 return 0;
13917 else if (targ)
13918 return 1;
db2767b6
MM
13919 }
13920 else
13921 {
830bfa74
MM
13922 /* If PARM is `const T' and ARG is only `int', we don't have
13923 a match unless we are allowing additional qualification.
13924 If ARG is `const int' and PARM is just `T' that's OK;
13925 that binds `const int' to `T'. */
c8094d83 13926 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
830bfa74 13927 arg, parm))
db2767b6
MM
13928 return 1;
13929
830bfa74
MM
13930 /* Consider the case where ARG is `const volatile int' and
13931 PARM is `const T'. Then, T should be `volatile int'. */
c2ea3a40
NS
13932 arg = cp_build_qualified_type_real
13933 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
adecb3f4
MM
13934 if (arg == error_mark_node)
13935 return 1;
73b0fce8 13936
a1281f45
KL
13937 /* Simple cases: Value already set, does match or doesn't. */
13938 if (targ != NULL_TREE && same_type_p (targ, arg))
13939 return 0;
13940 else if (targ)
13941 return 1;
61cd552e 13942
94fc547c
MM
13943 /* Make sure that ARG is not a variable-sized array. (Note
13944 that were talking about variable-sized arrays (like
13945 `int[n]'), rather than arrays of unknown size (like
13946 `int[]').) We'll get very confused by such a type since
13947 the bound of the array will not be computable in an
13948 instantiation. Besides, such types are not allowed in
13949 ISO C++, so we can do as we please here. */
5377d5ba 13950 if (variably_modified_type_p (arg, NULL_TREE))
94fc547c 13951 return 1;
a4d674fc
JM
13952
13953 /* Strip typedefs as in convert_template_argument. */
cd41d410 13954 arg = strip_typedefs (arg);
94fc547c 13955 }
61cd552e 13956
5d80a306
DG
13957 /* If ARG is a parameter pack or an expansion, we cannot unify
13958 against it unless PARM is also a parameter pack. */
13959 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
13960 && !template_parameter_pack_p (parm))
13961 return 1;
13962
111a28c2
DS
13963 /* If the argument deduction results is a METHOD_TYPE,
13964 then there is a problem.
13965 METHOD_TYPE doesn't map to any real C++ type the result of
13966 the deduction can not be of that type. */
13967 if (TREE_CODE (arg) == METHOD_TYPE)
13968 return 1;
13969
916b63c3 13970 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
73b0fce8
KL
13971 return 0;
13972
f84b4be9 13973 case TEMPLATE_PARM_INDEX:
db2767b6 13974 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
3e9ac7e5
VR
13975 if (tparm == error_mark_node)
13976 return 1;
db2767b6 13977
c8094d83 13978 if (TEMPLATE_PARM_LEVEL (parm)
db2767b6
MM
13979 != template_decl_level (tparm))
13980 /* The PARM is not one we're trying to unify. Just check
13981 to see if it matches ARG. */
c8a209ca
NS
13982 return !(TREE_CODE (arg) == TREE_CODE (parm)
13983 && cp_tree_equal (parm, arg));
db2767b6 13984
f84b4be9 13985 idx = TEMPLATE_PARM_IDX (parm);
916b63c3 13986 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
db2767b6 13987
050367a3 13988 if (targ)
c8a209ca 13989 return !cp_tree_equal (targ, arg);
8d08fdba 13990
161c12b0
JM
13991 /* [temp.deduct.type] If, in the declaration of a function template
13992 with a non-type template-parameter, the non-type
13993 template-parameter is used in an expression in the function
13994 parameter-list and, if the corresponding template-argument is
13995 deduced, the template-argument type shall match the type of the
13996 template-parameter exactly, except that a template-argument
c8094d83 13997 deduced from an array bound may be of any integral type.
d7c4edd0 13998 The non-type parameter might use already deduced type parameters. */
bd0d5d4a 13999 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
e2005c8d
KL
14000 if (!TREE_TYPE (arg))
14001 /* Template-parameter dependent expression. Just accept it for now.
14002 It will later be processed in convert_template_argument. */
14003 ;
14004 else if (same_type_p (TREE_TYPE (arg), tparm))
14005 /* OK */;
161c12b0 14006 else if ((strict & UNIFY_ALLOW_INTEGER)
bd0d5d4a
JM
14007 && (TREE_CODE (tparm) == INTEGER_TYPE
14008 || TREE_CODE (tparm) == BOOLEAN_TYPE))
8baddbf1
MM
14009 /* Convert the ARG to the type of PARM; the deduced non-type
14010 template argument must exactly match the types of the
14011 corresponding parameter. */
ed7284dd 14012 arg = fold (build_nop (tparm, arg));
bd0d5d4a
JM
14013 else if (uses_template_parms (tparm))
14014 /* We haven't deduced the type of this parameter yet. Try again
14015 later. */
14016 return 0;
161c12b0
JM
14017 else
14018 return 1;
14019
5d80a306
DG
14020 /* If ARG is a parameter pack or an expansion, we cannot unify
14021 against it unless PARM is also a parameter pack. */
14022 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
14023 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
14024 return 1;
14025
916b63c3 14026 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
8d08fdba
MS
14027 return 0;
14028
28e8f3a0
GB
14029 case PTRMEM_CST:
14030 {
0cbd7506
MS
14031 /* A pointer-to-member constant can be unified only with
14032 another constant. */
28e8f3a0 14033 if (TREE_CODE (arg) != PTRMEM_CST)
0cbd7506 14034 return 1;
28e8f3a0
GB
14035
14036 /* Just unify the class member. It would be useless (and possibly
0cbd7506
MS
14037 wrong, depending on the strict flags) to unify also
14038 PTRMEM_CST_CLASS, because we want to be sure that both parm and
14039 arg refer to the same variable, even if through different
14040 classes. For instance:
28e8f3a0 14041
0cbd7506
MS
14042 struct A { int x; };
14043 struct B : A { };
28e8f3a0 14044
0cbd7506 14045 Unification of &A::x and &B::x must succeed. */
28e8f3a0 14046 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
0cbd7506 14047 PTRMEM_CST_MEMBER (arg), strict);
28e8f3a0
GB
14048 }
14049
8d08fdba 14050 case POINTER_TYPE:
830bfa74 14051 {
830bfa74
MM
14052 if (TREE_CODE (arg) != POINTER_TYPE)
14053 return 1;
c8094d83 14054
830bfa74
MM
14055 /* [temp.deduct.call]
14056
14057 A can be another pointer or pointer to member type that can
14058 be converted to the deduced A via a qualification
14059 conversion (_conv.qual_).
14060
14061 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
14062 This will allow for additional cv-qualification of the
028d1f20 14063 pointed-to types if appropriate. */
c8094d83 14064
028d1f20 14065 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
830bfa74
MM
14066 /* The derived-to-base conversion only persists through one
14067 level of pointers. */
028d1f20 14068 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
830bfa74 14069
c8094d83 14070 return unify (tparms, targs, TREE_TYPE (parm),
028d1f20 14071 TREE_TYPE (arg), strict);
830bfa74 14072 }
8d08fdba
MS
14073
14074 case REFERENCE_TYPE:
830bfa74
MM
14075 if (TREE_CODE (arg) != REFERENCE_TYPE)
14076 return 1;
14077 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
028d1f20 14078 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
14079
14080 case ARRAY_TYPE:
14081 if (TREE_CODE (arg) != ARRAY_TYPE)
14082 return 1;
3042d5be
MM
14083 if ((TYPE_DOMAIN (parm) == NULL_TREE)
14084 != (TYPE_DOMAIN (arg) == NULL_TREE))
14085 return 1;
8baddbf1
MM
14086 if (TYPE_DOMAIN (parm) != NULL_TREE)
14087 {
14088 tree parm_max;
14089 tree arg_max;
6ee3ffe8
MM
14090 bool parm_cst;
14091 bool arg_cst;
8baddbf1
MM
14092
14093 /* Our representation of array types uses "N - 1" as the
14094 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
6ee3ffe8
MM
14095 not an integer constant. We cannot unify arbitrarily
14096 complex expressions, so we eliminate the MINUS_EXPRs
14097 here. */
14098 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
14099 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
14100 if (!parm_cst)
8baddbf1 14101 {
6ee3ffe8 14102 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
8baddbf1
MM
14103 parm_max = TREE_OPERAND (parm_max, 0);
14104 }
6ee3ffe8
MM
14105 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
14106 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
14107 if (!arg_cst)
14108 {
14109 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
14110 trying to unify the type of a variable with the type
14111 of a template parameter. For example:
14112
14113 template <unsigned int N>
14114 void f (char (&) [N]);
14115 int g();
14116 void h(int i) {
14117 char a[g(i)];
14118 f(a);
14119 }
14120
14121 Here, the type of the ARG will be "int [g(i)]", and
14122 may be a SAVE_EXPR, etc. */
14123 if (TREE_CODE (arg_max) != MINUS_EXPR)
14124 return 1;
14125 arg_max = TREE_OPERAND (arg_max, 0);
14126 }
14127
14128 /* If only one of the bounds used a MINUS_EXPR, compensate
14129 by adding one to the other bound. */
14130 if (parm_cst && !arg_cst)
db3927fb 14131 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
6ee3ffe8
MM
14132 integer_type_node,
14133 parm_max,
14134 integer_one_node);
14135 else if (arg_cst && !parm_cst)
db3927fb 14136 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
6ee3ffe8
MM
14137 integer_type_node,
14138 arg_max,
14139 integer_one_node);
8baddbf1
MM
14140
14141 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
14142 return 1;
14143 }
830bfa74 14144 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
712467a4 14145 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
14146
14147 case REAL_TYPE:
37c46b43 14148 case COMPLEX_TYPE:
c00996a3 14149 case VECTOR_TYPE:
8d08fdba 14150 case INTEGER_TYPE:
42976354 14151 case BOOLEAN_TYPE:
3590f0a6 14152 case ENUMERAL_TYPE:
5ad5a526 14153 case VOID_TYPE:
f376e137
MS
14154 if (TREE_CODE (arg) != TREE_CODE (parm))
14155 return 1;
c8094d83 14156
9edc3913 14157 /* We have already checked cv-qualification at the top of the
514a1f18 14158 function. */
8baddbf1 14159 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
ca79f85d
JM
14160 return 1;
14161
8d08fdba
MS
14162 /* As far as unification is concerned, this wins. Later checks
14163 will invalidate it if necessary. */
14164 return 0;
14165
14166 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 14167 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 14168 case INTEGER_CST:
bd6dd845
MS
14169 while (TREE_CODE (arg) == NOP_EXPR)
14170 arg = TREE_OPERAND (arg, 0);
14171
8d08fdba
MS
14172 if (TREE_CODE (arg) != INTEGER_CST)
14173 return 1;
14174 return !tree_int_cst_equal (parm, arg);
14175
8d08fdba
MS
14176 case TREE_VEC:
14177 {
14178 int i;
14179 if (TREE_CODE (arg) != TREE_VEC)
14180 return 1;
14181 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
14182 return 1;
0dc09a61 14183 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
830bfa74 14184 if (unify (tparms, targs,
8d08fdba 14185 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
4393e105 14186 UNIFY_ALLOW_NONE))
8d08fdba
MS
14187 return 1;
14188 return 0;
14189 }
14190
8d08fdba 14191 case RECORD_TYPE:
f181d4ae 14192 case UNION_TYPE:
f181d4ae 14193 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 14194 return 1;
c8094d83 14195
a7a64a77
MM
14196 if (TYPE_PTRMEMFUNC_P (parm))
14197 {
14198 if (!TYPE_PTRMEMFUNC_P (arg))
14199 return 1;
14200
c8094d83 14201 return unify (tparms, targs,
a7a64a77
MM
14202 TYPE_PTRMEMFUNC_FN_TYPE (parm),
14203 TYPE_PTRMEMFUNC_FN_TYPE (arg),
14204 strict);
14205 }
14206
5db698f6 14207 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 14208 {
6467930b 14209 tree t = NULL_TREE;
4393e105 14210
028d1f20 14211 if (strict_in & UNIFY_ALLOW_DERIVED)
4393e105
MM
14212 {
14213 /* First, we try to unify the PARM and ARG directly. */
14214 t = try_class_unification (tparms, targs,
14215 parm, arg);
14216
14217 if (!t)
14218 {
14219 /* Fallback to the special case allowed in
14220 [temp.deduct.call]:
c8094d83 14221
4393e105
MM
14222 If P is a class, and P has the form
14223 template-id, then A can be a derived class of
14224 the deduced A. Likewise, if P is a pointer to
14225 a class of the form template-id, A can be a
14226 pointer to a derived class pointed to by the
14227 deduced A. */
8d83768f 14228 t = get_template_base (tparms, targs, parm, arg);
4393e105 14229
8d83768f 14230 if (!t)
4393e105
MM
14231 return 1;
14232 }
14233 }
c8094d83
MS
14234 else if (CLASSTYPE_TEMPLATE_INFO (arg)
14235 && (CLASSTYPE_TI_TEMPLATE (parm)
9fbf56f7 14236 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
14237 /* Perhaps PARM is something like S<U> and ARG is S<int>.
14238 Then, we should unify `int' and `U'. */
6467930b 14239 t = arg;
4393e105 14240 else
dc957d14 14241 /* There's no chance of unification succeeding. */
5566b478 14242 return 1;
6467930b 14243
830bfa74 14244 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
4393e105 14245 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
5566b478 14246 }
9edc3913 14247 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
5566b478 14248 return 1;
a4443a08 14249 return 0;
8d08fdba
MS
14250
14251 case METHOD_TYPE:
8d08fdba 14252 case FUNCTION_TYPE:
c166b898
ILT
14253 {
14254 unsigned int nargs;
14255 tree *args;
14256 tree a;
14257 unsigned int i;
830bfa74 14258
c166b898
ILT
14259 if (TREE_CODE (arg) != TREE_CODE (parm))
14260 return 1;
38d18b1a 14261
c166b898
ILT
14262 /* CV qualifications for methods can never be deduced, they must
14263 match exactly. We need to check them explicitly here,
14264 because type_unification_real treats them as any other
14265 cv-qualified parameter. */
14266 if (TREE_CODE (parm) == METHOD_TYPE
14267 && (!check_cv_quals_for_unify
14268 (UNIFY_ALLOW_NONE,
14269 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
14270 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
14271 return 1;
14272
14273 if (unify (tparms, targs, TREE_TYPE (parm),
14274 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
14275 return 1;
14276
14277 nargs = list_length (TYPE_ARG_TYPES (arg));
14278 args = XALLOCAVEC (tree, nargs);
14279 for (a = TYPE_ARG_TYPES (arg), i = 0;
14280 a != NULL_TREE && a != void_list_node;
14281 a = TREE_CHAIN (a), ++i)
14282 args[i] = TREE_VALUE (a);
14283 nargs = i;
14284
14285 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
14286 args, nargs, 1, DEDUCE_EXACT,
14287 LOOKUP_NORMAL);
14288 }
a4443a08
MS
14289
14290 case OFFSET_TYPE:
9804209d 14291 /* Unify a pointer to member with a pointer to member function, which
0cbd7506 14292 deduces the type of the member as a function type. */
9804209d 14293 if (TYPE_PTRMEMFUNC_P (arg))
0cbd7506
MS
14294 {
14295 tree method_type;
14296 tree fntype;
14297 cp_cv_quals cv_quals;
14298
14299 /* Check top-level cv qualifiers */
14300 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
14301 return 1;
14302
14303 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
14304 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
14305 return 1;
14306
14307 /* Determine the type of the function we are unifying against. */
14308 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
14309 fntype =
14310 build_function_type (TREE_TYPE (method_type),
14311 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
14312
14313 /* Extract the cv-qualifiers of the member function from the
14314 implicit object parameter and place them on the function
14315 type to be restored later. */
14316 cv_quals =
14317 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
14318 fntype = build_qualified_type (fntype, cv_quals);
14319 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
14320 }
9804209d 14321
a4443a08
MS
14322 if (TREE_CODE (arg) != OFFSET_TYPE)
14323 return 1;
830bfa74 14324 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
4393e105 14325 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
a4443a08 14326 return 1;
830bfa74 14327 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 14328 strict);
a4443a08 14329
f62dbf03 14330 case CONST_DECL:
a723baf1
MM
14331 if (DECL_TEMPLATE_PARM_P (parm))
14332 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
c8094d83 14333 if (arg != integral_constant_value (parm))
f62dbf03
JM
14334 return 1;
14335 return 0;
14336
28e8f3a0 14337 case FIELD_DECL:
027905b4
KL
14338 case TEMPLATE_DECL:
14339 /* Matched cases are handled by the ARG == PARM test above. */
14340 return 1;
14341
5d80a306
DG
14342 case TYPE_ARGUMENT_PACK:
14343 case NONTYPE_ARGUMENT_PACK:
14344 {
14345 tree packed_parms = ARGUMENT_PACK_ARGS (parm);
14346 tree packed_args = ARGUMENT_PACK_ARGS (arg);
14347 int i, len = TREE_VEC_LENGTH (packed_parms);
14348 int argslen = TREE_VEC_LENGTH (packed_args);
14349 int parm_variadic_p = 0;
14350
db60ff18
DG
14351 for (i = 0; i < len; ++i)
14352 {
14353 if (PACK_EXPANSION_P (TREE_VEC_ELT (packed_parms, i)))
14354 {
14355 if (i == len - 1)
14356 /* We can unify against something with a trailing
14357 parameter pack. */
14358 parm_variadic_p = 1;
14359 else
14360 /* Since there is something following the pack
14361 expansion, we cannot unify this template argument
14362 list. */
14363 return 0;
14364 }
14365 }
14366
5d80a306
DG
14367
14368 /* If we don't have enough arguments to satisfy the parameters
14369 (not counting the pack expression at the end), or we have
14370 too many arguments for a parameter list that doesn't end in
14371 a pack expression, we can't unify. */
14372 if (argslen < (len - parm_variadic_p)
14373 || (argslen > len && !parm_variadic_p))
14374 return 1;
14375
14376 /* Unify all of the parameters that precede the (optional)
14377 pack expression. */
14378 for (i = 0; i < len - parm_variadic_p; ++i)
14379 {
14380 if (unify (tparms, targs, TREE_VEC_ELT (packed_parms, i),
14381 TREE_VEC_ELT (packed_args, i), strict))
14382 return 1;
14383 }
14384
14385 if (parm_variadic_p)
14386 return unify_pack_expansion (tparms, targs,
14387 packed_parms, packed_args,
14388 strict, /*call_args_p=*/false,
14389 /*subr=*/false);
14390 return 0;
14391 }
14392
14393 break;
14394
3ad6a8e1
DG
14395 case TYPEOF_TYPE:
14396 case DECLTYPE_TYPE:
14397 /* Cannot deduce anything from TYPEOF_TYPE or DECLTYPE_TYPE
14398 nodes. */
14399 return 0;
14400
1ad8aeeb
DG
14401 case ERROR_MARK:
14402 /* Unification fails if we hit an error node. */
14403 return 1;
14404
8d08fdba 14405 default:
6615c446 14406 gcc_assert (EXPR_P (parm));
c8094d83 14407
98ddffc1 14408 /* We must be looking at an expression. This can happen with
c8094d83
MS
14409 something like:
14410
98ddffc1
NS
14411 template <int I>
14412 void foo(S<I>, S<I + 2>);
050367a3 14413
98ddffc1 14414 This is a "nondeduced context":
050367a3 14415
98ddffc1 14416 [deduct.type]
c8094d83 14417
98ddffc1 14418 The nondeduced contexts are:
050367a3 14419
98ddffc1
NS
14420 --A type that is a template-id in which one or more of
14421 the template-arguments is an expression that references
c8094d83 14422 a template-parameter.
050367a3 14423
98ddffc1
NS
14424 In these cases, we assume deduction succeeded, but don't
14425 actually infer any unifications. */
74601d7c 14426
98ddffc1
NS
14427 if (!uses_template_parms (parm)
14428 && !template_args_equal (parm, arg))
14429 return 1;
14430 else
14431 return 0;
8d08fdba
MS
14432 }
14433}
8d08fdba 14434\f
4684cd27
MM
14435/* Note that DECL can be defined in this translation unit, if
14436 required. */
14437
14438static void
14439mark_definable (tree decl)
14440{
14441 tree clone;
14442 DECL_NOT_REALLY_EXTERN (decl) = 1;
14443 FOR_EACH_CLONE (clone, decl)
14444 DECL_NOT_REALLY_EXTERN (clone) = 1;
14445}
14446
03d0f4af 14447/* Called if RESULT is explicitly instantiated, or is a member of an
4684cd27 14448 explicitly instantiated class. */
03d0f4af 14449
faae18ab 14450void
3a978d72 14451mark_decl_instantiated (tree result, int extern_p)
faae18ab 14452{
415c974c 14453 SET_DECL_EXPLICIT_INSTANTIATION (result);
3ae18eaf 14454
1f6f0cb6
MM
14455 /* If this entity has already been written out, it's too late to
14456 make any modifications. */
14457 if (TREE_ASM_WRITTEN (result))
14458 return;
14459
14460 if (TREE_CODE (result) != FUNCTION_DECL)
14461 /* The TREE_PUBLIC flag for function declarations will have been
14462 set correctly by tsubst. */
14463 TREE_PUBLIC (result) = 1;
14464
346eeb15
JM
14465 /* This might have been set by an earlier implicit instantiation. */
14466 DECL_COMDAT (result) = 0;
14467
4684cd27
MM
14468 if (extern_p)
14469 DECL_NOT_REALLY_EXTERN (result) = 0;
14470 else
faae18ab 14471 {
4684cd27 14472 mark_definable (result);
1a408d07
JM
14473 /* Always make artificials weak. */
14474 if (DECL_ARTIFICIAL (result) && flag_weak)
14475 comdat_linkage (result);
a7d87521
JM
14476 /* For WIN32 we also want to put explicit instantiations in
14477 linkonce sections. */
1a408d07 14478 else if (TREE_PUBLIC (result))
b385c841 14479 maybe_make_one_only (result);
faae18ab 14480 }
c8094d83 14481
4684cd27
MM
14482 /* If EXTERN_P, then this function will not be emitted -- unless
14483 followed by an explicit instantiation, at which point its linkage
14484 will be adjusted. If !EXTERN_P, then this function will be
14485 emitted here. In neither circumstance do we want
14486 import_export_decl to adjust the linkage. */
c8094d83 14487 DECL_INTERFACE_KNOWN (result) = 1;
faae18ab
MS
14488}
14489
e5214479 14490/* Given two function templates PAT1 and PAT2, return:
6467930b
MS
14491
14492 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
14493 -1 if PAT2 is more specialized than PAT1.
e5214479
JM
14494 0 if neither is more specialized.
14495
dda04398
NS
14496 LEN indicates the number of parameters we should consider
14497 (defaulted parameters should not be considered).
14498
14499 The 1998 std underspecified function template partial ordering, and
14500 DR214 addresses the issue. We take pairs of arguments, one from
c51940a2 14501 each of the templates, and deduce them against each other. One of
dda04398
NS
14502 the templates will be more specialized if all the *other*
14503 template's arguments deduce against its arguments and at least one
14504 of its arguments *does* *not* deduce against the other template's
14505 corresponding argument. Deduction is done as for class templates.
14506 The arguments used in deduction have reference and top level cv
14507 qualifiers removed. Iff both arguments were originally reference
14508 types *and* deduction succeeds in both directions, the template
14509 with the more cv-qualified argument wins for that pairing (if
14510 neither is more cv-qualified, they both are equal). Unlike regular
14511 deduction, after all the arguments have been deduced in this way,
14512 we do *not* verify the deduced template argument values can be
14513 substituted into non-deduced contexts, nor do we have to verify
14514 that all template arguments have been deduced. */
c8094d83 14515
6467930b 14516int
dda04398
NS
14517more_specialized_fn (tree pat1, tree pat2, int len)
14518{
14519 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
14520 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
14521 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
14522 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
14523 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
14524 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
14525 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
14526 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
14527 int better1 = 0;
14528 int better2 = 0;
3db45ab5 14529
48884537
NS
14530 /* Remove the this parameter from non-static member functions. If
14531 one is a non-static member function and the other is not a static
14532 member function, remove the first parameter from that function
14533 also. This situation occurs for operator functions where we
14534 locate both a member function (with this pointer) and non-member
14535 operator (with explicit first operand). */
dda04398 14536 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
48884537
NS
14537 {
14538 len--; /* LEN is the number of significant arguments for DECL1 */
14539 args1 = TREE_CHAIN (args1);
14540 if (!DECL_STATIC_FUNCTION_P (decl2))
14541 args2 = TREE_CHAIN (args2);
14542 }
14543 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
14544 {
14545 args2 = TREE_CHAIN (args2);
14546 if (!DECL_STATIC_FUNCTION_P (decl1))
14547 {
14548 len--;
14549 args1 = TREE_CHAIN (args1);
14550 }
14551 }
3db45ab5 14552
ee307009
NS
14553 /* If only one is a conversion operator, they are unordered. */
14554 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
14555 return 0;
c8094d83 14556
dda04398
NS
14557 /* Consider the return type for a conversion function */
14558 if (DECL_CONV_FN_P (decl1))
14559 {
dda04398
NS
14560 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
14561 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
14562 len++;
14563 }
c8094d83 14564
dda04398 14565 processing_template_decl++;
c8094d83 14566
56b1a554
JJ
14567 while (len--
14568 /* Stop when an ellipsis is seen. */
14569 && args1 != NULL_TREE && args2 != NULL_TREE)
dda04398
NS
14570 {
14571 tree arg1 = TREE_VALUE (args1);
14572 tree arg2 = TREE_VALUE (args2);
14573 int deduce1, deduce2;
14574 int quals1 = -1;
14575 int quals2 = -1;
6467930b 14576
5d80a306
DG
14577 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14578 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14579 {
14580 /* When both arguments are pack expansions, we need only
14581 unify the patterns themselves. */
14582 arg1 = PACK_EXPANSION_PATTERN (arg1);
14583 arg2 = PACK_EXPANSION_PATTERN (arg2);
14584
14585 /* This is the last comparison we need to do. */
14586 len = 0;
14587 }
14588
dda04398
NS
14589 if (TREE_CODE (arg1) == REFERENCE_TYPE)
14590 {
14591 arg1 = TREE_TYPE (arg1);
14592 quals1 = cp_type_quals (arg1);
14593 }
c8094d83 14594
dda04398
NS
14595 if (TREE_CODE (arg2) == REFERENCE_TYPE)
14596 {
14597 arg2 = TREE_TYPE (arg2);
14598 quals2 = cp_type_quals (arg2);
14599 }
6467930b 14600
dda04398
NS
14601 if ((quals1 < 0) != (quals2 < 0))
14602 {
14603 /* Only of the args is a reference, see if we should apply
14604 array/function pointer decay to it. This is not part of
14605 DR214, but is, IMHO, consistent with the deduction rules
14606 for the function call itself, and with our earlier
14607 implementation of the underspecified partial ordering
14608 rules. (nathan). */
14609 if (quals1 >= 0)
14610 {
14611 switch (TREE_CODE (arg1))
14612 {
14613 case ARRAY_TYPE:
14614 arg1 = TREE_TYPE (arg1);
14615 /* FALLTHROUGH. */
14616 case FUNCTION_TYPE:
14617 arg1 = build_pointer_type (arg1);
14618 break;
c8094d83 14619
dda04398
NS
14620 default:
14621 break;
14622 }
14623 }
14624 else
14625 {
14626 switch (TREE_CODE (arg2))
14627 {
14628 case ARRAY_TYPE:
14629 arg2 = TREE_TYPE (arg2);
14630 /* FALLTHROUGH. */
14631 case FUNCTION_TYPE:
14632 arg2 = build_pointer_type (arg2);
14633 break;
c8094d83 14634
dda04398
NS
14635 default:
14636 break;
14637 }
14638 }
14639 }
c8094d83 14640
dda04398
NS
14641 arg1 = TYPE_MAIN_VARIANT (arg1);
14642 arg2 = TYPE_MAIN_VARIANT (arg2);
c8094d83 14643
5d80a306
DG
14644 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
14645 {
b1d7b1c0 14646 int i, len2 = list_length (args2);
5d80a306
DG
14647 tree parmvec = make_tree_vec (1);
14648 tree argvec = make_tree_vec (len2);
14649 tree ta = args2;
14650
14651 /* Setup the parameter vector, which contains only ARG1. */
14652 TREE_VEC_ELT (parmvec, 0) = arg1;
14653
14654 /* Setup the argument vector, which contains the remaining
14655 arguments. */
14656 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
14657 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14658
14659 deduce1 = !unify_pack_expansion (tparms1, targs1, parmvec,
14660 argvec, UNIFY_ALLOW_NONE,
14661 /*call_args_p=*/false,
14662 /*subr=*/0);
14663
14664 /* We cannot deduce in the other direction, because ARG1 is
14665 a pack expansion but ARG2 is not. */
14666 deduce2 = 0;
14667 }
14668 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14669 {
b1d7b1c0 14670 int i, len1 = list_length (args1);
5d80a306
DG
14671 tree parmvec = make_tree_vec (1);
14672 tree argvec = make_tree_vec (len1);
14673 tree ta = args1;
14674
14675 /* Setup the parameter vector, which contains only ARG1. */
14676 TREE_VEC_ELT (parmvec, 0) = arg2;
14677
14678 /* Setup the argument vector, which contains the remaining
14679 arguments. */
14680 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
14681 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
14682
14683 deduce2 = !unify_pack_expansion (tparms2, targs2, parmvec,
14684 argvec, UNIFY_ALLOW_NONE,
14685 /*call_args_p=*/false,
14686 /*subr=*/0);
14687
14688 /* We cannot deduce in the other direction, because ARG2 is
14689 a pack expansion but ARG1 is not.*/
14690 deduce1 = 0;
14691 }
14692
14693 else
14694 {
14695 /* The normal case, where neither argument is a pack
14696 expansion. */
14697 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
14698 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
14699 }
dda04398
NS
14700
14701 if (!deduce1)
14702 better2 = -1;
14703 if (!deduce2)
14704 better1 = -1;
14705 if (better1 < 0 && better2 < 0)
14706 /* We've failed to deduce something in either direction.
14707 These must be unordered. */
14708 break;
c8094d83 14709
dda04398
NS
14710 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
14711 {
14712 /* Deduces in both directions, see if quals can
14713 disambiguate. Pretend the worse one failed to deduce. */
14714 if ((quals1 & quals2) == quals2)
14715 deduce1 = 0;
14716 if ((quals1 & quals2) == quals1)
14717 deduce2 = 0;
14718 }
14719 if (deduce1 && !deduce2 && !better2)
14720 better2 = 1;
14721 if (deduce2 && !deduce1 && !better1)
14722 better1 = 1;
c8094d83 14723
5d80a306
DG
14724 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
14725 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
14726 /* We have already processed all of the arguments in our
14727 handing of the pack expansion type. */
14728 len = 0;
14729
dda04398
NS
14730 args1 = TREE_CHAIN (args1);
14731 args2 = TREE_CHAIN (args2);
14732 }
14733
14734 processing_template_decl--;
14735
5d80a306
DG
14736 /* All things being equal, if the next argument is a pack expansion
14737 for one function but not for the other, prefer the
14738 non-variadic function. */
14739 if ((better1 > 0) - (better2 > 0) == 0
14740 && args1 && TREE_VALUE (args1)
14741 && args2 && TREE_VALUE (args2))
14742 {
14743 if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
14744 return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
14745 else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
14746 return 1;
14747 }
14748
dda04398 14749 return (better1 > 0) - (better2 > 0);
73aad9b9 14750}
6467930b 14751
916b63c3 14752/* Determine which of two partial specializations is more specialized.
6467930b 14753
916b63c3
MM
14754 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
14755 to the first partial specialization. The TREE_VALUE is the
14756 innermost set of template parameters for the partial
14757 specialization. PAT2 is similar, but for the second template.
14758
14759 Return 1 if the first partial specialization is more specialized;
14760 -1 if the second is more specialized; 0 if neither is more
14761 specialized.
55ece1b3 14762
916b63c3 14763 See [temp.class.order] for information about determining which of
3db45ab5 14764 two templates is more specialized. */
c8094d83 14765
b5791fdc 14766static int
916b63c3 14767more_specialized_class (tree pat1, tree pat2)
73aad9b9
JM
14768{
14769 tree targs;
916b63c3 14770 tree tmpl1, tmpl2;
73aad9b9 14771 int winner = 0;
5d80a306 14772 bool any_deductions = false;
73aad9b9 14773
916b63c3
MM
14774 tmpl1 = TREE_TYPE (pat1);
14775 tmpl2 = TREE_TYPE (pat2);
14776
c8094d83 14777 /* Just like what happens for functions, if we are ordering between
baa49730
GB
14778 different class template specializations, we may encounter dependent
14779 types in the arguments, and we need our dependency check functions
14780 to behave correctly. */
14781 ++processing_template_decl;
3db45ab5 14782 targs = get_class_bindings (TREE_VALUE (pat1),
916b63c3
MM
14783 CLASSTYPE_TI_ARGS (tmpl1),
14784 CLASSTYPE_TI_ARGS (tmpl2));
73aad9b9 14785 if (targs)
5d80a306
DG
14786 {
14787 --winner;
14788 any_deductions = true;
14789 }
73aad9b9 14790
3db45ab5 14791 targs = get_class_bindings (TREE_VALUE (pat2),
916b63c3
MM
14792 CLASSTYPE_TI_ARGS (tmpl2),
14793 CLASSTYPE_TI_ARGS (tmpl1));
73aad9b9 14794 if (targs)
5d80a306
DG
14795 {
14796 ++winner;
14797 any_deductions = true;
14798 }
baa49730 14799 --processing_template_decl;
6467930b 14800
5d80a306
DG
14801 /* In the case of a tie where at least one of the class templates
14802 has a parameter pack at the end, the template with the most
14803 non-packed parameters wins. */
14804 if (winner == 0
14805 && any_deductions
14806 && (template_args_variadic_p (TREE_PURPOSE (pat1))
14807 || template_args_variadic_p (TREE_PURPOSE (pat2))))
14808 {
14809 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
14810 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
14811 int len1 = TREE_VEC_LENGTH (args1);
14812 int len2 = TREE_VEC_LENGTH (args2);
14813
14814 /* We don't count the pack expansion at the end. */
14815 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
14816 --len1;
14817 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
14818 --len2;
14819
14820 if (len1 > len2)
14821 return 1;
14822 else if (len1 < len2)
14823 return -1;
14824 }
14825
6467930b
MS
14826 return winner;
14827}
73aad9b9
JM
14828
14829/* Return the template arguments that will produce the function signature
e1467ff2 14830 DECL from the function template FN, with the explicit template
a34d3336 14831 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
4393e105 14832 also match. Return NULL_TREE if no satisfactory arguments could be
a34d3336 14833 found. */
c8094d83 14834
76b9a14d 14835static tree
a34d3336 14836get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
73aad9b9 14837{
98c1c668 14838 int ntparms = DECL_NTPARMS (fn);
f31c0a32 14839 tree targs = make_tree_vec (ntparms);
4393e105 14840 tree decl_type;
03017874 14841 tree decl_arg_types;
c166b898
ILT
14842 tree *args;
14843 unsigned int nargs, ix;
14844 tree arg;
98c1c668 14845
4393e105
MM
14846 /* Substitute the explicit template arguments into the type of DECL.
14847 The call to fn_type_unification will handle substitution into the
14848 FN. */
14849 decl_type = TREE_TYPE (decl);
14850 if (explicit_args && uses_template_parms (decl_type))
14851 {
14852 tree tmpl;
14853 tree converted_args;
14854
14855 if (DECL_TEMPLATE_INFO (decl))
14856 tmpl = DECL_TI_TEMPLATE (decl);
14857 else
0e339752 14858 /* We can get here for some invalid specializations. */
4393e105
MM
14859 return NULL_TREE;
14860
14861 converted_args
e7e93965
MM
14862 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
14863 explicit_args, NULL_TREE,
3db45ab5 14864 tf_none,
e7e93965
MM
14865 /*require_all_args=*/false,
14866 /*use_default_args=*/false);
4393e105
MM
14867 if (converted_args == error_mark_node)
14868 return NULL_TREE;
c8094d83
MS
14869
14870 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
4393e105
MM
14871 if (decl_type == error_mark_node)
14872 return NULL_TREE;
14873 }
14874
e5214479 14875 /* Never do unification on the 'this' parameter. */
ba139ba8
MM
14876 decl_arg_types = skip_artificial_parms_for (decl,
14877 TYPE_ARG_TYPES (decl_type));
d7684f2d 14878
c166b898
ILT
14879 nargs = list_length (decl_arg_types);
14880 args = XALLOCAVEC (tree, nargs);
14881 for (arg = decl_arg_types, ix = 0;
14882 arg != NULL_TREE && arg != void_list_node;
14883 arg = TREE_CHAIN (arg), ++ix)
14884 args[ix] = TREE_VALUE (arg);
14885
c8094d83 14886 if (fn_type_unification (fn, explicit_args, targs,
c166b898 14887 args, ix,
8d3631f8 14888 (check_rettype || DECL_CONV_FN_P (fn)
0cbd7506 14889 ? TREE_TYPE (decl_type) : NULL_TREE),
30f86ec3 14890 DEDUCE_EXACT, LOOKUP_NORMAL))
76b9a14d
JM
14891 return NULL_TREE;
14892
76b9a14d
JM
14893 return targs;
14894}
14895
36a117a5
MM
14896/* Return the innermost template arguments that, when applied to a
14897 template specialization whose innermost template parameters are
d0940d56 14898 TPARMS, and whose specialization arguments are SPEC_ARGS, yield the
c8094d83 14899 ARGS.
36a117a5
MM
14900
14901 For example, suppose we have:
14902
14903 template <class T, class U> struct S {};
14904 template <class T> struct S<T*, int> {};
14905
14906 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
916b63c3 14907 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
36a117a5
MM
14908 int}. The resulting vector will be {double}, indicating that `T'
14909 is bound to `double'. */
14910
bd6dd845 14911static tree
916b63c3 14912get_class_bindings (tree tparms, tree spec_args, tree args)
73aad9b9 14913{
3b3ba9f0 14914 int i, ntparms = TREE_VEC_LENGTH (tparms);
916b63c3
MM
14915 tree deduced_args;
14916 tree innermost_deduced_args;
73aad9b9 14917
916b63c3
MM
14918 innermost_deduced_args = make_tree_vec (ntparms);
14919 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
14920 {
14921 deduced_args = copy_node (args);
14922 SET_TMPL_ARGS_LEVEL (deduced_args,
14923 TMPL_ARGS_DEPTH (deduced_args),
14924 innermost_deduced_args);
14925 }
14926 else
3db45ab5 14927 deduced_args = innermost_deduced_args;
916b63c3 14928
3db45ab5
MS
14929 if (unify (tparms, deduced_args,
14930 INNERMOST_TEMPLATE_ARGS (spec_args),
14931 INNERMOST_TEMPLATE_ARGS (args),
0cbd7506 14932 UNIFY_ALLOW_NONE))
fcfb9f96 14933 return NULL_TREE;
73aad9b9
JM
14934
14935 for (i = 0; i < ntparms; ++i)
916b63c3 14936 if (! TREE_VEC_ELT (innermost_deduced_args, i))
73aad9b9
JM
14937 return NULL_TREE;
14938
916b63c3
MM
14939 /* Verify that nondeduced template arguments agree with the type
14940 obtained from argument deduction.
3db45ab5 14941
916b63c3
MM
14942 For example:
14943
14944 struct A { typedef int X; };
14945 template <class T, class U> struct C {};
14946 template <class T> struct C<T, typename T::X> {};
14947
14948 Then with the instantiation `C<A, int>', we can deduce that
14949 `T' is `A' but unify () does not check whether `typename T::X'
14950 is `int'. */
14951 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
14952 if (spec_args == error_mark_node
14953 /* We only need to check the innermost arguments; the other
14954 arguments will always agree. */
14955 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
14956 INNERMOST_TEMPLATE_ARGS (args)))
74601d7c
KL
14957 return NULL_TREE;
14958
4cf36211
DG
14959 /* Now that we have bindings for all of the template arguments,
14960 ensure that the arguments deduced for the template template
14961 parameters have compatible template parameter lists. See the use
14962 of template_template_parm_bindings_ok_p in fn_type_unification
14963 for more information. */
14964 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
14965 return NULL_TREE;
14966
916b63c3 14967 return deduced_args;
73aad9b9
JM
14968}
14969
7ca383e6
MM
14970/* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
14971 Return the TREE_LIST node with the most specialized template, if
14972 any. If there is no most specialized template, the error_mark_node
14973 is returned.
14974
14975 Note that this function does not look at, or modify, the
14976 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
14977 returned is one of the elements of INSTANTIATIONS, callers may
14978 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
14979 and retrieve it from the value returned. */
73aad9b9
JM
14980
14981tree
7ca383e6 14982most_specialized_instantiation (tree templates)
73aad9b9 14983{
104bf76a 14984 tree fn, champ;
73aad9b9 14985
dda04398 14986 ++processing_template_decl;
c8094d83 14987
7ca383e6
MM
14988 champ = templates;
14989 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
73aad9b9 14990 {
dda04398 14991 int fate = 0;
c8094d83 14992
a34d3336
NS
14993 if (get_bindings (TREE_VALUE (champ),
14994 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
14995 NULL_TREE, /*check_ret=*/false))
dda04398
NS
14996 fate--;
14997
a34d3336
NS
14998 if (get_bindings (TREE_VALUE (fn),
14999 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15000 NULL_TREE, /*check_ret=*/false))
dda04398 15001 fate++;
c8094d83 15002
8ca4bf25
MM
15003 if (fate == -1)
15004 champ = fn;
15005 else if (!fate)
73aad9b9 15006 {
8ca4bf25
MM
15007 /* Equally specialized, move to next function. If there
15008 is no next function, nothing's most specialized. */
15009 fn = TREE_CHAIN (fn);
7ca383e6 15010 champ = fn;
8ca4bf25
MM
15011 if (!fn)
15012 break;
73aad9b9
JM
15013 }
15014 }
c8094d83 15015
dda04398
NS
15016 if (champ)
15017 /* Now verify that champ is better than everything earlier in the
15018 instantiation list. */
7ca383e6 15019 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
a34d3336
NS
15020 if (get_bindings (TREE_VALUE (champ),
15021 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
15022 NULL_TREE, /*check_ret=*/false)
15023 || !get_bindings (TREE_VALUE (fn),
15024 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
15025 NULL_TREE, /*check_ret=*/false))
dda04398
NS
15026 {
15027 champ = NULL_TREE;
15028 break;
15029 }
c8094d83 15030
dda04398 15031 processing_template_decl--;
c8094d83 15032
dda04398
NS
15033 if (!champ)
15034 return error_mark_node;
73aad9b9 15035
7ca383e6 15036 return champ;
73aad9b9
JM
15037}
15038
36a117a5 15039/* If DECL is a specialization of some template, return the most
f9a7ae04
MM
15040 general such template. Otherwise, returns NULL_TREE.
15041
15042 For example, given:
36a117a5
MM
15043
15044 template <class T> struct S { template <class U> void f(U); };
15045
15046 if TMPL is `template <class U> void S<int>::f(U)' this will return
15047 the full template. This function will not trace past partial
15048 specializations, however. For example, given in addition:
15049
15050 template <class T> struct S<T*> { template <class U> void f(U); };
15051
15052 if TMPL is `template <class U> void S<int*>::f(U)' this will return
15053 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9 15054
612c671a 15055tree
3a978d72 15056most_general_template (tree decl)
73aad9b9 15057{
f9a7ae04
MM
15058 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
15059 an immediate specialization. */
15060 if (TREE_CODE (decl) == FUNCTION_DECL)
15061 {
15062 if (DECL_TEMPLATE_INFO (decl)) {
15063 decl = DECL_TI_TEMPLATE (decl);
15064
15065 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
15066 template friend. */
15067 if (TREE_CODE (decl) != TEMPLATE_DECL)
15068 return NULL_TREE;
15069 } else
15070 return NULL_TREE;
15071 }
15072
15073 /* Look for more and more general templates. */
15074 while (DECL_TEMPLATE_INFO (decl))
15075 {
10b1d5e7
MM
15076 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
15077 (See cp-tree.h for details.) */
f9a7ae04
MM
15078 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
15079 break;
15080
6e049fcd
KL
15081 if (CLASS_TYPE_P (TREE_TYPE (decl))
15082 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
15083 break;
15084
f9a7ae04
MM
15085 /* Stop if we run into an explicitly specialized class template. */
15086 if (!DECL_NAMESPACE_SCOPE_P (decl)
15087 && DECL_CONTEXT (decl)
15088 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
15089 break;
15090
15091 decl = DECL_TI_TEMPLATE (decl);
15092 }
36a117a5
MM
15093
15094 return decl;
15095}
15096
916b63c3
MM
15097/* Return the most specialized of the class template partial
15098 specializations of TMPL which can produce TYPE, a specialization of
15099 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
15100 a _TYPE node corresponding to the partial specialization, while the
15101 TREE_PURPOSE is the set of template arguments that must be
15102 substituted into the TREE_TYPE in order to generate TYPE.
15103
15104 If the choice of partial specialization is ambiguous, a diagnostic
15105 is issued, and the error_mark_node is returned. If there are no
15106 partial specializations of TMPL matching TYPE, then NULL_TREE is
15107 returned. */
36a117a5 15108
e9659ab0 15109static tree
916b63c3 15110most_specialized_class (tree type, tree tmpl)
36a117a5
MM
15111{
15112 tree list = NULL_TREE;
15113 tree t;
15114 tree champ;
73aad9b9 15115 int fate;
916b63c3
MM
15116 bool ambiguous_p;
15117 tree args;
dc28490d 15118 tree outer_args = NULL_TREE;
73aad9b9 15119
36a117a5 15120 tmpl = most_general_template (tmpl);
916b63c3 15121 args = CLASSTYPE_TI_ARGS (type);
dc28490d
JM
15122
15123 /* For determining which partial specialization to use, only the
15124 innermost args are interesting. */
15125 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
15126 {
15127 outer_args = strip_innermost_template_args (args, 1);
15128 args = INNERMOST_TEMPLATE_ARGS (args);
15129 }
15130
36a117a5 15131 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 15132 {
916b63c3
MM
15133 tree partial_spec_args;
15134 tree spec_args;
dc28490d 15135 tree parms = TREE_VALUE (t);
916b63c3
MM
15136
15137 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
dc28490d
JM
15138 if (outer_args)
15139 {
15140 int i;
15141
fd452cef
JM
15142 ++processing_template_decl;
15143
dc28490d
JM
15144 /* Discard the outer levels of args, and then substitute in the
15145 template args from the enclosing class. */
15146 partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
15147 partial_spec_args = tsubst_template_args
15148 (partial_spec_args, outer_args, tf_none, NULL_TREE);
15149
15150 /* PARMS already refers to just the innermost parms, but the
15151 template parms in partial_spec_args had their levels lowered
15152 by tsubst, so we need to do the same for the parm list. We
15153 can't just tsubst the TREE_VEC itself, as tsubst wants to
15154 treat a TREE_VEC as an argument vector. */
15155 parms = copy_node (parms);
15156 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
15157 TREE_VEC_ELT (parms, i) =
15158 tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
fd452cef
JM
15159
15160 --processing_template_decl;
dc28490d
JM
15161 }
15162 spec_args = get_class_bindings (parms,
3db45ab5 15163 partial_spec_args,
916b63c3 15164 args);
36a117a5 15165 if (spec_args)
73aad9b9 15166 {
dc28490d
JM
15167 if (outer_args)
15168 spec_args = add_to_template_args (outer_args, spec_args);
916b63c3 15169 list = tree_cons (spec_args, TREE_VALUE (t), list);
73aad9b9
JM
15170 TREE_TYPE (list) = TREE_TYPE (t);
15171 }
15172 }
15173
15174 if (! list)
15175 return NULL_TREE;
15176
916b63c3 15177 ambiguous_p = false;
73aad9b9
JM
15178 t = list;
15179 champ = t;
15180 t = TREE_CHAIN (t);
15181 for (; t; t = TREE_CHAIN (t))
15182 {
916b63c3 15183 fate = more_specialized_class (champ, t);
73aad9b9
JM
15184 if (fate == 1)
15185 ;
15186 else
15187 {
15188 if (fate == 0)
15189 {
15190 t = TREE_CHAIN (t);
15191 if (! t)
916b63c3
MM
15192 {
15193 ambiguous_p = true;
15194 break;
15195 }
73aad9b9
JM
15196 }
15197 champ = t;
15198 }
15199 }
15200
916b63c3
MM
15201 if (!ambiguous_p)
15202 for (t = list; t && t != champ; t = TREE_CHAIN (t))
15203 {
15204 fate = more_specialized_class (champ, t);
15205 if (fate != 1)
15206 {
15207 ambiguous_p = true;
15208 break;
15209 }
15210 }
15211
15212 if (ambiguous_p)
73aad9b9 15213 {
916b63c3
MM
15214 const char *str = "candidates are:";
15215 error ("ambiguous class template instantiation for %q#T", type);
15216 for (t = list; t; t = TREE_CHAIN (t))
15217 {
15218 error ("%s %+#T", str, TREE_TYPE (t));
15219 str = " ";
15220 }
15221 return error_mark_node;
73aad9b9
JM
15222 }
15223
15224 return champ;
15225}
15226
eb8845be 15227/* Explicitly instantiate DECL. */
e92cc029 15228
8d08fdba 15229void
eb8845be 15230do_decl_instantiation (tree decl, tree storage)
8d08fdba 15231{
8d08fdba 15232 tree result = NULL_TREE;
faae18ab 15233 int extern_p = 0;
e8abc66f 15234
ad47b891 15235 if (!decl || decl == error_mark_node)
dc957d14 15236 /* An error occurred, for which grokdeclarator has already issued
3fa56191
MM
15237 an appropriate message. */
15238 return;
15239 else if (! DECL_LANG_SPECIFIC (decl))
ec255269 15240 {
0f51ccfc 15241 error ("explicit instantiation of non-template %q#D", decl);
ec255269
MS
15242 return;
15243 }
03d0f4af 15244 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 15245 {
03d0f4af
MM
15246 /* There is an asymmetry here in the way VAR_DECLs and
15247 FUNCTION_DECLs are handled by grokdeclarator. In the case of
15248 the latter, the DECL we get back will be marked as a
15249 template instantiation, and the appropriate
15250 DECL_TEMPLATE_INFO will be set up. This does not happen for
15251 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
15252 should handle VAR_DECLs as it currently handles
15253 FUNCTION_DECLs. */
8259e4f5
PC
15254 if (!DECL_CLASS_SCOPE_P (decl))
15255 {
15256 error ("%qD is not a static data member of a class template", decl);
15257 return;
15258 }
86ac0575 15259 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
e0860732 15260 if (!result || TREE_CODE (result) != VAR_DECL)
03d0f4af 15261 {
0f51ccfc 15262 error ("no matching template for %qD found", decl);
03d0f4af
MM
15263 return;
15264 }
8ea6dfae
SM
15265 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
15266 {
15267 error ("type %qT for explicit instantiation %qD does not match "
15268 "declared type %qT", TREE_TYPE (result), decl,
15269 TREE_TYPE (decl));
15270 return;
15271 }
6633d636
MS
15272 }
15273 else if (TREE_CODE (decl) != FUNCTION_DECL)
15274 {
0f51ccfc 15275 error ("explicit instantiation of %q#D", decl);
6633d636
MS
15276 return;
15277 }
03d0f4af
MM
15278 else
15279 result = decl;
672476cb 15280
03d0f4af 15281 /* Check for various error cases. Note that if the explicit
0e339752 15282 instantiation is valid the RESULT will currently be marked as an
03d0f4af
MM
15283 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
15284 until we get here. */
15285
15286 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 15287 {
07782718
KL
15288 /* DR 259 [temp.spec].
15289
15290 Both an explicit instantiation and a declaration of an explicit
15291 specialization shall not appear in a program unless the explicit
15292 instantiation follows a declaration of the explicit specialization.
03d0f4af 15293
07782718
KL
15294 For a given set of template parameters, if an explicit
15295 instantiation of a template appears after a declaration of an
15296 explicit specialization for that template, the explicit
15297 instantiation has no effect. */
672476cb
MM
15298 return;
15299 }
03d0f4af
MM
15300 else if (DECL_EXPLICIT_INSTANTIATION (result))
15301 {
15302 /* [temp.spec]
98c1c668 15303
03d0f4af 15304 No program shall explicitly instantiate any template more
c8094d83 15305 than once.
03d0f4af 15306
4684cd27
MM
15307 We check DECL_NOT_REALLY_EXTERN so as not to complain when
15308 the first instantiation was `extern' and the second is not,
15309 and EXTERN_P for the opposite case. */
15310 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
cbe5f3b3 15311 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
4684cd27
MM
15312 /* If an "extern" explicit instantiation follows an ordinary
15313 explicit instantiation, the template is instantiated. */
15314 if (extern_p)
03d0f4af
MM
15315 return;
15316 }
15317 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 15318 {
0f51ccfc 15319 error ("no matching template for %qD found", result);
faae18ab
MS
15320 return;
15321 }
03d0f4af 15322 else if (!DECL_TEMPLATE_INFO (result))
6633d636 15323 {
cbe5f3b3 15324 permerror (input_location, "explicit instantiation of non-template %q#D", result);
6633d636
MS
15325 return;
15326 }
15327
f0e01782 15328 if (storage == NULL_TREE)
00595019 15329 ;
faae18ab 15330 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af 15331 {
fcf73884 15332 if (!in_system_header && (cxx_dialect == cxx98))
509c9d60 15333 pedwarn (input_location, OPT_pedantic,
fcf73884 15334 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
0cbd7506 15335 "instantiations");
03d0f4af
MM
15336 extern_p = 1;
15337 }
f0e01782 15338 else
0f51ccfc 15339 error ("storage class %qD applied to template instantiation", storage);
3db45ab5 15340
9c12301f 15341 check_explicit_instantiation_namespace (result);
5566b478 15342 mark_decl_instantiated (result, extern_p);
c91a56d2 15343 if (! extern_p)
3db45ab5 15344 instantiate_decl (result, /*defer_ok=*/1,
eba839f9 15345 /*expl_inst_class_mem_p=*/false);
7177d104
MS
15346}
15347
b5791fdc 15348static void
3a978d72 15349mark_class_instantiated (tree t, int extern_p)
faae18ab
MS
15350{
15351 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
15352 SET_CLASSTYPE_INTERFACE_KNOWN (t);
15353 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
15354 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
15355 if (! extern_p)
15356 {
15357 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
15358 rest_of_type_compilation (t, 1);
15359 }
c8094d83 15360}
e8abc66f 15361
5e0c54e5 15362/* Called from do_type_instantiation through binding_table_foreach to
9bcb9aae 15363 do recursive instantiation for the type bound in ENTRY. */
5e0c54e5
GDR
15364static void
15365bt_instantiate_type_proc (binding_entry entry, void *data)
15366{
15367 tree storage = *(tree *) data;
15368
9e1e64ec 15369 if (MAYBE_CLASS_TYPE_P (entry->type)
5e0c54e5
GDR
15370 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
15371 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
15372}
15373
415c974c
MA
15374/* Called from do_type_instantiation to instantiate a member
15375 (a member function or a static member variable) of an
03fd3f84 15376 explicitly instantiated class template. */
415c974c
MA
15377static void
15378instantiate_class_member (tree decl, int extern_p)
15379{
15380 mark_decl_instantiated (decl, extern_p);
415c974c 15381 if (! extern_p)
3db45ab5 15382 instantiate_decl (decl, /*defer_ok=*/1,
eba839f9 15383 /*expl_inst_class_mem_p=*/true);
415c974c
MA
15384}
15385
a1bcc528
JM
15386/* Perform an explicit instantiation of template class T. STORAGE, if
15387 non-null, is the RID for extern, inline or static. COMPLAIN is
838dfd8a 15388 nonzero if this is called from the parser, zero if called recursively,
a1bcc528 15389 since the standard is unclear (as detailed below). */
c8094d83 15390
7177d104 15391void
3a978d72 15392do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
7177d104 15393{
e8abc66f
MS
15394 int extern_p = 0;
15395 int nomem_p = 0;
5566b478 15396 int static_p = 0;
4746cf84 15397 int previous_instantiation_extern_p = 0;
5566b478 15398
ca79f85d
JM
15399 if (TREE_CODE (t) == TYPE_DECL)
15400 t = TREE_TYPE (t);
15401
7ddedda4 15402 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 15403 {
0f51ccfc 15404 error ("explicit instantiation of non-template type %qT", t);
ca79f85d
JM
15405 return;
15406 }
15407
5566b478 15408 complete_type (t);
7177d104 15409
d0f062fb 15410 if (!COMPLETE_TYPE_P (t))
f0e01782 15411 {
c2ea3a40 15412 if (complain & tf_error)
0f51ccfc 15413 error ("explicit instantiation of %q#T before definition of template",
0cbd7506 15414 t);
f0e01782
MS
15415 return;
15416 }
15417
03d0f4af 15418 if (storage != NULL_TREE)
f0e01782 15419 {
fcf73884 15420 if (!in_system_header)
1ffe7b6e
PC
15421 {
15422 if (storage == ridpointers[(int) RID_EXTERN])
15423 {
15424 if (cxx_dialect == cxx98)
509c9d60
MLI
15425 pedwarn (input_location, OPT_pedantic,
15426 "ISO C++ 1998 forbids the use of %<extern%> on "
15427 "explicit instantiations");
1ffe7b6e
PC
15428 }
15429 else
509c9d60
MLI
15430 pedwarn (input_location, OPT_pedantic,
15431 "ISO C++ forbids the use of %qE"
15432 " on explicit instantiations", storage);
1ffe7b6e 15433 }
03d0f4af
MM
15434
15435 if (storage == ridpointers[(int) RID_INLINE])
15436 nomem_p = 1;
15437 else if (storage == ridpointers[(int) RID_EXTERN])
15438 extern_p = 1;
15439 else if (storage == ridpointers[(int) RID_STATIC])
15440 static_p = 1;
15441 else
15442 {
0f51ccfc 15443 error ("storage class %qD applied to template instantiation",
0cbd7506 15444 storage);
03d0f4af
MM
15445 extern_p = 0;
15446 }
f0e01782
MS
15447 }
15448
370af2d5 15449 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
03d0f4af 15450 {
07782718 15451 /* DR 259 [temp.spec].
a292b002 15452
07782718
KL
15453 Both an explicit instantiation and a declaration of an explicit
15454 specialization shall not appear in a program unless the explicit
15455 instantiation follows a declaration of the explicit specialization.
15456
15457 For a given set of template parameters, if an explicit
15458 instantiation of a template appears after a declaration of an
15459 explicit specialization for that template, the explicit
15460 instantiation has no effect. */
03d0f4af
MM
15461 return;
15462 }
15463 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 15464 {
03d0f4af
MM
15465 /* [temp.spec]
15466
15467 No program shall explicitly instantiate any template more
c8094d83 15468 than once.
03d0f4af 15469
0cbd7506 15470 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
4746cf84 15471 instantiation was `extern'. If EXTERN_P then the second is.
4684cd27 15472 These cases are OK. */
4746cf84
MA
15473 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
15474
15475 if (!previous_instantiation_extern_p && !extern_p
c2ea3a40 15476 && (complain & tf_error))
cbe5f3b3 15477 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
c8094d83 15478
03d0f4af
MM
15479 /* If we've already instantiated the template, just return now. */
15480 if (!CLASSTYPE_INTERFACE_ONLY (t))
15481 return;
44a8d0b3 15482 }
e8abc66f 15483
9c12301f 15484 check_explicit_instantiation_namespace (TYPE_NAME (t));
03d0f4af 15485 mark_class_instantiated (t, extern_p);
03d0f4af 15486
e8abc66f
MS
15487 if (nomem_p)
15488 return;
15489
7177d104 15490 {
db5ae43f 15491 tree tmp;
5566b478 15492
03d0f4af
MM
15493 /* In contrast to implicit instantiation, where only the
15494 declarations, and not the definitions, of members are
15495 instantiated, we have here:
15496
0cbd7506 15497 [temp.explicit]
03d0f4af
MM
15498
15499 The explicit instantiation of a class template specialization
15500 implies the instantiation of all of its members not
15501 previously explicitly specialized in the translation unit
c8094d83 15502 containing the explicit instantiation.
03d0f4af
MM
15503
15504 Of course, we can't instantiate member template classes, since
15505 we don't have any arguments for them. Note that the standard
dc957d14 15506 is unclear on whether the instantiation of the members are
415c974c 15507 *explicit* instantiations or not. However, the most natural
03fd3f84 15508 interpretation is that it should be an explicit instantiation. */
03d0f4af 15509
5566b478
MS
15510 if (! static_p)
15511 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 15512 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 15513 && DECL_TEMPLATE_INSTANTIATION (tmp))
415c974c 15514 instantiate_class_member (tmp, extern_p);
5566b478
MS
15515
15516 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
15517 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
415c974c 15518 instantiate_class_member (tmp, extern_p);
7177d104 15519
5e0c54e5
GDR
15520 if (CLASSTYPE_NESTED_UTDS (t))
15521 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
0cbd7506 15522 bt_instantiate_type_proc, &storage);
a292b002 15523 }
8d08fdba 15524}
a28e3c7f 15525
36a117a5
MM
15526/* Given a function DECL, which is a specialization of TMPL, modify
15527 DECL to be a re-instantiation of TMPL with the same template
15528 arguments. TMPL should be the template into which tsubst'ing
15529 should occur for DECL, not the most general template.
830bfa74
MM
15530
15531 One reason for doing this is a scenario like this:
15532
15533 template <class T>
15534 void f(const T&, int i);
15535
15536 void g() { f(3, 7); }
15537
15538 template <class T>
15539 void f(const T& t, const int i) { }
15540
15541 Note that when the template is first instantiated, with
15542 instantiate_template, the resulting DECL will have no name for the
15543 first parameter, and the wrong type for the second. So, when we go
15544 to instantiate the DECL, we regenerate it. */
15545
e9659ab0 15546static void
3a978d72 15547regenerate_decl_from_template (tree decl, tree tmpl)
830bfa74 15548{
f9a7ae04
MM
15549 /* The arguments used to instantiate DECL, from the most general
15550 template. */
830bfa74 15551 tree args;
830bfa74 15552 tree code_pattern;
830bfa74
MM
15553
15554 args = DECL_TI_ARGS (decl);
15555 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
15556
2b907f5c
KL
15557 /* Make sure that we can see identifiers, and compute access
15558 correctly. */
15559 push_access_scope (decl);
15560
c7222c02
MM
15561 if (TREE_CODE (decl) == FUNCTION_DECL)
15562 {
15563 tree decl_parm;
15564 tree pattern_parm;
15565 tree specs;
15566 int args_depth;
15567 int parms_depth;
c8094d83 15568
c7222c02 15569 args_depth = TMPL_ARGS_DEPTH (args);
c8094d83 15570 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
c7222c02
MM
15571 if (args_depth > parms_depth)
15572 args = get_innermost_template_args (args, parms_depth);
15573
15574 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
15575 args, tf_error, NULL_TREE);
15576 if (specs)
15577 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
15578 specs);
15579
15580 /* Merge parameter declarations. */
c8094d83 15581 decl_parm = skip_artificial_parms_for (decl,
c7222c02 15582 DECL_ARGUMENTS (decl));
c8094d83 15583 pattern_parm
c7222c02
MM
15584 = skip_artificial_parms_for (code_pattern,
15585 DECL_ARGUMENTS (code_pattern));
5d80a306 15586 while (decl_parm && !FUNCTION_PARAMETER_PACK_P (pattern_parm))
c7222c02
MM
15587 {
15588 tree parm_type;
b17bba6d 15589 tree attributes;
5d80a306 15590
c7222c02
MM
15591 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15592 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
15593 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
15594 NULL_TREE);
02bab9db 15595 parm_type = type_decays_to (parm_type);
c7222c02
MM
15596 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15597 TREE_TYPE (decl_parm) = parm_type;
b17bba6d
MM
15598 attributes = DECL_ATTRIBUTES (pattern_parm);
15599 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15600 {
15601 DECL_ATTRIBUTES (decl_parm) = attributes;
15602 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15603 }
c7222c02
MM
15604 decl_parm = TREE_CHAIN (decl_parm);
15605 pattern_parm = TREE_CHAIN (pattern_parm);
15606 }
5d80a306
DG
15607 /* Merge any parameters that match with the function parameter
15608 pack. */
15609 if (pattern_parm && FUNCTION_PARAMETER_PACK_P (pattern_parm))
15610 {
15611 int i, len;
15612 tree expanded_types;
15613 /* Expand the TYPE_PACK_EXPANSION that provides the types for
15614 the parameters in this function parameter pack. */
15615 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
15616 args, tf_error, NULL_TREE);
15617 len = TREE_VEC_LENGTH (expanded_types);
15618 for (i = 0; i < len; i++)
15619 {
15620 tree parm_type;
15621 tree attributes;
15622
15623 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
15624 /* Rename the parameter to include the index. */
15625 DECL_NAME (decl_parm) =
15626 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
15627 parm_type = TREE_VEC_ELT (expanded_types, i);
15628 parm_type = type_decays_to (parm_type);
15629 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
15630 TREE_TYPE (decl_parm) = parm_type;
15631 attributes = DECL_ATTRIBUTES (pattern_parm);
15632 if (DECL_ATTRIBUTES (decl_parm) != attributes)
15633 {
15634 DECL_ATTRIBUTES (decl_parm) = attributes;
15635 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
15636 }
15637 decl_parm = TREE_CHAIN (decl_parm);
15638 }
15639 }
c7222c02
MM
15640 /* Merge additional specifiers from the CODE_PATTERN. */
15641 if (DECL_DECLARED_INLINE_P (code_pattern)
15642 && !DECL_DECLARED_INLINE_P (decl))
15643 DECL_DECLARED_INLINE_P (decl) = 1;
c7222c02
MM
15644 }
15645 else if (TREE_CODE (decl) == VAR_DECL)
b794e321
MM
15646 DECL_INITIAL (decl) =
15647 tsubst_expr (DECL_INITIAL (code_pattern), args,
015c2c66
MM
15648 tf_error, DECL_TI_TEMPLATE (decl),
15649 /*integral_constant_expression_p=*/false);
c7222c02
MM
15650 else
15651 gcc_unreachable ();
36a117a5 15652
2b59fc25 15653 pop_access_scope (decl);
830bfa74
MM
15654}
15655
a723baf1
MM
15656/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
15657 substituted to get DECL. */
15658
d58b7c2d 15659tree
a723baf1
MM
15660template_for_substitution (tree decl)
15661{
15662 tree tmpl = DECL_TI_TEMPLATE (decl);
15663
15664 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
15665 for the instantiation. This is not always the most general
15666 template. Consider, for example:
15667
0cbd7506 15668 template <class T>
a723baf1 15669 struct S { template <class U> void f();
0cbd7506 15670 template <> void f<int>(); };
a723baf1
MM
15671
15672 and an instantiation of S<double>::f<int>. We want TD to be the
15673 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
15674 while (/* An instantiation cannot have a definition, so we need a
15675 more general template. */
15676 DECL_TEMPLATE_INSTANTIATION (tmpl)
15677 /* We must also deal with friend templates. Given:
15678
c8094d83 15679 template <class T> struct S {
a723baf1
MM
15680 template <class U> friend void f() {};
15681 };
15682
15683 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
15684 so far as the language is concerned, but that's still
15685 where we get the pattern for the instantiation from. On
15686 other hand, if the definition comes outside the class, say:
15687
c8094d83 15688 template <class T> struct S {
a723baf1
MM
15689 template <class U> friend void f();
15690 };
15691 template <class U> friend void f() {}
15692
15693 we don't need to look any further. That's what the check for
15694 DECL_INITIAL is for. */
15695 || (TREE_CODE (decl) == FUNCTION_DECL
15696 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
15697 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
15698 {
15699 /* The present template, TD, should not be a definition. If it
15700 were a definition, we should be using it! Note that we
15701 cannot restructure the loop to just keep going until we find
15702 a template with a definition, since that might go too far if
15703 a specialization was declared, but not defined. */
50bc768d
NS
15704 gcc_assert (TREE_CODE (decl) != VAR_DECL
15705 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
c8094d83 15706
a723baf1
MM
15707 /* Fetch the more general template. */
15708 tmpl = DECL_TI_TEMPLATE (tmpl);
15709 }
15710
15711 return tmpl;
15712}
15713
15305a73
JM
15714/* Returns true if we need to instantiate this template instance even if we
15715 know we aren't going to emit it.. */
15716
15717bool
15718always_instantiate_p (tree decl)
15719{
15720 /* We always instantiate inline functions so that we can inline them. An
15721 explicit instantiation declaration prohibits implicit instantiation of
15722 non-inline functions. With high levels of optimization, we would
15723 normally inline non-inline functions -- but we're not allowed to do
15724 that for "extern template" functions. Therefore, we check
15725 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
15726 return ((TREE_CODE (decl) == FUNCTION_DECL
15727 && DECL_DECLARED_INLINE_P (decl))
15728 /* And we need to instantiate static data members so that
15729 their initializers are available in integral constant
15730 expressions. */
15731 || (TREE_CODE (decl) == VAR_DECL
15732 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)));
15733}
15734
16d53b64 15735/* Produce the definition of D, a _DECL generated from a template. If
838dfd8a 15736 DEFER_OK is nonzero, then we don't have to actually do the
415c974c
MA
15737 instantiation now; we just have to do it sometime. Normally it is
15738 an error if this is an explicit instantiation but D is undefined.
eba839f9
MM
15739 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
15740 explicitly instantiated class template. */
f84b4be9 15741
a28e3c7f 15742tree
3db45ab5 15743instantiate_decl (tree d, int defer_ok,
eba839f9 15744 bool expl_inst_class_mem_p)
a28e3c7f 15745{
36a117a5 15746 tree tmpl = DECL_TI_TEMPLATE (d);
65f8b0fb
MM
15747 tree gen_args;
15748 tree args;
830bfa74 15749 tree td;
36a117a5
MM
15750 tree code_pattern;
15751 tree spec;
15752 tree gen_tmpl;
b794e321 15753 bool pattern_defined;
31a714f6 15754 int need_push;
82a98427 15755 location_t saved_loc = input_location;
f7e4e484 15756 bool external_p;
c8094d83 15757
36a117a5
MM
15758 /* This function should only be used to instantiate templates for
15759 functions and static member variables. */
50bc768d
NS
15760 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
15761 || TREE_CODE (d) == VAR_DECL);
36a117a5 15762
cec24319
MM
15763 /* Variables are never deferred; if instantiation is required, they
15764 are instantiated right away. That allows for better code in the
15765 case that an expression refers to the value of the variable --
15766 if the variable has a constant value the referring expression can
15767 take advantage of that fact. */
15768 if (TREE_CODE (d) == VAR_DECL)
15769 defer_ok = 0;
15770
db9b2174
MM
15771 /* Don't instantiate cloned functions. Instead, instantiate the
15772 functions they cloned. */
15773 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
15774 d = DECL_CLONED_FUNCTION (d);
15775
2b110bfc
JM
15776 if (DECL_TEMPLATE_INSTANTIATED (d)
15777 || DECL_TEMPLATE_SPECIALIZATION (d))
15778 /* D has already been instantiated or explicitly specialized, so
15779 there's nothing for us to do here.
15780
15781 It might seem reasonable to check whether or not D is an explicit
15782 instantiation, and, if so, stop here. But when an explicit
15783 instantiation is deferred until the end of the compilation,
15784 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
15785 the instantiation. */
36a117a5
MM
15786 return d;
15787
15305a73
JM
15788 /* Check to see whether we know that this template will be
15789 instantiated in some other file, as with "extern template"
15790 extension. */
15791 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
15792
15793 /* In general, we do not instantiate such templates. */
15794 if (external_p && !always_instantiate_p (d))
15795 return d;
15796
36a117a5 15797 gen_tmpl = most_general_template (tmpl);
65f8b0fb 15798 gen_args = DECL_TI_ARGS (d);
2b110bfc
JM
15799
15800 if (tmpl != gen_tmpl)
15801 /* We should already have the extra args. */
15802 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
15803 == TMPL_ARGS_DEPTH (gen_args));
15804 /* And what's in the hash table should match D. */
15805 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
15806 || spec == NULL_TREE);
36a117a5
MM
15807
15808 /* This needs to happen before any tsubsting. */
15809 if (! push_tinst_level (d))
15810 return d;
15811
297a5329
JM
15812 timevar_push (TV_PARSE);
15813
4d85e00e 15814 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
a723baf1
MM
15815 for the instantiation. */
15816 td = template_for_substitution (d);
fee23f54 15817 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 15818
2d22db1f 15819 /* We should never be trying to instantiate a member of a class
3db45ab5 15820 template or partial specialization. */
2d22db1f 15821 gcc_assert (d != code_pattern);
3db45ab5 15822
76d3baad
KL
15823 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
15824 || DECL_TEMPLATE_SPECIALIZATION (td))
649fc72d
NS
15825 /* In the case of a friend template whose definition is provided
15826 outside the class, we may have too many arguments. Drop the
76d3baad 15827 ones we don't need. The same is true for specializations. */
649fc72d
NS
15828 args = get_innermost_template_args
15829 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
15830 else
15831 args = gen_args;
65f8b0fb 15832
5566b478 15833 if (TREE_CODE (d) == FUNCTION_DECL)
39c76b4f 15834 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
5566b478 15835 else
36a117a5 15836 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
b794e321
MM
15837
15838 /* We may be in the middle of deferred access check. Disable it now. */
15839 push_deferring_access_checks (dk_no_deferred);
15840
4684cd27
MM
15841 /* Unless an explicit instantiation directive has already determined
15842 the linkage of D, remember that a definition is available for
15843 this entity. */
c8094d83 15844 if (pattern_defined
4684cd27
MM
15845 && !DECL_INTERFACE_KNOWN (d)
15846 && !DECL_NOT_REALLY_EXTERN (d))
15847 mark_definable (d);
de22184b 15848
f31686a3 15849 input_location = DECL_SOURCE_LOCATION (d);
de22184b 15850
eba839f9
MM
15851 /* If D is a member of an explicitly instantiated class template,
15852 and no definition is available, treat it like an implicit
3db45ab5
MS
15853 instantiation. */
15854 if (!pattern_defined && expl_inst_class_mem_p
15855 && DECL_EXPLICIT_INSTANTIATION (d))
5566b478 15856 {
4684cd27 15857 DECL_NOT_REALLY_EXTERN (d) = 0;
eba839f9 15858 DECL_INTERFACE_KNOWN (d) = 0;
4684cd27 15859 SET_DECL_IMPLICIT_INSTANTIATION (d);
5566b478
MS
15860 }
15861
4f2b0fb2
NS
15862 if (!defer_ok)
15863 {
15864 /* Recheck the substitutions to obtain any warning messages
15865 about ignoring cv qualifiers. */
15866 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
15867 tree type = TREE_TYPE (gen);
15868
0e902d98 15869 /* Make sure that we can see identifiers, and compute access
2b59fc25
KL
15870 correctly. D is already the target FUNCTION_DECL with the
15871 right context. */
15872 push_access_scope (d);
0e902d98 15873
4f2b0fb2
NS
15874 if (TREE_CODE (gen) == FUNCTION_DECL)
15875 {
23fca1f5 15876 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
5d80a306
DG
15877 tsubst_exception_specification (type, gen_args, tf_warning_or_error,
15878 d);
4f2b0fb2
NS
15879 /* Don't simply tsubst the function type, as that will give
15880 duplicate warnings about poor parameter qualifications.
15881 The function arguments are the same as the decl_arguments
c6002625 15882 without the top level cv qualifiers. */
4f2b0fb2
NS
15883 type = TREE_TYPE (type);
15884 }
23fca1f5 15885 tsubst (type, gen_args, tf_warning_or_error, d);
0e902d98 15886
2b59fc25 15887 pop_access_scope (d);
4f2b0fb2 15888 }
c8094d83 15889
16d53b64 15890 /* Defer all other templates, unless we have been explicitly
f7e4e484
MM
15891 forbidden from doing so. */
15892 if (/* If there is no definition, we cannot instantiate the
15893 template. */
3db45ab5 15894 ! pattern_defined
f7e4e484
MM
15895 /* If it's OK to postpone instantiation, do so. */
15896 || defer_ok
15897 /* If this is a static data member that will be defined
15898 elsewhere, we don't want to instantiate the entire data
15899 member, but we do want to instantiate the initializer so that
15900 we can substitute that elsewhere. */
15901 || (external_p && TREE_CODE (d) == VAR_DECL))
16d53b64 15902 {
b794e321
MM
15903 /* The definition of the static data member is now required so
15904 we must substitute the initializer. */
15905 if (TREE_CODE (d) == VAR_DECL
3db45ab5 15906 && !DECL_INITIAL (d)
b794e321
MM
15907 && DECL_INITIAL (code_pattern))
15908 {
4bff36d3
MM
15909 tree ns;
15910 tree init;
15911
15912 ns = decl_namespace_context (d);
27a725e2 15913 push_nested_namespace (ns);
b794e321 15914 push_nested_class (DECL_CONTEXT (d));
3db45ab5 15915 init = tsubst_expr (DECL_INITIAL (code_pattern),
4bff36d3 15916 args,
015c2c66
MM
15917 tf_warning_or_error, NULL_TREE,
15918 /*integral_constant_expression_p=*/false);
d174af6c
MM
15919 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
15920 /*asmspec_tree=*/NULL_TREE,
86414779 15921 LOOKUP_ONLYCONVERTING);
b794e321 15922 pop_nested_class ();
27a725e2 15923 pop_nested_namespace (ns);
b794e321
MM
15924 }
15925
f7e4e484
MM
15926 /* We restore the source position here because it's used by
15927 add_pending_template. */
82a98427 15928 input_location = saved_loc;
c27be9b9 15929
c8094d83 15930 if (at_eof && !pattern_defined
da3933ba
JM
15931 && DECL_EXPLICIT_INSTANTIATION (d)
15932 && DECL_NOT_REALLY_EXTERN (d))
03d0f4af
MM
15933 /* [temp.explicit]
15934
15935 The definition of a non-exported function template, a
15936 non-exported member function template, or a non-exported
15937 member function or static data member of a class template
15938 shall be present in every translation unit in which it is
15939 explicitly instantiated. */
cbe5f3b3
MLI
15940 permerror (input_location, "explicit instantiation of %qD "
15941 "but no definition available", d);
03d0f4af 15942
f7e4e484
MM
15943 /* ??? Historically, we have instantiated inline functions, even
15944 when marked as "extern template". */
15945 if (!(external_p && TREE_CODE (d) == VAR_DECL))
15946 add_pending_template (d);
de22184b 15947 goto out;
5566b478 15948 }
4684cd27
MM
15949 /* Tell the repository that D is available in this translation unit
15950 -- and see if it is supposed to be instantiated here. */
15951 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
15952 {
15953 /* In a PCH file, despite the fact that the repository hasn't
15954 requested instantiation in the PCH it is still possible that
15955 an instantiation will be required in a file that includes the
15956 PCH. */
15957 if (pch_file)
15958 add_pending_template (d);
15959 /* Instantiate inline functions so that the inliner can do its
15960 job, even though we'll not be emitting a copy of this
15961 function. */
4847609e 15962 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
4684cd27
MM
15963 goto out;
15964 }
5566b478 15965
6de9cd9a 15966 need_push = !cfun || !global_bindings_p ();
31a714f6
MM
15967 if (need_push)
15968 push_to_top_level ();
414ea4aa 15969
66e0c440
KL
15970 /* Mark D as instantiated so that recursive calls to
15971 instantiate_decl do not try to instantiate it again. */
15972 DECL_TEMPLATE_INSTANTIATED (d) = 1;
15973
2b0a63a3
MM
15974 /* Regenerate the declaration in case the template has been modified
15975 by a subsequent redeclaration. */
15976 regenerate_decl_from_template (d, td);
4684cd27 15977
120722ac 15978 /* We already set the file and line above. Reset them now in case
6de9cd9a 15979 they changed as a result of calling regenerate_decl_from_template. */
f31686a3 15980 input_location = DECL_SOURCE_LOCATION (d);
5156628f 15981
5566b478
MS
15982 if (TREE_CODE (d) == VAR_DECL)
15983 {
e92fb501
MM
15984 tree init;
15985
1d62c33e
MM
15986 /* Clear out DECL_RTL; whatever was there before may not be right
15987 since we've reset the type of the declaration. */
15988 SET_DECL_RTL (d, NULL_RTX);
5566b478 15989 DECL_IN_AGGR_P (d) = 0;
ea56c40c 15990
e92fb501
MM
15991 /* The initializer is placed in DECL_INITIAL by
15992 regenerate_decl_from_template. Pull it out so that
3600f678 15993 cp_finish_decl can process it. */
e92fb501
MM
15994 init = DECL_INITIAL (d);
15995 DECL_INITIAL (d) = NULL_TREE;
15996 DECL_INITIALIZED_P (d) = 0;
15997
4684cd27
MM
15998 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
15999 initializer. That function will defer actual emission until
16000 we have a chance to determine linkage. */
16001 DECL_EXTERNAL (d) = 0;
16002
73a8adb6 16003 /* Enter the scope of D so that access-checking works correctly. */
4684cd27 16004 push_nested_class (DECL_CONTEXT (d));
3600f678 16005 cp_finish_decl (d, init, false, NULL_TREE, 0);
73a8adb6 16006 pop_nested_class ();
5566b478
MS
16007 }
16008 else if (TREE_CODE (d) == FUNCTION_DECL)
16009 {
6bbf1598 16010 htab_t saved_local_specializations;
a723baf1
MM
16011 tree subst_decl;
16012 tree tmpl_parm;
16013 tree spec_parm;
6bbf1598
MM
16014
16015 /* Save away the current list, in case we are instantiating one
16016 template from within the body of another. */
16017 saved_local_specializations = local_specializations;
16018
6dfbb909 16019 /* Set up the list of local specializations. */
c8094d83 16020 local_specializations = htab_create (37,
69f794a7 16021 hash_local_specialization,
a723baf1 16022 eq_local_specializations,
6dfbb909
MM
16023 NULL);
16024
558475f0 16025 /* Set up context. */
058b15c1 16026 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
5566b478 16027
a723baf1
MM
16028 /* Create substitution entries for the parameters. */
16029 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
16030 tmpl_parm = DECL_ARGUMENTS (subst_decl);
16031 spec_parm = DECL_ARGUMENTS (d);
16032 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
16033 {
16034 register_local_specialization (spec_parm, tmpl_parm);
16035 spec_parm = skip_artificial_parms_for (d, spec_parm);
16036 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
16037 }
5d80a306 16038 while (tmpl_parm && !FUNCTION_PARAMETER_PACK_P (tmpl_parm))
a723baf1
MM
16039 {
16040 register_local_specialization (spec_parm, tmpl_parm);
16041 tmpl_parm = TREE_CHAIN (tmpl_parm);
16042 spec_parm = TREE_CHAIN (spec_parm);
16043 }
5d80a306
DG
16044 if (tmpl_parm && FUNCTION_PARAMETER_PACK_P (tmpl_parm))
16045 {
5d80a306
DG
16046 /* Register the (value) argument pack as a specialization of
16047 TMPL_PARM, then move on. */
6afcfe0a 16048 tree argpack = make_fnparm_pack (spec_parm);
5d80a306
DG
16049 register_local_specialization (argpack, tmpl_parm);
16050 tmpl_parm = TREE_CHAIN (tmpl_parm);
6afcfe0a 16051 spec_parm = NULL_TREE;
5d80a306 16052 }
50bc768d 16053 gcc_assert (!spec_parm);
a723baf1 16054
558475f0
MM
16055 /* Substitute into the body of the function. */
16056 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
015c2c66
MM
16057 tf_warning_or_error, tmpl,
16058 /*integral_constant_expression_p=*/false);
558475f0 16059
75407da3
SP
16060 /* Set the current input_location to the end of the function
16061 so that finish_function knows where we are. */
16062 input_location = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
16063
6dfbb909
MM
16064 /* We don't need the local specializations any more. */
16065 htab_delete (local_specializations);
6bbf1598 16066 local_specializations = saved_local_specializations;
6dfbb909 16067
4d6abc1c 16068 /* Finish the function. */
b2dd096b 16069 d = finish_function (0);
8cd2462c 16070 expand_or_defer_fn (d);
5566b478
MS
16071 }
16072
971cbc14
MM
16073 /* We're not deferring instantiation any more. */
16074 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
16075
31a714f6
MM
16076 if (need_push)
16077 pop_from_top_level ();
414ea4aa 16078
de22184b 16079out:
82a98427 16080 input_location = saved_loc;
7d021397 16081 pop_deferring_access_checks ();
5566b478 16082 pop_tinst_level ();
a28e3c7f 16083
297a5329
JM
16084 timevar_pop (TV_PARSE);
16085
a28e3c7f
MS
16086 return d;
16087}
5566b478 16088
0aafb128 16089/* Run through the list of templates that we wish we could
35046a54
KL
16090 instantiate, and instantiate any we can. RETRIES is the
16091 number of times we retry pending template instantiation. */
0aafb128 16092
35046a54
KL
16093void
16094instantiate_pending_templates (int retries)
0aafb128 16095{
0aafb128 16096 int reconsider;
aad626f7 16097 location_t saved_loc = input_location;
35046a54
KL
16098
16099 /* Instantiating templates may trigger vtable generation. This in turn
16100 may require further template instantiations. We place a limit here
16101 to avoid infinite loop. */
16102 if (pending_templates && retries >= max_tinst_depth)
16103 {
e2c3721c 16104 tree decl = pending_templates->tinst->decl;
96c993a8 16105
dee15844 16106 error ("template instantiation depth exceeds maximum of %d"
96c993a8
JM
16107 " instantiating %q+D, possibly from virtual table generation"
16108 " (use -ftemplate-depth-NN to increase the maximum)",
16109 max_tinst_depth, decl);
16110 if (TREE_CODE (decl) == FUNCTION_DECL)
16111 /* Pretend that we defined it. */
16112 DECL_INITIAL (decl) = error_mark_node;
35046a54
KL
16113 return;
16114 }
16115
c8094d83 16116 do
0aafb128 16117 {
e2c3721c
PB
16118 struct pending_template **t = &pending_templates;
16119 struct pending_template *last = NULL;
0aafb128 16120 reconsider = 0;
0aafb128
MM
16121 while (*t)
16122 {
e2c3721c
PB
16123 tree instantiation = reopen_tinst_level ((*t)->tinst);
16124 bool complete = false;
0aafb128 16125
2f939d94 16126 if (TYPE_P (instantiation))
0aafb128
MM
16127 {
16128 tree fn;
16129
d0f062fb 16130 if (!COMPLETE_TYPE_P (instantiation))
0aafb128
MM
16131 {
16132 instantiate_class_template (instantiation);
16133 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
c8094d83 16134 for (fn = TYPE_METHODS (instantiation);
0aafb128
MM
16135 fn;
16136 fn = TREE_CHAIN (fn))
16137 if (! DECL_ARTIFICIAL (fn))
3db45ab5 16138 instantiate_decl (fn,
eba839f9
MM
16139 /*defer_ok=*/0,
16140 /*expl_inst_class_mem_p=*/false);
d0f062fb 16141 if (COMPLETE_TYPE_P (instantiation))
35046a54 16142 reconsider = 1;
0aafb128
MM
16143 }
16144
e2c3721c 16145 complete = COMPLETE_TYPE_P (instantiation);
0aafb128
MM
16146 }
16147 else
16148 {
16d53b64 16149 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
16150 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
16151 {
3db45ab5 16152 instantiation
eba839f9
MM
16153 = instantiate_decl (instantiation,
16154 /*defer_ok=*/0,
16155 /*expl_inst_class_mem_p=*/false);
0aafb128 16156 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
35046a54 16157 reconsider = 1;
0aafb128
MM
16158 }
16159
e2c3721c
PB
16160 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
16161 || DECL_TEMPLATE_INSTANTIATED (instantiation));
16162 }
16163
16164 if (complete)
16165 /* If INSTANTIATION has been instantiated, then we don't
16166 need to consider it again in the future. */
16167 *t = (*t)->next;
16168 else
16169 {
16170 last = *t;
16171 t = &(*t)->next;
0aafb128 16172 }
84e5ca0f 16173 tinst_depth = 0;
e2c3721c 16174 current_tinst_level = NULL;
0aafb128 16175 }
46ccf50a 16176 last_pending_template = last;
c8094d83 16177 }
0aafb128
MM
16178 while (reconsider);
16179
aad626f7 16180 input_location = saved_loc;
0aafb128
MM
16181}
16182
fd74ca0b
MM
16183/* Substitute ARGVEC into T, which is a list of initializers for
16184 either base class or a non-static data member. The TREE_PURPOSEs
16185 are DECLs, and the TREE_VALUEs are the initializer values. Used by
16186 instantiate_decl. */
4393e105 16187
824b9a4c 16188static tree
3a978d72 16189tsubst_initializer_list (tree t, tree argvec)
5566b478 16190{
2282d28d 16191 tree inits = NULL_TREE;
5566b478
MS
16192
16193 for (; t; t = TREE_CHAIN (t))
16194 {
fd74ca0b
MM
16195 tree decl;
16196 tree init;
5d80a306
DG
16197 tree expanded_bases = NULL_TREE;
16198 tree expanded_arguments = NULL_TREE;
16199 int i, len = 1;
fd74ca0b 16200
5d80a306
DG
16201 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
16202 {
16203 tree expr;
16204 tree arg;
16205
16206 /* Expand the base class expansion type into separate base
16207 classes. */
16208 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
16209 tf_warning_or_error,
16210 NULL_TREE);
16211 if (expanded_bases == error_mark_node)
16212 continue;
16213
16214 /* We'll be building separate TREE_LISTs of arguments for
16215 each base. */
16216 len = TREE_VEC_LENGTH (expanded_bases);
16217 expanded_arguments = make_tree_vec (len);
16218 for (i = 0; i < len; i++)
16219 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
16220
16221 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
16222 expand each argument in the TREE_VALUE of t. */
16223 expr = make_node (EXPR_PACK_EXPANSION);
16224 PACK_EXPANSION_PARAMETER_PACKS (expr) =
16225 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
16226
625b6d91
DG
16227 if (TREE_VALUE (t) == void_type_node)
16228 /* VOID_TYPE_NODE is used to indicate
16229 value-initialization. */
16230 {
16231 for (i = 0; i < len; i++)
16232 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
16233 }
16234 else
16235 {
16236 /* Substitute parameter packs into each argument in the
16237 TREE_LIST. */
16238 in_base_initializer = 1;
16239 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
16240 {
16241 tree expanded_exprs;
16242
16243 /* Expand the argument. */
16244 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
16245 expanded_exprs
16246 = tsubst_pack_expansion (expr, argvec,
16247 tf_warning_or_error,
16248 NULL_TREE);
aecaad79
PC
16249 if (expanded_exprs == error_mark_node)
16250 continue;
625b6d91
DG
16251
16252 /* Prepend each of the expanded expressions to the
16253 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
16254 for (i = 0; i < len; i++)
16255 {
16256 TREE_VEC_ELT (expanded_arguments, i) =
16257 tree_cons (NULL_TREE,
16258 TREE_VEC_ELT (expanded_exprs, i),
16259 TREE_VEC_ELT (expanded_arguments, i));
16260 }
16261 }
16262 in_base_initializer = 0;
5d80a306 16263
625b6d91
DG
16264 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
16265 since we built them backwards. */
16266 for (i = 0; i < len; i++)
16267 {
16268 TREE_VEC_ELT (expanded_arguments, i) =
16269 nreverse (TREE_VEC_ELT (expanded_arguments, i));
16270 }
16271 }
5d80a306 16272 }
1f5a253a 16273
5d80a306
DG
16274 for (i = 0; i < len; ++i)
16275 {
16276 if (expanded_bases)
16277 {
16278 decl = TREE_VEC_ELT (expanded_bases, i);
16279 decl = expand_member_init (decl);
16280 init = TREE_VEC_ELT (expanded_arguments, i);
16281 }
16282 else
16283 {
16284 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
16285 tf_warning_or_error, NULL_TREE);
16286
16287 decl = expand_member_init (decl);
16288 if (decl && !DECL_P (decl))
16289 in_base_initializer = 1;
16290
16291 init = tsubst_expr (TREE_VALUE (t), argvec,
16292 tf_warning_or_error, NULL_TREE,
16293 /*integral_constant_expression_p=*/false);
16294 in_base_initializer = 0;
16295 }
16296
16297 if (decl)
16298 {
16299 init = build_tree_list (decl, init);
16300 TREE_CHAIN (init) = inits;
16301 inits = init;
16302 }
16303 }
5566b478 16304 }
2282d28d 16305 return inits;
5566b478
MS
16306}
16307
61a127b3
MM
16308/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
16309
16310static void
3a978d72 16311set_current_access_from_decl (tree decl)
61a127b3
MM
16312{
16313 if (TREE_PRIVATE (decl))
16314 current_access_specifier = access_private_node;
16315 else if (TREE_PROTECTED (decl))
16316 current_access_specifier = access_protected_node;
16317 else
16318 current_access_specifier = access_public_node;
16319}
16320
dbfe2124
MM
16321/* Instantiate an enumerated type. TAG is the template type, NEWTAG
16322 is the instantiation (which should have been created with
16323 start_enum) and ARGS are the template arguments to use. */
b87692e5 16324
dbfe2124 16325static void
3a978d72 16326tsubst_enum (tree tag, tree newtag, tree args)
b87692e5 16327{
dbfe2124 16328 tree e;
b87692e5
MS
16329
16330 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
16331 {
61a127b3 16332 tree value;
7b6d72fc
MM
16333 tree decl;
16334
16335 decl = TREE_VALUE (e);
61a127b3
MM
16336 /* Note that in a template enum, the TREE_VALUE is the
16337 CONST_DECL, not the corresponding INTEGER_CST. */
c8094d83 16338 value = tsubst_expr (DECL_INITIAL (decl),
015c2c66
MM
16339 args, tf_warning_or_error, NULL_TREE,
16340 /*integral_constant_expression_p=*/true);
61a127b3
MM
16341
16342 /* Give this enumeration constant the correct access. */
7b6d72fc 16343 set_current_access_from_decl (decl);
61a127b3
MM
16344
16345 /* Actually build the enumerator itself. */
c8094d83 16346 build_enumerator (DECL_NAME (decl), value, newtag);
dbfe2124 16347 }
b3d5a58b 16348
219670f1 16349 finish_enum (newtag);
f31686a3
RH
16350 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
16351 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
b87692e5 16352}
36a117a5 16353
1f6e1acc
AS
16354/* DECL is a FUNCTION_DECL that is a template specialization. Return
16355 its type -- but without substituting the innermost set of template
16356 arguments. So, innermost set of template parameters will appear in
5c74d5b0 16357 the type. */
1f6e1acc 16358
c8094d83 16359tree
3a978d72 16360get_mostly_instantiated_function_type (tree decl)
1f6e1acc 16361{
1f6e1acc
AS
16362 tree fn_type;
16363 tree tmpl;
16364 tree targs;
16365 tree tparms;
16366 int parm_depth;
16367
16368 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
16369 targs = DECL_TI_ARGS (decl);
16370 tparms = DECL_TEMPLATE_PARMS (tmpl);
16371 parm_depth = TMPL_PARMS_DEPTH (tparms);
16372
16373 /* There should be as many levels of arguments as there are levels
16374 of parameters. */
50bc768d 16375 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
1f6e1acc
AS
16376
16377 fn_type = TREE_TYPE (tmpl);
1f6e1acc
AS
16378
16379 if (parm_depth == 1)
16380 /* No substitution is necessary. */
16381 ;
16382 else
16383 {
fae6e246 16384 int i, save_access_control;
1f6e1acc
AS
16385 tree partial_args;
16386
16387 /* Replace the innermost level of the TARGS with NULL_TREEs to
dc957d14 16388 let tsubst know not to substitute for those parameters. */
1f6e1acc
AS
16389 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
16390 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
16391 SET_TMPL_ARGS_LEVEL (partial_args, i,
16392 TMPL_ARGS_LEVEL (targs, i));
16393 SET_TMPL_ARGS_LEVEL (partial_args,
16394 TMPL_ARGS_DEPTH (targs),
16395 make_tree_vec (DECL_NTPARMS (tmpl)));
16396
fae6e246
RH
16397 /* Disable access control as this function is used only during
16398 name-mangling. */
16399 save_access_control = flag_access_control;
16400 flag_access_control = 0;
5c74d5b0 16401
9579624e 16402 ++processing_template_decl;
1f6e1acc
AS
16403 /* Now, do the (partial) substitution to figure out the
16404 appropriate function type. */
c2ea3a40 16405 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
9579624e 16406 --processing_template_decl;
1f6e1acc
AS
16407
16408 /* Substitute into the template parameters to obtain the real
16409 innermost set of parameters. This step is important if the
16410 innermost set of template parameters contains value
16411 parameters whose types depend on outer template parameters. */
16412 TREE_VEC_LENGTH (partial_args)--;
c2ea3a40 16413 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
1f6e1acc 16414
fae6e246 16415 flag_access_control = save_access_control;
5c74d5b0 16416 }
1f6e1acc
AS
16417
16418 return fn_type;
16419}
669ec2b4 16420
cb753e49 16421/* Return truthvalue if we're processing a template different from
dc957d14 16422 the last one involved in diagnostics. */
cb753e49 16423int
3a978d72 16424problematic_instantiation_changed (void)
cb753e49
GDR
16425{
16426 return last_template_error_tick != tinst_level_tick;
16427}
16428
16429/* Remember current template involved in diagnostics. */
16430void
3a978d72 16431record_last_problematic_instantiation (void)
cb753e49
GDR
16432{
16433 last_template_error_tick = tinst_level_tick;
16434}
16435
e2c3721c 16436struct tinst_level *
3a978d72 16437current_instantiation (void)
cb753e49
GDR
16438{
16439 return current_tinst_level;
16440}
db3f4e4e
NS
16441
16442/* [temp.param] Check that template non-type parm TYPE is of an allowable
838dfd8a 16443 type. Return zero for ok, nonzero for disallowed. Issue error and
c2ea3a40 16444 warning messages under control of COMPLAIN. */
db3f4e4e
NS
16445
16446static int
3a978d72 16447invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
db3f4e4e 16448{
550a799d 16449 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
db3f4e4e
NS
16450 return 0;
16451 else if (POINTER_TYPE_P (type))
16452 return 0;
a5ac359a 16453 else if (TYPE_PTR_TO_MEMBER_P (type))
db3f4e4e 16454 return 0;
db3f4e4e
NS
16455 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
16456 return 0;
16457 else if (TREE_CODE (type) == TYPENAME_TYPE)
16458 return 0;
c8094d83 16459
c2ea3a40 16460 if (complain & tf_error)
0f51ccfc 16461 error ("%q#T is not a valid type for a template constant parameter", type);
db3f4e4e
NS
16462 return 1;
16463}
e2500fed 16464
5552b43c
MM
16465/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
16466 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
1fb3244a 16467
5552b43c
MM
16468static bool
16469dependent_type_p_r (tree type)
1fb3244a
MM
16470{
16471 tree scope;
16472
1fb3244a
MM
16473 /* [temp.dep.type]
16474
16475 A type is dependent if it is:
16476
6615c446
JO
16477 -- a template parameter. Template template parameters are types
16478 for us (since TYPE_P holds true for them) so we handle
16479 them here. */
c8094d83 16480 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
6bf92cb6 16481 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
1fb3244a
MM
16482 return true;
16483 /* -- a qualified-id with a nested-name-specifier which contains a
0cbd7506 16484 class-name that names a dependent type or whose unqualified-id
1fb3244a
MM
16485 names a dependent type. */
16486 if (TREE_CODE (type) == TYPENAME_TYPE)
16487 return true;
16488 /* -- a cv-qualified type where the cv-unqualified type is
0cbd7506 16489 dependent. */
1fb3244a
MM
16490 type = TYPE_MAIN_VARIANT (type);
16491 /* -- a compound type constructed from any dependent type. */
a5ac359a 16492 if (TYPE_PTR_TO_MEMBER_P (type))
1fb3244a 16493 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
c8094d83 16494 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1fb3244a
MM
16495 (type)));
16496 else if (TREE_CODE (type) == POINTER_TYPE
16497 || TREE_CODE (type) == REFERENCE_TYPE)
16498 return dependent_type_p (TREE_TYPE (type));
16499 else if (TREE_CODE (type) == FUNCTION_TYPE
16500 || TREE_CODE (type) == METHOD_TYPE)
16501 {
16502 tree arg_type;
16503
16504 if (dependent_type_p (TREE_TYPE (type)))
16505 return true;
c8094d83
MS
16506 for (arg_type = TYPE_ARG_TYPES (type);
16507 arg_type;
1fb3244a
MM
16508 arg_type = TREE_CHAIN (arg_type))
16509 if (dependent_type_p (TREE_VALUE (arg_type)))
16510 return true;
16511 return false;
16512 }
16513 /* -- an array type constructed from any dependent type or whose
0cbd7506 16514 size is specified by a constant expression that is
1fb3244a
MM
16515 value-dependent. */
16516 if (TREE_CODE (type) == ARRAY_TYPE)
16517 {
16518 if (TYPE_DOMAIN (type)
82390eb6 16519 && dependent_type_p (TYPE_DOMAIN (type)))
1fb3244a
MM
16520 return true;
16521 return dependent_type_p (TREE_TYPE (type));
16522 }
82390eb6
JM
16523 else if (TREE_CODE (type) == INTEGER_TYPE
16524 && !TREE_CONSTANT (TYPE_MAX_VALUE (type)))
16525 {
16526 /* If this is the TYPE_DOMAIN of an array type, consider it
c1165535
JM
16527 dependent. We already checked for value-dependence in
16528 compute_array_index_type. */
16529 return type_dependent_expression_p (TYPE_MAX_VALUE (type));
82390eb6 16530 }
c8094d83 16531
1fb3244a 16532 /* -- a template-id in which either the template name is a template
86306a6b
NS
16533 parameter ... */
16534 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1fb3244a 16535 return true;
86306a6b 16536 /* ... or any of the template arguments is a dependent type or
04c06002 16537 an expression that is type-dependent or value-dependent. */
86306a6b 16538 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
c8094d83 16539 && (any_dependent_template_arguments_p
7e99327d 16540 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
1fb3244a 16541 return true;
c8094d83 16542
3ad6a8e1
DG
16543 /* All TYPEOF_TYPEs and DECLTYPE_TYPEs are dependent; if the
16544 argument of the `typeof' expression is not type-dependent, then
16545 it should already been have resolved. */
16546 if (TREE_CODE (type) == TYPEOF_TYPE
16547 || TREE_CODE (type) == DECLTYPE_TYPE)
1fb3244a 16548 return true;
c8094d83 16549
5d80a306
DG
16550 /* A template argument pack is dependent if any of its packed
16551 arguments are. */
16552 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
16553 {
16554 tree args = ARGUMENT_PACK_ARGS (type);
16555 int i, len = TREE_VEC_LENGTH (args);
16556 for (i = 0; i < len; ++i)
16557 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
16558 return true;
16559 }
16560
16561 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
16562 be template parameters. */
16563 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
16564 return true;
16565
1fb3244a
MM
16566 /* The standard does not specifically mention types that are local
16567 to template functions or local classes, but they should be
16568 considered dependent too. For example:
16569
c8094d83 16570 template <int I> void f() {
0cbd7506 16571 enum E { a = I };
1fb3244a
MM
16572 S<sizeof (E)> s;
16573 }
16574
16575 The size of `E' cannot be known until the value of `I' has been
16576 determined. Therefore, `E' must be considered dependent. */
16577 scope = TYPE_CONTEXT (type);
16578 if (scope && TYPE_P (scope))
16579 return dependent_type_p (scope);
16580 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
16581 return type_dependent_expression_p (scope);
16582
16583 /* Other types are non-dependent. */
16584 return false;
16585}
16586
5552b43c
MM
16587/* Returns TRUE if TYPE is dependent, in the sense of
16588 [temp.dep.type]. */
16589
16590bool
16591dependent_type_p (tree type)
16592{
16593 /* If there are no template parameters in scope, then there can't be
16594 any dependent types. */
16595 if (!processing_template_decl)
e7e93965
MM
16596 {
16597 /* If we are not processing a template, then nobody should be
16598 providing us with a dependent type. */
16599 gcc_assert (type);
0257eee5 16600 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
e7e93965
MM
16601 return false;
16602 }
5552b43c
MM
16603
16604 /* If the type is NULL, we have not computed a type for the entity
16605 in question; in that case, the type is dependent. */
16606 if (!type)
16607 return true;
16608
16609 /* Erroneous types can be considered non-dependent. */
16610 if (type == error_mark_node)
16611 return false;
16612
16613 /* If we have not already computed the appropriate value for TYPE,
16614 do so now. */
16615 if (!TYPE_DEPENDENT_P_VALID (type))
16616 {
16617 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
16618 TYPE_DEPENDENT_P_VALID (type) = 1;
16619 }
16620
16621 return TYPE_DEPENDENT_P (type);
16622}
16623
1cb801bc
JM
16624/* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
16625 lookup. In other words, a dependent type that is not the current
16626 instantiation. */
16627
16628bool
16629dependent_scope_p (tree scope)
16630{
b77ba909
JM
16631 return (scope && TYPE_P (scope) && dependent_type_p (scope)
16632 && !currently_open_class (scope));
1cb801bc
JM
16633}
16634
8d83f792
MM
16635/* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
16636
16637static bool
16638dependent_scope_ref_p (tree expression, bool criterion (tree))
16639{
16640 tree scope;
16641 tree name;
16642
50bc768d 16643 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
8d83f792
MM
16644
16645 if (!TYPE_P (TREE_OPERAND (expression, 0)))
16646 return true;
16647
16648 scope = TREE_OPERAND (expression, 0);
16649 name = TREE_OPERAND (expression, 1);
16650
16651 /* [temp.dep.expr]
16652
16653 An id-expression is type-dependent if it contains a
16654 nested-name-specifier that contains a class-name that names a
16655 dependent type. */
1cb801bc 16656 /* The suggested resolution to Core Issue 224 implies that if the
8d83f792
MM
16657 qualifying type is the current class, then we must peek
16658 inside it. */
c8094d83 16659 if (DECL_P (name)
8d83f792
MM
16660 && currently_open_class (scope)
16661 && !criterion (name))
16662 return false;
16663 if (dependent_type_p (scope))
16664 return true;
16665
16666 return false;
16667}
16668
20929c7f 16669/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
fe89d797
MM
16670 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
16671 expression. */
1fb3244a 16672
14d22dd6 16673bool
1fb3244a
MM
16674value_dependent_expression_p (tree expression)
16675{
16676 if (!processing_template_decl)
16677 return false;
16678
16679 /* A name declared with a dependent type. */
7416ab02 16680 if (DECL_P (expression) && type_dependent_expression_p (expression))
1fb3244a 16681 return true;
c8094d83 16682
7416ab02
NS
16683 switch (TREE_CODE (expression))
16684 {
16685 case IDENTIFIER_NODE:
16686 /* A name that has not been looked up -- must be dependent. */
16687 return true;
16688
16689 case TEMPLATE_PARM_INDEX:
16690 /* A non-type template parm. */
16691 return true;
16692
16693 case CONST_DECL:
16694 /* A non-type template parm. */
16695 if (DECL_TEMPLATE_PARM_P (expression))
d17811fd 16696 return true;
3387721b 16697 return value_dependent_expression_p (DECL_INITIAL (expression));
d36d5600 16698
7416ab02 16699 case VAR_DECL:
c8094d83 16700 /* A constant with integral or enumeration type and is initialized
0cbd7506 16701 with an expression that is value-dependent. */
7416ab02
NS
16702 if (DECL_INITIAL (expression)
16703 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
16704 && value_dependent_expression_p (DECL_INITIAL (expression)))
16705 return true;
16706 return false;
16707
16708 case DYNAMIC_CAST_EXPR:
16709 case STATIC_CAST_EXPR:
16710 case CONST_CAST_EXPR:
16711 case REINTERPRET_CAST_EXPR:
16712 case CAST_EXPR:
16713 /* These expressions are value-dependent if the type to which
0cbd7506
MS
16714 the cast occurs is dependent or the expression being casted
16715 is value-dependent. */
7416ab02
NS
16716 {
16717 tree type = TREE_TYPE (expression);
c8094d83 16718
7416ab02
NS
16719 if (dependent_type_p (type))
16720 return true;
c8094d83 16721
7416ab02
NS
16722 /* A functional cast has a list of operands. */
16723 expression = TREE_OPERAND (expression, 0);
16724 if (!expression)
16725 {
16726 /* If there are no operands, it must be an expression such
16727 as "int()". This should not happen for aggregate types
16728 because it would form non-constant expressions. */
16729 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
c8094d83 16730
7416ab02
NS
16731 return false;
16732 }
c8094d83 16733
7416ab02 16734 if (TREE_CODE (expression) == TREE_LIST)
32a11c08 16735 return any_value_dependent_elements_p (expression);
c8094d83 16736
d17811fd 16737 return value_dependent_expression_p (expression);
7416ab02 16738 }
c8094d83 16739
7416ab02
NS
16740 case SIZEOF_EXPR:
16741 case ALIGNOF_EXPR:
16742 /* A `sizeof' expression is value-dependent if the operand is
5d80a306 16743 type-dependent or is a pack expansion. */
d17811fd 16744 expression = TREE_OPERAND (expression, 0);
5d80a306
DG
16745 if (PACK_EXPANSION_P (expression))
16746 return true;
16747 else if (TYPE_P (expression))
d17811fd
MM
16748 return dependent_type_p (expression);
16749 return type_dependent_expression_p (expression);
100d337a 16750
7416ab02
NS
16751 case SCOPE_REF:
16752 return dependent_scope_ref_p (expression, value_dependent_expression_p);
100d337a 16753
7416ab02
NS
16754 case COMPONENT_REF:
16755 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
16756 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
16757
16758 case CALL_EXPR:
fe89d797
MM
16759 /* A CALL_EXPR may appear in a constant expression if it is a
16760 call to a builtin function, e.g., __builtin_constant_p. All
16761 such calls are value-dependent. */
16762 return true;
7416ab02 16763
5d80a306
DG
16764 case NONTYPE_ARGUMENT_PACK:
16765 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
16766 is value-dependent. */
16767 {
16768 tree values = ARGUMENT_PACK_ARGS (expression);
16769 int i, len = TREE_VEC_LENGTH (values);
16770
16771 for (i = 0; i < len; ++i)
16772 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
16773 return true;
16774
16775 return false;
16776 }
16777
cb68ec50
PC
16778 case TRAIT_EXPR:
16779 {
16780 tree type2 = TRAIT_EXPR_TYPE2 (expression);
16781 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
16782 || (type2 ? dependent_type_p (type2) : false));
16783 }
16784
f01ec37d
SM
16785 case MODOP_EXPR:
16786 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
16787 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
16788
7416ab02
NS
16789 default:
16790 /* A constant expression is value-dependent if any subexpression is
0cbd7506 16791 value-dependent. */
1fb3244a
MM
16792 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
16793 {
7416ab02 16794 case tcc_reference:
6615c446 16795 case tcc_unary:
c8094d83 16796 return (value_dependent_expression_p
1fb3244a 16797 (TREE_OPERAND (expression, 0)));
c8094d83 16798
6615c446
JO
16799 case tcc_comparison:
16800 case tcc_binary:
c8094d83 16801 return ((value_dependent_expression_p
1fb3244a 16802 (TREE_OPERAND (expression, 0)))
c8094d83 16803 || (value_dependent_expression_p
1fb3244a 16804 (TREE_OPERAND (expression, 1))));
c8094d83 16805
6615c446 16806 case tcc_expression:
5039610b 16807 case tcc_vl_exp:
1fb3244a
MM
16808 {
16809 int i;
5039610b 16810 for (i = 0; i < TREE_OPERAND_LENGTH (expression); ++i)
14d22dd6
MM
16811 /* In some cases, some of the operands may be missing.
16812 (For example, in the case of PREDECREMENT_EXPR, the
16813 amount to increment by may be missing.) That doesn't
16814 make the expression dependent. */
16815 if (TREE_OPERAND (expression, i)
16816 && (value_dependent_expression_p
16817 (TREE_OPERAND (expression, i))))
1fb3244a
MM
16818 return true;
16819 return false;
16820 }
c8094d83 16821
6615c446 16822 default:
7416ab02 16823 break;
1fb3244a
MM
16824 }
16825 }
c8094d83 16826
1fb3244a
MM
16827 /* The expression is not value-dependent. */
16828 return false;
16829}
16830
16831/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
16832 [temp.dep.expr]. */
16833
16834bool
3a978d72 16835type_dependent_expression_p (tree expression)
1fb3244a
MM
16836{
16837 if (!processing_template_decl)
16838 return false;
16839
7efa3e22
NS
16840 if (expression == error_mark_node)
16841 return false;
10b1d5e7
MM
16842
16843 /* An unresolved name is always dependent. */
b01e6d2b
JM
16844 if (TREE_CODE (expression) == IDENTIFIER_NODE
16845 || TREE_CODE (expression) == USING_DECL)
10b1d5e7 16846 return true;
c8094d83 16847
1fb3244a
MM
16848 /* Some expression forms are never type-dependent. */
16849 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
16850 || TREE_CODE (expression) == SIZEOF_EXPR
16851 || TREE_CODE (expression) == ALIGNOF_EXPR
cb68ec50 16852 || TREE_CODE (expression) == TRAIT_EXPR
1fb3244a
MM
16853 || TREE_CODE (expression) == TYPEID_EXPR
16854 || TREE_CODE (expression) == DELETE_EXPR
16855 || TREE_CODE (expression) == VEC_DELETE_EXPR
16856 || TREE_CODE (expression) == THROW_EXPR)
16857 return false;
16858
16859 /* The types of these expressions depends only on the type to which
16860 the cast occurs. */
16861 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
16862 || TREE_CODE (expression) == STATIC_CAST_EXPR
16863 || TREE_CODE (expression) == CONST_CAST_EXPR
16864 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
16865 || TREE_CODE (expression) == CAST_EXPR)
16866 return dependent_type_p (TREE_TYPE (expression));
d17811fd 16867
1fb3244a
MM
16868 /* The types of these expressions depends only on the type created
16869 by the expression. */
d17811fd
MM
16870 if (TREE_CODE (expression) == NEW_EXPR
16871 || TREE_CODE (expression) == VEC_NEW_EXPR)
09d2f85f
KL
16872 {
16873 /* For NEW_EXPR tree nodes created inside a template, either
16874 the object type itself or a TREE_LIST may appear as the
16875 operand 1. */
16876 tree type = TREE_OPERAND (expression, 1);
16877 if (TREE_CODE (type) == TREE_LIST)
16878 /* This is an array type. We need to check array dimensions
16879 as well. */
16880 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
16881 || value_dependent_expression_p
16882 (TREE_OPERAND (TREE_VALUE (type), 1));
16883 else
16884 return dependent_type_p (type);
16885 }
1fb3244a 16886
5a57f1b2
JM
16887 if (TREE_CODE (expression) == SCOPE_REF
16888 && dependent_scope_ref_p (expression,
16889 type_dependent_expression_p))
16890 return true;
16891
16892 if (TREE_CODE (expression) == FUNCTION_DECL
16893 && DECL_LANG_SPECIFIC (expression)
16894 && DECL_TEMPLATE_INFO (expression)
16895 && (any_dependent_template_arguments_p
16896 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
16897 return true;
16898
16899 if (TREE_CODE (expression) == TEMPLATE_DECL
16900 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
16901 return false;
16902
c2acde1e
JM
16903 if (TREE_CODE (expression) == STMT_EXPR)
16904 expression = stmt_expr_value_expr (expression);
16905
86a09a9e
JM
16906 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
16907 {
16908 tree elt;
16909 unsigned i;
16910
16911 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
16912 {
16913 if (type_dependent_expression_p (elt))
16914 return true;
16915 }
16916 return false;
16917 }
16918
d17811fd
MM
16919 if (TREE_TYPE (expression) == unknown_type_node)
16920 {
16921 if (TREE_CODE (expression) == ADDR_EXPR)
16922 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
c353b8e3
MM
16923 if (TREE_CODE (expression) == COMPONENT_REF
16924 || TREE_CODE (expression) == OFFSET_REF)
6cb89308
NS
16925 {
16926 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
16927 return true;
16928 expression = TREE_OPERAND (expression, 1);
16929 if (TREE_CODE (expression) == IDENTIFIER_NODE)
16930 return false;
16931 }
3601f003
KL
16932 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
16933 if (TREE_CODE (expression) == SCOPE_REF)
16934 return false;
c8094d83 16935
d17811fd
MM
16936 if (TREE_CODE (expression) == BASELINK)
16937 expression = BASELINK_FUNCTIONS (expression);
c8094d83 16938
d17811fd
MM
16939 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
16940 {
ee3071ef
NS
16941 if (any_dependent_template_arguments_p
16942 (TREE_OPERAND (expression, 1)))
d17811fd
MM
16943 return true;
16944 expression = TREE_OPERAND (expression, 0);
16945 }
b207d6e2
MM
16946 gcc_assert (TREE_CODE (expression) == OVERLOAD
16947 || TREE_CODE (expression) == FUNCTION_DECL);
c8094d83 16948
315fb5db 16949 while (expression)
d17811fd 16950 {
315fb5db
NS
16951 if (type_dependent_expression_p (OVL_CURRENT (expression)))
16952 return true;
16953 expression = OVL_NEXT (expression);
d17811fd 16954 }
315fb5db 16955 return false;
d17811fd 16956 }
c8094d83 16957
3ce5fa4f 16958 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
3db45ab5 16959
1fb3244a
MM
16960 return (dependent_type_p (TREE_TYPE (expression)));
16961}
5094a795
JM
16962
16963/* Like type_dependent_expression_p, but it also works while not processing
16964 a template definition, i.e. during substitution or mangling. */
16965
16966bool
16967type_dependent_expression_p_push (tree expr)
16968{
16969 bool b;
16970 ++processing_template_decl;
16971 b = type_dependent_expression_p (expr);
16972 --processing_template_decl;
16973 return b;
16974}
1fb3244a 16975
c166b898 16976/* Returns TRUE if ARGS contains a type-dependent expression. */
1fb3244a
MM
16977
16978bool
c166b898 16979any_type_dependent_arguments_p (const VEC(tree,gc) *args)
d17811fd 16980{
c166b898
ILT
16981 unsigned int i;
16982 tree arg;
7efa3e22 16983
c166b898
ILT
16984 for (i = 0; VEC_iterate (tree, args, i, arg); ++i)
16985 {
7efa3e22 16986 if (type_dependent_expression_p (arg))
d17811fd 16987 return true;
d17811fd
MM
16988 }
16989 return false;
16990}
16991
32a11c08
MM
16992/* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
16993 expressions) contains any value-dependent expressions. */
16994
16995bool
58f9752a 16996any_value_dependent_elements_p (const_tree list)
32a11c08
MM
16997{
16998 for (; list; list = TREE_CHAIN (list))
16999 if (value_dependent_expression_p (TREE_VALUE (list)))
17000 return true;
17001
17002 return false;
17003}
17004
d17811fd
MM
17005/* Returns TRUE if the ARG (a template argument) is dependent. */
17006
5d80a306 17007bool
1fb3244a
MM
17008dependent_template_arg_p (tree arg)
17009{
17010 if (!processing_template_decl)
17011 return false;
17012
17013 if (TREE_CODE (arg) == TEMPLATE_DECL
17014 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17015 return dependent_template_p (arg);
5d80a306
DG
17016 else if (ARGUMENT_PACK_P (arg))
17017 {
17018 tree args = ARGUMENT_PACK_ARGS (arg);
17019 int i, len = TREE_VEC_LENGTH (args);
17020 for (i = 0; i < len; ++i)
17021 {
17022 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
17023 return true;
17024 }
17025
17026 return false;
17027 }
1fb3244a
MM
17028 else if (TYPE_P (arg))
17029 return dependent_type_p (arg);
17030 else
17031 return (type_dependent_expression_p (arg)
17032 || value_dependent_expression_p (arg));
17033}
17034
06d40de8
DG
17035/* Returns true if ARGS (a collection of template arguments) contains
17036 any types that require structural equality testing. */
17037
17038bool
17039any_template_arguments_need_structural_equality_p (tree args)
17040{
17041 int i;
17042 int j;
17043
17044 if (!args)
17045 return false;
17046 if (args == error_mark_node)
17047 return true;
17048
17049 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17050 {
17051 tree level = TMPL_ARGS_LEVEL (args, i + 1);
17052 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17053 {
17054 tree arg = TREE_VEC_ELT (level, j);
5d80a306
DG
17055 tree packed_args = NULL_TREE;
17056 int k, len = 1;
17057
17058 if (ARGUMENT_PACK_P (arg))
17059 {
17060 /* Look inside the argument pack. */
17061 packed_args = ARGUMENT_PACK_ARGS (arg);
17062 len = TREE_VEC_LENGTH (packed_args);
17063 }
17064
17065 for (k = 0; k < len; ++k)
17066 {
17067 if (packed_args)
17068 arg = TREE_VEC_ELT (packed_args, k);
17069
17070 if (error_operand_p (arg))
17071 return true;
17072 else if (TREE_CODE (arg) == TEMPLATE_DECL
17073 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
17074 continue;
17075 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
17076 return true;
17077 else if (!TYPE_P (arg) && TREE_TYPE (arg)
17078 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
17079 return true;
17080 }
06d40de8
DG
17081 }
17082 }
17083
17084 return false;
17085}
17086
d17811fd
MM
17087/* Returns true if ARGS (a collection of template arguments) contains
17088 any dependent arguments. */
1fb3244a 17089
d17811fd 17090bool
58f9752a 17091any_dependent_template_arguments_p (const_tree args)
1fb3244a 17092{
bf12d54d 17093 int i;
c353b8e3
MM
17094 int j;
17095
d17811fd
MM
17096 if (!args)
17097 return false;
7e497d0c
VR
17098 if (args == error_mark_node)
17099 return true;
d17811fd 17100
c353b8e3
MM
17101 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
17102 {
58f9752a 17103 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
c353b8e3
MM
17104 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
17105 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
17106 return true;
17107 }
1fb3244a 17108
1fb3244a
MM
17109 return false;
17110}
17111
17112/* Returns TRUE if the template TMPL is dependent. */
17113
17114bool
17115dependent_template_p (tree tmpl)
17116{
b95cc51a
MM
17117 if (TREE_CODE (tmpl) == OVERLOAD)
17118 {
17119 while (tmpl)
17120 {
17121 if (dependent_template_p (OVL_FUNCTION (tmpl)))
17122 return true;
17123 tmpl = OVL_CHAIN (tmpl);
17124 }
17125 return false;
17126 }
17127
1fb3244a
MM
17128 /* Template template parameters are dependent. */
17129 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
17130 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
17131 return true;
27ab0504 17132 /* So are names that have not been looked up. */
acccf788
MM
17133 if (TREE_CODE (tmpl) == SCOPE_REF
17134 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
d17811fd 17135 return true;
1fb3244a
MM
17136 /* So are member templates of dependent classes. */
17137 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
17138 return dependent_type_p (DECL_CONTEXT (tmpl));
17139 return false;
17140}
17141
d17811fd
MM
17142/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
17143
17144bool
17145dependent_template_id_p (tree tmpl, tree args)
17146{
17147 return (dependent_template_p (tmpl)
17148 || any_dependent_template_arguments_p (args));
17149}
17150
a68ab351
JJ
17151/* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
17152 is dependent. */
17153
17154bool
17155dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
17156{
17157 int i;
17158
17159 if (!processing_template_decl)
17160 return false;
17161
17162 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
17163 {
17164 tree decl = TREE_VEC_ELT (declv, i);
17165 tree init = TREE_VEC_ELT (initv, i);
17166 tree cond = TREE_VEC_ELT (condv, i);
17167 tree incr = TREE_VEC_ELT (incrv, i);
17168
17169 if (type_dependent_expression_p (decl))
17170 return true;
17171
17172 if (init && type_dependent_expression_p (init))
17173 return true;
17174
17175 if (type_dependent_expression_p (cond))
17176 return true;
17177
17178 if (COMPARISON_CLASS_P (cond)
17179 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
17180 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
17181 return true;
17182
17183 if (TREE_CODE (incr) == MODOP_EXPR)
17184 {
17185 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
17186 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
17187 return true;
17188 }
17189 else if (type_dependent_expression_p (incr))
17190 return true;
17191 else if (TREE_CODE (incr) == MODIFY_EXPR)
17192 {
17193 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
17194 return true;
17195 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
17196 {
17197 tree t = TREE_OPERAND (incr, 1);
17198 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
17199 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
17200 return true;
17201 }
17202 }
17203 }
17204
17205 return false;
17206}
17207
14d22dd6 17208/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
4195a767
NS
17209 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
17210 no such TYPE can be found. Note that this function peers inside
17211 uninstantiated templates and therefore should be used only in
17212 extremely limited situations. ONLY_CURRENT_P restricts this
17213 peering to the currently open classes hierarchy (which is required
17214 when comparing types). */
14d22dd6
MM
17215
17216tree
17217resolve_typename_type (tree type, bool only_current_p)
17218{
17219 tree scope;
17220 tree name;
17221 tree decl;
17222 int quals;
4514aa8c 17223 tree pushed_scope;
4195a767 17224 tree result;
14d22dd6 17225
50bc768d 17226 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
14d22dd6
MM
17227
17228 scope = TYPE_CONTEXT (type);
d0940d56
DS
17229 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
17230 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
17231 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
17232 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
17233 identifier of the TYPENAME_TYPE anymore.
17234 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
17235 TYPENAME_TYPE instead, we avoid messing up with a possible
17236 typedef variant case. */
17237 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
14d22dd6
MM
17238
17239 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
17240 it first before we can figure out what NAME refers to. */
17241 if (TREE_CODE (scope) == TYPENAME_TYPE)
17242 scope = resolve_typename_type (scope, only_current_p);
17243 /* If we don't know what SCOPE refers to, then we cannot resolve the
17244 TYPENAME_TYPE. */
4195a767
NS
17245 if (TREE_CODE (scope) == TYPENAME_TYPE)
17246 return type;
14d22dd6
MM
17247 /* If the SCOPE is a template type parameter, we have no way of
17248 resolving the name. */
17249 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
17250 return type;
17251 /* If the SCOPE is not the current instantiation, there's no reason
17252 to look inside it. */
17253 if (only_current_p && !currently_open_class (scope))
4195a767 17254 return type;
7a20d689
JM
17255 /* If SCOPE isn't the template itself, it will not have a valid
17256 TYPE_FIELDS list. */
17257 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
17258 /* scope is either the template itself or a compatible instantiation
17259 like X<T>, so look up the name in the original template. */
17260 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
17261 else
17262 /* scope is a partial instantiation, so we can't do the lookup or we
17263 will lose the template arguments. */
17264 return type;
14d22dd6
MM
17265 /* Enter the SCOPE so that name lookup will be resolved as if we
17266 were in the class definition. In particular, SCOPE will no
17267 longer be considered a dependent type. */
4514aa8c 17268 pushed_scope = push_scope (scope);
14d22dd6 17269 /* Look up the declaration. */
86ac0575 17270 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
4195a767
NS
17271
17272 result = NULL_TREE;
17273
14d22dd6
MM
17274 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
17275 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
17276 if (!decl)
4195a767 17277 /*nop*/;
14d22dd6
MM
17278 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
17279 && TREE_CODE (decl) == TYPE_DECL)
4195a767
NS
17280 {
17281 result = TREE_TYPE (decl);
17282 if (result == error_mark_node)
17283 result = NULL_TREE;
17284 }
14d22dd6
MM
17285 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
17286 && DECL_CLASS_TEMPLATE_P (decl))
17287 {
17288 tree tmpl;
17289 tree args;
17290 /* Obtain the template and the arguments. */
17291 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
17292 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
17293 /* Instantiate the template. */
4195a767
NS
17294 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
17295 /*entering_scope=*/0,
17296 tf_error | tf_user);
17297 if (result == error_mark_node)
17298 result = NULL_TREE;
14d22dd6 17299 }
4195a767 17300
14d22dd6 17301 /* Leave the SCOPE. */
4514aa8c
NS
17302 if (pushed_scope)
17303 pop_scope (pushed_scope);
14d22dd6 17304
4195a767
NS
17305 /* If we failed to resolve it, return the original typename. */
17306 if (!result)
17307 return type;
17308
17309 /* If lookup found a typename type, resolve that too. */
17310 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
17311 {
17312 /* Ill-formed programs can cause infinite recursion here, so we
17313 must catch that. */
17314 TYPENAME_IS_RESOLVING_P (type) = 1;
17315 result = resolve_typename_type (result, only_current_p);
17316 TYPENAME_IS_RESOLVING_P (type) = 0;
17317 }
17318
17319 /* Qualify the resulting type. */
17320 quals = cp_type_quals (type);
17321 if (quals)
17322 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
17323
17324 return result;
14d22dd6
MM
17325}
17326
d17811fd
MM
17327/* EXPR is an expression which is not type-dependent. Return a proxy
17328 for EXPR that can be used to compute the types of larger
17329 expressions containing EXPR. */
17330
17331tree
17332build_non_dependent_expr (tree expr)
17333{
0deb916c
MM
17334 tree inner_expr;
17335
c8094d83 17336 /* Preserve null pointer constants so that the type of things like
d17811fd
MM
17337 "p == 0" where "p" is a pointer can be determined. */
17338 if (null_ptr_cst_p (expr))
17339 return expr;
17340 /* Preserve OVERLOADs; the functions must be available to resolve
17341 types. */
c497db75 17342 inner_expr = expr;
c2acde1e
JM
17343 if (TREE_CODE (inner_expr) == STMT_EXPR)
17344 inner_expr = stmt_expr_value_expr (inner_expr);
c497db75
AO
17345 if (TREE_CODE (inner_expr) == ADDR_EXPR)
17346 inner_expr = TREE_OPERAND (inner_expr, 0);
17347 if (TREE_CODE (inner_expr) == COMPONENT_REF)
17348 inner_expr = TREE_OPERAND (inner_expr, 1);
2226e997 17349 if (is_overloaded_fn (inner_expr)
6439fffd 17350 || TREE_CODE (inner_expr) == OFFSET_REF)
d17811fd 17351 return expr;
5ae9ba3e
MM
17352 /* There is no need to return a proxy for a variable. */
17353 if (TREE_CODE (expr) == VAR_DECL)
17354 return expr;
7433e6d4
MM
17355 /* Preserve string constants; conversions from string constants to
17356 "char *" are allowed, even though normally a "const char *"
17357 cannot be used to initialize a "char *". */
17358 if (TREE_CODE (expr) == STRING_CST)
17359 return expr;
b7c707d1
MM
17360 /* Preserve arithmetic constants, as an optimization -- there is no
17361 reason to create a new node. */
17362 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
17363 return expr;
9b7be7b5
MM
17364 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
17365 There is at least one place where we want to know that a
17366 particular expression is a throw-expression: when checking a ?:
17367 expression, there are special rules if the second or third
878cbb73 17368 argument is a throw-expression. */
9b7be7b5
MM
17369 if (TREE_CODE (expr) == THROW_EXPR)
17370 return expr;
47d4c811
NS
17371
17372 if (TREE_CODE (expr) == COND_EXPR)
f293ce4b
RS
17373 return build3 (COND_EXPR,
17374 TREE_TYPE (expr),
17375 TREE_OPERAND (expr, 0),
c8094d83 17376 (TREE_OPERAND (expr, 1)
f293ce4b
RS
17377 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
17378 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
17379 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
8e1daa34
NS
17380 if (TREE_CODE (expr) == COMPOUND_EXPR
17381 && !COMPOUND_EXPR_OVERLOADED (expr))
f293ce4b
RS
17382 return build2 (COMPOUND_EXPR,
17383 TREE_TYPE (expr),
17384 TREE_OPERAND (expr, 0),
17385 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
c8094d83 17386
fdeff563
NS
17387 /* If the type is unknown, it can't really be non-dependent */
17388 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
3db45ab5 17389
c8094d83 17390 /* Otherwise, build a NON_DEPENDENT_EXPR.
d17811fd
MM
17391
17392 REFERENCE_TYPEs are not stripped for expressions in templates
17393 because doing so would play havoc with mangling. Consider, for
17394 example:
17395
c8094d83 17396 template <typename T> void f<T& g>() { g(); }
d17811fd
MM
17397
17398 In the body of "f", the expression for "g" will have
17399 REFERENCE_TYPE, even though the standard says that it should
17400 not. The reason is that we must preserve the syntactic form of
17401 the expression so that mangling (say) "f<g>" inside the body of
17402 "f" works out correctly. Therefore, the REFERENCE_TYPE is
17403 stripped here. */
018a5803 17404 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
d17811fd
MM
17405}
17406
c166b898
ILT
17407/* ARGS is a vector of expressions as arguments to a function call.
17408 Replace the arguments with equivalent non-dependent expressions.
17409 This modifies ARGS in place. */
d17811fd 17410
c166b898
ILT
17411void
17412make_args_non_dependent (VEC(tree,gc) *args)
d17811fd 17413{
c166b898
ILT
17414 unsigned int ix;
17415 tree arg;
d17811fd 17416
c166b898
ILT
17417 for (ix = 0; VEC_iterate (tree, args, ix, arg); ++ix)
17418 {
17419 tree newarg = build_non_dependent_expr (arg);
17420 if (newarg != arg)
17421 VEC_replace (tree, args, ix, newarg);
17422 }
d17811fd
MM
17423}
17424
86a09a9e
JM
17425/* Returns a type which represents 'auto'. We use a TEMPLATE_TYPE_PARM
17426 with a level one deeper than the actual template parms. */
17427
17428tree
17429make_auto (void)
17430{
17431 tree au;
17432
17433 /* ??? Is it worth caching this for multiple autos at the same level? */
17434 au = cxx_make_type (TEMPLATE_TYPE_PARM);
c2255bc4
AH
17435 TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
17436 TYPE_DECL, get_identifier ("auto"), au);
86a09a9e
JM
17437 TYPE_STUB_DECL (au) = TYPE_NAME (au);
17438 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
17439 (0, processing_template_decl + 1, processing_template_decl + 1,
17440 TYPE_NAME (au), NULL_TREE);
17441 TYPE_CANONICAL (au) = canonical_type_parameter (au);
17442 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
17443 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
17444
17445 return au;
17446}
17447
17448/* Replace auto in TYPE with std::initializer_list<auto>. */
17449
17450static tree
17451listify_autos (tree type, tree auto_node)
17452{
17453 tree std_init_list = namespace_binding
17454 (get_identifier ("initializer_list"), std_node);
17455 tree argvec;
17456 tree init_auto;
17457 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
17458 {
17459 error ("deducing auto from brace-enclosed initializer list requires "
17460 "#include <initializer_list>");
17461 return error_mark_node;
17462 }
17463 argvec = make_tree_vec (1);
17464 TREE_VEC_ELT (argvec, 0) = auto_node;
17465 init_auto = lookup_template_class (std_init_list, argvec, NULL_TREE,
17466 NULL_TREE, 0, tf_warning_or_error);
17467
17468 TREE_VEC_ELT (argvec, 0) = init_auto;
17469 if (processing_template_decl)
17470 argvec = add_to_template_args (current_template_args (), argvec);
17471 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17472}
17473
17474/* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
17475 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
17476
17477tree
17478do_auto_deduction (tree type, tree init, tree auto_node)
17479{
c166b898
ILT
17480 tree parms, tparms, targs;
17481 tree args[1];
86a09a9e
JM
17482 int val;
17483
17484 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
17485 with either a new invented type template parameter U or, if the
17486 initializer is a braced-init-list (8.5.4), with
17487 std::initializer_list<U>. */
17488 if (BRACE_ENCLOSED_INITIALIZER_P (init))
17489 type = listify_autos (type, auto_node);
17490
17491 parms = build_tree_list (NULL_TREE, type);
c166b898 17492 args[0] = init;
86a09a9e
JM
17493 tparms = make_tree_vec (1);
17494 targs = make_tree_vec (1);
17495 TREE_VEC_ELT (tparms, 0)
17496 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
c166b898 17497 val = type_unification_real (tparms, targs, parms, args, 1, 0,
86a09a9e
JM
17498 DEDUCE_CALL, LOOKUP_NORMAL);
17499 if (val > 0)
17500 {
17501 error ("unable to deduce %qT from %qE", type, init);
17502 return error_mark_node;
17503 }
17504
17505 if (processing_template_decl)
17506 targs = add_to_template_args (current_template_args (), targs);
17507 return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
17508}
17509
f57ca1ea
JM
17510/* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
17511 result. */
17512
17513tree
17514splice_late_return_type (tree type, tree late_return_type)
17515{
17516 tree argvec;
17517
17518 if (late_return_type == NULL_TREE)
17519 return type;
17520 argvec = make_tree_vec (1);
17521 TREE_VEC_ELT (argvec, 0) = late_return_type;
17522 if (processing_template_decl)
17523 argvec = add_to_template_args (current_template_args (), argvec);
17524 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
17525}
17526
86a09a9e
JM
17527/* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto'. */
17528
17529bool
17530is_auto (const_tree type)
17531{
17532 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
17533 && TYPE_IDENTIFIER (type) == get_identifier ("auto"))
17534 return true;
17535 else
17536 return false;
17537}
17538
17539/* Returns true iff TYPE contains a use of 'auto'. Since auto can only
17540 appear as a type-specifier for the declaration in question, we don't
17541 have to look through the whole type. */
17542
17543tree
17544type_uses_auto (tree type)
17545{
17546 enum tree_code code;
17547 if (is_auto (type))
17548 return type;
17549
17550 code = TREE_CODE (type);
17551
17552 if (code == POINTER_TYPE || code == REFERENCE_TYPE
17553 || code == OFFSET_TYPE || code == FUNCTION_TYPE
17554 || code == METHOD_TYPE || code == ARRAY_TYPE)
17555 return type_uses_auto (TREE_TYPE (type));
17556
17557 if (TYPE_PTRMEMFUNC_P (type))
17558 return type_uses_auto (TREE_TYPE (TREE_TYPE
17559 (TYPE_PTRMEMFUNC_FN_TYPE (type))));
17560
17561 return NULL_TREE;
17562}
17563
2eb25c98
DS
17564/* For a given template T, return the list of typedefs referenced
17565 in T for which access check is needed at T instantiation time.
17566 T is either a FUNCTION_DECL or a RECORD_TYPE.
17567 Those typedefs were added to T by the function
17568 append_type_to_template_for_access_check. */
17569
17570tree
17571get_types_needing_access_check (tree t)
17572{
17573 tree ti, result = NULL_TREE;
17574
17575 if (!t || t == error_mark_node)
17576 return t;
17577
17578 if (!(ti = get_template_info (t)))
17579 return NULL_TREE;
17580
9dbed529 17581 if (CLASS_TYPE_P (t)
c4efba1c 17582 || TREE_CODE (t) == FUNCTION_DECL)
2eb25c98
DS
17583 {
17584 if (!TI_TEMPLATE (ti))
17585 return NULL_TREE;
17586
17587 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
17588 }
17589
17590 return result;
17591}
17592
17593/* Append the typedef TYPE_DECL used in template T to a list of typedefs
17594 tied to T. That list of typedefs will be access checked at
17595 T instantiation time.
17596 T is either a FUNCTION_DECL or a RECORD_TYPE.
17597 TYPE_DECL is a TYPE_DECL node representing a typedef.
17598 SCOPE is the scope through which TYPE_DECL is accessed.
17599
17600 This function is a subroutine of
17601 append_type_to_template_for_access_check. */
17602
17603static void
17604append_type_to_template_for_access_check_1 (tree t,
17605 tree type_decl,
17606 tree scope)
17607{
17608 tree ti;
17609
17610 if (!t || t == error_mark_node)
17611 return;
17612
17613 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
9dbed529 17614 || CLASS_TYPE_P (t))
2eb25c98
DS
17615 && type_decl
17616 && TREE_CODE (type_decl) == TYPE_DECL
17617 && scope);
17618
17619 if (!(ti = get_template_info (t)))
17620 return;
17621
17622 gcc_assert (TI_TEMPLATE (ti));
17623
17624 TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) =
17625 tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti));
17626}
17627
d0940d56 17628/* Append TYPE_DECL to the template TEMPL.
2eb25c98 17629 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
d0940d56 17630 At TEMPL instanciation time, TYPE_DECL will be checked to see
2eb25c98
DS
17631 if it can be accessed through SCOPE.
17632
17633 e.g. consider the following code snippet:
17634
17635 class C
17636 {
17637 typedef int myint;
17638 };
17639
17640 template<class U> struct S
17641 {
17642 C::myint mi;
17643 };
17644
17645 S<char> s;
17646
17647 At S<char> instantiation time, we need to check the access of C::myint
17648 In other words, we need to check the access of the myint typedef through
17649 the C scope. For that purpose, this function will add the myint typedef
17650 and the scope C through which its being accessed to a list of typedefs
17651 tied to the template S. That list will be walked at template instantiation
17652 time and access check performed on each typedefs it contains.
17653 Note that this particular code snippet should yield an error because
17654 myint is private to C. */
d0940d56
DS
17655
17656void
17657append_type_to_template_for_access_check (tree templ,
17658 tree type_decl,
17659 tree scope)
17660{
2eb25c98 17661 tree node;
d0940d56 17662
2eb25c98 17663 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
d0940d56 17664
2eb25c98
DS
17665 /* Make sure we don't append the type to the template twice. */
17666 for (node = get_types_needing_access_check (templ);
d0940d56
DS
17667 node;
17668 node = TREE_CHAIN (node))
17669 {
17670 tree decl = TREE_PURPOSE (node);
17671 tree type_scope = TREE_VALUE (node);
17672
17673 if (decl == type_decl && type_scope == scope)
17674 return;
17675 }
17676
2eb25c98 17677 append_type_to_template_for_access_check_1 (templ, type_decl, scope);
d0940d56
DS
17678}
17679
2b110bfc
JM
17680/* Set up the hash tables for template instantiations. */
17681
17682void
17683init_template_processing (void)
17684{
17685 decl_specializations = htab_create_ggc (37,
17686 hash_specialization,
17687 eq_specializations,
17688 ggc_free);
17689 type_specializations = htab_create_ggc (37,
17690 hash_specialization,
17691 eq_specializations,
17692 ggc_free);
17693}
17694
e2500fed 17695#include "gt-cp-pt.h"