Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit d7f1262

Browse files
steeveVexu
authored andcommitted
Handle enum types in array accessors
Closes #227
1 parent 8e1ff20 commit d7f1262

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Translator.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3138,7 +3138,7 @@ fn transArrayAccess(t: *Translator, scope: *Scope, array_access: Node.ArrayAcces
31383138
const index = index: {
31393139
const index = try t.transExpr(scope, array_access.index, .used);
31403140
const index_qt = array_access.index.qt(t.tree);
3141-
const maybe_bigger_than_usize = switch (index_qt.base(t.comp).type) {
3141+
const maybe_bigger_than_usize = type: switch (index_qt.base(t.comp).type) {
31423142
.bool => {
31433143
break :index try ZigTag.int_from_bool.create(t.arena, index);
31443144
},
@@ -3147,6 +3147,7 @@ fn transArrayAccess(t: *Translator, scope: *Scope, array_access: Node.ArrayAcces
31473147
else => false,
31483148
},
31493149
.bit_int => |bit_int| bit_int.bits > t.comp.target.ptrBitWidth(),
3150+
.@"enum" => |e| if (e.tag) |tag| continue :type tag.base(t.comp).type else false,
31503151
else => unreachable,
31513152
};
31523153

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
typedef enum {
2+
one = 1,
3+
two = 2,
4+
} EnumValues;
5+
6+
static inline int dynamic_array_access(EnumValues idx) {
7+
static const int values[] = {0, 1, 2};
8+
return values[idx - 1];
9+
}
10+
11+
// translate
12+
// target=native-linux
13+
//
14+
// pub const one: c_int = 1;
15+
// pub const two: c_int = 2;
16+
// pub const EnumValues = c_uint;
17+
// pub fn dynamic_array_access(arg_idx: EnumValues) callconv(.c) c_int {
18+
// var idx = arg_idx;
19+
// _ = &idx;
20+
// const static_local_values = struct {
21+
// const values: [3]c_int = [3]c_int{
22+
// 0,
23+
// 1,
24+
// 2,
25+
// };
26+
// };
27+
// _ = &static_local_values;
28+
// return static_local_values.values[idx -% @as(EnumValues, 1)];
29+
// }

0 commit comments

Comments
 (0)