2009-04-21 Taras Glek <tglek@mozilla.com>
[official-gcc.git] / gcc / cp / rtti.c
blob9246fc2854d18821a37ffcb4156e57114df5a96a
1 /* RunTime Type Identification
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Mostly written by Jason Merrill (jason@cygnus.com).
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "assert.h"
32 #include "toplev.h"
33 #include "convert.h"
34 #include "target.h"
35 #include "c-pragma.h"
37 /* C++ returns type information to the user in struct type_info
38 objects. We also use type information to implement dynamic_cast and
39 exception handlers. Type information for a particular type is
40 indicated with an ABI defined structure derived from type_info.
41 This would all be very straight forward, but for the fact that the
42 runtime library provides the definitions of the type_info structure
43 and the ABI defined derived classes. We cannot build declarations
44 of them directly in the compiler, but we need to layout objects of
45 their type. Somewhere we have to lie.
47 We define layout compatible POD-structs with compiler-defined names
48 and generate the appropriate initializations for them (complete
49 with explicit mention of their vtable). When we have to provide a
50 type_info to the user we reinterpret_cast the internal compiler
51 type to type_info. A well formed program can only explicitly refer
52 to the type_infos of complete types (& cv void). However, we chain
53 pointer type_infos to the pointed-to-type, and that can be
54 incomplete. We only need the addresses of such incomplete
55 type_info objects for static initialization.
57 The type information VAR_DECL of a type is held on the
58 IDENTIFIER_GLOBAL_VALUE of the type's mangled name. That VAR_DECL
59 will be the internal type. It will usually have the correct
60 internal type reflecting the kind of type it represents (pointer,
61 array, function, class, inherited class, etc). When the type it
62 represents is incomplete, it will have the internal type
63 corresponding to type_info. That will only happen at the end of
64 translation, when we are emitting the type info objects. */
66 /* Auxiliary data we hold for each type_info derived object we need. */
67 typedef struct GTY (()) tinfo_s {
68 tree type; /* The RECORD_TYPE for this type_info object */
70 tree vtable; /* The VAR_DECL of the vtable. Only filled at end of
71 translation. */
73 tree name; /* IDENTIFIER_NODE for the ABI specified name of
74 the type_info derived type. */
75 } tinfo_s;
77 DEF_VEC_O(tinfo_s);
78 DEF_VEC_ALLOC_O(tinfo_s,gc);
80 typedef enum tinfo_kind
82 TK_TYPE_INFO_TYPE, /* abi::__type_info_pseudo */
83 TK_BASE_TYPE, /* abi::__base_class_type_info */
84 TK_BUILTIN_TYPE, /* abi::__fundamental_type_info */
85 TK_ARRAY_TYPE, /* abi::__array_type_info */
86 TK_FUNCTION_TYPE, /* abi::__function_type_info */
87 TK_ENUMERAL_TYPE, /* abi::__enum_type_info */
88 TK_POINTER_TYPE, /* abi::__pointer_type_info */
89 TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */
90 TK_CLASS_TYPE, /* abi::__class_type_info */
91 TK_SI_CLASS_TYPE, /* abi::__si_class_type_info */
92 TK_FIXED /* end of fixed descriptors. */
93 /* ... abi::__vmi_type_info<I> */
94 } tinfo_kind;
96 /* A vector of all tinfo decls that haven't yet been emitted. */
97 VEC(tree,gc) *unemitted_tinfo_decls;
99 /* A vector of all type_info derived types we need. The first few are
100 fixed and created early. The remainder are for multiple inheritance
101 and are generated as needed. */
102 static GTY (()) VEC(tinfo_s,gc) *tinfo_descs;
104 static tree ifnonnull (tree, tree);
105 static tree tinfo_name (tree);
106 static tree build_dynamic_cast_1 (tree, tree, tsubst_flags_t);
107 static tree throw_bad_cast (void);
108 static tree throw_bad_typeid (void);
109 static tree get_tinfo_decl_dynamic (tree);
110 static tree get_tinfo_ptr (tree);
111 static bool typeid_ok_p (void);
112 static int qualifier_flags (tree);
113 static bool target_incomplete_p (tree);
114 static tree tinfo_base_init (tinfo_s *, tree);
115 static tree generic_initializer (tinfo_s *, tree);
116 static tree ptr_initializer (tinfo_s *, tree);
117 static tree ptm_initializer (tinfo_s *, tree);
118 static tree class_initializer (tinfo_s *, tree, tree);
119 static void create_pseudo_type_info (int, const char *, ...);
120 static tree get_pseudo_ti_init (tree, unsigned);
121 static unsigned get_pseudo_ti_index (tree);
122 static void create_tinfo_types (void);
123 static bool typeinfo_in_lib_p (tree);
125 static int doing_runtime = 0;
127 static void
128 push_abi_namespace (void)
130 push_nested_namespace (abi_node);
131 push_visibility ("default");
134 static void
135 pop_abi_namespace (void)
137 pop_visibility ();
138 pop_nested_namespace (abi_node);
141 /* Declare language defined type_info type and a pointer to const
142 type_info. This is incomplete here, and will be completed when
143 the user #includes <typeinfo>. There are language defined
144 restrictions on what can be done until that is included. Create
145 the internal versions of the ABI types. */
147 void
148 init_rtti_processing (void)
150 tree type_info_type;
152 push_namespace (std_identifier);
153 type_info_type = xref_tag (class_type, get_identifier ("type_info"),
154 /*tag_scope=*/ts_current, false);
155 pop_namespace ();
156 const_type_info_type_node
157 = build_qualified_type (type_info_type, TYPE_QUAL_CONST);
158 type_info_ptr_type = build_pointer_type (const_type_info_type_node);
160 unemitted_tinfo_decls = VEC_alloc (tree, gc, 124);
162 create_tinfo_types ();
165 /* Given the expression EXP of type `class *', return the head of the
166 object pointed to by EXP with type cv void*, if the class has any
167 virtual functions (TYPE_POLYMORPHIC_P), else just return the
168 expression. */
170 tree
171 build_headof (tree exp)
173 tree type = TREE_TYPE (exp);
174 tree offset;
175 tree index;
177 gcc_assert (TREE_CODE (type) == POINTER_TYPE);
178 type = TREE_TYPE (type);
180 if (!TYPE_POLYMORPHIC_P (type))
181 return exp;
183 /* We use this a couple of times below, protect it. */
184 exp = save_expr (exp);
186 /* The offset-to-top field is at index -2 from the vptr. */
187 index = build_int_cst (NULL_TREE,
188 -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
190 offset = build_vtbl_ref (cp_build_indirect_ref (exp, NULL,
191 tf_warning_or_error),
192 index);
194 type = build_qualified_type (ptr_type_node,
195 cp_type_quals (TREE_TYPE (exp)));
196 return build2 (POINTER_PLUS_EXPR, type, exp,
197 convert_to_integer (sizetype, offset));
200 /* Get a bad_cast node for the program to throw...
202 See libstdc++/exception.cc for __throw_bad_cast */
204 static tree
205 throw_bad_cast (void)
207 tree fn = get_identifier ("__cxa_bad_cast");
208 if (!get_global_value_if_present (fn, &fn))
209 fn = push_throw_library_fn (fn, build_function_type (ptr_type_node,
210 void_list_node));
212 return build_cxx_call (fn, 0, NULL);
215 /* Return an expression for "__cxa_bad_typeid()". The expression
216 returned is an lvalue of type "const std::type_info". */
218 static tree
219 throw_bad_typeid (void)
221 tree fn = get_identifier ("__cxa_bad_typeid");
222 if (!get_global_value_if_present (fn, &fn))
224 tree t;
226 t = build_reference_type (const_type_info_type_node);
227 t = build_function_type (t, void_list_node);
228 fn = push_throw_library_fn (fn, t);
231 return build_cxx_call (fn, 0, NULL);
234 /* Return an lvalue expression whose type is "const std::type_info"
235 and whose value indicates the type of the expression EXP. If EXP
236 is a reference to a polymorphic class, return the dynamic type;
237 otherwise return the static type of the expression. */
239 static tree
240 get_tinfo_decl_dynamic (tree exp)
242 tree type;
243 tree t;
245 if (error_operand_p (exp))
246 return error_mark_node;
248 /* peel back references, so they match. */
249 type = non_reference (TREE_TYPE (exp));
251 /* Peel off cv qualifiers. */
252 type = TYPE_MAIN_VARIANT (type);
254 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
255 if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
256 type = complete_type_or_else (type, exp);
258 if (!type)
259 return error_mark_node;
261 /* If exp is a reference to polymorphic type, get the real type_info. */
262 if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0))
264 /* build reference to type_info from vtable. */
265 tree index;
267 /* The RTTI information is at index -1. */
268 index = build_int_cst (NULL_TREE,
269 -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
270 t = build_vtbl_ref (exp, index);
271 t = convert (type_info_ptr_type, t);
273 else
274 /* Otherwise return the type_info for the static type of the expr. */
275 t = get_tinfo_ptr (TYPE_MAIN_VARIANT (type));
277 return cp_build_indirect_ref (t, NULL, tf_warning_or_error);
280 static bool
281 typeid_ok_p (void)
283 tree pseudo_type_info, type_info_type;
285 if (! flag_rtti)
287 error ("cannot use typeid with -fno-rtti");
288 return false;
291 if (!COMPLETE_TYPE_P (const_type_info_type_node))
293 error ("must #include <typeinfo> before using typeid");
294 return false;
297 pseudo_type_info
298 = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE)->type;
299 type_info_type = TYPE_MAIN_VARIANT (const_type_info_type_node);
301 /* Make sure abi::__type_info_pseudo has the same alias set
302 as std::type_info. */
303 if (! TYPE_ALIAS_SET_KNOWN_P (pseudo_type_info))
304 TYPE_ALIAS_SET (pseudo_type_info) = get_alias_set (type_info_type);
305 else
306 gcc_assert (TYPE_ALIAS_SET (pseudo_type_info)
307 == get_alias_set (type_info_type));
309 return true;
312 /* Return an expression for "typeid(EXP)". The expression returned is
313 an lvalue of type "const std::type_info". */
315 tree
316 build_typeid (tree exp)
318 tree cond = NULL_TREE;
319 int nonnull = 0;
321 if (exp == error_mark_node || !typeid_ok_p ())
322 return error_mark_node;
324 if (processing_template_decl)
325 return build_min (TYPEID_EXPR, const_type_info_type_node, exp);
327 if (TREE_CODE (exp) == INDIRECT_REF
328 && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == POINTER_TYPE
329 && TYPE_POLYMORPHIC_P (TREE_TYPE (exp))
330 && ! resolves_to_fixed_type_p (exp, &nonnull)
331 && ! nonnull)
333 exp = stabilize_reference (exp);
334 cond = cp_convert (boolean_type_node, TREE_OPERAND (exp, 0));
337 exp = get_tinfo_decl_dynamic (exp);
339 if (exp == error_mark_node)
340 return error_mark_node;
342 if (cond)
344 tree bad = throw_bad_typeid ();
346 exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad);
349 return exp;
352 /* Generate the NTBS name of a type. */
353 static tree
354 tinfo_name (tree type)
356 const char *name;
357 tree name_string;
359 name = mangle_type_string (type);
360 name_string = fix_string_type (build_string (strlen (name) + 1, name));
361 return name_string;
364 /* Return a VAR_DECL for the internal ABI defined type_info object for
365 TYPE. You must arrange that the decl is mark_used, if actually use
366 it --- decls in vtables are only used if the vtable is output. */
368 tree
369 get_tinfo_decl (tree type)
371 tree name;
372 tree d;
374 if (variably_modified_type_p (type, /*fn=*/NULL_TREE))
376 error ("cannot create type information for type %qT because "
377 "it involves types of variable size",
378 type);
379 return error_mark_node;
382 if (TREE_CODE (type) == METHOD_TYPE)
383 type = build_function_type (TREE_TYPE (type),
384 TREE_CHAIN (TYPE_ARG_TYPES (type)));
386 /* For a class type, the variable is cached in the type node
387 itself. */
388 if (CLASS_TYPE_P (type))
390 d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type));
391 if (d)
392 return d;
395 name = mangle_typeinfo_for_type (type);
397 d = IDENTIFIER_GLOBAL_VALUE (name);
398 if (!d)
400 int ix = get_pseudo_ti_index (type);
401 tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, ix);
403 d = build_lang_decl (VAR_DECL, name, ti->type);
404 SET_DECL_ASSEMBLER_NAME (d, name);
405 /* Remember the type it is for. */
406 TREE_TYPE (name) = type;
407 DECL_TINFO_P (d) = 1;
408 DECL_ARTIFICIAL (d) = 1;
409 DECL_IGNORED_P (d) = 1;
410 TREE_READONLY (d) = 1;
411 TREE_STATIC (d) = 1;
412 /* Mark the variable as undefined -- but remember that we can
413 define it later if we need to do so. */
414 DECL_EXTERNAL (d) = 1;
415 DECL_NOT_REALLY_EXTERN (d) = 1;
416 set_linkage_according_to_type (type, d);
418 d = pushdecl_top_level_and_finish (d, NULL_TREE);
419 if (CLASS_TYPE_P (type))
420 CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d;
422 /* Add decl to the global array of tinfo decls. */
423 VEC_safe_push (tree, gc, unemitted_tinfo_decls, d);
426 return d;
429 /* Return a pointer to a type_info object describing TYPE, suitably
430 cast to the language defined type. */
432 static tree
433 get_tinfo_ptr (tree type)
435 tree decl = get_tinfo_decl (type);
437 mark_used (decl);
438 return build_nop (type_info_ptr_type,
439 build_address (decl));
442 /* Return the type_info object for TYPE. */
444 tree
445 get_typeid (tree type)
447 if (type == error_mark_node || !typeid_ok_p ())
448 return error_mark_node;
450 if (processing_template_decl)
451 return build_min (TYPEID_EXPR, const_type_info_type_node, type);
453 /* If the type of the type-id is a reference type, the result of the
454 typeid expression refers to a type_info object representing the
455 referenced type. */
456 type = non_reference (type);
458 /* The top-level cv-qualifiers of the lvalue expression or the type-id
459 that is the operand of typeid are always ignored. */
460 type = TYPE_MAIN_VARIANT (type);
462 /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */
463 if (CLASS_TYPE_P (type) || TREE_CODE (type) == UNKNOWN_TYPE)
464 type = complete_type_or_else (type, NULL_TREE);
466 if (!type)
467 return error_mark_node;
469 return cp_build_indirect_ref (get_tinfo_ptr (type), NULL,
470 tf_warning_or_error);
473 /* Check whether TEST is null before returning RESULT. If TEST is used in
474 RESULT, it must have previously had a save_expr applied to it. */
476 static tree
477 ifnonnull (tree test, tree result)
479 return build3 (COND_EXPR, TREE_TYPE (result),
480 build2 (EQ_EXPR, boolean_type_node, test,
481 cp_convert (TREE_TYPE (test), integer_zero_node)),
482 cp_convert (TREE_TYPE (result), integer_zero_node),
483 result);
486 /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working
487 paper. */
489 static tree
490 build_dynamic_cast_1 (tree type, tree expr, tsubst_flags_t complain)
492 enum tree_code tc = TREE_CODE (type);
493 tree exprtype = TREE_TYPE (expr);
494 tree dcast_fn;
495 tree old_expr = expr;
496 const char *errstr = NULL;
498 /* Save casted types in the function's used types hash table. */
499 used_types_insert (type);
501 /* T shall be a pointer or reference to a complete class type, or
502 `pointer to cv void''. */
503 switch (tc)
505 case POINTER_TYPE:
506 if (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE)
507 break;
508 /* Fall through. */
509 case REFERENCE_TYPE:
510 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
512 errstr = "target is not pointer or reference to class";
513 goto fail;
515 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
517 errstr = "target is not pointer or reference to complete type";
518 goto fail;
520 break;
522 default:
523 errstr = "target is not pointer or reference";
524 goto fail;
527 if (tc == POINTER_TYPE)
529 /* If T is a pointer type, v shall be an rvalue of a pointer to
530 complete class type, and the result is an rvalue of type T. */
532 if (TREE_CODE (exprtype) != POINTER_TYPE)
534 errstr = "source is not a pointer";
535 goto fail;
537 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
539 errstr = "source is not a pointer to class";
540 goto fail;
542 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
544 errstr = "source is a pointer to incomplete type";
545 goto fail;
548 else
550 exprtype = build_reference_type (exprtype);
552 /* T is a reference type, v shall be an lvalue of a complete class
553 type, and the result is an lvalue of the type referred to by T. */
555 if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype)))
557 errstr = "source is not of class type";
558 goto fail;
560 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype))))
562 errstr = "source is of incomplete class type";
563 goto fail;
566 /* Apply trivial conversion T -> T& for dereferenced ptrs. */
567 expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT,
568 LOOKUP_NORMAL, NULL_TREE);
571 /* The dynamic_cast operator shall not cast away constness. */
572 if (!at_least_as_qualified_p (TREE_TYPE (type),
573 TREE_TYPE (exprtype)))
575 errstr = "conversion casts away constness";
576 goto fail;
579 /* If *type is an unambiguous accessible base class of *exprtype,
580 convert statically. */
582 tree binfo;
584 binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type),
585 ba_check, NULL);
587 if (binfo)
589 expr = build_base_path (PLUS_EXPR, convert_from_reference (expr),
590 binfo, 0);
591 if (TREE_CODE (exprtype) == POINTER_TYPE)
592 expr = rvalue (expr);
593 return expr;
597 /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */
598 if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype)))
600 tree expr1;
601 /* if TYPE is `void *', return pointer to complete object. */
602 if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type)))
604 /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */
605 if (TREE_CODE (expr) == ADDR_EXPR
606 && TREE_CODE (TREE_OPERAND (expr, 0)) == VAR_DECL
607 && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE)
608 return build1 (NOP_EXPR, type, expr);
610 /* Since expr is used twice below, save it. */
611 expr = save_expr (expr);
613 expr1 = build_headof (expr);
614 if (TREE_TYPE (expr1) != type)
615 expr1 = build1 (NOP_EXPR, type, expr1);
616 return ifnonnull (expr, expr1);
618 else
620 tree retval;
621 tree result, td2, td3;
622 tree elems[4];
623 tree static_type, target_type, boff;
625 /* If we got here, we can't convert statically. Therefore,
626 dynamic_cast<D&>(b) (b an object) cannot succeed. */
627 if (tc == REFERENCE_TYPE)
629 if (TREE_CODE (old_expr) == VAR_DECL
630 && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE)
632 tree expr = throw_bad_cast ();
633 if (complain & tf_warning)
634 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
635 old_expr, type);
636 /* Bash it to the expected type. */
637 TREE_TYPE (expr) = type;
638 return expr;
641 /* Ditto for dynamic_cast<D*>(&b). */
642 else if (TREE_CODE (expr) == ADDR_EXPR)
644 tree op = TREE_OPERAND (expr, 0);
645 if (TREE_CODE (op) == VAR_DECL
646 && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE)
648 if (complain & tf_warning)
649 warning (0, "dynamic_cast of %q#D to %q#T can never succeed",
650 op, type);
651 retval = build_int_cst (type, 0);
652 return retval;
656 /* Use of dynamic_cast when -fno-rtti is prohibited. */
657 if (!flag_rtti)
659 if (complain & tf_error)
660 error ("%<dynamic_cast%> not permitted with -fno-rtti");
661 return error_mark_node;
664 target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
665 static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype));
666 td2 = get_tinfo_decl (target_type);
667 mark_used (td2);
668 td2 = cp_build_unary_op (ADDR_EXPR, td2, 0, complain);
669 td3 = get_tinfo_decl (static_type);
670 mark_used (td3);
671 td3 = cp_build_unary_op (ADDR_EXPR, td3, 0, complain);
673 /* Determine how T and V are related. */
674 boff = dcast_base_hint (static_type, target_type);
676 /* Since expr is used twice below, save it. */
677 expr = save_expr (expr);
679 expr1 = expr;
680 if (tc == REFERENCE_TYPE)
681 expr1 = cp_build_unary_op (ADDR_EXPR, expr1, 0, complain);
683 elems[0] = expr1;
684 elems[1] = td3;
685 elems[2] = td2;
686 elems[3] = boff;
688 dcast_fn = dynamic_cast_node;
689 if (!dcast_fn)
691 tree tmp;
692 tree tinfo_ptr;
693 const char *name;
695 push_abi_namespace ();
696 tinfo_ptr = xref_tag (class_type,
697 get_identifier ("__class_type_info"),
698 /*tag_scope=*/ts_current, false);
700 tinfo_ptr = build_pointer_type
701 (build_qualified_type
702 (tinfo_ptr, TYPE_QUAL_CONST));
703 name = "__dynamic_cast";
704 tmp = tree_cons
705 (NULL_TREE, const_ptr_type_node, tree_cons
706 (NULL_TREE, tinfo_ptr, tree_cons
707 (NULL_TREE, tinfo_ptr, tree_cons
708 (NULL_TREE, ptrdiff_type_node, void_list_node))));
709 tmp = build_function_type (ptr_type_node, tmp);
710 dcast_fn = build_library_fn_ptr (name, tmp);
711 DECL_PURE_P (dcast_fn) = 1;
712 pop_abi_namespace ();
713 dynamic_cast_node = dcast_fn;
715 result = build_cxx_call (dcast_fn, 4, elems);
717 if (tc == REFERENCE_TYPE)
719 tree bad = throw_bad_cast ();
720 tree neq;
722 result = save_expr (result);
723 neq = c_common_truthvalue_conversion (input_location, result);
724 return cp_convert (type,
725 build3 (COND_EXPR, TREE_TYPE (result),
726 neq, result, bad));
729 /* Now back to the type we want from a void*. */
730 result = cp_convert (type, result);
731 return ifnonnull (expr, result);
734 else
735 errstr = "source type is not polymorphic";
737 fail:
738 if (complain & tf_error)
739 error ("cannot dynamic_cast %qE (of type %q#T) to type %q#T (%s)",
740 expr, exprtype, type, errstr);
741 return error_mark_node;
744 tree
745 build_dynamic_cast (tree type, tree expr, tsubst_flags_t complain)
747 if (type == error_mark_node || expr == error_mark_node)
748 return error_mark_node;
750 if (processing_template_decl)
752 expr = build_min (DYNAMIC_CAST_EXPR, type, expr);
753 TREE_SIDE_EFFECTS (expr) = 1;
754 return convert_from_reference (expr);
757 return convert_from_reference (build_dynamic_cast_1 (type, expr, complain));
760 /* Return the runtime bit mask encoding the qualifiers of TYPE. */
762 static int
763 qualifier_flags (tree type)
765 int flags = 0;
766 int quals = cp_type_quals (type);
768 if (quals & TYPE_QUAL_CONST)
769 flags |= 1;
770 if (quals & TYPE_QUAL_VOLATILE)
771 flags |= 2;
772 if (quals & TYPE_QUAL_RESTRICT)
773 flags |= 4;
774 return flags;
777 /* Return true, if the pointer chain TYPE ends at an incomplete type, or
778 contains a pointer to member of an incomplete class. */
780 static bool
781 target_incomplete_p (tree type)
783 while (true)
784 if (TYPE_PTRMEM_P (type))
786 if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)))
787 return true;
788 type = TYPE_PTRMEM_POINTED_TO_TYPE (type);
790 else if (TREE_CODE (type) == POINTER_TYPE)
791 type = TREE_TYPE (type);
792 else
793 return !COMPLETE_OR_VOID_TYPE_P (type);
796 /* Returns true if TYPE involves an incomplete class type; in that
797 case, typeinfo variables for TYPE should be emitted with internal
798 linkage. */
800 static bool
801 involves_incomplete_p (tree type)
803 switch (TREE_CODE (type))
805 case POINTER_TYPE:
806 return target_incomplete_p (TREE_TYPE (type));
808 case OFFSET_TYPE:
809 ptrmem:
810 return
811 (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type))
812 || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type)));
814 case RECORD_TYPE:
815 if (TYPE_PTRMEMFUNC_P (type))
816 goto ptrmem;
817 /* Fall through. */
818 case UNION_TYPE:
819 if (!COMPLETE_TYPE_P (type))
820 return true;
822 default:
823 /* All other types do not involve incomplete class types. */
824 return false;
828 /* Return a CONSTRUCTOR for the common part of the type_info objects. This
829 is the vtable pointer and NTBS name. The NTBS name is emitted as a
830 comdat const char array, so it becomes a unique key for the type. Generate
831 and emit that VAR_DECL here. (We can't always emit the type_info itself
832 as comdat, because of pointers to incomplete.) */
834 static tree
835 tinfo_base_init (tinfo_s *ti, tree target)
837 tree init = NULL_TREE;
838 tree name_decl;
839 tree vtable_ptr;
842 tree name_name;
844 /* Generate the NTBS array variable. */
845 tree name_type = build_cplus_array_type
846 (build_qualified_type (char_type_node, TYPE_QUAL_CONST),
847 NULL_TREE);
848 tree name_string = tinfo_name (target);
850 /* Determine the name of the variable -- and remember with which
851 type it is associated. */
852 name_name = mangle_typeinfo_string_for_type (target);
853 TREE_TYPE (name_name) = target;
855 name_decl = build_lang_decl (VAR_DECL, name_name, name_type);
856 SET_DECL_ASSEMBLER_NAME (name_decl, name_name);
857 DECL_ARTIFICIAL (name_decl) = 1;
858 DECL_IGNORED_P (name_decl) = 1;
859 TREE_READONLY (name_decl) = 1;
860 TREE_STATIC (name_decl) = 1;
861 DECL_EXTERNAL (name_decl) = 0;
862 DECL_TINFO_P (name_decl) = 1;
863 set_linkage_according_to_type (target, name_decl);
864 import_export_decl (name_decl);
865 DECL_INITIAL (name_decl) = name_string;
866 mark_used (name_decl);
867 pushdecl_top_level_and_finish (name_decl, name_string);
870 vtable_ptr = ti->vtable;
871 if (!vtable_ptr)
873 tree real_type;
874 push_abi_namespace ();
875 real_type = xref_tag (class_type, ti->name,
876 /*tag_scope=*/ts_current, false);
877 pop_abi_namespace ();
879 if (!COMPLETE_TYPE_P (real_type))
881 /* We never saw a definition of this type, so we need to
882 tell the compiler that this is an exported class, as
883 indeed all of the __*_type_info classes are. */
884 SET_CLASSTYPE_INTERFACE_KNOWN (real_type);
885 CLASSTYPE_INTERFACE_ONLY (real_type) = 1;
888 vtable_ptr = get_vtable_decl (real_type, /*complete=*/1);
889 vtable_ptr = cp_build_unary_op (ADDR_EXPR, vtable_ptr, 0,
890 tf_warning_or_error);
892 /* We need to point into the middle of the vtable. */
893 vtable_ptr = build2
894 (POINTER_PLUS_EXPR, TREE_TYPE (vtable_ptr), vtable_ptr,
895 size_binop (MULT_EXPR,
896 size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE),
897 TYPE_SIZE_UNIT (vtable_entry_type)));
899 ti->vtable = vtable_ptr;
902 init = tree_cons (NULL_TREE, vtable_ptr, init);
904 init = tree_cons (NULL_TREE, decay_conversion (name_decl), init);
906 init = build_constructor_from_list (init_list_type_node, nreverse (init));
907 TREE_CONSTANT (init) = 1;
908 TREE_STATIC (init) = 1;
909 init = tree_cons (NULL_TREE, init, NULL_TREE);
911 return init;
914 /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the
915 information about the particular type_info derivation, which adds no
916 additional fields to the type_info base. */
918 static tree
919 generic_initializer (tinfo_s *ti, tree target)
921 tree init = tinfo_base_init (ti, target);
923 init = build_constructor_from_list (init_list_type_node, init);
924 TREE_CONSTANT (init) = 1;
925 TREE_STATIC (init) = 1;
926 return init;
929 /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE.
930 TI provides information about the particular type_info derivation,
931 which adds target type and qualifier flags members to the type_info base. */
933 static tree
934 ptr_initializer (tinfo_s *ti, tree target)
936 tree init = tinfo_base_init (ti, target);
937 tree to = TREE_TYPE (target);
938 int flags = qualifier_flags (to);
939 bool incomplete = target_incomplete_p (to);
941 if (incomplete)
942 flags |= 8;
943 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
944 init = tree_cons (NULL_TREE,
945 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
946 init);
948 init = build_constructor_from_list (init_list_type_node, nreverse (init));
949 TREE_CONSTANT (init) = 1;
950 TREE_STATIC (init) = 1;
951 return init;
954 /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE.
955 TI provides information about the particular type_info derivation,
956 which adds class, target type and qualifier flags members to the type_info
957 base. */
959 static tree
960 ptm_initializer (tinfo_s *ti, tree target)
962 tree init = tinfo_base_init (ti, target);
963 tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target);
964 tree klass = TYPE_PTRMEM_CLASS_TYPE (target);
965 int flags = qualifier_flags (to);
966 bool incomplete = target_incomplete_p (to);
968 if (incomplete)
969 flags |= 0x8;
970 if (!COMPLETE_TYPE_P (klass))
971 flags |= 0x10;
972 init = tree_cons (NULL_TREE, build_int_cst (NULL_TREE, flags), init);
973 init = tree_cons (NULL_TREE,
974 get_tinfo_ptr (TYPE_MAIN_VARIANT (to)),
975 init);
976 init = tree_cons (NULL_TREE,
977 get_tinfo_ptr (klass),
978 init);
980 init = build_constructor_from_list (init_list_type_node, nreverse (init));
981 TREE_CONSTANT (init) = 1;
982 TREE_STATIC (init) = 1;
983 return init;
986 /* Return the CONSTRUCTOR expr for a type_info of class TYPE.
987 TI provides information about the particular __class_type_info derivation,
988 which adds hint flags and TRAIL initializers to the type_info base. */
990 static tree
991 class_initializer (tinfo_s *ti, tree target, tree trail)
993 tree init = tinfo_base_init (ti, target);
995 TREE_CHAIN (init) = trail;
996 init = build_constructor_from_list (init_list_type_node, init);
997 TREE_CONSTANT (init) = 1;
998 TREE_STATIC (init) = 1;
999 return init;
1002 /* Returns true if the typeinfo for type should be placed in
1003 the runtime library. */
1005 static bool
1006 typeinfo_in_lib_p (tree type)
1008 /* The typeinfo objects for `T*' and `const T*' are in the runtime
1009 library for simple types T. */
1010 if (TREE_CODE (type) == POINTER_TYPE
1011 && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST
1012 || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED))
1013 type = TREE_TYPE (type);
1015 switch (TREE_CODE (type))
1017 case INTEGER_TYPE:
1018 case BOOLEAN_TYPE:
1019 case REAL_TYPE:
1020 case VOID_TYPE:
1021 return true;
1023 default:
1024 return false;
1028 /* Generate the initializer for the type info describing TYPE. TK_INDEX is
1029 the index of the descriptor in the tinfo_desc vector. */
1031 static tree
1032 get_pseudo_ti_init (tree type, unsigned tk_index)
1034 tinfo_s *ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1036 gcc_assert (at_eof);
1037 switch (tk_index)
1039 case TK_POINTER_MEMBER_TYPE:
1040 return ptm_initializer (ti, type);
1042 case TK_POINTER_TYPE:
1043 return ptr_initializer (ti, type);
1045 case TK_BUILTIN_TYPE:
1046 case TK_ENUMERAL_TYPE:
1047 case TK_FUNCTION_TYPE:
1048 case TK_ARRAY_TYPE:
1049 return generic_initializer (ti, type);
1051 case TK_CLASS_TYPE:
1052 return class_initializer (ti, type, NULL_TREE);
1054 case TK_SI_CLASS_TYPE:
1056 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0);
1057 tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1058 tree base_inits = tree_cons (NULL_TREE, tinfo, NULL_TREE);
1060 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1061 ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1062 return class_initializer (ti, type, base_inits);
1065 default:
1067 int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0)
1068 | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1));
1069 tree binfo = TYPE_BINFO (type);
1070 int nbases = BINFO_N_BASE_BINFOS (binfo);
1071 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1072 tree offset_type = integer_types[itk_long];
1073 tree base_inits = NULL_TREE;
1074 int ix;
1076 gcc_assert (tk_index >= TK_FIXED);
1078 /* Generate the base information initializer. */
1079 for (ix = nbases; ix--;)
1081 tree base_binfo = BINFO_BASE_BINFO (binfo, ix);
1082 tree base_init = NULL_TREE;
1083 int flags = 0;
1084 tree tinfo;
1085 tree offset;
1087 if (VEC_index (tree, base_accesses, ix) == access_public_node)
1088 flags |= 2;
1089 tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo));
1090 if (BINFO_VIRTUAL_P (base_binfo))
1092 /* We store the vtable offset at which the virtual
1093 base offset can be found. */
1094 offset = BINFO_VPTR_FIELD (base_binfo);
1095 flags |= 1;
1097 else
1098 offset = BINFO_OFFSET (base_binfo);
1100 /* Combine offset and flags into one field. */
1101 offset = fold_convert (offset_type, offset);
1102 offset = fold_build2 (LSHIFT_EXPR, offset_type, offset,
1103 build_int_cst (offset_type, 8));
1104 offset = fold_build2 (BIT_IOR_EXPR, offset_type, offset,
1105 build_int_cst (offset_type, flags));
1106 base_init = tree_cons (NULL_TREE, offset, base_init);
1107 base_init = tree_cons (NULL_TREE, tinfo, base_init);
1108 base_init = build_constructor_from_list (init_list_type_node, base_init);
1109 base_inits = tree_cons (NULL_TREE, base_init, base_inits);
1111 base_inits = build_constructor_from_list (init_list_type_node, base_inits);
1112 base_inits = tree_cons (NULL_TREE, base_inits, NULL_TREE);
1113 /* Prepend the number of bases. */
1114 base_inits = tree_cons (NULL_TREE,
1115 build_int_cst (NULL_TREE, nbases),
1116 base_inits);
1117 /* Prepend the hint flags. */
1118 base_inits = tree_cons (NULL_TREE,
1119 build_int_cst (NULL_TREE, hint),
1120 base_inits);
1122 /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */
1123 ti = VEC_index (tinfo_s, tinfo_descs, tk_index);
1124 return class_initializer (ti, type, base_inits);
1129 /* Generate the RECORD_TYPE containing the data layout of a type_info
1130 derivative as used by the runtime. This layout must be consistent with
1131 that defined in the runtime support. Also generate the VAR_DECL for the
1132 type's vtable. We explicitly manage the vtable member, and name it for
1133 real type as used in the runtime. The RECORD type has a different name,
1134 to avoid collisions. Return a TREE_LIST who's TINFO_PSEUDO_TYPE
1135 is the generated type and TINFO_VTABLE_NAME is the name of the
1136 vtable. We have to delay generating the VAR_DECL of the vtable
1137 until the end of the translation, when we'll have seen the library
1138 definition, if there was one.
1140 REAL_NAME is the runtime's name of the type. Trailing arguments are
1141 additional FIELD_DECL's for the structure. The final argument must be
1142 NULL. */
1144 static void
1145 create_pseudo_type_info (int tk, const char *real_name, ...)
1147 tinfo_s *ti;
1148 tree pseudo_type;
1149 char *pseudo_name;
1150 tree fields;
1151 tree field_decl;
1152 va_list ap;
1154 va_start (ap, real_name);
1156 /* Generate the pseudo type name. */
1157 pseudo_name = (char *) alloca (strlen (real_name) + 30);
1158 strcpy (pseudo_name, real_name);
1159 strcat (pseudo_name, "_pseudo");
1160 if (tk >= TK_FIXED)
1161 sprintf (pseudo_name + strlen (pseudo_name), "%d", tk - TK_FIXED);
1163 /* First field is the pseudo type_info base class. */
1164 fields = build_decl (FIELD_DECL, NULL_TREE,
1165 VEC_index (tinfo_s, tinfo_descs,
1166 TK_TYPE_INFO_TYPE)->type);
1168 /* Now add the derived fields. */
1169 while ((field_decl = va_arg (ap, tree)))
1171 TREE_CHAIN (field_decl) = fields;
1172 fields = field_decl;
1175 /* Create the pseudo type. */
1176 pseudo_type = make_class_type (RECORD_TYPE);
1177 finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE);
1178 CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type;
1180 ti = VEC_index (tinfo_s, tinfo_descs, tk);
1181 ti->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST);
1182 ti->name = get_identifier (real_name);
1183 ti->vtable = NULL_TREE;
1185 /* Pretend this is public so determine_visibility doesn't give vtables
1186 internal linkage. */
1187 TREE_PUBLIC (TYPE_MAIN_DECL (ti->type)) = 1;
1189 va_end (ap);
1192 /* Return the index of a pseudo type info type node used to describe
1193 TYPE. TYPE must be a complete type (or cv void), except at the end
1194 of the translation unit. */
1196 static unsigned
1197 get_pseudo_ti_index (tree type)
1199 unsigned ix;
1201 switch (TREE_CODE (type))
1203 case OFFSET_TYPE:
1204 ix = TK_POINTER_MEMBER_TYPE;
1205 break;
1207 case POINTER_TYPE:
1208 ix = TK_POINTER_TYPE;
1209 break;
1211 case ENUMERAL_TYPE:
1212 ix = TK_ENUMERAL_TYPE;
1213 break;
1215 case FUNCTION_TYPE:
1216 ix = TK_FUNCTION_TYPE;
1217 break;
1219 case ARRAY_TYPE:
1220 ix = TK_ARRAY_TYPE;
1221 break;
1223 case UNION_TYPE:
1224 case RECORD_TYPE:
1225 if (TYPE_PTRMEMFUNC_P (type))
1227 ix = TK_POINTER_MEMBER_TYPE;
1228 break;
1230 else if (!COMPLETE_TYPE_P (type))
1232 if (!at_eof)
1233 cxx_incomplete_type_error (NULL_TREE, type);
1234 ix = TK_CLASS_TYPE;
1235 break;
1237 else if (!BINFO_N_BASE_BINFOS (TYPE_BINFO (type)))
1239 ix = TK_CLASS_TYPE;
1240 break;
1242 else
1244 tree binfo = TYPE_BINFO (type);
1245 VEC(tree,gc) *base_accesses = BINFO_BASE_ACCESSES (binfo);
1246 tree base_binfo = BINFO_BASE_BINFO (binfo, 0);
1247 int num_bases = BINFO_N_BASE_BINFOS (binfo);
1249 if (num_bases == 1
1250 && VEC_index (tree, base_accesses, 0) == access_public_node
1251 && !BINFO_VIRTUAL_P (base_binfo)
1252 && integer_zerop (BINFO_OFFSET (base_binfo)))
1254 /* single non-virtual public. */
1255 ix = TK_SI_CLASS_TYPE;
1256 break;
1258 else
1260 tinfo_s *ti;
1261 tree array_domain, base_array;
1263 ix = TK_FIXED + num_bases;
1264 if (VEC_length (tinfo_s, tinfo_descs) <= ix)
1266 /* too short, extend. */
1267 unsigned len = VEC_length (tinfo_s, tinfo_descs);
1269 VEC_safe_grow (tinfo_s, gc, tinfo_descs, ix + 1);
1270 while (VEC_iterate (tinfo_s, tinfo_descs, len++, ti))
1271 ti->type = ti->vtable = ti->name = NULL_TREE;
1273 else if (VEC_index (tinfo_s, tinfo_descs, ix)->type)
1274 /* already created. */
1275 break;
1277 /* Create the array of __base_class_type_info entries.
1278 G++ 3.2 allocated an array that had one too many
1279 entries, and then filled that extra entries with
1280 zeros. */
1281 if (abi_version_at_least (2))
1282 array_domain = build_index_type (size_int (num_bases - 1));
1283 else
1284 array_domain = build_index_type (size_int (num_bases));
1285 base_array =
1286 build_array_type (VEC_index (tinfo_s, tinfo_descs,
1287 TK_BASE_TYPE)->type,
1288 array_domain);
1290 push_abi_namespace ();
1291 create_pseudo_type_info
1292 (ix, "__vmi_class_type_info",
1293 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1294 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1295 build_decl (FIELD_DECL, NULL_TREE, base_array),
1296 NULL);
1297 pop_abi_namespace ();
1298 break;
1301 default:
1302 ix = TK_BUILTIN_TYPE;
1303 break;
1305 return ix;
1308 /* Make sure the required builtin types exist for generating the type_info
1309 variable definitions. */
1311 static void
1312 create_tinfo_types (void)
1314 tinfo_s *ti;
1316 gcc_assert (!tinfo_descs);
1318 VEC_safe_grow (tinfo_s, gc, tinfo_descs, TK_FIXED);
1320 push_abi_namespace ();
1322 /* Create the internal type_info structure. This is used as a base for
1323 the other structures. */
1325 tree field, fields;
1327 field = build_decl (FIELD_DECL, NULL_TREE, const_ptr_type_node);
1328 fields = field;
1330 field = build_decl (FIELD_DECL, NULL_TREE, const_string_type_node);
1331 TREE_CHAIN (field) = fields;
1332 fields = field;
1334 ti = VEC_index (tinfo_s, tinfo_descs, TK_TYPE_INFO_TYPE);
1335 ti->type = make_class_type (RECORD_TYPE);
1336 ti->vtable = NULL_TREE;
1337 ti->name = NULL_TREE;
1338 finish_builtin_struct (ti->type, "__type_info_pseudo",
1339 fields, NULL_TREE);
1342 /* Fundamental type_info */
1343 create_pseudo_type_info (TK_BUILTIN_TYPE, "__fundamental_type_info", NULL);
1345 /* Array, function and enum type_info. No additional fields. */
1346 create_pseudo_type_info (TK_ARRAY_TYPE, "__array_type_info", NULL);
1347 create_pseudo_type_info (TK_FUNCTION_TYPE, "__function_type_info", NULL);
1348 create_pseudo_type_info (TK_ENUMERAL_TYPE, "__enum_type_info", NULL);
1350 /* Class type_info. No additional fields. */
1351 create_pseudo_type_info (TK_CLASS_TYPE, "__class_type_info", NULL);
1353 /* Single public non-virtual base class. Add pointer to base class.
1354 This is really a descendant of __class_type_info. */
1355 create_pseudo_type_info (TK_SI_CLASS_TYPE, "__si_class_type_info",
1356 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1357 NULL);
1359 /* Base class internal helper. Pointer to base type, offset to base,
1360 flags. */
1362 tree field, fields;
1364 field = build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type);
1365 fields = field;
1367 field = build_decl (FIELD_DECL, NULL_TREE, integer_types[itk_long]);
1368 TREE_CHAIN (field) = fields;
1369 fields = field;
1371 ti = VEC_index (tinfo_s, tinfo_descs, TK_BASE_TYPE);
1373 ti->type = make_class_type (RECORD_TYPE);
1374 ti->vtable = NULL_TREE;
1375 ti->name = NULL_TREE;
1376 finish_builtin_struct (ti->type, "__base_class_type_info_pseudo",
1377 fields, NULL_TREE);
1380 /* Pointer type_info. Adds two fields, qualification mask
1381 and pointer to the pointed to type. This is really a descendant of
1382 __pbase_type_info. */
1383 create_pseudo_type_info (TK_POINTER_TYPE, "__pointer_type_info",
1384 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1385 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1386 NULL);
1388 /* Pointer to member data type_info. Add qualifications flags,
1389 pointer to the member's type info and pointer to the class.
1390 This is really a descendant of __pbase_type_info. */
1391 create_pseudo_type_info (TK_POINTER_MEMBER_TYPE,
1392 "__pointer_to_member_type_info",
1393 build_decl (FIELD_DECL, NULL_TREE, integer_type_node),
1394 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1395 build_decl (FIELD_DECL, NULL_TREE, type_info_ptr_type),
1396 NULL);
1398 pop_abi_namespace ();
1401 /* Emit the type_info descriptors which are guaranteed to be in the runtime
1402 support. Generating them here guarantees consistency with the other
1403 structures. We use the following heuristic to determine when the runtime
1404 is being generated. If std::__fundamental_type_info is defined, and its
1405 destructor is defined, then the runtime is being built. */
1407 void
1408 emit_support_tinfos (void)
1410 static tree *const fundamentals[] =
1412 &void_type_node,
1413 &boolean_type_node,
1414 &wchar_type_node, &char16_type_node, &char32_type_node,
1415 &char_type_node, &signed_char_type_node, &unsigned_char_type_node,
1416 &short_integer_type_node, &short_unsigned_type_node,
1417 &integer_type_node, &unsigned_type_node,
1418 &long_integer_type_node, &long_unsigned_type_node,
1419 &long_long_integer_type_node, &long_long_unsigned_type_node,
1420 &float_type_node, &double_type_node, &long_double_type_node,
1423 int ix;
1424 tree bltn_type, dtor;
1426 push_abi_namespace ();
1427 bltn_type = xref_tag (class_type,
1428 get_identifier ("__fundamental_type_info"),
1429 /*tag_scope=*/ts_current, false);
1430 pop_abi_namespace ();
1431 if (!COMPLETE_TYPE_P (bltn_type))
1432 return;
1433 dtor = CLASSTYPE_DESTRUCTORS (bltn_type);
1434 if (!dtor || DECL_EXTERNAL (dtor))
1435 return;
1436 doing_runtime = 1;
1437 for (ix = 0; fundamentals[ix]; ix++)
1439 tree bltn = *fundamentals[ix];
1440 tree types[3];
1441 int i;
1443 types[0] = bltn;
1444 types[1] = build_pointer_type (bltn);
1445 types[2] = build_pointer_type (build_qualified_type (bltn,
1446 TYPE_QUAL_CONST));
1448 for (i = 0; i < 3; ++i)
1450 tree tinfo;
1452 tinfo = get_tinfo_decl (types[i]);
1453 TREE_USED (tinfo) = 1;
1454 mark_needed (tinfo);
1455 /* The C++ ABI requires that these objects be COMDAT. But,
1456 On systems without weak symbols, initialized COMDAT
1457 objects are emitted with internal linkage. (See
1458 comdat_linkage for details.) Since we want these objects
1459 to have external linkage so that copies do not have to be
1460 emitted in code outside the runtime library, we make them
1461 non-COMDAT here.
1463 It might also not be necessary to follow this detail of the
1464 ABI. */
1465 if (!flag_weak || ! targetm.cxx.library_rtti_comdat ())
1467 gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo));
1468 DECL_INTERFACE_KNOWN (tinfo) = 1;
1474 /* Finish a type info decl. DECL_PTR is a pointer to an unemitted
1475 tinfo decl. Determine whether it needs emitting, and if so
1476 generate the initializer. */
1478 bool
1479 emit_tinfo_decl (tree decl)
1481 tree type = TREE_TYPE (DECL_NAME (decl));
1482 int in_library = typeinfo_in_lib_p (type);
1484 gcc_assert (DECL_TINFO_P (decl));
1486 if (in_library)
1488 if (doing_runtime)
1489 DECL_EXTERNAL (decl) = 0;
1490 else
1492 /* If we're not in the runtime, then DECL (which is already
1493 DECL_EXTERNAL) will not be defined here. */
1494 DECL_INTERFACE_KNOWN (decl) = 1;
1495 return false;
1498 else if (involves_incomplete_p (type))
1500 if (!decl_needed_p (decl))
1501 return false;
1502 /* If TYPE involves an incomplete class type, then the typeinfo
1503 object will be emitted with internal linkage. There is no
1504 way to know whether or not types are incomplete until the end
1505 of the compilation, so this determination must be deferred
1506 until this point. */
1507 TREE_PUBLIC (decl) = 0;
1508 DECL_EXTERNAL (decl) = 0;
1509 DECL_INTERFACE_KNOWN (decl) = 1;
1512 import_export_decl (decl);
1513 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl))
1515 tree init;
1517 DECL_EXTERNAL (decl) = 0;
1518 init = get_pseudo_ti_init (type, get_pseudo_ti_index (type));
1519 DECL_INITIAL (decl) = init;
1520 mark_used (decl);
1521 finish_decl (decl, init, NULL_TREE, NULL_TREE);
1522 return true;
1524 else
1525 return false;
1528 #include "gt-cp-rtti.h"