]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
locale_facets.tcc (money_get<>::__do_get(iter_type, iter_type, bool, ios_base&, ios_b...
[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,
4514aa8c 3 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
8d08fdba 4 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
fc378698 5 Rewritten by Jason Merrill (jason@cygnus.com).
8d08fdba 6
f5adbb8d 7This file is part of GCC.
8d08fdba 8
f5adbb8d 9GCC is free software; you can redistribute it and/or modify
8d08fdba
MS
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
f5adbb8d 14GCC is distributed in the hope that it will be useful,
8d08fdba
MS
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
f5adbb8d 20along with GCC; see the file COPYING. If not, write to
1788952f
KC
21the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22Boston, MA 02110-1301, USA. */
8d08fdba
MS
23
24/* Known bugs or deficiencies include:
e92cc029 25
e92cc029
MS
26 all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win". */
8d08fdba
MS
28
29#include "config.h"
8d052bc7 30#include "system.h"
4977bab6
ZW
31#include "coretypes.h"
32#include "tm.h"
8d08fdba 33#include "obstack.h"
8d08fdba 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
d78e771d
ZW
56 were not yet available, or because we were putting off doing the work.
57 The TREE_PURPOSE of each entry is either a DECL (for a function or
58 static data member), or a TYPE (for a class) indicating what we are
59 hoping to instantiate. The TREE_VALUE is not used. */
e2500fed 60static GTY(()) tree pending_templates;
13e73b29 61static GTY(()) tree last_pending_template;
73aad9b9 62
67ffc812 63int processing_template_parmlist;
386b8a85
JM
64static int template_header_count;
65
e2500fed 66static GTY(()) tree saved_trees;
53824026 67static VEC(int,heap) *inline_parm_levels;
75650646 68
e2500fed 69static GTY(()) tree current_tinst_level;
3ae18eaf 70
2b59fc25
KL
71static GTY(()) tree saved_access_scope;
72
0fe0caa6
RH
73/* Live only within one (recursive) call to tsubst_expr. We use
74 this to pass the statement expression node from the STMT_EXPR
75 to the EXPR_STMT that is its result. */
76static tree cur_stmt_expr;
77
6dfbb909
MM
78/* A map from local variable declarations in the body of the template
79 presently being instantiated to the corresponding instantiated
80 local variables. */
81static htab_t local_specializations;
82
830bfa74
MM
83#define UNIFY_ALLOW_NONE 0
84#define UNIFY_ALLOW_MORE_CV_QUAL 1
85#define UNIFY_ALLOW_LESS_CV_QUAL 2
86#define UNIFY_ALLOW_DERIVED 4
161c12b0 87#define UNIFY_ALLOW_INTEGER 8
028d1f20 88#define UNIFY_ALLOW_OUTER_LEVEL 16
62e4a758
NS
89#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
90#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
830bfa74 91
3a978d72
NN
92static void push_access_scope (tree);
93static void pop_access_scope (tree);
94static int resolve_overloaded_unification (tree, tree, tree, tree,
95 unification_kind_t, int);
96static int try_one_overload (tree, tree, tree, tree, tree,
f23fb7f5 97 unification_kind_t, int, bool);
3a978d72
NN
98static int unify (tree, tree, tree, tree, int);
99static void add_pending_template (tree);
aa9d8196
VR
100static int push_tinst_level (tree);
101static void pop_tinst_level (void);
3a978d72
NN
102static void reopen_tinst_level (tree);
103static tree classtype_mangled_name (tree);
104static char* mangle_class_name_for_template (const char *, tree, tree);
105static tree tsubst_initializer_list (tree, tree);
3a978d72 106static tree get_class_bindings (tree, tree, tree);
3db45ab5 107static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
e7e93965 108 bool, bool);
3a978d72
NN
109static void tsubst_enum (tree, tree, tree);
110static tree add_to_template_args (tree, tree);
111static tree add_outermost_template_args (tree, tree);
112static bool check_instantiated_args (tree, tree, tsubst_flags_t);
c8094d83 113static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
3a978d72 114static int type_unification_real (tree, tree, tree, tree,
30f86ec3 115 int, unification_kind_t, int);
3a978d72 116static void note_template_header (int);
b6ab6892 117static tree convert_nontype_argument_function (tree, tree);
3a978d72
NN
118static tree convert_nontype_argument (tree, tree);
119static tree convert_template_argument (tree, tree, tree,
120 tsubst_flags_t, int, tree);
0c58f841
MA
121static int for_each_template_parm (tree, tree_fn_t, void*,
122 struct pointer_set_t*);
3a978d72
NN
123static tree build_template_parm_index (int, int, int, tree, tree);
124static int inline_needs_template_parms (tree);
125static void push_inline_template_parms_recursive (tree, int);
3a978d72 126static tree retrieve_local_specialization (tree);
3a978d72 127static void register_local_specialization (tree, tree);
3a978d72 128static tree reduce_template_parm_level (tree, tree, int);
3a978d72
NN
129static int mark_template_parm (tree, void *);
130static int template_parm_this_level_p (tree, void *);
131static tree tsubst_friend_function (tree, tree);
132static tree tsubst_friend_class (tree, tree);
133static int can_complete_type_without_circularity (tree);
a34d3336 134static tree get_bindings (tree, tree, tree, bool);
3a978d72
NN
135static int template_decl_level (tree);
136static int check_cv_quals_for_unify (int, tree, tree);
a91db711
NS
137static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
138static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
3a978d72
NN
139static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
140static void regenerate_decl_from_template (tree, tree);
3a978d72 141static tree most_specialized_class (tree, tree);
3a978d72 142static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
3a978d72
NN
143static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
144static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
9b910171 145static bool check_specialization_scope (void);
3a978d72
NN
146static tree process_partial_specialization (tree);
147static void set_current_access_from_decl (tree);
148static void check_default_tmpl_args (tree, tree, int, int);
3a978d72 149static tree get_template_base (tree, tree, tree, tree);
3a978d72
NN
150static tree try_class_unification (tree, tree, tree, tree);
151static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
152 tree, tree);
3a978d72
NN
153static int template_args_equal (tree, tree);
154static void tsubst_default_arguments (tree);
155static tree for_each_template_parm_r (tree *, int *, void *);
156static tree copy_default_args_to_explicit_spec_1 (tree, tree);
157static void copy_default_args_to_explicit_spec (tree);
158static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
a723baf1 159static int eq_local_specializations (const void *, const void *);
5552b43c 160static bool dependent_type_p_r (tree);
14d22dd6 161static tree tsubst (tree, tree, tsubst_flags_t, tree);
015c2c66 162static tree tsubst_expr (tree, tree, tsubst_flags_t, tree, bool);
14d22dd6 163static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
36a117a5 164
2b59fc25
KL
165/* Make the current scope suitable for access checking when we are
166 processing T. T can be FUNCTION_DECL for instantiated function
2b907f5c
KL
167 template, or VAR_DECL for static member variable (need by
168 instantiate_decl). */
2b59fc25 169
8ce33230 170static void
2b907f5c 171push_access_scope (tree t)
2b59fc25 172{
50bc768d
NS
173 gcc_assert (TREE_CODE (t) == FUNCTION_DECL
174 || TREE_CODE (t) == VAR_DECL);
2b59fc25 175
23ff7e2d
KL
176 if (DECL_FRIEND_CONTEXT (t))
177 push_nested_class (DECL_FRIEND_CONTEXT (t));
178 else if (DECL_CLASS_SCOPE_P (t))
2b907f5c 179 push_nested_class (DECL_CONTEXT (t));
0f399e5f
KL
180 else
181 push_to_top_level ();
c8094d83 182
2b907f5c 183 if (TREE_CODE (t) == FUNCTION_DECL)
0f399e5f
KL
184 {
185 saved_access_scope = tree_cons
186 (NULL_TREE, current_function_decl, saved_access_scope);
187 current_function_decl = t;
188 }
2b59fc25
KL
189}
190
2b59fc25
KL
191/* Restore the scope set up by push_access_scope. T is the node we
192 are processing. */
193
8ce33230 194static void
3a978d72 195pop_access_scope (tree t)
2b59fc25 196{
2b907f5c 197 if (TREE_CODE (t) == FUNCTION_DECL)
2b59fc25
KL
198 {
199 current_function_decl = TREE_VALUE (saved_access_scope);
200 saved_access_scope = TREE_CHAIN (saved_access_scope);
201 }
0f399e5f 202
23ff7e2d 203 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
0f399e5f
KL
204 pop_nested_class ();
205 else
206 pop_from_top_level ();
2b59fc25
KL
207}
208
a723baf1
MM
209/* Do any processing required when DECL (a member template
210 declaration) is finished. Returns the TEMPLATE_DECL corresponding
211 to DECL, unless it is a specialization, in which case the DECL
212 itself is returned. */
e1467ff2
MM
213
214tree
3a978d72 215finish_member_template_decl (tree decl)
e1467ff2 216{
a723baf1
MM
217 if (decl == error_mark_node)
218 return error_mark_node;
219
50bc768d 220 gcc_assert (DECL_P (decl));
a723baf1
MM
221
222 if (TREE_CODE (decl) == TYPE_DECL)
93cdc044 223 {
a723baf1
MM
224 tree type;
225
226 type = TREE_TYPE (decl);
c8094d83 227 if (IS_AGGR_TYPE (type)
a723baf1
MM
228 && CLASSTYPE_TEMPLATE_INFO (type)
229 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
93cdc044 230 {
a723baf1 231 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
93cdc044
JM
232 check_member_template (tmpl);
233 return tmpl;
234 }
8d019cef 235 return NULL_TREE;
93cdc044 236 }
07c88314 237 else if (TREE_CODE (decl) == FIELD_DECL)
0f51ccfc 238 error ("data member %qD cannot be a member template", decl);
a1da6cba 239 else if (DECL_TEMPLATE_INFO (decl))
e1467ff2 240 {
a1da6cba
MM
241 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
242 {
243 check_member_template (DECL_TI_TEMPLATE (decl));
244 return DECL_TI_TEMPLATE (decl);
245 }
246 else
247 return decl;
c8094d83 248 }
a1da6cba 249 else
0f51ccfc 250 error ("invalid member template declaration %qD", decl);
e1467ff2 251
a1da6cba 252 return error_mark_node;
f84b4be9 253}
e1467ff2 254
f84b4be9 255/* Returns the template nesting level of the indicated class TYPE.
c8094d83 256
f84b4be9
JM
257 For example, in:
258 template <class T>
259 struct A
260 {
261 template <class U>
262 struct B {};
263 };
264
c8094d83 265 A<T>::B<U> has depth two, while A<T> has depth one.
39c01e4c 266 Both A<T>::B<int> and A<int>::B<U> have depth one, if
260cd73f 267 they are instantiations, not specializations.
39c01e4c
MM
268
269 This function is guaranteed to return 0 if passed NULL_TREE so
270 that, for example, `template_class_depth (current_class_type)' is
271 always safe. */
f84b4be9 272
260cd73f
VR
273int
274template_class_depth (tree type)
f84b4be9 275{
93cdc044 276 int depth;
f84b4be9 277
c8094d83 278 for (depth = 0;
ed44da02 279 type && TREE_CODE (type) != NAMESPACE_DECL;
c8094d83 280 type = (TREE_CODE (type) == FUNCTION_DECL)
4f1c5b7d 281 ? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
ed44da02
MM
282 {
283 if (TREE_CODE (type) != FUNCTION_DECL)
284 {
285 if (CLASSTYPE_TEMPLATE_INFO (type)
286 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
260cd73f 287 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
ed44da02
MM
288 ++depth;
289 }
c8094d83 290 else
ed44da02
MM
291 {
292 if (DECL_TEMPLATE_INFO (type)
293 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
260cd73f 294 && uses_template_parms (DECL_TI_ARGS (type)))
ed44da02
MM
295 ++depth;
296 }
297 }
f84b4be9
JM
298
299 return depth;
e1467ff2 300}
98c1c668 301
cae40af6
JM
302/* Returns 1 if processing DECL as part of do_pending_inlines
303 needs us to push template parms. */
304
305static int
3a978d72 306inline_needs_template_parms (tree decl)
cae40af6
JM
307{
308 if (! DECL_TEMPLATE_INFO (decl))
309 return 0;
f84b4be9 310
36a117a5 311 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
cae40af6
JM
312 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
313}
314
315/* Subroutine of maybe_begin_member_template_processing.
316 Push the template parms in PARMS, starting from LEVELS steps into the
317 chain, and ending at the beginning, since template parms are listed
318 innermost first. */
319
320static void
3a978d72 321push_inline_template_parms_recursive (tree parmlist, int levels)
cae40af6
JM
322{
323 tree parms = TREE_VALUE (parmlist);
324 int i;
325
326 if (levels > 1)
327 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
786b5245 328
98c1c668 329 ++processing_template_decl;
cae40af6 330 current_template_parms
4890c2f4 331 = tree_cons (size_int (processing_template_decl),
98c1c668 332 parms, current_template_parms);
cae40af6
JM
333 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
334
ac20c67a 335 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
0cbd7506 336 NULL);
c8094d83 337 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
98c1c668 338 {
0f67a82f 339 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
42b304f1 340
0f67a82f
LM
341 if (parm == error_mark_node)
342 continue;
42b304f1 343
50bc768d 344 gcc_assert (DECL_P (parm));
cae40af6 345
98c1c668
JM
346 switch (TREE_CODE (parm))
347 {
786b5245 348 case TYPE_DECL:
73b0fce8 349 case TEMPLATE_DECL:
98c1c668
JM
350 pushdecl (parm);
351 break;
786b5245
MM
352
353 case PARM_DECL:
354 {
fc03edb3
MM
355 /* Make a CONST_DECL as is done in process_template_parm.
356 It is ugly that we recreate this here; the original
357 version built in process_template_parm is no longer
358 available. */
786b5245
MM
359 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
360 TREE_TYPE (parm));
c727aa5e 361 DECL_ARTIFICIAL (decl) = 1;
6de9cd9a
DN
362 TREE_CONSTANT (decl) = 1;
363 TREE_INVARIANT (decl) = 1;
364 TREE_READONLY (decl) = 1;
786b5245 365 DECL_INITIAL (decl) = DECL_INITIAL (parm);
cd9f6678 366 SET_DECL_TEMPLATE_PARM_P (decl);
786b5245
MM
367 pushdecl (decl);
368 }
cae40af6 369 break;
786b5245 370
98c1c668 371 default:
315fb5db 372 gcc_unreachable ();
98c1c668
JM
373 }
374 }
375}
376
cae40af6
JM
377/* Restore the template parameter context for a member template or
378 a friend template defined in a class definition. */
379
380void
3a978d72 381maybe_begin_member_template_processing (tree decl)
cae40af6
JM
382{
383 tree parms;
3dcaad8b 384 int levels = 0;
cae40af6 385
3dcaad8b
MM
386 if (inline_needs_template_parms (decl))
387 {
388 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
389 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
cae40af6 390
3dcaad8b
MM
391 if (DECL_TEMPLATE_SPECIALIZATION (decl))
392 {
393 --levels;
394 parms = TREE_CHAIN (parms);
395 }
cae40af6 396
3dcaad8b 397 push_inline_template_parms_recursive (parms, levels);
cae40af6
JM
398 }
399
3dcaad8b
MM
400 /* Remember how many levels of template parameters we pushed so that
401 we can pop them later. */
53824026 402 VEC_safe_push (int, heap, inline_parm_levels, levels);
cae40af6
JM
403}
404
1875c2b7 405/* Undo the effects of maybe_begin_member_template_processing. */
98c1c668 406
c8094d83 407void
3a978d72 408maybe_end_member_template_processing (void)
98c1c668 409{
3dcaad8b 410 int i;
53824026 411 int last;
3dcaad8b 412
53824026 413 if (VEC_length (int, inline_parm_levels) == 0)
98c1c668
JM
414 return;
415
53824026
KH
416 last = VEC_pop (int, inline_parm_levels);
417 for (i = 0; i < last; ++i)
cae40af6
JM
418 {
419 --processing_template_decl;
420 current_template_parms = TREE_CHAIN (current_template_parms);
421 poplevel (0, 0, 0);
422 }
98c1c668
JM
423}
424
36a117a5 425/* Return a new template argument vector which contains all of ARGS,
f9a7ae04 426 but has as its innermost set of arguments the EXTRA_ARGS. */
e6f1275f
JM
427
428static tree
3a978d72 429add_to_template_args (tree args, tree extra_args)
e6f1275f 430{
36a117a5
MM
431 tree new_args;
432 int extra_depth;
433 int i;
434 int j;
e6f1275f 435
36a117a5 436 extra_depth = TMPL_ARGS_DEPTH (extra_args);
f31c0a32 437 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
e6f1275f 438
36a117a5
MM
439 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
440 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
e6f1275f 441
36a117a5
MM
442 for (j = 1; j <= extra_depth; ++j, ++i)
443 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
c8094d83 444
e6f1275f
JM
445 return new_args;
446}
447
36a117a5
MM
448/* Like add_to_template_args, but only the outermost ARGS are added to
449 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
450 (EXTRA_ARGS) levels are added. This function is used to combine
451 the template arguments from a partial instantiation with the
452 template arguments used to attain the full instantiation from the
453 partial instantiation. */
98c1c668 454
4966381a 455static tree
3a978d72 456add_outermost_template_args (tree args, tree extra_args)
98c1c668
JM
457{
458 tree new_args;
459
e4a84209
MM
460 /* If there are more levels of EXTRA_ARGS than there are ARGS,
461 something very fishy is going on. */
50bc768d 462 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
e4a84209
MM
463
464 /* If *all* the new arguments will be the EXTRA_ARGS, just return
465 them. */
466 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
467 return extra_args;
468
36a117a5
MM
469 /* For the moment, we make ARGS look like it contains fewer levels. */
470 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
c8094d83 471
36a117a5 472 new_args = add_to_template_args (args, extra_args);
98c1c668 473
36a117a5
MM
474 /* Now, we restore ARGS to its full dimensions. */
475 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
98c1c668
JM
476
477 return new_args;
478}
5566b478 479
f9a7ae04
MM
480/* Return the N levels of innermost template arguments from the ARGS. */
481
482tree
3a978d72 483get_innermost_template_args (tree args, int n)
f9a7ae04
MM
484{
485 tree new_args;
486 int extra_levels;
487 int i;
488
50bc768d 489 gcc_assert (n >= 0);
f9a7ae04
MM
490
491 /* If N is 1, just return the innermost set of template arguments. */
492 if (n == 1)
493 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
c8094d83 494
f9a7ae04
MM
495 /* If we're not removing anything, just return the arguments we were
496 given. */
497 extra_levels = TMPL_ARGS_DEPTH (args) - n;
50bc768d 498 gcc_assert (extra_levels >= 0);
f9a7ae04
MM
499 if (extra_levels == 0)
500 return args;
501
502 /* Make a new set of arguments, not containing the outer arguments. */
503 new_args = make_tree_vec (n);
504 for (i = 1; i <= n; ++i)
c8094d83 505 SET_TMPL_ARGS_LEVEL (new_args, i,
f9a7ae04
MM
506 TMPL_ARGS_LEVEL (args, i + extra_levels));
507
508 return new_args;
509}
510
5566b478
MS
511/* We've got a template header coming up; push to a new level for storing
512 the parms. */
8d08fdba 513
8d08fdba 514void
3a978d72 515begin_template_parm_list (void)
8d08fdba 516{
6757edfe
MM
517 /* We use a non-tag-transparent scope here, which causes pushtag to
518 put tags in this scope, rather than in the enclosing class or
519 namespace scope. This is the right thing, since we want
520 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
521 global template class, push_template_decl handles putting the
522 TEMPLATE_DECL into top-level scope. For a nested template class,
523 e.g.:
524
525 template <class T> struct S1 {
0cbd7506 526 template <class T> struct S2 {};
6757edfe
MM
527 };
528
529 pushtag contains special code to call pushdecl_with_scope on the
530 TEMPLATE_DECL for S2. */
ac20c67a 531 begin_scope (sk_template_parms, NULL);
5156628f 532 ++processing_template_decl;
67ffc812 533 ++processing_template_parmlist;
386b8a85
JM
534 note_template_header (0);
535}
536
6c30752f 537/* This routine is called when a specialization is declared. If it is
9b910171
LM
538 invalid to declare a specialization here, an error is reported and
539 false is returned, otherwise this routine will return true. */
6c30752f 540
9b910171 541static bool
3a978d72 542check_specialization_scope (void)
6c30752f
MM
543{
544 tree scope = current_scope ();
3ddfb0e6 545
c8094d83
MS
546 /* [temp.expl.spec]
547
6c30752f
MM
548 An explicit specialization shall be declared in the namespace of
549 which the template is a member, or, for member templates, in the
550 namespace of which the enclosing class or enclosing class
551 template is a member. An explicit specialization of a member
552 function, member class or static data member of a class template
553 shall be declared in the namespace of which the class template
554 is a member. */
555 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
9b910171
LM
556 {
557 error ("explicit specialization in non-namespace scope %qD", scope);
558 return false;
559 }
3ddfb0e6 560
c8094d83 561 /* [temp.expl.spec]
6c30752f
MM
562
563 In an explicit specialization declaration for a member of a class
564 template or a member template that appears in namespace scope,
565 the member template and some of its enclosing class templates may
566 remain unspecialized, except that the declaration shall not
567 explicitly specialize a class member template if its enclosing
568 class templates are not explicitly specialized as well. */
c8094d83 569 if (current_template_parms)
9b910171
LM
570 {
571 error ("enclosing class templates are not explicitly specialized");
572 return false;
573 }
574
575 return true;
6c30752f
MM
576}
577
c6002625 578/* We've just seen template <>. */
386b8a85 579
9b910171 580bool
3a978d72 581begin_specialization (void)
386b8a85 582{
ac20c67a 583 begin_scope (sk_template_spec, NULL);
386b8a85 584 note_template_header (1);
9b910171 585 return check_specialization_scope ();
386b8a85
JM
586}
587
dc957d14 588/* Called at then end of processing a declaration preceded by
386b8a85
JM
589 template<>. */
590
c8094d83 591void
3a978d72 592end_specialization (void)
386b8a85 593{
74b846e0 594 finish_scope ();
386b8a85
JM
595 reset_specialization ();
596}
597
386b8a85 598/* Any template <>'s that we have seen thus far are not referring to a
c6002625 599 function specialization. */
386b8a85
JM
600
601void
3a978d72 602reset_specialization (void)
386b8a85
JM
603{
604 processing_specialization = 0;
605 template_header_count = 0;
606}
607
838dfd8a 608/* We've just seen a template header. If SPECIALIZATION is nonzero,
386b8a85
JM
609 it was of the form template <>. */
610
c8094d83 611static void
3a978d72 612note_template_header (int specialization)
386b8a85
JM
613{
614 processing_specialization = specialization;
615 template_header_count++;
616}
617
75650646 618/* We're beginning an explicit instantiation. */
386b8a85 619
75650646 620void
3a978d72 621begin_explicit_instantiation (void)
386b8a85 622{
50bc768d 623 gcc_assert (!processing_explicit_instantiation);
a723baf1 624 processing_explicit_instantiation = true;
75650646 625}
386b8a85 626
386b8a85 627
75650646 628void
3a978d72 629end_explicit_instantiation (void)
75650646 630{
50bc768d 631 gcc_assert (processing_explicit_instantiation);
a723baf1 632 processing_explicit_instantiation = false;
75650646 633}
386b8a85 634
13a44ee0 635/* An explicit specialization or partial specialization TMPL is being
b1cc95ce
MM
636 declared. Check that the namespace in which the specialization is
637 occurring is permissible. Returns false iff it is invalid to
638 specialize TMPL in the current namespace. */
c8094d83 639
b1cc95ce
MM
640static bool
641check_specialization_namespace (tree tmpl)
642{
643 tree tpl_ns = decl_namespace_context (tmpl);
644
645 /* [tmpl.expl.spec]
c8094d83 646
b1cc95ce
MM
647 An explicit specialization shall be declared in the namespace of
648 which the template is a member, or, for member templates, in the
649 namespace of which the enclosing class or enclosing class
650 template is a member. An explicit specialization of a member
651 function, member class or static data member of a class template
652 shall be declared in the namespace of which the class template is
653 a member. */
654 if (is_associated_namespace (current_namespace, tpl_ns))
655 /* Same or super-using namespace. */
656 return true;
657 else
658 {
0f51ccfc 659 pedwarn ("specialization of %qD in different namespace", tmpl);
dee15844 660 pedwarn (" from definition of %q+#D", tmpl);
b1cc95ce
MM
661 return false;
662 }
663}
664
9c12301f
MM
665/* SPEC is an explicit instantiation. Check that it is valid to
666 perform this explicit instantiation in the current namespace. */
667
668static void
669check_explicit_instantiation_namespace (tree spec)
670{
671 tree ns;
672
673 /* DR 275: An explicit instantiation shall appear in an enclosing
3db45ab5 674 namespace of its template. */
9c12301f
MM
675 ns = decl_namespace_context (spec);
676 if (!is_ancestor (current_namespace, ns))
677 pedwarn ("explicit instantiation of %qD in namespace %qD "
312ea236 678 "(which does not enclose namespace %qD)",
9c12301f
MM
679 spec, current_namespace, ns);
680}
681
36a117a5
MM
682/* The TYPE is being declared. If it is a template type, that means it
683 is a partial specialization. Do appropriate error-checking. */
684
9ce387d9 685tree
3a978d72 686maybe_process_partial_specialization (tree type)
36a117a5 687{
c9043d03
VR
688 tree context;
689
690 if (type == error_mark_node)
9ce387d9
VR
691 return error_mark_node;
692
693 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
694 {
695 error ("name of class shadows template template parameter %qD",
696 TYPE_NAME (type));
697 return error_mark_node;
698 }
c9043d03
VR
699
700 context = TYPE_CONTEXT (type);
6e049fcd 701
a723baf1 702 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
36a117a5 703 {
6e049fcd
KL
704 /* This is for ordinary explicit specialization and partial
705 specialization of a template class such as:
706
707 template <> class C<int>;
708
709 or:
710
711 template <class T> class C<T*>;
712
713 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
714
36a117a5 715 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
d0f062fb 716 && !COMPLETE_TYPE_P (type))
36a117a5 717 {
b1cc95ce 718 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
370af2d5 719 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
36a117a5
MM
720 if (processing_template_decl)
721 push_template_decl (TYPE_MAIN_DECL (type));
722 }
723 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
0f51ccfc 724 error ("specialization of %qT after instantiation", type);
36a117a5 725 }
6e049fcd
KL
726 else if (CLASS_TYPE_P (type)
727 && !CLASSTYPE_USE_TEMPLATE (type)
728 && CLASSTYPE_TEMPLATE_INFO (type)
729 && context && CLASS_TYPE_P (context)
730 && CLASSTYPE_TEMPLATE_INFO (context))
731 {
732 /* This is for an explicit specialization of member class
733 template according to [temp.expl.spec/18]:
734
735 template <> template <class U> class C<int>::D;
736
737 The context `C<int>' must be an implicit instantiation.
738 Otherwise this is just a member class template declared
739 earlier like:
740
741 template <> class C<int> { template <class U> class D; };
742 template <> template <class U> class C<int>::D;
743
744 In the first case, `C<int>::D' is a specialization of `C<T>::D'
745 while in the second case, `C<int>::D' is a primary template
746 and `C<T>::D' may not exist. */
747
748 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
749 && !COMPLETE_TYPE_P (type))
750 {
751 tree t;
752
753 if (current_namespace
754 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
755 {
0f51ccfc 756 pedwarn ("specializing %q#T in different namespace", type);
dee15844
JM
757 pedwarn (" from definition of %q+#D",
758 CLASSTYPE_TI_TEMPLATE (type));
6e049fcd
KL
759 }
760
761 /* Check for invalid specialization after instantiation:
762
763 template <> template <> class C<int>::D<int>;
764 template <> template <class U> class C<int>::D; */
765
766 for (t = DECL_TEMPLATE_INSTANTIATIONS
767 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
768 t; t = TREE_CHAIN (t))
769 if (TREE_VALUE (t) != type
770 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
0f51ccfc 771 error ("specialization %qT after instantiation %qT",
6e049fcd
KL
772 type, TREE_VALUE (t));
773
774 /* Mark TYPE as a specialization. And as a result, we only
775 have one level of template argument for the innermost
776 class template. */
777 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
778 CLASSTYPE_TI_ARGS (type)
779 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
780 }
781 }
20496fa2 782 else if (processing_specialization)
9ce387d9
VR
783 {
784 error ("explicit specialization of non-template %qT", type);
785 return error_mark_node;
786 }
787
788 return type;
36a117a5
MM
789}
790
ee81147e 791/* Returns nonzero if we can optimize the retrieval of specializations
c7222c02
MM
792 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
793 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
794
c8094d83 795static inline bool
c7222c02
MM
796optimize_specialization_lookup_p (tree tmpl)
797{
798 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
799 && DECL_CLASS_SCOPE_P (tmpl)
800 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
801 parameter. */
802 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1ca939e5
MM
803 /* The optimized lookup depends on the fact that the
804 template arguments for the member function template apply
805 purely to the containing class, which is not true if the
806 containing class is an explicit or partial
807 specialization. */
808 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
c7222c02
MM
809 && !DECL_MEMBER_TEMPLATE_P (tmpl)
810 && !DECL_CONV_FN_P (tmpl)
811 /* It is possible to have a template that is not a member
812 template and is not a member of a template class:
c8094d83
MS
813
814 template <typename T>
c7222c02 815 struct S { friend A::f(); };
c8094d83 816
c7222c02
MM
817 Here, the friend function is a template, but the context does
818 not have template information. The optimized lookup relies
819 on having ARGS be the template arguments for both the class
820 and the function template. */
821 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
822}
823
75650646
MM
824/* Retrieve the specialization (in the sense of [temp.spec] - a
825 specialization is either an instantiation or an explicit
826 specialization) of TMPL for the given template ARGS. If there is
827 no such specialization, return NULL_TREE. The ARGS are a vector of
828 arguments, or a vector of vectors of arguments, in the case of
c8094d83 829 templates with more than one level of parameters.
c7222c02
MM
830
831 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
832 then we search for a partial specialization matching ARGS. This
833 parameter is ignored if TMPL is not a class template. */
c8094d83 834
75650646 835static tree
c8094d83 836retrieve_specialization (tree tmpl, tree args,
c7222c02 837 bool class_specializations_p)
75650646 838{
2d8ba2c7
LM
839 if (args == error_mark_node)
840 return NULL_TREE;
841
50bc768d 842 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
75650646 843
36a117a5
MM
844 /* There should be as many levels of arguments as there are
845 levels of parameters. */
c8094d83 846 gcc_assert (TMPL_ARGS_DEPTH (args)
50bc768d 847 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
c8094d83 848
c7222c02
MM
849 if (optimize_specialization_lookup_p (tmpl))
850 {
851 tree class_template;
852 tree class_specialization;
d4e6fecb 853 VEC(tree,gc) *methods;
c7222c02
MM
854 tree fns;
855 int idx;
856
857 /* The template arguments actually apply to the containing
858 class. Find the class specialization with those
859 arguments. */
860 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
c8094d83 861 class_specialization
c7222c02
MM
862 = retrieve_specialization (class_template, args,
863 /*class_specializations_p=*/false);
864 if (!class_specialization)
865 return NULL_TREE;
866 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
867 for the specialization. */
868 idx = class_method_index_for_fn (class_specialization, tmpl);
869 if (idx == -1)
870 return NULL_TREE;
871 /* Iterate through the methods with the indicated name, looking
872 for the one that has an instance of TMPL. */
873 methods = CLASSTYPE_METHOD_VEC (class_specialization);
874 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
875 {
876 tree fn = OVL_CURRENT (fns);
877 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
878 return fn;
879 }
880 return NULL_TREE;
881 }
882 else
883 {
884 tree *sp;
885 tree *head;
886
887 /* Class templates store their instantiations on the
888 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
889 DECL_TEMPLATE_SPECIALIZATIONS list. */
890 if (!class_specializations_p
891 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
892 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
893 else
894 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
895 head = sp;
896 /* Iterate through the list until we find a matching template. */
897 while (*sp != NULL_TREE)
898 {
899 tree spec = *sp;
c8094d83 900
c7222c02
MM
901 if (comp_template_args (TREE_PURPOSE (spec), args))
902 {
903 /* Use the move-to-front heuristic to speed up future
c8094d83 904 searches. */
c7222c02
MM
905 if (spec != *head)
906 {
907 *sp = TREE_CHAIN (*sp);
908 TREE_CHAIN (spec) = *head;
909 *head = spec;
910 }
911 return TREE_VALUE (spec);
912 }
913 sp = &TREE_CHAIN (spec);
914 }
915 }
75650646
MM
916
917 return NULL_TREE;
386b8a85
JM
918}
919
dc957d14 920/* Like retrieve_specialization, but for local declarations. */
9188c363
MM
921
922static tree
3a978d72 923retrieve_local_specialization (tree tmpl)
9188c363 924{
67f5655f 925 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
3db45ab5 926 htab_hash_pointer (tmpl));
a723baf1 927 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
9188c363
MM
928}
929
838dfd8a 930/* Returns nonzero iff DECL is a specialization of TMPL. */
6757edfe
MM
931
932int
3a978d72 933is_specialization_of (tree decl, tree tmpl)
6757edfe
MM
934{
935 tree t;
936
937 if (TREE_CODE (decl) == FUNCTION_DECL)
938 {
c8094d83 939 for (t = decl;
6757edfe
MM
940 t != NULL_TREE;
941 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
942 if (t == tmpl)
943 return 1;
944 }
c8094d83 945 else
6757edfe 946 {
50bc768d 947 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6757edfe
MM
948
949 for (t = TREE_TYPE (decl);
950 t != NULL_TREE;
951 t = CLASSTYPE_USE_TEMPLATE (t)
952 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
9edc3913 953 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
6757edfe 954 return 1;
c8094d83 955 }
386b8a85 956
6757edfe
MM
957 return 0;
958}
75650646 959
d43f603d
KL
960/* Returns nonzero iff DECL is a specialization of friend declaration
961 FRIEND according to [temp.friend]. */
962
963bool
964is_specialization_of_friend (tree decl, tree friend)
965{
966 bool need_template = true;
967 int template_depth;
968
b939a023
KL
969 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
970 || TREE_CODE (decl) == TYPE_DECL);
d43f603d
KL
971
972 /* For [temp.friend/6] when FRIEND is an ordinary member function
973 of a template class, we want to check if DECL is a specialization
974 if this. */
975 if (TREE_CODE (friend) == FUNCTION_DECL
976 && DECL_TEMPLATE_INFO (friend)
977 && !DECL_USE_TEMPLATE (friend))
978 {
b939a023 979 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
d43f603d
KL
980 friend = DECL_TI_TEMPLATE (friend);
981 need_template = false;
982 }
b939a023
KL
983 else if (TREE_CODE (friend) == TEMPLATE_DECL
984 && !PRIMARY_TEMPLATE_P (friend))
985 need_template = false;
d43f603d
KL
986
987 /* There is nothing to do if this is not a template friend. */
988 if (TREE_CODE (friend) != TEMPLATE_DECL)
b939a023 989 return false;
d43f603d
KL
990
991 if (is_specialization_of (decl, friend))
b939a023 992 return true;
d43f603d
KL
993
994 /* [temp.friend/6]
995 A member of a class template may be declared to be a friend of a
996 non-template class. In this case, the corresponding member of
997 every specialization of the class template is a friend of the
998 class granting friendship.
c8094d83 999
d43f603d
KL
1000 For example, given a template friend declaration
1001
1002 template <class T> friend void A<T>::f();
1003
1004 the member function below is considered a friend
1005
1006 template <> struct A<int> {
1007 void f();
1008 };
1009
1010 For this type of template friend, TEMPLATE_DEPTH below will be
5c425df5 1011 nonzero. To determine if DECL is a friend of FRIEND, we first
d43f603d
KL
1012 check if the enclosing class is a specialization of another. */
1013
1014 template_depth = template_class_depth (DECL_CONTEXT (friend));
1015 if (template_depth
1016 && DECL_CLASS_SCOPE_P (decl)
c8094d83 1017 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
d43f603d
KL
1018 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
1019 {
1020 /* Next, we check the members themselves. In order to handle
b939a023 1021 a few tricky cases, such as when FRIEND's are
d43f603d
KL
1022
1023 template <class T> friend void A<T>::g(T t);
1024 template <class T> template <T t> friend void A<T>::h();
1025
b939a023
KL
1026 and DECL's are
1027
1028 void A<int>::g(int);
1029 template <int> void A<int>::h();
1030
1031 we need to figure out ARGS, the template arguments from
1032 the context of DECL. This is required for template substitution
1033 of `T' in the function parameter of `g' and template parameter
1034 of `h' in the above examples. Here ARGS corresponds to `int'. */
d43f603d
KL
1035
1036 tree context = DECL_CONTEXT (decl);
1037 tree args = NULL_TREE;
1038 int current_depth = 0;
b939a023 1039
d43f603d
KL
1040 while (current_depth < template_depth)
1041 {
1042 if (CLASSTYPE_TEMPLATE_INFO (context))
1043 {
1044 if (current_depth == 0)
1045 args = TYPE_TI_ARGS (context);
1046 else
1047 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1048 current_depth++;
1049 }
1050 context = TYPE_CONTEXT (context);
1051 }
1052
1053 if (TREE_CODE (decl) == FUNCTION_DECL)
1054 {
1055 bool is_template;
1056 tree friend_type;
1057 tree decl_type;
1058 tree friend_args_type;
1059 tree decl_args_type;
1060
1061 /* Make sure that both DECL and FRIEND are templates or
1062 non-templates. */
1063 is_template = DECL_TEMPLATE_INFO (decl)
1064 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1065 if (need_template ^ is_template)
b939a023 1066 return false;
d43f603d
KL
1067 else if (is_template)
1068 {
da1d7781 1069 /* If both are templates, check template parameter list. */
d43f603d
KL
1070 tree friend_parms
1071 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1072 args, tf_none);
1073 if (!comp_template_parms
1074 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1075 friend_parms))
b939a023 1076 return false;
d43f603d
KL
1077
1078 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1079 }
1080 else
1081 decl_type = TREE_TYPE (decl);
1082
1083 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1084 tf_none, NULL_TREE);
1085 if (friend_type == error_mark_node)
b939a023 1086 return false;
d43f603d
KL
1087
1088 /* Check if return types match. */
1089 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
b939a023 1090 return false;
d43f603d
KL
1091
1092 /* Check if function parameter types match, ignoring the
1093 `this' parameter. */
1094 friend_args_type = TYPE_ARG_TYPES (friend_type);
1095 decl_args_type = TYPE_ARG_TYPES (decl_type);
1096 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1097 friend_args_type = TREE_CHAIN (friend_args_type);
1098 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1099 decl_args_type = TREE_CHAIN (decl_args_type);
b939a023
KL
1100
1101 return compparms (decl_args_type, friend_args_type);
1102 }
1103 else
1104 {
1105 /* DECL is a TYPE_DECL */
1106 bool is_template;
1107 tree decl_type = TREE_TYPE (decl);
1108
1109 /* Make sure that both DECL and FRIEND are templates or
1110 non-templates. */
1111 is_template
1112 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1113 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1114
1115 if (need_template ^ is_template)
1116 return false;
1117 else if (is_template)
1118 {
1119 tree friend_parms;
1120 /* If both are templates, check the name of the two
1121 TEMPLATE_DECL's first because is_friend didn't. */
1122 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1123 != DECL_NAME (friend))
1124 return false;
1125
1126 /* Now check template parameter list. */
1127 friend_parms
1128 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1129 args, tf_none);
1130 return comp_template_parms
1131 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1132 friend_parms);
1133 }
1134 else
1135 return (DECL_NAME (decl)
1136 == DECL_NAME (friend));
d43f603d
KL
1137 }
1138 }
b939a023 1139 return false;
d43f603d
KL
1140}
1141
75650646 1142/* Register the specialization SPEC as a specialization of TMPL with
d63d5d0c
ILT
1143 the indicated ARGS. IS_FRIEND indicates whether the specialization
1144 is actually just a friend declaration. Returns SPEC, or an
1145 equivalent prior declaration, if available. */
75650646 1146
36a117a5 1147static tree
d63d5d0c 1148register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
75650646 1149{
c7222c02 1150 tree fn;
75650646 1151
50bc768d 1152 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
75650646 1153
c8094d83 1154 if (TREE_CODE (spec) == FUNCTION_DECL
36a117a5
MM
1155 && uses_template_parms (DECL_TI_ARGS (spec)))
1156 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1157 register it; we want the corresponding TEMPLATE_DECL instead.
1158 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1159 the more obvious `uses_template_parms (spec)' to avoid problems
1160 with default function arguments. In particular, given
1161 something like this:
1162
0cbd7506 1163 template <class T> void f(T t1, T t = T())
36a117a5
MM
1164
1165 the default argument expression is not substituted for in an
1166 instantiation unless and until it is actually needed. */
1167 return spec;
28ba38e3 1168
c8094d83 1169 fn = retrieve_specialization (tmpl, args,
c7222c02
MM
1170 /*class_specializations_p=*/false);
1171 /* We can sometimes try to re-register a specialization that we've
1172 already got. In particular, regenerate_decl_from_template calls
1173 duplicate_decls which will update the specialization list. But,
1174 we'll still get called again here anyhow. It's more convenient
1175 to simply allow this than to try to prevent it. */
1176 if (fn == spec)
1177 return spec;
1178 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
22e9174f 1179 {
c7222c02 1180 if (DECL_TEMPLATE_INSTANTIATION (fn))
22e9174f 1181 {
c8094d83 1182 if (TREE_USED (fn)
c7222c02 1183 || DECL_EXPLICIT_INSTANTIATION (fn))
22e9174f 1184 {
c7222c02
MM
1185 error ("specialization of %qD after instantiation",
1186 fn);
99f4234a 1187 return error_mark_node;
22e9174f 1188 }
c7222c02 1189 else
b1cc95ce 1190 {
a39041fa 1191 tree clone;
c7222c02
MM
1192 /* This situation should occur only if the first
1193 specialization is an implicit instantiation, the
1194 second is an explicit specialization, and the
1195 implicit instantiation has not yet been used. That
1196 situation can occur if we have implicitly
1197 instantiated a member function and then specialized
1198 it later.
c8094d83 1199
c7222c02
MM
1200 We can also wind up here if a friend declaration that
1201 looked like an instantiation turns out to be a
1202 specialization:
c8094d83 1203
c7222c02
MM
1204 template <class T> void foo(T);
1205 class S { friend void foo<>(int) };
c8094d83
MS
1206 template <> void foo(int);
1207
c7222c02
MM
1208 We transform the existing DECL in place so that any
1209 pointers to it become pointers to the updated
1210 declaration.
1211
1212 If there was a definition for the template, but not
1213 for the specialization, we want this to look as if
1214 there were no definition, and vice versa. */
1215 DECL_INITIAL (fn) = NULL_TREE;
d63d5d0c 1216 duplicate_decls (spec, fn, is_friend);
a39041fa 1217 /* The call to duplicate_decls will have applied
3db45ab5 1218 [temp.expl.spec]:
a39041fa 1219
3db45ab5 1220 An explicit specialization of a function template
a39041fa 1221 is inline only if it is explicitly declared to be,
bcf51da2 1222 and independently of whether its function template
a39041fa
MM
1223 is.
1224
1225 to the primary function; now copy the inline bits to
3db45ab5 1226 the various clones. */
a39041fa
MM
1227 FOR_EACH_CLONE (clone, fn)
1228 {
1229 DECL_DECLARED_INLINE_P (clone)
1230 = DECL_DECLARED_INLINE_P (fn);
1231 DECL_INLINE (clone)
1232 = DECL_INLINE (fn);
1233 }
9c12301f 1234 check_specialization_namespace (fn);
c8094d83 1235
b1cc95ce
MM
1236 return fn;
1237 }
22e9174f 1238 }
c7222c02
MM
1239 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1240 {
d63d5d0c 1241 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
c7222c02
MM
1242 /* Dup decl failed, but this is a new definition. Set the
1243 line number so any errors match this new
1244 definition. */
1245 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
c8094d83 1246
c7222c02
MM
1247 return fn;
1248 }
b1cc95ce
MM
1249 }
1250
1251 /* A specialization must be declared in the same namespace as the
1252 template it is specializing. */
1253 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1254 && !check_specialization_namespace (tmpl))
74b0d490 1255 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
75650646 1256
c7222c02
MM
1257 if (!optimize_specialization_lookup_p (tmpl))
1258 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1259 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
36a117a5
MM
1260
1261 return spec;
1262}
1263
1264/* Unregister the specialization SPEC as a specialization of TMPL.
b3445994
MM
1265 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1266 if the SPEC was listed as a specialization of TMPL. */
36a117a5 1267
b3445994
MM
1268bool
1269reregister_specialization (tree spec, tree tmpl, tree new_spec)
36a117a5
MM
1270{
1271 tree* s;
1272
1273 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1274 *s != NULL_TREE;
1275 s = &TREE_CHAIN (*s))
1276 if (TREE_VALUE (*s) == spec)
1277 {
b3445994
MM
1278 if (!new_spec)
1279 *s = TREE_CHAIN (*s);
1280 else
a3d87771 1281 TREE_VALUE (*s) = new_spec;
36a117a5
MM
1282 return 1;
1283 }
1284
1285 return 0;
75650646
MM
1286}
1287
a723baf1
MM
1288/* Compare an entry in the local specializations hash table P1 (which
1289 is really a pointer to a TREE_LIST) with P2 (which is really a
1290 DECL). */
1291
1292static int
1293eq_local_specializations (const void *p1, const void *p2)
1294{
1295 return TREE_VALUE ((tree) p1) == (tree) p2;
1296}
1297
69f794a7
MM
1298/* Hash P1, an entry in the local specializations table. */
1299
1300static hashval_t
1301hash_local_specialization (const void* p1)
1302{
1303 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1304}
1305
6dfbb909
MM
1306/* Like register_specialization, but for local declarations. We are
1307 registering SPEC, an instantiation of TMPL. */
9188c363 1308
414ea4aa 1309static void
3a978d72 1310register_local_specialization (tree spec, tree tmpl)
9188c363 1311{
6dfbb909
MM
1312 void **slot;
1313
c8094d83 1314 slot = htab_find_slot_with_hash (local_specializations, tmpl,
69f794a7 1315 htab_hash_pointer (tmpl), INSERT);
a723baf1 1316 *slot = build_tree_list (spec, tmpl);
9188c363
MM
1317}
1318
e1467ff2
MM
1319/* Print the list of candidate FNS in an error message. */
1320
104bf76a 1321void
3a978d72 1322print_candidates (tree fns)
e1467ff2
MM
1323{
1324 tree fn;
1325
d8e178a0 1326 const char *str = "candidates are:";
e1467ff2
MM
1327
1328 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1329 {
d6479fe7
MM
1330 tree f;
1331
1332 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
dee15844 1333 error ("%s %+#D", str, OVL_CURRENT (f));
e1467ff2
MM
1334 str = " ";
1335 }
1336}
1337
75650646 1338/* Returns the template (one of the functions given by TEMPLATE_ID)
e1467ff2 1339 which can be specialized to match the indicated DECL with the
03017874
MM
1340 explicit template args given in TEMPLATE_ID. The DECL may be
1341 NULL_TREE if none is available. In that case, the functions in
1342 TEMPLATE_ID are non-members.
1343
838dfd8a 1344 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
03017874
MM
1345 specialization of a member template.
1346
5fe7b654
GB
1347 The TEMPLATE_COUNT is the number of references to qualifying
1348 template classes that appeared in the name of the function. See
1349 check_explicit_specialization for a more accurate description.
1350
29a1da1c
MM
1351 TSK indicates what kind of template declaration (if any) is being
1352 declared. TSK_TEMPLATE indicates that the declaration given by
1353 DECL, though a FUNCTION_DECL, has template parameters, and is
1354 therefore a template function.
1355
03017874
MM
1356 The template args (those explicitly specified and those deduced)
1357 are output in a newly created vector *TARGS_OUT.
1358
1359 If it is impossible to determine the result, an error message is
bf8f3f93 1360 issued. The error_mark_node is returned to indicate failure. */
75650646 1361
e9659ab0 1362static tree
c8094d83 1363determine_specialization (tree template_id,
0cbd7506
MS
1364 tree decl,
1365 tree* targs_out,
5fe7b654 1366 int need_member_template,
29a1da1c
MM
1367 int template_count,
1368 tmpl_spec_kind tsk)
386b8a85 1369{
03017874
MM
1370 tree fns;
1371 tree targs;
1372 tree explicit_targs;
1373 tree candidates = NULL_TREE;
7ca383e6
MM
1374 /* A TREE_LIST of templates of which DECL may be a specialization.
1375 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1376 corresponding TREE_PURPOSE is the set of template arguments that,
1377 when used to instantiate the template, would produce a function
1378 with the signature of DECL. */
03017874 1379 tree templates = NULL_TREE;
5fe7b654
GB
1380 int header_count;
1381 struct cp_binding_level *b;
386b8a85 1382
e1467ff2
MM
1383 *targs_out = NULL_TREE;
1384
728da672 1385 if (template_id == error_mark_node || decl == error_mark_node)
f2e48b67
BK
1386 return error_mark_node;
1387
1388 fns = TREE_OPERAND (template_id, 0);
03017874 1389 explicit_targs = TREE_OPERAND (template_id, 1);
f2e48b67 1390
27fafc8d
JM
1391 if (fns == error_mark_node)
1392 return error_mark_node;
1393
c6002625 1394 /* Check for baselinks. */
91e490ab 1395 if (BASELINK_P (fns))
da15dae6 1396 fns = BASELINK_FUNCTIONS (fns);
386b8a85 1397
91e490ab
MM
1398 if (!is_overloaded_fn (fns))
1399 {
0f51ccfc 1400 error ("%qD is not a function template", fns);
91e490ab
MM
1401 return error_mark_node;
1402 }
1403
5fe7b654
GB
1404 /* Count the number of template headers specified for this
1405 specialization. */
1406 header_count = 0;
c8094d83 1407 for (b = current_binding_level;
98f99d7f 1408 b->kind == sk_template_parms;
5fe7b654
GB
1409 b = b->level_chain)
1410 ++header_count;
1411
2c73f9f5 1412 for (; fns; fns = OVL_NEXT (fns))
386b8a85 1413 {
9dfce8fd 1414 tree fn = OVL_CURRENT (fns);
03017874
MM
1415
1416 if (TREE_CODE (fn) == TEMPLATE_DECL)
d955f6ea
KL
1417 {
1418 tree decl_arg_types;
0da99d4e 1419 tree fn_arg_types;
d955f6ea
KL
1420
1421 /* DECL might be a specialization of FN. */
1422
1423 /* Adjust the type of DECL in case FN is a static member. */
1424 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
c8094d83 1425 if (DECL_STATIC_FUNCTION_P (fn)
d955f6ea
KL
1426 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1427 decl_arg_types = TREE_CHAIN (decl_arg_types);
1428
1429 /* Check that the number of function parameters matches.
1430 For example,
1431 template <class T> void f(int i = 0);
1432 template <> void f<int>();
0e339752 1433 The specialization f<int> is invalid but is not caught
d955f6ea
KL
1434 by get_bindings below. */
1435
0da99d4e
GB
1436 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1437 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1438 continue;
1439
1440 /* For a non-static member function, we need to make sure that
1441 the const qualification is the same. This can be done by
1442 checking the 'this' in the argument list. */
1443 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
c8094d83 1444 && !same_type_p (TREE_VALUE (fn_arg_types),
0da99d4e 1445 TREE_VALUE (decl_arg_types)))
d955f6ea
KL
1446 continue;
1447
5fe7b654
GB
1448 /* In case of explicit specialization, we need to check if
1449 the number of template headers appearing in the specialization
1450 is correct. This is usually done in check_explicit_specialization,
1451 but the check done there cannot be exhaustive when specializing
1452 member functions. Consider the following code:
1453
1454 template <> void A<int>::f(int);
1455 template <> template <> void A<int>::f(int);
1456
1457 Assuming that A<int> is not itself an explicit specialization
1458 already, the first line specializes "f" which is a non-template
1459 member function, whilst the second line specializes "f" which
1460 is a template member function. So both lines are syntactically
1461 correct, and check_explicit_specialization does not reject
1462 them.
c8094d83 1463
5fe7b654
GB
1464 Here, we can do better, as we are matching the specialization
1465 against the declarations. We count the number of template
1466 headers, and we check if they match TEMPLATE_COUNT + 1
1467 (TEMPLATE_COUNT is the number of qualifying template classes,
1468 plus there must be another header for the member template
1469 itself).
c8094d83 1470
5fe7b654
GB
1471 Notice that if header_count is zero, this is not a
1472 specialization but rather a template instantiation, so there
1473 is no check we can perform here. */
1474 if (header_count && header_count != template_count + 1)
1475 continue;
1476
98f99d7f
MM
1477 /* Check that the number of template arguments at the
1478 innermost level for DECL is the same as for FN. */
1479 if (current_binding_level->kind == sk_template_parms
1480 && !current_binding_level->explicit_spec_p
1481 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
3db45ab5 1482 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
916b63c3 1483 (current_template_parms))))
98f99d7f 1484 continue;
c8094d83 1485
29a1da1c
MM
1486 /* Function templates cannot be specializations; there are
1487 no partial specializations of functions. Therefore, if
1488 the type of DECL does not match FN, there is no
1489 match. */
1490 if (tsk == tsk_template)
1491 {
1492 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1493 decl_arg_types))
1494 candidates = tree_cons (NULL_TREE, fn, candidates);
1495 continue;
1496 }
1497
d955f6ea
KL
1498 /* See whether this function might be a specialization of this
1499 template. */
a34d3336 1500 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
d955f6ea
KL
1501
1502 if (!targs)
1503 /* We cannot deduce template arguments that when used to
1504 specialize TMPL will produce DECL. */
1505 continue;
1506
1507 /* Save this template, and the arguments deduced. */
1508 templates = tree_cons (targs, fn, templates);
1509 }
03017874
MM
1510 else if (need_member_template)
1511 /* FN is an ordinary member function, and we need a
1512 specialization of a member template. */
d955f6ea 1513 ;
03017874
MM
1514 else if (TREE_CODE (fn) != FUNCTION_DECL)
1515 /* We can get IDENTIFIER_NODEs here in certain erroneous
1516 cases. */
d955f6ea 1517 ;
03017874
MM
1518 else if (!DECL_FUNCTION_MEMBER_P (fn))
1519 /* This is just an ordinary non-member function. Nothing can
1520 be a specialization of that. */
d955f6ea 1521 ;
3b82c249
KL
1522 else if (DECL_ARTIFICIAL (fn))
1523 /* Cannot specialize functions that are created implicitly. */
d955f6ea 1524 ;
75650646 1525 else
03017874
MM
1526 {
1527 tree decl_arg_types;
386b8a85 1528
03017874
MM
1529 /* This is an ordinary member function. However, since
1530 we're here, we can assume it's enclosing class is a
1531 template class. For example,
c8094d83 1532
03017874
MM
1533 template <typename T> struct S { void f(); };
1534 template <> void S<int>::f() {}
1535
1536 Here, S<int>::f is a non-template, but S<int> is a
1537 template class. If FN has the same type as DECL, we
1538 might be in business. */
f5d47abd
KL
1539
1540 if (!DECL_TEMPLATE_INFO (fn))
1541 /* Its enclosing class is an explicit specialization
1542 of a template class. This is not a candidate. */
1543 continue;
1544
03017874
MM
1545 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1546 TREE_TYPE (TREE_TYPE (fn))))
1547 /* The return types differ. */
1548 continue;
1549
1550 /* Adjust the type of DECL in case FN is a static member. */
1551 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
c8094d83 1552 if (DECL_STATIC_FUNCTION_P (fn)
03017874
MM
1553 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1554 decl_arg_types = TREE_CHAIN (decl_arg_types);
1555
c8094d83 1556 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
03017874
MM
1557 decl_arg_types))
1558 /* They match! */
1559 candidates = tree_cons (NULL_TREE, fn, candidates);
03017874 1560 }
386b8a85 1561 }
03017874 1562
bf8f3f93 1563 if (templates && TREE_CHAIN (templates))
386b8a85 1564 {
03017874 1565 /* We have:
c8094d83 1566
03017874
MM
1567 [temp.expl.spec]
1568
1569 It is possible for a specialization with a given function
1570 signature to be instantiated from more than one function
1571 template. In such cases, explicit specification of the
1572 template arguments must be used to uniquely identify the
1573 function template specialization being specialized.
1574
1575 Note that here, there's no suggestion that we're supposed to
1576 determine which of the candidate templates is most
1577 specialized. However, we, also have:
1578
1579 [temp.func.order]
1580
1581 Partial ordering of overloaded function template
1582 declarations is used in the following contexts to select
1583 the function template to which a function template
c8094d83 1584 specialization refers:
03017874 1585
0cbd7506 1586 -- when an explicit specialization refers to a function
c8094d83 1587 template.
03017874
MM
1588
1589 So, we do use the partial ordering rules, at least for now.
0e339752 1590 This extension can only serve to make invalid programs valid,
03017874
MM
1591 so it's safe. And, there is strong anecdotal evidence that
1592 the committee intended the partial ordering rules to apply;
1593 the EDG front-end has that behavior, and John Spicer claims
1594 that the committee simply forgot to delete the wording in
1595 [temp.expl.spec]. */
7ca383e6
MM
1596 tree tmpl = most_specialized_instantiation (templates);
1597 if (tmpl != error_mark_node)
1598 {
1599 templates = tmpl;
1600 TREE_CHAIN (templates) = NULL_TREE;
1601 }
e1467ff2
MM
1602 }
1603
03017874 1604 if (templates == NULL_TREE && candidates == NULL_TREE)
e1467ff2 1605 {
dee15844
JM
1606 error ("template-id %qD for %q+D does not match any template "
1607 "declaration", template_id, decl);
03017874 1608 return error_mark_node;
386b8a85 1609 }
03017874 1610 else if ((templates && TREE_CHAIN (templates))
bf8f3f93
MM
1611 || (candidates && TREE_CHAIN (candidates))
1612 || (templates && candidates))
386b8a85 1613 {
dee15844
JM
1614 error ("ambiguous template specialization %qD for %q+D",
1615 template_id, decl);
bf8f3f93
MM
1616 chainon (candidates, templates);
1617 print_candidates (candidates);
03017874 1618 return error_mark_node;
386b8a85
JM
1619 }
1620
c6002625 1621 /* We have one, and exactly one, match. */
03017874
MM
1622 if (candidates)
1623 {
29a1da1c
MM
1624 tree fn = TREE_VALUE (candidates);
1625 /* DECL is a re-declaration of a template function. */
1626 if (TREE_CODE (fn) == TEMPLATE_DECL)
1627 return fn;
03017874
MM
1628 /* It was a specialization of an ordinary member function in a
1629 template class. */
29a1da1c
MM
1630 *targs_out = copy_node (DECL_TI_ARGS (fn));
1631 return DECL_TI_TEMPLATE (fn);
03017874
MM
1632 }
1633
1634 /* It was a specialization of a template. */
17aec3eb 1635 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
03017874
MM
1636 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1637 {
1638 *targs_out = copy_node (targs);
c8094d83 1639 SET_TMPL_ARGS_LEVEL (*targs_out,
03017874
MM
1640 TMPL_ARGS_DEPTH (*targs_out),
1641 TREE_PURPOSE (templates));
1642 }
1643 else
1644 *targs_out = TREE_PURPOSE (templates);
e1467ff2 1645 return TREE_VALUE (templates);
8d08fdba 1646}
8afa707f
MM
1647
1648/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1649 but with the default argument values filled in from those in the
1650 TMPL_TYPES. */
c8094d83 1651
8afa707f 1652static tree
3a978d72
NN
1653copy_default_args_to_explicit_spec_1 (tree spec_types,
1654 tree tmpl_types)
8afa707f
MM
1655{
1656 tree new_spec_types;
1657
1658 if (!spec_types)
1659 return NULL_TREE;
1660
1661 if (spec_types == void_list_node)
1662 return void_list_node;
1663
1664 /* Substitute into the rest of the list. */
1665 new_spec_types =
1666 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1667 TREE_CHAIN (tmpl_types));
c8094d83 1668
8afa707f
MM
1669 /* Add the default argument for this parameter. */
1670 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1671 TREE_VALUE (spec_types),
1672 new_spec_types);
1673}
1674
1675/* DECL is an explicit specialization. Replicate default arguments
1676 from the template it specializes. (That way, code like:
1677
1678 template <class T> void f(T = 3);
1679 template <> void f(double);
c8094d83 1680 void g () { f (); }
8afa707f
MM
1681
1682 works, as required.) An alternative approach would be to look up
1683 the correct default arguments at the call-site, but this approach
1684 is consistent with how implicit instantiations are handled. */
1685
1686static void
3a978d72 1687copy_default_args_to_explicit_spec (tree decl)
8afa707f
MM
1688{
1689 tree tmpl;
1690 tree spec_types;
1691 tree tmpl_types;
1692 tree new_spec_types;
1693 tree old_type;
1694 tree new_type;
1695 tree t;
08c2df0f
NS
1696 tree object_type = NULL_TREE;
1697 tree in_charge = NULL_TREE;
e0fff4b3 1698 tree vtt = NULL_TREE;
8afa707f
MM
1699
1700 /* See if there's anything we need to do. */
1701 tmpl = DECL_TI_TEMPLATE (decl);
1702 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1703 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1704 if (TREE_PURPOSE (t))
1705 break;
1706 if (!t)
1707 return;
1708
1709 old_type = TREE_TYPE (decl);
1710 spec_types = TYPE_ARG_TYPES (old_type);
c8094d83 1711
8afa707f
MM
1712 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1713 {
08c2df0f 1714 /* Remove the this pointer, but remember the object's type for
0cbd7506 1715 CV quals. */
08c2df0f 1716 object_type = TREE_TYPE (TREE_VALUE (spec_types));
8afa707f
MM
1717 spec_types = TREE_CHAIN (spec_types);
1718 tmpl_types = TREE_CHAIN (tmpl_types);
c8094d83 1719
8afa707f 1720 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
0cbd7506
MS
1721 {
1722 /* DECL may contain more parameters than TMPL due to the extra
1723 in-charge parameter in constructors and destructors. */
1724 in_charge = spec_types;
08c2df0f
NS
1725 spec_types = TREE_CHAIN (spec_types);
1726 }
e0fff4b3
JM
1727 if (DECL_HAS_VTT_PARM_P (decl))
1728 {
1729 vtt = spec_types;
1730 spec_types = TREE_CHAIN (spec_types);
1731 }
8afa707f
MM
1732 }
1733
1734 /* Compute the merged default arguments. */
c8094d83 1735 new_spec_types =
8afa707f
MM
1736 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1737
08c2df0f
NS
1738 /* Compute the new FUNCTION_TYPE. */
1739 if (object_type)
8afa707f 1740 {
e0fff4b3 1741 if (vtt)
0cbd7506
MS
1742 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1743 TREE_VALUE (vtt),
1744 new_spec_types);
e0fff4b3 1745
08c2df0f 1746 if (in_charge)
0cbd7506
MS
1747 /* Put the in-charge parameter back. */
1748 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1749 TREE_VALUE (in_charge),
1750 new_spec_types);
08c2df0f 1751
43dc123f
MM
1752 new_type = build_method_type_directly (object_type,
1753 TREE_TYPE (old_type),
1754 new_spec_types);
8afa707f 1755 }
8afa707f
MM
1756 else
1757 new_type = build_function_type (TREE_TYPE (old_type),
08c2df0f 1758 new_spec_types);
e9525111
MM
1759 new_type = cp_build_type_attribute_variant (new_type,
1760 TYPE_ATTRIBUTES (old_type));
8afa707f
MM
1761 new_type = build_exception_variant (new_type,
1762 TYPE_RAISES_EXCEPTIONS (old_type));
1763 TREE_TYPE (decl) = new_type;
1764}
1765
386b8a85 1766/* Check to see if the function just declared, as indicated in
75650646
MM
1767 DECLARATOR, and in DECL, is a specialization of a function
1768 template. We may also discover that the declaration is an explicit
1769 instantiation at this point.
1770
e1467ff2 1771 Returns DECL, or an equivalent declaration that should be used
03017874
MM
1772 instead if all goes well. Issues an error message if something is
1773 amiss. Returns error_mark_node if the error is not easily
1774 recoverable.
c8094d83
MS
1775
1776 FLAGS is a bitmask consisting of the following flags:
75650646 1777
75650646
MM
1778 2: The function has a definition.
1779 4: The function is a friend.
75650646
MM
1780
1781 The TEMPLATE_COUNT is the number of references to qualifying
1782 template classes that appeared in the name of the function. For
1783 example, in
1784
1785 template <class T> struct S { void f(); };
1786 void S<int>::f();
c8094d83 1787
75650646
MM
1788 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1789 classes are not counted in the TEMPLATE_COUNT, so that in
1790
1791 template <class T> struct S {};
1792 template <> struct S<int> { void f(); }
36a117a5 1793 template <> void S<int>::f();
75650646
MM
1794
1795 the TEMPLATE_COUNT would be 0. (Note that this declaration is
0e339752 1796 invalid; there should be no template <>.)
75650646
MM
1797
1798 If the function is a specialization, it is marked as such via
1799 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
c8094d83 1800 is set up correctly, and it is added to the list of specializations
75650646 1801 for that template. */
386b8a85 1802
e1467ff2 1803tree
c8094d83 1804check_explicit_specialization (tree declarator,
0cbd7506
MS
1805 tree decl,
1806 int template_count,
1807 int flags)
386b8a85 1808{
75650646
MM
1809 int have_def = flags & 2;
1810 int is_friend = flags & 4;
1811 int specialization = 0;
e1467ff2 1812 int explicit_instantiation = 0;
fd4de5ff 1813 int member_specialization = 0;
75650646
MM
1814 tree ctype = DECL_CLASS_CONTEXT (decl);
1815 tree dname = DECL_NAME (decl);
74b846e0 1816 tmpl_spec_kind tsk;
386b8a85 1817
f65b7de3
GB
1818 if (is_friend)
1819 {
1820 if (!processing_specialization)
1821 tsk = tsk_none;
1822 else
1823 tsk = tsk_excessive_parms;
1824 }
1825 else
1826 tsk = current_tmpl_spec_kind (template_count);
75650646 1827
74b846e0
MM
1828 switch (tsk)
1829 {
1830 case tsk_none:
c8094d83 1831 if (processing_specialization)
75650646 1832 {
fd4de5ff
MM
1833 specialization = 1;
1834 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
75650646 1835 }
74b846e0 1836 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
386b8a85 1837 {
74b846e0
MM
1838 if (is_friend)
1839 /* This could be something like:
75650646 1840
74b846e0
MM
1841 template <class T> void f(T);
1842 class S { friend void f<>(int); } */
1843 specialization = 1;
1844 else
1845 {
1846 /* This case handles bogus declarations like template <>
1847 template <class T> void f<int>(); */
1848
0f51ccfc 1849 error ("template-id %qD in declaration of primary template",
0cbd7506 1850 declarator);
74b846e0
MM
1851 return decl;
1852 }
1853 }
1854 break;
1855
1856 case tsk_invalid_member_spec:
1857 /* The error has already been reported in
1858 check_specialization_scope. */
1859 return error_mark_node;
1860
1861 case tsk_invalid_expl_inst:
33bd39a2 1862 error ("template parameter list used in explicit instantiation");
74b846e0
MM
1863
1864 /* Fall through. */
1865
1866 case tsk_expl_inst:
fd4de5ff 1867 if (have_def)
33bd39a2 1868 error ("definition provided for explicit instantiation");
c8094d83 1869
fd4de5ff 1870 explicit_instantiation = 1;
74b846e0 1871 break;
fd4de5ff 1872
74b846e0 1873 case tsk_excessive_parms:
f03adc6b
GB
1874 case tsk_insufficient_parms:
1875 if (tsk == tsk_excessive_parms)
0cbd7506 1876 error ("too many template parameter lists in declaration of %qD",
f03adc6b
GB
1877 decl);
1878 else if (template_header_count)
0f51ccfc 1879 error("too few template parameter lists in declaration of %qD", decl);
f03adc6b 1880 else
0f51ccfc 1881 error("explicit specialization of %qD must be introduced by "
9e637a26 1882 "%<template <>%>", decl);
75650646 1883
74b846e0
MM
1884 /* Fall through. */
1885 case tsk_expl_spec:
1886 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1887 if (ctype)
1888 member_specialization = 1;
1889 else
1890 specialization = 1;
1891 break;
74b846e0
MM
1892
1893 case tsk_template:
1894 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
c6f2ed0d 1895 {
fd4de5ff
MM
1896 /* This case handles bogus declarations like template <>
1897 template <class T> void f<int>(); */
75650646 1898
655dc6ee 1899 if (uses_template_parms (declarator))
0f51ccfc 1900 error ("function template partial specialization %qD "
e1e93ad8 1901 "is not allowed", declarator);
655dc6ee 1902 else
0f51ccfc 1903 error ("template-id %qD in declaration of primary template",
0cbd7506 1904 declarator);
fd4de5ff 1905 return decl;
386b8a85 1906 }
74b846e0
MM
1907
1908 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1909 /* This is a specialization of a member template, without
1910 specialization the containing class. Something like:
1911
1912 template <class T> struct S {
c8094d83 1913 template <class U> void f (U);
0cbd7506 1914 };
74b846e0 1915 template <> template <class U> void S<int>::f(U) {}
c8094d83 1916
74b846e0
MM
1917 That's a specialization -- but of the entire template. */
1918 specialization = 1;
1919 break;
1920
1921 default:
315fb5db 1922 gcc_unreachable ();
75650646 1923 }
386b8a85 1924
670960ac
JM
1925 if (specialization || member_specialization)
1926 {
1927 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1928 for (; t; t = TREE_CHAIN (t))
1929 if (TREE_PURPOSE (t))
1930 {
33bd39a2 1931 pedwarn
8251199e 1932 ("default argument specified in explicit specialization");
670960ac
JM
1933 break;
1934 }
1935 }
1936
e1467ff2 1937 if (specialization || member_specialization || explicit_instantiation)
75650646
MM
1938 {
1939 tree tmpl = NULL_TREE;
1940 tree targs = NULL_TREE;
75650646
MM
1941
1942 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
386b8a85
JM
1943 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1944 {
1945 tree fns;
1946
50bc768d 1947 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2f54a1db 1948 if (ctype)
386b8a85 1949 fns = dname;
2f54a1db
GB
1950 else
1951 {
1952 /* If there is no class context, the explicit instantiation
0cbd7506 1953 must be at namespace scope. */
50bc768d 1954 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2f54a1db
GB
1955
1956 /* Find the namespace binding, using the declaration
0cbd7506 1957 context. */
ddf74938 1958 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
664a90c0 1959 false, true);
4230cec2
NS
1960 if (!fns || !is_overloaded_fn (fns))
1961 {
1962 error ("%qD is not a template function", dname);
1963 fns = error_mark_node;
1964 }
664a90c0
JM
1965 else
1966 {
1967 tree fn = OVL_CURRENT (fns);
ddf74938 1968 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
08d295c5
JM
1969 CP_DECL_CONTEXT (fn)))
1970 error ("%qD is not declared in %qD",
1971 decl, current_namespace);
664a90c0 1972 }
2f54a1db 1973 }
386b8a85 1974
2f54a1db 1975 declarator = lookup_template_function (fns, NULL_TREE);
386b8a85
JM
1976 }
1977
f2e48b67
BK
1978 if (declarator == error_mark_node)
1979 return error_mark_node;
1980
75650646
MM
1981 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1982 {
8ec2ac34 1983 if (!explicit_instantiation)
0e339752 1984 /* A specialization in class scope. This is invalid,
6c30752f
MM
1985 but the error will already have been flagged by
1986 check_specialization_scope. */
1987 return error_mark_node;
8ec2ac34 1988 else
b370501f 1989 {
0e339752 1990 /* It's not valid to write an explicit instantiation in
b370501f 1991 class scope, e.g.:
8ec2ac34 1992
0cbd7506 1993 class C { template void f(); }
8ec2ac34 1994
b370501f
KG
1995 This case is caught by the parser. However, on
1996 something like:
c8094d83 1997
b370501f 1998 template class C { void f(); };
8ec2ac34 1999
0e339752 2000 (which is invalid) we can get here. The error will be
b370501f
KG
2001 issued later. */
2002 ;
2003 }
8ec2ac34 2004
e1467ff2 2005 return decl;
75650646 2006 }
c8094d83 2007 else if (ctype != NULL_TREE
75650646
MM
2008 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
2009 IDENTIFIER_NODE))
386b8a85 2010 {
75650646
MM
2011 /* Find the list of functions in ctype that have the same
2012 name as the declared function. */
2013 tree name = TREE_OPERAND (declarator, 0);
03017874
MM
2014 tree fns = NULL_TREE;
2015 int idx;
2016
8ba658ee 2017 if (constructor_name_p (name, ctype))
386b8a85 2018 {
75650646 2019 int is_constructor = DECL_CONSTRUCTOR_P (decl);
c8094d83 2020
75650646 2021 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
9f4faeae 2022 : !CLASSTYPE_DESTRUCTORS (ctype))
75650646
MM
2023 {
2024 /* From [temp.expl.spec]:
c8094d83 2025
75650646
MM
2026 If such an explicit specialization for the member
2027 of a class template names an implicitly-declared
2028 special member function (clause _special_), the
c8094d83 2029 program is ill-formed.
e1467ff2
MM
2030
2031 Similar language is found in [temp.explicit]. */
33bd39a2 2032 error ("specialization of implicitly-declared special member function");
03017874 2033 return error_mark_node;
75650646 2034 }
386b8a85 2035
42da2fd8 2036 name = is_constructor ? ctor_identifier : dtor_identifier;
75650646 2037 }
42da2fd8 2038
421844e7 2039 if (!DECL_CONV_FN_P (decl))
03017874
MM
2040 {
2041 idx = lookup_fnfields_1 (ctype, name);
2042 if (idx >= 0)
aaaa46d2 2043 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
03017874
MM
2044 }
2045 else
2046 {
d4e6fecb 2047 VEC(tree,gc) *methods;
aaaa46d2 2048 tree ovl;
03017874
MM
2049
2050 /* For a type-conversion operator, we cannot do a
2051 name-based lookup. We might be looking for `operator
2052 int' which will be a specialization of `operator T'.
2053 So, we find *all* the conversion operators, and then
2054 select from them. */
2055 fns = NULL_TREE;
2056
2057 methods = CLASSTYPE_METHOD_VEC (ctype);
2058 if (methods)
5dd236e2 2059 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
9ba5ff0f 2060 VEC_iterate (tree, methods, idx, ovl);
aaaa46d2 2061 ++idx)
03017874 2062 {
aaaa46d2 2063 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
03017874
MM
2064 /* There are no more conversion functions. */
2065 break;
2066
2067 /* Glue all these conversion functions together
2068 with those we already have. */
2069 for (; ovl; ovl = OVL_NEXT (ovl))
2070 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2071 }
2072 }
c8094d83
MS
2073
2074 if (fns == NULL_TREE)
386b8a85 2075 {
0f51ccfc 2076 error ("no member function %qD declared in %qT", name, ctype);
03017874 2077 return error_mark_node;
386b8a85
JM
2078 }
2079 else
2080 TREE_OPERAND (declarator, 0) = fns;
2081 }
c8094d83 2082
e1467ff2
MM
2083 /* Figure out what exactly is being specialized at this point.
2084 Note that for an explicit instantiation, even one for a
38e01259 2085 member function, we cannot tell apriori whether the
e1467ff2 2086 instantiation is for a member template, or just a member
36a117a5
MM
2087 function of a template class. Even if a member template is
2088 being instantiated, the member template arguments may be
2089 elided if they can be deduced from the rest of the
2090 declaration. */
e1467ff2 2091 tmpl = determine_specialization (declarator, decl,
c8094d83 2092 &targs,
5fe7b654 2093 member_specialization,
29a1da1c
MM
2094 template_count,
2095 tsk);
c8094d83 2096
03017874
MM
2097 if (!tmpl || tmpl == error_mark_node)
2098 /* We couldn't figure out what this declaration was
2099 specializing. */
2100 return error_mark_node;
2101 else
386b8a85 2102 {
25aab5d0 2103 tree gen_tmpl = most_general_template (tmpl);
36a117a5 2104
e1467ff2
MM
2105 if (explicit_instantiation)
2106 {
03d0f4af 2107 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
c8094d83 2108 is done by do_decl_instantiation later. */
25aab5d0
MM
2109
2110 int arg_depth = TMPL_ARGS_DEPTH (targs);
2111 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2112
2113 if (arg_depth > parm_depth)
2114 {
2115 /* If TMPL is not the most general template (for
2116 example, if TMPL is a friend template that is
2117 injected into namespace scope), then there will
dc957d14 2118 be too many levels of TARGS. Remove some of them
25aab5d0
MM
2119 here. */
2120 int i;
2121 tree new_targs;
2122
f31c0a32 2123 new_targs = make_tree_vec (parm_depth);
25aab5d0
MM
2124 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2125 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2126 = TREE_VEC_ELT (targs, i);
2127 targs = new_targs;
2128 }
c8094d83 2129
3e4a3562 2130 return instantiate_template (tmpl, targs, tf_error);
e1467ff2 2131 }
74b846e0 2132
6c07f448
KL
2133 /* If we thought that the DECL was a member function, but it
2134 turns out to be specializing a static member function,
4546865e 2135 make DECL a static member function as well. */
6c07f448
KL
2136 if (DECL_STATIC_FUNCTION_P (tmpl)
2137 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
4546865e 2138 revert_static_member_fn (decl);
6c07f448 2139
f9a7ae04 2140 /* If this is a specialization of a member template of a
29a1da1c
MM
2141 template class, we want to return the TEMPLATE_DECL, not
2142 the specialization of it. */
74b846e0
MM
2143 if (tsk == tsk_template)
2144 {
2145 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
f9a7ae04 2146 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
b190f239
NS
2147 if (have_def)
2148 {
f31686a3
RH
2149 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2150 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2151 = DECL_SOURCE_LOCATION (decl);
08167d1c
AO
2152 /* We want to use the argument list specified in the
2153 definition, not in the original declaration. */
2154 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2155 = DECL_ARGUMENTS (decl);
b190f239 2156 }
74b846e0
MM
2157 return tmpl;
2158 }
2159
36a117a5 2160 /* Set up the DECL_TEMPLATE_INFO for DECL. */
e1b3e07d 2161 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
36a117a5 2162
8afa707f
MM
2163 /* Inherit default function arguments from the template
2164 DECL is specializing. */
2165 copy_default_args_to_explicit_spec (decl);
2166
c750255c
MM
2167 /* This specialization has the same protection as the
2168 template it specializes. */
2169 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2170 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
b9e75696 2171
9c12301f
MM
2172 /* If DECL is a friend declaration, declared using an
2173 unqualified name, the namespace associated with DECL may
2174 have been set incorrectly. For example, in:
3db45ab5
MS
2175
2176 template <typename T> void f(T);
2177 namespace N {
2178 struct S { friend void f<int>(int); }
2179 }
2180
2181 we will have set the DECL_CONTEXT for the friend
2182 declaration to N, rather than to the global namespace. */
9c12301f
MM
2183 if (DECL_NAMESPACE_SCOPE_P (decl))
2184 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
8d039470 2185
386b8a85 2186 if (is_friend && !have_def)
36a117a5
MM
2187 /* This is not really a declaration of a specialization.
2188 It's just the name of an instantiation. But, it's not
2189 a request for an instantiation, either. */
fbf1c34b 2190 SET_DECL_IMPLICIT_INSTANTIATION (decl);
08511114
KL
2191 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2192 /* This is indeed a specialization. In case of constructors
2193 and destructors, we need in-charge and not-in-charge
2194 versions in V3 ABI. */
2195 clone_function_decl (decl, /*update_method_vec_p=*/0);
75650646 2196
36a117a5
MM
2197 /* Register this specialization so that we can find it
2198 again. */
d63d5d0c 2199 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
386b8a85
JM
2200 }
2201 }
c8094d83 2202
e1467ff2 2203 return decl;
386b8a85 2204}
75650646 2205
75650646
MM
2206/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2207 parameters. These are represented in the same format used for
2208 DECL_TEMPLATE_PARMS. */
2209
50bc768d
NS
2210int
2211comp_template_parms (tree parms1, tree parms2)
75650646
MM
2212{
2213 tree p1;
2214 tree p2;
2215
2216 if (parms1 == parms2)
2217 return 1;
2218
c8094d83 2219 for (p1 = parms1, p2 = parms2;
75650646
MM
2220 p1 != NULL_TREE && p2 != NULL_TREE;
2221 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2222 {
2223 tree t1 = TREE_VALUE (p1);
2224 tree t2 = TREE_VALUE (p2);
2225 int i;
2226
50bc768d
NS
2227 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2228 gcc_assert (TREE_CODE (t2) == TREE_VEC);
75650646
MM
2229
2230 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2231 return 0;
2232
c8094d83 2233 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
75650646 2234 {
0f67a82f
LM
2235 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2236 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
42b304f1 2237
0f67a82f
LM
2238 /* If either of the template parameters are invalid, assume
2239 they match for the sake of error recovery. */
2240 if (parm1 == error_mark_node || parm2 == error_mark_node)
2241 return 1;
75650646
MM
2242
2243 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2244 return 0;
2245
2246 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2247 continue;
3bfdc719 2248 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
75650646
MM
2249 return 0;
2250 }
2251 }
2252
2253 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2254 /* One set of parameters has more parameters lists than the
2255 other. */
2256 return 0;
2257
2258 return 1;
2259}
2260
f3400fe2
JM
2261/* Complain if DECL shadows a template parameter.
2262
2263 [temp.local]: A template-parameter shall not be redeclared within its
2264 scope (including nested scopes). */
2265
2266void
3a978d72 2267check_template_shadow (tree decl)
f3400fe2 2268{
8f032717
MM
2269 tree olddecl;
2270
b5d9b9ab
MM
2271 /* If we're not in a template, we can't possibly shadow a template
2272 parameter. */
2273 if (!current_template_parms)
2274 return;
2275
2276 /* Figure out what we're shadowing. */
8f032717
MM
2277 if (TREE_CODE (decl) == OVERLOAD)
2278 decl = OVL_CURRENT (decl);
90ea9897 2279 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
f3400fe2 2280
b5d9b9ab
MM
2281 /* If there's no previous binding for this name, we're not shadowing
2282 anything, let alone a template parameter. */
2283 if (!olddecl)
2284 return;
2285
2286 /* If we're not shadowing a template parameter, we're done. Note
2287 that OLDDECL might be an OVERLOAD (or perhaps even an
2288 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2289 node. */
2f939d94 2290 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
b5d9b9ab
MM
2291 return;
2292
2293 /* We check for decl != olddecl to avoid bogus errors for using a
2294 name inside a class. We check TPFI to avoid duplicate errors for
2295 inline member templates. */
c8094d83 2296 if (decl == olddecl
b5d9b9ab
MM
2297 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2298 return;
2299
dee15844
JM
2300 error ("declaration of %q+#D", decl);
2301 error (" shadows template parm %q+#D", olddecl);
f3400fe2 2302}
22a7be53 2303
f3400fe2 2304/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
f84b4be9
JM
2305 ORIG_LEVEL, DECL, and TYPE. */
2306
2307static tree
c8094d83 2308build_template_parm_index (int index,
0cbd7506
MS
2309 int level,
2310 int orig_level,
2311 tree decl,
2312 tree type)
f84b4be9
JM
2313{
2314 tree t = make_node (TEMPLATE_PARM_INDEX);
2315 TEMPLATE_PARM_IDX (t) = index;
2316 TEMPLATE_PARM_LEVEL (t) = level;
2317 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2318 TEMPLATE_PARM_DECL (t) = decl;
2319 TREE_TYPE (t) = type;
3e4a3562 2320 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
6de9cd9a 2321 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
3e4a3562 2322 TREE_READONLY (t) = TREE_READONLY (decl);
f84b4be9
JM
2323
2324 return t;
2325}
2326
f84b4be9 2327/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
93cdc044 2328 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
f84b4be9
JM
2329 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2330 new one is created. */
2331
c8094d83 2332static tree
3a978d72 2333reduce_template_parm_level (tree index, tree type, int levels)
f84b4be9
JM
2334{
2335 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2336 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
93cdc044 2337 != TEMPLATE_PARM_LEVEL (index) - levels))
f84b4be9 2338 {
3e4a3562
NS
2339 tree orig_decl = TEMPLATE_PARM_DECL (index);
2340 tree decl, t;
c8094d83 2341
3e4a3562
NS
2342 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2343 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
6de9cd9a 2344 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
3e4a3562
NS
2345 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2346 DECL_ARTIFICIAL (decl) = 1;
2347 SET_DECL_TEMPLATE_PARM_P (decl);
c8094d83 2348
3e4a3562 2349 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
93cdc044 2350 TEMPLATE_PARM_LEVEL (index) - levels,
f84b4be9
JM
2351 TEMPLATE_PARM_ORIG_LEVEL (index),
2352 decl, type);
2353 TEMPLATE_PARM_DESCENDANTS (index) = t;
cae40af6 2354
820cc88f
DB
2355 /* Template template parameters need this. */
2356 if (TREE_CODE (decl) != CONST_DECL)
2357 DECL_TEMPLATE_PARMS (decl)
2358 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
f84b4be9
JM
2359 }
2360
2361 return TEMPLATE_PARM_DESCENDANTS (index);
2362}
2363
a14de1af 2364/* Process information from new template parameter PARM and append it to the
058b15c1
MM
2365 LIST being built. This new parameter is a non-type parameter iff
2366 IS_NON_TYPE is true. */
e92cc029 2367
8d08fdba 2368tree
a14de1af 2369process_template_parm (tree list, tree parm, bool is_non_type)
8d08fdba 2370{
8d08fdba 2371 tree decl = 0;
a292b002 2372 tree defval;
0f67a82f 2373 tree err_parm_list;
d47e3adf 2374 int idx = 0;
f84b4be9 2375
50bc768d 2376 gcc_assert (TREE_CODE (parm) == TREE_LIST);
a292b002 2377 defval = TREE_PURPOSE (parm);
5566b478
MS
2378
2379 if (list)
2380 {
d47e3adf
LM
2381 tree p = tree_last (list);
2382
0f67a82f 2383 if (p && TREE_VALUE (p) != error_mark_node)
d47e3adf
LM
2384 {
2385 p = TREE_VALUE (p);
2386 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2387 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2388 else
2389 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2390 }
5566b478 2391
5566b478
MS
2392 ++idx;
2393 }
2394 else
2395 idx = 0;
2396
058b15c1 2397 if (is_non_type)
8d08fdba 2398 {
058b15c1
MM
2399 parm = TREE_VALUE (parm);
2400
833aa4c4 2401 SET_DECL_TEMPLATE_PARM_P (parm);
d490621d 2402
620188c9 2403 if (TREE_TYPE (parm) == error_mark_node)
0f67a82f
LM
2404 {
2405 err_parm_list = build_tree_list (defval, parm);
2406 TREE_VALUE (err_parm_list) = error_mark_node;
2407 return chainon (list, err_parm_list);
2408 }
620188c9
VR
2409 else
2410 {
2411 /* [temp.param]
d490621d 2412
620188c9
VR
2413 The top-level cv-qualifiers on the template-parameter are
2414 ignored when determining its type. */
2415 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2416 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
0f67a82f
LM
2417 {
2418 err_parm_list = build_tree_list (defval, parm);
2419 TREE_VALUE (err_parm_list) = error_mark_node;
2420 return chainon (list, err_parm_list);
2421 }
620188c9 2422 }
d490621d 2423
8d08fdba 2424 /* A template parameter is not modifiable. */
6de9cd9a
DN
2425 TREE_CONSTANT (parm) = 1;
2426 TREE_INVARIANT (parm) = 1;
2427 TREE_READONLY (parm) = 1;
8d08fdba 2428 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
6de9cd9a
DN
2429 TREE_CONSTANT (decl) = 1;
2430 TREE_INVARIANT (decl) = 1;
2431 TREE_READONLY (decl) = 1;
c8094d83 2432 DECL_INITIAL (parm) = DECL_INITIAL (decl)
f84b4be9
JM
2433 = build_template_parm_index (idx, processing_template_decl,
2434 processing_template_decl,
2435 decl, TREE_TYPE (parm));
8d08fdba
MS
2436 }
2437 else
2438 {
73b0fce8 2439 tree t;
058b15c1 2440 parm = TREE_VALUE (TREE_VALUE (parm));
c8094d83 2441
73b0fce8
KL
2442 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2443 {
33848bb0 2444 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
c8094d83 2445 /* This is for distinguishing between real templates and template
73b0fce8
KL
2446 template parameters */
2447 TREE_TYPE (parm) = t;
2448 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2449 decl = parm;
2450 }
2451 else
2452 {
33848bb0 2453 t = make_aggr_type (TEMPLATE_TYPE_PARM);
f4f206f4 2454 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
73b0fce8
KL
2455 decl = build_decl (TYPE_DECL, parm, t);
2456 }
c8094d83 2457
d2e5ee5c
MS
2458 TYPE_NAME (t) = decl;
2459 TYPE_STUB_DECL (t) = decl;
a292b002 2460 parm = decl;
f84b4be9 2461 TEMPLATE_TYPE_PARM_INDEX (t)
c8094d83 2462 = build_template_parm_index (idx, processing_template_decl,
f84b4be9
JM
2463 processing_template_decl,
2464 decl, TREE_TYPE (parm));
8d08fdba 2465 }
c727aa5e 2466 DECL_ARTIFICIAL (decl) = 1;
cd9f6678 2467 SET_DECL_TEMPLATE_PARM_P (decl);
8d08fdba 2468 pushdecl (decl);
a292b002 2469 parm = build_tree_list (defval, parm);
8d08fdba
MS
2470 return chainon (list, parm);
2471}
2472
2473/* The end of a template parameter list has been reached. Process the
2474 tree list into a parameter vector, converting each parameter into a more
2475 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2476 as PARM_DECLs. */
2477
2478tree
3a978d72 2479end_template_parm_list (tree parms)
8d08fdba 2480{
5566b478 2481 int nparms;
9471d3e2 2482 tree parm, next;
5566b478
MS
2483 tree saved_parmlist = make_tree_vec (list_length (parms));
2484
5566b478 2485 current_template_parms
4890c2f4 2486 = tree_cons (size_int (processing_template_decl),
5566b478 2487 saved_parmlist, current_template_parms);
8d08fdba 2488
9471d3e2
NS
2489 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2490 {
2491 next = TREE_CHAIN (parm);
2492 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2493 TREE_CHAIN (parm) = NULL_TREE;
2494 }
a292b002 2495
67ffc812
MM
2496 --processing_template_parmlist;
2497
8d08fdba
MS
2498 return saved_parmlist;
2499}
2500
5566b478
MS
2501/* end_template_decl is called after a template declaration is seen. */
2502
8d08fdba 2503void
3a978d72 2504end_template_decl (void)
8d08fdba 2505{
386b8a85
JM
2506 reset_specialization ();
2507
5156628f 2508 if (! processing_template_decl)
73aad9b9
JM
2509 return;
2510
5566b478 2511 /* This matches the pushlevel in begin_template_parm_list. */
74b846e0 2512 finish_scope ();
8d08fdba 2513
5566b478
MS
2514 --processing_template_decl;
2515 current_template_parms = TREE_CHAIN (current_template_parms);
5566b478 2516}
8d08fdba 2517
36a117a5
MM
2518/* Given a template argument vector containing the template PARMS.
2519 The innermost PARMS are given first. */
9a3b49ac 2520
b5791fdc 2521static tree
3a978d72 2522current_template_args (void)
5566b478 2523{
36a117a5 2524 tree header;
b370501f 2525 tree args = NULL_TREE;
36a117a5 2526 int length = TMPL_PARMS_DEPTH (current_template_parms);
98c1c668
JM
2527 int l = length;
2528
36a117a5
MM
2529 /* If there is only one level of template parameters, we do not
2530 create a TREE_VEC of TREE_VECs. Instead, we return a single
2531 TREE_VEC containing the arguments. */
2532 if (length > 1)
2533 args = make_tree_vec (length);
2534
2535 for (header = current_template_parms; header; header = TREE_CHAIN (header))
8d08fdba 2536 {
5566b478 2537 tree a = copy_node (TREE_VALUE (header));
36a117a5
MM
2538 int i;
2539
5566b478 2540 TREE_TYPE (a) = NULL_TREE;
36a117a5 2541 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
5566b478 2542 {
98c1c668
JM
2543 tree t = TREE_VEC_ELT (a, i);
2544
36a117a5 2545 /* T will be a list if we are called from within a
98c1c668
JM
2546 begin/end_template_parm_list pair, but a vector directly
2547 if within a begin/end_member_template_processing pair. */
c8094d83 2548 if (TREE_CODE (t) == TREE_LIST)
98c1c668
JM
2549 {
2550 t = TREE_VALUE (t);
c8094d83 2551
0f67a82f
LM
2552 if (t != error_mark_node)
2553 {
2554 if (TREE_CODE (t) == TYPE_DECL
2555 || TREE_CODE (t) == TEMPLATE_DECL)
2556 t = TREE_TYPE (t);
2557 else
2558 t = DECL_INITIAL (t);
2559 }
2560
36a117a5 2561 TREE_VEC_ELT (a, i) = t;
98c1c668 2562 }
5566b478 2563 }
36a117a5
MM
2564
2565 if (length > 1)
2566 TREE_VEC_ELT (args, --l) = a;
2567 else
2568 args = a;
8d08fdba
MS
2569 }
2570
9a3b49ac
MS
2571 return args;
2572}
75650646 2573
e1467ff2 2574/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
c7222c02 2575 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
ee81147e 2576 a member template. Used by push_template_decl below. */
e1467ff2 2577
75650646 2578static tree
c7222c02 2579build_template_decl (tree decl, tree parms, bool member_template_p)
75650646
MM
2580{
2581 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2582 DECL_TEMPLATE_PARMS (tmpl) = parms;
2583 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
c7222c02 2584 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
75650646
MM
2585 if (DECL_LANG_SPECIFIC (decl))
2586 {
8f032717
MM
2587 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2588 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
c8460010 2589 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
11f98788 2590 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
1f6e1acc
AS
2591 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2592 if (DECL_OVERLOADED_OPERATOR_P (decl))
c8094d83 2593 SET_OVERLOADED_OPERATOR_CODE (tmpl,
1f6e1acc 2594 DECL_OVERLOADED_OPERATOR_P (decl));
75650646
MM
2595 }
2596
2597 return tmpl;
2598}
2599
050367a3
MM
2600struct template_parm_data
2601{
6c30752f
MM
2602 /* The level of the template parameters we are currently
2603 processing. */
050367a3 2604 int level;
6c30752f
MM
2605
2606 /* The index of the specialization argument we are currently
2607 processing. */
2608 int current_arg;
2609
2610 /* An array whose size is the number of template parameters. The
838dfd8a 2611 elements are nonzero if the parameter has been used in any one
6c30752f 2612 of the arguments processed so far. */
050367a3 2613 int* parms;
6c30752f
MM
2614
2615 /* An array whose size is the number of template arguments. The
838dfd8a 2616 elements are nonzero if the argument makes use of template
6c30752f
MM
2617 parameters of this level. */
2618 int* arg_uses_template_parms;
050367a3
MM
2619};
2620
2621/* Subroutine of push_template_decl used to see if each template
2622 parameter in a partial specialization is used in the explicit
2623 argument list. If T is of the LEVEL given in DATA (which is
2624 treated as a template_parm_data*), then DATA->PARMS is marked
2625 appropriately. */
2626
2627static int
3a978d72 2628mark_template_parm (tree t, void* data)
050367a3
MM
2629{
2630 int level;
2631 int idx;
2632 struct template_parm_data* tpd = (struct template_parm_data*) data;
2633
2634 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2635 {
2636 level = TEMPLATE_PARM_LEVEL (t);
2637 idx = TEMPLATE_PARM_IDX (t);
2638 }
2639 else
2640 {
2641 level = TEMPLATE_TYPE_LEVEL (t);
2642 idx = TEMPLATE_TYPE_IDX (t);
2643 }
2644
2645 if (level == tpd->level)
6c30752f
MM
2646 {
2647 tpd->parms[idx] = 1;
2648 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2649 }
050367a3
MM
2650
2651 /* Return zero so that for_each_template_parm will continue the
2652 traversal of the tree; we want to mark *every* template parm. */
2653 return 0;
2654}
2655
6c30752f
MM
2656/* Process the partial specialization DECL. */
2657
e9659ab0 2658static tree
3a978d72 2659process_partial_specialization (tree decl)
6c30752f
MM
2660{
2661 tree type = TREE_TYPE (decl);
2662 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2663 tree specargs = CLASSTYPE_TI_ARGS (type);
f9a7ae04 2664 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
6c30752f
MM
2665 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2666 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2667 int nargs = TREE_VEC_LENGTH (inner_args);
2668 int ntparms = TREE_VEC_LENGTH (inner_parms);
2669 int i;
2670 int did_error_intro = 0;
6c30752f
MM
2671 struct template_parm_data tpd;
2672 struct template_parm_data tpd2;
2673
6c30752f
MM
2674 /* We check that each of the template parameters given in the
2675 partial specialization is used in the argument list to the
2676 specialization. For example:
2677
2678 template <class T> struct S;
2679 template <class T> struct S<T*>;
2680
2681 The second declaration is OK because `T*' uses the template
2682 parameter T, whereas
2683
2684 template <class T> struct S<int>;
2685
2686 is no good. Even trickier is:
2687
2688 template <class T>
2689 struct S1
2690 {
2691 template <class U>
2692 struct S2;
2693 template <class U>
2694 struct S2<T>;
2695 };
2696
0e339752 2697 The S2<T> declaration is actually invalid; it is a
c8094d83 2698 full-specialization. Of course,
6c30752f
MM
2699
2700 template <class U>
2701 struct S2<T (*)(U)>;
2702
2703 or some such would have been OK. */
2704 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
67f5655f 2705 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
fad205ff 2706 memset (tpd.parms, 0, sizeof (int) * ntparms);
6c30752f 2707
67f5655f 2708 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
fad205ff 2709 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
6c30752f
MM
2710 for (i = 0; i < nargs; ++i)
2711 {
2712 tpd.current_arg = i;
2713 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2714 &mark_template_parm,
4f2c9d7e
MM
2715 &tpd,
2716 NULL);
6c30752f
MM
2717 }
2718 for (i = 0; i < ntparms; ++i)
2719 if (tpd.parms[i] == 0)
2720 {
2721 /* One of the template parms was not used in the
f9a7ae04 2722 specialization. */
6c30752f
MM
2723 if (!did_error_intro)
2724 {
33bd39a2 2725 error ("template parameters not used in partial specialization:");
6c30752f
MM
2726 did_error_intro = 1;
2727 }
2728
0f51ccfc 2729 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
6c30752f
MM
2730 }
2731
2732 /* [temp.class.spec]
2733
2734 The argument list of the specialization shall not be identical to
2735 the implicit argument list of the primary template. */
c8094d83
MS
2736 if (comp_template_args
2737 (inner_args,
f9a7ae04
MM
2738 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2739 (maintmpl)))))
0f51ccfc 2740 error ("partial specialization %qT does not specialize any template arguments", type);
6c30752f
MM
2741
2742 /* [temp.class.spec]
2743
2744 A partially specialized non-type argument expression shall not
2745 involve template parameters of the partial specialization except
2746 when the argument expression is a simple identifier.
2747
2748 The type of a template parameter corresponding to a specialized
2749 non-type argument shall not be dependent on a parameter of the
2750 specialization. */
50bc768d 2751 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
6c30752f
MM
2752 tpd2.parms = 0;
2753 for (i = 0; i < nargs; ++i)
2754 {
2755 tree arg = TREE_VEC_ELT (inner_args, i);
2756 if (/* These first two lines are the `non-type' bit. */
2f939d94 2757 !TYPE_P (arg)
6c30752f
MM
2758 && TREE_CODE (arg) != TEMPLATE_DECL
2759 /* This next line is the `argument expression is not just a
2760 simple identifier' condition and also the `specialized
2761 non-type argument' bit. */
2762 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2763 {
2764 if (tpd.arg_uses_template_parms[i])
0f51ccfc 2765 error ("template argument %qE involves template parameter(s)", arg);
c8094d83 2766 else
6c30752f
MM
2767 {
2768 /* Look at the corresponding template parameter,
2769 marking which template parameters its type depends
2770 upon. */
c8094d83
MS
2771 tree type =
2772 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
6c30752f
MM
2773 i)));
2774
2775 if (!tpd2.parms)
2776 {
2777 /* We haven't yet initialized TPD2. Do so now. */
c8094d83 2778 tpd2.arg_uses_template_parms
67f5655f 2779 = (int *) alloca (sizeof (int) * nargs);
104bf76a 2780 /* The number of parameters here is the number in the
76a83782
RH
2781 main template, which, as checked in the assertion
2782 above, is NARGS. */
67f5655f 2783 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
c8094d83 2784 tpd2.level =
6c30752f
MM
2785 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2786 }
2787
104bf76a 2788 /* Mark the template parameters. But this time, we're
6c30752f
MM
2789 looking for the template parameters of the main
2790 template, not in the specialization. */
2791 tpd2.current_arg = i;
2792 tpd2.arg_uses_template_parms[i] = 0;
fad205ff 2793 memset (tpd2.parms, 0, sizeof (int) * nargs);
6c30752f
MM
2794 for_each_template_parm (type,
2795 &mark_template_parm,
4f2c9d7e
MM
2796 &tpd2,
2797 NULL);
c8094d83 2798
6c30752f
MM
2799 if (tpd2.arg_uses_template_parms [i])
2800 {
2801 /* The type depended on some template parameters.
2802 If they are fully specialized in the
2803 specialization, that's OK. */
2804 int j;
2805 for (j = 0; j < nargs; ++j)
2806 if (tpd2.parms[j] != 0
2807 && tpd.arg_uses_template_parms [j])
2808 {
0f51ccfc 2809 error ("type %qT of template argument %qE depends "
0cbd7506
MS
2810 "on template parameter(s)",
2811 type,
2812 arg);
6c30752f
MM
2813 break;
2814 }
2815 }
2816 }
2817 }
2818 }
2819
c8094d83 2820 if (retrieve_specialization (maintmpl, specargs,
c7222c02 2821 /*class_specializations_p=*/true))
6c30752f
MM
2822 /* We've already got this specialization. */
2823 return decl;
2824
d8b64f80 2825 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
916b63c3 2826 = tree_cons (specargs, inner_parms,
e1b3e07d 2827 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
6c30752f
MM
2828 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2829 return decl;
2830}
2831
6ba89f8e
MM
2832/* Check that a template declaration's use of default arguments is not
2833 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
838dfd8a
KH
2834 nonzero if DECL is the thing declared by a primary template.
2835 IS_PARTIAL is nonzero if DECL is a partial specialization. */
6ba89f8e
MM
2836
2837static void
3a978d72 2838check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
6ba89f8e 2839{
d8e178a0 2840 const char *msg;
66191c20
MM
2841 int last_level_to_check;
2842 tree parm_level;
6ba89f8e 2843
c8094d83 2844 /* [temp.param]
6ba89f8e
MM
2845
2846 A default template-argument shall not be specified in a
2847 function template declaration or a function template definition, nor
2848 in the template-parameter-list of the definition of a member of a
2849 class template. */
2850
4f1c5b7d 2851 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
e0942dcd
MM
2852 /* You can't have a function template declaration in a local
2853 scope, nor you can you define a member of a class template in a
2854 local scope. */
2855 return;
2856
6ba89f8e
MM
2857 if (current_class_type
2858 && !TYPE_BEING_DEFINED (current_class_type)
3d7de1fa 2859 && DECL_LANG_SPECIFIC (decl)
5937a6f9
MM
2860 /* If this is either a friend defined in the scope of the class
2861 or a member function. */
6df5158a
NS
2862 && (DECL_FUNCTION_MEMBER_P (decl)
2863 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2864 : DECL_FRIEND_CONTEXT (decl)
2865 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2866 : false)
5937a6f9
MM
2867 /* And, if it was a member function, it really was defined in
2868 the scope of the class. */
6df5158a
NS
2869 && (!DECL_FUNCTION_MEMBER_P (decl)
2870 || DECL_INITIALIZED_IN_CLASS_P (decl)))
6ba89f8e 2871 /* We already checked these parameters when the template was
5937a6f9
MM
2872 declared, so there's no need to do it again now. This function
2873 was defined in class scope, but we're processing it's body now
2874 that the class is complete. */
6ba89f8e
MM
2875 return;
2876
66191c20 2877 /* [temp.param]
c8094d83 2878
66191c20
MM
2879 If a template-parameter has a default template-argument, all
2880 subsequent template-parameters shall have a default
2881 template-argument supplied. */
2882 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2883 {
2884 tree inner_parms = TREE_VALUE (parm_level);
2885 int ntparms = TREE_VEC_LENGTH (inner_parms);
c8094d83 2886 int seen_def_arg_p = 0;
66191c20
MM
2887 int i;
2888
c8094d83 2889 for (i = 0; i < ntparms; ++i)
66191c20
MM
2890 {
2891 tree parm = TREE_VEC_ELT (inner_parms, i);
42b304f1
LM
2892
2893 if (parm == error_mark_node)
2894 continue;
2895
66191c20
MM
2896 if (TREE_PURPOSE (parm))
2897 seen_def_arg_p = 1;
2898 else if (seen_def_arg_p)
2899 {
0f51ccfc 2900 error ("no default argument for %qD", TREE_VALUE (parm));
66191c20
MM
2901 /* For better subsequent error-recovery, we indicate that
2902 there should have been a default argument. */
2903 TREE_PURPOSE (parm) = error_mark_node;
2904 }
2905 }
2906 }
2907
6ba89f8e
MM
2908 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2909 /* For an ordinary class template, default template arguments are
2910 allowed at the innermost level, e.g.:
0cbd7506 2911 template <class T = int>
6ba89f8e
MM
2912 struct S {};
2913 but, in a partial specialization, they're not allowed even
2914 there, as we have in [temp.class.spec]:
c8094d83 2915
6ba89f8e 2916 The template parameter list of a specialization shall not
c8094d83 2917 contain default template argument values.
6ba89f8e
MM
2918
2919 So, for a partial specialization, or for a function template,
2920 we look at all of them. */
2921 ;
2922 else
2923 /* But, for a primary class template that is not a partial
2924 specialization we look at all template parameters except the
2925 innermost ones. */
2926 parms = TREE_CHAIN (parms);
2927
2928 /* Figure out what error message to issue. */
2929 if (TREE_CODE (decl) == FUNCTION_DECL)
8653a2c3 2930 msg = "default template arguments may not be used in function templates";
6ba89f8e 2931 else if (is_partial)
8653a2c3 2932 msg = "default template arguments may not be used in partial specializations";
6ba89f8e 2933 else
0f51ccfc 2934 msg = "default argument for template parameter for class enclosing %qD";
6ba89f8e
MM
2935
2936 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2937 /* If we're inside a class definition, there's no need to
104bf76a 2938 examine the parameters to the class itself. On the one
6ba89f8e 2939 hand, they will be checked when the class is defined, and,
0e339752 2940 on the other, default arguments are valid in things like:
0cbd7506
MS
2941 template <class T = double>
2942 struct S { template <class U> void f(U); };
6ba89f8e
MM
2943 Here the default argument for `S' has no bearing on the
2944 declaration of `f'. */
2945 last_level_to_check = template_class_depth (current_class_type) + 1;
2946 else
2947 /* Check everything. */
2948 last_level_to_check = 0;
2949
c8094d83
MS
2950 for (parm_level = parms;
2951 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
66191c20 2952 parm_level = TREE_CHAIN (parm_level))
6ba89f8e 2953 {
66191c20
MM
2954 tree inner_parms = TREE_VALUE (parm_level);
2955 int i;
2956 int ntparms;
6ba89f8e
MM
2957
2958 ntparms = TREE_VEC_LENGTH (inner_parms);
c8094d83 2959 for (i = 0; i < ntparms; ++i)
42b304f1
LM
2960 {
2961 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
2962 continue;
6ba89f8e 2963
42b304f1
LM
2964 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2965 {
2966 if (msg)
2967 {
2968 error (msg, decl);
2969 msg = 0;
2970 }
2971
2972 /* Clear out the default argument so that we are not
2973 confused later. */
2974 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2975 }
2976 }
6ba89f8e
MM
2977
2978 /* At this point, if we're still interested in issuing messages,
2979 they must apply to classes surrounding the object declared. */
2980 if (msg)
c8094d83 2981 msg = "default argument for template parameter for class enclosing %qD";
6ba89f8e
MM
2982 }
2983}
2984
5dd236e2
NS
2985/* Worker for push_template_decl_real, called via
2986 for_each_template_parm. DATA is really an int, indicating the
2987 level of the parameters we are interested in. If T is a template
838dfd8a 2988 parameter of that level, return nonzero. */
5dd236e2
NS
2989
2990static int
3a978d72 2991template_parm_this_level_p (tree t, void* data)
5dd236e2 2992{
6e04241f 2993 int this_level = *(int *)data;
5dd236e2
NS
2994 int level;
2995
2996 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2997 level = TEMPLATE_PARM_LEVEL (t);
2998 else
2999 level = TEMPLATE_TYPE_LEVEL (t);
3000 return level == this_level;
3001}
3002
3ac3d9ea 3003/* Creates a TEMPLATE_DECL for the indicated DECL using the template
f84b4be9
JM
3004 parameters given by current_template_args, or reuses a
3005 previously existing one, if appropriate. Returns the DECL, or an
c8094d83 3006 equivalent one, if it is replaced via a call to duplicate_decls.
6757edfe 3007
d63d5d0c 3008 If IS_FRIEND is true, DECL is a friend declaration. */
3ac3d9ea
MM
3009
3010tree
d63d5d0c 3011push_template_decl_real (tree decl, bool is_friend)
9a3b49ac
MS
3012{
3013 tree tmpl;
f84b4be9 3014 tree args;
9a3b49ac 3015 tree info;
f84b4be9
JM
3016 tree ctx;
3017 int primary;
6ba89f8e 3018 int is_partial;
cfe507be 3019 int new_template_p = 0;
c7222c02
MM
3020 /* True if the template is a member template, in the sense of
3021 [temp.mem]. */
3022 bool member_template_p = false;
6ba89f8e 3023
c0694c4b
MM
3024 if (decl == error_mark_node)
3025 return decl;
3026
6ba89f8e 3027 /* See if this is a partial specialization. */
9188c363 3028 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
6ba89f8e 3029 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
370af2d5 3030 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
6757edfe 3031
d63d5d0c
ILT
3032 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3033 is_friend = true;
f84b4be9
JM
3034
3035 if (is_friend)
3036 /* For a friend, we want the context of the friend function, not
3037 the type of which it is a friend. */
3038 ctx = DECL_CONTEXT (decl);
4f1c5b7d
MM
3039 else if (CP_DECL_CONTEXT (decl)
3040 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
f84b4be9
JM
3041 /* In the case of a virtual function, we want the class in which
3042 it is defined. */
4f1c5b7d 3043 ctx = CP_DECL_CONTEXT (decl);
f84b4be9 3044 else
dc957d14 3045 /* Otherwise, if we're currently defining some class, the DECL
f84b4be9 3046 is assumed to be a member of the class. */
9188c363 3047 ctx = current_scope ();
f84b4be9 3048
2c73f9f5
ML
3049 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3050 ctx = NULL_TREE;
3051
3052 if (!DECL_CONTEXT (decl))
cb0dbb9a 3053 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 3054
6ba89f8e 3055 /* See if this is a primary template. */
c9cbfca6
JM
3056 if (is_friend && ctx)
3057 /* A friend template that specifies a class context, i.e.
3058 template <typename T> friend void A<T>::f();
3059 is not primary. */
3060 primary = 0;
3061 else
3062 primary = template_parm_scope_p ();
9a3b49ac 3063
83566abf
JM
3064 if (primary)
3065 {
c7222c02
MM
3066 if (DECL_CLASS_SCOPE_P (decl))
3067 member_template_p = true;
2f1b1731
MM
3068 if (TREE_CODE (decl) == TYPE_DECL
3069 && ANON_AGGRNAME_P (DECL_NAME (decl)))
33bd39a2 3070 error ("template class without a name");
717a7d5d 3071 else if (TREE_CODE (decl) == FUNCTION_DECL)
4b0d3cbe 3072 {
717a7d5d
MM
3073 if (DECL_DESTRUCTOR_P (decl))
3074 {
3075 /* [temp.mem]
c8094d83 3076
0cbd7506 3077 A destructor shall not be a member template. */
0f51ccfc 3078 error ("destructor %qD declared as member template", decl);
717a7d5d
MM
3079 return error_mark_node;
3080 }
3081 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3082 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3083 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3084 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3085 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3086 == void_list_node)))
3087 {
c8094d83 3088 /* [basic.stc.dynamic.allocation]
717a7d5d 3089
0cbd7506 3090 An allocation function can be a function
717a7d5d
MM
3091 template. ... Template allocation functions shall
3092 have two or more parameters. */
0f51ccfc 3093 error ("invalid template declaration of %qD", decl);
7c60008e 3094 return error_mark_node;
717a7d5d 3095 }
4b0d3cbe 3096 }
8ca4bf25
MM
3097 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3098 && CLASS_TYPE_P (TREE_TYPE (decl)))
2aaf816d
JM
3099 /* OK */;
3100 else
11325dcd 3101 {
0f51ccfc 3102 error ("template declaration of %q#D", decl);
11325dcd
KL
3103 return error_mark_node;
3104 }
83566abf
JM
3105 }
3106
6ba89f8e
MM
3107 /* Check to see that the rules regarding the use of default
3108 arguments are not being violated. */
c8094d83 3109 check_default_tmpl_args (decl, current_template_parms,
6ba89f8e 3110 primary, is_partial);
73aad9b9 3111
6ba89f8e
MM
3112 if (is_partial)
3113 return process_partial_specialization (decl);
d32789d8 3114
9a3b49ac
MS
3115 args = current_template_args ();
3116
c8094d83 3117 if (!ctx
f84b4be9 3118 || TREE_CODE (ctx) == FUNCTION_DECL
a723baf1 3119 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
f84b4be9 3120 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
8d08fdba 3121 {
75650646 3122 if (DECL_LANG_SPECIFIC (decl)
f84b4be9
JM
3123 && DECL_TEMPLATE_INFO (decl)
3124 && DECL_TI_TEMPLATE (decl))
3125 tmpl = DECL_TI_TEMPLATE (decl);
1c10870d
AS
3126 /* If DECL is a TYPE_DECL for a class-template, then there won't
3127 be DECL_LANG_SPECIFIC. The information equivalent to
3128 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
c8094d83 3129 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
1c10870d
AS
3130 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3131 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3132 {
3133 /* Since a template declaration already existed for this
3134 class-type, we must be redeclaring it here. Make sure
0e339752 3135 that the redeclaration is valid. */
1c10870d
AS
3136 redeclare_class_template (TREE_TYPE (decl),
3137 current_template_parms);
3138 /* We don't need to create a new TEMPLATE_DECL; just use the
3139 one we already had. */
3140 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3141 }
f84b4be9 3142 else
786b5245 3143 {
c7222c02
MM
3144 tmpl = build_template_decl (decl, current_template_parms,
3145 member_template_p);
cfe507be
MM
3146 new_template_p = 1;
3147
f84b4be9
JM
3148 if (DECL_LANG_SPECIFIC (decl)
3149 && DECL_TEMPLATE_SPECIALIZATION (decl))
3150 {
3151 /* A specialization of a member template of a template
c6002625 3152 class. */
f84b4be9
JM
3153 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3154 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3155 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3156 }
786b5245 3157 }
8d08fdba
MS
3158 }
3159 else
3160 {
e1a5ccf7 3161 tree a, t, current, parms;
ba4f4e5d 3162 int i;
6633d636 3163
6b9ab5cc
MM
3164 if (TREE_CODE (decl) == TYPE_DECL)
3165 {
ed44da02
MM
3166 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3167 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3168 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3169 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3170 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6b9ab5cc
MM
3171 else
3172 {
0f51ccfc 3173 error ("%qD does not declare a template type", decl);
6b9ab5cc
MM
3174 return decl;
3175 }
3176 }
f3368a90 3177 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
c91a56d2 3178 {
0f51ccfc 3179 error ("template definition of non-template %q#D", decl);
3ac3d9ea 3180 return decl;
c91a56d2 3181 }
8d08fdba 3182 else
5566b478 3183 tmpl = DECL_TI_TEMPLATE (decl);
c8094d83 3184
c353b8e3 3185 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
c8094d83 3186 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
c353b8e3 3187 && DECL_TEMPLATE_SPECIALIZATION (decl)
c7222c02 3188 && DECL_MEMBER_TEMPLATE_P (tmpl))
98c1c668 3189 {
e1a5ccf7
JM
3190 tree new_tmpl;
3191
3192 /* The declaration is a specialization of a member
3193 template, declared outside the class. Therefore, the
3194 innermost template arguments will be NULL, so we
3195 replace them with the arguments determined by the
3196 earlier call to check_explicit_specialization. */
3197 args = DECL_TI_ARGS (decl);
3198
c8094d83 3199 new_tmpl
c7222c02
MM
3200 = build_template_decl (decl, current_template_parms,
3201 member_template_p);
e1a5ccf7
JM
3202 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3203 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3204 DECL_TI_TEMPLATE (decl) = new_tmpl;
3205 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
c8094d83 3206 DECL_TEMPLATE_INFO (new_tmpl)
e1b3e07d 3207 = tree_cons (tmpl, args, NULL_TREE);
e1a5ccf7 3208
c8094d83
MS
3209 register_specialization (new_tmpl,
3210 most_general_template (tmpl),
d63d5d0c
ILT
3211 args,
3212 is_friend);
e1a5ccf7 3213 return decl;
98c1c668 3214 }
98c1c668 3215
e1a5ccf7 3216 /* Make sure the template headers we got make sense. */
6633d636 3217
e1a5ccf7
JM
3218 parms = DECL_TEMPLATE_PARMS (tmpl);
3219 i = TMPL_PARMS_DEPTH (parms);
3220 if (TMPL_ARGS_DEPTH (args) != i)
98c1c668 3221 {
0f51ccfc 3222 error ("expected %d levels of template parms for %q#D, got %d",
0cbd7506 3223 i, decl, TMPL_ARGS_DEPTH (args));
98c1c668 3224 }
e1a5ccf7
JM
3225 else
3226 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3227 {
3228 a = TMPL_ARGS_LEVEL (args, i);
3229 t = INNERMOST_TEMPLATE_PARMS (parms);
3230
3231 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3232 {
3233 if (current == decl)
0f51ccfc 3234 error ("got %d template parameters for %q#D",
0cbd7506 3235 TREE_VEC_LENGTH (a), decl);
e1a5ccf7 3236 else
0f51ccfc 3237 error ("got %d template parameters for %q#T",
0cbd7506 3238 TREE_VEC_LENGTH (a), current);
33bd39a2 3239 error (" but %d required", TREE_VEC_LENGTH (t));
f1cc0515 3240 return error_mark_node;
e1a5ccf7 3241 }
98c1c668 3242
e1a5ccf7 3243 /* Perhaps we should also check that the parms are used in the
0cbd7506 3244 appropriate qualifying scopes in the declarator? */
6633d636 3245
e1a5ccf7
JM
3246 if (current == decl)
3247 current = ctx;
3248 else
3249 current = TYPE_CONTEXT (current);
3250 }
5566b478 3251 }
8d08fdba 3252
5566b478
MS
3253 DECL_TEMPLATE_RESULT (tmpl) = decl;
3254 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 3255
36a117a5
MM
3256 /* Push template declarations for global functions and types. Note
3257 that we do not try to push a global template friend declared in a
3258 template class; such a thing may well depend on the template
39c01e4c 3259 parameters of the class. */
c8094d83 3260 if (new_template_p && !ctx
36a117a5 3261 && !(is_friend && template_class_depth (current_class_type) > 0))
c6f9f83b 3262 {
d63d5d0c 3263 tmpl = pushdecl_namespace_level (tmpl, is_friend);
c6f9f83b
KL
3264 if (tmpl == error_mark_node)
3265 return error_mark_node;
bd3d082e
KL
3266
3267 /* Hide template friend classes that haven't been declared yet. */
3268 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3269 {
3270 DECL_ANTICIPATED (tmpl) = 1;
3271 DECL_FRIEND_P (tmpl) = 1;
3272 }
c6f9f83b 3273 }
8d08fdba 3274
5566b478 3275 if (primary)
5dd236e2
NS
3276 {
3277 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3278 if (DECL_CONV_FN_P (tmpl))
3279 {
6e04241f
GS
3280 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3281
5dd236e2
NS
3282 /* It is a conversion operator. See if the type converted to
3283 depends on innermost template operands. */
c8094d83 3284
d43f603d
KL
3285 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3286 depth))
5dd236e2
NS
3287 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3288 }
3289 }
5566b478 3290
a692ad2e 3291 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
28ba38e3
KL
3292 back to its most general template. If TMPL is a specialization,
3293 ARGS may only have the innermost set of arguments. Add the missing
3294 argument levels if necessary. */
3295 if (DECL_TEMPLATE_INFO (tmpl))
3296 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3297
e1b3e07d 3298 info = tree_cons (tmpl, args, NULL_TREE);
5566b478 3299
9188c363 3300 if (DECL_IMPLICIT_TYPEDEF_P (decl))
8d08fdba 3301 {
ed44da02
MM
3302 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3303 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
b60ecc04
MM
3304 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3305 /* Don't change the name if we've already set it up. */
3306 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
75650646 3307 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
51c184be 3308 }
2aaf816d 3309 else if (DECL_LANG_SPECIFIC (decl))
5566b478 3310 DECL_TEMPLATE_INFO (decl) = info;
3ac3d9ea
MM
3311
3312 return DECL_TEMPLATE_RESULT (tmpl);
8d08fdba
MS
3313}
3314
6757edfe 3315tree
3a978d72 3316push_template_decl (tree decl)
6757edfe 3317{
d63d5d0c 3318 return push_template_decl_real (decl, false);
6757edfe
MM
3319}
3320
3321/* Called when a class template TYPE is redeclared with the indicated
3322 template PARMS, e.g.:
7fe6899f
MM
3323
3324 template <class T> struct S;
3325 template <class T> struct S {}; */
3326
60feef2c 3327bool
3a978d72 3328redeclare_class_template (tree type, tree parms)
7fe6899f 3329{
3d7de1fa 3330 tree tmpl;
6757edfe 3331 tree tmpl_parms;
7fe6899f
MM
3332 int i;
3333
3d7de1fa
MM
3334 if (!TYPE_TEMPLATE_INFO (type))
3335 {
0f51ccfc 3336 error ("%qT is not a template type", type);
60feef2c 3337 return false;
3d7de1fa
MM
3338 }
3339
3340 tmpl = TYPE_TI_TEMPLATE (type);
7fe6899f
MM
3341 if (!PRIMARY_TEMPLATE_P (tmpl))
3342 /* The type is nested in some template class. Nothing to worry
3343 about here; there are no new template parameters for the nested
3344 type. */
60feef2c 3345 return true;
7fe6899f 3346
ee921ddf
MM
3347 if (!parms)
3348 {
3349 error ("template specifiers not specified in declaration of %qD",
3350 tmpl);
60feef2c 3351 return false;
ee921ddf
MM
3352 }
3353
6757edfe
MM
3354 parms = INNERMOST_TEMPLATE_PARMS (parms);
3355 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3356
7fe6899f
MM
3357 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3358 {
dee15844 3359 error ("previous declaration %q+D", tmpl);
f49d8c52 3360 error ("used %d template parameter(s) instead of %d",
c8094d83 3361 TREE_VEC_LENGTH (tmpl_parms),
f49d8c52 3362 TREE_VEC_LENGTH (parms));
60feef2c 3363 return false;
7fe6899f
MM
3364 }
3365
3366 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3367 {
2d8ba2c7
LM
3368 tree tmpl_parm;
3369 tree parm;
3370 tree tmpl_default;
3371 tree parm_default;
3372
3373 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
3374 || TREE_VEC_ELT (parms, i) == error_mark_node)
3375 continue;
3376
3377 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3378 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3379 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3380 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
7fe6899f 3381
2649701f
KL
3382 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3383 TEMPLATE_DECL. */
0f67a82f
LM
3384 if (tmpl_parm != error_mark_node
3385 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3386 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3387 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
7fe6899f 3388 {
dee15844 3389 error ("template parameter %q+#D", tmpl_parm);
0f51ccfc 3390 error ("redeclared here as %q#D", parm);
60feef2c 3391 return false;
7fe6899f
MM
3392 }
3393
3394 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3395 {
3396 /* We have in [temp.param]:
3397
3398 A template-parameter may not be given default arguments
3399 by two different declarations in the same scope. */
0f51ccfc 3400 error ("redefinition of default argument for %q#D", parm);
ddd2d57e 3401 error ("%J original definition appeared here", tmpl_parm);
60feef2c 3402 return false;
7fe6899f
MM
3403 }
3404
3405 if (parm_default != NULL_TREE)
3406 /* Update the previous template parameters (which are the ones
3407 that will really count) with the new default value. */
3408 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
7ce74c5e
MM
3409 else if (tmpl_default != NULL_TREE)
3410 /* Update the new parameters, too; they'll be used as the
3411 parameters for any members. */
3412 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
7fe6899f 3413 }
60feef2c
LM
3414
3415 return true;
7fe6899f 3416}
75650646 3417
9baa27a9
MM
3418/* Simplify EXPR if it is a non-dependent expression. Returns the
3419 (possibly simplified) expression. */
3420
3421tree
3422fold_non_dependent_expr (tree expr)
3423{
d4a200d3
SM
3424 if (expr == NULL_TREE)
3425 return NULL_TREE;
3426
9baa27a9
MM
3427 /* If we're in a template, but EXPR isn't value dependent, simplify
3428 it. We're supposed to treat:
c8094d83 3429
9baa27a9
MM
3430 template <typename T> void f(T[1 + 1]);
3431 template <typename T> void f(T[2]);
c8094d83 3432
9baa27a9
MM
3433 as two declarations of the same function, for example. */
3434 if (processing_template_decl
3435 && !type_dependent_expression_p (expr)
7416ab02 3436 && !value_dependent_expression_p (expr))
9baa27a9
MM
3437 {
3438 HOST_WIDE_INT saved_processing_template_decl;
3439
3440 saved_processing_template_decl = processing_template_decl;
3441 processing_template_decl = 0;
3442 expr = tsubst_copy_and_build (expr,
3443 /*args=*/NULL_TREE,
3444 tf_error,
3445 /*in_decl=*/NULL_TREE,
015c2c66
MM
3446 /*function_p=*/false,
3447 /*integral_constant_expression_p=*/true);
9baa27a9
MM
3448 processing_template_decl = saved_processing_template_decl;
3449 }
3450 return expr;
3451}
3452
b6ab6892
GB
3453/* EXPR is an expression which is used in a constant-expression context.
3454 For instance, it could be a VAR_DECL with a constant initializer.
3455 Extract the innest constant expression.
c8094d83 3456
8a784e4a
NS
3457 This is basically a more powerful version of
3458 integral_constant_value, which can be used also in templates where
3459 initializers can maintain a syntactic rather than semantic form
3460 (even if they are non-dependent, for access-checking purposes). */
b6ab6892 3461
993acaec 3462static tree
b6ab6892
GB
3463fold_decl_constant_value (tree expr)
3464{
4ef69b83
GB
3465 tree const_expr = expr;
3466 do
b6ab6892 3467 {
b6ab6892 3468 expr = fold_non_dependent_expr (const_expr);
4ef69b83 3469 const_expr = integral_constant_value (expr);
b6ab6892 3470 }
4ef69b83 3471 while (expr != const_expr);
b6ab6892 3472
8a784e4a 3473 return expr;
b6ab6892
GB
3474}
3475
3476/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3477 must be a function or a pointer-to-function type, as specified
3478 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3479 and check that the resulting function has external linkage. */
3480
3481static tree
3482convert_nontype_argument_function (tree type, tree expr)
3483{
3484 tree fns = expr;
3485 tree fn, fn_no_ptr;
3486
3487 fn = instantiate_type (type, fns, tf_none);
3488 if (fn == error_mark_node)
3489 return error_mark_node;
3490
3491 fn_no_ptr = fn;
3492 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3493 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
eff3a276
MM
3494 if (TREE_CODE (fn_no_ptr) == BASELINK)
3495 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
3496
b6ab6892
GB
3497 /* [temp.arg.nontype]/1
3498
3499 A template-argument for a non-type, non-template template-parameter
3500 shall be one of:
3501 [...]
3502 -- the address of an object or function with external linkage. */
3503 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3504 {
3505 error ("%qE is not a valid template argument for type %qT "
3506 "because function %qD has not external linkage",
3507 expr, type, fn_no_ptr);
3508 return NULL_TREE;
3509 }
3510
3511 return fn;
3512}
3513
75650646
MM
3514/* Attempt to convert the non-type template parameter EXPR to the
3515 indicated TYPE. If the conversion is successful, return the
dc957d14 3516 converted value. If the conversion is unsuccessful, return
75650646
MM
3517 NULL_TREE if we issued an error message, or error_mark_node if we
3518 did not. We issue error messages for out-and-out bad template
3519 parameters, but not simply because the conversion failed, since we
9baa27a9 3520 might be just trying to do argument deduction. Both TYPE and EXPR
b6ab6892
GB
3521 must be non-dependent.
3522
3523 The conversion follows the special rules described in
3524 [temp.arg.nontype], and it is much more strict than an implicit
3525 conversion.
3526
3527 This function is called twice for each template argument (see
3528 lookup_template_class for a more accurate description of this
3529 problem). This means that we need to handle expressions which
3530 are not valid in a C++ source, but can be created from the
3531 first call (for instance, casts to perform conversions). These
3532 hacks can go away after we fix the double coercion problem. */
75650646
MM
3533
3534static tree
3a978d72 3535convert_nontype_argument (tree type, tree expr)
75650646 3536{
9baa27a9
MM
3537 tree expr_type;
3538
b6ab6892
GB
3539 /* Detect immediately string literals as invalid non-type argument.
3540 This special-case is not needed for correctness (we would easily
3541 catch this later), but only to provide better diagnostic for this
3542 common user mistake. As suggested by DR 100, we do not mention
3543 linkage issues in the diagnostic as this is not the point. */
3544 if (TREE_CODE (expr) == STRING_CST)
3545 {
3546 error ("%qE is not a valid template argument for type %qT "
3547 "because string literals can never be used in this context",
3548 expr, type);
3549 return NULL_TREE;
3550 }
3551
9baa27a9
MM
3552 /* If we are in a template, EXPR may be non-dependent, but still
3553 have a syntactic, rather than semantic, form. For example, EXPR
3554 might be a SCOPE_REF, rather than the VAR_DECL to which the
3555 SCOPE_REF refers. Preserving the qualifying scope is necessary
3556 so that access checking can be performed when the template is
3557 instantiated -- but here we need the resolved form so that we can
3558 convert the argument. */
3559 expr = fold_non_dependent_expr (expr);
b166a559
VR
3560 if (error_operand_p (expr))
3561 return error_mark_node;
9baa27a9 3562 expr_type = TREE_TYPE (expr);
75650646 3563
b6ab6892
GB
3564 /* HACK: Due to double coercion, we can get a
3565 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3566 which is the tree that we built on the first call (see
3567 below when coercing to reference to object or to reference to
3568 function). We just strip everything and get to the arg.
3569 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3570 for examples. */
3571 if (TREE_CODE (expr) == NOP_EXPR)
75650646 3572 {
b6ab6892 3573 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
75650646 3574 {
b6ab6892
GB
3575 /* ??? Maybe we could use convert_from_reference here, but we
3576 would need to relax its constraints because the NOP_EXPR
3577 could actually change the type to something more cv-qualified,
3578 and this is not folded by convert_from_reference. */
3579 tree addr = TREE_OPERAND (expr, 0);
3580 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3581 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3582 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3583 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3584 (TREE_TYPE (expr_type),
3585 TREE_TYPE (TREE_TYPE (addr))));
3586
3587 expr = TREE_OPERAND (addr, 0);
3588 expr_type = TREE_TYPE (expr);
75650646
MM
3589 }
3590
b6ab6892
GB
3591 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3592 parameter is a pointer to object, through decay and
3593 qualification conversion. Let's strip everything. */
3594 else if (TYPE_PTROBV_P (type))
75650646 3595 {
b6ab6892
GB
3596 STRIP_NOPS (expr);
3597 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3598 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3599 /* Skip the ADDR_EXPR only if it is part of the decay for
3600 an array. Otherwise, it is part of the original argument
3601 in the source code. */
3602 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3603 expr = TREE_OPERAND (expr, 0);
3604 expr_type = TREE_TYPE (expr);
f3400fe2 3605 }
75650646 3606 }
c61dce3a 3607
b6ab6892 3608 /* [temp.arg.nontype]/5, bullet 1
75650646 3609
b6ab6892
GB
3610 For a non-type template-parameter of integral or enumeration type,
3611 integral promotions (_conv.prom_) and integral conversions
3612 (_conv.integral_) are applied. */
3613 if (INTEGRAL_TYPE_P (type))
75650646 3614 {
75650646
MM
3615 if (!INTEGRAL_TYPE_P (expr_type))
3616 return error_mark_node;
fddabb2c 3617
b6ab6892
GB
3618 expr = fold_decl_constant_value (expr);
3619 /* Notice that there are constant expressions like '4 % 0' which
3620 do not fold into integer constants. */
db02b6b9 3621 if (TREE_CODE (expr) != INTEGER_CST)
b6ab6892
GB
3622 {
3623 error ("%qE is not a valid template argument for type %qT "
3624 "because it is a non-constant expression", expr, type);
3625 return NULL_TREE;
3626 }
75650646 3627
b6ab6892
GB
3628 /* At this point, an implicit conversion does what we want,
3629 because we already know that the expression is of integral
3630 type. */
3631 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3632 if (expr == error_mark_node)
3633 return error_mark_node;
75650646 3634
b6ab6892
GB
3635 /* Conversion was allowed: fold it to a bare integer constant. */
3636 expr = fold (expr);
3637 }
3638 /* [temp.arg.nontype]/5, bullet 2
75650646 3639
b6ab6892
GB
3640 For a non-type template-parameter of type pointer to object,
3641 qualification conversions (_conv.qual_) and the array-to-pointer
3642 conversion (_conv.array_) are applied. */
3643 else if (TYPE_PTROBV_P (type))
3644 {
3645 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
75650646 3646
b6ab6892
GB
3647 A template-argument for a non-type, non-template template-parameter
3648 shall be one of: [...]
75650646 3649
b6ab6892
GB
3650 -- the name of a non-type template-parameter;
3651 -- the address of an object or function with external linkage, [...]
3652 expressed as "& id-expression" where the & is optional if the name
3653 refers to a function or array, or if the corresponding
3654 template-parameter is a reference.
c8094d83 3655
b6ab6892
GB
3656 Here, we do not care about functions, as they are invalid anyway
3657 for a parameter of type pointer-to-object. */
1082fd10
MM
3658
3659 if (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr))
3660 /* Non-type template parameters are OK. */
3661 ;
3662 else if (TREE_CODE (expr) != ADDR_EXPR
3663 && TREE_CODE (expr_type) != ARRAY_TYPE)
3664 {
3665 if (TREE_CODE (expr) == VAR_DECL)
3666 {
3667 error ("%qD is not a valid template argument "
3668 "because %qD is a variable, not the address of "
3669 "a variable",
3670 expr, expr);
3671 return NULL_TREE;
3672 }
3673 /* Other values, like integer constants, might be valid
3674 non-type arguments of some other type. */
3675 return error_mark_node;
3676 }
3677 else
3678 {
3679 tree decl;
3680
3681 decl = ((TREE_CODE (expr) == ADDR_EXPR)
3682 ? TREE_OPERAND (expr, 0) : expr);
3683 if (TREE_CODE (decl) != VAR_DECL)
3684 {
3685 error ("%qE is not a valid template argument of type %qT "
3686 "because %qE is not a variable",
3687 expr, type, decl);
3688 return NULL_TREE;
3689 }
3690 else if (!DECL_EXTERNAL_LINKAGE_P (decl))
3691 {
3692 error ("%qE is not a valid template argument of type %qT "
3693 "because %qD does not have external linkage",
3694 expr, type, decl);
3695 return NULL_TREE;
3696 }
3697 }
b6ab6892
GB
3698
3699 expr = decay_conversion (expr);
3700 if (expr == error_mark_node)
3701 return error_mark_node;
75650646 3702
b6ab6892
GB
3703 expr = perform_qualification_conversions (type, expr);
3704 if (expr == error_mark_node)
3705 return error_mark_node;
b6ab6892
GB
3706 }
3707 /* [temp.arg.nontype]/5, bullet 3
3708
3709 For a non-type template-parameter of type reference to object, no
3710 conversions apply. The type referred to by the reference may be more
3711 cv-qualified than the (otherwise identical) type of the
3712 template-argument. The template-parameter is bound directly to the
3713 template-argument, which must be an lvalue. */
3714 else if (TYPE_REF_OBJ_P (type))
3715 {
3716 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3717 expr_type))
3718 return error_mark_node;
75650646 3719
b6ab6892
GB
3720 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3721 {
3722 error ("%qE is not a valid template argument for type %qT "
3723 "because of conflicts in cv-qualification", expr, type);
3724 return NULL_TREE;
3725 }
c8094d83 3726
b6ab6892
GB
3727 if (!real_lvalue_p (expr))
3728 {
3729 error ("%qE is not a valid template argument for type %qT "
944fd207 3730 "because it is not an lvalue", expr, type);
b6ab6892
GB
3731 return NULL_TREE;
3732 }
e6e174e5 3733
b6ab6892 3734 /* [temp.arg.nontype]/1
75650646 3735
b6ab6892
GB
3736 A template-argument for a non-type, non-template template-parameter
3737 shall be one of: [...]
75650646 3738
03fd3f84 3739 -- the address of an object or function with external linkage. */
b6ab6892
GB
3740 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3741 {
3742 error ("%qE is not a valid template argument for type %qT "
3743 "because object %qD has not external linkage",
3744 expr, type, expr);
3745 return NULL_TREE;
3746 }
0dc09a61 3747
b6ab6892
GB
3748 expr = build_nop (type, build_address (expr));
3749 }
3750 /* [temp.arg.nontype]/5, bullet 4
3751
3752 For a non-type template-parameter of type pointer to function, only
3753 the function-to-pointer conversion (_conv.func_) is applied. If the
3754 template-argument represents a set of overloaded functions (or a
3755 pointer to such), the matching function is selected from the set
3756 (_over.over_). */
3757 else if (TYPE_PTRFN_P (type))
3758 {
3759 /* If the argument is a template-id, we might not have enough
2c164de6 3760 context information to decay the pointer. */
b6ab6892
GB
3761 if (!type_unknown_p (expr_type))
3762 {
3763 expr = decay_conversion (expr);
3764 if (expr == error_mark_node)
3765 return error_mark_node;
3766 }
75650646 3767
b6ab6892
GB
3768 expr = convert_nontype_argument_function (type, expr);
3769 if (!expr || expr == error_mark_node)
3770 return expr;
3771 }
3772 /* [temp.arg.nontype]/5, bullet 5
75650646 3773
b6ab6892
GB
3774 For a non-type template-parameter of type reference to function, no
3775 conversions apply. If the template-argument represents a set of
3776 overloaded functions, the matching function is selected from the set
3777 (_over.over_). */
3778 else if (TYPE_REFFN_P (type))
3779 {
3780 if (TREE_CODE (expr) == ADDR_EXPR)
3781 {
3782 error ("%qE is not a valid template argument for type %qT "
3783 "because it is a pointer", expr, type);
3784 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3785 return NULL_TREE;
3786 }
75650646 3787
b6ab6892
GB
3788 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3789 if (!expr || expr == error_mark_node)
3790 return expr;
75650646 3791
7866705a 3792 expr = build_nop (type, build_address (expr));
b6ab6892
GB
3793 }
3794 /* [temp.arg.nontype]/5, bullet 6
75650646 3795
b6ab6892
GB
3796 For a non-type template-parameter of type pointer to member function,
3797 no conversions apply. If the template-argument represents a set of
3798 overloaded member functions, the matching member function is selected
3799 from the set (_over.over_). */
3800 else if (TYPE_PTRMEMFUNC_P (type))
3801 {
3802 expr = instantiate_type (type, expr, tf_none);
3803 if (expr == error_mark_node)
3804 return error_mark_node;
75650646 3805
b6ab6892
GB
3806 /* There is no way to disable standard conversions in
3807 resolve_address_of_overloaded_function (called by
3808 instantiate_type). It is possible that the call succeeded by
3809 converting &B::I to &D::I (where B is a base of D), so we need
3810 to reject this conversion here.
75650646 3811
b6ab6892
GB
3812 Actually, even if there was a way to disable standard conversions,
3813 it would still be better to reject them here so that we can
3814 provide a superior diagnostic. */
3815 if (!same_type_p (TREE_TYPE (expr), type))
3816 {
3817 /* Make sure we are just one standard conversion off. */
3818 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3819 error ("%qE is not a valid template argument for type %qT "
3820 "because it is of type %qT", expr, type,
3821 TREE_TYPE (expr));
3822 inform ("standard conversions are not allowed in this context");
3823 return NULL_TREE;
3824 }
3825 }
3826 /* [temp.arg.nontype]/5, bullet 7
59e7a901 3827
b6ab6892
GB
3828 For a non-type template-parameter of type pointer to data member,
3829 qualification conversions (_conv.qual_) are applied. */
3830 else if (TYPE_PTRMEM_P (type))
3831 {
3832 expr = perform_qualification_conversions (type, expr);
3833 if (expr == error_mark_node)
75650646 3834 return expr;
75650646 3835 }
b6ab6892
GB
3836 /* A template non-type parameter must be one of the above. */
3837 else
3838 gcc_unreachable ();
75650646 3839
b6ab6892
GB
3840 /* Sanity check: did we actually convert the argument to the
3841 right type? */
3842 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3843 return expr;
75650646
MM
3844}
3845
b6ab6892 3846
c8094d83
MS
3847/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3848 template template parameters. Both PARM_PARMS and ARG_PARMS are
3849 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
744fac59 3850 or PARM_DECL.
c8094d83 3851
744fac59 3852 Consider the example:
e7e93965
MM
3853 template <class T> class A;
3854 template<template <class U> class TT> class B;
744fac59 3855
e7e93965
MM
3856 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3857 the parameters to A, and OUTER_ARGS contains A. */
744fac59
KL
3858
3859static int
c8094d83 3860coerce_template_template_parms (tree parm_parms,
0cbd7506
MS
3861 tree arg_parms,
3862 tsubst_flags_t complain,
3a978d72 3863 tree in_decl,
0cbd7506 3864 tree outer_args)
744fac59
KL
3865{
3866 int nparms, nargs, i;
3867 tree parm, arg;
3868
50bc768d
NS
3869 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3870 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
744fac59
KL
3871
3872 nparms = TREE_VEC_LENGTH (parm_parms);
3873 nargs = TREE_VEC_LENGTH (arg_parms);
3874
e7e93965 3875 if (nargs != nparms)
744fac59
KL
3876 return 0;
3877
3878 for (i = 0; i < nparms; ++i)
3879 {
2d8ba2c7
LM
3880 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
3881 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
42b304f1
LM
3882 continue;
3883
744fac59
KL
3884 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3885 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3886
3887 if (arg == NULL_TREE || arg == error_mark_node
0cbd7506 3888 || parm == NULL_TREE || parm == error_mark_node)
744fac59
KL
3889 return 0;
3890
3891 if (TREE_CODE (arg) != TREE_CODE (parm))
0cbd7506 3892 return 0;
744fac59
KL
3893
3894 switch (TREE_CODE (parm))
3895 {
3896 case TYPE_DECL:
3897 break;
3898
3899 case TEMPLATE_DECL:
3900 /* We encounter instantiations of templates like
3901 template <template <template <class> class> class TT>
3902 class C; */
700466c2
JM
3903 {
3904 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3905 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3906
8c6ab2db
NS
3907 if (!coerce_template_template_parms
3908 (parmparm, argparm, complain, in_decl, outer_args))
700466c2
JM
3909 return 0;
3910 }
3911 break;
744fac59
KL
3912
3913 case PARM_DECL:
3914 /* The tsubst call is used to handle cases such as
00bdb87f
KL
3915
3916 template <int> class C {};
3917 template <class T, template <T> class TT> class D {};
3918 D<int, C> d;
3919
744fac59 3920 i.e. the parameter list of TT depends on earlier parameters. */
00bdb87f
KL
3921 if (!dependent_type_p (TREE_TYPE (arg))
3922 && !same_type_p
3923 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3924 TREE_TYPE (arg)))
744fac59
KL
3925 return 0;
3926 break;
c8094d83 3927
744fac59 3928 default:
315fb5db 3929 gcc_unreachable ();
744fac59
KL
3930 }
3931 }
3932 return 1;
3933}
3934
8b5b8b7c
MM
3935/* Convert the indicated template ARG as necessary to match the
3936 indicated template PARM. Returns the converted ARG, or
c2ea3a40
NS
3937 error_mark_node if the conversion was unsuccessful. Error and
3938 warning messages are issued under control of COMPLAIN. This
3939 conversion is for the Ith parameter in the parameter list. ARGS is
3940 the full set of template arguments deduced so far. */
8b5b8b7c
MM
3941
3942static tree
c8094d83 3943convert_template_argument (tree parm,
0cbd7506
MS
3944 tree arg,
3945 tree args,
3946 tsubst_flags_t complain,
3947 int i,
3948 tree in_decl)
8b5b8b7c
MM
3949{
3950 tree val;
8b5b8b7c 3951 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
c8094d83 3952
c8094d83 3953 if (TREE_CODE (arg) == TREE_LIST
a5ac359a 3954 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
c8094d83 3955 {
8b5b8b7c
MM
3956 /* The template argument was the name of some
3957 member function. That's usually
0e339752 3958 invalid, but static members are OK. In any
8b5b8b7c
MM
3959 case, grab the underlying fields/functions
3960 and issue an error later if required. */
3961 arg = TREE_VALUE (arg);
3962 TREE_TYPE (arg) = unknown_type_node;
3963 }
3964
3965 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3966 requires_type = (TREE_CODE (parm) == TYPE_DECL
3967 || requires_tmpl_type);
3968
cbd63935
KL
3969 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3970 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3971 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3972 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
c8094d83 3973
b8c6534b
KL
3974 if (is_tmpl_type
3975 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3976 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8b5b8b7c 3977 arg = TYPE_STUB_DECL (arg);
8b5b8b7c 3978
2f939d94 3979 is_type = TYPE_P (arg) || is_tmpl_type;
8b5b8b7c
MM
3980
3981 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3982 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3983 {
0f51ccfc 3984 pedwarn ("to refer to a type member of a template parameter, "
0cbd7506 3985 "use %<typename %E%>", arg);
c8094d83 3986
8b5b8b7c 3987 arg = make_typename_type (TREE_OPERAND (arg, 0),
3baa501d 3988 TREE_OPERAND (arg, 1),
fc6a28d7 3989 typename_type,
c2ea3a40 3990 complain & tf_error);
8b5b8b7c
MM
3991 is_type = 1;
3992 }
3993 if (is_type != requires_type)
3994 {
3995 if (in_decl)
3996 {
c2ea3a40 3997 if (complain & tf_error)
8b5b8b7c 3998 {
0f51ccfc 3999 error ("type/value mismatch at argument %d in template "
0cbd7506
MS
4000 "parameter list for %qD",
4001 i + 1, in_decl);
8b5b8b7c 4002 if (is_type)
0f51ccfc 4003 error (" expected a constant of type %qT, got %qT",
0cbd7506
MS
4004 TREE_TYPE (parm),
4005 (is_tmpl_type ? DECL_NAME (arg) : arg));
d12a7283 4006 else if (requires_tmpl_type)
0f51ccfc 4007 error (" expected a class template, got %qE", arg);
8b5b8b7c 4008 else
0f51ccfc 4009 error (" expected a type, got %qE", arg);
8b5b8b7c
MM
4010 }
4011 }
4012 return error_mark_node;
4013 }
4014 if (is_tmpl_type ^ requires_tmpl_type)
4015 {
c2ea3a40 4016 if (in_decl && (complain & tf_error))
8b5b8b7c 4017 {
0f51ccfc 4018 error ("type/value mismatch at argument %d in template "
0cbd7506
MS
4019 "parameter list for %qD",
4020 i + 1, in_decl);
8b5b8b7c 4021 if (is_tmpl_type)
0f51ccfc 4022 error (" expected a type, got %qT", DECL_NAME (arg));
8b5b8b7c 4023 else
0f51ccfc 4024 error (" expected a class template, got %qT", arg);
8b5b8b7c
MM
4025 }
4026 return error_mark_node;
4027 }
c8094d83 4028
8b5b8b7c
MM
4029 if (is_type)
4030 {
4031 if (requires_tmpl_type)
4032 {
b8c6534b
KL
4033 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
4034 /* The number of argument required is not known yet.
4035 Just accept it for now. */
4036 val = TREE_TYPE (arg);
8b5b8b7c
MM
4037 else
4038 {
b8c6534b
KL
4039 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
4040 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
4041
4042 if (coerce_template_template_parms (parmparm, argparm,
4043 complain, in_decl,
6150b602 4044 args))
8b5b8b7c 4045 {
b8c6534b 4046 val = arg;
c8094d83
MS
4047
4048 /* TEMPLATE_TEMPLATE_PARM node is preferred over
b8c6534b 4049 TEMPLATE_DECL. */
c8094d83 4050 if (val != error_mark_node
b8c6534b
KL
4051 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
4052 val = TREE_TYPE (val);
8b5b8b7c 4053 }
b8c6534b
KL
4054 else
4055 {
c2ea3a40 4056 if (in_decl && (complain & tf_error))
b8c6534b 4057 {
0f51ccfc 4058 error ("type/value mismatch at argument %d in "
0cbd7506
MS
4059 "template parameter list for %qD",
4060 i + 1, in_decl);
0f51ccfc 4061 error (" expected a template of type %qD, got %qD",
0cbd7506 4062 parm, arg);
b8c6534b 4063 }
c8094d83 4064
b8c6534b
KL
4065 val = error_mark_node;
4066 }
8b5b8b7c
MM
4067 }
4068 }
4069 else
058b15c1 4070 val = arg;
685e39c2
MM
4071 /* We only form one instance of each template specialization.
4072 Therefore, if we use a non-canonical variant (i.e., a
3db45ab5 4073 typedef), any future messages referring to the type will use
685e39c2
MM
4074 the typedef, which is confusing if those future uses do not
4075 themselves also use the typedef. */
4076 if (TYPE_P (val))
4077 val = canonical_type_variant (val);
8b5b8b7c
MM
4078 }
4079 else
4080 {
4393e105 4081 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
8b5b8b7c 4082
11b810f1 4083 if (invalid_nontype_parm_type_p (t, complain))
0cbd7506 4084 return error_mark_node;
c8094d83 4085
d768a589 4086 if (!uses_template_parms (arg) && !uses_template_parms (t))
8b5b8b7c
MM
4087 /* We used to call digest_init here. However, digest_init
4088 will report errors, which we don't want when complain
4089 is zero. More importantly, digest_init will try too
4090 hard to convert things: for example, `0' should not be
4091 converted to pointer type at this point according to
4092 the standard. Accepting this is not merely an
4093 extension, since deciding whether or not these
4094 conversions can occur is part of determining which
dc957d14 4095 function template to call, or whether a given explicit
0e339752 4096 argument specification is valid. */
8b5b8b7c
MM
4097 val = convert_nontype_argument (t, arg);
4098 else
4099 val = arg;
4100
4101 if (val == NULL_TREE)
4102 val = error_mark_node;
c2ea3a40 4103 else if (val == error_mark_node && (complain & tf_error))
0f51ccfc 4104 error ("could not convert template argument %qE to %qT", arg, t);
8b5b8b7c
MM
4105 }
4106
4107 return val;
4108}
4109
4110/* Convert all template arguments to their appropriate types, and
4111 return a vector containing the innermost resulting template
c2ea3a40 4112 arguments. If any error occurs, return error_mark_node. Error and
3e4a3562 4113 warning messages are issued under control of COMPLAIN.
75650646 4114
e7e93965 4115 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
84dc00e8 4116 for arguments not specified in ARGS. Otherwise, if
e7e93965
MM
4117 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4118 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4119 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4120 ARGS. */
c8094d83 4121
8d08fdba 4122static tree
c8094d83 4123coerce_template_parms (tree parms,
0cbd7506
MS
4124 tree args,
4125 tree in_decl,
3a978d72 4126 tsubst_flags_t complain,
e7e93965
MM
4127 bool require_all_args,
4128 bool use_default_args)
8d08fdba 4129{
a292b002 4130 int nparms, nargs, i, lost = 0;
e4a84209 4131 tree inner_args;
8b5b8b7c
MM
4132 tree new_args;
4133 tree new_inner_args;
a292b002 4134
f9a7ae04 4135 inner_args = INNERMOST_TEMPLATE_ARGS (args);
bf12d54d 4136 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
a292b002
MS
4137 nparms = TREE_VEC_LENGTH (parms);
4138
4139 if (nargs > nparms
4140 || (nargs < nparms
e7e93965
MM
4141 && require_all_args
4142 && (!use_default_args
42b304f1
LM
4143 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4144 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8d08fdba 4145 {
c8094d83 4146 if (complain & tf_error)
75650646 4147 {
33bd39a2 4148 error ("wrong number of template arguments (%d, should be %d)",
0cbd7506 4149 nargs, nparms);
c8094d83 4150
75650646 4151 if (in_decl)
dee15844 4152 error ("provided for %q+D", in_decl);
75650646
MM
4153 }
4154
8d08fdba
MS
4155 return error_mark_node;
4156 }
4157
f31c0a32 4158 new_inner_args = make_tree_vec (nparms);
8b5b8b7c
MM
4159 new_args = add_outermost_template_args (args, new_inner_args);
4160 for (i = 0; i < nparms; i++)
8d08fdba 4161 {
8b5b8b7c
MM
4162 tree arg;
4163 tree parm;
e4a84209 4164
8b5b8b7c
MM
4165 /* Get the Ith template parameter. */
4166 parm = TREE_VEC_ELT (parms, i);
42b304f1
LM
4167
4168 if (parm == error_mark_node)
2d8ba2c7
LM
4169 {
4170 TREE_VEC_ELT (new_inner_args, i) = error_mark_node;
42b304f1 4171 continue;
2d8ba2c7 4172 }
75650646 4173
8b5b8b7c 4174 /* Calculate the Ith argument. */
bf12d54d 4175 if (i < nargs)
8b5b8b7c 4176 arg = TREE_VEC_ELT (inner_args, i);
e7e93965 4177 else if (require_all_args)
04c06002 4178 /* There must be a default arg in this case. */
a91db711
NS
4179 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4180 complain, in_decl);
ffd49b19
NS
4181 else
4182 break;
c8094d83 4183
50bc768d 4184 gcc_assert (arg);
ffd49b19 4185 if (arg == error_mark_node)
e34b0922
KL
4186 {
4187 if (complain & tf_error)
4188 error ("template argument %d is invalid", i + 1);
4189 }
c8094d83
MS
4190 else
4191 arg = convert_template_argument (TREE_VALUE (parm),
8b5b8b7c 4192 arg, new_args, complain, i,
c8094d83
MS
4193 in_decl);
4194
8b5b8b7c 4195 if (arg == error_mark_node)
8d08fdba 4196 lost++;
8b5b8b7c 4197 TREE_VEC_ELT (new_inner_args, i) = arg;
8d08fdba 4198 }
8b5b8b7c 4199
8d08fdba
MS
4200 if (lost)
4201 return error_mark_node;
8b5b8b7c
MM
4202
4203 return new_inner_args;
8d08fdba
MS
4204}
4205
34016c81
JM
4206/* Returns 1 if template args OT and NT are equivalent. */
4207
d8e178a0 4208static int
3a978d72 4209template_args_equal (tree ot, tree nt)
34016c81
JM
4210{
4211 if (nt == ot)
4212 return 1;
74601d7c 4213
34016c81
JM
4214 if (TREE_CODE (nt) == TREE_VEC)
4215 /* For member templates */
74601d7c
KL
4216 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4217 else if (TYPE_P (nt))
660845bf 4218 return TYPE_P (ot) && same_type_p (ot, nt);
74601d7c
KL
4219 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4220 return 0;
34016c81 4221 else
c8a209ca 4222 return cp_tree_equal (ot, nt);
34016c81
JM
4223}
4224
4225/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
f84b4be9
JM
4226 of template arguments. Returns 0 otherwise. */
4227
6757edfe 4228int
3a978d72 4229comp_template_args (tree oldargs, tree newargs)
5566b478
MS
4230{
4231 int i;
4232
386b8a85
JM
4233 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4234 return 0;
4235
5566b478
MS
4236 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4237 {
4238 tree nt = TREE_VEC_ELT (newargs, i);
4239 tree ot = TREE_VEC_ELT (oldargs, i);
4240
34016c81 4241 if (! template_args_equal (ot, nt))
61a127b3 4242 return 0;
5566b478
MS
4243 }
4244 return 1;
4245}
4246
8d08fdba
MS
4247/* Given class template name and parameter list, produce a user-friendly name
4248 for the instantiation. */
e92cc029 4249
8d08fdba 4250static char *
3a978d72 4251mangle_class_name_for_template (const char* name, tree parms, tree arglist)
8d08fdba
MS
4252{
4253 static struct obstack scratch_obstack;
4254 static char *scratch_firstobj;
4255 int i, nparms;
8d08fdba
MS
4256
4257 if (!scratch_firstobj)
fc378698 4258 gcc_obstack_init (&scratch_obstack);
8d08fdba
MS
4259 else
4260 obstack_free (&scratch_obstack, scratch_firstobj);
67f5655f 4261 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
8d08fdba 4262
18e314ad
GS
4263#define ccat(C) obstack_1grow (&scratch_obstack, (C));
4264#define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
8d08fdba
MS
4265
4266 cat (name);
4267 ccat ('<');
4268 nparms = TREE_VEC_LENGTH (parms);
f9a7ae04 4269 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
50bc768d 4270 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
8d08fdba
MS
4271 for (i = 0; i < nparms; i++)
4272 {
42b304f1
LM
4273 tree parm;
4274 tree arg;
4275
42b304f1
LM
4276 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4277 arg = TREE_VEC_ELT (arglist, i);
8d08fdba 4278
0f67a82f
LM
4279 if (parm == error_mark_node)
4280 continue;
4281
8d08fdba
MS
4282 if (i)
4283 ccat (',');
4284
a292b002 4285 if (TREE_CODE (parm) == TYPE_DECL)
8d08fdba 4286 {
761f0855 4287 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
8d08fdba
MS
4288 continue;
4289 }
73b0fce8
KL
4290 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4291 {
4292 if (TREE_CODE (arg) == TEMPLATE_DECL)
2c73f9f5 4293 {
c8094d83 4294 /* Already substituted with real template. Just output
2c73f9f5 4295 the template name here */
0cbd7506
MS
4296 tree context = DECL_CONTEXT (arg);
4297 if (context)
4298 {
4299 /* The template may be defined in a namespace, or
4300 may be a member template. */
4301 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
50bc768d
NS
4302 || CLASS_TYPE_P (context));
4303 cat (decl_as_string (DECL_CONTEXT (arg),
4304 TFF_PLAIN_IDENTIFIER));
4305 cat ("::");
2c73f9f5
ML
4306 }
4307 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4308 }
73b0fce8 4309 else
f4f206f4 4310 /* Output the parameter declaration. */
761f0855 4311 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
73b0fce8
KL
4312 continue;
4313 }
8d08fdba 4314 else
50bc768d 4315 gcc_assert (TREE_CODE (parm) == PARM_DECL);
8d08fdba 4316
8d08fdba
MS
4317 /* No need to check arglist against parmlist here; we did that
4318 in coerce_template_parms, called from lookup_template_class. */
761f0855 4319 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
8d08fdba
MS
4320 }
4321 {
4322 char *bufp = obstack_next_free (&scratch_obstack);
4323 int offset = 0;
4324 while (bufp[offset - 1] == ' ')
4325 offset--;
4326 obstack_blank_fast (&scratch_obstack, offset);
4327
4328 /* B<C<char> >, not B<C<char>> */
4329 if (bufp[offset - 1] == '>')
4330 ccat (' ');
4331 }
4332 ccat ('>');
4333 ccat ('\0');
4334 return (char *) obstack_base (&scratch_obstack);
8d08fdba
MS
4335}
4336
bd6dd845 4337static tree
3a978d72 4338classtype_mangled_name (tree t)
5566b478
MS
4339{
4340 if (CLASSTYPE_TEMPLATE_INFO (t)
36a117a5
MM
4341 /* Specializations have already had their names set up in
4342 lookup_template_class. */
370af2d5 4343 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9fbf56f7
MM
4344 {
4345 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4346
36a117a5
MM
4347 /* For non-primary templates, the template parameters are
4348 implicit from their surrounding context. */
9fbf56f7
MM
4349 if (PRIMARY_TEMPLATE_P (tmpl))
4350 {
4351 tree name = DECL_NAME (tmpl);
4352 char *mangled_name = mangle_class_name_for_template
c8094d83 4353 (IDENTIFIER_POINTER (name),
9fbf56f7
MM
4354 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4355 CLASSTYPE_TI_ARGS (t));
4356 tree id = get_identifier (mangled_name);
4357 IDENTIFIER_TEMPLATE (id) = name;
4358 return id;
4359 }
5566b478 4360 }
9fbf56f7
MM
4361
4362 return TYPE_IDENTIFIER (t);
5566b478
MS
4363}
4364
4365static void
3a978d72 4366add_pending_template (tree d)
5566b478 4367{
3ae18eaf
JM
4368 tree ti = (TYPE_P (d)
4369 ? CLASSTYPE_TEMPLATE_INFO (d)
4370 : DECL_TEMPLATE_INFO (d));
46ccf50a 4371 tree pt;
3ae18eaf 4372 int level;
e92cc029 4373
824b9a4c 4374 if (TI_PENDING_TEMPLATE_FLAG (ti))
5566b478
MS
4375 return;
4376
3ae18eaf
JM
4377 /* We are called both from instantiate_decl, where we've already had a
4378 tinst_level pushed, and instantiate_template, where we haven't.
4379 Compensate. */
4380 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4381
4382 if (level)
4383 push_tinst_level (d);
4384
46ccf50a
JM
4385 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4386 if (last_pending_template)
4387 TREE_CHAIN (last_pending_template) = pt;
4388 else
4389 pending_templates = pt;
4390
4391 last_pending_template = pt;
4392
824b9a4c 4393 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3ae18eaf
JM
4394
4395 if (level)
4396 pop_tinst_level ();
5566b478
MS
4397}
4398
386b8a85 4399
4ba126e4
MM
4400/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4401 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4402 documentation for TEMPLATE_ID_EXPR. */
386b8a85
JM
4403
4404tree
3a978d72 4405lookup_template_function (tree fns, tree arglist)
386b8a85 4406{
2c73f9f5 4407 tree type;
050367a3 4408
4ba126e4
MM
4409 if (fns == error_mark_node || arglist == error_mark_node)
4410 return error_mark_node;
4411
50bc768d 4412 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4230cec2
NS
4413 gcc_assert (fns && (is_overloaded_fn (fns)
4414 || TREE_CODE (fns) == IDENTIFIER_NODE));
4ba126e4 4415
50ad9642
MM
4416 if (BASELINK_P (fns))
4417 {
f293ce4b
RS
4418 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4419 unknown_type_node,
4420 BASELINK_FUNCTIONS (fns),
4421 arglist);
50ad9642
MM
4422 return fns;
4423 }
4424
2c73f9f5
ML
4425 type = TREE_TYPE (fns);
4426 if (TREE_CODE (fns) == OVERLOAD || !type)
4427 type = unknown_type_node;
c8094d83 4428
f293ce4b 4429 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
386b8a85
JM
4430}
4431
a2b60a0e
MM
4432/* Within the scope of a template class S<T>, the name S gets bound
4433 (in build_self_reference) to a TYPE_DECL for the class, not a
4434 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4435 or one of its enclosing classes, and that type is a template,
4436 return the associated TEMPLATE_DECL. Otherwise, the original
4437 DECL is returned. */
4438
a723baf1 4439tree
3a978d72 4440maybe_get_template_decl_from_type_decl (tree decl)
a2b60a0e
MM
4441{
4442 return (decl != NULL_TREE
c8094d83 4443 && TREE_CODE (decl) == TYPE_DECL
a2b60a0e 4444 && DECL_ARTIFICIAL (decl)
511b60ff 4445 && CLASS_TYPE_P (TREE_TYPE (decl))
c8094d83 4446 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
a2b60a0e
MM
4447 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4448}
386b8a85 4449
8d08fdba
MS
4450/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4451 parameters, find the desired type.
4452
4453 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8d08fdba
MS
4454
4455 IN_DECL, if non-NULL, is the template declaration we are trying to
c8094d83 4456 instantiate.
75650646 4457
838dfd8a 4458 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
36a117a5 4459 the class we are looking up.
c8094d83 4460
c2ea3a40 4461 Issue error and warning messages under control of COMPLAIN.
36a117a5 4462
75650646
MM
4463 If the template class is really a local class in a template
4464 function, then the FUNCTION_CONTEXT is the function in which it is
c8094d83 4465 being instantiated.
b6ab6892
GB
4466
4467 ??? Note that this function is currently called *twice* for each
4468 template-id: the first time from the parser, while creating the
4469 incomplete type (finish_template_type), and the second type during the
4470 real instantiation (instantiate_template_class). This is surely something
4471 that we want to avoid. It also causes some problems with argument
4472 coercion (see convert_nontype_argument for more information on this). */
e92cc029 4473
8d08fdba 4474tree
c8094d83 4475lookup_template_class (tree d1,
0cbd7506
MS
4476 tree arglist,
4477 tree in_decl,
4478 tree context,
4479 int entering_scope,
4480 tsubst_flags_t complain)
8d08fdba 4481{
a703fb38 4482 tree template = NULL_TREE, parmlist;
dbfe2124 4483 tree t;
c8094d83 4484
fd295cb2 4485 timevar_push (TV_NAME_LOOKUP);
c8094d83 4486
5566b478
MS
4487 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4488 {
90ea9897
MM
4489 tree value = innermost_non_namespace_value (d1);
4490 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4491 template = value;
73b0fce8
KL
4492 else
4493 {
2c73f9f5
ML
4494 if (context)
4495 push_decl_namespace (context);
10e6657a 4496 template = lookup_name (d1);
3ebc5c52 4497 template = maybe_get_template_decl_from_type_decl (template);
2c73f9f5
ML
4498 if (context)
4499 pop_decl_namespace ();
73b0fce8 4500 }
8d019cef
JM
4501 if (template)
4502 context = DECL_CONTEXT (template);
5566b478 4503 }
c91a56d2
MS
4504 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4505 {
802dbc34
JM
4506 tree type = TREE_TYPE (d1);
4507
4508 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4509 an implicit typename for the second A. Deal with it. */
4510 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4511 type = TREE_TYPE (type);
c8094d83 4512
802dbc34 4513 if (CLASSTYPE_TEMPLATE_INFO (type))
f3400fe2 4514 {
802dbc34 4515 template = CLASSTYPE_TI_TEMPLATE (type);
f3400fe2
JM
4516 d1 = DECL_NAME (template);
4517 }
c91a56d2 4518 }
c8094d83 4519 else if (TREE_CODE (d1) == ENUMERAL_TYPE
2f939d94 4520 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
5566b478 4521 {
ed44da02 4522 template = TYPE_TI_TEMPLATE (d1);
5566b478
MS
4523 d1 = DECL_NAME (template);
4524 }
93cdc044 4525 else if (TREE_CODE (d1) == TEMPLATE_DECL
17aec3eb 4526 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
93cdc044
JM
4527 {
4528 template = d1;
4529 d1 = DECL_NAME (template);
4530 context = DECL_CONTEXT (template);
4531 }
8d08fdba 4532
90ea9897 4533 /* Issue an error message if we didn't find a template. */
8d08fdba 4534 if (! template)
f3400fe2 4535 {
c2ea3a40 4536 if (complain & tf_error)
0cbd7506 4537 error ("%qT is not a template", d1);
fd295cb2 4538 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
f3400fe2 4539 }
2c73f9f5 4540
a87b4257 4541 if (TREE_CODE (template) != TEMPLATE_DECL
0cbd7506 4542 /* Make sure it's a user visible template, if it was named by
42eaed49
NS
4543 the user. */
4544 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4545 && !PRIMARY_TEMPLATE_P (template)))
8d08fdba 4546 {
c2ea3a40 4547 if (complain & tf_error)
0cbd7506
MS
4548 {
4549 error ("non-template type %qT used as a template", d1);
4550 if (in_decl)
dee15844 4551 error ("for template declaration %q+D", in_decl);
f9c244b8 4552 }
fd295cb2 4553 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
8d08fdba 4554 }
8d08fdba 4555
42eaed49 4556 complain &= ~tf_user;
c8094d83 4557
73b0fce8
KL
4558 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4559 {
4560 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
0cbd7506 4561 template arguments */
73b0fce8 4562
1899c3a4 4563 tree parm;
73b0fce8
KL
4564 tree arglist2;
4565
73b0fce8
KL
4566 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4567
4f96ff63
KL
4568 /* Consider an example where a template template parameter declared as
4569
4570 template <class T, class U = std::allocator<T> > class TT
4571
c8094d83
MS
4572 The template parameter level of T and U are one level larger than
4573 of TT. To proper process the default argument of U, say when an
4f96ff63 4574 instantiation `TT<int>' is seen, we need to build the full
342cea95
KL
4575 arguments containing {int} as the innermost level. Outer levels,
4576 available when not appearing as default template argument, can be
4577 obtained from `current_template_args ()'.
4f96ff63 4578
342cea95
KL
4579 Suppose that TT is later substituted with std::vector. The above
4580 instantiation is `TT<int, std::allocator<T> >' with TT at
4581 level 1, and T at level 2, while the template arguments at level 1
4582 becomes {std::vector} and the inner level 2 is {int}. */
4583
4584 if (current_template_parms)
4f96ff63
KL
4585 arglist = add_to_template_args (current_template_args (), arglist);
4586
f9c244b8 4587 arglist2 = coerce_template_parms (parmlist, arglist, template,
3db45ab5 4588 complain,
e7e93965
MM
4589 /*require_all_args=*/true,
4590 /*use_default_args=*/true);
3e4a3562 4591 if (arglist2 == error_mark_node
544aef8c 4592 || (!uses_template_parms (arglist2)
3e4a3562 4593 && check_instantiated_args (template, arglist2, complain)))
0cbd7506 4594 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
73b0fce8 4595
dac65501 4596 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
fd295cb2 4597 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
73b0fce8 4598 }
c8094d83 4599 else
8d08fdba 4600 {
36a117a5 4601 tree template_type = TREE_TYPE (template);
7ac7b28f 4602 tree gen_tmpl;
36a117a5
MM
4603 tree type_decl;
4604 tree found = NULL_TREE;
4605 int arg_depth;
4606 int parm_depth;
dbfe2124 4607 int is_partial_instantiation;
830bfa74 4608
7ac7b28f
MM
4609 gen_tmpl = most_general_template (template);
4610 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
36a117a5
MM
4611 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4612 arg_depth = TMPL_ARGS_DEPTH (arglist);
4613
4614 if (arg_depth == 1 && parm_depth > 1)
4615 {
39c01e4c 4616 /* We've been given an incomplete set of template arguments.
36a117a5
MM
4617 For example, given:
4618
4619 template <class T> struct S1 {
0cbd7506 4620 template <class U> struct S2 {};
36a117a5 4621 template <class U> struct S2<U*> {};
0cbd7506 4622 };
c8094d83 4623
36a117a5
MM
4624 we will be called with an ARGLIST of `U*', but the
4625 TEMPLATE will be `template <class T> template
4626 <class U> struct S1<T>::S2'. We must fill in the missing
4627 arguments. */
c8094d83 4628 arglist
7ac7b28f
MM
4629 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4630 arglist);
36a117a5
MM
4631 arg_depth = TMPL_ARGS_DEPTH (arglist);
4632 }
5566b478 4633
41f5d4b1 4634 /* Now we should have enough arguments. */
50bc768d 4635 gcc_assert (parm_depth == arg_depth);
c8094d83 4636
7ac7b28f
MM
4637 /* From here on, we're only interested in the most general
4638 template. */
4639 template = gen_tmpl;
4640
36a117a5
MM
4641 /* Calculate the BOUND_ARGS. These will be the args that are
4642 actually tsubst'd into the definition to create the
4643 instantiation. */
4644 if (parm_depth > 1)
830bfa74
MM
4645 {
4646 /* We have multiple levels of arguments to coerce, at once. */
830bfa74 4647 int i;
e4a84209 4648 int saved_depth = TMPL_ARGS_DEPTH (arglist);
36a117a5 4649
f31c0a32 4650 tree bound_args = make_tree_vec (parm_depth);
c8094d83 4651
e4a84209 4652 for (i = saved_depth,
c8094d83 4653 t = DECL_TEMPLATE_PARMS (template);
e4a84209 4654 i > 0 && t != NULL_TREE;
830bfa74 4655 --i, t = TREE_CHAIN (t))
e4a84209 4656 {
ee3071ef
NS
4657 tree a = coerce_template_parms (TREE_VALUE (t),
4658 arglist, template,
3db45ab5 4659 complain,
e7e93965
MM
4660 /*require_all_args=*/true,
4661 /*use_default_args=*/true);
88e98cfe
KL
4662
4663 /* Don't process further if one of the levels fails. */
4664 if (a == error_mark_node)
4665 {
4666 /* Restore the ARGLIST to its full size. */
4667 TREE_VEC_LENGTH (arglist) = saved_depth;
4668 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4669 }
c8094d83 4670
e4a84209
MM
4671 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4672
4673 /* We temporarily reduce the length of the ARGLIST so
4674 that coerce_template_parms will see only the arguments
4675 corresponding to the template parameters it is
4676 examining. */
4677 TREE_VEC_LENGTH (arglist)--;
4678 }
4679
4680 /* Restore the ARGLIST to its full size. */
4681 TREE_VEC_LENGTH (arglist) = saved_depth;
4682
36a117a5 4683 arglist = bound_args;
830bfa74
MM
4684 }
4685 else
36a117a5
MM
4686 arglist
4687 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
f9a7ae04 4688 INNERMOST_TEMPLATE_ARGS (arglist),
f9c244b8 4689 template,
3db45ab5 4690 complain,
e7e93965
MM
4691 /*require_all_args=*/true,
4692 /*use_default_args=*/true);
36a117a5 4693
c353b8e3 4694 if (arglist == error_mark_node)
36a117a5 4695 /* We were unable to bind the arguments. */
fd295cb2 4696 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5566b478 4697
36a117a5
MM
4698 /* In the scope of a template class, explicit references to the
4699 template class refer to the type of the template, not any
4700 instantiation of it. For example, in:
c8094d83 4701
36a117a5
MM
4702 template <class T> class C { void f(C<T>); }
4703
4704 the `C<T>' is just the same as `C'. Outside of the
4705 class, however, such a reference is an instantiation. */
ed44da02 4706 if (comp_template_args (TYPE_TI_ARGS (template_type),
36a117a5
MM
4707 arglist))
4708 {
4709 found = template_type;
c8094d83 4710
36a117a5 4711 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5566b478 4712 {
36a117a5 4713 tree ctx;
c8094d83
MS
4714
4715 for (ctx = current_class_type;
5f04800c
KL
4716 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4717 ctx = (TYPE_P (ctx)
4718 ? TYPE_CONTEXT (ctx)
4719 : DECL_CONTEXT (ctx)))
4720 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4721 goto found_ctx;
c8094d83 4722
6df5158a
NS
4723 /* We're not in the scope of the class, so the
4724 TEMPLATE_TYPE is not the type we want after all. */
4725 found = NULL_TREE;
4726 found_ctx:;
5566b478
MS
4727 }
4728 }
36a117a5 4729 if (found)
0cbd7506 4730 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
414ea4aa 4731
c7222c02
MM
4732 /* If we already have this specialization, return it. */
4733 found = retrieve_specialization (template, arglist,
4734 /*class_specializations_p=*/false);
4735 if (found)
4736 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5566b478 4737
dbfe2124 4738 /* This type is a "partial instantiation" if any of the template
ab097535 4739 arguments still involve template parameters. Note that we set
486e4077
MM
4740 IS_PARTIAL_INSTANTIATION for partial specializations as
4741 well. */
dbfe2124
MM
4742 is_partial_instantiation = uses_template_parms (arglist);
4743
c353b8e3
MM
4744 /* If the deduced arguments are invalid, then the binding
4745 failed. */
4746 if (!is_partial_instantiation
4747 && check_instantiated_args (template,
4748 INNERMOST_TEMPLATE_ARGS (arglist),
4749 complain))
4750 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
c8094d83
MS
4751
4752 if (!is_partial_instantiation
3ebc5c52
MM
4753 && !PRIMARY_TEMPLATE_P (template)
4754 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4755 {
3ebc5c52
MM
4756 found = xref_tag_from_type (TREE_TYPE (template),
4757 DECL_NAME (template),
29ef83de 4758 /*tag_scope=*/ts_global);
fd295cb2 4759 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
3ebc5c52 4760 }
c8094d83 4761
95ee998c 4762 context = tsubst (DECL_CONTEXT (template), arglist,
c2ea3a40 4763 complain, in_decl);
95ee998c
MM
4764 if (!context)
4765 context = global_namespace;
4766
36a117a5 4767 /* Create the type. */
ed44da02
MM
4768 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4769 {
dbfe2124 4770 if (!is_partial_instantiation)
92777ce4
NS
4771 {
4772 set_current_access_from_decl (TYPE_NAME (template_type));
4773 t = start_enum (TYPE_IDENTIFIER (template_type));
4774 }
ed44da02 4775 else
dbfe2124 4776 /* We don't want to call start_enum for this type, since
ed44da02
MM
4777 the values for the enumeration constants may involve
4778 template parameters. And, no one should be interested
4779 in the enumeration constants for such a type. */
4780 t = make_node (ENUMERAL_TYPE);
4781 }
4782 else
4783 {
33848bb0 4784 t = make_aggr_type (TREE_CODE (template_type));
c8094d83 4785 CLASSTYPE_DECLARED_CLASS (t)
ed44da02 4786 = CLASSTYPE_DECLARED_CLASS (template_type);
ed44da02 4787 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
f668f160 4788 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
ae673f14
JM
4789
4790 /* A local class. Make sure the decl gets registered properly. */
4791 if (context == current_function_decl)
bd3d082e 4792 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
ed44da02
MM
4793 }
4794
ae673f14
JM
4795 /* If we called start_enum or pushtag above, this information
4796 will already be set up. */
ed44da02
MM
4797 if (!TYPE_NAME (t))
4798 {
4799 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
c8094d83 4800
9188c363 4801 type_decl = create_implicit_typedef (DECL_NAME (template), t);
ed44da02 4802 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9188c363 4803 TYPE_STUB_DECL (t) = type_decl;
c8094d83 4804 DECL_SOURCE_LOCATION (type_decl)
f31686a3 4805 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
ed44da02
MM
4806 }
4807 else
4808 type_decl = TYPE_NAME (t);
36a117a5 4809
cab7a9a3
KL
4810 TREE_PRIVATE (type_decl)
4811 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4812 TREE_PROTECTED (type_decl)
4813 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
12af7ba3
MM
4814 DECL_IN_SYSTEM_HEADER (type_decl)
4815 = DECL_IN_SYSTEM_HEADER (template);
8d039470
MS
4816 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4817 {
4818 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4819 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4820 }
cab7a9a3 4821
9fbf56f7
MM
4822 /* Set up the template information. We have to figure out which
4823 template is the immediate parent if this is a full
4824 instantiation. */
4825 if (parm_depth == 1 || is_partial_instantiation
4826 || !PRIMARY_TEMPLATE_P (template))
4827 /* This case is easy; there are no member templates involved. */
4828 found = template;
4829 else
4830 {
ab097535
NS
4831 /* This is a full instantiation of a member template. Look
4832 for a partial instantiation of which this is an instance. */
9fbf56f7
MM
4833
4834 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4835 found; found = TREE_CHAIN (found))
4836 {
4837 int success;
4838 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4839
4840 /* We only want partial instantiations, here, not
4841 specializations or full instantiations. */
370af2d5 4842 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
9fbf56f7
MM
4843 || !uses_template_parms (TREE_VALUE (found)))
4844 continue;
4845
4846 /* Temporarily reduce by one the number of levels in the
4847 ARGLIST and in FOUND so as to avoid comparing the
4848 last set of arguments. */
4849 TREE_VEC_LENGTH (arglist)--;
4850 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4851
4852 /* See if the arguments match. If they do, then TMPL is
4853 the partial instantiation we want. */
4854 success = comp_template_args (TREE_PURPOSE (found), arglist);
4855
4856 /* Restore the argument vectors to their full size. */
4857 TREE_VEC_LENGTH (arglist)++;
4858 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4859
4860 if (success)
4861 {
4862 found = tmpl;
4863 break;
4864 }
4865 }
4866
4867 if (!found)
ab097535
NS
4868 {
4869 /* There was no partial instantiation. This happens
0cbd7506
MS
4870 where C<T> is a member template of A<T> and it's used
4871 in something like
c8094d83 4872
0cbd7506
MS
4873 template <typename T> struct B { A<T>::C<int> m; };
4874 B<float>;
c8094d83 4875
0cbd7506
MS
4876 Create the partial instantiation.
4877 */
4878 TREE_VEC_LENGTH (arglist)--;
4879 found = tsubst (template, arglist, complain, NULL_TREE);
4880 TREE_VEC_LENGTH (arglist)++;
4881 }
9fbf56f7
MM
4882 }
4883
c8094d83
MS
4884 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4885 DECL_TEMPLATE_INSTANTIATIONS (template)
4886 = tree_cons (arglist, t,
dbfe2124
MM
4887 DECL_TEMPLATE_INSTANTIATIONS (template));
4888
c8094d83 4889 if (TREE_CODE (t) == ENUMERAL_TYPE
dbfe2124 4890 && !is_partial_instantiation)
61a127b3
MM
4891 /* Now that the type has been registered on the instantiations
4892 list, we set up the enumerators. Because the enumeration
4893 constants may involve the enumeration type itself, we make
4894 sure to register the type first, and then create the
4895 constants. That way, doing tsubst_expr for the enumeration
4896 constants won't result in recursive calls here; we'll find
4897 the instantiation and exit above. */
4898 tsubst_enum (template_type, t, arglist);
dbfe2124 4899
36a117a5
MM
4900 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4901 is set up. */
ed44da02
MM
4902 if (TREE_CODE (t) != ENUMERAL_TYPE)
4903 DECL_NAME (type_decl) = classtype_mangled_name (t);
7813d14c 4904 if (is_partial_instantiation)
077e7015
MM
4905 /* If the type makes use of template parameters, the
4906 code that generates debugging information will crash. */
4907 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
8d08fdba 4908
b9e75696
JM
4909 /* Possibly limit visibility based on template args. */
4910 TREE_PUBLIC (type_decl) = 1;
4911 determine_visibility (type_decl);
4912
fd295cb2 4913 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
36a117a5 4914 }
fd295cb2 4915 timevar_pop (TV_NAME_LOOKUP);
8d08fdba
MS
4916}
4917\f
c8094d83 4918struct pair_fn_data
8d08fdba 4919{
8dfaeb63
MM
4920 tree_fn_t fn;
4921 void *data;
0c58f841 4922 struct pointer_set_t *visited;
8dfaeb63
MM
4923};
4924
4925/* Called from for_each_template_parm via walk_tree. */
581d38d0 4926
8dfaeb63 4927static tree
350fae66 4928for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8dfaeb63
MM
4929{
4930 tree t = *tp;
4931 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4932 tree_fn_t fn = pfd->fn;
4933 void *data = pfd->data;
4f2c9d7e 4934
2f939d94 4935 if (TYPE_P (t)
4f2c9d7e 4936 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
8dfaeb63 4937 return error_mark_node;
581d38d0 4938
8d08fdba
MS
4939 switch (TREE_CODE (t))
4940 {
8d08fdba 4941 case RECORD_TYPE:
9076e292 4942 if (TYPE_PTRMEMFUNC_P (t))
8dfaeb63 4943 break;
ed44da02
MM
4944 /* Fall through. */
4945
8d08fdba 4946 case UNION_TYPE:
ed44da02 4947 case ENUMERAL_TYPE:
8dfaeb63
MM
4948 if (!TYPE_TEMPLATE_INFO (t))
4949 *walk_subtrees = 0;
4950 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4f2c9d7e 4951 fn, data, pfd->visited))
8dfaeb63
MM
4952 return error_mark_node;
4953 break;
4954
588c2d1c 4955 case METHOD_TYPE:
8dfaeb63
MM
4956 /* Since we're not going to walk subtrees, we have to do this
4957 explicitly here. */
4f2c9d7e
MM
4958 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4959 pfd->visited))
8dfaeb63 4960 return error_mark_node;
4890c2f4 4961 /* Fall through. */
588c2d1c
MM
4962
4963 case FUNCTION_TYPE:
8dfaeb63 4964 /* Check the return type. */
4f2c9d7e 4965 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
8dfaeb63
MM
4966 return error_mark_node;
4967
588c2d1c
MM
4968 /* Check the parameter types. Since default arguments are not
4969 instantiated until they are needed, the TYPE_ARG_TYPES may
4970 contain expressions that involve template parameters. But,
4971 no-one should be looking at them yet. And, once they're
4972 instantiated, they don't contain template parameters, so
4973 there's no point in looking at them then, either. */
4974 {
4975 tree parm;
4976
4977 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4f2c9d7e
MM
4978 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4979 pfd->visited))
8dfaeb63 4980 return error_mark_node;
5566b478 4981
8dfaeb63
MM
4982 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4983 want walk_tree walking into them itself. */
4984 *walk_subtrees = 0;
4985 }
4986 break;
3ac3d9ea 4987
a723baf1 4988 case TYPEOF_TYPE:
c8094d83 4989 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
a723baf1
MM
4990 pfd->visited))
4991 return error_mark_node;
4992 break;
4993
8d08fdba 4994 case FUNCTION_DECL:
5566b478 4995 case VAR_DECL:
5566b478 4996 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4f2c9d7e
MM
4997 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4998 pfd->visited))
8dfaeb63
MM
4999 return error_mark_node;
5000 /* Fall through. */
5001
8d08fdba 5002 case PARM_DECL:
a723baf1
MM
5003 case CONST_DECL:
5004 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
5005 && for_each_template_parm (DECL_INITIAL (t), fn, data,
5006 pfd->visited))
5007 return error_mark_node;
c8094d83 5008 if (DECL_CONTEXT (t)
4f2c9d7e
MM
5009 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
5010 pfd->visited))
8dfaeb63
MM
5011 return error_mark_node;
5012 break;
8d08fdba 5013
a1281f45 5014 case BOUND_TEMPLATE_TEMPLATE_PARM:
744fac59 5015 /* Record template parameters such as `T' inside `TT<T>'. */
4f2c9d7e 5016 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
8dfaeb63
MM
5017 return error_mark_node;
5018 /* Fall through. */
5019
a1281f45 5020 case TEMPLATE_TEMPLATE_PARM:
744fac59 5021 case TEMPLATE_TYPE_PARM:
f84b4be9 5022 case TEMPLATE_PARM_INDEX:
8dfaeb63
MM
5023 if (fn && (*fn)(t, data))
5024 return error_mark_node;
5025 else if (!fn)
5026 return error_mark_node;
5027 break;
5156628f 5028
8dfaeb63 5029 case TEMPLATE_DECL:
f4f206f4 5030 /* A template template parameter is encountered. */
8dfaeb63 5031 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4f2c9d7e 5032 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
8dfaeb63 5033 return error_mark_node;
db5ae43f 5034
8dfaeb63
MM
5035 /* Already substituted template template parameter */
5036 *walk_subtrees = 0;
5037 break;
b8865407 5038
4699c561 5039 case TYPENAME_TYPE:
c8094d83 5040 if (!fn
4f2c9d7e
MM
5041 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
5042 data, pfd->visited))
8dfaeb63
MM
5043 return error_mark_node;
5044 break;
4699c561 5045
8dfaeb63
MM
5046 case CONSTRUCTOR:
5047 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
5048 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4f2c9d7e
MM
5049 (TREE_TYPE (t)), fn, data,
5050 pfd->visited))
8dfaeb63
MM
5051 return error_mark_node;
5052 break;
c8094d83 5053
b8865407
MM
5054 case INDIRECT_REF:
5055 case COMPONENT_REF:
4699c561 5056 /* If there's no type, then this thing must be some expression
b8865407 5057 involving template parameters. */
4699c561 5058 if (!fn && !TREE_TYPE (t))
8dfaeb63
MM
5059 return error_mark_node;
5060 break;
b8865407 5061
42976354
BK
5062 case MODOP_EXPR:
5063 case CAST_EXPR:
5064 case REINTERPRET_CAST_EXPR:
5065 case CONST_CAST_EXPR:
5066 case STATIC_CAST_EXPR:
5067 case DYNAMIC_CAST_EXPR:
42976354
BK
5068 case ARROW_EXPR:
5069 case DOTSTAR_EXPR:
5070 case TYPEID_EXPR:
40242ccf 5071 case PSEUDO_DTOR_EXPR:
b8865407 5072 if (!fn)
8dfaeb63
MM
5073 return error_mark_node;
5074 break;
abff8e06 5075
bd9bb3d2
MM
5076 case BASELINK:
5077 /* If we do not handle this case specially, we end up walking
5078 the BINFO hierarchy, which is circular, and therefore
5079 confuses walk_tree. */
5080 *walk_subtrees = 0;
4f2c9d7e
MM
5081 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
5082 pfd->visited))
bd9bb3d2
MM
5083 return error_mark_node;
5084 break;
5085
8d08fdba 5086 default:
8dfaeb63 5087 break;
8d08fdba 5088 }
8dfaeb63
MM
5089
5090 /* We didn't find any template parameters we liked. */
5091 return NULL_TREE;
5092}
5093
c8094d83
MS
5094/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5095 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
a1281f45 5096 call FN with the parameter and the DATA.
838dfd8a 5097 If FN returns nonzero, the iteration is terminated, and
8dfaeb63 5098 for_each_template_parm returns 1. Otherwise, the iteration
838dfd8a 5099 continues. If FN never returns a nonzero value, the value
8dfaeb63
MM
5100 returned by for_each_template_parm is 0. If FN is NULL, it is
5101 considered to be the function which always returns 1. */
5102
5103static int
0c58f841
MA
5104for_each_template_parm (tree t, tree_fn_t fn, void* data,
5105 struct pointer_set_t *visited)
8dfaeb63
MM
5106{
5107 struct pair_fn_data pfd;
ad2ae3b2 5108 int result;
8dfaeb63
MM
5109
5110 /* Set up. */
5111 pfd.fn = fn;
5112 pfd.data = data;
5113
4890c2f4
MM
5114 /* Walk the tree. (Conceptually, we would like to walk without
5115 duplicates, but for_each_template_parm_r recursively calls
5116 for_each_template_parm, so we would need to reorganize a fair
4f2c9d7e
MM
5117 bit to use walk_tree_without_duplicates, so we keep our own
5118 visited list.) */
5119 if (visited)
5120 pfd.visited = visited;
5121 else
0c58f841 5122 pfd.visited = pointer_set_create ();
c8094d83
MS
5123 result = walk_tree (&t,
5124 for_each_template_parm_r,
ad2ae3b2 5125 &pfd,
c1e39976 5126 pfd.visited) != NULL_TREE;
ad2ae3b2
MM
5127
5128 /* Clean up. */
5129 if (!visited)
0c58f841
MA
5130 {
5131 pointer_set_destroy (pfd.visited);
5132 pfd.visited = 0;
5133 }
ad2ae3b2
MM
5134
5135 return result;
8d08fdba
MS
5136}
5137
d43f603d
KL
5138/* Returns true if T depends on any template parameter. */
5139
050367a3 5140int
3a978d72 5141uses_template_parms (tree t)
050367a3 5142{
c353b8e3
MM
5143 bool dependent_p;
5144 int saved_processing_template_decl;
5145
5146 saved_processing_template_decl = processing_template_decl;
5147 if (!saved_processing_template_decl)
5148 processing_template_decl = 1;
5149 if (TYPE_P (t))
5150 dependent_p = dependent_type_p (t);
5151 else if (TREE_CODE (t) == TREE_VEC)
5152 dependent_p = any_dependent_template_arguments_p (t);
5153 else if (TREE_CODE (t) == TREE_LIST)
5154 dependent_p = (uses_template_parms (TREE_VALUE (t))
5155 || uses_template_parms (TREE_CHAIN (t)));
3ce5fa4f
NS
5156 else if (TREE_CODE (t) == TYPE_DECL)
5157 dependent_p = dependent_type_p (TREE_TYPE (t));
c8094d83
MS
5158 else if (DECL_P (t)
5159 || EXPR_P (t)
c353b8e3
MM
5160 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5161 || TREE_CODE (t) == OVERLOAD
5162 || TREE_CODE (t) == BASELINK
b207d6e2 5163 || TREE_CODE (t) == IDENTIFIER_NODE
6615c446 5164 || CONSTANT_CLASS_P (t))
c353b8e3
MM
5165 dependent_p = (type_dependent_expression_p (t)
5166 || value_dependent_expression_p (t));
315fb5db
NS
5167 else
5168 {
5169 gcc_assert (t == error_mark_node);
5170 dependent_p = false;
5171 }
c8094d83 5172
c353b8e3
MM
5173 processing_template_decl = saved_processing_template_decl;
5174
5175 return dependent_p;
050367a3
MM
5176}
5177
d43f603d
KL
5178/* Returns true if T depends on any template parameter with level LEVEL. */
5179
5180int
5181uses_template_parms_level (tree t, int level)
5182{
5183 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5184}
5185
27fafc8d 5186static int tinst_depth;
e9f32eb5 5187extern int max_tinst_depth;
5566b478 5188#ifdef GATHER_STATISTICS
27fafc8d 5189int depth_reached;
5566b478 5190#endif
8dfaeb63
MM
5191static int tinst_level_tick;
5192static int last_template_error_tick;
8d08fdba 5193
3ae18eaf
JM
5194/* We're starting to instantiate D; record the template instantiation context
5195 for diagnostics and to restore it later. */
5196
aa9d8196 5197static int
3a978d72 5198push_tinst_level (tree d)
8d08fdba 5199{
3ae18eaf 5200 tree new;
8d08fdba 5201
7215f9a0
MS
5202 if (tinst_depth >= max_tinst_depth)
5203 {
8adf5b5e
JM
5204 /* If the instantiation in question still has unbound template parms,
5205 we don't really care if we can't instantiate it, so just return.
0cbd7506 5206 This happens with base instantiation for implicit `typename'. */
8adf5b5e
JM
5207 if (uses_template_parms (d))
5208 return 0;
5209
1139b3d8 5210 last_template_error_tick = tinst_level_tick;
0f51ccfc 5211 error ("template instantiation depth exceeds maximum of %d (use "
0cbd7506 5212 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
b4f4233d 5213 max_tinst_depth, d);
5566b478 5214
cb753e49 5215 print_instantiation_context ();
5566b478 5216
7215f9a0
MS
5217 return 0;
5218 }
5219
12af7ba3
MM
5220 new = make_node (TINST_LEVEL);
5221 TINST_DECL (new) = d;
5222 TINST_LOCATION (new) = input_location;
5223 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
3ae18eaf 5224 TREE_CHAIN (new) = current_tinst_level;
8d08fdba 5225 current_tinst_level = new;
5566b478 5226
7215f9a0 5227 ++tinst_depth;
5566b478
MS
5228#ifdef GATHER_STATISTICS
5229 if (tinst_depth > depth_reached)
5230 depth_reached = tinst_depth;
5231#endif
5232
27fafc8d 5233 ++tinst_level_tick;
7215f9a0 5234 return 1;
8d08fdba
MS
5235}
5236
3ae18eaf
JM
5237/* We're done instantiating this template; return to the instantiation
5238 context. */
5239
aa9d8196 5240static void
3a978d72 5241pop_tinst_level (void)
8d08fdba 5242{
3ae18eaf 5243 tree old = current_tinst_level;
8d08fdba 5244
ae58fa02
MM
5245 /* Restore the filename and line number stashed away when we started
5246 this instantiation. */
406d77a4 5247 input_location = TINST_LOCATION (old);
12af7ba3 5248 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
3ae18eaf 5249 current_tinst_level = TREE_CHAIN (old);
7215f9a0 5250 --tinst_depth;
27fafc8d 5251 ++tinst_level_tick;
8d08fdba
MS
5252}
5253
3ae18eaf
JM
5254/* We're instantiating a deferred template; restore the template
5255 instantiation context in which the instantiation was requested, which
5256 is one step out from LEVEL. */
5257
5258static void
3a978d72 5259reopen_tinst_level (tree level)
3ae18eaf
JM
5260{
5261 tree t;
5262
5263 tinst_depth = 0;
5264 for (t = level; t; t = TREE_CHAIN (t))
5265 ++tinst_depth;
5266
5267 current_tinst_level = level;
5268 pop_tinst_level ();
5269}
5270
f84b4be9
JM
5271/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5272 vector of template arguments, as for tsubst.
5273
dc957d14 5274 Returns an appropriate tsubst'd friend declaration. */
f84b4be9
JM
5275
5276static tree
3a978d72 5277tsubst_friend_function (tree decl, tree args)
f84b4be9
JM
5278{
5279 tree new_friend;
27fafc8d 5280
c8094d83 5281 if (TREE_CODE (decl) == FUNCTION_DECL
f84b4be9
JM
5282 && DECL_TEMPLATE_INSTANTIATION (decl)
5283 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5284 /* This was a friend declared with an explicit template
5285 argument list, e.g.:
c8094d83 5286
f84b4be9 5287 friend void f<>(T);
c8094d83 5288
f84b4be9
JM
5289 to indicate that f was a template instantiation, not a new
5290 function declaration. Now, we have to figure out what
5291 instantiation of what template. */
5292 {
76e57b45 5293 tree template_id, arglist, fns;
f84b4be9
JM
5294 tree new_args;
5295 tree tmpl;
ed2fa432 5296 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
c8094d83 5297
76e57b45 5298 /* Friend functions are looked up in the containing namespace scope.
0cbd7506
MS
5299 We must enter that scope, to avoid finding member functions of the
5300 current cless with same name. */
76e57b45
NS
5301 push_nested_namespace (ns);
5302 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
015c2c66
MM
5303 tf_warning_or_error, NULL_TREE,
5304 /*integral_constant_expression_p=*/false);
76e57b45
NS
5305 pop_nested_namespace (ns);
5306 arglist = tsubst (DECL_TI_ARGS (decl), args,
23fca1f5 5307 tf_warning_or_error, NULL_TREE);
76e57b45 5308 template_id = lookup_template_function (fns, arglist);
c8094d83 5309
23fca1f5 5310 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
36a117a5 5311 tmpl = determine_specialization (template_id, new_friend,
c8094d83 5312 &new_args,
5fe7b654 5313 /*need_member_template=*/0,
29a1da1c
MM
5314 TREE_VEC_LENGTH (args),
5315 tsk_none);
6e432b31 5316 return instantiate_template (tmpl, new_args, tf_error);
f84b4be9 5317 }
36a117a5 5318
23fca1f5 5319 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
c8094d83 5320
36a117a5 5321 /* The NEW_FRIEND will look like an instantiation, to the
f84b4be9
JM
5322 compiler, but is not an instantiation from the point of view of
5323 the language. For example, we might have had:
c8094d83 5324
f84b4be9
JM
5325 template <class T> struct S {
5326 template <class U> friend void f(T, U);
5327 };
c8094d83 5328
f84b4be9
JM
5329 Then, in S<int>, template <class U> void f(int, U) is not an
5330 instantiation of anything. */
ac2b3222
AP
5331 if (new_friend == error_mark_node)
5332 return error_mark_node;
c8094d83 5333
f84b4be9
JM
5334 DECL_USE_TEMPLATE (new_friend) = 0;
5335 if (TREE_CODE (decl) == TEMPLATE_DECL)
655dc6ee
JM
5336 {
5337 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5338 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5339 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5340 }
36a117a5 5341
92643fea
MM
5342 /* The mangled name for the NEW_FRIEND is incorrect. The function
5343 is not a template instantiation and should not be mangled like
5344 one. Therefore, we forget the mangling here; we'll recompute it
5345 later if we need it. */
36a117a5
MM
5346 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5347 {
19e7881c 5348 SET_DECL_RTL (new_friend, NULL_RTX);
92643fea 5349 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
36a117a5 5350 }
c8094d83 5351
6eb3bb27 5352 if (DECL_NAMESPACE_SCOPE_P (new_friend))
f84b4be9 5353 {
36a117a5 5354 tree old_decl;
fbf1c34b
MM
5355 tree new_friend_template_info;
5356 tree new_friend_result_template_info;
92da7074 5357 tree ns;
fbf1c34b
MM
5358 int new_friend_is_defn;
5359
5360 /* We must save some information from NEW_FRIEND before calling
5361 duplicate decls since that function will free NEW_FRIEND if
5362 possible. */
5363 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5bd61841 5364 new_friend_is_defn =
c8094d83 5365 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5bd61841
MM
5366 (template_for_substitution (new_friend)))
5367 != NULL_TREE);
f84b4be9 5368 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
fbf1c34b
MM
5369 {
5370 /* This declaration is a `primary' template. */
5371 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
c8094d83 5372
fbf1c34b 5373 new_friend_result_template_info
17aec3eb 5374 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
fbf1c34b
MM
5375 }
5376 else
5bd61841 5377 new_friend_result_template_info = NULL_TREE;
36a117a5 5378
b01e6d2b
JM
5379 /* Make the init_value nonzero so pushdecl knows this is a defn. */
5380 if (new_friend_is_defn)
5381 DECL_INITIAL (new_friend) = error_mark_node;
5382
1c227897
MM
5383 /* Inside pushdecl_namespace_level, we will push into the
5384 current namespace. However, the friend function should go
c6002625 5385 into the namespace of the template. */
92da7074
ML
5386 ns = decl_namespace_context (new_friend);
5387 push_nested_namespace (ns);
d63d5d0c 5388 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
92da7074 5389 pop_nested_namespace (ns);
36a117a5 5390
0014c247
VR
5391 if (old_decl == error_mark_node)
5392 return error_mark_node;
5393
36a117a5
MM
5394 if (old_decl != new_friend)
5395 {
5396 /* This new friend declaration matched an existing
5397 declaration. For example, given:
5398
5399 template <class T> void f(T);
c8094d83
MS
5400 template <class U> class C {
5401 template <class T> friend void f(T) {}
36a117a5
MM
5402 };
5403
5404 the friend declaration actually provides the definition
5405 of `f', once C has been instantiated for some type. So,
5406 old_decl will be the out-of-class template declaration,
5407 while new_friend is the in-class definition.
5408
5409 But, if `f' was called before this point, the
5410 instantiation of `f' will have DECL_TI_ARGS corresponding
5411 to `T' but not to `U', references to which might appear
5412 in the definition of `f'. Previously, the most general
5413 template for an instantiation of `f' was the out-of-class
5414 version; now it is the in-class version. Therefore, we
5415 run through all specialization of `f', adding to their
5416 DECL_TI_ARGS appropriately. In particular, they need a
5417 new set of outer arguments, corresponding to the
c8094d83 5418 arguments for this class instantiation.
36a117a5
MM
5419
5420 The same situation can arise with something like this:
5421
5422 friend void f(int);
c8094d83 5423 template <class T> class C {
0cbd7506
MS
5424 friend void f(T) {}
5425 };
36a117a5
MM
5426
5427 when `C<int>' is instantiated. Now, `f(int)' is defined
5428 in the class. */
5429
fbf1c34b
MM
5430 if (!new_friend_is_defn)
5431 /* On the other hand, if the in-class declaration does
5432 *not* provide a definition, then we don't want to alter
5433 existing definitions. We can just leave everything
5434 alone. */
36a117a5 5435 ;
fbf1c34b 5436 else
36a117a5 5437 {
fbf1c34b
MM
5438 /* Overwrite whatever template info was there before, if
5439 any, with the new template information pertaining to
5440 the declaration. */
5441 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5442
5443 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8d83f792
MM
5444 reregister_specialization (new_friend,
5445 most_general_template (old_decl),
5446 old_decl);
c8094d83 5447 else
36a117a5 5448 {
fbf1c34b
MM
5449 tree t;
5450 tree new_friend_args;
5451
c8094d83 5452 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
fbf1c34b 5453 = new_friend_result_template_info;
c8094d83 5454
fbf1c34b 5455 new_friend_args = TI_ARGS (new_friend_template_info);
c8094d83 5456 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
fbf1c34b
MM
5457 t != NULL_TREE;
5458 t = TREE_CHAIN (t))
5459 {
5460 tree spec = TREE_VALUE (t);
c8094d83
MS
5461
5462 DECL_TI_ARGS (spec)
fbf1c34b
MM
5463 = add_outermost_template_args (new_friend_args,
5464 DECL_TI_ARGS (spec));
fbf1c34b
MM
5465 }
5466
5467 /* Now, since specializations are always supposed to
5468 hang off of the most general template, we must move
5469 them. */
5470 t = most_general_template (old_decl);
5471 if (t != old_decl)
5472 {
5473 DECL_TEMPLATE_SPECIALIZATIONS (t)
5474 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5475 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5476 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5477 }
36a117a5
MM
5478 }
5479 }
5480
5481 /* The information from NEW_FRIEND has been merged into OLD_DECL
5482 by duplicate_decls. */
5483 new_friend = old_decl;
5484 }
f84b4be9 5485 }
6e432b31 5486 else
f84b4be9 5487 {
6e432b31
KL
5488 tree context = DECL_CONTEXT (new_friend);
5489 bool dependent_p;
5490
5491 /* In the code
5492 template <class T> class C {
5493 template <class U> friend void C1<U>::f (); // case 1
5494 friend void C2<T>::f (); // case 2
5495 };
5496 we only need to make sure CONTEXT is a complete type for
5497 case 2. To distinguish between the two cases, we note that
5498 CONTEXT of case 1 remains dependent type after tsubst while
5499 this isn't true for case 2. */
5500 ++processing_template_decl;
5501 dependent_p = dependent_type_p (context);
5502 --processing_template_decl;
5503
5504 if (!dependent_p
5505 && !complete_type_or_else (context, NULL_TREE))
5506 return error_mark_node;
5507
5508 if (COMPLETE_TYPE_P (context))
5509 {
5510 /* Check to see that the declaration is really present, and,
5511 possibly obtain an improved declaration. */
5512 tree fn = check_classfn (context,
5513 new_friend, NULL_TREE);
5514
5515 if (fn)
5516 new_friend = fn;
5517 }
f84b4be9
JM
5518 }
5519
5520 return new_friend;
5521}
5522
1aed5355
MM
5523/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5524 template arguments, as for tsubst.
6757edfe 5525
dc957d14 5526 Returns an appropriate tsubst'd friend type or error_mark_node on
4b054b80 5527 failure. */
6757edfe
MM
5528
5529static tree
3a978d72 5530tsubst_friend_class (tree friend_tmpl, tree args)
6757edfe 5531{
1aed5355 5532 tree friend_type;
25aab5d0 5533 tree tmpl;
3e0ec82f 5534 tree context;
6757edfe 5535
3e0ec82f
MM
5536 context = DECL_CONTEXT (friend_tmpl);
5537
5538 if (context)
77adef84 5539 {
3e0ec82f
MM
5540 if (TREE_CODE (context) == NAMESPACE_DECL)
5541 push_nested_namespace (context);
5542 else
c8094d83 5543 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
3e0ec82f 5544 }
25aab5d0 5545
105d72c5
MM
5546 /* Look for a class template declaration. We look for hidden names
5547 because two friend declarations of the same template are the
5548 same. For example, in:
5549
5550 struct A {
5551 template <typename> friend class F;
5552 };
5553 template <typename> struct B {
5554 template <typename> friend class F;
5555 };
5556
5557 both F templates are the same. */
5558 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
5559 /*block_p=*/true, 0,
5560 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
25aab5d0 5561
3e0ec82f
MM
5562 /* But, if we don't find one, it might be because we're in a
5563 situation like this:
77adef84 5564
3e0ec82f
MM
5565 template <class T>
5566 struct S {
5567 template <class U>
5568 friend struct S;
5569 };
5570
5571 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5572 for `S<int>', not the TEMPLATE_DECL. */
5573 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5574 {
10e6657a 5575 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
3e0ec82f 5576 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
25aab5d0 5577 }
6757edfe 5578
25aab5d0 5579 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6757edfe
MM
5580 {
5581 /* The friend template has already been declared. Just
36a117a5
MM
5582 check to see that the declarations match, and install any new
5583 default parameters. We must tsubst the default parameters,
5584 of course. We only need the innermost template parameters
5585 because that is all that redeclare_class_template will look
5586 at. */
3e0ec82f
MM
5587 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5588 > TMPL_ARGS_DEPTH (args))
5589 {
5590 tree parms;
5591 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
23fca1f5 5592 args, tf_warning_or_error);
3e0ec82f
MM
5593 redeclare_class_template (TREE_TYPE (tmpl), parms);
5594 }
5595
6757edfe
MM
5596 friend_type = TREE_TYPE (tmpl);
5597 }
5598 else
5599 {
5600 /* The friend template has not already been declared. In this
5601 case, the instantiation of the template class will cause the
5602 injection of this template into the global scope. */
23fca1f5 5603 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
dca56f77
VR
5604 if (tmpl == error_mark_node)
5605 return error_mark_node;
6757edfe
MM
5606
5607 /* The new TMPL is not an instantiation of anything, so we
0cbd7506 5608 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6757edfe
MM
5609 the new type because that is supposed to be the corresponding
5610 template decl, i.e., TMPL. */
5611 DECL_USE_TEMPLATE (tmpl) = 0;
5612 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5613 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
68c78847
KL
5614 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5615 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6757edfe
MM
5616
5617 /* Inject this template into the global scope. */
d63d5d0c 5618 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6757edfe
MM
5619 }
5620
c8094d83 5621 if (context)
3e0ec82f
MM
5622 {
5623 if (TREE_CODE (context) == NAMESPACE_DECL)
5624 pop_nested_namespace (context);
5625 else
5626 pop_nested_class ();
5627 }
5628
6757edfe
MM
5629 return friend_type;
5630}
f84b4be9 5631
17f29637
KL
5632/* Returns zero if TYPE cannot be completed later due to circularity.
5633 Otherwise returns one. */
5634
d5372501 5635static int
3a978d72 5636can_complete_type_without_circularity (tree type)
17f29637
KL
5637{
5638 if (type == NULL_TREE || type == error_mark_node)
5639 return 0;
5640 else if (COMPLETE_TYPE_P (type))
5641 return 1;
5642 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5643 return can_complete_type_without_circularity (TREE_TYPE (type));
8c6ab2db
NS
5644 else if (CLASS_TYPE_P (type)
5645 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
17f29637
KL
5646 return 0;
5647 else
5648 return 1;
5649}
5650
8d08fdba 5651tree
3a978d72 5652instantiate_class_template (tree type)
8d08fdba 5653{
7088fca9 5654 tree template, args, pattern, t, member;
36a117a5 5655 tree typedecl;
dbbf88d1 5656 tree pbinfo;
cad7e87b 5657 tree base_list;
c8094d83 5658
5566b478 5659 if (type == error_mark_node)
8d08fdba
MS
5660 return error_mark_node;
5661
c8094d83 5662 if (TYPE_BEING_DEFINED (type)
ca099ac8
MM
5663 || COMPLETE_TYPE_P (type)
5664 || dependent_type_p (type))
5566b478
MS
5665 return type;
5666
6bdb985f 5667 /* Figure out which template is being instantiated. */
36a117a5 5668 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
50bc768d 5669 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
73aad9b9 5670
4c571114
MM
5671 /* Determine what specialization of the original template to
5672 instantiate. */
916b63c3 5673 t = most_specialized_class (type, template);
8fbc5ae7 5674 if (t == error_mark_node)
73aad9b9 5675 {
8fbc5ae7
MM
5676 TYPE_BEING_DEFINED (type) = 1;
5677 return error_mark_node;
73aad9b9 5678 }
916b63c3
MM
5679 else if (t)
5680 {
5681 /* This TYPE is actually an instantiation of a partial
5682 specialization. We replace the innermost set of ARGS with
5683 the arguments appropriate for substitution. For example,
5684 given:
3db45ab5
MS
5685
5686 template <class T> struct S {};
916b63c3 5687 template <class T> struct S<T*> {};
6bdb985f 5688
916b63c3 5689 and supposing that we are instantiating S<int*>, ARGS will
3db45ab5 5690 presently be {int*} -- but we need {int}. */
916b63c3
MM
5691 pattern = TREE_TYPE (t);
5692 args = TREE_PURPOSE (t);
5693 }
73aad9b9 5694 else
916b63c3
MM
5695 {
5696 pattern = TREE_TYPE (template);
5697 args = CLASSTYPE_TI_ARGS (type);
5698 }
5566b478 5699
4c571114
MM
5700 /* If the template we're instantiating is incomplete, then clearly
5701 there's nothing we can do. */
d0f062fb 5702 if (!COMPLETE_TYPE_P (pattern))
f31c0a32 5703 return type;
5566b478 5704
4c571114
MM
5705 /* If we've recursively instantiated too many templates, stop. */
5706 if (! push_tinst_level (type))
f31c0a32 5707 return type;
4c571114
MM
5708
5709 /* Now we're really doing the instantiation. Mark the type as in
5710 the process of being defined. */
5711 TYPE_BEING_DEFINED (type) = 1;
5712
78757caa
KL
5713 /* We may be in the middle of deferred access check. Disable
5714 it now. */
5715 push_deferring_access_checks (dk_no_deferred);
5716
c353b8e3 5717 push_to_top_level ();
4c571114 5718
7813d14c 5719 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8d08fdba 5720
68ea098a
NS
5721 /* Set the input location to the template definition. This is needed
5722 if tsubsting causes an error. */
12af7ba3
MM
5723 typedecl = TYPE_MAIN_DECL (type);
5724 input_location = DECL_SOURCE_LOCATION (typedecl);
5725 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
68ea098a 5726
f7da6097 5727 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
834c6dff
MM
5728 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5729 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
f7da6097 5730 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
f7da6097
MS
5731 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5732 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
f7da6097
MS
5733 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5734 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
5735 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5736 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
f7da6097
MS
5737 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5738 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
11cf4d18 5739 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
eff71ab0 5740 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6bdb8141
JM
5741 if (ANON_AGGR_TYPE_P (pattern))
5742 SET_ANON_AGGR_TYPE_P (type);
8d039470
MS
5743 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5744 {
5745 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5746 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5747 }
f7da6097 5748
dbbf88d1 5749 pbinfo = TYPE_BINFO (pattern);
1456deaf 5750
315fb5db
NS
5751 /* We should never instantiate a nested class before its enclosing
5752 class; we need to look up the nested class by name before we can
5753 instantiate it, and that lookup should instantiate the enclosing
5754 class. */
5755 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5756 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5757 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
1456deaf 5758
cad7e87b 5759 base_list = NULL_TREE;
fa743e8c 5760 if (BINFO_N_BASE_BINFOS (pbinfo))
3fd71a52 5761 {
fa743e8c 5762 tree pbase_binfo;
a2507277 5763 tree context = TYPE_CONTEXT (type);
4514aa8c 5764 tree pushed_scope;
3fd71a52 5765 int i;
5566b478 5766
a2507277
NS
5767 /* We must enter the scope containing the type, as that is where
5768 the accessibility of types named in dependent bases are
5769 looked up from. */
4514aa8c 5770 pushed_scope = push_scope (context ? context : global_namespace);
c8094d83 5771
3fd71a52
MM
5772 /* Substitute into each of the bases to determine the actual
5773 basetypes. */
fa743e8c 5774 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
3fd71a52
MM
5775 {
5776 tree base;
fa743e8c 5777 tree access = BINFO_BASE_ACCESS (pbinfo, i);
711734a9 5778
dc957d14 5779 /* Substitute to figure out the base class. */
fa743e8c 5780 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
3fd71a52
MM
5781 if (base == error_mark_node)
5782 continue;
c8094d83 5783
3fd71a52 5784 base_list = tree_cons (access, base, base_list);
fa743e8c 5785 if (BINFO_VIRTUAL_P (pbase_binfo))
809e3e7f 5786 TREE_TYPE (base_list) = integer_type_node;
3fd71a52 5787 }
dfbcd65a 5788
3fd71a52
MM
5789 /* The list is now in reverse order; correct that. */
5790 base_list = nreverse (base_list);
5791
4514aa8c
NS
5792 if (pushed_scope)
5793 pop_scope (pushed_scope);
3fd71a52 5794 }
cad7e87b
NS
5795 /* Now call xref_basetypes to set up all the base-class
5796 information. */
5797 xref_basetypes (type, base_list);
5798
5566b478 5799
b74a0560
MM
5800 /* Now that our base classes are set up, enter the scope of the
5801 class, so that name lookups into base classes, etc. will work
dc957d14 5802 correctly. This is precisely analogous to what we do in
b74a0560
MM
5803 begin_class_definition when defining an ordinary non-template
5804 class. */
29370796 5805 pushclass (type);
b74a0560 5806
7088fca9 5807 /* Now members are processed in the order of declaration. */
8c6ab2db
NS
5808 for (member = CLASSTYPE_DECL_LIST (pattern);
5809 member; member = TREE_CHAIN (member))
8d08fdba 5810 {
7088fca9 5811 tree t = TREE_VALUE (member);
5566b478 5812
7088fca9 5813 if (TREE_PURPOSE (member))
ed44da02 5814 {
7088fca9
KL
5815 if (TYPE_P (t))
5816 {
5e0c54e5 5817 /* Build new CLASSTYPE_NESTED_UTDS. */
8d08fdba 5818
7088fca9 5819 tree newtag;
883a2bff
MM
5820 bool class_template_p;
5821
2678bae8
VR
5822 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5823 && TYPE_LANG_SPECIFIC (t)
5824 && CLASSTYPE_IS_TEMPLATE (t));
883a2bff 5825 /* If the member is a class template, then -- even after
6c745393 5826 substitution -- there may be dependent types in the
883a2bff
MM
5827 template argument list for the class. We increment
5828 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5829 that function will assume that no types are dependent
5830 when outside of a template. */
5831 if (class_template_p)
5832 ++processing_template_decl;
2678bae8 5833 newtag = tsubst (t, args, tf_error, NULL_TREE);
883a2bff
MM
5834 if (class_template_p)
5835 --processing_template_decl;
2620d095
KL
5836 if (newtag == error_mark_node)
5837 continue;
5838
7088fca9
KL
5839 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5840 {
2678bae8
VR
5841 tree name = TYPE_IDENTIFIER (t);
5842
883a2bff 5843 if (class_template_p)
7088fca9
KL
5844 /* Unfortunately, lookup_template_class sets
5845 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
3afd2e20
NS
5846 instantiation (i.e., for the type of a member
5847 template class nested within a template class.)
5848 This behavior is required for
5849 maybe_process_partial_specialization to work
5850 correctly, but is not accurate in this case;
5851 the TAG is not an instantiation of anything.
5852 (The corresponding TEMPLATE_DECL is an
5853 instantiation, but the TYPE is not.) */
7088fca9
KL
5854 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5855
5856 /* Now, we call pushtag to put this NEWTAG into the scope of
5857 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5858 pushtag calling push_template_decl. We don't have to do
5859 this for enums because it will already have been done in
5860 tsubst_enum. */
5861 if (name)
5862 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
bd3d082e 5863 pushtag (name, newtag, /*tag_scope=*/ts_current);
7088fca9
KL
5864 }
5865 }
c8094d83 5866 else if (TREE_CODE (t) == FUNCTION_DECL
7088fca9
KL
5867 || DECL_FUNCTION_TEMPLATE_P (t))
5868 {
5869 /* Build new TYPE_METHODS. */
fecafe5e 5870 tree r;
c8094d83 5871
fecafe5e 5872 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5873 ++processing_template_decl;
fecafe5e
NS
5874 r = tsubst (t, args, tf_error, NULL_TREE);
5875 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5876 --processing_template_decl;
7088fca9 5877 set_current_access_from_decl (r);
7088fca9
KL
5878 finish_member_declaration (r);
5879 }
5880 else
5881 {
5882 /* Build new TYPE_FIELDS. */
fa8d6e85 5883
7088fca9
KL
5884 if (TREE_CODE (t) != CONST_DECL)
5885 {
5886 tree r;
fa8d6e85 5887
d479d37f
NS
5888 /* The the file and line for this declaration, to
5889 assist in error message reporting. Since we
5890 called push_tinst_level above, we don't need to
5891 restore these. */
f31686a3 5892 input_location = DECL_SOURCE_LOCATION (t);
fa8d6e85 5893
fb5ce3c9 5894 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5895 ++processing_template_decl;
23fca1f5 5896 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
fb5ce3c9 5897 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5898 --processing_template_decl;
7088fca9
KL
5899 if (TREE_CODE (r) == VAR_DECL)
5900 {
b794e321
MM
5901 /* In [temp.inst]:
5902
5903 [t]he initialization (and any associated
5904 side-effects) of a static data member does
5905 not occur unless the static data member is
5906 itself used in a way that requires the
5907 definition of the static data member to
3db45ab5 5908 exist.
b794e321
MM
5909
5910 Therefore, we do not substitute into the
3db45ab5
MS
5911 initialized for the static data member here. */
5912 finish_static_data_member_decl
5913 (r,
5914 /*init=*/NULL_TREE,
d174af6c 5915 /*init_const_expr_p=*/false,
3db45ab5 5916 /*asmspec_tree=*/NULL_TREE,
b794e321 5917 /*flags=*/0);
7088fca9
KL
5918 if (DECL_INITIALIZED_IN_CLASS_P (r))
5919 check_static_variable_definition (r, TREE_TYPE (r));
5920 }
5921 else if (TREE_CODE (r) == FIELD_DECL)
5922 {
5923 /* Determine whether R has a valid type and can be
5924 completed later. If R is invalid, then it is
5925 replaced by error_mark_node so that it will not be
5926 added to TYPE_FIELDS. */
5927 tree rtype = TREE_TYPE (r);
5928 if (can_complete_type_without_circularity (rtype))
5929 complete_type (rtype);
5930
5931 if (!COMPLETE_TYPE_P (rtype))
5932 {
5933 cxx_incomplete_type_error (r, rtype);
0cbd7506 5934 r = error_mark_node;
7088fca9
KL
5935 }
5936 }
5566b478 5937
7088fca9
KL
5938 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5939 such a thing will already have been added to the field
5940 list by tsubst_enum in finish_member_declaration in the
5e0c54e5 5941 CLASSTYPE_NESTED_UTDS case above. */
7088fca9
KL
5942 if (!(TREE_CODE (r) == TYPE_DECL
5943 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
43b26a01 5944 && DECL_ARTIFICIAL (r)))
7088fca9
KL
5945 {
5946 set_current_access_from_decl (r);
5947 finish_member_declaration (r);
5948 }
0cbd7506 5949 }
7088fca9 5950 }
61fc8c9e 5951 }
7088fca9
KL
5952 else
5953 {
5954 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5955 {
5956 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5957
5958 tree friend_type = t;
b939a023 5959 bool adjust_processing_template_decl = false;
1aed5355 5960
7088fca9 5961 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
b939a023 5962 {
5a24482e 5963 /* template <class T> friend class C; */
b939a023 5964 friend_type = tsubst_friend_class (friend_type, args);
0cbd7506 5965 adjust_processing_template_decl = true;
b939a023
KL
5966 }
5967 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5968 {
5a24482e 5969 /* template <class T> friend class C::D; */
b939a023 5970 friend_type = tsubst (friend_type, args,
23fca1f5 5971 tf_warning_or_error, NULL_TREE);
b939a023
KL
5972 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5973 friend_type = TREE_TYPE (friend_type);
0cbd7506 5974 adjust_processing_template_decl = true;
b939a023
KL
5975 }
5976 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5977 {
5a24482e
KL
5978 /* This could be either
5979
5980 friend class T::C;
5981
5982 when dependent_type_p is false or
5983
5984 template <class U> friend class T::C;
5985
5986 otherwise. */
b939a023 5987 friend_type = tsubst (friend_type, args,
23fca1f5 5988 tf_warning_or_error, NULL_TREE);
b939a023
KL
5989 /* Bump processing_template_decl for correct
5990 dependent_type_p calculation. */
5991 ++processing_template_decl;
5992 if (dependent_type_p (friend_type))
5993 adjust_processing_template_decl = true;
5994 --processing_template_decl;
5995 }
5a24482e
KL
5996 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
5997 && hidden_name_p (TYPE_NAME (friend_type)))
7088fca9 5998 {
5a24482e
KL
5999 /* friend class C;
6000
6001 where C hasn't been declared yet. Let's lookup name
6002 from namespace scope directly, bypassing any name that
6003 come from dependent base class. */
7088fca9
KL
6004 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
6005
6006 /* The call to xref_tag_from_type does injection for friend
6007 classes. */
6008 push_nested_namespace (ns);
c8094d83
MS
6009 friend_type =
6010 xref_tag_from_type (friend_type, NULL_TREE,
5a24482e 6011 /*tag_scope=*/ts_current);
7088fca9
KL
6012 pop_nested_namespace (ns);
6013 }
5a24482e
KL
6014 else if (uses_template_parms (friend_type))
6015 /* friend class C<T>; */
6016 friend_type = tsubst (friend_type, args,
23fca1f5 6017 tf_warning_or_error, NULL_TREE);
5a24482e
KL
6018 /* Otherwise it's
6019
6020 friend class C;
6021
6022 where C is already declared or
6023
6024 friend class C<int>;
6025
0cbd7506 6026 We don't have to do anything in these cases. */
1aed5355 6027
b939a023 6028 if (adjust_processing_template_decl)
7088fca9
KL
6029 /* Trick make_friend_class into realizing that the friend
6030 we're adding is a template, not an ordinary class. It's
6031 important that we use make_friend_class since it will
6032 perform some error-checking and output cross-reference
6033 information. */
6034 ++processing_template_decl;
fc378698 6035
b939a023 6036 if (friend_type != error_mark_node)
0cbd7506 6037 make_friend_class (type, friend_type, /*complain=*/false);
fc378698 6038
b939a023 6039 if (adjust_processing_template_decl)
7088fca9
KL
6040 --processing_template_decl;
6041 }
6042 else
9579624e
KL
6043 {
6044 /* Build new DECL_FRIENDLIST. */
6045 tree r;
6046
6e432b31
KL
6047 /* The the file and line for this declaration, to
6048 assist in error message reporting. Since we
6049 called push_tinst_level above, we don't need to
6050 restore these. */
6051 input_location = DECL_SOURCE_LOCATION (t);
6052
9579624e 6053 if (TREE_CODE (t) == TEMPLATE_DECL)
cad7e87b
NS
6054 {
6055 ++processing_template_decl;
6056 push_deferring_access_checks (dk_no_check);
6057 }
c8094d83 6058
9579624e 6059 r = tsubst_friend_function (t, args);
9579624e 6060 add_friend (type, r, /*complain=*/false);
cad7e87b
NS
6061 if (TREE_CODE (t) == TEMPLATE_DECL)
6062 {
6063 pop_deferring_access_checks ();
6064 --processing_template_decl;
6065 }
9579624e 6066 }
7088fca9
KL
6067 }
6068 }
5566b478 6069
61a127b3
MM
6070 /* Set the file and line number information to whatever is given for
6071 the class itself. This puts error messages involving generated
6072 implicit functions at a predictable point, and the same point
6073 that would be used for non-template classes. */
f31686a3 6074 input_location = DECL_SOURCE_LOCATION (typedecl);
6de9cd9a 6075
61a127b3 6076 unreverse_member_declarations (type);
9f33663b 6077 finish_struct_1 (type);
5524676d 6078 TYPE_BEING_DEFINED (type) = 0;
8d08fdba 6079
9188c363
MM
6080 /* Now that the class is complete, instantiate default arguments for
6081 any member functions. We don't do this earlier because the
6082 default arguments may reference members of the class. */
6083 if (!PRIMARY_TEMPLATE_P (template))
6084 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
c8094d83 6085 if (TREE_CODE (t) == FUNCTION_DECL
dc957d14 6086 /* Implicitly generated member functions will not have template
9188c363
MM
6087 information; they are not instantiations, but instead are
6088 created "fresh" for each instantiation. */
6089 && DECL_TEMPLATE_INFO (t))
6090 tsubst_default_arguments (t);
6091
b74a0560 6092 popclass ();
5566b478 6093 pop_from_top_level ();
78757caa 6094 pop_deferring_access_checks ();
5566b478
MS
6095 pop_tinst_level ();
6096
4684cd27
MM
6097 /* The vtable for a template class can be emitted in any translation
6098 unit in which the class is instantiated. When there is no key
6099 method, however, finish_struct_1 will already have added TYPE to
6100 the keyed_classes list. */
6101 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9aad8f83
MA
6102 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6103
5566b478 6104 return type;
8d08fdba
MS
6105}
6106
00d3396f 6107static tree
a91db711 6108tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
00d3396f 6109{
a91db711 6110 tree r;
c8094d83 6111
a91db711
NS
6112 if (!t)
6113 r = t;
6114 else if (TYPE_P (t))
6115 r = tsubst (t, args, complain, in_decl);
6116 else
00d3396f 6117 {
015c2c66
MM
6118 r = tsubst_expr (t, args, complain, in_decl,
6119 /*integral_constant_expression_p=*/true);
6f25cb35 6120 r = fold_non_dependent_expr (r);
bd83b409 6121 }
a91db711 6122 return r;
bd83b409
NS
6123}
6124
a91db711 6125/* Substitute ARGS into the vector or list of template arguments T. */
830bfa74 6126
e9659ab0 6127static tree
a91db711 6128tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
830bfa74 6129{
bf12d54d 6130 int len = TREE_VEC_LENGTH (t);
a91db711 6131 int need_new = 0, i;
67f5655f 6132 tree *elts = (tree *) alloca (len * sizeof (tree));
c8094d83 6133
830bfa74
MM
6134 for (i = 0; i < len; i++)
6135 {
bf12d54d
NS
6136 tree orig_arg = TREE_VEC_ELT (t, i);
6137 tree new_arg;
a91db711 6138
bf12d54d
NS
6139 if (TREE_CODE (orig_arg) == TREE_VEC)
6140 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
830bfa74 6141 else
a91db711 6142 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
c8094d83 6143
a91db711 6144 if (new_arg == error_mark_node)
08e72a19
JM
6145 return error_mark_node;
6146
a91db711
NS
6147 elts[i] = new_arg;
6148 if (new_arg != orig_arg)
830bfa74
MM
6149 need_new = 1;
6150 }
c8094d83 6151
830bfa74
MM
6152 if (!need_new)
6153 return t;
a91db711 6154
bf12d54d
NS
6155 t = make_tree_vec (len);
6156 for (i = 0; i < len; i++)
6157 TREE_VEC_ELT (t, i) = elts[i];
c8094d83 6158
830bfa74
MM
6159 return t;
6160}
6161
36a117a5
MM
6162/* Return the result of substituting ARGS into the template parameters
6163 given by PARMS. If there are m levels of ARGS and m + n levels of
6164 PARMS, then the result will contain n levels of PARMS. For
6165 example, if PARMS is `template <class T> template <class U>
6166 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
6167 result will be `template <int*, double, class V>'. */
6168
e9659ab0 6169static tree
3a978d72 6170tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
36a117a5 6171{
f71f87f9
MM
6172 tree r = NULL_TREE;
6173 tree* new_parms;
36a117a5 6174
8ca4bf25
MM
6175 /* When substituting into a template, we must set
6176 PROCESSING_TEMPLATE_DECL as the template parameters may be
6177 dependent if they are based on one-another, and the dependency
6178 predicates are short-circuit outside of templates. */
6179 ++processing_template_decl;
6180
36a117a5
MM
6181 for (new_parms = &r;
6182 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6183 new_parms = &(TREE_CHAIN (*new_parms)),
6184 parms = TREE_CHAIN (parms))
6185 {
c8094d83 6186 tree new_vec =
36a117a5
MM
6187 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6188 int i;
c8094d83 6189
36a117a5
MM
6190 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6191 {
42b304f1
LM
6192 tree tuple;
6193 tree default_value;
6194 tree parm_decl;
6195
6196 if (parms == error_mark_node)
6197 continue;
6198
6199 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6200
6201 if (tuple == error_mark_node)
6202 continue;
6203
6204 default_value = TREE_PURPOSE (tuple);
6205 parm_decl = TREE_VALUE (tuple);
833aa4c4
NS
6206
6207 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
a207780f
VR
6208 if (TREE_CODE (parm_decl) == PARM_DECL
6209 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6210 parm_decl = error_mark_node;
a91db711
NS
6211 default_value = tsubst_template_arg (default_value, args,
6212 complain, NULL_TREE);
c8094d83 6213
a91db711 6214 tuple = build_tree_list (default_value, parm_decl);
833aa4c4 6215 TREE_VEC_ELT (new_vec, i) = tuple;
36a117a5 6216 }
c8094d83
MS
6217
6218 *new_parms =
6219 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
4890c2f4 6220 - TMPL_ARGS_DEPTH (args)),
36a117a5
MM
6221 new_vec, NULL_TREE);
6222 }
6223
8ca4bf25
MM
6224 --processing_template_decl;
6225
36a117a5
MM
6226 return r;
6227}
6228
ed44da02
MM
6229/* Substitute the ARGS into the indicated aggregate (or enumeration)
6230 type T. If T is not an aggregate or enumeration type, it is
6231 handled as if by tsubst. IN_DECL is as for tsubst. If
838dfd8a 6232 ENTERING_SCOPE is nonzero, T is the context for a template which
dc957d14 6233 we are presently tsubst'ing. Return the substituted value. */
36a117a5 6234
e9659ab0 6235static tree
c8094d83 6236tsubst_aggr_type (tree t,
0cbd7506
MS
6237 tree args,
6238 tsubst_flags_t complain,
6239 tree in_decl,
6240 int entering_scope)
36a117a5
MM
6241{
6242 if (t == NULL_TREE)
6243 return NULL_TREE;
6244
6245 switch (TREE_CODE (t))
6246 {
6247 case RECORD_TYPE:
6248 if (TYPE_PTRMEMFUNC_P (t))
46c895ac 6249 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
36a117a5 6250
f4f206f4 6251 /* Else fall through. */
ed44da02 6252 case ENUMERAL_TYPE:
36a117a5 6253 case UNION_TYPE:
5db698f6 6254 if (TYPE_TEMPLATE_INFO (t))
36a117a5
MM
6255 {
6256 tree argvec;
6257 tree context;
6258 tree r;
3489ea0c
MM
6259 bool saved_skip_evaluation;
6260
6261 /* In "sizeof(X<I>)" we need to evaluate "I". */
6262 saved_skip_evaluation = skip_evaluation;
6263 skip_evaluation = false;
36a117a5
MM
6264
6265 /* First, determine the context for the type we are looking
6266 up. */
4f7847ca
NS
6267 context = TYPE_CONTEXT (t);
6268 if (context)
6269 context = tsubst_aggr_type (context, args, complain,
36a117a5 6270 in_decl, /*entering_scope=*/1);
36a117a5
MM
6271
6272 /* Then, figure out what arguments are appropriate for the
6273 type we are trying to find. For example, given:
6274
6275 template <class T> struct S;
6276 template <class T, class U> void f(T, U) { S<U> su; }
6277
6278 and supposing that we are instantiating f<int, double>,
6279 then our ARGS will be {int, double}, but, when looking up
6280 S we only want {double}. */
a91db711
NS
6281 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6282 complain, in_decl);
08e72a19 6283 if (argvec == error_mark_node)
3489ea0c
MM
6284 r = error_mark_node;
6285 else
6286 {
6287 r = lookup_template_class (t, argvec, in_decl, context,
6288 entering_scope, complain);
6289 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6290 }
3db45ab5 6291
3489ea0c 6292 skip_evaluation = saved_skip_evaluation;
36a117a5 6293
3489ea0c 6294 return r;
36a117a5 6295 }
c8094d83 6296 else
36a117a5
MM
6297 /* This is not a template type, so there's nothing to do. */
6298 return t;
6299
6300 default:
4393e105 6301 return tsubst (t, args, complain, in_decl);
36a117a5
MM
6302 }
6303}
6304
9188c363
MM
6305/* Substitute into the default argument ARG (a default argument for
6306 FN), which has the indicated TYPE. */
6307
6308tree
3a978d72 6309tsubst_default_argument (tree fn, tree type, tree arg)
9188c363 6310{
2436b51f
MM
6311 tree saved_class_ptr = NULL_TREE;
6312 tree saved_class_ref = NULL_TREE;
6313
9188c363
MM
6314 /* This default argument came from a template. Instantiate the
6315 default argument here, not in tsubst. In the case of
c8094d83
MS
6316 something like:
6317
9188c363
MM
6318 template <class T>
6319 struct S {
6320 static T t();
6321 void f(T = t());
6322 };
c8094d83 6323
9188c363 6324 we must be careful to do name lookup in the scope of S<T>,
d5a10cf0 6325 rather than in the current class. */
2b59fc25 6326 push_access_scope (fn);
2436b51f
MM
6327 /* The "this" pointer is not valid in a default argument. */
6328 if (cfun)
6329 {
6330 saved_class_ptr = current_class_ptr;
6331 cp_function_chain->x_current_class_ptr = NULL_TREE;
6332 saved_class_ref = current_class_ref;
6333 cp_function_chain->x_current_class_ref = NULL_TREE;
6334 }
9188c363 6335
d5a10cf0 6336 push_deferring_access_checks(dk_no_deferred);
5a8613b2
MM
6337 /* The default argument expression may cause implicitly defined
6338 member functions to be synthesized, which will result in garbage
6339 collection. We must treat this situation as if we were within
6340 the body of function so as to avoid collecting live data on the
6341 stack. */
6342 ++function_depth;
c2ea3a40 6343 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
015c2c66
MM
6344 tf_warning_or_error, NULL_TREE,
6345 /*integral_constant_expression_p=*/false);
5a8613b2 6346 --function_depth;
d5a10cf0
MM
6347 pop_deferring_access_checks();
6348
2436b51f
MM
6349 /* Restore the "this" pointer. */
6350 if (cfun)
6351 {
6352 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6353 cp_function_chain->x_current_class_ref = saved_class_ref;
6354 }
6355
2b59fc25 6356 pop_access_scope (fn);
9188c363
MM
6357
6358 /* Make sure the default argument is reasonable. */
6359 arg = check_default_argument (type, arg);
6360
6361 return arg;
6362}
6363
6364/* Substitute into all the default arguments for FN. */
6365
6366static void
3a978d72 6367tsubst_default_arguments (tree fn)
9188c363
MM
6368{
6369 tree arg;
6370 tree tmpl_args;
6371
6372 tmpl_args = DECL_TI_ARGS (fn);
6373
6374 /* If this function is not yet instantiated, we certainly don't need
6375 its default arguments. */
6376 if (uses_template_parms (tmpl_args))
6377 return;
6378
c8094d83
MS
6379 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6380 arg;
9188c363
MM
6381 arg = TREE_CHAIN (arg))
6382 if (TREE_PURPOSE (arg))
c8094d83 6383 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9188c363
MM
6384 TREE_VALUE (arg),
6385 TREE_PURPOSE (arg));
6386}
6387
fc6a28d7
MM
6388/* Substitute the ARGS into the T, which is a _DECL. Return the
6389 result of the substitution. Issue error and warning messages under
6390 control of COMPLAIN. */
00d3396f 6391
e9659ab0 6392static tree
fc6a28d7 6393tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8d08fdba 6394{
82a98427 6395 location_t saved_loc;
b370501f 6396 tree r = NULL_TREE;
4b2811e9 6397 tree in_decl = t;
830bfa74 6398
ae58fa02 6399 /* Set the filename and linenumber to improve error-reporting. */
82a98427 6400 saved_loc = input_location;
f31686a3 6401 input_location = DECL_SOURCE_LOCATION (t);
b7484fbe 6402
8d08fdba
MS
6403 switch (TREE_CODE (t))
6404 {
98c1c668
JM
6405 case TEMPLATE_DECL:
6406 {
28e42b7e
KL
6407 /* We can get here when processing a member function template,
6408 member class template, and template template parameter of
6409 a template class. */
98c1c668 6410 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 6411 tree spec;
28e42b7e
KL
6412 tree tmpl_args;
6413 tree full_args;
98c1c668 6414
28e42b7e 6415 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
db2767b6 6416 {
28e42b7e
KL
6417 /* Template template parameter is treated here. */
6418 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6419 if (new_type == error_mark_node)
6420 return error_mark_node;
36a117a5 6421
28e42b7e
KL
6422 r = copy_decl (t);
6423 TREE_CHAIN (r) = NULL_TREE;
6424 TREE_TYPE (r) = new_type;
6425 DECL_TEMPLATE_RESULT (r)
6426 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
c8094d83 6427 DECL_TEMPLATE_PARMS (r)
28e42b7e
KL
6428 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6429 complain);
6430 TYPE_NAME (new_type) = r;
6431 break;
6432 }
36a117a5 6433
28e42b7e
KL
6434 /* We might already have an instance of this template.
6435 The ARGS are for the surrounding class type, so the
6436 full args contain the tsubst'd args for the context,
6437 plus the innermost args from the template decl. */
c8094d83 6438 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
28e42b7e
KL
6439 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6440 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8ca4bf25
MM
6441 /* Because this is a template, the arguments will still be
6442 dependent, even after substitution. If
6443 PROCESSING_TEMPLATE_DECL is not set, the dependency
6444 predicates will short-circuit. */
6445 ++processing_template_decl;
28e42b7e
KL
6446 full_args = tsubst_template_args (tmpl_args, args,
6447 complain, in_decl);
8ca4bf25 6448 --processing_template_decl;
bf2f7328
VR
6449 if (full_args == error_mark_node)
6450 return error_mark_node;
28e42b7e
KL
6451
6452 /* tsubst_template_args doesn't copy the vector if
6453 nothing changed. But, *something* should have
6454 changed. */
6455 gcc_assert (full_args != tmpl_args);
6456
6457 spec = retrieve_specialization (t, full_args,
6458 /*class_specializations_p=*/true);
6459 if (spec != NULL_TREE)
6460 {
6461 r = spec;
6462 break;
db2767b6 6463 }
98c1c668
JM
6464
6465 /* Make a new template decl. It will be similar to the
c8094d83 6466 original, but will record the current template arguments.
98c1c668
JM
6467 We also create a new function declaration, which is just
6468 like the old one, but points to this new template, rather
6469 than the old one. */
0acf7199 6470 r = copy_decl (t);
50bc768d 6471 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
ae58fa02 6472 TREE_CHAIN (r) = NULL_TREE;
db2767b6 6473
ae58fa02 6474 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
6475
6476 if (TREE_CODE (decl) == TYPE_DECL)
6477 {
8ca4bf25
MM
6478 tree new_type;
6479 ++processing_template_decl;
6480 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
3db45ab5 6481 --processing_template_decl;
2620d095
KL
6482 if (new_type == error_mark_node)
6483 return error_mark_node;
6484
ae58fa02
MM
6485 TREE_TYPE (r) = new_type;
6486 CLASSTYPE_TI_TEMPLATE (new_type) = r;
17aec3eb 6487 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
ae58fa02 6488 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8ca4bf25 6489 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
93cdc044
JM
6490 }
6491 else
6492 {
8ca4bf25
MM
6493 tree new_decl;
6494 ++processing_template_decl;
6495 new_decl = tsubst (decl, args, complain, in_decl);
6496 --processing_template_decl;
caec1dc0
KL
6497 if (new_decl == error_mark_node)
6498 return error_mark_node;
17aec3eb
RK
6499
6500 DECL_TEMPLATE_RESULT (r) = new_decl;
ae58fa02
MM
6501 DECL_TI_TEMPLATE (new_decl) = r;
6502 TREE_TYPE (r) = TREE_TYPE (new_decl);
6503 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
3db45ab5 6504 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
93cdc044
JM
6505 }
6506
ae58fa02
MM
6507 SET_DECL_IMPLICIT_INSTANTIATION (r);
6508 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6509 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
6510
6511 /* The template parameters for this new template are all the
6512 template parameters for the old template, except the
c6002625 6513 outermost level of parameters. */
c8094d83 6514 DECL_TEMPLATE_PARMS (r)
4393e105 6515 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
c2ea3a40 6516 complain);
98c1c668 6517
93cdc044 6518 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 6519 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 6520
8c6ab2db
NS
6521 if (TREE_CODE (decl) != TYPE_DECL)
6522 /* Record this non-type partial instantiation. */
c8094d83 6523 register_specialization (r, t,
d63d5d0c
ILT
6524 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6525 false);
98c1c668 6526 }
ae58fa02 6527 break;
8d08fdba
MS
6528
6529 case FUNCTION_DECL:
6530 {
386b8a85 6531 tree ctx;
87603ed0 6532 tree argvec = NULL_TREE;
cf38f48a 6533 tree *friends;
36a117a5 6534 tree gen_tmpl;
fc6a28d7 6535 tree type;
5566b478 6536 int member;
d8c4447d
MM
6537 int args_depth;
6538 int parms_depth;
5566b478 6539
36a117a5 6540 /* Nobody should be tsubst'ing into non-template functions. */
50bc768d 6541 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
36a117a5
MM
6542
6543 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6544 {
6545 tree spec;
00cf3e31
MM
6546 bool dependent_p;
6547
6548 /* If T is not dependent, just return it. We have to
6549 increment PROCESSING_TEMPLATE_DECL because
6550 value_dependent_expression_p assumes that nothing is
6551 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6552 ++processing_template_decl;
6553 dependent_p = value_dependent_expression_p (t);
6554 --processing_template_decl;
6555 if (!dependent_p)
6556 return t;
36a117a5
MM
6557
6558 /* Calculate the most general template of which R is a
6559 specialization, and the complete set of arguments used to
6560 specialize R. */
6561 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
c8094d83 6562 argvec = tsubst_template_args (DECL_TI_ARGS
a91db711 6563 (DECL_TEMPLATE_RESULT (gen_tmpl)),
c8094d83 6564 args, complain, in_decl);
36a117a5
MM
6565
6566 /* Check to see if we already have this specialization. */
c7222c02
MM
6567 spec = retrieve_specialization (gen_tmpl, argvec,
6568 /*class_specializations_p=*/false);
7ddedda4 6569
36a117a5 6570 if (spec)
ae58fa02
MM
6571 {
6572 r = spec;
6573 break;
6574 }
d8c4447d 6575
f9a7ae04
MM
6576 /* We can see more levels of arguments than parameters if
6577 there was a specialization of a member template, like
6578 this:
6579
0cbd7506 6580 template <class T> struct S { template <class U> void f(); }
c8094d83 6581 template <> template <class U> void S<int>::f(U);
f9a7ae04 6582
dc957d14 6583 Here, we'll be substituting into the specialization,
f9a7ae04
MM
6584 because that's where we can find the code we actually
6585 want to generate, but we'll have enough arguments for
c8094d83 6586 the most general template.
f9a7ae04
MM
6587
6588 We also deal with the peculiar case:
d8c4447d 6589
c8094d83 6590 template <class T> struct S {
d8c4447d
MM
6591 template <class U> friend void f();
6592 };
74b846e0 6593 template <class U> void f() {}
d8c4447d
MM
6594 template S<int>;
6595 template void f<double>();
6596
6597 Here, the ARGS for the instantiation of will be {int,
6598 double}. But, we only need as many ARGS as there are
6599 levels of template parameters in CODE_PATTERN. We are
6600 careful not to get fooled into reducing the ARGS in
6601 situations like:
6602
6603 template <class T> struct S { template <class U> void f(U); }
6604 template <class T> template <> void S<T>::f(int) {}
6605
6606 which we can spot because the pattern will be a
6607 specialization in this case. */
6608 args_depth = TMPL_ARGS_DEPTH (args);
c8094d83
MS
6609 parms_depth =
6610 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
d8c4447d
MM
6611 if (args_depth > parms_depth
6612 && !DECL_TEMPLATE_SPECIALIZATION (t))
f9a7ae04 6613 args = get_innermost_template_args (args, parms_depth);
36a117a5
MM
6614 }
6615 else
6616 {
6617 /* This special case arises when we have something like this:
6618
0cbd7506 6619 template <class T> struct S {
c8094d83 6620 friend void f<int>(int, double);
36a117a5
MM
6621 };
6622
6623 Here, the DECL_TI_TEMPLATE for the friend declaration
10b1d5e7
MM
6624 will be an IDENTIFIER_NODE. We are being called from
6625 tsubst_friend_function, and we want only to create a
6626 new decl (R) with appropriate types so that we can call
6627 determine_specialization. */
36a117a5
MM
6628 gen_tmpl = NULL_TREE;
6629 }
6630
6eb3bb27 6631 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 6632 {
5566b478
MS
6633 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6634 member = 2;
6635 else
6636 member = 1;
c8094d83 6637 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8c6ab2db 6638 complain, t, /*entering_scope=*/1);
5566b478
MS
6639 }
6640 else
6641 {
6642 member = 0;
4f1c5b7d 6643 ctx = DECL_CONTEXT (t);
5566b478 6644 }
fc6a28d7 6645 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
b3da7bb1
MM
6646 if (type == error_mark_node)
6647 return error_mark_node;
8d08fdba 6648
5566b478 6649 /* We do NOT check for matching decls pushed separately at this
0cbd7506
MS
6650 point, as they may not represent instantiations of this
6651 template, and in any case are considered separate under the
6652 discrete model. */
0acf7199 6653 r = copy_decl (t);
e1467ff2 6654 DECL_USE_TEMPLATE (r) = 0;
5566b478 6655 TREE_TYPE (r) = type;
92643fea
MM
6656 /* Clear out the mangled name and RTL for the instantiation. */
6657 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6658 SET_DECL_RTL (r, NULL_RTX);
5bd61841 6659 DECL_INITIAL (r) = NULL_TREE;
4f1c5b7d 6660 DECL_CONTEXT (r) = ctx;
5566b478 6661
c8094d83 6662 if (member && DECL_CONV_FN_P (r))
1f84ec23
MM
6663 /* Type-conversion operator. Reconstruct the name, in
6664 case it's the name of one of the template's parameters. */
6665 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5566b478 6666
4393e105 6667 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
c2ea3a40 6668 complain, t);
477f6664 6669 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
6670
6671 TREE_STATIC (r) = 0;
6672 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6673 DECL_EXTERNAL (r) = 1;
4684cd27
MM
6674 /* If this is an instantiation of a function with internal
6675 linkage, we already know what object file linkage will be
6676 assigned to the instantiation. */
6677 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
711734a9
JM
6678 DECL_DEFER_OUTPUT (r) = 0;
6679 TREE_CHAIN (r) = NULL_TREE;
6680 DECL_PENDING_INLINE_INFO (r) = 0;
59026e79 6681 DECL_PENDING_INLINE_P (r) = 0;
655dc6ee 6682 DECL_SAVED_TREE (r) = NULL_TREE;
711734a9 6683 TREE_USED (r) = 0;
db9b2174
MM
6684 if (DECL_CLONED_FUNCTION (r))
6685 {
6686 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
c2ea3a40 6687 args, complain, t);
db9b2174
MM
6688 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6689 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6690 }
711734a9 6691
92643fea
MM
6692 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6693 this in the special friend case mentioned above where
6694 GEN_TMPL is NULL. */
36a117a5 6695 if (gen_tmpl)
386b8a85 6696 {
c8094d83 6697 DECL_TEMPLATE_INFO (r)
e1b3e07d 6698 = tree_cons (gen_tmpl, argvec, NULL_TREE);
36a117a5 6699 SET_DECL_IMPLICIT_INSTANTIATION (r);
d63d5d0c 6700 register_specialization (r, gen_tmpl, argvec, false);
36a117a5 6701
9188c363
MM
6702 /* We're not supposed to instantiate default arguments
6703 until they are called, for a template. But, for a
6704 declaration like:
6705
0cbd7506
MS
6706 template <class T> void f ()
6707 { extern void g(int i = T()); }
c8094d83 6708
9188c363
MM
6709 we should do the substitution when the template is
6710 instantiated. We handle the member function case in
6711 instantiate_class_template since the default arguments
6712 might refer to other members of the class. */
6713 if (!member
6714 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6715 && !uses_template_parms (argvec))
6716 tsubst_default_arguments (r);
386b8a85 6717 }
c43e95f8
MM
6718 else
6719 DECL_TEMPLATE_INFO (r) = NULL_TREE;
f181d4ae 6720
cf38f48a
MM
6721 /* Copy the list of befriending classes. */
6722 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6723 *friends;
c8094d83 6724 friends = &TREE_CHAIN (*friends))
cf38f48a
MM
6725 {
6726 *friends = copy_node (*friends);
6727 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
c2ea3a40 6728 args, complain,
cf38f48a
MM
6729 in_decl);
6730 }
6731
212e7048 6732 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
f181d4ae
MM
6733 {
6734 maybe_retrofit_in_chrg (r);
212e7048
MM
6735 if (DECL_CONSTRUCTOR_P (r))
6736 grok_ctor_properties (ctx, r);
2be678ff
JM
6737 /* If this is an instantiation of a member template, clone it.
6738 If it isn't, that'll be handled by
6739 clone_constructors_and_destructors. */
5e818b93 6740 if (PRIMARY_TEMPLATE_P (gen_tmpl))
271e6f02 6741 clone_function_decl (r, /*update_method_vec_p=*/0);
f181d4ae 6742 }
398cd199
VR
6743 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
6744 && !grok_op_properties (r, (complain & tf_error) != 0))
6745 return error_mark_node;
c8b2e872
MM
6746
6747 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6748 SET_DECL_FRIEND_CONTEXT (r,
6749 tsubst (DECL_FRIEND_CONTEXT (t),
6750 args, complain, in_decl));
b9e75696
JM
6751
6752 /* Possibly limit visibility based on template args. */
6753 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
b70f0f48
JM
6754 if (DECL_VISIBILITY_SPECIFIED (t))
6755 {
6756 DECL_VISIBILITY_SPECIFIED (r) = 0;
6757 DECL_ATTRIBUTES (r)
6758 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6759 }
b9e75696 6760 determine_visibility (r);
8d08fdba 6761 }
ae58fa02 6762 break;
8d08fdba
MS
6763
6764 case PARM_DECL:
6765 {
fc6a28d7
MM
6766 tree type;
6767
ae58fa02 6768 r = copy_node (t);
833aa4c4
NS
6769 if (DECL_TEMPLATE_PARM_P (t))
6770 SET_DECL_TEMPLATE_PARM_P (r);
8e51619a 6771
fc6a28d7 6772 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
02bab9db 6773 type = type_decays_to (type);
5566b478 6774 TREE_TYPE (r) = type;
9804209d 6775 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1 6776
560ad596
MM
6777 if (DECL_INITIAL (r))
6778 {
6779 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6780 DECL_INITIAL (r) = TREE_TYPE (r);
6781 else
6782 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6783 complain, in_decl);
6784 }
db2767b6 6785
5566b478 6786 DECL_CONTEXT (r) = NULL_TREE;
8e51619a
JM
6787
6788 if (!DECL_TEMPLATE_PARM_P (r))
6789 DECL_ARG_TYPE (r) = type_passed_as (type);
8d08fdba 6790 if (TREE_CHAIN (t))
4393e105 6791 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
c2ea3a40 6792 complain, TREE_CHAIN (t));
8d08fdba 6793 }
ae58fa02 6794 break;
8d08fdba 6795
5566b478
MS
6796 case FIELD_DECL:
6797 {
fc6a28d7
MM
6798 tree type;
6799
0acf7199 6800 r = copy_decl (t);
fc6a28d7
MM
6801 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6802 if (type == error_mark_node)
6803 return error_mark_node;
1b8899d1 6804 TREE_TYPE (r) = type;
9804209d 6805 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1 6806
015c2c66
MM
6807 /* DECL_INITIAL gives the number of bits in a bit-field. */
6808 DECL_INITIAL (r)
6809 = tsubst_expr (DECL_INITIAL (t), args,
6810 complain, in_decl,
6811 /*integral_constant_expression_p=*/true);
1b8899d1
MM
6812 /* We don't have to set DECL_CONTEXT here; it is set by
6813 finish_member_declaration. */
5566b478 6814 TREE_CHAIN (r) = NULL_TREE;
c8094d83 6815 if (VOID_TYPE_P (type))
dee15844 6816 error ("instantiation of %q+D as type %qT", r, type);
5566b478 6817 }
ae58fa02 6818 break;
5566b478
MS
6819
6820 case USING_DECL:
98ed9dae
NS
6821 /* We reach here only for member using decls. */
6822 if (DECL_DEPENDENT_P (t))
6823 {
6824 r = do_class_using_decl
6825 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6826 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6827 if (!r)
6828 r = error_mark_node;
6829 }
6830 else
6831 {
6832 r = copy_node (t);
6833 TREE_CHAIN (r) = NULL_TREE;
6834 }
ae58fa02 6835 break;
5566b478 6836
9188c363 6837 case TYPE_DECL:
5566b478
MS
6838 case VAR_DECL:
6839 {
1cea0434
KG
6840 tree argvec = NULL_TREE;
6841 tree gen_tmpl = NULL_TREE;
36a117a5 6842 tree spec;
1cea0434 6843 tree tmpl = NULL_TREE;
9188c363 6844 tree ctx;
fc6a28d7 6845 tree type = NULL_TREE;
f604fc3b 6846 bool local_p;
9188c363 6847
fc6a28d7
MM
6848 if (TREE_CODE (t) == TYPE_DECL)
6849 {
6850 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6851 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6852 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6853 {
6854 /* If this is the canonical decl, we don't have to
6855 mess with instantiations, and often we can't (for
6856 typename, template type parms and such). Note that
6857 TYPE_NAME is not correct for the above test if
6858 we've copied the type for a typedef. */
6859 r = TYPE_NAME (type);
6860 break;
6861 }
6862 }
c8094d83 6863
f604fc3b
MM
6864 /* Check to see if we already have the specialization we
6865 need. */
6866 spec = NULL_TREE;
6867 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
6868 {
6869 /* T is a static data member or namespace-scope entity.
6870 We have to substitute into namespace-scope variables
6871 (even though such entities are never templates) because
6872 of cases like:
6873
6874 template <class T> void f() { extern T t; }
6875
6876 where the entity referenced is not known until
6877 instantiation time. */
6878 local_p = false;
6879 ctx = DECL_CONTEXT (t);
6880 if (DECL_CLASS_SCOPE_P (t))
6881 {
6882 ctx = tsubst_aggr_type (ctx, args,
6883 complain,
6884 in_decl, /*entering_scope=*/1);
6885 /* If CTX is unchanged, then T is in fact the
6886 specialization we want. That situation occurs when
6887 referencing a static data member within in its own
6888 class. We can use pointer equality, rather than
6889 same_type_p, because DECL_CONTEXT is always
6890 canonical. */
6891 if (ctx == DECL_CONTEXT (t))
6892 spec = t;
6893 }
5566b478 6894
f604fc3b
MM
6895 if (!spec)
6896 {
6897 tmpl = DECL_TI_TEMPLATE (t);
6898 gen_tmpl = most_general_template (tmpl);
6899 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6900 spec = (retrieve_specialization
6901 (gen_tmpl, argvec,
6902 /*class_specializations_p=*/false));
6903 }
6904 }
9188c363 6905 else
6dfbb909 6906 {
f604fc3b
MM
6907 /* A local variable. */
6908 local_p = true;
6dfbb909
MM
6909 /* Subsequent calls to pushdecl will fill this in. */
6910 ctx = NULL_TREE;
f604fc3b 6911 spec = retrieve_local_specialization (t);
6dfbb909 6912 }
f604fc3b
MM
6913 /* If we already have the specialization we need, there is
6914 nothing more to do. */
36a117a5 6915 if (spec)
ae58fa02
MM
6916 {
6917 r = spec;
6918 break;
6919 }
5566b478 6920
f604fc3b 6921 /* Create a new node for the specialization we need. */
0acf7199 6922 r = copy_decl (t);
edebf865 6923 if (TREE_CODE (r) == VAR_DECL)
39703eb9 6924 {
8b0a8500
MM
6925 /* Even if the original location is out of scope, the
6926 newly substituted one is not. */
6927 DECL_DEAD_FOR_LOCAL (r) = 0;
6928 DECL_INITIALIZED_P (r) = 0;
6929 DECL_TEMPLATE_INSTANTIATED (r) = 0;
fc6a28d7
MM
6930 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6931 if (type == error_mark_node)
6932 return error_mark_node;
39703eb9
MM
6933 type = complete_type (type);
6934 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6935 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
62e19030 6936 type = check_var_type (DECL_NAME (r), type);
56b4ea3d
RH
6937
6938 if (DECL_HAS_VALUE_EXPR_P (t))
6939 {
6940 tree ve = DECL_VALUE_EXPR (t);
015c2c66
MM
6941 ve = tsubst_expr (ve, args, complain, in_decl,
6942 /*constant_expression_p=*/false);
3db45ab5 6943 SET_DECL_VALUE_EXPR (r, ve);
56b4ea3d 6944 }
39703eb9 6945 }
a3d87771
MM
6946 else if (DECL_SELF_REFERENCE_P (t))
6947 SET_DECL_SELF_REFERENCE_P (r);
01f4137f 6948 TREE_TYPE (r) = type;
9804209d 6949 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
5566b478 6950 DECL_CONTEXT (r) = ctx;
92643fea
MM
6951 /* Clear out the mangled name and RTL for the instantiation. */
6952 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
820cc88f
DB
6953 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6954 SET_DECL_RTL (r, NULL_RTX);
8b0a8500
MM
6955 /* The initializer must not be expanded until it is required;
6956 see [temp.inst]. */
d11ad92e 6957 DECL_INITIAL (r) = NULL_TREE;
820cc88f
DB
6958 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6959 SET_DECL_RTL (r, NULL_RTX);
06ceef4e 6960 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
b9e75696
JM
6961 if (TREE_CODE (r) == VAR_DECL)
6962 {
6963 /* Possibly limit visibility based on template args. */
6964 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
b70f0f48
JM
6965 if (DECL_VISIBILITY_SPECIFIED (t))
6966 {
6967 DECL_VISIBILITY_SPECIFIED (r) = 0;
6968 DECL_ATTRIBUTES (r)
6969 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6970 }
b9e75696
JM
6971 determine_visibility (r);
6972 }
5566b478 6973
6dfbb909
MM
6974 if (!local_p)
6975 {
6976 /* A static data member declaration is always marked
6977 external when it is declared in-class, even if an
6978 initializer is present. We mimic the non-template
6979 processing here. */
6980 DECL_EXTERNAL (r) = 1;
fa8d6e85 6981
d63d5d0c 6982 register_specialization (r, gen_tmpl, argvec, false);
6dfbb909
MM
6983 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6984 SET_DECL_IMPLICIT_INSTANTIATION (r);
6985 }
9188c363 6986 else
6dfbb909 6987 register_local_specialization (r, t);
5566b478 6988
5566b478 6989 TREE_CHAIN (r) = NULL_TREE;
edebf865 6990 layout_decl (r, 0);
5566b478 6991 }
ae58fa02 6992 break;
5566b478 6993
ae58fa02 6994 default:
315fb5db 6995 gcc_unreachable ();
c8094d83 6996 }
ae58fa02
MM
6997
6998 /* Restore the file and line information. */
82a98427 6999 input_location = saved_loc;
ae58fa02
MM
7000
7001 return r;
7002}
7003
34cd5ae7 7004/* Substitute into the ARG_TYPES of a function type. */
cabc336a 7005
e9659ab0 7006static tree
c8094d83 7007tsubst_arg_types (tree arg_types,
0cbd7506
MS
7008 tree args,
7009 tsubst_flags_t complain,
7010 tree in_decl)
cabc336a
MM
7011{
7012 tree remaining_arg_types;
cabc336a 7013 tree type;
5e97d404
NS
7014 tree default_arg;
7015 tree result = NULL_TREE;
cabc336a
MM
7016
7017 if (!arg_types || arg_types == void_list_node)
7018 return arg_types;
c8094d83 7019
cabc336a 7020 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
4393e105
MM
7021 args, complain, in_decl);
7022 if (remaining_arg_types == error_mark_node)
7023 return error_mark_node;
7024
7025 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
7026 if (type == error_mark_node)
7027 return error_mark_node;
4b2811e9
NS
7028 if (VOID_TYPE_P (type))
7029 {
c2ea3a40 7030 if (complain & tf_error)
0cbd7506
MS
7031 {
7032 error ("invalid parameter type %qT", type);
7033 if (in_decl)
dee15844 7034 error ("in declaration %q+D", in_decl);
0cbd7506 7035 }
4b2811e9
NS
7036 return error_mark_node;
7037 }
cabc336a 7038
4393e105
MM
7039 /* Do array-to-pointer, function-to-pointer conversion, and ignore
7040 top-level qualifiers as required. */
7041 type = TYPE_MAIN_VARIANT (type_decays_to (type));
cabc336a 7042
5e97d404
NS
7043 /* We do not substitute into default arguments here. The standard
7044 mandates that they be instantiated only when needed, which is
7045 done in build_over_call. */
7046 default_arg = TREE_PURPOSE (arg_types);
c8094d83 7047
5e97d404
NS
7048 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
7049 {
7050 /* We've instantiated a template before its default arguments
0cbd7506
MS
7051 have been parsed. This can happen for a nested template
7052 class, and is not an error unless we require the default
7053 argument in a call of this function. */
5e97d404 7054 result = tree_cons (default_arg, type, remaining_arg_types);
01ea1ea8 7055 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
5e97d404
NS
7056 }
7057 else
7058 result = hash_tree_cons (default_arg, type, remaining_arg_types);
c8094d83 7059
5e97d404 7060 return result;
cabc336a
MM
7061}
7062
4393e105
MM
7063/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
7064 *not* handle the exception-specification for FNTYPE, because the
7065 initial substitution of explicitly provided template parameters
7066 during argument deduction forbids substitution into the
7067 exception-specification:
7068
7069 [temp.deduct]
7070
7071 All references in the function type of the function template to the
7072 corresponding template parameters are replaced by the specified tem-
7073 plate argument values. If a substitution in a template parameter or
7074 in the function type of the function template results in an invalid
7075 type, type deduction fails. [Note: The equivalent substitution in
7076 exception specifications is done only when the function is instanti-
7077 ated, at which point a program is ill-formed if the substitution
7078 results in an invalid type.] */
7079
7080static tree
c8094d83 7081tsubst_function_type (tree t,
0cbd7506
MS
7082 tree args,
7083 tsubst_flags_t complain,
7084 tree in_decl)
4393e105
MM
7085{
7086 tree return_type;
7087 tree arg_types;
7088 tree fntype;
7089
8dd3f57a 7090 /* The TYPE_CONTEXT is not used for function/method types. */
50bc768d 7091 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
4393e105 7092
46c895ac 7093 /* Substitute the return type. */
4393e105
MM
7094 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7095 if (return_type == error_mark_node)
7096 return error_mark_node;
6e2993bf
MM
7097 /* The standard does not presently indicate that creation of a
7098 function type with an invalid return type is a deduction failure.
270af55d 7099 However, that is clearly analogous to creating an array of "void"
c8094d83 7100 or a reference to a reference. This is core issue #486. */
6e2993bf
MM
7101 if (TREE_CODE (return_type) == ARRAY_TYPE
7102 || TREE_CODE (return_type) == FUNCTION_TYPE)
7103 {
7104 if (complain & tf_error)
7105 {
7106 if (TREE_CODE (return_type) == ARRAY_TYPE)
7107 error ("function returning an array");
7108 else
7109 error ("function returning a function");
7110 }
7111 return error_mark_node;
7112 }
4393e105 7113
34cd5ae7 7114 /* Substitute the argument types. */
4393e105 7115 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
c8094d83 7116 complain, in_decl);
4393e105
MM
7117 if (arg_types == error_mark_node)
7118 return error_mark_node;
c8094d83 7119
4393e105
MM
7120 /* Construct a new type node and return it. */
7121 if (TREE_CODE (t) == FUNCTION_TYPE)
7122 fntype = build_function_type (return_type, arg_types);
7123 else
7124 {
7125 tree r = TREE_TYPE (TREE_VALUE (arg_types));
7126 if (! IS_AGGR_TYPE (r))
7127 {
7128 /* [temp.deduct]
c8094d83 7129
4393e105
MM
7130 Type deduction may fail for any of the following
7131 reasons:
c8094d83 7132
4393e105
MM
7133 -- Attempting to create "pointer to member of T" when T
7134 is not a class type. */
c2ea3a40 7135 if (complain & tf_error)
0f51ccfc 7136 error ("creating pointer to member function of non-class type %qT",
4393e105
MM
7137 r);
7138 return error_mark_node;
7139 }
c8094d83
MS
7140
7141 fntype = build_method_type_directly (r, return_type,
43dc123f 7142 TREE_CHAIN (arg_types));
4393e105 7143 }
c2ea3a40 7144 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
e9525111 7145 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
c8094d83
MS
7146
7147 return fntype;
4393e105
MM
7148}
7149
c7222c02
MM
7150/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
7151 ARGS into that specification, and return the substituted
7152 specification. If there is no specification, return NULL_TREE. */
7153
7154static tree
c8094d83
MS
7155tsubst_exception_specification (tree fntype,
7156 tree args,
c7222c02
MM
7157 tsubst_flags_t complain,
7158 tree in_decl)
7159{
7160 tree specs;
7161 tree new_specs;
7162
7163 specs = TYPE_RAISES_EXCEPTIONS (fntype);
7164 new_specs = NULL_TREE;
7165 if (specs)
7166 {
7167 if (! TREE_VALUE (specs))
7168 new_specs = specs;
7169 else
7170 while (specs)
7171 {
7172 tree spec;
7173 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
7174 if (spec == error_mark_node)
7175 return spec;
7176 new_specs = add_exception_specifier (new_specs, spec, complain);
7177 specs = TREE_CHAIN (specs);
7178 }
7179 }
7180 return new_specs;
7181}
7182
4393e105
MM
7183/* Take the tree structure T and replace template parameters used
7184 therein with the argument vector ARGS. IN_DECL is an associated
7185 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
c2ea3a40
NS
7186 Issue error and warning messages under control of COMPLAIN. Note
7187 that we must be relatively non-tolerant of extensions here, in
7188 order to preserve conformance; if we allow substitutions that
7189 should not be allowed, we may allow argument deductions that should
7190 not succeed, and therefore report ambiguous overload situations
7191 where there are none. In theory, we could allow the substitution,
7192 but indicate that it should have failed, and allow our caller to
7193 make sure that the right thing happens, but we don't try to do this
7194 yet.
4393e105
MM
7195
7196 This function is used for dealing with types, decls and the like;
7197 for expressions, use tsubst_expr or tsubst_copy. */
ae58fa02 7198
14d22dd6 7199static tree
3a978d72 7200tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
ae58fa02 7201{
0ecfe0b4 7202 tree type, r;
ae58fa02
MM
7203
7204 if (t == NULL_TREE || t == error_mark_node
7205 || t == integer_type_node
7206 || t == void_type_node
7207 || t == char_type_node
539599c1 7208 || t == unknown_type_node
ae58fa02
MM
7209 || TREE_CODE (t) == NAMESPACE_DECL)
7210 return t;
7211
fc6a28d7
MM
7212 if (DECL_P (t))
7213 return tsubst_decl (t, args, complain);
7214
ae58fa02
MM
7215 if (TREE_CODE (t) == IDENTIFIER_NODE)
7216 type = IDENTIFIER_TYPE_VALUE (t);
7217 else
7218 type = TREE_TYPE (t);
399dedb9 7219
50bc768d 7220 gcc_assert (type != unknown_type_node);
ae58fa02 7221
fc6a28d7 7222 if (type
ae58fa02 7223 && TREE_CODE (t) != TYPENAME_TYPE
4393e105
MM
7224 && TREE_CODE (t) != IDENTIFIER_NODE
7225 && TREE_CODE (t) != FUNCTION_TYPE
7226 && TREE_CODE (t) != METHOD_TYPE)
7227 type = tsubst (type, args, complain, in_decl);
7228 if (type == error_mark_node)
7229 return error_mark_node;
ae58fa02 7230
ae58fa02
MM
7231 switch (TREE_CODE (t))
7232 {
7233 case RECORD_TYPE:
7234 case UNION_TYPE:
7235 case ENUMERAL_TYPE:
4393e105
MM
7236 return tsubst_aggr_type (t, args, complain, in_decl,
7237 /*entering_scope=*/0);
ae58fa02
MM
7238
7239 case ERROR_MARK:
7240 case IDENTIFIER_NODE:
ae58fa02
MM
7241 case VOID_TYPE:
7242 case REAL_TYPE:
7243 case COMPLEX_TYPE:
c00996a3 7244 case VECTOR_TYPE:
ae58fa02
MM
7245 case BOOLEAN_TYPE:
7246 case INTEGER_CST:
7247 case REAL_CST:
7248 case STRING_CST:
7249 return t;
7250
7251 case INTEGER_TYPE:
7252 if (t == integer_type_node)
7253 return t;
7254
7255 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7256 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7257 return t;
d2e5ee5c 7258
5566b478 7259 {
ddce3528 7260 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 7261
6ee3ffe8
MM
7262 max = tsubst_expr (omax, args, complain, in_decl,
7263 /*integral_constant_expression_p=*/false);
7264 max = fold_non_dependent_expr (max);
4ef69b83 7265 max = fold_decl_constant_value (max);
8dd3f57a 7266
eff3a276
MM
7267 if (TREE_CODE (max) != INTEGER_CST
7268 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
7269 && !at_function_scope_p ())
7270 {
7271 if (complain & tf_error)
7272 error ("array bound is not an integer constant");
7273 return error_mark_node;
7274 }
7275
95cd6f6f 7276 /* [temp.deduct]
4393e105 7277
95cd6f6f
JC
7278 Type deduction may fail for any of the following
7279 reasons:
4393e105 7280
95cd6f6f
JC
7281 Attempting to create an array with a size that is
7282 zero or negative. */
7283 if (integer_zerop (max) && !(complain & tf_error))
7284 /* We must fail if performing argument deduction (as
7285 indicated by the state of complain), so that
7286 another substitution can be found. */
7287 return error_mark_node;
95cd6f6f
JC
7288 else if (TREE_CODE (max) == INTEGER_CST
7289 && INT_CST_LT (max, integer_zero_node))
7290 {
c2ea3a40 7291 if (complain & tf_error)
95cd6f6f 7292 error ("creating array with negative size (%qE)", max);
4393e105
MM
7293
7294 return error_mark_node;
0ecfe0b4
JM
7295 }
7296
c95cd22e 7297 return compute_array_index_type (NULL_TREE, max);
ae58fa02
MM
7298 }
7299
7300 case TEMPLATE_TYPE_PARM:
7301 case TEMPLATE_TEMPLATE_PARM:
a1281f45 7302 case BOUND_TEMPLATE_TEMPLATE_PARM:
ae58fa02
MM
7303 case TEMPLATE_PARM_INDEX:
7304 {
7305 int idx;
7306 int level;
7307 int levels;
315fb5db 7308 tree arg = NULL_TREE;
0ecfe0b4
JM
7309
7310 r = NULL_TREE;
ae58fa02 7311
315fb5db 7312 gcc_assert (TREE_VEC_LENGTH (args) > 0);
ae58fa02 7313 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
a1281f45
KL
7314 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7315 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
ae58fa02
MM
7316 {
7317 idx = TEMPLATE_TYPE_IDX (t);
7318 level = TEMPLATE_TYPE_LEVEL (t);
7319 }
7320 else
7321 {
7322 idx = TEMPLATE_PARM_IDX (t);
7323 level = TEMPLATE_PARM_LEVEL (t);
7324 }
7325
315fb5db
NS
7326 levels = TMPL_ARGS_DEPTH (args);
7327 if (level <= levels)
7328 arg = TMPL_ARG (args, level, idx);
ae58fa02 7329
315fb5db
NS
7330 if (arg == error_mark_node)
7331 return error_mark_node;
7332 else if (arg != NULL_TREE)
7333 {
7334 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
ae58fa02 7335 {
9180c238 7336 int quals;
315fb5db 7337 gcc_assert (TYPE_P (arg));
9180c238
JM
7338
7339 /* cv-quals from the template are discarded when
7340 substituting in a function or reference type. */
7341 if (TREE_CODE (arg) == FUNCTION_TYPE
7342 || TREE_CODE (arg) == METHOD_TYPE
7343 || TREE_CODE (arg) == REFERENCE_TYPE)
7344 quals = cp_type_quals (arg);
7345 else
7346 quals = cp_type_quals (arg) | cp_type_quals (t);
7347
315fb5db 7348 return cp_build_qualified_type_real
9180c238 7349 (arg, quals, complain | tf_ignore_bad_quals);
315fb5db
NS
7350 }
7351 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7352 {
7353 /* We are processing a type constructed from a
7354 template template parameter. */
7355 tree argvec = tsubst (TYPE_TI_ARGS (t),
7356 args, complain, in_decl);
7357 if (argvec == error_mark_node)
7358 return error_mark_node;
c8094d83 7359
315fb5db
NS
7360 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7361 are resolving nested-types in the signature of a
7362 member function templates. Otherwise ARG is a
7363 TEMPLATE_DECL and is the real template to be
7364 instantiated. */
7365 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7366 arg = TYPE_NAME (arg);
c8094d83
MS
7367
7368 r = lookup_template_class (arg,
7369 argvec, in_decl,
315fb5db
NS
7370 DECL_CONTEXT (arg),
7371 /*entering_scope=*/0,
7372 complain);
7373 return cp_build_qualified_type_real
7374 (r, TYPE_QUALS (t), complain);
ae58fa02 7375 }
315fb5db
NS
7376 else
7377 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7378 return arg;
ae58fa02
MM
7379 }
7380
7381 if (level == 1)
7382 /* This can happen during the attempted tsubst'ing in
7383 unify. This means that we don't yet have any information
7384 about the template parameter in question. */
7385 return t;
7386
7387 /* If we get here, we must have been looking at a parm for a
7388 more deeply nested template. Make a new version of this
7389 template parameter, but with a lower level. */
7390 switch (TREE_CODE (t))
7391 {
7392 case TEMPLATE_TYPE_PARM:
7393 case TEMPLATE_TEMPLATE_PARM:
a1281f45 7394 case BOUND_TEMPLATE_TEMPLATE_PARM:
89d684bb 7395 if (cp_type_quals (t))
ae58fa02 7396 {
9ccf6541 7397 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
0cbd7506
MS
7398 r = cp_build_qualified_type_real
7399 (r, cp_type_quals (t),
4f2b0fb2
NS
7400 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7401 ? tf_ignore_bad_quals : 0));
9ccf6541
MM
7402 }
7403 else
7404 {
11e74ea6 7405 r = copy_type (t);
9ccf6541
MM
7406 TEMPLATE_TYPE_PARM_INDEX (r)
7407 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7408 r, levels);
7409 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7410 TYPE_MAIN_VARIANT (r) = r;
7411 TYPE_POINTER_TO (r) = NULL_TREE;
7412 TYPE_REFERENCE_TO (r) = NULL_TREE;
7413
a1281f45 7414 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9ccf6541
MM
7415 {
7416 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
c8094d83 7417 complain, in_decl);
9ccf6541
MM
7418 if (argvec == error_mark_node)
7419 return error_mark_node;
4393e105 7420
9ccf6541
MM
7421 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7422 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7423 }
ae58fa02
MM
7424 }
7425 break;
7426
7427 case TEMPLATE_PARM_INDEX:
7428 r = reduce_template_parm_level (t, type, levels);
7429 break;
c8094d83 7430
ae58fa02 7431 default:
315fb5db 7432 gcc_unreachable ();
ae58fa02
MM
7433 }
7434
5566b478 7435 return r;
ae58fa02 7436 }
5566b478 7437
8d08fdba
MS
7438 case TREE_LIST:
7439 {
058b15c1 7440 tree purpose, value, chain;
8d08fdba
MS
7441
7442 if (t == void_list_node)
7443 return t;
7444
8d08fdba
MS
7445 purpose = TREE_PURPOSE (t);
7446 if (purpose)
4393e105
MM
7447 {
7448 purpose = tsubst (purpose, args, complain, in_decl);
7449 if (purpose == error_mark_node)
7450 return error_mark_node;
7451 }
8d08fdba
MS
7452 value = TREE_VALUE (t);
7453 if (value)
4393e105
MM
7454 {
7455 value = tsubst (value, args, complain, in_decl);
7456 if (value == error_mark_node)
7457 return error_mark_node;
7458 }
8d08fdba
MS
7459 chain = TREE_CHAIN (t);
7460 if (chain && chain != void_type_node)
4393e105
MM
7461 {
7462 chain = tsubst (chain, args, complain, in_decl);
7463 if (chain == error_mark_node)
7464 return error_mark_node;
7465 }
8d08fdba
MS
7466 if (purpose == TREE_PURPOSE (t)
7467 && value == TREE_VALUE (t)
7468 && chain == TREE_CHAIN (t))
7469 return t;
058b15c1 7470 return hash_tree_cons (purpose, value, chain);
8d08fdba 7471 }
c8094d83 7472
95b4aca6 7473 case TREE_BINFO:
bd7eccc4 7474 /* We should never be tsubsting a binfo. */
315fb5db 7475 gcc_unreachable ();
85b71cf2 7476
95b4aca6
NS
7477 case TREE_VEC:
7478 /* A vector of template arguments. */
50bc768d 7479 gcc_assert (!type);
a91db711 7480 return tsubst_template_args (t, args, complain, in_decl);
8d08fdba 7481
8d08fdba
MS
7482 case POINTER_TYPE:
7483 case REFERENCE_TYPE:
7484 {
8d08fdba 7485 enum tree_code code;
79a7c7fa 7486
46c895ac 7487 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8d08fdba
MS
7488 return t;
7489
7490 code = TREE_CODE (t);
4393e105
MM
7491
7492
7493 /* [temp.deduct]
c8094d83 7494
4393e105 7495 Type deduction may fail for any of the following
c8094d83 7496 reasons:
4393e105
MM
7497
7498 -- Attempting to create a pointer to reference type.
7499 -- Attempting to create a reference to a reference type or
7500 a reference to void. */
0ecfe0b4
JM
7501 if (TREE_CODE (type) == REFERENCE_TYPE
7502 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa 7503 {
82a98427 7504 static location_t last_loc;
79a7c7fa
JM
7505
7506 /* We keep track of the last time we issued this error
7507 message to avoid spewing a ton of messages during a
7508 single bad template instantiation. */
c2ea3a40 7509 if (complain & tf_error
93409b8c 7510#ifdef USE_MAPPED_LOCATION
9b60dfd7 7511 && last_loc != input_location
93409b8c 7512#else
82a98427 7513 && (last_loc.line != input_line
9b60dfd7 7514 || last_loc.file != input_filename)
93409b8c 7515#endif
9b60dfd7 7516 )
79a7c7fa 7517 {
0ecfe0b4 7518 if (TREE_CODE (type) == VOID_TYPE)
33bd39a2 7519 error ("forming reference to void");
0ecfe0b4 7520 else
0f51ccfc 7521 error ("forming %s to reference type %qT",
0cbd7506
MS
7522 (code == POINTER_TYPE) ? "pointer" : "reference",
7523 type);
82a98427 7524 last_loc = input_location;
79a7c7fa
JM
7525 }
7526
4393e105 7527 return error_mark_node;
79a7c7fa
JM
7528 }
7529 else if (code == POINTER_TYPE)
46c895ac
NS
7530 {
7531 r = build_pointer_type (type);
7532 if (TREE_CODE (type) == METHOD_TYPE)
7533 r = build_ptrmemfunc_type (r);
7534 }
8d08fdba
MS
7535 else
7536 r = build_reference_type (type);
adecb3f4 7537 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
79a7c7fa 7538
a5f1c5f8
NS
7539 if (r != error_mark_node)
7540 /* Will this ever be needed for TYPE_..._TO values? */
7541 layout_type (r);
c8094d83 7542
8d08fdba
MS
7543 return r;
7544 }
a4443a08 7545 case OFFSET_TYPE:
0ecfe0b4 7546 {
4393e105
MM
7547 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7548 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7549 {
7550 /* [temp.deduct]
7551
7552 Type deduction may fail for any of the following
7553 reasons:
c8094d83 7554
4393e105 7555 -- Attempting to create "pointer to member of T" when T
0cbd7506 7556 is not a class type. */
c2ea3a40 7557 if (complain & tf_error)
0f51ccfc 7558 error ("creating pointer to member of non-class type %qT", r);
4393e105
MM
7559 return error_mark_node;
7560 }
46c895ac
NS
7561 if (TREE_CODE (type) == REFERENCE_TYPE)
7562 {
4f09be91 7563 if (complain & tf_error)
0f51ccfc 7564 error ("creating pointer to member reference type %qT", type);
cb6d4a9f
VR
7565 return error_mark_node;
7566 }
7567 if (TREE_CODE (type) == VOID_TYPE)
7568 {
7569 if (complain & tf_error)
7570 error ("creating pointer to member of type void");
46c895ac
NS
7571 return error_mark_node;
7572 }
50bc768d 7573 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
46c895ac 7574 if (TREE_CODE (type) == FUNCTION_TYPE)
a5ac359a 7575 {
0cbd7506
MS
7576 /* The type of the implicit object parameter gets its
7577 cv-qualifiers from the FUNCTION_TYPE. */
a5ac359a 7578 tree method_type;
0cbd7506
MS
7579 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7580 cp_type_quals (type));
7581 tree memptr;
7582 method_type = build_method_type_directly (this_type,
43dc123f
MM
7583 TREE_TYPE (type),
7584 TYPE_ARG_TYPES (type));
0cbd7506
MS
7585 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7586 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7587 complain);
a5ac359a 7588 }
46c895ac 7589 else
b7a78333
MM
7590 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7591 TYPE_QUALS (t),
7592 complain);
0ecfe0b4 7593 }
8d08fdba
MS
7594 case FUNCTION_TYPE:
7595 case METHOD_TYPE:
7596 {
c11b6f21 7597 tree fntype;
c7222c02 7598 tree specs;
4393e105
MM
7599 fntype = tsubst_function_type (t, args, complain, in_decl);
7600 if (fntype == error_mark_node)
7601 return error_mark_node;
cabc336a 7602
34cd5ae7 7603 /* Substitute the exception specification. */
c8094d83 7604 specs = tsubst_exception_specification (t, args, complain,
c7222c02 7605 in_decl);
9f6206d9
VR
7606 if (specs == error_mark_node)
7607 return error_mark_node;
c7222c02
MM
7608 if (specs)
7609 fntype = build_exception_variant (fntype, specs);
c11b6f21 7610 return fntype;
8d08fdba
MS
7611 }
7612 case ARRAY_TYPE:
7613 {
4393e105
MM
7614 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7615 if (domain == error_mark_node)
7616 return error_mark_node;
7617
7618 /* As an optimization, we avoid regenerating the array type if
7619 it will obviously be the same as T. */
8d08fdba
MS
7620 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7621 return t;
0ecfe0b4 7622
c8094d83 7623 /* These checks should match the ones in grokdeclarator.
4393e105 7624
c8094d83
MS
7625 [temp.deduct]
7626
7627 The deduction may fail for any of the following reasons:
4393e105
MM
7628
7629 -- Attempting to create an array with an element type that
c8094d83 7630 is void, a function type, or a reference type, or [DR337]
cfb91b67 7631 an abstract class type. */
c8094d83 7632 if (TREE_CODE (type) == VOID_TYPE
4393e105
MM
7633 || TREE_CODE (type) == FUNCTION_TYPE
7634 || TREE_CODE (type) == REFERENCE_TYPE)
0ecfe0b4 7635 {
c2ea3a40 7636 if (complain & tf_error)
0f51ccfc 7637 error ("creating array of %qT", type);
4393e105 7638 return error_mark_node;
0ecfe0b4 7639 }
cfb91b67
GB
7640 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7641 {
7642 if (complain & tf_error)
c8094d83 7643 error ("creating array of %qT, which is an abstract class type",
cfb91b67 7644 type);
c8094d83 7645 return error_mark_node;
cfb91b67 7646 }
0ecfe0b4 7647
8d08fdba
MS
7648 r = build_cplus_array_type (type, domain);
7649 return r;
7650 }
7651
8d08fdba 7652 case PLUS_EXPR:
5566b478 7653 case MINUS_EXPR:
4393e105 7654 {
c2ea3a40
NS
7655 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7656 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
4393e105
MM
7657
7658 if (e1 == error_mark_node || e2 == error_mark_node)
7659 return error_mark_node;
7660
7866705a 7661 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
4393e105 7662 }
8d08fdba
MS
7663
7664 case NEGATE_EXPR:
7665 case NOP_EXPR:
4393e105 7666 {
c2ea3a40 7667 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
7668 if (e == error_mark_node)
7669 return error_mark_node;
7670
7866705a 7671 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
4393e105 7672 }
8d08fdba 7673
5566b478
MS
7674 case TYPENAME_TYPE:
7675 {
4393e105
MM
7676 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7677 in_decl, /*entering_scope=*/1);
7678 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
c8094d83 7679 complain, in_decl);
4393e105
MM
7680
7681 if (ctx == error_mark_node || f == error_mark_node)
7682 return error_mark_node;
ae58fa02 7683
bf8f3f93
MM
7684 if (!IS_AGGR_TYPE (ctx))
7685 {
c2ea3a40 7686 if (complain & tf_error)
0f51ccfc 7687 error ("%qT is not a class, struct, or union type", ctx);
bf8f3f93
MM
7688 return error_mark_node;
7689 }
7690 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7691 {
7692 /* Normally, make_typename_type does not require that the CTX
7693 have complete type in order to allow things like:
c8094d83 7694
0cbd7506 7695 template <class T> struct S { typename S<T>::X Y; };
ae58fa02 7696
bf8f3f93
MM
7697 But, such constructs have already been resolved by this
7698 point, so here CTX really should have complete type, unless
7699 it's a partial instantiation. */
4393e105 7700 ctx = complete_type (ctx);
d0f062fb 7701 if (!COMPLETE_TYPE_P (ctx))
4393e105 7702 {
c2ea3a40 7703 if (complain & tf_error)
7a228918 7704 cxx_incomplete_type_error (NULL_TREE, ctx);
4393e105
MM
7705 return error_mark_node;
7706 }
7707 }
ae58fa02 7708
fc6a28d7 7709 f = make_typename_type (ctx, f, typename_type,
4f2b0fb2 7710 (complain & tf_error) | tf_keep_type_decl);
f0bcd168
MM
7711 if (f == error_mark_node)
7712 return f;
0cbd7506
MS
7713 if (TREE_CODE (f) == TYPE_DECL)
7714 {
4f2b0fb2 7715 complain |= tf_ignore_bad_quals;
0cbd7506
MS
7716 f = TREE_TYPE (f);
7717 }
c8094d83 7718
fc6a28d7
MM
7719 if (TREE_CODE (f) != TYPENAME_TYPE)
7720 {
7721 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
c8094d83 7722 error ("%qT resolves to %qT, which is not an enumeration type",
fc6a28d7
MM
7723 t, f);
7724 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
c8094d83 7725 error ("%qT resolves to %qT, which is is not a class type",
fc6a28d7
MM
7726 t, f);
7727 }
7728
0cbd7506
MS
7729 return cp_build_qualified_type_real
7730 (f, cp_type_quals (f) | cp_type_quals (t), complain);
5566b478 7731 }
c8094d83 7732
b8c6534b
KL
7733 case UNBOUND_CLASS_TEMPLATE:
7734 {
7735 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7736 in_decl, /*entering_scope=*/1);
7737 tree name = TYPE_IDENTIFIER (t);
b939a023 7738 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
b8c6534b
KL
7739
7740 if (ctx == error_mark_node || name == error_mark_node)
7741 return error_mark_node;
7742
b939a023
KL
7743 if (parm_list)
7744 parm_list = tsubst_template_parms (parm_list, args, complain);
7745 return make_unbound_class_template (ctx, name, parm_list, complain);
b8c6534b
KL
7746 }
7747
5566b478 7748 case INDIRECT_REF:
5566b478 7749 case ADDR_EXPR:
058b15c1 7750 case CALL_EXPR:
315fb5db 7751 gcc_unreachable ();
5566b478
MS
7752
7753 case ARRAY_REF:
4393e105 7754 {
c2ea3a40 7755 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
015c2c66
MM
7756 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
7757 /*integral_constant_expression_p=*/false);
4393e105
MM
7758 if (e1 == error_mark_node || e2 == error_mark_node)
7759 return error_mark_node;
7760
44de5aeb 7761 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
4393e105 7762 }
5566b478 7763
fc378698 7764 case SCOPE_REF:
4393e105 7765 {
c2ea3a40 7766 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
7767 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7768 if (e1 == error_mark_node || e2 == error_mark_node)
7769 return error_mark_node;
7770
02ed62dd
MM
7771 return build_qualified_name (/*type=*/NULL_TREE,
7772 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
4393e105 7773 }
fc378698 7774
b894fc05 7775 case TYPEOF_TYPE:
4393e105 7776 {
b830b74c 7777 tree type;
4393e105 7778
015c2c66
MM
7779 type = finish_typeof (tsubst_expr
7780 (TYPEOF_TYPE_EXPR (t), args,
7781 complain, in_decl,
7782 /*integral_constant_expression_p=*/false));
b830b74c 7783 return cp_build_qualified_type_real (type,
dce50630 7784 cp_type_quals (t)
b830b74c 7785 | cp_type_quals (type),
dce50630 7786 complain);
4393e105 7787 }
b894fc05 7788
8d08fdba 7789 default:
9e637a26 7790 sorry ("use of %qs in template",
8d08fdba
MS
7791 tree_code_name [(int) TREE_CODE (t)]);
7792 return error_mark_node;
7793 }
7794}
7795
ee76b931
MM
7796/* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7797 type of the expression on the left-hand side of the "." or "->"
7798 operator. */
7799
7800static tree
7801tsubst_baselink (tree baselink, tree object_type,
7802 tree args, tsubst_flags_t complain, tree in_decl)
7803{
7804 tree name;
7805 tree qualifying_scope;
7806 tree fns;
6f67f000 7807 tree optype;
ee76b931
MM
7808 tree template_args = 0;
7809 bool template_id_p = false;
7810
51a203d9
MM
7811 /* A baselink indicates a function from a base class. Both the
7812 BASELINK_ACCESS_BINFO and the base class referenced may
7813 indicate bases of the template class, rather than the
7814 instantiated class. In addition, lookups that were not
7815 ambiguous before may be ambiguous now. Therefore, we perform
7816 the lookup again. */
ee76b931 7817 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
51a203d9
MM
7818 qualifying_scope = tsubst (qualifying_scope, args,
7819 complain, in_decl);
ee76b931 7820 fns = BASELINK_FUNCTIONS (baselink);
6f67f000 7821 optype = BASELINK_OPTYPE (baselink);
ee76b931
MM
7822 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7823 {
7824 template_id_p = true;
7825 template_args = TREE_OPERAND (fns, 1);
7826 fns = TREE_OPERAND (fns, 0);
bf12d54d
NS
7827 if (template_args)
7828 template_args = tsubst_template_args (template_args, args,
7829 complain, in_decl);
ee76b931
MM
7830 }
7831 name = DECL_NAME (get_first_fn (fns));
7832 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
c8094d83 7833
9b60dfd7
MM
7834 /* If lookup found a single function, mark it as used at this
7835 point. (If it lookup found multiple functions the one selected
7836 later by overload resolution will be marked as used at that
7837 point.) */
7838 if (BASELINK_P (baselink))
7839 fns = BASELINK_FUNCTIONS (baselink);
7840 if (!template_id_p && !really_overloaded_fn (fns))
7841 mark_used (OVL_CURRENT (fns));
7842
7843 /* Add back the template arguments, if present. */
ee76b931 7844 if (BASELINK_P (baselink) && template_id_p)
c8094d83 7845 BASELINK_FUNCTIONS (baselink)
ee76b931
MM
7846 = build_nt (TEMPLATE_ID_EXPR,
7847 BASELINK_FUNCTIONS (baselink),
7848 template_args);
6f67f000
MM
7849 /* Update the conversion operator type. */
7850 BASELINK_OPTYPE (baselink)
7851 = tsubst (optype, args, complain, in_decl);
9b60dfd7 7852
ee76b931
MM
7853 if (!object_type)
7854 object_type = current_class_type;
c8094d83 7855 return adjust_result_of_qualified_name_lookup (baselink,
ee76b931
MM
7856 qualifying_scope,
7857 object_type);
7858}
7859
7860/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7861 true if the qualified-id will be a postfix-expression in-and-of
7862 itself; false if more of the postfix-expression follows the
7863 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7864 of "&". */
7865
7866static tree
c8094d83 7867tsubst_qualified_id (tree qualified_id, tree args,
ee76b931
MM
7868 tsubst_flags_t complain, tree in_decl,
7869 bool done, bool address_p)
7870{
7871 tree expr;
7872 tree scope;
7873 tree name;
7874 bool is_template;
7875 tree template_args;
7876
50bc768d 7877 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
ee76b931 7878
ee76b931
MM
7879 /* Figure out what name to look up. */
7880 name = TREE_OPERAND (qualified_id, 1);
7881 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7882 {
7883 is_template = true;
bf12d54d
NS
7884 template_args = TREE_OPERAND (name, 1);
7885 if (template_args)
7886 template_args = tsubst_template_args (template_args, args,
7887 complain, in_decl);
ee76b931
MM
7888 name = TREE_OPERAND (name, 0);
7889 }
7890 else
7891 {
7892 is_template = false;
7893 template_args = NULL_TREE;
7894 }
7895
6eeba0cc
MM
7896 /* Substitute into the qualifying scope. When there are no ARGS, we
7897 are just trying to simplify a non-dependent expression. In that
7898 case the qualifying scope may be dependent, and, in any case,
7899 substituting will not help. */
7900 scope = TREE_OPERAND (qualified_id, 0);
7901 if (args)
7902 {
7903 scope = tsubst (scope, args, complain, in_decl);
7904 expr = tsubst_copy (name, args, complain, in_decl);
7905 }
7906 else
7907 expr = name;
10b1d5e7 7908
ab73670a 7909 if (dependent_type_p (scope))
3db45ab5
MS
7910 return build_qualified_name (/*type=*/NULL_TREE,
7911 scope, expr,
02ed62dd 7912 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
c8094d83 7913
5e08432e 7914 if (!BASELINK_P (name) && !DECL_P (expr))
12483c9f 7915 {
8ca4bf25
MM
7916 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7917 /* If this were actually a destructor call, it would have been
7918 parsed as such by the parser. */
7919 expr = error_mark_node;
7920 else
7921 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12483c9f
NS
7922 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7923 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7924 {
7925 if (complain & tf_error)
b2693faf 7926 {
0f51ccfc 7927 error ("dependent-name %qE is parsed as a non-type, but "
b2693faf 7928 "instantiation yields a type", qualified_id);
0f51ccfc 7929 inform ("say %<typename %E%> if a type is meant", qualified_id);
b2693faf 7930 }
12483c9f
NS
7931 return error_mark_node;
7932 }
7933 }
c8094d83 7934
279b8466 7935 if (DECL_P (expr))
8f78f01f
MM
7936 {
7937 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7938 scope);
7939 /* Remember that there was a reference to this entity. */
7940 mark_used (expr);
7941 }
7942
7943 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7944 {
7945 if (complain & tf_error)
c8094d83 7946 qualified_name_lookup_error (scope,
8f78f01f
MM
7947 TREE_OPERAND (qualified_id, 1),
7948 expr);
7949 return error_mark_node;
7950 }
ee76b931
MM
7951
7952 if (is_template)
10b1d5e7 7953 expr = lookup_template_function (expr, template_args);
ee76b931 7954
22038b2c 7955 if (expr == error_mark_node && complain & tf_error)
8f78f01f
MM
7956 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7957 expr);
22038b2c 7958 else if (TYPE_P (scope))
ee76b931 7959 {
c8094d83 7960 expr = (adjust_result_of_qualified_name_lookup
ee76b931 7961 (expr, scope, current_class_type));
3db45ab5 7962 expr = (finish_qualified_id_expr
02ed62dd
MM
7963 (scope, expr, done, address_p,
7964 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
7965 /*template_arg_p=*/false));
ee76b931 7966 }
c8094d83 7967
015c2c66
MM
7968 /* Expressions do not generally have reference type. */
7969 if (TREE_CODE (expr) != SCOPE_REF
7970 /* However, if we're about to form a pointer-to-member, we just
7971 want the referenced member referenced. */
7972 && TREE_CODE (expr) != OFFSET_REF)
aec5ba60 7973 expr = convert_from_reference (expr);
ee76b931
MM
7974
7975 return expr;
7976}
7977
00d3396f
JM
7978/* Like tsubst, but deals with expressions. This function just replaces
7979 template parms; to finish processing the resultant expression, use
7980 tsubst_expr. */
7981
14d22dd6 7982static tree
3a978d72 7983tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5566b478
MS
7984{
7985 enum tree_code code;
8452b1d3 7986 tree r;
8d08fdba 7987
5566b478
MS
7988 if (t == NULL_TREE || t == error_mark_node)
7989 return t;
7990
7991 code = TREE_CODE (t);
b7484fbe 7992
5566b478
MS
7993 switch (code)
7994 {
7995 case PARM_DECL:
a723baf1 7996 r = retrieve_local_specialization (t);
50bc768d 7997 gcc_assert (r != NULL);
c0694c4b 7998 mark_used (r);
a723baf1 7999 return r;
5566b478
MS
8000
8001 case CONST_DECL:
ed44da02
MM
8002 {
8003 tree enum_type;
8004 tree v;
8005
a723baf1
MM
8006 if (DECL_TEMPLATE_PARM_P (t))
8007 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7b6d72fc
MM
8008 /* There is no need to substitute into namespace-scope
8009 enumerators. */
8010 if (DECL_NAMESPACE_SCOPE_P (t))
ed44da02 8011 return t;
d5a10cf0
MM
8012 /* If ARGS is NULL, then T is known to be non-dependent. */
8013 if (args == NULL_TREE)
8a784e4a 8014 return integral_constant_value (t);
ed44da02
MM
8015
8016 /* Unfortunately, we cannot just call lookup_name here.
9188c363 8017 Consider:
c8094d83 8018
9188c363
MM
8019 template <int I> int f() {
8020 enum E { a = I };
8021 struct S { void g() { E e = a; } };
8022 };
c8094d83 8023
9188c363
MM
8024 When we instantiate f<7>::S::g(), say, lookup_name is not
8025 clever enough to find f<7>::a. */
c8094d83
MS
8026 enum_type
8027 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
ed44da02
MM
8028 /*entering_scope=*/0);
8029
c8094d83
MS
8030 for (v = TYPE_VALUES (enum_type);
8031 v != NULL_TREE;
ed44da02
MM
8032 v = TREE_CHAIN (v))
8033 if (TREE_PURPOSE (v) == DECL_NAME (t))
8034 return TREE_VALUE (v);
8035
8036 /* We didn't find the name. That should never happen; if
8037 name-lookup found it during preliminary parsing, we
8038 should find it again here during instantiation. */
315fb5db 8039 gcc_unreachable ();
ed44da02 8040 }
db04386f 8041 return t;
ed44da02 8042
5566b478
MS
8043 case FIELD_DECL:
8044 if (DECL_CONTEXT (t))
8045 {
0978790f 8046 tree ctx;
0978790f 8047
4393e105 8048 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
36a117a5 8049 /*entering_scope=*/1);
0978790f 8050 if (ctx != DECL_CONTEXT (t))
bad1f462
KL
8051 {
8052 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
8053 if (!r)
8054 {
8055 if (complain & tf_error)
8056 error ("using invalid field %qD", t);
8057 return error_mark_node;
8058 }
8059 return r;
8060 }
5566b478 8061 }
c8094d83 8062
5566b478
MS
8063 return t;
8064
8065 case VAR_DECL:
8066 case FUNCTION_DECL:
a723baf1
MM
8067 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8068 || local_variable_p (t))
4393e105 8069 t = tsubst (t, args, complain, in_decl);
5566b478
MS
8070 mark_used (t);
8071 return t;
8072
a723baf1 8073 case BASELINK:
ee76b931 8074 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
a723baf1 8075
98c1c668 8076 case TEMPLATE_DECL:
a723baf1 8077 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
c8094d83 8078 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
a723baf1 8079 args, complain, in_decl);
c7222c02 8080 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
4393e105 8081 return tsubst (t, args, complain, in_decl);
fcea7401
KL
8082 else if (DECL_CLASS_SCOPE_P (t)
8083 && uses_template_parms (DECL_CONTEXT (t)))
8084 {
8085 /* Template template argument like the following example need
8086 special treatment:
8087
8088 template <template <class> class TT> struct C {};
8089 template <class T> struct D {
8090 template <class U> struct E {};
0cbd7506 8091 C<E> c; // #1
fcea7401
KL
8092 };
8093 D<int> d; // #2
8094
8095 We are processing the template argument `E' in #1 for
8096 the template instantiation #2. Originally, `E' is a
8097 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
8098 have to substitute this with one having context `D<int>'. */
8099
8100 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
8101 return lookup_field (context, DECL_NAME(t), 0, false);
8102 }
98c1c668 8103 else
fcea7401 8104 /* Ordinary template template argument. */
98c1c668
JM
8105 return t;
8106
5566b478
MS
8107 case CAST_EXPR:
8108 case REINTERPRET_CAST_EXPR:
e92cc029
MS
8109 case CONST_CAST_EXPR:
8110 case STATIC_CAST_EXPR:
8111 case DYNAMIC_CAST_EXPR:
51924768 8112 case NOP_EXPR:
5566b478 8113 return build1
4393e105
MM
8114 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8115 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478
MS
8116
8117 case INDIRECT_REF:
5566b478
MS
8118 case NEGATE_EXPR:
8119 case TRUTH_NOT_EXPR:
b87692e5 8120 case BIT_NOT_EXPR:
5566b478 8121 case ADDR_EXPR:
392e3d51 8122 case UNARY_PLUS_EXPR: /* Unary + */
5566b478 8123 case SIZEOF_EXPR:
abff8e06 8124 case ALIGNOF_EXPR:
5566b478 8125 case ARROW_EXPR:
fc378698 8126 case THROW_EXPR:
5156628f 8127 case TYPEID_EXPR:
f5733617
SS
8128 case REALPART_EXPR:
8129 case IMAGPART_EXPR:
5566b478 8130 return build1
6a629cac 8131 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
4393e105 8132 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478 8133
a723baf1
MM
8134 case COMPONENT_REF:
8135 {
8136 tree object;
8137 tree name;
8138
8139 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8140 name = TREE_OPERAND (t, 1);
c8094d83 8141 if (TREE_CODE (name) == BIT_NOT_EXPR)
a723baf1
MM
8142 {
8143 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8144 complain, in_decl);
8145 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8146 }
8147 else if (TREE_CODE (name) == SCOPE_REF
8148 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
8149 {
8150 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
8151 complain, in_decl);
8152 name = TREE_OPERAND (name, 1);
8153 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8154 complain, in_decl);
8155 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
02ed62dd 8156 name = build_qualified_name (/*type=*/NULL_TREE,
3db45ab5 8157 base, name,
02ed62dd 8158 /*template_p=*/false);
a723baf1 8159 }
ee76b931 8160 else if (TREE_CODE (name) == BASELINK)
c8094d83
MS
8161 name = tsubst_baselink (name,
8162 non_reference (TREE_TYPE (object)),
8163 args, complain,
ee76b931 8164 in_decl);
a723baf1 8165 else
ee76b931 8166 name = tsubst_copy (name, args, complain, in_decl);
44de5aeb 8167 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
a723baf1
MM
8168 }
8169
5566b478
MS
8170 case PLUS_EXPR:
8171 case MINUS_EXPR:
8172 case MULT_EXPR:
8173 case TRUNC_DIV_EXPR:
8174 case CEIL_DIV_EXPR:
8175 case FLOOR_DIV_EXPR:
8176 case ROUND_DIV_EXPR:
8177 case EXACT_DIV_EXPR:
8178 case BIT_AND_EXPR:
5566b478
MS
8179 case BIT_IOR_EXPR:
8180 case BIT_XOR_EXPR:
8181 case TRUNC_MOD_EXPR:
8182 case FLOOR_MOD_EXPR:
8183 case TRUTH_ANDIF_EXPR:
8184 case TRUTH_ORIF_EXPR:
8185 case TRUTH_AND_EXPR:
8186 case TRUTH_OR_EXPR:
8187 case RSHIFT_EXPR:
8188 case LSHIFT_EXPR:
8189 case RROTATE_EXPR:
8190 case LROTATE_EXPR:
8191 case EQ_EXPR:
8192 case NE_EXPR:
8193 case MAX_EXPR:
8194 case MIN_EXPR:
8195 case LE_EXPR:
8196 case GE_EXPR:
8197 case LT_EXPR:
8198 case GT_EXPR:
5566b478 8199 case COMPOUND_EXPR:
5566b478
MS
8200 case DOTSTAR_EXPR:
8201 case MEMBER_REF:
519c9806
MM
8202 case PREDECREMENT_EXPR:
8203 case PREINCREMENT_EXPR:
8204 case POSTDECREMENT_EXPR:
8205 case POSTINCREMENT_EXPR:
5566b478 8206 return build_nt
4393e105
MM
8207 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8208 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478 8209
02ed62dd
MM
8210 case SCOPE_REF:
8211 return build_qualified_name (/*type=*/NULL_TREE,
8212 tsubst_copy (TREE_OPERAND (t, 0),
8213 args, complain, in_decl),
8214 tsubst_copy (TREE_OPERAND (t, 1),
8215 args, complain, in_decl),
8216 QUALIFIED_NAME_IS_TEMPLATE (t));
8217
d8987adb
NS
8218 case ARRAY_REF:
8219 return build_nt
8220 (ARRAY_REF,
8221 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8222 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8223 NULL_TREE, NULL_TREE);
8224
5566b478 8225 case CALL_EXPR:
c8094d83 8226 return build_nt (code,
a723baf1
MM
8227 tsubst_copy (TREE_OPERAND (t, 0), args,
8228 complain, in_decl),
8229 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
8230 in_decl),
8231 NULL_TREE);
5566b478 8232
5566b478
MS
8233 case COND_EXPR:
8234 case MODOP_EXPR:
40242ccf 8235 case PSEUDO_DTOR_EXPR:
67da3287 8236 {
8452b1d3 8237 r = build_nt
4393e105
MM
8238 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8239 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8240 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
e4c2c34b 8241 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
67da3287
MM
8242 return r;
8243 }
5566b478
MS
8244
8245 case NEW_EXPR:
8246 {
8452b1d3 8247 r = build_nt
4393e105
MM
8248 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8249 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8250 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
5566b478
MS
8251 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
8252 return r;
8253 }
8254
8255 case DELETE_EXPR:
8256 {
8452b1d3 8257 r = build_nt
4393e105
MM
8258 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8259 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
8260 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
8261 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
8262 return r;
8263 }
8264
386b8a85
JM
8265 case TEMPLATE_ID_EXPR:
8266 {
0cbd7506 8267 /* Substituted template arguments */
a91db711
NS
8268 tree fn = TREE_OPERAND (t, 0);
8269 tree targs = TREE_OPERAND (t, 1);
856216bb 8270
a91db711 8271 fn = tsubst_copy (fn, args, complain, in_decl);
bf12d54d
NS
8272 if (targs)
8273 targs = tsubst_template_args (targs, args, complain, in_decl);
c8094d83 8274
a91db711 8275 return lookup_template_function (fn, targs);
386b8a85
JM
8276 }
8277
5566b478
MS
8278 case TREE_LIST:
8279 {
8280 tree purpose, value, chain;
8281
8282 if (t == void_list_node)
8283 return t;
8284
8285 purpose = TREE_PURPOSE (t);
8286 if (purpose)
4393e105 8287 purpose = tsubst_copy (purpose, args, complain, in_decl);
5566b478
MS
8288 value = TREE_VALUE (t);
8289 if (value)
4393e105 8290 value = tsubst_copy (value, args, complain, in_decl);
5566b478
MS
8291 chain = TREE_CHAIN (t);
8292 if (chain && chain != void_type_node)
4393e105 8293 chain = tsubst_copy (chain, args, complain, in_decl);
5566b478
MS
8294 if (purpose == TREE_PURPOSE (t)
8295 && value == TREE_VALUE (t)
8296 && chain == TREE_CHAIN (t))
8297 return t;
8298 return tree_cons (purpose, value, chain);
8299 }
8300
8301 case RECORD_TYPE:
8302 case UNION_TYPE:
8303 case ENUMERAL_TYPE:
8304 case INTEGER_TYPE:
8305 case TEMPLATE_TYPE_PARM:
73b0fce8 8306 case TEMPLATE_TEMPLATE_PARM:
a1281f45 8307 case BOUND_TEMPLATE_TEMPLATE_PARM:
f84b4be9 8308 case TEMPLATE_PARM_INDEX:
5566b478
MS
8309 case POINTER_TYPE:
8310 case REFERENCE_TYPE:
8311 case OFFSET_TYPE:
8312 case FUNCTION_TYPE:
8313 case METHOD_TYPE:
8314 case ARRAY_TYPE:
8315 case TYPENAME_TYPE:
b8c6534b 8316 case UNBOUND_CLASS_TEMPLATE:
2a2b1d56 8317 case TYPEOF_TYPE:
f84b4be9 8318 case TYPE_DECL:
4393e105 8319 return tsubst (t, args, complain, in_decl);
5566b478 8320
e92cc029 8321 case IDENTIFIER_NODE:
421844e7 8322 if (IDENTIFIER_TYPENAME_P (t))
1f6e1acc
AS
8323 {
8324 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
1f84ec23 8325 return mangle_conv_op_name_for_type (new_type);
1f6e1acc 8326 }
e92cc029
MS
8327 else
8328 return t;
8329
5156628f 8330 case CONSTRUCTOR:
4038c495
GB
8331 /* This is handled by tsubst_copy_and_build. */
8332 gcc_unreachable ();
5156628f 8333
371534a9 8334 case VA_ARG_EXPR:
ea333e1c 8335 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
c2ea3a40
NS
8336 in_decl),
8337 tsubst (TREE_TYPE (t), args, complain, in_decl));
f9817201 8338
543a0daa
RH
8339 case CLEANUP_POINT_EXPR:
8340 /* We shouldn't have built any of these during initial template
8341 generation. Instead, they should be built during instantiation
8342 in response to the saved STMT_IS_FULL_EXPR_P setting. */
315fb5db 8343 gcc_unreachable ();
543a0daa 8344
7eab6e7b
MM
8345 case OFFSET_REF:
8346 mark_used (TREE_OPERAND (t, 1));
8347 return t;
8348
5566b478
MS
8349 default:
8350 return t;
8351 }
8352}
8353
1799e5d5
RH
8354/* Like tsubst_copy, but specifically for OpenMP clauses. */
8355
8356static tree
8357tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8358 tree in_decl)
8359{
8360 tree new_clauses = NULL, nc, oc;
8361
8362 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8363 {
8364 nc = copy_node (oc);
8365 OMP_CLAUSE_CHAIN (nc) = new_clauses;
8366 new_clauses = nc;
8367
8368 switch (OMP_CLAUSE_CODE (nc))
8369 {
8370 case OMP_CLAUSE_PRIVATE:
8371 case OMP_CLAUSE_SHARED:
8372 case OMP_CLAUSE_FIRSTPRIVATE:
8373 case OMP_CLAUSE_LASTPRIVATE:
8374 case OMP_CLAUSE_REDUCTION:
8375 case OMP_CLAUSE_COPYIN:
8376 case OMP_CLAUSE_COPYPRIVATE:
8377 case OMP_CLAUSE_IF:
8378 case OMP_CLAUSE_NUM_THREADS:
8379 case OMP_CLAUSE_SCHEDULE:
8380 OMP_CLAUSE_OPERAND (nc, 0)
015c2c66
MM
8381 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
8382 in_decl, /*integral_constant_expression_p=*/false);
1799e5d5
RH
8383 break;
8384 case OMP_CLAUSE_NOWAIT:
8385 case OMP_CLAUSE_ORDERED:
8386 case OMP_CLAUSE_DEFAULT:
8387 break;
8388 default:
8389 gcc_unreachable ();
8390 }
8391 }
8392
8393 return finish_omp_clauses (nreverse (new_clauses));
8394}
8395
f74dcfb7
JJ
8396/* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8397
8398static tree
8399tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8400 tree in_decl)
8401{
8402#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8403
8404 tree purpose, value, chain;
8405
8406 if (t == NULL)
8407 return t;
8408
8409 if (TREE_CODE (t) != TREE_LIST)
8410 return tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
8411 /*function_p=*/false,
8412 /*integral_constant_expression_p=*/false);
f74dcfb7
JJ
8413
8414 if (t == void_list_node)
8415 return t;
8416
8417 purpose = TREE_PURPOSE (t);
8418 if (purpose)
8419 purpose = RECUR (purpose);
8420 value = TREE_VALUE (t);
8421 if (value)
8422 value = RECUR (value);
8423 chain = TREE_CHAIN (t);
8424 if (chain && chain != void_type_node)
8425 chain = RECUR (chain);
8426 return tree_cons (purpose, value, chain);
8427#undef RECUR
8428}
8429
cc23546e
JO
8430/* Like tsubst_copy for expressions, etc. but also does semantic
8431 processing. */
00d3396f 8432
14d22dd6 8433static tree
015c2c66
MM
8434tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
8435 bool integral_constant_expression_p)
5566b478 8436{
015c2c66
MM
8437#define RECUR(NODE) \
8438 tsubst_expr ((NODE), args, complain, in_decl, \
8439 integral_constant_expression_p)
8440
fd10dd09 8441 tree stmt, tmp;
558475f0 8442
5566b478
MS
8443 if (t == NULL_TREE || t == error_mark_node)
8444 return t;
8445
93409b8c
PB
8446 if (EXPR_HAS_LOCATION (t))
8447 input_location = EXPR_LOCATION (t);
7c34ced1
RH
8448 if (STATEMENT_CODE_P (TREE_CODE (t)))
8449 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8450
5566b478 8451 switch (TREE_CODE (t))
8d08fdba 8452 {
325c3691
RH
8453 case STATEMENT_LIST:
8454 {
8455 tree_stmt_iterator i;
8456 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
015c2c66 8457 RECUR (tsi_stmt (i));
325c3691
RH
8458 break;
8459 }
8460
558475f0 8461 case CTOR_INITIALIZER:
c8094d83 8462 finish_mem_initializers (tsubst_initializer_list
2282d28d
MM
8463 (TREE_OPERAND (t, 0), args));
8464 break;
558475f0 8465
5088b058 8466 case RETURN_EXPR:
015c2c66 8467 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
5566b478
MS
8468 break;
8469
8470 case EXPR_STMT:
015c2c66 8471 tmp = RECUR (EXPR_STMT_EXPR (t));
7c34ced1
RH
8472 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8473 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8474 else
8475 finish_expr_stmt (tmp);
8476 break;
5566b478 8477
9da99f7d 8478 case USING_STMT:
015c2c66 8479 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
9da99f7d 8480 break;
c8094d83 8481
350fae66 8482 case DECL_EXPR:
5566b478 8483 {
e0942dcd
MM
8484 tree decl;
8485 tree init;
5566b478 8486
350fae66 8487 decl = DECL_EXPR_DECL (t);
acef433b
MM
8488 if (TREE_CODE (decl) == LABEL_DECL)
8489 finish_label_decl (DECL_NAME (decl));
fbfe8c9e
NS
8490 else if (TREE_CODE (decl) == USING_DECL)
8491 {
98ed9dae 8492 tree scope = USING_DECL_SCOPE (decl);
fbfe8c9e 8493 tree name = DECL_NAME (decl);
22038b2c 8494 tree decl;
c8094d83 8495
015c2c66 8496 scope = RECUR (scope);
22038b2c 8497 decl = lookup_qualified_name (scope, name,
12483c9f
NS
8498 /*is_type_p=*/false,
8499 /*complain=*/false);
8f78f01f
MM
8500 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8501 qualified_name_lookup_error (scope, name, decl);
22038b2c 8502 else
ed5f054f 8503 do_local_using_decl (decl, scope, name);
fbfe8c9e 8504 }
acef433b
MM
8505 else
8506 {
8507 init = DECL_INITIAL (decl);
8508 decl = tsubst (decl, args, complain, in_decl);
ce2e5191
NS
8509 if (decl != error_mark_node)
8510 {
0cbd7506
MS
8511 /* By marking the declaration as instantiated, we avoid
8512 trying to instantiate it. Since instantiate_decl can't
8513 handle local variables, and since we've already done
8514 all that needs to be done, that's the right thing to
8515 do. */
8516 if (TREE_CODE (decl) == VAR_DECL)
8517 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
edebf865
MM
8518 if (TREE_CODE (decl) == VAR_DECL
8519 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8520 /* Anonymous aggregates are a special case. */
8521 finish_anon_union (decl);
c8094d83 8522 else
ed2fa432 8523 {
edebf865 8524 maybe_push_decl (decl);
39703eb9
MM
8525 if (TREE_CODE (decl) == VAR_DECL
8526 && DECL_PRETTY_FUNCTION_P (decl))
edebf865
MM
8527 {
8528 /* For __PRETTY_FUNCTION__ we have to adjust the
8529 initializer. */
8530 const char *const name
8531 = cxx_printable_name (current_function_decl, 2);
bb885938 8532 init = cp_fname_init (name, &TREE_TYPE (decl));
edebf865
MM
8533 }
8534 else
015c2c66 8535 init = RECUR (init);
d174af6c 8536 finish_decl (decl, init, NULL_TREE);
ed2fa432 8537 }
ce2e5191 8538 }
acef433b 8539 }
fd10dd09 8540
350fae66 8541 /* A DECL_EXPR can also be used as an expression, in the condition
325c3691
RH
8542 clause of an if/for/while construct. */
8543 return decl;
5566b478 8544 }
8d08fdba 8545
5566b478 8546 case FOR_STMT:
7c34ced1 8547 stmt = begin_for_stmt ();
015c2c66 8548 RECUR (FOR_INIT_STMT (t));
7c34ced1 8549 finish_for_init_stmt (stmt);
015c2c66 8550 tmp = RECUR (FOR_COND (t));
7c34ced1 8551 finish_for_cond (tmp, stmt);
015c2c66 8552 tmp = RECUR (FOR_EXPR (t));
7c34ced1 8553 finish_for_expr (tmp, stmt);
015c2c66 8554 RECUR (FOR_BODY (t));
7c34ced1 8555 finish_for_stmt (stmt);
5566b478 8556 break;
8d08fdba 8557
5566b478 8558 case WHILE_STMT:
7c34ced1 8559 stmt = begin_while_stmt ();
015c2c66 8560 tmp = RECUR (WHILE_COND (t));
7c34ced1 8561 finish_while_stmt_cond (tmp, stmt);
015c2c66 8562 RECUR (WHILE_BODY (t));
7c34ced1 8563 finish_while_stmt (stmt);
5566b478 8564 break;
8d08fdba 8565
5566b478 8566 case DO_STMT:
7c34ced1 8567 stmt = begin_do_stmt ();
015c2c66 8568 RECUR (DO_BODY (t));
7c34ced1 8569 finish_do_body (stmt);
015c2c66 8570 tmp = RECUR (DO_COND (t));
7c34ced1 8571 finish_do_stmt (tmp, stmt);
5566b478 8572 break;
a0a33927 8573
5566b478 8574 case IF_STMT:
7c34ced1 8575 stmt = begin_if_stmt ();
015c2c66 8576 tmp = RECUR (IF_COND (t));
7c34ced1 8577 finish_if_stmt_cond (tmp, stmt);
015c2c66 8578 RECUR (THEN_CLAUSE (t));
7c34ced1 8579 finish_then_clause (stmt);
8d08fdba 8580
7c34ced1
RH
8581 if (ELSE_CLAUSE (t))
8582 {
8583 begin_else_clause (stmt);
015c2c66 8584 RECUR (ELSE_CLAUSE (t));
7c34ced1
RH
8585 finish_else_clause (stmt);
8586 }
8587
8588 finish_if_stmt (stmt);
5566b478 8589 break;
8d08fdba 8590
5882f0f3 8591 case BIND_EXPR:
7c34ced1
RH
8592 if (BIND_EXPR_BODY_BLOCK (t))
8593 stmt = begin_function_body ();
8594 else
8595 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8596 ? BCS_TRY_BLOCK : 0);
ade3dc07 8597
015c2c66 8598 RECUR (BIND_EXPR_BODY (t));
ade3dc07 8599
7c34ced1
RH
8600 if (BIND_EXPR_BODY_BLOCK (t))
8601 finish_function_body (stmt);
8602 else
8603 finish_compound_stmt (stmt);
5566b478 8604 break;
8d08fdba 8605
5566b478 8606 case BREAK_STMT:
ad321293 8607 finish_break_stmt ();
5566b478 8608 break;
8d08fdba 8609
6467930b 8610 case CONTINUE_STMT:
ad321293 8611 finish_continue_stmt ();
6467930b
MS
8612 break;
8613
5566b478 8614 case SWITCH_STMT:
7c34ced1 8615 stmt = begin_switch_stmt ();
015c2c66 8616 tmp = RECUR (SWITCH_STMT_COND (t));
7c34ced1 8617 finish_switch_cond (tmp, stmt);
015c2c66 8618 RECUR (SWITCH_STMT_BODY (t));
7c34ced1 8619 finish_switch_stmt (stmt);
5566b478
MS
8620 break;
8621
8c161995 8622 case CASE_LABEL_EXPR:
015c2c66
MM
8623 finish_case_label (RECUR (CASE_LOW (t)),
8624 RECUR (CASE_HIGH (t)));
5566b478
MS
8625 break;
8626
9e14e18f 8627 case LABEL_EXPR:
9e14e18f 8628 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
5566b478
MS
8629 break;
8630
9e14e18f 8631 case GOTO_EXPR:
fd10dd09
JM
8632 tmp = GOTO_DESTINATION (t);
8633 if (TREE_CODE (tmp) != LABEL_DECL)
aa09da44
MM
8634 /* Computed goto's must be tsubst'd into. On the other hand,
8635 non-computed gotos must not be; the identifier in question
8636 will have no binding. */
015c2c66 8637 tmp = RECUR (tmp);
3fa56191 8638 else
fd10dd09
JM
8639 tmp = DECL_NAME (tmp);
8640 finish_goto_stmt (tmp);
ad321293
MM
8641 break;
8642
e130a54b 8643 case ASM_EXPR:
c87978aa 8644 tmp = finish_asm_stmt
6de9cd9a 8645 (ASM_VOLATILE_P (t),
015c2c66 8646 RECUR (ASM_STRING (t)),
f74dcfb7
JJ
8647 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8648 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8649 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
131263fa
AP
8650 {
8651 tree asm_expr = tmp;
8652 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8653 asm_expr = TREE_OPERAND (asm_expr, 0);
8654 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8655 }
5566b478 8656 break;
faf5394a
MS
8657
8658 case TRY_BLOCK:
f1dedc31 8659 if (CLEANUP_P (t))
62409b39 8660 {
57b52417 8661 stmt = begin_try_block ();
015c2c66 8662 RECUR (TRY_STMTS (t));
62409b39 8663 finish_cleanup_try_block (stmt);
015c2c66 8664 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
62409b39 8665 }
f1dedc31
MM
8666 else
8667 {
eaf6fb90
MM
8668 tree compound_stmt = NULL_TREE;
8669
62409b39 8670 if (FN_TRY_BLOCK_P (t))
eaf6fb90 8671 stmt = begin_function_try_block (&compound_stmt);
62409b39
MM
8672 else
8673 stmt = begin_try_block ();
8674
015c2c66 8675 RECUR (TRY_STMTS (t));
62409b39
MM
8676
8677 if (FN_TRY_BLOCK_P (t))
8678 finish_function_try_block (stmt);
8679 else
8680 finish_try_block (stmt);
8681
015c2c66 8682 RECUR (TRY_HANDLERS (t));
84df082b 8683 if (FN_TRY_BLOCK_P (t))
eaf6fb90 8684 finish_function_handler_sequence (stmt, compound_stmt);
84df082b
MM
8685 else
8686 finish_handler_sequence (stmt);
f1dedc31 8687 }
faf5394a 8688 break;
c8094d83 8689
faf5394a 8690 case HANDLER:
b35d4555 8691 {
3a2419a7 8692 tree decl = HANDLER_PARMS (t);
b35d4555 8693
3a2419a7 8694 if (decl)
b35d4555 8695 {
b35d4555 8696 decl = tsubst (decl, args, complain, in_decl);
f8191e64
MM
8697 /* Prevent instantiate_decl from trying to instantiate
8698 this variable. We've already done all that needs to be
8699 done. */
3a2419a7
VR
8700 if (decl != error_mark_node)
8701 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
b35d4555 8702 }
3a2419a7 8703 stmt = begin_handler ();
1a6025b4 8704 finish_handler_parms (decl, stmt);
015c2c66 8705 RECUR (HANDLER_BODY (t));
1a6025b4 8706 finish_handler (stmt);
b35d4555 8707 }
faf5394a
MS
8708 break;
8709
b87692e5 8710 case TAG_DEFN:
fd10dd09 8711 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
b87692e5
MS
8712 break;
8713
1799e5d5
RH
8714 case OMP_PARALLEL:
8715 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8716 args, complain, in_decl);
8717 stmt = begin_omp_parallel ();
015c2c66 8718 RECUR (OMP_PARALLEL_BODY (t));
761041be
JJ
8719 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
8720 = OMP_PARALLEL_COMBINED (t);
1799e5d5
RH
8721 break;
8722
8723 case OMP_FOR:
8724 {
8725 tree clauses, decl, init, cond, incr, body, pre_body;
8726
8727 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8728 args, complain, in_decl);
8729 init = OMP_FOR_INIT (t);
8730 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
015c2c66
MM
8731 decl = RECUR (TREE_OPERAND (init, 0));
8732 init = RECUR (TREE_OPERAND (init, 1));
8733 cond = RECUR (OMP_FOR_COND (t));
8734 incr = RECUR (OMP_FOR_INCR (t));
1799e5d5
RH
8735
8736 stmt = begin_omp_structured_block ();
8737
8738 pre_body = push_stmt_list ();
015c2c66 8739 RECUR (OMP_FOR_PRE_BODY (t));
1799e5d5
RH
8740 pre_body = pop_stmt_list (pre_body);
8741
8742 body = push_stmt_list ();
015c2c66 8743 RECUR (OMP_FOR_BODY (t));
1799e5d5
RH
8744 body = pop_stmt_list (body);
8745
8746 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8747 pre_body);
8748 if (t)
8749 OMP_FOR_CLAUSES (t) = clauses;
8750
8751 add_stmt (finish_omp_structured_block (stmt));
8752 }
8753 break;
8754
8755 case OMP_SECTIONS:
8756 case OMP_SINGLE:
8757 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8758 stmt = push_stmt_list ();
015c2c66 8759 RECUR (OMP_BODY (t));
1799e5d5
RH
8760 stmt = pop_stmt_list (stmt);
8761
8762 t = copy_node (t);
8763 OMP_BODY (t) = stmt;
8764 OMP_CLAUSES (t) = tmp;
8765 add_stmt (t);
8766 break;
8767
8768 case OMP_SECTION:
8769 case OMP_CRITICAL:
8770 case OMP_MASTER:
8771 case OMP_ORDERED:
8772 stmt = push_stmt_list ();
015c2c66 8773 RECUR (OMP_BODY (t));
1799e5d5
RH
8774 stmt = pop_stmt_list (stmt);
8775
8776 t = copy_node (t);
8777 OMP_BODY (t) = stmt;
8778 add_stmt (t);
8779 break;
8780
8781 case OMP_ATOMIC:
8782 {
8783 tree op0, op1;
015c2c66
MM
8784 op0 = RECUR (TREE_OPERAND (t, 0));
8785 op1 = RECUR (TREE_OPERAND (t, 1));
fe89d797 8786 finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1);
1799e5d5
RH
8787 }
8788 break;
8789
5566b478 8790 default:
315fb5db 8791 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
c8094d83 8792
315fb5db 8793 return tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
8794 /*function_p=*/false,
8795 integral_constant_expression_p);
5566b478 8796 }
fd10dd09 8797
325c3691 8798 return NULL_TREE;
015c2c66 8799#undef RECUR
8d08fdba
MS
8800}
8801
b3445994
MM
8802/* T is a postfix-expression that is not being used in a function
8803 call. Return the substituted version of T. */
8804
8805static tree
c8094d83 8806tsubst_non_call_postfix_expression (tree t, tree args,
b3445994
MM
8807 tsubst_flags_t complain,
8808 tree in_decl)
8809{
8810 if (TREE_CODE (t) == SCOPE_REF)
8811 t = tsubst_qualified_id (t, args, complain, in_decl,
8812 /*done=*/false, /*address_p=*/false);
8813 else
8814 t = tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
8815 /*function_p=*/false,
8816 /*integral_constant_expression_p=*/false);
b3445994
MM
8817
8818 return t;
8819}
8820
cc23546e 8821/* Like tsubst but deals with expressions and performs semantic
b3445994 8822 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
cc23546e 8823
ee76b931 8824tree
c8094d83 8825tsubst_copy_and_build (tree t,
0cbd7506
MS
8826 tree args,
8827 tsubst_flags_t complain,
8828 tree in_decl,
015c2c66
MM
8829 bool function_p,
8830 bool integral_constant_expression_p)
cc23546e 8831{
015c2c66
MM
8832#define RECUR(NODE) \
8833 tsubst_copy_and_build (NODE, args, complain, in_decl, \
8834 /*function_p=*/false, \
8835 integral_constant_expression_p)
b3445994 8836
ee76b931
MM
8837 tree op1;
8838
cc23546e
JO
8839 if (t == NULL_TREE || t == error_mark_node)
8840 return t;
8841
8842 switch (TREE_CODE (t))
8843 {
399dedb9
NS
8844 case USING_DECL:
8845 t = DECL_NAME (t);
852dcbdd 8846 /* Fall through. */
b3445994 8847 case IDENTIFIER_NODE:
cc23546e 8848 {
b3445994 8849 tree decl;
b3445994 8850 cp_id_kind idk;
67c03833 8851 bool non_integral_constant_expression_p;
b3445994
MM
8852 const char *error_msg;
8853
b3445994 8854 if (IDENTIFIER_TYPENAME_P (t))
cc23546e 8855 {
b3445994
MM
8856 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8857 t = mangle_conv_op_name_for_type (new_type);
cc23546e 8858 }
b3445994
MM
8859
8860 /* Look up the name. */
10e6657a 8861 decl = lookup_name (t);
b3445994
MM
8862
8863 /* By convention, expressions use ERROR_MARK_NODE to indicate
8864 failure, not NULL_TREE. */
8865 if (decl == NULL_TREE)
8866 decl = error_mark_node;
8867
10b1d5e7 8868 decl = finish_id_expression (t, decl, NULL_TREE,
b3445994 8869 &idk,
015c2c66 8870 integral_constant_expression_p,
67c03833
JM
8871 /*allow_non_integral_constant_expression_p=*/false,
8872 &non_integral_constant_expression_p,
02ed62dd
MM
8873 /*template_p=*/false,
8874 /*done=*/true,
8875 /*address_p=*/false,
8876 /*template_arg_p=*/false,
b3445994
MM
8877 &error_msg);
8878 if (error_msg)
8879 error (error_msg);
8880 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8881 decl = unqualified_name_lookup_error (decl);
8882 return decl;
cc23546e
JO
8883 }
8884
8885 case TEMPLATE_ID_EXPR:
8886 {
8887 tree object;
b3445994 8888 tree template = RECUR (TREE_OPERAND (t, 0));
bf12d54d
NS
8889 tree targs = TREE_OPERAND (t, 1);
8890
8891 if (targs)
8892 targs = tsubst_template_args (targs, args, complain, in_decl);
c8094d83 8893
cc23546e
JO
8894 if (TREE_CODE (template) == COMPONENT_REF)
8895 {
8896 object = TREE_OPERAND (template, 0);
8897 template = TREE_OPERAND (template, 1);
8898 }
8899 else
8900 object = NULL_TREE;
bd83b409 8901 template = lookup_template_function (template, targs);
c8094d83 8902
cc23546e 8903 if (object)
c8094d83 8904 return build3 (COMPONENT_REF, TREE_TYPE (template),
f293ce4b 8905 object, template, NULL_TREE);
cc23546e 8906 else
eff3a276 8907 return baselink_for_fns (template);
cc23546e
JO
8908 }
8909
8910 case INDIRECT_REF:
db24eb1f
NS
8911 {
8912 tree r = RECUR (TREE_OPERAND (t, 0));
8913
8914 if (REFERENCE_REF_P (t))
8915 {
e8c66fe0
NS
8916 /* A type conversion to reference type will be enclosed in
8917 such an indirect ref, but the substitution of the cast
8918 will have also added such an indirect ref. */
8919 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8920 r = convert_from_reference (r);
db24eb1f
NS
8921 }
8922 else
8923 r = build_x_indirect_ref (r, "unary *");
8924 return r;
8925 }
cc23546e 8926
0da99d4e
GB
8927 case NOP_EXPR:
8928 return build_nop
8929 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8930 RECUR (TREE_OPERAND (t, 0)));
8931
cc23546e 8932 case CAST_EXPR:
cc23546e 8933 case REINTERPRET_CAST_EXPR:
cc23546e 8934 case CONST_CAST_EXPR:
cc23546e 8935 case DYNAMIC_CAST_EXPR:
cc23546e 8936 case STATIC_CAST_EXPR:
015c2c66
MM
8937 {
8938 tree type;
8939 tree op;
8940
8941 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8942 if (integral_constant_expression_p
8943 && !cast_valid_in_integral_constant_expression_p (type))
8944 {
8945 error ("a cast to a type other than an integral or "
8946 "enumeration type cannot appear in a constant-expression");
8947 return error_mark_node;
8948 }
8949
8950 op = RECUR (TREE_OPERAND (t, 0));
8951
8952 switch (TREE_CODE (t))
8953 {
8954 case CAST_EXPR:
8955 return build_functional_cast (type, op);
8956 case REINTERPRET_CAST_EXPR:
8957 return build_reinterpret_cast (type, op);
8958 case CONST_CAST_EXPR:
8959 return build_const_cast (type, op);
8960 case DYNAMIC_CAST_EXPR:
8961 return build_dynamic_cast (type, op);
8962 case STATIC_CAST_EXPR:
8963 return build_static_cast (type, op);
8964 default:
8965 gcc_unreachable ();
8966 }
8967 }
cc23546e 8968
cc23546e
JO
8969 case POSTDECREMENT_EXPR:
8970 case POSTINCREMENT_EXPR:
b3445994
MM
8971 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8972 args, complain, in_decl);
ee76b931
MM
8973 return build_x_unary_op (TREE_CODE (t), op1);
8974
8975 case PREDECREMENT_EXPR:
8976 case PREINCREMENT_EXPR:
cc23546e
JO
8977 case NEGATE_EXPR:
8978 case BIT_NOT_EXPR:
cc23546e 8979 case ABS_EXPR:
d17811fd 8980 case TRUTH_NOT_EXPR:
392e3d51 8981 case UNARY_PLUS_EXPR: /* Unary + */
d17811fd
MM
8982 case REALPART_EXPR:
8983 case IMAGPART_EXPR:
b3445994 8984 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
cc23546e 8985
cc23546e 8986 case ADDR_EXPR:
ee76b931
MM
8987 op1 = TREE_OPERAND (t, 0);
8988 if (TREE_CODE (op1) == SCOPE_REF)
c8094d83 8989 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
ee76b931
MM
8990 /*done=*/true, /*address_p=*/true);
8991 else
c8094d83 8992 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
b3445994 8993 in_decl);
fc2b8477
MM
8994 if (TREE_CODE (op1) == LABEL_DECL)
8995 return finish_label_address_expr (DECL_NAME (op1));
ee76b931
MM
8996 return build_x_unary_op (ADDR_EXPR, op1);
8997
cc23546e
JO
8998 case PLUS_EXPR:
8999 case MINUS_EXPR:
9000 case MULT_EXPR:
9001 case TRUNC_DIV_EXPR:
9002 case CEIL_DIV_EXPR:
9003 case FLOOR_DIV_EXPR:
9004 case ROUND_DIV_EXPR:
9005 case EXACT_DIV_EXPR:
9006 case BIT_AND_EXPR:
cc23546e
JO
9007 case BIT_IOR_EXPR:
9008 case BIT_XOR_EXPR:
9009 case TRUNC_MOD_EXPR:
9010 case FLOOR_MOD_EXPR:
9011 case TRUTH_ANDIF_EXPR:
9012 case TRUTH_ORIF_EXPR:
9013 case TRUTH_AND_EXPR:
9014 case TRUTH_OR_EXPR:
9015 case RSHIFT_EXPR:
9016 case LSHIFT_EXPR:
9017 case RROTATE_EXPR:
9018 case LROTATE_EXPR:
9019 case EQ_EXPR:
9020 case NE_EXPR:
9021 case MAX_EXPR:
9022 case MIN_EXPR:
9023 case LE_EXPR:
9024 case GE_EXPR:
9025 case LT_EXPR:
9026 case GT_EXPR:
9027 case MEMBER_REF:
b3445994 9028 case DOTSTAR_EXPR:
cc23546e 9029 return build_x_binary_op
c8094d83 9030 (TREE_CODE (t),
b3445994 9031 RECUR (TREE_OPERAND (t, 0)),
ec835fb2
MM
9032 RECUR (TREE_OPERAND (t, 1)),
9033 /*overloaded_p=*/NULL);
cc23546e
JO
9034
9035 case SCOPE_REF:
ee76b931
MM
9036 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
9037 /*address_p=*/false);
cc23546e 9038 case ARRAY_REF:
b3445994
MM
9039 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9040 args, complain, in_decl);
d8987adb
NS
9041 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
9042 /*overloaded_p=*/NULL);
c8094d83 9043
cc23546e
JO
9044 case SIZEOF_EXPR:
9045 case ALIGNOF_EXPR:
d17811fd
MM
9046 op1 = TREE_OPERAND (t, 0);
9047 if (!args)
9048 {
9049 /* When there are no ARGS, we are trying to evaluate a
9050 non-dependent expression from the parser. Trying to do
9051 the substitutions may not work. */
9052 if (!TYPE_P (op1))
9053 op1 = TREE_TYPE (op1);
9054 }
9055 else
9056 {
9057 ++skip_evaluation;
015c2c66
MM
9058 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
9059 /*function_p=*/false,
9060 /*integral_constant_expression_p=*/false);
d17811fd
MM
9061 --skip_evaluation;
9062 }
7a18b933
NS
9063 if (TYPE_P (op1))
9064 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
d17811fd 9065 else
7a18b933 9066 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
cc23546e
JO
9067
9068 case MODOP_EXPR:
e4c2c34b
JM
9069 {
9070 tree r = build_x_modify_expr
9071 (RECUR (TREE_OPERAND (t, 0)),
9072 TREE_CODE (TREE_OPERAND (t, 1)),
9073 RECUR (TREE_OPERAND (t, 2)));
bcf9a914
JM
9074 /* TREE_NO_WARNING must be set if either the expression was
9075 parenthesized or it uses an operator such as >>= rather
9076 than plain assignment. In the former case, it was already
9077 set and must be copied. In the latter case,
9078 build_x_modify_expr sets it and it must not be reset
9079 here. */
9080 if (TREE_NO_WARNING (t))
9081 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
e4c2c34b
JM
9082 return r;
9083 }
cc23546e
JO
9084
9085 case ARROW_EXPR:
b3445994
MM
9086 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9087 args, complain, in_decl);
ee76b931
MM
9088 /* Remember that there was a reference to this entity. */
9089 if (DECL_P (op1))
9090 mark_used (op1);
9091 return build_x_arrow (op1);
cc23546e
JO
9092
9093 case NEW_EXPR:
9094 return build_new
b3445994
MM
9095 (RECUR (TREE_OPERAND (t, 0)),
9096 RECUR (TREE_OPERAND (t, 1)),
9097 RECUR (TREE_OPERAND (t, 2)),
058b15c1 9098 RECUR (TREE_OPERAND (t, 3)),
cc23546e
JO
9099 NEW_EXPR_USE_GLOBAL (t));
9100
9101 case DELETE_EXPR:
9102 return delete_sanity
b3445994
MM
9103 (RECUR (TREE_OPERAND (t, 0)),
9104 RECUR (TREE_OPERAND (t, 1)),
cc23546e
JO
9105 DELETE_EXPR_USE_VEC (t),
9106 DELETE_EXPR_USE_GLOBAL (t));
9107
9108 case COMPOUND_EXPR:
b3445994
MM
9109 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
9110 RECUR (TREE_OPERAND (t, 1)));
cc23546e 9111
ee76b931 9112 case CALL_EXPR:
cc23546e 9113 {
ee76b931
MM
9114 tree function;
9115 tree call_args;
ee76b931 9116 bool qualified_p;
ee935db4 9117 bool koenig_p;
ee76b931
MM
9118
9119 function = TREE_OPERAND (t, 0);
6d80c4b9
MM
9120 /* When we parsed the expression, we determined whether or
9121 not Koenig lookup should be performed. */
9122 koenig_p = KOENIG_LOOKUP_P (t);
ee76b931 9123 if (TREE_CODE (function) == SCOPE_REF)
cc23546e 9124 {
ee76b931
MM
9125 qualified_p = true;
9126 function = tsubst_qualified_id (function, args, complain, in_decl,
c8094d83 9127 /*done=*/false,
ee76b931 9128 /*address_p=*/false);
cc23546e 9129 }
ee76b931 9130 else
cc23546e 9131 {
61e71a9e
NS
9132 if (TREE_CODE (function) == COMPONENT_REF)
9133 {
9134 tree op = TREE_OPERAND (function, 1);
9135
9136 qualified_p = (TREE_CODE (op) == SCOPE_REF
9137 || (BASELINK_P (op)
9138 && BASELINK_QUALIFIED_P (op)));
9139 }
9140 else
9141 qualified_p = false;
3db45ab5 9142
c8094d83 9143 function = tsubst_copy_and_build (function, args, complain,
b3445994 9144 in_decl,
015c2c66
MM
9145 !qualified_p,
9146 integral_constant_expression_p);
9147
6d80c4b9
MM
9148 if (BASELINK_P (function))
9149 qualified_p = true;
cc23546e 9150 }
cc23546e 9151
b3445994 9152 call_args = RECUR (TREE_OPERAND (t, 1));
676e33ca
MM
9153
9154 /* We do not perform argument-dependent lookup if normal
9155 lookup finds a non-function, in accordance with the
9156 expected resolution of DR 218. */
ee935db4 9157 if (koenig_p
44370687
MM
9158 && ((is_overloaded_fn (function)
9159 /* If lookup found a member function, the Koenig lookup is
9160 not appropriate, even if an unqualified-name was used
9161 to denote the function. */
9162 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
b3445994 9163 || TREE_CODE (function) == IDENTIFIER_NODE))
6d80c4b9
MM
9164 function = perform_koenig_lookup (function, call_args);
9165
9166 if (TREE_CODE (function) == IDENTIFIER_NODE)
b3445994 9167 {
6d80c4b9
MM
9168 unqualified_name_lookup_error (function);
9169 return error_mark_node;
b3445994
MM
9170 }
9171
9172 /* Remember that there was a reference to this entity. */
9173 if (DECL_P (function))
9174 mark_used (function);
9175
d17811fd
MM
9176 if (TREE_CODE (function) == OFFSET_REF)
9177 return build_offset_ref_call_from_tree (function, call_args);
9178 if (TREE_CODE (function) == COMPONENT_REF)
9f880ef9
MM
9179 {
9180 if (!BASELINK_P (TREE_OPERAND (function, 1)))
9181 return finish_call_expr (function, call_args,
9182 /*disallow_virtual=*/false,
9183 /*koenig_p=*/false);
9184 else
c8094d83 9185 return (build_new_method_call
9f880ef9
MM
9186 (TREE_OPERAND (function, 0),
9187 TREE_OPERAND (function, 1),
c8094d83 9188 call_args, NULL_TREE,
63c9a190
MM
9189 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
9190 /*fn_p=*/NULL));
9f880ef9 9191 }
c8094d83 9192 return finish_call_expr (function, call_args,
6d80c4b9
MM
9193 /*disallow_virtual=*/qualified_p,
9194 koenig_p);
cc23546e
JO
9195 }
9196
9197 case COND_EXPR:
9198 return build_x_conditional_expr
b3445994
MM
9199 (RECUR (TREE_OPERAND (t, 0)),
9200 RECUR (TREE_OPERAND (t, 1)),
9201 RECUR (TREE_OPERAND (t, 2)));
cc23546e
JO
9202
9203 case PSEUDO_DTOR_EXPR:
c8094d83 9204 return finish_pseudo_destructor_expr
b3445994
MM
9205 (RECUR (TREE_OPERAND (t, 0)),
9206 RECUR (TREE_OPERAND (t, 1)),
9207 RECUR (TREE_OPERAND (t, 2)));
cc23546e
JO
9208
9209 case TREE_LIST:
9210 {
9211 tree purpose, value, chain;
9212
9213 if (t == void_list_node)
9214 return t;
9215
9216 purpose = TREE_PURPOSE (t);
9217 if (purpose)
b3445994 9218 purpose = RECUR (purpose);
cc23546e
JO
9219 value = TREE_VALUE (t);
9220 if (value)
b3445994 9221 value = RECUR (value);
cc23546e
JO
9222 chain = TREE_CHAIN (t);
9223 if (chain && chain != void_type_node)
b3445994 9224 chain = RECUR (chain);
cc23546e
JO
9225 if (purpose == TREE_PURPOSE (t)
9226 && value == TREE_VALUE (t)
9227 && chain == TREE_CHAIN (t))
9228 return t;
9229 return tree_cons (purpose, value, chain);
9230 }
9231
9232 case COMPONENT_REF:
9233 {
ee76b931 9234 tree object;
3897c0aa 9235 tree object_type;
ee76b931
MM
9236 tree member;
9237
b3445994
MM
9238 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9239 args, complain, in_decl);
ee76b931
MM
9240 /* Remember that there was a reference to this entity. */
9241 if (DECL_P (object))
9242 mark_used (object);
3897c0aa 9243 object_type = TREE_TYPE (object);
ee76b931
MM
9244
9245 member = TREE_OPERAND (t, 1);
9246 if (BASELINK_P (member))
c8094d83 9247 member = tsubst_baselink (member,
ee76b931
MM
9248 non_reference (TREE_TYPE (object)),
9249 args, complain, in_decl);
9250 else
9251 member = tsubst_copy (member, args, complain, in_decl);
bad1f462
KL
9252 if (member == error_mark_node)
9253 return error_mark_node;
3897c0aa
MM
9254
9255 if (object_type && !CLASS_TYPE_P (object_type))
cc23546e
JO
9256 {
9257 if (TREE_CODE (member) == BIT_NOT_EXPR)
c8094d83 9258 return finish_pseudo_destructor_expr (object,
cc23546e 9259 NULL_TREE,
3897c0aa 9260 object_type);
cc23546e
JO
9261 else if (TREE_CODE (member) == SCOPE_REF
9262 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
c8094d83 9263 return finish_pseudo_destructor_expr (object,
cc23546e 9264 object,
3897c0aa 9265 object_type);
cc23546e
JO
9266 }
9267 else if (TREE_CODE (member) == SCOPE_REF
9268 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
9269 {
9270 tree tmpl;
9271 tree args;
c8094d83 9272
cc23546e
JO
9273 /* Lookup the template functions now that we know what the
9274 scope is. */
9275 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
9276 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
c8094d83 9277 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12483c9f
NS
9278 /*is_type_p=*/false,
9279 /*complain=*/false);
cc23546e 9280 if (BASELINK_P (member))
44370687 9281 {
c8094d83 9282 BASELINK_FUNCTIONS (member)
44370687
MM
9283 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
9284 args);
c8094d83
MS
9285 member = (adjust_result_of_qualified_name_lookup
9286 (member, BINFO_TYPE (BASELINK_BINFO (member)),
3897c0aa 9287 object_type));
44370687 9288 }
cc23546e
JO
9289 else
9290 {
3897c0aa 9291 qualified_name_lookup_error (object_type, tmpl, member);
cc23546e
JO
9292 return error_mark_node;
9293 }
9294 }
2436b51f
MM
9295 else if (TREE_CODE (member) == SCOPE_REF
9296 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9297 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9298 {
9299 if (complain & tf_error)
9300 {
9301 if (TYPE_P (TREE_OPERAND (member, 0)))
c8094d83 9302 error ("%qT is not a class or namespace",
2436b51f
MM
9303 TREE_OPERAND (member, 0));
9304 else
c8094d83 9305 error ("%qD is not a class or namespace",
2436b51f
MM
9306 TREE_OPERAND (member, 0));
9307 }
9308 return error_mark_node;
9309 }
a3f10e50
NS
9310 else if (TREE_CODE (member) == FIELD_DECL)
9311 return finish_non_static_data_member (member, object, NULL_TREE);
cc23546e 9312
02ed62dd
MM
9313 return finish_class_member_access_expr (object, member,
9314 /*template_p=*/false);
cc23546e
JO
9315 }
9316
9317 case THROW_EXPR:
9318 return build_throw
b3445994 9319 (RECUR (TREE_OPERAND (t, 0)));
cc23546e
JO
9320
9321 case CONSTRUCTOR:
9322 {
4038c495
GB
9323 VEC(constructor_elt,gc) *n;
9324 constructor_elt *ce;
9325 unsigned HOST_WIDE_INT idx;
cc23546e 9326 tree r;
cc23546e 9327 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
4038c495 9328 bool process_index_p;
cc23546e 9329
a97728cf
VR
9330 if (type == error_mark_node)
9331 return error_mark_node;
9332
cc23546e
JO
9333 /* digest_init will do the wrong thing if we let it. */
9334 if (type && TYPE_PTRMEMFUNC_P (type))
9335 return t;
9336
4038c495 9337 /* We do not want to process the index of aggregate
cc23546e
JO
9338 initializers as they are identifier nodes which will be
9339 looked up by digest_init. */
4038c495 9340 process_index_p = !(type && IS_AGGR_TYPE (type));
c8094d83 9341
4038c495
GB
9342 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9343 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9344 {
9345 if (ce->index && process_index_p)
9346 ce->index = RECUR (ce->index);
9347 ce->value = RECUR (ce->value);
cc23546e 9348 }
c8094d83 9349
4038c495 9350 r = build_constructor (NULL_TREE, n);
cc23546e
JO
9351 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
9352
9353 if (type)
fb8549a1
MM
9354 {
9355 r = reshape_init (type, r);
9356 return digest_init (type, r);
9357 }
cc23546e
JO
9358 return r;
9359 }
9360
9361 case TYPEID_EXPR:
9362 {
b3445994 9363 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
cc23546e
JO
9364 if (TYPE_P (operand_0))
9365 return get_typeid (operand_0);
9366 return build_typeid (operand_0);
9367 }
9368
cc23546e 9369 case VAR_DECL:
db24eb1f
NS
9370 if (!args)
9371 return t;
9372 /* Fall through */
c8094d83 9373
db24eb1f
NS
9374 case PARM_DECL:
9375 {
9376 tree r = tsubst_copy (t, args, complain, in_decl);
c8094d83 9377
db24eb1f
NS
9378 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9379 /* If the original type was a reference, we'll be wrapped in
9380 the appropriate INDIRECT_REF. */
9381 r = convert_from_reference (r);
9382 return r;
9383 }
cc23546e
JO
9384
9385 case VA_ARG_EXPR:
b3445994 9386 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
c8094d83 9387 tsubst_copy (TREE_TYPE (t), args, complain,
b3445994 9388 in_decl));
cc23546e 9389
4bceb077 9390 case OFFSETOF_EXPR:
c291f8b1 9391 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
4bceb077 9392
0fe0caa6
RH
9393 case STMT_EXPR:
9394 {
9395 tree old_stmt_expr = cur_stmt_expr;
9396 tree stmt_expr = begin_stmt_expr ();
9397
9398 cur_stmt_expr = stmt_expr;
015c2c66
MM
9399 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
9400 integral_constant_expression_p);
0fe0caa6
RH
9401 stmt_expr = finish_stmt_expr (stmt_expr, false);
9402 cur_stmt_expr = old_stmt_expr;
9403
9404 return stmt_expr;
9405 }
9406
a5201a91
MM
9407 case CONST_DECL:
9408 t = tsubst_copy (t, args, complain, in_decl);
9409 /* As in finish_id_expression, we resolve enumeration constants
9410 to their underlying values. */
9411 if (TREE_CODE (t) == CONST_DECL)
6193b8b7
DJ
9412 {
9413 used_types_insert (TREE_TYPE (t));
9414 return DECL_INITIAL (t);
9415 }
a5201a91
MM
9416 return t;
9417
cc23546e 9418 default:
e58a9aa1
ZL
9419 /* Handle Objective-C++ constructs, if appropriate. */
9420 {
9421 tree subst
9422 = objcp_tsubst_copy_and_build (t, args, complain,
9423 in_decl, /*function_p=*/false);
9424 if (subst)
9425 return subst;
9426 }
cc23546e
JO
9427 return tsubst_copy (t, args, complain, in_decl);
9428 }
b3445994
MM
9429
9430#undef RECUR
cc23546e
JO
9431}
9432
3e4a3562
NS
9433/* Verify that the instantiated ARGS are valid. For type arguments,
9434 make sure that the type's linkage is ok. For non-type arguments,
34cd5ae7 9435 make sure they are constants if they are integral or enumerations.
9bcb9aae 9436 Emit an error under control of COMPLAIN, and return TRUE on error. */
3e4a3562
NS
9437
9438static bool
3a978d72 9439check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
3e4a3562
NS
9440{
9441 int ix, len = DECL_NTPARMS (tmpl);
9442 bool result = false;
9443
9444 for (ix = 0; ix != len; ix++)
9445 {
9446 tree t = TREE_VEC_ELT (args, ix);
c8094d83 9447
3e4a3562
NS
9448 if (TYPE_P (t))
9449 {
9450 /* [basic.link]: A name with no linkage (notably, the name
9451 of a class or enumeration declared in a local scope)
9452 shall not be used to declare an entity with linkage.
9453 This implies that names with no linkage cannot be used as
9454 template arguments. */
4684cd27 9455 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
3e4a3562
NS
9456
9457 if (nt)
9458 {
cb807ba3 9459 /* DR 488 makes use of a type with no linkage cause
3db45ab5 9460 type deduction to fail. */
2010cdcd
MM
9461 if (complain & tf_error)
9462 {
9463 if (TYPE_ANONYMOUS_P (nt))
9464 error ("%qT is/uses anonymous type", t);
9465 else
9df540a8 9466 error ("template argument for %qD uses local type %qT",
3db45ab5 9467 tmpl, t);
2010cdcd 9468 }
3e4a3562
NS
9469 result = true;
9470 }
9471 /* In order to avoid all sorts of complications, we do not
9472 allow variably-modified types as template arguments. */
5377d5ba 9473 else if (variably_modified_type_p (t, NULL_TREE))
3e4a3562
NS
9474 {
9475 if (complain & tf_error)
0f51ccfc 9476 error ("%qT is a variably modified type", t);
3e4a3562
NS
9477 result = true;
9478 }
9479 }
9480 /* A non-type argument of integral or enumerated type must be a
9481 constant. */
9482 else if (TREE_TYPE (t)
9483 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9484 && !TREE_CONSTANT (t))
9485 {
9486 if (complain & tf_error)
0f51ccfc 9487 error ("integral expression %qE is not constant", t);
3e4a3562
NS
9488 result = true;
9489 }
9490 }
2010cdcd 9491 if (result && (complain & tf_error))
0f51ccfc 9492 error (" trying to instantiate %qD", tmpl);
3e4a3562
NS
9493 return result;
9494}
9495
6ba89f8e 9496/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
9497 the template arguments in TARG_PTR. */
9498
5566b478 9499tree
3a978d72 9500instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
8d08fdba 9501{
5566b478 9502 tree fndecl;
36a117a5
MM
9503 tree gen_tmpl;
9504 tree spec;
a95799ec 9505 HOST_WIDE_INT saved_processing_template_decl;
5566b478 9506
27fafc8d
JM
9507 if (tmpl == error_mark_node)
9508 return error_mark_node;
9509
50bc768d 9510 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
386b8a85 9511
db9b2174
MM
9512 /* If this function is a clone, handle it specially. */
9513 if (DECL_CLONED_FUNCTION_P (tmpl))
3ad97789 9514 {
a30f62e0 9515 tree spec;
3ad97789 9516 tree clone;
c8094d83 9517
a30f62e0
MM
9518 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9519 complain);
9520 if (spec == error_mark_node)
9521 return error_mark_node;
9522
c6002625 9523 /* Look for the clone. */
4684cd27 9524 FOR_EACH_CLONE (clone, spec)
3ad97789
NS
9525 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9526 return clone;
9527 /* We should always have found the clone by now. */
315fb5db 9528 gcc_unreachable ();
3ad97789
NS
9529 return NULL_TREE;
9530 }
c8094d83 9531
36a117a5 9532 /* Check to see if we already have this specialization. */
c8094d83 9533 spec = retrieve_specialization (tmpl, targ_ptr,
c7222c02 9534 /*class_specializations_p=*/false);
36a117a5
MM
9535 if (spec != NULL_TREE)
9536 return spec;
9537
f9a7ae04
MM
9538 gen_tmpl = most_general_template (tmpl);
9539 if (tmpl != gen_tmpl)
386b8a85 9540 {
36a117a5
MM
9541 /* The TMPL is a partial instantiation. To get a full set of
9542 arguments we must add the arguments used to perform the
9543 partial instantiation. */
9544 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9545 targ_ptr);
36a117a5
MM
9546
9547 /* Check to see if we already have this specialization. */
c7222c02
MM
9548 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9549 /*class_specializations_p=*/false);
75650646
MM
9550 if (spec != NULL_TREE)
9551 return spec;
386b8a85
JM
9552 }
9553
3e4a3562
NS
9554 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9555 complain))
9556 return error_mark_node;
c8094d83 9557
2b907f5c
KL
9558 /* We are building a FUNCTION_DECL, during which the access of its
9559 parameters and return types have to be checked. However this
9560 FUNCTION_DECL which is the desired context for access checking
9561 is not built yet. We solve this chicken-and-egg problem by
9562 deferring all checks until we have the FUNCTION_DECL. */
9563 push_deferring_access_checks (dk_deferred);
5c74d5b0 9564
a95799ec
MM
9565 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
9566 (because, for example, we have encountered a non-dependent
a1d08991
MM
9567 function call in the body of a template function and must now
9568 determine which of several overloaded functions will be called),
9569 within the instantiation itself we are not processing a
9570 template. */
a95799ec
MM
9571 saved_processing_template_decl = processing_template_decl;
9572 processing_template_decl = 0;
9573 /* Substitute template parameters to obtain the specialization. */
17aec3eb 9574 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
3e4a3562 9575 targ_ptr, complain, gen_tmpl);
a95799ec 9576 processing_template_decl = saved_processing_template_decl;
6b6b60af
MM
9577 if (fndecl == error_mark_node)
9578 return error_mark_node;
5c74d5b0 9579
2b907f5c
KL
9580 /* Now we know the specialization, compute access previously
9581 deferred. */
9582 push_access_scope (fndecl);
9583 perform_deferred_access_checks ();
9584 pop_access_scope (fndecl);
9585 pop_deferring_access_checks ();
5c74d5b0 9586
36a117a5
MM
9587 /* The DECL_TI_TEMPLATE should always be the immediate parent
9588 template, not the most general template. */
9589 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 9590
94350948 9591 /* If we've just instantiated the main entry point for a function,
3ad97789
NS
9592 instantiate all the alternate entry points as well. We do this
9593 by cloning the instantiation of the main entry point, not by
9594 instantiating the template clones. */
9595 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9596 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
94350948 9597
5566b478 9598 return fndecl;
8d08fdba 9599}
5566b478 9600
4393e105
MM
9601/* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9602 arguments that are being used when calling it. TARGS is a vector
c8094d83 9603 into which the deduced template arguments are placed.
8d08fdba
MS
9604
9605 Return zero for success, 2 for an incomplete match that doesn't resolve
9606 all the types, and 1 for complete failure. An error message will be
9607 printed only for an incomplete match.
9608
e5214479
JM
9609 If FN is a conversion operator, or we are trying to produce a specific
9610 specialization, RETURN_TYPE is the return type desired.
9f54c803
MM
9611
9612 The EXPLICIT_TARGS are explicit template arguments provided via a
9613 template-id.
6467930b 9614
830bfa74
MM
9615 The parameter STRICT is one of:
9616
c8094d83 9617 DEDUCE_CALL:
830bfa74
MM
9618 We are deducing arguments for a function call, as in
9619 [temp.deduct.call].
9620
9621 DEDUCE_CONV:
c8094d83 9622 We are deducing arguments for a conversion function, as in
830bfa74
MM
9623 [temp.deduct.conv].
9624
9625 DEDUCE_EXACT:
62e4a758
NS
9626 We are deducing arguments when doing an explicit instantiation
9627 as in [temp.explicit], when determining an explicit specialization
9628 as in [temp.expl.spec], or when taking the address of a function
a34d3336 9629 template, as in [temp.deduct.funcaddr]. */
8d08fdba
MS
9630
9631int
c8094d83 9632fn_type_unification (tree fn,
0cbd7506
MS
9633 tree explicit_targs,
9634 tree targs,
9635 tree args,
9636 tree return_type,
30f86ec3
FJ
9637 unification_kind_t strict,
9638 int flags)
386b8a85 9639{
4393e105
MM
9640 tree parms;
9641 tree fntype;
adecb3f4 9642 int result;
386b8a85 9643
50bc768d 9644 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
2b59fc25 9645
4393e105
MM
9646 fntype = TREE_TYPE (fn);
9647 if (explicit_targs)
75650646 9648 {
4393e105 9649 /* [temp.deduct]
c8094d83 9650
4393e105
MM
9651 The specified template arguments must match the template
9652 parameters in kind (i.e., type, nontype, template), and there
9653 must not be more arguments than there are parameters;
9654 otherwise type deduction fails.
9655
9656 Nontype arguments must match the types of the corresponding
9657 nontype template parameters, or must be convertible to the
9658 types of the corresponding nontype parameters as specified in
9659 _temp.arg.nontype_, otherwise type deduction fails.
9660
9661 All references in the function type of the function template
9662 to the corresponding template parameters are replaced by the
9663 specified template argument values. If a substitution in a
9664 template parameter or in the function type of the function
9665 template results in an invalid type, type deduction fails. */
9666 int i;
9667 tree converted_args;
ffd49b19 9668 bool incomplete;
75650646 9669
c8a7ed43
AO
9670 if (explicit_targs == error_mark_node)
9671 return 1;
9672
4393e105 9673 converted_args
c8094d83
MS
9674 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9675 explicit_targs, NULL_TREE, tf_none,
e7e93965
MM
9676 /*require_all_args=*/false,
9677 /*use_default_args=*/false));
4393e105 9678 if (converted_args == error_mark_node)
75650646 9679 return 1;
386b8a85 9680
ffd49b19 9681 /* Substitute the explicit args into the function type. This is
0cbd7506
MS
9682 necessary so that, for instance, explicitly declared function
9683 arguments can match null pointed constants. If we were given
9684 an incomplete set of explicit args, we must not do semantic
9685 processing during substitution as we could create partial
9686 instantiations. */
ffd49b19
NS
9687 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9688 processing_template_decl += incomplete;
c2ea3a40 9689 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
ffd49b19 9690 processing_template_decl -= incomplete;
c8094d83 9691
4393e105
MM
9692 if (fntype == error_mark_node)
9693 return 1;
050367a3 9694
4393e105 9695 /* Place the explicitly specified arguments in TARGS. */
ffd49b19 9696 for (i = NUM_TMPL_ARGS (converted_args); i--;)
4393e105 9697 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
75650646 9698 }
c8094d83 9699
4393e105 9700 parms = TYPE_ARG_TYPES (fntype);
e5214479
JM
9701 /* Never do unification on the 'this' parameter. */
9702 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9703 parms = TREE_CHAIN (parms);
c8094d83 9704
8d3631f8
NS
9705 if (return_type)
9706 {
8d3631f8
NS
9707 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9708 args = tree_cons (NULL_TREE, return_type, args);
4393e105
MM
9709 }
9710
4393e105
MM
9711 /* We allow incomplete unification without an error message here
9712 because the standard doesn't seem to explicitly prohibit it. Our
9713 callers must be ready to deal with unification failures in any
9714 event. */
c8094d83 9715 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
adecb3f4 9716 targs, parms, args, /*subr=*/0,
30f86ec3 9717 strict, flags);
adecb3f4 9718
c8094d83 9719 if (result == 0)
adecb3f4 9720 /* All is well so far. Now, check:
c8094d83
MS
9721
9722 [temp.deduct]
9723
adecb3f4
MM
9724 When all template arguments have been deduced, all uses of
9725 template parameters in nondeduced contexts are replaced with
9726 the corresponding deduced argument values. If the
9727 substitution results in an invalid type, as described above,
9728 type deduction fails. */
c2ea3a40 9729 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
adecb3f4
MM
9730 == error_mark_node)
9731 return 1;
9732
9733 return result;
830bfa74
MM
9734}
9735
9736/* Adjust types before performing type deduction, as described in
9737 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
9738 sections are symmetric. PARM is the type of a function parameter
9739 or the return type of the conversion function. ARG is the type of
9740 the argument passed to the call, or the type of the value
dc957d14 9741 initialized with the result of the conversion function. */
386b8a85 9742
62e4a758 9743static int
c8094d83 9744maybe_adjust_types_for_deduction (unification_kind_t strict,
0cbd7506
MS
9745 tree* parm,
9746 tree* arg)
830bfa74 9747{
62e4a758 9748 int result = 0;
c8094d83 9749
830bfa74
MM
9750 switch (strict)
9751 {
9752 case DEDUCE_CALL:
9753 break;
9754
9755 case DEDUCE_CONV:
9756 {
4c7d0dff
MM
9757 /* Swap PARM and ARG throughout the remainder of this
9758 function; the handling is precisely symmetric since PARM
9759 will initialize ARG rather than vice versa. */
830bfa74
MM
9760 tree* temp = parm;
9761 parm = arg;
9762 arg = temp;
9763 break;
9764 }
9765
9766 case DEDUCE_EXACT:
9767 /* There is nothing to do in this case. */
62e4a758 9768 return 0;
830bfa74
MM
9769
9770 default:
315fb5db 9771 gcc_unreachable ();
830bfa74
MM
9772 }
9773
9774 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9775 {
9776 /* [temp.deduct.call]
c8094d83 9777
830bfa74 9778 If P is not a reference type:
c8094d83 9779
830bfa74
MM
9780 --If A is an array type, the pointer type produced by the
9781 array-to-pointer standard conversion (_conv.array_) is
9782 used in place of A for type deduction; otherwise,
c8094d83 9783
830bfa74
MM
9784 --If A is a function type, the pointer type produced by
9785 the function-to-pointer standard conversion
9786 (_conv.func_) is used in place of A for type deduction;
9787 otherwise,
c8094d83 9788
830bfa74
MM
9789 --If A is a cv-qualified type, the top level
9790 cv-qualifiers of A's type are ignored for type
9791 deduction. */
9792 if (TREE_CODE (*arg) == ARRAY_TYPE)
9793 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 9794 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
9795 *arg = build_pointer_type (*arg);
9796 else
9797 *arg = TYPE_MAIN_VARIANT (*arg);
9798 }
c8094d83 9799
830bfa74 9800 /* [temp.deduct.call]
c8094d83 9801
830bfa74
MM
9802 If P is a cv-qualified type, the top level cv-qualifiers
9803 of P's type are ignored for type deduction. If P is a
9804 reference type, the type referred to by P is used for
9805 type deduction. */
9806 *parm = TYPE_MAIN_VARIANT (*parm);
9807 if (TREE_CODE (*parm) == REFERENCE_TYPE)
62e4a758
NS
9808 {
9809 *parm = TREE_TYPE (*parm);
9810 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9811 }
1c82cc90
NS
9812
9813 /* DR 322. For conversion deduction, remove a reference type on parm
9814 too (which has been swapped into ARG). */
9815 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9816 *arg = TREE_TYPE (*arg);
c8094d83 9817
62e4a758 9818 return result;
386b8a85
JM
9819}
9820
e5214479 9821/* Most parms like fn_type_unification.
9f54c803
MM
9822
9823 If SUBR is 1, we're being called recursively (to unify the
9824 arguments of a function or method parameter of a function
38d18b1a 9825 template). */
386b8a85 9826
4966381a 9827static int
c8094d83 9828type_unification_real (tree tparms,
0cbd7506
MS
9829 tree targs,
9830 tree xparms,
9831 tree xargs,
9832 int subr,
30f86ec3
FJ
9833 unification_kind_t strict,
9834 int flags)
8d08fdba
MS
9835{
9836 tree parm, arg;
9837 int i;
9838 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 9839 int sub_strict;
bd0d5d4a
JM
9840 int saw_undeduced = 0;
9841 tree parms, args;
8d08fdba 9842
50bc768d
NS
9843 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9844 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9845 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9846 gcc_assert (ntparms > 0);
8d08fdba 9847
830bfa74
MM
9848 switch (strict)
9849 {
9850 case DEDUCE_CALL:
028d1f20 9851 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
0cbd7506 9852 | UNIFY_ALLOW_DERIVED);
830bfa74 9853 break;
c8094d83 9854
830bfa74
MM
9855 case DEDUCE_CONV:
9856 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9857 break;
9858
9859 case DEDUCE_EXACT:
9860 sub_strict = UNIFY_ALLOW_NONE;
9861 break;
c8094d83 9862
830bfa74 9863 default:
315fb5db 9864 gcc_unreachable ();
830bfa74
MM
9865 }
9866
bd0d5d4a
JM
9867 again:
9868 parms = xparms;
9869 args = xargs;
bd0d5d4a 9870
a34d3336
NS
9871 while (parms && parms != void_list_node
9872 && args && args != void_list_node)
8d08fdba
MS
9873 {
9874 parm = TREE_VALUE (parms);
9875 parms = TREE_CHAIN (parms);
9876 arg = TREE_VALUE (args);
9877 args = TREE_CHAIN (args);
9878
9879 if (arg == error_mark_node)
9880 return 1;
9881 if (arg == unknown_type_node)
34016c81
JM
9882 /* We can't deduce anything from this, but we might get all the
9883 template args from other function args. */
9884 continue;
b7484fbe 9885
03e70705
JM
9886 /* Conversions will be performed on a function argument that
9887 corresponds with a function parameter that contains only
9888 non-deducible template parameters and explicitly specified
9889 template parameters. */
c353b8e3 9890 if (!uses_template_parms (parm))
b7484fbe 9891 {
03e70705
JM
9892 tree type;
9893
2f939d94 9894 if (!TYPE_P (arg))
03e70705
JM
9895 type = TREE_TYPE (arg);
9896 else
c353b8e3 9897 type = arg;
03e70705 9898
fad86f7a 9899 if (same_type_p (parm, type))
343c89cd 9900 continue;
fad86f7a 9901 if (strict != DEDUCE_EXACT
3db45ab5 9902 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
30f86ec3 9903 flags))
fad86f7a 9904 continue;
3db45ab5 9905
b7484fbe
MS
9906 return 1;
9907 }
c8094d83 9908
2f939d94 9909 if (!TYPE_P (arg))
8d08fdba 9910 {
50bc768d 9911 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
34016c81 9912 if (type_unknown_p (arg))
28cbf42c 9913 {
34016c81
JM
9914 /* [temp.deduct.type] A template-argument can be deduced from
9915 a pointer to function or pointer to member function
9916 argument if the set of overloaded functions does not
9917 contain function templates and at most one of a set of
9918 overloaded functions provides a unique match. */
9919
9920 if (resolve_overloaded_unification
4393e105 9921 (tparms, targs, parm, arg, strict, sub_strict)
34016c81
JM
9922 != 0)
9923 return 1;
9924 continue;
28cbf42c 9925 }
8d08fdba 9926 arg = TREE_TYPE (arg);
08476342
NS
9927 if (arg == error_mark_node)
9928 return 1;
8d08fdba 9929 }
c8094d83 9930
62e4a758 9931 {
0cbd7506 9932 int arg_strict = sub_strict;
c8094d83 9933
0cbd7506 9934 if (!subr)
62e4a758 9935 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
4393e105 9936
0cbd7506
MS
9937 if (unify (tparms, targs, parm, arg, arg_strict))
9938 return 1;
62e4a758 9939 }
8d08fdba 9940 }
c8094d83 9941
8d08fdba
MS
9942 /* Fail if we've reached the end of the parm list, and more args
9943 are present, and the parm list isn't variadic. */
9944 if (args && args != void_list_node && parms == void_list_node)
9945 return 1;
f4f206f4 9946 /* Fail if parms are left and they don't have default values. */
a34d3336 9947 if (parms && parms != void_list_node
8d08fdba
MS
9948 && TREE_PURPOSE (parms) == NULL_TREE)
9949 return 1;
bd0d5d4a 9950
8d08fdba
MS
9951 if (!subr)
9952 for (i = 0; i < ntparms; i++)
a34d3336 9953 if (!TREE_VEC_ELT (targs, i))
8d08fdba 9954 {
2d8ba2c7
LM
9955 tree tparm;
9956
9957 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
9958 continue;
9959
9960 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
bd0d5d4a
JM
9961
9962 /* If this is an undeduced nontype parameter that depends on
9963 a type parameter, try another pass; its type may have been
9964 deduced from a later argument than the one from which
9965 this parameter can be deduced. */
9966 if (TREE_CODE (tparm) == PARM_DECL
9967 && uses_template_parms (TREE_TYPE (tparm))
9968 && !saw_undeduced++)
9969 goto again;
9970
8d08fdba
MS
9971 return 2;
9972 }
c8094d83 9973
8d08fdba
MS
9974 return 0;
9975}
9976
34016c81
JM
9977/* Subroutine of type_unification_real. Args are like the variables at the
9978 call site. ARG is an overloaded function (or template-id); we try
9979 deducing template args from each of the overloads, and if only one
9980 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9981
9982static int
c8094d83 9983resolve_overloaded_unification (tree tparms,
0cbd7506
MS
9984 tree targs,
9985 tree parm,
9986 tree arg,
9987 unification_kind_t strict,
3a978d72 9988 int sub_strict)
34016c81
JM
9989{
9990 tree tempargs = copy_node (targs);
9991 int good = 0;
f23fb7f5 9992 bool addr_p;
34016c81
JM
9993
9994 if (TREE_CODE (arg) == ADDR_EXPR)
f23fb7f5
MM
9995 {
9996 arg = TREE_OPERAND (arg, 0);
9997 addr_p = true;
9998 }
9999 else
10000 addr_p = false;
9f3d9e46 10001
d8f8dca1
MM
10002 if (TREE_CODE (arg) == COMPONENT_REF)
10003 /* Handle `&x' where `x' is some static or non-static member
10004 function name. */
10005 arg = TREE_OPERAND (arg, 1);
10006
05e0b2f4
JM
10007 if (TREE_CODE (arg) == OFFSET_REF)
10008 arg = TREE_OPERAND (arg, 1);
10009
9f3d9e46 10010 /* Strip baselink information. */
50ad9642
MM
10011 if (BASELINK_P (arg))
10012 arg = BASELINK_FUNCTIONS (arg);
9f3d9e46 10013
34016c81
JM
10014 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
10015 {
10016 /* If we got some explicit template args, we need to plug them into
10017 the affected templates before we try to unify, in case the
10018 explicit args will completely resolve the templates in question. */
10019
10020 tree expl_subargs = TREE_OPERAND (arg, 1);
10021 arg = TREE_OPERAND (arg, 0);
10022
10023 for (; arg; arg = OVL_NEXT (arg))
10024 {
10025 tree fn = OVL_CURRENT (arg);
10026 tree subargs, elem;
10027
10028 if (TREE_CODE (fn) != TEMPLATE_DECL)
10029 continue;
10030
a34d3336
NS
10031 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
10032 expl_subargs, /*check_ret=*/false);
34016c81
JM
10033 if (subargs)
10034 {
c2ea3a40 10035 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
c8094d83 10036 good += try_one_overload (tparms, targs, tempargs, parm,
f23fb7f5 10037 elem, strict, sub_strict, addr_p);
34016c81
JM
10038 }
10039 }
10040 }
315fb5db 10041 else
34016c81 10042 {
315fb5db
NS
10043 gcc_assert (TREE_CODE (arg) == OVERLOAD
10044 || TREE_CODE (arg) == FUNCTION_DECL);
c8094d83 10045
34016c81 10046 for (; arg; arg = OVL_NEXT (arg))
f23fb7f5
MM
10047 good += try_one_overload (tparms, targs, tempargs, parm,
10048 TREE_TYPE (OVL_CURRENT (arg)),
10049 strict, sub_strict, addr_p);
34016c81 10050 }
34016c81
JM
10051
10052 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10053 to function or pointer to member function argument if the set of
10054 overloaded functions does not contain function templates and at most
10055 one of a set of overloaded functions provides a unique match.
10056
10057 So if we found multiple possibilities, we return success but don't
10058 deduce anything. */
10059
10060 if (good == 1)
10061 {
10062 int i = TREE_VEC_LENGTH (targs);
10063 for (; i--; )
10064 if (TREE_VEC_ELT (tempargs, i))
10065 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
10066 }
10067 if (good)
10068 return 0;
10069
10070 return 1;
10071}
10072
10073/* Subroutine of resolve_overloaded_unification; does deduction for a single
10074 overload. Fills TARGS with any deduced arguments, or error_mark_node if
10075 different overloads deduce different arguments for a given parm.
f23fb7f5
MM
10076 ADDR_P is true if the expression for which deduction is being
10077 performed was of the form "& fn" rather than simply "fn".
10078
34016c81
JM
10079 Returns 1 on success. */
10080
10081static int
3a978d72 10082try_one_overload (tree tparms,
0cbd7506
MS
10083 tree orig_targs,
10084 tree targs,
10085 tree parm,
10086 tree arg,
10087 unification_kind_t strict,
f23fb7f5
MM
10088 int sub_strict,
10089 bool addr_p)
34016c81
JM
10090{
10091 int nargs;
10092 tree tempargs;
10093 int i;
10094
10095 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10096 to function or pointer to member function argument if the set of
10097 overloaded functions does not contain function templates and at most
10098 one of a set of overloaded functions provides a unique match.
10099
10100 So if this is a template, just return success. */
10101
10102 if (uses_template_parms (arg))
10103 return 1;
10104
f23fb7f5
MM
10105 if (TREE_CODE (arg) == METHOD_TYPE)
10106 arg = build_ptrmemfunc_type (build_pointer_type (arg));
10107 else if (addr_p)
10108 arg = build_pointer_type (arg);
10109
62e4a758 10110 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
34016c81
JM
10111
10112 /* We don't copy orig_targs for this because if we have already deduced
10113 some template args from previous args, unify would complain when we
10114 try to deduce a template parameter for the same argument, even though
10115 there isn't really a conflict. */
10116 nargs = TREE_VEC_LENGTH (targs);
f31c0a32 10117 tempargs = make_tree_vec (nargs);
34016c81 10118
4393e105 10119 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
34016c81
JM
10120 return 0;
10121
10122 /* First make sure we didn't deduce anything that conflicts with
e97e5263 10123 explicitly specified args. */
34016c81
JM
10124 for (i = nargs; i--; )
10125 {
10126 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 10127 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81 10128
a34d3336
NS
10129 if (!elt)
10130 /*NOP*/;
34016c81 10131 else if (uses_template_parms (elt))
a34d3336
NS
10132 /* Since we're unifying against ourselves, we will fill in
10133 template args used in the function parm list with our own
10134 template parms. Discard them. */
10135 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
10136 else if (oldelt && !template_args_equal (oldelt, elt))
34016c81
JM
10137 return 0;
10138 }
10139
10140 for (i = nargs; i--; )
10141 {
10142 tree elt = TREE_VEC_ELT (tempargs, i);
10143
10144 if (elt)
10145 TREE_VEC_ELT (targs, i) = elt;
10146 }
10147
10148 return 1;
10149}
10150
4393e105
MM
10151/* PARM is a template class (perhaps with unbound template
10152 parameters). ARG is a fully instantiated type. If ARG can be
10153 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
10154 TARGS are as for unify. */
fcfb9f96
MM
10155
10156static tree
3a978d72 10157try_class_unification (tree tparms, tree targs, tree parm, tree arg)
4393e105 10158{
4393e105
MM
10159 tree copy_of_targs;
10160
10161 if (!CLASSTYPE_TEMPLATE_INFO (arg)
c8094d83 10162 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
68361a03 10163 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
4393e105
MM
10164 return NULL_TREE;
10165
10166 /* We need to make a new template argument vector for the call to
10167 unify. If we used TARGS, we'd clutter it up with the result of
10168 the attempted unification, even if this class didn't work out.
10169 We also don't want to commit ourselves to all the unifications
10170 we've already done, since unification is supposed to be done on
10171 an argument-by-argument basis. In other words, consider the
10172 following pathological case:
10173
10174 template <int I, int J, int K>
10175 struct S {};
c8094d83 10176
4393e105
MM
10177 template <int I, int J>
10178 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
c8094d83 10179
4393e105
MM
10180 template <int I, int J, int K>
10181 void f(S<I, J, K>, S<I, I, I>);
c8094d83 10182
4393e105 10183 void g() {
0cbd7506
MS
10184 S<0, 0, 0> s0;
10185 S<0, 1, 2> s2;
c8094d83 10186
0cbd7506 10187 f(s0, s2);
4393e105
MM
10188 }
10189
10190 Now, by the time we consider the unification involving `s2', we
10191 already know that we must have `f<0, 0, 0>'. But, even though
0e339752 10192 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
4393e105
MM
10193 because there are two ways to unify base classes of S<0, 1, 2>
10194 with S<I, I, I>. If we kept the already deduced knowledge, we
10195 would reject the possibility I=1. */
f31c0a32 10196 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
c8094d83 10197
4393e105 10198 /* If unification failed, we're done. */
74601d7c
KL
10199 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
10200 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
4393e105 10201 return NULL_TREE;
74601d7c
KL
10202
10203 return arg;
4393e105
MM
10204}
10205
a3a0fc7f
NS
10206/* Given a template type PARM and a class type ARG, find the unique
10207 base type in ARG that is an instance of PARM. We do not examine
10208 ARG itself; only its base-classes. If there is not exactly one
10209 appropriate base class, return NULL_TREE. PARM may be the type of
10210 a partial specialization, as well as a plain template type. Used
10211 by unify. */
4393e105
MM
10212
10213static tree
a3a0fc7f 10214get_template_base (tree tparms, tree targs, tree parm, tree arg)
fcfb9f96 10215{
a3a0fc7f
NS
10216 tree rval = NULL_TREE;
10217 tree binfo;
10218
10219 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
c8094d83 10220
a3a0fc7f
NS
10221 binfo = TYPE_BINFO (complete_type (arg));
10222 if (!binfo)
10223 /* The type could not be completed. */
10224 return NULL_TREE;
fcfb9f96 10225
a3a0fc7f
NS
10226 /* Walk in inheritance graph order. The search order is not
10227 important, and this avoids multiple walks of virtual bases. */
10228 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
fcfb9f96 10229 {
a3a0fc7f 10230 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
fcfb9f96 10231
8d83768f
NS
10232 if (r)
10233 {
10234 /* If there is more than one satisfactory baseclass, then:
fcfb9f96 10235
8d83768f 10236 [temp.deduct.call]
fcfb9f96 10237
8d83768f
NS
10238 If they yield more than one possible deduced A, the type
10239 deduction fails.
4393e105 10240
8d83768f 10241 applies. */
a3a0fc7f
NS
10242 if (rval && !same_type_p (r, rval))
10243 return NULL_TREE;
c8094d83 10244
a3a0fc7f 10245 rval = r;
8d83768f 10246 }
fcfb9f96
MM
10247 }
10248
a3a0fc7f 10249 return rval;
fcfb9f96
MM
10250}
10251
db2767b6
MM
10252/* Returns the level of DECL, which declares a template parameter. */
10253
e9659ab0 10254static int
3a978d72 10255template_decl_level (tree decl)
db2767b6
MM
10256{
10257 switch (TREE_CODE (decl))
10258 {
10259 case TYPE_DECL:
10260 case TEMPLATE_DECL:
10261 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
10262
10263 case PARM_DECL:
10264 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
10265
10266 default:
315fb5db 10267 gcc_unreachable ();
db2767b6 10268 }
315fb5db 10269 return 0;
db2767b6
MM
10270}
10271
830bfa74
MM
10272/* Decide whether ARG can be unified with PARM, considering only the
10273 cv-qualifiers of each type, given STRICT as documented for unify.
324f9dfb 10274 Returns nonzero iff the unification is OK on that basis. */
e92cc029 10275
e9659ab0 10276static int
3a978d72 10277check_cv_quals_for_unify (int strict, tree arg, tree parm)
830bfa74 10278{
4f2b0fb2
NS
10279 int arg_quals = cp_type_quals (arg);
10280 int parm_quals = cp_type_quals (parm);
10281
355f774d
NS
10282 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10283 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 10284 {
2e9ceb77 10285 /* Although a CVR qualifier is ignored when being applied to a
0cbd7506
MS
10286 substituted template parameter ([8.3.2]/1 for example), that
10287 does not apply during deduction [14.8.2.4]/1, (even though
10288 that is not explicitly mentioned, [14.8.2.4]/9 indicates
10289 this). Except when we're allowing additional CV qualifiers
10290 at the outer level [14.8.2.1]/3,1st bullet. */
2e9ceb77
NS
10291 if ((TREE_CODE (arg) == REFERENCE_TYPE
10292 || TREE_CODE (arg) == FUNCTION_TYPE
10293 || TREE_CODE (arg) == METHOD_TYPE)
10294 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
10295 return 0;
10296
10297 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
10298 && (parm_quals & TYPE_QUAL_RESTRICT))
10299 return 0;
4f2b0fb2 10300 }
2e9ceb77 10301
62e4a758 10302 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 10303 && (arg_quals & parm_quals) != parm_quals)
ef637255
MM
10304 return 0;
10305
62e4a758 10306 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
4f2b0fb2 10307 && (parm_quals & arg_quals) != arg_quals)
ef637255
MM
10308 return 0;
10309
ef637255 10310 return 1;
830bfa74
MM
10311}
10312
916b63c3
MM
10313/* Deduce the value of template parameters. TPARMS is the (innermost)
10314 set of template parameters to a template. TARGS is the bindings
10315 for those template parameters, as determined thus far; TARGS may
10316 include template arguments for outer levels of template parameters
10317 as well. PARM is a parameter to a template function, or a
10318 subcomponent of that parameter; ARG is the corresponding argument.
10319 This function attempts to match PARM with ARG in a manner
10320 consistent with the existing assignments in TARGS. If more values
10321 are deduced, then TARGS is updated.
10322
10323 Returns 0 if the type deduction succeeds, 1 otherwise. The
10324 parameter STRICT is a bitwise or of the following flags:
830bfa74
MM
10325
10326 UNIFY_ALLOW_NONE:
10327 Require an exact match between PARM and ARG.
10328 UNIFY_ALLOW_MORE_CV_QUAL:
028d1f20
NS
10329 Allow the deduced ARG to be more cv-qualified (by qualification
10330 conversion) than ARG.
830bfa74
MM
10331 UNIFY_ALLOW_LESS_CV_QUAL:
10332 Allow the deduced ARG to be less cv-qualified than ARG.
10333 UNIFY_ALLOW_DERIVED:
10334 Allow the deduced ARG to be a template base class of ARG,
10335 or a pointer to a template base class of the type pointed to by
161c12b0
JM
10336 ARG.
10337 UNIFY_ALLOW_INTEGER:
10338 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
c8094d83 10339 case for more information.
028d1f20
NS
10340 UNIFY_ALLOW_OUTER_LEVEL:
10341 This is the outermost level of a deduction. Used to determine validity
10342 of qualification conversions. A valid qualification conversion must
10343 have const qualified pointers leading up to the inner type which
10344 requires additional CV quals, except at the outer level, where const
10345 is not required [conv.qual]. It would be normal to set this flag in
62e4a758
NS
10346 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10347 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10348 This is the outermost level of a deduction, and PARM can be more CV
10349 qualified at this point.
10350 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10351 This is the outermost level of a deduction, and PARM can be less CV
8baddbf1 10352 qualified at this point. */
830bfa74 10353
e9659ab0 10354static int
3a978d72 10355unify (tree tparms, tree targs, tree parm, tree arg, int strict)
8d08fdba
MS
10356{
10357 int idx;
050367a3 10358 tree targ;
db2767b6 10359 tree tparm;
028d1f20 10360 int strict_in = strict;
8d08fdba
MS
10361
10362 /* I don't think this will do the right thing with respect to types.
10363 But the only case I've seen it in so far has been array bounds, where
10364 signedness is the only information lost, and I think that will be
10365 okay. */
10366 while (TREE_CODE (parm) == NOP_EXPR)
10367 parm = TREE_OPERAND (parm, 0);
10368
10369 if (arg == error_mark_node)
10370 return 1;
10371 if (arg == unknown_type_node)
34016c81
JM
10372 /* We can't deduce anything from this, but we might get all the
10373 template args from other function args. */
10374 return 0;
10375
db2767b6 10376 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 10377 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
10378 template parameters. We might need them if we're trying to
10379 figure out which of two things is more specialized. */
10380 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
10381 return 0;
10382
830bfa74
MM
10383 /* Immediately reject some pairs that won't unify because of
10384 cv-qualification mismatches. */
10385 if (TREE_CODE (arg) == TREE_CODE (parm)
2f939d94 10386 && TYPE_P (arg)
d0ab7624 10387 /* It is the elements of the array which hold the cv quals of an array
0cbd7506
MS
10388 type, and the elements might be template type parms. We'll check
10389 when we recurse. */
d0ab7624 10390 && TREE_CODE (arg) != ARRAY_TYPE
830bfa74
MM
10391 /* We check the cv-qualifiers when unifying with template type
10392 parameters below. We want to allow ARG `const T' to unify with
10393 PARM `T' for example, when computing which of two templates
10394 is more specialized, for example. */
10395 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
028d1f20 10396 && !check_cv_quals_for_unify (strict_in, arg, parm))
49432171
JM
10397 return 1;
10398
028d1f20 10399 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
3ea099f1 10400 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
028d1f20
NS
10401 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10402 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10403 strict &= ~UNIFY_ALLOW_DERIVED;
62e4a758
NS
10404 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10405 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
c8094d83 10406
8d08fdba
MS
10407 switch (TREE_CODE (parm))
10408 {
2ca340ae 10409 case TYPENAME_TYPE:
fccef71e 10410 case SCOPE_REF:
b8c6534b 10411 case UNBOUND_CLASS_TEMPLATE:
2ca340ae
JM
10412 /* In a type which contains a nested-name-specifier, template
10413 argument values cannot be deduced for template parameters used
10414 within the nested-name-specifier. */
10415 return 0;
10416
8d08fdba 10417 case TEMPLATE_TYPE_PARM:
73b0fce8 10418 case TEMPLATE_TEMPLATE_PARM:
a1281f45 10419 case BOUND_TEMPLATE_TEMPLATE_PARM:
db2767b6
MM
10420 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10421
10422 if (TEMPLATE_TYPE_LEVEL (parm)
10423 != template_decl_level (tparm))
10424 /* The PARM is not one we're trying to unify. Just check
10425 to see if it matches ARG. */
10426 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 10427 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 10428 idx = TEMPLATE_TYPE_IDX (parm);
916b63c3 10429 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
db2767b6 10430 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 10431
73b0fce8 10432 /* Check for mixed types and values. */
db2767b6
MM
10433 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10434 && TREE_CODE (tparm) != TYPE_DECL)
c8094d83 10435 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
db2767b6 10436 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
10437 return 1;
10438
a1281f45 10439 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
73b0fce8 10440 {
b429fdf0
KL
10441 /* ARG must be constructed from a template class or a template
10442 template parameter. */
10443 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
4e95268d 10444 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
a1281f45 10445 return 1;
73b0fce8 10446
a1281f45 10447 {
a1281f45 10448 tree parmvec = TYPE_TI_ARGS (parm);
6df91b00 10449 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
a1281f45 10450 tree argtmplvec
b429fdf0 10451 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
a1281f45 10452 int i;
73b0fce8 10453
e7e93965
MM
10454 /* The resolution to DR150 makes clear that default
10455 arguments for an N-argument may not be used to bind T
10456 to a template template parameter with fewer than N
10457 parameters. It is not safe to permit the binding of
10458 default arguments as an extension, as that may change
10459 the meaning of a conforming program. Consider:
10460
10461 struct Dense { static const unsigned int dim = 1; };
10462
10463 template <template <typename> class View,
10464 typename Block>
10465 void operator+(float, View<Block> const&);
10466
3db45ab5
MS
10467 template <typename Block,
10468 unsigned int Dim = Block::dim>
e7e93965
MM
10469 struct Lvalue_proxy { operator float() const; };
10470
10471 void
10472 test_1d (void) {
10473 Lvalue_proxy<Dense> p;
10474 float b;
10475 b + p;
10476 }
a1281f45 10477
e7e93965
MM
10478 Here, if Lvalue_proxy is permitted to bind to View, then
10479 the global operator+ will be used; if they are not, the
3db45ab5
MS
10480 Lvalue_proxy will be converted to float. */
10481 if (coerce_template_parms (argtmplvec, parmvec,
e7e93965
MM
10482 TYPE_TI_TEMPLATE (parm),
10483 tf_none,
10484 /*require_all_args=*/true,
10485 /*use_default_args=*/false)
0cbd7506 10486 == error_mark_node)
a1281f45 10487 return 1;
c8094d83
MS
10488
10489 /* Deduce arguments T, i from TT<T> or TT<i>.
a1281f45
KL
10490 We check each element of PARMVEC and ARGVEC individually
10491 rather than the whole TREE_VEC since they can have
10492 different number of elements. */
6b9b6b15 10493
a1281f45
KL
10494 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10495 {
0cbd7506 10496 if (unify (tparms, targs,
c8094d83
MS
10497 TREE_VEC_ELT (parmvec, i),
10498 TREE_VEC_ELT (argvec, i),
a1281f45
KL
10499 UNIFY_ALLOW_NONE))
10500 return 1;
73b0fce8 10501 }
a1281f45 10502 }
b429fdf0 10503 arg = TYPE_TI_TEMPLATE (arg);
a1281f45
KL
10504
10505 /* Fall through to deduce template name. */
10506 }
10507
10508 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10509 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10510 {
10511 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10512
10513 /* Simple cases: Value already set, does match or doesn't. */
10514 if (targ != NULL_TREE && template_args_equal (targ, arg))
10515 return 0;
10516 else if (targ)
10517 return 1;
db2767b6
MM
10518 }
10519 else
10520 {
830bfa74
MM
10521 /* If PARM is `const T' and ARG is only `int', we don't have
10522 a match unless we are allowing additional qualification.
10523 If ARG is `const int' and PARM is just `T' that's OK;
10524 that binds `const int' to `T'. */
c8094d83 10525 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
830bfa74 10526 arg, parm))
db2767b6
MM
10527 return 1;
10528
830bfa74
MM
10529 /* Consider the case where ARG is `const volatile int' and
10530 PARM is `const T'. Then, T should be `volatile int'. */
c2ea3a40
NS
10531 arg = cp_build_qualified_type_real
10532 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
adecb3f4
MM
10533 if (arg == error_mark_node)
10534 return 1;
73b0fce8 10535
a1281f45
KL
10536 /* Simple cases: Value already set, does match or doesn't. */
10537 if (targ != NULL_TREE && same_type_p (targ, arg))
10538 return 0;
10539 else if (targ)
10540 return 1;
61cd552e 10541
94fc547c
MM
10542 /* Make sure that ARG is not a variable-sized array. (Note
10543 that were talking about variable-sized arrays (like
10544 `int[n]'), rather than arrays of unknown size (like
10545 `int[]').) We'll get very confused by such a type since
10546 the bound of the array will not be computable in an
10547 instantiation. Besides, such types are not allowed in
10548 ISO C++, so we can do as we please here. */
5377d5ba 10549 if (variably_modified_type_p (arg, NULL_TREE))
94fc547c
MM
10550 return 1;
10551 }
61cd552e 10552
916b63c3 10553 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
73b0fce8
KL
10554 return 0;
10555
f84b4be9 10556 case TEMPLATE_PARM_INDEX:
db2767b6 10557 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
3e9ac7e5
VR
10558 if (tparm == error_mark_node)
10559 return 1;
db2767b6 10560
c8094d83 10561 if (TEMPLATE_PARM_LEVEL (parm)
db2767b6
MM
10562 != template_decl_level (tparm))
10563 /* The PARM is not one we're trying to unify. Just check
10564 to see if it matches ARG. */
c8a209ca
NS
10565 return !(TREE_CODE (arg) == TREE_CODE (parm)
10566 && cp_tree_equal (parm, arg));
db2767b6 10567
f84b4be9 10568 idx = TEMPLATE_PARM_IDX (parm);
916b63c3 10569 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
db2767b6 10570
050367a3 10571 if (targ)
c8a209ca 10572 return !cp_tree_equal (targ, arg);
8d08fdba 10573
161c12b0
JM
10574 /* [temp.deduct.type] If, in the declaration of a function template
10575 with a non-type template-parameter, the non-type
10576 template-parameter is used in an expression in the function
10577 parameter-list and, if the corresponding template-argument is
10578 deduced, the template-argument type shall match the type of the
10579 template-parameter exactly, except that a template-argument
c8094d83 10580 deduced from an array bound may be of any integral type.
d7c4edd0 10581 The non-type parameter might use already deduced type parameters. */
bd0d5d4a 10582 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
e2005c8d
KL
10583 if (!TREE_TYPE (arg))
10584 /* Template-parameter dependent expression. Just accept it for now.
10585 It will later be processed in convert_template_argument. */
10586 ;
10587 else if (same_type_p (TREE_TYPE (arg), tparm))
10588 /* OK */;
161c12b0 10589 else if ((strict & UNIFY_ALLOW_INTEGER)
bd0d5d4a
JM
10590 && (TREE_CODE (tparm) == INTEGER_TYPE
10591 || TREE_CODE (tparm) == BOOLEAN_TYPE))
8baddbf1
MM
10592 /* Convert the ARG to the type of PARM; the deduced non-type
10593 template argument must exactly match the types of the
10594 corresponding parameter. */
10595 arg = fold (build_nop (TREE_TYPE (parm), arg));
bd0d5d4a
JM
10596 else if (uses_template_parms (tparm))
10597 /* We haven't deduced the type of this parameter yet. Try again
10598 later. */
10599 return 0;
161c12b0
JM
10600 else
10601 return 1;
10602
916b63c3 10603 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
8d08fdba
MS
10604 return 0;
10605
28e8f3a0
GB
10606 case PTRMEM_CST:
10607 {
0cbd7506
MS
10608 /* A pointer-to-member constant can be unified only with
10609 another constant. */
28e8f3a0 10610 if (TREE_CODE (arg) != PTRMEM_CST)
0cbd7506 10611 return 1;
28e8f3a0
GB
10612
10613 /* Just unify the class member. It would be useless (and possibly
0cbd7506
MS
10614 wrong, depending on the strict flags) to unify also
10615 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10616 arg refer to the same variable, even if through different
10617 classes. For instance:
28e8f3a0 10618
0cbd7506
MS
10619 struct A { int x; };
10620 struct B : A { };
28e8f3a0 10621
0cbd7506 10622 Unification of &A::x and &B::x must succeed. */
28e8f3a0 10623 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
0cbd7506 10624 PTRMEM_CST_MEMBER (arg), strict);
28e8f3a0
GB
10625 }
10626
8d08fdba 10627 case POINTER_TYPE:
830bfa74 10628 {
830bfa74
MM
10629 if (TREE_CODE (arg) != POINTER_TYPE)
10630 return 1;
c8094d83 10631
830bfa74
MM
10632 /* [temp.deduct.call]
10633
10634 A can be another pointer or pointer to member type that can
10635 be converted to the deduced A via a qualification
10636 conversion (_conv.qual_).
10637
10638 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10639 This will allow for additional cv-qualification of the
028d1f20 10640 pointed-to types if appropriate. */
c8094d83 10641
028d1f20 10642 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
830bfa74
MM
10643 /* The derived-to-base conversion only persists through one
10644 level of pointers. */
028d1f20 10645 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
830bfa74 10646
c8094d83 10647 return unify (tparms, targs, TREE_TYPE (parm),
028d1f20 10648 TREE_TYPE (arg), strict);
830bfa74 10649 }
8d08fdba
MS
10650
10651 case REFERENCE_TYPE:
830bfa74
MM
10652 if (TREE_CODE (arg) != REFERENCE_TYPE)
10653 return 1;
10654 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
028d1f20 10655 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
10656
10657 case ARRAY_TYPE:
10658 if (TREE_CODE (arg) != ARRAY_TYPE)
10659 return 1;
3042d5be
MM
10660 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10661 != (TYPE_DOMAIN (arg) == NULL_TREE))
10662 return 1;
8baddbf1
MM
10663 if (TYPE_DOMAIN (parm) != NULL_TREE)
10664 {
10665 tree parm_max;
10666 tree arg_max;
6ee3ffe8
MM
10667 bool parm_cst;
10668 bool arg_cst;
8baddbf1
MM
10669
10670 /* Our representation of array types uses "N - 1" as the
10671 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
6ee3ffe8
MM
10672 not an integer constant. We cannot unify arbitrarily
10673 complex expressions, so we eliminate the MINUS_EXPRs
10674 here. */
10675 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10676 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
10677 if (!parm_cst)
8baddbf1 10678 {
6ee3ffe8 10679 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
8baddbf1
MM
10680 parm_max = TREE_OPERAND (parm_max, 0);
10681 }
6ee3ffe8
MM
10682 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10683 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
10684 if (!arg_cst)
10685 {
10686 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
10687 trying to unify the type of a variable with the type
10688 of a template parameter. For example:
10689
10690 template <unsigned int N>
10691 void f (char (&) [N]);
10692 int g();
10693 void h(int i) {
10694 char a[g(i)];
10695 f(a);
10696 }
10697
10698 Here, the type of the ARG will be "int [g(i)]", and
10699 may be a SAVE_EXPR, etc. */
10700 if (TREE_CODE (arg_max) != MINUS_EXPR)
10701 return 1;
10702 arg_max = TREE_OPERAND (arg_max, 0);
10703 }
10704
10705 /* If only one of the bounds used a MINUS_EXPR, compensate
10706 by adding one to the other bound. */
10707 if (parm_cst && !arg_cst)
10708 parm_max = fold_build2 (PLUS_EXPR,
10709 integer_type_node,
10710 parm_max,
10711 integer_one_node);
10712 else if (arg_cst && !parm_cst)
10713 arg_max = fold_build2 (PLUS_EXPR,
10714 integer_type_node,
10715 arg_max,
10716 integer_one_node);
8baddbf1
MM
10717
10718 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10719 return 1;
10720 }
830bfa74 10721 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
712467a4 10722 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
10723
10724 case REAL_TYPE:
37c46b43 10725 case COMPLEX_TYPE:
c00996a3 10726 case VECTOR_TYPE:
8d08fdba 10727 case INTEGER_TYPE:
42976354 10728 case BOOLEAN_TYPE:
3590f0a6 10729 case ENUMERAL_TYPE:
5ad5a526 10730 case VOID_TYPE:
f376e137
MS
10731 if (TREE_CODE (arg) != TREE_CODE (parm))
10732 return 1;
c8094d83 10733
9edc3913 10734 /* We have already checked cv-qualification at the top of the
514a1f18 10735 function. */
8baddbf1 10736 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
ca79f85d
JM
10737 return 1;
10738
8d08fdba
MS
10739 /* As far as unification is concerned, this wins. Later checks
10740 will invalidate it if necessary. */
10741 return 0;
10742
10743 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 10744 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 10745 case INTEGER_CST:
bd6dd845
MS
10746 while (TREE_CODE (arg) == NOP_EXPR)
10747 arg = TREE_OPERAND (arg, 0);
10748
8d08fdba
MS
10749 if (TREE_CODE (arg) != INTEGER_CST)
10750 return 1;
10751 return !tree_int_cst_equal (parm, arg);
10752
8d08fdba
MS
10753 case TREE_VEC:
10754 {
10755 int i;
10756 if (TREE_CODE (arg) != TREE_VEC)
10757 return 1;
10758 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10759 return 1;
0dc09a61 10760 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
830bfa74 10761 if (unify (tparms, targs,
8d08fdba 10762 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
4393e105 10763 UNIFY_ALLOW_NONE))
8d08fdba
MS
10764 return 1;
10765 return 0;
10766 }
10767
8d08fdba 10768 case RECORD_TYPE:
f181d4ae 10769 case UNION_TYPE:
f181d4ae 10770 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 10771 return 1;
c8094d83 10772
a7a64a77
MM
10773 if (TYPE_PTRMEMFUNC_P (parm))
10774 {
10775 if (!TYPE_PTRMEMFUNC_P (arg))
10776 return 1;
10777
c8094d83 10778 return unify (tparms, targs,
a7a64a77
MM
10779 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10780 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10781 strict);
10782 }
10783
5db698f6 10784 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 10785 {
6467930b 10786 tree t = NULL_TREE;
4393e105 10787
028d1f20 10788 if (strict_in & UNIFY_ALLOW_DERIVED)
4393e105
MM
10789 {
10790 /* First, we try to unify the PARM and ARG directly. */
10791 t = try_class_unification (tparms, targs,
10792 parm, arg);
10793
10794 if (!t)
10795 {
10796 /* Fallback to the special case allowed in
10797 [temp.deduct.call]:
c8094d83 10798
4393e105
MM
10799 If P is a class, and P has the form
10800 template-id, then A can be a derived class of
10801 the deduced A. Likewise, if P is a pointer to
10802 a class of the form template-id, A can be a
10803 pointer to a derived class pointed to by the
10804 deduced A. */
8d83768f 10805 t = get_template_base (tparms, targs, parm, arg);
4393e105 10806
8d83768f 10807 if (!t)
4393e105
MM
10808 return 1;
10809 }
10810 }
c8094d83
MS
10811 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10812 && (CLASSTYPE_TI_TEMPLATE (parm)
9fbf56f7 10813 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
10814 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10815 Then, we should unify `int' and `U'. */
6467930b 10816 t = arg;
4393e105 10817 else
dc957d14 10818 /* There's no chance of unification succeeding. */
5566b478 10819 return 1;
6467930b 10820
830bfa74 10821 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
4393e105 10822 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
5566b478 10823 }
9edc3913 10824 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
5566b478 10825 return 1;
a4443a08 10826 return 0;
8d08fdba
MS
10827
10828 case METHOD_TYPE:
8d08fdba 10829 case FUNCTION_TYPE:
830bfa74 10830 if (TREE_CODE (arg) != TREE_CODE (parm))
8d08fdba 10831 return 1;
830bfa74 10832
38d18b1a 10833 /* CV qualifications for methods can never be deduced, they must
3db45ab5
MS
10834 match exactly. We need to check them explicitly here,
10835 because type_unification_real treats them as any other
10836 cvqualified parameter. */
38d18b1a
NS
10837 if (TREE_CODE (parm) == METHOD_TYPE
10838 && (!check_cv_quals_for_unify
10839 (UNIFY_ALLOW_NONE,
10840 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10841 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10842 return 1;
10843
830bfa74 10844 if (unify (tparms, targs, TREE_TYPE (parm),
4393e105 10845 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
28cbf42c 10846 return 1;
386b8a85 10847 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
30f86ec3
FJ
10848 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10849 LOOKUP_NORMAL);
a4443a08
MS
10850
10851 case OFFSET_TYPE:
9804209d 10852 /* Unify a pointer to member with a pointer to member function, which
0cbd7506 10853 deduces the type of the member as a function type. */
9804209d 10854 if (TYPE_PTRMEMFUNC_P (arg))
0cbd7506
MS
10855 {
10856 tree method_type;
10857 tree fntype;
10858 cp_cv_quals cv_quals;
10859
10860 /* Check top-level cv qualifiers */
10861 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10862 return 1;
10863
10864 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10865 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10866 return 1;
10867
10868 /* Determine the type of the function we are unifying against. */
10869 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10870 fntype =
10871 build_function_type (TREE_TYPE (method_type),
10872 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10873
10874 /* Extract the cv-qualifiers of the member function from the
10875 implicit object parameter and place them on the function
10876 type to be restored later. */
10877 cv_quals =
10878 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10879 fntype = build_qualified_type (fntype, cv_quals);
10880 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10881 }
9804209d 10882
a4443a08
MS
10883 if (TREE_CODE (arg) != OFFSET_TYPE)
10884 return 1;
830bfa74 10885 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
4393e105 10886 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
a4443a08 10887 return 1;
830bfa74 10888 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 10889 strict);
a4443a08 10890
f62dbf03 10891 case CONST_DECL:
a723baf1
MM
10892 if (DECL_TEMPLATE_PARM_P (parm))
10893 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
c8094d83 10894 if (arg != integral_constant_value (parm))
f62dbf03
JM
10895 return 1;
10896 return 0;
10897
28e8f3a0 10898 case FIELD_DECL:
027905b4
KL
10899 case TEMPLATE_DECL:
10900 /* Matched cases are handled by the ARG == PARM test above. */
10901 return 1;
10902
8d08fdba 10903 default:
6615c446 10904 gcc_assert (EXPR_P (parm));
c8094d83 10905
98ddffc1 10906 /* We must be looking at an expression. This can happen with
c8094d83
MS
10907 something like:
10908
98ddffc1
NS
10909 template <int I>
10910 void foo(S<I>, S<I + 2>);
050367a3 10911
98ddffc1 10912 This is a "nondeduced context":
050367a3 10913
98ddffc1 10914 [deduct.type]
c8094d83 10915
98ddffc1 10916 The nondeduced contexts are:
050367a3 10917
98ddffc1
NS
10918 --A type that is a template-id in which one or more of
10919 the template-arguments is an expression that references
c8094d83 10920 a template-parameter.
050367a3 10921
98ddffc1
NS
10922 In these cases, we assume deduction succeeded, but don't
10923 actually infer any unifications. */
74601d7c 10924
98ddffc1
NS
10925 if (!uses_template_parms (parm)
10926 && !template_args_equal (parm, arg))
10927 return 1;
10928 else
10929 return 0;
8d08fdba
MS
10930 }
10931}
8d08fdba 10932\f
4684cd27
MM
10933/* Note that DECL can be defined in this translation unit, if
10934 required. */
10935
10936static void
10937mark_definable (tree decl)
10938{
10939 tree clone;
10940 DECL_NOT_REALLY_EXTERN (decl) = 1;
10941 FOR_EACH_CLONE (clone, decl)
10942 DECL_NOT_REALLY_EXTERN (clone) = 1;
10943}
10944
03d0f4af 10945/* Called if RESULT is explicitly instantiated, or is a member of an
4684cd27 10946 explicitly instantiated class. */
03d0f4af 10947
faae18ab 10948void
3a978d72 10949mark_decl_instantiated (tree result, int extern_p)
faae18ab 10950{
415c974c 10951 SET_DECL_EXPLICIT_INSTANTIATION (result);
3ae18eaf 10952
1f6f0cb6
MM
10953 /* If this entity has already been written out, it's too late to
10954 make any modifications. */
10955 if (TREE_ASM_WRITTEN (result))
10956 return;
10957
10958 if (TREE_CODE (result) != FUNCTION_DECL)
10959 /* The TREE_PUBLIC flag for function declarations will have been
10960 set correctly by tsubst. */
10961 TREE_PUBLIC (result) = 1;
10962
346eeb15
JM
10963 /* This might have been set by an earlier implicit instantiation. */
10964 DECL_COMDAT (result) = 0;
10965
4684cd27
MM
10966 if (extern_p)
10967 DECL_NOT_REALLY_EXTERN (result) = 0;
10968 else
faae18ab 10969 {
4684cd27 10970 mark_definable (result);
1a408d07
JM
10971 /* Always make artificials weak. */
10972 if (DECL_ARTIFICIAL (result) && flag_weak)
10973 comdat_linkage (result);
a7d87521
JM
10974 /* For WIN32 we also want to put explicit instantiations in
10975 linkonce sections. */
1a408d07 10976 else if (TREE_PUBLIC (result))
b385c841 10977 maybe_make_one_only (result);
faae18ab 10978 }
c8094d83 10979
4684cd27
MM
10980 /* If EXTERN_P, then this function will not be emitted -- unless
10981 followed by an explicit instantiation, at which point its linkage
10982 will be adjusted. If !EXTERN_P, then this function will be
10983 emitted here. In neither circumstance do we want
10984 import_export_decl to adjust the linkage. */
c8094d83 10985 DECL_INTERFACE_KNOWN (result) = 1;
faae18ab
MS
10986}
10987
e5214479 10988/* Given two function templates PAT1 and PAT2, return:
6467930b
MS
10989
10990 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10991 -1 if PAT2 is more specialized than PAT1.
e5214479
JM
10992 0 if neither is more specialized.
10993
dda04398
NS
10994 LEN indicates the number of parameters we should consider
10995 (defaulted parameters should not be considered).
10996
10997 The 1998 std underspecified function template partial ordering, and
10998 DR214 addresses the issue. We take pairs of arguments, one from
c51940a2 10999 each of the templates, and deduce them against each other. One of
dda04398
NS
11000 the templates will be more specialized if all the *other*
11001 template's arguments deduce against its arguments and at least one
11002 of its arguments *does* *not* deduce against the other template's
11003 corresponding argument. Deduction is done as for class templates.
11004 The arguments used in deduction have reference and top level cv
11005 qualifiers removed. Iff both arguments were originally reference
11006 types *and* deduction succeeds in both directions, the template
11007 with the more cv-qualified argument wins for that pairing (if
11008 neither is more cv-qualified, they both are equal). Unlike regular
11009 deduction, after all the arguments have been deduced in this way,
11010 we do *not* verify the deduced template argument values can be
11011 substituted into non-deduced contexts, nor do we have to verify
11012 that all template arguments have been deduced. */
c8094d83 11013
6467930b 11014int
dda04398
NS
11015more_specialized_fn (tree pat1, tree pat2, int len)
11016{
11017 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
11018 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
11019 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
11020 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
11021 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
11022 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
11023 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
11024 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
11025 int better1 = 0;
11026 int better2 = 0;
3db45ab5 11027
48884537
NS
11028 /* Remove the this parameter from non-static member functions. If
11029 one is a non-static member function and the other is not a static
11030 member function, remove the first parameter from that function
11031 also. This situation occurs for operator functions where we
11032 locate both a member function (with this pointer) and non-member
11033 operator (with explicit first operand). */
dda04398 11034 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
48884537
NS
11035 {
11036 len--; /* LEN is the number of significant arguments for DECL1 */
11037 args1 = TREE_CHAIN (args1);
11038 if (!DECL_STATIC_FUNCTION_P (decl2))
11039 args2 = TREE_CHAIN (args2);
11040 }
11041 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
11042 {
11043 args2 = TREE_CHAIN (args2);
11044 if (!DECL_STATIC_FUNCTION_P (decl1))
11045 {
11046 len--;
11047 args1 = TREE_CHAIN (args1);
11048 }
11049 }
3db45ab5 11050
ee307009
NS
11051 /* If only one is a conversion operator, they are unordered. */
11052 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
11053 return 0;
c8094d83 11054
dda04398
NS
11055 /* Consider the return type for a conversion function */
11056 if (DECL_CONV_FN_P (decl1))
11057 {
dda04398
NS
11058 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
11059 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
11060 len++;
11061 }
c8094d83 11062
dda04398 11063 processing_template_decl++;
c8094d83 11064
dda04398
NS
11065 while (len--)
11066 {
11067 tree arg1 = TREE_VALUE (args1);
11068 tree arg2 = TREE_VALUE (args2);
11069 int deduce1, deduce2;
11070 int quals1 = -1;
11071 int quals2 = -1;
6467930b 11072
dda04398
NS
11073 if (TREE_CODE (arg1) == REFERENCE_TYPE)
11074 {
11075 arg1 = TREE_TYPE (arg1);
11076 quals1 = cp_type_quals (arg1);
11077 }
c8094d83 11078
dda04398
NS
11079 if (TREE_CODE (arg2) == REFERENCE_TYPE)
11080 {
11081 arg2 = TREE_TYPE (arg2);
11082 quals2 = cp_type_quals (arg2);
11083 }
6467930b 11084
dda04398
NS
11085 if ((quals1 < 0) != (quals2 < 0))
11086 {
11087 /* Only of the args is a reference, see if we should apply
11088 array/function pointer decay to it. This is not part of
11089 DR214, but is, IMHO, consistent with the deduction rules
11090 for the function call itself, and with our earlier
11091 implementation of the underspecified partial ordering
11092 rules. (nathan). */
11093 if (quals1 >= 0)
11094 {
11095 switch (TREE_CODE (arg1))
11096 {
11097 case ARRAY_TYPE:
11098 arg1 = TREE_TYPE (arg1);
11099 /* FALLTHROUGH. */
11100 case FUNCTION_TYPE:
11101 arg1 = build_pointer_type (arg1);
11102 break;
c8094d83 11103
dda04398
NS
11104 default:
11105 break;
11106 }
11107 }
11108 else
11109 {
11110 switch (TREE_CODE (arg2))
11111 {
11112 case ARRAY_TYPE:
11113 arg2 = TREE_TYPE (arg2);
11114 /* FALLTHROUGH. */
11115 case FUNCTION_TYPE:
11116 arg2 = build_pointer_type (arg2);
11117 break;
c8094d83 11118
dda04398
NS
11119 default:
11120 break;
11121 }
11122 }
11123 }
c8094d83 11124
dda04398
NS
11125 arg1 = TYPE_MAIN_VARIANT (arg1);
11126 arg2 = TYPE_MAIN_VARIANT (arg2);
c8094d83 11127
dda04398
NS
11128 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
11129 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
11130
11131 if (!deduce1)
11132 better2 = -1;
11133 if (!deduce2)
11134 better1 = -1;
11135 if (better1 < 0 && better2 < 0)
11136 /* We've failed to deduce something in either direction.
11137 These must be unordered. */
11138 break;
c8094d83 11139
dda04398
NS
11140 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
11141 {
11142 /* Deduces in both directions, see if quals can
11143 disambiguate. Pretend the worse one failed to deduce. */
11144 if ((quals1 & quals2) == quals2)
11145 deduce1 = 0;
11146 if ((quals1 & quals2) == quals1)
11147 deduce2 = 0;
11148 }
11149 if (deduce1 && !deduce2 && !better2)
11150 better2 = 1;
11151 if (deduce2 && !deduce1 && !better1)
11152 better1 = 1;
c8094d83 11153
dda04398
NS
11154 args1 = TREE_CHAIN (args1);
11155 args2 = TREE_CHAIN (args2);
11156 }
11157
11158 processing_template_decl--;
11159
11160 return (better1 > 0) - (better2 > 0);
73aad9b9 11161}
6467930b 11162
916b63c3 11163/* Determine which of two partial specializations is more specialized.
6467930b 11164
916b63c3
MM
11165 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
11166 to the first partial specialization. The TREE_VALUE is the
11167 innermost set of template parameters for the partial
11168 specialization. PAT2 is similar, but for the second template.
11169
11170 Return 1 if the first partial specialization is more specialized;
11171 -1 if the second is more specialized; 0 if neither is more
11172 specialized.
55ece1b3 11173
916b63c3 11174 See [temp.class.order] for information about determining which of
3db45ab5 11175 two templates is more specialized. */
c8094d83 11176
b5791fdc 11177static int
916b63c3 11178more_specialized_class (tree pat1, tree pat2)
73aad9b9
JM
11179{
11180 tree targs;
916b63c3 11181 tree tmpl1, tmpl2;
73aad9b9
JM
11182 int winner = 0;
11183
916b63c3
MM
11184 tmpl1 = TREE_TYPE (pat1);
11185 tmpl2 = TREE_TYPE (pat2);
11186
c8094d83 11187 /* Just like what happens for functions, if we are ordering between
baa49730
GB
11188 different class template specializations, we may encounter dependent
11189 types in the arguments, and we need our dependency check functions
11190 to behave correctly. */
11191 ++processing_template_decl;
3db45ab5 11192 targs = get_class_bindings (TREE_VALUE (pat1),
916b63c3
MM
11193 CLASSTYPE_TI_ARGS (tmpl1),
11194 CLASSTYPE_TI_ARGS (tmpl2));
73aad9b9
JM
11195 if (targs)
11196 --winner;
11197
3db45ab5 11198 targs = get_class_bindings (TREE_VALUE (pat2),
916b63c3
MM
11199 CLASSTYPE_TI_ARGS (tmpl2),
11200 CLASSTYPE_TI_ARGS (tmpl1));
73aad9b9 11201 if (targs)
6467930b 11202 ++winner;
baa49730 11203 --processing_template_decl;
6467930b
MS
11204
11205 return winner;
11206}
73aad9b9
JM
11207
11208/* Return the template arguments that will produce the function signature
e1467ff2 11209 DECL from the function template FN, with the explicit template
a34d3336 11210 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
4393e105 11211 also match. Return NULL_TREE if no satisfactory arguments could be
a34d3336 11212 found. */
c8094d83 11213
76b9a14d 11214static tree
a34d3336 11215get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
73aad9b9 11216{
98c1c668 11217 int ntparms = DECL_NTPARMS (fn);
f31c0a32 11218 tree targs = make_tree_vec (ntparms);
4393e105 11219 tree decl_type;
03017874 11220 tree decl_arg_types;
98c1c668 11221
4393e105
MM
11222 /* Substitute the explicit template arguments into the type of DECL.
11223 The call to fn_type_unification will handle substitution into the
11224 FN. */
11225 decl_type = TREE_TYPE (decl);
11226 if (explicit_args && uses_template_parms (decl_type))
11227 {
11228 tree tmpl;
11229 tree converted_args;
11230
11231 if (DECL_TEMPLATE_INFO (decl))
11232 tmpl = DECL_TI_TEMPLATE (decl);
11233 else
0e339752 11234 /* We can get here for some invalid specializations. */
4393e105
MM
11235 return NULL_TREE;
11236
11237 converted_args
e7e93965
MM
11238 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
11239 explicit_args, NULL_TREE,
3db45ab5 11240 tf_none,
e7e93965
MM
11241 /*require_all_args=*/false,
11242 /*use_default_args=*/false);
4393e105
MM
11243 if (converted_args == error_mark_node)
11244 return NULL_TREE;
c8094d83
MS
11245
11246 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
4393e105
MM
11247 if (decl_type == error_mark_node)
11248 return NULL_TREE;
11249 }
11250
11251 decl_arg_types = TYPE_ARG_TYPES (decl_type);
e5214479
JM
11252 /* Never do unification on the 'this' parameter. */
11253 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
03017874 11254 decl_arg_types = TREE_CHAIN (decl_arg_types);
d7684f2d 11255
c8094d83 11256 if (fn_type_unification (fn, explicit_args, targs,
03017874 11257 decl_arg_types,
8d3631f8 11258 (check_rettype || DECL_CONV_FN_P (fn)
0cbd7506 11259 ? TREE_TYPE (decl_type) : NULL_TREE),
30f86ec3 11260 DEDUCE_EXACT, LOOKUP_NORMAL))
76b9a14d
JM
11261 return NULL_TREE;
11262
76b9a14d
JM
11263 return targs;
11264}
11265
36a117a5
MM
11266/* Return the innermost template arguments that, when applied to a
11267 template specialization whose innermost template parameters are
9471d3e2 11268 TPARMS, and whose specialization arguments are PARMS, yield the
c8094d83 11269 ARGS.
36a117a5
MM
11270
11271 For example, suppose we have:
11272
11273 template <class T, class U> struct S {};
11274 template <class T> struct S<T*, int> {};
11275
11276 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
916b63c3 11277 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
36a117a5
MM
11278 int}. The resulting vector will be {double}, indicating that `T'
11279 is bound to `double'. */
11280
bd6dd845 11281static tree
916b63c3 11282get_class_bindings (tree tparms, tree spec_args, tree args)
73aad9b9 11283{
3b3ba9f0 11284 int i, ntparms = TREE_VEC_LENGTH (tparms);
916b63c3
MM
11285 tree deduced_args;
11286 tree innermost_deduced_args;
73aad9b9 11287
916b63c3
MM
11288 innermost_deduced_args = make_tree_vec (ntparms);
11289 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11290 {
11291 deduced_args = copy_node (args);
11292 SET_TMPL_ARGS_LEVEL (deduced_args,
11293 TMPL_ARGS_DEPTH (deduced_args),
11294 innermost_deduced_args);
11295 }
11296 else
3db45ab5 11297 deduced_args = innermost_deduced_args;
916b63c3 11298
3db45ab5
MS
11299 if (unify (tparms, deduced_args,
11300 INNERMOST_TEMPLATE_ARGS (spec_args),
11301 INNERMOST_TEMPLATE_ARGS (args),
0cbd7506 11302 UNIFY_ALLOW_NONE))
fcfb9f96 11303 return NULL_TREE;
73aad9b9
JM
11304
11305 for (i = 0; i < ntparms; ++i)
916b63c3 11306 if (! TREE_VEC_ELT (innermost_deduced_args, i))
73aad9b9
JM
11307 return NULL_TREE;
11308
916b63c3
MM
11309 /* Verify that nondeduced template arguments agree with the type
11310 obtained from argument deduction.
3db45ab5 11311
916b63c3
MM
11312 For example:
11313
11314 struct A { typedef int X; };
11315 template <class T, class U> struct C {};
11316 template <class T> struct C<T, typename T::X> {};
11317
11318 Then with the instantiation `C<A, int>', we can deduce that
11319 `T' is `A' but unify () does not check whether `typename T::X'
11320 is `int'. */
11321 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
11322 if (spec_args == error_mark_node
11323 /* We only need to check the innermost arguments; the other
11324 arguments will always agree. */
11325 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
11326 INNERMOST_TEMPLATE_ARGS (args)))
74601d7c
KL
11327 return NULL_TREE;
11328
916b63c3 11329 return deduced_args;
73aad9b9
JM
11330}
11331
7ca383e6
MM
11332/* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
11333 Return the TREE_LIST node with the most specialized template, if
11334 any. If there is no most specialized template, the error_mark_node
11335 is returned.
11336
11337 Note that this function does not look at, or modify, the
11338 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
11339 returned is one of the elements of INSTANTIATIONS, callers may
11340 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
11341 and retrieve it from the value returned. */
73aad9b9
JM
11342
11343tree
7ca383e6 11344most_specialized_instantiation (tree templates)
73aad9b9 11345{
104bf76a 11346 tree fn, champ;
73aad9b9 11347
dda04398 11348 ++processing_template_decl;
c8094d83 11349
7ca383e6
MM
11350 champ = templates;
11351 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
73aad9b9 11352 {
dda04398 11353 int fate = 0;
c8094d83 11354
a34d3336
NS
11355 if (get_bindings (TREE_VALUE (champ),
11356 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11357 NULL_TREE, /*check_ret=*/false))
dda04398
NS
11358 fate--;
11359
a34d3336
NS
11360 if (get_bindings (TREE_VALUE (fn),
11361 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11362 NULL_TREE, /*check_ret=*/false))
dda04398 11363 fate++;
c8094d83 11364
8ca4bf25
MM
11365 if (fate == -1)
11366 champ = fn;
11367 else if (!fate)
73aad9b9 11368 {
8ca4bf25
MM
11369 /* Equally specialized, move to next function. If there
11370 is no next function, nothing's most specialized. */
11371 fn = TREE_CHAIN (fn);
7ca383e6 11372 champ = fn;
8ca4bf25
MM
11373 if (!fn)
11374 break;
73aad9b9
JM
11375 }
11376 }
c8094d83 11377
dda04398
NS
11378 if (champ)
11379 /* Now verify that champ is better than everything earlier in the
11380 instantiation list. */
7ca383e6 11381 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
a34d3336
NS
11382 if (get_bindings (TREE_VALUE (champ),
11383 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11384 NULL_TREE, /*check_ret=*/false)
11385 || !get_bindings (TREE_VALUE (fn),
11386 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11387 NULL_TREE, /*check_ret=*/false))
dda04398
NS
11388 {
11389 champ = NULL_TREE;
11390 break;
11391 }
c8094d83 11392
dda04398 11393 processing_template_decl--;
c8094d83 11394
dda04398
NS
11395 if (!champ)
11396 return error_mark_node;
73aad9b9 11397
7ca383e6 11398 return champ;
73aad9b9
JM
11399}
11400
36a117a5 11401/* If DECL is a specialization of some template, return the most
f9a7ae04
MM
11402 general such template. Otherwise, returns NULL_TREE.
11403
11404 For example, given:
36a117a5
MM
11405
11406 template <class T> struct S { template <class U> void f(U); };
11407
11408 if TMPL is `template <class U> void S<int>::f(U)' this will return
11409 the full template. This function will not trace past partial
11410 specializations, however. For example, given in addition:
11411
11412 template <class T> struct S<T*> { template <class U> void f(U); };
11413
11414 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11415 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9 11416
612c671a 11417tree
3a978d72 11418most_general_template (tree decl)
73aad9b9 11419{
f9a7ae04
MM
11420 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11421 an immediate specialization. */
11422 if (TREE_CODE (decl) == FUNCTION_DECL)
11423 {
11424 if (DECL_TEMPLATE_INFO (decl)) {
11425 decl = DECL_TI_TEMPLATE (decl);
11426
11427 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11428 template friend. */
11429 if (TREE_CODE (decl) != TEMPLATE_DECL)
11430 return NULL_TREE;
11431 } else
11432 return NULL_TREE;
11433 }
11434
11435 /* Look for more and more general templates. */
11436 while (DECL_TEMPLATE_INFO (decl))
11437 {
10b1d5e7
MM
11438 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11439 (See cp-tree.h for details.) */
f9a7ae04
MM
11440 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11441 break;
11442
6e049fcd
KL
11443 if (CLASS_TYPE_P (TREE_TYPE (decl))
11444 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11445 break;
11446
f9a7ae04
MM
11447 /* Stop if we run into an explicitly specialized class template. */
11448 if (!DECL_NAMESPACE_SCOPE_P (decl)
11449 && DECL_CONTEXT (decl)
11450 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11451 break;
11452
11453 decl = DECL_TI_TEMPLATE (decl);
11454 }
36a117a5
MM
11455
11456 return decl;
11457}
11458
916b63c3
MM
11459/* Return the most specialized of the class template partial
11460 specializations of TMPL which can produce TYPE, a specialization of
11461 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11462 a _TYPE node corresponding to the partial specialization, while the
11463 TREE_PURPOSE is the set of template arguments that must be
11464 substituted into the TREE_TYPE in order to generate TYPE.
11465
11466 If the choice of partial specialization is ambiguous, a diagnostic
11467 is issued, and the error_mark_node is returned. If there are no
11468 partial specializations of TMPL matching TYPE, then NULL_TREE is
11469 returned. */
36a117a5 11470
e9659ab0 11471static tree
916b63c3 11472most_specialized_class (tree type, tree tmpl)
36a117a5
MM
11473{
11474 tree list = NULL_TREE;
11475 tree t;
11476 tree champ;
73aad9b9 11477 int fate;
916b63c3
MM
11478 bool ambiguous_p;
11479 tree args;
73aad9b9 11480
36a117a5 11481 tmpl = most_general_template (tmpl);
916b63c3 11482 args = CLASSTYPE_TI_ARGS (type);
36a117a5 11483 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 11484 {
916b63c3
MM
11485 tree partial_spec_args;
11486 tree spec_args;
11487
11488 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
3db45ab5
MS
11489 spec_args = get_class_bindings (TREE_VALUE (t),
11490 partial_spec_args,
916b63c3 11491 args);
36a117a5 11492 if (spec_args)
73aad9b9 11493 {
916b63c3 11494 list = tree_cons (spec_args, TREE_VALUE (t), list);
73aad9b9
JM
11495 TREE_TYPE (list) = TREE_TYPE (t);
11496 }
11497 }
11498
11499 if (! list)
11500 return NULL_TREE;
11501
916b63c3 11502 ambiguous_p = false;
73aad9b9
JM
11503 t = list;
11504 champ = t;
11505 t = TREE_CHAIN (t);
11506 for (; t; t = TREE_CHAIN (t))
11507 {
916b63c3 11508 fate = more_specialized_class (champ, t);
73aad9b9
JM
11509 if (fate == 1)
11510 ;
11511 else
11512 {
11513 if (fate == 0)
11514 {
11515 t = TREE_CHAIN (t);
11516 if (! t)
916b63c3
MM
11517 {
11518 ambiguous_p = true;
11519 break;
11520 }
73aad9b9
JM
11521 }
11522 champ = t;
11523 }
11524 }
11525
916b63c3
MM
11526 if (!ambiguous_p)
11527 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11528 {
11529 fate = more_specialized_class (champ, t);
11530 if (fate != 1)
11531 {
11532 ambiguous_p = true;
11533 break;
11534 }
11535 }
11536
11537 if (ambiguous_p)
73aad9b9 11538 {
916b63c3
MM
11539 const char *str = "candidates are:";
11540 error ("ambiguous class template instantiation for %q#T", type);
11541 for (t = list; t; t = TREE_CHAIN (t))
11542 {
11543 error ("%s %+#T", str, TREE_TYPE (t));
11544 str = " ";
11545 }
11546 return error_mark_node;
73aad9b9
JM
11547 }
11548
11549 return champ;
11550}
11551
eb8845be 11552/* Explicitly instantiate DECL. */
e92cc029 11553
8d08fdba 11554void
eb8845be 11555do_decl_instantiation (tree decl, tree storage)
8d08fdba 11556{
8d08fdba 11557 tree result = NULL_TREE;
faae18ab 11558 int extern_p = 0;
e8abc66f 11559
ad47b891 11560 if (!decl || decl == error_mark_node)
dc957d14 11561 /* An error occurred, for which grokdeclarator has already issued
3fa56191
MM
11562 an appropriate message. */
11563 return;
11564 else if (! DECL_LANG_SPECIFIC (decl))
ec255269 11565 {
0f51ccfc 11566 error ("explicit instantiation of non-template %q#D", decl);
ec255269
MS
11567 return;
11568 }
03d0f4af 11569 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 11570 {
03d0f4af
MM
11571 /* There is an asymmetry here in the way VAR_DECLs and
11572 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11573 the latter, the DECL we get back will be marked as a
11574 template instantiation, and the appropriate
11575 DECL_TEMPLATE_INFO will be set up. This does not happen for
11576 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11577 should handle VAR_DECLs as it currently handles
11578 FUNCTION_DECLs. */
86ac0575 11579 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
e0860732 11580 if (!result || TREE_CODE (result) != VAR_DECL)
03d0f4af 11581 {
0f51ccfc 11582 error ("no matching template for %qD found", decl);
03d0f4af
MM
11583 return;
11584 }
6633d636
MS
11585 }
11586 else if (TREE_CODE (decl) != FUNCTION_DECL)
11587 {
0f51ccfc 11588 error ("explicit instantiation of %q#D", decl);
6633d636
MS
11589 return;
11590 }
03d0f4af
MM
11591 else
11592 result = decl;
672476cb 11593
03d0f4af 11594 /* Check for various error cases. Note that if the explicit
0e339752 11595 instantiation is valid the RESULT will currently be marked as an
03d0f4af
MM
11596 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11597 until we get here. */
11598
11599 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 11600 {
07782718
KL
11601 /* DR 259 [temp.spec].
11602
11603 Both an explicit instantiation and a declaration of an explicit
11604 specialization shall not appear in a program unless the explicit
11605 instantiation follows a declaration of the explicit specialization.
03d0f4af 11606
07782718
KL
11607 For a given set of template parameters, if an explicit
11608 instantiation of a template appears after a declaration of an
11609 explicit specialization for that template, the explicit
11610 instantiation has no effect. */
672476cb
MM
11611 return;
11612 }
03d0f4af
MM
11613 else if (DECL_EXPLICIT_INSTANTIATION (result))
11614 {
11615 /* [temp.spec]
98c1c668 11616
03d0f4af 11617 No program shall explicitly instantiate any template more
c8094d83 11618 than once.
03d0f4af 11619
4684cd27
MM
11620 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11621 the first instantiation was `extern' and the second is not,
11622 and EXTERN_P for the opposite case. */
11623 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
0f51ccfc 11624 pedwarn ("duplicate explicit instantiation of %q#D", result);
4684cd27
MM
11625 /* If an "extern" explicit instantiation follows an ordinary
11626 explicit instantiation, the template is instantiated. */
11627 if (extern_p)
03d0f4af
MM
11628 return;
11629 }
11630 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 11631 {
0f51ccfc 11632 error ("no matching template for %qD found", result);
faae18ab
MS
11633 return;
11634 }
03d0f4af 11635 else if (!DECL_TEMPLATE_INFO (result))
6633d636 11636 {
0f51ccfc 11637 pedwarn ("explicit instantiation of non-template %q#D", result);
6633d636
MS
11638 return;
11639 }
11640
f0e01782 11641 if (storage == NULL_TREE)
00595019 11642 ;
faae18ab 11643 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af 11644 {
c02f5e29 11645 if (pedantic && !in_system_header)
0f51ccfc 11646 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
0cbd7506 11647 "instantiations");
03d0f4af
MM
11648 extern_p = 1;
11649 }
f0e01782 11650 else
0f51ccfc 11651 error ("storage class %qD applied to template instantiation", storage);
3db45ab5 11652
9c12301f 11653 check_explicit_instantiation_namespace (result);
5566b478 11654 mark_decl_instantiated (result, extern_p);
c91a56d2 11655 if (! extern_p)
3db45ab5 11656 instantiate_decl (result, /*defer_ok=*/1,
eba839f9 11657 /*expl_inst_class_mem_p=*/false);
7177d104
MS
11658}
11659
b5791fdc 11660static void
3a978d72 11661mark_class_instantiated (tree t, int extern_p)
faae18ab
MS
11662{
11663 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
11664 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11665 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
11666 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11667 if (! extern_p)
11668 {
11669 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11670 rest_of_type_compilation (t, 1);
11671 }
c8094d83 11672}
e8abc66f 11673
5e0c54e5 11674/* Called from do_type_instantiation through binding_table_foreach to
9bcb9aae 11675 do recursive instantiation for the type bound in ENTRY. */
5e0c54e5
GDR
11676static void
11677bt_instantiate_type_proc (binding_entry entry, void *data)
11678{
11679 tree storage = *(tree *) data;
11680
11681 if (IS_AGGR_TYPE (entry->type)
11682 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11683 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11684}
11685
415c974c
MA
11686/* Called from do_type_instantiation to instantiate a member
11687 (a member function or a static member variable) of an
03fd3f84 11688 explicitly instantiated class template. */
415c974c
MA
11689static void
11690instantiate_class_member (tree decl, int extern_p)
11691{
11692 mark_decl_instantiated (decl, extern_p);
415c974c 11693 if (! extern_p)
3db45ab5 11694 instantiate_decl (decl, /*defer_ok=*/1,
eba839f9 11695 /*expl_inst_class_mem_p=*/true);
415c974c
MA
11696}
11697
a1bcc528
JM
11698/* Perform an explicit instantiation of template class T. STORAGE, if
11699 non-null, is the RID for extern, inline or static. COMPLAIN is
838dfd8a 11700 nonzero if this is called from the parser, zero if called recursively,
a1bcc528 11701 since the standard is unclear (as detailed below). */
c8094d83 11702
7177d104 11703void
3a978d72 11704do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
7177d104 11705{
e8abc66f
MS
11706 int extern_p = 0;
11707 int nomem_p = 0;
5566b478 11708 int static_p = 0;
4746cf84 11709 int previous_instantiation_extern_p = 0;
5566b478 11710
ca79f85d
JM
11711 if (TREE_CODE (t) == TYPE_DECL)
11712 t = TREE_TYPE (t);
11713
7ddedda4 11714 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 11715 {
0f51ccfc 11716 error ("explicit instantiation of non-template type %qT", t);
ca79f85d
JM
11717 return;
11718 }
11719
5566b478 11720 complete_type (t);
7177d104 11721
d0f062fb 11722 if (!COMPLETE_TYPE_P (t))
f0e01782 11723 {
c2ea3a40 11724 if (complain & tf_error)
0f51ccfc 11725 error ("explicit instantiation of %q#T before definition of template",
0cbd7506 11726 t);
f0e01782
MS
11727 return;
11728 }
11729
03d0f4af 11730 if (storage != NULL_TREE)
f0e01782 11731 {
c02f5e29 11732 if (pedantic && !in_system_header)
c8094d83 11733 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
0cbd7506 11734 storage);
03d0f4af
MM
11735
11736 if (storage == ridpointers[(int) RID_INLINE])
11737 nomem_p = 1;
11738 else if (storage == ridpointers[(int) RID_EXTERN])
11739 extern_p = 1;
11740 else if (storage == ridpointers[(int) RID_STATIC])
11741 static_p = 1;
11742 else
11743 {
0f51ccfc 11744 error ("storage class %qD applied to template instantiation",
0cbd7506 11745 storage);
03d0f4af
MM
11746 extern_p = 0;
11747 }
f0e01782
MS
11748 }
11749
370af2d5 11750 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
03d0f4af 11751 {
07782718 11752 /* DR 259 [temp.spec].
a292b002 11753
07782718
KL
11754 Both an explicit instantiation and a declaration of an explicit
11755 specialization shall not appear in a program unless the explicit
11756 instantiation follows a declaration of the explicit specialization.
11757
11758 For a given set of template parameters, if an explicit
11759 instantiation of a template appears after a declaration of an
11760 explicit specialization for that template, the explicit
11761 instantiation has no effect. */
03d0f4af
MM
11762 return;
11763 }
11764 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 11765 {
03d0f4af
MM
11766 /* [temp.spec]
11767
11768 No program shall explicitly instantiate any template more
c8094d83 11769 than once.
03d0f4af 11770
0cbd7506 11771 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
4746cf84 11772 instantiation was `extern'. If EXTERN_P then the second is.
4684cd27 11773 These cases are OK. */
4746cf84
MA
11774 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11775
11776 if (!previous_instantiation_extern_p && !extern_p
c2ea3a40 11777 && (complain & tf_error))
0f51ccfc 11778 pedwarn ("duplicate explicit instantiation of %q#T", t);
c8094d83 11779
03d0f4af
MM
11780 /* If we've already instantiated the template, just return now. */
11781 if (!CLASSTYPE_INTERFACE_ONLY (t))
11782 return;
44a8d0b3 11783 }
e8abc66f 11784
9c12301f 11785 check_explicit_instantiation_namespace (TYPE_NAME (t));
03d0f4af 11786 mark_class_instantiated (t, extern_p);
03d0f4af 11787
e8abc66f
MS
11788 if (nomem_p)
11789 return;
11790
7177d104 11791 {
db5ae43f 11792 tree tmp;
5566b478 11793
03d0f4af
MM
11794 /* In contrast to implicit instantiation, where only the
11795 declarations, and not the definitions, of members are
11796 instantiated, we have here:
11797
0cbd7506 11798 [temp.explicit]
03d0f4af
MM
11799
11800 The explicit instantiation of a class template specialization
11801 implies the instantiation of all of its members not
11802 previously explicitly specialized in the translation unit
c8094d83 11803 containing the explicit instantiation.
03d0f4af
MM
11804
11805 Of course, we can't instantiate member template classes, since
11806 we don't have any arguments for them. Note that the standard
dc957d14 11807 is unclear on whether the instantiation of the members are
415c974c 11808 *explicit* instantiations or not. However, the most natural
03fd3f84 11809 interpretation is that it should be an explicit instantiation. */
03d0f4af 11810
5566b478
MS
11811 if (! static_p)
11812 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 11813 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 11814 && DECL_TEMPLATE_INSTANTIATION (tmp))
415c974c 11815 instantiate_class_member (tmp, extern_p);
5566b478
MS
11816
11817 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11818 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
415c974c 11819 instantiate_class_member (tmp, extern_p);
7177d104 11820
5e0c54e5
GDR
11821 if (CLASSTYPE_NESTED_UTDS (t))
11822 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
0cbd7506 11823 bt_instantiate_type_proc, &storage);
a292b002 11824 }
8d08fdba 11825}
a28e3c7f 11826
36a117a5
MM
11827/* Given a function DECL, which is a specialization of TMPL, modify
11828 DECL to be a re-instantiation of TMPL with the same template
11829 arguments. TMPL should be the template into which tsubst'ing
11830 should occur for DECL, not the most general template.
830bfa74
MM
11831
11832 One reason for doing this is a scenario like this:
11833
11834 template <class T>
11835 void f(const T&, int i);
11836
11837 void g() { f(3, 7); }
11838
11839 template <class T>
11840 void f(const T& t, const int i) { }
11841
11842 Note that when the template is first instantiated, with
11843 instantiate_template, the resulting DECL will have no name for the
11844 first parameter, and the wrong type for the second. So, when we go
11845 to instantiate the DECL, we regenerate it. */
11846
e9659ab0 11847static void
3a978d72 11848regenerate_decl_from_template (tree decl, tree tmpl)
830bfa74 11849{
f9a7ae04
MM
11850 /* The arguments used to instantiate DECL, from the most general
11851 template. */
830bfa74 11852 tree args;
830bfa74 11853 tree code_pattern;
830bfa74
MM
11854
11855 args = DECL_TI_ARGS (decl);
11856 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11857
2b907f5c
KL
11858 /* Make sure that we can see identifiers, and compute access
11859 correctly. */
11860 push_access_scope (decl);
11861
c7222c02
MM
11862 if (TREE_CODE (decl) == FUNCTION_DECL)
11863 {
11864 tree decl_parm;
11865 tree pattern_parm;
11866 tree specs;
11867 int args_depth;
11868 int parms_depth;
c8094d83 11869
c7222c02 11870 args_depth = TMPL_ARGS_DEPTH (args);
c8094d83 11871 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
c7222c02
MM
11872 if (args_depth > parms_depth)
11873 args = get_innermost_template_args (args, parms_depth);
11874
11875 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11876 args, tf_error, NULL_TREE);
11877 if (specs)
11878 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11879 specs);
11880
11881 /* Merge parameter declarations. */
c8094d83 11882 decl_parm = skip_artificial_parms_for (decl,
c7222c02 11883 DECL_ARGUMENTS (decl));
c8094d83 11884 pattern_parm
c7222c02
MM
11885 = skip_artificial_parms_for (code_pattern,
11886 DECL_ARGUMENTS (code_pattern));
11887 while (decl_parm)
11888 {
11889 tree parm_type;
b17bba6d 11890 tree attributes;
c7222c02
MM
11891
11892 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11893 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11894 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11895 NULL_TREE);
02bab9db 11896 parm_type = type_decays_to (parm_type);
c7222c02
MM
11897 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11898 TREE_TYPE (decl_parm) = parm_type;
b17bba6d
MM
11899 attributes = DECL_ATTRIBUTES (pattern_parm);
11900 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11901 {
11902 DECL_ATTRIBUTES (decl_parm) = attributes;
11903 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11904 }
c7222c02
MM
11905 decl_parm = TREE_CHAIN (decl_parm);
11906 pattern_parm = TREE_CHAIN (pattern_parm);
11907 }
830bfa74 11908
c7222c02
MM
11909 /* Merge additional specifiers from the CODE_PATTERN. */
11910 if (DECL_DECLARED_INLINE_P (code_pattern)
11911 && !DECL_DECLARED_INLINE_P (decl))
11912 DECL_DECLARED_INLINE_P (decl) = 1;
11913 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11914 DECL_INLINE (decl) = 1;
11915 }
11916 else if (TREE_CODE (decl) == VAR_DECL)
b794e321
MM
11917 DECL_INITIAL (decl) =
11918 tsubst_expr (DECL_INITIAL (code_pattern), args,
015c2c66
MM
11919 tf_error, DECL_TI_TEMPLATE (decl),
11920 /*integral_constant_expression_p=*/false);
c7222c02
MM
11921 else
11922 gcc_unreachable ();
36a117a5 11923
2b59fc25 11924 pop_access_scope (decl);
830bfa74
MM
11925}
11926
a723baf1
MM
11927/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11928 substituted to get DECL. */
11929
d58b7c2d 11930tree
a723baf1
MM
11931template_for_substitution (tree decl)
11932{
11933 tree tmpl = DECL_TI_TEMPLATE (decl);
11934
11935 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11936 for the instantiation. This is not always the most general
11937 template. Consider, for example:
11938
0cbd7506 11939 template <class T>
a723baf1 11940 struct S { template <class U> void f();
0cbd7506 11941 template <> void f<int>(); };
a723baf1
MM
11942
11943 and an instantiation of S<double>::f<int>. We want TD to be the
11944 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11945 while (/* An instantiation cannot have a definition, so we need a
11946 more general template. */
11947 DECL_TEMPLATE_INSTANTIATION (tmpl)
11948 /* We must also deal with friend templates. Given:
11949
c8094d83 11950 template <class T> struct S {
a723baf1
MM
11951 template <class U> friend void f() {};
11952 };
11953
11954 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11955 so far as the language is concerned, but that's still
11956 where we get the pattern for the instantiation from. On
11957 other hand, if the definition comes outside the class, say:
11958
c8094d83 11959 template <class T> struct S {
a723baf1
MM
11960 template <class U> friend void f();
11961 };
11962 template <class U> friend void f() {}
11963
11964 we don't need to look any further. That's what the check for
11965 DECL_INITIAL is for. */
11966 || (TREE_CODE (decl) == FUNCTION_DECL
11967 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11968 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11969 {
11970 /* The present template, TD, should not be a definition. If it
11971 were a definition, we should be using it! Note that we
11972 cannot restructure the loop to just keep going until we find
11973 a template with a definition, since that might go too far if
11974 a specialization was declared, but not defined. */
50bc768d
NS
11975 gcc_assert (TREE_CODE (decl) != VAR_DECL
11976 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
c8094d83 11977
a723baf1
MM
11978 /* Fetch the more general template. */
11979 tmpl = DECL_TI_TEMPLATE (tmpl);
11980 }
11981
11982 return tmpl;
11983}
11984
16d53b64 11985/* Produce the definition of D, a _DECL generated from a template. If
838dfd8a 11986 DEFER_OK is nonzero, then we don't have to actually do the
415c974c
MA
11987 instantiation now; we just have to do it sometime. Normally it is
11988 an error if this is an explicit instantiation but D is undefined.
eba839f9
MM
11989 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
11990 explicitly instantiated class template. */
f84b4be9 11991
a28e3c7f 11992tree
3db45ab5 11993instantiate_decl (tree d, int defer_ok,
eba839f9 11994 bool expl_inst_class_mem_p)
a28e3c7f 11995{
36a117a5 11996 tree tmpl = DECL_TI_TEMPLATE (d);
65f8b0fb
MM
11997 tree gen_args;
11998 tree args;
830bfa74 11999 tree td;
36a117a5
MM
12000 tree code_pattern;
12001 tree spec;
12002 tree gen_tmpl;
b794e321 12003 bool pattern_defined;
31a714f6 12004 int need_push;
82a98427 12005 location_t saved_loc = input_location;
f7e4e484 12006 bool external_p;
c8094d83 12007
36a117a5
MM
12008 /* This function should only be used to instantiate templates for
12009 functions and static member variables. */
50bc768d
NS
12010 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
12011 || TREE_CODE (d) == VAR_DECL);
36a117a5 12012
cec24319
MM
12013 /* Variables are never deferred; if instantiation is required, they
12014 are instantiated right away. That allows for better code in the
12015 case that an expression refers to the value of the variable --
12016 if the variable has a constant value the referring expression can
12017 take advantage of that fact. */
12018 if (TREE_CODE (d) == VAR_DECL)
12019 defer_ok = 0;
12020
db9b2174
MM
12021 /* Don't instantiate cloned functions. Instead, instantiate the
12022 functions they cloned. */
12023 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
12024 d = DECL_CLONED_FUNCTION (d);
12025
fbf1c34b 12026 if (DECL_TEMPLATE_INSTANTIATED (d))
03d0f4af 12027 /* D has already been instantiated. It might seem reasonable to
dc957d14 12028 check whether or not D is an explicit instantiation, and, if so,
03d0f4af
MM
12029 stop here. But when an explicit instantiation is deferred
12030 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
12031 is set, even though we still need to do the instantiation. */
36a117a5
MM
12032 return d;
12033
12034 /* If we already have a specialization of this declaration, then
12035 there's no reason to instantiate it. Note that
12036 retrieve_specialization gives us both instantiations and
12037 specializations, so we must explicitly check
12038 DECL_TEMPLATE_SPECIALIZATION. */
12039 gen_tmpl = most_general_template (tmpl);
65f8b0fb 12040 gen_args = DECL_TI_ARGS (d);
c7222c02
MM
12041 spec = retrieve_specialization (gen_tmpl, gen_args,
12042 /*class_specializations_p=*/false);
36a117a5
MM
12043 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
12044 return spec;
12045
12046 /* This needs to happen before any tsubsting. */
12047 if (! push_tinst_level (d))
12048 return d;
12049
297a5329
JM
12050 timevar_push (TV_PARSE);
12051
4d85e00e 12052 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
a723baf1
MM
12053 for the instantiation. */
12054 td = template_for_substitution (d);
fee23f54 12055 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 12056
2d22db1f 12057 /* We should never be trying to instantiate a member of a class
3db45ab5 12058 template or partial specialization. */
2d22db1f 12059 gcc_assert (d != code_pattern);
3db45ab5 12060
76d3baad
KL
12061 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
12062 || DECL_TEMPLATE_SPECIALIZATION (td))
649fc72d
NS
12063 /* In the case of a friend template whose definition is provided
12064 outside the class, we may have too many arguments. Drop the
76d3baad 12065 ones we don't need. The same is true for specializations. */
649fc72d
NS
12066 args = get_innermost_template_args
12067 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
12068 else
12069 args = gen_args;
65f8b0fb 12070
5566b478 12071 if (TREE_CODE (d) == FUNCTION_DECL)
39c76b4f 12072 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
5566b478 12073 else
36a117a5 12074 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
b794e321
MM
12075
12076 /* We may be in the middle of deferred access check. Disable it now. */
12077 push_deferring_access_checks (dk_no_deferred);
12078
4684cd27
MM
12079 /* Unless an explicit instantiation directive has already determined
12080 the linkage of D, remember that a definition is available for
12081 this entity. */
c8094d83 12082 if (pattern_defined
4684cd27
MM
12083 && !DECL_INTERFACE_KNOWN (d)
12084 && !DECL_NOT_REALLY_EXTERN (d))
12085 mark_definable (d);
de22184b 12086
f31686a3 12087 input_location = DECL_SOURCE_LOCATION (d);
de22184b 12088
eba839f9
MM
12089 /* If D is a member of an explicitly instantiated class template,
12090 and no definition is available, treat it like an implicit
3db45ab5
MS
12091 instantiation. */
12092 if (!pattern_defined && expl_inst_class_mem_p
12093 && DECL_EXPLICIT_INSTANTIATION (d))
5566b478 12094 {
4684cd27 12095 DECL_NOT_REALLY_EXTERN (d) = 0;
eba839f9 12096 DECL_INTERFACE_KNOWN (d) = 0;
4684cd27 12097 SET_DECL_IMPLICIT_INSTANTIATION (d);
5566b478
MS
12098 }
12099
4f2b0fb2
NS
12100 if (!defer_ok)
12101 {
12102 /* Recheck the substitutions to obtain any warning messages
12103 about ignoring cv qualifiers. */
12104 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
12105 tree type = TREE_TYPE (gen);
12106
0e902d98 12107 /* Make sure that we can see identifiers, and compute access
2b59fc25
KL
12108 correctly. D is already the target FUNCTION_DECL with the
12109 right context. */
12110 push_access_scope (d);
0e902d98 12111
4f2b0fb2
NS
12112 if (TREE_CODE (gen) == FUNCTION_DECL)
12113 {
23fca1f5 12114 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
65f8b0fb 12115 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
23fca1f5 12116 tf_warning_or_error, d);
4f2b0fb2
NS
12117 /* Don't simply tsubst the function type, as that will give
12118 duplicate warnings about poor parameter qualifications.
12119 The function arguments are the same as the decl_arguments
c6002625 12120 without the top level cv qualifiers. */
4f2b0fb2
NS
12121 type = TREE_TYPE (type);
12122 }
23fca1f5 12123 tsubst (type, gen_args, tf_warning_or_error, d);
0e902d98 12124
2b59fc25 12125 pop_access_scope (d);
4f2b0fb2 12126 }
c8094d83 12127
f7e4e484
MM
12128 /* Check to see whether we know that this template will be
12129 instantiated in some other file, as with "extern template"
12130 extension. */
12131 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
12132 /* In general, we do not instantiate such templates... */
12133 if (external_p
12134 /* ... but we instantiate inline functions so that we can inline
12135 them and ... */
12136 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
12137 /* ... we instantiate static data members whose values are
12138 needed in integral constant expressions. */
3db45ab5 12139 && ! (TREE_CODE (d) == VAR_DECL
f7e4e484 12140 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
930cd796 12141 goto out;
16d53b64 12142 /* Defer all other templates, unless we have been explicitly
f7e4e484
MM
12143 forbidden from doing so. */
12144 if (/* If there is no definition, we cannot instantiate the
12145 template. */
3db45ab5 12146 ! pattern_defined
f7e4e484
MM
12147 /* If it's OK to postpone instantiation, do so. */
12148 || defer_ok
12149 /* If this is a static data member that will be defined
12150 elsewhere, we don't want to instantiate the entire data
12151 member, but we do want to instantiate the initializer so that
12152 we can substitute that elsewhere. */
12153 || (external_p && TREE_CODE (d) == VAR_DECL))
16d53b64 12154 {
b794e321
MM
12155 /* The definition of the static data member is now required so
12156 we must substitute the initializer. */
12157 if (TREE_CODE (d) == VAR_DECL
3db45ab5 12158 && !DECL_INITIAL (d)
b794e321
MM
12159 && DECL_INITIAL (code_pattern))
12160 {
4bff36d3
MM
12161 tree ns;
12162 tree init;
12163
12164 ns = decl_namespace_context (d);
27a725e2 12165 push_nested_namespace (ns);
b794e321 12166 push_nested_class (DECL_CONTEXT (d));
3db45ab5 12167 init = tsubst_expr (DECL_INITIAL (code_pattern),
4bff36d3 12168 args,
015c2c66
MM
12169 tf_warning_or_error, NULL_TREE,
12170 /*integral_constant_expression_p=*/false);
d174af6c
MM
12171 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
12172 /*asmspec_tree=*/NULL_TREE,
86414779 12173 LOOKUP_ONLYCONVERTING);
b794e321 12174 pop_nested_class ();
27a725e2 12175 pop_nested_namespace (ns);
b794e321
MM
12176 }
12177
f7e4e484
MM
12178 /* We restore the source position here because it's used by
12179 add_pending_template. */
82a98427 12180 input_location = saved_loc;
c27be9b9 12181
c8094d83 12182 if (at_eof && !pattern_defined
03d0f4af
MM
12183 && DECL_EXPLICIT_INSTANTIATION (d))
12184 /* [temp.explicit]
12185
12186 The definition of a non-exported function template, a
12187 non-exported member function template, or a non-exported
12188 member function or static data member of a class template
12189 shall be present in every translation unit in which it is
12190 explicitly instantiated. */
33bd39a2 12191 pedwarn
0f51ccfc 12192 ("explicit instantiation of %qD but no definition available", d);
03d0f4af 12193
f7e4e484
MM
12194 /* ??? Historically, we have instantiated inline functions, even
12195 when marked as "extern template". */
12196 if (!(external_p && TREE_CODE (d) == VAR_DECL))
12197 add_pending_template (d);
de22184b 12198 goto out;
5566b478 12199 }
4684cd27
MM
12200 /* Tell the repository that D is available in this translation unit
12201 -- and see if it is supposed to be instantiated here. */
12202 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
12203 {
12204 /* In a PCH file, despite the fact that the repository hasn't
12205 requested instantiation in the PCH it is still possible that
12206 an instantiation will be required in a file that includes the
12207 PCH. */
12208 if (pch_file)
12209 add_pending_template (d);
12210 /* Instantiate inline functions so that the inliner can do its
12211 job, even though we'll not be emitting a copy of this
12212 function. */
c2a124b2 12213 if (!(TREE_CODE (d) == FUNCTION_DECL
c8094d83 12214 && flag_inline_trees
c2a124b2 12215 && DECL_DECLARED_INLINE_P (d)))
4684cd27
MM
12216 goto out;
12217 }
5566b478 12218
6de9cd9a 12219 need_push = !cfun || !global_bindings_p ();
31a714f6
MM
12220 if (need_push)
12221 push_to_top_level ();
414ea4aa 12222
66e0c440
KL
12223 /* Mark D as instantiated so that recursive calls to
12224 instantiate_decl do not try to instantiate it again. */
12225 DECL_TEMPLATE_INSTANTIATED (d) = 1;
12226
2b0a63a3
MM
12227 /* Regenerate the declaration in case the template has been modified
12228 by a subsequent redeclaration. */
12229 regenerate_decl_from_template (d, td);
4684cd27 12230
120722ac 12231 /* We already set the file and line above. Reset them now in case
6de9cd9a 12232 they changed as a result of calling regenerate_decl_from_template. */
f31686a3 12233 input_location = DECL_SOURCE_LOCATION (d);
5156628f 12234
5566b478
MS
12235 if (TREE_CODE (d) == VAR_DECL)
12236 {
e92fb501
MM
12237 tree init;
12238
1d62c33e
MM
12239 /* Clear out DECL_RTL; whatever was there before may not be right
12240 since we've reset the type of the declaration. */
12241 SET_DECL_RTL (d, NULL_RTX);
5566b478 12242 DECL_IN_AGGR_P (d) = 0;
ea56c40c 12243
e92fb501
MM
12244 /* The initializer is placed in DECL_INITIAL by
12245 regenerate_decl_from_template. Pull it out so that
12246 finish_decl can process it. */
12247 init = DECL_INITIAL (d);
12248 DECL_INITIAL (d) = NULL_TREE;
12249 DECL_INITIALIZED_P (d) = 0;
12250
4684cd27
MM
12251 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12252 initializer. That function will defer actual emission until
12253 we have a chance to determine linkage. */
12254 DECL_EXTERNAL (d) = 0;
12255
73a8adb6 12256 /* Enter the scope of D so that access-checking works correctly. */
4684cd27 12257 push_nested_class (DECL_CONTEXT (d));
e92fb501 12258 finish_decl (d, init, NULL_TREE);
73a8adb6 12259 pop_nested_class ();
5566b478
MS
12260 }
12261 else if (TREE_CODE (d) == FUNCTION_DECL)
12262 {
6bbf1598 12263 htab_t saved_local_specializations;
a723baf1
MM
12264 tree subst_decl;
12265 tree tmpl_parm;
12266 tree spec_parm;
6bbf1598
MM
12267
12268 /* Save away the current list, in case we are instantiating one
12269 template from within the body of another. */
12270 saved_local_specializations = local_specializations;
12271
6dfbb909 12272 /* Set up the list of local specializations. */
c8094d83 12273 local_specializations = htab_create (37,
69f794a7 12274 hash_local_specialization,
a723baf1 12275 eq_local_specializations,
6dfbb909
MM
12276 NULL);
12277
558475f0 12278 /* Set up context. */
058b15c1 12279 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
5566b478 12280
a723baf1
MM
12281 /* Create substitution entries for the parameters. */
12282 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
12283 tmpl_parm = DECL_ARGUMENTS (subst_decl);
12284 spec_parm = DECL_ARGUMENTS (d);
12285 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
12286 {
12287 register_local_specialization (spec_parm, tmpl_parm);
12288 spec_parm = skip_artificial_parms_for (d, spec_parm);
12289 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
12290 }
12291 while (tmpl_parm)
12292 {
12293 register_local_specialization (spec_parm, tmpl_parm);
12294 tmpl_parm = TREE_CHAIN (tmpl_parm);
12295 spec_parm = TREE_CHAIN (spec_parm);
12296 }
50bc768d 12297 gcc_assert (!spec_parm);
a723baf1 12298
558475f0
MM
12299 /* Substitute into the body of the function. */
12300 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
015c2c66
MM
12301 tf_warning_or_error, tmpl,
12302 /*integral_constant_expression_p=*/false);
558475f0 12303
6dfbb909
MM
12304 /* We don't need the local specializations any more. */
12305 htab_delete (local_specializations);
6bbf1598 12306 local_specializations = saved_local_specializations;
6dfbb909 12307
4d6abc1c 12308 /* Finish the function. */
b2dd096b 12309 d = finish_function (0);
8cd2462c 12310 expand_or_defer_fn (d);
5566b478
MS
12311 }
12312
971cbc14
MM
12313 /* We're not deferring instantiation any more. */
12314 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
12315
31a714f6
MM
12316 if (need_push)
12317 pop_from_top_level ();
414ea4aa 12318
de22184b 12319out:
82a98427 12320 input_location = saved_loc;
7d021397 12321 pop_deferring_access_checks ();
5566b478 12322 pop_tinst_level ();
a28e3c7f 12323
297a5329
JM
12324 timevar_pop (TV_PARSE);
12325
a28e3c7f
MS
12326 return d;
12327}
5566b478 12328
0aafb128 12329/* Run through the list of templates that we wish we could
35046a54
KL
12330 instantiate, and instantiate any we can. RETRIES is the
12331 number of times we retry pending template instantiation. */
0aafb128 12332
35046a54
KL
12333void
12334instantiate_pending_templates (int retries)
0aafb128
MM
12335{
12336 tree *t;
46ccf50a 12337 tree last = NULL_TREE;
0aafb128 12338 int reconsider;
aad626f7 12339 location_t saved_loc = input_location;
12af7ba3 12340 int saved_in_system_header = in_system_header;
35046a54
KL
12341
12342 /* Instantiating templates may trigger vtable generation. This in turn
12343 may require further template instantiations. We place a limit here
12344 to avoid infinite loop. */
12345 if (pending_templates && retries >= max_tinst_depth)
12346 {
96c993a8
JM
12347 tree decl = TREE_VALUE (pending_templates);
12348
dee15844 12349 error ("template instantiation depth exceeds maximum of %d"
96c993a8
JM
12350 " instantiating %q+D, possibly from virtual table generation"
12351 " (use -ftemplate-depth-NN to increase the maximum)",
12352 max_tinst_depth, decl);
12353 if (TREE_CODE (decl) == FUNCTION_DECL)
12354 /* Pretend that we defined it. */
12355 DECL_INITIAL (decl) = error_mark_node;
35046a54
KL
12356 return;
12357 }
12358
c8094d83 12359 do
0aafb128
MM
12360 {
12361 reconsider = 0;
12362
12363 t = &pending_templates;
12364 while (*t)
12365 {
0aafb128
MM
12366 tree instantiation = TREE_VALUE (*t);
12367
3ae18eaf 12368 reopen_tinst_level (TREE_PURPOSE (*t));
0aafb128 12369
2f939d94 12370 if (TYPE_P (instantiation))
0aafb128
MM
12371 {
12372 tree fn;
12373
d0f062fb 12374 if (!COMPLETE_TYPE_P (instantiation))
0aafb128
MM
12375 {
12376 instantiate_class_template (instantiation);
12377 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
c8094d83 12378 for (fn = TYPE_METHODS (instantiation);
0aafb128
MM
12379 fn;
12380 fn = TREE_CHAIN (fn))
12381 if (! DECL_ARTIFICIAL (fn))
3db45ab5 12382 instantiate_decl (fn,
eba839f9
MM
12383 /*defer_ok=*/0,
12384 /*expl_inst_class_mem_p=*/false);
d0f062fb 12385 if (COMPLETE_TYPE_P (instantiation))
35046a54 12386 reconsider = 1;
0aafb128
MM
12387 }
12388
d0f062fb 12389 if (COMPLETE_TYPE_P (instantiation))
0aafb128
MM
12390 /* If INSTANTIATION has been instantiated, then we don't
12391 need to consider it again in the future. */
12392 *t = TREE_CHAIN (*t);
46ccf50a
JM
12393 else
12394 {
12395 last = *t;
12396 t = &TREE_CHAIN (*t);
12397 }
0aafb128
MM
12398 }
12399 else
12400 {
16d53b64 12401 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
12402 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12403 {
3db45ab5 12404 instantiation
eba839f9
MM
12405 = instantiate_decl (instantiation,
12406 /*defer_ok=*/0,
12407 /*expl_inst_class_mem_p=*/false);
0aafb128 12408 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
35046a54 12409 reconsider = 1;
0aafb128
MM
12410 }
12411
16d53b64 12412 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
12413 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12414 /* If INSTANTIATION has been instantiated, then we don't
12415 need to consider it again in the future. */
12416 *t = TREE_CHAIN (*t);
46ccf50a
JM
12417 else
12418 {
12419 last = *t;
12420 t = &TREE_CHAIN (*t);
12421 }
0aafb128 12422 }
84e5ca0f
NS
12423 tinst_depth = 0;
12424 current_tinst_level = NULL_TREE;
0aafb128 12425 }
46ccf50a 12426 last_pending_template = last;
c8094d83 12427 }
0aafb128
MM
12428 while (reconsider);
12429
aad626f7 12430 input_location = saved_loc;
12af7ba3 12431 in_system_header = saved_in_system_header;
0aafb128
MM
12432}
12433
fd74ca0b
MM
12434/* Substitute ARGVEC into T, which is a list of initializers for
12435 either base class or a non-static data member. The TREE_PURPOSEs
12436 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12437 instantiate_decl. */
4393e105 12438
824b9a4c 12439static tree
3a978d72 12440tsubst_initializer_list (tree t, tree argvec)
5566b478 12441{
2282d28d 12442 tree inits = NULL_TREE;
5566b478
MS
12443
12444 for (; t; t = TREE_CHAIN (t))
12445 {
fd74ca0b
MM
12446 tree decl;
12447 tree init;
fd74ca0b 12448
23fca1f5 12449 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
fd74ca0b 12450 NULL_TREE);
1f5a253a
NS
12451 decl = expand_member_init (decl);
12452 if (decl && !DECL_P (decl))
12453 in_base_initializer = 1;
c8094d83 12454
23fca1f5 12455 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
015c2c66
MM
12456 NULL_TREE,
12457 /*integral_constant_expression_p=*/false);
1f5a253a
NS
12458 in_base_initializer = 0;
12459
12460 if (decl)
2282d28d 12461 {
1f5a253a 12462 init = build_tree_list (decl, init);
2282d28d
MM
12463 TREE_CHAIN (init) = inits;
12464 inits = init;
12465 }
5566b478 12466 }
2282d28d 12467 return inits;
5566b478
MS
12468}
12469
61a127b3
MM
12470/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12471
12472static void
3a978d72 12473set_current_access_from_decl (tree decl)
61a127b3
MM
12474{
12475 if (TREE_PRIVATE (decl))
12476 current_access_specifier = access_private_node;
12477 else if (TREE_PROTECTED (decl))
12478 current_access_specifier = access_protected_node;
12479 else
12480 current_access_specifier = access_public_node;
12481}
12482
dbfe2124
MM
12483/* Instantiate an enumerated type. TAG is the template type, NEWTAG
12484 is the instantiation (which should have been created with
12485 start_enum) and ARGS are the template arguments to use. */
b87692e5 12486
dbfe2124 12487static void
3a978d72 12488tsubst_enum (tree tag, tree newtag, tree args)
b87692e5 12489{
dbfe2124 12490 tree e;
b87692e5
MS
12491
12492 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12493 {
61a127b3 12494 tree value;
7b6d72fc
MM
12495 tree decl;
12496
12497 decl = TREE_VALUE (e);
61a127b3
MM
12498 /* Note that in a template enum, the TREE_VALUE is the
12499 CONST_DECL, not the corresponding INTEGER_CST. */
c8094d83 12500 value = tsubst_expr (DECL_INITIAL (decl),
015c2c66
MM
12501 args, tf_warning_or_error, NULL_TREE,
12502 /*integral_constant_expression_p=*/true);
61a127b3
MM
12503
12504 /* Give this enumeration constant the correct access. */
7b6d72fc 12505 set_current_access_from_decl (decl);
61a127b3
MM
12506
12507 /* Actually build the enumerator itself. */
c8094d83 12508 build_enumerator (DECL_NAME (decl), value, newtag);
dbfe2124 12509 }
b3d5a58b 12510
219670f1 12511 finish_enum (newtag);
f31686a3
RH
12512 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12513 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
b87692e5 12514}
36a117a5 12515
1f6e1acc
AS
12516/* DECL is a FUNCTION_DECL that is a template specialization. Return
12517 its type -- but without substituting the innermost set of template
12518 arguments. So, innermost set of template parameters will appear in
5c74d5b0 12519 the type. */
1f6e1acc 12520
c8094d83 12521tree
3a978d72 12522get_mostly_instantiated_function_type (tree decl)
1f6e1acc 12523{
1f6e1acc
AS
12524 tree fn_type;
12525 tree tmpl;
12526 tree targs;
12527 tree tparms;
12528 int parm_depth;
12529
12530 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12531 targs = DECL_TI_ARGS (decl);
12532 tparms = DECL_TEMPLATE_PARMS (tmpl);
12533 parm_depth = TMPL_PARMS_DEPTH (tparms);
12534
12535 /* There should be as many levels of arguments as there are levels
12536 of parameters. */
50bc768d 12537 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
1f6e1acc
AS
12538
12539 fn_type = TREE_TYPE (tmpl);
1f6e1acc
AS
12540
12541 if (parm_depth == 1)
12542 /* No substitution is necessary. */
12543 ;
12544 else
12545 {
fae6e246 12546 int i, save_access_control;
1f6e1acc
AS
12547 tree partial_args;
12548
12549 /* Replace the innermost level of the TARGS with NULL_TREEs to
dc957d14 12550 let tsubst know not to substitute for those parameters. */
1f6e1acc
AS
12551 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12552 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12553 SET_TMPL_ARGS_LEVEL (partial_args, i,
12554 TMPL_ARGS_LEVEL (targs, i));
12555 SET_TMPL_ARGS_LEVEL (partial_args,
12556 TMPL_ARGS_DEPTH (targs),
12557 make_tree_vec (DECL_NTPARMS (tmpl)));
12558
fae6e246
RH
12559 /* Disable access control as this function is used only during
12560 name-mangling. */
12561 save_access_control = flag_access_control;
12562 flag_access_control = 0;
5c74d5b0 12563
9579624e 12564 ++processing_template_decl;
1f6e1acc
AS
12565 /* Now, do the (partial) substitution to figure out the
12566 appropriate function type. */
c2ea3a40 12567 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
9579624e 12568 --processing_template_decl;
1f6e1acc
AS
12569
12570 /* Substitute into the template parameters to obtain the real
12571 innermost set of parameters. This step is important if the
12572 innermost set of template parameters contains value
12573 parameters whose types depend on outer template parameters. */
12574 TREE_VEC_LENGTH (partial_args)--;
c2ea3a40 12575 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
1f6e1acc 12576
fae6e246 12577 flag_access_control = save_access_control;
5c74d5b0 12578 }
1f6e1acc
AS
12579
12580 return fn_type;
12581}
669ec2b4 12582
cb753e49 12583/* Return truthvalue if we're processing a template different from
dc957d14 12584 the last one involved in diagnostics. */
cb753e49 12585int
3a978d72 12586problematic_instantiation_changed (void)
cb753e49
GDR
12587{
12588 return last_template_error_tick != tinst_level_tick;
12589}
12590
12591/* Remember current template involved in diagnostics. */
12592void
3a978d72 12593record_last_problematic_instantiation (void)
cb753e49
GDR
12594{
12595 last_template_error_tick = tinst_level_tick;
12596}
12597
12598tree
3a978d72 12599current_instantiation (void)
cb753e49
GDR
12600{
12601 return current_tinst_level;
12602}
db3f4e4e
NS
12603
12604/* [temp.param] Check that template non-type parm TYPE is of an allowable
838dfd8a 12605 type. Return zero for ok, nonzero for disallowed. Issue error and
c2ea3a40 12606 warning messages under control of COMPLAIN. */
db3f4e4e
NS
12607
12608static int
3a978d72 12609invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
db3f4e4e
NS
12610{
12611 if (INTEGRAL_TYPE_P (type))
12612 return 0;
12613 else if (POINTER_TYPE_P (type))
12614 return 0;
a5ac359a 12615 else if (TYPE_PTR_TO_MEMBER_P (type))
db3f4e4e 12616 return 0;
db3f4e4e
NS
12617 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12618 return 0;
12619 else if (TREE_CODE (type) == TYPENAME_TYPE)
12620 return 0;
c8094d83 12621
c2ea3a40 12622 if (complain & tf_error)
0f51ccfc 12623 error ("%q#T is not a valid type for a template constant parameter", type);
db3f4e4e
NS
12624 return 1;
12625}
e2500fed 12626
5552b43c
MM
12627/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12628 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
1fb3244a 12629
5552b43c
MM
12630static bool
12631dependent_type_p_r (tree type)
1fb3244a
MM
12632{
12633 tree scope;
12634
1fb3244a
MM
12635 /* [temp.dep.type]
12636
12637 A type is dependent if it is:
12638
6615c446
JO
12639 -- a template parameter. Template template parameters are types
12640 for us (since TYPE_P holds true for them) so we handle
12641 them here. */
c8094d83 12642 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
6bf92cb6 12643 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
1fb3244a
MM
12644 return true;
12645 /* -- a qualified-id with a nested-name-specifier which contains a
0cbd7506 12646 class-name that names a dependent type or whose unqualified-id
1fb3244a
MM
12647 names a dependent type. */
12648 if (TREE_CODE (type) == TYPENAME_TYPE)
12649 return true;
12650 /* -- a cv-qualified type where the cv-unqualified type is
0cbd7506 12651 dependent. */
1fb3244a
MM
12652 type = TYPE_MAIN_VARIANT (type);
12653 /* -- a compound type constructed from any dependent type. */
a5ac359a 12654 if (TYPE_PTR_TO_MEMBER_P (type))
1fb3244a 12655 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
c8094d83 12656 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1fb3244a
MM
12657 (type)));
12658 else if (TREE_CODE (type) == POINTER_TYPE
12659 || TREE_CODE (type) == REFERENCE_TYPE)
12660 return dependent_type_p (TREE_TYPE (type));
12661 else if (TREE_CODE (type) == FUNCTION_TYPE
12662 || TREE_CODE (type) == METHOD_TYPE)
12663 {
12664 tree arg_type;
12665
12666 if (dependent_type_p (TREE_TYPE (type)))
12667 return true;
c8094d83
MS
12668 for (arg_type = TYPE_ARG_TYPES (type);
12669 arg_type;
1fb3244a
MM
12670 arg_type = TREE_CHAIN (arg_type))
12671 if (dependent_type_p (TREE_VALUE (arg_type)))
12672 return true;
12673 return false;
12674 }
12675 /* -- an array type constructed from any dependent type or whose
0cbd7506 12676 size is specified by a constant expression that is
1fb3244a
MM
12677 value-dependent. */
12678 if (TREE_CODE (type) == ARRAY_TYPE)
12679 {
12680 if (TYPE_DOMAIN (type)
c8094d83 12681 && ((value_dependent_expression_p
1fb3244a
MM
12682 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12683 || (type_dependent_expression_p
12684 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12685 return true;
12686 return dependent_type_p (TREE_TYPE (type));
12687 }
c8094d83 12688
1fb3244a 12689 /* -- a template-id in which either the template name is a template
86306a6b
NS
12690 parameter ... */
12691 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1fb3244a 12692 return true;
86306a6b 12693 /* ... or any of the template arguments is a dependent type or
04c06002 12694 an expression that is type-dependent or value-dependent. */
86306a6b 12695 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
c8094d83 12696 && (any_dependent_template_arguments_p
7e99327d 12697 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
1fb3244a 12698 return true;
c8094d83 12699
1fb3244a
MM
12700 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12701 expression is not type-dependent, then it should already been
12702 have resolved. */
12703 if (TREE_CODE (type) == TYPEOF_TYPE)
12704 return true;
c8094d83 12705
1fb3244a
MM
12706 /* The standard does not specifically mention types that are local
12707 to template functions or local classes, but they should be
12708 considered dependent too. For example:
12709
c8094d83 12710 template <int I> void f() {
0cbd7506 12711 enum E { a = I };
1fb3244a
MM
12712 S<sizeof (E)> s;
12713 }
12714
12715 The size of `E' cannot be known until the value of `I' has been
12716 determined. Therefore, `E' must be considered dependent. */
12717 scope = TYPE_CONTEXT (type);
12718 if (scope && TYPE_P (scope))
12719 return dependent_type_p (scope);
12720 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12721 return type_dependent_expression_p (scope);
12722
12723 /* Other types are non-dependent. */
12724 return false;
12725}
12726
5552b43c
MM
12727/* Returns TRUE if TYPE is dependent, in the sense of
12728 [temp.dep.type]. */
12729
12730bool
12731dependent_type_p (tree type)
12732{
12733 /* If there are no template parameters in scope, then there can't be
12734 any dependent types. */
12735 if (!processing_template_decl)
e7e93965
MM
12736 {
12737 /* If we are not processing a template, then nobody should be
12738 providing us with a dependent type. */
12739 gcc_assert (type);
12740 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12741 return false;
12742 }
5552b43c
MM
12743
12744 /* If the type is NULL, we have not computed a type for the entity
12745 in question; in that case, the type is dependent. */
12746 if (!type)
12747 return true;
12748
12749 /* Erroneous types can be considered non-dependent. */
12750 if (type == error_mark_node)
12751 return false;
12752
12753 /* If we have not already computed the appropriate value for TYPE,
12754 do so now. */
12755 if (!TYPE_DEPENDENT_P_VALID (type))
12756 {
12757 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12758 TYPE_DEPENDENT_P_VALID (type) = 1;
12759 }
12760
12761 return TYPE_DEPENDENT_P (type);
12762}
12763
8d83f792
MM
12764/* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12765
12766static bool
12767dependent_scope_ref_p (tree expression, bool criterion (tree))
12768{
12769 tree scope;
12770 tree name;
12771
50bc768d 12772 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
8d83f792
MM
12773
12774 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12775 return true;
12776
12777 scope = TREE_OPERAND (expression, 0);
12778 name = TREE_OPERAND (expression, 1);
12779
12780 /* [temp.dep.expr]
12781
12782 An id-expression is type-dependent if it contains a
12783 nested-name-specifier that contains a class-name that names a
12784 dependent type. */
12785 /* The suggested resolution to Core Issue 2 implies that if the
12786 qualifying type is the current class, then we must peek
12787 inside it. */
c8094d83 12788 if (DECL_P (name)
8d83f792
MM
12789 && currently_open_class (scope)
12790 && !criterion (name))
12791 return false;
12792 if (dependent_type_p (scope))
12793 return true;
12794
12795 return false;
12796}
12797
20929c7f 12798/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
fe89d797
MM
12799 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
12800 expression. */
1fb3244a 12801
14d22dd6 12802bool
1fb3244a
MM
12803value_dependent_expression_p (tree expression)
12804{
12805 if (!processing_template_decl)
12806 return false;
12807
12808 /* A name declared with a dependent type. */
7416ab02 12809 if (DECL_P (expression) && type_dependent_expression_p (expression))
1fb3244a 12810 return true;
c8094d83 12811
7416ab02
NS
12812 switch (TREE_CODE (expression))
12813 {
12814 case IDENTIFIER_NODE:
12815 /* A name that has not been looked up -- must be dependent. */
12816 return true;
12817
12818 case TEMPLATE_PARM_INDEX:
12819 /* A non-type template parm. */
12820 return true;
12821
12822 case CONST_DECL:
12823 /* A non-type template parm. */
12824 if (DECL_TEMPLATE_PARM_P (expression))
d17811fd 12825 return true;
7416ab02 12826 return false;
d36d5600 12827
7416ab02 12828 case VAR_DECL:
c8094d83 12829 /* A constant with integral or enumeration type and is initialized
0cbd7506 12830 with an expression that is value-dependent. */
7416ab02
NS
12831 if (DECL_INITIAL (expression)
12832 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12833 && value_dependent_expression_p (DECL_INITIAL (expression)))
12834 return true;
12835 return false;
12836
12837 case DYNAMIC_CAST_EXPR:
12838 case STATIC_CAST_EXPR:
12839 case CONST_CAST_EXPR:
12840 case REINTERPRET_CAST_EXPR:
12841 case CAST_EXPR:
12842 /* These expressions are value-dependent if the type to which
0cbd7506
MS
12843 the cast occurs is dependent or the expression being casted
12844 is value-dependent. */
7416ab02
NS
12845 {
12846 tree type = TREE_TYPE (expression);
c8094d83 12847
7416ab02
NS
12848 if (dependent_type_p (type))
12849 return true;
c8094d83 12850
7416ab02
NS
12851 /* A functional cast has a list of operands. */
12852 expression = TREE_OPERAND (expression, 0);
12853 if (!expression)
12854 {
12855 /* If there are no operands, it must be an expression such
12856 as "int()". This should not happen for aggregate types
12857 because it would form non-constant expressions. */
12858 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
c8094d83 12859
7416ab02
NS
12860 return false;
12861 }
c8094d83 12862
7416ab02
NS
12863 if (TREE_CODE (expression) == TREE_LIST)
12864 {
12865 for (; expression; expression = TREE_CHAIN (expression))
d17811fd
MM
12866 if (value_dependent_expression_p (TREE_VALUE (expression)))
12867 return true;
7416ab02
NS
12868 return false;
12869 }
c8094d83 12870
d17811fd 12871 return value_dependent_expression_p (expression);
7416ab02 12872 }
c8094d83 12873
7416ab02
NS
12874 case SIZEOF_EXPR:
12875 case ALIGNOF_EXPR:
12876 /* A `sizeof' expression is value-dependent if the operand is
0cbd7506 12877 type-dependent. */
d17811fd
MM
12878 expression = TREE_OPERAND (expression, 0);
12879 if (TYPE_P (expression))
12880 return dependent_type_p (expression);
12881 return type_dependent_expression_p (expression);
100d337a 12882
7416ab02
NS
12883 case SCOPE_REF:
12884 return dependent_scope_ref_p (expression, value_dependent_expression_p);
100d337a 12885
7416ab02
NS
12886 case COMPONENT_REF:
12887 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12888 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12889
12890 case CALL_EXPR:
fe89d797
MM
12891 /* A CALL_EXPR may appear in a constant expression if it is a
12892 call to a builtin function, e.g., __builtin_constant_p. All
12893 such calls are value-dependent. */
12894 return true;
7416ab02
NS
12895
12896 default:
12897 /* A constant expression is value-dependent if any subexpression is
0cbd7506 12898 value-dependent. */
1fb3244a
MM
12899 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12900 {
7416ab02 12901 case tcc_reference:
6615c446 12902 case tcc_unary:
c8094d83 12903 return (value_dependent_expression_p
1fb3244a 12904 (TREE_OPERAND (expression, 0)));
c8094d83 12905
6615c446
JO
12906 case tcc_comparison:
12907 case tcc_binary:
c8094d83 12908 return ((value_dependent_expression_p
1fb3244a 12909 (TREE_OPERAND (expression, 0)))
c8094d83 12910 || (value_dependent_expression_p
1fb3244a 12911 (TREE_OPERAND (expression, 1))));
c8094d83 12912
6615c446 12913 case tcc_expression:
1fb3244a
MM
12914 {
12915 int i;
54e4aedb 12916 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
14d22dd6
MM
12917 /* In some cases, some of the operands may be missing.
12918 (For example, in the case of PREDECREMENT_EXPR, the
12919 amount to increment by may be missing.) That doesn't
12920 make the expression dependent. */
12921 if (TREE_OPERAND (expression, i)
12922 && (value_dependent_expression_p
12923 (TREE_OPERAND (expression, i))))
1fb3244a
MM
12924 return true;
12925 return false;
12926 }
c8094d83 12927
6615c446 12928 default:
7416ab02 12929 break;
1fb3244a
MM
12930 }
12931 }
c8094d83 12932
1fb3244a
MM
12933 /* The expression is not value-dependent. */
12934 return false;
12935}
12936
12937/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12938 [temp.dep.expr]. */
12939
12940bool
3a978d72 12941type_dependent_expression_p (tree expression)
1fb3244a
MM
12942{
12943 if (!processing_template_decl)
12944 return false;
12945
7efa3e22
NS
12946 if (expression == error_mark_node)
12947 return false;
10b1d5e7
MM
12948
12949 /* An unresolved name is always dependent. */
b01e6d2b
JM
12950 if (TREE_CODE (expression) == IDENTIFIER_NODE
12951 || TREE_CODE (expression) == USING_DECL)
10b1d5e7 12952 return true;
c8094d83 12953
1fb3244a
MM
12954 /* Some expression forms are never type-dependent. */
12955 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12956 || TREE_CODE (expression) == SIZEOF_EXPR
12957 || TREE_CODE (expression) == ALIGNOF_EXPR
12958 || TREE_CODE (expression) == TYPEID_EXPR
12959 || TREE_CODE (expression) == DELETE_EXPR
12960 || TREE_CODE (expression) == VEC_DELETE_EXPR
12961 || TREE_CODE (expression) == THROW_EXPR)
12962 return false;
12963
12964 /* The types of these expressions depends only on the type to which
12965 the cast occurs. */
12966 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12967 || TREE_CODE (expression) == STATIC_CAST_EXPR
12968 || TREE_CODE (expression) == CONST_CAST_EXPR
12969 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12970 || TREE_CODE (expression) == CAST_EXPR)
12971 return dependent_type_p (TREE_TYPE (expression));
d17811fd 12972
1fb3244a
MM
12973 /* The types of these expressions depends only on the type created
12974 by the expression. */
d17811fd
MM
12975 if (TREE_CODE (expression) == NEW_EXPR
12976 || TREE_CODE (expression) == VEC_NEW_EXPR)
09d2f85f
KL
12977 {
12978 /* For NEW_EXPR tree nodes created inside a template, either
12979 the object type itself or a TREE_LIST may appear as the
12980 operand 1. */
12981 tree type = TREE_OPERAND (expression, 1);
12982 if (TREE_CODE (type) == TREE_LIST)
12983 /* This is an array type. We need to check array dimensions
12984 as well. */
12985 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12986 || value_dependent_expression_p
12987 (TREE_OPERAND (TREE_VALUE (type), 1));
12988 else
12989 return dependent_type_p (type);
12990 }
1fb3244a 12991
5a57f1b2
JM
12992 if (TREE_CODE (expression) == SCOPE_REF
12993 && dependent_scope_ref_p (expression,
12994 type_dependent_expression_p))
12995 return true;
12996
12997 if (TREE_CODE (expression) == FUNCTION_DECL
12998 && DECL_LANG_SPECIFIC (expression)
12999 && DECL_TEMPLATE_INFO (expression)
13000 && (any_dependent_template_arguments_p
13001 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
13002 return true;
13003
13004 if (TREE_CODE (expression) == TEMPLATE_DECL
13005 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
13006 return false;
13007
d17811fd
MM
13008 if (TREE_TYPE (expression) == unknown_type_node)
13009 {
13010 if (TREE_CODE (expression) == ADDR_EXPR)
13011 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
c353b8e3
MM
13012 if (TREE_CODE (expression) == COMPONENT_REF
13013 || TREE_CODE (expression) == OFFSET_REF)
6cb89308
NS
13014 {
13015 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
13016 return true;
13017 expression = TREE_OPERAND (expression, 1);
13018 if (TREE_CODE (expression) == IDENTIFIER_NODE)
13019 return false;
13020 }
3601f003
KL
13021 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
13022 if (TREE_CODE (expression) == SCOPE_REF)
13023 return false;
c8094d83 13024
d17811fd
MM
13025 if (TREE_CODE (expression) == BASELINK)
13026 expression = BASELINK_FUNCTIONS (expression);
c8094d83 13027
d17811fd
MM
13028 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
13029 {
ee3071ef
NS
13030 if (any_dependent_template_arguments_p
13031 (TREE_OPERAND (expression, 1)))
d17811fd
MM
13032 return true;
13033 expression = TREE_OPERAND (expression, 0);
13034 }
b207d6e2
MM
13035 gcc_assert (TREE_CODE (expression) == OVERLOAD
13036 || TREE_CODE (expression) == FUNCTION_DECL);
c8094d83 13037
315fb5db 13038 while (expression)
d17811fd 13039 {
315fb5db
NS
13040 if (type_dependent_expression_p (OVL_CURRENT (expression)))
13041 return true;
13042 expression = OVL_NEXT (expression);
d17811fd 13043 }
315fb5db 13044 return false;
d17811fd 13045 }
c8094d83 13046
3ce5fa4f 13047 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
3db45ab5 13048
1fb3244a
MM
13049 return (dependent_type_p (TREE_TYPE (expression)));
13050}
13051
d17811fd
MM
13052/* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
13053 contains a type-dependent expression. */
1fb3244a
MM
13054
13055bool
d17811fd
MM
13056any_type_dependent_arguments_p (tree args)
13057{
13058 while (args)
13059 {
7efa3e22
NS
13060 tree arg = TREE_VALUE (args);
13061
13062 if (type_dependent_expression_p (arg))
d17811fd
MM
13063 return true;
13064 args = TREE_CHAIN (args);
13065 }
13066 return false;
13067}
13068
13069/* Returns TRUE if the ARG (a template argument) is dependent. */
13070
13071static bool
1fb3244a
MM
13072dependent_template_arg_p (tree arg)
13073{
13074 if (!processing_template_decl)
13075 return false;
13076
13077 if (TREE_CODE (arg) == TEMPLATE_DECL
13078 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13079 return dependent_template_p (arg);
13080 else if (TYPE_P (arg))
13081 return dependent_type_p (arg);
13082 else
13083 return (type_dependent_expression_p (arg)
13084 || value_dependent_expression_p (arg));
13085}
13086
d17811fd
MM
13087/* Returns true if ARGS (a collection of template arguments) contains
13088 any dependent arguments. */
1fb3244a 13089
d17811fd
MM
13090bool
13091any_dependent_template_arguments_p (tree args)
1fb3244a 13092{
bf12d54d 13093 int i;
c353b8e3
MM
13094 int j;
13095
d17811fd
MM
13096 if (!args)
13097 return false;
7e497d0c
VR
13098 if (args == error_mark_node)
13099 return true;
d17811fd 13100
c353b8e3
MM
13101 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
13102 {
13103 tree level = TMPL_ARGS_LEVEL (args, i + 1);
13104 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
13105 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
13106 return true;
13107 }
1fb3244a 13108
1fb3244a
MM
13109 return false;
13110}
13111
13112/* Returns TRUE if the template TMPL is dependent. */
13113
13114bool
13115dependent_template_p (tree tmpl)
13116{
b95cc51a
MM
13117 if (TREE_CODE (tmpl) == OVERLOAD)
13118 {
13119 while (tmpl)
13120 {
13121 if (dependent_template_p (OVL_FUNCTION (tmpl)))
13122 return true;
13123 tmpl = OVL_CHAIN (tmpl);
13124 }
13125 return false;
13126 }
13127
1fb3244a
MM
13128 /* Template template parameters are dependent. */
13129 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
13130 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
13131 return true;
27ab0504 13132 /* So are names that have not been looked up. */
acccf788
MM
13133 if (TREE_CODE (tmpl) == SCOPE_REF
13134 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
d17811fd 13135 return true;
1fb3244a
MM
13136 /* So are member templates of dependent classes. */
13137 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
13138 return dependent_type_p (DECL_CONTEXT (tmpl));
13139 return false;
13140}
13141
d17811fd
MM
13142/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
13143
13144bool
13145dependent_template_id_p (tree tmpl, tree args)
13146{
13147 return (dependent_template_p (tmpl)
13148 || any_dependent_template_arguments_p (args));
13149}
13150
14d22dd6
MM
13151/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13152 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
13153 can be found. Note that this function peers inside uninstantiated
13154 templates and therefore should be used only in extremely limited
dda04398 13155 situations. ONLY_CURRENT_P restricts this peering to the currently
c51940a2 13156 open classes hierarchy (which is required when comparing types). */
14d22dd6
MM
13157
13158tree
13159resolve_typename_type (tree type, bool only_current_p)
13160{
13161 tree scope;
13162 tree name;
13163 tree decl;
13164 int quals;
4514aa8c 13165 tree pushed_scope;
14d22dd6 13166
50bc768d 13167 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
14d22dd6
MM
13168
13169 scope = TYPE_CONTEXT (type);
13170 name = TYPE_IDENTIFIER (type);
13171
13172 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13173 it first before we can figure out what NAME refers to. */
13174 if (TREE_CODE (scope) == TYPENAME_TYPE)
13175 scope = resolve_typename_type (scope, only_current_p);
13176 /* If we don't know what SCOPE refers to, then we cannot resolve the
13177 TYPENAME_TYPE. */
13178 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
13179 return error_mark_node;
13180 /* If the SCOPE is a template type parameter, we have no way of
13181 resolving the name. */
13182 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13183 return type;
13184 /* If the SCOPE is not the current instantiation, there's no reason
13185 to look inside it. */
13186 if (only_current_p && !currently_open_class (scope))
13187 return error_mark_node;
ca099ac8
MM
13188 /* If SCOPE is a partial instantiation, it will not have a valid
13189 TYPE_FIELDS list, so use the original template. */
353b4fc0 13190 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
14d22dd6
MM
13191 /* Enter the SCOPE so that name lookup will be resolved as if we
13192 were in the class definition. In particular, SCOPE will no
13193 longer be considered a dependent type. */
4514aa8c 13194 pushed_scope = push_scope (scope);
14d22dd6 13195 /* Look up the declaration. */
86ac0575 13196 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
14d22dd6
MM
13197 /* Obtain the set of qualifiers applied to the TYPE. */
13198 quals = cp_type_quals (type);
13199 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
13200 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
13201 if (!decl)
13202 type = error_mark_node;
13203 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
13204 && TREE_CODE (decl) == TYPE_DECL)
13205 type = TREE_TYPE (decl);
13206 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
13207 && DECL_CLASS_TEMPLATE_P (decl))
13208 {
13209 tree tmpl;
13210 tree args;
13211 /* Obtain the template and the arguments. */
13212 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
13213 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
13214 /* Instantiate the template. */
13215 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
42eaed49 13216 /*entering_scope=*/0, tf_error | tf_user);
14d22dd6
MM
13217 }
13218 else
13219 type = error_mark_node;
13220 /* Qualify the resulting type. */
13221 if (type != error_mark_node && quals)
13222 type = cp_build_qualified_type (type, quals);
13223 /* Leave the SCOPE. */
4514aa8c
NS
13224 if (pushed_scope)
13225 pop_scope (pushed_scope);
14d22dd6
MM
13226
13227 return type;
13228}
13229
d17811fd
MM
13230/* EXPR is an expression which is not type-dependent. Return a proxy
13231 for EXPR that can be used to compute the types of larger
13232 expressions containing EXPR. */
13233
13234tree
13235build_non_dependent_expr (tree expr)
13236{
0deb916c
MM
13237 tree inner_expr;
13238
c8094d83 13239 /* Preserve null pointer constants so that the type of things like
d17811fd
MM
13240 "p == 0" where "p" is a pointer can be determined. */
13241 if (null_ptr_cst_p (expr))
13242 return expr;
13243 /* Preserve OVERLOADs; the functions must be available to resolve
13244 types. */
c497db75
AO
13245 inner_expr = expr;
13246 if (TREE_CODE (inner_expr) == ADDR_EXPR)
13247 inner_expr = TREE_OPERAND (inner_expr, 0);
13248 if (TREE_CODE (inner_expr) == COMPONENT_REF)
13249 inner_expr = TREE_OPERAND (inner_expr, 1);
2226e997 13250 if (is_overloaded_fn (inner_expr)
6439fffd 13251 || TREE_CODE (inner_expr) == OFFSET_REF)
d17811fd 13252 return expr;
5ae9ba3e
MM
13253 /* There is no need to return a proxy for a variable. */
13254 if (TREE_CODE (expr) == VAR_DECL)
13255 return expr;
7433e6d4
MM
13256 /* Preserve string constants; conversions from string constants to
13257 "char *" are allowed, even though normally a "const char *"
13258 cannot be used to initialize a "char *". */
13259 if (TREE_CODE (expr) == STRING_CST)
13260 return expr;
b7c707d1
MM
13261 /* Preserve arithmetic constants, as an optimization -- there is no
13262 reason to create a new node. */
13263 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
13264 return expr;
9b7be7b5
MM
13265 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13266 There is at least one place where we want to know that a
13267 particular expression is a throw-expression: when checking a ?:
13268 expression, there are special rules if the second or third
878cbb73 13269 argument is a throw-expression. */
9b7be7b5
MM
13270 if (TREE_CODE (expr) == THROW_EXPR)
13271 return expr;
47d4c811
NS
13272
13273 if (TREE_CODE (expr) == COND_EXPR)
f293ce4b
RS
13274 return build3 (COND_EXPR,
13275 TREE_TYPE (expr),
13276 TREE_OPERAND (expr, 0),
c8094d83 13277 (TREE_OPERAND (expr, 1)
f293ce4b
RS
13278 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
13279 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
13280 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
8e1daa34
NS
13281 if (TREE_CODE (expr) == COMPOUND_EXPR
13282 && !COMPOUND_EXPR_OVERLOADED (expr))
f293ce4b
RS
13283 return build2 (COMPOUND_EXPR,
13284 TREE_TYPE (expr),
13285 TREE_OPERAND (expr, 0),
13286 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
c8094d83 13287
fdeff563
NS
13288 /* If the type is unknown, it can't really be non-dependent */
13289 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
3db45ab5 13290
c8094d83 13291 /* Otherwise, build a NON_DEPENDENT_EXPR.
d17811fd
MM
13292
13293 REFERENCE_TYPEs are not stripped for expressions in templates
13294 because doing so would play havoc with mangling. Consider, for
13295 example:
13296
c8094d83 13297 template <typename T> void f<T& g>() { g(); }
d17811fd
MM
13298
13299 In the body of "f", the expression for "g" will have
13300 REFERENCE_TYPE, even though the standard says that it should
13301 not. The reason is that we must preserve the syntactic form of
13302 the expression so that mangling (say) "f<g>" inside the body of
13303 "f" works out correctly. Therefore, the REFERENCE_TYPE is
13304 stripped here. */
018a5803 13305 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
d17811fd
MM
13306}
13307
13308/* ARGS is a TREE_LIST of expressions as arguments to a function call.
13309 Return a new TREE_LIST with the various arguments replaced with
13310 equivalent non-dependent expressions. */
13311
13312tree
13313build_non_dependent_args (tree args)
13314{
13315 tree a;
13316 tree new_args;
13317
13318 new_args = NULL_TREE;
13319 for (a = args; a; a = TREE_CHAIN (a))
c8094d83 13320 new_args = tree_cons (NULL_TREE,
d17811fd
MM
13321 build_non_dependent_expr (TREE_VALUE (a)),
13322 new_args);
13323 return nreverse (new_args);
13324}
13325
e2500fed 13326#include "gt-cp-pt.h"
This page took 6.056378 seconds and 5 git commands to generate.