2 * Struct and union declarations.
4 * Specification: $(LINK2 https://dlang.org/spec/struct.html, Structs, Unions)
6 * Copyright: Copyright (C) 1999-2023 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/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/src/dmd/dstruct.d
17 import dmd
.arraytypes
;
20 import dmd
.declaration
;
24 import dmd
.dsymbolsem
;
27 import dmd
.expression
;
31 import dmd
.identifier
;
41 /***************************************
42 * Search sd for a member function of the form:
43 * `extern (D) string toString();`
45 * sd = struct declaration to search
47 * FuncDeclaration of `toString()` if found, `null` if not
49 extern (C
++) FuncDeclaration
search_toString(StructDeclaration sd
)
51 Dsymbol s
= search_function(sd
, Id
.tostring
);
52 FuncDeclaration fd
= s ? s
.isFuncDeclaration() : null;
55 __gshared TypeFunction tftostring
;
58 tftostring
= new TypeFunction(ParameterList(), Type
.tstring
, LINK
.d
);
59 tftostring
= tftostring
.merge().toTypeFunction();
61 fd
= fd
.overloadExactMatch(tftostring
);
66 /***************************************
67 * Request additional semantic analysis for TypeInfo generation.
70 * t = type that TypeInfo is being generated for
72 extern (C
++) void semanticTypeInfo(Scope
* sc
, Type t
)
78 if (sc
.flags
& (SCOPE
.ctfe | SCOPE
.compile | SCOPE
.ctfeBlock
))
85 void visitVector(TypeVector t
)
87 semanticTypeInfo(sc
, t
.basetype
);
90 void visitAArray(TypeAArray t
)
92 semanticTypeInfo(sc
, t
.index
);
93 semanticTypeInfo(sc
, t
.next
);
96 void visitStruct(TypeStruct t
)
98 //printf("semanticTypeInfo.visit(TypeStruct = %s)\n", t.toChars());
99 StructDeclaration sd
= t
.sym
;
101 /* Step 1: create TypeInfoDeclaration
103 if (!sc
) // inline may request TypeInfo.
106 scx
._module
= sd
.getModule();
107 getTypeInfoType(sd
.loc
, t
, &scx
);
108 sd
.requestTypeInfo
= true;
112 // don't yet have to generate TypeInfo instance if
113 // the typeid(T) expression exists in speculative scope.
117 getTypeInfoType(sd
.loc
, t
, sc
);
118 sd
.requestTypeInfo
= true;
120 // https://issues.dlang.org/show_bug.cgi?id=15149
121 // if the typeid operand type comes from a
122 // result of auto function, it may be yet speculative.
123 // unSpeculative(sc, sd);
126 /* Step 2: If the TypeInfo generation requires sd.semantic3, run it later.
127 * This should be done even if typeid(T) exists in speculative scope.
128 * Because it may appear later in non-speculative scope.
131 return; // opaque struct
132 if (!sd
.xeq
&& !sd
.xcmp
&& !sd
.postblit
&& !sd
.tidtor
&& !sd
.xhash
&& !search_toString(sd
))
133 return; // none of TypeInfo-specific members
135 // If the struct is in a non-root module, run semantic3 to get
136 // correct symbols for the member function.
137 if (sd
.semanticRun
>= PASS
.semantic3
)
139 // semantic3 is already done
141 else if (TemplateInstance ti
= sd
.isInstantiated())
143 if (ti
.minst
&& !ti
.minst
.isRoot())
144 Module
.addDeferredSemantic3(sd
);
150 //printf("deferred sem3 for TypeInfo - sd = %s, inNonRoot = %d\n", sd.toChars(), sd.inNonRoot());
151 Module
.addDeferredSemantic3(sd
);
156 void visitTuple(TypeTuple t
)
160 foreach (arg
; *t
.arguments
)
162 semanticTypeInfo(sc
, arg
.type
);
167 /* Note structural similarity of this Type walker to that in isSpeculativeType()
170 Type tb
= t
.toBasetype();
173 case Tvector
: visitVector(tb
.isTypeVector()); break;
174 case Taarray
: visitAArray(tb
.isTypeAArray()); break;
175 case Tstruct
: visitStruct(tb
.isTypeStruct()); break;
176 case Ttuple
: visitTuple (tb
.isTypeTuple()); break;
181 default: semanticTypeInfo(sc
, tb
.nextOf()); break;
185 enum StructFlags
: int
188 hasPointers
= 0x1, // NB: should use noPointers as in ClassFlags
191 /***********************************************************
192 * All `struct` declarations are an instance of this.
194 extern (C
++) class StructDeclaration
: AggregateDeclaration
196 FuncDeclarations postblits
; // Array of postblit functions
197 FuncDeclaration postblit
; // aggregate postblit
199 FuncDeclaration xeq
; // TypeInfo_Struct.xopEquals
200 FuncDeclaration xcmp
; // TypeInfo_Struct.xopCmp
201 FuncDeclaration xhash
; // TypeInfo_Struct.xtoHash
202 extern (C
++) __gshared FuncDeclaration xerreq
; // object.xopEquals
203 extern (C
++) __gshared FuncDeclaration xerrcmp
; // object.xopCmp
205 // ABI-specific type(s) if the struct can be passed in registers
208 structalign_t alignment
; // alignment applied outside of the struct
209 ThreeState ispod
; // if struct is POD
211 // `bool` fields that are compacted into bit fields in a string mixin
212 private extern (D
) static struct BitFields
214 bool zeroInit
; // !=0 if initialize with 0 fill
215 bool hasIdentityAssign
; // true if has identity opAssign
216 bool hasBlitAssign
; // true if opAssign is a blit
217 bool hasIdentityEquals
; // true if has identity opEquals
218 bool hasNoFields
; // has no fields
219 bool hasCopyCtor
; // copy constructor
220 bool hasPointerField
; // members with indirections
221 bool hasVoidInitPointers
; // void-initialized unsafe fields
222 bool hasSystemFields
; // @system members
223 bool hasFieldWithInvariant
; // invariants
224 bool computedTypeProperties
;// the above 3 fields are computed
225 // Even if struct is defined as non-root symbol, some built-in operations
226 // (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo.
227 // For those, today TypeInfo_Struct is generated in COMDAT.
228 bool requestTypeInfo
;
231 import dmd
.common
.bitfields
: generateBitFields
;
232 mixin(generateBitFields
!(BitFields
, ushort));
234 extern (D
) this(const ref Loc loc
, Identifier id
, bool inObject
)
237 zeroInit
= false; // assume false until we do semantic processing
238 ispod
= ThreeState
.none
;
239 // For forward references
240 type
= new TypeStruct(this);
244 if (id
== Id
.ModuleInfo
&& !Module
.moduleinfo
)
245 Module
.moduleinfo
= this;
249 static StructDeclaration
create(const ref Loc loc
, Identifier id
, bool inObject
)
251 return new StructDeclaration(loc
, id
, inObject
);
254 override StructDeclaration
syntaxCopy(Dsymbol s
)
256 StructDeclaration sd
=
257 s ?
cast(StructDeclaration
)s
258 : new StructDeclaration(loc
, ident
, false);
259 ScopeDsymbol
.syntaxCopy(sd
);
263 override final Dsymbol
search(const ref Loc loc
, Identifier ident
, int flags
= SearchLocalsOnly
)
265 //printf("%s.StructDeclaration::search('%s', flags = x%x)\n", toChars(), ident.toChars(), flags);
266 if (_scope
&& !symtab
)
267 dsymbolSemantic(this, _scope
);
269 if (!members ||
!symtab
) // opaque or semantic() is not yet called
271 // .stringof is always defined (but may be hidden by some other symbol)
272 if(ident
!= Id
.stringof
&& !(flags
& IgnoreErrors
) && semanticRun
< PASS
.semanticdone
)
273 error("is forward referenced when looking for `%s`", ident
.toChars());
277 return ScopeDsymbol
.search(loc
, ident
, flags
);
280 override const(char)* kind() const
285 override final void finalizeSize()
287 //printf("StructDeclaration::finalizeSize() %s, sizeok = %d\n", toChars(), sizeok);
288 assert(sizeok
!= Sizeok
.done
);
290 if (sizeok
== Sizeok
.inProcess
)
294 sizeok
= Sizeok
.inProcess
;
296 //printf("+StructDeclaration::finalizeSize() %s, fields.length = %d, sizeok = %d\n", toChars(), fields.length, sizeok);
298 fields
.setDim(0); // workaround
300 // Set the offsets of the fields and determine the size of the struct
301 FieldState fieldState
;
302 bool isunion
= isUnionDeclaration() !is null;
303 for (size_t i
= 0; i
< members
.length
; i
++)
305 Dsymbol s
= (*members
)[i
];
306 s
.setFieldOffset(this, fieldState
, isunion
);
308 if (type
.ty
== Terror
)
319 // A fine mess of what size a zero sized struct should be
320 final switch (classKind
)
329 if (target
.c
.bitFieldStyle
== TargetC
.BitFieldStyle
.MS
)
331 /* Undocumented MS behavior for:
332 * struct S { int :0; };
336 else if (target
.c
.bitFieldStyle
== TargetC
.BitFieldStyle
.DM
)
347 // Round struct size up to next alignsize boundary.
348 // This will ensure that arrays of structs will get their internals
350 if (alignment
.isDefault() || alignment
.isPack())
351 structsize
= (structsize
+ alignsize
- 1) & ~(alignsize
- 1);
353 structsize
= (structsize
+ alignment
.get() - 1) & ~(alignment
.get() - 1);
355 sizeok
= Sizeok
.done
;
357 //printf("-StructDeclaration::finalizeSize() %s, fields.length = %d, structsize = %d\n", toChars(), fields.length, structsize);
362 // Calculate fields[i].overlapped
363 if (checkOverlappedFields())
369 // Determine if struct is all zeros or not
375 if (vd
._init
.isVoidInitializer())
376 /* Treat as 0 for the purposes of putting the initializer
377 * in the BSS segment, or doing a mass set to 0
381 // Zero size fields are zero initialized
382 if (vd
.type
.size(vd
.loc
) == 0)
385 // Examine init to see if it is all 0s.
386 auto exp
= vd
.getConstInitializer();
387 if (!exp ||
!_isZeroInit(exp
))
393 else if (!vd
.type
.isZeroInit(loc
))
401 argTypes
= target
.toArgTypes(type
);
404 /// Compute cached type properties for `TypeStruct`
405 extern(D
) final void determineTypeProperties()
407 if (computedTypeProperties
)
411 if (vd
.storage_class
& STC
.ref_ || vd
.hasPointers())
412 hasPointerField
= true;
414 if (vd
._init
&& vd
._init
.isVoidInitializer() && vd
.type
.hasPointers())
415 hasVoidInitPointers
= true;
417 if (vd
.storage_class
& STC
.system || vd
.type
.hasSystemFields())
418 hasSystemFields
= true;
420 if (!vd
._init
&& vd
.type
.hasVoidInitPointers())
421 hasVoidInitPointers
= true;
423 if (vd
.type
.hasInvariant())
424 hasFieldWithInvariant
= true;
426 computedTypeProperties
= true;
429 /***************************************
430 * Determine if struct is POD (Plain Old Data).
435 * $(LI no postblits, destructors, or assignment operators)
436 * $(LI no `ref` fields or fields that are themselves non-POD)
438 * The idea being these are compatible with C structs.
441 * true if struct is POD
445 // If we've already determined whether this struct is POD.
446 if (ispod
!= ThreeState
.none
)
447 return (ispod
== ThreeState
.yes
);
449 ispod
= ThreeState
.yes
;
451 if (enclosing || postblit || dtor || hasCopyCtor
)
453 ispod
= ThreeState
.no
;
457 // Recursively check all fields are POD.
458 for (size_t i
= 0; i
< fields
.length
; i
++)
460 VarDeclaration v
= fields
[i
];
461 if (v
.storage_class
& STC
.ref_
)
463 ispod
= ThreeState
.no
;
467 Type tv
= v
.type
.baseElemOf();
468 if (tv
.ty
== Tstruct
)
470 TypeStruct ts
= cast(TypeStruct
)tv
;
471 StructDeclaration sd
= ts
.sym
;
474 ispod
= ThreeState
.no
;
480 return (ispod
== ThreeState
.yes
);
483 /***************************************
484 * Determine if struct has copy construction (copy constructor or postblit)
486 * true if struct has copy construction
488 final bool hasCopyConstruction()
490 return postblit || hasCopyCtor
;
493 override final inout(StructDeclaration
) isStructDeclaration() inout @nogc nothrow pure @safe
498 override void accept(Visitor v
)
503 final uint numArgTypes() const
505 return argTypes
&& argTypes
.arguments ?
cast(uint) argTypes
.arguments
.length
: 0;
508 final Type
argType(uint index
)
510 return index
< numArgTypes() ?
(*argTypes
.arguments
)[index
].type
: null;
514 /***************************************
515 * Verifies whether the struct declaration has a
516 * constructor that is not a copy constructor.
517 * Optionally, it can check whether the struct
518 * declaration has a regular constructor, that
522 * checkDisabled = if the struct has a regular
523 non-disabled constructor
525 * true, if the struct has a regular (optionally,
526 * not disabled) constructor, false otherwise.
528 final bool hasRegularCtor(bool checkDisabled
= false)
534 overloadApply(ctor
, (Dsymbol s
)
536 if (auto td
= s
.isTemplateDeclaration())
538 if (checkDisabled
&& td
.onemember
)
540 if (auto ctorDecl
= td
.onemember
.isCtorDeclaration())
542 if (ctorDecl
.storage_class
& STC
.disable
)
549 if (auto ctorDecl
= s
.isCtorDeclaration())
551 if (!ctorDecl
.isCpCtor
&& (!checkDisabled ||
!(ctorDecl
.storage_class
& STC
.disable
)))
563 /**********************************
564 * Determine if exp is all binary zeros.
566 * exp = expression to check
568 * true if it's all binary 0
570 private bool _isZeroInit(Expression exp
)
575 return exp
.toInteger() == 0;
581 case EXP
.structLiteral
:
583 auto sle
= cast(StructLiteralExp
) exp
;
584 foreach (i
; 0 .. sle
.sd
.fields
.length
)
586 auto field
= sle
.sd
.fields
[i
];
587 if (field
.type
.size(field
.loc
))
589 auto e
= (*sle
.elements
)[i
];
590 if (e ?
!_isZeroInit(e
)
591 : !field
.type
.isZeroInit(field
.loc
))
598 case EXP
.arrayLiteral
:
600 auto ale
= cast(ArrayLiteralExp
)exp
;
602 const dim
= ale
.elements ? ale
.elements
.length
: 0;
604 if (ale
.type
.toBasetype().ty
== Tarray
) // if initializing a dynamic array
607 foreach (i
; 0 .. dim
)
609 if (!_isZeroInit(ale
[i
]))
613 /* Note that true is returned for all T[0]
620 StringExp se
= cast(StringExp
)exp
;
622 if (se
.type
.toBasetype().ty
== Tarray
) // if initializing a dynamic array
625 foreach (i
; 0 .. se
.len
)
627 if (se
.getCodeUnit(i
))
635 auto ve
= cast(VectorExp
) exp
;
636 return _isZeroInit(ve
.e1
);
642 import dmd
.root
.ctfloat
: CTFloat
;
643 return (exp
.toReal() is CTFloat
.zero
) &&
644 (exp
.toImaginary() is CTFloat
.zero
);
652 /***********************************************************
653 * Unions are a variation on structs.
655 extern (C
++) final class UnionDeclaration
: StructDeclaration
657 extern (D
) this(const ref Loc loc
, Identifier id
)
659 super(loc
, id
, false);
662 override UnionDeclaration
syntaxCopy(Dsymbol s
)
665 auto ud
= new UnionDeclaration(loc
, ident
);
666 StructDeclaration
.syntaxCopy(ud
);
670 override const(char)* kind() const
675 override inout(UnionDeclaration
) isUnionDeclaration() inout
680 override void accept(Visitor v
)