Skip to content

Rename @byteOffsetOf to @offsetOf #9073

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

Merged
merged 2 commits into from
Jun 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ test "pointer to non-bit-aligned field" {
}
{#code_end#}
<p>
This can be observed with {#link|@bitOffsetOf#} and {#link|byteOffsetOf#}:
This can be observed with {#link|@bitOffsetOf#} and {#link|offsetOf#}:
</p>
{#code_begin|test#}
const std = @import("std");
Expand All @@ -2733,9 +2733,9 @@ test "pointer to non-bit-aligned field" {
try expect(@bitOffsetOf(BitField, "b") == 3);
try expect(@bitOffsetOf(BitField, "c") == 6);

try expect(@byteOffsetOf(BitField, "a") == 0);
try expect(@byteOffsetOf(BitField, "b") == 0);
try expect(@byteOffsetOf(BitField, "c") == 0);
try expect(@offsetOf(BitField, "a") == 0);
try expect(@offsetOf(BitField, "b") == 0);
try expect(@offsetOf(BitField, "c") == 0);
}
}
{#code_end#}
Expand Down Expand Up @@ -7026,7 +7026,7 @@ fn func(y: *i32) void {
For packed structs, non-byte-aligned fields will share a byte offset, but they will have different
bit offsets.
</p>
{#see_also|@byteOffsetOf#}
{#see_also|@offsetOf#}
{#header_close#}

{#header_open|@boolToInt#}
Expand Down Expand Up @@ -7106,8 +7106,8 @@ fn func(y: *i32) void {
</p>
{#header_close#}

{#header_open|@byteOffsetOf#}
<pre>{#syntax#}@byteOffsetOf(comptime T: type, comptime field_name: []const u8) comptime_int{#endsyntax#}</pre>
{#header_open|@offsetOf#}
<pre>{#syntax#}@offsetOf(comptime T: type, comptime field_name: []const u8) comptime_int{#endsyntax#}</pre>
<p>
Returns the byte offset of a field relative to its containing struct.
</p>
Expand Down
26 changes: 13 additions & 13 deletions lib/std/os/bits/darwin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ pub const Kevent = extern struct {
// to make sure the struct is laid out the same. These values were
// produced from C code using the offsetof macro.
comptime {
assert(@byteOffsetOf(Kevent, "ident") == 0);
assert(@byteOffsetOf(Kevent, "filter") == 8);
assert(@byteOffsetOf(Kevent, "flags") == 10);
assert(@byteOffsetOf(Kevent, "fflags") == 12);
assert(@byteOffsetOf(Kevent, "data") == 16);
assert(@byteOffsetOf(Kevent, "udata") == 24);
assert(@offsetOf(Kevent, "ident") == 0);
assert(@offsetOf(Kevent, "filter") == 8);
assert(@offsetOf(Kevent, "flags") == 10);
assert(@offsetOf(Kevent, "fflags") == 12);
assert(@offsetOf(Kevent, "data") == 16);
assert(@offsetOf(Kevent, "udata") == 24);
}

pub const kevent64_s = extern struct {
Expand All @@ -210,13 +210,13 @@ pub const kevent64_s = extern struct {
// to make sure the struct is laid out the same. These values were
// produced from C code using the offsetof macro.
comptime {
assert(@byteOffsetOf(kevent64_s, "ident") == 0);
assert(@byteOffsetOf(kevent64_s, "filter") == 8);
assert(@byteOffsetOf(kevent64_s, "flags") == 10);
assert(@byteOffsetOf(kevent64_s, "fflags") == 12);
assert(@byteOffsetOf(kevent64_s, "data") == 16);
assert(@byteOffsetOf(kevent64_s, "udata") == 24);
assert(@byteOffsetOf(kevent64_s, "ext") == 32);
assert(@offsetOf(kevent64_s, "ident") == 0);
assert(@offsetOf(kevent64_s, "filter") == 8);
assert(@offsetOf(kevent64_s, "flags") == 10);
assert(@offsetOf(kevent64_s, "fflags") == 12);
assert(@offsetOf(kevent64_s, "data") == 16);
assert(@offsetOf(kevent64_s, "udata") == 24);
assert(@offsetOf(kevent64_s, "ext") == 32);
}

pub const mach_port_t = c_uint;
Expand Down
2 changes: 1 addition & 1 deletion lib/std/os/bits/dragonfly.zig
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub const dirent = extern struct {
d_name: [256]u8,

pub fn reclen(self: dirent) u16 {
return (@byteOffsetOf(dirent, "d_name") + self.d_namlen + 1 + 7) & ~@as(u16, 7);
return (@offsetOf(dirent, "d_name") + self.d_namlen + 1 + 7) & ~@as(u16, 7);
}
};

Expand Down
15 changes: 15 additions & 0 deletions lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ test "zig fmt: rewrite suspend without block expression" {
);
}

// TODO Remove this after zig 0.9.0 is released.
test "zig fmt: rewrite @byteOffsetOf to @offsetOf" {
try testTransform(
\\fn foo() void {
\\ @byteOffsetOf(Foo, "bar");
\\}
\\
,
\\fn foo() void {
\\ @offsetOf(Foo, "bar");
\\}
\\
);
}

test "zig fmt: simple top level comptime block" {
try testCanonical(
\\// line comment
Expand Down
7 changes: 6 additions & 1 deletion lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,12 @@ fn renderBuiltinCall(
) Error!void {
const token_tags = tree.tokens.items(.tag);

try renderToken(ais, tree, builtin_token, .none); // @name
const builtin_name = tokenSliceForRender(tree, builtin_token);
if (mem.eql(u8, builtin_name, "@byteOffsetOf")) {
try ais.writer().writeAll("@offsetOf");
} else {
try renderToken(ais, tree, builtin_token, .none); // @name
}

if (params.len == 0) {
try renderToken(ais, tree, builtin_token + 1, .none); // (
Expand Down
4 changes: 2 additions & 2 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2027,7 +2027,7 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: ast.Node.Index) Inner
.shl_exact,
.shr_exact,
.bit_offset_of,
.byte_offset_of,
.offset_of,
.cmpxchg_strong,
.cmpxchg_weak,
.splat,
Expand Down Expand Up @@ -6816,7 +6816,7 @@ fn builtinCall(
.shr_exact => return shiftOp(gz, scope, rl, node, params[0], params[1], .shr_exact),

.bit_offset_of => return offsetOf(gz, scope, rl, node, params[0], params[1], .bit_offset_of),
.byte_offset_of => return offsetOf(gz, scope, rl, node, params[0], params[1], .byte_offset_of),
.offset_of => return offsetOf(gz, scope, rl, node, params[0], params[1], .offset_of),

.c_undef => return simpleCBuiltin(gz, scope, rl, node, params[0], .c_undef),
.c_include => return simpleCBuiltin(gz, scope, rl, node, params[0], .c_include),
Expand Down
6 changes: 3 additions & 3 deletions src/BuiltinFn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const Tag = enum {
mul_add,
byte_swap,
bit_reverse,
byte_offset_of,
offset_of,
call,
c_define,
c_import,
Expand Down Expand Up @@ -235,9 +235,9 @@ pub const list = list: {
},
},
.{
"@byteOffsetOf",
"@offsetOf",
.{
.tag = .byte_offset_of,
.tag = .offset_of,
.param_count = 2,
},
},
Expand Down
6 changes: 3 additions & 3 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub fn analyzeBody(
.shl_exact => try sema.zirShlExact(block, inst),
.shr_exact => try sema.zirShrExact(block, inst),
.bit_offset_of => try sema.zirBitOffsetOf(block, inst),
.byte_offset_of => try sema.zirByteOffsetOf(block, inst),
.offset_of => try sema.zirOffsetOf(block, inst),
.cmpxchg_strong => try sema.zirCmpxchg(block, inst),
.cmpxchg_weak => try sema.zirCmpxchg(block, inst),
.splat => try sema.zirSplat(block, inst),
Expand Down Expand Up @@ -5860,10 +5860,10 @@ fn zirBitOffsetOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerE
return sema.mod.fail(&block.base, src, "TODO: Sema.zirBitOffsetOf", .{});
}

fn zirByteOffsetOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
fn zirOffsetOf(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
const src = inst_data.src();
return sema.mod.fail(&block.base, src, "TODO: Sema.zirByteOffsetOf", .{});
return sema.mod.fail(&block.base, src, "TODO: Sema.zirOffsetOf", .{});
}

fn zirCmpxchg(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {
Expand Down
10 changes: 5 additions & 5 deletions src/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,9 @@ pub const Inst = struct {
/// Implements the `@bitOffsetOf` builtin.
/// Uses the `pl_node` union field with payload `Bin`.
bit_offset_of,
/// Implements the `@byteOffsetOf` builtin.
/// Implements the `@offsetOf` builtin.
/// Uses the `pl_node` union field with payload `Bin`.
byte_offset_of,
offset_of,
/// Implements the `@cmpxchgStrong` builtin.
/// Uses the `pl_node` union field with payload `Cmpxchg`.
cmpxchg_strong,
Expand Down Expand Up @@ -1166,7 +1166,7 @@ pub const Inst = struct {
.shl_exact,
.shr_exact,
.bit_offset_of,
.byte_offset_of,
.offset_of,
.cmpxchg_strong,
.cmpxchg_weak,
.splat,
Expand Down Expand Up @@ -1436,7 +1436,7 @@ pub const Inst = struct {
.shr_exact = .pl_node,

.bit_offset_of = .pl_node,
.byte_offset_of = .pl_node,
.offset_of = .pl_node,
.cmpxchg_strong = .pl_node,
.cmpxchg_weak = .pl_node,
.splat = .pl_node,
Expand Down Expand Up @@ -2992,7 +2992,7 @@ const Writer = struct {
.mod,
.rem,
.bit_offset_of,
.byte_offset_of,
.offset_of,
.splat,
.reduce,
.atomic_load,
Expand Down
2 changes: 1 addition & 1 deletion src/air.zig
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ pub const Inst = struct {
pub const convertable_br_size = std.math.max(@sizeOf(BrBlockFlat), @sizeOf(Br));
pub const convertable_br_align = std.math.max(@alignOf(BrBlockFlat), @alignOf(Br));
comptime {
assert(@byteOffsetOf(BrBlockFlat, "base") == @byteOffsetOf(Br, "base"));
assert(@offsetOf(BrBlockFlat, "base") == @offsetOf(Br, "base"));
}

pub const BrBlockFlat = struct {
Expand Down
6 changes: 3 additions & 3 deletions src/stage1/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ enum BuiltinFnId {
BuiltinFnIdPtrToInt,
BuiltinFnIdTagName,
BuiltinFnIdFieldParentPtr,
BuiltinFnIdByteOffsetOf,
BuiltinFnIdOffsetOf,
BuiltinFnIdBitOffsetOf,
BuiltinFnIdAsyncCall,
BuiltinFnIdShlExact,
Expand Down Expand Up @@ -2576,7 +2576,7 @@ enum IrInstSrcId {
IrInstSrcIdPanic,
IrInstSrcIdTagName,
IrInstSrcIdFieldParentPtr,
IrInstSrcIdByteOffsetOf,
IrInstSrcIdOffsetOf,
IrInstSrcIdBitOffsetOf,
IrInstSrcIdTypeInfo,
IrInstSrcIdType,
Expand Down Expand Up @@ -4047,7 +4047,7 @@ struct IrInstGenFieldParentPtr {
TypeStructField *field;
};

struct IrInstSrcByteOffsetOf {
struct IrInstSrcOffsetOf {
IrInstSrc base;

IrInstSrc *type_value;
Expand Down
16 changes: 8 additions & 8 deletions src/stage1/astgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,8 @@ void destroy_instruction_src(IrInstSrc *inst) {
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcPanic *>(inst));
case IrInstSrcIdFieldParentPtr:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcFieldParentPtr *>(inst));
case IrInstSrcIdByteOffsetOf:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcByteOffsetOf *>(inst));
case IrInstSrcIdOffsetOf:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcOffsetOf *>(inst));
case IrInstSrcIdBitOffsetOf:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcBitOffsetOf *>(inst));
case IrInstSrcIdTypeInfo:
Expand Down Expand Up @@ -777,8 +777,8 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcFieldParentPtr *) {
return IrInstSrcIdFieldParentPtr;
}

static constexpr IrInstSrcId ir_inst_id(IrInstSrcByteOffsetOf *) {
return IrInstSrcIdByteOffsetOf;
static constexpr IrInstSrcId ir_inst_id(IrInstSrcOffsetOf *) {
return IrInstSrcIdOffsetOf;
}

static constexpr IrInstSrcId ir_inst_id(IrInstSrcBitOffsetOf *) {
Expand Down Expand Up @@ -2472,10 +2472,10 @@ static IrInstSrc *ir_build_field_parent_ptr_src(Stage1AstGen *ag, Scope *scope,
return &inst->base;
}

static IrInstSrc *ir_build_byte_offset_of(Stage1AstGen *ag, Scope *scope, AstNode *source_node,
static IrInstSrc *ir_build_offset_of(Stage1AstGen *ag, Scope *scope, AstNode *source_node,
IrInstSrc *type_value, IrInstSrc *field_name)
{
IrInstSrcByteOffsetOf *instruction = ir_build_instruction<IrInstSrcByteOffsetOf>(ag, scope, source_node);
IrInstSrcOffsetOf *instruction = ir_build_instruction<IrInstSrcOffsetOf>(ag, scope, source_node);
instruction->type_value = type_value;
instruction->field_name = field_name;

Expand Down Expand Up @@ -4945,7 +4945,7 @@ static IrInstSrc *astgen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, AstNode
arg0_value, arg1_value, arg2_value);
return ir_lval_wrap(ag, scope, field_parent_ptr, lval, result_loc);
}
case BuiltinFnIdByteOffsetOf:
case BuiltinFnIdOffsetOf:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstSrc *arg0_value = astgen_node(ag, arg0_node, scope);
Expand All @@ -4957,7 +4957,7 @@ static IrInstSrc *astgen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, AstNode
if (arg1_value == ag->codegen->invalid_inst_src)
return arg1_value;

IrInstSrc *offset_of = ir_build_byte_offset_of(ag, scope, node, arg0_value, arg1_value);
IrInstSrc *offset_of = ir_build_offset_of(ag, scope, node, arg0_value, arg1_value);
return ir_lval_wrap(ag, scope, offset_of, lval, result_loc);
}
case BuiltinFnIdBitOffsetOf:
Expand Down
2 changes: 1 addition & 1 deletion src/stage1/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8902,7 +8902,7 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdPtrToInt, "ptrToInt", 1);
create_builtin_fn(g, BuiltinFnIdTagName, "tagName", 1);
create_builtin_fn(g, BuiltinFnIdFieldParentPtr, "fieldParentPtr", 3);
create_builtin_fn(g, BuiltinFnIdByteOffsetOf, "byteOffsetOf", 2);
create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2);
create_builtin_fn(g, BuiltinFnIdBitOffsetOf, "bitOffsetOf", 2);
create_builtin_fn(g, BuiltinFnIdDivExact, "divExact", 2);
create_builtin_fn(g, BuiltinFnIdDivTrunc, "divTrunc", 2);
Expand Down
8 changes: 4 additions & 4 deletions src/stage1/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17082,7 +17082,7 @@ static TypeStructField *validate_host_int_byte_offset(IrAnalyze *ira,
return field;
}

static IrInstGen *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira, IrInstSrcByteOffsetOf *instruction) {
static IrInstGen *ir_analyze_instruction_offset_of(IrAnalyze *ira, IrInstSrcOffsetOf *instruction) {
IrInstGen *type_value = instruction->type_value->child;
if (type_is_invalid(type_value->value->type))
return ira->codegen->invalid_inst_gen;
Expand Down Expand Up @@ -24368,8 +24368,8 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
return ir_analyze_instruction_enum_tag_name(ira, (IrInstSrcTagName *)instruction);
case IrInstSrcIdFieldParentPtr:
return ir_analyze_instruction_field_parent_ptr(ira, (IrInstSrcFieldParentPtr *)instruction);
case IrInstSrcIdByteOffsetOf:
return ir_analyze_instruction_byte_offset_of(ira, (IrInstSrcByteOffsetOf *)instruction);
case IrInstSrcIdOffsetOf:
return ir_analyze_instruction_offset_of(ira, (IrInstSrcOffsetOf *)instruction);
case IrInstSrcIdBitOffsetOf:
return ir_analyze_instruction_bit_offset_of(ira, (IrInstSrcBitOffsetOf *)instruction);
case IrInstSrcIdTypeInfo:
Expand Down Expand Up @@ -24824,7 +24824,7 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {
case IrInstSrcIdTypeName:
case IrInstSrcIdTagName:
case IrInstSrcIdFieldParentPtr:
case IrInstSrcIdByteOffsetOf:
case IrInstSrcIdOffsetOf:
case IrInstSrcIdBitOffsetOf:
case IrInstSrcIdTypeInfo:
case IrInstSrcIdType:
Expand Down
12 changes: 6 additions & 6 deletions src/stage1/ir_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {
return "SrcTagName";
case IrInstSrcIdFieldParentPtr:
return "SrcFieldParentPtr";
case IrInstSrcIdByteOffsetOf:
return "SrcByteOffsetOf";
case IrInstSrcIdOffsetOf:
return "SrcOffsetOf";
case IrInstSrcIdBitOffsetOf:
return "SrcBitOffsetOf";
case IrInstSrcIdTypeInfo:
Expand Down Expand Up @@ -2292,8 +2292,8 @@ static void ir_print_field_parent_ptr(IrPrintGen *irp, IrInstGenFieldParentPtr *
fprintf(irp->f, ")");
}

static void ir_print_byte_offset_of(IrPrintSrc *irp, IrInstSrcByteOffsetOf *instruction) {
fprintf(irp->f, "@byte_offset_of(");
static void ir_print_offset_of(IrPrintSrc *irp, IrInstSrcOffsetOf *instruction) {
fprintf(irp->f, "@offset_of(");
ir_print_other_inst_src(irp, instruction->type_value);
fprintf(irp->f, ",");
ir_print_other_inst_src(irp, instruction->field_name);
Expand Down Expand Up @@ -2946,8 +2946,8 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai
case IrInstSrcIdFieldParentPtr:
ir_print_field_parent_ptr(irp, (IrInstSrcFieldParentPtr *)instruction);
break;
case IrInstSrcIdByteOffsetOf:
ir_print_byte_offset_of(irp, (IrInstSrcByteOffsetOf *)instruction);
case IrInstSrcIdOffsetOf:
ir_print_offset_of(irp, (IrInstSrcOffsetOf *)instruction);
break;
case IrInstSrcIdBitOffsetOf:
ir_print_bit_offset_of(irp, (IrInstSrcBitOffsetOf *)instruction);
Expand Down
2 changes: 1 addition & 1 deletion src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ fn transSimpleOffsetOfExpr(
const quoted_field_name = try std.fmt.allocPrint(c.arena, "\"{s}\"", .{raw_field_name});
const field_name_node = try Tag.string_literal.create(c.arena, quoted_field_name);

return Tag.byte_offset_of.create(c.arena, .{
return Tag.offset_of.create(c.arena, .{
.lhs = type_node,
.rhs = field_name_node,
});
Expand Down
Loading