refactor(types): shrink src/types.zig to editor/parse metadata (A8.2)
Remove the legacy parallel type model's compiler-like surface. The compiler pipeline resolves/lowers/lays out against canonical src/ir/types.zig (TypeId/TypeTable); src/types.zig.Type is now strictly editor-indexing + parse-time name metadata. - src/types.zig: delete the type-resolution surface (widen, bitWidth, isImplicitlyConvertibleTo) and every helper left dead once it was gone (eql, isInt/isFloat/isSigned/isUnsigned, isTuple/isVector, and the already-unused classification predicates isEnum/isUnion/isString/ isStringLike/isAny/optionalChild/sliceElementType/manyPointerElementType/ vectorElementType/isFunctionType/isClosureType/isCallable). Keep the Type union plus the display/name-classification helpers sema/lsp/parser use (fromName, fromTypeExpr, toName, displayName, isStruct/isOptional/isSlice/ isPointer/isManyPointer/isArray, pointerPointeeType). Seal the file with a doc comment. - src/sema.zig: inferExprType no longer calls Type.widen for arithmetic; it approximates the display type as the left operand's (no second resolver in the editor index). - src/ir/type_bridge.zig: delete the dead bridgeType (legacy Type -> TypeId) function + its sole sx_types import; resolveAstType and the AST->TypeId path are untouched. - src/ir/ir.zig: drop the bridgeType re-export. - src/ir/type_bridge.test.zig: drop the two bridgeType tests (function gone). Gate: zig build, zig build test (exit 0), tests/run_examples.sh 361/0, zero examples/expected churn.
This commit is contained in:
@@ -64,7 +64,6 @@ pub const LLVMEmitter = emit_llvm.LLVMEmitter;
|
||||
|
||||
pub const type_bridge = @import("type_bridge.zig");
|
||||
pub const resolveAstType = type_bridge.resolveAstType;
|
||||
pub const bridgeType = type_bridge.bridgeType;
|
||||
|
||||
pub const jni_descriptor = @import("jni_descriptor.zig");
|
||||
pub const jni_java_emit = @import("jni_java_emit.zig");
|
||||
|
||||
@@ -9,42 +9,6 @@ const TypeId = types.TypeId;
|
||||
const TypeInfo = types.TypeInfo;
|
||||
const TypeTable = types.TypeTable;
|
||||
|
||||
test "bridgeType: primitives" {
|
||||
const alloc = std.testing.allocator;
|
||||
var table = TypeTable.init(alloc);
|
||||
defer table.deinit();
|
||||
|
||||
try std.testing.expectEqual(TypeId.s32, type_bridge.bridgeType(.{ .signed = 32 }, &table, null));
|
||||
try std.testing.expectEqual(TypeId.u8, type_bridge.bridgeType(.{ .unsigned = 8 }, &table, null));
|
||||
try std.testing.expectEqual(TypeId.f64, type_bridge.bridgeType(.f64, &table, null));
|
||||
try std.testing.expectEqual(TypeId.void, type_bridge.bridgeType(.void_type, &table, null));
|
||||
try std.testing.expectEqual(TypeId.bool, type_bridge.bridgeType(.boolean, &table, null));
|
||||
try std.testing.expectEqual(TypeId.string, type_bridge.bridgeType(.string_type, &table, null));
|
||||
try std.testing.expectEqual(TypeId.any, type_bridge.bridgeType(.any_type, &table, null));
|
||||
}
|
||||
|
||||
test "bridgeType: composite types" {
|
||||
const alloc = std.testing.allocator;
|
||||
var table = TypeTable.init(alloc);
|
||||
defer table.deinit();
|
||||
|
||||
// Pointer
|
||||
const ptr_id = type_bridge.bridgeType(.{ .pointer_type = .{ .pointee_name = "s32" } }, &table, null);
|
||||
try std.testing.expectEqual(TypeInfo{ .pointer = .{ .pointee = .s32 } }, table.get(ptr_id));
|
||||
|
||||
// Slice
|
||||
const slice_id = type_bridge.bridgeType(.{ .slice_type = .{ .element_name = "u8" } }, &table, null);
|
||||
try std.testing.expectEqual(TypeInfo{ .slice = .{ .element = .u8 } }, table.get(slice_id));
|
||||
|
||||
// Array
|
||||
const arr_id = type_bridge.bridgeType(.{ .array_type = .{ .element_name = "f32", .length = 4 } }, &table, null);
|
||||
try std.testing.expectEqual(TypeInfo{ .array = .{ .element = .f32, .length = 4 } }, table.get(arr_id));
|
||||
|
||||
// Optional
|
||||
const opt_id = type_bridge.bridgeType(.{ .optional_type = .{ .child_name = "s64" } }, &table, null);
|
||||
try std.testing.expectEqual(TypeInfo{ .optional = .{ .child = .s64 } }, table.get(opt_id));
|
||||
}
|
||||
|
||||
test "resolveAstType: primitive type_expr" {
|
||||
const alloc = std.testing.allocator;
|
||||
var table = TypeTable.init(alloc);
|
||||
|
||||
@@ -2,7 +2,6 @@ const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const ast = @import("../ast.zig");
|
||||
const Node = ast.Node;
|
||||
const sx_types = @import("../types.zig");
|
||||
const ir_types = @import("types.zig");
|
||||
const TypeId = ir_types.TypeId;
|
||||
const TypeInfo = ir_types.TypeInfo;
|
||||
@@ -106,122 +105,8 @@ pub fn resolveAstType(node: ?*const Node, table: *TypeTable, alias_map: AliasMap
|
||||
};
|
||||
}
|
||||
|
||||
// ── types.Type → TypeId ─────────────────────────────────────────────────
|
||||
// Translate an existing codegen Type value into an IR TypeId. Used when
|
||||
// we have access to the codegen's resolved type info (Phase 3+).
|
||||
|
||||
pub fn bridgeType(ty: sx_types.Type, table: *TypeTable, alias_map: AliasMap) TypeId {
|
||||
return switch (ty) {
|
||||
.signed => |w| switch (w) {
|
||||
8 => .s8,
|
||||
16 => .s16,
|
||||
32 => .s32,
|
||||
64 => .s64,
|
||||
// Non-standard width: intern the exact width rather than quantising
|
||||
// to s64 (which would silently change the type's size).
|
||||
else => table.intern(.{ .signed = w }),
|
||||
},
|
||||
.unsigned => |w| switch (w) {
|
||||
8 => .u8,
|
||||
16 => .u16,
|
||||
32 => .u32,
|
||||
64 => .u64,
|
||||
else => table.intern(.{ .unsigned = w }),
|
||||
},
|
||||
.f32 => .f32,
|
||||
.f64 => .f64,
|
||||
.void_type => .void,
|
||||
.boolean => .bool,
|
||||
.string_type => .string,
|
||||
.any_type => .any,
|
||||
.usize_type => .usize,
|
||||
.isize_type => .isize,
|
||||
.enum_type => |name| resolveNamedType(name, .@"enum", table),
|
||||
.struct_type => |name| resolveNamedType(name, .@"struct", table),
|
||||
.union_type => |name| resolveNamedType(name, .@"union", table),
|
||||
.array_type => |info| blk: {
|
||||
const elem = resolveTypeName(info.element_name, table, alias_map);
|
||||
break :blk table.arrayOf(elem, info.length);
|
||||
},
|
||||
.slice_type => |info| blk: {
|
||||
const elem = resolveTypeName(info.element_name, table, alias_map);
|
||||
break :blk table.sliceOf(elem);
|
||||
},
|
||||
.pointer_type => |info| blk: {
|
||||
const pointee = resolveTypeName(info.pointee_name, table, alias_map);
|
||||
break :blk table.ptrTo(pointee);
|
||||
},
|
||||
.many_pointer_type => |info| blk: {
|
||||
const elem = resolveTypeName(info.element_name, table, alias_map);
|
||||
break :blk table.manyPtrTo(elem);
|
||||
},
|
||||
.optional_type => |info| blk: {
|
||||
const child = resolveTypeName(info.child_name, table, alias_map);
|
||||
break :blk table.optionalOf(child);
|
||||
},
|
||||
.vector_type => |info| blk: {
|
||||
const elem = resolveTypeName(info.element_name, table, alias_map);
|
||||
break :blk table.vectorOf(elem, info.length);
|
||||
},
|
||||
.function_type => |info| blk: {
|
||||
const alloc = table.alloc;
|
||||
var param_ids = std.ArrayList(TypeId).empty;
|
||||
for (info.param_types) |pt| {
|
||||
param_ids.append(alloc, bridgeType(pt, table, alias_map)) catch unreachable;
|
||||
}
|
||||
const ret_id = bridgeType(info.return_type.*, table, alias_map);
|
||||
break :blk table.functionType(param_ids.items, ret_id);
|
||||
},
|
||||
.closure_type => |info| blk: {
|
||||
const alloc = table.alloc;
|
||||
var param_ids = std.ArrayList(TypeId).empty;
|
||||
for (info.param_types) |pt| {
|
||||
param_ids.append(alloc, bridgeType(pt, table, alias_map)) catch unreachable;
|
||||
}
|
||||
const ret_id = bridgeType(info.return_type.*, table, alias_map);
|
||||
break :blk table.closureType(param_ids.items, ret_id);
|
||||
},
|
||||
.tuple_type => |info| blk: {
|
||||
const alloc = table.alloc;
|
||||
var field_ids = std.ArrayList(TypeId).empty;
|
||||
for (info.field_types) |ft| {
|
||||
field_ids.append(alloc, bridgeType(ft, table, alias_map)) catch unreachable;
|
||||
}
|
||||
var name_ids: ?[]const StringId = null;
|
||||
if (info.field_names) |names| {
|
||||
var ids = std.ArrayList(StringId).empty;
|
||||
for (names) |n| {
|
||||
ids.append(alloc, table.internString(n)) catch unreachable;
|
||||
}
|
||||
name_ids = ids.items;
|
||||
}
|
||||
break :blk table.intern(.{ .tuple = .{
|
||||
.fields = field_ids.items,
|
||||
.names = name_ids,
|
||||
} });
|
||||
},
|
||||
.meta_type => .any, // meta types map to Any for now
|
||||
.unresolved => .unresolved,
|
||||
};
|
||||
}
|
||||
|
||||
// ── Internal helpers ─────────────────────────────────────────────────────
|
||||
|
||||
const NamedKind = enum { @"struct", @"enum", @"union" };
|
||||
|
||||
fn resolveNamedType(name: []const u8, kind: NamedKind, table: *TypeTable) TypeId {
|
||||
// Check if primitive first
|
||||
if (resolveTypePrimitive(name)) |id| return id;
|
||||
|
||||
// Register as a named type
|
||||
const name_id = table.internString(name);
|
||||
return switch (kind) {
|
||||
.@"struct" => table.intern(.{ .@"struct" = .{ .name = name_id, .fields = &.{} } }),
|
||||
.@"enum" => table.intern(.{ .@"enum" = .{ .name = name_id, .variants = &.{} } }),
|
||||
.@"union" => table.intern(.{ .@"union" = .{ .name = name_id, .fields = &.{} } }),
|
||||
};
|
||||
}
|
||||
|
||||
/// Resolve a bare type name. The algorithm lives in `type_resolver.zig`
|
||||
/// (`TypeResolver.resolveNamed`, the single source); `type_bridge` forwards the
|
||||
/// caller-threaded `alias_map` (the single-source `ProgramIndex.type_alias_map`).
|
||||
|
||||
Reference in New Issue
Block a user