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