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(...@@ -6127,15 +6127,18 @@ fn builtinCall(
6127 .c_import => return cImport( gz, scope, rl, node, params[0]),6127 .c_import => return cImport( gz, scope, rl, node, params[0]),
61286128
6129 .@"export" => {6129 .@"export" => {
6130 // TODO: @export is supposed to be able to export things other than functions.6130 const node_tags = tree.nodes.items(.tag);
6131 // Instead of `comptimeExpr` here we need `decl_ref`.6131 // This function causes a Decl to be exported. The first parameter is not an expression,
6132 const fn_to_export = try comptimeExpr(gz, scope, .none, params[0]);6132 // but an identifier of the Decl to be exported.
6133 // TODO: the second parameter here is supposed to be6133 if (node_tags[params[0]] != .identifier) {
6134 // `std.builtin.ExportOptions`, not a string.6134 return astgen.failNode(params[0], "the first @export parameter must be an identifier", .{});
6135 const export_name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[1]);6135 }
6136 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Bin{6136 const ident_token = main_tokens[params[0]];
6137 .lhs = fn_to_export,6137 const decl_name = try gz.identAsString(ident_token);
6138 .rhs = export_name,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,
6139 });6142 });
6140 return rvalue(gz, scope, rl, .void_value, node);6143 return rvalue(gz, scope, rl, .void_value, node);
6141 },6144 },
src/Sema.zig+9-10
...@@ -1766,21 +1766,20 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!...@@ -1766,21 +1766,20 @@ fn zirExport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
1766 defer tracy.end();1766 defer tracy.end();
17671767
1768 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;1768 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;
1770 const src = inst_data.src();1770 const src = inst_data.src();
1771 const lhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };1771 const lhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
1772 const rhs_src: LazySrcLoc = .{ .node_offset_builtin_call_arg1 = inst_data.src_node };1772 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`1777 // TODO respect the name, linkage, and section options. Until then we export
1775 // instruction, which could reference any decl, which is then supposed to get1778 // as the decl name.
1776 // exported, regardless of whether or not it is a function.1779 _ = options;
1777 const target_fn = try sema.resolveInstConst(block, lhs_src, extra.lhs);1780 const export_name = mem.spanZ(decl.name);
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);
17811781
1782 const actual_fn = target_fn.val.castTag(.function).?.data;1782 try sema.mod.analyzeExport(&block.base, src, export_name, decl);
1783 try sema.mod.analyzeExport(&block.base, src, export_name, actual_fn.owner_decl);
1784}1783}
17851784
1786fn zirSetAlignStack(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!void {1785fn 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 {...@@ -1352,6 +1352,7 @@ pub const Inst = struct {
1352 float_mode_type,1352 float_mode_type,
1353 reduce_op_type,1353 reduce_op_type,
1354 call_options_type,1354 call_options_type,
1355 export_options_type,
13551356
1356 /// `undefined` (untyped)1357 /// `undefined` (untyped)
1357 undef,1358 undef,
...@@ -1575,6 +1576,10 @@ pub const Inst = struct {...@@ -1575,6 +1576,10 @@ pub const Inst = struct {
1575 .ty = Type.initTag(.type),1576 .ty = Type.initTag(.type),
1576 .val = Value.initTag(.call_options_type),1577 .val = Value.initTag(.call_options_type),
1577 },1578 },
1579 .export_options_type = .{
1580 .ty = Type.initTag(.type),
1581 .val = Value.initTag(.export_options_type),
1582 },
15781583
1579 .undef = .{1584 .undef = .{
1580 .ty = Type.initTag(.@"undefined"),1585 .ty = Type.initTag(.@"undefined"),
...@@ -2214,6 +2219,12 @@ pub const Inst = struct {...@@ -2214,6 +2219,12 @@ pub const Inst = struct {
2214 src_node: i32,2219 src_node: i32,
2215 };2220 };
22162221
2222 pub const Export = struct {
2223 /// Null-terminated string index.
2224 decl_name: u32,
2225 options: Ref,
2226 };
2227
2217 /// Trailing: `CompileErrors.Item` for each `items_len`.2228 /// Trailing: `CompileErrors.Item` for each `items_len`.
2218 pub const CompileErrors = struct {2229 pub const CompileErrors = struct {
2219 items_len: u32,2230 items_len: u32,
...@@ -2451,7 +2462,6 @@ const Writer = struct {...@@ -2451,7 +2462,6 @@ const Writer = struct {
2451 .xor,2462 .xor,
2452 .store_node,2463 .store_node,
2453 .error_union_type,2464 .error_union_type,
2454 .@"export",
2455 .merge_error_sets,2465 .merge_error_sets,
2456 .bit_and,2466 .bit_and,
2457 .bit_or,2467 .bit_or,
...@@ -2479,6 +2489,8 @@ const Writer = struct {...@@ -2479,6 +2489,8 @@ const Writer = struct {
2479 .bitcast_result_ptr,2489 .bitcast_result_ptr,
2480 => try self.writePlNodeBin(stream, inst),2490 => try self.writePlNodeBin(stream, inst),
24812491
2492 .@"export" => try self.writePlNodeExport(stream, inst),
2493
2482 .call,2494 .call,
2483 .call_chkused,2495 .call_chkused,
2484 .call_compile_time,2496 .call_compile_time,
...@@ -2729,6 +2741,17 @@ const Writer = struct {...@@ -2729,6 +2741,17 @@ const Writer = struct {
2729 try self.writeSrc(stream, inst_data.src());2741 try self.writeSrc(stream, inst_data.src());
2730 }2742 }
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
2732 fn writePlNodeErrorSetDecl(self: *Writer, stream: anytype, inst: Inst.Index) !void {2755 fn writePlNodeErrorSetDecl(self: *Writer, stream: anytype, inst: Inst.Index) !void {
2733 const inst_data = self.code.instructions.items(.data)[inst].pl_node;2756 const inst_data = self.code.instructions.items(.data)[inst].pl_node;
2734 const extra = self.code.extraData(Inst.ErrorSetDecl, inst_data.payload_index);2757 const extra = self.code.extraData(Inst.ErrorSetDecl, inst_data.payload_index);
src/type.zig+18
...@@ -99,6 +99,7 @@ pub const Type = extern union {...@@ -99,6 +99,7 @@ pub const Type = extern union {
99 .empty_struct_literal,99 .empty_struct_literal,
100 .@"struct",100 .@"struct",
101 .call_options,101 .call_options,
102 .export_options,
102 => return .Struct,103 => return .Struct,
103104
104 .enum_full,105 .enum_full,
...@@ -616,6 +617,7 @@ pub const Type = extern union {...@@ -616,6 +617,7 @@ pub const Type = extern union {
616 .float_mode,617 .float_mode,
617 .reduce_op,618 .reduce_op,
618 .call_options,619 .call_options,
620 .export_options,
619 => unreachable,621 => unreachable,
620622
621 .array_u8,623 .array_u8,
...@@ -794,6 +796,7 @@ pub const Type = extern union {...@@ -794,6 +796,7 @@ pub const Type = extern union {
794 .float_mode => return writer.writeAll("std.builtin.FloatMode"),796 .float_mode => return writer.writeAll("std.builtin.FloatMode"),
795 .reduce_op => return writer.writeAll("std.builtin.ReduceOp"),797 .reduce_op => return writer.writeAll("std.builtin.ReduceOp"),
796 .call_options => return writer.writeAll("std.builtin.CallOptions"),798 .call_options => return writer.writeAll("std.builtin.CallOptions"),
799 .export_options => return writer.writeAll("std.builtin.ExportOptions"),
797 .function => {800 .function => {
798 const payload = ty.castTag(.function).?.data;801 const payload = ty.castTag(.function).?.data;
799 try writer.writeAll("fn(");802 try writer.writeAll("fn(");
...@@ -1008,6 +1011,7 @@ pub const Type = extern union {...@@ -1008,6 +1011,7 @@ pub const Type = extern union {
1008 .float_mode => return Value.initTag(.float_mode_type),1011 .float_mode => return Value.initTag(.float_mode_type),
1009 .reduce_op => return Value.initTag(.reduce_op_type),1012 .reduce_op => return Value.initTag(.reduce_op_type),
1010 .call_options => return Value.initTag(.call_options_type),1013 .call_options => return Value.initTag(.call_options_type),
1014 .export_options => return Value.initTag(.export_options_type),
1011 .inferred_alloc_const => unreachable,1015 .inferred_alloc_const => unreachable,
1012 .inferred_alloc_mut => unreachable,1016 .inferred_alloc_mut => unreachable,
1013 else => return Value.Tag.ty.create(allocator, self),1017 else => return Value.Tag.ty.create(allocator, self),
...@@ -1065,6 +1069,7 @@ pub const Type = extern union {...@@ -1065,6 +1069,7 @@ pub const Type = extern union {
1065 .float_mode,1069 .float_mode,
1066 .reduce_op,1070 .reduce_op,
1067 .call_options,1071 .call_options,
1072 .export_options,
1068 => true,1073 => true,
10691074
1070 .@"struct" => {1075 .@"struct" => {
...@@ -1175,6 +1180,7 @@ pub const Type = extern union {...@@ -1175,6 +1180,7 @@ pub const Type = extern union {
1175 .float_mode,1180 .float_mode,
1176 .reduce_op,1181 .reduce_op,
1177 .call_options,1182 .call_options,
1183 .export_options,
1178 => return 1,1184 => return 1,
11791185
1180 .fn_noreturn_no_args, // represents machine code; not a pointer1186 .fn_noreturn_no_args, // represents machine code; not a pointer
...@@ -1352,6 +1358,7 @@ pub const Type = extern union {...@@ -1352,6 +1358,7 @@ pub const Type = extern union {
1352 .float_mode,1358 .float_mode,
1353 .reduce_op,1359 .reduce_op,
1354 .call_options,1360 .call_options,
1361 .export_options,
1355 => return 1,1362 => return 1,
13561363
1357 .array_u8 => self.castTag(.array_u8).?.data,1364 .array_u8 => self.castTag(.array_u8).?.data,
...@@ -1615,6 +1622,7 @@ pub const Type = extern union {...@@ -1615,6 +1622,7 @@ pub const Type = extern union {
1615 .float_mode,1622 .float_mode,
1616 .reduce_op,1623 .reduce_op,
1617 .call_options,1624 .call_options,
1625 .export_options,
1618 => @panic("TODO at some point we gotta resolve builtin types"),1626 => @panic("TODO at some point we gotta resolve builtin types"),
1619 };1627 };
1620 }1628 }
...@@ -2238,6 +2246,7 @@ pub const Type = extern union {...@@ -2238,6 +2246,7 @@ pub const Type = extern union {
2238 .float_mode,2246 .float_mode,
2239 .reduce_op,2247 .reduce_op,
2240 .call_options,2248 .call_options,
2249 .export_options,
2241 => return null,2250 => return null,
22422251
2243 .@"struct" => {2252 .@"struct" => {
...@@ -2403,6 +2412,7 @@ pub const Type = extern union {...@@ -2403,6 +2412,7 @@ pub const Type = extern union {
2403 .float_mode,2412 .float_mode,
2404 .reduce_op,2413 .reduce_op,
2405 .call_options,2414 .call_options,
2415 .export_options,
2406 => @panic("TODO resolve std.builtin types"),2416 => @panic("TODO resolve std.builtin types"),
24072417
2408 else => unreachable,2418 else => unreachable,
...@@ -2425,6 +2435,7 @@ pub const Type = extern union {...@@ -2425,6 +2435,7 @@ pub const Type = extern union {
2425 .float_mode,2435 .float_mode,
2426 .reduce_op,2436 .reduce_op,
2427 .call_options,2437 .call_options,
2438 .export_options,
2428 => @panic("TODO resolve std.builtin types"),2439 => @panic("TODO resolve std.builtin types"),
2429 else => unreachable,2440 else => unreachable,
2430 }2441 }
...@@ -2446,6 +2457,7 @@ pub const Type = extern union {...@@ -2446,6 +2457,7 @@ pub const Type = extern union {
2446 .float_mode,2457 .float_mode,
2447 .reduce_op,2458 .reduce_op,
2448 .call_options,2459 .call_options,
2460 .export_options,
2449 => @panic("TODO resolve std.builtin types"),2461 => @panic("TODO resolve std.builtin types"),
2450 else => unreachable,2462 else => unreachable,
2451 }2463 }
...@@ -2489,6 +2501,7 @@ pub const Type = extern union {...@@ -2489,6 +2501,7 @@ pub const Type = extern union {
2489 .float_mode,2501 .float_mode,
2490 .reduce_op,2502 .reduce_op,
2491 .call_options,2503 .call_options,
2504 .export_options,
2492 => @panic("TODO resolve std.builtin types"),2505 => @panic("TODO resolve std.builtin types"),
2493 else => unreachable,2506 else => unreachable,
2494 }2507 }
...@@ -2518,6 +2531,7 @@ pub const Type = extern union {...@@ -2518,6 +2531,7 @@ pub const Type = extern union {
2518 .float_mode,2531 .float_mode,
2519 .reduce_op,2532 .reduce_op,
2520 .call_options,2533 .call_options,
2534 .export_options,
2521 => @panic("TODO resolve std.builtin types"),2535 => @panic("TODO resolve std.builtin types"),
2522 else => unreachable,2536 else => unreachable,
2523 }2537 }
...@@ -2548,6 +2562,7 @@ pub const Type = extern union {...@@ -2548,6 +2562,7 @@ pub const Type = extern union {
2548 .float_mode,2562 .float_mode,
2549 .reduce_op,2563 .reduce_op,
2550 .call_options,2564 .call_options,
2565 .export_options,
2551 => @panic("TODO resolve std.builtin types"),2566 => @panic("TODO resolve std.builtin types"),
2552 else => unreachable,2567 else => unreachable,
2553 }2568 }
...@@ -2587,6 +2602,7 @@ pub const Type = extern union {...@@ -2587,6 +2602,7 @@ pub const Type = extern union {
2587 .float_mode,2602 .float_mode,
2588 .reduce_op,2603 .reduce_op,
2589 .call_options,2604 .call_options,
2605 .export_options,
2590 => @panic("TODO resolve std.builtin types"),2606 => @panic("TODO resolve std.builtin types"),
25912607
2592 else => unreachable,2608 else => unreachable,
...@@ -2643,6 +2659,7 @@ pub const Type = extern union {...@@ -2643,6 +2659,7 @@ pub const Type = extern union {
2643 float_mode,2659 float_mode,
2644 reduce_op,2660 reduce_op,
2645 call_options,2661 call_options,
2662 export_options,
2646 @"null",2663 @"null",
2647 @"undefined",2664 @"undefined",
2648 fn_noreturn_no_args,2665 fn_noreturn_no_args,
...@@ -2754,6 +2771,7 @@ pub const Type = extern union {...@@ -2754,6 +2771,7 @@ pub const Type = extern union {
2754 .float_mode,2771 .float_mode,
2755 .reduce_op,2772 .reduce_op,
2756 .call_options,2773 .call_options,
2774 .export_options,
2757 => @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"),2775 => @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"),
27582776
2759 .array_u8,2777 .array_u8,
src/value.zig+7
...@@ -72,6 +72,7 @@ pub const Value = extern union {...@@ -72,6 +72,7 @@ pub const Value = extern union {
72 float_mode_type,72 float_mode_type,
73 reduce_op_type,73 reduce_op_type,
74 call_options_type,74 call_options_type,
75 export_options_type,
7576
76 undef,77 undef,
77 zero,78 zero,
...@@ -185,6 +186,7 @@ pub const Value = extern union {...@@ -185,6 +186,7 @@ pub const Value = extern union {
185 .float_mode_type,186 .float_mode_type,
186 .reduce_op_type,187 .reduce_op_type,
187 .call_options_type,188 .call_options_type,
189 .export_options_type,
188 => @compileError("Value Tag " ++ @tagName(t) ++ " has no payload"),190 => @compileError("Value Tag " ++ @tagName(t) ++ " has no payload"),
189191
190 .int_big_positive,192 .int_big_positive,
...@@ -351,6 +353,7 @@ pub const Value = extern union {...@@ -351,6 +353,7 @@ pub const Value = extern union {
351 .float_mode_type,353 .float_mode_type,
352 .reduce_op_type,354 .reduce_op_type,
353 .call_options_type,355 .call_options_type,
356 .export_options_type,
354 => unreachable,357 => unreachable,
355358
356 .ty => {359 .ty => {
...@@ -506,6 +509,7 @@ pub const Value = extern union {...@@ -506,6 +509,7 @@ pub const Value = extern union {
506 .float_mode_type => return out_stream.writeAll("std.builtin.FloatMode"),509 .float_mode_type => return out_stream.writeAll("std.builtin.FloatMode"),
507 .reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),510 .reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),
508 .call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),511 .call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),
512 .export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),
509 .abi_align_default => return out_stream.writeAll("(default ABI alignment)"),513 .abi_align_default => return out_stream.writeAll("(default ABI alignment)"),
510514
511 .empty_struct_value => return out_stream.writeAll("struct {}{}"),515 .empty_struct_value => return out_stream.writeAll("struct {}{}"),
...@@ -635,6 +639,7 @@ pub const Value = extern union {...@@ -635,6 +639,7 @@ pub const Value = extern union {
635 .float_mode_type => Type.initTag(.float_mode),639 .float_mode_type => Type.initTag(.float_mode),
636 .reduce_op_type => Type.initTag(.reduce_op),640 .reduce_op_type => Type.initTag(.reduce_op),
637 .call_options_type => Type.initTag(.call_options),641 .call_options_type => Type.initTag(.call_options),
642 .export_options_type => Type.initTag(.export_options),
638643
639 .int_type => {644 .int_type => {
640 const payload = self.castTag(.int_type).?.data;645 const payload = self.castTag(.int_type).?.data;
...@@ -1181,6 +1186,7 @@ pub const Value = extern union {...@@ -1181,6 +1186,7 @@ pub const Value = extern union {
1181 .float_mode_type,1186 .float_mode_type,
1182 .reduce_op_type,1187 .reduce_op_type,
1183 .call_options_type,1188 .call_options_type,
1189 .export_options_type,
1184 => @panic("TODO this hash function looks pretty broken. audit it"),1190 => @panic("TODO this hash function looks pretty broken. audit it"),
1185 }1191 }
1186 return hasher.final();1192 return hasher.final();
...@@ -1336,6 +1342,7 @@ pub const Value = extern union {...@@ -1336,6 +1342,7 @@ pub const Value = extern union {
1336 .float_mode_type,1342 .float_mode_type,
1337 .reduce_op_type,1343 .reduce_op_type,
1338 .call_options_type,1344 .call_options_type,
1345 .export_options_type,
1339 => true,1346 => true,
13401347
1341 .zero,1348 .zero,