Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / cp / pt.c
blob9f42d008ec655f6e88aba06bc4993bf25422240d
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-constant `%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 (nt) == TREE_VEC)
1162 /* For member templates */
1163 if (comp_template_args (nt, ot))
1164 continue;
1166 else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
1168 if (comptypes (ot, nt, 1))
1169 continue;
1171 else if (cp_tree_equal (ot, nt) > 0)
1172 continue;
1173 return 0;
1175 return 1;
1178 /* Given class template name and parameter list, produce a user-friendly name
1179 for the instantiation. */
1181 static char *
1182 mangle_class_name_for_template (name, parms, arglist)
1183 char *name;
1184 tree parms, arglist;
1186 static struct obstack scratch_obstack;
1187 static char *scratch_firstobj;
1188 int i, nparms;
1190 if (!scratch_firstobj)
1191 gcc_obstack_init (&scratch_obstack);
1192 else
1193 obstack_free (&scratch_obstack, scratch_firstobj);
1194 scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
1196 #if 0
1197 #define buflen sizeof(buf)
1198 #define check if (bufp >= buf+buflen-1) goto too_long
1199 #define ccat(c) *bufp++=(c); check
1200 #define advance bufp+=strlen(bufp); check
1201 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
1202 #else
1203 #define check
1204 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
1205 #define advance
1206 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
1207 #endif
1209 cat (name);
1210 ccat ('<');
1211 nparms = TREE_VEC_LENGTH (parms);
1212 my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
1213 for (i = 0; i < nparms; i++)
1215 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
1216 tree arg = TREE_VEC_ELT (arglist, i);
1218 if (i)
1219 ccat (',');
1221 if (TREE_CODE (parm) == TYPE_DECL)
1223 cat (type_as_string (arg, 0));
1224 continue;
1226 else
1227 my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
1229 if (TREE_CODE (arg) == TREE_LIST)
1231 /* New list cell was built because old chain link was in
1232 use. */
1233 my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
1234 arg = TREE_VALUE (arg);
1236 /* No need to check arglist against parmlist here; we did that
1237 in coerce_template_parms, called from lookup_template_class. */
1238 cat (expr_as_string (arg, 0));
1241 char *bufp = obstack_next_free (&scratch_obstack);
1242 int offset = 0;
1243 while (bufp[offset - 1] == ' ')
1244 offset--;
1245 obstack_blank_fast (&scratch_obstack, offset);
1247 /* B<C<char> >, not B<C<char>> */
1248 if (bufp[offset - 1] == '>')
1249 ccat (' ');
1251 ccat ('>');
1252 ccat ('\0');
1253 return (char *) obstack_base (&scratch_obstack);
1255 #if 0
1256 too_long:
1257 #endif
1258 fatal ("out of (preallocated) string space creating template instantiation name");
1259 /* NOTREACHED */
1260 return NULL;
1263 static tree
1264 classtype_mangled_name (t)
1265 tree t;
1267 if (CLASSTYPE_TEMPLATE_INFO (t)
1268 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
1270 tree name = DECL_NAME (CLASSTYPE_TI_TEMPLATE (t));
1271 char *mangled_name = mangle_class_name_for_template
1272 (IDENTIFIER_POINTER (name),
1273 DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t)),
1274 CLASSTYPE_TI_ARGS (t));
1275 tree id = get_identifier (mangled_name);
1276 IDENTIFIER_TEMPLATE (id) = name;
1277 return id;
1279 else
1280 return TYPE_IDENTIFIER (t);
1283 static void
1284 add_pending_template (d)
1285 tree d;
1287 tree ti;
1289 if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
1290 ti = CLASSTYPE_TEMPLATE_INFO (d);
1291 else
1292 ti = DECL_TEMPLATE_INFO (d);
1294 if (TI_PENDING_TEMPLATE_FLAG (ti))
1295 return;
1297 *template_tail = perm_tree_cons
1298 (current_function_decl, d, NULL_TREE);
1299 template_tail = &TREE_CHAIN (*template_tail);
1300 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
1304 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS (which
1305 may be either a _DECL or an overloaded function or an
1306 IDENTIFIER_NODE), and ARGLIST. */
1308 tree
1309 lookup_template_function (fns, arglist)
1310 tree fns, arglist;
1312 if (fns == NULL_TREE)
1314 cp_error ("non-template used as template");
1315 return error_mark_node;
1318 if (arglist != NULL_TREE && !TREE_PERMANENT (arglist))
1320 push_obstacks (&permanent_obstack, &permanent_obstack);
1321 arglist = copy_list (arglist);
1322 pop_obstacks ();
1325 return build_min (TEMPLATE_ID_EXPR,
1326 TREE_TYPE (fns)
1327 ? TREE_TYPE (fns) : unknown_type_node,
1328 fns, arglist);
1332 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
1333 parameters, find the desired type.
1335 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
1336 Since ARGLIST is build on the decl_obstack, we must copy it here
1337 to keep it from being reclaimed when the decl storage is reclaimed.
1339 IN_DECL, if non-NULL, is the template declaration we are trying to
1340 instantiate. */
1342 tree
1343 lookup_template_class (d1, arglist, in_decl)
1344 tree d1, arglist;
1345 tree in_decl;
1347 tree template, parmlist;
1348 char *mangled_name;
1349 tree id, t;
1351 if (TREE_CODE (d1) == IDENTIFIER_NODE)
1353 template = IDENTIFIER_GLOBAL_VALUE (d1); /* XXX */
1354 if (! template)
1355 template = IDENTIFIER_CLASS_VALUE (d1);
1357 else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
1359 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1));
1360 d1 = DECL_NAME (template);
1362 else if (TREE_CODE_CLASS (TREE_CODE (d1)) == 't' && IS_AGGR_TYPE (d1))
1364 template = CLASSTYPE_TI_TEMPLATE (d1);
1365 d1 = DECL_NAME (template);
1367 else
1368 my_friendly_abort (272);
1370 /* With something like `template <class T> class X class X { ... };'
1371 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
1372 We don't want to do that, but we have to deal with the situation, so
1373 let's give them some syntax errors to chew on instead of a crash. */
1374 if (! template)
1375 return error_mark_node;
1376 if (TREE_CODE (template) != TEMPLATE_DECL)
1378 cp_error ("non-template type `%T' used as a template", d1);
1379 if (in_decl)
1380 cp_error_at ("for template declaration `%D'", in_decl);
1381 return error_mark_node;
1384 if (PRIMARY_TEMPLATE_P (template))
1386 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
1388 arglist = coerce_template_parms (parmlist, arglist, template);
1389 if (arglist == error_mark_node)
1390 return error_mark_node;
1391 if (uses_template_parms (arglist))
1393 tree found;
1394 if (comp_template_args
1395 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist))
1396 found = TREE_TYPE (template);
1397 else
1399 for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
1400 found; found = TREE_CHAIN (found))
1402 if (TI_USES_TEMPLATE_PARMS (found)
1403 && comp_template_args (TREE_PURPOSE (found), arglist))
1404 break;
1406 if (found)
1407 found = TREE_VALUE (found);
1410 if (found)
1412 if (can_free (&permanent_obstack, arglist))
1413 obstack_free (&permanent_obstack, arglist);
1414 return found;
1418 mangled_name = mangle_class_name_for_template (IDENTIFIER_POINTER (d1),
1419 parmlist, arglist);
1420 id = get_identifier (mangled_name);
1421 IDENTIFIER_TEMPLATE (id) = d1;
1423 maybe_push_to_top_level (uses_template_parms (arglist));
1424 t = xref_tag_from_type (TREE_TYPE (template), id, 1);
1425 pop_from_top_level ();
1427 else
1429 tree ctx = lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
1430 arglist, in_decl);
1431 id = d1;
1432 arglist = CLASSTYPE_TI_ARGS (ctx);
1434 if (TYPE_BEING_DEFINED (ctx) && ctx == current_class_type)
1436 int save_temp = processing_template_decl;
1437 processing_template_decl = 0;
1438 t = xref_tag_from_type (TREE_TYPE (template), id, 0);
1439 processing_template_decl = save_temp;
1441 else
1443 t = lookup_nested_type_by_name (ctx, id);
1444 my_friendly_assert (t != NULL_TREE, 42);
1448 /* Seems to be wanted. */
1449 CLASSTYPE_GOT_SEMICOLON (t) = 1;
1451 if (! CLASSTYPE_TEMPLATE_INFO (t))
1453 arglist = copy_to_permanent (arglist);
1454 CLASSTYPE_TEMPLATE_INFO (t)
1455 = perm_tree_cons (template, arglist, NULL_TREE);
1456 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
1457 (arglist, t, DECL_TEMPLATE_INSTANTIATIONS (template));
1458 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
1459 = uses_template_parms (arglist);
1461 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
1463 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
1464 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t)) = id;
1465 /* if (! uses_template_parms (arglist)) */
1466 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t))
1467 = get_identifier (build_overload_name (t, 1, 1));
1469 if (flag_external_templates && ! uses_template_parms (arglist)
1470 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
1471 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
1472 add_pending_template (t);
1475 return t;
1478 /* Should be defined in parse.h. */
1479 extern int yychar;
1482 uses_template_parms (t)
1483 tree t;
1485 if (!t)
1486 return 0;
1487 switch (TREE_CODE (t))
1489 case INDIRECT_REF:
1490 case COMPONENT_REF:
1491 /* We assume that the object must be instantiated in order to build
1492 the COMPONENT_REF, so we test only whether the type of the
1493 COMPONENT_REF uses template parms. */
1494 return uses_template_parms (TREE_TYPE (t));
1496 case IDENTIFIER_NODE:
1497 if (!IDENTIFIER_TEMPLATE (t))
1498 return 0;
1499 my_friendly_abort (42);
1501 /* aggregates of tree nodes */
1502 case TREE_VEC:
1504 int i = TREE_VEC_LENGTH (t);
1505 while (i--)
1506 if (uses_template_parms (TREE_VEC_ELT (t, i)))
1507 return 1;
1508 return 0;
1510 case TREE_LIST:
1511 if (uses_template_parms (TREE_PURPOSE (t))
1512 || uses_template_parms (TREE_VALUE (t)))
1513 return 1;
1514 return uses_template_parms (TREE_CHAIN (t));
1516 /* constructed type nodes */
1517 case POINTER_TYPE:
1518 case REFERENCE_TYPE:
1519 return uses_template_parms (TREE_TYPE (t));
1520 case RECORD_TYPE:
1521 if (TYPE_PTRMEMFUNC_FLAG (t))
1522 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t));
1523 case UNION_TYPE:
1524 if (! CLASSTYPE_TEMPLATE_INFO (t))
1525 return 0;
1526 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t)));
1527 case FUNCTION_TYPE:
1528 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1529 return 1;
1530 return uses_template_parms (TREE_TYPE (t));
1531 case ARRAY_TYPE:
1532 if (uses_template_parms (TYPE_DOMAIN (t)))
1533 return 1;
1534 return uses_template_parms (TREE_TYPE (t));
1535 case OFFSET_TYPE:
1536 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t)))
1537 return 1;
1538 return uses_template_parms (TREE_TYPE (t));
1539 case METHOD_TYPE:
1540 if (uses_template_parms (TYPE_METHOD_BASETYPE (t)))
1541 return 1;
1542 if (uses_template_parms (TYPE_ARG_TYPES (t)))
1543 return 1;
1544 return uses_template_parms (TREE_TYPE (t));
1546 /* decl nodes */
1547 case TYPE_DECL:
1548 return uses_template_parms (TREE_TYPE (t));
1550 case FUNCTION_DECL:
1551 case VAR_DECL:
1552 /* ??? What about FIELD_DECLs? */
1553 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
1554 && uses_template_parms (DECL_TI_ARGS (t)))
1555 return 1;
1556 /* fall through */
1557 case CONST_DECL:
1558 case PARM_DECL:
1559 if (uses_template_parms (TREE_TYPE (t)))
1560 return 1;
1561 if (DECL_CONTEXT (t) && uses_template_parms (DECL_CONTEXT (t)))
1562 return 1;
1563 return 0;
1565 case CALL_EXPR:
1566 return uses_template_parms (TREE_TYPE (t));
1567 case ADDR_EXPR:
1568 return uses_template_parms (TREE_OPERAND (t, 0));
1570 /* template parm nodes */
1571 case TEMPLATE_TYPE_PARM:
1572 case TEMPLATE_CONST_PARM:
1573 return 1;
1575 /* simple type nodes */
1576 case INTEGER_TYPE:
1577 if (uses_template_parms (TYPE_MIN_VALUE (t)))
1578 return 1;
1579 return uses_template_parms (TYPE_MAX_VALUE (t));
1581 case REAL_TYPE:
1582 case COMPLEX_TYPE:
1583 case VOID_TYPE:
1584 case BOOLEAN_TYPE:
1585 return 0;
1587 case ENUMERAL_TYPE:
1589 tree v;
1591 for (v = TYPE_VALUES (t); v != NULL_TREE; v = TREE_CHAIN (v))
1592 if (uses_template_parms (TREE_VALUE (v)))
1593 return 1;
1595 return 0;
1597 /* constants */
1598 case INTEGER_CST:
1599 case REAL_CST:
1600 case STRING_CST:
1601 return 0;
1603 case ERROR_MARK:
1604 /* Non-error_mark_node ERROR_MARKs are bad things. */
1605 my_friendly_assert (t == error_mark_node, 274);
1606 /* NOTREACHED */
1607 return 0;
1609 case LOOKUP_EXPR:
1610 case TYPENAME_TYPE:
1611 return 1;
1613 case SCOPE_REF:
1614 return uses_template_parms (TREE_OPERAND (t, 0));
1616 case CONSTRUCTOR:
1617 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
1618 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
1619 return uses_template_parms (TREE_OPERAND (t, 1));
1621 case MODOP_EXPR:
1622 case CAST_EXPR:
1623 case REINTERPRET_CAST_EXPR:
1624 case CONST_CAST_EXPR:
1625 case STATIC_CAST_EXPR:
1626 case DYNAMIC_CAST_EXPR:
1627 case SIZEOF_EXPR:
1628 case ARROW_EXPR:
1629 case DOTSTAR_EXPR:
1630 case TYPEID_EXPR:
1631 return 1;
1633 default:
1634 switch (TREE_CODE_CLASS (TREE_CODE (t)))
1636 case '1':
1637 case '2':
1638 case 'e':
1639 case '<':
1641 int i;
1642 for (i = tree_code_length[(int) TREE_CODE (t)]; --i >= 0;)
1643 if (uses_template_parms (TREE_OPERAND (t, i)))
1644 return 1;
1645 return 0;
1647 default:
1648 break;
1650 sorry ("testing %s for template parms",
1651 tree_code_name [(int) TREE_CODE (t)]);
1652 my_friendly_abort (82);
1653 /* NOTREACHED */
1654 return 0;
1658 static struct tinst_level *current_tinst_level = 0;
1659 static struct tinst_level *free_tinst_level = 0;
1660 static int tinst_depth = 0;
1661 extern int max_tinst_depth;
1662 #ifdef GATHER_STATISTICS
1663 int depth_reached = 0;
1664 #endif
1666 static int
1667 push_tinst_level (d)
1668 tree d;
1670 struct tinst_level *new;
1672 if (tinst_depth >= max_tinst_depth)
1674 struct tinst_level *p = current_tinst_level;
1675 int line = lineno;
1676 char *file = input_filename;
1678 error ("template instantiation depth exceeds maximum of %d",
1679 max_tinst_depth);
1680 error (" (use -ftemplate-depth-NN to increase the maximum)");
1681 cp_error (" instantiating `%D'", d);
1683 for (; p; p = p->next)
1685 cp_error (" instantiated from `%D'", p->decl);
1686 lineno = p->line;
1687 input_filename = p->file;
1689 error (" instantiated from here");
1691 lineno = line;
1692 input_filename = file;
1694 return 0;
1697 if (free_tinst_level)
1699 new = free_tinst_level;
1700 free_tinst_level = new->next;
1702 else
1703 new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
1705 new->decl = d;
1706 new->line = lineno;
1707 new->file = input_filename;
1708 new->next = current_tinst_level;
1709 current_tinst_level = new;
1711 ++tinst_depth;
1712 #ifdef GATHER_STATISTICS
1713 if (tinst_depth > depth_reached)
1714 depth_reached = tinst_depth;
1715 #endif
1717 return 1;
1720 void
1721 pop_tinst_level ()
1723 struct tinst_level *old = current_tinst_level;
1725 current_tinst_level = old->next;
1726 old->next = free_tinst_level;
1727 free_tinst_level = old;
1728 --tinst_depth;
1731 struct tinst_level *
1732 tinst_for_decl ()
1734 struct tinst_level *p = current_tinst_level;
1736 if (p)
1737 for (; p->next ; p = p->next )
1739 return p;
1742 tree
1743 instantiate_class_template (type)
1744 tree type;
1746 tree template, template_info, args, pattern, t, *field_chain;
1748 if (type == error_mark_node)
1749 return error_mark_node;
1751 template_info = CLASSTYPE_TEMPLATE_INFO (type);
1753 if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
1754 return type;
1756 template = TI_TEMPLATE (template_info);
1757 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
1758 args = TI_ARGS (template_info);
1760 t = most_specialized_class
1761 (DECL_TEMPLATE_SPECIALIZATIONS (template), args);
1763 if (t == error_mark_node)
1765 char *str = "candidates are:";
1766 cp_error ("ambiguous class template instantiation for `%#T'", type);
1767 for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; t = TREE_CHAIN (t))
1769 if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args))
1771 cp_error_at ("%s %+#T", str, TREE_TYPE (t));
1772 str = " ";
1775 TYPE_BEING_DEFINED (type) = 1;
1776 return error_mark_node;
1778 else if (t)
1779 pattern = TREE_TYPE (t);
1780 else
1781 pattern = TREE_TYPE (template);
1783 if (TYPE_SIZE (pattern) == NULL_TREE)
1784 return type;
1786 if (t)
1787 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
1789 TYPE_BEING_DEFINED (type) = 1;
1791 if (! push_tinst_level (type))
1792 return type;
1794 maybe_push_to_top_level (uses_template_parms (type));
1795 pushclass (type, 0);
1797 if (flag_external_templates)
1799 if (flag_alt_external_templates)
1801 CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
1802 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
1803 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1804 = ! CLASSTYPE_INTERFACE_ONLY (type)
1805 && CLASSTYPE_INTERFACE_KNOWN (type);
1807 else
1809 CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
1810 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1811 (type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
1812 CLASSTYPE_VTABLE_NEEDS_WRITING (type)
1813 = ! CLASSTYPE_INTERFACE_ONLY (type)
1814 && CLASSTYPE_INTERFACE_KNOWN (type);
1817 else
1819 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
1820 CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
1823 TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
1824 TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
1825 TYPE_HAS_ASSIGNMENT (type) = TYPE_HAS_ASSIGNMENT (pattern);
1826 TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
1827 TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
1828 TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
1829 TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
1830 TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
1831 TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
1832 TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
1833 TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
1834 TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
1835 TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
1836 TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
1837 TYPE_GETS_INIT_AGGR (type) = TYPE_GETS_INIT_AGGR (pattern);
1838 TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
1839 TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
1840 TYPE_USES_COMPLEX_INHERITANCE (type)
1841 = TYPE_USES_COMPLEX_INHERITANCE (pattern);
1842 TYPE_USES_MULTIPLE_INHERITANCE (type)
1843 = TYPE_USES_MULTIPLE_INHERITANCE (pattern);
1844 TYPE_USES_VIRTUAL_BASECLASSES (type)
1845 = TYPE_USES_VIRTUAL_BASECLASSES (pattern);
1846 TYPE_PACKED (type) = TYPE_PACKED (pattern);
1847 TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
1850 tree binfo = TYPE_BINFO (type);
1851 tree pbases = TYPE_BINFO_BASETYPES (pattern);
1853 if (pbases)
1855 tree bases;
1856 int i;
1857 int len = TREE_VEC_LENGTH (pbases);
1858 bases = make_tree_vec (len);
1859 for (i = 0; i < len; ++i)
1861 tree elt;
1863 TREE_VEC_ELT (bases, i) = elt
1864 = tsubst (TREE_VEC_ELT (pbases, i), args,
1865 TREE_VEC_LENGTH (args), NULL_TREE);
1866 BINFO_INHERITANCE_CHAIN (elt) = binfo;
1868 if (! IS_AGGR_TYPE (TREE_TYPE (elt)))
1869 cp_error
1870 ("base type `%T' of `%T' fails to be a struct or class type",
1871 TREE_TYPE (elt), type);
1872 else if (! uses_template_parms (type)
1873 && (TYPE_SIZE (complete_type (TREE_TYPE (elt)))
1874 == NULL_TREE))
1875 cp_error ("base class `%T' of `%T' has incomplete type",
1876 TREE_TYPE (elt), type);
1878 /* Don't initialize this until the vector is filled out, or
1879 lookups will crash. */
1880 BINFO_BASETYPES (binfo) = bases;
1884 CLASSTYPE_LOCAL_TYPEDECLS (type) = CLASSTYPE_LOCAL_TYPEDECLS (pattern);
1886 field_chain = &TYPE_FIELDS (type);
1888 for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
1890 tree name = TREE_PURPOSE (t);
1891 tree tag = TREE_VALUE (t);
1893 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1894 if (TREE_CODE (tag) == ENUMERAL_TYPE)
1896 tree e, newtag = tsubst_enum (tag, args,
1897 TREE_VEC_LENGTH (args), field_chain);
1899 while (*field_chain)
1901 DECL_FIELD_CONTEXT (*field_chain) = type;
1902 field_chain = &TREE_CHAIN (*field_chain);
1905 else
1906 tsubst (tag, args,
1907 TREE_VEC_LENGTH (args), NULL_TREE);
1910 /* Don't replace enum constants here. */
1911 for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
1912 if (TREE_CODE (t) != CONST_DECL)
1914 tree r = tsubst (t, args,
1915 TREE_VEC_LENGTH (args), NULL_TREE);
1916 if (TREE_CODE (r) == VAR_DECL)
1918 if (! uses_template_parms (r))
1919 pending_statics = perm_tree_cons (NULL_TREE, r, pending_statics);
1920 /* Perhaps I should do more of grokfield here. */
1921 start_decl_1 (r);
1922 DECL_IN_AGGR_P (r) = 1;
1923 DECL_EXTERNAL (r) = 1;
1924 cp_finish_decl (r, DECL_INITIAL (r), NULL_TREE, 0, 0);
1927 *field_chain = r;
1928 field_chain = &TREE_CHAIN (r);
1931 TYPE_METHODS (type) = tsubst_chain (TYPE_METHODS (pattern), args);
1932 for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
1934 if (DECL_CONSTRUCTOR_P (t))
1935 grok_ctor_properties (type, t);
1936 else if (IDENTIFIER_OPNAME_P (DECL_NAME (t)))
1937 grok_op_properties (t, DECL_VIRTUAL_P (t), 0);
1940 DECL_FRIENDLIST (TYPE_MAIN_DECL (type))
1941 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern)),
1942 args, TREE_VEC_LENGTH (args), NULL_TREE);
1945 tree d = CLASSTYPE_FRIEND_CLASSES (type)
1946 = tsubst (CLASSTYPE_FRIEND_CLASSES (pattern), args,
1947 TREE_VEC_LENGTH (args), NULL_TREE);
1949 /* This does injection for friend classes. */
1950 for (; d; d = TREE_CHAIN (d))
1951 TREE_VALUE (d) = xref_tag_from_type (TREE_VALUE (d), NULL_TREE, 1);
1953 d = tsubst (DECL_TEMPLATE_INJECT (template), args,
1954 TREE_VEC_LENGTH (args), NULL_TREE);
1956 for (; d; d = TREE_CHAIN (d))
1958 tree t = TREE_VALUE (d);
1960 if (TREE_CODE (t) == TYPE_DECL)
1961 /* Already injected. */;
1962 else
1963 pushdecl (t);
1967 if (! uses_template_parms (type))
1969 tree tmp;
1970 for (tmp = TYPE_FIELDS (type); tmp; tmp = TREE_CHAIN (tmp))
1971 if (TREE_CODE (tmp) == FIELD_DECL)
1973 TREE_TYPE (tmp) = complete_type (TREE_TYPE (tmp));
1974 require_complete_type (tmp);
1977 type = finish_struct_1 (type, 0);
1978 CLASSTYPE_GOT_SEMICOLON (type) = 1;
1980 repo_template_used (type);
1981 if (at_eof && TYPE_BINFO_VTABLE (type) != NULL_TREE)
1982 finish_prevtable_vardecl (NULL, TYPE_BINFO_VTABLE (type));
1984 else
1986 TYPE_SIZE (type) = integer_zero_node;
1987 CLASSTYPE_METHOD_VEC (type)
1988 = finish_struct_methods (type, TYPE_METHODS (type), 1);
1991 TYPE_BEING_DEFINED (type) = 0;
1992 popclass (0);
1994 pop_from_top_level ();
1995 pop_tinst_level ();
1997 return type;
2000 static int
2001 list_eq (t1, t2)
2002 tree t1, t2;
2004 if (t1 == NULL_TREE)
2005 return t2 == NULL_TREE;
2006 if (t2 == NULL_TREE)
2007 return 0;
2008 /* Don't care if one declares its arg const and the other doesn't -- the
2009 main variant of the arg type is all that matters. */
2010 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
2011 != TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
2012 return 0;
2013 return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
2016 tree
2017 lookup_nested_type_by_name (ctype, name)
2018 tree ctype, name;
2020 tree t;
2022 complete_type (ctype);
2024 for (t = CLASSTYPE_TAGS (ctype); t; t = TREE_CHAIN (t))
2026 if (name == TREE_PURPOSE (t)
2027 /* this catches typedef enum { foo } bar; */
2028 || name == TYPE_IDENTIFIER (TREE_VALUE (t)))
2029 return TREE_VALUE (t);
2031 return NULL_TREE;
2034 /* If arg is a non-type template parameter that does not depend on template
2035 arguments, fold it like we weren't in the body of a template. */
2037 static tree
2038 maybe_fold_nontype_arg (arg)
2039 tree arg;
2041 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
2042 && !uses_template_parms (arg))
2044 /* Sometimes, one of the args was an expression involving a
2045 template constant parameter, like N - 1. Now that we've
2046 tsubst'd, we might have something like 2 - 1. This will
2047 confuse lookup_template_class, so we do constant folding
2048 here. We have to unset processing_template_decl, to
2049 fool build_expr_from_tree() into building an actual
2050 tree. */
2052 int saved_processing_template_decl = processing_template_decl;
2053 processing_template_decl = 0;
2054 arg = fold (build_expr_from_tree (arg));
2055 processing_template_decl = saved_processing_template_decl;
2057 return arg;
2060 /* Take the tree structure T and replace template parameters used therein
2061 with the argument vector ARGS. NARGS is the number of args; should
2062 be removed. IN_DECL is an associated decl for diagnostics.
2064 tsubst is used for dealing with types, decls and the like; for
2065 expressions, use tsubst_expr or tsubst_copy. */
2067 tree
2068 tsubst (t, args, nargs, in_decl)
2069 tree t, args;
2070 int nargs;
2071 tree in_decl;
2073 tree type;
2075 if (t == NULL_TREE || t == error_mark_node
2076 || t == integer_type_node
2077 || t == void_type_node
2078 || t == char_type_node)
2079 return t;
2081 type = TREE_TYPE (t);
2082 if (type == unknown_type_node)
2083 my_friendly_abort (42);
2084 if (type && TREE_CODE (t) != FUNCTION_DECL
2085 && TREE_CODE (t) != TYPENAME_TYPE)
2086 type = tsubst (type, args, nargs, in_decl);
2088 switch (TREE_CODE (t))
2090 case RECORD_TYPE:
2091 if (TYPE_PTRMEMFUNC_P (t))
2093 tree r = build_ptrmemfunc_type
2094 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, nargs, in_decl));
2095 return cp_build_type_variant (r, TYPE_READONLY (t),
2096 TYPE_VOLATILE (t));
2099 /* else fall through */
2100 case UNION_TYPE:
2101 if (uses_template_parms (t))
2103 tree argvec = tsubst (CLASSTYPE_TI_ARGS (t), args, nargs, in_decl);
2104 tree r = lookup_template_class (t, argvec, in_decl);
2105 return cp_build_type_variant (r, TYPE_READONLY (t),
2106 TYPE_VOLATILE (t));
2109 /* else fall through */
2110 case ERROR_MARK:
2111 case IDENTIFIER_NODE:
2112 case OP_IDENTIFIER:
2113 case VOID_TYPE:
2114 case REAL_TYPE:
2115 case COMPLEX_TYPE:
2116 case BOOLEAN_TYPE:
2117 case INTEGER_CST:
2118 case REAL_CST:
2119 case STRING_CST:
2120 return t;
2122 case ENUMERAL_TYPE:
2124 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2125 if (ctx == NULL_TREE)
2126 return t;
2127 else if (ctx == current_function_decl)
2128 return lookup_name (TYPE_IDENTIFIER (t), 1);
2129 else
2130 return lookup_nested_type_by_name (ctx, TYPE_IDENTIFIER (t));
2133 case INTEGER_TYPE:
2134 if (t == integer_type_node)
2135 return t;
2137 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
2138 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
2139 return t;
2142 tree max = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
2143 max = tsubst_expr (max, args, nargs, in_decl);
2144 if (processing_template_decl)
2146 tree itype = make_node (INTEGER_TYPE);
2147 TYPE_MIN_VALUE (itype) = size_zero_node;
2148 TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
2149 integer_one_node);
2150 return itype;
2153 max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node, 1));
2154 return build_index_2_type (size_zero_node, max);
2157 case TEMPLATE_TYPE_PARM:
2158 case TEMPLATE_CONST_PARM:
2160 int idx;
2161 int level;
2163 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2165 idx = TEMPLATE_TYPE_IDX (t);
2166 level = TEMPLATE_TYPE_LEVEL (t);
2168 else
2170 idx = TEMPLATE_CONST_IDX (t);
2171 level = TEMPLATE_CONST_LEVEL (t);
2174 if (TREE_VEC_LENGTH (args) > 0)
2176 tree arg = NULL_TREE;
2178 if (TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC)
2180 if (TREE_VEC_LENGTH (args) >= level - 1)
2181 arg = TREE_VEC_ELT
2182 (TREE_VEC_ELT (args, level - 1), idx);
2184 else if (level == 1)
2185 arg = TREE_VEC_ELT (args, idx);
2187 if (arg != NULL_TREE)
2189 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
2190 return cp_build_type_variant
2191 (arg, TYPE_READONLY (arg) || TYPE_READONLY (t),
2192 TYPE_VOLATILE (arg) || TYPE_VOLATILE (t));
2193 else
2194 return arg;
2198 /* If we get here, we must have been looking at a parm for a
2199 more deeply nested template. */
2200 my_friendly_assert((TREE_CODE (t) == TEMPLATE_CONST_PARM
2201 && TEMPLATE_CONST_LEVEL (t) > 1)
2202 || (TREE_CODE (t) == TEMPLATE_TYPE_PARM
2203 && TEMPLATE_TYPE_LEVEL (t) > 1),
2205 return t;
2208 case TEMPLATE_DECL:
2210 /* We can get here when processing a member template function
2211 of a template class. */
2212 tree tmpl;
2213 tree decl = DECL_TEMPLATE_RESULT (t);
2214 tree new_decl;
2215 tree parms;
2216 tree spec;
2217 int i;
2219 /* We might already have an instance of this template. */
2220 tree instances = DECL_TEMPLATE_INSTANTIATIONS (t);
2221 tree ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, in_decl);
2223 for (; instances; instances = TREE_CHAIN (instances))
2224 if (DECL_CLASS_CONTEXT (TREE_VALUE (instances)) == ctx)
2225 return TREE_VALUE (instances);
2227 /* Make a new template decl. It will be similar to the
2228 original, but will record the current template arguments.
2229 We also create a new function declaration, which is just
2230 like the old one, but points to this new template, rather
2231 than the old one. */
2232 tmpl = copy_node (t);
2233 copy_lang_decl (tmpl);
2234 my_friendly_assert (DECL_LANG_SPECIFIC (tmpl) != 0, 0);
2235 DECL_CHAIN (tmpl) = NULL_TREE;
2236 TREE_CHAIN (tmpl) = NULL_TREE;
2237 DECL_TEMPLATE_INFO (tmpl) = build_tree_list (t, args);
2238 new_decl = tsubst (decl, args, nargs, in_decl);
2239 DECL_RESULT (tmpl) = new_decl;
2240 DECL_TI_TEMPLATE (new_decl) = tmpl;
2241 TREE_TYPE (tmpl) = TREE_TYPE (new_decl);
2242 DECL_TEMPLATE_INSTANTIATIONS (tmpl) = NULL_TREE;
2243 SET_DECL_IMPLICIT_INSTANTIATION (tmpl);
2245 /* The template parameters for this new template are all the
2246 template parameters for the old template, except the
2247 outermost level of parameters. */
2248 DECL_TEMPLATE_PARMS (tmpl)
2249 = copy_node (DECL_TEMPLATE_PARMS (tmpl));
2250 for (parms = DECL_TEMPLATE_PARMS (tmpl);
2251 TREE_CHAIN (parms) != NULL_TREE;
2252 parms = TREE_CHAIN (parms))
2253 TREE_CHAIN (parms) = copy_node (TREE_CHAIN (parms));
2255 /* Record this partial instantiation. */
2256 DECL_TEMPLATE_INSTANTIATIONS (t)
2257 = perm_tree_cons (NULL_TREE, tmpl,
2258 DECL_TEMPLATE_INSTANTIATIONS (t));
2260 DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = NULL_TREE;
2261 return tmpl;
2264 case FUNCTION_DECL:
2266 tree r = NULL_TREE;
2267 tree ctx;
2269 int member;
2271 if (DECL_CONTEXT (t) != NULL_TREE
2272 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t))) == 't')
2274 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
2275 member = 2;
2276 else
2277 member = 1;
2278 ctx = tsubst (DECL_CLASS_CONTEXT (t), args, nargs, t);
2279 type = tsubst (type, args, nargs, in_decl);
2281 else
2283 member = 0;
2284 ctx = NULL_TREE;
2285 type = tsubst (type, args, nargs, in_decl);
2288 /* Do we already have this instantiation? */
2289 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2291 tree tmpl = DECL_TI_TEMPLATE (t);
2292 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2294 for (; decls; decls = TREE_CHAIN (decls))
2295 if (TREE_TYPE (TREE_VALUE (decls)) == type
2296 && DECL_CLASS_CONTEXT (TREE_VALUE (decls)) == ctx
2297 && comp_template_args (TREE_PURPOSE (decls), args))
2298 return TREE_VALUE (decls);
2301 /* We do NOT check for matching decls pushed separately at this
2302 point, as they may not represent instantiations of this
2303 template, and in any case are considered separate under the
2304 discrete model. Instead, see add_maybe_template. */
2306 r = copy_node (t);
2307 copy_lang_decl (r);
2308 TREE_TYPE (r) = type;
2310 DECL_CONTEXT (r)
2311 = tsubst (DECL_CONTEXT (t), args, nargs, t);
2312 DECL_CLASS_CONTEXT (r) = ctx;
2314 if (member && !strncmp (OPERATOR_TYPENAME_FORMAT,
2315 IDENTIFIER_POINTER (DECL_NAME (r)),
2316 sizeof (OPERATOR_TYPENAME_FORMAT) - 1))
2318 /* Type-conversion operator. Reconstruct the name, in
2319 case it's the name of one of the template's parameters. */
2320 DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
2323 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t)))
2325 char *buf, *dbuf = build_overload_name (ctx, 1, 1);
2326 int len = sizeof (DESTRUCTOR_DECL_PREFIX) - 1;
2327 buf = (char *) alloca (strlen (dbuf)
2328 + sizeof (DESTRUCTOR_DECL_PREFIX));
2329 bcopy (DESTRUCTOR_DECL_PREFIX, buf, len);
2330 buf[len] = '\0';
2331 strcat (buf, dbuf);
2332 DECL_ASSEMBLER_NAME (r) = get_identifier (buf);
2334 else
2336 /* Instantiations of template functions must be mangled
2337 specially, in order to conform to 14.5.5.1
2338 [temp.over.link]. We use in_decl below rather than
2339 DECL_TI_TEMPLATE (r) because the latter is set to
2340 NULL_TREE in instantiate_decl. */
2341 tree tmpl;
2342 tree arg_types;
2344 if (DECL_TEMPLATE_INFO (r))
2345 tmpl = DECL_TI_TEMPLATE (r);
2346 else
2347 tmpl = in_decl;
2349 /* tmpl will be NULL if this is a specialization of a
2350 member template of a template class. */
2351 if (name_mangling_version < 1
2352 || tmpl == NULL_TREE
2353 || (member && !is_member_template (tmpl)
2354 && !DECL_TEMPLATE_INFO (tmpl)))
2356 arg_types = TYPE_ARG_TYPES (type);
2357 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2358 arg_types = hash_tree_chain
2359 (build_pointer_type (DECL_CONTEXT (r)),
2360 arg_types);
2362 DECL_ASSEMBLER_NAME (r)
2363 = build_decl_overload (DECL_NAME (r), arg_types,
2364 member);
2366 else
2368 /* We pass the outermost template parameters to
2369 build_template_decl_overload since the innermost
2370 template parameters are still just template
2371 parameters; there are no corresponding substitution
2372 arguments. */
2373 /* FIXME The messed up thing here is that we get here with
2374 full args and only one level of parms. This is necessary
2375 because when we partially instantiate a member template,
2376 even though there's really only one level of parms left
2377 we re-use the parms from the original template, which
2378 have level 2. When this is fixed we can remove the
2379 add_to_template_args from instantiate_template. */
2380 tree tparms = DECL_TEMPLATE_PARMS (tmpl);
2382 while (tparms && TREE_CHAIN (tparms) != NULL_TREE)
2383 tparms = TREE_CHAIN (tparms);
2385 my_friendly_assert (tparms != NULL_TREE
2386 && TREE_CODE (tparms) == TREE_LIST,
2388 tparms = TREE_VALUE (tparms);
2390 arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl));
2391 if (member && TREE_CODE (type) == FUNCTION_TYPE)
2392 arg_types = hash_tree_chain
2393 (build_pointer_type (DECL_CONTEXT (r)),
2394 arg_types);
2396 DECL_ASSEMBLER_NAME (r)
2397 = build_template_decl_overload
2398 (DECL_NAME (r), arg_types,
2399 TREE_TYPE (TREE_TYPE (tmpl)),
2400 tparms,
2401 TREE_CODE (TREE_VEC_ELT (args, 0)) == TREE_VEC
2402 ? TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1) :
2403 args,
2404 member);
2407 DECL_RTL (r) = 0;
2408 make_decl_rtl (r, NULL_PTR, 1);
2410 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args, nargs, t);
2411 DECL_MAIN_VARIANT (r) = r;
2412 DECL_RESULT (r) = NULL_TREE;
2413 DECL_INITIAL (r) = NULL_TREE;
2415 TREE_STATIC (r) = 0;
2416 TREE_PUBLIC (r) = 1;
2417 DECL_EXTERNAL (r) = 1;
2418 DECL_INTERFACE_KNOWN (r) = 0;
2419 DECL_DEFER_OUTPUT (r) = 0;
2420 TREE_CHAIN (r) = NULL_TREE;
2421 DECL_CHAIN (r) = NULL_TREE;
2423 if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
2424 grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
2426 /* Look for matching decls for the moment. */
2427 if (! member && ! flag_ansi_overloading)
2429 tree decls = lookup_name_nonclass (DECL_NAME (t));
2430 tree d = NULL_TREE;
2432 if (decls == NULL_TREE)
2433 /* no match */;
2434 else if (is_overloaded_fn (decls))
2435 for (decls = get_first_fn (decls); decls;
2436 decls = DECL_CHAIN (decls))
2438 if (TREE_CODE (decls) == FUNCTION_DECL
2439 && TREE_TYPE (decls) == type)
2441 d = decls;
2442 break;
2446 if (d)
2448 int dcl_only = ! DECL_INITIAL (d);
2449 if (dcl_only)
2450 DECL_INITIAL (r) = error_mark_node;
2451 duplicate_decls (r, d);
2452 r = d;
2453 if (dcl_only)
2454 DECL_INITIAL (r) = 0;
2458 if (DECL_TEMPLATE_INFO (t) != NULL_TREE)
2460 tree tmpl = DECL_TI_TEMPLATE (t);
2461 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2462 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2464 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
2465 argvec = add_to_template_args (DECL_TI_ARGS (tmpl), argvec);
2467 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2468 *declsp = perm_tree_cons (argvec, r, *declsp);
2470 /* If we have a preexisting version of this function, don't expand
2471 the template version, use the other instead. */
2472 if (TREE_STATIC (r) || DECL_TEMPLATE_SPECIALIZATION (r))
2473 SET_DECL_TEMPLATE_SPECIALIZATION (r);
2474 else
2475 SET_DECL_IMPLICIT_INSTANTIATION (r);
2477 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
2478 = tree_cons (argvec, r, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
2481 /* Like grokfndecl. If we don't do this, pushdecl will mess up our
2482 TREE_CHAIN because it doesn't find a previous decl. Sigh. */
2483 if (member
2484 && IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) == NULL_TREE)
2485 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r)) = r;
2487 return r;
2490 case PARM_DECL:
2492 tree r = copy_node (t);
2493 TREE_TYPE (r) = type;
2494 DECL_INITIAL (r) = TREE_TYPE (r);
2495 DECL_CONTEXT (r) = NULL_TREE;
2496 #ifdef PROMOTE_PROTOTYPES
2497 if ((TREE_CODE (type) == INTEGER_TYPE
2498 || TREE_CODE (type) == ENUMERAL_TYPE)
2499 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
2500 DECL_ARG_TYPE (r) = integer_type_node;
2501 #endif
2502 if (TREE_CHAIN (t))
2503 TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args, nargs, TREE_CHAIN (t));
2504 return r;
2507 case FIELD_DECL:
2509 tree r = copy_node (t);
2510 TREE_TYPE (r) = type;
2511 copy_lang_decl (r);
2512 #if 0
2513 DECL_FIELD_CONTEXT (r) = tsubst (DECL_FIELD_CONTEXT (t), args, nargs, in_decl);
2514 #endif
2515 DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args, nargs, in_decl);
2516 TREE_CHAIN (r) = NULL_TREE;
2517 return r;
2520 case USING_DECL:
2522 tree r = copy_node (t);
2523 DECL_INITIAL (r)
2524 = tsubst_copy (DECL_INITIAL (t), args, nargs, in_decl);
2525 TREE_CHAIN (r) = NULL_TREE;
2526 return r;
2529 case VAR_DECL:
2531 tree r;
2532 tree ctx = tsubst_copy (DECL_CONTEXT (t), args, nargs, in_decl);
2534 /* Do we already have this instantiation? */
2535 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2537 tree tmpl = DECL_TI_TEMPLATE (t);
2538 tree decls = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2540 for (; decls; decls = TREE_CHAIN (decls))
2541 if (DECL_CONTEXT (TREE_VALUE (decls)) == ctx)
2542 return TREE_VALUE (decls);
2545 r = copy_node (t);
2546 TREE_TYPE (r) = type;
2547 DECL_CONTEXT (r) = ctx;
2548 if (TREE_STATIC (r))
2549 DECL_ASSEMBLER_NAME (r)
2550 = build_static_name (DECL_CONTEXT (r), DECL_NAME (r));
2552 /* Don't try to expand the initializer until someone tries to use
2553 this variable; otherwise we run into circular dependencies. */
2554 DECL_INITIAL (r) = NULL_TREE;
2556 DECL_RTL (r) = 0;
2557 DECL_SIZE (r) = 0;
2559 if (DECL_LANG_SPECIFIC (r))
2561 copy_lang_decl (r);
2562 DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
2565 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2567 tree tmpl = DECL_TI_TEMPLATE (t);
2568 tree *declsp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
2569 tree argvec = tsubst (DECL_TI_ARGS (t), args, nargs, in_decl);
2571 DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
2572 *declsp = perm_tree_cons (argvec, r, *declsp);
2573 SET_DECL_IMPLICIT_INSTANTIATION (r);
2575 TREE_CHAIN (r) = NULL_TREE;
2576 return r;
2579 case TYPE_DECL:
2580 if (t == TYPE_NAME (TREE_TYPE (t)))
2581 return TYPE_NAME (type);
2584 tree r = copy_node (t);
2585 TREE_TYPE (r) = type;
2586 DECL_CONTEXT (r) = current_class_type;
2587 TREE_CHAIN (r) = NULL_TREE;
2588 return r;
2591 case TREE_LIST:
2593 tree purpose, value, chain, result;
2594 int via_public, via_virtual, via_protected;
2596 if (t == void_list_node)
2597 return t;
2599 via_public = TREE_VIA_PUBLIC (t);
2600 via_protected = TREE_VIA_PROTECTED (t);
2601 via_virtual = TREE_VIA_VIRTUAL (t);
2603 purpose = TREE_PURPOSE (t);
2604 if (purpose)
2605 purpose = tsubst (purpose, args, nargs, in_decl);
2606 value = TREE_VALUE (t);
2607 if (value)
2608 value = tsubst (value, args, nargs, in_decl);
2609 chain = TREE_CHAIN (t);
2610 if (chain && chain != void_type_node)
2611 chain = tsubst (chain, args, nargs, in_decl);
2612 if (purpose == TREE_PURPOSE (t)
2613 && value == TREE_VALUE (t)
2614 && chain == TREE_CHAIN (t))
2615 return t;
2616 result = hash_tree_cons (via_public, via_virtual, via_protected,
2617 purpose, value, chain);
2618 TREE_PARMLIST (result) = TREE_PARMLIST (t);
2619 return result;
2621 case TREE_VEC:
2622 if (type != NULL_TREE)
2624 /* A binfo node. */
2626 t = copy_node (t);
2628 if (type == TREE_TYPE (t))
2629 return t;
2631 TREE_TYPE (t) = complete_type (type);
2632 if (IS_AGGR_TYPE (type))
2634 BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
2635 BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
2636 if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
2637 BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
2639 return t;
2642 /* Otherwise, a vector of template arguments. */
2644 int len = TREE_VEC_LENGTH (t), need_new = 0, i;
2645 tree *elts = (tree *) alloca (len * sizeof (tree));
2647 bzero ((char *) elts, len * sizeof (tree));
2649 for (i = 0; i < len; i++)
2651 elts[i] = maybe_fold_nontype_arg
2652 (tsubst_expr (TREE_VEC_ELT (t, i), args, nargs, in_decl));
2654 if (elts[i] != TREE_VEC_ELT (t, i))
2655 need_new = 1;
2658 if (!need_new)
2659 return t;
2661 t = make_tree_vec (len);
2662 for (i = 0; i < len; i++)
2663 TREE_VEC_ELT (t, i) = elts[i];
2665 return t;
2667 case POINTER_TYPE:
2668 case REFERENCE_TYPE:
2670 tree r;
2671 enum tree_code code;
2672 if (type == TREE_TYPE (t))
2673 return t;
2675 code = TREE_CODE (t);
2676 if (code == POINTER_TYPE)
2677 r = build_pointer_type (type);
2678 else
2679 r = build_reference_type (type);
2680 r = cp_build_type_variant (r, TYPE_READONLY (t), TYPE_VOLATILE (t));
2681 /* Will this ever be needed for TYPE_..._TO values? */
2682 layout_type (r);
2683 return r;
2685 case OFFSET_TYPE:
2686 return build_offset_type
2687 (tsubst (TYPE_OFFSET_BASETYPE (t), args, nargs, in_decl), type);
2688 case FUNCTION_TYPE:
2689 case METHOD_TYPE:
2691 tree values = TYPE_ARG_TYPES (t);
2692 tree context = TYPE_CONTEXT (t);
2693 tree raises = TYPE_RAISES_EXCEPTIONS (t);
2694 tree fntype;
2696 /* Don't bother recursing if we know it won't change anything. */
2697 if (values != void_list_node)
2699 /* This should probably be rewritten to use hash_tree_cons for
2700 the memory savings. */
2701 tree first = NULL_TREE;
2702 tree last;
2704 for (; values && values != void_list_node;
2705 values = TREE_CHAIN (values))
2707 tree value = TYPE_MAIN_VARIANT (type_decays_to
2708 (tsubst (TREE_VALUE (values), args, nargs, in_decl)));
2709 /* Don't instantiate default args unless they are used.
2710 Handle it in build_over_call instead. */
2711 tree purpose = TREE_PURPOSE (values);
2712 tree x = build_tree_list (purpose, value);
2714 if (first)
2715 TREE_CHAIN (last) = x;
2716 else
2717 first = x;
2718 last = x;
2721 if (values == void_list_node)
2722 TREE_CHAIN (last) = void_list_node;
2724 values = first;
2726 if (context)
2727 context = tsubst (context, args, nargs, in_decl);
2728 /* Could also optimize cases where return value and
2729 values have common elements (e.g., T min(const &T, const T&). */
2731 /* If the above parameters haven't changed, just return the type. */
2732 if (type == TREE_TYPE (t)
2733 && values == TYPE_VALUES (t)
2734 && context == TYPE_CONTEXT (t))
2735 return t;
2737 /* Construct a new type node and return it. */
2738 if (TREE_CODE (t) == FUNCTION_TYPE
2739 && context == NULL_TREE)
2741 fntype = build_function_type (type, values);
2743 else if (context == NULL_TREE)
2745 tree base = tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))),
2746 args, nargs, in_decl);
2747 fntype = build_cplus_method_type (base, type,
2748 TREE_CHAIN (values));
2750 else
2752 fntype = make_node (TREE_CODE (t));
2753 TREE_TYPE (fntype) = type;
2754 TYPE_CONTEXT (fntype) = context;
2755 TYPE_VALUES (fntype) = values;
2756 TYPE_SIZE (fntype) = TYPE_SIZE (t);
2757 TYPE_ALIGN (fntype) = TYPE_ALIGN (t);
2758 TYPE_MODE (fntype) = TYPE_MODE (t);
2759 if (TYPE_METHOD_BASETYPE (t))
2760 TYPE_METHOD_BASETYPE (fntype) = tsubst (TYPE_METHOD_BASETYPE (t),
2761 args, nargs, in_decl);
2762 /* Need to generate hash value. */
2763 my_friendly_abort (84);
2765 fntype = build_type_variant (fntype,
2766 TYPE_READONLY (t),
2767 TYPE_VOLATILE (t));
2768 if (raises)
2770 raises = tsubst (raises, args, nargs, in_decl);
2771 fntype = build_exception_variant (fntype, raises);
2773 return fntype;
2775 case ARRAY_TYPE:
2777 tree domain = tsubst (TYPE_DOMAIN (t), args, nargs, in_decl);
2778 tree r;
2779 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
2780 return t;
2781 r = build_cplus_array_type (type, domain);
2782 return r;
2785 case PLUS_EXPR:
2786 case MINUS_EXPR:
2787 return fold (build (TREE_CODE (t), TREE_TYPE (t),
2788 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2789 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl)));
2791 case NEGATE_EXPR:
2792 case NOP_EXPR:
2793 return fold (build1 (TREE_CODE (t), TREE_TYPE (t),
2794 tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl)));
2796 case TYPENAME_TYPE:
2798 tree ctx = tsubst (TYPE_CONTEXT (t), args, nargs, in_decl);
2799 tree f = make_typename_type (ctx, TYPE_IDENTIFIER (t));
2800 return cp_build_type_variant
2801 (f, TYPE_READONLY (f) || TYPE_READONLY (t),
2802 TYPE_VOLATILE (f) || TYPE_VOLATILE (t));
2805 case INDIRECT_REF:
2806 return make_pointer_declarator
2807 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2809 case ADDR_EXPR:
2810 return make_reference_declarator
2811 (type, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl));
2813 case ARRAY_REF:
2814 return build_parse_node
2815 (ARRAY_REF, tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2816 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
2818 case CALL_EXPR:
2819 return make_call_declarator
2820 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2821 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
2822 TREE_OPERAND (t, 2),
2823 tsubst (TREE_TYPE (t), args, nargs, in_decl));
2825 case SCOPE_REF:
2826 return build_parse_node
2827 (TREE_CODE (t), tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
2828 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl));
2830 default:
2831 sorry ("use of `%s' in template",
2832 tree_code_name [(int) TREE_CODE (t)]);
2833 return error_mark_node;
2837 void
2838 do_pushlevel ()
2840 emit_line_note (input_filename, lineno);
2841 pushlevel (0);
2842 clear_last_expr ();
2843 push_momentary ();
2844 expand_start_bindings (0);
2847 tree
2848 do_poplevel ()
2850 tree t;
2851 int saved_warn_unused;
2853 if (processing_template_decl)
2855 saved_warn_unused = warn_unused;
2856 warn_unused = 0;
2858 expand_end_bindings (getdecls (), kept_level_p (), 0);
2859 if (processing_template_decl)
2860 warn_unused = saved_warn_unused;
2861 t = poplevel (kept_level_p (), 1, 0);
2862 pop_momentary ();
2863 return t;
2866 /* Like tsubst, but deals with expressions. This function just replaces
2867 template parms; to finish processing the resultant expression, use
2868 tsubst_expr. */
2870 tree
2871 tsubst_copy (t, args, nargs, in_decl)
2872 tree t, args;
2873 int nargs;
2874 tree in_decl;
2876 enum tree_code code;
2878 if (t == NULL_TREE || t == error_mark_node)
2879 return t;
2881 code = TREE_CODE (t);
2883 switch (code)
2885 case PARM_DECL:
2886 return do_identifier (DECL_NAME (t), 0);
2888 case CONST_DECL:
2889 case FIELD_DECL:
2890 if (DECL_CONTEXT (t))
2892 tree ctx = tsubst (DECL_CONTEXT (t), args, nargs, in_decl);
2893 if (ctx == current_function_decl)
2894 return lookup_name (DECL_NAME (t), 0);
2895 else if (ctx != DECL_CONTEXT (t))
2896 return lookup_field (ctx, DECL_NAME (t), 0, 0);
2898 return t;
2900 case VAR_DECL:
2901 case FUNCTION_DECL:
2902 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
2903 t = tsubst (t, args, nargs, in_decl);
2904 mark_used (t);
2905 return t;
2907 case TEMPLATE_DECL:
2908 if (is_member_template (t))
2909 return tsubst (t, args, nargs, in_decl);
2910 else
2911 return t;
2913 #if 0
2914 case IDENTIFIER_NODE:
2915 return do_identifier (t, 0);
2916 #endif
2918 case CAST_EXPR:
2919 case REINTERPRET_CAST_EXPR:
2920 case CONST_CAST_EXPR:
2921 case STATIC_CAST_EXPR:
2922 case DYNAMIC_CAST_EXPR:
2923 return build1
2924 (code, tsubst (TREE_TYPE (t), args, nargs, in_decl),
2925 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2927 case INDIRECT_REF:
2928 case PREDECREMENT_EXPR:
2929 case PREINCREMENT_EXPR:
2930 case POSTDECREMENT_EXPR:
2931 case POSTINCREMENT_EXPR:
2932 case NEGATE_EXPR:
2933 case TRUTH_NOT_EXPR:
2934 case BIT_NOT_EXPR:
2935 case ADDR_EXPR:
2936 case CONVERT_EXPR: /* Unary + */
2937 case SIZEOF_EXPR:
2938 case ARROW_EXPR:
2939 case THROW_EXPR:
2940 case TYPEID_EXPR:
2941 return build1
2942 (code, NULL_TREE,
2943 tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl));
2945 case PLUS_EXPR:
2946 case MINUS_EXPR:
2947 case MULT_EXPR:
2948 case TRUNC_DIV_EXPR:
2949 case CEIL_DIV_EXPR:
2950 case FLOOR_DIV_EXPR:
2951 case ROUND_DIV_EXPR:
2952 case EXACT_DIV_EXPR:
2953 case BIT_AND_EXPR:
2954 case BIT_ANDTC_EXPR:
2955 case BIT_IOR_EXPR:
2956 case BIT_XOR_EXPR:
2957 case TRUNC_MOD_EXPR:
2958 case FLOOR_MOD_EXPR:
2959 case TRUTH_ANDIF_EXPR:
2960 case TRUTH_ORIF_EXPR:
2961 case TRUTH_AND_EXPR:
2962 case TRUTH_OR_EXPR:
2963 case RSHIFT_EXPR:
2964 case LSHIFT_EXPR:
2965 case RROTATE_EXPR:
2966 case LROTATE_EXPR:
2967 case EQ_EXPR:
2968 case NE_EXPR:
2969 case MAX_EXPR:
2970 case MIN_EXPR:
2971 case LE_EXPR:
2972 case GE_EXPR:
2973 case LT_EXPR:
2974 case GT_EXPR:
2975 case COMPONENT_REF:
2976 case ARRAY_REF:
2977 case COMPOUND_EXPR:
2978 case SCOPE_REF:
2979 case DOTSTAR_EXPR:
2980 case MEMBER_REF:
2981 return build_nt
2982 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
2983 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
2985 case CALL_EXPR:
2987 tree fn = TREE_OPERAND (t, 0);
2988 if (really_overloaded_fn (fn))
2989 fn = tsubst_copy (get_first_fn (fn), args, nargs, in_decl);
2990 else
2991 fn = tsubst_copy (fn, args, nargs, in_decl);
2992 return build_nt
2993 (code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
2994 NULL_TREE);
2997 case METHOD_CALL_EXPR:
2999 tree name = TREE_OPERAND (t, 0);
3000 if (TREE_CODE (name) == BIT_NOT_EXPR)
3002 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3003 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
3005 else if (TREE_CODE (name) == SCOPE_REF
3006 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
3008 tree base = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3009 name = TREE_OPERAND (name, 1);
3010 name = tsubst_copy (TREE_OPERAND (name, 0), args, nargs, in_decl);
3011 name = build1 (BIT_NOT_EXPR, NULL_TREE, TYPE_MAIN_VARIANT (name));
3012 name = build_nt (SCOPE_REF, base, name);
3014 else
3015 name = tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl);
3016 return build_nt
3017 (code, name, tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3018 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl),
3019 NULL_TREE);
3022 case COND_EXPR:
3023 case MODOP_EXPR:
3024 return build_nt
3025 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3026 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3027 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3029 case NEW_EXPR:
3031 tree r = build_nt
3032 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3033 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl),
3034 tsubst_copy (TREE_OPERAND (t, 2), args, nargs, in_decl));
3035 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
3036 return r;
3039 case DELETE_EXPR:
3041 tree r = build_nt
3042 (code, tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl),
3043 tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl));
3044 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
3045 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
3046 return r;
3049 case TEMPLATE_ID_EXPR:
3051 /* Substituted template arguments */
3052 tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, nargs, in_decl);
3053 tree chain;
3054 for (chain = targs; chain; chain = TREE_CHAIN (chain))
3055 TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
3057 return lookup_template_function
3058 (tsubst_copy (TREE_OPERAND (t, 0), args, nargs, in_decl), targs);
3061 case TREE_LIST:
3063 tree purpose, value, chain;
3065 if (t == void_list_node)
3066 return t;
3068 purpose = TREE_PURPOSE (t);
3069 if (purpose)
3070 purpose = tsubst_copy (purpose, args, nargs, in_decl);
3071 value = TREE_VALUE (t);
3072 if (value)
3073 value = tsubst_copy (value, args, nargs, in_decl);
3074 chain = TREE_CHAIN (t);
3075 if (chain && chain != void_type_node)
3076 chain = tsubst_copy (chain, args, nargs, in_decl);
3077 if (purpose == TREE_PURPOSE (t)
3078 && value == TREE_VALUE (t)
3079 && chain == TREE_CHAIN (t))
3080 return t;
3081 return tree_cons (purpose, value, chain);
3084 case RECORD_TYPE:
3085 case UNION_TYPE:
3086 case ENUMERAL_TYPE:
3087 case INTEGER_TYPE:
3088 case TEMPLATE_TYPE_PARM:
3089 case TEMPLATE_CONST_PARM:
3090 case POINTER_TYPE:
3091 case REFERENCE_TYPE:
3092 case OFFSET_TYPE:
3093 case FUNCTION_TYPE:
3094 case METHOD_TYPE:
3095 case ARRAY_TYPE:
3096 case TYPENAME_TYPE:
3097 return tsubst (t, args, nargs, in_decl);
3099 case IDENTIFIER_NODE:
3100 if (IDENTIFIER_TYPENAME_P (t))
3101 return build_typename_overload
3102 (tsubst (TREE_TYPE (t), args, nargs, in_decl));
3103 else
3104 return t;
3106 case CONSTRUCTOR:
3107 return build
3108 (CONSTRUCTOR, tsubst (TREE_TYPE (t), args, nargs, in_decl), NULL_TREE,
3109 tsubst_copy (CONSTRUCTOR_ELTS (t), args, nargs, in_decl));
3111 default:
3112 return t;
3116 /* Like tsubst_copy, but also does semantic processing and RTL expansion. */
3118 tree
3119 tsubst_expr (t, args, nargs, in_decl)
3120 tree t, args;
3121 int nargs;
3122 tree in_decl;
3124 if (t == NULL_TREE || t == error_mark_node)
3125 return t;
3127 if (processing_template_decl)
3128 return tsubst_copy (t, args, nargs, in_decl);
3130 switch (TREE_CODE (t))
3132 case RETURN_STMT:
3133 lineno = TREE_COMPLEXITY (t);
3134 emit_line_note (input_filename, lineno);
3135 c_expand_return
3136 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3137 finish_stmt ();
3138 break;
3140 case EXPR_STMT:
3141 lineno = TREE_COMPLEXITY (t);
3142 emit_line_note (input_filename, lineno);
3143 t = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3144 /* Do default conversion if safe and possibly important,
3145 in case within ({...}). */
3146 if ((TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE && lvalue_p (t))
3147 || TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
3148 t = default_conversion (t);
3149 cplus_expand_expr_stmt (t);
3150 clear_momentary ();
3151 finish_stmt ();
3152 break;
3154 case DECL_STMT:
3156 int i = suspend_momentary ();
3157 tree dcl, init;
3159 lineno = TREE_COMPLEXITY (t);
3160 emit_line_note (input_filename, lineno);
3161 dcl = start_decl
3162 (tsubst (TREE_OPERAND (t, 0), args, nargs, in_decl),
3163 tsubst (TREE_OPERAND (t, 1), args, nargs, in_decl),
3164 TREE_OPERAND (t, 2) != 0);
3165 init = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3166 cp_finish_decl
3167 (dcl, init, NULL_TREE, 1, /*init ? LOOKUP_ONLYCONVERTING :*/ 0);
3168 resume_momentary (i);
3169 return dcl;
3172 case FOR_STMT:
3174 tree tmp;
3175 int init_scope = (flag_new_for_scope > 0 && TREE_OPERAND (t, 0)
3176 && TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3177 int cond_scope = (TREE_OPERAND (t, 1)
3178 && TREE_CODE (TREE_OPERAND (t, 1)) == DECL_STMT);
3180 lineno = TREE_COMPLEXITY (t);
3181 emit_line_note (input_filename, lineno);
3182 if (init_scope)
3183 do_pushlevel ();
3184 for (tmp = TREE_OPERAND (t, 0); tmp; tmp = TREE_CHAIN (tmp))
3185 tsubst_expr (tmp, args, nargs, in_decl);
3186 emit_nop ();
3187 emit_line_note (input_filename, lineno);
3188 expand_start_loop_continue_elsewhere (1);
3190 if (cond_scope)
3191 do_pushlevel ();
3192 tmp = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3193 emit_line_note (input_filename, lineno);
3194 if (tmp)
3195 expand_exit_loop_if_false (0, condition_conversion (tmp));
3197 if (! cond_scope)
3198 do_pushlevel ();
3199 tsubst_expr (TREE_OPERAND (t, 3), args, nargs, in_decl);
3200 do_poplevel ();
3202 emit_line_note (input_filename, lineno);
3203 expand_loop_continue_here ();
3204 tmp = tsubst_expr (TREE_OPERAND (t, 2), args, nargs, in_decl);
3205 if (tmp)
3206 cplus_expand_expr_stmt (tmp);
3208 expand_end_loop ();
3209 if (init_scope)
3210 do_poplevel ();
3211 finish_stmt ();
3213 break;
3215 case WHILE_STMT:
3217 tree cond;
3219 lineno = TREE_COMPLEXITY (t);
3220 emit_nop ();
3221 emit_line_note (input_filename, lineno);
3222 expand_start_loop (1);
3224 cond = TREE_OPERAND (t, 0);
3225 if (TREE_CODE (cond) == DECL_STMT)
3226 do_pushlevel ();
3227 cond = tsubst_expr (cond, args, nargs, in_decl);
3228 emit_line_note (input_filename, lineno);
3229 expand_exit_loop_if_false (0, condition_conversion (cond));
3231 if (TREE_CODE (TREE_OPERAND (t, 0)) != DECL_STMT)
3232 do_pushlevel ();
3233 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3234 do_poplevel ();
3236 expand_end_loop ();
3237 finish_stmt ();
3239 break;
3241 case DO_STMT:
3243 tree cond;
3245 lineno = TREE_COMPLEXITY (t);
3246 emit_nop ();
3247 emit_line_note (input_filename, lineno);
3248 expand_start_loop_continue_elsewhere (1);
3250 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3251 expand_loop_continue_here ();
3253 cond = tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3254 emit_line_note (input_filename, lineno);
3255 expand_exit_loop_if_false (0, condition_conversion (cond));
3256 expand_end_loop ();
3258 clear_momentary ();
3259 finish_stmt ();
3261 break;
3263 case IF_STMT:
3265 tree tmp;
3266 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3268 lineno = TREE_COMPLEXITY (t);
3269 if (cond_scope)
3270 do_pushlevel ();
3271 tmp = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3272 emit_line_note (input_filename, lineno);
3273 expand_start_cond (condition_conversion (tmp), 0);
3275 if (tmp = TREE_OPERAND (t, 1), tmp)
3276 tsubst_expr (tmp, args, nargs, in_decl);
3278 if (tmp = TREE_OPERAND (t, 2), tmp)
3280 expand_start_else ();
3281 tsubst_expr (tmp, args, nargs, in_decl);
3284 expand_end_cond ();
3286 if (cond_scope)
3287 do_poplevel ();
3289 finish_stmt ();
3291 break;
3293 case COMPOUND_STMT:
3295 tree substmt = TREE_OPERAND (t, 0);
3297 lineno = TREE_COMPLEXITY (t);
3299 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3300 do_pushlevel ();
3302 for (; substmt; substmt = TREE_CHAIN (substmt))
3303 tsubst_expr (substmt, args, nargs, in_decl);
3305 if (COMPOUND_STMT_NO_SCOPE (t) == 0)
3306 do_poplevel ();
3308 break;
3310 case BREAK_STMT:
3311 lineno = TREE_COMPLEXITY (t);
3312 emit_line_note (input_filename, lineno);
3313 if (! expand_exit_something ())
3314 error ("break statement not within loop or switch");
3315 break;
3317 case CONTINUE_STMT:
3318 lineno = TREE_COMPLEXITY (t);
3319 emit_line_note (input_filename, lineno);
3320 if (! expand_continue_loop (0))
3321 error ("continue statement not within a loop");
3322 break;
3324 case SWITCH_STMT:
3326 tree val, tmp;
3327 int cond_scope = (TREE_CODE (TREE_OPERAND (t, 0)) == DECL_STMT);
3329 lineno = TREE_COMPLEXITY (t);
3330 if (cond_scope)
3331 do_pushlevel ();
3332 val = tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3333 emit_line_note (input_filename, lineno);
3334 c_expand_start_case (val);
3335 push_switch ();
3337 if (tmp = TREE_OPERAND (t, 1), tmp)
3338 tsubst_expr (tmp, args, nargs, in_decl);
3340 expand_end_case (val);
3341 pop_switch ();
3343 if (cond_scope)
3344 do_poplevel ();
3346 finish_stmt ();
3348 break;
3350 case CASE_LABEL:
3351 do_case (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl),
3352 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl));
3353 break;
3355 case LABEL_DECL:
3356 t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
3357 DECL_NAME (t));
3358 if (t)
3359 expand_label (t);
3360 break;
3362 case GOTO_STMT:
3363 lineno = TREE_COMPLEXITY (t);
3364 emit_line_note (input_filename, lineno);
3365 if (TREE_CODE (TREE_OPERAND (t, 0)) == IDENTIFIER_NODE)
3367 tree decl = lookup_label (TREE_OPERAND (t, 0));
3368 TREE_USED (decl) = 1;
3369 expand_goto (decl);
3371 else
3372 expand_computed_goto
3373 (tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl));
3374 break;
3376 case TRY_BLOCK:
3377 lineno = TREE_COMPLEXITY (t);
3378 emit_line_note (input_filename, lineno);
3379 expand_start_try_stmts ();
3380 tsubst_expr (TREE_OPERAND (t, 0), args, nargs, in_decl);
3381 expand_start_all_catch ();
3383 tree handler = TREE_OPERAND (t, 1);
3384 for (; handler; handler = TREE_CHAIN (handler))
3385 tsubst_expr (handler, args, nargs, in_decl);
3387 expand_end_all_catch ();
3388 break;
3390 case HANDLER:
3391 lineno = TREE_COMPLEXITY (t);
3392 do_pushlevel ();
3393 if (TREE_OPERAND (t, 0))
3395 tree d = TREE_OPERAND (t, 0);
3396 expand_start_catch_block
3397 (tsubst (TREE_OPERAND (d, 1), args, nargs, in_decl),
3398 tsubst (TREE_OPERAND (d, 0), args, nargs, in_decl));
3400 else
3401 expand_start_catch_block (NULL_TREE, NULL_TREE);
3402 tsubst_expr (TREE_OPERAND (t, 1), args, nargs, in_decl);
3403 expand_end_catch_block ();
3404 do_poplevel ();
3405 break;
3407 case TAG_DEFN:
3408 lineno = TREE_COMPLEXITY (t);
3409 t = TREE_TYPE (t);
3410 if (TREE_CODE (t) == ENUMERAL_TYPE)
3411 tsubst_enum (t, args, nargs, NULL);
3412 break;
3414 default:
3415 return build_expr_from_tree (tsubst_copy (t, args, nargs, in_decl));
3417 return NULL_TREE;
3420 tree
3421 instantiate_template (tmpl, targ_ptr)
3422 tree tmpl, targ_ptr;
3424 tree fndecl;
3425 int i, len;
3426 struct obstack *old_fmp_obstack;
3427 extern struct obstack *function_maybepermanent_obstack;
3429 my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
3431 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3433 tree specs;
3435 /* Check to see if there is a matching specialization. */
3436 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3437 specs != NULL_TREE;
3438 specs = TREE_CHAIN (specs))
3439 if (comp_template_args (TREE_PURPOSE (specs), targ_ptr))
3440 return TREE_VALUE (specs);
3443 push_obstacks (&permanent_obstack, &permanent_obstack);
3444 old_fmp_obstack = function_maybepermanent_obstack;
3445 function_maybepermanent_obstack = &permanent_obstack;
3447 len = DECL_NTPARMS (tmpl);
3449 i = len;
3450 while (i--)
3452 tree t = TREE_VEC_ELT (targ_ptr, i);
3453 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3455 tree nt = target_type (t);
3456 if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
3458 cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
3459 cp_error (" trying to instantiate `%D'", tmpl);
3460 fndecl = error_mark_node;
3461 goto out;
3464 TREE_VEC_ELT (targ_ptr, i) = copy_to_permanent (t);
3466 targ_ptr = copy_to_permanent (targ_ptr);
3468 if (DECL_TEMPLATE_INFO (tmpl) && DECL_TI_ARGS (tmpl))
3469 targ_ptr = add_to_template_args (DECL_TI_ARGS (tmpl), targ_ptr);
3471 /* substitute template parameters */
3472 fndecl = tsubst (DECL_RESULT (tmpl), targ_ptr, len, tmpl);
3474 if (flag_external_templates)
3475 add_pending_template (fndecl);
3477 out:
3478 function_maybepermanent_obstack = old_fmp_obstack;
3479 pop_obstacks ();
3481 return fndecl;
3484 /* Push the name of the class template into the scope of the instantiation. */
3486 void
3487 overload_template_name (type)
3488 tree type;
3490 tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
3491 tree decl;
3493 if (IDENTIFIER_CLASS_VALUE (id)
3494 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
3495 return;
3497 decl = build_decl (TYPE_DECL, id, type);
3498 SET_DECL_ARTIFICIAL (decl);
3499 pushdecl_class_level (decl);
3502 /* Like type_unification but designed specially to handle conversion
3503 operators. */
3506 fn_type_unification (fn, explicit_targs, targs, args, return_type, strict)
3507 tree fn, explicit_targs, targs, args, return_type;
3508 int strict;
3510 int i, dummy = 0;
3511 tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
3512 tree decl_arg_types = args;
3514 my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
3516 if (IDENTIFIER_TYPENAME_P (DECL_NAME (fn)))
3518 /* This is a template conversion operator. Use the return types
3519 as well as the argument types. */
3520 fn_arg_types = scratch_tree_cons (NULL_TREE,
3521 TREE_TYPE (TREE_TYPE (fn)),
3522 fn_arg_types);
3523 decl_arg_types = scratch_tree_cons (NULL_TREE,
3524 return_type,
3525 decl_arg_types);
3528 i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (fn),
3529 &TREE_VEC_ELT (targs, 0),
3530 fn_arg_types,
3531 decl_arg_types,
3532 explicit_targs,
3533 &dummy, strict, 0);
3535 return i;
3539 /* Type unification.
3541 We have a function template signature with one or more references to
3542 template parameters, and a parameter list we wish to fit to this
3543 template. If possible, produce a list of parameters for the template
3544 which will cause it to fit the supplied parameter list.
3546 Return zero for success, 2 for an incomplete match that doesn't resolve
3547 all the types, and 1 for complete failure. An error message will be
3548 printed only for an incomplete match.
3550 TPARMS[NTPARMS] is an array of template parameter types;
3551 TARGS[NTPARMS] is the array of template parameter values. PARMS is
3552 the function template's signature (using TEMPLATE_PARM_IDX nodes),
3553 and ARGS is the argument list we're trying to match against it.
3555 If SUBR is 1, we're being called recursively (to unify the arguments of
3556 a function or method parameter of a function template), so don't zero
3557 out targs and don't fail on an incomplete match.
3559 If STRICT is 1, the match must be exact (for casts of overloaded
3560 addresses, explicit instantiation, and more_specialized). */
3563 type_unification (tparms, targs, parms, args, targs_in, nsubsts,
3564 strict, allow_incomplete)
3565 tree tparms, *targs, parms, args, targs_in;
3566 int *nsubsts, strict, allow_incomplete;
3568 int ntparms = TREE_VEC_LENGTH (tparms);
3569 tree t;
3570 int i;
3571 int r;
3573 bzero ((char *) targs, sizeof (tree) * ntparms);
3575 /* Insert any explicit template arguments. They are encoded as the
3576 operands of NOP_EXPRs so that unify can tell that they are
3577 explicit arguments. */
3578 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3579 targs[i] = build1 (NOP_EXPR, NULL_TREE, TREE_VALUE (t));
3581 r = type_unification_real (tparms, targs, parms, args, nsubsts, 0,
3582 strict, allow_incomplete);
3584 for (i = 0, t = targs_in; t != NULL_TREE; t = TREE_CHAIN (t), ++i)
3585 if (TREE_CODE (targs[i]) == NOP_EXPR)
3586 targs[i] = TREE_OPERAND (targs[i], 0);
3588 return r;
3592 static int
3593 type_unification_real (tparms, targs, parms, args, nsubsts, subr,
3594 strict, allow_incomplete)
3595 tree tparms, *targs, parms, args;
3596 int *nsubsts, subr, strict, allow_incomplete;
3598 tree parm, arg;
3599 int i;
3600 int ntparms = TREE_VEC_LENGTH (tparms);
3602 my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
3603 my_friendly_assert (parms == NULL_TREE
3604 || TREE_CODE (parms) == TREE_LIST, 290);
3605 /* ARGS could be NULL (via a call from parse.y to
3606 build_x_function_call). */
3607 if (args)
3608 my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
3609 my_friendly_assert (ntparms > 0, 292);
3611 while (parms
3612 && parms != void_list_node
3613 && args
3614 && args != void_list_node)
3616 parm = TREE_VALUE (parms);
3617 parms = TREE_CHAIN (parms);
3618 arg = TREE_VALUE (args);
3619 args = TREE_CHAIN (args);
3621 if (arg == error_mark_node)
3622 return 1;
3623 if (arg == unknown_type_node)
3624 return 1;
3626 /* Conversions will be performed on a function argument that
3627 corresponds with a function parameter that contains only
3628 non-deducible template parameters and explicitly specified
3629 template parameters. */
3630 if (! uses_template_parms (parm))
3632 tree type;
3634 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3635 type = TREE_TYPE (arg);
3636 else
3638 type = arg;
3639 arg = NULL_TREE;
3642 if (strict)
3644 if (comptypes (parm, type, 1))
3645 continue;
3647 else if (arg)
3649 if (can_convert_arg (parm, type, arg))
3650 continue;
3652 else
3654 if (can_convert (parm, type))
3655 continue;
3658 return 1;
3661 #if 0
3662 if (TREE_CODE (arg) == VAR_DECL)
3663 arg = TREE_TYPE (arg);
3664 else if (TREE_CODE_CLASS (TREE_CODE (arg)) == 'e')
3665 arg = TREE_TYPE (arg);
3666 #else
3667 if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
3669 my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
3670 if (TREE_CODE (arg) == TREE_LIST
3671 && TREE_TYPE (arg) == unknown_type_node
3672 && TREE_CODE (TREE_VALUE (arg)) == TEMPLATE_DECL)
3674 int nsubsts, ntparms;
3675 tree *targs;
3677 /* Have to back unify here */
3678 arg = TREE_VALUE (arg);
3679 nsubsts = 0;
3680 ntparms = DECL_NTPARMS (arg);
3681 targs = (tree *) alloca (sizeof (tree) * ntparms);
3682 parm = expr_tree_cons (NULL_TREE, parm, NULL_TREE);
3683 return
3684 type_unification (DECL_INNERMOST_TEMPLATE_PARMS (arg),
3685 targs,
3686 TYPE_ARG_TYPES (TREE_TYPE (arg)),
3687 parm, NULL_TREE, &nsubsts, strict,
3688 allow_incomplete);
3690 arg = TREE_TYPE (arg);
3692 #endif
3693 if (! subr && TREE_CODE (arg) == REFERENCE_TYPE)
3694 arg = TREE_TYPE (arg);
3696 if (! subr && TREE_CODE (parm) != REFERENCE_TYPE)
3698 if (TREE_CODE (arg) == FUNCTION_TYPE
3699 || TREE_CODE (arg) == METHOD_TYPE)
3700 arg = build_pointer_type (arg);
3701 else if (TREE_CODE (arg) == ARRAY_TYPE)
3702 arg = build_pointer_type (TREE_TYPE (arg));
3703 else
3704 arg = TYPE_MAIN_VARIANT (arg);
3707 switch (unify (tparms, targs, ntparms, parm, arg, nsubsts, strict))
3709 case 0:
3710 break;
3711 case 1:
3712 return 1;
3715 /* Fail if we've reached the end of the parm list, and more args
3716 are present, and the parm list isn't variadic. */
3717 if (args && args != void_list_node && parms == void_list_node)
3718 return 1;
3719 /* Fail if parms are left and they don't have default values. */
3720 if (parms
3721 && parms != void_list_node
3722 && TREE_PURPOSE (parms) == NULL_TREE)
3723 return 1;
3724 if (!subr)
3725 for (i = 0; i < ntparms; i++)
3726 if (!targs[i])
3728 if (!allow_incomplete)
3729 error ("incomplete type unification");
3730 return 2;
3732 return 0;
3735 /* Tail recursion is your friend. */
3737 static int
3738 unify (tparms, targs, ntparms, parm, arg, nsubsts, strict)
3739 tree tparms, *targs, parm, arg;
3740 int *nsubsts, ntparms, strict;
3742 int idx;
3744 /* I don't think this will do the right thing with respect to types.
3745 But the only case I've seen it in so far has been array bounds, where
3746 signedness is the only information lost, and I think that will be
3747 okay. */
3748 while (TREE_CODE (parm) == NOP_EXPR)
3749 parm = TREE_OPERAND (parm, 0);
3751 if (arg == error_mark_node)
3752 return 1;
3753 if (arg == unknown_type_node)
3754 return 1;
3755 if (arg == parm)
3756 return 0;
3758 switch (TREE_CODE (parm))
3760 case TYPENAME_TYPE:
3761 /* In a type which contains a nested-name-specifier, template
3762 argument values cannot be deduced for template parameters used
3763 within the nested-name-specifier. */
3764 return 0;
3766 case TEMPLATE_TYPE_PARM:
3767 (*nsubsts)++;
3768 idx = TEMPLATE_TYPE_IDX (parm);
3769 /* Check for mixed types and values. */
3770 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms, idx))) != TYPE_DECL)
3771 return 1;
3773 if (!strict && targs[idx] != NULL_TREE &&
3774 TREE_CODE (targs[idx]) == NOP_EXPR)
3775 /* An explicit template argument. Don't even try to match
3776 here; the overload resolution code will manage check to
3777 see whether the call is legal. */
3778 return 0;
3780 if (strict && (TYPE_READONLY (arg) < TYPE_READONLY (parm)
3781 || TYPE_VOLATILE (arg) < TYPE_VOLATILE (parm)))
3782 return 1;
3783 #if 0
3784 /* Template type parameters cannot contain cv-quals; i.e.
3785 template <class T> void f (T& a, T& b) will not generate
3786 void f (const int& a, const int& b). */
3787 if (TYPE_READONLY (arg) > TYPE_READONLY (parm)
3788 || TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm))
3789 return 1;
3790 arg = TYPE_MAIN_VARIANT (arg);
3791 #else
3793 int constp = TYPE_READONLY (arg) > TYPE_READONLY (parm);
3794 int volatilep = TYPE_VOLATILE (arg) > TYPE_VOLATILE (parm);
3795 arg = cp_build_type_variant (arg, constp, volatilep);
3797 #endif
3798 /* Simple cases: Value already set, does match or doesn't. */
3799 if (targs[idx] == arg
3800 || (targs[idx]
3801 && TREE_CODE (targs[idx]) == NOP_EXPR
3802 && TREE_OPERAND (targs[idx], 0) == arg))
3803 return 0;
3804 else if (targs[idx])
3805 return 1;
3806 targs[idx] = arg;
3807 return 0;
3808 case TEMPLATE_CONST_PARM:
3809 (*nsubsts)++;
3810 idx = TEMPLATE_CONST_IDX (parm);
3811 if (targs[idx])
3813 int i = cp_tree_equal (targs[idx], arg);
3814 if (i == 1)
3815 return 0;
3816 else if (i == 0)
3817 return 1;
3818 else
3819 my_friendly_abort (42);
3822 targs[idx] = copy_to_permanent (arg);
3823 return 0;
3825 case POINTER_TYPE:
3826 if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
3827 return unify (tparms, targs, ntparms, parm,
3828 TYPE_PTRMEMFUNC_FN_TYPE (arg), nsubsts, strict);
3830 if (TREE_CODE (arg) != POINTER_TYPE)
3831 return 1;
3832 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3833 nsubsts, strict);
3835 case REFERENCE_TYPE:
3836 if (TREE_CODE (arg) == REFERENCE_TYPE)
3837 arg = TREE_TYPE (arg);
3838 return unify (tparms, targs, ntparms, TREE_TYPE (parm), arg,
3839 nsubsts, strict);
3841 case ARRAY_TYPE:
3842 if (TREE_CODE (arg) != ARRAY_TYPE)
3843 return 1;
3844 if (unify (tparms, targs, ntparms, TYPE_DOMAIN (parm), TYPE_DOMAIN (arg),
3845 nsubsts, strict) != 0)
3846 return 1;
3847 return unify (tparms, targs, ntparms, TREE_TYPE (parm), TREE_TYPE (arg),
3848 nsubsts, strict);
3850 case REAL_TYPE:
3851 case COMPLEX_TYPE:
3852 case INTEGER_TYPE:
3853 case BOOLEAN_TYPE:
3854 case VOID_TYPE:
3855 if (TREE_CODE (arg) != TREE_CODE (parm))
3856 return 1;
3858 if (TREE_CODE (parm) == INTEGER_TYPE)
3860 if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
3861 && unify (tparms, targs, ntparms, TYPE_MIN_VALUE (parm),
3862 TYPE_MIN_VALUE (arg), nsubsts, strict))
3863 return 1;
3864 if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
3865 && unify (tparms, targs, ntparms, TYPE_MAX_VALUE (parm),
3866 TYPE_MAX_VALUE (arg), nsubsts, strict))
3867 return 1;
3869 else if (TREE_CODE (parm) == REAL_TYPE
3870 && TYPE_MAIN_VARIANT (arg) != TYPE_MAIN_VARIANT (parm))
3871 return 1;
3873 /* As far as unification is concerned, this wins. Later checks
3874 will invalidate it if necessary. */
3875 return 0;
3877 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
3878 /* Type INTEGER_CST can come from ordinary constant template args. */
3879 case INTEGER_CST:
3880 while (TREE_CODE (arg) == NOP_EXPR)
3881 arg = TREE_OPERAND (arg, 0);
3883 if (TREE_CODE (arg) != INTEGER_CST)
3884 return 1;
3885 return !tree_int_cst_equal (parm, arg);
3887 case MINUS_EXPR:
3889 tree t1, t2;
3890 t1 = TREE_OPERAND (parm, 0);
3891 t2 = TREE_OPERAND (parm, 1);
3892 return unify (tparms, targs, ntparms, t1,
3893 fold (build (PLUS_EXPR, integer_type_node, arg, t2)),
3894 nsubsts, strict);
3897 case TREE_VEC:
3899 int i;
3900 if (TREE_CODE (arg) != TREE_VEC)
3901 return 1;
3902 if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
3903 return 1;
3904 for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
3905 if (unify (tparms, targs, ntparms,
3906 TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
3907 nsubsts, strict))
3908 return 1;
3909 return 0;
3912 case RECORD_TYPE:
3913 if (TYPE_PTRMEMFUNC_FLAG (parm))
3914 return unify (tparms, targs, ntparms, TYPE_PTRMEMFUNC_FN_TYPE (parm),
3915 arg, nsubsts, strict);
3917 /* Allow trivial conversions. */
3918 if (TREE_CODE (arg) != RECORD_TYPE
3919 || TYPE_READONLY (parm) < TYPE_READONLY (arg)
3920 || TYPE_VOLATILE (parm) < TYPE_VOLATILE (arg))
3921 return 1;
3923 if (CLASSTYPE_TEMPLATE_INFO (parm) && uses_template_parms (parm))
3925 tree t = NULL_TREE;
3926 if (flag_ansi_overloading && ! strict)
3927 t = get_template_base (CLASSTYPE_TI_TEMPLATE (parm), arg);
3928 else if
3929 (CLASSTYPE_TEMPLATE_INFO (arg)
3930 && CLASSTYPE_TI_TEMPLATE (parm) == CLASSTYPE_TI_TEMPLATE (arg))
3931 t = arg;
3932 if (! t || t == error_mark_node)
3933 return 1;
3935 return unify (tparms, targs, ntparms, CLASSTYPE_TI_ARGS (parm),
3936 CLASSTYPE_TI_ARGS (t), nsubsts, strict);
3938 else if (TYPE_MAIN_VARIANT (parm) != TYPE_MAIN_VARIANT (arg))
3939 return 1;
3940 return 0;
3942 case METHOD_TYPE:
3943 if (TREE_CODE (arg) != METHOD_TYPE)
3944 return 1;
3945 goto check_args;
3947 case FUNCTION_TYPE:
3948 if (TREE_CODE (arg) != FUNCTION_TYPE)
3949 return 1;
3950 check_args:
3951 if (unify (tparms, targs, ntparms, TREE_TYPE (parm),
3952 TREE_TYPE (arg), nsubsts, strict))
3953 return 1;
3954 return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
3955 TYPE_ARG_TYPES (arg), nsubsts, 1,
3956 strict, 0);
3958 case OFFSET_TYPE:
3959 if (TREE_CODE (arg) != OFFSET_TYPE)
3960 return 1;
3961 if (unify (tparms, targs, ntparms, TYPE_OFFSET_BASETYPE (parm),
3962 TYPE_OFFSET_BASETYPE (arg), nsubsts, strict))
3963 return 1;
3964 return unify (tparms, targs, ntparms, TREE_TYPE (parm),
3965 TREE_TYPE (arg), nsubsts, strict);
3967 case CONST_DECL:
3968 if (arg != decl_constant_value (parm))
3969 return 1;
3970 return 0;
3972 default:
3973 sorry ("use of `%s' in template type unification",
3974 tree_code_name [(int) TREE_CODE (parm)]);
3975 return 1;
3979 void
3980 mark_decl_instantiated (result, extern_p)
3981 tree result;
3982 int extern_p;
3984 if (DECL_TEMPLATE_INSTANTIATION (result))
3985 SET_DECL_EXPLICIT_INSTANTIATION (result);
3986 TREE_PUBLIC (result) = 1;
3988 if (! extern_p)
3990 DECL_INTERFACE_KNOWN (result) = 1;
3991 DECL_NOT_REALLY_EXTERN (result) = 1;
3993 /* For WIN32 we also want to put explicit instantiations in
3994 linkonce sections. */
3995 if (supports_one_only () && ! SUPPORTS_WEAK)
3996 comdat_linkage (result);
3998 else if (TREE_CODE (result) == FUNCTION_DECL)
3999 mark_inline_for_output (result);
4002 /* Given two function templates PAT1 and PAT2, return:
4004 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
4005 -1 if PAT2 is more specialized than PAT1.
4006 0 if neither is more specialized. */
4009 more_specialized (pat1, pat2)
4010 tree pat1, pat2;
4012 tree targs;
4013 int winner = 0;
4015 targs = get_bindings (pat1, pat2);
4016 if (targs)
4018 --winner;
4021 targs = get_bindings (pat2, pat1);
4022 if (targs)
4024 ++winner;
4027 return winner;
4030 /* Given two class template specialization list nodes PAT1 and PAT2, return:
4032 1 if PAT1 is more specialized than PAT2 as described in [temp.class.order].
4033 -1 if PAT2 is more specialized than PAT1.
4034 0 if neither is more specialized. */
4037 more_specialized_class (pat1, pat2)
4038 tree pat1, pat2;
4040 tree targs;
4041 int winner = 0;
4043 targs = get_class_bindings
4044 (TREE_VALUE (pat1), TREE_PURPOSE (pat1), TREE_PURPOSE (pat2));
4045 if (targs)
4046 --winner;
4048 targs = get_class_bindings
4049 (TREE_VALUE (pat2), TREE_PURPOSE (pat2), TREE_PURPOSE (pat1));
4050 if (targs)
4051 ++winner;
4053 return winner;
4056 /* Return the template arguments that will produce the function signature
4057 DECL from the function template FN. */
4059 tree
4060 get_bindings (fn, decl)
4061 tree fn, decl;
4063 int ntparms = DECL_NTPARMS (fn);
4064 tree targs = make_scratch_vec (ntparms);
4065 int i;
4067 i = fn_type_unification (fn, NULL_TREE, targs,
4068 TYPE_ARG_TYPES (TREE_TYPE (decl)),
4069 TREE_TYPE (TREE_TYPE (decl)),
4072 if (i == 0)
4073 return targs;
4074 return 0;
4077 static tree
4078 get_class_bindings (tparms, parms, args)
4079 tree tparms, parms, args;
4081 int i, dummy, ntparms = TREE_VEC_LENGTH (tparms);
4082 tree vec = make_temp_vec (ntparms);
4084 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
4086 switch (unify (tparms, &TREE_VEC_ELT (vec, 0), ntparms,
4087 TREE_VEC_ELT (parms, i), TREE_VEC_ELT (args, i),
4088 &dummy, 1))
4090 case 0:
4091 break;
4092 case 1:
4093 return NULL_TREE;
4097 for (i = 0; i < ntparms; ++i)
4098 if (! TREE_VEC_ELT (vec, i))
4099 return NULL_TREE;
4101 return vec;
4104 /* Return the most specialized of the list of templates in FNS that can
4105 produce an instantiation matching DECL. */
4107 tree
4108 most_specialized (fns, decl)
4109 tree fns, decl;
4111 tree fn, champ, args, *p;
4112 int fate;
4114 for (p = &fns; *p; )
4116 args = get_bindings (TREE_VALUE (*p), decl);
4117 if (args)
4119 p = &TREE_CHAIN (*p);
4121 else
4122 *p = TREE_CHAIN (*p);
4125 if (! fns)
4126 return NULL_TREE;
4128 fn = fns;
4129 champ = TREE_VALUE (fn);
4130 fn = TREE_CHAIN (fn);
4131 for (; fn; fn = TREE_CHAIN (fn))
4133 fate = more_specialized (champ, TREE_VALUE (fn));
4134 if (fate == 1)
4136 else
4138 if (fate == 0)
4140 fn = TREE_CHAIN (fn);
4141 if (! fn)
4142 return error_mark_node;
4144 champ = TREE_VALUE (fn);
4148 for (fn = fns; fn && TREE_VALUE (fn) != champ; fn = TREE_CHAIN (fn))
4150 fate = more_specialized (champ, TREE_VALUE (fn));
4151 if (fate != 1)
4152 return error_mark_node;
4155 return champ;
4158 /* Return the most specialized of the class template specializations in
4159 SPECS that can produce an instantiation matching ARGS. */
4161 tree
4162 most_specialized_class (specs, mainargs)
4163 tree specs, mainargs;
4165 tree list = NULL_TREE, t, args, champ;
4166 int fate;
4168 for (t = specs; t; t = TREE_CHAIN (t))
4170 args = get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), mainargs);
4171 if (args)
4173 list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
4174 TREE_TYPE (list) = TREE_TYPE (t);
4178 if (! list)
4179 return NULL_TREE;
4181 t = list;
4182 champ = t;
4183 t = TREE_CHAIN (t);
4184 for (; t; t = TREE_CHAIN (t))
4186 fate = more_specialized_class (champ, t);
4187 if (fate == 1)
4189 else
4191 if (fate == 0)
4193 t = TREE_CHAIN (t);
4194 if (! t)
4195 return error_mark_node;
4197 champ = t;
4201 for (t = list; t && t != champ; t = TREE_CHAIN (t))
4203 fate = more_specialized_class (champ, t);
4204 if (fate != 1)
4205 return error_mark_node;
4208 return champ;
4211 /* called from the parser. */
4213 void
4214 do_decl_instantiation (declspecs, declarator, storage)
4215 tree declspecs, declarator, storage;
4217 tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
4218 tree name;
4219 tree fn;
4220 tree result = NULL_TREE;
4221 int extern_p = 0;
4222 tree templates = NULL_TREE;
4224 if (! DECL_LANG_SPECIFIC (decl))
4226 cp_error ("explicit instantiation of non-template `%#D'", decl);
4227 return;
4230 /* If we've already seen this template instance, use it. */
4231 if (TREE_CODE (decl) == VAR_DECL)
4233 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
4234 if (result && TREE_CODE (result) != VAR_DECL)
4235 result = NULL_TREE;
4237 else if (TREE_CODE (decl) != FUNCTION_DECL)
4239 cp_error ("explicit instantiation of `%#D'", decl);
4240 return;
4242 else if (DECL_FUNCTION_MEMBER_P (decl))
4244 if (DECL_TEMPLATE_INSTANTIATION (decl))
4245 result = decl;
4246 else if (name = DECL_ASSEMBLER_NAME (decl),
4247 fn = IDENTIFIER_GLOBAL_VALUE (name),
4248 fn && DECL_TEMPLATE_INSTANTIATION (fn))
4249 result = fn;
4250 else
4252 /* Maybe this is an instantiation of a member template
4253 function. */
4254 tree ctype = DECL_CONTEXT (decl);
4256 name = DECL_NAME (decl);
4257 fn = lookup_fnfields (TYPE_BINFO (ctype), name, 1);
4258 if (fn)
4259 fn = TREE_VALUE (fn);
4261 for (; fn; fn = DECL_CHAIN (fn))
4262 if (TREE_CODE (fn) == TEMPLATE_DECL)
4263 templates = decl_tree_cons (NULL_TREE, fn, templates);
4266 else if (name = DECL_NAME (decl), fn = IDENTIFIER_GLOBAL_VALUE (name), fn)
4268 for (fn = get_first_fn (fn); fn; fn = DECL_CHAIN (fn))
4269 if (TREE_CODE (fn) == TEMPLATE_DECL)
4270 templates = decl_tree_cons (NULL_TREE, fn, templates);
4273 if (templates && !result)
4275 tree args;
4276 result = most_specialized (templates, decl);
4277 if (result == error_mark_node)
4279 char *str = "candidates are:";
4280 cp_error ("ambiguous template instantiation for `%D' requested", decl);
4281 for (fn = templates; fn; fn = TREE_CHAIN (fn))
4283 cp_error_at ("%s %+#D", str, TREE_VALUE (fn));
4284 str = " ";
4286 return;
4288 else if (result)
4290 args = get_bindings (result, decl);
4291 result = instantiate_template (result, args);
4295 if (! result)
4297 cp_error ("no matching template for `%D' found", decl);
4298 return;
4301 if (! DECL_TEMPLATE_INFO (result))
4303 cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
4304 return;
4307 if (flag_external_templates)
4308 return;
4310 if (storage == NULL_TREE)
4312 else if (storage == ridpointers[(int) RID_EXTERN])
4313 extern_p = 1;
4314 else
4315 cp_error ("storage class `%D' applied to template instantiation",
4316 storage);
4318 mark_decl_instantiated (result, extern_p);
4319 repo_template_instantiated (result, extern_p);
4320 if (! extern_p)
4321 instantiate_decl (result);
4324 void
4325 mark_class_instantiated (t, extern_p)
4326 tree t;
4327 int extern_p;
4329 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
4331 if (supports_one_only () && ! SUPPORTS_WEAK)
4332 /* For WIN32 we also want to put explicit instantiations in
4333 linkonce sections. */;
4334 else
4336 SET_CLASSTYPE_INTERFACE_KNOWN (t);
4337 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
4340 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
4341 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
4342 if (! extern_p)
4344 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
4345 rest_of_type_compilation (t, 1);
4349 void
4350 do_type_instantiation (t, storage)
4351 tree t, storage;
4353 int extern_p = 0;
4354 int nomem_p = 0;
4355 int static_p = 0;
4357 if (TREE_CODE (t) == TYPE_DECL)
4358 t = TREE_TYPE (t);
4360 if (! IS_AGGR_TYPE (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
4362 cp_error ("explicit instantiation of non-template type `%T'", t);
4363 return;
4366 complete_type (t);
4368 /* With -fexternal-templates, explicit instantiations are treated the same
4369 as implicit ones. */
4370 if (flag_external_templates)
4371 return;
4373 if (TYPE_SIZE (t) == NULL_TREE)
4375 cp_error ("explicit instantiation of `%#T' before definition of template",
4377 return;
4380 if (storage == NULL_TREE)
4381 /* OK */;
4382 else if (storage == ridpointers[(int) RID_INLINE])
4383 nomem_p = 1;
4384 else if (storage == ridpointers[(int) RID_EXTERN])
4385 extern_p = 1;
4386 else if (storage == ridpointers[(int) RID_STATIC])
4387 static_p = 1;
4388 else
4390 cp_error ("storage class `%D' applied to template instantiation",
4391 storage);
4392 extern_p = 0;
4395 /* We've already instantiated this. */
4396 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && ! CLASSTYPE_INTERFACE_ONLY (t)
4397 && extern_p)
4398 return;
4400 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
4402 mark_class_instantiated (t, extern_p);
4403 repo_template_instantiated (t, extern_p);
4406 if (nomem_p)
4407 return;
4410 tree tmp;
4412 if (! static_p)
4413 for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
4414 if (TREE_CODE (tmp) == FUNCTION_DECL
4415 && DECL_TEMPLATE_INSTANTIATION (tmp))
4417 mark_decl_instantiated (tmp, extern_p);
4418 repo_template_instantiated (tmp, extern_p);
4419 if (! extern_p)
4420 instantiate_decl (tmp);
4423 for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
4424 if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
4426 mark_decl_instantiated (tmp, extern_p);
4427 repo_template_instantiated (tmp, extern_p);
4428 if (! extern_p)
4429 instantiate_decl (tmp);
4432 for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
4433 if (IS_AGGR_TYPE (TREE_VALUE (tmp)))
4434 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
4438 tree
4439 instantiate_decl (d)
4440 tree d;
4442 tree ti = DECL_TEMPLATE_INFO (d);
4443 tree tmpl = TI_TEMPLATE (ti);
4444 tree args = TI_ARGS (ti);
4445 tree td;
4446 tree decl_pattern, code_pattern;
4447 tree save_ti;
4448 int nested = in_function_p ();
4449 int d_defined;
4450 int pattern_defined;
4451 int line = lineno;
4452 char *file = input_filename;
4454 for (td = tmpl; DECL_TEMPLATE_INSTANTIATION (td); )
4455 td = DECL_TI_TEMPLATE (td);
4457 /* In the case of a member template, decl_pattern is the partially
4458 instantiated declaration (in the instantiated class), and code_pattern
4459 is the original template definition. */
4460 decl_pattern = DECL_TEMPLATE_RESULT (tmpl);
4461 code_pattern = DECL_TEMPLATE_RESULT (td);
4463 if (TREE_CODE (d) == FUNCTION_DECL)
4465 d_defined = (DECL_INITIAL (d) != NULL_TREE);
4466 pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
4468 else
4470 d_defined = ! DECL_IN_AGGR_P (d);
4471 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
4474 if (d_defined)
4475 return d;
4477 if (TREE_CODE (d) == FUNCTION_DECL)
4479 tree specs;
4481 /* Check to see if there is a matching specialization. */
4482 for (specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4483 specs != NULL_TREE;
4484 specs = TREE_CHAIN (specs))
4485 if (comp_template_args (TREE_PURPOSE (specs), args))
4486 return TREE_VALUE (specs);
4489 /* This needs to happen before any tsubsting. */
4490 if (! push_tinst_level (d))
4491 return d;
4493 push_to_top_level ();
4494 lineno = DECL_SOURCE_LINE (d);
4495 input_filename = DECL_SOURCE_FILE (d);
4497 /* We need to set up DECL_INITIAL regardless of pattern_defined if the
4498 variable is a static const initialized in the class body. */
4499 if (TREE_CODE (d) == VAR_DECL
4500 && ! DECL_INITIAL (d) && DECL_INITIAL (code_pattern))
4502 pushclass (DECL_CONTEXT (d), 2);
4503 DECL_INITIAL (d) = tsubst_expr (DECL_INITIAL (code_pattern), args,
4504 TREE_VEC_LENGTH (args), tmpl);
4505 popclass (1);
4508 /* import_export_decl has to happen after DECL_INITIAL is set up. */
4509 if (pattern_defined)
4511 repo_template_used (d);
4513 if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
4515 if (flag_alt_external_templates)
4517 if (interface_unknown)
4518 warn_if_unknown_interface (d);
4520 else if (DECL_INTERFACE_KNOWN (code_pattern))
4522 DECL_INTERFACE_KNOWN (d) = 1;
4523 DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
4525 else
4526 warn_if_unknown_interface (code_pattern);
4529 if (at_eof)
4530 import_export_decl (d);
4533 /* Reject all external templates except inline functions. */
4534 if (DECL_INTERFACE_KNOWN (d)
4535 && ! DECL_NOT_REALLY_EXTERN (d)
4536 && ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
4537 goto out;
4539 /* Defer all templates except inline functions used in another function. */
4540 if (! pattern_defined
4541 || (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
4542 && ! at_eof))
4544 add_pending_template (d);
4545 goto out;
4548 lineno = DECL_SOURCE_LINE (d);
4549 input_filename = DECL_SOURCE_FILE (d);
4551 /* Trick tsubst into giving us a new decl in case the template changed. */
4552 save_ti = DECL_TEMPLATE_INFO (decl_pattern);
4553 DECL_TEMPLATE_INFO (decl_pattern) = NULL_TREE;
4554 td = tsubst (decl_pattern, args, TREE_VEC_LENGTH (args), tmpl);
4555 SET_DECL_IMPLICIT_INSTANTIATION (td);
4556 DECL_TEMPLATE_INFO (decl_pattern) = save_ti;
4558 /* And set up DECL_INITIAL, since tsubst doesn't. */
4559 if (TREE_CODE (td) == VAR_DECL)
4561 pushclass (DECL_CONTEXT (d), 2);
4562 DECL_INITIAL (td) = tsubst_expr (DECL_INITIAL (code_pattern), args,
4563 TREE_VEC_LENGTH (args), tmpl);
4564 popclass (1);
4567 if (TREE_CODE (d) == FUNCTION_DECL)
4569 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
4570 new decl. */
4571 DECL_INITIAL (td) = error_mark_node;
4573 if (DECL_TEMPLATE_SPECIALIZATION (td) && !DECL_TEMPLATE_INFO (td))
4574 /* Set up the information about what is being specialized. */
4575 DECL_TEMPLATE_INFO (td) = DECL_TEMPLATE_INFO (d);
4577 duplicate_decls (td, d);
4578 if (TREE_CODE (d) == FUNCTION_DECL)
4579 DECL_INITIAL (td) = 0;
4581 if (TREE_CODE (d) == VAR_DECL)
4583 DECL_IN_AGGR_P (d) = 0;
4584 if (DECL_INTERFACE_KNOWN (d))
4585 DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
4586 else
4588 DECL_EXTERNAL (d) = 1;
4589 DECL_NOT_REALLY_EXTERN (d) = 1;
4591 cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
4593 else if (TREE_CODE (d) == FUNCTION_DECL)
4595 tree t = DECL_SAVED_TREE (code_pattern);
4597 start_function (NULL_TREE, d, NULL_TREE, 1);
4598 store_parm_decls ();
4600 if (t && TREE_CODE (t) == RETURN_INIT)
4602 store_return_init
4603 (TREE_OPERAND (t, 0),
4604 tsubst_expr (TREE_OPERAND (t, 1), args,
4605 TREE_VEC_LENGTH (args), tmpl));
4606 t = TREE_CHAIN (t);
4609 if (t && TREE_CODE (t) == CTOR_INITIALIZER)
4611 current_member_init_list
4612 = tsubst_expr_values (TREE_OPERAND (t, 0), args);
4613 current_base_init_list
4614 = tsubst_expr_values (TREE_OPERAND (t, 1), args);
4615 t = TREE_CHAIN (t);
4618 setup_vtbl_ptr ();
4619 /* Always keep the BLOCK node associated with the outermost
4620 pair of curly braces of a function. These are needed
4621 for correct operation of dwarfout.c. */
4622 keep_next_level ();
4624 my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
4625 tsubst_expr (t, args, TREE_VEC_LENGTH (args), tmpl);
4627 finish_function (lineno, 0, nested);
4630 out:
4631 lineno = line;
4632 input_filename = file;
4634 pop_from_top_level ();
4635 pop_tinst_level ();
4637 return d;
4640 tree
4641 tsubst_chain (t, argvec)
4642 tree t, argvec;
4644 if (t)
4646 tree first = tsubst (t, argvec,
4647 TREE_VEC_LENGTH (argvec), NULL_TREE);
4648 tree last = first;
4650 for (t = TREE_CHAIN (t); t; t = TREE_CHAIN (t))
4652 tree x = tsubst (t, argvec, TREE_VEC_LENGTH (argvec), NULL_TREE);
4653 TREE_CHAIN (last) = x;
4654 last = x;
4657 return first;
4659 return NULL_TREE;
4662 static tree
4663 tsubst_expr_values (t, argvec)
4664 tree t, argvec;
4666 tree first = NULL_TREE;
4667 tree *p = &first;
4669 for (; t; t = TREE_CHAIN (t))
4671 tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
4672 TREE_VEC_LENGTH (argvec), NULL_TREE);
4673 tree val = tsubst_expr (TREE_VALUE (t), argvec,
4674 TREE_VEC_LENGTH (argvec), NULL_TREE);
4675 *p = build_tree_list (pur, val);
4676 p = &TREE_CHAIN (*p);
4678 return first;
4681 tree last_tree;
4683 void
4684 add_tree (t)
4685 tree t;
4687 last_tree = TREE_CHAIN (last_tree) = t;
4690 /* D is an undefined function declaration in the presence of templates with
4691 the same name, listed in FNS. If one of them can produce D as an
4692 instantiation, remember this so we can instantiate it at EOF if D has
4693 not been defined by that time. */
4695 void
4696 add_maybe_template (d, fns)
4697 tree d, fns;
4699 tree t;
4701 if (DECL_MAYBE_TEMPLATE (d))
4702 return;
4704 t = most_specialized (fns, d);
4705 if (! t)
4706 return;
4707 if (t == error_mark_node)
4709 cp_error ("ambiguous template instantiation for `%D'", d);
4710 return;
4713 *maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
4714 maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
4715 DECL_MAYBE_TEMPLATE (d) = 1;
4718 /* Instantiate an enumerated type. Used by instantiate_class_template and
4719 tsubst_expr. */
4721 static tree
4722 tsubst_enum (tag, args, nargs, field_chain)
4723 tree tag, args;
4724 int nargs;
4725 tree * field_chain;
4727 extern tree current_local_enum;
4728 tree prev_local_enum = current_local_enum;
4730 tree newtag = start_enum (TYPE_IDENTIFIER (tag));
4731 tree e, values = NULL_TREE;
4733 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
4735 tree elt = build_enumerator (TREE_PURPOSE (e),
4736 tsubst_expr (TREE_VALUE (e), args,
4737 nargs, NULL_TREE));
4738 TREE_CHAIN (elt) = values;
4739 values = elt;
4742 finish_enum (newtag, values);
4744 if (NULL != field_chain)
4745 *field_chain = grok_enum_decls (newtag, NULL_TREE);
4747 current_local_enum = prev_local_enum;
4749 return newtag;