authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-04-08 14:22:56+02:00
committergravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-04-08 14:22:56+02:00
log91e416bbf049b875d090ba050081d9964ed4b452
treecfb780ee22b3b5da33c574489dca348bb632ae5f
parent272fe0cbfe4d59a307389e20b3bf57099b182ebe

stage2: add a simplified export builtin call

std.builtin.ExportOptions is not yet supported, thus the second argument of export is now a simple string that specifies the exported symbol name.

3 files changed, 48 insertions(+), 1 deletions(-)

src/AstGen.zig+13-1
...@@ -1336,6 +1336,7 @@ fn blockExprStmts(...@@ -1336,6 +1336,7 @@ fn blockExprStmts(
1336 .dbg_stmt_node,1336 .dbg_stmt_node,
1337 .ensure_result_used,1337 .ensure_result_used,
1338 .ensure_result_non_error,1338 .ensure_result_non_error,
1339 .@"export",
1339 .set_eval_branch_quota,1340 .set_eval_branch_quota,
1340 .compile_log,1341 .compile_log,
1341 .ensure_err_payload_void,1342 .ensure_err_payload_void,
...@@ -4146,6 +4147,18 @@ fn builtinCall(...@@ -4146,6 +4147,18 @@ fn builtinCall(
4146 return rvalue(gz, scope, rl, result, node);4147 return rvalue(gz, scope, rl, result, node);
4147 },4148 },
41484149
4150 .@"export" => {
4151 const target_fn = try expr(gz, scope, .none, params[0]);
4152 // FIXME: When structs work in stage2, actually implement this correctly!
4153 // Currently the name is always signifies Strong linkage.
4154 const export_name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[1]);
4155 _ = try gz.addPlNode(.@"export", node, zir.Inst.Bin{
4156 .lhs = target_fn,
4157 .rhs = export_name,
4158 });
4159 return rvalue(gz, scope, rl, .void_value, node);
4160 },
4161
4149 .add_with_overflow,4162 .add_with_overflow,
4150 .align_cast,4163 .align_cast,
4151 .align_of,4164 .align_of,
...@@ -4175,7 +4188,6 @@ fn builtinCall(...@@ -4175,7 +4188,6 @@ fn builtinCall(
4175 .error_name,4188 .error_name,
4176 .error_return_trace,4189 .error_return_trace,
4177 .err_set_cast,4190 .err_set_cast,
4178 .@"export",
4179 .fence,4191 .fence,
4180 .field_parent_ptr,4192 .field_parent_ptr,
4181 .float_to_int,4193 .float_to_int,
src/Sema.zig+29
...@@ -342,6 +342,10 @@ pub fn analyzeBody(...@@ -342,6 +342,10 @@ pub fn analyzeBody(
342 try sema.zirValidateStructInitPtr(block, inst);342 try sema.zirValidateStructInitPtr(block, inst);
343 continue;343 continue;
344 },344 },
345 .@"export" => {
346 try sema.zirExport(block, inst);
347 continue;
348 },
345349
346 // Special case instructions to handle comptime control flow.350 // Special case instructions to handle comptime control flow.
347 .repeat_inline => {351 .repeat_inline => {
...@@ -1333,6 +1337,31 @@ fn analyzeBlockBody(...@@ -1333,6 +1337,31 @@ fn analyzeBlockBody(
1333 return &merges.block_inst.base;1337 return &merges.block_inst.base;
1334}1338}
13351339
1340fn zirExport(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!void {
1341 const tracy = trace(@src());
1342 defer tracy.end();
1343
1344 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
1345 const extra = sema.code.extraData(zir.Inst.Bin, inst_data.payload_index).data;
1346 const src = inst_data.src();
1347
1348 const target_fn = try sema.resolveInst(extra.lhs);
1349 const target_fn_val = try sema.resolveConstValue(
1350 block,
1351 .{ .node_offset_builtin_call_arg0 = inst_data.src_node },
1352 target_fn,
1353 );
1354
1355 const export_name = try sema.resolveConstString(
1356 block,
1357 .{ .node_offset_builtin_call_arg1 = inst_data.src_node },
1358 extra.rhs,
1359 );
1360
1361 const actual_fn = target_fn_val.castTag(.function).?.data;
1362 try sema.mod.analyzeExport(&block.base, src, export_name, actual_fn.owner_decl);
1363}
1364
1336fn zirBreakpoint(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!void {1365fn zirBreakpoint(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!void {
1337 const tracy = trace(@src());1366 const tracy = trace(@src());
1338 defer tracy.end();1367 defer tracy.end();
src/zir.zig+6
...@@ -328,6 +328,10 @@ pub const Inst = struct {...@@ -328,6 +328,10 @@ pub const Inst = struct {
328 error_union_type,328 error_union_type,
329 /// `error.Foo` syntax. Uses the `str_tok` field of the Data union.329 /// `error.Foo` syntax. Uses the `str_tok` field of the Data union.
330 error_value,330 error_value,
331 /// Exports a function with a specified name. This can be used at comptime
332 /// to export a function conditionally.
333 /// Uses the `pl_node` union field. Payload is `Bin`.
334 @"export",
331 /// Given a pointer to a struct or object that contains virtual fields, returns a pointer335 /// Given a pointer to a struct or object that contains virtual fields, returns a pointer
332 /// to the named field. The field name is stored in string_bytes. Used by a.b syntax.336 /// to the named field. The field name is stored in string_bytes. Used by a.b syntax.
333 /// Uses `pl_node` field. The AST node is the a.b syntax. Payload is Field.337 /// Uses `pl_node` field. The AST node is the a.b syntax. Payload is Field.
...@@ -737,6 +741,7 @@ pub const Inst = struct {...@@ -737,6 +741,7 @@ pub const Inst = struct {
737 .elem_val_node,741 .elem_val_node,
738 .ensure_result_used,742 .ensure_result_used,
739 .ensure_result_non_error,743 .ensure_result_non_error,
744 .@"export",
740 .floatcast,745 .floatcast,
741 .field_ptr,746 .field_ptr,
742 .field_val,747 .field_val,
...@@ -1682,6 +1687,7 @@ const Writer = struct {...@@ -1682,6 +1687,7 @@ const Writer = struct {
1682 .xor,1687 .xor,
1683 .store_node,1688 .store_node,
1684 .error_union_type,1689 .error_union_type,
1690 .@"export",
1685 .merge_error_sets,1691 .merge_error_sets,
1686 .bit_and,1692 .bit_and,
1687 .bit_or,1693 .bit_or,