authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-22 16:07:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-22 16:07:58-07:00
log3d637e6dd257d617867e12ac949d966d2c2ef48a
tree4e9b0513c63fd21c91850f777c40154081f71dab
parent130ad08001f73d62e0c6daf3848da4f7aedff614

AstGen: fix `@export`

Make it properly use `std.builtin.ExportOptions`.

5 files changed, 70 insertions(+), 20 deletions(-)

src/AstGen.zig+12-9
......@@ -6127,15 +6127,18 @@ fn builtinCall(
61276127 .c_import => return cImport( gz, scope, rl, node, params[0]),
61286128
61296129 .@"export" => {
6130 // TODO: @export is supposed to be able to export things other than functions.
6131 // Instead of `comptimeExpr` here we need `decl_ref`.
6132 const fn_to_export = try comptimeExpr(gz, scope, .none, params[0]);
6133 // TODO: the second parameter here is supposed to be
6134 // `std.builtin.ExportOptions`, not a string.
6135 const export_name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[1]);
6136 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Bin{
6137 .lhs = fn_to_export,
6138 .rhs = export_name,
6130 const node_tags = tree.nodes.items(.tag);
6131 // This function causes a Decl to be exported. The first parameter is not an expression,
6132 // but an identifier of the Decl to be exported.
6133 if (node_tags[params[0]] != .identifier) {
6134 return astgen.failNode(params[0], "the first @export parameter must be an identifier", .{});
6135 }
6136 const ident_token = main_tokens[params[0]];
6137 const decl_name = try gz.identAsString(ident_token);
6138 const options = try comptimeExpr(gz, scope, .{ .ty = .export_options_type }, params[1]);
6139 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Export{
6140 .decl_name = decl_name,
6141 .options = options,
61396142 });
61406143 return rvalue(gz, scope, rl, .void_value, node);
61416144 },
src/Sema.zig+9-10
......@@ -1766,21 +1766,20 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
17661766 defer tracy.end();
17671767
17681768 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1769 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
1769 const extra = sema.code.extraData(Zir.Inst.Export, inst_data.payload_index).data;
17701770 const src = inst_data.src();
17711771 const lhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
17721772 const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };
1773 const decl_name = sema.code.nullTerminatedString(extra.decl_name);
1774 const decl = try sema.lookupIdentifier(block, lhs_src, decl_name);
1775 const options = try sema.resolveInstConst(block, rhs_src, extra.options);
17731776
1774 // TODO (see corresponding TODO in AstGen) this is supposed to be a `decl_ref`
1775 // instruction, which could reference any decl, which is then supposed to get
1776 // exported, regardless of whether or not it is a function.
1777 const target_fn = try sema.resolveInstConst(block, lhs_src, extra.lhs);
1778 // TODO (see corresponding TODO in AstGen) this is supposed to be
1779 // `std.builtin.ExportOptions`, not a string.
1780 const export_name = try sema.resolveConstString(block, rhs_src, extra.rhs);
1777 // TODO respect the name, linkage, and section options. Until then we export
1778 // as the decl name.
1779 _ = options;
1780 const export_name = mem.spanZ(decl.name);
17811781
1782 const actual_fn = target_fn.val.castTag(.function).?.data;
1783 try sema.mod.analyzeExport(&block.base, src, export_name, actual_fn.owner_decl);
1782 try sema.mod.analyzeExport(&block.base, src, export_name, decl);
17841783}
17851784
17861785fn zirSetAlignStack(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {
src/Zir.zig+24-1
......@@ -1352,6 +1352,7 @@ pub const Inst = struct {
13521352 float_mode_type,
13531353 reduce_op_type,
13541354 call_options_type,
1355 export_options_type,
13551356
13561357 /// `undefined` (untyped)
13571358 undef,
......@@ -1575,6 +1576,10 @@ pub const Inst = struct {
15751576 .ty = Type.initTag(.type),
15761577 .val = Value.initTag(.call_options_type),
15771578 },
1579 .export_options_type = .{
1580 .ty = Type.initTag(.type),
1581 .val = Value.initTag(.export_options_type),
1582 },
15781583
15791584 .undef = .{
15801585 .ty = Type.initTag(.@"undefined"),
......@@ -2214,6 +2219,12 @@ pub const Inst = struct {
22142219 src_node: i32,
22152220 };
22162221
2222 pub const Export = struct {
2223 /// Null-terminated string index.
2224 decl_name: u32,
2225 options: Ref,
2226 };
2227
22172228 /// Trailing: `CompileErrors.Item` for each `items_len`.
22182229 pub const CompileErrors = struct {
22192230 items_len: u32,
......@@ -2451,7 +2462,6 @@ const Writer = struct {
24512462 .xor,
24522463 .store_node,
24532464 .error_union_type,
2454 .@"export",
24552465 .merge_error_sets,
24562466 .bit_and,
24572467 .bit_or,
......@@ -2479,6 +2489,8 @@ const Writer = struct {
24792489 .bitcast_result_ptr,
24802490 => try self.writePlNodeBin(stream, inst),
24812491
2492 .@"export" => try self.writePlNodeExport(stream, inst),
2493
24822494 .call,
24832495 .call_chkused,
24842496 .call_compile_time,
......@@ -2729,6 +2741,17 @@ const Writer = struct {
27292741 try self.writeSrc(stream, inst_data.src());
27302742 }
27312743
2744 fn writePlNodeExport(self: *Writer, stream: anytype, inst: Inst.Index) !void {
2745 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
2746 const extra = self.code.extraData(Inst.Export, inst_data.payload_index).data;
2747 const decl_name = self.code.nullTerminatedString(extra.decl_name);
2748
2749 try stream.print("{}, ", .{std.zig.fmtId(decl_name)});
2750 try self.writeInstRef(stream, extra.options);
2751 try stream.writeAll(") ");
2752 try self.writeSrc(stream, inst_data.src());
2753 }
2754
27322755 fn writePlNodeErrorSetDecl(self: *Writer, stream: anytype, inst: Inst.Index) !void {
27332756 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
27342757 const extra = self.code.extraData(Inst.ErrorSetDecl, inst_data.payload_index);
src/type.zig+18
......@@ -99,6 +99,7 @@ pub const Type = extern union {
9999 .empty_struct_literal,
100100 .@"struct",
101101 .call_options,
102 .export_options,
102103 => return .Struct,
103104
104105 .enum_full,
......@@ -616,6 +617,7 @@ pub const Type = extern union {
616617 .float_mode,
617618 .reduce_op,
618619 .call_options,
620 .export_options,
619621 => unreachable,
620622
621623 .array_u8,
......@@ -794,6 +796,7 @@ pub const Type = extern union {
794796 .float_mode => return writer.writeAll("std.builtin.FloatMode"),
795797 .reduce_op => return writer.writeAll("std.builtin.ReduceOp"),
796798 .call_options => return writer.writeAll("std.builtin.CallOptions"),
799 .export_options => return writer.writeAll("std.builtin.ExportOptions"),
797800 .function => {
798801 const payload = ty.castTag(.function).?.data;
799802 try writer.writeAll("fn(");
......@@ -1008,6 +1011,7 @@ pub const Type = extern union {
10081011 .float_mode => return Value.initTag(.float_mode_type),
10091012 .reduce_op => return Value.initTag(.reduce_op_type),
10101013 .call_options => return Value.initTag(.call_options_type),
1014 .export_options => return Value.initTag(.export_options_type),
10111015 .inferred_alloc_const => unreachable,
10121016 .inferred_alloc_mut => unreachable,
10131017 else => return Value.Tag.ty.create(allocator, self),
......@@ -1065,6 +1069,7 @@ pub const Type = extern union {
10651069 .float_mode,
10661070 .reduce_op,
10671071 .call_options,
1072 .export_options,
10681073 => true,
10691074
10701075 .@"struct" => {
......@@ -1175,6 +1180,7 @@ pub const Type = extern union {
11751180 .float_mode,
11761181 .reduce_op,
11771182 .call_options,
1183 .export_options,
11781184 => return 1,
11791185
11801186 .fn_noreturn_no_args, // represents machine code; not a pointer
......@@ -1352,6 +1358,7 @@ pub const Type = extern union {
13521358 .float_mode,
13531359 .reduce_op,
13541360 .call_options,
1361 .export_options,
13551362 => return 1,
13561363
13571364 .array_u8 => self.castTag(.array_u8).?.data,
......@@ -1615,6 +1622,7 @@ pub const Type = extern union {
16151622 .float_mode,
16161623 .reduce_op,
16171624 .call_options,
1625 .export_options,
16181626 => @panic("TODO at some point we gotta resolve builtin types"),
16191627 };
16201628 }
......@@ -2238,6 +2246,7 @@ pub const Type = extern union {
22382246 .float_mode,
22392247 .reduce_op,
22402248 .call_options,
2249 .export_options,
22412250 => return null,
22422251
22432252 .@"struct" => {
......@@ -2403,6 +2412,7 @@ pub const Type = extern union {
24032412 .float_mode,
24042413 .reduce_op,
24052414 .call_options,
2415 .export_options,
24062416 => @panic("TODO resolve std.builtin types"),
24072417
24082418 else => unreachable,
......@@ -2425,6 +2435,7 @@ pub const Type = extern union {
24252435 .float_mode,
24262436 .reduce_op,
24272437 .call_options,
2438 .export_options,
24282439 => @panic("TODO resolve std.builtin types"),
24292440 else => unreachable,
24302441 }
......@@ -2446,6 +2457,7 @@ pub const Type = extern union {
24462457 .float_mode,
24472458 .reduce_op,
24482459 .call_options,
2460 .export_options,
24492461 => @panic("TODO resolve std.builtin types"),
24502462 else => unreachable,
24512463 }
......@@ -2489,6 +2501,7 @@ pub const Type = extern union {
24892501 .float_mode,
24902502 .reduce_op,
24912503 .call_options,
2504 .export_options,
24922505 => @panic("TODO resolve std.builtin types"),
24932506 else => unreachable,
24942507 }
......@@ -2518,6 +2531,7 @@ pub const Type = extern union {
25182531 .float_mode,
25192532 .reduce_op,
25202533 .call_options,
2534 .export_options,
25212535 => @panic("TODO resolve std.builtin types"),
25222536 else => unreachable,
25232537 }
......@@ -2548,6 +2562,7 @@ pub const Type = extern union {
25482562 .float_mode,
25492563 .reduce_op,
25502564 .call_options,
2565 .export_options,
25512566 => @panic("TODO resolve std.builtin types"),
25522567 else => unreachable,
25532568 }
......@@ -2587,6 +2602,7 @@ pub const Type = extern union {
25872602 .float_mode,
25882603 .reduce_op,
25892604 .call_options,
2605 .export_options,
25902606 => @panic("TODO resolve std.builtin types"),
25912607
25922608 else => unreachable,
......@@ -2643,6 +2659,7 @@ pub const Type = extern union {
26432659 float_mode,
26442660 reduce_op,
26452661 call_options,
2662 export_options,
26462663 @"null",
26472664 @"undefined",
26482665 fn_noreturn_no_args,
......@@ -2754,6 +2771,7 @@ pub const Type = extern union {
27542771 .float_mode,
27552772 .reduce_op,
27562773 .call_options,
2774 .export_options,
27572775 => @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"),
27582776
27592777 .array_u8,
src/value.zig+7
......@@ -72,6 +72,7 @@ pub const Value = extern union {
7272 float_mode_type,
7373 reduce_op_type,
7474 call_options_type,
75 export_options_type,
7576
7677 undef,
7778 zero,
......@@ -185,6 +186,7 @@ pub const Value = extern union {
185186 .float_mode_type,
186187 .reduce_op_type,
187188 .call_options_type,
189 .export_options_type,
188190 => @compileError("Value Tag " ++ @tagName(t) ++ " has no payload"),
189191
190192 .int_big_positive,
......@@ -351,6 +353,7 @@ pub const Value = extern union {
351353 .float_mode_type,
352354 .reduce_op_type,
353355 .call_options_type,
356 .export_options_type,
354357 => unreachable,
355358
356359 .ty => {
......@@ -506,6 +509,7 @@ pub const Value = extern union {
506509 .float_mode_type => return out_stream.writeAll("std.builtin.FloatMode"),
507510 .reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),
508511 .call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),
512 .export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),
509513 .abi_align_default => return out_stream.writeAll("(default ABI alignment)"),
510514
511515 .empty_struct_value => return out_stream.writeAll("struct {}{}"),
......@@ -635,6 +639,7 @@ pub const Value = extern union {
635639 .float_mode_type => Type.initTag(.float_mode),
636640 .reduce_op_type => Type.initTag(.reduce_op),
637641 .call_options_type => Type.initTag(.call_options),
642 .export_options_type => Type.initTag(.export_options),
638643
639644 .int_type => {
640645 const payload = self.castTag(.int_type).?.data;
......@@ -1181,6 +1186,7 @@ pub const Value = extern union {
11811186 .float_mode_type,
11821187 .reduce_op_type,
11831188 .call_options_type,
1189 .export_options_type,
11841190 => @panic("TODO this hash function looks pretty broken. audit it"),
11851191 }
11861192 return hasher.final();
......@@ -1336,6 +1342,7 @@ pub const Value = extern union {
13361342 .float_mode_type,
13371343 .reduce_op_type,
13381344 .call_options_type,
1345 .export_options_type,
13391346 => true,
13401347
13411348 .zero,