Merge from mainline
[official-gcc.git] / gcc / cp / rtti.c
blob402ab3de171406927de6293ab24a0f47f2f56cf1
1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4 Mostly written by Jason Merrill (jason@cygnus.com).
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "output.h"
30 #include "assert.h"
31 #include "toplev.h"
33 /* Accessors for the type_info objects. We need to remember several things
34 about each of the type_info types. The global tree nodes such as
35 bltn_desc_type_node are TREE_LISTs, and these macros are used to access
36 the required information. */
37 /* The RECORD_TYPE of a type_info derived class. */
38 #define TINFO_PSEUDO_TYPE(NODE) TREE_TYPE (NODE)
39 /* The VAR_DECL of the vtable for the type_info derived class. */
40 #define TINFO_VTABLE_DECL(NODE) TREE_VALUE (NODE)
42 extern struct obstack permanent_obstack;
44 static tree build_headof PARAMS((tree));
45 static tree ifnonnull PARAMS((tree, tree));
46 static tree tinfo_name PARAMS((tree));
47 static tree build_dynamic_cast_1 PARAMS((tree, tree));
48 static tree throw_bad_cast PARAMS((void));
49 static tree throw_bad_typeid PARAMS((void));
50 static tree get_tinfo_decl_dynamic PARAMS((tree));
51 static bool typeid_ok_p PARAMS ((void));
52 static int qualifier_flags PARAMS((tree));
53 static int target_incomplete_p PARAMS((tree));
54 static tree tinfo_base_init PARAMS((tree, tree));
55 static tree generic_initializer PARAMS((tree, tree));
56 static tree ptr_initializer PARAMS((tree, tree, int *));
57 static tree ptm_initializer PARAMS((tree, tree, int *));
58 static tree dfs_class_hint_mark PARAMS ((tree, void *));
59 static tree dfs_class_hint_unmark PARAMS ((tree, void *));
60 static int class_hint_flags PARAMS((tree));
61 static tree class_initializer PARAMS((tree, tree, tree));
62 static tree synthesize_tinfo_var PARAMS((tree, tree));
63 static tree create_real_tinfo_var PARAMS((tree, tree, tree, tree, int));
64 static tree create_pseudo_type_info PARAMS((const char *, int, ...));
65 static tree get_vmi_pseudo_type_info PARAMS((int));
66 static void create_tinfo_types PARAMS((void));
67 static int typeinfo_in_lib_p PARAMS((tree));
69 static int doing_runtime = 0;
71 void
72 init_rtti_processing ()
74 push_namespace (std_identifier);
75 type_info_type_node = xref_tag
76 (ctk_class, get_identifier ("type_info"), 1);
77 pop_namespace ();
78 tinfo_decl_type =
79 build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
82 /* Given the expression EXP of type `class *', return the head of the
83 object pointed to by EXP with type cv void*, if the class has any
84 virtual functions (TYPE_POLYMORPHIC_P), else just return the
85 expression. */
87 static tree
88 build_headof (exp)
89 tree exp;
91 tree type = TREE_TYPE (exp);
92 tree offset;
93 tree index;
95 my_friendly_assert (TREE_CODE (type) == POINTER_TYPE, 20000112);
96 type = TREE_TYPE (type);
98 if (!TYPE_POLYMORPHIC_P (type))
99 return exp;
101 /* We use this a couple of times below, protect it. */
102 exp = save_expr (exp);
104 /* The offset-to-top field is at index -2 from the vptr. */
105 index = build_int_2 (-2, -1);
107 offset = build_vtbl_ref (build_indirect_ref (exp, NULL), index);
109 type = build_qualified_type (ptr_type_node,
110 CP_TYPE_QUALS (TREE_TYPE (exp)));
111 return build (PLUS_EXPR, type, exp,
112 cp_convert (ptrdiff_type_node, offset));
115 /* Get a bad_cast node for the program to throw...
117 See libstdc++/exception.cc for __throw_bad_cast */
119 static tree
120 throw_bad_cast ()
122 tree fn = get_identifier ("__cxa_bad_cast");
123 if (IDENTIFIER_GLOBAL_VALUE (fn))
124 fn = IDENTIFIER_GLOBAL_VALUE (fn);
125 else
126 fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
127 void_list_node));
129 return build_call (fn, NULL_TREE);
132 static tree
133 throw_bad_typeid ()
135 tree fn = get_identifier ("__cxa_bad_typeid");
136 if (IDENTIFIER_GLOBAL_VALUE (fn))
137 fn = IDENTIFIER_GLOBAL_VALUE (fn);
138 else
140 tree t = build_qualified_type (type_info_type_node, TYPE_QUAL_CONST);
141 t = build_function_type (build_reference_type (t), void_list_node);
142 fn = push_throw_library_fn (fn, t);
145 return build_call (fn, NULL_TREE);
148 /* Return an expression for the address of the typeinfo node
149 associated with EXP. If EXP is a reference to a polymorphic class,
150 return the dynamic type; otherwise return the static type of the
151 expression. */
153 static tree
154 get_tinfo_decl_dynamic (exp)
155 tree exp;
157 tree type;
159 if (exp == error_mark_node)
160 return error_mark_node;
162 type = TREE_TYPE (exp);
164 /* peel back references, so they match. */
165 if (TREE_CODE (type) == REFERENCE_TYPE)
166 type = TREE_TYPE (type);
168 /* Peel off cv qualifiers. */
169 type = TYPE_MAIN_VARIANT (type);
171 if (!VOID_TYPE_P (type))
172 type = complete_type_or_else (type, exp);
174 if (!type)
175 return error_mark_node;
177 /* If exp is a reference to polymorphic type, get the real type_info. */
178 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
180 /* build reference to type_info from vtable. */
181 tree t;
182 tree index;
184 /* The RTTI information is at index -1. */
185 index = integer_minus_one_node;
186 t = build_vfn_ref (exp, index);
187 TREE_TYPE (t) = build_pointer_type (tinfo_decl_type);
188 return t;
191 /* otherwise return the type_info for the static type of the expr. */
192 exp = get_tinfo_decl (TYPE_MAIN_VARIANT (type));
193 return build_unary_op (ADDR_EXPR, exp, 0);
196 static bool
197 typeid_ok_p ()
199 if (! flag_rtti)
201 error ("cannot use typeid with -fno-rtti");
202 return false;
205 if (!COMPLETE_TYPE_P (type_info_type_node))
207 error ("must #include <typeinfo> before using typeid");
208 return false;
211 return true;
214 tree
215 build_typeid (exp)
216 tree exp;
218 tree cond = NULL_TREE;
219 int nonnull = 0;
221 if (exp == error_mark_node || !typeid_ok_p ())
222 return error_mark_node;
224 if (processing_template_decl)
225 return build_min_nt (TYPEID_EXPR, exp);
227 if (TREE_CODE (exp) == INDIRECT_REF
228 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
229 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
230 && ! resolves_to_fixed_type_p (exp, &nonnull)
231 && ! nonnull)
233 exp = stabilize_reference (exp);
234 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
237 exp = get_tinfo_decl_dynamic (exp);
239 if (exp == error_mark_node)
240 return error_mark_node;
242 exp = build_indirect_ref (exp, NULL);
244 if (cond)
246 tree bad = throw_bad_typeid ();
248 exp = build (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
251 return convert_from_reference (exp);
254 /* Generate the NTBS name of a type. */
255 static tree
256 tinfo_name (type)
257 tree type;
259 const char *name;
260 tree name_string;
262 name = mangle_type_string (type);
263 name_string = combine_strings (build_string (strlen (name) + 1, name));
264 return name_string;
267 /* Returns a decl for the type_info variable for TYPE. You must
268 arrange that the decl is mark_used, if actually use it --- decls in
269 vtables are only used if the vtable is output. */
271 tree
272 get_tinfo_decl (type)
273 tree type;
275 tree name;
276 tree d;
278 if (COMPLETE_TYPE_P (type)
279 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
281 cp_error ("cannot create type information for type `%T' because its size is variable",
282 type);
283 return error_mark_node;
286 if (TREE_CODE (type) == OFFSET_TYPE)
287 type = TREE_TYPE (type);
288 if (TREE_CODE (type) == METHOD_TYPE)
289 type = build_function_type (TREE_TYPE (type),
290 TREE_CHAIN (TYPE_ARG_TYPES (type)));
292 name = mangle_typeinfo_for_type (type);
294 d = IDENTIFIER_GLOBAL_VALUE (name);
295 if (d)
296 /* OK */;
297 else
299 /* The tinfo decl is the type_info object itself. We make all
300 tinfo objects look as type_info, even though they will end up
301 being a subclass of that when emitted. This means that we'll
302 erroneously think we know the dynamic type -- be careful in the
303 runtime. */
304 d = build_lang_decl (VAR_DECL, name, tinfo_decl_type);
306 DECL_ARTIFICIAL (d) = 1;
307 DECL_ALIGN (d) = TYPE_ALIGN (ptr_type_node);
308 DECL_USER_ALIGN (d) = 0;
309 TREE_READONLY (d) = 1;
310 TREE_STATIC (d) = 1;
311 DECL_EXTERNAL (d) = 1;
312 TREE_PUBLIC (d) = 1;
313 if (flag_weak || !typeinfo_in_lib_p (d))
314 comdat_linkage (d);
315 SET_DECL_ASSEMBLER_NAME (d, name);
316 cp_finish_decl (d, NULL_TREE, NULL_TREE, 0);
318 pushdecl_top_level (d);
319 /* Remember the type it is for. */
320 TREE_TYPE (name) = type;
321 TREE_USED (name) = 1;
323 return d;
326 /* Return the type_info object for TYPE. */
328 tree
329 get_typeid (type)
330 tree type;
332 if (type == error_mark_node || !typeid_ok_p ())
333 return error_mark_node;
335 if (processing_template_decl)
336 return build_min_nt (TYPEID_EXPR, type);
338 /* If the type of the type-id is a reference type, the result of the
339 typeid expression refers to a type_info object representing the
340 referenced type. */
341 if (TREE_CODE (type) == REFERENCE_TYPE)
342 type = TREE_TYPE (type);
344 /* The top-level cv-qualifiers of the lvalue expression or the type-id
345 that is the operand of typeid are always ignored. */
346 type = TYPE_MAIN_VARIANT (type);
348 if (!VOID_TYPE_P (type))
349 type = complete_type_or_else (type, NULL_TREE);
351 if (!type)
352 return error_mark_node;
354 return get_tinfo_decl (type);
357 /* Check whether TEST is null before returning RESULT. If TEST is used in
358 RESULT, it must have previously had a save_expr applied to it. */
360 static tree
361 ifnonnull (test, result)
362 tree test, result;
364 return build (COND_EXPR, TREE_TYPE (result),
365 build (EQ_EXPR, boolean_type_node, test, integer_zero_node),
366 cp_convert (TREE_TYPE (result), integer_zero_node),
367 result);
370 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
371 paper. */
373 static tree
374 build_dynamic_cast_1 (type, expr)
375 tree type, expr;
377 enum tree_code tc = TREE_CODE (type);
378 tree exprtype = TREE_TYPE (expr);
379 tree dcast_fn;
380 tree old_expr = expr;
381 const char *errstr = NULL;
383 /* T shall be a pointer or reference to a complete class type, or
384 `pointer to cv void''. */
385 switch (tc)
387 case POINTER_TYPE:
388 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
389 break;
390 case REFERENCE_TYPE:
391 if (! IS_AGGR_TYPE (TREE_TYPE (type)))
393 errstr = "target is not pointer or reference to class";
394 goto fail;
396 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
398 errstr = "target is not pointer or reference to complete type";
399 goto fail;
401 break;
403 default:
404 errstr = "target is not pointer or reference";
405 goto fail;
408 if (TREE_CODE (expr) == OFFSET_REF)
410 expr = resolve_offset_ref (expr);
411 exprtype = TREE_TYPE (expr);
414 if (tc == POINTER_TYPE)
415 expr = convert_from_reference (expr);
416 else if (TREE_CODE (exprtype) != REFERENCE_TYPE)
418 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
419 exprtype = build_reference_type (exprtype);
420 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
421 LOOKUP_NORMAL, NULL_TREE);
424 exprtype = TREE_TYPE (expr);
426 if (tc == POINTER_TYPE)
428 /* If T is a pointer type, v shall be an rvalue of a pointer to
429 complete class type, and the result is an rvalue of type T. */
431 if (TREE_CODE (exprtype) != POINTER_TYPE)
433 errstr = "source is not a pointer";
434 goto fail;
436 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
438 errstr = "source is not a pointer to class";
439 goto fail;
441 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
443 errstr = "source is a pointer to incomplete type";
444 goto fail;
447 else
449 /* T is a reference type, v shall be an lvalue of a complete class
450 type, and the result is an lvalue of the type referred to by T. */
452 if (! IS_AGGR_TYPE (TREE_TYPE (exprtype)))
454 errstr = "source is not of class type";
455 goto fail;
457 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
459 errstr = "source is of incomplete class type";
460 goto fail;
465 /* The dynamic_cast operator shall not cast away constness. */
466 if (!at_least_as_qualified_p (TREE_TYPE (type),
467 TREE_TYPE (exprtype)))
469 errstr = "conversion casts away constness";
470 goto fail;
473 /* If *type is an unambiguous accessible base class of *exprtype,
474 convert statically. */
476 int distance;
477 tree path;
479 distance = get_base_distance (TREE_TYPE (type), TREE_TYPE (exprtype), 1,
480 &path);
482 if (distance == -2)
484 cp_error ("dynamic_cast from `%T' to ambiguous base class `%T'",
485 TREE_TYPE (exprtype), TREE_TYPE (type));
486 return error_mark_node;
488 if (distance == -3)
490 cp_error ("dynamic_cast from `%T' to private base class `%T'",
491 TREE_TYPE (exprtype), TREE_TYPE (type));
492 return error_mark_node;
495 if (distance >= 0)
497 expr = build_vbase_path (PLUS_EXPR, type, expr, path, 0);
498 if (TREE_CODE (exprtype) == POINTER_TYPE)
499 expr = non_lvalue (expr);
500 return expr;
504 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
505 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
507 tree expr1;
508 /* if TYPE is `void *', return pointer to complete object. */
509 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
511 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
512 if (TREE_CODE (expr) == ADDR_EXPR
513 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
514 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
515 return build1 (NOP_EXPR, type, expr);
517 /* Since expr is used twice below, save it. */
518 expr = save_expr (expr);
520 expr1 = build_headof (expr);
521 if (TREE_TYPE (expr1) != type)
522 expr1 = build1 (NOP_EXPR, type, expr1);
523 return ifnonnull (expr, expr1);
525 else
527 tree retval;
528 tree result, td2, td3, elems;
529 tree static_type, target_type, boff;
531 /* If we got here, we can't convert statically. Therefore,
532 dynamic_cast<D&>(b) (b an object) cannot succeed. */
533 if (tc == REFERENCE_TYPE)
535 if (TREE_CODE (old_expr) == VAR_DECL
536 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
538 tree expr = throw_bad_cast ();
539 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
540 old_expr, type);
541 /* Bash it to the expected type. */
542 TREE_TYPE (expr) = type;
543 return expr;
546 /* Ditto for dynamic_cast<D*>(&b). */
547 else if (TREE_CODE (expr) == ADDR_EXPR)
549 tree op = TREE_OPERAND (expr, 0);
550 if (TREE_CODE (op) == VAR_DECL
551 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
553 cp_warning ("dynamic_cast of `%#D' to `%#T' can never succeed",
554 op, type);
555 retval = build_int_2 (0, 0);
556 TREE_TYPE (retval) = type;
557 return retval;
561 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
562 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
563 td2 = build_unary_op (ADDR_EXPR, get_tinfo_decl (target_type), 0);
564 td3 = build_unary_op (ADDR_EXPR, get_tinfo_decl (static_type), 0);
566 /* Determine how T and V are related. */
567 boff = get_dynamic_cast_base_type (static_type, target_type);
569 /* Since expr is used twice below, save it. */
570 expr = save_expr (expr);
572 expr1 = expr;
573 if (tc == REFERENCE_TYPE)
574 expr1 = build_unary_op (ADDR_EXPR, expr1, 0);
576 elems = tree_cons
577 (NULL_TREE, expr1, tree_cons
578 (NULL_TREE, td3, tree_cons
579 (NULL_TREE, td2, tree_cons
580 (NULL_TREE, boff, NULL_TREE))));
582 dcast_fn = dynamic_cast_node;
583 if (!dcast_fn)
585 tree tmp;
586 tree tinfo_ptr;
587 tree ns = abi_node;
588 const char *name;
590 push_nested_namespace (ns);
591 tinfo_ptr = xref_tag (ctk_class,
592 get_identifier ("__class_type_info"),
595 tinfo_ptr = build_pointer_type
596 (build_qualified_type
597 (tinfo_ptr, TYPE_QUAL_CONST));
598 name = "__dynamic_cast";
599 tmp = tree_cons
600 (NULL_TREE, const_ptr_type_node, tree_cons
601 (NULL_TREE, tinfo_ptr, tree_cons
602 (NULL_TREE, tinfo_ptr, tree_cons
603 (NULL_TREE, ptrdiff_type_node, void_list_node))));
604 tmp = build_function_type (ptr_type_node, tmp);
605 dcast_fn = build_library_fn_ptr (name, tmp);
606 pop_nested_namespace (ns);
607 dynamic_cast_node = dcast_fn;
609 result = build_call (dcast_fn, elems);
611 if (tc == REFERENCE_TYPE)
613 tree bad = throw_bad_cast ();
615 result = save_expr (result);
616 return build (COND_EXPR, type, result, result, bad);
619 /* Now back to the type we want from a void*. */
620 result = cp_convert (type, result);
621 return ifnonnull (expr, result);
624 else
625 errstr = "source type is not polymorphic";
627 fail:
628 cp_error ("cannot dynamic_cast `%E' (of type `%#T') to type `%#T' (%s)",
629 expr, exprtype, type, errstr);
630 return error_mark_node;
633 tree
634 build_dynamic_cast (type, expr)
635 tree type, expr;
637 if (type == error_mark_node || expr == error_mark_node)
638 return error_mark_node;
640 if (processing_template_decl)
641 return build_min (DYNAMIC_CAST_EXPR, type, expr);
643 return convert_from_reference (build_dynamic_cast_1 (type, expr));
646 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
648 static int
649 qualifier_flags (type)
650 tree type;
652 int flags = 0;
653 /* we want the qualifiers on this type, not any array core, it might have */
654 int quals = TYPE_QUALS (type);
656 if (quals & TYPE_QUAL_CONST)
657 flags |= 1;
658 if (quals & TYPE_QUAL_VOLATILE)
659 flags |= 2;
660 if (quals & TYPE_QUAL_RESTRICT)
661 flags |= 4;
662 return flags;
665 /* Return non-zero, if the pointer chain TYPE ends at an incomplete type, or
666 contains a pointer to member of an incomplete class. */
668 static int
669 target_incomplete_p (type)
670 tree type;
672 while (TREE_CODE (type) == POINTER_TYPE)
673 if (TYPE_PTRMEM_P (type))
675 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
676 return 1;
677 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
679 else
680 type = TREE_TYPE (type);
681 if (!COMPLETE_OR_VOID_TYPE_P (type))
682 return 1;
684 return 0;
687 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
688 is the vtable pointer and NTBS name. The NTBS name is emitted as a
689 comdat const char array, so it becomes a unique key for the type. Generate
690 and emit that VAR_DECL here. (We can't always emit the type_info itself
691 as comdat, because of pointers to incomplete.) */
693 static tree
694 tinfo_base_init (desc, target)
695 tree desc;
696 tree target;
698 tree init = NULL_TREE;
699 tree name_decl;
702 tree name_name;
704 /* Generate the NTBS array variable. */
705 tree name_type = build_cplus_array_type
706 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
707 NULL_TREE);
708 tree name_string = tinfo_name (target);
710 name_name = mangle_typeinfo_string_for_type (target);
711 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
713 DECL_ARTIFICIAL (name_decl) = 1;
714 TREE_READONLY (name_decl) = 1;
715 TREE_STATIC (name_decl) = 1;
716 DECL_EXTERNAL (name_decl) = 0;
717 TREE_PUBLIC (name_decl) = 1;
718 comdat_linkage (name_decl);
719 /* External name of the string containing the type's name has a
720 special name. */
721 SET_DECL_ASSEMBLER_NAME (name_decl,
722 mangle_typeinfo_string_for_type (target));
723 DECL_INITIAL (name_decl) = name_string;
724 cp_finish_decl (name_decl, name_string, NULL_TREE, 0);
725 pushdecl_top_level (name_decl);
728 if (TINFO_VTABLE_DECL (desc))
730 tree vtbl_ptr = TINFO_VTABLE_DECL (desc);
731 init = tree_cons (NULL_TREE, vtbl_ptr, init);
734 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
736 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
737 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
738 init = tree_cons (NULL_TREE, init, NULL_TREE);
740 return init;
743 /* Return the CONSTRUCTOR expr for a type_info of TYPE. DESC provides the
744 information about the particular type_info derivation, which adds no
745 additional fields to the type_info base. */
747 static tree
748 generic_initializer (desc, target)
749 tree desc;
750 tree target;
752 tree init = tinfo_base_init (desc, target);
754 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
755 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
756 return init;
759 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
760 DESC provides information about the particular type_info derivation,
761 which adds target type and qualifier flags members to the type_info base. */
763 static tree
764 ptr_initializer (desc, target, non_public_ptr)
765 tree desc;
766 tree target;
767 int *non_public_ptr;
769 tree init = tinfo_base_init (desc, target);
770 tree to = TREE_TYPE (target);
771 int flags = qualifier_flags (to);
772 int incomplete = target_incomplete_p (to);
774 if (incomplete)
776 flags |= 8;
777 *non_public_ptr = 1;
779 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
780 init = tree_cons (NULL_TREE,
781 build_unary_op (ADDR_EXPR,
782 get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
783 init);
785 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
786 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
787 return init;
790 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
791 DESC provides information about the particular type_info derivation,
792 which adds class, target type and qualifier flags members to the type_info
793 base. */
795 static tree
796 ptm_initializer (desc, target, non_public_ptr)
797 tree desc;
798 tree target;
799 int *non_public_ptr;
801 tree init = tinfo_base_init (desc, target);
802 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
803 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
804 int flags = qualifier_flags (to);
805 int incomplete = target_incomplete_p (to);
807 if (incomplete)
809 flags |= 0x8;
810 *non_public_ptr = 1;
812 if (!COMPLETE_TYPE_P (klass))
814 flags |= 0x10;
815 *non_public_ptr = 1;
817 init = tree_cons (NULL_TREE, build_int_2 (flags, 0), init);
818 init = tree_cons (NULL_TREE,
819 build_unary_op (ADDR_EXPR,
820 get_tinfo_decl (TYPE_MAIN_VARIANT (to)), 0),
821 init);
822 init = tree_cons (NULL_TREE,
823 build_unary_op (ADDR_EXPR, get_tinfo_decl (klass), 0),
824 init);
826 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, nreverse (init));
827 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
828 return init;
831 /* Check base BINFO to set hint flags in *DATA, which is really an int.
832 We use CLASSTYPE_MARKED to tag types we've found as non-virtual bases and
833 CLASSTYPE_MARKED2 to tag those which are virtual bases. Remember it is
834 possible for a type to be both a virtual and non-virtual base. */
836 static tree
837 dfs_class_hint_mark (binfo, data)
838 tree binfo;
839 void *data;
841 tree basetype = BINFO_TYPE (binfo);
842 int *hint = (int *) data;
844 if (TREE_VIA_VIRTUAL (binfo))
846 if (CLASSTYPE_MARKED (basetype))
847 *hint |= 1;
848 if (CLASSTYPE_MARKED2 (basetype))
849 *hint |= 2;
850 SET_CLASSTYPE_MARKED2 (basetype);
852 else
854 if (CLASSTYPE_MARKED (basetype) || CLASSTYPE_MARKED2 (basetype))
855 *hint |= 1;
856 SET_CLASSTYPE_MARKED (basetype);
858 if (!TREE_VIA_PUBLIC (binfo) && TYPE_BINFO (basetype) != binfo)
859 *hint |= 4;
860 return NULL_TREE;
863 /* Clear the base's dfs marks, after searching for duplicate bases. */
865 static tree
866 dfs_class_hint_unmark (binfo, data)
867 tree binfo;
868 void *data ATTRIBUTE_UNUSED;
870 tree basetype = BINFO_TYPE (binfo);
872 CLEAR_CLASSTYPE_MARKED (basetype);
873 CLEAR_CLASSTYPE_MARKED2 (basetype);
874 return NULL_TREE;
877 /* Determine the hint flags describing the features of a class's heirarchy. */
879 static int
880 class_hint_flags (type)
881 tree type;
883 int hint_flags = 0;
884 int i;
886 dfs_walk (TYPE_BINFO (type), dfs_class_hint_mark, NULL, &hint_flags);
887 dfs_walk (TYPE_BINFO (type), dfs_class_hint_unmark, NULL, NULL);
889 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
891 tree base_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
893 if (TREE_VIA_PUBLIC (base_binfo))
894 hint_flags |= 0x8;
896 return hint_flags;
899 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
900 DESC provides information about the particular __class_type_info derivation,
901 which adds hint flags and TRAIL initializers to the type_info base. */
903 static tree
904 class_initializer (desc, target, trail)
905 tree desc;
906 tree target;
907 tree trail;
909 tree init = tinfo_base_init (desc, target);
911 TREE_CHAIN (init) = trail;
912 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, init);
913 TREE_HAS_CONSTRUCTOR (init) = TREE_CONSTANT (init) = TREE_STATIC (init) = 1;
914 return init;
917 /* Returns non-zero if the typeinfo for type should be placed in
918 the runtime library. */
920 static int
921 typeinfo_in_lib_p (type)
922 tree type;
924 /* The typeinfo objects for `T*' and `const T*' are in the runtime
925 library for simple types T. */
926 if (TREE_CODE (type) == POINTER_TYPE
927 && (CP_TYPE_QUALS (TREE_TYPE (type)) == TYPE_QUAL_CONST
928 || CP_TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
929 type = TREE_TYPE (type);
931 switch (TREE_CODE (type))
933 case INTEGER_TYPE:
934 case BOOLEAN_TYPE:
935 case CHAR_TYPE:
936 case REAL_TYPE:
937 case VOID_TYPE:
938 return 1;
940 default:
941 return 0;
945 /* Generate a pseudo_type_info VAR_DECL suitable for the supplied
946 TARGET_TYPE and given the REAL_NAME. This is the structure expected by
947 the runtime, and therefore has additional fields. If we need not emit a
948 definition (because the runtime must contain it), return NULL_TREE,
949 otherwise return the VAR_DECL. */
951 static tree
952 synthesize_tinfo_var (target_type, real_name)
953 tree target_type;
954 tree real_name;
956 tree var_init = NULL_TREE;
957 tree var_type = NULL_TREE;
958 int non_public = 0;
960 switch (TREE_CODE (target_type))
962 case POINTER_TYPE:
963 if (TYPE_PTRMEM_P (target_type))
965 var_type = ptm_desc_type_node;
966 var_init = ptm_initializer (var_type, target_type, &non_public);
968 else
970 if (typeinfo_in_lib_p (target_type) && !doing_runtime)
971 /* These are in the runtime. */
972 return NULL_TREE;
973 var_type = ptr_desc_type_node;
974 var_init = ptr_initializer (var_type, target_type, &non_public);
976 break;
977 case ENUMERAL_TYPE:
978 var_type = enum_desc_type_node;
979 var_init = generic_initializer (var_type, target_type);
980 break;
981 case FUNCTION_TYPE:
982 var_type = func_desc_type_node;
983 var_init = generic_initializer (var_type, target_type);
984 break;
985 case ARRAY_TYPE:
986 var_type = ary_desc_type_node;
987 var_init = generic_initializer (var_type, target_type);
988 break;
989 case UNION_TYPE:
990 case RECORD_TYPE:
991 if (TYPE_PTRMEMFUNC_P (target_type))
993 var_type = ptm_desc_type_node;
994 var_init = ptm_initializer (var_type, target_type, &non_public);
996 else if (!COMPLETE_TYPE_P (target_type))
998 /* Emit a non-public class_type_info. */
999 non_public = 1;
1000 var_type = class_desc_type_node;
1001 var_init = class_initializer (var_type, target_type, NULL_TREE);
1003 else if (!CLASSTYPE_N_BASECLASSES (target_type))
1005 var_type = class_desc_type_node;
1006 var_init = class_initializer (var_type, target_type, NULL_TREE);
1008 else
1010 /* if this has a single public non-virtual base, it's easier */
1011 tree binfo = TYPE_BINFO (target_type);
1012 int nbases = BINFO_N_BASETYPES (binfo);
1013 tree base_binfos = BINFO_BASETYPES (binfo);
1014 tree base_inits = NULL_TREE;
1015 int is_simple = nbases == 1;
1016 int ix;
1018 /* Generate the base information initializer. */
1019 for (ix = nbases; ix--;)
1021 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
1022 tree base_init = NULL_TREE;
1023 int flags = 0;
1024 tree tinfo;
1025 tree offset;
1027 if (TREE_PUBLIC (base_binfo))
1028 flags |= 2;
1029 tinfo = get_tinfo_decl (BINFO_TYPE (base_binfo));
1030 tinfo = build_unary_op (ADDR_EXPR, tinfo, 0);
1031 if (TREE_VIA_VIRTUAL (base_binfo))
1033 /* We store the vtable offset at which the virtual
1034 base offset can be found. */
1035 offset = BINFO_VPTR_FIELD (binfo_for_vbase (BINFO_TYPE (base_binfo),
1036 target_type));
1037 offset = convert (sizetype, offset);
1038 flags |= 1;
1040 else
1041 offset = BINFO_OFFSET (base_binfo);
1043 /* is it a single public inheritance? */
1044 if (is_simple && flags == 2 && integer_zerop (offset))
1046 base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1047 break;
1049 is_simple = 0;
1051 /* combine offset and flags into one field */
1052 offset = cp_build_binary_op (LSHIFT_EXPR, offset,
1053 build_int_2 (8, 0));
1054 offset = cp_build_binary_op (BIT_IOR_EXPR, offset,
1055 build_int_2 (flags, 0));
1056 base_init = tree_cons (NULL_TREE, offset, base_init);
1057 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1058 base_init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_init);
1059 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1062 if (is_simple)
1063 var_type = si_class_desc_type_node;
1064 else
1066 int hint = class_hint_flags (target_type);
1068 base_inits = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, base_inits);
1069 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1070 /* Prepend the number of bases. */
1071 base_inits = tree_cons (NULL_TREE,
1072 build_int_2 (nbases, 0), base_inits);
1073 /* Prepend the hint flags. */
1074 base_inits = tree_cons (NULL_TREE,
1075 build_int_2 (hint, 0), base_inits);
1076 var_type = get_vmi_pseudo_type_info (nbases);
1078 var_init = class_initializer (var_type, target_type, base_inits);
1080 break;
1082 default:
1083 if (typeinfo_in_lib_p (target_type))
1085 if (!doing_runtime)
1086 /* These are guaranteed to be in the runtime. */
1087 return NULL_TREE;
1088 var_type = bltn_desc_type_node;
1089 var_init = generic_initializer (var_type, target_type);
1090 break;
1092 my_friendly_abort (20000117);
1095 return create_real_tinfo_var (target_type,
1096 real_name, TINFO_PSEUDO_TYPE (var_type),
1097 var_init, non_public);
1100 /* Create the real typeinfo variable. NON_PUBLIC indicates that we cannot
1101 make this variable public (comdat). */
1103 static tree
1104 create_real_tinfo_var (target_type, name, type, init, non_public)
1105 tree target_type;
1106 tree name;
1107 tree type;
1108 tree init;
1109 int non_public;
1111 static int count = 0;
1112 tree decl;
1113 tree hidden_name;
1114 char hidden[30];
1116 /* We cannot give this the name NAME, as that already is globally
1117 bound to the tinfo_decl we originally created for this type in
1118 get_tinfo_decl. */
1119 sprintf (hidden, "__ti_%d", count++);
1120 hidden_name = get_identifier (hidden);
1122 decl = build_lang_decl (VAR_DECL, hidden_name,
1123 build_qualified_type (type, TYPE_QUAL_CONST));
1124 DECL_ARTIFICIAL (decl) = 1;
1125 TREE_READONLY (decl) = 1;
1126 TREE_STATIC (decl) = 1;
1127 DECL_EXTERNAL (decl) = 0;
1129 if (!non_public)
1131 TREE_PUBLIC (decl) = 1;
1132 if (flag_weak || !typeinfo_in_lib_p (target_type))
1133 comdat_linkage (decl);
1135 SET_DECL_ASSEMBLER_NAME (decl, name);
1136 DECL_INITIAL (decl) = init;
1137 cp_finish_decl (decl, init, NULL_TREE, 0);
1138 pushdecl_top_level (decl);
1139 TREE_USED (decl) = 1;
1140 return decl;
1143 /* Generate the RECORD_TYPE containing the data layout of a type_info
1144 derivative as used by the runtime. This layout must be consistent with
1145 that defined in the runtime support. Also generate the VAR_DECL for the
1146 type's vtable. We explicitly manage the vtable member, and name it for
1147 real type as used in the runtime. The RECORD type has a different name,
1148 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1149 is the generated type and TINFO_VTABLE_DECL is the vtable decl.
1151 REAL_NAME is the runtime's name of the type. Trailing arguments are
1152 additional FIELD_DECL's for the structure. The final argument must be
1153 NULL. */
1155 static tree
1156 create_pseudo_type_info VPARAMS((const char *real_name, int ident, ...))
1158 #ifndef ANSI_PROTOTYPES
1159 char const *real_name;
1160 int ident;
1161 #endif
1162 va_list ap;
1163 tree real_type, pseudo_type;
1164 char *pseudo_name;
1165 tree vtable_decl;
1166 int ix;
1167 tree fields[10];
1168 tree field_decl;
1169 tree result;
1171 VA_START (ap, ident);
1172 #ifndef ANSI_PROTOTYPES
1173 real_name = va_arg (ap, char const *);
1174 ident = va_arg (app, int);
1175 #endif
1177 /* Generate the pseudo type name. */
1178 pseudo_name = (char *)alloca (strlen (real_name) + 30);
1179 strcpy (pseudo_name, real_name);
1180 strcat (pseudo_name, "_pseudo");
1181 if (ident)
1182 sprintf (pseudo_name + strlen (pseudo_name), "%d", ident);
1184 /* Get the vtable decl. */
1185 real_type = xref_tag (ctk_class, get_identifier (real_name), 1);
1186 vtable_decl = get_vtable_decl (real_type, /*complete=*/1);
1187 vtable_decl = build_unary_op (ADDR_EXPR, vtable_decl, 0);
1189 /* We need to point into the middle of the vtable. */
1190 vtable_decl = build (PLUS_EXPR,
1191 TREE_TYPE (vtable_decl),
1192 vtable_decl,
1193 size_binop (MULT_EXPR,
1194 size_int (2),
1195 TYPE_SIZE_UNIT (vtable_entry_type)));
1196 TREE_CONSTANT (vtable_decl) = 1;
1198 /* First field is the pseudo type_info base class. */
1199 fields[0] = build_decl (FIELD_DECL, NULL_TREE, ti_desc_type_node);
1201 /* Now add the derived fields. */
1202 for (ix = 0; (field_decl = va_arg (ap, tree));)
1203 fields[++ix] = field_decl;
1205 /* Create the pseudo type. */
1206 pseudo_type = make_aggr_type (RECORD_TYPE);
1207 finish_builtin_type (pseudo_type, pseudo_name, fields, ix, ptr_type_node);
1208 TYPE_HAS_CONSTRUCTOR (pseudo_type) = 1;
1209 va_end (ap);
1211 result = tree_cons (NULL_TREE, NULL_TREE, NULL_TREE);
1212 TINFO_VTABLE_DECL (result) = vtable_decl;
1213 TINFO_PSEUDO_TYPE (result) = pseudo_type;
1215 return result;
1218 /* Return a descriptor for a vmi type with NUM_BASES bases. */
1220 static tree
1221 get_vmi_pseudo_type_info (num_bases)
1222 int num_bases;
1224 tree desc;
1225 tree array_domain, base_array;
1227 if (TREE_VEC_LENGTH (vmi_class_desc_type_node) <= num_bases)
1229 int ix;
1230 tree extend = make_tree_vec (num_bases + 5);
1232 for (ix = TREE_VEC_LENGTH (vmi_class_desc_type_node); ix--;)
1233 TREE_VEC_ELT (extend, ix) = TREE_VEC_ELT (vmi_class_desc_type_node, ix);
1234 vmi_class_desc_type_node = extend;
1236 desc = TREE_VEC_ELT (vmi_class_desc_type_node, num_bases);
1238 if (desc)
1239 return desc;
1241 /* Add number of bases and trailing array of base_class_type_info. */
1242 array_domain = build_index_type (size_int (num_bases));
1243 base_array = build_array_type (base_desc_type_node, array_domain);
1245 push_nested_namespace (abi_node);
1247 desc = create_pseudo_type_info
1248 ("__vmi_class_type_info", num_bases,
1249 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1250 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1251 build_decl (FIELD_DECL, NULL_TREE, base_array),
1252 NULL);
1254 pop_nested_namespace (abi_node);
1256 TREE_VEC_ELT (vmi_class_desc_type_node, num_bases) = desc;
1257 return desc;
1260 /* Make sure the required builtin types exist for generating the type_info
1261 varable definitions. */
1263 static void
1264 create_tinfo_types ()
1266 tree ptr_type_info;
1268 if (bltn_desc_type_node)
1269 return;
1270 push_nested_namespace (abi_node);
1272 ptr_type_info = build_pointer_type
1273 (build_qualified_type
1274 (type_info_type_node, TYPE_QUAL_CONST));
1276 /* Create the internal type_info structure. This is used as a base for
1277 the other structures. */
1279 tree fields[2];
1281 ti_desc_type_node = make_aggr_type (RECORD_TYPE);
1282 fields[0] = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1283 fields[1] = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1284 finish_builtin_type (ti_desc_type_node, "__type_info_pseudo",
1285 fields, 1, ptr_type_node);
1286 TYPE_HAS_CONSTRUCTOR (ti_desc_type_node) = 1;
1289 /* Fundamental type_info */
1290 bltn_desc_type_node = create_pseudo_type_info
1291 ("__fundamental_type_info", 0,
1292 NULL);
1294 /* Array, function and enum type_info. No additional fields. */
1295 ary_desc_type_node = create_pseudo_type_info
1296 ("__array_type_info", 0,
1297 NULL);
1298 func_desc_type_node = create_pseudo_type_info
1299 ("__function_type_info", 0,
1300 NULL);
1301 enum_desc_type_node = create_pseudo_type_info
1302 ("__enum_type_info", 0,
1303 NULL);
1305 /* Class type_info. Add a flags field. */
1306 class_desc_type_node = create_pseudo_type_info
1307 ("__class_type_info", 0,
1308 NULL);
1310 /* Single public non-virtual base class. Add pointer to base class.
1311 This is really a descendant of __class_type_info. */
1312 si_class_desc_type_node = create_pseudo_type_info
1313 ("__si_class_type_info", 0,
1314 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1315 NULL);
1317 /* Base class internal helper. Pointer to base type, offset to base,
1318 flags. */
1320 tree fields[2];
1322 fields[0] = build_decl (FIELD_DECL, NULL_TREE, ptr_type_info);
1323 fields[1] = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1324 base_desc_type_node = make_aggr_type (RECORD_TYPE);
1325 finish_builtin_type (base_desc_type_node, "__base_class_type_info_pseudo",
1326 fields, 1, ptr_type_node);
1327 TYPE_HAS_CONSTRUCTOR (base_desc_type_node) = 1;
1330 /* General heirarchy is created as necessary in this vector. */
1331 vmi_class_desc_type_node = make_tree_vec (10);
1333 /* Pointer type_info. Adds two fields, qualification mask
1334 and pointer to the pointed to type. This is really a descendant of
1335 __pbase_type_info. */
1336 ptr_desc_type_node = create_pseudo_type_info
1337 ("__pointer_type_info", 0,
1338 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1339 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1340 NULL);
1342 /* Pointer to member data type_info. Add qualifications flags,
1343 pointer to the member's type info and pointer to the class.
1344 This is really a descendant of __pbase_type_info. */
1345 ptm_desc_type_node = create_pseudo_type_info
1346 ("__pointer_to_member_type_info", 0,
1347 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1348 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1349 build_decl (FIELD_DECL, NULL_TREE, ptr_type_info),
1350 NULL);
1352 pop_nested_namespace (abi_node);
1355 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1356 support. Generating them here guarantees consistency with the other
1357 structures. We use the following heuristic to determine when the runtime
1358 is being generated. If std::__fundamental_type_info is defined, and its
1359 destructor is defined, then the runtime is being built. */
1361 void
1362 emit_support_tinfos ()
1364 static tree *const fundamentals[] =
1366 &void_type_node,
1367 &boolean_type_node,
1368 &wchar_type_node,
1369 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1370 &short_integer_type_node, &short_unsigned_type_node,
1371 &integer_type_node, &unsigned_type_node,
1372 &long_integer_type_node, &long_unsigned_type_node,
1373 &long_long_integer_type_node, &long_long_unsigned_type_node,
1374 &float_type_node, &double_type_node, &long_double_type_node,
1377 int ix;
1378 tree bltn_type, dtor;
1380 push_nested_namespace (abi_node);
1381 bltn_type = xref_tag (ctk_class,
1382 get_identifier ("__fundamental_type_info"), 1);
1383 pop_nested_namespace (abi_node);
1384 if (!COMPLETE_TYPE_P (bltn_type))
1385 return;
1386 dtor = CLASSTYPE_DESTRUCTOR (bltn_type);
1387 if (DECL_EXTERNAL (dtor))
1388 return;
1389 doing_runtime = 1;
1390 for (ix = 0; fundamentals[ix]; ix++)
1392 tree bltn = *fundamentals[ix];
1393 tree bltn_ptr = build_pointer_type (bltn);
1394 tree bltn_const_ptr = build_pointer_type
1395 (build_qualified_type (bltn, TYPE_QUAL_CONST));
1396 tree tinfo;
1398 tinfo = get_tinfo_decl (bltn);
1399 TREE_USED (tinfo) = 1;
1400 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1402 tinfo = get_tinfo_decl (bltn_ptr);
1403 TREE_USED (tinfo) = 1;
1404 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1406 tinfo = get_tinfo_decl (bltn_const_ptr);
1407 TREE_USED (tinfo) = 1;
1408 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (tinfo)) = 1;
1412 /* Return non-zero, iff T is a type_info variable which has not had a
1413 definition emitted for it. */
1416 tinfo_decl_p (t, data)
1417 tree t;
1418 void *data ATTRIBUTE_UNUSED;
1420 return TREE_CODE (t) == VAR_DECL
1421 && IDENTIFIER_GLOBAL_VALUE (DECL_NAME (t)) == (t)
1422 && TREE_TYPE (t) == tinfo_decl_type
1423 && TREE_TYPE (DECL_NAME (t));
1426 /* Emit a suitable type_info definition for the type_info decl pointed to by
1427 DECL_PTR. We emit a completely new variable, of the correct type for the
1428 actual type this is describing. The DECL_ASSEMBLER_NAME of the generated
1429 definition is set to that of the supplied decl, so that they can be tied
1430 up. Mark the supplied decl as having been dealt with. Emitting one
1431 definition might cause other definitions to be required.
1433 We need to do things this way, because we're trying to do something like
1435 struct B : A {
1439 extern const A tinfo_var;
1441 const B tinfo_var = {...};
1443 which is not permitted. Also, we've not necessarily seen the definition of B.
1444 So we do something like the following,
1446 extern const A tinfo_var;
1448 struct pseudo_A {
1449 const void *vtable_ptr;
1450 const char *name;
1452 struct pseudo_B {
1453 pseudo_A base;
1457 const pseudo_B proxy_tinfo_var attribute((assembler_name="tinfo_var")) =
1459 {&B::vtable, "..."},
1463 pseudo_A and pseudo_B must be layout equivalent to the real definitions in
1464 the runtime. */
1467 emit_tinfo_decl (decl_ptr, data)
1468 tree *decl_ptr;
1469 void *data ATTRIBUTE_UNUSED;
1471 tree tinfo_decl = *decl_ptr;
1472 tree tinfo_type, decl;
1474 my_friendly_assert (TREE_TYPE (tinfo_decl) == tinfo_decl_type, 20000121);
1475 tinfo_type = TREE_TYPE (DECL_NAME (tinfo_decl));
1476 my_friendly_assert (tinfo_type != NULL_TREE, 20000120);
1478 if (!DECL_NEEDED_P (tinfo_decl))
1479 return 0;
1480 /* Say we've dealt with it. */
1481 TREE_TYPE (DECL_NAME (tinfo_decl)) = NULL_TREE;
1483 create_tinfo_types ();
1484 decl = synthesize_tinfo_var (tinfo_type, DECL_ASSEMBLER_NAME (tinfo_decl));
1486 return decl != 0;