lib/choir/src/backends/artifact/model/linkage/symbol.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const std = @import("std");
2 const linkage = @import("root.zig");
3
4 const Allocator = std.mem.Allocator;
5
6 pub const SymbolKind = enum(u8) {
7 unknown = 1,
8 function = 2,
9 data = 3,
10 runtime = 4,
11 };
12
13 pub const SymbolBinding = enum(u8) {
14 external = 1,
15 local = 2,
16 weak = 3,
17 };
18
19 pub const Symbol = struct {
20 name: []const u8,
21 kind: SymbolKind = .unknown,
22 binding: SymbolBinding = .external,
23 /// Parameter and result types of a function symbol whose producer records them.
24 signature: ?linkage.Signature = null,
25
26 pub fn dupe(self: Symbol, allocator: Allocator) Allocator.Error!Symbol {
27 return .{
28 .name = try linkage.slice.dupe(allocator, self.name),
29 .kind = self.kind,
30 .binding = self.binding,
31 .signature = self.signature,
32 };
33 }
34
35 pub fn deinit(self: Symbol, allocator: Allocator) void {
36 linkage.slice.free(allocator, self.name);
37 }
38
39 pub fn eql(self: Symbol, other: Symbol) bool {
40 return self.kind == other.kind and
41 self.binding == other.binding and
42 signaturesEql(self.signature, other.signature) and
43 std.mem.eql(u8, self.name, other.name);
44 }
45 };
46
47 fn signaturesEql(left: ?linkage.Signature, right: ?linkage.Signature) bool {
48 if (left == null or right == null) return left == null and right == null;
49 return left.?.eql(&right.?);
50 }