1 /* Handle parameterized types (templates) for GNU C++.
2 Copyright (C) 1992, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* Known bugs or deficiencies include:
24 * templates for class static data don't work (methods only)
25 * duplicated method templates can crash the compiler
26 * interface/impl data is taken from file defining the template
27 * all methods must be provided in header files; can't use a source
28 file that contains only the method templates and "just win"
44 extern struct obstack permanent_obstack
;
47 extern char *input_filename
;
48 struct pending_inline
*pending_template_expansions
;
50 tree current_template_parms
;
51 HOST_WIDE_INT processing_template_decl
;
53 tree pending_templates
;
54 static tree
*template_tail
= &pending_templates
;
56 int minimal_parse_mode
;
58 #define obstack_chunk_alloc xmalloc
59 #define obstack_chunk_free free
63 void pop_template_decls ();
65 tree
classtype_mangled_name ();
66 static char * mangle_class_name_for_template ();
67 tree
tsubst_expr_values ();
69 /* We've got a template header coming up; push to a new level for storing
73 begin_template_parm_list ()
76 declare_pseudo_global_level ();
79 /* Process information from new template parameter NEXT and append it to the
82 process_template_parm (list
, next
)
90 my_friendly_assert (TREE_CODE (parm
) == TREE_LIST
, 259);
91 defval
= TREE_PURPOSE (parm
);
92 parm
= TREE_VALUE (parm
);
93 is_type
= TREE_PURPOSE (parm
) == class_type_node
;
97 tree p
= TREE_VALUE (tree_last (list
));
99 if (TREE_CODE (p
) == TYPE_DECL
)
100 idx
= TEMPLATE_TYPE_IDX (TREE_TYPE (p
));
102 idx
= TEMPLATE_CONST_IDX (DECL_INITIAL (p
));
111 my_friendly_assert (TREE_CODE (TREE_PURPOSE (parm
)) == TREE_LIST
, 260);
112 /* is a const-param */
113 parm
= grokdeclarator (TREE_VALUE (parm
), TREE_PURPOSE (parm
),
114 PARM
, 0, NULL_TREE
, NULL_TREE
);
115 /* A template parameter is not modifiable. */
116 TREE_READONLY (parm
) = 1;
117 if (IS_AGGR_TYPE (TREE_TYPE (parm
)))
119 sorry ("aggregate template parameter types");
120 TREE_TYPE (parm
) = void_type_node
;
122 tinfo
= make_node (TEMPLATE_CONST_PARM
);
123 my_friendly_assert (TREE_PERMANENT (tinfo
), 260.5);
124 if (TREE_PERMANENT (parm
) == 0)
126 parm
= copy_node (parm
);
127 TREE_PERMANENT (parm
) = 1;
129 TREE_TYPE (tinfo
) = TREE_TYPE (parm
);
130 decl
= build_decl (CONST_DECL
, DECL_NAME (parm
), TREE_TYPE (parm
));
131 DECL_INITIAL (decl
) = tinfo
;
132 DECL_INITIAL (parm
) = tinfo
;
133 TEMPLATE_CONST_SET_INFO (tinfo
, idx
, processing_template_decl
+ 1);
137 tree t
= make_lang_type (TEMPLATE_TYPE_PARM
);
138 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
139 decl
= build_decl (TYPE_DECL
, TREE_VALUE (parm
), t
);
140 TYPE_MAIN_DECL (t
) = decl
;
142 TEMPLATE_TYPE_SET_INFO (t
, idx
, processing_template_decl
+ 1);
144 SET_DECL_ARTIFICIAL (decl
);
146 parm
= build_tree_list (defval
, parm
);
147 return chainon (list
, parm
);
150 /* The end of a template parameter list has been reached. Process the
151 tree list into a parameter vector, converting each parameter into a more
152 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
156 end_template_parm_list (parms
)
161 tree saved_parmlist
= make_tree_vec (list_length (parms
));
163 ++processing_template_decl
;
164 current_template_parms
165 = tree_cons (build_int_2 (0, processing_template_decl
),
166 saved_parmlist
, current_template_parms
);
168 for (parm
= parms
, nparms
= 0; parm
; parm
= TREE_CHAIN (parm
), nparms
++)
169 TREE_VEC_ELT (saved_parmlist
, nparms
) = parm
;
171 return saved_parmlist
;
174 /* end_template_decl is called after a template declaration is seen. */
179 /* This matches the pushlevel in begin_template_parm_list. */
182 --processing_template_decl
;
183 current_template_parms
= TREE_CHAIN (current_template_parms
);
184 (void) get_pending_sizes (); /* Why? */
188 push_template_decl (decl
)
191 tree header
= current_template_parms
;
193 tree args
= NULL_TREE
;
195 tree ctx
= DECL_CONTEXT (decl
) ? DECL_CONTEXT (decl
) : current_class_type
;
199 if (TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_FRIEND_P (decl
)
200 && DECL_CLASS_CONTEXT (decl
))
202 /* Note that this template is a "primary template" */
203 else if (! ctx
|| ! CLASSTYPE_TEMPLATE_INFO (ctx
)
204 /* || (processing_template_decl > CLASSTYPE_TEMPLATE_LEVEL (ctx)) */)
209 tree a
= copy_node (TREE_VALUE (header
));
210 int i
= TREE_VEC_LENGTH (a
);
211 TREE_TYPE (a
) = NULL_TREE
;
214 tree t
= TREE_VALUE (TREE_VEC_ELT (a
, i
));
215 if (TREE_CODE (t
) == TYPE_DECL
)
218 t
= DECL_INITIAL (t
);
219 TREE_VEC_ELT (a
, i
) = t
;
221 args
= tree_cons (TREE_PURPOSE (header
), a
, args
);
222 header
= TREE_CHAIN (header
);
224 args
= nreverse (args
);
225 args
= TREE_VALUE (args
);
227 if (! ctx
|| TYPE_BEING_DEFINED (ctx
))
229 tmpl
= build_lang_decl (TEMPLATE_DECL
, DECL_NAME (decl
), NULL_TREE
);
230 DECL_TEMPLATE_PARMS (tmpl
) = TREE_VALUE (current_template_parms
);
231 DECL_CONTEXT (tmpl
) = DECL_CONTEXT (decl
);
235 if (TREE_CODE (decl
) == TYPE_DECL
)
236 tmpl
= CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl
));
237 else if (! DECL_TEMPLATE_INFO (decl
))
238 /* A member definition that doesn't match anything in the class. */
241 tmpl
= DECL_TI_TEMPLATE (decl
);
244 DECL_TEMPLATE_RESULT (tmpl
) = decl
;
245 TREE_TYPE (tmpl
) = TREE_TYPE (decl
);
248 tmpl
= pushdecl_top_level (tmpl
);
251 TREE_TYPE (DECL_TEMPLATE_PARMS (tmpl
)) = tmpl
;
253 info
= perm_tree_cons (tmpl
, args
, NULL_TREE
);
255 if (TREE_CODE (decl
) == TYPE_DECL
)
257 CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl
)) = info
;
258 DECL_NAME (decl
) = classtype_mangled_name (TREE_TYPE (decl
));
260 else if (! DECL_LANG_SPECIFIC (decl
))
261 cp_error ("template declaration of `%#D'", decl
);
263 DECL_TEMPLATE_INFO (decl
) = info
;
266 tree tsubst
PROTO ((tree
, tree
*, int, tree
));
268 /* Convert all template arguments to their appropriate types, and return
269 a vector containing the resulting values. If any error occurs, return
272 coerce_template_parms (parms
, arglist
, in_decl
)
276 int nparms
, nargs
, i
, lost
= 0;
279 if (arglist
== NULL_TREE
)
281 else if (TREE_CODE (arglist
) == TREE_VEC
)
282 nargs
= TREE_VEC_LENGTH (arglist
);
284 nargs
= list_length (arglist
);
286 nparms
= TREE_VEC_LENGTH (parms
);
290 && TREE_PURPOSE (TREE_VEC_ELT (parms
, nargs
)) == NULL_TREE
))
292 error ("incorrect number of parameters (%d, should be %d)",
295 cp_error_at ("in template expansion for decl `%D'", in_decl
);
296 return error_mark_node
;
299 if (arglist
&& TREE_CODE (arglist
) == TREE_VEC
)
300 vec
= copy_node (arglist
);
303 vec
= make_tree_vec (nparms
);
304 for (i
= 0; i
< nparms
; i
++)
311 arglist
= TREE_CHAIN (arglist
);
313 if (arg
== error_mark_node
)
316 arg
= TREE_VALUE (arg
);
319 arg
= tsubst (TREE_PURPOSE (TREE_VEC_ELT (parms
, i
)),
320 &TREE_VEC_ELT (vec
, 0), i
, in_decl
);
322 TREE_VEC_ELT (vec
, i
) = arg
;
325 for (i
= 0; i
< nparms
; i
++)
327 tree arg
= TREE_VEC_ELT (vec
, i
);
328 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
330 int is_type
, requires_type
;
332 is_type
= TREE_CODE_CLASS (TREE_CODE (arg
)) == 't';
333 requires_type
= TREE_CODE (parm
) == TYPE_DECL
;
335 if (requires_type
&& ! is_type
&& TREE_CODE (arg
) == SCOPE_REF
336 && TREE_CODE (TREE_OPERAND (arg
, 0)) == TEMPLATE_TYPE_PARM
)
338 cp_pedwarn ("to refer to a type member of a template parameter,");
339 cp_pedwarn (" use `typename %E'", arg
);
340 arg
= make_typename_type (TREE_OPERAND (arg
, 0),
341 TREE_OPERAND (arg
, 1));
344 if (is_type
!= requires_type
)
348 cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
351 cp_error (" expected a constant of type `%T', got `%T'",
352 TREE_TYPE (parm
), arg
);
354 cp_error (" expected a type, got `%E'", arg
);
357 TREE_VEC_ELT (vec
, i
) = error_mark_node
;
362 val
= groktypename (arg
);
363 if (! current_template_parms
)
365 tree t
= target_type (val
);
367 && decl_function_context (TYPE_MAIN_DECL (t
)))
369 cp_error ("type `%T' composed from a local class is not a valid template-argument", val
);
370 return error_mark_node
;
376 tree t
= tsubst (TREE_TYPE (parm
), &TREE_VEC_ELT (vec
, 0),
377 TREE_VEC_LENGTH (vec
), in_decl
);
378 if (current_template_parms
)
381 val
= digest_init (t
, arg
, (tree
*) 0);
383 if (val
== error_mark_node
|| current_template_parms
)
386 /* 14.2: Other template-arguments must be constant-expressions,
387 addresses of objects or functions with external linkage, or of
388 static class members. */
389 else if (!TREE_CONSTANT (val
))
391 cp_error ("non-const `%E' cannot be used as template argument",
393 val
= error_mark_node
;
395 else if (POINTER_TYPE_P (TREE_TYPE (val
))
396 && ! integer_zerop (val
)
397 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != OFFSET_TYPE
398 && TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) != METHOD_TYPE
)
402 if (TREE_CODE (t
) == ADDR_EXPR
)
404 tree a
= TREE_OPERAND (t
, 0);
406 if (TREE_CODE (a
) == STRING_CST
)
408 cp_error ("string literal %E is not a valid template argument", a
);
409 error ("because it is the address of an object with static linkage");
410 val
= error_mark_node
;
412 else if (TREE_CODE (a
) != VAR_DECL
413 && TREE_CODE (a
) != FUNCTION_DECL
)
415 else if (! DECL_PUBLIC (a
))
417 cp_error ("address of non-extern `%E' cannot be used as template argument", a
);
418 val
= error_mark_node
;
424 cp_error ("`%E' is not a valid template argument", t
);
425 error ("it must be %s%s with external linkage",
426 TREE_CODE (TREE_TYPE (val
)) == POINTER_TYPE
427 ? "a pointer to " : "",
428 TREE_CODE (TREE_TYPE (TREE_TYPE (val
))) == FUNCTION_TYPE
429 ? "a function" : "an object");
430 val
= error_mark_node
;
435 if (val
== error_mark_node
)
438 TREE_VEC_ELT (vec
, i
) = val
;
441 return error_mark_node
;
446 comp_template_args (oldargs
, newargs
)
447 tree oldargs
, newargs
;
451 for (i
= 0; i
< TREE_VEC_LENGTH (oldargs
); ++i
)
453 tree nt
= TREE_VEC_ELT (newargs
, i
);
454 tree ot
= TREE_VEC_ELT (oldargs
, i
);
458 if (TREE_CODE (nt
) != TREE_CODE (ot
))
460 if (TREE_CODE_CLASS (TREE_CODE (ot
)) == 't' && comptypes (ot
, nt
, 1))
462 if (TREE_CODE (ot
) == TEMPLATE_CONST_PARM
463 && TEMPLATE_CONST_IDX (nt
) == TEMPLATE_CONST_IDX (ot
))
470 /* Given class template name and parameter list, produce a user-friendly name
471 for the instantiation. */
473 mangle_class_name_for_template (name
, parms
, arglist
)
477 static struct obstack scratch_obstack
;
478 static char *scratch_firstobj
;
481 if (!scratch_firstobj
)
482 gcc_obstack_init (&scratch_obstack
);
484 obstack_free (&scratch_obstack
, scratch_firstobj
);
485 scratch_firstobj
= obstack_alloc (&scratch_obstack
, 1);
488 #define buflen sizeof(buf)
489 #define check if (bufp >= buf+buflen-1) goto too_long
490 #define ccat(c) *bufp++=(c); check
491 #define advance bufp+=strlen(bufp); check
492 #define cat(s) strncpy(bufp, s, buf+buflen-bufp-1); advance
495 #define ccat(c) obstack_1grow (&scratch_obstack, (c));
497 #define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
502 nparms
= TREE_VEC_LENGTH (parms
);
503 my_friendly_assert (nparms
== TREE_VEC_LENGTH (arglist
), 268);
504 for (i
= 0; i
< nparms
; i
++)
506 tree parm
= TREE_VALUE (TREE_VEC_ELT (parms
, i
));
507 tree arg
= TREE_VEC_ELT (arglist
, i
);
512 if (TREE_CODE (parm
) == TYPE_DECL
)
514 cat (type_as_string (arg
, 0));
518 my_friendly_assert (TREE_CODE (parm
) == PARM_DECL
, 269);
520 if (TREE_CODE (arg
) == TREE_LIST
)
522 /* New list cell was built because old chain link was in
524 my_friendly_assert (TREE_PURPOSE (arg
) == NULL_TREE
, 270);
525 arg
= TREE_VALUE (arg
);
527 /* No need to check arglist against parmlist here; we did that
528 in coerce_template_parms, called from lookup_template_class. */
529 cat (expr_as_string (arg
, 0));
532 char *bufp
= obstack_next_free (&scratch_obstack
);
534 while (bufp
[offset
- 1] == ' ')
536 obstack_blank_fast (&scratch_obstack
, offset
);
538 /* B<C<char> >, not B<C<char>> */
539 if (bufp
[offset
- 1] == '>')
544 return (char *) obstack_base (&scratch_obstack
);
549 fatal ("out of (preallocated) string space creating template instantiation name");
555 classtype_mangled_name (t
)
558 if (CLASSTYPE_TEMPLATE_INFO (t
)
559 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t
)))
561 tree name
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (t
));
562 char *mangled_name
= mangle_class_name_for_template
563 (IDENTIFIER_POINTER (name
),
564 DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (t
)),
565 CLASSTYPE_TI_ARGS (t
));
566 tree id
= get_identifier (mangled_name
);
567 IDENTIFIER_TEMPLATE (id
) = name
;
571 return TYPE_IDENTIFIER (t
);
575 add_pending_template (d
)
578 if (TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)))
581 *template_tail
= perm_tree_cons
582 (current_function_decl
, d
, NULL_TREE
);
583 template_tail
= &TREE_CHAIN (*template_tail
);
584 TREE_LANG_FLAG_0 (DECL_TEMPLATE_INFO (d
)) = 1;
587 /* Given an IDENTIFIER_NODE (type TEMPLATE_DECL) and a chain of
588 parameters, find the desired type.
590 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
591 Since ARGLIST is build on the decl_obstack, we must copy it here
592 to keep it from being reclaimed when the decl storage is reclaimed.
594 IN_DECL, if non-NULL, is the template declaration we are trying to
597 lookup_template_class (d1
, arglist
, in_decl
)
601 tree
template, parmlist
;
606 if (TREE_CODE (d1
) == IDENTIFIER_NODE
)
608 template = IDENTIFIER_GLOBAL_VALUE (d1
); /* XXX */
610 template = IDENTIFIER_CLASS_VALUE (d1
);
612 else if (TREE_CODE_CLASS (TREE_CODE (d1
)) == 't' && IS_AGGR_TYPE (d1
))
614 template = CLASSTYPE_TI_TEMPLATE (d1
);
615 d1
= DECL_NAME (template);
618 my_friendly_abort (272);
620 /* With something like `template <class T> class X class X { ... };'
621 we could end up with D1 having nothing but an IDENTIFIER_LOCAL_VALUE.
622 We don't want to do that, but we have to deal with the situation, so
623 let's give them some syntax errors to chew on instead of a crash. */
625 return error_mark_node
;
626 if (TREE_CODE (template) != TEMPLATE_DECL
)
628 cp_error ("non-template type `%T' used as a template", d1
);
630 cp_error_at ("for template declaration `%D'", in_decl
);
631 return error_mark_node
;
634 if (PRIMARY_TEMPLATE_P (template))
636 parmlist
= DECL_TEMPLATE_PARMS (template);
638 arglist
= coerce_template_parms (parmlist
, arglist
, template);
639 if (arglist
== error_mark_node
)
640 return error_mark_node
;
641 if (uses_template_parms (arglist
))
644 if (comp_template_args
645 (CLASSTYPE_TI_ARGS (TREE_TYPE (template)), arglist
))
646 found
= TREE_TYPE (template);
649 for (found
= DECL_TEMPLATE_INSTANTIATIONS (template);
650 found
; found
= TREE_CHAIN (found
))
652 if (TI_USES_TEMPLATE_PARMS (found
)
653 && comp_template_args (TREE_PURPOSE (found
), arglist
))
657 found
= TREE_VALUE (found
);
662 if (can_free (&permanent_obstack
, arglist
))
663 obstack_free (&permanent_obstack
, arglist
);
668 mangled_name
= mangle_class_name_for_template (IDENTIFIER_POINTER (d1
),
670 id
= get_identifier (mangled_name
);
671 IDENTIFIER_TEMPLATE (id
) = d1
;
673 maybe_push_to_top_level (uses_template_parms (arglist
));
674 t
= xref_tag_from_type (TREE_TYPE (template), id
, 1);
675 pop_from_top_level ();
679 tree ctx
= lookup_template_class (TYPE_CONTEXT (TREE_TYPE (template)),
682 arglist
= CLASSTYPE_TI_ARGS (ctx
);
684 if (TYPE_BEING_DEFINED (ctx
) && ctx
== current_class_type
)
686 tree save_parms
= current_template_parms
;
687 current_template_parms
= NULL_TREE
;
688 t
= xref_tag_from_type (TREE_TYPE (template), id
, 0);
689 current_template_parms
= save_parms
;
693 t
= lookup_nested_type_by_name (ctx
, id
);
694 my_friendly_assert (t
!= NULL_TREE
, 42);
698 /* Seems to be wanted. */
699 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
701 if (! CLASSTYPE_TEMPLATE_INFO (t
))
703 arglist
= copy_to_permanent (arglist
);
704 CLASSTYPE_TEMPLATE_INFO (t
)
705 = perm_tree_cons (template, arglist
, NULL_TREE
);
706 DECL_TEMPLATE_INSTANTIATIONS (template) = perm_tree_cons
707 (arglist
, t
, DECL_TEMPLATE_INSTANTIATIONS (template));
708 TI_USES_TEMPLATE_PARMS (DECL_TEMPLATE_INSTANTIATIONS (template))
709 = uses_template_parms (arglist
);
711 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t
);
713 /* We need to set this again after CLASSTYPE_TEMPLATE_INFO is set up. */
714 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
)) = id
;
715 if (! uses_template_parms (arglist
))
716 DECL_ASSEMBLER_NAME (TYPE_MAIN_DECL (t
))
717 = get_identifier (build_overload_name (t
, 1, 1));
719 if (flag_external_templates
&& ! uses_template_parms (arglist
)
720 && CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
721 && ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
725 instantiate_class_template (t
);
727 for (d
= TYPE_METHODS (t
); d
; d
= TREE_CHAIN (d
))
728 add_pending_template (d
);
735 /* Should be defined in parse.h. */
739 uses_template_parms (t
)
744 switch (TREE_CODE (t
))
748 /* We assume that the object must be instantiated in order to build
749 the COMPONENT_REF, so we test only whether the type of the
750 COMPONENT_REF uses template parms. */
751 return uses_template_parms (TREE_TYPE (t
));
753 case IDENTIFIER_NODE
:
754 if (!IDENTIFIER_TEMPLATE (t
))
756 my_friendly_abort (42);
758 /* aggregates of tree nodes */
761 int i
= TREE_VEC_LENGTH (t
);
763 if (uses_template_parms (TREE_VEC_ELT (t
, i
)))
768 if (uses_template_parms (TREE_PURPOSE (t
))
769 || uses_template_parms (TREE_VALUE (t
)))
771 return uses_template_parms (TREE_CHAIN (t
));
773 /* constructed type nodes */
776 return uses_template_parms (TREE_TYPE (t
));
778 if (TYPE_PTRMEMFUNC_FLAG (t
))
779 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (t
));
781 if (! CLASSTYPE_TEMPLATE_INFO (t
))
783 return uses_template_parms (TREE_VALUE (CLASSTYPE_TEMPLATE_INFO (t
)));
785 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
787 return uses_template_parms (TREE_TYPE (t
));
789 if (uses_template_parms (TYPE_DOMAIN (t
)))
791 return uses_template_parms (TREE_TYPE (t
));
793 if (uses_template_parms (TYPE_OFFSET_BASETYPE (t
)))
795 return uses_template_parms (TREE_TYPE (t
));
797 if (uses_template_parms (TYPE_METHOD_BASETYPE (t
)))
799 if (uses_template_parms (TYPE_ARG_TYPES (t
)))
801 return uses_template_parms (TREE_TYPE (t
));
805 return uses_template_parms (TREE_TYPE (t
));
809 /* ??? What about FIELD_DECLs? */
810 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
)
811 && uses_template_parms (DECL_TI_ARGS (t
)))
816 if (uses_template_parms (TREE_TYPE (t
)))
818 if (DECL_CONTEXT (t
) && uses_template_parms (DECL_CONTEXT (t
)))
823 return uses_template_parms (TREE_TYPE (t
));
825 return uses_template_parms (TREE_OPERAND (t
, 0));
827 /* template parm nodes */
828 case TEMPLATE_TYPE_PARM
:
829 case TEMPLATE_CONST_PARM
:
832 /* simple type nodes */
834 if (uses_template_parms (TYPE_MIN_VALUE (t
)))
836 return uses_template_parms (TYPE_MAX_VALUE (t
));
851 /* Non-error_mark_node ERROR_MARKs are bad things. */
852 my_friendly_assert (t
== error_mark_node
, 274);
861 if (TREE_TYPE (t
) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t
)))
862 return uses_template_parms (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t
)));
863 /* else fall through */
866 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
874 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
875 if (uses_template_parms (TREE_OPERAND (t
, i
)))
882 sorry ("testing %s for template parms",
883 tree_code_name
[(int) TREE_CODE (t
)]);
884 my_friendly_abort (82);
890 static struct tinst_level
*current_tinst_level
= 0;
891 static struct tinst_level
*free_tinst_level
= 0;
892 static int tinst_depth
= 0;
893 int max_tinst_depth
= 17;
894 #ifdef GATHER_STATISTICS
895 int depth_reached
= 0;
902 struct tinst_level
*new;
904 if (tinst_depth
>= max_tinst_depth
)
906 struct tinst_level
*p
= current_tinst_level
;
908 char *file
= input_filename
;
910 error ("template instantiation depth exceeds maximum of %d",
912 cp_error (" instantiating `%D'", d
);
914 for (; p
; p
= p
->next
)
916 cp_error (" instantiated from `%D'", p
->decl
);
918 input_filename
= p
->file
;
920 error (" instantiated from here");
923 input_filename
= file
;
928 if (free_tinst_level
)
930 new = free_tinst_level
;
931 free_tinst_level
= new->next
;
934 new = (struct tinst_level
*) xmalloc (sizeof (struct tinst_level
));
938 new->file
= input_filename
;
939 new->next
= current_tinst_level
;
940 current_tinst_level
= new;
943 #ifdef GATHER_STATISTICS
944 if (tinst_depth
> depth_reached
)
945 depth_reached
= tinst_depth
;
954 struct tinst_level
*old
= current_tinst_level
;
956 current_tinst_level
= old
->next
;
957 old
->next
= free_tinst_level
;
958 free_tinst_level
= old
;
965 struct tinst_level
*p
= current_tinst_level
;
968 for (; p
->next
; p
= p
->next
)
974 instantiate_class_template (type
)
977 tree
template, template_info
, args
, pattern
, t
, *field_chain
;
979 if (type
== error_mark_node
)
980 return error_mark_node
;
982 template_info
= CLASSTYPE_TEMPLATE_INFO (type
);
984 if (TYPE_BEING_DEFINED (type
) || TYPE_SIZE (type
))
987 template = TI_TEMPLATE (template_info
);
988 my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL
, 279);
989 args
= TI_ARGS (template_info
);
990 pattern
= TREE_TYPE (template);
992 if (TYPE_SIZE (pattern
) == NULL_TREE
)
995 TYPE_BEING_DEFINED (type
) = 1;
997 if (! push_tinst_level (type
))
1000 maybe_push_to_top_level (uses_template_parms (type
));
1001 pushclass (type
, 0);
1003 if (flag_external_templates
)
1005 if (flag_alt_external_templates
)
1007 CLASSTYPE_INTERFACE_ONLY (type
) = interface_only
;
1008 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type
, interface_unknown
);
1009 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1010 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1011 && CLASSTYPE_INTERFACE_KNOWN (type
);
1015 CLASSTYPE_INTERFACE_ONLY (type
) = CLASSTYPE_INTERFACE_ONLY (pattern
);
1016 SET_CLASSTYPE_INTERFACE_UNKNOWN_X
1017 (type
, CLASSTYPE_INTERFACE_UNKNOWN (pattern
));
1018 CLASSTYPE_VTABLE_NEEDS_WRITING (type
)
1019 = ! CLASSTYPE_INTERFACE_ONLY (type
)
1020 && CLASSTYPE_INTERFACE_KNOWN (type
);
1025 SET_CLASSTYPE_INTERFACE_UNKNOWN (type
);
1026 CLASSTYPE_VTABLE_NEEDS_WRITING (type
) = 1;
1030 tree binfo
= TYPE_BINFO (type
);
1031 tree pbases
= TYPE_BINFO_BASETYPES (pattern
);
1037 int len
= TREE_VEC_LENGTH (pbases
);
1038 BINFO_BASETYPES (binfo
) = bases
= make_tree_vec (len
);
1039 for (i
= 0; i
< len
; ++i
)
1043 TREE_VEC_ELT (bases
, i
) = elt
1044 = tsubst (TREE_VEC_ELT (pbases
, i
), &TREE_VEC_ELT (args
, 0),
1045 TREE_VEC_LENGTH (args
), NULL_TREE
);
1046 BINFO_INHERITANCE_CHAIN (elt
) = binfo
;
1048 if (! uses_template_parms (type
) &&
1049 TYPE_SIZE (complete_type (TREE_TYPE (elt
))) == NULL_TREE
)
1050 cp_error ("base class `%T' of `%T' has incomplete type",
1051 TREE_TYPE (elt
), type
);
1056 CLASSTYPE_LOCAL_TYPEDECLS (type
) = CLASSTYPE_LOCAL_TYPEDECLS (pattern
);
1058 field_chain
= &TYPE_FIELDS (type
);
1060 for (t
= CLASSTYPE_TAGS (pattern
); t
; t
= TREE_CHAIN (t
))
1062 tree name
= TREE_PURPOSE (t
);
1063 tree tag
= TREE_VALUE (t
);
1066 /* These will add themselves to CLASSTYPE_TAGS for the new type. */
1067 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1068 newtag
= start_enum (name
);
1070 newtag
= tsubst (tag
, &TREE_VEC_ELT (args
, 0),
1071 TREE_VEC_LENGTH (args
), NULL_TREE
);
1073 if (TREE_CODE (tag
) == ENUMERAL_TYPE
)
1075 tree e
, values
= NULL_TREE
, *last
= &values
;
1077 for (e
= TYPE_VALUES (tag
); e
; e
= TREE_CHAIN (e
))
1079 tree elt
= build_enumerator
1081 tsubst_expr (TREE_VALUE (e
), &TREE_VEC_ELT (args
, 0),
1082 TREE_VEC_LENGTH (args
), NULL_TREE
));
1083 DECL_FIELD_CONTEXT (TREE_VALUE (elt
)) = type
;
1085 last
= &TREE_CHAIN (elt
);
1088 finish_enum (newtag
, values
);
1090 *field_chain
= grok_enum_decls (newtag
, NULL_TREE
);
1091 while (*field_chain
)
1092 field_chain
= &TREE_CHAIN (*field_chain
);
1096 /* Don't replace enum constants here. */
1097 for (t
= TYPE_FIELDS (pattern
); t
; t
= TREE_CHAIN (t
))
1098 if (TREE_CODE (t
) != CONST_DECL
)
1100 tree r
= tsubst (t
, &TREE_VEC_ELT (args
, 0),
1101 TREE_VEC_LENGTH (args
), NULL_TREE
);
1102 if (TREE_CODE (r
) == VAR_DECL
)
1104 if (! uses_template_parms (r
))
1105 pending_statics
= perm_tree_cons (NULL_TREE
, r
, pending_statics
);
1106 /* Perhaps I should do more of grokfield here. */
1108 DECL_IN_AGGR_P (r
) = 1;
1109 DECL_EXTERNAL (r
) = 1;
1110 cp_finish_decl (r
, DECL_INITIAL (r
), NULL_TREE
, 0, 0);
1114 field_chain
= &TREE_CHAIN (r
);
1117 TYPE_METHODS (type
) = tsubst_chain (TYPE_METHODS (pattern
), args
);
1119 DECL_FRIENDLIST (TYPE_MAIN_DECL (type
))
1120 = tsubst (DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern
)),
1121 &TREE_VEC_ELT (args
, 0), TREE_VEC_LENGTH (args
), NULL_TREE
);
1124 tree d
= CLASSTYPE_FRIEND_CLASSES (type
) =
1125 tsubst (CLASSTYPE_FRIEND_CLASSES (pattern
), &TREE_VEC_ELT (args
, 0),
1126 TREE_VEC_LENGTH (args
), NULL_TREE
);
1128 /* This does injection for friend classes. */
1129 for (; d
; d
= TREE_CHAIN (d
))
1130 TREE_VALUE (d
) = xref_tag_from_type (TREE_VALUE (d
), NULL_TREE
, 1);
1132 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
))
1137 tree t
= TREE_VALUE (d
);
1139 if (TREE_CODE (t
) == TYPE_DECL
)
1140 /* Already injected. */;
1146 TYPE_HAS_CONSTRUCTOR (type
) = TYPE_HAS_CONSTRUCTOR (pattern
);
1147 TYPE_HAS_DESTRUCTOR (type
) = TYPE_HAS_DESTRUCTOR (pattern
);
1148 TYPE_HAS_ASSIGNMENT (type
) = TYPE_HAS_ASSIGNMENT (pattern
);
1149 TYPE_OVERLOADS_CALL_EXPR (type
) = TYPE_OVERLOADS_CALL_EXPR (pattern
);
1150 TYPE_OVERLOADS_ARRAY_REF (type
) = TYPE_OVERLOADS_ARRAY_REF (pattern
);
1151 TYPE_OVERLOADS_ARROW (type
) = TYPE_OVERLOADS_ARROW (pattern
);
1152 TYPE_GETS_NEW (type
) = TYPE_GETS_NEW (pattern
);
1153 TYPE_GETS_DELETE (type
) = TYPE_GETS_DELETE (pattern
);
1154 TYPE_VEC_DELETE_TAKES_SIZE (type
) = TYPE_VEC_DELETE_TAKES_SIZE (pattern
);
1155 TYPE_HAS_ASSIGN_REF (type
) = TYPE_HAS_ASSIGN_REF (pattern
);
1156 TYPE_HAS_CONST_ASSIGN_REF (type
) = TYPE_HAS_CONST_ASSIGN_REF (pattern
);
1157 TYPE_HAS_ABSTRACT_ASSIGN_REF (type
) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern
);
1158 TYPE_HAS_INIT_REF (type
) = TYPE_HAS_INIT_REF (pattern
);
1159 TYPE_HAS_CONST_INIT_REF (type
) = TYPE_HAS_CONST_INIT_REF (pattern
);
1160 TYPE_GETS_INIT_AGGR (type
) = TYPE_GETS_INIT_AGGR (pattern
);
1161 TYPE_HAS_DEFAULT_CONSTRUCTOR (type
) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern
);
1162 TYPE_HAS_CONVERSION (type
) = TYPE_HAS_CONVERSION (pattern
);
1163 TYPE_USES_COMPLEX_INHERITANCE (type
)
1164 = TYPE_USES_COMPLEX_INHERITANCE (pattern
);
1165 TYPE_USES_VIRTUAL_BASECLASSES (type
)
1166 = TYPE_USES_VIRTUAL_BASECLASSES (pattern
);
1168 if (! uses_template_parms (type
))
1171 for (tmp
= TYPE_FIELDS (type
); tmp
; tmp
= TREE_CHAIN (tmp
))
1172 if (TREE_CODE (tmp
) == FIELD_DECL
1173 && TREE_CODE (DECL_NAME (tmp
)) != SCOPE_REF
)
1174 require_complete_type (tmp
);
1176 /* XXX handle attributes */
1177 type
= finish_struct_1 (type
, 0, 0);
1181 TYPE_SIZE (type
) = integer_zero_node
;
1182 CLASSTYPE_METHOD_VEC (type
)
1183 = finish_struct_methods (type
, TYPE_METHODS (type
), 1);
1186 TYPE_BEING_DEFINED (type
) = 0;
1189 pop_from_top_level ();
1199 if (t1
== NULL_TREE
)
1200 return t2
== NULL_TREE
;
1201 if (t2
== NULL_TREE
)
1203 /* Don't care if one declares its arg const and the other doesn't -- the
1204 main variant of the arg type is all that matters. */
1205 if (TYPE_MAIN_VARIANT (TREE_VALUE (t1
))
1206 != TYPE_MAIN_VARIANT (TREE_VALUE (t2
)))
1208 return list_eq (TREE_CHAIN (t1
), TREE_CHAIN (t2
));
1212 lookup_nested_type_by_name (ctype
, name
)
1217 complete_type (ctype
);
1219 for (t
= CLASSTYPE_TAGS (ctype
); t
; t
= TREE_CHAIN (t
))
1221 if (name
== TREE_PURPOSE (t
))
1222 return TREE_VALUE (t
);
1228 tsubst (t
, args
, nargs
, in_decl
)
1235 if (t
== NULL_TREE
|| t
== error_mark_node
1236 || t
== integer_type_node
1237 || t
== void_type_node
1238 || t
== char_type_node
)
1241 type
= TREE_TYPE (t
);
1242 if (type
== unknown_type_node
)
1243 my_friendly_abort (42);
1244 if (type
&& TREE_CODE (t
) != FUNCTION_DECL
)
1245 type
= tsubst (type
, args
, nargs
, in_decl
);
1247 switch (TREE_CODE (t
))
1250 if (TYPE_PTRMEMFUNC_P (t
))
1252 tree r
= build_ptrmemfunc_type
1253 (tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t
), args
, nargs
, in_decl
));
1254 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1258 /* else fall through */
1260 if (uses_template_parms (t
))
1262 tree argvec
= tsubst (CLASSTYPE_TI_ARGS (t
), args
, nargs
, in_decl
);
1263 tree r
= lookup_template_class (t
, argvec
, in_decl
);
1264 return cp_build_type_variant (r
, TYPE_READONLY (t
),
1268 /* else fall through */
1270 case IDENTIFIER_NODE
:
1282 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1283 if (ctx
== NULL_TREE
)
1286 return lookup_nested_type_by_name (ctx
, TYPE_IDENTIFIER (t
));
1290 if (t
== integer_type_node
)
1293 if (TREE_CODE (TYPE_MIN_VALUE (t
)) == INTEGER_CST
1294 && TREE_CODE (TYPE_MAX_VALUE (t
)) == INTEGER_CST
)
1298 tree max
= tsubst_expr (TYPE_MAX_VALUE (t
), args
, nargs
, in_decl
);
1299 if (current_template_parms
)
1301 tree itype
= make_node (INTEGER_TYPE
);
1302 TYPE_MIN_VALUE (itype
) = size_zero_node
;
1303 TYPE_MAX_VALUE (itype
) = max
;
1306 return build_index_2_type (size_zero_node
, max
);
1309 case TEMPLATE_TYPE_PARM
:
1311 tree arg
= args
[TEMPLATE_TYPE_IDX (t
)];
1312 return cp_build_type_variant
1313 (arg
, TYPE_READONLY (arg
) || TYPE_READONLY (t
),
1314 TYPE_VOLATILE (arg
) || TYPE_VOLATILE (t
));
1317 case TEMPLATE_CONST_PARM
:
1318 return args
[TEMPLATE_CONST_IDX (t
)];
1323 tree arg_types
, ctx
;
1327 if (DECL_CONTEXT (t
) != NULL_TREE
1328 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (t
))) == 't')
1330 if (DECL_NAME (t
) == constructor_name (DECL_CONTEXT (t
)))
1334 ctx
= tsubst (DECL_CLASS_CONTEXT (t
), args
, nargs
, t
);
1335 type
= tsubst (type
, args
, nargs
, in_decl
);
1341 type
= tsubst (type
, args
, nargs
, in_decl
);
1344 if (type
== TREE_TYPE (t
)
1345 && (! member
|| ctx
== DECL_CLASS_CONTEXT (t
)))
1348 /* Do we already have this instantiation? */
1349 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1351 tree tmpl
= TREE_PURPOSE (DECL_TEMPLATE_INFO (t
));
1352 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1354 for (; decls
; decls
= TREE_CHAIN (decls
))
1355 if (TREE_TYPE (TREE_VALUE (decls
)) == type
1356 && DECL_CLASS_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1357 return TREE_VALUE (decls
);
1360 /* We do NOT check for matching decls pushed separately at this
1361 point, as they may not represent instantiations of this
1362 template, and in any case are considered separate under the
1367 TREE_TYPE (r
) = type
;
1370 = tsubst (DECL_CONTEXT (t
), args
, nargs
, t
);
1371 DECL_CLASS_CONTEXT (r
) = ctx
;
1373 if (member
&& !strncmp (OPERATOR_TYPENAME_FORMAT
,
1374 IDENTIFIER_POINTER (DECL_NAME (r
)),
1375 sizeof (OPERATOR_TYPENAME_FORMAT
) - 1))
1377 /* Type-conversion operator. Reconstruct the name, in
1378 case it's the name of one of the template's parameters. */
1379 DECL_NAME (r
) = build_typename_overload (TREE_TYPE (type
));
1382 arg_types
= TYPE_VALUES (type
);
1384 if (member
&& TREE_CODE (type
) == FUNCTION_TYPE
)
1385 arg_types
= hash_tree_chain
1386 (build_pointer_type (DECL_CONTEXT (r
)), arg_types
);
1388 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (t
)))
1390 char *buf
, *dbuf
= build_overload_name (ctx
, 1, 1);
1391 int len
= sizeof (DESTRUCTOR_DECL_PREFIX
) - 1;
1392 buf
= (char *) alloca (strlen (dbuf
)
1393 + sizeof (DESTRUCTOR_DECL_PREFIX
));
1394 bcopy (DESTRUCTOR_DECL_PREFIX
, buf
, len
);
1397 DECL_ASSEMBLER_NAME (r
) = get_identifier (buf
);
1400 DECL_ASSEMBLER_NAME (r
)
1401 = build_decl_overload (DECL_NAME (r
), arg_types
, member
);
1403 make_decl_rtl (r
, NULL_PTR
, 1);
1405 DECL_ARGUMENTS (r
) = tsubst (DECL_ARGUMENTS (t
), args
, nargs
, t
);
1406 DECL_MAIN_VARIANT (r
) = r
;
1407 DECL_RESULT (r
) = NULL_TREE
;
1408 DECL_INITIAL (r
) = NULL_TREE
;
1410 TREE_STATIC (r
) = 0;
1411 TREE_PUBLIC (r
) = 1;
1412 DECL_EXTERNAL (r
) = 1;
1413 DECL_INTERFACE_KNOWN (r
) = 0;
1414 DECL_DEFER_OUTPUT (r
) = 0;
1415 TREE_CHAIN (r
) = NULL_TREE
;
1416 DECL_CHAIN (r
) = NULL_TREE
;
1418 if (IDENTIFIER_OPNAME_P (DECL_NAME (r
)))
1419 grok_op_properties (r
, DECL_VIRTUAL_P (r
), DECL_FRIEND_P (r
));
1421 /* Look for matching decls for the moment. */
1424 tree decls
= lookup_name_nonclass (DECL_NAME (t
));
1427 if (decls
== NULL_TREE
)
1429 else if (is_overloaded_fn (decls
))
1430 for (decls
= get_first_fn (decls
); decls
;
1431 decls
= DECL_CHAIN (decls
))
1433 if (TREE_CODE (decls
) == FUNCTION_DECL
1434 && TREE_TYPE (decls
) == type
)
1443 int dcl_only
= ! DECL_INITIAL (d
);
1445 DECL_INITIAL (r
) = error_mark_node
;
1446 duplicate_decls (r
, d
);
1449 DECL_INITIAL (r
) = 0;
1453 if (DECL_TEMPLATE_INFO (t
) != NULL_TREE
)
1455 tree tmpl
= DECL_TI_TEMPLATE (t
);
1456 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1457 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1458 args
, nargs
, in_decl
);
1460 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1461 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1463 /* If we have a preexisting version of this function, don't expand
1464 the template version, use the other instead. */
1465 if (TREE_STATIC (r
) || DECL_TEMPLATE_SPECIALIZATION (r
))
1466 SET_DECL_TEMPLATE_SPECIALIZATION (r
);
1468 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1470 DECL_TEMPLATE_INSTANTIATIONS (tmpl
) =
1471 tree_cons (argvec
, r
, DECL_TEMPLATE_INSTANTIATIONS (tmpl
));
1479 tree r
= copy_node (t
);
1480 TREE_TYPE (r
) = type
;
1481 DECL_INITIAL (r
) = TREE_TYPE (r
);
1482 DECL_CONTEXT (r
) = NULL_TREE
;
1483 #ifdef PROMOTE_PROTOTYPES
1484 if ((TREE_CODE (type
) == INTEGER_TYPE
1485 || TREE_CODE (type
) == ENUMERAL_TYPE
)
1486 && TYPE_PRECISION (type
) < TYPE_PRECISION (integer_type_node
))
1487 DECL_ARG_TYPE (r
) = integer_type_node
;
1490 TREE_CHAIN (r
) = tsubst (TREE_CHAIN (t
), args
, nargs
, TREE_CHAIN (t
));
1496 tree r
= copy_node (t
);
1497 TREE_TYPE (r
) = type
;
1500 DECL_FIELD_CONTEXT (r
) = tsubst (DECL_FIELD_CONTEXT (t
), args
, nargs
, in_decl
);
1502 DECL_INITIAL (r
) = tsubst_expr (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1503 TREE_CHAIN (r
) = NULL_TREE
;
1509 tree r
= copy_node (t
);
1511 = tsubst_copy (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1512 TREE_CHAIN (r
) = NULL_TREE
;
1519 tree ctx
= tsubst_copy (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1521 /* Do we already have this instantiation? */
1522 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1524 tree tmpl
= DECL_TI_TEMPLATE (t
);
1525 tree decls
= DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1527 for (; decls
; decls
= TREE_CHAIN (decls
))
1528 if (DECL_CONTEXT (TREE_VALUE (decls
)) == ctx
)
1529 return TREE_VALUE (decls
);
1533 TREE_TYPE (r
) = type
;
1534 DECL_CONTEXT (r
) = ctx
;
1535 if (TREE_STATIC (r
))
1536 DECL_ASSEMBLER_NAME (r
)
1537 = build_static_name (DECL_CONTEXT (r
), DECL_NAME (r
));
1538 DECL_INITIAL (r
) = tsubst_expr
1539 (DECL_INITIAL (t
), args
, nargs
, in_decl
);
1544 if (DECL_LANG_SPECIFIC (r
))
1547 DECL_CLASS_CONTEXT (r
) = DECL_CONTEXT (r
);
1550 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1552 tree tmpl
= DECL_TI_TEMPLATE (t
);
1553 tree
*declsp
= &DECL_TEMPLATE_INSTANTIATIONS (tmpl
);
1554 tree argvec
= tsubst (TREE_VALUE (DECL_TEMPLATE_INFO (t
)),
1555 args
, nargs
, in_decl
);
1557 DECL_TEMPLATE_INFO (r
) = perm_tree_cons (tmpl
, argvec
, NULL_TREE
);
1558 *declsp
= perm_tree_cons (argvec
, r
, *declsp
);
1559 SET_DECL_IMPLICIT_INSTANTIATION (r
);
1561 TREE_CHAIN (r
) = NULL_TREE
;
1567 tree r
= copy_node (t
);
1568 TREE_TYPE (r
) = type
;
1569 set_nested_typename (r
, current_class_name
, DECL_NAME (r
), type
);
1570 TREE_CHAIN (r
) = NULL_TREE
;
1576 tree purpose
, value
, chain
, result
;
1577 int via_public
, via_virtual
, via_protected
;
1579 if (t
== void_list_node
)
1582 via_public
= TREE_VIA_PUBLIC (t
);
1583 via_protected
= TREE_VIA_PROTECTED (t
);
1584 via_virtual
= TREE_VIA_VIRTUAL (t
);
1586 purpose
= TREE_PURPOSE (t
);
1588 purpose
= tsubst (purpose
, args
, nargs
, in_decl
);
1589 value
= TREE_VALUE (t
);
1591 value
= tsubst (value
, args
, nargs
, in_decl
);
1592 chain
= TREE_CHAIN (t
);
1593 if (chain
&& chain
!= void_type_node
)
1594 chain
= tsubst (chain
, args
, nargs
, in_decl
);
1595 if (purpose
== TREE_PURPOSE (t
)
1596 && value
== TREE_VALUE (t
)
1597 && chain
== TREE_CHAIN (t
))
1599 result
= hash_tree_cons (via_public
, via_virtual
, via_protected
,
1600 purpose
, value
, chain
);
1601 TREE_PARMLIST (result
) = TREE_PARMLIST (t
);
1605 if (type
!= NULL_TREE
)
1609 if (type
== TREE_TYPE (t
))
1612 TREE_TYPE (t
) = complete_type (type
);
1613 BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (type
);
1614 BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (type
);
1615 if (TYPE_BINFO_BASETYPES (type
) != NULL_TREE
)
1616 BINFO_BASETYPES (t
) = copy_node (TYPE_BINFO_BASETYPES (type
));
1621 int len
= TREE_VEC_LENGTH (t
), need_new
= 0, i
;
1622 tree
*elts
= (tree
*) alloca (len
* sizeof (tree
));
1624 bzero ((char *) elts
, len
* sizeof (tree
));
1626 for (i
= 0; i
< len
; i
++)
1628 elts
[i
] = tsubst_expr (TREE_VEC_ELT (t
, i
), args
, nargs
, in_decl
);
1629 if (elts
[i
] != TREE_VEC_ELT (t
, i
))
1636 t
= make_tree_vec (len
);
1637 for (i
= 0; i
< len
; i
++)
1638 TREE_VEC_ELT (t
, i
) = elts
[i
];
1643 case REFERENCE_TYPE
:
1646 enum tree_code code
;
1647 if (type
== TREE_TYPE (t
))
1650 code
= TREE_CODE (t
);
1651 if (code
== POINTER_TYPE
)
1652 r
= build_pointer_type (type
);
1654 r
= build_reference_type (type
);
1655 r
= cp_build_type_variant (r
, TYPE_READONLY (t
), TYPE_VOLATILE (t
));
1656 /* Will this ever be needed for TYPE_..._TO values? */
1661 return build_offset_type
1662 (tsubst (TYPE_OFFSET_BASETYPE (t
), args
, nargs
, in_decl
), type
);
1666 tree values
= TYPE_ARG_TYPES (t
);
1667 tree context
= TYPE_CONTEXT (t
);
1670 /* Don't bother recursing if we know it won't change anything. */
1671 if (values
!= void_list_node
)
1673 /* This should probably be rewritten to use hash_tree_cons for
1674 the memory savings. */
1675 tree first
= NULL_TREE
;
1678 for (; values
&& values
!= void_list_node
;
1679 values
= TREE_CHAIN (values
))
1682 = tsubst (TREE_VALUE (values
), args
, nargs
, in_decl
);
1683 tree purpose
= tsubst_expr (TREE_PURPOSE (values
),
1684 args
, nargs
, in_decl
);
1685 tree x
= build_tree_list (purpose
, value
);
1688 TREE_CHAIN (last
) = x
;
1694 if (values
== void_list_node
)
1695 TREE_CHAIN (last
) = void_list_node
;
1700 context
= tsubst (context
, args
, nargs
, in_decl
);
1701 /* Could also optimize cases where return value and
1702 values have common elements (e.g., T min(const &T, const T&). */
1704 /* If the above parameters haven't changed, just return the type. */
1705 if (type
== TREE_TYPE (t
)
1706 && values
== TYPE_VALUES (t
)
1707 && context
== TYPE_CONTEXT (t
))
1710 /* Construct a new type node and return it. */
1711 if (TREE_CODE (t
) == FUNCTION_TYPE
1712 && context
== NULL_TREE
)
1714 new_value
= build_function_type (type
, values
);
1716 else if (context
== NULL_TREE
)
1718 tree base
= tsubst (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t
))),
1719 args
, nargs
, in_decl
);
1720 new_value
= build_cplus_method_type (base
, type
,
1721 TREE_CHAIN (values
));
1725 new_value
= make_node (TREE_CODE (t
));
1726 TREE_TYPE (new_value
) = type
;
1727 TYPE_CONTEXT (new_value
) = context
;
1728 TYPE_VALUES (new_value
) = values
;
1729 TYPE_SIZE (new_value
) = TYPE_SIZE (t
);
1730 TYPE_ALIGN (new_value
) = TYPE_ALIGN (t
);
1731 TYPE_MODE (new_value
) = TYPE_MODE (t
);
1732 if (TYPE_METHOD_BASETYPE (t
))
1733 TYPE_METHOD_BASETYPE (new_value
) = tsubst (TYPE_METHOD_BASETYPE (t
),
1734 args
, nargs
, in_decl
);
1735 /* Need to generate hash value. */
1736 my_friendly_abort (84);
1738 new_value
= build_type_variant (new_value
,
1745 tree domain
= tsubst (TYPE_DOMAIN (t
), args
, nargs
, in_decl
);
1747 if (type
== TREE_TYPE (t
) && domain
== TYPE_DOMAIN (t
))
1749 r
= build_cplus_array_type (type
, domain
);
1755 return fold (build (TREE_CODE (t
), TREE_TYPE (t
),
1756 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1757 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
)));
1761 return fold (build1 (TREE_CODE (t
), TREE_TYPE (t
),
1762 tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
)));
1766 tree ctx
= tsubst (TYPE_CONTEXT (t
), args
, nargs
, in_decl
);
1767 tree f
= make_typename_type (ctx
, TYPE_IDENTIFIER (t
));
1768 return cp_build_type_variant
1769 (f
, TYPE_READONLY (f
) || TYPE_READONLY (t
),
1770 TYPE_VOLATILE (f
) || TYPE_VOLATILE (t
));
1774 return make_pointer_declarator
1775 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1778 return make_reference_declarator
1779 (type
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1782 return build_parse_node
1783 (ARRAY_REF
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1784 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1787 return build_parse_node
1788 (CALL_EXPR
, tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1789 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
), 0);
1792 return build_parse_node
1793 (TREE_CODE (t
), tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1794 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1797 sorry ("use of `%s' in template",
1798 tree_code_name
[(int) TREE_CODE (t
)]);
1799 return error_mark_node
;
1806 emit_line_note (input_filename
, lineno
);
1810 expand_start_bindings (0);
1818 expand_end_bindings (getdecls (), kept_level_p (), 1);
1819 t
= poplevel (kept_level_p (), 1, 0);
1825 tsubst_copy (t
, args
, nargs
, in_decl
)
1830 enum tree_code code
;
1832 if (t
== NULL_TREE
|| t
== error_mark_node
)
1835 code
= TREE_CODE (t
);
1840 return do_identifier (DECL_NAME (t
), 0);
1844 if (DECL_CONTEXT (t
))
1846 tree ctx
= tsubst (DECL_CONTEXT (t
), args
, nargs
, in_decl
);
1847 if (ctx
!= DECL_CONTEXT (t
))
1848 return lookup_field (ctx
, DECL_NAME (t
), 0, 0);
1854 if (DECL_LANG_SPECIFIC (t
) && DECL_TEMPLATE_INFO (t
))
1855 t
= tsubst (t
, args
, nargs
, in_decl
);
1860 case IDENTIFIER_NODE
:
1861 return do_identifier (t
, 0);
1865 case REINTERPRET_CAST_EXPR
:
1867 (code
, tsubst (TREE_TYPE (t
), args
, nargs
, in_decl
),
1868 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1871 case PREDECREMENT_EXPR
:
1872 case PREINCREMENT_EXPR
:
1873 case POSTDECREMENT_EXPR
:
1874 case POSTINCREMENT_EXPR
:
1876 case TRUTH_NOT_EXPR
:
1878 case CONVERT_EXPR
: /* Unary + */
1884 tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
1889 case TRUNC_DIV_EXPR
:
1891 case FLOOR_DIV_EXPR
:
1892 case ROUND_DIV_EXPR
:
1893 case EXACT_DIV_EXPR
:
1895 case BIT_ANDTC_EXPR
:
1898 case TRUNC_MOD_EXPR
:
1899 case FLOOR_MOD_EXPR
:
1900 case TRUTH_ANDIF_EXPR
:
1901 case TRUTH_ORIF_EXPR
:
1902 case TRUTH_AND_EXPR
:
1923 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1924 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1928 tree fn
= TREE_OPERAND (t
, 0);
1929 if (really_overloaded_fn (fn
))
1930 fn
= tsubst_copy (TREE_VALUE (fn
), args
, nargs
, in_decl
);
1932 fn
= tsubst_copy (fn
, args
, nargs
, in_decl
);
1934 (code
, fn
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1938 case METHOD_CALL_EXPR
:
1940 tree name
= TREE_OPERAND (t
, 0);
1941 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
1943 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1944 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
1946 else if (TREE_CODE (name
) == SCOPE_REF
1947 && TREE_CODE (TREE_OPERAND (name
, 1)) == BIT_NOT_EXPR
)
1949 tree base
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1950 name
= TREE_OPERAND (name
, 1);
1951 name
= tsubst_copy (TREE_OPERAND (name
, 0), args
, nargs
, in_decl
);
1952 name
= build1 (BIT_NOT_EXPR
, NULL_TREE
, TYPE_MAIN_VARIANT (name
));
1953 name
= build_nt (SCOPE_REF
, base
, name
);
1956 name
= tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
1958 (code
, name
, tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1959 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
),
1966 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1967 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1968 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1973 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1974 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
1975 tsubst_copy (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
1976 NEW_EXPR_USE_GLOBAL (r
) = NEW_EXPR_USE_GLOBAL (t
);
1983 (code
, tsubst_copy (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
1984 tsubst_copy (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
1985 DELETE_EXPR_USE_GLOBAL (r
) = DELETE_EXPR_USE_GLOBAL (t
);
1986 DELETE_EXPR_USE_VEC (r
) = DELETE_EXPR_USE_VEC (t
);
1992 tree purpose
, value
, chain
;
1994 if (t
== void_list_node
)
1997 purpose
= TREE_PURPOSE (t
);
1999 purpose
= tsubst_copy (purpose
, args
, nargs
, in_decl
);
2000 value
= TREE_VALUE (t
);
2002 value
= tsubst_copy (value
, args
, nargs
, in_decl
);
2003 chain
= TREE_CHAIN (t
);
2004 if (chain
&& chain
!= void_type_node
)
2005 chain
= tsubst_copy (chain
, args
, nargs
, in_decl
);
2006 if (purpose
== TREE_PURPOSE (t
)
2007 && value
== TREE_VALUE (t
)
2008 && chain
== TREE_CHAIN (t
))
2010 return tree_cons (purpose
, value
, chain
);
2017 case TEMPLATE_TYPE_PARM
:
2018 case TEMPLATE_CONST_PARM
:
2020 case REFERENCE_TYPE
:
2026 return tsubst (t
, args
, nargs
, in_decl
);
2034 tsubst_expr (t
, args
, nargs
, in_decl
)
2039 if (t
== NULL_TREE
|| t
== error_mark_node
)
2042 if (current_template_parms
)
2043 return tsubst_copy (t
, args
, nargs
, in_decl
);
2045 switch (TREE_CODE (t
))
2048 lineno
= TREE_COMPLEXITY (t
);
2049 emit_line_note (input_filename
, lineno
);
2051 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2056 lineno
= TREE_COMPLEXITY (t
);
2057 emit_line_note (input_filename
, lineno
);
2058 t
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2059 /* Do default conversion if safe and possibly important,
2060 in case within ({...}). */
2061 if ((TREE_CODE (TREE_TYPE (t
)) == ARRAY_TYPE
&& lvalue_p (t
))
2062 || TREE_CODE (TREE_TYPE (t
)) == FUNCTION_TYPE
)
2063 t
= default_conversion (t
);
2064 cplus_expand_expr_stmt (t
);
2071 int i
= suspend_momentary ();
2074 lineno
= TREE_COMPLEXITY (t
);
2075 emit_line_note (input_filename
, lineno
);
2077 (tsubst (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2078 tsubst (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
),
2079 TREE_OPERAND (t
, 3) != 0,
2080 tsubst (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
));
2082 (dcl
, tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
),
2083 NULL_TREE
, 1, LOOKUP_ONLYCONVERTING
);
2084 resume_momentary (i
);
2091 int init_scope
= (flag_new_for_scope
> 0 && TREE_OPERAND (t
, 0)
2092 && TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2093 int cond_scope
= (TREE_OPERAND (t
, 1)
2094 && TREE_CODE (TREE_OPERAND (t
, 1)) == DECL_STMT
);
2096 lineno
= TREE_COMPLEXITY (t
);
2097 emit_line_note (input_filename
, lineno
);
2100 for (tmp
= TREE_OPERAND (t
, 0); tmp
; tmp
= TREE_CHAIN (tmp
))
2101 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2103 emit_line_note (input_filename
, lineno
);
2104 expand_start_loop_continue_elsewhere (1);
2108 tmp
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2109 emit_line_note (input_filename
, lineno
);
2111 expand_exit_loop_if_false (0, condition_conversion (tmp
));
2115 tsubst_expr (TREE_OPERAND (t
, 3), args
, nargs
, in_decl
);
2118 emit_line_note (input_filename
, lineno
);
2119 expand_loop_continue_here ();
2120 tmp
= tsubst_expr (TREE_OPERAND (t
, 2), args
, nargs
, in_decl
);
2122 cplus_expand_expr_stmt (tmp
);
2135 lineno
= TREE_COMPLEXITY (t
);
2137 emit_line_note (input_filename
, lineno
);
2138 expand_start_loop (1);
2140 cond
= TREE_OPERAND (t
, 0);
2141 if (TREE_CODE (cond
) == DECL_STMT
)
2143 cond
= tsubst_expr (cond
, args
, nargs
, in_decl
);
2144 emit_line_note (input_filename
, lineno
);
2145 expand_exit_loop_if_false (0, condition_conversion (cond
));
2147 if (TREE_CODE (TREE_OPERAND (t
, 0)) != DECL_STMT
)
2149 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2161 lineno
= TREE_COMPLEXITY (t
);
2163 emit_line_note (input_filename
, lineno
);
2164 expand_start_loop_continue_elsewhere (1);
2166 tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2167 expand_loop_continue_here ();
2169 cond
= tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
);
2170 emit_line_note (input_filename
, lineno
);
2171 expand_exit_loop_if_false (0, condition_conversion (cond
));
2182 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2184 lineno
= TREE_COMPLEXITY (t
);
2187 tmp
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2188 emit_line_note (input_filename
, lineno
);
2189 expand_start_cond (condition_conversion (tmp
), 0);
2191 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2192 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2194 if (tmp
= TREE_OPERAND (t
, 2), tmp
)
2196 expand_start_else ();
2197 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2211 tree substmt
= TREE_OPERAND (t
, 0);
2213 lineno
= TREE_COMPLEXITY (t
);
2215 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2218 for (; substmt
; substmt
= TREE_CHAIN (substmt
))
2219 tsubst_expr (substmt
, args
, nargs
, in_decl
);
2221 if (COMPOUND_STMT_NO_SCOPE (t
) == 0)
2227 lineno
= TREE_COMPLEXITY (t
);
2228 emit_line_note (input_filename
, lineno
);
2229 if (! expand_exit_something ())
2230 error ("break statement not within loop or switch");
2236 int cond_scope
= (TREE_CODE (TREE_OPERAND (t
, 0)) == DECL_STMT
);
2238 lineno
= TREE_COMPLEXITY (t
);
2241 val
= tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
);
2242 emit_line_note (input_filename
, lineno
);
2243 c_expand_start_case (val
);
2246 if (tmp
= TREE_OPERAND (t
, 1), tmp
)
2247 tsubst_expr (tmp
, args
, nargs
, in_decl
);
2249 expand_end_case (val
);
2260 do_case (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
),
2261 tsubst_expr (TREE_OPERAND (t
, 1), args
, nargs
, in_decl
));
2265 t
= define_label (DECL_SOURCE_FILE (t
), DECL_SOURCE_LINE (t
),
2272 lineno
= TREE_COMPLEXITY (t
);
2273 emit_line_note (input_filename
, lineno
);
2274 if (TREE_CODE (TREE_OPERAND (t
, 0)) == IDENTIFIER_NODE
)
2276 tree decl
= lookup_label (TREE_OPERAND (t
, 0));
2277 TREE_USED (decl
) = 1;
2281 expand_computed_goto
2282 (tsubst_expr (TREE_OPERAND (t
, 0), args
, nargs
, in_decl
));
2286 return build_expr_from_tree (tsubst_copy (t
, args
, nargs
, in_decl
));
2292 instantiate_template (tmpl
, targ_ptr
)
2293 tree tmpl
, *targ_ptr
;
2297 struct obstack
*old_fmp_obstack
;
2298 extern struct obstack
*function_maybepermanent_obstack
;
2300 push_obstacks (&permanent_obstack
, &permanent_obstack
);
2301 old_fmp_obstack
= function_maybepermanent_obstack
;
2302 function_maybepermanent_obstack
= &permanent_obstack
;
2304 my_friendly_assert (TREE_CODE (tmpl
) == TEMPLATE_DECL
, 283);
2305 len
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (tmpl
));
2310 tree t
= targ_ptr
[i
];
2311 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
2313 tree nt
= target_type (t
);
2314 if (IS_AGGR_TYPE (nt
) && decl_function_context (TYPE_MAIN_DECL (nt
)))
2316 cp_error ("type `%T' composed from a local class is not a valid template-argument", t
);
2317 cp_error (" trying to instantiate `%D'", tmpl
);
2318 fndecl
= error_mark_node
;
2322 targ_ptr
[i
] = copy_to_permanent (t
);
2325 /* substitute template parameters */
2326 fndecl
= tsubst (DECL_RESULT (tmpl
), targ_ptr
, len
, tmpl
);
2329 function_maybepermanent_obstack
= old_fmp_obstack
;
2335 /* Push the name of the class template into the scope of the instantiation. */
2338 overload_template_name (type
)
2341 tree id
= DECL_NAME (CLASSTYPE_TI_TEMPLATE (type
));
2344 if (IDENTIFIER_CLASS_VALUE (id
)
2345 && TREE_TYPE (IDENTIFIER_CLASS_VALUE (id
)) == type
)
2348 decl
= build_decl (TYPE_DECL
, id
, type
);
2349 SET_DECL_ARTIFICIAL (decl
);
2350 pushdecl_class_level (decl
);
2353 /* Type unification.
2355 We have a function template signature with one or more references to
2356 template parameters, and a parameter list we wish to fit to this
2357 template. If possible, produce a list of parameters for the template
2358 which will cause it to fit the supplied parameter list.
2360 Return zero for success, 2 for an incomplete match that doesn't resolve
2361 all the types, and 1 for complete failure. An error message will be
2362 printed only for an incomplete match.
2364 TPARMS[NTPARMS] is an array of template parameter types;
2365 TARGS[NTPARMS] is the array of template parameter values. PARMS is
2366 the function template's signature (using TEMPLATE_PARM_IDX nodes),
2367 and ARGS is the argument list we're trying to match against it.
2369 If SUBR is 1, we're being called recursively (to unify the arguments of
2370 a function or method parameter of a function template), so don't zero
2371 out targs and don't fail on an incomplete match. */
2374 type_unification (tparms
, targs
, parms
, args
, nsubsts
, subr
)
2375 tree tparms
, *targs
, parms
, args
;
2380 int ntparms
= TREE_VEC_LENGTH (tparms
);
2382 my_friendly_assert (TREE_CODE (tparms
) == TREE_VEC
, 289);
2383 my_friendly_assert (TREE_CODE (parms
) == TREE_LIST
, 290);
2384 /* ARGS could be NULL (via a call from parse.y to
2385 build_x_function_call). */
2387 my_friendly_assert (TREE_CODE (args
) == TREE_LIST
, 291);
2388 my_friendly_assert (ntparms
> 0, 292);
2391 bzero ((char *) targs
, sizeof (tree
) * ntparms
);
2394 && parms
!= void_list_node
2396 && args
!= void_list_node
)
2398 parm
= TREE_VALUE (parms
);
2399 parms
= TREE_CHAIN (parms
);
2400 arg
= TREE_VALUE (args
);
2401 args
= TREE_CHAIN (args
);
2403 if (arg
== error_mark_node
)
2405 if (arg
== unknown_type_node
)
2408 if (! uses_template_parms (parm
)
2409 && TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2411 if (can_convert_arg (parm
, TREE_TYPE (arg
), arg
))
2417 if (TREE_CODE (arg
) == VAR_DECL
)
2418 arg
= TREE_TYPE (arg
);
2419 else if (TREE_CODE_CLASS (TREE_CODE (arg
)) == 'e')
2420 arg
= TREE_TYPE (arg
);
2422 if (TREE_CODE_CLASS (TREE_CODE (arg
)) != 't')
2424 my_friendly_assert (TREE_TYPE (arg
) != NULL_TREE
, 293);
2425 if (TREE_CODE (arg
) == TREE_LIST
2426 && TREE_TYPE (arg
) == unknown_type_node
2427 && TREE_CODE (TREE_VALUE (arg
)) == TEMPLATE_DECL
)
2429 int nsubsts
, ntparms
;
2432 /* Have to back unify here */
2433 arg
= TREE_VALUE (arg
);
2435 ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (arg
));
2436 targs
= (tree
*) alloca (sizeof (tree
) * ntparms
);
2437 parm
= tree_cons (NULL_TREE
, parm
, NULL_TREE
);
2438 return type_unification (DECL_TEMPLATE_PARMS (arg
), targs
,
2439 TYPE_ARG_TYPES (TREE_TYPE (arg
)),
2442 arg
= TREE_TYPE (arg
);
2445 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2446 arg
= TREE_TYPE (arg
);
2448 if (TREE_CODE (parm
) != REFERENCE_TYPE
)
2450 if (TREE_CODE (arg
) == FUNCTION_TYPE
2451 || TREE_CODE (arg
) == METHOD_TYPE
)
2452 arg
= build_pointer_type (arg
);
2453 else if (TREE_CODE (arg
) == ARRAY_TYPE
)
2454 arg
= build_pointer_type (TREE_TYPE (arg
));
2456 arg
= TYPE_MAIN_VARIANT (arg
);
2459 switch (unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
))
2467 /* Fail if we've reached the end of the parm list, and more args
2468 are present, and the parm list isn't variadic. */
2469 if (args
&& args
!= void_list_node
&& parms
== void_list_node
)
2471 /* Fail if parms are left and they don't have default values. */
2473 && parms
!= void_list_node
2474 && TREE_PURPOSE (parms
) == NULL_TREE
)
2477 for (i
= 0; i
< ntparms
; i
++)
2480 error ("incomplete type unification");
2486 /* Tail recursion is your friend. */
2488 unify (tparms
, targs
, ntparms
, parm
, arg
, nsubsts
)
2489 tree tparms
, *targs
, parm
, arg
;
2490 int *nsubsts
, ntparms
;
2494 /* I don't think this will do the right thing with respect to types.
2495 But the only case I've seen it in so far has been array bounds, where
2496 signedness is the only information lost, and I think that will be
2498 while (TREE_CODE (parm
) == NOP_EXPR
)
2499 parm
= TREE_OPERAND (parm
, 0);
2501 if (arg
== error_mark_node
)
2503 if (arg
== unknown_type_node
)
2508 switch (TREE_CODE (parm
))
2510 case TEMPLATE_TYPE_PARM
:
2512 idx
= TEMPLATE_TYPE_IDX (parm
);
2514 /* Template type parameters cannot contain cv-quals; i.e.
2515 template <class T> void f (T& a, T& b) will not generate
2516 void f (const int& a, const int& b). */
2517 if (TYPE_READONLY (arg
) > TYPE_READONLY (parm
)
2518 || TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
))
2520 arg
= TYPE_MAIN_VARIANT (arg
);
2523 int constp
= TYPE_READONLY (arg
) > TYPE_READONLY (parm
);
2524 int volatilep
= TYPE_VOLATILE (arg
) > TYPE_VOLATILE (parm
);
2525 arg
= cp_build_type_variant (arg
, constp
, volatilep
);
2528 /* Simple cases: Value already set, does match or doesn't. */
2529 if (targs
[idx
] == arg
)
2531 else if (targs
[idx
])
2533 /* Check for mixed types and values. */
2534 if (TREE_CODE (TREE_VALUE (TREE_VEC_ELT (tparms
, idx
))) != TYPE_DECL
)
2538 case TEMPLATE_CONST_PARM
:
2540 idx
= TEMPLATE_CONST_IDX (parm
);
2541 if (targs
[idx
] == arg
)
2543 else if (targs
[idx
])
2545 tree t
= targs
[idx
];
2546 if (TREE_CODE (t
) == TREE_CODE (arg
))
2547 switch (TREE_CODE (arg
))
2550 if (tree_int_cst_equal (t
, arg
))
2554 if (REAL_VALUES_EQUAL (TREE_REAL_CST (t
), TREE_REAL_CST (arg
)))
2557 /* STRING_CST values are not valid template const parms. */
2561 my_friendly_abort (87);
2564 /* else if (typeof arg != tparms[idx])
2567 targs
[idx
] = copy_to_permanent (arg
);
2571 if (TREE_CODE (arg
) != POINTER_TYPE
)
2573 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2576 case REFERENCE_TYPE
:
2577 if (TREE_CODE (arg
) == REFERENCE_TYPE
)
2578 arg
= TREE_TYPE (arg
);
2579 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), arg
, nsubsts
);
2582 if (TREE_CODE (arg
) != ARRAY_TYPE
)
2584 if (unify (tparms
, targs
, ntparms
, TYPE_DOMAIN (parm
), TYPE_DOMAIN (arg
),
2587 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
), TREE_TYPE (arg
),
2592 if (TREE_CODE (arg
) != TREE_CODE (parm
))
2595 if (TREE_CODE (parm
) == INTEGER_TYPE
)
2597 if (TYPE_MIN_VALUE (parm
) && TYPE_MIN_VALUE (arg
)
2598 && unify (tparms
, targs
, ntparms
,
2599 TYPE_MIN_VALUE (parm
), TYPE_MIN_VALUE (arg
), nsubsts
))
2601 if (TYPE_MAX_VALUE (parm
) && TYPE_MAX_VALUE (arg
)
2602 && unify (tparms
, targs
, ntparms
,
2603 TYPE_MAX_VALUE (parm
), TYPE_MAX_VALUE (arg
), nsubsts
))
2606 /* As far as unification is concerned, this wins. Later checks
2607 will invalidate it if necessary. */
2610 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
2612 if (TREE_CODE (arg
) != INTEGER_CST
)
2614 return !tree_int_cst_equal (parm
, arg
);
2619 t1
= TREE_OPERAND (parm
, 0);
2620 t2
= TREE_OPERAND (parm
, 1);
2621 return unify (tparms
, targs
, ntparms
, t1
,
2622 fold (build (PLUS_EXPR
, integer_type_node
, arg
, t2
)),
2629 if (TREE_CODE (arg
) != TREE_VEC
)
2631 if (TREE_VEC_LENGTH (parm
) != TREE_VEC_LENGTH (arg
))
2633 for (i
= TREE_VEC_LENGTH (parm
) - 1; i
>= 0; i
--)
2634 if (unify (tparms
, targs
, ntparms
,
2635 TREE_VEC_ELT (parm
, i
), TREE_VEC_ELT (arg
, i
),
2642 if (TYPE_PTRMEMFUNC_FLAG (parm
))
2643 return unify (tparms
, targs
, ntparms
, TYPE_PTRMEMFUNC_FN_TYPE (parm
),
2646 /* Allow trivial conversions. */
2647 if (TREE_CODE (arg
) != RECORD_TYPE
2648 || TYPE_READONLY (parm
) < TYPE_READONLY (arg
)
2649 || TYPE_VOLATILE (parm
) < TYPE_VOLATILE (arg
))
2652 if (CLASSTYPE_TEMPLATE_INFO (parm
) && CLASSTYPE_TEMPLATE_INFO (arg
)
2653 && uses_template_parms (parm
))
2655 if (CLASSTYPE_TI_TEMPLATE (parm
) != CLASSTYPE_TI_TEMPLATE (arg
))
2657 return unify (tparms
, targs
, ntparms
, CLASSTYPE_TI_ARGS (parm
),
2658 CLASSTYPE_TI_ARGS (arg
), nsubsts
);
2660 else if (TYPE_MAIN_VARIANT (parm
) != TYPE_MAIN_VARIANT (arg
))
2665 if (TREE_CODE (arg
) != METHOD_TYPE
)
2670 if (TREE_CODE (arg
) != FUNCTION_TYPE
)
2673 if (unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2674 TREE_TYPE (arg
), nsubsts
))
2676 return type_unification (tparms
, targs
, TYPE_ARG_TYPES (parm
),
2677 TYPE_ARG_TYPES (arg
), nsubsts
, 1);
2680 if (TREE_CODE (arg
) != OFFSET_TYPE
)
2682 if (unify (tparms
, targs
, ntparms
, TYPE_OFFSET_BASETYPE (parm
),
2683 TYPE_OFFSET_BASETYPE (arg
), nsubsts
))
2685 return unify (tparms
, targs
, ntparms
, TREE_TYPE (parm
),
2686 TREE_TYPE (arg
), nsubsts
);
2689 sorry ("use of `%s' in template type unification",
2690 tree_code_name
[(int) TREE_CODE (parm
)]);
2696 mark_decl_instantiated (result
, extern_p
)
2700 if (DECL_TEMPLATE_INSTANTIATION (result
))
2701 SET_DECL_EXPLICIT_INSTANTIATION (result
);
2702 TREE_PUBLIC (result
) = 1;
2706 DECL_INTERFACE_KNOWN (result
) = 1;
2707 DECL_NOT_REALLY_EXTERN (result
) = 1;
2711 /* called from the parser. */
2713 do_function_instantiation (declspecs
, declarator
, storage
)
2714 tree declspecs
, declarator
, storage
;
2716 tree decl
= grokdeclarator (declarator
, declspecs
, NORMAL
, 0,
2717 NULL_TREE
, NULL_TREE
);
2720 tree result
= NULL_TREE
;
2723 if (! DECL_LANG_SPECIFIC (decl
))
2725 cp_error ("explicit instantiation of non-template `%#D'", decl
);
2729 /* If we've already seen this template instance, use it. */
2730 if (name
= DECL_ASSEMBLER_NAME (decl
),
2731 fn
= IDENTIFIER_GLOBAL_VALUE (name
),
2732 fn
&& DECL_TEMPLATE_INSTANTIATION (fn
))
2734 else if (fn
&& DECL_CONTEXT (fn
))
2736 else if (name
= DECL_NAME (decl
), fn
= IDENTIFIER_GLOBAL_VALUE (name
), fn
)
2738 for (fn
= get_first_fn (fn
); fn
; fn
= DECL_CHAIN (fn
))
2739 if (decls_match (fn
, decl
)
2740 && DECL_DEFER_OUTPUT (fn
))
2745 else if (TREE_CODE (fn
) == TEMPLATE_DECL
)
2747 int ntparms
= TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (fn
));
2748 tree
*targs
= (tree
*) malloc (sizeof (tree
) * ntparms
);
2750 i
= type_unification (DECL_TEMPLATE_PARMS (fn
), targs
,
2751 TYPE_ARG_TYPES (TREE_TYPE (fn
)),
2752 TYPE_ARG_TYPES (TREE_TYPE (decl
)),
2757 cp_error ("ambiguous template instantiation for `%D' requested", decl
);
2759 result
= instantiate_template (fn
, targs
);
2766 cp_error ("no matching template for `%D' found", decl
);
2770 if (flag_external_templates
)
2773 if (storage
== NULL_TREE
)
2775 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2778 cp_error ("storage class `%D' applied to template instantiation",
2782 instantiate_decl (result
);
2783 mark_decl_instantiated (result
, extern_p
);
2784 repo_template_instantiated (result
, extern_p
);
2788 mark_class_instantiated (t
, extern_p
)
2792 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t
);
2793 SET_CLASSTYPE_INTERFACE_KNOWN (t
);
2794 CLASSTYPE_INTERFACE_ONLY (t
) = extern_p
;
2795 CLASSTYPE_VTABLE_NEEDS_WRITING (t
) = ! extern_p
;
2796 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t
)) = extern_p
;
2799 CLASSTYPE_DEBUG_REQUESTED (t
) = 1;
2800 rest_of_type_compilation (t
, 1);
2805 do_type_instantiation (name
, storage
)
2808 tree t
= TREE_TYPE (name
);
2815 /* With -fexternal-templates, explicit instantiations are treated the same
2816 as implicit ones. */
2817 if (flag_external_templates
)
2820 if (TYPE_SIZE (t
) == NULL_TREE
)
2822 cp_error ("explicit instantiation of `%#T' before definition of template",
2827 if (storage
== NULL_TREE
)
2829 else if (storage
== ridpointers
[(int) RID_INLINE
])
2831 else if (storage
== ridpointers
[(int) RID_EXTERN
])
2833 else if (storage
== ridpointers
[(int) RID_STATIC
])
2837 cp_error ("storage class `%D' applied to template instantiation",
2842 /* We've already instantiated this. */
2843 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t
) && ! CLASSTYPE_INTERFACE_ONLY (t
)
2847 if (! CLASSTYPE_TEMPLATE_SPECIALIZATION (t
))
2849 mark_class_instantiated (t
, extern_p
);
2850 repo_template_instantiated (t
, extern_p
);
2860 for (tmp
= TYPE_METHODS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2861 if (DECL_TEMPLATE_INSTANTIATION (tmp
))
2863 mark_decl_instantiated (tmp
, extern_p
);
2864 repo_template_instantiated (tmp
, extern_p
);
2866 instantiate_decl (tmp
);
2869 for (tmp
= TYPE_FIELDS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2870 if (TREE_CODE (tmp
) == VAR_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp
))
2872 mark_decl_instantiated (tmp
, extern_p
);
2873 repo_template_instantiated (tmp
, extern_p
);
2875 instantiate_decl (tmp
);
2878 for (tmp
= CLASSTYPE_TAGS (t
); tmp
; tmp
= TREE_CHAIN (tmp
))
2879 if (IS_AGGR_TYPE (TREE_VALUE (tmp
)))
2880 do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp
)), storage
);
2885 instantiate_decl (d
)
2888 tree ti
= DECL_TEMPLATE_INFO (d
);
2889 tree tmpl
= TI_TEMPLATE (ti
);
2890 tree args
= TI_ARGS (ti
);
2892 tree pattern
= DECL_TEMPLATE_RESULT (tmpl
);
2894 int nested
= in_function_p ();
2896 int pattern_defined
;
2898 if (TREE_CODE (d
) == FUNCTION_DECL
)
2900 d_defined
= (DECL_INITIAL (d
) != NULL_TREE
);
2901 pattern_defined
= (DECL_INITIAL (pattern
) != NULL_TREE
);
2905 d_defined
= ! DECL_IN_AGGR_P (d
);
2906 pattern_defined
= ! DECL_IN_AGGR_P (pattern
);
2911 else if (pattern_defined
)
2913 repo_template_used (d
);
2915 if (flag_external_templates
&& ! DECL_INTERFACE_KNOWN (d
))
2917 if (flag_alt_external_templates
)
2919 if (interface_unknown
)
2920 warn_if_unknown_interface (d
);
2922 else if (DECL_INTERFACE_KNOWN (pattern
))
2924 DECL_INTERFACE_KNOWN (d
) = 1;
2925 DECL_NOT_REALLY_EXTERN (d
) = ! DECL_EXTERNAL (pattern
);
2928 warn_if_unknown_interface (pattern
);
2931 if (at_eof
&& ! DECL_INLINE (d
))
2932 import_export_decl (d
);
2935 if (! pattern_defined
2936 || (TREE_CODE (d
) == FUNCTION_DECL
&& ! DECL_INLINE (d
)
2937 && (! DECL_INTERFACE_KNOWN (d
)
2938 || ! DECL_NOT_REALLY_EXTERN (d
))))
2940 add_pending_template (d
);
2944 if (! push_tinst_level (d
))
2947 if (TREE_CODE (d
) == FUNCTION_DECL
&& nested
)
2948 push_cp_function_context (NULL_TREE
);
2949 push_to_top_level ();
2951 /* Trick tsubst into giving us a new decl in case the template changed. */
2952 save_ti
= DECL_TEMPLATE_INFO (pattern
);
2953 DECL_TEMPLATE_INFO (pattern
) = NULL_TREE
;
2954 td
= tsubst (pattern
, &TREE_VEC_ELT (args
, 0), TREE_VEC_LENGTH (args
), tmpl
);
2955 DECL_TEMPLATE_INFO (pattern
) = save_ti
;
2957 /* Convince duplicate_decls to use the DECL_ARGUMENTS from the new decl. */
2958 if (TREE_CODE (d
) == FUNCTION_DECL
)
2959 DECL_INITIAL (td
) = error_mark_node
;
2960 duplicate_decls (td
, d
);
2961 if (TREE_CODE (d
) == FUNCTION_DECL
)
2962 DECL_INITIAL (td
) = 0;
2964 if (TREE_CODE (d
) == VAR_DECL
)
2966 DECL_IN_AGGR_P (d
) = 0;
2967 if (DECL_INTERFACE_KNOWN (d
))
2968 DECL_EXTERNAL (d
) = ! DECL_NOT_REALLY_EXTERN (d
);
2971 DECL_EXTERNAL (d
) = 1;
2972 DECL_NOT_REALLY_EXTERN (d
) = 1;
2974 cp_finish_decl (d
, DECL_INITIAL (d
), NULL_TREE
, 0, 0);
2976 else if (TREE_CODE (d
) == FUNCTION_DECL
)
2978 tree t
= DECL_SAVED_TREE (pattern
);
2980 char *file
= input_filename
;
2982 lineno
= DECL_SOURCE_LINE (d
);
2983 input_filename
= DECL_SOURCE_FILE (d
);
2985 start_function (NULL_TREE
, d
, NULL_TREE
, NULL_TREE
, 1);
2986 store_parm_decls ();
2988 if (t
&& TREE_CODE (t
) == RETURN_INIT
)
2991 (TREE_OPERAND (t
, 0),
2992 tsubst_expr (TREE_OPERAND (t
, 1), &TREE_VEC_ELT (args
, 0),
2993 TREE_VEC_LENGTH (args
), tmpl
));
2997 if (t
&& TREE_CODE (t
) == CTOR_INITIALIZER
)
2999 current_member_init_list
3000 = tsubst_expr_values (TREE_OPERAND (t
, 0), args
);
3001 current_base_init_list
3002 = tsubst_expr_values (TREE_OPERAND (t
, 1), args
);
3007 /* Always keep the BLOCK node associated with the outermost
3008 pair of curley braces of a function. These are needed
3009 for correct operation of dwarfout.c. */
3012 my_friendly_assert (TREE_CODE (t
) == COMPOUND_STMT
, 42);
3013 tsubst_expr (t
, &TREE_VEC_ELT (args
, 0),
3014 TREE_VEC_LENGTH (args
), tmpl
);
3016 finish_function (lineno
, 0, nested
);
3019 input_filename
= file
;
3022 pop_from_top_level ();
3023 if (TREE_CODE (d
) == FUNCTION_DECL
&& nested
)
3024 pop_cp_function_context (NULL_TREE
);
3031 tsubst_chain (t
, argvec
)
3036 tree first
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3037 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3040 for (t
= TREE_CHAIN (t
); t
; t
= TREE_CHAIN (t
))
3042 tree x
= tsubst (t
, &TREE_VEC_ELT (argvec
, 0),
3043 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3044 TREE_CHAIN (last
) = x
;
3054 tsubst_expr_values (t
, argvec
)
3057 tree first
= NULL_TREE
;
3060 for (; t
; t
= TREE_CHAIN (t
))
3062 tree pur
= tsubst_copy (TREE_PURPOSE (t
), &TREE_VEC_ELT (argvec
, 0),
3063 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3064 tree val
= tsubst_expr (TREE_VALUE (t
), &TREE_VEC_ELT (argvec
, 0),
3065 TREE_VEC_LENGTH (argvec
), NULL_TREE
);
3066 *p
= build_tree_list (pur
, val
);
3067 p
= &TREE_CHAIN (*p
);
3078 last_tree
= TREE_CHAIN (last_tree
) = t
;