Skip to content

Commit

Permalink
Parser: use enum for offsetof bits/bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Oct 13, 2024
1 parent a077096 commit 514395a
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6843,8 +6843,8 @@ fn castExpr(p: *Parser) Error!Result {
switch (p.tok_ids[p.tok_i]) {
.builtin_choose_expr => return p.builtinChooseExpr(),
.builtin_va_arg => return p.builtinVaArg(),
.builtin_offsetof => return p.builtinOffsetof(false),
.builtin_bitoffsetof => return p.builtinOffsetof(true),
.builtin_offsetof => return p.builtinOffsetof(.bytes),
.builtin_bitoffsetof => return p.builtinOffsetof(.bits),
.builtin_types_compatible_p => return p.typesCompatible(),
// TODO: other special-cased builtins
else => {},
Expand Down Expand Up @@ -6968,7 +6968,9 @@ fn builtinVaArg(p: *Parser) Error!Result {
}) };
}

fn builtinOffsetof(p: *Parser, want_bits: bool) Error!Result {
const OffsetKind = enum { bits, bytes };

fn builtinOffsetof(p: *Parser, offset_kind: OffsetKind) Error!Result {
const builtin_tok = p.tok_i;
p.tok_i += 1;

Expand All @@ -6993,7 +6995,7 @@ fn builtinOffsetof(p: *Parser, want_bits: bool) Error!Result {

_ = try p.expectToken(.comma);

const offsetof_expr = try p.offsetofMemberDesignator(ty, want_bits);
const offsetof_expr = try p.offsetofMemberDesignator(ty, offset_kind);

try p.expectClosing(l_paren, .r_paren);

Expand All @@ -7009,7 +7011,7 @@ fn builtinOffsetof(p: *Parser, want_bits: bool) Error!Result {
}

/// offsetofMemberDesignator: IDENTIFIER ('.' IDENTIFIER | '[' expr ']' )*
fn offsetofMemberDesignator(p: *Parser, base_ty: Type, want_bits: bool) Error!Result {
fn offsetofMemberDesignator(p: *Parser, base_ty: Type, offset_kind: OffsetKind) Error!Result {
errdefer p.skipTo(.r_paren);
const base_field_name_tok = try p.expectIdentifier();
const base_field_name = try StrInt.intern(p.comp, p.tokSlice(base_field_name_tok));
Expand Down Expand Up @@ -7062,7 +7064,7 @@ fn offsetofMemberDesignator(p: *Parser, base_ty: Type, want_bits: bool) Error!Re
},
else => break,
};
const val = try Value.int(if (want_bits) total_offset else total_offset / 8, p.comp);
const val = try Value.int(if (offset_kind == .bits) total_offset else total_offset / 8, p.comp);
return Result{ .ty = base_ty, .val = val, .node = lhs.node };
}

Expand Down

0 comments on commit 514395a

Please sign in to comment.