import of gcc-2.8
[official-gcc.git] / gcc / cp / pt.c
blob49339b1b5a8b2fb1eb37dfd743c7d0ffd9eb34fb
1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
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.
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.
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. */
23 /* Known bugs or deficiencies include:
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". */
28 #include "config.h"
29 #include <stdio.h>
30 #include "obstack.h"
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"
42 #ifdef HAVE_STDLIB_H
43 #include <stdlib.h>
44 #endif
46 extern struct obstack permanent_obstack;
48 extern int lineno;
49 extern char *input_filename;
50 struct pending_inline *pending_template_expansions;
52 tree current_template_parms;
53 HOST_WIDE_INT processing_template_decl;
55 tree pending_templates;
56 static tree *template_tail = &pending_templates;
58 tree maybe_templates;
59 static tree *maybe_template_tail = &maybe_templates;
61 int minimal_parse_mode;
63 int processing_specialization;
64 static int template_header_count;
66 #define obstack_chunk_alloc xmalloc
67 #define obstack_chunk_free free
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));
86 /* Restore the template parameter context. */
88 void
89 begin_member_template_processing (decl)
90 tree decl;
92 tree parms;
93 int i;
95 parms = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
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)
104 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
105 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 0);
107 switch (TREE_CODE (parm))
109 case TYPE_DECL:
110 pushdecl (parm);
111 break;
113 case PARM_DECL:
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);
121 break;
123 default:
124 my_friendly_abort (0);
129 /* Undo the effects of begin_member_template_processing. */
131 void
132 end_member_template_processing ()
134 if (! processing_template_decl)
135 return;
137 --processing_template_decl;
138 current_template_parms = TREE_CHAIN (current_template_parms);
139 poplevel (0, 0, 0);
142 /* Returns non-zero iff T is a member template function. Works if T
143 is either a FUNCTION_DECL or a TEMPLATE_DECL. */
146 is_member_template (t)
147 tree t;
149 int r = 0;
151 if (TREE_CODE (t) != FUNCTION_DECL
152 && !DECL_FUNCTION_TEMPLATE_P (t))
153 /* Anything that isn't a template or a template function is
154 certainly not a member template. */
155 return 0;
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))))
162 tree tmpl = NULL_TREE;
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);
170 if (tmpl)
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);
177 if (CLASSTYPE_TEMPLATE_INFO (ctx))
179 tree args;
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. */
186 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
187 template_class_levels = 1;
188 else
190 int i;
192 args = CLASSTYPE_TI_ARGS (ctx);
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)))
200 template_class_levels++;
201 break;
206 if (parm_levels > template_class_levels)
207 r = 1;
211 return r;
214 /* Return a new template argument vector which contains all of ARGS,
215 but has as its innermost set of arguments the EXTRA_ARGS. */
217 static tree
218 add_to_template_args (args, extra_args)
219 tree args;
220 tree extra_args;
222 tree new_args;
224 if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC)
226 new_args = make_tree_vec (2);
227 TREE_VEC_ELT (new_args, 0) = args;
229 else
231 int i;
233 new_args = make_tree_vec (TREE_VEC_LENGTH (args) - 1);
235 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
236 TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i);
239 TREE_VEC_ELT (new_args,
240 TREE_VEC_LENGTH (new_args) - 1) = extra_args;
242 return new_args;
245 /* We've got a template header coming up; push to a new level for storing
246 the parms. */
248 void
249 begin_template_parm_list ()
251 pushlevel (0);
252 declare_pseudo_global_level ();
253 ++processing_template_decl;
254 note_template_header (0);
258 /* We've just seen template <>. */
260 void
261 begin_specialization ()
263 note_template_header (1);
267 /* Called at then end of processing a declaration preceded by
268 template<>. */
270 void
271 end_specialization ()
273 reset_specialization ();
277 /* Any template <>'s that we have seen thus far are not referring to a
278 function specialization. */
280 void
281 reset_specialization ()
283 processing_specialization = 0;
284 template_header_count = 0;
288 /* We've just seen a template header. If SPECIALIZATION is non-zero,
289 it was of the form template <>. */
291 static void
292 note_template_header (specialization)
293 int specialization;
295 processing_specialization = specialization;
296 template_header_count++;
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. */
304 static int
305 processing_explicit_specialization (template_count)
306 int template_count;
308 /* A function declaration is an explicit specialization of a member
309 template if all of the following conditions hold:
311 o There was a template <...> preceding the declaration.
312 o The last template <...> was in fact template <>.
313 o The number of template <...>'s preceding the declaration, less
314 the number of template classes with arguments specified used to
315 qualify the function name, is 1.
317 For example:
319 template <> void S<int>::foo();
320 template <class T> template <> void S<T>::foo();
321 template <> struct S<int> { ... template <> void foo(); }
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. */
327 return processing_specialization
328 && template_header_count > template_count;
331 /* Returns the template function specialized by TEMPLATE_ID, or
332 NULL_TREE if there is none.
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. */
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;
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;
359 my_friendly_assert (TREE_CODE (template_id) == TEMPLATE_ID_EXPR,
360 0);
362 fns = TREE_OPERAND (template_id, 0);
364 overloaded = fns != NULL_TREE && really_overloaded_fn (fns);
366 for (fn = (fns != NULL_TREE) ? get_first_fn (fns) : NULL_TREE;
367 fn != NULL_TREE;
368 fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE)
370 int dummy = 0;
371 tree targs;
373 if (name == NULL_TREE)
374 name = DECL_NAME (fn);
376 if (TREE_CODE (fn) != TEMPLATE_DECL
377 || (need_member_template && !is_member_template (fn)))
378 continue;
380 if (list_length (TREE_OPERAND (template_id, 1)) > DECL_NTPARMS (fn))
381 continue;
383 targs = make_scratch_vec (DECL_NTPARMS (fn));
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);
395 if (i == 0)
397 /* Unification was successful. See if the return types
398 match. */
399 if (type != NULL_TREE)
401 tree tmpl_return_type = tsubst (TREE_TYPE (TREE_TYPE (fn)),
402 targs,
403 DECL_NTPARMS (fn),
404 NULL_TREE);
406 if (tmpl_return_type != TREE_TYPE (type))
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;
420 matching_fns = scratch_tree_cons (fn, targs, matching_fns);
424 if (matching_fns == NULL_TREE)
426 if (complain)
427 cp_error ("Specialization of `%s' does not match any template declaration.",
428 IDENTIFIER_POINTER (name));
429 *targs_out = NULL_TREE;
430 return NULL_TREE;
433 if (TREE_CHAIN (matching_fns) != NULL_TREE)
435 if (complain)
437 tree fn;
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));
444 *targs_out = NULL_TREE;
445 return NULL_TREE;
448 /* We have one, and exactly one, match. */
449 *targs_out = TREE_VALUE (matching_fns);
450 return TREE_PURPOSE (matching_fns);
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. */
465 check_explicit_specialization (declarator, decl, template_count, flags)
466 tree declarator;
467 tree decl;
468 int template_count;
469 int flags;
471 int finish_member = flags == 1;
472 int have_def = flags == 2;
473 int is_friend = flags == 3;
475 if (processing_explicit_specialization (template_count)
476 || finish_member
477 || TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
479 tree tmpl = NULL_TREE;
480 tree dname = DECL_NAME (decl);
481 tree ctype = DECL_CLASS_CONTEXT (decl);
482 tree targs;
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:
488 o The explicitly specified parameters together with those
489 that can be deduced by template argument deduction
490 uniquely determine a particular specialization.
492 See [temp.expl.spec]. */
494 if (!finish_member
495 && TREE_CODE (declarator) == TEMPLATE_ID_EXPR
496 && !processing_explicit_specialization (template_count)
497 && !is_friend)
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 preceded by `template <>'");
507 if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
509 tree fns;
511 my_friendly_assert (TREE_CODE (declarator) == IDENTIFIER_NODE,
513 if (!ctype)
514 fns = IDENTIFIER_GLOBAL_VALUE (dname);
515 else
516 fns = dname;
518 declarator = lookup_template_function (fns, NULL_TREE);
521 if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
523 /* A friend declaration. We can't do much, because we don't
524 know what this resolves to, yet. */
525 my_friendly_assert (is_friend != 0, 0);
526 SET_DECL_IMPLICIT_INSTANTIATION (decl);
527 return 1;
530 if (ctype
531 && TREE_CODE (TREE_OPERAND (declarator, 0)) == IDENTIFIER_NODE)
533 tree fns;
535 if (TYPE_BEING_DEFINED (ctype) && !finish_member)
537 /* Since finish_struct_1 has not been called yet, we
538 can't call lookup_fnfields. We note that this
539 template is a specialization, and proceed, letting
540 finish_struct_methods fix this up later. */
541 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
542 DECL_TEMPLATE_INFO (decl)
543 = perm_tree_cons (NULL_TREE,
544 TREE_OPERAND (declarator, 1),
545 NULL_TREE);
546 return 1;
549 fns = lookup_fnfields (TYPE_BINFO (ctype),
550 TREE_OPERAND (declarator, 0),
553 if (fns == NULL_TREE)
555 cp_error ("No member template `%s' declared in `%T'",
556 IDENTIFIER_POINTER (TREE_OPERAND (declarator,
557 0)),
558 ctype);
559 return 1;
561 else
562 TREE_OPERAND (declarator, 0) = fns;
565 tmpl =
566 determine_explicit_specialization
567 (declarator, TREE_TYPE (decl), &targs,
568 TREE_CODE (decl) == TEMPLATE_DECL, 1);
570 if (tmpl)
572 /* Mangle the function name appropriately. */
573 if (name_mangling_version >= 1)
575 tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
577 if (ctype
578 && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE)
579 arg_types =
580 hash_tree_chain (build_pointer_type (ctype),
581 arg_types);
583 DECL_ASSEMBLER_NAME (decl)
584 = build_template_decl_overload
585 (DECL_NAME (decl),
586 arg_types,
587 TREE_TYPE (TREE_TYPE (tmpl)),
588 DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
589 targs, ctype != NULL_TREE);
592 if (is_friend && !have_def)
594 /* This is not really a declaration of a specialization.
595 It's just the name of an instantiation. But, it's not
596 a request for an instantiation, either. */
597 SET_DECL_IMPLICIT_INSTANTIATION (decl);
598 DECL_TEMPLATE_INFO (decl)
599 = perm_tree_cons (tmpl, targs, NULL_TREE);
600 return 1;
603 /* This function declaration is a template specialization.
604 Record that fact. */
605 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
606 DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
607 = perm_tree_cons (targs, decl,
608 DECL_TEMPLATE_SPECIALIZATIONS
609 (tmpl));
610 /* If DECL_TI_TEMPLATE (decl), the decl is an
611 instantiation of a specialization of a member template.
612 (In other words, there was a member template, in a
613 class template. That member template was specialized.
614 We then instantiated the class, so there is now an
615 instance of that specialization.)
617 According to the CD2,
619 14.7.3.13 [tmpl.expl.spec]
621 A specialization of a member function template or
622 member class template of a non-specialized class
623 template is itself a template.
625 So, we just leave the template info alone in this case. */
626 if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl)))
627 DECL_TEMPLATE_INFO (decl)
628 = perm_tree_cons (tmpl, targs, NULL_TREE);
629 return 1;
633 return 0;
636 /* Process information from new template parameter NEXT and append it to the
637 LIST being built. */
639 tree
640 process_template_parm (list, next)
641 tree list, next;
643 tree parm;
644 tree decl = 0;
645 tree defval;
646 int is_type, idx;
647 parm = next;
648 my_friendly_assert (TREE_CODE (parm) == TREE_LIST, 259);
649 defval = TREE_PURPOSE (parm);
650 parm = TREE_VALUE (parm);
651 is_type = TREE_PURPOSE (parm) == class_type_node;
653 if (list)
655 tree p = TREE_VALUE (tree_last (list));
657 if (TREE_CODE (p) == TYPE_DECL)
658 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
659 else
660 idx = TEMPLATE_CONST_IDX (DECL_INITIAL (p));
661 ++idx;
663 else
664 idx = 0;
666 if (!is_type)
668 tree tinfo = 0;
669 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm)) == TREE_LIST, 260);
670 /* is a const-param */
671 parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
672 PARM, 0, NULL_TREE);
673 /* A template parameter is not modifiable. */
674 TREE_READONLY (parm) = 1;
675 if (IS_AGGR_TYPE (TREE_TYPE (parm))
676 && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM)
678 cp_error ("`%#T' is not a valid type for a template constant parameter",
679 TREE_TYPE (parm));
680 if (DECL_NAME (parm) == NULL_TREE)
681 error (" a template type parameter must begin with `class' or `typename'");
682 TREE_TYPE (parm) = void_type_node;
684 else if (pedantic
685 && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE
686 || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE))
687 cp_pedwarn ("`%T' is not a valid type for a template constant parameter",
688 TREE_TYPE (parm));
689 tinfo = make_node (TEMPLATE_CONST_PARM);
690 my_friendly_assert (TREE_PERMANENT (tinfo), 260.5);
691 if (TREE_PERMANENT (parm) == 0)
693 parm = copy_node (parm);
694 TREE_PERMANENT (parm) = 1;
696 TREE_TYPE (tinfo) = TREE_TYPE (parm);
697 decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
698 DECL_INITIAL (decl) = tinfo;
699 DECL_INITIAL (parm) = tinfo;
700 TEMPLATE_CONST_SET_INFO (tinfo, idx, processing_template_decl);
702 else
704 tree t = make_lang_type (TEMPLATE_TYPE_PARM);
705 CLASSTYPE_GOT_SEMICOLON (t) = 1;
706 decl = build_decl (TYPE_DECL, TREE_VALUE (parm), t);
707 TYPE_NAME (t) = decl;
708 TYPE_STUB_DECL (t) = decl;
709 parm = decl;
710 TEMPLATE_TYPE_SET_INFO (t, idx, processing_template_decl);
712 SET_DECL_ARTIFICIAL (decl);
713 pushdecl (decl);
714 parm = build_tree_list (defval, parm);
715 return chainon (list, parm);
718 /* The end of a template parameter list has been reached. Process the
719 tree list into a parameter vector, converting each parameter into a more
720 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
721 as PARM_DECLs. */
723 tree
724 end_template_parm_list (parms)
725 tree parms;
727 int nparms;
728 tree parm;
729 tree saved_parmlist = make_tree_vec (list_length (parms));
731 current_template_parms
732 = tree_cons (build_int_2 (0, processing_template_decl),
733 saved_parmlist, current_template_parms);
735 for (parm = parms, nparms = 0; parm; parm = TREE_CHAIN (parm), nparms++)
736 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
738 return saved_parmlist;
741 /* end_template_decl is called after a template declaration is seen. */
743 void
744 end_template_decl ()
746 reset_specialization ();
748 if (! processing_template_decl)
749 return;
751 /* This matches the pushlevel in begin_template_parm_list. */
752 poplevel (0, 0, 0);
754 --processing_template_decl;
755 current_template_parms = TREE_CHAIN (current_template_parms);
756 (void) get_pending_sizes (); /* Why? */
759 /* Generate a valid set of template args from current_template_parms. */
761 tree
762 current_template_args ()
764 tree header = current_template_parms;
765 int length = list_length (header);
766 tree args = make_tree_vec (length);
767 int l = length;
769 while (header)
771 tree a = copy_node (TREE_VALUE (header));
772 int i = TREE_VEC_LENGTH (a);
773 TREE_TYPE (a) = NULL_TREE;
774 while (i--)
776 tree t = TREE_VEC_ELT (a, i);
778 /* t will be a list if we are called from within a
779 begin/end_template_parm_list pair, but a vector directly
780 if within a begin/end_member_template_processing pair. */
781 if (TREE_CODE (t) == TREE_LIST)
783 t = TREE_VALUE (t);
785 if (TREE_CODE (t) == TYPE_DECL)
786 t = TREE_TYPE (t);
787 else
788 t = DECL_INITIAL (t);
791 TREE_VEC_ELT (a, i) = t;
793 TREE_VEC_ELT (args, --l) = a;
794 header = TREE_CHAIN (header);
797 return args;
800 void
801 push_template_decl (decl)
802 tree decl;
804 tree tmpl;
805 tree args = NULL_TREE;
806 tree info;
807 tree ctx = DECL_CONTEXT (decl) ? DECL_CONTEXT (decl) : current_class_type;
808 int primary = 0;
810 /* Kludge! */
811 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl)
812 && DECL_CLASS_CONTEXT (decl))
814 /* Note that this template is a "primary template" */
815 else if (! ctx || ! CLASSTYPE_TEMPLATE_INFO (ctx)
816 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
817 primary = 1;
819 /* Partial specialization. */
820 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)
821 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
823 tree type = TREE_TYPE (decl);
824 tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
825 tree mainargs = CLASSTYPE_TI_ARGS (type);
826 tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl);
828 for (; spec; spec = TREE_CHAIN (spec))
830 /* purpose: args to main template
831 value: spec template */
832 if (comp_template_args (TREE_PURPOSE (spec), mainargs))
833 return;
836 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
837 = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms),
838 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
839 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
840 return;
843 args = current_template_args ();
845 if (! ctx || TYPE_BEING_DEFINED (ctx))
847 tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
848 DECL_TEMPLATE_PARMS (tmpl) = current_template_parms;
849 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
850 if (DECL_LANG_SPECIFIC (decl))
852 DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl);
853 DECL_STATIC_FUNCTION_P (tmpl) =
854 DECL_STATIC_FUNCTION_P (decl);
856 if (DECL_TEMPLATE_SPECIALIZATION (decl))
858 /* A specialization of a member template of a template
859 class. */
860 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
861 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
862 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
866 else
868 tree t;
869 tree a;
871 if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
872 cp_error ("must specialize `%#T' before defining member `%#D'",
873 ctx, decl);
874 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
875 tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl));
876 else if (! DECL_TEMPLATE_INFO (decl))
878 cp_error ("template definition of non-template `%#D'", decl);
879 return;
881 else
882 tmpl = DECL_TI_TEMPLATE (decl);
884 if (is_member_template (tmpl))
886 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
887 t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl));
888 if (TREE_VEC_LENGTH (t)
889 != TREE_VEC_LENGTH (a))
891 cp_error ("got %d template parameters for `%#D'",
892 TREE_VEC_LENGTH (a), decl);
893 cp_error (" but %d required", TREE_VEC_LENGTH (t));
895 if (TREE_VEC_LENGTH (args) > 1)
896 /* Get the template parameters for the enclosing template
897 class. */
898 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2);
899 else
900 a = NULL_TREE;
902 else
903 a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
905 t = NULL_TREE;
907 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
909 /* When processing an inline member template of a
910 specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */
911 if (CLASSTYPE_TI_SPEC_INFO (ctx))
912 t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx));
914 else if (CLASSTYPE_TEMPLATE_INFO (ctx))
915 t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx));
917 /* There should be template arguments if and only if there is a
918 template class. */
919 my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0);
921 if (t != NULL_TREE
922 && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
924 cp_error ("got %d template parameters for `%#D'",
925 TREE_VEC_LENGTH (a), decl);
926 cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t));
929 /* Get the innermost set of template arguments. */
930 args = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1);
932 DECL_TEMPLATE_RESULT (tmpl) = decl;
933 TREE_TYPE (tmpl) = TREE_TYPE (decl);
935 if (! ctx)
936 tmpl = pushdecl_top_level (tmpl);
938 if (primary)
939 TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl;
941 info = perm_tree_cons (tmpl, args, NULL_TREE);
943 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
945 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info;
946 DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
948 else if (! DECL_LANG_SPECIFIC (decl))
949 cp_error ("template declaration of `%#D'", decl);
950 else
951 DECL_TEMPLATE_INFO (decl) = info;
954 /* Convert all template arguments to their appropriate types, and return
955 a vector containing the resulting values. If any error occurs, return
956 error_mark_node. */
958 static tree
959 coerce_template_parms (parms, arglist, in_decl)
960 tree parms, arglist;
961 tree in_decl;
963 int nparms, nargs, i, lost = 0;
964 tree vec;
966 if (arglist == NULL_TREE)
967 nargs = 0;
968 else if (TREE_CODE (arglist) == TREE_VEC)
969 nargs = TREE_VEC_LENGTH (arglist);
970 else
971 nargs = list_length (arglist);
973 nparms = TREE_VEC_LENGTH (parms);
975 if (nargs > nparms
976 || (nargs < nparms
977 && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
979 error ("incorrect number of parameters (%d, should be %d)",
980 nargs, nparms);
981 if (in_decl)
982 cp_error_at ("in template expansion for decl `%D'", in_decl);
983 return error_mark_node;
986 if (arglist && TREE_CODE (arglist) == TREE_VEC)
987 vec = copy_node (arglist);
988 else
990 vec = make_tree_vec (nparms);
991 for (i = 0; i < nparms; i++)
993 tree arg;
995 if (arglist)
997 arg = arglist;
998 arglist = TREE_CHAIN (arglist);
1000 if (arg == error_mark_node)
1001 lost++;
1002 else
1003 arg = TREE_VALUE (arg);
1005 else if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (parms, i)))
1006 == TYPE_DECL)
1007 arg = tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1008 vec, i, in_decl);
1009 else
1010 arg = tsubst_expr (TREE_PURPOSE (TREE_VEC_ELT (parms, i)),
1011 vec, i, in_decl);
1013 TREE_VEC_ELT (vec, i) = arg;
1016 for (i = 0; i < nparms; i++)
1018 tree arg = TREE_VEC_ELT (vec, i);
1019 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1020 tree val = 0;
1021 int is_type, requires_type;
1023 is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't';
1024 requires_type = TREE_CODE (parm) == TYPE_DECL;
1026 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
1027 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
1029 cp_pedwarn ("to refer to a type member of a template parameter,");
1030 cp_pedwarn (" use `typename %E'", arg);
1031 arg = make_typename_type (TREE_OPERAND (arg, 0),
1032 TREE_OPERAND (arg, 1));
1033 is_type = 1;
1035 if (is_type != requires_type)
1037 if (in_decl)
1039 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
1040 i, in_decl);
1041 if (is_type)
1042 cp_error (" expected a constant of type `%T', got `%T'",
1043 TREE_TYPE (parm), arg);
1044 else
1045 cp_error (" expected a type, got `%E'", arg);
1047 lost++;
1048 TREE_VEC_ELT (vec, i) = error_mark_node;
1049 continue;
1051 if (is_type)
1053 val = groktypename (arg);
1054 if (! processing_template_decl)
1056 tree t = target_type (val);
1057 if (TREE_CODE (t) != TYPENAME_TYPE
1058 && IS_AGGR_TYPE (t)
1059 && decl_function_context (TYPE_MAIN_DECL (t)))
1061 cp_error ("type `%T' composed from a local class is not a valid template-argument", val);
1062 return error_mark_node;
1066 else
1068 tree t = tsubst (TREE_TYPE (parm), vec,
1069 TREE_VEC_LENGTH (vec), in_decl);
1070 if (processing_template_decl)
1071 val = arg;
1072 else
1073 val = digest_init (t, arg, (tree *) 0);
1075 if (val == error_mark_node || processing_template_decl)
1078 /* 14.2: Other template-arguments must be constant-expressions,
1079 addresses of objects or functions with external linkage, or of
1080 static class members. */
1081 else if (IS_AGGR_TYPE (TREE_TYPE (val)))
1083 cp_error ("object `%E' cannot be used as template argument", arg);
1084 val = error_mark_node;
1086 else if (!TREE_CONSTANT (val))
1088 cp_error ("non-const `%E' cannot be used as template argument",
1089 arg);
1090 val = error_mark_node;
1092 else if (POINTER_TYPE_P (TREE_TYPE (val))
1093 && ! integer_zerop (val)
1094 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != OFFSET_TYPE
1095 && TREE_CODE (TREE_TYPE (TREE_TYPE (val))) != METHOD_TYPE)
1097 t = val;
1098 STRIP_NOPS (t);
1099 if (TREE_CODE (t) == ADDR_EXPR)
1101 tree a = TREE_OPERAND (t, 0);
1102 STRIP_NOPS (a);
1103 if (TREE_CODE (a) == STRING_CST)
1105 cp_error ("string literal %E is not a valid template argument", a);
1106 error ("because it is the address of an object with static linkage");
1107 val = error_mark_node;
1109 else if (TREE_CODE (a) != VAR_DECL
1110 && TREE_CODE (a) != FUNCTION_DECL)
1111 goto bad;
1112 else if (! TREE_PUBLIC (a))
1114 cp_error ("address of non-extern `%E' cannot be used as template argument", a);
1115 val = error_mark_node;
1118 else
1120 bad:
1121 cp_error ("`%E' is not a valid template argument", t);
1122 error ("it must be %s%s with external linkage",
1123 TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
1124 ? "a pointer to " : "",
1125 TREE_CODE (TREE_TYPE (TREE_TYPE (val))) == FUNCTION_TYPE
1126 ? "a function" : "an object");
1127 val = error_mark_node;
1132 if (val == error_mark_node)
1133 lost++;
1135 TREE_VEC_ELT (vec, i) = val;
1137 if (lost)
1138 return error_mark_node;
1139 return vec;
1142 static int
1143 comp_template_args (oldargs, newargs)
1144 tree oldargs, newargs;
1146 int i;
1148 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
1149 return 0;
1151 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
1153 tree nt = TREE_VEC_ELT (newargs, i);
1154 tree ot = TREE_VEC_ELT (oldargs, i);
1156 if (nt == ot)
1157 continue;
1158 if (TREE_CODE (nt) != TREE_CODE (ot))
1159 return 0;
1160 if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
1162 if (comptypes (ot, nt, 1))
1163 continue;
1165 else if (cp_tree_equal (ot, nt) > 0)
1166 continue;
1167 return 0;
1169 return 1;
1172 /* Given class template name and parameter list, produce a user-friendly name
1173 for the instantiation. */
1175 static char *
1176 mangle_class_name_for_template (name, parms, arglist)
1177 char *name;
1178 tree parms, arglist;
1180 static struct obstack scratch_obstack;
1181 static char *scratch_firstobj;
1182 int i, nparms;
1184 if (!scratch_firstobj)
1185 gcc_obstack_init (&scratch_obstack);
1186 else
1187 obstack_free (&scratch_obstack, scratch_firstobj);
1188 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
1190 #if 0
1191 #define buflen sizeof(buf)
1192 #define check if (bufp >= buf+buflen-1) goto too_long
1193 #define ccat(c) *bufp++=(c); check
1194 #define advance bufp+=strlen(bufp); check
1195 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1196 #else
1197 #define check
1198 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1199 #define advance
1200 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1201 #endif
1203 cat (name);
1204 ccat ('<');
1205 nparms = TREE_VEC_LENGTH (parms);
1206 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
1207 for (i = 0; i < nparms; i++)
1209 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1210 tree arg = TREE_VEC_ELT (arglist, i);
1212 if (i)
1213 ccat (',');
1215 if (TREE_CODE (parm) == TYPE_DECL)
1217 cat (type_as_string (arg, 0));
1218 continue;
1220 else
1221 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
1223 if (TREE_CODE (arg) == TREE_LIST)
1225 /* New list cell was built because old chain link was in
1226 use. */
1227 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
1228 arg = TREE_VALUE (arg);
1230 /* No need to check arglist against parmlist here; we did that
1231 in coerce_template_parms, called from lookup_template_class. */
1232 cat (expr_as_string (arg, 0));
1235 char *bufp = obstack_next_free (&scratch_obstack);
1236 int offset = 0;
1237 while (bufp[offset - 1] == ' ')
1238 offset--;
1239 obstack_blank_fast (&scratch_obstack, offset);
1241 /* B<C<char> >, not B<C<char>> */
1242 if (bufp[offset - 1] == '>')
1243 ccat (' ');
1245 ccat ('>');
1246 ccat ('\0');
1247 return (char *) obstack_base (&scratch_obstack);
1249 #if 0
1250 too_long:
1251 #endif
1252 fatal ("out of (preallocated) string space creating template instantiation name");
1253 /* NOTREACHED */
1254 return NULL;
1257 static tree
1258 classtype_mangled_name (t)
1259 tree t;
1261 if (CLASSTYPE_TEMPLATE_INFO (t)
1262 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
1264 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
1265 char *mangled_name = mangle_class_name_for_template
1266 (IDENTIFIER_POINTER (name),
1267 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
1268 CLASSTYPE_TI_ARGS (t));
1269 tree id = get_identifier (mangled_name);
1270 IDENTIFIER_TEMPLATE (id) = name;
1271 return id;
1273 else
1274 return TYPE_IDENTIFIER (t);
1277 static void
1278 add_pending_template (d)
1279 tree d;
1281 tree ti;
1283 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
1284 ti = CLASSTYPE_TEMPLATE_INFO (d);
1285 else
1286 ti = DECL_TEMPLATE_INFO (d);
1288 if (TI_PENDING_TEMPLATE_FLAG (ti))
1289 return;
1291 *template_tail = perm_tree_cons
1292 (current_function_decl, d, NULL_TREE);
1293 template_tail = &TREE_CHAIN (*template_tail);
1294 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
1298 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1299 may be either a _DECL or an overloaded function or an
1300 IDENTIFIER_NODE), and ARGLIST. */
1302 tree
1303 lookup_template_function (fns, arglist)
1304 tree fns, arglist;
1306 if (fns == NULL_TREE)
1308 cp_error ("non-template used as template");
1309 return error_mark_node;
1312 if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
1314 push_obstacks (&permanent_obstack, &permanent_obstack);
1315 arglist = copy_list (arglist);
1316 pop_obstacks ();
1319 return build_min (TEMPLATE_ID_EXPR,
1320 TREE_TYPE (fns)
1321 ? TREE_TYPE (fns) : unknown_type_node,
1322 fns, arglist);
1326 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1327 parameters, find the desired type.
1329 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1330 Since ARGLIST is build on the decl_obstack, we must copy it here
1331 to keep it from being reclaimed when the decl storage is reclaimed.
1333 IN_DECL, if non-NULL, is the template declaration we are trying to
1334 instantiate. */
1336 tree
1337 lookup_template_class (d1, arglist, in_decl)
1338 tree d1, arglist;
1339 tree in_decl;
1341 tree template, parmlist;
1342 char *mangled_name;
1343 tree id, t;
1345 if (TREE_CODE (d1) == IDENTIFIER_NODE)
1347 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
1348 if (! template)
1349 template = IDENTIFIER_CLASS_VALUE (d1);
1351 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
1353 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
1354 d1 = DECL_NAME (template);
1356 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
1358 template = CLASSTYPE_TI_TEMPLATE (d1);
1359 d1 = DECL_NAME (template);
1361 else
1362 my_friendly_abort (272);
1364 /* With something like `template <class T> class X class X { ... };'
1365 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1366 We don't want to do that, but we have to deal with the situation, so
1367 let's give them some syntax errors to chew on instead of a crash. */
1368 if (! template)
1369 return error_mark_node;
1370 if (TREE_CODE (template) != TEMPLATE_DECL)
1372 cp_error ("non-template type `%T' used as a template", d1);
1373 if (in_decl)
1374 cp_error_at ("for template declaration `%D'", in_decl);
1375 return error_mark_node;
1378 if (PRIMARY_TEMPLATE_P (template))
1380 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
1382 arglist = coerce_template_parms (parmlist, arglist, template);
1383 if (arglist == error_mark_node)
1384 return error_mark_node;
1385 if (uses_template_parms (arglist))
1387 tree found;
1388 if (comp_template_args
1389 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
1390 found = TREE_TYPE (template);
1391 else
1393 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
1394 found; found = TREE_CHAIN (found))
1396 if (TI_USES_TEMPLATE_PARMS (found)
1397 && comp_template_args (TREE_PURPOSE (found), arglist))
1398 break;
1400 if (found)
1401 found = TREE_VALUE (found);
1404 if (found)
1406 if (can_free (&permanent_obstack, arglist))
1407 obstack_free (&permanent_obstack, arglist);
1408 return found;
1412 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
1413 parmlist, arglist);
1414 id = get_identifier (mangled_name);
1415 IDENTIFIER_TEMPLATE (id) = d1;
1417 maybe_push_to_top_level (uses_template_parms (arglist));
1418 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
1419 pop_from_top_level ();
1421 else
1423 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1424 arglist, in_decl);
1425 id = d1;
1426 arglist = CLASSTYPE_TI_ARGS (ctx);
1428 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
1430 int save_temp = processing_template_decl;
1431 processing_template_decl = 0;
1432 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
1433 processing_template_decl = save_temp;
1435 else
1437 t = lookup_nested_type_by_name (ctx, id);
1438 my_friendly_assert (t != NULL_TREE, 42);
1442 /* Seems to be wanted. */
1443 CLASSTYPE_GOT_SEMICOLON (t) = 1;
1445 if (! CLASSTYPE_TEMPLATE_INFO (t))
1447 arglist = copy_to_permanent (arglist);
1448 CLASSTYPE_TEMPLATE_INFO (t)
1449 = perm_tree_cons (template, arglist, NULL_TREE);
1450 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1451 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
1452 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1453 = uses_template_parms (arglist);
1455 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
1457 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1458 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
1459 /* if (! uses_template_parms (arglist)) */
1460 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
1461 = get_identifier (build_overload_name (t, 1, 1));
1463 if (flag_external_templates && ! uses_template_parms (arglist)
1464 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1465 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1466 add_pending_template (t);
1469 return t;
1472 /* Should be defined in parse.h. */
1473 extern int yychar;
1476 uses_template_parms (t)
1477 tree t;
1479 if (!t)
1480 return 0;
1481 switch (TREE_CODE (t))
1483 case INDIRECT_REF:
1484 case COMPONENT_REF:
1485 /* We assume that the object must be instantiated in order to build
1486 the COMPONENT_REF, so we test only whether the type of the
1487 COMPONENT_REF uses template parms. */
1488 return uses_template_parms (TREE_TYPE (t));
1490 case IDENTIFIER_NODE:
1491 if (!IDENTIFIER_TEMPLATE (t))
1492 return 0;
1493 my_friendly_abort (42);
1495 /* aggregates of tree nodes */
1496 case TREE_VEC:
1498 int i = TREE_VEC_LENGTH (t);
1499 while (i--)
1500 if (uses_template_parms (TREE_VEC_ELT (t, i)))
1501 return 1;
1502 return 0;
1504 case TREE_LIST:
1505 if (uses_template_parms (TREE_PURPOSE (t))
1506 || uses_template_parms (TREE_VALUE (t)))
1507 return 1;
1508 return uses_template_parms (TREE_CHAIN (t));
1510 /* constructed type nodes */
1511 case POINTER_TYPE:
1512 case REFERENCE_TYPE:
1513 return uses_template_parms (TREE_TYPE (t));
1514 case RECORD_TYPE:
1515 if (TYPE_PTRMEMFUNC_FLAG (t))
1516 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
1517 case UNION_TYPE:
1518 if (! CLASSTYPE_TEMPLATE_INFO (t))
1519 return 0;
1520 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
1521 case FUNCTION_TYPE:
1522 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1523 return 1;
1524 return uses_template_parms (TREE_TYPE (t));
1525 case ARRAY_TYPE:
1526 if (uses_template_parms (TYPE_DOMAIN (t)))
1527 return 1;
1528 return uses_template_parms (TREE_TYPE (t));
1529 case OFFSET_TYPE:
1530 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
1531 return 1;
1532 return uses_template_parms (TREE_TYPE (t));
1533 case METHOD_TYPE:
1534 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
1535 return 1;
1536 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1537 return 1;
1538 return uses_template_parms (TREE_TYPE (t));
1540 /* decl nodes */
1541 case TYPE_DECL:
1542 return uses_template_parms (TREE_TYPE (t));
1544 case FUNCTION_DECL:
1545 case VAR_DECL:
1546 /* ??? What about FIELD_DECLs? */
1547 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
1548 && uses_template_parms (DECL_TI_ARGS (t)))
1549 return 1;
1550 /* fall through */
1551 case CONST_DECL:
1552 case PARM_DECL:
1553 if (uses_template_parms (TREE_TYPE (t)))
1554 return 1;
1555 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
1556 return 1;
1557 return 0;
1559 case CALL_EXPR:
1560 return uses_template_parms (TREE_TYPE (t));
1561 case ADDR_EXPR:
1562 return uses_template_parms (TREE_OPERAND (t, 0));
1564 /* template parm nodes */
1565 case TEMPLATE_TYPE_PARM:
1566 case TEMPLATE_CONST_PARM:
1567 return 1;
1569 /* simple type nodes */
1570 case INTEGER_TYPE:
1571 if (uses_template_parms (TYPE_MIN_VALUE (t)))
1572 return 1;
1573 return uses_template_parms (TYPE_MAX_VALUE (t));
1575 case REAL_TYPE:
1576 case COMPLEX_TYPE:
1577 case VOID_TYPE:
1578 case BOOLEAN_TYPE:
1579 return 0;
1581 case ENUMERAL_TYPE:
1583 tree v;
1585 for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
1586 if (uses_template_parms (TREE_VALUE (v)))
1587 return 1;
1589 return 0;
1591 /* constants */
1592 case INTEGER_CST:
1593 case REAL_CST:
1594 case STRING_CST:
1595 return 0;
1597 case ERROR_MARK:
1598 /* Non-error_mark_node ERROR_MARKs are bad things. */
1599 my_friendly_assert (t == error_mark_node, 274);
1600 /* NOTREACHED */
1601 return 0;
1603 case LOOKUP_EXPR:
1604 case TYPENAME_TYPE:
1605 return 1;
1607 case SCOPE_REF:
1608 return uses_template_parms (TREE_OPERAND (t, 0));
1610 case CONSTRUCTOR:
1611 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1612 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1613 return uses_template_parms (TREE_OPERAND (t, 1));
1615 case MODOP_EXPR:
1616 case CAST_EXPR:
1617 case REINTERPRET_CAST_EXPR:
1618 case CONST_CAST_EXPR:
1619 case STATIC_CAST_EXPR:
1620 case DYNAMIC_CAST_EXPR:
1621 case SIZEOF_EXPR:
1622 case ARROW_EXPR:
1623 case DOTSTAR_EXPR:
1624 case TYPEID_EXPR:
1625 return 1;
1627 default:
1628 switch (TREE_CODE_CLASS (TREE_CODE (t)))
1630 case '1':
1631 case '2':
1632 case 'e':
1633 case '<':
1635 int i;
1636 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
1637 if (uses_template_parms (TREE_OPERAND (t, i)))
1638 return 1;
1639 return 0;
1641 default:
1642 break;
1644 sorry ("testing %s for template parms",
1645 tree_code_name [(int) TREE_CODE (t)]);
1646 my_friendly_abort (82);
1647 /* NOTREACHED */
1648 return 0;
1652 static struct tinst_level *current_tinst_level = 0;
1653 static struct tinst_level *free_tinst_level = 0;
1654 static int tinst_depth = 0;
1655 extern int max_tinst_depth;
1656 #ifdef GATHER_STATISTICS
1657 int depth_reached = 0;
1658 #endif
1660 static int
1661 push_tinst_level (d)
1662 tree d;
1664 struct tinst_level *new;
1666 if (tinst_depth >= max_tinst_depth)
1668 struct tinst_level *p = current_tinst_level;
1669 int line = lineno;
1670 char *file = input_filename;
1672 error ("template instantiation depth exceeds maximum of %d",
1673 max_tinst_depth);
1674 error (" (use -ftemplate-depth-NN to increase the maximum)");
1675 cp_error (" instantiating `%D'", d);
1677 for (; p; p = p->next)
1679 cp_error (" instantiated from `%D'", p->decl);
1680 lineno = p->line;
1681 input_filename = p->file;
1683 error (" instantiated from here");
1685 lineno = line;
1686 input_filename = file;
1688 return 0;
1691 if (free_tinst_level)
1693 new = free_tinst_level;
1694 free_tinst_level = new->next;
1696 else
1697 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1699 new->decl = d;
1700 new->line = lineno;
1701 new->file = input_filename;
1702 new->next = current_tinst_level;
1703 current_tinst_level = new;
1705 ++tinst_depth;
1706 #ifdef GATHER_STATISTICS
1707 if (tinst_depth > depth_reached)
1708 depth_reached = tinst_depth;
1709 #endif
1711 return 1;
1714 void
1715 pop_tinst_level ()
1717 struct tinst_level *old = current_tinst_level;
1719 current_tinst_level = old->next;
1720 old->next = free_tinst_level;
1721 free_tinst_level = old;
1722 --tinst_depth;
1725 struct tinst_level *
1726 tinst_for_decl ()
1728 struct tinst_level *p = current_tinst_level;
1730 if (p)
1731 for (; p->next ; p = p->next )
1733 return p;
1736 tree
1737 instantiate_class_template (type)
1738 tree type;
1740 tree template, template_info, args, pattern, t, *field_chain;
1742 if (type == error_mark_node)
1743 return error_mark_node;
1745 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1747 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1748 return type;
1750 template = TI_TEMPLATE (template_info);
1751 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1752 args = TI_ARGS (template_info);
1754 t = most_specialized_class
1755 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1757 if (t == error_mark_node)
1759 char *str = "candidates are:";
1760 cp_error ("ambiguous class template instantiation for `%#T'", type);
1761 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1763 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1765 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1766 str = " ";
1769 TYPE_BEING_DEFINED (type) = 1;
1770 return error_mark_node;
1772 else if (t)
1773 pattern = TREE_TYPE (t);
1774 else
1775 pattern = TREE_TYPE (template);
1777 if (TYPE_SIZE (pattern) == NULL_TREE)
1778 return type;
1780 if (t)
1781 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1783 TYPE_BEING_DEFINED (type) = 1;
1785 if (! push_tinst_level (type))
1786 return type;
1788 maybe_push_to_top_level (uses_template_parms (type));
1789 pushclass (type, 0);
1791 if (flag_external_templates)
1793 if (flag_alt_external_templates)
1795 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1796 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1797 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1798 = ! CLASSTYPE_INTERFACE_ONLY (type)
1799 && CLASSTYPE_INTERFACE_KNOWN (type);
1801 else
1803 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1804 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1805 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1806 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1807 = ! CLASSTYPE_INTERFACE_ONLY (type)
1808 && CLASSTYPE_INTERFACE_KNOWN (type);
1811 else
1813 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1814 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
1817 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1818 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1819 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1820 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1821 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1822 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1823 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1824 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1825 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1826 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1827 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1828 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1829 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1830 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1831 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1832 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1833 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1834 TYPE_USES_COMPLEX_INHERITANCE (type)
1835 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1836 TYPE_USES_MULTIPLE_INHERITANCE (type)
1837 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1838 TYPE_USES_VIRTUAL_BASECLASSES (type)
1839 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1840 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1841 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1844 tree binfo = TYPE_BINFO (type);
1845 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1847 if (pbases)
1849 tree bases;
1850 int i;
1851 int len = TREE_VEC_LENGTH (pbases);
1852 bases = make_tree_vec (len);
1853 for (i = 0; i < len; ++i)
1855 tree elt;
1857 TREE_VEC_ELT (bases, i) = elt
1858 = tsubst (TREE_VEC_ELT (pbases, i), args,
1859 TREE_VEC_LENGTH (args), NULL_TREE);
1860 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1862 if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
1863 cp_error
1864 ("base type `%T' of `%T' fails to be a struct or class type",
1865 TREE_TYPE (elt), type);
1866 else if (! uses_template_parms (type)
1867 && (TYPE_SIZE (complete_type (TREE_TYPE (elt)))
1868 == NULL_TREE))
1869 cp_error ("base class `%T' of `%T' has incomplete type",
1870 TREE_TYPE (elt), type);
1872 /* Don't initialize this until the vector is filled out, or
1873 lookups will crash. */
1874 BINFO_BASETYPES (binfo) = bases;
1878 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1880 field_chain = &TYPE_FIELDS (type);
1882 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1884 tree name = TREE_PURPOSE (t);
1885 tree tag = TREE_VALUE (t);
1887 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1888 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1890 tree e, newtag = tsubst_enum (tag, args,
1891 TREE_VEC_LENGTH (args), field_chain);
1893 while (*field_chain)
1895 DECL_FIELD_CONTEXT (*field_chain) = type;
1896 field_chain = &TREE_CHAIN (*field_chain);
1899 else
1900 tsubst (tag, args,
1901 TREE_VEC_LENGTH (args), NULL_TREE);
1904 /* Don't replace enum constants here. */
1905 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1906 if (TREE_CODE (t) != CONST_DECL)
1908 tree r = tsubst (t, args,
1909 TREE_VEC_LENGTH (args), NULL_TREE);
1910 if (TREE_CODE (r) == VAR_DECL)
1912 if (! uses_template_parms (r))
1913 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1914 /* Perhaps I should do more of grokfield here. */
1915 start_decl_1 (r);
1916 DECL_IN_AGGR_P (r) = 1;
1917 DECL_EXTERNAL (r) = 1;
1918 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1921 *field_chain = r;
1922 field_chain = &TREE_CHAIN (r);
1925 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1926 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
1928 if (DECL_CONSTRUCTOR_P (t))
1929 grok_ctor_properties (type, t);
1930 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
1931 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
1934 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1935 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1936 args, TREE_VEC_LENGTH (args), NULL_TREE);
1939 tree d = CLASSTYPE_FRIEND_CLASSES (type)
1940 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args,
1941 TREE_VEC_LENGTH (args), NULL_TREE);
1943 /* This does injection for friend classes. */
1944 for (; d; d = TREE_CHAIN (d))
1945 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1947 d = tsubst (DECL_TEMPLATE_INJECT (template), args,
1948 TREE_VEC_LENGTH (args), NULL_TREE);
1950 for (; d; d = TREE_CHAIN (d))
1952 tree t = TREE_VALUE (d);
1954 if (TREE_CODE (t) == TYPE_DECL)
1955 /* Already injected. */;
1956 else
1957 pushdecl (t);
1961 if (! uses_template_parms (type))
1963 tree tmp;
1964 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1965 if (TREE_CODE (tmp) == FIELD_DECL)
1967 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1968 require_complete_type (tmp);
1971 type = finish_struct_1 (type, 0);
1972 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1974 repo_template_used (type);
1975 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1976 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1978 else
1980 TYPE_SIZE (type) = integer_zero_node;
1981 CLASSTYPE_METHOD_VEC (type)
1982 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1985 TYPE_BEING_DEFINED (type) = 0;
1986 popclass (0);
1988 pop_from_top_level ();
1989 pop_tinst_level ();
1991 return type;
1994 static int
1995 list_eq (t1, t2)
1996 tree t1, t2;
1998 if (t1 == NULL_TREE)
1999 return t2 == NULL_TREE;
2000 if (t2 == NULL_TREE)
2001 return 0;
2002 /* Don't care if one declares its arg const and the other doesn't -- the
2003 main variant of the arg type is all that matters. */
2004 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
2005 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
2006 return 0;
2007 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
2010 tree
2011 lookup_nested_type_by_name (ctype, name)
2012 tree ctype, name;
2014 tree t;
2016 complete_type (ctype);
2018 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
2020 if (name == TREE_PURPOSE (t)
2021 /* this catches typedef enum { foo } bar; */
2022 || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
2023 return TREE_VALUE (t);
2025 return NULL_TREE;
2028 tree
2029 tsubst (t, args, nargs, in_decl)
2030 tree t, args;
2031 int nargs;
2032 tree in_decl;
2034 tree type;
2036 if (t == NULL_TREE || t == error_mark_node
2037 || t == integer_type_node
2038 || t == void_type_node
2039 || t == char_type_node)
2040 return t;
2042 type = TREE_TYPE (t);
2043 if (type == unknown_type_node)
2044 my_friendly_abort (42);
2045 if (type && TREE_CODE (t) != FUNCTION_DECL
2046 && TREE_CODE (t) != TYPENAME_TYPE)
2047 type = tsubst (type, args, nargs, in_decl);
2049 switch (TREE_CODE (t))
2051 case RECORD_TYPE:
2052 if (TYPE_PTRMEMFUNC_P (t))
2054 tree r = build_ptrmemfunc_type
2055 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
2056 return cp_build_type_variant (r, TYPE_READONLY (t),
2057 TYPE_VOLATILE (t));
2060 /* else fall through */
2061 case UNION_TYPE:
2062 if (uses_template_parms (t))
2064 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
2065 tree r = lookup_template_class (t, argvec, in_decl);
2066 return cp_build_type_variant (r, TYPE_READONLY (t),
2067 TYPE_VOLATILE (t));
2070 /* else fall through */
2071 case ERROR_MARK:
2072 case IDENTIFIER_NODE:
2073 case OP_IDENTIFIER:
2074 case VOID_TYPE:
2075 case REAL_TYPE:
2076 case COMPLEX_TYPE:
2077 case BOOLEAN_TYPE:
2078 case INTEGER_CST:
2079 case REAL_CST:
2080 case STRING_CST:
2081 return t;
2083 case ENUMERAL_TYPE:
2085 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2086 if (ctx == NULL_TREE)
2087 return t;
2088 else if (ctx == current_function_decl)
2089 return lookup_name (TYPE_IDENTIFIER (t), 1);
2090 else
2091 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
2094 case INTEGER_TYPE:
2095 if (t == integer_type_node)
2096 return t;
2098 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
2099 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
2100 return t;
2103 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
2104 max = tsubst_expr (max, args, nargs, in_decl);
2105 if (processing_template_decl)
2107 tree itype = make_node (INTEGER_TYPE);
2108 TYPE_MIN_VALUE (itype) = size_zero_node;
2109 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
2110 integer_one_node);
2111 return itype;
2114 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
2115 return build_index_2_type (size_zero_node, max);
2118 case TEMPLATE_TYPE_PARM:
2119 case TEMPLATE_CONST_PARM:
2121 int idx;
2122 int level;
2124 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2126 idx = TEMPLATE_TYPE_IDX (t);
2127 level = TEMPLATE_TYPE_LEVEL (t);
2129 else
2131 idx = TEMPLATE_CONST_IDX (t);
2132 level = TEMPLATE_CONST_LEVEL (t);
2135 if (TREE_VEC_LENGTH (args) > 0)
2137 tree arg = NULL_TREE;
2139 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2141 if (TREE_VEC_LENGTH (args) >= level - 1)
2142 arg = TREE_VEC_ELT
2143 (TREE_VEC_ELT (args, level - 1), idx);
2145 else if (level == 1)
2146 arg = TREE_VEC_ELT (args, idx);
2148 if (arg != NULL_TREE)
2150 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2151 return cp_build_type_variant
2152 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
2153 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
2154 else
2155 return arg;
2159 /* If we get here, we must have been looking at a parm for a
2160 more deeply nested template. */
2161 my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM
2162 && TEMPLATE_CONST_LEVEL (t) > 1)
2163 || (TREE_CODE (t) == TEMPLATE_TYPE_PARM
2164 && TEMPLATE_TYPE_LEVEL (t) > 1),
2166 return t;
2169 case TEMPLATE_DECL:
2171 /* We can get here when processing a member template function
2172 of a template class. */
2173 tree tmpl;
2174 tree decl = DECL_TEMPLATE_RESULT (t);
2175 tree new_decl;
2176 tree parms;
2177 tree spec;
2178 int i;
2180 /* We might already have an instance of this template. */
2181 tree instances = DECL_TEMPLATE_INSTANTIATIONS (t);
2182 tree ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, in_decl);
2184 for (; instances; instances = TREE_CHAIN (instances))
2185 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances)) == ctx)
2186 return TREE_VALUE (instances);
2188 /* Make a new template decl. It will be similar to the
2189 original, but will record the current template arguments.
2190 We also create a new function declaration, which is just
2191 like the old one, but points to this new template, rather
2192 than the old one. */
2193 tmpl = copy_node (t);
2194 copy_lang_decl (tmpl);
2195 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0);
2196 DECL_CHAIN (tmpl) = NULL_TREE;
2197 TREE_CHAIN (tmpl) = NULL_TREE;
2198 DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
2199 new_decl = tsubst (decl, args, nargs, in_decl);
2200 DECL_RESULT (tmpl) = new_decl;
2201 DECL_TI_TEMPLATE (new_decl) = tmpl;
2202 TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
2203 DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
2204 SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
2206 /* The template parameters for this new template are all the
2207 template parameters for the old template, except the
2208 outermost level of parameters. */
2209 DECL_TEMPLATE_PARMS (tmpl)
2210 = copy_node (DECL_TEMPLATE_PARMS (tmpl));
2211 for (parms = DECL_TEMPLATE_PARMS (tmpl);
2212 TREE_CHAIN (parms) != NULL_TREE;
2213 parms = TREE_CHAIN (parms))
2214 TREE_CHAIN (parms) = copy_node (TREE_CHAIN (parms));
2216 /* Record this partial instantiation. */
2217 DECL_TEMPLATE_INSTANTIATIONS (t)
2218 = perm_tree_cons (NULL_TREE, tmpl,
2219 DECL_TEMPLATE_INSTANTIATIONS (t));
2221 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
2222 return tmpl;
2225 case FUNCTION_DECL:
2227 tree r = NULL_TREE;
2228 tree ctx;
2230 int member;
2232 if (DECL_CONTEXT (t) != NULL_TREE
2233 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
2235 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
2236 member = 2;
2237 else
2238 member = 1;
2239 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
2240 type = tsubst (type, args, nargs, in_decl);
2242 else
2244 member = 0;
2245 ctx = NULL_TREE;
2246 type = tsubst (type, args, nargs, in_decl);
2249 if (type == TREE_TYPE (t)
2250 && (! member || ctx == DECL_CLASS_CONTEXT (t)))
2252 t = copy_node (t);
2253 copy_lang_decl (t);
2254 return t;
2257 /* Do we already have this instantiation? */
2258 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2260 tree tmpl = DECL_TI_TEMPLATE (t);
2261 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2263 for (; decls; decls = TREE_CHAIN (decls))
2264 if (TREE_TYPE (TREE_VALUE (decls)) == type
2265 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx)
2266 return TREE_VALUE (decls);
2269 /* We do NOT check for matching decls pushed separately at this
2270 point, as they may not represent instantiations of this
2271 template, and in any case are considered separate under the
2272 discrete model. Instead, see add_maybe_template. */
2274 r = copy_node (t);
2275 copy_lang_decl (r);
2276 TREE_TYPE (r) = type;
2278 DECL_CONTEXT (r)
2279 = tsubst (DECL_CONTEXT (t), args, nargs, t);
2280 DECL_CLASS_CONTEXT (r) = ctx;
2282 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
2283 IDENTIFIER_POINTER (DECL_NAME (r)),
2284 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
2286 /* Type-conversion operator. Reconstruct the name, in
2287 case it's the name of one of the template's parameters. */
2288 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
2291 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
2293 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
2294 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
2295 buf = (char *) alloca (strlen (dbuf)
2296 + sizeof (DESTRUCTOR_DECL_PREFIX));
2297 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
2298 buf[len] = '\0';
2299 strcat (buf, dbuf);
2300 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
2302 else
2304 /* Instantiations of template functions must be mangled
2305 specially, in order to conform to 14.5.5.1
2306 [temp.over.link]. We use in_decl below rather than
2307 DECL_TI_TEMPLATE (r) because the latter is set to
2308 NULL_TREE in instantiate_decl. */
2309 tree tmpl;
2310 tree arg_types;
2312 if (DECL_TEMPLATE_INFO (r))
2313 tmpl = DECL_TI_TEMPLATE (r);
2314 else
2315 tmpl = in_decl;
2317 /* tmpl will be NULL if this is a specialization of a
2318 member template of a template class. */
2319 if (name_mangling_version < 1
2320 || tmpl == NULL_TREE
2321 || (member && !is_member_template (tmpl)
2322 && !DECL_TEMPLATE_INFO (tmpl)))
2324 arg_types = TYPE_ARG_TYPES (type);
2325 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2326 arg_types = hash_tree_chain
2327 (build_pointer_type (DECL_CONTEXT (r)),
2328 arg_types);
2330 DECL_ASSEMBLER_NAME (r)
2331 = build_decl_overload (DECL_NAME (r), arg_types,
2332 member);
2334 else
2336 /* We pass the outermost template parameters to
2337 build_template_decl_overload since the innermost
2338 template parameters are still just template
2339 parameters; there are no corresponding substitution
2340 arguments. */
2341 /* FIXME The messed up thing here is that we get here with
2342 full args and only one level of parms. This is necessary
2343 because when we partially instantiate a member template,
2344 even though there's really only one level of parms left
2345 we re-use the parms from the original template, which
2346 have level 2. When this is fixed we can remove the
2347 add_to_template_args from instantiate_template. */
2348 tree tparms = DECL_TEMPLATE_PARMS (tmpl);
2350 while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
2351 tparms = TREE_CHAIN (tparms);
2353 my_friendly_assert (tparms != NULL_TREE
2354 && TREE_CODE (tparms) == TREE_LIST,
2356 tparms = TREE_VALUE (tparms);
2358 arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
2359 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2360 arg_types = hash_tree_chain
2361 (build_pointer_type (DECL_CONTEXT (r)),
2362 arg_types);
2364 DECL_ASSEMBLER_NAME (r)
2365 = build_template_decl_overload
2366 (DECL_NAME (r), arg_types,
2367 TREE_TYPE (TREE_TYPE (tmpl)),
2368 tparms,
2369 TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC
2370 ? TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1) :
2371 args,
2372 member);
2375 DECL_RTL (r) = 0;
2376 make_decl_rtl (r, NULL_PTR, 1);
2378 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
2379 DECL_MAIN_VARIANT (r) = r;
2380 DECL_RESULT (r) = NULL_TREE;
2381 DECL_INITIAL (r) = NULL_TREE;
2383 TREE_STATIC (r) = 0;
2384 TREE_PUBLIC (r) = 1;
2385 DECL_EXTERNAL (r) = 1;
2386 DECL_INTERFACE_KNOWN (r) = 0;
2387 DECL_DEFER_OUTPUT (r) = 0;
2388 TREE_CHAIN (r) = NULL_TREE;
2389 DECL_CHAIN (r) = NULL_TREE;
2391 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
2392 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
2394 /* Look for matching decls for the moment. */
2395 if (! member && ! flag_ansi_overloading)
2397 tree decls = lookup_name_nonclass (DECL_NAME (t));
2398 tree d = NULL_TREE;
2400 if (decls == NULL_TREE)
2401 /* no match */;
2402 else if (is_overloaded_fn (decls))
2403 for (decls = get_first_fn (decls); decls;
2404 decls = DECL_CHAIN (decls))
2406 if (TREE_CODE (decls) == FUNCTION_DECL
2407 && TREE_TYPE (decls) == type)
2409 d = decls;
2410 break;
2414 if (d)
2416 int dcl_only = ! DECL_INITIAL (d);
2417 if (dcl_only)
2418 DECL_INITIAL (r) = error_mark_node;
2419 duplicate_decls (r, d);
2420 r = d;
2421 if (dcl_only)
2422 DECL_INITIAL (r) = 0;
2426 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2428 tree tmpl = DECL_TI_TEMPLATE (t);
2429 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2430 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2432 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
2433 argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec);
2435 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2436 *declsp = perm_tree_cons (argvec, r, *declsp);
2438 /* If we have a preexisting version of this function, don't expand
2439 the template version, use the other instead. */
2440 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
2441 SET_DECL_TEMPLATE_SPECIALIZATION (r);
2442 else
2443 SET_DECL_IMPLICIT_INSTANTIATION (r);
2445 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
2446 = tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
2449 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2450 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2451 if (member
2452 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
2453 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
2455 return r;
2458 case PARM_DECL:
2460 tree r = copy_node (t);
2461 TREE_TYPE (r) = type;
2462 DECL_INITIAL (r) = TREE_TYPE (r);
2463 DECL_CONTEXT (r) = NULL_TREE;
2464 #ifdef PROMOTE_PROTOTYPES
2465 if ((TREE_CODE (type) == INTEGER_TYPE
2466 || TREE_CODE (type) == ENUMERAL_TYPE)
2467 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2468 DECL_ARG_TYPE (r) = integer_type_node;
2469 #endif
2470 if (TREE_CHAIN (t))
2471 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
2472 return r;
2475 case FIELD_DECL:
2477 tree r = copy_node (t);
2478 TREE_TYPE (r) = type;
2479 copy_lang_decl (r);
2480 #if 0
2481 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
2482 #endif
2483 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
2484 TREE_CHAIN (r) = NULL_TREE;
2485 return r;
2488 case USING_DECL:
2490 tree r = copy_node (t);
2491 DECL_INITIAL (r)
2492 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
2493 TREE_CHAIN (r) = NULL_TREE;
2494 return r;
2497 case VAR_DECL:
2499 tree r;
2500 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
2502 /* Do we already have this instantiation? */
2503 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2505 tree tmpl = DECL_TI_TEMPLATE (t);
2506 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2508 for (; decls; decls = TREE_CHAIN (decls))
2509 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
2510 return TREE_VALUE (decls);
2513 r = copy_node (t);
2514 TREE_TYPE (r) = type;
2515 DECL_CONTEXT (r) = ctx;
2516 if (TREE_STATIC (r))
2517 DECL_ASSEMBLER_NAME (r)
2518 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
2520 /* Don't try to expand the initializer until someone tries to use
2521 this variable; otherwise we run into circular dependencies. */
2522 DECL_INITIAL (r) = NULL_TREE;
2524 DECL_RTL (r) = 0;
2525 DECL_SIZE (r) = 0;
2527 if (DECL_LANG_SPECIFIC (r))
2529 copy_lang_decl (r);
2530 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
2533 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2535 tree tmpl = DECL_TI_TEMPLATE (t);
2536 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2537 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2539 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2540 *declsp = perm_tree_cons (argvec, r, *declsp);
2541 SET_DECL_IMPLICIT_INSTANTIATION (r);
2543 TREE_CHAIN (r) = NULL_TREE;
2544 return r;
2547 case TYPE_DECL:
2548 if (t == TYPE_NAME (TREE_TYPE (t)))
2549 return TYPE_NAME (type);
2552 tree r = copy_node (t);
2553 TREE_TYPE (r) = type;
2554 DECL_CONTEXT (r) = current_class_type;
2555 TREE_CHAIN (r) = NULL_TREE;
2556 return r;
2559 case TREE_LIST:
2561 tree purpose, value, chain, result;
2562 int via_public, via_virtual, via_protected;
2564 if (t == void_list_node)
2565 return t;
2567 via_public = TREE_VIA_PUBLIC (t);
2568 via_protected = TREE_VIA_PROTECTED (t);
2569 via_virtual = TREE_VIA_VIRTUAL (t);
2571 purpose = TREE_PURPOSE (t);
2572 if (purpose)
2573 purpose = tsubst (purpose, args, nargs, in_decl);
2574 value = TREE_VALUE (t);
2575 if (value)
2576 value = tsubst (value, args, nargs, in_decl);
2577 chain = TREE_CHAIN (t);
2578 if (chain && chain != void_type_node)
2579 chain = tsubst (chain, args, nargs, in_decl);
2580 if (purpose == TREE_PURPOSE (t)
2581 && value == TREE_VALUE (t)
2582 && chain == TREE_CHAIN (t))
2583 return t;
2584 result = hash_tree_cons (via_public, via_virtual, via_protected,
2585 purpose, value, chain);
2586 TREE_PARMLIST (result) = TREE_PARMLIST (t);
2587 return result;
2589 case TREE_VEC:
2590 if (type != NULL_TREE)
2592 /* A binfo node. */
2594 t = copy_node (t);
2596 if (type == TREE_TYPE (t))
2597 return t;
2599 TREE_TYPE (t) = complete_type (type);
2600 if (IS_AGGR_TYPE (type))
2602 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
2603 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
2604 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
2605 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
2607 return t;
2610 /* Otherwise, a vector of template arguments. */
2612 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
2613 tree *elts = (tree *) alloca (len * sizeof (tree));
2615 bzero ((char *) elts, len * sizeof (tree));
2617 for (i = 0; i < len; i++)
2619 elts[i] = tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl);
2621 if (TREE_CODE_CLASS (TREE_CODE (elts[i])) != 't'
2622 && !uses_template_parms (elts[i]))
2624 /* Sometimes, one of the args was an expression involving a
2625 template constant parameter, like N - 1. Now that we've
2626 tsubst'd, we might have something like 2 - 1. This will
2627 confuse lookup_template_class, so we do constant folding
2628 here. We have to unset processing_template_decl, to
2629 fool build_expr_from_tree() into building an actual
2630 tree. */
2632 int saved_processing_template_decl = processing_template_decl;
2633 processing_template_decl = 0;
2634 elts[i] = fold (build_expr_from_tree (elts[i]));
2635 processing_template_decl = saved_processing_template_decl;
2638 if (elts[i] != TREE_VEC_ELT (t, i))
2639 need_new = 1;
2642 if (!need_new)
2643 return t;
2645 t = make_tree_vec (len);
2646 for (i = 0; i < len; i++)
2647 TREE_VEC_ELT (t, i) = elts[i];
2649 return t;
2651 case POINTER_TYPE:
2652 case REFERENCE_TYPE:
2654 tree r;
2655 enum tree_code code;
2656 if (type == TREE_TYPE (t))
2657 return t;
2659 code = TREE_CODE (t);
2660 if (code == POINTER_TYPE)
2661 r = build_pointer_type (type);
2662 else
2663 r = build_reference_type (type);
2664 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
2665 /* Will this ever be needed for TYPE_..._TO values? */
2666 layout_type (r);
2667 return r;
2669 case OFFSET_TYPE:
2670 return build_offset_type
2671 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
2672 case FUNCTION_TYPE:
2673 case METHOD_TYPE:
2675 tree values = TYPE_ARG_TYPES (t);
2676 tree context = TYPE_CONTEXT (t);
2677 tree raises = TYPE_RAISES_EXCEPTIONS (t);
2678 tree fntype;
2680 /* Don't bother recursing if we know it won't change anything. */
2681 if (values != void_list_node)
2683 /* This should probably be rewritten to use hash_tree_cons for
2684 the memory savings. */
2685 tree first = NULL_TREE;
2686 tree last;
2688 for (; values && values != void_list_node;
2689 values = TREE_CHAIN (values))
2691 tree value = TYPE_MAIN_VARIANT (type_decays_to
2692 (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
2693 /* Don't instantiate default args unless they are used.
2694 Handle it in build_over_call instead. */
2695 tree purpose = TREE_PURPOSE (values);
2696 tree x = build_tree_list (purpose, value);
2698 if (first)
2699 TREE_CHAIN (last) = x;
2700 else
2701 first = x;
2702 last = x;
2705 if (values == void_list_node)
2706 TREE_CHAIN (last) = void_list_node;
2708 values = first;
2710 if (context)
2711 context = tsubst (context, args, nargs, in_decl);
2712 /* Could also optimize cases where return value and
2713 values have common elements (e.g., T min(const &T, const T&). */
2715 /* If the above parameters haven't changed, just return the type. */
2716 if (type == TREE_TYPE (t)
2717 && values == TYPE_VALUES (t)
2718 && context == TYPE_CONTEXT (t))
2719 return t;
2721 /* Construct a new type node and return it. */
2722 if (TREE_CODE (t) == FUNCTION_TYPE
2723 && context == NULL_TREE)
2725 fntype = build_function_type (type, values);
2727 else if (context == NULL_TREE)
2729 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
2730 args, nargs, in_decl);
2731 fntype = build_cplus_method_type (base, type,
2732 TREE_CHAIN (values));
2734 else
2736 fntype = make_node (TREE_CODE (t));
2737 TREE_TYPE (fntype) = type;
2738 TYPE_CONTEXT (fntype) = context;
2739 TYPE_VALUES (fntype) = values;
2740 TYPE_SIZE (fntype) = TYPE_SIZE (t);
2741 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
2742 TYPE_MODE (fntype) = TYPE_MODE (t);
2743 if (TYPE_METHOD_BASETYPE (t))
2744 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
2745 args, nargs, in_decl);
2746 /* Need to generate hash value. */
2747 my_friendly_abort (84);
2749 fntype = build_type_variant (fntype,
2750 TYPE_READONLY (t),
2751 TYPE_VOLATILE (t));
2752 if (raises)
2754 raises = tsubst (raises, args, nargs, in_decl);
2755 fntype = build_exception_variant (fntype, raises);
2757 return fntype;
2759 case ARRAY_TYPE:
2761 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
2762 tree r;
2763 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
2764 return t;
2765 r = build_cplus_array_type (type, domain);
2766 return r;
2769 case PLUS_EXPR:
2770 case MINUS_EXPR:
2771 return fold (build (TREE_CODE (t), TREE_TYPE (t),
2772 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2773 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
2775 case NEGATE_EXPR:
2776 case NOP_EXPR:
2777 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
2778 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
2780 case TYPENAME_TYPE:
2782 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2783 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
2784 return cp_build_type_variant
2785 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
2786 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
2789 case INDIRECT_REF:
2790 return make_pointer_declarator
2791 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2793 case ADDR_EXPR:
2794 return make_reference_declarator
2795 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2797 case ARRAY_REF:
2798 return build_parse_node
2799 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2800 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2802 case CALL_EXPR:
2803 return make_call_declarator
2804 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2805 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2806 TREE_OPERAND (t, 2),
2807 tsubst (TREE_TYPE (t), args, nargs, in_decl));
2809 case SCOPE_REF:
2810 return build_parse_node
2811 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2812 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
2814 default:
2815 sorry ("use of `%s' in template",
2816 tree_code_name [(int) TREE_CODE (t)]);
2817 return error_mark_node;
2821 void
2822 do_pushlevel ()
2824 emit_line_note (input_filename, lineno);
2825 pushlevel (0);
2826 clear_last_expr ();
2827 push_momentary ();
2828 expand_start_bindings (0);
2831 tree
2832 do_poplevel ()
2834 tree t;
2835 int saved_warn_unused;
2837 if (processing_template_decl)
2839 saved_warn_unused = warn_unused;
2840 warn_unused = 0;
2842 expand_end_bindings (getdecls (), kept_level_p (), 0);
2843 if (processing_template_decl)
2844 warn_unused = saved_warn_unused;
2845 t = poplevel (kept_level_p (), 1, 0);
2846 pop_momentary ();
2847 return t;
2850 tree
2851 tsubst_copy (t, args, nargs, in_decl)
2852 tree t, args;
2853 int nargs;
2854 tree in_decl;
2856 enum tree_code code;
2858 if (t == NULL_TREE || t == error_mark_node)
2859 return t;
2861 code = TREE_CODE (t);
2863 switch (code)
2865 case PARM_DECL:
2866 return do_identifier (DECL_NAME (t), 0);
2868 case CONST_DECL:
2869 case FIELD_DECL:
2870 if (DECL_CONTEXT (t))
2872 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
2873 if (ctx == current_function_decl)
2874 return lookup_name (DECL_NAME (t), 0);
2875 else if (ctx != DECL_CONTEXT (t))
2876 return lookup_field (ctx, DECL_NAME (t), 0, 0);
2878 return t;
2880 case VAR_DECL:
2881 case FUNCTION_DECL:
2882 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2883 t = tsubst (t, args, nargs, in_decl);
2884 mark_used (t);
2885 return t;
2887 case TEMPLATE_DECL:
2888 if (is_member_template (t))
2889 return tsubst (t, args, nargs, in_decl);
2890 else
2891 return t;
2893 #if 0
2894 case IDENTIFIER_NODE:
2895 return do_identifier (t, 0);
2896 #endif
2898 case CAST_EXPR:
2899 case REINTERPRET_CAST_EXPR:
2900 case CONST_CAST_EXPR:
2901 case STATIC_CAST_EXPR:
2902 case DYNAMIC_CAST_EXPR:
2903 return build1
2904 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2905 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2907 case INDIRECT_REF:
2908 case PREDECREMENT_EXPR:
2909 case PREINCREMENT_EXPR:
2910 case POSTDECREMENT_EXPR:
2911 case POSTINCREMENT_EXPR:
2912 case NEGATE_EXPR:
2913 case TRUTH_NOT_EXPR:
2914 case BIT_NOT_EXPR:
2915 case ADDR_EXPR:
2916 case CONVERT_EXPR: /* Unary + */
2917 case SIZEOF_EXPR:
2918 case ARROW_EXPR:
2919 case THROW_EXPR:
2920 case TYPEID_EXPR:
2921 return build1
2922 (code, NULL_TREE,
2923 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2925 case PLUS_EXPR:
2926 case MINUS_EXPR:
2927 case MULT_EXPR:
2928 case TRUNC_DIV_EXPR:
2929 case CEIL_DIV_EXPR:
2930 case FLOOR_DIV_EXPR:
2931 case ROUND_DIV_EXPR:
2932 case EXACT_DIV_EXPR:
2933 case BIT_AND_EXPR:
2934 case BIT_ANDTC_EXPR:
2935 case BIT_IOR_EXPR:
2936 case BIT_XOR_EXPR:
2937 case TRUNC_MOD_EXPR:
2938 case FLOOR_MOD_EXPR:
2939 case TRUTH_ANDIF_EXPR:
2940 case TRUTH_ORIF_EXPR:
2941 case TRUTH_AND_EXPR:
2942 case TRUTH_OR_EXPR:
2943 case RSHIFT_EXPR:
2944 case LSHIFT_EXPR:
2945 case RROTATE_EXPR:
2946 case LROTATE_EXPR:
2947 case EQ_EXPR:
2948 case NE_EXPR:
2949 case MAX_EXPR:
2950 case MIN_EXPR:
2951 case LE_EXPR:
2952 case GE_EXPR:
2953 case LT_EXPR:
2954 case GT_EXPR:
2955 case COMPONENT_REF:
2956 case ARRAY_REF:
2957 case COMPOUND_EXPR:
2958 case SCOPE_REF:
2959 case DOTSTAR_EXPR:
2960 case MEMBER_REF:
2961 return build_nt
2962 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2963 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2965 case CALL_EXPR:
2967 tree fn = TREE_OPERAND (t, 0);
2968 if (really_overloaded_fn (fn))
2969 fn = tsubst_copy (TREE_VALUE (fn), args, nargs, in_decl);
2970 else
2971 fn = tsubst_copy (fn, args, nargs, in_decl);
2972 return build_nt
2973 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2974 NULL_TREE);
2977 case METHOD_CALL_EXPR:
2979 tree name = TREE_OPERAND (t, 0);
2980 if (TREE_CODE (name) == BIT_NOT_EXPR)
2982 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2983 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2985 else if (TREE_CODE (name) == SCOPE_REF
2986 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
2988 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2989 name = TREE_OPERAND (name, 1);
2990 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
2991 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
2992 name = build_nt (SCOPE_REF, base, name);
2994 else
2995 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
2996 return build_nt
2997 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2998 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
2999 NULL_TREE);
3002 case COND_EXPR:
3003 case MODOP_EXPR:
3004 return build_nt
3005 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3006 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3007 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3009 case NEW_EXPR:
3011 tree r = build_nt
3012 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3013 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3014 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3015 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
3016 return r;
3019 case DELETE_EXPR:
3021 tree r = build_nt
3022 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3023 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
3024 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
3025 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
3026 return r;
3029 case TEMPLATE_ID_EXPR:
3031 tree r = lookup_template_function
3032 (tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3033 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
3034 return r;
3037 case TREE_LIST:
3039 tree purpose, value, chain;
3041 if (t == void_list_node)
3042 return t;
3044 purpose = TREE_PURPOSE (t);
3045 if (purpose)
3046 purpose = tsubst_copy (purpose, args, nargs, in_decl);
3047 value = TREE_VALUE (t);
3048 if (value)
3049 value = tsubst_copy (value, args, nargs, in_decl);
3050 chain = TREE_CHAIN (t);
3051 if (chain && chain != void_type_node)
3052 chain = tsubst_copy (chain, args, nargs, in_decl);
3053 if (purpose == TREE_PURPOSE (t)
3054 && value == TREE_VALUE (t)
3055 && chain == TREE_CHAIN (t))
3056 return t;
3057 return tree_cons (purpose, value, chain);
3060 case RECORD_TYPE:
3061 case UNION_TYPE:
3062 case ENUMERAL_TYPE:
3063 case INTEGER_TYPE:
3064 case TEMPLATE_TYPE_PARM:
3065 case TEMPLATE_CONST_PARM:
3066 case POINTER_TYPE:
3067 case REFERENCE_TYPE:
3068 case OFFSET_TYPE:
3069 case FUNCTION_TYPE:
3070 case METHOD_TYPE:
3071 case ARRAY_TYPE:
3072 case TYPENAME_TYPE:
3073 return tsubst (t, args, nargs, in_decl);
3075 case IDENTIFIER_NODE:
3076 if (IDENTIFIER_TYPENAME_P (t))
3077 return build_typename_overload
3078 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
3079 else
3080 return t;
3082 case CONSTRUCTOR:
3083 return build
3084 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
3085 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
3087 default:
3088 return t;
3092 tree
3093 tsubst_expr (t, args, nargs, in_decl)
3094 tree t, args;
3095 int nargs;
3096 tree in_decl;
3098 if (t == NULL_TREE || t == error_mark_node)
3099 return t;
3101 if (processing_template_decl)
3102 return tsubst_copy (t, args, nargs, in_decl);
3104 switch (TREE_CODE (t))
3106 case RETURN_STMT:
3107 lineno = TREE_COMPLEXITY (t);
3108 emit_line_note (input_filename, lineno);
3109 c_expand_return
3110 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3111 finish_stmt ();
3112 break;
3114 case EXPR_STMT:
3115 lineno = TREE_COMPLEXITY (t);
3116 emit_line_note (input_filename, lineno);
3117 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3118 /* Do default conversion if safe and possibly important,
3119 in case within ({...}). */
3120 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
3121 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
3122 t = default_conversion (t);
3123 cplus_expand_expr_stmt (t);
3124 clear_momentary ();
3125 finish_stmt ();
3126 break;
3128 case DECL_STMT:
3130 int i = suspend_momentary ();
3131 tree dcl, init;
3133 lineno = TREE_COMPLEXITY (t);
3134 emit_line_note (input_filename, lineno);
3135 dcl = start_decl
3136 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
3137 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
3138 TREE_OPERAND (t, 2) != 0);
3139 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3140 cp_finish_decl
3141 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3142 resume_momentary (i);
3143 return dcl;
3146 case FOR_STMT:
3148 tree tmp;
3149 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
3150 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3151 int cond_scope = (TREE_OPERAND (t, 1)
3152 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
3154 lineno = TREE_COMPLEXITY (t);
3155 emit_line_note (input_filename, lineno);
3156 if (init_scope)
3157 do_pushlevel ();
3158 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
3159 tsubst_expr (tmp, args, nargs, in_decl);
3160 emit_nop ();
3161 emit_line_note (input_filename, lineno);
3162 expand_start_loop_continue_elsewhere (1);
3164 if (cond_scope)
3165 do_pushlevel ();
3166 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3167 emit_line_note (input_filename, lineno);
3168 if (tmp)
3169 expand_exit_loop_if_false (0, condition_conversion (tmp));
3171 if (! cond_scope)
3172 do_pushlevel ();
3173 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
3174 do_poplevel ();
3176 emit_line_note (input_filename, lineno);
3177 expand_loop_continue_here ();
3178 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3179 if (tmp)
3180 cplus_expand_expr_stmt (tmp);
3182 expand_end_loop ();
3183 if (init_scope)
3184 do_poplevel ();
3185 finish_stmt ();
3187 break;
3189 case WHILE_STMT:
3191 tree cond;
3193 lineno = TREE_COMPLEXITY (t);
3194 emit_nop ();
3195 emit_line_note (input_filename, lineno);
3196 expand_start_loop (1);
3198 cond = TREE_OPERAND (t, 0);
3199 if (TREE_CODE (cond) == DECL_STMT)
3200 do_pushlevel ();
3201 cond = tsubst_expr (cond, args, nargs, in_decl);
3202 emit_line_note (input_filename, lineno);
3203 expand_exit_loop_if_false (0, condition_conversion (cond));
3205 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
3206 do_pushlevel ();
3207 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3208 do_poplevel ();
3210 expand_end_loop ();
3211 finish_stmt ();
3213 break;
3215 case DO_STMT:
3217 tree cond;
3219 lineno = TREE_COMPLEXITY (t);
3220 emit_nop ();
3221 emit_line_note (input_filename, lineno);
3222 expand_start_loop_continue_elsewhere (1);
3224 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3225 expand_loop_continue_here ();
3227 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3228 emit_line_note (input_filename, lineno);
3229 expand_exit_loop_if_false (0, condition_conversion (cond));
3230 expand_end_loop ();
3232 clear_momentary ();
3233 finish_stmt ();
3235 break;
3237 case IF_STMT:
3239 tree tmp;
3240 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3242 lineno = TREE_COMPLEXITY (t);
3243 if (cond_scope)
3244 do_pushlevel ();
3245 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3246 emit_line_note (input_filename, lineno);
3247 expand_start_cond (condition_conversion (tmp), 0);
3249 if (tmp = TREE_OPERAND (t, 1), tmp)
3250 tsubst_expr (tmp, args, nargs, in_decl);
3252 if (tmp = TREE_OPERAND (t, 2), tmp)
3254 expand_start_else ();
3255 tsubst_expr (tmp, args, nargs, in_decl);
3258 expand_end_cond ();
3260 if (cond_scope)
3261 do_poplevel ();
3263 finish_stmt ();
3265 break;
3267 case COMPOUND_STMT:
3269 tree substmt = TREE_OPERAND (t, 0);
3271 lineno = TREE_COMPLEXITY (t);
3273 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3274 do_pushlevel ();
3276 for (; substmt; substmt = TREE_CHAIN (substmt))
3277 tsubst_expr (substmt, args, nargs, in_decl);
3279 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3280 do_poplevel ();
3282 break;
3284 case BREAK_STMT:
3285 lineno = TREE_COMPLEXITY (t);
3286 emit_line_note (input_filename, lineno);
3287 if (! expand_exit_something ())
3288 error ("break statement not within loop or switch");
3289 break;
3291 case CONTINUE_STMT:
3292 lineno = TREE_COMPLEXITY (t);
3293 emit_line_note (input_filename, lineno);
3294 if (! expand_continue_loop (0))
3295 error ("continue statement not within a loop");
3296 break;
3298 case SWITCH_STMT:
3300 tree val, tmp;
3301 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3303 lineno = TREE_COMPLEXITY (t);
3304 if (cond_scope)
3305 do_pushlevel ();
3306 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3307 emit_line_note (input_filename, lineno);
3308 c_expand_start_case (val);
3309 push_switch ();
3311 if (tmp = TREE_OPERAND (t, 1), tmp)
3312 tsubst_expr (tmp, args, nargs, in_decl);
3314 expand_end_case (val);
3315 pop_switch ();
3317 if (cond_scope)
3318 do_poplevel ();
3320 finish_stmt ();
3322 break;
3324 case CASE_LABEL:
3325 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
3326 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
3327 break;
3329 case LABEL_DECL:
3330 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
3331 DECL_NAME (t));
3332 if (t)
3333 expand_label (t);
3334 break;
3336 case GOTO_STMT:
3337 lineno = TREE_COMPLEXITY (t);
3338 emit_line_note (input_filename, lineno);
3339 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
3341 tree decl = lookup_label (TREE_OPERAND (t, 0));
3342 TREE_USED (decl) = 1;
3343 expand_goto (decl);
3345 else
3346 expand_computed_goto
3347 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3348 break;
3350 case TRY_BLOCK:
3351 lineno = TREE_COMPLEXITY (t);
3352 emit_line_note (input_filename, lineno);
3353 expand_start_try_stmts ();
3354 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3355 expand_start_all_catch ();
3357 tree handler = TREE_OPERAND (t, 1);
3358 for (; handler; handler = TREE_CHAIN (handler))
3359 tsubst_expr (handler, args, nargs, in_decl);
3361 expand_end_all_catch ();
3362 break;
3364 case HANDLER:
3365 lineno = TREE_COMPLEXITY (t);
3366 do_pushlevel ();
3367 if (TREE_OPERAND (t, 0))
3369 tree d = TREE_OPERAND (t, 0);
3370 expand_start_catch_block
3371 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
3372 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
3374 else
3375 expand_start_catch_block (NULL_TREE, NULL_TREE);
3376 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3377 expand_end_catch_block ();
3378 do_poplevel ();
3379 break;
3381 case TAG_DEFN:
3382 lineno = TREE_COMPLEXITY (t);
3383 t = TREE_TYPE (t);
3384 if (TREE_CODE (t) == ENUMERAL_TYPE)
3385 tsubst_enum (t, args, nargs, NULL);
3386 break;
3388 default:
3389 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
3391 return NULL_TREE;
3394 tree
3395 instantiate_template (tmpl, targ_ptr)
3396 tree tmpl, targ_ptr;
3398 tree fndecl;
3399 int i, len;
3400 struct obstack *old_fmp_obstack;
3401 extern struct obstack *function_maybepermanent_obstack;
3403 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
3405 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3407 tree specs;
3409 /* Check to see if there is a matching specialization. */
3410 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3411 specs != NULL_TREE;
3412 specs = TREE_CHAIN (specs))
3413 if (comp_template_args (TREE_PURPOSE (specs), targ_ptr))
3414 return TREE_VALUE (specs);
3417 push_obstacks (&permanent_obstack, &permanent_obstack);
3418 old_fmp_obstack = function_maybepermanent_obstack;
3419 function_maybepermanent_obstack = &permanent_obstack;
3421 len = DECL_NTPARMS (tmpl);
3423 i = len;
3424 while (i--)
3426 tree t = TREE_VEC_ELT (targ_ptr, i);
3427 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3429 tree nt = target_type (t);
3430 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
3432 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
3433 cp_error (" trying to instantiate `%D'", tmpl);
3434 fndecl = error_mark_node;
3435 goto out;
3438 TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
3440 targ_ptr = copy_to_permanent (targ_ptr);
3442 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
3443 targ_ptr = add_to_template_args (DECL_TI_ARGS (tmpl), targ_ptr);
3445 /* substitute template parameters */
3446 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
3448 if (flag_external_templates)
3449 add_pending_template (fndecl);
3451 out:
3452 function_maybepermanent_obstack = old_fmp_obstack;
3453 pop_obstacks ();
3455 return fndecl;
3458 /* Push the name of the class template into the scope of the instantiation. */
3460 void
3461 overload_template_name (type)
3462 tree type;
3464 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
3465 tree decl;
3467 if (IDENTIFIER_CLASS_VALUE (id)
3468 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
3469 return;
3471 decl = build_decl (TYPE_DECL, id, type);
3472 SET_DECL_ARTIFICIAL (decl);
3473 pushdecl_class_level (decl);
3476 /* Like type_unification but designed specially to handle conversion
3477 operators. */
3480 fn_type_unification (fn, explicit_targs, targs, args, return_type, strict)
3481 tree fn, explicit_targs, targs, args, return_type;
3482 int strict;
3484 int i, dummy = 0;
3485 tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
3486 tree decl_arg_types = args;
3488 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
3490 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn)))
3492 /* This is a template conversion operator. Use the return types
3493 as well as the argument types. */
3494 fn_arg_types = scratch_tree_cons (NULL_TREE,
3495 TREE_TYPE (TREE_TYPE (fn)),
3496 fn_arg_types);
3497 decl_arg_types = scratch_tree_cons (NULL_TREE,
3498 return_type,
3499 decl_arg_types);
3502 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
3503 &TREE_VEC_ELT (targs, 0),
3504 fn_arg_types,
3505 decl_arg_types,
3506 explicit_targs,
3507 &dummy, strict, 0);
3509 return i;
3513 /* Type unification.
3515 We have a function template signature with one or more references to
3516 template parameters, and a parameter list we wish to fit to this
3517 template. If possible, produce a list of parameters for the template
3518 which will cause it to fit the supplied parameter list.
3520 Return zero for success, 2 for an incomplete match that doesn't resolve
3521 all the types, and 1 for complete failure. An error message will be
3522 printed only for an incomplete match.
3524 TPARMS[NTPARMS] is an array of template parameter types;
3525 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3526 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3527 and ARGS is the argument list we're trying to match against it.
3529 If SUBR is 1, we're being called recursively (to unify the arguments of
3530 a function or method parameter of a function template), so don't zero
3531 out targs and don't fail on an incomplete match.
3533 If STRICT is 1, the match must be exact (for casts of overloaded
3534 addresses, explicit instantiation, and more_specialized). */
3537 type_unification (tparms, targs, parms, args, targs_in, nsubsts,
3538 strict, allow_incomplete)
3539 tree tparms, *targs, parms, args, targs_in;
3540 int *nsubsts, strict, allow_incomplete;
3542 int ntparms = TREE_VEC_LENGTH (tparms);
3543 tree t;
3544 int i;
3545 int r;
3547 bzero ((char *) targs, sizeof (tree) * ntparms);
3549 /* Insert any explicit template arguments. They are encoded as the
3550 operands of NOP_EXPRs so that unify can tell that they are
3551 explicit arguments. */
3552 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3553 targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VALUE (t));
3555 r = type_unification_real (tparms, targs, parms, args, nsubsts, 0,
3556 strict, allow_incomplete);
3558 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3559 if (TREE_CODE (targs[i]) == NOP_EXPR)
3560 targs[i] = TREE_OPERAND (targs[i], 0);
3562 return r;
3566 static int
3567 type_unification_real (tparms, targs, parms, args, nsubsts, subr,
3568 strict, allow_incomplete)
3569 tree tparms, *targs, parms, args;
3570 int *nsubsts, subr, strict, allow_incomplete;
3572 tree parm, arg;
3573 int i;
3574 int ntparms = TREE_VEC_LENGTH (tparms);
3576 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
3577 my_friendly_assert (parms == NULL_TREE
3578 || TREE_CODE (parms) == TREE_LIST, 290);
3579 /* ARGS could be NULL (via a call from parse.y to
3580 build_x_function_call). */
3581 if (args)
3582 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
3583 my_friendly_assert (ntparms > 0, 292);
3585 while (parms
3586 && parms != void_list_node
3587 && args
3588 && args != void_list_node)
3590 parm = TREE_VALUE (parms);
3591 parms = TREE_CHAIN (parms);
3592 arg = TREE_VALUE (args);
3593 args = TREE_CHAIN (args);
3595 if (arg == error_mark_node)
3596 return 1;
3597 if (arg == unknown_type_node)
3598 return 1;
3600 /* Conversions will be performed on a function argument that
3601 corresponds with a function parameter that contains only
3602 non-deducible template parameters and explicitly specified
3603 template parameters. */
3604 if (! uses_template_parms (parm))
3606 tree type;
3608 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3609 type = TREE_TYPE (arg);
3610 else
3612 type = arg;
3613 arg = NULL_TREE;
3616 if (strict)
3618 if (comptypes (parm, type, 1))
3619 continue;
3621 else if (arg)
3623 if (can_convert_arg (parm, type, arg))
3624 continue;
3626 else
3628 if (can_convert (parm, type))
3629 continue;
3632 return 1;
3635 #if 0
3636 if (TREE_CODE (arg) == VAR_DECL)
3637 arg = TREE_TYPE (arg);
3638 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
3639 arg = TREE_TYPE (arg);
3640 #else
3641 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3643 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
3644 if (TREE_CODE (arg) == TREE_LIST
3645 && TREE_TYPE (arg) == unknown_type_node
3646 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
3648 int nsubsts, ntparms;
3649 tree *targs;
3651 /* Have to back unify here */
3652 arg = TREE_VALUE (arg);
3653 nsubsts = 0;
3654 ntparms = DECL_NTPARMS (arg);
3655 targs = (tree *) alloca (sizeof (tree) * ntparms);
3656 parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
3657 return
3658 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg),
3659 targs,
3660 TYPE_ARG_TYPES (TREE_TYPE (arg)),
3661 parm, NULL_TREE, &nsubsts, strict,
3662 allow_incomplete);
3664 arg = TREE_TYPE (arg);
3666 #endif
3667 if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
3668 arg = TREE_TYPE (arg);
3670 if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
3672 if (TREE_CODE (arg) == FUNCTION_TYPE
3673 || TREE_CODE (arg) == METHOD_TYPE)
3674 arg = build_pointer_type (arg);
3675 else if (TREE_CODE (arg) == ARRAY_TYPE)
3676 arg = build_pointer_type (TREE_TYPE (arg));
3677 else
3678 arg = TYPE_MAIN_VARIANT (arg);
3681 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
3683 case 0:
3684 break;
3685 case 1:
3686 return 1;
3689 /* Fail if we've reached the end of the parm list, and more args
3690 are present, and the parm list isn't variadic. */
3691 if (args && args != void_list_node && parms == void_list_node)
3692 return 1;
3693 /* Fail if parms are left and they don't have default values. */
3694 if (parms
3695 && parms != void_list_node
3696 && TREE_PURPOSE (parms) == NULL_TREE)
3697 return 1;
3698 if (!subr)
3699 for (i = 0; i < ntparms; i++)
3700 if (!targs[i])
3702 if (!allow_incomplete)
3703 error ("incomplete type unification");
3704 return 2;
3706 return 0;
3709 /* Tail recursion is your friend. */
3711 static int
3712 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
3713 tree tparms, *targs, parm, arg;
3714 int *nsubsts, ntparms, strict;
3716 int idx;
3718 /* I don't think this will do the right thing with respect to types.
3719 But the only case I've seen it in so far has been array bounds, where
3720 signedness is the only information lost, and I think that will be
3721 okay. */
3722 while (TREE_CODE (parm) == NOP_EXPR)
3723 parm = TREE_OPERAND (parm, 0);
3725 if (arg == error_mark_node)
3726 return 1;
3727 if (arg == unknown_type_node)
3728 return 1;
3729 if (arg == parm)
3730 return 0;
3732 switch (TREE_CODE (parm))
3734 case TYPENAME_TYPE:
3735 /* In a type which contains a nested-name-specifier, template
3736 argument values cannot be deduced for template parameters used
3737 within the nested-name-specifier. */
3738 return 0;
3740 case TEMPLATE_TYPE_PARM:
3741 (*nsubsts)++;
3742 idx = TEMPLATE_TYPE_IDX (parm);
3743 /* Check for mixed types and values. */
3744 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
3745 return 1;
3747 if (!strict && targs[idx] != NULL_TREE &&
3748 TREE_CODE (targs[idx]) == NOP_EXPR)
3749 /* An explicit template argument. Don't even try to match
3750 here; the overload resolution code will manage check to
3751 see whether the call is legal. */
3752 return 0;
3754 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
3755 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
3756 return 1;
3757 #if 0
3758 /* Template type parameters cannot contain cv-quals; i.e.
3759 template <class T> void f (T& a, T& b) will not generate
3760 void f (const int& a, const int& b). */
3761 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
3762 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
3763 return 1;
3764 arg = TYPE_MAIN_VARIANT (arg);
3765 #else
3767 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
3768 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
3769 arg = cp_build_type_variant (arg, constp, volatilep);
3771 #endif
3772 /* Simple cases: Value already set, does match or doesn't. */
3773 if (targs[idx] == arg
3774 || (targs[idx]
3775 && TREE_CODE (targs[idx]) == NOP_EXPR
3776 && TREE_OPERAND (targs[idx], 0) == arg))
3777 return 0;
3778 else if (targs[idx])
3779 return 1;
3780 targs[idx] = arg;
3781 return 0;
3782 case TEMPLATE_CONST_PARM:
3783 (*nsubsts)++;
3784 idx = TEMPLATE_CONST_IDX (parm);
3785 if (targs[idx])
3787 int i = cp_tree_equal (targs[idx], arg);
3788 if (i == 1)
3789 return 0;
3790 else if (i == 0)
3791 return 1;
3792 else
3793 my_friendly_abort (42);
3796 targs[idx] = copy_to_permanent (arg);
3797 return 0;
3799 case POINTER_TYPE:
3800 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
3801 return unify (tparms, targs, ntparms, parm,
3802 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
3804 if (TREE_CODE (arg) != POINTER_TYPE)
3805 return 1;
3806 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3807 nsubsts, strict);
3809 case REFERENCE_TYPE:
3810 if (TREE_CODE (arg) == REFERENCE_TYPE)
3811 arg = TREE_TYPE (arg);
3812 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
3813 nsubsts, strict);
3815 case ARRAY_TYPE:
3816 if (TREE_CODE (arg) != ARRAY_TYPE)
3817 return 1;
3818 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
3819 nsubsts, strict) != 0)
3820 return 1;
3821 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3822 nsubsts, strict);
3824 case REAL_TYPE:
3825 case COMPLEX_TYPE:
3826 case INTEGER_TYPE:
3827 case BOOLEAN_TYPE:
3828 if (TREE_CODE (arg) != TREE_CODE (parm))
3829 return 1;
3831 if (TREE_CODE (parm) == INTEGER_TYPE)
3833 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
3834 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
3835 TYPE_MIN_VALUE (arg), nsubsts, strict))
3836 return 1;
3837 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
3838 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
3839 TYPE_MAX_VALUE (arg), nsubsts, strict))
3840 return 1;
3842 else if (TREE_CODE (parm) == REAL_TYPE
3843 && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
3844 return 1;
3846 /* As far as unification is concerned, this wins. Later checks
3847 will invalidate it if necessary. */
3848 return 0;
3850 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3851 /* Type INTEGER_CST can come from ordinary constant template args. */
3852 case INTEGER_CST:
3853 while (TREE_CODE (arg) == NOP_EXPR)
3854 arg = TREE_OPERAND (arg, 0);
3856 if (TREE_CODE (arg) != INTEGER_CST)
3857 return 1;
3858 return !tree_int_cst_equal (parm, arg);
3860 case MINUS_EXPR:
3862 tree t1, t2;
3863 t1 = TREE_OPERAND (parm, 0);
3864 t2 = TREE_OPERAND (parm, 1);
3865 return unify (tparms, targs, ntparms, t1,
3866 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
3867 nsubsts, strict);
3870 case TREE_VEC:
3872 int i;
3873 if (TREE_CODE (arg) != TREE_VEC)
3874 return 1;
3875 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
3876 return 1;
3877 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
3878 if (unify (tparms, targs, ntparms,
3879 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
3880 nsubsts, strict))
3881 return 1;
3882 return 0;
3885 case RECORD_TYPE:
3886 if (TYPE_PTRMEMFUNC_FLAG (parm))
3887 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
3888 arg, nsubsts, strict);
3890 /* Allow trivial conversions. */
3891 if (TREE_CODE (arg) != RECORD_TYPE
3892 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
3893 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
3894 return 1;
3896 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
3898 tree t = NULL_TREE;
3899 if (flag_ansi_overloading && ! strict)
3900 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
3901 else if
3902 (CLASSTYPE_TEMPLATE_INFO (arg)
3903 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
3904 t = arg;
3905 if (! t || t == error_mark_node)
3906 return 1;
3908 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
3909 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
3911 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
3912 return 1;
3913 return 0;
3915 case METHOD_TYPE:
3916 if (TREE_CODE (arg) != METHOD_TYPE)
3917 return 1;
3918 goto check_args;
3920 case FUNCTION_TYPE:
3921 if (TREE_CODE (arg) != FUNCTION_TYPE)
3922 return 1;
3923 check_args:
3924 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
3925 TREE_TYPE (arg), nsubsts, strict))
3926 return 1;
3927 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3928 TYPE_ARG_TYPES (arg), nsubsts, 1,
3929 strict, 0);
3931 case OFFSET_TYPE:
3932 if (TREE_CODE (arg) != OFFSET_TYPE)
3933 return 1;
3934 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
3935 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
3936 return 1;
3937 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
3938 TREE_TYPE (arg), nsubsts, strict);
3940 case CONST_DECL:
3941 if (arg != decl_constant_value (parm))
3942 return 1;
3943 return 0;
3945 default:
3946 sorry ("use of `%s' in template type unification",
3947 tree_code_name [(int) TREE_CODE (parm)]);
3948 return 1;
3952 void
3953 mark_decl_instantiated (result, extern_p)
3954 tree result;
3955 int extern_p;
3957 if (DECL_TEMPLATE_INSTANTIATION (result))
3958 SET_DECL_EXPLICIT_INSTANTIATION (result);
3959 TREE_PUBLIC (result) = 1;
3961 if (! extern_p)
3963 DECL_INTERFACE_KNOWN (result) = 1;
3964 DECL_NOT_REALLY_EXTERN (result) = 1;
3966 else if (TREE_CODE (result) == FUNCTION_DECL)
3967 mark_inline_for_output (result);
3970 /* Given two function templates PAT1 and PAT2, return:
3972 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
3973 -1 if PAT2 is more specialized than PAT1.
3974 0 if neither is more specialized. */
3977 more_specialized (pat1, pat2)
3978 tree pat1, pat2;
3980 tree targs;
3981 int winner = 0;
3983 targs = get_bindings (pat1, pat2);
3984 if (targs)
3986 --winner;
3989 targs = get_bindings (pat2, pat1);
3990 if (targs)
3992 ++winner;
3995 return winner;
3998 /* Given two class template specialization list nodes PAT1 and PAT2, return:
4000 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4001 -1 if PAT2 is more specialized than PAT1.
4002 0 if neither is more specialized. */
4005 more_specialized_class (pat1, pat2)
4006 tree pat1, pat2;
4008 tree targs;
4009 int winner = 0;
4011 targs = get_class_bindings
4012 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
4013 if (targs)
4014 --winner;
4016 targs = get_class_bindings
4017 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
4018 if (targs)
4019 ++winner;
4021 return winner;
4024 /* Return the template arguments that will produce the function signature
4025 DECL from the function template FN. */
4027 tree
4028 get_bindings (fn, decl)
4029 tree fn, decl;
4031 int ntparms = DECL_NTPARMS (fn);
4032 tree targs = make_scratch_vec (ntparms);
4033 int i;
4035 i = fn_type_unification (fn, NULL_TREE, targs,
4036 TYPE_ARG_TYPES (TREE_TYPE (decl)),
4037 TREE_TYPE (TREE_TYPE (decl)),
4040 if (i == 0)
4041 return targs;
4042 return 0;
4045 static tree
4046 get_class_bindings (tparms, parms, args)
4047 tree tparms, parms, args;
4049 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
4050 tree vec = make_temp_vec (ntparms);
4052 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
4054 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
4055 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
4056 &dummy, 1))
4058 case 0:
4059 break;
4060 case 1:
4061 return NULL_TREE;
4065 for (i = 0; i < ntparms; ++i)
4066 if (! TREE_VEC_ELT (vec, i))
4067 return NULL_TREE;
4069 return vec;
4072 /* Return the most specialized of the list of templates in FNS that can
4073 produce an instantiation matching DECL. */
4075 tree
4076 most_specialized (fns, decl)
4077 tree fns, decl;
4079 tree fn, champ, args, *p;
4080 int fate;
4082 for (p = &fns; *p; )
4084 args = get_bindings (TREE_VALUE (*p), decl);
4085 if (args)
4087 p = &TREE_CHAIN (*p);
4089 else
4090 *p = TREE_CHAIN (*p);
4093 if (! fns)
4094 return NULL_TREE;
4096 fn = fns;
4097 champ = TREE_VALUE (fn);
4098 fn = TREE_CHAIN (fn);
4099 for (; fn; fn = TREE_CHAIN (fn))
4101 fate = more_specialized (champ, TREE_VALUE (fn));
4102 if (fate == 1)
4104 else
4106 if (fate == 0)
4108 fn = TREE_CHAIN (fn);
4109 if (! fn)
4110 return error_mark_node;
4112 champ = TREE_VALUE (fn);
4116 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
4118 fate = more_specialized (champ, TREE_VALUE (fn));
4119 if (fate != 1)
4120 return error_mark_node;
4123 return champ;
4126 /* Return the most specialized of the class template specializations in
4127 SPECS that can produce an instantiation matching ARGS. */
4129 tree
4130 most_specialized_class (specs, mainargs)
4131 tree specs, mainargs;
4133 tree list = NULL_TREE, t, args, champ;
4134 int fate;
4136 for (t = specs; t; t = TREE_CHAIN (t))
4138 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
4139 if (args)
4141 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
4142 TREE_TYPE (list) = TREE_TYPE (t);
4146 if (! list)
4147 return NULL_TREE;
4149 t = list;
4150 champ = t;
4151 t = TREE_CHAIN (t);
4152 for (; t; t = TREE_CHAIN (t))
4154 fate = more_specialized_class (champ, t);
4155 if (fate == 1)
4157 else
4159 if (fate == 0)
4161 t = TREE_CHAIN (t);
4162 if (! t)
4163 return error_mark_node;
4165 champ = t;
4169 for (t = list; t && t != champ; t = TREE_CHAIN (t))
4171 fate = more_specialized_class (champ, t);
4172 if (fate != 1)
4173 return error_mark_node;
4176 return champ;
4179 /* called from the parser. */
4181 void
4182 do_decl_instantiation (declspecs, declarator, storage)
4183 tree declspecs, declarator, storage;
4185 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
4186 tree name;
4187 tree fn;
4188 tree result = NULL_TREE;
4189 int extern_p = 0;
4190 tree templates = NULL_TREE;
4192 if (! DECL_LANG_SPECIFIC (decl))
4194 cp_error ("explicit instantiation of non-template `%#D'", decl);
4195 return;
4198 /* If we've already seen this template instance, use it. */
4199 if (TREE_CODE (decl) == VAR_DECL)
4201 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
4202 if (result && TREE_CODE (result) != VAR_DECL)
4203 result = NULL_TREE;
4205 else if (TREE_CODE (decl) != FUNCTION_DECL)
4207 cp_error ("explicit instantiation of `%#D'", decl);
4208 return;
4210 else if (DECL_FUNCTION_MEMBER_P (decl))
4212 if (DECL_TEMPLATE_INSTANTIATION (decl))
4213 result = decl;
4214 else if (name = DECL_ASSEMBLER_NAME (decl),
4215 fn = IDENTIFIER_GLOBAL_VALUE (name),
4216 fn && DECL_TEMPLATE_INSTANTIATION (fn))
4217 result = fn;
4218 else
4220 /* Maybe this is an instantiation of a member template
4221 function. */
4222 tree ctype = DECL_CONTEXT (decl);
4224 name = DECL_NAME (decl);
4225 fn = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
4226 if (fn)
4227 fn = TREE_VALUE (fn);
4229 for (; fn; fn = DECL_CHAIN (fn))
4230 if (TREE_CODE (fn) == TEMPLATE_DECL)
4231 templates = decl_tree_cons (NULL_TREE, fn, templates);
4234 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
4236 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
4237 if (TREE_CODE (fn) == TEMPLATE_DECL)
4238 templates = decl_tree_cons (NULL_TREE, fn, templates);
4241 if (templates && !result)
4243 tree args;
4244 result = most_specialized (templates, decl);
4245 if (result == error_mark_node)
4247 char *str = "candidates are:";
4248 cp_error ("ambiguous template instantiation for `%D' requested", decl);
4249 for (fn = templates; fn; fn = TREE_CHAIN (fn))
4251 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
4252 str = " ";
4254 return;
4256 else if (result)
4258 args = get_bindings (result, decl);
4259 result = instantiate_template (result, args);
4263 if (! result)
4265 cp_error ("no matching template for `%D' found", decl);
4266 return;
4269 if (! DECL_TEMPLATE_INFO (result))
4271 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
4272 return;
4275 if (flag_external_templates)
4276 return;
4278 if (storage == NULL_TREE)
4280 else if (storage == ridpointers[(int) RID_EXTERN])
4281 extern_p = 1;
4282 else
4283 cp_error ("storage class `%D' applied to template instantiation",
4284 storage);
4286 mark_decl_instantiated (result, extern_p);
4287 repo_template_instantiated (result, extern_p);
4288 if (! extern_p)
4289 instantiate_decl (result);
4292 void
4293 mark_class_instantiated (t, extern_p)
4294 tree t;
4295 int extern_p;
4297 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
4298 SET_CLASSTYPE_INTERFACE_KNOWN (t);
4299 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
4300 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
4301 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
4302 if (! extern_p)
4304 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
4305 rest_of_type_compilation (t, 1);
4309 void
4310 do_type_instantiation (t, storage)
4311 tree t, storage;
4313 int extern_p = 0;
4314 int nomem_p = 0;
4315 int static_p = 0;
4317 if (TREE_CODE (t) == TYPE_DECL)
4318 t = TREE_TYPE (t);
4320 if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
4322 cp_error ("explicit instantiation of non-template type `%T'", t);
4323 return;
4326 complete_type (t);
4328 /* With -fexternal-templates, explicit instantiations are treated the same
4329 as implicit ones. */
4330 if (flag_external_templates)
4331 return;
4333 if (TYPE_SIZE (t) == NULL_TREE)
4335 cp_error ("explicit instantiation of `%#T' before definition of template",
4337 return;
4340 if (storage == NULL_TREE)
4341 /* OK */;
4342 else if (storage == ridpointers[(int) RID_INLINE])
4343 nomem_p = 1;
4344 else if (storage == ridpointers[(int) RID_EXTERN])
4345 extern_p = 1;
4346 else if (storage == ridpointers[(int) RID_STATIC])
4347 static_p = 1;
4348 else
4350 cp_error ("storage class `%D' applied to template instantiation",
4351 storage);
4352 extern_p = 0;
4355 /* We've already instantiated this. */
4356 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
4357 && extern_p)
4358 return;
4360 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4362 mark_class_instantiated (t, extern_p);
4363 repo_template_instantiated (t, extern_p);
4366 if (nomem_p)
4367 return;
4370 tree tmp;
4372 if (! static_p)
4373 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
4374 if (DECL_TEMPLATE_INSTANTIATION (tmp))
4376 mark_decl_instantiated (tmp, extern_p);
4377 repo_template_instantiated (tmp, extern_p);
4378 if (! extern_p)
4379 instantiate_decl (tmp);
4382 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
4383 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
4385 mark_decl_instantiated (tmp, extern_p);
4386 repo_template_instantiated (tmp, extern_p);
4387 if (! extern_p)
4388 instantiate_decl (tmp);
4391 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
4392 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
4393 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
4397 tree
4398 instantiate_decl (d)
4399 tree d;
4401 tree ti = DECL_TEMPLATE_INFO (d);
4402 tree tmpl = TI_TEMPLATE (ti);
4403 tree args = TI_ARGS (ti);
4404 tree td;
4405 tree decl_pattern, code_pattern;
4406 tree save_ti;
4407 int nested = in_function_p ();
4408 int d_defined;
4409 int pattern_defined;
4410 int line = lineno;
4411 char *file = input_filename;
4413 for (td = tmpl; DECL_TEMPLATE_INSTANTIATION (td); )
4414 td = DECL_TI_TEMPLATE (td);
4416 /* In the case of a member template, decl_pattern is the partially
4417 instantiated declaration (in the instantiated class), and code_pattern
4418 is the original template definition. */
4419 decl_pattern = DECL_TEMPLATE_RESULT (tmpl);
4420 code_pattern = DECL_TEMPLATE_RESULT (td);
4422 if (TREE_CODE (d) == FUNCTION_DECL)
4424 d_defined = (DECL_INITIAL (d) != NULL_TREE);
4425 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
4427 else
4429 d_defined = ! DECL_IN_AGGR_P (d);
4430 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
4433 if (d_defined)
4434 return d;
4436 if (TREE_CODE (d) == FUNCTION_DECL)
4438 tree specs;
4440 /* Check to see if there is a matching specialization. */
4441 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4442 specs != NULL_TREE;
4443 specs = TREE_CHAIN (specs))
4444 if (comp_template_args (TREE_PURPOSE (specs), args))
4445 return TREE_VALUE (specs);
4448 /* This needs to happen before any tsubsting. */
4449 if (! push_tinst_level (d))
4450 return d;
4452 push_to_top_level ();
4453 lineno = DECL_SOURCE_LINE (d);
4454 input_filename = DECL_SOURCE_FILE (d);
4456 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
4457 variable is a static const initialized in the class body. */
4458 if (TREE_CODE (d) == VAR_DECL
4459 && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern))
4461 pushclass (DECL_CONTEXT (d), 2);
4462 DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args,
4463 TREE_VEC_LENGTH (args), tmpl);
4464 popclass (1);
4467 /* import_export_decl has to happen after DECL_INITIAL is set up. */
4468 if (pattern_defined)
4470 repo_template_used (d);
4472 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
4474 if (flag_alt_external_templates)
4476 if (interface_unknown)
4477 warn_if_unknown_interface (d);
4479 else if (DECL_INTERFACE_KNOWN (code_pattern))
4481 DECL_INTERFACE_KNOWN (d) = 1;
4482 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
4484 else
4485 warn_if_unknown_interface (code_pattern);
4488 if (at_eof)
4489 import_export_decl (d);
4492 if (! pattern_defined
4493 || (TREE_CODE (d) == FUNCTION_DECL && ! DECL_INLINE (d)
4494 && (! DECL_INTERFACE_KNOWN (d)
4495 || ! DECL_NOT_REALLY_EXTERN (d)))
4496 /* Kludge: if we compile a constructor in the middle of processing a
4497 toplevel declaration, we blow away the declspecs in
4498 temp_decl_obstack when we call permanent_allocation in
4499 finish_function. So don't compile it yet. */
4500 || (TREE_CODE (d) == FUNCTION_DECL && ! nested && ! at_eof))
4502 add_pending_template (d);
4503 goto out;
4506 lineno = DECL_SOURCE_LINE (d);
4507 input_filename = DECL_SOURCE_FILE (d);
4509 /* Trick tsubst into giving us a new decl in case the template changed. */
4510 save_ti = DECL_TEMPLATE_INFO (decl_pattern);
4511 DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE;
4512 td = tsubst (decl_pattern, args, TREE_VEC_LENGTH (args), tmpl);
4513 SET_DECL_IMPLICIT_INSTANTIATION (td);
4514 DECL_TEMPLATE_INFO (decl_pattern) = save_ti;
4516 /* And set up DECL_INITIAL, since tsubst doesn't. */
4517 if (TREE_CODE (td) == VAR_DECL)
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);
4525 if (TREE_CODE (d) == FUNCTION_DECL)
4527 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4528 new decl. */
4529 DECL_INITIAL (td) = error_mark_node;
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);
4535 duplicate_decls (td, d);
4536 if (TREE_CODE (d) == FUNCTION_DECL)
4537 DECL_INITIAL (td) = 0;
4539 if (TREE_CODE (d) == VAR_DECL)
4541 DECL_IN_AGGR_P (d) = 0;
4542 if (DECL_INTERFACE_KNOWN (d))
4543 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
4544 else
4546 DECL_EXTERNAL (d) = 1;
4547 DECL_NOT_REALLY_EXTERN (d) = 1;
4549 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
4551 else if (TREE_CODE (d) == FUNCTION_DECL)
4553 tree t = DECL_SAVED_TREE (code_pattern);
4555 start_function (NULL_TREE, d, NULL_TREE, 1);
4556 store_parm_decls ();
4558 if (t && TREE_CODE (t) == RETURN_INIT)
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);
4567 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
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);
4576 setup_vtbl_ptr ();
4577 /* Always keep the BLOCK node associated with the outermost
4578 pair of curly braces of a function. These are needed
4579 for correct operation of dwarfout.c. */
4580 keep_next_level ();
4582 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
4583 tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl);
4585 finish_function (lineno, 0, nested);
4588 out:
4589 lineno = line;
4590 input_filename = file;
4592 pop_from_top_level ();
4593 pop_tinst_level ();
4595 return d;
4598 tree
4599 tsubst_chain (t, argvec)
4600 tree t, argvec;
4602 if (t)
4604 tree first = tsubst (t, argvec,
4605 TREE_VEC_LENGTH (argvec), NULL_TREE);
4606 tree last = first;
4608 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
4610 tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE);
4611 TREE_CHAIN (last) = x;
4612 last = x;
4615 return first;
4617 return NULL_TREE;
4620 static tree
4621 tsubst_expr_values (t, argvec)
4622 tree t, argvec;
4624 tree first = NULL_TREE;
4625 tree *p = &first;
4627 for (; t; t = TREE_CHAIN (t))
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);
4636 return first;
4639 tree last_tree;
4641 void
4642 add_tree (t)
4643 tree t;
4645 last_tree = TREE_CHAIN (last_tree) = t;
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. */
4653 void
4654 add_maybe_template (d, fns)
4655 tree d, fns;
4657 tree t;
4659 if (DECL_MAYBE_TEMPLATE (d))
4660 return;
4662 t = most_specialized (fns, d);
4663 if (! t)
4664 return;
4665 if (t == error_mark_node)
4667 cp_error ("ambiguous template instantiation for `%D'", d);
4668 return;
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;
4676 /* Instantiate an enumerated type. Used by instantiate_class_template and
4677 tsubst_expr. */
4679 static tree
4680 tsubst_enum (tag, args, nargs, field_chain)
4681 tree tag, args;
4682 int nargs;
4683 tree * field_chain;
4685 extern tree current_local_enum;
4686 tree prev_local_enum = current_local_enum;
4688 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
4689 tree e, values = NULL_TREE;
4691 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
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;
4700 finish_enum (newtag, values);
4702 if (NULL != field_chain)
4703 *field_chain = grok_enum_decls (newtag, NULL_TREE);
4705 current_local_enum = prev_local_enum;
4707 return newtag;