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