]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
re PR tree-optimization/28411 ("Illegal instruction" error with -ftrapv)
[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);
145static void check_specialization_scope (void);
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
0e339752 538 invalid to declare a specialization here, an error is reported. */
6c30752f 539
e9659ab0 540static void
3a978d72 541check_specialization_scope (void)
6c30752f
MM
542{
543 tree scope = current_scope ();
3ddfb0e6 544
c8094d83
MS
545 /* [temp.expl.spec]
546
6c30752f
MM
547 An explicit specialization shall be declared in the namespace of
548 which the template is a member, or, for member templates, in the
549 namespace of which the enclosing class or enclosing class
550 template is a member. An explicit specialization of a member
551 function, member class or static data member of a class template
552 shall be declared in the namespace of which the class template
553 is a member. */
554 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
0f51ccfc 555 error ("explicit specialization in non-namespace scope %qD", scope);
3ddfb0e6 556
c8094d83 557 /* [temp.expl.spec]
6c30752f
MM
558
559 In an explicit specialization declaration for a member of a class
560 template or a member template that appears in namespace scope,
561 the member template and some of its enclosing class templates may
562 remain unspecialized, except that the declaration shall not
563 explicitly specialize a class member template if its enclosing
564 class templates are not explicitly specialized as well. */
c8094d83 565 if (current_template_parms)
33bd39a2 566 error ("enclosing class templates are not explicitly specialized");
6c30752f
MM
567}
568
c6002625 569/* We've just seen template <>. */
386b8a85
JM
570
571void
3a978d72 572begin_specialization (void)
386b8a85 573{
ac20c67a 574 begin_scope (sk_template_spec, NULL);
386b8a85 575 note_template_header (1);
6c30752f 576 check_specialization_scope ();
386b8a85
JM
577}
578
dc957d14 579/* Called at then end of processing a declaration preceded by
386b8a85
JM
580 template<>. */
581
c8094d83 582void
3a978d72 583end_specialization (void)
386b8a85 584{
74b846e0 585 finish_scope ();
386b8a85
JM
586 reset_specialization ();
587}
588
386b8a85 589/* Any template <>'s that we have seen thus far are not referring to a
c6002625 590 function specialization. */
386b8a85
JM
591
592void
3a978d72 593reset_specialization (void)
386b8a85
JM
594{
595 processing_specialization = 0;
596 template_header_count = 0;
597}
598
838dfd8a 599/* We've just seen a template header. If SPECIALIZATION is nonzero,
386b8a85
JM
600 it was of the form template <>. */
601
c8094d83 602static void
3a978d72 603note_template_header (int specialization)
386b8a85
JM
604{
605 processing_specialization = specialization;
606 template_header_count++;
607}
608
75650646 609/* We're beginning an explicit instantiation. */
386b8a85 610
75650646 611void
3a978d72 612begin_explicit_instantiation (void)
386b8a85 613{
50bc768d 614 gcc_assert (!processing_explicit_instantiation);
a723baf1 615 processing_explicit_instantiation = true;
75650646 616}
386b8a85 617
386b8a85 618
75650646 619void
3a978d72 620end_explicit_instantiation (void)
75650646 621{
50bc768d 622 gcc_assert (processing_explicit_instantiation);
a723baf1 623 processing_explicit_instantiation = false;
75650646 624}
386b8a85 625
13a44ee0 626/* An explicit specialization or partial specialization TMPL is being
b1cc95ce
MM
627 declared. Check that the namespace in which the specialization is
628 occurring is permissible. Returns false iff it is invalid to
629 specialize TMPL in the current namespace. */
c8094d83 630
b1cc95ce
MM
631static bool
632check_specialization_namespace (tree tmpl)
633{
634 tree tpl_ns = decl_namespace_context (tmpl);
635
636 /* [tmpl.expl.spec]
c8094d83 637
b1cc95ce
MM
638 An explicit specialization shall be declared in the namespace of
639 which the template is a member, or, for member templates, in the
640 namespace of which the enclosing class or enclosing class
641 template is a member. An explicit specialization of a member
642 function, member class or static data member of a class template
643 shall be declared in the namespace of which the class template is
644 a member. */
645 if (is_associated_namespace (current_namespace, tpl_ns))
646 /* Same or super-using namespace. */
647 return true;
648 else
649 {
0f51ccfc 650 pedwarn ("specialization of %qD in different namespace", tmpl);
dee15844 651 pedwarn (" from definition of %q+#D", tmpl);
b1cc95ce
MM
652 return false;
653 }
654}
655
9c12301f
MM
656/* SPEC is an explicit instantiation. Check that it is valid to
657 perform this explicit instantiation in the current namespace. */
658
659static void
660check_explicit_instantiation_namespace (tree spec)
661{
662 tree ns;
663
664 /* DR 275: An explicit instantiation shall appear in an enclosing
3db45ab5 665 namespace of its template. */
9c12301f
MM
666 ns = decl_namespace_context (spec);
667 if (!is_ancestor (current_namespace, ns))
668 pedwarn ("explicit instantiation of %qD in namespace %qD "
312ea236 669 "(which does not enclose namespace %qD)",
9c12301f
MM
670 spec, current_namespace, ns);
671}
672
36a117a5
MM
673/* The TYPE is being declared. If it is a template type, that means it
674 is a partial specialization. Do appropriate error-checking. */
675
c8094d83 676void
3a978d72 677maybe_process_partial_specialization (tree type)
36a117a5 678{
c9043d03
VR
679 tree context;
680
681 if (type == error_mark_node)
682 return;
683
684 context = TYPE_CONTEXT (type);
6e049fcd 685
a723baf1 686 if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
36a117a5 687 {
6e049fcd
KL
688 /* This is for ordinary explicit specialization and partial
689 specialization of a template class such as:
690
691 template <> class C<int>;
692
693 or:
694
695 template <class T> class C<T*>;
696
697 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
698
36a117a5 699 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
d0f062fb 700 && !COMPLETE_TYPE_P (type))
36a117a5 701 {
b1cc95ce 702 check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
370af2d5 703 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
36a117a5
MM
704 if (processing_template_decl)
705 push_template_decl (TYPE_MAIN_DECL (type));
706 }
707 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
0f51ccfc 708 error ("specialization of %qT after instantiation", type);
36a117a5 709 }
6e049fcd
KL
710 else if (CLASS_TYPE_P (type)
711 && !CLASSTYPE_USE_TEMPLATE (type)
712 && CLASSTYPE_TEMPLATE_INFO (type)
713 && context && CLASS_TYPE_P (context)
714 && CLASSTYPE_TEMPLATE_INFO (context))
715 {
716 /* This is for an explicit specialization of member class
717 template according to [temp.expl.spec/18]:
718
719 template <> template <class U> class C<int>::D;
720
721 The context `C<int>' must be an implicit instantiation.
722 Otherwise this is just a member class template declared
723 earlier like:
724
725 template <> class C<int> { template <class U> class D; };
726 template <> template <class U> class C<int>::D;
727
728 In the first case, `C<int>::D' is a specialization of `C<T>::D'
729 while in the second case, `C<int>::D' is a primary template
730 and `C<T>::D' may not exist. */
731
732 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
733 && !COMPLETE_TYPE_P (type))
734 {
735 tree t;
736
737 if (current_namespace
738 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
739 {
0f51ccfc 740 pedwarn ("specializing %q#T in different namespace", type);
dee15844
JM
741 pedwarn (" from definition of %q+#D",
742 CLASSTYPE_TI_TEMPLATE (type));
6e049fcd
KL
743 }
744
745 /* Check for invalid specialization after instantiation:
746
747 template <> template <> class C<int>::D<int>;
748 template <> template <class U> class C<int>::D; */
749
750 for (t = DECL_TEMPLATE_INSTANTIATIONS
751 (most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
752 t; t = TREE_CHAIN (t))
753 if (TREE_VALUE (t) != type
754 && TYPE_CONTEXT (TREE_VALUE (t)) == context)
0f51ccfc 755 error ("specialization %qT after instantiation %qT",
6e049fcd
KL
756 type, TREE_VALUE (t));
757
758 /* Mark TYPE as a specialization. And as a result, we only
759 have one level of template argument for the innermost
760 class template. */
761 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
762 CLASSTYPE_TI_ARGS (type)
763 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
764 }
765 }
20496fa2 766 else if (processing_specialization)
0f51ccfc 767 error ("explicit specialization of non-template %qT", type);
36a117a5
MM
768}
769
ee81147e 770/* Returns nonzero if we can optimize the retrieval of specializations
c7222c02
MM
771 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
772 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
773
c8094d83 774static inline bool
c7222c02
MM
775optimize_specialization_lookup_p (tree tmpl)
776{
777 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
778 && DECL_CLASS_SCOPE_P (tmpl)
779 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
780 parameter. */
781 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1ca939e5
MM
782 /* The optimized lookup depends on the fact that the
783 template arguments for the member function template apply
784 purely to the containing class, which is not true if the
785 containing class is an explicit or partial
786 specialization. */
787 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
c7222c02
MM
788 && !DECL_MEMBER_TEMPLATE_P (tmpl)
789 && !DECL_CONV_FN_P (tmpl)
790 /* It is possible to have a template that is not a member
791 template and is not a member of a template class:
c8094d83
MS
792
793 template <typename T>
c7222c02 794 struct S { friend A::f(); };
c8094d83 795
c7222c02
MM
796 Here, the friend function is a template, but the context does
797 not have template information. The optimized lookup relies
798 on having ARGS be the template arguments for both the class
799 and the function template. */
800 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
801}
802
75650646
MM
803/* Retrieve the specialization (in the sense of [temp.spec] - a
804 specialization is either an instantiation or an explicit
805 specialization) of TMPL for the given template ARGS. If there is
806 no such specialization, return NULL_TREE. The ARGS are a vector of
807 arguments, or a vector of vectors of arguments, in the case of
c8094d83 808 templates with more than one level of parameters.
c7222c02
MM
809
810 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
811 then we search for a partial specialization matching ARGS. This
812 parameter is ignored if TMPL is not a class template. */
c8094d83 813
75650646 814static tree
c8094d83 815retrieve_specialization (tree tmpl, tree args,
c7222c02 816 bool class_specializations_p)
75650646 817{
2d8ba2c7
LM
818 if (args == error_mark_node)
819 return NULL_TREE;
820
50bc768d 821 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
75650646 822
36a117a5
MM
823 /* There should be as many levels of arguments as there are
824 levels of parameters. */
c8094d83 825 gcc_assert (TMPL_ARGS_DEPTH (args)
50bc768d 826 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
c8094d83 827
c7222c02
MM
828 if (optimize_specialization_lookup_p (tmpl))
829 {
830 tree class_template;
831 tree class_specialization;
d4e6fecb 832 VEC(tree,gc) *methods;
c7222c02
MM
833 tree fns;
834 int idx;
835
836 /* The template arguments actually apply to the containing
837 class. Find the class specialization with those
838 arguments. */
839 class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
c8094d83 840 class_specialization
c7222c02
MM
841 = retrieve_specialization (class_template, args,
842 /*class_specializations_p=*/false);
843 if (!class_specialization)
844 return NULL_TREE;
845 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
846 for the specialization. */
847 idx = class_method_index_for_fn (class_specialization, tmpl);
848 if (idx == -1)
849 return NULL_TREE;
850 /* Iterate through the methods with the indicated name, looking
851 for the one that has an instance of TMPL. */
852 methods = CLASSTYPE_METHOD_VEC (class_specialization);
853 for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
854 {
855 tree fn = OVL_CURRENT (fns);
856 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
857 return fn;
858 }
859 return NULL_TREE;
860 }
861 else
862 {
863 tree *sp;
864 tree *head;
865
866 /* Class templates store their instantiations on the
867 DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
868 DECL_TEMPLATE_SPECIALIZATIONS list. */
869 if (!class_specializations_p
870 && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
871 sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
872 else
873 sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
874 head = sp;
875 /* Iterate through the list until we find a matching template. */
876 while (*sp != NULL_TREE)
877 {
878 tree spec = *sp;
c8094d83 879
c7222c02
MM
880 if (comp_template_args (TREE_PURPOSE (spec), args))
881 {
882 /* Use the move-to-front heuristic to speed up future
c8094d83 883 searches. */
c7222c02
MM
884 if (spec != *head)
885 {
886 *sp = TREE_CHAIN (*sp);
887 TREE_CHAIN (spec) = *head;
888 *head = spec;
889 }
890 return TREE_VALUE (spec);
891 }
892 sp = &TREE_CHAIN (spec);
893 }
894 }
75650646
MM
895
896 return NULL_TREE;
386b8a85
JM
897}
898
dc957d14 899/* Like retrieve_specialization, but for local declarations. */
9188c363
MM
900
901static tree
3a978d72 902retrieve_local_specialization (tree tmpl)
9188c363 903{
67f5655f 904 tree spec = (tree) htab_find_with_hash (local_specializations, tmpl,
3db45ab5 905 htab_hash_pointer (tmpl));
a723baf1 906 return spec ? TREE_PURPOSE (spec) : NULL_TREE;
9188c363
MM
907}
908
838dfd8a 909/* Returns nonzero iff DECL is a specialization of TMPL. */
6757edfe
MM
910
911int
3a978d72 912is_specialization_of (tree decl, tree tmpl)
6757edfe
MM
913{
914 tree t;
915
916 if (TREE_CODE (decl) == FUNCTION_DECL)
917 {
c8094d83 918 for (t = decl;
6757edfe
MM
919 t != NULL_TREE;
920 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
921 if (t == tmpl)
922 return 1;
923 }
c8094d83 924 else
6757edfe 925 {
50bc768d 926 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
6757edfe
MM
927
928 for (t = TREE_TYPE (decl);
929 t != NULL_TREE;
930 t = CLASSTYPE_USE_TEMPLATE (t)
931 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
9edc3913 932 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
6757edfe 933 return 1;
c8094d83 934 }
386b8a85 935
6757edfe
MM
936 return 0;
937}
75650646 938
d43f603d
KL
939/* Returns nonzero iff DECL is a specialization of friend declaration
940 FRIEND according to [temp.friend]. */
941
942bool
943is_specialization_of_friend (tree decl, tree friend)
944{
945 bool need_template = true;
946 int template_depth;
947
b939a023
KL
948 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
949 || TREE_CODE (decl) == TYPE_DECL);
d43f603d
KL
950
951 /* For [temp.friend/6] when FRIEND is an ordinary member function
952 of a template class, we want to check if DECL is a specialization
953 if this. */
954 if (TREE_CODE (friend) == FUNCTION_DECL
955 && DECL_TEMPLATE_INFO (friend)
956 && !DECL_USE_TEMPLATE (friend))
957 {
b939a023 958 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
d43f603d
KL
959 friend = DECL_TI_TEMPLATE (friend);
960 need_template = false;
961 }
b939a023
KL
962 else if (TREE_CODE (friend) == TEMPLATE_DECL
963 && !PRIMARY_TEMPLATE_P (friend))
964 need_template = false;
d43f603d
KL
965
966 /* There is nothing to do if this is not a template friend. */
967 if (TREE_CODE (friend) != TEMPLATE_DECL)
b939a023 968 return false;
d43f603d
KL
969
970 if (is_specialization_of (decl, friend))
b939a023 971 return true;
d43f603d
KL
972
973 /* [temp.friend/6]
974 A member of a class template may be declared to be a friend of a
975 non-template class. In this case, the corresponding member of
976 every specialization of the class template is a friend of the
977 class granting friendship.
c8094d83 978
d43f603d
KL
979 For example, given a template friend declaration
980
981 template <class T> friend void A<T>::f();
982
983 the member function below is considered a friend
984
985 template <> struct A<int> {
986 void f();
987 };
988
989 For this type of template friend, TEMPLATE_DEPTH below will be
5c425df5 990 nonzero. To determine if DECL is a friend of FRIEND, we first
d43f603d
KL
991 check if the enclosing class is a specialization of another. */
992
993 template_depth = template_class_depth (DECL_CONTEXT (friend));
994 if (template_depth
995 && DECL_CLASS_SCOPE_P (decl)
c8094d83 996 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
d43f603d
KL
997 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
998 {
999 /* Next, we check the members themselves. In order to handle
b939a023 1000 a few tricky cases, such as when FRIEND's are
d43f603d
KL
1001
1002 template <class T> friend void A<T>::g(T t);
1003 template <class T> template <T t> friend void A<T>::h();
1004
b939a023
KL
1005 and DECL's are
1006
1007 void A<int>::g(int);
1008 template <int> void A<int>::h();
1009
1010 we need to figure out ARGS, the template arguments from
1011 the context of DECL. This is required for template substitution
1012 of `T' in the function parameter of `g' and template parameter
1013 of `h' in the above examples. Here ARGS corresponds to `int'. */
d43f603d
KL
1014
1015 tree context = DECL_CONTEXT (decl);
1016 tree args = NULL_TREE;
1017 int current_depth = 0;
b939a023 1018
d43f603d
KL
1019 while (current_depth < template_depth)
1020 {
1021 if (CLASSTYPE_TEMPLATE_INFO (context))
1022 {
1023 if (current_depth == 0)
1024 args = TYPE_TI_ARGS (context);
1025 else
1026 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1027 current_depth++;
1028 }
1029 context = TYPE_CONTEXT (context);
1030 }
1031
1032 if (TREE_CODE (decl) == FUNCTION_DECL)
1033 {
1034 bool is_template;
1035 tree friend_type;
1036 tree decl_type;
1037 tree friend_args_type;
1038 tree decl_args_type;
1039
1040 /* Make sure that both DECL and FRIEND are templates or
1041 non-templates. */
1042 is_template = DECL_TEMPLATE_INFO (decl)
1043 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1044 if (need_template ^ is_template)
b939a023 1045 return false;
d43f603d
KL
1046 else if (is_template)
1047 {
da1d7781 1048 /* If both are templates, check template parameter list. */
d43f603d
KL
1049 tree friend_parms
1050 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1051 args, tf_none);
1052 if (!comp_template_parms
1053 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1054 friend_parms))
b939a023 1055 return false;
d43f603d
KL
1056
1057 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1058 }
1059 else
1060 decl_type = TREE_TYPE (decl);
1061
1062 friend_type = tsubst_function_type (TREE_TYPE (friend), args,
1063 tf_none, NULL_TREE);
1064 if (friend_type == error_mark_node)
b939a023 1065 return false;
d43f603d
KL
1066
1067 /* Check if return types match. */
1068 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
b939a023 1069 return false;
d43f603d
KL
1070
1071 /* Check if function parameter types match, ignoring the
1072 `this' parameter. */
1073 friend_args_type = TYPE_ARG_TYPES (friend_type);
1074 decl_args_type = TYPE_ARG_TYPES (decl_type);
1075 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
1076 friend_args_type = TREE_CHAIN (friend_args_type);
1077 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1078 decl_args_type = TREE_CHAIN (decl_args_type);
b939a023
KL
1079
1080 return compparms (decl_args_type, friend_args_type);
1081 }
1082 else
1083 {
1084 /* DECL is a TYPE_DECL */
1085 bool is_template;
1086 tree decl_type = TREE_TYPE (decl);
1087
1088 /* Make sure that both DECL and FRIEND are templates or
1089 non-templates. */
1090 is_template
1091 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1093
1094 if (need_template ^ is_template)
1095 return false;
1096 else if (is_template)
1097 {
1098 tree friend_parms;
1099 /* If both are templates, check the name of the two
1100 TEMPLATE_DECL's first because is_friend didn't. */
1101 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1102 != DECL_NAME (friend))
1103 return false;
1104
1105 /* Now check template parameter list. */
1106 friend_parms
1107 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
1108 args, tf_none);
1109 return comp_template_parms
1110 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1111 friend_parms);
1112 }
1113 else
1114 return (DECL_NAME (decl)
1115 == DECL_NAME (friend));
d43f603d
KL
1116 }
1117 }
b939a023 1118 return false;
d43f603d
KL
1119}
1120
75650646 1121/* Register the specialization SPEC as a specialization of TMPL with
d63d5d0c
ILT
1122 the indicated ARGS. IS_FRIEND indicates whether the specialization
1123 is actually just a friend declaration. Returns SPEC, or an
1124 equivalent prior declaration, if available. */
75650646 1125
36a117a5 1126static tree
d63d5d0c 1127register_specialization (tree spec, tree tmpl, tree args, bool is_friend)
75650646 1128{
c7222c02 1129 tree fn;
75650646 1130
50bc768d 1131 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
75650646 1132
c8094d83 1133 if (TREE_CODE (spec) == FUNCTION_DECL
36a117a5
MM
1134 && uses_template_parms (DECL_TI_ARGS (spec)))
1135 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1136 register it; we want the corresponding TEMPLATE_DECL instead.
1137 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1138 the more obvious `uses_template_parms (spec)' to avoid problems
1139 with default function arguments. In particular, given
1140 something like this:
1141
0cbd7506 1142 template <class T> void f(T t1, T t = T())
36a117a5
MM
1143
1144 the default argument expression is not substituted for in an
1145 instantiation unless and until it is actually needed. */
1146 return spec;
28ba38e3 1147
c8094d83 1148 fn = retrieve_specialization (tmpl, args,
c7222c02
MM
1149 /*class_specializations_p=*/false);
1150 /* We can sometimes try to re-register a specialization that we've
1151 already got. In particular, regenerate_decl_from_template calls
1152 duplicate_decls which will update the specialization list. But,
1153 we'll still get called again here anyhow. It's more convenient
1154 to simply allow this than to try to prevent it. */
1155 if (fn == spec)
1156 return spec;
1157 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
22e9174f 1158 {
c7222c02 1159 if (DECL_TEMPLATE_INSTANTIATION (fn))
22e9174f 1160 {
c8094d83 1161 if (TREE_USED (fn)
c7222c02 1162 || DECL_EXPLICIT_INSTANTIATION (fn))
22e9174f 1163 {
c7222c02
MM
1164 error ("specialization of %qD after instantiation",
1165 fn);
99f4234a 1166 return error_mark_node;
22e9174f 1167 }
c7222c02 1168 else
b1cc95ce 1169 {
a39041fa 1170 tree clone;
c7222c02
MM
1171 /* This situation should occur only if the first
1172 specialization is an implicit instantiation, the
1173 second is an explicit specialization, and the
1174 implicit instantiation has not yet been used. That
1175 situation can occur if we have implicitly
1176 instantiated a member function and then specialized
1177 it later.
c8094d83 1178
c7222c02
MM
1179 We can also wind up here if a friend declaration that
1180 looked like an instantiation turns out to be a
1181 specialization:
c8094d83 1182
c7222c02
MM
1183 template <class T> void foo(T);
1184 class S { friend void foo<>(int) };
c8094d83
MS
1185 template <> void foo(int);
1186
c7222c02
MM
1187 We transform the existing DECL in place so that any
1188 pointers to it become pointers to the updated
1189 declaration.
1190
1191 If there was a definition for the template, but not
1192 for the specialization, we want this to look as if
1193 there were no definition, and vice versa. */
1194 DECL_INITIAL (fn) = NULL_TREE;
d63d5d0c 1195 duplicate_decls (spec, fn, is_friend);
a39041fa 1196 /* The call to duplicate_decls will have applied
3db45ab5 1197 [temp.expl.spec]:
a39041fa 1198
3db45ab5 1199 An explicit specialization of a function template
a39041fa 1200 is inline only if it is explicitly declared to be,
bcf51da2 1201 and independently of whether its function template
a39041fa
MM
1202 is.
1203
1204 to the primary function; now copy the inline bits to
3db45ab5 1205 the various clones. */
a39041fa
MM
1206 FOR_EACH_CLONE (clone, fn)
1207 {
1208 DECL_DECLARED_INLINE_P (clone)
1209 = DECL_DECLARED_INLINE_P (fn);
1210 DECL_INLINE (clone)
1211 = DECL_INLINE (fn);
1212 }
9c12301f 1213 check_specialization_namespace (fn);
c8094d83 1214
b1cc95ce
MM
1215 return fn;
1216 }
22e9174f 1217 }
c7222c02
MM
1218 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1219 {
d63d5d0c 1220 if (!duplicate_decls (spec, fn, is_friend) && DECL_INITIAL (spec))
c7222c02
MM
1221 /* Dup decl failed, but this is a new definition. Set the
1222 line number so any errors match this new
1223 definition. */
1224 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
c8094d83 1225
c7222c02
MM
1226 return fn;
1227 }
b1cc95ce
MM
1228 }
1229
1230 /* A specialization must be declared in the same namespace as the
1231 template it is specializing. */
1232 if (DECL_TEMPLATE_SPECIALIZATION (spec)
1233 && !check_specialization_namespace (tmpl))
74b0d490 1234 DECL_CONTEXT (spec) = FROB_CONTEXT (decl_namespace_context (tmpl));
75650646 1235
c7222c02
MM
1236 if (!optimize_specialization_lookup_p (tmpl))
1237 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
1238 = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
36a117a5
MM
1239
1240 return spec;
1241}
1242
1243/* Unregister the specialization SPEC as a specialization of TMPL.
b3445994
MM
1244 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1245 if the SPEC was listed as a specialization of TMPL. */
36a117a5 1246
b3445994
MM
1247bool
1248reregister_specialization (tree spec, tree tmpl, tree new_spec)
36a117a5
MM
1249{
1250 tree* s;
1251
1252 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
1253 *s != NULL_TREE;
1254 s = &TREE_CHAIN (*s))
1255 if (TREE_VALUE (*s) == spec)
1256 {
b3445994
MM
1257 if (!new_spec)
1258 *s = TREE_CHAIN (*s);
1259 else
a3d87771 1260 TREE_VALUE (*s) = new_spec;
36a117a5
MM
1261 return 1;
1262 }
1263
1264 return 0;
75650646
MM
1265}
1266
a723baf1
MM
1267/* Compare an entry in the local specializations hash table P1 (which
1268 is really a pointer to a TREE_LIST) with P2 (which is really a
1269 DECL). */
1270
1271static int
1272eq_local_specializations (const void *p1, const void *p2)
1273{
1274 return TREE_VALUE ((tree) p1) == (tree) p2;
1275}
1276
69f794a7
MM
1277/* Hash P1, an entry in the local specializations table. */
1278
1279static hashval_t
1280hash_local_specialization (const void* p1)
1281{
1282 return htab_hash_pointer (TREE_VALUE ((tree) p1));
1283}
1284
6dfbb909
MM
1285/* Like register_specialization, but for local declarations. We are
1286 registering SPEC, an instantiation of TMPL. */
9188c363 1287
414ea4aa 1288static void
3a978d72 1289register_local_specialization (tree spec, tree tmpl)
9188c363 1290{
6dfbb909
MM
1291 void **slot;
1292
c8094d83 1293 slot = htab_find_slot_with_hash (local_specializations, tmpl,
69f794a7 1294 htab_hash_pointer (tmpl), INSERT);
a723baf1 1295 *slot = build_tree_list (spec, tmpl);
9188c363
MM
1296}
1297
e1467ff2
MM
1298/* Print the list of candidate FNS in an error message. */
1299
104bf76a 1300void
3a978d72 1301print_candidates (tree fns)
e1467ff2
MM
1302{
1303 tree fn;
1304
d8e178a0 1305 const char *str = "candidates are:";
e1467ff2
MM
1306
1307 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
1308 {
d6479fe7
MM
1309 tree f;
1310
1311 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
dee15844 1312 error ("%s %+#D", str, OVL_CURRENT (f));
e1467ff2
MM
1313 str = " ";
1314 }
1315}
1316
75650646 1317/* Returns the template (one of the functions given by TEMPLATE_ID)
e1467ff2 1318 which can be specialized to match the indicated DECL with the
03017874
MM
1319 explicit template args given in TEMPLATE_ID. The DECL may be
1320 NULL_TREE if none is available. In that case, the functions in
1321 TEMPLATE_ID are non-members.
1322
838dfd8a 1323 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
03017874
MM
1324 specialization of a member template.
1325
5fe7b654
GB
1326 The TEMPLATE_COUNT is the number of references to qualifying
1327 template classes that appeared in the name of the function. See
1328 check_explicit_specialization for a more accurate description.
1329
29a1da1c
MM
1330 TSK indicates what kind of template declaration (if any) is being
1331 declared. TSK_TEMPLATE indicates that the declaration given by
1332 DECL, though a FUNCTION_DECL, has template parameters, and is
1333 therefore a template function.
1334
03017874
MM
1335 The template args (those explicitly specified and those deduced)
1336 are output in a newly created vector *TARGS_OUT.
1337
1338 If it is impossible to determine the result, an error message is
bf8f3f93 1339 issued. The error_mark_node is returned to indicate failure. */
75650646 1340
e9659ab0 1341static tree
c8094d83 1342determine_specialization (tree template_id,
0cbd7506
MS
1343 tree decl,
1344 tree* targs_out,
5fe7b654 1345 int need_member_template,
29a1da1c
MM
1346 int template_count,
1347 tmpl_spec_kind tsk)
386b8a85 1348{
03017874
MM
1349 tree fns;
1350 tree targs;
1351 tree explicit_targs;
1352 tree candidates = NULL_TREE;
7ca383e6
MM
1353 /* A TREE_LIST of templates of which DECL may be a specialization.
1354 The TREE_VALUE of each node is a TEMPLATE_DECL. The
1355 corresponding TREE_PURPOSE is the set of template arguments that,
1356 when used to instantiate the template, would produce a function
1357 with the signature of DECL. */
03017874 1358 tree templates = NULL_TREE;
5fe7b654
GB
1359 int header_count;
1360 struct cp_binding_level *b;
386b8a85 1361
e1467ff2
MM
1362 *targs_out = NULL_TREE;
1363
728da672 1364 if (template_id == error_mark_node || decl == error_mark_node)
f2e48b67
BK
1365 return error_mark_node;
1366
1367 fns = TREE_OPERAND (template_id, 0);
03017874 1368 explicit_targs = TREE_OPERAND (template_id, 1);
f2e48b67 1369
27fafc8d
JM
1370 if (fns == error_mark_node)
1371 return error_mark_node;
1372
c6002625 1373 /* Check for baselinks. */
91e490ab 1374 if (BASELINK_P (fns))
da15dae6 1375 fns = BASELINK_FUNCTIONS (fns);
386b8a85 1376
91e490ab
MM
1377 if (!is_overloaded_fn (fns))
1378 {
0f51ccfc 1379 error ("%qD is not a function template", fns);
91e490ab
MM
1380 return error_mark_node;
1381 }
1382
5fe7b654
GB
1383 /* Count the number of template headers specified for this
1384 specialization. */
1385 header_count = 0;
c8094d83 1386 for (b = current_binding_level;
98f99d7f 1387 b->kind == sk_template_parms;
5fe7b654
GB
1388 b = b->level_chain)
1389 ++header_count;
1390
2c73f9f5 1391 for (; fns; fns = OVL_NEXT (fns))
386b8a85 1392 {
9dfce8fd 1393 tree fn = OVL_CURRENT (fns);
03017874
MM
1394
1395 if (TREE_CODE (fn) == TEMPLATE_DECL)
d955f6ea
KL
1396 {
1397 tree decl_arg_types;
0da99d4e 1398 tree fn_arg_types;
d955f6ea
KL
1399
1400 /* DECL might be a specialization of FN. */
1401
1402 /* Adjust the type of DECL in case FN is a static member. */
1403 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
c8094d83 1404 if (DECL_STATIC_FUNCTION_P (fn)
d955f6ea
KL
1405 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1406 decl_arg_types = TREE_CHAIN (decl_arg_types);
1407
1408 /* Check that the number of function parameters matches.
1409 For example,
1410 template <class T> void f(int i = 0);
1411 template <> void f<int>();
0e339752 1412 The specialization f<int> is invalid but is not caught
d955f6ea
KL
1413 by get_bindings below. */
1414
0da99d4e
GB
1415 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
1416 if (list_length (fn_arg_types) != list_length (decl_arg_types))
1417 continue;
1418
1419 /* For a non-static member function, we need to make sure that
1420 the const qualification is the same. This can be done by
1421 checking the 'this' in the argument list. */
1422 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
c8094d83 1423 && !same_type_p (TREE_VALUE (fn_arg_types),
0da99d4e 1424 TREE_VALUE (decl_arg_types)))
d955f6ea
KL
1425 continue;
1426
5fe7b654
GB
1427 /* In case of explicit specialization, we need to check if
1428 the number of template headers appearing in the specialization
1429 is correct. This is usually done in check_explicit_specialization,
1430 but the check done there cannot be exhaustive when specializing
1431 member functions. Consider the following code:
1432
1433 template <> void A<int>::f(int);
1434 template <> template <> void A<int>::f(int);
1435
1436 Assuming that A<int> is not itself an explicit specialization
1437 already, the first line specializes "f" which is a non-template
1438 member function, whilst the second line specializes "f" which
1439 is a template member function. So both lines are syntactically
1440 correct, and check_explicit_specialization does not reject
1441 them.
c8094d83 1442
5fe7b654
GB
1443 Here, we can do better, as we are matching the specialization
1444 against the declarations. We count the number of template
1445 headers, and we check if they match TEMPLATE_COUNT + 1
1446 (TEMPLATE_COUNT is the number of qualifying template classes,
1447 plus there must be another header for the member template
1448 itself).
c8094d83 1449
5fe7b654
GB
1450 Notice that if header_count is zero, this is not a
1451 specialization but rather a template instantiation, so there
1452 is no check we can perform here. */
1453 if (header_count && header_count != template_count + 1)
1454 continue;
1455
98f99d7f
MM
1456 /* Check that the number of template arguments at the
1457 innermost level for DECL is the same as for FN. */
1458 if (current_binding_level->kind == sk_template_parms
1459 && !current_binding_level->explicit_spec_p
1460 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
3db45ab5 1461 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
916b63c3 1462 (current_template_parms))))
98f99d7f 1463 continue;
c8094d83 1464
29a1da1c
MM
1465 /* Function templates cannot be specializations; there are
1466 no partial specializations of functions. Therefore, if
1467 the type of DECL does not match FN, there is no
1468 match. */
1469 if (tsk == tsk_template)
1470 {
1471 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1472 decl_arg_types))
1473 candidates = tree_cons (NULL_TREE, fn, candidates);
1474 continue;
1475 }
1476
d955f6ea
KL
1477 /* See whether this function might be a specialization of this
1478 template. */
a34d3336 1479 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
d955f6ea
KL
1480
1481 if (!targs)
1482 /* We cannot deduce template arguments that when used to
1483 specialize TMPL will produce DECL. */
1484 continue;
1485
1486 /* Save this template, and the arguments deduced. */
1487 templates = tree_cons (targs, fn, templates);
1488 }
03017874
MM
1489 else if (need_member_template)
1490 /* FN is an ordinary member function, and we need a
1491 specialization of a member template. */
d955f6ea 1492 ;
03017874
MM
1493 else if (TREE_CODE (fn) != FUNCTION_DECL)
1494 /* We can get IDENTIFIER_NODEs here in certain erroneous
1495 cases. */
d955f6ea 1496 ;
03017874
MM
1497 else if (!DECL_FUNCTION_MEMBER_P (fn))
1498 /* This is just an ordinary non-member function. Nothing can
1499 be a specialization of that. */
d955f6ea 1500 ;
3b82c249
KL
1501 else if (DECL_ARTIFICIAL (fn))
1502 /* Cannot specialize functions that are created implicitly. */
d955f6ea 1503 ;
75650646 1504 else
03017874
MM
1505 {
1506 tree decl_arg_types;
386b8a85 1507
03017874
MM
1508 /* This is an ordinary member function. However, since
1509 we're here, we can assume it's enclosing class is a
1510 template class. For example,
c8094d83 1511
03017874
MM
1512 template <typename T> struct S { void f(); };
1513 template <> void S<int>::f() {}
1514
1515 Here, S<int>::f is a non-template, but S<int> is a
1516 template class. If FN has the same type as DECL, we
1517 might be in business. */
f5d47abd
KL
1518
1519 if (!DECL_TEMPLATE_INFO (fn))
1520 /* Its enclosing class is an explicit specialization
1521 of a template class. This is not a candidate. */
1522 continue;
1523
03017874
MM
1524 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1525 TREE_TYPE (TREE_TYPE (fn))))
1526 /* The return types differ. */
1527 continue;
1528
1529 /* Adjust the type of DECL in case FN is a static member. */
1530 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
c8094d83 1531 if (DECL_STATIC_FUNCTION_P (fn)
03017874
MM
1532 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1533 decl_arg_types = TREE_CHAIN (decl_arg_types);
1534
c8094d83 1535 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
03017874
MM
1536 decl_arg_types))
1537 /* They match! */
1538 candidates = tree_cons (NULL_TREE, fn, candidates);
03017874 1539 }
386b8a85 1540 }
03017874 1541
bf8f3f93 1542 if (templates && TREE_CHAIN (templates))
386b8a85 1543 {
03017874 1544 /* We have:
c8094d83 1545
03017874
MM
1546 [temp.expl.spec]
1547
1548 It is possible for a specialization with a given function
1549 signature to be instantiated from more than one function
1550 template. In such cases, explicit specification of the
1551 template arguments must be used to uniquely identify the
1552 function template specialization being specialized.
1553
1554 Note that here, there's no suggestion that we're supposed to
1555 determine which of the candidate templates is most
1556 specialized. However, we, also have:
1557
1558 [temp.func.order]
1559
1560 Partial ordering of overloaded function template
1561 declarations is used in the following contexts to select
1562 the function template to which a function template
c8094d83 1563 specialization refers:
03017874 1564
0cbd7506 1565 -- when an explicit specialization refers to a function
c8094d83 1566 template.
03017874
MM
1567
1568 So, we do use the partial ordering rules, at least for now.
0e339752 1569 This extension can only serve to make invalid programs valid,
03017874
MM
1570 so it's safe. And, there is strong anecdotal evidence that
1571 the committee intended the partial ordering rules to apply;
1572 the EDG front-end has that behavior, and John Spicer claims
1573 that the committee simply forgot to delete the wording in
1574 [temp.expl.spec]. */
7ca383e6
MM
1575 tree tmpl = most_specialized_instantiation (templates);
1576 if (tmpl != error_mark_node)
1577 {
1578 templates = tmpl;
1579 TREE_CHAIN (templates) = NULL_TREE;
1580 }
e1467ff2
MM
1581 }
1582
03017874 1583 if (templates == NULL_TREE && candidates == NULL_TREE)
e1467ff2 1584 {
dee15844
JM
1585 error ("template-id %qD for %q+D does not match any template "
1586 "declaration", template_id, decl);
03017874 1587 return error_mark_node;
386b8a85 1588 }
03017874 1589 else if ((templates && TREE_CHAIN (templates))
bf8f3f93
MM
1590 || (candidates && TREE_CHAIN (candidates))
1591 || (templates && candidates))
386b8a85 1592 {
dee15844
JM
1593 error ("ambiguous template specialization %qD for %q+D",
1594 template_id, decl);
bf8f3f93
MM
1595 chainon (candidates, templates);
1596 print_candidates (candidates);
03017874 1597 return error_mark_node;
386b8a85
JM
1598 }
1599
c6002625 1600 /* We have one, and exactly one, match. */
03017874
MM
1601 if (candidates)
1602 {
29a1da1c
MM
1603 tree fn = TREE_VALUE (candidates);
1604 /* DECL is a re-declaration of a template function. */
1605 if (TREE_CODE (fn) == TEMPLATE_DECL)
1606 return fn;
03017874
MM
1607 /* It was a specialization of an ordinary member function in a
1608 template class. */
29a1da1c
MM
1609 *targs_out = copy_node (DECL_TI_ARGS (fn));
1610 return DECL_TI_TEMPLATE (fn);
03017874
MM
1611 }
1612
1613 /* It was a specialization of a template. */
17aec3eb 1614 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
03017874
MM
1615 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1616 {
1617 *targs_out = copy_node (targs);
c8094d83 1618 SET_TMPL_ARGS_LEVEL (*targs_out,
03017874
MM
1619 TMPL_ARGS_DEPTH (*targs_out),
1620 TREE_PURPOSE (templates));
1621 }
1622 else
1623 *targs_out = TREE_PURPOSE (templates);
e1467ff2 1624 return TREE_VALUE (templates);
8d08fdba 1625}
8afa707f
MM
1626
1627/* Returns a chain of parameter types, exactly like the SPEC_TYPES,
1628 but with the default argument values filled in from those in the
1629 TMPL_TYPES. */
c8094d83 1630
8afa707f 1631static tree
3a978d72
NN
1632copy_default_args_to_explicit_spec_1 (tree spec_types,
1633 tree tmpl_types)
8afa707f
MM
1634{
1635 tree new_spec_types;
1636
1637 if (!spec_types)
1638 return NULL_TREE;
1639
1640 if (spec_types == void_list_node)
1641 return void_list_node;
1642
1643 /* Substitute into the rest of the list. */
1644 new_spec_types =
1645 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
1646 TREE_CHAIN (tmpl_types));
c8094d83 1647
8afa707f
MM
1648 /* Add the default argument for this parameter. */
1649 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
1650 TREE_VALUE (spec_types),
1651 new_spec_types);
1652}
1653
1654/* DECL is an explicit specialization. Replicate default arguments
1655 from the template it specializes. (That way, code like:
1656
1657 template <class T> void f(T = 3);
1658 template <> void f(double);
c8094d83 1659 void g () { f (); }
8afa707f
MM
1660
1661 works, as required.) An alternative approach would be to look up
1662 the correct default arguments at the call-site, but this approach
1663 is consistent with how implicit instantiations are handled. */
1664
1665static void
3a978d72 1666copy_default_args_to_explicit_spec (tree decl)
8afa707f
MM
1667{
1668 tree tmpl;
1669 tree spec_types;
1670 tree tmpl_types;
1671 tree new_spec_types;
1672 tree old_type;
1673 tree new_type;
1674 tree t;
08c2df0f
NS
1675 tree object_type = NULL_TREE;
1676 tree in_charge = NULL_TREE;
e0fff4b3 1677 tree vtt = NULL_TREE;
8afa707f
MM
1678
1679 /* See if there's anything we need to do. */
1680 tmpl = DECL_TI_TEMPLATE (decl);
1681 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
1682 for (t = tmpl_types; t; t = TREE_CHAIN (t))
1683 if (TREE_PURPOSE (t))
1684 break;
1685 if (!t)
1686 return;
1687
1688 old_type = TREE_TYPE (decl);
1689 spec_types = TYPE_ARG_TYPES (old_type);
c8094d83 1690
8afa707f
MM
1691 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1692 {
08c2df0f 1693 /* Remove the this pointer, but remember the object's type for
0cbd7506 1694 CV quals. */
08c2df0f 1695 object_type = TREE_TYPE (TREE_VALUE (spec_types));
8afa707f
MM
1696 spec_types = TREE_CHAIN (spec_types);
1697 tmpl_types = TREE_CHAIN (tmpl_types);
c8094d83 1698
8afa707f 1699 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
0cbd7506
MS
1700 {
1701 /* DECL may contain more parameters than TMPL due to the extra
1702 in-charge parameter in constructors and destructors. */
1703 in_charge = spec_types;
08c2df0f
NS
1704 spec_types = TREE_CHAIN (spec_types);
1705 }
e0fff4b3
JM
1706 if (DECL_HAS_VTT_PARM_P (decl))
1707 {
1708 vtt = spec_types;
1709 spec_types = TREE_CHAIN (spec_types);
1710 }
8afa707f
MM
1711 }
1712
1713 /* Compute the merged default arguments. */
c8094d83 1714 new_spec_types =
8afa707f
MM
1715 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
1716
08c2df0f
NS
1717 /* Compute the new FUNCTION_TYPE. */
1718 if (object_type)
8afa707f 1719 {
e0fff4b3 1720 if (vtt)
0cbd7506
MS
1721 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
1722 TREE_VALUE (vtt),
1723 new_spec_types);
e0fff4b3 1724
08c2df0f 1725 if (in_charge)
0cbd7506
MS
1726 /* Put the in-charge parameter back. */
1727 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
1728 TREE_VALUE (in_charge),
1729 new_spec_types);
08c2df0f 1730
43dc123f
MM
1731 new_type = build_method_type_directly (object_type,
1732 TREE_TYPE (old_type),
1733 new_spec_types);
8afa707f 1734 }
8afa707f
MM
1735 else
1736 new_type = build_function_type (TREE_TYPE (old_type),
08c2df0f 1737 new_spec_types);
e9525111
MM
1738 new_type = cp_build_type_attribute_variant (new_type,
1739 TYPE_ATTRIBUTES (old_type));
8afa707f
MM
1740 new_type = build_exception_variant (new_type,
1741 TYPE_RAISES_EXCEPTIONS (old_type));
1742 TREE_TYPE (decl) = new_type;
1743}
1744
386b8a85 1745/* Check to see if the function just declared, as indicated in
75650646
MM
1746 DECLARATOR, and in DECL, is a specialization of a function
1747 template. We may also discover that the declaration is an explicit
1748 instantiation at this point.
1749
e1467ff2 1750 Returns DECL, or an equivalent declaration that should be used
03017874
MM
1751 instead if all goes well. Issues an error message if something is
1752 amiss. Returns error_mark_node if the error is not easily
1753 recoverable.
c8094d83
MS
1754
1755 FLAGS is a bitmask consisting of the following flags:
75650646 1756
75650646
MM
1757 2: The function has a definition.
1758 4: The function is a friend.
75650646
MM
1759
1760 The TEMPLATE_COUNT is the number of references to qualifying
1761 template classes that appeared in the name of the function. For
1762 example, in
1763
1764 template <class T> struct S { void f(); };
1765 void S<int>::f();
c8094d83 1766
75650646
MM
1767 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1768 classes are not counted in the TEMPLATE_COUNT, so that in
1769
1770 template <class T> struct S {};
1771 template <> struct S<int> { void f(); }
36a117a5 1772 template <> void S<int>::f();
75650646
MM
1773
1774 the TEMPLATE_COUNT would be 0. (Note that this declaration is
0e339752 1775 invalid; there should be no template <>.)
75650646
MM
1776
1777 If the function is a specialization, it is marked as such via
1778 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
c8094d83 1779 is set up correctly, and it is added to the list of specializations
75650646 1780 for that template. */
386b8a85 1781
e1467ff2 1782tree
c8094d83 1783check_explicit_specialization (tree declarator,
0cbd7506
MS
1784 tree decl,
1785 int template_count,
1786 int flags)
386b8a85 1787{
75650646
MM
1788 int have_def = flags & 2;
1789 int is_friend = flags & 4;
1790 int specialization = 0;
e1467ff2 1791 int explicit_instantiation = 0;
fd4de5ff 1792 int member_specialization = 0;
75650646
MM
1793 tree ctype = DECL_CLASS_CONTEXT (decl);
1794 tree dname = DECL_NAME (decl);
74b846e0 1795 tmpl_spec_kind tsk;
386b8a85 1796
f65b7de3
GB
1797 if (is_friend)
1798 {
1799 if (!processing_specialization)
1800 tsk = tsk_none;
1801 else
1802 tsk = tsk_excessive_parms;
1803 }
1804 else
1805 tsk = current_tmpl_spec_kind (template_count);
75650646 1806
74b846e0
MM
1807 switch (tsk)
1808 {
1809 case tsk_none:
c8094d83 1810 if (processing_specialization)
75650646 1811 {
fd4de5ff
MM
1812 specialization = 1;
1813 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
75650646 1814 }
74b846e0 1815 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
386b8a85 1816 {
74b846e0
MM
1817 if (is_friend)
1818 /* This could be something like:
75650646 1819
74b846e0
MM
1820 template <class T> void f(T);
1821 class S { friend void f<>(int); } */
1822 specialization = 1;
1823 else
1824 {
1825 /* This case handles bogus declarations like template <>
1826 template <class T> void f<int>(); */
1827
0f51ccfc 1828 error ("template-id %qD in declaration of primary template",
0cbd7506 1829 declarator);
74b846e0
MM
1830 return decl;
1831 }
1832 }
1833 break;
1834
1835 case tsk_invalid_member_spec:
1836 /* The error has already been reported in
1837 check_specialization_scope. */
1838 return error_mark_node;
1839
1840 case tsk_invalid_expl_inst:
33bd39a2 1841 error ("template parameter list used in explicit instantiation");
74b846e0
MM
1842
1843 /* Fall through. */
1844
1845 case tsk_expl_inst:
fd4de5ff 1846 if (have_def)
33bd39a2 1847 error ("definition provided for explicit instantiation");
c8094d83 1848
fd4de5ff 1849 explicit_instantiation = 1;
74b846e0 1850 break;
fd4de5ff 1851
74b846e0 1852 case tsk_excessive_parms:
f03adc6b
GB
1853 case tsk_insufficient_parms:
1854 if (tsk == tsk_excessive_parms)
0cbd7506 1855 error ("too many template parameter lists in declaration of %qD",
f03adc6b
GB
1856 decl);
1857 else if (template_header_count)
0f51ccfc 1858 error("too few template parameter lists in declaration of %qD", decl);
f03adc6b 1859 else
0f51ccfc 1860 error("explicit specialization of %qD must be introduced by "
9e637a26 1861 "%<template <>%>", decl);
75650646 1862
74b846e0
MM
1863 /* Fall through. */
1864 case tsk_expl_spec:
1865 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1866 if (ctype)
1867 member_specialization = 1;
1868 else
1869 specialization = 1;
1870 break;
74b846e0
MM
1871
1872 case tsk_template:
1873 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
c6f2ed0d 1874 {
fd4de5ff
MM
1875 /* This case handles bogus declarations like template <>
1876 template <class T> void f<int>(); */
75650646 1877
655dc6ee 1878 if (uses_template_parms (declarator))
0f51ccfc 1879 error ("function template partial specialization %qD "
e1e93ad8 1880 "is not allowed", declarator);
655dc6ee 1881 else
0f51ccfc 1882 error ("template-id %qD in declaration of primary template",
0cbd7506 1883 declarator);
fd4de5ff 1884 return decl;
386b8a85 1885 }
74b846e0
MM
1886
1887 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
1888 /* This is a specialization of a member template, without
1889 specialization the containing class. Something like:
1890
1891 template <class T> struct S {
c8094d83 1892 template <class U> void f (U);
0cbd7506 1893 };
74b846e0 1894 template <> template <class U> void S<int>::f(U) {}
c8094d83 1895
74b846e0
MM
1896 That's a specialization -- but of the entire template. */
1897 specialization = 1;
1898 break;
1899
1900 default:
315fb5db 1901 gcc_unreachable ();
75650646 1902 }
386b8a85 1903
670960ac
JM
1904 if (specialization || member_specialization)
1905 {
1906 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1907 for (; t; t = TREE_CHAIN (t))
1908 if (TREE_PURPOSE (t))
1909 {
33bd39a2 1910 pedwarn
8251199e 1911 ("default argument specified in explicit specialization");
670960ac
JM
1912 break;
1913 }
1914 }
1915
e1467ff2 1916 if (specialization || member_specialization || explicit_instantiation)
75650646
MM
1917 {
1918 tree tmpl = NULL_TREE;
1919 tree targs = NULL_TREE;
75650646
MM
1920
1921 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
386b8a85
JM
1922 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1923 {
1924 tree fns;
1925
50bc768d 1926 gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
2f54a1db 1927 if (ctype)
386b8a85 1928 fns = dname;
2f54a1db
GB
1929 else
1930 {
1931 /* If there is no class context, the explicit instantiation
0cbd7506 1932 must be at namespace scope. */
50bc768d 1933 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2f54a1db
GB
1934
1935 /* Find the namespace binding, using the declaration
0cbd7506 1936 context. */
ddf74938 1937 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
664a90c0 1938 false, true);
4230cec2
NS
1939 if (!fns || !is_overloaded_fn (fns))
1940 {
1941 error ("%qD is not a template function", dname);
1942 fns = error_mark_node;
1943 }
664a90c0
JM
1944 else
1945 {
1946 tree fn = OVL_CURRENT (fns);
ddf74938 1947 if (!is_associated_namespace (CP_DECL_CONTEXT (decl),
08d295c5
JM
1948 CP_DECL_CONTEXT (fn)))
1949 error ("%qD is not declared in %qD",
1950 decl, current_namespace);
664a90c0 1951 }
2f54a1db 1952 }
386b8a85 1953
2f54a1db 1954 declarator = lookup_template_function (fns, NULL_TREE);
386b8a85
JM
1955 }
1956
f2e48b67
BK
1957 if (declarator == error_mark_node)
1958 return error_mark_node;
1959
75650646
MM
1960 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1961 {
8ec2ac34 1962 if (!explicit_instantiation)
0e339752 1963 /* A specialization in class scope. This is invalid,
6c30752f
MM
1964 but the error will already have been flagged by
1965 check_specialization_scope. */
1966 return error_mark_node;
8ec2ac34 1967 else
b370501f 1968 {
0e339752 1969 /* It's not valid to write an explicit instantiation in
b370501f 1970 class scope, e.g.:
8ec2ac34 1971
0cbd7506 1972 class C { template void f(); }
8ec2ac34 1973
b370501f
KG
1974 This case is caught by the parser. However, on
1975 something like:
c8094d83 1976
b370501f 1977 template class C { void f(); };
8ec2ac34 1978
0e339752 1979 (which is invalid) we can get here. The error will be
b370501f
KG
1980 issued later. */
1981 ;
1982 }
8ec2ac34 1983
e1467ff2 1984 return decl;
75650646 1985 }
c8094d83 1986 else if (ctype != NULL_TREE
75650646
MM
1987 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1988 IDENTIFIER_NODE))
386b8a85 1989 {
75650646
MM
1990 /* Find the list of functions in ctype that have the same
1991 name as the declared function. */
1992 tree name = TREE_OPERAND (declarator, 0);
03017874
MM
1993 tree fns = NULL_TREE;
1994 int idx;
1995
8ba658ee 1996 if (constructor_name_p (name, ctype))
386b8a85 1997 {
75650646 1998 int is_constructor = DECL_CONSTRUCTOR_P (decl);
c8094d83 1999
75650646 2000 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
9f4faeae 2001 : !CLASSTYPE_DESTRUCTORS (ctype))
75650646
MM
2002 {
2003 /* From [temp.expl.spec]:
c8094d83 2004
75650646
MM
2005 If such an explicit specialization for the member
2006 of a class template names an implicitly-declared
2007 special member function (clause _special_), the
c8094d83 2008 program is ill-formed.
e1467ff2
MM
2009
2010 Similar language is found in [temp.explicit]. */
33bd39a2 2011 error ("specialization of implicitly-declared special member function");
03017874 2012 return error_mark_node;
75650646 2013 }
386b8a85 2014
42da2fd8 2015 name = is_constructor ? ctor_identifier : dtor_identifier;
75650646 2016 }
42da2fd8 2017
421844e7 2018 if (!DECL_CONV_FN_P (decl))
03017874
MM
2019 {
2020 idx = lookup_fnfields_1 (ctype, name);
2021 if (idx >= 0)
aaaa46d2 2022 fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
03017874
MM
2023 }
2024 else
2025 {
d4e6fecb 2026 VEC(tree,gc) *methods;
aaaa46d2 2027 tree ovl;
03017874
MM
2028
2029 /* For a type-conversion operator, we cannot do a
2030 name-based lookup. We might be looking for `operator
2031 int' which will be a specialization of `operator T'.
2032 So, we find *all* the conversion operators, and then
2033 select from them. */
2034 fns = NULL_TREE;
2035
2036 methods = CLASSTYPE_METHOD_VEC (ctype);
2037 if (methods)
5dd236e2 2038 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
9ba5ff0f 2039 VEC_iterate (tree, methods, idx, ovl);
aaaa46d2 2040 ++idx)
03017874 2041 {
aaaa46d2 2042 if (!DECL_CONV_FN_P (OVL_CURRENT (ovl)))
03017874
MM
2043 /* There are no more conversion functions. */
2044 break;
2045
2046 /* Glue all these conversion functions together
2047 with those we already have. */
2048 for (; ovl; ovl = OVL_NEXT (ovl))
2049 fns = ovl_cons (OVL_CURRENT (ovl), fns);
2050 }
2051 }
c8094d83
MS
2052
2053 if (fns == NULL_TREE)
386b8a85 2054 {
0f51ccfc 2055 error ("no member function %qD declared in %qT", name, ctype);
03017874 2056 return error_mark_node;
386b8a85
JM
2057 }
2058 else
2059 TREE_OPERAND (declarator, 0) = fns;
2060 }
c8094d83 2061
e1467ff2
MM
2062 /* Figure out what exactly is being specialized at this point.
2063 Note that for an explicit instantiation, even one for a
38e01259 2064 member function, we cannot tell apriori whether the
e1467ff2 2065 instantiation is for a member template, or just a member
36a117a5
MM
2066 function of a template class. Even if a member template is
2067 being instantiated, the member template arguments may be
2068 elided if they can be deduced from the rest of the
2069 declaration. */
e1467ff2 2070 tmpl = determine_specialization (declarator, decl,
c8094d83 2071 &targs,
5fe7b654 2072 member_specialization,
29a1da1c
MM
2073 template_count,
2074 tsk);
c8094d83 2075
03017874
MM
2076 if (!tmpl || tmpl == error_mark_node)
2077 /* We couldn't figure out what this declaration was
2078 specializing. */
2079 return error_mark_node;
2080 else
386b8a85 2081 {
25aab5d0 2082 tree gen_tmpl = most_general_template (tmpl);
36a117a5 2083
e1467ff2
MM
2084 if (explicit_instantiation)
2085 {
03d0f4af 2086 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
c8094d83 2087 is done by do_decl_instantiation later. */
25aab5d0
MM
2088
2089 int arg_depth = TMPL_ARGS_DEPTH (targs);
2090 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2091
2092 if (arg_depth > parm_depth)
2093 {
2094 /* If TMPL is not the most general template (for
2095 example, if TMPL is a friend template that is
2096 injected into namespace scope), then there will
dc957d14 2097 be too many levels of TARGS. Remove some of them
25aab5d0
MM
2098 here. */
2099 int i;
2100 tree new_targs;
2101
f31c0a32 2102 new_targs = make_tree_vec (parm_depth);
25aab5d0
MM
2103 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
2104 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
2105 = TREE_VEC_ELT (targs, i);
2106 targs = new_targs;
2107 }
c8094d83 2108
3e4a3562 2109 return instantiate_template (tmpl, targs, tf_error);
e1467ff2 2110 }
74b846e0 2111
6c07f448
KL
2112 /* If we thought that the DECL was a member function, but it
2113 turns out to be specializing a static member function,
4546865e 2114 make DECL a static member function as well. */
6c07f448
KL
2115 if (DECL_STATIC_FUNCTION_P (tmpl)
2116 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
4546865e 2117 revert_static_member_fn (decl);
6c07f448 2118
f9a7ae04 2119 /* If this is a specialization of a member template of a
29a1da1c
MM
2120 template class, we want to return the TEMPLATE_DECL, not
2121 the specialization of it. */
74b846e0
MM
2122 if (tsk == tsk_template)
2123 {
2124 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
f9a7ae04 2125 DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
b190f239
NS
2126 if (have_def)
2127 {
f31686a3
RH
2128 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
2129 DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
2130 = DECL_SOURCE_LOCATION (decl);
08167d1c
AO
2131 /* We want to use the argument list specified in the
2132 definition, not in the original declaration. */
2133 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
2134 = DECL_ARGUMENTS (decl);
b190f239 2135 }
74b846e0
MM
2136 return tmpl;
2137 }
2138
36a117a5 2139 /* Set up the DECL_TEMPLATE_INFO for DECL. */
e1b3e07d 2140 DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
36a117a5 2141
8afa707f
MM
2142 /* Inherit default function arguments from the template
2143 DECL is specializing. */
2144 copy_default_args_to_explicit_spec (decl);
2145
c750255c
MM
2146 /* This specialization has the same protection as the
2147 template it specializes. */
2148 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
2149 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
b9e75696 2150
9c12301f
MM
2151 /* If DECL is a friend declaration, declared using an
2152 unqualified name, the namespace associated with DECL may
2153 have been set incorrectly. For example, in:
3db45ab5
MS
2154
2155 template <typename T> void f(T);
2156 namespace N {
2157 struct S { friend void f<int>(int); }
2158 }
2159
2160 we will have set the DECL_CONTEXT for the friend
2161 declaration to N, rather than to the global namespace. */
9c12301f
MM
2162 if (DECL_NAMESPACE_SCOPE_P (decl))
2163 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
8d039470 2164
386b8a85 2165 if (is_friend && !have_def)
36a117a5
MM
2166 /* This is not really a declaration of a specialization.
2167 It's just the name of an instantiation. But, it's not
2168 a request for an instantiation, either. */
fbf1c34b 2169 SET_DECL_IMPLICIT_INSTANTIATION (decl);
08511114
KL
2170 else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
2171 /* This is indeed a specialization. In case of constructors
2172 and destructors, we need in-charge and not-in-charge
2173 versions in V3 ABI. */
2174 clone_function_decl (decl, /*update_method_vec_p=*/0);
75650646 2175
36a117a5
MM
2176 /* Register this specialization so that we can find it
2177 again. */
d63d5d0c 2178 decl = register_specialization (decl, gen_tmpl, targs, is_friend);
386b8a85
JM
2179 }
2180 }
c8094d83 2181
e1467ff2 2182 return decl;
386b8a85 2183}
75650646 2184
75650646
MM
2185/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
2186 parameters. These are represented in the same format used for
2187 DECL_TEMPLATE_PARMS. */
2188
50bc768d
NS
2189int
2190comp_template_parms (tree parms1, tree parms2)
75650646
MM
2191{
2192 tree p1;
2193 tree p2;
2194
2195 if (parms1 == parms2)
2196 return 1;
2197
c8094d83 2198 for (p1 = parms1, p2 = parms2;
75650646
MM
2199 p1 != NULL_TREE && p2 != NULL_TREE;
2200 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
2201 {
2202 tree t1 = TREE_VALUE (p1);
2203 tree t2 = TREE_VALUE (p2);
2204 int i;
2205
50bc768d
NS
2206 gcc_assert (TREE_CODE (t1) == TREE_VEC);
2207 gcc_assert (TREE_CODE (t2) == TREE_VEC);
75650646
MM
2208
2209 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
2210 return 0;
2211
c8094d83 2212 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
75650646 2213 {
0f67a82f
LM
2214 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
2215 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
42b304f1 2216
0f67a82f
LM
2217 /* If either of the template parameters are invalid, assume
2218 they match for the sake of error recovery. */
2219 if (parm1 == error_mark_node || parm2 == error_mark_node)
2220 return 1;
75650646
MM
2221
2222 if (TREE_CODE (parm1) != TREE_CODE (parm2))
2223 return 0;
2224
2225 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
2226 continue;
3bfdc719 2227 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
75650646
MM
2228 return 0;
2229 }
2230 }
2231
2232 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
2233 /* One set of parameters has more parameters lists than the
2234 other. */
2235 return 0;
2236
2237 return 1;
2238}
2239
f3400fe2
JM
2240/* Complain if DECL shadows a template parameter.
2241
2242 [temp.local]: A template-parameter shall not be redeclared within its
2243 scope (including nested scopes). */
2244
2245void
3a978d72 2246check_template_shadow (tree decl)
f3400fe2 2247{
8f032717
MM
2248 tree olddecl;
2249
b5d9b9ab
MM
2250 /* If we're not in a template, we can't possibly shadow a template
2251 parameter. */
2252 if (!current_template_parms)
2253 return;
2254
2255 /* Figure out what we're shadowing. */
8f032717
MM
2256 if (TREE_CODE (decl) == OVERLOAD)
2257 decl = OVL_CURRENT (decl);
90ea9897 2258 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
f3400fe2 2259
b5d9b9ab
MM
2260 /* If there's no previous binding for this name, we're not shadowing
2261 anything, let alone a template parameter. */
2262 if (!olddecl)
2263 return;
2264
2265 /* If we're not shadowing a template parameter, we're done. Note
2266 that OLDDECL might be an OVERLOAD (or perhaps even an
2267 ERROR_MARK), so we can't just blithely assume it to be a _DECL
2268 node. */
2f939d94 2269 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
b5d9b9ab
MM
2270 return;
2271
2272 /* We check for decl != olddecl to avoid bogus errors for using a
2273 name inside a class. We check TPFI to avoid duplicate errors for
2274 inline member templates. */
c8094d83 2275 if (decl == olddecl
b5d9b9ab
MM
2276 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
2277 return;
2278
dee15844
JM
2279 error ("declaration of %q+#D", decl);
2280 error (" shadows template parm %q+#D", olddecl);
f3400fe2 2281}
22a7be53 2282
f3400fe2 2283/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
f84b4be9
JM
2284 ORIG_LEVEL, DECL, and TYPE. */
2285
2286static tree
c8094d83 2287build_template_parm_index (int index,
0cbd7506
MS
2288 int level,
2289 int orig_level,
2290 tree decl,
2291 tree type)
f84b4be9
JM
2292{
2293 tree t = make_node (TEMPLATE_PARM_INDEX);
2294 TEMPLATE_PARM_IDX (t) = index;
2295 TEMPLATE_PARM_LEVEL (t) = level;
2296 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
2297 TEMPLATE_PARM_DECL (t) = decl;
2298 TREE_TYPE (t) = type;
3e4a3562 2299 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
6de9cd9a 2300 TREE_INVARIANT (t) = TREE_INVARIANT (decl);
3e4a3562 2301 TREE_READONLY (t) = TREE_READONLY (decl);
f84b4be9
JM
2302
2303 return t;
2304}
2305
f84b4be9 2306/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
93cdc044 2307 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
f84b4be9
JM
2308 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
2309 new one is created. */
2310
c8094d83 2311static tree
3a978d72 2312reduce_template_parm_level (tree index, tree type, int levels)
f84b4be9
JM
2313{
2314 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
2315 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
93cdc044 2316 != TEMPLATE_PARM_LEVEL (index) - levels))
f84b4be9 2317 {
3e4a3562
NS
2318 tree orig_decl = TEMPLATE_PARM_DECL (index);
2319 tree decl, t;
c8094d83 2320
3e4a3562
NS
2321 decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
2322 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
6de9cd9a 2323 TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
3e4a3562
NS
2324 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
2325 DECL_ARTIFICIAL (decl) = 1;
2326 SET_DECL_TEMPLATE_PARM_P (decl);
c8094d83 2327
3e4a3562 2328 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
93cdc044 2329 TEMPLATE_PARM_LEVEL (index) - levels,
f84b4be9
JM
2330 TEMPLATE_PARM_ORIG_LEVEL (index),
2331 decl, type);
2332 TEMPLATE_PARM_DESCENDANTS (index) = t;
cae40af6 2333
820cc88f
DB
2334 /* Template template parameters need this. */
2335 if (TREE_CODE (decl) != CONST_DECL)
2336 DECL_TEMPLATE_PARMS (decl)
2337 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
f84b4be9
JM
2338 }
2339
2340 return TEMPLATE_PARM_DESCENDANTS (index);
2341}
2342
a14de1af 2343/* Process information from new template parameter PARM and append it to the
058b15c1
MM
2344 LIST being built. This new parameter is a non-type parameter iff
2345 IS_NON_TYPE is true. */
e92cc029 2346
8d08fdba 2347tree
a14de1af 2348process_template_parm (tree list, tree parm, bool is_non_type)
8d08fdba 2349{
8d08fdba 2350 tree decl = 0;
a292b002 2351 tree defval;
0f67a82f 2352 tree err_parm_list;
d47e3adf 2353 int idx = 0;
f84b4be9 2354
50bc768d 2355 gcc_assert (TREE_CODE (parm) == TREE_LIST);
a292b002 2356 defval = TREE_PURPOSE (parm);
5566b478
MS
2357
2358 if (list)
2359 {
d47e3adf
LM
2360 tree p = tree_last (list);
2361
0f67a82f 2362 if (p && TREE_VALUE (p) != error_mark_node)
d47e3adf
LM
2363 {
2364 p = TREE_VALUE (p);
2365 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
2366 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
2367 else
2368 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
2369 }
5566b478 2370
5566b478
MS
2371 ++idx;
2372 }
2373 else
2374 idx = 0;
2375
058b15c1 2376 if (is_non_type)
8d08fdba 2377 {
058b15c1
MM
2378 parm = TREE_VALUE (parm);
2379
833aa4c4 2380 SET_DECL_TEMPLATE_PARM_P (parm);
d490621d 2381
620188c9 2382 if (TREE_TYPE (parm) == error_mark_node)
0f67a82f
LM
2383 {
2384 err_parm_list = build_tree_list (defval, parm);
2385 TREE_VALUE (err_parm_list) = error_mark_node;
2386 return chainon (list, err_parm_list);
2387 }
620188c9
VR
2388 else
2389 {
2390 /* [temp.param]
d490621d 2391
620188c9
VR
2392 The top-level cv-qualifiers on the template-parameter are
2393 ignored when determining its type. */
2394 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
2395 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
0f67a82f
LM
2396 {
2397 err_parm_list = build_tree_list (defval, parm);
2398 TREE_VALUE (err_parm_list) = error_mark_node;
2399 return chainon (list, err_parm_list);
2400 }
620188c9 2401 }
d490621d 2402
8d08fdba 2403 /* A template parameter is not modifiable. */
6de9cd9a
DN
2404 TREE_CONSTANT (parm) = 1;
2405 TREE_INVARIANT (parm) = 1;
2406 TREE_READONLY (parm) = 1;
8d08fdba 2407 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
6de9cd9a
DN
2408 TREE_CONSTANT (decl) = 1;
2409 TREE_INVARIANT (decl) = 1;
2410 TREE_READONLY (decl) = 1;
c8094d83 2411 DECL_INITIAL (parm) = DECL_INITIAL (decl)
f84b4be9
JM
2412 = build_template_parm_index (idx, processing_template_decl,
2413 processing_template_decl,
2414 decl, TREE_TYPE (parm));
8d08fdba
MS
2415 }
2416 else
2417 {
73b0fce8 2418 tree t;
058b15c1 2419 parm = TREE_VALUE (TREE_VALUE (parm));
c8094d83 2420
73b0fce8
KL
2421 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
2422 {
33848bb0 2423 t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
c8094d83 2424 /* This is for distinguishing between real templates and template
73b0fce8
KL
2425 template parameters */
2426 TREE_TYPE (parm) = t;
2427 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
2428 decl = parm;
2429 }
2430 else
2431 {
33848bb0 2432 t = make_aggr_type (TEMPLATE_TYPE_PARM);
f4f206f4 2433 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
73b0fce8
KL
2434 decl = build_decl (TYPE_DECL, parm, t);
2435 }
c8094d83 2436
d2e5ee5c
MS
2437 TYPE_NAME (t) = decl;
2438 TYPE_STUB_DECL (t) = decl;
a292b002 2439 parm = decl;
f84b4be9 2440 TEMPLATE_TYPE_PARM_INDEX (t)
c8094d83 2441 = build_template_parm_index (idx, processing_template_decl,
f84b4be9
JM
2442 processing_template_decl,
2443 decl, TREE_TYPE (parm));
8d08fdba 2444 }
c727aa5e 2445 DECL_ARTIFICIAL (decl) = 1;
cd9f6678 2446 SET_DECL_TEMPLATE_PARM_P (decl);
8d08fdba 2447 pushdecl (decl);
a292b002 2448 parm = build_tree_list (defval, parm);
8d08fdba
MS
2449 return chainon (list, parm);
2450}
2451
2452/* The end of a template parameter list has been reached. Process the
2453 tree list into a parameter vector, converting each parameter into a more
2454 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
2455 as PARM_DECLs. */
2456
2457tree
3a978d72 2458end_template_parm_list (tree parms)
8d08fdba 2459{
5566b478 2460 int nparms;
9471d3e2 2461 tree parm, next;
5566b478
MS
2462 tree saved_parmlist = make_tree_vec (list_length (parms));
2463
5566b478 2464 current_template_parms
4890c2f4 2465 = tree_cons (size_int (processing_template_decl),
5566b478 2466 saved_parmlist, current_template_parms);
8d08fdba 2467
9471d3e2
NS
2468 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
2469 {
2470 next = TREE_CHAIN (parm);
2471 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
2472 TREE_CHAIN (parm) = NULL_TREE;
2473 }
a292b002 2474
67ffc812
MM
2475 --processing_template_parmlist;
2476
8d08fdba
MS
2477 return saved_parmlist;
2478}
2479
5566b478
MS
2480/* end_template_decl is called after a template declaration is seen. */
2481
8d08fdba 2482void
3a978d72 2483end_template_decl (void)
8d08fdba 2484{
386b8a85
JM
2485 reset_specialization ();
2486
5156628f 2487 if (! processing_template_decl)
73aad9b9
JM
2488 return;
2489
5566b478 2490 /* This matches the pushlevel in begin_template_parm_list. */
74b846e0 2491 finish_scope ();
8d08fdba 2492
5566b478
MS
2493 --processing_template_decl;
2494 current_template_parms = TREE_CHAIN (current_template_parms);
5566b478 2495}
8d08fdba 2496
36a117a5
MM
2497/* Given a template argument vector containing the template PARMS.
2498 The innermost PARMS are given first. */
9a3b49ac 2499
b5791fdc 2500static tree
3a978d72 2501current_template_args (void)
5566b478 2502{
36a117a5 2503 tree header;
b370501f 2504 tree args = NULL_TREE;
36a117a5 2505 int length = TMPL_PARMS_DEPTH (current_template_parms);
98c1c668
JM
2506 int l = length;
2507
36a117a5
MM
2508 /* If there is only one level of template parameters, we do not
2509 create a TREE_VEC of TREE_VECs. Instead, we return a single
2510 TREE_VEC containing the arguments. */
2511 if (length > 1)
2512 args = make_tree_vec (length);
2513
2514 for (header = current_template_parms; header; header = TREE_CHAIN (header))
8d08fdba 2515 {
5566b478 2516 tree a = copy_node (TREE_VALUE (header));
36a117a5
MM
2517 int i;
2518
5566b478 2519 TREE_TYPE (a) = NULL_TREE;
36a117a5 2520 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
5566b478 2521 {
98c1c668
JM
2522 tree t = TREE_VEC_ELT (a, i);
2523
36a117a5 2524 /* T will be a list if we are called from within a
98c1c668
JM
2525 begin/end_template_parm_list pair, but a vector directly
2526 if within a begin/end_member_template_processing pair. */
c8094d83 2527 if (TREE_CODE (t) == TREE_LIST)
98c1c668
JM
2528 {
2529 t = TREE_VALUE (t);
c8094d83 2530
0f67a82f
LM
2531 if (t != error_mark_node)
2532 {
2533 if (TREE_CODE (t) == TYPE_DECL
2534 || TREE_CODE (t) == TEMPLATE_DECL)
2535 t = TREE_TYPE (t);
2536 else
2537 t = DECL_INITIAL (t);
2538 }
2539
36a117a5 2540 TREE_VEC_ELT (a, i) = t;
98c1c668 2541 }
5566b478 2542 }
36a117a5
MM
2543
2544 if (length > 1)
2545 TREE_VEC_ELT (args, --l) = a;
2546 else
2547 args = a;
8d08fdba
MS
2548 }
2549
9a3b49ac
MS
2550 return args;
2551}
75650646 2552
e1467ff2 2553/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
c7222c02 2554 template PARMS. If MEMBER_TEMPLATE_P is true, the new template is
ee81147e 2555 a member template. Used by push_template_decl below. */
e1467ff2 2556
75650646 2557static tree
c7222c02 2558build_template_decl (tree decl, tree parms, bool member_template_p)
75650646
MM
2559{
2560 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
2561 DECL_TEMPLATE_PARMS (tmpl) = parms;
2562 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
c7222c02 2563 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
75650646
MM
2564 if (DECL_LANG_SPECIFIC (decl))
2565 {
8f032717
MM
2566 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2567 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
c8460010 2568 DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
11f98788 2569 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
1f6e1acc
AS
2570 DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
2571 if (DECL_OVERLOADED_OPERATOR_P (decl))
c8094d83 2572 SET_OVERLOADED_OPERATOR_CODE (tmpl,
1f6e1acc 2573 DECL_OVERLOADED_OPERATOR_P (decl));
75650646
MM
2574 }
2575
2576 return tmpl;
2577}
2578
050367a3
MM
2579struct template_parm_data
2580{
6c30752f
MM
2581 /* The level of the template parameters we are currently
2582 processing. */
050367a3 2583 int level;
6c30752f
MM
2584
2585 /* The index of the specialization argument we are currently
2586 processing. */
2587 int current_arg;
2588
2589 /* An array whose size is the number of template parameters. The
838dfd8a 2590 elements are nonzero if the parameter has been used in any one
6c30752f 2591 of the arguments processed so far. */
050367a3 2592 int* parms;
6c30752f
MM
2593
2594 /* An array whose size is the number of template arguments. The
838dfd8a 2595 elements are nonzero if the argument makes use of template
6c30752f
MM
2596 parameters of this level. */
2597 int* arg_uses_template_parms;
050367a3
MM
2598};
2599
2600/* Subroutine of push_template_decl used to see if each template
2601 parameter in a partial specialization is used in the explicit
2602 argument list. If T is of the LEVEL given in DATA (which is
2603 treated as a template_parm_data*), then DATA->PARMS is marked
2604 appropriately. */
2605
2606static int
3a978d72 2607mark_template_parm (tree t, void* data)
050367a3
MM
2608{
2609 int level;
2610 int idx;
2611 struct template_parm_data* tpd = (struct template_parm_data*) data;
2612
2613 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2614 {
2615 level = TEMPLATE_PARM_LEVEL (t);
2616 idx = TEMPLATE_PARM_IDX (t);
2617 }
2618 else
2619 {
2620 level = TEMPLATE_TYPE_LEVEL (t);
2621 idx = TEMPLATE_TYPE_IDX (t);
2622 }
2623
2624 if (level == tpd->level)
6c30752f
MM
2625 {
2626 tpd->parms[idx] = 1;
2627 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2628 }
050367a3
MM
2629
2630 /* Return zero so that for_each_template_parm will continue the
2631 traversal of the tree; we want to mark *every* template parm. */
2632 return 0;
2633}
2634
6c30752f
MM
2635/* Process the partial specialization DECL. */
2636
e9659ab0 2637static tree
3a978d72 2638process_partial_specialization (tree decl)
6c30752f
MM
2639{
2640 tree type = TREE_TYPE (decl);
2641 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2642 tree specargs = CLASSTYPE_TI_ARGS (type);
f9a7ae04 2643 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
6c30752f
MM
2644 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2645 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2646 int nargs = TREE_VEC_LENGTH (inner_args);
2647 int ntparms = TREE_VEC_LENGTH (inner_parms);
2648 int i;
2649 int did_error_intro = 0;
6c30752f
MM
2650 struct template_parm_data tpd;
2651 struct template_parm_data tpd2;
2652
6c30752f
MM
2653 /* We check that each of the template parameters given in the
2654 partial specialization is used in the argument list to the
2655 specialization. For example:
2656
2657 template <class T> struct S;
2658 template <class T> struct S<T*>;
2659
2660 The second declaration is OK because `T*' uses the template
2661 parameter T, whereas
2662
2663 template <class T> struct S<int>;
2664
2665 is no good. Even trickier is:
2666
2667 template <class T>
2668 struct S1
2669 {
2670 template <class U>
2671 struct S2;
2672 template <class U>
2673 struct S2<T>;
2674 };
2675
0e339752 2676 The S2<T> declaration is actually invalid; it is a
c8094d83 2677 full-specialization. Of course,
6c30752f
MM
2678
2679 template <class U>
2680 struct S2<T (*)(U)>;
2681
2682 or some such would have been OK. */
2683 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
67f5655f 2684 tpd.parms = (int *) alloca (sizeof (int) * ntparms);
fad205ff 2685 memset (tpd.parms, 0, sizeof (int) * ntparms);
6c30752f 2686
67f5655f 2687 tpd.arg_uses_template_parms = (int *) alloca (sizeof (int) * nargs);
fad205ff 2688 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
6c30752f
MM
2689 for (i = 0; i < nargs; ++i)
2690 {
2691 tpd.current_arg = i;
2692 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2693 &mark_template_parm,
4f2c9d7e
MM
2694 &tpd,
2695 NULL);
6c30752f
MM
2696 }
2697 for (i = 0; i < ntparms; ++i)
2698 if (tpd.parms[i] == 0)
2699 {
2700 /* One of the template parms was not used in the
f9a7ae04 2701 specialization. */
6c30752f
MM
2702 if (!did_error_intro)
2703 {
33bd39a2 2704 error ("template parameters not used in partial specialization:");
6c30752f
MM
2705 did_error_intro = 1;
2706 }
2707
0f51ccfc 2708 error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
6c30752f
MM
2709 }
2710
2711 /* [temp.class.spec]
2712
2713 The argument list of the specialization shall not be identical to
2714 the implicit argument list of the primary template. */
c8094d83
MS
2715 if (comp_template_args
2716 (inner_args,
f9a7ae04
MM
2717 INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
2718 (maintmpl)))))
0f51ccfc 2719 error ("partial specialization %qT does not specialize any template arguments", type);
6c30752f
MM
2720
2721 /* [temp.class.spec]
2722
2723 A partially specialized non-type argument expression shall not
2724 involve template parameters of the partial specialization except
2725 when the argument expression is a simple identifier.
2726
2727 The type of a template parameter corresponding to a specialized
2728 non-type argument shall not be dependent on a parameter of the
2729 specialization. */
50bc768d 2730 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
6c30752f
MM
2731 tpd2.parms = 0;
2732 for (i = 0; i < nargs; ++i)
2733 {
2734 tree arg = TREE_VEC_ELT (inner_args, i);
2735 if (/* These first two lines are the `non-type' bit. */
2f939d94 2736 !TYPE_P (arg)
6c30752f
MM
2737 && TREE_CODE (arg) != TEMPLATE_DECL
2738 /* This next line is the `argument expression is not just a
2739 simple identifier' condition and also the `specialized
2740 non-type argument' bit. */
2741 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2742 {
2743 if (tpd.arg_uses_template_parms[i])
0f51ccfc 2744 error ("template argument %qE involves template parameter(s)", arg);
c8094d83 2745 else
6c30752f
MM
2746 {
2747 /* Look at the corresponding template parameter,
2748 marking which template parameters its type depends
2749 upon. */
c8094d83
MS
2750 tree type =
2751 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
6c30752f
MM
2752 i)));
2753
2754 if (!tpd2.parms)
2755 {
2756 /* We haven't yet initialized TPD2. Do so now. */
c8094d83 2757 tpd2.arg_uses_template_parms
67f5655f 2758 = (int *) alloca (sizeof (int) * nargs);
104bf76a 2759 /* The number of parameters here is the number in the
76a83782
RH
2760 main template, which, as checked in the assertion
2761 above, is NARGS. */
67f5655f 2762 tpd2.parms = (int *) alloca (sizeof (int) * nargs);
c8094d83 2763 tpd2.level =
6c30752f
MM
2764 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2765 }
2766
104bf76a 2767 /* Mark the template parameters. But this time, we're
6c30752f
MM
2768 looking for the template parameters of the main
2769 template, not in the specialization. */
2770 tpd2.current_arg = i;
2771 tpd2.arg_uses_template_parms[i] = 0;
fad205ff 2772 memset (tpd2.parms, 0, sizeof (int) * nargs);
6c30752f
MM
2773 for_each_template_parm (type,
2774 &mark_template_parm,
4f2c9d7e
MM
2775 &tpd2,
2776 NULL);
c8094d83 2777
6c30752f
MM
2778 if (tpd2.arg_uses_template_parms [i])
2779 {
2780 /* The type depended on some template parameters.
2781 If they are fully specialized in the
2782 specialization, that's OK. */
2783 int j;
2784 for (j = 0; j < nargs; ++j)
2785 if (tpd2.parms[j] != 0
2786 && tpd.arg_uses_template_parms [j])
2787 {
0f51ccfc 2788 error ("type %qT of template argument %qE depends "
0cbd7506
MS
2789 "on template parameter(s)",
2790 type,
2791 arg);
6c30752f
MM
2792 break;
2793 }
2794 }
2795 }
2796 }
2797 }
2798
c8094d83 2799 if (retrieve_specialization (maintmpl, specargs,
c7222c02 2800 /*class_specializations_p=*/true))
6c30752f
MM
2801 /* We've already got this specialization. */
2802 return decl;
2803
d8b64f80 2804 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
916b63c3 2805 = tree_cons (specargs, inner_parms,
e1b3e07d 2806 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
6c30752f
MM
2807 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2808 return decl;
2809}
2810
6ba89f8e
MM
2811/* Check that a template declaration's use of default arguments is not
2812 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
838dfd8a
KH
2813 nonzero if DECL is the thing declared by a primary template.
2814 IS_PARTIAL is nonzero if DECL is a partial specialization. */
6ba89f8e
MM
2815
2816static void
3a978d72 2817check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
6ba89f8e 2818{
d8e178a0 2819 const char *msg;
66191c20
MM
2820 int last_level_to_check;
2821 tree parm_level;
6ba89f8e 2822
c8094d83 2823 /* [temp.param]
6ba89f8e
MM
2824
2825 A default template-argument shall not be specified in a
2826 function template declaration or a function template definition, nor
2827 in the template-parameter-list of the definition of a member of a
2828 class template. */
2829
4f1c5b7d 2830 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
e0942dcd
MM
2831 /* You can't have a function template declaration in a local
2832 scope, nor you can you define a member of a class template in a
2833 local scope. */
2834 return;
2835
6ba89f8e
MM
2836 if (current_class_type
2837 && !TYPE_BEING_DEFINED (current_class_type)
3d7de1fa 2838 && DECL_LANG_SPECIFIC (decl)
5937a6f9
MM
2839 /* If this is either a friend defined in the scope of the class
2840 or a member function. */
6df5158a
NS
2841 && (DECL_FUNCTION_MEMBER_P (decl)
2842 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
2843 : DECL_FRIEND_CONTEXT (decl)
2844 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
2845 : false)
5937a6f9
MM
2846 /* And, if it was a member function, it really was defined in
2847 the scope of the class. */
6df5158a
NS
2848 && (!DECL_FUNCTION_MEMBER_P (decl)
2849 || DECL_INITIALIZED_IN_CLASS_P (decl)))
6ba89f8e 2850 /* We already checked these parameters when the template was
5937a6f9
MM
2851 declared, so there's no need to do it again now. This function
2852 was defined in class scope, but we're processing it's body now
2853 that the class is complete. */
6ba89f8e
MM
2854 return;
2855
66191c20 2856 /* [temp.param]
c8094d83 2857
66191c20
MM
2858 If a template-parameter has a default template-argument, all
2859 subsequent template-parameters shall have a default
2860 template-argument supplied. */
2861 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2862 {
2863 tree inner_parms = TREE_VALUE (parm_level);
2864 int ntparms = TREE_VEC_LENGTH (inner_parms);
c8094d83 2865 int seen_def_arg_p = 0;
66191c20
MM
2866 int i;
2867
c8094d83 2868 for (i = 0; i < ntparms; ++i)
66191c20
MM
2869 {
2870 tree parm = TREE_VEC_ELT (inner_parms, i);
42b304f1
LM
2871
2872 if (parm == error_mark_node)
2873 continue;
2874
66191c20
MM
2875 if (TREE_PURPOSE (parm))
2876 seen_def_arg_p = 1;
2877 else if (seen_def_arg_p)
2878 {
0f51ccfc 2879 error ("no default argument for %qD", TREE_VALUE (parm));
66191c20
MM
2880 /* For better subsequent error-recovery, we indicate that
2881 there should have been a default argument. */
2882 TREE_PURPOSE (parm) = error_mark_node;
2883 }
2884 }
2885 }
2886
6ba89f8e
MM
2887 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2888 /* For an ordinary class template, default template arguments are
2889 allowed at the innermost level, e.g.:
0cbd7506 2890 template <class T = int>
6ba89f8e
MM
2891 struct S {};
2892 but, in a partial specialization, they're not allowed even
2893 there, as we have in [temp.class.spec]:
c8094d83 2894
6ba89f8e 2895 The template parameter list of a specialization shall not
c8094d83 2896 contain default template argument values.
6ba89f8e
MM
2897
2898 So, for a partial specialization, or for a function template,
2899 we look at all of them. */
2900 ;
2901 else
2902 /* But, for a primary class template that is not a partial
2903 specialization we look at all template parameters except the
2904 innermost ones. */
2905 parms = TREE_CHAIN (parms);
2906
2907 /* Figure out what error message to issue. */
2908 if (TREE_CODE (decl) == FUNCTION_DECL)
8653a2c3 2909 msg = "default template arguments may not be used in function templates";
6ba89f8e 2910 else if (is_partial)
8653a2c3 2911 msg = "default template arguments may not be used in partial specializations";
6ba89f8e 2912 else
0f51ccfc 2913 msg = "default argument for template parameter for class enclosing %qD";
6ba89f8e
MM
2914
2915 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2916 /* If we're inside a class definition, there's no need to
104bf76a 2917 examine the parameters to the class itself. On the one
6ba89f8e 2918 hand, they will be checked when the class is defined, and,
0e339752 2919 on the other, default arguments are valid in things like:
0cbd7506
MS
2920 template <class T = double>
2921 struct S { template <class U> void f(U); };
6ba89f8e
MM
2922 Here the default argument for `S' has no bearing on the
2923 declaration of `f'. */
2924 last_level_to_check = template_class_depth (current_class_type) + 1;
2925 else
2926 /* Check everything. */
2927 last_level_to_check = 0;
2928
c8094d83
MS
2929 for (parm_level = parms;
2930 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
66191c20 2931 parm_level = TREE_CHAIN (parm_level))
6ba89f8e 2932 {
66191c20
MM
2933 tree inner_parms = TREE_VALUE (parm_level);
2934 int i;
2935 int ntparms;
6ba89f8e
MM
2936
2937 ntparms = TREE_VEC_LENGTH (inner_parms);
c8094d83 2938 for (i = 0; i < ntparms; ++i)
42b304f1
LM
2939 {
2940 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
2941 continue;
6ba89f8e 2942
42b304f1
LM
2943 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2944 {
2945 if (msg)
2946 {
2947 error (msg, decl);
2948 msg = 0;
2949 }
2950
2951 /* Clear out the default argument so that we are not
2952 confused later. */
2953 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2954 }
2955 }
6ba89f8e
MM
2956
2957 /* At this point, if we're still interested in issuing messages,
2958 they must apply to classes surrounding the object declared. */
2959 if (msg)
c8094d83 2960 msg = "default argument for template parameter for class enclosing %qD";
6ba89f8e
MM
2961 }
2962}
2963
5dd236e2
NS
2964/* Worker for push_template_decl_real, called via
2965 for_each_template_parm. DATA is really an int, indicating the
2966 level of the parameters we are interested in. If T is a template
838dfd8a 2967 parameter of that level, return nonzero. */
5dd236e2
NS
2968
2969static int
3a978d72 2970template_parm_this_level_p (tree t, void* data)
5dd236e2 2971{
6e04241f 2972 int this_level = *(int *)data;
5dd236e2
NS
2973 int level;
2974
2975 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2976 level = TEMPLATE_PARM_LEVEL (t);
2977 else
2978 level = TEMPLATE_TYPE_LEVEL (t);
2979 return level == this_level;
2980}
2981
3ac3d9ea 2982/* Creates a TEMPLATE_DECL for the indicated DECL using the template
f84b4be9
JM
2983 parameters given by current_template_args, or reuses a
2984 previously existing one, if appropriate. Returns the DECL, or an
c8094d83 2985 equivalent one, if it is replaced via a call to duplicate_decls.
6757edfe 2986
d63d5d0c 2987 If IS_FRIEND is true, DECL is a friend declaration. */
3ac3d9ea
MM
2988
2989tree
d63d5d0c 2990push_template_decl_real (tree decl, bool is_friend)
9a3b49ac
MS
2991{
2992 tree tmpl;
f84b4be9 2993 tree args;
9a3b49ac 2994 tree info;
f84b4be9
JM
2995 tree ctx;
2996 int primary;
6ba89f8e 2997 int is_partial;
cfe507be 2998 int new_template_p = 0;
c7222c02
MM
2999 /* True if the template is a member template, in the sense of
3000 [temp.mem]. */
3001 bool member_template_p = false;
6ba89f8e 3002
c0694c4b
MM
3003 if (decl == error_mark_node)
3004 return decl;
3005
6ba89f8e 3006 /* See if this is a partial specialization. */
9188c363 3007 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
6ba89f8e 3008 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
370af2d5 3009 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
6757edfe 3010
d63d5d0c
ILT
3011 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
3012 is_friend = true;
f84b4be9
JM
3013
3014 if (is_friend)
3015 /* For a friend, we want the context of the friend function, not
3016 the type of which it is a friend. */
3017 ctx = DECL_CONTEXT (decl);
4f1c5b7d
MM
3018 else if (CP_DECL_CONTEXT (decl)
3019 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
f84b4be9
JM
3020 /* In the case of a virtual function, we want the class in which
3021 it is defined. */
4f1c5b7d 3022 ctx = CP_DECL_CONTEXT (decl);
f84b4be9 3023 else
dc957d14 3024 /* Otherwise, if we're currently defining some class, the DECL
f84b4be9 3025 is assumed to be a member of the class. */
9188c363 3026 ctx = current_scope ();
f84b4be9 3027
2c73f9f5
ML
3028 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
3029 ctx = NULL_TREE;
3030
3031 if (!DECL_CONTEXT (decl))
cb0dbb9a 3032 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 3033
6ba89f8e 3034 /* See if this is a primary template. */
c9cbfca6
JM
3035 if (is_friend && ctx)
3036 /* A friend template that specifies a class context, i.e.
3037 template <typename T> friend void A<T>::f();
3038 is not primary. */
3039 primary = 0;
3040 else
3041 primary = template_parm_scope_p ();
9a3b49ac 3042
83566abf
JM
3043 if (primary)
3044 {
c7222c02
MM
3045 if (DECL_CLASS_SCOPE_P (decl))
3046 member_template_p = true;
2f1b1731
MM
3047 if (TREE_CODE (decl) == TYPE_DECL
3048 && ANON_AGGRNAME_P (DECL_NAME (decl)))
33bd39a2 3049 error ("template class without a name");
717a7d5d 3050 else if (TREE_CODE (decl) == FUNCTION_DECL)
4b0d3cbe 3051 {
717a7d5d
MM
3052 if (DECL_DESTRUCTOR_P (decl))
3053 {
3054 /* [temp.mem]
c8094d83 3055
0cbd7506 3056 A destructor shall not be a member template. */
0f51ccfc 3057 error ("destructor %qD declared as member template", decl);
717a7d5d
MM
3058 return error_mark_node;
3059 }
3060 if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
3061 && (!TYPE_ARG_TYPES (TREE_TYPE (decl))
3062 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
3063 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
3064 || (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
3065 == void_list_node)))
3066 {
c8094d83 3067 /* [basic.stc.dynamic.allocation]
717a7d5d 3068
0cbd7506 3069 An allocation function can be a function
717a7d5d
MM
3070 template. ... Template allocation functions shall
3071 have two or more parameters. */
0f51ccfc 3072 error ("invalid template declaration of %qD", decl);
7c60008e 3073 return error_mark_node;
717a7d5d 3074 }
4b0d3cbe 3075 }
8ca4bf25
MM
3076 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
3077 && CLASS_TYPE_P (TREE_TYPE (decl)))
2aaf816d
JM
3078 /* OK */;
3079 else
11325dcd 3080 {
0f51ccfc 3081 error ("template declaration of %q#D", decl);
11325dcd
KL
3082 return error_mark_node;
3083 }
83566abf
JM
3084 }
3085
6ba89f8e
MM
3086 /* Check to see that the rules regarding the use of default
3087 arguments are not being violated. */
c8094d83 3088 check_default_tmpl_args (decl, current_template_parms,
6ba89f8e 3089 primary, is_partial);
73aad9b9 3090
6ba89f8e
MM
3091 if (is_partial)
3092 return process_partial_specialization (decl);
d32789d8 3093
9a3b49ac
MS
3094 args = current_template_args ();
3095
c8094d83 3096 if (!ctx
f84b4be9 3097 || TREE_CODE (ctx) == FUNCTION_DECL
a723baf1 3098 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
f84b4be9 3099 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
8d08fdba 3100 {
75650646 3101 if (DECL_LANG_SPECIFIC (decl)
f84b4be9
JM
3102 && DECL_TEMPLATE_INFO (decl)
3103 && DECL_TI_TEMPLATE (decl))
3104 tmpl = DECL_TI_TEMPLATE (decl);
1c10870d
AS
3105 /* If DECL is a TYPE_DECL for a class-template, then there won't
3106 be DECL_LANG_SPECIFIC. The information equivalent to
3107 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
c8094d83 3108 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
1c10870d
AS
3109 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3110 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3111 {
3112 /* Since a template declaration already existed for this
3113 class-type, we must be redeclaring it here. Make sure
0e339752 3114 that the redeclaration is valid. */
1c10870d
AS
3115 redeclare_class_template (TREE_TYPE (decl),
3116 current_template_parms);
3117 /* We don't need to create a new TEMPLATE_DECL; just use the
3118 one we already had. */
3119 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
3120 }
f84b4be9 3121 else
786b5245 3122 {
c7222c02
MM
3123 tmpl = build_template_decl (decl, current_template_parms,
3124 member_template_p);
cfe507be
MM
3125 new_template_p = 1;
3126
f84b4be9
JM
3127 if (DECL_LANG_SPECIFIC (decl)
3128 && DECL_TEMPLATE_SPECIALIZATION (decl))
3129 {
3130 /* A specialization of a member template of a template
c6002625 3131 class. */
f84b4be9
JM
3132 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3133 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
3134 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3135 }
786b5245 3136 }
8d08fdba
MS
3137 }
3138 else
3139 {
e1a5ccf7 3140 tree a, t, current, parms;
ba4f4e5d 3141 int i;
6633d636 3142
6b9ab5cc
MM
3143 if (TREE_CODE (decl) == TYPE_DECL)
3144 {
ed44da02
MM
3145 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
3146 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
3147 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
3148 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
3149 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6b9ab5cc
MM
3150 else
3151 {
0f51ccfc 3152 error ("%qD does not declare a template type", decl);
6b9ab5cc
MM
3153 return decl;
3154 }
3155 }
f3368a90 3156 else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
c91a56d2 3157 {
0f51ccfc 3158 error ("template definition of non-template %q#D", decl);
3ac3d9ea 3159 return decl;
c91a56d2 3160 }
8d08fdba 3161 else
5566b478 3162 tmpl = DECL_TI_TEMPLATE (decl);
c8094d83 3163
c353b8e3 3164 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
c8094d83 3165 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
c353b8e3 3166 && DECL_TEMPLATE_SPECIALIZATION (decl)
c7222c02 3167 && DECL_MEMBER_TEMPLATE_P (tmpl))
98c1c668 3168 {
e1a5ccf7
JM
3169 tree new_tmpl;
3170
3171 /* The declaration is a specialization of a member
3172 template, declared outside the class. Therefore, the
3173 innermost template arguments will be NULL, so we
3174 replace them with the arguments determined by the
3175 earlier call to check_explicit_specialization. */
3176 args = DECL_TI_ARGS (decl);
3177
c8094d83 3178 new_tmpl
c7222c02
MM
3179 = build_template_decl (decl, current_template_parms,
3180 member_template_p);
e1a5ccf7
JM
3181 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
3182 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
3183 DECL_TI_TEMPLATE (decl) = new_tmpl;
3184 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
c8094d83 3185 DECL_TEMPLATE_INFO (new_tmpl)
e1b3e07d 3186 = tree_cons (tmpl, args, NULL_TREE);
e1a5ccf7 3187
c8094d83
MS
3188 register_specialization (new_tmpl,
3189 most_general_template (tmpl),
d63d5d0c
ILT
3190 args,
3191 is_friend);
e1a5ccf7 3192 return decl;
98c1c668 3193 }
98c1c668 3194
e1a5ccf7 3195 /* Make sure the template headers we got make sense. */
6633d636 3196
e1a5ccf7
JM
3197 parms = DECL_TEMPLATE_PARMS (tmpl);
3198 i = TMPL_PARMS_DEPTH (parms);
3199 if (TMPL_ARGS_DEPTH (args) != i)
98c1c668 3200 {
0f51ccfc 3201 error ("expected %d levels of template parms for %q#D, got %d",
0cbd7506 3202 i, decl, TMPL_ARGS_DEPTH (args));
98c1c668 3203 }
e1a5ccf7
JM
3204 else
3205 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
3206 {
3207 a = TMPL_ARGS_LEVEL (args, i);
3208 t = INNERMOST_TEMPLATE_PARMS (parms);
3209
3210 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
3211 {
3212 if (current == decl)
0f51ccfc 3213 error ("got %d template parameters for %q#D",
0cbd7506 3214 TREE_VEC_LENGTH (a), decl);
e1a5ccf7 3215 else
0f51ccfc 3216 error ("got %d template parameters for %q#T",
0cbd7506 3217 TREE_VEC_LENGTH (a), current);
33bd39a2 3218 error (" but %d required", TREE_VEC_LENGTH (t));
f1cc0515 3219 return error_mark_node;
e1a5ccf7 3220 }
98c1c668 3221
e1a5ccf7 3222 /* Perhaps we should also check that the parms are used in the
0cbd7506 3223 appropriate qualifying scopes in the declarator? */
6633d636 3224
e1a5ccf7
JM
3225 if (current == decl)
3226 current = ctx;
3227 else
3228 current = TYPE_CONTEXT (current);
3229 }
5566b478 3230 }
8d08fdba 3231
5566b478
MS
3232 DECL_TEMPLATE_RESULT (tmpl) = decl;
3233 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 3234
36a117a5
MM
3235 /* Push template declarations for global functions and types. Note
3236 that we do not try to push a global template friend declared in a
3237 template class; such a thing may well depend on the template
39c01e4c 3238 parameters of the class. */
c8094d83 3239 if (new_template_p && !ctx
36a117a5 3240 && !(is_friend && template_class_depth (current_class_type) > 0))
c6f9f83b 3241 {
d63d5d0c 3242 tmpl = pushdecl_namespace_level (tmpl, is_friend);
c6f9f83b
KL
3243 if (tmpl == error_mark_node)
3244 return error_mark_node;
bd3d082e
KL
3245
3246 /* Hide template friend classes that haven't been declared yet. */
3247 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
3248 {
3249 DECL_ANTICIPATED (tmpl) = 1;
3250 DECL_FRIEND_P (tmpl) = 1;
3251 }
c6f9f83b 3252 }
8d08fdba 3253
5566b478 3254 if (primary)
5dd236e2
NS
3255 {
3256 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
3257 if (DECL_CONV_FN_P (tmpl))
3258 {
6e04241f
GS
3259 int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3260
5dd236e2
NS
3261 /* It is a conversion operator. See if the type converted to
3262 depends on innermost template operands. */
c8094d83 3263
d43f603d
KL
3264 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
3265 depth))
5dd236e2
NS
3266 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
3267 }
3268 }
5566b478 3269
a692ad2e 3270 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
28ba38e3
KL
3271 back to its most general template. If TMPL is a specialization,
3272 ARGS may only have the innermost set of arguments. Add the missing
3273 argument levels if necessary. */
3274 if (DECL_TEMPLATE_INFO (tmpl))
3275 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
3276
e1b3e07d 3277 info = tree_cons (tmpl, args, NULL_TREE);
5566b478 3278
9188c363 3279 if (DECL_IMPLICIT_TYPEDEF_P (decl))
8d08fdba 3280 {
ed44da02
MM
3281 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
3282 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
b60ecc04
MM
3283 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
3284 /* Don't change the name if we've already set it up. */
3285 && !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
75650646 3286 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
51c184be 3287 }
2aaf816d 3288 else if (DECL_LANG_SPECIFIC (decl))
5566b478 3289 DECL_TEMPLATE_INFO (decl) = info;
3ac3d9ea
MM
3290
3291 return DECL_TEMPLATE_RESULT (tmpl);
8d08fdba
MS
3292}
3293
6757edfe 3294tree
3a978d72 3295push_template_decl (tree decl)
6757edfe 3296{
d63d5d0c 3297 return push_template_decl_real (decl, false);
6757edfe
MM
3298}
3299
3300/* Called when a class template TYPE is redeclared with the indicated
3301 template PARMS, e.g.:
7fe6899f
MM
3302
3303 template <class T> struct S;
3304 template <class T> struct S {}; */
3305
c8094d83 3306void
3a978d72 3307redeclare_class_template (tree type, tree parms)
7fe6899f 3308{
3d7de1fa 3309 tree tmpl;
6757edfe 3310 tree tmpl_parms;
7fe6899f
MM
3311 int i;
3312
3d7de1fa
MM
3313 if (!TYPE_TEMPLATE_INFO (type))
3314 {
0f51ccfc 3315 error ("%qT is not a template type", type);
3d7de1fa
MM
3316 return;
3317 }
3318
3319 tmpl = TYPE_TI_TEMPLATE (type);
7fe6899f
MM
3320 if (!PRIMARY_TEMPLATE_P (tmpl))
3321 /* The type is nested in some template class. Nothing to worry
3322 about here; there are no new template parameters for the nested
3323 type. */
3324 return;
3325
ee921ddf
MM
3326 if (!parms)
3327 {
3328 error ("template specifiers not specified in declaration of %qD",
3329 tmpl);
3330 return;
3331 }
3332
6757edfe
MM
3333 parms = INNERMOST_TEMPLATE_PARMS (parms);
3334 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
3335
7fe6899f
MM
3336 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
3337 {
dee15844 3338 error ("previous declaration %q+D", tmpl);
f49d8c52 3339 error ("used %d template parameter(s) instead of %d",
c8094d83 3340 TREE_VEC_LENGTH (tmpl_parms),
f49d8c52 3341 TREE_VEC_LENGTH (parms));
7fe6899f
MM
3342 return;
3343 }
3344
3345 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
3346 {
2d8ba2c7
LM
3347 tree tmpl_parm;
3348 tree parm;
3349 tree tmpl_default;
3350 tree parm_default;
3351
3352 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
3353 || TREE_VEC_ELT (parms, i) == error_mark_node)
3354 continue;
3355
3356 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
3357 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3358 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
3359 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
7fe6899f 3360
2649701f
KL
3361 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
3362 TEMPLATE_DECL. */
0f67a82f
LM
3363 if (tmpl_parm != error_mark_node
3364 && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
3365 || (TREE_CODE (tmpl_parm) != TYPE_DECL
3366 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))))
7fe6899f 3367 {
dee15844 3368 error ("template parameter %q+#D", tmpl_parm);
0f51ccfc 3369 error ("redeclared here as %q#D", parm);
7fe6899f
MM
3370 return;
3371 }
3372
3373 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
3374 {
3375 /* We have in [temp.param]:
3376
3377 A template-parameter may not be given default arguments
3378 by two different declarations in the same scope. */
0f51ccfc 3379 error ("redefinition of default argument for %q#D", parm);
ddd2d57e 3380 error ("%J original definition appeared here", tmpl_parm);
7fe6899f
MM
3381 return;
3382 }
3383
3384 if (parm_default != NULL_TREE)
3385 /* Update the previous template parameters (which are the ones
3386 that will really count) with the new default value. */
3387 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
7ce74c5e
MM
3388 else if (tmpl_default != NULL_TREE)
3389 /* Update the new parameters, too; they'll be used as the
3390 parameters for any members. */
3391 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
7fe6899f
MM
3392 }
3393}
75650646 3394
9baa27a9
MM
3395/* Simplify EXPR if it is a non-dependent expression. Returns the
3396 (possibly simplified) expression. */
3397
3398tree
3399fold_non_dependent_expr (tree expr)
3400{
3401 /* If we're in a template, but EXPR isn't value dependent, simplify
3402 it. We're supposed to treat:
c8094d83 3403
9baa27a9
MM
3404 template <typename T> void f(T[1 + 1]);
3405 template <typename T> void f(T[2]);
c8094d83 3406
9baa27a9
MM
3407 as two declarations of the same function, for example. */
3408 if (processing_template_decl
3409 && !type_dependent_expression_p (expr)
7416ab02 3410 && !value_dependent_expression_p (expr))
9baa27a9
MM
3411 {
3412 HOST_WIDE_INT saved_processing_template_decl;
3413
3414 saved_processing_template_decl = processing_template_decl;
3415 processing_template_decl = 0;
3416 expr = tsubst_copy_and_build (expr,
3417 /*args=*/NULL_TREE,
3418 tf_error,
3419 /*in_decl=*/NULL_TREE,
015c2c66
MM
3420 /*function_p=*/false,
3421 /*integral_constant_expression_p=*/true);
9baa27a9
MM
3422 processing_template_decl = saved_processing_template_decl;
3423 }
3424 return expr;
3425}
3426
b6ab6892
GB
3427/* EXPR is an expression which is used in a constant-expression context.
3428 For instance, it could be a VAR_DECL with a constant initializer.
3429 Extract the innest constant expression.
c8094d83 3430
8a784e4a
NS
3431 This is basically a more powerful version of
3432 integral_constant_value, which can be used also in templates where
3433 initializers can maintain a syntactic rather than semantic form
3434 (even if they are non-dependent, for access-checking purposes). */
b6ab6892 3435
993acaec 3436static tree
b6ab6892
GB
3437fold_decl_constant_value (tree expr)
3438{
4ef69b83
GB
3439 tree const_expr = expr;
3440 do
b6ab6892 3441 {
b6ab6892 3442 expr = fold_non_dependent_expr (const_expr);
4ef69b83 3443 const_expr = integral_constant_value (expr);
b6ab6892 3444 }
4ef69b83 3445 while (expr != const_expr);
b6ab6892 3446
8a784e4a 3447 return expr;
b6ab6892
GB
3448}
3449
3450/* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
3451 must be a function or a pointer-to-function type, as specified
3452 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
3453 and check that the resulting function has external linkage. */
3454
3455static tree
3456convert_nontype_argument_function (tree type, tree expr)
3457{
3458 tree fns = expr;
3459 tree fn, fn_no_ptr;
3460
3461 fn = instantiate_type (type, fns, tf_none);
3462 if (fn == error_mark_node)
3463 return error_mark_node;
3464
3465 fn_no_ptr = fn;
3466 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
3467 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
eff3a276
MM
3468 if (TREE_CODE (fn_no_ptr) == BASELINK)
3469 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
3470
b6ab6892
GB
3471 /* [temp.arg.nontype]/1
3472
3473 A template-argument for a non-type, non-template template-parameter
3474 shall be one of:
3475 [...]
3476 -- the address of an object or function with external linkage. */
3477 if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
3478 {
3479 error ("%qE is not a valid template argument for type %qT "
3480 "because function %qD has not external linkage",
3481 expr, type, fn_no_ptr);
3482 return NULL_TREE;
3483 }
3484
3485 return fn;
3486}
3487
75650646
MM
3488/* Attempt to convert the non-type template parameter EXPR to the
3489 indicated TYPE. If the conversion is successful, return the
dc957d14 3490 converted value. If the conversion is unsuccessful, return
75650646
MM
3491 NULL_TREE if we issued an error message, or error_mark_node if we
3492 did not. We issue error messages for out-and-out bad template
3493 parameters, but not simply because the conversion failed, since we
9baa27a9 3494 might be just trying to do argument deduction. Both TYPE and EXPR
b6ab6892
GB
3495 must be non-dependent.
3496
3497 The conversion follows the special rules described in
3498 [temp.arg.nontype], and it is much more strict than an implicit
3499 conversion.
3500
3501 This function is called twice for each template argument (see
3502 lookup_template_class for a more accurate description of this
3503 problem). This means that we need to handle expressions which
3504 are not valid in a C++ source, but can be created from the
3505 first call (for instance, casts to perform conversions). These
3506 hacks can go away after we fix the double coercion problem. */
75650646
MM
3507
3508static tree
3a978d72 3509convert_nontype_argument (tree type, tree expr)
75650646 3510{
9baa27a9
MM
3511 tree expr_type;
3512
b6ab6892
GB
3513 /* Detect immediately string literals as invalid non-type argument.
3514 This special-case is not needed for correctness (we would easily
3515 catch this later), but only to provide better diagnostic for this
3516 common user mistake. As suggested by DR 100, we do not mention
3517 linkage issues in the diagnostic as this is not the point. */
3518 if (TREE_CODE (expr) == STRING_CST)
3519 {
3520 error ("%qE is not a valid template argument for type %qT "
3521 "because string literals can never be used in this context",
3522 expr, type);
3523 return NULL_TREE;
3524 }
3525
9baa27a9
MM
3526 /* If we are in a template, EXPR may be non-dependent, but still
3527 have a syntactic, rather than semantic, form. For example, EXPR
3528 might be a SCOPE_REF, rather than the VAR_DECL to which the
3529 SCOPE_REF refers. Preserving the qualifying scope is necessary
3530 so that access checking can be performed when the template is
3531 instantiated -- but here we need the resolved form so that we can
3532 convert the argument. */
3533 expr = fold_non_dependent_expr (expr);
b166a559
VR
3534 if (error_operand_p (expr))
3535 return error_mark_node;
9baa27a9 3536 expr_type = TREE_TYPE (expr);
75650646 3537
b6ab6892
GB
3538 /* HACK: Due to double coercion, we can get a
3539 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
3540 which is the tree that we built on the first call (see
3541 below when coercing to reference to object or to reference to
3542 function). We just strip everything and get to the arg.
3543 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
3544 for examples. */
3545 if (TREE_CODE (expr) == NOP_EXPR)
75650646 3546 {
b6ab6892 3547 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
75650646 3548 {
b6ab6892
GB
3549 /* ??? Maybe we could use convert_from_reference here, but we
3550 would need to relax its constraints because the NOP_EXPR
3551 could actually change the type to something more cv-qualified,
3552 and this is not folded by convert_from_reference. */
3553 tree addr = TREE_OPERAND (expr, 0);
3554 gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
3555 gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
3556 gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
3557 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3558 (TREE_TYPE (expr_type),
3559 TREE_TYPE (TREE_TYPE (addr))));
3560
3561 expr = TREE_OPERAND (addr, 0);
3562 expr_type = TREE_TYPE (expr);
75650646
MM
3563 }
3564
b6ab6892
GB
3565 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
3566 parameter is a pointer to object, through decay and
3567 qualification conversion. Let's strip everything. */
3568 else if (TYPE_PTROBV_P (type))
75650646 3569 {
b6ab6892
GB
3570 STRIP_NOPS (expr);
3571 gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
3572 gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
3573 /* Skip the ADDR_EXPR only if it is part of the decay for
3574 an array. Otherwise, it is part of the original argument
3575 in the source code. */
3576 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
3577 expr = TREE_OPERAND (expr, 0);
3578 expr_type = TREE_TYPE (expr);
f3400fe2 3579 }
75650646 3580 }
c61dce3a 3581
b6ab6892 3582 /* [temp.arg.nontype]/5, bullet 1
75650646 3583
b6ab6892
GB
3584 For a non-type template-parameter of integral or enumeration type,
3585 integral promotions (_conv.prom_) and integral conversions
3586 (_conv.integral_) are applied. */
3587 if (INTEGRAL_TYPE_P (type))
75650646 3588 {
75650646
MM
3589 if (!INTEGRAL_TYPE_P (expr_type))
3590 return error_mark_node;
fddabb2c 3591
b6ab6892
GB
3592 expr = fold_decl_constant_value (expr);
3593 /* Notice that there are constant expressions like '4 % 0' which
3594 do not fold into integer constants. */
db02b6b9 3595 if (TREE_CODE (expr) != INTEGER_CST)
b6ab6892
GB
3596 {
3597 error ("%qE is not a valid template argument for type %qT "
3598 "because it is a non-constant expression", expr, type);
3599 return NULL_TREE;
3600 }
75650646 3601
b6ab6892
GB
3602 /* At this point, an implicit conversion does what we want,
3603 because we already know that the expression is of integral
3604 type. */
3605 expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
3606 if (expr == error_mark_node)
3607 return error_mark_node;
75650646 3608
b6ab6892
GB
3609 /* Conversion was allowed: fold it to a bare integer constant. */
3610 expr = fold (expr);
3611 }
3612 /* [temp.arg.nontype]/5, bullet 2
75650646 3613
b6ab6892
GB
3614 For a non-type template-parameter of type pointer to object,
3615 qualification conversions (_conv.qual_) and the array-to-pointer
3616 conversion (_conv.array_) are applied. */
3617 else if (TYPE_PTROBV_P (type))
3618 {
3619 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
75650646 3620
b6ab6892
GB
3621 A template-argument for a non-type, non-template template-parameter
3622 shall be one of: [...]
75650646 3623
b6ab6892
GB
3624 -- the name of a non-type template-parameter;
3625 -- the address of an object or function with external linkage, [...]
3626 expressed as "& id-expression" where the & is optional if the name
3627 refers to a function or array, or if the corresponding
3628 template-parameter is a reference.
c8094d83 3629
b6ab6892
GB
3630 Here, we do not care about functions, as they are invalid anyway
3631 for a parameter of type pointer-to-object. */
3632 bool constant_address_p =
3633 (TREE_CODE (expr) == ADDR_EXPR
3634 || TREE_CODE (expr_type) == ARRAY_TYPE
3635 || (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
3636
3637 expr = decay_conversion (expr);
3638 if (expr == error_mark_node)
3639 return error_mark_node;
75650646 3640
b6ab6892
GB
3641 expr = perform_qualification_conversions (type, expr);
3642 if (expr == error_mark_node)
3643 return error_mark_node;
0dc09a61 3644
b6ab6892
GB
3645 if (!constant_address_p)
3646 {
b9e75696
JM
3647 error ("%qE is not a valid template argument for type %qT "
3648 "because it is not a constant pointer", expr, type);
3649 return NULL_TREE;
b6ab6892
GB
3650 }
3651 }
3652 /* [temp.arg.nontype]/5, bullet 3
3653
3654 For a non-type template-parameter of type reference to object, no
3655 conversions apply. The type referred to by the reference may be more
3656 cv-qualified than the (otherwise identical) type of the
3657 template-argument. The template-parameter is bound directly to the
3658 template-argument, which must be an lvalue. */
3659 else if (TYPE_REF_OBJ_P (type))
3660 {
3661 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
3662 expr_type))
3663 return error_mark_node;
75650646 3664
b6ab6892
GB
3665 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
3666 {
3667 error ("%qE is not a valid template argument for type %qT "
3668 "because of conflicts in cv-qualification", expr, type);
3669 return NULL_TREE;
3670 }
c8094d83 3671
b6ab6892
GB
3672 if (!real_lvalue_p (expr))
3673 {
3674 error ("%qE is not a valid template argument for type %qT "
944fd207 3675 "because it is not an lvalue", expr, type);
b6ab6892
GB
3676 return NULL_TREE;
3677 }
e6e174e5 3678
b6ab6892 3679 /* [temp.arg.nontype]/1
75650646 3680
b6ab6892
GB
3681 A template-argument for a non-type, non-template template-parameter
3682 shall be one of: [...]
75650646 3683
03fd3f84 3684 -- the address of an object or function with external linkage. */
b6ab6892
GB
3685 if (!DECL_EXTERNAL_LINKAGE_P (expr))
3686 {
3687 error ("%qE is not a valid template argument for type %qT "
3688 "because object %qD has not external linkage",
3689 expr, type, expr);
3690 return NULL_TREE;
3691 }
0dc09a61 3692
b6ab6892
GB
3693 expr = build_nop (type, build_address (expr));
3694 }
3695 /* [temp.arg.nontype]/5, bullet 4
3696
3697 For a non-type template-parameter of type pointer to function, only
3698 the function-to-pointer conversion (_conv.func_) is applied. If the
3699 template-argument represents a set of overloaded functions (or a
3700 pointer to such), the matching function is selected from the set
3701 (_over.over_). */
3702 else if (TYPE_PTRFN_P (type))
3703 {
3704 /* If the argument is a template-id, we might not have enough
2c164de6 3705 context information to decay the pointer. */
b6ab6892
GB
3706 if (!type_unknown_p (expr_type))
3707 {
3708 expr = decay_conversion (expr);
3709 if (expr == error_mark_node)
3710 return error_mark_node;
3711 }
75650646 3712
b6ab6892
GB
3713 expr = convert_nontype_argument_function (type, expr);
3714 if (!expr || expr == error_mark_node)
3715 return expr;
3716 }
3717 /* [temp.arg.nontype]/5, bullet 5
75650646 3718
b6ab6892
GB
3719 For a non-type template-parameter of type reference to function, no
3720 conversions apply. If the template-argument represents a set of
3721 overloaded functions, the matching function is selected from the set
3722 (_over.over_). */
3723 else if (TYPE_REFFN_P (type))
3724 {
3725 if (TREE_CODE (expr) == ADDR_EXPR)
3726 {
3727 error ("%qE is not a valid template argument for type %qT "
3728 "because it is a pointer", expr, type);
3729 inform ("try using %qE instead", TREE_OPERAND (expr, 0));
3730 return NULL_TREE;
3731 }
75650646 3732
b6ab6892
GB
3733 expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
3734 if (!expr || expr == error_mark_node)
3735 return expr;
75650646 3736
7866705a 3737 expr = build_nop (type, build_address (expr));
b6ab6892
GB
3738 }
3739 /* [temp.arg.nontype]/5, bullet 6
75650646 3740
b6ab6892
GB
3741 For a non-type template-parameter of type pointer to member function,
3742 no conversions apply. If the template-argument represents a set of
3743 overloaded member functions, the matching member function is selected
3744 from the set (_over.over_). */
3745 else if (TYPE_PTRMEMFUNC_P (type))
3746 {
3747 expr = instantiate_type (type, expr, tf_none);
3748 if (expr == error_mark_node)
3749 return error_mark_node;
75650646 3750
b6ab6892
GB
3751 /* There is no way to disable standard conversions in
3752 resolve_address_of_overloaded_function (called by
3753 instantiate_type). It is possible that the call succeeded by
3754 converting &B::I to &D::I (where B is a base of D), so we need
3755 to reject this conversion here.
75650646 3756
b6ab6892
GB
3757 Actually, even if there was a way to disable standard conversions,
3758 it would still be better to reject them here so that we can
3759 provide a superior diagnostic. */
3760 if (!same_type_p (TREE_TYPE (expr), type))
3761 {
3762 /* Make sure we are just one standard conversion off. */
3763 gcc_assert (can_convert (type, TREE_TYPE (expr)));
3764 error ("%qE is not a valid template argument for type %qT "
3765 "because it is of type %qT", expr, type,
3766 TREE_TYPE (expr));
3767 inform ("standard conversions are not allowed in this context");
3768 return NULL_TREE;
3769 }
3770 }
3771 /* [temp.arg.nontype]/5, bullet 7
59e7a901 3772
b6ab6892
GB
3773 For a non-type template-parameter of type pointer to data member,
3774 qualification conversions (_conv.qual_) are applied. */
3775 else if (TYPE_PTRMEM_P (type))
3776 {
3777 expr = perform_qualification_conversions (type, expr);
3778 if (expr == error_mark_node)
75650646 3779 return expr;
75650646 3780 }
b6ab6892
GB
3781 /* A template non-type parameter must be one of the above. */
3782 else
3783 gcc_unreachable ();
75650646 3784
b6ab6892
GB
3785 /* Sanity check: did we actually convert the argument to the
3786 right type? */
3787 gcc_assert (same_type_p (type, TREE_TYPE (expr)));
3788 return expr;
75650646
MM
3789}
3790
b6ab6892 3791
c8094d83
MS
3792/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3793 template template parameters. Both PARM_PARMS and ARG_PARMS are
3794 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
744fac59 3795 or PARM_DECL.
c8094d83 3796
744fac59 3797 Consider the example:
e7e93965
MM
3798 template <class T> class A;
3799 template<template <class U> class TT> class B;
744fac59 3800
e7e93965
MM
3801 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
3802 the parameters to A, and OUTER_ARGS contains A. */
744fac59
KL
3803
3804static int
c8094d83 3805coerce_template_template_parms (tree parm_parms,
0cbd7506
MS
3806 tree arg_parms,
3807 tsubst_flags_t complain,
3a978d72 3808 tree in_decl,
0cbd7506 3809 tree outer_args)
744fac59
KL
3810{
3811 int nparms, nargs, i;
3812 tree parm, arg;
3813
50bc768d
NS
3814 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
3815 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
744fac59
KL
3816
3817 nparms = TREE_VEC_LENGTH (parm_parms);
3818 nargs = TREE_VEC_LENGTH (arg_parms);
3819
e7e93965 3820 if (nargs != nparms)
744fac59
KL
3821 return 0;
3822
3823 for (i = 0; i < nparms; ++i)
3824 {
2d8ba2c7
LM
3825 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
3826 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
42b304f1
LM
3827 continue;
3828
744fac59
KL
3829 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3830 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3831
3832 if (arg == NULL_TREE || arg == error_mark_node
0cbd7506 3833 || parm == NULL_TREE || parm == error_mark_node)
744fac59
KL
3834 return 0;
3835
3836 if (TREE_CODE (arg) != TREE_CODE (parm))
0cbd7506 3837 return 0;
744fac59
KL
3838
3839 switch (TREE_CODE (parm))
3840 {
3841 case TYPE_DECL:
3842 break;
3843
3844 case TEMPLATE_DECL:
3845 /* We encounter instantiations of templates like
3846 template <template <template <class> class> class TT>
3847 class C; */
700466c2
JM
3848 {
3849 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3850 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3851
8c6ab2db
NS
3852 if (!coerce_template_template_parms
3853 (parmparm, argparm, complain, in_decl, outer_args))
700466c2
JM
3854 return 0;
3855 }
3856 break;
744fac59
KL
3857
3858 case PARM_DECL:
3859 /* The tsubst call is used to handle cases such as
00bdb87f
KL
3860
3861 template <int> class C {};
3862 template <class T, template <T> class TT> class D {};
3863 D<int, C> d;
3864
744fac59 3865 i.e. the parameter list of TT depends on earlier parameters. */
00bdb87f
KL
3866 if (!dependent_type_p (TREE_TYPE (arg))
3867 && !same_type_p
3868 (tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
3869 TREE_TYPE (arg)))
744fac59
KL
3870 return 0;
3871 break;
c8094d83 3872
744fac59 3873 default:
315fb5db 3874 gcc_unreachable ();
744fac59
KL
3875 }
3876 }
3877 return 1;
3878}
3879
8b5b8b7c
MM
3880/* Convert the indicated template ARG as necessary to match the
3881 indicated template PARM. Returns the converted ARG, or
c2ea3a40
NS
3882 error_mark_node if the conversion was unsuccessful. Error and
3883 warning messages are issued under control of COMPLAIN. This
3884 conversion is for the Ith parameter in the parameter list. ARGS is
3885 the full set of template arguments deduced so far. */
8b5b8b7c
MM
3886
3887static tree
c8094d83 3888convert_template_argument (tree parm,
0cbd7506
MS
3889 tree arg,
3890 tree args,
3891 tsubst_flags_t complain,
3892 int i,
3893 tree in_decl)
8b5b8b7c
MM
3894{
3895 tree val;
8b5b8b7c 3896 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
c8094d83 3897
c8094d83 3898 if (TREE_CODE (arg) == TREE_LIST
a5ac359a 3899 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
c8094d83 3900 {
8b5b8b7c
MM
3901 /* The template argument was the name of some
3902 member function. That's usually
0e339752 3903 invalid, but static members are OK. In any
8b5b8b7c
MM
3904 case, grab the underlying fields/functions
3905 and issue an error later if required. */
3906 arg = TREE_VALUE (arg);
3907 TREE_TYPE (arg) = unknown_type_node;
3908 }
3909
3910 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3911 requires_type = (TREE_CODE (parm) == TYPE_DECL
3912 || requires_tmpl_type);
3913
cbd63935
KL
3914 is_tmpl_type = ((TREE_CODE (arg) == TEMPLATE_DECL
3915 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3916 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3917 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
c8094d83 3918
b8c6534b
KL
3919 if (is_tmpl_type
3920 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
3921 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8b5b8b7c 3922 arg = TYPE_STUB_DECL (arg);
8b5b8b7c 3923
2f939d94 3924 is_type = TYPE_P (arg) || is_tmpl_type;
8b5b8b7c
MM
3925
3926 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3927 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3928 {
0f51ccfc 3929 pedwarn ("to refer to a type member of a template parameter, "
0cbd7506 3930 "use %<typename %E%>", arg);
c8094d83 3931
8b5b8b7c 3932 arg = make_typename_type (TREE_OPERAND (arg, 0),
3baa501d 3933 TREE_OPERAND (arg, 1),
fc6a28d7 3934 typename_type,
c2ea3a40 3935 complain & tf_error);
8b5b8b7c
MM
3936 is_type = 1;
3937 }
3938 if (is_type != requires_type)
3939 {
3940 if (in_decl)
3941 {
c2ea3a40 3942 if (complain & tf_error)
8b5b8b7c 3943 {
0f51ccfc 3944 error ("type/value mismatch at argument %d in template "
0cbd7506
MS
3945 "parameter list for %qD",
3946 i + 1, in_decl);
8b5b8b7c 3947 if (is_type)
0f51ccfc 3948 error (" expected a constant of type %qT, got %qT",
0cbd7506
MS
3949 TREE_TYPE (parm),
3950 (is_tmpl_type ? DECL_NAME (arg) : arg));
d12a7283 3951 else if (requires_tmpl_type)
0f51ccfc 3952 error (" expected a class template, got %qE", arg);
8b5b8b7c 3953 else
0f51ccfc 3954 error (" expected a type, got %qE", arg);
8b5b8b7c
MM
3955 }
3956 }
3957 return error_mark_node;
3958 }
3959 if (is_tmpl_type ^ requires_tmpl_type)
3960 {
c2ea3a40 3961 if (in_decl && (complain & tf_error))
8b5b8b7c 3962 {
0f51ccfc 3963 error ("type/value mismatch at argument %d in template "
0cbd7506
MS
3964 "parameter list for %qD",
3965 i + 1, in_decl);
8b5b8b7c 3966 if (is_tmpl_type)
0f51ccfc 3967 error (" expected a type, got %qT", DECL_NAME (arg));
8b5b8b7c 3968 else
0f51ccfc 3969 error (" expected a class template, got %qT", arg);
8b5b8b7c
MM
3970 }
3971 return error_mark_node;
3972 }
c8094d83 3973
8b5b8b7c
MM
3974 if (is_type)
3975 {
3976 if (requires_tmpl_type)
3977 {
b8c6534b
KL
3978 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
3979 /* The number of argument required is not known yet.
3980 Just accept it for now. */
3981 val = TREE_TYPE (arg);
8b5b8b7c
MM
3982 else
3983 {
b8c6534b
KL
3984 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3985 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3986
3987 if (coerce_template_template_parms (parmparm, argparm,
3988 complain, in_decl,
6150b602 3989 args))
8b5b8b7c 3990 {
b8c6534b 3991 val = arg;
c8094d83
MS
3992
3993 /* TEMPLATE_TEMPLATE_PARM node is preferred over
b8c6534b 3994 TEMPLATE_DECL. */
c8094d83 3995 if (val != error_mark_node
b8c6534b
KL
3996 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3997 val = TREE_TYPE (val);
8b5b8b7c 3998 }
b8c6534b
KL
3999 else
4000 {
c2ea3a40 4001 if (in_decl && (complain & tf_error))
b8c6534b 4002 {
0f51ccfc 4003 error ("type/value mismatch at argument %d in "
0cbd7506
MS
4004 "template parameter list for %qD",
4005 i + 1, in_decl);
0f51ccfc 4006 error (" expected a template of type %qD, got %qD",
0cbd7506 4007 parm, arg);
b8c6534b 4008 }
c8094d83 4009
b8c6534b
KL
4010 val = error_mark_node;
4011 }
8b5b8b7c
MM
4012 }
4013 }
4014 else
058b15c1 4015 val = arg;
685e39c2
MM
4016 /* We only form one instance of each template specialization.
4017 Therefore, if we use a non-canonical variant (i.e., a
3db45ab5 4018 typedef), any future messages referring to the type will use
685e39c2
MM
4019 the typedef, which is confusing if those future uses do not
4020 themselves also use the typedef. */
4021 if (TYPE_P (val))
4022 val = canonical_type_variant (val);
8b5b8b7c
MM
4023 }
4024 else
4025 {
4393e105 4026 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
8b5b8b7c 4027
11b810f1 4028 if (invalid_nontype_parm_type_p (t, complain))
0cbd7506 4029 return error_mark_node;
c8094d83 4030
d768a589 4031 if (!uses_template_parms (arg) && !uses_template_parms (t))
8b5b8b7c
MM
4032 /* We used to call digest_init here. However, digest_init
4033 will report errors, which we don't want when complain
4034 is zero. More importantly, digest_init will try too
4035 hard to convert things: for example, `0' should not be
4036 converted to pointer type at this point according to
4037 the standard. Accepting this is not merely an
4038 extension, since deciding whether or not these
4039 conversions can occur is part of determining which
dc957d14 4040 function template to call, or whether a given explicit
0e339752 4041 argument specification is valid. */
8b5b8b7c
MM
4042 val = convert_nontype_argument (t, arg);
4043 else
4044 val = arg;
4045
4046 if (val == NULL_TREE)
4047 val = error_mark_node;
c2ea3a40 4048 else if (val == error_mark_node && (complain & tf_error))
0f51ccfc 4049 error ("could not convert template argument %qE to %qT", arg, t);
8b5b8b7c
MM
4050 }
4051
4052 return val;
4053}
4054
4055/* Convert all template arguments to their appropriate types, and
4056 return a vector containing the innermost resulting template
c2ea3a40 4057 arguments. If any error occurs, return error_mark_node. Error and
3e4a3562 4058 warning messages are issued under control of COMPLAIN.
75650646 4059
e7e93965 4060 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
84dc00e8 4061 for arguments not specified in ARGS. Otherwise, if
e7e93965
MM
4062 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
4063 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
4064 USE_DEFAULT_ARGS is false, then all arguments must be specified in
4065 ARGS. */
c8094d83 4066
8d08fdba 4067static tree
c8094d83 4068coerce_template_parms (tree parms,
0cbd7506
MS
4069 tree args,
4070 tree in_decl,
3a978d72 4071 tsubst_flags_t complain,
e7e93965
MM
4072 bool require_all_args,
4073 bool use_default_args)
8d08fdba 4074{
a292b002 4075 int nparms, nargs, i, lost = 0;
e4a84209 4076 tree inner_args;
8b5b8b7c
MM
4077 tree new_args;
4078 tree new_inner_args;
a292b002 4079
f9a7ae04 4080 inner_args = INNERMOST_TEMPLATE_ARGS (args);
bf12d54d 4081 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
a292b002
MS
4082 nparms = TREE_VEC_LENGTH (parms);
4083
4084 if (nargs > nparms
4085 || (nargs < nparms
e7e93965
MM
4086 && require_all_args
4087 && (!use_default_args
42b304f1
LM
4088 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
4089 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8d08fdba 4090 {
c8094d83 4091 if (complain & tf_error)
75650646 4092 {
33bd39a2 4093 error ("wrong number of template arguments (%d, should be %d)",
0cbd7506 4094 nargs, nparms);
c8094d83 4095
75650646 4096 if (in_decl)
dee15844 4097 error ("provided for %q+D", in_decl);
75650646
MM
4098 }
4099
8d08fdba
MS
4100 return error_mark_node;
4101 }
4102
f31c0a32 4103 new_inner_args = make_tree_vec (nparms);
8b5b8b7c
MM
4104 new_args = add_outermost_template_args (args, new_inner_args);
4105 for (i = 0; i < nparms; i++)
8d08fdba 4106 {
8b5b8b7c
MM
4107 tree arg;
4108 tree parm;
e4a84209 4109
8b5b8b7c
MM
4110 /* Get the Ith template parameter. */
4111 parm = TREE_VEC_ELT (parms, i);
42b304f1
LM
4112
4113 if (parm == error_mark_node)
2d8ba2c7
LM
4114 {
4115 TREE_VEC_ELT (new_inner_args, i) = error_mark_node;
42b304f1 4116 continue;
2d8ba2c7 4117 }
75650646 4118
8b5b8b7c 4119 /* Calculate the Ith argument. */
bf12d54d 4120 if (i < nargs)
8b5b8b7c 4121 arg = TREE_VEC_ELT (inner_args, i);
e7e93965 4122 else if (require_all_args)
04c06002 4123 /* There must be a default arg in this case. */
a91db711
NS
4124 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
4125 complain, in_decl);
ffd49b19
NS
4126 else
4127 break;
c8094d83 4128
50bc768d 4129 gcc_assert (arg);
ffd49b19 4130 if (arg == error_mark_node)
e34b0922
KL
4131 {
4132 if (complain & tf_error)
4133 error ("template argument %d is invalid", i + 1);
4134 }
c8094d83
MS
4135 else
4136 arg = convert_template_argument (TREE_VALUE (parm),
8b5b8b7c 4137 arg, new_args, complain, i,
c8094d83
MS
4138 in_decl);
4139
8b5b8b7c 4140 if (arg == error_mark_node)
8d08fdba 4141 lost++;
8b5b8b7c 4142 TREE_VEC_ELT (new_inner_args, i) = arg;
8d08fdba 4143 }
8b5b8b7c 4144
8d08fdba
MS
4145 if (lost)
4146 return error_mark_node;
8b5b8b7c
MM
4147
4148 return new_inner_args;
8d08fdba
MS
4149}
4150
34016c81
JM
4151/* Returns 1 if template args OT and NT are equivalent. */
4152
d8e178a0 4153static int
3a978d72 4154template_args_equal (tree ot, tree nt)
34016c81
JM
4155{
4156 if (nt == ot)
4157 return 1;
74601d7c 4158
34016c81
JM
4159 if (TREE_CODE (nt) == TREE_VEC)
4160 /* For member templates */
74601d7c
KL
4161 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
4162 else if (TYPE_P (nt))
660845bf 4163 return TYPE_P (ot) && same_type_p (ot, nt);
74601d7c
KL
4164 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
4165 return 0;
34016c81 4166 else
c8a209ca 4167 return cp_tree_equal (ot, nt);
34016c81
JM
4168}
4169
4170/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
f84b4be9
JM
4171 of template arguments. Returns 0 otherwise. */
4172
6757edfe 4173int
3a978d72 4174comp_template_args (tree oldargs, tree newargs)
5566b478
MS
4175{
4176 int i;
4177
386b8a85
JM
4178 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
4179 return 0;
4180
5566b478
MS
4181 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
4182 {
4183 tree nt = TREE_VEC_ELT (newargs, i);
4184 tree ot = TREE_VEC_ELT (oldargs, i);
4185
34016c81 4186 if (! template_args_equal (ot, nt))
61a127b3 4187 return 0;
5566b478
MS
4188 }
4189 return 1;
4190}
4191
8d08fdba
MS
4192/* Given class template name and parameter list, produce a user-friendly name
4193 for the instantiation. */
e92cc029 4194
8d08fdba 4195static char *
3a978d72 4196mangle_class_name_for_template (const char* name, tree parms, tree arglist)
8d08fdba
MS
4197{
4198 static struct obstack scratch_obstack;
4199 static char *scratch_firstobj;
4200 int i, nparms;
8d08fdba
MS
4201
4202 if (!scratch_firstobj)
fc378698 4203 gcc_obstack_init (&scratch_obstack);
8d08fdba
MS
4204 else
4205 obstack_free (&scratch_obstack, scratch_firstobj);
67f5655f 4206 scratch_firstobj = (char *) obstack_alloc (&scratch_obstack, 1);
8d08fdba 4207
18e314ad
GS
4208#define ccat(C) obstack_1grow (&scratch_obstack, (C));
4209#define cat(S) obstack_grow (&scratch_obstack, (S), strlen (S))
8d08fdba
MS
4210
4211 cat (name);
4212 ccat ('<');
4213 nparms = TREE_VEC_LENGTH (parms);
f9a7ae04 4214 arglist = INNERMOST_TEMPLATE_ARGS (arglist);
50bc768d 4215 gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
8d08fdba
MS
4216 for (i = 0; i < nparms; i++)
4217 {
42b304f1
LM
4218 tree parm;
4219 tree arg;
4220
42b304f1
LM
4221 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
4222 arg = TREE_VEC_ELT (arglist, i);
8d08fdba 4223
0f67a82f
LM
4224 if (parm == error_mark_node)
4225 continue;
4226
8d08fdba
MS
4227 if (i)
4228 ccat (',');
4229
a292b002 4230 if (TREE_CODE (parm) == TYPE_DECL)
8d08fdba 4231 {
761f0855 4232 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
8d08fdba
MS
4233 continue;
4234 }
73b0fce8
KL
4235 else if (TREE_CODE (parm) == TEMPLATE_DECL)
4236 {
4237 if (TREE_CODE (arg) == TEMPLATE_DECL)
2c73f9f5 4238 {
c8094d83 4239 /* Already substituted with real template. Just output
2c73f9f5 4240 the template name here */
0cbd7506
MS
4241 tree context = DECL_CONTEXT (arg);
4242 if (context)
4243 {
4244 /* The template may be defined in a namespace, or
4245 may be a member template. */
4246 gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
50bc768d
NS
4247 || CLASS_TYPE_P (context));
4248 cat (decl_as_string (DECL_CONTEXT (arg),
4249 TFF_PLAIN_IDENTIFIER));
4250 cat ("::");
2c73f9f5
ML
4251 }
4252 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
4253 }
73b0fce8 4254 else
f4f206f4 4255 /* Output the parameter declaration. */
761f0855 4256 cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
73b0fce8
KL
4257 continue;
4258 }
8d08fdba 4259 else
50bc768d 4260 gcc_assert (TREE_CODE (parm) == PARM_DECL);
8d08fdba 4261
8d08fdba
MS
4262 /* No need to check arglist against parmlist here; we did that
4263 in coerce_template_parms, called from lookup_template_class. */
761f0855 4264 cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
8d08fdba
MS
4265 }
4266 {
4267 char *bufp = obstack_next_free (&scratch_obstack);
4268 int offset = 0;
4269 while (bufp[offset - 1] == ' ')
4270 offset--;
4271 obstack_blank_fast (&scratch_obstack, offset);
4272
4273 /* B<C<char> >, not B<C<char>> */
4274 if (bufp[offset - 1] == '>')
4275 ccat (' ');
4276 }
4277 ccat ('>');
4278 ccat ('\0');
4279 return (char *) obstack_base (&scratch_obstack);
8d08fdba
MS
4280}
4281
bd6dd845 4282static tree
3a978d72 4283classtype_mangled_name (tree t)
5566b478
MS
4284{
4285 if (CLASSTYPE_TEMPLATE_INFO (t)
36a117a5
MM
4286 /* Specializations have already had their names set up in
4287 lookup_template_class. */
370af2d5 4288 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9fbf56f7
MM
4289 {
4290 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
4291
36a117a5
MM
4292 /* For non-primary templates, the template parameters are
4293 implicit from their surrounding context. */
9fbf56f7
MM
4294 if (PRIMARY_TEMPLATE_P (tmpl))
4295 {
4296 tree name = DECL_NAME (tmpl);
4297 char *mangled_name = mangle_class_name_for_template
c8094d83 4298 (IDENTIFIER_POINTER (name),
9fbf56f7
MM
4299 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
4300 CLASSTYPE_TI_ARGS (t));
4301 tree id = get_identifier (mangled_name);
4302 IDENTIFIER_TEMPLATE (id) = name;
4303 return id;
4304 }
5566b478 4305 }
9fbf56f7
MM
4306
4307 return TYPE_IDENTIFIER (t);
5566b478
MS
4308}
4309
4310static void
3a978d72 4311add_pending_template (tree d)
5566b478 4312{
3ae18eaf
JM
4313 tree ti = (TYPE_P (d)
4314 ? CLASSTYPE_TEMPLATE_INFO (d)
4315 : DECL_TEMPLATE_INFO (d));
46ccf50a 4316 tree pt;
3ae18eaf 4317 int level;
e92cc029 4318
824b9a4c 4319 if (TI_PENDING_TEMPLATE_FLAG (ti))
5566b478
MS
4320 return;
4321
3ae18eaf
JM
4322 /* We are called both from instantiate_decl, where we've already had a
4323 tinst_level pushed, and instantiate_template, where we haven't.
4324 Compensate. */
4325 level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
4326
4327 if (level)
4328 push_tinst_level (d);
4329
46ccf50a
JM
4330 pt = tree_cons (current_tinst_level, d, NULL_TREE);
4331 if (last_pending_template)
4332 TREE_CHAIN (last_pending_template) = pt;
4333 else
4334 pending_templates = pt;
4335
4336 last_pending_template = pt;
4337
824b9a4c 4338 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3ae18eaf
JM
4339
4340 if (level)
4341 pop_tinst_level ();
5566b478
MS
4342}
4343
386b8a85 4344
4ba126e4
MM
4345/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
4346 ARGLIST. Valid choices for FNS are given in the cp-tree.def
4347 documentation for TEMPLATE_ID_EXPR. */
386b8a85
JM
4348
4349tree
3a978d72 4350lookup_template_function (tree fns, tree arglist)
386b8a85 4351{
2c73f9f5 4352 tree type;
050367a3 4353
4ba126e4
MM
4354 if (fns == error_mark_node || arglist == error_mark_node)
4355 return error_mark_node;
4356
50bc768d 4357 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
4230cec2
NS
4358 gcc_assert (fns && (is_overloaded_fn (fns)
4359 || TREE_CODE (fns) == IDENTIFIER_NODE));
4ba126e4 4360
50ad9642
MM
4361 if (BASELINK_P (fns))
4362 {
f293ce4b
RS
4363 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
4364 unknown_type_node,
4365 BASELINK_FUNCTIONS (fns),
4366 arglist);
50ad9642
MM
4367 return fns;
4368 }
4369
2c73f9f5
ML
4370 type = TREE_TYPE (fns);
4371 if (TREE_CODE (fns) == OVERLOAD || !type)
4372 type = unknown_type_node;
c8094d83 4373
f293ce4b 4374 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
386b8a85
JM
4375}
4376
a2b60a0e
MM
4377/* Within the scope of a template class S<T>, the name S gets bound
4378 (in build_self_reference) to a TYPE_DECL for the class, not a
4379 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
4380 or one of its enclosing classes, and that type is a template,
4381 return the associated TEMPLATE_DECL. Otherwise, the original
4382 DECL is returned. */
4383
a723baf1 4384tree
3a978d72 4385maybe_get_template_decl_from_type_decl (tree decl)
a2b60a0e
MM
4386{
4387 return (decl != NULL_TREE
c8094d83 4388 && TREE_CODE (decl) == TYPE_DECL
a2b60a0e 4389 && DECL_ARTIFICIAL (decl)
511b60ff 4390 && CLASS_TYPE_P (TREE_TYPE (decl))
c8094d83 4391 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
a2b60a0e
MM
4392 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
4393}
386b8a85 4394
8d08fdba
MS
4395/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
4396 parameters, find the desired type.
4397
4398 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8d08fdba
MS
4399
4400 IN_DECL, if non-NULL, is the template declaration we are trying to
c8094d83 4401 instantiate.
75650646 4402
838dfd8a 4403 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
36a117a5 4404 the class we are looking up.
c8094d83 4405
c2ea3a40 4406 Issue error and warning messages under control of COMPLAIN.
36a117a5 4407
75650646
MM
4408 If the template class is really a local class in a template
4409 function, then the FUNCTION_CONTEXT is the function in which it is
c8094d83 4410 being instantiated.
b6ab6892
GB
4411
4412 ??? Note that this function is currently called *twice* for each
4413 template-id: the first time from the parser, while creating the
4414 incomplete type (finish_template_type), and the second type during the
4415 real instantiation (instantiate_template_class). This is surely something
4416 that we want to avoid. It also causes some problems with argument
4417 coercion (see convert_nontype_argument for more information on this). */
e92cc029 4418
8d08fdba 4419tree
c8094d83 4420lookup_template_class (tree d1,
0cbd7506
MS
4421 tree arglist,
4422 tree in_decl,
4423 tree context,
4424 int entering_scope,
4425 tsubst_flags_t complain)
8d08fdba 4426{
a703fb38 4427 tree template = NULL_TREE, parmlist;
dbfe2124 4428 tree t;
c8094d83 4429
fd295cb2 4430 timevar_push (TV_NAME_LOOKUP);
c8094d83 4431
5566b478
MS
4432 if (TREE_CODE (d1) == IDENTIFIER_NODE)
4433 {
90ea9897
MM
4434 tree value = innermost_non_namespace_value (d1);
4435 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
4436 template = value;
73b0fce8
KL
4437 else
4438 {
2c73f9f5
ML
4439 if (context)
4440 push_decl_namespace (context);
10e6657a 4441 template = lookup_name (d1);
3ebc5c52 4442 template = maybe_get_template_decl_from_type_decl (template);
2c73f9f5
ML
4443 if (context)
4444 pop_decl_namespace ();
73b0fce8 4445 }
8d019cef
JM
4446 if (template)
4447 context = DECL_CONTEXT (template);
5566b478 4448 }
c91a56d2
MS
4449 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
4450 {
802dbc34
JM
4451 tree type = TREE_TYPE (d1);
4452
4453 /* If we are declaring a constructor, say A<T>::A<T>, we will get
4454 an implicit typename for the second A. Deal with it. */
4455 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
4456 type = TREE_TYPE (type);
c8094d83 4457
802dbc34 4458 if (CLASSTYPE_TEMPLATE_INFO (type))
f3400fe2 4459 {
802dbc34 4460 template = CLASSTYPE_TI_TEMPLATE (type);
f3400fe2
JM
4461 d1 = DECL_NAME (template);
4462 }
c91a56d2 4463 }
c8094d83 4464 else if (TREE_CODE (d1) == ENUMERAL_TYPE
2f939d94 4465 || (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
5566b478 4466 {
ed44da02 4467 template = TYPE_TI_TEMPLATE (d1);
5566b478
MS
4468 d1 = DECL_NAME (template);
4469 }
93cdc044 4470 else if (TREE_CODE (d1) == TEMPLATE_DECL
17aec3eb 4471 && TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
93cdc044
JM
4472 {
4473 template = d1;
4474 d1 = DECL_NAME (template);
4475 context = DECL_CONTEXT (template);
4476 }
8d08fdba 4477
90ea9897 4478 /* Issue an error message if we didn't find a template. */
8d08fdba 4479 if (! template)
f3400fe2 4480 {
c2ea3a40 4481 if (complain & tf_error)
0cbd7506 4482 error ("%qT is not a template", d1);
fd295cb2 4483 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
f3400fe2 4484 }
2c73f9f5 4485
a87b4257 4486 if (TREE_CODE (template) != TEMPLATE_DECL
0cbd7506 4487 /* Make sure it's a user visible template, if it was named by
42eaed49
NS
4488 the user. */
4489 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
4490 && !PRIMARY_TEMPLATE_P (template)))
8d08fdba 4491 {
c2ea3a40 4492 if (complain & tf_error)
0cbd7506
MS
4493 {
4494 error ("non-template type %qT used as a template", d1);
4495 if (in_decl)
dee15844 4496 error ("for template declaration %q+D", in_decl);
f9c244b8 4497 }
fd295cb2 4498 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
8d08fdba 4499 }
8d08fdba 4500
42eaed49 4501 complain &= ~tf_user;
c8094d83 4502
73b0fce8
KL
4503 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
4504 {
4505 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
0cbd7506 4506 template arguments */
73b0fce8 4507
1899c3a4 4508 tree parm;
73b0fce8
KL
4509 tree arglist2;
4510
73b0fce8
KL
4511 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
4512
4f96ff63
KL
4513 /* Consider an example where a template template parameter declared as
4514
4515 template <class T, class U = std::allocator<T> > class TT
4516
c8094d83
MS
4517 The template parameter level of T and U are one level larger than
4518 of TT. To proper process the default argument of U, say when an
4f96ff63 4519 instantiation `TT<int>' is seen, we need to build the full
342cea95
KL
4520 arguments containing {int} as the innermost level. Outer levels,
4521 available when not appearing as default template argument, can be
4522 obtained from `current_template_args ()'.
4f96ff63 4523
342cea95
KL
4524 Suppose that TT is later substituted with std::vector. The above
4525 instantiation is `TT<int, std::allocator<T> >' with TT at
4526 level 1, and T at level 2, while the template arguments at level 1
4527 becomes {std::vector} and the inner level 2 is {int}. */
4528
4529 if (current_template_parms)
4f96ff63
KL
4530 arglist = add_to_template_args (current_template_args (), arglist);
4531
f9c244b8 4532 arglist2 = coerce_template_parms (parmlist, arglist, template,
3db45ab5 4533 complain,
e7e93965
MM
4534 /*require_all_args=*/true,
4535 /*use_default_args=*/true);
3e4a3562 4536 if (arglist2 == error_mark_node
544aef8c 4537 || (!uses_template_parms (arglist2)
3e4a3562 4538 && check_instantiated_args (template, arglist2, complain)))
0cbd7506 4539 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
73b0fce8 4540
dac65501 4541 parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
fd295cb2 4542 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, parm);
73b0fce8 4543 }
c8094d83 4544 else
8d08fdba 4545 {
36a117a5 4546 tree template_type = TREE_TYPE (template);
7ac7b28f 4547 tree gen_tmpl;
36a117a5
MM
4548 tree type_decl;
4549 tree found = NULL_TREE;
4550 int arg_depth;
4551 int parm_depth;
dbfe2124 4552 int is_partial_instantiation;
830bfa74 4553
7ac7b28f
MM
4554 gen_tmpl = most_general_template (template);
4555 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
36a117a5
MM
4556 parm_depth = TMPL_PARMS_DEPTH (parmlist);
4557 arg_depth = TMPL_ARGS_DEPTH (arglist);
4558
4559 if (arg_depth == 1 && parm_depth > 1)
4560 {
39c01e4c 4561 /* We've been given an incomplete set of template arguments.
36a117a5
MM
4562 For example, given:
4563
4564 template <class T> struct S1 {
0cbd7506 4565 template <class U> struct S2 {};
36a117a5 4566 template <class U> struct S2<U*> {};
0cbd7506 4567 };
c8094d83 4568
36a117a5
MM
4569 we will be called with an ARGLIST of `U*', but the
4570 TEMPLATE will be `template <class T> template
4571 <class U> struct S1<T>::S2'. We must fill in the missing
4572 arguments. */
c8094d83 4573 arglist
7ac7b28f
MM
4574 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
4575 arglist);
36a117a5
MM
4576 arg_depth = TMPL_ARGS_DEPTH (arglist);
4577 }
5566b478 4578
41f5d4b1 4579 /* Now we should have enough arguments. */
50bc768d 4580 gcc_assert (parm_depth == arg_depth);
c8094d83 4581
7ac7b28f
MM
4582 /* From here on, we're only interested in the most general
4583 template. */
4584 template = gen_tmpl;
4585
36a117a5
MM
4586 /* Calculate the BOUND_ARGS. These will be the args that are
4587 actually tsubst'd into the definition to create the
4588 instantiation. */
4589 if (parm_depth > 1)
830bfa74
MM
4590 {
4591 /* We have multiple levels of arguments to coerce, at once. */
830bfa74 4592 int i;
e4a84209 4593 int saved_depth = TMPL_ARGS_DEPTH (arglist);
36a117a5 4594
f31c0a32 4595 tree bound_args = make_tree_vec (parm_depth);
c8094d83 4596
e4a84209 4597 for (i = saved_depth,
c8094d83 4598 t = DECL_TEMPLATE_PARMS (template);
e4a84209 4599 i > 0 && t != NULL_TREE;
830bfa74 4600 --i, t = TREE_CHAIN (t))
e4a84209 4601 {
ee3071ef
NS
4602 tree a = coerce_template_parms (TREE_VALUE (t),
4603 arglist, template,
3db45ab5 4604 complain,
e7e93965
MM
4605 /*require_all_args=*/true,
4606 /*use_default_args=*/true);
88e98cfe
KL
4607
4608 /* Don't process further if one of the levels fails. */
4609 if (a == error_mark_node)
4610 {
4611 /* Restore the ARGLIST to its full size. */
4612 TREE_VEC_LENGTH (arglist) = saved_depth;
4613 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
4614 }
c8094d83 4615
e4a84209
MM
4616 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
4617
4618 /* We temporarily reduce the length of the ARGLIST so
4619 that coerce_template_parms will see only the arguments
4620 corresponding to the template parameters it is
4621 examining. */
4622 TREE_VEC_LENGTH (arglist)--;
4623 }
4624
4625 /* Restore the ARGLIST to its full size. */
4626 TREE_VEC_LENGTH (arglist) = saved_depth;
4627
36a117a5 4628 arglist = bound_args;
830bfa74
MM
4629 }
4630 else
36a117a5
MM
4631 arglist
4632 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
f9a7ae04 4633 INNERMOST_TEMPLATE_ARGS (arglist),
f9c244b8 4634 template,
3db45ab5 4635 complain,
e7e93965
MM
4636 /*require_all_args=*/true,
4637 /*use_default_args=*/true);
36a117a5 4638
c353b8e3 4639 if (arglist == error_mark_node)
36a117a5 4640 /* We were unable to bind the arguments. */
fd295cb2 4641 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5566b478 4642
36a117a5
MM
4643 /* In the scope of a template class, explicit references to the
4644 template class refer to the type of the template, not any
4645 instantiation of it. For example, in:
c8094d83 4646
36a117a5
MM
4647 template <class T> class C { void f(C<T>); }
4648
4649 the `C<T>' is just the same as `C'. Outside of the
4650 class, however, such a reference is an instantiation. */
ed44da02 4651 if (comp_template_args (TYPE_TI_ARGS (template_type),
36a117a5
MM
4652 arglist))
4653 {
4654 found = template_type;
c8094d83 4655
36a117a5 4656 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5566b478 4657 {
36a117a5 4658 tree ctx;
c8094d83
MS
4659
4660 for (ctx = current_class_type;
5f04800c
KL
4661 ctx && TREE_CODE (ctx) != NAMESPACE_DECL;
4662 ctx = (TYPE_P (ctx)
4663 ? TYPE_CONTEXT (ctx)
4664 : DECL_CONTEXT (ctx)))
4665 if (TYPE_P (ctx) && same_type_p (ctx, template_type))
4666 goto found_ctx;
c8094d83 4667
6df5158a
NS
4668 /* We're not in the scope of the class, so the
4669 TEMPLATE_TYPE is not the type we want after all. */
4670 found = NULL_TREE;
4671 found_ctx:;
5566b478
MS
4672 }
4673 }
36a117a5 4674 if (found)
0cbd7506 4675 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
414ea4aa 4676
c7222c02
MM
4677 /* If we already have this specialization, return it. */
4678 found = retrieve_specialization (template, arglist,
4679 /*class_specializations_p=*/false);
4680 if (found)
4681 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
5566b478 4682
dbfe2124 4683 /* This type is a "partial instantiation" if any of the template
ab097535 4684 arguments still involve template parameters. Note that we set
486e4077
MM
4685 IS_PARTIAL_INSTANTIATION for partial specializations as
4686 well. */
dbfe2124
MM
4687 is_partial_instantiation = uses_template_parms (arglist);
4688
c353b8e3
MM
4689 /* If the deduced arguments are invalid, then the binding
4690 failed. */
4691 if (!is_partial_instantiation
4692 && check_instantiated_args (template,
4693 INNERMOST_TEMPLATE_ARGS (arglist),
4694 complain))
4695 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
c8094d83
MS
4696
4697 if (!is_partial_instantiation
3ebc5c52
MM
4698 && !PRIMARY_TEMPLATE_P (template)
4699 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
4700 {
3ebc5c52
MM
4701 found = xref_tag_from_type (TREE_TYPE (template),
4702 DECL_NAME (template),
29ef83de 4703 /*tag_scope=*/ts_global);
fd295cb2 4704 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
3ebc5c52 4705 }
c8094d83 4706
95ee998c 4707 context = tsubst (DECL_CONTEXT (template), arglist,
c2ea3a40 4708 complain, in_decl);
95ee998c
MM
4709 if (!context)
4710 context = global_namespace;
4711
36a117a5 4712 /* Create the type. */
ed44da02
MM
4713 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
4714 {
dbfe2124 4715 if (!is_partial_instantiation)
92777ce4
NS
4716 {
4717 set_current_access_from_decl (TYPE_NAME (template_type));
4718 t = start_enum (TYPE_IDENTIFIER (template_type));
4719 }
ed44da02 4720 else
dbfe2124 4721 /* We don't want to call start_enum for this type, since
ed44da02
MM
4722 the values for the enumeration constants may involve
4723 template parameters. And, no one should be interested
4724 in the enumeration constants for such a type. */
4725 t = make_node (ENUMERAL_TYPE);
4726 }
4727 else
4728 {
33848bb0 4729 t = make_aggr_type (TREE_CODE (template_type));
c8094d83 4730 CLASSTYPE_DECLARED_CLASS (t)
ed44da02 4731 = CLASSTYPE_DECLARED_CLASS (template_type);
ed44da02 4732 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
f668f160 4733 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
ae673f14
JM
4734
4735 /* A local class. Make sure the decl gets registered properly. */
4736 if (context == current_function_decl)
bd3d082e 4737 pushtag (DECL_NAME (template), t, /*tag_scope=*/ts_current);
ed44da02
MM
4738 }
4739
ae673f14
JM
4740 /* If we called start_enum or pushtag above, this information
4741 will already be set up. */
ed44da02
MM
4742 if (!TYPE_NAME (t))
4743 {
4744 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
c8094d83 4745
9188c363 4746 type_decl = create_implicit_typedef (DECL_NAME (template), t);
ed44da02 4747 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9188c363 4748 TYPE_STUB_DECL (t) = type_decl;
c8094d83 4749 DECL_SOURCE_LOCATION (type_decl)
f31686a3 4750 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
ed44da02
MM
4751 }
4752 else
4753 type_decl = TYPE_NAME (t);
36a117a5 4754
cab7a9a3
KL
4755 TREE_PRIVATE (type_decl)
4756 = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
4757 TREE_PROTECTED (type_decl)
4758 = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
12af7ba3
MM
4759 DECL_IN_SYSTEM_HEADER (type_decl)
4760 = DECL_IN_SYSTEM_HEADER (template);
8d039470
MS
4761 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
4762 {
4763 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
4764 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
4765 }
cab7a9a3 4766
9fbf56f7
MM
4767 /* Set up the template information. We have to figure out which
4768 template is the immediate parent if this is a full
4769 instantiation. */
4770 if (parm_depth == 1 || is_partial_instantiation
4771 || !PRIMARY_TEMPLATE_P (template))
4772 /* This case is easy; there are no member templates involved. */
4773 found = template;
4774 else
4775 {
ab097535
NS
4776 /* This is a full instantiation of a member template. Look
4777 for a partial instantiation of which this is an instance. */
9fbf56f7
MM
4778
4779 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4780 found; found = TREE_CHAIN (found))
4781 {
4782 int success;
4783 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4784
4785 /* We only want partial instantiations, here, not
4786 specializations or full instantiations. */
370af2d5 4787 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
9fbf56f7
MM
4788 || !uses_template_parms (TREE_VALUE (found)))
4789 continue;
4790
4791 /* Temporarily reduce by one the number of levels in the
4792 ARGLIST and in FOUND so as to avoid comparing the
4793 last set of arguments. */
4794 TREE_VEC_LENGTH (arglist)--;
4795 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4796
4797 /* See if the arguments match. If they do, then TMPL is
4798 the partial instantiation we want. */
4799 success = comp_template_args (TREE_PURPOSE (found), arglist);
4800
4801 /* Restore the argument vectors to their full size. */
4802 TREE_VEC_LENGTH (arglist)++;
4803 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4804
4805 if (success)
4806 {
4807 found = tmpl;
4808 break;
4809 }
4810 }
4811
4812 if (!found)
ab097535
NS
4813 {
4814 /* There was no partial instantiation. This happens
0cbd7506
MS
4815 where C<T> is a member template of A<T> and it's used
4816 in something like
c8094d83 4817
0cbd7506
MS
4818 template <typename T> struct B { A<T>::C<int> m; };
4819 B<float>;
c8094d83 4820
0cbd7506
MS
4821 Create the partial instantiation.
4822 */
4823 TREE_VEC_LENGTH (arglist)--;
4824 found = tsubst (template, arglist, complain, NULL_TREE);
4825 TREE_VEC_LENGTH (arglist)++;
4826 }
9fbf56f7
MM
4827 }
4828
c8094d83
MS
4829 SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
4830 DECL_TEMPLATE_INSTANTIATIONS (template)
4831 = tree_cons (arglist, t,
dbfe2124
MM
4832 DECL_TEMPLATE_INSTANTIATIONS (template));
4833
c8094d83 4834 if (TREE_CODE (t) == ENUMERAL_TYPE
dbfe2124 4835 && !is_partial_instantiation)
61a127b3
MM
4836 /* Now that the type has been registered on the instantiations
4837 list, we set up the enumerators. Because the enumeration
4838 constants may involve the enumeration type itself, we make
4839 sure to register the type first, and then create the
4840 constants. That way, doing tsubst_expr for the enumeration
4841 constants won't result in recursive calls here; we'll find
4842 the instantiation and exit above. */
4843 tsubst_enum (template_type, t, arglist);
dbfe2124 4844
36a117a5
MM
4845 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4846 is set up. */
ed44da02
MM
4847 if (TREE_CODE (t) != ENUMERAL_TYPE)
4848 DECL_NAME (type_decl) = classtype_mangled_name (t);
7813d14c 4849 if (is_partial_instantiation)
077e7015
MM
4850 /* If the type makes use of template parameters, the
4851 code that generates debugging information will crash. */
4852 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
8d08fdba 4853
b9e75696
JM
4854 /* Possibly limit visibility based on template args. */
4855 TREE_PUBLIC (type_decl) = 1;
4856 determine_visibility (type_decl);
4857
fd295cb2 4858 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
36a117a5 4859 }
fd295cb2 4860 timevar_pop (TV_NAME_LOOKUP);
8d08fdba
MS
4861}
4862\f
c8094d83 4863struct pair_fn_data
8d08fdba 4864{
8dfaeb63
MM
4865 tree_fn_t fn;
4866 void *data;
0c58f841 4867 struct pointer_set_t *visited;
8dfaeb63
MM
4868};
4869
4870/* Called from for_each_template_parm via walk_tree. */
581d38d0 4871
8dfaeb63 4872static tree
350fae66 4873for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
8dfaeb63
MM
4874{
4875 tree t = *tp;
4876 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
4877 tree_fn_t fn = pfd->fn;
4878 void *data = pfd->data;
4f2c9d7e 4879
2f939d94 4880 if (TYPE_P (t)
4f2c9d7e 4881 && for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
8dfaeb63 4882 return error_mark_node;
581d38d0 4883
8d08fdba
MS
4884 switch (TREE_CODE (t))
4885 {
8d08fdba 4886 case RECORD_TYPE:
9076e292 4887 if (TYPE_PTRMEMFUNC_P (t))
8dfaeb63 4888 break;
ed44da02
MM
4889 /* Fall through. */
4890
8d08fdba 4891 case UNION_TYPE:
ed44da02 4892 case ENUMERAL_TYPE:
8dfaeb63
MM
4893 if (!TYPE_TEMPLATE_INFO (t))
4894 *walk_subtrees = 0;
4895 else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
4f2c9d7e 4896 fn, data, pfd->visited))
8dfaeb63
MM
4897 return error_mark_node;
4898 break;
4899
588c2d1c 4900 case METHOD_TYPE:
8dfaeb63
MM
4901 /* Since we're not going to walk subtrees, we have to do this
4902 explicitly here. */
4f2c9d7e
MM
4903 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
4904 pfd->visited))
8dfaeb63 4905 return error_mark_node;
4890c2f4 4906 /* Fall through. */
588c2d1c
MM
4907
4908 case FUNCTION_TYPE:
8dfaeb63 4909 /* Check the return type. */
4f2c9d7e 4910 if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
8dfaeb63
MM
4911 return error_mark_node;
4912
588c2d1c
MM
4913 /* Check the parameter types. Since default arguments are not
4914 instantiated until they are needed, the TYPE_ARG_TYPES may
4915 contain expressions that involve template parameters. But,
4916 no-one should be looking at them yet. And, once they're
4917 instantiated, they don't contain template parameters, so
4918 there's no point in looking at them then, either. */
4919 {
4920 tree parm;
4921
4922 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4f2c9d7e
MM
4923 if (for_each_template_parm (TREE_VALUE (parm), fn, data,
4924 pfd->visited))
8dfaeb63 4925 return error_mark_node;
5566b478 4926
8dfaeb63
MM
4927 /* Since we've already handled the TYPE_ARG_TYPES, we don't
4928 want walk_tree walking into them itself. */
4929 *walk_subtrees = 0;
4930 }
4931 break;
3ac3d9ea 4932
a723baf1 4933 case TYPEOF_TYPE:
c8094d83 4934 if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
a723baf1
MM
4935 pfd->visited))
4936 return error_mark_node;
4937 break;
4938
8d08fdba 4939 case FUNCTION_DECL:
5566b478 4940 case VAR_DECL:
5566b478 4941 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
4f2c9d7e
MM
4942 && for_each_template_parm (DECL_TI_ARGS (t), fn, data,
4943 pfd->visited))
8dfaeb63
MM
4944 return error_mark_node;
4945 /* Fall through. */
4946
8d08fdba 4947 case PARM_DECL:
a723baf1
MM
4948 case CONST_DECL:
4949 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
4950 && for_each_template_parm (DECL_INITIAL (t), fn, data,
4951 pfd->visited))
4952 return error_mark_node;
c8094d83 4953 if (DECL_CONTEXT (t)
4f2c9d7e
MM
4954 && for_each_template_parm (DECL_CONTEXT (t), fn, data,
4955 pfd->visited))
8dfaeb63
MM
4956 return error_mark_node;
4957 break;
8d08fdba 4958
a1281f45 4959 case BOUND_TEMPLATE_TEMPLATE_PARM:
744fac59 4960 /* Record template parameters such as `T' inside `TT<T>'. */
4f2c9d7e 4961 if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
8dfaeb63
MM
4962 return error_mark_node;
4963 /* Fall through. */
4964
a1281f45 4965 case TEMPLATE_TEMPLATE_PARM:
744fac59 4966 case TEMPLATE_TYPE_PARM:
f84b4be9 4967 case TEMPLATE_PARM_INDEX:
8dfaeb63
MM
4968 if (fn && (*fn)(t, data))
4969 return error_mark_node;
4970 else if (!fn)
4971 return error_mark_node;
4972 break;
5156628f 4973
8dfaeb63 4974 case TEMPLATE_DECL:
f4f206f4 4975 /* A template template parameter is encountered. */
8dfaeb63 4976 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
4f2c9d7e 4977 && for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
8dfaeb63 4978 return error_mark_node;
db5ae43f 4979
8dfaeb63
MM
4980 /* Already substituted template template parameter */
4981 *walk_subtrees = 0;
4982 break;
b8865407 4983
4699c561 4984 case TYPENAME_TYPE:
c8094d83 4985 if (!fn
4f2c9d7e
MM
4986 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
4987 data, pfd->visited))
8dfaeb63
MM
4988 return error_mark_node;
4989 break;
4699c561 4990
8dfaeb63
MM
4991 case CONSTRUCTOR:
4992 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
4993 && for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4f2c9d7e
MM
4994 (TREE_TYPE (t)), fn, data,
4995 pfd->visited))
8dfaeb63
MM
4996 return error_mark_node;
4997 break;
c8094d83 4998
b8865407
MM
4999 case INDIRECT_REF:
5000 case COMPONENT_REF:
4699c561 5001 /* If there's no type, then this thing must be some expression
b8865407 5002 involving template parameters. */
4699c561 5003 if (!fn && !TREE_TYPE (t))
8dfaeb63
MM
5004 return error_mark_node;
5005 break;
b8865407 5006
42976354
BK
5007 case MODOP_EXPR:
5008 case CAST_EXPR:
5009 case REINTERPRET_CAST_EXPR:
5010 case CONST_CAST_EXPR:
5011 case STATIC_CAST_EXPR:
5012 case DYNAMIC_CAST_EXPR:
42976354
BK
5013 case ARROW_EXPR:
5014 case DOTSTAR_EXPR:
5015 case TYPEID_EXPR:
40242ccf 5016 case PSEUDO_DTOR_EXPR:
b8865407 5017 if (!fn)
8dfaeb63
MM
5018 return error_mark_node;
5019 break;
abff8e06 5020
bd9bb3d2
MM
5021 case BASELINK:
5022 /* If we do not handle this case specially, we end up walking
5023 the BINFO hierarchy, which is circular, and therefore
5024 confuses walk_tree. */
5025 *walk_subtrees = 0;
4f2c9d7e
MM
5026 if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
5027 pfd->visited))
bd9bb3d2
MM
5028 return error_mark_node;
5029 break;
5030
8d08fdba 5031 default:
8dfaeb63 5032 break;
8d08fdba 5033 }
8dfaeb63
MM
5034
5035 /* We didn't find any template parameters we liked. */
5036 return NULL_TREE;
5037}
5038
c8094d83
MS
5039/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
5040 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
a1281f45 5041 call FN with the parameter and the DATA.
838dfd8a 5042 If FN returns nonzero, the iteration is terminated, and
8dfaeb63 5043 for_each_template_parm returns 1. Otherwise, the iteration
838dfd8a 5044 continues. If FN never returns a nonzero value, the value
8dfaeb63
MM
5045 returned by for_each_template_parm is 0. If FN is NULL, it is
5046 considered to be the function which always returns 1. */
5047
5048static int
0c58f841
MA
5049for_each_template_parm (tree t, tree_fn_t fn, void* data,
5050 struct pointer_set_t *visited)
8dfaeb63
MM
5051{
5052 struct pair_fn_data pfd;
ad2ae3b2 5053 int result;
8dfaeb63
MM
5054
5055 /* Set up. */
5056 pfd.fn = fn;
5057 pfd.data = data;
5058
4890c2f4
MM
5059 /* Walk the tree. (Conceptually, we would like to walk without
5060 duplicates, but for_each_template_parm_r recursively calls
5061 for_each_template_parm, so we would need to reorganize a fair
4f2c9d7e
MM
5062 bit to use walk_tree_without_duplicates, so we keep our own
5063 visited list.) */
5064 if (visited)
5065 pfd.visited = visited;
5066 else
0c58f841 5067 pfd.visited = pointer_set_create ();
c8094d83
MS
5068 result = walk_tree (&t,
5069 for_each_template_parm_r,
ad2ae3b2 5070 &pfd,
c1e39976 5071 pfd.visited) != NULL_TREE;
ad2ae3b2
MM
5072
5073 /* Clean up. */
5074 if (!visited)
0c58f841
MA
5075 {
5076 pointer_set_destroy (pfd.visited);
5077 pfd.visited = 0;
5078 }
ad2ae3b2
MM
5079
5080 return result;
8d08fdba
MS
5081}
5082
d43f603d
KL
5083/* Returns true if T depends on any template parameter. */
5084
050367a3 5085int
3a978d72 5086uses_template_parms (tree t)
050367a3 5087{
c353b8e3
MM
5088 bool dependent_p;
5089 int saved_processing_template_decl;
5090
5091 saved_processing_template_decl = processing_template_decl;
5092 if (!saved_processing_template_decl)
5093 processing_template_decl = 1;
5094 if (TYPE_P (t))
5095 dependent_p = dependent_type_p (t);
5096 else if (TREE_CODE (t) == TREE_VEC)
5097 dependent_p = any_dependent_template_arguments_p (t);
5098 else if (TREE_CODE (t) == TREE_LIST)
5099 dependent_p = (uses_template_parms (TREE_VALUE (t))
5100 || uses_template_parms (TREE_CHAIN (t)));
3ce5fa4f
NS
5101 else if (TREE_CODE (t) == TYPE_DECL)
5102 dependent_p = dependent_type_p (TREE_TYPE (t));
c8094d83
MS
5103 else if (DECL_P (t)
5104 || EXPR_P (t)
c353b8e3
MM
5105 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
5106 || TREE_CODE (t) == OVERLOAD
5107 || TREE_CODE (t) == BASELINK
b207d6e2 5108 || TREE_CODE (t) == IDENTIFIER_NODE
6615c446 5109 || CONSTANT_CLASS_P (t))
c353b8e3
MM
5110 dependent_p = (type_dependent_expression_p (t)
5111 || value_dependent_expression_p (t));
315fb5db
NS
5112 else
5113 {
5114 gcc_assert (t == error_mark_node);
5115 dependent_p = false;
5116 }
c8094d83 5117
c353b8e3
MM
5118 processing_template_decl = saved_processing_template_decl;
5119
5120 return dependent_p;
050367a3
MM
5121}
5122
d43f603d
KL
5123/* Returns true if T depends on any template parameter with level LEVEL. */
5124
5125int
5126uses_template_parms_level (tree t, int level)
5127{
5128 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
5129}
5130
27fafc8d 5131static int tinst_depth;
e9f32eb5 5132extern int max_tinst_depth;
5566b478 5133#ifdef GATHER_STATISTICS
27fafc8d 5134int depth_reached;
5566b478 5135#endif
8dfaeb63
MM
5136static int tinst_level_tick;
5137static int last_template_error_tick;
8d08fdba 5138
3ae18eaf
JM
5139/* We're starting to instantiate D; record the template instantiation context
5140 for diagnostics and to restore it later. */
5141
aa9d8196 5142static int
3a978d72 5143push_tinst_level (tree d)
8d08fdba 5144{
3ae18eaf 5145 tree new;
8d08fdba 5146
7215f9a0
MS
5147 if (tinst_depth >= max_tinst_depth)
5148 {
8adf5b5e
JM
5149 /* If the instantiation in question still has unbound template parms,
5150 we don't really care if we can't instantiate it, so just return.
0cbd7506 5151 This happens with base instantiation for implicit `typename'. */
8adf5b5e
JM
5152 if (uses_template_parms (d))
5153 return 0;
5154
1139b3d8 5155 last_template_error_tick = tinst_level_tick;
0f51ccfc 5156 error ("template instantiation depth exceeds maximum of %d (use "
0cbd7506 5157 "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
b4f4233d 5158 max_tinst_depth, d);
5566b478 5159
cb753e49 5160 print_instantiation_context ();
5566b478 5161
7215f9a0
MS
5162 return 0;
5163 }
5164
12af7ba3
MM
5165 new = make_node (TINST_LEVEL);
5166 TINST_DECL (new) = d;
5167 TINST_LOCATION (new) = input_location;
5168 TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
3ae18eaf 5169 TREE_CHAIN (new) = current_tinst_level;
8d08fdba 5170 current_tinst_level = new;
5566b478 5171
7215f9a0 5172 ++tinst_depth;
5566b478
MS
5173#ifdef GATHER_STATISTICS
5174 if (tinst_depth > depth_reached)
5175 depth_reached = tinst_depth;
5176#endif
5177
27fafc8d 5178 ++tinst_level_tick;
7215f9a0 5179 return 1;
8d08fdba
MS
5180}
5181
3ae18eaf
JM
5182/* We're done instantiating this template; return to the instantiation
5183 context. */
5184
aa9d8196 5185static void
3a978d72 5186pop_tinst_level (void)
8d08fdba 5187{
3ae18eaf 5188 tree old = current_tinst_level;
8d08fdba 5189
ae58fa02
MM
5190 /* Restore the filename and line number stashed away when we started
5191 this instantiation. */
406d77a4 5192 input_location = TINST_LOCATION (old);
12af7ba3 5193 in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
3ae18eaf 5194 current_tinst_level = TREE_CHAIN (old);
7215f9a0 5195 --tinst_depth;
27fafc8d 5196 ++tinst_level_tick;
8d08fdba
MS
5197}
5198
3ae18eaf
JM
5199/* We're instantiating a deferred template; restore the template
5200 instantiation context in which the instantiation was requested, which
5201 is one step out from LEVEL. */
5202
5203static void
3a978d72 5204reopen_tinst_level (tree level)
3ae18eaf
JM
5205{
5206 tree t;
5207
5208 tinst_depth = 0;
5209 for (t = level; t; t = TREE_CHAIN (t))
5210 ++tinst_depth;
5211
5212 current_tinst_level = level;
5213 pop_tinst_level ();
5214}
5215
f84b4be9
JM
5216/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
5217 vector of template arguments, as for tsubst.
5218
dc957d14 5219 Returns an appropriate tsubst'd friend declaration. */
f84b4be9
JM
5220
5221static tree
3a978d72 5222tsubst_friend_function (tree decl, tree args)
f84b4be9
JM
5223{
5224 tree new_friend;
27fafc8d 5225
c8094d83 5226 if (TREE_CODE (decl) == FUNCTION_DECL
f84b4be9
JM
5227 && DECL_TEMPLATE_INSTANTIATION (decl)
5228 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
5229 /* This was a friend declared with an explicit template
5230 argument list, e.g.:
c8094d83 5231
f84b4be9 5232 friend void f<>(T);
c8094d83 5233
f84b4be9
JM
5234 to indicate that f was a template instantiation, not a new
5235 function declaration. Now, we have to figure out what
5236 instantiation of what template. */
5237 {
76e57b45 5238 tree template_id, arglist, fns;
f84b4be9
JM
5239 tree new_args;
5240 tree tmpl;
ed2fa432 5241 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
c8094d83 5242
76e57b45 5243 /* Friend functions are looked up in the containing namespace scope.
0cbd7506
MS
5244 We must enter that scope, to avoid finding member functions of the
5245 current cless with same name. */
76e57b45
NS
5246 push_nested_namespace (ns);
5247 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
015c2c66
MM
5248 tf_warning_or_error, NULL_TREE,
5249 /*integral_constant_expression_p=*/false);
76e57b45
NS
5250 pop_nested_namespace (ns);
5251 arglist = tsubst (DECL_TI_ARGS (decl), args,
23fca1f5 5252 tf_warning_or_error, NULL_TREE);
76e57b45 5253 template_id = lookup_template_function (fns, arglist);
c8094d83 5254
23fca1f5 5255 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
36a117a5 5256 tmpl = determine_specialization (template_id, new_friend,
c8094d83 5257 &new_args,
5fe7b654 5258 /*need_member_template=*/0,
29a1da1c
MM
5259 TREE_VEC_LENGTH (args),
5260 tsk_none);
6e432b31 5261 return instantiate_template (tmpl, new_args, tf_error);
f84b4be9 5262 }
36a117a5 5263
23fca1f5 5264 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
c8094d83 5265
36a117a5 5266 /* The NEW_FRIEND will look like an instantiation, to the
f84b4be9
JM
5267 compiler, but is not an instantiation from the point of view of
5268 the language. For example, we might have had:
c8094d83 5269
f84b4be9
JM
5270 template <class T> struct S {
5271 template <class U> friend void f(T, U);
5272 };
c8094d83 5273
f84b4be9
JM
5274 Then, in S<int>, template <class U> void f(int, U) is not an
5275 instantiation of anything. */
ac2b3222
AP
5276 if (new_friend == error_mark_node)
5277 return error_mark_node;
c8094d83 5278
f84b4be9
JM
5279 DECL_USE_TEMPLATE (new_friend) = 0;
5280 if (TREE_CODE (decl) == TEMPLATE_DECL)
655dc6ee
JM
5281 {
5282 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
5283 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
5284 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
5285 }
36a117a5 5286
92643fea
MM
5287 /* The mangled name for the NEW_FRIEND is incorrect. The function
5288 is not a template instantiation and should not be mangled like
5289 one. Therefore, we forget the mangling here; we'll recompute it
5290 later if we need it. */
36a117a5
MM
5291 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
5292 {
19e7881c 5293 SET_DECL_RTL (new_friend, NULL_RTX);
92643fea 5294 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
36a117a5 5295 }
c8094d83 5296
6eb3bb27 5297 if (DECL_NAMESPACE_SCOPE_P (new_friend))
f84b4be9 5298 {
36a117a5 5299 tree old_decl;
fbf1c34b
MM
5300 tree new_friend_template_info;
5301 tree new_friend_result_template_info;
92da7074 5302 tree ns;
fbf1c34b
MM
5303 int new_friend_is_defn;
5304
5305 /* We must save some information from NEW_FRIEND before calling
5306 duplicate decls since that function will free NEW_FRIEND if
5307 possible. */
5308 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
5bd61841 5309 new_friend_is_defn =
c8094d83 5310 (DECL_INITIAL (DECL_TEMPLATE_RESULT
5bd61841
MM
5311 (template_for_substitution (new_friend)))
5312 != NULL_TREE);
f84b4be9 5313 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
fbf1c34b
MM
5314 {
5315 /* This declaration is a `primary' template. */
5316 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
c8094d83 5317
fbf1c34b 5318 new_friend_result_template_info
17aec3eb 5319 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
fbf1c34b
MM
5320 }
5321 else
5bd61841 5322 new_friend_result_template_info = NULL_TREE;
36a117a5 5323
1c227897
MM
5324 /* Inside pushdecl_namespace_level, we will push into the
5325 current namespace. However, the friend function should go
c6002625 5326 into the namespace of the template. */
92da7074
ML
5327 ns = decl_namespace_context (new_friend);
5328 push_nested_namespace (ns);
d63d5d0c 5329 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
92da7074 5330 pop_nested_namespace (ns);
36a117a5 5331
0014c247
VR
5332 if (old_decl == error_mark_node)
5333 return error_mark_node;
5334
36a117a5
MM
5335 if (old_decl != new_friend)
5336 {
5337 /* This new friend declaration matched an existing
5338 declaration. For example, given:
5339
5340 template <class T> void f(T);
c8094d83
MS
5341 template <class U> class C {
5342 template <class T> friend void f(T) {}
36a117a5
MM
5343 };
5344
5345 the friend declaration actually provides the definition
5346 of `f', once C has been instantiated for some type. So,
5347 old_decl will be the out-of-class template declaration,
5348 while new_friend is the in-class definition.
5349
5350 But, if `f' was called before this point, the
5351 instantiation of `f' will have DECL_TI_ARGS corresponding
5352 to `T' but not to `U', references to which might appear
5353 in the definition of `f'. Previously, the most general
5354 template for an instantiation of `f' was the out-of-class
5355 version; now it is the in-class version. Therefore, we
5356 run through all specialization of `f', adding to their
5357 DECL_TI_ARGS appropriately. In particular, they need a
5358 new set of outer arguments, corresponding to the
c8094d83 5359 arguments for this class instantiation.
36a117a5
MM
5360
5361 The same situation can arise with something like this:
5362
5363 friend void f(int);
c8094d83 5364 template <class T> class C {
0cbd7506
MS
5365 friend void f(T) {}
5366 };
36a117a5
MM
5367
5368 when `C<int>' is instantiated. Now, `f(int)' is defined
5369 in the class. */
5370
fbf1c34b
MM
5371 if (!new_friend_is_defn)
5372 /* On the other hand, if the in-class declaration does
5373 *not* provide a definition, then we don't want to alter
5374 existing definitions. We can just leave everything
5375 alone. */
36a117a5 5376 ;
fbf1c34b 5377 else
36a117a5 5378 {
fbf1c34b
MM
5379 /* Overwrite whatever template info was there before, if
5380 any, with the new template information pertaining to
5381 the declaration. */
5382 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
5383
5384 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
8d83f792
MM
5385 reregister_specialization (new_friend,
5386 most_general_template (old_decl),
5387 old_decl);
c8094d83 5388 else
36a117a5 5389 {
fbf1c34b
MM
5390 tree t;
5391 tree new_friend_args;
5392
c8094d83 5393 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
fbf1c34b 5394 = new_friend_result_template_info;
c8094d83 5395
fbf1c34b 5396 new_friend_args = TI_ARGS (new_friend_template_info);
c8094d83 5397 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
fbf1c34b
MM
5398 t != NULL_TREE;
5399 t = TREE_CHAIN (t))
5400 {
5401 tree spec = TREE_VALUE (t);
c8094d83
MS
5402
5403 DECL_TI_ARGS (spec)
fbf1c34b
MM
5404 = add_outermost_template_args (new_friend_args,
5405 DECL_TI_ARGS (spec));
fbf1c34b
MM
5406 }
5407
5408 /* Now, since specializations are always supposed to
5409 hang off of the most general template, we must move
5410 them. */
5411 t = most_general_template (old_decl);
5412 if (t != old_decl)
5413 {
5414 DECL_TEMPLATE_SPECIALIZATIONS (t)
5415 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
5416 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
5417 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
5418 }
36a117a5
MM
5419 }
5420 }
5421
5422 /* The information from NEW_FRIEND has been merged into OLD_DECL
5423 by duplicate_decls. */
5424 new_friend = old_decl;
5425 }
f84b4be9 5426 }
6e432b31 5427 else
f84b4be9 5428 {
6e432b31
KL
5429 tree context = DECL_CONTEXT (new_friend);
5430 bool dependent_p;
5431
5432 /* In the code
5433 template <class T> class C {
5434 template <class U> friend void C1<U>::f (); // case 1
5435 friend void C2<T>::f (); // case 2
5436 };
5437 we only need to make sure CONTEXT is a complete type for
5438 case 2. To distinguish between the two cases, we note that
5439 CONTEXT of case 1 remains dependent type after tsubst while
5440 this isn't true for case 2. */
5441 ++processing_template_decl;
5442 dependent_p = dependent_type_p (context);
5443 --processing_template_decl;
5444
5445 if (!dependent_p
5446 && !complete_type_or_else (context, NULL_TREE))
5447 return error_mark_node;
5448
5449 if (COMPLETE_TYPE_P (context))
5450 {
5451 /* Check to see that the declaration is really present, and,
5452 possibly obtain an improved declaration. */
5453 tree fn = check_classfn (context,
5454 new_friend, NULL_TREE);
5455
5456 if (fn)
5457 new_friend = fn;
5458 }
f84b4be9
JM
5459 }
5460
5461 return new_friend;
5462}
5463
1aed5355
MM
5464/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
5465 template arguments, as for tsubst.
6757edfe 5466
dc957d14 5467 Returns an appropriate tsubst'd friend type or error_mark_node on
4b054b80 5468 failure. */
6757edfe
MM
5469
5470static tree
3a978d72 5471tsubst_friend_class (tree friend_tmpl, tree args)
6757edfe 5472{
1aed5355 5473 tree friend_type;
25aab5d0 5474 tree tmpl;
3e0ec82f 5475 tree context;
6757edfe 5476
3e0ec82f
MM
5477 context = DECL_CONTEXT (friend_tmpl);
5478
5479 if (context)
77adef84 5480 {
3e0ec82f
MM
5481 if (TREE_CODE (context) == NAMESPACE_DECL)
5482 push_nested_namespace (context);
5483 else
c8094d83 5484 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
3e0ec82f 5485 }
25aab5d0 5486
105d72c5
MM
5487 /* Look for a class template declaration. We look for hidden names
5488 because two friend declarations of the same template are the
5489 same. For example, in:
5490
5491 struct A {
5492 template <typename> friend class F;
5493 };
5494 template <typename> struct B {
5495 template <typename> friend class F;
5496 };
5497
5498 both F templates are the same. */
5499 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
5500 /*block_p=*/true, 0,
5501 LOOKUP_COMPLAIN | LOOKUP_HIDDEN);
25aab5d0 5502
3e0ec82f
MM
5503 /* But, if we don't find one, it might be because we're in a
5504 situation like this:
77adef84 5505
3e0ec82f
MM
5506 template <class T>
5507 struct S {
5508 template <class U>
5509 friend struct S;
5510 };
5511
5512 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
5513 for `S<int>', not the TEMPLATE_DECL. */
5514 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5515 {
10e6657a 5516 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
3e0ec82f 5517 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
25aab5d0 5518 }
6757edfe 5519
25aab5d0 5520 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6757edfe
MM
5521 {
5522 /* The friend template has already been declared. Just
36a117a5
MM
5523 check to see that the declarations match, and install any new
5524 default parameters. We must tsubst the default parameters,
5525 of course. We only need the innermost template parameters
5526 because that is all that redeclare_class_template will look
5527 at. */
3e0ec82f
MM
5528 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
5529 > TMPL_ARGS_DEPTH (args))
5530 {
5531 tree parms;
5532 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
23fca1f5 5533 args, tf_warning_or_error);
3e0ec82f
MM
5534 redeclare_class_template (TREE_TYPE (tmpl), parms);
5535 }
5536
6757edfe
MM
5537 friend_type = TREE_TYPE (tmpl);
5538 }
5539 else
5540 {
5541 /* The friend template has not already been declared. In this
5542 case, the instantiation of the template class will cause the
5543 injection of this template into the global scope. */
23fca1f5 5544 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
dca56f77
VR
5545 if (tmpl == error_mark_node)
5546 return error_mark_node;
6757edfe
MM
5547
5548 /* The new TMPL is not an instantiation of anything, so we
0cbd7506 5549 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
6757edfe
MM
5550 the new type because that is supposed to be the corresponding
5551 template decl, i.e., TMPL. */
5552 DECL_USE_TEMPLATE (tmpl) = 0;
5553 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
5554 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
68c78847
KL
5555 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
5556 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
6757edfe
MM
5557
5558 /* Inject this template into the global scope. */
d63d5d0c 5559 friend_type = TREE_TYPE (pushdecl_top_level_maybe_friend (tmpl, true));
6757edfe
MM
5560 }
5561
c8094d83 5562 if (context)
3e0ec82f
MM
5563 {
5564 if (TREE_CODE (context) == NAMESPACE_DECL)
5565 pop_nested_namespace (context);
5566 else
5567 pop_nested_class ();
5568 }
5569
6757edfe
MM
5570 return friend_type;
5571}
f84b4be9 5572
17f29637
KL
5573/* Returns zero if TYPE cannot be completed later due to circularity.
5574 Otherwise returns one. */
5575
d5372501 5576static int
3a978d72 5577can_complete_type_without_circularity (tree type)
17f29637
KL
5578{
5579 if (type == NULL_TREE || type == error_mark_node)
5580 return 0;
5581 else if (COMPLETE_TYPE_P (type))
5582 return 1;
5583 else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
5584 return can_complete_type_without_circularity (TREE_TYPE (type));
8c6ab2db
NS
5585 else if (CLASS_TYPE_P (type)
5586 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
17f29637
KL
5587 return 0;
5588 else
5589 return 1;
5590}
5591
8d08fdba 5592tree
3a978d72 5593instantiate_class_template (tree type)
8d08fdba 5594{
7088fca9 5595 tree template, args, pattern, t, member;
36a117a5 5596 tree typedecl;
dbbf88d1 5597 tree pbinfo;
cad7e87b 5598 tree base_list;
c8094d83 5599
5566b478 5600 if (type == error_mark_node)
8d08fdba
MS
5601 return error_mark_node;
5602
c8094d83 5603 if (TYPE_BEING_DEFINED (type)
ca099ac8
MM
5604 || COMPLETE_TYPE_P (type)
5605 || dependent_type_p (type))
5566b478
MS
5606 return type;
5607
6bdb985f 5608 /* Figure out which template is being instantiated. */
36a117a5 5609 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
50bc768d 5610 gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
73aad9b9 5611
4c571114
MM
5612 /* Determine what specialization of the original template to
5613 instantiate. */
916b63c3 5614 t = most_specialized_class (type, template);
8fbc5ae7 5615 if (t == error_mark_node)
73aad9b9 5616 {
8fbc5ae7
MM
5617 TYPE_BEING_DEFINED (type) = 1;
5618 return error_mark_node;
73aad9b9 5619 }
916b63c3
MM
5620 else if (t)
5621 {
5622 /* This TYPE is actually an instantiation of a partial
5623 specialization. We replace the innermost set of ARGS with
5624 the arguments appropriate for substitution. For example,
5625 given:
3db45ab5
MS
5626
5627 template <class T> struct S {};
916b63c3 5628 template <class T> struct S<T*> {};
6bdb985f 5629
916b63c3 5630 and supposing that we are instantiating S<int*>, ARGS will
3db45ab5 5631 presently be {int*} -- but we need {int}. */
916b63c3
MM
5632 pattern = TREE_TYPE (t);
5633 args = TREE_PURPOSE (t);
5634 }
73aad9b9 5635 else
916b63c3
MM
5636 {
5637 pattern = TREE_TYPE (template);
5638 args = CLASSTYPE_TI_ARGS (type);
5639 }
5566b478 5640
4c571114
MM
5641 /* If the template we're instantiating is incomplete, then clearly
5642 there's nothing we can do. */
d0f062fb 5643 if (!COMPLETE_TYPE_P (pattern))
f31c0a32 5644 return type;
5566b478 5645
4c571114
MM
5646 /* If we've recursively instantiated too many templates, stop. */
5647 if (! push_tinst_level (type))
f31c0a32 5648 return type;
4c571114
MM
5649
5650 /* Now we're really doing the instantiation. Mark the type as in
5651 the process of being defined. */
5652 TYPE_BEING_DEFINED (type) = 1;
5653
78757caa
KL
5654 /* We may be in the middle of deferred access check. Disable
5655 it now. */
5656 push_deferring_access_checks (dk_no_deferred);
5657
c353b8e3 5658 push_to_top_level ();
4c571114 5659
7813d14c 5660 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
8d08fdba 5661
68ea098a
NS
5662 /* Set the input location to the template definition. This is needed
5663 if tsubsting causes an error. */
12af7ba3
MM
5664 typedecl = TYPE_MAIN_DECL (type);
5665 input_location = DECL_SOURCE_LOCATION (typedecl);
5666 in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
68ea098a 5667
f7da6097 5668 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
834c6dff
MM
5669 TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
5670 TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
f7da6097 5671 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
f7da6097
MS
5672 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5673 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
f7da6097
MS
5674 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5675 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
5676 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5677 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
f7da6097
MS
5678 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5679 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
11cf4d18 5680 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
eff71ab0 5681 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6bdb8141
JM
5682 if (ANON_AGGR_TYPE_P (pattern))
5683 SET_ANON_AGGR_TYPE_P (type);
8d039470
MS
5684 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
5685 {
5686 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
5687 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
5688 }
f7da6097 5689
dbbf88d1 5690 pbinfo = TYPE_BINFO (pattern);
1456deaf 5691
315fb5db
NS
5692 /* We should never instantiate a nested class before its enclosing
5693 class; we need to look up the nested class by name before we can
5694 instantiate it, and that lookup should instantiate the enclosing
5695 class. */
5696 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
5697 || COMPLETE_TYPE_P (TYPE_CONTEXT (type))
5698 || TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
1456deaf 5699
cad7e87b 5700 base_list = NULL_TREE;
fa743e8c 5701 if (BINFO_N_BASE_BINFOS (pbinfo))
3fd71a52 5702 {
fa743e8c 5703 tree pbase_binfo;
a2507277 5704 tree context = TYPE_CONTEXT (type);
4514aa8c 5705 tree pushed_scope;
3fd71a52 5706 int i;
5566b478 5707
a2507277
NS
5708 /* We must enter the scope containing the type, as that is where
5709 the accessibility of types named in dependent bases are
5710 looked up from. */
4514aa8c 5711 pushed_scope = push_scope (context ? context : global_namespace);
c8094d83 5712
3fd71a52
MM
5713 /* Substitute into each of the bases to determine the actual
5714 basetypes. */
fa743e8c 5715 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
3fd71a52
MM
5716 {
5717 tree base;
fa743e8c 5718 tree access = BINFO_BASE_ACCESS (pbinfo, i);
711734a9 5719
dc957d14 5720 /* Substitute to figure out the base class. */
fa743e8c 5721 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
3fd71a52
MM
5722 if (base == error_mark_node)
5723 continue;
c8094d83 5724
3fd71a52 5725 base_list = tree_cons (access, base, base_list);
fa743e8c 5726 if (BINFO_VIRTUAL_P (pbase_binfo))
809e3e7f 5727 TREE_TYPE (base_list) = integer_type_node;
3fd71a52 5728 }
dfbcd65a 5729
3fd71a52
MM
5730 /* The list is now in reverse order; correct that. */
5731 base_list = nreverse (base_list);
5732
4514aa8c
NS
5733 if (pushed_scope)
5734 pop_scope (pushed_scope);
3fd71a52 5735 }
cad7e87b
NS
5736 /* Now call xref_basetypes to set up all the base-class
5737 information. */
5738 xref_basetypes (type, base_list);
5739
5566b478 5740
b74a0560
MM
5741 /* Now that our base classes are set up, enter the scope of the
5742 class, so that name lookups into base classes, etc. will work
dc957d14 5743 correctly. This is precisely analogous to what we do in
b74a0560
MM
5744 begin_class_definition when defining an ordinary non-template
5745 class. */
29370796 5746 pushclass (type);
b74a0560 5747
7088fca9 5748 /* Now members are processed in the order of declaration. */
8c6ab2db
NS
5749 for (member = CLASSTYPE_DECL_LIST (pattern);
5750 member; member = TREE_CHAIN (member))
8d08fdba 5751 {
7088fca9 5752 tree t = TREE_VALUE (member);
5566b478 5753
7088fca9 5754 if (TREE_PURPOSE (member))
ed44da02 5755 {
7088fca9
KL
5756 if (TYPE_P (t))
5757 {
5e0c54e5 5758 /* Build new CLASSTYPE_NESTED_UTDS. */
8d08fdba 5759
7088fca9 5760 tree newtag;
883a2bff
MM
5761 bool class_template_p;
5762
2678bae8
VR
5763 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
5764 && TYPE_LANG_SPECIFIC (t)
5765 && CLASSTYPE_IS_TEMPLATE (t));
883a2bff 5766 /* If the member is a class template, then -- even after
6c745393 5767 substitution -- there may be dependent types in the
883a2bff
MM
5768 template argument list for the class. We increment
5769 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
5770 that function will assume that no types are dependent
5771 when outside of a template. */
5772 if (class_template_p)
5773 ++processing_template_decl;
2678bae8 5774 newtag = tsubst (t, args, tf_error, NULL_TREE);
883a2bff
MM
5775 if (class_template_p)
5776 --processing_template_decl;
2620d095
KL
5777 if (newtag == error_mark_node)
5778 continue;
5779
7088fca9
KL
5780 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
5781 {
2678bae8
VR
5782 tree name = TYPE_IDENTIFIER (t);
5783
883a2bff 5784 if (class_template_p)
7088fca9
KL
5785 /* Unfortunately, lookup_template_class sets
5786 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
3afd2e20
NS
5787 instantiation (i.e., for the type of a member
5788 template class nested within a template class.)
5789 This behavior is required for
5790 maybe_process_partial_specialization to work
5791 correctly, but is not accurate in this case;
5792 the TAG is not an instantiation of anything.
5793 (The corresponding TEMPLATE_DECL is an
5794 instantiation, but the TYPE is not.) */
7088fca9
KL
5795 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5796
5797 /* Now, we call pushtag to put this NEWTAG into the scope of
5798 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5799 pushtag calling push_template_decl. We don't have to do
5800 this for enums because it will already have been done in
5801 tsubst_enum. */
5802 if (name)
5803 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
bd3d082e 5804 pushtag (name, newtag, /*tag_scope=*/ts_current);
7088fca9
KL
5805 }
5806 }
c8094d83 5807 else if (TREE_CODE (t) == FUNCTION_DECL
7088fca9
KL
5808 || DECL_FUNCTION_TEMPLATE_P (t))
5809 {
5810 /* Build new TYPE_METHODS. */
fecafe5e 5811 tree r;
c8094d83 5812
fecafe5e 5813 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5814 ++processing_template_decl;
fecafe5e
NS
5815 r = tsubst (t, args, tf_error, NULL_TREE);
5816 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5817 --processing_template_decl;
7088fca9 5818 set_current_access_from_decl (r);
7088fca9
KL
5819 finish_member_declaration (r);
5820 }
5821 else
5822 {
5823 /* Build new TYPE_FIELDS. */
fa8d6e85 5824
7088fca9
KL
5825 if (TREE_CODE (t) != CONST_DECL)
5826 {
5827 tree r;
fa8d6e85 5828
d479d37f
NS
5829 /* The the file and line for this declaration, to
5830 assist in error message reporting. Since we
5831 called push_tinst_level above, we don't need to
5832 restore these. */
f31686a3 5833 input_location = DECL_SOURCE_LOCATION (t);
fa8d6e85 5834
fb5ce3c9 5835 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5836 ++processing_template_decl;
23fca1f5 5837 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
fb5ce3c9 5838 if (TREE_CODE (t) == TEMPLATE_DECL)
9579624e 5839 --processing_template_decl;
7088fca9
KL
5840 if (TREE_CODE (r) == VAR_DECL)
5841 {
b794e321
MM
5842 /* In [temp.inst]:
5843
5844 [t]he initialization (and any associated
5845 side-effects) of a static data member does
5846 not occur unless the static data member is
5847 itself used in a way that requires the
5848 definition of the static data member to
3db45ab5 5849 exist.
b794e321
MM
5850
5851 Therefore, we do not substitute into the
3db45ab5
MS
5852 initialized for the static data member here. */
5853 finish_static_data_member_decl
5854 (r,
5855 /*init=*/NULL_TREE,
d174af6c 5856 /*init_const_expr_p=*/false,
3db45ab5 5857 /*asmspec_tree=*/NULL_TREE,
b794e321 5858 /*flags=*/0);
7088fca9
KL
5859 if (DECL_INITIALIZED_IN_CLASS_P (r))
5860 check_static_variable_definition (r, TREE_TYPE (r));
5861 }
5862 else if (TREE_CODE (r) == FIELD_DECL)
5863 {
5864 /* Determine whether R has a valid type and can be
5865 completed later. If R is invalid, then it is
5866 replaced by error_mark_node so that it will not be
5867 added to TYPE_FIELDS. */
5868 tree rtype = TREE_TYPE (r);
5869 if (can_complete_type_without_circularity (rtype))
5870 complete_type (rtype);
5871
5872 if (!COMPLETE_TYPE_P (rtype))
5873 {
5874 cxx_incomplete_type_error (r, rtype);
0cbd7506 5875 r = error_mark_node;
7088fca9
KL
5876 }
5877 }
5566b478 5878
7088fca9
KL
5879 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
5880 such a thing will already have been added to the field
5881 list by tsubst_enum in finish_member_declaration in the
5e0c54e5 5882 CLASSTYPE_NESTED_UTDS case above. */
7088fca9
KL
5883 if (!(TREE_CODE (r) == TYPE_DECL
5884 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
43b26a01 5885 && DECL_ARTIFICIAL (r)))
7088fca9
KL
5886 {
5887 set_current_access_from_decl (r);
5888 finish_member_declaration (r);
5889 }
0cbd7506 5890 }
7088fca9 5891 }
61fc8c9e 5892 }
7088fca9
KL
5893 else
5894 {
5895 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
5896 {
5897 /* Build new CLASSTYPE_FRIEND_CLASSES. */
5898
5899 tree friend_type = t;
b939a023 5900 bool adjust_processing_template_decl = false;
1aed5355 5901
7088fca9 5902 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
b939a023 5903 {
5a24482e 5904 /* template <class T> friend class C; */
b939a023 5905 friend_type = tsubst_friend_class (friend_type, args);
0cbd7506 5906 adjust_processing_template_decl = true;
b939a023
KL
5907 }
5908 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
5909 {
5a24482e 5910 /* template <class T> friend class C::D; */
b939a023 5911 friend_type = tsubst (friend_type, args,
23fca1f5 5912 tf_warning_or_error, NULL_TREE);
b939a023
KL
5913 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5914 friend_type = TREE_TYPE (friend_type);
0cbd7506 5915 adjust_processing_template_decl = true;
b939a023
KL
5916 }
5917 else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
5918 {
5a24482e
KL
5919 /* This could be either
5920
5921 friend class T::C;
5922
5923 when dependent_type_p is false or
5924
5925 template <class U> friend class T::C;
5926
5927 otherwise. */
b939a023 5928 friend_type = tsubst (friend_type, args,
23fca1f5 5929 tf_warning_or_error, NULL_TREE);
b939a023
KL
5930 /* Bump processing_template_decl for correct
5931 dependent_type_p calculation. */
5932 ++processing_template_decl;
5933 if (dependent_type_p (friend_type))
5934 adjust_processing_template_decl = true;
5935 --processing_template_decl;
5936 }
5a24482e
KL
5937 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
5938 && hidden_name_p (TYPE_NAME (friend_type)))
7088fca9 5939 {
5a24482e
KL
5940 /* friend class C;
5941
5942 where C hasn't been declared yet. Let's lookup name
5943 from namespace scope directly, bypassing any name that
5944 come from dependent base class. */
7088fca9
KL
5945 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
5946
5947 /* The call to xref_tag_from_type does injection for friend
5948 classes. */
5949 push_nested_namespace (ns);
c8094d83
MS
5950 friend_type =
5951 xref_tag_from_type (friend_type, NULL_TREE,
5a24482e 5952 /*tag_scope=*/ts_current);
7088fca9
KL
5953 pop_nested_namespace (ns);
5954 }
5a24482e
KL
5955 else if (uses_template_parms (friend_type))
5956 /* friend class C<T>; */
5957 friend_type = tsubst (friend_type, args,
23fca1f5 5958 tf_warning_or_error, NULL_TREE);
5a24482e
KL
5959 /* Otherwise it's
5960
5961 friend class C;
5962
5963 where C is already declared or
5964
5965 friend class C<int>;
5966
0cbd7506 5967 We don't have to do anything in these cases. */
1aed5355 5968
b939a023 5969 if (adjust_processing_template_decl)
7088fca9
KL
5970 /* Trick make_friend_class into realizing that the friend
5971 we're adding is a template, not an ordinary class. It's
5972 important that we use make_friend_class since it will
5973 perform some error-checking and output cross-reference
5974 information. */
5975 ++processing_template_decl;
fc378698 5976
b939a023 5977 if (friend_type != error_mark_node)
0cbd7506 5978 make_friend_class (type, friend_type, /*complain=*/false);
fc378698 5979
b939a023 5980 if (adjust_processing_template_decl)
7088fca9
KL
5981 --processing_template_decl;
5982 }
5983 else
9579624e
KL
5984 {
5985 /* Build new DECL_FRIENDLIST. */
5986 tree r;
5987
6e432b31
KL
5988 /* The the file and line for this declaration, to
5989 assist in error message reporting. Since we
5990 called push_tinst_level above, we don't need to
5991 restore these. */
5992 input_location = DECL_SOURCE_LOCATION (t);
5993
9579624e 5994 if (TREE_CODE (t) == TEMPLATE_DECL)
cad7e87b
NS
5995 {
5996 ++processing_template_decl;
5997 push_deferring_access_checks (dk_no_check);
5998 }
c8094d83 5999
9579624e 6000 r = tsubst_friend_function (t, args);
9579624e 6001 add_friend (type, r, /*complain=*/false);
cad7e87b
NS
6002 if (TREE_CODE (t) == TEMPLATE_DECL)
6003 {
6004 pop_deferring_access_checks ();
6005 --processing_template_decl;
6006 }
9579624e 6007 }
7088fca9
KL
6008 }
6009 }
5566b478 6010
61a127b3
MM
6011 /* Set the file and line number information to whatever is given for
6012 the class itself. This puts error messages involving generated
6013 implicit functions at a predictable point, and the same point
6014 that would be used for non-template classes. */
f31686a3 6015 input_location = DECL_SOURCE_LOCATION (typedecl);
6de9cd9a 6016
61a127b3 6017 unreverse_member_declarations (type);
9f33663b 6018 finish_struct_1 (type);
5524676d 6019 TYPE_BEING_DEFINED (type) = 0;
8d08fdba 6020
9188c363
MM
6021 /* Now that the class is complete, instantiate default arguments for
6022 any member functions. We don't do this earlier because the
6023 default arguments may reference members of the class. */
6024 if (!PRIMARY_TEMPLATE_P (template))
6025 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
c8094d83 6026 if (TREE_CODE (t) == FUNCTION_DECL
dc957d14 6027 /* Implicitly generated member functions will not have template
9188c363
MM
6028 information; they are not instantiations, but instead are
6029 created "fresh" for each instantiation. */
6030 && DECL_TEMPLATE_INFO (t))
6031 tsubst_default_arguments (t);
6032
b74a0560 6033 popclass ();
5566b478 6034 pop_from_top_level ();
78757caa 6035 pop_deferring_access_checks ();
5566b478
MS
6036 pop_tinst_level ();
6037
4684cd27
MM
6038 /* The vtable for a template class can be emitted in any translation
6039 unit in which the class is instantiated. When there is no key
6040 method, however, finish_struct_1 will already have added TYPE to
6041 the keyed_classes list. */
6042 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
9aad8f83
MA
6043 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
6044
5566b478 6045 return type;
8d08fdba
MS
6046}
6047
00d3396f 6048static tree
a91db711 6049tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
00d3396f 6050{
a91db711 6051 tree r;
c8094d83 6052
a91db711
NS
6053 if (!t)
6054 r = t;
6055 else if (TYPE_P (t))
6056 r = tsubst (t, args, complain, in_decl);
6057 else
00d3396f 6058 {
015c2c66
MM
6059 r = tsubst_expr (t, args, complain, in_decl,
6060 /*integral_constant_expression_p=*/true);
6f25cb35 6061 r = fold_non_dependent_expr (r);
bd83b409 6062 }
a91db711 6063 return r;
bd83b409
NS
6064}
6065
a91db711 6066/* Substitute ARGS into the vector or list of template arguments T. */
830bfa74 6067
e9659ab0 6068static tree
a91db711 6069tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
830bfa74 6070{
bf12d54d 6071 int len = TREE_VEC_LENGTH (t);
a91db711 6072 int need_new = 0, i;
67f5655f 6073 tree *elts = (tree *) alloca (len * sizeof (tree));
c8094d83 6074
830bfa74
MM
6075 for (i = 0; i < len; i++)
6076 {
bf12d54d
NS
6077 tree orig_arg = TREE_VEC_ELT (t, i);
6078 tree new_arg;
a91db711 6079
bf12d54d
NS
6080 if (TREE_CODE (orig_arg) == TREE_VEC)
6081 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
830bfa74 6082 else
a91db711 6083 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
c8094d83 6084
a91db711 6085 if (new_arg == error_mark_node)
08e72a19
JM
6086 return error_mark_node;
6087
a91db711
NS
6088 elts[i] = new_arg;
6089 if (new_arg != orig_arg)
830bfa74
MM
6090 need_new = 1;
6091 }
c8094d83 6092
830bfa74
MM
6093 if (!need_new)
6094 return t;
a91db711 6095
bf12d54d
NS
6096 t = make_tree_vec (len);
6097 for (i = 0; i < len; i++)
6098 TREE_VEC_ELT (t, i) = elts[i];
c8094d83 6099
830bfa74
MM
6100 return t;
6101}
6102
36a117a5
MM
6103/* Return the result of substituting ARGS into the template parameters
6104 given by PARMS. If there are m levels of ARGS and m + n levels of
6105 PARMS, then the result will contain n levels of PARMS. For
6106 example, if PARMS is `template <class T> template <class U>
6107 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
6108 result will be `template <int*, double, class V>'. */
6109
e9659ab0 6110static tree
3a978d72 6111tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
36a117a5 6112{
f71f87f9
MM
6113 tree r = NULL_TREE;
6114 tree* new_parms;
36a117a5 6115
8ca4bf25
MM
6116 /* When substituting into a template, we must set
6117 PROCESSING_TEMPLATE_DECL as the template parameters may be
6118 dependent if they are based on one-another, and the dependency
6119 predicates are short-circuit outside of templates. */
6120 ++processing_template_decl;
6121
36a117a5
MM
6122 for (new_parms = &r;
6123 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
6124 new_parms = &(TREE_CHAIN (*new_parms)),
6125 parms = TREE_CHAIN (parms))
6126 {
c8094d83 6127 tree new_vec =
36a117a5
MM
6128 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
6129 int i;
c8094d83 6130
36a117a5
MM
6131 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
6132 {
42b304f1
LM
6133 tree tuple;
6134 tree default_value;
6135 tree parm_decl;
6136
6137 if (parms == error_mark_node)
6138 continue;
6139
6140 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
6141
6142 if (tuple == error_mark_node)
6143 continue;
6144
6145 default_value = TREE_PURPOSE (tuple);
6146 parm_decl = TREE_VALUE (tuple);
833aa4c4
NS
6147
6148 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
a207780f
VR
6149 if (TREE_CODE (parm_decl) == PARM_DECL
6150 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
6151 parm_decl = error_mark_node;
a91db711
NS
6152 default_value = tsubst_template_arg (default_value, args,
6153 complain, NULL_TREE);
c8094d83 6154
a91db711 6155 tuple = build_tree_list (default_value, parm_decl);
833aa4c4 6156 TREE_VEC_ELT (new_vec, i) = tuple;
36a117a5 6157 }
c8094d83
MS
6158
6159 *new_parms =
6160 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
4890c2f4 6161 - TMPL_ARGS_DEPTH (args)),
36a117a5
MM
6162 new_vec, NULL_TREE);
6163 }
6164
8ca4bf25
MM
6165 --processing_template_decl;
6166
36a117a5
MM
6167 return r;
6168}
6169
ed44da02
MM
6170/* Substitute the ARGS into the indicated aggregate (or enumeration)
6171 type T. If T is not an aggregate or enumeration type, it is
6172 handled as if by tsubst. IN_DECL is as for tsubst. If
838dfd8a 6173 ENTERING_SCOPE is nonzero, T is the context for a template which
dc957d14 6174 we are presently tsubst'ing. Return the substituted value. */
36a117a5 6175
e9659ab0 6176static tree
c8094d83 6177tsubst_aggr_type (tree t,
0cbd7506
MS
6178 tree args,
6179 tsubst_flags_t complain,
6180 tree in_decl,
6181 int entering_scope)
36a117a5
MM
6182{
6183 if (t == NULL_TREE)
6184 return NULL_TREE;
6185
6186 switch (TREE_CODE (t))
6187 {
6188 case RECORD_TYPE:
6189 if (TYPE_PTRMEMFUNC_P (t))
46c895ac 6190 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
36a117a5 6191
f4f206f4 6192 /* Else fall through. */
ed44da02 6193 case ENUMERAL_TYPE:
36a117a5 6194 case UNION_TYPE:
5db698f6 6195 if (TYPE_TEMPLATE_INFO (t))
36a117a5
MM
6196 {
6197 tree argvec;
6198 tree context;
6199 tree r;
3489ea0c
MM
6200 bool saved_skip_evaluation;
6201
6202 /* In "sizeof(X<I>)" we need to evaluate "I". */
6203 saved_skip_evaluation = skip_evaluation;
6204 skip_evaluation = false;
36a117a5
MM
6205
6206 /* First, determine the context for the type we are looking
6207 up. */
4f7847ca
NS
6208 context = TYPE_CONTEXT (t);
6209 if (context)
6210 context = tsubst_aggr_type (context, args, complain,
36a117a5 6211 in_decl, /*entering_scope=*/1);
36a117a5
MM
6212
6213 /* Then, figure out what arguments are appropriate for the
6214 type we are trying to find. For example, given:
6215
6216 template <class T> struct S;
6217 template <class T, class U> void f(T, U) { S<U> su; }
6218
6219 and supposing that we are instantiating f<int, double>,
6220 then our ARGS will be {int, double}, but, when looking up
6221 S we only want {double}. */
a91db711
NS
6222 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
6223 complain, in_decl);
08e72a19 6224 if (argvec == error_mark_node)
3489ea0c
MM
6225 r = error_mark_node;
6226 else
6227 {
6228 r = lookup_template_class (t, argvec, in_decl, context,
6229 entering_scope, complain);
6230 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
6231 }
3db45ab5 6232
3489ea0c 6233 skip_evaluation = saved_skip_evaluation;
36a117a5 6234
3489ea0c 6235 return r;
36a117a5 6236 }
c8094d83 6237 else
36a117a5
MM
6238 /* This is not a template type, so there's nothing to do. */
6239 return t;
6240
6241 default:
4393e105 6242 return tsubst (t, args, complain, in_decl);
36a117a5
MM
6243 }
6244}
6245
9188c363
MM
6246/* Substitute into the default argument ARG (a default argument for
6247 FN), which has the indicated TYPE. */
6248
6249tree
3a978d72 6250tsubst_default_argument (tree fn, tree type, tree arg)
9188c363 6251{
2436b51f
MM
6252 tree saved_class_ptr = NULL_TREE;
6253 tree saved_class_ref = NULL_TREE;
6254
9188c363
MM
6255 /* This default argument came from a template. Instantiate the
6256 default argument here, not in tsubst. In the case of
c8094d83
MS
6257 something like:
6258
9188c363
MM
6259 template <class T>
6260 struct S {
6261 static T t();
6262 void f(T = t());
6263 };
c8094d83 6264
9188c363 6265 we must be careful to do name lookup in the scope of S<T>,
d5a10cf0 6266 rather than in the current class. */
2b59fc25 6267 push_access_scope (fn);
2436b51f
MM
6268 /* The "this" pointer is not valid in a default argument. */
6269 if (cfun)
6270 {
6271 saved_class_ptr = current_class_ptr;
6272 cp_function_chain->x_current_class_ptr = NULL_TREE;
6273 saved_class_ref = current_class_ref;
6274 cp_function_chain->x_current_class_ref = NULL_TREE;
6275 }
9188c363 6276
d5a10cf0 6277 push_deferring_access_checks(dk_no_deferred);
5a8613b2
MM
6278 /* The default argument expression may cause implicitly defined
6279 member functions to be synthesized, which will result in garbage
6280 collection. We must treat this situation as if we were within
6281 the body of function so as to avoid collecting live data on the
6282 stack. */
6283 ++function_depth;
c2ea3a40 6284 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
015c2c66
MM
6285 tf_warning_or_error, NULL_TREE,
6286 /*integral_constant_expression_p=*/false);
5a8613b2 6287 --function_depth;
d5a10cf0
MM
6288 pop_deferring_access_checks();
6289
2436b51f
MM
6290 /* Restore the "this" pointer. */
6291 if (cfun)
6292 {
6293 cp_function_chain->x_current_class_ptr = saved_class_ptr;
6294 cp_function_chain->x_current_class_ref = saved_class_ref;
6295 }
6296
2b59fc25 6297 pop_access_scope (fn);
9188c363
MM
6298
6299 /* Make sure the default argument is reasonable. */
6300 arg = check_default_argument (type, arg);
6301
6302 return arg;
6303}
6304
6305/* Substitute into all the default arguments for FN. */
6306
6307static void
3a978d72 6308tsubst_default_arguments (tree fn)
9188c363
MM
6309{
6310 tree arg;
6311 tree tmpl_args;
6312
6313 tmpl_args = DECL_TI_ARGS (fn);
6314
6315 /* If this function is not yet instantiated, we certainly don't need
6316 its default arguments. */
6317 if (uses_template_parms (tmpl_args))
6318 return;
6319
c8094d83
MS
6320 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
6321 arg;
9188c363
MM
6322 arg = TREE_CHAIN (arg))
6323 if (TREE_PURPOSE (arg))
c8094d83 6324 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
9188c363
MM
6325 TREE_VALUE (arg),
6326 TREE_PURPOSE (arg));
6327}
6328
fc6a28d7
MM
6329/* Substitute the ARGS into the T, which is a _DECL. Return the
6330 result of the substitution. Issue error and warning messages under
6331 control of COMPLAIN. */
00d3396f 6332
e9659ab0 6333static tree
fc6a28d7 6334tsubst_decl (tree t, tree args, tsubst_flags_t complain)
8d08fdba 6335{
82a98427 6336 location_t saved_loc;
b370501f 6337 tree r = NULL_TREE;
4b2811e9 6338 tree in_decl = t;
830bfa74 6339
ae58fa02 6340 /* Set the filename and linenumber to improve error-reporting. */
82a98427 6341 saved_loc = input_location;
f31686a3 6342 input_location = DECL_SOURCE_LOCATION (t);
b7484fbe 6343
8d08fdba
MS
6344 switch (TREE_CODE (t))
6345 {
98c1c668
JM
6346 case TEMPLATE_DECL:
6347 {
28e42b7e
KL
6348 /* We can get here when processing a member function template,
6349 member class template, and template template parameter of
6350 a template class. */
98c1c668 6351 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 6352 tree spec;
28e42b7e
KL
6353 tree tmpl_args;
6354 tree full_args;
98c1c668 6355
28e42b7e 6356 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
db2767b6 6357 {
28e42b7e
KL
6358 /* Template template parameter is treated here. */
6359 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6360 if (new_type == error_mark_node)
6361 return error_mark_node;
36a117a5 6362
28e42b7e
KL
6363 r = copy_decl (t);
6364 TREE_CHAIN (r) = NULL_TREE;
6365 TREE_TYPE (r) = new_type;
6366 DECL_TEMPLATE_RESULT (r)
6367 = build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
c8094d83 6368 DECL_TEMPLATE_PARMS (r)
28e42b7e
KL
6369 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
6370 complain);
6371 TYPE_NAME (new_type) = r;
6372 break;
6373 }
36a117a5 6374
28e42b7e
KL
6375 /* We might already have an instance of this template.
6376 The ARGS are for the surrounding class type, so the
6377 full args contain the tsubst'd args for the context,
6378 plus the innermost args from the template decl. */
c8094d83 6379 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
28e42b7e
KL
6380 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
6381 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
8ca4bf25
MM
6382 /* Because this is a template, the arguments will still be
6383 dependent, even after substitution. If
6384 PROCESSING_TEMPLATE_DECL is not set, the dependency
6385 predicates will short-circuit. */
6386 ++processing_template_decl;
28e42b7e
KL
6387 full_args = tsubst_template_args (tmpl_args, args,
6388 complain, in_decl);
8ca4bf25 6389 --processing_template_decl;
bf2f7328
VR
6390 if (full_args == error_mark_node)
6391 return error_mark_node;
28e42b7e
KL
6392
6393 /* tsubst_template_args doesn't copy the vector if
6394 nothing changed. But, *something* should have
6395 changed. */
6396 gcc_assert (full_args != tmpl_args);
6397
6398 spec = retrieve_specialization (t, full_args,
6399 /*class_specializations_p=*/true);
6400 if (spec != NULL_TREE)
6401 {
6402 r = spec;
6403 break;
db2767b6 6404 }
98c1c668
JM
6405
6406 /* Make a new template decl. It will be similar to the
c8094d83 6407 original, but will record the current template arguments.
98c1c668
JM
6408 We also create a new function declaration, which is just
6409 like the old one, but points to this new template, rather
6410 than the old one. */
0acf7199 6411 r = copy_decl (t);
50bc768d 6412 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
ae58fa02 6413 TREE_CHAIN (r) = NULL_TREE;
db2767b6 6414
ae58fa02 6415 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
6416
6417 if (TREE_CODE (decl) == TYPE_DECL)
6418 {
8ca4bf25
MM
6419 tree new_type;
6420 ++processing_template_decl;
6421 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
3db45ab5 6422 --processing_template_decl;
2620d095
KL
6423 if (new_type == error_mark_node)
6424 return error_mark_node;
6425
ae58fa02
MM
6426 TREE_TYPE (r) = new_type;
6427 CLASSTYPE_TI_TEMPLATE (new_type) = r;
17aec3eb 6428 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
ae58fa02 6429 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
8ca4bf25 6430 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
93cdc044
JM
6431 }
6432 else
6433 {
8ca4bf25
MM
6434 tree new_decl;
6435 ++processing_template_decl;
6436 new_decl = tsubst (decl, args, complain, in_decl);
6437 --processing_template_decl;
caec1dc0
KL
6438 if (new_decl == error_mark_node)
6439 return error_mark_node;
17aec3eb
RK
6440
6441 DECL_TEMPLATE_RESULT (r) = new_decl;
ae58fa02
MM
6442 DECL_TI_TEMPLATE (new_decl) = r;
6443 TREE_TYPE (r) = TREE_TYPE (new_decl);
6444 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
3db45ab5 6445 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
93cdc044
JM
6446 }
6447
ae58fa02
MM
6448 SET_DECL_IMPLICIT_INSTANTIATION (r);
6449 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
6450 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
6451
6452 /* The template parameters for this new template are all the
6453 template parameters for the old template, except the
c6002625 6454 outermost level of parameters. */
c8094d83 6455 DECL_TEMPLATE_PARMS (r)
4393e105 6456 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
c2ea3a40 6457 complain);
98c1c668 6458
93cdc044 6459 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 6460 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 6461
8c6ab2db
NS
6462 if (TREE_CODE (decl) != TYPE_DECL)
6463 /* Record this non-type partial instantiation. */
c8094d83 6464 register_specialization (r, t,
d63d5d0c
ILT
6465 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
6466 false);
98c1c668 6467 }
ae58fa02 6468 break;
8d08fdba
MS
6469
6470 case FUNCTION_DECL:
6471 {
386b8a85 6472 tree ctx;
87603ed0 6473 tree argvec = NULL_TREE;
cf38f48a 6474 tree *friends;
36a117a5 6475 tree gen_tmpl;
fc6a28d7 6476 tree type;
5566b478 6477 int member;
d8c4447d
MM
6478 int args_depth;
6479 int parms_depth;
5566b478 6480
36a117a5 6481 /* Nobody should be tsubst'ing into non-template functions. */
50bc768d 6482 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
36a117a5
MM
6483
6484 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
6485 {
6486 tree spec;
00cf3e31
MM
6487 bool dependent_p;
6488
6489 /* If T is not dependent, just return it. We have to
6490 increment PROCESSING_TEMPLATE_DECL because
6491 value_dependent_expression_p assumes that nothing is
6492 dependent when PROCESSING_TEMPLATE_DECL is zero. */
6493 ++processing_template_decl;
6494 dependent_p = value_dependent_expression_p (t);
6495 --processing_template_decl;
6496 if (!dependent_p)
6497 return t;
36a117a5
MM
6498
6499 /* Calculate the most general template of which R is a
6500 specialization, and the complete set of arguments used to
6501 specialize R. */
6502 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
c8094d83 6503 argvec = tsubst_template_args (DECL_TI_ARGS
a91db711 6504 (DECL_TEMPLATE_RESULT (gen_tmpl)),
c8094d83 6505 args, complain, in_decl);
36a117a5
MM
6506
6507 /* Check to see if we already have this specialization. */
c7222c02
MM
6508 spec = retrieve_specialization (gen_tmpl, argvec,
6509 /*class_specializations_p=*/false);
7ddedda4 6510
36a117a5 6511 if (spec)
ae58fa02
MM
6512 {
6513 r = spec;
6514 break;
6515 }
d8c4447d 6516
f9a7ae04
MM
6517 /* We can see more levels of arguments than parameters if
6518 there was a specialization of a member template, like
6519 this:
6520
0cbd7506 6521 template <class T> struct S { template <class U> void f(); }
c8094d83 6522 template <> template <class U> void S<int>::f(U);
f9a7ae04 6523
dc957d14 6524 Here, we'll be substituting into the specialization,
f9a7ae04
MM
6525 because that's where we can find the code we actually
6526 want to generate, but we'll have enough arguments for
c8094d83 6527 the most general template.
f9a7ae04
MM
6528
6529 We also deal with the peculiar case:
d8c4447d 6530
c8094d83 6531 template <class T> struct S {
d8c4447d
MM
6532 template <class U> friend void f();
6533 };
74b846e0 6534 template <class U> void f() {}
d8c4447d
MM
6535 template S<int>;
6536 template void f<double>();
6537
6538 Here, the ARGS for the instantiation of will be {int,
6539 double}. But, we only need as many ARGS as there are
6540 levels of template parameters in CODE_PATTERN. We are
6541 careful not to get fooled into reducing the ARGS in
6542 situations like:
6543
6544 template <class T> struct S { template <class U> void f(U); }
6545 template <class T> template <> void S<T>::f(int) {}
6546
6547 which we can spot because the pattern will be a
6548 specialization in this case. */
6549 args_depth = TMPL_ARGS_DEPTH (args);
c8094d83
MS
6550 parms_depth =
6551 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
d8c4447d
MM
6552 if (args_depth > parms_depth
6553 && !DECL_TEMPLATE_SPECIALIZATION (t))
f9a7ae04 6554 args = get_innermost_template_args (args, parms_depth);
36a117a5
MM
6555 }
6556 else
6557 {
6558 /* This special case arises when we have something like this:
6559
0cbd7506 6560 template <class T> struct S {
c8094d83 6561 friend void f<int>(int, double);
36a117a5
MM
6562 };
6563
6564 Here, the DECL_TI_TEMPLATE for the friend declaration
10b1d5e7
MM
6565 will be an IDENTIFIER_NODE. We are being called from
6566 tsubst_friend_function, and we want only to create a
6567 new decl (R) with appropriate types so that we can call
6568 determine_specialization. */
36a117a5
MM
6569 gen_tmpl = NULL_TREE;
6570 }
6571
6eb3bb27 6572 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 6573 {
5566b478
MS
6574 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
6575 member = 2;
6576 else
6577 member = 1;
c8094d83 6578 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
8c6ab2db 6579 complain, t, /*entering_scope=*/1);
5566b478
MS
6580 }
6581 else
6582 {
6583 member = 0;
4f1c5b7d 6584 ctx = DECL_CONTEXT (t);
5566b478 6585 }
fc6a28d7 6586 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
b3da7bb1
MM
6587 if (type == error_mark_node)
6588 return error_mark_node;
8d08fdba 6589
5566b478 6590 /* We do NOT check for matching decls pushed separately at this
0cbd7506
MS
6591 point, as they may not represent instantiations of this
6592 template, and in any case are considered separate under the
6593 discrete model. */
0acf7199 6594 r = copy_decl (t);
e1467ff2 6595 DECL_USE_TEMPLATE (r) = 0;
5566b478 6596 TREE_TYPE (r) = type;
92643fea
MM
6597 /* Clear out the mangled name and RTL for the instantiation. */
6598 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
6599 SET_DECL_RTL (r, NULL_RTX);
5bd61841 6600 DECL_INITIAL (r) = NULL_TREE;
4f1c5b7d 6601 DECL_CONTEXT (r) = ctx;
5566b478 6602
c8094d83 6603 if (member && DECL_CONV_FN_P (r))
1f84ec23
MM
6604 /* Type-conversion operator. Reconstruct the name, in
6605 case it's the name of one of the template's parameters. */
6606 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
5566b478 6607
4393e105 6608 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
c2ea3a40 6609 complain, t);
477f6664 6610 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
6611
6612 TREE_STATIC (r) = 0;
6613 TREE_PUBLIC (r) = TREE_PUBLIC (t);
6614 DECL_EXTERNAL (r) = 1;
4684cd27
MM
6615 /* If this is an instantiation of a function with internal
6616 linkage, we already know what object file linkage will be
6617 assigned to the instantiation. */
6618 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
711734a9
JM
6619 DECL_DEFER_OUTPUT (r) = 0;
6620 TREE_CHAIN (r) = NULL_TREE;
6621 DECL_PENDING_INLINE_INFO (r) = 0;
59026e79 6622 DECL_PENDING_INLINE_P (r) = 0;
655dc6ee 6623 DECL_SAVED_TREE (r) = NULL_TREE;
711734a9 6624 TREE_USED (r) = 0;
db9b2174
MM
6625 if (DECL_CLONED_FUNCTION (r))
6626 {
6627 DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
c2ea3a40 6628 args, complain, t);
db9b2174
MM
6629 TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
6630 TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
6631 }
711734a9 6632
92643fea
MM
6633 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
6634 this in the special friend case mentioned above where
6635 GEN_TMPL is NULL. */
36a117a5 6636 if (gen_tmpl)
386b8a85 6637 {
c8094d83 6638 DECL_TEMPLATE_INFO (r)
e1b3e07d 6639 = tree_cons (gen_tmpl, argvec, NULL_TREE);
36a117a5 6640 SET_DECL_IMPLICIT_INSTANTIATION (r);
d63d5d0c 6641 register_specialization (r, gen_tmpl, argvec, false);
36a117a5 6642
9188c363
MM
6643 /* We're not supposed to instantiate default arguments
6644 until they are called, for a template. But, for a
6645 declaration like:
6646
0cbd7506
MS
6647 template <class T> void f ()
6648 { extern void g(int i = T()); }
c8094d83 6649
9188c363
MM
6650 we should do the substitution when the template is
6651 instantiated. We handle the member function case in
6652 instantiate_class_template since the default arguments
6653 might refer to other members of the class. */
6654 if (!member
6655 && !PRIMARY_TEMPLATE_P (gen_tmpl)
6656 && !uses_template_parms (argvec))
6657 tsubst_default_arguments (r);
386b8a85 6658 }
c43e95f8
MM
6659 else
6660 DECL_TEMPLATE_INFO (r) = NULL_TREE;
f181d4ae 6661
cf38f48a
MM
6662 /* Copy the list of befriending classes. */
6663 for (friends = &DECL_BEFRIENDING_CLASSES (r);
6664 *friends;
c8094d83 6665 friends = &TREE_CHAIN (*friends))
cf38f48a
MM
6666 {
6667 *friends = copy_node (*friends);
6668 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
c2ea3a40 6669 args, complain,
cf38f48a
MM
6670 in_decl);
6671 }
6672
212e7048 6673 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
f181d4ae
MM
6674 {
6675 maybe_retrofit_in_chrg (r);
212e7048
MM
6676 if (DECL_CONSTRUCTOR_P (r))
6677 grok_ctor_properties (ctx, r);
2be678ff
JM
6678 /* If this is an instantiation of a member template, clone it.
6679 If it isn't, that'll be handled by
6680 clone_constructors_and_destructors. */
5e818b93 6681 if (PRIMARY_TEMPLATE_P (gen_tmpl))
271e6f02 6682 clone_function_decl (r, /*update_method_vec_p=*/0);
f181d4ae 6683 }
398cd199
VR
6684 else if (IDENTIFIER_OPNAME_P (DECL_NAME (r))
6685 && !grok_op_properties (r, (complain & tf_error) != 0))
6686 return error_mark_node;
c8b2e872
MM
6687
6688 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
6689 SET_DECL_FRIEND_CONTEXT (r,
6690 tsubst (DECL_FRIEND_CONTEXT (t),
6691 args, complain, in_decl));
b9e75696
JM
6692
6693 /* Possibly limit visibility based on template args. */
6694 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
b70f0f48
JM
6695 if (DECL_VISIBILITY_SPECIFIED (t))
6696 {
6697 DECL_VISIBILITY_SPECIFIED (r) = 0;
6698 DECL_ATTRIBUTES (r)
6699 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6700 }
b9e75696 6701 determine_visibility (r);
8d08fdba 6702 }
ae58fa02 6703 break;
8d08fdba
MS
6704
6705 case PARM_DECL:
6706 {
fc6a28d7
MM
6707 tree type;
6708
ae58fa02 6709 r = copy_node (t);
833aa4c4
NS
6710 if (DECL_TEMPLATE_PARM_P (t))
6711 SET_DECL_TEMPLATE_PARM_P (r);
8e51619a 6712
fc6a28d7 6713 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
02bab9db 6714 type = type_decays_to (type);
5566b478 6715 TREE_TYPE (r) = type;
9804209d 6716 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1 6717
560ad596
MM
6718 if (DECL_INITIAL (r))
6719 {
6720 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
6721 DECL_INITIAL (r) = TREE_TYPE (r);
6722 else
6723 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
6724 complain, in_decl);
6725 }
db2767b6 6726
5566b478 6727 DECL_CONTEXT (r) = NULL_TREE;
8e51619a
JM
6728
6729 if (!DECL_TEMPLATE_PARM_P (r))
6730 DECL_ARG_TYPE (r) = type_passed_as (type);
8d08fdba 6731 if (TREE_CHAIN (t))
4393e105 6732 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
c2ea3a40 6733 complain, TREE_CHAIN (t));
8d08fdba 6734 }
ae58fa02 6735 break;
8d08fdba 6736
5566b478
MS
6737 case FIELD_DECL:
6738 {
fc6a28d7
MM
6739 tree type;
6740
0acf7199 6741 r = copy_decl (t);
fc6a28d7
MM
6742 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6743 if (type == error_mark_node)
6744 return error_mark_node;
1b8899d1 6745 TREE_TYPE (r) = type;
9804209d 6746 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
1b8899d1 6747
015c2c66
MM
6748 /* DECL_INITIAL gives the number of bits in a bit-field. */
6749 DECL_INITIAL (r)
6750 = tsubst_expr (DECL_INITIAL (t), args,
6751 complain, in_decl,
6752 /*integral_constant_expression_p=*/true);
1b8899d1
MM
6753 /* We don't have to set DECL_CONTEXT here; it is set by
6754 finish_member_declaration. */
5566b478 6755 TREE_CHAIN (r) = NULL_TREE;
c8094d83 6756 if (VOID_TYPE_P (type))
dee15844 6757 error ("instantiation of %q+D as type %qT", r, type);
5566b478 6758 }
ae58fa02 6759 break;
5566b478
MS
6760
6761 case USING_DECL:
98ed9dae
NS
6762 /* We reach here only for member using decls. */
6763 if (DECL_DEPENDENT_P (t))
6764 {
6765 r = do_class_using_decl
6766 (tsubst_copy (USING_DECL_SCOPE (t), args, complain, in_decl),
6767 tsubst_copy (DECL_NAME (t), args, complain, in_decl));
6768 if (!r)
6769 r = error_mark_node;
6770 }
6771 else
6772 {
6773 r = copy_node (t);
6774 TREE_CHAIN (r) = NULL_TREE;
6775 }
ae58fa02 6776 break;
5566b478 6777
9188c363 6778 case TYPE_DECL:
5566b478
MS
6779 case VAR_DECL:
6780 {
1cea0434
KG
6781 tree argvec = NULL_TREE;
6782 tree gen_tmpl = NULL_TREE;
36a117a5 6783 tree spec;
1cea0434 6784 tree tmpl = NULL_TREE;
9188c363 6785 tree ctx;
fc6a28d7 6786 tree type = NULL_TREE;
f604fc3b 6787 bool local_p;
9188c363 6788
fc6a28d7
MM
6789 if (TREE_CODE (t) == TYPE_DECL)
6790 {
6791 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6792 if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
6793 || t == TYPE_MAIN_DECL (TREE_TYPE (t)))
6794 {
6795 /* If this is the canonical decl, we don't have to
6796 mess with instantiations, and often we can't (for
6797 typename, template type parms and such). Note that
6798 TYPE_NAME is not correct for the above test if
6799 we've copied the type for a typedef. */
6800 r = TYPE_NAME (type);
6801 break;
6802 }
6803 }
c8094d83 6804
f604fc3b
MM
6805 /* Check to see if we already have the specialization we
6806 need. */
6807 spec = NULL_TREE;
6808 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
6809 {
6810 /* T is a static data member or namespace-scope entity.
6811 We have to substitute into namespace-scope variables
6812 (even though such entities are never templates) because
6813 of cases like:
6814
6815 template <class T> void f() { extern T t; }
6816
6817 where the entity referenced is not known until
6818 instantiation time. */
6819 local_p = false;
6820 ctx = DECL_CONTEXT (t);
6821 if (DECL_CLASS_SCOPE_P (t))
6822 {
6823 ctx = tsubst_aggr_type (ctx, args,
6824 complain,
6825 in_decl, /*entering_scope=*/1);
6826 /* If CTX is unchanged, then T is in fact the
6827 specialization we want. That situation occurs when
6828 referencing a static data member within in its own
6829 class. We can use pointer equality, rather than
6830 same_type_p, because DECL_CONTEXT is always
6831 canonical. */
6832 if (ctx == DECL_CONTEXT (t))
6833 spec = t;
6834 }
5566b478 6835
f604fc3b
MM
6836 if (!spec)
6837 {
6838 tmpl = DECL_TI_TEMPLATE (t);
6839 gen_tmpl = most_general_template (tmpl);
6840 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
6841 spec = (retrieve_specialization
6842 (gen_tmpl, argvec,
6843 /*class_specializations_p=*/false));
6844 }
6845 }
9188c363 6846 else
6dfbb909 6847 {
f604fc3b
MM
6848 /* A local variable. */
6849 local_p = true;
6dfbb909
MM
6850 /* Subsequent calls to pushdecl will fill this in. */
6851 ctx = NULL_TREE;
f604fc3b 6852 spec = retrieve_local_specialization (t);
6dfbb909 6853 }
f604fc3b
MM
6854 /* If we already have the specialization we need, there is
6855 nothing more to do. */
36a117a5 6856 if (spec)
ae58fa02
MM
6857 {
6858 r = spec;
6859 break;
6860 }
5566b478 6861
f604fc3b 6862 /* Create a new node for the specialization we need. */
0acf7199 6863 r = copy_decl (t);
edebf865 6864 if (TREE_CODE (r) == VAR_DECL)
39703eb9 6865 {
8b0a8500
MM
6866 /* Even if the original location is out of scope, the
6867 newly substituted one is not. */
6868 DECL_DEAD_FOR_LOCAL (r) = 0;
6869 DECL_INITIALIZED_P (r) = 0;
6870 DECL_TEMPLATE_INSTANTIATED (r) = 0;
fc6a28d7
MM
6871 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6872 if (type == error_mark_node)
6873 return error_mark_node;
39703eb9
MM
6874 type = complete_type (type);
6875 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
6876 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
62e19030 6877 type = check_var_type (DECL_NAME (r), type);
56b4ea3d
RH
6878
6879 if (DECL_HAS_VALUE_EXPR_P (t))
6880 {
6881 tree ve = DECL_VALUE_EXPR (t);
015c2c66
MM
6882 ve = tsubst_expr (ve, args, complain, in_decl,
6883 /*constant_expression_p=*/false);
3db45ab5 6884 SET_DECL_VALUE_EXPR (r, ve);
56b4ea3d 6885 }
39703eb9 6886 }
a3d87771
MM
6887 else if (DECL_SELF_REFERENCE_P (t))
6888 SET_DECL_SELF_REFERENCE_P (r);
01f4137f 6889 TREE_TYPE (r) = type;
9804209d 6890 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
5566b478 6891 DECL_CONTEXT (r) = ctx;
92643fea
MM
6892 /* Clear out the mangled name and RTL for the instantiation. */
6893 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
820cc88f
DB
6894 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6895 SET_DECL_RTL (r, NULL_RTX);
8b0a8500
MM
6896 /* The initializer must not be expanded until it is required;
6897 see [temp.inst]. */
d11ad92e 6898 DECL_INITIAL (r) = NULL_TREE;
820cc88f
DB
6899 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
6900 SET_DECL_RTL (r, NULL_RTX);
06ceef4e 6901 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
b9e75696
JM
6902 if (TREE_CODE (r) == VAR_DECL)
6903 {
6904 /* Possibly limit visibility based on template args. */
6905 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
b70f0f48
JM
6906 if (DECL_VISIBILITY_SPECIFIED (t))
6907 {
6908 DECL_VISIBILITY_SPECIFIED (r) = 0;
6909 DECL_ATTRIBUTES (r)
6910 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
6911 }
b9e75696
JM
6912 determine_visibility (r);
6913 }
5566b478 6914
6dfbb909
MM
6915 if (!local_p)
6916 {
6917 /* A static data member declaration is always marked
6918 external when it is declared in-class, even if an
6919 initializer is present. We mimic the non-template
6920 processing here. */
6921 DECL_EXTERNAL (r) = 1;
fa8d6e85 6922
d63d5d0c 6923 register_specialization (r, gen_tmpl, argvec, false);
6dfbb909
MM
6924 DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
6925 SET_DECL_IMPLICIT_INSTANTIATION (r);
6926 }
9188c363 6927 else
6dfbb909 6928 register_local_specialization (r, t);
5566b478 6929
5566b478 6930 TREE_CHAIN (r) = NULL_TREE;
edebf865 6931 layout_decl (r, 0);
5566b478 6932 }
ae58fa02 6933 break;
5566b478 6934
ae58fa02 6935 default:
315fb5db 6936 gcc_unreachable ();
c8094d83 6937 }
ae58fa02
MM
6938
6939 /* Restore the file and line information. */
82a98427 6940 input_location = saved_loc;
ae58fa02
MM
6941
6942 return r;
6943}
6944
34cd5ae7 6945/* Substitute into the ARG_TYPES of a function type. */
cabc336a 6946
e9659ab0 6947static tree
c8094d83 6948tsubst_arg_types (tree arg_types,
0cbd7506
MS
6949 tree args,
6950 tsubst_flags_t complain,
6951 tree in_decl)
cabc336a
MM
6952{
6953 tree remaining_arg_types;
cabc336a 6954 tree type;
5e97d404
NS
6955 tree default_arg;
6956 tree result = NULL_TREE;
cabc336a
MM
6957
6958 if (!arg_types || arg_types == void_list_node)
6959 return arg_types;
c8094d83 6960
cabc336a 6961 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
4393e105
MM
6962 args, complain, in_decl);
6963 if (remaining_arg_types == error_mark_node)
6964 return error_mark_node;
6965
6966 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6967 if (type == error_mark_node)
6968 return error_mark_node;
4b2811e9
NS
6969 if (VOID_TYPE_P (type))
6970 {
c2ea3a40 6971 if (complain & tf_error)
0cbd7506
MS
6972 {
6973 error ("invalid parameter type %qT", type);
6974 if (in_decl)
dee15844 6975 error ("in declaration %q+D", in_decl);
0cbd7506 6976 }
4b2811e9
NS
6977 return error_mark_node;
6978 }
cabc336a 6979
4393e105
MM
6980 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6981 top-level qualifiers as required. */
6982 type = TYPE_MAIN_VARIANT (type_decays_to (type));
cabc336a 6983
5e97d404
NS
6984 /* We do not substitute into default arguments here. The standard
6985 mandates that they be instantiated only when needed, which is
6986 done in build_over_call. */
6987 default_arg = TREE_PURPOSE (arg_types);
c8094d83 6988
5e97d404
NS
6989 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
6990 {
6991 /* We've instantiated a template before its default arguments
0cbd7506
MS
6992 have been parsed. This can happen for a nested template
6993 class, and is not an error unless we require the default
6994 argument in a call of this function. */
5e97d404 6995 result = tree_cons (default_arg, type, remaining_arg_types);
01ea1ea8 6996 VEC_safe_push (tree, gc, DEFARG_INSTANTIATIONS (default_arg), result);
5e97d404
NS
6997 }
6998 else
6999 result = hash_tree_cons (default_arg, type, remaining_arg_types);
c8094d83 7000
5e97d404 7001 return result;
cabc336a
MM
7002}
7003
4393e105
MM
7004/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
7005 *not* handle the exception-specification for FNTYPE, because the
7006 initial substitution of explicitly provided template parameters
7007 during argument deduction forbids substitution into the
7008 exception-specification:
7009
7010 [temp.deduct]
7011
7012 All references in the function type of the function template to the
7013 corresponding template parameters are replaced by the specified tem-
7014 plate argument values. If a substitution in a template parameter or
7015 in the function type of the function template results in an invalid
7016 type, type deduction fails. [Note: The equivalent substitution in
7017 exception specifications is done only when the function is instanti-
7018 ated, at which point a program is ill-formed if the substitution
7019 results in an invalid type.] */
7020
7021static tree
c8094d83 7022tsubst_function_type (tree t,
0cbd7506
MS
7023 tree args,
7024 tsubst_flags_t complain,
7025 tree in_decl)
4393e105
MM
7026{
7027 tree return_type;
7028 tree arg_types;
7029 tree fntype;
7030
8dd3f57a 7031 /* The TYPE_CONTEXT is not used for function/method types. */
50bc768d 7032 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
4393e105 7033
46c895ac 7034 /* Substitute the return type. */
4393e105
MM
7035 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
7036 if (return_type == error_mark_node)
7037 return error_mark_node;
6e2993bf
MM
7038 /* The standard does not presently indicate that creation of a
7039 function type with an invalid return type is a deduction failure.
270af55d 7040 However, that is clearly analogous to creating an array of "void"
c8094d83 7041 or a reference to a reference. This is core issue #486. */
6e2993bf
MM
7042 if (TREE_CODE (return_type) == ARRAY_TYPE
7043 || TREE_CODE (return_type) == FUNCTION_TYPE)
7044 {
7045 if (complain & tf_error)
7046 {
7047 if (TREE_CODE (return_type) == ARRAY_TYPE)
7048 error ("function returning an array");
7049 else
7050 error ("function returning a function");
7051 }
7052 return error_mark_node;
7053 }
4393e105 7054
34cd5ae7 7055 /* Substitute the argument types. */
4393e105 7056 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
c8094d83 7057 complain, in_decl);
4393e105
MM
7058 if (arg_types == error_mark_node)
7059 return error_mark_node;
c8094d83 7060
4393e105
MM
7061 /* Construct a new type node and return it. */
7062 if (TREE_CODE (t) == FUNCTION_TYPE)
7063 fntype = build_function_type (return_type, arg_types);
7064 else
7065 {
7066 tree r = TREE_TYPE (TREE_VALUE (arg_types));
7067 if (! IS_AGGR_TYPE (r))
7068 {
7069 /* [temp.deduct]
c8094d83 7070
4393e105
MM
7071 Type deduction may fail for any of the following
7072 reasons:
c8094d83 7073
4393e105
MM
7074 -- Attempting to create "pointer to member of T" when T
7075 is not a class type. */
c2ea3a40 7076 if (complain & tf_error)
0f51ccfc 7077 error ("creating pointer to member function of non-class type %qT",
4393e105
MM
7078 r);
7079 return error_mark_node;
7080 }
c8094d83
MS
7081
7082 fntype = build_method_type_directly (r, return_type,
43dc123f 7083 TREE_CHAIN (arg_types));
4393e105 7084 }
c2ea3a40 7085 fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
e9525111 7086 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
c8094d83
MS
7087
7088 return fntype;
4393e105
MM
7089}
7090
c7222c02
MM
7091/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
7092 ARGS into that specification, and return the substituted
7093 specification. If there is no specification, return NULL_TREE. */
7094
7095static tree
c8094d83
MS
7096tsubst_exception_specification (tree fntype,
7097 tree args,
c7222c02
MM
7098 tsubst_flags_t complain,
7099 tree in_decl)
7100{
7101 tree specs;
7102 tree new_specs;
7103
7104 specs = TYPE_RAISES_EXCEPTIONS (fntype);
7105 new_specs = NULL_TREE;
7106 if (specs)
7107 {
7108 if (! TREE_VALUE (specs))
7109 new_specs = specs;
7110 else
7111 while (specs)
7112 {
7113 tree spec;
7114 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
7115 if (spec == error_mark_node)
7116 return spec;
7117 new_specs = add_exception_specifier (new_specs, spec, complain);
7118 specs = TREE_CHAIN (specs);
7119 }
7120 }
7121 return new_specs;
7122}
7123
4393e105
MM
7124/* Take the tree structure T and replace template parameters used
7125 therein with the argument vector ARGS. IN_DECL is an associated
7126 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
c2ea3a40
NS
7127 Issue error and warning messages under control of COMPLAIN. Note
7128 that we must be relatively non-tolerant of extensions here, in
7129 order to preserve conformance; if we allow substitutions that
7130 should not be allowed, we may allow argument deductions that should
7131 not succeed, and therefore report ambiguous overload situations
7132 where there are none. In theory, we could allow the substitution,
7133 but indicate that it should have failed, and allow our caller to
7134 make sure that the right thing happens, but we don't try to do this
7135 yet.
4393e105
MM
7136
7137 This function is used for dealing with types, decls and the like;
7138 for expressions, use tsubst_expr or tsubst_copy. */
ae58fa02 7139
14d22dd6 7140static tree
3a978d72 7141tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
ae58fa02 7142{
0ecfe0b4 7143 tree type, r;
ae58fa02
MM
7144
7145 if (t == NULL_TREE || t == error_mark_node
7146 || t == integer_type_node
7147 || t == void_type_node
7148 || t == char_type_node
539599c1 7149 || t == unknown_type_node
ae58fa02
MM
7150 || TREE_CODE (t) == NAMESPACE_DECL)
7151 return t;
7152
fc6a28d7
MM
7153 if (DECL_P (t))
7154 return tsubst_decl (t, args, complain);
7155
ae58fa02
MM
7156 if (TREE_CODE (t) == IDENTIFIER_NODE)
7157 type = IDENTIFIER_TYPE_VALUE (t);
7158 else
7159 type = TREE_TYPE (t);
399dedb9 7160
50bc768d 7161 gcc_assert (type != unknown_type_node);
ae58fa02 7162
fc6a28d7 7163 if (type
ae58fa02 7164 && TREE_CODE (t) != TYPENAME_TYPE
4393e105
MM
7165 && TREE_CODE (t) != IDENTIFIER_NODE
7166 && TREE_CODE (t) != FUNCTION_TYPE
7167 && TREE_CODE (t) != METHOD_TYPE)
7168 type = tsubst (type, args, complain, in_decl);
7169 if (type == error_mark_node)
7170 return error_mark_node;
ae58fa02 7171
ae58fa02
MM
7172 switch (TREE_CODE (t))
7173 {
7174 case RECORD_TYPE:
7175 case UNION_TYPE:
7176 case ENUMERAL_TYPE:
4393e105
MM
7177 return tsubst_aggr_type (t, args, complain, in_decl,
7178 /*entering_scope=*/0);
ae58fa02
MM
7179
7180 case ERROR_MARK:
7181 case IDENTIFIER_NODE:
ae58fa02
MM
7182 case VOID_TYPE:
7183 case REAL_TYPE:
7184 case COMPLEX_TYPE:
c00996a3 7185 case VECTOR_TYPE:
ae58fa02
MM
7186 case BOOLEAN_TYPE:
7187 case INTEGER_CST:
7188 case REAL_CST:
7189 case STRING_CST:
7190 return t;
7191
7192 case INTEGER_TYPE:
7193 if (t == integer_type_node)
7194 return t;
7195
7196 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
7197 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
7198 return t;
d2e5ee5c 7199
5566b478 7200 {
ddce3528 7201 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 7202
a91db711
NS
7203 /* The array dimension behaves like a non-type template arg,
7204 in that we want to fold it as much as possible. */
7205 max = tsubst_template_arg (omax, args, complain, in_decl);
4ef69b83 7206 max = fold_decl_constant_value (max);
8dd3f57a 7207
eff3a276
MM
7208 if (TREE_CODE (max) != INTEGER_CST
7209 && TREE_CODE (max) != TEMPLATE_PARM_INDEX
7210 && !at_function_scope_p ())
7211 {
7212 if (complain & tf_error)
7213 error ("array bound is not an integer constant");
7214 return error_mark_node;
7215 }
7216
95cd6f6f 7217 /* [temp.deduct]
4393e105 7218
95cd6f6f
JC
7219 Type deduction may fail for any of the following
7220 reasons:
4393e105 7221
95cd6f6f
JC
7222 Attempting to create an array with a size that is
7223 zero or negative. */
7224 if (integer_zerop (max) && !(complain & tf_error))
7225 /* We must fail if performing argument deduction (as
7226 indicated by the state of complain), so that
7227 another substitution can be found. */
7228 return error_mark_node;
95cd6f6f
JC
7229 else if (TREE_CODE (max) == INTEGER_CST
7230 && INT_CST_LT (max, integer_zero_node))
7231 {
c2ea3a40 7232 if (complain & tf_error)
95cd6f6f 7233 error ("creating array with negative size (%qE)", max);
4393e105
MM
7234
7235 return error_mark_node;
0ecfe0b4
JM
7236 }
7237
c95cd22e 7238 return compute_array_index_type (NULL_TREE, max);
ae58fa02
MM
7239 }
7240
7241 case TEMPLATE_TYPE_PARM:
7242 case TEMPLATE_TEMPLATE_PARM:
a1281f45 7243 case BOUND_TEMPLATE_TEMPLATE_PARM:
ae58fa02
MM
7244 case TEMPLATE_PARM_INDEX:
7245 {
7246 int idx;
7247 int level;
7248 int levels;
315fb5db 7249 tree arg = NULL_TREE;
0ecfe0b4
JM
7250
7251 r = NULL_TREE;
ae58fa02 7252
315fb5db 7253 gcc_assert (TREE_VEC_LENGTH (args) > 0);
ae58fa02 7254 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
a1281f45
KL
7255 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7256 || TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
ae58fa02
MM
7257 {
7258 idx = TEMPLATE_TYPE_IDX (t);
7259 level = TEMPLATE_TYPE_LEVEL (t);
7260 }
7261 else
7262 {
7263 idx = TEMPLATE_PARM_IDX (t);
7264 level = TEMPLATE_PARM_LEVEL (t);
7265 }
7266
315fb5db
NS
7267 levels = TMPL_ARGS_DEPTH (args);
7268 if (level <= levels)
7269 arg = TMPL_ARG (args, level, idx);
ae58fa02 7270
315fb5db
NS
7271 if (arg == error_mark_node)
7272 return error_mark_node;
7273 else if (arg != NULL_TREE)
7274 {
7275 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
ae58fa02 7276 {
9180c238 7277 int quals;
315fb5db 7278 gcc_assert (TYPE_P (arg));
9180c238
JM
7279
7280 /* cv-quals from the template are discarded when
7281 substituting in a function or reference type. */
7282 if (TREE_CODE (arg) == FUNCTION_TYPE
7283 || TREE_CODE (arg) == METHOD_TYPE
7284 || TREE_CODE (arg) == REFERENCE_TYPE)
7285 quals = cp_type_quals (arg);
7286 else
7287 quals = cp_type_quals (arg) | cp_type_quals (t);
7288
315fb5db 7289 return cp_build_qualified_type_real
9180c238 7290 (arg, quals, complain | tf_ignore_bad_quals);
315fb5db
NS
7291 }
7292 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
7293 {
7294 /* We are processing a type constructed from a
7295 template template parameter. */
7296 tree argvec = tsubst (TYPE_TI_ARGS (t),
7297 args, complain, in_decl);
7298 if (argvec == error_mark_node)
7299 return error_mark_node;
c8094d83 7300
315fb5db
NS
7301 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
7302 are resolving nested-types in the signature of a
7303 member function templates. Otherwise ARG is a
7304 TEMPLATE_DECL and is the real template to be
7305 instantiated. */
7306 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
7307 arg = TYPE_NAME (arg);
c8094d83
MS
7308
7309 r = lookup_template_class (arg,
7310 argvec, in_decl,
315fb5db
NS
7311 DECL_CONTEXT (arg),
7312 /*entering_scope=*/0,
7313 complain);
7314 return cp_build_qualified_type_real
7315 (r, TYPE_QUALS (t), complain);
ae58fa02 7316 }
315fb5db
NS
7317 else
7318 /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX. */
7319 return arg;
ae58fa02
MM
7320 }
7321
7322 if (level == 1)
7323 /* This can happen during the attempted tsubst'ing in
7324 unify. This means that we don't yet have any information
7325 about the template parameter in question. */
7326 return t;
7327
7328 /* If we get here, we must have been looking at a parm for a
7329 more deeply nested template. Make a new version of this
7330 template parameter, but with a lower level. */
7331 switch (TREE_CODE (t))
7332 {
7333 case TEMPLATE_TYPE_PARM:
7334 case TEMPLATE_TEMPLATE_PARM:
a1281f45 7335 case BOUND_TEMPLATE_TEMPLATE_PARM:
89d684bb 7336 if (cp_type_quals (t))
ae58fa02 7337 {
9ccf6541 7338 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
0cbd7506
MS
7339 r = cp_build_qualified_type_real
7340 (r, cp_type_quals (t),
4f2b0fb2
NS
7341 complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
7342 ? tf_ignore_bad_quals : 0));
9ccf6541
MM
7343 }
7344 else
7345 {
11e74ea6 7346 r = copy_type (t);
9ccf6541
MM
7347 TEMPLATE_TYPE_PARM_INDEX (r)
7348 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
7349 r, levels);
7350 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
7351 TYPE_MAIN_VARIANT (r) = r;
7352 TYPE_POINTER_TO (r) = NULL_TREE;
7353 TYPE_REFERENCE_TO (r) = NULL_TREE;
7354
a1281f45 7355 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
9ccf6541
MM
7356 {
7357 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
c8094d83 7358 complain, in_decl);
9ccf6541
MM
7359 if (argvec == error_mark_node)
7360 return error_mark_node;
4393e105 7361
9ccf6541
MM
7362 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
7363 = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
7364 }
ae58fa02
MM
7365 }
7366 break;
7367
7368 case TEMPLATE_PARM_INDEX:
7369 r = reduce_template_parm_level (t, type, levels);
7370 break;
c8094d83 7371
ae58fa02 7372 default:
315fb5db 7373 gcc_unreachable ();
ae58fa02
MM
7374 }
7375
5566b478 7376 return r;
ae58fa02 7377 }
5566b478 7378
8d08fdba
MS
7379 case TREE_LIST:
7380 {
058b15c1 7381 tree purpose, value, chain;
8d08fdba
MS
7382
7383 if (t == void_list_node)
7384 return t;
7385
8d08fdba
MS
7386 purpose = TREE_PURPOSE (t);
7387 if (purpose)
4393e105
MM
7388 {
7389 purpose = tsubst (purpose, args, complain, in_decl);
7390 if (purpose == error_mark_node)
7391 return error_mark_node;
7392 }
8d08fdba
MS
7393 value = TREE_VALUE (t);
7394 if (value)
4393e105
MM
7395 {
7396 value = tsubst (value, args, complain, in_decl);
7397 if (value == error_mark_node)
7398 return error_mark_node;
7399 }
8d08fdba
MS
7400 chain = TREE_CHAIN (t);
7401 if (chain && chain != void_type_node)
4393e105
MM
7402 {
7403 chain = tsubst (chain, args, complain, in_decl);
7404 if (chain == error_mark_node)
7405 return error_mark_node;
7406 }
8d08fdba
MS
7407 if (purpose == TREE_PURPOSE (t)
7408 && value == TREE_VALUE (t)
7409 && chain == TREE_CHAIN (t))
7410 return t;
058b15c1 7411 return hash_tree_cons (purpose, value, chain);
8d08fdba 7412 }
c8094d83 7413
95b4aca6 7414 case TREE_BINFO:
bd7eccc4 7415 /* We should never be tsubsting a binfo. */
315fb5db 7416 gcc_unreachable ();
85b71cf2 7417
95b4aca6
NS
7418 case TREE_VEC:
7419 /* A vector of template arguments. */
50bc768d 7420 gcc_assert (!type);
a91db711 7421 return tsubst_template_args (t, args, complain, in_decl);
8d08fdba 7422
8d08fdba
MS
7423 case POINTER_TYPE:
7424 case REFERENCE_TYPE:
7425 {
8d08fdba 7426 enum tree_code code;
79a7c7fa 7427
46c895ac 7428 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
8d08fdba
MS
7429 return t;
7430
7431 code = TREE_CODE (t);
4393e105
MM
7432
7433
7434 /* [temp.deduct]
c8094d83 7435
4393e105 7436 Type deduction may fail for any of the following
c8094d83 7437 reasons:
4393e105
MM
7438
7439 -- Attempting to create a pointer to reference type.
7440 -- Attempting to create a reference to a reference type or
7441 a reference to void. */
0ecfe0b4
JM
7442 if (TREE_CODE (type) == REFERENCE_TYPE
7443 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa 7444 {
82a98427 7445 static location_t last_loc;
79a7c7fa
JM
7446
7447 /* We keep track of the last time we issued this error
7448 message to avoid spewing a ton of messages during a
7449 single bad template instantiation. */
c2ea3a40 7450 if (complain & tf_error
93409b8c 7451#ifdef USE_MAPPED_LOCATION
9b60dfd7 7452 && last_loc != input_location
93409b8c 7453#else
82a98427 7454 && (last_loc.line != input_line
9b60dfd7 7455 || last_loc.file != input_filename)
93409b8c 7456#endif
9b60dfd7 7457 )
79a7c7fa 7458 {
0ecfe0b4 7459 if (TREE_CODE (type) == VOID_TYPE)
33bd39a2 7460 error ("forming reference to void");
0ecfe0b4 7461 else
0f51ccfc 7462 error ("forming %s to reference type %qT",
0cbd7506
MS
7463 (code == POINTER_TYPE) ? "pointer" : "reference",
7464 type);
82a98427 7465 last_loc = input_location;
79a7c7fa
JM
7466 }
7467
4393e105 7468 return error_mark_node;
79a7c7fa
JM
7469 }
7470 else if (code == POINTER_TYPE)
46c895ac
NS
7471 {
7472 r = build_pointer_type (type);
7473 if (TREE_CODE (type) == METHOD_TYPE)
7474 r = build_ptrmemfunc_type (r);
7475 }
8d08fdba
MS
7476 else
7477 r = build_reference_type (type);
adecb3f4 7478 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
79a7c7fa 7479
a5f1c5f8
NS
7480 if (r != error_mark_node)
7481 /* Will this ever be needed for TYPE_..._TO values? */
7482 layout_type (r);
c8094d83 7483
8d08fdba
MS
7484 return r;
7485 }
a4443a08 7486 case OFFSET_TYPE:
0ecfe0b4 7487 {
4393e105
MM
7488 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
7489 if (r == error_mark_node || !IS_AGGR_TYPE (r))
7490 {
7491 /* [temp.deduct]
7492
7493 Type deduction may fail for any of the following
7494 reasons:
c8094d83 7495
4393e105 7496 -- Attempting to create "pointer to member of T" when T
0cbd7506 7497 is not a class type. */
c2ea3a40 7498 if (complain & tf_error)
0f51ccfc 7499 error ("creating pointer to member of non-class type %qT", r);
4393e105
MM
7500 return error_mark_node;
7501 }
46c895ac
NS
7502 if (TREE_CODE (type) == REFERENCE_TYPE)
7503 {
4f09be91 7504 if (complain & tf_error)
0f51ccfc 7505 error ("creating pointer to member reference type %qT", type);
cb6d4a9f
VR
7506 return error_mark_node;
7507 }
7508 if (TREE_CODE (type) == VOID_TYPE)
7509 {
7510 if (complain & tf_error)
7511 error ("creating pointer to member of type void");
46c895ac
NS
7512 return error_mark_node;
7513 }
50bc768d 7514 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
46c895ac 7515 if (TREE_CODE (type) == FUNCTION_TYPE)
a5ac359a 7516 {
0cbd7506
MS
7517 /* The type of the implicit object parameter gets its
7518 cv-qualifiers from the FUNCTION_TYPE. */
a5ac359a 7519 tree method_type;
0cbd7506
MS
7520 tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
7521 cp_type_quals (type));
7522 tree memptr;
7523 method_type = build_method_type_directly (this_type,
43dc123f
MM
7524 TREE_TYPE (type),
7525 TYPE_ARG_TYPES (type));
0cbd7506
MS
7526 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
7527 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
7528 complain);
a5ac359a 7529 }
46c895ac 7530 else
b7a78333
MM
7531 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
7532 TYPE_QUALS (t),
7533 complain);
0ecfe0b4 7534 }
8d08fdba
MS
7535 case FUNCTION_TYPE:
7536 case METHOD_TYPE:
7537 {
c11b6f21 7538 tree fntype;
c7222c02 7539 tree specs;
4393e105
MM
7540 fntype = tsubst_function_type (t, args, complain, in_decl);
7541 if (fntype == error_mark_node)
7542 return error_mark_node;
cabc336a 7543
34cd5ae7 7544 /* Substitute the exception specification. */
c8094d83 7545 specs = tsubst_exception_specification (t, args, complain,
c7222c02 7546 in_decl);
9f6206d9
VR
7547 if (specs == error_mark_node)
7548 return error_mark_node;
c7222c02
MM
7549 if (specs)
7550 fntype = build_exception_variant (fntype, specs);
c11b6f21 7551 return fntype;
8d08fdba
MS
7552 }
7553 case ARRAY_TYPE:
7554 {
4393e105
MM
7555 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
7556 if (domain == error_mark_node)
7557 return error_mark_node;
7558
7559 /* As an optimization, we avoid regenerating the array type if
7560 it will obviously be the same as T. */
8d08fdba
MS
7561 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
7562 return t;
0ecfe0b4 7563
c8094d83 7564 /* These checks should match the ones in grokdeclarator.
4393e105 7565
c8094d83
MS
7566 [temp.deduct]
7567
7568 The deduction may fail for any of the following reasons:
4393e105
MM
7569
7570 -- Attempting to create an array with an element type that
c8094d83 7571 is void, a function type, or a reference type, or [DR337]
cfb91b67 7572 an abstract class type. */
c8094d83 7573 if (TREE_CODE (type) == VOID_TYPE
4393e105
MM
7574 || TREE_CODE (type) == FUNCTION_TYPE
7575 || TREE_CODE (type) == REFERENCE_TYPE)
0ecfe0b4 7576 {
c2ea3a40 7577 if (complain & tf_error)
0f51ccfc 7578 error ("creating array of %qT", type);
4393e105 7579 return error_mark_node;
0ecfe0b4 7580 }
cfb91b67
GB
7581 if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
7582 {
7583 if (complain & tf_error)
c8094d83 7584 error ("creating array of %qT, which is an abstract class type",
cfb91b67 7585 type);
c8094d83 7586 return error_mark_node;
cfb91b67 7587 }
0ecfe0b4 7588
8d08fdba
MS
7589 r = build_cplus_array_type (type, domain);
7590 return r;
7591 }
7592
8d08fdba 7593 case PLUS_EXPR:
5566b478 7594 case MINUS_EXPR:
4393e105 7595 {
c2ea3a40
NS
7596 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
7597 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
4393e105
MM
7598
7599 if (e1 == error_mark_node || e2 == error_mark_node)
7600 return error_mark_node;
7601
7866705a 7602 return fold_build2 (TREE_CODE (t), TREE_TYPE (t), e1, e2);
4393e105 7603 }
8d08fdba
MS
7604
7605 case NEGATE_EXPR:
7606 case NOP_EXPR:
4393e105 7607 {
c2ea3a40 7608 tree e = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
7609 if (e == error_mark_node)
7610 return error_mark_node;
7611
7866705a 7612 return fold_build1 (TREE_CODE (t), TREE_TYPE (t), e);
4393e105 7613 }
8d08fdba 7614
5566b478
MS
7615 case TYPENAME_TYPE:
7616 {
4393e105
MM
7617 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7618 in_decl, /*entering_scope=*/1);
7619 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
c8094d83 7620 complain, in_decl);
4393e105
MM
7621
7622 if (ctx == error_mark_node || f == error_mark_node)
7623 return error_mark_node;
ae58fa02 7624
bf8f3f93
MM
7625 if (!IS_AGGR_TYPE (ctx))
7626 {
c2ea3a40 7627 if (complain & tf_error)
0f51ccfc 7628 error ("%qT is not a class, struct, or union type", ctx);
bf8f3f93
MM
7629 return error_mark_node;
7630 }
7631 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
7632 {
7633 /* Normally, make_typename_type does not require that the CTX
7634 have complete type in order to allow things like:
c8094d83 7635
0cbd7506 7636 template <class T> struct S { typename S<T>::X Y; };
ae58fa02 7637
bf8f3f93
MM
7638 But, such constructs have already been resolved by this
7639 point, so here CTX really should have complete type, unless
7640 it's a partial instantiation. */
4393e105 7641 ctx = complete_type (ctx);
d0f062fb 7642 if (!COMPLETE_TYPE_P (ctx))
4393e105 7643 {
c2ea3a40 7644 if (complain & tf_error)
7a228918 7645 cxx_incomplete_type_error (NULL_TREE, ctx);
4393e105
MM
7646 return error_mark_node;
7647 }
7648 }
ae58fa02 7649
fc6a28d7 7650 f = make_typename_type (ctx, f, typename_type,
4f2b0fb2 7651 (complain & tf_error) | tf_keep_type_decl);
f0bcd168
MM
7652 if (f == error_mark_node)
7653 return f;
0cbd7506
MS
7654 if (TREE_CODE (f) == TYPE_DECL)
7655 {
4f2b0fb2 7656 complain |= tf_ignore_bad_quals;
0cbd7506
MS
7657 f = TREE_TYPE (f);
7658 }
c8094d83 7659
fc6a28d7
MM
7660 if (TREE_CODE (f) != TYPENAME_TYPE)
7661 {
7662 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
c8094d83 7663 error ("%qT resolves to %qT, which is not an enumeration type",
fc6a28d7
MM
7664 t, f);
7665 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
c8094d83 7666 error ("%qT resolves to %qT, which is is not a class type",
fc6a28d7
MM
7667 t, f);
7668 }
7669
0cbd7506
MS
7670 return cp_build_qualified_type_real
7671 (f, cp_type_quals (f) | cp_type_quals (t), complain);
5566b478 7672 }
c8094d83 7673
b8c6534b
KL
7674 case UNBOUND_CLASS_TEMPLATE:
7675 {
7676 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
7677 in_decl, /*entering_scope=*/1);
7678 tree name = TYPE_IDENTIFIER (t);
b939a023 7679 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
b8c6534b
KL
7680
7681 if (ctx == error_mark_node || name == error_mark_node)
7682 return error_mark_node;
7683
b939a023
KL
7684 if (parm_list)
7685 parm_list = tsubst_template_parms (parm_list, args, complain);
7686 return make_unbound_class_template (ctx, name, parm_list, complain);
b8c6534b
KL
7687 }
7688
5566b478 7689 case INDIRECT_REF:
5566b478 7690 case ADDR_EXPR:
058b15c1 7691 case CALL_EXPR:
315fb5db 7692 gcc_unreachable ();
5566b478
MS
7693
7694 case ARRAY_REF:
4393e105 7695 {
c2ea3a40 7696 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
015c2c66
MM
7697 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain, in_decl,
7698 /*integral_constant_expression_p=*/false);
4393e105
MM
7699 if (e1 == error_mark_node || e2 == error_mark_node)
7700 return error_mark_node;
7701
44de5aeb 7702 return build_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
4393e105 7703 }
5566b478 7704
fc378698 7705 case SCOPE_REF:
4393e105 7706 {
c2ea3a40 7707 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
4393e105
MM
7708 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
7709 if (e1 == error_mark_node || e2 == error_mark_node)
7710 return error_mark_node;
7711
02ed62dd
MM
7712 return build_qualified_name (/*type=*/NULL_TREE,
7713 e1, e2, QUALIFIED_NAME_IS_TEMPLATE (t));
4393e105 7714 }
fc378698 7715
b894fc05 7716 case TYPEOF_TYPE:
4393e105 7717 {
b830b74c 7718 tree type;
4393e105 7719
015c2c66
MM
7720 type = finish_typeof (tsubst_expr
7721 (TYPEOF_TYPE_EXPR (t), args,
7722 complain, in_decl,
7723 /*integral_constant_expression_p=*/false));
b830b74c 7724 return cp_build_qualified_type_real (type,
dce50630 7725 cp_type_quals (t)
b830b74c 7726 | cp_type_quals (type),
dce50630 7727 complain);
4393e105 7728 }
b894fc05 7729
8d08fdba 7730 default:
9e637a26 7731 sorry ("use of %qs in template",
8d08fdba
MS
7732 tree_code_name [(int) TREE_CODE (t)]);
7733 return error_mark_node;
7734 }
7735}
7736
ee76b931
MM
7737/* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
7738 type of the expression on the left-hand side of the "." or "->"
7739 operator. */
7740
7741static tree
7742tsubst_baselink (tree baselink, tree object_type,
7743 tree args, tsubst_flags_t complain, tree in_decl)
7744{
7745 tree name;
7746 tree qualifying_scope;
7747 tree fns;
6f67f000 7748 tree optype;
ee76b931
MM
7749 tree template_args = 0;
7750 bool template_id_p = false;
7751
7752 /* A baselink indicates a function from a base class. The
7753 BASELINK_ACCESS_BINFO and BASELINK_BINFO are going to have
7754 non-dependent types; otherwise, the lookup could not have
7755 succeeded. However, they may indicate bases of the template
c8094d83 7756 class, rather than the instantiated class.
ee76b931
MM
7757
7758 In addition, lookups that were not ambiguous before may be
04c06002 7759 ambiguous now. Therefore, we perform the lookup again. */
ee76b931
MM
7760 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
7761 fns = BASELINK_FUNCTIONS (baselink);
6f67f000 7762 optype = BASELINK_OPTYPE (baselink);
ee76b931
MM
7763 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
7764 {
7765 template_id_p = true;
7766 template_args = TREE_OPERAND (fns, 1);
7767 fns = TREE_OPERAND (fns, 0);
bf12d54d
NS
7768 if (template_args)
7769 template_args = tsubst_template_args (template_args, args,
7770 complain, in_decl);
ee76b931
MM
7771 }
7772 name = DECL_NAME (get_first_fn (fns));
7773 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
c8094d83 7774
9b60dfd7
MM
7775 /* If lookup found a single function, mark it as used at this
7776 point. (If it lookup found multiple functions the one selected
7777 later by overload resolution will be marked as used at that
7778 point.) */
7779 if (BASELINK_P (baselink))
7780 fns = BASELINK_FUNCTIONS (baselink);
7781 if (!template_id_p && !really_overloaded_fn (fns))
7782 mark_used (OVL_CURRENT (fns));
7783
7784 /* Add back the template arguments, if present. */
ee76b931 7785 if (BASELINK_P (baselink) && template_id_p)
c8094d83 7786 BASELINK_FUNCTIONS (baselink)
ee76b931
MM
7787 = build_nt (TEMPLATE_ID_EXPR,
7788 BASELINK_FUNCTIONS (baselink),
7789 template_args);
6f67f000
MM
7790 /* Update the conversion operator type. */
7791 BASELINK_OPTYPE (baselink)
7792 = tsubst (optype, args, complain, in_decl);
9b60dfd7 7793
ee76b931
MM
7794 if (!object_type)
7795 object_type = current_class_type;
c8094d83 7796 return adjust_result_of_qualified_name_lookup (baselink,
ee76b931
MM
7797 qualifying_scope,
7798 object_type);
7799}
7800
7801/* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
7802 true if the qualified-id will be a postfix-expression in-and-of
7803 itself; false if more of the postfix-expression follows the
7804 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
7805 of "&". */
7806
7807static tree
c8094d83 7808tsubst_qualified_id (tree qualified_id, tree args,
ee76b931
MM
7809 tsubst_flags_t complain, tree in_decl,
7810 bool done, bool address_p)
7811{
7812 tree expr;
7813 tree scope;
7814 tree name;
7815 bool is_template;
7816 tree template_args;
7817
50bc768d 7818 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
ee76b931 7819
ee76b931
MM
7820 /* Figure out what name to look up. */
7821 name = TREE_OPERAND (qualified_id, 1);
7822 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
7823 {
7824 is_template = true;
bf12d54d
NS
7825 template_args = TREE_OPERAND (name, 1);
7826 if (template_args)
7827 template_args = tsubst_template_args (template_args, args,
7828 complain, in_decl);
ee76b931
MM
7829 name = TREE_OPERAND (name, 0);
7830 }
7831 else
7832 {
7833 is_template = false;
7834 template_args = NULL_TREE;
7835 }
7836
6eeba0cc
MM
7837 /* Substitute into the qualifying scope. When there are no ARGS, we
7838 are just trying to simplify a non-dependent expression. In that
7839 case the qualifying scope may be dependent, and, in any case,
7840 substituting will not help. */
7841 scope = TREE_OPERAND (qualified_id, 0);
7842 if (args)
7843 {
7844 scope = tsubst (scope, args, complain, in_decl);
7845 expr = tsubst_copy (name, args, complain, in_decl);
7846 }
7847 else
7848 expr = name;
10b1d5e7 7849
ab73670a 7850 if (dependent_type_p (scope))
3db45ab5
MS
7851 return build_qualified_name (/*type=*/NULL_TREE,
7852 scope, expr,
02ed62dd 7853 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
c8094d83 7854
5e08432e 7855 if (!BASELINK_P (name) && !DECL_P (expr))
12483c9f 7856 {
8ca4bf25
MM
7857 if (TREE_CODE (expr) == BIT_NOT_EXPR)
7858 /* If this were actually a destructor call, it would have been
7859 parsed as such by the parser. */
7860 expr = error_mark_node;
7861 else
7862 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
12483c9f
NS
7863 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
7864 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
7865 {
7866 if (complain & tf_error)
b2693faf 7867 {
0f51ccfc 7868 error ("dependent-name %qE is parsed as a non-type, but "
b2693faf 7869 "instantiation yields a type", qualified_id);
0f51ccfc 7870 inform ("say %<typename %E%> if a type is meant", qualified_id);
b2693faf 7871 }
12483c9f
NS
7872 return error_mark_node;
7873 }
7874 }
c8094d83 7875
279b8466 7876 if (DECL_P (expr))
8f78f01f
MM
7877 {
7878 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
7879 scope);
7880 /* Remember that there was a reference to this entity. */
7881 mark_used (expr);
7882 }
7883
7884 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
7885 {
7886 if (complain & tf_error)
c8094d83 7887 qualified_name_lookup_error (scope,
8f78f01f
MM
7888 TREE_OPERAND (qualified_id, 1),
7889 expr);
7890 return error_mark_node;
7891 }
ee76b931
MM
7892
7893 if (is_template)
10b1d5e7 7894 expr = lookup_template_function (expr, template_args);
ee76b931 7895
22038b2c 7896 if (expr == error_mark_node && complain & tf_error)
8f78f01f
MM
7897 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
7898 expr);
22038b2c 7899 else if (TYPE_P (scope))
ee76b931 7900 {
c8094d83 7901 expr = (adjust_result_of_qualified_name_lookup
ee76b931 7902 (expr, scope, current_class_type));
3db45ab5 7903 expr = (finish_qualified_id_expr
02ed62dd
MM
7904 (scope, expr, done, address_p,
7905 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
7906 /*template_arg_p=*/false));
ee76b931 7907 }
c8094d83 7908
015c2c66
MM
7909 /* Expressions do not generally have reference type. */
7910 if (TREE_CODE (expr) != SCOPE_REF
7911 /* However, if we're about to form a pointer-to-member, we just
7912 want the referenced member referenced. */
7913 && TREE_CODE (expr) != OFFSET_REF)
aec5ba60 7914 expr = convert_from_reference (expr);
ee76b931
MM
7915
7916 return expr;
7917}
7918
00d3396f
JM
7919/* Like tsubst, but deals with expressions. This function just replaces
7920 template parms; to finish processing the resultant expression, use
7921 tsubst_expr. */
7922
14d22dd6 7923static tree
3a978d72 7924tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
5566b478
MS
7925{
7926 enum tree_code code;
8452b1d3 7927 tree r;
8d08fdba 7928
5566b478
MS
7929 if (t == NULL_TREE || t == error_mark_node)
7930 return t;
7931
7932 code = TREE_CODE (t);
b7484fbe 7933
5566b478
MS
7934 switch (code)
7935 {
7936 case PARM_DECL:
a723baf1 7937 r = retrieve_local_specialization (t);
50bc768d 7938 gcc_assert (r != NULL);
c0694c4b 7939 mark_used (r);
a723baf1 7940 return r;
5566b478
MS
7941
7942 case CONST_DECL:
ed44da02
MM
7943 {
7944 tree enum_type;
7945 tree v;
7946
a723baf1
MM
7947 if (DECL_TEMPLATE_PARM_P (t))
7948 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
7b6d72fc
MM
7949 /* There is no need to substitute into namespace-scope
7950 enumerators. */
7951 if (DECL_NAMESPACE_SCOPE_P (t))
ed44da02 7952 return t;
d5a10cf0
MM
7953 /* If ARGS is NULL, then T is known to be non-dependent. */
7954 if (args == NULL_TREE)
8a784e4a 7955 return integral_constant_value (t);
ed44da02
MM
7956
7957 /* Unfortunately, we cannot just call lookup_name here.
9188c363 7958 Consider:
c8094d83 7959
9188c363
MM
7960 template <int I> int f() {
7961 enum E { a = I };
7962 struct S { void g() { E e = a; } };
7963 };
c8094d83 7964
9188c363
MM
7965 When we instantiate f<7>::S::g(), say, lookup_name is not
7966 clever enough to find f<7>::a. */
c8094d83
MS
7967 enum_type
7968 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
ed44da02
MM
7969 /*entering_scope=*/0);
7970
c8094d83
MS
7971 for (v = TYPE_VALUES (enum_type);
7972 v != NULL_TREE;
ed44da02
MM
7973 v = TREE_CHAIN (v))
7974 if (TREE_PURPOSE (v) == DECL_NAME (t))
7975 return TREE_VALUE (v);
7976
7977 /* We didn't find the name. That should never happen; if
7978 name-lookup found it during preliminary parsing, we
7979 should find it again here during instantiation. */
315fb5db 7980 gcc_unreachable ();
ed44da02 7981 }
db04386f 7982 return t;
ed44da02 7983
5566b478
MS
7984 case FIELD_DECL:
7985 if (DECL_CONTEXT (t))
7986 {
0978790f 7987 tree ctx;
0978790f 7988
4393e105 7989 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
36a117a5 7990 /*entering_scope=*/1);
0978790f 7991 if (ctx != DECL_CONTEXT (t))
bad1f462
KL
7992 {
7993 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
7994 if (!r)
7995 {
7996 if (complain & tf_error)
7997 error ("using invalid field %qD", t);
7998 return error_mark_node;
7999 }
8000 return r;
8001 }
5566b478 8002 }
c8094d83 8003
5566b478
MS
8004 return t;
8005
8006 case VAR_DECL:
8007 case FUNCTION_DECL:
a723baf1
MM
8008 if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
8009 || local_variable_p (t))
4393e105 8010 t = tsubst (t, args, complain, in_decl);
5566b478
MS
8011 mark_used (t);
8012 return t;
8013
a723baf1 8014 case BASELINK:
ee76b931 8015 return tsubst_baselink (t, current_class_type, args, complain, in_decl);
a723baf1 8016
98c1c668 8017 case TEMPLATE_DECL:
a723baf1 8018 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
c8094d83 8019 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
a723baf1 8020 args, complain, in_decl);
c7222c02 8021 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
4393e105 8022 return tsubst (t, args, complain, in_decl);
fcea7401
KL
8023 else if (DECL_CLASS_SCOPE_P (t)
8024 && uses_template_parms (DECL_CONTEXT (t)))
8025 {
8026 /* Template template argument like the following example need
8027 special treatment:
8028
8029 template <template <class> class TT> struct C {};
8030 template <class T> struct D {
8031 template <class U> struct E {};
0cbd7506 8032 C<E> c; // #1
fcea7401
KL
8033 };
8034 D<int> d; // #2
8035
8036 We are processing the template argument `E' in #1 for
8037 the template instantiation #2. Originally, `E' is a
8038 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
8039 have to substitute this with one having context `D<int>'. */
8040
8041 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
8042 return lookup_field (context, DECL_NAME(t), 0, false);
8043 }
98c1c668 8044 else
fcea7401 8045 /* Ordinary template template argument. */
98c1c668
JM
8046 return t;
8047
5566b478
MS
8048 case CAST_EXPR:
8049 case REINTERPRET_CAST_EXPR:
e92cc029
MS
8050 case CONST_CAST_EXPR:
8051 case STATIC_CAST_EXPR:
8052 case DYNAMIC_CAST_EXPR:
51924768 8053 case NOP_EXPR:
5566b478 8054 return build1
4393e105
MM
8055 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
8056 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478
MS
8057
8058 case INDIRECT_REF:
5566b478
MS
8059 case NEGATE_EXPR:
8060 case TRUTH_NOT_EXPR:
b87692e5 8061 case BIT_NOT_EXPR:
5566b478 8062 case ADDR_EXPR:
392e3d51 8063 case UNARY_PLUS_EXPR: /* Unary + */
5566b478 8064 case SIZEOF_EXPR:
abff8e06 8065 case ALIGNOF_EXPR:
5566b478 8066 case ARROW_EXPR:
fc378698 8067 case THROW_EXPR:
5156628f 8068 case TYPEID_EXPR:
f5733617
SS
8069 case REALPART_EXPR:
8070 case IMAGPART_EXPR:
5566b478 8071 return build1
6a629cac 8072 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
4393e105 8073 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478 8074
a723baf1
MM
8075 case COMPONENT_REF:
8076 {
8077 tree object;
8078 tree name;
8079
8080 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
8081 name = TREE_OPERAND (t, 1);
c8094d83 8082 if (TREE_CODE (name) == BIT_NOT_EXPR)
a723baf1
MM
8083 {
8084 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8085 complain, in_decl);
8086 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
8087 }
8088 else if (TREE_CODE (name) == SCOPE_REF
8089 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
8090 {
8091 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
8092 complain, in_decl);
8093 name = TREE_OPERAND (name, 1);
8094 name = tsubst_copy (TREE_OPERAND (name, 0), args,
8095 complain, in_decl);
8096 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
02ed62dd 8097 name = build_qualified_name (/*type=*/NULL_TREE,
3db45ab5 8098 base, name,
02ed62dd 8099 /*template_p=*/false);
a723baf1 8100 }
ee76b931 8101 else if (TREE_CODE (name) == BASELINK)
c8094d83
MS
8102 name = tsubst_baselink (name,
8103 non_reference (TREE_TYPE (object)),
8104 args, complain,
ee76b931 8105 in_decl);
a723baf1 8106 else
ee76b931 8107 name = tsubst_copy (name, args, complain, in_decl);
44de5aeb 8108 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
a723baf1
MM
8109 }
8110
5566b478
MS
8111 case PLUS_EXPR:
8112 case MINUS_EXPR:
8113 case MULT_EXPR:
8114 case TRUNC_DIV_EXPR:
8115 case CEIL_DIV_EXPR:
8116 case FLOOR_DIV_EXPR:
8117 case ROUND_DIV_EXPR:
8118 case EXACT_DIV_EXPR:
8119 case BIT_AND_EXPR:
5566b478
MS
8120 case BIT_IOR_EXPR:
8121 case BIT_XOR_EXPR:
8122 case TRUNC_MOD_EXPR:
8123 case FLOOR_MOD_EXPR:
8124 case TRUTH_ANDIF_EXPR:
8125 case TRUTH_ORIF_EXPR:
8126 case TRUTH_AND_EXPR:
8127 case TRUTH_OR_EXPR:
8128 case RSHIFT_EXPR:
8129 case LSHIFT_EXPR:
8130 case RROTATE_EXPR:
8131 case LROTATE_EXPR:
8132 case EQ_EXPR:
8133 case NE_EXPR:
8134 case MAX_EXPR:
8135 case MIN_EXPR:
8136 case LE_EXPR:
8137 case GE_EXPR:
8138 case LT_EXPR:
8139 case GT_EXPR:
5566b478 8140 case COMPOUND_EXPR:
5566b478
MS
8141 case DOTSTAR_EXPR:
8142 case MEMBER_REF:
519c9806
MM
8143 case PREDECREMENT_EXPR:
8144 case PREINCREMENT_EXPR:
8145 case POSTDECREMENT_EXPR:
8146 case POSTINCREMENT_EXPR:
5566b478 8147 return build_nt
4393e105
MM
8148 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8149 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478 8150
02ed62dd
MM
8151 case SCOPE_REF:
8152 return build_qualified_name (/*type=*/NULL_TREE,
8153 tsubst_copy (TREE_OPERAND (t, 0),
8154 args, complain, in_decl),
8155 tsubst_copy (TREE_OPERAND (t, 1),
8156 args, complain, in_decl),
8157 QUALIFIED_NAME_IS_TEMPLATE (t));
8158
d8987adb
NS
8159 case ARRAY_REF:
8160 return build_nt
8161 (ARRAY_REF,
8162 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8163 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8164 NULL_TREE, NULL_TREE);
8165
5566b478 8166 case CALL_EXPR:
c8094d83 8167 return build_nt (code,
a723baf1
MM
8168 tsubst_copy (TREE_OPERAND (t, 0), args,
8169 complain, in_decl),
8170 tsubst_copy (TREE_OPERAND (t, 1), args, complain,
8171 in_decl),
8172 NULL_TREE);
5566b478 8173
5566b478
MS
8174 case COND_EXPR:
8175 case MODOP_EXPR:
40242ccf 8176 case PSEUDO_DTOR_EXPR:
67da3287 8177 {
8452b1d3 8178 r = build_nt
4393e105
MM
8179 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8180 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8181 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
e4c2c34b 8182 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
67da3287
MM
8183 return r;
8184 }
5566b478
MS
8185
8186 case NEW_EXPR:
8187 {
8452b1d3 8188 r = build_nt
4393e105
MM
8189 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8190 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
8191 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
5566b478
MS
8192 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
8193 return r;
8194 }
8195
8196 case DELETE_EXPR:
8197 {
8452b1d3 8198 r = build_nt
4393e105
MM
8199 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
8200 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
8201 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
8202 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
8203 return r;
8204 }
8205
386b8a85
JM
8206 case TEMPLATE_ID_EXPR:
8207 {
0cbd7506 8208 /* Substituted template arguments */
a91db711
NS
8209 tree fn = TREE_OPERAND (t, 0);
8210 tree targs = TREE_OPERAND (t, 1);
856216bb 8211
a91db711 8212 fn = tsubst_copy (fn, args, complain, in_decl);
bf12d54d
NS
8213 if (targs)
8214 targs = tsubst_template_args (targs, args, complain, in_decl);
c8094d83 8215
a91db711 8216 return lookup_template_function (fn, targs);
386b8a85
JM
8217 }
8218
5566b478
MS
8219 case TREE_LIST:
8220 {
8221 tree purpose, value, chain;
8222
8223 if (t == void_list_node)
8224 return t;
8225
8226 purpose = TREE_PURPOSE (t);
8227 if (purpose)
4393e105 8228 purpose = tsubst_copy (purpose, args, complain, in_decl);
5566b478
MS
8229 value = TREE_VALUE (t);
8230 if (value)
4393e105 8231 value = tsubst_copy (value, args, complain, in_decl);
5566b478
MS
8232 chain = TREE_CHAIN (t);
8233 if (chain && chain != void_type_node)
4393e105 8234 chain = tsubst_copy (chain, args, complain, in_decl);
5566b478
MS
8235 if (purpose == TREE_PURPOSE (t)
8236 && value == TREE_VALUE (t)
8237 && chain == TREE_CHAIN (t))
8238 return t;
8239 return tree_cons (purpose, value, chain);
8240 }
8241
8242 case RECORD_TYPE:
8243 case UNION_TYPE:
8244 case ENUMERAL_TYPE:
8245 case INTEGER_TYPE:
8246 case TEMPLATE_TYPE_PARM:
73b0fce8 8247 case TEMPLATE_TEMPLATE_PARM:
a1281f45 8248 case BOUND_TEMPLATE_TEMPLATE_PARM:
f84b4be9 8249 case TEMPLATE_PARM_INDEX:
5566b478
MS
8250 case POINTER_TYPE:
8251 case REFERENCE_TYPE:
8252 case OFFSET_TYPE:
8253 case FUNCTION_TYPE:
8254 case METHOD_TYPE:
8255 case ARRAY_TYPE:
8256 case TYPENAME_TYPE:
b8c6534b 8257 case UNBOUND_CLASS_TEMPLATE:
2a2b1d56 8258 case TYPEOF_TYPE:
f84b4be9 8259 case TYPE_DECL:
4393e105 8260 return tsubst (t, args, complain, in_decl);
5566b478 8261
e92cc029 8262 case IDENTIFIER_NODE:
421844e7 8263 if (IDENTIFIER_TYPENAME_P (t))
1f6e1acc
AS
8264 {
8265 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
1f84ec23 8266 return mangle_conv_op_name_for_type (new_type);
1f6e1acc 8267 }
e92cc029
MS
8268 else
8269 return t;
8270
5156628f 8271 case CONSTRUCTOR:
4038c495
GB
8272 /* This is handled by tsubst_copy_and_build. */
8273 gcc_unreachable ();
5156628f 8274
371534a9 8275 case VA_ARG_EXPR:
ea333e1c 8276 return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
c2ea3a40
NS
8277 in_decl),
8278 tsubst (TREE_TYPE (t), args, complain, in_decl));
f9817201 8279
543a0daa
RH
8280 case CLEANUP_POINT_EXPR:
8281 /* We shouldn't have built any of these during initial template
8282 generation. Instead, they should be built during instantiation
8283 in response to the saved STMT_IS_FULL_EXPR_P setting. */
315fb5db 8284 gcc_unreachable ();
543a0daa 8285
7eab6e7b
MM
8286 case OFFSET_REF:
8287 mark_used (TREE_OPERAND (t, 1));
8288 return t;
8289
5566b478
MS
8290 default:
8291 return t;
8292 }
8293}
8294
1799e5d5
RH
8295/* Like tsubst_copy, but specifically for OpenMP clauses. */
8296
8297static tree
8298tsubst_omp_clauses (tree clauses, tree args, tsubst_flags_t complain,
8299 tree in_decl)
8300{
8301 tree new_clauses = NULL, nc, oc;
8302
8303 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
8304 {
8305 nc = copy_node (oc);
8306 OMP_CLAUSE_CHAIN (nc) = new_clauses;
8307 new_clauses = nc;
8308
8309 switch (OMP_CLAUSE_CODE (nc))
8310 {
8311 case OMP_CLAUSE_PRIVATE:
8312 case OMP_CLAUSE_SHARED:
8313 case OMP_CLAUSE_FIRSTPRIVATE:
8314 case OMP_CLAUSE_LASTPRIVATE:
8315 case OMP_CLAUSE_REDUCTION:
8316 case OMP_CLAUSE_COPYIN:
8317 case OMP_CLAUSE_COPYPRIVATE:
8318 case OMP_CLAUSE_IF:
8319 case OMP_CLAUSE_NUM_THREADS:
8320 case OMP_CLAUSE_SCHEDULE:
8321 OMP_CLAUSE_OPERAND (nc, 0)
015c2c66
MM
8322 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
8323 in_decl, /*integral_constant_expression_p=*/false);
1799e5d5
RH
8324 break;
8325 case OMP_CLAUSE_NOWAIT:
8326 case OMP_CLAUSE_ORDERED:
8327 case OMP_CLAUSE_DEFAULT:
8328 break;
8329 default:
8330 gcc_unreachable ();
8331 }
8332 }
8333
8334 return finish_omp_clauses (nreverse (new_clauses));
8335}
8336
f74dcfb7
JJ
8337/* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
8338
8339static tree
8340tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
8341 tree in_decl)
8342{
8343#define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
8344
8345 tree purpose, value, chain;
8346
8347 if (t == NULL)
8348 return t;
8349
8350 if (TREE_CODE (t) != TREE_LIST)
8351 return tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
8352 /*function_p=*/false,
8353 /*integral_constant_expression_p=*/false);
f74dcfb7
JJ
8354
8355 if (t == void_list_node)
8356 return t;
8357
8358 purpose = TREE_PURPOSE (t);
8359 if (purpose)
8360 purpose = RECUR (purpose);
8361 value = TREE_VALUE (t);
8362 if (value)
8363 value = RECUR (value);
8364 chain = TREE_CHAIN (t);
8365 if (chain && chain != void_type_node)
8366 chain = RECUR (chain);
8367 return tree_cons (purpose, value, chain);
8368#undef RECUR
8369}
8370
cc23546e
JO
8371/* Like tsubst_copy for expressions, etc. but also does semantic
8372 processing. */
00d3396f 8373
14d22dd6 8374static tree
015c2c66
MM
8375tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
8376 bool integral_constant_expression_p)
5566b478 8377{
015c2c66
MM
8378#define RECUR(NODE) \
8379 tsubst_expr ((NODE), args, complain, in_decl, \
8380 integral_constant_expression_p)
8381
fd10dd09 8382 tree stmt, tmp;
558475f0 8383
5566b478
MS
8384 if (t == NULL_TREE || t == error_mark_node)
8385 return t;
8386
93409b8c
PB
8387 if (EXPR_HAS_LOCATION (t))
8388 input_location = EXPR_LOCATION (t);
7c34ced1
RH
8389 if (STATEMENT_CODE_P (TREE_CODE (t)))
8390 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
8391
5566b478 8392 switch (TREE_CODE (t))
8d08fdba 8393 {
325c3691
RH
8394 case STATEMENT_LIST:
8395 {
8396 tree_stmt_iterator i;
8397 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
015c2c66 8398 RECUR (tsi_stmt (i));
325c3691
RH
8399 break;
8400 }
8401
558475f0 8402 case CTOR_INITIALIZER:
c8094d83 8403 finish_mem_initializers (tsubst_initializer_list
2282d28d
MM
8404 (TREE_OPERAND (t, 0), args));
8405 break;
558475f0 8406
5088b058 8407 case RETURN_EXPR:
015c2c66 8408 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
5566b478
MS
8409 break;
8410
8411 case EXPR_STMT:
015c2c66 8412 tmp = RECUR (EXPR_STMT_EXPR (t));
7c34ced1
RH
8413 if (EXPR_STMT_STMT_EXPR_RESULT (t))
8414 finish_stmt_expr_expr (tmp, cur_stmt_expr);
8415 else
8416 finish_expr_stmt (tmp);
8417 break;
5566b478 8418
9da99f7d 8419 case USING_STMT:
015c2c66 8420 do_using_directive (RECUR (USING_STMT_NAMESPACE (t)));
9da99f7d 8421 break;
c8094d83 8422
350fae66 8423 case DECL_EXPR:
5566b478 8424 {
e0942dcd
MM
8425 tree decl;
8426 tree init;
5566b478 8427
350fae66 8428 decl = DECL_EXPR_DECL (t);
acef433b
MM
8429 if (TREE_CODE (decl) == LABEL_DECL)
8430 finish_label_decl (DECL_NAME (decl));
fbfe8c9e
NS
8431 else if (TREE_CODE (decl) == USING_DECL)
8432 {
98ed9dae 8433 tree scope = USING_DECL_SCOPE (decl);
fbfe8c9e 8434 tree name = DECL_NAME (decl);
22038b2c 8435 tree decl;
c8094d83 8436
015c2c66 8437 scope = RECUR (scope);
22038b2c 8438 decl = lookup_qualified_name (scope, name,
12483c9f
NS
8439 /*is_type_p=*/false,
8440 /*complain=*/false);
8f78f01f
MM
8441 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
8442 qualified_name_lookup_error (scope, name, decl);
22038b2c 8443 else
ed5f054f 8444 do_local_using_decl (decl, scope, name);
fbfe8c9e 8445 }
acef433b
MM
8446 else
8447 {
8448 init = DECL_INITIAL (decl);
8449 decl = tsubst (decl, args, complain, in_decl);
ce2e5191
NS
8450 if (decl != error_mark_node)
8451 {
0cbd7506
MS
8452 /* By marking the declaration as instantiated, we avoid
8453 trying to instantiate it. Since instantiate_decl can't
8454 handle local variables, and since we've already done
8455 all that needs to be done, that's the right thing to
8456 do. */
8457 if (TREE_CODE (decl) == VAR_DECL)
8458 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
edebf865
MM
8459 if (TREE_CODE (decl) == VAR_DECL
8460 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
8461 /* Anonymous aggregates are a special case. */
8462 finish_anon_union (decl);
c8094d83 8463 else
ed2fa432 8464 {
edebf865 8465 maybe_push_decl (decl);
39703eb9
MM
8466 if (TREE_CODE (decl) == VAR_DECL
8467 && DECL_PRETTY_FUNCTION_P (decl))
edebf865
MM
8468 {
8469 /* For __PRETTY_FUNCTION__ we have to adjust the
8470 initializer. */
8471 const char *const name
8472 = cxx_printable_name (current_function_decl, 2);
bb885938 8473 init = cp_fname_init (name, &TREE_TYPE (decl));
edebf865
MM
8474 }
8475 else
015c2c66 8476 init = RECUR (init);
d174af6c 8477 finish_decl (decl, init, NULL_TREE);
ed2fa432 8478 }
ce2e5191 8479 }
acef433b 8480 }
fd10dd09 8481
350fae66 8482 /* A DECL_EXPR can also be used as an expression, in the condition
325c3691
RH
8483 clause of an if/for/while construct. */
8484 return decl;
5566b478 8485 }
8d08fdba 8486
5566b478 8487 case FOR_STMT:
7c34ced1 8488 stmt = begin_for_stmt ();
015c2c66 8489 RECUR (FOR_INIT_STMT (t));
7c34ced1 8490 finish_for_init_stmt (stmt);
015c2c66 8491 tmp = RECUR (FOR_COND (t));
7c34ced1 8492 finish_for_cond (tmp, stmt);
015c2c66 8493 tmp = RECUR (FOR_EXPR (t));
7c34ced1 8494 finish_for_expr (tmp, stmt);
015c2c66 8495 RECUR (FOR_BODY (t));
7c34ced1 8496 finish_for_stmt (stmt);
5566b478 8497 break;
8d08fdba 8498
5566b478 8499 case WHILE_STMT:
7c34ced1 8500 stmt = begin_while_stmt ();
015c2c66 8501 tmp = RECUR (WHILE_COND (t));
7c34ced1 8502 finish_while_stmt_cond (tmp, stmt);
015c2c66 8503 RECUR (WHILE_BODY (t));
7c34ced1 8504 finish_while_stmt (stmt);
5566b478 8505 break;
8d08fdba 8506
5566b478 8507 case DO_STMT:
7c34ced1 8508 stmt = begin_do_stmt ();
015c2c66 8509 RECUR (DO_BODY (t));
7c34ced1 8510 finish_do_body (stmt);
015c2c66 8511 tmp = RECUR (DO_COND (t));
7c34ced1 8512 finish_do_stmt (tmp, stmt);
5566b478 8513 break;
a0a33927 8514
5566b478 8515 case IF_STMT:
7c34ced1 8516 stmt = begin_if_stmt ();
015c2c66 8517 tmp = RECUR (IF_COND (t));
7c34ced1 8518 finish_if_stmt_cond (tmp, stmt);
015c2c66 8519 RECUR (THEN_CLAUSE (t));
7c34ced1 8520 finish_then_clause (stmt);
8d08fdba 8521
7c34ced1
RH
8522 if (ELSE_CLAUSE (t))
8523 {
8524 begin_else_clause (stmt);
015c2c66 8525 RECUR (ELSE_CLAUSE (t));
7c34ced1
RH
8526 finish_else_clause (stmt);
8527 }
8528
8529 finish_if_stmt (stmt);
5566b478 8530 break;
8d08fdba 8531
5882f0f3 8532 case BIND_EXPR:
7c34ced1
RH
8533 if (BIND_EXPR_BODY_BLOCK (t))
8534 stmt = begin_function_body ();
8535 else
8536 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
8537 ? BCS_TRY_BLOCK : 0);
ade3dc07 8538
015c2c66 8539 RECUR (BIND_EXPR_BODY (t));
ade3dc07 8540
7c34ced1
RH
8541 if (BIND_EXPR_BODY_BLOCK (t))
8542 finish_function_body (stmt);
8543 else
8544 finish_compound_stmt (stmt);
5566b478 8545 break;
8d08fdba 8546
5566b478 8547 case BREAK_STMT:
ad321293 8548 finish_break_stmt ();
5566b478 8549 break;
8d08fdba 8550
6467930b 8551 case CONTINUE_STMT:
ad321293 8552 finish_continue_stmt ();
6467930b
MS
8553 break;
8554
5566b478 8555 case SWITCH_STMT:
7c34ced1 8556 stmt = begin_switch_stmt ();
015c2c66 8557 tmp = RECUR (SWITCH_STMT_COND (t));
7c34ced1 8558 finish_switch_cond (tmp, stmt);
015c2c66 8559 RECUR (SWITCH_STMT_BODY (t));
7c34ced1 8560 finish_switch_stmt (stmt);
5566b478
MS
8561 break;
8562
8c161995 8563 case CASE_LABEL_EXPR:
015c2c66
MM
8564 finish_case_label (RECUR (CASE_LOW (t)),
8565 RECUR (CASE_HIGH (t)));
5566b478
MS
8566 break;
8567
9e14e18f 8568 case LABEL_EXPR:
9e14e18f 8569 finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
5566b478
MS
8570 break;
8571
9e14e18f 8572 case GOTO_EXPR:
fd10dd09
JM
8573 tmp = GOTO_DESTINATION (t);
8574 if (TREE_CODE (tmp) != LABEL_DECL)
aa09da44
MM
8575 /* Computed goto's must be tsubst'd into. On the other hand,
8576 non-computed gotos must not be; the identifier in question
8577 will have no binding. */
015c2c66 8578 tmp = RECUR (tmp);
3fa56191 8579 else
fd10dd09
JM
8580 tmp = DECL_NAME (tmp);
8581 finish_goto_stmt (tmp);
ad321293
MM
8582 break;
8583
e130a54b 8584 case ASM_EXPR:
c87978aa 8585 tmp = finish_asm_stmt
6de9cd9a 8586 (ASM_VOLATILE_P (t),
015c2c66 8587 RECUR (ASM_STRING (t)),
f74dcfb7
JJ
8588 tsubst_copy_asm_operands (ASM_OUTPUTS (t), args, complain, in_decl),
8589 tsubst_copy_asm_operands (ASM_INPUTS (t), args, complain, in_decl),
8590 tsubst_copy_asm_operands (ASM_CLOBBERS (t), args, complain, in_decl));
131263fa
AP
8591 {
8592 tree asm_expr = tmp;
8593 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
8594 asm_expr = TREE_OPERAND (asm_expr, 0);
8595 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
8596 }
5566b478 8597 break;
faf5394a
MS
8598
8599 case TRY_BLOCK:
f1dedc31 8600 if (CLEANUP_P (t))
62409b39 8601 {
57b52417 8602 stmt = begin_try_block ();
015c2c66 8603 RECUR (TRY_STMTS (t));
62409b39 8604 finish_cleanup_try_block (stmt);
015c2c66 8605 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
62409b39 8606 }
f1dedc31
MM
8607 else
8608 {
eaf6fb90
MM
8609 tree compound_stmt = NULL_TREE;
8610
62409b39 8611 if (FN_TRY_BLOCK_P (t))
eaf6fb90 8612 stmt = begin_function_try_block (&compound_stmt);
62409b39
MM
8613 else
8614 stmt = begin_try_block ();
8615
015c2c66 8616 RECUR (TRY_STMTS (t));
62409b39
MM
8617
8618 if (FN_TRY_BLOCK_P (t))
8619 finish_function_try_block (stmt);
8620 else
8621 finish_try_block (stmt);
8622
015c2c66 8623 RECUR (TRY_HANDLERS (t));
84df082b 8624 if (FN_TRY_BLOCK_P (t))
eaf6fb90 8625 finish_function_handler_sequence (stmt, compound_stmt);
84df082b
MM
8626 else
8627 finish_handler_sequence (stmt);
f1dedc31 8628 }
faf5394a 8629 break;
c8094d83 8630
faf5394a 8631 case HANDLER:
b35d4555 8632 {
3a2419a7 8633 tree decl = HANDLER_PARMS (t);
b35d4555 8634
3a2419a7 8635 if (decl)
b35d4555 8636 {
b35d4555 8637 decl = tsubst (decl, args, complain, in_decl);
f8191e64
MM
8638 /* Prevent instantiate_decl from trying to instantiate
8639 this variable. We've already done all that needs to be
8640 done. */
3a2419a7
VR
8641 if (decl != error_mark_node)
8642 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
b35d4555 8643 }
3a2419a7 8644 stmt = begin_handler ();
1a6025b4 8645 finish_handler_parms (decl, stmt);
015c2c66 8646 RECUR (HANDLER_BODY (t));
1a6025b4 8647 finish_handler (stmt);
b35d4555 8648 }
faf5394a
MS
8649 break;
8650
b87692e5 8651 case TAG_DEFN:
fd10dd09 8652 tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
b87692e5
MS
8653 break;
8654
1799e5d5
RH
8655 case OMP_PARALLEL:
8656 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t),
8657 args, complain, in_decl);
8658 stmt = begin_omp_parallel ();
015c2c66 8659 RECUR (OMP_PARALLEL_BODY (t));
761041be
JJ
8660 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
8661 = OMP_PARALLEL_COMBINED (t);
1799e5d5
RH
8662 break;
8663
8664 case OMP_FOR:
8665 {
8666 tree clauses, decl, init, cond, incr, body, pre_body;
8667
8668 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t),
8669 args, complain, in_decl);
8670 init = OMP_FOR_INIT (t);
8671 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
015c2c66
MM
8672 decl = RECUR (TREE_OPERAND (init, 0));
8673 init = RECUR (TREE_OPERAND (init, 1));
8674 cond = RECUR (OMP_FOR_COND (t));
8675 incr = RECUR (OMP_FOR_INCR (t));
1799e5d5
RH
8676
8677 stmt = begin_omp_structured_block ();
8678
8679 pre_body = push_stmt_list ();
015c2c66 8680 RECUR (OMP_FOR_PRE_BODY (t));
1799e5d5
RH
8681 pre_body = pop_stmt_list (pre_body);
8682
8683 body = push_stmt_list ();
015c2c66 8684 RECUR (OMP_FOR_BODY (t));
1799e5d5
RH
8685 body = pop_stmt_list (body);
8686
8687 t = finish_omp_for (EXPR_LOCATION (t), decl, init, cond, incr, body,
8688 pre_body);
8689 if (t)
8690 OMP_FOR_CLAUSES (t) = clauses;
8691
8692 add_stmt (finish_omp_structured_block (stmt));
8693 }
8694 break;
8695
8696 case OMP_SECTIONS:
8697 case OMP_SINGLE:
8698 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), args, complain, in_decl);
8699 stmt = push_stmt_list ();
015c2c66 8700 RECUR (OMP_BODY (t));
1799e5d5
RH
8701 stmt = pop_stmt_list (stmt);
8702
8703 t = copy_node (t);
8704 OMP_BODY (t) = stmt;
8705 OMP_CLAUSES (t) = tmp;
8706 add_stmt (t);
8707 break;
8708
8709 case OMP_SECTION:
8710 case OMP_CRITICAL:
8711 case OMP_MASTER:
8712 case OMP_ORDERED:
8713 stmt = push_stmt_list ();
015c2c66 8714 RECUR (OMP_BODY (t));
1799e5d5
RH
8715 stmt = pop_stmt_list (stmt);
8716
8717 t = copy_node (t);
8718 OMP_BODY (t) = stmt;
8719 add_stmt (t);
8720 break;
8721
8722 case OMP_ATOMIC:
8723 {
8724 tree op0, op1;
015c2c66
MM
8725 op0 = RECUR (TREE_OPERAND (t, 0));
8726 op1 = RECUR (TREE_OPERAND (t, 1));
fe89d797 8727 finish_omp_atomic (OMP_ATOMIC_CODE (t), op0, op1);
1799e5d5
RH
8728 }
8729 break;
8730
5566b478 8731 default:
315fb5db 8732 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
c8094d83 8733
315fb5db 8734 return tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
8735 /*function_p=*/false,
8736 integral_constant_expression_p);
5566b478 8737 }
fd10dd09 8738
325c3691 8739 return NULL_TREE;
015c2c66 8740#undef RECUR
8d08fdba
MS
8741}
8742
b3445994
MM
8743/* T is a postfix-expression that is not being used in a function
8744 call. Return the substituted version of T. */
8745
8746static tree
c8094d83 8747tsubst_non_call_postfix_expression (tree t, tree args,
b3445994
MM
8748 tsubst_flags_t complain,
8749 tree in_decl)
8750{
8751 if (TREE_CODE (t) == SCOPE_REF)
8752 t = tsubst_qualified_id (t, args, complain, in_decl,
8753 /*done=*/false, /*address_p=*/false);
8754 else
8755 t = tsubst_copy_and_build (t, args, complain, in_decl,
015c2c66
MM
8756 /*function_p=*/false,
8757 /*integral_constant_expression_p=*/false);
b3445994
MM
8758
8759 return t;
8760}
8761
cc23546e 8762/* Like tsubst but deals with expressions and performs semantic
b3445994 8763 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
cc23546e 8764
ee76b931 8765tree
c8094d83 8766tsubst_copy_and_build (tree t,
0cbd7506
MS
8767 tree args,
8768 tsubst_flags_t complain,
8769 tree in_decl,
015c2c66
MM
8770 bool function_p,
8771 bool integral_constant_expression_p)
cc23546e 8772{
015c2c66
MM
8773#define RECUR(NODE) \
8774 tsubst_copy_and_build (NODE, args, complain, in_decl, \
8775 /*function_p=*/false, \
8776 integral_constant_expression_p)
b3445994 8777
ee76b931
MM
8778 tree op1;
8779
cc23546e
JO
8780 if (t == NULL_TREE || t == error_mark_node)
8781 return t;
8782
8783 switch (TREE_CODE (t))
8784 {
399dedb9
NS
8785 case USING_DECL:
8786 t = DECL_NAME (t);
852dcbdd 8787 /* Fall through. */
b3445994 8788 case IDENTIFIER_NODE:
cc23546e 8789 {
b3445994 8790 tree decl;
b3445994 8791 cp_id_kind idk;
67c03833 8792 bool non_integral_constant_expression_p;
b3445994
MM
8793 const char *error_msg;
8794
b3445994 8795 if (IDENTIFIER_TYPENAME_P (t))
cc23546e 8796 {
b3445994
MM
8797 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8798 t = mangle_conv_op_name_for_type (new_type);
cc23546e 8799 }
b3445994
MM
8800
8801 /* Look up the name. */
10e6657a 8802 decl = lookup_name (t);
b3445994
MM
8803
8804 /* By convention, expressions use ERROR_MARK_NODE to indicate
8805 failure, not NULL_TREE. */
8806 if (decl == NULL_TREE)
8807 decl = error_mark_node;
8808
10b1d5e7 8809 decl = finish_id_expression (t, decl, NULL_TREE,
b3445994 8810 &idk,
015c2c66 8811 integral_constant_expression_p,
67c03833
JM
8812 /*allow_non_integral_constant_expression_p=*/false,
8813 &non_integral_constant_expression_p,
02ed62dd
MM
8814 /*template_p=*/false,
8815 /*done=*/true,
8816 /*address_p=*/false,
8817 /*template_arg_p=*/false,
b3445994
MM
8818 &error_msg);
8819 if (error_msg)
8820 error (error_msg);
8821 if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
8822 decl = unqualified_name_lookup_error (decl);
8823 return decl;
cc23546e
JO
8824 }
8825
8826 case TEMPLATE_ID_EXPR:
8827 {
8828 tree object;
b3445994 8829 tree template = RECUR (TREE_OPERAND (t, 0));
bf12d54d
NS
8830 tree targs = TREE_OPERAND (t, 1);
8831
8832 if (targs)
8833 targs = tsubst_template_args (targs, args, complain, in_decl);
c8094d83 8834
cc23546e
JO
8835 if (TREE_CODE (template) == COMPONENT_REF)
8836 {
8837 object = TREE_OPERAND (template, 0);
8838 template = TREE_OPERAND (template, 1);
8839 }
8840 else
8841 object = NULL_TREE;
bd83b409 8842 template = lookup_template_function (template, targs);
c8094d83 8843
cc23546e 8844 if (object)
c8094d83 8845 return build3 (COMPONENT_REF, TREE_TYPE (template),
f293ce4b 8846 object, template, NULL_TREE);
cc23546e 8847 else
eff3a276 8848 return baselink_for_fns (template);
cc23546e
JO
8849 }
8850
8851 case INDIRECT_REF:
db24eb1f
NS
8852 {
8853 tree r = RECUR (TREE_OPERAND (t, 0));
8854
8855 if (REFERENCE_REF_P (t))
8856 {
e8c66fe0
NS
8857 /* A type conversion to reference type will be enclosed in
8858 such an indirect ref, but the substitution of the cast
8859 will have also added such an indirect ref. */
8860 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
8861 r = convert_from_reference (r);
db24eb1f
NS
8862 }
8863 else
8864 r = build_x_indirect_ref (r, "unary *");
8865 return r;
8866 }
cc23546e 8867
0da99d4e
GB
8868 case NOP_EXPR:
8869 return build_nop
8870 (tsubst (TREE_TYPE (t), args, complain, in_decl),
8871 RECUR (TREE_OPERAND (t, 0)));
8872
cc23546e 8873 case CAST_EXPR:
cc23546e 8874 case REINTERPRET_CAST_EXPR:
cc23546e 8875 case CONST_CAST_EXPR:
cc23546e 8876 case DYNAMIC_CAST_EXPR:
cc23546e 8877 case STATIC_CAST_EXPR:
015c2c66
MM
8878 {
8879 tree type;
8880 tree op;
8881
8882 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
8883 if (integral_constant_expression_p
8884 && !cast_valid_in_integral_constant_expression_p (type))
8885 {
8886 error ("a cast to a type other than an integral or "
8887 "enumeration type cannot appear in a constant-expression");
8888 return error_mark_node;
8889 }
8890
8891 op = RECUR (TREE_OPERAND (t, 0));
8892
8893 switch (TREE_CODE (t))
8894 {
8895 case CAST_EXPR:
8896 return build_functional_cast (type, op);
8897 case REINTERPRET_CAST_EXPR:
8898 return build_reinterpret_cast (type, op);
8899 case CONST_CAST_EXPR:
8900 return build_const_cast (type, op);
8901 case DYNAMIC_CAST_EXPR:
8902 return build_dynamic_cast (type, op);
8903 case STATIC_CAST_EXPR:
8904 return build_static_cast (type, op);
8905 default:
8906 gcc_unreachable ();
8907 }
8908 }
cc23546e 8909
cc23546e
JO
8910 case POSTDECREMENT_EXPR:
8911 case POSTINCREMENT_EXPR:
b3445994
MM
8912 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8913 args, complain, in_decl);
ee76b931
MM
8914 return build_x_unary_op (TREE_CODE (t), op1);
8915
8916 case PREDECREMENT_EXPR:
8917 case PREINCREMENT_EXPR:
cc23546e
JO
8918 case NEGATE_EXPR:
8919 case BIT_NOT_EXPR:
cc23546e 8920 case ABS_EXPR:
d17811fd 8921 case TRUTH_NOT_EXPR:
392e3d51 8922 case UNARY_PLUS_EXPR: /* Unary + */
d17811fd
MM
8923 case REALPART_EXPR:
8924 case IMAGPART_EXPR:
b3445994 8925 return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
cc23546e 8926
cc23546e 8927 case ADDR_EXPR:
ee76b931
MM
8928 op1 = TREE_OPERAND (t, 0);
8929 if (TREE_CODE (op1) == SCOPE_REF)
c8094d83 8930 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
ee76b931
MM
8931 /*done=*/true, /*address_p=*/true);
8932 else
c8094d83 8933 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
b3445994 8934 in_decl);
fc2b8477
MM
8935 if (TREE_CODE (op1) == LABEL_DECL)
8936 return finish_label_address_expr (DECL_NAME (op1));
ee76b931
MM
8937 return build_x_unary_op (ADDR_EXPR, op1);
8938
cc23546e
JO
8939 case PLUS_EXPR:
8940 case MINUS_EXPR:
8941 case MULT_EXPR:
8942 case TRUNC_DIV_EXPR:
8943 case CEIL_DIV_EXPR:
8944 case FLOOR_DIV_EXPR:
8945 case ROUND_DIV_EXPR:
8946 case EXACT_DIV_EXPR:
8947 case BIT_AND_EXPR:
cc23546e
JO
8948 case BIT_IOR_EXPR:
8949 case BIT_XOR_EXPR:
8950 case TRUNC_MOD_EXPR:
8951 case FLOOR_MOD_EXPR:
8952 case TRUTH_ANDIF_EXPR:
8953 case TRUTH_ORIF_EXPR:
8954 case TRUTH_AND_EXPR:
8955 case TRUTH_OR_EXPR:
8956 case RSHIFT_EXPR:
8957 case LSHIFT_EXPR:
8958 case RROTATE_EXPR:
8959 case LROTATE_EXPR:
8960 case EQ_EXPR:
8961 case NE_EXPR:
8962 case MAX_EXPR:
8963 case MIN_EXPR:
8964 case LE_EXPR:
8965 case GE_EXPR:
8966 case LT_EXPR:
8967 case GT_EXPR:
8968 case MEMBER_REF:
b3445994 8969 case DOTSTAR_EXPR:
cc23546e 8970 return build_x_binary_op
c8094d83 8971 (TREE_CODE (t),
b3445994 8972 RECUR (TREE_OPERAND (t, 0)),
ec835fb2
MM
8973 RECUR (TREE_OPERAND (t, 1)),
8974 /*overloaded_p=*/NULL);
cc23546e
JO
8975
8976 case SCOPE_REF:
ee76b931
MM
8977 return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
8978 /*address_p=*/false);
cc23546e 8979 case ARRAY_REF:
b3445994
MM
8980 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
8981 args, complain, in_decl);
d8987adb
NS
8982 return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
8983 /*overloaded_p=*/NULL);
c8094d83 8984
cc23546e
JO
8985 case SIZEOF_EXPR:
8986 case ALIGNOF_EXPR:
d17811fd
MM
8987 op1 = TREE_OPERAND (t, 0);
8988 if (!args)
8989 {
8990 /* When there are no ARGS, we are trying to evaluate a
8991 non-dependent expression from the parser. Trying to do
8992 the substitutions may not work. */
8993 if (!TYPE_P (op1))
8994 op1 = TREE_TYPE (op1);
8995 }
8996 else
8997 {
8998 ++skip_evaluation;
015c2c66
MM
8999 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
9000 /*function_p=*/false,
9001 /*integral_constant_expression_p=*/false);
d17811fd
MM
9002 --skip_evaluation;
9003 }
7a18b933
NS
9004 if (TYPE_P (op1))
9005 return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
d17811fd 9006 else
7a18b933 9007 return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
cc23546e
JO
9008
9009 case MODOP_EXPR:
e4c2c34b
JM
9010 {
9011 tree r = build_x_modify_expr
9012 (RECUR (TREE_OPERAND (t, 0)),
9013 TREE_CODE (TREE_OPERAND (t, 1)),
9014 RECUR (TREE_OPERAND (t, 2)));
bcf9a914
JM
9015 /* TREE_NO_WARNING must be set if either the expression was
9016 parenthesized or it uses an operator such as >>= rather
9017 than plain assignment. In the former case, it was already
9018 set and must be copied. In the latter case,
9019 build_x_modify_expr sets it and it must not be reset
9020 here. */
9021 if (TREE_NO_WARNING (t))
9022 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
e4c2c34b
JM
9023 return r;
9024 }
cc23546e
JO
9025
9026 case ARROW_EXPR:
b3445994
MM
9027 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9028 args, complain, in_decl);
ee76b931
MM
9029 /* Remember that there was a reference to this entity. */
9030 if (DECL_P (op1))
9031 mark_used (op1);
9032 return build_x_arrow (op1);
cc23546e
JO
9033
9034 case NEW_EXPR:
9035 return build_new
b3445994
MM
9036 (RECUR (TREE_OPERAND (t, 0)),
9037 RECUR (TREE_OPERAND (t, 1)),
9038 RECUR (TREE_OPERAND (t, 2)),
058b15c1 9039 RECUR (TREE_OPERAND (t, 3)),
cc23546e
JO
9040 NEW_EXPR_USE_GLOBAL (t));
9041
9042 case DELETE_EXPR:
9043 return delete_sanity
b3445994
MM
9044 (RECUR (TREE_OPERAND (t, 0)),
9045 RECUR (TREE_OPERAND (t, 1)),
cc23546e
JO
9046 DELETE_EXPR_USE_VEC (t),
9047 DELETE_EXPR_USE_GLOBAL (t));
9048
9049 case COMPOUND_EXPR:
b3445994
MM
9050 return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
9051 RECUR (TREE_OPERAND (t, 1)));
cc23546e 9052
ee76b931 9053 case CALL_EXPR:
cc23546e 9054 {
ee76b931
MM
9055 tree function;
9056 tree call_args;
ee76b931 9057 bool qualified_p;
ee935db4 9058 bool koenig_p;
ee76b931
MM
9059
9060 function = TREE_OPERAND (t, 0);
6d80c4b9
MM
9061 /* When we parsed the expression, we determined whether or
9062 not Koenig lookup should be performed. */
9063 koenig_p = KOENIG_LOOKUP_P (t);
ee76b931 9064 if (TREE_CODE (function) == SCOPE_REF)
cc23546e 9065 {
ee76b931
MM
9066 qualified_p = true;
9067 function = tsubst_qualified_id (function, args, complain, in_decl,
c8094d83 9068 /*done=*/false,
ee76b931 9069 /*address_p=*/false);
cc23546e 9070 }
ee76b931 9071 else
cc23546e 9072 {
61e71a9e
NS
9073 if (TREE_CODE (function) == COMPONENT_REF)
9074 {
9075 tree op = TREE_OPERAND (function, 1);
9076
9077 qualified_p = (TREE_CODE (op) == SCOPE_REF
9078 || (BASELINK_P (op)
9079 && BASELINK_QUALIFIED_P (op)));
9080 }
9081 else
9082 qualified_p = false;
3db45ab5 9083
c8094d83 9084 function = tsubst_copy_and_build (function, args, complain,
b3445994 9085 in_decl,
015c2c66
MM
9086 !qualified_p,
9087 integral_constant_expression_p);
9088
6d80c4b9
MM
9089 if (BASELINK_P (function))
9090 qualified_p = true;
cc23546e 9091 }
cc23546e 9092
b3445994 9093 call_args = RECUR (TREE_OPERAND (t, 1));
676e33ca
MM
9094
9095 /* We do not perform argument-dependent lookup if normal
9096 lookup finds a non-function, in accordance with the
9097 expected resolution of DR 218. */
ee935db4 9098 if (koenig_p
44370687
MM
9099 && ((is_overloaded_fn (function)
9100 /* If lookup found a member function, the Koenig lookup is
9101 not appropriate, even if an unqualified-name was used
9102 to denote the function. */
9103 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
b3445994 9104 || TREE_CODE (function) == IDENTIFIER_NODE))
6d80c4b9
MM
9105 function = perform_koenig_lookup (function, call_args);
9106
9107 if (TREE_CODE (function) == IDENTIFIER_NODE)
b3445994 9108 {
6d80c4b9
MM
9109 unqualified_name_lookup_error (function);
9110 return error_mark_node;
b3445994
MM
9111 }
9112
9113 /* Remember that there was a reference to this entity. */
9114 if (DECL_P (function))
9115 mark_used (function);
9116
d17811fd
MM
9117 if (TREE_CODE (function) == OFFSET_REF)
9118 return build_offset_ref_call_from_tree (function, call_args);
9119 if (TREE_CODE (function) == COMPONENT_REF)
9f880ef9
MM
9120 {
9121 if (!BASELINK_P (TREE_OPERAND (function, 1)))
9122 return finish_call_expr (function, call_args,
9123 /*disallow_virtual=*/false,
9124 /*koenig_p=*/false);
9125 else
c8094d83 9126 return (build_new_method_call
9f880ef9
MM
9127 (TREE_OPERAND (function, 0),
9128 TREE_OPERAND (function, 1),
c8094d83 9129 call_args, NULL_TREE,
63c9a190
MM
9130 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
9131 /*fn_p=*/NULL));
9f880ef9 9132 }
c8094d83 9133 return finish_call_expr (function, call_args,
6d80c4b9
MM
9134 /*disallow_virtual=*/qualified_p,
9135 koenig_p);
cc23546e
JO
9136 }
9137
9138 case COND_EXPR:
9139 return build_x_conditional_expr
b3445994
MM
9140 (RECUR (TREE_OPERAND (t, 0)),
9141 RECUR (TREE_OPERAND (t, 1)),
9142 RECUR (TREE_OPERAND (t, 2)));
cc23546e
JO
9143
9144 case PSEUDO_DTOR_EXPR:
c8094d83 9145 return finish_pseudo_destructor_expr
b3445994
MM
9146 (RECUR (TREE_OPERAND (t, 0)),
9147 RECUR (TREE_OPERAND (t, 1)),
9148 RECUR (TREE_OPERAND (t, 2)));
cc23546e
JO
9149
9150 case TREE_LIST:
9151 {
9152 tree purpose, value, chain;
9153
9154 if (t == void_list_node)
9155 return t;
9156
9157 purpose = TREE_PURPOSE (t);
9158 if (purpose)
b3445994 9159 purpose = RECUR (purpose);
cc23546e
JO
9160 value = TREE_VALUE (t);
9161 if (value)
b3445994 9162 value = RECUR (value);
cc23546e
JO
9163 chain = TREE_CHAIN (t);
9164 if (chain && chain != void_type_node)
b3445994 9165 chain = RECUR (chain);
cc23546e
JO
9166 if (purpose == TREE_PURPOSE (t)
9167 && value == TREE_VALUE (t)
9168 && chain == TREE_CHAIN (t))
9169 return t;
9170 return tree_cons (purpose, value, chain);
9171 }
9172
9173 case COMPONENT_REF:
9174 {
ee76b931 9175 tree object;
3897c0aa 9176 tree object_type;
ee76b931
MM
9177 tree member;
9178
b3445994
MM
9179 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
9180 args, complain, in_decl);
ee76b931
MM
9181 /* Remember that there was a reference to this entity. */
9182 if (DECL_P (object))
9183 mark_used (object);
3897c0aa 9184 object_type = TREE_TYPE (object);
ee76b931
MM
9185
9186 member = TREE_OPERAND (t, 1);
9187 if (BASELINK_P (member))
c8094d83 9188 member = tsubst_baselink (member,
ee76b931
MM
9189 non_reference (TREE_TYPE (object)),
9190 args, complain, in_decl);
9191 else
9192 member = tsubst_copy (member, args, complain, in_decl);
bad1f462
KL
9193 if (member == error_mark_node)
9194 return error_mark_node;
3897c0aa
MM
9195
9196 if (object_type && !CLASS_TYPE_P (object_type))
cc23546e
JO
9197 {
9198 if (TREE_CODE (member) == BIT_NOT_EXPR)
c8094d83 9199 return finish_pseudo_destructor_expr (object,
cc23546e 9200 NULL_TREE,
3897c0aa 9201 object_type);
cc23546e
JO
9202 else if (TREE_CODE (member) == SCOPE_REF
9203 && (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
c8094d83 9204 return finish_pseudo_destructor_expr (object,
cc23546e 9205 object,
3897c0aa 9206 object_type);
cc23546e
JO
9207 }
9208 else if (TREE_CODE (member) == SCOPE_REF
9209 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
9210 {
9211 tree tmpl;
9212 tree args;
c8094d83 9213
cc23546e
JO
9214 /* Lookup the template functions now that we know what the
9215 scope is. */
9216 tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
9217 args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
c8094d83 9218 member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
12483c9f
NS
9219 /*is_type_p=*/false,
9220 /*complain=*/false);
cc23546e 9221 if (BASELINK_P (member))
44370687 9222 {
c8094d83 9223 BASELINK_FUNCTIONS (member)
44370687
MM
9224 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
9225 args);
c8094d83
MS
9226 member = (adjust_result_of_qualified_name_lookup
9227 (member, BINFO_TYPE (BASELINK_BINFO (member)),
3897c0aa 9228 object_type));
44370687 9229 }
cc23546e
JO
9230 else
9231 {
3897c0aa 9232 qualified_name_lookup_error (object_type, tmpl, member);
cc23546e
JO
9233 return error_mark_node;
9234 }
9235 }
2436b51f
MM
9236 else if (TREE_CODE (member) == SCOPE_REF
9237 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
9238 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
9239 {
9240 if (complain & tf_error)
9241 {
9242 if (TYPE_P (TREE_OPERAND (member, 0)))
c8094d83 9243 error ("%qT is not a class or namespace",
2436b51f
MM
9244 TREE_OPERAND (member, 0));
9245 else
c8094d83 9246 error ("%qD is not a class or namespace",
2436b51f
MM
9247 TREE_OPERAND (member, 0));
9248 }
9249 return error_mark_node;
9250 }
a3f10e50
NS
9251 else if (TREE_CODE (member) == FIELD_DECL)
9252 return finish_non_static_data_member (member, object, NULL_TREE);
cc23546e 9253
02ed62dd
MM
9254 return finish_class_member_access_expr (object, member,
9255 /*template_p=*/false);
cc23546e
JO
9256 }
9257
9258 case THROW_EXPR:
9259 return build_throw
b3445994 9260 (RECUR (TREE_OPERAND (t, 0)));
cc23546e
JO
9261
9262 case CONSTRUCTOR:
9263 {
4038c495
GB
9264 VEC(constructor_elt,gc) *n;
9265 constructor_elt *ce;
9266 unsigned HOST_WIDE_INT idx;
cc23546e 9267 tree r;
cc23546e 9268 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
4038c495 9269 bool process_index_p;
cc23546e 9270
a97728cf
VR
9271 if (type == error_mark_node)
9272 return error_mark_node;
9273
cc23546e
JO
9274 /* digest_init will do the wrong thing if we let it. */
9275 if (type && TYPE_PTRMEMFUNC_P (type))
9276 return t;
9277
4038c495 9278 /* We do not want to process the index of aggregate
cc23546e
JO
9279 initializers as they are identifier nodes which will be
9280 looked up by digest_init. */
4038c495 9281 process_index_p = !(type && IS_AGGR_TYPE (type));
c8094d83 9282
4038c495
GB
9283 n = VEC_copy (constructor_elt, gc, CONSTRUCTOR_ELTS (t));
9284 for (idx = 0; VEC_iterate (constructor_elt, n, idx, ce); idx++)
9285 {
9286 if (ce->index && process_index_p)
9287 ce->index = RECUR (ce->index);
9288 ce->value = RECUR (ce->value);
cc23546e 9289 }
c8094d83 9290
4038c495 9291 r = build_constructor (NULL_TREE, n);
cc23546e
JO
9292 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
9293
9294 if (type)
4038c495 9295 return digest_init (type, r);
cc23546e
JO
9296 return r;
9297 }
9298
9299 case TYPEID_EXPR:
9300 {
b3445994 9301 tree operand_0 = RECUR (TREE_OPERAND (t, 0));
cc23546e
JO
9302 if (TYPE_P (operand_0))
9303 return get_typeid (operand_0);
9304 return build_typeid (operand_0);
9305 }
9306
cc23546e 9307 case VAR_DECL:
db24eb1f
NS
9308 if (!args)
9309 return t;
9310 /* Fall through */
c8094d83 9311
db24eb1f
NS
9312 case PARM_DECL:
9313 {
9314 tree r = tsubst_copy (t, args, complain, in_decl);
c8094d83 9315
db24eb1f
NS
9316 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
9317 /* If the original type was a reference, we'll be wrapped in
9318 the appropriate INDIRECT_REF. */
9319 r = convert_from_reference (r);
9320 return r;
9321 }
cc23546e
JO
9322
9323 case VA_ARG_EXPR:
b3445994 9324 return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
c8094d83 9325 tsubst_copy (TREE_TYPE (t), args, complain,
b3445994 9326 in_decl));
cc23546e 9327
4bceb077 9328 case OFFSETOF_EXPR:
c291f8b1 9329 return finish_offsetof (RECUR (TREE_OPERAND (t, 0)));
4bceb077 9330
0fe0caa6
RH
9331 case STMT_EXPR:
9332 {
9333 tree old_stmt_expr = cur_stmt_expr;
9334 tree stmt_expr = begin_stmt_expr ();
9335
9336 cur_stmt_expr = stmt_expr;
015c2c66
MM
9337 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
9338 integral_constant_expression_p);
0fe0caa6
RH
9339 stmt_expr = finish_stmt_expr (stmt_expr, false);
9340 cur_stmt_expr = old_stmt_expr;
9341
9342 return stmt_expr;
9343 }
9344
a5201a91
MM
9345 case CONST_DECL:
9346 t = tsubst_copy (t, args, complain, in_decl);
9347 /* As in finish_id_expression, we resolve enumeration constants
9348 to their underlying values. */
9349 if (TREE_CODE (t) == CONST_DECL)
6193b8b7
DJ
9350 {
9351 used_types_insert (TREE_TYPE (t));
9352 return DECL_INITIAL (t);
9353 }
a5201a91
MM
9354 return t;
9355
cc23546e 9356 default:
e58a9aa1
ZL
9357 /* Handle Objective-C++ constructs, if appropriate. */
9358 {
9359 tree subst
9360 = objcp_tsubst_copy_and_build (t, args, complain,
9361 in_decl, /*function_p=*/false);
9362 if (subst)
9363 return subst;
9364 }
cc23546e
JO
9365 return tsubst_copy (t, args, complain, in_decl);
9366 }
b3445994
MM
9367
9368#undef RECUR
cc23546e
JO
9369}
9370
3e4a3562
NS
9371/* Verify that the instantiated ARGS are valid. For type arguments,
9372 make sure that the type's linkage is ok. For non-type arguments,
34cd5ae7 9373 make sure they are constants if they are integral or enumerations.
9bcb9aae 9374 Emit an error under control of COMPLAIN, and return TRUE on error. */
3e4a3562
NS
9375
9376static bool
3a978d72 9377check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
3e4a3562
NS
9378{
9379 int ix, len = DECL_NTPARMS (tmpl);
9380 bool result = false;
9381
9382 for (ix = 0; ix != len; ix++)
9383 {
9384 tree t = TREE_VEC_ELT (args, ix);
c8094d83 9385
3e4a3562
NS
9386 if (TYPE_P (t))
9387 {
9388 /* [basic.link]: A name with no linkage (notably, the name
9389 of a class or enumeration declared in a local scope)
9390 shall not be used to declare an entity with linkage.
9391 This implies that names with no linkage cannot be used as
9392 template arguments. */
4684cd27 9393 tree nt = no_linkage_check (t, /*relaxed_p=*/false);
3e4a3562
NS
9394
9395 if (nt)
9396 {
cb807ba3 9397 /* DR 488 makes use of a type with no linkage cause
3db45ab5 9398 type deduction to fail. */
2010cdcd
MM
9399 if (complain & tf_error)
9400 {
9401 if (TYPE_ANONYMOUS_P (nt))
9402 error ("%qT is/uses anonymous type", t);
9403 else
9df540a8 9404 error ("template argument for %qD uses local type %qT",
3db45ab5 9405 tmpl, t);
2010cdcd 9406 }
3e4a3562
NS
9407 result = true;
9408 }
9409 /* In order to avoid all sorts of complications, we do not
9410 allow variably-modified types as template arguments. */
5377d5ba 9411 else if (variably_modified_type_p (t, NULL_TREE))
3e4a3562
NS
9412 {
9413 if (complain & tf_error)
0f51ccfc 9414 error ("%qT is a variably modified type", t);
3e4a3562
NS
9415 result = true;
9416 }
9417 }
9418 /* A non-type argument of integral or enumerated type must be a
9419 constant. */
9420 else if (TREE_TYPE (t)
9421 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
9422 && !TREE_CONSTANT (t))
9423 {
9424 if (complain & tf_error)
0f51ccfc 9425 error ("integral expression %qE is not constant", t);
3e4a3562
NS
9426 result = true;
9427 }
9428 }
2010cdcd 9429 if (result && (complain & tf_error))
0f51ccfc 9430 error (" trying to instantiate %qD", tmpl);
3e4a3562
NS
9431 return result;
9432}
9433
6ba89f8e 9434/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
9435 the template arguments in TARG_PTR. */
9436
5566b478 9437tree
3a978d72 9438instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
8d08fdba 9439{
5566b478 9440 tree fndecl;
36a117a5
MM
9441 tree gen_tmpl;
9442 tree spec;
a95799ec 9443 HOST_WIDE_INT saved_processing_template_decl;
5566b478 9444
27fafc8d
JM
9445 if (tmpl == error_mark_node)
9446 return error_mark_node;
9447
50bc768d 9448 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
386b8a85 9449
db9b2174
MM
9450 /* If this function is a clone, handle it specially. */
9451 if (DECL_CLONED_FUNCTION_P (tmpl))
3ad97789 9452 {
a30f62e0 9453 tree spec;
3ad97789 9454 tree clone;
c8094d83 9455
a30f62e0
MM
9456 spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
9457 complain);
9458 if (spec == error_mark_node)
9459 return error_mark_node;
9460
c6002625 9461 /* Look for the clone. */
4684cd27 9462 FOR_EACH_CLONE (clone, spec)
3ad97789
NS
9463 if (DECL_NAME (clone) == DECL_NAME (tmpl))
9464 return clone;
9465 /* We should always have found the clone by now. */
315fb5db 9466 gcc_unreachable ();
3ad97789
NS
9467 return NULL_TREE;
9468 }
c8094d83 9469
36a117a5 9470 /* Check to see if we already have this specialization. */
c8094d83 9471 spec = retrieve_specialization (tmpl, targ_ptr,
c7222c02 9472 /*class_specializations_p=*/false);
36a117a5
MM
9473 if (spec != NULL_TREE)
9474 return spec;
9475
f9a7ae04
MM
9476 gen_tmpl = most_general_template (tmpl);
9477 if (tmpl != gen_tmpl)
386b8a85 9478 {
36a117a5
MM
9479 /* The TMPL is a partial instantiation. To get a full set of
9480 arguments we must add the arguments used to perform the
9481 partial instantiation. */
9482 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
9483 targ_ptr);
36a117a5
MM
9484
9485 /* Check to see if we already have this specialization. */
c7222c02
MM
9486 spec = retrieve_specialization (gen_tmpl, targ_ptr,
9487 /*class_specializations_p=*/false);
75650646
MM
9488 if (spec != NULL_TREE)
9489 return spec;
386b8a85
JM
9490 }
9491
3e4a3562
NS
9492 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
9493 complain))
9494 return error_mark_node;
c8094d83 9495
2b907f5c
KL
9496 /* We are building a FUNCTION_DECL, during which the access of its
9497 parameters and return types have to be checked. However this
9498 FUNCTION_DECL which is the desired context for access checking
9499 is not built yet. We solve this chicken-and-egg problem by
9500 deferring all checks until we have the FUNCTION_DECL. */
9501 push_deferring_access_checks (dk_deferred);
5c74d5b0 9502
a95799ec
MM
9503 /* Although PROCESSING_TEMPLATE_DECL may be true at this point
9504 (because, for example, we have encountered a non-dependent
a1d08991
MM
9505 function call in the body of a template function and must now
9506 determine which of several overloaded functions will be called),
9507 within the instantiation itself we are not processing a
9508 template. */
a95799ec
MM
9509 saved_processing_template_decl = processing_template_decl;
9510 processing_template_decl = 0;
9511 /* Substitute template parameters to obtain the specialization. */
17aec3eb 9512 fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
3e4a3562 9513 targ_ptr, complain, gen_tmpl);
a95799ec 9514 processing_template_decl = saved_processing_template_decl;
6b6b60af
MM
9515 if (fndecl == error_mark_node)
9516 return error_mark_node;
5c74d5b0 9517
2b907f5c
KL
9518 /* Now we know the specialization, compute access previously
9519 deferred. */
9520 push_access_scope (fndecl);
9521 perform_deferred_access_checks ();
9522 pop_access_scope (fndecl);
9523 pop_deferring_access_checks ();
5c74d5b0 9524
36a117a5
MM
9525 /* The DECL_TI_TEMPLATE should always be the immediate parent
9526 template, not the most general template. */
9527 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 9528
94350948 9529 /* If we've just instantiated the main entry point for a function,
3ad97789
NS
9530 instantiate all the alternate entry points as well. We do this
9531 by cloning the instantiation of the main entry point, not by
9532 instantiating the template clones. */
9533 if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
9534 clone_function_decl (fndecl, /*update_method_vec_p=*/0);
94350948 9535
5566b478 9536 return fndecl;
8d08fdba 9537}
5566b478 9538
4393e105
MM
9539/* The FN is a TEMPLATE_DECL for a function. The ARGS are the
9540 arguments that are being used when calling it. TARGS is a vector
c8094d83 9541 into which the deduced template arguments are placed.
8d08fdba
MS
9542
9543 Return zero for success, 2 for an incomplete match that doesn't resolve
9544 all the types, and 1 for complete failure. An error message will be
9545 printed only for an incomplete match.
9546
e5214479
JM
9547 If FN is a conversion operator, or we are trying to produce a specific
9548 specialization, RETURN_TYPE is the return type desired.
9f54c803
MM
9549
9550 The EXPLICIT_TARGS are explicit template arguments provided via a
9551 template-id.
6467930b 9552
830bfa74
MM
9553 The parameter STRICT is one of:
9554
c8094d83 9555 DEDUCE_CALL:
830bfa74
MM
9556 We are deducing arguments for a function call, as in
9557 [temp.deduct.call].
9558
9559 DEDUCE_CONV:
c8094d83 9560 We are deducing arguments for a conversion function, as in
830bfa74
MM
9561 [temp.deduct.conv].
9562
9563 DEDUCE_EXACT:
62e4a758
NS
9564 We are deducing arguments when doing an explicit instantiation
9565 as in [temp.explicit], when determining an explicit specialization
9566 as in [temp.expl.spec], or when taking the address of a function
a34d3336 9567 template, as in [temp.deduct.funcaddr]. */
8d08fdba
MS
9568
9569int
c8094d83 9570fn_type_unification (tree fn,
0cbd7506
MS
9571 tree explicit_targs,
9572 tree targs,
9573 tree args,
9574 tree return_type,
30f86ec3
FJ
9575 unification_kind_t strict,
9576 int flags)
386b8a85 9577{
4393e105
MM
9578 tree parms;
9579 tree fntype;
adecb3f4 9580 int result;
386b8a85 9581
50bc768d 9582 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
2b59fc25 9583
4393e105
MM
9584 fntype = TREE_TYPE (fn);
9585 if (explicit_targs)
75650646 9586 {
4393e105 9587 /* [temp.deduct]
c8094d83 9588
4393e105
MM
9589 The specified template arguments must match the template
9590 parameters in kind (i.e., type, nontype, template), and there
9591 must not be more arguments than there are parameters;
9592 otherwise type deduction fails.
9593
9594 Nontype arguments must match the types of the corresponding
9595 nontype template parameters, or must be convertible to the
9596 types of the corresponding nontype parameters as specified in
9597 _temp.arg.nontype_, otherwise type deduction fails.
9598
9599 All references in the function type of the function template
9600 to the corresponding template parameters are replaced by the
9601 specified template argument values. If a substitution in a
9602 template parameter or in the function type of the function
9603 template results in an invalid type, type deduction fails. */
9604 int i;
9605 tree converted_args;
ffd49b19 9606 bool incomplete;
75650646 9607
c8a7ed43
AO
9608 if (explicit_targs == error_mark_node)
9609 return 1;
9610
4393e105 9611 converted_args
c8094d83
MS
9612 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
9613 explicit_targs, NULL_TREE, tf_none,
e7e93965
MM
9614 /*require_all_args=*/false,
9615 /*use_default_args=*/false));
4393e105 9616 if (converted_args == error_mark_node)
75650646 9617 return 1;
386b8a85 9618
ffd49b19 9619 /* Substitute the explicit args into the function type. This is
0cbd7506
MS
9620 necessary so that, for instance, explicitly declared function
9621 arguments can match null pointed constants. If we were given
9622 an incomplete set of explicit args, we must not do semantic
9623 processing during substitution as we could create partial
9624 instantiations. */
ffd49b19
NS
9625 incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
9626 processing_template_decl += incomplete;
c2ea3a40 9627 fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
ffd49b19 9628 processing_template_decl -= incomplete;
c8094d83 9629
4393e105
MM
9630 if (fntype == error_mark_node)
9631 return 1;
050367a3 9632
4393e105 9633 /* Place the explicitly specified arguments in TARGS. */
ffd49b19 9634 for (i = NUM_TMPL_ARGS (converted_args); i--;)
4393e105 9635 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
75650646 9636 }
c8094d83 9637
4393e105 9638 parms = TYPE_ARG_TYPES (fntype);
e5214479
JM
9639 /* Never do unification on the 'this' parameter. */
9640 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
9641 parms = TREE_CHAIN (parms);
c8094d83 9642
8d3631f8
NS
9643 if (return_type)
9644 {
8d3631f8
NS
9645 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
9646 args = tree_cons (NULL_TREE, return_type, args);
4393e105
MM
9647 }
9648
4393e105
MM
9649 /* We allow incomplete unification without an error message here
9650 because the standard doesn't seem to explicitly prohibit it. Our
9651 callers must be ready to deal with unification failures in any
9652 event. */
c8094d83 9653 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
adecb3f4 9654 targs, parms, args, /*subr=*/0,
30f86ec3 9655 strict, flags);
adecb3f4 9656
c8094d83 9657 if (result == 0)
adecb3f4 9658 /* All is well so far. Now, check:
c8094d83
MS
9659
9660 [temp.deduct]
9661
adecb3f4
MM
9662 When all template arguments have been deduced, all uses of
9663 template parameters in nondeduced contexts are replaced with
9664 the corresponding deduced argument values. If the
9665 substitution results in an invalid type, as described above,
9666 type deduction fails. */
c2ea3a40 9667 if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
adecb3f4
MM
9668 == error_mark_node)
9669 return 1;
9670
9671 return result;
830bfa74
MM
9672}
9673
9674/* Adjust types before performing type deduction, as described in
9675 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
9676 sections are symmetric. PARM is the type of a function parameter
9677 or the return type of the conversion function. ARG is the type of
9678 the argument passed to the call, or the type of the value
dc957d14 9679 initialized with the result of the conversion function. */
386b8a85 9680
62e4a758 9681static int
c8094d83 9682maybe_adjust_types_for_deduction (unification_kind_t strict,
0cbd7506
MS
9683 tree* parm,
9684 tree* arg)
830bfa74 9685{
62e4a758 9686 int result = 0;
c8094d83 9687
830bfa74
MM
9688 switch (strict)
9689 {
9690 case DEDUCE_CALL:
9691 break;
9692
9693 case DEDUCE_CONV:
9694 {
4c7d0dff
MM
9695 /* Swap PARM and ARG throughout the remainder of this
9696 function; the handling is precisely symmetric since PARM
9697 will initialize ARG rather than vice versa. */
830bfa74
MM
9698 tree* temp = parm;
9699 parm = arg;
9700 arg = temp;
9701 break;
9702 }
9703
9704 case DEDUCE_EXACT:
9705 /* There is nothing to do in this case. */
62e4a758 9706 return 0;
830bfa74
MM
9707
9708 default:
315fb5db 9709 gcc_unreachable ();
830bfa74
MM
9710 }
9711
9712 if (TREE_CODE (*parm) != REFERENCE_TYPE)
9713 {
9714 /* [temp.deduct.call]
c8094d83 9715
830bfa74 9716 If P is not a reference type:
c8094d83 9717
830bfa74
MM
9718 --If A is an array type, the pointer type produced by the
9719 array-to-pointer standard conversion (_conv.array_) is
9720 used in place of A for type deduction; otherwise,
c8094d83 9721
830bfa74
MM
9722 --If A is a function type, the pointer type produced by
9723 the function-to-pointer standard conversion
9724 (_conv.func_) is used in place of A for type deduction;
9725 otherwise,
c8094d83 9726
830bfa74
MM
9727 --If A is a cv-qualified type, the top level
9728 cv-qualifiers of A's type are ignored for type
9729 deduction. */
9730 if (TREE_CODE (*arg) == ARRAY_TYPE)
9731 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 9732 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
9733 *arg = build_pointer_type (*arg);
9734 else
9735 *arg = TYPE_MAIN_VARIANT (*arg);
9736 }
c8094d83 9737
830bfa74 9738 /* [temp.deduct.call]
c8094d83 9739
830bfa74
MM
9740 If P is a cv-qualified type, the top level cv-qualifiers
9741 of P's type are ignored for type deduction. If P is a
9742 reference type, the type referred to by P is used for
9743 type deduction. */
9744 *parm = TYPE_MAIN_VARIANT (*parm);
9745 if (TREE_CODE (*parm) == REFERENCE_TYPE)
62e4a758
NS
9746 {
9747 *parm = TREE_TYPE (*parm);
9748 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
9749 }
1c82cc90
NS
9750
9751 /* DR 322. For conversion deduction, remove a reference type on parm
9752 too (which has been swapped into ARG). */
9753 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
9754 *arg = TREE_TYPE (*arg);
c8094d83 9755
62e4a758 9756 return result;
386b8a85
JM
9757}
9758
e5214479 9759/* Most parms like fn_type_unification.
9f54c803
MM
9760
9761 If SUBR is 1, we're being called recursively (to unify the
9762 arguments of a function or method parameter of a function
38d18b1a 9763 template). */
386b8a85 9764
4966381a 9765static int
c8094d83 9766type_unification_real (tree tparms,
0cbd7506
MS
9767 tree targs,
9768 tree xparms,
9769 tree xargs,
9770 int subr,
30f86ec3
FJ
9771 unification_kind_t strict,
9772 int flags)
8d08fdba
MS
9773{
9774 tree parm, arg;
9775 int i;
9776 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 9777 int sub_strict;
bd0d5d4a
JM
9778 int saw_undeduced = 0;
9779 tree parms, args;
8d08fdba 9780
50bc768d
NS
9781 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
9782 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
9783 gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
9784 gcc_assert (ntparms > 0);
8d08fdba 9785
830bfa74
MM
9786 switch (strict)
9787 {
9788 case DEDUCE_CALL:
028d1f20 9789 sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | UNIFY_ALLOW_MORE_CV_QUAL
0cbd7506 9790 | UNIFY_ALLOW_DERIVED);
830bfa74 9791 break;
c8094d83 9792
830bfa74
MM
9793 case DEDUCE_CONV:
9794 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
9795 break;
9796
9797 case DEDUCE_EXACT:
9798 sub_strict = UNIFY_ALLOW_NONE;
9799 break;
c8094d83 9800
830bfa74 9801 default:
315fb5db 9802 gcc_unreachable ();
830bfa74
MM
9803 }
9804
bd0d5d4a
JM
9805 again:
9806 parms = xparms;
9807 args = xargs;
bd0d5d4a 9808
a34d3336
NS
9809 while (parms && parms != void_list_node
9810 && args && args != void_list_node)
8d08fdba
MS
9811 {
9812 parm = TREE_VALUE (parms);
9813 parms = TREE_CHAIN (parms);
9814 arg = TREE_VALUE (args);
9815 args = TREE_CHAIN (args);
9816
9817 if (arg == error_mark_node)
9818 return 1;
9819 if (arg == unknown_type_node)
34016c81
JM
9820 /* We can't deduce anything from this, but we might get all the
9821 template args from other function args. */
9822 continue;
b7484fbe 9823
03e70705
JM
9824 /* Conversions will be performed on a function argument that
9825 corresponds with a function parameter that contains only
9826 non-deducible template parameters and explicitly specified
9827 template parameters. */
c353b8e3 9828 if (!uses_template_parms (parm))
b7484fbe 9829 {
03e70705
JM
9830 tree type;
9831
2f939d94 9832 if (!TYPE_P (arg))
03e70705
JM
9833 type = TREE_TYPE (arg);
9834 else
c353b8e3 9835 type = arg;
03e70705 9836
fad86f7a 9837 if (same_type_p (parm, type))
343c89cd 9838 continue;
fad86f7a 9839 if (strict != DEDUCE_EXACT
3db45ab5 9840 && can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg,
30f86ec3 9841 flags))
fad86f7a 9842 continue;
3db45ab5 9843
b7484fbe
MS
9844 return 1;
9845 }
c8094d83 9846
2f939d94 9847 if (!TYPE_P (arg))
8d08fdba 9848 {
50bc768d 9849 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
34016c81 9850 if (type_unknown_p (arg))
28cbf42c 9851 {
34016c81
JM
9852 /* [temp.deduct.type] A template-argument can be deduced from
9853 a pointer to function or pointer to member function
9854 argument if the set of overloaded functions does not
9855 contain function templates and at most one of a set of
9856 overloaded functions provides a unique match. */
9857
9858 if (resolve_overloaded_unification
4393e105 9859 (tparms, targs, parm, arg, strict, sub_strict)
34016c81
JM
9860 != 0)
9861 return 1;
9862 continue;
28cbf42c 9863 }
8d08fdba 9864 arg = TREE_TYPE (arg);
08476342
NS
9865 if (arg == error_mark_node)
9866 return 1;
8d08fdba 9867 }
c8094d83 9868
62e4a758 9869 {
0cbd7506 9870 int arg_strict = sub_strict;
c8094d83 9871
0cbd7506 9872 if (!subr)
62e4a758 9873 arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
4393e105 9874
0cbd7506
MS
9875 if (unify (tparms, targs, parm, arg, arg_strict))
9876 return 1;
62e4a758 9877 }
8d08fdba 9878 }
c8094d83 9879
8d08fdba
MS
9880 /* Fail if we've reached the end of the parm list, and more args
9881 are present, and the parm list isn't variadic. */
9882 if (args && args != void_list_node && parms == void_list_node)
9883 return 1;
f4f206f4 9884 /* Fail if parms are left and they don't have default values. */
a34d3336 9885 if (parms && parms != void_list_node
8d08fdba
MS
9886 && TREE_PURPOSE (parms) == NULL_TREE)
9887 return 1;
bd0d5d4a 9888
8d08fdba
MS
9889 if (!subr)
9890 for (i = 0; i < ntparms; i++)
a34d3336 9891 if (!TREE_VEC_ELT (targs, i))
8d08fdba 9892 {
2d8ba2c7
LM
9893 tree tparm;
9894
9895 if (TREE_VEC_ELT (tparms, i) == error_mark_node)
9896 continue;
9897
9898 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
bd0d5d4a
JM
9899
9900 /* If this is an undeduced nontype parameter that depends on
9901 a type parameter, try another pass; its type may have been
9902 deduced from a later argument than the one from which
9903 this parameter can be deduced. */
9904 if (TREE_CODE (tparm) == PARM_DECL
9905 && uses_template_parms (TREE_TYPE (tparm))
9906 && !saw_undeduced++)
9907 goto again;
9908
8d08fdba
MS
9909 return 2;
9910 }
c8094d83 9911
8d08fdba
MS
9912 return 0;
9913}
9914
34016c81
JM
9915/* Subroutine of type_unification_real. Args are like the variables at the
9916 call site. ARG is an overloaded function (or template-id); we try
9917 deducing template args from each of the overloads, and if only one
9918 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
9919
9920static int
c8094d83 9921resolve_overloaded_unification (tree tparms,
0cbd7506
MS
9922 tree targs,
9923 tree parm,
9924 tree arg,
9925 unification_kind_t strict,
3a978d72 9926 int sub_strict)
34016c81
JM
9927{
9928 tree tempargs = copy_node (targs);
9929 int good = 0;
f23fb7f5 9930 bool addr_p;
34016c81
JM
9931
9932 if (TREE_CODE (arg) == ADDR_EXPR)
f23fb7f5
MM
9933 {
9934 arg = TREE_OPERAND (arg, 0);
9935 addr_p = true;
9936 }
9937 else
9938 addr_p = false;
9f3d9e46 9939
d8f8dca1
MM
9940 if (TREE_CODE (arg) == COMPONENT_REF)
9941 /* Handle `&x' where `x' is some static or non-static member
9942 function name. */
9943 arg = TREE_OPERAND (arg, 1);
9944
05e0b2f4
JM
9945 if (TREE_CODE (arg) == OFFSET_REF)
9946 arg = TREE_OPERAND (arg, 1);
9947
9f3d9e46 9948 /* Strip baselink information. */
50ad9642
MM
9949 if (BASELINK_P (arg))
9950 arg = BASELINK_FUNCTIONS (arg);
9f3d9e46 9951
34016c81
JM
9952 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
9953 {
9954 /* If we got some explicit template args, we need to plug them into
9955 the affected templates before we try to unify, in case the
9956 explicit args will completely resolve the templates in question. */
9957
9958 tree expl_subargs = TREE_OPERAND (arg, 1);
9959 arg = TREE_OPERAND (arg, 0);
9960
9961 for (; arg; arg = OVL_NEXT (arg))
9962 {
9963 tree fn = OVL_CURRENT (arg);
9964 tree subargs, elem;
9965
9966 if (TREE_CODE (fn) != TEMPLATE_DECL)
9967 continue;
9968
a34d3336
NS
9969 subargs = get_bindings (fn, DECL_TEMPLATE_RESULT (fn),
9970 expl_subargs, /*check_ret=*/false);
34016c81
JM
9971 if (subargs)
9972 {
c2ea3a40 9973 elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
c8094d83 9974 good += try_one_overload (tparms, targs, tempargs, parm,
f23fb7f5 9975 elem, strict, sub_strict, addr_p);
34016c81
JM
9976 }
9977 }
9978 }
315fb5db 9979 else
34016c81 9980 {
315fb5db
NS
9981 gcc_assert (TREE_CODE (arg) == OVERLOAD
9982 || TREE_CODE (arg) == FUNCTION_DECL);
c8094d83 9983
34016c81 9984 for (; arg; arg = OVL_NEXT (arg))
f23fb7f5
MM
9985 good += try_one_overload (tparms, targs, tempargs, parm,
9986 TREE_TYPE (OVL_CURRENT (arg)),
9987 strict, sub_strict, addr_p);
34016c81 9988 }
34016c81
JM
9989
9990 /* [temp.deduct.type] A template-argument can be deduced from a pointer
9991 to function or pointer to member function argument if the set of
9992 overloaded functions does not contain function templates and at most
9993 one of a set of overloaded functions provides a unique match.
9994
9995 So if we found multiple possibilities, we return success but don't
9996 deduce anything. */
9997
9998 if (good == 1)
9999 {
10000 int i = TREE_VEC_LENGTH (targs);
10001 for (; i--; )
10002 if (TREE_VEC_ELT (tempargs, i))
10003 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
10004 }
10005 if (good)
10006 return 0;
10007
10008 return 1;
10009}
10010
10011/* Subroutine of resolve_overloaded_unification; does deduction for a single
10012 overload. Fills TARGS with any deduced arguments, or error_mark_node if
10013 different overloads deduce different arguments for a given parm.
f23fb7f5
MM
10014 ADDR_P is true if the expression for which deduction is being
10015 performed was of the form "& fn" rather than simply "fn".
10016
34016c81
JM
10017 Returns 1 on success. */
10018
10019static int
3a978d72 10020try_one_overload (tree tparms,
0cbd7506
MS
10021 tree orig_targs,
10022 tree targs,
10023 tree parm,
10024 tree arg,
10025 unification_kind_t strict,
f23fb7f5
MM
10026 int sub_strict,
10027 bool addr_p)
34016c81
JM
10028{
10029 int nargs;
10030 tree tempargs;
10031 int i;
10032
10033 /* [temp.deduct.type] A template-argument can be deduced from a pointer
10034 to function or pointer to member function argument if the set of
10035 overloaded functions does not contain function templates and at most
10036 one of a set of overloaded functions provides a unique match.
10037
10038 So if this is a template, just return success. */
10039
10040 if (uses_template_parms (arg))
10041 return 1;
10042
f23fb7f5
MM
10043 if (TREE_CODE (arg) == METHOD_TYPE)
10044 arg = build_ptrmemfunc_type (build_pointer_type (arg));
10045 else if (addr_p)
10046 arg = build_pointer_type (arg);
10047
62e4a758 10048 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
34016c81
JM
10049
10050 /* We don't copy orig_targs for this because if we have already deduced
10051 some template args from previous args, unify would complain when we
10052 try to deduce a template parameter for the same argument, even though
10053 there isn't really a conflict. */
10054 nargs = TREE_VEC_LENGTH (targs);
f31c0a32 10055 tempargs = make_tree_vec (nargs);
34016c81 10056
4393e105 10057 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
34016c81
JM
10058 return 0;
10059
10060 /* First make sure we didn't deduce anything that conflicts with
e97e5263 10061 explicitly specified args. */
34016c81
JM
10062 for (i = nargs; i--; )
10063 {
10064 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 10065 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81 10066
a34d3336
NS
10067 if (!elt)
10068 /*NOP*/;
34016c81 10069 else if (uses_template_parms (elt))
a34d3336
NS
10070 /* Since we're unifying against ourselves, we will fill in
10071 template args used in the function parm list with our own
10072 template parms. Discard them. */
10073 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
10074 else if (oldelt && !template_args_equal (oldelt, elt))
34016c81
JM
10075 return 0;
10076 }
10077
10078 for (i = nargs; i--; )
10079 {
10080 tree elt = TREE_VEC_ELT (tempargs, i);
10081
10082 if (elt)
10083 TREE_VEC_ELT (targs, i) = elt;
10084 }
10085
10086 return 1;
10087}
10088
4393e105
MM
10089/* PARM is a template class (perhaps with unbound template
10090 parameters). ARG is a fully instantiated type. If ARG can be
10091 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
10092 TARGS are as for unify. */
fcfb9f96
MM
10093
10094static tree
3a978d72 10095try_class_unification (tree tparms, tree targs, tree parm, tree arg)
4393e105 10096{
4393e105
MM
10097 tree copy_of_targs;
10098
10099 if (!CLASSTYPE_TEMPLATE_INFO (arg)
c8094d83 10100 || (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
68361a03 10101 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
4393e105
MM
10102 return NULL_TREE;
10103
10104 /* We need to make a new template argument vector for the call to
10105 unify. If we used TARGS, we'd clutter it up with the result of
10106 the attempted unification, even if this class didn't work out.
10107 We also don't want to commit ourselves to all the unifications
10108 we've already done, since unification is supposed to be done on
10109 an argument-by-argument basis. In other words, consider the
10110 following pathological case:
10111
10112 template <int I, int J, int K>
10113 struct S {};
c8094d83 10114
4393e105
MM
10115 template <int I, int J>
10116 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
c8094d83 10117
4393e105
MM
10118 template <int I, int J, int K>
10119 void f(S<I, J, K>, S<I, I, I>);
c8094d83 10120
4393e105 10121 void g() {
0cbd7506
MS
10122 S<0, 0, 0> s0;
10123 S<0, 1, 2> s2;
c8094d83 10124
0cbd7506 10125 f(s0, s2);
4393e105
MM
10126 }
10127
10128 Now, by the time we consider the unification involving `s2', we
10129 already know that we must have `f<0, 0, 0>'. But, even though
0e339752 10130 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
4393e105
MM
10131 because there are two ways to unify base classes of S<0, 1, 2>
10132 with S<I, I, I>. If we kept the already deduced knowledge, we
10133 would reject the possibility I=1. */
f31c0a32 10134 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
c8094d83 10135
4393e105 10136 /* If unification failed, we're done. */
74601d7c
KL
10137 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
10138 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
4393e105 10139 return NULL_TREE;
74601d7c
KL
10140
10141 return arg;
4393e105
MM
10142}
10143
a3a0fc7f
NS
10144/* Given a template type PARM and a class type ARG, find the unique
10145 base type in ARG that is an instance of PARM. We do not examine
10146 ARG itself; only its base-classes. If there is not exactly one
10147 appropriate base class, return NULL_TREE. PARM may be the type of
10148 a partial specialization, as well as a plain template type. Used
10149 by unify. */
4393e105
MM
10150
10151static tree
a3a0fc7f 10152get_template_base (tree tparms, tree targs, tree parm, tree arg)
fcfb9f96 10153{
a3a0fc7f
NS
10154 tree rval = NULL_TREE;
10155 tree binfo;
10156
10157 gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
c8094d83 10158
a3a0fc7f
NS
10159 binfo = TYPE_BINFO (complete_type (arg));
10160 if (!binfo)
10161 /* The type could not be completed. */
10162 return NULL_TREE;
fcfb9f96 10163
a3a0fc7f
NS
10164 /* Walk in inheritance graph order. The search order is not
10165 important, and this avoids multiple walks of virtual bases. */
10166 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
fcfb9f96 10167 {
a3a0fc7f 10168 tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
fcfb9f96 10169
8d83768f
NS
10170 if (r)
10171 {
10172 /* If there is more than one satisfactory baseclass, then:
fcfb9f96 10173
8d83768f 10174 [temp.deduct.call]
fcfb9f96 10175
8d83768f
NS
10176 If they yield more than one possible deduced A, the type
10177 deduction fails.
4393e105 10178
8d83768f 10179 applies. */
a3a0fc7f
NS
10180 if (rval && !same_type_p (r, rval))
10181 return NULL_TREE;
c8094d83 10182
a3a0fc7f 10183 rval = r;
8d83768f 10184 }
fcfb9f96
MM
10185 }
10186
a3a0fc7f 10187 return rval;
fcfb9f96
MM
10188}
10189
db2767b6
MM
10190/* Returns the level of DECL, which declares a template parameter. */
10191
e9659ab0 10192static int
3a978d72 10193template_decl_level (tree decl)
db2767b6
MM
10194{
10195 switch (TREE_CODE (decl))
10196 {
10197 case TYPE_DECL:
10198 case TEMPLATE_DECL:
10199 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
10200
10201 case PARM_DECL:
10202 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
10203
10204 default:
315fb5db 10205 gcc_unreachable ();
db2767b6 10206 }
315fb5db 10207 return 0;
db2767b6
MM
10208}
10209
830bfa74
MM
10210/* Decide whether ARG can be unified with PARM, considering only the
10211 cv-qualifiers of each type, given STRICT as documented for unify.
324f9dfb 10212 Returns nonzero iff the unification is OK on that basis. */
e92cc029 10213
e9659ab0 10214static int
3a978d72 10215check_cv_quals_for_unify (int strict, tree arg, tree parm)
830bfa74 10216{
4f2b0fb2
NS
10217 int arg_quals = cp_type_quals (arg);
10218 int parm_quals = cp_type_quals (parm);
10219
355f774d
NS
10220 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10221 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 10222 {
2e9ceb77 10223 /* Although a CVR qualifier is ignored when being applied to a
0cbd7506
MS
10224 substituted template parameter ([8.3.2]/1 for example), that
10225 does not apply during deduction [14.8.2.4]/1, (even though
10226 that is not explicitly mentioned, [14.8.2.4]/9 indicates
10227 this). Except when we're allowing additional CV qualifiers
10228 at the outer level [14.8.2.1]/3,1st bullet. */
2e9ceb77
NS
10229 if ((TREE_CODE (arg) == REFERENCE_TYPE
10230 || TREE_CODE (arg) == FUNCTION_TYPE
10231 || TREE_CODE (arg) == METHOD_TYPE)
10232 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
10233 return 0;
10234
10235 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
10236 && (parm_quals & TYPE_QUAL_RESTRICT))
10237 return 0;
4f2b0fb2 10238 }
2e9ceb77 10239
62e4a758 10240 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
4f2b0fb2 10241 && (arg_quals & parm_quals) != parm_quals)
ef637255
MM
10242 return 0;
10243
62e4a758 10244 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
4f2b0fb2 10245 && (parm_quals & arg_quals) != arg_quals)
ef637255
MM
10246 return 0;
10247
ef637255 10248 return 1;
830bfa74
MM
10249}
10250
916b63c3
MM
10251/* Deduce the value of template parameters. TPARMS is the (innermost)
10252 set of template parameters to a template. TARGS is the bindings
10253 for those template parameters, as determined thus far; TARGS may
10254 include template arguments for outer levels of template parameters
10255 as well. PARM is a parameter to a template function, or a
10256 subcomponent of that parameter; ARG is the corresponding argument.
10257 This function attempts to match PARM with ARG in a manner
10258 consistent with the existing assignments in TARGS. If more values
10259 are deduced, then TARGS is updated.
10260
10261 Returns 0 if the type deduction succeeds, 1 otherwise. The
10262 parameter STRICT is a bitwise or of the following flags:
830bfa74
MM
10263
10264 UNIFY_ALLOW_NONE:
10265 Require an exact match between PARM and ARG.
10266 UNIFY_ALLOW_MORE_CV_QUAL:
028d1f20
NS
10267 Allow the deduced ARG to be more cv-qualified (by qualification
10268 conversion) than ARG.
830bfa74
MM
10269 UNIFY_ALLOW_LESS_CV_QUAL:
10270 Allow the deduced ARG to be less cv-qualified than ARG.
10271 UNIFY_ALLOW_DERIVED:
10272 Allow the deduced ARG to be a template base class of ARG,
10273 or a pointer to a template base class of the type pointed to by
161c12b0
JM
10274 ARG.
10275 UNIFY_ALLOW_INTEGER:
10276 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
c8094d83 10277 case for more information.
028d1f20
NS
10278 UNIFY_ALLOW_OUTER_LEVEL:
10279 This is the outermost level of a deduction. Used to determine validity
10280 of qualification conversions. A valid qualification conversion must
10281 have const qualified pointers leading up to the inner type which
10282 requires additional CV quals, except at the outer level, where const
10283 is not required [conv.qual]. It would be normal to set this flag in
62e4a758
NS
10284 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
10285 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
10286 This is the outermost level of a deduction, and PARM can be more CV
10287 qualified at this point.
10288 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
10289 This is the outermost level of a deduction, and PARM can be less CV
8baddbf1 10290 qualified at this point. */
830bfa74 10291
e9659ab0 10292static int
3a978d72 10293unify (tree tparms, tree targs, tree parm, tree arg, int strict)
8d08fdba
MS
10294{
10295 int idx;
050367a3 10296 tree targ;
db2767b6 10297 tree tparm;
028d1f20 10298 int strict_in = strict;
8d08fdba
MS
10299
10300 /* I don't think this will do the right thing with respect to types.
10301 But the only case I've seen it in so far has been array bounds, where
10302 signedness is the only information lost, and I think that will be
10303 okay. */
10304 while (TREE_CODE (parm) == NOP_EXPR)
10305 parm = TREE_OPERAND (parm, 0);
10306
10307 if (arg == error_mark_node)
10308 return 1;
10309 if (arg == unknown_type_node)
34016c81
JM
10310 /* We can't deduce anything from this, but we might get all the
10311 template args from other function args. */
10312 return 0;
10313
db2767b6 10314 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 10315 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
10316 template parameters. We might need them if we're trying to
10317 figure out which of two things is more specialized. */
10318 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
10319 return 0;
10320
830bfa74
MM
10321 /* Immediately reject some pairs that won't unify because of
10322 cv-qualification mismatches. */
10323 if (TREE_CODE (arg) == TREE_CODE (parm)
2f939d94 10324 && TYPE_P (arg)
d0ab7624 10325 /* It is the elements of the array which hold the cv quals of an array
0cbd7506
MS
10326 type, and the elements might be template type parms. We'll check
10327 when we recurse. */
d0ab7624 10328 && TREE_CODE (arg) != ARRAY_TYPE
830bfa74
MM
10329 /* We check the cv-qualifiers when unifying with template type
10330 parameters below. We want to allow ARG `const T' to unify with
10331 PARM `T' for example, when computing which of two templates
10332 is more specialized, for example. */
10333 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
028d1f20 10334 && !check_cv_quals_for_unify (strict_in, arg, parm))
49432171
JM
10335 return 1;
10336
028d1f20 10337 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
3ea099f1 10338 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
028d1f20
NS
10339 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
10340 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
10341 strict &= ~UNIFY_ALLOW_DERIVED;
62e4a758
NS
10342 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
10343 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
c8094d83 10344
8d08fdba
MS
10345 switch (TREE_CODE (parm))
10346 {
2ca340ae 10347 case TYPENAME_TYPE:
fccef71e 10348 case SCOPE_REF:
b8c6534b 10349 case UNBOUND_CLASS_TEMPLATE:
2ca340ae
JM
10350 /* In a type which contains a nested-name-specifier, template
10351 argument values cannot be deduced for template parameters used
10352 within the nested-name-specifier. */
10353 return 0;
10354
8d08fdba 10355 case TEMPLATE_TYPE_PARM:
73b0fce8 10356 case TEMPLATE_TEMPLATE_PARM:
a1281f45 10357 case BOUND_TEMPLATE_TEMPLATE_PARM:
db2767b6
MM
10358 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
10359
10360 if (TEMPLATE_TYPE_LEVEL (parm)
10361 != template_decl_level (tparm))
10362 /* The PARM is not one we're trying to unify. Just check
10363 to see if it matches ARG. */
10364 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 10365 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 10366 idx = TEMPLATE_TYPE_IDX (parm);
916b63c3 10367 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
db2767b6 10368 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 10369
73b0fce8 10370 /* Check for mixed types and values. */
db2767b6
MM
10371 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
10372 && TREE_CODE (tparm) != TYPE_DECL)
c8094d83 10373 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
db2767b6 10374 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
10375 return 1;
10376
a1281f45 10377 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
73b0fce8 10378 {
b429fdf0
KL
10379 /* ARG must be constructed from a template class or a template
10380 template parameter. */
10381 if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
4e95268d 10382 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
a1281f45 10383 return 1;
73b0fce8 10384
a1281f45 10385 {
a1281f45 10386 tree parmvec = TYPE_TI_ARGS (parm);
6df91b00 10387 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
a1281f45 10388 tree argtmplvec
b429fdf0 10389 = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (arg));
a1281f45 10390 int i;
73b0fce8 10391
e7e93965
MM
10392 /* The resolution to DR150 makes clear that default
10393 arguments for an N-argument may not be used to bind T
10394 to a template template parameter with fewer than N
10395 parameters. It is not safe to permit the binding of
10396 default arguments as an extension, as that may change
10397 the meaning of a conforming program. Consider:
10398
10399 struct Dense { static const unsigned int dim = 1; };
10400
10401 template <template <typename> class View,
10402 typename Block>
10403 void operator+(float, View<Block> const&);
10404
3db45ab5
MS
10405 template <typename Block,
10406 unsigned int Dim = Block::dim>
e7e93965
MM
10407 struct Lvalue_proxy { operator float() const; };
10408
10409 void
10410 test_1d (void) {
10411 Lvalue_proxy<Dense> p;
10412 float b;
10413 b + p;
10414 }
a1281f45 10415
e7e93965
MM
10416 Here, if Lvalue_proxy is permitted to bind to View, then
10417 the global operator+ will be used; if they are not, the
3db45ab5
MS
10418 Lvalue_proxy will be converted to float. */
10419 if (coerce_template_parms (argtmplvec, parmvec,
e7e93965
MM
10420 TYPE_TI_TEMPLATE (parm),
10421 tf_none,
10422 /*require_all_args=*/true,
10423 /*use_default_args=*/false)
0cbd7506 10424 == error_mark_node)
a1281f45 10425 return 1;
c8094d83
MS
10426
10427 /* Deduce arguments T, i from TT<T> or TT<i>.
a1281f45
KL
10428 We check each element of PARMVEC and ARGVEC individually
10429 rather than the whole TREE_VEC since they can have
10430 different number of elements. */
6b9b6b15 10431
a1281f45
KL
10432 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
10433 {
0cbd7506 10434 if (unify (tparms, targs,
c8094d83
MS
10435 TREE_VEC_ELT (parmvec, i),
10436 TREE_VEC_ELT (argvec, i),
a1281f45
KL
10437 UNIFY_ALLOW_NONE))
10438 return 1;
73b0fce8 10439 }
a1281f45 10440 }
b429fdf0 10441 arg = TYPE_TI_TEMPLATE (arg);
a1281f45
KL
10442
10443 /* Fall through to deduce template name. */
10444 }
10445
10446 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
10447 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
10448 {
10449 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
10450
10451 /* Simple cases: Value already set, does match or doesn't. */
10452 if (targ != NULL_TREE && template_args_equal (targ, arg))
10453 return 0;
10454 else if (targ)
10455 return 1;
db2767b6
MM
10456 }
10457 else
10458 {
830bfa74
MM
10459 /* If PARM is `const T' and ARG is only `int', we don't have
10460 a match unless we are allowing additional qualification.
10461 If ARG is `const int' and PARM is just `T' that's OK;
10462 that binds `const int' to `T'. */
c8094d83 10463 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
830bfa74 10464 arg, parm))
db2767b6
MM
10465 return 1;
10466
830bfa74
MM
10467 /* Consider the case where ARG is `const volatile int' and
10468 PARM is `const T'. Then, T should be `volatile int'. */
c2ea3a40
NS
10469 arg = cp_build_qualified_type_real
10470 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
adecb3f4
MM
10471 if (arg == error_mark_node)
10472 return 1;
73b0fce8 10473
a1281f45
KL
10474 /* Simple cases: Value already set, does match or doesn't. */
10475 if (targ != NULL_TREE && same_type_p (targ, arg))
10476 return 0;
10477 else if (targ)
10478 return 1;
61cd552e 10479
94fc547c
MM
10480 /* Make sure that ARG is not a variable-sized array. (Note
10481 that were talking about variable-sized arrays (like
10482 `int[n]'), rather than arrays of unknown size (like
10483 `int[]').) We'll get very confused by such a type since
10484 the bound of the array will not be computable in an
10485 instantiation. Besides, such types are not allowed in
10486 ISO C++, so we can do as we please here. */
5377d5ba 10487 if (variably_modified_type_p (arg, NULL_TREE))
94fc547c
MM
10488 return 1;
10489 }
61cd552e 10490
916b63c3 10491 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
73b0fce8
KL
10492 return 0;
10493
f84b4be9 10494 case TEMPLATE_PARM_INDEX:
db2767b6 10495 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
3e9ac7e5
VR
10496 if (tparm == error_mark_node)
10497 return 1;
db2767b6 10498
c8094d83 10499 if (TEMPLATE_PARM_LEVEL (parm)
db2767b6
MM
10500 != template_decl_level (tparm))
10501 /* The PARM is not one we're trying to unify. Just check
10502 to see if it matches ARG. */
c8a209ca
NS
10503 return !(TREE_CODE (arg) == TREE_CODE (parm)
10504 && cp_tree_equal (parm, arg));
db2767b6 10505
f84b4be9 10506 idx = TEMPLATE_PARM_IDX (parm);
916b63c3 10507 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
db2767b6 10508
050367a3 10509 if (targ)
c8a209ca 10510 return !cp_tree_equal (targ, arg);
8d08fdba 10511
161c12b0
JM
10512 /* [temp.deduct.type] If, in the declaration of a function template
10513 with a non-type template-parameter, the non-type
10514 template-parameter is used in an expression in the function
10515 parameter-list and, if the corresponding template-argument is
10516 deduced, the template-argument type shall match the type of the
10517 template-parameter exactly, except that a template-argument
c8094d83 10518 deduced from an array bound may be of any integral type.
d7c4edd0 10519 The non-type parameter might use already deduced type parameters. */
bd0d5d4a 10520 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
e2005c8d
KL
10521 if (!TREE_TYPE (arg))
10522 /* Template-parameter dependent expression. Just accept it for now.
10523 It will later be processed in convert_template_argument. */
10524 ;
10525 else if (same_type_p (TREE_TYPE (arg), tparm))
10526 /* OK */;
161c12b0 10527 else if ((strict & UNIFY_ALLOW_INTEGER)
bd0d5d4a
JM
10528 && (TREE_CODE (tparm) == INTEGER_TYPE
10529 || TREE_CODE (tparm) == BOOLEAN_TYPE))
8baddbf1
MM
10530 /* Convert the ARG to the type of PARM; the deduced non-type
10531 template argument must exactly match the types of the
10532 corresponding parameter. */
10533 arg = fold (build_nop (TREE_TYPE (parm), arg));
bd0d5d4a
JM
10534 else if (uses_template_parms (tparm))
10535 /* We haven't deduced the type of this parameter yet. Try again
10536 later. */
10537 return 0;
161c12b0
JM
10538 else
10539 return 1;
10540
916b63c3 10541 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
8d08fdba
MS
10542 return 0;
10543
28e8f3a0
GB
10544 case PTRMEM_CST:
10545 {
0cbd7506
MS
10546 /* A pointer-to-member constant can be unified only with
10547 another constant. */
28e8f3a0 10548 if (TREE_CODE (arg) != PTRMEM_CST)
0cbd7506 10549 return 1;
28e8f3a0
GB
10550
10551 /* Just unify the class member. It would be useless (and possibly
0cbd7506
MS
10552 wrong, depending on the strict flags) to unify also
10553 PTRMEM_CST_CLASS, because we want to be sure that both parm and
10554 arg refer to the same variable, even if through different
10555 classes. For instance:
28e8f3a0 10556
0cbd7506
MS
10557 struct A { int x; };
10558 struct B : A { };
28e8f3a0 10559
0cbd7506 10560 Unification of &A::x and &B::x must succeed. */
28e8f3a0 10561 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
0cbd7506 10562 PTRMEM_CST_MEMBER (arg), strict);
28e8f3a0
GB
10563 }
10564
8d08fdba 10565 case POINTER_TYPE:
830bfa74 10566 {
830bfa74
MM
10567 if (TREE_CODE (arg) != POINTER_TYPE)
10568 return 1;
c8094d83 10569
830bfa74
MM
10570 /* [temp.deduct.call]
10571
10572 A can be another pointer or pointer to member type that can
10573 be converted to the deduced A via a qualification
10574 conversion (_conv.qual_).
10575
10576 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
10577 This will allow for additional cv-qualification of the
028d1f20 10578 pointed-to types if appropriate. */
c8094d83 10579
028d1f20 10580 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
830bfa74
MM
10581 /* The derived-to-base conversion only persists through one
10582 level of pointers. */
028d1f20 10583 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
830bfa74 10584
c8094d83 10585 return unify (tparms, targs, TREE_TYPE (parm),
028d1f20 10586 TREE_TYPE (arg), strict);
830bfa74 10587 }
8d08fdba
MS
10588
10589 case REFERENCE_TYPE:
830bfa74
MM
10590 if (TREE_CODE (arg) != REFERENCE_TYPE)
10591 return 1;
10592 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
028d1f20 10593 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
10594
10595 case ARRAY_TYPE:
10596 if (TREE_CODE (arg) != ARRAY_TYPE)
10597 return 1;
3042d5be
MM
10598 if ((TYPE_DOMAIN (parm) == NULL_TREE)
10599 != (TYPE_DOMAIN (arg) == NULL_TREE))
10600 return 1;
8baddbf1
MM
10601 if (TYPE_DOMAIN (parm) != NULL_TREE)
10602 {
10603 tree parm_max;
10604 tree arg_max;
10605
10606 parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
10607 arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
10608
10609 /* Our representation of array types uses "N - 1" as the
10610 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
10611 not an integer constant. */
10612 if (TREE_CODE (parm_max) == MINUS_EXPR)
10613 {
c8094d83 10614 arg_max = fold_build2 (PLUS_EXPR,
7866705a
SB
10615 integer_type_node,
10616 arg_max,
10617 TREE_OPERAND (parm_max, 1));
8baddbf1
MM
10618 parm_max = TREE_OPERAND (parm_max, 0);
10619 }
10620
10621 if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
10622 return 1;
10623 }
830bfa74 10624 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
712467a4 10625 strict & UNIFY_ALLOW_MORE_CV_QUAL);
8d08fdba
MS
10626
10627 case REAL_TYPE:
37c46b43 10628 case COMPLEX_TYPE:
c00996a3 10629 case VECTOR_TYPE:
8d08fdba 10630 case INTEGER_TYPE:
42976354 10631 case BOOLEAN_TYPE:
3590f0a6 10632 case ENUMERAL_TYPE:
5ad5a526 10633 case VOID_TYPE:
f376e137
MS
10634 if (TREE_CODE (arg) != TREE_CODE (parm))
10635 return 1;
c8094d83 10636
9edc3913 10637 /* We have already checked cv-qualification at the top of the
514a1f18 10638 function. */
8baddbf1 10639 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
ca79f85d
JM
10640 return 1;
10641
8d08fdba
MS
10642 /* As far as unification is concerned, this wins. Later checks
10643 will invalidate it if necessary. */
10644 return 0;
10645
10646 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 10647 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 10648 case INTEGER_CST:
bd6dd845
MS
10649 while (TREE_CODE (arg) == NOP_EXPR)
10650 arg = TREE_OPERAND (arg, 0);
10651
8d08fdba
MS
10652 if (TREE_CODE (arg) != INTEGER_CST)
10653 return 1;
10654 return !tree_int_cst_equal (parm, arg);
10655
8d08fdba
MS
10656 case TREE_VEC:
10657 {
10658 int i;
10659 if (TREE_CODE (arg) != TREE_VEC)
10660 return 1;
10661 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
10662 return 1;
0dc09a61 10663 for (i = 0; i < TREE_VEC_LENGTH (parm); ++i)
830bfa74 10664 if (unify (tparms, targs,
8d08fdba 10665 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
4393e105 10666 UNIFY_ALLOW_NONE))
8d08fdba
MS
10667 return 1;
10668 return 0;
10669 }
10670
8d08fdba 10671 case RECORD_TYPE:
f181d4ae 10672 case UNION_TYPE:
f181d4ae 10673 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 10674 return 1;
c8094d83 10675
a7a64a77
MM
10676 if (TYPE_PTRMEMFUNC_P (parm))
10677 {
10678 if (!TYPE_PTRMEMFUNC_P (arg))
10679 return 1;
10680
c8094d83 10681 return unify (tparms, targs,
a7a64a77
MM
10682 TYPE_PTRMEMFUNC_FN_TYPE (parm),
10683 TYPE_PTRMEMFUNC_FN_TYPE (arg),
10684 strict);
10685 }
10686
5db698f6 10687 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 10688 {
6467930b 10689 tree t = NULL_TREE;
4393e105 10690
028d1f20 10691 if (strict_in & UNIFY_ALLOW_DERIVED)
4393e105
MM
10692 {
10693 /* First, we try to unify the PARM and ARG directly. */
10694 t = try_class_unification (tparms, targs,
10695 parm, arg);
10696
10697 if (!t)
10698 {
10699 /* Fallback to the special case allowed in
10700 [temp.deduct.call]:
c8094d83 10701
4393e105
MM
10702 If P is a class, and P has the form
10703 template-id, then A can be a derived class of
10704 the deduced A. Likewise, if P is a pointer to
10705 a class of the form template-id, A can be a
10706 pointer to a derived class pointed to by the
10707 deduced A. */
8d83768f 10708 t = get_template_base (tparms, targs, parm, arg);
4393e105 10709
8d83768f 10710 if (!t)
4393e105
MM
10711 return 1;
10712 }
10713 }
c8094d83
MS
10714 else if (CLASSTYPE_TEMPLATE_INFO (arg)
10715 && (CLASSTYPE_TI_TEMPLATE (parm)
9fbf56f7 10716 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
10717 /* Perhaps PARM is something like S<U> and ARG is S<int>.
10718 Then, we should unify `int' and `U'. */
6467930b 10719 t = arg;
4393e105 10720 else
dc957d14 10721 /* There's no chance of unification succeeding. */
5566b478 10722 return 1;
6467930b 10723
830bfa74 10724 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
4393e105 10725 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
5566b478 10726 }
9edc3913 10727 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
5566b478 10728 return 1;
a4443a08 10729 return 0;
8d08fdba
MS
10730
10731 case METHOD_TYPE:
8d08fdba 10732 case FUNCTION_TYPE:
830bfa74 10733 if (TREE_CODE (arg) != TREE_CODE (parm))
8d08fdba 10734 return 1;
830bfa74 10735
38d18b1a 10736 /* CV qualifications for methods can never be deduced, they must
3db45ab5
MS
10737 match exactly. We need to check them explicitly here,
10738 because type_unification_real treats them as any other
10739 cvqualified parameter. */
38d18b1a
NS
10740 if (TREE_CODE (parm) == METHOD_TYPE
10741 && (!check_cv_quals_for_unify
10742 (UNIFY_ALLOW_NONE,
10743 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (arg))),
10744 TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (parm))))))
10745 return 1;
10746
830bfa74 10747 if (unify (tparms, targs, TREE_TYPE (parm),
4393e105 10748 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
28cbf42c 10749 return 1;
386b8a85 10750 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
30f86ec3
FJ
10751 TYPE_ARG_TYPES (arg), 1, DEDUCE_EXACT,
10752 LOOKUP_NORMAL);
a4443a08
MS
10753
10754 case OFFSET_TYPE:
9804209d 10755 /* Unify a pointer to member with a pointer to member function, which
0cbd7506 10756 deduces the type of the member as a function type. */
9804209d 10757 if (TYPE_PTRMEMFUNC_P (arg))
0cbd7506
MS
10758 {
10759 tree method_type;
10760 tree fntype;
10761 cp_cv_quals cv_quals;
10762
10763 /* Check top-level cv qualifiers */
10764 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
10765 return 1;
10766
10767 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
10768 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
10769 return 1;
10770
10771 /* Determine the type of the function we are unifying against. */
10772 method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
10773 fntype =
10774 build_function_type (TREE_TYPE (method_type),
10775 TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
10776
10777 /* Extract the cv-qualifiers of the member function from the
10778 implicit object parameter and place them on the function
10779 type to be restored later. */
10780 cv_quals =
10781 cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
10782 fntype = build_qualified_type (fntype, cv_quals);
10783 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
10784 }
9804209d 10785
a4443a08
MS
10786 if (TREE_CODE (arg) != OFFSET_TYPE)
10787 return 1;
830bfa74 10788 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
4393e105 10789 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
a4443a08 10790 return 1;
830bfa74 10791 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 10792 strict);
a4443a08 10793
f62dbf03 10794 case CONST_DECL:
a723baf1
MM
10795 if (DECL_TEMPLATE_PARM_P (parm))
10796 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
c8094d83 10797 if (arg != integral_constant_value (parm))
f62dbf03
JM
10798 return 1;
10799 return 0;
10800
28e8f3a0 10801 case FIELD_DECL:
027905b4
KL
10802 case TEMPLATE_DECL:
10803 /* Matched cases are handled by the ARG == PARM test above. */
10804 return 1;
10805
8d08fdba 10806 default:
6615c446 10807 gcc_assert (EXPR_P (parm));
c8094d83 10808
98ddffc1 10809 /* We must be looking at an expression. This can happen with
c8094d83
MS
10810 something like:
10811
98ddffc1
NS
10812 template <int I>
10813 void foo(S<I>, S<I + 2>);
050367a3 10814
98ddffc1 10815 This is a "nondeduced context":
050367a3 10816
98ddffc1 10817 [deduct.type]
c8094d83 10818
98ddffc1 10819 The nondeduced contexts are:
050367a3 10820
98ddffc1
NS
10821 --A type that is a template-id in which one or more of
10822 the template-arguments is an expression that references
c8094d83 10823 a template-parameter.
050367a3 10824
98ddffc1
NS
10825 In these cases, we assume deduction succeeded, but don't
10826 actually infer any unifications. */
74601d7c 10827
98ddffc1
NS
10828 if (!uses_template_parms (parm)
10829 && !template_args_equal (parm, arg))
10830 return 1;
10831 else
10832 return 0;
8d08fdba
MS
10833 }
10834}
8d08fdba 10835\f
4684cd27
MM
10836/* Note that DECL can be defined in this translation unit, if
10837 required. */
10838
10839static void
10840mark_definable (tree decl)
10841{
10842 tree clone;
10843 DECL_NOT_REALLY_EXTERN (decl) = 1;
10844 FOR_EACH_CLONE (clone, decl)
10845 DECL_NOT_REALLY_EXTERN (clone) = 1;
10846}
10847
03d0f4af 10848/* Called if RESULT is explicitly instantiated, or is a member of an
4684cd27 10849 explicitly instantiated class. */
03d0f4af 10850
faae18ab 10851void
3a978d72 10852mark_decl_instantiated (tree result, int extern_p)
faae18ab 10853{
415c974c 10854 SET_DECL_EXPLICIT_INSTANTIATION (result);
3ae18eaf 10855
1f6f0cb6
MM
10856 /* If this entity has already been written out, it's too late to
10857 make any modifications. */
10858 if (TREE_ASM_WRITTEN (result))
10859 return;
10860
10861 if (TREE_CODE (result) != FUNCTION_DECL)
10862 /* The TREE_PUBLIC flag for function declarations will have been
10863 set correctly by tsubst. */
10864 TREE_PUBLIC (result) = 1;
10865
346eeb15
JM
10866 /* This might have been set by an earlier implicit instantiation. */
10867 DECL_COMDAT (result) = 0;
10868
4684cd27
MM
10869 if (extern_p)
10870 DECL_NOT_REALLY_EXTERN (result) = 0;
10871 else
faae18ab 10872 {
4684cd27 10873 mark_definable (result);
1a408d07
JM
10874 /* Always make artificials weak. */
10875 if (DECL_ARTIFICIAL (result) && flag_weak)
10876 comdat_linkage (result);
a7d87521
JM
10877 /* For WIN32 we also want to put explicit instantiations in
10878 linkonce sections. */
1a408d07 10879 else if (TREE_PUBLIC (result))
b385c841 10880 maybe_make_one_only (result);
faae18ab 10881 }
c8094d83 10882
4684cd27
MM
10883 /* If EXTERN_P, then this function will not be emitted -- unless
10884 followed by an explicit instantiation, at which point its linkage
10885 will be adjusted. If !EXTERN_P, then this function will be
10886 emitted here. In neither circumstance do we want
10887 import_export_decl to adjust the linkage. */
c8094d83 10888 DECL_INTERFACE_KNOWN (result) = 1;
faae18ab
MS
10889}
10890
e5214479 10891/* Given two function templates PAT1 and PAT2, return:
6467930b
MS
10892
10893 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
10894 -1 if PAT2 is more specialized than PAT1.
e5214479
JM
10895 0 if neither is more specialized.
10896
dda04398
NS
10897 LEN indicates the number of parameters we should consider
10898 (defaulted parameters should not be considered).
10899
10900 The 1998 std underspecified function template partial ordering, and
10901 DR214 addresses the issue. We take pairs of arguments, one from
c51940a2 10902 each of the templates, and deduce them against each other. One of
dda04398
NS
10903 the templates will be more specialized if all the *other*
10904 template's arguments deduce against its arguments and at least one
10905 of its arguments *does* *not* deduce against the other template's
10906 corresponding argument. Deduction is done as for class templates.
10907 The arguments used in deduction have reference and top level cv
10908 qualifiers removed. Iff both arguments were originally reference
10909 types *and* deduction succeeds in both directions, the template
10910 with the more cv-qualified argument wins for that pairing (if
10911 neither is more cv-qualified, they both are equal). Unlike regular
10912 deduction, after all the arguments have been deduced in this way,
10913 we do *not* verify the deduced template argument values can be
10914 substituted into non-deduced contexts, nor do we have to verify
10915 that all template arguments have been deduced. */
c8094d83 10916
6467930b 10917int
dda04398
NS
10918more_specialized_fn (tree pat1, tree pat2, int len)
10919{
10920 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
10921 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
10922 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
10923 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
10924 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
10925 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
10926 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
10927 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
10928 int better1 = 0;
10929 int better2 = 0;
3db45ab5 10930
48884537
NS
10931 /* Remove the this parameter from non-static member functions. If
10932 one is a non-static member function and the other is not a static
10933 member function, remove the first parameter from that function
10934 also. This situation occurs for operator functions where we
10935 locate both a member function (with this pointer) and non-member
10936 operator (with explicit first operand). */
dda04398 10937 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
48884537
NS
10938 {
10939 len--; /* LEN is the number of significant arguments for DECL1 */
10940 args1 = TREE_CHAIN (args1);
10941 if (!DECL_STATIC_FUNCTION_P (decl2))
10942 args2 = TREE_CHAIN (args2);
10943 }
10944 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
10945 {
10946 args2 = TREE_CHAIN (args2);
10947 if (!DECL_STATIC_FUNCTION_P (decl1))
10948 {
10949 len--;
10950 args1 = TREE_CHAIN (args1);
10951 }
10952 }
3db45ab5 10953
ee307009
NS
10954 /* If only one is a conversion operator, they are unordered. */
10955 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
10956 return 0;
c8094d83 10957
dda04398
NS
10958 /* Consider the return type for a conversion function */
10959 if (DECL_CONV_FN_P (decl1))
10960 {
dda04398
NS
10961 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
10962 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
10963 len++;
10964 }
c8094d83 10965
dda04398 10966 processing_template_decl++;
c8094d83 10967
dda04398
NS
10968 while (len--)
10969 {
10970 tree arg1 = TREE_VALUE (args1);
10971 tree arg2 = TREE_VALUE (args2);
10972 int deduce1, deduce2;
10973 int quals1 = -1;
10974 int quals2 = -1;
6467930b 10975
dda04398
NS
10976 if (TREE_CODE (arg1) == REFERENCE_TYPE)
10977 {
10978 arg1 = TREE_TYPE (arg1);
10979 quals1 = cp_type_quals (arg1);
10980 }
c8094d83 10981
dda04398
NS
10982 if (TREE_CODE (arg2) == REFERENCE_TYPE)
10983 {
10984 arg2 = TREE_TYPE (arg2);
10985 quals2 = cp_type_quals (arg2);
10986 }
6467930b 10987
dda04398
NS
10988 if ((quals1 < 0) != (quals2 < 0))
10989 {
10990 /* Only of the args is a reference, see if we should apply
10991 array/function pointer decay to it. This is not part of
10992 DR214, but is, IMHO, consistent with the deduction rules
10993 for the function call itself, and with our earlier
10994 implementation of the underspecified partial ordering
10995 rules. (nathan). */
10996 if (quals1 >= 0)
10997 {
10998 switch (TREE_CODE (arg1))
10999 {
11000 case ARRAY_TYPE:
11001 arg1 = TREE_TYPE (arg1);
11002 /* FALLTHROUGH. */
11003 case FUNCTION_TYPE:
11004 arg1 = build_pointer_type (arg1);
11005 break;
c8094d83 11006
dda04398
NS
11007 default:
11008 break;
11009 }
11010 }
11011 else
11012 {
11013 switch (TREE_CODE (arg2))
11014 {
11015 case ARRAY_TYPE:
11016 arg2 = TREE_TYPE (arg2);
11017 /* FALLTHROUGH. */
11018 case FUNCTION_TYPE:
11019 arg2 = build_pointer_type (arg2);
11020 break;
c8094d83 11021
dda04398
NS
11022 default:
11023 break;
11024 }
11025 }
11026 }
c8094d83 11027
dda04398
NS
11028 arg1 = TYPE_MAIN_VARIANT (arg1);
11029 arg2 = TYPE_MAIN_VARIANT (arg2);
c8094d83 11030
dda04398
NS
11031 deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
11032 deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
11033
11034 if (!deduce1)
11035 better2 = -1;
11036 if (!deduce2)
11037 better1 = -1;
11038 if (better1 < 0 && better2 < 0)
11039 /* We've failed to deduce something in either direction.
11040 These must be unordered. */
11041 break;
c8094d83 11042
dda04398
NS
11043 if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
11044 {
11045 /* Deduces in both directions, see if quals can
11046 disambiguate. Pretend the worse one failed to deduce. */
11047 if ((quals1 & quals2) == quals2)
11048 deduce1 = 0;
11049 if ((quals1 & quals2) == quals1)
11050 deduce2 = 0;
11051 }
11052 if (deduce1 && !deduce2 && !better2)
11053 better2 = 1;
11054 if (deduce2 && !deduce1 && !better1)
11055 better1 = 1;
c8094d83 11056
dda04398
NS
11057 args1 = TREE_CHAIN (args1);
11058 args2 = TREE_CHAIN (args2);
11059 }
11060
11061 processing_template_decl--;
11062
11063 return (better1 > 0) - (better2 > 0);
73aad9b9 11064}
6467930b 11065
916b63c3 11066/* Determine which of two partial specializations is more specialized.
6467930b 11067
916b63c3
MM
11068 PAT1 is a TREE_LIST whose TREE_TYPE is the _TYPE node corresponding
11069 to the first partial specialization. The TREE_VALUE is the
11070 innermost set of template parameters for the partial
11071 specialization. PAT2 is similar, but for the second template.
11072
11073 Return 1 if the first partial specialization is more specialized;
11074 -1 if the second is more specialized; 0 if neither is more
11075 specialized.
55ece1b3 11076
916b63c3 11077 See [temp.class.order] for information about determining which of
3db45ab5 11078 two templates is more specialized. */
c8094d83 11079
b5791fdc 11080static int
916b63c3 11081more_specialized_class (tree pat1, tree pat2)
73aad9b9
JM
11082{
11083 tree targs;
916b63c3 11084 tree tmpl1, tmpl2;
73aad9b9
JM
11085 int winner = 0;
11086
916b63c3
MM
11087 tmpl1 = TREE_TYPE (pat1);
11088 tmpl2 = TREE_TYPE (pat2);
11089
c8094d83 11090 /* Just like what happens for functions, if we are ordering between
baa49730
GB
11091 different class template specializations, we may encounter dependent
11092 types in the arguments, and we need our dependency check functions
11093 to behave correctly. */
11094 ++processing_template_decl;
3db45ab5 11095 targs = get_class_bindings (TREE_VALUE (pat1),
916b63c3
MM
11096 CLASSTYPE_TI_ARGS (tmpl1),
11097 CLASSTYPE_TI_ARGS (tmpl2));
73aad9b9
JM
11098 if (targs)
11099 --winner;
11100
3db45ab5 11101 targs = get_class_bindings (TREE_VALUE (pat2),
916b63c3
MM
11102 CLASSTYPE_TI_ARGS (tmpl2),
11103 CLASSTYPE_TI_ARGS (tmpl1));
73aad9b9 11104 if (targs)
6467930b 11105 ++winner;
baa49730 11106 --processing_template_decl;
6467930b
MS
11107
11108 return winner;
11109}
73aad9b9
JM
11110
11111/* Return the template arguments that will produce the function signature
e1467ff2 11112 DECL from the function template FN, with the explicit template
a34d3336 11113 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
4393e105 11114 also match. Return NULL_TREE if no satisfactory arguments could be
a34d3336 11115 found. */
c8094d83 11116
76b9a14d 11117static tree
a34d3336 11118get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
73aad9b9 11119{
98c1c668 11120 int ntparms = DECL_NTPARMS (fn);
f31c0a32 11121 tree targs = make_tree_vec (ntparms);
4393e105 11122 tree decl_type;
03017874 11123 tree decl_arg_types;
98c1c668 11124
4393e105
MM
11125 /* Substitute the explicit template arguments into the type of DECL.
11126 The call to fn_type_unification will handle substitution into the
11127 FN. */
11128 decl_type = TREE_TYPE (decl);
11129 if (explicit_args && uses_template_parms (decl_type))
11130 {
11131 tree tmpl;
11132 tree converted_args;
11133
11134 if (DECL_TEMPLATE_INFO (decl))
11135 tmpl = DECL_TI_TEMPLATE (decl);
11136 else
0e339752 11137 /* We can get here for some invalid specializations. */
4393e105
MM
11138 return NULL_TREE;
11139
11140 converted_args
e7e93965
MM
11141 = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
11142 explicit_args, NULL_TREE,
3db45ab5 11143 tf_none,
e7e93965
MM
11144 /*require_all_args=*/false,
11145 /*use_default_args=*/false);
4393e105
MM
11146 if (converted_args == error_mark_node)
11147 return NULL_TREE;
c8094d83
MS
11148
11149 decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
4393e105
MM
11150 if (decl_type == error_mark_node)
11151 return NULL_TREE;
11152 }
11153
11154 decl_arg_types = TYPE_ARG_TYPES (decl_type);
e5214479
JM
11155 /* Never do unification on the 'this' parameter. */
11156 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
03017874 11157 decl_arg_types = TREE_CHAIN (decl_arg_types);
d7684f2d 11158
c8094d83 11159 if (fn_type_unification (fn, explicit_args, targs,
03017874 11160 decl_arg_types,
8d3631f8 11161 (check_rettype || DECL_CONV_FN_P (fn)
0cbd7506 11162 ? TREE_TYPE (decl_type) : NULL_TREE),
30f86ec3 11163 DEDUCE_EXACT, LOOKUP_NORMAL))
76b9a14d
JM
11164 return NULL_TREE;
11165
76b9a14d
JM
11166 return targs;
11167}
11168
36a117a5
MM
11169/* Return the innermost template arguments that, when applied to a
11170 template specialization whose innermost template parameters are
9471d3e2 11171 TPARMS, and whose specialization arguments are PARMS, yield the
c8094d83 11172 ARGS.
36a117a5
MM
11173
11174 For example, suppose we have:
11175
11176 template <class T, class U> struct S {};
11177 template <class T> struct S<T*, int> {};
11178
11179 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
916b63c3 11180 {T}, the SPEC_ARGS will be {T*, int} and the ARGS will be {double*,
36a117a5
MM
11181 int}. The resulting vector will be {double}, indicating that `T'
11182 is bound to `double'. */
11183
bd6dd845 11184static tree
916b63c3 11185get_class_bindings (tree tparms, tree spec_args, tree args)
73aad9b9 11186{
3b3ba9f0 11187 int i, ntparms = TREE_VEC_LENGTH (tparms);
916b63c3
MM
11188 tree deduced_args;
11189 tree innermost_deduced_args;
73aad9b9 11190
916b63c3
MM
11191 innermost_deduced_args = make_tree_vec (ntparms);
11192 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11193 {
11194 deduced_args = copy_node (args);
11195 SET_TMPL_ARGS_LEVEL (deduced_args,
11196 TMPL_ARGS_DEPTH (deduced_args),
11197 innermost_deduced_args);
11198 }
11199 else
3db45ab5 11200 deduced_args = innermost_deduced_args;
916b63c3 11201
3db45ab5
MS
11202 if (unify (tparms, deduced_args,
11203 INNERMOST_TEMPLATE_ARGS (spec_args),
11204 INNERMOST_TEMPLATE_ARGS (args),
0cbd7506 11205 UNIFY_ALLOW_NONE))
fcfb9f96 11206 return NULL_TREE;
73aad9b9
JM
11207
11208 for (i = 0; i < ntparms; ++i)
916b63c3 11209 if (! TREE_VEC_ELT (innermost_deduced_args, i))
73aad9b9
JM
11210 return NULL_TREE;
11211
916b63c3
MM
11212 /* Verify that nondeduced template arguments agree with the type
11213 obtained from argument deduction.
3db45ab5 11214
916b63c3
MM
11215 For example:
11216
11217 struct A { typedef int X; };
11218 template <class T, class U> struct C {};
11219 template <class T> struct C<T, typename T::X> {};
11220
11221 Then with the instantiation `C<A, int>', we can deduce that
11222 `T' is `A' but unify () does not check whether `typename T::X'
11223 is `int'. */
11224 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
11225 if (spec_args == error_mark_node
11226 /* We only need to check the innermost arguments; the other
11227 arguments will always agree. */
11228 || !comp_template_args (INNERMOST_TEMPLATE_ARGS (spec_args),
11229 INNERMOST_TEMPLATE_ARGS (args)))
74601d7c
KL
11230 return NULL_TREE;
11231
916b63c3 11232 return deduced_args;
73aad9b9
JM
11233}
11234
7ca383e6
MM
11235/* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
11236 Return the TREE_LIST node with the most specialized template, if
11237 any. If there is no most specialized template, the error_mark_node
11238 is returned.
11239
11240 Note that this function does not look at, or modify, the
11241 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
11242 returned is one of the elements of INSTANTIATIONS, callers may
11243 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
11244 and retrieve it from the value returned. */
73aad9b9
JM
11245
11246tree
7ca383e6 11247most_specialized_instantiation (tree templates)
73aad9b9 11248{
104bf76a 11249 tree fn, champ;
73aad9b9 11250
dda04398 11251 ++processing_template_decl;
c8094d83 11252
7ca383e6
MM
11253 champ = templates;
11254 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
73aad9b9 11255 {
dda04398 11256 int fate = 0;
c8094d83 11257
a34d3336
NS
11258 if (get_bindings (TREE_VALUE (champ),
11259 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11260 NULL_TREE, /*check_ret=*/false))
dda04398
NS
11261 fate--;
11262
a34d3336
NS
11263 if (get_bindings (TREE_VALUE (fn),
11264 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11265 NULL_TREE, /*check_ret=*/false))
dda04398 11266 fate++;
c8094d83 11267
8ca4bf25
MM
11268 if (fate == -1)
11269 champ = fn;
11270 else if (!fate)
73aad9b9 11271 {
8ca4bf25
MM
11272 /* Equally specialized, move to next function. If there
11273 is no next function, nothing's most specialized. */
11274 fn = TREE_CHAIN (fn);
7ca383e6 11275 champ = fn;
8ca4bf25
MM
11276 if (!fn)
11277 break;
73aad9b9
JM
11278 }
11279 }
c8094d83 11280
dda04398
NS
11281 if (champ)
11282 /* Now verify that champ is better than everything earlier in the
11283 instantiation list. */
7ca383e6 11284 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn))
a34d3336
NS
11285 if (get_bindings (TREE_VALUE (champ),
11286 DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
11287 NULL_TREE, /*check_ret=*/false)
11288 || !get_bindings (TREE_VALUE (fn),
11289 DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
11290 NULL_TREE, /*check_ret=*/false))
dda04398
NS
11291 {
11292 champ = NULL_TREE;
11293 break;
11294 }
c8094d83 11295
dda04398 11296 processing_template_decl--;
c8094d83 11297
dda04398
NS
11298 if (!champ)
11299 return error_mark_node;
73aad9b9 11300
7ca383e6 11301 return champ;
73aad9b9
JM
11302}
11303
36a117a5 11304/* If DECL is a specialization of some template, return the most
f9a7ae04
MM
11305 general such template. Otherwise, returns NULL_TREE.
11306
11307 For example, given:
36a117a5
MM
11308
11309 template <class T> struct S { template <class U> void f(U); };
11310
11311 if TMPL is `template <class U> void S<int>::f(U)' this will return
11312 the full template. This function will not trace past partial
11313 specializations, however. For example, given in addition:
11314
11315 template <class T> struct S<T*> { template <class U> void f(U); };
11316
11317 if TMPL is `template <class U> void S<int*>::f(U)' this will return
11318 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9 11319
612c671a 11320tree
3a978d72 11321most_general_template (tree decl)
73aad9b9 11322{
f9a7ae04
MM
11323 /* If DECL is a FUNCTION_DECL, find the TEMPLATE_DECL of which it is
11324 an immediate specialization. */
11325 if (TREE_CODE (decl) == FUNCTION_DECL)
11326 {
11327 if (DECL_TEMPLATE_INFO (decl)) {
11328 decl = DECL_TI_TEMPLATE (decl);
11329
11330 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE for a
11331 template friend. */
11332 if (TREE_CODE (decl) != TEMPLATE_DECL)
11333 return NULL_TREE;
11334 } else
11335 return NULL_TREE;
11336 }
11337
11338 /* Look for more and more general templates. */
11339 while (DECL_TEMPLATE_INFO (decl))
11340 {
10b1d5e7
MM
11341 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
11342 (See cp-tree.h for details.) */
f9a7ae04
MM
11343 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11344 break;
11345
6e049fcd
KL
11346 if (CLASS_TYPE_P (TREE_TYPE (decl))
11347 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
11348 break;
11349
f9a7ae04
MM
11350 /* Stop if we run into an explicitly specialized class template. */
11351 if (!DECL_NAMESPACE_SCOPE_P (decl)
11352 && DECL_CONTEXT (decl)
11353 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
11354 break;
11355
11356 decl = DECL_TI_TEMPLATE (decl);
11357 }
36a117a5
MM
11358
11359 return decl;
11360}
11361
916b63c3
MM
11362/* Return the most specialized of the class template partial
11363 specializations of TMPL which can produce TYPE, a specialization of
11364 TMPL. The value returned is actually a TREE_LIST; the TREE_TYPE is
11365 a _TYPE node corresponding to the partial specialization, while the
11366 TREE_PURPOSE is the set of template arguments that must be
11367 substituted into the TREE_TYPE in order to generate TYPE.
11368
11369 If the choice of partial specialization is ambiguous, a diagnostic
11370 is issued, and the error_mark_node is returned. If there are no
11371 partial specializations of TMPL matching TYPE, then NULL_TREE is
11372 returned. */
36a117a5 11373
e9659ab0 11374static tree
916b63c3 11375most_specialized_class (tree type, tree tmpl)
36a117a5
MM
11376{
11377 tree list = NULL_TREE;
11378 tree t;
11379 tree champ;
73aad9b9 11380 int fate;
916b63c3
MM
11381 bool ambiguous_p;
11382 tree args;
73aad9b9 11383
36a117a5 11384 tmpl = most_general_template (tmpl);
916b63c3 11385 args = CLASSTYPE_TI_ARGS (type);
36a117a5 11386 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 11387 {
916b63c3
MM
11388 tree partial_spec_args;
11389 tree spec_args;
11390
11391 partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
3db45ab5
MS
11392 spec_args = get_class_bindings (TREE_VALUE (t),
11393 partial_spec_args,
916b63c3 11394 args);
36a117a5 11395 if (spec_args)
73aad9b9 11396 {
916b63c3 11397 list = tree_cons (spec_args, TREE_VALUE (t), list);
73aad9b9
JM
11398 TREE_TYPE (list) = TREE_TYPE (t);
11399 }
11400 }
11401
11402 if (! list)
11403 return NULL_TREE;
11404
916b63c3 11405 ambiguous_p = false;
73aad9b9
JM
11406 t = list;
11407 champ = t;
11408 t = TREE_CHAIN (t);
11409 for (; t; t = TREE_CHAIN (t))
11410 {
916b63c3 11411 fate = more_specialized_class (champ, t);
73aad9b9
JM
11412 if (fate == 1)
11413 ;
11414 else
11415 {
11416 if (fate == 0)
11417 {
11418 t = TREE_CHAIN (t);
11419 if (! t)
916b63c3
MM
11420 {
11421 ambiguous_p = true;
11422 break;
11423 }
73aad9b9
JM
11424 }
11425 champ = t;
11426 }
11427 }
11428
916b63c3
MM
11429 if (!ambiguous_p)
11430 for (t = list; t && t != champ; t = TREE_CHAIN (t))
11431 {
11432 fate = more_specialized_class (champ, t);
11433 if (fate != 1)
11434 {
11435 ambiguous_p = true;
11436 break;
11437 }
11438 }
11439
11440 if (ambiguous_p)
73aad9b9 11441 {
916b63c3
MM
11442 const char *str = "candidates are:";
11443 error ("ambiguous class template instantiation for %q#T", type);
11444 for (t = list; t; t = TREE_CHAIN (t))
11445 {
11446 error ("%s %+#T", str, TREE_TYPE (t));
11447 str = " ";
11448 }
11449 return error_mark_node;
73aad9b9
JM
11450 }
11451
11452 return champ;
11453}
11454
eb8845be 11455/* Explicitly instantiate DECL. */
e92cc029 11456
8d08fdba 11457void
eb8845be 11458do_decl_instantiation (tree decl, tree storage)
8d08fdba 11459{
8d08fdba 11460 tree result = NULL_TREE;
faae18ab 11461 int extern_p = 0;
e8abc66f 11462
ad47b891 11463 if (!decl || decl == error_mark_node)
dc957d14 11464 /* An error occurred, for which grokdeclarator has already issued
3fa56191
MM
11465 an appropriate message. */
11466 return;
11467 else if (! DECL_LANG_SPECIFIC (decl))
ec255269 11468 {
0f51ccfc 11469 error ("explicit instantiation of non-template %q#D", decl);
ec255269
MS
11470 return;
11471 }
03d0f4af 11472 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 11473 {
03d0f4af
MM
11474 /* There is an asymmetry here in the way VAR_DECLs and
11475 FUNCTION_DECLs are handled by grokdeclarator. In the case of
11476 the latter, the DECL we get back will be marked as a
11477 template instantiation, and the appropriate
11478 DECL_TEMPLATE_INFO will be set up. This does not happen for
11479 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
11480 should handle VAR_DECLs as it currently handles
11481 FUNCTION_DECLs. */
86ac0575 11482 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
e0860732 11483 if (!result || TREE_CODE (result) != VAR_DECL)
03d0f4af 11484 {
0f51ccfc 11485 error ("no matching template for %qD found", decl);
03d0f4af
MM
11486 return;
11487 }
6633d636
MS
11488 }
11489 else if (TREE_CODE (decl) != FUNCTION_DECL)
11490 {
0f51ccfc 11491 error ("explicit instantiation of %q#D", decl);
6633d636
MS
11492 return;
11493 }
03d0f4af
MM
11494 else
11495 result = decl;
672476cb 11496
03d0f4af 11497 /* Check for various error cases. Note that if the explicit
0e339752 11498 instantiation is valid the RESULT will currently be marked as an
03d0f4af
MM
11499 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
11500 until we get here. */
11501
11502 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 11503 {
07782718
KL
11504 /* DR 259 [temp.spec].
11505
11506 Both an explicit instantiation and a declaration of an explicit
11507 specialization shall not appear in a program unless the explicit
11508 instantiation follows a declaration of the explicit specialization.
03d0f4af 11509
07782718
KL
11510 For a given set of template parameters, if an explicit
11511 instantiation of a template appears after a declaration of an
11512 explicit specialization for that template, the explicit
11513 instantiation has no effect. */
672476cb
MM
11514 return;
11515 }
03d0f4af
MM
11516 else if (DECL_EXPLICIT_INSTANTIATION (result))
11517 {
11518 /* [temp.spec]
98c1c668 11519
03d0f4af 11520 No program shall explicitly instantiate any template more
c8094d83 11521 than once.
03d0f4af 11522
4684cd27
MM
11523 We check DECL_NOT_REALLY_EXTERN so as not to complain when
11524 the first instantiation was `extern' and the second is not,
11525 and EXTERN_P for the opposite case. */
11526 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
0f51ccfc 11527 pedwarn ("duplicate explicit instantiation of %q#D", result);
4684cd27
MM
11528 /* If an "extern" explicit instantiation follows an ordinary
11529 explicit instantiation, the template is instantiated. */
11530 if (extern_p)
03d0f4af
MM
11531 return;
11532 }
11533 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 11534 {
0f51ccfc 11535 error ("no matching template for %qD found", result);
faae18ab
MS
11536 return;
11537 }
03d0f4af 11538 else if (!DECL_TEMPLATE_INFO (result))
6633d636 11539 {
0f51ccfc 11540 pedwarn ("explicit instantiation of non-template %q#D", result);
6633d636
MS
11541 return;
11542 }
11543
f0e01782 11544 if (storage == NULL_TREE)
00595019 11545 ;
faae18ab 11546 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af 11547 {
c02f5e29 11548 if (pedantic && !in_system_header)
0f51ccfc 11549 pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
0cbd7506 11550 "instantiations");
03d0f4af
MM
11551 extern_p = 1;
11552 }
f0e01782 11553 else
0f51ccfc 11554 error ("storage class %qD applied to template instantiation", storage);
3db45ab5 11555
9c12301f 11556 check_explicit_instantiation_namespace (result);
5566b478 11557 mark_decl_instantiated (result, extern_p);
c91a56d2 11558 if (! extern_p)
3db45ab5 11559 instantiate_decl (result, /*defer_ok=*/1,
eba839f9 11560 /*expl_inst_class_mem_p=*/false);
7177d104
MS
11561}
11562
b5791fdc 11563static void
3a978d72 11564mark_class_instantiated (tree t, int extern_p)
faae18ab
MS
11565{
11566 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
11567 SET_CLASSTYPE_INTERFACE_KNOWN (t);
11568 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
11569 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
11570 if (! extern_p)
11571 {
11572 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
11573 rest_of_type_compilation (t, 1);
11574 }
c8094d83 11575}
e8abc66f 11576
5e0c54e5 11577/* Called from do_type_instantiation through binding_table_foreach to
9bcb9aae 11578 do recursive instantiation for the type bound in ENTRY. */
5e0c54e5
GDR
11579static void
11580bt_instantiate_type_proc (binding_entry entry, void *data)
11581{
11582 tree storage = *(tree *) data;
11583
11584 if (IS_AGGR_TYPE (entry->type)
11585 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
11586 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
11587}
11588
415c974c
MA
11589/* Called from do_type_instantiation to instantiate a member
11590 (a member function or a static member variable) of an
03fd3f84 11591 explicitly instantiated class template. */
415c974c
MA
11592static void
11593instantiate_class_member (tree decl, int extern_p)
11594{
11595 mark_decl_instantiated (decl, extern_p);
415c974c 11596 if (! extern_p)
3db45ab5 11597 instantiate_decl (decl, /*defer_ok=*/1,
eba839f9 11598 /*expl_inst_class_mem_p=*/true);
415c974c
MA
11599}
11600
a1bcc528
JM
11601/* Perform an explicit instantiation of template class T. STORAGE, if
11602 non-null, is the RID for extern, inline or static. COMPLAIN is
838dfd8a 11603 nonzero if this is called from the parser, zero if called recursively,
a1bcc528 11604 since the standard is unclear (as detailed below). */
c8094d83 11605
7177d104 11606void
3a978d72 11607do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
7177d104 11608{
e8abc66f
MS
11609 int extern_p = 0;
11610 int nomem_p = 0;
5566b478 11611 int static_p = 0;
4746cf84 11612 int previous_instantiation_extern_p = 0;
5566b478 11613
ca79f85d
JM
11614 if (TREE_CODE (t) == TYPE_DECL)
11615 t = TREE_TYPE (t);
11616
7ddedda4 11617 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 11618 {
0f51ccfc 11619 error ("explicit instantiation of non-template type %qT", t);
ca79f85d
JM
11620 return;
11621 }
11622
5566b478 11623 complete_type (t);
7177d104 11624
d0f062fb 11625 if (!COMPLETE_TYPE_P (t))
f0e01782 11626 {
c2ea3a40 11627 if (complain & tf_error)
0f51ccfc 11628 error ("explicit instantiation of %q#T before definition of template",
0cbd7506 11629 t);
f0e01782
MS
11630 return;
11631 }
11632
03d0f4af 11633 if (storage != NULL_TREE)
f0e01782 11634 {
c02f5e29 11635 if (pedantic && !in_system_header)
c8094d83 11636 pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
0cbd7506 11637 storage);
03d0f4af
MM
11638
11639 if (storage == ridpointers[(int) RID_INLINE])
11640 nomem_p = 1;
11641 else if (storage == ridpointers[(int) RID_EXTERN])
11642 extern_p = 1;
11643 else if (storage == ridpointers[(int) RID_STATIC])
11644 static_p = 1;
11645 else
11646 {
0f51ccfc 11647 error ("storage class %qD applied to template instantiation",
0cbd7506 11648 storage);
03d0f4af
MM
11649 extern_p = 0;
11650 }
f0e01782
MS
11651 }
11652
370af2d5 11653 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
03d0f4af 11654 {
07782718 11655 /* DR 259 [temp.spec].
a292b002 11656
07782718
KL
11657 Both an explicit instantiation and a declaration of an explicit
11658 specialization shall not appear in a program unless the explicit
11659 instantiation follows a declaration of the explicit specialization.
11660
11661 For a given set of template parameters, if an explicit
11662 instantiation of a template appears after a declaration of an
11663 explicit specialization for that template, the explicit
11664 instantiation has no effect. */
03d0f4af
MM
11665 return;
11666 }
11667 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 11668 {
03d0f4af
MM
11669 /* [temp.spec]
11670
11671 No program shall explicitly instantiate any template more
c8094d83 11672 than once.
03d0f4af 11673
0cbd7506 11674 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
4746cf84 11675 instantiation was `extern'. If EXTERN_P then the second is.
4684cd27 11676 These cases are OK. */
4746cf84
MA
11677 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
11678
11679 if (!previous_instantiation_extern_p && !extern_p
c2ea3a40 11680 && (complain & tf_error))
0f51ccfc 11681 pedwarn ("duplicate explicit instantiation of %q#T", t);
c8094d83 11682
03d0f4af
MM
11683 /* If we've already instantiated the template, just return now. */
11684 if (!CLASSTYPE_INTERFACE_ONLY (t))
11685 return;
44a8d0b3 11686 }
e8abc66f 11687
9c12301f 11688 check_explicit_instantiation_namespace (TYPE_NAME (t));
03d0f4af 11689 mark_class_instantiated (t, extern_p);
03d0f4af 11690
e8abc66f
MS
11691 if (nomem_p)
11692 return;
11693
7177d104 11694 {
db5ae43f 11695 tree tmp;
5566b478 11696
03d0f4af
MM
11697 /* In contrast to implicit instantiation, where only the
11698 declarations, and not the definitions, of members are
11699 instantiated, we have here:
11700
0cbd7506 11701 [temp.explicit]
03d0f4af
MM
11702
11703 The explicit instantiation of a class template specialization
11704 implies the instantiation of all of its members not
11705 previously explicitly specialized in the translation unit
c8094d83 11706 containing the explicit instantiation.
03d0f4af
MM
11707
11708 Of course, we can't instantiate member template classes, since
11709 we don't have any arguments for them. Note that the standard
dc957d14 11710 is unclear on whether the instantiation of the members are
415c974c 11711 *explicit* instantiations or not. However, the most natural
03fd3f84 11712 interpretation is that it should be an explicit instantiation. */
03d0f4af 11713
5566b478
MS
11714 if (! static_p)
11715 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 11716 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 11717 && DECL_TEMPLATE_INSTANTIATION (tmp))
415c974c 11718 instantiate_class_member (tmp, extern_p);
5566b478
MS
11719
11720 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
11721 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
415c974c 11722 instantiate_class_member (tmp, extern_p);
7177d104 11723
5e0c54e5
GDR
11724 if (CLASSTYPE_NESTED_UTDS (t))
11725 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
0cbd7506 11726 bt_instantiate_type_proc, &storage);
a292b002 11727 }
8d08fdba 11728}
a28e3c7f 11729
36a117a5
MM
11730/* Given a function DECL, which is a specialization of TMPL, modify
11731 DECL to be a re-instantiation of TMPL with the same template
11732 arguments. TMPL should be the template into which tsubst'ing
11733 should occur for DECL, not the most general template.
830bfa74
MM
11734
11735 One reason for doing this is a scenario like this:
11736
11737 template <class T>
11738 void f(const T&, int i);
11739
11740 void g() { f(3, 7); }
11741
11742 template <class T>
11743 void f(const T& t, const int i) { }
11744
11745 Note that when the template is first instantiated, with
11746 instantiate_template, the resulting DECL will have no name for the
11747 first parameter, and the wrong type for the second. So, when we go
11748 to instantiate the DECL, we regenerate it. */
11749
e9659ab0 11750static void
3a978d72 11751regenerate_decl_from_template (tree decl, tree tmpl)
830bfa74 11752{
f9a7ae04
MM
11753 /* The arguments used to instantiate DECL, from the most general
11754 template. */
830bfa74 11755 tree args;
830bfa74 11756 tree code_pattern;
830bfa74
MM
11757
11758 args = DECL_TI_ARGS (decl);
11759 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
11760
2b907f5c
KL
11761 /* Make sure that we can see identifiers, and compute access
11762 correctly. */
11763 push_access_scope (decl);
11764
c7222c02
MM
11765 if (TREE_CODE (decl) == FUNCTION_DECL)
11766 {
11767 tree decl_parm;
11768 tree pattern_parm;
11769 tree specs;
11770 int args_depth;
11771 int parms_depth;
c8094d83 11772
c7222c02 11773 args_depth = TMPL_ARGS_DEPTH (args);
c8094d83 11774 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
c7222c02
MM
11775 if (args_depth > parms_depth)
11776 args = get_innermost_template_args (args, parms_depth);
11777
11778 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
11779 args, tf_error, NULL_TREE);
11780 if (specs)
11781 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
11782 specs);
11783
11784 /* Merge parameter declarations. */
c8094d83 11785 decl_parm = skip_artificial_parms_for (decl,
c7222c02 11786 DECL_ARGUMENTS (decl));
c8094d83 11787 pattern_parm
c7222c02
MM
11788 = skip_artificial_parms_for (code_pattern,
11789 DECL_ARGUMENTS (code_pattern));
11790 while (decl_parm)
11791 {
11792 tree parm_type;
b17bba6d 11793 tree attributes;
c7222c02
MM
11794
11795 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
11796 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
11797 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
11798 NULL_TREE);
02bab9db 11799 parm_type = type_decays_to (parm_type);
c7222c02
MM
11800 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
11801 TREE_TYPE (decl_parm) = parm_type;
b17bba6d
MM
11802 attributes = DECL_ATTRIBUTES (pattern_parm);
11803 if (DECL_ATTRIBUTES (decl_parm) != attributes)
11804 {
11805 DECL_ATTRIBUTES (decl_parm) = attributes;
11806 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
11807 }
c7222c02
MM
11808 decl_parm = TREE_CHAIN (decl_parm);
11809 pattern_parm = TREE_CHAIN (pattern_parm);
11810 }
830bfa74 11811
c7222c02
MM
11812 /* Merge additional specifiers from the CODE_PATTERN. */
11813 if (DECL_DECLARED_INLINE_P (code_pattern)
11814 && !DECL_DECLARED_INLINE_P (decl))
11815 DECL_DECLARED_INLINE_P (decl) = 1;
11816 if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
11817 DECL_INLINE (decl) = 1;
11818 }
11819 else if (TREE_CODE (decl) == VAR_DECL)
b794e321
MM
11820 DECL_INITIAL (decl) =
11821 tsubst_expr (DECL_INITIAL (code_pattern), args,
015c2c66
MM
11822 tf_error, DECL_TI_TEMPLATE (decl),
11823 /*integral_constant_expression_p=*/false);
c7222c02
MM
11824 else
11825 gcc_unreachable ();
36a117a5 11826
2b59fc25 11827 pop_access_scope (decl);
830bfa74
MM
11828}
11829
a723baf1
MM
11830/* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
11831 substituted to get DECL. */
11832
d58b7c2d 11833tree
a723baf1
MM
11834template_for_substitution (tree decl)
11835{
11836 tree tmpl = DECL_TI_TEMPLATE (decl);
11837
11838 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
11839 for the instantiation. This is not always the most general
11840 template. Consider, for example:
11841
0cbd7506 11842 template <class T>
a723baf1 11843 struct S { template <class U> void f();
0cbd7506 11844 template <> void f<int>(); };
a723baf1
MM
11845
11846 and an instantiation of S<double>::f<int>. We want TD to be the
11847 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
11848 while (/* An instantiation cannot have a definition, so we need a
11849 more general template. */
11850 DECL_TEMPLATE_INSTANTIATION (tmpl)
11851 /* We must also deal with friend templates. Given:
11852
c8094d83 11853 template <class T> struct S {
a723baf1
MM
11854 template <class U> friend void f() {};
11855 };
11856
11857 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
11858 so far as the language is concerned, but that's still
11859 where we get the pattern for the instantiation from. On
11860 other hand, if the definition comes outside the class, say:
11861
c8094d83 11862 template <class T> struct S {
a723baf1
MM
11863 template <class U> friend void f();
11864 };
11865 template <class U> friend void f() {}
11866
11867 we don't need to look any further. That's what the check for
11868 DECL_INITIAL is for. */
11869 || (TREE_CODE (decl) == FUNCTION_DECL
11870 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
11871 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
11872 {
11873 /* The present template, TD, should not be a definition. If it
11874 were a definition, we should be using it! Note that we
11875 cannot restructure the loop to just keep going until we find
11876 a template with a definition, since that might go too far if
11877 a specialization was declared, but not defined. */
50bc768d
NS
11878 gcc_assert (TREE_CODE (decl) != VAR_DECL
11879 || DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
c8094d83 11880
a723baf1
MM
11881 /* Fetch the more general template. */
11882 tmpl = DECL_TI_TEMPLATE (tmpl);
11883 }
11884
11885 return tmpl;
11886}
11887
16d53b64 11888/* Produce the definition of D, a _DECL generated from a template. If
838dfd8a 11889 DEFER_OK is nonzero, then we don't have to actually do the
415c974c
MA
11890 instantiation now; we just have to do it sometime. Normally it is
11891 an error if this is an explicit instantiation but D is undefined.
eba839f9
MM
11892 EXPL_INST_CLASS_MEM_P is true iff D is a member of an
11893 explicitly instantiated class template. */
f84b4be9 11894
a28e3c7f 11895tree
3db45ab5 11896instantiate_decl (tree d, int defer_ok,
eba839f9 11897 bool expl_inst_class_mem_p)
a28e3c7f 11898{
36a117a5 11899 tree tmpl = DECL_TI_TEMPLATE (d);
65f8b0fb
MM
11900 tree gen_args;
11901 tree args;
830bfa74 11902 tree td;
36a117a5
MM
11903 tree code_pattern;
11904 tree spec;
11905 tree gen_tmpl;
b794e321 11906 bool pattern_defined;
31a714f6 11907 int need_push;
82a98427 11908 location_t saved_loc = input_location;
f7e4e484 11909 bool external_p;
c8094d83 11910
36a117a5
MM
11911 /* This function should only be used to instantiate templates for
11912 functions and static member variables. */
50bc768d
NS
11913 gcc_assert (TREE_CODE (d) == FUNCTION_DECL
11914 || TREE_CODE (d) == VAR_DECL);
36a117a5 11915
cec24319
MM
11916 /* Variables are never deferred; if instantiation is required, they
11917 are instantiated right away. That allows for better code in the
11918 case that an expression refers to the value of the variable --
11919 if the variable has a constant value the referring expression can
11920 take advantage of that fact. */
11921 if (TREE_CODE (d) == VAR_DECL)
11922 defer_ok = 0;
11923
db9b2174
MM
11924 /* Don't instantiate cloned functions. Instead, instantiate the
11925 functions they cloned. */
11926 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
11927 d = DECL_CLONED_FUNCTION (d);
11928
fbf1c34b 11929 if (DECL_TEMPLATE_INSTANTIATED (d))
03d0f4af 11930 /* D has already been instantiated. It might seem reasonable to
dc957d14 11931 check whether or not D is an explicit instantiation, and, if so,
03d0f4af
MM
11932 stop here. But when an explicit instantiation is deferred
11933 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
11934 is set, even though we still need to do the instantiation. */
36a117a5
MM
11935 return d;
11936
11937 /* If we already have a specialization of this declaration, then
11938 there's no reason to instantiate it. Note that
11939 retrieve_specialization gives us both instantiations and
11940 specializations, so we must explicitly check
11941 DECL_TEMPLATE_SPECIALIZATION. */
11942 gen_tmpl = most_general_template (tmpl);
65f8b0fb 11943 gen_args = DECL_TI_ARGS (d);
c7222c02
MM
11944 spec = retrieve_specialization (gen_tmpl, gen_args,
11945 /*class_specializations_p=*/false);
36a117a5
MM
11946 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
11947 return spec;
11948
11949 /* This needs to happen before any tsubsting. */
11950 if (! push_tinst_level (d))
11951 return d;
11952
297a5329
JM
11953 timevar_push (TV_PARSE);
11954
4d85e00e 11955 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
a723baf1
MM
11956 for the instantiation. */
11957 td = template_for_substitution (d);
fee23f54 11958 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 11959
2d22db1f 11960 /* We should never be trying to instantiate a member of a class
3db45ab5 11961 template or partial specialization. */
2d22db1f 11962 gcc_assert (d != code_pattern);
3db45ab5 11963
76d3baad
KL
11964 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
11965 || DECL_TEMPLATE_SPECIALIZATION (td))
649fc72d
NS
11966 /* In the case of a friend template whose definition is provided
11967 outside the class, we may have too many arguments. Drop the
76d3baad 11968 ones we don't need. The same is true for specializations. */
649fc72d
NS
11969 args = get_innermost_template_args
11970 (gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
11971 else
11972 args = gen_args;
65f8b0fb 11973
5566b478 11974 if (TREE_CODE (d) == FUNCTION_DECL)
39c76b4f 11975 pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
5566b478 11976 else
36a117a5 11977 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
b794e321
MM
11978
11979 /* We may be in the middle of deferred access check. Disable it now. */
11980 push_deferring_access_checks (dk_no_deferred);
11981
4684cd27
MM
11982 /* Unless an explicit instantiation directive has already determined
11983 the linkage of D, remember that a definition is available for
11984 this entity. */
c8094d83 11985 if (pattern_defined
4684cd27
MM
11986 && !DECL_INTERFACE_KNOWN (d)
11987 && !DECL_NOT_REALLY_EXTERN (d))
11988 mark_definable (d);
de22184b 11989
f31686a3 11990 input_location = DECL_SOURCE_LOCATION (d);
de22184b 11991
eba839f9
MM
11992 /* If D is a member of an explicitly instantiated class template,
11993 and no definition is available, treat it like an implicit
3db45ab5
MS
11994 instantiation. */
11995 if (!pattern_defined && expl_inst_class_mem_p
11996 && DECL_EXPLICIT_INSTANTIATION (d))
5566b478 11997 {
4684cd27 11998 DECL_NOT_REALLY_EXTERN (d) = 0;
eba839f9 11999 DECL_INTERFACE_KNOWN (d) = 0;
4684cd27 12000 SET_DECL_IMPLICIT_INSTANTIATION (d);
5566b478
MS
12001 }
12002
4f2b0fb2
NS
12003 if (!defer_ok)
12004 {
12005 /* Recheck the substitutions to obtain any warning messages
12006 about ignoring cv qualifiers. */
12007 tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
12008 tree type = TREE_TYPE (gen);
12009
0e902d98 12010 /* Make sure that we can see identifiers, and compute access
2b59fc25
KL
12011 correctly. D is already the target FUNCTION_DECL with the
12012 right context. */
12013 push_access_scope (d);
0e902d98 12014
4f2b0fb2
NS
12015 if (TREE_CODE (gen) == FUNCTION_DECL)
12016 {
23fca1f5 12017 tsubst (DECL_ARGUMENTS (gen), gen_args, tf_warning_or_error, d);
65f8b0fb 12018 tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
23fca1f5 12019 tf_warning_or_error, d);
4f2b0fb2
NS
12020 /* Don't simply tsubst the function type, as that will give
12021 duplicate warnings about poor parameter qualifications.
12022 The function arguments are the same as the decl_arguments
c6002625 12023 without the top level cv qualifiers. */
4f2b0fb2
NS
12024 type = TREE_TYPE (type);
12025 }
23fca1f5 12026 tsubst (type, gen_args, tf_warning_or_error, d);
0e902d98 12027
2b59fc25 12028 pop_access_scope (d);
4f2b0fb2 12029 }
c8094d83 12030
f7e4e484
MM
12031 /* Check to see whether we know that this template will be
12032 instantiated in some other file, as with "extern template"
12033 extension. */
12034 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
12035 /* In general, we do not instantiate such templates... */
12036 if (external_p
12037 /* ... but we instantiate inline functions so that we can inline
12038 them and ... */
12039 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d))
12040 /* ... we instantiate static data members whose values are
12041 needed in integral constant expressions. */
3db45ab5 12042 && ! (TREE_CODE (d) == VAR_DECL
f7e4e484 12043 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (d)))
930cd796 12044 goto out;
16d53b64 12045 /* Defer all other templates, unless we have been explicitly
f7e4e484
MM
12046 forbidden from doing so. */
12047 if (/* If there is no definition, we cannot instantiate the
12048 template. */
3db45ab5 12049 ! pattern_defined
f7e4e484
MM
12050 /* If it's OK to postpone instantiation, do so. */
12051 || defer_ok
12052 /* If this is a static data member that will be defined
12053 elsewhere, we don't want to instantiate the entire data
12054 member, but we do want to instantiate the initializer so that
12055 we can substitute that elsewhere. */
12056 || (external_p && TREE_CODE (d) == VAR_DECL))
16d53b64 12057 {
b794e321
MM
12058 /* The definition of the static data member is now required so
12059 we must substitute the initializer. */
12060 if (TREE_CODE (d) == VAR_DECL
3db45ab5 12061 && !DECL_INITIAL (d)
b794e321
MM
12062 && DECL_INITIAL (code_pattern))
12063 {
4bff36d3
MM
12064 tree ns;
12065 tree init;
12066
12067 ns = decl_namespace_context (d);
27a725e2 12068 push_nested_namespace (ns);
b794e321 12069 push_nested_class (DECL_CONTEXT (d));
3db45ab5 12070 init = tsubst_expr (DECL_INITIAL (code_pattern),
4bff36d3 12071 args,
015c2c66
MM
12072 tf_warning_or_error, NULL_TREE,
12073 /*integral_constant_expression_p=*/false);
d174af6c
MM
12074 cp_finish_decl (d, init, /*init_const_expr_p=*/false,
12075 /*asmspec_tree=*/NULL_TREE,
86414779 12076 LOOKUP_ONLYCONVERTING);
b794e321 12077 pop_nested_class ();
27a725e2 12078 pop_nested_namespace (ns);
b794e321
MM
12079 }
12080
f7e4e484
MM
12081 /* We restore the source position here because it's used by
12082 add_pending_template. */
82a98427 12083 input_location = saved_loc;
c27be9b9 12084
c8094d83 12085 if (at_eof && !pattern_defined
03d0f4af
MM
12086 && DECL_EXPLICIT_INSTANTIATION (d))
12087 /* [temp.explicit]
12088
12089 The definition of a non-exported function template, a
12090 non-exported member function template, or a non-exported
12091 member function or static data member of a class template
12092 shall be present in every translation unit in which it is
12093 explicitly instantiated. */
33bd39a2 12094 pedwarn
0f51ccfc 12095 ("explicit instantiation of %qD but no definition available", d);
03d0f4af 12096
f7e4e484
MM
12097 /* ??? Historically, we have instantiated inline functions, even
12098 when marked as "extern template". */
12099 if (!(external_p && TREE_CODE (d) == VAR_DECL))
12100 add_pending_template (d);
de22184b 12101 goto out;
5566b478 12102 }
4684cd27
MM
12103 /* Tell the repository that D is available in this translation unit
12104 -- and see if it is supposed to be instantiated here. */
12105 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
12106 {
12107 /* In a PCH file, despite the fact that the repository hasn't
12108 requested instantiation in the PCH it is still possible that
12109 an instantiation will be required in a file that includes the
12110 PCH. */
12111 if (pch_file)
12112 add_pending_template (d);
12113 /* Instantiate inline functions so that the inliner can do its
12114 job, even though we'll not be emitting a copy of this
12115 function. */
c2a124b2 12116 if (!(TREE_CODE (d) == FUNCTION_DECL
c8094d83 12117 && flag_inline_trees
c2a124b2 12118 && DECL_DECLARED_INLINE_P (d)))
4684cd27
MM
12119 goto out;
12120 }
5566b478 12121
6de9cd9a 12122 need_push = !cfun || !global_bindings_p ();
31a714f6
MM
12123 if (need_push)
12124 push_to_top_level ();
414ea4aa 12125
66e0c440
KL
12126 /* Mark D as instantiated so that recursive calls to
12127 instantiate_decl do not try to instantiate it again. */
12128 DECL_TEMPLATE_INSTANTIATED (d) = 1;
12129
2b0a63a3
MM
12130 /* Regenerate the declaration in case the template has been modified
12131 by a subsequent redeclaration. */
12132 regenerate_decl_from_template (d, td);
4684cd27 12133
120722ac 12134 /* We already set the file and line above. Reset them now in case
6de9cd9a 12135 they changed as a result of calling regenerate_decl_from_template. */
f31686a3 12136 input_location = DECL_SOURCE_LOCATION (d);
5156628f 12137
5566b478
MS
12138 if (TREE_CODE (d) == VAR_DECL)
12139 {
e92fb501
MM
12140 tree init;
12141
1d62c33e
MM
12142 /* Clear out DECL_RTL; whatever was there before may not be right
12143 since we've reset the type of the declaration. */
12144 SET_DECL_RTL (d, NULL_RTX);
5566b478 12145 DECL_IN_AGGR_P (d) = 0;
ea56c40c 12146
e92fb501
MM
12147 /* The initializer is placed in DECL_INITIAL by
12148 regenerate_decl_from_template. Pull it out so that
12149 finish_decl can process it. */
12150 init = DECL_INITIAL (d);
12151 DECL_INITIAL (d) = NULL_TREE;
12152 DECL_INITIALIZED_P (d) = 0;
12153
4684cd27
MM
12154 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
12155 initializer. That function will defer actual emission until
12156 we have a chance to determine linkage. */
12157 DECL_EXTERNAL (d) = 0;
12158
73a8adb6 12159 /* Enter the scope of D so that access-checking works correctly. */
4684cd27 12160 push_nested_class (DECL_CONTEXT (d));
e92fb501 12161 finish_decl (d, init, NULL_TREE);
73a8adb6 12162 pop_nested_class ();
5566b478
MS
12163 }
12164 else if (TREE_CODE (d) == FUNCTION_DECL)
12165 {
6bbf1598 12166 htab_t saved_local_specializations;
a723baf1
MM
12167 tree subst_decl;
12168 tree tmpl_parm;
12169 tree spec_parm;
6bbf1598
MM
12170
12171 /* Save away the current list, in case we are instantiating one
12172 template from within the body of another. */
12173 saved_local_specializations = local_specializations;
12174
6dfbb909 12175 /* Set up the list of local specializations. */
c8094d83 12176 local_specializations = htab_create (37,
69f794a7 12177 hash_local_specialization,
a723baf1 12178 eq_local_specializations,
6dfbb909
MM
12179 NULL);
12180
558475f0 12181 /* Set up context. */
058b15c1 12182 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
5566b478 12183
a723baf1
MM
12184 /* Create substitution entries for the parameters. */
12185 subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
12186 tmpl_parm = DECL_ARGUMENTS (subst_decl);
12187 spec_parm = DECL_ARGUMENTS (d);
12188 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
12189 {
12190 register_local_specialization (spec_parm, tmpl_parm);
12191 spec_parm = skip_artificial_parms_for (d, spec_parm);
12192 tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
12193 }
12194 while (tmpl_parm)
12195 {
12196 register_local_specialization (spec_parm, tmpl_parm);
12197 tmpl_parm = TREE_CHAIN (tmpl_parm);
12198 spec_parm = TREE_CHAIN (spec_parm);
12199 }
50bc768d 12200 gcc_assert (!spec_parm);
a723baf1 12201
558475f0
MM
12202 /* Substitute into the body of the function. */
12203 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
015c2c66
MM
12204 tf_warning_or_error, tmpl,
12205 /*integral_constant_expression_p=*/false);
558475f0 12206
6dfbb909
MM
12207 /* We don't need the local specializations any more. */
12208 htab_delete (local_specializations);
6bbf1598 12209 local_specializations = saved_local_specializations;
6dfbb909 12210
4d6abc1c 12211 /* Finish the function. */
b2dd096b 12212 d = finish_function (0);
8cd2462c 12213 expand_or_defer_fn (d);
5566b478
MS
12214 }
12215
971cbc14
MM
12216 /* We're not deferring instantiation any more. */
12217 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
12218
31a714f6
MM
12219 if (need_push)
12220 pop_from_top_level ();
414ea4aa 12221
de22184b 12222out:
82a98427 12223 input_location = saved_loc;
7d021397 12224 pop_deferring_access_checks ();
5566b478 12225 pop_tinst_level ();
a28e3c7f 12226
297a5329
JM
12227 timevar_pop (TV_PARSE);
12228
a28e3c7f
MS
12229 return d;
12230}
5566b478 12231
0aafb128 12232/* Run through the list of templates that we wish we could
35046a54
KL
12233 instantiate, and instantiate any we can. RETRIES is the
12234 number of times we retry pending template instantiation. */
0aafb128 12235
35046a54
KL
12236void
12237instantiate_pending_templates (int retries)
0aafb128
MM
12238{
12239 tree *t;
46ccf50a 12240 tree last = NULL_TREE;
0aafb128 12241 int reconsider;
aad626f7 12242 location_t saved_loc = input_location;
12af7ba3 12243 int saved_in_system_header = in_system_header;
35046a54
KL
12244
12245 /* Instantiating templates may trigger vtable generation. This in turn
12246 may require further template instantiations. We place a limit here
12247 to avoid infinite loop. */
12248 if (pending_templates && retries >= max_tinst_depth)
12249 {
96c993a8
JM
12250 tree decl = TREE_VALUE (pending_templates);
12251
dee15844 12252 error ("template instantiation depth exceeds maximum of %d"
96c993a8
JM
12253 " instantiating %q+D, possibly from virtual table generation"
12254 " (use -ftemplate-depth-NN to increase the maximum)",
12255 max_tinst_depth, decl);
12256 if (TREE_CODE (decl) == FUNCTION_DECL)
12257 /* Pretend that we defined it. */
12258 DECL_INITIAL (decl) = error_mark_node;
35046a54
KL
12259 return;
12260 }
12261
c8094d83 12262 do
0aafb128
MM
12263 {
12264 reconsider = 0;
12265
12266 t = &pending_templates;
12267 while (*t)
12268 {
0aafb128
MM
12269 tree instantiation = TREE_VALUE (*t);
12270
3ae18eaf 12271 reopen_tinst_level (TREE_PURPOSE (*t));
0aafb128 12272
2f939d94 12273 if (TYPE_P (instantiation))
0aafb128
MM
12274 {
12275 tree fn;
12276
d0f062fb 12277 if (!COMPLETE_TYPE_P (instantiation))
0aafb128
MM
12278 {
12279 instantiate_class_template (instantiation);
12280 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
c8094d83 12281 for (fn = TYPE_METHODS (instantiation);
0aafb128
MM
12282 fn;
12283 fn = TREE_CHAIN (fn))
12284 if (! DECL_ARTIFICIAL (fn))
3db45ab5 12285 instantiate_decl (fn,
eba839f9
MM
12286 /*defer_ok=*/0,
12287 /*expl_inst_class_mem_p=*/false);
d0f062fb 12288 if (COMPLETE_TYPE_P (instantiation))
35046a54 12289 reconsider = 1;
0aafb128
MM
12290 }
12291
d0f062fb 12292 if (COMPLETE_TYPE_P (instantiation))
0aafb128
MM
12293 /* If INSTANTIATION has been instantiated, then we don't
12294 need to consider it again in the future. */
12295 *t = TREE_CHAIN (*t);
46ccf50a
JM
12296 else
12297 {
12298 last = *t;
12299 t = &TREE_CHAIN (*t);
12300 }
0aafb128
MM
12301 }
12302 else
12303 {
16d53b64 12304 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
12305 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
12306 {
3db45ab5 12307 instantiation
eba839f9
MM
12308 = instantiate_decl (instantiation,
12309 /*defer_ok=*/0,
12310 /*expl_inst_class_mem_p=*/false);
0aafb128 12311 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
35046a54 12312 reconsider = 1;
0aafb128
MM
12313 }
12314
16d53b64 12315 if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
0aafb128
MM
12316 || DECL_TEMPLATE_INSTANTIATED (instantiation))
12317 /* If INSTANTIATION has been instantiated, then we don't
12318 need to consider it again in the future. */
12319 *t = TREE_CHAIN (*t);
46ccf50a
JM
12320 else
12321 {
12322 last = *t;
12323 t = &TREE_CHAIN (*t);
12324 }
0aafb128 12325 }
84e5ca0f
NS
12326 tinst_depth = 0;
12327 current_tinst_level = NULL_TREE;
0aafb128 12328 }
46ccf50a 12329 last_pending_template = last;
c8094d83 12330 }
0aafb128
MM
12331 while (reconsider);
12332
aad626f7 12333 input_location = saved_loc;
12af7ba3 12334 in_system_header = saved_in_system_header;
0aafb128
MM
12335}
12336
fd74ca0b
MM
12337/* Substitute ARGVEC into T, which is a list of initializers for
12338 either base class or a non-static data member. The TREE_PURPOSEs
12339 are DECLs, and the TREE_VALUEs are the initializer values. Used by
12340 instantiate_decl. */
4393e105 12341
824b9a4c 12342static tree
3a978d72 12343tsubst_initializer_list (tree t, tree argvec)
5566b478 12344{
2282d28d 12345 tree inits = NULL_TREE;
5566b478
MS
12346
12347 for (; t; t = TREE_CHAIN (t))
12348 {
fd74ca0b
MM
12349 tree decl;
12350 tree init;
fd74ca0b 12351
23fca1f5 12352 decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_warning_or_error,
fd74ca0b 12353 NULL_TREE);
1f5a253a
NS
12354 decl = expand_member_init (decl);
12355 if (decl && !DECL_P (decl))
12356 in_base_initializer = 1;
c8094d83 12357
23fca1f5 12358 init = tsubst_expr (TREE_VALUE (t), argvec, tf_warning_or_error,
015c2c66
MM
12359 NULL_TREE,
12360 /*integral_constant_expression_p=*/false);
1f5a253a
NS
12361 in_base_initializer = 0;
12362
12363 if (decl)
2282d28d 12364 {
1f5a253a 12365 init = build_tree_list (decl, init);
2282d28d
MM
12366 TREE_CHAIN (init) = inits;
12367 inits = init;
12368 }
5566b478 12369 }
2282d28d 12370 return inits;
5566b478
MS
12371}
12372
61a127b3
MM
12373/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
12374
12375static void
3a978d72 12376set_current_access_from_decl (tree decl)
61a127b3
MM
12377{
12378 if (TREE_PRIVATE (decl))
12379 current_access_specifier = access_private_node;
12380 else if (TREE_PROTECTED (decl))
12381 current_access_specifier = access_protected_node;
12382 else
12383 current_access_specifier = access_public_node;
12384}
12385
dbfe2124
MM
12386/* Instantiate an enumerated type. TAG is the template type, NEWTAG
12387 is the instantiation (which should have been created with
12388 start_enum) and ARGS are the template arguments to use. */
b87692e5 12389
dbfe2124 12390static void
3a978d72 12391tsubst_enum (tree tag, tree newtag, tree args)
b87692e5 12392{
dbfe2124 12393 tree e;
b87692e5
MS
12394
12395 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
12396 {
61a127b3 12397 tree value;
7b6d72fc
MM
12398 tree decl;
12399
12400 decl = TREE_VALUE (e);
61a127b3
MM
12401 /* Note that in a template enum, the TREE_VALUE is the
12402 CONST_DECL, not the corresponding INTEGER_CST. */
c8094d83 12403 value = tsubst_expr (DECL_INITIAL (decl),
015c2c66
MM
12404 args, tf_warning_or_error, NULL_TREE,
12405 /*integral_constant_expression_p=*/true);
61a127b3
MM
12406
12407 /* Give this enumeration constant the correct access. */
7b6d72fc 12408 set_current_access_from_decl (decl);
61a127b3
MM
12409
12410 /* Actually build the enumerator itself. */
c8094d83 12411 build_enumerator (DECL_NAME (decl), value, newtag);
dbfe2124 12412 }
b3d5a58b 12413
219670f1 12414 finish_enum (newtag);
f31686a3
RH
12415 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
12416 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
b87692e5 12417}
36a117a5 12418
1f6e1acc
AS
12419/* DECL is a FUNCTION_DECL that is a template specialization. Return
12420 its type -- but without substituting the innermost set of template
12421 arguments. So, innermost set of template parameters will appear in
5c74d5b0 12422 the type. */
1f6e1acc 12423
c8094d83 12424tree
3a978d72 12425get_mostly_instantiated_function_type (tree decl)
1f6e1acc 12426{
1f6e1acc
AS
12427 tree fn_type;
12428 tree tmpl;
12429 tree targs;
12430 tree tparms;
12431 int parm_depth;
12432
12433 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
12434 targs = DECL_TI_ARGS (decl);
12435 tparms = DECL_TEMPLATE_PARMS (tmpl);
12436 parm_depth = TMPL_PARMS_DEPTH (tparms);
12437
12438 /* There should be as many levels of arguments as there are levels
12439 of parameters. */
50bc768d 12440 gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
1f6e1acc
AS
12441
12442 fn_type = TREE_TYPE (tmpl);
1f6e1acc
AS
12443
12444 if (parm_depth == 1)
12445 /* No substitution is necessary. */
12446 ;
12447 else
12448 {
fae6e246 12449 int i, save_access_control;
1f6e1acc
AS
12450 tree partial_args;
12451
12452 /* Replace the innermost level of the TARGS with NULL_TREEs to
dc957d14 12453 let tsubst know not to substitute for those parameters. */
1f6e1acc
AS
12454 partial_args = make_tree_vec (TREE_VEC_LENGTH (targs));
12455 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
12456 SET_TMPL_ARGS_LEVEL (partial_args, i,
12457 TMPL_ARGS_LEVEL (targs, i));
12458 SET_TMPL_ARGS_LEVEL (partial_args,
12459 TMPL_ARGS_DEPTH (targs),
12460 make_tree_vec (DECL_NTPARMS (tmpl)));
12461
fae6e246
RH
12462 /* Disable access control as this function is used only during
12463 name-mangling. */
12464 save_access_control = flag_access_control;
12465 flag_access_control = 0;
5c74d5b0 12466
9579624e 12467 ++processing_template_decl;
1f6e1acc
AS
12468 /* Now, do the (partial) substitution to figure out the
12469 appropriate function type. */
c2ea3a40 12470 fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
9579624e 12471 --processing_template_decl;
1f6e1acc
AS
12472
12473 /* Substitute into the template parameters to obtain the real
12474 innermost set of parameters. This step is important if the
12475 innermost set of template parameters contains value
12476 parameters whose types depend on outer template parameters. */
12477 TREE_VEC_LENGTH (partial_args)--;
c2ea3a40 12478 tparms = tsubst_template_parms (tparms, partial_args, tf_error);
1f6e1acc 12479
fae6e246 12480 flag_access_control = save_access_control;
5c74d5b0 12481 }
1f6e1acc
AS
12482
12483 return fn_type;
12484}
669ec2b4 12485
cb753e49 12486/* Return truthvalue if we're processing a template different from
dc957d14 12487 the last one involved in diagnostics. */
cb753e49 12488int
3a978d72 12489problematic_instantiation_changed (void)
cb753e49
GDR
12490{
12491 return last_template_error_tick != tinst_level_tick;
12492}
12493
12494/* Remember current template involved in diagnostics. */
12495void
3a978d72 12496record_last_problematic_instantiation (void)
cb753e49
GDR
12497{
12498 last_template_error_tick = tinst_level_tick;
12499}
12500
12501tree
3a978d72 12502current_instantiation (void)
cb753e49
GDR
12503{
12504 return current_tinst_level;
12505}
db3f4e4e
NS
12506
12507/* [temp.param] Check that template non-type parm TYPE is of an allowable
838dfd8a 12508 type. Return zero for ok, nonzero for disallowed. Issue error and
c2ea3a40 12509 warning messages under control of COMPLAIN. */
db3f4e4e
NS
12510
12511static int
3a978d72 12512invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
db3f4e4e
NS
12513{
12514 if (INTEGRAL_TYPE_P (type))
12515 return 0;
12516 else if (POINTER_TYPE_P (type))
12517 return 0;
a5ac359a 12518 else if (TYPE_PTR_TO_MEMBER_P (type))
db3f4e4e 12519 return 0;
db3f4e4e
NS
12520 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12521 return 0;
12522 else if (TREE_CODE (type) == TYPENAME_TYPE)
12523 return 0;
c8094d83 12524
c2ea3a40 12525 if (complain & tf_error)
0f51ccfc 12526 error ("%q#T is not a valid type for a template constant parameter", type);
db3f4e4e
NS
12527 return 1;
12528}
e2500fed 12529
5552b43c
MM
12530/* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
12531 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
1fb3244a 12532
5552b43c
MM
12533static bool
12534dependent_type_p_r (tree type)
1fb3244a
MM
12535{
12536 tree scope;
12537
1fb3244a
MM
12538 /* [temp.dep.type]
12539
12540 A type is dependent if it is:
12541
6615c446
JO
12542 -- a template parameter. Template template parameters are types
12543 for us (since TYPE_P holds true for them) so we handle
12544 them here. */
c8094d83 12545 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
6bf92cb6 12546 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
1fb3244a
MM
12547 return true;
12548 /* -- a qualified-id with a nested-name-specifier which contains a
0cbd7506 12549 class-name that names a dependent type or whose unqualified-id
1fb3244a
MM
12550 names a dependent type. */
12551 if (TREE_CODE (type) == TYPENAME_TYPE)
12552 return true;
12553 /* -- a cv-qualified type where the cv-unqualified type is
0cbd7506 12554 dependent. */
1fb3244a
MM
12555 type = TYPE_MAIN_VARIANT (type);
12556 /* -- a compound type constructed from any dependent type. */
a5ac359a 12557 if (TYPE_PTR_TO_MEMBER_P (type))
1fb3244a 12558 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
c8094d83 12559 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
1fb3244a
MM
12560 (type)));
12561 else if (TREE_CODE (type) == POINTER_TYPE
12562 || TREE_CODE (type) == REFERENCE_TYPE)
12563 return dependent_type_p (TREE_TYPE (type));
12564 else if (TREE_CODE (type) == FUNCTION_TYPE
12565 || TREE_CODE (type) == METHOD_TYPE)
12566 {
12567 tree arg_type;
12568
12569 if (dependent_type_p (TREE_TYPE (type)))
12570 return true;
c8094d83
MS
12571 for (arg_type = TYPE_ARG_TYPES (type);
12572 arg_type;
1fb3244a
MM
12573 arg_type = TREE_CHAIN (arg_type))
12574 if (dependent_type_p (TREE_VALUE (arg_type)))
12575 return true;
12576 return false;
12577 }
12578 /* -- an array type constructed from any dependent type or whose
0cbd7506 12579 size is specified by a constant expression that is
1fb3244a
MM
12580 value-dependent. */
12581 if (TREE_CODE (type) == ARRAY_TYPE)
12582 {
12583 if (TYPE_DOMAIN (type)
c8094d83 12584 && ((value_dependent_expression_p
1fb3244a
MM
12585 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
12586 || (type_dependent_expression_p
12587 (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
12588 return true;
12589 return dependent_type_p (TREE_TYPE (type));
12590 }
c8094d83 12591
1fb3244a 12592 /* -- a template-id in which either the template name is a template
86306a6b
NS
12593 parameter ... */
12594 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
1fb3244a 12595 return true;
86306a6b 12596 /* ... or any of the template arguments is a dependent type or
04c06002 12597 an expression that is type-dependent or value-dependent. */
86306a6b 12598 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
c8094d83 12599 && (any_dependent_template_arguments_p
7e99327d 12600 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
1fb3244a 12601 return true;
c8094d83 12602
1fb3244a
MM
12603 /* All TYPEOF_TYPEs are dependent; if the argument of the `typeof'
12604 expression is not type-dependent, then it should already been
12605 have resolved. */
12606 if (TREE_CODE (type) == TYPEOF_TYPE)
12607 return true;
c8094d83 12608
1fb3244a
MM
12609 /* The standard does not specifically mention types that are local
12610 to template functions or local classes, but they should be
12611 considered dependent too. For example:
12612
c8094d83 12613 template <int I> void f() {
0cbd7506 12614 enum E { a = I };
1fb3244a
MM
12615 S<sizeof (E)> s;
12616 }
12617
12618 The size of `E' cannot be known until the value of `I' has been
12619 determined. Therefore, `E' must be considered dependent. */
12620 scope = TYPE_CONTEXT (type);
12621 if (scope && TYPE_P (scope))
12622 return dependent_type_p (scope);
12623 else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
12624 return type_dependent_expression_p (scope);
12625
12626 /* Other types are non-dependent. */
12627 return false;
12628}
12629
5552b43c
MM
12630/* Returns TRUE if TYPE is dependent, in the sense of
12631 [temp.dep.type]. */
12632
12633bool
12634dependent_type_p (tree type)
12635{
12636 /* If there are no template parameters in scope, then there can't be
12637 any dependent types. */
12638 if (!processing_template_decl)
e7e93965
MM
12639 {
12640 /* If we are not processing a template, then nobody should be
12641 providing us with a dependent type. */
12642 gcc_assert (type);
12643 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM);
12644 return false;
12645 }
5552b43c
MM
12646
12647 /* If the type is NULL, we have not computed a type for the entity
12648 in question; in that case, the type is dependent. */
12649 if (!type)
12650 return true;
12651
12652 /* Erroneous types can be considered non-dependent. */
12653 if (type == error_mark_node)
12654 return false;
12655
12656 /* If we have not already computed the appropriate value for TYPE,
12657 do so now. */
12658 if (!TYPE_DEPENDENT_P_VALID (type))
12659 {
12660 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
12661 TYPE_DEPENDENT_P_VALID (type) = 1;
12662 }
12663
12664 return TYPE_DEPENDENT_P (type);
12665}
12666
8d83f792
MM
12667/* Returns TRUE if EXPRESSION is dependent, according to CRITERION. */
12668
12669static bool
12670dependent_scope_ref_p (tree expression, bool criterion (tree))
12671{
12672 tree scope;
12673 tree name;
12674
50bc768d 12675 gcc_assert (TREE_CODE (expression) == SCOPE_REF);
8d83f792
MM
12676
12677 if (!TYPE_P (TREE_OPERAND (expression, 0)))
12678 return true;
12679
12680 scope = TREE_OPERAND (expression, 0);
12681 name = TREE_OPERAND (expression, 1);
12682
12683 /* [temp.dep.expr]
12684
12685 An id-expression is type-dependent if it contains a
12686 nested-name-specifier that contains a class-name that names a
12687 dependent type. */
12688 /* The suggested resolution to Core Issue 2 implies that if the
12689 qualifying type is the current class, then we must peek
12690 inside it. */
c8094d83 12691 if (DECL_P (name)
8d83f792
MM
12692 && currently_open_class (scope)
12693 && !criterion (name))
12694 return false;
12695 if (dependent_type_p (scope))
12696 return true;
12697
12698 return false;
12699}
12700
20929c7f 12701/* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
fe89d797
MM
12702 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
12703 expression. */
1fb3244a 12704
14d22dd6 12705bool
1fb3244a
MM
12706value_dependent_expression_p (tree expression)
12707{
12708 if (!processing_template_decl)
12709 return false;
12710
12711 /* A name declared with a dependent type. */
7416ab02 12712 if (DECL_P (expression) && type_dependent_expression_p (expression))
1fb3244a 12713 return true;
c8094d83 12714
7416ab02
NS
12715 switch (TREE_CODE (expression))
12716 {
12717 case IDENTIFIER_NODE:
12718 /* A name that has not been looked up -- must be dependent. */
12719 return true;
12720
12721 case TEMPLATE_PARM_INDEX:
12722 /* A non-type template parm. */
12723 return true;
12724
12725 case CONST_DECL:
12726 /* A non-type template parm. */
12727 if (DECL_TEMPLATE_PARM_P (expression))
d17811fd 12728 return true;
7416ab02 12729 return false;
d36d5600 12730
7416ab02 12731 case VAR_DECL:
c8094d83 12732 /* A constant with integral or enumeration type and is initialized
0cbd7506 12733 with an expression that is value-dependent. */
7416ab02
NS
12734 if (DECL_INITIAL (expression)
12735 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
12736 && value_dependent_expression_p (DECL_INITIAL (expression)))
12737 return true;
12738 return false;
12739
12740 case DYNAMIC_CAST_EXPR:
12741 case STATIC_CAST_EXPR:
12742 case CONST_CAST_EXPR:
12743 case REINTERPRET_CAST_EXPR:
12744 case CAST_EXPR:
12745 /* These expressions are value-dependent if the type to which
0cbd7506
MS
12746 the cast occurs is dependent or the expression being casted
12747 is value-dependent. */
7416ab02
NS
12748 {
12749 tree type = TREE_TYPE (expression);
c8094d83 12750
7416ab02
NS
12751 if (dependent_type_p (type))
12752 return true;
c8094d83 12753
7416ab02
NS
12754 /* A functional cast has a list of operands. */
12755 expression = TREE_OPERAND (expression, 0);
12756 if (!expression)
12757 {
12758 /* If there are no operands, it must be an expression such
12759 as "int()". This should not happen for aggregate types
12760 because it would form non-constant expressions. */
12761 gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
c8094d83 12762
7416ab02
NS
12763 return false;
12764 }
c8094d83 12765
7416ab02
NS
12766 if (TREE_CODE (expression) == TREE_LIST)
12767 {
12768 for (; expression; expression = TREE_CHAIN (expression))
d17811fd
MM
12769 if (value_dependent_expression_p (TREE_VALUE (expression)))
12770 return true;
7416ab02
NS
12771 return false;
12772 }
c8094d83 12773
d17811fd 12774 return value_dependent_expression_p (expression);
7416ab02 12775 }
c8094d83 12776
7416ab02
NS
12777 case SIZEOF_EXPR:
12778 case ALIGNOF_EXPR:
12779 /* A `sizeof' expression is value-dependent if the operand is
0cbd7506 12780 type-dependent. */
d17811fd
MM
12781 expression = TREE_OPERAND (expression, 0);
12782 if (TYPE_P (expression))
12783 return dependent_type_p (expression);
12784 return type_dependent_expression_p (expression);
100d337a 12785
7416ab02
NS
12786 case SCOPE_REF:
12787 return dependent_scope_ref_p (expression, value_dependent_expression_p);
100d337a 12788
7416ab02
NS
12789 case COMPONENT_REF:
12790 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
12791 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
12792
12793 case CALL_EXPR:
fe89d797
MM
12794 /* A CALL_EXPR may appear in a constant expression if it is a
12795 call to a builtin function, e.g., __builtin_constant_p. All
12796 such calls are value-dependent. */
12797 return true;
7416ab02
NS
12798
12799 default:
12800 /* A constant expression is value-dependent if any subexpression is
0cbd7506 12801 value-dependent. */
1fb3244a
MM
12802 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
12803 {
7416ab02 12804 case tcc_reference:
6615c446 12805 case tcc_unary:
c8094d83 12806 return (value_dependent_expression_p
1fb3244a 12807 (TREE_OPERAND (expression, 0)));
c8094d83 12808
6615c446
JO
12809 case tcc_comparison:
12810 case tcc_binary:
c8094d83 12811 return ((value_dependent_expression_p
1fb3244a 12812 (TREE_OPERAND (expression, 0)))
c8094d83 12813 || (value_dependent_expression_p
1fb3244a 12814 (TREE_OPERAND (expression, 1))));
c8094d83 12815
6615c446 12816 case tcc_expression:
1fb3244a
MM
12817 {
12818 int i;
54e4aedb 12819 for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
14d22dd6
MM
12820 /* In some cases, some of the operands may be missing.
12821 (For example, in the case of PREDECREMENT_EXPR, the
12822 amount to increment by may be missing.) That doesn't
12823 make the expression dependent. */
12824 if (TREE_OPERAND (expression, i)
12825 && (value_dependent_expression_p
12826 (TREE_OPERAND (expression, i))))
1fb3244a
MM
12827 return true;
12828 return false;
12829 }
c8094d83 12830
6615c446 12831 default:
7416ab02 12832 break;
1fb3244a
MM
12833 }
12834 }
c8094d83 12835
1fb3244a
MM
12836 /* The expression is not value-dependent. */
12837 return false;
12838}
12839
12840/* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
12841 [temp.dep.expr]. */
12842
12843bool
3a978d72 12844type_dependent_expression_p (tree expression)
1fb3244a
MM
12845{
12846 if (!processing_template_decl)
12847 return false;
12848
7efa3e22
NS
12849 if (expression == error_mark_node)
12850 return false;
10b1d5e7
MM
12851
12852 /* An unresolved name is always dependent. */
12853 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12854 return true;
c8094d83 12855
1fb3244a
MM
12856 /* Some expression forms are never type-dependent. */
12857 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
12858 || TREE_CODE (expression) == SIZEOF_EXPR
12859 || TREE_CODE (expression) == ALIGNOF_EXPR
12860 || TREE_CODE (expression) == TYPEID_EXPR
12861 || TREE_CODE (expression) == DELETE_EXPR
12862 || TREE_CODE (expression) == VEC_DELETE_EXPR
12863 || TREE_CODE (expression) == THROW_EXPR)
12864 return false;
12865
12866 /* The types of these expressions depends only on the type to which
12867 the cast occurs. */
12868 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
12869 || TREE_CODE (expression) == STATIC_CAST_EXPR
12870 || TREE_CODE (expression) == CONST_CAST_EXPR
12871 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
12872 || TREE_CODE (expression) == CAST_EXPR)
12873 return dependent_type_p (TREE_TYPE (expression));
d17811fd 12874
1fb3244a
MM
12875 /* The types of these expressions depends only on the type created
12876 by the expression. */
d17811fd
MM
12877 if (TREE_CODE (expression) == NEW_EXPR
12878 || TREE_CODE (expression) == VEC_NEW_EXPR)
09d2f85f
KL
12879 {
12880 /* For NEW_EXPR tree nodes created inside a template, either
12881 the object type itself or a TREE_LIST may appear as the
12882 operand 1. */
12883 tree type = TREE_OPERAND (expression, 1);
12884 if (TREE_CODE (type) == TREE_LIST)
12885 /* This is an array type. We need to check array dimensions
12886 as well. */
12887 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
12888 || value_dependent_expression_p
12889 (TREE_OPERAND (TREE_VALUE (type), 1));
12890 else
12891 return dependent_type_p (type);
12892 }
1fb3244a 12893
5a57f1b2
JM
12894 if (TREE_CODE (expression) == SCOPE_REF
12895 && dependent_scope_ref_p (expression,
12896 type_dependent_expression_p))
12897 return true;
12898
12899 if (TREE_CODE (expression) == FUNCTION_DECL
12900 && DECL_LANG_SPECIFIC (expression)
12901 && DECL_TEMPLATE_INFO (expression)
12902 && (any_dependent_template_arguments_p
12903 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
12904 return true;
12905
12906 if (TREE_CODE (expression) == TEMPLATE_DECL
12907 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
12908 return false;
12909
d17811fd
MM
12910 if (TREE_TYPE (expression) == unknown_type_node)
12911 {
12912 if (TREE_CODE (expression) == ADDR_EXPR)
12913 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
c353b8e3
MM
12914 if (TREE_CODE (expression) == COMPONENT_REF
12915 || TREE_CODE (expression) == OFFSET_REF)
6cb89308
NS
12916 {
12917 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
12918 return true;
12919 expression = TREE_OPERAND (expression, 1);
12920 if (TREE_CODE (expression) == IDENTIFIER_NODE)
12921 return false;
12922 }
3601f003
KL
12923 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
12924 if (TREE_CODE (expression) == SCOPE_REF)
12925 return false;
c8094d83 12926
d17811fd
MM
12927 if (TREE_CODE (expression) == BASELINK)
12928 expression = BASELINK_FUNCTIONS (expression);
c8094d83 12929
d17811fd
MM
12930 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
12931 {
ee3071ef
NS
12932 if (any_dependent_template_arguments_p
12933 (TREE_OPERAND (expression, 1)))
d17811fd
MM
12934 return true;
12935 expression = TREE_OPERAND (expression, 0);
12936 }
b207d6e2
MM
12937 gcc_assert (TREE_CODE (expression) == OVERLOAD
12938 || TREE_CODE (expression) == FUNCTION_DECL);
c8094d83 12939
315fb5db 12940 while (expression)
d17811fd 12941 {
315fb5db
NS
12942 if (type_dependent_expression_p (OVL_CURRENT (expression)))
12943 return true;
12944 expression = OVL_NEXT (expression);
d17811fd 12945 }
315fb5db 12946 return false;
d17811fd 12947 }
c8094d83 12948
3ce5fa4f 12949 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
3db45ab5 12950
1fb3244a
MM
12951 return (dependent_type_p (TREE_TYPE (expression)));
12952}
12953
d17811fd
MM
12954/* Returns TRUE if ARGS (a TREE_LIST of arguments to a function call)
12955 contains a type-dependent expression. */
1fb3244a
MM
12956
12957bool
d17811fd
MM
12958any_type_dependent_arguments_p (tree args)
12959{
12960 while (args)
12961 {
7efa3e22
NS
12962 tree arg = TREE_VALUE (args);
12963
12964 if (type_dependent_expression_p (arg))
d17811fd
MM
12965 return true;
12966 args = TREE_CHAIN (args);
12967 }
12968 return false;
12969}
12970
12971/* Returns TRUE if the ARG (a template argument) is dependent. */
12972
12973static bool
1fb3244a
MM
12974dependent_template_arg_p (tree arg)
12975{
12976 if (!processing_template_decl)
12977 return false;
12978
12979 if (TREE_CODE (arg) == TEMPLATE_DECL
12980 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12981 return dependent_template_p (arg);
12982 else if (TYPE_P (arg))
12983 return dependent_type_p (arg);
12984 else
12985 return (type_dependent_expression_p (arg)
12986 || value_dependent_expression_p (arg));
12987}
12988
d17811fd
MM
12989/* Returns true if ARGS (a collection of template arguments) contains
12990 any dependent arguments. */
1fb3244a 12991
d17811fd
MM
12992bool
12993any_dependent_template_arguments_p (tree args)
1fb3244a 12994{
bf12d54d 12995 int i;
c353b8e3
MM
12996 int j;
12997
d17811fd
MM
12998 if (!args)
12999 return false;
7e497d0c
VR
13000 if (args == error_mark_node)
13001 return true;
d17811fd 13002
c353b8e3
MM
13003 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
13004 {
13005 tree level = TMPL_ARGS_LEVEL (args, i + 1);
13006 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
13007 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
13008 return true;
13009 }
1fb3244a 13010
1fb3244a
MM
13011 return false;
13012}
13013
13014/* Returns TRUE if the template TMPL is dependent. */
13015
13016bool
13017dependent_template_p (tree tmpl)
13018{
b95cc51a
MM
13019 if (TREE_CODE (tmpl) == OVERLOAD)
13020 {
13021 while (tmpl)
13022 {
13023 if (dependent_template_p (OVL_FUNCTION (tmpl)))
13024 return true;
13025 tmpl = OVL_CHAIN (tmpl);
13026 }
13027 return false;
13028 }
13029
1fb3244a
MM
13030 /* Template template parameters are dependent. */
13031 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
13032 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
13033 return true;
27ab0504 13034 /* So are names that have not been looked up. */
acccf788
MM
13035 if (TREE_CODE (tmpl) == SCOPE_REF
13036 || TREE_CODE (tmpl) == IDENTIFIER_NODE)
d17811fd 13037 return true;
1fb3244a
MM
13038 /* So are member templates of dependent classes. */
13039 if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
13040 return dependent_type_p (DECL_CONTEXT (tmpl));
13041 return false;
13042}
13043
d17811fd
MM
13044/* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
13045
13046bool
13047dependent_template_id_p (tree tmpl, tree args)
13048{
13049 return (dependent_template_p (tmpl)
13050 || any_dependent_template_arguments_p (args));
13051}
13052
14d22dd6
MM
13053/* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
13054 TYPENAME_TYPE corresponds. Returns ERROR_MARK_NODE if no such TYPE
13055 can be found. Note that this function peers inside uninstantiated
13056 templates and therefore should be used only in extremely limited
dda04398 13057 situations. ONLY_CURRENT_P restricts this peering to the currently
c51940a2 13058 open classes hierarchy (which is required when comparing types). */
14d22dd6
MM
13059
13060tree
13061resolve_typename_type (tree type, bool only_current_p)
13062{
13063 tree scope;
13064 tree name;
13065 tree decl;
13066 int quals;
4514aa8c 13067 tree pushed_scope;
14d22dd6 13068
50bc768d 13069 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
14d22dd6
MM
13070
13071 scope = TYPE_CONTEXT (type);
13072 name = TYPE_IDENTIFIER (type);
13073
13074 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
13075 it first before we can figure out what NAME refers to. */
13076 if (TREE_CODE (scope) == TYPENAME_TYPE)
13077 scope = resolve_typename_type (scope, only_current_p);
13078 /* If we don't know what SCOPE refers to, then we cannot resolve the
13079 TYPENAME_TYPE. */
13080 if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
13081 return error_mark_node;
13082 /* If the SCOPE is a template type parameter, we have no way of
13083 resolving the name. */
13084 if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
13085 return type;
13086 /* If the SCOPE is not the current instantiation, there's no reason
13087 to look inside it. */
13088 if (only_current_p && !currently_open_class (scope))
13089 return error_mark_node;
ca099ac8
MM
13090 /* If SCOPE is a partial instantiation, it will not have a valid
13091 TYPE_FIELDS list, so use the original template. */
353b4fc0 13092 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
14d22dd6
MM
13093 /* Enter the SCOPE so that name lookup will be resolved as if we
13094 were in the class definition. In particular, SCOPE will no
13095 longer be considered a dependent type. */
4514aa8c 13096 pushed_scope = push_scope (scope);
14d22dd6 13097 /* Look up the declaration. */
86ac0575 13098 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
14d22dd6
MM
13099 /* Obtain the set of qualifiers applied to the TYPE. */
13100 quals = cp_type_quals (type);
13101 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
13102 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
13103 if (!decl)
13104 type = error_mark_node;
13105 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
13106 && TREE_CODE (decl) == TYPE_DECL)
13107 type = TREE_TYPE (decl);
13108 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
13109 && DECL_CLASS_TEMPLATE_P (decl))
13110 {
13111 tree tmpl;
13112 tree args;
13113 /* Obtain the template and the arguments. */
13114 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
13115 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
13116 /* Instantiate the template. */
13117 type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
42eaed49 13118 /*entering_scope=*/0, tf_error | tf_user);
14d22dd6
MM
13119 }
13120 else
13121 type = error_mark_node;
13122 /* Qualify the resulting type. */
13123 if (type != error_mark_node && quals)
13124 type = cp_build_qualified_type (type, quals);
13125 /* Leave the SCOPE. */
4514aa8c
NS
13126 if (pushed_scope)
13127 pop_scope (pushed_scope);
14d22dd6
MM
13128
13129 return type;
13130}
13131
d17811fd
MM
13132/* EXPR is an expression which is not type-dependent. Return a proxy
13133 for EXPR that can be used to compute the types of larger
13134 expressions containing EXPR. */
13135
13136tree
13137build_non_dependent_expr (tree expr)
13138{
0deb916c
MM
13139 tree inner_expr;
13140
c8094d83 13141 /* Preserve null pointer constants so that the type of things like
d17811fd
MM
13142 "p == 0" where "p" is a pointer can be determined. */
13143 if (null_ptr_cst_p (expr))
13144 return expr;
13145 /* Preserve OVERLOADs; the functions must be available to resolve
13146 types. */
c497db75
AO
13147 inner_expr = expr;
13148 if (TREE_CODE (inner_expr) == ADDR_EXPR)
13149 inner_expr = TREE_OPERAND (inner_expr, 0);
13150 if (TREE_CODE (inner_expr) == COMPONENT_REF)
13151 inner_expr = TREE_OPERAND (inner_expr, 1);
2226e997 13152 if (is_overloaded_fn (inner_expr)
6439fffd 13153 || TREE_CODE (inner_expr) == OFFSET_REF)
d17811fd 13154 return expr;
5ae9ba3e
MM
13155 /* There is no need to return a proxy for a variable. */
13156 if (TREE_CODE (expr) == VAR_DECL)
13157 return expr;
7433e6d4
MM
13158 /* Preserve string constants; conversions from string constants to
13159 "char *" are allowed, even though normally a "const char *"
13160 cannot be used to initialize a "char *". */
13161 if (TREE_CODE (expr) == STRING_CST)
13162 return expr;
b7c707d1
MM
13163 /* Preserve arithmetic constants, as an optimization -- there is no
13164 reason to create a new node. */
13165 if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
13166 return expr;
9b7be7b5
MM
13167 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
13168 There is at least one place where we want to know that a
13169 particular expression is a throw-expression: when checking a ?:
13170 expression, there are special rules if the second or third
878cbb73 13171 argument is a throw-expression. */
9b7be7b5
MM
13172 if (TREE_CODE (expr) == THROW_EXPR)
13173 return expr;
47d4c811
NS
13174
13175 if (TREE_CODE (expr) == COND_EXPR)
f293ce4b
RS
13176 return build3 (COND_EXPR,
13177 TREE_TYPE (expr),
13178 TREE_OPERAND (expr, 0),
c8094d83 13179 (TREE_OPERAND (expr, 1)
f293ce4b
RS
13180 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
13181 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
13182 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
8e1daa34
NS
13183 if (TREE_CODE (expr) == COMPOUND_EXPR
13184 && !COMPOUND_EXPR_OVERLOADED (expr))
f293ce4b
RS
13185 return build2 (COMPOUND_EXPR,
13186 TREE_TYPE (expr),
13187 TREE_OPERAND (expr, 0),
13188 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
c8094d83 13189
fdeff563
NS
13190 /* If the type is unknown, it can't really be non-dependent */
13191 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
3db45ab5 13192
c8094d83 13193 /* Otherwise, build a NON_DEPENDENT_EXPR.
d17811fd
MM
13194
13195 REFERENCE_TYPEs are not stripped for expressions in templates
13196 because doing so would play havoc with mangling. Consider, for
13197 example:
13198
c8094d83 13199 template <typename T> void f<T& g>() { g(); }
d17811fd
MM
13200
13201 In the body of "f", the expression for "g" will have
13202 REFERENCE_TYPE, even though the standard says that it should
13203 not. The reason is that we must preserve the syntactic form of
13204 the expression so that mangling (say) "f<g>" inside the body of
13205 "f" works out correctly. Therefore, the REFERENCE_TYPE is
13206 stripped here. */
018a5803 13207 return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
d17811fd
MM
13208}
13209
13210/* ARGS is a TREE_LIST of expressions as arguments to a function call.
13211 Return a new TREE_LIST with the various arguments replaced with
13212 equivalent non-dependent expressions. */
13213
13214tree
13215build_non_dependent_args (tree args)
13216{
13217 tree a;
13218 tree new_args;
13219
13220 new_args = NULL_TREE;
13221 for (a = args; a; a = TREE_CHAIN (a))
c8094d83 13222 new_args = tree_cons (NULL_TREE,
d17811fd
MM
13223 build_non_dependent_expr (TREE_VALUE (a)),
13224 new_args);
13225 return nreverse (new_args);
13226}
13227
e2500fed 13228#include "gt-cp-pt.h"
This page took 5.909519 seconds and 5 git commands to generate.