Skip to content

Commit 5269cbe

Browse files
authored
Merge pull request #9797 from Vexu/stage2
stage2: implement cImport
2 parents 1ad905c + 0c74ce1 commit 5269cbe

11 files changed

+550
-267
lines changed

src/AstGen.zig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7082,7 +7082,7 @@ fn builtinCall(
70827082
.bit_cast => return bitCast( gz, scope, rl, node, params[0], params[1]),
70837083
.TypeOf => return typeOf( gz, scope, rl, node, params),
70847084
.union_init => return unionInit(gz, scope, rl, node, params),
7085-
.c_import => return cImport( gz, scope, rl, node, params[0]),
7085+
.c_import => return cImport( gz, scope, node, params[0]),
70867086

70877087
.@"export" => {
70887088
const node_tags = tree.nodes.items(.tag);
@@ -7269,6 +7269,7 @@ fn builtinCall(
72697269
return rvalue(gz, rl, result, node);
72707270
},
72717271
.c_define => {
7272+
if (!gz.c_import) return gz.astgen.failNode(node, "C define valid only inside C import block", .{});
72727273
const name = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, params[0]);
72737274
const value = try comptimeExpr(gz, scope, .none, params[1]);
72747275
const result = try gz.addExtendedPayload(.c_define, Zir.Inst.BinNode{
@@ -7641,6 +7642,8 @@ fn simpleCBuiltin(
76417642
operand_node: Ast.Node.Index,
76427643
tag: Zir.Inst.Extended,
76437644
) InnerError!Zir.Inst.Ref {
7645+
const name: []const u8 = if (tag == .c_undef) "C undef" else "C include";
7646+
if (!gz.c_import) return gz.astgen.failNode(node, "{s} valid only inside C import block", .{name});
76447647
const operand = try comptimeExpr(gz, scope, .{ .ty = .const_slice_u8_type }, operand_node);
76457648
_ = try gz.addExtendedPayload(tag, Zir.Inst.UnNode{
76467649
.node = gz.nodeIndexToRelative(node),
@@ -7689,7 +7692,6 @@ fn shiftOp(
76897692
fn cImport(
76907693
gz: *GenZir,
76917694
scope: *Scope,
7692-
rl: ResultLoc,
76937695
node: Ast.Node.Index,
76947696
body_node: Ast.Node.Index,
76957697
) InnerError!Zir.Inst.Ref {
@@ -7698,6 +7700,7 @@ fn cImport(
76987700

76997701
var block_scope = gz.makeSubBlock(scope);
77007702
block_scope.force_comptime = true;
7703+
block_scope.c_import = true;
77017704
defer block_scope.instructions.deinit(gpa);
77027705

77037706
const block_inst = try gz.addBlock(.c_import, node);
@@ -7708,7 +7711,7 @@ fn cImport(
77087711
try block_scope.setBlockBody(block_inst);
77097712
try gz.instructions.append(gpa, block_inst);
77107713

7711-
return rvalue(gz, rl, .void_value, node);
7714+
return indexToRef(block_inst);
77127715
}
77137716

77147717
fn overflowArithmetic(
@@ -9025,6 +9028,7 @@ const GenZir = struct {
90259028
base: Scope = Scope{ .tag = base_tag },
90269029
force_comptime: bool,
90279030
in_defer: bool,
9031+
c_import: bool = false,
90289032
/// How decls created in this scope should be named.
90299033
anon_name_strategy: Zir.Inst.NameStrategy = .anon,
90309034
/// The containing decl AST node.
@@ -9070,6 +9074,7 @@ const GenZir = struct {
90709074
return .{
90719075
.force_comptime = gz.force_comptime,
90729076
.in_defer = gz.in_defer,
9077+
.c_import = gz.c_import,
90739078
.decl_node_index = gz.decl_node_index,
90749079
.decl_line = gz.decl_line,
90759080
.parent = scope,

src/Compilation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2644,7 +2644,7 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
26442644

26452645
const dep_basename = std.fs.path.basename(out_dep_path);
26462646
try man.addDepFilePost(zig_cache_tmp_dir, dep_basename);
2647-
try comp.stage1_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
2647+
if (build_options.is_stage1 and comp.bin_file.options.use_stage1) try comp.stage1_cache_manifest.addDepFilePost(zig_cache_tmp_dir, dep_basename);
26482648

26492649
const digest = man.final();
26502650
const o_sub_path = try std.fs.path.join(arena, &[_][]const u8{ "o", &digest });

src/Module.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,8 @@ pub const Scope = struct {
13251325
/// when null, it is determined by build mode, changed by @setRuntimeSafety
13261326
want_safety: ?bool = null,
13271327

1328+
c_import_buf: ?*std.ArrayList(u8) = null,
1329+
13281330
const Param = struct {
13291331
/// `noreturn` means `anytype`.
13301332
ty: Type,
@@ -1377,6 +1379,7 @@ pub const Scope = struct {
13771379
.runtime_loop = parent.runtime_loop,
13781380
.runtime_index = parent.runtime_index,
13791381
.want_safety = parent.want_safety,
1382+
.c_import_buf = parent.c_import_buf,
13801383
};
13811384
}
13821385

0 commit comments

Comments
 (0)