authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-21 13:10:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-21 13:10:21-04:00
log5a21d3dce0c020833bfe426049e2391ae3977846
treed1f5c329609672edbc26cb892649cc1f3d36fc51
parent44f2ee101f244342ec7e4cd81bb21d9a0c23267b
parentf2186e5fa753ef84003fcc21cd60692e009a8941
signature Commit is signed but in an unrecognized format.

Merge branch 'BitByteOffsetOfs' of https://github.com/raulgrell/zig into raulgrell-BitByteOffsetOfs


7 files changed, 175 insertions(+), 55 deletions(-)

doc/langref.html.in+7-1
...@@ -5869,11 +5869,17 @@ fn add(a: i32, b: i32) i32 {...@@ -5869,11 +5869,17 @@ fn add(a: i32, b: i32) i32 {
5869 {#see_also|@inlineCall#}5869 {#see_also|@inlineCall#}
5870 {#header_close#}5870 {#header_close#}
5871 {#header_open|@offsetOf#}5871 {#header_open|@offsetOf#}
5872 <pre>{#syntax#}@offsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int{#endsyntax#}</pre>5872 <pre>{#syntax#}@byteOffsetOf(comptime T: type, comptime field_name: [] const u8) comptime_int{#endsyntax#}</pre>
5873 <p>5873 <p>
5874 This function returns the byte offset of a field relative to its containing struct.5874 This function returns the byte offset of a field relative to its containing struct.
5875 </p>5875 </p>
5876 {#header_close#}5876 {#header_close#}
5877 {#header_open|@bitOffsetOf#}
5878 <pre><code class="zig">@bitOffsetOf(comptime T: type, comptime field_name: [] const u8) (number literal)</code></pre>
5879 <p>
5880 This function returns the bit offset of a field relative to its containing struct.
5881 </p>
5882 {#header_close#}
5877 {#header_open|@OpaqueType#}5883 {#header_open|@OpaqueType#}
5878 <pre>{#syntax#}@OpaqueType() type{#endsyntax#}</pre>5884 <pre>{#syntax#}@OpaqueType() type{#endsyntax#}</pre>
5879 <p>5885 <p>
src/all_types.hpp+12-3
...@@ -1409,7 +1409,8 @@ enum BuiltinFnId {...@@ -1409,7 +1409,8 @@ enum BuiltinFnId {
1409 BuiltinFnIdTagName,1409 BuiltinFnIdTagName,
1410 BuiltinFnIdTagType,1410 BuiltinFnIdTagType,
1411 BuiltinFnIdFieldParentPtr,1411 BuiltinFnIdFieldParentPtr,
1412 BuiltinFnIdOffsetOf,1412 BuiltinFnIdByteOffsetOf,
1413 BuiltinFnIdBitOffsetOf,
1413 BuiltinFnIdInlineCall,1414 BuiltinFnIdInlineCall,
1414 BuiltinFnIdNoInlineCall,1415 BuiltinFnIdNoInlineCall,
1415 BuiltinFnIdNewStackCall,1416 BuiltinFnIdNewStackCall,
...@@ -2134,7 +2135,8 @@ enum IrInstructionId {...@@ -2134,7 +2135,8 @@ enum IrInstructionId {
2134 IrInstructionIdTagName,2135 IrInstructionIdTagName,
2135 IrInstructionIdTagType,2136 IrInstructionIdTagType,
2136 IrInstructionIdFieldParentPtr,2137 IrInstructionIdFieldParentPtr,
2137 IrInstructionIdOffsetOf,2138 IrInstructionIdByteOffsetOf,
2139 IrInstructionIdBitOffsetOf,
2138 IrInstructionIdTypeInfo,2140 IrInstructionIdTypeInfo,
2139 IrInstructionIdTypeId,2141 IrInstructionIdTypeId,
2140 IrInstructionIdSetEvalBranchQuota,2142 IrInstructionIdSetEvalBranchQuota,
...@@ -3037,7 +3039,14 @@ struct IrInstructionFieldParentPtr {...@@ -3037,7 +3039,14 @@ struct IrInstructionFieldParentPtr {
3037 TypeStructField *field;3039 TypeStructField *field;
3038};3040};
30393041
3040struct IrInstructionOffsetOf {3042struct IrInstructionByteOffsetOf {
3043 IrInstruction base;
3044
3045 IrInstruction *type_value;
3046 IrInstruction *field_name;
3047};
3048
3049struct IrInstructionBitOffsetOf {
3041 IrInstruction base;3050 IrInstruction base;
30423051
3043 IrInstruction *type_value;3052 IrInstruction *type_value;
src/codegen.cpp+4-2
...@@ -5098,7 +5098,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -5098,7 +5098,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
5098 case IrInstructionIdTypeName:5098 case IrInstructionIdTypeName:
5099 case IrInstructionIdDeclRef:5099 case IrInstructionIdDeclRef:
5100 case IrInstructionIdSwitchVar:5100 case IrInstructionIdSwitchVar:
5101 case IrInstructionIdOffsetOf:5101 case IrInstructionIdByteOffsetOf:
5102 case IrInstructionIdBitOffsetOf:
5102 case IrInstructionIdTypeInfo:5103 case IrInstructionIdTypeInfo:
5103 case IrInstructionIdTypeId:5104 case IrInstructionIdTypeId:
5104 case IrInstructionIdSetEvalBranchQuota:5105 case IrInstructionIdSetEvalBranchQuota:
...@@ -6671,7 +6672,8 @@ static void define_builtin_fns(CodeGen *g) {...@@ -6671,7 +6672,8 @@ static void define_builtin_fns(CodeGen *g) {
6671 create_builtin_fn(g, BuiltinFnIdTagName, "tagName", 1);6672 create_builtin_fn(g, BuiltinFnIdTagName, "tagName", 1);
6672 create_builtin_fn(g, BuiltinFnIdTagType, "TagType", 1);6673 create_builtin_fn(g, BuiltinFnIdTagType, "TagType", 1);
6673 create_builtin_fn(g, BuiltinFnIdFieldParentPtr, "fieldParentPtr", 3);6674 create_builtin_fn(g, BuiltinFnIdFieldParentPtr, "fieldParentPtr", 3);
6674 create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2);6675 create_builtin_fn(g, BuiltinFnIdByteOffsetOf, "byteOffsetOf", 2);
6676 create_builtin_fn(g, BuiltinFnIdBitOffsetOf, "bitOffsetOf", 2);
6675 create_builtin_fn(g, BuiltinFnIdDivExact, "divExact", 2);6677 create_builtin_fn(g, BuiltinFnIdDivExact, "divExact", 2);
6676 create_builtin_fn(g, BuiltinFnIdDivTrunc, "divTrunc", 2);6678 create_builtin_fn(g, BuiltinFnIdDivTrunc, "divTrunc", 2);
6677 create_builtin_fn(g, BuiltinFnIdDivFloor, "divFloor", 2);6679 create_builtin_fn(g, BuiltinFnIdDivFloor, "divFloor", 2);
src/ir.cpp+91-27
...@@ -720,8 +720,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFieldParentPtr *...@@ -720,8 +720,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionFieldParentPtr *
720 return IrInstructionIdFieldParentPtr;720 return IrInstructionIdFieldParentPtr;
721}721}
722722
723static constexpr IrInstructionId ir_instruction_id(IrInstructionOffsetOf *) {723static constexpr IrInstructionId ir_instruction_id(IrInstructionByteOffsetOf *) {
724 return IrInstructionIdOffsetOf;724 return IrInstructionIdByteOffsetOf;
725}
726
727static constexpr IrInstructionId ir_instruction_id(IrInstructionBitOffsetOf *) {
728 return IrInstructionIdBitOffsetOf;
725}729}
726730
727static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) {731static constexpr IrInstructionId ir_instruction_id(IrInstructionTypeInfo *) {
...@@ -2628,10 +2632,23 @@ static IrInstruction *ir_build_field_parent_ptr(IrBuilder *irb, Scope *scope, As...@@ -2628,10 +2632,23 @@ static IrInstruction *ir_build_field_parent_ptr(IrBuilder *irb, Scope *scope, As
2628 return &instruction->base;2632 return &instruction->base;
2629}2633}
26302634
2631static IrInstruction *ir_build_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node,2635static IrInstruction *ir_build_byte_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node,
2636 IrInstruction *type_value, IrInstruction *field_name)
2637{
2638 IrInstructionByteOffsetOf *instruction = ir_build_instruction<IrInstructionByteOffsetOf>(irb, scope, source_node);
2639 instruction->type_value = type_value;
2640 instruction->field_name = field_name;
2641
2642 ir_ref_instruction(type_value, irb->current_basic_block);
2643 ir_ref_instruction(field_name, irb->current_basic_block);
2644
2645 return &instruction->base;
2646}
2647
2648static IrInstruction *ir_build_bit_offset_of(IrBuilder *irb, Scope *scope, AstNode *source_node,
2632 IrInstruction *type_value, IrInstruction *field_name)2649 IrInstruction *type_value, IrInstruction *field_name)
2633{2650{
2634 IrInstructionOffsetOf *instruction = ir_build_instruction<IrInstructionOffsetOf>(irb, scope, source_node);2651 IrInstructionBitOffsetOf *instruction = ir_build_instruction<IrInstructionBitOffsetOf>(irb, scope, source_node);
2635 instruction->type_value = type_value;2652 instruction->type_value = type_value;
2636 instruction->field_name = field_name;2653 instruction->field_name = field_name;
26372654
...@@ -4688,7 +4705,22 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -4688,7 +4705,22 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
4688 IrInstruction *field_parent_ptr = ir_build_field_parent_ptr(irb, scope, node, arg0_value, arg1_value, arg2_value, nullptr);4705 IrInstruction *field_parent_ptr = ir_build_field_parent_ptr(irb, scope, node, arg0_value, arg1_value, arg2_value, nullptr);
4689 return ir_lval_wrap(irb, scope, field_parent_ptr, lval);4706 return ir_lval_wrap(irb, scope, field_parent_ptr, lval);
4690 }4707 }
4691 case BuiltinFnIdOffsetOf:4708 case BuiltinFnIdByteOffsetOf:
4709 {
4710 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4711 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
4712 if (arg0_value == irb->codegen->invalid_instruction)
4713 return arg0_value;
4714
4715 AstNode *arg1_node = node->data.fn_call_expr.params.at(1);
4716 IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope);
4717 if (arg1_value == irb->codegen->invalid_instruction)
4718 return arg1_value;
4719
4720 IrInstruction *offset_of = ir_build_byte_offset_of(irb, scope, node, arg0_value, arg1_value);
4721 return ir_lval_wrap(irb, scope, offset_of, lval);
4722 }
4723 case BuiltinFnIdBitOffsetOf:
4692 {4724 {
4693 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);4725 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
4694 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);4726 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
...@@ -4700,7 +4732,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -4700,7 +4732,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
4700 if (arg1_value == irb->codegen->invalid_instruction)4732 if (arg1_value == irb->codegen->invalid_instruction)
4701 return arg1_value;4733 return arg1_value;
47024734
4703 IrInstruction *offset_of = ir_build_offset_of(irb, scope, node, arg0_value, arg1_value);4735 IrInstruction *offset_of = ir_build_bit_offset_of(irb, scope, node, arg0_value, arg1_value);
4704 return ir_lval_wrap(irb, scope, offset_of, lval);4736 return ir_lval_wrap(irb, scope, offset_of, lval);
4705 }4737 }
4706 case BuiltinFnIdInlineCall:4738 case BuiltinFnIdInlineCall:
...@@ -17010,47 +17042,76 @@ static ZigType *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,...@@ -17010,47 +17042,76 @@ static ZigType *ir_analyze_instruction_field_parent_ptr(IrAnalyze *ira,
17010 return result_type;17042 return result_type;
17011}17043}
1701217044
17013static ZigType *ir_analyze_instruction_offset_of(IrAnalyze *ira,17045static TypeStructField *validate_byte_offset(IrAnalyze *ira,
17014 IrInstructionOffsetOf *instruction)17046 IrInstruction *type_value,
17047 IrInstruction *field_name_value,
17048 size_t *byte_offset)
17015{17049{
17016 Error err;
17017 IrInstruction *type_value = instruction->type_value->other;
17018 ZigType *container_type = ir_resolve_type(ira, type_value);17050 ZigType *container_type = ir_resolve_type(ira, type_value);
17019 if (type_is_invalid(container_type))17051 if (type_is_invalid(container_type))
17020 return ira->codegen->builtin_types.entry_invalid;17052 return nullptr;
1702117053
17054 Error err;
17022 if ((err = ensure_complete_type(ira->codegen, container_type)))17055 if ((err = ensure_complete_type(ira->codegen, container_type)))
17023 return ira->codegen->builtin_types.entry_invalid;17056 return nullptr;
1702417057
17025 IrInstruction *field_name_value = instruction->field_name->other;
17026 Buf *field_name = ir_resolve_str(ira, field_name_value);17058 Buf *field_name = ir_resolve_str(ira, field_name_value);
17027 if (!field_name)17059 if (!field_name)
17028 return ira->codegen->builtin_types.entry_invalid;17060 return nullptr;
1702917061
17030 if (container_type->id != ZigTypeIdStruct) {17062 if (container_type->id != ZigTypeIdStruct) {
17031 ir_add_error(ira, type_value,17063 ir_add_error(ira, type_value,
17032 buf_sprintf("expected struct type, found '%s'", buf_ptr(&container_type->name)));17064 buf_sprintf("expected struct type, found '%s'", buf_ptr(&container_type->name)));
17033 return ira->codegen->builtin_types.entry_invalid;17065 return nullptr;
17034 }17066 }
1703517067
17036 TypeStructField *field = find_struct_type_field(container_type, field_name);17068 TypeStructField *field = find_struct_type_field(container_type, field_name);
17037 if (field == nullptr) {17069 if (field == nullptr) {
17038 ir_add_error(ira, field_name_value,17070 ir_add_error(ira, field_name_value,
17039 buf_sprintf("struct '%s' has no field '%s'",17071 buf_sprintf("struct '%s' has no field '%s'",
17040 buf_ptr(&container_type->name), buf_ptr(field_name)));17072 buf_ptr(&container_type->name), buf_ptr(field_name)));
17041 return ira->codegen->builtin_types.entry_invalid;17073 return nullptr;
17042 }17074 }
1704317075
17044 if (!type_has_bits(field->type_entry)) {17076 if (!type_has_bits(field->type_entry)) {
17045 ir_add_error(ira, field_name_value,17077 ir_add_error(ira, field_name_value,
17046 buf_sprintf("zero-bit field '%s' in struct '%s' has no offset",17078 buf_sprintf("zero-bit field '%s' in struct '%s' has no offset",
17047 buf_ptr(field_name), buf_ptr(&container_type->name)));17079 buf_ptr(field_name), buf_ptr(&container_type->name)));
17048 return ira->codegen->builtin_types.entry_invalid;17080 return nullptr;
17049 }17081 }
17050 size_t byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);17082
17051 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);17083 *byte_offset = LLVMOffsetOfElement(ira->codegen->target_data_ref, container_type->type_ref, field->gen_index);
17052 bigint_init_unsigned(&out_val->data.x_bigint, byte_offset);17084 return field;
17053 return ira->codegen->builtin_types.entry_num_lit_int;17085}
17086
17087static ZigType *ir_analyze_instruction_byte_offset_of(IrAnalyze *ira,
17088 IrInstructionByteOffsetOf *instruction)
17089{
17090 IrInstruction *type_value = instruction->type_value->other;
17091 IrInstruction *field_name_value = instruction->field_name->other;
17092 size_t byte_offset = 0;
17093 if (validate_byte_offset(ira, type_value, field_name_value, &byte_offset)) {
17094 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
17095 bigint_init_unsigned(&out_val->data.x_bigint, byte_offset);
17096 return ira->codegen->builtin_types.entry_num_lit_int;
17097 }
17098 return ira->codegen->builtin_types.entry_invalid;
17099}
17100
17101static ZigType *ir_analyze_instruction_bit_offset_of(IrAnalyze *ira,
17102 IrInstructionBitOffsetOf *instruction)
17103{
17104 IrInstruction *type_value = instruction->type_value->other;
17105 IrInstruction *field_name_value = instruction->field_name->other;
17106 size_t byte_offset = 0;
17107 TypeStructField *field = nullptr;
17108 if ((field = validate_byte_offset(ira, type_value, field_name_value, &byte_offset))) {
17109 size_t bit_offset = byte_offset * 8 + field->packed_bits_offset;
17110 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
17111 bigint_init_unsigned(&out_val->data.x_bigint, bit_offset);
17112 return ira->codegen->builtin_types.entry_num_lit_int;
17113 }
17114 return ira->codegen->builtin_types.entry_invalid;
17054}17115}
1705517116
17056static void ensure_field_index(ZigType *type, const char *field_name, size_t index)17117static void ensure_field_index(ZigType *type, const char *field_name, size_t index)
...@@ -21550,8 +21611,10 @@ static ZigType *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *ins...@@ -21550,8 +21611,10 @@ static ZigType *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *ins
21550 return ir_analyze_instruction_enum_tag_name(ira, (IrInstructionTagName *)instruction);21611 return ir_analyze_instruction_enum_tag_name(ira, (IrInstructionTagName *)instruction);
21551 case IrInstructionIdFieldParentPtr:21612 case IrInstructionIdFieldParentPtr:
21552 return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction);21613 return ir_analyze_instruction_field_parent_ptr(ira, (IrInstructionFieldParentPtr *)instruction);
21553 case IrInstructionIdOffsetOf:21614 case IrInstructionIdByteOffsetOf:
21554 return ir_analyze_instruction_offset_of(ira, (IrInstructionOffsetOf *)instruction);21615 return ir_analyze_instruction_byte_offset_of(ira, (IrInstructionByteOffsetOf *)instruction);
21616 case IrInstructionIdBitOffsetOf:
21617 return ir_analyze_instruction_bit_offset_of(ira, (IrInstructionBitOffsetOf *)instruction);
21555 case IrInstructionIdTypeInfo:21618 case IrInstructionIdTypeInfo:
21556 return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction);21619 return ir_analyze_instruction_type_info(ira, (IrInstructionTypeInfo *) instruction);
21557 case IrInstructionIdTypeId:21620 case IrInstructionIdTypeId:
...@@ -21835,7 +21898,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -21835,7 +21898,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
21835 case IrInstructionIdTypeName:21898 case IrInstructionIdTypeName:
21836 case IrInstructionIdTagName:21899 case IrInstructionIdTagName:
21837 case IrInstructionIdFieldParentPtr:21900 case IrInstructionIdFieldParentPtr:
21838 case IrInstructionIdOffsetOf:21901 case IrInstructionIdByteOffsetOf:
21902 case IrInstructionIdBitOffsetOf:
21839 case IrInstructionIdTypeInfo:21903 case IrInstructionIdTypeInfo:
21840 case IrInstructionIdTypeId:21904 case IrInstructionIdTypeId:
21841 case IrInstructionIdAlignCast:21905 case IrInstructionIdAlignCast:
src/ir_print.cpp+15-4
...@@ -1041,8 +1041,16 @@ static void ir_print_field_parent_ptr(IrPrint *irp, IrInstructionFieldParentPtr...@@ -1041,8 +1041,16 @@ static void ir_print_field_parent_ptr(IrPrint *irp, IrInstructionFieldParentPtr
1041 fprintf(irp->f, ")");1041 fprintf(irp->f, ")");
1042}1042}
10431043
1044static void ir_print_offset_of(IrPrint *irp, IrInstructionOffsetOf *instruction) {1044static void ir_print_byte_offset_of(IrPrint *irp, IrInstructionByteOffsetOf *instruction) {
1045 fprintf(irp->f, "@offset_of(");1045 fprintf(irp->f, "@byte_offset_of(");
1046 ir_print_other_instruction(irp, instruction->type_value);
1047 fprintf(irp->f, ",");
1048 ir_print_other_instruction(irp, instruction->field_name);
1049 fprintf(irp->f, ")");
1050}
1051
1052static void ir_print_bit_offset_of(IrPrint *irp, IrInstructionBitOffsetOf *instruction) {
1053 fprintf(irp->f, "@bit_offset_of(");
1046 ir_print_other_instruction(irp, instruction->type_value);1054 ir_print_other_instruction(irp, instruction->type_value);
1047 fprintf(irp->f, ",");1055 fprintf(irp->f, ",");
1048 ir_print_other_instruction(irp, instruction->field_name);1056 ir_print_other_instruction(irp, instruction->field_name);
...@@ -1649,8 +1657,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -1649,8 +1657,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
1649 case IrInstructionIdFieldParentPtr:1657 case IrInstructionIdFieldParentPtr:
1650 ir_print_field_parent_ptr(irp, (IrInstructionFieldParentPtr *)instruction);1658 ir_print_field_parent_ptr(irp, (IrInstructionFieldParentPtr *)instruction);
1651 break;1659 break;
1652 case IrInstructionIdOffsetOf:1660 case IrInstructionIdByteOffsetOf:
1653 ir_print_offset_of(irp, (IrInstructionOffsetOf *)instruction);1661 ir_print_byte_offset_of(irp, (IrInstructionByteOffsetOf *)instruction);
1662 break;
1663 case IrInstructionIdBitOffsetOf:
1664 ir_print_bit_offset_of(irp, (IrInstructionBitOffsetOf *)instruction);
1654 break;1665 break;
1655 case IrInstructionIdTypeInfo:1666 case IrInstructionIdTypeInfo:
1656 ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);1667 ir_print_type_info(irp, (IrInstructionTypeInfo *)instruction);
test/cases/sizeof_and_typeof.zig+28-12
...@@ -7,28 +7,44 @@ test "sizeofAndTypeOf" {...@@ -7,28 +7,44 @@ test "sizeofAndTypeOf" {
7const x: u16 = 13;7const x: u16 = 13;
8const z: @typeOf(x) = 19;8const z: @typeOf(x) = 19;
99
10const A = struct {10const S = struct {
11 a: u8,11 a: u8,
12 b: u32,12 b: u32,
13 c: u8,13 c: u8,
14 d: u3,
15 e: u5,
16 f: u16,
17 g: u16,
14};18};
1519
16const P = packed struct {20const P = packed struct {
17 a: u8,21 a: u8,
18 b: u32,22 b: u32,
19 c: u8,23 c: u8,
24 d: u3,
25 e: u5,
26 f: u16,
27 g: u16,
20};28};
2129
22test "offsetOf" {30test "byteOffsetOf" {
23 // Packed structs have fixed memory layout
24 const p: P = undefined;31 const p: P = undefined;
25 assert(@offsetOf(P, "a") == 0);32 std.debug.assert(@byteOffsetOf(P, "a") == 0 and @byteOffsetOf(S, "a") == 0);
26 assert(@offsetOf(@typeOf(p), "b") == 1);33 std.debug.assert(@byteOffsetOf(P, "b") == 1 and @byteOffsetOf(S, "b") == 4);
27 assert(@offsetOf(@typeOf(p), "c") == 5);34 std.debug.assert(@byteOffsetOf(P, "c") == 5 and @byteOffsetOf(S, "c") == 8);
2835 std.debug.assert(@byteOffsetOf(P, "d") == 6 and @byteOffsetOf(S, "d") == 9);
29 // Non-packed struct fields can be moved/padded36 std.debug.assert(@byteOffsetOf(P, "e") == 6 and @byteOffsetOf(S, "e") == 10);
30 const a: A = undefined;37 std.debug.assert(@byteOffsetOf(P, "f") == 7 and @byteOffsetOf(S, "f") == 12);
31 assert(@ptrToInt(&a.a) - @ptrToInt(&a) == @offsetOf(A, "a"));38 std.debug.assert(@byteOffsetOf(P, "g") == 9 and @byteOffsetOf(S, "g") == 14);
32 assert(@ptrToInt(&a.b) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "b"));
33 assert(@ptrToInt(&a.c) - @ptrToInt(&a) == @offsetOf(@typeOf(a), "c"));
34}39}
40
41test "bitOffsetOf" {
42 const p: P = undefined;
43 std.debug.assert(@bitOffsetOf(P, "a") == 0 and @bitOffsetOf(S, "a") == 0);
44 std.debug.assert(@bitOffsetOf(P, "b") == 8 and @bitOffsetOf(S, "b") == 32);
45 std.debug.assert(@bitOffsetOf(P, "c") == 40 and @bitOffsetOf(S, "c") == 64);
46 std.debug.assert(@bitOffsetOf(P, "d") == 48 and @bitOffsetOf(S, "d") == 72);
47 std.debug.assert(@bitOffsetOf(P, "e") == 51 and @bitOffsetOf(S, "e") == 80);
48 std.debug.assert(@bitOffsetOf(P, "f") == 56 and @bitOffsetOf(S, "f") == 96);
49 std.debug.assert(@bitOffsetOf(P, "g") == 72 and @bitOffsetOf(S, "g") == 112);
50}
\ No newline at end of file
test/compile_errors.zig+18-6
...@@ -3763,22 +3763,22 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -3763,22 +3763,22 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
3763 );3763 );
37643764
3765 cases.add(3765 cases.add(
3766 "@offsetOf - non struct",3766 "@byteOffsetOf - non struct",
3767 \\const Foo = i32;3767 \\const Foo = i32;
3768 \\export fn foo() usize {3768 \\export fn foo() usize {
3769 \\ return @offsetOf(Foo, "a",);3769 \\ return @byteOffsetOf(Foo, "a",);
3770 \\}3770 \\}
3771 ,3771 ,
3772 ".tmp_source.zig:3:22: error: expected struct type, found 'i32'",3772 ".tmp_source.zig:3:22: error: expected struct type, found 'i32'",
3773 );3773 );
37743774
3775 cases.add(3775 cases.add(
3776 "@offsetOf - bad field name",3776 "@byteOffsetOf - bad field name",
3777 \\const Foo = struct {3777 \\const Foo = struct {
3778 \\ derp: i32,3778 \\ derp: i32,
3779 \\};3779 \\};
3780 \\export fn foo() usize {3780 \\export fn foo() usize {
3781 \\ return @offsetOf(Foo, "a",);3781 \\ return @byteOffsetOf(Foo, "a",);
3782 \\}3782 \\}
3783 ,3783 ,
3784 ".tmp_source.zig:5:27: error: struct 'Foo' has no field 'a'",3784 ".tmp_source.zig:5:27: error: struct 'Foo' has no field 'a'",
...@@ -5084,12 +5084,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5084,12 +5084,24 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5084 );5084 );
50855085
5086 cases.add(5086 cases.add(
5087 "taking offset of void field in struct",5087 "taking byte offset of void field in struct",
5088 \\const Empty = struct {5088 \\const Empty = struct {
5089 \\ val: void,5089 \\ val: void,
5090 \\};5090 \\};
5091 \\export fn foo() void {5091 \\export fn foo() void {
5092 \\ const fieldOffset = @offsetOf(Empty, "val",);5092 \\ const fieldOffset = @byteOffsetOf(Empty, "val",);
5093 \\}
5094 ,
5095 ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset",
5096 );
5097
5098 cases.add(
5099 "taking bit offset of void field in struct",
5100 \\const Empty = struct {
5101 \\ val: void,
5102 \\};
5103 \\export fn foo() void {
5104 \\ const fieldOffset = @bitOffsetOf(Empty, "val",);
5093 \\}5105 \\}
5094 ,5106 ,
5095 ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset",5107 ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset",