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_CLASS (TREE_CODE (ot
)) == 't' && comptypes (ot
, nt
, 1))
458 if (TREE_CODE (ot
) == TEMPLATE_CONST_PARM
459 && TEMPLATE_CONST_IDX (nt
) == TEMPLATE_CONST_IDX (ot
))
466 /* Given class template name and parameter list, produce a user-friendly name
467 for the instantiation. */
469 mangle_class_name_for_template (name
, parms
, arglist
)
473 static struct obstack scratch_obstack
;
474 static char *scratch_firstobj
;
477 if (!scratch_firstobj
)
479 gcc_obstack_init (&scratch_obstack
);
480 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
483 obstack_free (&scratch_obstack
, scratch_firstobj
);
486 #define buflen sizeof(buf)
487 #define check if (bufp >= buf+buflen-1) goto too_long
488 #define ccat(c) *bufp++=(c); check
489 #define advance bufp+=strlen(bufp); check
490 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
493 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
495 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
500 nparms
= TREE_VEC_LENGTH (parms
);
501 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
502 for (i
= 0; i
< nparms
; i
++)
504 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
505 tree arg
= TREE_VEC_ELT (arglist
, i
);
510 if (TREE_CODE (parm
) == TYPE_DECL
)
512 cat (type_as_string (arg
, 0));
516 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
518 if (TREE_CODE (arg
) == TREE_LIST
)
520 /* New list cell was built because old chain link was in
522 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
523 arg
= TREE_VALUE (arg
);
525 /* No need to check arglist against parmlist here; we did that
526 in coerce_template_parms, called from lookup_template_class. */
527 cat (expr_as_string (arg
, 0));
530 char *bufp
= obstack_next_free (&scratch_obstack
);
532 while (bufp
[offset
- 1] == ' ')
534 obstack_blank_fast (&scratch_obstack
, offset
);
536 /* B<C<char> >, not B<C<char>> */
537 if (bufp
[offset
- 1] == '>')
542 return (char *) obstack_base (&scratch_obstack
);
547 fatal ("out of (preallocated) string space creating template instantiation name");
553 classtype_mangled_name (t
)
556 if (CLASSTYPE_TEMPLATE_INFO (t
)
557 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
559 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
560 char *mangled_name
= mangle_class_name_for_template
561 (IDENTIFIER_POINTER (name
),
562 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
563 CLASSTYPE_TI_ARGS (t
));
564 tree id
= get_identifier (mangled_name
);
565 IDENTIFIER_TEMPLATE (id
) = name
;
569 return TYPE_IDENTIFIER (t
);
573 add_pending_template (d
)
576 if (TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)))
579 *template_tail
= perm_tree_cons
580 (current_function_decl
, d
, NULL_TREE
);
581 template_tail
= &TREE_CHAIN (*template_tail
);
582 TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)) = 1;
585 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
586 parameters, find the desired type.
588 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
589 Since ARGLIST is build on the decl_obstack, we must copy it here
590 to keep it from being reclaimed when the decl storage is reclaimed.
592 IN_DECL, if non-NULL, is the template declaration we are trying to
595 lookup_template_class (d1
, arglist
, in_decl
)
599 tree
template, parmlist
;
604 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
606 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
608 template = IDENTIFIER_CLASS_VALUE (d1
);
610 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
612 template = CLASSTYPE_TI_TEMPLATE (d1
);
613 d1
= DECL_NAME (template);
616 my_friendly_abort (272);
618 /* With something like `template <class T> class X class X { ... };'
619 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
620 We don't want to do that, but we have to deal with the situation, so
621 let's give them some syntax errors to chew on instead of a crash. */
623 return error_mark_node
;
624 if (TREE_CODE (template) != TEMPLATE_DECL
)
626 cp_error ("non-template type `%T' used as a template", d1
);
628 cp_error_at ("for template declaration `%D'", in_decl
);
629 return error_mark_node
;
632 if (TREE_CODE (TREE_TYPE (template)) == RECORD_TYPE
)
633 code_type_node
= (CLASSTYPE_DECLARED_CLASS (TREE_TYPE (template))
634 ? class_type_node
: record_type_node
);
636 code_type_node
= union_type_node
;
638 if (PRIMARY_TEMPLATE_P (template))
640 parmlist
= DECL_TEMPLATE_PARMS (template);
642 arglist
= coerce_template_parms (parmlist
, arglist
, template);
643 if (arglist
== error_mark_node
)
644 return error_mark_node
;
645 if (uses_template_parms (arglist
))
648 if (comp_template_args
649 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
650 found
= TREE_TYPE (template);
653 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
654 found
; found
= TREE_CHAIN (found
))
656 if (TI_USES_TEMPLATE_PARMS (found
)
657 && comp_template_args (TREE_PURPOSE (found
), arglist
))
661 found
= TREE_VALUE (found
);
666 if (can_free (&permanent_obstack
, arglist
))
667 obstack_free (&permanent_obstack
, arglist
);
672 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
674 id
= get_identifier (mangled_name
);
675 IDENTIFIER_TEMPLATE (id
) = d1
;
677 maybe_push_to_top_level (uses_template_parms (arglist
));
678 t
= xref_tag (code_type_node
, id
, NULL_TREE
, 1);
679 pop_from_top_level ();
683 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
686 arglist
= CLASSTYPE_TI_ARGS (ctx
);
688 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
690 tree save_parms
= current_template_parms
;
691 current_template_parms
= NULL_TREE
;
692 t
= xref_tag (code_type_node
, id
, NULL_TREE
, 0);
693 current_template_parms
= save_parms
;
697 t
= lookup_nested_type_by_name (ctx
, id
);
698 my_friendly_assert (t
!= NULL_TREE
, 42);
702 /* Seems to be wanted. */
703 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
705 if (! CLASSTYPE_TEMPLATE_INFO (t
))
707 arglist
= copy_to_permanent (arglist
);
708 CLASSTYPE_TEMPLATE_INFO (t
)
709 = perm_tree_cons (template, arglist
, NULL_TREE
);
710 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
711 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
712 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
713 = uses_template_parms (arglist
);
715 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
717 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
718 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
719 if (! uses_template_parms (arglist
))
720 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
721 = get_identifier (build_overload_name (t
, 1, 1));
723 if (flag_external_templates
&& ! uses_template_parms (arglist
)
724 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
725 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
729 instantiate_class_template (t
);
731 for (d
= TYPE_METHODS (t
); d
; d
= TREE_CHAIN (d
))
732 add_pending_template (d
);
739 /* Should be defined in parse.h. */
743 uses_template_parms (t
)
748 switch (TREE_CODE (t
))
752 /* We assume that the object must be instantiated in order to build
753 the COMPONENT_REF, so we test only whether the type of the
754 COMPONENT_REF uses template parms. */
755 return uses_template_parms (TREE_TYPE (t
));
757 case IDENTIFIER_NODE
:
758 if (!IDENTIFIER_TEMPLATE (t
))
760 my_friendly_abort (42);
762 /* aggregates of tree nodes */
765 int i
= TREE_VEC_LENGTH (t
);
767 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
772 if (uses_template_parms (TREE_PURPOSE (t
))
773 || uses_template_parms (TREE_VALUE (t
)))
775 return uses_template_parms (TREE_CHAIN (t
));
777 /* constructed type nodes */
780 return uses_template_parms (TREE_TYPE (t
));
782 if (TYPE_PTRMEMFUNC_FLAG (t
))
783 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
785 if (! CLASSTYPE_TEMPLATE_INFO (t
))
787 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
789 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
791 return uses_template_parms (TREE_TYPE (t
));
793 if (uses_template_parms (TYPE_DOMAIN (t
)))
795 return uses_template_parms (TREE_TYPE (t
));
797 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
799 return uses_template_parms (TREE_TYPE (t
));
801 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
803 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
805 return uses_template_parms (TREE_TYPE (t
));
809 return uses_template_parms (TREE_TYPE (t
));
813 /* ??? What about FIELD_DECLs? */
814 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
815 && uses_template_parms (DECL_TI_ARGS (t
)))
820 if (uses_template_parms (TREE_TYPE (t
)))
822 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
827 return uses_template_parms (TREE_TYPE (t
));
829 return uses_template_parms (TREE_OPERAND (t
, 0));
831 /* template parm nodes */
832 case TEMPLATE_TYPE_PARM
:
833 case TEMPLATE_CONST_PARM
:
836 /* simple type nodes */
838 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
840 return uses_template_parms (TYPE_MAX_VALUE (t
));
855 /* Non-error_mark_node ERROR_MARKs are bad things. */
856 my_friendly_assert (t
== error_mark_node
, 274);
865 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
866 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
867 /* else fall through */
870 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
878 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
879 if (uses_template_parms (TREE_OPERAND (t
, i
)))
886 sorry ("testing %s for template parms",
887 tree_code_name
[(int) TREE_CODE (t
)]);
888 my_friendly_abort (82);
894 static struct tinst_level
*current_tinst_level
= 0;
895 static struct tinst_level
*free_tinst_level
= 0;
896 static int tinst_depth
= 0;
897 int max_tinst_depth
= 17;
898 #ifdef GATHER_STATISTICS
899 int depth_reached
= 0;
906 struct tinst_level
*new;
908 if (tinst_depth
>= max_tinst_depth
)
910 struct tinst_level
*p
= current_tinst_level
;
912 char *file
= input_filename
;
914 error ("template instantiation depth exceeds maximum of %d",
916 cp_error (" instantiating `%D'", d
);
918 for (; p
; p
= p
->next
)
920 cp_error (" instantiated from `%D'", p
->decl
);
922 input_filename
= p
->file
;
924 error (" instantiated from here");
927 input_filename
= file
;
932 if (free_tinst_level
)
934 new = free_tinst_level
;
935 free_tinst_level
= new->next
;
938 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
942 new->file
= input_filename
;
943 new->next
= current_tinst_level
;
944 current_tinst_level
= new;
947 #ifdef GATHER_STATISTICS
948 if (tinst_depth
> depth_reached
)
949 depth_reached
= tinst_depth
;
958 struct tinst_level
*old
= current_tinst_level
;
960 current_tinst_level
= old
->next
;
961 old
->next
= free_tinst_level
;
962 free_tinst_level
= old
;
969 struct tinst_level
*p
= current_tinst_level
;
972 for (; p
->next
; p
= p
->next
)
978 instantiate_class_template (type
)
981 tree
template, template_info
, args
, pattern
, t
, *field_chain
, *tag_chain
;
983 if (type
== error_mark_node
)
984 return error_mark_node
;
986 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
988 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
991 template = TI_TEMPLATE (template_info
);
992 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
993 args
= TI_ARGS (template_info
);
994 pattern
= TREE_TYPE (template);
996 if (TYPE_SIZE (pattern
) == NULL_TREE
)
999 TYPE_BEING_DEFINED (type
) = 1;
1001 if (! push_tinst_level (type
))
1004 maybe_push_to_top_level (uses_template_parms (type
));
1005 pushclass (type
, 0);
1007 if (flag_external_templates
)
1009 if (flag_alt_external_templates
)
1011 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1012 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1013 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1014 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1015 && CLASSTYPE_INTERFACE_KNOWN (type
);
1019 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1020 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1021 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1022 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1023 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1024 && CLASSTYPE_INTERFACE_KNOWN (type
);
1029 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1030 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1034 tree binfo
= TYPE_BINFO (type
);
1035 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1041 int len
= TREE_VEC_LENGTH (pbases
);
1042 BINFO_BASETYPES (binfo
) = bases
= make_tree_vec (len
);
1043 for (i
= 0; i
< len
; ++i
)
1047 TREE_VEC_ELT (bases
, i
) = elt
1048 = tsubst (TREE_VEC_ELT (pbases
, i
), &TREE_VEC_ELT (args
, 0),
1049 TREE_VEC_LENGTH (args
), NULL_TREE
);
1050 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1052 if (! uses_template_parms (type
) &&
1053 TYPE_SIZE (complete_type (TREE_TYPE (elt
))) == NULL_TREE
)
1054 cp_error ("base class `%T' of `%T' has incomplete type",
1055 TREE_TYPE (elt
), type
);
1060 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1062 field_chain
= &TYPE_FIELDS (type
);
1063 tag_chain
= &CLASSTYPE_TAGS (type
);
1065 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1067 tree name
= TREE_PURPOSE (t
);
1068 tree tag
= TREE_VALUE (t
);
1071 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1072 newtag
= start_enum (name
);
1074 newtag
= tsubst (tag
, &TREE_VEC_ELT (args
, 0),
1075 TREE_VEC_LENGTH (args
), NULL_TREE
);
1077 *tag_chain
= build_tree_list (name
, newtag
);
1078 tag_chain
= &TREE_CHAIN (*tag_chain
);
1080 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1082 tree e
, values
= NULL_TREE
, *last
= &values
;
1084 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
1086 tree elt
= build_enumerator
1088 tsubst_expr (TREE_VALUE (e
), &TREE_VEC_ELT (args
, 0),
1089 TREE_VEC_LENGTH (args
), NULL_TREE
));
1090 DECL_FIELD_CONTEXT (TREE_VALUE (elt
)) = type
;
1092 last
= &TREE_CHAIN (elt
);
1095 finish_enum (newtag
, values
);
1097 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
1098 while (*field_chain
)
1099 field_chain
= &TREE_CHAIN (*field_chain
);
1103 /* Don't replace enum constants here. */
1104 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1105 if (TREE_CODE (t
) != CONST_DECL
)
1107 tree r
= tsubst (t
, &TREE_VEC_ELT (args
, 0),
1108 TREE_VEC_LENGTH (args
), NULL_TREE
);
1109 if (TREE_CODE (r
) == VAR_DECL
)
1111 if (! uses_template_parms (r
))
1112 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1113 /* Perhaps I should do more of grokfield here. */
1115 DECL_IN_AGGR_P (r
) = 1;
1116 DECL_EXTERNAL (r
) = 1;
1117 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1121 field_chain
= &TREE_CHAIN (r
);
1124 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1126 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1127 = tsubst_chain (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)), args
);
1128 CLASSTYPE_FRIEND_CLASSES (type
)
1129 = tsubst_chain (CLASSTYPE_FRIEND_CLASSES (pattern
), args
);
1132 tree d
= tsubst (DECL_TEMPLATE_INJECT (template), &TREE_VEC_ELT (args
, 0),
1133 TREE_VEC_LENGTH (args
), NULL_TREE
);
1135 for (; d
; d
= TREE_CHAIN (d
))
1136 pushdecl (TREE_VALUE (d
));
1139 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1140 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1141 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1142 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1143 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1144 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1145 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1146 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1147 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1148 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1149 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1150 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1151 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1152 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1153 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1154 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1155 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1156 TYPE_USES_COMPLEX_INHERITANCE (type
)
1157 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1158 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1159 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1161 if (! uses_template_parms (type
))
1164 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1165 if (TREE_CODE (tmp
) == FIELD_DECL
1166 && TREE_CODE (DECL_NAME (tmp
)) != SCOPE_REF
)
1167 require_complete_type (tmp
);
1169 /* XXX handle attributes */
1170 type
= finish_struct_1 (type
, 0, 0);
1174 TYPE_SIZE (type
) = integer_zero_node
;
1175 CLASSTYPE_METHOD_VEC (type
)
1176 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1179 TYPE_BEING_DEFINED (type
) = 0;
1182 pop_from_top_level ();
1192 if (t1
== NULL_TREE
)
1193 return t2
== NULL_TREE
;
1194 if (t2
== NULL_TREE
)
1196 /* Don't care if one declares its arg const and the other doesn't -- the
1197 main variant of the arg type is all that matters. */
1198 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1199 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1201 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1205 lookup_nested_type_by_name (ctype
, name
)
1210 complete_type (ctype
);
1212 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1214 if (name
== TREE_PURPOSE (t
))
1215 return TREE_VALUE (t
);
1221 tsubst (t
, args
, nargs
, in_decl
)
1228 if (t
== NULL_TREE
|| t
== error_mark_node
1229 || t
== integer_type_node
1230 || t
== void_type_node
1231 || t
== char_type_node
)
1234 type
= TREE_TYPE (t
);
1235 if (type
== unknown_type_node
)
1236 my_friendly_abort (42);
1237 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
)
1238 type
= tsubst (type
, args
, nargs
, in_decl
);
1240 switch (TREE_CODE (t
))
1243 if (TYPE_PTRMEMFUNC_P (t
))
1245 tree r
= build_ptrmemfunc_type
1246 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1247 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1251 /* else fall through */
1253 if (uses_template_parms (t
))
1255 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
1256 tree r
= lookup_template_class (t
, argvec
, in_decl
);
1257 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1261 /* else fall through */
1263 case IDENTIFIER_NODE
:
1275 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1276 if (ctx
== NULL_TREE
)
1279 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
1283 if (t
== integer_type_node
)
1286 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1287 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1291 tree max
= tsubst_expr (TYPE_MAX_VALUE (t
), args
, nargs
, in_decl
);
1292 if (current_template_parms
)
1294 tree itype
= make_node (INTEGER_TYPE
);
1295 TYPE_MIN_VALUE (itype
) = size_zero_node
;
1296 TYPE_MAX_VALUE (itype
) = max
;
1299 return build_index_2_type (size_zero_node
, max
);
1302 case TEMPLATE_TYPE_PARM
:
1304 tree arg
= args
[TEMPLATE_TYPE_IDX (t
)];
1305 return cp_build_type_variant
1306 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1307 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1310 case TEMPLATE_CONST_PARM
:
1311 return args
[TEMPLATE_CONST_IDX (t
)];
1316 tree arg_types
, ctx
;
1320 if (DECL_CONTEXT (t
) != NULL_TREE
1321 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1323 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
1327 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1328 type
= tsubst (type
, args
, nargs
, in_decl
);
1334 type
= tsubst (type
, args
, nargs
, in_decl
);
1337 if (type
== TREE_TYPE (t
)
1338 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
1341 /* Do we already have this instantiation? */
1342 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1344 tree tmpl
= TREE_PURPOSE (DECL_TEMPLATE_INFO (t
));
1345 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1347 for (; decls
; decls
= TREE_CHAIN (decls
))
1348 if (TREE_TYPE (TREE_VALUE (decls
)) == type
1349 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1350 return TREE_VALUE (decls
);
1353 /* We do NOT check for matching decls pushed separately at this
1354 point, as they may not represent instantiations of this
1355 template, and in any case are considered separate under the
1360 TREE_TYPE (r
) = type
;
1363 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1364 DECL_CLASS_CONTEXT (r
) = ctx
;
1366 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
1367 IDENTIFIER_POINTER (DECL_NAME (r
)),
1368 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1370 /* Type-conversion operator. Reconstruct the name, in
1371 case it's the name of one of the template's parameters. */
1372 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
1375 arg_types
= TYPE_VALUES (type
);
1377 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
1378 arg_types
= hash_tree_chain
1379 (build_pointer_type (DECL_CONTEXT (r
)), arg_types
);
1381 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
1383 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
1384 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
1385 buf
= (char *) alloca (strlen (dbuf
)
1386 + sizeof (DESTRUCTOR_DECL_PREFIX
));
1387 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
1390 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
1393 DECL_ASSEMBLER_NAME (r
)
1394 = build_decl_overload (DECL_NAME (r
), arg_types
, member
);
1396 make_decl_rtl (r
, NULL_PTR
, 1);
1398 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1399 DECL_MAIN_VARIANT (r
) = r
;
1400 DECL_RESULT (r
) = NULL_TREE
;
1401 DECL_INITIAL (r
) = NULL_TREE
;
1403 TREE_STATIC (r
) = 0;
1404 TREE_PUBLIC (r
) = 1;
1405 DECL_EXTERNAL (r
) = 1;
1406 DECL_INTERFACE_KNOWN (r
) = 0;
1407 DECL_DEFER_OUTPUT (r
) = 0;
1408 TREE_CHAIN (r
) = NULL_TREE
;
1409 DECL_CHAIN (r
) = NULL_TREE
;
1411 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
1412 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
1414 /* Look for matching decls for the moment. */
1417 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
1420 if (decls
== NULL_TREE
)
1422 else if (is_overloaded_fn (decls
))
1423 for (decls
= get_first_fn (decls
); decls
;
1424 decls
= DECL_CHAIN (decls
))
1426 if (TREE_CODE (decls
) == FUNCTION_DECL
1427 && TREE_TYPE (decls
) == type
)
1436 int dcl_only
= ! DECL_INITIAL (d
);
1438 DECL_INITIAL (r
) = error_mark_node
;
1439 duplicate_decls (r
, d
);
1442 DECL_INITIAL (r
) = 0;
1446 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1448 tree tmpl
= DECL_TI_TEMPLATE (t
);
1449 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1450 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1451 args
, nargs
, in_decl
);
1453 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1454 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1456 /* If we have a preexisting version of this function, don't expand
1457 the template version, use the other instead. */
1458 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
1459 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
1461 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1463 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) =
1464 tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1472 tree r
= copy_node (t
);
1473 TREE_TYPE (r
) = type
;
1474 DECL_INITIAL (r
) = TREE_TYPE (r
);
1475 DECL_CONTEXT (r
) = NULL_TREE
;
1476 #ifdef PROMOTE_PROTOTYPES
1477 if ((TREE_CODE (type
) == INTEGER_TYPE
1478 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1479 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
1480 DECL_ARG_TYPE (r
) = integer_type_node
;
1483 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1489 tree r
= copy_node (t
);
1490 TREE_TYPE (r
) = type
;
1493 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
1495 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1496 TREE_CHAIN (r
) = NULL_TREE
;
1502 tree r
= copy_node (t
);
1504 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1505 TREE_CHAIN (r
) = NULL_TREE
;
1512 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1514 /* Do we already have this instantiation? */
1515 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1517 tree tmpl
= DECL_TI_TEMPLATE (t
);
1518 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1520 for (; decls
; decls
= TREE_CHAIN (decls
))
1521 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1522 return TREE_VALUE (decls
);
1526 TREE_TYPE (r
) = type
;
1527 DECL_CONTEXT (r
) = ctx
;
1528 if (TREE_STATIC (r
))
1529 DECL_ASSEMBLER_NAME (r
)
1530 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
1531 DECL_INITIAL (r
) = tsubst_expr
1532 (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1537 if (DECL_LANG_SPECIFIC (r
))
1540 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
1543 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1545 tree tmpl
= DECL_TI_TEMPLATE (t
);
1546 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1547 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1548 args
, nargs
, in_decl
);
1550 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1551 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1552 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1554 TREE_CHAIN (r
) = NULL_TREE
;
1560 tree r
= copy_node (t
);
1561 TREE_TYPE (r
) = type
;
1562 set_nested_typename (r
, current_class_name
, DECL_NAME (r
), type
);
1563 TREE_CHAIN (r
) = NULL_TREE
;
1569 tree purpose
, value
, chain
, result
;
1570 int via_public
, via_virtual
, via_protected
;
1572 if (t
== void_list_node
)
1575 via_public
= TREE_VIA_PUBLIC (t
);
1576 via_protected
= TREE_VIA_PROTECTED (t
);
1577 via_virtual
= TREE_VIA_VIRTUAL (t
);
1579 purpose
= TREE_PURPOSE (t
);
1581 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1582 value
= TREE_VALUE (t
);
1584 value
= tsubst (value
, args
, nargs
, in_decl
);
1585 chain
= TREE_CHAIN (t
);
1586 if (chain
&& chain
!= void_type_node
)
1587 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1588 if (purpose
== TREE_PURPOSE (t
)
1589 && value
== TREE_VALUE (t
)
1590 && chain
== TREE_CHAIN (t
))
1592 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1593 purpose
, value
, chain
);
1594 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1598 if (type
!= NULL_TREE
)
1602 if (type
== TREE_TYPE (t
))
1605 TREE_TYPE (t
) = complete_type (type
);
1606 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
1607 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
1608 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
1609 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
1614 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1615 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1617 bzero ((char *) elts
, len
* sizeof (tree
));
1619 for (i
= 0; i
< len
; i
++)
1621 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1622 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1629 t
= make_tree_vec (len
);
1630 for (i
= 0; i
< len
; i
++)
1631 TREE_VEC_ELT (t
, i
) = elts
[i
];
1636 case REFERENCE_TYPE
:
1639 enum tree_code code
;
1640 if (type
== TREE_TYPE (t
))
1643 code
= TREE_CODE (t
);
1644 if (code
== POINTER_TYPE
)
1645 r
= build_pointer_type (type
);
1647 r
= build_reference_type (type
);
1648 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1649 /* Will this ever be needed for TYPE_..._TO values? */
1654 return build_offset_type
1655 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1659 tree values
= TYPE_ARG_TYPES (t
);
1660 tree context
= TYPE_CONTEXT (t
);
1663 /* Don't bother recursing if we know it won't change anything. */
1664 if (values
!= void_list_node
)
1666 /* This should probably be rewritten to use hash_tree_cons for
1667 the memory savings. */
1668 tree first
= NULL_TREE
;
1671 for (; values
&& values
!= void_list_node
;
1672 values
= TREE_CHAIN (values
))
1675 = tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
);
1676 tree purpose
= tsubst_expr (TREE_PURPOSE (values
),
1677 args
, nargs
, in_decl
);
1678 tree x
= build_tree_list (purpose
, value
);
1681 TREE_CHAIN (last
) = x
;
1687 if (values
== void_list_node
)
1688 TREE_CHAIN (last
) = void_list_node
;
1693 context
= tsubst (context
, args
, nargs
, in_decl
);
1694 /* Could also optimize cases where return value and
1695 values have common elements (e.g., T min(const &T, const T&). */
1697 /* If the above parameters haven't changed, just return the type. */
1698 if (type
== TREE_TYPE (t
)
1699 && values
== TYPE_VALUES (t
)
1700 && context
== TYPE_CONTEXT (t
))
1703 /* Construct a new type node and return it. */
1704 if (TREE_CODE (t
) == FUNCTION_TYPE
1705 && context
== NULL_TREE
)
1707 new_value
= build_function_type (type
, values
);
1709 else if (context
== NULL_TREE
)
1711 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1712 args
, nargs
, in_decl
);
1713 new_value
= build_cplus_method_type (base
, type
,
1714 TREE_CHAIN (values
));
1718 new_value
= make_node (TREE_CODE (t
));
1719 TREE_TYPE (new_value
) = type
;
1720 TYPE_CONTEXT (new_value
) = context
;
1721 TYPE_VALUES (new_value
) = values
;
1722 TYPE_SIZE (new_value
) = TYPE_SIZE (t
);
1723 TYPE_ALIGN (new_value
) = TYPE_ALIGN (t
);
1724 TYPE_MODE (new_value
) = TYPE_MODE (t
);
1725 if (TYPE_METHOD_BASETYPE (t
))
1726 TYPE_METHOD_BASETYPE (new_value
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1727 args
, nargs
, in_decl
);
1728 /* Need to generate hash value. */
1729 my_friendly_abort (84);
1731 new_value
= build_type_variant (new_value
,
1738 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1740 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1742 r
= build_cplus_array_type (type
, domain
);
1748 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1749 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1750 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1754 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1755 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1759 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1760 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
1761 return cp_build_type_variant
1762 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
1763 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
1767 return make_pointer_declarator
1768 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1771 return make_reference_declarator
1772 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1775 return build_parse_node
1776 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1777 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1780 return build_parse_node
1781 (CALL_EXPR
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1782 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
), 0);
1785 sorry ("use of `%s' in template",
1786 tree_code_name
[(int) TREE_CODE (t
)]);
1787 return error_mark_node
;
1794 emit_line_note (input_filename
, lineno
);
1798 expand_start_bindings (0);
1806 expand_end_bindings (getdecls (), kept_level_p (), 1);
1807 t
= poplevel (kept_level_p (), 1, 0);
1813 tsubst_copy (t
, args
, nargs
, in_decl
)
1818 enum tree_code code
;
1820 if (t
== NULL_TREE
|| t
== error_mark_node
)
1823 code
= TREE_CODE (t
);
1828 return do_identifier (DECL_NAME (t
), 0);
1832 if (DECL_CONTEXT (t
))
1834 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1835 if (ctx
!= DECL_CONTEXT (t
))
1836 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
1842 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1843 t
= tsubst (t
, args
, nargs
, in_decl
);
1848 case IDENTIFIER_NODE
:
1849 return do_identifier (t
, 0);
1853 case REINTERPRET_CAST_EXPR
:
1855 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
1856 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1859 case PREDECREMENT_EXPR
:
1860 case PREINCREMENT_EXPR
:
1861 case POSTDECREMENT_EXPR
:
1862 case POSTINCREMENT_EXPR
:
1864 case TRUTH_NOT_EXPR
:
1866 case CONVERT_EXPR
: /* Unary + */
1871 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1876 case TRUNC_DIV_EXPR
:
1878 case FLOOR_DIV_EXPR
:
1879 case ROUND_DIV_EXPR
:
1880 case EXACT_DIV_EXPR
:
1882 case BIT_ANDTC_EXPR
:
1885 case TRUNC_MOD_EXPR
:
1886 case FLOOR_MOD_EXPR
:
1887 case TRUTH_ANDIF_EXPR
:
1888 case TRUTH_ORIF_EXPR
:
1889 case TRUTH_AND_EXPR
:
1910 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1911 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1915 tree fn
= TREE_OPERAND (t
, 0);
1916 if (really_overloaded_fn (fn
))
1917 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
1919 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
1921 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1925 case METHOD_CALL_EXPR
:
1927 tree name
= TREE_OPERAND (t
, 0);
1928 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1930 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1931 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
1933 else if (TREE_CODE (name
) == SCOPE_REF
1934 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
1936 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1937 name
= TREE_OPERAND (name
, 1);
1938 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1939 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, name
);
1940 name
= build_nt (SCOPE_REF
, base
, name
);
1943 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
1945 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1946 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
1953 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1954 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1955 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1960 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1961 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1962 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1963 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
1970 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1971 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1972 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
1973 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
1979 tree purpose
, value
, chain
;
1981 if (t
== void_list_node
)
1984 purpose
= TREE_PURPOSE (t
);
1986 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
1987 value
= TREE_VALUE (t
);
1989 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
1990 chain
= TREE_CHAIN (t
);
1991 if (chain
&& chain
!= void_type_node
)
1992 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
1993 if (purpose
== TREE_PURPOSE (t
)
1994 && value
== TREE_VALUE (t
)
1995 && chain
== TREE_CHAIN (t
))
1997 return tree_cons (purpose
, value
, chain
);
2004 case TEMPLATE_TYPE_PARM
:
2005 case TEMPLATE_CONST_PARM
:
2007 case REFERENCE_TYPE
:
2013 return tsubst (t
, args
, nargs
, in_decl
);
2021 tsubst_expr (t
, args
, nargs
, in_decl
)
2026 if (t
== NULL_TREE
|| t
== error_mark_node
)
2029 if (current_template_parms
)
2030 return tsubst_copy (t
, args
, nargs
, in_decl
);
2032 switch (TREE_CODE (t
))
2035 lineno
= TREE_COMPLEXITY (t
);
2036 emit_line_note (input_filename
, lineno
);
2038 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2043 lineno
= TREE_COMPLEXITY (t
);
2044 emit_line_note (input_filename
, lineno
);
2045 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2046 /* Do default conversion if safe and possibly important,
2047 in case within ({...}). */
2048 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
2049 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
2050 t
= default_conversion (t
);
2051 cplus_expand_expr_stmt (t
);
2058 int i
= suspend_momentary ();
2061 lineno
= TREE_COMPLEXITY (t
);
2062 emit_line_note (input_filename
, lineno
);
2064 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2065 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2066 TREE_OPERAND (t
, 3) != 0,
2067 tsubst (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2069 (dcl
, tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
),
2070 NULL_TREE
, 1, LOOKUP_ONLYCONVERTING
);
2071 resume_momentary (i
);
2078 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
2079 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2080 int cond_scope
= (TREE_OPERAND (t
, 1)
2081 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
2083 lineno
= TREE_COMPLEXITY (t
);
2084 emit_line_note (input_filename
, lineno
);
2087 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
2088 tsubst_expr (tmp
, 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
) == RETURN_INIT
)
2978 (TREE_OPERAND (t
, 0),
2979 tsubst_expr (TREE_OPERAND (t
, 1), &TREE_VEC_ELT (args
, 0),
2980 TREE_VEC_LENGTH (args
), tmpl
));
2984 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
2986 current_member_init_list
2987 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
2988 current_base_init_list
2989 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
2994 /* Always keep the BLOCK node associated with the outermost
2995 pair of curley braces of a function. These are needed
2996 for correct operation of dwarfout.c. */
2999 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
3000 tsubst_expr (t
, &TREE_VEC_ELT (args
, 0),
3001 TREE_VEC_LENGTH (args
), tmpl
);
3003 finish_function (lineno
, 0, nested
);
3006 input_filename
= file
;
3009 pop_from_top_level ();
3010 if (TREE_CODE (d
) == FUNCTION_DECL
&& nested
)
3011 pop_cp_function_context (NULL_TREE
);
3018 tsubst_chain (t
, argvec
)
3023 tree first
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3024 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3027 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
3029 tree x
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3030 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3031 TREE_CHAIN (last
) = x
;
3041 tsubst_expr_values (t
, argvec
)
3044 tree first
= NULL_TREE
;
3047 for (; t
; t
= TREE_CHAIN (t
))
3049 tree pur
= tsubst_copy (TREE_PURPOSE (t
), &TREE_VEC_ELT (argvec
, 0),
3050 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3051 tree val
= tsubst_expr (TREE_VALUE (t
), &TREE_VEC_ELT (argvec
, 0),
3052 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3053 *p
= build_tree_list (pur
, val
);
3054 p
= &TREE_CHAIN (*p
);
3065 last_tree
= TREE_CHAIN (last_tree
) = t
;