]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/pt.c
Makefile.in (cppexp.o): Depend on cpphash.h.
[gcc.git] / gcc / cp / pt.c
CommitLineData
8d08fdba 1/* Handle parameterized types (templates) for GNU C++.
4c571114 2 Copyright (C) 1992, 93-97, 1998, 1999 Free Software Foundation, Inc.
8d08fdba 3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
fc378698 4 Rewritten by Jason Merrill (jason@cygnus.com).
8d08fdba
MS
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba
MS
22
23/* Known bugs or deficiencies include:
e92cc029 24
e92cc029
MS
25 all methods must be provided in header files; can't use a source
26 file that contains only the method templates and "just win". */
8d08fdba
MS
27
28#include "config.h"
8d052bc7 29#include "system.h"
8d08fdba
MS
30#include "obstack.h"
31
32#include "tree.h"
33#include "flags.h"
34#include "cp-tree.h"
35#include "decl.h"
36#include "parse.h"
f0e01782 37#include "lex.h"
e8abc66f 38#include "output.h"
a9aedbc2 39#include "defaults.h"
49c249e1 40#include "except.h"
54f92bfb 41#include "toplev.h"
3dcaad8b 42#include "rtl.h"
091f9839 43#include "defaults.h"
9cd64686 44#include "ggc.h"
49c249e1 45
050367a3
MM
46/* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48typedef int (*tree_fn_t) PROTO((tree, void*));
49
8d08fdba 50extern struct obstack permanent_obstack;
8d08fdba
MS
51
52extern int lineno;
53extern char *input_filename;
8d08fdba 54
0aafb128
MM
55/* The PENDING_TEMPLATES is a TREE_LIST of templates whose
56 instantiations have been deferred, either because their definitions
57 were not yet available, or because we were putting off doing the
58 work. The TREE_PURPOSE of each entry is a SRCLOC indicating where
59 the instantiate request occurred; the TREE_VALUE is a either a DECL
60 (for a function or static data member), or a TYPE (for a class)
61 indicating what we are hoping to instantiate. */
62static tree pending_templates;
5566b478
MS
63static tree *template_tail = &pending_templates;
64
0aafb128 65static tree maybe_templates;
73aad9b9
JM
66static tree *maybe_template_tail = &maybe_templates;
67
67ffc812 68int processing_template_parmlist;
386b8a85
JM
69static int template_header_count;
70
75650646 71static tree saved_trees;
3dcaad8b
MM
72static varray_type inline_parm_levels;
73static size_t inline_parm_levels_used;
75650646 74
8d08fdba
MS
75#define obstack_chunk_alloc xmalloc
76#define obstack_chunk_free free
77
830bfa74
MM
78#define UNIFY_ALLOW_NONE 0
79#define UNIFY_ALLOW_MORE_CV_QUAL 1
80#define UNIFY_ALLOW_LESS_CV_QUAL 2
81#define UNIFY_ALLOW_DERIVED 4
161c12b0 82#define UNIFY_ALLOW_INTEGER 8
830bfa74 83
4393e105
MM
84#define GTB_VIA_VIRTUAL 1 /* The base class we are examining is
85 virtual, or a base class of a virtual
86 base. */
87#define GTB_IGNORE_TYPE 2 /* We don't need to try to unify the current
88 type with the desired type. */
89
34016c81 90static int resolve_overloaded_unification PROTO((tree, tree, tree, tree,
4393e105 91 unification_kind_t, int));
e97e5263 92static int try_one_overload PROTO((tree, tree, tree, tree, tree,
4393e105
MM
93 unification_kind_t, int));
94static int unify PROTO((tree, tree, tree, tree, int));
49c249e1
JM
95static void add_pending_template PROTO((tree));
96static int push_tinst_level PROTO((tree));
97static tree classtype_mangled_name PROTO((tree));
36a117a5 98static char *mangle_class_name_for_template PROTO((char *, tree, tree));
49c249e1 99static tree tsubst_expr_values PROTO((tree, tree));
49c249e1 100static int list_eq PROTO((tree, tree));
36a117a5 101static tree get_class_bindings PROTO((tree, tree, tree));
744fac59 102static tree coerce_template_parms PROTO((tree, tree, tree, int, int));
dbfe2124 103static void tsubst_enum PROTO((tree, tree, tree));
98c1c668 104static tree add_to_template_args PROTO((tree, tree));
36a117a5 105static tree add_outermost_template_args PROTO((tree, tree));
830bfa74
MM
106static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
107 tree*));
050367a3 108static int type_unification_real PROTO((tree, tree, tree, tree,
4393e105 109 int, unification_kind_t, int));
386b8a85 110static void note_template_header PROTO((int));
840b09b4 111static tree maybe_fold_nontype_arg PROTO((tree));
e1467ff2 112static tree convert_nontype_argument PROTO((tree, tree));
8b5b8b7c
MM
113static tree convert_template_argument PROTO ((tree, tree, tree, int,
114 int , tree));
76b9a14d 115static tree get_bindings_overload PROTO((tree, tree, tree));
050367a3 116static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
4b580446 117static tree build_template_parm_index PROTO((int, int, int, tree, tree));
69ac77ce
JL
118static int inline_needs_template_parms PROTO((tree));
119static void push_inline_template_parms_recursive PROTO((tree, int));
120static tree retrieve_specialization PROTO((tree, tree));
9188c363 121static tree retrieve_local_specialization PROTO((tree, tree));
36a117a5 122static tree register_specialization PROTO((tree, tree, tree));
9188c363 123static tree register_local_specialization PROTO((tree, tree, tree));
36a117a5 124static int unregister_specialization PROTO((tree, tree));
69ac77ce
JL
125static tree reduce_template_parm_level PROTO((tree, tree, int));
126static tree build_template_decl PROTO((tree, tree));
127static int mark_template_parm PROTO((tree, void *));
128static tree tsubst_friend_function PROTO((tree, tree));
6757edfe 129static tree tsubst_friend_class PROTO((tree, tree));
69ac77ce 130static tree get_bindings_real PROTO((tree, tree, tree, int));
04a81d94 131static int template_decl_level PROTO((tree));
a2b60a0e 132static tree maybe_get_template_decl_from_type_decl PROTO((tree));
830bfa74 133static int check_cv_quals_for_unify PROTO((int, tree, tree));
4393e105
MM
134static tree tsubst_template_arg_vector PROTO((tree, tree, int));
135static tree tsubst_template_parms PROTO((tree, tree, int));
830bfa74 136static void regenerate_decl_from_template PROTO((tree, tree));
36a117a5
MM
137static tree most_specialized PROTO((tree, tree, tree));
138static tree most_specialized_class PROTO((tree, tree));
139static tree most_general_template PROTO((tree));
140static void set_mangled_name_for_template_decl PROTO((tree));
39c01e4c 141static int template_class_depth_real PROTO((tree, int));
4393e105 142static tree tsubst_aggr_type PROTO((tree, tree, int, tree, int));
ae58fa02 143static tree tsubst_decl PROTO((tree, tree, tree, tree));
4393e105
MM
144static tree tsubst_arg_types PROTO((tree, tree, int, tree));
145static tree tsubst_function_type PROTO((tree, tree, int, tree));
6c30752f
MM
146static void check_specialization_scope PROTO((void));
147static tree process_partial_specialization PROTO((tree));
61a127b3 148static void set_current_access_from_decl PROTO((tree));
6ba89f8e 149static void check_default_tmpl_args PROTO((tree, tree, int, int));
4393e105
MM
150static tree tsubst_call_declarator_parms PROTO((tree, tree, int, tree));
151static tree get_template_base_recursive PROTO((tree, tree,
152 tree, tree, tree, int));
00aeceec 153static tree get_template_base PROTO((tree, tree, tree, tree));
4393e105
MM
154static tree try_class_unification PROTO((tree, tree, tree, tree));
155static int coerce_template_template_parms PROTO((tree, tree, int,
156 tree, tree));
bf8f3f93 157static tree determine_specialization PROTO((tree, tree, tree *, int));
d8e178a0
KG
158static int template_args_equal PROTO((tree, tree));
159static void print_template_context PROTO((int));
9188c363 160static void tsubst_default_arguments PROTO((tree));
36a117a5
MM
161
162/* We use TREE_VECs to hold template arguments. If there is only one
163 level of template arguments, then the TREE_VEC contains the
164 arguments directly. If there is more than one level of template
165 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
166 containing the template arguments for a single level. The first
167 entry in the outer TREE_VEC is the outermost level of template
168 parameters; the last is the innermost.
169
170 It is incorrect to ever form a template argument vector containing
171 only one level of arguments, but which is a TREE_VEC containing as
172 its only entry the TREE_VEC for that level. */
173
36a117a5
MM
174/* Non-zero if the template arguments is actually a vector of vectors,
175 rather than just a vector. */
176#define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
177 (NODE != NULL_TREE \
178 && TREE_CODE (NODE) == TREE_VEC \
179 && TREE_VEC_LENGTH (NODE) > 0 \
180 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
67ffc812
MM
181 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
182
e4a84209
MM
183/* The depth of a template argument vector. When called directly by
184 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
185 template arguments. In fact, we may even see NULL_TREE if there
186 are no template arguments. In both of those cases, there is only
187 one level of template arguments. */
188#define TMPL_ARGS_DEPTH(NODE) \
189 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
190
36a117a5
MM
191/* The LEVELth level of the template ARGS. Note that template
192 parameter levels are indexed from 1, not from 0. */
193#define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
194 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
195 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
196
197/* Set the LEVELth level of the template ARGS to VAL. This macro does
198 not work with single-level argument vectors. */
199#define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
200 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
201
202/* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
203#define TMPL_ARG(ARGS, LEVEL, IDX) \
204 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
205
206/* Set the IDXth element in the LEVELth level of ARGS to VAL. This
207 macro does not work with single-level argument vectors. */
208#define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
209 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
210
e4a84209
MM
211/* Given a single level of template arguments in NODE, return the
212 number of arguments. */
213#define NUM_TMPL_ARGS(NODE) \
214 ((NODE) == NULL_TREE ? 0 \
215 : (TREE_CODE (NODE) == TREE_VEC \
216 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
217
36a117a5
MM
218/* The number of levels of template parameters given by NODE. */
219#define TMPL_PARMS_DEPTH(NODE) \
220 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
221
9cd64686
MM
222/* Called once to initialize pt.c. */
223
224void
225init_pt ()
226{
227 ggc_add_tree_root (&pending_templates, 1);
228 ggc_add_tree_root (&maybe_templates, 1);
229 ggc_add_tree_root (&saved_trees, 1);
230}
231
e1467ff2
MM
232/* Do any processing required when DECL (a member template declaration
233 using TEMPLATE_PARAMETERS as its innermost parameter list) is
234 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
235 it is a specialization, in which case the DECL itself is returned. */
236
237tree
61a127b3 238finish_member_template_decl (decl)
e1467ff2
MM
239 tree decl;
240{
93cdc044
JM
241 if (decl == NULL_TREE || decl == void_type_node)
242 return NULL_TREE;
3ddfb0e6
MM
243 else if (decl == error_mark_node)
244 /* By returning NULL_TREE, the parser will just ignore this
245 declaration. We have already issued the error. */
246 return NULL_TREE;
93cdc044
JM
247 else if (TREE_CODE (decl) == TREE_LIST)
248 {
5f311aec 249 /* Assume that the class is the only declspec. */
93cdc044 250 decl = TREE_VALUE (decl);
8d019cef
JM
251 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
252 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
93cdc044
JM
253 {
254 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
255 check_member_template (tmpl);
256 return tmpl;
257 }
8d019cef 258 return NULL_TREE;
93cdc044 259 }
a1da6cba 260 else if (DECL_TEMPLATE_INFO (decl))
e1467ff2 261 {
a1da6cba
MM
262 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
263 {
264 check_member_template (DECL_TI_TEMPLATE (decl));
265 return DECL_TI_TEMPLATE (decl);
266 }
267 else
268 return decl;
269 }
270 else
8251199e 271 cp_error ("invalid member template declaration `%D'", decl);
e1467ff2 272
a1da6cba 273 return error_mark_node;
f84b4be9 274}
e1467ff2 275
f84b4be9
JM
276/* Returns the template nesting level of the indicated class TYPE.
277
278 For example, in:
279 template <class T>
280 struct A
281 {
282 template <class U>
283 struct B {};
284 };
285
39c01e4c
MM
286 A<T>::B<U> has depth two, while A<T> has depth one.
287 Both A<T>::B<int> and A<int>::B<U> have depth one, if
288 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
289 specializations.
290
291 This function is guaranteed to return 0 if passed NULL_TREE so
292 that, for example, `template_class_depth (current_class_type)' is
293 always safe. */
f84b4be9 294
e9659ab0 295static int
39c01e4c 296template_class_depth_real (type, count_specializations)
f84b4be9 297 tree type;
39c01e4c 298 int count_specializations;
f84b4be9 299{
93cdc044 300 int depth;
f84b4be9 301
2c73f9f5 302 for (depth = 0;
ed44da02
MM
303 type && TREE_CODE (type) != NAMESPACE_DECL;
304 type = (TREE_CODE (type) == FUNCTION_DECL)
305 ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type))
306 {
307 if (TREE_CODE (type) != FUNCTION_DECL)
308 {
309 if (CLASSTYPE_TEMPLATE_INFO (type)
310 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
311 && ((count_specializations
312 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
313 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
314 ++depth;
315 }
316 else
317 {
318 if (DECL_TEMPLATE_INFO (type)
319 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
320 && ((count_specializations
321 && DECL_TEMPLATE_SPECIALIZATION (type))
322 || uses_template_parms (DECL_TI_ARGS (type))))
323 ++depth;
324 }
325 }
f84b4be9
JM
326
327 return depth;
e1467ff2 328}
98c1c668 329
39c01e4c
MM
330/* Returns the template nesting level of the indicated class TYPE.
331 Like template_class_depth_real, but instantiations do not count in
332 the depth. */
333
334int
335template_class_depth (type)
336 tree type;
337{
338 return template_class_depth_real (type, /*count_specializations=*/0);
339}
340
cae40af6
JM
341/* Returns 1 if processing DECL as part of do_pending_inlines
342 needs us to push template parms. */
343
344static int
345inline_needs_template_parms (decl)
346 tree decl;
347{
348 if (! DECL_TEMPLATE_INFO (decl))
349 return 0;
f84b4be9 350
36a117a5 351 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
cae40af6
JM
352 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
353}
354
355/* Subroutine of maybe_begin_member_template_processing.
356 Push the template parms in PARMS, starting from LEVELS steps into the
357 chain, and ending at the beginning, since template parms are listed
358 innermost first. */
359
360static void
361push_inline_template_parms_recursive (parmlist, levels)
362 tree parmlist;
363 int levels;
364{
365 tree parms = TREE_VALUE (parmlist);
366 int i;
367
368 if (levels > 1)
369 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
786b5245 370
98c1c668 371 ++processing_template_decl;
cae40af6 372 current_template_parms
98c1c668
JM
373 = tree_cons (build_int_2 (0, processing_template_decl),
374 parms, current_template_parms);
cae40af6
JM
375 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
376
786b5245 377 pushlevel (0);
98c1c668
JM
378 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
379 {
786b5245
MM
380 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
381 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
cae40af6 382
98c1c668
JM
383 switch (TREE_CODE (parm))
384 {
786b5245 385 case TYPE_DECL:
73b0fce8 386 case TEMPLATE_DECL:
98c1c668
JM
387 pushdecl (parm);
388 break;
786b5245
MM
389
390 case PARM_DECL:
391 {
fc03edb3
MM
392 /* Make a CONST_DECL as is done in process_template_parm.
393 It is ugly that we recreate this here; the original
394 version built in process_template_parm is no longer
395 available. */
786b5245
MM
396 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
397 TREE_TYPE (parm));
fc03edb3 398 SET_DECL_ARTIFICIAL (decl);
786b5245 399 DECL_INITIAL (decl) = DECL_INITIAL (parm);
fc03edb3 400 DECL_TEMPLATE_PARM_P (decl) = 1;
786b5245
MM
401 pushdecl (decl);
402 }
cae40af6 403 break;
786b5245 404
98c1c668
JM
405 default:
406 my_friendly_abort (0);
407 }
408 }
409}
410
cae40af6
JM
411/* Restore the template parameter context for a member template or
412 a friend template defined in a class definition. */
413
414void
415maybe_begin_member_template_processing (decl)
416 tree decl;
417{
418 tree parms;
3dcaad8b 419 int levels = 0;
cae40af6 420
3dcaad8b
MM
421 if (inline_needs_template_parms (decl))
422 {
423 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
424 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
cae40af6 425
3dcaad8b
MM
426 if (DECL_TEMPLATE_SPECIALIZATION (decl))
427 {
428 --levels;
429 parms = TREE_CHAIN (parms);
430 }
cae40af6 431
3dcaad8b 432 push_inline_template_parms_recursive (parms, levels);
cae40af6
JM
433 }
434
3dcaad8b
MM
435 /* Remember how many levels of template parameters we pushed so that
436 we can pop them later. */
437 if (!inline_parm_levels)
438 VARRAY_INT_INIT (inline_parm_levels, 4, "inline_parm_levels");
439 if (inline_parm_levels_used == inline_parm_levels->num_elements)
440 VARRAY_GROW (inline_parm_levels, 2 * inline_parm_levels_used);
441 VARRAY_INT (inline_parm_levels, inline_parm_levels_used) = levels;
442 ++inline_parm_levels_used;
cae40af6
JM
443}
444
98c1c668
JM
445/* Undo the effects of begin_member_template_processing. */
446
447void
b370501f 448maybe_end_member_template_processing ()
98c1c668 449{
3dcaad8b
MM
450 int i;
451
452 if (!inline_parm_levels_used)
98c1c668
JM
453 return;
454
3dcaad8b
MM
455 --inline_parm_levels_used;
456 for (i = 0;
457 i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
458 ++i)
cae40af6
JM
459 {
460 --processing_template_decl;
461 current_template_parms = TREE_CHAIN (current_template_parms);
462 poplevel (0, 0, 0);
463 }
98c1c668
JM
464}
465
cae40af6 466/* Returns non-zero iff T is a member template function. We must be
f84b4be9 467 careful as in
75650646
MM
468
469 template <class T> class C { void f(); }
470
471 Here, f is a template function, and a member, but not a member
472 template. This function does not concern itself with the origin of
473 T, only its present state. So if we have
474
475 template <class T> class C { template <class U> void f(U); }
476
477 then neither C<int>::f<char> nor C<T>::f<double> is considered
f181d4ae
MM
478 to be a member template. But, `template <class U> void
479 C<int>::f(U)' is considered a member template. */
98c1c668 480
cae40af6
JM
481int
482is_member_template (t)
98c1c668
JM
483 tree t;
484{
f181d4ae 485 if (!DECL_FUNCTION_TEMPLATE_P (t))
75650646 486 /* Anything that isn't a function or a template function is
aa5f3bad
MM
487 certainly not a member template. */
488 return 0;
489
cae40af6
JM
490 /* A local class can't have member templates. */
491 if (hack_decl_function_context (t))
492 return 0;
493
f181d4ae 494 return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
f84b4be9
JM
495 /* If there are more levels of template parameters than
496 there are template classes surrounding the declaration,
497 then we have a member template. */
f181d4ae
MM
498 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
499 template_class_depth (DECL_CLASS_CONTEXT (t))));
f84b4be9 500}
98c1c668 501
ba4f4e5d 502#if 0 /* UNUSED */
2ee4e084
MM
503/* Returns non-zero iff T is a member template class. See
504 is_member_template for a description of what precisely constitutes
505 a member template. */
506
507int
508is_member_template_class (t)
509 tree t;
510{
511 if (!DECL_CLASS_TEMPLATE_P (t))
512 /* Anything that isn't a class template, is certainly not a member
513 template. */
514 return 0;
515
516 if (!DECL_CLASS_SCOPE_P (t))
517 /* Anything whose context isn't a class type is surely not a
518 member template. */
519 return 0;
520
521 /* If there are more levels of template parameters than there are
522 template classes surrounding the declaration, then we have a
523 member template. */
36a117a5 524 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
2ee4e084
MM
525 template_class_depth (DECL_CONTEXT (t)));
526}
ba4f4e5d 527#endif
2ee4e084 528
36a117a5
MM
529/* Return a new template argument vector which contains all of ARGS,
530 but has as its innermost set of arguments the EXTRA_ARGS. The
531 resulting vector will be built on a temporary obstack, and so must
532 be explicitly copied to the permanent obstack, if required. */
e6f1275f
JM
533
534static tree
36a117a5
MM
535add_to_template_args (args, extra_args)
536 tree args;
537 tree extra_args;
e6f1275f 538{
36a117a5
MM
539 tree new_args;
540 int extra_depth;
541 int i;
542 int j;
e6f1275f 543
36a117a5
MM
544 extra_depth = TMPL_ARGS_DEPTH (extra_args);
545 new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
e6f1275f 546
36a117a5
MM
547 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
548 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
e6f1275f 549
36a117a5
MM
550 for (j = 1; j <= extra_depth; ++j, ++i)
551 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
552
e6f1275f
JM
553 return new_args;
554}
555
36a117a5
MM
556/* Like add_to_template_args, but only the outermost ARGS are added to
557 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
558 (EXTRA_ARGS) levels are added. This function is used to combine
559 the template arguments from a partial instantiation with the
560 template arguments used to attain the full instantiation from the
561 partial instantiation. */
98c1c668 562
4966381a 563static tree
36a117a5 564add_outermost_template_args (args, extra_args)
98c1c668
JM
565 tree args;
566 tree extra_args;
567{
568 tree new_args;
569
e4a84209
MM
570 /* If there are more levels of EXTRA_ARGS than there are ARGS,
571 something very fishy is going on. */
572 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
573 0);
574
575 /* If *all* the new arguments will be the EXTRA_ARGS, just return
576 them. */
577 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
578 return extra_args;
579
36a117a5
MM
580 /* For the moment, we make ARGS look like it contains fewer levels. */
581 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
582
583 new_args = add_to_template_args (args, extra_args);
98c1c668 584
36a117a5
MM
585 /* Now, we restore ARGS to its full dimensions. */
586 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
98c1c668
JM
587
588 return new_args;
589}
5566b478
MS
590
591/* We've got a template header coming up; push to a new level for storing
592 the parms. */
8d08fdba 593
8d08fdba
MS
594void
595begin_template_parm_list ()
596{
6757edfe
MM
597 /* We use a non-tag-transparent scope here, which causes pushtag to
598 put tags in this scope, rather than in the enclosing class or
599 namespace scope. This is the right thing, since we want
600 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
601 global template class, push_template_decl handles putting the
602 TEMPLATE_DECL into top-level scope. For a nested template class,
603 e.g.:
604
605 template <class T> struct S1 {
606 template <class T> struct S2 {};
607 };
608
609 pushtag contains special code to call pushdecl_with_scope on the
610 TEMPLATE_DECL for S2. */
8d08fdba 611 pushlevel (0);
5566b478 612 declare_pseudo_global_level ();
5156628f 613 ++processing_template_decl;
67ffc812 614 ++processing_template_parmlist;
386b8a85
JM
615 note_template_header (0);
616}
617
6c30752f
MM
618/* This routine is called when a specialization is declared. If it is
619 illegal to declare a specialization here, an error is reported. */
620
e9659ab0 621static void
6c30752f
MM
622check_specialization_scope ()
623{
624 tree scope = current_scope ();
3ddfb0e6 625
6c30752f
MM
626 /* [temp.expl.spec]
627
628 An explicit specialization shall be declared in the namespace of
629 which the template is a member, or, for member templates, in the
630 namespace of which the enclosing class or enclosing class
631 template is a member. An explicit specialization of a member
632 function, member class or static data member of a class template
633 shall be declared in the namespace of which the class template
634 is a member. */
635 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
8251199e 636 cp_error ("explicit specialization in non-namespace scope `%D'",
6c30752f 637 scope);
3ddfb0e6 638
6c30752f
MM
639 /* [temp.expl.spec]
640
641 In an explicit specialization declaration for a member of a class
642 template or a member template that appears in namespace scope,
643 the member template and some of its enclosing class templates may
644 remain unspecialized, except that the declaration shall not
645 explicitly specialize a class member template if its enclosing
646 class templates are not explicitly specialized as well. */
647 if (current_template_parms)
fd4de5ff 648 cp_error ("enclosing class templates are not explicitly specialized");
6c30752f
MM
649}
650
386b8a85
JM
651/* We've just seen template <>. */
652
653void
654begin_specialization ()
655{
656 note_template_header (1);
6c30752f 657 check_specialization_scope ();
386b8a85
JM
658}
659
386b8a85
JM
660/* Called at then end of processing a declaration preceeded by
661 template<>. */
662
663void
664end_specialization ()
665{
666 reset_specialization ();
667}
668
386b8a85
JM
669/* Any template <>'s that we have seen thus far are not referring to a
670 function specialization. */
671
672void
673reset_specialization ()
674{
675 processing_specialization = 0;
676 template_header_count = 0;
677}
678
386b8a85
JM
679/* We've just seen a template header. If SPECIALIZATION is non-zero,
680 it was of the form template <>. */
681
4966381a 682static void
386b8a85
JM
683note_template_header (specialization)
684 int specialization;
685{
686 processing_specialization = specialization;
687 template_header_count++;
688}
689
75650646 690/* We're beginning an explicit instantiation. */
386b8a85 691
75650646
MM
692void
693begin_explicit_instantiation ()
386b8a85 694{
75650646
MM
695 ++processing_explicit_instantiation;
696}
386b8a85 697
386b8a85 698
75650646
MM
699void
700end_explicit_instantiation ()
701{
702 my_friendly_assert(processing_explicit_instantiation > 0, 0);
703 --processing_explicit_instantiation;
704}
386b8a85 705
36a117a5
MM
706/* The TYPE is being declared. If it is a template type, that means it
707 is a partial specialization. Do appropriate error-checking. */
708
709void
710maybe_process_partial_specialization (type)
711 tree type;
712{
713 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
714 {
715 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
716 && TYPE_SIZE (type) == NULL_TREE)
717 {
700466c2
JM
718 if (current_namespace
719 != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
720 {
721 cp_pedwarn ("specializing `%#T' in different namespace", type);
722 cp_pedwarn_at (" from definition of `%#D'",
723 CLASSTYPE_TI_TEMPLATE (type));
724 }
36a117a5
MM
725 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
726 if (processing_template_decl)
727 push_template_decl (TYPE_MAIN_DECL (type));
728 }
729 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
8251199e 730 cp_error ("specialization of `%T' after instantiation", type);
36a117a5 731 }
20496fa2
JM
732 else if (processing_specialization)
733 cp_error ("explicit specialization of non-template `%T'", type);
36a117a5
MM
734}
735
75650646
MM
736/* Retrieve the specialization (in the sense of [temp.spec] - a
737 specialization is either an instantiation or an explicit
738 specialization) of TMPL for the given template ARGS. If there is
739 no such specialization, return NULL_TREE. The ARGS are a vector of
740 arguments, or a vector of vectors of arguments, in the case of
741 templates with more than one level of parameters. */
742
743static tree
744retrieve_specialization (tmpl, args)
745 tree tmpl;
746 tree args;
747{
748 tree s;
749
750 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
751
36a117a5
MM
752 /* There should be as many levels of arguments as there are
753 levels of parameters. */
754 my_friendly_assert (TMPL_ARGS_DEPTH (args)
755 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
756 0);
757
75650646
MM
758 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
759 s != NULL_TREE;
760 s = TREE_CHAIN (s))
761 if (comp_template_args (TREE_PURPOSE (s), args))
762 return TREE_VALUE (s);
763
764 return NULL_TREE;
386b8a85
JM
765}
766
9188c363
MM
767/* Like retrieve_speciailization, but for local declarations. FN is
768 the function in which we are looking for an instantiation. */
769
770static tree
771retrieve_local_specialization (tmpl, fn)
772 tree tmpl;
773 tree fn;
774{
775 tree s = purpose_member (fn, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
776 return s ? TREE_VALUE (s) : NULL_TREE;
777}
778
6757edfe
MM
779/* Returns non-zero iff DECL is a specialization of TMPL. */
780
781int
782is_specialization_of (decl, tmpl)
783 tree decl;
784 tree tmpl;
785{
786 tree t;
787
788 if (TREE_CODE (decl) == FUNCTION_DECL)
789 {
790 for (t = decl;
791 t != NULL_TREE;
792 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
793 if (t == tmpl)
794 return 1;
795 }
796 else
797 {
798 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
799
800 for (t = TREE_TYPE (decl);
801 t != NULL_TREE;
802 t = CLASSTYPE_USE_TEMPLATE (t)
803 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
3bfdc719
MM
804 if (same_type_p (TYPE_MAIN_VARIANT (t),
805 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl))))
6757edfe
MM
806 return 1;
807 }
386b8a85 808
6757edfe
MM
809 return 0;
810}
75650646
MM
811
812/* Register the specialization SPEC as a specialization of TMPL with
36a117a5
MM
813 the indicated ARGS. Returns SPEC, or an equivalent prior
814 declaration, if available. */
75650646 815
36a117a5 816static tree
75650646
MM
817register_specialization (spec, tmpl, args)
818 tree spec;
819 tree tmpl;
820 tree args;
821{
822 tree s;
823
824 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
825
36a117a5
MM
826 if (TREE_CODE (spec) == FUNCTION_DECL
827 && uses_template_parms (DECL_TI_ARGS (spec)))
828 /* This is the FUNCTION_DECL for a partial instantiation. Don't
829 register it; we want the corresponding TEMPLATE_DECL instead.
830 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
831 the more obvious `uses_template_parms (spec)' to avoid problems
832 with default function arguments. In particular, given
833 something like this:
834
835 template <class T> void f(T t1, T t = T())
836
837 the default argument expression is not substituted for in an
838 instantiation unless and until it is actually needed. */
839 return spec;
75650646 840
36a117a5
MM
841 /* There should be as many levels of arguments as there are
842 levels of parameters. */
843 my_friendly_assert (TMPL_ARGS_DEPTH (args)
844 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
845 0);
846
75650646
MM
847 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
848 s != NULL_TREE;
849 s = TREE_CHAIN (s))
22e9174f
MM
850 {
851 tree fn = TREE_VALUE (s);
852
853 /* We can sometimes try to re-register a specialization that we've
854 already got. In particular, regenerate_decl_from_template
855 calls duplicate_decls which will update the specialization
856 list. But, we'll still get called again here anyhow. It's
857 more convenient to simply allow this than to try to prevent it. */
858 if (fn == spec)
859 return spec;
860 else if (comp_template_args (TREE_PURPOSE (s), args))
861 {
862 if (DECL_TEMPLATE_SPECIALIZATION (spec))
863 {
864 if (DECL_TEMPLATE_INSTANTIATION (fn))
865 {
866 if (TREE_USED (fn)
867 || DECL_EXPLICIT_INSTANTIATION (fn))
868 {
869 cp_error ("specialization of %D after instantiation",
870 fn);
871 return spec;
872 }
873 else
874 {
875 /* This situation should occur only if the first
876 specialization is an implicit instantiation,
877 the second is an explicit specialization, and
878 the implicit instantiation has not yet been
879 used. That situation can occur if we have
880 implicitly instantiated a member function and
881 then specialized it later.
882
883 We can also wind up here if a friend
884 declaration that looked like an instantiation
885 turns out to be a specialization:
886
887 template <class T> void foo(T);
888 class S { friend void foo<>(int) };
889 template <> void foo(int);
890
891 We transform the existing DECL in place so that
892 any pointers to it become pointers to the
893 updated declaration.
894
895 If there was a definition for the template, but
896 not for the specialization, we want this to
897 look as if there is no definition, and vice
898 versa. */
899 DECL_INITIAL (fn) = NULL_TREE;
900 duplicate_decls (spec, fn);
901
902 return fn;
903 }
904 }
905 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
906 {
907 duplicate_decls (spec, fn);
908 return fn;
909 }
910 }
911 }
75650646
MM
912 }
913
914 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
915 = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
36a117a5
MM
916
917 return spec;
918}
919
920/* Unregister the specialization SPEC as a specialization of TMPL.
921 Returns nonzero if the SPEC was listed as a specialization of
922 TMPL. */
923
924static int
925unregister_specialization (spec, tmpl)
926 tree spec;
927 tree tmpl;
928{
929 tree* s;
930
931 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
932 *s != NULL_TREE;
933 s = &TREE_CHAIN (*s))
934 if (TREE_VALUE (*s) == spec)
935 {
936 *s = TREE_CHAIN (*s);
937 return 1;
938 }
939
940 return 0;
75650646
MM
941}
942
9188c363
MM
943/* Like register_specialization, but for local declarations. FN is
944 the function in which we are registering SPEC, an instantiation of
945 TMPL. */
946
947static tree
948register_local_specialization (spec, tmpl, fn)
949 tree spec;
950 tree tmpl;
951 tree fn;
952{
953 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
954 = perm_tree_cons (fn, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
955
956 return spec;
957}
958
e1467ff2
MM
959/* Print the list of candidate FNS in an error message. */
960
104bf76a 961void
e1467ff2
MM
962print_candidates (fns)
963 tree fns;
964{
965 tree fn;
966
d8e178a0 967 const char *str = "candidates are:";
e1467ff2
MM
968
969 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
970 {
d6479fe7
MM
971 tree f;
972
973 for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
974 cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
e1467ff2
MM
975 str = " ";
976 }
977}
978
75650646 979/* Returns the template (one of the functions given by TEMPLATE_ID)
e1467ff2 980 which can be specialized to match the indicated DECL with the
03017874
MM
981 explicit template args given in TEMPLATE_ID. The DECL may be
982 NULL_TREE if none is available. In that case, the functions in
983 TEMPLATE_ID are non-members.
984
985 If NEED_MEMBER_TEMPLATE is non-zero the function is known to be a
986 specialization of a member template.
987
988 The template args (those explicitly specified and those deduced)
989 are output in a newly created vector *TARGS_OUT.
990
991 If it is impossible to determine the result, an error message is
bf8f3f93 992 issued. The error_mark_node is returned to indicate failure. */
75650646 993
e9659ab0 994static tree
e1467ff2 995determine_specialization (template_id, decl, targs_out,
bf8f3f93 996 need_member_template)
386b8a85 997 tree template_id;
e1467ff2 998 tree decl;
386b8a85
JM
999 tree* targs_out;
1000 int need_member_template;
386b8a85 1001{
03017874
MM
1002 tree fns;
1003 tree targs;
1004 tree explicit_targs;
1005 tree candidates = NULL_TREE;
1006 tree templates = NULL_TREE;
386b8a85 1007
e1467ff2
MM
1008 *targs_out = NULL_TREE;
1009
f2e48b67
BK
1010 if (template_id == error_mark_node)
1011 return error_mark_node;
1012
1013 fns = TREE_OPERAND (template_id, 0);
03017874 1014 explicit_targs = TREE_OPERAND (template_id, 1);
f2e48b67 1015
27fafc8d
JM
1016 if (fns == error_mark_node)
1017 return error_mark_node;
1018
2c73f9f5 1019 /* Check for baselinks. */
91e490ab 1020 if (BASELINK_P (fns))
2c73f9f5 1021 fns = TREE_VALUE (fns);
386b8a85 1022
91e490ab
MM
1023 if (!is_overloaded_fn (fns))
1024 {
9dfce8fd 1025 cp_error ("`%D' is not a function template", fns);
91e490ab
MM
1026 return error_mark_node;
1027 }
1028
2c73f9f5 1029 for (; fns; fns = OVL_NEXT (fns))
386b8a85 1030 {
75650646
MM
1031 tree tmpl;
1032
9dfce8fd 1033 tree fn = OVL_CURRENT (fns);
03017874
MM
1034
1035 if (TREE_CODE (fn) == TEMPLATE_DECL)
1036 /* DECL might be a specialization of FN. */
1037 tmpl = fn;
1038 else if (need_member_template)
1039 /* FN is an ordinary member function, and we need a
1040 specialization of a member template. */
1041 continue;
1042 else if (TREE_CODE (fn) != FUNCTION_DECL)
1043 /* We can get IDENTIFIER_NODEs here in certain erroneous
1044 cases. */
1045 continue;
1046 else if (!DECL_FUNCTION_MEMBER_P (fn))
1047 /* This is just an ordinary non-member function. Nothing can
1048 be a specialization of that. */
1049 continue;
75650646 1050 else
03017874
MM
1051 {
1052 tree decl_arg_types;
386b8a85 1053
03017874
MM
1054 /* This is an ordinary member function. However, since
1055 we're here, we can assume it's enclosing class is a
1056 template class. For example,
1057
1058 template <typename T> struct S { void f(); };
1059 template <> void S<int>::f() {}
1060
1061 Here, S<int>::f is a non-template, but S<int> is a
1062 template class. If FN has the same type as DECL, we
1063 might be in business. */
1064 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
1065 TREE_TYPE (TREE_TYPE (fn))))
1066 /* The return types differ. */
1067 continue;
1068
1069 /* Adjust the type of DECL in case FN is a static member. */
1070 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1071 if (DECL_STATIC_FUNCTION_P (fn)
1072 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1073 decl_arg_types = TREE_CHAIN (decl_arg_types);
1074
1075 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
1076 decl_arg_types))
1077 /* They match! */
1078 candidates = tree_cons (NULL_TREE, fn, candidates);
1079
1080 continue;
1081 }
386b8a85 1082
bf8f3f93
MM
1083 /* See whether this function might be a specialization of this
1084 template. */
1085 targs = get_bindings (tmpl, decl, explicit_targs);
03017874
MM
1086
1087 if (!targs)
bf8f3f93 1088 /* We cannot deduce template arguments that when used to
03017874
MM
1089 specialize TMPL will produce DECL. */
1090 continue;
1091
1092 /* Save this template, and the arguments deduced. */
1093 templates = scratch_tree_cons (targs, tmpl, templates);
386b8a85 1094 }
03017874 1095
bf8f3f93 1096 if (templates && TREE_CHAIN (templates))
386b8a85 1097 {
03017874
MM
1098 /* We have:
1099
1100 [temp.expl.spec]
1101
1102 It is possible for a specialization with a given function
1103 signature to be instantiated from more than one function
1104 template. In such cases, explicit specification of the
1105 template arguments must be used to uniquely identify the
1106 function template specialization being specialized.
1107
1108 Note that here, there's no suggestion that we're supposed to
1109 determine which of the candidate templates is most
1110 specialized. However, we, also have:
1111
1112 [temp.func.order]
1113
1114 Partial ordering of overloaded function template
1115 declarations is used in the following contexts to select
1116 the function template to which a function template
1117 specialization refers:
1118
1119 -- when an explicit specialization refers to a function
1120 template.
1121
1122 So, we do use the partial ordering rules, at least for now.
1123 This extension can only serve to make illegal programs legal,
1124 so it's safe. And, there is strong anecdotal evidence that
1125 the committee intended the partial ordering rules to apply;
1126 the EDG front-end has that behavior, and John Spicer claims
1127 that the committee simply forgot to delete the wording in
1128 [temp.expl.spec]. */
1129 tree tmpl = most_specialized (templates, decl, explicit_targs);
1130 if (tmpl && tmpl != error_mark_node)
1131 {
1132 targs = get_bindings (tmpl, decl, explicit_targs);
1133 templates = scratch_tree_cons (targs, tmpl, NULL_TREE);
1134 }
e1467ff2
MM
1135 }
1136
03017874 1137 if (templates == NULL_TREE && candidates == NULL_TREE)
e1467ff2 1138 {
bf8f3f93
MM
1139 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
1140 template_id, decl);
03017874 1141 return error_mark_node;
386b8a85 1142 }
03017874 1143 else if ((templates && TREE_CHAIN (templates))
bf8f3f93
MM
1144 || (candidates && TREE_CHAIN (candidates))
1145 || (templates && candidates))
386b8a85 1146 {
bf8f3f93
MM
1147 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
1148 template_id, decl);
1149 chainon (candidates, templates);
1150 print_candidates (candidates);
03017874 1151 return error_mark_node;
386b8a85
JM
1152 }
1153
1154 /* We have one, and exactly one, match. */
03017874
MM
1155 if (candidates)
1156 {
1157 /* It was a specialization of an ordinary member function in a
1158 template class. */
1159 *targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
1160 return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
1161 }
1162
1163 /* It was a specialization of a template. */
1164 targs = DECL_TI_ARGS (DECL_RESULT (TREE_VALUE (templates)));
1165 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
1166 {
1167 *targs_out = copy_node (targs);
1168 SET_TMPL_ARGS_LEVEL (*targs_out,
1169 TMPL_ARGS_DEPTH (*targs_out),
1170 TREE_PURPOSE (templates));
1171 }
1172 else
1173 *targs_out = TREE_PURPOSE (templates);
e1467ff2 1174 return TREE_VALUE (templates);
8d08fdba 1175}
6757edfe 1176
386b8a85 1177/* Check to see if the function just declared, as indicated in
75650646
MM
1178 DECLARATOR, and in DECL, is a specialization of a function
1179 template. We may also discover that the declaration is an explicit
1180 instantiation at this point.
1181
e1467ff2 1182 Returns DECL, or an equivalent declaration that should be used
03017874
MM
1183 instead if all goes well. Issues an error message if something is
1184 amiss. Returns error_mark_node if the error is not easily
1185 recoverable.
75650646 1186
75650646
MM
1187 FLAGS is a bitmask consisting of the following flags:
1188
75650646
MM
1189 2: The function has a definition.
1190 4: The function is a friend.
75650646
MM
1191
1192 The TEMPLATE_COUNT is the number of references to qualifying
1193 template classes that appeared in the name of the function. For
1194 example, in
1195
1196 template <class T> struct S { void f(); };
1197 void S<int>::f();
1198
1199 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1200 classes are not counted in the TEMPLATE_COUNT, so that in
1201
1202 template <class T> struct S {};
1203 template <> struct S<int> { void f(); }
36a117a5 1204 template <> void S<int>::f();
75650646
MM
1205
1206 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1207 illegal; there should be no template <>.)
1208
1209 If the function is a specialization, it is marked as such via
1210 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1211 is set up correctly, and it is added to the list of specializations
1212 for that template. */
386b8a85 1213
e1467ff2 1214tree
27bb8339 1215check_explicit_specialization (declarator, decl, template_count, flags)
386b8a85
JM
1216 tree declarator;
1217 tree decl;
1218 int template_count;
1219 int flags;
1220{
75650646
MM
1221 int have_def = flags & 2;
1222 int is_friend = flags & 4;
1223 int specialization = 0;
e1467ff2 1224 int explicit_instantiation = 0;
fd4de5ff 1225 int member_specialization = 0;
75650646
MM
1226
1227 tree ctype = DECL_CLASS_CONTEXT (decl);
1228 tree dname = DECL_NAME (decl);
386b8a85 1229
fd4de5ff 1230 if (processing_specialization)
386b8a85 1231 {
fd4de5ff 1232 /* The last template header was of the form template <>. */
75650646 1233
fd4de5ff
MM
1234 if (template_header_count > template_count)
1235 {
1236 /* There were more template headers than qualifying template
1237 classes. */
1238 if (template_header_count - template_count > 1)
03017874
MM
1239 /* There shouldn't be that many template parameter lists.
1240 There can be at most one parameter list for every
1241 qualifying class, plus one for the function itself. */
fd4de5ff 1242 cp_error ("too many template parameter lists in declaration of `%D'", decl);
75650646 1243
fd4de5ff
MM
1244 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1245 if (ctype)
1246 member_specialization = 1;
1247 else
1248 specialization = 1;
75650646 1249 }
fd4de5ff 1250 else if (template_header_count == template_count)
75650646 1251 {
fd4de5ff
MM
1252 /* The counts are equal. So, this might be a
1253 specialization, but it is not a specialization of a
1254 member template. It might be something like
1255
1256 template <class T> struct S {
1257 void f(int i);
1258 };
1259 template <>
1260 void S<int>::f(int i) {} */
1261 specialization = 1;
1262 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
75650646 1263 }
fd4de5ff 1264 else
386b8a85 1265 {
fd4de5ff
MM
1266 /* This cannot be an explicit specialization. There are not
1267 enough headers for all of the qualifying classes. For
1268 example, we might have:
1269
1270 template <>
1271 void S<int>::T<char>::f();
75650646 1272
fd4de5ff
MM
1273 But, we're missing another template <>. */
1274 cp_error("too few template parameter lists in declaration of `%D'", decl);
1275 return decl;
1276 }
1277 }
1278 else if (processing_explicit_instantiation)
1279 {
1280 if (template_header_count)
1281 cp_error ("template parameter list used in explicit instantiation");
1282
1283 if (have_def)
1284 cp_error ("definition provided for explicit instantiation");
75650646 1285
fd4de5ff
MM
1286 explicit_instantiation = 1;
1287 }
1288 else if (ctype != NULL_TREE
1289 && !TYPE_BEING_DEFINED (ctype)
1290 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1291 && !is_friend)
1292 {
1293 /* This case catches outdated code that looks like this:
1294
1295 template <class T> struct S { void f(); };
1296 void S<int>::f() {} // Missing template <>
1297
1298 We disable this check when the type is being defined to
1299 avoid complaining about default compiler-generated
1300 constructors, destructors, and assignment operators.
1301 Since the type is an instantiation, not a specialization,
1302 these are the only functions that can be defined before
1303 the class is complete. */
75650646 1304
c6f2ed0d
JM
1305 /* If they said
1306 template <class T> void S<int>::f() {}
1307 that's bogus. */
fd4de5ff
MM
1308 if (template_header_count)
1309 {
1310 cp_error ("template parameters specified in specialization");
1311 return decl;
c6f2ed0d 1312 }
fd4de5ff
MM
1313
1314 if (pedantic)
1315 cp_pedwarn
1316 ("explicit specialization not preceded by `template <>'");
1317 specialization = 1;
1318 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1319 }
1320 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1321 {
1322 if (is_friend)
1323 /* This could be something like:
1324
1325 template <class T> void f(T);
1326 class S { friend void f<>(int); } */
1327 specialization = 1;
1328 else
c6f2ed0d 1329 {
fd4de5ff
MM
1330 /* This case handles bogus declarations like template <>
1331 template <class T> void f<int>(); */
75650646 1332
fd4de5ff
MM
1333 cp_error ("template-id `%D' in declaration of primary template",
1334 declarator);
1335 return decl;
386b8a85 1336 }
75650646 1337 }
386b8a85 1338
670960ac
JM
1339 if (specialization || member_specialization)
1340 {
1341 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1342 for (; t; t = TREE_CHAIN (t))
1343 if (TREE_PURPOSE (t))
1344 {
8251199e
JM
1345 cp_pedwarn
1346 ("default argument specified in explicit specialization");
670960ac
JM
1347 break;
1348 }
f3400fe2
JM
1349 if (current_lang_name == lang_name_c)
1350 cp_error ("template specialization with C linkage");
670960ac
JM
1351 }
1352
e1467ff2 1353 if (specialization || member_specialization || explicit_instantiation)
75650646
MM
1354 {
1355 tree tmpl = NULL_TREE;
1356 tree targs = NULL_TREE;
75650646
MM
1357
1358 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
386b8a85
JM
1359 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1360 {
1361 tree fns;
1362
1363 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1364 0);
1365 if (!ctype)
30394414 1366 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
386b8a85
JM
1367 else
1368 fns = dname;
1369
75650646
MM
1370 declarator =
1371 lookup_template_function (fns, NULL_TREE);
386b8a85
JM
1372 }
1373
f2e48b67
BK
1374 if (declarator == error_mark_node)
1375 return error_mark_node;
1376
75650646
MM
1377 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1378 {
8ec2ac34 1379 if (!explicit_instantiation)
6c30752f
MM
1380 /* A specialization in class scope. This is illegal,
1381 but the error will already have been flagged by
1382 check_specialization_scope. */
1383 return error_mark_node;
8ec2ac34 1384 else
b370501f
KG
1385 {
1386 /* It's not legal to write an explicit instantiation in
1387 class scope, e.g.:
8ec2ac34 1388
b370501f 1389 class C { template void f(); }
8ec2ac34 1390
b370501f
KG
1391 This case is caught by the parser. However, on
1392 something like:
8ec2ac34 1393
b370501f 1394 template class C { void f(); };
8ec2ac34 1395
b370501f
KG
1396 (which is illegal) we can get here. The error will be
1397 issued later. */
1398 ;
1399 }
8ec2ac34 1400
e1467ff2 1401 return decl;
75650646 1402 }
8f032717
MM
1403 else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1404 {
1405 /* A friend declaration. We can't do much, because we don't
1406 know what this resolves to, yet. */
1407 my_friendly_assert (is_friend != 0, 0);
1408 my_friendly_assert (!explicit_instantiation, 0);
1409 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1410 return decl;
1411 }
75650646
MM
1412 else if (ctype != NULL_TREE
1413 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1414 IDENTIFIER_NODE))
386b8a85 1415 {
75650646
MM
1416 /* Find the list of functions in ctype that have the same
1417 name as the declared function. */
1418 tree name = TREE_OPERAND (declarator, 0);
03017874
MM
1419 tree fns = NULL_TREE;
1420 int idx;
1421
75650646
MM
1422 if (name == constructor_name (ctype)
1423 || name == constructor_name_full (ctype))
386b8a85 1424 {
75650646
MM
1425 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1426
1427 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1428 : !TYPE_HAS_DESTRUCTOR (ctype))
1429 {
1430 /* From [temp.expl.spec]:
e1467ff2 1431
75650646
MM
1432 If such an explicit specialization for the member
1433 of a class template names an implicitly-declared
1434 special member function (clause _special_), the
e1467ff2
MM
1435 program is ill-formed.
1436
1437 Similar language is found in [temp.explicit]. */
8251199e 1438 cp_error ("specialization of implicitly-declared special member function");
03017874 1439 return error_mark_node;
75650646 1440 }
386b8a85 1441
42da2fd8 1442 name = is_constructor ? ctor_identifier : dtor_identifier;
75650646 1443 }
42da2fd8 1444
03017874
MM
1445 if (!IDENTIFIER_TYPENAME_P (name))
1446 {
1447 idx = lookup_fnfields_1 (ctype, name);
1448 if (idx >= 0)
1449 fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
1450 }
1451 else
1452 {
1453 tree methods;
1454
1455 /* For a type-conversion operator, we cannot do a
1456 name-based lookup. We might be looking for `operator
1457 int' which will be a specialization of `operator T'.
1458 So, we find *all* the conversion operators, and then
1459 select from them. */
1460 fns = NULL_TREE;
1461
1462 methods = CLASSTYPE_METHOD_VEC (ctype);
1463 if (methods)
1464 for (idx = 2; idx < TREE_VEC_LENGTH (methods); ++idx)
1465 {
1466 tree ovl = TREE_VEC_ELT (methods, idx);
1467
1468 if (!ovl || !DECL_CONV_FN_P (OVL_CURRENT (ovl)))
1469 /* There are no more conversion functions. */
1470 break;
1471
1472 /* Glue all these conversion functions together
1473 with those we already have. */
1474 for (; ovl; ovl = OVL_NEXT (ovl))
1475 fns = ovl_cons (OVL_CURRENT (ovl), fns);
1476 }
1477 }
1478
386b8a85
JM
1479 if (fns == NULL_TREE)
1480 {
b3f738da
MM
1481 cp_error ("no member function `%D' declared in `%T'",
1482 name, ctype);
03017874 1483 return error_mark_node;
386b8a85
JM
1484 }
1485 else
1486 TREE_OPERAND (declarator, 0) = fns;
1487 }
75650646 1488
e1467ff2
MM
1489 /* Figure out what exactly is being specialized at this point.
1490 Note that for an explicit instantiation, even one for a
38e01259 1491 member function, we cannot tell apriori whether the
e1467ff2 1492 instantiation is for a member template, or just a member
36a117a5
MM
1493 function of a template class. Even if a member template is
1494 being instantiated, the member template arguments may be
1495 elided if they can be deduced from the rest of the
1496 declaration. */
e1467ff2
MM
1497 tmpl = determine_specialization (declarator, decl,
1498 &targs,
bf8f3f93 1499 member_specialization);
386b8a85 1500
03017874
MM
1501 if (!tmpl || tmpl == error_mark_node)
1502 /* We couldn't figure out what this declaration was
1503 specializing. */
1504 return error_mark_node;
1505 else
386b8a85 1506 {
25aab5d0 1507 tree gen_tmpl = most_general_template (tmpl);
36a117a5 1508
e1467ff2
MM
1509 if (explicit_instantiation)
1510 {
03d0f4af 1511 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
25aab5d0
MM
1512 is done by do_decl_instantiation later. */
1513
1514 int arg_depth = TMPL_ARGS_DEPTH (targs);
1515 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
1516
1517 if (arg_depth > parm_depth)
1518 {
1519 /* If TMPL is not the most general template (for
1520 example, if TMPL is a friend template that is
1521 injected into namespace scope), then there will
1522 be too many levels fo TARGS. Remove some of them
1523 here. */
1524 int i;
1525 tree new_targs;
1526
1527 new_targs = make_temp_vec (parm_depth);
1528 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
1529 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
1530 = TREE_VEC_ELT (targs, i);
1531 targs = new_targs;
1532 }
1533
1534 decl = instantiate_template (tmpl, targs);
e1467ff2
MM
1535 return decl;
1536 }
36a117a5
MM
1537
1538 /* If we though that the DECL was a member function, but it
1539 turns out to be specializing a static member function,
1540 make DECL a static member function as well. */
1541 if (DECL_STATIC_FUNCTION_P (tmpl)
1542 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
366c0f1e
MM
1543 {
1544 revert_static_member_fn (&decl, 0, 0);
1545 last_function_parms = TREE_CHAIN (last_function_parms);
1546 }
e1467ff2 1547
36a117a5
MM
1548 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1549 DECL_TEMPLATE_INFO (decl)
1550 = perm_tree_cons (tmpl, targs, NULL_TREE);
1551
e1467ff2
MM
1552 /* Mangle the function name appropriately. Note that we do
1553 not mangle specializations of non-template member
1554 functions of template classes, e.g. with
36a117a5 1555
e1467ff2 1556 template <class T> struct S { void f(); }
36a117a5 1557
e1467ff2 1558 and given the specialization
36a117a5 1559
e1467ff2 1560 template <> void S<int>::f() {}
36a117a5 1561
e1467ff2
MM
1562 we do not mangle S<int>::f() here. That's because it's
1563 just an ordinary member function and doesn't need special
36a117a5
MM
1564 treatment. We do this here so that the ordinary,
1565 non-template, name-mangling algorith will not be used
1566 later. */
e1467ff2 1567 if ((is_member_template (tmpl) || ctype == NULL_TREE)
75650646 1568 && name_mangling_version >= 1)
36a117a5 1569 set_mangled_name_for_template_decl (decl);
386b8a85
JM
1570
1571 if (is_friend && !have_def)
36a117a5
MM
1572 /* This is not really a declaration of a specialization.
1573 It's just the name of an instantiation. But, it's not
1574 a request for an instantiation, either. */
fbf1c34b 1575 SET_DECL_IMPLICIT_INSTANTIATION (decl);
75650646 1576
36a117a5
MM
1577 /* Register this specialization so that we can find it
1578 again. */
1579 decl = register_specialization (decl, gen_tmpl, targs);
386b8a85
JM
1580 }
1581 }
75650646 1582
e1467ff2 1583 return decl;
386b8a85 1584}
75650646 1585
39c01e4c
MM
1586/* TYPE is being declared. Verify that the use of template headers
1587 and such is reasonable. Issue error messages if not. */
1588
1589void
1590maybe_check_template_type (type)
1591 tree type;
1592{
1593 if (template_header_count)
1594 {
1595 /* We are in the scope of some `template <...>' header. */
1596
1597 int context_depth
1598 = template_class_depth_real (TYPE_CONTEXT (type),
1599 /*count_specializations=*/1);
1600
1601 if (template_header_count <= context_depth)
1602 /* This is OK; the template headers are for the context. We
1603 are actually too lenient here; like
1604 check_explicit_specialization we should consider the number
1605 of template types included in the actual declaration. For
1606 example,
1607
1608 template <class T> struct S {
1609 template <class U> template <class V>
1610 struct I {};
1611 };
1612
1613 is illegal, but:
1614
1615 template <class T> struct S {
1616 template <class U> struct I;
1617 };
1618
1619 template <class T> template <class U.
1620 struct S<T>::I {};
1621
1622 is not. */
1623 ;
1624 else if (template_header_count > context_depth + 1)
1625 /* There are two many template parameter lists. */
8251199e 1626 cp_error ("too many template parameter lists in declaration of `%T'", type);
39c01e4c
MM
1627 }
1628}
1629
75650646
MM
1630/* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1631 parameters. These are represented in the same format used for
1632 DECL_TEMPLATE_PARMS. */
1633
1634int comp_template_parms (parms1, parms2)
1635 tree parms1;
1636 tree parms2;
1637{
1638 tree p1;
1639 tree p2;
1640
1641 if (parms1 == parms2)
1642 return 1;
1643
1644 for (p1 = parms1, p2 = parms2;
1645 p1 != NULL_TREE && p2 != NULL_TREE;
1646 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1647 {
1648 tree t1 = TREE_VALUE (p1);
1649 tree t2 = TREE_VALUE (p2);
1650 int i;
1651
1652 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1653 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1654
1655 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1656 return 0;
1657
1658 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1659 {
1660 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1661 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1662
1663 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1664 return 0;
1665
1666 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1667 continue;
3bfdc719 1668 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
75650646
MM
1669 return 0;
1670 }
1671 }
1672
1673 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1674 /* One set of parameters has more parameters lists than the
1675 other. */
1676 return 0;
1677
1678 return 1;
1679}
1680
f3400fe2
JM
1681/* Complain if DECL shadows a template parameter.
1682
1683 [temp.local]: A template-parameter shall not be redeclared within its
1684 scope (including nested scopes). */
1685
1686void
1687check_template_shadow (decl)
1688 tree decl;
1689{
8f032717
MM
1690 tree olddecl;
1691
b5d9b9ab
MM
1692 /* If we're not in a template, we can't possibly shadow a template
1693 parameter. */
1694 if (!current_template_parms)
1695 return;
1696
1697 /* Figure out what we're shadowing. */
8f032717
MM
1698 if (TREE_CODE (decl) == OVERLOAD)
1699 decl = OVL_CURRENT (decl);
1700 olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
f3400fe2 1701
b5d9b9ab
MM
1702 /* If there's no previous binding for this name, we're not shadowing
1703 anything, let alone a template parameter. */
1704 if (!olddecl)
1705 return;
1706
1707 /* If we're not shadowing a template parameter, we're done. Note
1708 that OLDDECL might be an OVERLOAD (or perhaps even an
1709 ERROR_MARK), so we can't just blithely assume it to be a _DECL
1710 node. */
1711 if (TREE_CODE_CLASS (TREE_CODE (olddecl)) != 'd'
1712 || !DECL_TEMPLATE_PARM_P (olddecl))
1713 return;
1714
1715 /* We check for decl != olddecl to avoid bogus errors for using a
1716 name inside a class. We check TPFI to avoid duplicate errors for
1717 inline member templates. */
1718 if (decl == olddecl
1719 || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
1720 return;
1721
1722 cp_error_at ("declaration of `%#D'", decl);
1723 cp_error_at (" shadows template parm `%#D'", olddecl);
f3400fe2 1724}
22a7be53 1725
f3400fe2 1726/* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
f84b4be9
JM
1727 ORIG_LEVEL, DECL, and TYPE. */
1728
1729static tree
1730build_template_parm_index (index, level, orig_level, decl, type)
1731 int index;
1732 int level;
1733 int orig_level;
1734 tree decl;
1735 tree type;
1736{
1737 tree t = make_node (TEMPLATE_PARM_INDEX);
1738 TEMPLATE_PARM_IDX (t) = index;
1739 TEMPLATE_PARM_LEVEL (t) = level;
1740 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1741 TEMPLATE_PARM_DECL (t) = decl;
1742 TREE_TYPE (t) = type;
1743
1744 return t;
1745}
1746
f84b4be9 1747/* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
93cdc044 1748 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
f84b4be9
JM
1749 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1750 new one is created. */
1751
1752static tree
93cdc044 1753reduce_template_parm_level (index, type, levels)
f84b4be9
JM
1754 tree index;
1755 tree type;
93cdc044 1756 int levels;
f84b4be9
JM
1757{
1758 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1759 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
93cdc044 1760 != TEMPLATE_PARM_LEVEL (index) - levels))
f84b4be9
JM
1761 {
1762 tree decl
1763 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1764 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1765 type);
1766 tree t
1767 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
93cdc044 1768 TEMPLATE_PARM_LEVEL (index) - levels,
f84b4be9
JM
1769 TEMPLATE_PARM_ORIG_LEVEL (index),
1770 decl, type);
1771 TEMPLATE_PARM_DESCENDANTS (index) = t;
cae40af6
JM
1772
1773 /* Template template parameters need this. */
1774 DECL_TEMPLATE_PARMS (decl)
1775 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
f84b4be9
JM
1776 }
1777
1778 return TEMPLATE_PARM_DESCENDANTS (index);
1779}
1780
8d08fdba 1781/* Process information from new template parameter NEXT and append it to the
5566b478 1782 LIST being built. */
e92cc029 1783
8d08fdba
MS
1784tree
1785process_template_parm (list, next)
1786 tree list, next;
1787{
1788 tree parm;
1789 tree decl = 0;
a292b002 1790 tree defval;
5566b478 1791 int is_type, idx;
f84b4be9 1792
8d08fdba
MS
1793 parm = next;
1794 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
a292b002
MS
1795 defval = TREE_PURPOSE (parm);
1796 parm = TREE_VALUE (parm);
1797 is_type = TREE_PURPOSE (parm) == class_type_node;
5566b478
MS
1798
1799 if (list)
1800 {
1801 tree p = TREE_VALUE (tree_last (list));
1802
1803 if (TREE_CODE (p) == TYPE_DECL)
1804 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
73b0fce8
KL
1805 else if (TREE_CODE (p) == TEMPLATE_DECL)
1806 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
5566b478 1807 else
f84b4be9 1808 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
5566b478
MS
1809 ++idx;
1810 }
1811 else
1812 idx = 0;
1813
8d08fdba
MS
1814 if (!is_type)
1815 {
a292b002 1816 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
8d08fdba 1817 /* is a const-param */
a292b002 1818 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
c11b6f21 1819 PARM, 0, NULL_TREE);
d490621d
MM
1820
1821 /* [temp.param]
1822
1823 The top-level cv-qualifiers on the template-parameter are
1824 ignored when determining its type. */
1825 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
1826
8d08fdba
MS
1827 /* A template parameter is not modifiable. */
1828 TREE_READONLY (parm) = 1;
c91a56d2 1829 if (IS_AGGR_TYPE (TREE_TYPE (parm))
410c2fba
KL
1830 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
1831 && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
8d08fdba 1832 {
8251199e 1833 cp_error ("`%#T' is not a valid type for a template constant parameter",
c91a56d2
MS
1834 TREE_TYPE (parm));
1835 if (DECL_NAME (parm) == NULL_TREE)
8251199e 1836 error (" a template type parameter must begin with `class' or `typename'");
8d08fdba
MS
1837 TREE_TYPE (parm) = void_type_node;
1838 }
37c46b43
MS
1839 else if (pedantic
1840 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
1841 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
8251199e 1842 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
eb66be0e 1843 TREE_TYPE (parm));
8d08fdba
MS
1844 if (TREE_PERMANENT (parm) == 0)
1845 {
1846 parm = copy_node (parm);
1847 TREE_PERMANENT (parm) = 1;
1848 }
8d08fdba 1849 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
f84b4be9
JM
1850 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1851 = build_template_parm_index (idx, processing_template_decl,
1852 processing_template_decl,
1853 decl, TREE_TYPE (parm));
8d08fdba
MS
1854 }
1855 else
1856 {
73b0fce8
KL
1857 tree t;
1858 parm = TREE_VALUE (parm);
1859
1860 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1861 {
1862 t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1863 /* This is for distinguishing between real templates and template
1864 template parameters */
1865 TREE_TYPE (parm) = t;
1866 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1867 decl = parm;
1868 }
1869 else
1870 {
1871 t = make_lang_type (TEMPLATE_TYPE_PARM);
1872 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1873 decl = build_decl (TYPE_DECL, parm, t);
1874 }
1875
d2e5ee5c
MS
1876 TYPE_NAME (t) = decl;
1877 TYPE_STUB_DECL (t) = decl;
a292b002 1878 parm = decl;
f84b4be9
JM
1879 TEMPLATE_TYPE_PARM_INDEX (t)
1880 = build_template_parm_index (idx, processing_template_decl,
1881 processing_template_decl,
1882 decl, TREE_TYPE (parm));
8d08fdba 1883 }
8ccc31eb 1884 SET_DECL_ARTIFICIAL (decl);
fc03edb3 1885 DECL_TEMPLATE_PARM_P (decl) = 1;
8d08fdba 1886 pushdecl (decl);
a292b002 1887 parm = build_tree_list (defval, parm);
8d08fdba
MS
1888 return chainon (list, parm);
1889}
1890
1891/* The end of a template parameter list has been reached. Process the
1892 tree list into a parameter vector, converting each parameter into a more
1893 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1894 as PARM_DECLs. */
1895
1896tree
1897end_template_parm_list (parms)
1898 tree parms;
1899{
5566b478 1900 int nparms;
8d08fdba 1901 tree parm;
5566b478
MS
1902 tree saved_parmlist = make_tree_vec (list_length (parms));
1903
5566b478
MS
1904 current_template_parms
1905 = tree_cons (build_int_2 (0, processing_template_decl),
1906 saved_parmlist, current_template_parms);
8d08fdba 1907
91e490ab
MM
1908 for (parm = parms, nparms = 0;
1909 parm;
1910 parm = TREE_CHAIN (parm), nparms++)
66191c20 1911 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
a292b002 1912
67ffc812
MM
1913 --processing_template_parmlist;
1914
8d08fdba
MS
1915 return saved_parmlist;
1916}
1917
5566b478
MS
1918/* end_template_decl is called after a template declaration is seen. */
1919
8d08fdba 1920void
5566b478 1921end_template_decl ()
8d08fdba 1922{
386b8a85
JM
1923 reset_specialization ();
1924
5156628f 1925 if (! processing_template_decl)
73aad9b9
JM
1926 return;
1927
5566b478
MS
1928 /* This matches the pushlevel in begin_template_parm_list. */
1929 poplevel (0, 0, 0);
8d08fdba 1930
5566b478
MS
1931 --processing_template_decl;
1932 current_template_parms = TREE_CHAIN (current_template_parms);
1933 (void) get_pending_sizes (); /* Why? */
1934}
8d08fdba 1935
36a117a5
MM
1936/* Given a template argument vector containing the template PARMS.
1937 The innermost PARMS are given first. */
9a3b49ac
MS
1938
1939tree
1940current_template_args ()
5566b478 1941{
36a117a5 1942 tree header;
b370501f 1943 tree args = NULL_TREE;
36a117a5 1944 int length = TMPL_PARMS_DEPTH (current_template_parms);
98c1c668
JM
1945 int l = length;
1946
36a117a5
MM
1947 /* If there is only one level of template parameters, we do not
1948 create a TREE_VEC of TREE_VECs. Instead, we return a single
1949 TREE_VEC containing the arguments. */
1950 if (length > 1)
1951 args = make_tree_vec (length);
1952
1953 for (header = current_template_parms; header; header = TREE_CHAIN (header))
8d08fdba 1954 {
5566b478 1955 tree a = copy_node (TREE_VALUE (header));
36a117a5
MM
1956 int i;
1957
5566b478 1958 TREE_TYPE (a) = NULL_TREE;
36a117a5 1959 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
5566b478 1960 {
98c1c668
JM
1961 tree t = TREE_VEC_ELT (a, i);
1962
36a117a5 1963 /* T will be a list if we are called from within a
98c1c668
JM
1964 begin/end_template_parm_list pair, but a vector directly
1965 if within a begin/end_member_template_processing pair. */
1966 if (TREE_CODE (t) == TREE_LIST)
1967 {
1968 t = TREE_VALUE (t);
1969
73b0fce8
KL
1970 if (TREE_CODE (t) == TYPE_DECL
1971 || TREE_CODE (t) == TEMPLATE_DECL)
98c1c668
JM
1972 t = TREE_TYPE (t);
1973 else
1974 t = DECL_INITIAL (t);
36a117a5 1975 TREE_VEC_ELT (a, i) = t;
98c1c668 1976 }
5566b478 1977 }
36a117a5
MM
1978
1979 if (length > 1)
1980 TREE_VEC_ELT (args, --l) = a;
1981 else
1982 args = a;
8d08fdba
MS
1983 }
1984
9a3b49ac
MS
1985 return args;
1986}
75650646 1987
e1467ff2
MM
1988/* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1989 template PARMS. Used by push_template_decl below. */
1990
75650646
MM
1991static tree
1992build_template_decl (decl, parms)
1993 tree decl;
1994 tree parms;
1995{
1996 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
1997 DECL_TEMPLATE_PARMS (tmpl) = parms;
1998 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
1999 if (DECL_LANG_SPECIFIC (decl))
2000 {
2001 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
8f032717
MM
2002 DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
2003 DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
11f98788 2004 DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
75650646
MM
2005 }
2006
2007 return tmpl;
2008}
2009
050367a3
MM
2010struct template_parm_data
2011{
6c30752f
MM
2012 /* The level of the template parameters we are currently
2013 processing. */
050367a3 2014 int level;
6c30752f
MM
2015
2016 /* The index of the specialization argument we are currently
2017 processing. */
2018 int current_arg;
2019
2020 /* An array whose size is the number of template parameters. The
2021 elements are non-zero if the parameter has been used in any one
2022 of the arguments processed so far. */
050367a3 2023 int* parms;
6c30752f
MM
2024
2025 /* An array whose size is the number of template arguments. The
2026 elements are non-zero if the argument makes use of template
2027 parameters of this level. */
2028 int* arg_uses_template_parms;
050367a3
MM
2029};
2030
2031/* Subroutine of push_template_decl used to see if each template
2032 parameter in a partial specialization is used in the explicit
2033 argument list. If T is of the LEVEL given in DATA (which is
2034 treated as a template_parm_data*), then DATA->PARMS is marked
2035 appropriately. */
2036
2037static int
2038mark_template_parm (t, data)
2039 tree t;
2040 void* data;
2041{
2042 int level;
2043 int idx;
2044 struct template_parm_data* tpd = (struct template_parm_data*) data;
2045
2046 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
2047 {
2048 level = TEMPLATE_PARM_LEVEL (t);
2049 idx = TEMPLATE_PARM_IDX (t);
2050 }
2051 else
2052 {
2053 level = TEMPLATE_TYPE_LEVEL (t);
2054 idx = TEMPLATE_TYPE_IDX (t);
2055 }
2056
2057 if (level == tpd->level)
6c30752f
MM
2058 {
2059 tpd->parms[idx] = 1;
2060 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
2061 }
050367a3
MM
2062
2063 /* Return zero so that for_each_template_parm will continue the
2064 traversal of the tree; we want to mark *every* template parm. */
2065 return 0;
2066}
2067
6c30752f
MM
2068/* Process the partial specialization DECL. */
2069
e9659ab0 2070static tree
6c30752f
MM
2071process_partial_specialization (decl)
2072 tree decl;
2073{
2074 tree type = TREE_TYPE (decl);
2075 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
2076 tree specargs = CLASSTYPE_TI_ARGS (type);
2077 tree inner_args = innermost_args (specargs);
2078 tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
2079 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
2080 int nargs = TREE_VEC_LENGTH (inner_args);
2081 int ntparms = TREE_VEC_LENGTH (inner_parms);
2082 int i;
2083 int did_error_intro = 0;
6c30752f
MM
2084 struct template_parm_data tpd;
2085 struct template_parm_data tpd2;
2086
6c30752f
MM
2087 /* We check that each of the template parameters given in the
2088 partial specialization is used in the argument list to the
2089 specialization. For example:
2090
2091 template <class T> struct S;
2092 template <class T> struct S<T*>;
2093
2094 The second declaration is OK because `T*' uses the template
2095 parameter T, whereas
2096
2097 template <class T> struct S<int>;
2098
2099 is no good. Even trickier is:
2100
2101 template <class T>
2102 struct S1
2103 {
2104 template <class U>
2105 struct S2;
2106 template <class U>
2107 struct S2<T>;
2108 };
2109
2110 The S2<T> declaration is actually illegal; it is a
2111 full-specialization. Of course,
2112
2113 template <class U>
2114 struct S2<T (*)(U)>;
2115
2116 or some such would have been OK. */
2117 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
2118 tpd.parms = alloca (sizeof (int) * ntparms);
1ddb2906 2119 bzero ((PTR) tpd.parms, sizeof (int) * ntparms);
6c30752f
MM
2120
2121 tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
1ddb2906 2122 bzero ((PTR) tpd.arg_uses_template_parms, sizeof (int) * nargs);
6c30752f
MM
2123 for (i = 0; i < nargs; ++i)
2124 {
2125 tpd.current_arg = i;
2126 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
2127 &mark_template_parm,
2128 &tpd);
2129 }
2130 for (i = 0; i < ntparms; ++i)
2131 if (tpd.parms[i] == 0)
2132 {
2133 /* One of the template parms was not used in the
2134 specialization. */
2135 if (!did_error_intro)
2136 {
8251199e 2137 cp_error ("template parameters not used in partial specialization:");
6c30752f
MM
2138 did_error_intro = 1;
2139 }
2140
8251199e 2141 cp_error (" `%D'",
6c30752f
MM
2142 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
2143 }
2144
2145 /* [temp.class.spec]
2146
2147 The argument list of the specialization shall not be identical to
2148 the implicit argument list of the primary template. */
2149 if (comp_template_args (inner_args,
2150 innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
2151 (maintmpl)))))
8251199e 2152 cp_error ("partial specialization `%T' does not specialize any template arguments", type);
6c30752f
MM
2153
2154 /* [temp.class.spec]
2155
2156 A partially specialized non-type argument expression shall not
2157 involve template parameters of the partial specialization except
2158 when the argument expression is a simple identifier.
2159
2160 The type of a template parameter corresponding to a specialized
2161 non-type argument shall not be dependent on a parameter of the
2162 specialization. */
2163 my_friendly_assert (nargs == DECL_NTPARMS (maintmpl), 0);
2164 tpd2.parms = 0;
2165 for (i = 0; i < nargs; ++i)
2166 {
2167 tree arg = TREE_VEC_ELT (inner_args, i);
2168 if (/* These first two lines are the `non-type' bit. */
2169 TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
2170 && TREE_CODE (arg) != TEMPLATE_DECL
2171 /* This next line is the `argument expression is not just a
2172 simple identifier' condition and also the `specialized
2173 non-type argument' bit. */
2174 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
2175 {
2176 if (tpd.arg_uses_template_parms[i])
8251199e 2177 cp_error ("template argument `%E' involves template parameter(s)", arg);
6c30752f
MM
2178 else
2179 {
2180 /* Look at the corresponding template parameter,
2181 marking which template parameters its type depends
2182 upon. */
2183 tree type =
2184 TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
2185 i)));
2186
2187 if (!tpd2.parms)
2188 {
2189 /* We haven't yet initialized TPD2. Do so now. */
2190 tpd2.arg_uses_template_parms
2191 = (int*) alloca (sizeof (int) * nargs);
104bf76a 2192 /* The number of parameters here is the number in the
76a83782
RH
2193 main template, which, as checked in the assertion
2194 above, is NARGS. */
6c30752f
MM
2195 tpd2.parms = (int*) alloca (sizeof (int) * nargs);
2196 tpd2.level =
2197 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
2198 }
2199
104bf76a 2200 /* Mark the template parameters. But this time, we're
6c30752f
MM
2201 looking for the template parameters of the main
2202 template, not in the specialization. */
2203 tpd2.current_arg = i;
2204 tpd2.arg_uses_template_parms[i] = 0;
1ddb2906 2205 bzero ((PTR) tpd2.parms, sizeof (int) * nargs);
6c30752f
MM
2206 for_each_template_parm (type,
2207 &mark_template_parm,
2208 &tpd2);
2209
2210 if (tpd2.arg_uses_template_parms [i])
2211 {
2212 /* The type depended on some template parameters.
2213 If they are fully specialized in the
2214 specialization, that's OK. */
2215 int j;
2216 for (j = 0; j < nargs; ++j)
2217 if (tpd2.parms[j] != 0
2218 && tpd.arg_uses_template_parms [j])
2219 {
104bf76a 2220 cp_error ("type `%T' of template argument `%E' depends on template parameter(s)",
6c30752f
MM
2221 type,
2222 arg);
2223 break;
2224 }
2225 }
2226 }
2227 }
2228 }
2229
2230 if (retrieve_specialization (maintmpl, specargs))
2231 /* We've already got this specialization. */
2232 return decl;
2233
2234 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
2235 = perm_tree_cons (inner_args, inner_parms,
2236 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
2237 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
2238 return decl;
2239}
2240
6ba89f8e
MM
2241/* Check that a template declaration's use of default arguments is not
2242 invalid. Here, PARMS are the template parameters. IS_PRIMARY is
2243 non-zero if DECL is the thing declared by a primary template.
2244 IS_PARTIAL is non-zero if DECL is a partial specialization. */
2245
2246static void
2247check_default_tmpl_args (decl, parms, is_primary, is_partial)
2248 tree decl;
2249 tree parms;
2250 int is_primary;
2251 int is_partial;
2252{
d8e178a0 2253 const char *msg;
66191c20
MM
2254 int last_level_to_check;
2255 tree parm_level;
6ba89f8e
MM
2256
2257 /* [temp.param]
2258
2259 A default template-argument shall not be specified in a
2260 function template declaration or a function template definition, nor
2261 in the template-parameter-list of the definition of a member of a
2262 class template. */
2263
e0942dcd
MM
2264 if (TREE_CODE (DECL_REAL_CONTEXT (decl)) == FUNCTION_DECL)
2265 /* You can't have a function template declaration in a local
2266 scope, nor you can you define a member of a class template in a
2267 local scope. */
2268 return;
2269
6ba89f8e
MM
2270 if (current_class_type
2271 && !TYPE_BEING_DEFINED (current_class_type)
3d7de1fa 2272 && DECL_LANG_SPECIFIC (decl)
5937a6f9
MM
2273 /* If this is either a friend defined in the scope of the class
2274 or a member function. */
2275 && DECL_CLASS_CONTEXT (decl) == current_class_type
2276 /* And, if it was a member function, it really was defined in
2277 the scope of the class. */
2278 && (!DECL_FUNCTION_MEMBER_P (decl) || DECL_DEFINED_IN_CLASS_P (decl)))
6ba89f8e 2279 /* We already checked these parameters when the template was
5937a6f9
MM
2280 declared, so there's no need to do it again now. This function
2281 was defined in class scope, but we're processing it's body now
2282 that the class is complete. */
6ba89f8e
MM
2283 return;
2284
66191c20
MM
2285 /* [temp.param]
2286
2287 If a template-parameter has a default template-argument, all
2288 subsequent template-parameters shall have a default
2289 template-argument supplied. */
2290 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
2291 {
2292 tree inner_parms = TREE_VALUE (parm_level);
2293 int ntparms = TREE_VEC_LENGTH (inner_parms);
2294 int seen_def_arg_p = 0;
2295 int i;
2296
2297 for (i = 0; i < ntparms; ++i)
2298 {
2299 tree parm = TREE_VEC_ELT (inner_parms, i);
2300 if (TREE_PURPOSE (parm))
2301 seen_def_arg_p = 1;
2302 else if (seen_def_arg_p)
2303 {
2304 cp_error ("no default argument for `%D'", TREE_VALUE (parm));
2305 /* For better subsequent error-recovery, we indicate that
2306 there should have been a default argument. */
2307 TREE_PURPOSE (parm) = error_mark_node;
2308 }
2309 }
2310 }
2311
6ba89f8e
MM
2312 if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
2313 /* For an ordinary class template, default template arguments are
2314 allowed at the innermost level, e.g.:
2315 template <class T = int>
2316 struct S {};
2317 but, in a partial specialization, they're not allowed even
2318 there, as we have in [temp.class.spec]:
2319
2320 The template parameter list of a specialization shall not
2321 contain default template argument values.
2322
2323 So, for a partial specialization, or for a function template,
2324 we look at all of them. */
2325 ;
2326 else
2327 /* But, for a primary class template that is not a partial
2328 specialization we look at all template parameters except the
2329 innermost ones. */
2330 parms = TREE_CHAIN (parms);
2331
2332 /* Figure out what error message to issue. */
2333 if (TREE_CODE (decl) == FUNCTION_DECL)
2334 msg = "default argument for template parameter in function template `%D'";
2335 else if (is_partial)
2336 msg = "default argument in partial specialization `%D'";
2337 else
2338 msg = "default argument for template parameter for class enclosing `%D'";
2339
2340 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
2341 /* If we're inside a class definition, there's no need to
104bf76a 2342 examine the parameters to the class itself. On the one
6ba89f8e
MM
2343 hand, they will be checked when the class is defined, and,
2344 on the other, default arguments are legal in things like:
2345 template <class T = double>
2346 struct S { template <class U> void f(U); };
2347 Here the default argument for `S' has no bearing on the
2348 declaration of `f'. */
2349 last_level_to_check = template_class_depth (current_class_type) + 1;
2350 else
2351 /* Check everything. */
2352 last_level_to_check = 0;
2353
66191c20
MM
2354 for (parm_level = parms;
2355 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
2356 parm_level = TREE_CHAIN (parm_level))
6ba89f8e 2357 {
66191c20
MM
2358 tree inner_parms = TREE_VALUE (parm_level);
2359 int i;
2360 int ntparms;
6ba89f8e
MM
2361
2362 ntparms = TREE_VEC_LENGTH (inner_parms);
2363 for (i = 0; i < ntparms; ++i)
2364 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
2365 {
2366 if (msg)
2367 {
2368 cp_error (msg, decl);
2369 msg = 0;
2370 }
2371
2372 /* Clear out the default argument so that we are not
2373 confused later. */
2374 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
2375 }
2376
2377 /* At this point, if we're still interested in issuing messages,
2378 they must apply to classes surrounding the object declared. */
2379 if (msg)
2380 msg = "default argument for template parameter for class enclosing `%D'";
2381 }
2382}
2383
3ac3d9ea 2384/* Creates a TEMPLATE_DECL for the indicated DECL using the template
f84b4be9
JM
2385 parameters given by current_template_args, or reuses a
2386 previously existing one, if appropriate. Returns the DECL, or an
6757edfe
MM
2387 equivalent one, if it is replaced via a call to duplicate_decls.
2388
2389 If IS_FRIEND is non-zero, DECL is a friend declaration. */
3ac3d9ea
MM
2390
2391tree
6757edfe 2392push_template_decl_real (decl, is_friend)
9a3b49ac 2393 tree decl;
6757edfe 2394 int is_friend;
9a3b49ac
MS
2395{
2396 tree tmpl;
f84b4be9 2397 tree args;
9a3b49ac 2398 tree info;
f84b4be9
JM
2399 tree ctx;
2400 int primary;
6ba89f8e 2401 int is_partial;
cfe507be 2402 int new_template_p = 0;
6ba89f8e
MM
2403
2404 /* See if this is a partial specialization. */
9188c363 2405 is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
6ba89f8e
MM
2406 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
2407 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
6757edfe
MM
2408
2409 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
f84b4be9
JM
2410
2411 if (is_friend)
2412 /* For a friend, we want the context of the friend function, not
2413 the type of which it is a friend. */
2414 ctx = DECL_CONTEXT (decl);
2c73f9f5
ML
2415 else if (DECL_REAL_CONTEXT (decl)
2416 && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
f84b4be9
JM
2417 /* In the case of a virtual function, we want the class in which
2418 it is defined. */
2419 ctx = DECL_REAL_CONTEXT (decl);
2420 else
2421 /* Otherwise, if we're currently definining some class, the DECL
2422 is assumed to be a member of the class. */
9188c363 2423 ctx = current_scope ();
f84b4be9 2424
2c73f9f5
ML
2425 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
2426 ctx = NULL_TREE;
2427
2428 if (!DECL_CONTEXT (decl))
cb0dbb9a 2429 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
2c73f9f5 2430
6ba89f8e 2431 /* See if this is a primary template. */
9188c363 2432 primary = pseudo_global_level_p ();
9a3b49ac 2433
83566abf
JM
2434 if (primary)
2435 {
2436 if (current_lang_name == lang_name_c)
8251199e 2437 cp_error ("template with C linkage");
4ce3d537
MM
2438 else if (TREE_CODE (decl) == TYPE_DECL
2439 && ANON_AGGRNAME_P (DECL_NAME (decl)))
8251199e 2440 cp_error ("template class without a name");
2aaf816d
JM
2441 else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
2442 && CLASS_TYPE_P (TREE_TYPE (decl)))
2443 || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx))
2444 || TREE_CODE (decl) == FUNCTION_DECL)
2445 /* OK */;
2446 else
4ce3d537 2447 cp_error ("template declaration of `%#D'", decl);
83566abf
JM
2448 }
2449
6ba89f8e
MM
2450 /* Check to see that the rules regarding the use of default
2451 arguments are not being violated. */
2452 check_default_tmpl_args (decl, current_template_parms,
2453 primary, is_partial);
73aad9b9 2454
6ba89f8e
MM
2455 if (is_partial)
2456 return process_partial_specialization (decl);
d32789d8 2457
9a3b49ac
MS
2458 args = current_template_args ();
2459
f84b4be9
JM
2460 if (!ctx
2461 || TREE_CODE (ctx) == FUNCTION_DECL
2462 || TYPE_BEING_DEFINED (ctx)
2463 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
8d08fdba 2464 {
75650646 2465 if (DECL_LANG_SPECIFIC (decl)
f84b4be9
JM
2466 && DECL_TEMPLATE_INFO (decl)
2467 && DECL_TI_TEMPLATE (decl))
2468 tmpl = DECL_TI_TEMPLATE (decl);
1c10870d
AS
2469 /* If DECL is a TYPE_DECL for a class-template, then there won't
2470 be DECL_LANG_SPECIFIC. The information equivalent to
2471 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
2472 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
2473 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2474 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2475 {
2476 /* Since a template declaration already existed for this
2477 class-type, we must be redeclaring it here. Make sure
2478 that the redeclaration is legal. */
2479 redeclare_class_template (TREE_TYPE (decl),
2480 current_template_parms);
2481 /* We don't need to create a new TEMPLATE_DECL; just use the
2482 one we already had. */
2483 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
2484 }
f84b4be9 2485 else
786b5245 2486 {
f84b4be9 2487 tmpl = build_template_decl (decl, current_template_parms);
cfe507be
MM
2488 new_template_p = 1;
2489
f84b4be9
JM
2490 if (DECL_LANG_SPECIFIC (decl)
2491 && DECL_TEMPLATE_SPECIALIZATION (decl))
2492 {
2493 /* A specialization of a member template of a template
2494 class. */
2495 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
2496 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
2497 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
2498 }
786b5245 2499 }
8d08fdba
MS
2500 }
2501 else
2502 {
e1a5ccf7 2503 tree a, t, current, parms;
ba4f4e5d 2504 int i;
6633d636 2505
73aad9b9 2506 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
8251199e 2507 cp_error ("must specialize `%#T' before defining member `%#D'",
73aad9b9 2508 ctx, decl);
6b9ab5cc
MM
2509 if (TREE_CODE (decl) == TYPE_DECL)
2510 {
ed44da02
MM
2511 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
2512 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
2513 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
2514 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
2515 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
6b9ab5cc
MM
2516 else
2517 {
8251199e 2518 cp_error ("`%D' does not declare a template type", decl);
6b9ab5cc
MM
2519 return decl;
2520 }
2521 }
fc378698 2522 else if (! DECL_TEMPLATE_INFO (decl))
c91a56d2 2523 {
8251199e 2524 cp_error ("template definition of non-template `%#D'", decl);
3ac3d9ea 2525 return decl;
c91a56d2 2526 }
8d08fdba 2527 else
5566b478 2528 tmpl = DECL_TI_TEMPLATE (decl);
98c1c668 2529
e1a5ccf7
JM
2530 if (is_member_template (tmpl)
2531 && DECL_FUNCTION_TEMPLATE_P (tmpl)
2532 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
2533 && DECL_TEMPLATE_SPECIALIZATION (decl))
98c1c668 2534 {
e1a5ccf7
JM
2535 tree new_tmpl;
2536
2537 /* The declaration is a specialization of a member
2538 template, declared outside the class. Therefore, the
2539 innermost template arguments will be NULL, so we
2540 replace them with the arguments determined by the
2541 earlier call to check_explicit_specialization. */
2542 args = DECL_TI_ARGS (decl);
2543
2544 new_tmpl
2545 = build_template_decl (decl, current_template_parms);
2546 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
2547 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2548 DECL_TI_TEMPLATE (decl) = new_tmpl;
2549 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2550 DECL_TEMPLATE_INFO (new_tmpl) =
2551 perm_tree_cons (tmpl, args, NULL_TREE);
2552
2553 register_specialization (new_tmpl, tmpl, args);
2554 return decl;
98c1c668 2555 }
98c1c668 2556
e1a5ccf7 2557 /* Make sure the template headers we got make sense. */
6633d636 2558
e1a5ccf7
JM
2559 parms = DECL_TEMPLATE_PARMS (tmpl);
2560 i = TMPL_PARMS_DEPTH (parms);
2561 if (TMPL_ARGS_DEPTH (args) != i)
98c1c668 2562 {
e1a5ccf7
JM
2563 cp_error ("expected %d levels of template parms for `%#D', got %d",
2564 i, decl, TMPL_ARGS_DEPTH (args));
98c1c668 2565 }
e1a5ccf7
JM
2566 else
2567 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
2568 {
2569 a = TMPL_ARGS_LEVEL (args, i);
2570 t = INNERMOST_TEMPLATE_PARMS (parms);
2571
2572 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2573 {
2574 if (current == decl)
2575 cp_error ("got %d template parameters for `%#D'",
2576 TREE_VEC_LENGTH (a), decl);
2577 else
2578 cp_error ("got %d template parameters for `%#T'",
2579 TREE_VEC_LENGTH (a), current);
2580 cp_error (" but %d required", TREE_VEC_LENGTH (t));
2581 }
98c1c668 2582
e1a5ccf7
JM
2583 /* Perhaps we should also check that the parms are used in the
2584 appropriate qualifying scopes in the declarator? */
6633d636 2585
e1a5ccf7
JM
2586 if (current == decl)
2587 current = ctx;
2588 else
2589 current = TYPE_CONTEXT (current);
2590 }
5566b478 2591 }
8d08fdba 2592
5566b478
MS
2593 DECL_TEMPLATE_RESULT (tmpl) = decl;
2594 TREE_TYPE (tmpl) = TREE_TYPE (decl);
8d08fdba 2595
36a117a5
MM
2596 /* Push template declarations for global functions and types. Note
2597 that we do not try to push a global template friend declared in a
2598 template class; such a thing may well depend on the template
39c01e4c 2599 parameters of the class. */
cfe507be 2600 if (new_template_p && !ctx
36a117a5 2601 && !(is_friend && template_class_depth (current_class_type) > 0))
2c73f9f5 2602 tmpl = pushdecl_namespace_level (tmpl);
8d08fdba 2603
5566b478 2604 if (primary)
6757edfe 2605 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5566b478 2606
2a1e9fdd 2607 info = perm_tree_cons (tmpl, args, NULL_TREE);
5566b478 2608
9188c363 2609 if (DECL_IMPLICIT_TYPEDEF_P (decl))
8d08fdba 2610 {
ed44da02
MM
2611 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2612 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2613 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE)
75650646 2614 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
51c184be 2615 }
2aaf816d 2616 else if (DECL_LANG_SPECIFIC (decl))
5566b478 2617 DECL_TEMPLATE_INFO (decl) = info;
3ac3d9ea
MM
2618
2619 return DECL_TEMPLATE_RESULT (tmpl);
8d08fdba
MS
2620}
2621
6757edfe
MM
2622tree
2623push_template_decl (decl)
2624 tree decl;
2625{
2626 return push_template_decl_real (decl, 0);
2627}
2628
2629/* Called when a class template TYPE is redeclared with the indicated
2630 template PARMS, e.g.:
7fe6899f
MM
2631
2632 template <class T> struct S;
2633 template <class T> struct S {}; */
2634
2635void
6757edfe 2636redeclare_class_template (type, parms)
7fe6899f 2637 tree type;
6757edfe 2638 tree parms;
7fe6899f 2639{
3d7de1fa 2640 tree tmpl;
6757edfe 2641 tree tmpl_parms;
7fe6899f
MM
2642 int i;
2643
3d7de1fa
MM
2644 if (!TYPE_TEMPLATE_INFO (type))
2645 {
2646 cp_error ("`%T' is not a template type", type);
2647 return;
2648 }
2649
2650 tmpl = TYPE_TI_TEMPLATE (type);
7fe6899f
MM
2651 if (!PRIMARY_TEMPLATE_P (tmpl))
2652 /* The type is nested in some template class. Nothing to worry
2653 about here; there are no new template parameters for the nested
2654 type. */
2655 return;
2656
6757edfe
MM
2657 parms = INNERMOST_TEMPLATE_PARMS (parms);
2658 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2659
7fe6899f
MM
2660 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2661 {
8251199e
JM
2662 cp_error_at ("previous declaration `%D'", tmpl);
2663 cp_error ("used %d template parameter%s instead of %d",
7fe6899f
MM
2664 TREE_VEC_LENGTH (tmpl_parms),
2665 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2666 TREE_VEC_LENGTH (parms));
2667 return;
2668 }
2669
2670 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2671 {
2672 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2673 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2674 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2675 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2676
2677 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2678 {
8251199e
JM
2679 cp_error_at ("template parameter `%#D'", tmpl_parm);
2680 cp_error ("redeclared here as `%#D'", parm);
7fe6899f
MM
2681 return;
2682 }
2683
2684 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2685 {
2686 /* We have in [temp.param]:
2687
2688 A template-parameter may not be given default arguments
2689 by two different declarations in the same scope. */
8251199e
JM
2690 cp_error ("redefinition of default argument for `%#D'", parm);
2691 cp_error_at (" original definition appeared here", tmpl_parm);
7fe6899f
MM
2692 return;
2693 }
2694
2695 if (parm_default != NULL_TREE)
2696 /* Update the previous template parameters (which are the ones
2697 that will really count) with the new default value. */
2698 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
7ce74c5e
MM
2699 else if (tmpl_default != NULL_TREE)
2700 /* Update the new parameters, too; they'll be used as the
2701 parameters for any members. */
2702 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
7fe6899f
MM
2703 }
2704}
75650646 2705
75650646
MM
2706/* Attempt to convert the non-type template parameter EXPR to the
2707 indicated TYPE. If the conversion is successful, return the
2708 converted value. If the conversion is unsuccesful, return
2709 NULL_TREE if we issued an error message, or error_mark_node if we
2710 did not. We issue error messages for out-and-out bad template
2711 parameters, but not simply because the conversion failed, since we
2712 might be just trying to do argument deduction. By the time this
2713 function is called, neither TYPE nor EXPR may make use of template
2714 parameters. */
2715
2716static tree
e1467ff2 2717convert_nontype_argument (type, expr)
75650646
MM
2718 tree type;
2719 tree expr;
2720{
2721 tree expr_type = TREE_TYPE (expr);
2722
2723 /* A template-argument for a non-type, non-template
2724 template-parameter shall be one of:
2725
2726 --an integral constant-expression of integral or enumeration
2727 type; or
2728
2729 --the name of a non-type template-parameter; or
2730
2731 --the name of an object or function with external linkage,
2732 including function templates and function template-ids but
86052cc3 2733 excluding non-static class members, expressed as id-expression;
75650646
MM
2734 or
2735
2736 --the address of an object or function with external linkage,
2737 including function templates and function template-ids but
2738 excluding non-static class members, expressed as & id-expression
2739 where the & is optional if the name refers to a function or
2740 array; or
2741
2742 --a pointer to member expressed as described in _expr.unary.op_. */
2743
638dd8fc
MM
2744 /* An integral constant-expression can include const variables or
2745 enumerators. Simplify things by folding them to their values,
2746 unless we're about to bind the declaration to a reference
2747 parameter. */
2748 if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr)
2749 && TREE_CODE (type) != REFERENCE_TYPE)
86052cc3
JM
2750 expr = decl_constant_value (expr);
2751
7bf2682f
MM
2752 if (is_overloaded_fn (expr))
2753 /* OK for now. We'll check that it has external linkage later.
2754 Check this first since if expr_type is the unknown_type_node
2755 we would otherwise complain below. */
2756 ;
e08a8f45
MM
2757 else if (TYPE_PTRMEM_P (expr_type)
2758 || TYPE_PTRMEMFUNC_P (expr_type))
2759 {
2760 if (TREE_CODE (expr) != PTRMEM_CST)
2761 goto bad_argument;
2762 }
f3400fe2 2763 else if (TYPE_PTR_P (expr_type)
e08a8f45 2764 || TYPE_PTRMEM_P (expr_type)
f3400fe2
JM
2765 || TREE_CODE (expr_type) == ARRAY_TYPE
2766 || TREE_CODE (type) == REFERENCE_TYPE
75650646
MM
2767 /* If expr is the address of an overloaded function, we
2768 will get the unknown_type_node at this point. */
2769 || expr_type == unknown_type_node)
2770 {
2771 tree referent;
49bf0d6f
MM
2772 tree e = expr;
2773 STRIP_NOPS (e);
75650646 2774
f3400fe2
JM
2775 if (TREE_CODE (type) == REFERENCE_TYPE
2776 || TREE_CODE (expr_type) == ARRAY_TYPE)
2777 referent = e;
2778 else
75650646 2779 {
f3400fe2
JM
2780 if (TREE_CODE (e) != ADDR_EXPR)
2781 {
2782 bad_argument:
2783 cp_error ("`%E' is not a valid template argument", expr);
e08a8f45
MM
2784 if (TYPE_PTR_P (expr_type))
2785 {
2786 if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
2787 cp_error ("it must be the address of a function with external linkage");
2788 else
2789 cp_error ("it must be the address of an object with external linkage");
2790 }
2791 else if (TYPE_PTRMEM_P (expr_type)
2792 || TYPE_PTRMEMFUNC_P (expr_type))
2793 cp_error ("it must be a pointer-to-member of the form `&X::Y'");
2794
f3400fe2
JM
2795 return NULL_TREE;
2796 }
2797
2798 referent = TREE_OPERAND (e, 0);
2799 STRIP_NOPS (referent);
75650646
MM
2800 }
2801
75650646
MM
2802 if (TREE_CODE (referent) == STRING_CST)
2803 {
8251199e 2804 cp_error ("string literal %E is not a valid template argument",
75650646 2805 referent);
8251199e 2806 error ("because it is the address of an object with static linkage");
75650646
MM
2807 return NULL_TREE;
2808 }
2809
2810 if (is_overloaded_fn (referent))
2811 /* We'll check that it has external linkage later. */
2812 ;
2813 else if (TREE_CODE (referent) != VAR_DECL)
2814 goto bad_argument;
2815 else if (!TREE_PUBLIC (referent))
2816 {
8251199e 2817 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
75650646
MM
2818 return error_mark_node;
2819 }
2820 }
f3400fe2
JM
2821 else if (INTEGRAL_TYPE_P (expr_type)
2822 || TYPE_PTRMEM_P (expr_type)
2823 || TYPE_PTRMEMFUNC_P (expr_type)
2824 /* The next two are g++ extensions. */
2825 || TREE_CODE (expr_type) == REAL_TYPE
2826 || TREE_CODE (expr_type) == COMPLEX_TYPE)
75650646 2827 {
f3400fe2
JM
2828 if (! TREE_CONSTANT (expr))
2829 {
2830 non_constant:
2831 cp_error ("non-constant `%E' cannot be used as template argument",
2832 expr);
2833 return NULL_TREE;
2834 }
75650646 2835 }
75650646
MM
2836 else
2837 {
8251199e 2838 cp_error ("object `%E' cannot be used as template argument", expr);
75650646
MM
2839 return NULL_TREE;
2840 }
2841
2842 switch (TREE_CODE (type))
2843 {
2844 case INTEGER_TYPE:
2845 case BOOLEAN_TYPE:
2846 case ENUMERAL_TYPE:
2847 /* For a non-type template-parameter of integral or enumeration
2848 type, integral promotions (_conv.prom_) and integral
2849 conversions (_conv.integral_) are applied. */
2850 if (!INTEGRAL_TYPE_P (expr_type))
2851 return error_mark_node;
2852
2853 /* It's safe to call digest_init in this case; we know we're
2854 just converting one integral constant expression to another. */
db02b6b9 2855 expr = digest_init (type, expr, (tree*) 0);
75650646 2856
db02b6b9 2857 if (TREE_CODE (expr) != INTEGER_CST)
f3400fe2 2858 /* Curiously, some TREE_CONSTANT integral expressions do not
db02b6b9
MM
2859 simplify to integer constants. For example, `3 % 0',
2860 remains a TRUNC_MOD_EXPR. */
2861 goto non_constant;
2862
2863 return expr;
2864
abff8e06
JM
2865 case REAL_TYPE:
2866 case COMPLEX_TYPE:
2867 /* These are g++ extensions. */
2868 if (TREE_CODE (expr_type) != TREE_CODE (type))
2869 return error_mark_node;
2870
db02b6b9
MM
2871 expr = digest_init (type, expr, (tree*) 0);
2872
2873 if (TREE_CODE (expr) != REAL_CST)
2874 goto non_constant;
2875
2876 return expr;
abff8e06 2877
75650646
MM
2878 case POINTER_TYPE:
2879 {
2880 tree type_pointed_to = TREE_TYPE (type);
2881
2882 if (TYPE_PTRMEM_P (type))
93aa462d
MM
2883 {
2884 tree e;
2885
2886 /* For a non-type template-parameter of type pointer to data
2887 member, qualification conversions (_conv.qual_) are
2888 applied. */
2889 e = perform_qualification_conversions (type, expr);
2890 if (TREE_CODE (e) == NOP_EXPR)
87533b37
MM
2891 /* The call to perform_qualification_conversions will
2892 insert a NOP_EXPR over EXPR to do express conversion,
2893 if necessary. But, that will confuse us if we use
2894 this (converted) template parameter to instantiate
2895 another template; then the thing will not look like a
2896 valid template argument. So, just make a new
2897 constant, of the appropriate type. */
2898 e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
93aa462d
MM
2899 return e;
2900 }
75650646
MM
2901 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
2902 {
2903 /* For a non-type template-parameter of type pointer to
2904 function, only the function-to-pointer conversion
2905 (_conv.func_) is applied. If the template-argument
2906 represents a set of overloaded functions (or a pointer to
2907 such), the matching function is selected from the set
2908 (_over.over_). */
2909 tree fns;
2910 tree fn;
2911
7bf2682f 2912 if (TREE_CODE (expr) == ADDR_EXPR)
75650646
MM
2913 fns = TREE_OPERAND (expr, 0);
2914 else
2915 fns = expr;
2916
a05273d4 2917 fn = instantiate_type (type_pointed_to, fns, 0);
75650646
MM
2918
2919 if (fn == error_mark_node)
2920 return error_mark_node;
2921
2922 if (!TREE_PUBLIC (fn))
2923 {
2924 if (really_overloaded_fn (fns))
2925 return error_mark_node;
2926 else
2927 goto bad_argument;
2928 }
2929
2930 expr = build_unary_op (ADDR_EXPR, fn, 0);
2931
3bfdc719 2932 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
75650646
MM
2933 0);
2934 return expr;
2935 }
2936 else
2937 {
2938 /* For a non-type template-parameter of type pointer to
2939 object, qualification conversions (_conv.qual_) and the
2940 array-to-pointer conversion (_conv.array_) are applied.
2941 [Note: In particular, neither the null pointer conversion
2942 (_conv.ptr_) nor the derived-to-base conversion
2943 (_conv.ptr_) are applied. Although 0 is a valid
2944 template-argument for a non-type template-parameter of
2945 integral type, it is not a valid template-argument for a
e1467ff2
MM
2946 non-type template-parameter of pointer type.]
2947
2948 The call to decay_conversion performs the
2949 array-to-pointer conversion, if appropriate. */
2950 expr = decay_conversion (expr);
75650646
MM
2951
2952 if (expr == error_mark_node)
2953 return error_mark_node;
2954 else
2955 return perform_qualification_conversions (type, expr);
2956 }
2957 }
2958 break;
2959
2960 case REFERENCE_TYPE:
2961 {
2962 tree type_referred_to = TREE_TYPE (type);
2963
2964 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
2965 {
2966 /* For a non-type template-parameter of type reference to
39c01e4c
MM
2967 function, no conversions apply. If the
2968 template-argument represents a set of overloaded
2969 functions, the matching function is selected from the
2970 set (_over.over_). */
75650646
MM
2971 tree fns = expr;
2972 tree fn;
2973
a05273d4 2974 fn = instantiate_type (type_referred_to, fns, 0);
e6e174e5
JM
2975
2976 if (fn == error_mark_node)
2977 return error_mark_node;
75650646
MM
2978
2979 if (!TREE_PUBLIC (fn))
2980 {
2981 if (really_overloaded_fn (fns))
2982 /* Don't issue an error here; we might get a different
2983 function if the overloading had worked out
2984 differently. */
2985 return error_mark_node;
2986 else
2987 goto bad_argument;
2988 }
2989
3bfdc719
MM
2990 my_friendly_assert (same_type_p (type_referred_to,
2991 TREE_TYPE (fn)),
75650646
MM
2992 0);
2993
2994 return fn;
2995 }
2996 else
2997 {
2998 /* For a non-type template-parameter of type reference to
2999 object, no conversions apply. The type referred to by the
3000 reference may be more cv-qualified than the (otherwise
3001 identical) type of the template-argument. The
3002 template-parameter is bound directly to the
3003 template-argument, which must be an lvalue. */
ff350acd
JM
3004 if ((TYPE_MAIN_VARIANT (expr_type)
3005 != TYPE_MAIN_VARIANT (type_referred_to))
91063b51
MM
3006 || !at_least_as_qualified_p (type_referred_to,
3007 expr_type)
75650646
MM
3008 || !real_lvalue_p (expr))
3009 return error_mark_node;
3010 else
3011 return expr;
3012 }
3013 }
3014 break;
3015
3016 case RECORD_TYPE:
3017 {
410c2fba
KL
3018 if (!TYPE_PTRMEMFUNC_P (type))
3019 /* This handles templates like
3020 template<class T, T t> void f();
3021 when T is substituted with any class. The second template
3022 parameter becomes invalid and the template candidate is
3023 rejected. */
3024 return error_mark_node;
75650646
MM
3025
3026 /* For a non-type template-parameter of type pointer to member
3027 function, no conversions apply. If the template-argument
3028 represents a set of overloaded member functions, the
3029 matching member function is selected from the set
3030 (_over.over_). */
3031
3032 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
3033 expr_type != unknown_type_node)
3034 return error_mark_node;
3035
e08a8f45 3036 if (TREE_CODE (expr) == PTRMEM_CST)
75650646
MM
3037 {
3038 /* A ptr-to-member constant. */
3bfdc719 3039 if (!same_type_p (type, expr_type))
75650646
MM
3040 return error_mark_node;
3041 else
3042 return expr;
3043 }
3044
3045 if (TREE_CODE (expr) != ADDR_EXPR)
3046 return error_mark_node;
3047
d8f8dca1 3048 expr = instantiate_type (type, expr, 0);
75650646 3049
d8f8dca1 3050 if (expr == error_mark_node)
75650646
MM
3051 return error_mark_node;
3052
3bfdc719 3053 my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
75650646
MM
3054 0);
3055 return expr;
3056 }
3057 break;
3058
3059 default:
3060 /* All non-type parameters must have one of these types. */
3061 my_friendly_abort (0);
3062 break;
3063 }
3064
3065 return error_mark_node;
3066}
3067
744fac59
KL
3068/* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
3069 template template parameters. Both PARM_PARMS and ARG_PARMS are
3070 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
3071 or PARM_DECL.
3072
3073 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
3074 the case, then extra parameters must have default arguments.
3075
3076 Consider the example:
3077 template <class T, class Allocator = allocator> class vector;
3078 template<template <class U> class TT> class C;
3079
3080 C<vector> is a valid instantiation. PARM_PARMS for the above code
3081 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
3082 T and Allocator) and OUTER_ARGS contains the argument that is used to
3083 substitute the TT parameter. */
3084
3085static int
4393e105
MM
3086coerce_template_template_parms (parm_parms, arg_parms, complain,
3087 in_decl, outer_args)
3088 tree parm_parms, arg_parms;
3089 int complain;
3090 tree in_decl, outer_args;
744fac59
KL
3091{
3092 int nparms, nargs, i;
3093 tree parm, arg;
3094
3095 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
3096 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
3097
3098 nparms = TREE_VEC_LENGTH (parm_parms);
3099 nargs = TREE_VEC_LENGTH (arg_parms);
3100
3101 /* The rule here is opposite of coerce_template_parms. */
3102 if (nargs < nparms
3103 || (nargs > nparms
3104 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
3105 return 0;
3106
3107 for (i = 0; i < nparms; ++i)
3108 {
3109 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
3110 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
3111
3112 if (arg == NULL_TREE || arg == error_mark_node
3113 || parm == NULL_TREE || parm == error_mark_node)
3114 return 0;
3115
3116 if (TREE_CODE (arg) != TREE_CODE (parm))
3117 return 0;
3118
3119 switch (TREE_CODE (parm))
3120 {
3121 case TYPE_DECL:
3122 break;
3123
3124 case TEMPLATE_DECL:
3125 /* We encounter instantiations of templates like
3126 template <template <template <class> class> class TT>
3127 class C; */
700466c2
JM
3128 {
3129 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3130 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3131
3132 if (!coerce_template_template_parms (parmparm, argparm,
4393e105
MM
3133 complain, in_decl,
3134 outer_args))
700466c2
JM
3135 return 0;
3136 }
3137 break;
744fac59
KL
3138
3139 case PARM_DECL:
3140 /* The tsubst call is used to handle cases such as
3141 template <class T, template <T> class TT> class D;
3142 i.e. the parameter list of TT depends on earlier parameters. */
4393e105
MM
3143 if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
3144 complain, in_decl),
3bfdc719 3145 TREE_TYPE (arg)))
744fac59
KL
3146 return 0;
3147 break;
3148
3149 default:
3150 my_friendly_abort (0);
3151 }
3152 }
3153 return 1;
3154}
3155
8b5b8b7c
MM
3156/* Convert the indicated template ARG as necessary to match the
3157 indicated template PARM. Returns the converted ARG, or
3158 error_mark_node if the conversion was unsuccessful. Error messages
3159 are issued if COMPLAIN is non-zero. This conversion is for the Ith
3160 parameter in the parameter list. ARGS is the full set of template
3161 arguments deduced so far. */
3162
3163static tree
3164convert_template_argument (parm, arg, args, complain, i, in_decl)
3165 tree parm;
3166 tree arg;
3167 tree args;
3168 int complain;
3169 int i;
3170 tree in_decl;
3171{
3172 tree val;
3173 tree inner_args;
3174 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
3175
3176 inner_args = innermost_args (args);
3177
3178 if (TREE_CODE (arg) == TREE_LIST
3179 && TREE_TYPE (arg) != NULL_TREE
3180 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
3181 {
3182 /* The template argument was the name of some
3183 member function. That's usually
3184 illegal, but static members are OK. In any
3185 case, grab the underlying fields/functions
3186 and issue an error later if required. */
3187 arg = TREE_VALUE (arg);
3188 TREE_TYPE (arg) = unknown_type_node;
3189 }
3190
3191 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
3192 requires_type = (TREE_CODE (parm) == TYPE_DECL
3193 || requires_tmpl_type);
3194
3195 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
3196 we also accept implicitly created TYPE_DECL as a valid argument.
3197 This is necessary to handle the case where we pass a template name
3198 to a template template parameter in a scope where we've derived from
3199 in instantiation of that template, so the template name refers to that
3200 instantiation. We really ought to handle this better. */
3201 is_tmpl_type
3202 = ((TREE_CODE (arg) == TEMPLATE_DECL
3203 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
3204 || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7ddedda4 3205 && !TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (arg))
8b5b8b7c
MM
3206 || (TREE_CODE (arg) == RECORD_TYPE
3207 && CLASSTYPE_TEMPLATE_INFO (arg)
3208 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
3209 && DECL_ARTIFICIAL (TYPE_NAME (arg))
3210 && requires_tmpl_type
b54ccf71 3211 && is_base_of_enclosing_class (arg, current_class_type)));
8b5b8b7c
MM
3212 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
3213 arg = TYPE_STUB_DECL (arg);
3214 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
3215 arg = CLASSTYPE_TI_TEMPLATE (arg);
3216
3217 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
3218
3219 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
3220 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
3221 {
8251199e
JM
3222 cp_pedwarn ("to refer to a type member of a template parameter,");
3223 cp_pedwarn (" use `typename %E'", arg);
8b5b8b7c
MM
3224
3225 arg = make_typename_type (TREE_OPERAND (arg, 0),
3226 TREE_OPERAND (arg, 1));
3227 is_type = 1;
3228 }
3229 if (is_type != requires_type)
3230 {
3231 if (in_decl)
3232 {
3233 if (complain)
3234 {
8251199e 3235 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c
MM
3236 i + 1, in_decl);
3237 if (is_type)
8251199e 3238 cp_error (" expected a constant of type `%T', got `%T'",
8b5b8b7c
MM
3239 TREE_TYPE (parm),
3240 (is_tmpl_type ? DECL_NAME (arg) : arg));
3241 else
8251199e 3242 cp_error (" expected a type, got `%E'", arg);
8b5b8b7c
MM
3243 }
3244 }
3245 return error_mark_node;
3246 }
3247 if (is_tmpl_type ^ requires_tmpl_type)
3248 {
3249 if (in_decl && complain)
3250 {
8251199e 3251 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c
MM
3252 i + 1, in_decl);
3253 if (is_tmpl_type)
8251199e 3254 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
8b5b8b7c 3255 else
8251199e 3256 cp_error (" expected a class template, got `%T'", arg);
8b5b8b7c
MM
3257 }
3258 return error_mark_node;
3259 }
3260
3261 if (is_type)
3262 {
3263 if (requires_tmpl_type)
3264 {
3265 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
3266 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
3267
4393e105 3268 if (coerce_template_template_parms (parmparm, argparm, complain,
8b5b8b7c
MM
3269 in_decl, inner_args))
3270 {
3271 val = arg;
3272
3273 /* TEMPLATE_TEMPLATE_PARM node is preferred over
3274 TEMPLATE_DECL. */
3275 if (val != error_mark_node
3276 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
3277 val = TREE_TYPE (val);
3278 }
3279 else
3280 {
3281 if (in_decl && complain)
3282 {
8251199e 3283 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
8b5b8b7c 3284 i + 1, in_decl);
8251199e 3285 cp_error (" expected a template of type `%D', got `%D'", parm, arg);
8b5b8b7c
MM
3286 }
3287
3288 val = error_mark_node;
3289 }
3290 }
3291 else
3292 {
3293 val = groktypename (arg);
3294 if (! processing_template_decl)
3295 {
3296 /* [basic.link]: A name with no linkage (notably, the
3297 name of a class or enumeration declared in a local
3298 scope) shall not be used to declare an entity with
3299 linkage. This implies that names with no linkage
3300 cannot be used as template arguments. */
3301 tree t = no_linkage_check (val);
3302 if (t)
3303 {
3304 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8251199e
JM
3305 cp_pedwarn
3306 ("template-argument `%T' uses anonymous type", val);
8b5b8b7c 3307 else
8251199e
JM
3308 cp_error
3309 ("template-argument `%T' uses local type `%T'",
8b5b8b7c
MM
3310 val, t);
3311 return error_mark_node;
3312 }
3313 }
3314 }
3315 }
3316 else
3317 {
4393e105 3318 tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
8b5b8b7c
MM
3319
3320 if (processing_template_decl)
3321 arg = maybe_fold_nontype_arg (arg);
3322
3323 if (!uses_template_parms (arg) && !uses_template_parms (t))
3324 /* We used to call digest_init here. However, digest_init
3325 will report errors, which we don't want when complain
3326 is zero. More importantly, digest_init will try too
3327 hard to convert things: for example, `0' should not be
3328 converted to pointer type at this point according to
3329 the standard. Accepting this is not merely an
3330 extension, since deciding whether or not these
3331 conversions can occur is part of determining which
3332 function template to call, or whether a given epxlicit
3333 argument specification is legal. */
3334 val = convert_nontype_argument (t, arg);
3335 else
3336 val = arg;
3337
3338 if (val == NULL_TREE)
3339 val = error_mark_node;
3340 else if (val == error_mark_node && complain)
8251199e 3341 cp_error ("could not convert template argument `%E' to `%T'",
8b5b8b7c
MM
3342 arg, t);
3343 }
3344
3345 return val;
3346}
3347
3348/* Convert all template arguments to their appropriate types, and
3349 return a vector containing the innermost resulting template
3350 arguments. If any error occurs, return error_mark_node, and, if
3351 COMPLAIN is non-zero, issue an error message. Some error messages
3352 are issued even if COMPLAIN is zero; for instance, if a template
3353 argument is composed from a local class.
75650646
MM
3354
3355 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
3356 provided in ARGLIST, or else trailing parameters must have default
3357 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
cabc336a
MM
3358 deduction for any unspecified trailing arguments.
3359
3360 The resulting TREE_VEC is allocated on a temporary obstack, and
3361 must be explicitly copied if it will be permanent. */
de575009 3362
8d08fdba 3363static tree
8b5b8b7c 3364coerce_template_parms (parms, args, in_decl,
75650646 3365 complain,
744fac59 3366 require_all_arguments)
8b5b8b7c 3367 tree parms, args;
8d08fdba 3368 tree in_decl;
75650646
MM
3369 int complain;
3370 int require_all_arguments;
8d08fdba 3371{
a292b002 3372 int nparms, nargs, i, lost = 0;
e4a84209 3373 tree inner_args;
8b5b8b7c
MM
3374 tree new_args;
3375 tree new_inner_args;
a292b002 3376
8b5b8b7c 3377 inner_args = innermost_args (args);
e4a84209 3378 nargs = NUM_TMPL_ARGS (inner_args);
a292b002
MS
3379 nparms = TREE_VEC_LENGTH (parms);
3380
3381 if (nargs > nparms
3382 || (nargs < nparms
75650646 3383 && require_all_arguments
a292b002 3384 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
8d08fdba 3385 {
75650646
MM
3386 if (complain)
3387 {
8251199e 3388 cp_error ("wrong number of template arguments (%d, should be %d)",
e4a84209 3389 nargs, nparms);
75650646
MM
3390
3391 if (in_decl)
8251199e 3392 cp_error_at ("provided for `%D'", in_decl);
75650646
MM
3393 }
3394
8d08fdba
MS
3395 return error_mark_node;
3396 }
3397
cabc336a 3398 new_inner_args = make_temp_vec (nparms);
8b5b8b7c
MM
3399 new_args = add_outermost_template_args (args, new_inner_args);
3400 for (i = 0; i < nparms; i++)
8d08fdba 3401 {
8b5b8b7c
MM
3402 tree arg;
3403 tree parm;
e4a84209 3404
8b5b8b7c
MM
3405 /* Get the Ith template parameter. */
3406 parm = TREE_VEC_ELT (parms, i);
75650646 3407
8b5b8b7c
MM
3408 /* Calculate the Ith argument. */
3409 if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
8d08fdba 3410 {
8b5b8b7c
MM
3411 arg = TREE_VALUE (inner_args);
3412 inner_args = TREE_CHAIN (inner_args);
8d08fdba 3413 }
8b5b8b7c
MM
3414 else if (i < nargs)
3415 arg = TREE_VEC_ELT (inner_args, i);
3416 /* If no template argument was supplied, look for a default
3417 value. */
3418 else if (TREE_PURPOSE (parm) == NULL_TREE)
3419 {
3420 /* There was no default value. */
3421 my_friendly_assert (!require_all_arguments, 0);
3422 break;
3423 }
3424 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
4393e105 3425 arg = tsubst (TREE_PURPOSE (parm), new_args, complain, in_decl);
8b5b8b7c 3426 else
4393e105
MM
3427 arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
3428 in_decl);
e4a84209 3429
8b5b8b7c 3430 /* Now, convert the Ith argument, as necessary. */
75650646
MM
3431 if (arg == NULL_TREE)
3432 /* We're out of arguments. */
3433 {
3434 my_friendly_assert (!require_all_arguments, 0);
3435 break;
3436 }
8b5b8b7c 3437 else if (arg == error_mark_node)
75650646 3438 {
8251199e 3439 cp_error ("template argument %d is invalid", i + 1);
8b5b8b7c 3440 arg = error_mark_node;
8d08fdba 3441 }
8b5b8b7c
MM
3442 else
3443 arg = convert_template_argument (TREE_VALUE (parm),
3444 arg, new_args, complain, i,
3445 in_decl);
3446
3447 if (arg == error_mark_node)
8d08fdba 3448 lost++;
8b5b8b7c 3449 TREE_VEC_ELT (new_inner_args, i) = arg;
8d08fdba 3450 }
8b5b8b7c 3451
8d08fdba
MS
3452 if (lost)
3453 return error_mark_node;
8b5b8b7c
MM
3454
3455 return new_inner_args;
8d08fdba
MS
3456}
3457
34016c81
JM
3458/* Returns 1 if template args OT and NT are equivalent. */
3459
d8e178a0 3460static int
34016c81
JM
3461template_args_equal (ot, nt)
3462 tree ot, nt;
3463{
3464 if (nt == ot)
3465 return 1;
3466 if (TREE_CODE (nt) != TREE_CODE (ot))
3467 return 0;
3468 if (TREE_CODE (nt) == TREE_VEC)
3469 /* For member templates */
3470 return comp_template_args (ot, nt);
3471 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
3bfdc719 3472 return same_type_p (ot, nt);
34016c81
JM
3473 else
3474 return (cp_tree_equal (ot, nt) > 0);
3475}
3476
3477/* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets
f84b4be9
JM
3478 of template arguments. Returns 0 otherwise. */
3479
6757edfe 3480int
5566b478
MS
3481comp_template_args (oldargs, newargs)
3482 tree oldargs, newargs;
3483{
3484 int i;
3485
386b8a85
JM
3486 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
3487 return 0;
3488
5566b478
MS
3489 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
3490 {
3491 tree nt = TREE_VEC_ELT (newargs, i);
3492 tree ot = TREE_VEC_ELT (oldargs, i);
3493
34016c81 3494 if (! template_args_equal (ot, nt))
61a127b3 3495 return 0;
5566b478
MS
3496 }
3497 return 1;
3498}
3499
8d08fdba
MS
3500/* Given class template name and parameter list, produce a user-friendly name
3501 for the instantiation. */
e92cc029 3502
8d08fdba 3503static char *
36a117a5 3504mangle_class_name_for_template (name, parms, arglist)
8d08fdba
MS
3505 char *name;
3506 tree parms, arglist;
3507{
3508 static struct obstack scratch_obstack;
3509 static char *scratch_firstobj;
3510 int i, nparms;
8d08fdba
MS
3511
3512 if (!scratch_firstobj)
fc378698 3513 gcc_obstack_init (&scratch_obstack);
8d08fdba
MS
3514 else
3515 obstack_free (&scratch_obstack, scratch_firstobj);
fc378698 3516 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
8d08fdba 3517
8d08fdba 3518#define ccat(c) obstack_1grow (&scratch_obstack, (c));
8d08fdba 3519#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
8d08fdba
MS
3520
3521 cat (name);
3522 ccat ('<');
3523 nparms = TREE_VEC_LENGTH (parms);
36a117a5 3524 arglist = innermost_args (arglist);
8d08fdba
MS
3525 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
3526 for (i = 0; i < nparms; i++)
3527 {
a292b002
MS
3528 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
3529 tree arg = TREE_VEC_ELT (arglist, i);
8d08fdba
MS
3530
3531 if (i)
3532 ccat (',');
3533
a292b002 3534 if (TREE_CODE (parm) == TYPE_DECL)
8d08fdba 3535 {
294471c3 3536 cat (type_as_string_real (arg, 0, 1));
8d08fdba
MS
3537 continue;
3538 }
73b0fce8
KL
3539 else if (TREE_CODE (parm) == TEMPLATE_DECL)
3540 {
3541 if (TREE_CODE (arg) == TEMPLATE_DECL)
2c73f9f5
ML
3542 {
3543 /* Already substituted with real template. Just output
3544 the template name here */
cb0dbb9a 3545 tree context = DECL_CONTEXT (arg);
ec4f972f
AS
3546 if (context)
3547 {
3548 /* The template may be defined in a namespace, or
3549 may be a member template. */
3550 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL
3551 || CLASS_TYPE_P (context),
3552 980422);
2c73f9f5
ML
3553 cat(decl_as_string (DECL_CONTEXT (arg), 0));
3554 cat("::");
3555 }
3556 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
3557 }
73b0fce8
KL
3558 else
3559 /* Output the parameter declaration */
294471c3 3560 cat (type_as_string_real (arg, 0, 1));
73b0fce8
KL
3561 continue;
3562 }
8d08fdba
MS
3563 else
3564 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
3565
3566 if (TREE_CODE (arg) == TREE_LIST)
3567 {
3568 /* New list cell was built because old chain link was in
3569 use. */
3570 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
3571 arg = TREE_VALUE (arg);
3572 }
3573 /* No need to check arglist against parmlist here; we did that
3574 in coerce_template_parms, called from lookup_template_class. */
3575 cat (expr_as_string (arg, 0));
3576 }
3577 {
3578 char *bufp = obstack_next_free (&scratch_obstack);
3579 int offset = 0;
3580 while (bufp[offset - 1] == ' ')
3581 offset--;
3582 obstack_blank_fast (&scratch_obstack, offset);
3583
3584 /* B<C<char> >, not B<C<char>> */
3585 if (bufp[offset - 1] == '>')
3586 ccat (' ');
3587 }
3588 ccat ('>');
3589 ccat ('\0');
3590 return (char *) obstack_base (&scratch_obstack);
8d08fdba
MS
3591}
3592
bd6dd845 3593static tree
5566b478
MS
3594classtype_mangled_name (t)
3595 tree t;
3596{
3597 if (CLASSTYPE_TEMPLATE_INFO (t)
36a117a5
MM
3598 /* Specializations have already had their names set up in
3599 lookup_template_class. */
9fbf56f7
MM
3600 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
3601 {
3602 tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
3603
36a117a5
MM
3604 /* For non-primary templates, the template parameters are
3605 implicit from their surrounding context. */
9fbf56f7
MM
3606 if (PRIMARY_TEMPLATE_P (tmpl))
3607 {
3608 tree name = DECL_NAME (tmpl);
3609 char *mangled_name = mangle_class_name_for_template
3610 (IDENTIFIER_POINTER (name),
3611 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
3612 CLASSTYPE_TI_ARGS (t));
3613 tree id = get_identifier (mangled_name);
3614 IDENTIFIER_TEMPLATE (id) = name;
3615 return id;
3616 }
5566b478 3617 }
9fbf56f7
MM
3618
3619 return TYPE_IDENTIFIER (t);
5566b478
MS
3620}
3621
3622static void
3623add_pending_template (d)
3624 tree d;
3625{
e92cc029
MS
3626 tree ti;
3627
3628 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
3629 ti = CLASSTYPE_TEMPLATE_INFO (d);
3630 else
3631 ti = DECL_TEMPLATE_INFO (d);
3632
824b9a4c 3633 if (TI_PENDING_TEMPLATE_FLAG (ti))
5566b478
MS
3634 return;
3635
3636 *template_tail = perm_tree_cons
1139b3d8 3637 (build_srcloc_here (), d, NULL_TREE);
5566b478 3638 template_tail = &TREE_CHAIN (*template_tail);
824b9a4c 3639 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
5566b478
MS
3640}
3641
386b8a85
JM
3642
3643/* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3644 may be either a _DECL or an overloaded function or an
3645 IDENTIFIER_NODE), and ARGLIST. */
3646
3647tree
3648lookup_template_function (fns, arglist)
3649 tree fns, arglist;
3650{
2c73f9f5 3651 tree type;
050367a3 3652
386b8a85
JM
3653 if (fns == NULL_TREE)
3654 {
8251199e 3655 cp_error ("non-template used as template");
386b8a85
JM
3656 return error_mark_node;
3657 }
3658
2c73f9f5
ML
3659 type = TREE_TYPE (fns);
3660 if (TREE_CODE (fns) == OVERLOAD || !type)
3661 type = unknown_type_node;
3662
672476cb
MM
3663 if (processing_template_decl)
3664 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3665 else
3666 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
386b8a85
JM
3667}
3668
a2b60a0e
MM
3669/* Within the scope of a template class S<T>, the name S gets bound
3670 (in build_self_reference) to a TYPE_DECL for the class, not a
3671 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3672 or one of its enclosing classes, and that type is a template,
3673 return the associated TEMPLATE_DECL. Otherwise, the original
3674 DECL is returned. */
3675
e9659ab0 3676static tree
a2b60a0e
MM
3677maybe_get_template_decl_from_type_decl (decl)
3678 tree decl;
3679{
3680 return (decl != NULL_TREE
3681 && TREE_CODE (decl) == TYPE_DECL
3682 && DECL_ARTIFICIAL (decl)
511b60ff 3683 && CLASS_TYPE_P (TREE_TYPE (decl))
a2b60a0e
MM
3684 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3685 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3686}
386b8a85 3687
8d08fdba
MS
3688/* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3689 parameters, find the desired type.
3690
3691 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
36a117a5
MM
3692 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3693 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3694 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3695 copy it here to keep it from being reclaimed when the decl storage
3696 is reclaimed.
8d08fdba
MS
3697
3698 IN_DECL, if non-NULL, is the template declaration we are trying to
75650646
MM
3699 instantiate.
3700
36a117a5
MM
3701 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3702 the class we are looking up.
3703
75650646
MM
3704 If the template class is really a local class in a template
3705 function, then the FUNCTION_CONTEXT is the function in which it is
3706 being instantiated. */
e92cc029 3707
8d08fdba 3708tree
36a117a5 3709lookup_template_class (d1, arglist, in_decl, context, entering_scope)
8d08fdba
MS
3710 tree d1, arglist;
3711 tree in_decl;
e1467ff2 3712 tree context;
36a117a5 3713 int entering_scope;
8d08fdba 3714{
a703fb38 3715 tree template = NULL_TREE, parmlist;
dbfe2124 3716 tree t;
5566b478
MS
3717
3718 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3719 {
f181d4ae
MM
3720 if (IDENTIFIER_VALUE (d1)
3721 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
3722 template = IDENTIFIER_VALUE (d1);
73b0fce8
KL
3723 else
3724 {
2c73f9f5
ML
3725 if (context)
3726 push_decl_namespace (context);
3ebc5c52
MM
3727 template = lookup_name (d1, /*prefer_type=*/0);
3728 template = maybe_get_template_decl_from_type_decl (template);
2c73f9f5
ML
3729 if (context)
3730 pop_decl_namespace ();
73b0fce8 3731 }
8d019cef
JM
3732 if (template)
3733 context = DECL_CONTEXT (template);
5566b478 3734 }
c91a56d2
MS
3735 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3736 {
802dbc34
JM
3737 tree type = TREE_TYPE (d1);
3738
3739 /* If we are declaring a constructor, say A<T>::A<T>, we will get
3740 an implicit typename for the second A. Deal with it. */
3741 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
3742 type = TREE_TYPE (type);
3743
3744 if (CLASSTYPE_TEMPLATE_INFO (type))
f3400fe2 3745 {
802dbc34 3746 template = CLASSTYPE_TI_TEMPLATE (type);
f3400fe2
JM
3747 d1 = DECL_NAME (template);
3748 }
c91a56d2 3749 }
ed44da02
MM
3750 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3751 || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't'
3752 && IS_AGGR_TYPE (d1)))
5566b478 3753 {
ed44da02 3754 template = TYPE_TI_TEMPLATE (d1);
5566b478
MS
3755 d1 = DECL_NAME (template);
3756 }
93cdc044
JM
3757 else if (TREE_CODE (d1) == TEMPLATE_DECL
3758 && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
3759 {
3760 template = d1;
3761 d1 = DECL_NAME (template);
3762 context = DECL_CONTEXT (template);
3763 }
5566b478
MS
3764 else
3765 my_friendly_abort (272);
8d08fdba 3766
8d08fdba 3767 /* With something like `template <class T> class X class X { ... };'
f181d4ae
MM
3768 we could end up with D1 having nothing but an IDENTIFIER_VALUE.
3769 We don't want to do that, but we have to deal with the situation,
3770 so let's give them some syntax errors to chew on instead of a
3771 crash. */
8d08fdba 3772 if (! template)
f3400fe2
JM
3773 {
3774 cp_error ("`%T' is not a template", d1);
3775 return error_mark_node;
3776 }
2c73f9f5 3777
c3baf4b5
JM
3778 if (context == NULL_TREE)
3779 context = global_namespace;
2c73f9f5 3780
8d08fdba
MS
3781 if (TREE_CODE (template) != TEMPLATE_DECL)
3782 {
8251199e 3783 cp_error ("non-template type `%T' used as a template", d1);
8d08fdba 3784 if (in_decl)
8251199e 3785 cp_error_at ("for template declaration `%D'", in_decl);
8d08fdba
MS
3786 return error_mark_node;
3787 }
8d08fdba 3788
73b0fce8
KL
3789 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3790 {
3791 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3792 template arguments */
3793
3794 tree parm = copy_template_template_parm (TREE_TYPE (template));
3795 tree template2 = TYPE_STUB_DECL (parm);
3796 tree arglist2;
3797
73b0fce8
KL
3798 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3799
744fac59 3800 arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1);
73b0fce8
KL
3801 if (arglist2 == error_mark_node)
3802 return error_mark_node;
3803
7ddedda4 3804 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)
73b0fce8
KL
3805 = perm_tree_cons (template2, arglist2, NULL_TREE);
3806 TYPE_SIZE (parm) = 0;
3807 return parm;
3808 }
36a117a5 3809 else
8d08fdba 3810 {
36a117a5 3811 tree template_type = TREE_TYPE (template);
7ac7b28f 3812 tree gen_tmpl;
36a117a5
MM
3813 tree type_decl;
3814 tree found = NULL_TREE;
3815 int arg_depth;
3816 int parm_depth;
dbfe2124 3817 int is_partial_instantiation;
830bfa74 3818
7ac7b28f
MM
3819 gen_tmpl = most_general_template (template);
3820 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
36a117a5
MM
3821 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3822 arg_depth = TMPL_ARGS_DEPTH (arglist);
3823
7ddedda4
MM
3824 /* We build up the coerced arguments and such on the
3825 momentary_obstack. */
3826 push_momentary ();
3827
36a117a5
MM
3828 if (arg_depth == 1 && parm_depth > 1)
3829 {
39c01e4c 3830 /* We've been given an incomplete set of template arguments.
36a117a5
MM
3831 For example, given:
3832
3833 template <class T> struct S1 {
3834 template <class U> struct S2 {};
3835 template <class U> struct S2<U*> {};
3836 };
3837
3838 we will be called with an ARGLIST of `U*', but the
3839 TEMPLATE will be `template <class T> template
3840 <class U> struct S1<T>::S2'. We must fill in the missing
3841 arguments. */
7ac7b28f
MM
3842 arglist
3843 = add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
3844 arglist);
36a117a5
MM
3845 arg_depth = TMPL_ARGS_DEPTH (arglist);
3846 }
5566b478 3847
7ac7b28f 3848 /* Now we should enough arguments. */
36a117a5
MM
3849 my_friendly_assert (parm_depth == arg_depth, 0);
3850
7ac7b28f
MM
3851 /* From here on, we're only interested in the most general
3852 template. */
3853 template = gen_tmpl;
3854
36a117a5
MM
3855 /* Calculate the BOUND_ARGS. These will be the args that are
3856 actually tsubst'd into the definition to create the
3857 instantiation. */
3858 if (parm_depth > 1)
830bfa74
MM
3859 {
3860 /* We have multiple levels of arguments to coerce, at once. */
830bfa74 3861 int i;
e4a84209 3862 int saved_depth = TMPL_ARGS_DEPTH (arglist);
36a117a5 3863
cabc336a 3864 tree bound_args = make_temp_vec (parm_depth);
830bfa74 3865
e4a84209 3866 for (i = saved_depth,
830bfa74 3867 t = DECL_TEMPLATE_PARMS (template);
e4a84209 3868 i > 0 && t != NULL_TREE;
830bfa74 3869 --i, t = TREE_CHAIN (t))
e4a84209
MM
3870 {
3871 tree a = coerce_template_parms (TREE_VALUE (t),
3872 arglist, template, 1, 1);
3873 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
3874
3875 /* We temporarily reduce the length of the ARGLIST so
3876 that coerce_template_parms will see only the arguments
3877 corresponding to the template parameters it is
3878 examining. */
3879 TREE_VEC_LENGTH (arglist)--;
3880 }
3881
3882 /* Restore the ARGLIST to its full size. */
3883 TREE_VEC_LENGTH (arglist) = saved_depth;
3884
36a117a5 3885 arglist = bound_args;
830bfa74
MM
3886 }
3887 else
36a117a5
MM
3888 arglist
3889 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
3890 innermost_args (arglist),
3891 template, 1, 1);
3892
3893 if (arglist == error_mark_node)
3894 /* We were unable to bind the arguments. */
5566b478 3895 return error_mark_node;
5566b478 3896
36a117a5
MM
3897 /* In the scope of a template class, explicit references to the
3898 template class refer to the type of the template, not any
3899 instantiation of it. For example, in:
3900
3901 template <class T> class C { void f(C<T>); }
3902
3903 the `C<T>' is just the same as `C'. Outside of the
3904 class, however, such a reference is an instantiation. */
ed44da02 3905 if (comp_template_args (TYPE_TI_ARGS (template_type),
36a117a5
MM
3906 arglist))
3907 {
3908 found = template_type;
3909
3910 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
5566b478 3911 {
36a117a5
MM
3912 tree ctx;
3913
3914 /* Note that we use DECL_CONTEXT, rather than
3915 CP_DECL_CONTEXT, so that the termination test is
3916 always just `ctx'. We're not interested in namepace
3917 scopes. */
3918 for (ctx = current_class_type;
3919 ctx;
3920 ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
3921 ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
3bfdc719 3922 if (same_type_p (ctx, template_type))
36a117a5
MM
3923 break;
3924
3925 if (!ctx)
3926 /* We're not in the scope of the class, so the
3927 TEMPLATE_TYPE is not the type we want after
3928 all. */
3929 found = NULL_TREE;
5566b478
MS
3930 }
3931 }
36a117a5
MM
3932
3933 if (!found)
75650646 3934 {
36a117a5
MM
3935 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3936 found; found = TREE_CHAIN (found))
3937 if (comp_template_args (TREE_PURPOSE (found), arglist))
3938 break;
75650646 3939
36a117a5
MM
3940 if (found)
3941 found = TREE_VALUE (found);
8d08fdba 3942 }
3ebc5c52 3943
36a117a5 3944 if (found)
8d08fdba 3945 {
7ddedda4 3946 pop_momentary ();
36a117a5 3947 return found;
8d08fdba 3948 }
5566b478 3949
dbfe2124 3950 /* This type is a "partial instantiation" if any of the template
486e4077
MM
3951 arguments still inolve template parameters. Note that we set
3952 IS_PARTIAL_INSTANTIATION for partial specializations as
3953 well. */
dbfe2124
MM
3954 is_partial_instantiation = uses_template_parms (arglist);
3955
3ebc5c52
MM
3956 if (!is_partial_instantiation
3957 && !PRIMARY_TEMPLATE_P (template)
3958 && TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
3959 {
3960 pop_momentary ();
3961 found = xref_tag_from_type (TREE_TYPE (template),
3962 DECL_NAME (template),
3963 /*globalize=*/1);
3964 return found;
3965 }
3966
3967 /* Since we didn't find the type, we'll have to create it.
3968 Since we'll be saving this type on the
3969 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3970 push_obstacks (&permanent_obstack, &permanent_obstack);
3971
36a117a5 3972 /* Create the type. */
ed44da02
MM
3973 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
3974 {
dbfe2124 3975 if (!is_partial_instantiation)
61a127b3 3976 t = start_enum (TYPE_IDENTIFIER (template_type));
ed44da02 3977 else
dbfe2124 3978 /* We don't want to call start_enum for this type, since
ed44da02
MM
3979 the values for the enumeration constants may involve
3980 template parameters. And, no one should be interested
3981 in the enumeration constants for such a type. */
3982 t = make_node (ENUMERAL_TYPE);
3983 }
3984 else
3985 {
3986 t = make_lang_type (TREE_CODE (template_type));
3987 CLASSTYPE_DECLARED_CLASS (t)
3988 = CLASSTYPE_DECLARED_CLASS (template_type);
3989 CLASSTYPE_GOT_SEMICOLON (t) = 1;
3990 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
f668f160 3991 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
ed44da02
MM
3992 }
3993
61a127b3 3994 /* If we called start_enum above, this information will already
ed44da02
MM
3995 be set up. */
3996 if (!TYPE_NAME (t))
3997 {
3998 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
36a117a5 3999
9188c363 4000 type_decl = create_implicit_typedef (DECL_NAME (template), t);
ed44da02 4001 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9188c363 4002 TYPE_STUB_DECL (t) = type_decl;
ed44da02
MM
4003 DECL_SOURCE_FILE (type_decl)
4004 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
4005 DECL_SOURCE_LINE (type_decl)
4006 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
ed44da02
MM
4007 }
4008 else
4009 type_decl = TYPE_NAME (t);
36a117a5 4010
9fbf56f7
MM
4011 /* Set up the template information. We have to figure out which
4012 template is the immediate parent if this is a full
4013 instantiation. */
4014 if (parm_depth == 1 || is_partial_instantiation
4015 || !PRIMARY_TEMPLATE_P (template))
4016 /* This case is easy; there are no member templates involved. */
4017 found = template;
4018 else
4019 {
4020 /* This is a full instantiation of a member template. There
4021 should be some partial instantiation of which this is an
4022 instance. */
4023
4024 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
4025 found; found = TREE_CHAIN (found))
4026 {
4027 int success;
4028 tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
4029
4030 /* We only want partial instantiations, here, not
4031 specializations or full instantiations. */
4032 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
4033 || !uses_template_parms (TREE_VALUE (found)))
4034 continue;
4035
4036 /* Temporarily reduce by one the number of levels in the
4037 ARGLIST and in FOUND so as to avoid comparing the
4038 last set of arguments. */
4039 TREE_VEC_LENGTH (arglist)--;
4040 TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
4041
4042 /* See if the arguments match. If they do, then TMPL is
4043 the partial instantiation we want. */
4044 success = comp_template_args (TREE_PURPOSE (found), arglist);
4045
4046 /* Restore the argument vectors to their full size. */
4047 TREE_VEC_LENGTH (arglist)++;
4048 TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
4049
4050 if (success)
4051 {
4052 found = tmpl;
4053 break;
4054 }
4055 }
4056
4057 if (!found)
4058 my_friendly_abort (0);
4059 }
4060
ed44da02 4061 SET_TYPE_TEMPLATE_INFO (t,
9fbf56f7 4062 tree_cons (found, arglist, NULL_TREE));
dbfe2124
MM
4063 DECL_TEMPLATE_INSTANTIATIONS (template)
4064 = tree_cons (arglist, t,
4065 DECL_TEMPLATE_INSTANTIATIONS (template));
4066
4067 if (TREE_CODE (t) == ENUMERAL_TYPE
4068 && !is_partial_instantiation)
61a127b3
MM
4069 /* Now that the type has been registered on the instantiations
4070 list, we set up the enumerators. Because the enumeration
4071 constants may involve the enumeration type itself, we make
4072 sure to register the type first, and then create the
4073 constants. That way, doing tsubst_expr for the enumeration
4074 constants won't result in recursive calls here; we'll find
4075 the instantiation and exit above. */
4076 tsubst_enum (template_type, t, arglist);
dbfe2124
MM
4077
4078 /* We're done with the permanent obstack, now. */
4079 pop_obstacks ();
7ddedda4
MM
4080 /* We're also done with the momentary allocation we started
4081 above. */
4082 pop_momentary ();
5566b478 4083
36a117a5
MM
4084 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
4085 is set up. */
ed44da02
MM
4086 if (TREE_CODE (t) != ENUMERAL_TYPE)
4087 DECL_NAME (type_decl) = classtype_mangled_name (t);
36a117a5 4088 DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
dbfe2124 4089 if (!is_partial_instantiation)
36a117a5
MM
4090 {
4091 DECL_ASSEMBLER_NAME (type_decl)
4092 = get_identifier (build_overload_name (t, 1, 1));
50a6dbd7
JM
4093
4094 /* For backwards compatibility; code that uses
4095 -fexternal-templates expects looking up a template to
4096 instantiate it. I think DDD still relies on this.
4097 (jason 8/20/1998) */
ed44da02
MM
4098 if (TREE_CODE (t) != ENUMERAL_TYPE
4099 && flag_external_templates
36a117a5
MM
4100 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
4101 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
4102 add_pending_template (t);
4103 }
4104 else
077e7015
MM
4105 /* If the type makes use of template parameters, the
4106 code that generates debugging information will crash. */
4107 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
8d08fdba 4108
36a117a5
MM
4109 return t;
4110 }
8d08fdba
MS
4111}
4112\f
050367a3
MM
4113/* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
4114 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
4115 If FN returns non-zero, the iteration is terminated, and
4116 for_each_template_parm returns 1. Otherwise, the iteration
4117 continues. If FN never returns a non-zero value, the value
4118 returned by for_each_template_parm is 0. If FN is NULL, it is
4119 considered to be the function which always returns 1. */
4120
e9659ab0 4121static int
050367a3 4122for_each_template_parm (t, fn, data)
8d08fdba 4123 tree t;
050367a3
MM
4124 tree_fn_t fn;
4125 void* data;
8d08fdba
MS
4126{
4127 if (!t)
4128 return 0;
581d38d0
MM
4129
4130 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
4131 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
4132 return 1;
4133
8d08fdba
MS
4134 switch (TREE_CODE (t))
4135 {
297e73d8 4136 case ARRAY_REF:
05e0b2f4 4137 case OFFSET_REF:
297e73d8
MM
4138 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
4139 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
4140
8d08fdba
MS
4141 case IDENTIFIER_NODE:
4142 if (!IDENTIFIER_TEMPLATE (t))
4143 return 0;
5566b478 4144 my_friendly_abort (42);
8d08fdba
MS
4145
4146 /* aggregates of tree nodes */
4147 case TREE_VEC:
4148 {
4149 int i = TREE_VEC_LENGTH (t);
4150 while (i--)
050367a3 4151 if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
8d08fdba
MS
4152 return 1;
4153 return 0;
4154 }
4155 case TREE_LIST:
050367a3
MM
4156 if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
4157 || for_each_template_parm (TREE_VALUE (t), fn, data))
8d08fdba 4158 return 1;
050367a3 4159 return for_each_template_parm (TREE_CHAIN (t), fn, data);
8d08fdba 4160
2c73f9f5
ML
4161 case OVERLOAD:
4162 if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
4163 return 1;
4164 return for_each_template_parm (OVL_CHAIN (t), fn, data);
4165
8d08fdba
MS
4166 /* constructed type nodes */
4167 case POINTER_TYPE:
4168 case REFERENCE_TYPE:
050367a3 4169 return for_each_template_parm (TREE_TYPE (t), fn, data);
ed44da02 4170
8d08fdba 4171 case RECORD_TYPE:
b7484fbe 4172 if (TYPE_PTRMEMFUNC_FLAG (t))
050367a3
MM
4173 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
4174 fn, data);
ed44da02
MM
4175 /* Fall through. */
4176
8d08fdba 4177 case UNION_TYPE:
ed44da02
MM
4178 case ENUMERAL_TYPE:
4179 if (! TYPE_TEMPLATE_INFO (t))
8d08fdba 4180 return 0;
050367a3 4181 return for_each_template_parm (TREE_VALUE
ed44da02 4182 (TYPE_TEMPLATE_INFO (t)),
050367a3 4183 fn, data);
588c2d1c
MM
4184 case METHOD_TYPE:
4185 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
8d08fdba 4186 return 1;
588c2d1c
MM
4187 /* Fall through. */
4188
4189 case FUNCTION_TYPE:
4190 /* Check the parameter types. Since default arguments are not
4191 instantiated until they are needed, the TYPE_ARG_TYPES may
4192 contain expressions that involve template parameters. But,
4193 no-one should be looking at them yet. And, once they're
4194 instantiated, they don't contain template parameters, so
4195 there's no point in looking at them then, either. */
4196 {
4197 tree parm;
4198
4199 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
4200 if (for_each_template_parm (TREE_VALUE (parm), fn, data))
4201 return 1;
4202 }
4203
4204 /* Check the return type, too. */
050367a3 4205 return for_each_template_parm (TREE_TYPE (t), fn, data);
588c2d1c 4206
8d08fdba 4207 case ARRAY_TYPE:
050367a3 4208 if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
8d08fdba 4209 return 1;
050367a3 4210 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba 4211 case OFFSET_TYPE:
050367a3 4212 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
8d08fdba 4213 return 1;
050367a3 4214 return for_each_template_parm (TREE_TYPE (t), fn, data);
8d08fdba
MS
4215
4216 /* decl nodes */
4217 case TYPE_DECL:
050367a3 4218 return for_each_template_parm (TREE_TYPE (t), fn, data);
5566b478 4219
73b0fce8
KL
4220 case TEMPLATE_DECL:
4221 /* A template template parameter is encountered */
4222 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
744fac59
KL
4223 return for_each_template_parm (TREE_TYPE (t), fn, data);
4224 /* Already substituted template template parameter */
73b0fce8
KL
4225 return 0;
4226
3ac3d9ea 4227 case CONST_DECL:
050367a3 4228 if (for_each_template_parm (DECL_INITIAL (t), fn, data))
3ac3d9ea
MM
4229 return 1;
4230 goto check_type_and_context;
4231
8d08fdba 4232 case FUNCTION_DECL:
5566b478 4233 case VAR_DECL:
5566b478 4234 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
050367a3 4235 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
8d08fdba
MS
4236 return 1;
4237 /* fall through */
8d08fdba 4238 case PARM_DECL:
3ac3d9ea 4239 check_type_and_context:
050367a3 4240 if (for_each_template_parm (TREE_TYPE (t), fn, data))
5566b478 4241 return 1;
050367a3
MM
4242 if (DECL_CONTEXT (t)
4243 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
8d08fdba 4244 return 1;
8d08fdba
MS
4245 return 0;
4246
4247 case CALL_EXPR:
297e73d8
MM
4248 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
4249 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
4250
8d08fdba 4251 case ADDR_EXPR:
050367a3 4252 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
8d08fdba
MS
4253
4254 /* template parm nodes */
73b0fce8 4255 case TEMPLATE_TEMPLATE_PARM:
744fac59 4256 /* Record template parameters such as `T' inside `TT<T>'. */
7ddedda4
MM
4257 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)
4258 && for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
744fac59
KL
4259 return 1;
4260 case TEMPLATE_TYPE_PARM:
f84b4be9 4261 case TEMPLATE_PARM_INDEX:
050367a3
MM
4262 if (fn)
4263 return (*fn)(t, data);
4264 else
4265 return 1;
8d08fdba
MS
4266
4267 /* simple type nodes */
4268 case INTEGER_TYPE:
050367a3 4269 if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
8d08fdba 4270 return 1;
050367a3 4271 return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
8d08fdba
MS
4272
4273 case REAL_TYPE:
37c46b43 4274 case COMPLEX_TYPE:
8d08fdba 4275 case VOID_TYPE:
2986ae00 4276 case BOOLEAN_TYPE:
2c73f9f5 4277 case NAMESPACE_DECL:
4699c561 4278 case FIELD_DECL:
8d08fdba
MS
4279 return 0;
4280
4281 /* constants */
4282 case INTEGER_CST:
4283 case REAL_CST:
4284 case STRING_CST:
4285 return 0;
4286
4287 case ERROR_MARK:
4288 /* Non-error_mark_node ERROR_MARKs are bad things. */
4289 my_friendly_assert (t == error_mark_node, 274);
4290 /* NOTREACHED */
4291 return 0;
4292
61a127b3
MM
4293 case PTRMEM_CST:
4294 return for_each_template_parm (TREE_TYPE (t), fn, data);
4295
5156628f 4296 case SCOPE_REF:
050367a3 4297 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
5156628f 4298
db5ae43f
MS
4299 case CONSTRUCTOR:
4300 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
050367a3
MM
4301 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
4302 (TREE_TYPE (t)), fn, data);
4303 return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
db5ae43f 4304
b8865407
MM
4305 case SIZEOF_EXPR:
4306 case ALIGNOF_EXPR:
4307 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
4308
4699c561
MM
4309 case TYPENAME_TYPE:
4310 if (!fn)
4311 return 1;
4312 return (for_each_template_parm (TYPE_CONTEXT (t), fn, data)
4313 || for_each_template_parm (TYPENAME_TYPE_FULLNAME (t),
4314 fn, data));
4315
b8865407
MM
4316 case INDIRECT_REF:
4317 case COMPONENT_REF:
4699c561 4318 /* If there's no type, then this thing must be some expression
b8865407 4319 involving template parameters. */
4699c561
MM
4320 if (!fn && !TREE_TYPE (t))
4321 return 1;
4322 if (TREE_CODE (t) == COMPONENT_REF)
4323 return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
4324 || for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
4325 else
4326 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
b8865407 4327
42976354
BK
4328 case MODOP_EXPR:
4329 case CAST_EXPR:
4330 case REINTERPRET_CAST_EXPR:
4331 case CONST_CAST_EXPR:
4332 case STATIC_CAST_EXPR:
4333 case DYNAMIC_CAST_EXPR:
42976354
BK
4334 case ARROW_EXPR:
4335 case DOTSTAR_EXPR:
4336 case TYPEID_EXPR:
b8865407 4337 case LOOKUP_EXPR:
40242ccf 4338 case PSEUDO_DTOR_EXPR:
b8865407
MM
4339 if (!fn)
4340 return 1;
4341 /* Fall through. */
abff8e06 4342
8d08fdba
MS
4343 default:
4344 switch (TREE_CODE_CLASS (TREE_CODE (t)))
4345 {
4346 case '1':
4347 case '2':
ec255269 4348 case 'e':
8d08fdba
MS
4349 case '<':
4350 {
4351 int i;
bbc06b4b 4352 for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;)
050367a3 4353 if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
8d08fdba
MS
4354 return 1;
4355 return 0;
4356 }
4357 default:
4358 break;
4359 }
4360 sorry ("testing %s for template parms",
4361 tree_code_name [(int) TREE_CODE (t)]);
4362 my_friendly_abort (82);
4363 /* NOTREACHED */
4364 return 0;
4365 }
4366}
4367
050367a3
MM
4368int
4369uses_template_parms (t)
4370 tree t;
4371{
4372 return for_each_template_parm (t, 0, 0);
4373}
4374
27fafc8d
JM
4375static struct tinst_level *current_tinst_level;
4376static struct tinst_level *free_tinst_level;
4377static int tinst_depth;
e9f32eb5 4378extern int max_tinst_depth;
5566b478 4379#ifdef GATHER_STATISTICS
27fafc8d 4380int depth_reached;
5566b478 4381#endif
27fafc8d
JM
4382int tinst_level_tick;
4383int last_template_error_tick;
8d08fdba 4384
7bae46f4 4385/* Print out all the template instantiations that we are currently
27fafc8d
JM
4386 working on. If ERR, we are being called from cp_thing, so do
4387 the right thing for an error message. */
7bae46f4 4388
27fafc8d
JM
4389static void
4390print_template_context (err)
4391 int err;
7bae46f4
JM
4392{
4393 struct tinst_level *p = current_tinst_level;
4394 int line = lineno;
4395 char *file = input_filename;
4396
49e04385 4397 if (err && p)
27fafc8d 4398 {
49e04385
MM
4399 if (current_function_decl != p->decl
4400 && current_function_decl != NULL_TREE)
4401 /* We can get here during the processing of some synthesized
4402 method. Then, p->decl will be the function that's causing
4403 the synthesis. */
4404 ;
27fafc8d 4405 else
27fafc8d 4406 {
49e04385
MM
4407 if (current_function_decl == p->decl)
4408 /* Avoid redundancy with the the "In function" line. */;
4409 else
4410 fprintf (stderr, "%s: In instantiation of `%s':\n",
4411 file, decl_as_string (p->decl, 0));
4412
038fb86f
JM
4413 line = p->line;
4414 file = p->file;
27fafc8d
JM
4415 p = p->next;
4416 }
4417 }
4418
7bae46f4
JM
4419 for (; p; p = p->next)
4420 {
038fb86f
JM
4421 fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line,
4422 decl_as_string (p->decl, 0));
4423 line = p->line;
4424 file = p->file;
7bae46f4 4425 }
038fb86f 4426 fprintf (stderr, "%s:%d: instantiated from here\n", file, line);
7bae46f4
JM
4427}
4428
27fafc8d
JM
4429/* Called from cp_thing to print the template context for an error. */
4430
4431void
4432maybe_print_template_context ()
4433{
4434 if (last_template_error_tick == tinst_level_tick
4435 || current_tinst_level == 0)
4436 return;
4437
4438 last_template_error_tick = tinst_level_tick;
4439 print_template_context (1);
4440}
4441
bd6dd845 4442static int
5566b478
MS
4443push_tinst_level (d)
4444 tree d;
8d08fdba
MS
4445{
4446 struct tinst_level *new;
8d08fdba 4447
7215f9a0
MS
4448 if (tinst_depth >= max_tinst_depth)
4449 {
8adf5b5e
JM
4450 /* If the instantiation in question still has unbound template parms,
4451 we don't really care if we can't instantiate it, so just return.
4452 This happens with base instantiation for implicit `typename'. */
4453 if (uses_template_parms (d))
4454 return 0;
4455
1139b3d8 4456 last_template_error_tick = tinst_level_tick;
8251199e 4457 error ("template instantiation depth exceeds maximum of %d",
7215f9a0 4458 max_tinst_depth);
8251199e
JM
4459 error (" (use -ftemplate-depth-NN to increase the maximum)");
4460 cp_error (" instantiating `%D'", d);
5566b478 4461
27fafc8d 4462 print_template_context (0);
5566b478 4463
7215f9a0
MS
4464 return 0;
4465 }
4466
8d08fdba
MS
4467 if (free_tinst_level)
4468 {
4469 new = free_tinst_level;
4470 free_tinst_level = new->next;
4471 }
4472 else
4473 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
4474
5566b478
MS
4475 new->decl = d;
4476 new->line = lineno;
4477 new->file = input_filename;
8d08fdba
MS
4478 new->next = current_tinst_level;
4479 current_tinst_level = new;
5566b478 4480
7215f9a0 4481 ++tinst_depth;
5566b478
MS
4482#ifdef GATHER_STATISTICS
4483 if (tinst_depth > depth_reached)
4484 depth_reached = tinst_depth;
4485#endif
4486
27fafc8d 4487 ++tinst_level_tick;
7215f9a0 4488 return 1;
8d08fdba
MS
4489}
4490
4491void
4492pop_tinst_level ()
4493{
4494 struct tinst_level *old = current_tinst_level;
4495
ae58fa02
MM
4496 /* Restore the filename and line number stashed away when we started
4497 this instantiation. */
4498 lineno = old->line;
4499 input_filename = old->file;
5f2c99c4 4500 extract_interface_info ();
ae58fa02 4501
8d08fdba
MS
4502 current_tinst_level = old->next;
4503 old->next = free_tinst_level;
4504 free_tinst_level = old;
7215f9a0 4505 --tinst_depth;
27fafc8d 4506 ++tinst_level_tick;
8d08fdba
MS
4507}
4508
4509struct tinst_level *
4510tinst_for_decl ()
4511{
4512 struct tinst_level *p = current_tinst_level;
4513
4514 if (p)
4515 for (; p->next ; p = p->next )
4516 ;
4517 return p;
4518}
4519
f84b4be9
JM
4520/* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
4521 vector of template arguments, as for tsubst.
4522
4523 Returns an appropriate tsbust'd friend declaration. */
4524
4525static tree
4526tsubst_friend_function (decl, args)
4527 tree decl;
4528 tree args;
4529{
4530 tree new_friend;
27fafc8d
JM
4531 int line = lineno;
4532 char *file = input_filename;
4533
4534 lineno = DECL_SOURCE_LINE (decl);
4535 input_filename = DECL_SOURCE_FILE (decl);
4536
f84b4be9
JM
4537 if (TREE_CODE (decl) == FUNCTION_DECL
4538 && DECL_TEMPLATE_INSTANTIATION (decl)
4539 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
4540 /* This was a friend declared with an explicit template
4541 argument list, e.g.:
4542
4543 friend void f<>(T);
4544
4545 to indicate that f was a template instantiation, not a new
4546 function declaration. Now, we have to figure out what
4547 instantiation of what template. */
4548 {
4549 tree template_id;
4550 tree new_args;
4551 tree tmpl;
f84b4be9
JM
4552
4553 template_id
4554 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
4393e105
MM
4555 args, /*complain=*/1,
4556 NULL_TREE),
f84b4be9 4557 tsubst (DECL_TI_ARGS (decl),
4393e105
MM
4558 args, /*complain=*/1,
4559 NULL_TREE));
39c01e4c
MM
4560 /* FIXME: The decl we create via the next tsubst could be
4561 created on a temporary obstack. */
4393e105 4562 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
36a117a5
MM
4563 tmpl = determine_specialization (template_id, new_friend,
4564 &new_args,
bf8f3f93 4565 /*need_member_template=*/0);
27fafc8d
JM
4566 new_friend = instantiate_template (tmpl, new_args);
4567 goto done;
f84b4be9 4568 }
36a117a5 4569
4393e105 4570 new_friend = tsubst (decl, args, /*complain=*/1, NULL_TREE);
f84b4be9 4571
36a117a5 4572 /* The NEW_FRIEND will look like an instantiation, to the
f84b4be9
JM
4573 compiler, but is not an instantiation from the point of view of
4574 the language. For example, we might have had:
4575
4576 template <class T> struct S {
4577 template <class U> friend void f(T, U);
4578 };
4579
4580 Then, in S<int>, template <class U> void f(int, U) is not an
4581 instantiation of anything. */
4582 DECL_USE_TEMPLATE (new_friend) = 0;
4583 if (TREE_CODE (decl) == TEMPLATE_DECL)
4584 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
36a117a5
MM
4585
4586 /* The mangled name for the NEW_FRIEND is incorrect. The call to
4587 tsubst will have resulted in a call to
4588 set_mangled_name_for_template_decl. But, the function is not a
4589 template instantiation and should not be mangled like one.
4590 Therefore, we remangle the function name. We don't have to do
4591 this if the NEW_FRIEND is a template since
4592 set_mangled_name_for_template_decl doesn't do anything if the
4593 function declaration still uses template arguments. */
4594 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
4595 {
4596 set_mangled_name_for_decl (new_friend);
4597 DECL_RTL (new_friend) = 0;
4598 make_decl_rtl (new_friend, NULL_PTR, 1);
4599 }
4600
6eb3bb27 4601 if (DECL_NAMESPACE_SCOPE_P (new_friend))
f84b4be9 4602 {
36a117a5 4603 tree old_decl;
fbf1c34b
MM
4604 tree new_friend_template_info;
4605 tree new_friend_result_template_info;
92da7074 4606 tree ns;
fbf1c34b
MM
4607 int new_friend_is_defn;
4608
4609 /* We must save some information from NEW_FRIEND before calling
4610 duplicate decls since that function will free NEW_FRIEND if
4611 possible. */
4612 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
f84b4be9 4613 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
fbf1c34b
MM
4614 {
4615 /* This declaration is a `primary' template. */
4616 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
4617
4618 new_friend_is_defn
4619 = DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE;
4620 new_friend_result_template_info
4621 = DECL_TEMPLATE_INFO (DECL_RESULT (new_friend));
4622 }
4623 else
4624 {
4625 new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
4626 new_friend_result_template_info = NULL_TREE;
4627 }
36a117a5 4628
92da7074
ML
4629 /* Inside pushdecl_namespace_level, we will push into the
4630 current namespace. However, the friend function should
4631 tyically go into the namespace of the template. */
4632 ns = decl_namespace_context (new_friend);
4633 push_nested_namespace (ns);
36a117a5 4634 old_decl = pushdecl_namespace_level (new_friend);
92da7074 4635 pop_nested_namespace (ns);
36a117a5
MM
4636
4637 if (old_decl != new_friend)
4638 {
4639 /* This new friend declaration matched an existing
4640 declaration. For example, given:
4641
4642 template <class T> void f(T);
4643 template <class U> class C {
4644 template <class T> friend void f(T) {}
4645 };
4646
4647 the friend declaration actually provides the definition
4648 of `f', once C has been instantiated for some type. So,
4649 old_decl will be the out-of-class template declaration,
4650 while new_friend is the in-class definition.
4651
4652 But, if `f' was called before this point, the
4653 instantiation of `f' will have DECL_TI_ARGS corresponding
4654 to `T' but not to `U', references to which might appear
4655 in the definition of `f'. Previously, the most general
4656 template for an instantiation of `f' was the out-of-class
4657 version; now it is the in-class version. Therefore, we
4658 run through all specialization of `f', adding to their
4659 DECL_TI_ARGS appropriately. In particular, they need a
4660 new set of outer arguments, corresponding to the
4661 arguments for this class instantiation.
4662
4663 The same situation can arise with something like this:
4664
4665 friend void f(int);
4666 template <class T> class C {
4667 friend void f(T) {}
4668 };
4669
4670 when `C<int>' is instantiated. Now, `f(int)' is defined
4671 in the class. */
4672
fbf1c34b
MM
4673 if (!new_friend_is_defn)
4674 /* On the other hand, if the in-class declaration does
4675 *not* provide a definition, then we don't want to alter
4676 existing definitions. We can just leave everything
4677 alone. */
36a117a5 4678 ;
fbf1c34b 4679 else
36a117a5 4680 {
fbf1c34b
MM
4681 /* Overwrite whatever template info was there before, if
4682 any, with the new template information pertaining to
4683 the declaration. */
4684 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
4685
4686 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
4687 /* duplicate_decls will take care of this case. */
4688 ;
4689 else
36a117a5 4690 {
fbf1c34b
MM
4691 tree t;
4692 tree new_friend_args;
4693
4694 DECL_TEMPLATE_INFO (DECL_RESULT (old_decl))
4695 = new_friend_result_template_info;
4696
4697 new_friend_args = TI_ARGS (new_friend_template_info);
4698 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
4699 t != NULL_TREE;
4700 t = TREE_CHAIN (t))
4701 {
4702 tree spec = TREE_VALUE (t);
36a117a5 4703
fbf1c34b
MM
4704 DECL_TI_ARGS (spec)
4705 = add_outermost_template_args (new_friend_args,
4706 DECL_TI_ARGS (spec));
fbf1c34b
MM
4707 }
4708
4709 /* Now, since specializations are always supposed to
4710 hang off of the most general template, we must move
4711 them. */
4712 t = most_general_template (old_decl);
4713 if (t != old_decl)
4714 {
4715 DECL_TEMPLATE_SPECIALIZATIONS (t)
4716 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
4717 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
4718 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
4719 }
36a117a5
MM
4720 }
4721 }
4722
4723 /* The information from NEW_FRIEND has been merged into OLD_DECL
4724 by duplicate_decls. */
4725 new_friend = old_decl;
4726 }
f84b4be9
JM
4727 }
4728 else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
4729 {
4730 /* Check to see that the declaration is really present, and,
4731 possibly obtain an improved declaration. */
4732 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4733 new_friend);
4734
4735 if (fn)
4736 new_friend = fn;
4737 }
4738
27fafc8d
JM
4739 done:
4740 lineno = line;
4741 input_filename = file;
f84b4be9
JM
4742 return new_friend;
4743}
4744
1aed5355
MM
4745/* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4746 template arguments, as for tsubst.
6757edfe
MM
4747
4748 Returns an appropriate tsbust'd friend type. */
4749
4750static tree
1aed5355
MM
4751tsubst_friend_class (friend_tmpl, args)
4752 tree friend_tmpl;
6757edfe
MM
4753 tree args;
4754{
1aed5355 4755 tree friend_type;
25aab5d0 4756 tree tmpl;
6757edfe 4757
25aab5d0
MM
4758 /* First, we look for a class template. */
4759 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/0);
4760
4761 /* But, if we don't find one, it might be because we're in a
4762 situation like this:
4763
4764 template <class T>
4765 struct S {
4766 template <class U>
4767 friend struct S;
4768 };
4769
4770 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
4771 for `S<int>', not the TEMPLATE_DECL. */
bc639f90 4772 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
25aab5d0
MM
4773 {
4774 tmpl = lookup_name (DECL_NAME (friend_tmpl), /*prefer_type=*/1);
4775 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4776 }
6757edfe 4777
25aab5d0 4778 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
6757edfe
MM
4779 {
4780 /* The friend template has already been declared. Just
36a117a5
MM
4781 check to see that the declarations match, and install any new
4782 default parameters. We must tsubst the default parameters,
4783 of course. We only need the innermost template parameters
4784 because that is all that redeclare_class_template will look
4785 at. */
4786 tree parms
4787 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4393e105 4788 args, /*complain=*/1);
36a117a5 4789 redeclare_class_template (TREE_TYPE (tmpl), parms);
6757edfe
MM
4790 friend_type = TREE_TYPE (tmpl);
4791 }
4792 else
4793 {
4794 /* The friend template has not already been declared. In this
4795 case, the instantiation of the template class will cause the
4796 injection of this template into the global scope. */
4393e105 4797 tmpl = tsubst (friend_tmpl, args, /*complain=*/1, NULL_TREE);
6757edfe
MM
4798
4799 /* The new TMPL is not an instantiation of anything, so we
4800 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4801 the new type because that is supposed to be the corresponding
4802 template decl, i.e., TMPL. */
4803 DECL_USE_TEMPLATE (tmpl) = 0;
4804 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4805 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4806
4807 /* Inject this template into the global scope. */
4808 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4809 }
4810
4811 return friend_type;
4812}
f84b4be9 4813
8d08fdba 4814tree
5566b478
MS
4815instantiate_class_template (type)
4816 tree type;
8d08fdba 4817{
61a127b3 4818 tree template, args, pattern, t;
36a117a5 4819 tree typedecl;
8d08fdba 4820
5566b478 4821 if (type == error_mark_node)
8d08fdba
MS
4822 return error_mark_node;
4823
5566b478
MS
4824 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
4825 return type;
4826
7ddedda4
MM
4827 /* We want to allocate temporary vectors of template arguments and
4828 template argument expressions on the momentary obstack, not on
4829 the expression obstack. Otherwise, all the space allocated in
4830 argument coercion and such is simply lost. */
4831 push_momentary ();
4832
6bdb985f 4833 /* Figure out which template is being instantiated. */
36a117a5 4834 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
5566b478 4835 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
73aad9b9 4836
6bdb985f
MM
4837 /* Figure out which arguments are being used to do the
4838 instantiation. */
4839 args = CLASSTYPE_TI_ARGS (type);
4c571114 4840 PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
6bdb985f 4841
4c571114
MM
4842 if (pedantic && PARTIAL_INSTANTIATION_P (type))
4843 /* If this is a partial instantiation, then we can't instantiate
4844 the type; there's no telling whether or not one of the
4845 template parameters might eventually be instantiated to some
4846 value that results in a specialization being used. For
4847 example, consider:
4848
4849 template <class T>
4850 struct S {};
4851
4852 template <class U>
4853 void f(S<U>);
4854
4855 template <>
4856 struct S<int> {};
4857
4858 Now, the `S<U>' in `f<int>' is the specialization, not an
5db698f6 4859 instantiation of the original template. */
4c571114
MM
4860 goto end;
4861
4862 /* Determine what specialization of the original template to
4863 instantiate. */
4864 if (PARTIAL_INSTANTIATION_P (type))
6bdb985f
MM
4865 /* There's no telling which specialization is appropriate at this
4866 point. Since all peeking at the innards of this partial
4867 instantiation are extensions (like the "implicit typename"
4868 extension, which allows users to omit the keyword `typename' on
4869 names that are declared as types in template base classes), we
4870 are free to do what we please.
4871
4872 Trying to figure out which partial instantiation to use can
4873 cause a crash. (Some of the template arguments don't even have
4874 types.) So, we just use the most general version. */
4875 t = NULL_TREE;
4876 else
73aad9b9 4877 {
6bdb985f
MM
4878 t = most_specialized_class (template, args);
4879
4880 if (t == error_mark_node)
73aad9b9 4881 {
d8e178a0 4882 const char *str = "candidates are:";
6bdb985f
MM
4883 cp_error ("ambiguous class template instantiation for `%#T'", type);
4884 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
4885 t = TREE_CHAIN (t))
73aad9b9 4886 {
6bdb985f
MM
4887 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4888 args))
4889 {
4890 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4891 str = " ";
4892 }
73aad9b9 4893 }
6bdb985f
MM
4894 TYPE_BEING_DEFINED (type) = 1;
4895 type = error_mark_node;
4896 goto end;
73aad9b9 4897 }
73aad9b9 4898 }
6bdb985f
MM
4899
4900 if (t)
73aad9b9
JM
4901 pattern = TREE_TYPE (t);
4902 else
4903 pattern = TREE_TYPE (template);
5566b478 4904
4c571114
MM
4905 /* If the template we're instantiating is incomplete, then clearly
4906 there's nothing we can do. */
5566b478 4907 if (TYPE_SIZE (pattern) == NULL_TREE)
7ddedda4 4908 goto end;
5566b478 4909
4c571114
MM
4910 /* If this is a partial instantiation, don't tsubst anything. We will
4911 only use this type for implicit typename, so the actual contents don't
4912 matter. All that matters is whether a particular name is a type. */
4913 if (PARTIAL_INSTANTIATION_P (type))
4914 {
4915 /* The fields set here must be kept in sync with those cleared
4916 in begin_class_definition. */
4917 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4918 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4919 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4920 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4921 /* Pretend that the type is complete, so that we will look
4922 inside it during name lookup and such. */
4923 TYPE_SIZE (type) = integer_zero_node;
4924 goto end;
4925 }
4926
4927 /* If we've recursively instantiated too many templates, stop. */
4928 if (! push_tinst_level (type))
4929 goto end;
4930
4931 /* Now we're really doing the instantiation. Mark the type as in
4932 the process of being defined. */
4933 TYPE_BEING_DEFINED (type) = 1;
4934
4935 maybe_push_to_top_level (uses_template_parms (type));
4c571114 4936
73aad9b9 4937 if (t)
36a117a5
MM
4938 {
4939 /* This TYPE is actually a instantiation of of a partial
4940 specialization. We replace the innermost set of ARGS with
4941 the arguments appropriate for substitution. For example,
4942 given:
4943
4944 template <class T> struct S {};
4945 template <class T> struct S<T*> {};
4946
4947 and supposing that we are instantiating S<int*>, ARGS will
4948 present be {int*} but we need {int}. */
4949 tree inner_args
4950 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4951 args);
4952
4953 /* If there were multiple levels in ARGS, replacing the
4954 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4955 want, so we make a copy first. */
4956 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4957 {
4958 args = copy_node (args);
4959 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4960 }
4961 else
4962 args = inner_args;
4963 }
8d019cef 4964
5566b478
MS
4965 if (flag_external_templates)
4966 {
4967 if (flag_alt_external_templates)
4968 {
4969 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
4970 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
4971 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
2604412d
JM
4972 = (! CLASSTYPE_INTERFACE_ONLY (type)
4973 && CLASSTYPE_INTERFACE_KNOWN (type));
5566b478
MS
4974 }
4975 else
4976 {
4977 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
4978 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4979 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
4980 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
2604412d
JM
4981 = (! CLASSTYPE_INTERFACE_ONLY (type)
4982 && CLASSTYPE_INTERFACE_KNOWN (type));
5566b478
MS
4983 }
4984 }
4985 else
8d08fdba 4986 {
5566b478
MS
4987 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
4988 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
8d08fdba
MS
4989 }
4990
f7da6097
MS
4991 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
4992 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
f7da6097
MS
4993 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
4994 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
4995 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
4996 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
4997 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
4998 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
4999 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
5000 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
5001 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
5002 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
5003 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
f7da6097
MS
5004 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
5005 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
5006 TYPE_USES_COMPLEX_INHERITANCE (type)
5007 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
5008 TYPE_USES_MULTIPLE_INHERITANCE (type)
5009 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
5010 TYPE_USES_VIRTUAL_BASECLASSES (type)
5011 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
5012 TYPE_PACKED (type) = TYPE_PACKED (pattern);
5013 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
eff71ab0 5014 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
6bdb8141
JM
5015 if (ANON_AGGR_TYPE_P (pattern))
5016 SET_ANON_AGGR_TYPE_P (type);
f7da6097 5017
3fd71a52
MM
5018 if (TYPE_BINFO_BASETYPES (pattern))
5019 {
5020 tree base_list = NULL_TREE;
5021 tree pbases = TYPE_BINFO_BASETYPES (pattern);
5022 int i;
5566b478 5023
3fd71a52
MM
5024 /* Substitute into each of the bases to determine the actual
5025 basetypes. */
5026 for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
5027 {
5028 tree base;
5029 tree access;
5030 tree pbase;
5566b478 5031
3fd71a52 5032 pbase = TREE_VEC_ELT (pbases, i);
711734a9 5033
3fd71a52
MM
5034 /* Substitue to figure out the base class. */
5035 base = tsubst (BINFO_TYPE (pbase), args,
5036 /*complain=*/1, NULL_TREE);
5037 if (base == error_mark_node)
5038 continue;
711734a9 5039
3fd71a52
MM
5040 /* Calculate the correct access node. */
5041 if (TREE_VIA_VIRTUAL (pbase))
5042 {
5043 if (TREE_VIA_PUBLIC (pbase))
5044 access = access_public_virtual_node;
5045 else if (TREE_VIA_PROTECTED (pbase))
5046 access = access_protected_virtual_node;
d6479fe7 5047 else
3fd71a52
MM
5048 access = access_private_virtual_node;
5049 }
5050 else
5051 {
5052 if (TREE_VIA_PUBLIC (pbase))
5053 access = access_public_node;
5054 else if (TREE_VIA_PROTECTED (pbase))
5055 access = access_protected_node;
d6479fe7 5056 else
3fd71a52
MM
5057 access = access_private_node;
5058 }
dfbcd65a 5059
3fd71a52
MM
5060 base_list = tree_cons (access, base, base_list);
5061 }
dfbcd65a 5062
3fd71a52
MM
5063 /* The list is now in reverse order; correct that. */
5064 base_list = nreverse (base_list);
5065
5066 /* Now call xref_basetypes to set up all the base-class
5067 information. */
5068 xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE
5069 ? (CLASSTYPE_DECLARED_CLASS (pattern)
5070 ? class_type_node : record_type_node)
5071 : union_type_node,
5072 DECL_NAME (TYPE_NAME (pattern)),
5073 type,
5074 base_list);
5075 }
5566b478 5076
b74a0560
MM
5077 /* Now that our base classes are set up, enter the scope of the
5078 class, so that name lookups into base classes, etc. will work
5079 corectly. This is precisely analagous to what we do in
5080 begin_class_definition when defining an ordinary non-template
5081 class. */
5082 pushclass (type, 1);
5083
5566b478 5084 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
8d08fdba 5085 {
5566b478 5086 tree tag = TREE_VALUE (t);
36a117a5
MM
5087 tree name = TYPE_IDENTIFIER (tag);
5088 tree newtag;
5566b478 5089
4393e105 5090 newtag = tsubst (tag, args, /*complain=*/1, NULL_TREE);
61a127b3 5091 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
ed44da02 5092 {
486e4077
MM
5093 if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
5094 /* Unfortunately, lookup_template_class sets
5095 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
5096 instantiation (i.e., for the type of a member template
5097 class nested within a template class.) This behavior is
5098 required for maybe_process_partial_specialization to work
5099 correctly, but is not accurate in this case; the TAG is not
5100 an instantiation of anything. (The corresponding
5101 TEMPLATE_DECL is an instantiation, but the TYPE is not.) */
5102 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
5103
ed44da02
MM
5104 /* Now, we call pushtag to put this NEWTAG into the scope of
5105 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
5106 pushtag calling push_template_decl. We don't have to do
5107 this for enums because it will already have been done in
5108 tsubst_enum. */
5109 if (name)
5110 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
5111 pushtag (name, newtag, /*globalize=*/0);
5112 }
8d08fdba
MS
5113 }
5114
5566b478
MS
5115 /* Don't replace enum constants here. */
5116 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
8dd3f57a 5117 if (TREE_CODE (t) != CONST_DECL)
5566b478 5118 {
ae58fa02
MM
5119 tree r;
5120
5121 /* The the file and line for this declaration, to assist in
5122 error message reporting. Since we called push_tinst_level
5123 above, we don't need to restore these. */
5124 lineno = DECL_SOURCE_LINE (t);
5125 input_filename = DECL_SOURCE_FILE (t);
5126
4393e105 5127 r = tsubst (t, args, /*complain=*/1, NULL_TREE);
5566b478
MS
5128 if (TREE_CODE (r) == VAR_DECL)
5129 {
fa8d6e85
MM
5130 tree init;
5131
94c82a77 5132 if (DECL_DEFINED_IN_CLASS_P (r))
fa8d6e85
MM
5133 init = tsubst_expr (DECL_INITIAL (t), args,
5134 /*complain=*/1, NULL_TREE);
5135 else
5136 init = NULL_TREE;
5137
5138 finish_static_data_member_decl (r, init,
5139 /*asmspec_tree=*/NULL_TREE,
5140 /*need_pop=*/0,
5141 /*flags=*/0);
5142
6ba89f8e
MM
5143 if (DECL_DEFINED_IN_CLASS_P (r))
5144 check_static_variable_definition (r, TREE_TYPE (r));
5566b478 5145 }
61a127b3
MM
5146
5147 /* R will have a TREE_CHAIN if and only if it has already been
5148 processed by finish_member_declaration. This can happen
5149 if, for example, it is a TYPE_DECL for a class-scoped
5150 ENUMERAL_TYPE; such a thing will already have been added to
5151 the field list by tsubst_enum above. */
5152 if (!TREE_CHAIN (r))
5153 {
5154 set_current_access_from_decl (r);
5155 finish_member_declaration (r);
5156 }
5566b478 5157 }
8d08fdba 5158
61a127b3
MM
5159 /* Set up the list (TYPE_METHODS) and vector (CLASSTYPE_METHOD_VEC)
5160 for this instantiation. */
5161 for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
5162 {
4393e105 5163 tree r = tsubst (t, args, /*complain=*/1, NULL_TREE);
61a127b3
MM
5164 set_current_access_from_decl (r);
5165 finish_member_declaration (r);
5166 }
8d08fdba 5167
2604412d
JM
5168 /* Construct the DECL_FRIENDLIST for the new class type. */
5169 typedecl = TYPE_MAIN_DECL (type);
5170 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
5171 t != NULL_TREE;
5172 t = TREE_CHAIN (t))
f84b4be9 5173 {
2604412d 5174 tree friends;
f84b4be9 5175
2604412d
JM
5176 for (friends = TREE_VALUE (t);
5177 friends != NULL_TREE;
5178 friends = TREE_CHAIN (friends))
0c0aac2f
MM
5179 if (TREE_PURPOSE (friends) == error_mark_node)
5180 add_friend (type,
5181 tsubst_friend_function (TREE_VALUE (friends),
5182 args));
5183 else
5184 add_friends (type,
5185 tsubst_copy (TREE_PURPOSE (t), args,
5186 /*complain=*/1, NULL_TREE),
5187 tsubst (TREE_PURPOSE (friends), args,
5188 /*complain=*/1, NULL_TREE));
2604412d 5189 }
5566b478 5190
6757edfe
MM
5191 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
5192 t != NULL_TREE;
5193 t = TREE_CHAIN (t))
5194 {
5195 tree friend_type = TREE_VALUE (t);
1aed5355 5196 tree new_friend_type;
6757edfe 5197
36a117a5
MM
5198 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5199 new_friend_type = tsubst_friend_class (friend_type, args);
5200 else if (uses_template_parms (friend_type))
4393e105
MM
5201 new_friend_type = tsubst (friend_type, args, /*complain=*/1,
5202 NULL_TREE);
36a117a5 5203 else
61fc8c9e
MM
5204 {
5205 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
36a117a5 5206
61fc8c9e
MM
5207 /* The call to xref_tag_from_type does injection for friend
5208 classes. */
5209 push_nested_namespace (ns);
5210 new_friend_type =
5211 xref_tag_from_type (friend_type, NULL_TREE, 1);
5212 pop_nested_namespace (ns);
5213 }
1aed5355
MM
5214
5215 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5216 /* Trick make_friend_class into realizing that the friend
5217 we're adding is a template, not an ordinary class. It's
5218 important that we use make_friend_class since it will
5219 perform some error-checking and output cross-reference
5220 information. */
5221 ++processing_template_decl;
5222
5223 make_friend_class (type, new_friend_type);
fc378698 5224
1aed5355
MM
5225 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
5226 --processing_template_decl;
6757edfe 5227 }
fc378698 5228
2604412d
JM
5229 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
5230 if (TREE_CODE (t) == FIELD_DECL)
5231 {
5232 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
5233 require_complete_type (t);
5234 }
5566b478 5235
61a127b3
MM
5236 /* Set the file and line number information to whatever is given for
5237 the class itself. This puts error messages involving generated
5238 implicit functions at a predictable point, and the same point
5239 that would be used for non-template classes. */
5240 lineno = DECL_SOURCE_LINE (typedecl);
5241 input_filename = DECL_SOURCE_FILE (typedecl);
5242
5243 unreverse_member_declarations (type);
9f33663b 5244 finish_struct_1 (type);
2604412d 5245 CLASSTYPE_GOT_SEMICOLON (type) = 1;
e92cc029 5246
5524676d
JM
5247 /* Clear this now so repo_template_used is happy. */
5248 TYPE_BEING_DEFINED (type) = 0;
2604412d 5249 repo_template_used (type);
8d08fdba 5250
9188c363
MM
5251 /* Now that the class is complete, instantiate default arguments for
5252 any member functions. We don't do this earlier because the
5253 default arguments may reference members of the class. */
5254 if (!PRIMARY_TEMPLATE_P (template))
5255 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
5256 if (TREE_CODE (t) == FUNCTION_DECL
5257 /* Implicitly generated member functions will not have tmplate
5258 information; they are not instantiations, but instead are
5259 created "fresh" for each instantiation. */
5260 && DECL_TEMPLATE_INFO (t))
5261 tsubst_default_arguments (t);
5262
b74a0560 5263 popclass ();
5566b478
MS
5264 pop_from_top_level ();
5265 pop_tinst_level ();
5266
7ddedda4
MM
5267 end:
5268 pop_momentary ();
5269
5566b478 5270 return type;
8d08fdba
MS
5271}
5272
5273static int
5274list_eq (t1, t2)
5275 tree t1, t2;
5276{
5277 if (t1 == NULL_TREE)
5278 return t2 == NULL_TREE;
5279 if (t2 == NULL_TREE)
5280 return 0;
5281 /* Don't care if one declares its arg const and the other doesn't -- the
5282 main variant of the arg type is all that matters. */
5283 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
5284 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
5285 return 0;
5286 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
5287}
5288
00d3396f
JM
5289/* If arg is a non-type template parameter that does not depend on template
5290 arguments, fold it like we weren't in the body of a template. */
5291
5292static tree
5293maybe_fold_nontype_arg (arg)
5294 tree arg;
5295{
5296 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
5297 && !uses_template_parms (arg))
5298 {
5299 /* Sometimes, one of the args was an expression involving a
5300 template constant parameter, like N - 1. Now that we've
5301 tsubst'd, we might have something like 2 - 1. This will
5302 confuse lookup_template_class, so we do constant folding
5303 here. We have to unset processing_template_decl, to
5304 fool build_expr_from_tree() into building an actual
5305 tree. */
5306
5307 int saved_processing_template_decl = processing_template_decl;
5308 processing_template_decl = 0;
5309 arg = fold (build_expr_from_tree (arg));
5310 processing_template_decl = saved_processing_template_decl;
5311 }
5312 return arg;
5313}
5314
a221d52f 5315/* Return the TREE_VEC with the arguments for the innermost template header,
36a117a5
MM
5316 where ARGS is either that or the VEC of VECs for all the
5317 arguments. */
a221d52f
JM
5318
5319tree
36a117a5 5320innermost_args (args)
a221d52f 5321 tree args;
a221d52f 5322{
36a117a5 5323 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
a221d52f 5324}
f84b4be9 5325
830bfa74
MM
5326/* Substitute ARGS into the vector of template arguments T. */
5327
e9659ab0 5328static tree
4393e105 5329tsubst_template_arg_vector (t, args, complain)
830bfa74
MM
5330 tree t;
5331 tree args;
4393e105 5332 int complain;
830bfa74
MM
5333{
5334 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
5335 tree *elts = (tree *) alloca (len * sizeof (tree));
5336
5337 bzero ((char *) elts, len * sizeof (tree));
5338
5339 for (i = 0; i < len; i++)
5340 {
5341 if (TREE_VEC_ELT (t, i) != NULL_TREE
5342 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
4393e105
MM
5343 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
5344 args, complain);
830bfa74
MM
5345 else
5346 elts[i] = maybe_fold_nontype_arg
4393e105
MM
5347 (tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
5348 NULL_TREE));
830bfa74
MM
5349
5350 if (elts[i] != TREE_VEC_ELT (t, i))
5351 need_new = 1;
5352 }
5353
5354 if (!need_new)
5355 return t;
5356
7ddedda4 5357 t = make_temp_vec (len);
830bfa74
MM
5358 for (i = 0; i < len; i++)
5359 TREE_VEC_ELT (t, i) = elts[i];
5360
5361 return t;
5362}
5363
36a117a5
MM
5364/* Return the result of substituting ARGS into the template parameters
5365 given by PARMS. If there are m levels of ARGS and m + n levels of
5366 PARMS, then the result will contain n levels of PARMS. For
5367 example, if PARMS is `template <class T> template <class U>
5368 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
5369 result will be `template <int*, double, class V>'. */
5370
e9659ab0 5371static tree
4393e105 5372tsubst_template_parms (parms, args, complain)
36a117a5
MM
5373 tree parms;
5374 tree args;
4393e105 5375 int complain;
36a117a5 5376{
f71f87f9
MM
5377 tree r = NULL_TREE;
5378 tree* new_parms;
36a117a5
MM
5379
5380 for (new_parms = &r;
5381 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
5382 new_parms = &(TREE_CHAIN (*new_parms)),
5383 parms = TREE_CHAIN (parms))
5384 {
5385 tree new_vec =
5386 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
5387 int i;
5388
5389 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
5390 {
5391 tree default_value =
5392 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5393 tree parm_decl =
5394 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
5395
5396 TREE_VEC_ELT (new_vec, i)
4393e105
MM
5397 = build_tree_list (tsubst (default_value, args, complain,
5398 NULL_TREE),
5399 tsubst (parm_decl, args, complain,
5400 NULL_TREE));
36a117a5
MM
5401 }
5402
5403 *new_parms =
5404 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms)
5405 - TMPL_ARGS_DEPTH (args))),
5406 new_vec, NULL_TREE);
5407 }
5408
5409 return r;
5410}
5411
ed44da02
MM
5412/* Substitute the ARGS into the indicated aggregate (or enumeration)
5413 type T. If T is not an aggregate or enumeration type, it is
5414 handled as if by tsubst. IN_DECL is as for tsubst. If
5415 ENTERING_SCOPE is non-zero, T is the context for a template which
5416 we are presently tsubst'ing. Return the subsituted value. */
36a117a5 5417
e9659ab0 5418static tree
4393e105 5419tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
36a117a5
MM
5420 tree t;
5421 tree args;
4393e105 5422 int complain;
36a117a5
MM
5423 tree in_decl;
5424 int entering_scope;
5425{
5426 if (t == NULL_TREE)
5427 return NULL_TREE;
5428
5429 switch (TREE_CODE (t))
5430 {
5431 case RECORD_TYPE:
5432 if (TYPE_PTRMEMFUNC_P (t))
5433 {
5434 tree r = build_ptrmemfunc_type
4393e105 5435 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl));
adecb3f4
MM
5436 return cp_build_qualified_type_real (r, TYPE_QUALS (t),
5437 complain);
36a117a5
MM
5438 }
5439
5440 /* else fall through */
ed44da02 5441 case ENUMERAL_TYPE:
36a117a5 5442 case UNION_TYPE:
5db698f6 5443 if (TYPE_TEMPLATE_INFO (t))
36a117a5
MM
5444 {
5445 tree argvec;
5446 tree context;
5447 tree r;
5448
5449 /* First, determine the context for the type we are looking
5450 up. */
5451 if (TYPE_CONTEXT (t) != NULL_TREE)
5452 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
4393e105 5453 complain,
36a117a5
MM
5454 in_decl, /*entering_scope=*/1);
5455 else
5456 context = NULL_TREE;
5457
5458 /* Then, figure out what arguments are appropriate for the
5459 type we are trying to find. For example, given:
5460
5461 template <class T> struct S;
5462 template <class T, class U> void f(T, U) { S<U> su; }
5463
5464 and supposing that we are instantiating f<int, double>,
5465 then our ARGS will be {int, double}, but, when looking up
5466 S we only want {double}. */
7ddedda4 5467 push_momentary ();
4393e105
MM
5468 argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
5469 complain);
36a117a5
MM
5470
5471 r = lookup_template_class (t, argvec, in_decl, context,
5472 entering_scope);
7ddedda4 5473 pop_momentary ();
36a117a5 5474
adecb3f4
MM
5475 return cp_build_qualified_type_real (r, TYPE_QUALS (t),
5476 complain);
36a117a5
MM
5477 }
5478 else
5479 /* This is not a template type, so there's nothing to do. */
5480 return t;
5481
5482 default:
4393e105 5483 return tsubst (t, args, complain, in_decl);
36a117a5
MM
5484 }
5485}
5486
9188c363
MM
5487/* Substitute into the default argument ARG (a default argument for
5488 FN), which has the indicated TYPE. */
5489
5490tree
5491tsubst_default_argument (fn, type, arg)
5492 tree fn;
5493 tree type;
5494 tree arg;
5495{
5496 /* This default argument came from a template. Instantiate the
5497 default argument here, not in tsubst. In the case of
5498 something like:
5499
5500 template <class T>
5501 struct S {
5502 static T t();
5503 void f(T = t());
5504 };
5505
5506 we must be careful to do name lookup in the scope of S<T>,
5507 rather than in the current class. */
5508 if (DECL_CLASS_SCOPE_P (fn))
5509 pushclass (DECL_REAL_CONTEXT (fn), 2);
5510
5511 arg = tsubst_expr (arg, DECL_TI_ARGS (fn), /*complain=*/1, NULL_TREE);
5512
5513 if (DECL_CLASS_SCOPE_P (fn))
5514 popclass ();
5515
5516 /* Make sure the default argument is reasonable. */
5517 arg = check_default_argument (type, arg);
5518
5519 return arg;
5520}
5521
5522/* Substitute into all the default arguments for FN. */
5523
5524static void
5525tsubst_default_arguments (fn)
5526 tree fn;
5527{
5528 tree arg;
5529 tree tmpl_args;
5530
5531 tmpl_args = DECL_TI_ARGS (fn);
5532
5533 /* If this function is not yet instantiated, we certainly don't need
5534 its default arguments. */
5535 if (uses_template_parms (tmpl_args))
5536 return;
5537
5538 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
5539 arg;
5540 arg = TREE_CHAIN (arg))
5541 if (TREE_PURPOSE (arg))
5542 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
5543 TREE_VALUE (arg),
5544 TREE_PURPOSE (arg));
5545}
5546
ae58fa02
MM
5547/* Substitute the ARGS into the T, which is a _DECL. TYPE is the
5548 (already computed) substitution of ARGS into TREE_TYPE (T), if
5549 appropriate. Return the result of the substitution. IN_DECL is as
5550 for tsubst. */
00d3396f 5551
e9659ab0 5552static tree
ae58fa02
MM
5553tsubst_decl (t, args, type, in_decl)
5554 tree t;
5555 tree args;
5556 tree type;
8d08fdba
MS
5557 tree in_decl;
5558{
ae58fa02
MM
5559 int saved_lineno;
5560 char* saved_filename;
b370501f 5561 tree r = NULL_TREE;
830bfa74 5562
ae58fa02
MM
5563 /* Set the filename and linenumber to improve error-reporting. */
5564 saved_lineno = lineno;
5565 saved_filename = input_filename;
5566 lineno = DECL_SOURCE_LINE (t);
5567 input_filename = DECL_SOURCE_FILE (t);
b7484fbe 5568
8d08fdba
MS
5569 switch (TREE_CODE (t))
5570 {
98c1c668
JM
5571 case TEMPLATE_DECL:
5572 {
5573 /* We can get here when processing a member template function
5574 of a template class. */
98c1c668 5575 tree decl = DECL_TEMPLATE_RESULT (t);
386b8a85 5576 tree spec;
db2767b6 5577 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
98c1c668 5578
db2767b6
MM
5579 if (!is_template_template_parm)
5580 {
36a117a5
MM
5581 /* We might already have an instance of this template.
5582 The ARGS are for the surrounding class type, so the
5583 full args contain the tsubst'd args for the context,
5584 plus the innermost args from the template decl. */
5585 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
5586 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
5587 : DECL_TI_ARGS (DECL_RESULT (t));
7ddedda4
MM
5588 tree full_args;
5589
5590 push_momentary ();
4393e105
MM
5591 full_args = tsubst_template_arg_vector (tmpl_args, args,
5592 /*complain=*/1);
36a117a5
MM
5593
5594 /* tsubst_template_arg_vector doesn't copy the vector if
5595 nothing changed. But, *something* should have
5596 changed. */
5597 my_friendly_assert (full_args != tmpl_args, 0);
5598
5599 spec = retrieve_specialization (t, full_args);
7ddedda4 5600 pop_momentary ();
db2767b6 5601 if (spec != NULL_TREE)
ae58fa02
MM
5602 {
5603 r = spec;
5604 break;
5605 }
db2767b6 5606 }
98c1c668
JM
5607
5608 /* Make a new template decl. It will be similar to the
5609 original, but will record the current template arguments.
5610 We also create a new function declaration, which is just
5611 like the old one, but points to this new template, rather
5612 than the old one. */
ae58fa02
MM
5613 r = copy_node (t);
5614 copy_lang_decl (r);
5615 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
5616 TREE_CHAIN (r) = NULL_TREE;
db2767b6
MM
5617
5618 if (is_template_template_parm)
5619 {
4393e105 5620 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
ae58fa02
MM
5621 DECL_RESULT (r) = new_decl;
5622 TREE_TYPE (r) = TREE_TYPE (new_decl);
5623 break;
db2767b6
MM
5624 }
5625
ae58fa02 5626 DECL_CONTEXT (r)
4393e105
MM
5627 = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1,
5628 in_decl, /*entering_scope=*/1);
ae58fa02 5629 DECL_CLASS_CONTEXT (r)
4393e105
MM
5630 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args,
5631 /*complain=*/1, in_decl,
5632 /*entering_scope=*/1);
ae58fa02 5633 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
93cdc044
JM
5634
5635 if (TREE_CODE (decl) == TYPE_DECL)
5636 {
4393e105
MM
5637 tree new_type = tsubst (TREE_TYPE (t), args,
5638 /*complain=*/1, in_decl);
ae58fa02
MM
5639 TREE_TYPE (r) = new_type;
5640 CLASSTYPE_TI_TEMPLATE (new_type) = r;
5641 DECL_RESULT (r) = TYPE_MAIN_DECL (new_type);
5642 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
93cdc044
JM
5643 }
5644 else
5645 {
4393e105 5646 tree new_decl = tsubst (decl, args, /*complain=*/1, in_decl);
ae58fa02
MM
5647 DECL_RESULT (r) = new_decl;
5648 DECL_TI_TEMPLATE (new_decl) = r;
5649 TREE_TYPE (r) = TREE_TYPE (new_decl);
5650 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
93cdc044
JM
5651 }
5652
ae58fa02
MM
5653 SET_DECL_IMPLICIT_INSTANTIATION (r);
5654 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
5655 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
98c1c668
JM
5656
5657 /* The template parameters for this new template are all the
5658 template parameters for the old template, except the
5659 outermost level of parameters. */
ae58fa02 5660 DECL_TEMPLATE_PARMS (r)
4393e105
MM
5661 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
5662 /*complain=*/1);
98c1c668 5663
93cdc044 5664 if (PRIMARY_TEMPLATE_P (t))
ae58fa02 5665 DECL_PRIMARY_TEMPLATE (r) = r;
93cdc044 5666
8d019cef 5667 /* We don't partially instantiate partial specializations. */
93cdc044 5668 if (TREE_CODE (decl) == TYPE_DECL)
ae58fa02 5669 break;
93cdc044 5670
75650646
MM
5671 for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
5672 spec != NULL_TREE;
5673 spec = TREE_CHAIN (spec))
5674 {
5675 /* It helps to consider example here. Consider:
5676
5677 template <class T>
5678 struct S {
5679 template <class U>
5680 void f(U u);
5681
5682 template <>
5683 void f(T* t) {}
5684 };
5685
5686 Now, for example, we are instantiating S<int>::f(U u).
5687 We want to make a template:
5688
5689 template <class U>
5690 void S<int>::f(U);
5691
5692 It will have a specialization, for the case U = int*, of
5693 the form:
5694
5695 template <>
5696 void S<int>::f<int*>(int*);
5697
5698 This specialization will be an instantiation of
5699 the specialization given in the declaration of S, with
5700 argument list int*. */
5701
5702 tree fn = TREE_VALUE (spec);
5703 tree spec_args;
5704 tree new_fn;
5705
5706 if (!DECL_TEMPLATE_SPECIALIZATION (fn))
5707 /* Instantiations are on the same list, but they're of
5708 no concern to us. */
5709 continue;
5710
36a117a5
MM
5711 if (TREE_CODE (fn) != TEMPLATE_DECL)
5712 /* A full specialization. There's no need to record
5713 that here. */
5714 continue;
5715
4393e105
MM
5716 spec_args = tsubst (DECL_TI_ARGS (fn), args,
5717 /*complain=*/1, in_decl);
36a117a5 5718 new_fn = tsubst (DECL_RESULT (most_general_template (fn)),
4393e105 5719 spec_args, /*complain=*/1, in_decl);
36a117a5 5720 DECL_TI_TEMPLATE (new_fn) = fn;
ae58fa02 5721 register_specialization (new_fn, r,
36a117a5 5722 innermost_args (spec_args));
75650646
MM
5723 }
5724
5725 /* Record this partial instantiation. */
ae58fa02
MM
5726 register_specialization (r, t,
5727 DECL_TI_ARGS (DECL_RESULT (r)));
75650646 5728
98c1c668 5729 }
ae58fa02 5730 break;
8d08fdba
MS
5731
5732 case FUNCTION_DECL:
5733 {
386b8a85 5734 tree ctx;
87603ed0 5735 tree argvec = NULL_TREE;
cf38f48a 5736 tree *friends;
36a117a5 5737 tree gen_tmpl;
5566b478 5738 int member;
d8c4447d
MM
5739 int args_depth;
5740 int parms_depth;
5566b478 5741
36a117a5
MM
5742 /* Nobody should be tsubst'ing into non-template functions. */
5743 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5744
5745 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
5746 {
5747 tree spec;
5748
7ddedda4
MM
5749 /* Allocate template arguments on the momentary obstack,
5750 in case we don't need to keep them. */
5751 push_momentary ();
5752
36a117a5
MM
5753 /* Calculate the most general template of which R is a
5754 specialization, and the complete set of arguments used to
5755 specialize R. */
5756 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
7ddedda4
MM
5757 argvec
5758 = tsubst_template_arg_vector (DECL_TI_ARGS
5759 (DECL_TEMPLATE_RESULT (gen_tmpl)),
4393e105 5760 args, /*complain=*/1);
36a117a5
MM
5761
5762 /* Check to see if we already have this specialization. */
5763 spec = retrieve_specialization (gen_tmpl, argvec);
7ddedda4 5764
36a117a5 5765 if (spec)
ae58fa02
MM
5766 {
5767 r = spec;
7ddedda4 5768 pop_momentary ();
ae58fa02
MM
5769 break;
5770 }
d8c4447d 5771
7ddedda4
MM
5772 pop_momentary ();
5773
d8c4447d
MM
5774 /* Here, we deal with the peculiar case:
5775
5776 template <class T> struct S {
5777 template <class U> friend void f();
5778 };
5779 template <class U> friend void f() {}
5780 template S<int>;
5781 template void f<double>();
5782
5783 Here, the ARGS for the instantiation of will be {int,
5784 double}. But, we only need as many ARGS as there are
5785 levels of template parameters in CODE_PATTERN. We are
5786 careful not to get fooled into reducing the ARGS in
5787 situations like:
5788
5789 template <class T> struct S { template <class U> void f(U); }
5790 template <class T> template <> void S<T>::f(int) {}
5791
5792 which we can spot because the pattern will be a
5793 specialization in this case. */
5794 args_depth = TMPL_ARGS_DEPTH (args);
5795 parms_depth =
5796 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
5797 if (args_depth > parms_depth
5798 && !DECL_TEMPLATE_SPECIALIZATION (t))
5799 {
5800 my_friendly_assert (DECL_FRIEND_P (t), 0);
5801
5802 if (parms_depth > 1)
5803 {
5804 int i;
5805
5806 args = make_temp_vec (parms_depth);
5807 for (i = 0; i < parms_depth; ++i)
5808 TREE_VEC_ELT (args, i) =
5809 TREE_VEC_ELT (args, i + (args_depth - parms_depth));
5810 }
5811 else
5812 args = TREE_VEC_ELT (args, args_depth - parms_depth);
5813 }
36a117a5
MM
5814 }
5815 else
5816 {
5817 /* This special case arises when we have something like this:
5818
5819 template <class T> struct S {
5820 friend void f<int>(int, double);
5821 };
5822
5823 Here, the DECL_TI_TEMPLATE for the friend declaration
39c01e4c
MM
5824 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
5825 being called from tsubst_friend_function, and we want
5826 only to create a new decl (R) with appropriate types so
5827 that we can call determine_specialization. */
5828 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
5829 == LOOKUP_EXPR)
5830 || (TREE_CODE (DECL_TI_TEMPLATE (t))
5831 == IDENTIFIER_NODE), 0);
36a117a5
MM
5832 gen_tmpl = NULL_TREE;
5833 }
5834
6eb3bb27 5835 if (DECL_CLASS_SCOPE_P (t))
8d08fdba 5836 {
5566b478
MS
5837 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
5838 member = 2;
5839 else
5840 member = 1;
4393e105
MM
5841 ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args,
5842 /*complain=*/1, t,
36a117a5 5843 /*entering_scope=*/1);
5566b478
MS
5844 }
5845 else
5846 {
5847 member = 0;
5848 ctx = NULL_TREE;
5566b478 5849 }
4393e105 5850 type = tsubst (type, args, /*complain=*/1, in_decl);
8d08fdba 5851
5566b478
MS
5852 /* We do NOT check for matching decls pushed separately at this
5853 point, as they may not represent instantiations of this
5854 template, and in any case are considered separate under the
312e7d50 5855 discrete model. Instead, see add_maybe_template. */
5566b478
MS
5856
5857 r = copy_node (t);
5858 copy_lang_decl (r);
e1467ff2 5859 DECL_USE_TEMPLATE (r) = 0;
5566b478
MS
5860 TREE_TYPE (r) = type;
5861
5862 DECL_CONTEXT (r)
4393e105
MM
5863 = tsubst_aggr_type (DECL_CONTEXT (t), args, /*complain=*/1, t,
5864 /*entering_scope=*/1);
5566b478
MS
5865 DECL_CLASS_CONTEXT (r) = ctx;
5866
4c571114 5867 if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r)))
f181d4ae
MM
5868 /* Type-conversion operator. Reconstruct the name, in
5869 case it's the name of one of the template's parameters. */
5870 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
5566b478 5871
4393e105
MM
5872 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
5873 /*complain=*/1, t);
711734a9
JM
5874 DECL_MAIN_VARIANT (r) = r;
5875 DECL_RESULT (r) = NULL_TREE;
711734a9
JM
5876
5877 TREE_STATIC (r) = 0;
5878 TREE_PUBLIC (r) = TREE_PUBLIC (t);
5879 DECL_EXTERNAL (r) = 1;
5880 DECL_INTERFACE_KNOWN (r) = 0;
5881 DECL_DEFER_OUTPUT (r) = 0;
5882 TREE_CHAIN (r) = NULL_TREE;
5883 DECL_PENDING_INLINE_INFO (r) = 0;
5884 TREE_USED (r) = 0;
5885
36a117a5
MM
5886 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
5887 name. There's no need to do this in the special friend
5888 case mentioned above where GEN_TMPL is NULL. */
5889 if (gen_tmpl)
386b8a85 5890 {
36a117a5
MM
5891 DECL_TEMPLATE_INFO (r)
5892 = perm_tree_cons (gen_tmpl, argvec, NULL_TREE);
5893 SET_DECL_IMPLICIT_INSTANTIATION (r);
5894 register_specialization (r, gen_tmpl, argvec);
5895
5896 /* Set the mangled name for R. */
5897 if (DECL_DESTRUCTOR_P (t))
5898 DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx);
5899 else
386b8a85 5900 {
36a117a5
MM
5901 /* Instantiations of template functions must be mangled
5902 specially, in order to conform to 14.5.5.1
5903 [temp.over.link]. */
5904 tree tmpl = DECL_TI_TEMPLATE (t);
386b8a85 5905
36a117a5
MM
5906 /* TMPL will be NULL if this is a specialization of a
5907 member function of a template class. */
5908 if (name_mangling_version < 1
5909 || tmpl == NULL_TREE
5910 || (member && !is_member_template (tmpl)
5911 && !DECL_TEMPLATE_INFO (tmpl)))
5912 set_mangled_name_for_decl (r);
75650646 5913 else
36a117a5 5914 set_mangled_name_for_template_decl (r);
386b8a85 5915 }
36a117a5
MM
5916
5917 DECL_RTL (r) = 0;
5918 make_decl_rtl (r, NULL_PTR, 1);
5919
5920 /* Like grokfndecl. If we don't do this, pushdecl will
5921 mess up our TREE_CHAIN because it doesn't find a
5922 previous decl. Sigh. */
5923 if (member
c36ae80f 5924 && ! uses_template_parms (r)
36a117a5
MM
5925 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r))
5926 == NULL_TREE))
5927 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
9188c363
MM
5928
5929 /* We're not supposed to instantiate default arguments
5930 until they are called, for a template. But, for a
5931 declaration like:
5932
5933 template <class T> void f ()
5934 { extern void g(int i = T()); }
5935
5936 we should do the substitution when the template is
5937 instantiated. We handle the member function case in
5938 instantiate_class_template since the default arguments
5939 might refer to other members of the class. */
5940 if (!member
5941 && !PRIMARY_TEMPLATE_P (gen_tmpl)
5942 && !uses_template_parms (argvec))
5943 tsubst_default_arguments (r);
386b8a85 5944 }
f181d4ae 5945
cf38f48a
MM
5946 /* Copy the list of befriending classes. */
5947 for (friends = &DECL_BEFRIENDING_CLASSES (r);
5948 *friends;
5949 friends = &TREE_CHAIN (*friends))
5950 {
5951 *friends = copy_node (*friends);
5952 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
5953 args, /*complain=*/1,
5954 in_decl);
5955 }
5956
f181d4ae
MM
5957 if (DECL_CONSTRUCTOR_P (r))
5958 {
5959 maybe_retrofit_in_chrg (r);
5960 grok_ctor_properties (ctx, r);
5961 }
f71f87f9 5962 else if (DECL_OVERLOADED_OPERATOR_P (r))
f181d4ae 5963 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
8d08fdba 5964 }
ae58fa02 5965 break;
8d08fdba
MS
5966
5967 case PARM_DECL:
5968 {
ae58fa02 5969 r = copy_node (t);
5566b478 5970 TREE_TYPE (r) = type;
1b8899d1
MM
5971 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5972
db2767b6
MM
5973 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5974 DECL_INITIAL (r) = TREE_TYPE (r);
5975 else
4393e105
MM
5976 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
5977 /*complain=*/1, in_decl);
db2767b6 5978
5566b478 5979 DECL_CONTEXT (r) = NULL_TREE;
fa56377d
JJ
5980 if (PROMOTE_PROTOTYPES
5981 && (TREE_CODE (type) == INTEGER_TYPE
5982 || TREE_CODE (type) == ENUMERAL_TYPE)
f83b0cb6
JM
5983 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5984 DECL_ARG_TYPE (r) = integer_type_node;
8d08fdba 5985 if (TREE_CHAIN (t))
4393e105
MM
5986 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
5987 /*complain=*/1, TREE_CHAIN (t));
8d08fdba 5988 }
ae58fa02 5989 break;
8d08fdba 5990
5566b478
MS
5991 case FIELD_DECL:
5992 {
ae58fa02 5993 r = copy_node (t);
5566b478 5994 copy_lang_decl (r);
1b8899d1
MM
5995 TREE_TYPE (r) = type;
5996 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5997
5998 /* We don't have to set DECL_CONTEXT here; it is set by
5999 finish_member_declaration. */
4393e105
MM
6000 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
6001 /*complain=*/1, in_decl);
5566b478 6002 TREE_CHAIN (r) = NULL_TREE;
4393e105 6003 if (TREE_CODE (type) == VOID_TYPE)
8251199e 6004 cp_error_at ("instantiation of `%D' as type void", r);
5566b478 6005 }
ae58fa02 6006 break;
5566b478
MS
6007
6008 case USING_DECL:
6009 {
ae58fa02 6010 r = copy_node (t);
5566b478 6011 DECL_INITIAL (r)
4393e105 6012 = tsubst_copy (DECL_INITIAL (t), args, /*complain=*/1, in_decl);
5566b478 6013 TREE_CHAIN (r) = NULL_TREE;
5566b478 6014 }
ae58fa02 6015 break;
5566b478 6016
9188c363
MM
6017 case TYPE_DECL:
6018 if (DECL_IMPLICIT_TYPEDEF_P (t))
6019 {
6020 /* For an implicit typedef, we just want the implicit
6021 typedef for the tsubst'd type. We've already got the
6022 tsubst'd type, as TYPE, so we just need it's associated
6023 declaration. */
6024 r = TYPE_NAME (type);
6025 break;
6026 }
6027 else if (!DECL_LANG_SPECIFIC (t))
6028 {
6029 /* For a template type parameter, we don't have to do
6030 anything special. */
6031 r= TYPE_NAME (type);
6032 break;
6033 }
6034
6035 /* Fall through. */
6036
5566b478
MS
6037 case VAR_DECL:
6038 {
36a117a5
MM
6039 tree argvec;
6040 tree gen_tmpl;
6041 tree spec;
6042 tree tmpl;
9188c363
MM
6043 tree ctx;
6044
8dd3f57a
MM
6045 /* Nobody should be tsubst'ing into non-template variables. */
6046 my_friendly_assert (DECL_LANG_SPECIFIC (t)
6047 && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5566b478 6048
9188c363
MM
6049 if (TYPE_P (DECL_CONTEXT (t)))
6050 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
6051 /*complain=*/1,
6052 in_decl, /*entering_scope=*/1);
6053 else
6054 /* Subsequent calls to pushdecl will fill this in. */
6055 ctx = NULL_TREE;
6056
36a117a5
MM
6057 /* Check to see if we already have this specialization. */
6058 tmpl = DECL_TI_TEMPLATE (t);
6059 gen_tmpl = most_general_template (tmpl);
4393e105 6060 argvec = tsubst (DECL_TI_ARGS (t), args, /*complain=*/1, in_decl);
9188c363
MM
6061 if (ctx)
6062 spec = retrieve_specialization (gen_tmpl, argvec);
6063 else
6064 spec = retrieve_local_specialization (gen_tmpl,
6065 current_function_decl);
6066
36a117a5 6067 if (spec)
ae58fa02
MM
6068 {
6069 r = spec;
6070 break;
6071 }
5566b478 6072
e0942dcd
MM
6073 /* This declaration is going to have to be around for a while,
6074 so me make sure it is on a saveable obstack. */
6075 push_obstacks_nochange ();
6076 saveable_allocation ();
5566b478 6077 r = copy_node (t);
e0942dcd
MM
6078 pop_obstacks ();
6079
5566b478 6080 TREE_TYPE (r) = type;
1b8899d1 6081 c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
5566b478 6082 DECL_CONTEXT (r) = ctx;
d11ad92e
MS
6083
6084 /* Don't try to expand the initializer until someone tries to use
6085 this variable; otherwise we run into circular dependencies. */
6086 DECL_INITIAL (r) = NULL_TREE;
5566b478
MS
6087 DECL_RTL (r) = 0;
6088 DECL_SIZE (r) = 0;
36a117a5
MM
6089 copy_lang_decl (r);
6090 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
5566b478 6091
e0942dcd
MM
6092 /* Even if the original location is out of scope, the newly
6093 substituted one is not. */
6094 if (TREE_CODE (r) == VAR_DECL)
6095 DECL_DEAD_FOR_LOCAL (r) = 0;
6096
fa8d6e85
MM
6097 /* A static data member declaration is always marked external
6098 when it is declared in-class, even if an initializer is
6099 present. We mimic the non-template processing here. */
e0942dcd
MM
6100 if (ctx)
6101 DECL_EXTERNAL (r) = 1;
fa8d6e85 6102
36a117a5
MM
6103 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
6104 SET_DECL_IMPLICIT_INSTANTIATION (r);
9188c363
MM
6105 if (ctx)
6106 register_specialization (r, gen_tmpl, argvec);
6107 else
6108 register_local_specialization (r, gen_tmpl,
6109 current_function_decl);
5566b478 6110
5566b478 6111 TREE_CHAIN (r) = NULL_TREE;
9188c363 6112 if (TREE_CODE (r) == VAR_DECL && TREE_CODE (type) == VOID_TYPE)
8251199e 6113 cp_error_at ("instantiation of `%D' as type void", r);
5566b478 6114 }
ae58fa02 6115 break;
5566b478 6116
ae58fa02
MM
6117 default:
6118 my_friendly_abort (0);
6119 }
6120
6121 /* Restore the file and line information. */
6122 lineno = saved_lineno;
6123 input_filename = saved_filename;
6124
6125 return r;
6126}
6127
cabc336a
MM
6128/* Substitue into the ARG_TYPES of a function type. */
6129
e9659ab0 6130static tree
4393e105 6131tsubst_arg_types (arg_types, args, complain, in_decl)
cabc336a
MM
6132 tree arg_types;
6133 tree args;
4393e105 6134 int complain;
cabc336a
MM
6135 tree in_decl;
6136{
6137 tree remaining_arg_types;
cabc336a
MM
6138 tree type;
6139
6140 if (!arg_types || arg_types == void_list_node)
6141 return arg_types;
6142
6143 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
4393e105
MM
6144 args, complain, in_decl);
6145 if (remaining_arg_types == error_mark_node)
6146 return error_mark_node;
6147
6148 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
6149 if (type == error_mark_node)
6150 return error_mark_node;
cabc336a 6151
4393e105
MM
6152 /* Do array-to-pointer, function-to-pointer conversion, and ignore
6153 top-level qualifiers as required. */
6154 type = TYPE_MAIN_VARIANT (type_decays_to (type));
cabc336a
MM
6155
6156 /* Note that we do not substitute into default arguments here. The
6157 standard mandates that they be instantiated only when needed,
6158 which is done in build_over_call. */
51632249
JM
6159 return hash_tree_cons (TREE_PURPOSE (arg_types), type,
6160 remaining_arg_types);
cabc336a
MM
6161
6162}
6163
4393e105
MM
6164/* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
6165 *not* handle the exception-specification for FNTYPE, because the
6166 initial substitution of explicitly provided template parameters
6167 during argument deduction forbids substitution into the
6168 exception-specification:
6169
6170 [temp.deduct]
6171
6172 All references in the function type of the function template to the
6173 corresponding template parameters are replaced by the specified tem-
6174 plate argument values. If a substitution in a template parameter or
6175 in the function type of the function template results in an invalid
6176 type, type deduction fails. [Note: The equivalent substitution in
6177 exception specifications is done only when the function is instanti-
6178 ated, at which point a program is ill-formed if the substitution
6179 results in an invalid type.] */
6180
6181static tree
6182tsubst_function_type (t, args, complain, in_decl)
6183 tree t;
6184 tree args;
6185 int complain;
6186 tree in_decl;
6187{
6188 tree return_type;
6189 tree arg_types;
6190 tree fntype;
6191
8dd3f57a 6192 /* The TYPE_CONTEXT is not used for function/method types. */
4393e105
MM
6193 my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
6194
6195 /* Substitue the return type. */
6196 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
6197 if (return_type == error_mark_node)
6198 return error_mark_node;
6199
6200 /* Substitue the argument types. */
6201 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
6202 complain, in_decl);
6203 if (arg_types == error_mark_node)
6204 return error_mark_node;
6205
6206 /* Construct a new type node and return it. */
6207 if (TREE_CODE (t) == FUNCTION_TYPE)
6208 fntype = build_function_type (return_type, arg_types);
6209 else
6210 {
6211 tree r = TREE_TYPE (TREE_VALUE (arg_types));
6212 if (! IS_AGGR_TYPE (r))
6213 {
6214 /* [temp.deduct]
6215
6216 Type deduction may fail for any of the following
6217 reasons:
6218
6219 -- Attempting to create "pointer to member of T" when T
6220 is not a class type. */
6221 if (complain)
6222 cp_error ("creating pointer to member function of non-class type `%T'",
6223 r);
6224 return error_mark_node;
6225 }
6226
6227 fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
6228 (arg_types));
6229 }
6230 fntype = build_qualified_type (fntype, TYPE_QUALS (t));
ada846eb 6231 fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
4393e105
MM
6232
6233 return fntype;
6234}
6235
297e73d8
MM
6236/* Substitute into the PARMS of a call-declarator. */
6237
e9659ab0 6238static tree
4393e105 6239tsubst_call_declarator_parms (parms, args, complain, in_decl)
297e73d8
MM
6240 tree parms;
6241 tree args;
4393e105 6242 int complain;
297e73d8
MM
6243 tree in_decl;
6244{
6245 tree new_parms;
6246 tree type;
6247 tree defarg;
6248
6249 if (!parms || parms == void_list_node)
6250 return parms;
6251
6252 new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
4393e105 6253 args, complain, in_decl);
297e73d8
MM
6254
6255 /* Figure out the type of this parameter. */
4393e105 6256 type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
297e73d8
MM
6257
6258 /* Figure out the default argument as well. Note that we use
4393e105
MM
6259 tsubst_expr since the default argument is really an expression. */
6260 defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
297e73d8
MM
6261
6262 /* Chain this parameter on to the front of those we have already
6263 processed. We don't use hash_tree_cons because that function
6264 doesn't check TREE_PARMLIST. */
6265 new_parms = tree_cons (defarg, type, new_parms);
6266
6267 /* And note that these are parameters. */
6268 TREE_PARMLIST (new_parms) = 1;
6269
6270 return new_parms;
6271}
6272
4393e105
MM
6273/* Take the tree structure T and replace template parameters used
6274 therein with the argument vector ARGS. IN_DECL is an associated
6275 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
6276 An appropriate error message is issued only if COMPLAIN is
6277 non-zero. Note that we must be relatively non-tolerant of
6278 extensions here, in order to preserve conformance; if we allow
6279 substitutions that should not be allowed, we may allow argument
6280 deductions that should not succeed, and therefore report ambiguous
6281 overload situations where there are none. In theory, we could
6282 allow the substitution, but indicate that it should have failed,
6283 and allow our caller to make sure that the right thing happens, but
6284 we don't try to do this yet.
6285
6286 This function is used for dealing with types, decls and the like;
6287 for expressions, use tsubst_expr or tsubst_copy. */
ae58fa02
MM
6288
6289tree
4393e105 6290tsubst (t, args, complain, in_decl)
ae58fa02 6291 tree t, args;
4393e105 6292 int complain;
ae58fa02
MM
6293 tree in_decl;
6294{
0ecfe0b4 6295 tree type, r;
ae58fa02
MM
6296
6297 if (t == NULL_TREE || t == error_mark_node
6298 || t == integer_type_node
6299 || t == void_type_node
6300 || t == char_type_node
6301 || TREE_CODE (t) == NAMESPACE_DECL)
6302 return t;
6303
6304 if (TREE_CODE (t) == IDENTIFIER_NODE)
6305 type = IDENTIFIER_TYPE_VALUE (t);
6306 else
6307 type = TREE_TYPE (t);
6308 if (type == unknown_type_node)
6309 my_friendly_abort (42);
6310
6311 if (type && TREE_CODE (t) != FUNCTION_DECL
6312 && TREE_CODE (t) != TYPENAME_TYPE
6313 && TREE_CODE (t) != TEMPLATE_DECL
4393e105
MM
6314 && TREE_CODE (t) != IDENTIFIER_NODE
6315 && TREE_CODE (t) != FUNCTION_TYPE
6316 && TREE_CODE (t) != METHOD_TYPE)
6317 type = tsubst (type, args, complain, in_decl);
6318 if (type == error_mark_node)
6319 return error_mark_node;
ae58fa02
MM
6320
6321 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
6322 return tsubst_decl (t, args, type, in_decl);
6323
6324 switch (TREE_CODE (t))
6325 {
6326 case RECORD_TYPE:
6327 case UNION_TYPE:
6328 case ENUMERAL_TYPE:
4393e105
MM
6329 return tsubst_aggr_type (t, args, complain, in_decl,
6330 /*entering_scope=*/0);
ae58fa02
MM
6331
6332 case ERROR_MARK:
6333 case IDENTIFIER_NODE:
6334 case OP_IDENTIFIER:
6335 case VOID_TYPE:
6336 case REAL_TYPE:
6337 case COMPLEX_TYPE:
6338 case BOOLEAN_TYPE:
6339 case INTEGER_CST:
6340 case REAL_CST:
6341 case STRING_CST:
6342 return t;
6343
6344 case INTEGER_TYPE:
6345 if (t == integer_type_node)
6346 return t;
6347
6348 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
6349 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
6350 return t;
d2e5ee5c 6351
5566b478 6352 {
ddce3528 6353 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
7ddedda4 6354
ddce3528 6355 max = tsubst_expr (omax, args, complain, in_decl);
4393e105
MM
6356 if (max == error_mark_node)
6357 return error_mark_node;
6358
8dd3f57a
MM
6359 /* See if we can reduce this expression to something simpler. */
6360 max = maybe_fold_nontype_arg (max);
6361 if (!processing_template_decl && TREE_READONLY_DECL_P (max))
6362 max = decl_constant_value (max);
6363
856216bb
MM
6364 if (processing_template_decl
6365 /* When providing explicit arguments to a template
6366 function, but leaving some arguments for subsequent
6367 deduction, MAX may be template-dependent even if we're
6368 not PROCESSING_TEMPLATE_DECL. */
6369 || TREE_CODE (max) != INTEGER_CST)
ae58fa02 6370 {
8dd3f57a
MM
6371 tree itype = make_node (INTEGER_TYPE);
6372 TYPE_MIN_VALUE (itype) = size_zero_node;
6373 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
6374 integer_one_node);
6375 return itype;
ae58fa02
MM
6376 }
6377
ddce3528
JM
6378 if (integer_zerop (omax))
6379 {
6380 /* Still allow an explicit array of size zero. */
6381 if (pedantic)
6382 pedwarn ("creating array with size zero");
6383 }
6384 else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node))
0ecfe0b4 6385 {
4393e105
MM
6386 /* [temp.deduct]
6387
6388 Type deduction may fail for any of the following
6389 reasons:
6390
bf8f3f93
MM
6391 Attempting to create an array with a size that is
6392 zero or negative. */
4393e105
MM
6393 if (complain)
6394 cp_error ("creating array with size `%E'", max);
6395
6396 return error_mark_node;
0ecfe0b4
JM
6397 }
6398
337c90cc 6399 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node));
120ecb8a 6400 return build_index_type (max);
ae58fa02
MM
6401 }
6402
6403 case TEMPLATE_TYPE_PARM:
6404 case TEMPLATE_TEMPLATE_PARM:
6405 case TEMPLATE_PARM_INDEX:
6406 {
6407 int idx;
6408 int level;
6409 int levels;
0ecfe0b4
JM
6410
6411 r = NULL_TREE;
ae58fa02
MM
6412
6413 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
6414 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
6415 {
6416 idx = TEMPLATE_TYPE_IDX (t);
6417 level = TEMPLATE_TYPE_LEVEL (t);
6418 }
6419 else
6420 {
6421 idx = TEMPLATE_PARM_IDX (t);
6422 level = TEMPLATE_PARM_LEVEL (t);
6423 }
6424
6425 if (TREE_VEC_LENGTH (args) > 0)
6426 {
6427 tree arg = NULL_TREE;
6428
6429 levels = TMPL_ARGS_DEPTH (args);
6430 if (level <= levels)
6431 arg = TMPL_ARG (args, level, idx);
6432
8b5b8b7c
MM
6433 if (arg == error_mark_node)
6434 return error_mark_node;
6435 else if (arg != NULL_TREE)
ae58fa02
MM
6436 {
6437 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
6438 {
6439 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg))
6440 == 't', 0);
adecb3f4
MM
6441 return cp_build_qualified_type_real
6442 (arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t),
6443 complain);
ae58fa02
MM
6444 }
6445 else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
6446 {
7ddedda4 6447 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
ae58fa02
MM
6448 {
6449 /* We are processing a type constructed from
6450 a template template parameter */
7ddedda4 6451 tree argvec = tsubst (TYPE_TI_ARGS (t),
4393e105
MM
6452 args, complain, in_decl);
6453 if (argvec == error_mark_node)
6454 return error_mark_node;
6455
ae58fa02
MM
6456 /* We can get a TEMPLATE_TEMPLATE_PARM here when
6457 we are resolving nested-types in the signature of
6458 a member function templates.
6459 Otherwise ARG is a TEMPLATE_DECL and is the real
6460 template to be instantiated. */
6461 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
6462 arg = TYPE_NAME (arg);
6463
6464 r = lookup_template_class (DECL_NAME (arg),
6465 argvec, in_decl,
6466 DECL_CONTEXT (arg),
6467 /*entering_scope=*/0);
adecb3f4
MM
6468 return cp_build_qualified_type_real (r,
6469 TYPE_QUALS (t),
6470 complain);
ae58fa02
MM
6471 }
6472 else
6473 /* We are processing a template argument list. */
6474 return arg;
6475 }
6476 else
6477 return arg;
6478 }
6479 }
51924768
JM
6480 else
6481 my_friendly_abort (981018);
ae58fa02
MM
6482
6483 if (level == 1)
6484 /* This can happen during the attempted tsubst'ing in
6485 unify. This means that we don't yet have any information
6486 about the template parameter in question. */
6487 return t;
6488
6489 /* If we get here, we must have been looking at a parm for a
6490 more deeply nested template. Make a new version of this
6491 template parameter, but with a lower level. */
6492 switch (TREE_CODE (t))
6493 {
6494 case TEMPLATE_TYPE_PARM:
6495 case TEMPLATE_TEMPLATE_PARM:
6496 r = copy_node (t);
6497 TEMPLATE_TYPE_PARM_INDEX (r)
6498 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
6499 r, levels);
6500 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
6501 TYPE_MAIN_VARIANT (r) = r;
6502 TYPE_POINTER_TO (r) = NULL_TREE;
6503 TYPE_REFERENCE_TO (r) = NULL_TREE;
6504
6505 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
7ddedda4 6506 && TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
ae58fa02 6507 {
4393e105
MM
6508 tree argvec = tsubst (TYPE_TI_ARGS (t), args,
6509 complain, in_decl);
6510 if (argvec == error_mark_node)
6511 return error_mark_node;
6512
7ddedda4 6513 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
ae58fa02
MM
6514 = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
6515 }
6516 break;
6517
6518 case TEMPLATE_PARM_INDEX:
6519 r = reduce_template_parm_level (t, type, levels);
6520 break;
6521
6522 default:
6523 my_friendly_abort (0);
6524 }
6525
5566b478 6526 return r;
ae58fa02 6527 }
5566b478 6528
8d08fdba
MS
6529 case TREE_LIST:
6530 {
6531 tree purpose, value, chain, result;
8d08fdba
MS
6532
6533 if (t == void_list_node)
6534 return t;
6535
8d08fdba
MS
6536 purpose = TREE_PURPOSE (t);
6537 if (purpose)
4393e105
MM
6538 {
6539 purpose = tsubst (purpose, args, complain, in_decl);
6540 if (purpose == error_mark_node)
6541 return error_mark_node;
6542 }
8d08fdba
MS
6543 value = TREE_VALUE (t);
6544 if (value)
4393e105
MM
6545 {
6546 value = tsubst (value, args, complain, in_decl);
6547 if (value == error_mark_node)
6548 return error_mark_node;
6549 }
8d08fdba
MS
6550 chain = TREE_CHAIN (t);
6551 if (chain && chain != void_type_node)
4393e105
MM
6552 {
6553 chain = tsubst (chain, args, complain, in_decl);
6554 if (chain == error_mark_node)
6555 return error_mark_node;
6556 }
8d08fdba
MS
6557 if (purpose == TREE_PURPOSE (t)
6558 && value == TREE_VALUE (t)
6559 && chain == TREE_CHAIN (t))
6560 return t;
51632249 6561 result = hash_tree_cons (purpose, value, chain);
8d08fdba
MS
6562 TREE_PARMLIST (result) = TREE_PARMLIST (t);
6563 return result;
6564 }
6565 case TREE_VEC:
5566b478
MS
6566 if (type != NULL_TREE)
6567 {
1ceaad38
JM
6568 /* A binfo node. We always need to make a copy, of the node
6569 itself and of its BINFO_BASETYPES. */
85b71cf2 6570
5566b478
MS
6571 t = copy_node (t);
6572
c7449f2b
JM
6573 /* Make sure type isn't a typedef copy. */
6574 type = BINFO_TYPE (TYPE_BINFO (type));
6575
5566b478 6576 TREE_TYPE (t) = complete_type (type);
6633d636
MS
6577 if (IS_AGGR_TYPE (type))
6578 {
6579 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
6580 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
6581 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
6582 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
6583 }
5566b478
MS
6584 return t;
6585 }
85b71cf2
JM
6586
6587 /* Otherwise, a vector of template arguments. */
4393e105 6588 return tsubst_template_arg_vector (t, args, complain);
8d08fdba 6589
8d08fdba
MS
6590 case POINTER_TYPE:
6591 case REFERENCE_TYPE:
6592 {
8d08fdba 6593 enum tree_code code;
79a7c7fa 6594
8d08fdba
MS
6595 if (type == TREE_TYPE (t))
6596 return t;
6597
6598 code = TREE_CODE (t);
4393e105
MM
6599
6600
6601 /* [temp.deduct]
6602
6603 Type deduction may fail for any of the following
6604 reasons:
6605
6606 -- Attempting to create a pointer to reference type.
6607 -- Attempting to create a reference to a reference type or
6608 a reference to void. */
0ecfe0b4
JM
6609 if (TREE_CODE (type) == REFERENCE_TYPE
6610 || (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
79a7c7fa
JM
6611 {
6612 static int last_line = 0;
6613 static char* last_file = 0;
6614
6615 /* We keep track of the last time we issued this error
6616 message to avoid spewing a ton of messages during a
6617 single bad template instantiation. */
4393e105
MM
6618 if (complain && (last_line != lineno ||
6619 last_file != input_filename))
79a7c7fa 6620 {
0ecfe0b4
JM
6621 if (TREE_CODE (type) == VOID_TYPE)
6622 cp_error ("forming reference to void");
6623 else
6624 cp_error ("forming %s to reference type `%T'",
6625 (code == POINTER_TYPE) ? "pointer" : "reference",
6626 type);
79a7c7fa
JM
6627 last_line = lineno;
6628 last_file = input_filename;
6629 }
6630
4393e105 6631 return error_mark_node;
79a7c7fa
JM
6632 }
6633 else if (code == POINTER_TYPE)
8d08fdba
MS
6634 r = build_pointer_type (type);
6635 else
6636 r = build_reference_type (type);
adecb3f4 6637 r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
79a7c7fa 6638
8d08fdba
MS
6639 /* Will this ever be needed for TYPE_..._TO values? */
6640 layout_type (r);
6641 return r;
6642 }
a4443a08 6643 case OFFSET_TYPE:
0ecfe0b4 6644 {
4393e105
MM
6645 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
6646 if (r == error_mark_node || !IS_AGGR_TYPE (r))
6647 {
6648 /* [temp.deduct]
6649
6650 Type deduction may fail for any of the following
6651 reasons:
6652
6653 -- Attempting to create "pointer to member of T" when T
6654 is not a class type. */
6655 if (complain)
6656 cp_error ("creating pointer to member of non-class type `%T'",
6657 r);
6658 return error_mark_node;
6659 }
0ecfe0b4
JM
6660 return build_offset_type (r, type);
6661 }
8d08fdba
MS
6662 case FUNCTION_TYPE:
6663 case METHOD_TYPE:
6664 {
c11b6f21 6665 tree fntype;
4393e105 6666 tree raises;
8d08fdba 6667
4393e105
MM
6668 fntype = tsubst_function_type (t, args, complain, in_decl);
6669 if (fntype == error_mark_node)
6670 return error_mark_node;
cabc336a
MM
6671
6672 /* Substitue the exception specification. */
6673 raises = TYPE_RAISES_EXCEPTIONS (t);
c11b6f21
MS
6674 if (raises)
6675 {
4cc1d462
NS
6676 tree list = NULL_TREE;
6677
6678 if (! TREE_VALUE (raises))
6679 list = raises;
6680 else
6681 for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
6682 {
6683 tree spec = TREE_VALUE (raises);
6684
6685 spec = tsubst (spec, args, complain, in_decl);
6686 if (spec == error_mark_node)
6687 return spec;
6688 list = add_exception_specifier (list, spec, complain);
6689 }
6690 fntype = build_exception_variant (fntype, list);
c11b6f21
MS
6691 }
6692 return fntype;
8d08fdba
MS
6693 }
6694 case ARRAY_TYPE:
6695 {
4393e105
MM
6696 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
6697 if (domain == error_mark_node)
6698 return error_mark_node;
6699
6700 /* As an optimization, we avoid regenerating the array type if
6701 it will obviously be the same as T. */
8d08fdba
MS
6702 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
6703 return t;
0ecfe0b4 6704
4393e105
MM
6705 /* These checks should match the ones in grokdeclarator.
6706
6707 [temp.deduct]
6708
6709 The deduction may fail for any of the following reasons:
6710
6711 -- Attempting to create an array with an element type that
6712 is void, a function type, or a reference type. */
6713 if (TREE_CODE (type) == VOID_TYPE
6714 || TREE_CODE (type) == FUNCTION_TYPE
6715 || TREE_CODE (type) == REFERENCE_TYPE)
0ecfe0b4 6716 {
4393e105
MM
6717 if (complain)
6718 cp_error ("creating array of `%T'", type);
6719 return error_mark_node;
0ecfe0b4
JM
6720 }
6721
8d08fdba
MS
6722 r = build_cplus_array_type (type, domain);
6723 return r;
6724 }
6725
8d08fdba 6726 case PLUS_EXPR:
5566b478 6727 case MINUS_EXPR:
4393e105
MM
6728 {
6729 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6730 in_decl);
6731 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain,
6732 in_decl);
6733
6734 if (e1 == error_mark_node || e2 == error_mark_node)
6735 return error_mark_node;
6736
6737 return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
6738 }
8d08fdba
MS
6739
6740 case NEGATE_EXPR:
6741 case NOP_EXPR:
4393e105
MM
6742 {
6743 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6744 in_decl);
6745 if (e == error_mark_node)
6746 return error_mark_node;
6747
6748 return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
6749 }
8d08fdba 6750
5566b478
MS
6751 case TYPENAME_TYPE:
6752 {
4393e105
MM
6753 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
6754 in_decl, /*entering_scope=*/1);
6755 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
6756 complain, in_decl);
6757
6758 if (ctx == error_mark_node || f == error_mark_node)
6759 return error_mark_node;
ae58fa02 6760
bf8f3f93
MM
6761 if (!IS_AGGR_TYPE (ctx))
6762 {
6763 if (complain)
6764 cp_error ("`%T' is not a class, struct, or union type",
6765 ctx);
6766 return error_mark_node;
6767 }
6768 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
6769 {
6770 /* Normally, make_typename_type does not require that the CTX
6771 have complete type in order to allow things like:
ae58fa02 6772
bf8f3f93 6773 template <class T> struct S { typename S<T>::X Y; };
ae58fa02 6774
bf8f3f93
MM
6775 But, such constructs have already been resolved by this
6776 point, so here CTX really should have complete type, unless
6777 it's a partial instantiation. */
4393e105
MM
6778 ctx = complete_type (ctx);
6779 if (!TYPE_SIZE (ctx))
6780 {
6781 if (complain)
6782 incomplete_type_error (NULL_TREE, ctx);
6783 return error_mark_node;
6784 }
6785 }
ae58fa02 6786
b2b7d40a 6787 f = make_typename_type (ctx, f);
f0bcd168
MM
6788 if (f == error_mark_node)
6789 return f;
adecb3f4
MM
6790 return cp_build_qualified_type_real (f,
6791 CP_TYPE_QUALS (f)
6792 | CP_TYPE_QUALS (t),
6793 complain);
5566b478
MS
6794 }
6795
6796 case INDIRECT_REF:
4393e105
MM
6797 {
6798 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6799 in_decl);
6800 if (e == error_mark_node)
6801 return error_mark_node;
6802 return make_pointer_declarator (type, e);
6803 }
6804
5566b478 6805 case ADDR_EXPR:
4393e105
MM
6806 {
6807 tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
6808 in_decl);
6809 if (e == error_mark_node)
6810 return error_mark_node;
6811 return make_reference_declarator (type, e);
6812 }
5566b478
MS
6813
6814 case ARRAY_REF:
4393e105
MM
6815 {
6816 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6817 in_decl);
6fb2c05a
JM
6818 tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain,
6819 in_decl);
4393e105
MM
6820 if (e1 == error_mark_node || e2 == error_mark_node)
6821 return error_mark_node;
6822
6823 return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr);
6824 }
5566b478
MS
6825
6826 case CALL_EXPR:
4393e105
MM
6827 {
6828 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6829 in_decl);
43f887f9
MM
6830 tree e2 = (tsubst_call_declarator_parms
6831 (CALL_DECLARATOR_PARMS (t), args, complain, in_decl));
6832 tree e3 = tsubst (CALL_DECLARATOR_EXCEPTION_SPEC (t), args,
6833 complain, in_decl);
4393e105
MM
6834
6835 if (e1 == error_mark_node || e2 == error_mark_node
6836 || e3 == error_mark_node)
6837 return error_mark_node;
6838
43f887f9 6839 return make_call_declarator (e1, e2, CALL_DECLARATOR_QUALS (t), e3);
4393e105 6840 }
5566b478 6841
fc378698 6842 case SCOPE_REF:
4393e105
MM
6843 {
6844 tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
6845 in_decl);
6846 tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
6847 if (e1 == error_mark_node || e2 == error_mark_node)
6848 return error_mark_node;
6849
6850 return build_parse_node (TREE_CODE (t), e1, e2);
6851 }
fc378698 6852
b894fc05 6853 case TYPEOF_TYPE:
4393e105
MM
6854 {
6855 tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain,
6856 in_decl);
6857 if (e1 == error_mark_node)
6858 return error_mark_node;
6859
6860 return TREE_TYPE (e1);
6861 }
b894fc05 6862
8d08fdba 6863 default:
5566b478 6864 sorry ("use of `%s' in template",
8d08fdba
MS
6865 tree_code_name [(int) TREE_CODE (t)]);
6866 return error_mark_node;
6867 }
6868}
6869
00d3396f
JM
6870/* Like tsubst, but deals with expressions. This function just replaces
6871 template parms; to finish processing the resultant expression, use
6872 tsubst_expr. */
6873
5566b478 6874tree
4393e105 6875tsubst_copy (t, args, complain, in_decl)
98c1c668 6876 tree t, args;
4393e105 6877 int complain;
5566b478
MS
6878 tree in_decl;
6879{
6880 enum tree_code code;
8452b1d3 6881 tree r;
8d08fdba 6882
5566b478
MS
6883 if (t == NULL_TREE || t == error_mark_node)
6884 return t;
6885
6886 code = TREE_CODE (t);
b7484fbe 6887
5566b478
MS
6888 switch (code)
6889 {
6890 case PARM_DECL:
a759e627 6891 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
5566b478
MS
6892
6893 case CONST_DECL:
ed44da02
MM
6894 {
6895 tree enum_type;
6896 tree v;
6897
6898 if (!DECL_CONTEXT (t))
6899 /* This is a global enumeration constant. */
6900 return t;
6901
6902 /* Unfortunately, we cannot just call lookup_name here.
9188c363
MM
6903 Consider:
6904
6905 template <int I> int f() {
6906 enum E { a = I };
6907 struct S { void g() { E e = a; } };
6908 };
6909
6910 When we instantiate f<7>::S::g(), say, lookup_name is not
6911 clever enough to find f<7>::a. */
ed44da02 6912 enum_type
4393e105 6913 = tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
ed44da02
MM
6914 /*entering_scope=*/0);
6915
6916 for (v = TYPE_VALUES (enum_type);
6917 v != NULL_TREE;
6918 v = TREE_CHAIN (v))
6919 if (TREE_PURPOSE (v) == DECL_NAME (t))
6920 return TREE_VALUE (v);
6921
6922 /* We didn't find the name. That should never happen; if
6923 name-lookup found it during preliminary parsing, we
6924 should find it again here during instantiation. */
6925 my_friendly_abort (0);
6926 }
db04386f 6927 return t;
ed44da02 6928
5566b478
MS
6929 case FIELD_DECL:
6930 if (DECL_CONTEXT (t))
6931 {
0978790f 6932 tree ctx;
0978790f 6933
4393e105 6934 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
36a117a5 6935 /*entering_scope=*/1);
0978790f 6936 if (ctx != DECL_CONTEXT (t))
5566b478
MS
6937 return lookup_field (ctx, DECL_NAME (t), 0, 0);
6938 }
6939 return t;
6940
6941 case VAR_DECL:
6942 case FUNCTION_DECL:
6943 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
4393e105 6944 t = tsubst (t, args, complain, in_decl);
5566b478
MS
6945 mark_used (t);
6946 return t;
6947
98c1c668
JM
6948 case TEMPLATE_DECL:
6949 if (is_member_template (t))
4393e105 6950 return tsubst (t, args, complain, in_decl);
98c1c668
JM
6951 else
6952 return t;
6953
672476cb
MM
6954 case LOOKUP_EXPR:
6955 {
6956 /* We must tsbust into a LOOKUP_EXPR in case the names to
6957 which it refers is a conversion operator; in that case the
6958 name will change. We avoid making unnecessary copies,
6959 however. */
6960
4393e105 6961 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
672476cb
MM
6962
6963 if (id != TREE_OPERAND (t, 0))
6964 {
8452b1d3 6965 r = build_nt (LOOKUP_EXPR, id);
672476cb
MM
6966 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
6967 t = r;
6968 }
6969
6970 return t;
6971 }
6972
5566b478
MS
6973 case CAST_EXPR:
6974 case REINTERPRET_CAST_EXPR:
e92cc029
MS
6975 case CONST_CAST_EXPR:
6976 case STATIC_CAST_EXPR:
6977 case DYNAMIC_CAST_EXPR:
51924768 6978 case NOP_EXPR:
5566b478 6979 return build1
4393e105
MM
6980 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
6981 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478
MS
6982
6983 case INDIRECT_REF:
6984 case PREDECREMENT_EXPR:
6985 case PREINCREMENT_EXPR:
6986 case POSTDECREMENT_EXPR:
6987 case POSTINCREMENT_EXPR:
6988 case NEGATE_EXPR:
6989 case TRUTH_NOT_EXPR:
b87692e5 6990 case BIT_NOT_EXPR:
5566b478
MS
6991 case ADDR_EXPR:
6992 case CONVERT_EXPR: /* Unary + */
6993 case SIZEOF_EXPR:
abff8e06 6994 case ALIGNOF_EXPR:
5566b478 6995 case ARROW_EXPR:
fc378698 6996 case THROW_EXPR:
5156628f 6997 case TYPEID_EXPR:
5566b478 6998 return build1
6a629cac 6999 (code, tsubst (TREE_TYPE (t), args, complain, in_decl),
4393e105 7000 tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
5566b478
MS
7001
7002 case PLUS_EXPR:
7003 case MINUS_EXPR:
7004 case MULT_EXPR:
7005 case TRUNC_DIV_EXPR:
7006 case CEIL_DIV_EXPR:
7007 case FLOOR_DIV_EXPR:
7008 case ROUND_DIV_EXPR:
7009 case EXACT_DIV_EXPR:
7010 case BIT_AND_EXPR:
7011 case BIT_ANDTC_EXPR:
7012 case BIT_IOR_EXPR:
7013 case BIT_XOR_EXPR:
7014 case TRUNC_MOD_EXPR:
7015 case FLOOR_MOD_EXPR:
7016 case TRUTH_ANDIF_EXPR:
7017 case TRUTH_ORIF_EXPR:
7018 case TRUTH_AND_EXPR:
7019 case TRUTH_OR_EXPR:
7020 case RSHIFT_EXPR:
7021 case LSHIFT_EXPR:
7022 case RROTATE_EXPR:
7023 case LROTATE_EXPR:
7024 case EQ_EXPR:
7025 case NE_EXPR:
7026 case MAX_EXPR:
7027 case MIN_EXPR:
7028 case LE_EXPR:
7029 case GE_EXPR:
7030 case LT_EXPR:
7031 case GT_EXPR:
7032 case COMPONENT_REF:
7033 case ARRAY_REF:
7034 case COMPOUND_EXPR:
7035 case SCOPE_REF:
7036 case DOTSTAR_EXPR:
7037 case MEMBER_REF:
7038 return build_nt
4393e105
MM
7039 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7040 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
7041
7042 case CALL_EXPR:
7043 {
7044 tree fn = TREE_OPERAND (t, 0);
f84b4be9 7045 if (is_overloaded_fn (fn))
4393e105 7046 fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
5566b478 7047 else
f84b4be9 7048 /* Sometimes FN is a LOOKUP_EXPR. */
4393e105 7049 fn = tsubst_copy (fn, args, complain, in_decl);
5566b478 7050 return build_nt
4393e105
MM
7051 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7052 in_decl),
5566b478
MS
7053 NULL_TREE);
7054 }
7055
7056 case METHOD_CALL_EXPR:
7057 {
7058 tree name = TREE_OPERAND (t, 0);
7059 if (TREE_CODE (name) == BIT_NOT_EXPR)
7060 {
4393e105
MM
7061 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7062 complain, in_decl);
7bae46f4 7063 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
7064 }
7065 else if (TREE_CODE (name) == SCOPE_REF
7066 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
7067 {
4393e105
MM
7068 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
7069 complain, in_decl);
5566b478 7070 name = TREE_OPERAND (name, 1);
4393e105
MM
7071 name = tsubst_copy (TREE_OPERAND (name, 0), args,
7072 complain, in_decl);
11686454 7073 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5566b478
MS
7074 name = build_nt (SCOPE_REF, base, name);
7075 }
7076 else
4393e105 7077 name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
5566b478 7078 return build_nt
4393e105
MM
7079 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
7080 complain, in_decl),
7081 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
5566b478
MS
7082 NULL_TREE);
7083 }
7084
558475f0
MM
7085 case STMT_EXPR:
7086 /* This processing should really occur in tsubst_expr, However,
7087 tsubst_expr does not recurse into expressions, since it
7088 assumes that there aren't any statements inside them.
7089 Instead, it simply calls build_expr_from_tree. So, we need
7090 to expand the STMT_EXPR here. */
7091 if (!processing_template_decl)
7092 {
7093 tree rtl_expr = begin_stmt_expr ();
7094 tree block = tsubst_expr (STMT_EXPR_STMT (t), args,
7095 complain, in_decl);
9d9169de 7096 return finish_stmt_expr (rtl_expr, block);
558475f0 7097 }
9d9169de
MM
7098
7099 return t;
558475f0 7100
5566b478
MS
7101 case COND_EXPR:
7102 case MODOP_EXPR:
40242ccf 7103 case PSEUDO_DTOR_EXPR:
67da3287 7104 {
8452b1d3 7105 r = build_nt
4393e105
MM
7106 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7107 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7108 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
67da3287
MM
7109 return r;
7110 }
5566b478
MS
7111
7112 case NEW_EXPR:
7113 {
8452b1d3 7114 r = build_nt
4393e105
MM
7115 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7116 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
7117 tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
5566b478
MS
7118 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
7119 return r;
7120 }
7121
7122 case DELETE_EXPR:
7123 {
8452b1d3 7124 r = build_nt
4393e105
MM
7125 (code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
7126 tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
5566b478
MS
7127 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
7128 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
7129 return r;
7130 }
7131
386b8a85
JM
7132 case TEMPLATE_ID_EXPR:
7133 {
00d3396f 7134 /* Substituted template arguments */
4393e105
MM
7135 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
7136 in_decl);
856216bb
MM
7137
7138 if (targs && TREE_CODE (targs) == TREE_LIST)
7139 {
7140 tree chain;
7141 for (chain = targs; chain; chain = TREE_CHAIN (chain))
7142 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
7143 }
7144 else if (targs)
7145 {
7146 int i;
7147 for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
7148 TREE_VEC_ELT (targs, i)
7149 = maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
7150 }
00d3396f
JM
7151
7152 return lookup_template_function
4393e105 7153 (tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
386b8a85
JM
7154 }
7155
5566b478
MS
7156 case TREE_LIST:
7157 {
7158 tree purpose, value, chain;
7159
7160 if (t == void_list_node)
7161 return t;
7162
7163 purpose = TREE_PURPOSE (t);
7164 if (purpose)
4393e105 7165 purpose = tsubst_copy (purpose, args, complain, in_decl);
5566b478
MS
7166 value = TREE_VALUE (t);
7167 if (value)
4393e105 7168 value = tsubst_copy (value, args, complain, in_decl);
5566b478
MS
7169 chain = TREE_CHAIN (t);
7170 if (chain && chain != void_type_node)
4393e105 7171 chain = tsubst_copy (chain, args, complain, in_decl);
5566b478
MS
7172 if (purpose == TREE_PURPOSE (t)
7173 && value == TREE_VALUE (t)
7174 && chain == TREE_CHAIN (t))
7175 return t;
7176 return tree_cons (purpose, value, chain);
7177 }
7178
7179 case RECORD_TYPE:
7180 case UNION_TYPE:
7181 case ENUMERAL_TYPE:
7182 case INTEGER_TYPE:
7183 case TEMPLATE_TYPE_PARM:
73b0fce8 7184 case TEMPLATE_TEMPLATE_PARM:
f84b4be9 7185 case TEMPLATE_PARM_INDEX:
5566b478
MS
7186 case POINTER_TYPE:
7187 case REFERENCE_TYPE:
7188 case OFFSET_TYPE:
7189 case FUNCTION_TYPE:
7190 case METHOD_TYPE:
7191 case ARRAY_TYPE:
7192 case TYPENAME_TYPE:
f84b4be9 7193 case TYPE_DECL:
4393e105 7194 return tsubst (t, args, complain, in_decl);
5566b478 7195
e92cc029 7196 case IDENTIFIER_NODE:
8e710788
BK
7197 if (IDENTIFIER_TYPENAME_P (t)
7198 /* Make sure it's not just a variable named `__opr', for instance,
7199 which can occur in some existing code. */
7200 && TREE_TYPE (t))
e92cc029 7201 return build_typename_overload
4393e105 7202 (tsubst (TREE_TYPE (t), args, complain, in_decl));
e92cc029
MS
7203 else
7204 return t;
7205
5156628f 7206 case CONSTRUCTOR:
8452b1d3
JM
7207 {
7208 r = build
4393e105
MM
7209 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
7210 NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
7211 complain, in_decl));
8452b1d3
JM
7212 TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
7213 return r;
7214 }
5156628f 7215
5566b478
MS
7216 default:
7217 return t;
7218 }
7219}
7220
558475f0 7221/* Like tsubst_copy, but also does semantic processing. */
00d3396f 7222
5566b478 7223tree
4393e105 7224tsubst_expr (t, args, complain, in_decl)
98c1c668 7225 tree t, args;
4393e105 7226 int complain;
5566b478
MS
7227 tree in_decl;
7228{
558475f0
MM
7229 tree stmt;
7230
5566b478
MS
7231 if (t == NULL_TREE || t == error_mark_node)
7232 return t;
7233
5156628f 7234 if (processing_template_decl)
4393e105 7235 return tsubst_copy (t, args, complain, in_decl);
5566b478
MS
7236
7237 switch (TREE_CODE (t))
8d08fdba 7238 {
558475f0
MM
7239 case RETURN_INIT:
7240 finish_named_return_value
7241 (TREE_OPERAND (t, 0),
7242 tsubst_expr (TREE_OPERAND (t, 1), args, /*complain=*/1, in_decl));
7243 tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7244 break;
7245
7246 case CTOR_INITIALIZER:
7247 current_member_init_list
7248 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
7249 current_base_init_list
7250 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
7251 setup_vtbl_ptr ();
7252 tsubst_expr (TREE_CHAIN (t), args, complain, in_decl);
7253 break;
7254
5566b478 7255 case RETURN_STMT:
f71f87f9 7256 lineno = STMT_LINENO (t);
ad321293 7257 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
4393e105 7258 args, complain, in_decl));
5566b478
MS
7259 break;
7260
7261 case EXPR_STMT:
f71f87f9 7262 lineno = STMT_LINENO (t);
ad321293 7263 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
4393e105 7264 args, complain, in_decl));
5566b478
MS
7265 break;
7266
7267 case DECL_STMT:
7268 {
7269 int i = suspend_momentary ();
e0942dcd
MM
7270 tree decl;
7271 tree init;
5566b478 7272
f71f87f9 7273 lineno = STMT_LINENO (t);
e0942dcd 7274 decl = DECL_STMT_DECL (t);
acef433b
MM
7275 if (TREE_CODE (decl) == LABEL_DECL)
7276 finish_label_decl (DECL_NAME (decl));
7277 else
7278 {
7279 init = DECL_INITIAL (decl);
7280 decl = tsubst (decl, args, complain, in_decl);
7281 init = tsubst_expr (init, args, complain, in_decl);
7282 DECL_INITIAL (decl) = init;
b7b8bcd2
MM
7283 /* By marking the declaration as instantiated, we avoid
7284 trying to instantiate it. Since instantiate_decl can't
7285 handle local variables, and since we've already done
7286 all that needs to be done, that's the right thing to
7287 do. */
acef433b
MM
7288 if (TREE_CODE (decl) == VAR_DECL)
7289 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
7290 maybe_push_decl (decl);
b7b8bcd2 7291 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
acef433b
MM
7292 add_decl_stmt (decl);
7293 }
5566b478 7294 resume_momentary (i);
e0942dcd 7295 return decl;
5566b478 7296 }
8d08fdba 7297
5566b478
MS
7298 case FOR_STMT:
7299 {
7300 tree tmp;
f71f87f9 7301 lineno = STMT_LINENO (t);
5566b478 7302
558475f0 7303 stmt = begin_for_stmt ();
ad321293 7304 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
4393e105 7305 tsubst_expr (tmp, args, complain, in_decl);
558475f0 7306 finish_for_init_stmt (stmt);
ad321293 7307 finish_for_cond (tsubst_expr (FOR_COND (t), args,
4393e105 7308 complain, in_decl),
558475f0 7309 stmt);
4393e105 7310 tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
558475f0 7311 finish_for_expr (tmp, stmt);
4393e105 7312 tsubst_expr (FOR_BODY (t), args, complain, in_decl);
558475f0 7313 finish_for_stmt (tmp, stmt);
5566b478
MS
7314 }
7315 break;
8d08fdba 7316
5566b478
MS
7317 case WHILE_STMT:
7318 {
f71f87f9 7319 lineno = STMT_LINENO (t);
558475f0 7320 stmt = begin_while_stmt ();
ad321293 7321 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
4393e105 7322 args, complain, in_decl),
558475f0 7323 stmt);
4393e105 7324 tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
558475f0 7325 finish_while_stmt (stmt);
5566b478
MS
7326 }
7327 break;
8d08fdba 7328
5566b478
MS
7329 case DO_STMT:
7330 {
f71f87f9 7331 lineno = STMT_LINENO (t);
558475f0 7332 stmt = begin_do_stmt ();
4393e105 7333 tsubst_expr (DO_BODY (t), args, complain, in_decl);
558475f0 7334 finish_do_body (stmt);
ad321293 7335 finish_do_stmt (tsubst_expr (DO_COND (t), args,
4393e105 7336 complain, in_decl),
558475f0 7337 stmt);
5566b478
MS
7338 }
7339 break;
a0a33927 7340
5566b478 7341 case IF_STMT:
8d08fdba 7342 {
5566b478 7343 tree tmp;
5566b478 7344
f71f87f9 7345 lineno = STMT_LINENO (t);
558475f0 7346 stmt = begin_if_stmt ();
ad321293 7347 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
4393e105 7348 args, complain, in_decl),
558475f0 7349 stmt);
db5ae43f 7350
ad321293 7351 if (tmp = THEN_CLAUSE (t), tmp)
db5ae43f 7352 {
4393e105 7353 tsubst_expr (tmp, args, complain, in_decl);
558475f0 7354 finish_then_clause (stmt);
db5ae43f
MS
7355 }
7356
ad321293
MM
7357 if (tmp = ELSE_CLAUSE (t), tmp)
7358 {
7359 begin_else_clause ();
4393e105 7360 tsubst_expr (tmp, args, complain, in_decl);
558475f0 7361 finish_else_clause (stmt);
ad321293 7362 }
8d08fdba 7363
ad321293 7364 finish_if_stmt ();
8d08fdba 7365 }
5566b478 7366 break;
8d08fdba 7367
5566b478
MS
7368 case COMPOUND_STMT:
7369 {
ad321293 7370 tree substmt;
8d08fdba 7371
f71f87f9 7372 lineno = STMT_LINENO (t);
558475f0 7373 stmt = begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
ad321293
MM
7374 for (substmt = COMPOUND_BODY (t);
7375 substmt != NULL_TREE;
7376 substmt = TREE_CHAIN (substmt))
4393e105 7377 tsubst_expr (substmt, args, complain, in_decl);
558475f0 7378 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t), stmt);
5566b478
MS
7379 }
7380 break;
8d08fdba 7381
5566b478 7382 case BREAK_STMT:
f71f87f9 7383 lineno = STMT_LINENO (t);
ad321293 7384 finish_break_stmt ();
5566b478 7385 break;
8d08fdba 7386
6467930b 7387 case CONTINUE_STMT:
f71f87f9 7388 lineno = STMT_LINENO (t);
ad321293 7389 finish_continue_stmt ();
6467930b
MS
7390 break;
7391
5566b478
MS
7392 case SWITCH_STMT:
7393 {
558475f0 7394 tree val;
5566b478 7395
f71f87f9 7396 lineno = STMT_LINENO (t);
ad321293 7397 begin_switch_stmt ();
4393e105 7398 val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
558475f0
MM
7399 stmt = finish_switch_cond (val);
7400 tsubst_expr (SWITCH_BODY (t), args, complain, in_decl);
7401 finish_switch_stmt (val, stmt);
5566b478
MS
7402 }
7403 break;
7404
7405 case CASE_LABEL:
4393e105
MM
7406 finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
7407 tsubst_expr (CASE_HIGH (t), args, complain, in_decl));
5566b478
MS
7408 break;
7409
acef433b
MM
7410 case LABEL_STMT:
7411 lineno = STMT_LINENO (t);
7412 finish_label_stmt (DECL_NAME (LABEL_STMT_LABEL (t)));
5566b478
MS
7413 break;
7414
7415 case GOTO_STMT:
f71f87f9 7416 lineno = STMT_LINENO (t);
aa09da44 7417 t = GOTO_DESTINATION (t);
3fa56191 7418 if (TREE_CODE (t) != LABEL_DECL)
aa09da44
MM
7419 /* Computed goto's must be tsubst'd into. On the other hand,
7420 non-computed gotos must not be; the identifier in question
7421 will have no binding. */
4393e105 7422 t = tsubst_expr (t, args, complain, in_decl);
3fa56191
MM
7423 else
7424 t = DECL_NAME (t);
aa09da44 7425 finish_goto_stmt (t);
ad321293
MM
7426 break;
7427
7428 case ASM_STMT:
f71f87f9 7429 lineno = STMT_LINENO (t);
3ebc5c52 7430 finish_asm_stmt (ASM_CV_QUAL (t),
4393e105
MM
7431 tsubst_expr (ASM_STRING (t), args, complain, in_decl),
7432 tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
7433 tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
7434 tsubst_expr (ASM_CLOBBERS (t), args, complain,
7435 in_decl));
5566b478 7436 break;
faf5394a
MS
7437
7438 case TRY_BLOCK:
f71f87f9 7439 lineno = STMT_LINENO (t);
558475f0 7440 stmt = begin_try_block ();
4393e105 7441 tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
558475f0 7442 finish_try_block (stmt);
f1dedc31
MM
7443 if (CLEANUP_P (t))
7444 finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
7445 complain, in_decl),
7446 stmt);
7447 else
7448 {
7449 tree handler = TRY_HANDLERS (t);
7450 for (; handler; handler = TREE_CHAIN (handler))
7451 tsubst_expr (handler, args, complain, in_decl);
7452 finish_handler_sequence (stmt);
7453 }
faf5394a
MS
7454 break;
7455
7456 case HANDLER:
f71f87f9 7457 lineno = STMT_LINENO (t);
558475f0 7458 stmt = begin_handler ();
ad321293 7459 if (HANDLER_PARMS (t))
3c5c0849
MM
7460 expand_start_catch_block
7461 (tsubst (DECL_STMT_DECL (HANDLER_PARMS (t)),
7462 args, complain, in_decl));
faf5394a 7463 else
3c5c0849 7464 expand_start_catch_block (NULL_TREE);
558475f0 7465 finish_handler_parms (stmt);
4393e105 7466 tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
558475f0 7467 finish_handler (stmt);
faf5394a
MS
7468 break;
7469
b87692e5 7470 case TAG_DEFN:
f71f87f9 7471 lineno = STMT_LINENO (t);
b87692e5
MS
7472 t = TREE_TYPE (t);
7473 if (TREE_CODE (t) == ENUMERAL_TYPE)
4393e105 7474 tsubst (t, args, complain, NULL_TREE);
b87692e5
MS
7475 break;
7476
5566b478 7477 default:
4393e105 7478 return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
5566b478
MS
7479 }
7480 return NULL_TREE;
8d08fdba
MS
7481}
7482
6ba89f8e 7483/* Instantiate the indicated variable or function template TMPL with
36a117a5
MM
7484 the template arguments in TARG_PTR. */
7485
5566b478
MS
7486tree
7487instantiate_template (tmpl, targ_ptr)
98c1c668 7488 tree tmpl, targ_ptr;
8d08fdba 7489{
5566b478 7490 tree fndecl;
36a117a5
MM
7491 tree gen_tmpl;
7492 tree spec;
5566b478
MS
7493 int i, len;
7494 struct obstack *old_fmp_obstack;
7495 extern struct obstack *function_maybepermanent_obstack;
36a117a5 7496 tree inner_args;
5566b478 7497
27fafc8d
JM
7498 if (tmpl == error_mark_node)
7499 return error_mark_node;
7500
386b8a85
JM
7501 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
7502
36a117a5
MM
7503 /* Check to see if we already have this specialization. */
7504 spec = retrieve_specialization (tmpl, targ_ptr);
7505 if (spec != NULL_TREE)
7506 return spec;
7507
7508 if (DECL_TEMPLATE_INFO (tmpl))
386b8a85 7509 {
36a117a5
MM
7510 /* The TMPL is a partial instantiation. To get a full set of
7511 arguments we must add the arguments used to perform the
7512 partial instantiation. */
7513 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
7514 targ_ptr);
7515 gen_tmpl = most_general_template (tmpl);
7516
7517 /* Check to see if we already have this specialization. */
7518 spec = retrieve_specialization (gen_tmpl, targ_ptr);
75650646
MM
7519 if (spec != NULL_TREE)
7520 return spec;
386b8a85 7521 }
36a117a5
MM
7522 else
7523 gen_tmpl = tmpl;
386b8a85 7524
5566b478
MS
7525 push_obstacks (&permanent_obstack, &permanent_obstack);
7526 old_fmp_obstack = function_maybepermanent_obstack;
7527 function_maybepermanent_obstack = &permanent_obstack;
8d08fdba 7528
36a117a5
MM
7529 len = DECL_NTPARMS (gen_tmpl);
7530 inner_args = innermost_args (targ_ptr);
5566b478
MS
7531 i = len;
7532 while (i--)
8d08fdba 7533 {
36a117a5 7534 tree t = TREE_VEC_ELT (inner_args, i);
5566b478
MS
7535 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
7536 {
7537 tree nt = target_type (t);
ec255269 7538 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5566b478 7539 {
8251199e
JM
7540 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
7541 cp_error (" trying to instantiate `%D'", gen_tmpl);
5566b478
MS
7542 fndecl = error_mark_node;
7543 goto out;
7544 }
7545 }
8d08fdba
MS
7546 }
7547
5566b478 7548 /* substitute template parameters */
4393e105 7549 fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, /*complain=*/1, gen_tmpl);
36a117a5
MM
7550 /* The DECL_TI_TEMPLATE should always be the immediate parent
7551 template, not the most general template. */
7552 DECL_TI_TEMPLATE (fndecl) = tmpl;
8d08fdba 7553
824b9a4c
MS
7554 if (flag_external_templates)
7555 add_pending_template (fndecl);
7556
5566b478
MS
7557 out:
7558 function_maybepermanent_obstack = old_fmp_obstack;
7559 pop_obstacks ();
8d08fdba 7560
5566b478 7561 return fndecl;
8d08fdba 7562}
5566b478
MS
7563
7564/* Push the name of the class template into the scope of the instantiation. */
8d08fdba
MS
7565
7566void
5566b478
MS
7567overload_template_name (type)
7568 tree type;
8d08fdba 7569{
5566b478
MS
7570 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
7571 tree decl;
8d08fdba 7572
5566b478
MS
7573 if (IDENTIFIER_CLASS_VALUE (id)
7574 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
7575 return;
8d08fdba 7576
5566b478
MS
7577 decl = build_decl (TYPE_DECL, id, type);
7578 SET_DECL_ARTIFICIAL (decl);
7579 pushdecl_class_level (decl);
8d08fdba
MS
7580}
7581
4393e105
MM
7582/* The FN is a TEMPLATE_DECL for a function. The ARGS are the
7583 arguments that are being used when calling it. TARGS is a vector
7584 into which the deduced template arguments are placed.
8d08fdba
MS
7585
7586 Return zero for success, 2 for an incomplete match that doesn't resolve
7587 all the types, and 1 for complete failure. An error message will be
7588 printed only for an incomplete match.
7589
4393e105
MM
7590 If FN is a conversion operator, RETURN_TYPE is the type desired as
7591 the result of the conversion operator.
8d08fdba 7592
4393e105 7593 TPARMS is a vector of template parameters.
9f54c803
MM
7594
7595 The EXPLICIT_TARGS are explicit template arguments provided via a
7596 template-id.
6467930b 7597
830bfa74
MM
7598 The parameter STRICT is one of:
7599
7600 DEDUCE_CALL:
7601 We are deducing arguments for a function call, as in
7602 [temp.deduct.call].
7603
7604 DEDUCE_CONV:
7605 We are deducing arguments for a conversion function, as in
7606 [temp.deduct.conv].
7607
7608 DEDUCE_EXACT:
7609 We are deducing arguments when calculating the partial
7610 ordering between specializations of function or class
7611 templates, as in [temp.func.order] and [temp.class.order],
7612 when doing an explicit instantiation as in [temp.explicit],
9f54c803 7613 when determining an explicit specialization as in
830bfa74 7614 [temp.expl.spec], or when taking the address of a function
4393e105
MM
7615 template, as in [temp.deduct.funcaddr].
7616
4393e105 7617 The other arguments are as for type_unification. */
8d08fdba
MS
7618
7619int
4393e105 7620fn_type_unification (fn, explicit_targs, targs, args, return_type,
03017874 7621 strict)
4393e105 7622 tree fn, explicit_targs, targs, args, return_type;
830bfa74 7623 unification_kind_t strict;
386b8a85 7624{
4393e105
MM
7625 tree parms;
7626 tree fntype;
adecb3f4 7627 int result;
386b8a85 7628
4393e105
MM
7629 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
7630
7631 fntype = TREE_TYPE (fn);
7632 if (explicit_targs)
75650646 7633 {
4393e105
MM
7634 /* [temp.deduct]
7635
7636 The specified template arguments must match the template
7637 parameters in kind (i.e., type, nontype, template), and there
7638 must not be more arguments than there are parameters;
7639 otherwise type deduction fails.
7640
7641 Nontype arguments must match the types of the corresponding
7642 nontype template parameters, or must be convertible to the
7643 types of the corresponding nontype parameters as specified in
7644 _temp.arg.nontype_, otherwise type deduction fails.
7645
7646 All references in the function type of the function template
7647 to the corresponding template parameters are replaced by the
7648 specified template argument values. If a substitution in a
7649 template parameter or in the function type of the function
7650 template results in an invalid type, type deduction fails. */
7651 int i;
7652 tree converted_args;
75650646 7653
4393e105
MM
7654 converted_args
7655 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7656 explicit_targs, NULL_TREE, /*complain=*/0,
7657 /*require_all_arguments=*/0));
7658 if (converted_args == error_mark_node)
75650646 7659 return 1;
386b8a85 7660
4393e105
MM
7661 fntype = tsubst (fntype, converted_args, /*complain=*/0, NULL_TREE);
7662 if (fntype == error_mark_node)
7663 return 1;
050367a3 7664
4393e105
MM
7665 /* Place the explicitly specified arguments in TARGS. */
7666 for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
7667 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
75650646 7668 }
4393e105
MM
7669
7670 parms = TYPE_ARG_TYPES (fntype);
386b8a85 7671
4393e105
MM
7672 if (DECL_CONV_FN_P (fn))
7673 {
7674 /* This is a template conversion operator. Use the return types
edef8288
JM
7675 as well as the argument types. We use it instead of 'this', since
7676 we could be comparing conversions from different classes. */
4393e105 7677 parms = scratch_tree_cons (NULL_TREE, TREE_TYPE (fntype),
edef8288
JM
7678 TREE_CHAIN (parms));
7679 args = scratch_tree_cons (NULL_TREE, return_type, TREE_CHAIN (args));
4393e105
MM
7680 }
7681
4393e105
MM
7682 /* We allow incomplete unification without an error message here
7683 because the standard doesn't seem to explicitly prohibit it. Our
7684 callers must be ready to deal with unification failures in any
7685 event. */
adecb3f4
MM
7686 result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
7687 targs, parms, args, /*subr=*/0,
7688 strict, /*allow_incomplete*/1);
7689
7690 if (result == 0)
7691 /* All is well so far. Now, check:
7692
7693 [temp.deduct]
7694
7695 When all template arguments have been deduced, all uses of
7696 template parameters in nondeduced contexts are replaced with
7697 the corresponding deduced argument values. If the
7698 substitution results in an invalid type, as described above,
7699 type deduction fails. */
7700 if (tsubst (TREE_TYPE (fn), targs, /*complain=*/0, NULL_TREE)
7701 == error_mark_node)
7702 return 1;
7703
7704 return result;
830bfa74
MM
7705}
7706
7707/* Adjust types before performing type deduction, as described in
7708 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
4c7d0dff
MM
7709 sections are symmetric. PARM is the type of a function parameter
7710 or the return type of the conversion function. ARG is the type of
7711 the argument passed to the call, or the type of the value
7712 intialized with the result of the conversion function. */
386b8a85 7713
e9659ab0 7714static void
830bfa74
MM
7715maybe_adjust_types_for_deduction (strict, parm, arg)
7716 unification_kind_t strict;
7717 tree* parm;
7718 tree* arg;
7719{
7720 switch (strict)
7721 {
7722 case DEDUCE_CALL:
7723 break;
7724
7725 case DEDUCE_CONV:
7726 {
4c7d0dff
MM
7727 /* Swap PARM and ARG throughout the remainder of this
7728 function; the handling is precisely symmetric since PARM
7729 will initialize ARG rather than vice versa. */
830bfa74
MM
7730 tree* temp = parm;
7731 parm = arg;
7732 arg = temp;
7733 break;
7734 }
7735
7736 case DEDUCE_EXACT:
7737 /* There is nothing to do in this case. */
7738 return;
7739
7740 default:
7741 my_friendly_abort (0);
7742 }
7743
7744 if (TREE_CODE (*parm) != REFERENCE_TYPE)
7745 {
7746 /* [temp.deduct.call]
7747
7748 If P is not a reference type:
7749
7750 --If A is an array type, the pointer type produced by the
7751 array-to-pointer standard conversion (_conv.array_) is
7752 used in place of A for type deduction; otherwise,
7753
7754 --If A is a function type, the pointer type produced by
7755 the function-to-pointer standard conversion
7756 (_conv.func_) is used in place of A for type deduction;
7757 otherwise,
7758
7759 --If A is a cv-qualified type, the top level
7760 cv-qualifiers of A's type are ignored for type
7761 deduction. */
7762 if (TREE_CODE (*arg) == ARRAY_TYPE)
7763 *arg = build_pointer_type (TREE_TYPE (*arg));
d8f8dca1 7764 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
830bfa74
MM
7765 *arg = build_pointer_type (*arg);
7766 else
7767 *arg = TYPE_MAIN_VARIANT (*arg);
7768 }
7769
7770 /* [temp.deduct.call]
7771
7772 If P is a cv-qualified type, the top level cv-qualifiers
7773 of P's type are ignored for type deduction. If P is a
7774 reference type, the type referred to by P is used for
7775 type deduction. */
7776 *parm = TYPE_MAIN_VARIANT (*parm);
7777 if (TREE_CODE (*parm) == REFERENCE_TYPE)
7778 *parm = TREE_TYPE (*parm);
386b8a85
JM
7779}
7780
4393e105 7781/* Like type_unfication.
9f54c803
MM
7782
7783 If SUBR is 1, we're being called recursively (to unify the
7784 arguments of a function or method parameter of a function
7785 template). */
386b8a85 7786
4966381a 7787static int
3b3ba9f0 7788type_unification_real (tparms, targs, parms, args, subr,
4393e105 7789 strict, allow_incomplete)
050367a3 7790 tree tparms, targs, parms, args;
830bfa74
MM
7791 int subr;
7792 unification_kind_t strict;
7793 int allow_incomplete;
8d08fdba
MS
7794{
7795 tree parm, arg;
7796 int i;
7797 int ntparms = TREE_VEC_LENGTH (tparms);
830bfa74 7798 int sub_strict;
8d08fdba
MS
7799
7800 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
386b8a85
JM
7801 my_friendly_assert (parms == NULL_TREE
7802 || TREE_CODE (parms) == TREE_LIST, 290);
51c184be 7803 /* ARGS could be NULL (via a call from parse.y to
8d08fdba
MS
7804 build_x_function_call). */
7805 if (args)
7806 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
7807 my_friendly_assert (ntparms > 0, 292);
7808
830bfa74
MM
7809 switch (strict)
7810 {
7811 case DEDUCE_CALL:
7812 sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
7813 break;
7814
7815 case DEDUCE_CONV:
7816 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
7817 break;
7818
7819 case DEDUCE_EXACT:
7820 sub_strict = UNIFY_ALLOW_NONE;
7821 break;
7822
7823 default:
7824 my_friendly_abort (0);
7825 }
7826
8d08fdba
MS
7827 while (parms
7828 && parms != void_list_node
7829 && args
7830 && args != void_list_node)
7831 {
7832 parm = TREE_VALUE (parms);
7833 parms = TREE_CHAIN (parms);
7834 arg = TREE_VALUE (args);
7835 args = TREE_CHAIN (args);
7836
7837 if (arg == error_mark_node)
7838 return 1;
7839 if (arg == unknown_type_node)
34016c81
JM
7840 /* We can't deduce anything from this, but we might get all the
7841 template args from other function args. */
7842 continue;
b7484fbe 7843
03e70705
JM
7844 /* Conversions will be performed on a function argument that
7845 corresponds with a function parameter that contains only
7846 non-deducible template parameters and explicitly specified
7847 template parameters. */
7848 if (! uses_template_parms (parm))
b7484fbe 7849 {
03e70705
JM
7850 tree type;
7851
7852 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7853 type = TREE_TYPE (arg);
7854 else
7855 {
7856 type = arg;
7857 arg = NULL_TREE;
7858 }
7859
830bfa74 7860 if (strict == DEDUCE_EXACT)
03e70705 7861 {
3bfdc719 7862 if (same_type_p (parm, type))
03e70705
JM
7863 continue;
7864 }
03e70705 7865 else
343c89cd
JM
7866 /* It might work; we shouldn't check now, because we might
7867 get into infinite recursion. Overload resolution will
7868 handle it. */
7869 continue;
03e70705 7870
b7484fbe
MS
7871 return 1;
7872 }
7873
8d08fdba
MS
7874 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
7875 {
7876 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
34016c81 7877 if (type_unknown_p (arg))
28cbf42c 7878 {
34016c81
JM
7879 /* [temp.deduct.type] A template-argument can be deduced from
7880 a pointer to function or pointer to member function
7881 argument if the set of overloaded functions does not
7882 contain function templates and at most one of a set of
7883 overloaded functions provides a unique match. */
7884
7885 if (resolve_overloaded_unification
4393e105 7886 (tparms, targs, parm, arg, strict, sub_strict)
34016c81
JM
7887 != 0)
7888 return 1;
7889 continue;
28cbf42c 7890 }
8d08fdba
MS
7891 arg = TREE_TYPE (arg);
7892 }
4393e105 7893
830bfa74
MM
7894 if (!subr)
7895 maybe_adjust_types_for_deduction (strict, &parm, &arg);
db5ae43f 7896
4393e105 7897 switch (unify (tparms, targs, parm, arg, sub_strict))
8d08fdba
MS
7898 {
7899 case 0:
7900 break;
7901 case 1:
7902 return 1;
7903 }
7904 }
7905 /* Fail if we've reached the end of the parm list, and more args
7906 are present, and the parm list isn't variadic. */
7907 if (args && args != void_list_node && parms == void_list_node)
7908 return 1;
7909 /* Fail if parms are left and they don't have default values. */
7910 if (parms
7911 && parms != void_list_node
7912 && TREE_PURPOSE (parms) == NULL_TREE)
7913 return 1;
7914 if (!subr)
7915 for (i = 0; i < ntparms; i++)
050367a3 7916 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
8d08fdba 7917 {
386b8a85 7918 if (!allow_incomplete)
8251199e 7919 error ("incomplete type unification");
8d08fdba
MS
7920 return 2;
7921 }
7922 return 0;
7923}
7924
34016c81
JM
7925/* Subroutine of type_unification_real. Args are like the variables at the
7926 call site. ARG is an overloaded function (or template-id); we try
7927 deducing template args from each of the overloads, and if only one
7928 succeeds, we go with that. Modifies TARGS and returns 0 on success. */
7929
7930static int
7931resolve_overloaded_unification (tparms, targs, parm, arg, strict,
4393e105 7932 sub_strict)
34016c81
JM
7933 tree tparms, targs, parm, arg;
7934 unification_kind_t strict;
7935 int sub_strict;
34016c81
JM
7936{
7937 tree tempargs = copy_node (targs);
7938 int good = 0;
7939
7940 if (TREE_CODE (arg) == ADDR_EXPR)
7941 arg = TREE_OPERAND (arg, 0);
9f3d9e46 7942
d8f8dca1
MM
7943 if (TREE_CODE (arg) == COMPONENT_REF)
7944 /* Handle `&x' where `x' is some static or non-static member
7945 function name. */
7946 arg = TREE_OPERAND (arg, 1);
7947
05e0b2f4
JM
7948 if (TREE_CODE (arg) == OFFSET_REF)
7949 arg = TREE_OPERAND (arg, 1);
7950
9f3d9e46
JM
7951 /* Strip baselink information. */
7952 while (TREE_CODE (arg) == TREE_LIST)
7953 arg = TREE_VALUE (arg);
7954
34016c81
JM
7955 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
7956 {
7957 /* If we got some explicit template args, we need to plug them into
7958 the affected templates before we try to unify, in case the
7959 explicit args will completely resolve the templates in question. */
7960
7961 tree expl_subargs = TREE_OPERAND (arg, 1);
7962 arg = TREE_OPERAND (arg, 0);
7963
7964 for (; arg; arg = OVL_NEXT (arg))
7965 {
7966 tree fn = OVL_CURRENT (arg);
7967 tree subargs, elem;
7968
7969 if (TREE_CODE (fn) != TEMPLATE_DECL)
7970 continue;
7971
7972 subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs);
7973 if (subargs)
7974 {
4393e105
MM
7975 elem = tsubst (TREE_TYPE (fn), subargs, /*complain=*/0,
7976 NULL_TREE);
d8f8dca1
MM
7977 if (TREE_CODE (elem) == METHOD_TYPE)
7978 elem = build_ptrmemfunc_type (build_pointer_type (elem));
e97e5263 7979 good += try_one_overload (tparms, targs, tempargs, parm, elem,
4393e105 7980 strict, sub_strict);
34016c81
JM
7981 }
7982 }
7983 }
7984 else if (TREE_CODE (arg) == OVERLOAD)
7985 {
7986 for (; arg; arg = OVL_NEXT (arg))
d8f8dca1
MM
7987 {
7988 tree type = TREE_TYPE (OVL_CURRENT (arg));
7989 if (TREE_CODE (type) == METHOD_TYPE)
7990 type = build_ptrmemfunc_type (build_pointer_type (type));
7991 good += try_one_overload (tparms, targs, tempargs, parm,
7992 type,
4393e105 7993 strict, sub_strict);
d8f8dca1 7994 }
34016c81
JM
7995 }
7996 else
7997 my_friendly_abort (981006);
7998
7999 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8000 to function or pointer to member function argument if the set of
8001 overloaded functions does not contain function templates and at most
8002 one of a set of overloaded functions provides a unique match.
8003
8004 So if we found multiple possibilities, we return success but don't
8005 deduce anything. */
8006
8007 if (good == 1)
8008 {
8009 int i = TREE_VEC_LENGTH (targs);
8010 for (; i--; )
8011 if (TREE_VEC_ELT (tempargs, i))
8012 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
8013 }
8014 if (good)
8015 return 0;
8016
8017 return 1;
8018}
8019
8020/* Subroutine of resolve_overloaded_unification; does deduction for a single
8021 overload. Fills TARGS with any deduced arguments, or error_mark_node if
8022 different overloads deduce different arguments for a given parm.
8023 Returns 1 on success. */
8024
8025static int
e97e5263 8026try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
4393e105 8027 sub_strict)
e97e5263 8028 tree tparms, orig_targs, targs, parm, arg;
34016c81
JM
8029 unification_kind_t strict;
8030 int sub_strict;
34016c81
JM
8031{
8032 int nargs;
8033 tree tempargs;
8034 int i;
8035
8036 /* [temp.deduct.type] A template-argument can be deduced from a pointer
8037 to function or pointer to member function argument if the set of
8038 overloaded functions does not contain function templates and at most
8039 one of a set of overloaded functions provides a unique match.
8040
8041 So if this is a template, just return success. */
8042
8043 if (uses_template_parms (arg))
8044 return 1;
8045
8046 maybe_adjust_types_for_deduction (strict, &parm, &arg);
8047
8048 /* We don't copy orig_targs for this because if we have already deduced
8049 some template args from previous args, unify would complain when we
8050 try to deduce a template parameter for the same argument, even though
8051 there isn't really a conflict. */
8052 nargs = TREE_VEC_LENGTH (targs);
8053 tempargs = make_scratch_vec (nargs);
8054
4393e105 8055 if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
34016c81
JM
8056 return 0;
8057
8058 /* First make sure we didn't deduce anything that conflicts with
e97e5263 8059 explicitly specified args. */
34016c81
JM
8060 for (i = nargs; i--; )
8061 {
8062 tree elt = TREE_VEC_ELT (tempargs, i);
e97e5263 8063 tree oldelt = TREE_VEC_ELT (orig_targs, i);
34016c81
JM
8064
8065 if (elt == NULL_TREE)
8066 continue;
8067 else if (uses_template_parms (elt))
8068 {
8069 /* Since we're unifying against ourselves, we will fill in template
8070 args used in the function parm list with our own template parms.
8071 Discard them. */
8072 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
8073 continue;
8074 }
8075 else if (oldelt && ! template_args_equal (oldelt, elt))
8076 return 0;
8077 }
8078
8079 for (i = nargs; i--; )
8080 {
8081 tree elt = TREE_VEC_ELT (tempargs, i);
8082
8083 if (elt)
8084 TREE_VEC_ELT (targs, i) = elt;
8085 }
8086
8087 return 1;
8088}
8089
4393e105
MM
8090/* PARM is a template class (perhaps with unbound template
8091 parameters). ARG is a fully instantiated type. If ARG can be
8092 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
8093 TARGS are as for unify. */
fcfb9f96
MM
8094
8095static tree
4393e105 8096try_class_unification (tparms, targs, parm, arg)
fcfb9f96
MM
8097 tree tparms;
8098 tree targs;
4393e105
MM
8099 tree parm;
8100 tree arg;
8101{
8102 int i;
8103 tree copy_of_targs;
8104
8105 if (!CLASSTYPE_TEMPLATE_INFO (arg)
8106 || CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
8107 return NULL_TREE;
8108
8109 /* We need to make a new template argument vector for the call to
8110 unify. If we used TARGS, we'd clutter it up with the result of
8111 the attempted unification, even if this class didn't work out.
8112 We also don't want to commit ourselves to all the unifications
8113 we've already done, since unification is supposed to be done on
8114 an argument-by-argument basis. In other words, consider the
8115 following pathological case:
8116
8117 template <int I, int J, int K>
8118 struct S {};
8119
8120 template <int I, int J>
8121 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
8122
8123 template <int I, int J, int K>
8124 void f(S<I, J, K>, S<I, I, I>);
8125
8126 void g() {
8127 S<0, 0, 0> s0;
8128 S<0, 1, 2> s2;
8129
8130 f(s0, s2);
8131 }
8132
8133 Now, by the time we consider the unification involving `s2', we
8134 already know that we must have `f<0, 0, 0>'. But, even though
8135 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is not legal
8136 because there are two ways to unify base classes of S<0, 1, 2>
8137 with S<I, I, I>. If we kept the already deduced knowledge, we
8138 would reject the possibility I=1. */
8139 push_momentary ();
8140 copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs));
8141 i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
8142 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE);
8143 pop_momentary ();
8144
8145 /* If unification failed, we're done. */
8146 if (i != 0)
8147 return NULL_TREE;
8148 else
8149 return arg;
8150}
8151
8152/* Subroutine of get_template_base. RVAL, if non-NULL, is a base we
8153 have alreay discovered to be satisfactory. ARG_BINFO is the binfo
8154 for the base class of ARG that we are currently examining. */
8155
8156static tree
8157get_template_base_recursive (tparms, targs, parm,
8158 arg_binfo, rval, flags)
8159 tree tparms;
8160 tree targs;
8161 tree arg_binfo;
fcfb9f96 8162 tree rval;
4393e105
MM
8163 tree parm;
8164 int flags;
fcfb9f96
MM
8165{
8166 tree binfos;
8167 int i, n_baselinks;
4393e105 8168 tree arg = BINFO_TYPE (arg_binfo);
fcfb9f96 8169
4393e105 8170 if (!(flags & GTB_IGNORE_TYPE))
fcfb9f96 8171 {
4393e105
MM
8172 tree r = try_class_unification (tparms, targs,
8173 parm, arg);
fcfb9f96 8174
4393e105 8175 /* If there is more than one satisfactory baseclass, then:
fcfb9f96 8176
4393e105
MM
8177 [temp.deduct.call]
8178
8179 If they yield more than one possible deduced A, the type
8180 deduction fails.
8181
8182 applies. */
8183 if (r && rval && !same_type_p (r, rval))
8184 return error_mark_node;
8185 else if (r)
8186 rval = r;
fcfb9f96
MM
8187 }
8188
4393e105 8189 binfos = BINFO_BASETYPES (arg_binfo);
fcfb9f96
MM
8190 n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
8191
8192 /* Process base types. */
8193 for (i = 0; i < n_baselinks; i++)
8194 {
8195 tree base_binfo = TREE_VEC_ELT (binfos, i);
4393e105 8196 int this_virtual;
fcfb9f96 8197
4393e105
MM
8198 /* Skip this base, if we've already seen it. */
8199 if (BINFO_MARKED (base_binfo))
8200 continue;
8201
8202 this_virtual =
8203 (flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
8204
8205 /* When searching for a non-virtual, we cannot mark virtually
8206 found binfos. */
8207 if (! this_virtual)
8208 SET_BINFO_MARKED (base_binfo);
8209
8210 rval = get_template_base_recursive (tparms, targs,
8211 parm,
8212 base_binfo,
8213 rval,
8214 GTB_VIA_VIRTUAL * this_virtual);
8215
8216 /* If we discovered more than one matching base class, we can
8217 stop now. */
8218 if (rval == error_mark_node)
8219 return error_mark_node;
fcfb9f96
MM
8220 }
8221
8222 return rval;
8223}
8224
4393e105
MM
8225/* Given a template type PARM and a class type ARG, find the unique
8226 base type in ARG that is an instance of PARM. We do not examine
8227 ARG itself; only its base-classes. If there is no appropriate base
8228 class, return NULL_TREE. If there is more than one, return
8229 error_mark_node. PARM may be the type of a partial specialization,
8230 as well as a plain template type. Used by unify. */
fcfb9f96
MM
8231
8232static tree
4393e105 8233get_template_base (tparms, targs, parm, arg)
fcfb9f96
MM
8234 tree tparms;
8235 tree targs;
4393e105
MM
8236 tree parm;
8237 tree arg;
fcfb9f96 8238{
4393e105
MM
8239 tree rval;
8240 tree arg_binfo;
fcfb9f96 8241
4393e105
MM
8242 my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
8243
8244 arg_binfo = TYPE_BINFO (complete_type (arg));
8245 rval = get_template_base_recursive (tparms, targs,
8246 parm, arg_binfo,
8247 NULL_TREE,
8248 GTB_IGNORE_TYPE);
fcfb9f96 8249
4393e105
MM
8250 /* Since get_template_base_recursive marks the bases classes, we
8251 must unmark them here. */
d6479fe7 8252 dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
fcfb9f96
MM
8253
8254 return rval;
8255}
8256
db2767b6
MM
8257/* Returns the level of DECL, which declares a template parameter. */
8258
e9659ab0 8259static int
db2767b6
MM
8260template_decl_level (decl)
8261 tree decl;
8262{
8263 switch (TREE_CODE (decl))
8264 {
8265 case TYPE_DECL:
8266 case TEMPLATE_DECL:
8267 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
8268
8269 case PARM_DECL:
8270 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
8271
8272 default:
8273 my_friendly_abort (0);
5ffe581d 8274 return 0;
db2767b6
MM
8275 }
8276}
8277
830bfa74
MM
8278/* Decide whether ARG can be unified with PARM, considering only the
8279 cv-qualifiers of each type, given STRICT as documented for unify.
8280 Returns non-zero iff the unification is OK on that basis.*/
e92cc029 8281
e9659ab0 8282static int
830bfa74
MM
8283check_cv_quals_for_unify (strict, arg, parm)
8284 int strict;
8285 tree arg;
8286 tree parm;
8287{
ef637255
MM
8288 if (!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
8289 && !at_least_as_qualified_p (arg, parm))
8290 return 0;
8291
8292 if (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
8293 && !at_least_as_qualified_p (parm, arg))
8294 return 0;
8295
ef637255 8296 return 1;
830bfa74
MM
8297}
8298
8299/* Takes parameters as for type_unification. Returns 0 if the
8300 type deduction suceeds, 1 otherwise. The parameter STRICT is a
8301 bitwise or of the following flags:
8302
8303 UNIFY_ALLOW_NONE:
8304 Require an exact match between PARM and ARG.
8305 UNIFY_ALLOW_MORE_CV_QUAL:
8306 Allow the deduced ARG to be more cv-qualified than ARG.
8307 UNIFY_ALLOW_LESS_CV_QUAL:
8308 Allow the deduced ARG to be less cv-qualified than ARG.
8309 UNIFY_ALLOW_DERIVED:
8310 Allow the deduced ARG to be a template base class of ARG,
8311 or a pointer to a template base class of the type pointed to by
161c12b0
JM
8312 ARG.
8313 UNIFY_ALLOW_INTEGER:
8314 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
8315 case for more information. */
830bfa74 8316
e9659ab0 8317static int
4393e105 8318unify (tparms, targs, parm, arg, strict)
050367a3 8319 tree tparms, targs, parm, arg;
830bfa74 8320 int strict;
8d08fdba
MS
8321{
8322 int idx;
050367a3 8323 tree targ;
db2767b6 8324 tree tparm;
8d08fdba
MS
8325
8326 /* I don't think this will do the right thing with respect to types.
8327 But the only case I've seen it in so far has been array bounds, where
8328 signedness is the only information lost, and I think that will be
8329 okay. */
8330 while (TREE_CODE (parm) == NOP_EXPR)
8331 parm = TREE_OPERAND (parm, 0);
8332
8333 if (arg == error_mark_node)
8334 return 1;
8335 if (arg == unknown_type_node)
34016c81
JM
8336 /* We can't deduce anything from this, but we might get all the
8337 template args from other function args. */
8338 return 0;
8339
db2767b6 8340 /* If PARM uses template parameters, then we can't bail out here,
6bdb985f 8341 even if ARG == PARM, since we won't record unifications for the
db2767b6
MM
8342 template parameters. We might need them if we're trying to
8343 figure out which of two things is more specialized. */
8344 if (arg == parm && !uses_template_parms (parm))
8d08fdba
MS
8345 return 0;
8346
830bfa74
MM
8347 /* Immediately reject some pairs that won't unify because of
8348 cv-qualification mismatches. */
8349 if (TREE_CODE (arg) == TREE_CODE (parm)
49432171 8350 && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
830bfa74
MM
8351 /* We check the cv-qualifiers when unifying with template type
8352 parameters below. We want to allow ARG `const T' to unify with
8353 PARM `T' for example, when computing which of two templates
8354 is more specialized, for example. */
8355 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
8356 && !check_cv_quals_for_unify (strict, arg, parm))
49432171
JM
8357 return 1;
8358
8d08fdba
MS
8359 switch (TREE_CODE (parm))
8360 {
2ca340ae
JM
8361 case TYPENAME_TYPE:
8362 /* In a type which contains a nested-name-specifier, template
8363 argument values cannot be deduced for template parameters used
8364 within the nested-name-specifier. */
8365 return 0;
8366
8d08fdba 8367 case TEMPLATE_TYPE_PARM:
73b0fce8 8368 case TEMPLATE_TEMPLATE_PARM:
db2767b6
MM
8369 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8370
8371 if (TEMPLATE_TYPE_LEVEL (parm)
8372 != template_decl_level (tparm))
8373 /* The PARM is not one we're trying to unify. Just check
8374 to see if it matches ARG. */
8375 return (TREE_CODE (arg) == TREE_CODE (parm)
3bfdc719 8376 && same_type_p (parm, arg)) ? 0 : 1;
73b0fce8 8377 idx = TEMPLATE_TYPE_IDX (parm);
050367a3 8378 targ = TREE_VEC_ELT (targs, idx);
db2767b6 8379 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
050367a3 8380
73b0fce8 8381 /* Check for mixed types and values. */
db2767b6
MM
8382 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
8383 && TREE_CODE (tparm) != TYPE_DECL)
8384 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
8385 && TREE_CODE (tparm) != TEMPLATE_DECL))
73b0fce8
KL
8386 return 1;
8387
db2767b6 8388 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
73b0fce8 8389 {
7ddedda4 8390 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm))
db2767b6
MM
8391 {
8392 /* We arrive here when PARM does not involve template
8393 specialization. */
73b0fce8 8394
db2767b6
MM
8395 /* ARG must be constructed from a template class. */
8396 if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
8397 return 1;
73b0fce8 8398
db2767b6 8399 {
7ddedda4
MM
8400 tree parmtmpl = TYPE_TI_TEMPLATE (parm);
8401 tree parmvec = TYPE_TI_ARGS (parm);
db2767b6
MM
8402 tree argvec = CLASSTYPE_TI_ARGS (arg);
8403 tree argtmplvec
8404 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
8405 int i;
8406
8407 /* The parameter and argument roles have to be switched here
8408 in order to handle default arguments properly. For example,
8409 template<template <class> class TT> void f(TT<int>)
8410 should be able to accept vector<int> which comes from
744fac59 8411 template <class T, class Allocator = allocator>
73b0fce8
KL
8412 class vector. */
8413
fcfcdfc8 8414 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
db2767b6
MM
8415 == error_mark_node)
8416 return 1;
73b0fce8 8417
6b9b6b15
JM
8418 /* Deduce arguments T, i from TT<T> or TT<i>.
8419 We check each element of PARMVEC and ARGVEC individually
8420 rather than the whole TREE_VEC since they can have
8421 different number of elements. */
8422
db2767b6
MM
8423 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
8424 {
8425 tree t = TREE_VEC_ELT (parmvec, i);
73b0fce8 8426
830bfa74
MM
8427 if (unify (tparms, targs, t,
8428 TREE_VEC_ELT (argvec, i),
4393e105 8429 UNIFY_ALLOW_NONE))
db2767b6
MM
8430 return 1;
8431 }
73b0fce8 8432 }
db2767b6
MM
8433 arg = CLASSTYPE_TI_TEMPLATE (arg);
8434 }
8435 }
8436 else
8437 {
830bfa74
MM
8438 /* If PARM is `const T' and ARG is only `int', we don't have
8439 a match unless we are allowing additional qualification.
8440 If ARG is `const int' and PARM is just `T' that's OK;
8441 that binds `const int' to `T'. */
8442 if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL,
8443 arg, parm))
db2767b6
MM
8444 return 1;
8445
830bfa74
MM
8446 /* Consider the case where ARG is `const volatile int' and
8447 PARM is `const T'. Then, T should be `volatile int'. */
8448 arg =
adecb3f4
MM
8449 cp_build_qualified_type_real (arg,
8450 CP_TYPE_QUALS (arg)
8451 & ~CP_TYPE_QUALS (parm),
8452 /*complain=*/0);
8453 if (arg == error_mark_node)
8454 return 1;
73b0fce8
KL
8455 }
8456
8457 /* Simple cases: Value already set, does match or doesn't. */
4393e105 8458 if (targ != NULL_TREE && same_type_p (targ, arg))
73b0fce8 8459 return 0;
050367a3 8460 else if (targ)
73b0fce8 8461 return 1;
61cd552e
MM
8462
8463 /* Make sure that ARG is not a variable-sized array. (Note that
8464 were talking about variable-sized arrays (like `int[n]'),
8465 rather than arrays of unknown size (like `int[]').) We'll
8466 get very confused by such a type since the bound of the array
8467 will not be computable in an instantiation. Besides, such
8468 types are not allowed in ISO C++, so we can do as we please
8469 here. */
8470 if (TREE_CODE (arg) == ARRAY_TYPE
8471 && !uses_template_parms (arg)
8472 && (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg)))
8473 != INTEGER_CST))
8474 return 1;
8475
050367a3 8476 TREE_VEC_ELT (targs, idx) = arg;
73b0fce8
KL
8477 return 0;
8478
f84b4be9 8479 case TEMPLATE_PARM_INDEX:
db2767b6
MM
8480 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
8481
8482 if (TEMPLATE_PARM_LEVEL (parm)
8483 != template_decl_level (tparm))
8484 /* The PARM is not one we're trying to unify. Just check
8485 to see if it matches ARG. */
8486 return (TREE_CODE (arg) == TREE_CODE (parm)
67ffc812 8487 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
db2767b6 8488
f84b4be9 8489 idx = TEMPLATE_PARM_IDX (parm);
050367a3 8490 targ = TREE_VEC_ELT (targs, idx);
db2767b6 8491
050367a3 8492 if (targ)
8d08fdba 8493 {
67ffc812 8494 int i = (cp_tree_equal (targ, arg) > 0);
312e7d50
JM
8495 if (i == 1)
8496 return 0;
8497 else if (i == 0)
8498 return 1;
8499 else
8500 my_friendly_abort (42);
8d08fdba 8501 }
8d08fdba 8502
161c12b0
JM
8503 /* [temp.deduct.type] If, in the declaration of a function template
8504 with a non-type template-parameter, the non-type
8505 template-parameter is used in an expression in the function
8506 parameter-list and, if the corresponding template-argument is
8507 deduced, the template-argument type shall match the type of the
8508 template-parameter exactly, except that a template-argument
8509 deduced from an array bound may be of any integral type. */
8510 if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm)))
8511 /* OK */;
8512 else if ((strict & UNIFY_ALLOW_INTEGER)
8513 && (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
8514 || TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE))
8515 /* OK */;
8516 else
8517 return 1;
8518
2a1e9fdd 8519 TREE_VEC_ELT (targs, idx) = arg;
8d08fdba
MS
8520 return 0;
8521
8522 case POINTER_TYPE:
830bfa74
MM
8523 {
8524 int sub_strict;
4ac14744 8525
830bfa74
MM
8526 if (TREE_CODE (arg) != POINTER_TYPE)
8527 return 1;
8528
8529 /* [temp.deduct.call]
8530
8531 A can be another pointer or pointer to member type that can
8532 be converted to the deduced A via a qualification
8533 conversion (_conv.qual_).
8534
8535 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
8536 This will allow for additional cv-qualification of the
8537 pointed-to types if appropriate. In general, this is a bit
8538 too generous; we are only supposed to allow qualification
8539 conversions and this method will allow an ARG of char** and
8540 a deduced ARG of const char**. However, overload
8541 resolution will subsequently invalidate the candidate, so
8542 this is probably OK. */
8543 sub_strict = strict;
8544
a7a64a77 8545 if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE)
830bfa74
MM
8546 /* The derived-to-base conversion only persists through one
8547 level of pointers. */
8548 sub_strict &= ~UNIFY_ALLOW_DERIVED;
8549
a7a64a77
MM
8550 return unify (tparms, targs, TREE_TYPE (parm),
8551 TREE_TYPE (arg), sub_strict);
830bfa74 8552 }
8d08fdba
MS
8553
8554 case REFERENCE_TYPE:
830bfa74
MM
8555 if (TREE_CODE (arg) != REFERENCE_TYPE)
8556 return 1;
8557 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 8558 UNIFY_ALLOW_NONE);
8d08fdba
MS
8559
8560 case ARRAY_TYPE:
8561 if (TREE_CODE (arg) != ARRAY_TYPE)
8562 return 1;
3042d5be
MM
8563 if ((TYPE_DOMAIN (parm) == NULL_TREE)
8564 != (TYPE_DOMAIN (arg) == NULL_TREE))
8565 return 1;
8566 if (TYPE_DOMAIN (parm) != NULL_TREE
830bfa74 8567 && unify (tparms, targs, TYPE_DOMAIN (parm),
4393e105 8568 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
8d08fdba 8569 return 1;
830bfa74 8570 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 8571 UNIFY_ALLOW_NONE);
8d08fdba
MS
8572
8573 case REAL_TYPE:
37c46b43 8574 case COMPLEX_TYPE:
8d08fdba 8575 case INTEGER_TYPE:
42976354 8576 case BOOLEAN_TYPE:
5ad5a526 8577 case VOID_TYPE:
f376e137
MS
8578 if (TREE_CODE (arg) != TREE_CODE (parm))
8579 return 1;
8580
514a1f18
JM
8581 if (TREE_CODE (parm) == INTEGER_TYPE
8582 && TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
8d08fdba
MS
8583 {
8584 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
830bfa74 8585 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
4393e105 8586 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
8d08fdba
MS
8587 return 1;
8588 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
830bfa74 8589 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
4393e105 8590 TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER))
8d08fdba
MS
8591 return 1;
8592 }
514a1f18
JM
8593 /* We use the TYPE_MAIN_VARIANT since we have already
8594 checked cv-qualification at the top of the
8595 function. */
3bfdc719
MM
8596 else if (!same_type_p (TYPE_MAIN_VARIANT (arg),
8597 TYPE_MAIN_VARIANT (parm)))
ca79f85d
JM
8598 return 1;
8599
8d08fdba
MS
8600 /* As far as unification is concerned, this wins. Later checks
8601 will invalidate it if necessary. */
8602 return 0;
8603
8604 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
bd6dd845 8605 /* Type INTEGER_CST can come from ordinary constant template args. */
8d08fdba 8606 case INTEGER_CST:
bd6dd845
MS
8607 while (TREE_CODE (arg) == NOP_EXPR)
8608 arg = TREE_OPERAND (arg, 0);
8609
8d08fdba
MS
8610 if (TREE_CODE (arg) != INTEGER_CST)
8611 return 1;
8612 return !tree_int_cst_equal (parm, arg);
8613
8d08fdba
MS
8614 case TREE_VEC:
8615 {
8616 int i;
8617 if (TREE_CODE (arg) != TREE_VEC)
8618 return 1;
8619 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
8620 return 1;
8621 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
830bfa74 8622 if (unify (tparms, targs,
8d08fdba 8623 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
4393e105 8624 UNIFY_ALLOW_NONE))
8d08fdba
MS
8625 return 1;
8626 return 0;
8627 }
8628
8d08fdba 8629 case RECORD_TYPE:
f181d4ae 8630 case UNION_TYPE:
f181d4ae 8631 if (TREE_CODE (arg) != TREE_CODE (parm))
a4443a08 8632 return 1;
830bfa74 8633
a7a64a77
MM
8634 if (TYPE_PTRMEMFUNC_P (parm))
8635 {
8636 if (!TYPE_PTRMEMFUNC_P (arg))
8637 return 1;
8638
8639 return unify (tparms, targs,
8640 TYPE_PTRMEMFUNC_FN_TYPE (parm),
8641 TYPE_PTRMEMFUNC_FN_TYPE (arg),
8642 strict);
8643 }
8644
5db698f6 8645 if (CLASSTYPE_TEMPLATE_INFO (parm))
5566b478 8646 {
6467930b 8647 tree t = NULL_TREE;
4393e105 8648
830bfa74 8649 if (strict & UNIFY_ALLOW_DERIVED)
4393e105
MM
8650 {
8651 /* First, we try to unify the PARM and ARG directly. */
8652 t = try_class_unification (tparms, targs,
8653 parm, arg);
8654
8655 if (!t)
8656 {
8657 /* Fallback to the special case allowed in
8658 [temp.deduct.call]:
8659
8660 If P is a class, and P has the form
8661 template-id, then A can be a derived class of
8662 the deduced A. Likewise, if P is a pointer to
8663 a class of the form template-id, A can be a
8664 pointer to a derived class pointed to by the
8665 deduced A. */
8666 t = get_template_base (tparms, targs,
8667 parm, arg);
8668
8669 if (! t || t == error_mark_node)
8670 return 1;
8671 }
8672 }
6df47b06 8673 else if (CLASSTYPE_TEMPLATE_INFO (arg)
9fbf56f7
MM
8674 && (CLASSTYPE_TI_TEMPLATE (parm)
8675 == CLASSTYPE_TI_TEMPLATE (arg)))
6df47b06
MM
8676 /* Perhaps PARM is something like S<U> and ARG is S<int>.
8677 Then, we should unify `int' and `U'. */
6467930b 8678 t = arg;
4393e105
MM
8679 else
8680 /* There's no chance of unication succeeding. */
5566b478 8681 return 1;
6467930b 8682
830bfa74 8683 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
4393e105 8684 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
5566b478 8685 }
3bfdc719
MM
8686 else if (!same_type_p (TYPE_MAIN_VARIANT (parm),
8687 TYPE_MAIN_VARIANT (arg)))
5566b478 8688 return 1;
a4443a08 8689 return 0;
8d08fdba
MS
8690
8691 case METHOD_TYPE:
8d08fdba 8692 case FUNCTION_TYPE:
830bfa74 8693 if (TREE_CODE (arg) != TREE_CODE (parm))
8d08fdba 8694 return 1;
830bfa74
MM
8695
8696 if (unify (tparms, targs, TREE_TYPE (parm),
4393e105 8697 TREE_TYPE (arg), UNIFY_ALLOW_NONE))
28cbf42c 8698 return 1;
386b8a85 8699 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3b3ba9f0 8700 TYPE_ARG_TYPES (arg), 1,
4393e105 8701 DEDUCE_EXACT, 0);
a4443a08
MS
8702
8703 case OFFSET_TYPE:
8704 if (TREE_CODE (arg) != OFFSET_TYPE)
8705 return 1;
830bfa74 8706 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
4393e105 8707 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
a4443a08 8708 return 1;
830bfa74 8709 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
4393e105 8710 strict);
a4443a08 8711
f62dbf03 8712 case CONST_DECL:
830bfa74 8713 if (arg != decl_constant_value (parm))
f62dbf03
JM
8714 return 1;
8715 return 0;
8716
027905b4
KL
8717 case TEMPLATE_DECL:
8718 /* Matched cases are handled by the ARG == PARM test above. */
8719 return 1;
8720
53929c47
JM
8721 case MINUS_EXPR:
8722 if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
8723 {
8724 /* We handle this case specially, since it comes up with
8725 arrays. In particular, something like:
8726
8727 template <int N> void f(int (&x)[N]);
8728
8729 Here, we are trying to unify the range type, which
8730 looks like [0 ... (N - 1)]. */
8731 tree t, t1, t2;
8732 t1 = TREE_OPERAND (parm, 0);
8733 t2 = TREE_OPERAND (parm, 1);
8734
8735 /* Should this be a regular fold? */
8736 t = maybe_fold_nontype_arg (build (PLUS_EXPR,
8737 integer_type_node,
8738 arg, t2));
8739
4393e105 8740 return unify (tparms, targs, t1, t, strict);
53929c47
JM
8741 }
8742 /* else fall through */
8743
8d08fdba 8744 default:
050367a3 8745 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
4393e105
MM
8746 /* We're looking at an expression. This can happen with
8747 something like:
8748
8749 template <int I>
8750 void foo(S<I>, S<I + 2>);
050367a3 8751
4393e105 8752 This is a "nondeduced context":
050367a3 8753
4393e105
MM
8754 [deduct.type]
8755
8756 The nondeduced contexts are:
050367a3 8757
4393e105
MM
8758 --A type that is a template-id in which one or more of
8759 the template-arguments is an expression that references
8760 a template-parameter.
050367a3 8761
4393e105
MM
8762 In these cases, we assume deduction succeeded, but don't
8763 actually infer any unifications. */
8764 return 0;
050367a3 8765 else
b4c4a9ec
MM
8766 sorry ("use of `%s' in template type unification",
8767 tree_code_name [(int) TREE_CODE (parm)]);
050367a3 8768
8d08fdba
MS
8769 return 1;
8770 }
8771}
8d08fdba 8772\f
03d0f4af
MM
8773/* Called if RESULT is explicitly instantiated, or is a member of an
8774 explicitly instantiated class, or if using -frepo and the
8775 instantiation of RESULT has been assigned to this file. */
8776
faae18ab 8777void
5566b478 8778mark_decl_instantiated (result, extern_p)
faae18ab
MS
8779 tree result;
8780 int extern_p;
8781{
75650646
MM
8782 if (TREE_CODE (result) != FUNCTION_DECL)
8783 /* The TREE_PUBLIC flag for function declarations will have been
8784 set correctly by tsubst. */
8785 TREE_PUBLIC (result) = 1;
faae18ab
MS
8786
8787 if (! extern_p)
8788 {
8789 DECL_INTERFACE_KNOWN (result) = 1;
8790 DECL_NOT_REALLY_EXTERN (result) = 1;
a7d87521 8791
1a408d07
JM
8792 /* Always make artificials weak. */
8793 if (DECL_ARTIFICIAL (result) && flag_weak)
8794 comdat_linkage (result);
a7d87521
JM
8795 /* For WIN32 we also want to put explicit instantiations in
8796 linkonce sections. */
1a408d07 8797 else if (TREE_PUBLIC (result))
b385c841 8798 maybe_make_one_only (result);
faae18ab 8799 }
f49422da
MS
8800 else if (TREE_CODE (result) == FUNCTION_DECL)
8801 mark_inline_for_output (result);
faae18ab
MS
8802}
8803
e1467ff2
MM
8804/* Given two function templates PAT1 and PAT2, and explicit template
8805 arguments EXPLICIT_ARGS return:
6467930b
MS
8806
8807 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
8808 -1 if PAT2 is more specialized than PAT1.
8809 0 if neither is more specialized. */
8810
8811int
e1467ff2
MM
8812more_specialized (pat1, pat2, explicit_args)
8813 tree pat1, pat2, explicit_args;
6467930b 8814{
98c1c668 8815 tree targs;
73aad9b9 8816 int winner = 0;
6467930b 8817
4393e105 8818 targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args);
73aad9b9 8819 if (targs)
36a117a5 8820 --winner;
6467930b 8821
4393e105 8822 targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args);
73aad9b9 8823 if (targs)
36a117a5 8824 ++winner;
6467930b 8825
73aad9b9
JM
8826 return winner;
8827}
6467930b 8828
73aad9b9 8829/* Given two class template specialization list nodes PAT1 and PAT2, return:
6467930b 8830
73aad9b9
JM
8831 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
8832 -1 if PAT2 is more specialized than PAT1.
8833 0 if neither is more specialized. */
8834
8835int
8836more_specialized_class (pat1, pat2)
8837 tree pat1, pat2;
8838{
8839 tree targs;
8840 int winner = 0;
8841
36a117a5
MM
8842 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
8843 TREE_PURPOSE (pat2));
73aad9b9
JM
8844 if (targs)
8845 --winner;
8846
36a117a5
MM
8847 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
8848 TREE_PURPOSE (pat1));
73aad9b9 8849 if (targs)
6467930b
MS
8850 ++winner;
8851
8852 return winner;
8853}
73aad9b9
JM
8854
8855/* Return the template arguments that will produce the function signature
e1467ff2 8856 DECL from the function template FN, with the explicit template
76b9a14d 8857 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
4393e105
MM
8858 also match. Return NULL_TREE if no satisfactory arguments could be
8859 found. */
73aad9b9 8860
76b9a14d
JM
8861static tree
8862get_bindings_real (fn, decl, explicit_args, check_rettype)
e1467ff2 8863 tree fn, decl, explicit_args;
76b9a14d 8864 int check_rettype;
73aad9b9 8865{
98c1c668 8866 int ntparms = DECL_NTPARMS (fn);
e66d884e 8867 tree targs = make_scratch_vec (ntparms);
4393e105 8868 tree decl_type;
03017874 8869 tree decl_arg_types;
98c1c668
JM
8870 int i;
8871
4393e105
MM
8872 /* Substitute the explicit template arguments into the type of DECL.
8873 The call to fn_type_unification will handle substitution into the
8874 FN. */
8875 decl_type = TREE_TYPE (decl);
8876 if (explicit_args && uses_template_parms (decl_type))
8877 {
8878 tree tmpl;
8879 tree converted_args;
8880
8881 if (DECL_TEMPLATE_INFO (decl))
8882 tmpl = DECL_TI_TEMPLATE (decl);
8883 else
8884 /* We can get here for some illegal specializations. */
8885 return NULL_TREE;
8886
8887 converted_args
8888 = (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
8889 explicit_args, NULL_TREE,
8890 /*complain=*/0,
8891 /*require_all_arguments=*/0));
8892 if (converted_args == error_mark_node)
8893 return NULL_TREE;
8894
8895 decl_type = tsubst (decl_type, converted_args, /*complain=*/0,
8896 NULL_TREE);
8897 if (decl_type == error_mark_node)
8898 return NULL_TREE;
8899 }
8900
03017874
MM
8901 /* If FN is a static member function, adjust the type of DECL
8902 appropriately. */
4393e105 8903 decl_arg_types = TYPE_ARG_TYPES (decl_type);
d7684f2d
MM
8904 if (DECL_STATIC_FUNCTION_P (fn)
8905 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
03017874 8906 decl_arg_types = TREE_CHAIN (decl_arg_types);
d7684f2d 8907
e1467ff2 8908 i = fn_type_unification (fn, explicit_args, targs,
03017874
MM
8909 decl_arg_types,
8910 TREE_TYPE (decl_type),
8911 DEDUCE_EXACT);
98c1c668 8912
76b9a14d
JM
8913 if (i != 0)
8914 return NULL_TREE;
8915
8916 if (check_rettype)
e1467ff2
MM
8917 {
8918 /* Check to see that the resulting return type is also OK. */
36a117a5 8919 tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs,
4393e105 8920 /*complain=*/0, NULL_TREE);
e1467ff2 8921
3bfdc719 8922 if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl))))
e1467ff2 8923 return NULL_TREE;
e1467ff2
MM
8924 }
8925
76b9a14d
JM
8926 return targs;
8927}
8928
8929/* For most uses, we want to check the return type. */
8930
8931tree
8932get_bindings (fn, decl, explicit_args)
8933 tree fn, decl, explicit_args;
8934{
8935 return get_bindings_real (fn, decl, explicit_args, 1);
8936}
8937
8938/* But for more_specialized, we only care about the parameter types. */
8939
8940static tree
8941get_bindings_overload (fn, decl, explicit_args)
8942 tree fn, decl, explicit_args;
8943{
8944 return get_bindings_real (fn, decl, explicit_args, 0);
73aad9b9
JM
8945}
8946
36a117a5
MM
8947/* Return the innermost template arguments that, when applied to a
8948 template specialization whose innermost template parameters are
8949 TPARMS, and whose specialization arguments are ARGS, yield the
8950 ARGS.
8951
8952 For example, suppose we have:
8953
8954 template <class T, class U> struct S {};
8955 template <class T> struct S<T*, int> {};
8956
8957 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
8958 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
8959 int}. The resulting vector will be {double}, indicating that `T'
8960 is bound to `double'. */
8961
bd6dd845 8962static tree
36a117a5
MM
8963get_class_bindings (tparms, parms, args)
8964 tree tparms, parms, args;
73aad9b9 8965{
3b3ba9f0 8966 int i, ntparms = TREE_VEC_LENGTH (tparms);
73aad9b9
JM
8967 tree vec = make_temp_vec (ntparms);
8968
36a117a5 8969 args = innermost_args (args);
8d019cef 8970
4393e105 8971 if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE))
fcfb9f96 8972 return NULL_TREE;
73aad9b9
JM
8973
8974 for (i = 0; i < ntparms; ++i)
8975 if (! TREE_VEC_ELT (vec, i))
8976 return NULL_TREE;
8977
8978 return vec;
8979}
8980
104bf76a
MM
8981/* In INSTANTIATIONS is a list of <INSTANTIATION, TEMPLATE> pairs.
8982 Pick the most specialized template, and return the corresponding
8983 instantiation, or if there is no corresponding instantiation, the
8984 template itself. EXPLICIT_ARGS is any template arguments explicity
8985 mentioned in a template-id. If there is no most specialized
8986 tempalte, error_mark_node is returned. If there are no templates
8987 at all, NULL_TREE is returned. */
73aad9b9
JM
8988
8989tree
104bf76a
MM
8990most_specialized_instantiation (instantiations, explicit_args)
8991 tree instantiations;
8992 tree explicit_args;
73aad9b9 8993{
104bf76a 8994 tree fn, champ;
73aad9b9
JM
8995 int fate;
8996
104bf76a 8997 if (!instantiations)
73aad9b9
JM
8998 return NULL_TREE;
8999
104bf76a
MM
9000 champ = instantiations;
9001 for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
73aad9b9 9002 {
104bf76a
MM
9003 fate = more_specialized (TREE_VALUE (champ),
9004 TREE_VALUE (fn), explicit_args);
73aad9b9
JM
9005 if (fate == 1)
9006 ;
9007 else
9008 {
9009 if (fate == 0)
9010 {
9011 fn = TREE_CHAIN (fn);
9012 if (! fn)
9013 return error_mark_node;
9014 }
104bf76a 9015 champ = fn;
73aad9b9
JM
9016 }
9017 }
9018
104bf76a 9019 for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
73aad9b9 9020 {
104bf76a
MM
9021 fate = more_specialized (TREE_VALUE (champ),
9022 TREE_VALUE (fn), explicit_args);
73aad9b9
JM
9023 if (fate != 1)
9024 return error_mark_node;
9025 }
9026
104bf76a
MM
9027 return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
9028}
9029
9030/* Return the most specialized of the list of templates in FNS that can
9031 produce an instantiation matching DECL, given the explicit template
9032 arguments EXPLICIT_ARGS. */
9033
e9659ab0 9034static tree
104bf76a
MM
9035most_specialized (fns, decl, explicit_args)
9036 tree fns, decl, explicit_args;
9037{
9038 tree candidates = NULL_TREE;
9039 tree fn, args;
9040
9041 for (fn = fns; fn; fn = TREE_CHAIN (fn))
9042 {
9043 tree candidate = TREE_VALUE (fn);
9044
9045 args = get_bindings (candidate, decl, explicit_args);
9046 if (args)
9047 candidates = scratch_tree_cons (NULL_TREE, candidate,
9048 candidates);
9049 }
9050
9051 return most_specialized_instantiation (candidates, explicit_args);
73aad9b9
JM
9052}
9053
36a117a5
MM
9054/* If DECL is a specialization of some template, return the most
9055 general such template. For example, given:
9056
9057 template <class T> struct S { template <class U> void f(U); };
9058
9059 if TMPL is `template <class U> void S<int>::f(U)' this will return
9060 the full template. This function will not trace past partial
9061 specializations, however. For example, given in addition:
9062
9063 template <class T> struct S<T*> { template <class U> void f(U); };
9064
9065 if TMPL is `template <class U> void S<int*>::f(U)' this will return
9066 `template <class T> template <class U> S<T*>::f(U)'. */
73aad9b9 9067
e9659ab0 9068static tree
36a117a5
MM
9069most_general_template (decl)
9070 tree decl;
73aad9b9 9071{
36a117a5
MM
9072 while (DECL_TEMPLATE_INFO (decl))
9073 decl = DECL_TI_TEMPLATE (decl);
9074
9075 return decl;
9076}
9077
9078/* Return the most specialized of the class template specializations
9079 of TMPL which can produce an instantiation matching ARGS, or
9080 error_mark_node if the choice is ambiguous. */
9081
e9659ab0 9082static tree
36a117a5
MM
9083most_specialized_class (tmpl, args)
9084 tree tmpl;
9085 tree args;
9086{
9087 tree list = NULL_TREE;
9088 tree t;
9089 tree champ;
73aad9b9
JM
9090 int fate;
9091
36a117a5
MM
9092 tmpl = most_general_template (tmpl);
9093 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
73aad9b9 9094 {
36a117a5
MM
9095 tree spec_args
9096 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
9097 if (spec_args)
73aad9b9
JM
9098 {
9099 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
9100 TREE_TYPE (list) = TREE_TYPE (t);
9101 }
9102 }
9103
9104 if (! list)
9105 return NULL_TREE;
9106
9107 t = list;
9108 champ = t;
9109 t = TREE_CHAIN (t);
9110 for (; t; t = TREE_CHAIN (t))
9111 {
9112 fate = more_specialized_class (champ, t);
9113 if (fate == 1)
9114 ;
9115 else
9116 {
9117 if (fate == 0)
9118 {
9119 t = TREE_CHAIN (t);
9120 if (! t)
9121 return error_mark_node;
9122 }
9123 champ = t;
9124 }
9125 }
9126
9127 for (t = list; t && t != champ; t = TREE_CHAIN (t))
9128 {
85b71cf2 9129 fate = more_specialized_class (champ, t);
73aad9b9
JM
9130 if (fate != 1)
9131 return error_mark_node;
9132 }
9133
9134 return champ;
9135}
9136
8d08fdba 9137/* called from the parser. */
e92cc029 9138
8d08fdba 9139void
6633d636 9140do_decl_instantiation (declspecs, declarator, storage)
f0e01782 9141 tree declspecs, declarator, storage;
8d08fdba 9142{
c11b6f21 9143 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
8d08fdba 9144 tree result = NULL_TREE;
faae18ab 9145 int extern_p = 0;
e8abc66f 9146
3fa56191
MM
9147 if (!decl)
9148 /* An error ocurred, for which grokdeclarator has already issued
9149 an appropriate message. */
9150 return;
9151 else if (! DECL_LANG_SPECIFIC (decl))
ec255269 9152 {
8251199e 9153 cp_error ("explicit instantiation of non-template `%#D'", decl);
ec255269
MS
9154 return;
9155 }
03d0f4af 9156 else if (TREE_CODE (decl) == VAR_DECL)
6633d636 9157 {
03d0f4af
MM
9158 /* There is an asymmetry here in the way VAR_DECLs and
9159 FUNCTION_DECLs are handled by grokdeclarator. In the case of
9160 the latter, the DECL we get back will be marked as a
9161 template instantiation, and the appropriate
9162 DECL_TEMPLATE_INFO will be set up. This does not happen for
9163 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
9164 should handle VAR_DECLs as it currently handles
9165 FUNCTION_DECLs. */
6633d636
MS
9166 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
9167 if (result && TREE_CODE (result) != VAR_DECL)
03d0f4af 9168 {
8251199e 9169 cp_error ("no matching template for `%D' found", result);
03d0f4af
MM
9170 return;
9171 }
6633d636
MS
9172 }
9173 else if (TREE_CODE (decl) != FUNCTION_DECL)
9174 {
8251199e 9175 cp_error ("explicit instantiation of `%#D'", decl);
6633d636
MS
9176 return;
9177 }
03d0f4af
MM
9178 else
9179 result = decl;
672476cb 9180
03d0f4af
MM
9181 /* Check for various error cases. Note that if the explicit
9182 instantiation is legal the RESULT will currently be marked as an
9183 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
9184 until we get here. */
9185
9186 if (DECL_TEMPLATE_SPECIALIZATION (result))
672476cb 9187 {
03d0f4af
MM
9188 /* [temp.spec]
9189
9190 No program shall both explicitly instantiate and explicitly
9191 specialize a template. */
6f1b4c42
JM
9192 cp_pedwarn ("explicit instantiation of `%#D' after", result);
9193 cp_pedwarn_at ("explicit specialization here", result);
672476cb
MM
9194 return;
9195 }
03d0f4af
MM
9196 else if (DECL_EXPLICIT_INSTANTIATION (result))
9197 {
9198 /* [temp.spec]
98c1c668 9199
03d0f4af
MM
9200 No program shall explicitly instantiate any template more
9201 than once.
9202
2aaf816d
JM
9203 We check DECL_INTERFACE_KNOWN so as not to complain when the first
9204 instantiation was `extern' and the second is not, and EXTERN_P for
9205 the opposite case. If -frepo, chances are we already got marked
9206 as an explicit instantion because of the repo file. */
9207 if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
6f1b4c42 9208 cp_pedwarn ("duplicate explicit instantiation of `%#D'", result);
03d0f4af
MM
9209
9210 /* If we've already instantiated the template, just return now. */
9211 if (DECL_INTERFACE_KNOWN (result))
9212 return;
9213 }
9214 else if (!DECL_IMPLICIT_INSTANTIATION (result))
faae18ab 9215 {
8251199e 9216 cp_error ("no matching template for `%D' found", result);
faae18ab
MS
9217 return;
9218 }
03d0f4af 9219 else if (!DECL_TEMPLATE_INFO (result))
6633d636 9220 {
8251199e 9221 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
6633d636
MS
9222 return;
9223 }
9224
a0a33927
MS
9225 if (flag_external_templates)
9226 return;
9227
f0e01782 9228 if (storage == NULL_TREE)
00595019 9229 ;
faae18ab 9230 else if (storage == ridpointers[(int) RID_EXTERN])
03d0f4af
MM
9231 {
9232 if (pedantic)
8251199e 9233 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
03d0f4af
MM
9234 extern_p = 1;
9235 }
f0e01782 9236 else
8251199e 9237 cp_error ("storage class `%D' applied to template instantiation",
f0e01782 9238 storage);
5566b478 9239
03d0f4af 9240 SET_DECL_EXPLICIT_INSTANTIATION (result);
5566b478 9241 mark_decl_instantiated (result, extern_p);
44a8d0b3 9242 repo_template_instantiated (result, extern_p);
c91a56d2
MS
9243 if (! extern_p)
9244 instantiate_decl (result);
7177d104
MS
9245}
9246
faae18ab
MS
9247void
9248mark_class_instantiated (t, extern_p)
9249 tree t;
9250 int extern_p;
9251{
9252 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
eb773359
JM
9253 SET_CLASSTYPE_INTERFACE_KNOWN (t);
9254 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
faae18ab
MS
9255 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
9256 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
9257 if (! extern_p)
9258 {
9259 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
9260 rest_of_type_compilation (t, 1);
9261 }
9262}
e8abc66f 9263
7177d104 9264void
ca79f85d
JM
9265do_type_instantiation (t, storage)
9266 tree t, storage;
7177d104 9267{
e8abc66f
MS
9268 int extern_p = 0;
9269 int nomem_p = 0;
5566b478
MS
9270 int static_p = 0;
9271
ca79f85d
JM
9272 if (TREE_CODE (t) == TYPE_DECL)
9273 t = TREE_TYPE (t);
9274
7ddedda4 9275 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
ca79f85d 9276 {
8251199e 9277 cp_error ("explicit instantiation of non-template type `%T'", t);
ca79f85d
JM
9278 return;
9279 }
9280
5566b478 9281 complete_type (t);
7177d104 9282
a292b002
MS
9283 /* With -fexternal-templates, explicit instantiations are treated the same
9284 as implicit ones. */
a0a33927
MS
9285 if (flag_external_templates)
9286 return;
9287
f0e01782
MS
9288 if (TYPE_SIZE (t) == NULL_TREE)
9289 {
8251199e 9290 cp_error ("explicit instantiation of `%#T' before definition of template",
f0e01782
MS
9291 t);
9292 return;
9293 }
9294
03d0f4af 9295 if (storage != NULL_TREE)
f0e01782 9296 {
03d0f4af 9297 if (pedantic)
8251199e
JM
9298 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
9299 IDENTIFIER_POINTER (storage));
03d0f4af
MM
9300
9301 if (storage == ridpointers[(int) RID_INLINE])
9302 nomem_p = 1;
9303 else if (storage == ridpointers[(int) RID_EXTERN])
9304 extern_p = 1;
9305 else if (storage == ridpointers[(int) RID_STATIC])
9306 static_p = 1;
9307 else
9308 {
8251199e 9309 cp_error ("storage class `%D' applied to template instantiation",
03d0f4af
MM
9310 storage);
9311 extern_p = 0;
9312 }
f0e01782
MS
9313 }
9314
03d0f4af
MM
9315 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
9316 {
9317 /* [temp.spec]
a292b002 9318
03d0f4af
MM
9319 No program shall both explicitly instantiate and explicitly
9320 specialize a template. */
8251199e
JM
9321 cp_error ("explicit instantiation of `%#T' after", t);
9322 cp_error_at ("explicit specialization here", t);
03d0f4af
MM
9323 return;
9324 }
9325 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
44a8d0b3 9326 {
03d0f4af
MM
9327 /* [temp.spec]
9328
9329 No program shall explicitly instantiate any template more
9330 than once.
9331
2aaf816d
JM
9332 If CLASSTYPE_INTERFACE_ONLY, then the first explicit instantiation
9333 was `extern'. If EXTERN_P then the second is. If -frepo, chances
9334 are we already got marked as an explicit instantion because of the
9335 repo file. All these cases are OK. */
9336 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository)
9337 cp_pedwarn ("duplicate explicit instantiation of `%#T'", t);
03d0f4af
MM
9338
9339 /* If we've already instantiated the template, just return now. */
9340 if (!CLASSTYPE_INTERFACE_ONLY (t))
9341 return;
44a8d0b3 9342 }
e8abc66f 9343
03d0f4af
MM
9344 mark_class_instantiated (t, extern_p);
9345 repo_template_instantiated (t, extern_p);
9346
e8abc66f
MS
9347 if (nomem_p)
9348 return;
9349
7177d104 9350 {
db5ae43f 9351 tree tmp;
5566b478 9352
03d0f4af
MM
9353 /* In contrast to implicit instantiation, where only the
9354 declarations, and not the definitions, of members are
9355 instantiated, we have here:
9356
9357 [temp.explicit]
9358
9359 The explicit instantiation of a class template specialization
9360 implies the instantiation of all of its members not
9361 previously explicitly specialized in the translation unit
9362 containing the explicit instantiation.
9363
9364 Of course, we can't instantiate member template classes, since
9365 we don't have any arguments for them. Note that the standard
9366 is unclear on whether the instatiation of the members are
9367 *explicit* instantiations or not. We choose to be generous,
9368 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
9369 the explicit instantiation of a class where some of the members
9370 have no definition in the current translation unit. */
9371
5566b478
MS
9372 if (! static_p)
9373 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
a7d87521 9374 if (TREE_CODE (tmp) == FUNCTION_DECL
1f109f0f 9375 && DECL_TEMPLATE_INSTANTIATION (tmp))
5566b478
MS
9376 {
9377 mark_decl_instantiated (tmp, extern_p);
9378 repo_template_instantiated (tmp, extern_p);
9379 if (! extern_p)
9380 instantiate_decl (tmp);
9381 }
9382
9383 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
9384 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
863adfc0 9385 {
5566b478 9386 mark_decl_instantiated (tmp, extern_p);
863adfc0 9387 repo_template_instantiated (tmp, extern_p);
5566b478
MS
9388 if (! extern_p)
9389 instantiate_decl (tmp);
863adfc0 9390 }
7177d104 9391
a292b002 9392 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
e4a84209
MM
9393 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
9394 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
f376e137 9395 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
a292b002 9396 }
8d08fdba 9397}
a28e3c7f 9398
36a117a5
MM
9399/* Given a function DECL, which is a specialization of TMPL, modify
9400 DECL to be a re-instantiation of TMPL with the same template
9401 arguments. TMPL should be the template into which tsubst'ing
9402 should occur for DECL, not the most general template.
830bfa74
MM
9403
9404 One reason for doing this is a scenario like this:
9405
9406 template <class T>
9407 void f(const T&, int i);
9408
9409 void g() { f(3, 7); }
9410
9411 template <class T>
9412 void f(const T& t, const int i) { }
9413
9414 Note that when the template is first instantiated, with
9415 instantiate_template, the resulting DECL will have no name for the
9416 first parameter, and the wrong type for the second. So, when we go
9417 to instantiate the DECL, we regenerate it. */
9418
e9659ab0 9419static void
830bfa74
MM
9420regenerate_decl_from_template (decl, tmpl)
9421 tree decl;
9422 tree tmpl;
9423{
9424 tree args;
830bfa74
MM
9425 tree code_pattern;
9426 tree new_decl;
36a117a5
MM
9427 tree gen_tmpl;
9428 int unregistered;
830bfa74
MM
9429
9430 args = DECL_TI_ARGS (decl);
9431 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
9432
8251199e
JM
9433 /* Unregister the specialization so that when we tsubst we will not
9434 just return DECL. We don't have to unregister DECL from TMPL
9435 because if would only be registered there if it were a partial
9436 instantiation of a specialization, which it isn't: it's a full
9437 instantiation. */
36a117a5
MM
9438 gen_tmpl = most_general_template (tmpl);
9439 unregistered = unregister_specialization (decl, gen_tmpl);
9440
9441 /* If the DECL was not unregistered then something peculiar is
9442 happening: we created a specialization but did not call
9443 register_specialization for it. */
9444 my_friendly_assert (unregistered, 0);
9445
61a127b3
MM
9446 if (TREE_CODE (decl) == VAR_DECL)
9447 /* Make sure that we can see identifiers, and compute access
9448 correctly, for the class members used in the declaration of
9449 this static variable. */
9450 pushclass (DECL_CONTEXT (decl), 2);
9451
d8c4447d 9452 /* Do the substitution to get the new declaration. */
4393e105 9453 new_decl = tsubst (code_pattern, args, /*complain=*/1, NULL_TREE);
830bfa74
MM
9454
9455 if (TREE_CODE (decl) == VAR_DECL)
9456 {
9457 /* Set up DECL_INITIAL, since tsubst doesn't. */
830bfa74 9458 DECL_INITIAL (new_decl) =
d8c4447d 9459 tsubst_expr (DECL_INITIAL (code_pattern), args,
4393e105 9460 /*complain=*/1, DECL_TI_TEMPLATE (decl));
61a127b3 9461 /* Pop the class context we pushed above. */
b74a0560 9462 popclass ();
830bfa74 9463 }
2b0a63a3 9464 else if (TREE_CODE (decl) == FUNCTION_DECL)
fbf1c34b
MM
9465 {
9466 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
9467 new decl. */
9468 DECL_INITIAL (new_decl) = error_mark_node;
9469 /* And don't complain about a duplicate definition. */
9470 DECL_INITIAL (decl) = NULL_TREE;
9471 }
36a117a5
MM
9472
9473 /* The immediate parent of the new template is still whatever it was
9474 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
9475 general template. We also reset the DECL_ASSEMBLER_NAME since
9476 tsubst always calculates the name as if the function in question
9477 were really a template instance, and sometimes, with friend
9478 functions, this is not so. See tsubst_friend_function for
9479 details. */
9480 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
9481 DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl);
9482 DECL_RTL (new_decl) = DECL_RTL (decl);
9483
9484 /* Call duplicate decls to merge the old and new declarations. */
830bfa74
MM
9485 duplicate_decls (new_decl, decl);
9486
36a117a5
MM
9487 /* Now, re-register the specialization. */
9488 register_specialization (decl, gen_tmpl, args);
830bfa74
MM
9489}
9490
f84b4be9
JM
9491/* Produce the definition of D, a _DECL generated from a template. */
9492
a28e3c7f 9493tree
5566b478
MS
9494instantiate_decl (d)
9495 tree d;
a28e3c7f 9496{
36a117a5
MM
9497 tree tmpl = DECL_TI_TEMPLATE (d);
9498 tree args = DECL_TI_ARGS (d);
830bfa74 9499 tree td;
36a117a5
MM
9500 tree code_pattern;
9501 tree spec;
9502 tree gen_tmpl;
5566b478 9503 int nested = in_function_p ();
5566b478 9504 int pattern_defined;
c27be9b9
JM
9505 int line = lineno;
9506 char *file = input_filename;
5566b478 9507
36a117a5
MM
9508 /* This function should only be used to instantiate templates for
9509 functions and static member variables. */
9510 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
9511 || TREE_CODE (d) == VAR_DECL, 0);
9512
fbf1c34b 9513 if (DECL_TEMPLATE_INSTANTIATED (d))
03d0f4af
MM
9514 /* D has already been instantiated. It might seem reasonable to
9515 check whether or not D is an explict instantiation, and, if so,
9516 stop here. But when an explicit instantiation is deferred
9517 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
9518 is set, even though we still need to do the instantiation. */
36a117a5
MM
9519 return d;
9520
9521 /* If we already have a specialization of this declaration, then
9522 there's no reason to instantiate it. Note that
9523 retrieve_specialization gives us both instantiations and
9524 specializations, so we must explicitly check
9525 DECL_TEMPLATE_SPECIALIZATION. */
9526 gen_tmpl = most_general_template (tmpl);
9527 spec = retrieve_specialization (gen_tmpl, args);
9528 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
9529 return spec;
9530
9531 /* This needs to happen before any tsubsting. */
9532 if (! push_tinst_level (d))
9533 return d;
9534
4d85e00e
MM
9535 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
9536 for the instantiation. This is not always the most general
9537 template. Consider, for example:
9538
9539 template <class T>
9540 struct S { template <class U> void f();
9541 template <> void f<int>(); };
9542
9543 and an instantiation of S<double>::f<int>. We want TD to be the
9544 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
9545 td = tmpl;
9546 for (td = tmpl;
9547 /* An instantiation cannot have a definition, so we need a
9548 more general template. */
9549 DECL_TEMPLATE_INSTANTIATION (td)
9550 /* We must also deal with friend templates. Given:
9551
9552 template <class T> struct S {
9553 template <class U> friend void f() {};
9554 };
9555
9556 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
9557 so far as the language is concerned, but that's still
9558 where we get the pattern for the instantiation from. On
9559 ther hand, if the definition comes outside the class, say:
9560
9561 template <class T> struct S {
9562 template <class U> friend void f();
9563 };
9564 template <class U> friend void f() {}
9565
9566 we don't need to look any further. That's what the check for
9567 DECL_INITIAL is for. */
9568 || (TREE_CODE (d) == FUNCTION_DECL
61289ca3 9569 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
4d85e00e 9570 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)));
f84b4be9 9571 )
4d85e00e
MM
9572 {
9573 /* The present template, TD, should not be a definition. If it
9574 were a definition, we should be using it! Note that we
9575 cannot restructure the loop to just keep going until we find
9576 a template with a definition, since that might go too far if
9577 a specialization was declared, but not defined. */
4d85e00e
MM
9578 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
9579 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
9580 0);
9581
9582 /* Fetch the more general template. */
9583 td = DECL_TI_TEMPLATE (td);
9584 }
27bb8339 9585
fee23f54 9586 code_pattern = DECL_TEMPLATE_RESULT (td);
27bb8339 9587
5566b478 9588 if (TREE_CODE (d) == FUNCTION_DECL)
36a117a5 9589 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
5566b478 9590 else
36a117a5 9591 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
de22184b
MS
9592
9593 push_to_top_level ();
9594 lineno = DECL_SOURCE_LINE (d);
9595 input_filename = DECL_SOURCE_FILE (d);
9596
de22184b 9597 if (pattern_defined)
5566b478
MS
9598 {
9599 repo_template_used (d);
9600
9601 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
9602 {
9603 if (flag_alt_external_templates)
9604 {
9605 if (interface_unknown)
9606 warn_if_unknown_interface (d);
9607 }
fee23f54 9608 else if (DECL_INTERFACE_KNOWN (code_pattern))
5566b478
MS
9609 {
9610 DECL_INTERFACE_KNOWN (d) = 1;
fee23f54 9611 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
5566b478
MS
9612 }
9613 else
fee23f54 9614 warn_if_unknown_interface (code_pattern);
5566b478
MS
9615 }
9616
e92cc029 9617 if (at_eof)
5566b478
MS
9618 import_export_decl (d);
9619 }
9620
c4ae3f91
JM
9621 /* Reject all external templates except inline functions. */
9622 if (DECL_INTERFACE_KNOWN (d)
9623 && ! DECL_NOT_REALLY_EXTERN (d)
9624 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
9625 goto out;
9626
67ffc812 9627 if (TREE_CODE (d) == VAR_DECL
711734a9 9628 && TREE_READONLY (d)
67ffc812
MM
9629 && DECL_INITIAL (d) == NULL_TREE
9630 && DECL_INITIAL (code_pattern) != NULL_TREE)
9631 /* We need to set up DECL_INITIAL regardless of pattern_defined if
9632 the variable is a static const initialized in the class body. */;
9633 else if (! pattern_defined
9634 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
9635 && ! at_eof))
5566b478 9636 {
67ffc812 9637 /* Defer all templates except inline functions used in another
c27be9b9
JM
9638 function. We restore the source position here because it's used
9639 by add_pending_template. */
9640 lineno = line;
9641 input_filename = file;
9642
03d0f4af
MM
9643 if (at_eof && !pattern_defined
9644 && DECL_EXPLICIT_INSTANTIATION (d))
9645 /* [temp.explicit]
9646
9647 The definition of a non-exported function template, a
9648 non-exported member function template, or a non-exported
9649 member function or static data member of a class template
9650 shall be present in every translation unit in which it is
9651 explicitly instantiated. */
8251199e 9652 cp_error ("explicit instantiation of `%D' but no definition available",
03d0f4af
MM
9653 d);
9654
5566b478 9655 add_pending_template (d);
de22184b 9656 goto out;
5566b478
MS
9657 }
9658
2b0a63a3
MM
9659 /* We're now committed to instantiating this template. Mark it as
9660 instantiated so that recursive calls to instantiate_decl do not
9661 try to instantiate it again. */
fbf1c34b 9662 DECL_TEMPLATE_INSTANTIATED (d) = 1;
120722ac 9663
2b0a63a3
MM
9664 /* Regenerate the declaration in case the template has been modified
9665 by a subsequent redeclaration. */
9666 regenerate_decl_from_template (d, td);
9667
120722ac
MM
9668 /* We already set the file and line above. Reset them now in case
9669 they changed as a result of calling regenerate_decl_from_template. */
5156628f
MS
9670 lineno = DECL_SOURCE_LINE (d);
9671 input_filename = DECL_SOURCE_FILE (d);
9672
5566b478
MS
9673 if (TREE_CODE (d) == VAR_DECL)
9674 {
9675 DECL_IN_AGGR_P (d) = 0;
9676 if (DECL_INTERFACE_KNOWN (d))
9677 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
9678 else
9679 {
9680 DECL_EXTERNAL (d) = 1;
9681 DECL_NOT_REALLY_EXTERN (d) = 1;
9682 }
9683 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
9684 }
9685 else if (TREE_CODE (d) == FUNCTION_DECL)
9686 {
558475f0
MM
9687 extern struct obstack *saveable_obstack;
9688 extern struct obstack *rtl_obstack;
9689 int saved_expanding_p = expanding_p;
9690
9691 /* We're not expanding all the way to RTL here. */
9692 expanding_p = 0;
5566b478 9693
558475f0 9694 /* Set up context. */
c11b6f21 9695 start_function (NULL_TREE, d, NULL_TREE, 1);
5566b478
MS
9696 store_parm_decls ();
9697
558475f0
MM
9698 /* Anything we might
9699 want to save is going to have to be saved forever. Note that
9700 we don't want to save all kinds of temporary clutter that
9701 might end up on the temporary obstack so we don't want to
9702 call push_permanent_obstack. */
9703 push_obstacks_nochange ();
9704 saveable_obstack = &permanent_obstack;
9705 /* We only need this because of the cases where we generate
9706 RTL_EXPRs. We should really be generating RTL_EXPRs until
9707 final expansion time; when that is fixed, this can go. */
9708 rtl_obstack = &permanent_obstack;
9709 /* Substitute into the body of the function. */
9710 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
9711 /*complain=*/1, tmpl);
9712
9713 /* Clean up. */
9714 pop_obstacks ();
fdfcc44c 9715 finish_function (lineno, 0);
558475f0
MM
9716 expanding_p = saved_expanding_p;
9717
9718 /* Now, generate RTL for the function. */
9719 expand_body (d);
5566b478
MS
9720 }
9721
de22184b 9722out:
c27be9b9
JM
9723 lineno = line;
9724 input_filename = file;
9725
5566b478 9726 pop_from_top_level ();
5566b478 9727 pop_tinst_level ();
a28e3c7f 9728
a28e3c7f
MS
9729 return d;
9730}
5566b478 9731
0aafb128
MM
9732/* Run through the list of templates that we wish we could
9733 instantiate, and instantiate any we can. */
9734
9735int
9736instantiate_pending_templates ()
9737{
9738 tree *t;
9739 int instantiated_something = 0;
9740 int reconsider;
9741
9742 do
9743 {
9744 reconsider = 0;
9745
9746 t = &pending_templates;
9747 while (*t)
9748 {
9749 tree srcloc = TREE_PURPOSE (*t);
9750 tree instantiation = TREE_VALUE (*t);
9751
9752 input_filename = SRCLOC_FILE (srcloc);
9753 lineno = SRCLOC_LINE (srcloc);
9754
9755 if (TREE_CODE_CLASS (TREE_CODE (instantiation)) == 't')
9756 {
9757 tree fn;
9758
9759 if (!TYPE_SIZE (instantiation))
9760 {
9761 instantiate_class_template (instantiation);
9762 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
9763 for (fn = TYPE_METHODS (instantiation);
9764 fn;
9765 fn = TREE_CHAIN (fn))
9766 if (! DECL_ARTIFICIAL (fn))
9767 instantiate_decl (fn);
9768 if (TYPE_SIZE (instantiation))
9769 {
9770 instantiated_something = 1;
9771 reconsider = 1;
9772 }
9773 }
9774
9775 if (TYPE_SIZE (instantiation))
9776 /* If INSTANTIATION has been instantiated, then we don't
9777 need to consider it again in the future. */
9778 *t = TREE_CHAIN (*t);
9779 else
9780 t = &TREE_CHAIN (*t);
9781 }
9782 else
9783 {
9784 if (DECL_TEMPLATE_INSTANTIATION (instantiation)
9785 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
9786 {
9787 instantiation = instantiate_decl (instantiation);
9788 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
9789 {
9790 instantiated_something = 1;
9791 reconsider = 1;
9792 }
9793 }
9794
9795 if (!DECL_TEMPLATE_INSTANTIATION (instantiation)
9796 || DECL_TEMPLATE_INSTANTIATED (instantiation))
9797 /* If INSTANTIATION has been instantiated, then we don't
9798 need to consider it again in the future. */
9799 *t = TREE_CHAIN (*t);
9800 else
9801 t = &TREE_CHAIN (*t);
9802 }
9803 }
9804 template_tail = t;
9805
9806 /* Go through the things that are template instantiations if we are
9807 using guiding declarations. */
9808 t = &maybe_templates;
9809 while (*t)
9810 {
9811 tree template;
9812 tree fn;
9813 tree args;
9814
9815 fn = TREE_VALUE (*t);
9816
9817 if (DECL_INITIAL (fn))
9818 /* If the FN is already defined, then it was either already
9819 instantiated or, even though guiding declarations were
9820 allowed, a non-template definition was provided. */
9821 ;
9822 else
9823 {
9824 template = TREE_PURPOSE (*t);
9825 args = get_bindings (template, fn, NULL_TREE);
9826 fn = instantiate_template (template, args);
9827 instantiate_decl (fn);
9828 reconsider = 1;
9829 }
9830
9831 /* Remove this entry from the chain. */
9832 *t = TREE_CHAIN (*t);
9833 }
9834 maybe_template_tail = t;
9835 }
9836 while (reconsider);
9837
9838 return instantiated_something;
9839}
9840
4393e105
MM
9841/* Substitute ARGVEC into T, which is a TREE_LIST. In particular, it
9842 is an initializer list: the TREE_PURPOSEs are DECLs, and the
9843 TREE_VALUEs are initializer values. Used by instantiate_decl. */
9844
824b9a4c 9845static tree
5566b478
MS
9846tsubst_expr_values (t, argvec)
9847 tree t, argvec;
9848{
9849 tree first = NULL_TREE;
9850 tree *p = &first;
9851
9852 for (; t; t = TREE_CHAIN (t))
9853 {
4393e105
MM
9854 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
9855 /*complain=*/1, NULL_TREE);
9856 tree val = tsubst_expr (TREE_VALUE (t), argvec, /*complain=*/1,
9857 NULL_TREE);
5566b478
MS
9858 *p = build_tree_list (pur, val);
9859 p = &TREE_CHAIN (*p);
9860 }
9861 return first;
9862}
9863
5566b478
MS
9864void
9865add_tree (t)
9866 tree t;
9867{
9868 last_tree = TREE_CHAIN (last_tree) = t;
9869}
73aad9b9 9870
75650646
MM
9871
9872void
9873begin_tree ()
9874{
9875 saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
9876 last_tree = NULL_TREE;
9877}
9878
9879
9880void
9881end_tree ()
9882{
9883 my_friendly_assert (saved_trees != NULL_TREE, 0);
9884
9885 last_tree = TREE_VALUE (saved_trees);
9886 saved_trees = TREE_CHAIN (saved_trees);
9887}
9888
73aad9b9
JM
9889/* D is an undefined function declaration in the presence of templates with
9890 the same name, listed in FNS. If one of them can produce D as an
9891 instantiation, remember this so we can instantiate it at EOF if D has
9892 not been defined by that time. */
9893
9894void
9895add_maybe_template (d, fns)
9896 tree d, fns;
9897{
9898 tree t;
9899
9900 if (DECL_MAYBE_TEMPLATE (d))
9901 return;
9902
e1467ff2 9903 t = most_specialized (fns, d, NULL_TREE);
73aad9b9
JM
9904 if (! t)
9905 return;
9906 if (t == error_mark_node)
9907 {
8251199e 9908 cp_error ("ambiguous template instantiation for `%D'", d);
73aad9b9
JM
9909 return;
9910 }
9911
9912 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
9913 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
9914 DECL_MAYBE_TEMPLATE (d) = 1;
9915}
b87692e5 9916
61a127b3
MM
9917/* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
9918
9919static void
9920set_current_access_from_decl (decl)
9921 tree decl;
9922{
9923 if (TREE_PRIVATE (decl))
9924 current_access_specifier = access_private_node;
9925 else if (TREE_PROTECTED (decl))
9926 current_access_specifier = access_protected_node;
9927 else
9928 current_access_specifier = access_public_node;
9929}
9930
dbfe2124
MM
9931/* Instantiate an enumerated type. TAG is the template type, NEWTAG
9932 is the instantiation (which should have been created with
9933 start_enum) and ARGS are the template arguments to use. */
b87692e5 9934
dbfe2124
MM
9935static void
9936tsubst_enum (tag, newtag, args)
9937 tree tag;
9938 tree newtag;
9939 tree args;
b87692e5 9940{
dbfe2124 9941 tree e;
b87692e5
MS
9942
9943 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
9944 {
61a127b3
MM
9945 tree value;
9946 tree elt;
9947
9948 /* Note that in a template enum, the TREE_VALUE is the
9949 CONST_DECL, not the corresponding INTEGER_CST. */
9950 value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
4393e105
MM
9951 args, /*complain=*/1,
9952 NULL_TREE);
61a127b3
MM
9953
9954 /* Give this enumeration constant the correct access. */
9955 set_current_access_from_decl (TREE_VALUE (e));
9956
9957 /* Actually build the enumerator itself. */
9958 elt = build_enumerator (TREE_PURPOSE (e), value, newtag);
b87692e5 9959
dbfe2124
MM
9960 /* We save the enumerators we have built so far in the
9961 TYPE_VALUES so that if the enumeration constants for
9962 subsequent enumerators involve those for previous ones,
9963 tsubst_copy will be able to find them. */
9964 TREE_CHAIN (elt) = TYPE_VALUES (newtag);
9965 TYPE_VALUES (newtag) = elt;
9966 }
b3d5a58b 9967
dbfe2124 9968 finish_enum (newtag);
b87692e5 9969}
36a117a5
MM
9970
9971/* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
9972 is either an instantiation or specialization of a template
9973 function. */
9974
9975static void
9976set_mangled_name_for_template_decl (decl)
9977 tree decl;
9978{
9979 tree saved_namespace;
b370501f 9980 tree context = NULL_TREE;
36a117a5
MM
9981 tree fn_type;
9982 tree ret_type;
9983 tree parm_types;
9984 tree tparms;
9985 tree targs;
9986 tree tmpl;
9987 int parm_depth;
9988
9989 my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0);
9990 my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0);
9991
9992 /* The names of template functions must be mangled so as to indicate
9993 what template is being specialized with what template arguments.
9994 For example, each of the following three functions must get
9995 different mangled names:
9996
9997 void f(int);
9998 template <> void f<7>(int);
9999 template <> void f<8>(int); */
10000
10001 targs = DECL_TI_ARGS (decl);
10002 if (uses_template_parms (targs))
10003 /* This DECL is for a partial instantiation. There's no need to
10004 mangle the name of such an entity. */
10005 return;
10006
10007 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
10008 tparms = DECL_TEMPLATE_PARMS (tmpl);
10009 parm_depth = TMPL_PARMS_DEPTH (tparms);
10010
10011 /* There should be as many levels of arguments as there are levels
10012 of parameters. */
10013 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
10014
10015 /* We now compute the PARMS and RET_TYPE to give to
10016 build_decl_overload_real. The PARMS and RET_TYPE are the
10017 parameter and return types of the template, after all but the
10018 innermost template arguments have been substituted, not the
10019 parameter and return types of the function DECL. For example,
10020 given:
10021
10022 template <class T> T f(T);
10023
10024 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
10025 A more subtle example is:
10026
10027 template <class T> struct S { template <class U> void f(T, U); }
10028
10029 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
10030 {int, U}. Thus, the args that we want to subsitute into the
10031 return and parameter type for the function are those in TARGS,
10032 with the innermost level omitted. */
10033 fn_type = TREE_TYPE (tmpl);
10034 if (DECL_STATIC_FUNCTION_P (decl))
61a127b3 10035 context = DECL_CLASS_CONTEXT (decl);
36a117a5
MM
10036
10037 if (parm_depth == 1)
10038 /* No substitution is necessary. */
10039 ;
10040 else
10041 {
10042 int i;
10043 tree partial_args;
10044
10045 /* Replace the innermost level of the TARGS with NULL_TREEs to
10046 let tsubst know not to subsitute for those parameters. */
10047 partial_args = make_temp_vec (TREE_VEC_LENGTH (targs));
10048 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
10049 SET_TMPL_ARGS_LEVEL (partial_args, i,
10050 TMPL_ARGS_LEVEL (targs, i));
10051 SET_TMPL_ARGS_LEVEL (partial_args,
10052 TMPL_ARGS_DEPTH (targs),
10053 make_temp_vec (DECL_NTPARMS (tmpl)));
10054
10055 /* Now, do the (partial) substitution to figure out the
10056 appropriate function type. */
4393e105 10057 fn_type = tsubst (fn_type, partial_args, /*complain=*/1, NULL_TREE);
36a117a5 10058 if (DECL_STATIC_FUNCTION_P (decl))
4393e105 10059 context = tsubst (context, partial_args, /*complain=*/1, NULL_TREE);
36a117a5
MM
10060
10061 /* Substitute into the template parameters to obtain the real
10062 innermost set of parameters. This step is important if the
10063 innermost set of template parameters contains value
10064 parameters whose types depend on outer template parameters. */
10065 TREE_VEC_LENGTH (partial_args)--;
4393e105 10066 tparms = tsubst_template_parms (tparms, partial_args, /*complain=*/1);
36a117a5
MM
10067 }
10068
10069 /* Now, get the innermost parameters and arguments, and figure out
10070 the parameter and return types. */
10071 tparms = INNERMOST_TEMPLATE_PARMS (tparms);
10072 targs = innermost_args (targs);
10073 ret_type = TREE_TYPE (fn_type);
10074 parm_types = TYPE_ARG_TYPES (fn_type);
10075
10076 /* For a static member function, we generate a fake `this' pointer,
10077 for the purposes of mangling. This indicates of which class the
10078 function is a member. Because of:
10079
10080 [class.static]
10081
10082 There shall not be a static and a nonstatic member function
10083 with the same name and the same parameter types
10084
10085 we don't have to worry that this will result in a clash with a
10086 non-static member function. */
10087 if (DECL_STATIC_FUNCTION_P (decl))
10088 parm_types = hash_tree_chain (build_pointer_type (context), parm_types);
10089
10090 /* There should be the same number of template parameters as
10091 template arguments. */
10092 my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs),
10093 0);
10094
10095 /* If the template is in a namespace, we need to put that into the
10096 mangled name. Unfortunately, build_decl_overload_real does not
10097 get the decl to mangle, so it relies on the current
10098 namespace. Therefore, we set that here temporarily. */
10099 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702);
10100 saved_namespace = current_namespace;
10101 current_namespace = CP_DECL_CONTEXT (decl);
10102
10103 /* Actually set the DCL_ASSEMBLER_NAME. */
10104 DECL_ASSEMBLER_NAME (decl)
10105 = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type,
10106 tparms, targs,
10107 DECL_FUNCTION_MEMBER_P (decl)
10108 + DECL_CONSTRUCTOR_P (decl));
10109
10110 /* Restore the previously active namespace. */
10111 current_namespace = saved_namespace;
10112}
This page took 2.073231 seconds and 5 git commands to generate.