iomanip.h: Use __extension__ for `extern' explicit template instantiations.
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 /* Known bugs or deficiencies include:
24
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". */
27
28 #include "config.h"
29 #include "system.h"
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"
37 #include "lex.h"
38 #include "output.h"
39 #include "defaults.h"
40 #include "except.h"
41 #include "toplev.h"
42
43 /* The type of functions taking a tree, and some additional data, and
44 returning an int. */
45 typedef int (*tree_fn_t) PROTO((tree, void*));
46
47 extern struct obstack permanent_obstack;
48
49 extern int lineno;
50 extern char *input_filename;
51 struct pending_inline *pending_template_expansions;
52
53 tree current_template_parms;
54 HOST_WIDE_INT processing_template_decl;
55
56 tree pending_templates;
57 static tree *template_tail = &pending_templates;
58
59 tree maybe_templates;
60 static tree *maybe_template_tail = &maybe_templates;
61
62 int minimal_parse_mode;
63
64 int processing_specialization;
65 int processing_explicit_instantiation;
66 int processing_template_parmlist;
67 static int template_header_count;
68
69 static tree saved_trees;
70
71 #define obstack_chunk_alloc xmalloc
72 #define obstack_chunk_free free
73
74 #define UNIFY_ALLOW_NONE 0
75 #define UNIFY_ALLOW_MORE_CV_QUAL 1
76 #define UNIFY_ALLOW_LESS_CV_QUAL 2
77 #define UNIFY_ALLOW_DERIVED 4
78
79 static int unify PROTO((tree, tree, tree, tree, int, int*));
80 static void add_pending_template PROTO((tree));
81 static int push_tinst_level PROTO((tree));
82 static tree classtype_mangled_name PROTO((tree));
83 static char *mangle_class_name_for_template PROTO((char *, tree, tree));
84 static tree tsubst_expr_values PROTO((tree, tree));
85 static int list_eq PROTO((tree, tree));
86 static tree get_class_bindings PROTO((tree, tree, tree));
87 static tree coerce_template_parms PROTO((tree, tree, tree, int, int));
88 static tree tsubst_enum PROTO((tree, tree));
89 static tree add_to_template_args PROTO((tree, tree));
90 static tree add_outermost_template_args PROTO((tree, tree));
91 static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
92 tree*));
93 static int type_unification_real PROTO((tree, tree, tree, tree,
94 int, unification_kind_t, int, int*));
95 static void note_template_header PROTO((int));
96 static tree maybe_fold_nontype_arg PROTO((tree));
97 static tree convert_nontype_argument PROTO((tree, tree));
98 static tree get_bindings_overload PROTO((tree, tree, tree));
99 static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
100 static tree build_template_parm_index PROTO((int, int, int, tree, tree));
101 static int inline_needs_template_parms PROTO((tree));
102 static void push_inline_template_parms_recursive PROTO((tree, int));
103 static tree retrieve_specialization PROTO((tree, tree));
104 static tree register_specialization PROTO((tree, tree, tree));
105 static int unregister_specialization PROTO((tree, tree));
106 static void print_candidates PROTO((tree));
107 static tree reduce_template_parm_level PROTO((tree, tree, int));
108 static tree build_template_decl PROTO((tree, tree));
109 static int mark_template_parm PROTO((tree, void *));
110 static tree tsubst_friend_function PROTO((tree, tree));
111 static tree tsubst_friend_class PROTO((tree, tree));
112 static tree get_bindings_real PROTO((tree, tree, tree, int));
113 static int template_decl_level PROTO((tree));
114 static tree maybe_get_template_decl_from_type_decl PROTO((tree));
115 static int check_cv_quals_for_unify PROTO((int, tree, tree));
116 static tree tsubst_template_arg_vector PROTO((tree, tree));
117 static tree tsubst_template_parms PROTO((tree, tree));
118 static void regenerate_decl_from_template PROTO((tree, tree));
119 static int is_member_template_class PROTO((tree));
120 static tree most_specialized PROTO((tree, tree, tree));
121 static tree most_specialized_class PROTO((tree, tree));
122 static tree most_general_template PROTO((tree));
123 static void set_mangled_name_for_template_decl PROTO((tree));
124 static int template_class_depth_real PROTO((tree, int));
125 static tree tsubst_aggr_type PROTO((tree, tree, tree, int));
126 static tree tsubst_decl PROTO((tree, tree, tree, tree));
127
128 /* We use TREE_VECs to hold template arguments. If there is only one
129 level of template arguments, then the TREE_VEC contains the
130 arguments directly. If there is more than one level of template
131 arguments, then each entry in the TREE_VEC is itself a TREE_VEC,
132 containing the template arguments for a single level. The first
133 entry in the outer TREE_VEC is the outermost level of template
134 parameters; the last is the innermost.
135
136 It is incorrect to ever form a template argument vector containing
137 only one level of arguments, but which is a TREE_VEC containing as
138 its only entry the TREE_VEC for that level. */
139
140 /* Non-zero if the template arguments is actually a vector of vectors,
141 rather than just a vector. */
142 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
143 (NODE != NULL_TREE \
144 && TREE_CODE (NODE) == TREE_VEC \
145 && TREE_VEC_LENGTH (NODE) > 0 \
146 && TREE_VEC_ELT (NODE, 0) != NULL_TREE \
147 && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
148
149 /* The depth of a template argument vector. When called directly by
150 the parser, we use a TREE_LIST rather than a TREE_VEC to represent
151 template arguments. In fact, we may even see NULL_TREE if there
152 are no template arguments. In both of those cases, there is only
153 one level of template arguments. */
154 #define TMPL_ARGS_DEPTH(NODE) \
155 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
156
157 /* The LEVELth level of the template ARGS. Note that template
158 parameter levels are indexed from 1, not from 0. */
159 #define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
160 (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
161 ? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
162
163 /* Set the LEVELth level of the template ARGS to VAL. This macro does
164 not work with single-level argument vectors. */
165 #define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
166 (TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
167
168 /* Accesses the IDXth parameter in the LEVELth level of the ARGS. */
169 #define TMPL_ARG(ARGS, LEVEL, IDX) \
170 (TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
171
172 /* Set the IDXth element in the LEVELth level of ARGS to VAL. This
173 macro does not work with single-level argument vectors. */
174 #define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
175 (TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
176
177 /* Given a single level of template arguments in NODE, return the
178 number of arguments. */
179 #define NUM_TMPL_ARGS(NODE) \
180 ((NODE) == NULL_TREE ? 0 \
181 : (TREE_CODE (NODE) == TREE_VEC \
182 ? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
183
184 /* The number of levels of template parameters given by NODE. */
185 #define TMPL_PARMS_DEPTH(NODE) \
186 (TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
187
188 /* Do any processing required when DECL (a member template declaration
189 using TEMPLATE_PARAMETERS as its innermost parameter list) is
190 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
191 it is a specialization, in which case the DECL itself is returned. */
192
193 tree
194 finish_member_template_decl (template_parameters, decl)
195 tree template_parameters;
196 tree decl;
197 {
198 finish_template_decl (template_parameters);
199
200 if (decl == NULL_TREE || decl == void_type_node)
201 return NULL_TREE;
202 else if (TREE_CODE (decl) == TREE_LIST)
203 {
204 /* Assume that the class is the only declspec. */
205 decl = TREE_VALUE (decl);
206 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
207 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
208 {
209 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
210 check_member_template (tmpl);
211 return tmpl;
212 }
213 return NULL_TREE;
214 }
215 else if (DECL_TEMPLATE_INFO (decl))
216 {
217 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
218 {
219 check_member_template (DECL_TI_TEMPLATE (decl));
220 return DECL_TI_TEMPLATE (decl);
221 }
222 else
223 return decl;
224 }
225 else
226 cp_error ("invalid member template declaration `%D'", decl);
227
228
229 return error_mark_node;
230 }
231
232 /* Returns the template nesting level of the indicated class TYPE.
233
234 For example, in:
235 template <class T>
236 struct A
237 {
238 template <class U>
239 struct B {};
240 };
241
242 A<T>::B<U> has depth two, while A<T> has depth one.
243 Both A<T>::B<int> and A<int>::B<U> have depth one, if
244 COUNT_SPECIALIZATIONS is 0 or if they are instantiations, not
245 specializations.
246
247 This function is guaranteed to return 0 if passed NULL_TREE so
248 that, for example, `template_class_depth (current_class_type)' is
249 always safe. */
250
251 int
252 template_class_depth_real (type, count_specializations)
253 tree type;
254 int count_specializations;
255 {
256 int depth;
257
258 for (depth = 0;
259 type && TREE_CODE (type) != NAMESPACE_DECL;
260 type = (TREE_CODE (type) == FUNCTION_DECL)
261 ? DECL_REAL_CONTEXT (type) : TYPE_CONTEXT (type))
262 {
263 if (TREE_CODE (type) != FUNCTION_DECL)
264 {
265 if (CLASSTYPE_TEMPLATE_INFO (type)
266 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
267 && ((count_specializations
268 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
269 || uses_template_parms (CLASSTYPE_TI_ARGS (type))))
270 ++depth;
271 }
272 else
273 {
274 if (DECL_TEMPLATE_INFO (type)
275 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
276 && ((count_specializations
277 && DECL_TEMPLATE_SPECIALIZATION (type))
278 || uses_template_parms (DECL_TI_ARGS (type))))
279 ++depth;
280 }
281 }
282
283 return depth;
284 }
285
286 /* Returns the template nesting level of the indicated class TYPE.
287 Like template_class_depth_real, but instantiations do not count in
288 the depth. */
289
290 int
291 template_class_depth (type)
292 tree type;
293 {
294 return template_class_depth_real (type, /*count_specializations=*/0);
295 }
296
297 /* Returns 1 if processing DECL as part of do_pending_inlines
298 needs us to push template parms. */
299
300 static int
301 inline_needs_template_parms (decl)
302 tree decl;
303 {
304 if (! DECL_TEMPLATE_INFO (decl))
305 return 0;
306
307 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
308 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
309 }
310
311 /* Subroutine of maybe_begin_member_template_processing.
312 Push the template parms in PARMS, starting from LEVELS steps into the
313 chain, and ending at the beginning, since template parms are listed
314 innermost first. */
315
316 static void
317 push_inline_template_parms_recursive (parmlist, levels)
318 tree parmlist;
319 int levels;
320 {
321 tree parms = TREE_VALUE (parmlist);
322 int i;
323
324 if (levels > 1)
325 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
326
327 ++processing_template_decl;
328 current_template_parms
329 = tree_cons (build_int_2 (0, processing_template_decl),
330 parms, current_template_parms);
331 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
332
333 pushlevel (0);
334 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
335 {
336 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
337 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
338
339 switch (TREE_CODE (parm))
340 {
341 case TYPE_DECL:
342 case TEMPLATE_DECL:
343 pushdecl (parm);
344 break;
345
346 case PARM_DECL:
347 {
348 /* Make a CONST_DECL as is done in process_template_parm. */
349 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
350 TREE_TYPE (parm));
351 DECL_INITIAL (decl) = DECL_INITIAL (parm);
352 pushdecl (decl);
353 }
354 break;
355
356 default:
357 my_friendly_abort (0);
358 }
359 }
360 }
361
362 /* Restore the template parameter context for a member template or
363 a friend template defined in a class definition. */
364
365 void
366 maybe_begin_member_template_processing (decl)
367 tree decl;
368 {
369 tree parms;
370 int levels;
371
372 if (! inline_needs_template_parms (decl))
373 return;
374
375 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
376
377 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
378
379 if (DECL_TEMPLATE_SPECIALIZATION (decl))
380 {
381 --levels;
382 parms = TREE_CHAIN (parms);
383 }
384
385 push_inline_template_parms_recursive (parms, levels);
386 }
387
388 /* Undo the effects of begin_member_template_processing. */
389
390 void
391 maybe_end_member_template_processing (decl)
392 tree decl;
393 {
394 if (! processing_template_decl)
395 return;
396
397 while (current_template_parms
398 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
399 {
400 --processing_template_decl;
401 current_template_parms = TREE_CHAIN (current_template_parms);
402 poplevel (0, 0, 0);
403 }
404 }
405
406 /* Returns non-zero iff T is a member template function. We must be
407 careful as in
408
409 template <class T> class C { void f(); }
410
411 Here, f is a template function, and a member, but not a member
412 template. This function does not concern itself with the origin of
413 T, only its present state. So if we have
414
415 template <class T> class C { template <class U> void f(U); }
416
417 then neither C<int>::f<char> nor C<T>::f<double> is considered
418 to be a member template. */
419
420 int
421 is_member_template (t)
422 tree t;
423 {
424 if (TREE_CODE (t) != FUNCTION_DECL
425 && !DECL_FUNCTION_TEMPLATE_P (t))
426 /* Anything that isn't a function or a template function is
427 certainly not a member template. */
428 return 0;
429
430 /* A local class can't have member templates. */
431 if (hack_decl_function_context (t))
432 return 0;
433
434 if ((DECL_FUNCTION_MEMBER_P (t)
435 && !DECL_TEMPLATE_SPECIALIZATION (t))
436 || (TREE_CODE (t) == TEMPLATE_DECL
437 && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
438 {
439 tree tmpl;
440
441 if (DECL_FUNCTION_TEMPLATE_P (t))
442 tmpl = t;
443 else if (DECL_TEMPLATE_INFO (t)
444 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
445 tmpl = DECL_TI_TEMPLATE (t);
446 else
447 tmpl = NULL_TREE;
448
449 if (tmpl
450 /* If there are more levels of template parameters than
451 there are template classes surrounding the declaration,
452 then we have a member template. */
453 && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)) >
454 template_class_depth (DECL_CLASS_CONTEXT (t))))
455 return 1;
456 }
457
458 return 0;
459 }
460
461 /* Returns non-zero iff T is a member template class. See
462 is_member_template for a description of what precisely constitutes
463 a member template. */
464
465 int
466 is_member_template_class (t)
467 tree t;
468 {
469 if (!DECL_CLASS_TEMPLATE_P (t))
470 /* Anything that isn't a class template, is certainly not a member
471 template. */
472 return 0;
473
474 if (!DECL_CLASS_SCOPE_P (t))
475 /* Anything whose context isn't a class type is surely not a
476 member template. */
477 return 0;
478
479 /* If there are more levels of template parameters than there are
480 template classes surrounding the declaration, then we have a
481 member template. */
482 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
483 template_class_depth (DECL_CONTEXT (t)));
484 }
485
486 /* Return a new template argument vector which contains all of ARGS,
487 but has as its innermost set of arguments the EXTRA_ARGS. The
488 resulting vector will be built on a temporary obstack, and so must
489 be explicitly copied to the permanent obstack, if required. */
490
491 static tree
492 add_to_template_args (args, extra_args)
493 tree args;
494 tree extra_args;
495 {
496 tree new_args;
497 int extra_depth;
498 int i;
499 int j;
500
501 extra_depth = TMPL_ARGS_DEPTH (extra_args);
502 new_args = make_temp_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
503
504 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
505 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
506
507 for (j = 1; j <= extra_depth; ++j, ++i)
508 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
509
510 return new_args;
511 }
512
513 /* Like add_to_template_args, but only the outermost ARGS are added to
514 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
515 (EXTRA_ARGS) levels are added. This function is used to combine
516 the template arguments from a partial instantiation with the
517 template arguments used to attain the full instantiation from the
518 partial instantiation. */
519
520 static tree
521 add_outermost_template_args (args, extra_args)
522 tree args;
523 tree extra_args;
524 {
525 tree new_args;
526
527 /* If there are more levels of EXTRA_ARGS than there are ARGS,
528 something very fishy is going on. */
529 my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
530 0);
531
532 /* If *all* the new arguments will be the EXTRA_ARGS, just return
533 them. */
534 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
535 return extra_args;
536
537 /* For the moment, we make ARGS look like it contains fewer levels. */
538 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
539
540 new_args = add_to_template_args (args, extra_args);
541
542 /* Now, we restore ARGS to its full dimensions. */
543 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
544
545 return new_args;
546 }
547
548 /* We've got a template header coming up; push to a new level for storing
549 the parms. */
550
551 void
552 begin_template_parm_list ()
553 {
554 /* We use a non-tag-transparent scope here, which causes pushtag to
555 put tags in this scope, rather than in the enclosing class or
556 namespace scope. This is the right thing, since we want
557 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
558 global template class, push_template_decl handles putting the
559 TEMPLATE_DECL into top-level scope. For a nested template class,
560 e.g.:
561
562 template <class T> struct S1 {
563 template <class T> struct S2 {};
564 };
565
566 pushtag contains special code to call pushdecl_with_scope on the
567 TEMPLATE_DECL for S2. */
568 pushlevel (0);
569 declare_pseudo_global_level ();
570 ++processing_template_decl;
571 ++processing_template_parmlist;
572 note_template_header (0);
573 }
574
575 /* We've just seen template <>. */
576
577 void
578 begin_specialization ()
579 {
580 note_template_header (1);
581 }
582
583 /* Called at then end of processing a declaration preceeded by
584 template<>. */
585
586 void
587 end_specialization ()
588 {
589 reset_specialization ();
590 }
591
592 /* Any template <>'s that we have seen thus far are not referring to a
593 function specialization. */
594
595 void
596 reset_specialization ()
597 {
598 processing_specialization = 0;
599 template_header_count = 0;
600 }
601
602 /* We've just seen a template header. If SPECIALIZATION is non-zero,
603 it was of the form template <>. */
604
605 static void
606 note_template_header (specialization)
607 int specialization;
608 {
609 processing_specialization = specialization;
610 template_header_count++;
611 }
612
613 /* We're beginning an explicit instantiation. */
614
615 void
616 begin_explicit_instantiation ()
617 {
618 ++processing_explicit_instantiation;
619 }
620
621
622 void
623 end_explicit_instantiation ()
624 {
625 my_friendly_assert(processing_explicit_instantiation > 0, 0);
626 --processing_explicit_instantiation;
627 }
628
629 /* The TYPE is being declared. If it is a template type, that means it
630 is a partial specialization. Do appropriate error-checking. */
631
632 void
633 maybe_process_partial_specialization (type)
634 tree type;
635 {
636 if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
637 {
638 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
639 && TYPE_SIZE (type) == NULL_TREE)
640 {
641 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
642 if (processing_template_decl)
643 push_template_decl (TYPE_MAIN_DECL (type));
644 }
645 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
646 cp_error ("specialization of `%T' after instantiation", type);
647 }
648 }
649
650 /* Retrieve the specialization (in the sense of [temp.spec] - a
651 specialization is either an instantiation or an explicit
652 specialization) of TMPL for the given template ARGS. If there is
653 no such specialization, return NULL_TREE. The ARGS are a vector of
654 arguments, or a vector of vectors of arguments, in the case of
655 templates with more than one level of parameters. */
656
657 static tree
658 retrieve_specialization (tmpl, args)
659 tree tmpl;
660 tree args;
661 {
662 tree s;
663
664 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
665
666 /* There should be as many levels of arguments as there are
667 levels of parameters. */
668 my_friendly_assert (TMPL_ARGS_DEPTH (args)
669 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
670 0);
671
672 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
673 s != NULL_TREE;
674 s = TREE_CHAIN (s))
675 if (comp_template_args (TREE_PURPOSE (s), args))
676 return TREE_VALUE (s);
677
678 return NULL_TREE;
679 }
680
681 /* Returns non-zero iff DECL is a specialization of TMPL. */
682
683 int
684 is_specialization_of (decl, tmpl)
685 tree decl;
686 tree tmpl;
687 {
688 tree t;
689
690 if (TREE_CODE (decl) == FUNCTION_DECL)
691 {
692 for (t = decl;
693 t != NULL_TREE;
694 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
695 if (t == tmpl)
696 return 1;
697 }
698 else
699 {
700 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 0);
701
702 for (t = TREE_TYPE (decl);
703 t != NULL_TREE;
704 t = CLASSTYPE_USE_TEMPLATE (t)
705 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
706 if (comptypes (TYPE_MAIN_VARIANT (t),
707 TYPE_MAIN_VARIANT (TREE_TYPE (tmpl)), 1))
708 return 1;
709 }
710
711 return 0;
712 }
713
714 /* Register the specialization SPEC as a specialization of TMPL with
715 the indicated ARGS. Returns SPEC, or an equivalent prior
716 declaration, if available. */
717
718 static tree
719 register_specialization (spec, tmpl, args)
720 tree spec;
721 tree tmpl;
722 tree args;
723 {
724 tree s;
725
726 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
727
728 if (TREE_CODE (spec) == FUNCTION_DECL
729 && uses_template_parms (DECL_TI_ARGS (spec)))
730 /* This is the FUNCTION_DECL for a partial instantiation. Don't
731 register it; we want the corresponding TEMPLATE_DECL instead.
732 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
733 the more obvious `uses_template_parms (spec)' to avoid problems
734 with default function arguments. In particular, given
735 something like this:
736
737 template <class T> void f(T t1, T t = T())
738
739 the default argument expression is not substituted for in an
740 instantiation unless and until it is actually needed. */
741 return spec;
742
743 /* There should be as many levels of arguments as there are
744 levels of parameters. */
745 my_friendly_assert (TMPL_ARGS_DEPTH (args)
746 == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)),
747 0);
748
749 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
750 s != NULL_TREE;
751 s = TREE_CHAIN (s))
752 if (comp_template_args (TREE_PURPOSE (s), args))
753 {
754 tree fn = TREE_VALUE (s);
755
756 if (DECL_TEMPLATE_SPECIALIZATION (spec))
757 {
758 if (DECL_TEMPLATE_INSTANTIATION (fn))
759 {
760 if (TREE_USED (fn)
761 || DECL_EXPLICIT_INSTANTIATION (fn))
762 {
763 cp_error ("specialization of %D after instantiation",
764 fn);
765 return spec;
766 }
767 else
768 {
769 /* This situation should occur only if the first
770 specialization is an implicit instantiation,
771 the second is an explicit specialization, and
772 the implicit instantiation has not yet been
773 used. That situation can occur if we have
774 implicitly instantiated a member function and
775 then specialized it later.
776
777 We can also wind up here if a friend
778 declaration that looked like an instantiation
779 turns out to be a specialization:
780
781 template <class T> void foo(T);
782 class S { friend void foo<>(int) };
783 template <> void foo(int);
784
785 We transform the existing DECL in place so that
786 any pointers to it become pointers to the
787 updated declaration. */
788 duplicate_decls (spec, TREE_VALUE (s));
789 return TREE_VALUE (s);
790 }
791 }
792 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
793 {
794 duplicate_decls (spec, TREE_VALUE (s));
795 return TREE_VALUE (s);
796 }
797 }
798 }
799
800 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
801 = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
802
803 return spec;
804 }
805
806 /* Unregister the specialization SPEC as a specialization of TMPL.
807 Returns nonzero if the SPEC was listed as a specialization of
808 TMPL. */
809
810 static int
811 unregister_specialization (spec, tmpl)
812 tree spec;
813 tree tmpl;
814 {
815 tree* s;
816
817 for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
818 *s != NULL_TREE;
819 s = &TREE_CHAIN (*s))
820 if (TREE_VALUE (*s) == spec)
821 {
822 *s = TREE_CHAIN (*s);
823 return 1;
824 }
825
826 return 0;
827 }
828
829 /* Print the list of candidate FNS in an error message. */
830
831 static void
832 print_candidates (fns)
833 tree fns;
834 {
835 tree fn;
836
837 char* str = "candidates are:";
838
839 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
840 {
841 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
842 str = " ";
843 }
844 }
845
846 /* Returns the template (one of the functions given by TEMPLATE_ID)
847 which can be specialized to match the indicated DECL with the
848 explicit template args given in TEMPLATE_ID. If
849 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
850 member template. The template args (those explicitly specified and
851 those deduced) are output in a newly created vector *TARGS_OUT. If
852 it is impossible to determine the result, an error message is
853 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
854 available. */
855
856 tree
857 determine_specialization (template_id, decl, targs_out,
858 need_member_template,
859 complain)
860 tree template_id;
861 tree decl;
862 tree* targs_out;
863 int need_member_template;
864 int complain;
865 {
866 tree fns, targs_in;
867 tree templates = NULL_TREE;
868 tree fn;
869 int i;
870
871 *targs_out = NULL_TREE;
872
873 if (template_id == error_mark_node)
874 return error_mark_node;
875
876 fns = TREE_OPERAND (template_id, 0);
877 targs_in = TREE_OPERAND (template_id, 1);
878
879 if (fns == error_mark_node)
880 return error_mark_node;
881
882 /* Check for baselinks. */
883 if (TREE_CODE (fns) == TREE_LIST)
884 fns = TREE_VALUE (fns);
885
886 for (; fns; fns = OVL_NEXT (fns))
887 {
888 tree tmpl;
889
890 fn = OVL_CURRENT (fns);
891 if (!need_member_template
892 && TREE_CODE (fn) == FUNCTION_DECL
893 && DECL_FUNCTION_MEMBER_P (fn)
894 && DECL_USE_TEMPLATE (fn)
895 && DECL_TI_TEMPLATE (fn))
896 /* We can get here when processing something like:
897 template <class T> class X { void f(); }
898 template <> void X<int>::f() {}
899 We're specializing a member function, but not a member
900 template. */
901 tmpl = DECL_TI_TEMPLATE (fn);
902 else if (TREE_CODE (fn) != TEMPLATE_DECL
903 || (need_member_template && !is_member_template (fn)))
904 continue;
905 else
906 tmpl = fn;
907
908 if (list_length (targs_in) > DECL_NTPARMS (tmpl))
909 continue;
910
911 if (decl == NULL_TREE)
912 {
913 tree targs = make_scratch_vec (DECL_NTPARMS (tmpl));
914
915 /* We allow incomplete unification here, because we are going to
916 check all the functions. */
917 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
918 targs,
919 NULL_TREE,
920 NULL_TREE,
921 targs_in,
922 DEDUCE_EXACT, 1);
923
924 if (i == 0)
925 /* Unification was successful. */
926 templates = scratch_tree_cons (targs, tmpl, templates);
927 }
928 else
929 templates = scratch_tree_cons (NULL_TREE, tmpl, templates);
930 }
931
932 if (decl != NULL_TREE)
933 {
934 tree tmpl = most_specialized (templates, decl, targs_in);
935 tree inner_args;
936 tree tmpl_args;
937
938 if (tmpl == error_mark_node)
939 goto ambiguous;
940 else if (tmpl == NULL_TREE)
941 goto no_match;
942
943 inner_args = get_bindings (tmpl, decl, targs_in);
944 tmpl_args = DECL_TI_ARGS (DECL_RESULT (tmpl));
945 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (tmpl_args))
946 {
947 *targs_out = copy_node (tmpl_args);
948 SET_TMPL_ARGS_LEVEL (*targs_out,
949 TMPL_ARGS_DEPTH (*targs_out),
950 inner_args);
951 }
952 else
953 *targs_out = inner_args;
954
955 return tmpl;
956 }
957
958 if (templates == NULL_TREE)
959 {
960 no_match:
961 if (complain)
962 {
963 cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
964 template_id, decl);
965 return error_mark_node;
966 }
967 return NULL_TREE;
968 }
969 else if (TREE_CHAIN (templates) != NULL_TREE)
970 {
971 ambiguous:
972 if (complain)
973 {
974 cp_error_at ("ambiguous template specialization `%D' for `%+D'",
975 template_id, decl);
976 print_candidates (templates);
977 return error_mark_node;
978 }
979 return NULL_TREE;
980 }
981
982 /* We have one, and exactly one, match. */
983 *targs_out = TREE_PURPOSE (templates);
984 return TREE_VALUE (templates);
985 }
986
987 /* Check to see if the function just declared, as indicated in
988 DECLARATOR, and in DECL, is a specialization of a function
989 template. We may also discover that the declaration is an explicit
990 instantiation at this point.
991
992 Returns DECL, or an equivalent declaration that should be used
993 instead.
994
995 FLAGS is a bitmask consisting of the following flags:
996
997 1: We are being called by finish_struct. (We are unable to
998 determine what template is specialized by an in-class
999 declaration until the class definition is complete, so
1000 finish_struct_methods calls this function again later to finish
1001 the job.)
1002 2: The function has a definition.
1003 4: The function is a friend.
1004 8: The function is known to be a specialization of a member
1005 template.
1006
1007 The TEMPLATE_COUNT is the number of references to qualifying
1008 template classes that appeared in the name of the function. For
1009 example, in
1010
1011 template <class T> struct S { void f(); };
1012 void S<int>::f();
1013
1014 the TEMPLATE_COUNT would be 1. However, explicitly specialized
1015 classes are not counted in the TEMPLATE_COUNT, so that in
1016
1017 template <class T> struct S {};
1018 template <> struct S<int> { void f(); }
1019 template <> void S<int>::f();
1020
1021 the TEMPLATE_COUNT would be 0. (Note that this declaration is
1022 illegal; there should be no template <>.)
1023
1024 If the function is a specialization, it is marked as such via
1025 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
1026 is set up correctly, and it is added to the list of specializations
1027 for that template. */
1028
1029 tree
1030 check_explicit_specialization (declarator, decl, template_count, flags)
1031 tree declarator;
1032 tree decl;
1033 int template_count;
1034 int flags;
1035 {
1036 int finish_member = flags & 1;
1037 int have_def = flags & 2;
1038 int is_friend = flags & 4;
1039 int specialization = 0;
1040 int explicit_instantiation = 0;
1041 int member_specialization = flags & 8;
1042
1043 tree ctype = DECL_CLASS_CONTEXT (decl);
1044 tree dname = DECL_NAME (decl);
1045
1046 if (!finish_member)
1047 {
1048 if (processing_specialization)
1049 {
1050 /* The last template header was of the form template <>. */
1051
1052 if (template_header_count > template_count)
1053 {
1054 /* There were more template headers than qualifying template
1055 classes. */
1056 if (template_header_count - template_count > 1)
1057 /* There shouldn't be that many template parameter
1058 lists. There can be at most one parameter list for
1059 every qualifying class, plus one for the function
1060 itself. */
1061 cp_error ("too many template parameter lists in declaration of `%D'", decl);
1062
1063 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1064 if (ctype)
1065 member_specialization = 1;
1066 else
1067 specialization = 1;
1068 }
1069 else if (template_header_count == template_count)
1070 {
1071 /* The counts are equal. So, this might be a
1072 specialization, but it is not a specialization of a
1073 member template. It might be something like
1074
1075 template <class T> struct S {
1076 void f(int i);
1077 };
1078 template <>
1079 void S<int>::f(int i) {} */
1080 specialization = 1;
1081 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1082 }
1083 else
1084 {
1085 /* This cannot be an explicit specialization. There are not
1086 enough headers for all of the qualifying classes. For
1087 example, we might have:
1088
1089 template <>
1090 void S<int>::T<char>::f();
1091
1092 But, we're missing another template <>. */
1093 cp_error("too few template parameter lists in declaration of `%D'", decl);
1094 return decl;
1095 }
1096 }
1097 else if (processing_explicit_instantiation)
1098 {
1099 if (template_header_count)
1100 cp_error ("template parameter list used in explicit instantiation");
1101
1102 if (have_def)
1103 cp_error ("definition provided for explicit instantiation");
1104
1105 explicit_instantiation = 1;
1106 }
1107 else if (ctype != NULL_TREE
1108 && !TYPE_BEING_DEFINED (ctype)
1109 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)
1110 && !is_friend)
1111 {
1112 /* This case catches outdated code that looks like this:
1113
1114 template <class T> struct S { void f(); };
1115 void S<int>::f() {} // Missing template <>
1116
1117 We disable this check when the type is being defined to
1118 avoid complaining about default compiler-generated
1119 constructors, destructors, and assignment operators.
1120 Since the type is an instantiation, not a specialization,
1121 these are the only functions that can be defined before
1122 the class is complete. */
1123
1124 /* If they said
1125 template <class T> void S<int>::f() {}
1126 that's bogus. */
1127 if (template_header_count)
1128 {
1129 cp_error ("template parameters specified in specialization");
1130 return decl;
1131 }
1132
1133 if (pedantic)
1134 cp_pedwarn
1135 ("explicit specialization not preceded by `template <>'");
1136 specialization = 1;
1137 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
1138 }
1139 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
1140 {
1141 if (is_friend)
1142 /* This could be something like:
1143
1144 template <class T> void f(T);
1145 class S { friend void f<>(int); } */
1146 specialization = 1;
1147 else
1148 {
1149 /* This case handles bogus declarations like template <>
1150 template <class T> void f<int>(); */
1151
1152 cp_error ("template-id `%D' in declaration of primary template",
1153 declarator);
1154 return decl;
1155 }
1156 }
1157 }
1158
1159 if (specialization || member_specialization)
1160 {
1161 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
1162 for (; t; t = TREE_CHAIN (t))
1163 if (TREE_PURPOSE (t))
1164 {
1165 cp_pedwarn
1166 ("default argument specified in explicit specialization");
1167 break;
1168 }
1169 }
1170
1171 if (specialization || member_specialization || explicit_instantiation)
1172 {
1173 tree gen_tmpl;
1174 tree tmpl = NULL_TREE;
1175 tree targs = NULL_TREE;
1176
1177 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
1178 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
1179 {
1180 tree fns;
1181
1182 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
1183 0);
1184 if (!ctype)
1185 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
1186 else
1187 fns = dname;
1188
1189 declarator =
1190 lookup_template_function (fns, NULL_TREE);
1191 }
1192
1193 if (declarator == error_mark_node)
1194 return error_mark_node;
1195
1196 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
1197 {
1198 /* A friend declaration. We can't do much, because we don't
1199 know what this resolves to, yet. */
1200 my_friendly_assert (is_friend != 0, 0);
1201 my_friendly_assert (!explicit_instantiation, 0);
1202 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1203 return decl;
1204 }
1205
1206 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
1207 {
1208 if (!explicit_instantiation)
1209 {
1210 /* Since finish_struct_1 has not been called yet, we
1211 can't call lookup_fnfields. We note that this
1212 template is a specialization, and proceed, letting
1213 finish_struct fix this up later. */
1214 tree ti = perm_tree_cons (NULL_TREE,
1215 TREE_OPERAND (declarator, 1),
1216 NULL_TREE);
1217 TI_PENDING_SPECIALIZATION_FLAG (ti) = 1;
1218 DECL_TEMPLATE_INFO (decl) = ti;
1219 }
1220 else
1221 /* It's not legal to write an explicit instantiation in
1222 class scope, e.g.:
1223
1224 class C { template void f(); }
1225
1226 This case is caught by the parser. However, on
1227 something like:
1228
1229 template class C { void f(); };
1230
1231 (which is illegal) we can get here. The error will be
1232 issued later. */
1233 ;
1234
1235 return decl;
1236 }
1237 else if (ctype != NULL_TREE
1238 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1239 IDENTIFIER_NODE))
1240 {
1241 /* Find the list of functions in ctype that have the same
1242 name as the declared function. */
1243 tree name = TREE_OPERAND (declarator, 0);
1244 tree fns;
1245
1246 if (name == constructor_name (ctype)
1247 || name == constructor_name_full (ctype))
1248 {
1249 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1250
1251 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1252 : !TYPE_HAS_DESTRUCTOR (ctype))
1253 {
1254 /* From [temp.expl.spec]:
1255
1256 If such an explicit specialization for the member
1257 of a class template names an implicitly-declared
1258 special member function (clause _special_), the
1259 program is ill-formed.
1260
1261 Similar language is found in [temp.explicit]. */
1262 cp_error ("specialization of implicitly-declared special member function");
1263
1264 return decl;
1265 }
1266
1267 name = is_constructor ? ctor_identifier : dtor_identifier;
1268 }
1269
1270 fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
1271
1272 if (fns == NULL_TREE)
1273 {
1274 cp_error ("no member function `%s' declared in `%T'",
1275 IDENTIFIER_POINTER (name),
1276 ctype);
1277 return decl;
1278 }
1279 else
1280 TREE_OPERAND (declarator, 0) = fns;
1281 }
1282
1283 /* Figure out what exactly is being specialized at this point.
1284 Note that for an explicit instantiation, even one for a
1285 member function, we cannot tell apriori whether the
1286 instantiation is for a member template, or just a member
1287 function of a template class. Even if a member template is
1288 being instantiated, the member template arguments may be
1289 elided if they can be deduced from the rest of the
1290 declaration. */
1291 tmpl = determine_specialization (declarator, decl,
1292 &targs,
1293 member_specialization,
1294 1);
1295
1296 if (tmpl && tmpl != error_mark_node)
1297 {
1298 gen_tmpl = most_general_template (tmpl);
1299
1300 if (explicit_instantiation)
1301 {
1302 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
1303 is done by do_decl_instantiation later. */
1304 decl = instantiate_template (tmpl, innermost_args (targs));
1305 return decl;
1306 }
1307
1308 /* If we though that the DECL was a member function, but it
1309 turns out to be specializing a static member function,
1310 make DECL a static member function as well. */
1311 if (DECL_STATIC_FUNCTION_P (tmpl)
1312 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1313 {
1314 revert_static_member_fn (&decl, 0, 0);
1315 last_function_parms = TREE_CHAIN (last_function_parms);
1316 }
1317
1318 /* Set up the DECL_TEMPLATE_INFO for DECL. */
1319 DECL_TEMPLATE_INFO (decl)
1320 = perm_tree_cons (tmpl, targs, NULL_TREE);
1321
1322 /* Mangle the function name appropriately. Note that we do
1323 not mangle specializations of non-template member
1324 functions of template classes, e.g. with
1325
1326 template <class T> struct S { void f(); }
1327
1328 and given the specialization
1329
1330 template <> void S<int>::f() {}
1331
1332 we do not mangle S<int>::f() here. That's because it's
1333 just an ordinary member function and doesn't need special
1334 treatment. We do this here so that the ordinary,
1335 non-template, name-mangling algorith will not be used
1336 later. */
1337 if ((is_member_template (tmpl) || ctype == NULL_TREE)
1338 && name_mangling_version >= 1)
1339 set_mangled_name_for_template_decl (decl);
1340
1341 if (is_friend && !have_def)
1342 /* This is not really a declaration of a specialization.
1343 It's just the name of an instantiation. But, it's not
1344 a request for an instantiation, either. */
1345 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1346
1347 /* Register this specialization so that we can find it
1348 again. */
1349 decl = register_specialization (decl, gen_tmpl, targs);
1350
1351 return decl;
1352 }
1353 }
1354
1355 return decl;
1356 }
1357
1358 /* TYPE is being declared. Verify that the use of template headers
1359 and such is reasonable. Issue error messages if not. */
1360
1361 void
1362 maybe_check_template_type (type)
1363 tree type;
1364 {
1365 if (template_header_count)
1366 {
1367 /* We are in the scope of some `template <...>' header. */
1368
1369 int context_depth
1370 = template_class_depth_real (TYPE_CONTEXT (type),
1371 /*count_specializations=*/1);
1372
1373 if (template_header_count <= context_depth)
1374 /* This is OK; the template headers are for the context. We
1375 are actually too lenient here; like
1376 check_explicit_specialization we should consider the number
1377 of template types included in the actual declaration. For
1378 example,
1379
1380 template <class T> struct S {
1381 template <class U> template <class V>
1382 struct I {};
1383 };
1384
1385 is illegal, but:
1386
1387 template <class T> struct S {
1388 template <class U> struct I;
1389 };
1390
1391 template <class T> template <class U.
1392 struct S<T>::I {};
1393
1394 is not. */
1395 ;
1396 else if (template_header_count > context_depth + 1)
1397 /* There are two many template parameter lists. */
1398 cp_error ("too many template parameter lists in declaration of `%T'", type);
1399 }
1400 }
1401
1402 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1403 parameters. These are represented in the same format used for
1404 DECL_TEMPLATE_PARMS. */
1405
1406 int comp_template_parms (parms1, parms2)
1407 tree parms1;
1408 tree parms2;
1409 {
1410 tree p1;
1411 tree p2;
1412
1413 if (parms1 == parms2)
1414 return 1;
1415
1416 for (p1 = parms1, p2 = parms2;
1417 p1 != NULL_TREE && p2 != NULL_TREE;
1418 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1419 {
1420 tree t1 = TREE_VALUE (p1);
1421 tree t2 = TREE_VALUE (p2);
1422 int i;
1423
1424 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1425 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1426
1427 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1428 return 0;
1429
1430 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1431 {
1432 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1433 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1434
1435 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1436 return 0;
1437
1438 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1439 continue;
1440 else if (!comptypes (TREE_TYPE (parm1),
1441 TREE_TYPE (parm2), 1))
1442 return 0;
1443 }
1444 }
1445
1446 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1447 /* One set of parameters has more parameters lists than the
1448 other. */
1449 return 0;
1450
1451 return 1;
1452 }
1453
1454 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1455 ORIG_LEVEL, DECL, and TYPE. */
1456
1457 static tree
1458 build_template_parm_index (index, level, orig_level, decl, type)
1459 int index;
1460 int level;
1461 int orig_level;
1462 tree decl;
1463 tree type;
1464 {
1465 tree t = make_node (TEMPLATE_PARM_INDEX);
1466 TEMPLATE_PARM_IDX (t) = index;
1467 TEMPLATE_PARM_LEVEL (t) = level;
1468 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1469 TEMPLATE_PARM_DECL (t) = decl;
1470 TREE_TYPE (t) = type;
1471
1472 return t;
1473 }
1474
1475 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1476 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1477 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1478 new one is created. */
1479
1480 static tree
1481 reduce_template_parm_level (index, type, levels)
1482 tree index;
1483 tree type;
1484 int levels;
1485 {
1486 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1487 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1488 != TEMPLATE_PARM_LEVEL (index) - levels))
1489 {
1490 tree decl
1491 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1492 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1493 type);
1494 tree t
1495 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1496 TEMPLATE_PARM_LEVEL (index) - levels,
1497 TEMPLATE_PARM_ORIG_LEVEL (index),
1498 decl, type);
1499 TEMPLATE_PARM_DESCENDANTS (index) = t;
1500
1501 /* Template template parameters need this. */
1502 DECL_TEMPLATE_PARMS (decl)
1503 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1504 }
1505
1506 return TEMPLATE_PARM_DESCENDANTS (index);
1507 }
1508
1509 /* Process information from new template parameter NEXT and append it to the
1510 LIST being built. */
1511
1512 tree
1513 process_template_parm (list, next)
1514 tree list, next;
1515 {
1516 tree parm;
1517 tree decl = 0;
1518 tree defval;
1519 int is_type, idx;
1520
1521 parm = next;
1522 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1523 defval = TREE_PURPOSE (parm);
1524 parm = TREE_VALUE (parm);
1525 is_type = TREE_PURPOSE (parm) == class_type_node;
1526
1527 if (list)
1528 {
1529 tree p = TREE_VALUE (tree_last (list));
1530
1531 if (TREE_CODE (p) == TYPE_DECL)
1532 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1533 else if (TREE_CODE (p) == TEMPLATE_DECL)
1534 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
1535 else
1536 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1537 ++idx;
1538 }
1539 else
1540 idx = 0;
1541
1542 if (!is_type)
1543 {
1544 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1545 /* is a const-param */
1546 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1547 PARM, 0, NULL_TREE);
1548 /* A template parameter is not modifiable. */
1549 TREE_READONLY (parm) = 1;
1550 if (IS_AGGR_TYPE (TREE_TYPE (parm))
1551 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
1552 && TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
1553 {
1554 cp_error ("`%#T' is not a valid type for a template constant parameter",
1555 TREE_TYPE (parm));
1556 if (DECL_NAME (parm) == NULL_TREE)
1557 error (" a template type parameter must begin with `class' or `typename'");
1558 TREE_TYPE (parm) = void_type_node;
1559 }
1560 else if (pedantic
1561 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
1562 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
1563 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1564 TREE_TYPE (parm));
1565 if (TREE_PERMANENT (parm) == 0)
1566 {
1567 parm = copy_node (parm);
1568 TREE_PERMANENT (parm) = 1;
1569 }
1570 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1571 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1572 = build_template_parm_index (idx, processing_template_decl,
1573 processing_template_decl,
1574 decl, TREE_TYPE (parm));
1575 }
1576 else
1577 {
1578 tree t;
1579 parm = TREE_VALUE (parm);
1580
1581 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1582 {
1583 t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1584 /* This is for distinguishing between real templates and template
1585 template parameters */
1586 TREE_TYPE (parm) = t;
1587 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1588 decl = parm;
1589 }
1590 else
1591 {
1592 t = make_lang_type (TEMPLATE_TYPE_PARM);
1593 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1594 decl = build_decl (TYPE_DECL, parm, t);
1595 }
1596
1597 CLASSTYPE_GOT_SEMICOLON (t) = 1;
1598 TYPE_NAME (t) = decl;
1599 TYPE_STUB_DECL (t) = decl;
1600 parm = decl;
1601 TEMPLATE_TYPE_PARM_INDEX (t)
1602 = build_template_parm_index (idx, processing_template_decl,
1603 processing_template_decl,
1604 decl, TREE_TYPE (parm));
1605 }
1606 SET_DECL_ARTIFICIAL (decl);
1607 pushdecl (decl);
1608 parm = build_tree_list (defval, parm);
1609 return chainon (list, parm);
1610 }
1611
1612 /* The end of a template parameter list has been reached. Process the
1613 tree list into a parameter vector, converting each parameter into a more
1614 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1615 as PARM_DECLs. */
1616
1617 tree
1618 end_template_parm_list (parms)
1619 tree parms;
1620 {
1621 int nparms;
1622 tree parm;
1623 tree saved_parmlist = make_tree_vec (list_length (parms));
1624
1625 current_template_parms
1626 = tree_cons (build_int_2 (0, processing_template_decl),
1627 saved_parmlist, current_template_parms);
1628
1629 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
1630 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
1631
1632 --processing_template_parmlist;
1633
1634 return saved_parmlist;
1635 }
1636
1637 /* end_template_decl is called after a template declaration is seen. */
1638
1639 void
1640 end_template_decl ()
1641 {
1642 reset_specialization ();
1643
1644 if (! processing_template_decl)
1645 return;
1646
1647 /* This matches the pushlevel in begin_template_parm_list. */
1648 poplevel (0, 0, 0);
1649
1650 --processing_template_decl;
1651 current_template_parms = TREE_CHAIN (current_template_parms);
1652 (void) get_pending_sizes (); /* Why? */
1653 }
1654
1655 /* Given a template argument vector containing the template PARMS.
1656 The innermost PARMS are given first. */
1657
1658 tree
1659 current_template_args ()
1660 {
1661 tree header;
1662 tree args;
1663 int length = TMPL_PARMS_DEPTH (current_template_parms);
1664 int l = length;
1665
1666 /* If there is only one level of template parameters, we do not
1667 create a TREE_VEC of TREE_VECs. Instead, we return a single
1668 TREE_VEC containing the arguments. */
1669 if (length > 1)
1670 args = make_tree_vec (length);
1671
1672 for (header = current_template_parms; header; header = TREE_CHAIN (header))
1673 {
1674 tree a = copy_node (TREE_VALUE (header));
1675 int i;
1676
1677 TREE_TYPE (a) = NULL_TREE;
1678 for (i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
1679 {
1680 tree t = TREE_VEC_ELT (a, i);
1681
1682 /* T will be a list if we are called from within a
1683 begin/end_template_parm_list pair, but a vector directly
1684 if within a begin/end_member_template_processing pair. */
1685 if (TREE_CODE (t) == TREE_LIST)
1686 {
1687 t = TREE_VALUE (t);
1688
1689 if (TREE_CODE (t) == TYPE_DECL
1690 || TREE_CODE (t) == TEMPLATE_DECL)
1691 t = TREE_TYPE (t);
1692 else
1693 t = DECL_INITIAL (t);
1694 TREE_VEC_ELT (a, i) = t;
1695 }
1696 }
1697
1698 if (length > 1)
1699 TREE_VEC_ELT (args, --l) = a;
1700 else
1701 args = a;
1702 }
1703
1704 return args;
1705 }
1706
1707 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1708 template PARMS. Used by push_template_decl below. */
1709
1710 static tree
1711 build_template_decl (decl, parms)
1712 tree decl;
1713 tree parms;
1714 {
1715 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
1716 DECL_TEMPLATE_PARMS (tmpl) = parms;
1717 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
1718 if (DECL_LANG_SPECIFIC (decl))
1719 {
1720 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
1721 DECL_STATIC_FUNCTION_P (tmpl) =
1722 DECL_STATIC_FUNCTION_P (decl);
1723 }
1724
1725 return tmpl;
1726 }
1727
1728 struct template_parm_data
1729 {
1730 int level;
1731 int* parms;
1732 };
1733
1734 /* Subroutine of push_template_decl used to see if each template
1735 parameter in a partial specialization is used in the explicit
1736 argument list. If T is of the LEVEL given in DATA (which is
1737 treated as a template_parm_data*), then DATA->PARMS is marked
1738 appropriately. */
1739
1740 static int
1741 mark_template_parm (t, data)
1742 tree t;
1743 void* data;
1744 {
1745 int level;
1746 int idx;
1747 struct template_parm_data* tpd = (struct template_parm_data*) data;
1748
1749 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
1750 {
1751 level = TEMPLATE_PARM_LEVEL (t);
1752 idx = TEMPLATE_PARM_IDX (t);
1753 }
1754 else
1755 {
1756 level = TEMPLATE_TYPE_LEVEL (t);
1757 idx = TEMPLATE_TYPE_IDX (t);
1758 }
1759
1760 if (level == tpd->level)
1761 tpd->parms[idx] = 1;
1762
1763 /* Return zero so that for_each_template_parm will continue the
1764 traversal of the tree; we want to mark *every* template parm. */
1765 return 0;
1766 }
1767
1768 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
1769 parameters given by current_template_args, or reuses a
1770 previously existing one, if appropriate. Returns the DECL, or an
1771 equivalent one, if it is replaced via a call to duplicate_decls.
1772
1773 If IS_FRIEND is non-zero, DECL is a friend declaration. */
1774
1775 tree
1776 push_template_decl_real (decl, is_friend)
1777 tree decl;
1778 int is_friend;
1779 {
1780 tree tmpl;
1781 tree args;
1782 tree info;
1783 tree ctx;
1784 int primary;
1785
1786 is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
1787
1788 if (is_friend)
1789 /* For a friend, we want the context of the friend function, not
1790 the type of which it is a friend. */
1791 ctx = DECL_CONTEXT (decl);
1792 else if (DECL_REAL_CONTEXT (decl)
1793 && TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
1794 /* In the case of a virtual function, we want the class in which
1795 it is defined. */
1796 ctx = DECL_REAL_CONTEXT (decl);
1797 else
1798 /* Otherwise, if we're currently definining some class, the DECL
1799 is assumed to be a member of the class. */
1800 ctx = current_class_type;
1801
1802 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
1803 ctx = NULL_TREE;
1804
1805 if (!DECL_CONTEXT (decl))
1806 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
1807
1808 /* For determining whether this is a primary template or not, we're really
1809 interested in the lexical context, not the true context. */
1810 if (is_friend)
1811 info = current_class_type;
1812 else
1813 info = ctx;
1814
1815 if (info && TREE_CODE (info) == FUNCTION_DECL)
1816 primary = 0;
1817 /* Note that template_class_depth returns 0 if given NULL_TREE, so
1818 this next line works even when we are at global scope. */
1819 else if (processing_template_decl > template_class_depth (info))
1820 primary = 1;
1821 else
1822 primary = 0;
1823
1824 if (primary)
1825 {
1826 if (current_lang_name == lang_name_c)
1827 cp_error ("template with C linkage");
1828 if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl)))
1829 cp_error ("template class without a name");
1830 if (TREE_CODE (decl) == TYPE_DECL
1831 && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
1832 cp_error ("template declaration of `%#T'", TREE_TYPE (decl));
1833 }
1834
1835 /* Partial specialization. */
1836 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
1837 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
1838 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
1839 {
1840 tree type = TREE_TYPE (decl);
1841 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
1842 tree specargs = CLASSTYPE_TI_ARGS (type);
1843
1844 /* We check that each of the template parameters given in the
1845 partial specialization is used in the argument list to the
1846 specialization. For example:
1847
1848 template <class T> struct S;
1849 template <class T> struct S<T*>;
1850
1851 The second declaration is OK because `T*' uses the template
1852 parameter T, whereas
1853
1854 template <class T> struct S<int>;
1855
1856 is no good. Even trickier is:
1857
1858 template <class T>
1859 struct S1
1860 {
1861 template <class U>
1862 struct S2;
1863 template <class U>
1864 struct S2<T>;
1865 };
1866
1867 The S2<T> declaration is actually illegal; it is a
1868 full-specialization. Of course,
1869
1870 template <class U>
1871 struct S2<T (*)(U)>;
1872
1873 or some such would have been OK. */
1874 int i;
1875 struct template_parm_data tpd;
1876 int ntparms
1877 = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (current_template_parms));
1878 int did_error_intro = 0;
1879
1880 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
1881 tpd.parms = alloca (sizeof (int) * ntparms);
1882 for (i = 0; i < ntparms; ++i)
1883 tpd.parms[i] = 0;
1884 for (i = 0; i < TREE_VEC_LENGTH (specargs); ++i)
1885 for_each_template_parm (TREE_VEC_ELT (specargs, i),
1886 &mark_template_parm,
1887 &tpd);
1888 for (i = 0; i < ntparms; ++i)
1889 if (tpd.parms[i] == 0)
1890 {
1891 /* One of the template parms was not used in the
1892 specialization. */
1893 if (!did_error_intro)
1894 {
1895 cp_error ("template parameters not used in partial specialization:");
1896 did_error_intro = 1;
1897 }
1898
1899 cp_error (" `%D'",
1900 TREE_VALUE (TREE_VEC_ELT
1901 (TREE_VALUE (current_template_parms),
1902 i)));
1903 }
1904
1905 /* [temp.class.spec]
1906
1907 The argument list of the specialization shall not be
1908 identical to the implicit argument list of the primary
1909 template. */
1910 if (comp_template_args (specargs,
1911 CLASSTYPE_TI_ARGS (TREE_TYPE (maintmpl))))
1912 cp_error ("partial specialization `%T' does not specialize any template arguments", type);
1913
1914 if (retrieve_specialization (maintmpl, specargs))
1915 /* We've already got this specialization. */
1916 return decl;
1917
1918 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
1919 = perm_tree_cons (innermost_args (specargs),
1920 INNERMOST_TEMPLATE_PARMS (current_template_parms),
1921 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
1922 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
1923 return decl;
1924 }
1925
1926 args = current_template_args ();
1927
1928 if (!ctx
1929 || TREE_CODE (ctx) == FUNCTION_DECL
1930 || TYPE_BEING_DEFINED (ctx)
1931 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
1932 {
1933 if (DECL_LANG_SPECIFIC (decl)
1934 && DECL_TEMPLATE_INFO (decl)
1935 && DECL_TI_TEMPLATE (decl))
1936 tmpl = DECL_TI_TEMPLATE (decl);
1937 else
1938 {
1939 tmpl = build_template_decl (decl, current_template_parms);
1940
1941 if (DECL_LANG_SPECIFIC (decl)
1942 && DECL_TEMPLATE_SPECIALIZATION (decl))
1943 {
1944 /* A specialization of a member template of a template
1945 class. */
1946 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1947 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
1948 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
1949 }
1950 }
1951 }
1952 else
1953 {
1954 tree t;
1955 tree a;
1956
1957 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1958 cp_error ("must specialize `%#T' before defining member `%#D'",
1959 ctx, decl);
1960 if (TREE_CODE (decl) == TYPE_DECL)
1961 {
1962 if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
1963 || TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
1964 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
1965 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
1966 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
1967 else
1968 {
1969 cp_error ("`%D' does not declare a template type", decl);
1970 return decl;
1971 }
1972 }
1973 else if (! DECL_TEMPLATE_INFO (decl))
1974 {
1975 cp_error ("template definition of non-template `%#D'", decl);
1976 return decl;
1977 }
1978 else
1979 tmpl = DECL_TI_TEMPLATE (decl);
1980
1981 if (is_member_template (tmpl) || is_member_template_class (tmpl))
1982 {
1983 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
1984 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
1985 && DECL_TEMPLATE_SPECIALIZATION (decl))
1986 {
1987 tree new_tmpl;
1988
1989 /* The declaration is a specialization of a member
1990 template, declared outside the class. Therefore, the
1991 innermost template arguments will be NULL, so we
1992 replace them with the arguments determined by the
1993 earlier call to check_explicit_specialization. */
1994 args = DECL_TI_ARGS (decl);
1995
1996 new_tmpl
1997 = build_template_decl (decl, current_template_parms);
1998 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
1999 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
2000 DECL_TI_TEMPLATE (decl) = new_tmpl;
2001 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
2002 DECL_TEMPLATE_INFO (new_tmpl) =
2003 perm_tree_cons (tmpl, args, NULL_TREE);
2004
2005 register_specialization (new_tmpl, tmpl, args);
2006 return decl;
2007 }
2008
2009 a = innermost_args (args);
2010 t = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2011 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2012 {
2013 cp_error ("got %d template parameters for `%#D'",
2014 TREE_VEC_LENGTH (a), decl);
2015 cp_error (" but %d required", TREE_VEC_LENGTH (t));
2016 }
2017 if (TMPL_ARGS_DEPTH (args) > 1)
2018 /* Get the template parameters for the enclosing template
2019 class. */
2020 a = TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args) - 1);
2021 else
2022 a = NULL_TREE;
2023 }
2024 else
2025 a = innermost_args (args);
2026
2027 t = NULL_TREE;
2028
2029 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2030 {
2031 /* When processing an inline member template of a
2032 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
2033 if (CLASSTYPE_TI_SPEC_INFO (ctx))
2034 t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
2035 }
2036 else if (CLASSTYPE_TEMPLATE_INFO (ctx))
2037 t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));
2038
2039 /* There should be template arguments if and only if there is a
2040 template class. */
2041 my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0);
2042
2043 if (t != NULL_TREE
2044 && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
2045 {
2046 cp_error ("got %d template parameters for `%#D'",
2047 TREE_VEC_LENGTH (a), decl);
2048 cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
2049 }
2050 }
2051
2052 DECL_TEMPLATE_RESULT (tmpl) = decl;
2053 TREE_TYPE (tmpl) = TREE_TYPE (decl);
2054
2055 /* Push template declarations for global functions and types. Note
2056 that we do not try to push a global template friend declared in a
2057 template class; such a thing may well depend on the template
2058 parameters of the class. */
2059 if (! ctx
2060 && !(is_friend && template_class_depth (current_class_type) > 0))
2061 tmpl = pushdecl_namespace_level (tmpl);
2062
2063 if (primary)
2064 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
2065
2066 info = perm_tree_cons (tmpl, args, NULL_TREE);
2067
2068 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
2069 {
2070 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
2071 if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
2072 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE)
2073 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
2074 }
2075 else if (! DECL_LANG_SPECIFIC (decl))
2076 cp_error ("template declaration of `%#D'", decl);
2077 else
2078 DECL_TEMPLATE_INFO (decl) = info;
2079
2080 return DECL_TEMPLATE_RESULT (tmpl);
2081 }
2082
2083 tree
2084 push_template_decl (decl)
2085 tree decl;
2086 {
2087 return push_template_decl_real (decl, 0);
2088 }
2089
2090 /* Called when a class template TYPE is redeclared with the indicated
2091 template PARMS, e.g.:
2092
2093 template <class T> struct S;
2094 template <class T> struct S {}; */
2095
2096 void
2097 redeclare_class_template (type, parms)
2098 tree type;
2099 tree parms;
2100 {
2101 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
2102 tree tmpl_parms;
2103 int i;
2104
2105 if (!PRIMARY_TEMPLATE_P (tmpl))
2106 /* The type is nested in some template class. Nothing to worry
2107 about here; there are no new template parameters for the nested
2108 type. */
2109 return;
2110
2111 parms = INNERMOST_TEMPLATE_PARMS (parms);
2112 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
2113
2114 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
2115 {
2116 cp_error_at ("previous declaration `%D'", tmpl);
2117 cp_error ("used %d template parameter%s instead of %d",
2118 TREE_VEC_LENGTH (tmpl_parms),
2119 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
2120 TREE_VEC_LENGTH (parms));
2121 return;
2122 }
2123
2124 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
2125 {
2126 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
2127 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2128 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
2129 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
2130
2131 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
2132 {
2133 cp_error_at ("template parameter `%#D'", tmpl_parm);
2134 cp_error ("redeclared here as `%#D'", parm);
2135 return;
2136 }
2137
2138 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
2139 {
2140 /* We have in [temp.param]:
2141
2142 A template-parameter may not be given default arguments
2143 by two different declarations in the same scope. */
2144 cp_error ("redefinition of default argument for `%#D'", parm);
2145 cp_error_at (" original definition appeared here", tmpl_parm);
2146 return;
2147 }
2148
2149 if (parm_default != NULL_TREE)
2150 /* Update the previous template parameters (which are the ones
2151 that will really count) with the new default value. */
2152 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
2153 }
2154 }
2155
2156 /* Attempt to convert the non-type template parameter EXPR to the
2157 indicated TYPE. If the conversion is successful, return the
2158 converted value. If the conversion is unsuccesful, return
2159 NULL_TREE if we issued an error message, or error_mark_node if we
2160 did not. We issue error messages for out-and-out bad template
2161 parameters, but not simply because the conversion failed, since we
2162 might be just trying to do argument deduction. By the time this
2163 function is called, neither TYPE nor EXPR may make use of template
2164 parameters. */
2165
2166 static tree
2167 convert_nontype_argument (type, expr)
2168 tree type;
2169 tree expr;
2170 {
2171 tree expr_type = TREE_TYPE (expr);
2172
2173 /* A template-argument for a non-type, non-template
2174 template-parameter shall be one of:
2175
2176 --an integral constant-expression of integral or enumeration
2177 type; or
2178
2179 --the name of a non-type template-parameter; or
2180
2181 --the name of an object or function with external linkage,
2182 including function templates and function template-ids but
2183 excluding non-static class members, expressed as id-expression;
2184 or
2185
2186 --the address of an object or function with external linkage,
2187 including function templates and function template-ids but
2188 excluding non-static class members, expressed as & id-expression
2189 where the & is optional if the name refers to a function or
2190 array; or
2191
2192 --a pointer to member expressed as described in _expr.unary.op_. */
2193
2194 /* An integral constant-expression can include const variables
2195 or enumerators. */
2196 if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr))
2197 expr = decl_constant_value (expr);
2198
2199 if (is_overloaded_fn (expr))
2200 /* OK for now. We'll check that it has external linkage later.
2201 Check this first since if expr_type is the unknown_type_node
2202 we would otherwise complain below. */
2203 ;
2204 else if (INTEGRAL_TYPE_P (expr_type)
2205 || TYPE_PTRMEM_P (expr_type)
2206 || TYPE_PTRMEMFUNC_P (expr_type)
2207 /* The next two are g++ extensions. */
2208 || TREE_CODE (expr_type) == REAL_TYPE
2209 || TREE_CODE (expr_type) == COMPLEX_TYPE)
2210 {
2211 if (! TREE_CONSTANT (expr))
2212 {
2213 non_constant:
2214 cp_error ("non-constant `%E' cannot be used as template argument",
2215 expr);
2216 return NULL_TREE;
2217 }
2218 }
2219 else if (TYPE_PTR_P (expr_type)
2220 /* If expr is the address of an overloaded function, we
2221 will get the unknown_type_node at this point. */
2222 || expr_type == unknown_type_node)
2223 {
2224 tree referent;
2225 tree e = expr;
2226 STRIP_NOPS (e);
2227
2228 if (TREE_CODE (e) != ADDR_EXPR)
2229 {
2230 bad_argument:
2231 cp_error ("`%E' is not a valid template argument", expr);
2232 error ("it must be %s%s with external linkage",
2233 TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
2234 ? "a pointer to " : "",
2235 TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE
2236 ? "a function" : "an object");
2237 return NULL_TREE;
2238 }
2239
2240 referent = TREE_OPERAND (e, 0);
2241 STRIP_NOPS (referent);
2242
2243 if (TREE_CODE (referent) == STRING_CST)
2244 {
2245 cp_error ("string literal %E is not a valid template argument",
2246 referent);
2247 error ("because it is the address of an object with static linkage");
2248 return NULL_TREE;
2249 }
2250
2251 if (is_overloaded_fn (referent))
2252 /* We'll check that it has external linkage later. */
2253 ;
2254 else if (TREE_CODE (referent) != VAR_DECL)
2255 goto bad_argument;
2256 else if (!TREE_PUBLIC (referent))
2257 {
2258 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
2259 return error_mark_node;
2260 }
2261 }
2262 else if (TREE_CODE (expr) == VAR_DECL)
2263 {
2264 if (!TREE_PUBLIC (expr))
2265 goto bad_argument;
2266 }
2267 else
2268 {
2269 cp_error ("object `%E' cannot be used as template argument", expr);
2270 return NULL_TREE;
2271 }
2272
2273 switch (TREE_CODE (type))
2274 {
2275 case INTEGER_TYPE:
2276 case BOOLEAN_TYPE:
2277 case ENUMERAL_TYPE:
2278 /* For a non-type template-parameter of integral or enumeration
2279 type, integral promotions (_conv.prom_) and integral
2280 conversions (_conv.integral_) are applied. */
2281 if (!INTEGRAL_TYPE_P (expr_type))
2282 return error_mark_node;
2283
2284 /* It's safe to call digest_init in this case; we know we're
2285 just converting one integral constant expression to another. */
2286 expr = digest_init (type, expr, (tree*) 0);
2287
2288 if (TREE_CODE (expr) != INTEGER_CST)
2289 /* Curiously, some TREE_CONSTNAT integral expressions do not
2290 simplify to integer constants. For example, `3 % 0',
2291 remains a TRUNC_MOD_EXPR. */
2292 goto non_constant;
2293
2294 return expr;
2295
2296 case REAL_TYPE:
2297 case COMPLEX_TYPE:
2298 /* These are g++ extensions. */
2299 if (TREE_CODE (expr_type) != TREE_CODE (type))
2300 return error_mark_node;
2301
2302 expr = digest_init (type, expr, (tree*) 0);
2303
2304 if (TREE_CODE (expr) != REAL_CST)
2305 goto non_constant;
2306
2307 return expr;
2308
2309 case POINTER_TYPE:
2310 {
2311 tree type_pointed_to = TREE_TYPE (type);
2312
2313 if (TYPE_PTRMEM_P (type))
2314 /* For a non-type template-parameter of type pointer to data
2315 member, qualification conversions (_conv.qual_) are
2316 applied. */
2317 return perform_qualification_conversions (type, expr);
2318 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
2319 {
2320 /* For a non-type template-parameter of type pointer to
2321 function, only the function-to-pointer conversion
2322 (_conv.func_) is applied. If the template-argument
2323 represents a set of overloaded functions (or a pointer to
2324 such), the matching function is selected from the set
2325 (_over.over_). */
2326 tree fns;
2327 tree fn;
2328
2329 if (TREE_CODE (expr) == ADDR_EXPR)
2330 fns = TREE_OPERAND (expr, 0);
2331 else
2332 fns = expr;
2333
2334 fn = instantiate_type (type_pointed_to, fns, 0);
2335
2336 if (fn == error_mark_node)
2337 return error_mark_node;
2338
2339 if (!TREE_PUBLIC (fn))
2340 {
2341 if (really_overloaded_fn (fns))
2342 return error_mark_node;
2343 else
2344 goto bad_argument;
2345 }
2346
2347 expr = build_unary_op (ADDR_EXPR, fn, 0);
2348
2349 my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1),
2350 0);
2351 return expr;
2352 }
2353 else
2354 {
2355 /* For a non-type template-parameter of type pointer to
2356 object, qualification conversions (_conv.qual_) and the
2357 array-to-pointer conversion (_conv.array_) are applied.
2358 [Note: In particular, neither the null pointer conversion
2359 (_conv.ptr_) nor the derived-to-base conversion
2360 (_conv.ptr_) are applied. Although 0 is a valid
2361 template-argument for a non-type template-parameter of
2362 integral type, it is not a valid template-argument for a
2363 non-type template-parameter of pointer type.]
2364
2365 The call to decay_conversion performs the
2366 array-to-pointer conversion, if appropriate. */
2367 expr = decay_conversion (expr);
2368
2369 if (expr == error_mark_node)
2370 return error_mark_node;
2371 else
2372 return perform_qualification_conversions (type, expr);
2373 }
2374 }
2375 break;
2376
2377 case REFERENCE_TYPE:
2378 {
2379 tree type_referred_to = TREE_TYPE (type);
2380
2381 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
2382 {
2383 /* For a non-type template-parameter of type reference to
2384 function, no conversions apply. If the
2385 template-argument represents a set of overloaded
2386 functions, the matching function is selected from the
2387 set (_over.over_). */
2388 tree fns = expr;
2389 tree fn;
2390
2391 fn = instantiate_type (type_referred_to, fns, 0);
2392
2393 if (!TREE_PUBLIC (fn))
2394 {
2395 if (really_overloaded_fn (fns))
2396 /* Don't issue an error here; we might get a different
2397 function if the overloading had worked out
2398 differently. */
2399 return error_mark_node;
2400 else
2401 goto bad_argument;
2402 }
2403
2404 if (fn == error_mark_node)
2405 return error_mark_node;
2406
2407 my_friendly_assert (comptypes (type, TREE_TYPE (fn), 1),
2408 0);
2409
2410 return fn;
2411 }
2412 else
2413 {
2414 /* For a non-type template-parameter of type reference to
2415 object, no conversions apply. The type referred to by the
2416 reference may be more cv-qualified than the (otherwise
2417 identical) type of the template-argument. The
2418 template-parameter is bound directly to the
2419 template-argument, which must be an lvalue. */
2420 if (!comptypes (TYPE_MAIN_VARIANT (expr_type),
2421 TYPE_MAIN_VARIANT (type), 1)
2422 || (TYPE_READONLY (expr_type) >
2423 TYPE_READONLY (type_referred_to))
2424 || (TYPE_VOLATILE (expr_type) >
2425 TYPE_VOLATILE (type_referred_to))
2426 || !real_lvalue_p (expr))
2427 return error_mark_node;
2428 else
2429 return expr;
2430 }
2431 }
2432 break;
2433
2434 case RECORD_TYPE:
2435 {
2436 tree fns;
2437 tree fn;
2438
2439 if (!TYPE_PTRMEMFUNC_P (type))
2440 /* This handles templates like
2441 template<class T, T t> void f();
2442 when T is substituted with any class. The second template
2443 parameter becomes invalid and the template candidate is
2444 rejected. */
2445 return error_mark_node;
2446
2447 /* For a non-type template-parameter of type pointer to member
2448 function, no conversions apply. If the template-argument
2449 represents a set of overloaded member functions, the
2450 matching member function is selected from the set
2451 (_over.over_). */
2452
2453 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
2454 expr_type != unknown_type_node)
2455 return error_mark_node;
2456
2457 if (TREE_CODE (expr) == CONSTRUCTOR)
2458 {
2459 /* A ptr-to-member constant. */
2460 if (!comptypes (type, expr_type, 1))
2461 return error_mark_node;
2462 else
2463 return expr;
2464 }
2465
2466 if (TREE_CODE (expr) != ADDR_EXPR)
2467 return error_mark_node;
2468
2469 fns = TREE_OPERAND (expr, 0);
2470
2471 fn = instantiate_type (TREE_TYPE (TREE_TYPE (type)),
2472 fns, 0);
2473
2474 if (fn == error_mark_node)
2475 return error_mark_node;
2476
2477 expr = build_unary_op (ADDR_EXPR, fn, 0);
2478
2479 my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1),
2480 0);
2481 return expr;
2482 }
2483 break;
2484
2485 default:
2486 /* All non-type parameters must have one of these types. */
2487 my_friendly_abort (0);
2488 break;
2489 }
2490
2491 return error_mark_node;
2492 }
2493
2494 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
2495 template template parameters. Both PARM_PARMS and ARG_PARMS are
2496 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
2497 or PARM_DECL.
2498
2499 ARG_PARMS may contain more parameters than PARM_PARMS. If this is
2500 the case, then extra parameters must have default arguments.
2501
2502 Consider the example:
2503 template <class T, class Allocator = allocator> class vector;
2504 template<template <class U> class TT> class C;
2505
2506 C<vector> is a valid instantiation. PARM_PARMS for the above code
2507 contains a TYPE_DECL (for U), ARG_PARMS contains two TYPE_DECLs (for
2508 T and Allocator) and OUTER_ARGS contains the argument that is used to
2509 substitute the TT parameter. */
2510
2511 static int
2512 coerce_template_template_parms (parm_parms, arg_parms, in_decl, outer_args)
2513 tree parm_parms, arg_parms, in_decl, outer_args;
2514 {
2515 int nparms, nargs, i;
2516 tree parm, arg;
2517
2518 my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
2519 my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
2520
2521 nparms = TREE_VEC_LENGTH (parm_parms);
2522 nargs = TREE_VEC_LENGTH (arg_parms);
2523
2524 /* The rule here is opposite of coerce_template_parms. */
2525 if (nargs < nparms
2526 || (nargs > nparms
2527 && TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
2528 return 0;
2529
2530 for (i = 0; i < nparms; ++i)
2531 {
2532 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
2533 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
2534
2535 if (arg == NULL_TREE || arg == error_mark_node
2536 || parm == NULL_TREE || parm == error_mark_node)
2537 return 0;
2538
2539 if (TREE_CODE (arg) != TREE_CODE (parm))
2540 return 0;
2541
2542 switch (TREE_CODE (parm))
2543 {
2544 case TYPE_DECL:
2545 break;
2546
2547 case TEMPLATE_DECL:
2548 /* We encounter instantiations of templates like
2549 template <template <template <class> class> class TT>
2550 class C; */
2551 sorry ("nested template template parameter");
2552 return 0;
2553
2554 case PARM_DECL:
2555 /* The tsubst call is used to handle cases such as
2556 template <class T, template <T> class TT> class D;
2557 i.e. the parameter list of TT depends on earlier parameters. */
2558 if (!comptypes (tsubst (TREE_TYPE (parm), outer_args, in_decl),
2559 TREE_TYPE (arg), 1))
2560 return 0;
2561 break;
2562
2563 default:
2564 my_friendly_abort (0);
2565 }
2566 }
2567 return 1;
2568 }
2569
2570 /* Convert all template arguments to their appropriate types, and return
2571 a vector containing the resulting values. If any error occurs, return
2572 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
2573 Some error messages are issued even if COMPLAIN is zero; for
2574 instance, if a template argument is composed from a local class.
2575
2576 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
2577 provided in ARGLIST, or else trailing parameters must have default
2578 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
2579 deduction for any unspecified trailing arguments. */
2580
2581 static tree
2582 coerce_template_parms (parms, arglist, in_decl,
2583 complain,
2584 require_all_arguments)
2585 tree parms, arglist;
2586 tree in_decl;
2587 int complain;
2588 int require_all_arguments;
2589 {
2590 int nparms, nargs, i, lost = 0;
2591 tree inner_args;
2592 tree vec;
2593
2594 inner_args = innermost_args (arglist);
2595 nargs = NUM_TMPL_ARGS (inner_args);
2596 nparms = TREE_VEC_LENGTH (parms);
2597
2598 if (nargs > nparms
2599 || (nargs < nparms
2600 && require_all_arguments
2601 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
2602 {
2603 if (complain)
2604 {
2605 cp_error ("wrong number of template arguments (%d, should be %d)",
2606 nargs, nparms);
2607
2608 if (in_decl)
2609 cp_error_at ("provided for `%D'", in_decl);
2610 }
2611
2612 return error_mark_node;
2613 }
2614
2615 /* Create in VEC the appropriate innermost arguments, and reset
2616 ARGLIST to contain the complete set of arguments. */
2617 if (inner_args && TREE_CODE (inner_args) == TREE_VEC && nargs == nparms)
2618 {
2619 /* If we already have all the arguments, we can just use them.
2620 This is an optimization over the code in the `else' branch
2621 below, and should be functionally identicial. */
2622 vec = copy_node (inner_args);
2623 arglist = add_outermost_template_args (arglist, vec);
2624 }
2625 else
2626 {
2627 /* If we don't already have all the arguments we must get what
2628 we can from default template arguments. The tricky bit is
2629 that previous arguments can influence the default values,
2630 e.g.:
2631
2632 template <class T, class U = T> void foo();
2633
2634 If we see `foo<int>' we have to come up with an {int, int}
2635 vector. */
2636
2637 tree new_arglist;
2638
2639 vec = make_tree_vec (nparms);
2640 new_arglist = add_outermost_template_args (arglist, vec);
2641
2642 for (i = 0; i < nparms; i++)
2643 {
2644 tree arg;
2645 tree parm = TREE_VEC_ELT (parms, i);
2646
2647 if (arglist && TREE_CODE (arglist) == TREE_LIST)
2648 {
2649 arg = arglist;
2650 arglist = TREE_CHAIN (arglist);
2651
2652 if (arg == error_mark_node)
2653 lost++;
2654 else
2655 arg = TREE_VALUE (arg);
2656 }
2657 else if (i < nargs)
2658 {
2659 arg = TREE_VEC_ELT (inner_args, i);
2660 if (arg == error_mark_node)
2661 lost++;
2662 }
2663 /* If no template argument was supplied, look for a default
2664 value. */
2665 else if (TREE_PURPOSE (parm) == NULL_TREE)
2666 {
2667 /* There was no default value. */
2668 my_friendly_assert (!require_all_arguments, 0);
2669 break;
2670 }
2671 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
2672 arg = tsubst (TREE_PURPOSE (parm), new_arglist, in_decl);
2673 else
2674 arg = tsubst_expr (TREE_PURPOSE (parm), new_arglist, in_decl);
2675
2676 TREE_VEC_ELT (vec, i) = arg;
2677 }
2678
2679 /* We've left ARGLIST intact up to this point, in order to allow
2680 iteration through it in the case that it was a TREE_LIST, but
2681 from here on it should contain the full set of template
2682 arguments. */
2683 arglist = new_arglist;
2684 }
2685
2686 for (i = 0; i < nparms; i++)
2687 {
2688 tree arg = TREE_VEC_ELT (vec, i);
2689 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2690 tree val = 0;
2691 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
2692
2693 if (arg == NULL_TREE)
2694 /* We're out of arguments. */
2695 {
2696 my_friendly_assert (!require_all_arguments, 0);
2697 break;
2698 }
2699
2700 if (arg == error_mark_node)
2701 {
2702 cp_error ("template argument %d is invalid", i + 1);
2703 lost++;
2704 continue;
2705 }
2706
2707 if (TREE_CODE (arg) == TREE_LIST
2708 && TREE_TYPE (arg) != NULL_TREE
2709 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
2710 {
2711 /* The template argument was the name of some
2712 member function. That's usually
2713 illegal, but static members are OK. In any
2714 case, grab the underlying fields/functions
2715 and issue an error later if required. */
2716 arg = TREE_VALUE (arg);
2717 TREE_TYPE (arg) = unknown_type_node;
2718 }
2719
2720 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
2721 requires_type = TREE_CODE (parm) == TYPE_DECL
2722 || requires_tmpl_type;
2723
2724 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2725 we also accept implicitly created TYPE_DECL as a valid argument.
2726 This is necessary to handle the case where we pass a template name
2727 to a template template parameter in a scope where we've derived from
2728 in instantiation of that template, so the template name refers to that
2729 instantiation. We really ought to handle this better. */
2730 is_tmpl_type = (TREE_CODE (arg) == TEMPLATE_DECL
2731 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
2732 || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
2733 && !CLASSTYPE_TEMPLATE_INFO (arg))
2734 || (TREE_CODE (arg) == RECORD_TYPE
2735 && CLASSTYPE_TEMPLATE_INFO (arg)
2736 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
2737 && DECL_ARTIFICIAL (TYPE_NAME (arg))
2738 && requires_tmpl_type
2739 && current_class_type
2740 /* FIXME what about nested types? */
2741 && get_binfo (arg, current_class_type, 0));
2742 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2743 arg = TYPE_STUB_DECL (arg);
2744 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
2745 arg = CLASSTYPE_TI_TEMPLATE (arg);
2746
2747 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
2748
2749 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
2750 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
2751 {
2752 cp_pedwarn ("to refer to a type member of a template parameter,");
2753 cp_pedwarn (" use `typename %E'", arg);
2754
2755 arg = make_typename_type (TREE_OPERAND (arg, 0),
2756 TREE_OPERAND (arg, 1));
2757 is_type = 1;
2758 }
2759 if (is_type != requires_type)
2760 {
2761 if (in_decl)
2762 {
2763 if (complain)
2764 {
2765 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2766 i + 1, in_decl);
2767 if (is_type)
2768 cp_error (" expected a constant of type `%T', got `%T'",
2769 TREE_TYPE (parm),
2770 (is_tmpl_type ? DECL_NAME (arg) : arg));
2771 else
2772 cp_error (" expected a type, got `%E'", arg);
2773 }
2774 }
2775 lost++;
2776 TREE_VEC_ELT (vec, i) = error_mark_node;
2777 continue;
2778 }
2779 if (is_tmpl_type ^ requires_tmpl_type)
2780 {
2781 if (in_decl && complain)
2782 {
2783 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2784 i + 1, in_decl);
2785 if (is_tmpl_type)
2786 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
2787 else
2788 cp_error (" expected a class template, got `%T'", arg);
2789 }
2790 lost++;
2791 TREE_VEC_ELT (vec, i) = error_mark_node;
2792 continue;
2793 }
2794
2795 if (is_type)
2796 {
2797 if (requires_tmpl_type)
2798 {
2799 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
2800 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
2801
2802 if (coerce_template_template_parms (parmparm, argparm,
2803 in_decl, vec))
2804 {
2805 val = arg;
2806
2807 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2808 TEMPLATE_DECL. */
2809 if (val != error_mark_node
2810 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
2811 val = TREE_TYPE (val);
2812 }
2813 else
2814 {
2815 if (in_decl && complain)
2816 {
2817 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2818 i + 1, in_decl);
2819 cp_error (" expected a template of type `%D', got `%D'", parm, arg);
2820 }
2821
2822 val = error_mark_node;
2823 }
2824 }
2825 else
2826 {
2827 val = groktypename (arg);
2828 if (! processing_template_decl)
2829 {
2830 /* [basic.link]: A name with no linkage (notably, the
2831 name of a class or enumeration declared in a local
2832 scope) shall not be used to declare an entity with
2833 linkage. This implies that names with no linkage
2834 cannot be used as template arguments. */
2835 tree t = no_linkage_check (val);
2836 if (t)
2837 {
2838 if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
2839 cp_pedwarn
2840 ("template-argument `%T' uses anonymous type", val);
2841 else
2842 cp_error
2843 ("template-argument `%T' uses local type `%T'",
2844 val, t);
2845 return error_mark_node;
2846 }
2847 }
2848 }
2849 }
2850 else
2851 {
2852 tree t = tsubst (TREE_TYPE (parm), arglist, in_decl);
2853
2854 if (processing_template_decl)
2855 arg = maybe_fold_nontype_arg (arg);
2856
2857 if (!uses_template_parms (arg) && !uses_template_parms (t))
2858 /* We used to call digest_init here. However, digest_init
2859 will report errors, which we don't want when complain
2860 is zero. More importantly, digest_init will try too
2861 hard to convert things: for example, `0' should not be
2862 converted to pointer type at this point according to
2863 the standard. Accepting this is not merely an
2864 extension, since deciding whether or not these
2865 conversions can occur is part of determining which
2866 function template to call, or whether a given epxlicit
2867 argument specification is legal. */
2868 val = convert_nontype_argument (t, arg);
2869 else
2870 val = arg;
2871
2872 if (val == NULL_TREE)
2873 val = error_mark_node;
2874 else if (val == error_mark_node && complain)
2875 cp_error ("could not convert template argument `%E' to `%T'",
2876 arg, t);
2877 }
2878
2879 if (val == error_mark_node)
2880 lost++;
2881
2882 TREE_VEC_ELT (vec, i) = val;
2883 }
2884 if (lost)
2885 return error_mark_node;
2886 return vec;
2887 }
2888
2889 /* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
2890 of template arguments. Returns 0 otherwise. */
2891
2892 int
2893 comp_template_args (oldargs, newargs)
2894 tree oldargs, newargs;
2895 {
2896 int i;
2897
2898 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
2899 return 0;
2900
2901 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
2902 {
2903 tree nt = TREE_VEC_ELT (newargs, i);
2904 tree ot = TREE_VEC_ELT (oldargs, i);
2905
2906 if (nt == ot)
2907 continue;
2908 if (TREE_CODE (nt) != TREE_CODE (ot))
2909 return 0;
2910 if (TREE_CODE (nt) == TREE_VEC)
2911 {
2912 /* For member templates */
2913 if (comp_template_args (ot, nt))
2914 continue;
2915 }
2916 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
2917 {
2918 if (comptypes (ot, nt, 1))
2919 continue;
2920 }
2921 else if (cp_tree_equal (ot, nt) > 0)
2922 continue;
2923 return 0;
2924 }
2925 return 1;
2926 }
2927
2928 /* Given class template name and parameter list, produce a user-friendly name
2929 for the instantiation. */
2930
2931 static char *
2932 mangle_class_name_for_template (name, parms, arglist)
2933 char *name;
2934 tree parms, arglist;
2935 {
2936 static struct obstack scratch_obstack;
2937 static char *scratch_firstobj;
2938 int i, nparms;
2939
2940 if (!scratch_firstobj)
2941 gcc_obstack_init (&scratch_obstack);
2942 else
2943 obstack_free (&scratch_obstack, scratch_firstobj);
2944 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
2945
2946 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2947 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2948
2949 cat (name);
2950 ccat ('<');
2951 nparms = TREE_VEC_LENGTH (parms);
2952 arglist = innermost_args (arglist);
2953 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
2954 for (i = 0; i < nparms; i++)
2955 {
2956 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2957 tree arg = TREE_VEC_ELT (arglist, i);
2958
2959 if (i)
2960 ccat (',');
2961
2962 if (TREE_CODE (parm) == TYPE_DECL)
2963 {
2964 cat (type_as_string_real (arg, 0, 1));
2965 continue;
2966 }
2967 else if (TREE_CODE (parm) == TEMPLATE_DECL)
2968 {
2969 if (TREE_CODE (arg) == TEMPLATE_DECL)
2970 {
2971 /* Already substituted with real template. Just output
2972 the template name here */
2973 tree context = DECL_CONTEXT (arg);
2974 if (context)
2975 {
2976 my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422);
2977 cat(decl_as_string (DECL_CONTEXT (arg), 0));
2978 cat("::");
2979 }
2980 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
2981 }
2982 else
2983 /* Output the parameter declaration */
2984 cat (type_as_string_real (arg, 0, 1));
2985 continue;
2986 }
2987 else
2988 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
2989
2990 if (TREE_CODE (arg) == TREE_LIST)
2991 {
2992 /* New list cell was built because old chain link was in
2993 use. */
2994 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
2995 arg = TREE_VALUE (arg);
2996 }
2997 /* No need to check arglist against parmlist here; we did that
2998 in coerce_template_parms, called from lookup_template_class. */
2999 cat (expr_as_string (arg, 0));
3000 }
3001 {
3002 char *bufp = obstack_next_free (&scratch_obstack);
3003 int offset = 0;
3004 while (bufp[offset - 1] == ' ')
3005 offset--;
3006 obstack_blank_fast (&scratch_obstack, offset);
3007
3008 /* B<C<char> >, not B<C<char>> */
3009 if (bufp[offset - 1] == '>')
3010 ccat (' ');
3011 }
3012 ccat ('>');
3013 ccat ('\0');
3014 return (char *) obstack_base (&scratch_obstack);
3015 }
3016
3017 static tree
3018 classtype_mangled_name (t)
3019 tree t;
3020 {
3021 if (CLASSTYPE_TEMPLATE_INFO (t)
3022 /* Specializations have already had their names set up in
3023 lookup_template_class. */
3024 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (t)
3025 /* For non-primary templates, the template parameters are
3026 implicit from their surrounding context. */
3027 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
3028 {
3029 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
3030 char *mangled_name = mangle_class_name_for_template
3031 (IDENTIFIER_POINTER (name),
3032 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
3033 CLASSTYPE_TI_ARGS (t));
3034 tree id = get_identifier (mangled_name);
3035 IDENTIFIER_TEMPLATE (id) = name;
3036 return id;
3037 }
3038 else
3039 return TYPE_IDENTIFIER (t);
3040 }
3041
3042 static void
3043 add_pending_template (d)
3044 tree d;
3045 {
3046 tree ti;
3047
3048 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
3049 ti = CLASSTYPE_TEMPLATE_INFO (d);
3050 else
3051 ti = DECL_TEMPLATE_INFO (d);
3052
3053 if (TI_PENDING_TEMPLATE_FLAG (ti))
3054 return;
3055
3056 *template_tail = perm_tree_cons
3057 (build_srcloc_here (), d, NULL_TREE);
3058 template_tail = &TREE_CHAIN (*template_tail);
3059 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
3060 }
3061
3062
3063 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
3064 may be either a _DECL or an overloaded function or an
3065 IDENTIFIER_NODE), and ARGLIST. */
3066
3067 tree
3068 lookup_template_function (fns, arglist)
3069 tree fns, arglist;
3070 {
3071 tree type;
3072
3073 if (fns == NULL_TREE)
3074 {
3075 cp_error ("non-template used as template");
3076 return error_mark_node;
3077 }
3078
3079 type = TREE_TYPE (fns);
3080 if (TREE_CODE (fns) == OVERLOAD || !type)
3081 type = unknown_type_node;
3082
3083 if (processing_template_decl)
3084 return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
3085 else
3086 return build (TEMPLATE_ID_EXPR, type, fns, arglist);
3087 }
3088
3089 /* Within the scope of a template class S<T>, the name S gets bound
3090 (in build_self_reference) to a TYPE_DECL for the class, not a
3091 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
3092 or one of its enclosing classes, and that type is a template,
3093 return the associated TEMPLATE_DECL. Otherwise, the original
3094 DECL is returned. */
3095
3096 tree
3097 maybe_get_template_decl_from_type_decl (decl)
3098 tree decl;
3099 {
3100 return (decl != NULL_TREE
3101 && TREE_CODE (decl) == TYPE_DECL
3102 && DECL_ARTIFICIAL (decl)
3103 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
3104 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
3105 }
3106
3107 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
3108 parameters, find the desired type.
3109
3110 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
3111 (Actually ARGLIST may be either a TREE_LIST or a TREE_VEC. It will
3112 be a TREE_LIST if called directly from the parser, and a TREE_VEC
3113 otherwise.) Since ARGLIST is build on the decl_obstack, we must
3114 copy it here to keep it from being reclaimed when the decl storage
3115 is reclaimed.
3116
3117 IN_DECL, if non-NULL, is the template declaration we are trying to
3118 instantiate.
3119
3120 If ENTERING_SCOPE is non-zero, we are about to enter the scope of
3121 the class we are looking up.
3122
3123 If the template class is really a local class in a template
3124 function, then the FUNCTION_CONTEXT is the function in which it is
3125 being instantiated. */
3126
3127 tree
3128 lookup_template_class (d1, arglist, in_decl, context, entering_scope)
3129 tree d1, arglist;
3130 tree in_decl;
3131 tree context;
3132 int entering_scope;
3133 {
3134 tree template = NULL_TREE, parmlist;
3135 char *mangled_name;
3136 tree id, t;
3137
3138 if (TREE_CODE (d1) == IDENTIFIER_NODE)
3139 {
3140 if (IDENTIFIER_LOCAL_VALUE (d1)
3141 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1)))
3142 template = IDENTIFIER_LOCAL_VALUE (d1);
3143 else
3144 {
3145 if (context)
3146 push_decl_namespace (context);
3147 if (current_class_type != NULL_TREE)
3148 template =
3149 maybe_get_template_decl_from_type_decl
3150 (IDENTIFIER_CLASS_VALUE (d1));
3151 if (template == NULL_TREE)
3152 template = lookup_name_nonclass (d1);
3153 if (context)
3154 pop_decl_namespace ();
3155 }
3156 if (template)
3157 context = DECL_CONTEXT (template);
3158 }
3159 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
3160 {
3161 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)) == NULL_TREE)
3162 return error_mark_node;
3163 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
3164 d1 = DECL_NAME (template);
3165 }
3166 else if (TREE_CODE (d1) == ENUMERAL_TYPE
3167 || (TREE_CODE_CLASS (TREE_CODE (d1)) == 't'
3168 && IS_AGGR_TYPE (d1)))
3169 {
3170 template = TYPE_TI_TEMPLATE (d1);
3171 d1 = DECL_NAME (template);
3172 }
3173 else if (TREE_CODE (d1) == TEMPLATE_DECL
3174 && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
3175 {
3176 template = d1;
3177 d1 = DECL_NAME (template);
3178 context = DECL_CONTEXT (template);
3179 }
3180 else
3181 my_friendly_abort (272);
3182
3183 /* With something like `template <class T> class X class X { ... };'
3184 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
3185 We don't want to do that, but we have to deal with the situation, so
3186 let's give them some syntax errors to chew on instead of a crash. */
3187 if (! template)
3188 return error_mark_node;
3189
3190 if (context == NULL_TREE)
3191 context = global_namespace;
3192
3193 if (TREE_CODE (template) != TEMPLATE_DECL)
3194 {
3195 cp_error ("non-template type `%T' used as a template", d1);
3196 if (in_decl)
3197 cp_error_at ("for template declaration `%D'", in_decl);
3198 return error_mark_node;
3199 }
3200
3201 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
3202 {
3203 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
3204 template arguments */
3205
3206 tree parm = copy_template_template_parm (TREE_TYPE (template));
3207 tree template2 = TYPE_STUB_DECL (parm);
3208 tree arglist2;
3209
3210 CLASSTYPE_GOT_SEMICOLON (parm) = 1;
3211 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
3212
3213 arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1);
3214 if (arglist2 == error_mark_node)
3215 return error_mark_node;
3216
3217 arglist2 = copy_to_permanent (arglist2);
3218 CLASSTYPE_TEMPLATE_INFO (parm)
3219 = perm_tree_cons (template2, arglist2, NULL_TREE);
3220 TYPE_SIZE (parm) = 0;
3221 return parm;
3222 }
3223 else
3224 {
3225 tree template_type = TREE_TYPE (template);
3226 tree type_decl;
3227 tree found = NULL_TREE;
3228 int arg_depth;
3229 int parm_depth;
3230
3231 template = most_general_template (template);
3232 parmlist = DECL_TEMPLATE_PARMS (template);
3233 parm_depth = TMPL_PARMS_DEPTH (parmlist);
3234 arg_depth = TMPL_ARGS_DEPTH (arglist);
3235
3236 if (arg_depth == 1 && parm_depth > 1)
3237 {
3238 /* We've been given an incomplete set of template arguments.
3239 For example, given:
3240
3241 template <class T> struct S1 {
3242 template <class U> struct S2 {};
3243 template <class U> struct S2<U*> {};
3244 };
3245
3246 we will be called with an ARGLIST of `U*', but the
3247 TEMPLATE will be `template <class T> template
3248 <class U> struct S1<T>::S2'. We must fill in the missing
3249 arguments. */
3250 my_friendly_assert (context != NULL_TREE, 0);
3251 while (!IS_AGGR_TYPE_CODE (TREE_CODE (context))
3252 && context != global_namespace)
3253 context = DECL_REAL_CONTEXT (context);
3254
3255 if (context == global_namespace)
3256 /* This is bad. We cannot get enough arguments, even from
3257 the surrounding context, to resolve this class. One
3258 case where this might happen is (illegal) code like:
3259
3260 template <class U>
3261 template <class T>
3262 struct S {
3263 A(const A<T>& a) {}
3264 };
3265
3266 We should catch this error sooner (at the opening curly
3267 for `S', but it is better to be safe than sorry here. */
3268 {
3269 cp_error ("invalid use of `%D'", template);
3270 return error_mark_node;
3271 }
3272
3273 arglist = add_to_template_args (TYPE_TI_ARGS (context),
3274 arglist);
3275 arg_depth = TMPL_ARGS_DEPTH (arglist);
3276 }
3277
3278 my_friendly_assert (parm_depth == arg_depth, 0);
3279
3280 /* Calculate the BOUND_ARGS. These will be the args that are
3281 actually tsubst'd into the definition to create the
3282 instantiation. */
3283 if (parm_depth > 1)
3284 {
3285 /* We have multiple levels of arguments to coerce, at once. */
3286 int i;
3287 int saved_depth = TMPL_ARGS_DEPTH (arglist);
3288
3289 tree bound_args = make_tree_vec (parm_depth);
3290
3291 for (i = saved_depth,
3292 t = DECL_TEMPLATE_PARMS (template);
3293 i > 0 && t != NULL_TREE;
3294 --i, t = TREE_CHAIN (t))
3295 {
3296 tree a = coerce_template_parms (TREE_VALUE (t),
3297 arglist, template, 1, 1);
3298 SET_TMPL_ARGS_LEVEL (bound_args, i, a);
3299
3300 /* We temporarily reduce the length of the ARGLIST so
3301 that coerce_template_parms will see only the arguments
3302 corresponding to the template parameters it is
3303 examining. */
3304 TREE_VEC_LENGTH (arglist)--;
3305 }
3306
3307 /* Restore the ARGLIST to its full size. */
3308 TREE_VEC_LENGTH (arglist) = saved_depth;
3309
3310 arglist = bound_args;
3311 }
3312 else
3313 arglist
3314 = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
3315 innermost_args (arglist),
3316 template, 1, 1);
3317
3318 if (arglist == error_mark_node)
3319 /* We were unable to bind the arguments. */
3320 return error_mark_node;
3321
3322 /* In the scope of a template class, explicit references to the
3323 template class refer to the type of the template, not any
3324 instantiation of it. For example, in:
3325
3326 template <class T> class C { void f(C<T>); }
3327
3328 the `C<T>' is just the same as `C'. Outside of the
3329 class, however, such a reference is an instantiation. */
3330 if (comp_template_args (TYPE_TI_ARGS (template_type),
3331 arglist))
3332 {
3333 found = template_type;
3334
3335 if (!entering_scope && PRIMARY_TEMPLATE_P (template))
3336 {
3337 tree ctx;
3338
3339 /* Note that we use DECL_CONTEXT, rather than
3340 CP_DECL_CONTEXT, so that the termination test is
3341 always just `ctx'. We're not interested in namepace
3342 scopes. */
3343 for (ctx = current_class_type;
3344 ctx;
3345 ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
3346 ? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
3347 if (comptypes (ctx, template_type, 1))
3348 break;
3349
3350 if (!ctx)
3351 /* We're not in the scope of the class, so the
3352 TEMPLATE_TYPE is not the type we want after
3353 all. */
3354 found = NULL_TREE;
3355 }
3356 }
3357
3358 if (!found)
3359 {
3360 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
3361 found; found = TREE_CHAIN (found))
3362 if (comp_template_args (TREE_PURPOSE (found), arglist))
3363 break;
3364
3365 if (found)
3366 found = TREE_VALUE (found);
3367 }
3368
3369 if (found)
3370 {
3371 if (can_free (&permanent_obstack, arglist))
3372 obstack_free (&permanent_obstack, arglist);
3373 return found;
3374 }
3375
3376 /* Since we didn't find the type, we'll have to create it.
3377 Since we'll be saving this type on the
3378 DECL_TEMPLATE_INSTANTIATIONS list, it must be permanent. */
3379 push_obstacks (&permanent_obstack, &permanent_obstack);
3380
3381 /* Create the type. */
3382 if (TREE_CODE (template_type) == ENUMERAL_TYPE)
3383 {
3384 if (!uses_template_parms (arglist))
3385 t = tsubst_enum (template_type, arglist);
3386 else
3387 /* We don't want to call tsubst_enum for this type, since
3388 the values for the enumeration constants may involve
3389 template parameters. And, no one should be interested
3390 in the enumeration constants for such a type. */
3391 t = make_node (ENUMERAL_TYPE);
3392 }
3393 else
3394 {
3395 t = make_lang_type (TREE_CODE (template_type));
3396 CLASSTYPE_DECLARED_CLASS (t)
3397 = CLASSTYPE_DECLARED_CLASS (template_type);
3398 CLASSTYPE_GOT_SEMICOLON (t) = 1;
3399 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
3400 TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
3401 }
3402
3403 /* If we called tsubst_enum above, this information will already
3404 be set up. */
3405 if (!TYPE_NAME (t))
3406 {
3407 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3408
3409 /* Create a stub TYPE_DECL for it. */
3410 type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t);
3411 SET_DECL_ARTIFICIAL (type_decl);
3412 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
3413 DECL_SOURCE_FILE (type_decl)
3414 = DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
3415 DECL_SOURCE_LINE (type_decl)
3416 = DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
3417 TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl;
3418 }
3419 else
3420 type_decl = TYPE_NAME (t);
3421
3422 /* We're done with the permanent obstack, now. */
3423 pop_obstacks ();
3424
3425 /* Set up the template information. */
3426 arglist = copy_to_permanent (arglist);
3427 SET_TYPE_TEMPLATE_INFO (t,
3428 perm_tree_cons (template, arglist, NULL_TREE));
3429 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
3430 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
3431
3432 /* Reset the name of the type, now that CLASSTYPE_TEMPLATE_INFO
3433 is set up. */
3434 if (TREE_CODE (t) != ENUMERAL_TYPE)
3435 DECL_NAME (type_decl) = classtype_mangled_name (t);
3436 DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
3437 if (! uses_template_parms (arglist))
3438 {
3439 DECL_ASSEMBLER_NAME (type_decl)
3440 = get_identifier (build_overload_name (t, 1, 1));
3441
3442 /* For backwards compatibility; code that uses
3443 -fexternal-templates expects looking up a template to
3444 instantiate it. I think DDD still relies on this.
3445 (jason 8/20/1998) */
3446 if (TREE_CODE (t) != ENUMERAL_TYPE
3447 && flag_external_templates
3448 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
3449 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
3450 add_pending_template (t);
3451 }
3452 else
3453 /* If the type makes use of template parameters, the
3454 code that generates debugging information will crash. */
3455 DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
3456
3457 return t;
3458 }
3459 }
3460 \f
3461 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
3462 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
3463 If FN returns non-zero, the iteration is terminated, and
3464 for_each_template_parm returns 1. Otherwise, the iteration
3465 continues. If FN never returns a non-zero value, the value
3466 returned by for_each_template_parm is 0. If FN is NULL, it is
3467 considered to be the function which always returns 1. */
3468
3469 int
3470 for_each_template_parm (t, fn, data)
3471 tree t;
3472 tree_fn_t fn;
3473 void* data;
3474 {
3475 if (!t)
3476 return 0;
3477
3478 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
3479 && for_each_template_parm (TYPE_CONTEXT (t), fn, data))
3480 return 1;
3481
3482 switch (TREE_CODE (t))
3483 {
3484 case INDIRECT_REF:
3485 case COMPONENT_REF:
3486 /* We assume that the object must be instantiated in order to build
3487 the COMPONENT_REF, so we test only whether the type of the
3488 COMPONENT_REF uses template parms. */
3489 return for_each_template_parm (TREE_TYPE (t), fn, data);
3490
3491 case IDENTIFIER_NODE:
3492 if (!IDENTIFIER_TEMPLATE (t))
3493 return 0;
3494 my_friendly_abort (42);
3495
3496 /* aggregates of tree nodes */
3497 case TREE_VEC:
3498 {
3499 int i = TREE_VEC_LENGTH (t);
3500 while (i--)
3501 if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
3502 return 1;
3503 return 0;
3504 }
3505 case TREE_LIST:
3506 if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
3507 || for_each_template_parm (TREE_VALUE (t), fn, data))
3508 return 1;
3509 return for_each_template_parm (TREE_CHAIN (t), fn, data);
3510
3511 case OVERLOAD:
3512 if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
3513 return 1;
3514 return for_each_template_parm (OVL_CHAIN (t), fn, data);
3515
3516 /* constructed type nodes */
3517 case POINTER_TYPE:
3518 case REFERENCE_TYPE:
3519 return for_each_template_parm (TREE_TYPE (t), fn, data);
3520
3521 case RECORD_TYPE:
3522 if (TYPE_PTRMEMFUNC_FLAG (t))
3523 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
3524 fn, data);
3525 /* Fall through. */
3526
3527 case UNION_TYPE:
3528 case ENUMERAL_TYPE:
3529 if (! TYPE_TEMPLATE_INFO (t))
3530 return 0;
3531 return for_each_template_parm (TREE_VALUE
3532 (TYPE_TEMPLATE_INFO (t)),
3533 fn, data);
3534 case FUNCTION_TYPE:
3535 if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data))
3536 return 1;
3537 return for_each_template_parm (TREE_TYPE (t), fn, data);
3538 case ARRAY_TYPE:
3539 if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
3540 return 1;
3541 return for_each_template_parm (TREE_TYPE (t), fn, data);
3542 case OFFSET_TYPE:
3543 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
3544 return 1;
3545 return for_each_template_parm (TREE_TYPE (t), fn, data);
3546 case METHOD_TYPE:
3547 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
3548 return 1;
3549 if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data))
3550 return 1;
3551 return for_each_template_parm (TREE_TYPE (t), fn, data);
3552
3553 /* decl nodes */
3554 case TYPE_DECL:
3555 return for_each_template_parm (TREE_TYPE (t), fn, data);
3556
3557 case TEMPLATE_DECL:
3558 /* A template template parameter is encountered */
3559 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3560 return for_each_template_parm (TREE_TYPE (t), fn, data);
3561 /* Already substituted template template parameter */
3562 return 0;
3563
3564 case CONST_DECL:
3565 if (for_each_template_parm (DECL_INITIAL (t), fn, data))
3566 return 1;
3567 goto check_type_and_context;
3568
3569 case FUNCTION_DECL:
3570 case VAR_DECL:
3571 /* ??? What about FIELD_DECLs? */
3572 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
3573 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
3574 return 1;
3575 /* fall through */
3576 case PARM_DECL:
3577 check_type_and_context:
3578 if (for_each_template_parm (TREE_TYPE (t), fn, data))
3579 return 1;
3580 if (DECL_CONTEXT (t)
3581 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
3582 return 1;
3583 return 0;
3584
3585 case CALL_EXPR:
3586 return for_each_template_parm (TREE_TYPE (t), fn, data);
3587 case ADDR_EXPR:
3588 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3589
3590 /* template parm nodes */
3591 case TEMPLATE_TEMPLATE_PARM:
3592 /* Record template parameters such as `T' inside `TT<T>'. */
3593 if (CLASSTYPE_TEMPLATE_INFO (t)
3594 && for_each_template_parm (CLASSTYPE_TI_ARGS (t), fn, data))
3595 return 1;
3596 case TEMPLATE_TYPE_PARM:
3597 case TEMPLATE_PARM_INDEX:
3598 if (fn)
3599 return (*fn)(t, data);
3600 else
3601 return 1;
3602
3603 /* simple type nodes */
3604 case INTEGER_TYPE:
3605 if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
3606 return 1;
3607 return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
3608
3609 case REAL_TYPE:
3610 case COMPLEX_TYPE:
3611 case VOID_TYPE:
3612 case BOOLEAN_TYPE:
3613 case NAMESPACE_DECL:
3614 return 0;
3615
3616 /* constants */
3617 case INTEGER_CST:
3618 case REAL_CST:
3619 case STRING_CST:
3620 return 0;
3621
3622 case ERROR_MARK:
3623 /* Non-error_mark_node ERROR_MARKs are bad things. */
3624 my_friendly_assert (t == error_mark_node, 274);
3625 /* NOTREACHED */
3626 return 0;
3627
3628 case LOOKUP_EXPR:
3629 case TYPENAME_TYPE:
3630 return 1;
3631
3632 case SCOPE_REF:
3633 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3634
3635 case CONSTRUCTOR:
3636 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3637 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3638 (TREE_TYPE (t)), fn, data);
3639 return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
3640
3641 case MODOP_EXPR:
3642 case CAST_EXPR:
3643 case REINTERPRET_CAST_EXPR:
3644 case CONST_CAST_EXPR:
3645 case STATIC_CAST_EXPR:
3646 case DYNAMIC_CAST_EXPR:
3647 case ARROW_EXPR:
3648 case DOTSTAR_EXPR:
3649 case TYPEID_EXPR:
3650 return 1;
3651
3652 case SIZEOF_EXPR:
3653 case ALIGNOF_EXPR:
3654 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3655
3656 default:
3657 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3658 {
3659 case '1':
3660 case '2':
3661 case 'e':
3662 case '<':
3663 {
3664 int i;
3665 for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;)
3666 if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
3667 return 1;
3668 return 0;
3669 }
3670 default:
3671 break;
3672 }
3673 sorry ("testing %s for template parms",
3674 tree_code_name [(int) TREE_CODE (t)]);
3675 my_friendly_abort (82);
3676 /* NOTREACHED */
3677 return 0;
3678 }
3679 }
3680
3681 int
3682 uses_template_parms (t)
3683 tree t;
3684 {
3685 return for_each_template_parm (t, 0, 0);
3686 }
3687
3688 static struct tinst_level *current_tinst_level;
3689 static struct tinst_level *free_tinst_level;
3690 static int tinst_depth;
3691 extern int max_tinst_depth;
3692 #ifdef GATHER_STATISTICS
3693 int depth_reached;
3694 #endif
3695 int tinst_level_tick;
3696 int last_template_error_tick;
3697
3698 /* Print out all the template instantiations that we are currently
3699 working on. If ERR, we are being called from cp_thing, so do
3700 the right thing for an error message. */
3701
3702 static void
3703 print_template_context (err)
3704 int err;
3705 {
3706 struct tinst_level *p = current_tinst_level;
3707 int line = lineno;
3708 char *file = input_filename;
3709
3710 if (err && p)
3711 {
3712 if (current_function_decl != p->decl
3713 && current_function_decl != NULL_TREE)
3714 /* We can get here during the processing of some synthesized
3715 method. Then, p->decl will be the function that's causing
3716 the synthesis. */
3717 ;
3718 else
3719 {
3720 if (current_function_decl == p->decl)
3721 /* Avoid redundancy with the the "In function" line. */;
3722 else
3723 fprintf (stderr, "%s: In instantiation of `%s':\n",
3724 file, decl_as_string (p->decl, 0));
3725
3726 line = p->line;
3727 file = p->file;
3728 p = p->next;
3729 }
3730 }
3731
3732 for (; p; p = p->next)
3733 {
3734 fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line,
3735 decl_as_string (p->decl, 0));
3736 line = p->line;
3737 file = p->file;
3738 }
3739 fprintf (stderr, "%s:%d: instantiated from here\n", file, line);
3740 }
3741
3742 /* Called from cp_thing to print the template context for an error. */
3743
3744 void
3745 maybe_print_template_context ()
3746 {
3747 if (last_template_error_tick == tinst_level_tick
3748 || current_tinst_level == 0)
3749 return;
3750
3751 last_template_error_tick = tinst_level_tick;
3752 print_template_context (1);
3753 }
3754
3755 static int
3756 push_tinst_level (d)
3757 tree d;
3758 {
3759 struct tinst_level *new;
3760
3761 if (tinst_depth >= max_tinst_depth)
3762 {
3763 /* If the instantiation in question still has unbound template parms,
3764 we don't really care if we can't instantiate it, so just return.
3765 This happens with base instantiation for implicit `typename'. */
3766 if (uses_template_parms (d))
3767 return 0;
3768
3769 last_template_error_tick = tinst_level_tick;
3770 error ("template instantiation depth exceeds maximum of %d",
3771 max_tinst_depth);
3772 error (" (use -ftemplate-depth-NN to increase the maximum)");
3773 cp_error (" instantiating `%D'", d);
3774
3775 print_template_context (0);
3776
3777 return 0;
3778 }
3779
3780 if (free_tinst_level)
3781 {
3782 new = free_tinst_level;
3783 free_tinst_level = new->next;
3784 }
3785 else
3786 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
3787
3788 new->decl = d;
3789 new->line = lineno;
3790 new->file = input_filename;
3791 new->next = current_tinst_level;
3792 current_tinst_level = new;
3793
3794 ++tinst_depth;
3795 #ifdef GATHER_STATISTICS
3796 if (tinst_depth > depth_reached)
3797 depth_reached = tinst_depth;
3798 #endif
3799
3800 ++tinst_level_tick;
3801 return 1;
3802 }
3803
3804 void
3805 pop_tinst_level ()
3806 {
3807 struct tinst_level *old = current_tinst_level;
3808
3809 /* Restore the filename and line number stashed away when we started
3810 this instantiation. */
3811 lineno = old->line;
3812 input_filename = old->file;
3813
3814 current_tinst_level = old->next;
3815 old->next = free_tinst_level;
3816 free_tinst_level = old;
3817 --tinst_depth;
3818 ++tinst_level_tick;
3819 }
3820
3821 struct tinst_level *
3822 tinst_for_decl ()
3823 {
3824 struct tinst_level *p = current_tinst_level;
3825
3826 if (p)
3827 for (; p->next ; p = p->next )
3828 ;
3829 return p;
3830 }
3831
3832 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
3833 vector of template arguments, as for tsubst.
3834
3835 Returns an appropriate tsbust'd friend declaration. */
3836
3837 static tree
3838 tsubst_friend_function (decl, args)
3839 tree decl;
3840 tree args;
3841 {
3842 tree new_friend;
3843 int line = lineno;
3844 char *file = input_filename;
3845
3846 lineno = DECL_SOURCE_LINE (decl);
3847 input_filename = DECL_SOURCE_FILE (decl);
3848
3849 if (TREE_CODE (decl) == FUNCTION_DECL
3850 && DECL_TEMPLATE_INSTANTIATION (decl)
3851 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
3852 /* This was a friend declared with an explicit template
3853 argument list, e.g.:
3854
3855 friend void f<>(T);
3856
3857 to indicate that f was a template instantiation, not a new
3858 function declaration. Now, we have to figure out what
3859 instantiation of what template. */
3860 {
3861 tree template_id;
3862 tree new_args;
3863 tree tmpl;
3864
3865 template_id
3866 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
3867 args, NULL_TREE),
3868 tsubst (DECL_TI_ARGS (decl),
3869 args, NULL_TREE));
3870 /* FIXME: The decl we create via the next tsubst could be
3871 created on a temporary obstack. */
3872 new_friend = tsubst (decl, args, NULL_TREE);
3873 tmpl = determine_specialization (template_id, new_friend,
3874 &new_args,
3875 /*need_member_template=*/0,
3876 /*complain=*/1);
3877 new_friend = instantiate_template (tmpl, new_args);
3878 goto done;
3879 }
3880
3881 new_friend = tsubst (decl, args, NULL_TREE);
3882
3883 /* The NEW_FRIEND will look like an instantiation, to the
3884 compiler, but is not an instantiation from the point of view of
3885 the language. For example, we might have had:
3886
3887 template <class T> struct S {
3888 template <class U> friend void f(T, U);
3889 };
3890
3891 Then, in S<int>, template <class U> void f(int, U) is not an
3892 instantiation of anything. */
3893 DECL_USE_TEMPLATE (new_friend) = 0;
3894 if (TREE_CODE (decl) == TEMPLATE_DECL)
3895 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
3896
3897 /* The mangled name for the NEW_FRIEND is incorrect. The call to
3898 tsubst will have resulted in a call to
3899 set_mangled_name_for_template_decl. But, the function is not a
3900 template instantiation and should not be mangled like one.
3901 Therefore, we remangle the function name. We don't have to do
3902 this if the NEW_FRIEND is a template since
3903 set_mangled_name_for_template_decl doesn't do anything if the
3904 function declaration still uses template arguments. */
3905 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
3906 {
3907 set_mangled_name_for_decl (new_friend);
3908 DECL_RTL (new_friend) = 0;
3909 make_decl_rtl (new_friend, NULL_PTR, 1);
3910 }
3911
3912 if (DECL_NAMESPACE_SCOPE_P (new_friend))
3913 {
3914 tree old_decl;
3915 tree new_friend_args;
3916
3917 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
3918 /* This declaration is a `primary' template. */
3919 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
3920
3921 /* We must save the DECL_TI_ARGS for NEW_FRIEND here because
3922 pushdecl may call duplicate_decls which will free NEW_FRIEND
3923 if possible. */
3924 new_friend_args = DECL_TI_ARGS (new_friend);
3925 old_decl = pushdecl_namespace_level (new_friend);
3926
3927 if (old_decl != new_friend)
3928 {
3929 /* This new friend declaration matched an existing
3930 declaration. For example, given:
3931
3932 template <class T> void f(T);
3933 template <class U> class C {
3934 template <class T> friend void f(T) {}
3935 };
3936
3937 the friend declaration actually provides the definition
3938 of `f', once C has been instantiated for some type. So,
3939 old_decl will be the out-of-class template declaration,
3940 while new_friend is the in-class definition.
3941
3942 But, if `f' was called before this point, the
3943 instantiation of `f' will have DECL_TI_ARGS corresponding
3944 to `T' but not to `U', references to which might appear
3945 in the definition of `f'. Previously, the most general
3946 template for an instantiation of `f' was the out-of-class
3947 version; now it is the in-class version. Therefore, we
3948 run through all specialization of `f', adding to their
3949 DECL_TI_ARGS appropriately. In particular, they need a
3950 new set of outer arguments, corresponding to the
3951 arguments for this class instantiation.
3952
3953 The same situation can arise with something like this:
3954
3955 friend void f(int);
3956 template <class T> class C {
3957 friend void f(T) {}
3958 };
3959
3960 when `C<int>' is instantiated. Now, `f(int)' is defined
3961 in the class. */
3962
3963 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
3964 /* duplicate_decls will take care of this case. */
3965 ;
3966 else
3967 {
3968 tree t;
3969
3970 for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
3971 t != NULL_TREE;
3972 t = TREE_CHAIN (t))
3973 {
3974 tree spec = TREE_VALUE (t);
3975
3976 DECL_TI_ARGS (spec)
3977 = add_outermost_template_args (new_friend_args,
3978 DECL_TI_ARGS (spec));
3979 DECL_TI_ARGS (spec)
3980 = copy_to_permanent (DECL_TI_ARGS (spec));
3981 }
3982
3983 /* Now, since specializations are always supposed to
3984 hang off of the most general template, we must move
3985 them. */
3986 t = most_general_template (old_decl);
3987 if (t != old_decl)
3988 {
3989 DECL_TEMPLATE_SPECIALIZATIONS (t)
3990 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
3991 DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
3992 DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
3993 }
3994 }
3995
3996 /* The information from NEW_FRIEND has been merged into OLD_DECL
3997 by duplicate_decls. */
3998 new_friend = old_decl;
3999 }
4000 }
4001 else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
4002 {
4003 /* Check to see that the declaration is really present, and,
4004 possibly obtain an improved declaration. */
4005 tree fn = check_classfn (DECL_CONTEXT (new_friend),
4006 new_friend);
4007
4008 if (fn)
4009 new_friend = fn;
4010 }
4011
4012 done:
4013 lineno = line;
4014 input_filename = file;
4015 return new_friend;
4016 }
4017
4018 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
4019 template arguments, as for tsubst.
4020
4021 Returns an appropriate tsbust'd friend type. */
4022
4023 static tree
4024 tsubst_friend_class (friend_tmpl, args)
4025 tree friend_tmpl;
4026 tree args;
4027 {
4028 tree friend_type;
4029 tree tmpl = lookup_name (DECL_NAME (friend_tmpl), 1);
4030
4031 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
4032
4033 if (tmpl != NULL_TREE && DECL_CLASS_TEMPLATE_P (tmpl))
4034 {
4035 /* The friend template has already been declared. Just
4036 check to see that the declarations match, and install any new
4037 default parameters. We must tsubst the default parameters,
4038 of course. We only need the innermost template parameters
4039 because that is all that redeclare_class_template will look
4040 at. */
4041 tree parms
4042 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
4043 args);
4044 redeclare_class_template (TREE_TYPE (tmpl), parms);
4045 friend_type = TREE_TYPE (tmpl);
4046 }
4047 else
4048 {
4049 /* The friend template has not already been declared. In this
4050 case, the instantiation of the template class will cause the
4051 injection of this template into the global scope. */
4052 tmpl = tsubst (friend_tmpl, args, NULL_TREE);
4053
4054 /* The new TMPL is not an instantiation of anything, so we
4055 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
4056 the new type because that is supposed to be the corresponding
4057 template decl, i.e., TMPL. */
4058 DECL_USE_TEMPLATE (tmpl) = 0;
4059 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
4060 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
4061
4062 /* Inject this template into the global scope. */
4063 friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
4064 }
4065
4066 return friend_type;
4067 }
4068
4069 tree
4070 instantiate_class_template (type)
4071 tree type;
4072 {
4073 tree template, args, pattern, t, *field_chain;
4074 tree typedecl;
4075
4076 if (type == error_mark_node)
4077 return error_mark_node;
4078
4079 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
4080 return type;
4081
4082 template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
4083 args = CLASSTYPE_TI_ARGS (type);
4084 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
4085 t = most_specialized_class (template, args);
4086
4087 if (t == error_mark_node)
4088 {
4089 char *str = "candidates are:";
4090 cp_error ("ambiguous class template instantiation for `%#T'", type);
4091 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
4092 {
4093 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4094 args))
4095 {
4096 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
4097 str = " ";
4098 }
4099 }
4100 TYPE_BEING_DEFINED (type) = 1;
4101 return error_mark_node;
4102 }
4103 else if (t)
4104 pattern = TREE_TYPE (t);
4105 else
4106 pattern = TREE_TYPE (template);
4107
4108 if (TYPE_SIZE (pattern) == NULL_TREE)
4109 return type;
4110
4111 if (t)
4112 {
4113 /* This TYPE is actually a instantiation of of a partial
4114 specialization. We replace the innermost set of ARGS with
4115 the arguments appropriate for substitution. For example,
4116 given:
4117
4118 template <class T> struct S {};
4119 template <class T> struct S<T*> {};
4120
4121 and supposing that we are instantiating S<int*>, ARGS will
4122 present be {int*} but we need {int}. */
4123 tree inner_args
4124 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
4125 args);
4126
4127 /* If there were multiple levels in ARGS, replacing the
4128 innermost level would alter CLASSTYPE_TI_ARGS, which we don't
4129 want, so we make a copy first. */
4130 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
4131 {
4132 args = copy_node (args);
4133 SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
4134 }
4135 else
4136 args = inner_args;
4137 }
4138
4139 if (pedantic && uses_template_parms (args))
4140 /* If there are still template parameters amongst the args, then
4141 we can't instantiate the type; there's no telling whether or not one
4142 of the template parameters might eventually be instantiated to some
4143 value that results in a specialization being used. */
4144 return type;
4145
4146 TYPE_BEING_DEFINED (type) = 1;
4147
4148 if (! push_tinst_level (type))
4149 return type;
4150
4151 maybe_push_to_top_level (uses_template_parms (type));
4152 pushclass (type, 0);
4153
4154 /* We must copy the arguments to the permanent obstack since
4155 during the tsubst'ing below they may wind up in the
4156 DECL_TI_ARGS of some instantiated member template. */
4157 args = copy_to_permanent (args);
4158
4159 if (flag_external_templates)
4160 {
4161 if (flag_alt_external_templates)
4162 {
4163 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
4164 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
4165 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4166 = (! CLASSTYPE_INTERFACE_ONLY (type)
4167 && CLASSTYPE_INTERFACE_KNOWN (type));
4168 }
4169 else
4170 {
4171 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
4172 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
4173 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
4174 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
4175 = (! CLASSTYPE_INTERFACE_ONLY (type)
4176 && CLASSTYPE_INTERFACE_KNOWN (type));
4177 }
4178 }
4179 else
4180 {
4181 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
4182 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
4183 }
4184
4185 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
4186 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
4187 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
4188 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
4189 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
4190 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
4191 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
4192 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
4193 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
4194 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
4195 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
4196 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
4197 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
4198 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
4199 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
4200 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
4201 TYPE_USES_COMPLEX_INHERITANCE (type)
4202 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
4203 TYPE_USES_MULTIPLE_INHERITANCE (type)
4204 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
4205 TYPE_USES_VIRTUAL_BASECLASSES (type)
4206 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
4207 TYPE_PACKED (type) = TYPE_PACKED (pattern);
4208 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
4209 TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
4210
4211 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
4212
4213 /* If this is a partial instantiation, don't tsubst anything. We will
4214 only use this type for implicit typename, so the actual contents don't
4215 matter. All that matters is whether a particular name is a type. */
4216 if (uses_template_parms (type))
4217 {
4218 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
4219 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
4220 TYPE_METHODS (type) = TYPE_METHODS (pattern);
4221 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
4222 TYPE_SIZE (type) = integer_zero_node;
4223 goto end;
4224 }
4225
4226 {
4227 tree binfo = TYPE_BINFO (type);
4228 tree pbases = TYPE_BINFO_BASETYPES (pattern);
4229
4230 if (pbases)
4231 {
4232 tree bases;
4233 int i;
4234 int len = TREE_VEC_LENGTH (pbases);
4235 bases = make_tree_vec (len);
4236 for (i = 0; i < len; ++i)
4237 {
4238 tree elt, basetype;
4239
4240 TREE_VEC_ELT (bases, i) = elt
4241 = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE);
4242 BINFO_INHERITANCE_CHAIN (elt) = binfo;
4243
4244 basetype = TREE_TYPE (elt);
4245
4246 if (! IS_AGGR_TYPE (basetype))
4247 cp_error
4248 ("base type `%T' of `%T' fails to be a struct or class type",
4249 basetype, type);
4250 else if (TYPE_SIZE (complete_type (basetype)) == NULL_TREE)
4251 cp_error ("base class `%T' of `%T' has incomplete type",
4252 basetype, type);
4253
4254 /* These are set up in xref_basetypes for normal classes, so
4255 we have to handle them here for template bases. */
4256 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype))
4257 {
4258 TYPE_USES_VIRTUAL_BASECLASSES (type) = 1;
4259 TYPE_USES_COMPLEX_INHERITANCE (type) = 1;
4260 }
4261 TYPE_GETS_NEW (type) |= TYPE_GETS_NEW (basetype);
4262 TYPE_GETS_DELETE (type) |= TYPE_GETS_DELETE (basetype);
4263 CLASSTYPE_LOCAL_TYPEDECLS (type)
4264 |= CLASSTYPE_LOCAL_TYPEDECLS (basetype);
4265 }
4266 /* Don't initialize this until the vector is filled out, or
4267 lookups will crash. */
4268 BINFO_BASETYPES (binfo) = bases;
4269 }
4270 }
4271
4272 field_chain = &TYPE_FIELDS (type);
4273
4274 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
4275 {
4276 tree tag = TREE_VALUE (t);
4277 tree name = TYPE_IDENTIFIER (tag);
4278 tree newtag;
4279
4280 newtag = tsubst (tag, args, NULL_TREE);
4281 if (TREE_CODE (newtag) == ENUMERAL_TYPE)
4282 {
4283 extern tree current_local_enum;
4284 tree prev_local_enum = current_local_enum;
4285
4286 if (TYPE_VALUES (newtag))
4287 {
4288 tree v;
4289
4290 /* We must set things up so that CURRENT_LOCAL_ENUM is the
4291 CONST_DECL for the last enumeration constant, since the
4292 CONST_DECLs are chained backwards. */
4293 for (v = TYPE_VALUES (newtag); TREE_CHAIN (v);
4294 v = TREE_CHAIN (v))
4295 ;
4296
4297 current_local_enum
4298 = IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (v));
4299 *field_chain = grok_enum_decls (NULL_TREE);
4300 current_local_enum = prev_local_enum;
4301
4302 while (*field_chain)
4303 {
4304 DECL_FIELD_CONTEXT (*field_chain) = type;
4305 field_chain = &TREE_CHAIN (*field_chain);
4306 }
4307 }
4308 }
4309 else
4310 {
4311 /* Now, we call pushtag to put this NEWTAG into the scope of
4312 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
4313 pushtag calling push_template_decl. We don't have to do
4314 this for enums because it will already have been done in
4315 tsubst_enum. */
4316 if (name)
4317 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
4318 pushtag (name, newtag, /*globalize=*/0);
4319 }
4320 }
4321
4322 /* Don't replace enum constants here. */
4323 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
4324 if (TREE_CODE (t) != CONST_DECL)
4325 {
4326 tree r;
4327
4328 /* The the file and line for this declaration, to assist in
4329 error message reporting. Since we called push_tinst_level
4330 above, we don't need to restore these. */
4331 lineno = DECL_SOURCE_LINE (t);
4332 input_filename = DECL_SOURCE_FILE (t);
4333
4334 r = tsubst (t, args, NULL_TREE);
4335 if (TREE_CODE (r) == VAR_DECL)
4336 {
4337 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
4338 /* Perhaps we should do more of grokfield here. */
4339 start_decl_1 (r);
4340 DECL_IN_AGGR_P (r) = 1;
4341 DECL_EXTERNAL (r) = 1;
4342 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
4343 }
4344
4345 *field_chain = r;
4346 field_chain = &TREE_CHAIN (r);
4347 }
4348
4349 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
4350
4351 /* Construct the DECL_FRIENDLIST for the new class type. */
4352 typedecl = TYPE_MAIN_DECL (type);
4353 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
4354 t != NULL_TREE;
4355 t = TREE_CHAIN (t))
4356 {
4357 tree friends;
4358
4359 DECL_FRIENDLIST (typedecl)
4360 = tree_cons (TREE_PURPOSE (t), NULL_TREE,
4361 DECL_FRIENDLIST (typedecl));
4362
4363 for (friends = TREE_VALUE (t);
4364 friends != NULL_TREE;
4365 friends = TREE_CHAIN (friends))
4366 {
4367 if (TREE_PURPOSE (friends) == error_mark_node)
4368 {
4369 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4370 = tree_cons (error_mark_node,
4371 tsubst_friend_function (TREE_VALUE (friends),
4372 args),
4373 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
4374 }
4375 else
4376 {
4377 TREE_VALUE (DECL_FRIENDLIST (typedecl))
4378 = tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE),
4379 NULL_TREE,
4380 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
4381
4382 }
4383 }
4384 }
4385
4386 for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
4387 t != NULL_TREE;
4388 t = TREE_CHAIN (t))
4389 {
4390 tree friend_type = TREE_VALUE (t);
4391 tree new_friend_type;
4392
4393 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4394 new_friend_type = tsubst_friend_class (friend_type, args);
4395 else if (uses_template_parms (friend_type))
4396 new_friend_type = tsubst (friend_type, args, NULL_TREE);
4397 else
4398 /* The call to xref_tag_from_type does injection for friend
4399 classes. */
4400 new_friend_type =
4401 xref_tag_from_type (friend_type, NULL_TREE, 1);
4402
4403
4404 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4405 /* Trick make_friend_class into realizing that the friend
4406 we're adding is a template, not an ordinary class. It's
4407 important that we use make_friend_class since it will
4408 perform some error-checking and output cross-reference
4409 information. */
4410 ++processing_template_decl;
4411
4412 make_friend_class (type, new_friend_type);
4413
4414 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
4415 --processing_template_decl;
4416 }
4417
4418 /* This does injection for friend functions. */
4419 if (!processing_template_decl)
4420 {
4421 t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE);
4422
4423 for (; t; t = TREE_CHAIN (t))
4424 {
4425 tree d = TREE_VALUE (t);
4426
4427 if (TREE_CODE (d) == TYPE_DECL)
4428 /* Already injected. */;
4429 else
4430 pushdecl (d);
4431 }
4432 }
4433
4434 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
4435 if (TREE_CODE (t) == FIELD_DECL)
4436 {
4437 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
4438 require_complete_type (t);
4439 }
4440
4441 type = finish_struct_1 (type, 0);
4442 CLASSTYPE_GOT_SEMICOLON (type) = 1;
4443
4444 repo_template_used (type);
4445 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
4446 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
4447
4448 end:
4449 TYPE_BEING_DEFINED (type) = 0;
4450 popclass (0);
4451
4452 pop_from_top_level ();
4453 pop_tinst_level ();
4454
4455 return type;
4456 }
4457
4458 static int
4459 list_eq (t1, t2)
4460 tree t1, t2;
4461 {
4462 if (t1 == NULL_TREE)
4463 return t2 == NULL_TREE;
4464 if (t2 == NULL_TREE)
4465 return 0;
4466 /* Don't care if one declares its arg const and the other doesn't -- the
4467 main variant of the arg type is all that matters. */
4468 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
4469 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
4470 return 0;
4471 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
4472 }
4473
4474 /* If arg is a non-type template parameter that does not depend on template
4475 arguments, fold it like we weren't in the body of a template. */
4476
4477 static tree
4478 maybe_fold_nontype_arg (arg)
4479 tree arg;
4480 {
4481 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
4482 && !uses_template_parms (arg))
4483 {
4484 /* Sometimes, one of the args was an expression involving a
4485 template constant parameter, like N - 1. Now that we've
4486 tsubst'd, we might have something like 2 - 1. This will
4487 confuse lookup_template_class, so we do constant folding
4488 here. We have to unset processing_template_decl, to
4489 fool build_expr_from_tree() into building an actual
4490 tree. */
4491
4492 int saved_processing_template_decl = processing_template_decl;
4493 processing_template_decl = 0;
4494 arg = fold (build_expr_from_tree (arg));
4495 processing_template_decl = saved_processing_template_decl;
4496 }
4497 return arg;
4498 }
4499
4500 /* Return the TREE_VEC with the arguments for the innermost template header,
4501 where ARGS is either that or the VEC of VECs for all the
4502 arguments. */
4503
4504 tree
4505 innermost_args (args)
4506 tree args;
4507 {
4508 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
4509 }
4510
4511 /* Substitute ARGS into the vector of template arguments T. */
4512
4513 tree
4514 tsubst_template_arg_vector (t, args)
4515 tree t;
4516 tree args;
4517 {
4518 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
4519 tree *elts = (tree *) alloca (len * sizeof (tree));
4520
4521 bzero ((char *) elts, len * sizeof (tree));
4522
4523 for (i = 0; i < len; i++)
4524 {
4525 if (TREE_VEC_ELT (t, i) != NULL_TREE
4526 && TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
4527 elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i), args);
4528 else
4529 elts[i] = maybe_fold_nontype_arg
4530 (tsubst_expr (TREE_VEC_ELT (t, i), args, NULL_TREE));
4531
4532 if (elts[i] != TREE_VEC_ELT (t, i))
4533 need_new = 1;
4534 }
4535
4536 if (!need_new)
4537 return t;
4538
4539 t = make_tree_vec (len);
4540 for (i = 0; i < len; i++)
4541 TREE_VEC_ELT (t, i) = elts[i];
4542
4543 return t;
4544 }
4545
4546 /* Return the result of substituting ARGS into the template parameters
4547 given by PARMS. If there are m levels of ARGS and m + n levels of
4548 PARMS, then the result will contain n levels of PARMS. For
4549 example, if PARMS is `template <class T> template <class U>
4550 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
4551 result will be `template <int*, double, class V>'. */
4552
4553 tree
4554 tsubst_template_parms (parms, args)
4555 tree parms;
4556 tree args;
4557 {
4558 tree r;
4559 tree* new_parms = &r;
4560
4561 for (new_parms = &r;
4562 TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
4563 new_parms = &(TREE_CHAIN (*new_parms)),
4564 parms = TREE_CHAIN (parms))
4565 {
4566 tree new_vec =
4567 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
4568 int i;
4569
4570 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
4571 {
4572 tree default_value =
4573 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
4574 tree parm_decl =
4575 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
4576
4577 TREE_VEC_ELT (new_vec, i)
4578 = build_tree_list (tsubst (default_value, args, NULL_TREE),
4579 tsubst (parm_decl, args, NULL_TREE));
4580
4581 }
4582
4583 *new_parms =
4584 tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms)
4585 - TMPL_ARGS_DEPTH (args))),
4586 new_vec, NULL_TREE);
4587 }
4588
4589 return r;
4590 }
4591
4592 /* Substitute the ARGS into the indicated aggregate (or enumeration)
4593 type T. If T is not an aggregate or enumeration type, it is
4594 handled as if by tsubst. IN_DECL is as for tsubst. If
4595 ENTERING_SCOPE is non-zero, T is the context for a template which
4596 we are presently tsubst'ing. Return the subsituted value. */
4597
4598 tree
4599 tsubst_aggr_type (t, args, in_decl, entering_scope)
4600 tree t;
4601 tree args;
4602 tree in_decl;
4603 int entering_scope;
4604 {
4605 if (t == NULL_TREE)
4606 return NULL_TREE;
4607
4608 switch (TREE_CODE (t))
4609 {
4610 case RECORD_TYPE:
4611 if (TYPE_PTRMEMFUNC_P (t))
4612 {
4613 tree r = build_ptrmemfunc_type
4614 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl));
4615 return cp_build_type_variant (r, TYPE_READONLY (t),
4616 TYPE_VOLATILE (t));
4617 }
4618
4619 /* else fall through */
4620 case ENUMERAL_TYPE:
4621 case UNION_TYPE:
4622 if (uses_template_parms (t))
4623 {
4624 tree argvec;
4625 tree context;
4626 tree r;
4627
4628 /* First, determine the context for the type we are looking
4629 up. */
4630 if (TYPE_CONTEXT (t) != NULL_TREE)
4631 context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
4632 in_decl, /*entering_scope=*/1);
4633 else
4634 context = NULL_TREE;
4635
4636 /* Then, figure out what arguments are appropriate for the
4637 type we are trying to find. For example, given:
4638
4639 template <class T> struct S;
4640 template <class T, class U> void f(T, U) { S<U> su; }
4641
4642 and supposing that we are instantiating f<int, double>,
4643 then our ARGS will be {int, double}, but, when looking up
4644 S we only want {double}. */
4645 argvec = tsubst (TYPE_TI_ARGS (t), args, in_decl);
4646
4647 r = lookup_template_class (t, argvec, in_decl, context,
4648 entering_scope);
4649
4650 return cp_build_type_variant (r, TYPE_READONLY (t),
4651 TYPE_VOLATILE (t));
4652 }
4653 else
4654 /* This is not a template type, so there's nothing to do. */
4655 return t;
4656
4657 default:
4658 return tsubst (t, args, in_decl);
4659 }
4660 }
4661
4662 /* Substitute the ARGS into the T, which is a _DECL. TYPE is the
4663 (already computed) substitution of ARGS into TREE_TYPE (T), if
4664 appropriate. Return the result of the substitution. IN_DECL is as
4665 for tsubst. */
4666
4667 tree
4668 tsubst_decl (t, args, type, in_decl)
4669 tree t;
4670 tree args;
4671 tree type;
4672 tree in_decl;
4673 {
4674 int saved_lineno;
4675 char* saved_filename;
4676 tree r;
4677
4678 /* Set the filename and linenumber to improve error-reporting. */
4679 saved_lineno = lineno;
4680 saved_filename = input_filename;
4681 lineno = DECL_SOURCE_LINE (t);
4682 input_filename = DECL_SOURCE_FILE (t);
4683
4684 switch (TREE_CODE (t))
4685 {
4686 case TEMPLATE_DECL:
4687 {
4688 /* We can get here when processing a member template function
4689 of a template class. */
4690 tree decl = DECL_TEMPLATE_RESULT (t);
4691 tree parms;
4692 tree* new_parms;
4693 tree spec;
4694 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
4695
4696 if (!is_template_template_parm)
4697 {
4698 /* We might already have an instance of this template.
4699 The ARGS are for the surrounding class type, so the
4700 full args contain the tsubst'd args for the context,
4701 plus the innermost args from the template decl. */
4702 tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
4703 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
4704 : DECL_TI_ARGS (DECL_RESULT (t));
4705 tree full_args = tsubst (tmpl_args, args, in_decl);
4706
4707 /* tsubst_template_arg_vector doesn't copy the vector if
4708 nothing changed. But, *something* should have
4709 changed. */
4710 my_friendly_assert (full_args != tmpl_args, 0);
4711
4712 spec = retrieve_specialization (t, full_args);
4713 if (spec != NULL_TREE)
4714 {
4715 r = spec;
4716 break;
4717 }
4718 }
4719
4720 /* Make a new template decl. It will be similar to the
4721 original, but will record the current template arguments.
4722 We also create a new function declaration, which is just
4723 like the old one, but points to this new template, rather
4724 than the old one. */
4725 r = copy_node (t);
4726 copy_lang_decl (r);
4727 my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
4728 TREE_CHAIN (r) = NULL_TREE;
4729
4730 if (is_template_template_parm)
4731 {
4732 tree new_decl = tsubst (decl, args, in_decl);
4733 DECL_RESULT (r) = new_decl;
4734 TREE_TYPE (r) = TREE_TYPE (new_decl);
4735 break;
4736 }
4737
4738 DECL_CONTEXT (r)
4739 = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
4740 /*entering_scope=*/1);
4741 DECL_CLASS_CONTEXT (r)
4742 = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, in_decl,
4743 /*entering_scope=*/1);
4744 DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
4745
4746 if (TREE_CODE (decl) == TYPE_DECL)
4747 {
4748 tree new_type = tsubst (TREE_TYPE (t), args, in_decl);
4749 TREE_TYPE (r) = new_type;
4750 CLASSTYPE_TI_TEMPLATE (new_type) = r;
4751 DECL_RESULT (r) = TYPE_MAIN_DECL (new_type);
4752 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
4753 }
4754 else
4755 {
4756 tree new_decl = tsubst (decl, args, in_decl);
4757 DECL_RESULT (r) = new_decl;
4758 DECL_TI_TEMPLATE (new_decl) = r;
4759 TREE_TYPE (r) = TREE_TYPE (new_decl);
4760 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
4761 }
4762
4763 SET_DECL_IMPLICIT_INSTANTIATION (r);
4764 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
4765 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
4766
4767 /* The template parameters for this new template are all the
4768 template parameters for the old template, except the
4769 outermost level of parameters. */
4770 DECL_TEMPLATE_PARMS (r)
4771 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args);
4772
4773 if (PRIMARY_TEMPLATE_P (t))
4774 DECL_PRIMARY_TEMPLATE (r) = r;
4775
4776 /* We don't partially instantiate partial specializations. */
4777 if (TREE_CODE (decl) == TYPE_DECL)
4778 break;
4779
4780 for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
4781 spec != NULL_TREE;
4782 spec = TREE_CHAIN (spec))
4783 {
4784 /* It helps to consider example here. Consider:
4785
4786 template <class T>
4787 struct S {
4788 template <class U>
4789 void f(U u);
4790
4791 template <>
4792 void f(T* t) {}
4793 };
4794
4795 Now, for example, we are instantiating S<int>::f(U u).
4796 We want to make a template:
4797
4798 template <class U>
4799 void S<int>::f(U);
4800
4801 It will have a specialization, for the case U = int*, of
4802 the form:
4803
4804 template <>
4805 void S<int>::f<int*>(int*);
4806
4807 This specialization will be an instantiation of
4808 the specialization given in the declaration of S, with
4809 argument list int*. */
4810
4811 tree fn = TREE_VALUE (spec);
4812 tree spec_args;
4813 tree new_fn;
4814
4815 if (!DECL_TEMPLATE_SPECIALIZATION (fn))
4816 /* Instantiations are on the same list, but they're of
4817 no concern to us. */
4818 continue;
4819
4820 if (TREE_CODE (fn) != TEMPLATE_DECL)
4821 /* A full specialization. There's no need to record
4822 that here. */
4823 continue;
4824
4825 spec_args = tsubst (DECL_TI_ARGS (fn), args, in_decl);
4826 new_fn = tsubst (DECL_RESULT (most_general_template (fn)),
4827 spec_args, in_decl);
4828 DECL_TI_TEMPLATE (new_fn) = fn;
4829 register_specialization (new_fn, r,
4830 innermost_args (spec_args));
4831 }
4832
4833 /* Record this partial instantiation. */
4834 register_specialization (r, t,
4835 DECL_TI_ARGS (DECL_RESULT (r)));
4836
4837 }
4838 break;
4839
4840 case FUNCTION_DECL:
4841 {
4842 tree ctx;
4843 tree argvec;
4844 tree gen_tmpl;
4845 int member;
4846
4847 /* Nobody should be tsubst'ing into non-template functions. */
4848 my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
4849
4850 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
4851 {
4852 tree spec;
4853
4854 /* Calculate the most general template of which R is a
4855 specialization, and the complete set of arguments used to
4856 specialize R. */
4857 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
4858 argvec = tsubst (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (gen_tmpl)),
4859 args, in_decl);
4860
4861 /* Check to see if we already have this specialization. */
4862 spec = retrieve_specialization (gen_tmpl, argvec);
4863 if (spec)
4864 {
4865 r = spec;
4866 break;
4867 }
4868 }
4869 else
4870 {
4871 /* This special case arises when we have something like this:
4872
4873 template <class T> struct S {
4874 friend void f<int>(int, double);
4875 };
4876
4877 Here, the DECL_TI_TEMPLATE for the friend declaration
4878 will be a LOOKUP_EXPR or an IDENTIFIER_NODE. We are
4879 being called from tsubst_friend_function, and we want
4880 only to create a new decl (R) with appropriate types so
4881 that we can call determine_specialization. */
4882 my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
4883 == LOOKUP_EXPR)
4884 || (TREE_CODE (DECL_TI_TEMPLATE (t))
4885 == IDENTIFIER_NODE), 0);
4886 gen_tmpl = NULL_TREE;
4887 }
4888
4889 if (DECL_CLASS_SCOPE_P (t))
4890 {
4891 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
4892 member = 2;
4893 else
4894 member = 1;
4895 ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args, t,
4896 /*entering_scope=*/1);
4897 }
4898 else
4899 {
4900 member = 0;
4901 ctx = NULL_TREE;
4902 }
4903 type = tsubst (type, args, in_decl);
4904
4905 /* We do NOT check for matching decls pushed separately at this
4906 point, as they may not represent instantiations of this
4907 template, and in any case are considered separate under the
4908 discrete model. Instead, see add_maybe_template. */
4909
4910 r = copy_node (t);
4911 copy_lang_decl (r);
4912 DECL_USE_TEMPLATE (r) = 0;
4913 TREE_TYPE (r) = type;
4914
4915 DECL_CONTEXT (r)
4916 = tsubst_aggr_type (DECL_CONTEXT (t), args, t, /*entering_scope=*/1);
4917 DECL_CLASS_CONTEXT (r) = ctx;
4918
4919 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
4920 IDENTIFIER_POINTER (DECL_NAME (r)),
4921 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
4922 {
4923 /* Type-conversion operator. Reconstruct the name, in
4924 case it's the name of one of the template's parameters. */
4925 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
4926 }
4927
4928 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t);
4929 DECL_MAIN_VARIANT (r) = r;
4930 DECL_RESULT (r) = NULL_TREE;
4931 DECL_INITIAL (r) = NULL_TREE;
4932
4933 TREE_STATIC (r) = 0;
4934 TREE_PUBLIC (r) = TREE_PUBLIC (t);
4935 DECL_EXTERNAL (r) = 1;
4936 DECL_INTERFACE_KNOWN (r) = 0;
4937 DECL_DEFER_OUTPUT (r) = 0;
4938 TREE_CHAIN (r) = NULL_TREE;
4939 DECL_PENDING_INLINE_INFO (r) = 0;
4940 TREE_USED (r) = 0;
4941
4942 if (DECL_CONSTRUCTOR_P (r))
4943 {
4944 maybe_retrofit_in_chrg (r);
4945 grok_ctor_properties (ctx, r);
4946 }
4947 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
4948 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
4949
4950 /* Set up the DECL_TEMPLATE_INFO for R and compute its mangled
4951 name. There's no need to do this in the special friend
4952 case mentioned above where GEN_TMPL is NULL. */
4953 if (gen_tmpl)
4954 {
4955 DECL_TEMPLATE_INFO (r)
4956 = perm_tree_cons (gen_tmpl, argvec, NULL_TREE);
4957 SET_DECL_IMPLICIT_INSTANTIATION (r);
4958 register_specialization (r, gen_tmpl, argvec);
4959
4960 /* Set the mangled name for R. */
4961 if (DECL_DESTRUCTOR_P (t))
4962 DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx);
4963 else
4964 {
4965 /* Instantiations of template functions must be mangled
4966 specially, in order to conform to 14.5.5.1
4967 [temp.over.link]. */
4968 tree tmpl = DECL_TI_TEMPLATE (t);
4969
4970 /* TMPL will be NULL if this is a specialization of a
4971 member function of a template class. */
4972 if (name_mangling_version < 1
4973 || tmpl == NULL_TREE
4974 || (member && !is_member_template (tmpl)
4975 && !DECL_TEMPLATE_INFO (tmpl)))
4976 set_mangled_name_for_decl (r);
4977 else
4978 set_mangled_name_for_template_decl (r);
4979 }
4980
4981 DECL_RTL (r) = 0;
4982 make_decl_rtl (r, NULL_PTR, 1);
4983
4984 /* Like grokfndecl. If we don't do this, pushdecl will
4985 mess up our TREE_CHAIN because it doesn't find a
4986 previous decl. Sigh. */
4987 if (member
4988 && (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r))
4989 == NULL_TREE))
4990 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
4991 }
4992 }
4993 break;
4994
4995 case PARM_DECL:
4996 {
4997 r = copy_node (t);
4998 TREE_TYPE (r) = type;
4999 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
5000 DECL_INITIAL (r) = TREE_TYPE (r);
5001 else
5002 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl);
5003
5004 DECL_CONTEXT (r) = NULL_TREE;
5005 #ifdef PROMOTE_PROTOTYPES
5006 if ((TREE_CODE (type) == INTEGER_TYPE
5007 || TREE_CODE (type) == ENUMERAL_TYPE)
5008 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
5009 DECL_ARG_TYPE (r) = integer_type_node;
5010 #endif
5011 if (TREE_CHAIN (t))
5012 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t));
5013 }
5014 break;
5015
5016 case FIELD_DECL:
5017 {
5018 r = copy_node (t);
5019 TREE_TYPE (r) = type;
5020 copy_lang_decl (r);
5021 #if 0
5022 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl);
5023 #endif
5024 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl);
5025 TREE_CHAIN (r) = NULL_TREE;
5026 if (TREE_CODE (type) == VOID_TYPE)
5027 cp_error_at ("instantiation of `%D' as type void", r);
5028 }
5029 break;
5030
5031 case USING_DECL:
5032 {
5033 r = copy_node (t);
5034 DECL_INITIAL (r)
5035 = tsubst_copy (DECL_INITIAL (t), args, in_decl);
5036 TREE_CHAIN (r) = NULL_TREE;
5037 }
5038 break;
5039
5040 case VAR_DECL:
5041 {
5042 tree argvec;
5043 tree gen_tmpl;
5044 tree spec;
5045 tree tmpl;
5046 tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5047 /*entering_scope=*/1);
5048
5049 /* Nobody should be tsubst'ing into non-template variables. */
5050 my_friendly_assert (DECL_LANG_SPECIFIC (t)
5051 && DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
5052
5053 /* Check to see if we already have this specialization. */
5054 tmpl = DECL_TI_TEMPLATE (t);
5055 gen_tmpl = most_general_template (tmpl);
5056 argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
5057 spec = retrieve_specialization (gen_tmpl, argvec);
5058
5059 if (spec)
5060 {
5061 r = spec;
5062 break;
5063 }
5064
5065 r = copy_node (t);
5066 TREE_TYPE (r) = type;
5067 DECL_CONTEXT (r) = ctx;
5068 if (TREE_STATIC (r))
5069 DECL_ASSEMBLER_NAME (r)
5070 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
5071
5072 /* Don't try to expand the initializer until someone tries to use
5073 this variable; otherwise we run into circular dependencies. */
5074 DECL_INITIAL (r) = NULL_TREE;
5075 DECL_RTL (r) = 0;
5076 DECL_SIZE (r) = 0;
5077 copy_lang_decl (r);
5078 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
5079
5080 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
5081 SET_DECL_IMPLICIT_INSTANTIATION (r);
5082 register_specialization (r, gen_tmpl, argvec);
5083
5084 TREE_CHAIN (r) = NULL_TREE;
5085 if (TREE_CODE (type) == VOID_TYPE)
5086 cp_error_at ("instantiation of `%D' as type void", r);
5087 }
5088 break;
5089
5090 case TYPE_DECL:
5091 if (t == TYPE_NAME (TREE_TYPE (t)))
5092 r = TYPE_NAME (type);
5093 else
5094 {
5095 r = copy_node (t);
5096 TREE_TYPE (r) = type;
5097 DECL_CONTEXT (r) = current_class_type;
5098 TREE_CHAIN (r) = NULL_TREE;
5099 }
5100 break;
5101
5102 default:
5103 my_friendly_abort (0);
5104 }
5105
5106 /* Restore the file and line information. */
5107 lineno = saved_lineno;
5108 input_filename = saved_filename;
5109
5110 return r;
5111 }
5112
5113
5114 /* Take the tree structure T and replace template parameters used therein
5115 with the argument vector ARGS. IN_DECL is an associated decl for
5116 diagnostics.
5117
5118 tsubst is used for dealing with types, decls and the like; for
5119 expressions, use tsubst_expr or tsubst_copy. */
5120
5121 tree
5122 tsubst (t, args, in_decl)
5123 tree t, args;
5124 tree in_decl;
5125 {
5126 tree type;
5127
5128 if (t == NULL_TREE || t == error_mark_node
5129 || t == integer_type_node
5130 || t == void_type_node
5131 || t == char_type_node
5132 || TREE_CODE (t) == NAMESPACE_DECL)
5133 return t;
5134
5135 if (TREE_CODE (t) == IDENTIFIER_NODE)
5136 type = IDENTIFIER_TYPE_VALUE (t);
5137 else
5138 type = TREE_TYPE (t);
5139 if (type == unknown_type_node)
5140 my_friendly_abort (42);
5141
5142 if (type && TREE_CODE (t) != FUNCTION_DECL
5143 && TREE_CODE (t) != TYPENAME_TYPE
5144 && TREE_CODE (t) != TEMPLATE_DECL
5145 && TREE_CODE (t) != IDENTIFIER_NODE)
5146 type = tsubst (type, args, in_decl);
5147
5148 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
5149 return tsubst_decl (t, args, type, in_decl);
5150
5151 switch (TREE_CODE (t))
5152 {
5153 case RECORD_TYPE:
5154 case UNION_TYPE:
5155 case ENUMERAL_TYPE:
5156 return tsubst_aggr_type (t, args, in_decl, /*entering_scope=*/0);
5157
5158 case ERROR_MARK:
5159 case IDENTIFIER_NODE:
5160 case OP_IDENTIFIER:
5161 case VOID_TYPE:
5162 case REAL_TYPE:
5163 case COMPLEX_TYPE:
5164 case BOOLEAN_TYPE:
5165 case INTEGER_CST:
5166 case REAL_CST:
5167 case STRING_CST:
5168 return t;
5169
5170 case INTEGER_TYPE:
5171 if (t == integer_type_node)
5172 return t;
5173
5174 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
5175 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
5176 return t;
5177
5178 {
5179 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
5180 max = tsubst_expr (max, args, in_decl);
5181 if (processing_template_decl)
5182 {
5183 tree itype = make_node (INTEGER_TYPE);
5184 TYPE_MIN_VALUE (itype) = size_zero_node;
5185 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
5186 integer_one_node);
5187 return itype;
5188 }
5189
5190 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
5191 return build_index_2_type (size_zero_node, max);
5192 }
5193
5194 case TEMPLATE_TYPE_PARM:
5195 case TEMPLATE_TEMPLATE_PARM:
5196 case TEMPLATE_PARM_INDEX:
5197 {
5198 int idx;
5199 int level;
5200 int levels;
5201 tree r = NULL_TREE;
5202
5203 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
5204 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5205 {
5206 idx = TEMPLATE_TYPE_IDX (t);
5207 level = TEMPLATE_TYPE_LEVEL (t);
5208 }
5209 else
5210 {
5211 idx = TEMPLATE_PARM_IDX (t);
5212 level = TEMPLATE_PARM_LEVEL (t);
5213 }
5214
5215 if (TREE_VEC_LENGTH (args) > 0)
5216 {
5217 tree arg = NULL_TREE;
5218
5219 levels = TMPL_ARGS_DEPTH (args);
5220 if (level <= levels)
5221 arg = TMPL_ARG (args, level, idx);
5222
5223 if (arg != NULL_TREE)
5224 {
5225 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5226 {
5227 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg))
5228 == 't', 0);
5229 return cp_build_type_variant
5230 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
5231 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
5232 }
5233 else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
5234 {
5235 if (CLASSTYPE_TEMPLATE_INFO (t))
5236 {
5237 /* We are processing a type constructed from
5238 a template template parameter */
5239 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t),
5240 args, in_decl);
5241 tree r;
5242
5243 /* We can get a TEMPLATE_TEMPLATE_PARM here when
5244 we are resolving nested-types in the signature of
5245 a member function templates.
5246 Otherwise ARG is a TEMPLATE_DECL and is the real
5247 template to be instantiated. */
5248 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
5249 arg = TYPE_NAME (arg);
5250
5251 r = lookup_template_class (DECL_NAME (arg),
5252 argvec, in_decl,
5253 DECL_CONTEXT (arg),
5254 /*entering_scope=*/0);
5255 return cp_build_type_variant (r, TYPE_READONLY (t),
5256 TYPE_VOLATILE (t));
5257 }
5258 else
5259 /* We are processing a template argument list. */
5260 return arg;
5261 }
5262 else
5263 return arg;
5264 }
5265 }
5266
5267 if (level == 1)
5268 /* This can happen during the attempted tsubst'ing in
5269 unify. This means that we don't yet have any information
5270 about the template parameter in question. */
5271 return t;
5272
5273 /* If we get here, we must have been looking at a parm for a
5274 more deeply nested template. Make a new version of this
5275 template parameter, but with a lower level. */
5276 switch (TREE_CODE (t))
5277 {
5278 case TEMPLATE_TYPE_PARM:
5279 case TEMPLATE_TEMPLATE_PARM:
5280 r = copy_node (t);
5281 TEMPLATE_TYPE_PARM_INDEX (r)
5282 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
5283 r, levels);
5284 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
5285 TYPE_MAIN_VARIANT (r) = r;
5286 TYPE_POINTER_TO (r) = NULL_TREE;
5287 TYPE_REFERENCE_TO (r) = NULL_TREE;
5288
5289 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
5290 && CLASSTYPE_TEMPLATE_INFO (t))
5291 {
5292 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl);
5293 CLASSTYPE_TEMPLATE_INFO (r)
5294 = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
5295 }
5296 break;
5297
5298 case TEMPLATE_PARM_INDEX:
5299 r = reduce_template_parm_level (t, type, levels);
5300 break;
5301
5302 default:
5303 my_friendly_abort (0);
5304 }
5305
5306 return r;
5307 }
5308
5309 case TREE_LIST:
5310 {
5311 tree purpose, value, chain, result;
5312 int via_public, via_virtual, via_protected;
5313
5314 if (t == void_list_node)
5315 return t;
5316
5317 via_public = TREE_VIA_PUBLIC (t);
5318 via_protected = TREE_VIA_PROTECTED (t);
5319 via_virtual = TREE_VIA_VIRTUAL (t);
5320
5321 purpose = TREE_PURPOSE (t);
5322 if (purpose)
5323 purpose = tsubst (purpose, args, in_decl);
5324 value = TREE_VALUE (t);
5325 if (value)
5326 value = tsubst (value, args, in_decl);
5327 chain = TREE_CHAIN (t);
5328 if (chain && chain != void_type_node)
5329 chain = tsubst (chain, args, in_decl);
5330 if (purpose == TREE_PURPOSE (t)
5331 && value == TREE_VALUE (t)
5332 && chain == TREE_CHAIN (t))
5333 return t;
5334 result = hash_tree_cons (via_public, via_virtual, via_protected,
5335 purpose, value, chain);
5336 TREE_PARMLIST (result) = TREE_PARMLIST (t);
5337 return result;
5338 }
5339 case TREE_VEC:
5340 if (type != NULL_TREE)
5341 {
5342 /* A binfo node. We always need to make a copy, of the node
5343 itself and of its BINFO_BASETYPES. */
5344
5345 t = copy_node (t);
5346
5347 /* Make sure type isn't a typedef copy. */
5348 type = BINFO_TYPE (TYPE_BINFO (type));
5349
5350 TREE_TYPE (t) = complete_type (type);
5351 if (IS_AGGR_TYPE (type))
5352 {
5353 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
5354 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
5355 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
5356 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
5357 }
5358 return t;
5359 }
5360
5361 /* Otherwise, a vector of template arguments. */
5362 return tsubst_template_arg_vector (t, args);
5363
5364 case POINTER_TYPE:
5365 case REFERENCE_TYPE:
5366 {
5367 tree r;
5368 enum tree_code code;
5369
5370 if (type == TREE_TYPE (t))
5371 return t;
5372
5373 code = TREE_CODE (t);
5374 if (TREE_CODE (type) == REFERENCE_TYPE)
5375 {
5376 static int last_line = 0;
5377 static char* last_file = 0;
5378
5379 /* We keep track of the last time we issued this error
5380 message to avoid spewing a ton of messages during a
5381 single bad template instantiation. */
5382 if (last_line != lineno ||
5383 last_file != input_filename)
5384 {
5385 cp_error ("cannot form type %s to reference type %T during template instantiation",
5386 (code == POINTER_TYPE) ? "pointer" : "reference",
5387 type);
5388 last_line = lineno;
5389 last_file = input_filename;
5390 }
5391
5392 /* Use the underlying type in an attempt at error
5393 recovery; maybe the user meant vector<int> and wrote
5394 vector<int&>, or some such. */
5395 if (code == REFERENCE_TYPE)
5396 r = type;
5397 else
5398 r = build_pointer_type (TREE_TYPE (type));
5399 }
5400 else if (code == POINTER_TYPE)
5401 r = build_pointer_type (type);
5402 else
5403 r = build_reference_type (type);
5404 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
5405
5406 /* Will this ever be needed for TYPE_..._TO values? */
5407 layout_type (r);
5408 return r;
5409 }
5410 case OFFSET_TYPE:
5411 return build_offset_type
5412 (tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl), type);
5413 case FUNCTION_TYPE:
5414 case METHOD_TYPE:
5415 {
5416 tree values = TYPE_ARG_TYPES (t);
5417 tree context = TYPE_CONTEXT (t);
5418 tree raises = TYPE_RAISES_EXCEPTIONS (t);
5419 tree fntype;
5420
5421 /* Don't bother recursing if we know it won't change anything. */
5422 if (values != void_list_node)
5423 {
5424 /* This should probably be rewritten to use hash_tree_cons for
5425 the memory savings. */
5426 tree first = NULL_TREE;
5427 tree last = NULL_TREE;
5428
5429 for (; values && values != void_list_node;
5430 values = TREE_CHAIN (values))
5431 {
5432 tree value = TYPE_MAIN_VARIANT (type_decays_to
5433 (tsubst (TREE_VALUE (values), args, in_decl)));
5434 /* Don't instantiate default args unless they are used.
5435 Handle it in build_over_call instead. */
5436 tree purpose = TREE_PURPOSE (values);
5437 tree x = build_tree_list (purpose, value);
5438
5439 if (first)
5440 TREE_CHAIN (last) = x;
5441 else
5442 first = x;
5443 last = x;
5444 }
5445
5446 if (values == void_list_node)
5447 TREE_CHAIN (last) = void_list_node;
5448
5449 values = first;
5450 }
5451 if (context)
5452 context = tsubst (context, args, in_decl);
5453 /* Could also optimize cases where return value and
5454 values have common elements (e.g., T min(const &T, const T&). */
5455
5456 /* If the above parameters haven't changed, just return the type. */
5457 if (type == TREE_TYPE (t)
5458 && values == TYPE_VALUES (t)
5459 && context == TYPE_CONTEXT (t))
5460 return t;
5461
5462 /* Construct a new type node and return it. */
5463 if (TREE_CODE (t) == FUNCTION_TYPE
5464 && context == NULL_TREE)
5465 {
5466 fntype = build_function_type (type, values);
5467 }
5468 else if (context == NULL_TREE)
5469 {
5470 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
5471 args, in_decl);
5472 fntype = build_cplus_method_type (base, type,
5473 TREE_CHAIN (values));
5474 }
5475 else
5476 {
5477 fntype = make_node (TREE_CODE (t));
5478 TREE_TYPE (fntype) = type;
5479 TYPE_CONTEXT (fntype) = FROB_CONTEXT (context);
5480 TYPE_VALUES (fntype) = values;
5481 TYPE_SIZE (fntype) = TYPE_SIZE (t);
5482 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
5483 TYPE_MODE (fntype) = TYPE_MODE (t);
5484 if (TYPE_METHOD_BASETYPE (t))
5485 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
5486 args, in_decl);
5487 /* Need to generate hash value. */
5488 my_friendly_abort (84);
5489 }
5490 fntype = build_type_variant (fntype,
5491 TYPE_READONLY (t),
5492 TYPE_VOLATILE (t));
5493 if (raises)
5494 {
5495 raises = tsubst (raises, args, in_decl);
5496 fntype = build_exception_variant (fntype, raises);
5497 }
5498 return fntype;
5499 }
5500 case ARRAY_TYPE:
5501 {
5502 tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl);
5503 tree r;
5504 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
5505 return t;
5506 r = build_cplus_array_type (type, domain);
5507 return r;
5508 }
5509
5510 case PLUS_EXPR:
5511 case MINUS_EXPR:
5512 return fold (build (TREE_CODE (t), TREE_TYPE (t),
5513 tsubst (TREE_OPERAND (t, 0), args, in_decl),
5514 tsubst (TREE_OPERAND (t, 1), args, in_decl)));
5515
5516 case NEGATE_EXPR:
5517 case NOP_EXPR:
5518 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
5519 tsubst (TREE_OPERAND (t, 0), args, in_decl)));
5520
5521 case TYPENAME_TYPE:
5522 {
5523 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, in_decl,
5524 /*entering_scope=*/1);
5525 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl);
5526
5527 /* Normally, make_typename_type does not require that the CTX
5528 have complete type in order to allow things like:
5529
5530 template <class T> struct S { typename S<T>::X Y; };
5531
5532 But, such constructs have already been resolved by this
5533 point, so here CTX really should have complete type, unless
5534 it's a partial instantiation. */
5535 if (!uses_template_parms (ctx)
5536 && !complete_type_or_else (ctx))
5537 return error_mark_node;
5538
5539 f = make_typename_type (ctx, f);
5540 return cp_build_type_variant
5541 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
5542 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
5543 }
5544
5545 case INDIRECT_REF:
5546 return make_pointer_declarator
5547 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
5548
5549 case ADDR_EXPR:
5550 return make_reference_declarator
5551 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
5552
5553 case ARRAY_REF:
5554 return build_parse_node
5555 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl),
5556 tsubst_expr (TREE_OPERAND (t, 1), args, in_decl));
5557
5558 case CALL_EXPR:
5559 return make_call_declarator
5560 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
5561 tsubst (TREE_OPERAND (t, 1), args, in_decl),
5562 TREE_OPERAND (t, 2),
5563 tsubst (TREE_TYPE (t), args, in_decl));
5564
5565 case SCOPE_REF:
5566 return build_parse_node
5567 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl),
5568 tsubst (TREE_OPERAND (t, 1), args, in_decl));
5569
5570 default:
5571 sorry ("use of `%s' in template",
5572 tree_code_name [(int) TREE_CODE (t)]);
5573 return error_mark_node;
5574 }
5575 }
5576
5577 void
5578 do_pushlevel ()
5579 {
5580 emit_line_note (input_filename, lineno);
5581 pushlevel (0);
5582 clear_last_expr ();
5583 push_momentary ();
5584 expand_start_bindings (0);
5585 }
5586
5587 tree
5588 do_poplevel ()
5589 {
5590 tree t;
5591 int saved_warn_unused = 0;
5592
5593 if (processing_template_decl)
5594 {
5595 saved_warn_unused = warn_unused;
5596 warn_unused = 0;
5597 }
5598 expand_end_bindings (getdecls (), kept_level_p (), 0);
5599 if (processing_template_decl)
5600 warn_unused = saved_warn_unused;
5601 t = poplevel (kept_level_p (), 1, 0);
5602 pop_momentary ();
5603 return t;
5604 }
5605
5606 /* Like tsubst, but deals with expressions. This function just replaces
5607 template parms; to finish processing the resultant expression, use
5608 tsubst_expr. */
5609
5610 tree
5611 tsubst_copy (t, args, in_decl)
5612 tree t, args;
5613 tree in_decl;
5614 {
5615 enum tree_code code;
5616
5617 if (t == NULL_TREE || t == error_mark_node)
5618 return t;
5619
5620 code = TREE_CODE (t);
5621
5622 switch (code)
5623 {
5624 case PARM_DECL:
5625 return do_identifier (DECL_NAME (t), 0, NULL_TREE);
5626
5627 case CONST_DECL:
5628 {
5629 tree enum_type;
5630 tree v;
5631
5632 if (!DECL_CONTEXT (t))
5633 /* This is a global enumeration constant. */
5634 return t;
5635
5636 /* Unfortunately, we cannot just call lookup_name here.
5637 Consider:
5638
5639 template <int I> int f() {
5640 enum E { a = I };
5641 struct S { void g() { E e = a; } };
5642 };
5643
5644 When we instantiate f<7>::S::g(), say, lookup_name is not
5645 clever enough to find f<7>::a. */
5646 enum_type
5647 = tsubst_aggr_type (TREE_TYPE (t), args, in_decl,
5648 /*entering_scope=*/0);
5649
5650 for (v = TYPE_VALUES (enum_type);
5651 v != NULL_TREE;
5652 v = TREE_CHAIN (v))
5653 if (TREE_PURPOSE (v) == DECL_NAME (t))
5654 return TREE_VALUE (v);
5655
5656 /* We didn't find the name. That should never happen; if
5657 name-lookup found it during preliminary parsing, we
5658 should find it again here during instantiation. */
5659 my_friendly_abort (0);
5660 }
5661 return t;
5662
5663 case FIELD_DECL:
5664 if (DECL_CONTEXT (t))
5665 {
5666 tree ctx;
5667
5668 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, in_decl,
5669 /*entering_scope=*/1);
5670 if (ctx != DECL_CONTEXT (t))
5671 return lookup_field (ctx, DECL_NAME (t), 0, 0);
5672 }
5673 return t;
5674
5675 case VAR_DECL:
5676 case FUNCTION_DECL:
5677 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
5678 t = tsubst (t, args, in_decl);
5679 mark_used (t);
5680 return t;
5681
5682 case TEMPLATE_DECL:
5683 if (is_member_template (t))
5684 return tsubst (t, args, in_decl);
5685 else
5686 return t;
5687
5688 case LOOKUP_EXPR:
5689 {
5690 /* We must tsbust into a LOOKUP_EXPR in case the names to
5691 which it refers is a conversion operator; in that case the
5692 name will change. We avoid making unnecessary copies,
5693 however. */
5694
5695 tree id = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
5696
5697 if (id != TREE_OPERAND (t, 0))
5698 {
5699 tree r = build_nt (LOOKUP_EXPR, id);
5700 LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
5701 t = r;
5702 }
5703
5704 return t;
5705 }
5706
5707 case CAST_EXPR:
5708 case REINTERPRET_CAST_EXPR:
5709 case CONST_CAST_EXPR:
5710 case STATIC_CAST_EXPR:
5711 case DYNAMIC_CAST_EXPR:
5712 return build1
5713 (code, tsubst (TREE_TYPE (t), args, in_decl),
5714 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
5715
5716 case INDIRECT_REF:
5717 case PREDECREMENT_EXPR:
5718 case PREINCREMENT_EXPR:
5719 case POSTDECREMENT_EXPR:
5720 case POSTINCREMENT_EXPR:
5721 case NEGATE_EXPR:
5722 case TRUTH_NOT_EXPR:
5723 case BIT_NOT_EXPR:
5724 case ADDR_EXPR:
5725 case CONVERT_EXPR: /* Unary + */
5726 case SIZEOF_EXPR:
5727 case ALIGNOF_EXPR:
5728 case ARROW_EXPR:
5729 case THROW_EXPR:
5730 case TYPEID_EXPR:
5731 return build1
5732 (code, NULL_TREE,
5733 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
5734
5735 case PLUS_EXPR:
5736 case MINUS_EXPR:
5737 case MULT_EXPR:
5738 case TRUNC_DIV_EXPR:
5739 case CEIL_DIV_EXPR:
5740 case FLOOR_DIV_EXPR:
5741 case ROUND_DIV_EXPR:
5742 case EXACT_DIV_EXPR:
5743 case BIT_AND_EXPR:
5744 case BIT_ANDTC_EXPR:
5745 case BIT_IOR_EXPR:
5746 case BIT_XOR_EXPR:
5747 case TRUNC_MOD_EXPR:
5748 case FLOOR_MOD_EXPR:
5749 case TRUTH_ANDIF_EXPR:
5750 case TRUTH_ORIF_EXPR:
5751 case TRUTH_AND_EXPR:
5752 case TRUTH_OR_EXPR:
5753 case RSHIFT_EXPR:
5754 case LSHIFT_EXPR:
5755 case RROTATE_EXPR:
5756 case LROTATE_EXPR:
5757 case EQ_EXPR:
5758 case NE_EXPR:
5759 case MAX_EXPR:
5760 case MIN_EXPR:
5761 case LE_EXPR:
5762 case GE_EXPR:
5763 case LT_EXPR:
5764 case GT_EXPR:
5765 case COMPONENT_REF:
5766 case ARRAY_REF:
5767 case COMPOUND_EXPR:
5768 case SCOPE_REF:
5769 case DOTSTAR_EXPR:
5770 case MEMBER_REF:
5771 return build_nt
5772 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
5773 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
5774
5775 case CALL_EXPR:
5776 {
5777 tree fn = TREE_OPERAND (t, 0);
5778 if (is_overloaded_fn (fn))
5779 fn = tsubst_copy (get_first_fn (fn), args, in_decl);
5780 else
5781 /* Sometimes FN is a LOOKUP_EXPR. */
5782 fn = tsubst_copy (fn, args, in_decl);
5783 return build_nt
5784 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
5785 NULL_TREE);
5786 }
5787
5788 case METHOD_CALL_EXPR:
5789 {
5790 tree name = TREE_OPERAND (t, 0);
5791 if (TREE_CODE (name) == BIT_NOT_EXPR)
5792 {
5793 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
5794 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5795 }
5796 else if (TREE_CODE (name) == SCOPE_REF
5797 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
5798 {
5799 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
5800 name = TREE_OPERAND (name, 1);
5801 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
5802 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
5803 name = build_nt (SCOPE_REF, base, name);
5804 }
5805 else
5806 name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
5807 return build_nt
5808 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
5809 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl),
5810 NULL_TREE);
5811 }
5812
5813 case BIND_EXPR:
5814 case COND_EXPR:
5815 case MODOP_EXPR:
5816 {
5817 tree r = build_nt
5818 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
5819 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
5820 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
5821
5822 if (code == BIND_EXPR && !processing_template_decl)
5823 {
5824 /* This processing should really occur in tsubst_expr,
5825 However, tsubst_expr does not recurse into expressions,
5826 since it assumes that there aren't any statements
5827 inside them. Instead, it simply calls
5828 build_expr_from_tree. So, we need to expand the
5829 BIND_EXPR here. */
5830 tree rtl_expr = begin_stmt_expr ();
5831 tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl);
5832 r = finish_stmt_expr (rtl_expr, block);
5833 }
5834
5835 return r;
5836 }
5837
5838 case NEW_EXPR:
5839 {
5840 tree r = build_nt
5841 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
5842 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
5843 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
5844 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
5845 return r;
5846 }
5847
5848 case DELETE_EXPR:
5849 {
5850 tree r = build_nt
5851 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
5852 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
5853 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
5854 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
5855 return r;
5856 }
5857
5858 case TEMPLATE_ID_EXPR:
5859 {
5860 /* Substituted template arguments */
5861 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl);
5862 tree chain;
5863 for (chain = targs; chain; chain = TREE_CHAIN (chain))
5864 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
5865
5866 return lookup_template_function
5867 (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs);
5868 }
5869
5870 case TREE_LIST:
5871 {
5872 tree purpose, value, chain;
5873
5874 if (t == void_list_node)
5875 return t;
5876
5877 purpose = TREE_PURPOSE (t);
5878 if (purpose)
5879 purpose = tsubst_copy (purpose, args, in_decl);
5880 value = TREE_VALUE (t);
5881 if (value)
5882 value = tsubst_copy (value, args, in_decl);
5883 chain = TREE_CHAIN (t);
5884 if (chain && chain != void_type_node)
5885 chain = tsubst_copy (chain, args, in_decl);
5886 if (purpose == TREE_PURPOSE (t)
5887 && value == TREE_VALUE (t)
5888 && chain == TREE_CHAIN (t))
5889 return t;
5890 return tree_cons (purpose, value, chain);
5891 }
5892
5893 case RECORD_TYPE:
5894 case UNION_TYPE:
5895 case ENUMERAL_TYPE:
5896 case INTEGER_TYPE:
5897 case TEMPLATE_TYPE_PARM:
5898 case TEMPLATE_TEMPLATE_PARM:
5899 case TEMPLATE_PARM_INDEX:
5900 case POINTER_TYPE:
5901 case REFERENCE_TYPE:
5902 case OFFSET_TYPE:
5903 case FUNCTION_TYPE:
5904 case METHOD_TYPE:
5905 case ARRAY_TYPE:
5906 case TYPENAME_TYPE:
5907 case TYPE_DECL:
5908 return tsubst (t, args, in_decl);
5909
5910 case IDENTIFIER_NODE:
5911 if (IDENTIFIER_TYPENAME_P (t))
5912 return build_typename_overload
5913 (tsubst (TREE_TYPE (t), args, in_decl));
5914 else
5915 return t;
5916
5917 case CONSTRUCTOR:
5918 return build
5919 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE,
5920 tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl));
5921
5922 default:
5923 return t;
5924 }
5925 }
5926
5927 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
5928
5929 tree
5930 tsubst_expr (t, args, in_decl)
5931 tree t, args;
5932 tree in_decl;
5933 {
5934 if (t == NULL_TREE || t == error_mark_node)
5935 return t;
5936
5937 if (processing_template_decl)
5938 return tsubst_copy (t, args, in_decl);
5939
5940 switch (TREE_CODE (t))
5941 {
5942 case RETURN_STMT:
5943 lineno = TREE_COMPLEXITY (t);
5944 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
5945 args, in_decl));
5946 break;
5947
5948 case EXPR_STMT:
5949 lineno = TREE_COMPLEXITY (t);
5950 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
5951 args, in_decl));
5952 break;
5953
5954 case DECL_STMT:
5955 {
5956 int i = suspend_momentary ();
5957 tree dcl, init;
5958
5959 lineno = TREE_COMPLEXITY (t);
5960 emit_line_note (input_filename, lineno);
5961 dcl = start_decl
5962 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
5963 tsubst (TREE_OPERAND (t, 1), args, in_decl),
5964 TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE);
5965 init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl);
5966 cp_finish_decl
5967 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5968 resume_momentary (i);
5969 return dcl;
5970 }
5971
5972 case FOR_STMT:
5973 {
5974 tree tmp;
5975 lineno = TREE_COMPLEXITY (t);
5976
5977 begin_for_stmt ();
5978 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
5979 tsubst_expr (tmp, args, in_decl);
5980 finish_for_init_stmt (NULL_TREE);
5981 finish_for_cond (tsubst_expr (FOR_COND (t), args,
5982 in_decl),
5983 NULL_TREE);
5984 tmp = tsubst_expr (FOR_EXPR (t), args, in_decl);
5985 finish_for_expr (tmp, NULL_TREE);
5986 tsubst_expr (FOR_BODY (t), args, in_decl);
5987 finish_for_stmt (tmp, NULL_TREE);
5988 }
5989 break;
5990
5991 case WHILE_STMT:
5992 {
5993 lineno = TREE_COMPLEXITY (t);
5994 begin_while_stmt ();
5995 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
5996 args, in_decl),
5997 NULL_TREE);
5998 tsubst_expr (WHILE_BODY (t), args, in_decl);
5999 finish_while_stmt (NULL_TREE);
6000 }
6001 break;
6002
6003 case DO_STMT:
6004 {
6005 lineno = TREE_COMPLEXITY (t);
6006 begin_do_stmt ();
6007 tsubst_expr (DO_BODY (t), args, in_decl);
6008 finish_do_body (NULL_TREE);
6009 finish_do_stmt (tsubst_expr (DO_COND (t), args,
6010 in_decl),
6011 NULL_TREE);
6012 }
6013 break;
6014
6015 case IF_STMT:
6016 {
6017 tree tmp;
6018
6019 lineno = TREE_COMPLEXITY (t);
6020 begin_if_stmt ();
6021 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
6022 args, in_decl),
6023 NULL_TREE);
6024
6025 if (tmp = THEN_CLAUSE (t), tmp)
6026 {
6027 tsubst_expr (tmp, args, in_decl);
6028 finish_then_clause (NULL_TREE);
6029 }
6030
6031 if (tmp = ELSE_CLAUSE (t), tmp)
6032 {
6033 begin_else_clause ();
6034 tsubst_expr (tmp, args, in_decl);
6035 finish_else_clause (NULL_TREE);
6036 }
6037
6038 finish_if_stmt ();
6039 }
6040 break;
6041
6042 case COMPOUND_STMT:
6043 {
6044 tree substmt;
6045
6046 lineno = TREE_COMPLEXITY (t);
6047 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
6048 for (substmt = COMPOUND_BODY (t);
6049 substmt != NULL_TREE;
6050 substmt = TREE_CHAIN (substmt))
6051 tsubst_expr (substmt, args, in_decl);
6052 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
6053 NULL_TREE);
6054 }
6055 break;
6056
6057 case BREAK_STMT:
6058 lineno = TREE_COMPLEXITY (t);
6059 finish_break_stmt ();
6060 break;
6061
6062 case CONTINUE_STMT:
6063 lineno = TREE_COMPLEXITY (t);
6064 finish_continue_stmt ();
6065 break;
6066
6067 case SWITCH_STMT:
6068 {
6069 tree val, tmp;
6070
6071 lineno = TREE_COMPLEXITY (t);
6072 begin_switch_stmt ();
6073 val = tsubst_expr (SWITCH_COND (t), args, in_decl);
6074 finish_switch_cond (val);
6075
6076 if (tmp = TREE_OPERAND (t, 1), tmp)
6077 tsubst_expr (tmp, args, in_decl);
6078
6079 finish_switch_stmt (val, NULL_TREE);
6080 }
6081 break;
6082
6083 case CASE_LABEL:
6084 finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl),
6085 tsubst_expr (CASE_HIGH (t), args, in_decl));
6086 break;
6087
6088 case LABEL_DECL:
6089 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
6090 DECL_NAME (t));
6091 if (t)
6092 expand_label (t);
6093 break;
6094
6095 case GOTO_STMT:
6096 lineno = TREE_COMPLEXITY (t);
6097 t = GOTO_DESTINATION (t);
6098 if (TREE_CODE (t) != IDENTIFIER_NODE)
6099 /* Computed goto's must be tsubst'd into. On the other hand,
6100 non-computed gotos must not be; the identifier in question
6101 will have no binding. */
6102 t = tsubst_expr (t, args, in_decl);
6103 finish_goto_stmt (t);
6104 break;
6105
6106 case ASM_STMT:
6107 lineno = TREE_COMPLEXITY (t);
6108 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl),
6109 tsubst_expr (ASM_STRING (t), args, in_decl),
6110 tsubst_expr (ASM_OUTPUTS (t), args, in_decl),
6111 tsubst_expr (ASM_INPUTS (t), args, in_decl),
6112 tsubst_expr (ASM_CLOBBERS (t), args, in_decl));
6113 break;
6114
6115 case TRY_BLOCK:
6116 lineno = TREE_COMPLEXITY (t);
6117 begin_try_block ();
6118 tsubst_expr (TRY_STMTS (t), args, in_decl);
6119 finish_try_block (NULL_TREE);
6120 {
6121 tree handler = TRY_HANDLERS (t);
6122 for (; handler; handler = TREE_CHAIN (handler))
6123 tsubst_expr (handler, args, in_decl);
6124 }
6125 finish_handler_sequence (NULL_TREE);
6126 break;
6127
6128 case HANDLER:
6129 lineno = TREE_COMPLEXITY (t);
6130 begin_handler ();
6131 if (HANDLER_PARMS (t))
6132 {
6133 tree d = HANDLER_PARMS (t);
6134 expand_start_catch_block
6135 (tsubst (TREE_OPERAND (d, 1), args, in_decl),
6136 tsubst (TREE_OPERAND (d, 0), args, in_decl));
6137 }
6138 else
6139 expand_start_catch_block (NULL_TREE, NULL_TREE);
6140 finish_handler_parms (NULL_TREE);
6141 tsubst_expr (HANDLER_BODY (t), args, in_decl);
6142 finish_handler (NULL_TREE);
6143 break;
6144
6145 case TAG_DEFN:
6146 lineno = TREE_COMPLEXITY (t);
6147 t = TREE_TYPE (t);
6148 if (TREE_CODE (t) == ENUMERAL_TYPE)
6149 tsubst (t, args, NULL_TREE);
6150 break;
6151
6152 default:
6153 return build_expr_from_tree (tsubst_copy (t, args, in_decl));
6154 }
6155 return NULL_TREE;
6156 }
6157
6158 /* Instantiate the indicated variable of function template TMPL with
6159 the template arguments in TARG_PTR. */
6160
6161 tree
6162 instantiate_template (tmpl, targ_ptr)
6163 tree tmpl, targ_ptr;
6164 {
6165 tree fndecl;
6166 tree gen_tmpl;
6167 tree spec;
6168 int i, len;
6169 struct obstack *old_fmp_obstack;
6170 extern struct obstack *function_maybepermanent_obstack;
6171 tree inner_args;
6172
6173 if (tmpl == error_mark_node)
6174 return error_mark_node;
6175
6176 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
6177
6178 /* Check to see if we already have this specialization. */
6179 spec = retrieve_specialization (tmpl, targ_ptr);
6180 if (spec != NULL_TREE)
6181 return spec;
6182
6183 if (DECL_TEMPLATE_INFO (tmpl))
6184 {
6185 /* The TMPL is a partial instantiation. To get a full set of
6186 arguments we must add the arguments used to perform the
6187 partial instantiation. */
6188 targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
6189 targ_ptr);
6190 gen_tmpl = most_general_template (tmpl);
6191
6192 /* Check to see if we already have this specialization. */
6193 spec = retrieve_specialization (gen_tmpl, targ_ptr);
6194 if (spec != NULL_TREE)
6195 return spec;
6196 }
6197 else
6198 gen_tmpl = tmpl;
6199
6200 push_obstacks (&permanent_obstack, &permanent_obstack);
6201 old_fmp_obstack = function_maybepermanent_obstack;
6202 function_maybepermanent_obstack = &permanent_obstack;
6203
6204 len = DECL_NTPARMS (gen_tmpl);
6205 inner_args = innermost_args (targ_ptr);
6206 i = len;
6207 while (i--)
6208 {
6209 tree t = TREE_VEC_ELT (inner_args, i);
6210 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
6211 {
6212 tree nt = target_type (t);
6213 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
6214 {
6215 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
6216 cp_error (" trying to instantiate `%D'", gen_tmpl);
6217 fndecl = error_mark_node;
6218 goto out;
6219 }
6220 }
6221 }
6222 targ_ptr = copy_to_permanent (targ_ptr);
6223
6224 /* substitute template parameters */
6225 fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, gen_tmpl);
6226 /* The DECL_TI_TEMPLATE should always be the immediate parent
6227 template, not the most general template. */
6228 DECL_TI_TEMPLATE (fndecl) = tmpl;
6229
6230 if (flag_external_templates)
6231 add_pending_template (fndecl);
6232
6233 out:
6234 function_maybepermanent_obstack = old_fmp_obstack;
6235 pop_obstacks ();
6236
6237 return fndecl;
6238 }
6239
6240 /* Push the name of the class template into the scope of the instantiation. */
6241
6242 void
6243 overload_template_name (type)
6244 tree type;
6245 {
6246 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
6247 tree decl;
6248
6249 if (IDENTIFIER_CLASS_VALUE (id)
6250 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
6251 return;
6252
6253 decl = build_decl (TYPE_DECL, id, type);
6254 SET_DECL_ARTIFICIAL (decl);
6255 pushdecl_class_level (decl);
6256 }
6257
6258 /* Like type_unification but designed specially to handle conversion
6259 operators.
6260
6261 The FN is a TEMPLATE_DECL for a function. The ARGS are the
6262 arguments that are being used when calling it.
6263
6264 If FN is a conversion operator, RETURN_TYPE is the type desired as
6265 the result of the conversion operator.
6266
6267 The EXTRA_FN_ARG, if any, is the type of an additional
6268 parameter to be added to the beginning of FN's parameter list.
6269
6270 The other arguments are as for type_unification. */
6271
6272 int
6273 fn_type_unification (fn, explicit_targs, targs, args, return_type,
6274 strict, extra_fn_arg)
6275 tree fn, explicit_targs, targs, args, return_type;
6276 unification_kind_t strict;
6277 tree extra_fn_arg;
6278 {
6279 tree parms;
6280
6281 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
6282
6283 parms = TYPE_ARG_TYPES (TREE_TYPE (fn));
6284
6285 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn)))
6286 {
6287 /* This is a template conversion operator. Use the return types
6288 as well as the argument types. */
6289 parms = scratch_tree_cons (NULL_TREE,
6290 TREE_TYPE (TREE_TYPE (fn)),
6291 parms);
6292 args = scratch_tree_cons (NULL_TREE, return_type, args);
6293 }
6294
6295 if (extra_fn_arg != NULL_TREE)
6296 parms = scratch_tree_cons (NULL_TREE, extra_fn_arg, parms);
6297
6298 /* We allow incomplete unification without an error message here
6299 because the standard doesn't seem to explicitly prohibit it. Our
6300 callers must be ready to deal with unification failures in any
6301 event. */
6302 return type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
6303 targs,
6304 parms,
6305 args,
6306 explicit_targs,
6307 strict, 1);
6308 }
6309
6310
6311 /* Type unification.
6312
6313 We have a function template signature with one or more references to
6314 template parameters, and a parameter list we wish to fit to this
6315 template. If possible, produce a list of parameters for the template
6316 which will cause it to fit the supplied parameter list.
6317
6318 Return zero for success, 2 for an incomplete match that doesn't resolve
6319 all the types, and 1 for complete failure. An error message will be
6320 printed only for an incomplete match.
6321
6322 TPARMS[NTPARMS] is an array of template parameter types.
6323
6324 TARGS[NTPARMS] is the array into which the deduced template
6325 parameter values are placed. PARMS is the function template's
6326 signature (using TEMPLATE_PARM_IDX nodes), and ARGS is the argument
6327 list we're trying to match against it.
6328
6329 The EXPLICIT_TARGS are explicit template arguments provided via a
6330 template-id.
6331
6332 The parameter STRICT is one of:
6333
6334 DEDUCE_CALL:
6335 We are deducing arguments for a function call, as in
6336 [temp.deduct.call].
6337
6338 DEDUCE_CONV:
6339 We are deducing arguments for a conversion function, as in
6340 [temp.deduct.conv].
6341
6342 DEDUCE_EXACT:
6343 We are deducing arguments when calculating the partial
6344 ordering between specializations of function or class
6345 templates, as in [temp.func.order] and [temp.class.order],
6346 when doing an explicit instantiation as in [temp.explicit],
6347 when determining an explicit specialization as in
6348 [temp.expl.spec], or when taking the address of a function
6349 template, as in [temp.deduct.funcaddr]. */
6350
6351 int
6352 type_unification (tparms, targs, parms, args, explicit_targs,
6353 strict, allow_incomplete)
6354 tree tparms, targs, parms, args, explicit_targs;
6355 unification_kind_t strict;
6356 int allow_incomplete;
6357 {
6358 int* explicit_mask;
6359 int i;
6360
6361 for (i = 0; i < TREE_VEC_LENGTH (tparms); i++)
6362 TREE_VEC_ELT (targs, i) = NULL_TREE;
6363
6364 if (explicit_targs != NULL_TREE)
6365 {
6366 tree arg_vec;
6367 arg_vec = coerce_template_parms (tparms, explicit_targs, NULL_TREE, 0,
6368 0);
6369
6370 if (arg_vec == error_mark_node)
6371 return 1;
6372
6373 explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs));
6374 bzero ((char *) explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs));
6375
6376 for (i = 0;
6377 i < TREE_VEC_LENGTH (arg_vec)
6378 && TREE_VEC_ELT (arg_vec, i) != NULL_TREE;
6379 ++i)
6380 {
6381 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i);
6382 /* Let unify know that this argument was explicit. */
6383 explicit_mask [i] = 1;
6384 }
6385 }
6386 else
6387 explicit_mask = 0;
6388
6389 return
6390 type_unification_real (tparms, targs, parms, args, 0,
6391 strict, allow_incomplete, explicit_mask);
6392 }
6393
6394 /* Adjust types before performing type deduction, as described in
6395 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
6396 sections are symmetric. PARM is the type of a function parameter
6397 or the return type of the conversion function. ARG is the type of
6398 the argument passed to the call, or the type of the value
6399 intialized with the result of the conversion function. */
6400
6401 void
6402 maybe_adjust_types_for_deduction (strict, parm, arg)
6403 unification_kind_t strict;
6404 tree* parm;
6405 tree* arg;
6406 {
6407 switch (strict)
6408 {
6409 case DEDUCE_CALL:
6410 break;
6411
6412 case DEDUCE_CONV:
6413 {
6414 /* Swap PARM and ARG throughout the remainder of this
6415 function; the handling is precisely symmetric since PARM
6416 will initialize ARG rather than vice versa. */
6417 tree* temp = parm;
6418 parm = arg;
6419 arg = temp;
6420 break;
6421 }
6422
6423 case DEDUCE_EXACT:
6424 /* There is nothing to do in this case. */
6425 return;
6426
6427 default:
6428 my_friendly_abort (0);
6429 }
6430
6431 if (TREE_CODE (*parm) != REFERENCE_TYPE)
6432 {
6433 /* [temp.deduct.call]
6434
6435 If P is not a reference type:
6436
6437 --If A is an array type, the pointer type produced by the
6438 array-to-pointer standard conversion (_conv.array_) is
6439 used in place of A for type deduction; otherwise,
6440
6441 --If A is a function type, the pointer type produced by
6442 the function-to-pointer standard conversion
6443 (_conv.func_) is used in place of A for type deduction;
6444 otherwise,
6445
6446 --If A is a cv-qualified type, the top level
6447 cv-qualifiers of A's type are ignored for type
6448 deduction. */
6449 if (TREE_CODE (*arg) == ARRAY_TYPE)
6450 *arg = build_pointer_type (TREE_TYPE (*arg));
6451 else if (TREE_CODE (*arg) == FUNCTION_TYPE
6452 || TREE_CODE (*arg) == METHOD_TYPE)
6453 *arg = build_pointer_type (*arg);
6454 else
6455 *arg = TYPE_MAIN_VARIANT (*arg);
6456 }
6457
6458 /* [temp.deduct.call]
6459
6460 If P is a cv-qualified type, the top level cv-qualifiers
6461 of P's type are ignored for type deduction. If P is a
6462 reference type, the type referred to by P is used for
6463 type deduction. */
6464 *parm = TYPE_MAIN_VARIANT (*parm);
6465 if (TREE_CODE (*parm) == REFERENCE_TYPE)
6466 *parm = TREE_TYPE (*parm);
6467 }
6468
6469 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
6470 integers, with ones in positions corresponding to arguments in
6471 targs that were provided explicitly, and zeros elsewhere.
6472
6473 If SUBR is 1, we're being called recursively (to unify the
6474 arguments of a function or method parameter of a function
6475 template). */
6476
6477 static int
6478 type_unification_real (tparms, targs, parms, args, subr,
6479 strict, allow_incomplete, explicit_mask)
6480 tree tparms, targs, parms, args;
6481 int subr;
6482 unification_kind_t strict;
6483 int allow_incomplete;
6484 int* explicit_mask;
6485 {
6486 tree parm, arg;
6487 int i;
6488 int ntparms = TREE_VEC_LENGTH (tparms);
6489 int sub_strict;
6490
6491 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
6492 my_friendly_assert (parms == NULL_TREE
6493 || TREE_CODE (parms) == TREE_LIST, 290);
6494 /* ARGS could be NULL (via a call from parse.y to
6495 build_x_function_call). */
6496 if (args)
6497 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
6498 my_friendly_assert (ntparms > 0, 292);
6499
6500 switch (strict)
6501 {
6502 case DEDUCE_CALL:
6503 sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
6504 break;
6505
6506 case DEDUCE_CONV:
6507 sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
6508 break;
6509
6510 case DEDUCE_EXACT:
6511 sub_strict = UNIFY_ALLOW_NONE;
6512 break;
6513
6514 default:
6515 my_friendly_abort (0);
6516 }
6517
6518 while (parms
6519 && parms != void_list_node
6520 && args
6521 && args != void_list_node)
6522 {
6523 parm = TREE_VALUE (parms);
6524 parms = TREE_CHAIN (parms);
6525 arg = TREE_VALUE (args);
6526 args = TREE_CHAIN (args);
6527
6528 if (arg == error_mark_node)
6529 return 1;
6530 if (arg == unknown_type_node)
6531 return 1;
6532
6533 /* Conversions will be performed on a function argument that
6534 corresponds with a function parameter that contains only
6535 non-deducible template parameters and explicitly specified
6536 template parameters. */
6537 if (! uses_template_parms (parm))
6538 {
6539 tree type;
6540
6541 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
6542 type = TREE_TYPE (arg);
6543 else
6544 {
6545 type = arg;
6546 arg = NULL_TREE;
6547 }
6548
6549 if (strict == DEDUCE_EXACT)
6550 {
6551 if (comptypes (parm, type, 1))
6552 continue;
6553 }
6554 else
6555 /* It might work; we shouldn't check now, because we might
6556 get into infinite recursion. Overload resolution will
6557 handle it. */
6558 continue;
6559
6560 return 1;
6561 }
6562
6563 #if 0
6564 if (TREE_CODE (arg) == VAR_DECL)
6565 arg = TREE_TYPE (arg);
6566 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
6567 arg = TREE_TYPE (arg);
6568 #else
6569 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
6570 {
6571 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
6572 if (TREE_CODE (arg) == OVERLOAD
6573 && TREE_CODE (OVL_FUNCTION (arg)) == TEMPLATE_DECL)
6574 {
6575 tree targs;
6576 tree arg_type;
6577
6578 /* Have to back unify here */
6579 arg = OVL_FUNCTION (arg);
6580 targs = make_scratch_vec (DECL_NTPARMS (arg));
6581 arg_type = TREE_TYPE (arg);
6582 maybe_adjust_types_for_deduction (strict, &parm, &arg_type);
6583 parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
6584 arg_type = scratch_tree_cons (NULL_TREE, arg_type, NULL_TREE);
6585 return
6586 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg),
6587 targs, arg_type, parm, NULL_TREE,
6588 DEDUCE_EXACT, allow_incomplete);
6589 }
6590 arg = TREE_TYPE (arg);
6591 }
6592 #endif
6593 if (!subr)
6594 maybe_adjust_types_for_deduction (strict, &parm, &arg);
6595
6596 switch (unify (tparms, targs, parm, arg, sub_strict,
6597 explicit_mask))
6598 {
6599 case 0:
6600 break;
6601 case 1:
6602 return 1;
6603 }
6604 }
6605 /* Fail if we've reached the end of the parm list, and more args
6606 are present, and the parm list isn't variadic. */
6607 if (args && args != void_list_node && parms == void_list_node)
6608 return 1;
6609 /* Fail if parms are left and they don't have default values. */
6610 if (parms
6611 && parms != void_list_node
6612 && TREE_PURPOSE (parms) == NULL_TREE)
6613 return 1;
6614 if (!subr)
6615 for (i = 0; i < ntparms; i++)
6616 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
6617 {
6618 if (!allow_incomplete)
6619 error ("incomplete type unification");
6620 return 2;
6621 }
6622 return 0;
6623 }
6624
6625 /* Returns the level of DECL, which declares a template parameter. */
6626
6627 int
6628 template_decl_level (decl)
6629 tree decl;
6630 {
6631 switch (TREE_CODE (decl))
6632 {
6633 case TYPE_DECL:
6634 case TEMPLATE_DECL:
6635 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
6636
6637 case PARM_DECL:
6638 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
6639
6640 default:
6641 my_friendly_abort (0);
6642 return 0;
6643 }
6644 }
6645
6646 /* Decide whether ARG can be unified with PARM, considering only the
6647 cv-qualifiers of each type, given STRICT as documented for unify.
6648 Returns non-zero iff the unification is OK on that basis.*/
6649
6650 int
6651 check_cv_quals_for_unify (strict, arg, parm)
6652 int strict;
6653 tree arg;
6654 tree parm;
6655 {
6656 return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
6657 && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
6658 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
6659 || (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
6660 && (TYPE_READONLY (arg) > TYPE_READONLY (parm)
6661 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))));
6662 }
6663
6664 /* Takes parameters as for type_unification. Returns 0 if the
6665 type deduction suceeds, 1 otherwise. The parameter STRICT is a
6666 bitwise or of the following flags:
6667
6668 UNIFY_ALLOW_NONE:
6669 Require an exact match between PARM and ARG.
6670 UNIFY_ALLOW_MORE_CV_QUAL:
6671 Allow the deduced ARG to be more cv-qualified than ARG.
6672 UNIFY_ALLOW_LESS_CV_QUAL:
6673 Allow the deduced ARG to be less cv-qualified than ARG.
6674 UNIFY_ALLOW_DERIVED:
6675 Allow the deduced ARG to be a template base class of ARG,
6676 or a pointer to a template base class of the type pointed to by
6677 ARG. */
6678
6679 int
6680 unify (tparms, targs, parm, arg, strict, explicit_mask)
6681 tree tparms, targs, parm, arg;
6682 int strict;
6683 int* explicit_mask;
6684 {
6685 int idx;
6686 tree targ;
6687 tree tparm;
6688
6689 /* I don't think this will do the right thing with respect to types.
6690 But the only case I've seen it in so far has been array bounds, where
6691 signedness is the only information lost, and I think that will be
6692 okay. */
6693 while (TREE_CODE (parm) == NOP_EXPR)
6694 parm = TREE_OPERAND (parm, 0);
6695
6696 if (arg == error_mark_node)
6697 return 1;
6698 if (arg == unknown_type_node)
6699 return 1;
6700 /* If PARM uses template parameters, then we can't bail out here,
6701 even in ARG == PARM, since we won't record unifications for the
6702 template parameters. We might need them if we're trying to
6703 figure out which of two things is more specialized. */
6704 if (arg == parm && !uses_template_parms (parm))
6705 return 0;
6706
6707 /* Immediately reject some pairs that won't unify because of
6708 cv-qualification mismatches. */
6709 if (TREE_CODE (arg) == TREE_CODE (parm)
6710 && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
6711 /* We check the cv-qualifiers when unifying with template type
6712 parameters below. We want to allow ARG `const T' to unify with
6713 PARM `T' for example, when computing which of two templates
6714 is more specialized, for example. */
6715 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
6716 && !check_cv_quals_for_unify (strict, arg, parm))
6717 return 1;
6718
6719 switch (TREE_CODE (parm))
6720 {
6721 case TYPENAME_TYPE:
6722 /* In a type which contains a nested-name-specifier, template
6723 argument values cannot be deduced for template parameters used
6724 within the nested-name-specifier. */
6725 return 0;
6726
6727 case TEMPLATE_TYPE_PARM:
6728 case TEMPLATE_TEMPLATE_PARM:
6729 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
6730
6731 if (TEMPLATE_TYPE_LEVEL (parm)
6732 != template_decl_level (tparm))
6733 /* The PARM is not one we're trying to unify. Just check
6734 to see if it matches ARG. */
6735 return (TREE_CODE (arg) == TREE_CODE (parm)
6736 && comptypes (parm, arg, 1)) ? 0 : 1;
6737 idx = TEMPLATE_TYPE_IDX (parm);
6738 targ = TREE_VEC_ELT (targs, idx);
6739 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
6740
6741 /* Check for mixed types and values. */
6742 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
6743 && TREE_CODE (tparm) != TYPE_DECL)
6744 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
6745 && TREE_CODE (tparm) != TEMPLATE_DECL))
6746 return 1;
6747
6748 if (!strict && targ != NULL_TREE
6749 && explicit_mask && explicit_mask[idx])
6750 /* An explicit template argument. Don't even try to match
6751 here; the overload resolution code will manage check to
6752 see whether the call is legal. */
6753 return 0;
6754
6755 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
6756 {
6757 if (CLASSTYPE_TEMPLATE_INFO (parm))
6758 {
6759 /* We arrive here when PARM does not involve template
6760 specialization. */
6761
6762 /* ARG must be constructed from a template class. */
6763 if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
6764 return 1;
6765
6766 {
6767 tree parmtmpl = CLASSTYPE_TI_TEMPLATE (parm);
6768 tree parmvec = CLASSTYPE_TI_ARGS (parm);
6769 tree argvec = CLASSTYPE_TI_ARGS (arg);
6770 tree argtmplvec
6771 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
6772 int i;
6773
6774 /* The parameter and argument roles have to be switched here
6775 in order to handle default arguments properly. For example,
6776 template<template <class> class TT> void f(TT<int>)
6777 should be able to accept vector<int> which comes from
6778 template <class T, class Allocator = allocator>
6779 class vector. */
6780
6781 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1)
6782 == error_mark_node)
6783 return 1;
6784
6785 /* Deduce arguments T, i from TT<T> or TT<i>. */
6786 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
6787 {
6788 tree t = TREE_VEC_ELT (parmvec, i);
6789 if (TREE_CODE (t) != TEMPLATE_TYPE_PARM
6790 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM
6791 && TREE_CODE (t) != TEMPLATE_PARM_INDEX)
6792 continue;
6793
6794 /* This argument can be deduced. */
6795
6796 if (unify (tparms, targs, t,
6797 TREE_VEC_ELT (argvec, i),
6798 UNIFY_ALLOW_NONE, explicit_mask))
6799 return 1;
6800 }
6801 }
6802 arg = CLASSTYPE_TI_TEMPLATE (arg);
6803 }
6804 }
6805 else
6806 {
6807 /* If PARM is `const T' and ARG is only `int', we don't have
6808 a match unless we are allowing additional qualification.
6809 If ARG is `const int' and PARM is just `T' that's OK;
6810 that binds `const int' to `T'. */
6811 if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL,
6812 arg, parm))
6813 return 1;
6814
6815 /* Consider the case where ARG is `const volatile int' and
6816 PARM is `const T'. Then, T should be `volatile int'. */
6817 arg =
6818 cp_build_type_variant (arg,
6819 TYPE_READONLY (arg) > TYPE_READONLY (parm),
6820 TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm));
6821 }
6822
6823 /* Simple cases: Value already set, does match or doesn't. */
6824 if (targ != NULL_TREE
6825 && (comptypes (targ, arg, 1)
6826 || (explicit_mask && explicit_mask[idx])))
6827 return 0;
6828 else if (targ)
6829 return 1;
6830 TREE_VEC_ELT (targs, idx) = arg;
6831 return 0;
6832
6833 case TEMPLATE_PARM_INDEX:
6834 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
6835
6836 if (TEMPLATE_PARM_LEVEL (parm)
6837 != template_decl_level (tparm))
6838 /* The PARM is not one we're trying to unify. Just check
6839 to see if it matches ARG. */
6840 return (TREE_CODE (arg) == TREE_CODE (parm)
6841 && cp_tree_equal (parm, arg) > 0) ? 0 : 1;
6842
6843 idx = TEMPLATE_PARM_IDX (parm);
6844 targ = TREE_VEC_ELT (targs, idx);
6845
6846 if (targ)
6847 {
6848 int i = (cp_tree_equal (targ, arg) > 0);
6849 if (i == 1)
6850 return 0;
6851 else if (i == 0)
6852 return 1;
6853 else
6854 my_friendly_abort (42);
6855 }
6856
6857 TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg);
6858 return 0;
6859
6860 case POINTER_TYPE:
6861 {
6862 int sub_strict;
6863
6864 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
6865 return (unify (tparms, targs, parm,
6866 TYPE_PTRMEMFUNC_FN_TYPE (arg), strict,
6867 explicit_mask));
6868
6869 if (TREE_CODE (arg) != POINTER_TYPE)
6870 return 1;
6871
6872 /* [temp.deduct.call]
6873
6874 A can be another pointer or pointer to member type that can
6875 be converted to the deduced A via a qualification
6876 conversion (_conv.qual_).
6877
6878 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
6879 This will allow for additional cv-qualification of the
6880 pointed-to types if appropriate. In general, this is a bit
6881 too generous; we are only supposed to allow qualification
6882 conversions and this method will allow an ARG of char** and
6883 a deduced ARG of const char**. However, overload
6884 resolution will subsequently invalidate the candidate, so
6885 this is probably OK. */
6886 sub_strict = strict;
6887
6888 if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE
6889 || TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg)))
6890 /* The derived-to-base conversion only persists through one
6891 level of pointers. */
6892 sub_strict &= ~UNIFY_ALLOW_DERIVED;
6893
6894 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE
6895 (arg), sub_strict, explicit_mask);
6896 }
6897
6898 case REFERENCE_TYPE:
6899 if (TREE_CODE (arg) != REFERENCE_TYPE)
6900 return 1;
6901 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
6902 UNIFY_ALLOW_NONE, explicit_mask);
6903
6904 case ARRAY_TYPE:
6905 if (TREE_CODE (arg) != ARRAY_TYPE)
6906 return 1;
6907 if ((TYPE_DOMAIN (parm) == NULL_TREE)
6908 != (TYPE_DOMAIN (arg) == NULL_TREE))
6909 return 1;
6910 if (TYPE_DOMAIN (parm) != NULL_TREE
6911 && unify (tparms, targs, TYPE_DOMAIN (parm),
6912 TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE, explicit_mask) != 0)
6913 return 1;
6914 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
6915 UNIFY_ALLOW_NONE, explicit_mask);
6916
6917 case REAL_TYPE:
6918 case COMPLEX_TYPE:
6919 case INTEGER_TYPE:
6920 case BOOLEAN_TYPE:
6921 case VOID_TYPE:
6922 if (TREE_CODE (arg) != TREE_CODE (parm))
6923 return 1;
6924
6925 if (TREE_CODE (parm) == INTEGER_TYPE)
6926 {
6927 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
6928 && unify (tparms, targs, TYPE_MIN_VALUE (parm),
6929 TYPE_MIN_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask))
6930 return 1;
6931 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
6932 && unify (tparms, targs, TYPE_MAX_VALUE (parm),
6933 TYPE_MAX_VALUE (arg), UNIFY_ALLOW_NONE, explicit_mask))
6934 return 1;
6935 }
6936 else if (TREE_CODE (parm) == REAL_TYPE
6937 /* We use the TYPE_MAIN_VARIANT since we have already
6938 checked cv-qualification at the top of the
6939 function. */
6940 && !comptypes (TYPE_MAIN_VARIANT (arg),
6941 TYPE_MAIN_VARIANT (parm), 1))
6942 return 1;
6943
6944 /* As far as unification is concerned, this wins. Later checks
6945 will invalidate it if necessary. */
6946 return 0;
6947
6948 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
6949 /* Type INTEGER_CST can come from ordinary constant template args. */
6950 case INTEGER_CST:
6951 while (TREE_CODE (arg) == NOP_EXPR)
6952 arg = TREE_OPERAND (arg, 0);
6953
6954 if (TREE_CODE (arg) != INTEGER_CST)
6955 return 1;
6956 return !tree_int_cst_equal (parm, arg);
6957
6958 case TREE_VEC:
6959 {
6960 int i;
6961 if (TREE_CODE (arg) != TREE_VEC)
6962 return 1;
6963 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
6964 return 1;
6965 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
6966 if (unify (tparms, targs,
6967 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
6968 UNIFY_ALLOW_NONE, explicit_mask))
6969 return 1;
6970 return 0;
6971 }
6972
6973 case RECORD_TYPE:
6974 if (TYPE_PTRMEMFUNC_FLAG (parm))
6975 return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm),
6976 arg, strict, explicit_mask);
6977
6978 if (TREE_CODE (arg) != RECORD_TYPE)
6979 return 1;
6980
6981 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
6982 {
6983 tree t = NULL_TREE;
6984 if (strict & UNIFY_ALLOW_DERIVED)
6985 /* [temp.deduct.call]
6986
6987 If P is a class, and P has the form template-id, then A
6988 can be a derived class of the deduced A. Likewise, if
6989 P is a pointer to a class of the form template-id, A
6990 can be a pointer to a derived class pointed to by the
6991 deduced A. */
6992 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
6993 else if
6994 (CLASSTYPE_TEMPLATE_INFO (arg)
6995 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
6996 t = arg;
6997 if (! t || t == error_mark_node)
6998 return 1;
6999
7000 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
7001 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE,
7002 explicit_mask);
7003 }
7004 else if (!comptypes (TYPE_MAIN_VARIANT (parm),
7005 TYPE_MAIN_VARIANT (arg), 1))
7006 return 1;
7007 return 0;
7008
7009 case METHOD_TYPE:
7010 case FUNCTION_TYPE:
7011 if (TREE_CODE (arg) != TREE_CODE (parm))
7012 return 1;
7013
7014 if (unify (tparms, targs, TREE_TYPE (parm),
7015 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
7016 return 1;
7017 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
7018 TYPE_ARG_TYPES (arg), 1,
7019 DEDUCE_EXACT, 0, explicit_mask);
7020
7021 case OFFSET_TYPE:
7022 if (TREE_CODE (arg) != OFFSET_TYPE)
7023 return 1;
7024 if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
7025 TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE, explicit_mask))
7026 return 1;
7027 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
7028 UNIFY_ALLOW_NONE, explicit_mask);
7029
7030 case CONST_DECL:
7031 if (arg != decl_constant_value (parm))
7032 return 1;
7033 return 0;
7034
7035 case TEMPLATE_DECL:
7036 /* Matched cases are handled by the ARG == PARM test above. */
7037 return 1;
7038
7039 case MINUS_EXPR:
7040 if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
7041 {
7042 /* We handle this case specially, since it comes up with
7043 arrays. In particular, something like:
7044
7045 template <int N> void f(int (&x)[N]);
7046
7047 Here, we are trying to unify the range type, which
7048 looks like [0 ... (N - 1)]. */
7049 tree t, t1, t2;
7050 t1 = TREE_OPERAND (parm, 0);
7051 t2 = TREE_OPERAND (parm, 1);
7052
7053 /* Should this be a regular fold? */
7054 t = maybe_fold_nontype_arg (build (PLUS_EXPR,
7055 integer_type_node,
7056 arg, t2));
7057
7058 return unify (tparms, targs, t1, t, UNIFY_ALLOW_NONE,
7059 explicit_mask);
7060 }
7061 /* else fall through */
7062
7063 default:
7064 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
7065 {
7066 /* We're looking at an expression. This can happen with
7067 something like:
7068
7069 template <int I>
7070 void foo(S<I>, S<I + 2>);
7071
7072 If the call looked like:
7073
7074 foo(S<2>(), S<4>());
7075
7076 we would have already matched `I' with `2'. Now, we'd
7077 like to know if `4' matches `I + 2'. So, we substitute
7078 into that expression, and fold constants, in the hope of
7079 figuring it out. */
7080 tree t =
7081 maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE));
7082 tree a = maybe_fold_nontype_arg (arg);
7083
7084 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (t))))
7085 /* Good, we mangaged to simplify the exression. */
7086 return unify (tparms, targs, t, a, UNIFY_ALLOW_NONE,
7087 explicit_mask);
7088 else
7089 /* Bad, we couldn't simplify this. Assume it doesn't
7090 unify. */
7091 return 1;
7092 }
7093 else
7094 sorry ("use of `%s' in template type unification",
7095 tree_code_name [(int) TREE_CODE (parm)]);
7096
7097 return 1;
7098 }
7099 }
7100 \f
7101 /* Called if RESULT is explicitly instantiated, or is a member of an
7102 explicitly instantiated class, or if using -frepo and the
7103 instantiation of RESULT has been assigned to this file. */
7104
7105 void
7106 mark_decl_instantiated (result, extern_p)
7107 tree result;
7108 int extern_p;
7109 {
7110 if (TREE_CODE (result) != FUNCTION_DECL)
7111 /* The TREE_PUBLIC flag for function declarations will have been
7112 set correctly by tsubst. */
7113 TREE_PUBLIC (result) = 1;
7114
7115 if (! extern_p)
7116 {
7117 DECL_INTERFACE_KNOWN (result) = 1;
7118 DECL_NOT_REALLY_EXTERN (result) = 1;
7119
7120 /* For WIN32 we also want to put explicit instantiations in
7121 linkonce sections. */
7122 if (TREE_PUBLIC (result))
7123 maybe_make_one_only (result);
7124 }
7125 else if (TREE_CODE (result) == FUNCTION_DECL)
7126 mark_inline_for_output (result);
7127 }
7128
7129 /* Given two function templates PAT1 and PAT2, and explicit template
7130 arguments EXPLICIT_ARGS return:
7131
7132 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
7133 -1 if PAT2 is more specialized than PAT1.
7134 0 if neither is more specialized. */
7135
7136 int
7137 more_specialized (pat1, pat2, explicit_args)
7138 tree pat1, pat2, explicit_args;
7139 {
7140 tree targs;
7141 int winner = 0;
7142
7143 targs = get_bindings_overload (pat1, pat2, explicit_args);
7144 if (targs)
7145 --winner;
7146
7147 targs = get_bindings_overload (pat2, pat1, explicit_args);
7148 if (targs)
7149 ++winner;
7150
7151 return winner;
7152 }
7153
7154 /* Given two class template specialization list nodes PAT1 and PAT2, return:
7155
7156 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
7157 -1 if PAT2 is more specialized than PAT1.
7158 0 if neither is more specialized. */
7159
7160 int
7161 more_specialized_class (pat1, pat2)
7162 tree pat1, pat2;
7163 {
7164 tree targs;
7165 int winner = 0;
7166
7167 targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
7168 TREE_PURPOSE (pat2));
7169 if (targs)
7170 --winner;
7171
7172 targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
7173 TREE_PURPOSE (pat1));
7174 if (targs)
7175 ++winner;
7176
7177 return winner;
7178 }
7179
7180 /* Return the template arguments that will produce the function signature
7181 DECL from the function template FN, with the explicit template
7182 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
7183 also match. */
7184
7185 static tree
7186 get_bindings_real (fn, decl, explicit_args, check_rettype)
7187 tree fn, decl, explicit_args;
7188 int check_rettype;
7189 {
7190 int ntparms = DECL_NTPARMS (fn);
7191 tree targs = make_scratch_vec (ntparms);
7192 tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
7193 tree extra_fn_arg = NULL_TREE;
7194 int i;
7195
7196 if (DECL_STATIC_FUNCTION_P (fn)
7197 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
7198 {
7199 /* Sometimes we are trying to figure out what's being
7200 specialized by a declaration that looks like a method, and it
7201 turns out to be a static member function. */
7202 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn))
7203 && !is_member_template (fn))
7204 /* The natural thing to do here seems to be to remove the
7205 spurious `this' parameter from the DECL, but that prevents
7206 unification from making use of the class type. So,
7207 instead, we have fn_type_unification add to the parameters
7208 for FN. */
7209 extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn));
7210 else
7211 /* In this case, though, adding the extra_fn_arg can confuse
7212 things, so we remove from decl_arg_types instead. */
7213 decl_arg_types = TREE_CHAIN (decl_arg_types);
7214 }
7215
7216 i = fn_type_unification (fn, explicit_args, targs,
7217 decl_arg_types,
7218 TREE_TYPE (TREE_TYPE (decl)),
7219 DEDUCE_EXACT,
7220 extra_fn_arg);
7221
7222 if (i != 0)
7223 return NULL_TREE;
7224
7225 if (check_rettype)
7226 {
7227 /* Check to see that the resulting return type is also OK. */
7228 tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs,
7229 NULL_TREE);
7230
7231 if (!comptypes (t, TREE_TYPE (TREE_TYPE (decl)), 1))
7232 return NULL_TREE;
7233 }
7234
7235 return targs;
7236 }
7237
7238 /* For most uses, we want to check the return type. */
7239
7240 tree
7241 get_bindings (fn, decl, explicit_args)
7242 tree fn, decl, explicit_args;
7243 {
7244 return get_bindings_real (fn, decl, explicit_args, 1);
7245 }
7246
7247 /* But for more_specialized, we only care about the parameter types. */
7248
7249 static tree
7250 get_bindings_overload (fn, decl, explicit_args)
7251 tree fn, decl, explicit_args;
7252 {
7253 return get_bindings_real (fn, decl, explicit_args, 0);
7254 }
7255
7256 /* Return the innermost template arguments that, when applied to a
7257 template specialization whose innermost template parameters are
7258 TPARMS, and whose specialization arguments are ARGS, yield the
7259 ARGS.
7260
7261 For example, suppose we have:
7262
7263 template <class T, class U> struct S {};
7264 template <class T> struct S<T*, int> {};
7265
7266 Then, suppose we want to get `S<double*, int>'. The TPARMS will be
7267 {T}, the PARMS will be {T*, int} and the ARGS will be {double*,
7268 int}. The resulting vector will be {double}, indicating that `T'
7269 is bound to `double'. */
7270
7271 static tree
7272 get_class_bindings (tparms, parms, args)
7273 tree tparms, parms, args;
7274 {
7275 int i, ntparms = TREE_VEC_LENGTH (tparms);
7276 tree vec = make_temp_vec (ntparms);
7277
7278 args = innermost_args (args);
7279
7280 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
7281 {
7282 switch (unify (tparms, vec,
7283 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
7284 UNIFY_ALLOW_NONE, 0))
7285 {
7286 case 0:
7287 break;
7288 case 1:
7289 return NULL_TREE;
7290 }
7291 }
7292
7293 for (i = 0; i < ntparms; ++i)
7294 if (! TREE_VEC_ELT (vec, i))
7295 return NULL_TREE;
7296
7297 return vec;
7298 }
7299
7300 /* Return the most specialized of the list of templates in FNS that can
7301 produce an instantiation matching DECL, given the explicit template
7302 arguments EXPLICIT_ARGS. */
7303
7304 tree
7305 most_specialized (fns, decl, explicit_args)
7306 tree fns, decl, explicit_args;
7307 {
7308 tree candidates = NULL_TREE;
7309 tree fn, champ, args;
7310 int fate;
7311
7312 for (fn = fns; fn; fn = TREE_CHAIN (fn))
7313 {
7314 tree candidate = TREE_VALUE (fn);
7315
7316 args = get_bindings (candidate, decl, explicit_args);
7317 if (args)
7318 candidates = scratch_tree_cons (NULL_TREE, candidate,
7319 candidates);
7320 }
7321
7322 if (!candidates)
7323 return NULL_TREE;
7324
7325 champ = TREE_VALUE (candidates);
7326 for (fn = TREE_CHAIN (candidates); fn; fn = TREE_CHAIN (fn))
7327 {
7328 fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
7329 if (fate == 1)
7330 ;
7331 else
7332 {
7333 if (fate == 0)
7334 {
7335 fn = TREE_CHAIN (fn);
7336 if (! fn)
7337 return error_mark_node;
7338 }
7339 champ = TREE_VALUE (fn);
7340 }
7341 }
7342
7343 for (fn = candidates; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
7344 {
7345 fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
7346 if (fate != 1)
7347 return error_mark_node;
7348 }
7349
7350 return champ;
7351 }
7352
7353 /* If DECL is a specialization of some template, return the most
7354 general such template. For example, given:
7355
7356 template <class T> struct S { template <class U> void f(U); };
7357
7358 if TMPL is `template <class U> void S<int>::f(U)' this will return
7359 the full template. This function will not trace past partial
7360 specializations, however. For example, given in addition:
7361
7362 template <class T> struct S<T*> { template <class U> void f(U); };
7363
7364 if TMPL is `template <class U> void S<int*>::f(U)' this will return
7365 `template <class T> template <class U> S<T*>::f(U)'. */
7366
7367 tree
7368 most_general_template (decl)
7369 tree decl;
7370 {
7371 while (DECL_TEMPLATE_INFO (decl))
7372 decl = DECL_TI_TEMPLATE (decl);
7373
7374 return decl;
7375 }
7376
7377 /* Return the most specialized of the class template specializations
7378 of TMPL which can produce an instantiation matching ARGS, or
7379 error_mark_node if the choice is ambiguous. */
7380
7381 tree
7382 most_specialized_class (tmpl, args)
7383 tree tmpl;
7384 tree args;
7385 {
7386 tree list = NULL_TREE;
7387 tree t;
7388 tree champ;
7389 int fate;
7390
7391 tmpl = most_general_template (tmpl);
7392 for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
7393 {
7394 tree spec_args
7395 = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
7396 if (spec_args)
7397 {
7398 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
7399 TREE_TYPE (list) = TREE_TYPE (t);
7400 }
7401 }
7402
7403 if (! list)
7404 return NULL_TREE;
7405
7406 t = list;
7407 champ = t;
7408 t = TREE_CHAIN (t);
7409 for (; t; t = TREE_CHAIN (t))
7410 {
7411 fate = more_specialized_class (champ, t);
7412 if (fate == 1)
7413 ;
7414 else
7415 {
7416 if (fate == 0)
7417 {
7418 t = TREE_CHAIN (t);
7419 if (! t)
7420 return error_mark_node;
7421 }
7422 champ = t;
7423 }
7424 }
7425
7426 for (t = list; t && t != champ; t = TREE_CHAIN (t))
7427 {
7428 fate = more_specialized_class (champ, t);
7429 if (fate != 1)
7430 return error_mark_node;
7431 }
7432
7433 return champ;
7434 }
7435
7436 /* called from the parser. */
7437
7438 void
7439 do_decl_instantiation (declspecs, declarator, storage)
7440 tree declspecs, declarator, storage;
7441 {
7442 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
7443 tree result = NULL_TREE;
7444 int extern_p = 0;
7445
7446 if (! DECL_LANG_SPECIFIC (decl))
7447 {
7448 cp_error ("explicit instantiation of non-template `%#D'", decl);
7449 return;
7450 }
7451 else if (TREE_CODE (decl) == VAR_DECL)
7452 {
7453 /* There is an asymmetry here in the way VAR_DECLs and
7454 FUNCTION_DECLs are handled by grokdeclarator. In the case of
7455 the latter, the DECL we get back will be marked as a
7456 template instantiation, and the appropriate
7457 DECL_TEMPLATE_INFO will be set up. This does not happen for
7458 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
7459 should handle VAR_DECLs as it currently handles
7460 FUNCTION_DECLs. */
7461 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
7462 if (result && TREE_CODE (result) != VAR_DECL)
7463 {
7464 cp_error ("no matching template for `%D' found", result);
7465 return;
7466 }
7467 }
7468 else if (TREE_CODE (decl) != FUNCTION_DECL)
7469 {
7470 cp_error ("explicit instantiation of `%#D'", decl);
7471 return;
7472 }
7473 else
7474 result = decl;
7475
7476 /* Check for various error cases. Note that if the explicit
7477 instantiation is legal the RESULT will currently be marked as an
7478 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
7479 until we get here. */
7480
7481 if (DECL_TEMPLATE_SPECIALIZATION (result))
7482 {
7483 /* [temp.spec]
7484
7485 No program shall both explicitly instantiate and explicitly
7486 specialize a template. */
7487 cp_error ("explicit instantiation of `%#D' after", result);
7488 cp_error_at ("explicit specialization here", result);
7489 return;
7490 }
7491 else if (DECL_EXPLICIT_INSTANTIATION (result))
7492 {
7493 /* [temp.spec]
7494
7495 No program shall explicitly instantiate any template more
7496 than once.
7497
7498 We check DECL_INTERFACE_KNOWN so as not to complain when the
7499 first instantiation was `extern' and the second is not, and
7500 EXTERN_P for the opposite case. */
7501 if (DECL_INTERFACE_KNOWN (result) && !extern_p)
7502 cp_error ("duplicate explicit instantiation of `%#D'", result);
7503
7504 /* If we've already instantiated the template, just return now. */
7505 if (DECL_INTERFACE_KNOWN (result))
7506 return;
7507 }
7508 else if (!DECL_IMPLICIT_INSTANTIATION (result))
7509 {
7510 cp_error ("no matching template for `%D' found", result);
7511 return;
7512 }
7513 else if (!DECL_TEMPLATE_INFO (result))
7514 {
7515 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
7516 return;
7517 }
7518
7519 if (flag_external_templates)
7520 return;
7521
7522 if (storage == NULL_TREE)
7523 ;
7524 else if (storage == ridpointers[(int) RID_EXTERN])
7525 {
7526 if (pedantic)
7527 cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
7528 extern_p = 1;
7529 }
7530 else
7531 cp_error ("storage class `%D' applied to template instantiation",
7532 storage);
7533
7534 SET_DECL_EXPLICIT_INSTANTIATION (result);
7535 mark_decl_instantiated (result, extern_p);
7536 repo_template_instantiated (result, extern_p);
7537 if (! extern_p)
7538 instantiate_decl (result);
7539 }
7540
7541 void
7542 mark_class_instantiated (t, extern_p)
7543 tree t;
7544 int extern_p;
7545 {
7546 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
7547 SET_CLASSTYPE_INTERFACE_KNOWN (t);
7548 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
7549 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
7550 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
7551 if (! extern_p)
7552 {
7553 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
7554 rest_of_type_compilation (t, 1);
7555 }
7556 }
7557
7558 void
7559 do_type_instantiation (t, storage)
7560 tree t, storage;
7561 {
7562 int extern_p = 0;
7563 int nomem_p = 0;
7564 int static_p = 0;
7565
7566 if (TREE_CODE (t) == TYPE_DECL)
7567 t = TREE_TYPE (t);
7568
7569 if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
7570 {
7571 cp_error ("explicit instantiation of non-template type `%T'", t);
7572 return;
7573 }
7574
7575 complete_type (t);
7576
7577 /* With -fexternal-templates, explicit instantiations are treated the same
7578 as implicit ones. */
7579 if (flag_external_templates)
7580 return;
7581
7582 if (TYPE_SIZE (t) == NULL_TREE)
7583 {
7584 cp_error ("explicit instantiation of `%#T' before definition of template",
7585 t);
7586 return;
7587 }
7588
7589 if (storage != NULL_TREE)
7590 {
7591 if (pedantic)
7592 cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
7593 IDENTIFIER_POINTER (storage));
7594
7595 if (storage == ridpointers[(int) RID_INLINE])
7596 nomem_p = 1;
7597 else if (storage == ridpointers[(int) RID_EXTERN])
7598 extern_p = 1;
7599 else if (storage == ridpointers[(int) RID_STATIC])
7600 static_p = 1;
7601 else
7602 {
7603 cp_error ("storage class `%D' applied to template instantiation",
7604 storage);
7605 extern_p = 0;
7606 }
7607 }
7608
7609 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
7610 {
7611 /* [temp.spec]
7612
7613 No program shall both explicitly instantiate and explicitly
7614 specialize a template. */
7615 cp_error ("explicit instantiation of `%#T' after", t);
7616 cp_error_at ("explicit specialization here", t);
7617 return;
7618 }
7619 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
7620 {
7621 /* [temp.spec]
7622
7623 No program shall explicitly instantiate any template more
7624 than once.
7625
7626 If CLASSTYPE_INTERFACE_ONLY, then the first explicit
7627 instantiation was `extern', and if EXTERN_P then the second
7628 is. Both cases are OK. */
7629 if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p)
7630 cp_error ("duplicate explicit instantiation of `%#T'", t);
7631
7632 /* If we've already instantiated the template, just return now. */
7633 if (!CLASSTYPE_INTERFACE_ONLY (t))
7634 return;
7635 }
7636
7637 mark_class_instantiated (t, extern_p);
7638 repo_template_instantiated (t, extern_p);
7639
7640 if (nomem_p)
7641 return;
7642
7643 {
7644 tree tmp;
7645
7646 /* In contrast to implicit instantiation, where only the
7647 declarations, and not the definitions, of members are
7648 instantiated, we have here:
7649
7650 [temp.explicit]
7651
7652 The explicit instantiation of a class template specialization
7653 implies the instantiation of all of its members not
7654 previously explicitly specialized in the translation unit
7655 containing the explicit instantiation.
7656
7657 Of course, we can't instantiate member template classes, since
7658 we don't have any arguments for them. Note that the standard
7659 is unclear on whether the instatiation of the members are
7660 *explicit* instantiations or not. We choose to be generous,
7661 and not set DECL_EXPLICIT_INSTANTIATION. Therefore, we allow
7662 the explicit instantiation of a class where some of the members
7663 have no definition in the current translation unit. */
7664
7665 if (! static_p)
7666 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
7667 if (TREE_CODE (tmp) == FUNCTION_DECL
7668 && DECL_TEMPLATE_INSTANTIATION (tmp))
7669 {
7670 mark_decl_instantiated (tmp, extern_p);
7671 repo_template_instantiated (tmp, extern_p);
7672 if (! extern_p)
7673 instantiate_decl (tmp);
7674 }
7675
7676 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
7677 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
7678 {
7679 mark_decl_instantiated (tmp, extern_p);
7680 repo_template_instantiated (tmp, extern_p);
7681 if (! extern_p)
7682 instantiate_decl (tmp);
7683 }
7684
7685 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
7686 if (IS_AGGR_TYPE (TREE_VALUE (tmp))
7687 && !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
7688 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
7689 }
7690 }
7691
7692 /* Given a function DECL, which is a specialization of TMPL, modify
7693 DECL to be a re-instantiation of TMPL with the same template
7694 arguments. TMPL should be the template into which tsubst'ing
7695 should occur for DECL, not the most general template.
7696
7697 One reason for doing this is a scenario like this:
7698
7699 template <class T>
7700 void f(const T&, int i);
7701
7702 void g() { f(3, 7); }
7703
7704 template <class T>
7705 void f(const T& t, const int i) { }
7706
7707 Note that when the template is first instantiated, with
7708 instantiate_template, the resulting DECL will have no name for the
7709 first parameter, and the wrong type for the second. So, when we go
7710 to instantiate the DECL, we regenerate it. */
7711
7712 void
7713 regenerate_decl_from_template (decl, tmpl)
7714 tree decl;
7715 tree tmpl;
7716 {
7717 tree args;
7718 tree save_ti;
7719 tree code_pattern;
7720 tree new_decl;
7721 tree gen_tmpl;
7722 tree subst_args;
7723 int args_depth;
7724 int parms_depth;
7725 int unregistered;
7726
7727 args = DECL_TI_ARGS (decl);
7728 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
7729
7730 /* Unregister the specialization so that when we tsubst we will not
7731 just return DECL. We don't have to unregister DECL from TMPL
7732 because if would only be registered there if it were a partial
7733 instantiation of a specialization, which it isn't: it's a full
7734 instantiation. */
7735 gen_tmpl = most_general_template (tmpl);
7736 unregistered = unregister_specialization (decl, gen_tmpl);
7737
7738 /* If the DECL was not unregistered then something peculiar is
7739 happening: we created a specialization but did not call
7740 register_specialization for it. */
7741 my_friendly_assert (unregistered, 0);
7742
7743 /* Do the substitution to get the new declaration. Normally, of
7744 course, we want the full set of ARGS. However, one peculiar case
7745 is code like this:
7746
7747 template <class T> struct S {
7748 template <class U> friend void f();
7749 };
7750 template <class U> friend void f() {}
7751 template S<int>;
7752 template void f<double>();
7753
7754 Here, the ARGS for the instantiation of will be {int, double}.
7755 But, we only need as many ARGS as there are levels of template
7756 parameters in CODE_PATTERN. We are careful not to get fooled
7757 into reducing the ARGS in situations like:
7758
7759 template <class T> struct S { template <class U> void f(U); }
7760 template <class T> template <> void S<T>::f(int) {}
7761
7762 which we can spot because the innermost template args for the
7763 CODE_PATTERN don't use any template parameters. */
7764 args_depth = TMPL_ARGS_DEPTH (args);
7765 parms_depth =
7766 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (code_pattern)));
7767 if (args_depth > parms_depth
7768 && !DECL_TEMPLATE_SPECIALIZATION (code_pattern))
7769 {
7770 int i;
7771
7772 subst_args = make_temp_vec (parms_depth);
7773 for (i = 0; i < parms_depth; ++i)
7774 TREE_VEC_ELT (subst_args, i) =
7775 TREE_VEC_ELT (args, i + (args_depth - parms_depth));
7776 }
7777 else
7778 subst_args = args;
7779
7780 new_decl = tsubst (code_pattern, subst_args, NULL_TREE);
7781
7782 if (TREE_CODE (decl) == VAR_DECL)
7783 {
7784 /* Set up DECL_INITIAL, since tsubst doesn't. */
7785 pushclass (DECL_CONTEXT (decl), 2);
7786 DECL_INITIAL (new_decl) =
7787 tsubst_expr (DECL_INITIAL (code_pattern), subst_args,
7788 DECL_TI_TEMPLATE (decl));
7789 popclass (1);
7790 }
7791
7792 if (TREE_CODE (decl) == FUNCTION_DECL)
7793 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
7794 new decl. */
7795 DECL_INITIAL (new_decl) = error_mark_node;
7796
7797 /* The immediate parent of the new template is still whatever it was
7798 before, even though tsubst sets DECL_TI_TEMPLATE up as the most
7799 general template. We also reset the DECL_ASSEMBLER_NAME since
7800 tsubst always calculates the name as if the function in question
7801 were really a template instance, and sometimes, with friend
7802 functions, this is not so. See tsubst_friend_function for
7803 details. */
7804 DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
7805 DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl);
7806 DECL_RTL (new_decl) = DECL_RTL (decl);
7807
7808 /* Call duplicate decls to merge the old and new declarations. */
7809 duplicate_decls (new_decl, decl);
7810
7811 if (TREE_CODE (decl) == FUNCTION_DECL)
7812 DECL_INITIAL (new_decl) = NULL_TREE;
7813
7814 /* Now, re-register the specialization. */
7815 register_specialization (decl, gen_tmpl, args);
7816 }
7817
7818 /* Produce the definition of D, a _DECL generated from a template. */
7819
7820 tree
7821 instantiate_decl (d)
7822 tree d;
7823 {
7824 tree tmpl = DECL_TI_TEMPLATE (d);
7825 tree args = DECL_TI_ARGS (d);
7826 tree td;
7827 tree code_pattern;
7828 tree spec;
7829 tree gen_tmpl;
7830 int nested = in_function_p ();
7831 int pattern_defined;
7832 int line = lineno;
7833 char *file = input_filename;
7834
7835 /* This function should only be used to instantiate templates for
7836 functions and static member variables. */
7837 my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
7838 || TREE_CODE (d) == VAR_DECL, 0);
7839
7840 if ((TREE_CODE (d) == FUNCTION_DECL && DECL_INITIAL (d))
7841 || (TREE_CODE (d) == VAR_DECL && !DECL_IN_AGGR_P (d)))
7842 /* D has already been instantiated. It might seem reasonable to
7843 check whether or not D is an explict instantiation, and, if so,
7844 stop here. But when an explicit instantiation is deferred
7845 until the end of the compilation, DECL_EXPLICIT_INSTANTIATION
7846 is set, even though we still need to do the instantiation. */
7847 return d;
7848
7849 /* If we already have a specialization of this declaration, then
7850 there's no reason to instantiate it. Note that
7851 retrieve_specialization gives us both instantiations and
7852 specializations, so we must explicitly check
7853 DECL_TEMPLATE_SPECIALIZATION. */
7854 gen_tmpl = most_general_template (tmpl);
7855 spec = retrieve_specialization (gen_tmpl, args);
7856 if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
7857 return spec;
7858
7859 /* This needs to happen before any tsubsting. */
7860 if (! push_tinst_level (d))
7861 return d;
7862
7863 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
7864 for the instantiation. This is not always the most general
7865 template. Consider, for example:
7866
7867 template <class T>
7868 struct S { template <class U> void f();
7869 template <> void f<int>(); };
7870
7871 and an instantiation of S<double>::f<int>. We want TD to be the
7872 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
7873 td = tmpl;
7874 for (td = tmpl;
7875 /* An instantiation cannot have a definition, so we need a
7876 more general template. */
7877 DECL_TEMPLATE_INSTANTIATION (td)
7878 /* We must also deal with friend templates. Given:
7879
7880 template <class T> struct S {
7881 template <class U> friend void f() {};
7882 };
7883
7884 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
7885 so far as the language is concerned, but that's still
7886 where we get the pattern for the instantiation from. On
7887 ther hand, if the definition comes outside the class, say:
7888
7889 template <class T> struct S {
7890 template <class U> friend void f();
7891 };
7892 template <class U> friend void f() {}
7893
7894 we don't need to look any further. That's what the check for
7895 DECL_INITIAL is for. */
7896 || (TREE_CODE (d) == FUNCTION_DECL
7897 && DECL_TEMPLATE_INFO (td)
7898 && !DECL_TEMPLATE_SPECIALIZATION (td)
7899 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)));
7900 )
7901 {
7902 /* The present template, TD, should not be a definition. If it
7903 were a definition, we should be using it! Note that we
7904 cannot restructure the loop to just keep going until we find
7905 a template with a definition, since that might go too far if
7906 a specialization was declared, but not defined. */
7907 my_friendly_assert (!(TREE_CODE (d) == FUNCTION_DECL
7908 && DECL_INITIAL (DECL_TEMPLATE_RESULT (td))),
7909 0);
7910 my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
7911 && !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
7912 0);
7913
7914 /* Fetch the more general template. */
7915 td = DECL_TI_TEMPLATE (td);
7916 }
7917
7918 code_pattern = DECL_TEMPLATE_RESULT (td);
7919
7920 if (TREE_CODE (d) == FUNCTION_DECL)
7921 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
7922 else
7923 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
7924
7925 push_to_top_level ();
7926 lineno = DECL_SOURCE_LINE (d);
7927 input_filename = DECL_SOURCE_FILE (d);
7928
7929 if (pattern_defined)
7930 {
7931 repo_template_used (d);
7932
7933 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
7934 {
7935 if (flag_alt_external_templates)
7936 {
7937 if (interface_unknown)
7938 warn_if_unknown_interface (d);
7939 }
7940 else if (DECL_INTERFACE_KNOWN (code_pattern))
7941 {
7942 DECL_INTERFACE_KNOWN (d) = 1;
7943 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
7944 }
7945 else
7946 warn_if_unknown_interface (code_pattern);
7947 }
7948
7949 if (at_eof)
7950 import_export_decl (d);
7951 }
7952
7953 /* Reject all external templates except inline functions. */
7954 if (DECL_INTERFACE_KNOWN (d)
7955 && ! DECL_NOT_REALLY_EXTERN (d)
7956 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
7957 goto out;
7958
7959 if (TREE_CODE (d) == VAR_DECL
7960 && TREE_READONLY (d)
7961 && DECL_INITIAL (d) == NULL_TREE
7962 && DECL_INITIAL (code_pattern) != NULL_TREE)
7963 /* We need to set up DECL_INITIAL regardless of pattern_defined if
7964 the variable is a static const initialized in the class body. */;
7965 else if (! pattern_defined
7966 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
7967 && ! at_eof))
7968 {
7969 /* Defer all templates except inline functions used in another
7970 function. */
7971 lineno = line;
7972 input_filename = file;
7973
7974 if (at_eof && !pattern_defined
7975 && DECL_EXPLICIT_INSTANTIATION (d))
7976 /* [temp.explicit]
7977
7978 The definition of a non-exported function template, a
7979 non-exported member function template, or a non-exported
7980 member function or static data member of a class template
7981 shall be present in every translation unit in which it is
7982 explicitly instantiated. */
7983 cp_error ("explicit instantiation of `%D' but no definition available",
7984 d);
7985
7986 add_pending_template (d);
7987 goto out;
7988 }
7989
7990 regenerate_decl_from_template (d, td);
7991
7992 /* We already set the file and line above. Reset them now in case
7993 they changed as a result of calling regenerate_decl_from_template. */
7994 lineno = DECL_SOURCE_LINE (d);
7995 input_filename = DECL_SOURCE_FILE (d);
7996
7997 if (TREE_CODE (d) == VAR_DECL)
7998 {
7999 DECL_IN_AGGR_P (d) = 0;
8000 if (DECL_INTERFACE_KNOWN (d))
8001 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
8002 else
8003 {
8004 DECL_EXTERNAL (d) = 1;
8005 DECL_NOT_REALLY_EXTERN (d) = 1;
8006 }
8007 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
8008 }
8009 else if (TREE_CODE (d) == FUNCTION_DECL)
8010 {
8011 tree t = DECL_SAVED_TREE (code_pattern);
8012
8013 start_function (NULL_TREE, d, NULL_TREE, 1);
8014 store_parm_decls ();
8015
8016 if (t && TREE_CODE (t) == RETURN_INIT)
8017 {
8018 store_return_init
8019 (TREE_OPERAND (t, 0),
8020 tsubst_expr (TREE_OPERAND (t, 1), args, tmpl));
8021 t = TREE_CHAIN (t);
8022 }
8023
8024 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
8025 {
8026 current_member_init_list
8027 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
8028 current_base_init_list
8029 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
8030 t = TREE_CHAIN (t);
8031 }
8032
8033 setup_vtbl_ptr ();
8034 /* Always keep the BLOCK node associated with the outermost
8035 pair of curly braces of a function. These are needed
8036 for correct operation of dwarfout.c. */
8037 keep_next_level ();
8038
8039 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
8040 tsubst_expr (t, args, tmpl);
8041
8042 finish_function (lineno, 0, nested);
8043 }
8044
8045 out:
8046 lineno = line;
8047 input_filename = file;
8048
8049 pop_from_top_level ();
8050 pop_tinst_level ();
8051
8052 return d;
8053 }
8054
8055 tree
8056 tsubst_chain (t, argvec)
8057 tree t, argvec;
8058 {
8059 if (t)
8060 {
8061 tree first = tsubst (t, argvec, NULL_TREE);
8062 tree last = first;
8063
8064 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
8065 {
8066 tree x = tsubst (t, argvec, NULL_TREE);
8067 TREE_CHAIN (last) = x;
8068 last = x;
8069 }
8070
8071 return first;
8072 }
8073 return NULL_TREE;
8074 }
8075
8076 static tree
8077 tsubst_expr_values (t, argvec)
8078 tree t, argvec;
8079 {
8080 tree first = NULL_TREE;
8081 tree *p = &first;
8082
8083 for (; t; t = TREE_CHAIN (t))
8084 {
8085 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE);
8086 tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE);
8087 *p = build_tree_list (pur, val);
8088 p = &TREE_CHAIN (*p);
8089 }
8090 return first;
8091 }
8092
8093 tree last_tree;
8094
8095 void
8096 add_tree (t)
8097 tree t;
8098 {
8099 last_tree = TREE_CHAIN (last_tree) = t;
8100 }
8101
8102
8103 void
8104 begin_tree ()
8105 {
8106 saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
8107 last_tree = NULL_TREE;
8108 }
8109
8110
8111 void
8112 end_tree ()
8113 {
8114 my_friendly_assert (saved_trees != NULL_TREE, 0);
8115
8116 last_tree = TREE_VALUE (saved_trees);
8117 saved_trees = TREE_CHAIN (saved_trees);
8118 }
8119
8120 /* D is an undefined function declaration in the presence of templates with
8121 the same name, listed in FNS. If one of them can produce D as an
8122 instantiation, remember this so we can instantiate it at EOF if D has
8123 not been defined by that time. */
8124
8125 void
8126 add_maybe_template (d, fns)
8127 tree d, fns;
8128 {
8129 tree t;
8130
8131 if (DECL_MAYBE_TEMPLATE (d))
8132 return;
8133
8134 t = most_specialized (fns, d, NULL_TREE);
8135 if (! t)
8136 return;
8137 if (t == error_mark_node)
8138 {
8139 cp_error ("ambiguous template instantiation for `%D'", d);
8140 return;
8141 }
8142
8143 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
8144 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
8145 DECL_MAYBE_TEMPLATE (d) = 1;
8146 }
8147
8148 /* Instantiate an enumerated type. */
8149
8150 static tree
8151 tsubst_enum (tag, args)
8152 tree tag, args;
8153 {
8154 extern tree current_local_enum;
8155 tree prev_local_enum = current_local_enum;
8156
8157 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
8158 tree e, values = NULL_TREE;
8159
8160 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
8161 {
8162 tree value;
8163 tree elt;
8164
8165 value = TREE_VALUE (e);
8166 if (value)
8167 {
8168 if (TREE_CODE (value) == NOP_EXPR)
8169 /* This is the special case where the value is really a
8170 TEMPLATE_PARM_INDEX. See finish_enum. */
8171 value = TREE_OPERAND (value, 0);
8172 value = tsubst_expr (value, args, NULL_TREE);
8173 }
8174
8175 elt = build_enumerator (TREE_PURPOSE (e), value);
8176 TREE_CHAIN (elt) = values;
8177 values = elt;
8178 }
8179
8180 finish_enum (newtag, values);
8181
8182 current_local_enum = prev_local_enum;
8183
8184 return newtag;
8185 }
8186
8187 /* Set the DECL_ASSEMBLER_NAME for DECL, which is a FUNCTION_DECL that
8188 is either an instantiation or specialization of a template
8189 function. */
8190
8191 static void
8192 set_mangled_name_for_template_decl (decl)
8193 tree decl;
8194 {
8195 tree saved_namespace;
8196 tree context;
8197 tree fn_type;
8198 tree ret_type;
8199 tree parm_types;
8200 tree tparms;
8201 tree targs;
8202 tree tmpl;
8203 int parm_depth;
8204
8205 my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0);
8206 my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0);
8207
8208 /* The names of template functions must be mangled so as to indicate
8209 what template is being specialized with what template arguments.
8210 For example, each of the following three functions must get
8211 different mangled names:
8212
8213 void f(int);
8214 template <> void f<7>(int);
8215 template <> void f<8>(int); */
8216
8217 targs = DECL_TI_ARGS (decl);
8218 if (uses_template_parms (targs))
8219 /* This DECL is for a partial instantiation. There's no need to
8220 mangle the name of such an entity. */
8221 return;
8222
8223 tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
8224 tparms = DECL_TEMPLATE_PARMS (tmpl);
8225 parm_depth = TMPL_PARMS_DEPTH (tparms);
8226
8227 /* There should be as many levels of arguments as there are levels
8228 of parameters. */
8229 my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
8230
8231 /* We now compute the PARMS and RET_TYPE to give to
8232 build_decl_overload_real. The PARMS and RET_TYPE are the
8233 parameter and return types of the template, after all but the
8234 innermost template arguments have been substituted, not the
8235 parameter and return types of the function DECL. For example,
8236 given:
8237
8238 template <class T> T f(T);
8239
8240 both PARMS and RET_TYPE should be `T' even if DECL is `int f(int)'.
8241 A more subtle example is:
8242
8243 template <class T> struct S { template <class U> void f(T, U); }
8244
8245 Here, if DECL is `void S<int>::f(int, double)', PARMS should be
8246 {int, U}. Thus, the args that we want to subsitute into the
8247 return and parameter type for the function are those in TARGS,
8248 with the innermost level omitted. */
8249 fn_type = TREE_TYPE (tmpl);
8250 if (DECL_STATIC_FUNCTION_P (decl))
8251 context = DECL_CLASS_CONTEXT (decl);
8252
8253 if (parm_depth == 1)
8254 /* No substitution is necessary. */
8255 ;
8256 else
8257 {
8258 int i;
8259 tree partial_args;
8260
8261 /* Replace the innermost level of the TARGS with NULL_TREEs to
8262 let tsubst know not to subsitute for those parameters. */
8263 partial_args = make_temp_vec (TREE_VEC_LENGTH (targs));
8264 for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
8265 SET_TMPL_ARGS_LEVEL (partial_args, i,
8266 TMPL_ARGS_LEVEL (targs, i));
8267 SET_TMPL_ARGS_LEVEL (partial_args,
8268 TMPL_ARGS_DEPTH (targs),
8269 make_temp_vec (DECL_NTPARMS (tmpl)));
8270
8271 /* Now, do the (partial) substitution to figure out the
8272 appropriate function type. */
8273 fn_type = tsubst (fn_type, partial_args, NULL_TREE);
8274 if (DECL_STATIC_FUNCTION_P (decl))
8275 context = tsubst (context, partial_args, NULL_TREE);
8276
8277 /* Substitute into the template parameters to obtain the real
8278 innermost set of parameters. This step is important if the
8279 innermost set of template parameters contains value
8280 parameters whose types depend on outer template parameters. */
8281 TREE_VEC_LENGTH (partial_args)--;
8282 tparms = tsubst_template_parms (tparms, partial_args);
8283 }
8284
8285 /* Now, get the innermost parameters and arguments, and figure out
8286 the parameter and return types. */
8287 tparms = INNERMOST_TEMPLATE_PARMS (tparms);
8288 targs = innermost_args (targs);
8289 ret_type = TREE_TYPE (fn_type);
8290 parm_types = TYPE_ARG_TYPES (fn_type);
8291
8292 /* For a static member function, we generate a fake `this' pointer,
8293 for the purposes of mangling. This indicates of which class the
8294 function is a member. Because of:
8295
8296 [class.static]
8297
8298 There shall not be a static and a nonstatic member function
8299 with the same name and the same parameter types
8300
8301 we don't have to worry that this will result in a clash with a
8302 non-static member function. */
8303 if (DECL_STATIC_FUNCTION_P (decl))
8304 parm_types = hash_tree_chain (build_pointer_type (context), parm_types);
8305
8306 /* There should be the same number of template parameters as
8307 template arguments. */
8308 my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs),
8309 0);
8310
8311 /* If the template is in a namespace, we need to put that into the
8312 mangled name. Unfortunately, build_decl_overload_real does not
8313 get the decl to mangle, so it relies on the current
8314 namespace. Therefore, we set that here temporarily. */
8315 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702);
8316 saved_namespace = current_namespace;
8317 current_namespace = CP_DECL_CONTEXT (decl);
8318
8319 /* Actually set the DCL_ASSEMBLER_NAME. */
8320 DECL_ASSEMBLER_NAME (decl)
8321 = build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type,
8322 tparms, targs,
8323 DECL_FUNCTION_MEMBER_P (decl)
8324 + DECL_CONSTRUCTOR_P (decl));
8325
8326 /* Restore the previously active namespace. */
8327 current_namespace = saved_namespace;
8328 }
8329
8330