summaryrefslogtreecommitdiff
path: root/gcc/d/dmd/common/bitfields.d
blob: d17983d66b4416c674e0ff30653b44e273b97e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
 * A library bitfields utility
 *
 * Copyright: Copyright (C) 1999-2022 by The D Language Foundation, All Rights Reserved
 * Authors:   Dennis Korpel
 * License:   $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
 * Source:    $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/common/bitfields.d, common/bitfields.d)
 * Documentation: https://dlang.org/phobos/dmd_common_bitfields.html
 * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/common/bitfields.d
 */
module dmd.common.bitfields;

/**
 * Generate code for bit fields inside a struct/class body
 * Params:
 *   S = type of a struct with only boolean fields, which should become bit fields
 *   T = type of bit fields variable, must have enough bits to store all booleans
 * Returns: D code with a bit fields variable and getter / setter functions
 */
extern (D) string generateBitFields(S, T)()
if (__traits(isUnsigned, T))
{
    string result = "extern (C++) pure nothrow @nogc @safe final {";
    enum structName = __traits(identifier, S);

    foreach (size_t i, mem; __traits(allMembers, S))
    {
        static assert(is(typeof(__traits(getMember, S, mem)) == bool));
        static assert(i < T.sizeof * 8, "too many fields for bit field storage of type `"~T.stringof~"`");
        enum mask = "(1 << "~i.stringof~")";
        result ~= "
        /// set or get the corresponding "~structName~" member
        bool "~mem~"() const { return !!(bitFields & "~mask~"); }
        /// ditto
        bool "~mem~"(bool v)
        {
            v ? (bitFields |= "~mask~") : (bitFields &= ~"~mask~");
            return v;
        }";
    }
    return result ~ "}\n private "~T.stringof~" bitFields;\n";
}

///
unittest
{
    static struct B
    {
        bool x;
        bool y;
        bool z;
    }

    static struct S
    {
        mixin(generateBitFields!(B, ubyte));
    }

    S s;
    assert(!s.x);
    s.x = true;
    assert(s.x);
    s.x = false;
    assert(!s.x);

    s.y = true;
    assert(s.y);
    assert(!s.x);
    assert(!s.z);
}