| 1 |
/* typeinfo.cc -- D runtime type identification. |
| 2 |
Copyright (C) 2013-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
GCC is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 3, or (at your option) |
| 7 |
any later version. |
| 8 |
|
| 9 |
GCC is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with GCC; see the file COPYING3. If not see |
| 16 |
<http://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
#include "system.h" |
| 20 |
#include "coretypes.h" |
| 21 |
|
| 22 |
#include "dmd/aggregate.h" |
| 23 |
#include "dmd/dsymbol.h" |
| 24 |
#include "dmd/enum.h" |
| 25 |
#include "dmd/errors.h" |
| 26 |
#include "dmd/expression.h" |
| 27 |
#include "dmd/globals.h" |
| 28 |
#include "dmd/identifier.h" |
| 29 |
#include "dmd/module.h" |
| 30 |
#include "dmd/mtype.h" |
| 31 |
#include "dmd/scope.h" |
| 32 |
#include "dmd/template.h" |
| 33 |
#include "dmd/target.h" |
| 34 |
#include "dmd/typinf.h" |
| 35 |
|
| 36 |
#include "tree.h" |
| 37 |
#include "fold-const.h" |
| 38 |
#include "diagnostic.h" |
| 39 |
#include "stringpool.h" |
| 40 |
#include "toplev.h" |
| 41 |
#include "stor-layout.h" |
| 42 |
|
| 43 |
#include "d-tree.h" |
| 44 |
#include "d-frontend.h" |
| 45 |
#include "d-target.h" |
| 46 |
|
| 47 |
|
| 48 |
/* D returns type information to the user as TypeInfo class objects, and can |
| 49 |
be retrieved for any type using `typeid()'. We also use type information |
| 50 |
to implement many runtime library helpers, including `new', `delete', most |
| 51 |
dynamic array operations, and all associative array operations. |
| 52 |
|
| 53 |
Type information for a particular type is indicated with an ABI defined |
| 54 |
structure derived from TypeInfo. This would all be very straight forward, |
| 55 |
but for the fact that the runtime library provides the definitions of the |
| 56 |
TypeInfo structure and the ABI defined derived classes in `object.d', as |
| 57 |
well as having specific implementations of TypeInfo for built-in types |
| 58 |
in `rt/typeinfo`. We cannot build declarations of these directly in the |
| 59 |
compiler, but we need to layout objects of their type. |
| 60 |
|
| 61 |
To get around this, we define layout compatible POD-structs and generate the |
| 62 |
appropriate initializations for them. When we have to provide a TypeInfo to |
| 63 |
the user, we cast the internal compiler type to TypeInfo. |
| 64 |
|
| 65 |
It is only required that TypeInfo has a definition in `object.d'. It could |
| 66 |
happen that we are generating a type information for a TypeInfo object that |
| 67 |
has no declaration. We however only need the addresses of such incomplete |
| 68 |
TypeInfo objects for static initialization. */ |
| 69 |
|
| 70 |
enum tinfo_kind |
| 71 |
{ |
| 72 |
TK_TYPEINFO_TYPE, /* object.TypeInfo */ |
| 73 |
TK_CLASSINFO_TYPE, /* object.TypeInfo_Class */ |
| 74 |
TK_INTERFACE_TYPE, /* object.TypeInfo_Interface */ |
| 75 |
TK_STRUCT_TYPE, /* object.TypeInfo_Struct */ |
| 76 |
TK_POINTER_TYPE, /* object.TypeInfo_Pointer */ |
| 77 |
TK_ARRAY_TYPE, /* object.TypeInfo_Array */ |
| 78 |
TK_STATICARRAY_TYPE, /* object.TypeInfo_StaticArray */ |
| 79 |
TK_ASSOCIATIVEARRAY_TYPE, /* object.TypeInfo_AssociativeArray */ |
| 80 |
TK_VECTOR_TYPE, /* object.TypeInfo_Vector */ |
| 81 |
TK_ENUMERAL_TYPE, /* object.TypeInfo_Enum */ |
| 82 |
TK_FUNCTION_TYPE, /* object.TypeInfo_Function */ |
| 83 |
TK_DELEGATE_TYPE, /* object.TypeInfo_Delegate */ |
| 84 |
TK_TYPELIST_TYPE, /* object.TypeInfo_Tuple */ |
| 85 |
TK_CONST_TYPE, /* object.TypeInfo_Const */ |
| 86 |
TK_IMMUTABLE_TYPE, /* object.TypeInfo_Invariant */ |
| 87 |
TK_SHARED_TYPE, /* object.TypeInfo_Shared */ |
| 88 |
TK_INOUT_TYPE, /* object.TypeInfo_Inout */ |
| 89 |
TK_CPPTI_TYPE, /* object.__cpp_type_info_ptr */ |
| 90 |
TK_END |
| 91 |
}; |
| 92 |
|
| 93 |
/* An array of all internal TypeInfo derived types we need. |
| 94 |
The TypeInfo and ClassInfo types are created early, the |
| 95 |
remainder are generated as needed. */ |
| 96 |
|
| 97 |
static GTY(()) tree tinfo_types[TK_END]; |
| 98 |
|
| 99 |
/* Return the kind of TypeInfo used to describe TYPE. */ |
| 100 |
|
| 101 |
static tinfo_kind |
| 102 |
get_typeinfo_kind (Type *type) |
| 103 |
{ |
| 104 |
/* Check head shared/const modifiers first. */ |
| 105 |
if (type->isShared ()) |
| 106 |
return TK_SHARED_TYPE; |
| 107 |
else if (type->isConst ()) |
| 108 |
return TK_CONST_TYPE; |
| 109 |
else if (type->isImmutable ()) |
| 110 |
return TK_IMMUTABLE_TYPE; |
| 111 |
else if (type->isWild ()) |
| 112 |
return TK_INOUT_TYPE; |
| 113 |
|
| 114 |
switch (type->ty) |
| 115 |
{ |
| 116 |
case TY::Tpointer: |
| 117 |
return TK_POINTER_TYPE; |
| 118 |
|
| 119 |
case TY::Tarray: |
| 120 |
return TK_ARRAY_TYPE; |
| 121 |
|
| 122 |
case TY::Tsarray: |
| 123 |
return TK_STATICARRAY_TYPE; |
| 124 |
|
| 125 |
case TY::Taarray: |
| 126 |
return TK_ASSOCIATIVEARRAY_TYPE; |
| 127 |
|
| 128 |
case TY::Tstruct: |
| 129 |
return TK_STRUCT_TYPE; |
| 130 |
|
| 131 |
case TY::Tvector: |
| 132 |
return TK_VECTOR_TYPE; |
| 133 |
|
| 134 |
case TY::Tenum: |
| 135 |
return TK_ENUMERAL_TYPE; |
| 136 |
|
| 137 |
case TY::Tfunction: |
| 138 |
return TK_FUNCTION_TYPE; |
| 139 |
|
| 140 |
case TY::Tdelegate: |
| 141 |
return TK_DELEGATE_TYPE; |
| 142 |
|
| 143 |
case TY::Ttuple: |
| 144 |
return TK_TYPELIST_TYPE; |
| 145 |
|
| 146 |
case TY::Tclass: |
| 147 |
if (type->isTypeClass ()->sym->isInterfaceDeclaration ()) |
| 148 |
return TK_INTERFACE_TYPE; |
| 149 |
else |
| 150 |
return TK_CLASSINFO_TYPE; |
| 151 |
|
| 152 |
default: |
| 153 |
return TK_TYPEINFO_TYPE; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
/* Generate the RECORD_TYPE containing the data layout of a TypeInfo derivative |
| 158 |
as used by the runtime. This layout must be consistent with that defined in |
| 159 |
the `object.d' module. */ |
| 160 |
|
| 161 |
static void |
| 162 |
make_internal_typeinfo (tinfo_kind tk, Identifier *ident, ...) |
| 163 |
{ |
| 164 |
va_list ap; |
| 165 |
|
| 166 |
va_start (ap, ident); |
| 167 |
|
| 168 |
/* First two fields are from the TypeInfo base class. |
| 169 |
Note, finish_builtin_struct() expects these fields in reverse order. */ |
| 170 |
tree fields = create_field_decl (ptr_type_node, NULL, 1, 1); |
| 171 |
DECL_CHAIN (fields) = create_field_decl (vtbl_ptr_type_node, NULL, 1, 1); |
| 172 |
|
| 173 |
/* Now add the derived fields. */ |
| 174 |
tree field_type = va_arg (ap, tree); |
| 175 |
while (field_type != NULL_TREE) |
| 176 |
{ |
| 177 |
tree field = create_field_decl (field_type, NULL, 1, 1); |
| 178 |
DECL_CHAIN (field) = fields; |
| 179 |
fields = field; |
| 180 |
field_type = va_arg (ap, tree); |
| 181 |
} |
| 182 |
|
| 183 |
/* Create the TypeInfo type. */ |
| 184 |
tree type = make_node (RECORD_TYPE); |
| 185 |
TYPE_ARTIFICIAL (type) = 1; |
| 186 |
finish_builtin_struct (type, ident->toChars (), fields, NULL_TREE); |
| 187 |
|
| 188 |
tinfo_types[tk] = type; |
| 189 |
|
| 190 |
va_end (ap); |
| 191 |
} |
| 192 |
|
| 193 |
/* Reference to the `object` module, where all TypeInfo is defined. */ |
| 194 |
|
| 195 |
static Module *object_module; |
| 196 |
|
| 197 |
/* Helper for create_frontend_tinfo_types. Creates a typeinfo class |
| 198 |
declaration incase one wasn't supplied by reading `object.d'. */ |
| 199 |
|
| 200 |
static void |
| 201 |
make_frontend_typeinfo (Identifier *ident, ClassDeclaration *base = NULL) |
| 202 |
{ |
| 203 |
if (!base) |
| 204 |
base = Type::dtypeinfo; |
| 205 |
|
| 206 |
gcc_assert (object_module); |
| 207 |
|
| 208 |
/* Create object module in order to complete the semantic. */ |
| 209 |
if (!object_module->_scope) |
| 210 |
dmd::importAll (object_module, NULL); |
| 211 |
|
| 212 |
/* Object class doesn't exist, create a stub one that will cause an error if |
| 213 |
used. */ |
| 214 |
Loc loc = (object_module->md) ? object_module->md->loc : object_module->loc; |
| 215 |
if (!base) |
| 216 |
{ |
| 217 |
if (!ClassDeclaration::object) |
| 218 |
{ |
| 219 |
ClassDeclaration *object |
| 220 |
= ClassDeclaration::create (loc, Identifier::idPool ("Object"), |
| 221 |
NULL, NULL, true); |
| 222 |
object->parent = object_module; |
| 223 |
object->members = d_gc_malloc<Dsymbols> (); |
| 224 |
object->storage_class |= STCtemp; |
| 225 |
} |
| 226 |
|
| 227 |
base = ClassDeclaration::object; |
| 228 |
} |
| 229 |
|
| 230 |
/* Assignment of global typeinfo variables is managed by the ClassDeclaration |
| 231 |
constructor, so only need to new the declaration here. */ |
| 232 |
ClassDeclaration *tinfo = ClassDeclaration::create (loc, ident, NULL, NULL, |
| 233 |
true); |
| 234 |
tinfo->parent = object_module; |
| 235 |
tinfo->members = d_gc_malloc<Dsymbols> (); |
| 236 |
dmd::dsymbolSemantic (tinfo, object_module->_scope); |
| 237 |
tinfo->baseClass = base; |
| 238 |
/* This is a compiler generated class, and shouldn't be mistaken for being |
| 239 |
the type declared in the runtime library. */ |
| 240 |
tinfo->storage_class |= STCtemp; |
| 241 |
} |
| 242 |
|
| 243 |
/* Make sure the required builtin types exist for generating the TypeInfo |
| 244 |
variable definitions. */ |
| 245 |
|
| 246 |
void |
| 247 |
create_tinfo_types (Module *mod) |
| 248 |
{ |
| 249 |
/* Already generated internal types for the object module. */ |
| 250 |
if (object_module != NULL) |
| 251 |
return; |
| 252 |
|
| 253 |
/* Build the internal TypeInfo and ClassInfo types. |
| 254 |
See TypeInfoVisitor for documentation of field layout. */ |
| 255 |
make_internal_typeinfo (TK_TYPEINFO_TYPE, Identifier::idPool ("TypeInfo"), |
| 256 |
NULL); |
| 257 |
|
| 258 |
make_internal_typeinfo (TK_CLASSINFO_TYPE, |
| 259 |
Identifier::idPool ("TypeInfo_Class"), |
| 260 |
array_type_node, array_type_node, array_type_node, |
| 261 |
array_type_node, ptr_type_node, ptr_type_node, |
| 262 |
ptr_type_node, d_ushort_type, d_ushort_type, |
| 263 |
ptr_type_node, array_type_node, ptr_type_node, |
| 264 |
ptr_type_node, d_uint_type, d_uint_type, d_uint_type, |
| 265 |
d_uint_type, NULL); |
| 266 |
|
| 267 |
object_module = mod; |
| 268 |
} |
| 269 |
|
| 270 |
/* Same as create_tinfo_types, but builds all front-end TypeInfo variable |
| 271 |
definitions. */ |
| 272 |
|
| 273 |
static void |
| 274 |
create_frontend_tinfo_types (void) |
| 275 |
{ |
| 276 |
/* If there's no object module, then neither can there be TypeInfo. */ |
| 277 |
if (object_module == NULL) |
| 278 |
return; |
| 279 |
|
| 280 |
/* Create all frontend TypeInfo classes declarations. We rely on all |
| 281 |
existing, even if only just as stubs. */ |
| 282 |
if (!Type::dtypeinfo) |
| 283 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo"), |
| 284 |
ClassDeclaration::object); |
| 285 |
|
| 286 |
if (!Type::typeinfoclass) |
| 287 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Class")); |
| 288 |
|
| 289 |
if (!Type::typeinfointerface) |
| 290 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Interface")); |
| 291 |
|
| 292 |
if (!Type::typeinfostruct) |
| 293 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Struct")); |
| 294 |
|
| 295 |
if (!Type::typeinfopointer) |
| 296 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Pointer")); |
| 297 |
|
| 298 |
if (!Type::typeinfoarray) |
| 299 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Array")); |
| 300 |
|
| 301 |
if (!Type::typeinfostaticarray) |
| 302 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_StaticArray")); |
| 303 |
|
| 304 |
if (!Type::typeinfoassociativearray) |
| 305 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_AssociativeArray")); |
| 306 |
|
| 307 |
if (!Type::typeinfoenum) |
| 308 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Enum")); |
| 309 |
|
| 310 |
if (!Type::typeinfofunction) |
| 311 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Function")); |
| 312 |
|
| 313 |
if (!Type::typeinfodelegate) |
| 314 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Delegate")); |
| 315 |
|
| 316 |
if (!Type::typeinfotypelist) |
| 317 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Tuple")); |
| 318 |
|
| 319 |
if (!Type::typeinfoconst) |
| 320 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Const")); |
| 321 |
|
| 322 |
if (!Type::typeinfoinvariant) |
| 323 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Invariant"), |
| 324 |
Type::typeinfoconst); |
| 325 |
|
| 326 |
if (!Type::typeinfoshared) |
| 327 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Shared"), |
| 328 |
Type::typeinfoconst); |
| 329 |
|
| 330 |
if (!Type::typeinfowild) |
| 331 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Wild"), |
| 332 |
Type::typeinfoconst); |
| 333 |
|
| 334 |
if (!Type::typeinfovector) |
| 335 |
make_frontend_typeinfo (Identifier::idPool ("TypeInfo_Vector")); |
| 336 |
|
| 337 |
if (!ClassDeclaration::cpp_type_info_ptr) |
| 338 |
make_frontend_typeinfo (Identifier::idPool ("__cpp_type_info_ptr"), |
| 339 |
ClassDeclaration::object); |
| 340 |
} |
| 341 |
|
| 342 |
/* Return true if TypeInfo class TINFO is available in the runtime library. */ |
| 343 |
|
| 344 |
bool |
| 345 |
have_typeinfo_p (ClassDeclaration *tinfo) |
| 346 |
{ |
| 347 |
/* Run-time typeinfo disabled on command line. */ |
| 348 |
if (!global.params.useTypeInfo) |
| 349 |
return false; |
| 350 |
|
| 351 |
/* Can't layout TypeInfo if type is not declared, or is an opaque |
| 352 |
declaration in the object module. */ |
| 353 |
if (!tinfo || !tinfo->members) |
| 354 |
return false; |
| 355 |
|
| 356 |
/* Typeinfo is compiler-generated. */ |
| 357 |
if (tinfo->storage_class & STCtemp) |
| 358 |
return false; |
| 359 |
|
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
/* Implements the visitor interface to build the TypeInfo layout of all |
| 364 |
TypeInfoDeclaration AST classes emitted from the D Front-end. |
| 365 |
All visit methods accept one parameter D, which holds the frontend AST |
| 366 |
of the TypeInfo class. They also don't return any value, instead the |
| 367 |
generated symbol is cached internally and returned from the caller. */ |
| 368 |
|
| 369 |
class TypeInfoVisitor : public Visitor |
| 370 |
{ |
| 371 |
using Visitor::visit; |
| 372 |
|
| 373 |
tree decl_; |
| 374 |
vec<constructor_elt, va_gc> *init_; |
| 375 |
|
| 376 |
/* Build an internal comdat symbol for the manifest constant VALUE, so that |
| 377 |
its address can be taken. */ |
| 378 |
|
| 379 |
tree internal_reference (tree value) |
| 380 |
{ |
| 381 |
/* Use the typeinfo decl name as a prefix for the internal symbol. */ |
| 382 |
const char *prefix = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (this->decl_)); |
| 383 |
tree decl = build_artificial_decl (TREE_TYPE (value), value, prefix); |
| 384 |
|
| 385 |
/* The internal pointer reference should be public, but not visible outside |
| 386 |
the compilation unit. */ |
| 387 |
DECL_EXTERNAL (decl) = 0; |
| 388 |
TREE_PUBLIC (decl) = 1; |
| 389 |
DECL_VISIBILITY (decl) = VISIBILITY_INTERNAL; |
| 390 |
set_linkage_for_decl (decl); |
| 391 |
d_pushdecl (decl); |
| 392 |
|
| 393 |
return decl; |
| 394 |
} |
| 395 |
|
| 396 |
/* Add VALUE to the constructor values list. */ |
| 397 |
|
| 398 |
void layout_field (tree value) |
| 399 |
{ |
| 400 |
CONSTRUCTOR_APPEND_ELT (this->init_, NULL_TREE, value); |
| 401 |
} |
| 402 |
|
| 403 |
/* Write out STR as a static D string literal. */ |
| 404 |
|
| 405 |
void layout_string (const char *str) |
| 406 |
{ |
| 407 |
unsigned len = strlen (str); |
| 408 |
tree value = build_string (len, str); |
| 409 |
|
| 410 |
TREE_TYPE (value) = make_array_type (Type::tchar, len); |
| 411 |
TREE_CONSTANT (value) = 1; |
| 412 |
TREE_READONLY (value) = 1; |
| 413 |
TREE_STATIC (value) = 1; |
| 414 |
|
| 415 |
/* Taking the address, so assign the literal to a static var. */ |
| 416 |
tree decl = this->internal_reference (value); |
| 417 |
TREE_READONLY (decl) = 1; |
| 418 |
|
| 419 |
value = d_array_value (build_ctype (dmd::arrayOf (Type::tchar)), |
| 420 |
size_int (len), build_address (decl)); |
| 421 |
this->layout_field (value); |
| 422 |
} |
| 423 |
|
| 424 |
/* Write out the __vptr and optionally __monitor fields of class CD. */ |
| 425 |
|
| 426 |
void layout_base (ClassDeclaration *cd) |
| 427 |
{ |
| 428 |
gcc_assert (cd != NULL); |
| 429 |
|
| 430 |
if (have_typeinfo_p (cd)) |
| 431 |
this->layout_field (build_address (get_vtable_decl (cd))); |
| 432 |
else |
| 433 |
this->layout_field (null_pointer_node); |
| 434 |
|
| 435 |
if (cd->hasMonitor ()) |
| 436 |
this->layout_field (null_pointer_node); |
| 437 |
} |
| 438 |
|
| 439 |
/* Write out the interfaces field of class CD. |
| 440 |
Returns the array of interfaces that the field is pointing to. */ |
| 441 |
|
| 442 |
tree layout_interfaces (ClassDeclaration *cd) |
| 443 |
{ |
| 444 |
size_t offset = int_size_in_bytes (tinfo_types[TK_CLASSINFO_TYPE]); |
| 445 |
tree csym = build_address (get_classinfo_decl (cd)); |
| 446 |
|
| 447 |
/* Put out the offset to where vtblInterfaces are written. */ |
| 448 |
tree value = d_array_value (array_type_node, |
| 449 |
size_int (cd->vtblInterfaces->length), |
| 450 |
build_offset (csym, size_int (offset))); |
| 451 |
this->layout_field (value); |
| 452 |
|
| 453 |
/* Internally, the compiler sees Interface as: |
| 454 |
void*[4] interface; |
| 455 |
|
| 456 |
The run-time layout of Interface is: |
| 457 |
TypeInfo_Class classinfo; |
| 458 |
void*[] vtbl; |
| 459 |
size_t offset; */ |
| 460 |
vec<constructor_elt, va_gc> *elms = NULL; |
| 461 |
|
| 462 |
for (size_t i = 0; i < cd->vtblInterfaces->length; i++) |
| 463 |
{ |
| 464 |
BaseClass *b = (*cd->vtblInterfaces)[i]; |
| 465 |
ClassDeclaration *id = b->sym; |
| 466 |
vec<constructor_elt, va_gc> *v = NULL; |
| 467 |
|
| 468 |
/* Fill in the vtbl[]. */ |
| 469 |
if (!cd->isInterfaceDeclaration ()) |
| 470 |
b->fillVtbl (cd, &b->vtbl, 1); |
| 471 |
|
| 472 |
/* ClassInfo for the interface. */ |
| 473 |
value = build_address (get_classinfo_decl (id)); |
| 474 |
CONSTRUCTOR_APPEND_ELT (v, size_int (0), value); |
| 475 |
|
| 476 |
if (!cd->isInterfaceDeclaration ()) |
| 477 |
{ |
| 478 |
/* The vtable of the interface length and ptr. */ |
| 479 |
unsigned voffset = base_vtable_offset (cd, b); |
| 480 |
gcc_assert (voffset != 0u); |
| 481 |
value = build_offset (csym, size_int (voffset)); |
| 482 |
|
| 483 |
CONSTRUCTOR_APPEND_ELT (v, size_int (1), |
| 484 |
size_int (id->vtbl.length)); |
| 485 |
CONSTRUCTOR_APPEND_ELT (v, size_int (2), value); |
| 486 |
} |
| 487 |
|
| 488 |
/* The `this' offset. */ |
| 489 |
CONSTRUCTOR_APPEND_ELT (v, size_int (3), size_int (b->offset)); |
| 490 |
|
| 491 |
/* Add to the array of interfaces. */ |
| 492 |
value = build_padded_constructor (vtbl_interface_type_node, v); |
| 493 |
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value); |
| 494 |
} |
| 495 |
|
| 496 |
tree domain = size_int (cd->vtblInterfaces->length - 1); |
| 497 |
tree arrtype = build_array_type (vtbl_interface_type_node, |
| 498 |
build_index_type (domain)); |
| 499 |
return build_padded_constructor (arrtype, elms); |
| 500 |
} |
| 501 |
|
| 502 |
/* Write out the interfacing vtable[] of base class BCD that will be accessed |
| 503 |
from the overriding class CD. If both are the same class, then this will |
| 504 |
be its own vtable. INDEX is the offset in the interfaces array of the |
| 505 |
base class where the Interface reference can be found. |
| 506 |
This must be mirrored with base_vtable_offset(). */ |
| 507 |
|
| 508 |
void layout_base_vtable (ClassDeclaration *cd, ClassDeclaration *bcd, |
| 509 |
size_t index) |
| 510 |
{ |
| 511 |
BaseClass *bs = (*bcd->vtblInterfaces)[index]; |
| 512 |
ClassDeclaration *id = bs->sym; |
| 513 |
vec<constructor_elt, va_gc> *elms = NULL; |
| 514 |
FuncDeclarations bvtbl; |
| 515 |
|
| 516 |
if (id->vtbl.length == 0 || base_vtable_offset (cd, bs) == ~0u) |
| 517 |
return; |
| 518 |
|
| 519 |
/* Fill bvtbl with the functions we want to put out. */ |
| 520 |
if (cd != bcd && !bs->fillVtbl (cd, &bvtbl, 0)) |
| 521 |
return; |
| 522 |
|
| 523 |
/* First entry is struct Interface reference. */ |
| 524 |
if (id->vtblOffset ()) |
| 525 |
{ |
| 526 |
size_t offset = int_size_in_bytes (tinfo_types[TK_CLASSINFO_TYPE]); |
| 527 |
offset += (index * int_size_in_bytes (vtbl_interface_type_node)); |
| 528 |
tree value = build_offset (build_address (get_classinfo_decl (bcd)), |
| 529 |
size_int (offset)); |
| 530 |
CONSTRUCTOR_APPEND_ELT (elms, size_zero_node, value); |
| 531 |
} |
| 532 |
|
| 533 |
for (size_t i = id->vtblOffset () ? 1 : 0; i < id->vtbl.length; i++) |
| 534 |
{ |
| 535 |
FuncDeclaration *fd = (cd == bcd) ? bs->vtbl[i] : bvtbl[i]; |
| 536 |
if (fd != NULL) |
| 537 |
{ |
| 538 |
tree value = build_address (make_thunk (fd, bs->offset)); |
| 539 |
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
tree vtbldomain = build_index_type (size_int (id->vtbl.length - 1)); |
| 544 |
tree vtbltype = build_array_type (vtable_entry_type, vtbldomain); |
| 545 |
tree value = build_padded_constructor (vtbltype, elms); |
| 546 |
this->layout_field (value); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
public: |
| 551 |
TypeInfoVisitor (tree decl) |
| 552 |
{ |
| 553 |
this->decl_ = decl; |
| 554 |
this->init_ = NULL; |
| 555 |
} |
| 556 |
|
| 557 |
/* Return the completed constructor for the TypeInfo record. */ |
| 558 |
|
| 559 |
tree result (void) |
| 560 |
{ |
| 561 |
return build_struct_literal (TREE_TYPE (this->decl_), this->init_); |
| 562 |
} |
| 563 |
|
| 564 |
/* Layout of TypeInfo is: |
| 565 |
void **__vptr; |
| 566 |
void *__monitor; */ |
| 567 |
|
| 568 |
void visit (TypeInfoDeclaration *) final override |
| 569 |
{ |
| 570 |
/* The vtable for TypeInfo. */ |
| 571 |
this->layout_base (Type::dtypeinfo); |
| 572 |
} |
| 573 |
|
| 574 |
/* Layout of TypeInfo_Const is: |
| 575 |
void **__vptr; |
| 576 |
void *__monitor; |
| 577 |
TypeInfo base; */ |
| 578 |
|
| 579 |
void visit (TypeInfoConstDeclaration *d) final override |
| 580 |
{ |
| 581 |
Type *tm = dmd::mutableOf (d->tinfo); |
| 582 |
tm = dmd::merge2 (tm); |
| 583 |
|
| 584 |
/* The vtable for TypeInfo_Const. */ |
| 585 |
this->layout_base (Type::typeinfoconst); |
| 586 |
|
| 587 |
/* TypeInfo for the mutable type. */ |
| 588 |
this->layout_field (build_typeinfo (d->loc, tm)); |
| 589 |
} |
| 590 |
|
| 591 |
/* Layout of TypeInfo_Immutable is: |
| 592 |
void **__vptr; |
| 593 |
void *__monitor; |
| 594 |
TypeInfo base; */ |
| 595 |
|
| 596 |
void visit (TypeInfoInvariantDeclaration *d) final override |
| 597 |
{ |
| 598 |
Type *tm = dmd::mutableOf (d->tinfo); |
| 599 |
tm = dmd::merge2 (tm); |
| 600 |
|
| 601 |
/* The vtable for TypeInfo_Invariant. */ |
| 602 |
this->layout_base (Type::typeinfoinvariant); |
| 603 |
|
| 604 |
/* TypeInfo for the mutable type. */ |
| 605 |
this->layout_field (build_typeinfo (d->loc, tm)); |
| 606 |
} |
| 607 |
|
| 608 |
/* Layout of TypeInfo_Shared is: |
| 609 |
void **__vptr; |
| 610 |
void *__monitor; |
| 611 |
TypeInfo base; */ |
| 612 |
|
| 613 |
void visit (TypeInfoSharedDeclaration *d) final override |
| 614 |
{ |
| 615 |
Type *tm = dmd::unSharedOf (d->tinfo); |
| 616 |
tm = dmd::merge2 (tm); |
| 617 |
|
| 618 |
/* The vtable for TypeInfo_Shared. */ |
| 619 |
this->layout_base (Type::typeinfoshared); |
| 620 |
|
| 621 |
/* TypeInfo for the unshared type. */ |
| 622 |
this->layout_field (build_typeinfo (d->loc, tm)); |
| 623 |
} |
| 624 |
|
| 625 |
/* Layout of TypeInfo_Inout is: |
| 626 |
void **__vptr; |
| 627 |
void *__monitor; |
| 628 |
TypeInfo base; */ |
| 629 |
|
| 630 |
void visit (TypeInfoWildDeclaration *d) final override |
| 631 |
{ |
| 632 |
Type *tm = dmd::mutableOf (d->tinfo); |
| 633 |
tm = dmd::merge2 (tm); |
| 634 |
|
| 635 |
/* The vtable for TypeInfo_Inout. */ |
| 636 |
this->layout_base (Type::typeinfowild); |
| 637 |
|
| 638 |
/* TypeInfo for the mutable type. */ |
| 639 |
this->layout_field (build_typeinfo (d->loc, tm)); |
| 640 |
} |
| 641 |
|
| 642 |
/* Layout of TypeInfo_Enum is: |
| 643 |
void **__vptr; |
| 644 |
void *__monitor; |
| 645 |
TypeInfo base; |
| 646 |
string name; |
| 647 |
void[] m_init; */ |
| 648 |
|
| 649 |
void visit (TypeInfoEnumDeclaration *d) final override |
| 650 |
{ |
| 651 |
TypeEnum *ti = d->tinfo->isTypeEnum (); |
| 652 |
EnumDeclaration *ed = ti->sym; |
| 653 |
|
| 654 |
/* The vtable for TypeInfo_Enum. */ |
| 655 |
this->layout_base (Type::typeinfoenum); |
| 656 |
|
| 657 |
/* TypeInfo for enum members. */ |
| 658 |
tree memtype = (ed->memtype) ? build_typeinfo (d->loc, ed->memtype) |
| 659 |
: null_pointer_node; |
| 660 |
this->layout_field (memtype); |
| 661 |
|
| 662 |
/* Name of the enum declaration. */ |
| 663 |
this->layout_string (ed->toPrettyChars ()); |
| 664 |
|
| 665 |
/* Default initializer for enum. */ |
| 666 |
if (ed->members && !dmd::isZeroInit (d->tinfo)) |
| 667 |
{ |
| 668 |
tree length = size_int (dmd::size (ed->type)); |
| 669 |
tree ptr = build_address (enum_initializer_decl (ed)); |
| 670 |
this->layout_field (d_array_value (array_type_node, length, ptr)); |
| 671 |
} |
| 672 |
else |
| 673 |
this->layout_field (null_array_node); |
| 674 |
} |
| 675 |
|
| 676 |
/* Layout of TypeInfo_Pointer is: |
| 677 |
void **__vptr; |
| 678 |
void *__monitor; |
| 679 |
TypeInfo m_next; */ |
| 680 |
|
| 681 |
void visit (TypeInfoPointerDeclaration *d) final override |
| 682 |
{ |
| 683 |
TypePointer *ti = d->tinfo->isTypePointer (); |
| 684 |
|
| 685 |
/* The vtable for TypeInfo_Pointer. */ |
| 686 |
this->layout_base (Type::typeinfopointer); |
| 687 |
|
| 688 |
/* TypeInfo for pointer-to type. */ |
| 689 |
this->layout_field (build_typeinfo (d->loc, ti->next)); |
| 690 |
} |
| 691 |
|
| 692 |
/* Layout of TypeInfo_Array is: |
| 693 |
void **__vptr; |
| 694 |
void *__monitor; |
| 695 |
TypeInfo value; */ |
| 696 |
|
| 697 |
void visit (TypeInfoArrayDeclaration *d) final override |
| 698 |
{ |
| 699 |
TypeDArray *ti = d->tinfo->isTypeDArray (); |
| 700 |
|
| 701 |
/* The vtable for TypeInfo_Array. */ |
| 702 |
this->layout_base (Type::typeinfoarray); |
| 703 |
|
| 704 |
/* TypeInfo for array of type. */ |
| 705 |
this->layout_field (build_typeinfo (d->loc, ti->next)); |
| 706 |
} |
| 707 |
|
| 708 |
/* Layout of TypeInfo_StaticArray is: |
| 709 |
void **__vptr; |
| 710 |
void *__monitor; |
| 711 |
TypeInfo value; |
| 712 |
size_t len; */ |
| 713 |
|
| 714 |
void visit (TypeInfoStaticArrayDeclaration *d) final override |
| 715 |
{ |
| 716 |
TypeSArray *ti = d->tinfo->isTypeSArray (); |
| 717 |
|
| 718 |
/* The vtable for TypeInfo_StaticArray. */ |
| 719 |
this->layout_base (Type::typeinfostaticarray); |
| 720 |
|
| 721 |
/* TypeInfo for array of type. */ |
| 722 |
this->layout_field (build_typeinfo (d->loc, ti->next)); |
| 723 |
|
| 724 |
/* Static array length. */ |
| 725 |
this->layout_field (size_int (ti->dim->toInteger ())); |
| 726 |
} |
| 727 |
|
| 728 |
/* Layout of TypeInfo_AssociativeArray is: |
| 729 |
void **__vptr; |
| 730 |
void *__monitor; |
| 731 |
TypeInfo value; |
| 732 |
TypeInfo key; |
| 733 |
TypeInfo entry; */ |
| 734 |
|
| 735 |
void visit (TypeInfoAssociativeArrayDeclaration *d) final override |
| 736 |
{ |
| 737 |
TypeAArray *ti = d->tinfo->isTypeAArray (); |
| 738 |
|
| 739 |
/* The vtable for TypeInfo_AssociativeArray. */ |
| 740 |
this->layout_base (Type::typeinfoassociativearray); |
| 741 |
|
| 742 |
/* TypeInfo for value of type. */ |
| 743 |
this->layout_field (build_typeinfo (d->loc, ti->next)); |
| 744 |
|
| 745 |
/* TypeInfo for index of type. */ |
| 746 |
this->layout_field (build_typeinfo (d->loc, ti->index)); |
| 747 |
|
| 748 |
/* TypeInfo for the key/value pair. */ |
| 749 |
if (d->entry != NULL) |
| 750 |
this->layout_field (build_typeinfo (d->loc, d->entry)); |
| 751 |
else |
| 752 |
this->layout_field (null_pointer_node); |
| 753 |
} |
| 754 |
|
| 755 |
/* Layout of TypeInfo_Vector is: |
| 756 |
void **__vptr; |
| 757 |
void *__monitor; |
| 758 |
TypeInfo base; */ |
| 759 |
|
| 760 |
void visit (TypeInfoVectorDeclaration *d) final override |
| 761 |
{ |
| 762 |
TypeVector *ti = d->tinfo->isTypeVector (); |
| 763 |
|
| 764 |
/* The vtable for TypeInfo_Vector. */ |
| 765 |
this->layout_base (Type::typeinfovector); |
| 766 |
|
| 767 |
/* TypeInfo for equivalent static array. */ |
| 768 |
this->layout_field (build_typeinfo (d->loc, ti->basetype)); |
| 769 |
} |
| 770 |
|
| 771 |
/* Layout of TypeInfo_Function is: |
| 772 |
void **__vptr; |
| 773 |
void *__monitor; |
| 774 |
TypeInfo next; |
| 775 |
string deco; */ |
| 776 |
|
| 777 |
void visit (TypeInfoFunctionDeclaration *d) final override |
| 778 |
{ |
| 779 |
TypeFunction *ti = d->tinfo->isTypeFunction (); |
| 780 |
gcc_assert (ti->deco != NULL); |
| 781 |
|
| 782 |
/* The vtable for TypeInfo_Function. */ |
| 783 |
this->layout_base (Type::typeinfofunction); |
| 784 |
|
| 785 |
/* TypeInfo for function return value. */ |
| 786 |
this->layout_field (build_typeinfo (d->loc, ti->next)); |
| 787 |
|
| 788 |
/* Mangled name of function declaration. */ |
| 789 |
this->layout_string (d->tinfo->deco); |
| 790 |
} |
| 791 |
|
| 792 |
/* Layout of TypeInfo_Delegate is: |
| 793 |
void **__vptr; |
| 794 |
void *__monitor; |
| 795 |
TypeInfo next; |
| 796 |
string deco; */ |
| 797 |
|
| 798 |
void visit (TypeInfoDelegateDeclaration *d) final override |
| 799 |
{ |
| 800 |
TypeDelegate *ti = d->tinfo->isTypeDelegate (); |
| 801 |
gcc_assert (ti->deco != NULL); |
| 802 |
|
| 803 |
/* The vtable for TypeInfo_Delegate. */ |
| 804 |
this->layout_base (Type::typeinfodelegate); |
| 805 |
|
| 806 |
/* TypeInfo for delegate return value. */ |
| 807 |
this->layout_field (build_typeinfo (d->loc, ti->next)); |
| 808 |
|
| 809 |
/* Mangled name of delegate declaration. */ |
| 810 |
this->layout_string (d->tinfo->deco); |
| 811 |
} |
| 812 |
|
| 813 |
/* Layout of ClassInfo/TypeInfo_Class is: |
| 814 |
void **__vptr; |
| 815 |
void *__monitor; |
| 816 |
byte[] m_init; |
| 817 |
string name; |
| 818 |
void*[] vtbl; |
| 819 |
Interface[] interfaces; |
| 820 |
TypeInfo_Class base; |
| 821 |
void *destructor; |
| 822 |
void function(Object) classInvariant; |
| 823 |
ClassFlags m_flags; |
| 824 |
ushort depth; |
| 825 |
void *deallocator; |
| 826 |
OffsetTypeInfo[] m_offTi; |
| 827 |
void function(Object) defaultConstructor; |
| 828 |
ulong[2] nameSig |
| 829 |
immutable(void)* m_RTInfo; |
| 830 |
|
| 831 |
Information relating to interfaces, and their vtables are laid out |
| 832 |
immediately after the named fields, if there is anything to write. */ |
| 833 |
|
| 834 |
void visit (TypeInfoClassDeclaration *d) final override |
| 835 |
{ |
| 836 |
TypeClass *ti = d->tinfo->isTypeClass (); |
| 837 |
ClassDeclaration *cd = ti->sym; |
| 838 |
|
| 839 |
/* The vtable for ClassInfo. */ |
| 840 |
this->layout_base (Type::typeinfoclass); |
| 841 |
|
| 842 |
if (!cd->members) |
| 843 |
return; |
| 844 |
|
| 845 |
tree interfaces = NULL_TREE; |
| 846 |
|
| 847 |
if (!cd->isInterfaceDeclaration ()) |
| 848 |
{ |
| 849 |
/* Default initializer for class. */ |
| 850 |
tree init = aggregate_initializer_decl (cd); |
| 851 |
tree value = d_array_value (array_type_node, size_int (cd->structsize), |
| 852 |
build_address (init)); |
| 853 |
this->layout_field (value); |
| 854 |
|
| 855 |
/* Name of the class declaration. */ |
| 856 |
const char *name = cd->ident->toChars (); |
| 857 |
if (!(strlen (name) > 9 && memcmp (name, "TypeInfo_", 9) == 0)) |
| 858 |
name = cd->toPrettyChars (true); |
| 859 |
this->layout_string (name); |
| 860 |
|
| 861 |
/* The vtable of the class declaration. */ |
| 862 |
value = d_array_value (array_type_node, size_int (cd->vtbl.length), |
| 863 |
build_address (get_vtable_decl (cd))); |
| 864 |
this->layout_field (value); |
| 865 |
|
| 866 |
/* Array of base interfaces that have their own vtable. */ |
| 867 |
if (cd->vtblInterfaces->length) |
| 868 |
interfaces = this->layout_interfaces (cd); |
| 869 |
else |
| 870 |
this->layout_field (null_array_node); |
| 871 |
|
| 872 |
/* TypeInfo_Class base; */ |
| 873 |
tree base = (cd->baseClass) |
| 874 |
? build_address (get_classinfo_decl (cd->baseClass)) |
| 875 |
: null_pointer_node; |
| 876 |
this->layout_field (base); |
| 877 |
|
| 878 |
/* void *destructor; */ |
| 879 |
tree dtor = (cd->tidtor) ? build_address (get_symbol_decl (cd->tidtor)) |
| 880 |
: null_pointer_node; |
| 881 |
this->layout_field (dtor); |
| 882 |
|
| 883 |
/* void function(Object) classInvariant; */ |
| 884 |
tree inv = (cd->inv) ? build_address (get_symbol_decl (cd->inv)) |
| 885 |
: null_pointer_node; |
| 886 |
this->layout_field (inv); |
| 887 |
|
| 888 |
/* ClassFlags m_flags; */ |
| 889 |
int flags = ClassFlags::hasOffTi; |
| 890 |
if (cd->isCOMclass ()) |
| 891 |
flags |= ClassFlags::isCOMclass; |
| 892 |
|
| 893 |
if (cd->isCPPclass ()) |
| 894 |
flags |= ClassFlags::isCPPclass; |
| 895 |
|
| 896 |
flags |= ClassFlags::hasGetMembers; |
| 897 |
flags |= ClassFlags::hasTypeInfo; |
| 898 |
|
| 899 |
if (cd->ctor) |
| 900 |
flags |= ClassFlags::hasCtor; |
| 901 |
|
| 902 |
for (ClassDeclaration *bcd = cd; bcd; bcd = bcd->baseClass) |
| 903 |
{ |
| 904 |
if (bcd->dtor) |
| 905 |
{ |
| 906 |
flags |= ClassFlags::hasDtor; |
| 907 |
break; |
| 908 |
} |
| 909 |
} |
| 910 |
|
| 911 |
if (cd->isAbstract ()) |
| 912 |
flags |= ClassFlags::isAbstract; |
| 913 |
|
| 914 |
for (ClassDeclaration *bcd = cd; bcd; bcd = bcd->baseClass) |
| 915 |
{ |
| 916 |
if (!bcd->members) |
| 917 |
continue; |
| 918 |
|
| 919 |
for (size_t i = 0; i < bcd->members->length; i++) |
| 920 |
{ |
| 921 |
Dsymbol *sm = (*bcd->members)[i]; |
| 922 |
if (sm->hasPointers ()) |
| 923 |
goto Lhaspointers; |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
flags |= ClassFlags::noPointers; |
| 928 |
|
| 929 |
Lhaspointers: |
| 930 |
this->layout_field (build_integer_cst (flags, d_ushort_type)); |
| 931 |
|
| 932 |
/* ushort depth; (not implemented) */ |
| 933 |
this->layout_field (build_zero_cst (d_ushort_type)); |
| 934 |
|
| 935 |
/* void *deallocator; */ |
| 936 |
this->layout_field (null_pointer_node); |
| 937 |
|
| 938 |
/* OffsetTypeInfo[] m_offTi; (not implemented) */ |
| 939 |
this->layout_field (null_array_node); |
| 940 |
|
| 941 |
/* void function(Object) defaultConstructor; */ |
| 942 |
if (cd->defaultCtor && !(cd->defaultCtor->storage_class & STCdisable)) |
| 943 |
{ |
| 944 |
tree dctor = get_symbol_decl (cd->defaultCtor); |
| 945 |
this->layout_field (build_address (dctor)); |
| 946 |
} |
| 947 |
else |
| 948 |
this->layout_field (null_pointer_node); |
| 949 |
|
| 950 |
/* immutable(void)* m_RTInfo; */ |
| 951 |
if (cd->getRTInfo) |
| 952 |
this->layout_field (build_expr (cd->getRTInfo, true)); |
| 953 |
else if (!(flags & ClassFlags::noPointers)) |
| 954 |
this->layout_field (size_one_node); |
| 955 |
else |
| 956 |
this->layout_field (null_pointer_node); |
| 957 |
|
| 958 |
/* uint[4] nameSig; */ |
| 959 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 960 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 961 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 962 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 963 |
} |
| 964 |
else |
| 965 |
{ |
| 966 |
/* No initializer for interface. */ |
| 967 |
this->layout_field (null_array_node); |
| 968 |
|
| 969 |
/* Name of the interface declaration. */ |
| 970 |
this->layout_string (cd->toPrettyChars (true)); |
| 971 |
|
| 972 |
/* No vtable for interface declaration. */ |
| 973 |
this->layout_field (null_array_node); |
| 974 |
|
| 975 |
/* Array of base interfaces that have their own vtable. */ |
| 976 |
if (cd->vtblInterfaces->length) |
| 977 |
interfaces = this->layout_interfaces (cd); |
| 978 |
else |
| 979 |
this->layout_field (null_array_node); |
| 980 |
|
| 981 |
/* TypeInfo_Class base; |
| 982 |
void *destructor; |
| 983 |
void function(Object) classInvariant; */ |
| 984 |
this->layout_field (null_pointer_node); |
| 985 |
this->layout_field (null_pointer_node); |
| 986 |
this->layout_field (null_pointer_node); |
| 987 |
|
| 988 |
/* ClassFlags m_flags; */ |
| 989 |
int flags = ClassFlags::hasOffTi; |
| 990 |
flags |= ClassFlags::hasTypeInfo; |
| 991 |
if (cd->isCOMinterface ()) |
| 992 |
flags |= ClassFlags::isCOMclass; |
| 993 |
|
| 994 |
this->layout_field (build_integer_cst (flags, d_ushort_type)); |
| 995 |
|
| 996 |
/* ushort depth; (not implemented) */ |
| 997 |
this->layout_field (build_zero_cst (d_ushort_type)); |
| 998 |
|
| 999 |
/* void *deallocator; |
| 1000 |
OffsetTypeInfo[] m_offTi; (not implemented) |
| 1001 |
void function(Object) defaultConstructor; */ |
| 1002 |
this->layout_field (null_pointer_node); |
| 1003 |
this->layout_field (null_array_node); |
| 1004 |
this->layout_field (null_pointer_node); |
| 1005 |
|
| 1006 |
/* immutable(void)* m_RTInfo; */ |
| 1007 |
if (cd->getRTInfo) |
| 1008 |
this->layout_field (build_expr (cd->getRTInfo, true)); |
| 1009 |
else |
| 1010 |
this->layout_field (null_pointer_node); |
| 1011 |
|
| 1012 |
/* uint[4] nameSig; */ |
| 1013 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 1014 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 1015 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 1016 |
this->layout_field (build_zero_cst (d_uint_type)); |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* Put out array of Interfaces. */ |
| 1020 |
if (interfaces != NULL_TREE) |
| 1021 |
this->layout_field (interfaces); |
| 1022 |
|
| 1023 |
if (!cd->isInterfaceDeclaration ()) |
| 1024 |
{ |
| 1025 |
/* Put out this class' interface vtables[]. */ |
| 1026 |
for (size_t i = 0; i < cd->vtblInterfaces->length; i++) |
| 1027 |
this->layout_base_vtable (cd, cd, i); |
| 1028 |
|
| 1029 |
/* Put out the overriding interface vtables[]. */ |
| 1030 |
for (ClassDeclaration *bcd = cd->baseClass; bcd; bcd = bcd->baseClass) |
| 1031 |
{ |
| 1032 |
for (size_t i = 0; i < bcd->vtblInterfaces->length; i++) |
| 1033 |
this->layout_base_vtable (cd, bcd, i); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* Layout of TypeInfo_Interface is: |
| 1039 |
void **__vptr; |
| 1040 |
void *__monitor; |
| 1041 |
TypeInfo_Class info; */ |
| 1042 |
|
| 1043 |
void visit (TypeInfoInterfaceDeclaration *d) final override |
| 1044 |
{ |
| 1045 |
TypeClass *ti = d->tinfo->isTypeClass (); |
| 1046 |
|
| 1047 |
if (!ti->sym->vclassinfo) |
| 1048 |
ti->sym->vclassinfo = TypeInfoClassDeclaration::create (ti); |
| 1049 |
|
| 1050 |
/* The vtable for TypeInfo_Interface. */ |
| 1051 |
this->layout_base (Type::typeinfointerface); |
| 1052 |
|
| 1053 |
/* TypeInfo for class inheriting the interface. */ |
| 1054 |
tree tidecl = get_typeinfo_decl (ti->sym->vclassinfo); |
| 1055 |
this->layout_field (build_address (tidecl)); |
| 1056 |
} |
| 1057 |
|
| 1058 |
/* Layout of TypeInfo_Struct is: |
| 1059 |
void **__vptr; |
| 1060 |
void *__monitor; |
| 1061 |
string mangledName; |
| 1062 |
void[] m_init; |
| 1063 |
hash_t function(in void*) xtoHash; |
| 1064 |
bool function(in void*, in void*) xopEquals; |
| 1065 |
int function(in void*, in void*) xopCmp; |
| 1066 |
string function(const(void)*) xtoString; |
| 1067 |
StructFlags m_flags; |
| 1068 |
void function(void*) xdtor; |
| 1069 |
void function(void*) xpostblit; |
| 1070 |
uint m_align; |
| 1071 |
immutable(void)* xgetRTInfo; */ |
| 1072 |
|
| 1073 |
void visit (TypeInfoStructDeclaration *d) final override |
| 1074 |
{ |
| 1075 |
TypeStruct *ti = d->tinfo->isTypeStruct (); |
| 1076 |
StructDeclaration *sd = ti->sym; |
| 1077 |
|
| 1078 |
/* The vtable for TypeInfo_Struct. */ |
| 1079 |
this->layout_base (Type::typeinfostruct); |
| 1080 |
|
| 1081 |
if (!sd->members) |
| 1082 |
return; |
| 1083 |
|
| 1084 |
/* Mangled name of the struct declaration. */ |
| 1085 |
this->layout_string (ti->deco); |
| 1086 |
|
| 1087 |
/* Default initializer for struct. */ |
| 1088 |
tree ptr = (sd->zeroInit ()) ? null_pointer_node |
| 1089 |
: build_address (aggregate_initializer_decl (sd)); |
| 1090 |
this->layout_field (d_array_value (array_type_node, |
| 1091 |
size_int (sd->structsize), ptr)); |
| 1092 |
|
| 1093 |
/* hash_t function (in void*) xtoHash; */ |
| 1094 |
tree xhash = (sd->xhash) ? build_address (get_symbol_decl (sd->xhash)) |
| 1095 |
: null_pointer_node; |
| 1096 |
this->layout_field (xhash); |
| 1097 |
|
| 1098 |
/* bool function(in void*, in void*) xopEquals; */ |
| 1099 |
tree xeq = (sd->xeq) ? build_address (get_symbol_decl (sd->xeq)) |
| 1100 |
: null_pointer_node; |
| 1101 |
this->layout_field (xeq); |
| 1102 |
|
| 1103 |
/* int function(in void*, in void*) xopCmp; */ |
| 1104 |
tree xcmp = (sd->xcmp) ? build_address (get_symbol_decl (sd->xcmp)) |
| 1105 |
: null_pointer_node; |
| 1106 |
this->layout_field (xcmp); |
| 1107 |
|
| 1108 |
/* string function(const(void)*) xtoString; */ |
| 1109 |
FuncDeclaration *fdx = dmd::search_toString (sd); |
| 1110 |
if (fdx) |
| 1111 |
this->layout_field (build_address (get_symbol_decl (fdx))); |
| 1112 |
else |
| 1113 |
this->layout_field (null_pointer_node); |
| 1114 |
|
| 1115 |
/* StructFlags m_flags; */ |
| 1116 |
int m_flags = StructFlags::none; |
| 1117 |
if (dmd::hasPointers (ti)) |
| 1118 |
m_flags |= StructFlags::hasPointers; |
| 1119 |
this->layout_field (build_integer_cst (m_flags, d_uint_type)); |
| 1120 |
|
| 1121 |
/* void function(void*) xdtor; */ |
| 1122 |
tree dtor = (sd->tidtor) ? build_address (get_symbol_decl (sd->tidtor)) |
| 1123 |
: null_pointer_node; |
| 1124 |
this->layout_field (dtor); |
| 1125 |
|
| 1126 |
/* void function(void*) xpostblit; */ |
| 1127 |
if (sd->postblit && !(sd->postblit->storage_class & STCdisable)) |
| 1128 |
this->layout_field (build_address (get_symbol_decl (sd->postblit))); |
| 1129 |
else |
| 1130 |
this->layout_field (null_pointer_node); |
| 1131 |
|
| 1132 |
/* uint m_align; */ |
| 1133 |
this->layout_field (build_integer_cst (ti->alignsize (), d_uint_type)); |
| 1134 |
|
| 1135 |
/* immutable(void)* xgetRTInfo; */ |
| 1136 |
if (sd->getRTInfo) |
| 1137 |
this->layout_field (build_expr (sd->getRTInfo, true)); |
| 1138 |
else if (m_flags & StructFlags::hasPointers) |
| 1139 |
this->layout_field (size_one_node); |
| 1140 |
} |
| 1141 |
|
| 1142 |
/* Layout of TypeInfo_Tuple is: |
| 1143 |
void **__vptr; |
| 1144 |
void *__monitor; |
| 1145 |
TypeInfo[] elements; */ |
| 1146 |
|
| 1147 |
void visit (TypeInfoTupleDeclaration *d) final override |
| 1148 |
{ |
| 1149 |
TypeTuple *ti = d->tinfo->isTypeTuple (); |
| 1150 |
|
| 1151 |
/* The vtable for TypeInfo_Tuple. */ |
| 1152 |
this->layout_base (Type::typeinfotypelist); |
| 1153 |
|
| 1154 |
/* TypeInfo[] elements; */ |
| 1155 |
Type *satype = dmd::sarrayOf (Type::tvoidptr, ti->arguments->length); |
| 1156 |
vec<constructor_elt, va_gc> *elms = NULL; |
| 1157 |
for (size_t i = 0; i < ti->arguments->length; i++) |
| 1158 |
{ |
| 1159 |
Parameter *arg = (*ti->arguments)[i]; |
| 1160 |
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), |
| 1161 |
build_typeinfo (d->loc, arg->type)); |
| 1162 |
} |
| 1163 |
tree ctor = build_padded_constructor (build_ctype (satype), elms); |
| 1164 |
tree decl = this->internal_reference (ctor); |
| 1165 |
|
| 1166 |
tree length = size_int (ti->arguments->length); |
| 1167 |
tree ptr = build_address (decl); |
| 1168 |
this->layout_field (d_array_value (array_type_node, length, ptr)); |
| 1169 |
|
| 1170 |
rest_of_decl_compilation (decl, 1, 0); |
| 1171 |
} |
| 1172 |
}; |
| 1173 |
|
| 1174 |
|
| 1175 |
/* Main entry point for TypeInfoVisitor interface to generate |
| 1176 |
TypeInfo constructor for the TypeInfoDeclaration AST class D. */ |
| 1177 |
|
| 1178 |
tree |
| 1179 |
layout_typeinfo (TypeInfoDeclaration *d) |
| 1180 |
{ |
| 1181 |
if (!Type::dtypeinfo) |
| 1182 |
create_frontend_tinfo_types (); |
| 1183 |
|
| 1184 |
TypeInfoVisitor v = TypeInfoVisitor (get_typeinfo_decl (d)); |
| 1185 |
d->accept (&v); |
| 1186 |
return v.result (); |
| 1187 |
} |
| 1188 |
|
| 1189 |
/* Like layout_typeinfo, but generates the TypeInfo_Class for |
| 1190 |
the class or interface declaration CD. */ |
| 1191 |
|
| 1192 |
tree |
| 1193 |
layout_classinfo (ClassDeclaration *cd) |
| 1194 |
{ |
| 1195 |
if (!Type::dtypeinfo) |
| 1196 |
create_frontend_tinfo_types (); |
| 1197 |
|
| 1198 |
TypeInfoClassDeclaration *d = TypeInfoClassDeclaration::create (cd->type); |
| 1199 |
TypeInfoVisitor v = TypeInfoVisitor (get_classinfo_decl (cd)); |
| 1200 |
d->accept (&v); |
| 1201 |
return v.result (); |
| 1202 |
} |
| 1203 |
|
| 1204 |
/* Get the offset to the BC's vtbl[] initializer from the start of CD. |
| 1205 |
Returns "~0u" if the base class is not found in any vtable interfaces. */ |
| 1206 |
|
| 1207 |
unsigned |
| 1208 |
base_vtable_offset (ClassDeclaration *cd, BaseClass *bc) |
| 1209 |
{ |
| 1210 |
unsigned csymoffset = int_size_in_bytes (tinfo_types[TK_CLASSINFO_TYPE]); |
| 1211 |
unsigned interfacesize = int_size_in_bytes (vtbl_interface_type_node); |
| 1212 |
csymoffset += cd->vtblInterfaces->length * interfacesize; |
| 1213 |
|
| 1214 |
for (size_t i = 0; i < cd->vtblInterfaces->length; i++) |
| 1215 |
{ |
| 1216 |
BaseClass *b = (*cd->vtblInterfaces)[i]; |
| 1217 |
if (b == bc) |
| 1218 |
return csymoffset; |
| 1219 |
csymoffset += b->sym->vtbl.length * target.ptrsize; |
| 1220 |
} |
| 1221 |
|
| 1222 |
/* Check all overriding interface vtbl[]s. */ |
| 1223 |
for (ClassDeclaration *cd2 = cd->baseClass; cd2; cd2 = cd2->baseClass) |
| 1224 |
{ |
| 1225 |
for (size_t k = 0; k < cd2->vtblInterfaces->length; k++) |
| 1226 |
{ |
| 1227 |
BaseClass *bs = (*cd2->vtblInterfaces)[k]; |
| 1228 |
if (bs->fillVtbl (cd, NULL, 0)) |
| 1229 |
{ |
| 1230 |
if (bc == bs) |
| 1231 |
return csymoffset; |
| 1232 |
csymoffset += bs->sym->vtbl.length * target.ptrsize; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
} |
| 1236 |
|
| 1237 |
return ~0u; |
| 1238 |
} |
| 1239 |
|
| 1240 |
/* Layout fields that immediately come after the classinfo type for DECL if |
| 1241 |
there's any interfaces or interface vtables to be added. |
| 1242 |
This must be mirrored with base_vtable_offset(). */ |
| 1243 |
|
| 1244 |
static tree |
| 1245 |
layout_classinfo_interfaces (ClassDeclaration *decl) |
| 1246 |
{ |
| 1247 |
tree type = tinfo_types[TK_CLASSINFO_TYPE]; |
| 1248 |
size_t structsize = int_size_in_bytes (type); |
| 1249 |
|
| 1250 |
if (decl->vtblInterfaces->length) |
| 1251 |
{ |
| 1252 |
size_t interfacesize = int_size_in_bytes (vtbl_interface_type_node); |
| 1253 |
tree field; |
| 1254 |
|
| 1255 |
type = copy_aggregate_type (type); |
| 1256 |
|
| 1257 |
/* First layout the static array of Interface, which provides information |
| 1258 |
about the vtables that follow. */ |
| 1259 |
tree domain = size_int (decl->vtblInterfaces->length - 1); |
| 1260 |
tree arrtype = build_array_type (vtbl_interface_type_node, |
| 1261 |
build_index_type (domain)); |
| 1262 |
field = create_field_decl (arrtype, NULL, 1, 1); |
| 1263 |
insert_aggregate_field (type, field, structsize); |
| 1264 |
structsize += decl->vtblInterfaces->length * interfacesize; |
| 1265 |
|
| 1266 |
/* For each interface, layout each vtable. */ |
| 1267 |
for (size_t i = 0; i < decl->vtblInterfaces->length; i++) |
| 1268 |
{ |
| 1269 |
BaseClass *b = (*decl->vtblInterfaces)[i]; |
| 1270 |
ClassDeclaration *id = b->sym; |
| 1271 |
unsigned offset = base_vtable_offset (decl, b); |
| 1272 |
|
| 1273 |
if (id->vtbl.length && offset != ~0u) |
| 1274 |
{ |
| 1275 |
tree vtbldomain |
| 1276 |
= build_index_type (size_int (id->vtbl.length - 1)); |
| 1277 |
tree vtbltype = build_array_type (vtable_entry_type, vtbldomain); |
| 1278 |
|
| 1279 |
field = create_field_decl (vtbltype, NULL, 1, 1); |
| 1280 |
insert_aggregate_field (type, field, offset); |
| 1281 |
structsize += id->vtbl.length * target.ptrsize; |
| 1282 |
} |
| 1283 |
} |
| 1284 |
} |
| 1285 |
|
| 1286 |
/* Layout the arrays of overriding interface vtables. */ |
| 1287 |
for (ClassDeclaration *bcd = decl->baseClass; bcd; bcd = bcd->baseClass) |
| 1288 |
{ |
| 1289 |
for (size_t i = 0; i < bcd->vtblInterfaces->length; i++) |
| 1290 |
{ |
| 1291 |
BaseClass *b = (*bcd->vtblInterfaces)[i]; |
| 1292 |
ClassDeclaration *id = b->sym; |
| 1293 |
unsigned offset = base_vtable_offset (decl, b); |
| 1294 |
|
| 1295 |
if (id->vtbl.length && offset != ~0u) |
| 1296 |
{ |
| 1297 |
if (type == tinfo_types[TK_CLASSINFO_TYPE]) |
| 1298 |
type = copy_aggregate_type (type); |
| 1299 |
|
| 1300 |
tree vtbldomain |
| 1301 |
= build_index_type (size_int (id->vtbl.length - 1)); |
| 1302 |
tree vtbltype = build_array_type (vtable_entry_type, vtbldomain); |
| 1303 |
|
| 1304 |
tree field = create_field_decl (vtbltype, NULL, 1, 1); |
| 1305 |
insert_aggregate_field (type, field, offset); |
| 1306 |
structsize += id->vtbl.length * target.ptrsize; |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
/* Update the type size and record mode for the classinfo type. */ |
| 1312 |
if (type != tinfo_types[TK_CLASSINFO_TYPE]) |
| 1313 |
finish_aggregate_type (structsize, TYPE_ALIGN_UNIT (type), type); |
| 1314 |
|
| 1315 |
return type; |
| 1316 |
} |
| 1317 |
|
| 1318 |
/* Returns true if the TypeInfo for TYPE should be placed in |
| 1319 |
the runtime library. */ |
| 1320 |
|
| 1321 |
static bool |
| 1322 |
builtin_typeinfo_p (Type *type) |
| 1323 |
{ |
| 1324 |
if (type->isTypeBasic () || type->ty == TY::Tclass || type->ty == TY::Tnull) |
| 1325 |
return !type->mod; |
| 1326 |
|
| 1327 |
if (type->ty == TY::Tarray) |
| 1328 |
{ |
| 1329 |
/* Strings are so common, make them builtin. */ |
| 1330 |
Type *next = type->nextOf (); |
| 1331 |
return !type->mod |
| 1332 |
&& ((next->isTypeBasic () != NULL && !next->mod) |
| 1333 |
|| (next->ty == TY::Tchar && next->mod == MODimmutable) |
| 1334 |
|| (next->ty == TY::Tchar && next->mod == MODconst)); |
| 1335 |
} |
| 1336 |
|
| 1337 |
return false; |
| 1338 |
} |
| 1339 |
|
| 1340 |
/* Implements a visitor interface to create the decl tree for TypeInfo decls. |
| 1341 |
TypeInfo_Class objects differ in that they also have information about |
| 1342 |
the class type packed immediately after the TypeInfo symbol. |
| 1343 |
|
| 1344 |
If the frontend had an interface to allow distinguishing being these two |
| 1345 |
AST types, then that would be better for us. */ |
| 1346 |
|
| 1347 |
class TypeInfoDeclVisitor : public Visitor |
| 1348 |
{ |
| 1349 |
using Visitor::visit; |
| 1350 |
|
| 1351 |
public: |
| 1352 |
TypeInfoDeclVisitor (void) |
| 1353 |
{ |
| 1354 |
} |
| 1355 |
|
| 1356 |
void visit (TypeInfoDeclaration *tid) final override |
| 1357 |
{ |
| 1358 |
tree ident = get_identifier (tid->ident->toChars ()); |
| 1359 |
tree type = tinfo_types[get_typeinfo_kind (tid->tinfo)]; |
| 1360 |
gcc_assert (type != NULL_TREE); |
| 1361 |
|
| 1362 |
/* Built-in typeinfo will be referenced as one-only. */ |
| 1363 |
tid->csym = declare_extern_var (ident, type); |
| 1364 |
DECL_LANG_SPECIFIC (tid->csym) = build_lang_decl (tid); |
| 1365 |
|
| 1366 |
DECL_CONTEXT (tid->csym) = d_decl_context (tid); |
| 1367 |
TREE_READONLY (tid->csym) = 1; |
| 1368 |
} |
| 1369 |
|
| 1370 |
void visit (TypeInfoClassDeclaration *tid) final override |
| 1371 |
{ |
| 1372 |
TypeClass *tc = tid->tinfo->isTypeClass (); |
| 1373 |
tid->csym = get_classinfo_decl (tc->sym); |
| 1374 |
} |
| 1375 |
}; |
| 1376 |
|
| 1377 |
/* Get the VAR_DECL of the TypeInfo for DECL. If this does not yet exist, |
| 1378 |
create it. The TypeInfo decl provides information about the type of a given |
| 1379 |
expression or object. */ |
| 1380 |
|
| 1381 |
tree |
| 1382 |
get_typeinfo_decl (TypeInfoDeclaration *decl) |
| 1383 |
{ |
| 1384 |
if (decl->csym) |
| 1385 |
return decl->csym; |
| 1386 |
|
| 1387 |
gcc_assert (decl->tinfo->ty != TY::Terror); |
| 1388 |
|
| 1389 |
TypeInfoDeclVisitor v = TypeInfoDeclVisitor (); |
| 1390 |
decl->accept (&v); |
| 1391 |
gcc_assert (decl->csym != NULL_TREE); |
| 1392 |
|
| 1393 |
return decl->csym; |
| 1394 |
} |
| 1395 |
|
| 1396 |
/* Get the VAR_DECL of the ClassInfo for DECL. If this does not yet exist, |
| 1397 |
create it. The ClassInfo decl provides information about the dynamic type |
| 1398 |
of a given class type or object. */ |
| 1399 |
|
| 1400 |
tree |
| 1401 |
get_classinfo_decl (ClassDeclaration *decl) |
| 1402 |
{ |
| 1403 |
if (decl->csym) |
| 1404 |
return decl->csym; |
| 1405 |
|
| 1406 |
InterfaceDeclaration *id = decl->isInterfaceDeclaration (); |
| 1407 |
tree ident = mangle_internal_decl (decl, id ? "__Interface" : "__Class", "Z"); |
| 1408 |
tree type = layout_classinfo_interfaces (decl); |
| 1409 |
|
| 1410 |
decl->csym = declare_extern_var (ident, type); |
| 1411 |
DECL_LANG_SPECIFIC (decl->csym) = build_lang_decl (NULL); |
| 1412 |
|
| 1413 |
/* Class is a reference, want the record type. */ |
| 1414 |
DECL_CONTEXT (decl->csym) = TREE_TYPE (build_ctype (decl->type)); |
| 1415 |
/* ClassInfo cannot be const data, because we use the monitor on it. */ |
| 1416 |
TREE_READONLY (decl->csym) = 0; |
| 1417 |
|
| 1418 |
return decl->csym; |
| 1419 |
} |
| 1420 |
|
| 1421 |
/* Performs sanity checks on the `object.TypeInfo' type, raising an error if |
| 1422 |
RTTI is disabled, or the type is missing. LOC is the location used for error |
| 1423 |
messages. SC is the context, and EXPR is expression where TypeInfo is |
| 1424 |
required from, if either are set. */ |
| 1425 |
|
| 1426 |
void |
| 1427 |
check_typeinfo_type (const Loc &loc, Scope *sc, Expression *expr) |
| 1428 |
{ |
| 1429 |
if (!global.params.useTypeInfo) |
| 1430 |
{ |
| 1431 |
/* Even when compiling without RTTI we should still be able to evaluate |
| 1432 |
TypeInfo at compile-time, just not at run-time. */ |
| 1433 |
if (!sc || !sc->ctfe ()) |
| 1434 |
{ |
| 1435 |
static int warned = 0; |
| 1436 |
|
| 1437 |
if (expr != NULL) |
| 1438 |
error_at (make_location_t (loc), |
| 1439 |
"expression %qs requires %<object.TypeInfo%> and cannot " |
| 1440 |
"be used with %<-fno-rtti%>", expr->toChars ()); |
| 1441 |
else if (!warned) |
| 1442 |
error_at (make_location_t (loc), |
| 1443 |
"%<object.TypeInfo%> cannot be used with %<-fno-rtti%>"); |
| 1444 |
|
| 1445 |
warned = 1; |
| 1446 |
} |
| 1447 |
} |
| 1448 |
|
| 1449 |
if (Type::dtypeinfo == NULL |
| 1450 |
|| (Type::dtypeinfo->storage_class & STCtemp)) |
| 1451 |
{ |
| 1452 |
/* If TypeInfo has not been declared, warn about each location once. */ |
| 1453 |
static Loc warnloc; |
| 1454 |
|
| 1455 |
if (loc.filename () && !warnloc.equals (loc)) |
| 1456 |
{ |
| 1457 |
error_at (make_location_t (loc), |
| 1458 |
"%<object.TypeInfo%> could not be found, " |
| 1459 |
"but is implicitly used"); |
| 1460 |
warnloc = loc; |
| 1461 |
} |
| 1462 |
} |
| 1463 |
} |
| 1464 |
|
| 1465 |
/* Returns typeinfo reference for TYPE. LOC is the location used for error |
| 1466 |
messages. EXPR is the expression where TypeInfo is required, if set. */ |
| 1467 |
|
| 1468 |
tree |
| 1469 |
build_typeinfo (const Loc &loc, Type *type, Expression *expr) |
| 1470 |
{ |
| 1471 |
gcc_assert (type->ty != TY::Terror); |
| 1472 |
check_typeinfo_type (loc, NULL, expr); |
| 1473 |
create_typeinfo (type, NULL); |
| 1474 |
return build_address (get_typeinfo_decl (type->vtinfo)); |
| 1475 |
} |
| 1476 |
|
| 1477 |
tree build_typeinfo (Expression *expr, Type *type) |
| 1478 |
{ |
| 1479 |
return build_typeinfo (expr->loc, type, expr); |
| 1480 |
} |
| 1481 |
|
| 1482 |
/* Like layout_classinfo, but generates an Object that wraps around a |
| 1483 |
pointer to C++ type_info so it can be distinguished from D TypeInfo. */ |
| 1484 |
|
| 1485 |
void |
| 1486 |
layout_cpp_typeinfo (ClassDeclaration *cd) |
| 1487 |
{ |
| 1488 |
if (!Type::dtypeinfo) |
| 1489 |
create_frontend_tinfo_types (); |
| 1490 |
|
| 1491 |
gcc_assert (cd->isCPPclass ()); |
| 1492 |
|
| 1493 |
tree decl = get_cpp_typeinfo_decl (cd); |
| 1494 |
vec<constructor_elt, va_gc> *init = NULL; |
| 1495 |
|
| 1496 |
/* Use the vtable of __cpp_type_info_ptr, the EH personality routine |
| 1497 |
expects this, as it uses .classinfo identity comparison to test for |
| 1498 |
C++ catch handlers. */ |
| 1499 |
ClassDeclaration *cppti = ClassDeclaration::cpp_type_info_ptr; |
| 1500 |
if (have_typeinfo_p (cppti)) |
| 1501 |
{ |
| 1502 |
tree vptr = get_vtable_decl (cppti); |
| 1503 |
CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, build_address (vptr)); |
| 1504 |
} |
| 1505 |
else |
| 1506 |
CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, null_pointer_node); |
| 1507 |
|
| 1508 |
if (cppti->hasMonitor ()) |
| 1509 |
CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, null_pointer_node); |
| 1510 |
|
| 1511 |
/* Let C++ do the RTTI generation, and just reference the symbol as |
| 1512 |
extern, knowing the underlying type is not required. */ |
| 1513 |
const char *ident = target.cpp.typeInfoMangle (cd); |
| 1514 |
tree typeinfo = declare_extern_var (get_identifier (ident), |
| 1515 |
unknown_type_node); |
| 1516 |
TREE_READONLY (typeinfo) = 1; |
| 1517 |
CONSTRUCTOR_APPEND_ELT (init, NULL_TREE, build_address (typeinfo)); |
| 1518 |
|
| 1519 |
/* Build the initializer and emit. */ |
| 1520 |
DECL_INITIAL (decl) = build_struct_literal (TREE_TYPE (decl), init); |
| 1521 |
d_finish_decl (decl); |
| 1522 |
} |
| 1523 |
|
| 1524 |
/* Get the VAR_DECL of the __cpp_type_info_ptr for DECL. If this does not yet |
| 1525 |
exist, create it. The __cpp_type_info_ptr decl is then initialized with a |
| 1526 |
pointer to the C++ type_info for the given class. */ |
| 1527 |
|
| 1528 |
tree |
| 1529 |
get_cpp_typeinfo_decl (ClassDeclaration *decl) |
| 1530 |
{ |
| 1531 |
gcc_assert (decl->isCPPclass ()); |
| 1532 |
|
| 1533 |
if (decl->cpp_type_info_ptr_sym) |
| 1534 |
return decl->cpp_type_info_ptr_sym; |
| 1535 |
|
| 1536 |
if (!tinfo_types[TK_CPPTI_TYPE]) |
| 1537 |
make_internal_typeinfo (TK_CPPTI_TYPE, |
| 1538 |
Identifier::idPool ("__cpp_type_info_ptr"), |
| 1539 |
ptr_type_node, NULL); |
| 1540 |
|
| 1541 |
tree ident = mangle_internal_decl (decl, "_cpp_type_info_ptr", ""); |
| 1542 |
tree type = tinfo_types[TK_CPPTI_TYPE]; |
| 1543 |
|
| 1544 |
decl->cpp_type_info_ptr_sym = declare_extern_var (ident, type); |
| 1545 |
DECL_LANG_SPECIFIC (decl->cpp_type_info_ptr_sym) = build_lang_decl (NULL); |
| 1546 |
|
| 1547 |
/* Class is a reference, want the record type. */ |
| 1548 |
DECL_CONTEXT (decl->cpp_type_info_ptr_sym) |
| 1549 |
= TREE_TYPE (build_ctype (decl->type)); |
| 1550 |
TREE_READONLY (decl->cpp_type_info_ptr_sym) = 1; |
| 1551 |
|
| 1552 |
/* Layout the initializer and emit the symbol. */ |
| 1553 |
layout_cpp_typeinfo (decl); |
| 1554 |
|
| 1555 |
return decl->cpp_type_info_ptr_sym; |
| 1556 |
} |
| 1557 |
|
| 1558 |
/* Get the exact TypeInfo for TYPE, if it doesn't exist, create it. */ |
| 1559 |
|
| 1560 |
void |
| 1561 |
create_typeinfo (Type *type, Scope *sc) |
| 1562 |
{ |
| 1563 |
if (!Type::dtypeinfo) |
| 1564 |
create_frontend_tinfo_types (); |
| 1565 |
|
| 1566 |
/* Do this since not all Type's are merged. */ |
| 1567 |
Type *t = dmd::merge2 (type); |
| 1568 |
Identifier *ident; |
| 1569 |
|
| 1570 |
if (TypeAArray *ta = t->isTypeAArray ()) |
| 1571 |
t = dmd::makeNakedAssociativeArray (ta); |
| 1572 |
|
| 1573 |
if (!t->vtinfo) |
| 1574 |
{ |
| 1575 |
tinfo_kind tk = get_typeinfo_kind (t); |
| 1576 |
switch (tk) |
| 1577 |
{ |
| 1578 |
case TK_SHARED_TYPE: |
| 1579 |
case TK_CONST_TYPE: |
| 1580 |
case TK_IMMUTABLE_TYPE: |
| 1581 |
case TK_INOUT_TYPE: |
| 1582 |
case TK_POINTER_TYPE: |
| 1583 |
case TK_ARRAY_TYPE: |
| 1584 |
case TK_VECTOR_TYPE: |
| 1585 |
case TK_INTERFACE_TYPE: |
| 1586 |
/* Kinds of TypeInfo that add one extra pointer field. */ |
| 1587 |
if (tk == TK_SHARED_TYPE) |
| 1588 |
{ |
| 1589 |
/* Does both `shared' and `shared const'. */ |
| 1590 |
t->vtinfo = TypeInfoSharedDeclaration::create (t); |
| 1591 |
ident = Identifier::idPool ("TypeInfo_Shared"); |
| 1592 |
} |
| 1593 |
else if (tk == TK_CONST_TYPE) |
| 1594 |
{ |
| 1595 |
t->vtinfo = TypeInfoConstDeclaration::create (t); |
| 1596 |
ident = Identifier::idPool ("TypeInfo_Const"); |
| 1597 |
} |
| 1598 |
else if (tk == TK_IMMUTABLE_TYPE) |
| 1599 |
{ |
| 1600 |
t->vtinfo = TypeInfoInvariantDeclaration::create (t); |
| 1601 |
ident = Identifier::idPool ("TypeInfo_Invariant"); |
| 1602 |
} |
| 1603 |
else if (tk == TK_INOUT_TYPE) |
| 1604 |
{ |
| 1605 |
t->vtinfo = TypeInfoWildDeclaration::create (t); |
| 1606 |
ident = Identifier::idPool ("TypeInfo_Wild"); |
| 1607 |
} |
| 1608 |
else if (tk == TK_POINTER_TYPE) |
| 1609 |
{ |
| 1610 |
t->vtinfo = TypeInfoPointerDeclaration::create (t); |
| 1611 |
ident = Identifier::idPool ("TypeInfo_Pointer"); |
| 1612 |
} |
| 1613 |
else if (tk == TK_ARRAY_TYPE) |
| 1614 |
{ |
| 1615 |
t->vtinfo = TypeInfoArrayDeclaration::create (t); |
| 1616 |
ident = Identifier::idPool ("TypeInfo_Array"); |
| 1617 |
} |
| 1618 |
else if (tk == TK_VECTOR_TYPE) |
| 1619 |
{ |
| 1620 |
t->vtinfo = TypeInfoVectorDeclaration::create (t); |
| 1621 |
ident = Identifier::idPool ("TypeInfo_Vector"); |
| 1622 |
} |
| 1623 |
else if (tk == TK_INTERFACE_TYPE) |
| 1624 |
{ |
| 1625 |
t->vtinfo = TypeInfoInterfaceDeclaration::create (t); |
| 1626 |
ident = Identifier::idPool ("TypeInfo_Interface"); |
| 1627 |
} |
| 1628 |
else |
| 1629 |
gcc_unreachable (); |
| 1630 |
|
| 1631 |
if (!tinfo_types[tk]) |
| 1632 |
make_internal_typeinfo (tk, ident, ptr_type_node, NULL); |
| 1633 |
break; |
| 1634 |
|
| 1635 |
case TK_STATICARRAY_TYPE: |
| 1636 |
if (!tinfo_types[tk]) |
| 1637 |
{ |
| 1638 |
ident = Identifier::idPool ("TypeInfo_StaticArray"); |
| 1639 |
make_internal_typeinfo (tk, ident, ptr_type_node, size_type_node, |
| 1640 |
NULL); |
| 1641 |
} |
| 1642 |
t->vtinfo = TypeInfoStaticArrayDeclaration::create (t); |
| 1643 |
break; |
| 1644 |
|
| 1645 |
case TK_ASSOCIATIVEARRAY_TYPE: |
| 1646 |
if (!tinfo_types[tk]) |
| 1647 |
{ |
| 1648 |
ident = Identifier::idPool ("TypeInfo_AssociativeArray"); |
| 1649 |
make_internal_typeinfo (tk, ident, ptr_type_node, ptr_type_node, |
| 1650 |
ptr_type_node, NULL); |
| 1651 |
} |
| 1652 |
t->vtinfo = sc && have_typeinfo_p (Type::typeinfoassociativearray) |
| 1653 |
? dmd::getTypeInfoAssocArrayDeclaration (t->isTypeAArray (), sc) |
| 1654 |
: TypeInfoAssociativeArrayDeclaration::create (t); |
| 1655 |
break; |
| 1656 |
|
| 1657 |
case TK_STRUCT_TYPE: |
| 1658 |
if (!tinfo_types[tk]) |
| 1659 |
{ |
| 1660 |
ident = Identifier::idPool ("TypeInfo_Struct"); |
| 1661 |
make_internal_typeinfo (tk, ident, |
| 1662 |
array_type_node, array_type_node, |
| 1663 |
ptr_type_node, ptr_type_node, |
| 1664 |
ptr_type_node, ptr_type_node, |
| 1665 |
d_uint_type, ptr_type_node, |
| 1666 |
ptr_type_node, d_uint_type, |
| 1667 |
ptr_type_node, NULL); |
| 1668 |
} |
| 1669 |
t->vtinfo = TypeInfoStructDeclaration::create (t); |
| 1670 |
break; |
| 1671 |
|
| 1672 |
case TK_ENUMERAL_TYPE: |
| 1673 |
if (!tinfo_types[tk]) |
| 1674 |
{ |
| 1675 |
ident = Identifier::idPool ("TypeInfo_Enum"); |
| 1676 |
make_internal_typeinfo (tk, ident, |
| 1677 |
ptr_type_node, array_type_node, |
| 1678 |
array_type_node, NULL); |
| 1679 |
} |
| 1680 |
t->vtinfo = TypeInfoEnumDeclaration::create (t); |
| 1681 |
break; |
| 1682 |
|
| 1683 |
case TK_FUNCTION_TYPE: |
| 1684 |
case TK_DELEGATE_TYPE: |
| 1685 |
/* Functions and delegates share a common TypeInfo layout. */ |
| 1686 |
if (tk == TK_FUNCTION_TYPE) |
| 1687 |
{ |
| 1688 |
t->vtinfo = TypeInfoFunctionDeclaration::create (t); |
| 1689 |
ident = Identifier::idPool ("TypeInfo_Function"); |
| 1690 |
} |
| 1691 |
else if (tk == TK_DELEGATE_TYPE) |
| 1692 |
{ |
| 1693 |
t->vtinfo = TypeInfoDelegateDeclaration::create (t); |
| 1694 |
ident = Identifier::idPool ("TypeInfo_Delegate"); |
| 1695 |
} |
| 1696 |
else |
| 1697 |
gcc_unreachable (); |
| 1698 |
|
| 1699 |
if (!tinfo_types[tk]) |
| 1700 |
make_internal_typeinfo (tk, ident, ptr_type_node, |
| 1701 |
array_type_node, NULL); |
| 1702 |
break; |
| 1703 |
|
| 1704 |
case TK_TYPELIST_TYPE: |
| 1705 |
if (!tinfo_types[tk]) |
| 1706 |
{ |
| 1707 |
ident = Identifier::idPool ("TypeInfo_Tuple"); |
| 1708 |
make_internal_typeinfo (tk, ident, array_type_node, NULL); |
| 1709 |
} |
| 1710 |
t->vtinfo = TypeInfoTupleDeclaration::create (t); |
| 1711 |
break; |
| 1712 |
|
| 1713 |
case TK_CLASSINFO_TYPE: |
| 1714 |
t->vtinfo = TypeInfoClassDeclaration::create (t); |
| 1715 |
break; |
| 1716 |
|
| 1717 |
default: |
| 1718 |
t->vtinfo = TypeInfoDeclaration::create (t); |
| 1719 |
} |
| 1720 |
gcc_assert (t->vtinfo); |
| 1721 |
|
| 1722 |
/* If this has a custom implementation in rt/typeinfo, then |
| 1723 |
do not generate a COMDAT for it. */ |
| 1724 |
if (!builtin_typeinfo_p (t)) |
| 1725 |
{ |
| 1726 |
/* Find module that will go all the way to an object file. */ |
| 1727 |
Module *mod = sc ? sc->_module->importedFrom : NULL; |
| 1728 |
if (mod) |
| 1729 |
mod->members->push (t->vtinfo); |
| 1730 |
else |
| 1731 |
build_decl_tree (t->vtinfo); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
/* Types aren't merged, but we can share the vtinfo's. */ |
| 1735 |
if (!type->vtinfo) |
| 1736 |
type->vtinfo = t->vtinfo; |
| 1737 |
|
| 1738 |
gcc_assert (type->vtinfo != NULL); |
| 1739 |
} |
| 1740 |
|
| 1741 |
#include "gt-d-typeinfo.h" |