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