1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Known bugs or deficiencies include:
23 * templates for class static data don't work (methods only)
24 * duplicated method templates can crash the compiler
25 * interface/impl data is taken from file defining the template
26 * all methods must be provided in header files; can't use a source
27 file that contains only the method templates and "just win"
43 extern struct obstack permanent_obstack
;
46 extern char *input_filename
;
47 struct pending_inline
*pending_template_expansions
;
49 tree current_template_parms
;
50 HOST_WIDE_INT processing_template_decl
;
52 tree pending_templates
;
53 static tree
*template_tail
= &pending_templates
;
55 int minimal_parse_mode
;
57 #define obstack_chunk_alloc xmalloc
58 #define obstack_chunk_free free
62 void pop_template_decls ();
64 tree
classtype_mangled_name ();
65 static char * mangle_class_name_for_template ();
66 tree
tsubst_expr_values ();
68 /* We've got a template header coming up; push to a new level for storing
72 begin_template_parm_list ()
75 declare_pseudo_global_level ();
78 /* Process information from new template parameter NEXT and append it to the
81 process_template_parm (list
, next
)
89 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
90 defval
= TREE_PURPOSE (parm
);
91 parm
= TREE_VALUE (parm
);
92 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
96 tree p
= TREE_VALUE (tree_last (list
));
98 if (TREE_CODE (p
) == TYPE_DECL
)
99 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
101 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
110 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
111 /* is a const-param */
112 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
113 PARM
, 0, NULL_TREE
, NULL_TREE
);
114 /* A template parameter is not modifiable. */
115 TREE_READONLY (parm
) = 1;
116 if (IS_AGGR_TYPE (TREE_TYPE (parm
)))
118 sorry ("aggregate template parameter types");
119 TREE_TYPE (parm
) = void_type_node
;
121 tinfo
= make_node (TEMPLATE_CONST_PARM
);
122 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
123 if (TREE_PERMANENT (parm
) == 0)
125 parm
= copy_node (parm
);
126 TREE_PERMANENT (parm
) = 1;
128 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
129 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
130 DECL_INITIAL (decl
) = tinfo
;
131 DECL_INITIAL (parm
) = tinfo
;
132 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
+ 1);
136 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
137 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
138 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
139 TYPE_MAIN_DECL (t
) = decl
;
141 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
+ 1);
143 SET_DECL_ARTIFICIAL (decl
);
145 parm
= build_tree_list (defval
, parm
);
146 return chainon (list
, parm
);
149 /* The end of a template parameter list has been reached. Process the
150 tree list into a parameter vector, converting each parameter into a more
151 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
155 end_template_parm_list (parms
)
160 tree saved_parmlist
= make_tree_vec (list_length (parms
));
162 ++processing_template_decl
;
163 current_template_parms
164 = tree_cons (build_int_2 (0, processing_template_decl
),
165 saved_parmlist
, current_template_parms
);
167 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
168 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
170 return saved_parmlist
;
173 /* end_template_decl is called after a template declaration is seen. */
178 /* This matches the pushlevel in begin_template_parm_list. */
181 --processing_template_decl
;
182 current_template_parms
= TREE_CHAIN (current_template_parms
);
183 (void) get_pending_sizes (); /* Why? */
187 push_template_decl (decl
)
190 tree header
= current_template_parms
;
192 tree args
= NULL_TREE
;
194 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
198 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
199 && DECL_CLASS_CONTEXT (decl
))
201 /* Note that this template is a "primary template" */
202 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
203 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
208 tree a
= copy_node (TREE_VALUE (header
));
209 int i
= TREE_VEC_LENGTH (a
);
210 TREE_TYPE (a
) = NULL_TREE
;
213 tree t
= TREE_VALUE (TREE_VEC_ELT (a
, i
));
214 if (TREE_CODE (t
) == TYPE_DECL
)
217 t
= DECL_INITIAL (t
);
218 TREE_VEC_ELT (a
, i
) = t
;
220 args
= tree_cons (TREE_PURPOSE (header
), a
, args
);
221 header
= TREE_CHAIN (header
);
223 args
= nreverse (args
);
224 args
= TREE_VALUE (args
);
226 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
228 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
229 DECL_TEMPLATE_PARMS (tmpl
) = TREE_VALUE (current_template_parms
);
230 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
234 if (TREE_CODE (decl
) == TYPE_DECL
)
235 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
237 tmpl
= DECL_TI_TEMPLATE (decl
);
240 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
241 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
244 tmpl
= pushdecl_top_level (tmpl
);
247 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl
)) = tmpl
;
249 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
251 if (TREE_CODE (decl
) == TYPE_DECL
)
253 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
254 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
256 else if (! DECL_LANG_SPECIFIC (decl
))
257 cp_error ("template declaration of `%#D'", decl
);
259 DECL_TEMPLATE_INFO (decl
) = info
;
262 tree tsubst
PROTO ((tree
, tree
*, int, tree
));
264 /* Convert all template arguments to their appropriate types, and return
265 a vector containing the resulting values. If any error occurs, return
268 coerce_template_parms (parms
, arglist
, in_decl
)
272 int nparms
, nargs
, i
, lost
= 0;
275 if (arglist
== NULL_TREE
)
277 else if (TREE_CODE (arglist
) == TREE_VEC
)
278 nargs
= TREE_VEC_LENGTH (arglist
);
280 nargs
= list_length (arglist
);
282 nparms
= TREE_VEC_LENGTH (parms
);
286 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
288 error ("incorrect number of parameters (%d, should be %d)",
291 cp_error_at ("in template expansion for decl `%D'", in_decl
);
292 return error_mark_node
;
295 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
296 vec
= copy_node (arglist
);
299 vec
= make_tree_vec (nparms
);
300 for (i
= 0; i
< nparms
; i
++)
307 arglist
= TREE_CHAIN (arglist
);
309 if (arg
== error_mark_node
)
312 arg
= TREE_VALUE (arg
);
315 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
316 &TREE_VEC_ELT (vec
, 0), i
, in_decl
);
318 TREE_VEC_ELT (vec
, i
) = arg
;
321 for (i
= 0; i
< nparms
; i
++)
323 tree arg
= TREE_VEC_ELT (vec
, i
);
324 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
326 int is_type
, requires_type
;
328 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
329 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
331 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
332 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
334 cp_pedwarn ("to refer to a type member of a template parameter,");
335 cp_pedwarn (" use `typename %E'", arg
);
336 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
337 TREE_OPERAND (arg
, 1));
340 if (is_type
!= requires_type
)
344 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
347 cp_error (" expected a constant of type `%T', got `%T'",
348 TREE_TYPE (parm
), arg
);
350 cp_error (" expected a type, got `%E'", arg
);
353 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
358 val
= groktypename (arg
);
359 if (! current_template_parms
)
361 tree t
= target_type (val
);
363 && decl_function_context (TYPE_MAIN_DECL (t
)))
365 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
366 return error_mark_node
;
372 tree t
= tsubst (TREE_TYPE (parm
), &TREE_VEC_ELT (vec
, 0),
373 TREE_VEC_LENGTH (vec
), in_decl
);
374 if (current_template_parms
)
377 val
= digest_init (t
, arg
, (tree
*) 0);
379 if (val
== error_mark_node
|| current_template_parms
)
382 /* 14.2: Other template-arguments must be constant-expressions,
383 addresses of objects or functions with external linkage, or of
384 static class members. */
385 else if (!TREE_CONSTANT (val
))
387 cp_error ("non-const `%E' cannot be used as template argument",
389 val
= error_mark_node
;
391 else if (POINTER_TYPE_P (TREE_TYPE (val
))
392 && ! integer_zerop (val
)
393 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
394 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
398 if (TREE_CODE (t
) == ADDR_EXPR
)
400 tree a
= TREE_OPERAND (t
, 0);
402 if (TREE_CODE (a
) == STRING_CST
)
404 cp_error ("string literal %E is not a valid template argument", a
);
405 error ("because it is the address of an object with static linkage");
406 val
= error_mark_node
;
408 else if (TREE_CODE (a
) != VAR_DECL
409 && TREE_CODE (a
) != FUNCTION_DECL
)
411 else if (! DECL_PUBLIC (a
))
413 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
414 val
= error_mark_node
;
420 cp_error ("`%E' is not a valid template argument", t
);
421 error ("it must be %s%s with external linkage",
422 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
423 ? "a pointer to " : "",
424 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
425 ? "a function" : "an object");
426 val
= error_mark_node
;
431 if (val
== error_mark_node
)
434 TREE_VEC_ELT (vec
, i
) = val
;
437 return error_mark_node
;
442 comp_template_args (oldargs
, newargs
)
443 tree oldargs
, newargs
;
447 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
449 tree nt
= TREE_VEC_ELT (newargs
, i
);
450 tree ot
= TREE_VEC_ELT (oldargs
, i
);
454 if (TREE_CODE (nt
) != TREE_CODE (ot
))
456 if (TREE_CODE (ot
) == TEMPLATE_TYPE_PARM
457 && comptypes (ot
, nt
, 1))
459 if (TREE_CODE (ot
) == TEMPLATE_CONST_PARM
460 && TEMPLATE_CONST_IDX (nt
) == TEMPLATE_CONST_IDX (ot
))
467 /* Given class template name and parameter list, produce a user-friendly name
468 for the instantiation. */
470 mangle_class_name_for_template (name
, parms
, arglist
)
474 static struct obstack scratch_obstack
;
475 static char *scratch_firstobj
;
478 if (!scratch_firstobj
)
480 gcc_obstack_init (&scratch_obstack
);
481 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
484 obstack_free (&scratch_obstack
, scratch_firstobj
);
487 #define buflen sizeof(buf)
488 #define check if (bufp >= buf+buflen-1) goto too_long
489 #define ccat(c) *bufp++=(c); check
490 #define advance bufp+=strlen(bufp); check
491 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
494 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
496 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
501 nparms
= TREE_VEC_LENGTH (parms
);
502 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
503 for (i
= 0; i
< nparms
; i
++)
505 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
506 tree arg
= TREE_VEC_ELT (arglist
, i
);
511 if (TREE_CODE (parm
) == TYPE_DECL
)
513 cat (type_as_string (arg
, 0));
517 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
519 if (TREE_CODE (arg
) == TREE_LIST
)
521 /* New list cell was built because old chain link was in
523 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
524 arg
= TREE_VALUE (arg
);
526 /* No need to check arglist against parmlist here; we did that
527 in coerce_template_parms, called from lookup_template_class. */
528 cat (expr_as_string (arg
, 0));
531 char *bufp
= obstack_next_free (&scratch_obstack
);
533 while (bufp
[offset
- 1] == ' ')
535 obstack_blank_fast (&scratch_obstack
, offset
);
537 /* B<C<char> >, not B<C<char>> */
538 if (bufp
[offset
- 1] == '>')
543 return (char *) obstack_base (&scratch_obstack
);
548 fatal ("out of (preallocated) string space creating template instantiation name");
554 classtype_mangled_name (t
)
557 if (CLASSTYPE_TEMPLATE_INFO (t
)
558 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
560 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
561 char *mangled_name
= mangle_class_name_for_template
562 (IDENTIFIER_POINTER (name
),
563 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
564 CLASSTYPE_TI_ARGS (t
));
565 tree id
= get_identifier (mangled_name
);
566 IDENTIFIER_TEMPLATE (id
) = name
;
570 return TYPE_IDENTIFIER (t
);
574 add_pending_template (d
)
577 if (TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)))
580 *template_tail
= perm_tree_cons
581 (current_function_decl
, d
, NULL_TREE
);
582 template_tail
= &TREE_CHAIN (*template_tail
);
583 TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)) = 1;
586 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
587 parameters, find the desired type.
589 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
590 Since ARGLIST is build on the decl_obstack, we must copy it here
591 to keep it from being reclaimed when the decl storage is reclaimed.
593 IN_DECL, if non-NULL, is the template declaration we are trying to
596 lookup_template_class (d1
, arglist
, in_decl
)
600 tree
template, parmlist
;
605 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
607 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
609 template = IDENTIFIER_CLASS_VALUE (d1
);
611 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
613 template = CLASSTYPE_TI_TEMPLATE (d1
);
614 d1
= DECL_NAME (template);
617 my_friendly_abort (272);
619 /* With something like `template <class T> class X class X { ... };'
620 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
621 We don't want to do that, but we have to deal with the situation, so
622 let's give them some syntax errors to chew on instead of a crash. */
624 return error_mark_node
;
625 if (TREE_CODE (template) != TEMPLATE_DECL
)
627 cp_error ("non-template type `%T' used as a template", d1
);
629 cp_error_at ("for template declaration `%D'", in_decl
);
630 return error_mark_node
;
633 if (TREE_CODE (TREE_TYPE (template)) == RECORD_TYPE
)
634 code_type_node
= (CLASSTYPE_DECLARED_CLASS (TREE_TYPE (template))
635 ? class_type_node
: record_type_node
);
637 code_type_node
= union_type_node
;
639 if (PRIMARY_TEMPLATE_P (template))
641 parmlist
= DECL_TEMPLATE_PARMS (template);
643 arglist
= coerce_template_parms (parmlist
, arglist
, template);
644 if (arglist
== error_mark_node
)
645 return error_mark_node
;
646 if (uses_template_parms (arglist
))
649 if (comp_template_args
650 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
651 found
= TREE_TYPE (template);
654 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
655 found
; found
= TREE_CHAIN (found
))
657 if (TI_USES_TEMPLATE_PARMS (found
)
658 && comp_template_args (TREE_PURPOSE (found
), arglist
))
662 found
= TREE_VALUE (found
);
667 if (can_free (&permanent_obstack
, arglist
))
668 obstack_free (&permanent_obstack
, arglist
);
673 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
675 id
= get_identifier (mangled_name
);
676 IDENTIFIER_TEMPLATE (id
) = d1
;
678 maybe_push_to_top_level (uses_template_parms (arglist
));
679 t
= xref_tag (code_type_node
, id
, NULL_TREE
, 1);
680 pop_from_top_level ();
684 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
687 arglist
= CLASSTYPE_TI_ARGS (ctx
);
689 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
691 tree save_parms
= current_template_parms
;
692 current_template_parms
= NULL_TREE
;
693 t
= xref_tag (code_type_node
, id
, NULL_TREE
, 0);
694 current_template_parms
= save_parms
;
698 t
= lookup_nested_type_by_name (ctx
, id
);
699 my_friendly_assert (t
!= NULL_TREE
, 42);
703 /* Seems to be wanted. */
704 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
706 if (! CLASSTYPE_TEMPLATE_INFO (t
))
708 arglist
= copy_to_permanent (arglist
);
709 CLASSTYPE_TEMPLATE_INFO (t
)
710 = perm_tree_cons (template, arglist
, NULL_TREE
);
711 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
712 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
713 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
714 = uses_template_parms (arglist
);
716 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
718 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
719 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
720 if (! uses_template_parms (arglist
))
721 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
722 = get_identifier (build_overload_name (t
, 1, 1));
724 if (flag_external_templates
&& ! uses_template_parms (arglist
)
725 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
726 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
730 instantiate_class_template (t
);
732 for (d
= TYPE_METHODS (t
); d
; d
= TREE_CHAIN (d
))
733 add_pending_template (d
);
740 /* Should be defined in parse.h. */
744 uses_template_parms (t
)
749 switch (TREE_CODE (t
))
753 /* We assume that the object must be instantiated in order to build
754 the COMPONENT_REF, so we test only whether the type of the
755 COMPONENT_REF uses template parms. */
756 return uses_template_parms (TREE_TYPE (t
));
758 case IDENTIFIER_NODE
:
759 if (!IDENTIFIER_TEMPLATE (t
))
761 my_friendly_abort (42);
763 /* aggregates of tree nodes */
766 int i
= TREE_VEC_LENGTH (t
);
768 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
773 if (uses_template_parms (TREE_PURPOSE (t
))
774 || uses_template_parms (TREE_VALUE (t
)))
776 return uses_template_parms (TREE_CHAIN (t
));
778 /* constructed type nodes */
781 return uses_template_parms (TREE_TYPE (t
));
783 if (TYPE_PTRMEMFUNC_FLAG (t
))
784 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
786 if (! CLASSTYPE_TEMPLATE_INFO (t
))
788 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
790 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
792 return uses_template_parms (TREE_TYPE (t
));
794 if (uses_template_parms (TYPE_DOMAIN (t
)))
796 return uses_template_parms (TREE_TYPE (t
));
798 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
800 return uses_template_parms (TREE_TYPE (t
));
802 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
804 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
806 return uses_template_parms (TREE_TYPE (t
));
810 return uses_template_parms (TREE_TYPE (t
));
814 /* ??? What about FIELD_DECLs? */
815 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
816 && uses_template_parms (DECL_TI_ARGS (t
)))
821 if (uses_template_parms (TREE_TYPE (t
)))
823 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
828 return uses_template_parms (TREE_TYPE (t
));
830 return uses_template_parms (TREE_OPERAND (t
, 0));
832 /* template parm nodes */
833 case TEMPLATE_TYPE_PARM
:
834 case TEMPLATE_CONST_PARM
:
837 /* simple type nodes */
839 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
841 return uses_template_parms (TYPE_MAX_VALUE (t
));
856 /* Non-error_mark_node ERROR_MARKs are bad things. */
857 my_friendly_assert (t
== error_mark_node
, 274);
866 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
867 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
868 /* else fall through */
871 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
879 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
880 if (uses_template_parms (TREE_OPERAND (t
, i
)))
887 sorry ("testing %s for template parms",
888 tree_code_name
[(int) TREE_CODE (t
)]);
889 my_friendly_abort (82);
895 static struct tinst_level
*current_tinst_level
= 0;
896 static struct tinst_level
*free_tinst_level
= 0;
897 static int tinst_depth
= 0;
898 int max_tinst_depth
= 17;
899 #ifdef GATHER_STATISTICS
900 int depth_reached
= 0;
907 struct tinst_level
*new;
909 if (tinst_depth
>= max_tinst_depth
)
911 struct tinst_level
*p
= current_tinst_level
;
913 char *file
= input_filename
;
915 error ("template instantiation depth exceeds maximum of %d",
917 cp_error (" instantiating `%D'", d
);
919 for (; p
; p
= p
->next
)
921 cp_error (" instantiated from `%D'", p
->decl
);
923 input_filename
= p
->file
;
925 error (" instantiated from here");
928 input_filename
= file
;
933 if (free_tinst_level
)
935 new = free_tinst_level
;
936 free_tinst_level
= new->next
;
939 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
943 new->file
= input_filename
;
944 new->next
= current_tinst_level
;
945 current_tinst_level
= new;
948 #ifdef GATHER_STATISTICS
949 if (tinst_depth
> depth_reached
)
950 depth_reached
= tinst_depth
;
959 struct tinst_level
*old
= current_tinst_level
;
961 current_tinst_level
= old
->next
;
962 old
->next
= free_tinst_level
;
963 free_tinst_level
= old
;
970 struct tinst_level
*p
= current_tinst_level
;
973 for (; p
->next
; p
= p
->next
)
979 instantiate_class_template (type
)
982 tree
template, template_info
, args
, pattern
, t
, *field_chain
, *tag_chain
;
984 if (type
== error_mark_node
)
985 return error_mark_node
;
987 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
989 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
992 template = TI_TEMPLATE (template_info
);
993 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
994 args
= TI_ARGS (template_info
);
995 pattern
= TREE_TYPE (template);
997 if (TYPE_SIZE (pattern
) == NULL_TREE
)
1000 TYPE_BEING_DEFINED (type
) = 1;
1002 if (! push_tinst_level (type
))
1005 maybe_push_to_top_level (uses_template_parms (type
));
1006 pushclass (type
, 0);
1008 if (flag_external_templates
)
1010 if (flag_alt_external_templates
)
1012 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1013 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1014 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1015 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1016 && CLASSTYPE_INTERFACE_KNOWN (type
);
1020 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1021 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1022 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1023 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1024 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1025 && CLASSTYPE_INTERFACE_KNOWN (type
);
1030 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1031 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1035 tree binfo
= TYPE_BINFO (type
);
1036 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1042 int len
= TREE_VEC_LENGTH (pbases
);
1043 BINFO_BASETYPES (binfo
) = bases
= make_tree_vec (len
);
1044 for (i
= 0; i
< len
; ++i
)
1048 TREE_VEC_ELT (bases
, i
) = elt
1049 = tsubst (TREE_VEC_ELT (pbases
, i
), &TREE_VEC_ELT (args
, 0),
1050 TREE_VEC_LENGTH (args
), NULL_TREE
);
1051 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1053 if (! uses_template_parms (type
) &&
1054 TYPE_SIZE (complete_type (TREE_TYPE (elt
))) == NULL_TREE
)
1055 cp_error ("base class `%T' of `%T' has incomplete type",
1056 TREE_TYPE (elt
), type
);
1061 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1063 field_chain
= &TYPE_FIELDS (type
);
1064 tag_chain
= &CLASSTYPE_TAGS (type
);
1066 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1068 tree name
= TREE_PURPOSE (t
);
1069 tree tag
= TREE_VALUE (t
);
1072 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1073 newtag
= start_enum (name
);
1075 newtag
= tsubst (tag
, &TREE_VEC_ELT (args
, 0),
1076 TREE_VEC_LENGTH (args
), NULL_TREE
);
1078 *tag_chain
= build_tree_list (name
, newtag
);
1079 tag_chain
= &TREE_CHAIN (*tag_chain
);
1081 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1083 tree e
, values
= NULL_TREE
, *last
= &values
;
1085 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
1087 tree elt
= build_enumerator
1089 tsubst_expr (TREE_VALUE (e
), &TREE_VEC_ELT (args
, 0),
1090 TREE_VEC_LENGTH (args
), NULL_TREE
));
1091 DECL_FIELD_CONTEXT (TREE_VALUE (elt
)) = type
;
1093 last
= &TREE_CHAIN (elt
);
1096 finish_enum (newtag
, values
);
1098 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
1099 while (*field_chain
)
1100 field_chain
= &TREE_CHAIN (*field_chain
);
1104 /* Don't replace enum constants here. */
1105 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1106 if (TREE_CODE (t
) != CONST_DECL
)
1108 tree r
= tsubst (t
, &TREE_VEC_ELT (args
, 0),
1109 TREE_VEC_LENGTH (args
), NULL_TREE
);
1110 if (TREE_CODE (r
) == VAR_DECL
)
1112 if (! uses_template_parms (r
))
1113 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1114 /* Perhaps I should do more of grokfield here. */
1116 DECL_IN_AGGR_P (r
) = 1;
1117 DECL_EXTERNAL (r
) = 1;
1118 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1122 field_chain
= &TREE_CHAIN (r
);
1125 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1127 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1128 = tsubst_chain (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)), args
);
1129 CLASSTYPE_FRIEND_CLASSES (type
)
1130 = tsubst_chain (CLASSTYPE_FRIEND_CLASSES (pattern
), args
);
1133 tree d
= tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args
, 0),
1134 TREE_VEC_LENGTH (args
), NULL_TREE
);
1136 for (; d
; d
= TREE_CHAIN (d
))
1137 pushdecl (TREE_VALUE (d
));
1140 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1141 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1142 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1143 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1144 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1145 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1146 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1147 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1148 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1149 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1150 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1151 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1152 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1153 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1154 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1155 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1156 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1157 TYPE_USES_COMPLEX_INHERITANCE (type
)
1158 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1159 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1160 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1162 if (! uses_template_parms (type
))
1165 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1166 if (TREE_CODE (tmp
) == FIELD_DECL
1167 && TREE_CODE (DECL_NAME (tmp
)) != SCOPE_REF
)
1168 require_complete_type (tmp
);
1170 /* XXX handle attributes */
1171 type
= finish_struct_1 (type
, 0, 0);
1175 TYPE_SIZE (type
) = integer_zero_node
;
1176 CLASSTYPE_METHOD_VEC (type
)
1177 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1180 TYPE_BEING_DEFINED (type
) = 0;
1183 pop_from_top_level ();
1193 if (t1
== NULL_TREE
)
1194 return t2
== NULL_TREE
;
1195 if (t2
== NULL_TREE
)
1197 /* Don't care if one declares its arg const and the other doesn't -- the
1198 main variant of the arg type is all that matters. */
1199 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1200 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1202 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1206 lookup_nested_type_by_name (ctype
, name
)
1211 complete_type (ctype
);
1213 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1215 if (name
== TREE_PURPOSE (t
))
1216 return TREE_VALUE (t
);
1222 tsubst (t
, args
, nargs
, in_decl
)
1229 if (t
== NULL_TREE
|| t
== error_mark_node
1230 || t
== integer_type_node
1231 || t
== void_type_node
1232 || t
== char_type_node
)
1235 type
= TREE_TYPE (t
);
1236 if (type
== unknown_type_node
)
1237 my_friendly_abort (42);
1238 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
)
1239 type
= tsubst (type
, args
, nargs
, in_decl
);
1241 switch (TREE_CODE (t
))
1244 if (TYPE_PTRMEMFUNC_P (t
))
1246 tree r
= build_ptrmemfunc_type
1247 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1248 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1252 /* else fall through */
1254 if (uses_template_parms (t
))
1256 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
1257 tree r
= lookup_template_class (t
, argvec
, in_decl
);
1258 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1262 /* else fall through */
1264 case IDENTIFIER_NODE
:
1276 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1277 if (ctx
== NULL_TREE
)
1280 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
1284 if (t
== integer_type_node
)
1287 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1288 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1292 tree max
= tsubst_expr (TYPE_MAX_VALUE (t
), args
, nargs
, in_decl
);
1293 if (current_template_parms
)
1295 tree itype
= make_node (INTEGER_TYPE
);
1296 TYPE_MIN_VALUE (itype
) = size_zero_node
;
1297 TYPE_MAX_VALUE (itype
) = max
;
1300 return build_index_2_type (size_zero_node
, max
);
1303 case TEMPLATE_TYPE_PARM
:
1305 tree arg
= args
[TEMPLATE_TYPE_IDX (t
)];
1306 return cp_build_type_variant
1307 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1308 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1311 case TEMPLATE_CONST_PARM
:
1312 return args
[TEMPLATE_CONST_IDX (t
)];
1317 tree arg_types
, ctx
;
1321 if (DECL_CONTEXT (t
) != NULL_TREE
1322 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1324 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
1328 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1329 type
= tsubst (type
, args
, nargs
, in_decl
);
1335 type
= tsubst (type
, args
, nargs
, in_decl
);
1338 if (type
== TREE_TYPE (t
)
1339 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
1342 /* Do we already have this instantiation? */
1343 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1345 tree tmpl
= TREE_PURPOSE (DECL_TEMPLATE_INFO (t
));
1346 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1348 for (; decls
; decls
= TREE_CHAIN (decls
))
1349 if (TREE_TYPE (TREE_VALUE (decls
)) == type
1350 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1351 return TREE_VALUE (decls
);
1354 /* We do NOT check for matching decls pushed separately at this
1355 point, as they may not represent instantiations of this
1356 template, and in any case are considered separate under the
1361 TREE_TYPE (r
) = type
;
1364 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1365 DECL_CLASS_CONTEXT (r
) = ctx
;
1367 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
1368 IDENTIFIER_POINTER (DECL_NAME (r
)),
1369 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1371 /* Type-conversion operator. Reconstruct the name, in
1372 case it's the name of one of the template's parameters. */
1373 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
1376 arg_types
= TYPE_VALUES (type
);
1378 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
1379 arg_types
= hash_tree_chain
1380 (build_pointer_type (DECL_CONTEXT (r
)), arg_types
);
1382 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
1384 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
1385 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
1386 buf
= (char *) alloca (strlen (dbuf
)
1387 + sizeof (DESTRUCTOR_DECL_PREFIX
));
1388 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
1391 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
1394 DECL_ASSEMBLER_NAME (r
)
1395 = build_decl_overload (DECL_NAME (r
), arg_types
, member
);
1397 make_decl_rtl (r
, NULL_PTR
, 1);
1399 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1400 DECL_MAIN_VARIANT (r
) = r
;
1401 DECL_RESULT (r
) = NULL_TREE
;
1402 DECL_INITIAL (r
) = NULL_TREE
;
1404 TREE_STATIC (r
) = 0;
1405 TREE_PUBLIC (r
) = 1;
1406 DECL_EXTERNAL (r
) = 1;
1407 DECL_INTERFACE_KNOWN (r
) = 0;
1408 DECL_DEFER_OUTPUT (r
) = 0;
1409 TREE_CHAIN (r
) = NULL_TREE
;
1410 DECL_CHAIN (r
) = NULL_TREE
;
1412 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
1413 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
1415 /* Look for matching decls for the moment. */
1418 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
1421 if (decls
== NULL_TREE
)
1423 else if (is_overloaded_fn (decls
))
1424 for (decls
= get_first_fn (decls
); decls
;
1425 decls
= DECL_CHAIN (decls
))
1427 if (TREE_CODE (decls
) == FUNCTION_DECL
1428 && TREE_TYPE (decls
) == type
)
1437 int dcl_only
= ! DECL_INITIAL (d
);
1439 DECL_INITIAL (r
) = error_mark_node
;
1440 duplicate_decls (r
, d
);
1443 DECL_INITIAL (r
) = 0;
1447 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1449 tree tmpl
= DECL_TI_TEMPLATE (t
);
1450 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1451 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1452 args
, nargs
, in_decl
);
1454 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1455 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1457 /* If we have a preexisting version of this function, don't expand
1458 the template version, use the other instead. */
1459 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
1460 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
1462 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1464 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) =
1465 tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1473 tree r
= copy_node (t
);
1474 TREE_TYPE (r
) = type
;
1475 DECL_INITIAL (r
) = TREE_TYPE (r
);
1476 DECL_CONTEXT (r
) = NULL_TREE
;
1477 #ifdef PROMOTE_PROTOTYPES
1478 if ((TREE_CODE (type
) == INTEGER_TYPE
1479 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1480 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
1481 DECL_ARG_TYPE (r
) = integer_type_node
;
1484 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1490 tree r
= copy_node (t
);
1491 TREE_TYPE (r
) = type
;
1494 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
1496 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1497 TREE_CHAIN (r
) = NULL_TREE
;
1503 tree r
= copy_node (t
);
1505 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1506 TREE_CHAIN (r
) = NULL_TREE
;
1513 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1515 /* Do we already have this instantiation? */
1516 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1518 tree tmpl
= DECL_TI_TEMPLATE (t
);
1519 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1521 for (; decls
; decls
= TREE_CHAIN (decls
))
1522 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1523 return TREE_VALUE (decls
);
1527 TREE_TYPE (r
) = type
;
1528 DECL_CONTEXT (r
) = ctx
;
1529 if (TREE_STATIC (r
))
1530 DECL_ASSEMBLER_NAME (r
)
1531 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
1532 DECL_INITIAL (r
) = tsubst_expr
1533 (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1538 if (DECL_LANG_SPECIFIC (r
))
1541 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
1544 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1546 tree tmpl
= DECL_TI_TEMPLATE (t
);
1547 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1548 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1549 args
, nargs
, in_decl
);
1551 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1552 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1553 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1555 TREE_CHAIN (r
) = NULL_TREE
;
1561 tree r
= copy_node (t
);
1562 TREE_TYPE (r
) = type
;
1563 set_nested_typename (r
, current_class_name
, DECL_NAME (r
), type
);
1564 TREE_CHAIN (r
) = NULL_TREE
;
1570 tree purpose
, value
, chain
, result
;
1571 int via_public
, via_virtual
, via_protected
;
1573 if (t
== void_list_node
)
1576 via_public
= TREE_VIA_PUBLIC (t
);
1577 via_protected
= TREE_VIA_PROTECTED (t
);
1578 via_virtual
= TREE_VIA_VIRTUAL (t
);
1580 purpose
= TREE_PURPOSE (t
);
1582 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1583 value
= TREE_VALUE (t
);
1585 value
= tsubst (value
, args
, nargs
, in_decl
);
1586 chain
= TREE_CHAIN (t
);
1587 if (chain
&& chain
!= void_type_node
)
1588 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1589 if (purpose
== TREE_PURPOSE (t
)
1590 && value
== TREE_VALUE (t
)
1591 && chain
== TREE_CHAIN (t
))
1593 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1594 purpose
, value
, chain
);
1595 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1599 if (type
!= NULL_TREE
)
1603 if (type
== TREE_TYPE (t
))
1606 TREE_TYPE (t
) = complete_type (type
);
1607 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
1608 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
1609 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
1610 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
1615 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1616 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1618 bzero ((char *) elts
, len
* sizeof (tree
));
1620 for (i
= 0; i
< len
; i
++)
1622 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1623 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1630 t
= make_tree_vec (len
);
1631 for (i
= 0; i
< len
; i
++)
1632 TREE_VEC_ELT (t
, i
) = elts
[i
];
1637 case REFERENCE_TYPE
:
1640 enum tree_code code
;
1641 if (type
== TREE_TYPE (t
))
1644 code
= TREE_CODE (t
);
1645 if (code
== POINTER_TYPE
)
1646 r
= build_pointer_type (type
);
1648 r
= build_reference_type (type
);
1649 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1650 /* Will this ever be needed for TYPE_..._TO values? */
1655 return build_offset_type
1656 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1660 tree values
= TYPE_ARG_TYPES (t
);
1661 tree context
= TYPE_CONTEXT (t
);
1664 /* Don't bother recursing if we know it won't change anything. */
1665 if (values
!= void_list_node
)
1667 /* This should probably be rewritten to use hash_tree_cons for
1668 the memory savings. */
1669 tree first
= NULL_TREE
;
1672 for (; values
&& values
!= void_list_node
;
1673 values
= TREE_CHAIN (values
))
1676 = tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
);
1677 tree purpose
= tsubst_expr (TREE_PURPOSE (values
),
1678 args
, nargs
, in_decl
);
1679 tree x
= build_tree_list (purpose
, value
);
1682 TREE_CHAIN (last
) = x
;
1688 if (values
== void_list_node
)
1689 TREE_CHAIN (last
) = void_list_node
;
1694 context
= tsubst (context
, args
, nargs
, in_decl
);
1695 /* Could also optimize cases where return value and
1696 values have common elements (e.g., T min(const &T, const T&). */
1698 /* If the above parameters haven't changed, just return the type. */
1699 if (type
== TREE_TYPE (t
)
1700 && values
== TYPE_VALUES (t
)
1701 && context
== TYPE_CONTEXT (t
))
1704 /* Construct a new type node and return it. */
1705 if (TREE_CODE (t
) == FUNCTION_TYPE
1706 && context
== NULL_TREE
)
1708 new_value
= build_function_type (type
, values
);
1710 else if (context
== NULL_TREE
)
1712 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1713 args
, nargs
, in_decl
);
1714 new_value
= build_cplus_method_type (base
, type
,
1715 TREE_CHAIN (values
));
1719 new_value
= make_node (TREE_CODE (t
));
1720 TREE_TYPE (new_value
) = type
;
1721 TYPE_CONTEXT (new_value
) = context
;
1722 TYPE_VALUES (new_value
) = values
;
1723 TYPE_SIZE (new_value
) = TYPE_SIZE (t
);
1724 TYPE_ALIGN (new_value
) = TYPE_ALIGN (t
);
1725 TYPE_MODE (new_value
) = TYPE_MODE (t
);
1726 if (TYPE_METHOD_BASETYPE (t
))
1727 TYPE_METHOD_BASETYPE (new_value
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1728 args
, nargs
, in_decl
);
1729 /* Need to generate hash value. */
1730 my_friendly_abort (84);
1732 new_value
= build_type_variant (new_value
,
1739 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1741 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1743 r
= build_cplus_array_type (type
, domain
);
1749 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1750 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1751 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1755 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1756 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1760 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1761 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
1762 return cp_build_type_variant
1763 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
1764 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
1768 return make_pointer_declarator
1769 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1772 return make_reference_declarator
1773 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1776 return build_parse_node
1777 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1778 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1781 return build_parse_node
1782 (CALL_EXPR
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1783 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
), 0);
1786 sorry ("use of `%s' in template",
1787 tree_code_name
[(int) TREE_CODE (t
)]);
1788 return error_mark_node
;
1795 emit_line_note (input_filename
, lineno
);
1799 expand_start_bindings (0);
1807 expand_end_bindings (getdecls (), kept_level_p (), 1);
1808 t
= poplevel (kept_level_p (), 1, 0);
1814 tsubst_copy (t
, args
, nargs
, in_decl
)
1819 enum tree_code code
;
1821 if (t
== NULL_TREE
|| t
== error_mark_node
)
1824 code
= TREE_CODE (t
);
1829 return do_identifier (DECL_NAME (t
), 0);
1833 if (DECL_CONTEXT (t
))
1835 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1836 if (ctx
!= DECL_CONTEXT (t
))
1837 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
1843 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1844 t
= tsubst (t
, args
, nargs
, in_decl
);
1849 case IDENTIFIER_NODE
:
1850 return do_identifier (t
, 0);
1854 case REINTERPRET_CAST_EXPR
:
1856 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
1857 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1860 case PREDECREMENT_EXPR
:
1861 case PREINCREMENT_EXPR
:
1862 case POSTDECREMENT_EXPR
:
1863 case POSTINCREMENT_EXPR
:
1865 case TRUTH_NOT_EXPR
:
1867 case CONVERT_EXPR
: /* Unary + */
1872 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1877 case TRUNC_DIV_EXPR
:
1879 case FLOOR_DIV_EXPR
:
1880 case ROUND_DIV_EXPR
:
1881 case EXACT_DIV_EXPR
:
1883 case BIT_ANDTC_EXPR
:
1886 case TRUNC_MOD_EXPR
:
1887 case FLOOR_MOD_EXPR
:
1888 case TRUTH_ANDIF_EXPR
:
1889 case TRUTH_ORIF_EXPR
:
1890 case TRUTH_AND_EXPR
:
1911 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1912 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1916 tree fn
= TREE_OPERAND (t
, 0);
1917 if (really_overloaded_fn (fn
))
1918 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
1920 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
1922 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1926 case METHOD_CALL_EXPR
:
1928 tree name
= TREE_OPERAND (t
, 0);
1929 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1931 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1932 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
1934 else if (TREE_CODE (name
) == SCOPE_REF
1935 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
1937 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1938 name
= TREE_OPERAND (name
, 1);
1939 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1940 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
1941 name
= build_nt (SCOPE_REF
, base
, name
);
1944 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
1946 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1947 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
1954 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1955 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1956 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1961 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1962 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1963 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1964 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
1971 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1972 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1973 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
1974 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
1980 tree purpose
, value
, chain
;
1982 if (t
== void_list_node
)
1985 purpose
= TREE_PURPOSE (t
);
1987 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
1988 value
= TREE_VALUE (t
);
1990 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
1991 chain
= TREE_CHAIN (t
);
1992 if (chain
&& chain
!= void_type_node
)
1993 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
1994 if (purpose
== TREE_PURPOSE (t
)
1995 && value
== TREE_VALUE (t
)
1996 && chain
== TREE_CHAIN (t
))
1998 return tree_cons (purpose
, value
, chain
);
2005 case TEMPLATE_TYPE_PARM
:
2006 case TEMPLATE_CONST_PARM
:
2008 case REFERENCE_TYPE
:
2014 return tsubst (t
, args
, nargs
, in_decl
);
2022 tsubst_expr (t
, args
, nargs
, in_decl
)
2027 if (t
== NULL_TREE
|| t
== error_mark_node
)
2030 if (current_template_parms
)
2031 return tsubst_copy (t
, args
, nargs
, in_decl
);
2033 switch (TREE_CODE (t
))
2036 lineno
= TREE_COMPLEXITY (t
);
2037 emit_line_note (input_filename
, lineno
);
2039 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2044 lineno
= TREE_COMPLEXITY (t
);
2045 emit_line_note (input_filename
, lineno
);
2046 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2047 /* Do default conversion if safe and possibly important,
2048 in case within ({...}). */
2049 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
2050 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
2051 t
= default_conversion (t
);
2052 cplus_expand_expr_stmt (t
);
2059 int i
= suspend_momentary ();
2062 lineno
= TREE_COMPLEXITY (t
);
2063 emit_line_note (input_filename
, lineno
);
2065 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2066 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2067 TREE_OPERAND (t
, 3) != 0,
2068 tsubst (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2070 (dcl
, tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
),
2071 NULL_TREE
, 1, LOOKUP_ONLYCONVERTING
);
2072 resume_momentary (i
);
2079 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
2080 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2081 int cond_scope
= (TREE_OPERAND (t
, 1)
2082 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
2084 lineno
= TREE_COMPLEXITY (t
);
2085 emit_line_note (input_filename
, lineno
);
2088 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2090 emit_line_note (input_filename
, lineno
);
2091 expand_start_loop_continue_elsewhere (1);
2095 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2096 emit_line_note (input_filename
, lineno
);
2098 expand_exit_loop_if_false (0, condition_conversion (tmp
));
2102 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
2105 emit_line_note (input_filename
, lineno
);
2106 expand_loop_continue_here ();
2107 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
2109 cplus_expand_expr_stmt (tmp
);
2122 lineno
= TREE_COMPLEXITY (t
);
2124 emit_line_note (input_filename
, lineno
);
2125 expand_start_loop (1);
2127 cond
= TREE_OPERAND (t
, 0);
2128 if (TREE_CODE (cond
) == DECL_STMT
)
2130 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
2131 emit_line_note (input_filename
, lineno
);
2132 expand_exit_loop_if_false (0, condition_conversion (cond
));
2134 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
2136 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2148 lineno
= TREE_COMPLEXITY (t
);
2150 emit_line_note (input_filename
, lineno
);
2151 expand_start_loop_continue_elsewhere (1);
2153 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2154 expand_loop_continue_here ();
2156 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2157 emit_line_note (input_filename
, lineno
);
2158 expand_exit_loop_if_false (0, condition_conversion (cond
));
2169 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2171 lineno
= TREE_COMPLEXITY (t
);
2174 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2175 emit_line_note (input_filename
, lineno
);
2176 expand_start_cond (condition_conversion (tmp
), 0);
2178 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2179 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2181 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
2183 expand_start_else ();
2184 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2198 tree substmt
= TREE_OPERAND (t
, 0);
2200 lineno
= TREE_COMPLEXITY (t
);
2202 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2205 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
2206 tsubst_expr (substmt
, args
, nargs
, in_decl
);
2208 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2214 lineno
= TREE_COMPLEXITY (t
);
2215 emit_line_note (input_filename
, lineno
);
2216 if (! expand_exit_something ())
2217 error ("break statement not within loop or switch");
2223 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2225 lineno
= TREE_COMPLEXITY (t
);
2228 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2229 emit_line_note (input_filename
, lineno
);
2230 c_expand_start_case (val
);
2233 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2234 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2236 expand_end_case (val
);
2247 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2248 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2252 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
2259 lineno
= TREE_COMPLEXITY (t
);
2260 emit_line_note (input_filename
, lineno
);
2261 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
2263 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
2264 TREE_USED (decl
) = 1;
2268 expand_computed_goto
2269 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2273 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
2279 instantiate_template (tmpl
, targ_ptr
)
2280 tree tmpl
, *targ_ptr
;
2284 struct obstack
*old_fmp_obstack
;
2285 extern struct obstack
*function_maybepermanent_obstack
;
2287 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2288 old_fmp_obstack
= function_maybepermanent_obstack
;
2289 function_maybepermanent_obstack
= &permanent_obstack
;
2291 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
2292 len
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
2297 tree t
= targ_ptr
[i
];
2298 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
2300 tree nt
= target_type (t
);
2301 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
2303 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
2304 cp_error (" trying to instantiate `%D'", tmpl
);
2305 fndecl
= error_mark_node
;
2309 targ_ptr
[i
] = copy_to_permanent (t
);
2312 /* substitute template parameters */
2313 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
2316 function_maybepermanent_obstack
= old_fmp_obstack
;
2322 /* Push the name of the class template into the scope of the instantiation. */
2325 overload_template_name (type
)
2328 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
2331 if (IDENTIFIER_CLASS_VALUE (id
)
2332 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
2335 decl
= build_decl (TYPE_DECL
, id
, type
);
2336 SET_DECL_ARTIFICIAL (decl
);
2337 pushdecl_class_level (decl
);
2340 /* Type unification.
2342 We have a function template signature with one or more references to
2343 template parameters, and a parameter list we wish to fit to this
2344 template. If possible, produce a list of parameters for the template
2345 which will cause it to fit the supplied parameter list.
2347 Return zero for success, 2 for an incomplete match that doesn't resolve
2348 all the types, and 1 for complete failure. An error message will be
2349 printed only for an incomplete match.
2351 TPARMS[NTPARMS] is an array of template parameter types;
2352 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2353 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2354 and ARGS is the argument list we're trying to match against it.
2356 If SUBR is 1, we're being called recursively (to unify the arguments of
2357 a function or method parameter of a function template), so don't zero
2358 out targs and don't fail on an incomplete match. */
2361 type_unification (tparms
, targs
, parms
, args
, nsubsts
, subr
)
2362 tree tparms
, *targs
, parms
, args
;
2367 int ntparms
= TREE_VEC_LENGTH (tparms
);
2369 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
2370 my_friendly_assert (TREE_CODE (parms
) == TREE_LIST
, 290);
2371 /* ARGS could be NULL (via a call from parse.y to
2372 build_x_function_call). */
2374 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
2375 my_friendly_assert (ntparms
> 0, 292);
2378 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
2381 && parms
!= void_list_node
2383 && args
!= void_list_node
)
2385 parm
= TREE_VALUE (parms
);
2386 parms
= TREE_CHAIN (parms
);
2387 arg
= TREE_VALUE (args
);
2388 args
= TREE_CHAIN (args
);
2390 if (arg
== error_mark_node
)
2392 if (arg
== unknown_type_node
)
2395 if (! uses_template_parms (parm
)
2396 && TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2398 if (can_convert_arg (parm
, TREE_TYPE (arg
), arg
))
2404 if (TREE_CODE (arg
) == VAR_DECL
)
2405 arg
= TREE_TYPE (arg
);
2406 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
2407 arg
= TREE_TYPE (arg
);
2409 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2411 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
2412 if (TREE_CODE (arg
) == TREE_LIST
2413 && TREE_TYPE (arg
) == unknown_type_node
2414 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
2416 int nsubsts
, ntparms
;
2419 /* Have to back unify here */
2420 arg
= TREE_VALUE (arg
);
2422 ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg
));
2423 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2424 parm
= tree_cons (NULL_TREE
, parm
, NULL_TREE
);
2425 return type_unification (DECL_TEMPLATE_PARMS (arg
), targs
,
2426 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
2429 arg
= TREE_TYPE (arg
);
2432 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2433 arg
= TREE_TYPE (arg
);
2435 if (TREE_CODE (parm
) != REFERENCE_TYPE
)
2437 if (TREE_CODE (arg
) == FUNCTION_TYPE
2438 || TREE_CODE (arg
) == METHOD_TYPE
)
2439 arg
= build_pointer_type (arg
);
2440 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
2441 arg
= build_pointer_type (TREE_TYPE (arg
));
2443 arg
= TYPE_MAIN_VARIANT (arg
);
2446 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
))
2454 /* Fail if we've reached the end of the parm list, and more args
2455 are present, and the parm list isn't variadic. */
2456 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
2458 /* Fail if parms are left and they don't have default values. */
2460 && parms
!= void_list_node
2461 && TREE_PURPOSE (parms
) == NULL_TREE
)
2464 for (i
= 0; i
< ntparms
; i
++)
2467 error ("incomplete type unification");
2473 /* Tail recursion is your friend. */
2475 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
)
2476 tree tparms
, *targs
, parm
, arg
;
2477 int *nsubsts
, ntparms
;
2481 /* I don't think this will do the right thing with respect to types.
2482 But the only case I've seen it in so far has been array bounds, where
2483 signedness is the only information lost, and I think that will be
2485 while (TREE_CODE (parm
) == NOP_EXPR
)
2486 parm
= TREE_OPERAND (parm
, 0);
2488 if (arg
== error_mark_node
)
2490 if (arg
== unknown_type_node
)
2495 switch (TREE_CODE (parm
))
2497 case TEMPLATE_TYPE_PARM
:
2499 idx
= TEMPLATE_TYPE_IDX (parm
);
2501 /* Template type parameters cannot contain cv-quals; i.e.
2502 template <class T> void f (T& a, T& b) will not generate
2503 void f (const int& a, const int& b). */
2504 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
2505 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
2507 arg
= TYPE_MAIN_VARIANT (arg
);
2510 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
2511 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
2512 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
2515 /* Simple cases: Value already set, does match or doesn't. */
2516 if (targs
[idx
] == arg
)
2518 else if (targs
[idx
])
2520 /* Check for mixed types and values. */
2521 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
2525 case TEMPLATE_CONST_PARM
:
2527 idx
= TEMPLATE_CONST_IDX (parm
);
2528 if (targs
[idx
] == arg
)
2530 else if (targs
[idx
])
2532 tree t
= targs
[idx
];
2533 if (TREE_CODE (t
) == TREE_CODE (arg
))
2534 switch (TREE_CODE (arg
))
2537 if (tree_int_cst_equal (t
, arg
))
2541 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t
), TREE_REAL_CST (arg
)))
2544 /* STRING_CST values are not valid template const parms. */
2548 my_friendly_abort (87);
2551 /* else if (typeof arg != tparms[idx])
2554 targs
[idx
] = copy_to_permanent (arg
);
2558 if (TREE_CODE (arg
) != POINTER_TYPE
)
2560 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2563 case REFERENCE_TYPE
:
2564 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2565 arg
= TREE_TYPE (arg
);
2566 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
, nsubsts
);
2569 if (TREE_CODE (arg
) != ARRAY_TYPE
)
2571 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
2574 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2579 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2582 if (TREE_CODE (parm
) == INTEGER_TYPE
)
2584 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
2585 && unify (tparms
, targs
, ntparms
,
2586 TYPE_MIN_VALUE (parm
), TYPE_MIN_VALUE (arg
), nsubsts
))
2588 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
2589 && unify (tparms
, targs
, ntparms
,
2590 TYPE_MAX_VALUE (parm
), TYPE_MAX_VALUE (arg
), nsubsts
))
2593 /* As far as unification is concerned, this wins. Later checks
2594 will invalidate it if necessary. */
2597 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2599 if (TREE_CODE (arg
) != INTEGER_CST
)
2601 return !tree_int_cst_equal (parm
, arg
);
2606 t1
= TREE_OPERAND (parm
, 0);
2607 t2
= TREE_OPERAND (parm
, 1);
2608 return unify (tparms
, targs
, ntparms
, t1
,
2609 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
2616 if (TREE_CODE (arg
) != TREE_VEC
)
2618 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
2620 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
2621 if (unify (tparms
, targs
, ntparms
,
2622 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
2629 if (TYPE_PTRMEMFUNC_FLAG (parm
))
2630 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
2633 /* Allow trivial conversions. */
2634 if (TREE_CODE (arg
) != RECORD_TYPE
2635 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
2636 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
2639 if (CLASSTYPE_TEMPLATE_INFO (parm
) && CLASSTYPE_TEMPLATE_INFO (arg
)
2640 && uses_template_parms (parm
))
2642 if (CLASSTYPE_TI_TEMPLATE (parm
) != CLASSTYPE_TI_TEMPLATE (arg
))
2644 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
2645 CLASSTYPE_TI_ARGS (arg
), nsubsts
);
2647 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
2652 if (TREE_CODE (arg
) != METHOD_TYPE
)
2657 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
2660 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2661 TREE_TYPE (arg
), nsubsts
))
2663 return type_unification (tparms
, targs
, TYPE_ARG_TYPES (parm
),
2664 TYPE_ARG_TYPES (arg
), nsubsts
, 1);
2667 if (TREE_CODE (arg
) != OFFSET_TYPE
)
2669 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
2670 TYPE_OFFSET_BASETYPE (arg
), nsubsts
))
2672 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2673 TREE_TYPE (arg
), nsubsts
);
2676 sorry ("use of `%s' in template type unification",
2677 tree_code_name
[(int) TREE_CODE (parm
)]);
2683 mark_decl_instantiated (result
, extern_p
)
2687 if (DECL_TEMPLATE_INSTANTIATION (result
))
2688 SET_DECL_EXPLICIT_INSTANTIATION (result
);
2689 TREE_PUBLIC (result
) = 1;
2693 DECL_INTERFACE_KNOWN (result
) = 1;
2694 DECL_NOT_REALLY_EXTERN (result
) = 1;
2698 /* called from the parser. */
2700 do_function_instantiation (declspecs
, declarator
, storage
)
2701 tree declspecs
, declarator
, storage
;
2703 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0,
2704 NULL_TREE
, NULL_TREE
);
2707 tree result
= NULL_TREE
;
2710 if (! DECL_LANG_SPECIFIC (decl
))
2712 cp_error ("explicit instantiation of non-template `%#D'", decl
);
2716 /* If we've already seen this template instance, use it. */
2717 if (name
= DECL_ASSEMBLER_NAME (decl
),
2718 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
2719 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
2721 else if (fn
&& DECL_CONTEXT (fn
))
2723 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
2725 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
2726 if (decls_match (fn
, decl
)
2727 && DECL_DEFER_OUTPUT (fn
))
2732 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2734 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn
));
2735 tree
*targs
= (tree
*) malloc (sizeof (tree
) * ntparms
);
2737 i
= type_unification (DECL_TEMPLATE_PARMS (fn
), targs
,
2738 TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2739 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
2744 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
2746 result
= instantiate_template (fn
, targs
);
2753 cp_error ("no matching template for `%D' found", decl
);
2757 if (flag_external_templates
)
2760 if (storage
== NULL_TREE
)
2762 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2765 cp_error ("storage class `%D' applied to template instantiation",
2769 instantiate_decl (result
);
2770 mark_decl_instantiated (result
, extern_p
);
2771 repo_template_instantiated (result
, extern_p
);
2775 mark_class_instantiated (t
, extern_p
)
2779 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
2780 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
2781 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
2782 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
2783 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
2786 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2787 rest_of_type_compilation (t
, 1);
2792 do_type_instantiation (name
, storage
)
2795 tree t
= TREE_TYPE (name
);
2802 /* With -fexternal-templates, explicit instantiations are treated the same
2803 as implicit ones. */
2804 if (flag_external_templates
)
2807 if (TYPE_SIZE (t
) == NULL_TREE
)
2809 cp_error ("explicit instantiation of `%#T' before definition of template",
2814 if (storage
== NULL_TREE
)
2816 else if (storage
== ridpointers
[(int) RID_INLINE
])
2818 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2820 else if (storage
== ridpointers
[(int) RID_STATIC
])
2824 cp_error ("storage class `%D' applied to template instantiation",
2829 /* We've already instantiated this. */
2830 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
2834 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
2836 mark_class_instantiated (t
, extern_p
);
2837 repo_template_instantiated (t
, extern_p
);
2847 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2848 if (DECL_TEMPLATE_INSTANTIATION (tmp
))
2850 mark_decl_instantiated (tmp
, extern_p
);
2851 repo_template_instantiated (tmp
, extern_p
);
2853 instantiate_decl (tmp
);
2856 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2857 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
2859 mark_decl_instantiated (tmp
, extern_p
);
2860 repo_template_instantiated (tmp
, extern_p
);
2862 instantiate_decl (tmp
);
2865 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2866 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
2867 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
2872 instantiate_decl (d
)
2875 tree ti
= DECL_TEMPLATE_INFO (d
);
2876 tree tmpl
= TI_TEMPLATE (ti
);
2877 tree args
= TI_ARGS (ti
);
2879 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
2881 int nested
= in_function_p ();
2883 int pattern_defined
;
2885 if (TREE_CODE (d
) == FUNCTION_DECL
)
2887 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
2888 pattern_defined
= (DECL_INITIAL (pattern
) != NULL_TREE
);
2892 d_defined
= ! DECL_IN_AGGR_P (d
);
2893 pattern_defined
= ! DECL_IN_AGGR_P (pattern
);
2898 else if (pattern_defined
)
2900 repo_template_used (d
);
2902 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
2904 if (flag_alt_external_templates
)
2906 if (interface_unknown
)
2907 warn_if_unknown_interface (d
);
2909 else if (DECL_INTERFACE_KNOWN (pattern
))
2911 DECL_INTERFACE_KNOWN (d
) = 1;
2912 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (pattern
);
2915 warn_if_unknown_interface (pattern
);
2919 import_export_decl (d
);
2922 if (! pattern_defined
2923 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! DECL_INLINE (d
)
2924 && (! DECL_INTERFACE_KNOWN (d
)
2925 || ! DECL_NOT_REALLY_EXTERN (d
))))
2927 add_pending_template (d
);
2931 if (! push_tinst_level (d
))
2934 if (TREE_CODE (d
) == FUNCTION_DECL
&& nested
)
2935 push_cp_function_context (NULL_TREE
);
2936 push_to_top_level ();
2938 /* Trick tsubst into giving us a new decl in case the template changed. */
2939 save_ti
= DECL_TEMPLATE_INFO (pattern
);
2940 DECL_TEMPLATE_INFO (pattern
) = NULL_TREE
;
2941 td
= tsubst (pattern
, &TREE_VEC_ELT (args
, 0), TREE_VEC_LENGTH (args
), tmpl
);
2942 DECL_TEMPLATE_INFO (pattern
) = save_ti
;
2944 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
2945 if (TREE_CODE (d
) == FUNCTION_DECL
)
2946 DECL_INITIAL (td
) = error_mark_node
;
2947 duplicate_decls (td
, d
);
2948 if (TREE_CODE (d
) == FUNCTION_DECL
)
2949 DECL_INITIAL (td
) = 0;
2951 if (TREE_CODE (d
) == VAR_DECL
)
2953 DECL_IN_AGGR_P (d
) = 0;
2954 if (DECL_INTERFACE_KNOWN (d
))
2955 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
2958 DECL_EXTERNAL (d
) = 1;
2959 DECL_NOT_REALLY_EXTERN (d
) = 1;
2961 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
2963 else if (TREE_CODE (d
) == FUNCTION_DECL
)
2965 tree t
= DECL_SAVED_TREE (pattern
);
2967 char *file
= input_filename
;
2969 lineno
= DECL_SOURCE_LINE (d
);
2970 input_filename
= DECL_SOURCE_FILE (d
);
2972 start_function (NULL_TREE
, d
, NULL_TREE
, NULL_TREE
, 1);
2973 store_parm_decls ();
2975 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
2977 current_member_init_list
2978 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
2979 current_base_init_list
2980 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
2985 /* Always keep the BLOCK node associated with the outermost
2986 pair of curley braces of a function. These are needed
2987 for correct operation of dwarfout.c. */
2990 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
2991 tsubst_expr (t
, &TREE_VEC_ELT (args
, 0),
2992 TREE_VEC_LENGTH (args
), tmpl
);
2994 finish_function (lineno
, 0, nested
);
2997 input_filename
= file
;
3000 pop_from_top_level ();
3001 if (TREE_CODE (d
) == FUNCTION_DECL
&& nested
)
3002 pop_cp_function_context (NULL_TREE
);
3009 tsubst_chain (t
, argvec
)
3014 tree first
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3015 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3018 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
3020 tree x
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3021 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3022 TREE_CHAIN (last
) = x
;
3032 tsubst_expr_values (t
, argvec
)
3035 tree first
= NULL_TREE
;
3038 for (; t
; t
= TREE_CHAIN (t
))
3040 tree pur
= tsubst_copy (TREE_PURPOSE (t
), &TREE_VEC_ELT (argvec
, 0),
3041 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3042 tree val
= tsubst_expr (TREE_VALUE (t
), &TREE_VEC_ELT (argvec
, 0),
3043 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3044 *p
= build_tree_list (pur
, val
);
3045 p
= &TREE_CHAIN (*p
);
3056 last_tree
= TREE_CHAIN (last_tree
) = t
;