Skip to content

Commit

Permalink
Merge pull request ziglang#19470 from jacobly0/field-parent-ptr
Browse files Browse the repository at this point in the history
Rework `@fieldParentPtr` to use RLS
  • Loading branch information
andrewrk authored Mar 31, 2024
2 parents aff71c6 + e5ba70b commit a6ed3e6
Show file tree
Hide file tree
Showing 105 changed files with 7,223 additions and 5,165 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/clang_options_data.zig"
"${CMAKE_SOURCE_DIR}/src/codegen.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/c.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/c/type.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/c/Type.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/llvm.zig"
"${CMAKE_SOURCE_DIR}/src/codegen/llvm/bindings.zig"
"${CMAKE_SOURCE_DIR}/src/glibc.zig"
Expand Down
4 changes: 1 addition & 3 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ pub fn build(b: *std.Build) !void {
const only_c = b.option(bool, "only-c", "Translate the Zig compiler to C code, with only the C backend enabled") orelse false;
const target = t: {
var default_target: std.zig.CrossTarget = .{};
if (only_c) {
default_target.ofmt = .c;
}
default_target.ofmt = b.option(std.Target.ObjectFormat, "ofmt", "Object format to target") orelse if (only_c) .c else null;
break :t b.standardTargetOptions(.{ .default_target = default_target });
};

Expand Down
5 changes: 2 additions & 3 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -3107,7 +3107,7 @@ test "struct namespaced variable" {
// struct field order is determined by the compiler for optimal performance.
// however, you can still calculate a struct base pointer given a field pointer:
fn setYBasedOnX(x: *f32, y: f32) void {
const point = @fieldParentPtr(Point, "x", x);
const point: *Point = @fieldParentPtr("x", x);
point.y = y;
}
test "field parent pointer" {
Expand Down Expand Up @@ -8757,8 +8757,7 @@ test "decl access by string" {
{#header_close#}

{#header_open|@fieldParentPtr#}
<pre>{#syntax#}@fieldParentPtr(comptime ParentType: type, comptime field_name: []const u8,
field_ptr: *T) *ParentType{#endsyntax#}</pre>
<pre>{#syntax#}@fieldParentPtr(comptime field_name: []const u8, field_ptr: *T) anytype{#endsyntax#}</pre>
<p>
Given a pointer to a field, returns the base pointer of a struct.
</p>
Expand Down
12 changes: 6 additions & 6 deletions lib/compiler/aro/aro/pragmas/gcc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ const Directive = enum {
};

fn beforePreprocess(pragma: *Pragma, comp: *Compilation) void {
var self = @fieldParentPtr(GCC, "pragma", pragma);
var self: *GCC = @fieldParentPtr("pragma", pragma);
self.original_options = comp.diagnostics.options;
}

fn beforeParse(pragma: *Pragma, comp: *Compilation) void {
var self = @fieldParentPtr(GCC, "pragma", pragma);
var self: *GCC = @fieldParentPtr("pragma", pragma);
comp.diagnostics.options = self.original_options;
self.options_stack.items.len = 0;
}

fn afterParse(pragma: *Pragma, comp: *Compilation) void {
var self = @fieldParentPtr(GCC, "pragma", pragma);
var self: *GCC = @fieldParentPtr("pragma", pragma);
comp.diagnostics.options = self.original_options;
self.options_stack.items.len = 0;
}
Expand All @@ -60,7 +60,7 @@ pub fn init(allocator: mem.Allocator) !*Pragma {
}

fn deinit(pragma: *Pragma, comp: *Compilation) void {
var self = @fieldParentPtr(GCC, "pragma", pragma);
var self: *GCC = @fieldParentPtr("pragma", pragma);
self.options_stack.deinit(comp.gpa);
comp.gpa.destroy(self);
}
Expand Down Expand Up @@ -108,7 +108,7 @@ fn diagnosticHandler(self: *GCC, pp: *Preprocessor, start_idx: TokenIndex) Pragm
}

fn preprocessorHandler(pragma: *Pragma, pp: *Preprocessor, start_idx: TokenIndex) Pragma.Error!void {
var self = @fieldParentPtr(GCC, "pragma", pragma);
var self: *GCC = @fieldParentPtr("pragma", pragma);
const directive_tok = pp.tokens.get(start_idx + 1);
if (directive_tok.id == .nl) return;

Expand Down Expand Up @@ -174,7 +174,7 @@ fn preprocessorHandler(pragma: *Pragma, pp: *Preprocessor, start_idx: TokenIndex
}

fn parserHandler(pragma: *Pragma, p: *Parser, start_idx: TokenIndex) Compilation.Error!void {
var self = @fieldParentPtr(GCC, "pragma", pragma);
var self: *GCC = @fieldParentPtr("pragma", pragma);
const directive_tok = p.pp.tokens.get(start_idx + 1);
if (directive_tok.id == .nl) return;
const name = p.pp.expandedSlice(directive_tok);
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/aro/aro/pragmas/message.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn init(allocator: mem.Allocator) !*Pragma {
}

fn deinit(pragma: *Pragma, comp: *Compilation) void {
const self = @fieldParentPtr(Message, "pragma", pragma);
const self: *Message = @fieldParentPtr("pragma", pragma);
comp.gpa.destroy(self);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/compiler/aro/aro/pragmas/once.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ pub fn init(allocator: mem.Allocator) !*Pragma {
}

fn afterParse(pragma: *Pragma, _: *Compilation) void {
var self = @fieldParentPtr(Once, "pragma", pragma);
var self: *Once = @fieldParentPtr("pragma", pragma);
self.pragma_once.clearRetainingCapacity();
}

fn deinit(pragma: *Pragma, comp: *Compilation) void {
var self = @fieldParentPtr(Once, "pragma", pragma);
var self: *Once = @fieldParentPtr("pragma", pragma);
self.pragma_once.deinit();
comp.gpa.destroy(self);
}

fn preprocessorHandler(pragma: *Pragma, pp: *Preprocessor, start_idx: TokenIndex) Pragma.Error!void {
var self = @fieldParentPtr(Once, "pragma", pragma);
var self: *Once = @fieldParentPtr("pragma", pragma);
const name_tok = pp.tokens.get(start_idx);
const next = pp.tokens.get(start_idx + 1);
if (next.id != .nl) {
Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/aro/aro/pragmas/pack.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ pub fn init(allocator: mem.Allocator) !*Pragma {
}

fn deinit(pragma: *Pragma, comp: *Compilation) void {
var self = @fieldParentPtr(Pack, "pragma", pragma);
var self: *Pack = @fieldParentPtr("pragma", pragma);
self.stack.deinit(comp.gpa);
comp.gpa.destroy(self);
}

fn parserHandler(pragma: *Pragma, p: *Parser, start_idx: TokenIndex) Compilation.Error!void {
var pack = @fieldParentPtr(Pack, "pragma", pragma);
var pack: *Pack = @fieldParentPtr("pragma", pragma);
var idx = start_idx + 1;
const l_paren = p.pp.tokens.get(idx);
if (l_paren.id != .l_paren) {
Expand Down
10 changes: 5 additions & 5 deletions lib/compiler/aro/backend/Object.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn create(gpa: Allocator, target: std.Target) !*Object {

pub fn deinit(obj: *Object) void {
switch (obj.format) {
.elf => @fieldParentPtr(Elf, "obj", obj).deinit(),
.elf => @as(*Elf, @fieldParentPtr("obj", obj)).deinit(),
else => unreachable,
}
}
Expand All @@ -32,7 +32,7 @@ pub const Section = union(enum) {

pub fn getSection(obj: *Object, section: Section) !*std.ArrayList(u8) {
switch (obj.format) {
.elf => return @fieldParentPtr(Elf, "obj", obj).getSection(section),
.elf => return @as(*Elf, @fieldParentPtr("obj", obj)).getSection(section),
else => unreachable,
}
}
Expand All @@ -53,21 +53,21 @@ pub fn declareSymbol(
size: u64,
) ![]const u8 {
switch (obj.format) {
.elf => return @fieldParentPtr(Elf, "obj", obj).declareSymbol(section, name, linkage, @"type", offset, size),
.elf => return @as(*Elf, @fieldParentPtr("obj", obj)).declareSymbol(section, name, linkage, @"type", offset, size),
else => unreachable,
}
}

pub fn addRelocation(obj: *Object, name: []const u8, section: Section, address: u64, addend: i64) !void {
switch (obj.format) {
.elf => return @fieldParentPtr(Elf, "obj", obj).addRelocation(name, section, address, addend),
.elf => return @as(*Elf, @fieldParentPtr("obj", obj)).addRelocation(name, section, address, addend),
else => unreachable,
}
}

pub fn finish(obj: *Object, file: std.fs.File) !void {
switch (obj.format) {
.elf => return @fieldParentPtr(Elf, "obj", obj).finish(file),
.elf => return @as(*Elf, @fieldParentPtr("obj", obj)).finish(file),
else => unreachable,
}
}
20 changes: 10 additions & 10 deletions lib/compiler/aro_translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1098,13 +1098,13 @@ pub fn ScopeExtra(comptime ScopeExtraContext: type, comptime ScopeExtraType: typ
}
};

pub fn findBlockScope(inner: *ScopeExtraScope, c: *ScopeExtraContext) !*ScopeExtraScope.Block {
pub fn findBlockScope(inner: *ScopeExtraScope, c: *ScopeExtraContext) !*Block {
var scope = inner;
while (true) {
switch (scope.id) {
.root => unreachable,
.block => return @fieldParentPtr(Block, "base", scope),
.condition => return @fieldParentPtr(Condition, "base", scope).getBlockScope(c),
.block => return @fieldParentPtr("base", scope),
.condition => return @as(*Condition, @fieldParentPtr("base", scope)).getBlockScope(c),
else => scope = scope.parent.?,
}
}
Expand All @@ -1116,7 +1116,7 @@ pub fn ScopeExtra(comptime ScopeExtraContext: type, comptime ScopeExtraType: typ
switch (scope.id) {
.root => unreachable,
.block => {
const block = @fieldParentPtr(Block, "base", scope);
const block: *Block = @fieldParentPtr("base", scope);
if (block.return_type) |ty| return ty;
scope = scope.parent.?;
},
Expand All @@ -1128,15 +1128,15 @@ pub fn ScopeExtra(comptime ScopeExtraContext: type, comptime ScopeExtraType: typ
pub fn getAlias(scope: *ScopeExtraScope, name: []const u8) []const u8 {
return switch (scope.id) {
.root => return name,
.block => @fieldParentPtr(Block, "base", scope).getAlias(name),
.block => @as(*Block, @fieldParentPtr("base", scope)).getAlias(name),
.loop, .do_loop, .condition => scope.parent.?.getAlias(name),
};
}

pub fn contains(scope: *ScopeExtraScope, name: []const u8) bool {
return switch (scope.id) {
.root => @fieldParentPtr(Root, "base", scope).contains(name),
.block => @fieldParentPtr(Block, "base", scope).contains(name),
.root => @as(*Root, @fieldParentPtr("base", scope)).contains(name),
.block => @as(*Block, @fieldParentPtr("base", scope)).contains(name),
.loop, .do_loop, .condition => scope.parent.?.contains(name),
};
}
Expand All @@ -1158,11 +1158,11 @@ pub fn ScopeExtra(comptime ScopeExtraContext: type, comptime ScopeExtraType: typ
while (true) {
switch (scope.id) {
.root => {
const root = @fieldParentPtr(Root, "base", scope);
const root: *Root = @fieldParentPtr("base", scope);
return root.nodes.append(node);
},
.block => {
const block = @fieldParentPtr(Block, "base", scope);
const block: *Block = @fieldParentPtr("base", scope);
return block.statements.append(node);
},
else => scope = scope.parent.?,
Expand All @@ -1184,7 +1184,7 @@ pub fn ScopeExtra(comptime ScopeExtraContext: type, comptime ScopeExtraType: typ
switch (scope.id) {
.root => return,
.block => {
const block = @fieldParentPtr(Block, "base", scope);
const block: *Block = @fieldParentPtr("base", scope);
if (block.variable_discards.get(name)) |discard| {
discard.data.should_skip = true;
return;
Expand Down
16 changes: 8 additions & 8 deletions lib/compiler/aro_translate_c/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ pub const Node = extern union {
return null;

if (self.ptr_otherwise.tag == t)
return @fieldParentPtr(t.Type(), "base", self.ptr_otherwise);
return @alignCast(@fieldParentPtr("base", self.ptr_otherwise));

return null;
}
Expand Down Expand Up @@ -1220,7 +1220,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
});
},
.pub_var_simple, .var_simple => {
const payload = @fieldParentPtr(Payload.SimpleVarDecl, "base", node.ptr_otherwise).data;
const payload = @as(*Payload.SimpleVarDecl, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;
if (node.tag() == .pub_var_simple) _ = try c.addToken(.keyword_pub, "pub");
const const_tok = try c.addToken(.keyword_const, "const");
_ = try c.addIdentifier(payload.name);
Expand Down Expand Up @@ -1293,7 +1293,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
},
.var_decl => return renderVar(c, node),
.arg_redecl, .alias => {
const payload = @fieldParentPtr(Payload.ArgRedecl, "base", node.ptr_otherwise).data;
const payload = @as(*Payload.ArgRedecl, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;
if (node.tag() == .alias) _ = try c.addToken(.keyword_pub, "pub");
const mut_tok = if (node.tag() == .alias)
try c.addToken(.keyword_const, "const")
Expand Down Expand Up @@ -1492,7 +1492,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
});
},
.c_pointer, .single_pointer => {
const payload = @fieldParentPtr(Payload.Pointer, "base", node.ptr_otherwise).data;
const payload = @as(*Payload.Pointer, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;

const asterisk = if (node.tag() == .single_pointer)
try c.addToken(.asterisk, "*")
Expand Down Expand Up @@ -2085,7 +2085,7 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
}

fn renderRecord(c: *Context, node: Node) !NodeIndex {
const payload = @fieldParentPtr(Payload.Record, "base", node.ptr_otherwise).data;
const payload = @as(*Payload.Record, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;
if (payload.layout == .@"packed")
_ = try c.addToken(.keyword_packed, "packed")
else if (payload.layout == .@"extern")
Expand Down Expand Up @@ -2487,7 +2487,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
}

fn renderPrefixOp(c: *Context, node: Node, tag: std.zig.Ast.Node.Tag, tok_tag: TokenTag, bytes: []const u8) !NodeIndex {
const payload = @fieldParentPtr(Payload.UnOp, "base", node.ptr_otherwise).data;
const payload = @as(*Payload.UnOp, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;
return c.addNode(.{
.tag = tag,
.main_token = try c.addToken(tok_tag, bytes),
Expand All @@ -2499,7 +2499,7 @@ fn renderPrefixOp(c: *Context, node: Node, tag: std.zig.Ast.Node.Tag, tok_tag: T
}

fn renderBinOpGrouped(c: *Context, node: Node, tag: std.zig.Ast.Node.Tag, tok_tag: TokenTag, bytes: []const u8) !NodeIndex {
const payload = @fieldParentPtr(Payload.BinOp, "base", node.ptr_otherwise).data;
const payload = @as(*Payload.BinOp, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;
const lhs = try renderNodeGrouped(c, payload.lhs);
return c.addNode(.{
.tag = tag,
Expand All @@ -2512,7 +2512,7 @@ fn renderBinOpGrouped(c: *Context, node: Node, tag: std.zig.Ast.Node.Tag, tok_ta
}

fn renderBinOp(c: *Context, node: Node, tag: std.zig.Ast.Node.Tag, tok_tag: TokenTag, bytes: []const u8) !NodeIndex {
const payload = @fieldParentPtr(Payload.BinOp, "base", node.ptr_otherwise).data;
const payload = @as(*Payload.BinOp, @alignCast(@fieldParentPtr("base", node.ptr_otherwise))).data;
const lhs = try renderNode(c, payload.lhs);
return c.addNode(.{
.tag = tag,
Expand Down
Loading

0 comments on commit a6ed3e6

Please sign in to comment.