pt.c (mabybe_get_template_decl_from_type_decl): New function.
[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
42 /* The type of functions taking a tree, and some additional data, and
43 returning an int. */
44 typedef int (*tree_fn_t) PROTO((tree, void*));
45
46 extern struct obstack permanent_obstack;
47
48 extern int lineno;
49 extern char *input_filename;
50 struct pending_inline *pending_template_expansions;
51
52 tree current_template_parms;
53 HOST_WIDE_INT processing_template_decl;
54
55 tree pending_templates;
56 static tree *template_tail = &pending_templates;
57
58 tree maybe_templates;
59 static tree *maybe_template_tail = &maybe_templates;
60
61 int minimal_parse_mode;
62
63 int processing_specialization;
64 int processing_explicit_instantiation;
65 static int template_header_count;
66
67 static tree saved_trees;
68
69 #define obstack_chunk_alloc xmalloc
70 #define obstack_chunk_free free
71
72 static int unify PROTO((tree, tree, int, tree, tree, int, int*));
73 static void add_pending_template PROTO((tree));
74 static int push_tinst_level PROTO((tree));
75 static tree classtype_mangled_name PROTO((tree));
76 static char *mangle_class_name_for_template PROTO((char *, tree, tree, tree));
77 static tree tsubst_expr_values PROTO((tree, tree));
78 static int comp_template_args PROTO((tree, tree));
79 static int list_eq PROTO((tree, tree));
80 static tree get_class_bindings PROTO((tree, tree, tree, tree));
81 static tree coerce_template_parms PROTO((tree, tree, tree, int, int, int));
82 static tree tsubst_enum PROTO((tree, tree, tree *));
83 static tree add_to_template_args PROTO((tree, tree));
84 static int type_unification_real PROTO((tree, tree, tree, tree,
85 int, int, int, int*));
86 static tree complete_template_args PROTO((tree, tree, int));
87 static void note_template_header PROTO((int));
88 static tree maybe_fold_nontype_arg PROTO((tree));
89 static tree convert_nontype_argument PROTO((tree, tree));
90 static tree get_bindings_overload PROTO((tree, tree, tree));
91 static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
92 static tree build_template_parm_index PROTO((int, int, int, tree, tree));
93 static tree original_template PROTO((tree));
94 static int inline_needs_template_parms PROTO((tree));
95 static void push_inline_template_parms_recursive PROTO((tree, int));
96 static tree retrieve_specialization PROTO((tree, tree));
97 static void register_specialization PROTO((tree, tree, tree));
98 static void print_candidates PROTO((tree));
99 static tree reduce_template_parm_level PROTO((tree, tree, int));
100 static tree build_template_decl PROTO((tree, tree));
101 static int mark_template_parm PROTO((tree, void *));
102 static tree tsubst_friend_function PROTO((tree, tree));
103 static tree get_bindings_real PROTO((tree, tree, tree, int));
104 static int template_decl_level PROTO((tree));
105 static tree maybe_get_template_decl_from_type_decl PROTO((tree));
106
107 /* Do any processing required when DECL (a member template declaration
108 using TEMPLATE_PARAMETERS as its innermost parameter list) is
109 finished. Returns the TEMPLATE_DECL corresponding to DECL, unless
110 it is a specialization, in which case the DECL itself is returned. */
111
112 tree
113 finish_member_template_decl (template_parameters, decl)
114 tree template_parameters;
115 tree decl;
116 {
117 if (template_parameters)
118 end_template_decl ();
119 else
120 end_specialization ();
121
122 if (decl == NULL_TREE || decl == void_type_node)
123 return NULL_TREE;
124 else if (TREE_CODE (decl) == TREE_LIST)
125 {
126 /* Assume that the class is the only declspec. */
127 decl = TREE_VALUE (decl);
128 if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl)
129 && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl))
130 {
131 tree tmpl = CLASSTYPE_TI_TEMPLATE (decl);
132 check_member_template (tmpl);
133 return tmpl;
134 }
135 return NULL_TREE;
136 }
137 else if (DECL_TEMPLATE_INFO (decl))
138 {
139 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
140 {
141 check_member_template (DECL_TI_TEMPLATE (decl));
142 return DECL_TI_TEMPLATE (decl);
143 }
144 else
145 return decl;
146 }
147 else
148 cp_error ("invalid member template declaration `%D'", decl);
149
150
151 return error_mark_node;
152 }
153
154 /* Returns the template nesting level of the indicated class TYPE.
155
156 For example, in:
157 template <class T>
158 struct A
159 {
160 template <class U>
161 struct B {};
162 };
163
164 A<T>::B<U> has depth two, while A<T> has depth one. Also,
165 both A<T>::B<int> and A<int>::B<U> have depth one. */
166
167 int
168 template_class_depth (type)
169 tree type;
170 {
171 int depth;
172
173 for (depth = 0; type && TREE_CODE (type) != FUNCTION_DECL;
174 type = TYPE_CONTEXT (type))
175 if (CLASSTYPE_TEMPLATE_INFO (type)
176 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
177 && uses_template_parms (CLASSTYPE_TI_ARGS (type)))
178 ++depth;
179
180 return depth;
181 }
182
183 /* Return the original template for this decl, disregarding any
184 specializations. */
185
186 static tree
187 original_template (decl)
188 tree decl;
189 {
190 while (DECL_TEMPLATE_INFO (decl))
191 decl = DECL_TI_TEMPLATE (decl);
192 return decl;
193 }
194
195 /* Returns 1 if processing DECL as part of do_pending_inlines
196 needs us to push template parms. */
197
198 static int
199 inline_needs_template_parms (decl)
200 tree decl;
201 {
202 if (! DECL_TEMPLATE_INFO (decl))
203 return 0;
204
205 return (list_length (DECL_TEMPLATE_PARMS (original_template (decl)))
206 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
207 }
208
209 /* Subroutine of maybe_begin_member_template_processing.
210 Push the template parms in PARMS, starting from LEVELS steps into the
211 chain, and ending at the beginning, since template parms are listed
212 innermost first. */
213
214 static void
215 push_inline_template_parms_recursive (parmlist, levels)
216 tree parmlist;
217 int levels;
218 {
219 tree parms = TREE_VALUE (parmlist);
220 int i;
221
222 if (levels > 1)
223 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
224
225 ++processing_template_decl;
226 current_template_parms
227 = tree_cons (build_int_2 (0, processing_template_decl),
228 parms, current_template_parms);
229 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
230
231 pushlevel (0);
232 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
233 {
234 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
235 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
236
237 switch (TREE_CODE (parm))
238 {
239 case TYPE_DECL:
240 case TEMPLATE_DECL:
241 pushdecl (parm);
242 break;
243
244 case PARM_DECL:
245 {
246 /* Make a CONST_DECL as is done in process_template_parm. */
247 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
248 TREE_TYPE (parm));
249 DECL_INITIAL (decl) = DECL_INITIAL (parm);
250 pushdecl (decl);
251 }
252 break;
253
254 default:
255 my_friendly_abort (0);
256 }
257 }
258 }
259
260 /* Restore the template parameter context for a member template or
261 a friend template defined in a class definition. */
262
263 void
264 maybe_begin_member_template_processing (decl)
265 tree decl;
266 {
267 tree parms;
268 int levels;
269
270 if (! inline_needs_template_parms (decl))
271 return;
272
273 parms = DECL_TEMPLATE_PARMS (original_template (decl));
274
275 levels = list_length (parms) - processing_template_decl;
276
277 if (DECL_TEMPLATE_SPECIALIZATION (decl))
278 {
279 --levels;
280 parms = TREE_CHAIN (parms);
281 }
282
283 push_inline_template_parms_recursive (parms, levels);
284 }
285
286 /* Undo the effects of begin_member_template_processing. */
287
288 void
289 maybe_end_member_template_processing (decl)
290 tree decl;
291 {
292 if (! processing_template_decl)
293 return;
294
295 while (current_template_parms
296 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
297 {
298 --processing_template_decl;
299 current_template_parms = TREE_CHAIN (current_template_parms);
300 poplevel (0, 0, 0);
301 }
302 }
303
304 /* Returns non-zero iff T is a member template function. We must be
305 careful as in
306
307 template <class T> class C { void f(); }
308
309 Here, f is a template function, and a member, but not a member
310 template. This function does not concern itself with the origin of
311 T, only its present state. So if we have
312
313 template <class T> class C { template <class U> void f(U); }
314
315 then neither C<int>::f<char> nor C<T>::f<double> is considered
316 to be a member template. */
317
318 int
319 is_member_template (t)
320 tree t;
321 {
322 if (TREE_CODE (t) != FUNCTION_DECL
323 && !DECL_FUNCTION_TEMPLATE_P (t))
324 /* Anything that isn't a function or a template function is
325 certainly not a member template. */
326 return 0;
327
328 /* A local class can't have member templates. */
329 if (hack_decl_function_context (t))
330 return 0;
331
332 if ((DECL_FUNCTION_MEMBER_P (t)
333 && !DECL_TEMPLATE_SPECIALIZATION (t))
334 || (TREE_CODE (t) == TEMPLATE_DECL
335 && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
336 {
337 tree tmpl;
338
339 if (DECL_FUNCTION_TEMPLATE_P (t))
340 tmpl = t;
341 else if (DECL_TEMPLATE_INFO (t)
342 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
343 tmpl = DECL_TI_TEMPLATE (t);
344 else
345 tmpl = NULL_TREE;
346
347 if (tmpl
348 /* If there are more levels of template parameters than
349 there are template classes surrounding the declaration,
350 then we have a member template. */
351 && (list_length (DECL_TEMPLATE_PARMS (tmpl)) >
352 template_class_depth (DECL_CLASS_CONTEXT (t))))
353 return 1;
354 }
355
356 return 0;
357 }
358
359 /* Return a new template argument vector which contains all of ARGS
360 for all outer templates TYPE is contained in, but has as its
361 innermost set of arguments the EXTRA_ARGS. If UNBOUND_ONLY, we
362 are only interested in unbound template arguments, not arguments from
363 enclosing templates that have been instantiated already. */
364
365 static tree
366 complete_template_args (tmpl, extra_args, unbound_only)
367 tree tmpl, extra_args;
368 int unbound_only;
369 {
370 /* depth is the number of levels of enclosing args we're adding. */
371 int depth, i;
372 tree args, new_args, spec_args = NULL_TREE;
373
374 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
375 my_friendly_assert (TREE_CODE (extra_args) == TREE_VEC, 0);
376
377 if (DECL_TEMPLATE_INFO (tmpl) && !unbound_only)
378 {
379 /* A specialization of a member template of a template class shows up
380 as a TEMPLATE_DECL with DECL_TEMPLATE_SPECIALIZATION set.
381 DECL_TI_ARGS is the specialization args, and DECL_TI_TEMPLATE
382 is the template being specialized. */
383 if (DECL_TEMPLATE_SPECIALIZATION (tmpl))
384 {
385 spec_args = DECL_TI_ARGS (tmpl);
386 tmpl = DECL_TI_TEMPLATE (tmpl);
387 }
388
389 if (DECL_TEMPLATE_INFO (tmpl))
390 {
391 /* A partial instantiation of a member template shows up as a
392 TEMPLATE_DECL with DECL_TEMPLATE_INFO. DECL_TI_ARGS is
393 all the bound template arguments. */
394 args = DECL_TI_ARGS (tmpl);
395 if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC)
396 depth = 1;
397 else
398 depth = TREE_VEC_LENGTH (args);
399 }
400 else
401 /* If we are a specialization, we might have no previously bound
402 template args. */
403 depth = 0;
404
405 new_args = make_tree_vec (depth + 1 + (!!spec_args));
406
407 if (depth == 1)
408 TREE_VEC_ELT (new_args, 0) = args;
409 else
410 for (i = 0; i < depth; ++i)
411 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
412 }
413 else
414 {
415 tree type;
416 int skip;
417
418 /* For unbound args, we have to do more work. We are getting bindings
419 for the innermost args from extra_args, so we start from our
420 context and work out until we've seen all the args. We need to
421 do it this way to handle partial specialization. */
422
423 depth = list_length (DECL_TEMPLATE_PARMS (tmpl)) - 1;
424 if (depth == 0)
425 return extra_args;
426
427 new_args = make_tree_vec (depth + 1);
428
429 /* If this isn't a member template, extra_args is for the innermost
430 template class, so skip over it. */
431 skip = (! is_member_template (tmpl));
432
433 type = DECL_REAL_CONTEXT (tmpl);
434 for (i = depth; i; type = TYPE_CONTEXT (type))
435 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
436 {
437 if (skip)
438 skip = 0;
439 else
440 {
441 --i;
442 TREE_VEC_ELT (new_args, i) = CLASSTYPE_TI_ARGS (type);
443 }
444 }
445 }
446
447 TREE_VEC_ELT (new_args, depth) = extra_args;
448
449 if (spec_args)
450 TREE_VEC_ELT (new_args, depth + 1) = spec_args;
451
452 return new_args;
453 }
454
455 /* Return a new template argument vector which contains all of ARGS,
456 but has as its innermost set of arguments the EXTRA_ARGS. */
457
458 static tree
459 add_to_template_args (args, extra_args)
460 tree args;
461 tree extra_args;
462 {
463 tree new_args;
464
465 if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC)
466 {
467 new_args = make_tree_vec (2);
468 TREE_VEC_ELT (new_args, 0) = args;
469 }
470 else
471 {
472 int i;
473
474 new_args = make_tree_vec (TREE_VEC_LENGTH (args) + 1);
475
476 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
477 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
478 }
479
480 TREE_VEC_ELT (new_args,
481 TREE_VEC_LENGTH (new_args) - 1) = extra_args;
482
483 return new_args;
484 }
485
486 /* We've got a template header coming up; push to a new level for storing
487 the parms. */
488
489 void
490 begin_template_parm_list ()
491 {
492 pushlevel (0);
493 declare_pseudo_global_level ();
494 ++processing_template_decl;
495 note_template_header (0);
496 }
497
498
499 /* We've just seen template <>. */
500
501 void
502 begin_specialization ()
503 {
504 note_template_header (1);
505 }
506
507
508 /* Called at then end of processing a declaration preceeded by
509 template<>. */
510
511 void
512 end_specialization ()
513 {
514 reset_specialization ();
515 }
516
517
518 /* Any template <>'s that we have seen thus far are not referring to a
519 function specialization. */
520
521 void
522 reset_specialization ()
523 {
524 processing_specialization = 0;
525 template_header_count = 0;
526 }
527
528
529 /* We've just seen a template header. If SPECIALIZATION is non-zero,
530 it was of the form template <>. */
531
532 static void
533 note_template_header (specialization)
534 int specialization;
535 {
536 processing_specialization = specialization;
537 template_header_count++;
538 }
539
540
541 /* We're beginning an explicit instantiation. */
542
543 void
544 begin_explicit_instantiation ()
545 {
546 ++processing_explicit_instantiation;
547 }
548
549
550 void
551 end_explicit_instantiation ()
552 {
553 my_friendly_assert(processing_explicit_instantiation > 0, 0);
554 --processing_explicit_instantiation;
555 }
556
557
558 /* Retrieve the specialization (in the sense of [temp.spec] - a
559 specialization is either an instantiation or an explicit
560 specialization) of TMPL for the given template ARGS. If there is
561 no such specialization, return NULL_TREE. The ARGS are a vector of
562 arguments, or a vector of vectors of arguments, in the case of
563 templates with more than one level of parameters. */
564
565 static tree
566 retrieve_specialization (tmpl, args)
567 tree tmpl;
568 tree args;
569 {
570 tree s;
571
572 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
573
574 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
575 s != NULL_TREE;
576 s = TREE_CHAIN (s))
577 if (comp_template_args (TREE_PURPOSE (s), args))
578 return TREE_VALUE (s);
579
580 return NULL_TREE;
581 }
582
583
584
585 /* Register the specialization SPEC as a specialization of TMPL with
586 the indicated ARGS. */
587
588 static void
589 register_specialization (spec, tmpl, args)
590 tree spec;
591 tree tmpl;
592 tree args;
593 {
594 tree s;
595
596 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
597
598 if (TREE_CODE (spec) != TEMPLATE_DECL
599 && list_length (DECL_TEMPLATE_PARMS (tmpl)) > 1)
600 /* Avoid registering function declarations as
601 specializations of member templates, as would otherwise
602 happen with out-of-class specializations of member
603 templates. */
604 return;
605
606 for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
607 s != NULL_TREE;
608 s = TREE_CHAIN (s))
609 if (comp_template_args (TREE_PURPOSE (s), args))
610 {
611 tree fn = TREE_VALUE (s);
612
613 if (DECL_TEMPLATE_SPECIALIZATION (spec))
614 {
615 if (DECL_TEMPLATE_INSTANTIATION (fn))
616 {
617 if (TREE_USED (fn)
618 || DECL_EXPLICIT_INSTANTIATION (fn))
619 {
620 cp_error ("specialization of %D after instantiation",
621 fn);
622 return;
623 }
624 else
625 {
626 /* This situation should occur only if the first
627 specialization is an implicit instantiation,
628 the second is an explicit specialization, and
629 the implicit instantiation has not yet been
630 used. That situation can occur if we have
631 implicitly instantiated a member function of
632 class type, and then specialized it later. */
633 TREE_VALUE (s) = spec;
634 return;
635 }
636 }
637 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
638 {
639 if (DECL_INITIAL (fn))
640 cp_error ("duplicate specialization of %D", fn);
641
642 TREE_VALUE (s) = spec;
643 return;
644 }
645 }
646 }
647
648 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
649 = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
650 }
651
652
653 /* Print the list of candidate FNS in an error message. */
654
655 static void
656 print_candidates (fns)
657 tree fns;
658 {
659 tree fn;
660
661 char* str = "candidates are:";
662
663 for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
664 {
665 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
666 str = " ";
667 }
668 }
669
670 /* Returns the template (one of the functions given by TEMPLATE_ID)
671 which can be specialized to match the indicated DECL with the
672 explicit template args given in TEMPLATE_ID. If
673 NEED_MEMBER_TEMPLATE is true the function is a specialization of a
674 member template. The template args (those explicitly specified and
675 those deduced) are output in a newly created vector *TARGS_OUT. If
676 it is impossible to determine the result, an error message is
677 issued, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is
678 available. */
679
680 tree
681 determine_specialization (template_id, decl, targs_out,
682 need_member_template,
683 complain)
684 tree template_id;
685 tree decl;
686 tree* targs_out;
687 int need_member_template;
688 int complain;
689 {
690 tree fns = TREE_OPERAND (template_id, 0);
691 tree targs_in = TREE_OPERAND (template_id, 1);
692 tree templates = NULL_TREE;
693 tree fn;
694 int overloaded;
695 int i;
696
697 *targs_out = NULL_TREE;
698
699 if (is_overloaded_fn (fns))
700 fn = get_first_fn (fns);
701 else
702 fn = NULL_TREE;
703
704 overloaded = really_overloaded_fn (fns);
705 for (; fn != NULL_TREE;
706 fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE)
707 {
708 tree tmpl;
709
710 if (!need_member_template
711 && TREE_CODE (fn) == FUNCTION_DECL
712 && DECL_FUNCTION_MEMBER_P (fn)
713 && DECL_USE_TEMPLATE (fn)
714 && DECL_TI_TEMPLATE (fn))
715 /* We can get here when processing something like:
716 template <class T> class X { void f(); }
717 template <> void X<int>::f() {}
718 We're specializing a member function, but not a member
719 template. */
720 tmpl = DECL_TI_TEMPLATE (fn);
721 else if (TREE_CODE (fn) != TEMPLATE_DECL
722 || (need_member_template && !is_member_template (fn)))
723 continue;
724 else
725 tmpl = fn;
726
727 if (list_length (targs_in) > DECL_NTPARMS (tmpl))
728 continue;
729
730 if (decl == NULL_TREE)
731 {
732 tree targs = make_scratch_vec (DECL_NTPARMS (tmpl));
733
734 /* We allow incomplete unification here, because we are going to
735 check all the functions. */
736 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
737 targs,
738 NULL_TREE,
739 NULL_TREE,
740 targs_in,
741 1, 1);
742
743 if (i == 0)
744 /* Unification was successful. */
745 templates = scratch_tree_cons (targs, tmpl, templates);
746 }
747 else
748 templates = scratch_tree_cons (NULL_TREE, tmpl, templates);
749 }
750
751 if (decl != NULL_TREE)
752 {
753 tree tmpl = most_specialized (templates, decl, targs_in);
754
755 if (tmpl == error_mark_node)
756 goto ambiguous;
757 else if (tmpl == NULL_TREE)
758 goto no_match;
759
760 *targs_out = get_bindings (tmpl, decl, targs_in);
761 return tmpl;
762 }
763
764 if (templates == NULL_TREE)
765 {
766 no_match:
767 if (complain)
768 cp_error ("`%D' does not match any template declaration",
769 template_id);
770
771 return NULL_TREE;
772 }
773 else if (TREE_CHAIN (templates) != NULL_TREE)
774 {
775 ambiguous:
776 if (complain)
777 {
778 cp_error ("ambiguous template specialization `%D'",
779 template_id);
780 print_candidates (templates);
781 }
782 return NULL_TREE;
783 }
784
785 /* We have one, and exactly one, match. */
786 *targs_out = TREE_PURPOSE (templates);
787 return TREE_VALUE (templates);
788 }
789
790
791 /* Check to see if the function just declared, as indicated in
792 DECLARATOR, and in DECL, is a specialization of a function
793 template. We may also discover that the declaration is an explicit
794 instantiation at this point.
795
796 Returns DECL, or an equivalent declaration that should be used
797 instead.
798
799 FLAGS is a bitmask consisting of the following flags:
800
801 1: We are being called by finish_struct. (We are unable to
802 determine what template is specialized by an in-class
803 declaration until the class definition is complete, so
804 finish_struct_methods calls this function again later to finish
805 the job.)
806 2: The function has a definition.
807 4: The function is a friend.
808 8: The function is known to be a specialization of a member
809 template.
810
811 The TEMPLATE_COUNT is the number of references to qualifying
812 template classes that appeared in the name of the function. For
813 example, in
814
815 template <class T> struct S { void f(); };
816 void S<int>::f();
817
818 the TEMPLATE_COUNT would be 1. However, explicitly specialized
819 classes are not counted in the TEMPLATE_COUNT, so that in
820
821 template <class T> struct S {};
822 template <> struct S<int> { void f(); }
823 template <>
824 void S<int>::f();
825
826 the TEMPLATE_COUNT would be 0. (Note that this declaration is
827 illegal; there should be no template <>.)
828
829 If the function is a specialization, it is marked as such via
830 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
831 is set up correctly, and it is added to the list of specializations
832 for that template. */
833
834 tree
835 check_explicit_specialization (declarator, decl, template_count, flags)
836 tree declarator;
837 tree decl;
838 int template_count;
839 int flags;
840 {
841 int finish_member = flags & 1;
842 int have_def = flags & 2;
843 int is_friend = flags & 4;
844 int specialization = 0;
845 int explicit_instantiation = 0;
846 int member_specialization = flags & 8;
847
848 tree ctype = DECL_CLASS_CONTEXT (decl);
849 tree dname = DECL_NAME (decl);
850
851 if (!finish_member)
852 {
853 if (processing_specialization)
854 {
855 /* The last template header was of the form template <>. */
856
857 if (template_header_count > template_count)
858 {
859 /* There were more template headers than qualifying template
860 classes. */
861 if (template_header_count - template_count > 1)
862 /* There shouldn't be that many template parameter
863 lists. There can be at most one parameter list for
864 every qualifying class, plus one for the function
865 itself. */
866 cp_error ("too many template parameter lists in declaration of `%D'", decl);
867
868 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
869 if (ctype)
870 member_specialization = 1;
871 else
872 specialization = 1;
873 }
874 else if (template_header_count == template_count)
875 {
876 /* The counts are equal. So, this might be a
877 specialization, but it is not a specialization of a
878 member template. It might be something like
879
880 template <class T> struct S {
881 void f(int i);
882 };
883 template <>
884 void S<int>::f(int i) {} */
885 specialization = 1;
886 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
887 }
888 else
889 {
890 /* This cannot be an explicit specialization. There are not
891 enough headers for all of the qualifying classes. For
892 example, we might have:
893
894 template <>
895 void S<int>::T<char>::f();
896
897 But, we're missing another template <>. */
898 cp_error("too few template parameter lists in declaration of `%D'", decl);
899 return decl;
900 }
901 }
902 else if (processing_explicit_instantiation)
903 {
904 if (template_header_count)
905 cp_error ("template parameter list used in explicit instantiation");
906
907 if (have_def)
908 cp_error ("definition provided for explicit instantiation");
909
910 explicit_instantiation = 1;
911 }
912 else if (ctype != NULL_TREE
913 && !TYPE_BEING_DEFINED (ctype)
914 && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
915 {
916 /* This case catches outdated code that looks like this:
917
918 template <class T> struct S { void f(); };
919 void S<int>::f() {} // Missing template <>
920
921 We disable this check when the type is being defined to
922 avoid complaining about default compiler-generated
923 constructors, destructors, and assignment operators.
924 Since the type is an instantiation, not a specialization,
925 these are the only functions that can be defined before
926 the class is complete. */
927
928 /* If they said
929 template <class T> void S<int>::f() {}
930 that's bogus. */
931 if (template_header_count)
932 {
933 cp_error ("template parameters specified in specialization");
934 return decl;
935 }
936
937 if (pedantic)
938 cp_pedwarn
939 ("explicit specialization not preceded by `template <>'");
940 specialization = 1;
941 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
942 }
943 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
944 {
945 /* This case handles bogus declarations like
946 template <> template <class T>
947 void f<int>(); */
948
949 cp_error ("template-id `%D' in declaration of primary template",
950 declarator);
951 return decl;
952 }
953 }
954
955 if (specialization || member_specialization)
956 {
957 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
958 for (; t; t = TREE_CHAIN (t))
959 if (TREE_PURPOSE (t))
960 {
961 cp_pedwarn
962 ("default argument specified in explicit specialization");
963 break;
964 }
965 }
966
967 if (specialization || member_specialization || explicit_instantiation)
968 {
969 tree tmpl = NULL_TREE;
970 tree targs = NULL_TREE;
971
972 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
973 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
974 {
975 tree fns;
976
977 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
978 0);
979 if (!ctype)
980 fns = IDENTIFIER_NAMESPACE_VALUE (dname);
981 else
982 fns = dname;
983
984 declarator =
985 lookup_template_function (fns, NULL_TREE);
986 }
987
988 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
989 {
990 /* A friend declaration. We can't do much, because we don't
991 know what this resolves to, yet. */
992 my_friendly_assert (is_friend != 0, 0);
993 my_friendly_assert (!explicit_instantiation, 0);
994 SET_DECL_IMPLICIT_INSTANTIATION (decl);
995 return decl;
996 }
997
998 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
999 {
1000 /* Since finish_struct_1 has not been called yet, we
1001 can't call lookup_fnfields. We note that this
1002 template is a specialization, and proceed, letting
1003 finish_struct fix this up later. */
1004 tree ti = perm_tree_cons (NULL_TREE,
1005 TREE_OPERAND (declarator, 1),
1006 NULL_TREE);
1007 TI_PENDING_SPECIALIZATION_FLAG (ti) = 1;
1008 DECL_TEMPLATE_INFO (decl) = ti;
1009 /* This should not be an instantiation; explicit
1010 instantiation directives can only occur at the top
1011 level. */
1012 my_friendly_assert (!explicit_instantiation, 0);
1013 return decl;
1014 }
1015 else if (ctype != NULL_TREE
1016 && (TREE_CODE (TREE_OPERAND (declarator, 0)) ==
1017 IDENTIFIER_NODE))
1018 {
1019 /* Find the list of functions in ctype that have the same
1020 name as the declared function. */
1021 tree name = TREE_OPERAND (declarator, 0);
1022 tree fns;
1023
1024 if (name == constructor_name (ctype)
1025 || name == constructor_name_full (ctype))
1026 {
1027 int is_constructor = DECL_CONSTRUCTOR_P (decl);
1028
1029 if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
1030 : !TYPE_HAS_DESTRUCTOR (ctype))
1031 {
1032 /* From [temp.expl.spec]:
1033
1034 If such an explicit specialization for the member
1035 of a class template names an implicitly-declared
1036 special member function (clause _special_), the
1037 program is ill-formed.
1038
1039 Similar language is found in [temp.explicit]. */
1040 cp_error ("specialization of implicitly-declared special member function");
1041
1042 return decl;
1043 }
1044
1045 name = is_constructor ? ctor_identifier : dtor_identifier;
1046 }
1047
1048 fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
1049
1050 if (fns == NULL_TREE)
1051 {
1052 cp_error ("no member function `%s' declared in `%T'",
1053 IDENTIFIER_POINTER (name),
1054 ctype);
1055 return decl;
1056 }
1057 else
1058 TREE_OPERAND (declarator, 0) = fns;
1059 }
1060
1061 /* Figure out what exactly is being specialized at this point.
1062 Note that for an explicit instantiation, even one for a
1063 member function, we cannot tell apriori whether the the
1064 instantiation is for a member template, or just a member
1065 function of a template class. In particular, even in if the
1066 instantiation is for a member template, the template
1067 arguments could be deduced from the declaration. */
1068 tmpl = determine_specialization (declarator, decl,
1069 &targs,
1070 member_specialization,
1071 1);
1072
1073 if (tmpl)
1074 {
1075 if (explicit_instantiation)
1076 {
1077 decl = instantiate_template (tmpl, targs);
1078 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
1079 /* There doesn't seem to be anything in the draft to
1080 prevent a specialization from being explicitly
1081 instantiated. We're careful not to destroy the
1082 information indicating that this is a
1083 specialization here. */
1084 SET_DECL_EXPLICIT_INSTANTIATION (decl);
1085 return decl;
1086 }
1087 else if (DECL_STATIC_FUNCTION_P (tmpl)
1088 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1089 {
1090 revert_static_member_fn (&decl, 0, 0);
1091 last_function_parms = TREE_CHAIN (last_function_parms);
1092 }
1093
1094 /* Mangle the function name appropriately. Note that we do
1095 not mangle specializations of non-template member
1096 functions of template classes, e.g. with
1097 template <class T> struct S { void f(); }
1098 and given the specialization
1099 template <> void S<int>::f() {}
1100 we do not mangle S<int>::f() here. That's because it's
1101 just an ordinary member function and doesn't need special
1102 treatment. */
1103 if ((is_member_template (tmpl) || ctype == NULL_TREE)
1104 && name_mangling_version >= 1)
1105 {
1106 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
1107
1108 if (ctype
1109 && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE)
1110 arg_types =
1111 hash_tree_chain (build_pointer_type (ctype),
1112 arg_types);
1113
1114 DECL_ASSEMBLER_NAME (decl)
1115 = build_template_decl_overload
1116 (DECL_NAME (decl),
1117 arg_types,
1118 TREE_TYPE (TREE_TYPE (tmpl)),
1119 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
1120 targs, ctype != NULL_TREE);
1121 }
1122
1123 if (is_friend && !have_def)
1124 {
1125 /* This is not really a declaration of a specialization.
1126 It's just the name of an instantiation. But, it's not
1127 a request for an instantiation, either. */
1128 SET_DECL_IMPLICIT_INSTANTIATION (decl);
1129 DECL_TEMPLATE_INFO (decl)
1130 = perm_tree_cons (tmpl, targs, NULL_TREE);
1131 return decl;
1132 }
1133
1134 /* If DECL_TI_TEMPLATE (decl), the decl is an
1135 instantiation of a specialization of a member template.
1136 (In other words, there was a member template, in a
1137 class template. That member template was specialized.
1138 We then instantiated the class, so there is now an
1139 instance of that specialization.)
1140
1141 According to the CD2,
1142
1143 14.7.3.13 [tmpl.expl.spec]
1144
1145 A specialization of a member function template or
1146 member class template of a non-specialized class
1147 template is itself a template.
1148
1149 So, we just leave the template info alone in this case. */
1150 if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
1151 DECL_TEMPLATE_INFO (decl)
1152 = perm_tree_cons (tmpl, targs, NULL_TREE);
1153
1154 register_specialization (decl, tmpl, targs);
1155
1156 return decl;
1157 }
1158 }
1159
1160 return decl;
1161 }
1162
1163
1164 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
1165 parameters. These are represented in the same format used for
1166 DECL_TEMPLATE_PARMS. */
1167
1168 int comp_template_parms (parms1, parms2)
1169 tree parms1;
1170 tree parms2;
1171 {
1172 tree p1;
1173 tree p2;
1174
1175 if (parms1 == parms2)
1176 return 1;
1177
1178 for (p1 = parms1, p2 = parms2;
1179 p1 != NULL_TREE && p2 != NULL_TREE;
1180 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
1181 {
1182 tree t1 = TREE_VALUE (p1);
1183 tree t2 = TREE_VALUE (p2);
1184 int i;
1185
1186 my_friendly_assert (TREE_CODE (t1) == TREE_VEC, 0);
1187 my_friendly_assert (TREE_CODE (t2) == TREE_VEC, 0);
1188
1189 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
1190 return 0;
1191
1192 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
1193 {
1194 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
1195 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
1196
1197 if (TREE_CODE (parm1) != TREE_CODE (parm2))
1198 return 0;
1199
1200 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM)
1201 continue;
1202 else if (!comptypes (TREE_TYPE (parm1),
1203 TREE_TYPE (parm2), 1))
1204 return 0;
1205 }
1206 }
1207
1208 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
1209 /* One set of parameters has more parameters lists than the
1210 other. */
1211 return 0;
1212
1213 return 1;
1214 }
1215
1216
1217 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
1218 ORIG_LEVEL, DECL, and TYPE. */
1219
1220 static tree
1221 build_template_parm_index (index, level, orig_level, decl, type)
1222 int index;
1223 int level;
1224 int orig_level;
1225 tree decl;
1226 tree type;
1227 {
1228 tree t = make_node (TEMPLATE_PARM_INDEX);
1229 TEMPLATE_PARM_IDX (t) = index;
1230 TEMPLATE_PARM_LEVEL (t) = level;
1231 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
1232 TEMPLATE_PARM_DECL (t) = decl;
1233 TREE_TYPE (t) = type;
1234
1235 return t;
1236 }
1237
1238
1239 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
1240 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
1241 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
1242 new one is created. */
1243
1244 static tree
1245 reduce_template_parm_level (index, type, levels)
1246 tree index;
1247 tree type;
1248 int levels;
1249 {
1250 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
1251 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
1252 != TEMPLATE_PARM_LEVEL (index) - levels))
1253 {
1254 tree decl
1255 = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)),
1256 DECL_NAME (TEMPLATE_PARM_DECL (index)),
1257 type);
1258 tree t
1259 = build_template_parm_index (TEMPLATE_PARM_IDX (index),
1260 TEMPLATE_PARM_LEVEL (index) - levels,
1261 TEMPLATE_PARM_ORIG_LEVEL (index),
1262 decl, type);
1263 TEMPLATE_PARM_DESCENDANTS (index) = t;
1264
1265 /* Template template parameters need this. */
1266 DECL_TEMPLATE_PARMS (decl)
1267 = DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
1268 }
1269
1270 return TEMPLATE_PARM_DESCENDANTS (index);
1271 }
1272
1273 /* Process information from new template parameter NEXT and append it to the
1274 LIST being built. */
1275
1276 tree
1277 process_template_parm (list, next)
1278 tree list, next;
1279 {
1280 tree parm;
1281 tree decl = 0;
1282 tree defval;
1283 int is_type, idx;
1284
1285 parm = next;
1286 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
1287 defval = TREE_PURPOSE (parm);
1288 parm = TREE_VALUE (parm);
1289 is_type = TREE_PURPOSE (parm) == class_type_node;
1290
1291 if (list)
1292 {
1293 tree p = TREE_VALUE (tree_last (list));
1294
1295 if (TREE_CODE (p) == TYPE_DECL)
1296 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
1297 else if (TREE_CODE (p) == TEMPLATE_DECL)
1298 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (p)));
1299 else
1300 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
1301 ++idx;
1302 }
1303 else
1304 idx = 0;
1305
1306 if (!is_type)
1307 {
1308 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
1309 /* is a const-param */
1310 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
1311 PARM, 0, NULL_TREE);
1312 /* A template parameter is not modifiable. */
1313 TREE_READONLY (parm) = 1;
1314 if (IS_AGGR_TYPE (TREE_TYPE (parm))
1315 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
1316 {
1317 cp_error ("`%#T' is not a valid type for a template constant parameter",
1318 TREE_TYPE (parm));
1319 if (DECL_NAME (parm) == NULL_TREE)
1320 error (" a template type parameter must begin with `class' or `typename'");
1321 TREE_TYPE (parm) = void_type_node;
1322 }
1323 else if (pedantic
1324 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
1325 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
1326 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
1327 TREE_TYPE (parm));
1328 if (TREE_PERMANENT (parm) == 0)
1329 {
1330 parm = copy_node (parm);
1331 TREE_PERMANENT (parm) = 1;
1332 }
1333 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
1334 DECL_INITIAL (parm) = DECL_INITIAL (decl)
1335 = build_template_parm_index (idx, processing_template_decl,
1336 processing_template_decl,
1337 decl, TREE_TYPE (parm));
1338 }
1339 else
1340 {
1341 tree t;
1342 parm = TREE_VALUE (parm);
1343
1344 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
1345 {
1346 t = make_lang_type (TEMPLATE_TEMPLATE_PARM);
1347 /* This is for distinguishing between real templates and template
1348 template parameters */
1349 TREE_TYPE (parm) = t;
1350 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
1351 decl = parm;
1352 }
1353 else
1354 {
1355 t = make_lang_type (TEMPLATE_TYPE_PARM);
1356 /* parm is either IDENTIFIER_NODE or NULL_TREE */
1357 decl = build_decl (TYPE_DECL, parm, t);
1358 }
1359
1360 CLASSTYPE_GOT_SEMICOLON (t) = 1;
1361 TYPE_NAME (t) = decl;
1362 TYPE_STUB_DECL (t) = decl;
1363 parm = decl;
1364 TEMPLATE_TYPE_PARM_INDEX (t)
1365 = build_template_parm_index (idx, processing_template_decl,
1366 processing_template_decl,
1367 decl, TREE_TYPE (parm));
1368 }
1369 SET_DECL_ARTIFICIAL (decl);
1370 pushdecl (decl);
1371 parm = build_tree_list (defval, parm);
1372 return chainon (list, parm);
1373 }
1374
1375 /* The end of a template parameter list has been reached. Process the
1376 tree list into a parameter vector, converting each parameter into a more
1377 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
1378 as PARM_DECLs. */
1379
1380 tree
1381 end_template_parm_list (parms)
1382 tree parms;
1383 {
1384 int nparms;
1385 tree parm;
1386 tree saved_parmlist = make_tree_vec (list_length (parms));
1387
1388 current_template_parms
1389 = tree_cons (build_int_2 (0, processing_template_decl),
1390 saved_parmlist, current_template_parms);
1391
1392 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
1393 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
1394
1395 return saved_parmlist;
1396 }
1397
1398 /* end_template_decl is called after a template declaration is seen. */
1399
1400 void
1401 end_template_decl ()
1402 {
1403 reset_specialization ();
1404
1405 if (! processing_template_decl)
1406 return;
1407
1408 /* This matches the pushlevel in begin_template_parm_list. */
1409 poplevel (0, 0, 0);
1410
1411 --processing_template_decl;
1412 current_template_parms = TREE_CHAIN (current_template_parms);
1413 (void) get_pending_sizes (); /* Why? */
1414 }
1415
1416 /* Generate a valid set of template args from current_template_parms. */
1417
1418 tree
1419 current_template_args ()
1420 {
1421 tree header = current_template_parms;
1422 int length = list_length (header);
1423 tree args = make_tree_vec (length);
1424 int l = length;
1425
1426 while (header)
1427 {
1428 tree a = copy_node (TREE_VALUE (header));
1429 int i = TREE_VEC_LENGTH (a);
1430 TREE_TYPE (a) = NULL_TREE;
1431 while (i--)
1432 {
1433 tree t = TREE_VEC_ELT (a, i);
1434
1435 /* t will be a list if we are called from within a
1436 begin/end_template_parm_list pair, but a vector directly
1437 if within a begin/end_member_template_processing pair. */
1438 if (TREE_CODE (t) == TREE_LIST)
1439 {
1440 t = TREE_VALUE (t);
1441
1442 if (TREE_CODE (t) == TYPE_DECL
1443 || TREE_CODE (t) == TEMPLATE_DECL)
1444 t = TREE_TYPE (t);
1445 else
1446 t = DECL_INITIAL (t);
1447 }
1448
1449 TREE_VEC_ELT (a, i) = t;
1450 }
1451 TREE_VEC_ELT (args, --l) = a;
1452 header = TREE_CHAIN (header);
1453 }
1454
1455 return args;
1456 }
1457
1458
1459 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
1460 template PARMS. Used by push_template_decl below. */
1461
1462 static tree
1463 build_template_decl (decl, parms)
1464 tree decl;
1465 tree parms;
1466 {
1467 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
1468 DECL_TEMPLATE_PARMS (tmpl) = parms;
1469 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
1470 if (DECL_LANG_SPECIFIC (decl))
1471 {
1472 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
1473 DECL_STATIC_FUNCTION_P (tmpl) =
1474 DECL_STATIC_FUNCTION_P (decl);
1475 }
1476
1477 return tmpl;
1478 }
1479
1480 struct template_parm_data
1481 {
1482 int level;
1483 int* parms;
1484 };
1485
1486 /* Subroutine of push_template_decl used to see if each template
1487 parameter in a partial specialization is used in the explicit
1488 argument list. If T is of the LEVEL given in DATA (which is
1489 treated as a template_parm_data*), then DATA->PARMS is marked
1490 appropriately. */
1491
1492 static int
1493 mark_template_parm (t, data)
1494 tree t;
1495 void* data;
1496 {
1497 int level;
1498 int idx;
1499 struct template_parm_data* tpd = (struct template_parm_data*) data;
1500
1501 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
1502 {
1503 level = TEMPLATE_PARM_LEVEL (t);
1504 idx = TEMPLATE_PARM_IDX (t);
1505 }
1506 else
1507 {
1508 level = TEMPLATE_TYPE_LEVEL (t);
1509 idx = TEMPLATE_TYPE_IDX (t);
1510 }
1511
1512 if (level == tpd->level)
1513 tpd->parms[idx] = 1;
1514
1515 /* Return zero so that for_each_template_parm will continue the
1516 traversal of the tree; we want to mark *every* template parm. */
1517 return 0;
1518 }
1519
1520 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
1521 parameters given by current_template_args, or reuses a
1522 previously existing one, if appropriate. Returns the DECL, or an
1523 equivalent one, if it is replaced via a call to duplicate_decls. */
1524
1525 tree
1526 push_template_decl (decl)
1527 tree decl;
1528 {
1529 tree tmpl;
1530 tree args;
1531 tree info;
1532 tree ctx;
1533 int primary;
1534 int is_friend = (TREE_CODE (decl) == FUNCTION_DECL
1535 && DECL_FRIEND_P (decl));
1536
1537 if (is_friend)
1538 /* For a friend, we want the context of the friend function, not
1539 the type of which it is a friend. */
1540 ctx = DECL_CONTEXT (decl);
1541 else if (DECL_REAL_CONTEXT (decl))
1542 /* In the case of a virtual function, we want the class in which
1543 it is defined. */
1544 ctx = DECL_REAL_CONTEXT (decl);
1545 else
1546 /* Otherwise, if we're currently definining some class, the DECL
1547 is assumed to be a member of the class. */
1548 ctx = current_class_type;
1549
1550 /* For determining whether this is a primary template or not, we're really
1551 interested in the lexical context, not the true context. */
1552 if (is_friend)
1553 info = DECL_CLASS_CONTEXT (decl);
1554 else
1555 info = ctx;
1556
1557 if (info && TREE_CODE (info) == FUNCTION_DECL)
1558 primary = 0;
1559 else if (! info
1560 || (TYPE_BEING_DEFINED (info) && template_header_count
1561 && ! processing_specialization)
1562 || (template_header_count > template_class_depth (info)))
1563 primary = 1;
1564 else
1565 primary = 0;
1566
1567 if (primary)
1568 {
1569 if (current_lang_name == lang_name_c)
1570 cp_error ("template with C linkage");
1571 if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl)))
1572 cp_error ("template class without a name");
1573 }
1574
1575 /* Partial specialization. */
1576 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
1577 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
1578 {
1579 tree type = TREE_TYPE (decl);
1580 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
1581 tree mainargs = CLASSTYPE_TI_ARGS (type);
1582 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
1583
1584 /* We check that each of the template parameters given in the
1585 partial specialization is used in the argument list to the
1586 specialization. For example:
1587
1588 template <class T> struct S;
1589 template <class T> struct S<T*>;
1590
1591 The second declaration is OK because `T*' uses the template
1592 parameter T, whereas
1593
1594 template <class T> struct S<int>;
1595
1596 is no good. Even trickier is:
1597
1598 template <class T>
1599 struct S1
1600 {
1601 template <class U>
1602 struct S2;
1603 template <class U>
1604 struct S2<T>;
1605 };
1606
1607 The S2<T> declaration is actually illegal; it is a
1608 full-specialization. Of course,
1609
1610 template <class U>
1611 struct S2<T (*)(U)>;
1612
1613 or some such would have been OK. */
1614 int i;
1615 struct template_parm_data tpd;
1616 int ntparms = TREE_VEC_LENGTH (TREE_VALUE (current_template_parms));
1617 int did_error_intro = 0;
1618
1619 tpd.level = TREE_INT_CST_HIGH (TREE_PURPOSE (current_template_parms));
1620 tpd.parms = alloca (sizeof (int) * ntparms);
1621 for (i = 0; i < ntparms; ++i)
1622 tpd.parms[i] = 0;
1623 for (i = 0; i < TREE_VEC_LENGTH (mainargs); ++i)
1624 for_each_template_parm (TREE_VEC_ELT (mainargs, i),
1625 &mark_template_parm,
1626 &tpd);
1627 for (i = 0; i < ntparms; ++i)
1628 if (tpd.parms[i] == 0)
1629 {
1630 /* One of the template parms was not used in the
1631 specialization. */
1632 if (!did_error_intro)
1633 {
1634 cp_error ("template parameters not used in partial specialization:");
1635 did_error_intro = 1;
1636 }
1637
1638 cp_error (" `%D'",
1639 TREE_VALUE (TREE_VEC_ELT
1640 (TREE_VALUE (current_template_parms),
1641 i)));
1642 }
1643
1644 for (; spec; spec = TREE_CHAIN (spec))
1645 {
1646 /* purpose: args to main template
1647 value: spec template */
1648 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
1649 return decl;
1650 }
1651
1652 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
1653 = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms),
1654 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
1655 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
1656 return decl;
1657 }
1658
1659 args = current_template_args ();
1660
1661 if (!ctx
1662 || TREE_CODE (ctx) == FUNCTION_DECL
1663 || TYPE_BEING_DEFINED (ctx)
1664 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
1665 {
1666 if (DECL_LANG_SPECIFIC (decl)
1667 && DECL_TEMPLATE_INFO (decl)
1668 && DECL_TI_TEMPLATE (decl))
1669 tmpl = DECL_TI_TEMPLATE (decl);
1670 else
1671 {
1672 tmpl = build_template_decl (decl, current_template_parms);
1673
1674 if (DECL_LANG_SPECIFIC (decl)
1675 && DECL_TEMPLATE_SPECIALIZATION (decl))
1676 {
1677 /* A specialization of a member template of a template
1678 class. */
1679 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
1680 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
1681 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
1682 }
1683 }
1684 }
1685 else
1686 {
1687 tree t;
1688 tree a;
1689
1690 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
1691 cp_error ("must specialize `%#T' before defining member `%#D'",
1692 ctx, decl);
1693 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1694 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
1695 else if (! DECL_TEMPLATE_INFO (decl))
1696 {
1697 cp_error ("template definition of non-template `%#D'", decl);
1698 return decl;
1699 }
1700 else
1701 tmpl = DECL_TI_TEMPLATE (decl);
1702
1703 if (is_member_template (tmpl))
1704 {
1705 if (DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
1706 && DECL_TEMPLATE_SPECIALIZATION (decl))
1707 {
1708 tree new_tmpl;
1709
1710 /* The declaration is a specialization of a member
1711 template, declared outside the class. Therefore, the
1712 innermost template arguments will be NULL, so we
1713 replace them with the arguments determined by the
1714 earlier call to check_explicit_specialization. */
1715 args = DECL_TI_ARGS (decl);
1716
1717 new_tmpl
1718 = build_template_decl (decl, current_template_parms);
1719 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
1720 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
1721 DECL_TI_TEMPLATE (decl) = new_tmpl;
1722 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
1723 DECL_TEMPLATE_INFO (new_tmpl) =
1724 perm_tree_cons (tmpl, args, NULL_TREE);
1725
1726 register_specialization (new_tmpl, tmpl, args);
1727 return decl;
1728 }
1729
1730 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
1731 t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
1732 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
1733 {
1734 cp_error ("got %d template parameters for `%#D'",
1735 TREE_VEC_LENGTH (a), decl);
1736 cp_error (" but %d required", TREE_VEC_LENGTH (t));
1737 }
1738 if (TREE_VEC_LENGTH (args) > 1)
1739 /* Get the template parameters for the enclosing template
1740 class. */
1741 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2);
1742 else
1743 a = NULL_TREE;
1744 }
1745 else
1746 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
1747
1748 t = NULL_TREE;
1749
1750 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
1751 {
1752 /* When processing an inline member template of a
1753 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
1754 if (CLASSTYPE_TI_SPEC_INFO (ctx))
1755 t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
1756 }
1757 else if (CLASSTYPE_TEMPLATE_INFO (ctx))
1758 t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));
1759
1760 /* There should be template arguments if and only if there is a
1761 template class. */
1762 my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0);
1763
1764 if (t != NULL_TREE
1765 && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
1766 {
1767 cp_error ("got %d template parameters for `%#D'",
1768 TREE_VEC_LENGTH (a), decl);
1769 cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
1770 }
1771 }
1772 /* Get the innermost set of template arguments. */
1773 args = innermost_args (args, 0);
1774
1775 DECL_TEMPLATE_RESULT (tmpl) = decl;
1776 TREE_TYPE (tmpl) = TREE_TYPE (decl);
1777
1778 if (! ctx && primary)
1779 /* The check of PRIMARY ensures that we do not try to push a
1780 global template friend declared in a template class; such a
1781 thing may well depend on the template parameters of the class. */
1782 tmpl = pushdecl_top_level (tmpl);
1783
1784 if (primary)
1785 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
1786
1787 info = perm_tree_cons (tmpl, args, NULL_TREE);
1788
1789 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1790 {
1791 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
1792 if (!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
1793 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
1794 }
1795 else if (! DECL_LANG_SPECIFIC (decl))
1796 cp_error ("template declaration of `%#D'", decl);
1797 else
1798 DECL_TEMPLATE_INFO (decl) = info;
1799
1800 return DECL_TEMPLATE_RESULT (tmpl);
1801 }
1802
1803 /* Called when a class template TYPE is redeclared, e.g.:
1804
1805 template <class T> struct S;
1806 template <class T> struct S {}; */
1807
1808 void
1809 redeclare_class_template (type)
1810 tree type;
1811 {
1812 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1813 tree tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
1814 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
1815 int i;
1816
1817 if (!PRIMARY_TEMPLATE_P (tmpl))
1818 /* The type is nested in some template class. Nothing to worry
1819 about here; there are no new template parameters for the nested
1820 type. */
1821 return;
1822
1823 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
1824 {
1825 cp_error_at ("previous declaration `%D'", tmpl);
1826 cp_error ("used %d template parameter%s instead of %d",
1827 TREE_VEC_LENGTH (tmpl_parms),
1828 TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
1829 TREE_VEC_LENGTH (parms));
1830 return;
1831 }
1832
1833 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
1834 {
1835 tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
1836 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1837 tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
1838 tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
1839
1840 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm))
1841 {
1842 cp_error_at ("template parameter `%#D'", tmpl_parm);
1843 cp_error ("redeclared here as `%#D'", parm);
1844 return;
1845 }
1846
1847 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
1848 {
1849 /* We have in [temp.param]:
1850
1851 A template-parameter may not be given default arguments
1852 by two different declarations in the same scope. */
1853 cp_error ("redefinition of default argument for `%#D'", parm);
1854 return;
1855 }
1856
1857 if (parm_default != NULL_TREE)
1858 /* Update the previous template parameters (which are the ones
1859 that will really count) with the new default value. */
1860 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
1861 }
1862 }
1863
1864 /* Attempt to convert the non-type template parameter EXPR to the
1865 indicated TYPE. If the conversion is successful, return the
1866 converted value. If the conversion is unsuccesful, return
1867 NULL_TREE if we issued an error message, or error_mark_node if we
1868 did not. We issue error messages for out-and-out bad template
1869 parameters, but not simply because the conversion failed, since we
1870 might be just trying to do argument deduction. By the time this
1871 function is called, neither TYPE nor EXPR may make use of template
1872 parameters. */
1873
1874 static tree
1875 convert_nontype_argument (type, expr)
1876 tree type;
1877 tree expr;
1878 {
1879 tree expr_type = TREE_TYPE (expr);
1880
1881 /* A template-argument for a non-type, non-template
1882 template-parameter shall be one of:
1883
1884 --an integral constant-expression of integral or enumeration
1885 type; or
1886
1887 --the name of a non-type template-parameter; or
1888
1889 --the name of an object or function with external linkage,
1890 including function templates and function template-ids but
1891 excluding non-static class members, expressed as id-expression;
1892 or
1893
1894 --the address of an object or function with external linkage,
1895 including function templates and function template-ids but
1896 excluding non-static class members, expressed as & id-expression
1897 where the & is optional if the name refers to a function or
1898 array; or
1899
1900 --a pointer to member expressed as described in _expr.unary.op_. */
1901
1902 /* An integral constant-expression can include const variables
1903 or enumerators. */
1904 if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr))
1905 expr = decl_constant_value (expr);
1906
1907 if (is_overloaded_fn (expr))
1908 /* OK for now. We'll check that it has external linkage later.
1909 Check this first since if expr_type is the unknown_type_node
1910 we would otherwise complain below. */
1911 ;
1912 else if (INTEGRAL_TYPE_P (expr_type)
1913 || TYPE_PTRMEM_P (expr_type)
1914 || TYPE_PTRMEMFUNC_P (expr_type)
1915 /* The next two are g++ extensions. */
1916 || TREE_CODE (expr_type) == REAL_TYPE
1917 || TREE_CODE (expr_type) == COMPLEX_TYPE)
1918 {
1919 if (! TREE_CONSTANT (expr))
1920 {
1921 cp_error ("non-constant `%E' cannot be used as template argument",
1922 expr);
1923 return NULL_TREE;
1924 }
1925 }
1926 else if (TYPE_PTR_P (expr_type)
1927 /* If expr is the address of an overloaded function, we
1928 will get the unknown_type_node at this point. */
1929 || expr_type == unknown_type_node)
1930 {
1931 tree referent;
1932 tree e = expr;
1933 STRIP_NOPS (e);
1934
1935 if (TREE_CODE (e) != ADDR_EXPR)
1936 {
1937 bad_argument:
1938 cp_error ("`%E' is not a valid template argument", expr);
1939 error ("it must be %s%s with external linkage",
1940 TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE
1941 ? "a pointer to " : "",
1942 TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE
1943 ? "a function" : "an object");
1944 return NULL_TREE;
1945 }
1946
1947 referent = TREE_OPERAND (e, 0);
1948 STRIP_NOPS (referent);
1949
1950 if (TREE_CODE (referent) == STRING_CST)
1951 {
1952 cp_error ("string literal %E is not a valid template argument",
1953 referent);
1954 error ("because it is the address of an object with static linkage");
1955 return NULL_TREE;
1956 }
1957
1958 if (is_overloaded_fn (referent))
1959 /* We'll check that it has external linkage later. */
1960 ;
1961 else if (TREE_CODE (referent) != VAR_DECL)
1962 goto bad_argument;
1963 else if (!TREE_PUBLIC (referent))
1964 {
1965 cp_error ("address of non-extern `%E' cannot be used as template argument", referent);
1966 return error_mark_node;
1967 }
1968 }
1969 else if (TREE_CODE (expr) == VAR_DECL)
1970 {
1971 if (!TREE_PUBLIC (expr))
1972 goto bad_argument;
1973 }
1974 else
1975 {
1976 cp_error ("object `%E' cannot be used as template argument", expr);
1977 return NULL_TREE;
1978 }
1979
1980 switch (TREE_CODE (type))
1981 {
1982 case INTEGER_TYPE:
1983 case BOOLEAN_TYPE:
1984 case ENUMERAL_TYPE:
1985 /* For a non-type template-parameter of integral or enumeration
1986 type, integral promotions (_conv.prom_) and integral
1987 conversions (_conv.integral_) are applied. */
1988 if (!INTEGRAL_TYPE_P (expr_type))
1989 return error_mark_node;
1990
1991 /* It's safe to call digest_init in this case; we know we're
1992 just converting one integral constant expression to another. */
1993 return digest_init (type, expr, (tree*) 0);
1994
1995 case REAL_TYPE:
1996 case COMPLEX_TYPE:
1997 /* These are g++ extensions. */
1998 if (TREE_CODE (expr_type) != TREE_CODE (type))
1999 return error_mark_node;
2000
2001 return digest_init (type, expr, (tree*) 0);
2002
2003 case POINTER_TYPE:
2004 {
2005 tree type_pointed_to = TREE_TYPE (type);
2006
2007 if (TYPE_PTRMEM_P (type))
2008 /* For a non-type template-parameter of type pointer to data
2009 member, qualification conversions (_conv.qual_) are
2010 applied. */
2011 return perform_qualification_conversions (type, expr);
2012 else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
2013 {
2014 /* For a non-type template-parameter of type pointer to
2015 function, only the function-to-pointer conversion
2016 (_conv.func_) is applied. If the template-argument
2017 represents a set of overloaded functions (or a pointer to
2018 such), the matching function is selected from the set
2019 (_over.over_). */
2020 tree fns;
2021 tree fn;
2022
2023 if (TREE_CODE (expr) == ADDR_EXPR)
2024 fns = TREE_OPERAND (expr, 0);
2025 else
2026 fns = expr;
2027
2028 fn = instantiate_type (type_pointed_to, fns, 0);
2029
2030 if (fn == error_mark_node)
2031 return error_mark_node;
2032
2033 if (!TREE_PUBLIC (fn))
2034 {
2035 if (really_overloaded_fn (fns))
2036 return error_mark_node;
2037 else
2038 goto bad_argument;
2039 }
2040
2041 expr = build_unary_op (ADDR_EXPR, fn, 0);
2042
2043 my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1),
2044 0);
2045 return expr;
2046 }
2047 else
2048 {
2049 /* For a non-type template-parameter of type pointer to
2050 object, qualification conversions (_conv.qual_) and the
2051 array-to-pointer conversion (_conv.array_) are applied.
2052 [Note: In particular, neither the null pointer conversion
2053 (_conv.ptr_) nor the derived-to-base conversion
2054 (_conv.ptr_) are applied. Although 0 is a valid
2055 template-argument for a non-type template-parameter of
2056 integral type, it is not a valid template-argument for a
2057 non-type template-parameter of pointer type.]
2058
2059 The call to decay_conversion performs the
2060 array-to-pointer conversion, if appropriate. */
2061 expr = decay_conversion (expr);
2062
2063 if (expr == error_mark_node)
2064 return error_mark_node;
2065 else
2066 return perform_qualification_conversions (type, expr);
2067 }
2068 }
2069 break;
2070
2071 case REFERENCE_TYPE:
2072 {
2073 tree type_referred_to = TREE_TYPE (type);
2074
2075 if (TREE_CODE (type_referred_to) == FUNCTION_TYPE)
2076 {
2077 /* For a non-type template-parameter of type reference to
2078 function, no conversions apply. If the
2079 template-argument represents a set of overloaded
2080 functions, the matching function is selected from the
2081 set (_over.over_). */
2082 tree fns = expr;
2083 tree fn;
2084
2085 fn = instantiate_type (type_referred_to, fns, 0);
2086
2087 if (!TREE_PUBLIC (fn))
2088 {
2089 if (really_overloaded_fn (fns))
2090 /* Don't issue an error here; we might get a different
2091 function if the overloading had worked out
2092 differently. */
2093 return error_mark_node;
2094 else
2095 goto bad_argument;
2096 }
2097
2098 if (fn == error_mark_node)
2099 return error_mark_node;
2100
2101 my_friendly_assert (comptypes (type, TREE_TYPE (fn), 1),
2102 0);
2103
2104 return fn;
2105 }
2106 else
2107 {
2108 /* For a non-type template-parameter of type reference to
2109 object, no conversions apply. The type referred to by the
2110 reference may be more cv-qualified than the (otherwise
2111 identical) type of the template-argument. The
2112 template-parameter is bound directly to the
2113 template-argument, which must be an lvalue. */
2114 if (!comptypes (TYPE_MAIN_VARIANT (expr_type),
2115 TYPE_MAIN_VARIANT (type), 1)
2116 || (TYPE_READONLY (expr_type) >
2117 TYPE_READONLY (type_referred_to))
2118 || (TYPE_VOLATILE (expr_type) >
2119 TYPE_VOLATILE (type_referred_to))
2120 || !real_lvalue_p (expr))
2121 return error_mark_node;
2122 else
2123 return expr;
2124 }
2125 }
2126 break;
2127
2128 case RECORD_TYPE:
2129 {
2130 tree fns;
2131 tree fn;
2132
2133 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 0);
2134
2135 /* For a non-type template-parameter of type pointer to member
2136 function, no conversions apply. If the template-argument
2137 represents a set of overloaded member functions, the
2138 matching member function is selected from the set
2139 (_over.over_). */
2140
2141 if (!TYPE_PTRMEMFUNC_P (expr_type) &&
2142 expr_type != unknown_type_node)
2143 return error_mark_node;
2144
2145 if (TREE_CODE (expr) == CONSTRUCTOR)
2146 {
2147 /* A ptr-to-member constant. */
2148 if (!comptypes (type, expr_type, 1))
2149 return error_mark_node;
2150 else
2151 return expr;
2152 }
2153
2154 if (TREE_CODE (expr) != ADDR_EXPR)
2155 return error_mark_node;
2156
2157 fns = TREE_OPERAND (expr, 0);
2158
2159 fn = instantiate_type (TREE_TYPE (TREE_TYPE (type)),
2160 fns, 0);
2161
2162 if (fn == error_mark_node)
2163 return error_mark_node;
2164
2165 expr = build_unary_op (ADDR_EXPR, fn, 0);
2166
2167 my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1),
2168 0);
2169 return expr;
2170 }
2171 break;
2172
2173 default:
2174 /* All non-type parameters must have one of these types. */
2175 my_friendly_abort (0);
2176 break;
2177 }
2178
2179 return error_mark_node;
2180 }
2181
2182 /* Convert all template arguments to their appropriate types, and return
2183 a vector containing the resulting values. If any error occurs, return
2184 error_mark_node, and, if COMPLAIN is non-zero, issue an error message.
2185 Some error messages are issued even if COMPLAIN is zero; for
2186 instance, if a template argument is composed from a local class.
2187
2188 If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be
2189 provided in ARGLIST, or else trailing parameters must have default
2190 values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument
2191 deduction for any unspecified trailing arguments.
2192
2193 If IS_TMPL_PARM is non-zero, we will coercing parameters of template
2194 template arguments. In this case, ARGLIST is a chain of TREE_LIST
2195 nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. */
2196
2197 static tree
2198 coerce_template_parms (parms, arglist, in_decl,
2199 complain,
2200 require_all_arguments,
2201 is_tmpl_parm)
2202 tree parms, arglist;
2203 tree in_decl;
2204 int complain;
2205 int require_all_arguments;
2206 int is_tmpl_parm;
2207 {
2208 int nparms, nargs, i, lost = 0;
2209 tree vec = NULL_TREE;
2210
2211 if (arglist == NULL_TREE)
2212 nargs = 0;
2213 else if (TREE_CODE (arglist) == TREE_VEC)
2214 nargs = TREE_VEC_LENGTH (arglist);
2215 else
2216 nargs = list_length (arglist);
2217
2218 nparms = TREE_VEC_LENGTH (parms);
2219
2220 if (nargs > nparms
2221 || (nargs < nparms
2222 && require_all_arguments
2223 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
2224 {
2225 if (complain)
2226 {
2227 error ("incorrect number of parameters (%d, should be %d)",
2228 nargs, nparms);
2229
2230 if (in_decl)
2231 cp_error_at ("in template expansion for decl `%D'",
2232 in_decl);
2233 }
2234
2235 return error_mark_node;
2236 }
2237
2238 if (arglist && TREE_CODE (arglist) == TREE_VEC && nargs == nparms)
2239 vec = copy_node (arglist);
2240 else
2241 {
2242 vec = make_tree_vec (nparms);
2243
2244 for (i = 0; i < nparms; i++)
2245 {
2246 tree arg;
2247 tree parm = TREE_VEC_ELT (parms, i);
2248
2249 if (arglist)
2250 {
2251 arg = arglist;
2252 arglist = TREE_CHAIN (arglist);
2253
2254 if (arg == error_mark_node)
2255 lost++;
2256 else
2257 arg = TREE_VALUE (arg);
2258 }
2259 else if (is_tmpl_parm && i < nargs)
2260 {
2261 arg = TREE_VEC_ELT (arglist, i);
2262 if (arg == error_mark_node)
2263 lost++;
2264 }
2265 else if (TREE_PURPOSE (parm) == NULL_TREE)
2266 {
2267 my_friendly_assert (!require_all_arguments, 0);
2268 break;
2269 }
2270 else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
2271 arg = tsubst (TREE_PURPOSE (parm), vec, in_decl);
2272 else
2273 arg = tsubst_expr (TREE_PURPOSE (parm), vec, in_decl);
2274
2275 TREE_VEC_ELT (vec, i) = arg;
2276 }
2277 }
2278 for (i = 0; i < nparms; i++)
2279 {
2280 tree arg = TREE_VEC_ELT (vec, i);
2281 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2282 tree val = 0;
2283 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
2284
2285 if (is_tmpl_parm && i < nargs)
2286 {
2287 /* In case we are checking arguments inside a template template
2288 parameter, ARG that does not come from default argument is
2289 also a TREE_LIST node. Note that ARG can also be a TREE_LIST
2290 in other cases such as overloaded functions. */
2291 if (arg != NULL_TREE && arg != error_mark_node)
2292 arg = TREE_VALUE (arg);
2293 }
2294
2295 if (arg == NULL_TREE)
2296 /* We're out of arguments. */
2297 {
2298 my_friendly_assert (!require_all_arguments, 0);
2299 break;
2300 }
2301
2302 if (arg == error_mark_node)
2303 {
2304 cp_error ("template argument %d is invalid", i + 1);
2305 lost++;
2306 continue;
2307 }
2308
2309 if (TREE_CODE (arg) == TREE_LIST
2310 && TREE_TYPE (arg) != NULL_TREE
2311 && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
2312 {
2313 /* The template argument was the name of some
2314 member function. That's usually
2315 illegal, but static members are OK. In any
2316 case, grab the underlying fields/functions
2317 and issue an error later if required. */
2318 arg = TREE_VALUE (arg);
2319 TREE_TYPE (arg) = unknown_type_node;
2320 }
2321
2322 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
2323 requires_type = TREE_CODE (parm) == TYPE_DECL
2324 || requires_tmpl_type;
2325
2326 /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true,
2327 we also accept implicitly created TYPE_DECL as a valid argument. */
2328 is_tmpl_type = (TREE_CODE (arg) == TEMPLATE_DECL
2329 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
2330 || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
2331 && !CLASSTYPE_TEMPLATE_INFO (arg))
2332 || (TREE_CODE (arg) == RECORD_TYPE
2333 && CLASSTYPE_TEMPLATE_INFO (arg)
2334 && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL
2335 && DECL_ARTIFICIAL (TYPE_NAME (arg))
2336 && requires_tmpl_type);
2337 if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
2338 arg = TYPE_STUB_DECL (arg);
2339 else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE)
2340 arg = CLASSTYPE_TI_TEMPLATE (arg);
2341
2342 if (is_tmpl_parm && i < nargs)
2343 is_type = TREE_CODE (arg) == TYPE_DECL || is_tmpl_type;
2344 else
2345 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type;
2346
2347 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
2348 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
2349 {
2350 cp_pedwarn ("to refer to a type member of a template parameter,");
2351 cp_pedwarn (" use `typename %E'", arg);
2352
2353 arg = make_typename_type (TREE_OPERAND (arg, 0),
2354 TREE_OPERAND (arg, 1));
2355 is_type = 1;
2356 }
2357 if (is_type != requires_type)
2358 {
2359 if (in_decl)
2360 {
2361 if (complain)
2362 {
2363 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2364 i + 1, in_decl);
2365 if (is_type)
2366 cp_error (" expected a constant of type `%T', got `%T'",
2367 TREE_TYPE (parm),
2368 (is_tmpl_type ? DECL_NAME (arg) : arg));
2369 else
2370 cp_error (" expected a type, got `%E'", arg);
2371 }
2372 }
2373 lost++;
2374 TREE_VEC_ELT (vec, i) = error_mark_node;
2375 continue;
2376 }
2377 if (is_tmpl_type ^ requires_tmpl_type)
2378 {
2379 if (in_decl)
2380 {
2381 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
2382 i + 1, in_decl);
2383 if (is_tmpl_type)
2384 cp_error (" expected a type, got `%T'", DECL_NAME (arg));
2385 else
2386 cp_error (" expected a class template, got `%T'", arg);
2387 }
2388 lost++;
2389 TREE_VEC_ELT (vec, i) = error_mark_node;
2390 continue;
2391 }
2392 if (is_tmpl_parm)
2393 {
2394 if (requires_tmpl_type)
2395 {
2396 cp_error ("nested template template parameter not implemented");
2397 lost++;
2398 TREE_VEC_ELT (vec, i) = error_mark_node;
2399 }
2400 continue;
2401 }
2402
2403 if (is_type)
2404 {
2405 if (requires_tmpl_type)
2406 {
2407 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
2408 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
2409
2410 /* The parameter and argument roles have to be switched
2411 here in order to handle default arguments properly.
2412 For example,
2413 template<template <class> class TT> void f(TT<int>)
2414 should be able to accept vector<int> which comes from
2415 template <class T, class Allcator = allocator>
2416 class vector. */
2417
2418 val = coerce_template_parms (argparm, parmparm, in_decl, 1, 1, 1);
2419 if (val != error_mark_node)
2420 val = arg;
2421
2422 /* TEMPLATE_TEMPLATE_PARM node is preferred over
2423 TEMPLATE_DECL. */
2424 if (val != error_mark_node
2425 && DECL_TEMPLATE_TEMPLATE_PARM_P (val))
2426 val = TREE_TYPE (val);
2427 }
2428 else
2429 {
2430 val = groktypename (arg);
2431 if (! processing_template_decl)
2432 {
2433 tree t = target_type (val);
2434 if (TREE_CODE (t) != TYPENAME_TYPE
2435 && IS_AGGR_TYPE (t)
2436 && decl_function_context (TYPE_MAIN_DECL (t)))
2437 {
2438 cp_error ("type `%T' composed from a local class is not a valid template-argument",
2439 val);
2440 return error_mark_node;
2441 }
2442 }
2443 }
2444 }
2445 else
2446 {
2447 tree t = tsubst (TREE_TYPE (parm), vec, in_decl);
2448
2449 if (processing_template_decl)
2450 arg = maybe_fold_nontype_arg (arg);
2451
2452 if (!uses_template_parms (arg) && !uses_template_parms (t))
2453 /* We used to call digest_init here. However, digest_init
2454 will report errors, which we don't want when complain
2455 is zero. More importantly, digest_init will try too
2456 hard to convert things: for example, `0' should not be
2457 converted to pointer type at this point according to
2458 the standard. Accepting this is not merely an
2459 extension, since deciding whether or not these
2460 conversions can occur is part of determining which
2461 function template to call, or whether a given epxlicit
2462 argument specification is legal. */
2463 val = convert_nontype_argument (t, arg);
2464 else
2465 val = arg;
2466
2467 if (val == NULL_TREE)
2468 val = error_mark_node;
2469 else if (val == error_mark_node && complain)
2470 cp_error ("could not convert template argument `%E' to `%T'",
2471 arg, t);
2472 }
2473
2474 if (val == error_mark_node)
2475 lost++;
2476
2477 TREE_VEC_ELT (vec, i) = val;
2478 }
2479 if (lost)
2480 return error_mark_node;
2481 return vec;
2482 }
2483
2484 /* Renturns 1 iff the OLDARGS and NEWARGS are in fact identical sets
2485 of template arguments. Returns 0 otherwise. */
2486
2487 static int
2488 comp_template_args (oldargs, newargs)
2489 tree oldargs, newargs;
2490 {
2491 int i;
2492
2493 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
2494 return 0;
2495
2496 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
2497 {
2498 tree nt = TREE_VEC_ELT (newargs, i);
2499 tree ot = TREE_VEC_ELT (oldargs, i);
2500
2501 if (nt == ot)
2502 continue;
2503 if (TREE_CODE (nt) != TREE_CODE (ot))
2504 return 0;
2505 if (TREE_CODE (nt) == TREE_VEC)
2506 {
2507 /* For member templates */
2508 if (comp_template_args (nt, ot))
2509 continue;
2510 }
2511 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
2512 {
2513 if (comptypes (ot, nt, 1))
2514 continue;
2515 }
2516 else if (cp_tree_equal (ot, nt) > 0)
2517 continue;
2518 return 0;
2519 }
2520 return 1;
2521 }
2522
2523 /* Given class template name and parameter list, produce a user-friendly name
2524 for the instantiation. */
2525
2526 static char *
2527 mangle_class_name_for_template (name, parms, arglist, ctx)
2528 char *name;
2529 tree parms, arglist;
2530 tree ctx;
2531 {
2532 static struct obstack scratch_obstack;
2533 static char *scratch_firstobj;
2534 int i, nparms;
2535
2536 if (!scratch_firstobj)
2537 gcc_obstack_init (&scratch_obstack);
2538 else
2539 obstack_free (&scratch_obstack, scratch_firstobj);
2540 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
2541
2542 #if 0
2543 #define buflen sizeof(buf)
2544 #define check if (bufp >= buf+buflen-1) goto too_long
2545 #define ccat(c) *bufp++=(c); check
2546 #define advance bufp+=strlen(bufp); check
2547 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
2548 #else
2549 #define check
2550 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
2551 #define advance
2552 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
2553 #endif
2554
2555 if (ctx)
2556 {
2557 char* s;
2558
2559 if (TREE_CODE (ctx) == FUNCTION_DECL)
2560 s = fndecl_as_string (ctx, 0);
2561 else if (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
2562 s = type_as_string_real (ctx, 0, 1);
2563 else
2564 my_friendly_abort (0);
2565 cat (s);
2566 cat ("::");
2567 }
2568 cat (name);
2569 ccat ('<');
2570 nparms = TREE_VEC_LENGTH (parms);
2571 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
2572 for (i = 0; i < nparms; i++)
2573 {
2574 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
2575 tree arg = TREE_VEC_ELT (arglist, i);
2576
2577 if (i)
2578 ccat (',');
2579
2580 if (TREE_CODE (parm) == TYPE_DECL)
2581 {
2582 cat (type_as_string_real (arg, 0, 1));
2583 continue;
2584 }
2585 else if (TREE_CODE (parm) == TEMPLATE_DECL)
2586 {
2587 if (TREE_CODE (arg) == TEMPLATE_DECL)
2588 /* Already substituted with real template. Just output
2589 the template name here */
2590 cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
2591 else
2592 /* Output the parameter declaration */
2593 cat (type_as_string_real (arg, 0, 1));
2594 continue;
2595 }
2596 else
2597 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
2598
2599 if (TREE_CODE (arg) == TREE_LIST)
2600 {
2601 /* New list cell was built because old chain link was in
2602 use. */
2603 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
2604 arg = TREE_VALUE (arg);
2605 }
2606 /* No need to check arglist against parmlist here; we did that
2607 in coerce_template_parms, called from lookup_template_class. */
2608 cat (expr_as_string (arg, 0));
2609 }
2610 {
2611 char *bufp = obstack_next_free (&scratch_obstack);
2612 int offset = 0;
2613 while (bufp[offset - 1] == ' ')
2614 offset--;
2615 obstack_blank_fast (&scratch_obstack, offset);
2616
2617 /* B<C<char> >, not B<C<char>> */
2618 if (bufp[offset - 1] == '>')
2619 ccat (' ');
2620 }
2621 ccat ('>');
2622 ccat ('\0');
2623 return (char *) obstack_base (&scratch_obstack);
2624
2625 #if 0
2626 too_long:
2627 #endif
2628 fatal ("out of (preallocated) string space creating template instantiation name");
2629 /* NOTREACHED */
2630 return NULL;
2631 }
2632
2633 static tree
2634 classtype_mangled_name (t)
2635 tree t;
2636 {
2637 if (CLASSTYPE_TEMPLATE_INFO (t)
2638 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
2639 {
2640 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
2641 char *mangled_name = mangle_class_name_for_template
2642 (IDENTIFIER_POINTER (name),
2643 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
2644 CLASSTYPE_TI_ARGS (t), DECL_CONTEXT (t));
2645 tree id = get_identifier (mangled_name);
2646 IDENTIFIER_TEMPLATE (id) = name;
2647 return id;
2648 }
2649 else
2650 return TYPE_IDENTIFIER (t);
2651 }
2652
2653 static void
2654 add_pending_template (d)
2655 tree d;
2656 {
2657 tree ti;
2658
2659 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
2660 ti = CLASSTYPE_TEMPLATE_INFO (d);
2661 else
2662 ti = DECL_TEMPLATE_INFO (d);
2663
2664 if (TI_PENDING_TEMPLATE_FLAG (ti))
2665 return;
2666
2667 *template_tail = perm_tree_cons
2668 (current_function_decl, d, NULL_TREE);
2669 template_tail = &TREE_CHAIN (*template_tail);
2670 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
2671 }
2672
2673
2674 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
2675 may be either a _DECL or an overloaded function or an
2676 IDENTIFIER_NODE), and ARGLIST. */
2677
2678 tree
2679 lookup_template_function (fns, arglist)
2680 tree fns, arglist;
2681 {
2682 tree t;
2683
2684 if (fns == NULL_TREE)
2685 {
2686 cp_error ("non-template used as template");
2687 return error_mark_node;
2688 }
2689
2690 if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
2691 copy_to_permanent (arglist);
2692
2693 return build_min (TEMPLATE_ID_EXPR,
2694 TREE_TYPE (fns)
2695 ? TREE_TYPE (fns) : unknown_type_node,
2696 fns, arglist);
2697 }
2698
2699 /* Within the scope of a template class S<T>, the name S gets bound
2700 (in build_self_reference) to a TYPE_DECL for the class, not a
2701 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
2702 or one of its enclosing classes, and that type is a template,
2703 return the associated TEMPLATE_DECL. Otherwise, the original
2704 DECL is returned. */
2705
2706 tree
2707 maybe_get_template_decl_from_type_decl (decl)
2708 tree decl;
2709 {
2710 return (decl != NULL_TREE
2711 && TREE_CODE (decl) == TYPE_DECL
2712 && DECL_ARTIFICIAL (decl)
2713 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
2714 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
2715 }
2716
2717 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
2718 parameters, find the desired type.
2719
2720 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
2721 Since ARGLIST is build on the decl_obstack, we must copy it here
2722 to keep it from being reclaimed when the decl storage is reclaimed.
2723
2724 IN_DECL, if non-NULL, is the template declaration we are trying to
2725 instantiate.
2726
2727 If the template class is really a local class in a template
2728 function, then the FUNCTION_CONTEXT is the function in which it is
2729 being instantiated. */
2730
2731 tree
2732 lookup_template_class (d1, arglist, in_decl, context)
2733 tree d1, arglist;
2734 tree in_decl;
2735 tree context;
2736 {
2737 tree template = NULL_TREE, parmlist;
2738 char *mangled_name;
2739 tree id, t;
2740
2741 if (TREE_CODE (d1) == IDENTIFIER_NODE)
2742 {
2743 if (IDENTIFIER_LOCAL_VALUE (d1)
2744 && DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_LOCAL_VALUE (d1)))
2745 template = IDENTIFIER_LOCAL_VALUE (d1);
2746 else
2747 {
2748 template =
2749 maybe_get_template_decl_from_type_decl
2750 (IDENTIFIER_CLASS_VALUE (d1));
2751 if (template == NULL_TREE)
2752 template = IDENTIFIER_NAMESPACE_VALUE (d1);
2753 }
2754 if (template)
2755 context = DECL_CONTEXT (template);
2756 }
2757 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
2758 {
2759 if (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (d1)) == NULL_TREE)
2760 return error_mark_node;
2761 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
2762 d1 = DECL_NAME (template);
2763 }
2764 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
2765 {
2766 template = CLASSTYPE_TI_TEMPLATE (d1);
2767 d1 = DECL_NAME (template);
2768 }
2769 else if (TREE_CODE (d1) == TEMPLATE_DECL
2770 && TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
2771 {
2772 template = d1;
2773 d1 = DECL_NAME (template);
2774 context = DECL_CONTEXT (template);
2775 }
2776 else
2777 my_friendly_abort (272);
2778
2779 /* With something like `template <class T> class X class X { ... };'
2780 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
2781 We don't want to do that, but we have to deal with the situation, so
2782 let's give them some syntax errors to chew on instead of a crash. */
2783 if (! template)
2784 return error_mark_node;
2785 if (TREE_CODE (template) != TEMPLATE_DECL)
2786 {
2787 cp_error ("non-template type `%T' used as a template", d1);
2788 if (in_decl)
2789 cp_error_at ("for template declaration `%D'", in_decl);
2790 return error_mark_node;
2791 }
2792
2793 if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
2794 {
2795 /* Create a new TEMPLATE_DECL and TEMPLATE_TEMPLATE_PARM node to store
2796 template arguments */
2797
2798 tree parm = copy_template_template_parm (TREE_TYPE (template));
2799 tree template2 = TYPE_STUB_DECL (parm);
2800 tree arglist2;
2801
2802 CLASSTYPE_GOT_SEMICOLON (parm) = 1;
2803 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
2804
2805 arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1, 0);
2806 if (arglist2 == error_mark_node)
2807 return error_mark_node;
2808
2809 arglist2 = copy_to_permanent (arglist2);
2810 CLASSTYPE_TEMPLATE_INFO (parm)
2811 = perm_tree_cons (template2, arglist2, NULL_TREE);
2812 TYPE_SIZE (parm) = 0;
2813 return parm;
2814 }
2815 else if (PRIMARY_TEMPLATE_P (template)
2816 || (TREE_CODE (TYPE_CONTEXT (TREE_TYPE (template)))
2817 == FUNCTION_DECL))
2818 {
2819 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
2820
2821 arglist = coerce_template_parms (parmlist, arglist, template,
2822 1, 1, 0);
2823 if (arglist == error_mark_node)
2824 return error_mark_node;
2825 if (uses_template_parms (arglist))
2826 {
2827 tree found;
2828 if (comp_template_args
2829 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
2830 found = TREE_TYPE (template);
2831 else
2832 {
2833 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
2834 found; found = TREE_CHAIN (found))
2835 {
2836 if (TI_USES_TEMPLATE_PARMS (found)
2837 && comp_template_args (TREE_PURPOSE (found), arglist))
2838 break;
2839 }
2840 if (found)
2841 found = TREE_VALUE (found);
2842 }
2843
2844 if (found)
2845 {
2846 if (can_free (&permanent_obstack, arglist))
2847 obstack_free (&permanent_obstack, arglist);
2848 return found;
2849 }
2850 }
2851
2852 /* FIXME avoid duplication. */
2853 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
2854 parmlist,
2855 arglist,
2856 context);
2857 id = get_identifier (mangled_name);
2858 IDENTIFIER_TEMPLATE (id) = d1;
2859
2860 maybe_push_to_top_level (uses_template_parms (arglist));
2861 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
2862
2863 if (context != NULL_TREE)
2864 {
2865 /* Set up the context for the type_decl correctly. Note
2866 that we must clear DECL_ASSEMBLER_NAME to fool
2867 build_overload_name into creating a new name. */
2868 tree type_decl = TYPE_STUB_DECL (t);
2869
2870 TYPE_CONTEXT (t) = context;
2871 DECL_CONTEXT (type_decl) = context;
2872 DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
2873 DECL_ASSEMBLER_NAME (type_decl) =
2874 get_identifier (build_overload_name (t, 1, 1));
2875 }
2876
2877 pop_from_top_level ();
2878 }
2879 else
2880 {
2881 tree type_ctx = TYPE_CONTEXT (TREE_TYPE (template));
2882 tree args = tsubst (CLASSTYPE_TI_ARGS (type_ctx), arglist, in_decl);
2883 tree ctx = lookup_template_class (type_ctx, args,
2884 in_decl, NULL_TREE);
2885 id = d1;
2886 arglist = CLASSTYPE_TI_ARGS (ctx);
2887
2888 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
2889 {
2890 int save_temp = processing_template_decl;
2891 processing_template_decl = 0;
2892 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
2893 processing_template_decl = save_temp;
2894 }
2895 else
2896 {
2897 t = lookup_nested_type_by_name (ctx, id);
2898 my_friendly_assert (t != NULL_TREE, 42);
2899 }
2900 }
2901
2902 /* Seems to be wanted. */
2903 CLASSTYPE_GOT_SEMICOLON (t) = 1;
2904
2905 if (! CLASSTYPE_TEMPLATE_INFO (t))
2906 {
2907 arglist = copy_to_permanent (arglist);
2908 CLASSTYPE_TEMPLATE_INFO (t)
2909 = perm_tree_cons (template, arglist, NULL_TREE);
2910 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
2911 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
2912 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
2913 = uses_template_parms (arglist);
2914
2915 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
2916
2917 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
2918 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
2919 if (! uses_template_parms (arglist))
2920 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
2921 = get_identifier (build_overload_name (t, 1, 1));
2922
2923 if (flag_external_templates && ! uses_template_parms (arglist)
2924 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
2925 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
2926 add_pending_template (t);
2927 }
2928
2929 return t;
2930 }
2931 \f
2932 /* Should be defined in parse.h. */
2933 extern int yychar;
2934
2935 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM, or
2936 TEMPLATE_PARM_INDEX in T, call FN with the parameter and the DATA.
2937 If FN returns non-zero, the iteration is terminated, and
2938 for_each_template_parm returns 1. Otherwise, the iteration
2939 continues. If FN never returns a non-zero value, the value
2940 returned by for_each_template_parm is 0. If FN is NULL, it is
2941 considered to be the function which always returns 1. */
2942
2943 int
2944 for_each_template_parm (t, fn, data)
2945 tree t;
2946 tree_fn_t fn;
2947 void* data;
2948 {
2949 if (!t)
2950 return 0;
2951 switch (TREE_CODE (t))
2952 {
2953 case INDIRECT_REF:
2954 case COMPONENT_REF:
2955 /* We assume that the object must be instantiated in order to build
2956 the COMPONENT_REF, so we test only whether the type of the
2957 COMPONENT_REF uses template parms. */
2958 return for_each_template_parm (TREE_TYPE (t), fn, data);
2959
2960 case IDENTIFIER_NODE:
2961 if (!IDENTIFIER_TEMPLATE (t))
2962 return 0;
2963 my_friendly_abort (42);
2964
2965 /* aggregates of tree nodes */
2966 case TREE_VEC:
2967 {
2968 int i = TREE_VEC_LENGTH (t);
2969 while (i--)
2970 if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
2971 return 1;
2972 return 0;
2973 }
2974 case TREE_LIST:
2975 if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
2976 || for_each_template_parm (TREE_VALUE (t), fn, data))
2977 return 1;
2978 return for_each_template_parm (TREE_CHAIN (t), fn, data);
2979
2980 /* constructed type nodes */
2981 case POINTER_TYPE:
2982 case REFERENCE_TYPE:
2983 return for_each_template_parm (TREE_TYPE (t), fn, data);
2984 case RECORD_TYPE:
2985 if (TYPE_PTRMEMFUNC_FLAG (t))
2986 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
2987 fn, data);
2988 case UNION_TYPE:
2989 if (! CLASSTYPE_TEMPLATE_INFO (t))
2990 return 0;
2991 return for_each_template_parm (TREE_VALUE
2992 (CLASSTYPE_TEMPLATE_INFO (t)),
2993 fn, data);
2994 case FUNCTION_TYPE:
2995 if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data))
2996 return 1;
2997 return for_each_template_parm (TREE_TYPE (t), fn, data);
2998 case ARRAY_TYPE:
2999 if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
3000 return 1;
3001 return for_each_template_parm (TREE_TYPE (t), fn, data);
3002 case OFFSET_TYPE:
3003 if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
3004 return 1;
3005 return for_each_template_parm (TREE_TYPE (t), fn, data);
3006 case METHOD_TYPE:
3007 if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
3008 return 1;
3009 if (for_each_template_parm (TYPE_ARG_TYPES (t), fn, data))
3010 return 1;
3011 return for_each_template_parm (TREE_TYPE (t), fn, data);
3012
3013 /* decl nodes */
3014 case TYPE_DECL:
3015 return for_each_template_parm (TREE_TYPE (t), fn, data);
3016
3017 case TEMPLATE_DECL:
3018 /* A template template parameter is encountered */
3019 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3020 /* We are parsing a template declaration */
3021 return 1;
3022 /* We are instantiating templates with template template
3023 parameter */
3024 return 0;
3025
3026 case CONST_DECL:
3027 if (for_each_template_parm (DECL_INITIAL (t), fn, data))
3028 return 1;
3029 goto check_type_and_context;
3030
3031 case FUNCTION_DECL:
3032 case VAR_DECL:
3033 /* ??? What about FIELD_DECLs? */
3034 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
3035 && for_each_template_parm (DECL_TI_ARGS (t), fn, data))
3036 return 1;
3037 /* fall through */
3038 case PARM_DECL:
3039 check_type_and_context:
3040 if (for_each_template_parm (TREE_TYPE (t), fn, data))
3041 return 1;
3042 if (DECL_CONTEXT (t)
3043 && for_each_template_parm (DECL_CONTEXT (t), fn, data))
3044 return 1;
3045 return 0;
3046
3047 case CALL_EXPR:
3048 return for_each_template_parm (TREE_TYPE (t), fn, data);
3049 case ADDR_EXPR:
3050 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3051
3052 /* template parm nodes */
3053 case TEMPLATE_TYPE_PARM:
3054 case TEMPLATE_TEMPLATE_PARM:
3055 case TEMPLATE_PARM_INDEX:
3056 if (fn)
3057 return (*fn)(t, data);
3058 else
3059 return 1;
3060
3061 /* simple type nodes */
3062 case INTEGER_TYPE:
3063 if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
3064 return 1;
3065 return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
3066
3067 case REAL_TYPE:
3068 case COMPLEX_TYPE:
3069 case VOID_TYPE:
3070 case BOOLEAN_TYPE:
3071 return 0;
3072
3073 case ENUMERAL_TYPE:
3074 {
3075 tree v;
3076
3077 for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
3078 if (for_each_template_parm (TREE_VALUE (v), fn, data))
3079 return 1;
3080 }
3081 return 0;
3082
3083 /* constants */
3084 case INTEGER_CST:
3085 case REAL_CST:
3086 case STRING_CST:
3087 return 0;
3088
3089 case ERROR_MARK:
3090 /* Non-error_mark_node ERROR_MARKs are bad things. */
3091 my_friendly_assert (t == error_mark_node, 274);
3092 /* NOTREACHED */
3093 return 0;
3094
3095 case LOOKUP_EXPR:
3096 case TYPENAME_TYPE:
3097 return 1;
3098
3099 case SCOPE_REF:
3100 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3101
3102 case CONSTRUCTOR:
3103 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
3104 return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
3105 (TREE_TYPE (t)), fn, data);
3106 return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
3107
3108 case MODOP_EXPR:
3109 case CAST_EXPR:
3110 case REINTERPRET_CAST_EXPR:
3111 case CONST_CAST_EXPR:
3112 case STATIC_CAST_EXPR:
3113 case DYNAMIC_CAST_EXPR:
3114 case ARROW_EXPR:
3115 case DOTSTAR_EXPR:
3116 case TYPEID_EXPR:
3117 return 1;
3118
3119 case SIZEOF_EXPR:
3120 case ALIGNOF_EXPR:
3121 return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
3122
3123 default:
3124 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3125 {
3126 case '1':
3127 case '2':
3128 case 'e':
3129 case '<':
3130 {
3131 int i;
3132 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
3133 if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
3134 return 1;
3135 return 0;
3136 }
3137 default:
3138 break;
3139 }
3140 sorry ("testing %s for template parms",
3141 tree_code_name [(int) TREE_CODE (t)]);
3142 my_friendly_abort (82);
3143 /* NOTREACHED */
3144 return 0;
3145 }
3146 }
3147
3148 int
3149 uses_template_parms (t)
3150 tree t;
3151 {
3152 return for_each_template_parm (t, 0, 0);
3153 }
3154
3155 static struct tinst_level *current_tinst_level = 0;
3156 static struct tinst_level *free_tinst_level = 0;
3157 static int tinst_depth = 0;
3158 extern int max_tinst_depth;
3159 #ifdef GATHER_STATISTICS
3160 int depth_reached = 0;
3161 #endif
3162
3163 /* Print out all the template instantiations that we are currently
3164 working on. */
3165
3166 void
3167 print_template_context ()
3168 {
3169 struct tinst_level *p = current_tinst_level;
3170 int line = lineno;
3171 char *file = input_filename;
3172
3173 for (; p; p = p->next)
3174 {
3175 cp_error (" instantiated from `%D'", p->decl);
3176 lineno = p->line;
3177 input_filename = p->file;
3178 }
3179 error (" instantiated from here");
3180
3181 lineno = line;
3182 input_filename = file;
3183 }
3184
3185 static int
3186 push_tinst_level (d)
3187 tree d;
3188 {
3189 struct tinst_level *new;
3190
3191 if (tinst_depth >= max_tinst_depth)
3192 {
3193 /* If the instantiation in question still has unbound template parms,
3194 we don't really care if we can't instantiate it, so just return.
3195 This happens with base instantiation for implicit `typename'. */
3196 if (uses_template_parms (d))
3197 return 0;
3198
3199 error ("template instantiation depth exceeds maximum of %d",
3200 max_tinst_depth);
3201 error (" (use -ftemplate-depth-NN to increase the maximum)");
3202 cp_error (" instantiating `%D'", d);
3203
3204 print_template_context ();
3205
3206 return 0;
3207 }
3208
3209 if (free_tinst_level)
3210 {
3211 new = free_tinst_level;
3212 free_tinst_level = new->next;
3213 }
3214 else
3215 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
3216
3217 new->decl = d;
3218 new->line = lineno;
3219 new->file = input_filename;
3220 new->next = current_tinst_level;
3221 current_tinst_level = new;
3222
3223 ++tinst_depth;
3224 #ifdef GATHER_STATISTICS
3225 if (tinst_depth > depth_reached)
3226 depth_reached = tinst_depth;
3227 #endif
3228
3229 return 1;
3230 }
3231
3232 void
3233 pop_tinst_level ()
3234 {
3235 struct tinst_level *old = current_tinst_level;
3236
3237 current_tinst_level = old->next;
3238 old->next = free_tinst_level;
3239 free_tinst_level = old;
3240 --tinst_depth;
3241 }
3242
3243 struct tinst_level *
3244 tinst_for_decl ()
3245 {
3246 struct tinst_level *p = current_tinst_level;
3247
3248 if (p)
3249 for (; p->next ; p = p->next )
3250 ;
3251 return p;
3252 }
3253
3254
3255 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
3256 vector of template arguments, as for tsubst.
3257
3258 Returns an appropriate tsbust'd friend declaration. */
3259
3260 static tree
3261 tsubst_friend_function (decl, args)
3262 tree decl;
3263 tree args;
3264 {
3265 tree new_friend;
3266
3267 if (TREE_CODE (decl) == FUNCTION_DECL
3268 && DECL_TEMPLATE_INSTANTIATION (decl)
3269 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
3270 /* This was a friend declared with an explicit template
3271 argument list, e.g.:
3272
3273 friend void f<>(T);
3274
3275 to indicate that f was a template instantiation, not a new
3276 function declaration. Now, we have to figure out what
3277 instantiation of what template. */
3278 {
3279 tree template_id;
3280 tree new_args;
3281 tree tmpl;
3282 tree tinfo;
3283
3284 template_id
3285 = lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
3286 args, NULL_TREE),
3287 tsubst (DECL_TI_ARGS (decl),
3288 args, NULL_TREE));
3289
3290 /* Temporarily remove the DECL_TEMPLATE_INFO so as not to
3291 confuse tsubst. */
3292 tinfo = DECL_TEMPLATE_INFO (decl);
3293 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
3294 new_friend = tsubst (decl, args, NULL_TREE);
3295 DECL_TEMPLATE_INFO (decl) = tinfo;
3296
3297 tmpl = determine_specialization (template_id,
3298 new_friend,
3299 &new_args,
3300 0, 1);
3301 return instantiate_template (tmpl, new_args);
3302 }
3303 else
3304 new_friend = tsubst (decl, args, NULL_TREE);
3305
3306 /* The new_friend will look like an instantiation, to the
3307 compiler, but is not an instantiation from the point of view of
3308 the language. For example, we might have had:
3309
3310 template <class T> struct S {
3311 template <class U> friend void f(T, U);
3312 };
3313
3314 Then, in S<int>, template <class U> void f(int, U) is not an
3315 instantiation of anything. */
3316 DECL_USE_TEMPLATE (new_friend) = 0;
3317 if (TREE_CODE (decl) == TEMPLATE_DECL)
3318 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
3319
3320 if (DECL_CONTEXT (new_friend) == NULL_TREE)
3321 {
3322 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
3323 /* This declaration is a `primary' template. */
3324 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (new_friend))
3325 = new_friend;
3326
3327 new_friend = pushdecl_top_level (new_friend);
3328 }
3329 else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
3330 {
3331 /* Check to see that the declaration is really present, and,
3332 possibly obtain an improved declaration. */
3333 tree fn = check_classfn (DECL_CONTEXT (new_friend),
3334 new_friend);
3335
3336 if (fn)
3337 new_friend = fn;
3338 }
3339
3340 return new_friend;
3341 }
3342
3343
3344 tree
3345 instantiate_class_template (type)
3346 tree type;
3347 {
3348 tree template, template_info, args, pattern, t, *field_chain;
3349 tree typedecl, outer_args;
3350
3351 if (type == error_mark_node)
3352 return error_mark_node;
3353
3354 template_info = CLASSTYPE_TEMPLATE_INFO (type);
3355
3356 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
3357 return type;
3358
3359 template = TI_TEMPLATE (template_info);
3360 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
3361 args = TI_ARGS (template_info);
3362
3363 if (DECL_TEMPLATE_INFO (template))
3364 {
3365 outer_args = DECL_TI_ARGS (template);
3366 while (DECL_TEMPLATE_INFO (template))
3367 template = DECL_TI_TEMPLATE (template);
3368 }
3369 else
3370 outer_args = NULL_TREE;
3371
3372 t = most_specialized_class
3373 (DECL_TEMPLATE_SPECIALIZATIONS (template), args, outer_args);
3374
3375 if (t == error_mark_node)
3376 {
3377 char *str = "candidates are:";
3378 cp_error ("ambiguous class template instantiation for `%#T'", type);
3379 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
3380 {
3381 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
3382 args, outer_args))
3383 {
3384 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
3385 str = " ";
3386 }
3387 }
3388 TYPE_BEING_DEFINED (type) = 1;
3389 return error_mark_node;
3390 }
3391 else if (t)
3392 pattern = TREE_TYPE (t);
3393 else
3394 pattern = TREE_TYPE (template);
3395
3396 if (TYPE_SIZE (pattern) == NULL_TREE)
3397 return type;
3398
3399 if (t)
3400 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
3401 args, outer_args);
3402
3403 if (pedantic && uses_template_parms (args))
3404 /* If there are still template parameters amongst the args, then
3405 we can't instantiate the type; there's no telling whether or not one
3406 of the template parameters might eventually be instantiated to some
3407 value that results in a specialization being used. */
3408 return type;
3409
3410 /* We must copy the arguments to the permanent obstack since
3411 during the tsubst'ing below they may wind up in the
3412 DECL_TI_ARGS of some instantiated member template. */
3413 args = copy_to_permanent (args);
3414
3415 TYPE_BEING_DEFINED (type) = 1;
3416
3417 if (! push_tinst_level (type))
3418 return type;
3419
3420 maybe_push_to_top_level (uses_template_parms (type));
3421 pushclass (type, 0);
3422
3423 if (outer_args)
3424 args = add_to_template_args (outer_args, args);
3425
3426 if (flag_external_templates)
3427 {
3428 if (flag_alt_external_templates)
3429 {
3430 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
3431 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
3432 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
3433 = (! CLASSTYPE_INTERFACE_ONLY (type)
3434 && CLASSTYPE_INTERFACE_KNOWN (type));
3435 }
3436 else
3437 {
3438 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
3439 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
3440 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
3441 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
3442 = (! CLASSTYPE_INTERFACE_ONLY (type)
3443 && CLASSTYPE_INTERFACE_KNOWN (type));
3444 }
3445 }
3446 else
3447 {
3448 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
3449 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
3450 }
3451
3452 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
3453 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
3454 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
3455 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
3456 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
3457 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
3458 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
3459 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
3460 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
3461 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
3462 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
3463 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
3464 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
3465 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
3466 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
3467 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
3468 TYPE_USES_COMPLEX_INHERITANCE (type)
3469 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
3470 TYPE_USES_MULTIPLE_INHERITANCE (type)
3471 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
3472 TYPE_USES_VIRTUAL_BASECLASSES (type)
3473 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
3474 TYPE_PACKED (type) = TYPE_PACKED (pattern);
3475 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
3476
3477 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
3478
3479 /* If this is a partial instantiation, don't tsubst anything. We will
3480 only use this type for implicit typename, so the actual contents don't
3481 matter. All that matters is whether a particular name is a type. */
3482 if (uses_template_parms (type))
3483 {
3484 TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
3485 TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
3486 TYPE_METHODS (type) = TYPE_METHODS (pattern);
3487 CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
3488 TYPE_SIZE (type) = integer_zero_node;
3489 goto end;
3490 }
3491
3492 {
3493 tree binfo = TYPE_BINFO (type);
3494 tree pbases = TYPE_BINFO_BASETYPES (pattern);
3495
3496 if (pbases)
3497 {
3498 tree bases;
3499 int i;
3500 int len = TREE_VEC_LENGTH (pbases);
3501 bases = make_tree_vec (len);
3502 for (i = 0; i < len; ++i)
3503 {
3504 tree elt;
3505
3506 TREE_VEC_ELT (bases, i) = elt
3507 = tsubst (TREE_VEC_ELT (pbases, i), args, NULL_TREE);
3508 BINFO_INHERITANCE_CHAIN (elt) = binfo;
3509
3510 if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
3511 cp_error
3512 ("base type `%T' of `%T' fails to be a struct or class type",
3513 TREE_TYPE (elt), type);
3514 else if (TYPE_SIZE (complete_type (TREE_TYPE (elt))) == NULL_TREE)
3515 cp_error ("base class `%T' of `%T' has incomplete type",
3516 TREE_TYPE (elt), type);
3517 }
3518 /* Don't initialize this until the vector is filled out, or
3519 lookups will crash. */
3520 BINFO_BASETYPES (binfo) = bases;
3521 }
3522 }
3523
3524 field_chain = &TYPE_FIELDS (type);
3525
3526 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
3527 {
3528 tree tag = TREE_VALUE (t);
3529
3530 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
3531 if (TREE_CODE (tag) == ENUMERAL_TYPE)
3532 {
3533 (void) tsubst_enum (tag, args, field_chain);
3534 while (*field_chain)
3535 {
3536 DECL_FIELD_CONTEXT (*field_chain) = type;
3537 field_chain = &TREE_CHAIN (*field_chain);
3538 }
3539 }
3540 else
3541 tsubst (tag, args, NULL_TREE);
3542 }
3543
3544 /* Don't replace enum constants here. */
3545 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
3546 if (TREE_CODE (t) != CONST_DECL)
3547 {
3548 tree r = tsubst (t, args, NULL_TREE);
3549 if (TREE_CODE (r) == VAR_DECL)
3550 {
3551 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
3552 /* Perhaps we should do more of grokfield here. */
3553 start_decl_1 (r);
3554 DECL_IN_AGGR_P (r) = 1;
3555 DECL_EXTERNAL (r) = 1;
3556 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
3557 }
3558
3559 *field_chain = r;
3560 field_chain = &TREE_CHAIN (r);
3561 }
3562
3563 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
3564 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
3565 {
3566 if (DECL_CONSTRUCTOR_P (t))
3567 grok_ctor_properties (type, t);
3568 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
3569 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
3570 }
3571
3572 /* Construct the DECL_FRIENDLIST for the new class type. */
3573 typedecl = TYPE_MAIN_DECL (type);
3574 for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
3575 t != NULL_TREE;
3576 t = TREE_CHAIN (t))
3577 {
3578 tree friends;
3579
3580 DECL_FRIENDLIST (typedecl)
3581 = tree_cons (TREE_PURPOSE (t), NULL_TREE,
3582 DECL_FRIENDLIST (typedecl));
3583
3584 for (friends = TREE_VALUE (t);
3585 friends != NULL_TREE;
3586 friends = TREE_CHAIN (friends))
3587 {
3588 if (TREE_PURPOSE (friends) == error_mark_node)
3589 {
3590 TREE_VALUE (DECL_FRIENDLIST (typedecl))
3591 = tree_cons (error_mark_node,
3592 tsubst_friend_function (TREE_VALUE (friends),
3593 args),
3594 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
3595 }
3596 else
3597 {
3598 TREE_VALUE (DECL_FRIENDLIST (typedecl))
3599 = tree_cons (tsubst (TREE_PURPOSE (friends), args, NULL_TREE),
3600 NULL_TREE,
3601 TREE_VALUE (DECL_FRIENDLIST (typedecl)));
3602
3603 }
3604 }
3605 }
3606
3607 t = CLASSTYPE_FRIEND_CLASSES (type)
3608 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args, NULL_TREE);
3609
3610 /* This does injection for friend classes. */
3611 for (; t; t = TREE_CHAIN (t))
3612 TREE_VALUE (t) = xref_tag_from_type (TREE_VALUE (t), NULL_TREE, 1);
3613
3614 /* This does injection for friend functions. */
3615 if (!processing_template_decl)
3616 {
3617 t = tsubst (DECL_TEMPLATE_INJECT (template), args, NULL_TREE);
3618
3619 for (; t; t = TREE_CHAIN (t))
3620 {
3621 tree d = TREE_VALUE (t);
3622
3623 if (TREE_CODE (d) == TYPE_DECL)
3624 /* Already injected. */;
3625 else
3626 pushdecl (d);
3627 }
3628 }
3629
3630 for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
3631 if (TREE_CODE (t) == FIELD_DECL)
3632 {
3633 TREE_TYPE (t) = complete_type (TREE_TYPE (t));
3634 require_complete_type (t);
3635 }
3636
3637 type = finish_struct_1 (type, 0);
3638 CLASSTYPE_GOT_SEMICOLON (type) = 1;
3639
3640 repo_template_used (type);
3641 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
3642 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
3643
3644 end:
3645 TYPE_BEING_DEFINED (type) = 0;
3646 popclass (0);
3647
3648 pop_from_top_level ();
3649 pop_tinst_level ();
3650
3651 return type;
3652 }
3653
3654 static int
3655 list_eq (t1, t2)
3656 tree t1, t2;
3657 {
3658 if (t1 == NULL_TREE)
3659 return t2 == NULL_TREE;
3660 if (t2 == NULL_TREE)
3661 return 0;
3662 /* Don't care if one declares its arg const and the other doesn't -- the
3663 main variant of the arg type is all that matters. */
3664 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
3665 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
3666 return 0;
3667 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
3668 }
3669
3670 tree
3671 lookup_nested_type_by_name (ctype, name)
3672 tree ctype, name;
3673 {
3674 tree t;
3675
3676 complete_type (ctype);
3677
3678 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
3679 {
3680 if (name == TREE_PURPOSE (t)
3681 /* this catches typedef enum { foo } bar; */
3682 || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
3683 return TREE_VALUE (t);
3684 }
3685 return NULL_TREE;
3686 }
3687
3688 /* If arg is a non-type template parameter that does not depend on template
3689 arguments, fold it like we weren't in the body of a template. */
3690
3691 static tree
3692 maybe_fold_nontype_arg (arg)
3693 tree arg;
3694 {
3695 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
3696 && !uses_template_parms (arg))
3697 {
3698 /* Sometimes, one of the args was an expression involving a
3699 template constant parameter, like N - 1. Now that we've
3700 tsubst'd, we might have something like 2 - 1. This will
3701 confuse lookup_template_class, so we do constant folding
3702 here. We have to unset processing_template_decl, to
3703 fool build_expr_from_tree() into building an actual
3704 tree. */
3705
3706 int saved_processing_template_decl = processing_template_decl;
3707 processing_template_decl = 0;
3708 arg = fold (build_expr_from_tree (arg));
3709 processing_template_decl = saved_processing_template_decl;
3710 }
3711 return arg;
3712 }
3713
3714 /* Return the TREE_VEC with the arguments for the innermost template header,
3715 where ARGS is either that or the VEC of VECs for all the arguments.
3716
3717 If is_spec, then we are dealing with a specialization of a member
3718 template, and want the second-innermost args, the innermost ones that
3719 are instantiated. */
3720
3721 tree
3722 innermost_args (args, is_spec)
3723 tree args;
3724 int is_spec;
3725 {
3726 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
3727 return TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1 - is_spec);
3728 return args;
3729 }
3730
3731 /* Take the tree structure T and replace template parameters used therein
3732 with the argument vector ARGS. IN_DECL is an associated decl for
3733 diagnostics.
3734
3735 tsubst is used for dealing with types, decls and the like; for
3736 expressions, use tsubst_expr or tsubst_copy. */
3737
3738 tree
3739 tsubst (t, args, in_decl)
3740 tree t, args;
3741 tree in_decl;
3742 {
3743 tree type;
3744
3745 if (t == NULL_TREE || t == error_mark_node
3746 || t == integer_type_node
3747 || t == void_type_node
3748 || t == char_type_node)
3749 return t;
3750
3751 type = TREE_TYPE (t);
3752 if (type == unknown_type_node)
3753 my_friendly_abort (42);
3754 if (type && TREE_CODE (t) != FUNCTION_DECL
3755 && TREE_CODE (t) != TYPENAME_TYPE
3756 && TREE_CODE (t) != TEMPLATE_DECL)
3757 type = tsubst (type, args, in_decl);
3758
3759 switch (TREE_CODE (t))
3760 {
3761 case RECORD_TYPE:
3762 if (TYPE_PTRMEMFUNC_P (t))
3763 {
3764 tree r = build_ptrmemfunc_type
3765 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, in_decl));
3766 return cp_build_type_variant (r, TYPE_READONLY (t),
3767 TYPE_VOLATILE (t));
3768 }
3769
3770 /* else fall through */
3771 case UNION_TYPE:
3772 if (uses_template_parms (t))
3773 {
3774 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl);
3775 tree context;
3776 tree r;
3777
3778 context =
3779 TYPE_CONTEXT (t)
3780 ? tsubst (TYPE_CONTEXT (t), args, in_decl) : NULL_TREE;
3781
3782 r = lookup_template_class (t, argvec, in_decl, context);
3783
3784 return cp_build_type_variant (r, TYPE_READONLY (t),
3785 TYPE_VOLATILE (t));
3786 }
3787
3788 /* else fall through */
3789 case ERROR_MARK:
3790 case IDENTIFIER_NODE:
3791 case OP_IDENTIFIER:
3792 case VOID_TYPE:
3793 case REAL_TYPE:
3794 case COMPLEX_TYPE:
3795 case BOOLEAN_TYPE:
3796 case INTEGER_CST:
3797 case REAL_CST:
3798 case STRING_CST:
3799 return t;
3800
3801 case ENUMERAL_TYPE:
3802 {
3803 tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl);
3804 if (ctx == NULL_TREE)
3805 return t;
3806 else if (ctx == current_function_decl)
3807 return lookup_name (TYPE_IDENTIFIER (t), 1);
3808 else
3809 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
3810 }
3811
3812 case INTEGER_TYPE:
3813 if (t == integer_type_node)
3814 return t;
3815
3816 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
3817 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
3818 return t;
3819
3820 {
3821 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
3822 max = tsubst_expr (max, args, in_decl);
3823 if (processing_template_decl)
3824 {
3825 tree itype = make_node (INTEGER_TYPE);
3826 TYPE_MIN_VALUE (itype) = size_zero_node;
3827 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
3828 integer_one_node);
3829 return itype;
3830 }
3831
3832 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
3833 return build_index_2_type (size_zero_node, max);
3834 }
3835
3836 case TEMPLATE_TYPE_PARM:
3837 case TEMPLATE_TEMPLATE_PARM:
3838 case TEMPLATE_PARM_INDEX:
3839 {
3840 int idx;
3841 int level;
3842 int levels;
3843 tree r = NULL_TREE;
3844
3845 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
3846 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
3847 {
3848 idx = TEMPLATE_TYPE_IDX (t);
3849 level = TEMPLATE_TYPE_LEVEL (t);
3850 }
3851 else
3852 {
3853 idx = TEMPLATE_PARM_IDX (t);
3854 level = TEMPLATE_PARM_LEVEL (t);
3855 }
3856
3857 if (TREE_VEC_LENGTH (args) > 0)
3858 {
3859 tree arg = NULL_TREE;
3860
3861 if (TREE_VEC_ELT (args, 0) != NULL_TREE
3862 && TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
3863 {
3864 levels = TREE_VEC_LENGTH (args);
3865 if (level <= levels)
3866 arg = TREE_VEC_ELT
3867 (TREE_VEC_ELT (args, level - 1), idx);
3868 }
3869 else
3870 {
3871 levels = 1;
3872 if (level == 1)
3873 arg = TREE_VEC_ELT (args, idx);
3874 }
3875
3876 if (arg != NULL_TREE)
3877 {
3878 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
3879 return cp_build_type_variant
3880 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
3881 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
3882 else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
3883 {
3884 if (CLASSTYPE_TEMPLATE_INFO (t))
3885 {
3886 /* We are processing a type constructed from
3887 a template template parameter */
3888 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t),
3889 args, in_decl);
3890 tree r;
3891
3892 /* We can get a TEMPLATE_TEMPLATE_PARM here when
3893 we are resolving nested-types in the signature of
3894 a member function templates.
3895 Otherwise ARG is a TEMPLATE_DECL and is the real
3896 template to be instantiated. */
3897 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
3898 arg = TYPE_NAME (arg);
3899
3900 r = lookup_template_class (DECL_NAME (arg),
3901 argvec, in_decl,
3902 DECL_CONTEXT (arg));
3903 return cp_build_type_variant (r, TYPE_READONLY (t),
3904 TYPE_VOLATILE (t));
3905 }
3906 else
3907 /* We are processing a template argument list. */
3908 return arg;
3909 }
3910 else
3911 return arg;
3912 }
3913 }
3914
3915 if (level == 1)
3916 /* This can happen during the attempted tsubst'ing in
3917 unify. This means that we don't yet have any information
3918 about the template parameter in question. */
3919 return t;
3920
3921 /* If we get here, we must have been looking at a parm for a
3922 more deeply nested template. Make a new version of this
3923 template parameter, but with a lower level. */
3924 switch (TREE_CODE (t))
3925 {
3926 case TEMPLATE_TYPE_PARM:
3927 case TEMPLATE_TEMPLATE_PARM:
3928 r = copy_node (t);
3929 TEMPLATE_TYPE_PARM_INDEX (r)
3930 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
3931 r, levels);
3932 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
3933 TYPE_MAIN_VARIANT (r) = r;
3934 TYPE_POINTER_TO (r) = NULL_TREE;
3935 TYPE_REFERENCE_TO (r) = NULL_TREE;
3936
3937 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
3938 && CLASSTYPE_TEMPLATE_INFO (t))
3939 {
3940 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, in_decl);
3941 CLASSTYPE_TEMPLATE_INFO (r)
3942 = perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
3943 }
3944 break;
3945
3946 case TEMPLATE_PARM_INDEX:
3947 r = reduce_template_parm_level (t, TREE_TYPE (t), levels);
3948 break;
3949
3950 default:
3951 my_friendly_abort (0);
3952 }
3953
3954 return r;
3955 }
3956
3957 case TEMPLATE_DECL:
3958 {
3959 /* We can get here when processing a member template function
3960 of a template class. */
3961 tree tmpl;
3962 tree decl = DECL_TEMPLATE_RESULT (t);
3963 tree parms;
3964 tree* new_parms;
3965 tree spec;
3966 int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
3967
3968 if (!is_template_template_parm)
3969 {
3970 /* We might already have an instance of this template. */
3971 spec = retrieve_specialization (t, args);
3972 if (spec != NULL_TREE)
3973 return spec;
3974 }
3975
3976 /* Make a new template decl. It will be similar to the
3977 original, but will record the current template arguments.
3978 We also create a new function declaration, which is just
3979 like the old one, but points to this new template, rather
3980 than the old one. */
3981 tmpl = copy_node (t);
3982 copy_lang_decl (tmpl);
3983 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0);
3984 DECL_CHAIN (tmpl) = NULL_TREE;
3985 TREE_CHAIN (tmpl) = NULL_TREE;
3986
3987 if (is_template_template_parm)
3988 {
3989 tree new_decl = tsubst (decl, args, in_decl);
3990 DECL_RESULT (tmpl) = new_decl;
3991 TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
3992 return tmpl;
3993 }
3994
3995 DECL_CONTEXT (tmpl) = tsubst (DECL_CONTEXT (t),
3996 args, in_decl);
3997 DECL_CLASS_CONTEXT (tmpl) = tsubst (DECL_CLASS_CONTEXT (t),
3998 args, in_decl);
3999 DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
4000
4001 if (TREE_CODE (decl) == TYPE_DECL)
4002 {
4003 tree new_type = tsubst (TREE_TYPE (t), args, in_decl);
4004 TREE_TYPE (tmpl) = new_type;
4005 CLASSTYPE_TI_TEMPLATE (new_type) = tmpl;
4006 DECL_RESULT (tmpl) = TYPE_MAIN_DECL (new_type);
4007 }
4008 else
4009 {
4010 tree new_decl = tsubst (decl, args, in_decl);
4011 DECL_RESULT (tmpl) = new_decl;
4012 DECL_TI_TEMPLATE (new_decl) = tmpl;
4013 TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
4014 }
4015
4016 DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
4017 SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
4018
4019 /* The template parameters for this new template are all the
4020 template parameters for the old template, except the
4021 outermost level of parameters. */
4022 for (new_parms = &DECL_TEMPLATE_PARMS (tmpl),
4023 parms = DECL_TEMPLATE_PARMS (t);
4024 TREE_CHAIN (parms) != NULL_TREE;
4025 new_parms = &(TREE_CHAIN (*new_parms)),
4026 parms = TREE_CHAIN (parms))
4027 {
4028 tree new_vec =
4029 make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
4030 int i;
4031
4032 for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
4033 {
4034 tree default_value =
4035 TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
4036 tree parm_decl =
4037 TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
4038
4039 TREE_VEC_ELT (new_vec, i)
4040 = build_tree_list (tsubst (default_value, args, in_decl),
4041 tsubst (parm_decl, args, in_decl));
4042
4043 }
4044
4045 *new_parms =
4046 tree_cons (build_int_2 (0,
4047 TREE_INT_CST_HIGH
4048 (TREE_PURPOSE (parms)) - 1),
4049 new_vec,
4050 NULL_TREE);
4051 }
4052
4053 if (PRIMARY_TEMPLATE_P (t))
4054 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
4055
4056 /* We don't partially instantiate partial specializations. */
4057 if (TREE_CODE (decl) == TYPE_DECL)
4058 return tmpl;
4059
4060 /* What should we do with the specializations of this member
4061 template? Are they specializations of this new template,
4062 or instantiations of the templates they previously were?
4063 this new template? And where should their
4064 DECL_TI_TEMPLATES point? */
4065 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
4066 for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
4067 spec != NULL_TREE;
4068 spec = TREE_CHAIN (spec))
4069 {
4070 /* It helps to consider example here. Consider:
4071
4072 template <class T>
4073 struct S {
4074 template <class U>
4075 void f(U u);
4076
4077 template <>
4078 void f(T* t) {}
4079 };
4080
4081 Now, for example, we are instantiating S<int>::f(U u).
4082 We want to make a template:
4083
4084 template <class U>
4085 void S<int>::f(U);
4086
4087 It will have a specialization, for the case U = int*, of
4088 the form:
4089
4090 template <>
4091 void S<int>::f<int*>(int*);
4092
4093 This specialization will be an instantiation of
4094 the specialization given in the declaration of S, with
4095 argument list int*. */
4096
4097 tree fn = TREE_VALUE (spec);
4098 tree spec_args;
4099 tree new_fn;
4100
4101 if (!DECL_TEMPLATE_SPECIALIZATION (fn))
4102 /* Instantiations are on the same list, but they're of
4103 no concern to us. */
4104 continue;
4105
4106 spec_args = tsubst (DECL_TI_ARGS (fn), args,
4107 in_decl);
4108 new_fn = tsubst (DECL_RESULT (fn), args,
4109 in_decl);
4110 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) =
4111 perm_tree_cons (spec_args, new_fn,
4112 DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
4113 }
4114
4115 /* Record this partial instantiation. */
4116 register_specialization (tmpl, t, args);
4117
4118 return tmpl;
4119 }
4120
4121 case FUNCTION_DECL:
4122 {
4123 tree r = NULL_TREE;
4124 tree ctx;
4125 tree argvec;
4126 tree tmpl = NULL_TREE;
4127 int member;
4128
4129 if (DECL_CONTEXT (t) != NULL_TREE
4130 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
4131 {
4132 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
4133 member = 2;
4134 else
4135 member = 1;
4136 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, t);
4137 type = tsubst (type, args, in_decl);
4138 }
4139 else
4140 {
4141 member = 0;
4142 ctx = NULL_TREE;
4143 type = tsubst (type, args, in_decl);
4144 }
4145
4146 /* If we are instantiating a specialization, get the other args. */
4147 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
4148 {
4149 tree spec;
4150
4151 tmpl = DECL_TI_TEMPLATE (t);
4152
4153 /* Start by getting the innermost args. */
4154 argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
4155
4156 if (DECL_TEMPLATE_INFO (tmpl))
4157 argvec = complete_template_args (tmpl, argvec, 0);
4158
4159 /* Do we already have this instantiation? */
4160 spec = retrieve_specialization (tmpl, argvec);
4161 if (spec)
4162 return spec;
4163 }
4164
4165 /* We do NOT check for matching decls pushed separately at this
4166 point, as they may not represent instantiations of this
4167 template, and in any case are considered separate under the
4168 discrete model. Instead, see add_maybe_template. */
4169
4170 r = copy_node (t);
4171 copy_lang_decl (r);
4172 DECL_USE_TEMPLATE (r) = 0;
4173 TREE_TYPE (r) = type;
4174
4175 DECL_CONTEXT (r)
4176 = tsubst (DECL_CONTEXT (t), args, t);
4177 DECL_CLASS_CONTEXT (r) = ctx;
4178
4179 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
4180 IDENTIFIER_POINTER (DECL_NAME (r)),
4181 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
4182 {
4183 /* Type-conversion operator. Reconstruct the name, in
4184 case it's the name of one of the template's parameters. */
4185 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
4186 }
4187
4188 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
4189 {
4190 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
4191 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
4192 buf = (char *) alloca (strlen (dbuf)
4193 + sizeof (DESTRUCTOR_DECL_PREFIX));
4194 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
4195 buf[len] = '\0';
4196 strcat (buf, dbuf);
4197 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
4198 }
4199 else
4200 {
4201 /* Instantiations of template functions must be mangled
4202 specially, in order to conform to 14.5.5.1
4203 [temp.over.link]. We use in_decl below rather than
4204 DECL_TI_TEMPLATE (r) because the latter is set to
4205 NULL_TREE in instantiate_decl. */
4206 tree tmpl;
4207 tree arg_types;
4208
4209 if (DECL_TEMPLATE_INFO (r))
4210 tmpl = DECL_TI_TEMPLATE (r);
4211 else
4212 tmpl = in_decl;
4213
4214 /* tmpl will be NULL if this is a specialization of a
4215 member function of a template class. */
4216 if (name_mangling_version < 1
4217 || tmpl == NULL_TREE
4218 || (member && !is_member_template (tmpl)
4219 && !DECL_TEMPLATE_INFO (tmpl)))
4220 {
4221 arg_types = TYPE_ARG_TYPES (type);
4222 if (member && TREE_CODE (type) == FUNCTION_TYPE)
4223 arg_types = hash_tree_chain
4224 (build_pointer_type (DECL_CONTEXT (r)),
4225 arg_types);
4226
4227 DECL_ASSEMBLER_NAME (r)
4228 = build_decl_overload (DECL_NAME (r), arg_types,
4229 member);
4230 }
4231 else
4232 {
4233 tree tparms;
4234 tree targs;
4235
4236 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
4237 {
4238 /* We pass the outermost template parameters to
4239 build_template_decl_overload, since the innermost
4240 template parameters are still just template
4241 parameters; there are no corresponding subsitution
4242 arguments. Levels of parms that have been bound
4243 before are not represented in DECL_TEMPLATE_PARMS. */
4244 tparms = DECL_TEMPLATE_PARMS (tmpl);
4245 while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
4246 tparms = TREE_CHAIN (tparms);
4247
4248 targs = innermost_args (args, 0);
4249 }
4250 else
4251 {
4252 /* If the template is a specialization, then it is
4253 a member template specialization. We have
4254 something like:
4255
4256 template <class T> struct S {
4257 template <int i> void f();
4258 template <> void f<7>();
4259 };
4260
4261 and now we are forming S<double>::f<7>.
4262 Therefore, the template parameters of interest
4263 are those that are specialized by the template
4264 (i.e., the int), not those we are using to
4265 instantiate the template, i.e. the double. */
4266 tparms = DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (tmpl));
4267 targs = DECL_TI_ARGS (tmpl);
4268 }
4269
4270 my_friendly_assert (tparms != NULL_TREE
4271 && TREE_CODE (tparms) == TREE_LIST,
4272 0);
4273 tparms = TREE_VALUE (tparms);
4274
4275 arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
4276 if (member && TREE_CODE (type) == FUNCTION_TYPE)
4277 arg_types = hash_tree_chain
4278 (build_pointer_type (DECL_CONTEXT (r)),
4279 arg_types);
4280
4281 DECL_ASSEMBLER_NAME (r)
4282 = build_template_decl_overload
4283 (DECL_NAME (r), arg_types,
4284 TREE_TYPE (TREE_TYPE (tmpl)),
4285 tparms, targs, member);
4286 }
4287 }
4288 DECL_RTL (r) = 0;
4289 make_decl_rtl (r, NULL_PTR, 1);
4290
4291 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, t);
4292 DECL_MAIN_VARIANT (r) = r;
4293 DECL_RESULT (r) = NULL_TREE;
4294 DECL_INITIAL (r) = NULL_TREE;
4295
4296 TREE_STATIC (r) = 0;
4297 TREE_PUBLIC (r) = TREE_PUBLIC (t);
4298 DECL_EXTERNAL (r) = 1;
4299 DECL_INTERFACE_KNOWN (r) = 0;
4300 DECL_DEFER_OUTPUT (r) = 0;
4301 TREE_CHAIN (r) = NULL_TREE;
4302 DECL_CHAIN (r) = NULL_TREE;
4303 DECL_PENDING_INLINE_INFO (r) = 0;
4304 TREE_USED (r) = 0;
4305
4306 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
4307 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
4308
4309 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
4310 {
4311 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
4312
4313 /* If we're not using ANSI overloading, then we might have
4314 called duplicate_decls above, and gotten back an
4315 preexisting version of this function. We treat such a
4316 function as a specialization. Otherwise, we cleared
4317 both TREE_STATIC and DECL_TEMPLATE_SPECIALIZATION, so
4318 this condition will be false. */
4319 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
4320 SET_DECL_TEMPLATE_SPECIALIZATION (r);
4321 else
4322 SET_DECL_IMPLICIT_INSTANTIATION (r);
4323
4324 register_specialization (r, tmpl, argvec);
4325 }
4326
4327 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
4328 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
4329 if (member
4330 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
4331 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
4332
4333 return r;
4334 }
4335
4336 case PARM_DECL:
4337 {
4338 tree r = copy_node (t);
4339 TREE_TYPE (r) = type;
4340 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
4341 DECL_INITIAL (r) = TREE_TYPE (r);
4342 else
4343 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args, in_decl);
4344
4345 DECL_CONTEXT (r) = NULL_TREE;
4346 #ifdef PROMOTE_PROTOTYPES
4347 if ((TREE_CODE (type) == INTEGER_TYPE
4348 || TREE_CODE (type) == ENUMERAL_TYPE)
4349 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
4350 DECL_ARG_TYPE (r) = integer_type_node;
4351 #endif
4352 if (TREE_CHAIN (t))
4353 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, TREE_CHAIN (t));
4354 return r;
4355 }
4356
4357 case FIELD_DECL:
4358 {
4359 tree r = copy_node (t);
4360 TREE_TYPE (r) = type;
4361 copy_lang_decl (r);
4362 #if 0
4363 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, in_decl);
4364 #endif
4365 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, in_decl);
4366 TREE_CHAIN (r) = NULL_TREE;
4367 return r;
4368 }
4369
4370 case USING_DECL:
4371 {
4372 tree r = copy_node (t);
4373 DECL_INITIAL (r)
4374 = tsubst_copy (DECL_INITIAL (t), args, in_decl);
4375 TREE_CHAIN (r) = NULL_TREE;
4376 return r;
4377 }
4378
4379 case VAR_DECL:
4380 {
4381 tree r;
4382 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, in_decl);
4383
4384 /* Do we already have this instantiation? */
4385 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
4386 {
4387 tree tmpl = DECL_TI_TEMPLATE (t);
4388 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
4389
4390 for (; decls; decls = TREE_CHAIN (decls))
4391 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
4392 return TREE_VALUE (decls);
4393 }
4394
4395 r = copy_node (t);
4396 TREE_TYPE (r) = type;
4397 DECL_CONTEXT (r) = ctx;
4398 if (TREE_STATIC (r))
4399 DECL_ASSEMBLER_NAME (r)
4400 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
4401
4402 /* Don't try to expand the initializer until someone tries to use
4403 this variable; otherwise we run into circular dependencies. */
4404 DECL_INITIAL (r) = NULL_TREE;
4405
4406 DECL_RTL (r) = 0;
4407 DECL_SIZE (r) = 0;
4408
4409 if (DECL_LANG_SPECIFIC (r))
4410 {
4411 copy_lang_decl (r);
4412 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
4413 }
4414
4415 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
4416 {
4417 tree tmpl = DECL_TI_TEMPLATE (t);
4418 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
4419 tree argvec = tsubst (DECL_TI_ARGS (t), args, in_decl);
4420
4421 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
4422 *declsp = perm_tree_cons (argvec, r, *declsp);
4423 SET_DECL_IMPLICIT_INSTANTIATION (r);
4424 }
4425 TREE_CHAIN (r) = NULL_TREE;
4426 return r;
4427 }
4428
4429 case TYPE_DECL:
4430 if (t == TYPE_NAME (TREE_TYPE (t)))
4431 return TYPE_NAME (type);
4432
4433 {
4434 tree r = copy_node (t);
4435 TREE_TYPE (r) = type;
4436 DECL_CONTEXT (r) = current_class_type;
4437 TREE_CHAIN (r) = NULL_TREE;
4438 return r;
4439 }
4440
4441 case TREE_LIST:
4442 {
4443 tree purpose, value, chain, result;
4444 int via_public, via_virtual, via_protected;
4445
4446 if (t == void_list_node)
4447 return t;
4448
4449 via_public = TREE_VIA_PUBLIC (t);
4450 via_protected = TREE_VIA_PROTECTED (t);
4451 via_virtual = TREE_VIA_VIRTUAL (t);
4452
4453 purpose = TREE_PURPOSE (t);
4454 if (purpose)
4455 purpose = tsubst (purpose, args, in_decl);
4456 value = TREE_VALUE (t);
4457 if (value)
4458 value = tsubst (value, args, in_decl);
4459 chain = TREE_CHAIN (t);
4460 if (chain && chain != void_type_node)
4461 chain = tsubst (chain, args, in_decl);
4462 if (purpose == TREE_PURPOSE (t)
4463 && value == TREE_VALUE (t)
4464 && chain == TREE_CHAIN (t))
4465 return t;
4466 result = hash_tree_cons (via_public, via_virtual, via_protected,
4467 purpose, value, chain);
4468 TREE_PARMLIST (result) = TREE_PARMLIST (t);
4469 return result;
4470 }
4471 case TREE_VEC:
4472 if (type != NULL_TREE)
4473 {
4474 /* A binfo node. */
4475
4476 t = copy_node (t);
4477
4478 if (type == TREE_TYPE (t))
4479 return t;
4480
4481 TREE_TYPE (t) = complete_type (type);
4482 if (IS_AGGR_TYPE (type))
4483 {
4484 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
4485 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
4486 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
4487 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
4488 }
4489 return t;
4490 }
4491
4492 /* Otherwise, a vector of template arguments. */
4493 {
4494 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
4495 tree *elts = (tree *) alloca (len * sizeof (tree));
4496
4497 bzero ((char *) elts, len * sizeof (tree));
4498
4499 for (i = 0; i < len; i++)
4500 {
4501 elts[i] = maybe_fold_nontype_arg
4502 (tsubst_expr (TREE_VEC_ELT (t, i), args, in_decl));
4503
4504 if (elts[i] != TREE_VEC_ELT (t, i))
4505 need_new = 1;
4506 }
4507
4508 if (!need_new)
4509 return t;
4510
4511 t = make_tree_vec (len);
4512 for (i = 0; i < len; i++)
4513 TREE_VEC_ELT (t, i) = elts[i];
4514
4515 return t;
4516 }
4517 case POINTER_TYPE:
4518 case REFERENCE_TYPE:
4519 {
4520 tree r;
4521 enum tree_code code;
4522
4523 if (type == TREE_TYPE (t))
4524 return t;
4525
4526 code = TREE_CODE (t);
4527 if (TREE_CODE (type) == REFERENCE_TYPE)
4528 {
4529 static int last_line = 0;
4530 static char* last_file = 0;
4531
4532 /* We keep track of the last time we issued this error
4533 message to avoid spewing a ton of messages during a
4534 single bad template instantiation. */
4535 if (last_line != lineno ||
4536 last_file != input_filename)
4537 {
4538 cp_error ("cannot form type %s to reference type %T during template instantiation",
4539 (code == POINTER_TYPE) ? "pointer" : "reference",
4540 type);
4541 last_line = lineno;
4542 last_file = input_filename;
4543 }
4544
4545 /* Use the underlying type in an attempt at error
4546 recovery; maybe the user meant vector<int> and wrote
4547 vector<int&>, or some such. */
4548 if (code == REFERENCE_TYPE)
4549 r = type;
4550 else
4551 r = build_pointer_type (TREE_TYPE (type));
4552 }
4553 else if (code == POINTER_TYPE)
4554 r = build_pointer_type (type);
4555 else
4556 r = build_reference_type (type);
4557 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
4558
4559 /* Will this ever be needed for TYPE_..._TO values? */
4560 layout_type (r);
4561 return r;
4562 }
4563 case OFFSET_TYPE:
4564 return build_offset_type
4565 (tsubst (TYPE_OFFSET_BASETYPE (t), args, in_decl), type);
4566 case FUNCTION_TYPE:
4567 case METHOD_TYPE:
4568 {
4569 tree values = TYPE_ARG_TYPES (t);
4570 tree context = TYPE_CONTEXT (t);
4571 tree raises = TYPE_RAISES_EXCEPTIONS (t);
4572 tree fntype;
4573
4574 /* Don't bother recursing if we know it won't change anything. */
4575 if (values != void_list_node)
4576 {
4577 /* This should probably be rewritten to use hash_tree_cons for
4578 the memory savings. */
4579 tree first = NULL_TREE;
4580 tree last = NULL_TREE;
4581
4582 for (; values && values != void_list_node;
4583 values = TREE_CHAIN (values))
4584 {
4585 tree value = TYPE_MAIN_VARIANT (type_decays_to
4586 (tsubst (TREE_VALUE (values), args, in_decl)));
4587 /* Don't instantiate default args unless they are used.
4588 Handle it in build_over_call instead. */
4589 tree purpose = TREE_PURPOSE (values);
4590 tree x = build_tree_list (purpose, value);
4591
4592 if (first)
4593 TREE_CHAIN (last) = x;
4594 else
4595 first = x;
4596 last = x;
4597 }
4598
4599 if (values == void_list_node)
4600 TREE_CHAIN (last) = void_list_node;
4601
4602 values = first;
4603 }
4604 if (context)
4605 context = tsubst (context, args, in_decl);
4606 /* Could also optimize cases where return value and
4607 values have common elements (e.g., T min(const &T, const T&). */
4608
4609 /* If the above parameters haven't changed, just return the type. */
4610 if (type == TREE_TYPE (t)
4611 && values == TYPE_VALUES (t)
4612 && context == TYPE_CONTEXT (t))
4613 return t;
4614
4615 /* Construct a new type node and return it. */
4616 if (TREE_CODE (t) == FUNCTION_TYPE
4617 && context == NULL_TREE)
4618 {
4619 fntype = build_function_type (type, values);
4620 }
4621 else if (context == NULL_TREE)
4622 {
4623 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
4624 args, in_decl);
4625 fntype = build_cplus_method_type (base, type,
4626 TREE_CHAIN (values));
4627 }
4628 else
4629 {
4630 fntype = make_node (TREE_CODE (t));
4631 TREE_TYPE (fntype) = type;
4632 TYPE_CONTEXT (fntype) = context;
4633 TYPE_VALUES (fntype) = values;
4634 TYPE_SIZE (fntype) = TYPE_SIZE (t);
4635 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
4636 TYPE_MODE (fntype) = TYPE_MODE (t);
4637 if (TYPE_METHOD_BASETYPE (t))
4638 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
4639 args, in_decl);
4640 /* Need to generate hash value. */
4641 my_friendly_abort (84);
4642 }
4643 fntype = build_type_variant (fntype,
4644 TYPE_READONLY (t),
4645 TYPE_VOLATILE (t));
4646 if (raises)
4647 {
4648 raises = tsubst (raises, args, in_decl);
4649 fntype = build_exception_variant (fntype, raises);
4650 }
4651 return fntype;
4652 }
4653 case ARRAY_TYPE:
4654 {
4655 tree domain = tsubst (TYPE_DOMAIN (t), args, in_decl);
4656 tree r;
4657 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
4658 return t;
4659 r = build_cplus_array_type (type, domain);
4660 return r;
4661 }
4662
4663 case PLUS_EXPR:
4664 case MINUS_EXPR:
4665 return fold (build (TREE_CODE (t), TREE_TYPE (t),
4666 tsubst (TREE_OPERAND (t, 0), args, in_decl),
4667 tsubst (TREE_OPERAND (t, 1), args, in_decl)));
4668
4669 case NEGATE_EXPR:
4670 case NOP_EXPR:
4671 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
4672 tsubst (TREE_OPERAND (t, 0), args, in_decl)));
4673
4674 case TYPENAME_TYPE:
4675 {
4676 tree ctx = tsubst (TYPE_CONTEXT (t), args, in_decl);
4677 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args, in_decl);
4678 f = make_typename_type (ctx, f);
4679 return cp_build_type_variant
4680 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
4681 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
4682 }
4683
4684 case INDIRECT_REF:
4685 return make_pointer_declarator
4686 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
4687
4688 case ADDR_EXPR:
4689 return make_reference_declarator
4690 (type, tsubst (TREE_OPERAND (t, 0), args, in_decl));
4691
4692 case ARRAY_REF:
4693 return build_parse_node
4694 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, in_decl),
4695 tsubst_expr (TREE_OPERAND (t, 1), args, in_decl));
4696
4697 case CALL_EXPR:
4698 return make_call_declarator
4699 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
4700 tsubst (TREE_OPERAND (t, 1), args, in_decl),
4701 TREE_OPERAND (t, 2),
4702 tsubst (TREE_TYPE (t), args, in_decl));
4703
4704 case SCOPE_REF:
4705 return build_parse_node
4706 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, in_decl),
4707 tsubst (TREE_OPERAND (t, 1), args, in_decl));
4708
4709 default:
4710 sorry ("use of `%s' in template",
4711 tree_code_name [(int) TREE_CODE (t)]);
4712 return error_mark_node;
4713 }
4714 }
4715
4716 void
4717 do_pushlevel ()
4718 {
4719 emit_line_note (input_filename, lineno);
4720 pushlevel (0);
4721 clear_last_expr ();
4722 push_momentary ();
4723 expand_start_bindings (0);
4724 }
4725
4726 tree
4727 do_poplevel ()
4728 {
4729 tree t;
4730 int saved_warn_unused = 0;
4731
4732 if (processing_template_decl)
4733 {
4734 saved_warn_unused = warn_unused;
4735 warn_unused = 0;
4736 }
4737 expand_end_bindings (getdecls (), kept_level_p (), 0);
4738 if (processing_template_decl)
4739 warn_unused = saved_warn_unused;
4740 t = poplevel (kept_level_p (), 1, 0);
4741 pop_momentary ();
4742 return t;
4743 }
4744
4745 /* Like tsubst, but deals with expressions. This function just replaces
4746 template parms; to finish processing the resultant expression, use
4747 tsubst_expr. */
4748
4749 tree
4750 tsubst_copy (t, args, in_decl)
4751 tree t, args;
4752 tree in_decl;
4753 {
4754 enum tree_code code;
4755
4756 if (t == NULL_TREE || t == error_mark_node)
4757 return t;
4758
4759 code = TREE_CODE (t);
4760
4761 switch (code)
4762 {
4763 case PARM_DECL:
4764 return do_identifier (DECL_NAME (t), 0);
4765
4766 case CONST_DECL:
4767 case FIELD_DECL:
4768 if (DECL_CONTEXT (t))
4769 {
4770 tree ctx;
4771 if (TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
4772 return lookup_name (DECL_NAME (t), 0);
4773
4774 ctx = tsubst (DECL_CONTEXT (t), args, in_decl);
4775 if (ctx != DECL_CONTEXT (t))
4776 return lookup_field (ctx, DECL_NAME (t), 0, 0);
4777 }
4778 return t;
4779
4780 case VAR_DECL:
4781 case FUNCTION_DECL:
4782 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
4783 t = tsubst (t, args, in_decl);
4784 mark_used (t);
4785 return t;
4786
4787 case TEMPLATE_DECL:
4788 if (is_member_template (t))
4789 return tsubst (t, args, in_decl);
4790 else
4791 return t;
4792
4793 #if 0
4794 case IDENTIFIER_NODE:
4795 return do_identifier (t, 0);
4796 #endif
4797
4798 case CAST_EXPR:
4799 case REINTERPRET_CAST_EXPR:
4800 case CONST_CAST_EXPR:
4801 case STATIC_CAST_EXPR:
4802 case DYNAMIC_CAST_EXPR:
4803 return build1
4804 (code, tsubst (TREE_TYPE (t), args, in_decl),
4805 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
4806
4807 case INDIRECT_REF:
4808 case PREDECREMENT_EXPR:
4809 case PREINCREMENT_EXPR:
4810 case POSTDECREMENT_EXPR:
4811 case POSTINCREMENT_EXPR:
4812 case NEGATE_EXPR:
4813 case TRUTH_NOT_EXPR:
4814 case BIT_NOT_EXPR:
4815 case ADDR_EXPR:
4816 case CONVERT_EXPR: /* Unary + */
4817 case SIZEOF_EXPR:
4818 case ALIGNOF_EXPR:
4819 case ARROW_EXPR:
4820 case THROW_EXPR:
4821 case TYPEID_EXPR:
4822 return build1
4823 (code, NULL_TREE,
4824 tsubst_copy (TREE_OPERAND (t, 0), args, in_decl));
4825
4826 case PLUS_EXPR:
4827 case MINUS_EXPR:
4828 case MULT_EXPR:
4829 case TRUNC_DIV_EXPR:
4830 case CEIL_DIV_EXPR:
4831 case FLOOR_DIV_EXPR:
4832 case ROUND_DIV_EXPR:
4833 case EXACT_DIV_EXPR:
4834 case BIT_AND_EXPR:
4835 case BIT_ANDTC_EXPR:
4836 case BIT_IOR_EXPR:
4837 case BIT_XOR_EXPR:
4838 case TRUNC_MOD_EXPR:
4839 case FLOOR_MOD_EXPR:
4840 case TRUTH_ANDIF_EXPR:
4841 case TRUTH_ORIF_EXPR:
4842 case TRUTH_AND_EXPR:
4843 case TRUTH_OR_EXPR:
4844 case RSHIFT_EXPR:
4845 case LSHIFT_EXPR:
4846 case RROTATE_EXPR:
4847 case LROTATE_EXPR:
4848 case EQ_EXPR:
4849 case NE_EXPR:
4850 case MAX_EXPR:
4851 case MIN_EXPR:
4852 case LE_EXPR:
4853 case GE_EXPR:
4854 case LT_EXPR:
4855 case GT_EXPR:
4856 case COMPONENT_REF:
4857 case ARRAY_REF:
4858 case COMPOUND_EXPR:
4859 case SCOPE_REF:
4860 case DOTSTAR_EXPR:
4861 case MEMBER_REF:
4862 return build_nt
4863 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
4864 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
4865
4866 case CALL_EXPR:
4867 {
4868 tree fn = TREE_OPERAND (t, 0);
4869 if (is_overloaded_fn (fn))
4870 fn = tsubst_copy (get_first_fn (fn), args, in_decl);
4871 else
4872 /* Sometimes FN is a LOOKUP_EXPR. */
4873 fn = tsubst_copy (fn, args, in_decl);
4874 return build_nt
4875 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
4876 NULL_TREE);
4877 }
4878
4879 case METHOD_CALL_EXPR:
4880 {
4881 tree name = TREE_OPERAND (t, 0);
4882 if (TREE_CODE (name) == BIT_NOT_EXPR)
4883 {
4884 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
4885 if (TREE_CODE (name) != IDENTIFIER_NODE)
4886 name = TYPE_MAIN_VARIANT (name);
4887 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
4888 }
4889 else if (TREE_CODE (name) == SCOPE_REF
4890 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
4891 {
4892 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
4893 name = TREE_OPERAND (name, 1);
4894 name = tsubst_copy (TREE_OPERAND (name, 0), args, in_decl);
4895 if (TREE_CODE (name) != IDENTIFIER_NODE)
4896 name = TYPE_MAIN_VARIANT (name);
4897 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
4898 name = build_nt (SCOPE_REF, base, name);
4899 }
4900 else
4901 name = tsubst_copy (TREE_OPERAND (t, 0), args, in_decl);
4902 return build_nt
4903 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
4904 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl),
4905 NULL_TREE);
4906 }
4907
4908 case BIND_EXPR:
4909 case COND_EXPR:
4910 case MODOP_EXPR:
4911 {
4912 tree r = build_nt
4913 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
4914 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
4915 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
4916
4917 if (code == BIND_EXPR && !processing_template_decl)
4918 {
4919 /* This processing should really occur in tsubst_expr,
4920 However, tsubst_expr does not recurse into expressions,
4921 since it assumes that there aren't any statements
4922 inside them. Instead, it simply calls
4923 build_expr_from_tree. So, we need to expand the
4924 BIND_EXPR here. */
4925 tree rtl_expr = begin_stmt_expr ();
4926 tree block = tsubst_expr (TREE_OPERAND (r, 1), args, in_decl);
4927 r = finish_stmt_expr (rtl_expr, block);
4928 }
4929
4930 return r;
4931 }
4932
4933 case NEW_EXPR:
4934 {
4935 tree r = build_nt
4936 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
4937 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl),
4938 tsubst_copy (TREE_OPERAND (t, 2), args, in_decl));
4939 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
4940 return r;
4941 }
4942
4943 case DELETE_EXPR:
4944 {
4945 tree r = build_nt
4946 (code, tsubst_copy (TREE_OPERAND (t, 0), args, in_decl),
4947 tsubst_copy (TREE_OPERAND (t, 1), args, in_decl));
4948 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
4949 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
4950 return r;
4951 }
4952
4953 case TEMPLATE_ID_EXPR:
4954 {
4955 /* Substituted template arguments */
4956 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, in_decl);
4957 tree chain;
4958 for (chain = targs; chain; chain = TREE_CHAIN (chain))
4959 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
4960
4961 return lookup_template_function
4962 (tsubst_copy (TREE_OPERAND (t, 0), args, in_decl), targs);
4963 }
4964
4965 case TREE_LIST:
4966 {
4967 tree purpose, value, chain;
4968
4969 if (t == void_list_node)
4970 return t;
4971
4972 purpose = TREE_PURPOSE (t);
4973 if (purpose)
4974 purpose = tsubst_copy (purpose, args, in_decl);
4975 value = TREE_VALUE (t);
4976 if (value)
4977 value = tsubst_copy (value, args, in_decl);
4978 chain = TREE_CHAIN (t);
4979 if (chain && chain != void_type_node)
4980 chain = tsubst_copy (chain, args, in_decl);
4981 if (purpose == TREE_PURPOSE (t)
4982 && value == TREE_VALUE (t)
4983 && chain == TREE_CHAIN (t))
4984 return t;
4985 return tree_cons (purpose, value, chain);
4986 }
4987
4988 case RECORD_TYPE:
4989 case UNION_TYPE:
4990 case ENUMERAL_TYPE:
4991 case INTEGER_TYPE:
4992 case TEMPLATE_TYPE_PARM:
4993 case TEMPLATE_TEMPLATE_PARM:
4994 case TEMPLATE_PARM_INDEX:
4995 case POINTER_TYPE:
4996 case REFERENCE_TYPE:
4997 case OFFSET_TYPE:
4998 case FUNCTION_TYPE:
4999 case METHOD_TYPE:
5000 case ARRAY_TYPE:
5001 case TYPENAME_TYPE:
5002 case TYPE_DECL:
5003 return tsubst (t, args, in_decl);
5004
5005 case IDENTIFIER_NODE:
5006 if (IDENTIFIER_TYPENAME_P (t))
5007 return build_typename_overload
5008 (tsubst (TREE_TYPE (t), args, in_decl));
5009 else
5010 return t;
5011
5012 case CONSTRUCTOR:
5013 return build
5014 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, in_decl), NULL_TREE,
5015 tsubst_copy (CONSTRUCTOR_ELTS (t), args, in_decl));
5016
5017 default:
5018 return t;
5019 }
5020 }
5021
5022 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
5023
5024 tree
5025 tsubst_expr (t, args, in_decl)
5026 tree t, args;
5027 tree in_decl;
5028 {
5029 if (t == NULL_TREE || t == error_mark_node)
5030 return t;
5031
5032 if (processing_template_decl)
5033 return tsubst_copy (t, args, in_decl);
5034
5035 switch (TREE_CODE (t))
5036 {
5037 case RETURN_STMT:
5038 lineno = TREE_COMPLEXITY (t);
5039 finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
5040 args, in_decl));
5041 break;
5042
5043 case EXPR_STMT:
5044 lineno = TREE_COMPLEXITY (t);
5045 finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
5046 args, in_decl));
5047 break;
5048
5049 case DECL_STMT:
5050 {
5051 int i = suspend_momentary ();
5052 tree dcl, init;
5053
5054 lineno = TREE_COMPLEXITY (t);
5055 emit_line_note (input_filename, lineno);
5056 dcl = start_decl
5057 (tsubst (TREE_OPERAND (t, 0), args, in_decl),
5058 tsubst (TREE_OPERAND (t, 1), args, in_decl),
5059 TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE);
5060 init = tsubst_expr (TREE_OPERAND (t, 2), args, in_decl);
5061 cp_finish_decl
5062 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
5063 resume_momentary (i);
5064 return dcl;
5065 }
5066
5067 case FOR_STMT:
5068 {
5069 tree tmp;
5070 lineno = TREE_COMPLEXITY (t);
5071
5072 begin_for_stmt ();
5073 for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
5074 tsubst_expr (tmp, args, in_decl);
5075 finish_for_init_stmt (NULL_TREE);
5076 finish_for_cond (tsubst_expr (FOR_COND (t), args,
5077 in_decl),
5078 NULL_TREE);
5079 tmp = tsubst_expr (FOR_EXPR (t), args, in_decl);
5080 finish_for_expr (tmp, NULL_TREE);
5081 tsubst_expr (FOR_BODY (t), args, in_decl);
5082 finish_for_stmt (tmp, NULL_TREE);
5083 }
5084 break;
5085
5086 case WHILE_STMT:
5087 {
5088 lineno = TREE_COMPLEXITY (t);
5089 begin_while_stmt ();
5090 finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
5091 args, in_decl),
5092 NULL_TREE);
5093 tsubst_expr (WHILE_BODY (t), args, in_decl);
5094 finish_while_stmt (NULL_TREE);
5095 }
5096 break;
5097
5098 case DO_STMT:
5099 {
5100 lineno = TREE_COMPLEXITY (t);
5101 begin_do_stmt ();
5102 tsubst_expr (DO_BODY (t), args, in_decl);
5103 finish_do_body (NULL_TREE);
5104 finish_do_stmt (tsubst_expr (DO_COND (t), args,
5105 in_decl),
5106 NULL_TREE);
5107 }
5108 break;
5109
5110 case IF_STMT:
5111 {
5112 tree tmp;
5113
5114 lineno = TREE_COMPLEXITY (t);
5115 begin_if_stmt ();
5116 finish_if_stmt_cond (tsubst_expr (IF_COND (t),
5117 args, in_decl),
5118 NULL_TREE);
5119
5120 if (tmp = THEN_CLAUSE (t), tmp)
5121 {
5122 tsubst_expr (tmp, args, in_decl);
5123 finish_then_clause (NULL_TREE);
5124 }
5125
5126 if (tmp = ELSE_CLAUSE (t), tmp)
5127 {
5128 begin_else_clause ();
5129 tsubst_expr (tmp, args, in_decl);
5130 finish_else_clause (NULL_TREE);
5131 }
5132
5133 finish_if_stmt ();
5134 }
5135 break;
5136
5137 case COMPOUND_STMT:
5138 {
5139 tree substmt;
5140
5141 lineno = TREE_COMPLEXITY (t);
5142 begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
5143 for (substmt = COMPOUND_BODY (t);
5144 substmt != NULL_TREE;
5145 substmt = TREE_CHAIN (substmt))
5146 tsubst_expr (substmt, args, in_decl);
5147 return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
5148 NULL_TREE);
5149 }
5150 break;
5151
5152 case BREAK_STMT:
5153 lineno = TREE_COMPLEXITY (t);
5154 finish_break_stmt ();
5155 break;
5156
5157 case CONTINUE_STMT:
5158 lineno = TREE_COMPLEXITY (t);
5159 finish_continue_stmt ();
5160 break;
5161
5162 case SWITCH_STMT:
5163 {
5164 tree val, tmp;
5165
5166 lineno = TREE_COMPLEXITY (t);
5167 begin_switch_stmt ();
5168 val = tsubst_expr (SWITCH_COND (t), args, in_decl);
5169 finish_switch_cond (val);
5170
5171 if (tmp = TREE_OPERAND (t, 1), tmp)
5172 tsubst_expr (tmp, args, in_decl);
5173
5174 finish_switch_stmt (val, NULL_TREE);
5175 }
5176 break;
5177
5178 case CASE_LABEL:
5179 finish_case_label (tsubst_expr (CASE_LOW (t), args, in_decl),
5180 tsubst_expr (CASE_HIGH (t), args, in_decl));
5181 break;
5182
5183 case LABEL_DECL:
5184 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
5185 DECL_NAME (t));
5186 if (t)
5187 expand_label (t);
5188 break;
5189
5190 case GOTO_STMT:
5191 lineno = TREE_COMPLEXITY (t);
5192 t = GOTO_DESTINATION (t);
5193 if (TREE_CODE (t) != IDENTIFIER_NODE)
5194 /* Computed goto's must be tsubst'd into. On the other hand,
5195 non-computed gotos must not be; the identifier in question
5196 will have no binding. */
5197 t = tsubst_expr (t, args, in_decl);
5198 finish_goto_stmt (t);
5199 break;
5200
5201 case ASM_STMT:
5202 lineno = TREE_COMPLEXITY (t);
5203 finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, in_decl),
5204 tsubst_expr (ASM_STRING (t), args, in_decl),
5205 tsubst_expr (ASM_OUTPUTS (t), args, in_decl),
5206 tsubst_expr (ASM_INPUTS (t), args, in_decl),
5207 tsubst_expr (ASM_CLOBBERS (t), args, in_decl));
5208 break;
5209
5210 case TRY_BLOCK:
5211 lineno = TREE_COMPLEXITY (t);
5212 begin_try_block ();
5213 tsubst_expr (TRY_STMTS (t), args, in_decl);
5214 finish_try_block (NULL_TREE);
5215 {
5216 tree handler = TRY_HANDLERS (t);
5217 for (; handler; handler = TREE_CHAIN (handler))
5218 tsubst_expr (handler, args, in_decl);
5219 }
5220 finish_handler_sequence (NULL_TREE);
5221 break;
5222
5223 case HANDLER:
5224 lineno = TREE_COMPLEXITY (t);
5225 begin_handler ();
5226 if (HANDLER_PARMS (t))
5227 {
5228 tree d = HANDLER_PARMS (t);
5229 expand_start_catch_block
5230 (tsubst (TREE_OPERAND (d, 1), args, in_decl),
5231 tsubst (TREE_OPERAND (d, 0), args, in_decl));
5232 }
5233 else
5234 expand_start_catch_block (NULL_TREE, NULL_TREE);
5235 finish_handler_parms (NULL_TREE);
5236 tsubst_expr (HANDLER_BODY (t), args, in_decl);
5237 finish_handler (NULL_TREE);
5238 break;
5239
5240 case TAG_DEFN:
5241 lineno = TREE_COMPLEXITY (t);
5242 t = TREE_TYPE (t);
5243 if (TREE_CODE (t) == ENUMERAL_TYPE)
5244 tsubst_enum (t, args, NULL);
5245 break;
5246
5247 default:
5248 return build_expr_from_tree (tsubst_copy (t, args, in_decl));
5249 }
5250 return NULL_TREE;
5251 }
5252
5253 tree
5254 instantiate_template (tmpl, targ_ptr)
5255 tree tmpl, targ_ptr;
5256 {
5257 tree fndecl;
5258 int i, len;
5259 struct obstack *old_fmp_obstack;
5260 extern struct obstack *function_maybepermanent_obstack;
5261
5262 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
5263
5264 /* FIXME this won't work with member templates; we only have one level
5265 of args here. */
5266 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
5267 {
5268 /* Check to see if we already have this specialization. */
5269 tree spec = retrieve_specialization (tmpl, targ_ptr);
5270
5271 if (spec != NULL_TREE)
5272 return spec;
5273 }
5274
5275 push_obstacks (&permanent_obstack, &permanent_obstack);
5276 old_fmp_obstack = function_maybepermanent_obstack;
5277 function_maybepermanent_obstack = &permanent_obstack;
5278
5279 len = DECL_NTPARMS (tmpl);
5280
5281 i = len;
5282 while (i--)
5283 {
5284 tree t = TREE_VEC_ELT (targ_ptr, i);
5285 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
5286 {
5287 tree nt = target_type (t);
5288 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
5289 {
5290 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
5291 cp_error (" trying to instantiate `%D'", tmpl);
5292 fndecl = error_mark_node;
5293 goto out;
5294 }
5295 }
5296 TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
5297 }
5298 targ_ptr = copy_to_permanent (targ_ptr);
5299
5300 /* substitute template parameters */
5301 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, tmpl);
5302
5303 if (flag_external_templates)
5304 add_pending_template (fndecl);
5305
5306 out:
5307 function_maybepermanent_obstack = old_fmp_obstack;
5308 pop_obstacks ();
5309
5310 return fndecl;
5311 }
5312
5313 /* Push the name of the class template into the scope of the instantiation. */
5314
5315 void
5316 overload_template_name (type)
5317 tree type;
5318 {
5319 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
5320 tree decl;
5321
5322 if (IDENTIFIER_CLASS_VALUE (id)
5323 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
5324 return;
5325
5326 decl = build_decl (TYPE_DECL, id, type);
5327 SET_DECL_ARTIFICIAL (decl);
5328 pushdecl_class_level (decl);
5329 }
5330
5331
5332 /* Like type_unification but designed specially to handle conversion
5333 operators. The EXTRA_FN_ARG, if any, is the type of an additional
5334 parameter to be added to the beginning of FN's parameter list. */
5335
5336 int
5337 fn_type_unification (fn, explicit_targs, targs, args, return_type,
5338 strict, extra_fn_arg)
5339 tree fn, explicit_targs, targs, args, return_type;
5340 int strict;
5341 tree extra_fn_arg;
5342 {
5343 int i;
5344 tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
5345 tree decl_arg_types = args;
5346
5347 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
5348
5349 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn)))
5350 {
5351 /* This is a template conversion operator. Use the return types
5352 as well as the argument types. */
5353 fn_arg_types = scratch_tree_cons (NULL_TREE,
5354 TREE_TYPE (TREE_TYPE (fn)),
5355 fn_arg_types);
5356 decl_arg_types = scratch_tree_cons (NULL_TREE,
5357 return_type,
5358 decl_arg_types);
5359 }
5360
5361 if (extra_fn_arg != NULL_TREE)
5362 fn_arg_types = scratch_tree_cons (NULL_TREE, extra_fn_arg,
5363 fn_arg_types);
5364
5365 /* We allow incomplete unification without an error message here
5366 because the standard doesn't seem to explicitly prohibit it. Our
5367 callers must be ready to deal with unification failures in any
5368 event. */
5369 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
5370 targs,
5371 fn_arg_types,
5372 decl_arg_types,
5373 explicit_targs,
5374 strict, 1);
5375
5376 return i;
5377 }
5378
5379
5380 /* Type unification.
5381
5382 We have a function template signature with one or more references to
5383 template parameters, and a parameter list we wish to fit to this
5384 template. If possible, produce a list of parameters for the template
5385 which will cause it to fit the supplied parameter list.
5386
5387 Return zero for success, 2 for an incomplete match that doesn't resolve
5388 all the types, and 1 for complete failure. An error message will be
5389 printed only for an incomplete match.
5390
5391 TPARMS[NTPARMS] is an array of template parameter types;
5392 TARGS[NTPARMS] is the array of template parameter values. PARMS is
5393 the function template's signature (using TEMPLATE_PARM_IDX nodes),
5394 and ARGS is the argument list we're trying to match against it.
5395
5396 If SUBR is 1, we're being called recursively (to unify the arguments of
5397 a function or method parameter of a function template), so don't zero
5398 out targs and don't fail on an incomplete match.
5399
5400 If STRICT is 1, the match must be exact (for casts of overloaded
5401 addresses, explicit instantiation, and more_specialized). */
5402
5403 int
5404 type_unification (tparms, targs, parms, args, targs_in,
5405 strict, allow_incomplete)
5406 tree tparms, targs, parms, args, targs_in;
5407 int strict, allow_incomplete;
5408 {
5409 int ntparms = TREE_VEC_LENGTH (tparms);
5410 tree arg;
5411 int* explicit_mask;
5412 int i;
5413 int r;
5414
5415 for (i = 0; i < ntparms; i++)
5416 TREE_VEC_ELT (targs, i) = NULL_TREE;
5417
5418 if (targs_in != NULL_TREE)
5419 {
5420 tree arg_vec;
5421 arg_vec = coerce_template_parms (tparms, targs_in, NULL_TREE, 0,
5422 0, 0);
5423
5424 if (arg_vec == error_mark_node)
5425 return 1;
5426
5427 explicit_mask = alloca (sizeof (int) * TREE_VEC_LENGTH (targs));
5428 bzero (explicit_mask, sizeof(int) * TREE_VEC_LENGTH (targs));
5429
5430 for (i = 0;
5431 i < TREE_VEC_LENGTH (arg_vec)
5432 && TREE_VEC_ELT (arg_vec, i) != NULL_TREE;
5433 ++i)
5434 {
5435 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (arg_vec, i);
5436 /* Let unify know that this argument was explicit. */
5437 explicit_mask [i] = 1;
5438 }
5439 }
5440 else
5441 explicit_mask = 0;
5442
5443 r = type_unification_real (tparms, targs, parms, args, 0,
5444 strict, allow_incomplete, explicit_mask);
5445
5446 return r;
5447 }
5448
5449 /* Like type_unfication. EXPLICIT_MASK, if non-NULL, is an array of
5450 integers, with ones in positions corresponding to arguments in
5451 targs that were provided explicitly, and zeros elsewhere. */
5452
5453 static int
5454 type_unification_real (tparms, targs, parms, args, subr,
5455 strict, allow_incomplete, explicit_mask)
5456 tree tparms, targs, parms, args;
5457 int subr, strict, allow_incomplete;
5458 int* explicit_mask;
5459 {
5460 tree parm, arg;
5461 int i;
5462 int ntparms = TREE_VEC_LENGTH (tparms);
5463
5464 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
5465 my_friendly_assert (parms == NULL_TREE
5466 || TREE_CODE (parms) == TREE_LIST, 290);
5467 /* ARGS could be NULL (via a call from parse.y to
5468 build_x_function_call). */
5469 if (args)
5470 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
5471 my_friendly_assert (ntparms > 0, 292);
5472
5473 while (parms
5474 && parms != void_list_node
5475 && args
5476 && args != void_list_node)
5477 {
5478 parm = TREE_VALUE (parms);
5479 parms = TREE_CHAIN (parms);
5480 arg = TREE_VALUE (args);
5481 args = TREE_CHAIN (args);
5482
5483 if (arg == error_mark_node)
5484 return 1;
5485 if (arg == unknown_type_node)
5486 return 1;
5487
5488 /* Conversions will be performed on a function argument that
5489 corresponds with a function parameter that contains only
5490 non-deducible template parameters and explicitly specified
5491 template parameters. */
5492 if (! uses_template_parms (parm))
5493 {
5494 tree type;
5495
5496 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
5497 type = TREE_TYPE (arg);
5498 else
5499 {
5500 type = arg;
5501 arg = NULL_TREE;
5502 }
5503
5504 if (strict)
5505 {
5506 if (comptypes (parm, type, 1))
5507 continue;
5508 }
5509 else
5510 /* It might work; we shouldn't check now, because we might
5511 get into infinite recursion. Overload resolution will
5512 handle it. */
5513 continue;
5514
5515 return 1;
5516 }
5517
5518 #if 0
5519 if (TREE_CODE (arg) == VAR_DECL)
5520 arg = TREE_TYPE (arg);
5521 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
5522 arg = TREE_TYPE (arg);
5523 #else
5524 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
5525 {
5526 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
5527 if (TREE_CODE (arg) == TREE_LIST
5528 && TREE_TYPE (arg) == unknown_type_node
5529 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
5530 {
5531 int ntparms;
5532 tree targs;
5533
5534 /* Have to back unify here */
5535 arg = TREE_VALUE (arg);
5536 ntparms = DECL_NTPARMS (arg);
5537 targs = make_scratch_vec (ntparms);
5538 parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
5539 return
5540 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg),
5541 targs,
5542 TYPE_ARG_TYPES (TREE_TYPE (arg)),
5543 parm, NULL_TREE, strict,
5544 allow_incomplete);
5545 }
5546 arg = TREE_TYPE (arg);
5547 }
5548 #endif
5549 if (! flag_ansi && arg == TREE_TYPE (null_node))
5550 {
5551 warning ("using type void* for NULL");
5552 arg = ptr_type_node;
5553 }
5554
5555 if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
5556 arg = TREE_TYPE (arg);
5557
5558 if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
5559 {
5560 if (TREE_CODE (arg) == FUNCTION_TYPE
5561 || TREE_CODE (arg) == METHOD_TYPE)
5562 arg = build_pointer_type (arg);
5563 else if (TREE_CODE (arg) == ARRAY_TYPE)
5564 arg = build_pointer_type (TREE_TYPE (arg));
5565 else
5566 arg = TYPE_MAIN_VARIANT (arg);
5567 }
5568
5569 switch (unify (tparms, targs, ntparms, parm, arg, strict,
5570 explicit_mask))
5571 {
5572 case 0:
5573 break;
5574 case 1:
5575 return 1;
5576 }
5577 }
5578 /* Fail if we've reached the end of the parm list, and more args
5579 are present, and the parm list isn't variadic. */
5580 if (args && args != void_list_node && parms == void_list_node)
5581 return 1;
5582 /* Fail if parms are left and they don't have default values. */
5583 if (parms
5584 && parms != void_list_node
5585 && TREE_PURPOSE (parms) == NULL_TREE)
5586 return 1;
5587 if (!subr)
5588 for (i = 0; i < ntparms; i++)
5589 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
5590 {
5591 if (!allow_incomplete)
5592 error ("incomplete type unification");
5593 return 2;
5594 }
5595 return 0;
5596 }
5597
5598 /* Returns the level of DECL, which declares a template parameter. */
5599
5600 int
5601 template_decl_level (decl)
5602 tree decl;
5603 {
5604 switch (TREE_CODE (decl))
5605 {
5606 case TYPE_DECL:
5607 case TEMPLATE_DECL:
5608 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
5609
5610 case PARM_DECL:
5611 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
5612
5613 default:
5614 my_friendly_abort (0);
5615 return 0;
5616 }
5617 }
5618
5619 /* Tail recursion is your friend. */
5620
5621 static int
5622 unify (tparms, targs, ntparms, parm, arg, strict, explicit_mask)
5623 tree tparms, targs, parm, arg;
5624 int ntparms, strict;
5625 int* explicit_mask;
5626 {
5627 int idx;
5628 tree targ;
5629 tree tparm;
5630
5631 /* I don't think this will do the right thing with respect to types.
5632 But the only case I've seen it in so far has been array bounds, where
5633 signedness is the only information lost, and I think that will be
5634 okay. */
5635 while (TREE_CODE (parm) == NOP_EXPR)
5636 parm = TREE_OPERAND (parm, 0);
5637
5638 if (arg == error_mark_node)
5639 return 1;
5640 if (arg == unknown_type_node)
5641 return 1;
5642 /* If PARM uses template parameters, then we can't bail out here,
5643 even in ARG == PARM, since we won't record unifications for the
5644 template parameters. We might need them if we're trying to
5645 figure out which of two things is more specialized. */
5646 if (arg == parm && !uses_template_parms (parm))
5647 return 0;
5648
5649 /* We can't remove cv-quals when strict. */
5650 if (strict && TREE_CODE (arg) == TREE_CODE (parm)
5651 && TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
5652 && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
5653 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
5654 return 1;
5655
5656 switch (TREE_CODE (parm))
5657 {
5658 case TYPENAME_TYPE:
5659 /* In a type which contains a nested-name-specifier, template
5660 argument values cannot be deduced for template parameters used
5661 within the nested-name-specifier. */
5662 return 0;
5663
5664 case TEMPLATE_TYPE_PARM:
5665 case TEMPLATE_TEMPLATE_PARM:
5666 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
5667
5668 if (TEMPLATE_TYPE_LEVEL (parm)
5669 != template_decl_level (tparm))
5670 /* The PARM is not one we're trying to unify. Just check
5671 to see if it matches ARG. */
5672 return (TREE_CODE (arg) == TREE_CODE (parm)
5673 && comptypes (parm, arg, 1)) ? 0 : 1;
5674 idx = TEMPLATE_TYPE_IDX (parm);
5675 targ = TREE_VEC_ELT (targs, idx);
5676 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
5677
5678 /* Check for mixed types and values. */
5679 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5680 && TREE_CODE (tparm) != TYPE_DECL)
5681 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
5682 && TREE_CODE (tparm) != TEMPLATE_DECL))
5683 return 1;
5684
5685 if (!strict && targ != NULL_TREE
5686 && explicit_mask && explicit_mask[idx])
5687 /* An explicit template argument. Don't even try to match
5688 here; the overload resolution code will manage check to
5689 see whether the call is legal. */
5690 return 0;
5691
5692 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5693 {
5694 if (CLASSTYPE_TEMPLATE_INFO (parm))
5695 {
5696 /* We arrive here when PARM does not involve template
5697 specialization. */
5698
5699 /* ARG must be constructed from a template class. */
5700 if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
5701 return 1;
5702
5703 {
5704 tree parmtmpl = CLASSTYPE_TI_TEMPLATE (parm);
5705 tree parmvec = CLASSTYPE_TI_ARGS (parm);
5706 tree argvec = CLASSTYPE_TI_ARGS (arg);
5707 tree argtmplvec
5708 = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
5709 int i;
5710
5711 /* The parameter and argument roles have to be switched here
5712 in order to handle default arguments properly. For example,
5713 template<template <class> class TT> void f(TT<int>)
5714 should be able to accept vector<int> which comes from
5715 template <class T, class Allcator = allocator>
5716 class vector. */
5717
5718 if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 1, 1, 0)
5719 == error_mark_node)
5720 return 1;
5721
5722 /* Deduce arguments T, i from TT<T> or TT<i>. */
5723 for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
5724 {
5725 tree t = TREE_VEC_ELT (parmvec, i);
5726 if (TREE_CODE (t) != TEMPLATE_TYPE_PARM
5727 && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM
5728 && TREE_CODE (t) != TEMPLATE_PARM_INDEX)
5729 continue;
5730
5731 /* This argument can be deduced. */
5732
5733 if (unify (tparms, targs, ntparms, t,
5734 TREE_VEC_ELT (argvec, i), strict, explicit_mask))
5735 return 1;
5736 }
5737 }
5738 arg = CLASSTYPE_TI_TEMPLATE (arg);
5739 }
5740 }
5741 else
5742 {
5743 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
5744 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
5745 return 1;
5746
5747 #if 0
5748 /* Template type parameters cannot contain cv-quals; i.e.
5749 template <class T> void f (T& a, T& b) will not generate
5750 void f (const int& a, const int& b). */
5751 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
5752 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
5753 return 1;
5754 arg = TYPE_MAIN_VARIANT (arg);
5755 #else
5756 {
5757 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
5758 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
5759 arg = cp_build_type_variant (arg, constp, volatilep);
5760 }
5761 #endif
5762 }
5763
5764 /* Simple cases: Value already set, does match or doesn't. */
5765 if (targ != NULL_TREE
5766 && (comptypes (targ, arg, 1)
5767 || (explicit_mask && explicit_mask[idx])))
5768 return 0;
5769 else if (targ)
5770 return 1;
5771 TREE_VEC_ELT (targs, idx) = arg;
5772 return 0;
5773
5774 case TEMPLATE_PARM_INDEX:
5775 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
5776
5777 if (TEMPLATE_PARM_LEVEL (parm)
5778 != template_decl_level (tparm))
5779 /* The PARM is not one we're trying to unify. Just check
5780 to see if it matches ARG. */
5781 return (TREE_CODE (arg) == TREE_CODE (parm)
5782 && cp_tree_equal (parm, arg)) ? 0 : 1;
5783
5784 idx = TEMPLATE_PARM_IDX (parm);
5785 targ = TREE_VEC_ELT (targs, idx);
5786
5787 if (targ)
5788 {
5789 int i = cp_tree_equal (targ, arg);
5790 if (i == 1)
5791 return 0;
5792 else if (i == 0)
5793 return 1;
5794 else
5795 my_friendly_abort (42);
5796 }
5797
5798 TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg);
5799 return 0;
5800
5801 case POINTER_TYPE:
5802 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
5803 return unify (tparms, targs, ntparms, parm,
5804 TYPE_PTRMEMFUNC_FN_TYPE (arg), strict, explicit_mask);
5805
5806 if (TREE_CODE (arg) != POINTER_TYPE)
5807 return 1;
5808 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
5809 strict, explicit_mask);
5810
5811 case REFERENCE_TYPE:
5812 if (TREE_CODE (arg) == REFERENCE_TYPE)
5813 arg = TREE_TYPE (arg);
5814 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
5815 strict, explicit_mask);
5816
5817 case ARRAY_TYPE:
5818 if (TREE_CODE (arg) != ARRAY_TYPE)
5819 return 1;
5820 if ((TYPE_DOMAIN (parm) == NULL_TREE)
5821 != (TYPE_DOMAIN (arg) == NULL_TREE))
5822 return 1;
5823 if (TYPE_DOMAIN (parm) != NULL_TREE
5824 && unify (tparms, targs, ntparms, TYPE_DOMAIN (parm),
5825 TYPE_DOMAIN (arg), strict, explicit_mask) != 0)
5826 return 1;
5827 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
5828 strict, explicit_mask);
5829
5830 case REAL_TYPE:
5831 case COMPLEX_TYPE:
5832 case INTEGER_TYPE:
5833 case BOOLEAN_TYPE:
5834 case VOID_TYPE:
5835 if (TREE_CODE (arg) != TREE_CODE (parm))
5836 return 1;
5837
5838 if (TREE_CODE (parm) == INTEGER_TYPE)
5839 {
5840 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
5841 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
5842 TYPE_MIN_VALUE (arg), strict, explicit_mask))
5843 return 1;
5844 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
5845 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
5846 TYPE_MAX_VALUE (arg), strict, explicit_mask))
5847 return 1;
5848 }
5849 else if (TREE_CODE (parm) == REAL_TYPE
5850 && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
5851 return 1;
5852
5853 /* As far as unification is concerned, this wins. Later checks
5854 will invalidate it if necessary. */
5855 return 0;
5856
5857 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
5858 /* Type INTEGER_CST can come from ordinary constant template args. */
5859 case INTEGER_CST:
5860 while (TREE_CODE (arg) == NOP_EXPR)
5861 arg = TREE_OPERAND (arg, 0);
5862
5863 if (TREE_CODE (arg) != INTEGER_CST)
5864 return 1;
5865 return !tree_int_cst_equal (parm, arg);
5866
5867 case TREE_VEC:
5868 {
5869 int i;
5870 if (TREE_CODE (arg) != TREE_VEC)
5871 return 1;
5872 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
5873 return 1;
5874 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
5875 if (unify (tparms, targs, ntparms,
5876 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
5877 strict, explicit_mask))
5878 return 1;
5879 return 0;
5880 }
5881
5882 case RECORD_TYPE:
5883 if (TYPE_PTRMEMFUNC_FLAG (parm))
5884 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
5885 arg, strict, explicit_mask);
5886
5887 /* Allow trivial conversions. */
5888 if (TREE_CODE (arg) != RECORD_TYPE
5889 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
5890 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
5891 return 1;
5892
5893 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
5894 {
5895 tree t = NULL_TREE;
5896 if (! strict)
5897 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
5898 else if
5899 (CLASSTYPE_TEMPLATE_INFO (arg)
5900 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
5901 t = arg;
5902 if (! t || t == error_mark_node)
5903 return 1;
5904
5905 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
5906 CLASSTYPE_TI_ARGS (t), strict, explicit_mask);
5907 }
5908 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
5909 return 1;
5910 return 0;
5911
5912 case METHOD_TYPE:
5913 if (TREE_CODE (arg) != METHOD_TYPE)
5914 return 1;
5915 goto check_args;
5916
5917 case FUNCTION_TYPE:
5918 if (TREE_CODE (arg) != FUNCTION_TYPE)
5919 return 1;
5920 check_args:
5921 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
5922 TREE_TYPE (arg), strict, explicit_mask))
5923 return 1;
5924 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
5925 TYPE_ARG_TYPES (arg), 1,
5926 strict, 0, explicit_mask);
5927
5928 case OFFSET_TYPE:
5929 if (TREE_CODE (arg) != OFFSET_TYPE)
5930 return 1;
5931 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
5932 TYPE_OFFSET_BASETYPE (arg), strict, explicit_mask))
5933 return 1;
5934 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
5935 TREE_TYPE (arg), strict, explicit_mask);
5936
5937 case CONST_DECL:
5938 if (arg != decl_constant_value (parm))
5939 return 1;
5940 return 0;
5941
5942 case TEMPLATE_DECL:
5943 /* Matched cases are handled by the ARG == PARM test above. */
5944 return 1;
5945
5946 default:
5947 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
5948 {
5949 /* We're looking at an expression. This can happen with
5950 something like:
5951
5952 template <int I>
5953 void foo(S<I>, S<I + 2>);
5954
5955 If the call looked like:
5956
5957 foo(S<2>(), S<4>());
5958
5959 we would have already matched `I' with `2'. Now, we'd
5960 like to know if `4' matches `I + 2'. So, we substitute
5961 into that expression, and fold constants, in the hope of
5962 figuring it out. */
5963 tree t =
5964 maybe_fold_nontype_arg (tsubst_expr (parm, targs, NULL_TREE));
5965 enum tree_code tc = TREE_CODE (t);
5966
5967 if (tc == MINUS_EXPR
5968 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX
5969 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
5970 {
5971 /* We handle this case specially, since it comes up with
5972 arrays. In particular, something like:
5973
5974 template <int N> void f(int (&x)[N]);
5975
5976 Here, we are trying to unify the range type, which
5977 looks like [0 ... (N - 1)]. */
5978 tree t1, t2;
5979 t1 = TREE_OPERAND (parm, 0);
5980 t2 = TREE_OPERAND (parm, 1);
5981
5982 t = maybe_fold_nontype_arg (build (PLUS_EXPR,
5983 integer_type_node,
5984 arg, t2));
5985
5986 return unify (tparms, targs, ntparms, t1, t,
5987 strict, explicit_mask);
5988 }
5989
5990 if (!IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (tc)))
5991 /* Good, we mangaged to simplify the exression. */
5992 return unify (tparms, targs, ntparms, t, arg, strict,
5993 explicit_mask);
5994 else
5995 /* Bad, we couldn't simplify this. Assume it doesn't
5996 unify. */
5997 return 1;
5998 }
5999 else
6000 sorry ("use of `%s' in template type unification",
6001 tree_code_name [(int) TREE_CODE (parm)]);
6002
6003 return 1;
6004 }
6005 }
6006 \f
6007 void
6008 mark_decl_instantiated (result, extern_p)
6009 tree result;
6010 int extern_p;
6011 {
6012 if (DECL_TEMPLATE_INSTANTIATION (result))
6013 SET_DECL_EXPLICIT_INSTANTIATION (result);
6014
6015 if (TREE_CODE (result) != FUNCTION_DECL)
6016 /* The TREE_PUBLIC flag for function declarations will have been
6017 set correctly by tsubst. */
6018 TREE_PUBLIC (result) = 1;
6019
6020 if (! extern_p)
6021 {
6022 DECL_INTERFACE_KNOWN (result) = 1;
6023 DECL_NOT_REALLY_EXTERN (result) = 1;
6024
6025 /* For WIN32 we also want to put explicit instantiations in
6026 linkonce sections. */
6027 if (supports_one_only () && ! SUPPORTS_WEAK && TREE_PUBLIC (result))
6028 make_decl_one_only (result);
6029 }
6030 else if (TREE_CODE (result) == FUNCTION_DECL)
6031 mark_inline_for_output (result);
6032 }
6033
6034 /* Given two function templates PAT1 and PAT2, and explicit template
6035 arguments EXPLICIT_ARGS return:
6036
6037 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
6038 -1 if PAT2 is more specialized than PAT1.
6039 0 if neither is more specialized. */
6040
6041 int
6042 more_specialized (pat1, pat2, explicit_args)
6043 tree pat1, pat2, explicit_args;
6044 {
6045 tree targs;
6046 int winner = 0;
6047
6048 targs = get_bindings_overload (pat1, pat2, explicit_args);
6049 if (targs)
6050 {
6051 --winner;
6052 }
6053
6054 targs = get_bindings_overload (pat2, pat1, explicit_args);
6055 if (targs)
6056 {
6057 ++winner;
6058 }
6059
6060 return winner;
6061 }
6062
6063 /* Given two class template specialization list nodes PAT1 and PAT2, return:
6064
6065 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
6066 -1 if PAT2 is more specialized than PAT1.
6067 0 if neither is more specialized. */
6068
6069 int
6070 more_specialized_class (pat1, pat2)
6071 tree pat1, pat2;
6072 {
6073 tree targs;
6074 int winner = 0;
6075
6076 targs = get_class_bindings
6077 (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
6078 TREE_PURPOSE (pat2), NULL_TREE);
6079 if (targs)
6080 --winner;
6081
6082 targs = get_class_bindings
6083 (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
6084 TREE_PURPOSE (pat1), NULL_TREE);
6085 if (targs)
6086 ++winner;
6087
6088 return winner;
6089 }
6090
6091 /* Return the template arguments that will produce the function signature
6092 DECL from the function template FN, with the explicit template
6093 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is 1, the return type must
6094 also match. */
6095
6096 static tree
6097 get_bindings_real (fn, decl, explicit_args, check_rettype)
6098 tree fn, decl, explicit_args;
6099 int check_rettype;
6100 {
6101 int ntparms = DECL_NTPARMS (fn);
6102 tree targs = make_scratch_vec (ntparms);
6103 tree decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
6104 tree extra_fn_arg = NULL_TREE;
6105 int i;
6106
6107 if (DECL_STATIC_FUNCTION_P (fn)
6108 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
6109 {
6110 /* Sometimes we are trying to figure out what's being
6111 specialized by a declaration that looks like a method, and it
6112 turns out to be a static member function. */
6113 if (CLASSTYPE_TEMPLATE_INFO (DECL_REAL_CONTEXT (fn))
6114 && !is_member_template (fn))
6115 /* The natural thing to do here seems to be to remove the
6116 spurious `this' parameter from the DECL, but that prevents
6117 unification from making use of the class type. So,
6118 instead, we have fn_type_unification add to the parameters
6119 for FN. */
6120 extra_fn_arg = build_pointer_type (DECL_REAL_CONTEXT (fn));
6121 else
6122 /* In this case, though, adding the extra_fn_arg can confuse
6123 things, so we remove from decl_arg_types instead. */
6124 decl_arg_types = TREE_CHAIN (decl_arg_types);
6125 }
6126
6127 i = fn_type_unification (fn, explicit_args, targs,
6128 decl_arg_types,
6129 TREE_TYPE (TREE_TYPE (decl)),
6130 1,
6131 extra_fn_arg);
6132
6133 if (i != 0)
6134 return NULL_TREE;
6135
6136 if (check_rettype)
6137 {
6138 /* Check to see that the resulting return type is also OK. */
6139 tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)),
6140 complete_template_args (fn, targs, 1),
6141 NULL_TREE);
6142
6143 if (!comptypes (t, TREE_TYPE (TREE_TYPE (decl)), 1))
6144 return NULL_TREE;
6145 }
6146
6147 return targs;
6148 }
6149
6150 /* For most uses, we want to check the return type. */
6151
6152 tree
6153 get_bindings (fn, decl, explicit_args)
6154 tree fn, decl, explicit_args;
6155 {
6156 return get_bindings_real (fn, decl, explicit_args, 1);
6157 }
6158
6159 /* But for more_specialized, we only care about the parameter types. */
6160
6161 static tree
6162 get_bindings_overload (fn, decl, explicit_args)
6163 tree fn, decl, explicit_args;
6164 {
6165 return get_bindings_real (fn, decl, explicit_args, 0);
6166 }
6167
6168 static tree
6169 get_class_bindings (tparms, parms, args, outer_args)
6170 tree tparms, parms, args, outer_args;
6171 {
6172 int i, ntparms = TREE_VEC_LENGTH (tparms);
6173 tree vec = make_temp_vec (ntparms);
6174
6175 if (outer_args)
6176 {
6177 tparms = tsubst (tparms, outer_args, NULL_TREE);
6178 parms = tsubst (parms, outer_args, NULL_TREE);
6179 }
6180
6181 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
6182 {
6183 switch (unify (tparms, vec, ntparms,
6184 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
6185 1, 0))
6186 {
6187 case 0:
6188 break;
6189 case 1:
6190 return NULL_TREE;
6191 }
6192 }
6193
6194 for (i = 0; i < ntparms; ++i)
6195 if (! TREE_VEC_ELT (vec, i))
6196 return NULL_TREE;
6197
6198 return vec;
6199 }
6200
6201 /* Return the most specialized of the list of templates in FNS that can
6202 produce an instantiation matching DECL, given the explicit template
6203 arguments EXPLICIT_ARGS. */
6204
6205 tree
6206 most_specialized (fns, decl, explicit_args)
6207 tree fns, decl, explicit_args;
6208 {
6209 tree fn, champ, args, *p;
6210 int fate;
6211
6212 for (p = &fns; *p; )
6213 {
6214 args = get_bindings (TREE_VALUE (*p), decl, explicit_args);
6215 if (args)
6216 {
6217 p = &TREE_CHAIN (*p);
6218 }
6219 else
6220 *p = TREE_CHAIN (*p);
6221 }
6222
6223 if (! fns)
6224 return NULL_TREE;
6225
6226 fn = fns;
6227 champ = TREE_VALUE (fn);
6228 fn = TREE_CHAIN (fn);
6229 for (; fn; fn = TREE_CHAIN (fn))
6230 {
6231 fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
6232 if (fate == 1)
6233 ;
6234 else
6235 {
6236 if (fate == 0)
6237 {
6238 fn = TREE_CHAIN (fn);
6239 if (! fn)
6240 return error_mark_node;
6241 }
6242 champ = TREE_VALUE (fn);
6243 }
6244 }
6245
6246 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
6247 {
6248 fate = more_specialized (champ, TREE_VALUE (fn), explicit_args);
6249 if (fate != 1)
6250 return error_mark_node;
6251 }
6252
6253 return champ;
6254 }
6255
6256 /* Return the most specialized of the class template specializations in
6257 SPECS that can produce an instantiation matching ARGS. */
6258
6259 tree
6260 most_specialized_class (specs, mainargs, outer_args)
6261 tree specs, mainargs, outer_args;
6262 {
6263 tree list = NULL_TREE, t, args, champ;
6264 int fate;
6265
6266 for (t = specs; t; t = TREE_CHAIN (t))
6267 {
6268 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
6269 mainargs, outer_args);
6270 if (args)
6271 {
6272 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
6273 TREE_TYPE (list) = TREE_TYPE (t);
6274 }
6275 }
6276
6277 if (! list)
6278 return NULL_TREE;
6279
6280 t = list;
6281 champ = t;
6282 t = TREE_CHAIN (t);
6283 for (; t; t = TREE_CHAIN (t))
6284 {
6285 fate = more_specialized_class (champ, t);
6286 if (fate == 1)
6287 ;
6288 else
6289 {
6290 if (fate == 0)
6291 {
6292 t = TREE_CHAIN (t);
6293 if (! t)
6294 return error_mark_node;
6295 }
6296 champ = t;
6297 }
6298 }
6299
6300 for (t = list; t && t != champ; t = TREE_CHAIN (t))
6301 {
6302 fate = more_specialized_class (champ, t);
6303 if (fate != 1)
6304 return error_mark_node;
6305 }
6306
6307 return champ;
6308 }
6309
6310 /* called from the parser. */
6311
6312 void
6313 do_decl_instantiation (declspecs, declarator, storage)
6314 tree declspecs, declarator, storage;
6315 {
6316 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
6317 tree result = NULL_TREE;
6318 int extern_p = 0;
6319
6320 if (! DECL_LANG_SPECIFIC (decl))
6321 {
6322 cp_error ("explicit instantiation of non-template `%#D'", decl);
6323 return;
6324 }
6325
6326 /* If we've already seen this template instance, use it. */
6327 if (TREE_CODE (decl) == VAR_DECL)
6328 {
6329 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
6330 if (result && TREE_CODE (result) != VAR_DECL)
6331 result = NULL_TREE;
6332 }
6333 else if (TREE_CODE (decl) != FUNCTION_DECL)
6334 {
6335 cp_error ("explicit instantiation of `%#D'", decl);
6336 return;
6337 }
6338 else if (DECL_TEMPLATE_INSTANTIATION (decl))
6339 result = decl;
6340
6341 if (! result)
6342 {
6343 cp_error ("no matching template for `%D' found", decl);
6344 return;
6345 }
6346
6347 if (! DECL_TEMPLATE_INFO (result))
6348 {
6349 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
6350 return;
6351 }
6352
6353 if (flag_external_templates)
6354 return;
6355
6356 if (storage == NULL_TREE)
6357 ;
6358 else if (storage == ridpointers[(int) RID_EXTERN])
6359 extern_p = 1;
6360 else
6361 cp_error ("storage class `%D' applied to template instantiation",
6362 storage);
6363
6364 mark_decl_instantiated (result, extern_p);
6365 repo_template_instantiated (result, extern_p);
6366 if (! extern_p)
6367 instantiate_decl (result);
6368 }
6369
6370 void
6371 mark_class_instantiated (t, extern_p)
6372 tree t;
6373 int extern_p;
6374 {
6375 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
6376 SET_CLASSTYPE_INTERFACE_KNOWN (t);
6377 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
6378 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
6379 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
6380 if (! extern_p)
6381 {
6382 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
6383 rest_of_type_compilation (t, 1);
6384 }
6385 }
6386
6387 void
6388 do_type_instantiation (t, storage)
6389 tree t, storage;
6390 {
6391 int extern_p = 0;
6392 int nomem_p = 0;
6393 int static_p = 0;
6394
6395 if (TREE_CODE (t) == TYPE_DECL)
6396 t = TREE_TYPE (t);
6397
6398 if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
6399 {
6400 cp_error ("explicit instantiation of non-template type `%T'", t);
6401 return;
6402 }
6403
6404 complete_type (t);
6405
6406 /* With -fexternal-templates, explicit instantiations are treated the same
6407 as implicit ones. */
6408 if (flag_external_templates)
6409 return;
6410
6411 if (TYPE_SIZE (t) == NULL_TREE)
6412 {
6413 cp_error ("explicit instantiation of `%#T' before definition of template",
6414 t);
6415 return;
6416 }
6417
6418 if (storage == NULL_TREE)
6419 /* OK */;
6420 else if (storage == ridpointers[(int) RID_INLINE])
6421 nomem_p = 1;
6422 else if (storage == ridpointers[(int) RID_EXTERN])
6423 extern_p = 1;
6424 else if (storage == ridpointers[(int) RID_STATIC])
6425 static_p = 1;
6426 else
6427 {
6428 cp_error ("storage class `%D' applied to template instantiation",
6429 storage);
6430 extern_p = 0;
6431 }
6432
6433 /* We've already instantiated this. */
6434 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
6435 && extern_p)
6436 return;
6437
6438 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
6439 {
6440 mark_class_instantiated (t, extern_p);
6441 repo_template_instantiated (t, extern_p);
6442 }
6443
6444 if (nomem_p)
6445 return;
6446
6447 {
6448 tree tmp;
6449
6450 if (! static_p)
6451 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
6452 if (TREE_CODE (tmp) == FUNCTION_DECL
6453 && DECL_TEMPLATE_INSTANTIATION (tmp))
6454 {
6455 mark_decl_instantiated (tmp, extern_p);
6456 repo_template_instantiated (tmp, extern_p);
6457 if (! extern_p)
6458 instantiate_decl (tmp);
6459 }
6460
6461 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
6462 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
6463 {
6464 mark_decl_instantiated (tmp, extern_p);
6465 repo_template_instantiated (tmp, extern_p);
6466 if (! extern_p)
6467 instantiate_decl (tmp);
6468 }
6469
6470 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
6471 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
6472 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
6473 }
6474 }
6475
6476 /* Produce the definition of D, a _DECL generated from a template. */
6477
6478 tree
6479 instantiate_decl (d)
6480 tree d;
6481 {
6482 tree ti = DECL_TEMPLATE_INFO (d);
6483 tree tmpl = TI_TEMPLATE (ti);
6484 tree args = TI_ARGS (ti);
6485 tree td, temp;
6486 tree decl_pattern, code_pattern;
6487 tree save_ti;
6488 int nested = in_function_p ();
6489 int d_defined;
6490 int pattern_defined;
6491 int line = lineno;
6492 char *file = input_filename;
6493
6494 for (td = tmpl;
6495 DECL_TEMPLATE_INSTANTIATION (td)
6496 /* This next clause handles friend templates defined inside
6497 class templates. The friend templates are not really
6498 instantiations from the point of view of the language, but
6499 they are instantiations from the point of view of the
6500 compiler. */
6501 || (DECL_TEMPLATE_INFO (td) && !DECL_TEMPLATE_SPECIALIZATION (td));
6502 )
6503 td = DECL_TI_TEMPLATE (td);
6504
6505 /* In the case of a member template, decl_pattern is the partially
6506 instantiated declaration (in the instantiated class), and code_pattern
6507 is the original template definition. */
6508 decl_pattern = DECL_TEMPLATE_RESULT (tmpl);
6509 code_pattern = DECL_TEMPLATE_RESULT (td);
6510
6511 if (TREE_CODE (d) == FUNCTION_DECL)
6512 {
6513 d_defined = (DECL_INITIAL (d) != NULL_TREE);
6514 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
6515 }
6516 else
6517 {
6518 d_defined = ! DECL_IN_AGGR_P (d);
6519 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
6520 }
6521
6522 if (d_defined)
6523 return d;
6524
6525 if (TREE_CODE (d) == FUNCTION_DECL)
6526 {
6527 tree spec = retrieve_specialization (tmpl, args);
6528
6529 if (spec != NULL_TREE
6530 && DECL_TEMPLATE_SPECIALIZATION (spec))
6531 return spec;
6532 }
6533
6534 /* This needs to happen before any tsubsting. */
6535 if (! push_tinst_level (d))
6536 return d;
6537
6538 push_to_top_level ();
6539 lineno = DECL_SOURCE_LINE (d);
6540 input_filename = DECL_SOURCE_FILE (d);
6541
6542 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
6543 variable is a static const initialized in the class body. */
6544 if (TREE_CODE (d) == VAR_DECL
6545 && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern))
6546 {
6547 pushclass (DECL_CONTEXT (d), 2);
6548 DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args,
6549 tmpl);
6550 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, LOOKUP_NORMAL);
6551 }
6552
6553 if (pattern_defined)
6554 {
6555 repo_template_used (d);
6556
6557 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
6558 {
6559 if (flag_alt_external_templates)
6560 {
6561 if (interface_unknown)
6562 warn_if_unknown_interface (d);
6563 }
6564 else if (DECL_INTERFACE_KNOWN (code_pattern))
6565 {
6566 DECL_INTERFACE_KNOWN (d) = 1;
6567 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
6568 }
6569 else
6570 warn_if_unknown_interface (code_pattern);
6571 }
6572
6573 if (at_eof)
6574 import_export_decl (d);
6575 }
6576
6577 /* Reject all external templates except inline functions. */
6578 if (DECL_INTERFACE_KNOWN (d)
6579 && ! DECL_NOT_REALLY_EXTERN (d)
6580 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
6581 goto out;
6582
6583 /* Defer all templates except inline functions used in another function. */
6584 if (! pattern_defined
6585 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
6586 && ! at_eof))
6587 {
6588 add_pending_template (d);
6589 goto out;
6590 }
6591
6592 lineno = DECL_SOURCE_LINE (d);
6593 input_filename = DECL_SOURCE_FILE (d);
6594
6595 /* Trick tsubst into giving us a new decl in case the template changed. */
6596 save_ti = DECL_TEMPLATE_INFO (decl_pattern);
6597 DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE;
6598 /* decl_pattern has all but one level of template parms bound. Only pass
6599 in that one level of args. */
6600 temp = innermost_args (args, DECL_TEMPLATE_SPECIALIZATION (decl_pattern));
6601 td = tsubst (decl_pattern, temp, tmpl);
6602 SET_DECL_IMPLICIT_INSTANTIATION (td);
6603 DECL_TEMPLATE_INFO (decl_pattern) = save_ti;
6604
6605 /* And set up DECL_INITIAL, since tsubst doesn't. */
6606 if (TREE_CODE (td) == VAR_DECL)
6607 {
6608 pushclass (DECL_CONTEXT (d), 2);
6609 DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args,
6610 tmpl);
6611 popclass (1);
6612 }
6613
6614 if (TREE_CODE (d) == FUNCTION_DECL)
6615 {
6616 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
6617 new decl. */
6618 DECL_INITIAL (td) = error_mark_node;
6619
6620 if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td))
6621 /* Set up the information about what is being specialized. */
6622 DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d);
6623 }
6624 duplicate_decls (td, d);
6625 if (TREE_CODE (d) == FUNCTION_DECL)
6626 DECL_INITIAL (td) = 0;
6627
6628 if (TREE_CODE (d) == VAR_DECL)
6629 {
6630 DECL_IN_AGGR_P (d) = 0;
6631 if (DECL_INTERFACE_KNOWN (d))
6632 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
6633 else
6634 {
6635 DECL_EXTERNAL (d) = 1;
6636 DECL_NOT_REALLY_EXTERN (d) = 1;
6637 }
6638 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
6639 }
6640 else if (TREE_CODE (d) == FUNCTION_DECL)
6641 {
6642 tree t = DECL_SAVED_TREE (code_pattern);
6643
6644 start_function (NULL_TREE, d, NULL_TREE, 1);
6645 store_parm_decls ();
6646
6647 if (t && TREE_CODE (t) == RETURN_INIT)
6648 {
6649 store_return_init
6650 (TREE_OPERAND (t, 0),
6651 tsubst_expr (TREE_OPERAND (t, 1), args, tmpl));
6652 t = TREE_CHAIN (t);
6653 }
6654
6655 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
6656 {
6657 current_member_init_list
6658 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
6659 current_base_init_list
6660 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
6661 t = TREE_CHAIN (t);
6662 }
6663
6664 setup_vtbl_ptr ();
6665 /* Always keep the BLOCK node associated with the outermost
6666 pair of curly braces of a function. These are needed
6667 for correct operation of dwarfout.c. */
6668 keep_next_level ();
6669
6670 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
6671 tsubst_expr (t, args, tmpl);
6672
6673 finish_function (lineno, 0, nested);
6674 }
6675
6676 out:
6677 lineno = line;
6678 input_filename = file;
6679
6680 pop_from_top_level ();
6681 pop_tinst_level ();
6682
6683 return d;
6684 }
6685
6686 tree
6687 tsubst_chain (t, argvec)
6688 tree t, argvec;
6689 {
6690 if (t)
6691 {
6692 tree first = tsubst (t, argvec, NULL_TREE);
6693 tree last = first;
6694
6695 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
6696 {
6697 tree x = tsubst (t, argvec, NULL_TREE);
6698 TREE_CHAIN (last) = x;
6699 last = x;
6700 }
6701
6702 return first;
6703 }
6704 return NULL_TREE;
6705 }
6706
6707 static tree
6708 tsubst_expr_values (t, argvec)
6709 tree t, argvec;
6710 {
6711 tree first = NULL_TREE;
6712 tree *p = &first;
6713
6714 for (; t; t = TREE_CHAIN (t))
6715 {
6716 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec, NULL_TREE);
6717 tree val = tsubst_expr (TREE_VALUE (t), argvec, NULL_TREE);
6718 *p = build_tree_list (pur, val);
6719 p = &TREE_CHAIN (*p);
6720 }
6721 return first;
6722 }
6723
6724 tree last_tree;
6725
6726 void
6727 add_tree (t)
6728 tree t;
6729 {
6730 last_tree = TREE_CHAIN (last_tree) = t;
6731 }
6732
6733
6734 void
6735 begin_tree ()
6736 {
6737 saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
6738 last_tree = NULL_TREE;
6739 }
6740
6741
6742 void
6743 end_tree ()
6744 {
6745 my_friendly_assert (saved_trees != NULL_TREE, 0);
6746
6747 last_tree = TREE_VALUE (saved_trees);
6748 saved_trees = TREE_CHAIN (saved_trees);
6749 }
6750
6751 /* D is an undefined function declaration in the presence of templates with
6752 the same name, listed in FNS. If one of them can produce D as an
6753 instantiation, remember this so we can instantiate it at EOF if D has
6754 not been defined by that time. */
6755
6756 void
6757 add_maybe_template (d, fns)
6758 tree d, fns;
6759 {
6760 tree t;
6761
6762 if (DECL_MAYBE_TEMPLATE (d))
6763 return;
6764
6765 t = most_specialized (fns, d, NULL_TREE);
6766 if (! t)
6767 return;
6768 if (t == error_mark_node)
6769 {
6770 cp_error ("ambiguous template instantiation for `%D'", d);
6771 return;
6772 }
6773
6774 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
6775 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
6776 DECL_MAYBE_TEMPLATE (d) = 1;
6777 }
6778
6779 /* Instantiate an enumerated type. Used by instantiate_class_template and
6780 tsubst_expr. */
6781
6782 static tree
6783 tsubst_enum (tag, args, field_chain)
6784 tree tag, args;
6785 tree * field_chain;
6786 {
6787 extern tree current_local_enum;
6788 tree prev_local_enum = current_local_enum;
6789
6790 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
6791 tree e, values = NULL_TREE;
6792
6793 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
6794 {
6795 tree elt = build_enumerator (TREE_PURPOSE (e),
6796 tsubst_expr (TREE_VALUE (e), args,
6797 NULL_TREE));
6798 TREE_CHAIN (elt) = values;
6799 values = elt;
6800 }
6801
6802 finish_enum (newtag, values);
6803
6804 if (NULL != field_chain)
6805 *field_chain = grok_enum_decls (NULL_TREE);
6806
6807 current_local_enum = prev_local_enum;
6808
6809 return newtag;
6810 }