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