closures
This commit is contained in:
@@ -22,6 +22,7 @@ pub const Type = union(enum) {
|
||||
many_pointer_type: ManyPointerTypeInfo,
|
||||
vector_type: VectorTypeInfo,
|
||||
function_type: FunctionTypeInfo,
|
||||
closure_type: ClosureTypeInfo,
|
||||
any_type,
|
||||
optional_type: OptionalTypeInfo,
|
||||
meta_type: MetaTypeInfo,
|
||||
@@ -44,6 +45,11 @@ pub const Type = union(enum) {
|
||||
return_type: *const Type,
|
||||
};
|
||||
|
||||
pub const ClosureTypeInfo = struct {
|
||||
param_types: []const Type,
|
||||
return_type: *const Type,
|
||||
};
|
||||
|
||||
pub const ArrayTypeInfo = struct {
|
||||
element_name: []const u8,
|
||||
length: u32,
|
||||
@@ -95,6 +101,14 @@ pub const Type = union(enum) {
|
||||
}
|
||||
return info.return_type.eql(o.return_type.*);
|
||||
},
|
||||
.closure_type => |info| {
|
||||
const o = other.closure_type;
|
||||
if (info.param_types.len != o.param_types.len) return false;
|
||||
for (info.param_types, o.param_types) |a, b| {
|
||||
if (!a.eql(b)) return false;
|
||||
}
|
||||
return info.return_type.eql(o.return_type.*);
|
||||
},
|
||||
.optional_type => |info| std.mem.eql(u8, info.child_name, other.optional_type.child_name),
|
||||
.meta_type => |info| std.mem.eql(u8, info.name, other.meta_type.name),
|
||||
.tuple_type => |info| {
|
||||
@@ -302,6 +316,21 @@ pub const Type = union(enum) {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isClosureType(self: Type) bool {
|
||||
return switch (self) {
|
||||
.closure_type => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns true for both bare function pointers and closures
|
||||
pub fn isCallable(self: Type) bool {
|
||||
return switch (self) {
|
||||
.function_type, .closure_type => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isArray(self: Type) bool {
|
||||
return switch (self) {
|
||||
.array_type => true,
|
||||
@@ -356,6 +385,7 @@ pub const Type = union(enum) {
|
||||
.f64 => 64,
|
||||
.boolean => 1,
|
||||
.pointer_type, .many_pointer_type, .function_type => 64,
|
||||
.closure_type => 128, // { ptr, ptr } = 16 bytes
|
||||
else => 0,
|
||||
};
|
||||
}
|
||||
@@ -506,6 +536,20 @@ pub const Type = union(enum) {
|
||||
}
|
||||
return try buf.toOwnedSlice(allocator);
|
||||
},
|
||||
.closure_type => |info| {
|
||||
var buf = std.ArrayList(u8).empty;
|
||||
try buf.appendSlice(allocator, "Closure(");
|
||||
for (info.param_types, 0..) |pt, i| {
|
||||
if (i > 0) try buf.appendSlice(allocator, ", ");
|
||||
try buf.appendSlice(allocator, try pt.displayName(allocator));
|
||||
}
|
||||
try buf.append(allocator, ')');
|
||||
if (!std.meta.eql(info.return_type.*, Type.void_type)) {
|
||||
try buf.appendSlice(allocator, " -> ");
|
||||
try buf.appendSlice(allocator, try info.return_type.displayName(allocator));
|
||||
}
|
||||
return try buf.toOwnedSlice(allocator);
|
||||
},
|
||||
.optional_type => |info| return fmtAlloc(allocator, "?{s}", .{info.child_name}),
|
||||
.meta_type => |info| info.name,
|
||||
.tuple_type => |info| {
|
||||
|
||||
Reference in New Issue
Block a user