-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
stage2: Implement explicit backing integers for packed structs #12379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stage2: Implement explicit backing integers for packed structs #12379
Conversation
a9c36be
to
23f23ac
Compare
This is exciting Will probably simplify a lot of code like this: /// https://nodejs.org/api/os.html#osplatform
pub const OperatingSystem = enum(u16) {
none = 0,
all = all_value,
_,
pub const aix: u16 = 1 << 1;
pub const darwin: u16 = 1 << 2;
pub const freebsd: u16 = 1 << 3;
pub const linux: u16 = 1 << 4;
pub const openbsd: u16 = 1 << 5;
pub const sunos: u16 = 1 << 6;
pub const win32: u16 = 1 << 7;
pub const android: u16 = 1 << 8;
pub const all_value: u16 = aix | darwin | freebsd | linux | openbsd | sunos | win32 | android;
pub fn isMatch(this: OperatingSystem) bool {
if (comptime Environment.isLinux) {
return (@enumToInt(this) & linux) != 0;
} else if (comptime Environment.isMac) {
return (@enumToInt(this) & darwin) != 0;
} else {
return false;
}
}
const NameMap = ComptimeStringMap(u16, .{
.{ "aix", aix },
.{ "darwin", darwin },
.{ "freebsd", freebsd },
.{ "linux", linux },
.{ "openbsd", openbsd },
.{ "sunos", sunos },
.{ "win32", win32 },
.{ "android", android },
});
pub fn apply(this_: OperatingSystem, str: []const u8) OperatingSystem {
if (str.len == 0) {
return this_;
}
const this = @enumToInt(this_);
const is_not = str[0] == '!';
const offset: usize = if (str[0] == '!') 1 else 0;
const field: u16 = NameMap.get(str[offset..]) orelse return this_;
if (is_not) {
return @intToEnum(OperatingSystem, this & ~field);
} else {
return @intToEnum(OperatingSystem, this | field);
}
}
}; |
23f23ac
to
5136e82
Compare
I found some underlying issues with my previous approach and have just pushed an implementation that should be much more correct. The ZIR encoding now handles the case where the type requires a body properly and the layout of packed structs is not force-resolved while resolving their fields. However, I have run into an issue I'm not sure how to solve. With my changes
The |
Hmmm, that's a good question. In the interest of managing complexity, I think I would suggest a different approach, based on the observation that the intRem function is only ever called from Sema:
The idea being to move this function to Sema, and accept sema, block, src arguments. I made this change to other functions recently and it was a satisfactory approach. This will also allow you to grab the Allocator and Target from Sema instead of passing them explicitly. |
5136e82
to
b1acd4a
Compare
1e1e5f7
to
7c9936e
Compare
Thanks for the update to Autodoc! |
Not breaking it is the least I can do :) |
43411f6
to
1dd36a3
Compare
Now the backing integer of a packed struct type may be explicitly specified with e.g. `packed struct(u32) { ... }`.
1dd36a3
to
0d32b73
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work!
…backing-int stage2: Implement explicit backing integers for packed structs
Now the backing integer of a packed struct type may be explicitly
specified with e.g.
packed struct(u32) { ... }
.I haven't touched
Sema.zig
this extensively before, so any code/style pointers there are certainly appreciated.Related: #10113, #5049