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(
13361336 .dbg_stmt_node,
13371337 .ensure_result_used,
13381338 .ensure_result_non_error,
1339 .@"export",
13391340 .set_eval_branch_quota,
13401341 .compile_log,
13411342 .ensure_err_payload_void,
......@@ -4146,6 +4147,18 @@ fn builtinCall(
41464147 return rvalue(gz, scope, rl, result, node);
41474148 },
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
41494162 .add_with_overflow,
41504163 .align_cast,
41514164 .align_of,
......@@ -4175,7 +4188,6 @@ fn builtinCall(
41754188 .error_name,
41764189 .error_return_trace,
41774190 .err_set_cast,
4178 .@"export",
41794191 .fence,
41804192 .field_parent_ptr,
41814193 .float_to_int,
src/Sema.zig+29
......@@ -342,6 +342,10 @@ pub fn analyzeBody(
342342 try sema.zirValidateStructInitPtr(block, inst);
343343 continue;
344344 },
345 .@"export" => {
346 try sema.zirExport(block, inst);
347 continue;
348 },
345349
346350 // Special case instructions to handle comptime control flow.
347351 .repeat_inline => {
......@@ -1333,6 +1337,31 @@ fn analyzeBlockBody(
13331337 return &merges.block_inst.base;
13341338}
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
13361365fn zirBreakpoint(sema: *Sema, block: *Scope.Block, inst: zir.Inst.Index) InnerError!void {
13371366 const tracy = trace(@src());
13381367 defer tracy.end();
src/zir.zig+6
......@@ -328,6 +328,10 @@ pub const Inst = struct {
328328 error_union_type,
329329 /// `error.Foo` syntax. Uses the `str_tok` field of the Data union.
330330 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",
331335 /// Given a pointer to a struct or object that contains virtual fields, returns a pointer
332336 /// to the named field. The field name is stored in string_bytes. Used by a.b syntax.
333337 /// Uses `pl_node` field. The AST node is the a.b syntax. Payload is Field.
......@@ -737,6 +741,7 @@ pub const Inst = struct {
737741 .elem_val_node,
738742 .ensure_result_used,
739743 .ensure_result_non_error,
744 .@"export",
740745 .floatcast,
741746 .field_ptr,
742747 .field_val,
......@@ -1682,6 +1687,7 @@ const Writer = struct {
16821687 .xor,
16831688 .store_node,
16841689 .error_union_type,
1690 .@"export",
16851691 .merge_error_sets,
16861692 .bit_and,
16871693 .bit_or,