| 1 |
/** |
| 2 |
* A library bitfields utility |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: Dennis Korpel |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/common/bitfields.d, common/bitfields.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_common_bitfields.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/common/bitfields.d |
| 10 |
*/ |
| 11 |
module dmd.common.bitfields; |
| 12 |
|
| 13 |
/** |
| 14 |
* Generate code for bit fields inside a struct/class body |
| 15 |
* Params: |
| 16 |
* S = type of a struct with only boolean fields, which should become bit fields |
| 17 |
* T = type of bit fields variable, must have enough bits to store all booleans |
| 18 |
* Returns: D code with a bit fields variable and getter / setter functions |
| 19 |
*/ |
| 20 |
extern (D) string generateBitFields(S, T)() |
| 21 |
if (__traits(isUnsigned, T)) |
| 22 |
{ |
| 23 |
import core.bitop: bsr; |
| 24 |
|
| 25 |
string result = "extern (C++) pure nothrow @nogc @safe final {"; |
| 26 |
|
| 27 |
struct BitInfo |
| 28 |
{ |
| 29 |
int[] offset; |
| 30 |
int[] size; |
| 31 |
T initialValue; |
| 32 |
int totalSize; |
| 33 |
} |
| 34 |
|
| 35 |
// Iterate over members to compute bit offset and bit size for each of them |
| 36 |
enum BitInfo bitInfo = () { |
| 37 |
BitInfo result; |
| 38 |
int bitOffset = 0; |
| 39 |
foreach (size_t i, mem; __traits(allMembers, S)) |
| 40 |
{ |
| 41 |
alias memType = typeof(__traits(getMember, S, mem)); |
| 42 |
enum int bitSize = bsr(memType.max | 1) + 1; |
| 43 |
result.offset ~= bitOffset; |
| 44 |
result.size ~= bitSize; |
| 45 |
result.initialValue |= cast(T) __traits(getMember, S.init, mem) << bitOffset; |
| 46 |
bitOffset += bitSize; |
| 47 |
} |
| 48 |
result.totalSize = bitOffset; |
| 49 |
return result; |
| 50 |
} (); |
| 51 |
|
| 52 |
alias TP = typeof(T.init + 0u); // type that `T` gets promoted to, uint or ulong |
| 53 |
enum string toString(TP i) = i.stringof; // compile time 'integer to string' |
| 54 |
|
| 55 |
static assert(bitInfo.totalSize <= T.sizeof * 8, |
| 56 |
"sum of bit field size "~toString!(bitInfo.totalSize)~" exceeds storage type `"~T.stringof~"`"); |
| 57 |
|
| 58 |
foreach (size_t i, mem; __traits(allMembers, S)) |
| 59 |
{ |
| 60 |
enum typeName = typeof(__traits(getMember, S, mem)).stringof; |
| 61 |
enum shift = toString!(bitInfo.offset[i]); |
| 62 |
enum sizeMask = toString!((1 << bitInfo.size[i]) - 1); // 0x01 for bool, 0xFF for ubyte etc. |
| 63 |
result ~= " |
| 64 |
"~typeName~" "~mem~"() const scope { return cast("~typeName~") ((bitFields >>> "~shift~") & "~sizeMask~"); } |
| 65 |
"~typeName~" "~mem~"("~typeName~" v) scope |
| 66 |
{ |
| 67 |
bitFields &= ~("~sizeMask~" << "~shift~"); |
| 68 |
bitFields |= v << "~shift~"; |
| 69 |
return v; |
| 70 |
}"; |
| 71 |
} |
| 72 |
enum TP initVal = bitInfo.initialValue; |
| 73 |
return result ~ "\n}\n private "~T.stringof~" bitFields = " ~ toString!(initVal) ~ ";\n"; |
| 74 |
} |
| 75 |
|
| 76 |
/// |
| 77 |
unittest |
| 78 |
{ |
| 79 |
enum E |
| 80 |
{ |
| 81 |
a, b, c, |
| 82 |
} |
| 83 |
|
| 84 |
static struct B |
| 85 |
{ |
| 86 |
bool x; |
| 87 |
bool y; |
| 88 |
E e = E.c; |
| 89 |
bool z = 1; |
| 90 |
private ubyte w = 77; |
| 91 |
} |
| 92 |
|
| 93 |
static struct S |
| 94 |
{ |
| 95 |
mixin(generateBitFields!(B, ushort)); |
| 96 |
} |
| 97 |
|
| 98 |
S s; |
| 99 |
assert(!s.x); |
| 100 |
s.x = true; |
| 101 |
assert(s.x); |
| 102 |
s.x = false; |
| 103 |
assert(!s.x); |
| 104 |
|
| 105 |
s.y = true; |
| 106 |
assert(s.y); |
| 107 |
assert(!s.x); |
| 108 |
|
| 109 |
assert(s.e == E.c); |
| 110 |
s.e = E.a; |
| 111 |
assert(s.e == E.a); |
| 112 |
|
| 113 |
assert(s.z); |
| 114 |
assert(s.w == 77); |
| 115 |
s.w = 3; |
| 116 |
assert(s.w == 3); |
| 117 |
} |