]>
Commit | Line | Data |
---|---|---|
84a43794 | 1 | /* RunTime Type Identification |
8d9254fc | 2 | Copyright (C) 1995-2020 Free Software Foundation, Inc. |
6b5fbb55 | 3 | Mostly written by Jason Merrill (jason@cygnus.com). |
84a43794 | 4 | |
f5adbb8d | 5 | This file is part of GCC. |
84a43794 | 6 | |
f5adbb8d | 7 | GCC is free software; you can redistribute it and/or modify |
84a43794 | 8 | it under the terms of the GNU General Public License as published by |
e77f031d | 9 | the Free Software Foundation; either version 3, or (at your option) |
84a43794 MS |
10 | any later version. |
11 | ||
f5adbb8d | 12 | GCC is distributed in the hope that it will be useful, |
84a43794 MS |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
e77f031d NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
84a43794 | 20 | |
84a43794 | 21 | #include "config.h" |
8d052bc7 | 22 | #include "system.h" |
4977bab6 | 23 | #include "coretypes.h" |
2adfab87 | 24 | #include "target.h" |
2adfab87 | 25 | #include "cp-tree.h" |
4d0cdd0c | 26 | #include "memmodel.h" |
c517fc92 | 27 | #include "tm_p.h" |
d8a2d370 | 28 | #include "stringpool.h" |
2adfab87 | 29 | #include "intl.h" |
d8a2d370 | 30 | #include "stor-layout.h" |
39dabefd | 31 | #include "c-family/c-pragma.h" |
eea77d1f | 32 | #include "gcc-rich-location.h" |
5156628f | 33 | |
d689a8f1 NS |
34 | /* C++ returns type information to the user in struct type_info |
35 | objects. We also use type information to implement dynamic_cast and | |
36 | exception handlers. Type information for a particular type is | |
37 | indicated with an ABI defined structure derived from type_info. | |
38 | This would all be very straight forward, but for the fact that the | |
39 | runtime library provides the definitions of the type_info structure | |
40 | and the ABI defined derived classes. We cannot build declarations | |
41 | of them directly in the compiler, but we need to layout objects of | |
42 | their type. Somewhere we have to lie. | |
43 | ||
44 | We define layout compatible POD-structs with compiler-defined names | |
45 | and generate the appropriate initializations for them (complete | |
46 | with explicit mention of their vtable). When we have to provide a | |
47 | type_info to the user we reinterpret_cast the internal compiler | |
48 | type to type_info. A well formed program can only explicitly refer | |
49 | to the type_infos of complete types (& cv void). However, we chain | |
50 | pointer type_infos to the pointed-to-type, and that can be | |
51 | incomplete. We only need the addresses of such incomplete | |
52 | type_info objects for static initialization. | |
53 | ||
54 | The type information VAR_DECL of a type is held on the | |
87e3d7cf | 55 | get_global_binding of the type's mangled name. That VAR_DECL |
d689a8f1 NS |
56 | will be the internal type. It will usually have the correct |
57 | internal type reflecting the kind of type it represents (pointer, | |
58 | array, function, class, inherited class, etc). When the type it | |
59 | represents is incomplete, it will have the internal type | |
60 | corresponding to type_info. That will only happen at the end of | |
61 | translation, when we are emitting the type info objects. */ | |
62 | ||
1bf5fc34 | 63 | /* Auxiliary data we hold for each type_info derived object we need. */ |
a79683d5 | 64 | struct GTY (()) tinfo_s { |
1bf5fc34 NS |
65 | tree type; /* The RECORD_TYPE for this type_info object */ |
66 | ||
67 | tree vtable; /* The VAR_DECL of the vtable. Only filled at end of | |
68 | translation. */ | |
69 | ||
70 | tree name; /* IDENTIFIER_NODE for the ABI specified name of | |
71 | the type_info derived type. */ | |
a79683d5 | 72 | }; |
1bf5fc34 | 73 | |
1bf5fc34 | 74 | |
a79683d5 | 75 | enum tinfo_kind |
1bf5fc34 | 76 | { |
3a44f395 | 77 | TK_TYPE_INFO_TYPE, /* abi::__type_info_pseudo */ |
1bf5fc34 | 78 | TK_BASE_TYPE, /* abi::__base_class_type_info */ |
ac9a1c7e NS |
79 | TK_DERIVED_TYPES, /* Start of types derived from abi::__type_info */ |
80 | TK_BUILTIN_TYPE = TK_DERIVED_TYPES, /* abi::__fundamental_type_info */ | |
1bf5fc34 NS |
81 | TK_ARRAY_TYPE, /* abi::__array_type_info */ |
82 | TK_FUNCTION_TYPE, /* abi::__function_type_info */ | |
83 | TK_ENUMERAL_TYPE, /* abi::__enum_type_info */ | |
84 | TK_POINTER_TYPE, /* abi::__pointer_type_info */ | |
85 | TK_POINTER_MEMBER_TYPE, /* abi::__pointer_to_member_type_info */ | |
86 | TK_CLASS_TYPE, /* abi::__class_type_info */ | |
87 | TK_SI_CLASS_TYPE, /* abi::__si_class_type_info */ | |
ac9a1c7e NS |
88 | TK_VMI_CLASS_TYPES, /* abi::__vmi_class_type_info<int> */ |
89 | TK_MAX | |
90 | }; | |
91 | ||
92 | /* Names of the tinfo types. Must be same order as TK enumeration | |
93 | above. */ | |
94 | ||
95 | static const char *const tinfo_names[TK_MAX] = | |
96 | { | |
97 | "__type_info", | |
98 | "__base_class_type_info", | |
99 | "__fundamental_type_info", | |
100 | "__array_type_info", | |
101 | "__function_type_info", | |
102 | "__enum_type_info", | |
103 | "__pointer_type_info", | |
104 | "__pointer_to_member_type_info", | |
105 | "__class_type_info", | |
106 | "__si_class_type_info", | |
107 | "__vmi_class_type_info" | |
a79683d5 | 108 | }; |
84a43794 | 109 | |
1c773c7a KT |
110 | /* Helper macro to get maximum scalar-width of pointer or of the 'long'-type. |
111 | This of interest for llp64 targets. */ | |
112 | #define LONGPTR_T \ | |
113 | integer_types[(POINTER_SIZE <= TYPE_PRECISION (integer_types[itk_long]) \ | |
114 | ? itk_long : itk_long_long)] | |
115 | ||
94214953 | 116 | /* A vector of all tinfo decls that haven't yet been emitted. */ |
9771b263 | 117 | vec<tree, va_gc> *unemitted_tinfo_decls; |
a260bce6 | 118 | |
1bf5fc34 NS |
119 | /* A vector of all type_info derived types we need. The first few are |
120 | fixed and created early. The remainder are for multiple inheritance | |
121 | and are generated as needed. */ | |
9771b263 | 122 | static GTY (()) vec<tinfo_s, va_gc> *tinfo_descs; |
1bf5fc34 | 123 | |
4b978f96 | 124 | static tree ifnonnull (tree, tree, tsubst_flags_t); |
52669d59 | 125 | static tree tinfo_name (tree, bool); |
ca6932ad | 126 | static tree build_dynamic_cast_1 (location_t, tree, tree, tsubst_flags_t); |
cec57f72 NN |
127 | static tree throw_bad_cast (void); |
128 | static tree throw_bad_typeid (void); | |
cec57f72 NN |
129 | static tree get_tinfo_ptr (tree); |
130 | static bool typeid_ok_p (void); | |
131 | static int qualifier_flags (tree); | |
848eed92 | 132 | static bool target_incomplete_p (tree); |
1bf5fc34 NS |
133 | static tree tinfo_base_init (tinfo_s *, tree); |
134 | static tree generic_initializer (tinfo_s *, tree); | |
135 | static tree ptr_initializer (tinfo_s *, tree); | |
136 | static tree ptm_initializer (tinfo_s *, tree); | |
e20463aa | 137 | static tree class_initializer (tinfo_s *, tree, unsigned, ...); |
1bf5fc34 NS |
138 | static tree get_pseudo_ti_init (tree, unsigned); |
139 | static unsigned get_pseudo_ti_index (tree); | |
ac9a1c7e | 140 | static tinfo_s *get_tinfo_desc (unsigned); |
cec57f72 | 141 | static void create_tinfo_types (void); |
848eed92 | 142 | static bool typeinfo_in_lib_p (tree); |
7267d692 NS |
143 | |
144 | static int doing_runtime = 0; | |
84a43794 | 145 | \f |
a15f0fd0 JJ |
146 | static void |
147 | push_abi_namespace (void) | |
148 | { | |
149 | push_nested_namespace (abi_node); | |
9789ba46 | 150 | push_visibility ("default", 2); |
a15f0fd0 JJ |
151 | } |
152 | ||
153 | static void | |
154 | pop_abi_namespace (void) | |
155 | { | |
9789ba46 | 156 | pop_visibility (2); |
a15f0fd0 JJ |
157 | pop_nested_namespace (abi_node); |
158 | } | |
d689a8f1 NS |
159 | |
160 | /* Declare language defined type_info type and a pointer to const | |
161 | type_info. This is incomplete here, and will be completed when | |
162 | the user #includes <typeinfo>. There are language defined | |
163 | restrictions on what can be done until that is included. Create | |
164 | the internal versions of the ABI types. */ | |
165 | ||
6b5fbb55 | 166 | void |
848eed92 | 167 | init_rtti_processing (void) |
6b5fbb55 | 168 | { |
db24eb1f | 169 | tree type_info_type; |
c8094d83 | 170 | |
5a7c4505 | 171 | push_nested_namespace (std_node); |
db24eb1f | 172 | type_info_type = xref_tag (class_type, get_identifier ("type_info"), |
bd3d082e | 173 | /*tag_scope=*/ts_current, false); |
5a7c4505 | 174 | pop_nested_namespace (std_node); |
db24eb1f | 175 | const_type_info_type_node |
a3360e77 | 176 | = cp_build_qualified_type (type_info_type, TYPE_QUAL_CONST); |
db24eb1f | 177 | type_info_ptr_type = build_pointer_type (const_type_info_type_node); |
d689a8f1 | 178 | |
9771b263 | 179 | vec_alloc (unemitted_tinfo_decls, 124); |
c8094d83 | 180 | |
d689a8f1 | 181 | create_tinfo_types (); |
6b5fbb55 MS |
182 | } |
183 | ||
6b5fbb55 MS |
184 | /* Given the expression EXP of type `class *', return the head of the |
185 | object pointed to by EXP with type cv void*, if the class has any | |
4c6b7393 | 186 | virtual functions (TYPE_POLYMORPHIC_P), else just return the |
6b5fbb55 | 187 | expression. */ |
e92cc029 | 188 | |
6f06d231 | 189 | tree |
848eed92 | 190 | build_headof (tree exp) |
84a43794 MS |
191 | { |
192 | tree type = TREE_TYPE (exp); | |
de22184b | 193 | tree offset; |
da3d4dfa | 194 | tree index; |
84a43794 | 195 | |
50e10fa8 | 196 | gcc_assert (TYPE_PTR_P (type)); |
84a43794 MS |
197 | type = TREE_TYPE (type); |
198 | ||
4c6b7393 | 199 | if (!TYPE_POLYMORPHIC_P (type)) |
84a43794 MS |
200 | return exp; |
201 | ||
6b5fbb55 MS |
202 | /* We use this a couple of times below, protect it. */ |
203 | exp = save_expr (exp); | |
204 | ||
3461fba7 | 205 | /* The offset-to-top field is at index -2 from the vptr. */ |
4a90aeeb | 206 | index = build_int_cst (NULL_TREE, |
7d60be94 | 207 | -2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE); |
da3d4dfa | 208 | |
04757a2a | 209 | offset = build_vtbl_ref (cp_build_fold_indirect_ref (exp), |
5ade1ed2 | 210 | index); |
84a43794 | 211 | |
45309d28 ML |
212 | cp_build_qualified_type (ptr_type_node, |
213 | cp_type_quals (TREE_TYPE (exp))); | |
5d49b6a7 | 214 | return fold_build_pointer_plus (exp, offset); |
84a43794 | 215 | } |
5156628f | 216 | |
5156628f MS |
217 | /* Get a bad_cast node for the program to throw... |
218 | ||
219 | See libstdc++/exception.cc for __throw_bad_cast */ | |
220 | ||
221 | static tree | |
848eed92 | 222 | throw_bad_cast (void) |
5156628f | 223 | { |
4b4b2e58 NS |
224 | static tree fn; |
225 | if (!fn) | |
226 | { | |
227 | tree name = get_identifier ("__cxa_bad_cast"); | |
87e3d7cf | 228 | fn = get_global_binding (name); |
4b4b2e58 NS |
229 | if (!fn) |
230 | fn = push_throw_library_fn | |
231 | (name, build_function_type_list (ptr_type_node, NULL_TREE)); | |
232 | } | |
c8094d83 | 233 | |
e2e03032 | 234 | return build_cxx_call (fn, 0, NULL, tf_warning_or_error); |
5156628f MS |
235 | } |
236 | ||
2050a1bb MM |
237 | /* Return an expression for "__cxa_bad_typeid()". The expression |
238 | returned is an lvalue of type "const std::type_info". */ | |
239 | ||
5156628f | 240 | static tree |
848eed92 | 241 | throw_bad_typeid (void) |
5156628f | 242 | { |
4b4b2e58 NS |
243 | static tree fn; |
244 | if (!fn) | |
cf74fb86 | 245 | { |
4b4b2e58 | 246 | tree name = get_identifier ("__cxa_bad_typeid"); |
87e3d7cf | 247 | fn = get_global_binding (name); |
4b4b2e58 NS |
248 | if (!fn) |
249 | { | |
250 | tree t = build_reference_type (const_type_info_type_node); | |
251 | t = build_function_type_list (t, NULL_TREE); | |
252 | fn = push_throw_library_fn (name, t); | |
253 | } | |
cf74fb86 | 254 | } |
059fa5e7 | 255 | |
e2e03032 | 256 | return build_cxx_call (fn, 0, NULL, tf_warning_or_error); |
5156628f | 257 | } |
84a43794 | 258 | \f |
2050a1bb MM |
259 | /* Return an lvalue expression whose type is "const std::type_info" |
260 | and whose value indicates the type of the expression EXP. If EXP | |
261 | is a reference to a polymorphic class, return the dynamic type; | |
5156628f | 262 | otherwise return the static type of the expression. */ |
e92cc029 | 263 | |
e5f614d7 | 264 | static tree |
795801d6 | 265 | get_tinfo_decl_dynamic (tree exp, tsubst_flags_t complain) |
84a43794 MS |
266 | { |
267 | tree type; | |
2050a1bb | 268 | tree t; |
c8094d83 | 269 | |
717f8053 | 270 | if (error_operand_p (exp)) |
84a43794 MS |
271 | return error_mark_node; |
272 | ||
46f0d909 | 273 | exp = resolve_nondeduced_context (exp, complain); |
6780e186 | 274 | |
debf1662 | 275 | /* Peel back references, so they match. */ |
f7904392 | 276 | type = non_reference (unlowered_expr_type (exp)); |
84a43794 | 277 | |
e92cc029 | 278 | /* Peel off cv qualifiers. */ |
debf1662 | 279 | type = cv_unqualified (type); |
c8094d83 | 280 | |
41b059f3 | 281 | /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */ |
fbfc8363 JM |
282 | if (CLASS_TYPE_P (type) || type == unknown_type_node |
283 | || type == init_list_type_node) | |
795801d6 | 284 | type = complete_type_or_maybe_complain (type, exp, complain); |
c8094d83 | 285 | |
e5f614d7 NS |
286 | if (!type) |
287 | return error_mark_node; | |
6a8f78d5 | 288 | |
84a43794 | 289 | /* If exp is a reference to polymorphic type, get the real type_info. */ |
4c6b7393 | 290 | if (TYPE_POLYMORPHIC_P (type) && ! resolves_to_fixed_type_p (exp, 0)) |
84a43794 MS |
291 | { |
292 | /* build reference to type_info from vtable. */ | |
da3d4dfa | 293 | tree index; |
84a43794 | 294 | |
8fa33dfa | 295 | /* The RTTI information is at index -1. */ |
4a90aeeb | 296 | index = build_int_cst (NULL_TREE, |
7d60be94 | 297 | -1 * TARGET_VTABLE_DATA_ENTRY_DISTANCE); |
b5d1cbd2 | 298 | t = build_vtbl_ref (exp, index); |
6de9cd9a | 299 | t = convert (type_info_ptr_type, t); |
84a43794 | 300 | } |
2050a1bb MM |
301 | else |
302 | /* Otherwise return the type_info for the static type of the expr. */ | |
debf1662 | 303 | t = get_tinfo_ptr (type); |
84a43794 | 304 | |
04757a2a | 305 | return cp_build_fold_indirect_ref (t); |
5156628f MS |
306 | } |
307 | ||
7f54a851 | 308 | static bool |
848eed92 | 309 | typeid_ok_p (void) |
5156628f | 310 | { |
f915e937 NS |
311 | if (! flag_rtti) |
312 | { | |
a3f9f006 | 313 | error ("cannot use %<typeid%> with %<-fno-rtti%>"); |
7f54a851 | 314 | return false; |
f915e937 | 315 | } |
c8094d83 | 316 | |
db24eb1f | 317 | if (!COMPLETE_TYPE_P (const_type_info_type_node)) |
6a8f78d5 | 318 | { |
eea77d1f | 319 | gcc_rich_location richloc (input_location); |
85204e23 | 320 | maybe_add_include_fixit (&richloc, "<typeinfo>", false); |
64a5912c DM |
321 | error_at (&richloc, |
322 | "must %<#include <typeinfo>%> before using" | |
323 | " %<typeid%>"); | |
eea77d1f | 324 | |
7f54a851 | 325 | return false; |
6a8f78d5 | 326 | } |
c8094d83 | 327 | |
ac9a1c7e NS |
328 | tree pseudo = TYPE_MAIN_VARIANT (get_tinfo_desc (TK_TYPE_INFO_TYPE)->type); |
329 | tree real = TYPE_MAIN_VARIANT (const_type_info_type_node); | |
3a44f395 JJ |
330 | |
331 | /* Make sure abi::__type_info_pseudo has the same alias set | |
332 | as std::type_info. */ | |
ac9a1c7e NS |
333 | if (! TYPE_ALIAS_SET_KNOWN_P (pseudo)) |
334 | TYPE_ALIAS_SET (pseudo) = get_alias_set (real); | |
3a44f395 | 335 | else |
ac9a1c7e | 336 | gcc_assert (TYPE_ALIAS_SET (pseudo) == get_alias_set (real)); |
3a44f395 | 337 | |
7f54a851 NS |
338 | return true; |
339 | } | |
340 | ||
2050a1bb MM |
341 | /* Return an expression for "typeid(EXP)". The expression returned is |
342 | an lvalue of type "const std::type_info". */ | |
343 | ||
7f54a851 | 344 | tree |
795801d6 | 345 | build_typeid (tree exp, tsubst_flags_t complain) |
7f54a851 | 346 | { |
03a904b5 | 347 | tree cond = NULL_TREE, initial_expr = exp; |
7f54a851 NS |
348 | int nonnull = 0; |
349 | ||
350 | if (exp == error_mark_node || !typeid_ok_p ()) | |
351 | return error_mark_node; | |
352 | ||
5156628f | 353 | if (processing_template_decl) |
db24eb1f | 354 | return build_min (TYPEID_EXPR, const_type_info_type_node, exp); |
5156628f | 355 | |
c87e82a6 | 356 | if (TYPE_POLYMORPHIC_P (TREE_TYPE (exp)) |
5156628f MS |
357 | && ! resolves_to_fixed_type_p (exp, &nonnull) |
358 | && ! nonnull) | |
359 | { | |
03a904b5 | 360 | /* So we need to look into the vtable of the type of exp. |
c87e82a6 JM |
361 | Make sure it isn't a null lvalue. */ |
362 | exp = cp_build_addr_expr (exp, complain); | |
6ef15591 | 363 | exp = save_expr (exp); |
c87e82a6 | 364 | cond = cp_convert (boolean_type_node, exp, complain); |
04757a2a | 365 | exp = cp_build_fold_indirect_ref (exp); |
5156628f MS |
366 | } |
367 | ||
795801d6 | 368 | exp = get_tinfo_decl_dynamic (exp, complain); |
9a3b49ac MS |
369 | |
370 | if (exp == error_mark_node) | |
371 | return error_mark_node; | |
372 | ||
5156628f MS |
373 | if (cond) |
374 | { | |
375 | tree bad = throw_bad_typeid (); | |
376 | ||
f293ce4b | 377 | exp = build3 (COND_EXPR, TREE_TYPE (exp), cond, exp, bad); |
5156628f | 378 | } |
03a904b5 JJ |
379 | else |
380 | mark_type_use (initial_expr); | |
5156628f | 381 | |
2050a1bb | 382 | return exp; |
84a43794 MS |
383 | } |
384 | ||
52669d59 JQ |
385 | /* Generate the NTBS name of a type. If MARK_PRIVATE, put a '*' in front so that |
386 | comparisons will be done by pointer rather than string comparison. */ | |
db1147b2 | 387 | static tree |
52669d59 | 388 | tinfo_name (tree type, bool mark_private) |
db1147b2 | 389 | { |
1f6e1acc | 390 | const char *name; |
52669d59 | 391 | int length; |
1f6e1acc AS |
392 | tree name_string; |
393 | ||
52669d59 JQ |
394 | name = mangle_type_string (type); |
395 | length = strlen (name); | |
396 | ||
397 | if (mark_private) | |
398 | { | |
399 | /* Inject '*' at beginning of name to force pointer comparison. */ | |
c86f25e8 | 400 | char* buf = (char*) XALLOCAVEC (char, length + 2); |
52669d59 | 401 | buf[0] = '*'; |
c86f25e8 JM |
402 | memcpy (buf + 1, name, length + 1); |
403 | name_string = build_string (length + 2, buf); | |
52669d59 JQ |
404 | } |
405 | else | |
406 | name_string = build_string (length + 1, name); | |
407 | ||
408 | return fix_string_type (name_string); | |
db1147b2 NS |
409 | } |
410 | ||
d689a8f1 NS |
411 | /* Return a VAR_DECL for the internal ABI defined type_info object for |
412 | TYPE. You must arrange that the decl is mark_used, if actually use | |
c8094d83 | 413 | it --- decls in vtables are only used if the vtable is output. */ |
794d4a61 | 414 | |
6b5fbb55 | 415 | tree |
848eed92 | 416 | get_tinfo_decl (tree type) |
6b5fbb55 | 417 | { |
9a3b49ac | 418 | tree name; |
6b5fbb55 MS |
419 | tree d; |
420 | ||
f19218e5 | 421 | if (variably_modified_type_p (type, /*fn=*/NULL_TREE)) |
0c918ce5 | 422 | { |
94a073b2 JM |
423 | error ("cannot create type information for type %qT because " |
424 | "it involves types of variable size", | |
425 | type); | |
0c918ce5 MM |
426 | return error_mark_node; |
427 | } | |
428 | ||
9a3b49ac MS |
429 | if (TREE_CODE (type) == METHOD_TYPE) |
430 | type = build_function_type (TREE_TYPE (type), | |
431 | TREE_CHAIN (TYPE_ARG_TYPES (type))); | |
432 | ||
17d208b5 JM |
433 | type = complete_type (type); |
434 | ||
a82d6da5 MM |
435 | /* For a class type, the variable is cached in the type node |
436 | itself. */ | |
437 | if (CLASS_TYPE_P (type)) | |
438 | { | |
439 | d = CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)); | |
440 | if (d) | |
441 | return d; | |
442 | } | |
c8094d83 | 443 | |
1f84ec23 | 444 | name = mangle_typeinfo_for_type (type); |
669ec2b4 | 445 | |
87e3d7cf | 446 | d = get_global_binding (name); |
d689a8f1 | 447 | if (!d) |
7267d692 | 448 | { |
fe93f42f | 449 | int ix = get_pseudo_ti_index (type); |
ac9a1c7e NS |
450 | const tinfo_s *ti = get_tinfo_desc (ix); |
451 | ||
1bf5fc34 | 452 | d = build_lang_decl (VAR_DECL, name, ti->type); |
4684cd27 | 453 | SET_DECL_ASSEMBLER_NAME (d, name); |
73a8adb6 MM |
454 | /* Remember the type it is for. */ |
455 | TREE_TYPE (name) = type; | |
4684cd27 | 456 | DECL_TINFO_P (d) = 1; |
7267d692 | 457 | DECL_ARTIFICIAL (d) = 1; |
78e0d62b | 458 | DECL_IGNORED_P (d) = 1; |
7267d692 NS |
459 | TREE_READONLY (d) = 1; |
460 | TREE_STATIC (d) = 1; | |
4684cd27 MM |
461 | /* Mark the variable as undefined -- but remember that we can |
462 | define it later if we need to do so. */ | |
7267d692 | 463 | DECL_EXTERNAL (d) = 1; |
4684cd27 | 464 | DECL_NOT_REALLY_EXTERN (d) = 1; |
d71176b6 GK |
465 | set_linkage_according_to_type (type, d); |
466 | ||
2e3757e7 | 467 | d = pushdecl_top_level_and_finish (d, NULL_TREE); |
73a8adb6 MM |
468 | if (CLASS_TYPE_P (type)) |
469 | CLASSTYPE_TYPEINFO_VAR (TYPE_MAIN_VARIANT (type)) = d; | |
d689a8f1 | 470 | |
9bcb9aae | 471 | /* Add decl to the global array of tinfo decls. */ |
9771b263 | 472 | vec_safe_push (unemitted_tinfo_decls, d); |
7267d692 | 473 | } |
d689a8f1 | 474 | |
6b5fbb55 MS |
475 | return d; |
476 | } | |
477 | ||
d689a8f1 NS |
478 | /* Return a pointer to a type_info object describing TYPE, suitably |
479 | cast to the language defined type. */ | |
480 | ||
481 | static tree | |
848eed92 | 482 | get_tinfo_ptr (tree type) |
d689a8f1 | 483 | { |
bb4f6e6b JH |
484 | tree decl = get_tinfo_decl (type); |
485 | ||
486 | mark_used (decl); | |
c8094d83 | 487 | return build_nop (type_info_ptr_type, |
bb4f6e6b | 488 | build_address (decl)); |
d689a8f1 NS |
489 | } |
490 | ||
e5f614d7 | 491 | /* Return the type_info object for TYPE. */ |
e92cc029 | 492 | |
84a43794 | 493 | tree |
795801d6 | 494 | get_typeid (tree type, tsubst_flags_t complain) |
84a43794 | 495 | { |
7f54a851 | 496 | if (type == error_mark_node || !typeid_ok_p ()) |
84a43794 | 497 | return error_mark_node; |
c8094d83 | 498 | |
e9f32eb5 | 499 | if (processing_template_decl) |
db24eb1f | 500 | return build_min (TYPEID_EXPR, const_type_info_type_node, type); |
e9f32eb5 | 501 | |
6b5fbb55 MS |
502 | /* If the type of the type-id is a reference type, the result of the |
503 | typeid expression refers to a type_info object representing the | |
504 | referenced type. */ | |
ee76b931 | 505 | type = non_reference (type); |
84a43794 | 506 | |
80f7a782 JM |
507 | /* This is not one of the uses of a qualified function type in 8.3.5. */ |
508 | if (TREE_CODE (type) == FUNCTION_TYPE | |
509 | && (type_memfn_quals (type) != TYPE_UNQUALIFIED | |
510 | || type_memfn_rqual (type) != REF_QUAL_NONE)) | |
511 | { | |
512 | if (complain & tf_error) | |
a9c697b8 | 513 | error ("%<typeid%> of qualified function type %qT", type); |
80f7a782 JM |
514 | return error_mark_node; |
515 | } | |
516 | ||
6b5fbb55 | 517 | /* The top-level cv-qualifiers of the lvalue expression or the type-id |
e92cc029 | 518 | that is the operand of typeid are always ignored. */ |
debf1662 | 519 | type = cv_unqualified (type); |
84a43794 | 520 | |
41b059f3 | 521 | /* For UNKNOWN_TYPEs call complete_type_or_else to get diagnostics. */ |
fbfc8363 JM |
522 | if (CLASS_TYPE_P (type) || type == unknown_type_node |
523 | || type == init_list_type_node) | |
795801d6 | 524 | type = complete_type_or_maybe_complain (type, NULL_TREE, complain); |
c8094d83 | 525 | |
e5f614d7 NS |
526 | if (!type) |
527 | return error_mark_node; | |
6a8f78d5 | 528 | |
04757a2a | 529 | return cp_build_fold_indirect_ref (get_tinfo_ptr (type)); |
84a43794 MS |
530 | } |
531 | ||
a80e4195 MS |
532 | /* Check whether TEST is null before returning RESULT. If TEST is used in |
533 | RESULT, it must have previously had a save_expr applied to it. */ | |
534 | ||
824b9a4c | 535 | static tree |
4b978f96 | 536 | ifnonnull (tree test, tree result, tsubst_flags_t complain) |
a80e4195 | 537 | { |
7a965d51 JJ |
538 | tree cond = build2 (NE_EXPR, boolean_type_node, test, |
539 | cp_convert (TREE_TYPE (test), nullptr_node, complain)); | |
540 | /* This is a compiler generated comparison, don't emit | |
541 | e.g. -Wnonnull-compare warning for it. */ | |
542 | TREE_NO_WARNING (cond) = 1; | |
543 | return build3 (COND_EXPR, TREE_TYPE (result), cond, result, | |
544 | cp_convert (TREE_TYPE (result), nullptr_node, complain)); | |
a80e4195 MS |
545 | } |
546 | ||
84a43794 MS |
547 | /* Execute a dynamic cast, as described in section 5.2.6 of the 9/93 working |
548 | paper. */ | |
6b5fbb55 | 549 | |
42976354 | 550 | static tree |
ca6932ad PC |
551 | build_dynamic_cast_1 (location_t loc, tree type, tree expr, |
552 | tsubst_flags_t complain) | |
84a43794 MS |
553 | { |
554 | enum tree_code tc = TREE_CODE (type); | |
63891ba2 | 555 | tree exprtype; |
6b5fbb55 | 556 | tree dcast_fn; |
6a8f78d5 | 557 | tree old_expr = expr; |
c2e407f1 | 558 | const char *errstr = NULL; |
84a43794 | 559 | |
8d8d1a28 AH |
560 | /* Save casted types in the function's used types hash table. */ |
561 | used_types_insert (type); | |
562 | ||
64b83f2a SG |
563 | /* T shall be a pointer or reference to a complete class type, or |
564 | `pointer to cv void''. */ | |
84a43794 MS |
565 | switch (tc) |
566 | { | |
567 | case POINTER_TYPE: | |
50e10fa8 | 568 | if (VOID_TYPE_P (TREE_TYPE (type))) |
84a43794 | 569 | break; |
db24eb1f | 570 | /* Fall through. */ |
84a43794 | 571 | case REFERENCE_TYPE: |
9e1e64ec | 572 | if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (type))) |
64b83f2a | 573 | { |
4cd5a50a | 574 | errstr = _("target is not pointer or reference to class"); |
64b83f2a SG |
575 | goto fail; |
576 | } | |
d0f062fb | 577 | if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) |
64b83f2a | 578 | { |
4cd5a50a | 579 | errstr = _("target is not pointer or reference to complete type"); |
64b83f2a SG |
580 | goto fail; |
581 | } | |
cdf5b885 | 582 | break; |
64b83f2a | 583 | |
84a43794 | 584 | default: |
4cd5a50a | 585 | errstr = _("target is not pointer or reference"); |
84a43794 MS |
586 | goto fail; |
587 | } | |
588 | ||
64b83f2a | 589 | if (tc == POINTER_TYPE) |
84a43794 | 590 | { |
89fcabaf | 591 | expr = decay_conversion (expr, complain); |
63891ba2 PC |
592 | exprtype = TREE_TYPE (expr); |
593 | ||
64b83f2a SG |
594 | /* If T is a pointer type, v shall be an rvalue of a pointer to |
595 | complete class type, and the result is an rvalue of type T. */ | |
596 | ||
03a904b5 JJ |
597 | expr = mark_rvalue_use (expr); |
598 | ||
50e10fa8 | 599 | if (!TYPE_PTR_P (exprtype)) |
64b83f2a | 600 | { |
4cd5a50a | 601 | errstr = _("source is not a pointer"); |
64b83f2a SG |
602 | goto fail; |
603 | } | |
9e1e64ec | 604 | if (! MAYBE_CLASS_TYPE_P (TREE_TYPE (exprtype))) |
64b83f2a | 605 | { |
4cd5a50a | 606 | errstr = _("source is not a pointer to class"); |
64b83f2a SG |
607 | goto fail; |
608 | } | |
d0f062fb | 609 | if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (exprtype)))) |
64b83f2a | 610 | { |
4cd5a50a | 611 | errstr = _("source is a pointer to incomplete type"); |
64b83f2a SG |
612 | goto fail; |
613 | } | |
614 | } | |
615 | else | |
616 | { | |
03a904b5 | 617 | expr = mark_lvalue_use (expr); |
a347241b | 618 | exprtype = TREE_TYPE (expr); |
db24eb1f | 619 | |
64b83f2a SG |
620 | /* T is a reference type, v shall be an lvalue of a complete class |
621 | type, and the result is an lvalue of the type referred to by T. */ | |
a347241b | 622 | if (! MAYBE_CLASS_TYPE_P (exprtype)) |
64b83f2a | 623 | { |
4cd5a50a | 624 | errstr = _("source is not of class type"); |
64b83f2a SG |
625 | goto fail; |
626 | } | |
a347241b | 627 | if (!COMPLETE_TYPE_P (complete_type (exprtype))) |
64b83f2a | 628 | { |
4cd5a50a | 629 | errstr = _("source is of incomplete class type"); |
64b83f2a SG |
630 | goto fail; |
631 | } | |
a347241b JM |
632 | |
633 | exprtype = cp_build_reference_type (exprtype, !lvalue_p (expr)); | |
64b83f2a SG |
634 | } |
635 | ||
636 | /* The dynamic_cast operator shall not cast away constness. */ | |
637 | if (!at_least_as_qualified_p (TREE_TYPE (type), | |
638 | TREE_TYPE (exprtype))) | |
639 | { | |
4cd5a50a | 640 | errstr = _("conversion casts away constness"); |
64b83f2a | 641 | goto fail; |
84a43794 MS |
642 | } |
643 | ||
644 | /* If *type is an unambiguous accessible base class of *exprtype, | |
645 | convert statically. */ | |
646 | { | |
ffe7516f PC |
647 | tree binfo = lookup_base (TREE_TYPE (exprtype), TREE_TYPE (type), |
648 | ba_check, NULL, complain); | |
338d90b8 | 649 | if (binfo) |
ca6932ad | 650 | return build_static_cast (loc, type, expr, complain); |
84a43794 MS |
651 | } |
652 | ||
d1522e8f JM |
653 | /* Apply trivial conversion T -> T& for dereferenced ptrs. */ |
654 | if (tc == REFERENCE_TYPE) | |
655 | expr = convert_to_reference (exprtype, expr, CONV_IMPLICIT, | |
656 | LOOKUP_NORMAL, NULL_TREE, complain); | |
657 | ||
84a43794 | 658 | /* Otherwise *exprtype must be a polymorphic class (have a vtbl). */ |
4c6b7393 | 659 | if (TYPE_POLYMORPHIC_P (TREE_TYPE (exprtype))) |
84a43794 | 660 | { |
a80e4195 | 661 | tree expr1; |
84a43794 | 662 | /* if TYPE is `void *', return pointer to complete object. */ |
b72801e2 | 663 | if (tc == POINTER_TYPE && VOID_TYPE_P (TREE_TYPE (type))) |
84a43794 MS |
664 | { |
665 | /* if b is an object, dynamic_cast<void *>(&b) == (void *)&b. */ | |
666 | if (TREE_CODE (expr) == ADDR_EXPR | |
5a6ccc94 | 667 | && VAR_P (TREE_OPERAND (expr, 0)) |
84a43794 MS |
668 | && TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == RECORD_TYPE) |
669 | return build1 (NOP_EXPR, type, expr); | |
670 | ||
a80e4195 MS |
671 | /* Since expr is used twice below, save it. */ |
672 | expr = save_expr (expr); | |
673 | ||
674 | expr1 = build_headof (expr); | |
675 | if (TREE_TYPE (expr1) != type) | |
676 | expr1 = build1 (NOP_EXPR, type, expr1); | |
4b978f96 | 677 | return ifnonnull (expr, expr1, complain); |
84a43794 MS |
678 | } |
679 | else | |
680 | { | |
681 | tree retval; | |
94a0dd7b SL |
682 | tree result, td2, td3; |
683 | tree elems[4]; | |
0cbd7506 | 684 | tree static_type, target_type, boff; |
84a43794 | 685 | |
0cbd7506 | 686 | /* If we got here, we can't convert statically. Therefore, |
84a43794 | 687 | dynamic_cast<D&>(b) (b an object) cannot succeed. */ |
64b83f2a | 688 | if (tc == REFERENCE_TYPE) |
84a43794 | 689 | { |
5a6ccc94 | 690 | if (VAR_P (old_expr) |
6a8f78d5 | 691 | && TREE_CODE (TREE_TYPE (old_expr)) == RECORD_TYPE) |
84a43794 | 692 | { |
0cbd7506 | 693 | tree expr = throw_bad_cast (); |
5ade1ed2 | 694 | if (complain & tf_warning) |
ca6932ad PC |
695 | warning_at (loc, 0, |
696 | "%<dynamic_cast<%#T>(%#D)%> can never succeed", | |
697 | type, old_expr); | |
0cbd7506 MS |
698 | /* Bash it to the expected type. */ |
699 | TREE_TYPE (expr) = type; | |
059fa5e7 | 700 | return expr; |
84a43794 MS |
701 | } |
702 | } | |
703 | /* Ditto for dynamic_cast<D*>(&b). */ | |
704 | else if (TREE_CODE (expr) == ADDR_EXPR) | |
705 | { | |
706 | tree op = TREE_OPERAND (expr, 0); | |
5a6ccc94 | 707 | if (VAR_P (op) |
84a43794 MS |
708 | && TREE_CODE (TREE_TYPE (op)) == RECORD_TYPE) |
709 | { | |
5ade1ed2 | 710 | if (complain & tf_warning) |
ca6932ad PC |
711 | warning_at (loc, 0, |
712 | "%<dynamic_cast<%#T>(%#D)%> can never succeed", | |
713 | type, op); | |
c8094d83 | 714 | retval = build_int_cst (type, 0); |
84a43794 MS |
715 | return retval; |
716 | } | |
717 | } | |
718 | ||
6c9384c3 BS |
719 | /* Use of dynamic_cast when -fno-rtti is prohibited. */ |
720 | if (!flag_rtti) | |
721 | { | |
5ade1ed2 | 722 | if (complain & tf_error) |
ca6932ad PC |
723 | error_at (loc, |
724 | "%<dynamic_cast%> not permitted with %<-fno-rtti%>"); | |
6c9384c3 BS |
725 | return error_mark_node; |
726 | } | |
727 | ||
7267d692 NS |
728 | target_type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
729 | static_type = TYPE_MAIN_VARIANT (TREE_TYPE (exprtype)); | |
bb4f6e6b | 730 | td2 = get_tinfo_decl (target_type); |
9f635aba PC |
731 | if (!mark_used (td2, complain) && !(complain & tf_error)) |
732 | return error_mark_node; | |
93c0e0bb | 733 | td2 = cp_build_addr_expr (td2, complain); |
bb4f6e6b | 734 | td3 = get_tinfo_decl (static_type); |
9f635aba PC |
735 | if (!mark_used (td3, complain) && !(complain & tf_error)) |
736 | return error_mark_node; | |
93c0e0bb | 737 | td3 = cp_build_addr_expr (td3, complain); |
7267d692 | 738 | |
0cbd7506 MS |
739 | /* Determine how T and V are related. */ |
740 | boff = dcast_base_hint (static_type, target_type); | |
c8094d83 | 741 | |
6b5fbb55 MS |
742 | /* Since expr is used twice below, save it. */ |
743 | expr = save_expr (expr); | |
744 | ||
84a43794 MS |
745 | expr1 = expr; |
746 | if (tc == REFERENCE_TYPE) | |
93c0e0bb | 747 | expr1 = cp_build_addr_expr (expr1, complain); |
84a43794 | 748 | |
94a0dd7b SL |
749 | elems[0] = expr1; |
750 | elems[1] = td3; | |
751 | elems[2] = td2; | |
752 | elems[3] = boff; | |
6b5fbb55 | 753 | |
059fa5e7 NS |
754 | dcast_fn = dynamic_cast_node; |
755 | if (!dcast_fn) | |
6b5fbb55 MS |
756 | { |
757 | tree tmp; | |
7267d692 | 758 | tree tinfo_ptr; |
7267d692 | 759 | const char *name; |
c8094d83 | 760 | |
a15f0fd0 | 761 | push_abi_namespace (); |
88e5899c | 762 | tinfo_ptr = xref_tag (class_type, |
8fa33dfa | 763 | get_identifier ("__class_type_info"), |
bd3d082e | 764 | /*tag_scope=*/ts_current, false); |
c8094d83 | 765 | |
8fa33dfa | 766 | tinfo_ptr = build_pointer_type |
a3360e77 | 767 | (cp_build_qualified_type |
8fa33dfa MM |
768 | (tinfo_ptr, TYPE_QUAL_CONST)); |
769 | name = "__dynamic_cast"; | |
0244e6f7 NF |
770 | tmp = build_function_type_list (ptr_type_node, |
771 | const_ptr_type_node, | |
772 | tinfo_ptr, tinfo_ptr, | |
773 | ptrdiff_type_node, NULL_TREE); | |
fafa0521 JH |
774 | dcast_fn = build_library_fn_ptr (name, tmp, |
775 | ECF_LEAF | ECF_PURE | ECF_NOTHROW); | |
a15f0fd0 | 776 | pop_abi_namespace (); |
0cbd7506 | 777 | dynamic_cast_node = dcast_fn; |
6b5fbb55 | 778 | } |
e2e03032 | 779 | result = build_cxx_call (dcast_fn, 4, elems, complain); |
22edf943 | 780 | SET_EXPR_LOCATION (result, loc); |
84a43794 MS |
781 | |
782 | if (tc == REFERENCE_TYPE) | |
783 | { | |
7267d692 | 784 | tree bad = throw_bad_cast (); |
a0c04f8e | 785 | tree neq; |
c8094d83 | 786 | |
84a43794 | 787 | result = save_expr (result); |
2ab340fe | 788 | neq = cp_truthvalue_conversion (result, complain); |
c74147d2 RG |
789 | return cp_convert (type, |
790 | build3 (COND_EXPR, TREE_TYPE (result), | |
4b978f96 | 791 | neq, result, bad), complain); |
84a43794 MS |
792 | } |
793 | ||
e92cc029 | 794 | /* Now back to the type we want from a void*. */ |
4b978f96 PC |
795 | result = cp_convert (type, result, complain); |
796 | return ifnonnull (expr, result, complain); | |
84a43794 MS |
797 | } |
798 | } | |
64b83f2a | 799 | else |
4cd5a50a | 800 | errstr = _("source type is not polymorphic"); |
0f4d5c1c | 801 | |
84a43794 | 802 | fail: |
5ade1ed2 | 803 | if (complain & tf_error) |
ca6932ad PC |
804 | error_at (loc, "cannot %<dynamic_cast%> %qE (of type %q#T) " |
805 | "to type %q#T (%s)", | |
806 | old_expr, TREE_TYPE (old_expr), type, errstr); | |
84a43794 MS |
807 | return error_mark_node; |
808 | } | |
42976354 BK |
809 | |
810 | tree | |
ca6932ad PC |
811 | build_dynamic_cast (location_t loc, tree type, tree expr, |
812 | tsubst_flags_t complain) | |
42976354 | 813 | { |
04398fa8 PC |
814 | tree r; |
815 | ||
328d03d7 JM |
816 | if (type == error_mark_node || expr == error_mark_node) |
817 | return error_mark_node; | |
c8094d83 | 818 | |
328d03d7 | 819 | if (processing_template_decl) |
8e1daa34 NS |
820 | { |
821 | expr = build_min (DYNAMIC_CAST_EXPR, type, expr); | |
822 | TREE_SIDE_EFFECTS (expr) = 1; | |
ca6932ad PC |
823 | r = convert_from_reference (expr); |
824 | protected_set_expr_location (r, loc); | |
825 | return r; | |
8e1daa34 | 826 | } |
328d03d7 | 827 | |
ca6932ad PC |
828 | r = convert_from_reference (build_dynamic_cast_1 (loc, type, expr, |
829 | complain)); | |
04398fa8 | 830 | if (r != error_mark_node) |
ca6932ad PC |
831 | maybe_warn_about_useless_cast (loc, type, expr, complain); |
832 | protected_set_expr_location (r, loc); | |
04398fa8 | 833 | return r; |
42976354 | 834 | } |
04398fa8 | 835 | |
7267d692 NS |
836 | /* Return the runtime bit mask encoding the qualifiers of TYPE. */ |
837 | ||
838 | static int | |
848eed92 | 839 | qualifier_flags (tree type) |
7267d692 NS |
840 | { |
841 | int flags = 0; | |
05abed76 | 842 | int quals = cp_type_quals (type); |
c8094d83 | 843 | |
7267d692 NS |
844 | if (quals & TYPE_QUAL_CONST) |
845 | flags |= 1; | |
846 | if (quals & TYPE_QUAL_VOLATILE) | |
847 | flags |= 2; | |
6d61f400 NS |
848 | if (quals & TYPE_QUAL_RESTRICT) |
849 | flags |= 4; | |
7267d692 NS |
850 | return flags; |
851 | } | |
852 | ||
848eed92 | 853 | /* Return true, if the pointer chain TYPE ends at an incomplete type, or |
0a240972 NS |
854 | contains a pointer to member of an incomplete class. */ |
855 | ||
848eed92 GDR |
856 | static bool |
857 | target_incomplete_p (tree type) | |
0a240972 | 858 | { |
a5ac359a | 859 | while (true) |
66b1156a | 860 | if (TYPE_PTRDATAMEM_P (type)) |
0a240972 | 861 | { |
a5ac359a MM |
862 | if (!COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type))) |
863 | return true; | |
864 | type = TYPE_PTRMEM_POINTED_TO_TYPE (type); | |
0a240972 | 865 | } |
50e10fa8 | 866 | else if (TYPE_PTR_P (type)) |
0a240972 | 867 | type = TREE_TYPE (type); |
a5ac359a MM |
868 | else |
869 | return !COMPLETE_OR_VOID_TYPE_P (type); | |
0a240972 NS |
870 | } |
871 | ||
4684cd27 MM |
872 | /* Returns true if TYPE involves an incomplete class type; in that |
873 | case, typeinfo variables for TYPE should be emitted with internal | |
874 | linkage. */ | |
875 | ||
876 | static bool | |
877 | involves_incomplete_p (tree type) | |
878 | { | |
879 | switch (TREE_CODE (type)) | |
880 | { | |
881 | case POINTER_TYPE: | |
882 | return target_incomplete_p (TREE_TYPE (type)); | |
883 | ||
884 | case OFFSET_TYPE: | |
885 | ptrmem: | |
c8094d83 | 886 | return |
4684cd27 MM |
887 | (target_incomplete_p (TYPE_PTRMEM_POINTED_TO_TYPE (type)) |
888 | || !COMPLETE_TYPE_P (TYPE_PTRMEM_CLASS_TYPE (type))); | |
889 | ||
890 | case RECORD_TYPE: | |
891 | if (TYPE_PTRMEMFUNC_P (type)) | |
892 | goto ptrmem; | |
893 | /* Fall through. */ | |
894 | case UNION_TYPE: | |
895 | if (!COMPLETE_TYPE_P (type)) | |
896 | return true; | |
0b4b9552 | 897 | /* Fall through. */ |
4684cd27 MM |
898 | default: |
899 | /* All other types do not involve incomplete class types. */ | |
900 | return false; | |
901 | } | |
902 | } | |
903 | ||
7267d692 | 904 | /* Return a CONSTRUCTOR for the common part of the type_info objects. This |
0a240972 NS |
905 | is the vtable pointer and NTBS name. The NTBS name is emitted as a |
906 | comdat const char array, so it becomes a unique key for the type. Generate | |
907 | and emit that VAR_DECL here. (We can't always emit the type_info itself | |
908 | as comdat, because of pointers to incomplete.) */ | |
7267d692 NS |
909 | |
910 | static tree | |
1bf5fc34 | 911 | tinfo_base_init (tinfo_s *ti, tree target) |
7267d692 | 912 | { |
e20463aa | 913 | tree init; |
0a240972 | 914 | tree name_decl; |
d689a8f1 | 915 | tree vtable_ptr; |
9771b263 | 916 | vec<constructor_elt, va_gc> *v; |
c8094d83 | 917 | |
0a240972 | 918 | { |
52669d59 | 919 | tree name_name, name_string; |
c8094d83 | 920 | |
0a240972 | 921 | /* Generate the NTBS array variable. */ |
0a240972 | 922 | tree name_type = build_cplus_array_type |
a3360e77 | 923 | (cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST), |
0cbd7506 | 924 | NULL_TREE); |
1f6e1acc | 925 | |
4684cd27 MM |
926 | /* Determine the name of the variable -- and remember with which |
927 | type it is associated. */ | |
1f84ec23 | 928 | name_name = mangle_typeinfo_string_for_type (target); |
4684cd27 MM |
929 | TREE_TYPE (name_name) = target; |
930 | ||
0a240972 | 931 | name_decl = build_lang_decl (VAR_DECL, name_name, name_type); |
73a8adb6 | 932 | SET_DECL_ASSEMBLER_NAME (name_decl, name_name); |
0a240972 | 933 | DECL_ARTIFICIAL (name_decl) = 1; |
78e0d62b | 934 | DECL_IGNORED_P (name_decl) = 1; |
0a240972 NS |
935 | TREE_READONLY (name_decl) = 1; |
936 | TREE_STATIC (name_decl) = 1; | |
937 | DECL_EXTERNAL (name_decl) = 0; | |
4684cd27 | 938 | DECL_TINFO_P (name_decl) = 1; |
6e4be1fa | 939 | set_linkage_according_to_type (target, name_decl); |
4684cd27 | 940 | import_export_decl (name_decl); |
52669d59 | 941 | name_string = tinfo_name (target, !TREE_PUBLIC (name_decl)); |
0a240972 | 942 | DECL_INITIAL (name_decl) = name_string; |
bb4f6e6b | 943 | mark_used (name_decl); |
2e3757e7 | 944 | pushdecl_top_level_and_finish (name_decl, name_string); |
0a240972 | 945 | } |
d689a8f1 | 946 | |
1bf5fc34 | 947 | vtable_ptr = ti->vtable; |
d689a8f1 | 948 | if (!vtable_ptr) |
7267d692 | 949 | { |
d689a8f1 | 950 | tree real_type; |
a15f0fd0 | 951 | push_abi_namespace (); |
1bf5fc34 | 952 | real_type = xref_tag (class_type, ti->name, |
bd3d082e | 953 | /*tag_scope=*/ts_current, false); |
a15f0fd0 | 954 | pop_abi_namespace (); |
c8094d83 | 955 | |
d689a8f1 NS |
956 | if (!COMPLETE_TYPE_P (real_type)) |
957 | { | |
0cbd7506 | 958 | /* We never saw a definition of this type, so we need to |
d689a8f1 NS |
959 | tell the compiler that this is an exported class, as |
960 | indeed all of the __*_type_info classes are. */ | |
961 | SET_CLASSTYPE_INTERFACE_KNOWN (real_type); | |
962 | CLASSTYPE_INTERFACE_ONLY (real_type) = 1; | |
963 | } | |
964 | ||
965 | vtable_ptr = get_vtable_decl (real_type, /*complete=*/1); | |
93c0e0bb | 966 | vtable_ptr = cp_build_addr_expr (vtable_ptr, tf_warning_or_error); |
d689a8f1 NS |
967 | |
968 | /* We need to point into the middle of the vtable. */ | |
5d49b6a7 RG |
969 | vtable_ptr = fold_build_pointer_plus |
970 | (vtable_ptr, | |
d689a8f1 NS |
971 | size_binop (MULT_EXPR, |
972 | size_int (2 * TARGET_VTABLE_DATA_ENTRY_DISTANCE), | |
973 | TYPE_SIZE_UNIT (vtable_entry_type))); | |
3db45ab5 | 974 | |
1bf5fc34 | 975 | ti->vtable = vtable_ptr; |
7267d692 | 976 | } |
d689a8f1 | 977 | |
9771b263 | 978 | vec_alloc (v, 2); |
e20463aa | 979 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, vtable_ptr); |
89fcabaf PC |
980 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, |
981 | decay_conversion (name_decl, tf_warning_or_error)); | |
c8094d83 | 982 | |
e20463aa | 983 | init = build_constructor (init_list_type_node, v); |
6de9cd9a | 984 | TREE_CONSTANT (init) = 1; |
6de9cd9a | 985 | TREE_STATIC (init) = 1; |
c8094d83 | 986 | |
7267d692 NS |
987 | return init; |
988 | } | |
989 | ||
1bf5fc34 | 990 | /* Return the CONSTRUCTOR expr for a type_info of TYPE. TI provides the |
7267d692 NS |
991 | information about the particular type_info derivation, which adds no |
992 | additional fields to the type_info base. */ | |
993 | ||
994 | static tree | |
1bf5fc34 | 995 | generic_initializer (tinfo_s *ti, tree target) |
7267d692 | 996 | { |
1bf5fc34 | 997 | tree init = tinfo_base_init (ti, target); |
3db45ab5 | 998 | |
e20463aa | 999 | init = build_constructor_single (init_list_type_node, NULL_TREE, init); |
6de9cd9a | 1000 | TREE_CONSTANT (init) = 1; |
6de9cd9a | 1001 | TREE_STATIC (init) = 1; |
7267d692 NS |
1002 | return init; |
1003 | } | |
1004 | ||
1f4cb92b | 1005 | /* Return the CONSTRUCTOR expr for a type_info of pointer TYPE. |
1bf5fc34 | 1006 | TI provides information about the particular type_info derivation, |
7267d692 NS |
1007 | which adds target type and qualifier flags members to the type_info base. */ |
1008 | ||
1009 | static tree | |
1bf5fc34 | 1010 | ptr_initializer (tinfo_s *ti, tree target) |
7267d692 | 1011 | { |
1bf5fc34 | 1012 | tree init = tinfo_base_init (ti, target); |
7267d692 NS |
1013 | tree to = TREE_TYPE (target); |
1014 | int flags = qualifier_flags (to); | |
848eed92 | 1015 | bool incomplete = target_incomplete_p (to); |
9771b263 DN |
1016 | vec<constructor_elt, va_gc> *v; |
1017 | vec_alloc (v, 3); | |
c8094d83 | 1018 | |
0a240972 | 1019 | if (incomplete) |
4684cd27 | 1020 | flags |= 8; |
b8fd7909 JM |
1021 | if (tx_safe_fn_type_p (to)) |
1022 | { | |
1023 | flags |= 0x20; | |
1024 | to = tx_unsafe_fn_variant (to); | |
1025 | } | |
51dc6603 | 1026 | if (flag_noexcept_type |
7bdc7e06 | 1027 | && FUNC_OR_METHOD_TYPE_P (to) |
51dc6603 JM |
1028 | && TYPE_NOTHROW_P (to)) |
1029 | { | |
1030 | flags |= 0x40; | |
1031 | to = build_exception_variant (to, NULL_TREE); | |
1032 | } | |
e20463aa NF |
1033 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); |
1034 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags)); | |
1035 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, | |
1036 | get_tinfo_ptr (TYPE_MAIN_VARIANT (to))); | |
c8094d83 | 1037 | |
e20463aa | 1038 | init = build_constructor (init_list_type_node, v); |
6de9cd9a | 1039 | TREE_CONSTANT (init) = 1; |
6de9cd9a | 1040 | TREE_STATIC (init) = 1; |
7267d692 NS |
1041 | return init; |
1042 | } | |
1043 | ||
1f4cb92b | 1044 | /* Return the CONSTRUCTOR expr for a type_info of pointer to member data TYPE. |
1bf5fc34 | 1045 | TI provides information about the particular type_info derivation, |
0a240972 NS |
1046 | which adds class, target type and qualifier flags members to the type_info |
1047 | base. */ | |
7267d692 NS |
1048 | |
1049 | static tree | |
1bf5fc34 | 1050 | ptm_initializer (tinfo_s *ti, tree target) |
7267d692 | 1051 | { |
1bf5fc34 | 1052 | tree init = tinfo_base_init (ti, target); |
7267d692 NS |
1053 | tree to = TYPE_PTRMEM_POINTED_TO_TYPE (target); |
1054 | tree klass = TYPE_PTRMEM_CLASS_TYPE (target); | |
1055 | int flags = qualifier_flags (to); | |
848eed92 | 1056 | bool incomplete = target_incomplete_p (to); |
9771b263 DN |
1057 | vec<constructor_elt, va_gc> *v; |
1058 | vec_alloc (v, 4); | |
c8094d83 | 1059 | |
0a240972 | 1060 | if (incomplete) |
4684cd27 | 1061 | flags |= 0x8; |
0a240972 | 1062 | if (!COMPLETE_TYPE_P (klass)) |
4684cd27 | 1063 | flags |= 0x10; |
e20463aa NF |
1064 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); |
1065 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, build_int_cst (NULL_TREE, flags)); | |
1066 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, | |
1067 | get_tinfo_ptr (TYPE_MAIN_VARIANT (to))); | |
1068 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, get_tinfo_ptr (klass)); | |
1069 | ||
1070 | init = build_constructor (init_list_type_node, v); | |
6de9cd9a | 1071 | TREE_CONSTANT (init) = 1; |
6de9cd9a | 1072 | TREE_STATIC (init) = 1; |
c8094d83 | 1073 | return init; |
7267d692 NS |
1074 | } |
1075 | ||
7267d692 | 1076 | /* Return the CONSTRUCTOR expr for a type_info of class TYPE. |
1bf5fc34 | 1077 | TI provides information about the particular __class_type_info derivation, |
e20463aa | 1078 | which adds hint flags and N extra initializers to the type_info base. */ |
7267d692 NS |
1079 | |
1080 | static tree | |
e20463aa | 1081 | class_initializer (tinfo_s *ti, tree target, unsigned n, ...) |
7267d692 | 1082 | { |
1bf5fc34 | 1083 | tree init = tinfo_base_init (ti, target); |
e20463aa NF |
1084 | va_list extra_inits; |
1085 | unsigned i; | |
9771b263 DN |
1086 | vec<constructor_elt, va_gc> *v; |
1087 | vec_alloc (v, n+1); | |
e20463aa NF |
1088 | |
1089 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); | |
1090 | va_start (extra_inits, n); | |
1091 | for (i = 0; i < n; i++) | |
1092 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, va_arg (extra_inits, tree)); | |
1093 | va_end (extra_inits); | |
c8094d83 | 1094 | |
e20463aa | 1095 | init = build_constructor (init_list_type_node, v); |
6de9cd9a | 1096 | TREE_CONSTANT (init) = 1; |
6de9cd9a | 1097 | TREE_STATIC (init) = 1; |
c8094d83 | 1098 | return init; |
7267d692 NS |
1099 | } |
1100 | ||
848eed92 | 1101 | /* Returns true if the typeinfo for type should be placed in |
97458258 MM |
1102 | the runtime library. */ |
1103 | ||
848eed92 GDR |
1104 | static bool |
1105 | typeinfo_in_lib_p (tree type) | |
97458258 MM |
1106 | { |
1107 | /* The typeinfo objects for `T*' and `const T*' are in the runtime | |
1108 | library for simple types T. */ | |
50e10fa8 | 1109 | if (TYPE_PTR_P (type) |
89d684bb BM |
1110 | && (cp_type_quals (TREE_TYPE (type)) == TYPE_QUAL_CONST |
1111 | || cp_type_quals (TREE_TYPE (type)) == TYPE_UNQUALIFIED)) | |
97458258 MM |
1112 | type = TREE_TYPE (type); |
1113 | ||
1114 | switch (TREE_CODE (type)) | |
1115 | { | |
1116 | case INTEGER_TYPE: | |
1117 | case BOOLEAN_TYPE: | |
97458258 MM |
1118 | case REAL_TYPE: |
1119 | case VOID_TYPE: | |
1e85e720 | 1120 | case NULLPTR_TYPE: |
848eed92 | 1121 | return true; |
c8094d83 | 1122 | |
5116acc6 | 1123 | case LANG_TYPE: |
1e85e720 | 1124 | /* fall through. */ |
5116acc6 | 1125 | |
97458258 | 1126 | default: |
848eed92 | 1127 | return false; |
97458258 MM |
1128 | } |
1129 | } | |
1130 | ||
1bf5fc34 NS |
1131 | /* Generate the initializer for the type info describing TYPE. TK_INDEX is |
1132 | the index of the descriptor in the tinfo_desc vector. */ | |
7267d692 NS |
1133 | |
1134 | static tree | |
1bf5fc34 | 1135 | get_pseudo_ti_init (tree type, unsigned tk_index) |
7267d692 | 1136 | { |
ac9a1c7e | 1137 | tinfo_s *ti = get_tinfo_desc (tk_index); |
3db45ab5 | 1138 | |
50bc768d | 1139 | gcc_assert (at_eof); |
1bf5fc34 | 1140 | switch (tk_index) |
7267d692 | 1141 | { |
1bf5fc34 NS |
1142 | case TK_POINTER_MEMBER_TYPE: |
1143 | return ptm_initializer (ti, type); | |
3db45ab5 | 1144 | |
1bf5fc34 NS |
1145 | case TK_POINTER_TYPE: |
1146 | return ptr_initializer (ti, type); | |
3db45ab5 | 1147 | |
1bf5fc34 NS |
1148 | case TK_BUILTIN_TYPE: |
1149 | case TK_ENUMERAL_TYPE: | |
1150 | case TK_FUNCTION_TYPE: | |
1151 | case TK_ARRAY_TYPE: | |
1152 | return generic_initializer (ti, type); | |
1153 | ||
1154 | case TK_CLASS_TYPE: | |
e20463aa | 1155 | return class_initializer (ti, type, 0); |
1bf5fc34 NS |
1156 | |
1157 | case TK_SI_CLASS_TYPE: | |
1158 | { | |
1159 | tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), 0); | |
1160 | tree tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo)); | |
96d84476 JJ |
1161 | |
1162 | /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */ | |
9771b263 | 1163 | ti = &(*tinfo_descs)[tk_index]; |
e20463aa | 1164 | return class_initializer (ti, type, 1, tinfo); |
1bf5fc34 | 1165 | } |
97458258 | 1166 | |
7267d692 | 1167 | default: |
1bf5fc34 NS |
1168 | { |
1169 | int hint = ((CLASSTYPE_REPEATED_BASE_P (type) << 0) | |
1170 | | (CLASSTYPE_DIAMOND_SHAPED_P (type) << 1)); | |
1171 | tree binfo = TYPE_BINFO (type); | |
ac9a1c7e | 1172 | unsigned nbases = BINFO_N_BASE_BINFOS (binfo); |
9771b263 | 1173 | vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo); |
1c773c7a | 1174 | tree offset_type = LONGPTR_T; |
9771b263 | 1175 | vec<constructor_elt, va_gc> *init_vec = NULL; |
3db45ab5 | 1176 | |
ac9a1c7e | 1177 | gcc_assert (tk_index - TK_VMI_CLASS_TYPES + 1 == nbases); |
3db45ab5 | 1178 | |
9771b263 | 1179 | vec_safe_grow (init_vec, nbases); |
1bf5fc34 | 1180 | /* Generate the base information initializer. */ |
ac9a1c7e | 1181 | for (unsigned ix = nbases; ix--;) |
1bf5fc34 NS |
1182 | { |
1183 | tree base_binfo = BINFO_BASE_BINFO (binfo, ix); | |
1bf5fc34 NS |
1184 | int flags = 0; |
1185 | tree tinfo; | |
1186 | tree offset; | |
9771b263 | 1187 | vec<constructor_elt, va_gc> *v; |
3db45ab5 | 1188 | |
9771b263 | 1189 | if ((*base_accesses)[ix] == access_public_node) |
1bf5fc34 NS |
1190 | flags |= 2; |
1191 | tinfo = get_tinfo_ptr (BINFO_TYPE (base_binfo)); | |
1192 | if (BINFO_VIRTUAL_P (base_binfo)) | |
1193 | { | |
1194 | /* We store the vtable offset at which the virtual | |
3db45ab5 | 1195 | base offset can be found. */ |
1bf5fc34 | 1196 | offset = BINFO_VPTR_FIELD (base_binfo); |
1bf5fc34 NS |
1197 | flags |= 1; |
1198 | } | |
1199 | else | |
1200 | offset = BINFO_OFFSET (base_binfo); | |
3db45ab5 | 1201 | |
1bf5fc34 | 1202 | /* Combine offset and flags into one field. */ |
07b183fd | 1203 | offset = fold_convert (offset_type, offset); |
db3927fb AH |
1204 | offset = fold_build2_loc (input_location, |
1205 | LSHIFT_EXPR, offset_type, offset, | |
07b183fd | 1206 | build_int_cst (offset_type, 8)); |
db3927fb AH |
1207 | offset = fold_build2_loc (input_location, |
1208 | BIT_IOR_EXPR, offset_type, offset, | |
07b183fd | 1209 | build_int_cst (offset_type, flags)); |
9771b263 | 1210 | vec_alloc (v, 2); |
e20463aa NF |
1211 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, tinfo); |
1212 | CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, offset); | |
ac9a1c7e NS |
1213 | tree base_init = build_constructor (init_list_type_node, v); |
1214 | constructor_elt *e = &(*init_vec)[ix]; | |
e20463aa NF |
1215 | e->index = NULL_TREE; |
1216 | e->value = base_init; | |
1bf5fc34 | 1217 | } |
ac9a1c7e | 1218 | tree base_inits = build_constructor (init_list_type_node, init_vec); |
96d84476 JJ |
1219 | |
1220 | /* get_tinfo_ptr might have reallocated the tinfo_descs vector. */ | |
9771b263 | 1221 | ti = &(*tinfo_descs)[tk_index]; |
e20463aa NF |
1222 | return class_initializer (ti, type, 3, |
1223 | build_int_cst (NULL_TREE, hint), | |
1224 | build_int_cst (NULL_TREE, nbases), | |
1225 | base_inits); | |
1bf5fc34 | 1226 | } |
7267d692 | 1227 | } |
7267d692 NS |
1228 | } |
1229 | ||
1bf5fc34 NS |
1230 | /* Return the index of a pseudo type info type node used to describe |
1231 | TYPE. TYPE must be a complete type (or cv void), except at the end | |
1232 | of the translation unit. */ | |
7267d692 | 1233 | |
1bf5fc34 NS |
1234 | static unsigned |
1235 | get_pseudo_ti_index (tree type) | |
7267d692 | 1236 | { |
1bf5fc34 | 1237 | unsigned ix; |
3db45ab5 | 1238 | |
d689a8f1 | 1239 | switch (TREE_CODE (type)) |
7267d692 | 1240 | { |
a5ac359a | 1241 | case OFFSET_TYPE: |
1bf5fc34 NS |
1242 | ix = TK_POINTER_MEMBER_TYPE; |
1243 | break; | |
3db45ab5 | 1244 | |
d689a8f1 | 1245 | case POINTER_TYPE: |
1bf5fc34 NS |
1246 | ix = TK_POINTER_TYPE; |
1247 | break; | |
3db45ab5 | 1248 | |
d689a8f1 | 1249 | case ENUMERAL_TYPE: |
1bf5fc34 NS |
1250 | ix = TK_ENUMERAL_TYPE; |
1251 | break; | |
3db45ab5 | 1252 | |
d689a8f1 | 1253 | case FUNCTION_TYPE: |
1bf5fc34 NS |
1254 | ix = TK_FUNCTION_TYPE; |
1255 | break; | |
3db45ab5 | 1256 | |
d689a8f1 | 1257 | case ARRAY_TYPE: |
1bf5fc34 NS |
1258 | ix = TK_ARRAY_TYPE; |
1259 | break; | |
3db45ab5 | 1260 | |
d689a8f1 NS |
1261 | case UNION_TYPE: |
1262 | case RECORD_TYPE: | |
1263 | if (TYPE_PTRMEMFUNC_P (type)) | |
ac9a1c7e | 1264 | ix = TK_POINTER_MEMBER_TYPE; |
d689a8f1 NS |
1265 | else if (!COMPLETE_TYPE_P (type)) |
1266 | { | |
5aa3396c JM |
1267 | if (!at_eof) |
1268 | cxx_incomplete_type_error (NULL_TREE, type); | |
1bf5fc34 | 1269 | ix = TK_CLASS_TYPE; |
d689a8f1 | 1270 | } |
cadec7ca JJ |
1271 | else if (!TYPE_BINFO (type) |
1272 | || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type))) | |
ac9a1c7e | 1273 | ix = TK_CLASS_TYPE; |
d689a8f1 NS |
1274 | else |
1275 | { | |
dbbf88d1 | 1276 | tree binfo = TYPE_BINFO (type); |
9771b263 | 1277 | vec<tree, va_gc> *base_accesses = BINFO_BASE_ACCESSES (binfo); |
fa743e8c NS |
1278 | tree base_binfo = BINFO_BASE_BINFO (binfo, 0); |
1279 | int num_bases = BINFO_N_BASE_BINFOS (binfo); | |
c8094d83 | 1280 | |
d689a8f1 | 1281 | if (num_bases == 1 |
9771b263 | 1282 | && (*base_accesses)[0] == access_public_node |
809e3e7f | 1283 | && !BINFO_VIRTUAL_P (base_binfo) |
d689a8f1 | 1284 | && integer_zerop (BINFO_OFFSET (base_binfo))) |
ac9a1c7e NS |
1285 | /* single non-virtual public. */ |
1286 | ix = TK_SI_CLASS_TYPE; | |
d689a8f1 | 1287 | else |
ac9a1c7e | 1288 | ix = TK_VMI_CLASS_TYPES + num_bases - 1; |
d689a8f1 | 1289 | } |
ac9a1c7e NS |
1290 | break; |
1291 | ||
d689a8f1 | 1292 | default: |
1bf5fc34 NS |
1293 | ix = TK_BUILTIN_TYPE; |
1294 | break; | |
d689a8f1 | 1295 | } |
1bf5fc34 | 1296 | return ix; |
7267d692 NS |
1297 | } |
1298 | ||
ac9a1c7e NS |
1299 | /* Return pointer to tinfo descriptor. Possibly creating the tinfo |
1300 | descriptor in the first place. */ | |
7267d692 | 1301 | |
ac9a1c7e NS |
1302 | static tinfo_s * |
1303 | get_tinfo_desc (unsigned ix) | |
7267d692 | 1304 | { |
ac9a1c7e | 1305 | unsigned len = tinfo_descs->length (); |
3db45ab5 | 1306 | |
ac9a1c7e NS |
1307 | if (len <= ix) |
1308 | { | |
1309 | /* too short, extend. */ | |
1310 | len = ix + 1 - len; | |
1311 | vec_safe_reserve (tinfo_descs, len); | |
1312 | tinfo_s elt; | |
1313 | elt.type = elt.vtable = elt.name = NULL_TREE; | |
1314 | while (len--) | |
1315 | tinfo_descs->quick_push (elt); | |
1316 | } | |
7267d692 | 1317 | |
ac9a1c7e | 1318 | tinfo_s *res = &(*tinfo_descs)[ix]; |
3db45ab5 | 1319 | |
ac9a1c7e NS |
1320 | if (res->type) |
1321 | return res; | |
c8094d83 | 1322 | |
ac9a1c7e NS |
1323 | /* Ok, we have to create it. This layout must be consistent with |
1324 | that defined in the runtime support. We explicitly manage the | |
1325 | vtable member, and name it for real type as used in the runtime. | |
1326 | The RECORD type has a different name, to avoid collisions. We | |
1327 | have to delay generating the VAR_DECL of the vtable until the end | |
1328 | of the translation, when we'll have seen the library definition, | |
1329 | if there was one. */ | |
c8094d83 | 1330 | |
ac9a1c7e NS |
1331 | /* Fields to add, chained in reverse order. */ |
1332 | tree fields = NULL_TREE; | |
7267d692 | 1333 | |
ac9a1c7e NS |
1334 | if (ix >= TK_DERIVED_TYPES) |
1335 | { | |
1336 | /* First field is the pseudo type_info base class. */ | |
1337 | tree fld_base = build_decl (BUILTINS_LOCATION, FIELD_DECL, NULL_TREE, | |
1338 | get_tinfo_desc (TK_TYPE_INFO_TYPE)->type); | |
3db45ab5 | 1339 | |
ac9a1c7e NS |
1340 | DECL_CHAIN (fld_base) = fields; |
1341 | fields = fld_base; | |
1342 | } | |
3db45ab5 | 1343 | |
ac9a1c7e NS |
1344 | switch (ix) |
1345 | { | |
1346 | case TK_TYPE_INFO_TYPE: | |
1347 | { | |
1348 | tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1349 | NULL_TREE, const_ptr_type_node); | |
1350 | fields = fld_ptr; | |
1351 | ||
1352 | tree fld_str = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1353 | NULL_TREE, const_string_type_node); | |
1354 | DECL_CHAIN (fld_str) = fields; | |
1355 | fields = fld_str; | |
1356 | break; | |
1357 | } | |
c8094d83 | 1358 | |
ac9a1c7e NS |
1359 | case TK_BASE_TYPE: |
1360 | { | |
1361 | /* Base class internal helper. Pointer to base type, offset to | |
1362 | base, flags. */ | |
1363 | tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1364 | NULL_TREE, type_info_ptr_type); | |
1365 | DECL_CHAIN (fld_ptr) = fields; | |
1366 | fields = fld_ptr; | |
1367 | ||
1368 | tree fld_flag = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1369 | NULL_TREE, LONGPTR_T); | |
1370 | DECL_CHAIN (fld_flag) = fields; | |
1371 | fields = fld_flag; | |
1372 | break; | |
1373 | } | |
c8094d83 | 1374 | |
ac9a1c7e NS |
1375 | case TK_BUILTIN_TYPE: |
1376 | /* Fundamental type_info */ | |
1377 | break; | |
c8094d83 | 1378 | |
ac9a1c7e NS |
1379 | case TK_ARRAY_TYPE: |
1380 | break; | |
3db45ab5 | 1381 | |
ac9a1c7e NS |
1382 | case TK_FUNCTION_TYPE: |
1383 | break; | |
3db45ab5 | 1384 | |
ac9a1c7e NS |
1385 | case TK_ENUMERAL_TYPE: |
1386 | break; | |
1387 | ||
1388 | case TK_POINTER_TYPE: | |
1389 | case TK_POINTER_MEMBER_TYPE: | |
1390 | { | |
1391 | /* Pointer type_info. Adds two fields, qualification mask and | |
1392 | pointer to the pointed to type. This is really a | |
1393 | descendant of __pbase_type_info. */ | |
1394 | tree fld_mask = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1395 | NULL_TREE, integer_type_node); | |
1396 | DECL_CHAIN (fld_mask) = fields; | |
1397 | fields = fld_mask; | |
1398 | ||
1399 | tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1400 | NULL_TREE, type_info_ptr_type); | |
1401 | DECL_CHAIN (fld_ptr) = fields; | |
1402 | fields = fld_ptr; | |
1403 | ||
1404 | if (ix == TK_POINTER_MEMBER_TYPE) | |
1405 | { | |
1406 | /* Add a pointer to the class too. */ | |
1407 | tree fld_cls = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1408 | NULL_TREE, type_info_ptr_type); | |
1409 | DECL_CHAIN (fld_cls) = fields; | |
1410 | fields = fld_cls; | |
1411 | } | |
1412 | break; | |
1413 | } | |
1414 | ||
1415 | case TK_CLASS_TYPE: | |
1416 | /* Class type_info. No additional fields. */ | |
1417 | break; | |
1418 | ||
1419 | case TK_SI_CLASS_TYPE: | |
1420 | { | |
1421 | /* Single public non-virtual base class. Add pointer to base | |
1422 | class. This is really a descendant of | |
1423 | __class_type_info. */ | |
1424 | tree fld_ptr = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1425 | NULL_TREE, type_info_ptr_type); | |
1426 | DECL_CHAIN (fld_ptr) = fields; | |
1427 | fields = fld_ptr; | |
1428 | break; | |
1429 | } | |
1430 | ||
1431 | default: /* Multiple inheritance. */ | |
1432 | { | |
1433 | unsigned num_bases = ix - TK_VMI_CLASS_TYPES + 1; | |
1434 | ||
1435 | tree fld_flg = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1436 | NULL_TREE, integer_type_node); | |
1437 | DECL_CHAIN (fld_flg) = fields; | |
1438 | fields = fld_flg; | |
1439 | ||
1440 | tree fld_cnt = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1441 | NULL_TREE, integer_type_node); | |
1442 | DECL_CHAIN (fld_cnt) = fields; | |
1443 | fields = fld_cnt; | |
1444 | ||
1445 | /* Create the array of __base_class_type_info entries. */ | |
1446 | tree domain = build_index_type (size_int (num_bases - 1)); | |
1447 | tree array = build_array_type (get_tinfo_desc (TK_BASE_TYPE)->type, | |
1448 | domain); | |
1449 | tree fld_ary = build_decl (BUILTINS_LOCATION, FIELD_DECL, | |
1450 | NULL_TREE, array); | |
1451 | DECL_CHAIN (fld_ary) = fields; | |
1452 | fields = fld_ary; | |
1453 | break; | |
1454 | } | |
1455 | } | |
1456 | ||
1457 | push_abi_namespace (); | |
1458 | ||
1459 | /* Generate the pseudo type name. */ | |
1460 | const char *real_name = tinfo_names[ix < TK_VMI_CLASS_TYPES | |
1461 | ? ix : unsigned (TK_VMI_CLASS_TYPES)]; | |
1462 | size_t name_len = strlen (real_name); | |
1463 | char *pseudo_name = (char *) alloca (name_len + 30); | |
1464 | memcpy (pseudo_name, real_name, name_len); | |
1465 | /* Those >= TK_VMI_CLASS_TYPES need a discriminator, may as well | |
1466 | apply it to all. See get_peudo_tinfo_index where we make use of | |
1467 | this. */ | |
1468 | sprintf (pseudo_name + name_len, "_pseudo_%d", ix); | |
c8094d83 | 1469 | |
ac9a1c7e NS |
1470 | /* Create the pseudo type. */ |
1471 | tree pseudo_type = make_class_type (RECORD_TYPE); | |
1472 | /* Pass the fields chained in reverse. */ | |
1473 | finish_builtin_struct (pseudo_type, pseudo_name, fields, NULL_TREE); | |
1474 | CLASSTYPE_AS_BASE (pseudo_type) = pseudo_type; | |
80de0002 | 1475 | xref_basetypes (pseudo_type, /*bases=*/NULL_TREE); |
ac9a1c7e NS |
1476 | |
1477 | res->type = cp_build_qualified_type (pseudo_type, TYPE_QUAL_CONST); | |
1478 | res->name = get_identifier (real_name); | |
1479 | ||
1480 | /* Pretend this is public so determine_visibility doesn't give vtables | |
1481 | internal linkage. */ | |
1482 | TREE_PUBLIC (TYPE_MAIN_DECL (res->type)) = 1; | |
7267d692 | 1483 | |
a15f0fd0 | 1484 | pop_abi_namespace (); |
ac9a1c7e NS |
1485 | return res; |
1486 | } | |
1487 | ||
1488 | /* We lazily create the type info types. */ | |
1489 | ||
1490 | static void | |
1491 | create_tinfo_types (void) | |
1492 | { | |
1493 | gcc_assert (!tinfo_descs); | |
1494 | ||
1495 | vec_alloc (tinfo_descs, TK_MAX + 20); | |
7267d692 NS |
1496 | } |
1497 | ||
793c625f MG |
1498 | /* Helper for emit_support_tinfos. Emits the type_info descriptor of |
1499 | a single type. */ | |
1500 | ||
1501 | void | |
1502 | emit_support_tinfo_1 (tree bltn) | |
1503 | { | |
1504 | tree types[3]; | |
1505 | ||
1506 | if (bltn == NULL_TREE) | |
1507 | return; | |
1508 | types[0] = bltn; | |
1509 | types[1] = build_pointer_type (bltn); | |
1510 | types[2] = build_pointer_type (cp_build_qualified_type (bltn, | |
1511 | TYPE_QUAL_CONST)); | |
1512 | ||
1513 | for (int i = 0; i < 3; ++i) | |
1514 | { | |
1515 | tree tinfo = get_tinfo_decl (types[i]); | |
1516 | TREE_USED (tinfo) = 1; | |
1517 | mark_needed (tinfo); | |
1518 | /* The C++ ABI requires that these objects be COMDAT. But, | |
1519 | On systems without weak symbols, initialized COMDAT | |
1520 | objects are emitted with internal linkage. (See | |
1521 | comdat_linkage for details.) Since we want these objects | |
1522 | to have external linkage so that copies do not have to be | |
1523 | emitted in code outside the runtime library, we make them | |
1524 | non-COMDAT here. | |
1525 | ||
1526 | It might also not be necessary to follow this detail of the | |
1527 | ABI. */ | |
1528 | if (!flag_weak || ! targetm.cxx.library_rtti_comdat ()) | |
1529 | { | |
1530 | gcc_assert (TREE_PUBLIC (tinfo) && !DECL_COMDAT (tinfo)); | |
1531 | DECL_INTERFACE_KNOWN (tinfo) = 1; | |
1532 | } | |
1533 | } | |
1534 | } | |
1535 | ||
7267d692 NS |
1536 | /* Emit the type_info descriptors which are guaranteed to be in the runtime |
1537 | support. Generating them here guarantees consistency with the other | |
1538 | structures. We use the following heuristic to determine when the runtime | |
7f54a851 | 1539 | is being generated. If std::__fundamental_type_info is defined, and its |
7267d692 NS |
1540 | destructor is defined, then the runtime is being built. */ |
1541 | ||
1542 | void | |
848eed92 | 1543 | emit_support_tinfos (void) |
7267d692 | 1544 | { |
14c2101d JM |
1545 | /* Dummy static variable so we can put nullptr in the array; it will be |
1546 | set before we actually start to walk the array. */ | |
7267d692 NS |
1547 | static tree *const fundamentals[] = |
1548 | { | |
1549 | &void_type_node, | |
1550 | &boolean_type_node, | |
2d91f79d | 1551 | &wchar_type_node, &char8_type_node, &char16_type_node, &char32_type_node, |
7267d692 NS |
1552 | &char_type_node, &signed_char_type_node, &unsigned_char_type_node, |
1553 | &short_integer_type_node, &short_unsigned_type_node, | |
1554 | &integer_type_node, &unsigned_type_node, | |
1555 | &long_integer_type_node, &long_unsigned_type_node, | |
1556 | &long_long_integer_type_node, &long_long_unsigned_type_node, | |
1557 | &float_type_node, &double_type_node, &long_double_type_node, | |
fae927d3 | 1558 | &dfloat32_type_node, &dfloat64_type_node, &dfloat128_type_node, |
14c2101d | 1559 | &nullptr_type_node, |
7267d692 NS |
1560 | 0 |
1561 | }; | |
1562 | int ix; | |
c8094d83 | 1563 | |
ac9a1c7e NS |
1564 | /* Look for a defined class. */ |
1565 | tree bltn_type = lookup_qualified_name | |
f22f817c | 1566 | (abi_node, "__fundamental_type_info", true, false); |
ac9a1c7e NS |
1567 | if (TREE_CODE (bltn_type) != TYPE_DECL) |
1568 | return; | |
1569 | ||
1570 | bltn_type = TREE_TYPE (bltn_type); | |
d0f062fb | 1571 | if (!COMPLETE_TYPE_P (bltn_type)) |
7267d692 | 1572 | return; |
b2cf76f3 | 1573 | tree dtor = CLASSTYPE_DESTRUCTOR (bltn_type); |
9f4faeae | 1574 | if (!dtor || DECL_EXTERNAL (dtor)) |
7267d692 | 1575 | return; |
ac9a1c7e NS |
1576 | |
1577 | /* All these are really builtins. So set the location. */ | |
1578 | location_t saved_loc = input_location; | |
1579 | input_location = BUILTINS_LOCATION; | |
7267d692 NS |
1580 | doing_runtime = 1; |
1581 | for (ix = 0; fundamentals[ix]; ix++) | |
793c625f | 1582 | emit_support_tinfo_1 (*fundamentals[ix]); |
78a7c317 DD |
1583 | for (ix = 0; ix < NUM_INT_N_ENTS; ix ++) |
1584 | if (int_n_enabled_p[ix]) | |
1585 | { | |
1586 | emit_support_tinfo_1 (int_n_trees[ix].signed_type); | |
1587 | emit_support_tinfo_1 (int_n_trees[ix].unsigned_type); | |
1588 | } | |
bb59f396 MG |
1589 | for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t)) |
1590 | emit_support_tinfo_1 (TREE_VALUE (t)); | |
994e0ad4 JJ |
1591 | /* For compatibility, emit DFP typeinfos even when DFP isn't enabled, |
1592 | because we've emitted that in the past. */ | |
1593 | if (!targetm.decimal_float_supported_p ()) | |
1594 | { | |
1595 | gcc_assert (dfloat32_type_node == NULL_TREE | |
1596 | && dfloat64_type_node == NULL_TREE | |
1597 | && dfloat128_type_node == NULL_TREE); | |
1598 | fallback_dfloat32_type = make_node (REAL_TYPE); | |
1599 | fallback_dfloat64_type = make_node (REAL_TYPE); | |
1600 | fallback_dfloat128_type = make_node (REAL_TYPE); | |
1601 | emit_support_tinfo_1 (fallback_dfloat32_type); | |
1602 | emit_support_tinfo_1 (fallback_dfloat64_type); | |
1603 | emit_support_tinfo_1 (fallback_dfloat128_type); | |
1604 | } | |
ac9a1c7e | 1605 | input_location = saved_loc; |
7267d692 NS |
1606 | } |
1607 | ||
d689a8f1 NS |
1608 | /* Finish a type info decl. DECL_PTR is a pointer to an unemitted |
1609 | tinfo decl. Determine whether it needs emitting, and if so | |
1610 | generate the initializer. */ | |
7267d692 | 1611 | |
848eed92 | 1612 | bool |
a260bce6 | 1613 | emit_tinfo_decl (tree decl) |
7267d692 | 1614 | { |
d689a8f1 | 1615 | tree type = TREE_TYPE (DECL_NAME (decl)); |
c6f553d1 | 1616 | int in_library = typeinfo_in_lib_p (type); |
a260bce6 | 1617 | |
c8094d83 MS |
1618 | gcc_assert (DECL_TINFO_P (decl)); |
1619 | ||
4684cd27 MM |
1620 | if (in_library) |
1621 | { | |
1622 | if (doing_runtime) | |
1623 | DECL_EXTERNAL (decl) = 0; | |
1624 | else | |
1625 | { | |
1626 | /* If we're not in the runtime, then DECL (which is already | |
1627 | DECL_EXTERNAL) will not be defined here. */ | |
1628 | DECL_INTERFACE_KNOWN (decl) = 1; | |
1629 | return false; | |
1630 | } | |
1631 | } | |
1632 | else if (involves_incomplete_p (type)) | |
1633 | { | |
1634 | if (!decl_needed_p (decl)) | |
1635 | return false; | |
1636 | /* If TYPE involves an incomplete class type, then the typeinfo | |
1637 | object will be emitted with internal linkage. There is no | |
1638 | way to know whether or not types are incomplete until the end | |
1639 | of the compilation, so this determination must be deferred | |
1640 | until this point. */ | |
1641 | TREE_PUBLIC (decl) = 0; | |
1642 | DECL_EXTERNAL (decl) = 0; | |
1643 | DECL_INTERFACE_KNOWN (decl) = 1; | |
8c1ca7ee | 1644 | } |
ff48be5c | 1645 | |
4684cd27 MM |
1646 | import_export_decl (decl); |
1647 | if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl)) | |
1648 | { | |
1bf5fc34 | 1649 | tree init; |
3db45ab5 | 1650 | |
4684cd27 | 1651 | DECL_EXTERNAL (decl) = 0; |
1bf5fc34 NS |
1652 | init = get_pseudo_ti_init (type, get_pseudo_ti_index (type)); |
1653 | DECL_INITIAL (decl) = init; | |
4684cd27 | 1654 | mark_used (decl); |
3600f678 | 1655 | cp_finish_decl (decl, init, false, NULL_TREE, 0); |
c517fc92 JH |
1656 | /* Avoid targets optionally bumping up the alignment to improve |
1657 | vector instruction accesses, tinfo are never accessed this way. */ | |
1658 | #ifdef DATA_ABI_ALIGNMENT | |
fe37c7af | 1659 | SET_DECL_ALIGN (decl, DATA_ABI_ALIGNMENT (decl, TYPE_ALIGN (TREE_TYPE (decl)))); |
c517fc92 JH |
1660 | DECL_USER_ALIGN (decl) = true; |
1661 | #endif | |
4684cd27 MM |
1662 | return true; |
1663 | } | |
1664 | else | |
848eed92 | 1665 | return false; |
7267d692 | 1666 | } |
1bf5fc34 NS |
1667 | |
1668 | #include "gt-cp-rtti.h" |