Bitwise»Forums
4 posts
Aliasing struct fields to seemingly reduce levels of indirection.
Edited by Anikki on Reason: Initial post
In "Bitwise Extra, Day 27: Static Assembler, Part 2" (~14:09) pervognsen is accessing a field of a struct that's nested in other structs: https://www.youtube.com/watch?v=DRKUJBc4y6A&t=14m09s

I think a union could be used to create an alias to a frequently used field which is nested, say 2-3 levels:

Using the struct Parser as an example:

 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
struct Parser {
    union {
        lexer: Lexer;

        // alias parser.t = parser.lexer.token;
        struct {
            dummy1: uint8_t[offsetof(Lexer, token)];
            // t: typeof(Lexer, token);
            t: Token;
        }

        // alias parser.td = parser.lexer.token.data
        struct {
            dummy2: uint8_t[offsetof(Lexer, token) + offsetof(Token, data)];
            // td: typeof(Token, data);
            td: TokedData;
        }
    }

    // ...
}

func is_token(parser: Parser*, kind: TokenKind): bool { return parser.lexer.token.kind == kind; }
func is_token(parser: Parser*, kind: TokenKind): bool { return parser.t.kind == kind; }

xreg := parser.lexer.token.data.xreg;
xreg := parser.td.xreg;

Per Vognsen
49 posts / 1 project
Aliasing struct fields to seemingly reduce levels of indirection.
I checked in code for C11-style anonymous substructs/subunions, which facilitates this kind of thing without manual aliasing, the way I was already doing it for tagged unions in the C code for the Ion compiler.