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