1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
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)
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:
24 * templates for class static data don't work (methods only)
25 * duplicated method templates can crash the compiler
26 * interface/impl data is taken from file defining the template
27 * all methods must be provided in header files; can't use a source
28 file that contains only the method templates and "just win"
44 extern struct obstack permanent_obstack
;
47 extern char *input_filename
;
48 struct pending_inline
*pending_template_expansions
;
50 tree current_template_parms
;
51 HOST_WIDE_INT processing_template_decl
;
53 tree pending_templates
;
54 static tree
*template_tail
= &pending_templates
;
56 int minimal_parse_mode
;
58 #define obstack_chunk_alloc xmalloc
59 #define obstack_chunk_free free
63 void pop_template_decls ();
65 tree
classtype_mangled_name ();
66 static char * mangle_class_name_for_template ();
67 tree
tsubst_expr_values ();
69 /* We've got a template header coming up; push to a new level for storing
73 begin_template_parm_list ()
76 declare_pseudo_global_level ();
79 /* Process information from new template parameter NEXT and append it to the
82 process_template_parm (list
, next
)
90 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
91 defval
= TREE_PURPOSE (parm
);
92 parm
= TREE_VALUE (parm
);
93 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
97 tree p
= TREE_VALUE (tree_last (list
));
99 if (TREE_CODE (p
) == TYPE_DECL
)
100 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
102 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
111 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
112 /* is a const-param */
113 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
114 PARM
, 0, NULL_TREE
, NULL_TREE
);
115 /* A template parameter is not modifiable. */
116 TREE_READONLY (parm
) = 1;
117 if (IS_AGGR_TYPE (TREE_TYPE (parm
))
118 && TREE_CODE (TREE_TYPE (parm
)) != TEMPLATE_TYPE_PARM
)
120 cp_error ("`%#T' is not a valid type for a template constant parameter",
122 if (DECL_NAME (parm
) == NULL_TREE
)
123 error (" a template type parameter must begin with `class' or `typename'");
124 TREE_TYPE (parm
) = void_type_node
;
126 tinfo
= make_node (TEMPLATE_CONST_PARM
);
127 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
128 if (TREE_PERMANENT (parm
) == 0)
130 parm
= copy_node (parm
);
131 TREE_PERMANENT (parm
) = 1;
133 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
134 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
135 DECL_INITIAL (decl
) = tinfo
;
136 DECL_INITIAL (parm
) = tinfo
;
137 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
+ 1);
141 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
142 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
143 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
144 TYPE_MAIN_DECL (t
) = decl
;
146 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
+ 1);
148 SET_DECL_ARTIFICIAL (decl
);
150 parm
= build_tree_list (defval
, parm
);
151 return chainon (list
, parm
);
154 /* The end of a template parameter list has been reached. Process the
155 tree list into a parameter vector, converting each parameter into a more
156 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
160 end_template_parm_list (parms
)
165 tree saved_parmlist
= make_tree_vec (list_length (parms
));
167 ++processing_template_decl
;
168 current_template_parms
169 = tree_cons (build_int_2 (0, processing_template_decl
),
170 saved_parmlist
, current_template_parms
);
172 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
173 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
175 return saved_parmlist
;
178 /* end_template_decl is called after a template declaration is seen. */
183 /* This matches the pushlevel in begin_template_parm_list. */
186 --processing_template_decl
;
187 current_template_parms
= TREE_CHAIN (current_template_parms
);
188 (void) get_pending_sizes (); /* Why? */
192 push_template_decl (decl
)
195 tree header
= current_template_parms
;
197 tree args
= NULL_TREE
;
199 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
203 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
204 && DECL_CLASS_CONTEXT (decl
))
206 /* Note that this template is a "primary template" */
207 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
208 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
213 tree a
= copy_node (TREE_VALUE (header
));
214 int i
= TREE_VEC_LENGTH (a
);
215 TREE_TYPE (a
) = NULL_TREE
;
218 tree t
= TREE_VALUE (TREE_VEC_ELT (a
, i
));
219 if (TREE_CODE (t
) == TYPE_DECL
)
222 t
= DECL_INITIAL (t
);
223 TREE_VEC_ELT (a
, i
) = t
;
225 args
= tree_cons (TREE_PURPOSE (header
), a
, args
);
226 header
= TREE_CHAIN (header
);
228 args
= nreverse (args
);
229 args
= TREE_VALUE (args
);
231 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
233 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
234 DECL_TEMPLATE_PARMS (tmpl
) = TREE_VALUE (current_template_parms
);
235 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
239 if (TREE_CODE (decl
) == TYPE_DECL
)
240 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
241 else if (! DECL_TEMPLATE_INFO (decl
))
243 cp_error ("template definition of non-template `%#D'", decl
);
247 tmpl
= DECL_TI_TEMPLATE (decl
);
250 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
251 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
254 tmpl
= pushdecl_top_level (tmpl
);
257 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl
)) = tmpl
;
259 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
261 if (TREE_CODE (decl
) == TYPE_DECL
)
263 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
264 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
266 else if (! DECL_LANG_SPECIFIC (decl
))
267 cp_error ("template declaration of `%#D'", decl
);
269 DECL_TEMPLATE_INFO (decl
) = info
;
272 tree tsubst
PROTO ((tree
, tree
*, int, tree
));
274 /* Convert all template arguments to their appropriate types, and return
275 a vector containing the resulting values. If any error occurs, return
278 coerce_template_parms (parms
, arglist
, in_decl
)
282 int nparms
, nargs
, i
, lost
= 0;
285 if (arglist
== NULL_TREE
)
287 else if (TREE_CODE (arglist
) == TREE_VEC
)
288 nargs
= TREE_VEC_LENGTH (arglist
);
290 nargs
= list_length (arglist
);
292 nparms
= TREE_VEC_LENGTH (parms
);
296 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
298 error ("incorrect number of parameters (%d, should be %d)",
301 cp_error_at ("in template expansion for decl `%D'", in_decl
);
302 return error_mark_node
;
305 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
306 vec
= copy_node (arglist
);
309 vec
= make_tree_vec (nparms
);
310 for (i
= 0; i
< nparms
; i
++)
317 arglist
= TREE_CHAIN (arglist
);
319 if (arg
== error_mark_node
)
322 arg
= TREE_VALUE (arg
);
325 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
326 &TREE_VEC_ELT (vec
, 0), i
, in_decl
);
328 TREE_VEC_ELT (vec
, i
) = arg
;
331 for (i
= 0; i
< nparms
; i
++)
333 tree arg
= TREE_VEC_ELT (vec
, i
);
334 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
336 int is_type
, requires_type
;
338 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
339 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
341 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
342 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
344 cp_pedwarn ("to refer to a type member of a template parameter,");
345 cp_pedwarn (" use `typename %E'", arg
);
346 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
347 TREE_OPERAND (arg
, 1));
350 if (is_type
!= requires_type
)
354 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
357 cp_error (" expected a constant of type `%T', got `%T'",
358 TREE_TYPE (parm
), arg
);
360 cp_error (" expected a type, got `%E'", arg
);
363 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
368 val
= groktypename (arg
);
369 if (! current_template_parms
)
371 tree t
= target_type (val
);
373 && decl_function_context (TYPE_MAIN_DECL (t
)))
375 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
376 return error_mark_node
;
382 tree t
= tsubst (TREE_TYPE (parm
), &TREE_VEC_ELT (vec
, 0),
383 TREE_VEC_LENGTH (vec
), in_decl
);
384 if (current_template_parms
)
387 val
= digest_init (t
, arg
, (tree
*) 0);
389 if (val
== error_mark_node
|| current_template_parms
)
392 /* 14.2: Other template-arguments must be constant-expressions,
393 addresses of objects or functions with external linkage, or of
394 static class members. */
395 else if (!TREE_CONSTANT (val
))
397 cp_error ("non-const `%E' cannot be used as template argument",
399 val
= error_mark_node
;
401 else if (POINTER_TYPE_P (TREE_TYPE (val
))
402 && ! integer_zerop (val
)
403 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
404 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
408 if (TREE_CODE (t
) == ADDR_EXPR
)
410 tree a
= TREE_OPERAND (t
, 0);
412 if (TREE_CODE (a
) == STRING_CST
)
414 cp_error ("string literal %E is not a valid template argument", a
);
415 error ("because it is the address of an object with static linkage");
416 val
= error_mark_node
;
418 else if (TREE_CODE (a
) != VAR_DECL
419 && TREE_CODE (a
) != FUNCTION_DECL
)
421 else if (! DECL_PUBLIC (a
))
423 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
424 val
= error_mark_node
;
430 cp_error ("`%E' is not a valid template argument", t
);
431 error ("it must be %s%s with external linkage",
432 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
433 ? "a pointer to " : "",
434 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
435 ? "a function" : "an object");
436 val
= error_mark_node
;
441 if (val
== error_mark_node
)
444 TREE_VEC_ELT (vec
, i
) = val
;
447 return error_mark_node
;
452 comp_template_args (oldargs
, newargs
)
453 tree oldargs
, newargs
;
457 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
459 tree nt
= TREE_VEC_ELT (newargs
, i
);
460 tree ot
= TREE_VEC_ELT (oldargs
, i
);
464 if (TREE_CODE (nt
) != TREE_CODE (ot
))
466 if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't' && comptypes (ot
, nt
, 1))
468 if (TREE_CODE (ot
) == TEMPLATE_CONST_PARM
469 && TEMPLATE_CONST_IDX (nt
) == TEMPLATE_CONST_IDX (ot
))
476 /* Given class template name and parameter list, produce a user-friendly name
477 for the instantiation. */
479 mangle_class_name_for_template (name
, parms
, arglist
)
483 static struct obstack scratch_obstack
;
484 static char *scratch_firstobj
;
487 if (!scratch_firstobj
)
488 gcc_obstack_init (&scratch_obstack
);
490 obstack_free (&scratch_obstack
, scratch_firstobj
);
491 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
494 #define buflen sizeof(buf)
495 #define check if (bufp >= buf+buflen-1) goto too_long
496 #define ccat(c) *bufp++=(c); check
497 #define advance bufp+=strlen(bufp); check
498 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
501 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
503 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
508 nparms
= TREE_VEC_LENGTH (parms
);
509 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
510 for (i
= 0; i
< nparms
; i
++)
512 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
513 tree arg
= TREE_VEC_ELT (arglist
, i
);
518 if (TREE_CODE (parm
) == TYPE_DECL
)
520 cat (type_as_string (arg
, 0));
524 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
526 if (TREE_CODE (arg
) == TREE_LIST
)
528 /* New list cell was built because old chain link was in
530 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
531 arg
= TREE_VALUE (arg
);
533 /* No need to check arglist against parmlist here; we did that
534 in coerce_template_parms, called from lookup_template_class. */
535 cat (expr_as_string (arg
, 0));
538 char *bufp
= obstack_next_free (&scratch_obstack
);
540 while (bufp
[offset
- 1] == ' ')
542 obstack_blank_fast (&scratch_obstack
, offset
);
544 /* B<C<char> >, not B<C<char>> */
545 if (bufp
[offset
- 1] == '>')
550 return (char *) obstack_base (&scratch_obstack
);
555 fatal ("out of (preallocated) string space creating template instantiation name");
561 classtype_mangled_name (t
)
564 if (CLASSTYPE_TEMPLATE_INFO (t
)
565 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
567 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
568 char *mangled_name
= mangle_class_name_for_template
569 (IDENTIFIER_POINTER (name
),
570 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
571 CLASSTYPE_TI_ARGS (t
));
572 tree id
= get_identifier (mangled_name
);
573 IDENTIFIER_TEMPLATE (id
) = name
;
577 return TYPE_IDENTIFIER (t
);
581 add_pending_template (d
)
584 if (TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)))
587 *template_tail
= perm_tree_cons
588 (current_function_decl
, d
, NULL_TREE
);
589 template_tail
= &TREE_CHAIN (*template_tail
);
590 TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)) = 1;
593 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
594 parameters, find the desired type.
596 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
597 Since ARGLIST is build on the decl_obstack, we must copy it here
598 to keep it from being reclaimed when the decl storage is reclaimed.
600 IN_DECL, if non-NULL, is the template declaration we are trying to
603 lookup_template_class (d1
, arglist
, in_decl
)
607 tree
template, parmlist
;
612 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
614 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
616 template = IDENTIFIER_CLASS_VALUE (d1
);
618 else if (TREE_CODE (d1
) == TYPE_DECL
&& IS_AGGR_TYPE (TREE_TYPE (d1
)))
620 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (d1
));
621 d1
= DECL_NAME (template);
623 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
625 template = CLASSTYPE_TI_TEMPLATE (d1
);
626 d1
= DECL_NAME (template);
629 my_friendly_abort (272);
631 /* With something like `template <class T> class X class X { ... };'
632 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
633 We don't want to do that, but we have to deal with the situation, so
634 let's give them some syntax errors to chew on instead of a crash. */
636 return error_mark_node
;
637 if (TREE_CODE (template) != TEMPLATE_DECL
)
639 cp_error ("non-template type `%T' used as a template", d1
);
641 cp_error_at ("for template declaration `%D'", in_decl
);
642 return error_mark_node
;
645 if (PRIMARY_TEMPLATE_P (template))
647 parmlist
= DECL_TEMPLATE_PARMS (template);
649 arglist
= coerce_template_parms (parmlist
, arglist
, template);
650 if (arglist
== error_mark_node
)
651 return error_mark_node
;
652 if (uses_template_parms (arglist
))
655 if (comp_template_args
656 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
657 found
= TREE_TYPE (template);
660 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
661 found
; found
= TREE_CHAIN (found
))
663 if (TI_USES_TEMPLATE_PARMS (found
)
664 && comp_template_args (TREE_PURPOSE (found
), arglist
))
668 found
= TREE_VALUE (found
);
673 if (can_free (&permanent_obstack
, arglist
))
674 obstack_free (&permanent_obstack
, arglist
);
679 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
681 id
= get_identifier (mangled_name
);
682 IDENTIFIER_TEMPLATE (id
) = d1
;
684 maybe_push_to_top_level (uses_template_parms (arglist
));
685 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
686 pop_from_top_level ();
690 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
693 arglist
= CLASSTYPE_TI_ARGS (ctx
);
695 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
697 tree save_parms
= current_template_parms
;
698 current_template_parms
= NULL_TREE
;
699 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
700 current_template_parms
= save_parms
;
704 t
= lookup_nested_type_by_name (ctx
, id
);
705 my_friendly_assert (t
!= NULL_TREE
, 42);
709 /* Seems to be wanted. */
710 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
712 if (! CLASSTYPE_TEMPLATE_INFO (t
))
714 arglist
= copy_to_permanent (arglist
);
715 CLASSTYPE_TEMPLATE_INFO (t
)
716 = perm_tree_cons (template, arglist
, NULL_TREE
);
717 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
718 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
719 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
720 = uses_template_parms (arglist
);
722 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
724 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
725 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
726 if (! uses_template_parms (arglist
))
727 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
728 = get_identifier (build_overload_name (t
, 1, 1));
730 if (flag_external_templates
&& ! uses_template_parms (arglist
)
731 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
732 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
736 instantiate_class_template (t
);
738 for (d
= TYPE_METHODS (t
); d
; d
= TREE_CHAIN (d
))
739 add_pending_template (d
);
746 /* Should be defined in parse.h. */
750 uses_template_parms (t
)
755 switch (TREE_CODE (t
))
759 /* We assume that the object must be instantiated in order to build
760 the COMPONENT_REF, so we test only whether the type of the
761 COMPONENT_REF uses template parms. */
762 return uses_template_parms (TREE_TYPE (t
));
764 case IDENTIFIER_NODE
:
765 if (!IDENTIFIER_TEMPLATE (t
))
767 my_friendly_abort (42);
769 /* aggregates of tree nodes */
772 int i
= TREE_VEC_LENGTH (t
);
774 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
779 if (uses_template_parms (TREE_PURPOSE (t
))
780 || uses_template_parms (TREE_VALUE (t
)))
782 return uses_template_parms (TREE_CHAIN (t
));
784 /* constructed type nodes */
787 return uses_template_parms (TREE_TYPE (t
));
789 if (TYPE_PTRMEMFUNC_FLAG (t
))
790 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
792 if (! CLASSTYPE_TEMPLATE_INFO (t
))
794 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
796 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
798 return uses_template_parms (TREE_TYPE (t
));
800 if (uses_template_parms (TYPE_DOMAIN (t
)))
802 return uses_template_parms (TREE_TYPE (t
));
804 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
806 return uses_template_parms (TREE_TYPE (t
));
808 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
810 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
812 return uses_template_parms (TREE_TYPE (t
));
816 return uses_template_parms (TREE_TYPE (t
));
820 /* ??? What about FIELD_DECLs? */
821 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
822 && uses_template_parms (DECL_TI_ARGS (t
)))
827 if (uses_template_parms (TREE_TYPE (t
)))
829 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
834 return uses_template_parms (TREE_TYPE (t
));
836 return uses_template_parms (TREE_OPERAND (t
, 0));
838 /* template parm nodes */
839 case TEMPLATE_TYPE_PARM
:
840 case TEMPLATE_CONST_PARM
:
843 /* simple type nodes */
845 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
847 return uses_template_parms (TYPE_MAX_VALUE (t
));
862 /* Non-error_mark_node ERROR_MARKs are bad things. */
863 my_friendly_assert (t
== error_mark_node
, 274);
872 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
873 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
874 /* else fall through */
877 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
885 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
886 if (uses_template_parms (TREE_OPERAND (t
, i
)))
893 sorry ("testing %s for template parms",
894 tree_code_name
[(int) TREE_CODE (t
)]);
895 my_friendly_abort (82);
901 static struct tinst_level
*current_tinst_level
= 0;
902 static struct tinst_level
*free_tinst_level
= 0;
903 static int tinst_depth
= 0;
904 int max_tinst_depth
= 17;
905 #ifdef GATHER_STATISTICS
906 int depth_reached
= 0;
913 struct tinst_level
*new;
915 if (tinst_depth
>= max_tinst_depth
)
917 struct tinst_level
*p
= current_tinst_level
;
919 char *file
= input_filename
;
921 error ("template instantiation depth exceeds maximum of %d",
923 cp_error (" instantiating `%D'", d
);
925 for (; p
; p
= p
->next
)
927 cp_error (" instantiated from `%D'", p
->decl
);
929 input_filename
= p
->file
;
931 error (" instantiated from here");
934 input_filename
= file
;
939 if (free_tinst_level
)
941 new = free_tinst_level
;
942 free_tinst_level
= new->next
;
945 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
949 new->file
= input_filename
;
950 new->next
= current_tinst_level
;
951 current_tinst_level
= new;
954 #ifdef GATHER_STATISTICS
955 if (tinst_depth
> depth_reached
)
956 depth_reached
= tinst_depth
;
965 struct tinst_level
*old
= current_tinst_level
;
967 current_tinst_level
= old
->next
;
968 old
->next
= free_tinst_level
;
969 free_tinst_level
= old
;
976 struct tinst_level
*p
= current_tinst_level
;
979 for (; p
->next
; p
= p
->next
)
985 instantiate_class_template (type
)
988 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
990 if (type
== error_mark_node
)
991 return error_mark_node
;
993 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
995 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
998 template = TI_TEMPLATE (template_info
);
999 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
1000 args
= TI_ARGS (template_info
);
1001 pattern
= TREE_TYPE (template);
1003 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1006 TYPE_BEING_DEFINED (type
) = 1;
1008 if (! push_tinst_level (type
))
1011 maybe_push_to_top_level (uses_template_parms (type
));
1012 pushclass (type
, 0);
1014 if (flag_external_templates
)
1016 if (flag_alt_external_templates
)
1018 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1019 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1020 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1021 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1022 && CLASSTYPE_INTERFACE_KNOWN (type
);
1026 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1027 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1028 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1029 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1030 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1031 && CLASSTYPE_INTERFACE_KNOWN (type
);
1036 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1037 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1041 tree binfo
= TYPE_BINFO (type
);
1042 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1048 int len
= TREE_VEC_LENGTH (pbases
);
1049 BINFO_BASETYPES (binfo
) = bases
= make_tree_vec (len
);
1050 for (i
= 0; i
< len
; ++i
)
1054 TREE_VEC_ELT (bases
, i
) = elt
1055 = tsubst (TREE_VEC_ELT (pbases
, i
), &TREE_VEC_ELT (args
, 0),
1056 TREE_VEC_LENGTH (args
), NULL_TREE
);
1057 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1059 if (! uses_template_parms (type
) &&
1060 TYPE_SIZE (complete_type (TREE_TYPE (elt
))) == NULL_TREE
)
1061 cp_error ("base class `%T' of `%T' has incomplete type",
1062 TREE_TYPE (elt
), type
);
1067 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1069 field_chain
= &TYPE_FIELDS (type
);
1071 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1073 tree name
= TREE_PURPOSE (t
);
1074 tree tag
= TREE_VALUE (t
);
1077 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1078 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1079 newtag
= start_enum (name
);
1081 newtag
= tsubst (tag
, &TREE_VEC_ELT (args
, 0),
1082 TREE_VEC_LENGTH (args
), NULL_TREE
);
1084 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1086 tree e
, values
= NULL_TREE
, *last
= &values
;
1088 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
1090 tree elt
= build_enumerator
1092 tsubst_expr (TREE_VALUE (e
), &TREE_VEC_ELT (args
, 0),
1093 TREE_VEC_LENGTH (args
), NULL_TREE
));
1094 DECL_FIELD_CONTEXT (TREE_VALUE (elt
)) = type
;
1096 last
= &TREE_CHAIN (elt
);
1099 finish_enum (newtag
, values
);
1101 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
1102 while (*field_chain
)
1103 field_chain
= &TREE_CHAIN (*field_chain
);
1107 /* Don't replace enum constants here. */
1108 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1109 if (TREE_CODE (t
) != CONST_DECL
)
1111 tree r
= tsubst (t
, &TREE_VEC_ELT (args
, 0),
1112 TREE_VEC_LENGTH (args
), NULL_TREE
);
1113 if (TREE_CODE (r
) == VAR_DECL
)
1115 if (! uses_template_parms (r
))
1116 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1117 /* Perhaps I should do more of grokfield here. */
1119 DECL_IN_AGGR_P (r
) = 1;
1120 DECL_EXTERNAL (r
) = 1;
1121 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1125 field_chain
= &TREE_CHAIN (r
);
1128 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1130 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1131 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1132 &TREE_VEC_ELT (args
, 0), TREE_VEC_LENGTH (args
), NULL_TREE
);
1135 tree d
= CLASSTYPE_FRIEND_CLASSES (type
) =
1136 tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), &TREE_VEC_ELT (args
, 0),
1137 TREE_VEC_LENGTH (args
), NULL_TREE
);
1139 /* This does injection for friend classes. */
1140 for (; d
; d
= TREE_CHAIN (d
))
1141 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1143 d
= tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args
, 0),
1144 TREE_VEC_LENGTH (args
), NULL_TREE
);
1146 for (; d
; d
= TREE_CHAIN (d
))
1148 tree t
= TREE_VALUE (d
);
1150 if (TREE_CODE (t
) == TYPE_DECL
)
1151 /* Already injected. */;
1157 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1158 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1159 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1160 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1161 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1162 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1163 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1164 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1165 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1166 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1167 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1168 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1169 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1170 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1171 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1172 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1173 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1174 TYPE_USES_COMPLEX_INHERITANCE (type
)
1175 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1176 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1177 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1179 if (! uses_template_parms (type
))
1182 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1183 if (TREE_CODE (tmp
) == FIELD_DECL
1184 && TREE_CODE (DECL_NAME (tmp
)) != SCOPE_REF
)
1185 require_complete_type (tmp
);
1187 /* XXX handle attributes */
1188 type
= finish_struct_1 (type
, NULL_TREE
, 0);
1192 TYPE_SIZE (type
) = integer_zero_node
;
1193 CLASSTYPE_METHOD_VEC (type
)
1194 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1197 TYPE_BEING_DEFINED (type
) = 0;
1200 pop_from_top_level ();
1210 if (t1
== NULL_TREE
)
1211 return t2
== NULL_TREE
;
1212 if (t2
== NULL_TREE
)
1214 /* Don't care if one declares its arg const and the other doesn't -- the
1215 main variant of the arg type is all that matters. */
1216 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1217 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1219 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1223 lookup_nested_type_by_name (ctype
, name
)
1228 complete_type (ctype
);
1230 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1232 if (name
== TREE_PURPOSE (t
))
1233 return TREE_VALUE (t
);
1239 tsubst (t
, args
, nargs
, in_decl
)
1246 if (t
== NULL_TREE
|| t
== error_mark_node
1247 || t
== integer_type_node
1248 || t
== void_type_node
1249 || t
== char_type_node
)
1252 type
= TREE_TYPE (t
);
1253 if (type
== unknown_type_node
)
1254 my_friendly_abort (42);
1255 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
)
1256 type
= tsubst (type
, args
, nargs
, in_decl
);
1258 switch (TREE_CODE (t
))
1261 if (TYPE_PTRMEMFUNC_P (t
))
1263 tree r
= build_ptrmemfunc_type
1264 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1265 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1269 /* else fall through */
1271 if (uses_template_parms (t
))
1273 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
1274 tree r
= lookup_template_class (t
, argvec
, in_decl
);
1275 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1279 /* else fall through */
1281 case IDENTIFIER_NODE
:
1293 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1294 if (ctx
== NULL_TREE
)
1297 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
1301 if (t
== integer_type_node
)
1304 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1305 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1309 tree max
= tsubst_expr (TYPE_MAX_VALUE (t
), args
, nargs
, in_decl
);
1310 if (current_template_parms
)
1312 tree itype
= make_node (INTEGER_TYPE
);
1313 TYPE_MIN_VALUE (itype
) = size_zero_node
;
1314 TYPE_MAX_VALUE (itype
) = max
;
1317 return build_index_2_type (size_zero_node
, max
);
1320 case TEMPLATE_TYPE_PARM
:
1322 tree arg
= args
[TEMPLATE_TYPE_IDX (t
)];
1323 return cp_build_type_variant
1324 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1325 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1328 case TEMPLATE_CONST_PARM
:
1329 return args
[TEMPLATE_CONST_IDX (t
)];
1334 tree arg_types
, ctx
;
1338 if (DECL_CONTEXT (t
) != NULL_TREE
1339 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1341 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
1345 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1346 type
= tsubst (type
, args
, nargs
, in_decl
);
1352 type
= tsubst (type
, args
, nargs
, in_decl
);
1355 if (type
== TREE_TYPE (t
)
1356 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
1359 /* Do we already have this instantiation? */
1360 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1362 tree tmpl
= TREE_PURPOSE (DECL_TEMPLATE_INFO (t
));
1363 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1365 for (; decls
; decls
= TREE_CHAIN (decls
))
1366 if (TREE_TYPE (TREE_VALUE (decls
)) == type
1367 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1368 return TREE_VALUE (decls
);
1371 /* We do NOT check for matching decls pushed separately at this
1372 point, as they may not represent instantiations of this
1373 template, and in any case are considered separate under the
1378 TREE_TYPE (r
) = type
;
1381 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1382 DECL_CLASS_CONTEXT (r
) = ctx
;
1384 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
1385 IDENTIFIER_POINTER (DECL_NAME (r
)),
1386 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1388 /* Type-conversion operator. Reconstruct the name, in
1389 case it's the name of one of the template's parameters. */
1390 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
1393 arg_types
= TYPE_VALUES (type
);
1395 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
1396 arg_types
= hash_tree_chain
1397 (build_pointer_type (DECL_CONTEXT (r
)), arg_types
);
1399 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
1401 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
1402 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
1403 buf
= (char *) alloca (strlen (dbuf
)
1404 + sizeof (DESTRUCTOR_DECL_PREFIX
));
1405 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
1408 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
1411 DECL_ASSEMBLER_NAME (r
)
1412 = build_decl_overload (DECL_NAME (r
), arg_types
, member
);
1414 make_decl_rtl (r
, NULL_PTR
, 1);
1416 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1417 DECL_MAIN_VARIANT (r
) = r
;
1418 DECL_RESULT (r
) = NULL_TREE
;
1419 DECL_INITIAL (r
) = NULL_TREE
;
1421 TREE_STATIC (r
) = 0;
1422 TREE_PUBLIC (r
) = 1;
1423 DECL_EXTERNAL (r
) = 1;
1424 DECL_INTERFACE_KNOWN (r
) = 0;
1425 DECL_DEFER_OUTPUT (r
) = 0;
1426 TREE_CHAIN (r
) = NULL_TREE
;
1427 DECL_CHAIN (r
) = NULL_TREE
;
1429 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
1430 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
1432 /* Look for matching decls for the moment. */
1435 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
1438 if (decls
== NULL_TREE
)
1440 else if (is_overloaded_fn (decls
))
1441 for (decls
= get_first_fn (decls
); decls
;
1442 decls
= DECL_CHAIN (decls
))
1444 if (TREE_CODE (decls
) == FUNCTION_DECL
1445 && TREE_TYPE (decls
) == type
)
1454 int dcl_only
= ! DECL_INITIAL (d
);
1456 DECL_INITIAL (r
) = error_mark_node
;
1457 duplicate_decls (r
, d
);
1460 DECL_INITIAL (r
) = 0;
1464 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1466 tree tmpl
= DECL_TI_TEMPLATE (t
);
1467 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1468 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1469 args
, nargs
, in_decl
);
1471 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1472 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1474 /* If we have a preexisting version of this function, don't expand
1475 the template version, use the other instead. */
1476 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
1477 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
1479 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1481 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) =
1482 tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1490 tree r
= copy_node (t
);
1491 TREE_TYPE (r
) = type
;
1492 DECL_INITIAL (r
) = TREE_TYPE (r
);
1493 DECL_CONTEXT (r
) = NULL_TREE
;
1494 #ifdef PROMOTE_PROTOTYPES
1495 if ((TREE_CODE (type
) == INTEGER_TYPE
1496 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1497 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
1498 DECL_ARG_TYPE (r
) = integer_type_node
;
1501 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1507 tree r
= copy_node (t
);
1508 TREE_TYPE (r
) = type
;
1511 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
1513 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1514 TREE_CHAIN (r
) = NULL_TREE
;
1520 tree r
= copy_node (t
);
1522 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1523 TREE_CHAIN (r
) = NULL_TREE
;
1530 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1532 /* Do we already have this instantiation? */
1533 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1535 tree tmpl
= DECL_TI_TEMPLATE (t
);
1536 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1538 for (; decls
; decls
= TREE_CHAIN (decls
))
1539 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1540 return TREE_VALUE (decls
);
1544 TREE_TYPE (r
) = type
;
1545 DECL_CONTEXT (r
) = ctx
;
1546 if (TREE_STATIC (r
))
1547 DECL_ASSEMBLER_NAME (r
)
1548 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
1549 DECL_INITIAL (r
) = tsubst_expr
1550 (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1555 if (DECL_LANG_SPECIFIC (r
))
1558 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
1561 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1563 tree tmpl
= DECL_TI_TEMPLATE (t
);
1564 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1565 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1566 args
, nargs
, in_decl
);
1568 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1569 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1570 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1572 TREE_CHAIN (r
) = NULL_TREE
;
1578 tree r
= copy_node (t
);
1579 TREE_TYPE (r
) = type
;
1580 DECL_CONTEXT (r
) = current_class_type
;
1581 TREE_CHAIN (r
) = NULL_TREE
;
1587 tree purpose
, value
, chain
, result
;
1588 int via_public
, via_virtual
, via_protected
;
1590 if (t
== void_list_node
)
1593 via_public
= TREE_VIA_PUBLIC (t
);
1594 via_protected
= TREE_VIA_PROTECTED (t
);
1595 via_virtual
= TREE_VIA_VIRTUAL (t
);
1597 purpose
= TREE_PURPOSE (t
);
1599 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1600 value
= TREE_VALUE (t
);
1602 value
= tsubst (value
, args
, nargs
, in_decl
);
1603 chain
= TREE_CHAIN (t
);
1604 if (chain
&& chain
!= void_type_node
)
1605 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1606 if (purpose
== TREE_PURPOSE (t
)
1607 && value
== TREE_VALUE (t
)
1608 && chain
== TREE_CHAIN (t
))
1610 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1611 purpose
, value
, chain
);
1612 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1616 if (type
!= NULL_TREE
)
1620 if (type
== TREE_TYPE (t
))
1623 TREE_TYPE (t
) = complete_type (type
);
1624 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
1625 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
1626 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
1627 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
1632 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1633 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1635 bzero ((char *) elts
, len
* sizeof (tree
));
1637 for (i
= 0; i
< len
; i
++)
1639 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1640 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1647 t
= make_tree_vec (len
);
1648 for (i
= 0; i
< len
; i
++)
1649 TREE_VEC_ELT (t
, i
) = elts
[i
];
1654 case REFERENCE_TYPE
:
1657 enum tree_code code
;
1658 if (type
== TREE_TYPE (t
))
1661 code
= TREE_CODE (t
);
1662 if (code
== POINTER_TYPE
)
1663 r
= build_pointer_type (type
);
1665 r
= build_reference_type (type
);
1666 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1667 /* Will this ever be needed for TYPE_..._TO values? */
1672 return build_offset_type
1673 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1677 tree values
= TYPE_ARG_TYPES (t
);
1678 tree context
= TYPE_CONTEXT (t
);
1681 /* Don't bother recursing if we know it won't change anything. */
1682 if (values
!= void_list_node
)
1684 /* This should probably be rewritten to use hash_tree_cons for
1685 the memory savings. */
1686 tree first
= NULL_TREE
;
1689 for (; values
&& values
!= void_list_node
;
1690 values
= TREE_CHAIN (values
))
1693 = tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
);
1694 tree purpose
= tsubst_expr (TREE_PURPOSE (values
),
1695 args
, nargs
, in_decl
);
1696 tree x
= build_tree_list (purpose
, value
);
1699 TREE_CHAIN (last
) = x
;
1705 if (values
== void_list_node
)
1706 TREE_CHAIN (last
) = void_list_node
;
1711 context
= tsubst (context
, args
, nargs
, in_decl
);
1712 /* Could also optimize cases where return value and
1713 values have common elements (e.g., T min(const &T, const T&). */
1715 /* If the above parameters haven't changed, just return the type. */
1716 if (type
== TREE_TYPE (t
)
1717 && values
== TYPE_VALUES (t
)
1718 && context
== TYPE_CONTEXT (t
))
1721 /* Construct a new type node and return it. */
1722 if (TREE_CODE (t
) == FUNCTION_TYPE
1723 && context
== NULL_TREE
)
1725 new_value
= build_function_type (type
, values
);
1727 else if (context
== NULL_TREE
)
1729 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1730 args
, nargs
, in_decl
);
1731 new_value
= build_cplus_method_type (base
, type
,
1732 TREE_CHAIN (values
));
1736 new_value
= make_node (TREE_CODE (t
));
1737 TREE_TYPE (new_value
) = type
;
1738 TYPE_CONTEXT (new_value
) = context
;
1739 TYPE_VALUES (new_value
) = values
;
1740 TYPE_SIZE (new_value
) = TYPE_SIZE (t
);
1741 TYPE_ALIGN (new_value
) = TYPE_ALIGN (t
);
1742 TYPE_MODE (new_value
) = TYPE_MODE (t
);
1743 if (TYPE_METHOD_BASETYPE (t
))
1744 TYPE_METHOD_BASETYPE (new_value
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1745 args
, nargs
, in_decl
);
1746 /* Need to generate hash value. */
1747 my_friendly_abort (84);
1749 new_value
= build_type_variant (new_value
,
1756 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1758 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1760 r
= build_cplus_array_type (type
, domain
);
1766 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1767 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1768 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1772 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1773 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1777 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1778 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
1779 return cp_build_type_variant
1780 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
1781 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
1785 return make_pointer_declarator
1786 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1789 return make_reference_declarator
1790 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1793 return build_parse_node
1794 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1795 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1798 return build_parse_node
1799 (CALL_EXPR
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1800 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
), 0);
1803 return build_parse_node
1804 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1805 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1808 sorry ("use of `%s' in template",
1809 tree_code_name
[(int) TREE_CODE (t
)]);
1810 return error_mark_node
;
1817 emit_line_note (input_filename
, lineno
);
1821 expand_start_bindings (0);
1829 expand_end_bindings (getdecls (), kept_level_p (), 1);
1830 t
= poplevel (kept_level_p (), 1, 0);
1836 tsubst_copy (t
, args
, nargs
, in_decl
)
1841 enum tree_code code
;
1843 if (t
== NULL_TREE
|| t
== error_mark_node
)
1846 code
= TREE_CODE (t
);
1851 return do_identifier (DECL_NAME (t
), 0);
1855 if (DECL_CONTEXT (t
))
1857 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1858 if (ctx
!= DECL_CONTEXT (t
))
1859 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
1865 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1866 t
= tsubst (t
, args
, nargs
, in_decl
);
1871 case IDENTIFIER_NODE
:
1872 return do_identifier (t
, 0);
1876 case REINTERPRET_CAST_EXPR
:
1878 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
1879 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1882 case PREDECREMENT_EXPR
:
1883 case PREINCREMENT_EXPR
:
1884 case POSTDECREMENT_EXPR
:
1885 case POSTINCREMENT_EXPR
:
1887 case TRUTH_NOT_EXPR
:
1889 case CONVERT_EXPR
: /* Unary + */
1895 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1900 case TRUNC_DIV_EXPR
:
1902 case FLOOR_DIV_EXPR
:
1903 case ROUND_DIV_EXPR
:
1904 case EXACT_DIV_EXPR
:
1906 case BIT_ANDTC_EXPR
:
1909 case TRUNC_MOD_EXPR
:
1910 case FLOOR_MOD_EXPR
:
1911 case TRUTH_ANDIF_EXPR
:
1912 case TRUTH_ORIF_EXPR
:
1913 case TRUTH_AND_EXPR
:
1934 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1935 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1939 tree fn
= TREE_OPERAND (t
, 0);
1940 if (really_overloaded_fn (fn
))
1941 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
1943 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
1945 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1949 case METHOD_CALL_EXPR
:
1951 tree name
= TREE_OPERAND (t
, 0);
1952 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1954 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1955 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
1957 else if (TREE_CODE (name
) == SCOPE_REF
1958 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
1960 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1961 name
= TREE_OPERAND (name
, 1);
1962 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1963 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
1964 name
= build_nt (SCOPE_REF
, base
, name
);
1967 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
1969 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1970 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
1977 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1978 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1979 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1984 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1985 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1986 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1987 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
1994 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1995 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1996 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
1997 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
2003 tree purpose
, value
, chain
;
2005 if (t
== void_list_node
)
2008 purpose
= TREE_PURPOSE (t
);
2010 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
2011 value
= TREE_VALUE (t
);
2013 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
2014 chain
= TREE_CHAIN (t
);
2015 if (chain
&& chain
!= void_type_node
)
2016 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
2017 if (purpose
== TREE_PURPOSE (t
)
2018 && value
== TREE_VALUE (t
)
2019 && chain
== TREE_CHAIN (t
))
2021 return tree_cons (purpose
, value
, chain
);
2028 case TEMPLATE_TYPE_PARM
:
2029 case TEMPLATE_CONST_PARM
:
2031 case REFERENCE_TYPE
:
2037 return tsubst (t
, args
, nargs
, in_decl
);
2045 tsubst_expr (t
, args
, nargs
, in_decl
)
2050 if (t
== NULL_TREE
|| t
== error_mark_node
)
2053 if (current_template_parms
)
2054 return tsubst_copy (t
, args
, nargs
, in_decl
);
2056 switch (TREE_CODE (t
))
2059 lineno
= TREE_COMPLEXITY (t
);
2060 emit_line_note (input_filename
, lineno
);
2062 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2067 lineno
= TREE_COMPLEXITY (t
);
2068 emit_line_note (input_filename
, lineno
);
2069 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2070 /* Do default conversion if safe and possibly important,
2071 in case within ({...}). */
2072 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
2073 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
2074 t
= default_conversion (t
);
2075 cplus_expand_expr_stmt (t
);
2082 int i
= suspend_momentary ();
2085 lineno
= TREE_COMPLEXITY (t
);
2086 emit_line_note (input_filename
, lineno
);
2088 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2089 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2090 TREE_OPERAND (t
, 3) != 0,
2091 tsubst (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2093 (dcl
, tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
),
2094 NULL_TREE
, 1, LOOKUP_ONLYCONVERTING
);
2095 resume_momentary (i
);
2102 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
2103 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2104 int cond_scope
= (TREE_OPERAND (t
, 1)
2105 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
2107 lineno
= TREE_COMPLEXITY (t
);
2108 emit_line_note (input_filename
, lineno
);
2111 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
2112 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2114 emit_line_note (input_filename
, lineno
);
2115 expand_start_loop_continue_elsewhere (1);
2119 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2120 emit_line_note (input_filename
, lineno
);
2122 expand_exit_loop_if_false (0, condition_conversion (tmp
));
2126 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
2129 emit_line_note (input_filename
, lineno
);
2130 expand_loop_continue_here ();
2131 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
2133 cplus_expand_expr_stmt (tmp
);
2146 lineno
= TREE_COMPLEXITY (t
);
2148 emit_line_note (input_filename
, lineno
);
2149 expand_start_loop (1);
2151 cond
= TREE_OPERAND (t
, 0);
2152 if (TREE_CODE (cond
) == DECL_STMT
)
2154 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
2155 emit_line_note (input_filename
, lineno
);
2156 expand_exit_loop_if_false (0, condition_conversion (cond
));
2158 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
2160 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2172 lineno
= TREE_COMPLEXITY (t
);
2174 emit_line_note (input_filename
, lineno
);
2175 expand_start_loop_continue_elsewhere (1);
2177 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2178 expand_loop_continue_here ();
2180 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2181 emit_line_note (input_filename
, lineno
);
2182 expand_exit_loop_if_false (0, condition_conversion (cond
));
2193 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2195 lineno
= TREE_COMPLEXITY (t
);
2198 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2199 emit_line_note (input_filename
, lineno
);
2200 expand_start_cond (condition_conversion (tmp
), 0);
2202 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2203 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2205 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
2207 expand_start_else ();
2208 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2222 tree substmt
= TREE_OPERAND (t
, 0);
2224 lineno
= TREE_COMPLEXITY (t
);
2226 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2229 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
2230 tsubst_expr (substmt
, args
, nargs
, in_decl
);
2232 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2238 lineno
= TREE_COMPLEXITY (t
);
2239 emit_line_note (input_filename
, lineno
);
2240 if (! expand_exit_something ())
2241 error ("break statement not within loop or switch");
2247 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2249 lineno
= TREE_COMPLEXITY (t
);
2252 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2253 emit_line_note (input_filename
, lineno
);
2254 c_expand_start_case (val
);
2257 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2258 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2260 expand_end_case (val
);
2271 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2272 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2276 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
2283 lineno
= TREE_COMPLEXITY (t
);
2284 emit_line_note (input_filename
, lineno
);
2285 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
2287 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
2288 TREE_USED (decl
) = 1;
2292 expand_computed_goto
2293 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2297 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
2303 instantiate_template (tmpl
, targ_ptr
)
2304 tree tmpl
, *targ_ptr
;
2308 struct obstack
*old_fmp_obstack
;
2309 extern struct obstack
*function_maybepermanent_obstack
;
2311 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2312 old_fmp_obstack
= function_maybepermanent_obstack
;
2313 function_maybepermanent_obstack
= &permanent_obstack
;
2315 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
2316 len
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
2321 tree t
= targ_ptr
[i
];
2322 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
2324 tree nt
= target_type (t
);
2325 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
2327 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
2328 cp_error (" trying to instantiate `%D'", tmpl
);
2329 fndecl
= error_mark_node
;
2333 targ_ptr
[i
] = copy_to_permanent (t
);
2336 /* substitute template parameters */
2337 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
2340 function_maybepermanent_obstack
= old_fmp_obstack
;
2346 /* Push the name of the class template into the scope of the instantiation. */
2349 overload_template_name (type
)
2352 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
2355 if (IDENTIFIER_CLASS_VALUE (id
)
2356 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
2359 decl
= build_decl (TYPE_DECL
, id
, type
);
2360 SET_DECL_ARTIFICIAL (decl
);
2361 pushdecl_class_level (decl
);
2364 /* Type unification.
2366 We have a function template signature with one or more references to
2367 template parameters, and a parameter list we wish to fit to this
2368 template. If possible, produce a list of parameters for the template
2369 which will cause it to fit the supplied parameter list.
2371 Return zero for success, 2 for an incomplete match that doesn't resolve
2372 all the types, and 1 for complete failure. An error message will be
2373 printed only for an incomplete match.
2375 TPARMS[NTPARMS] is an array of template parameter types;
2376 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2377 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2378 and ARGS is the argument list we're trying to match against it.
2380 If SUBR is 1, we're being called recursively (to unify the arguments of
2381 a function or method parameter of a function template), so don't zero
2382 out targs and don't fail on an incomplete match. */
2385 type_unification (tparms
, targs
, parms
, args
, nsubsts
, subr
)
2386 tree tparms
, *targs
, parms
, args
;
2391 int ntparms
= TREE_VEC_LENGTH (tparms
);
2393 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
2394 my_friendly_assert (TREE_CODE (parms
) == TREE_LIST
, 290);
2395 /* ARGS could be NULL (via a call from parse.y to
2396 build_x_function_call). */
2398 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
2399 my_friendly_assert (ntparms
> 0, 292);
2402 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
2405 && parms
!= void_list_node
2407 && args
!= void_list_node
)
2409 parm
= TREE_VALUE (parms
);
2410 parms
= TREE_CHAIN (parms
);
2411 arg
= TREE_VALUE (args
);
2412 args
= TREE_CHAIN (args
);
2414 if (arg
== error_mark_node
)
2416 if (arg
== unknown_type_node
)
2419 if (! uses_template_parms (parm
)
2420 && TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2422 if (can_convert_arg (parm
, TREE_TYPE (arg
), arg
))
2428 if (TREE_CODE (arg
) == VAR_DECL
)
2429 arg
= TREE_TYPE (arg
);
2430 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
2431 arg
= TREE_TYPE (arg
);
2433 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2435 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
2436 if (TREE_CODE (arg
) == TREE_LIST
2437 && TREE_TYPE (arg
) == unknown_type_node
2438 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
2440 int nsubsts
, ntparms
;
2443 /* Have to back unify here */
2444 arg
= TREE_VALUE (arg
);
2446 ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg
));
2447 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2448 parm
= tree_cons (NULL_TREE
, parm
, NULL_TREE
);
2449 return type_unification (DECL_TEMPLATE_PARMS (arg
), targs
,
2450 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
2453 arg
= TREE_TYPE (arg
);
2456 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2457 arg
= TREE_TYPE (arg
);
2459 if (TREE_CODE (parm
) != REFERENCE_TYPE
)
2461 if (TREE_CODE (arg
) == FUNCTION_TYPE
2462 || TREE_CODE (arg
) == METHOD_TYPE
)
2463 arg
= build_pointer_type (arg
);
2464 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
2465 arg
= build_pointer_type (TREE_TYPE (arg
));
2467 arg
= TYPE_MAIN_VARIANT (arg
);
2470 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
))
2478 /* Fail if we've reached the end of the parm list, and more args
2479 are present, and the parm list isn't variadic. */
2480 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
2482 /* Fail if parms are left and they don't have default values. */
2484 && parms
!= void_list_node
2485 && TREE_PURPOSE (parms
) == NULL_TREE
)
2488 for (i
= 0; i
< ntparms
; i
++)
2491 error ("incomplete type unification");
2497 /* Tail recursion is your friend. */
2499 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
)
2500 tree tparms
, *targs
, parm
, arg
;
2501 int *nsubsts
, ntparms
;
2505 /* I don't think this will do the right thing with respect to types.
2506 But the only case I've seen it in so far has been array bounds, where
2507 signedness is the only information lost, and I think that will be
2509 while (TREE_CODE (parm
) == NOP_EXPR
)
2510 parm
= TREE_OPERAND (parm
, 0);
2512 if (arg
== error_mark_node
)
2514 if (arg
== unknown_type_node
)
2519 switch (TREE_CODE (parm
))
2521 case TEMPLATE_TYPE_PARM
:
2523 idx
= TEMPLATE_TYPE_IDX (parm
);
2525 /* Template type parameters cannot contain cv-quals; i.e.
2526 template <class T> void f (T& a, T& b) will not generate
2527 void f (const int& a, const int& b). */
2528 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
2529 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
2531 arg
= TYPE_MAIN_VARIANT (arg
);
2534 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
2535 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
2536 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
2539 /* Simple cases: Value already set, does match or doesn't. */
2540 if (targs
[idx
] == arg
)
2542 else if (targs
[idx
])
2544 /* Check for mixed types and values. */
2545 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
2549 case TEMPLATE_CONST_PARM
:
2551 idx
= TEMPLATE_CONST_IDX (parm
);
2552 if (targs
[idx
] == arg
)
2554 else if (targs
[idx
])
2556 tree t
= targs
[idx
];
2557 if (TREE_CODE (t
) == TREE_CODE (arg
))
2558 switch (TREE_CODE (arg
))
2561 if (tree_int_cst_equal (t
, arg
))
2565 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t
), TREE_REAL_CST (arg
)))
2568 /* STRING_CST values are not valid template const parms. */
2572 my_friendly_abort (87);
2575 /* else if (typeof arg != tparms[idx])
2578 targs
[idx
] = copy_to_permanent (arg
);
2582 if (TREE_CODE (arg
) != POINTER_TYPE
)
2584 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2587 case REFERENCE_TYPE
:
2588 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2589 arg
= TREE_TYPE (arg
);
2590 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
, nsubsts
);
2593 if (TREE_CODE (arg
) != ARRAY_TYPE
)
2595 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
2598 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2603 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2606 if (TREE_CODE (parm
) == INTEGER_TYPE
)
2608 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
2609 && unify (tparms
, targs
, ntparms
,
2610 TYPE_MIN_VALUE (parm
), TYPE_MIN_VALUE (arg
), nsubsts
))
2612 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
2613 && unify (tparms
, targs
, ntparms
,
2614 TYPE_MAX_VALUE (parm
), TYPE_MAX_VALUE (arg
), nsubsts
))
2617 /* As far as unification is concerned, this wins. Later checks
2618 will invalidate it if necessary. */
2621 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2623 if (TREE_CODE (arg
) != INTEGER_CST
)
2625 return !tree_int_cst_equal (parm
, arg
);
2630 t1
= TREE_OPERAND (parm
, 0);
2631 t2
= TREE_OPERAND (parm
, 1);
2632 return unify (tparms
, targs
, ntparms
, t1
,
2633 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
2640 if (TREE_CODE (arg
) != TREE_VEC
)
2642 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
2644 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
2645 if (unify (tparms
, targs
, ntparms
,
2646 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
2653 if (TYPE_PTRMEMFUNC_FLAG (parm
))
2654 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
2657 /* Allow trivial conversions. */
2658 if (TREE_CODE (arg
) != RECORD_TYPE
2659 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
2660 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
2663 if (CLASSTYPE_TEMPLATE_INFO (parm
) && CLASSTYPE_TEMPLATE_INFO (arg
)
2664 && uses_template_parms (parm
))
2666 if (CLASSTYPE_TI_TEMPLATE (parm
) != CLASSTYPE_TI_TEMPLATE (arg
))
2668 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
2669 CLASSTYPE_TI_ARGS (arg
), nsubsts
);
2671 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
2676 if (TREE_CODE (arg
) != METHOD_TYPE
)
2681 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
2684 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2685 TREE_TYPE (arg
), nsubsts
))
2687 return type_unification (tparms
, targs
, TYPE_ARG_TYPES (parm
),
2688 TYPE_ARG_TYPES (arg
), nsubsts
, 1);
2691 if (TREE_CODE (arg
) != OFFSET_TYPE
)
2693 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
2694 TYPE_OFFSET_BASETYPE (arg
), nsubsts
))
2696 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2697 TREE_TYPE (arg
), nsubsts
);
2700 sorry ("use of `%s' in template type unification",
2701 tree_code_name
[(int) TREE_CODE (parm
)]);
2707 mark_decl_instantiated (result
, extern_p
)
2711 if (DECL_TEMPLATE_INSTANTIATION (result
))
2712 SET_DECL_EXPLICIT_INSTANTIATION (result
);
2713 TREE_PUBLIC (result
) = 1;
2717 DECL_INTERFACE_KNOWN (result
) = 1;
2718 DECL_NOT_REALLY_EXTERN (result
) = 1;
2722 /* called from the parser. */
2724 do_function_instantiation (declspecs
, declarator
, storage
)
2725 tree declspecs
, declarator
, storage
;
2727 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0,
2728 NULL_TREE
, NULL_TREE
);
2731 tree result
= NULL_TREE
;
2734 if (! DECL_LANG_SPECIFIC (decl
))
2736 cp_error ("explicit instantiation of non-template `%#D'", decl
);
2740 /* If we've already seen this template instance, use it. */
2741 if (DECL_FUNCTION_MEMBER_P (decl
))
2743 if (DECL_TEMPLATE_INSTANTIATION (decl
))
2745 else if (name
= DECL_ASSEMBLER_NAME (decl
),
2746 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
2747 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
2750 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
2752 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
2753 if (decls_match (fn
, decl
)
2754 && DECL_DEFER_OUTPUT (fn
))
2759 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2761 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn
));
2762 tree
*targs
= (tree
*) malloc (sizeof (tree
) * ntparms
);
2764 i
= type_unification (DECL_TEMPLATE_PARMS (fn
), targs
,
2765 TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2766 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
2771 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
2773 result
= instantiate_template (fn
, targs
);
2780 cp_error ("no matching template for `%D' found", decl
);
2784 if (flag_external_templates
)
2787 if (storage
== NULL_TREE
)
2789 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2792 cp_error ("storage class `%D' applied to template instantiation",
2795 mark_decl_instantiated (result
, extern_p
);
2796 repo_template_instantiated (result
, extern_p
);
2798 instantiate_decl (result
);
2802 mark_class_instantiated (t
, extern_p
)
2806 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
2807 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
2808 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
2809 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
2810 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
2813 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2814 rest_of_type_compilation (t
, 1);
2819 do_type_instantiation (name
, storage
)
2822 tree t
= TREE_TYPE (name
);
2829 /* With -fexternal-templates, explicit instantiations are treated the same
2830 as implicit ones. */
2831 if (flag_external_templates
)
2834 if (TYPE_SIZE (t
) == NULL_TREE
)
2836 cp_error ("explicit instantiation of `%#T' before definition of template",
2841 if (storage
== NULL_TREE
)
2843 else if (storage
== ridpointers
[(int) RID_INLINE
])
2845 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2847 else if (storage
== ridpointers
[(int) RID_STATIC
])
2851 cp_error ("storage class `%D' applied to template instantiation",
2856 /* We've already instantiated this. */
2857 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
2861 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
2863 mark_class_instantiated (t
, extern_p
);
2864 repo_template_instantiated (t
, extern_p
);
2874 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2875 if (DECL_TEMPLATE_INSTANTIATION (tmp
))
2877 mark_decl_instantiated (tmp
, extern_p
);
2878 repo_template_instantiated (tmp
, extern_p
);
2880 instantiate_decl (tmp
);
2883 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2884 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
2886 mark_decl_instantiated (tmp
, extern_p
);
2887 repo_template_instantiated (tmp
, extern_p
);
2889 instantiate_decl (tmp
);
2892 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2893 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
2894 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
2899 instantiate_decl (d
)
2902 tree ti
= DECL_TEMPLATE_INFO (d
);
2903 tree tmpl
= TI_TEMPLATE (ti
);
2904 tree args
= TI_ARGS (ti
);
2906 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
2908 int nested
= in_function_p ();
2910 int pattern_defined
;
2912 if (TREE_CODE (d
) == FUNCTION_DECL
)
2914 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
2915 pattern_defined
= (DECL_INITIAL (pattern
) != NULL_TREE
);
2919 d_defined
= ! DECL_IN_AGGR_P (d
);
2920 pattern_defined
= ! DECL_IN_AGGR_P (pattern
);
2925 else if (pattern_defined
)
2927 repo_template_used (d
);
2929 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
2931 if (flag_alt_external_templates
)
2933 if (interface_unknown
)
2934 warn_if_unknown_interface (d
);
2936 else if (DECL_INTERFACE_KNOWN (pattern
))
2938 DECL_INTERFACE_KNOWN (d
) = 1;
2939 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (pattern
);
2942 warn_if_unknown_interface (pattern
);
2945 if (at_eof
&& ! DECL_INLINE (d
))
2946 import_export_decl (d
);
2949 if (! pattern_defined
2950 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! DECL_INLINE (d
)
2951 && (! DECL_INTERFACE_KNOWN (d
)
2952 || ! DECL_NOT_REALLY_EXTERN (d
)))
2953 /* Kludge: if we compile a constructor in the middle of processing a
2954 toplevel declaration, we blow away the declspecs in
2955 temp_decl_obstack when we call permanent_allocation in
2956 finish_function. So don't compile it yet. */
2957 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! nested
&& ! at_eof
))
2959 add_pending_template (d
);
2963 if (! push_tinst_level (d
))
2966 if (TREE_CODE (d
) == FUNCTION_DECL
&& nested
)
2967 push_cp_function_context (NULL_TREE
);
2968 push_to_top_level ();
2970 /* Trick tsubst into giving us a new decl in case the template changed. */
2971 save_ti
= DECL_TEMPLATE_INFO (pattern
);
2972 DECL_TEMPLATE_INFO (pattern
) = NULL_TREE
;
2973 td
= tsubst (pattern
, &TREE_VEC_ELT (args
, 0), TREE_VEC_LENGTH (args
), tmpl
);
2974 DECL_TEMPLATE_INFO (pattern
) = save_ti
;
2976 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
2977 if (TREE_CODE (d
) == FUNCTION_DECL
)
2978 DECL_INITIAL (td
) = error_mark_node
;
2979 duplicate_decls (td
, d
);
2980 if (TREE_CODE (d
) == FUNCTION_DECL
)
2981 DECL_INITIAL (td
) = 0;
2983 if (TREE_CODE (d
) == VAR_DECL
)
2985 DECL_IN_AGGR_P (d
) = 0;
2986 if (DECL_INTERFACE_KNOWN (d
))
2987 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
2990 DECL_EXTERNAL (d
) = 1;
2991 DECL_NOT_REALLY_EXTERN (d
) = 1;
2993 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
2995 else if (TREE_CODE (d
) == FUNCTION_DECL
)
2997 tree t
= DECL_SAVED_TREE (pattern
);
2999 char *file
= input_filename
;
3001 lineno
= DECL_SOURCE_LINE (d
);
3002 input_filename
= DECL_SOURCE_FILE (d
);
3004 start_function (NULL_TREE
, d
, NULL_TREE
, NULL_TREE
, 1);
3005 store_parm_decls ();
3007 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
3010 (TREE_OPERAND (t
, 0),
3011 tsubst_expr (TREE_OPERAND (t
, 1), &TREE_VEC_ELT (args
, 0),
3012 TREE_VEC_LENGTH (args
), tmpl
));
3016 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
3018 current_member_init_list
3019 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
3020 current_base_init_list
3021 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
3026 /* Always keep the BLOCK node associated with the outermost
3027 pair of curley braces of a function. These are needed
3028 for correct operation of dwarfout.c. */
3031 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
3032 tsubst_expr (t
, &TREE_VEC_ELT (args
, 0),
3033 TREE_VEC_LENGTH (args
), tmpl
);
3035 finish_function (lineno
, 0, nested
);
3038 input_filename
= file
;
3041 pop_from_top_level ();
3042 if (TREE_CODE (d
) == FUNCTION_DECL
&& nested
)
3043 pop_cp_function_context (NULL_TREE
);
3050 tsubst_chain (t
, argvec
)
3055 tree first
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3056 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3059 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
3061 tree x
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3062 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3063 TREE_CHAIN (last
) = x
;
3073 tsubst_expr_values (t
, argvec
)
3076 tree first
= NULL_TREE
;
3079 for (; t
; t
= TREE_CHAIN (t
))
3081 tree pur
= tsubst_copy (TREE_PURPOSE (t
), &TREE_VEC_ELT (argvec
, 0),
3082 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3083 tree val
= tsubst_expr (TREE_VALUE (t
), &TREE_VEC_ELT (argvec
, 0),
3084 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3085 *p
= build_tree_list (pur
, val
);
3086 p
= &TREE_CHAIN (*p
);
3097 last_tree
= TREE_CHAIN (last_tree
) = t
;