lib/choir/src/composition/module/callsite.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

 1 const std = @import("std");
 2 const composition = @import("../root.zig");
 3 
 4 const Allocator = std.mem.Allocator;
 5 const source = composition.source;
 6 
 7 pub const CallSiteSpec = struct {
 8     id: source.CallSiteId,
 9     name: []const u8,
10     caller: source.FragmentId,
11     caller_export: []const u8,
12     runtime_import: []const u8,
13     callee: source.FragmentId,
14     callee_export: []const u8,
15     inputs: []const source.BoundaryId,
16     outputs: []const source.BoundaryId,
17     abi_version: u32,
18     provenance: source.ProvenanceSpec,
19 };
20 
21 pub const CallSite = struct {
22     id: source.CallSiteId,
23     name: []const u8,
24     caller: source.FragmentId,
25     caller_export: []const u8,
26     runtime_import: []const u8,
27     callee: source.FragmentId,
28     callee_export: []const u8,
29     inputs: []const source.BoundaryId,
30     outputs: []const source.BoundaryId,
31     abi_version: u32,
32     provenance: source.Provenance,
33 
34     pub fn init(allocator: Allocator, spec: CallSiteSpec) Allocator.Error!CallSite {
35         const name = try allocator.dupe(u8, spec.name);
36         errdefer allocator.free(name);
37         const caller_export = try allocator.dupe(u8, spec.caller_export);
38         errdefer allocator.free(caller_export);
39         const runtime_import = try allocator.dupe(u8, spec.runtime_import);
40         errdefer allocator.free(runtime_import);
41         const callee_export = try allocator.dupe(u8, spec.callee_export);
42         errdefer allocator.free(callee_export);
43         const inputs = try source.slice.dupeOrEmpty(source.BoundaryId, allocator, spec.inputs);
44         errdefer source.slice.freeOrEmpty(source.BoundaryId, allocator, inputs);
45         const outputs = try source.slice.dupeOrEmpty(source.BoundaryId, allocator, spec.outputs);
46         errdefer source.slice.freeOrEmpty(source.BoundaryId, allocator, outputs);
47         return .{
48             .id = spec.id,
49             .name = name,
50             .caller = spec.caller,
51             .caller_export = caller_export,
52             .runtime_import = runtime_import,
53             .callee = spec.callee,
54             .callee_export = callee_export,
55             .inputs = inputs,
56             .outputs = outputs,
57             .abi_version = spec.abi_version,
58             .provenance = try source.Provenance.init(allocator, spec.provenance),
59         };
60     }
61 
62     pub fn deinit(self: *CallSite, allocator: Allocator) void {
63         self.provenance.deinit(allocator);
64         source.slice.freeOrEmpty(source.BoundaryId, allocator, self.outputs);
65         source.slice.freeOrEmpty(source.BoundaryId, allocator, self.inputs);
66         allocator.free(self.callee_export);
67         allocator.free(self.runtime_import);
68         allocator.free(self.caller_export);
69         allocator.free(self.name);
70         self.* = undefined;
71     }
72 };