| ... | @@ -11000,8 +11000,36 @@ fn zirBitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError | ... | @@ -11000,8 +11000,36 @@ fn zirBitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 11000 | | 11000 | |
| 11001 | fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { | 11001 | fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 11002 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; | 11002 | const inst_data = sema.code.instructions.items(.data)[inst].pl_node; |
| 11003 | const src = inst_data.src(); | 11003 | sema.src = .{ .node_offset_bin_op = inst_data.src_node }; |
| 11004 | return sema.fail(block, src, "TODO: Sema.zirOffsetOf", .{}); | 11004 | const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node }; |
| | 11005 | const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node }; |
| | 11006 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| | 11007 | |
| | 11008 | const ty = try sema.resolveType(block, lhs_src, extra.lhs); |
| | 11009 | const field_name = try sema.resolveConstString(block, rhs_src, extra.rhs); |
| | 11010 | |
| | 11011 | try sema.resolveTypeLayout(block, lhs_src, ty); |
| | 11012 | if (ty.tag() != .@"struct") { |
| | 11013 | return sema.fail( |
| | 11014 | block, |
| | 11015 | lhs_src, |
| | 11016 | "expected struct type, found '{}'", |
| | 11017 | .{ty}, |
| | 11018 | ); |
| | 11019 | } |
| | 11020 | |
| | 11021 | const index = ty.structFields().getIndex(field_name) orelse { |
| | 11022 | return sema.fail( |
| | 11023 | block, |
| | 11024 | rhs_src, |
| | 11025 | "struct '{}' has no field '{s}'", |
| | 11026 | .{ ty, field_name }, |
| | 11027 | ); |
| | 11028 | }; |
| | 11029 | |
| | 11030 | const target = sema.mod.getTarget(); |
| | 11031 | const offset = ty.structFieldOffset(index, target); |
| | 11032 | return sema.addIntUnsigned(Type.comptime_int, offset); |
| 11005 | } | 11033 | } |
| 11006 | | 11034 | |
| 11007 | /// Returns `true` if the type was a comptime_int. | 11035 | /// Returns `true` if the type was a comptime_int. |