]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/pt.c
new
[gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 1996 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 <stdio.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 #ifdef HAVE_STDLIB_H
43 #include <stdlib.h>
44 #endif
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 static int template_header_count;
65
66 #define obstack_chunk_alloc xmalloc
67 #define obstack_chunk_free free
68
69 static int unify PROTO((tree, tree *, int, tree, tree, int *, int));
70 static void add_pending_template PROTO((tree));
71 static int push_tinst_level PROTO((tree));
72 static tree classtype_mangled_name PROTO((tree));
73 static char *mangle_class_name_for_template PROTO((char *, tree, tree));
74 static tree tsubst_expr_values PROTO((tree, tree));
75 static int comp_template_args PROTO((tree, tree));
76 static int list_eq PROTO((tree, tree));
77 static tree get_class_bindings PROTO((tree, tree, tree));
78 static tree coerce_template_parms PROTO((tree, tree, tree));
79 static tree tsubst_enum PROTO((tree, tree, int, tree *));
80 static tree add_to_template_args PROTO((tree, tree));
81 static int type_unification_real PROTO((tree, tree *, tree, tree, int*,
82 int, int, int));
83 static int processing_explicit_specialization PROTO((int));
84 static void note_template_header PROTO((int));
85
86 /* Restore the template parameter context. */
87
88 void
89 begin_member_template_processing (decl)
90 tree decl;
91 {
92 tree parms;
93 int i;
94
95 parms = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
96
97 ++processing_template_decl;
98 current_template_parms
99 = tree_cons (build_int_2 (0, processing_template_decl),
100 parms, current_template_parms);
101 pushlevel (0);
102 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
103 {
104 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
105 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
106
107 switch (TREE_CODE (parm))
108 {
109 case TYPE_DECL:
110 pushdecl (parm);
111 break;
112
113 case PARM_DECL:
114 {
115 /* Make a CONST_DECL as is done in process_template_parm. */
116 tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
117 TREE_TYPE (parm));
118 DECL_INITIAL (decl) = DECL_INITIAL (parm);
119 pushdecl (decl);
120 }
121 break;
122
123 default:
124 my_friendly_abort (0);
125 }
126 }
127 }
128
129 /* Undo the effects of begin_member_template_processing. */
130
131 void
132 end_member_template_processing ()
133 {
134 if (! processing_template_decl)
135 return;
136
137 --processing_template_decl;
138 current_template_parms = TREE_CHAIN (current_template_parms);
139 poplevel (0, 0, 0);
140 }
141
142 /* Returns non-zero iff T is a member template function. Works if T
143 is either a FUNCTION_DECL or a TEMPLATE_DECL. */
144
145 int
146 is_member_template (t)
147 tree t;
148 {
149 int r = 0;
150
151 if (TREE_CODE (t) != FUNCTION_DECL
152 && !DECL_FUNCTION_TEMPLATE_P (t))
153 /* Anything that isn't a template or a template functon is
154 certainly not a member template. */
155 return 0;
156
157 if ((DECL_FUNCTION_MEMBER_P (t)
158 && !DECL_TEMPLATE_SPECIALIZATION (t))
159 || (TREE_CODE (t) == TEMPLATE_DECL &&
160 DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))))
161 {
162 tree tmpl = NULL_TREE;
163
164 if (DECL_FUNCTION_TEMPLATE_P (t))
165 tmpl = t;
166 else if (DECL_TEMPLATE_INFO (t)
167 && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t)))
168 tmpl = DECL_TI_TEMPLATE (t);
169
170 if (tmpl)
171 {
172 tree parms = DECL_TEMPLATE_PARMS (tmpl);
173 int parm_levels = list_length (parms);
174 int template_class_levels = 0;
175 tree ctx = DECL_CLASS_CONTEXT (t);
176
177 if (CLASSTYPE_TEMPLATE_INFO (ctx))
178 {
179 tree args;
180
181 /* Here, we should really count the number of levels
182 deep ctx is, making sure not to count any levels that
183 are just specializations. Since there are no member
184 template classes yet, we don't have to do all that. */
185
186 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
187 template_class_levels = 1;
188 else
189 {
190 int i;
191
192 args = CLASSTYPE_TI_ARGS (ctx);
193
194 if (args == NULL_TREE)
195 template_class_levels = 1;
196 else
197 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
198 if (uses_template_parms (TREE_VEC_ELT (args, i)))
199 {
200 template_class_levels++;
201 break;
202 }
203 }
204 }
205
206 if (parm_levels > template_class_levels)
207 r = 1;
208 }
209 }
210
211 return r;
212 }
213
214 /* Return a new template argument vector which contains all of ARGS,
215 but has as its innermost set of arguments the EXTRA_ARGS. */
216
217 static tree
218 add_to_template_args (args, extra_args)
219 tree args;
220 tree extra_args;
221 {
222 tree new_args;
223
224 if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC)
225 {
226 new_args = make_tree_vec (2);
227 TREE_VEC_ELT (new_args, 0) = args;
228 }
229 else
230 {
231 int i;
232
233 new_args = make_tree_vec (TREE_VEC_LENGTH (args) - 1);
234
235 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
236 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
237 }
238
239 TREE_VEC_ELT (new_args,
240 TREE_VEC_LENGTH (new_args) - 1) = extra_args;
241
242 return new_args;
243 }
244
245 /* We've got a template header coming up; push to a new level for storing
246 the parms. */
247
248 void
249 begin_template_parm_list ()
250 {
251 pushlevel (0);
252 declare_pseudo_global_level ();
253 ++processing_template_decl;
254 note_template_header (0);
255 }
256
257
258 /* We've just seen template <>. */
259
260 void
261 begin_specialization ()
262 {
263 note_template_header (1);
264 }
265
266
267 /* Called at then end of processing a declaration preceeded by
268 template<>. */
269
270 void
271 end_specialization ()
272 {
273 reset_specialization ();
274 }
275
276
277 /* Any template <>'s that we have seen thus far are not referring to a
278 function specialization. */
279
280 void
281 reset_specialization ()
282 {
283 processing_specialization = 0;
284 template_header_count = 0;
285 }
286
287
288 /* We've just seen a template header. If SPECIALIZATION is non-zero,
289 it was of the form template <>. */
290
291 static void
292 note_template_header (specialization)
293 int specialization;
294 {
295 processing_specialization = specialization;
296 template_header_count++;
297 }
298
299
300 /* Returns non-zero iff a declarator, in which the number of template
301 types that appeared was TEMPLATE_COUNT, is an explicit
302 specialization. */
303
304 static int
305 processing_explicit_specialization (template_count)
306 int template_count;
307 {
308 /* A function declaration is an explicit specialization of a member
309 template if all of the following conditions hold:
310
311 o There was a template <...> preceeding the declaration.
312 o The last template <...> was in fact template <>.
313 o The number of template <...>'s preceeding the declaration, less
314 the number of template classes with arguments specified used to
315 qualify the function name, is 1.
316
317 For example:
318
319 template <> void S<int>::foo();
320 template <class T> template <> void S<T>::foo();
321 template <> struct S<int> { ... template <> void foo(); }
322
323 The first of these is not a specialization of S<int>::foo() (it
324 is instead a specialization of S<T>::foo), while the next two are
325 specializations of member template functions. */
326
327 return processing_specialization
328 && template_header_count > template_count;
329 }
330
331 /* Returns the template function specialized by TEMPLATE_ID, or
332 NULL_TREE if there is none.
333
334 The TEMPLATE_ID is a TEMPLATE_ID_EXPR. The TYPE is
335 the type it has been declared to have. Return the TEMPLATE_DECL
336 that is being specialized, and put the specialization arguments in
337 *TARGS. If no appropriate specialization can be found, NULL_TREE is
338 returned, and *TARGS is assigned NULL_TREE. If complain is
339 non-zero, error messages are printed where appropriate. */
340
341 tree
342 determine_explicit_specialization (template_id, type, targs_out,
343 need_member_template,
344 complain)
345 tree template_id;
346 tree type;
347 tree* targs_out;
348 int need_member_template;
349 int complain;
350 {
351 int i;
352 int overloaded;
353 tree fns;
354 tree matching_fns = NULL_TREE;
355 tree result;
356 tree fn;
357
358 my_friendly_assert (TREE_CODE (template_id) == TEMPLATE_ID_EXPR
359 && TREE_OPERAND (template_id, 0), 0);
360
361 fns = TREE_OPERAND (template_id, 0);
362
363 if (is_overloaded_fn (fns))
364 fn = get_first_fn (fns);
365 else
366 fn = NULL_TREE;
367
368 overloaded = really_overloaded_fn (fns);
369
370 for (; fn != NULL_TREE;
371 fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE)
372 {
373 int dummy = 0;
374 tree targs;
375
376 if (TREE_CODE (fn) != TEMPLATE_DECL
377 || (need_member_template && !is_member_template (fn)))
378 continue;
379
380 if (list_length (TREE_OPERAND (template_id, 1)) > DECL_NTPARMS (fn))
381 continue;
382
383 targs = make_scratch_vec (DECL_NTPARMS (fn));
384
385 /* We allow incomplete unification here, because we are going to
386 check all the functions. */
387 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
388 &TREE_VEC_ELT (targs, 0),
389 type
390 ? TYPE_ARG_TYPES (TREE_TYPE (fn)) : NULL_TREE,
391 type ? TYPE_ARG_TYPES (type) : NULL_TREE,
392 TREE_OPERAND (template_id, 1),
393 &dummy, 1, 1);
394
395 if (i == 0)
396 {
397 /* Unification was successful. See if the return types
398 match. */
399 if (type != NULL_TREE)
400 {
401 tree tmpl_return_type = tsubst (TREE_TYPE (TREE_TYPE (fn)),
402 targs,
403 DECL_NTPARMS (fn),
404 NULL_TREE);
405
406 if (tmpl_return_type != TREE_TYPE (type))
407 {
408 /* Always complain about this. With ambiguity, some
409 other context, might resolve things. But, a
410 non-matching return type will always be a
411 problem. */
412 cp_error ("Return type of explicit specialization of");
413 cp_error ("`%D' is `%T', but should be `%T'.",
414 fn, TREE_TYPE (type), tmpl_return_type);
415 *targs_out = NULL_TREE;
416 return NULL_TREE;
417 }
418 }
419
420 matching_fns = scratch_tree_cons (fn, targs, matching_fns);
421 }
422 }
423
424 if (matching_fns == NULL_TREE)
425 {
426 if (complain)
427 cp_error ("`%D' does not match any template declaration.",
428 template_id);
429
430 *targs_out = NULL_TREE;
431 return NULL_TREE;
432 }
433
434 if (TREE_CHAIN (matching_fns) != NULL_TREE)
435 {
436 if (complain)
437 {
438 tree fn;
439
440 cp_error ("Ambiguous explicit specialization. Candidates are:");
441 for (fn = matching_fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
442 cp_error (" %D", TREE_PURPOSE (fn));
443 }
444
445 *targs_out = NULL_TREE;
446 return NULL_TREE;
447 }
448
449 /* We have one, and exactly one, match. */
450 *targs_out = TREE_VALUE (matching_fns);
451 return TREE_PURPOSE (matching_fns);
452 }
453
454
455 /* Check to see if the function just declared, as indicated in
456 DECLARATOR, and in DECL, is a specialization. Check that the
457 specialization is OK. If FLAGS == 1, we are being called by
458 finish_struct_methods. If FLAGS == 2, we are being called by
459 grokfndecl, and the function has a definition, or is a friend. If
460 FLAGS == 3, this is a friend declaration.
461 Returns 0 if the decl is not an explicit specialization or
462 instantiation, 1 if it is an explicit specialization, and 2 if it
463 is an explicit instantiation. */
464
465 int
466 check_explicit_specialization (declarator, decl, template_count, flags)
467 tree declarator;
468 tree decl;
469 int template_count;
470 int flags;
471 {
472 int finish_member = flags == 1;
473 int have_def = flags == 2;
474 int is_friend = flags == 3;
475
476 if (processing_explicit_specialization (template_count)
477 || finish_member
478 || TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
479 {
480 tree tmpl = NULL_TREE;
481 tree dname = DECL_NAME (decl);
482 tree ctype = DECL_CLASS_CONTEXT (decl);
483 tree targs;
484
485 /* We've come across a declarator that looks like: U f<T1,
486 T2, ...>(A1, A2, ..). This is an explicit template
487 specialization. Check that:
488
489 o The explicitly specified parameters together with those
490 that can be deduced by template argument deduction
491 uniquely determine a particular specialization.
492
493 See [temp.expl.spec]. */
494
495 if (!finish_member
496 && TREE_CODE (declarator) == TEMPLATE_ID_EXPR
497 && !processing_explicit_specialization (template_count)
498 && !is_friend)
499 {
500 if (! have_def && ! template_header_count && ! ctype)
501 /* This is not an explict specialization. It must be
502 an explicit instantiation. */
503 return 2;
504 else if (template_header_count > template_count
505 && !processing_specialization)
506 {
507 cp_error ("template-id `%D' in declaration of primary template",
508 declarator);
509 return 0;
510 }
511 else if (pedantic || uses_template_parms (decl))
512 pedwarn ("explicit specialization not preceeded by `template <>'");
513 }
514
515 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
516 {
517 tree fns;
518
519 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
520 0);
521 if (!ctype)
522 fns = IDENTIFIER_GLOBAL_VALUE (dname);
523 else
524 fns = dname;
525
526 declarator = lookup_template_function (fns, NULL_TREE);
527 }
528
529 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
530 {
531 /* A friend declaration. We can't do much, because we don't
532 know what this resolves to, yet. */
533 my_friendly_assert (is_friend != 0, 0);
534 SET_DECL_IMPLICIT_INSTANTIATION (decl);
535 return 1;
536 }
537
538 if (ctype
539 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE)
540 {
541 tree fns;
542
543 if (TYPE_BEING_DEFINED (ctype) && !finish_member)
544 {
545 /* Since finish_struct_1 has not been called yet, we
546 can't call lookup_fnfields. We note that this
547 template is a specialization, and proceed, letting
548 finish_struct_methods fix this up later. */
549 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
550 DECL_TEMPLATE_INFO (decl)
551 = perm_tree_cons (NULL_TREE,
552 TREE_OPERAND (declarator, 1),
553 NULL_TREE);
554 return 1;
555 }
556
557 fns = lookup_fnfields (TYPE_BINFO (ctype),
558 TREE_OPERAND (declarator, 0),
559 1);
560
561 if (fns == NULL_TREE)
562 {
563 cp_error ("No member template `%s' declared in `%T'",
564 IDENTIFIER_POINTER (TREE_OPERAND (declarator,
565 0)),
566 ctype);
567 return 1;
568 }
569 else
570 TREE_OPERAND (declarator, 0) = fns;
571 }
572
573 tmpl =
574 determine_explicit_specialization
575 (declarator, TREE_TYPE (decl), &targs,
576 TREE_CODE (decl) == TEMPLATE_DECL, 1);
577
578 if (tmpl)
579 {
580 /* Mangle the function name appropriately. */
581 if (name_mangling_version >= 1)
582 {
583 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
584
585 if (ctype
586 && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE)
587 arg_types =
588 hash_tree_chain (build_pointer_type (ctype),
589 arg_types);
590
591 DECL_ASSEMBLER_NAME (decl)
592 = build_template_decl_overload
593 (DECL_NAME (decl),
594 arg_types,
595 TREE_TYPE (TREE_TYPE (tmpl)),
596 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
597 targs, ctype != NULL_TREE);
598 }
599
600 if (is_friend && !have_def)
601 {
602 /* This is not really a declaration of a specialization.
603 It's just the name of an instantiation. But, it's not
604 a request for an instantiation, either. */
605 SET_DECL_IMPLICIT_INSTANTIATION (decl);
606 DECL_TEMPLATE_INFO (decl)
607 = perm_tree_cons (tmpl, targs, NULL_TREE);
608 return 1;
609 }
610
611 /* This function declaration is a template specialization.
612 Record that fact. */
613 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
614 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
615 = perm_tree_cons (targs, decl,
616 DECL_TEMPLATE_SPECIALIZATIONS
617 (tmpl));
618 /* If DECL_TI_TEMPLATE (decl), the decl is an
619 instantiation of a specialization of a member template.
620 (In other words, there was a member template, in a
621 class template. That member template was specialized.
622 We then instantiated the class, so there is now an
623 instance of that specialization.)
624
625 According to the CD2,
626
627 14.7.3.13 [tmpl.expl.spec]
628
629 A specialization of a member function template or
630 member class template of a non-specialized class
631 template is itself a template.
632
633 So, we just leave the template info alone in this case. */
634 if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
635 DECL_TEMPLATE_INFO (decl)
636 = perm_tree_cons (tmpl, targs, NULL_TREE);
637 return 1;
638 }
639 }
640
641 return 0;
642 }
643
644 /* Process information from new template parameter NEXT and append it to the
645 LIST being built. */
646
647 tree
648 process_template_parm (list, next)
649 tree list, next;
650 {
651 tree parm;
652 tree decl = 0;
653 tree defval;
654 int is_type, idx;
655 parm = next;
656 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
657 defval = TREE_PURPOSE (parm);
658 parm = TREE_VALUE (parm);
659 is_type = TREE_PURPOSE (parm) == class_type_node;
660
661 if (list)
662 {
663 tree p = TREE_VALUE (tree_last (list));
664
665 if (TREE_CODE (p) == TYPE_DECL)
666 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
667 else
668 idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
669 ++idx;
670 }
671 else
672 idx = 0;
673
674 if (!is_type)
675 {
676 tree tinfo = 0;
677 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
678 /* is a const-param */
679 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
680 PARM, 0, NULL_TREE);
681 /* A template parameter is not modifiable. */
682 TREE_READONLY (parm) = 1;
683 if (IS_AGGR_TYPE (TREE_TYPE (parm))
684 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
685 {
686 cp_error ("`%#T' is not a valid type for a template constant parameter",
687 TREE_TYPE (parm));
688 if (DECL_NAME (parm) == NULL_TREE)
689 error (" a template type parameter must begin with `class' or `typename'");
690 TREE_TYPE (parm) = void_type_node;
691 }
692 else if (pedantic
693 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
694 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
695 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
696 TREE_TYPE (parm));
697 tinfo = make_node (TEMPLATE_CONST_PARM);
698 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
699 if (TREE_PERMANENT (parm) == 0)
700 {
701 parm = copy_node (parm);
702 TREE_PERMANENT (parm) = 1;
703 }
704 TREE_TYPE (tinfo) = TREE_TYPE (parm);
705 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
706 DECL_INITIAL (decl) = tinfo;
707 DECL_INITIAL (parm) = tinfo;
708 TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl);
709 }
710 else
711 {
712 tree t = make_lang_type (TEMPLATE_TYPE_PARM);
713 CLASSTYPE_GOT_SEMICOLON (t) = 1;
714 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
715 TYPE_NAME (t) = decl;
716 TYPE_STUB_DECL (t) = decl;
717 parm = decl;
718 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
719 }
720 SET_DECL_ARTIFICIAL (decl);
721 pushdecl (decl);
722 parm = build_tree_list (defval, parm);
723 return chainon (list, parm);
724 }
725
726 /* The end of a template parameter list has been reached. Process the
727 tree list into a parameter vector, converting each parameter into a more
728 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
729 as PARM_DECLs. */
730
731 tree
732 end_template_parm_list (parms)
733 tree parms;
734 {
735 int nparms;
736 tree parm;
737 tree saved_parmlist = make_tree_vec (list_length (parms));
738
739 current_template_parms
740 = tree_cons (build_int_2 (0, processing_template_decl),
741 saved_parmlist, current_template_parms);
742
743 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
744 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
745
746 return saved_parmlist;
747 }
748
749 /* end_template_decl is called after a template declaration is seen. */
750
751 void
752 end_template_decl ()
753 {
754 reset_specialization ();
755
756 if (! processing_template_decl)
757 return;
758
759 /* This matches the pushlevel in begin_template_parm_list. */
760 poplevel (0, 0, 0);
761
762 --processing_template_decl;
763 current_template_parms = TREE_CHAIN (current_template_parms);
764 (void) get_pending_sizes (); /* Why? */
765 }
766
767 /* Generate a valid set of template args from current_template_parms. */
768
769 tree
770 current_template_args ()
771 {
772 tree header = current_template_parms;
773 int length = list_length (header);
774 tree args = make_tree_vec (length);
775 int l = length;
776
777 while (header)
778 {
779 tree a = copy_node (TREE_VALUE (header));
780 int i = TREE_VEC_LENGTH (a);
781 TREE_TYPE (a) = NULL_TREE;
782 while (i--)
783 {
784 tree t = TREE_VEC_ELT (a, i);
785
786 /* t will be a list if we are called from within a
787 begin/end_template_parm_list pair, but a vector directly
788 if within a begin/end_member_template_processing pair. */
789 if (TREE_CODE (t) == TREE_LIST)
790 {
791 t = TREE_VALUE (t);
792
793 if (TREE_CODE (t) == TYPE_DECL)
794 t = TREE_TYPE (t);
795 else
796 t = DECL_INITIAL (t);
797 }
798
799 TREE_VEC_ELT (a, i) = t;
800 }
801 TREE_VEC_ELT (args, --l) = a;
802 header = TREE_CHAIN (header);
803 }
804
805 return args;
806 }
807
808 void
809 push_template_decl (decl)
810 tree decl;
811 {
812 tree tmpl;
813 tree args = NULL_TREE;
814 tree info;
815 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
816 int primary = 0;
817
818 /* Kludge! */
819 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
820 && DECL_CLASS_CONTEXT (decl))
821 ;
822 /* Note that this template is a "primary template" */
823 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
824 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
825 primary = 1;
826
827 /* Partial specialization. */
828 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
829 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
830 {
831 tree type = TREE_TYPE (decl);
832 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
833 tree mainargs = CLASSTYPE_TI_ARGS (type);
834 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
835
836 for (; spec; spec = TREE_CHAIN (spec))
837 {
838 /* purpose: args to main template
839 value: spec template */
840 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
841 return;
842 }
843
844 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
845 = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms),
846 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
847 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
848 return;
849 }
850
851 args = current_template_args ();
852
853 if (! ctx || TYPE_BEING_DEFINED (ctx))
854 {
855 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
856 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
857 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
858 if (DECL_LANG_SPECIFIC (decl))
859 {
860 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
861 DECL_STATIC_FUNCTION_P (tmpl) =
862 DECL_STATIC_FUNCTION_P (decl);
863
864 if (DECL_TEMPLATE_SPECIALIZATION (decl))
865 {
866 /* A specialization of a member template of a template
867 class. */
868 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
869 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
870 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
871 }
872 }
873 }
874 else
875 {
876 tree t;
877 tree a;
878
879 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
880 cp_error ("must specialize `%#T' before defining member `%#D'",
881 ctx, decl);
882 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
883 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
884 else if (! DECL_TEMPLATE_INFO (decl))
885 {
886 cp_error ("template definition of non-template `%#D'", decl);
887 return;
888 }
889 else
890 tmpl = DECL_TI_TEMPLATE (decl);
891
892 if (is_member_template (tmpl))
893 {
894 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
895 t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
896 if (TREE_VEC_LENGTH (t)
897 != TREE_VEC_LENGTH (a))
898 {
899 cp_error ("got %d template parameters for `%#D'",
900 TREE_VEC_LENGTH (a), decl);
901 cp_error (" but %d required", TREE_VEC_LENGTH (t));
902 }
903 if (TREE_VEC_LENGTH (args) > 1)
904 /* Get the template parameters for the enclosing template
905 class. */
906 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2);
907 else
908 a = NULL_TREE;
909 }
910 else
911 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
912
913 t = NULL_TREE;
914
915 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
916 {
917 /* When processing an inline member template of a
918 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
919 if (CLASSTYPE_TI_SPEC_INFO (ctx))
920 t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
921 }
922 else if (CLASSTYPE_TEMPLATE_INFO (ctx))
923 t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));
924
925 /* There should be template arguments if and only if there is a
926 template class. */
927 my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0);
928
929 if (t != NULL_TREE
930 && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
931 {
932 cp_error ("got %d template parameters for `%#D'",
933 TREE_VEC_LENGTH (a), decl);
934 cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
935 }
936 }
937 /* Get the innermost set of template arguments. */
938 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
939
940 DECL_TEMPLATE_RESULT (tmpl) = decl;
941 TREE_TYPE (tmpl) = TREE_TYPE (decl);
942
943 if (! ctx)
944 tmpl = pushdecl_top_level (tmpl);
945
946 if (primary)
947 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
948
949 info = perm_tree_cons (tmpl, args, NULL_TREE);
950
951 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
952 {
953 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
954 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
955 }
956 else if (! DECL_LANG_SPECIFIC (decl))
957 cp_error ("template declaration of `%#D'", decl);
958 else
959 DECL_TEMPLATE_INFO (decl) = info;
960 }
961
962 /* Convert all template arguments to their appropriate types, and return
963 a vector containing the resulting values. If any error occurs, return
964 error_mark_node. */
965
966 static tree
967 coerce_template_parms (parms, arglist, in_decl)
968 tree parms, arglist;
969 tree in_decl;
970 {
971 int nparms, nargs, i, lost = 0;
972 tree vec;
973
974 if (arglist == NULL_TREE)
975 nargs = 0;
976 else if (TREE_CODE (arglist) == TREE_VEC)
977 nargs = TREE_VEC_LENGTH (arglist);
978 else
979 nargs = list_length (arglist);
980
981 nparms = TREE_VEC_LENGTH (parms);
982
983 if (nargs > nparms
984 || (nargs < nparms
985 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
986 {
987 error ("incorrect number of parameters (%d, should be %d)",
988 nargs, nparms);
989 if (in_decl)
990 cp_error_at ("in template expansion for decl `%D'", in_decl);
991 return error_mark_node;
992 }
993
994 if (arglist && TREE_CODE (arglist) == TREE_VEC)
995 vec = copy_node (arglist);
996 else
997 {
998 vec = make_tree_vec (nparms);
999 for (i = 0; i < nparms; i++)
1000 {
1001 tree arg;
1002
1003 if (arglist)
1004 {
1005 arg = arglist;
1006 arglist = TREE_CHAIN (arglist);
1007
1008 if (arg == error_mark_node)
1009 lost++;
1010 else
1011 arg = TREE_VALUE (arg);
1012 }
1013 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i)))
1014 == TYPE_DECL)
1015 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1016 vec, i, in_decl);
1017 else
1018 arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1019 vec, i, in_decl);
1020
1021 TREE_VEC_ELT (vec, i) = arg;
1022 }
1023 }
1024 for (i = 0; i < nparms; i++)
1025 {
1026 tree arg = TREE_VEC_ELT (vec, i);
1027 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1028 tree val = 0;
1029 int is_type, requires_type;
1030
1031 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
1032 requires_type = TREE_CODE (parm) == TYPE_DECL;
1033
1034 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
1035 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
1036 {
1037 cp_pedwarn ("to refer to a type member of a template parameter,");
1038 cp_pedwarn (" use `typename %E'", arg);
1039 arg = make_typename_type (TREE_OPERAND (arg, 0),
1040 TREE_OPERAND (arg, 1));
1041 is_type = 1;
1042 }
1043 if (is_type != requires_type)
1044 {
1045 if (in_decl)
1046 {
1047 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1048 i, in_decl);
1049 if (is_type)
1050 cp_error (" expected a constant of type `%T', got `%T'",
1051 TREE_TYPE (parm), arg);
1052 else
1053 cp_error (" expected a type, got `%E'", arg);
1054 }
1055 lost++;
1056 TREE_VEC_ELT (vec, i) = error_mark_node;
1057 continue;
1058 }
1059 if (is_type)
1060 {
1061 val = groktypename (arg);
1062 if (! processing_template_decl)
1063 {
1064 tree t = target_type (val);
1065 if (TREE_CODE (t) != TYPENAME_TYPE
1066 && IS_AGGR_TYPE (t)
1067 && decl_function_context (TYPE_MAIN_DECL (t)))
1068 {
1069 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
1070 return error_mark_node;
1071 }
1072 }
1073 }
1074 else
1075 {
1076 tree t = tsubst (TREE_TYPE (parm), vec,
1077 TREE_VEC_LENGTH (vec), in_decl);
1078 if (processing_template_decl)
1079 val = arg;
1080 else
1081 val = digest_init (t, arg, (tree *) 0);
1082
1083 if (val == error_mark_node || processing_template_decl)
1084 ;
1085
1086 /* 14.2: Other template-arguments must be constant-expressions,
1087 addresses of objects or functions with external linkage, or of
1088 static class members. */
1089 else if (IS_AGGR_TYPE (TREE_TYPE (val)))
1090 {
1091 cp_error ("object `%E' cannot be used as template argument", arg);
1092 val = error_mark_node;
1093 }
1094 else if (!TREE_CONSTANT (val))
1095 {
1096 cp_error ("non-constant `%E' cannot be used as template argument",
1097 arg);
1098 val = error_mark_node;
1099 }
1100 else if (POINTER_TYPE_P (TREE_TYPE (val))
1101 && ! integer_zerop (val)
1102 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
1103 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
1104 {
1105 t = val;
1106 STRIP_NOPS (t);
1107 if (TREE_CODE (t) == ADDR_EXPR)
1108 {
1109 tree a = TREE_OPERAND (t, 0);
1110 STRIP_NOPS (a);
1111 if (TREE_CODE (a) == STRING_CST)
1112 {
1113 cp_error ("string literal %E is not a valid template argument", a);
1114 error ("because it is the address of an object with static linkage");
1115 val = error_mark_node;
1116 }
1117 else if (TREE_CODE (a) != VAR_DECL
1118 && TREE_CODE (a) != FUNCTION_DECL)
1119 goto bad;
1120 else if (! TREE_PUBLIC (a))
1121 {
1122 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
1123 val = error_mark_node;
1124 }
1125 }
1126 else
1127 {
1128 bad:
1129 cp_error ("`%E' is not a valid template argument", t);
1130 error ("it must be %s%s with external linkage",
1131 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
1132 ? "a pointer to " : "",
1133 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
1134 ? "a function" : "an object");
1135 val = error_mark_node;
1136 }
1137 }
1138 }
1139
1140 if (val == error_mark_node)
1141 lost++;
1142
1143 TREE_VEC_ELT (vec, i) = val;
1144 }
1145 if (lost)
1146 return error_mark_node;
1147 return vec;
1148 }
1149
1150 static int
1151 comp_template_args (oldargs, newargs)
1152 tree oldargs, newargs;
1153 {
1154 int i;
1155
1156 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
1157 return 0;
1158
1159 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
1160 {
1161 tree nt = TREE_VEC_ELT (newargs, i);
1162 tree ot = TREE_VEC_ELT (oldargs, i);
1163
1164 if (nt == ot)
1165 continue;
1166 if (TREE_CODE (nt) != TREE_CODE (ot))
1167 return 0;
1168 if (TREE_CODE (nt) == TREE_VEC)
1169 {
1170 /* For member templates */
1171 if (comp_template_args (nt, ot))
1172 continue;
1173 }
1174 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
1175 {
1176 if (comptypes (ot, nt, 1))
1177 continue;
1178 }
1179 else if (cp_tree_equal (ot, nt) > 0)
1180 continue;
1181 return 0;
1182 }
1183 return 1;
1184 }
1185
1186 /* Given class template name and parameter list, produce a user-friendly name
1187 for the instantiation. */
1188
1189 static char *
1190 mangle_class_name_for_template (name, parms, arglist)
1191 char *name;
1192 tree parms, arglist;
1193 {
1194 static struct obstack scratch_obstack;
1195 static char *scratch_firstobj;
1196 int i, nparms;
1197
1198 if (!scratch_firstobj)
1199 gcc_obstack_init (&scratch_obstack);
1200 else
1201 obstack_free (&scratch_obstack, scratch_firstobj);
1202 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
1203
1204 #if 0
1205 #define buflen sizeof(buf)
1206 #define check if (bufp >= buf+buflen-1) goto too_long
1207 #define ccat(c) *bufp++=(c); check
1208 #define advance bufp+=strlen(bufp); check
1209 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1210 #else
1211 #define check
1212 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1213 #define advance
1214 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1215 #endif
1216
1217 cat (name);
1218 ccat ('<');
1219 nparms = TREE_VEC_LENGTH (parms);
1220 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
1221 for (i = 0; i < nparms; i++)
1222 {
1223 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1224 tree arg = TREE_VEC_ELT (arglist, i);
1225
1226 if (i)
1227 ccat (',');
1228
1229 if (TREE_CODE (parm) == TYPE_DECL)
1230 {
1231 cat (type_as_string (arg, 0));
1232 continue;
1233 }
1234 else
1235 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
1236
1237 if (TREE_CODE (arg) == TREE_LIST)
1238 {
1239 /* New list cell was built because old chain link was in
1240 use. */
1241 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
1242 arg = TREE_VALUE (arg);
1243 }
1244 /* No need to check arglist against parmlist here; we did that
1245 in coerce_template_parms, called from lookup_template_class. */
1246 cat (expr_as_string (arg, 0));
1247 }
1248 {
1249 char *bufp = obstack_next_free (&scratch_obstack);
1250 int offset = 0;
1251 while (bufp[offset - 1] == ' ')
1252 offset--;
1253 obstack_blank_fast (&scratch_obstack, offset);
1254
1255 /* B<C<char> >, not B<C<char>> */
1256 if (bufp[offset - 1] == '>')
1257 ccat (' ');
1258 }
1259 ccat ('>');
1260 ccat ('\0');
1261 return (char *) obstack_base (&scratch_obstack);
1262
1263 #if 0
1264 too_long:
1265 #endif
1266 fatal ("out of (preallocated) string space creating template instantiation name");
1267 /* NOTREACHED */
1268 return NULL;
1269 }
1270
1271 static tree
1272 classtype_mangled_name (t)
1273 tree t;
1274 {
1275 if (CLASSTYPE_TEMPLATE_INFO (t)
1276 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
1277 {
1278 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
1279 char *mangled_name = mangle_class_name_for_template
1280 (IDENTIFIER_POINTER (name),
1281 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
1282 CLASSTYPE_TI_ARGS (t));
1283 tree id = get_identifier (mangled_name);
1284 IDENTIFIER_TEMPLATE (id) = name;
1285 return id;
1286 }
1287 else
1288 return TYPE_IDENTIFIER (t);
1289 }
1290
1291 static void
1292 add_pending_template (d)
1293 tree d;
1294 {
1295 tree ti;
1296
1297 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
1298 ti = CLASSTYPE_TEMPLATE_INFO (d);
1299 else
1300 ti = DECL_TEMPLATE_INFO (d);
1301
1302 if (TI_PENDING_TEMPLATE_FLAG (ti))
1303 return;
1304
1305 *template_tail = perm_tree_cons
1306 (current_function_decl, d, NULL_TREE);
1307 template_tail = &TREE_CHAIN (*template_tail);
1308 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
1309 }
1310
1311
1312 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1313 may be either a _DECL or an overloaded function or an
1314 IDENTIFIER_NODE), and ARGLIST. */
1315
1316 tree
1317 lookup_template_function (fns, arglist)
1318 tree fns, arglist;
1319 {
1320 if (fns == NULL_TREE)
1321 {
1322 cp_error ("non-template used as template");
1323 return error_mark_node;
1324 }
1325
1326 if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
1327 {
1328 push_obstacks (&permanent_obstack, &permanent_obstack);
1329 arglist = copy_list (arglist);
1330 pop_obstacks ();
1331 }
1332
1333 return build_min (TEMPLATE_ID_EXPR,
1334 TREE_TYPE (fns)
1335 ? TREE_TYPE (fns) : unknown_type_node,
1336 fns, arglist);
1337 }
1338
1339
1340 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1341 parameters, find the desired type.
1342
1343 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1344 Since ARGLIST is build on the decl_obstack, we must copy it here
1345 to keep it from being reclaimed when the decl storage is reclaimed.
1346
1347 IN_DECL, if non-NULL, is the template declaration we are trying to
1348 instantiate. */
1349
1350 tree
1351 lookup_template_class (d1, arglist, in_decl)
1352 tree d1, arglist;
1353 tree in_decl;
1354 {
1355 tree template, parmlist;
1356 char *mangled_name;
1357 tree id, t;
1358
1359 if (TREE_CODE (d1) == IDENTIFIER_NODE)
1360 {
1361 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
1362 if (! template)
1363 template = IDENTIFIER_CLASS_VALUE (d1);
1364 }
1365 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
1366 {
1367 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
1368 d1 = DECL_NAME (template);
1369 }
1370 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
1371 {
1372 template = CLASSTYPE_TI_TEMPLATE (d1);
1373 d1 = DECL_NAME (template);
1374 }
1375 else
1376 my_friendly_abort (272);
1377
1378 /* With something like `template <class T> class X class X { ... };'
1379 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1380 We don't want to do that, but we have to deal with the situation, so
1381 let's give them some syntax errors to chew on instead of a crash. */
1382 if (! template)
1383 return error_mark_node;
1384 if (TREE_CODE (template) != TEMPLATE_DECL)
1385 {
1386 cp_error ("non-template type `%T' used as a template", d1);
1387 if (in_decl)
1388 cp_error_at ("for template declaration `%D'", in_decl);
1389 return error_mark_node;
1390 }
1391
1392 if (PRIMARY_TEMPLATE_P (template))
1393 {
1394 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
1395
1396 arglist = coerce_template_parms (parmlist, arglist, template);
1397 if (arglist == error_mark_node)
1398 return error_mark_node;
1399 if (uses_template_parms (arglist))
1400 {
1401 tree found;
1402 if (comp_template_args
1403 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
1404 found = TREE_TYPE (template);
1405 else
1406 {
1407 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
1408 found; found = TREE_CHAIN (found))
1409 {
1410 if (TI_USES_TEMPLATE_PARMS (found)
1411 && comp_template_args (TREE_PURPOSE (found), arglist))
1412 break;
1413 }
1414 if (found)
1415 found = TREE_VALUE (found);
1416 }
1417
1418 if (found)
1419 {
1420 if (can_free (&permanent_obstack, arglist))
1421 obstack_free (&permanent_obstack, arglist);
1422 return found;
1423 }
1424 }
1425
1426 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
1427 parmlist, arglist);
1428 id = get_identifier (mangled_name);
1429 IDENTIFIER_TEMPLATE (id) = d1;
1430
1431 maybe_push_to_top_level (uses_template_parms (arglist));
1432 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
1433 pop_from_top_level ();
1434 }
1435 else
1436 {
1437 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1438 arglist, in_decl);
1439 id = d1;
1440 arglist = CLASSTYPE_TI_ARGS (ctx);
1441
1442 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
1443 {
1444 int save_temp = processing_template_decl;
1445 processing_template_decl = 0;
1446 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
1447 processing_template_decl = save_temp;
1448 }
1449 else
1450 {
1451 t = lookup_nested_type_by_name (ctx, id);
1452 my_friendly_assert (t != NULL_TREE, 42);
1453 }
1454 }
1455
1456 /* Seems to be wanted. */
1457 CLASSTYPE_GOT_SEMICOLON (t) = 1;
1458
1459 if (! CLASSTYPE_TEMPLATE_INFO (t))
1460 {
1461 arglist = copy_to_permanent (arglist);
1462 CLASSTYPE_TEMPLATE_INFO (t)
1463 = perm_tree_cons (template, arglist, NULL_TREE);
1464 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1465 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
1466 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1467 = uses_template_parms (arglist);
1468
1469 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
1470
1471 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1472 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
1473 /* if (! uses_template_parms (arglist)) */
1474 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
1475 = get_identifier (build_overload_name (t, 1, 1));
1476
1477 if (flag_external_templates && ! uses_template_parms (arglist)
1478 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1479 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1480 add_pending_template (t);
1481 }
1482
1483 return t;
1484 }
1485 \f
1486 /* Should be defined in parse.h. */
1487 extern int yychar;
1488
1489 int
1490 uses_template_parms (t)
1491 tree t;
1492 {
1493 if (!t)
1494 return 0;
1495 switch (TREE_CODE (t))
1496 {
1497 case INDIRECT_REF:
1498 case COMPONENT_REF:
1499 /* We assume that the object must be instantiated in order to build
1500 the COMPONENT_REF, so we test only whether the type of the
1501 COMPONENT_REF uses template parms. */
1502 return uses_template_parms (TREE_TYPE (t));
1503
1504 case IDENTIFIER_NODE:
1505 if (!IDENTIFIER_TEMPLATE (t))
1506 return 0;
1507 my_friendly_abort (42);
1508
1509 /* aggregates of tree nodes */
1510 case TREE_VEC:
1511 {
1512 int i = TREE_VEC_LENGTH (t);
1513 while (i--)
1514 if (uses_template_parms (TREE_VEC_ELT (t, i)))
1515 return 1;
1516 return 0;
1517 }
1518 case TREE_LIST:
1519 if (uses_template_parms (TREE_PURPOSE (t))
1520 || uses_template_parms (TREE_VALUE (t)))
1521 return 1;
1522 return uses_template_parms (TREE_CHAIN (t));
1523
1524 /* constructed type nodes */
1525 case POINTER_TYPE:
1526 case REFERENCE_TYPE:
1527 return uses_template_parms (TREE_TYPE (t));
1528 case RECORD_TYPE:
1529 if (TYPE_PTRMEMFUNC_FLAG (t))
1530 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
1531 case UNION_TYPE:
1532 if (! CLASSTYPE_TEMPLATE_INFO (t))
1533 return 0;
1534 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
1535 case FUNCTION_TYPE:
1536 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1537 return 1;
1538 return uses_template_parms (TREE_TYPE (t));
1539 case ARRAY_TYPE:
1540 if (uses_template_parms (TYPE_DOMAIN (t)))
1541 return 1;
1542 return uses_template_parms (TREE_TYPE (t));
1543 case OFFSET_TYPE:
1544 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
1545 return 1;
1546 return uses_template_parms (TREE_TYPE (t));
1547 case METHOD_TYPE:
1548 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
1549 return 1;
1550 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1551 return 1;
1552 return uses_template_parms (TREE_TYPE (t));
1553
1554 /* decl nodes */
1555 case TYPE_DECL:
1556 return uses_template_parms (TREE_TYPE (t));
1557
1558 case FUNCTION_DECL:
1559 case VAR_DECL:
1560 /* ??? What about FIELD_DECLs? */
1561 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
1562 && uses_template_parms (DECL_TI_ARGS (t)))
1563 return 1;
1564 /* fall through */
1565 case CONST_DECL:
1566 case PARM_DECL:
1567 if (uses_template_parms (TREE_TYPE (t)))
1568 return 1;
1569 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
1570 return 1;
1571 return 0;
1572
1573 case CALL_EXPR:
1574 return uses_template_parms (TREE_TYPE (t));
1575 case ADDR_EXPR:
1576 return uses_template_parms (TREE_OPERAND (t, 0));
1577
1578 /* template parm nodes */
1579 case TEMPLATE_TYPE_PARM:
1580 case TEMPLATE_CONST_PARM:
1581 return 1;
1582
1583 /* simple type nodes */
1584 case INTEGER_TYPE:
1585 if (uses_template_parms (TYPE_MIN_VALUE (t)))
1586 return 1;
1587 return uses_template_parms (TYPE_MAX_VALUE (t));
1588
1589 case REAL_TYPE:
1590 case COMPLEX_TYPE:
1591 case VOID_TYPE:
1592 case BOOLEAN_TYPE:
1593 return 0;
1594
1595 case ENUMERAL_TYPE:
1596 {
1597 tree v;
1598
1599 for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
1600 if (uses_template_parms (TREE_VALUE (v)))
1601 return 1;
1602 }
1603 return 0;
1604
1605 /* constants */
1606 case INTEGER_CST:
1607 case REAL_CST:
1608 case STRING_CST:
1609 return 0;
1610
1611 case ERROR_MARK:
1612 /* Non-error_mark_node ERROR_MARKs are bad things. */
1613 my_friendly_assert (t == error_mark_node, 274);
1614 /* NOTREACHED */
1615 return 0;
1616
1617 case LOOKUP_EXPR:
1618 case TYPENAME_TYPE:
1619 return 1;
1620
1621 case SCOPE_REF:
1622 return uses_template_parms (TREE_OPERAND (t, 0));
1623
1624 case CONSTRUCTOR:
1625 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1626 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1627 return uses_template_parms (TREE_OPERAND (t, 1));
1628
1629 case MODOP_EXPR:
1630 case CAST_EXPR:
1631 case REINTERPRET_CAST_EXPR:
1632 case CONST_CAST_EXPR:
1633 case STATIC_CAST_EXPR:
1634 case DYNAMIC_CAST_EXPR:
1635 case SIZEOF_EXPR:
1636 case ARROW_EXPR:
1637 case DOTSTAR_EXPR:
1638 case TYPEID_EXPR:
1639 return 1;
1640
1641 default:
1642 switch (TREE_CODE_CLASS (TREE_CODE (t)))
1643 {
1644 case '1':
1645 case '2':
1646 case 'e':
1647 case '<':
1648 {
1649 int i;
1650 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
1651 if (uses_template_parms (TREE_OPERAND (t, i)))
1652 return 1;
1653 return 0;
1654 }
1655 default:
1656 break;
1657 }
1658 sorry ("testing %s for template parms",
1659 tree_code_name [(int) TREE_CODE (t)]);
1660 my_friendly_abort (82);
1661 /* NOTREACHED */
1662 return 0;
1663 }
1664 }
1665
1666 static struct tinst_level *current_tinst_level = 0;
1667 static struct tinst_level *free_tinst_level = 0;
1668 static int tinst_depth = 0;
1669 extern int max_tinst_depth;
1670 #ifdef GATHER_STATISTICS
1671 int depth_reached = 0;
1672 #endif
1673
1674 static int
1675 push_tinst_level (d)
1676 tree d;
1677 {
1678 struct tinst_level *new;
1679
1680 if (tinst_depth >= max_tinst_depth)
1681 {
1682 struct tinst_level *p = current_tinst_level;
1683 int line = lineno;
1684 char *file = input_filename;
1685
1686 error ("template instantiation depth exceeds maximum of %d",
1687 max_tinst_depth);
1688 error (" (use -ftemplate-depth-NN to increase the maximum)");
1689 cp_error (" instantiating `%D'", d);
1690
1691 for (; p; p = p->next)
1692 {
1693 cp_error (" instantiated from `%D'", p->decl);
1694 lineno = p->line;
1695 input_filename = p->file;
1696 }
1697 error (" instantiated from here");
1698
1699 lineno = line;
1700 input_filename = file;
1701
1702 return 0;
1703 }
1704
1705 if (free_tinst_level)
1706 {
1707 new = free_tinst_level;
1708 free_tinst_level = new->next;
1709 }
1710 else
1711 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1712
1713 new->decl = d;
1714 new->line = lineno;
1715 new->file = input_filename;
1716 new->next = current_tinst_level;
1717 current_tinst_level = new;
1718
1719 ++tinst_depth;
1720 #ifdef GATHER_STATISTICS
1721 if (tinst_depth > depth_reached)
1722 depth_reached = tinst_depth;
1723 #endif
1724
1725 return 1;
1726 }
1727
1728 void
1729 pop_tinst_level ()
1730 {
1731 struct tinst_level *old = current_tinst_level;
1732
1733 current_tinst_level = old->next;
1734 old->next = free_tinst_level;
1735 free_tinst_level = old;
1736 --tinst_depth;
1737 }
1738
1739 struct tinst_level *
1740 tinst_for_decl ()
1741 {
1742 struct tinst_level *p = current_tinst_level;
1743
1744 if (p)
1745 for (; p->next ; p = p->next )
1746 ;
1747 return p;
1748 }
1749
1750 tree
1751 instantiate_class_template (type)
1752 tree type;
1753 {
1754 tree template, template_info, args, pattern, t, *field_chain;
1755
1756 if (type == error_mark_node)
1757 return error_mark_node;
1758
1759 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1760
1761 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1762 return type;
1763
1764 template = TI_TEMPLATE (template_info);
1765 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1766 args = TI_ARGS (template_info);
1767
1768 t = most_specialized_class
1769 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1770
1771 if (t == error_mark_node)
1772 {
1773 char *str = "candidates are:";
1774 cp_error ("ambiguous class template instantiation for `%#T'", type);
1775 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1776 {
1777 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1778 {
1779 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1780 str = " ";
1781 }
1782 }
1783 TYPE_BEING_DEFINED (type) = 1;
1784 return error_mark_node;
1785 }
1786 else if (t)
1787 pattern = TREE_TYPE (t);
1788 else
1789 pattern = TREE_TYPE (template);
1790
1791 if (TYPE_SIZE (pattern) == NULL_TREE)
1792 return type;
1793
1794 if (t)
1795 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1796
1797 TYPE_BEING_DEFINED (type) = 1;
1798
1799 if (! push_tinst_level (type))
1800 return type;
1801
1802 maybe_push_to_top_level (uses_template_parms (type));
1803 pushclass (type, 0);
1804
1805 if (flag_external_templates)
1806 {
1807 if (flag_alt_external_templates)
1808 {
1809 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1810 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1811 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1812 = ! CLASSTYPE_INTERFACE_ONLY (type)
1813 && CLASSTYPE_INTERFACE_KNOWN (type);
1814 }
1815 else
1816 {
1817 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1818 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1819 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1820 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1821 = ! CLASSTYPE_INTERFACE_ONLY (type)
1822 && CLASSTYPE_INTERFACE_KNOWN (type);
1823 }
1824 }
1825 else
1826 {
1827 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1828 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
1829 }
1830
1831 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1832 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1833 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1834 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1835 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1836 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1837 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1838 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1839 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1840 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1841 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1842 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1843 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1844 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1845 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1846 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1847 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1848 TYPE_USES_COMPLEX_INHERITANCE (type)
1849 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1850 TYPE_USES_MULTIPLE_INHERITANCE (type)
1851 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1852 TYPE_USES_VIRTUAL_BASECLASSES (type)
1853 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1854 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1855 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1856
1857 {
1858 tree binfo = TYPE_BINFO (type);
1859 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1860
1861 if (pbases)
1862 {
1863 tree bases;
1864 int i;
1865 int len = TREE_VEC_LENGTH (pbases);
1866 bases = make_tree_vec (len);
1867 for (i = 0; i < len; ++i)
1868 {
1869 tree elt;
1870
1871 TREE_VEC_ELT (bases, i) = elt
1872 = tsubst (TREE_VEC_ELT (pbases, i), args,
1873 TREE_VEC_LENGTH (args), NULL_TREE);
1874 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1875
1876 if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
1877 cp_error
1878 ("base type `%T' of `%T' fails to be a struct or class type",
1879 TREE_TYPE (elt), type);
1880 else if (! uses_template_parms (type)
1881 && (TYPE_SIZE (complete_type (TREE_TYPE (elt)))
1882 == NULL_TREE))
1883 cp_error ("base class `%T' of `%T' has incomplete type",
1884 TREE_TYPE (elt), type);
1885 }
1886 /* Don't initialize this until the vector is filled out, or
1887 lookups will crash. */
1888 BINFO_BASETYPES (binfo) = bases;
1889 }
1890 }
1891
1892 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1893
1894 field_chain = &TYPE_FIELDS (type);
1895
1896 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1897 {
1898 tree name = TREE_PURPOSE (t);
1899 tree tag = TREE_VALUE (t);
1900
1901 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1902 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1903 {
1904 tree e, newtag = tsubst_enum (tag, args,
1905 TREE_VEC_LENGTH (args), field_chain);
1906
1907 while (*field_chain)
1908 {
1909 DECL_FIELD_CONTEXT (*field_chain) = type;
1910 field_chain = &TREE_CHAIN (*field_chain);
1911 }
1912 }
1913 else
1914 tsubst (tag, args,
1915 TREE_VEC_LENGTH (args), NULL_TREE);
1916 }
1917
1918 /* Don't replace enum constants here. */
1919 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1920 if (TREE_CODE (t) != CONST_DECL)
1921 {
1922 tree r = tsubst (t, args,
1923 TREE_VEC_LENGTH (args), NULL_TREE);
1924 if (TREE_CODE (r) == VAR_DECL)
1925 {
1926 if (! uses_template_parms (r))
1927 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1928 /* Perhaps I should do more of grokfield here. */
1929 start_decl_1 (r);
1930 DECL_IN_AGGR_P (r) = 1;
1931 DECL_EXTERNAL (r) = 1;
1932 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1933 }
1934
1935 *field_chain = r;
1936 field_chain = &TREE_CHAIN (r);
1937 }
1938
1939 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1940 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
1941 {
1942 if (DECL_CONSTRUCTOR_P (t))
1943 grok_ctor_properties (type, t);
1944 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
1945 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
1946 }
1947
1948 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1949 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1950 args, TREE_VEC_LENGTH (args), NULL_TREE);
1951
1952 {
1953 tree d = CLASSTYPE_FRIEND_CLASSES (type)
1954 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args,
1955 TREE_VEC_LENGTH (args), NULL_TREE);
1956
1957 /* This does injection for friend classes. */
1958 for (; d; d = TREE_CHAIN (d))
1959 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1960
1961 d = tsubst (DECL_TEMPLATE_INJECT (template), args,
1962 TREE_VEC_LENGTH (args), NULL_TREE);
1963
1964 for (; d; d = TREE_CHAIN (d))
1965 {
1966 tree t = TREE_VALUE (d);
1967
1968 if (TREE_CODE (t) == TYPE_DECL)
1969 /* Already injected. */;
1970 else
1971 pushdecl (t);
1972 }
1973 }
1974
1975 if (! uses_template_parms (type))
1976 {
1977 tree tmp;
1978 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1979 if (TREE_CODE (tmp) == FIELD_DECL)
1980 {
1981 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1982 require_complete_type (tmp);
1983 }
1984
1985 type = finish_struct_1 (type, 0);
1986 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1987
1988 repo_template_used (type);
1989 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1990 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1991 }
1992 else
1993 {
1994 TYPE_SIZE (type) = integer_zero_node;
1995 CLASSTYPE_METHOD_VEC (type)
1996 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1997 }
1998
1999 TYPE_BEING_DEFINED (type) = 0;
2000 popclass (0);
2001
2002 pop_from_top_level ();
2003 pop_tinst_level ();
2004
2005 return type;
2006 }
2007
2008 static int
2009 list_eq (t1, t2)
2010 tree t1, t2;
2011 {
2012 if (t1 == NULL_TREE)
2013 return t2 == NULL_TREE;
2014 if (t2 == NULL_TREE)
2015 return 0;
2016 /* Don't care if one declares its arg const and the other doesn't -- the
2017 main variant of the arg type is all that matters. */
2018 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
2019 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
2020 return 0;
2021 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
2022 }
2023
2024 tree
2025 lookup_nested_type_by_name (ctype, name)
2026 tree ctype, name;
2027 {
2028 tree t;
2029
2030 complete_type (ctype);
2031
2032 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
2033 {
2034 if (name == TREE_PURPOSE (t)
2035 /* this catches typedef enum { foo } bar; */
2036 || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
2037 return TREE_VALUE (t);
2038 }
2039 return NULL_TREE;
2040 }
2041
2042 /* If arg is a non-type template parameter that does not depend on template
2043 arguments, fold it like we weren't in the body of a template. */
2044
2045 static tree
2046 maybe_fold_nontype_arg (arg)
2047 tree arg;
2048 {
2049 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
2050 && !uses_template_parms (arg))
2051 {
2052 /* Sometimes, one of the args was an expression involving a
2053 template constant parameter, like N - 1. Now that we've
2054 tsubst'd, we might have something like 2 - 1. This will
2055 confuse lookup_template_class, so we do constant folding
2056 here. We have to unset processing_template_decl, to
2057 fool build_expr_from_tree() into building an actual
2058 tree. */
2059
2060 int saved_processing_template_decl = processing_template_decl;
2061 processing_template_decl = 0;
2062 arg = fold (build_expr_from_tree (arg));
2063 processing_template_decl = saved_processing_template_decl;
2064 }
2065 return arg;
2066 }
2067
2068 /* Take the tree structure T and replace template parameters used therein
2069 with the argument vector ARGS. NARGS is the number of args; should
2070 be removed. IN_DECL is an associated decl for diagnostics.
2071
2072 tsubst is used for dealing with types, decls and the like; for
2073 expressions, use tsubst_expr or tsubst_copy. */
2074
2075 tree
2076 tsubst (t, args, nargs, in_decl)
2077 tree t, args;
2078 int nargs;
2079 tree in_decl;
2080 {
2081 tree type;
2082
2083 if (t == NULL_TREE || t == error_mark_node
2084 || t == integer_type_node
2085 || t == void_type_node
2086 || t == char_type_node)
2087 return t;
2088
2089 type = TREE_TYPE (t);
2090 if (type == unknown_type_node)
2091 my_friendly_abort (42);
2092 if (type && TREE_CODE (t) != FUNCTION_DECL
2093 && TREE_CODE (t) != TYPENAME_TYPE)
2094 type = tsubst (type, args, nargs, in_decl);
2095
2096 switch (TREE_CODE (t))
2097 {
2098 case RECORD_TYPE:
2099 if (TYPE_PTRMEMFUNC_P (t))
2100 {
2101 tree r = build_ptrmemfunc_type
2102 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
2103 return cp_build_type_variant (r, TYPE_READONLY (t),
2104 TYPE_VOLATILE (t));
2105 }
2106
2107 /* else fall through */
2108 case UNION_TYPE:
2109 if (uses_template_parms (t))
2110 {
2111 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
2112 tree r = lookup_template_class (t, argvec, in_decl);
2113 return cp_build_type_variant (r, TYPE_READONLY (t),
2114 TYPE_VOLATILE (t));
2115 }
2116
2117 /* else fall through */
2118 case ERROR_MARK:
2119 case IDENTIFIER_NODE:
2120 case OP_IDENTIFIER:
2121 case VOID_TYPE:
2122 case REAL_TYPE:
2123 case COMPLEX_TYPE:
2124 case BOOLEAN_TYPE:
2125 case INTEGER_CST:
2126 case REAL_CST:
2127 case STRING_CST:
2128 return t;
2129
2130 case ENUMERAL_TYPE:
2131 {
2132 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2133 if (ctx == NULL_TREE)
2134 return t;
2135 else if (ctx == current_function_decl)
2136 return lookup_name (TYPE_IDENTIFIER (t), 1);
2137 else
2138 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
2139 }
2140
2141 case INTEGER_TYPE:
2142 if (t == integer_type_node)
2143 return t;
2144
2145 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
2146 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
2147 return t;
2148
2149 {
2150 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
2151 max = tsubst_expr (max, args, nargs, in_decl);
2152 if (processing_template_decl)
2153 {
2154 tree itype = make_node (INTEGER_TYPE);
2155 TYPE_MIN_VALUE (itype) = size_zero_node;
2156 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
2157 integer_one_node);
2158 return itype;
2159 }
2160
2161 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
2162 return build_index_2_type (size_zero_node, max);
2163 }
2164
2165 case TEMPLATE_TYPE_PARM:
2166 case TEMPLATE_CONST_PARM:
2167 {
2168 int idx;
2169 int level;
2170
2171 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2172 {
2173 idx = TEMPLATE_TYPE_IDX (t);
2174 level = TEMPLATE_TYPE_LEVEL (t);
2175 }
2176 else
2177 {
2178 idx = TEMPLATE_CONST_IDX (t);
2179 level = TEMPLATE_CONST_LEVEL (t);
2180 }
2181
2182 if (TREE_VEC_LENGTH (args) > 0)
2183 {
2184 tree arg = NULL_TREE;
2185
2186 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2187 {
2188 if (TREE_VEC_LENGTH (args) >= level - 1)
2189 arg = TREE_VEC_ELT
2190 (TREE_VEC_ELT (args, level - 1), idx);
2191 }
2192 else if (level == 1)
2193 arg = TREE_VEC_ELT (args, idx);
2194
2195 if (arg != NULL_TREE)
2196 {
2197 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2198 return cp_build_type_variant
2199 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
2200 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
2201 else
2202 return arg;
2203 }
2204 }
2205
2206 /* If we get here, we must have been looking at a parm for a
2207 more deeply nested template. */
2208 my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM
2209 && TEMPLATE_CONST_LEVEL (t) > 1)
2210 || (TREE_CODE (t) == TEMPLATE_TYPE_PARM
2211 && TEMPLATE_TYPE_LEVEL (t) > 1),
2212 0);
2213 return t;
2214 }
2215
2216 case TEMPLATE_DECL:
2217 {
2218 /* We can get here when processing a member template function
2219 of a template class. */
2220 tree tmpl;
2221 tree decl = DECL_TEMPLATE_RESULT (t);
2222 tree new_decl;
2223 tree parms;
2224 tree spec;
2225 int i;
2226
2227 /* We might already have an instance of this template. */
2228 tree instances = DECL_TEMPLATE_INSTANTIATIONS (t);
2229 tree ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, in_decl);
2230
2231 for (; instances; instances = TREE_CHAIN (instances))
2232 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances)) == ctx)
2233 return TREE_VALUE (instances);
2234
2235 /* Make a new template decl. It will be similar to the
2236 original, but will record the current template arguments.
2237 We also create a new function declaration, which is just
2238 like the old one, but points to this new template, rather
2239 than the old one. */
2240 tmpl = copy_node (t);
2241 copy_lang_decl (tmpl);
2242 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0);
2243 DECL_CHAIN (tmpl) = NULL_TREE;
2244 TREE_CHAIN (tmpl) = NULL_TREE;
2245 DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
2246 new_decl = tsubst (decl, args, nargs, in_decl);
2247 DECL_RESULT (tmpl) = new_decl;
2248 DECL_TI_TEMPLATE (new_decl) = tmpl;
2249 TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
2250 DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
2251 SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
2252
2253 /* The template parameters for this new template are all the
2254 template parameters for the old template, except the
2255 outermost level of parameters. */
2256 DECL_TEMPLATE_PARMS (tmpl)
2257 = copy_node (DECL_TEMPLATE_PARMS (tmpl));
2258 for (parms = DECL_TEMPLATE_PARMS (tmpl);
2259 TREE_CHAIN (parms) != NULL_TREE;
2260 parms = TREE_CHAIN (parms))
2261 TREE_CHAIN (parms) = copy_node (TREE_CHAIN (parms));
2262
2263 /* Record this partial instantiation. */
2264 DECL_TEMPLATE_INSTANTIATIONS (t)
2265 = perm_tree_cons (NULL_TREE, tmpl,
2266 DECL_TEMPLATE_INSTANTIATIONS (t));
2267
2268 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
2269 return tmpl;
2270 }
2271
2272 case FUNCTION_DECL:
2273 {
2274 tree r = NULL_TREE;
2275 tree ctx;
2276
2277 int member;
2278
2279 if (DECL_CONTEXT (t) != NULL_TREE
2280 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
2281 {
2282 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
2283 member = 2;
2284 else
2285 member = 1;
2286 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
2287 type = tsubst (type, args, nargs, in_decl);
2288 }
2289 else
2290 {
2291 member = 0;
2292 ctx = NULL_TREE;
2293 type = tsubst (type, args, nargs, in_decl);
2294 }
2295
2296 /* Do we already have this instantiation? */
2297 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2298 {
2299 tree tmpl = DECL_TI_TEMPLATE (t);
2300 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2301
2302 for (; decls; decls = TREE_CHAIN (decls))
2303 if (TREE_TYPE (TREE_VALUE (decls)) == type
2304 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx
2305 && comp_template_args (TREE_PURPOSE (decls), args))
2306 return TREE_VALUE (decls);
2307 }
2308
2309 /* We do NOT check for matching decls pushed separately at this
2310 point, as they may not represent instantiations of this
2311 template, and in any case are considered separate under the
2312 discrete model. Instead, see add_maybe_template. */
2313
2314 r = copy_node (t);
2315 copy_lang_decl (r);
2316 TREE_TYPE (r) = type;
2317
2318 DECL_CONTEXT (r)
2319 = tsubst (DECL_CONTEXT (t), args, nargs, t);
2320 DECL_CLASS_CONTEXT (r) = ctx;
2321
2322 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
2323 IDENTIFIER_POINTER (DECL_NAME (r)),
2324 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
2325 {
2326 /* Type-conversion operator. Reconstruct the name, in
2327 case it's the name of one of the template's parameters. */
2328 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
2329 }
2330
2331 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
2332 {
2333 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
2334 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
2335 buf = (char *) alloca (strlen (dbuf)
2336 + sizeof (DESTRUCTOR_DECL_PREFIX));
2337 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
2338 buf[len] = '\0';
2339 strcat (buf, dbuf);
2340 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
2341 }
2342 else
2343 {
2344 /* Instantiations of template functions must be mangled
2345 specially, in order to conform to 14.5.5.1
2346 [temp.over.link]. We use in_decl below rather than
2347 DECL_TI_TEMPLATE (r) because the latter is set to
2348 NULL_TREE in instantiate_decl. */
2349 tree tmpl;
2350 tree arg_types;
2351
2352 if (DECL_TEMPLATE_INFO (r))
2353 tmpl = DECL_TI_TEMPLATE (r);
2354 else
2355 tmpl = in_decl;
2356
2357 /* tmpl will be NULL if this is a specialization of a
2358 member template of a template class. */
2359 if (name_mangling_version < 1
2360 || tmpl == NULL_TREE
2361 || (member && !is_member_template (tmpl)
2362 && !DECL_TEMPLATE_INFO (tmpl)))
2363 {
2364 arg_types = TYPE_ARG_TYPES (type);
2365 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2366 arg_types = hash_tree_chain
2367 (build_pointer_type (DECL_CONTEXT (r)),
2368 arg_types);
2369
2370 DECL_ASSEMBLER_NAME (r)
2371 = build_decl_overload (DECL_NAME (r), arg_types,
2372 member);
2373 }
2374 else
2375 {
2376 /* We pass the outermost template parameters to
2377 build_template_decl_overload since the innermost
2378 template parameters are still just template
2379 parameters; there are no corresponding subsitution
2380 arguments. */
2381 /* FIXME The messed up thing here is that we get here with
2382 full args and only one level of parms. This is necessary
2383 because when we partially instantiate a member template,
2384 even though there's really only one level of parms left
2385 we re-use the parms from the original template, which
2386 have level 2. When this is fixed we can remove the
2387 add_to_template_args from instantiate_template. */
2388 tree tparms = DECL_TEMPLATE_PARMS (tmpl);
2389
2390 while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
2391 tparms = TREE_CHAIN (tparms);
2392
2393 my_friendly_assert (tparms != NULL_TREE
2394 && TREE_CODE (tparms) == TREE_LIST,
2395 0);
2396 tparms = TREE_VALUE (tparms);
2397
2398 arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
2399 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2400 arg_types = hash_tree_chain
2401 (build_pointer_type (DECL_CONTEXT (r)),
2402 arg_types);
2403
2404 DECL_ASSEMBLER_NAME (r)
2405 = build_template_decl_overload
2406 (DECL_NAME (r), arg_types,
2407 TREE_TYPE (TREE_TYPE (tmpl)),
2408 tparms,
2409 TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC
2410 ? TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1) :
2411 args,
2412 member);
2413 }
2414 }
2415 DECL_RTL (r) = 0;
2416 make_decl_rtl (r, NULL_PTR, 1);
2417
2418 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
2419 DECL_MAIN_VARIANT (r) = r;
2420 DECL_RESULT (r) = NULL_TREE;
2421 DECL_INITIAL (r) = NULL_TREE;
2422
2423 TREE_STATIC (r) = 0;
2424 TREE_PUBLIC (r) = 1;
2425 DECL_EXTERNAL (r) = 1;
2426 DECL_INTERFACE_KNOWN (r) = 0;
2427 DECL_DEFER_OUTPUT (r) = 0;
2428 TREE_CHAIN (r) = NULL_TREE;
2429 DECL_CHAIN (r) = NULL_TREE;
2430
2431 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
2432 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
2433
2434 /* Look for matching decls for the moment. */
2435 if (! member && ! flag_ansi_overloading)
2436 {
2437 tree decls = lookup_name_nonclass (DECL_NAME (t));
2438 tree d = NULL_TREE;
2439
2440 if (decls == NULL_TREE)
2441 /* no match */;
2442 else if (is_overloaded_fn (decls))
2443 for (decls = get_first_fn (decls); decls;
2444 decls = DECL_CHAIN (decls))
2445 {
2446 if (TREE_CODE (decls) == FUNCTION_DECL
2447 && TREE_TYPE (decls) == type)
2448 {
2449 d = decls;
2450 break;
2451 }
2452 }
2453
2454 if (d)
2455 {
2456 int dcl_only = ! DECL_INITIAL (d);
2457 if (dcl_only)
2458 DECL_INITIAL (r) = error_mark_node;
2459 duplicate_decls (r, d);
2460 r = d;
2461 if (dcl_only)
2462 DECL_INITIAL (r) = 0;
2463 }
2464 }
2465
2466 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2467 {
2468 tree tmpl = DECL_TI_TEMPLATE (t);
2469 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2470 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2471
2472 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
2473 argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec);
2474
2475 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2476 *declsp = perm_tree_cons (argvec, r, *declsp);
2477
2478 /* If we have a preexisting version of this function, don't expand
2479 the template version, use the other instead. */
2480 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
2481 SET_DECL_TEMPLATE_SPECIALIZATION (r);
2482 else
2483 SET_DECL_IMPLICIT_INSTANTIATION (r);
2484
2485 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
2486 = tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
2487 }
2488
2489 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2490 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2491 if (member
2492 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
2493 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
2494
2495 return r;
2496 }
2497
2498 case PARM_DECL:
2499 {
2500 tree r = copy_node (t);
2501 TREE_TYPE (r) = type;
2502 DECL_INITIAL (r) = TREE_TYPE (r);
2503 DECL_CONTEXT (r) = NULL_TREE;
2504 #ifdef PROMOTE_PROTOTYPES
2505 if ((TREE_CODE (type) == INTEGER_TYPE
2506 || TREE_CODE (type) == ENUMERAL_TYPE)
2507 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2508 DECL_ARG_TYPE (r) = integer_type_node;
2509 #endif
2510 if (TREE_CHAIN (t))
2511 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
2512 return r;
2513 }
2514
2515 case FIELD_DECL:
2516 {
2517 tree r = copy_node (t);
2518 TREE_TYPE (r) = type;
2519 copy_lang_decl (r);
2520 #if 0
2521 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
2522 #endif
2523 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
2524 TREE_CHAIN (r) = NULL_TREE;
2525 return r;
2526 }
2527
2528 case USING_DECL:
2529 {
2530 tree r = copy_node (t);
2531 DECL_INITIAL (r)
2532 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
2533 TREE_CHAIN (r) = NULL_TREE;
2534 return r;
2535 }
2536
2537 case VAR_DECL:
2538 {
2539 tree r;
2540 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
2541
2542 /* Do we already have this instantiation? */
2543 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2544 {
2545 tree tmpl = DECL_TI_TEMPLATE (t);
2546 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2547
2548 for (; decls; decls = TREE_CHAIN (decls))
2549 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
2550 return TREE_VALUE (decls);
2551 }
2552
2553 r = copy_node (t);
2554 TREE_TYPE (r) = type;
2555 DECL_CONTEXT (r) = ctx;
2556 if (TREE_STATIC (r))
2557 DECL_ASSEMBLER_NAME (r)
2558 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
2559
2560 /* Don't try to expand the initializer until someone tries to use
2561 this variable; otherwise we run into circular dependencies. */
2562 DECL_INITIAL (r) = NULL_TREE;
2563
2564 DECL_RTL (r) = 0;
2565 DECL_SIZE (r) = 0;
2566
2567 if (DECL_LANG_SPECIFIC (r))
2568 {
2569 copy_lang_decl (r);
2570 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
2571 }
2572
2573 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2574 {
2575 tree tmpl = DECL_TI_TEMPLATE (t);
2576 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2577 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2578
2579 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2580 *declsp = perm_tree_cons (argvec, r, *declsp);
2581 SET_DECL_IMPLICIT_INSTANTIATION (r);
2582 }
2583 TREE_CHAIN (r) = NULL_TREE;
2584 return r;
2585 }
2586
2587 case TYPE_DECL:
2588 if (t == TYPE_NAME (TREE_TYPE (t)))
2589 return TYPE_NAME (type);
2590
2591 {
2592 tree r = copy_node (t);
2593 TREE_TYPE (r) = type;
2594 DECL_CONTEXT (r) = current_class_type;
2595 TREE_CHAIN (r) = NULL_TREE;
2596 return r;
2597 }
2598
2599 case TREE_LIST:
2600 {
2601 tree purpose, value, chain, result;
2602 int via_public, via_virtual, via_protected;
2603
2604 if (t == void_list_node)
2605 return t;
2606
2607 via_public = TREE_VIA_PUBLIC (t);
2608 via_protected = TREE_VIA_PROTECTED (t);
2609 via_virtual = TREE_VIA_VIRTUAL (t);
2610
2611 purpose = TREE_PURPOSE (t);
2612 if (purpose)
2613 purpose = tsubst (purpose, args, nargs, in_decl);
2614 value = TREE_VALUE (t);
2615 if (value)
2616 value = tsubst (value, args, nargs, in_decl);
2617 chain = TREE_CHAIN (t);
2618 if (chain && chain != void_type_node)
2619 chain = tsubst (chain, args, nargs, in_decl);
2620 if (purpose == TREE_PURPOSE (t)
2621 && value == TREE_VALUE (t)
2622 && chain == TREE_CHAIN (t))
2623 return t;
2624 result = hash_tree_cons (via_public, via_virtual, via_protected,
2625 purpose, value, chain);
2626 TREE_PARMLIST (result) = TREE_PARMLIST (t);
2627 return result;
2628 }
2629 case TREE_VEC:
2630 if (type != NULL_TREE)
2631 {
2632 /* A binfo node. */
2633
2634 t = copy_node (t);
2635
2636 if (type == TREE_TYPE (t))
2637 return t;
2638
2639 TREE_TYPE (t) = complete_type (type);
2640 if (IS_AGGR_TYPE (type))
2641 {
2642 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
2643 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
2644 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
2645 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
2646 }
2647 return t;
2648 }
2649
2650 /* Otherwise, a vector of template arguments. */
2651 {
2652 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
2653 tree *elts = (tree *) alloca (len * sizeof (tree));
2654
2655 bzero ((char *) elts, len * sizeof (tree));
2656
2657 for (i = 0; i < len; i++)
2658 {
2659 elts[i] = maybe_fold_nontype_arg
2660 (tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl));
2661
2662 if (elts[i] != TREE_VEC_ELT (t, i))
2663 need_new = 1;
2664 }
2665
2666 if (!need_new)
2667 return t;
2668
2669 t = make_tree_vec (len);
2670 for (i = 0; i < len; i++)
2671 TREE_VEC_ELT (t, i) = elts[i];
2672
2673 return t;
2674 }
2675 case POINTER_TYPE:
2676 case REFERENCE_TYPE:
2677 {
2678 tree r;
2679 enum tree_code code;
2680 if (type == TREE_TYPE (t))
2681 return t;
2682
2683 code = TREE_CODE (t);
2684 if (code == POINTER_TYPE)
2685 r = build_pointer_type (type);
2686 else
2687 r = build_reference_type (type);
2688 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
2689 /* Will this ever be needed for TYPE_..._TO values? */
2690 layout_type (r);
2691 return r;
2692 }
2693 case OFFSET_TYPE:
2694 return build_offset_type
2695 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
2696 case FUNCTION_TYPE:
2697 case METHOD_TYPE:
2698 {
2699 tree values = TYPE_ARG_TYPES (t);
2700 tree context = TYPE_CONTEXT (t);
2701 tree raises = TYPE_RAISES_EXCEPTIONS (t);
2702 tree fntype;
2703
2704 /* Don't bother recursing if we know it won't change anything. */
2705 if (values != void_list_node)
2706 {
2707 /* This should probably be rewritten to use hash_tree_cons for
2708 the memory savings. */
2709 tree first = NULL_TREE;
2710 tree last;
2711
2712 for (; values && values != void_list_node;
2713 values = TREE_CHAIN (values))
2714 {
2715 tree value = TYPE_MAIN_VARIANT (type_decays_to
2716 (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
2717 /* Don't instantiate default args unless they are used.
2718 Handle it in build_over_call instead. */
2719 tree purpose = TREE_PURPOSE (values);
2720 tree x = build_tree_list (purpose, value);
2721
2722 if (first)
2723 TREE_CHAIN (last) = x;
2724 else
2725 first = x;
2726 last = x;
2727 }
2728
2729 if (values == void_list_node)
2730 TREE_CHAIN (last) = void_list_node;
2731
2732 values = first;
2733 }
2734 if (context)
2735 context = tsubst (context, args, nargs, in_decl);
2736 /* Could also optimize cases where return value and
2737 values have common elements (e.g., T min(const &T, const T&). */
2738
2739 /* If the above parameters haven't changed, just return the type. */
2740 if (type == TREE_TYPE (t)
2741 && values == TYPE_VALUES (t)
2742 && context == TYPE_CONTEXT (t))
2743 return t;
2744
2745 /* Construct a new type node and return it. */
2746 if (TREE_CODE (t) == FUNCTION_TYPE
2747 && context == NULL_TREE)
2748 {
2749 fntype = build_function_type (type, values);
2750 }
2751 else if (context == NULL_TREE)
2752 {
2753 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
2754 args, nargs, in_decl);
2755 fntype = build_cplus_method_type (base, type,
2756 TREE_CHAIN (values));
2757 }
2758 else
2759 {
2760 fntype = make_node (TREE_CODE (t));
2761 TREE_TYPE (fntype) = type;
2762 TYPE_CONTEXT (fntype) = context;
2763 TYPE_VALUES (fntype) = values;
2764 TYPE_SIZE (fntype) = TYPE_SIZE (t);
2765 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
2766 TYPE_MODE (fntype) = TYPE_MODE (t);
2767 if (TYPE_METHOD_BASETYPE (t))
2768 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
2769 args, nargs, in_decl);
2770 /* Need to generate hash value. */
2771 my_friendly_abort (84);
2772 }
2773 fntype = build_type_variant (fntype,
2774 TYPE_READONLY (t),
2775 TYPE_VOLATILE (t));
2776 if (raises)
2777 {
2778 raises = tsubst (raises, args, nargs, in_decl);
2779 fntype = build_exception_variant (fntype, raises);
2780 }
2781 return fntype;
2782 }
2783 case ARRAY_TYPE:
2784 {
2785 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
2786 tree r;
2787 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
2788 return t;
2789 r = build_cplus_array_type (type, domain);
2790 return r;
2791 }
2792
2793 case PLUS_EXPR:
2794 case MINUS_EXPR:
2795 return fold (build (TREE_CODE (t), TREE_TYPE (t),
2796 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2797 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
2798
2799 case NEGATE_EXPR:
2800 case NOP_EXPR:
2801 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
2802 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
2803
2804 case TYPENAME_TYPE:
2805 {
2806 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2807 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
2808 return cp_build_type_variant
2809 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
2810 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
2811 }
2812
2813 case INDIRECT_REF:
2814 return make_pointer_declarator
2815 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2816
2817 case ADDR_EXPR:
2818 return make_reference_declarator
2819 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2820
2821 case ARRAY_REF:
2822 return build_parse_node
2823 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2824 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2825
2826 case CALL_EXPR:
2827 return make_call_declarator
2828 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2829 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2830 TREE_OPERAND (t, 2),
2831 tsubst (TREE_TYPE (t), args, nargs, in_decl));
2832
2833 case SCOPE_REF:
2834 return build_parse_node
2835 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2836 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
2837
2838 default:
2839 sorry ("use of `%s' in template",
2840 tree_code_name [(int) TREE_CODE (t)]);
2841 return error_mark_node;
2842 }
2843 }
2844
2845 void
2846 do_pushlevel ()
2847 {
2848 emit_line_note (input_filename, lineno);
2849 pushlevel (0);
2850 clear_last_expr ();
2851 push_momentary ();
2852 expand_start_bindings (0);
2853 }
2854
2855 tree
2856 do_poplevel ()
2857 {
2858 tree t;
2859 int saved_warn_unused;
2860
2861 if (processing_template_decl)
2862 {
2863 saved_warn_unused = warn_unused;
2864 warn_unused = 0;
2865 }
2866 expand_end_bindings (getdecls (), kept_level_p (), 0);
2867 if (processing_template_decl)
2868 warn_unused = saved_warn_unused;
2869 t = poplevel (kept_level_p (), 1, 0);
2870 pop_momentary ();
2871 return t;
2872 }
2873
2874 /* Like tsubst, but deals with expressions. This function just replaces
2875 template parms; to finish processing the resultant expression, use
2876 tsubst_expr. */
2877
2878 tree
2879 tsubst_copy (t, args, nargs, in_decl)
2880 tree t, args;
2881 int nargs;
2882 tree in_decl;
2883 {
2884 enum tree_code code;
2885
2886 if (t == NULL_TREE || t == error_mark_node)
2887 return t;
2888
2889 code = TREE_CODE (t);
2890
2891 switch (code)
2892 {
2893 case PARM_DECL:
2894 return do_identifier (DECL_NAME (t), 0);
2895
2896 case CONST_DECL:
2897 case FIELD_DECL:
2898 if (DECL_CONTEXT (t))
2899 {
2900 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
2901 if (ctx == current_function_decl)
2902 return lookup_name (DECL_NAME (t), 0);
2903 else if (ctx != DECL_CONTEXT (t))
2904 return lookup_field (ctx, DECL_NAME (t), 0, 0);
2905 }
2906 return t;
2907
2908 case VAR_DECL:
2909 case FUNCTION_DECL:
2910 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2911 t = tsubst (t, args, nargs, in_decl);
2912 mark_used (t);
2913 return t;
2914
2915 case TEMPLATE_DECL:
2916 if (is_member_template (t))
2917 return tsubst (t, args, nargs, in_decl);
2918 else
2919 return t;
2920
2921 #if 0
2922 case IDENTIFIER_NODE:
2923 return do_identifier (t, 0);
2924 #endif
2925
2926 case CAST_EXPR:
2927 case REINTERPRET_CAST_EXPR:
2928 case CONST_CAST_EXPR:
2929 case STATIC_CAST_EXPR:
2930 case DYNAMIC_CAST_EXPR:
2931 return build1
2932 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2933 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2934
2935 case INDIRECT_REF:
2936 case PREDECREMENT_EXPR:
2937 case PREINCREMENT_EXPR:
2938 case POSTDECREMENT_EXPR:
2939 case POSTINCREMENT_EXPR:
2940 case NEGATE_EXPR:
2941 case TRUTH_NOT_EXPR:
2942 case BIT_NOT_EXPR:
2943 case ADDR_EXPR:
2944 case CONVERT_EXPR: /* Unary + */
2945 case SIZEOF_EXPR:
2946 case ARROW_EXPR:
2947 case THROW_EXPR:
2948 case TYPEID_EXPR:
2949 return build1
2950 (code, NULL_TREE,
2951 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2952
2953 case PLUS_EXPR:
2954 case MINUS_EXPR:
2955 case MULT_EXPR:
2956 case TRUNC_DIV_EXPR:
2957 case CEIL_DIV_EXPR:
2958 case FLOOR_DIV_EXPR:
2959 case ROUND_DIV_EXPR:
2960 case EXACT_DIV_EXPR:
2961 case BIT_AND_EXPR:
2962 case BIT_ANDTC_EXPR:
2963 case BIT_IOR_EXPR:
2964 case BIT_XOR_EXPR:
2965 case TRUNC_MOD_EXPR:
2966 case FLOOR_MOD_EXPR:
2967 case TRUTH_ANDIF_EXPR:
2968 case TRUTH_ORIF_EXPR:
2969 case TRUTH_AND_EXPR:
2970 case TRUTH_OR_EXPR:
2971 case RSHIFT_EXPR:
2972 case LSHIFT_EXPR:
2973 case RROTATE_EXPR:
2974 case LROTATE_EXPR:
2975 case EQ_EXPR:
2976 case NE_EXPR:
2977 case MAX_EXPR:
2978 case MIN_EXPR:
2979 case LE_EXPR:
2980 case GE_EXPR:
2981 case LT_EXPR:
2982 case GT_EXPR:
2983 case COMPONENT_REF:
2984 case ARRAY_REF:
2985 case COMPOUND_EXPR:
2986 case SCOPE_REF:
2987 case DOTSTAR_EXPR:
2988 case MEMBER_REF:
2989 return build_nt
2990 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2991 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2992
2993 case CALL_EXPR:
2994 {
2995 tree fn = TREE_OPERAND (t, 0);
2996 if (really_overloaded_fn (fn))
2997 fn = tsubst_copy (get_first_fn (fn), args, nargs, in_decl);
2998 else
2999 fn = tsubst_copy (fn, args, nargs, in_decl);
3000 return build_nt
3001 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3002 NULL_TREE);
3003 }
3004
3005 case METHOD_CALL_EXPR:
3006 {
3007 tree name = TREE_OPERAND (t, 0);
3008 if (TREE_CODE (name) == BIT_NOT_EXPR)
3009 {
3010 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3011 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
3012 }
3013 else if (TREE_CODE (name) == SCOPE_REF
3014 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
3015 {
3016 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3017 name = TREE_OPERAND (name, 1);
3018 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3019 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
3020 name = build_nt (SCOPE_REF, base, name);
3021 }
3022 else
3023 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
3024 return build_nt
3025 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3026 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
3027 NULL_TREE);
3028 }
3029
3030 case COND_EXPR:
3031 case MODOP_EXPR:
3032 return build_nt
3033 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3034 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3035 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3036
3037 case NEW_EXPR:
3038 {
3039 tree r = build_nt
3040 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3041 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3042 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3043 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
3044 return r;
3045 }
3046
3047 case DELETE_EXPR:
3048 {
3049 tree r = build_nt
3050 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3051 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
3052 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
3053 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
3054 return r;
3055 }
3056
3057 case TEMPLATE_ID_EXPR:
3058 {
3059 /* Substituted template arguments */
3060 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl);
3061 tree chain;
3062 for (chain = targs; chain; chain = TREE_CHAIN (chain))
3063 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
3064
3065 return lookup_template_function
3066 (tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), targs);
3067 }
3068
3069 case TREE_LIST:
3070 {
3071 tree purpose, value, chain;
3072
3073 if (t == void_list_node)
3074 return t;
3075
3076 purpose = TREE_PURPOSE (t);
3077 if (purpose)
3078 purpose = tsubst_copy (purpose, args, nargs, in_decl);
3079 value = TREE_VALUE (t);
3080 if (value)
3081 value = tsubst_copy (value, args, nargs, in_decl);
3082 chain = TREE_CHAIN (t);
3083 if (chain && chain != void_type_node)
3084 chain = tsubst_copy (chain, args, nargs, in_decl);
3085 if (purpose == TREE_PURPOSE (t)
3086 && value == TREE_VALUE (t)
3087 && chain == TREE_CHAIN (t))
3088 return t;
3089 return tree_cons (purpose, value, chain);
3090 }
3091
3092 case RECORD_TYPE:
3093 case UNION_TYPE:
3094 case ENUMERAL_TYPE:
3095 case INTEGER_TYPE:
3096 case TEMPLATE_TYPE_PARM:
3097 case TEMPLATE_CONST_PARM:
3098 case POINTER_TYPE:
3099 case REFERENCE_TYPE:
3100 case OFFSET_TYPE:
3101 case FUNCTION_TYPE:
3102 case METHOD_TYPE:
3103 case ARRAY_TYPE:
3104 case TYPENAME_TYPE:
3105 return tsubst (t, args, nargs, in_decl);
3106
3107 case IDENTIFIER_NODE:
3108 if (IDENTIFIER_TYPENAME_P (t))
3109 return build_typename_overload
3110 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
3111 else
3112 return t;
3113
3114 case CONSTRUCTOR:
3115 return build
3116 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
3117 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
3118
3119 default:
3120 return t;
3121 }
3122 }
3123
3124 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
3125
3126 tree
3127 tsubst_expr (t, args, nargs, in_decl)
3128 tree t, args;
3129 int nargs;
3130 tree in_decl;
3131 {
3132 if (t == NULL_TREE || t == error_mark_node)
3133 return t;
3134
3135 if (processing_template_decl)
3136 return tsubst_copy (t, args, nargs, in_decl);
3137
3138 switch (TREE_CODE (t))
3139 {
3140 case RETURN_STMT:
3141 lineno = TREE_COMPLEXITY (t);
3142 emit_line_note (input_filename, lineno);
3143 c_expand_return
3144 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3145 finish_stmt ();
3146 break;
3147
3148 case EXPR_STMT:
3149 lineno = TREE_COMPLEXITY (t);
3150 emit_line_note (input_filename, lineno);
3151 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3152 /* Do default conversion if safe and possibly important,
3153 in case within ({...}). */
3154 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
3155 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
3156 t = default_conversion (t);
3157 cplus_expand_expr_stmt (t);
3158 clear_momentary ();
3159 finish_stmt ();
3160 break;
3161
3162 case DECL_STMT:
3163 {
3164 int i = suspend_momentary ();
3165 tree dcl, init;
3166
3167 lineno = TREE_COMPLEXITY (t);
3168 emit_line_note (input_filename, lineno);
3169 dcl = start_decl
3170 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
3171 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
3172 TREE_OPERAND (t, 2) != 0);
3173 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3174 cp_finish_decl
3175 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3176 resume_momentary (i);
3177 return dcl;
3178 }
3179
3180 case FOR_STMT:
3181 {
3182 tree tmp;
3183 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
3184 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3185 int cond_scope = (TREE_OPERAND (t, 1)
3186 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
3187
3188 lineno = TREE_COMPLEXITY (t);
3189 emit_line_note (input_filename, lineno);
3190 if (init_scope)
3191 do_pushlevel ();
3192 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
3193 tsubst_expr (tmp, args, nargs, in_decl);
3194 emit_nop ();
3195 emit_line_note (input_filename, lineno);
3196 expand_start_loop_continue_elsewhere (1);
3197
3198 if (cond_scope)
3199 do_pushlevel ();
3200 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3201 emit_line_note (input_filename, lineno);
3202 if (tmp)
3203 expand_exit_loop_if_false (0, condition_conversion (tmp));
3204
3205 if (! cond_scope)
3206 do_pushlevel ();
3207 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
3208 do_poplevel ();
3209
3210 emit_line_note (input_filename, lineno);
3211 expand_loop_continue_here ();
3212 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3213 if (tmp)
3214 cplus_expand_expr_stmt (tmp);
3215
3216 expand_end_loop ();
3217 if (init_scope)
3218 do_poplevel ();
3219 finish_stmt ();
3220 }
3221 break;
3222
3223 case WHILE_STMT:
3224 {
3225 tree cond;
3226
3227 lineno = TREE_COMPLEXITY (t);
3228 emit_nop ();
3229 emit_line_note (input_filename, lineno);
3230 expand_start_loop (1);
3231
3232 cond = TREE_OPERAND (t, 0);
3233 if (TREE_CODE (cond) == DECL_STMT)
3234 do_pushlevel ();
3235 cond = tsubst_expr (cond, args, nargs, in_decl);
3236 emit_line_note (input_filename, lineno);
3237 expand_exit_loop_if_false (0, condition_conversion (cond));
3238
3239 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
3240 do_pushlevel ();
3241 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3242 do_poplevel ();
3243
3244 expand_end_loop ();
3245 finish_stmt ();
3246 }
3247 break;
3248
3249 case DO_STMT:
3250 {
3251 tree cond;
3252
3253 lineno = TREE_COMPLEXITY (t);
3254 emit_nop ();
3255 emit_line_note (input_filename, lineno);
3256 expand_start_loop_continue_elsewhere (1);
3257
3258 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3259 expand_loop_continue_here ();
3260
3261 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3262 emit_line_note (input_filename, lineno);
3263 expand_exit_loop_if_false (0, condition_conversion (cond));
3264 expand_end_loop ();
3265
3266 clear_momentary ();
3267 finish_stmt ();
3268 }
3269 break;
3270
3271 case IF_STMT:
3272 {
3273 tree tmp;
3274 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3275
3276 lineno = TREE_COMPLEXITY (t);
3277 if (cond_scope)
3278 do_pushlevel ();
3279 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3280 emit_line_note (input_filename, lineno);
3281 expand_start_cond (condition_conversion (tmp), 0);
3282
3283 if (tmp = TREE_OPERAND (t, 1), tmp)
3284 tsubst_expr (tmp, args, nargs, in_decl);
3285
3286 if (tmp = TREE_OPERAND (t, 2), tmp)
3287 {
3288 expand_start_else ();
3289 tsubst_expr (tmp, args, nargs, in_decl);
3290 }
3291
3292 expand_end_cond ();
3293
3294 if (cond_scope)
3295 do_poplevel ();
3296
3297 finish_stmt ();
3298 }
3299 break;
3300
3301 case COMPOUND_STMT:
3302 {
3303 tree substmt = TREE_OPERAND (t, 0);
3304
3305 lineno = TREE_COMPLEXITY (t);
3306
3307 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3308 do_pushlevel ();
3309
3310 for (; substmt; substmt = TREE_CHAIN (substmt))
3311 tsubst_expr (substmt, args, nargs, in_decl);
3312
3313 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3314 do_poplevel ();
3315 }
3316 break;
3317
3318 case BREAK_STMT:
3319 lineno = TREE_COMPLEXITY (t);
3320 emit_line_note (input_filename, lineno);
3321 if (! expand_exit_something ())
3322 error ("break statement not within loop or switch");
3323 break;
3324
3325 case CONTINUE_STMT:
3326 lineno = TREE_COMPLEXITY (t);
3327 emit_line_note (input_filename, lineno);
3328 if (! expand_continue_loop (0))
3329 error ("continue statement not within a loop");
3330 break;
3331
3332 case SWITCH_STMT:
3333 {
3334 tree val, tmp;
3335 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3336
3337 lineno = TREE_COMPLEXITY (t);
3338 if (cond_scope)
3339 do_pushlevel ();
3340 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3341 emit_line_note (input_filename, lineno);
3342 c_expand_start_case (val);
3343 push_switch ();
3344
3345 if (tmp = TREE_OPERAND (t, 1), tmp)
3346 tsubst_expr (tmp, args, nargs, in_decl);
3347
3348 expand_end_case (val);
3349 pop_switch ();
3350
3351 if (cond_scope)
3352 do_poplevel ();
3353
3354 finish_stmt ();
3355 }
3356 break;
3357
3358 case CASE_LABEL:
3359 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
3360 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
3361 break;
3362
3363 case LABEL_DECL:
3364 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
3365 DECL_NAME (t));
3366 if (t)
3367 expand_label (t);
3368 break;
3369
3370 case GOTO_STMT:
3371 lineno = TREE_COMPLEXITY (t);
3372 emit_line_note (input_filename, lineno);
3373 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
3374 {
3375 tree decl = lookup_label (TREE_OPERAND (t, 0));
3376 TREE_USED (decl) = 1;
3377 expand_goto (decl);
3378 }
3379 else
3380 expand_computed_goto
3381 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3382 break;
3383
3384 case TRY_BLOCK:
3385 lineno = TREE_COMPLEXITY (t);
3386 emit_line_note (input_filename, lineno);
3387 expand_start_try_stmts ();
3388 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3389 expand_start_all_catch ();
3390 {
3391 tree handler = TREE_OPERAND (t, 1);
3392 for (; handler; handler = TREE_CHAIN (handler))
3393 tsubst_expr (handler, args, nargs, in_decl);
3394 }
3395 expand_end_all_catch ();
3396 break;
3397
3398 case HANDLER:
3399 lineno = TREE_COMPLEXITY (t);
3400 do_pushlevel ();
3401 if (TREE_OPERAND (t, 0))
3402 {
3403 tree d = TREE_OPERAND (t, 0);
3404 expand_start_catch_block
3405 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
3406 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
3407 }
3408 else
3409 expand_start_catch_block (NULL_TREE, NULL_TREE);
3410 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3411 expand_end_catch_block ();
3412 do_poplevel ();
3413 break;
3414
3415 case TAG_DEFN:
3416 lineno = TREE_COMPLEXITY (t);
3417 t = TREE_TYPE (t);
3418 if (TREE_CODE (t) == ENUMERAL_TYPE)
3419 tsubst_enum (t, args, nargs, NULL);
3420 break;
3421
3422 default:
3423 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
3424 }
3425 return NULL_TREE;
3426 }
3427
3428 tree
3429 instantiate_template (tmpl, targ_ptr)
3430 tree tmpl, targ_ptr;
3431 {
3432 tree fndecl;
3433 int i, len;
3434 struct obstack *old_fmp_obstack;
3435 extern struct obstack *function_maybepermanent_obstack;
3436
3437 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
3438
3439 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3440 {
3441 tree specs;
3442
3443 /* Check to see if there is a matching specialization. */
3444 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3445 specs != NULL_TREE;
3446 specs = TREE_CHAIN (specs))
3447 if (comp_template_args (TREE_PURPOSE (specs), targ_ptr))
3448 return TREE_VALUE (specs);
3449 }
3450
3451 push_obstacks (&permanent_obstack, &permanent_obstack);
3452 old_fmp_obstack = function_maybepermanent_obstack;
3453 function_maybepermanent_obstack = &permanent_obstack;
3454
3455 len = DECL_NTPARMS (tmpl);
3456
3457 i = len;
3458 while (i--)
3459 {
3460 tree t = TREE_VEC_ELT (targ_ptr, i);
3461 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3462 {
3463 tree nt = target_type (t);
3464 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
3465 {
3466 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
3467 cp_error (" trying to instantiate `%D'", tmpl);
3468 fndecl = error_mark_node;
3469 goto out;
3470 }
3471 }
3472 TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
3473 }
3474 targ_ptr = copy_to_permanent (targ_ptr);
3475
3476 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
3477 targ_ptr = add_to_template_args (DECL_TI_ARGS (tmpl), targ_ptr);
3478
3479 /* substitute template parameters */
3480 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
3481
3482 if (flag_external_templates)
3483 add_pending_template (fndecl);
3484
3485 out:
3486 function_maybepermanent_obstack = old_fmp_obstack;
3487 pop_obstacks ();
3488
3489 return fndecl;
3490 }
3491
3492 /* Push the name of the class template into the scope of the instantiation. */
3493
3494 void
3495 overload_template_name (type)
3496 tree type;
3497 {
3498 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
3499 tree decl;
3500
3501 if (IDENTIFIER_CLASS_VALUE (id)
3502 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
3503 return;
3504
3505 decl = build_decl (TYPE_DECL, id, type);
3506 SET_DECL_ARTIFICIAL (decl);
3507 pushdecl_class_level (decl);
3508 }
3509
3510 /* Like type_unfication but designed specially to handle conversion
3511 operators. */
3512
3513 int
3514 fn_type_unification (fn, explicit_targs, targs, args, return_type, strict)
3515 tree fn, explicit_targs, targs, args, return_type;
3516 int strict;
3517 {
3518 int i, dummy = 0;
3519 tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
3520 tree decl_arg_types = args;
3521
3522 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
3523
3524 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn)))
3525 {
3526 /* This is a template conversion operator. Use the return types
3527 as well as the argument types. */
3528 fn_arg_types = scratch_tree_cons (NULL_TREE,
3529 TREE_TYPE (TREE_TYPE (fn)),
3530 fn_arg_types);
3531 decl_arg_types = scratch_tree_cons (NULL_TREE,
3532 return_type,
3533 decl_arg_types);
3534 }
3535
3536 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
3537 &TREE_VEC_ELT (targs, 0),
3538 fn_arg_types,
3539 decl_arg_types,
3540 explicit_targs,
3541 &dummy, strict, 0);
3542
3543 return i;
3544 }
3545
3546
3547 /* Type unification.
3548
3549 We have a function template signature with one or more references to
3550 template parameters, and a parameter list we wish to fit to this
3551 template. If possible, produce a list of parameters for the template
3552 which will cause it to fit the supplied parameter list.
3553
3554 Return zero for success, 2 for an incomplete match that doesn't resolve
3555 all the types, and 1 for complete failure. An error message will be
3556 printed only for an incomplete match.
3557
3558 TPARMS[NTPARMS] is an array of template parameter types;
3559 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3560 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3561 and ARGS is the argument list we're trying to match against it.
3562
3563 If SUBR is 1, we're being called recursively (to unify the arguments of
3564 a function or method parameter of a function template), so don't zero
3565 out targs and don't fail on an incomplete match.
3566
3567 If STRICT is 1, the match must be exact (for casts of overloaded
3568 addresses, explicit instantiation, and more_specialized). */
3569
3570 int
3571 type_unification (tparms, targs, parms, args, targs_in, nsubsts,
3572 strict, allow_incomplete)
3573 tree tparms, *targs, parms, args, targs_in;
3574 int *nsubsts, strict, allow_incomplete;
3575 {
3576 int ntparms = TREE_VEC_LENGTH (tparms);
3577 tree t;
3578 int i;
3579 int r;
3580
3581 bzero ((char *) targs, sizeof (tree) * ntparms);
3582
3583 /* Insert any explicit template arguments. They are encoded as the
3584 operands of NOP_EXPRs so that unify can tell that they are
3585 explicit arguments. */
3586 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3587 targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VALUE (t));
3588
3589 r = type_unification_real (tparms, targs, parms, args, nsubsts, 0,
3590 strict, allow_incomplete);
3591
3592 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3593 if (TREE_CODE (targs[i]) == NOP_EXPR)
3594 targs[i] = TREE_OPERAND (targs[i], 0);
3595
3596 return r;
3597 }
3598
3599
3600 static int
3601 type_unification_real (tparms, targs, parms, args, nsubsts, subr,
3602 strict, allow_incomplete)
3603 tree tparms, *targs, parms, args;
3604 int *nsubsts, subr, strict, allow_incomplete;
3605 {
3606 tree parm, arg;
3607 int i;
3608 int ntparms = TREE_VEC_LENGTH (tparms);
3609
3610 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
3611 my_friendly_assert (parms == NULL_TREE
3612 || TREE_CODE (parms) == TREE_LIST, 290);
3613 /* ARGS could be NULL (via a call from parse.y to
3614 build_x_function_call). */
3615 if (args)
3616 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
3617 my_friendly_assert (ntparms > 0, 292);
3618
3619 while (parms
3620 && parms != void_list_node
3621 && args
3622 && args != void_list_node)
3623 {
3624 parm = TREE_VALUE (parms);
3625 parms = TREE_CHAIN (parms);
3626 arg = TREE_VALUE (args);
3627 args = TREE_CHAIN (args);
3628
3629 if (arg == error_mark_node)
3630 return 1;
3631 if (arg == unknown_type_node)
3632 return 1;
3633
3634 /* Conversions will be performed on a function argument that
3635 corresponds with a function parameter that contains only
3636 non-deducible template parameters and explicitly specified
3637 template parameters. */
3638 if (! uses_template_parms (parm))
3639 {
3640 tree type;
3641
3642 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3643 type = TREE_TYPE (arg);
3644 else
3645 {
3646 type = arg;
3647 arg = NULL_TREE;
3648 }
3649
3650 if (strict)
3651 {
3652 if (comptypes (parm, type, 1))
3653 continue;
3654 }
3655 else if (arg)
3656 {
3657 if (can_convert_arg (parm, type, arg))
3658 continue;
3659 }
3660 else
3661 {
3662 if (can_convert (parm, type))
3663 continue;
3664 }
3665
3666 return 1;
3667 }
3668
3669 #if 0
3670 if (TREE_CODE (arg) == VAR_DECL)
3671 arg = TREE_TYPE (arg);
3672 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
3673 arg = TREE_TYPE (arg);
3674 #else
3675 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3676 {
3677 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
3678 if (TREE_CODE (arg) == TREE_LIST
3679 && TREE_TYPE (arg) == unknown_type_node
3680 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
3681 {
3682 int nsubsts, ntparms;
3683 tree *targs;
3684
3685 /* Have to back unify here */
3686 arg = TREE_VALUE (arg);
3687 nsubsts = 0;
3688 ntparms = DECL_NTPARMS (arg);
3689 targs = (tree *) alloca (sizeof (tree) * ntparms);
3690 parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
3691 return
3692 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg),
3693 targs,
3694 TYPE_ARG_TYPES (TREE_TYPE (arg)),
3695 parm, NULL_TREE, &nsubsts, strict,
3696 allow_incomplete);
3697 }
3698 arg = TREE_TYPE (arg);
3699 }
3700 #endif
3701 if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
3702 arg = TREE_TYPE (arg);
3703
3704 if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
3705 {
3706 if (TREE_CODE (arg) == FUNCTION_TYPE
3707 || TREE_CODE (arg) == METHOD_TYPE)
3708 arg = build_pointer_type (arg);
3709 else if (TREE_CODE (arg) == ARRAY_TYPE)
3710 arg = build_pointer_type (TREE_TYPE (arg));
3711 else
3712 arg = TYPE_MAIN_VARIANT (arg);
3713 }
3714
3715 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
3716 {
3717 case 0:
3718 break;
3719 case 1:
3720 return 1;
3721 }
3722 }
3723 /* Fail if we've reached the end of the parm list, and more args
3724 are present, and the parm list isn't variadic. */
3725 if (args && args != void_list_node && parms == void_list_node)
3726 return 1;
3727 /* Fail if parms are left and they don't have default values. */
3728 if (parms
3729 && parms != void_list_node
3730 && TREE_PURPOSE (parms) == NULL_TREE)
3731 return 1;
3732 if (!subr)
3733 for (i = 0; i < ntparms; i++)
3734 if (!targs[i])
3735 {
3736 if (!allow_incomplete)
3737 error ("incomplete type unification");
3738 return 2;
3739 }
3740 return 0;
3741 }
3742
3743 /* Tail recursion is your friend. */
3744
3745 static int
3746 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
3747 tree tparms, *targs, parm, arg;
3748 int *nsubsts, ntparms, strict;
3749 {
3750 int idx;
3751
3752 /* I don't think this will do the right thing with respect to types.
3753 But the only case I've seen it in so far has been array bounds, where
3754 signedness is the only information lost, and I think that will be
3755 okay. */
3756 while (TREE_CODE (parm) == NOP_EXPR)
3757 parm = TREE_OPERAND (parm, 0);
3758
3759 if (arg == error_mark_node)
3760 return 1;
3761 if (arg == unknown_type_node)
3762 return 1;
3763 if (arg == parm)
3764 return 0;
3765
3766 switch (TREE_CODE (parm))
3767 {
3768 case TYPENAME_TYPE:
3769 /* In a type which contains a nested-name-specifier, template
3770 argument values cannot be deduced for template parameters used
3771 within the nested-name-specifier. */
3772 return 0;
3773
3774 case TEMPLATE_TYPE_PARM:
3775 (*nsubsts)++;
3776 idx = TEMPLATE_TYPE_IDX (parm);
3777 /* Check for mixed types and values. */
3778 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
3779 return 1;
3780
3781 if (!strict && targs[idx] != NULL_TREE &&
3782 TREE_CODE (targs[idx]) == NOP_EXPR)
3783 /* An explicit template argument. Don't even try to match
3784 here; the overload resolution code will manage check to
3785 see whether the call is legal. */
3786 return 0;
3787
3788 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
3789 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
3790 return 1;
3791 #if 0
3792 /* Template type parameters cannot contain cv-quals; i.e.
3793 template <class T> void f (T& a, T& b) will not generate
3794 void f (const int& a, const int& b). */
3795 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
3796 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
3797 return 1;
3798 arg = TYPE_MAIN_VARIANT (arg);
3799 #else
3800 {
3801 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
3802 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
3803 arg = cp_build_type_variant (arg, constp, volatilep);
3804 }
3805 #endif
3806 /* Simple cases: Value already set, does match or doesn't. */
3807 if (targs[idx] == arg
3808 || (targs[idx]
3809 && TREE_CODE (targs[idx]) == NOP_EXPR
3810 && TREE_OPERAND (targs[idx], 0) == arg))
3811 return 0;
3812 else if (targs[idx])
3813 return 1;
3814 targs[idx] = arg;
3815 return 0;
3816 case TEMPLATE_CONST_PARM:
3817 (*nsubsts)++;
3818 idx = TEMPLATE_CONST_IDX (parm);
3819 if (targs[idx])
3820 {
3821 int i = cp_tree_equal (targs[idx], arg);
3822 if (i == 1)
3823 return 0;
3824 else if (i == 0)
3825 return 1;
3826 else
3827 my_friendly_abort (42);
3828 }
3829
3830 targs[idx] = copy_to_permanent (arg);
3831 return 0;
3832
3833 case POINTER_TYPE:
3834 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
3835 return unify (tparms, targs, ntparms, parm,
3836 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
3837
3838 if (TREE_CODE (arg) != POINTER_TYPE)
3839 return 1;
3840 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3841 nsubsts, strict);
3842
3843 case REFERENCE_TYPE:
3844 if (TREE_CODE (arg) == REFERENCE_TYPE)
3845 arg = TREE_TYPE (arg);
3846 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
3847 nsubsts, strict);
3848
3849 case ARRAY_TYPE:
3850 if (TREE_CODE (arg) != ARRAY_TYPE)
3851 return 1;
3852 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
3853 nsubsts, strict) != 0)
3854 return 1;
3855 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3856 nsubsts, strict);
3857
3858 case REAL_TYPE:
3859 case COMPLEX_TYPE:
3860 case INTEGER_TYPE:
3861 case BOOLEAN_TYPE:
3862 case VOID_TYPE:
3863 if (TREE_CODE (arg) != TREE_CODE (parm))
3864 return 1;
3865
3866 if (TREE_CODE (parm) == INTEGER_TYPE)
3867 {
3868 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
3869 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
3870 TYPE_MIN_VALUE (arg), nsubsts, strict))
3871 return 1;
3872 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
3873 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
3874 TYPE_MAX_VALUE (arg), nsubsts, strict))
3875 return 1;
3876 }
3877 else if (TREE_CODE (parm) == REAL_TYPE
3878 && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
3879 return 1;
3880
3881 /* As far as unification is concerned, this wins. Later checks
3882 will invalidate it if necessary. */
3883 return 0;
3884
3885 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3886 /* Type INTEGER_CST can come from ordinary constant template args. */
3887 case INTEGER_CST:
3888 while (TREE_CODE (arg) == NOP_EXPR)
3889 arg = TREE_OPERAND (arg, 0);
3890
3891 if (TREE_CODE (arg) != INTEGER_CST)
3892 return 1;
3893 return !tree_int_cst_equal (parm, arg);
3894
3895 case MINUS_EXPR:
3896 {
3897 tree t1, t2;
3898 t1 = TREE_OPERAND (parm, 0);
3899 t2 = TREE_OPERAND (parm, 1);
3900 return unify (tparms, targs, ntparms, t1,
3901 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
3902 nsubsts, strict);
3903 }
3904
3905 case TREE_VEC:
3906 {
3907 int i;
3908 if (TREE_CODE (arg) != TREE_VEC)
3909 return 1;
3910 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
3911 return 1;
3912 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
3913 if (unify (tparms, targs, ntparms,
3914 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
3915 nsubsts, strict))
3916 return 1;
3917 return 0;
3918 }
3919
3920 case RECORD_TYPE:
3921 if (TYPE_PTRMEMFUNC_FLAG (parm))
3922 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
3923 arg, nsubsts, strict);
3924
3925 /* Allow trivial conversions. */
3926 if (TREE_CODE (arg) != RECORD_TYPE
3927 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
3928 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
3929 return 1;
3930
3931 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
3932 {
3933 tree t = NULL_TREE;
3934 if (flag_ansi_overloading && ! strict)
3935 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
3936 else if
3937 (CLASSTYPE_TEMPLATE_INFO (arg)
3938 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
3939 t = arg;
3940 if (! t || t == error_mark_node)
3941 return 1;
3942
3943 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
3944 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
3945 }
3946 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
3947 return 1;
3948 return 0;
3949
3950 case METHOD_TYPE:
3951 if (TREE_CODE (arg) != METHOD_TYPE)
3952 return 1;
3953 goto check_args;
3954
3955 case FUNCTION_TYPE:
3956 if (TREE_CODE (arg) != FUNCTION_TYPE)
3957 return 1;
3958 check_args:
3959 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
3960 TREE_TYPE (arg), nsubsts, strict))
3961 return 1;
3962 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3963 TYPE_ARG_TYPES (arg), nsubsts, 1,
3964 strict, 0);
3965
3966 case OFFSET_TYPE:
3967 if (TREE_CODE (arg) != OFFSET_TYPE)
3968 return 1;
3969 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
3970 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
3971 return 1;
3972 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
3973 TREE_TYPE (arg), nsubsts, strict);
3974
3975 case CONST_DECL:
3976 if (arg != decl_constant_value (parm))
3977 return 1;
3978 return 0;
3979
3980 default:
3981 sorry ("use of `%s' in template type unification",
3982 tree_code_name [(int) TREE_CODE (parm)]);
3983 return 1;
3984 }
3985 }
3986 \f
3987 void
3988 mark_decl_instantiated (result, extern_p)
3989 tree result;
3990 int extern_p;
3991 {
3992 if (DECL_TEMPLATE_INSTANTIATION (result))
3993 SET_DECL_EXPLICIT_INSTANTIATION (result);
3994 TREE_PUBLIC (result) = 1;
3995
3996 if (! extern_p)
3997 {
3998 DECL_INTERFACE_KNOWN (result) = 1;
3999 DECL_NOT_REALLY_EXTERN (result) = 1;
4000
4001 /* For WIN32 we also want to put explicit instantiations in
4002 linkonce sections. */
4003 if (supports_one_only () && ! SUPPORTS_WEAK)
4004 comdat_linkage (result);
4005 }
4006 else if (TREE_CODE (result) == FUNCTION_DECL)
4007 mark_inline_for_output (result);
4008 }
4009
4010 /* Given two function templates PAT1 and PAT2, return:
4011
4012 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
4013 -1 if PAT2 is more specialized than PAT1.
4014 0 if neither is more specialized. */
4015
4016 int
4017 more_specialized (pat1, pat2)
4018 tree pat1, pat2;
4019 {
4020 tree targs;
4021 int winner = 0;
4022
4023 targs = get_bindings (pat1, pat2);
4024 if (targs)
4025 {
4026 --winner;
4027 }
4028
4029 targs = get_bindings (pat2, pat1);
4030 if (targs)
4031 {
4032 ++winner;
4033 }
4034
4035 return winner;
4036 }
4037
4038 /* Given two class template specialization list nodes PAT1 and PAT2, return:
4039
4040 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4041 -1 if PAT2 is more specialized than PAT1.
4042 0 if neither is more specialized. */
4043
4044 int
4045 more_specialized_class (pat1, pat2)
4046 tree pat1, pat2;
4047 {
4048 tree targs;
4049 int winner = 0;
4050
4051 targs = get_class_bindings
4052 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
4053 if (targs)
4054 --winner;
4055
4056 targs = get_class_bindings
4057 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
4058 if (targs)
4059 ++winner;
4060
4061 return winner;
4062 }
4063
4064 /* Return the template arguments that will produce the function signature
4065 DECL from the function template FN. */
4066
4067 tree
4068 get_bindings (fn, decl)
4069 tree fn, decl;
4070 {
4071 int ntparms = DECL_NTPARMS (fn);
4072 tree targs = make_scratch_vec (ntparms);
4073 int i;
4074
4075 i = fn_type_unification (fn, NULL_TREE, targs,
4076 TYPE_ARG_TYPES (TREE_TYPE (decl)),
4077 TREE_TYPE (TREE_TYPE (decl)),
4078 1);
4079
4080 if (i == 0)
4081 return targs;
4082 return 0;
4083 }
4084
4085 static tree
4086 get_class_bindings (tparms, parms, args)
4087 tree tparms, parms, args;
4088 {
4089 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
4090 tree vec = make_temp_vec (ntparms);
4091
4092 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
4093 {
4094 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
4095 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
4096 &dummy, 1))
4097 {
4098 case 0:
4099 break;
4100 case 1:
4101 return NULL_TREE;
4102 }
4103 }
4104
4105 for (i = 0; i < ntparms; ++i)
4106 if (! TREE_VEC_ELT (vec, i))
4107 return NULL_TREE;
4108
4109 return vec;
4110 }
4111
4112 /* Return the most specialized of the list of templates in FNS that can
4113 produce an instantiation matching DECL. */
4114
4115 tree
4116 most_specialized (fns, decl)
4117 tree fns, decl;
4118 {
4119 tree fn, champ, args, *p;
4120 int fate;
4121
4122 for (p = &fns; *p; )
4123 {
4124 args = get_bindings (TREE_VALUE (*p), decl);
4125 if (args)
4126 {
4127 p = &TREE_CHAIN (*p);
4128 }
4129 else
4130 *p = TREE_CHAIN (*p);
4131 }
4132
4133 if (! fns)
4134 return NULL_TREE;
4135
4136 fn = fns;
4137 champ = TREE_VALUE (fn);
4138 fn = TREE_CHAIN (fn);
4139 for (; fn; fn = TREE_CHAIN (fn))
4140 {
4141 fate = more_specialized (champ, TREE_VALUE (fn));
4142 if (fate == 1)
4143 ;
4144 else
4145 {
4146 if (fate == 0)
4147 {
4148 fn = TREE_CHAIN (fn);
4149 if (! fn)
4150 return error_mark_node;
4151 }
4152 champ = TREE_VALUE (fn);
4153 }
4154 }
4155
4156 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
4157 {
4158 fate = more_specialized (champ, TREE_VALUE (fn));
4159 if (fate != 1)
4160 return error_mark_node;
4161 }
4162
4163 return champ;
4164 }
4165
4166 /* Return the most specialized of the class template specializations in
4167 SPECS that can produce an instantiation matching ARGS. */
4168
4169 tree
4170 most_specialized_class (specs, mainargs)
4171 tree specs, mainargs;
4172 {
4173 tree list = NULL_TREE, t, args, champ;
4174 int fate;
4175
4176 for (t = specs; t; t = TREE_CHAIN (t))
4177 {
4178 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
4179 if (args)
4180 {
4181 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
4182 TREE_TYPE (list) = TREE_TYPE (t);
4183 }
4184 }
4185
4186 if (! list)
4187 return NULL_TREE;
4188
4189 t = list;
4190 champ = t;
4191 t = TREE_CHAIN (t);
4192 for (; t; t = TREE_CHAIN (t))
4193 {
4194 fate = more_specialized_class (champ, t);
4195 if (fate == 1)
4196 ;
4197 else
4198 {
4199 if (fate == 0)
4200 {
4201 t = TREE_CHAIN (t);
4202 if (! t)
4203 return error_mark_node;
4204 }
4205 champ = t;
4206 }
4207 }
4208
4209 for (t = list; t && t != champ; t = TREE_CHAIN (t))
4210 {
4211 fate = more_specialized_class (champ, t);
4212 if (fate != 1)
4213 return error_mark_node;
4214 }
4215
4216 return champ;
4217 }
4218
4219 /* called from the parser. */
4220
4221 void
4222 do_decl_instantiation (declspecs, declarator, storage)
4223 tree declspecs, declarator, storage;
4224 {
4225 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
4226 tree name;
4227 tree fn;
4228 tree result = NULL_TREE;
4229 int extern_p = 0;
4230 tree templates = NULL_TREE;
4231
4232 if (! DECL_LANG_SPECIFIC (decl))
4233 {
4234 cp_error ("explicit instantiation of non-template `%#D'", decl);
4235 return;
4236 }
4237
4238 /* If we've already seen this template instance, use it. */
4239 if (TREE_CODE (decl) == VAR_DECL)
4240 {
4241 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
4242 if (result && TREE_CODE (result) != VAR_DECL)
4243 result = NULL_TREE;
4244 }
4245 else if (TREE_CODE (decl) != FUNCTION_DECL)
4246 {
4247 cp_error ("explicit instantiation of `%#D'", decl);
4248 return;
4249 }
4250 else if (DECL_FUNCTION_MEMBER_P (decl))
4251 {
4252 if (DECL_TEMPLATE_INSTANTIATION (decl))
4253 result = decl;
4254 else if (name = DECL_ASSEMBLER_NAME (decl),
4255 fn = IDENTIFIER_GLOBAL_VALUE (name),
4256 fn && DECL_TEMPLATE_INSTANTIATION (fn))
4257 result = fn;
4258 else
4259 {
4260 /* Maybe this is an instantiation of a member template
4261 function. */
4262 tree ctype = DECL_CONTEXT (decl);
4263
4264 name = DECL_NAME (decl);
4265 fn = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
4266 if (fn)
4267 fn = TREE_VALUE (fn);
4268
4269 for (; fn; fn = DECL_CHAIN (fn))
4270 if (TREE_CODE (fn) == TEMPLATE_DECL)
4271 templates = decl_tree_cons (NULL_TREE, fn, templates);
4272 }
4273 }
4274 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
4275 {
4276 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
4277 if (TREE_CODE (fn) == TEMPLATE_DECL)
4278 templates = decl_tree_cons (NULL_TREE, fn, templates);
4279 }
4280
4281 if (templates && !result)
4282 {
4283 tree args;
4284 result = most_specialized (templates, decl);
4285 if (result == error_mark_node)
4286 {
4287 char *str = "candidates are:";
4288 cp_error ("ambiguous template instantiation for `%D' requested", decl);
4289 for (fn = templates; fn; fn = TREE_CHAIN (fn))
4290 {
4291 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
4292 str = " ";
4293 }
4294 return;
4295 }
4296 else if (result)
4297 {
4298 args = get_bindings (result, decl);
4299 result = instantiate_template (result, args);
4300 }
4301 }
4302
4303 if (! result)
4304 {
4305 cp_error ("no matching template for `%D' found", decl);
4306 return;
4307 }
4308
4309 if (! DECL_TEMPLATE_INFO (result))
4310 {
4311 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
4312 return;
4313 }
4314
4315 if (flag_external_templates)
4316 return;
4317
4318 if (storage == NULL_TREE)
4319 ;
4320 else if (storage == ridpointers[(int) RID_EXTERN])
4321 extern_p = 1;
4322 else
4323 cp_error ("storage class `%D' applied to template instantiation",
4324 storage);
4325
4326 mark_decl_instantiated (result, extern_p);
4327 repo_template_instantiated (result, extern_p);
4328 if (! extern_p)
4329 instantiate_decl (result);
4330 }
4331
4332 void
4333 mark_class_instantiated (t, extern_p)
4334 tree t;
4335 int extern_p;
4336 {
4337 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
4338 SET_CLASSTYPE_INTERFACE_KNOWN (t);
4339 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
4340 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
4341 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
4342 if (! extern_p)
4343 {
4344 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
4345 rest_of_type_compilation (t, 1);
4346 }
4347 }
4348
4349 void
4350 do_type_instantiation (t, storage)
4351 tree t, storage;
4352 {
4353 int extern_p = 0;
4354 int nomem_p = 0;
4355 int static_p = 0;
4356
4357 if (TREE_CODE (t) == TYPE_DECL)
4358 t = TREE_TYPE (t);
4359
4360 if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
4361 {
4362 cp_error ("explicit instantiation of non-template type `%T'", t);
4363 return;
4364 }
4365
4366 complete_type (t);
4367
4368 /* With -fexternal-templates, explicit instantiations are treated the same
4369 as implicit ones. */
4370 if (flag_external_templates)
4371 return;
4372
4373 if (TYPE_SIZE (t) == NULL_TREE)
4374 {
4375 cp_error ("explicit instantiation of `%#T' before definition of template",
4376 t);
4377 return;
4378 }
4379
4380 if (storage == NULL_TREE)
4381 /* OK */;
4382 else if (storage == ridpointers[(int) RID_INLINE])
4383 nomem_p = 1;
4384 else if (storage == ridpointers[(int) RID_EXTERN])
4385 extern_p = 1;
4386 else if (storage == ridpointers[(int) RID_STATIC])
4387 static_p = 1;
4388 else
4389 {
4390 cp_error ("storage class `%D' applied to template instantiation",
4391 storage);
4392 extern_p = 0;
4393 }
4394
4395 /* We've already instantiated this. */
4396 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
4397 && extern_p)
4398 return;
4399
4400 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4401 {
4402 mark_class_instantiated (t, extern_p);
4403 repo_template_instantiated (t, extern_p);
4404 }
4405
4406 if (nomem_p)
4407 return;
4408
4409 {
4410 tree tmp;
4411
4412 if (! static_p)
4413 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
4414 if (TREE_CODE (tmp) == FUNCTION_DECL
4415 && DECL_TEMPLATE_INSTANTIATION (tmp))
4416 {
4417 mark_decl_instantiated (tmp, extern_p);
4418 repo_template_instantiated (tmp, extern_p);
4419 if (! extern_p)
4420 instantiate_decl (tmp);
4421 }
4422
4423 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
4424 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
4425 {
4426 mark_decl_instantiated (tmp, extern_p);
4427 repo_template_instantiated (tmp, extern_p);
4428 if (! extern_p)
4429 instantiate_decl (tmp);
4430 }
4431
4432 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
4433 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
4434 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
4435 }
4436 }
4437
4438 tree
4439 instantiate_decl (d)
4440 tree d;
4441 {
4442 tree ti = DECL_TEMPLATE_INFO (d);
4443 tree tmpl = TI_TEMPLATE (ti);
4444 tree args = TI_ARGS (ti);
4445 tree td;
4446 tree decl_pattern, code_pattern;
4447 tree save_ti;
4448 int nested = in_function_p ();
4449 int d_defined;
4450 int pattern_defined;
4451 int line = lineno;
4452 char *file = input_filename;
4453
4454 for (td = tmpl; DECL_TEMPLATE_INSTANTIATION (td); )
4455 td = DECL_TI_TEMPLATE (td);
4456
4457 /* In the case of a member template, decl_pattern is the partially
4458 instantiated declaration (in the instantiated class), and code_pattern
4459 is the original template definition. */
4460 decl_pattern = DECL_TEMPLATE_RESULT (tmpl);
4461 code_pattern = DECL_TEMPLATE_RESULT (td);
4462
4463 if (TREE_CODE (d) == FUNCTION_DECL)
4464 {
4465 d_defined = (DECL_INITIAL (d) != NULL_TREE);
4466 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
4467 }
4468 else
4469 {
4470 d_defined = ! DECL_IN_AGGR_P (d);
4471 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
4472 }
4473
4474 if (d_defined)
4475 return d;
4476
4477 if (TREE_CODE (d) == FUNCTION_DECL)
4478 {
4479 tree specs;
4480
4481 /* Check to see if there is a matching specialization. */
4482 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4483 specs != NULL_TREE;
4484 specs = TREE_CHAIN (specs))
4485 if (comp_template_args (TREE_PURPOSE (specs), args))
4486 return TREE_VALUE (specs);
4487 }
4488
4489 /* This needs to happen before any tsubsting. */
4490 if (! push_tinst_level (d))
4491 return d;
4492
4493 push_to_top_level ();
4494 lineno = DECL_SOURCE_LINE (d);
4495 input_filename = DECL_SOURCE_FILE (d);
4496
4497 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
4498 variable is a static const initialized in the class body. */
4499 if (TREE_CODE (d) == VAR_DECL
4500 && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern))
4501 {
4502 pushclass (DECL_CONTEXT (d), 2);
4503 DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args,
4504 TREE_VEC_LENGTH (args), tmpl);
4505 popclass (1);
4506 }
4507
4508 if (pattern_defined)
4509 {
4510 repo_template_used (d);
4511
4512 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
4513 {
4514 if (flag_alt_external_templates)
4515 {
4516 if (interface_unknown)
4517 warn_if_unknown_interface (d);
4518 }
4519 else if (DECL_INTERFACE_KNOWN (code_pattern))
4520 {
4521 DECL_INTERFACE_KNOWN (d) = 1;
4522 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
4523 }
4524 else
4525 warn_if_unknown_interface (code_pattern);
4526 }
4527
4528 if (at_eof)
4529 import_export_decl (d);
4530 }
4531
4532 if (! pattern_defined
4533 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
4534 && (! DECL_INTERFACE_KNOWN (d)
4535 || ! DECL_NOT_REALLY_EXTERN (d)))
4536 /* Kludge: if we compile a constructor in the middle of processing a
4537 toplevel declaration, we blow away the declspecs in
4538 temp_decl_obstack when we call permanent_allocation in
4539 finish_function. So don't compile it yet. */
4540 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
4541 {
4542 add_pending_template (d);
4543 goto out;
4544 }
4545
4546 lineno = DECL_SOURCE_LINE (d);
4547 input_filename = DECL_SOURCE_FILE (d);
4548
4549 /* Trick tsubst into giving us a new decl in case the template changed. */
4550 save_ti = DECL_TEMPLATE_INFO (decl_pattern);
4551 DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE;
4552 td = tsubst (decl_pattern, args, TREE_VEC_LENGTH (args), tmpl);
4553 SET_DECL_IMPLICIT_INSTANTIATION (td);
4554 DECL_TEMPLATE_INFO (decl_pattern) = save_ti;
4555
4556 /* And set up DECL_INITIAL, since tsubst doesn't. */
4557 if (TREE_CODE (td) == VAR_DECL)
4558 {
4559 pushclass (DECL_CONTEXT (d), 2);
4560 DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args,
4561 TREE_VEC_LENGTH (args), tmpl);
4562 popclass (1);
4563 }
4564
4565 if (TREE_CODE (d) == FUNCTION_DECL)
4566 {
4567 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4568 new decl. */
4569 DECL_INITIAL (td) = error_mark_node;
4570
4571 if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td))
4572 /* Set up the information about what is being specialized. */
4573 DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d);
4574 }
4575 duplicate_decls (td, d);
4576 if (TREE_CODE (d) == FUNCTION_DECL)
4577 DECL_INITIAL (td) = 0;
4578
4579 if (TREE_CODE (d) == VAR_DECL)
4580 {
4581 DECL_IN_AGGR_P (d) = 0;
4582 if (DECL_INTERFACE_KNOWN (d))
4583 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
4584 else
4585 {
4586 DECL_EXTERNAL (d) = 1;
4587 DECL_NOT_REALLY_EXTERN (d) = 1;
4588 }
4589 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
4590 }
4591 else if (TREE_CODE (d) == FUNCTION_DECL)
4592 {
4593 tree t = DECL_SAVED_TREE (code_pattern);
4594
4595 start_function (NULL_TREE, d, NULL_TREE, 1);
4596 store_parm_decls ();
4597
4598 if (t && TREE_CODE (t) == RETURN_INIT)
4599 {
4600 store_return_init
4601 (TREE_OPERAND (t, 0),
4602 tsubst_expr (TREE_OPERAND (t, 1), args,
4603 TREE_VEC_LENGTH (args), tmpl));
4604 t = TREE_CHAIN (t);
4605 }
4606
4607 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
4608 {
4609 current_member_init_list
4610 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
4611 current_base_init_list
4612 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
4613 t = TREE_CHAIN (t);
4614 }
4615
4616 setup_vtbl_ptr ();
4617 /* Always keep the BLOCK node associated with the outermost
4618 pair of curley braces of a function. These are needed
4619 for correct operation of dwarfout.c. */
4620 keep_next_level ();
4621
4622 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
4623 tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl);
4624
4625 finish_function (lineno, 0, nested);
4626 }
4627
4628 out:
4629 lineno = line;
4630 input_filename = file;
4631
4632 pop_from_top_level ();
4633 pop_tinst_level ();
4634
4635 return d;
4636 }
4637
4638 tree
4639 tsubst_chain (t, argvec)
4640 tree t, argvec;
4641 {
4642 if (t)
4643 {
4644 tree first = tsubst (t, argvec,
4645 TREE_VEC_LENGTH (argvec), NULL_TREE);
4646 tree last = first;
4647
4648 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
4649 {
4650 tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE);
4651 TREE_CHAIN (last) = x;
4652 last = x;
4653 }
4654
4655 return first;
4656 }
4657 return NULL_TREE;
4658 }
4659
4660 static tree
4661 tsubst_expr_values (t, argvec)
4662 tree t, argvec;
4663 {
4664 tree first = NULL_TREE;
4665 tree *p = &first;
4666
4667 for (; t; t = TREE_CHAIN (t))
4668 {
4669 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
4670 TREE_VEC_LENGTH (argvec), NULL_TREE);
4671 tree val = tsubst_expr (TREE_VALUE (t), argvec,
4672 TREE_VEC_LENGTH (argvec), NULL_TREE);
4673 *p = build_tree_list (pur, val);
4674 p = &TREE_CHAIN (*p);
4675 }
4676 return first;
4677 }
4678
4679 tree last_tree;
4680
4681 void
4682 add_tree (t)
4683 tree t;
4684 {
4685 last_tree = TREE_CHAIN (last_tree) = t;
4686 }
4687
4688 /* D is an undefined function declaration in the presence of templates with
4689 the same name, listed in FNS. If one of them can produce D as an
4690 instantiation, remember this so we can instantiate it at EOF if D has
4691 not been defined by that time. */
4692
4693 void
4694 add_maybe_template (d, fns)
4695 tree d, fns;
4696 {
4697 tree t;
4698
4699 if (DECL_MAYBE_TEMPLATE (d))
4700 return;
4701
4702 t = most_specialized (fns, d);
4703 if (! t)
4704 return;
4705 if (t == error_mark_node)
4706 {
4707 cp_error ("ambiguous template instantiation for `%D'", d);
4708 return;
4709 }
4710
4711 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
4712 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
4713 DECL_MAYBE_TEMPLATE (d) = 1;
4714 }
4715
4716 /* Instantiate an enumerated type. Used by instantiate_class_template and
4717 tsubst_expr. */
4718
4719 static tree
4720 tsubst_enum (tag, args, nargs, field_chain)
4721 tree tag, args;
4722 int nargs;
4723 tree * field_chain;
4724 {
4725 extern tree current_local_enum;
4726 tree prev_local_enum = current_local_enum;
4727
4728 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
4729 tree e, values = NULL_TREE;
4730
4731 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
4732 {
4733 tree elt = build_enumerator (TREE_PURPOSE (e),
4734 tsubst_expr (TREE_VALUE (e), args,
4735 nargs, NULL_TREE));
4736 TREE_CHAIN (elt) = values;
4737 values = elt;
4738 }
4739
4740 finish_enum (newtag, values);
4741
4742 if (NULL != field_chain)
4743 *field_chain = grok_enum_decls (newtag, NULL_TREE);
4744
4745 current_local_enum = prev_local_enum;
4746
4747 return newtag;
4748 }
This page took 0.253051 seconds and 5 git commands to generate.