pointers
This commit is contained in:
@@ -18,6 +18,8 @@ pub const Type = union(enum) {
|
||||
union_type: []const u8,
|
||||
array_type: ArrayTypeInfo,
|
||||
slice_type: SliceTypeInfo,
|
||||
pointer_type: PointerTypeInfo,
|
||||
many_pointer_type: ManyPointerTypeInfo,
|
||||
vector_type: VectorTypeInfo,
|
||||
any_type,
|
||||
meta_type: MetaTypeInfo,
|
||||
@@ -26,6 +28,14 @@ pub const Type = union(enum) {
|
||||
element_name: []const u8,
|
||||
};
|
||||
|
||||
pub const PointerTypeInfo = struct {
|
||||
pointee_name: []const u8,
|
||||
};
|
||||
|
||||
pub const ManyPointerTypeInfo = struct {
|
||||
element_name: []const u8,
|
||||
};
|
||||
|
||||
pub const ArrayTypeInfo = struct {
|
||||
element_name: []const u8,
|
||||
length: u32,
|
||||
@@ -56,6 +66,14 @@ pub const Type = union(enum) {
|
||||
if (std.mem.eql(u8, name, "f32")) return .f32;
|
||||
if (std.mem.eql(u8, name, "f64")) return .f64;
|
||||
if (std.mem.eql(u8, name, "Any")) return .any_type;
|
||||
// Many-pointer: [*]T
|
||||
if (name.len >= 4 and name[0] == '[' and name[1] == '*' and name[2] == ']') {
|
||||
return .{ .many_pointer_type = .{ .element_name = name[3..] } };
|
||||
}
|
||||
// Pointer: *T
|
||||
if (name.len >= 2 and name[0] == '*') {
|
||||
return .{ .pointer_type = .{ .pointee_name = name[1..] } };
|
||||
}
|
||||
// Variable-width integers: s1..s64, u1..u64
|
||||
if (name.len >= 2 and (name[0] == 's' or name[0] == 'u')) {
|
||||
const width = std.fmt.parseInt(u8, name[1..], 10) catch return null;
|
||||
@@ -112,6 +130,34 @@ pub const Type = union(enum) {
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isPointer(self: Type) bool {
|
||||
return switch (self) {
|
||||
.pointer_type => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn pointerPointeeType(self: Type) ?Type {
|
||||
return switch (self) {
|
||||
.pointer_type => |info| fromName(info.pointee_name),
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isManyPointer(self: Type) bool {
|
||||
return switch (self) {
|
||||
.many_pointer_type => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn manyPointerElementType(self: Type) ?Type {
|
||||
return switch (self) {
|
||||
.many_pointer_type => |info| fromName(info.element_name),
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn isArray(self: Type) bool {
|
||||
return switch (self) {
|
||||
.array_type => true,
|
||||
@@ -184,6 +230,19 @@ pub const Type = union(enum) {
|
||||
if (self.isSlice() and target.isSlice()) {
|
||||
return std.mem.eql(u8, self.slice_type.element_name, target.slice_type.element_name);
|
||||
}
|
||||
// Pointer types: compare pointee names by content, null (*void) → any pointer
|
||||
if (self.isPointer() and target.isPointer()) {
|
||||
if (std.mem.eql(u8, self.pointer_type.pointee_name, "void")) return true;
|
||||
return std.mem.eql(u8, self.pointer_type.pointee_name, target.pointer_type.pointee_name);
|
||||
}
|
||||
// Many-pointer types: compare element names by content
|
||||
if (self.isManyPointer() and target.isManyPointer()) {
|
||||
return std.mem.eql(u8, self.many_pointer_type.element_name, target.many_pointer_type.element_name);
|
||||
}
|
||||
// *T → [*]T: pointer to element is implicitly convertible to many-pointer
|
||||
if (self.isPointer() and target.isManyPointer()) {
|
||||
return std.mem.eql(u8, self.pointer_type.pointee_name, target.many_pointer_type.element_name);
|
||||
}
|
||||
|
||||
const src_float = self.isFloat();
|
||||
const dst_float = target.isFloat();
|
||||
@@ -250,6 +309,18 @@ pub const Type = union(enum) {
|
||||
try buf.appendSlice(allocator, info.element_name);
|
||||
return try buf.toOwnedSlice(allocator);
|
||||
},
|
||||
.pointer_type => |info| {
|
||||
var buf = std.ArrayList(u8).empty;
|
||||
try buf.append(allocator, '*');
|
||||
try buf.appendSlice(allocator, info.pointee_name);
|
||||
return try buf.toOwnedSlice(allocator);
|
||||
},
|
||||
.many_pointer_type => |info| {
|
||||
var buf = std.ArrayList(u8).empty;
|
||||
try buf.appendSlice(allocator, "[*]");
|
||||
try buf.appendSlice(allocator, info.element_name);
|
||||
return try buf.toOwnedSlice(allocator);
|
||||
},
|
||||
.array_type => |info| {
|
||||
var buf = std.ArrayList(u8).empty;
|
||||
try buf.append(allocator, '[');
|
||||
|
||||
Reference in New Issue
Block a user