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