| 1 |
/** |
| 2 |
* Struct and union declarations. |
| 3 |
* |
| 4 |
* Specification: $(LINK2 https://dlang.org/spec/struct.html, Structs, Unions) |
| 5 |
* |
| 6 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 7 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 8 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 9 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/dstruct.d, _dstruct.d) |
| 10 |
* Documentation: https://dlang.org/phobos/dmd_dstruct.html |
| 11 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/dstruct.d |
| 12 |
*/ |
| 13 |
|
| 14 |
module dmd.dstruct; |
| 15 |
|
| 16 |
import core.stdc.stdio; |
| 17 |
|
| 18 |
import dmd.aggregate; |
| 19 |
import dmd.arraytypes; |
| 20 |
import dmd.astenums; |
| 21 |
import dmd.attrib; |
| 22 |
import dmd.declaration; |
| 23 |
import dmd.dmodule; |
| 24 |
import dmd.dscope; |
| 25 |
import dmd.dsymbol; |
| 26 |
import dmd.dsymbolsem : search, setFieldOffset; |
| 27 |
import dmd.dtemplate; |
| 28 |
import dmd.errors; |
| 29 |
import dmd.expression; |
| 30 |
import dmd.func; |
| 31 |
import dmd.funcsem; |
| 32 |
import dmd.globals; |
| 33 |
import dmd.id; |
| 34 |
import dmd.identifier; |
| 35 |
import dmd.location; |
| 36 |
import dmd.mtype; |
| 37 |
import dmd.opover; |
| 38 |
import dmd.target; |
| 39 |
import dmd.tokens; |
| 40 |
import dmd.typesem : isZeroInit, merge, size, hasPointers; |
| 41 |
import dmd.typinf; |
| 42 |
import dmd.visitor; |
| 43 |
|
| 44 |
/*************************************** |
| 45 |
* Search sd for a member function of the form: |
| 46 |
* `extern (D) string toString();` |
| 47 |
* Params: |
| 48 |
* sd = struct declaration to search |
| 49 |
* Returns: |
| 50 |
* FuncDeclaration of `toString()` if found, `null` if not |
| 51 |
*/ |
| 52 |
FuncDeclaration search_toString(StructDeclaration sd) |
| 53 |
{ |
| 54 |
Dsymbol s = search_function(sd, Id.tostring); |
| 55 |
FuncDeclaration fd = s ? s.isFuncDeclaration() : null; |
| 56 |
if (fd) |
| 57 |
{ |
| 58 |
__gshared TypeFunction tftostring; |
| 59 |
if (!tftostring) |
| 60 |
{ |
| 61 |
tftostring = new TypeFunction(ParameterList(), Type.tstring, LINK.d); |
| 62 |
tftostring = tftostring.merge().toTypeFunction(); |
| 63 |
} |
| 64 |
fd = fd.overloadExactMatch(tftostring); |
| 65 |
} |
| 66 |
return fd; |
| 67 |
} |
| 68 |
|
| 69 |
enum StructFlags : int |
| 70 |
{ |
| 71 |
none = 0x0, |
| 72 |
hasPointers = 0x1, // NB: should use noPointers as in ClassFlags |
| 73 |
} |
| 74 |
|
| 75 |
/*********************************************************** |
| 76 |
* All `struct` declarations are an instance of this. |
| 77 |
*/ |
| 78 |
extern (C++) class StructDeclaration : AggregateDeclaration |
| 79 |
{ |
| 80 |
FuncDeclarations postblits; // Array of postblit functions |
| 81 |
FuncDeclaration postblit; // aggregate postblit |
| 82 |
|
| 83 |
FuncDeclaration xeq; // TypeInfo_Struct.xopEquals |
| 84 |
FuncDeclaration xcmp; // TypeInfo_Struct.xopCmp |
| 85 |
FuncDeclaration xhash; // TypeInfo_Struct.xtoHash |
| 86 |
extern (C++) __gshared FuncDeclaration xerreq; // object.xopEquals |
| 87 |
extern (C++) __gshared FuncDeclaration xerrcmp; // object.xopCmp |
| 88 |
|
| 89 |
// ABI-specific type(s) if the struct can be passed in registers |
| 90 |
TypeTuple argTypes; |
| 91 |
|
| 92 |
structalign_t alignment; // alignment applied outside of the struct |
| 93 |
ThreeState ispod; // if struct is POD |
| 94 |
|
| 95 |
// `bool` fields that are compacted into bit fields in a string mixin |
| 96 |
private extern (D) static struct BitFields |
| 97 |
{ |
| 98 |
bool zeroInit; // !=0 if initialize with 0 fill |
| 99 |
bool hasIdentityAssign; // true if has identity opAssign |
| 100 |
bool hasBlitAssign; // true if opAssign is a blit |
| 101 |
bool hasIdentityEquals; // true if has identity opEquals |
| 102 |
bool hasNoFields; // has no fields |
| 103 |
bool hasCopyCtor; // copy constructor |
| 104 |
bool hasMoveCtor; // move constructor |
| 105 |
bool hasPointerField; // members with indirections |
| 106 |
bool hasVoidInitPointers; // void-initialized unsafe fields |
| 107 |
bool hasUnsafeBitpatterns; // @system members, pointers, bool |
| 108 |
bool hasFieldWithInvariant; // invariants |
| 109 |
bool computedTypeProperties;// the above 3 fields are computed |
| 110 |
// Even if struct is defined as non-root symbol, some built-in operations |
| 111 |
// (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo. |
| 112 |
// For those, today TypeInfo_Struct is generated in COMDAT. |
| 113 |
bool requestTypeInfo; |
| 114 |
} |
| 115 |
|
| 116 |
import dmd.common.bitfields : generateBitFields; |
| 117 |
mixin(generateBitFields!(BitFields, ushort)); |
| 118 |
|
| 119 |
extern (D) this(Loc loc, Identifier id, bool inObject) |
| 120 |
{ |
| 121 |
super(loc, id); |
| 122 |
this.dsym = DSYM.structDeclaration; |
| 123 |
zeroInit = false; // assume false until we do semantic processing |
| 124 |
ispod = ThreeState.none; |
| 125 |
// For forward references |
| 126 |
type = new TypeStruct(this); |
| 127 |
|
| 128 |
if (inObject) |
| 129 |
{ |
| 130 |
if (id == Id.ModuleInfo && !Module.moduleinfo) |
| 131 |
Module.moduleinfo = this; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
static StructDeclaration create(Loc loc, Identifier id, bool inObject) |
| 136 |
{ |
| 137 |
return new StructDeclaration(loc, id, inObject); |
| 138 |
} |
| 139 |
|
| 140 |
override StructDeclaration syntaxCopy(Dsymbol s) |
| 141 |
{ |
| 142 |
StructDeclaration sd = |
| 143 |
s ? cast(StructDeclaration)s |
| 144 |
: new StructDeclaration(loc, ident, false); |
| 145 |
ScopeDsymbol.syntaxCopy(sd); |
| 146 |
return sd; |
| 147 |
} |
| 148 |
|
| 149 |
override const(char)* kind() const |
| 150 |
{ |
| 151 |
return "struct"; |
| 152 |
} |
| 153 |
|
| 154 |
override final void finalizeSize() |
| 155 |
{ |
| 156 |
//printf("StructDeclaration::finalizeSize() %s, sizeok = %d\n", toChars(), sizeok); |
| 157 |
assert(sizeok != Sizeok.done); |
| 158 |
|
| 159 |
if (sizeok == Sizeok.inProcess) |
| 160 |
{ |
| 161 |
return; |
| 162 |
} |
| 163 |
sizeok = Sizeok.inProcess; |
| 164 |
|
| 165 |
//printf("+StructDeclaration::finalizeSize() %s, fields.length = %d, sizeok = %d\n", toChars(), fields.length, sizeok); |
| 166 |
|
| 167 |
fields.setDim(0); // workaround |
| 168 |
|
| 169 |
// Set the offsets of the fields and determine the size of the struct |
| 170 |
FieldState fieldState; |
| 171 |
bool isunion = isUnionDeclaration() !is null; |
| 172 |
for (size_t i = 0; i < members.length; i++) |
| 173 |
{ |
| 174 |
Dsymbol s = (*members)[i]; |
| 175 |
s.setFieldOffset(this, &fieldState, isunion); |
| 176 |
if (type.ty == Terror) |
| 177 |
{ |
| 178 |
errorSupplemental(s.loc, "error on member `%s`", s.toPrettyChars); |
| 179 |
errors = true; |
| 180 |
return; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
if (structsize == 0) |
| 185 |
{ |
| 186 |
hasNoFields = true; |
| 187 |
alignsize = 1; |
| 188 |
|
| 189 |
// A fine mess of what size a zero sized struct should be |
| 190 |
final switch (classKind) |
| 191 |
{ |
| 192 |
case ClassKind.d: |
| 193 |
case ClassKind.cpp: |
| 194 |
structsize = 1; |
| 195 |
break; |
| 196 |
|
| 197 |
case ClassKind.c: |
| 198 |
case ClassKind.objc: |
| 199 |
if (target.c.bitFieldStyle == TargetC.BitFieldStyle.MS) |
| 200 |
{ |
| 201 |
/* Undocumented MS behavior for: |
| 202 |
* struct S { int :0; }; |
| 203 |
*/ |
| 204 |
structsize = 4; |
| 205 |
} |
| 206 |
else |
| 207 |
structsize = 0; |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// Round struct size up to next alignsize boundary. |
| 213 |
// This will ensure that arrays of structs will get their internals |
| 214 |
// aligned properly. |
| 215 |
if (alignment.isDefault() || alignment.isPack()) |
| 216 |
structsize = (structsize + alignsize - 1) & ~(alignsize - 1); |
| 217 |
else |
| 218 |
structsize = (structsize + alignment.get() - 1) & ~(alignment.get() - 1); |
| 219 |
|
| 220 |
sizeok = Sizeok.done; |
| 221 |
|
| 222 |
//printf("-StructDeclaration::finalizeSize() %s, fields.length = %d, structsize = %d\n", toChars(), cast(int)fields.length, cast(int)structsize); |
| 223 |
|
| 224 |
if (errors) |
| 225 |
return; |
| 226 |
|
| 227 |
// Calculate fields[i].overlapped |
| 228 |
if (checkOverlappedFields()) |
| 229 |
{ |
| 230 |
errors = true; |
| 231 |
return; |
| 232 |
} |
| 233 |
|
| 234 |
// Determine if struct is all zeros or not |
| 235 |
zeroInit = true; |
| 236 |
auto lastOffset = -1; |
| 237 |
foreach (vd; fields) |
| 238 |
{ |
| 239 |
// First skip zero sized fields |
| 240 |
if (vd.type.size(vd.loc) == 0) |
| 241 |
continue; |
| 242 |
|
| 243 |
// only consider first sized member of an (anonymous) union |
| 244 |
if (vd.overlapped && vd.offset == lastOffset) |
| 245 |
continue; |
| 246 |
lastOffset = vd.offset; |
| 247 |
|
| 248 |
if (vd._init) |
| 249 |
{ |
| 250 |
if (vd._init.isVoidInitializer()) |
| 251 |
/* Treat as 0 for the purposes of putting the initializer |
| 252 |
* in the BSS segment, or doing a mass set to 0 |
| 253 |
*/ |
| 254 |
continue; |
| 255 |
|
| 256 |
// Examine init to see if it is all 0s. |
| 257 |
auto exp = vd.getConstInitializer(); |
| 258 |
if (!exp || !_isZeroInit(exp)) |
| 259 |
{ |
| 260 |
zeroInit = false; |
| 261 |
break; |
| 262 |
} |
| 263 |
} |
| 264 |
else if (!vd.type.isZeroInit(loc)) |
| 265 |
{ |
| 266 |
zeroInit = false; |
| 267 |
break; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
argTypes = target.toArgTypes(type); |
| 273 |
} |
| 274 |
|
| 275 |
/// Compute cached type properties for `TypeStruct` |
| 276 |
extern(D) final void determineTypeProperties() |
| 277 |
{ |
| 278 |
if (computedTypeProperties) |
| 279 |
return; |
| 280 |
foreach (vd; fields) |
| 281 |
{ |
| 282 |
if (vd.storage_class & STC.ref_ || vd.hasPointers()) |
| 283 |
{ |
| 284 |
hasPointerField = true; |
| 285 |
hasUnsafeBitpatterns = true; |
| 286 |
} |
| 287 |
|
| 288 |
if (vd._init && vd._init.isVoidInitializer() && vd.type.hasPointers()) |
| 289 |
hasVoidInitPointers = true; |
| 290 |
|
| 291 |
if (vd.storage_class & STC.system || vd.type.hasUnsafeBitpatterns()) |
| 292 |
hasUnsafeBitpatterns = true; |
| 293 |
|
| 294 |
if (!vd._init && vd.type.hasVoidInitPointers()) |
| 295 |
hasVoidInitPointers = true; |
| 296 |
|
| 297 |
if (vd.type.hasInvariant()) |
| 298 |
hasFieldWithInvariant = true; |
| 299 |
} |
| 300 |
computedTypeProperties = true; |
| 301 |
} |
| 302 |
|
| 303 |
/*************************************** |
| 304 |
* Determine if struct is POD (Plain Old Data). |
| 305 |
* |
| 306 |
* POD is defined as: |
| 307 |
* $(OL |
| 308 |
* $(LI not nested) |
| 309 |
* $(LI no postblits, destructors, or assignment operators) |
| 310 |
* $(LI no `ref` fields or fields that are themselves non-POD) |
| 311 |
* ) |
| 312 |
* The idea being these are compatible with C structs. |
| 313 |
* |
| 314 |
* Returns: |
| 315 |
* true if struct is POD |
| 316 |
*/ |
| 317 |
final bool isPOD() |
| 318 |
{ |
| 319 |
// If we've already determined whether this struct is POD. |
| 320 |
if (ispod != ThreeState.none) |
| 321 |
return (ispod == ThreeState.yes); |
| 322 |
|
| 323 |
import dmd.clone; |
| 324 |
|
| 325 |
bool hasCpCtorLocal; |
| 326 |
bool hasMoveCtorLocal; |
| 327 |
bool needCopyCtor; |
| 328 |
bool needMoveCtor; |
| 329 |
needCopyOrMoveCtor(this, hasCpCtorLocal, hasMoveCtorLocal, needCopyCtor, needMoveCtor); |
| 330 |
|
| 331 |
if (enclosing || // is nested |
| 332 |
search(this, loc, Id.postblit) || // has postblit |
| 333 |
search(this, loc, Id.dtor) || // has destructor |
| 334 |
/* This is commented out because otherwise buildkite vibe.d: |
| 335 |
`canCAS!Task` fails to compile |
| 336 |
*/ |
| 337 |
//hasMoveCtorLocal || // has move constructor |
| 338 |
hasCpCtorLocal) // has copy constructor |
| 339 |
{ |
| 340 |
ispod = ThreeState.no; |
| 341 |
return false; |
| 342 |
} |
| 343 |
|
| 344 |
// Recursively check all fields are POD. |
| 345 |
for (size_t i = 0; i < fields.length; i++) |
| 346 |
{ |
| 347 |
VarDeclaration v = fields[i]; |
| 348 |
if (v.storage_class & STC.ref_) |
| 349 |
{ |
| 350 |
ispod = ThreeState.no; |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
if (auto ts = v.type.baseElemOf().isTypeStruct()) |
| 355 |
{ |
| 356 |
if (!ts.sym.isPOD()) |
| 357 |
{ |
| 358 |
ispod = ThreeState.no; |
| 359 |
return false; |
| 360 |
} |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
ispod = ThreeState.yes; |
| 365 |
return true; |
| 366 |
} |
| 367 |
|
| 368 |
/*************************************** |
| 369 |
* Determine if struct has copy construction (copy constructor or postblit) |
| 370 |
* Returns: |
| 371 |
* true if struct has copy construction |
| 372 |
*/ |
| 373 |
final bool hasCopyConstruction() |
| 374 |
{ |
| 375 |
return postblit || hasCopyCtor; |
| 376 |
} |
| 377 |
|
| 378 |
override void accept(Visitor v) |
| 379 |
{ |
| 380 |
v.visit(this); |
| 381 |
} |
| 382 |
|
| 383 |
final uint numArgTypes() const |
| 384 |
{ |
| 385 |
return argTypes && argTypes.arguments ? cast(uint) argTypes.arguments.length : 0; |
| 386 |
} |
| 387 |
|
| 388 |
final Type argType(uint index) |
| 389 |
{ |
| 390 |
return index < numArgTypes() ? (*argTypes.arguments)[index].type : null; |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
/*************************************** |
| 395 |
* Verifies whether the struct declaration has a |
| 396 |
* constructor that is not a copy constructor. |
| 397 |
* Optionally, it can check whether the struct |
| 398 |
* declaration has a regular constructor, that |
| 399 |
* is not disabled. |
| 400 |
* |
| 401 |
* Params: |
| 402 |
* checkDisabled = if the struct has a regular |
| 403 |
non-disabled constructor |
| 404 |
* Returns: |
| 405 |
* true, if the struct has a regular (optionally, |
| 406 |
* not disabled) constructor, false otherwise. |
| 407 |
*/ |
| 408 |
final bool hasRegularCtor(bool checkDisabled = false) |
| 409 |
{ |
| 410 |
if (!ctor) |
| 411 |
return false; |
| 412 |
|
| 413 |
bool result; |
| 414 |
overloadApply(ctor, (Dsymbol s) |
| 415 |
{ |
| 416 |
if (auto td = s.isTemplateDeclaration()) |
| 417 |
{ |
| 418 |
if (checkDisabled && td.onemember) |
| 419 |
{ |
| 420 |
if (auto ctorDecl = td.onemember.isCtorDeclaration()) |
| 421 |
{ |
| 422 |
if (ctorDecl.storage_class & STC.disable) |
| 423 |
return 0; |
| 424 |
} |
| 425 |
} |
| 426 |
result = true; |
| 427 |
return 1; |
| 428 |
} |
| 429 |
if (auto ctorDecl = s.isCtorDeclaration()) |
| 430 |
{ |
| 431 |
if (!ctorDecl.isCpCtor && (!checkDisabled || !(ctorDecl.storage_class & STC.disable))) |
| 432 |
{ |
| 433 |
result = true; |
| 434 |
return 1; |
| 435 |
} |
| 436 |
} |
| 437 |
return 0; |
| 438 |
}); |
| 439 |
return result; |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
/********************************** |
| 444 |
* Determine if exp is all binary zeros. |
| 445 |
* Params: |
| 446 |
* exp = expression to check |
| 447 |
* Returns: |
| 448 |
* true if it's all binary 0 |
| 449 |
*/ |
| 450 |
bool _isZeroInit(Expression exp) |
| 451 |
{ |
| 452 |
switch (exp.op) |
| 453 |
{ |
| 454 |
case EXP.int64: |
| 455 |
return exp.toInteger() == 0; |
| 456 |
|
| 457 |
case EXP.null_: |
| 458 |
return true; |
| 459 |
|
| 460 |
case EXP.structLiteral: |
| 461 |
{ |
| 462 |
auto sle = exp.isStructLiteralExp(); |
| 463 |
if (sle.sd.isNested()) |
| 464 |
return false; |
| 465 |
const isCstruct = sle.sd.isCsymbol(); // C structs are default initialized to all zeros |
| 466 |
foreach (i; 0 .. sle.sd.fields.length) |
| 467 |
{ |
| 468 |
auto field = sle.sd.fields[i]; |
| 469 |
if (field.type.size(field.loc)) |
| 470 |
{ |
| 471 |
auto e = sle.elements && i < sle.elements.length ? (*sle.elements)[i] : null; |
| 472 |
if (e ? !_isZeroInit(e) |
| 473 |
: !isCstruct && !field.type.isZeroInit(field.loc)) |
| 474 |
return false; |
| 475 |
} |
| 476 |
} |
| 477 |
return true; |
| 478 |
} |
| 479 |
|
| 480 |
case EXP.arrayLiteral: |
| 481 |
{ |
| 482 |
auto ale = cast(ArrayLiteralExp)exp; |
| 483 |
|
| 484 |
const dim = ale.elements ? ale.elements.length : 0; |
| 485 |
|
| 486 |
if (ale.type.toBasetype().ty == Tarray) // if initializing a dynamic array |
| 487 |
return dim == 0; |
| 488 |
|
| 489 |
foreach (i; 0 .. dim) |
| 490 |
{ |
| 491 |
if (!_isZeroInit(ale[i])) |
| 492 |
return false; |
| 493 |
} |
| 494 |
|
| 495 |
/* Note that true is returned for all T[0] |
| 496 |
*/ |
| 497 |
return true; |
| 498 |
} |
| 499 |
|
| 500 |
case EXP.string_: |
| 501 |
{ |
| 502 |
auto se = cast(StringExp)exp; |
| 503 |
|
| 504 |
if (se.type.toBasetype().ty == Tarray) // if initializing a dynamic array |
| 505 |
return se.len == 0; |
| 506 |
|
| 507 |
foreach (i; 0 .. se.len) |
| 508 |
{ |
| 509 |
if (se.getIndex(i) != 0) |
| 510 |
return false; |
| 511 |
} |
| 512 |
return true; |
| 513 |
} |
| 514 |
|
| 515 |
case EXP.vector: |
| 516 |
{ |
| 517 |
auto ve = cast(VectorExp) exp; |
| 518 |
return _isZeroInit(ve.e1); |
| 519 |
} |
| 520 |
|
| 521 |
case EXP.float64: |
| 522 |
case EXP.complex80: |
| 523 |
{ |
| 524 |
import dmd.root.ctfloat : CTFloat; |
| 525 |
return (exp.toReal() is CTFloat.zero) && |
| 526 |
(exp.toImaginary() is CTFloat.zero); |
| 527 |
} |
| 528 |
|
| 529 |
default: |
| 530 |
return false; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
/*********************************************************** |
| 535 |
* Unions are a variation on structs. |
| 536 |
*/ |
| 537 |
extern (C++) final class UnionDeclaration : StructDeclaration |
| 538 |
{ |
| 539 |
extern (D) this(Loc loc, Identifier id) |
| 540 |
{ |
| 541 |
super(loc, id, false); |
| 542 |
this.dsym = DSYM.unionDeclaration; |
| 543 |
} |
| 544 |
|
| 545 |
override UnionDeclaration syntaxCopy(Dsymbol s) |
| 546 |
{ |
| 547 |
assert(!s); |
| 548 |
auto ud = new UnionDeclaration(loc, ident); |
| 549 |
StructDeclaration.syntaxCopy(ud); |
| 550 |
return ud; |
| 551 |
} |
| 552 |
|
| 553 |
override const(char)* kind() const |
| 554 |
{ |
| 555 |
return "union"; |
| 556 |
} |
| 557 |
|
| 558 |
override void accept(Visitor v) |
| 559 |
{ |
| 560 |
v.visit(this); |
| 561 |
} |
| 562 |
} |