authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-22 16:31:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-22 16:31:51-07:00
log7c453b91b85bab6800d24feb57c4f35b8ce48d57
treecf091f9555b03f3a28eba96b1774fb6e62d78e99
parent3d637e6dd257d617867e12ac949d966d2c2ef48a

AstGen: implement `@extern` builtin


6 files changed, 66 insertions(+), 0 deletions(-)

src/AstGen.zig+13
......@@ -6135,6 +6135,9 @@ fn builtinCall(
61356135 }
61366136 const ident_token = main_tokens[params[0]];
61376137 const decl_name = try gz.identAsString(ident_token);
6138 // TODO look for local variables in scope matching `decl_name` and emit a compile
6139 // error. Only top-level declarations can be exported. Until this is done, the
6140 // compile error will end up being "use of undeclared identifier" in Sema.
61386141 const options = try comptimeExpr(gz, scope, .{ .ty = .export_options_type }, params[1]);
61396142 _ = try gz.addPlNode(.@"export", node, Zir.Inst.Export{
61406143 .decl_name = decl_name,
......@@ -6142,6 +6145,16 @@ fn builtinCall(
61426145 });
61436146 return rvalue(gz, scope, rl, .void_value, node);
61446147 },
6148 .@"extern" => {
6149 const type_inst = try typeExpr(gz, scope, params[0]);
6150 const options = try comptimeExpr(gz, scope, .{ .ty = .extern_options_type }, params[1]);
6151 const result = try gz.addExtendedPayload(.builtin_extern, Zir.Inst.BinNode{
6152 .node = gz.nodeIndexToRelative(node),
6153 .lhs = type_inst,
6154 .rhs = options,
6155 });
6156 return rvalue(gz, scope, rl, result, node);
6157 },
61456158
61466159 .breakpoint => return simpleNoOpVoid(gz, scope, rl, node, .breakpoint),
61476160 .fence => return simpleNoOpVoid(gz, scope, rl, node, .fence),
src/BuiltinFn.zig+8
......@@ -39,6 +39,7 @@ pub const Tag = enum {
3939 error_to_int,
4040 err_set_cast,
4141 @"export",
42 @"extern",
4243 fence,
4344 field,
4445 field_parent_ptr,
......@@ -387,6 +388,13 @@ pub const list = list: {
387388 .param_count = 2,
388389 },
389390 },
391 .{
392 "@extern",
393 .{
394 .tag = .@"extern",
395 .param_count = 2,
396 },
397 },
390398 .{
391399 "@fence",
392400 .{
src/Sema.zig+11
......@@ -517,6 +517,7 @@ fn zirExtended(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerErro
517517 .frame => return sema.zirFrame( block, extended),
518518 .frame_address => return sema.zirFrameAddress( block, extended),
519519 .alloc => return sema.zirAllocExtended( block, extended),
520 .builtin_extern => return sema.zirBuiltinExtern( block, extended),
520521 .c_undef => return sema.zirCUndef( block, extended),
521522 .c_include => return sema.zirCInclude( block, extended),
522523 .c_define => return sema.zirCDefine( block, extended),
......@@ -5488,6 +5489,16 @@ fn zirWasmMemoryGrow(
54885489 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirWasmMemoryGrow", .{});
54895490}
54905491
5492fn zirBuiltinExtern(
5493 sema: *Sema,
5494 block: *Scope.Block,
5495 extended: Zir.Inst.Extended.InstData,
5496) InnerError!*Inst {
5497 const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data;
5498 const src: LazySrcLoc = .{ .node_offset = extra.node };
5499 return sema.mod.fail(&block.base, src, "TODO: implement Sema.zirBuiltinExtern", .{});
5500}
5501
54915502fn requireFunctionBlock(sema: *Sema, block: *Scope.Block, src: LazySrcLoc) !void {
54925503 if (sema.func == null) {
54935504 return sema.mod.fail(&block.base, src, "instruction illegal outside function body", .{});
src/Zir.zig+9
......@@ -1258,6 +1258,9 @@ pub const Inst = struct {
12581258 /// * 0b0X00 - 1=const, 0=var
12591259 /// * 0bX000 - is comptime
12601260 alloc,
1261 /// The `@extern` builtin.
1262 /// `operand` is payload index to `BinNode`.
1263 builtin_extern,
12611264 /// `operand` is payload index to `UnNode`.
12621265 c_undef,
12631266 /// `operand` is payload index to `UnNode`.
......@@ -1353,6 +1356,7 @@ pub const Inst = struct {
13531356 reduce_op_type,
13541357 call_options_type,
13551358 export_options_type,
1359 extern_options_type,
13561360
13571361 /// `undefined` (untyped)
13581362 undef,
......@@ -1580,6 +1584,10 @@ pub const Inst = struct {
15801584 .ty = Type.initTag(.type),
15811585 .val = Value.initTag(.export_options_type),
15821586 },
1587 .extern_options_type = .{
1588 .ty = Type.initTag(.type),
1589 .val = Value.initTag(.extern_options_type),
1590 },
15831591
15841592 .undef = .{
15851593 .ty = Type.initTag(.@"undefined"),
......@@ -2598,6 +2606,7 @@ const Writer = struct {
25982606
25992607 .func,
26002608 .alloc,
2609 .builtin_extern,
26012610 .c_undef,
26022611 .c_include,
26032612 .c_define,
src/type.zig+18
......@@ -100,6 +100,7 @@ pub const Type = extern union {
100100 .@"struct",
101101 .call_options,
102102 .export_options,
103 .extern_options,
103104 => return .Struct,
104105
105106 .enum_full,
......@@ -618,6 +619,7 @@ pub const Type = extern union {
618619 .reduce_op,
619620 .call_options,
620621 .export_options,
622 .extern_options,
621623 => unreachable,
622624
623625 .array_u8,
......@@ -797,6 +799,7 @@ pub const Type = extern union {
797799 .reduce_op => return writer.writeAll("std.builtin.ReduceOp"),
798800 .call_options => return writer.writeAll("std.builtin.CallOptions"),
799801 .export_options => return writer.writeAll("std.builtin.ExportOptions"),
802 .extern_options => return writer.writeAll("std.builtin.ExternOptions"),
800803 .function => {
801804 const payload = ty.castTag(.function).?.data;
802805 try writer.writeAll("fn(");
......@@ -1012,6 +1015,7 @@ pub const Type = extern union {
10121015 .reduce_op => return Value.initTag(.reduce_op_type),
10131016 .call_options => return Value.initTag(.call_options_type),
10141017 .export_options => return Value.initTag(.export_options_type),
1018 .extern_options => return Value.initTag(.extern_options_type),
10151019 .inferred_alloc_const => unreachable,
10161020 .inferred_alloc_mut => unreachable,
10171021 else => return Value.Tag.ty.create(allocator, self),
......@@ -1070,6 +1074,7 @@ pub const Type = extern union {
10701074 .reduce_op,
10711075 .call_options,
10721076 .export_options,
1077 .extern_options,
10731078 => true,
10741079
10751080 .@"struct" => {
......@@ -1181,6 +1186,7 @@ pub const Type = extern union {
11811186 .reduce_op,
11821187 .call_options,
11831188 .export_options,
1189 .extern_options,
11841190 => return 1,
11851191
11861192 .fn_noreturn_no_args, // represents machine code; not a pointer
......@@ -1359,6 +1365,7 @@ pub const Type = extern union {
13591365 .reduce_op,
13601366 .call_options,
13611367 .export_options,
1368 .extern_options,
13621369 => return 1,
13631370
13641371 .array_u8 => self.castTag(.array_u8).?.data,
......@@ -1623,6 +1630,7 @@ pub const Type = extern union {
16231630 .reduce_op,
16241631 .call_options,
16251632 .export_options,
1633 .extern_options,
16261634 => @panic("TODO at some point we gotta resolve builtin types"),
16271635 };
16281636 }
......@@ -2247,6 +2255,7 @@ pub const Type = extern union {
22472255 .reduce_op,
22482256 .call_options,
22492257 .export_options,
2258 .extern_options,
22502259 => return null,
22512260
22522261 .@"struct" => {
......@@ -2413,6 +2422,7 @@ pub const Type = extern union {
24132422 .reduce_op,
24142423 .call_options,
24152424 .export_options,
2425 .extern_options,
24162426 => @panic("TODO resolve std.builtin types"),
24172427
24182428 else => unreachable,
......@@ -2436,6 +2446,7 @@ pub const Type = extern union {
24362446 .reduce_op,
24372447 .call_options,
24382448 .export_options,
2449 .extern_options,
24392450 => @panic("TODO resolve std.builtin types"),
24402451 else => unreachable,
24412452 }
......@@ -2458,6 +2469,7 @@ pub const Type = extern union {
24582469 .reduce_op,
24592470 .call_options,
24602471 .export_options,
2472 .extern_options,
24612473 => @panic("TODO resolve std.builtin types"),
24622474 else => unreachable,
24632475 }
......@@ -2502,6 +2514,7 @@ pub const Type = extern union {
25022514 .reduce_op,
25032515 .call_options,
25042516 .export_options,
2517 .extern_options,
25052518 => @panic("TODO resolve std.builtin types"),
25062519 else => unreachable,
25072520 }
......@@ -2532,6 +2545,7 @@ pub const Type = extern union {
25322545 .reduce_op,
25332546 .call_options,
25342547 .export_options,
2548 .extern_options,
25352549 => @panic("TODO resolve std.builtin types"),
25362550 else => unreachable,
25372551 }
......@@ -2563,6 +2577,7 @@ pub const Type = extern union {
25632577 .reduce_op,
25642578 .call_options,
25652579 .export_options,
2580 .extern_options,
25662581 => @panic("TODO resolve std.builtin types"),
25672582 else => unreachable,
25682583 }
......@@ -2603,6 +2618,7 @@ pub const Type = extern union {
26032618 .reduce_op,
26042619 .call_options,
26052620 .export_options,
2621 .extern_options,
26062622 => @panic("TODO resolve std.builtin types"),
26072623
26082624 else => unreachable,
......@@ -2660,6 +2676,7 @@ pub const Type = extern union {
26602676 reduce_op,
26612677 call_options,
26622678 export_options,
2679 extern_options,
26632680 @"null",
26642681 @"undefined",
26652682 fn_noreturn_no_args,
......@@ -2772,6 +2789,7 @@ pub const Type = extern union {
27722789 .reduce_op,
27732790 .call_options,
27742791 .export_options,
2792 .extern_options,
27752793 => @compileError("Type Tag " ++ @tagName(t) ++ " has no payload"),
27762794
27772795 .array_u8,
src/value.zig+7
......@@ -73,6 +73,7 @@ pub const Value = extern union {
7373 reduce_op_type,
7474 call_options_type,
7575 export_options_type,
76 extern_options_type,
7677
7778 undef,
7879 zero,
......@@ -187,6 +188,7 @@ pub const Value = extern union {
187188 .reduce_op_type,
188189 .call_options_type,
189190 .export_options_type,
191 .extern_options_type,
190192 => @compileError("Value Tag " ++ @tagName(t) ++ " has no payload"),
191193
192194 .int_big_positive,
......@@ -354,6 +356,7 @@ pub const Value = extern union {
354356 .reduce_op_type,
355357 .call_options_type,
356358 .export_options_type,
359 .extern_options_type,
357360 => unreachable,
358361
359362 .ty => {
......@@ -510,6 +513,7 @@ pub const Value = extern union {
510513 .reduce_op_type => return out_stream.writeAll("std.builtin.ReduceOp"),
511514 .call_options_type => return out_stream.writeAll("std.builtin.CallOptions"),
512515 .export_options_type => return out_stream.writeAll("std.builtin.ExportOptions"),
516 .extern_options_type => return out_stream.writeAll("std.builtin.ExternOptions"),
513517 .abi_align_default => return out_stream.writeAll("(default ABI alignment)"),
514518
515519 .empty_struct_value => return out_stream.writeAll("struct {}{}"),
......@@ -640,6 +644,7 @@ pub const Value = extern union {
640644 .reduce_op_type => Type.initTag(.reduce_op),
641645 .call_options_type => Type.initTag(.call_options),
642646 .export_options_type => Type.initTag(.export_options),
647 .extern_options_type => Type.initTag(.extern_options),
643648
644649 .int_type => {
645650 const payload = self.castTag(.int_type).?.data;
......@@ -1187,6 +1192,7 @@ pub const Value = extern union {
11871192 .reduce_op_type,
11881193 .call_options_type,
11891194 .export_options_type,
1195 .extern_options_type,
11901196 => @panic("TODO this hash function looks pretty broken. audit it"),
11911197 }
11921198 return hasher.final();
......@@ -1343,6 +1349,7 @@ pub const Value = extern union {
13431349 .reduce_op_type,
13441350 .call_options_type,
13451351 .export_options_type,
1352 .extern_options_type,
13461353 => true,
13471354
13481355 .zero,