add predefined values to Index

This commit is contained in:
Techarix 2023-02-08 21:01:15 +01:00
parent 6278880f42
commit 57fc10eec0
6 changed files with 426 additions and 395 deletions

View File

@ -20,7 +20,7 @@ pub const ComptimeInterpreter = @This();
const log = std.log.scoped(.comptime_interpreter); const log = std.log.scoped(.comptime_interpreter);
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
ip: InternPool = .{}, ip: InternPool,
document_store: *DocumentStore, document_store: *DocumentStore,
uri: DocumentStore.Uri, uri: DocumentStore.Uri,
namespaces: std.MultiArrayList(Namespace) = .{}, namespaces: std.MultiArrayList(Namespace) = .{},
@ -209,8 +209,6 @@ pub fn interpret(
// .tagged_union_enum_tag_trailing, // .tagged_union_enum_tag_trailing,
.root, .root,
=> { => {
const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type });
try interpreter.namespaces.append(interpreter.allocator, .{ try interpreter.namespaces.append(interpreter.allocator, .{
.parent = namespace, .parent = namespace,
.node_idx = node_idx, .node_idx = node_idx,
@ -243,7 +241,7 @@ pub fn interpret(
else else
(try (try interpreter.interpret(container_field.ast.value_expr, container_namespace, .{})).getValue()).val; // TODO check ty (try (try interpreter.interpret(container_field.ast.value_expr, container_namespace, .{})).getValue()).val; // TODO check ty
if (init_type_value.ty != type_type) { if (init_type_value.ty != Index.type) {
try interpreter.recordError( try interpreter.recordError(
container_field.ast.type_expr, container_field.ast.type_expr,
"expected_type", "expected_type",
@ -269,7 +267,7 @@ pub fn interpret(
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = type_type, .ty = Index.type,
.val = struct_type, .val = struct_type,
} }; } };
}, },
@ -316,8 +314,7 @@ pub fn interpret(
if (type_value == null and init_value == null) return InterpretResult{ .nothing = {} }; if (type_value == null and init_value == null) return InterpretResult{ .nothing = {} };
if (type_value) |v| { if (type_value) |v| {
const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type }); if (v.ty != Index.type) return InterpretResult{ .nothing = {} };
if (v.ty != type_type) return InterpretResult{ .nothing = {} };
} }
const decl = interpreter.ip.getDecl(decl_index); const decl = interpreter.ip.getDecl(decl_index);
@ -385,44 +382,53 @@ pub fn interpret(
.identifier => { .identifier => {
const identifier = offsets.nodeToSlice(tree, node_idx); const identifier = offsets.nodeToSlice(tree, node_idx);
const simples = std.ComptimeStringMap(InternPool.Simple, .{ const simples = std.ComptimeStringMap(Index, .{
.{ "anyerror", .anyerror }, .{ "anyerror", Index.anyerror },
.{ "anyframe", .@"anyframe" }, .{ "anyframe", Index.@"anyframe" },
.{ "anyopaque", .anyopaque }, .{ "anyopaque", Index.anyopaque },
.{ "bool", .bool }, .{ "bool", Index.bool },
.{ "c_int", .c_int }, .{ "c_int", Index.c_int },
.{ "c_long", .c_long }, .{ "c_long", Index.c_long },
.{ "c_longdouble", .c_longdouble }, .{ "c_longdouble", Index.c_longdouble },
.{ "c_longlong", .c_longlong }, .{ "c_longlong", Index.c_longlong },
.{ "c_short", .c_short }, .{ "c_short", Index.c_short },
.{ "c_uint", .c_uint }, .{ "c_uint", Index.c_uint },
.{ "c_ulong", .c_ulong }, .{ "c_ulong", Index.c_ulong },
.{ "c_ulonglong", .c_ulonglong }, .{ "c_ulonglong", Index.c_ulonglong },
.{ "c_ushort", .c_ushort }, .{ "c_ushort", Index.c_ushort },
.{ "comptime_float", .comptime_float }, .{ "comptime_float", Index.comptime_float },
.{ "comptime_int", .comptime_int }, .{ "comptime_int", Index.comptime_int },
.{ "f128", .f128 }, .{ "f128", Index.f128 },
.{ "f16", .f16 }, .{ "f16", Index.f16 },
.{ "f32", .f32 }, .{ "f32", Index.f32 },
.{ "f64", .f64 }, .{ "f64", Index.f64 },
.{ "f80", .f80 }, .{ "f80", Index.f80 },
.{ "false", .bool_false }, .{ "false", Index.bool_false },
.{ "isize", .isize }, .{ "isize", Index.isize },
.{ "noreturn", .noreturn }, .{ "noreturn", Index.noreturn },
.{ "null", .null_value }, .{ "null", Index.null_value },
.{ "true", .bool_true }, .{ "true", Index.bool_true },
.{ "type", .type }, .{ "type", Index.type },
.{ "undefined", .undefined_value }, .{ "undefined", Index.undefined_value },
.{ "usize", .usize }, .{ "usize", Index.usize },
.{ "void", .void }, .{ "void", Index.void },
}); });
if (simples.get(identifier)) |simple| { if (simples.get(identifier)) |index| {
const ty: Index = switch (index) {
.undefined_value => .undefined_type,
.void_value => .void,
.unreachable_value => .noreturn,
.null_value => .null_type,
.bool_true => .bool,
.bool_false => .bool,
else => .type,
};
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = try interpreter.ip.get(interpreter.allocator, Key{ .simple = simple.toType() }), .ty = ty,
.val = try interpreter.ip.get(interpreter.allocator, Key{ .simple = simple }), .val = index,
} }; } };
} }
@ -430,7 +436,7 @@ pub fn interpret(
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type }), .ty = Index.type,
.val = try interpreter.ip.get(interpreter.allocator, Key{ .int_type = .{ .val = try interpreter.ip.get(interpreter.allocator, Key{ .int_type = .{
.signedness = if (identifier[0] == 'u') .unsigned else .signed, .signedness = if (identifier[0] == 'u') .unsigned else .signed,
.bits = std.fmt.parseInt(u16, identifier[1..], 10) catch break :blk, .bits = std.fmt.parseInt(u16, identifier[1..], 10) catch break :blk,
@ -456,7 +462,8 @@ pub fn interpret(
"use of undeclared identifier '{s}'", "use of undeclared identifier '{s}'",
.{identifier}, .{identifier},
); );
return error.IdentifierNotFound; // return error.IdentifierNotFound;
return InterpretResult{ .nothing = {} };
}, },
.field_access => { .field_access => {
if (data[node_idx].rhs == 0) return error.CriticalAstFailure; if (data[node_idx].rhs == 0) return error.CriticalAstFailure;
@ -472,7 +479,7 @@ pub fn interpret(
}; };
const can_have_fields: bool = switch (inner_lhs) { const can_have_fields: bool = switch (inner_lhs) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.type => blk: { .type => blk: {
if (irv.val == .none) break :blk true; if (irv.val == .none) break :blk true;
@ -530,7 +537,7 @@ pub fn interpret(
.value = Value{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = data[node_idx].rhs, .node_idx = data[node_idx].rhs,
.ty = try interpreter.ip.get(interpreter.allocator, .{ .simple = .usize }), .ty = Index.usize,
.val = .none, // TODO resolve length of Slice .val = .none, // TODO resolve length of Slice
}, },
}; };
@ -541,7 +548,7 @@ pub fn interpret(
.value = Value{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = data[node_idx].rhs, .node_idx = data[node_idx].rhs,
.ty = try interpreter.ip.get(interpreter.allocator, .{ .simple = .usize }), .ty = Index.usize,
.val = .none, // TODO resolve length of Slice .val = .none, // TODO resolve length of Slice
}, },
}; };
@ -556,7 +563,7 @@ pub fn interpret(
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = data[node_idx].rhs, .node_idx = data[node_idx].rhs,
.ty = try interpreter.ip.get(interpreter.allocator, .{ .simple = .comptime_int }), .ty = Index.comptime_int,
.val = len_value, .val = len_value,
} }; } };
} }
@ -564,8 +571,7 @@ pub fn interpret(
}, },
.optional_type => |optional_info| blk: { .optional_type => |optional_info| blk: {
if (!std.mem.eql(u8, field_name, "?")) break :blk false; if (!std.mem.eql(u8, field_name, "?")) break :blk false;
const null_value = try interpreter.ip.get(interpreter.allocator, .{ .simple = .null_value }); if (irv.val == Index.null_value) {
if (irv.val == null_value) {
try interpreter.recordError( try interpreter.recordError(
node_idx, node_idx,
"null_unwrap", "null_unwrap",
@ -613,7 +619,7 @@ pub fn interpret(
else => false, else => false,
}; };
const accessed_ty = if (inner_lhs == .simple and inner_lhs.simple == .type) irv.val else irv.ty; const accessed_ty = if (inner_lhs == .simple_type and inner_lhs.simple_type == .type) irv.val else irv.ty;
if (accessed_ty != .none) { if (accessed_ty != .none) {
if (can_have_fields) { if (can_have_fields) {
try interpreter.recordError( try interpreter.recordError(
@ -657,12 +663,9 @@ pub fn interpret(
// if (options.observe_values) { // if (options.observe_values) {
const ir = try interpreter.interpret(if_info.ast.cond_expr, namespace, options); const ir = try interpreter.interpret(if_info.ast.cond_expr, namespace, options);
const false_value = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .bool_false });
const true_value = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .bool_true });
const condition = (try ir.getValue()).val; const condition = (try ir.getValue()).val;
std.debug.assert(condition == false_value or condition == true_value); std.debug.assert(condition == Index.bool_false or condition == Index.bool_true);
if (condition == true_value) { if (condition == Index.bool_true) {
return try interpreter.interpret(if_info.ast.then_expr, namespace, options); return try interpreter.interpret(if_info.ast.then_expr, namespace, options);
} else { } else {
if (if_info.ast.else_expr != 0) { if (if_info.ast.else_expr != 0) {
@ -679,8 +682,8 @@ pub fn interpret(
.value = Value{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .bool }), .ty = Index.bool,
.val = try interpreter.ip.get(interpreter.allocator, Key{ .simple = if (a_value.val == b_value.val) .bool_true else .bool_false }), // TODO eql function required? .val = if (a_value.val == b_value.val) Index.bool_true else Index.bool_false, // TODO eql function required?
}, },
}; };
}, },
@ -690,9 +693,7 @@ pub fn interpret(
if (nl == .failure) return error.CriticalAstFailure; if (nl == .failure) return error.CriticalAstFailure;
const number_type = try interpreter.ip.get(interpreter.allocator, Key{ const number_type = if (nl == .float) Index.comptime_float else Index.comptime_int;
.simple = if (nl == .float) .comptime_float else .comptime_int,
});
const value = try interpreter.ip.get( const value = try interpreter.ip.get(
interpreter.allocator, interpreter.allocator,
@ -826,7 +827,7 @@ pub fn interpret(
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = try interpreter.ip.get(interpreter.allocator, Key{ .struct_type = struct_index }), .ty = try interpreter.ip.get(interpreter.allocator, Key{ .struct_type = struct_index }),
.val = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .undefined_value }), .val = Index.undefined_value,
} }; } };
} }
@ -834,13 +835,13 @@ pub fn interpret(
defer interpreter.allocator.free(import_uri); defer interpreter.allocator.free(import_uri);
var handle = interpreter.document_store.getOrLoadHandle(import_uri) orelse return error.ImportFailure; var handle = interpreter.document_store.getOrLoadHandle(import_uri) orelse return error.ImportFailure;
try interpreter.document_store.ensureInterpreterExists(handle.uri); _ = try interpreter.document_store.ensureInterpreterExists(handle.uri);
return InterpretResult{ return InterpretResult{
.value = Value{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type }), .ty = Index.type,
.val = .none, // TODO .val = .none, // TODO
}, },
}; };
@ -853,7 +854,7 @@ pub fn interpret(
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type }), .ty = Index.type,
.val = value.ty, .val = value.ty,
} }; } };
} }
@ -864,9 +865,7 @@ pub fn interpret(
const value = try (try interpreter.interpret(params[0], namespace, options)).getValue(); const value = try (try interpreter.interpret(params[0], namespace, options)).getValue();
const field_name = try (try interpreter.interpret(params[1], namespace, options)).getValue(); const field_name = try (try interpreter.interpret(params[1], namespace, options)).getValue();
const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type }); if (value.ty != Index.type or value.ty == .none) return error.InvalidBuiltin;
if (value.ty != type_type or value.ty == .none) return error.InvalidBuiltin;
if (interpreter.ip.indexToKey(field_name.ty) != .pointer_type) return error.InvalidBuiltin; // Check if it's a []const u8 if (interpreter.ip.indexToKey(field_name.ty) != .pointer_type) return error.InvalidBuiltin; // Check if it's a []const u8
if (value.val == .none) return error.InvalidBuiltin; if (value.val == .none) return error.InvalidBuiltin;
@ -881,8 +880,8 @@ pub fn interpret(
return InterpretResult{ .value = Value{ return InterpretResult{ .value = Value{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .bool }), .ty = Index.bool,
.val = try interpreter.ip.get(interpreter.allocator, Key{ .simple = if (has_decl) .bool_true else .bool_false }), .val = if (has_decl) Index.bool_true else Index.bool_false,
} }; } };
} }
@ -892,9 +891,7 @@ pub fn interpret(
const as_type = try (try interpreter.interpret(params[0], namespace, options)).getValue(); const as_type = try (try interpreter.interpret(params[0], namespace, options)).getValue();
const value = try (try interpreter.interpret(params[1], namespace, options)).getValue(); const value = try (try interpreter.interpret(params[1], namespace, options)).getValue();
const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type }); if (as_type.ty != Index.type) return error.InvalidBuiltin;
if (as_type.ty != type_type) return error.InvalidBuiltin;
return InterpretResult{ return InterpretResult{
.value = Value{ .value = Value{
@ -955,8 +952,6 @@ pub fn interpret(
// TODO: Resolve function type // TODO: Resolve function type
const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type });
const function_type = try interpreter.ip.get(interpreter.allocator, Key{ .function_type = .{ const function_type = try interpreter.ip.get(interpreter.allocator, Key{ .function_type = .{
.calling_convention = .Unspecified, .calling_convention = .Unspecified,
.alignment = 0, .alignment = 0,
@ -995,7 +990,7 @@ pub fn interpret(
const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{ const decl_index = try interpreter.ip.createDecl(interpreter.allocator, .{
.name = name, .name = name,
.ty = type_type, .ty = Index.type,
.val = function_type, .val = function_type,
.alignment = 0, // TODO .alignment = 0, // TODO
.address_space = .generic, // TODO .address_space = .generic, // TODO
@ -1040,10 +1035,9 @@ pub fn interpret(
}, },
.bool_not => { .bool_not => {
const result = try interpreter.interpret(data[node_idx].lhs, namespace, .{}); const result = try interpreter.interpret(data[node_idx].lhs, namespace, .{});
const bool_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .bool });
const value = try result.getValue(); const value = try result.getValue();
if (value.ty != bool_type) { if (value.ty != Index.bool) {
try interpreter.recordError( try interpreter.recordError(
node_idx, node_idx,
"invalid_deref", "invalid_deref",
@ -1053,15 +1047,12 @@ pub fn interpret(
return error.InvalidOperation; return error.InvalidOperation;
} }
const false_value = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .bool_false }); std.debug.assert(value.val == Index.bool_false or value.val == Index.bool_true);
const true_value = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .bool_true });
std.debug.assert(value.val == false_value or value.val == true_value);
return InterpretResult{ .value = .{ return InterpretResult{ .value = .{
.interpreter = interpreter, .interpreter = interpreter,
.node_idx = node_idx, .node_idx = node_idx,
.ty = bool_type, .ty = Index.bool,
.val = if (value.val == false_value) true_value else false_value, .val = if (value.val == Index.bool_false) Index.bool_true else Index.bool_false,
} }; } };
}, },
.address_of => { .address_of => {
@ -1146,14 +1137,12 @@ pub fn call(
}); });
const fn_namespace = @intToEnum(Namespace.Index, interpreter.namespaces.len - 1); const fn_namespace = @intToEnum(Namespace.Index, interpreter.namespaces.len - 1);
const type_type = try interpreter.ip.get(interpreter.allocator, Key{ .simple = .type });
var arg_it = proto.iterate(&tree); var arg_it = proto.iterate(&tree);
var arg_index: usize = 0; var arg_index: usize = 0;
while (ast.nextFnParam(&arg_it)) |param| { while (ast.nextFnParam(&arg_it)) |param| {
if (arg_index >= arguments.len) return error.MissingArguments; if (arg_index >= arguments.len) return error.MissingArguments;
var tex = try (try interpreter.interpret(param.type_expr, fn_namespace, options)).getValue(); var tex = try (try interpreter.interpret(param.type_expr, fn_namespace, options)).getValue();
if (tex.ty != type_type) { if (tex.ty != Index.type) {
try interpreter.recordError( try interpreter.recordError(
param.type_expr, param.type_expr,
"expected_type", "expected_type",

View File

@ -989,16 +989,26 @@ pub fn enumCompletionItems(self: DocumentStore, arena: std.mem.Allocator, handle
return try self.tagStoreCompletionItems(arena, handle, "enum_completions"); return try self.tagStoreCompletionItems(arena, handle, "enum_completions");
} }
pub fn ensureInterpreterExists(self: *DocumentStore, uri: Uri) !void { pub fn ensureInterpreterExists(self: *DocumentStore, uri: Uri) !*ComptimeInterpreter {
var handle = self.handles.get(uri).?; var handle = self.handles.get(uri).?;
if (handle.interpreter == null) { if (handle.interpreter != null) return handle.interpreter.?;
var int = try self.allocator.create(ComptimeInterpreter);
int.* = ComptimeInterpreter{ {
var interpreter = try self.allocator.create(ComptimeInterpreter);
errdefer self.allocator.destroy(interpreter);
var ip = try ComptimeInterpreter.InternPool.init(self.allocator);
errdefer ip.deinit(self.allocator);
interpreter.* = ComptimeInterpreter{
.allocator = self.allocator, .allocator = self.allocator,
.ip = ip,
.document_store = self, .document_store = self,
.uri = uri, .uri = uri,
}; };
handle.interpreter = int; handle.interpreter = interpreter;
_ = try int.interpret(0, .none, .{});
} }
_ = try handle.interpreter.?.interpret(0, .none, .{});
return handle.interpreter.?;
} }

View File

@ -161,7 +161,8 @@ pub const Decl = struct {
}; };
pub const Key = union(enum) { pub const Key = union(enum) {
simple: Simple, simple_type: SimpleType,
simple_value: SimpleValue,
int_type: Int, int_type: Int,
pointer_type: Pointer, pointer_type: Pointer,
@ -209,7 +210,8 @@ pub const Key = union(enum) {
pub fn tag(key: Key) Tag { pub fn tag(key: Key) Tag {
return switch (key) { return switch (key) {
.simple => .simple, .simple_type => .simple_type,
.simple_value => .simple_value,
.int_type => |int_info| switch (int_info.signedness) { .int_type => |int_info| switch (int_info.signedness) {
.signed => .type_int_signed, .signed => .type_int_signed,
@ -245,7 +247,7 @@ pub const Key = union(enum) {
pub fn zigTypeTag(key: Key) std.builtin.TypeId { pub fn zigTypeTag(key: Key) std.builtin.TypeId {
return switch (key) { return switch (key) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.f16, .f16,
.f32, .f32,
.f64, .f64,
@ -279,14 +281,6 @@ pub const Key = union(enum) {
.null_type => .Null, .null_type => .Null,
.undefined_type => .Undefined, .undefined_type => .Undefined,
.enum_literal_type => .EnumLiteral, .enum_literal_type => .EnumLiteral,
.undefined_value,
.void_value,
.unreachable_value,
.null_value,
.bool_true,
.bool_false,
=> unreachable,
}, },
.int_type => .Int, .int_type => .Int,
@ -303,6 +297,7 @@ pub const Key = union(enum) {
.vector_type => .Vector, .vector_type => .Vector,
.anyframe_type => .AnyFrame, .anyframe_type => .AnyFrame,
.simple_value,
.int_u64_value, .int_u64_value,
.int_i64_value, .int_i64_value,
.int_big_value, .int_big_value,
@ -322,46 +317,7 @@ pub const Key = union(enum) {
pub fn isType(key: Key) bool { pub fn isType(key: Key) bool {
return switch (key) { return switch (key) {
.simple => |simple| switch (simple) { .simple_type,
.f16,
.f32,
.f64,
.f80,
.f128,
.c_longdouble,
.usize,
.isize,
.c_short,
.c_ushort,
.c_int,
.c_uint,
.c_long,
.c_ulong,
.c_longlong,
.c_ulonglong,
.comptime_int,
.comptime_float,
.anyopaque,
.bool,
.void,
.type,
.anyerror,
.noreturn,
.@"anyframe",
.null_type,
.undefined_type,
.enum_literal_type,
=> true,
.undefined_value,
.void_value,
.unreachable_value,
.null_value,
.bool_true,
.bool_false,
=> false,
},
.int_type, .int_type,
.pointer_type, .pointer_type,
.array_type, .array_type,
@ -377,6 +333,7 @@ pub const Key = union(enum) {
.anyframe_type, .anyframe_type,
=> true, => true,
.simple_value,
.int_u64_value, .int_u64_value,
.int_i64_value, .int_i64_value,
.int_big_value, .int_big_value,
@ -403,7 +360,7 @@ pub const Key = union(enum) {
var key: Key = ty; var key: Key = ty;
while (true) switch (key) { while (true) switch (key) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.usize => return .{ .signedness = .signed, .bits = target.cpu.arch.ptrBitWidth() }, .usize => return .{ .signedness = .signed, .bits = target.cpu.arch.ptrBitWidth() },
.isize => return .{ .signedness = .unsigned, .bits = target.cpu.arch.ptrBitWidth() }, .isize => return .{ .signedness = .unsigned, .bits = target.cpu.arch.ptrBitWidth() },
@ -442,7 +399,7 @@ pub const Key = union(enum) {
/// Asserts the type is a fixed-size float or comptime_float. /// Asserts the type is a fixed-size float or comptime_float.
/// Returns 128 for comptime_float types. /// Returns 128 for comptime_float types.
pub fn floatBits(ty: Key, target: std.Target) u16 { pub fn floatBits(ty: Key, target: std.Target) u16 {
return switch (ty.simple) { return switch (ty.simple_type) {
.f16 => 16, .f16 => 16,
.f32 => 32, .f32 => 32,
.f64 => 64, .f64 => 64,
@ -489,8 +446,8 @@ pub const Key = union(enum) {
pub fn elemType2(ty: Key) Index { pub fn elemType2(ty: Key) Index {
return switch (ty) { return switch (ty) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.@"anyframe" => panicOrElse("TODO: return void type", Index.none), .@"anyframe" => Index.void,
else => unreachable, else => unreachable,
}, },
.pointer_type => |pointer_info| pointer_info.elem_type, .pointer_type => |pointer_info| pointer_info.elem_type,
@ -521,9 +478,9 @@ pub const Key = union(enum) {
}; };
} }
pub fn onePossibleValue(ty: Key, ip: InternPool) ?Key { pub fn onePossibleValue(ty: Key, ip: InternPool) Index {
switch (ty) { return switch (ty) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.f16, .f16,
.f32, .f32,
.f64, .f64,
@ -548,80 +505,67 @@ pub const Key = union(enum) {
.comptime_float, .comptime_float,
.@"anyframe", .@"anyframe",
.enum_literal_type, .enum_literal_type,
=> return null, => Index.none,
.void => return Key{ .simple = .void_value }, .void => Index.void_value,
.noreturn => return Key{ .simple = .unreachable_value }, .noreturn => Index.unreachable_value,
.null_type => return Key{ .simple = .null_value }, .null_type => Index.null_value,
.undefined_type => return Key{ .simple = .undefined_value }, .undefined_type => Index.undefined_value,
// values
.undefined_value,
.void_value,
.unreachable_value,
.null_value,
.bool_true,
.bool_false,
=> unreachable,
}, },
.int_type => |int_info| { .int_type => |int_info| {
if (int_info.bits == 0) { if (int_info.bits == 0) {
switch (int_info.signedness) { switch (int_info.signedness) {
.unsigned => return Key{ .int_u64_value = 0 }, .unsigned => return Index.zero,
.signed => return Key{ .int_i64_value = 0 }, .signed => return Index.zero, // do we need a signed zero?
} }
} }
return null; return Index.none;
}, },
.pointer_type => return null, .pointer_type => Index.none,
.array_type => |array_info| { .array_type => |array_info| {
if (array_info.len == 0) { if (array_info.len == 0) {
return panicOrElse("TODO return empty array value", null); return panicOrElse("TODO return empty array value", Index.none);
} else if (ip.indexToKey(array_info.child).onePossibleValue(ip)) |value| {
return value;
} }
return null; return ip.indexToKey(array_info.child).onePossibleValue(ip);
}, },
.struct_type => |struct_index| { .struct_type => |struct_index| {
const struct_info = ip.getStruct(struct_index); const struct_info = ip.getStruct(struct_index);
var field_it = struct_info.fields.iterator(); var field_it = struct_info.fields.iterator();
while (field_it.next()) |entry| { while (field_it.next()) |entry| {
if (entry.value_ptr.is_comptime) continue; if (entry.value_ptr.is_comptime) continue;
if (ip.indexToKey(entry.value_ptr.ty).onePossibleValue(ip) != null) continue; if (ip.indexToKey(entry.value_ptr.ty).onePossibleValue(ip) != Index.none) continue;
return null; return Index.none;
} }
return panicOrElse("TODO return empty struct value", null); return panicOrElse("TODO return empty struct value", Index.none);
}, },
.optional_type => |optional_info| { .optional_type => |optional_info| {
const child = ip.indexToKey(optional_info.payload_type); if (optional_info.payload_type == Index.noreturn) {
if (child == .simple and child.simple == .noreturn) { return Index.null_value;
return Key{ .simple = .null_value };
} }
return null; return Index.none;
}, },
.error_union_type => return null, .error_union_type => Index.none,
.error_set_type => return null, .error_set_type => Index.none,
.enum_type => |enum_index| { .enum_type => |enum_index| {
const enum_info = ip.getEnum(enum_index); const enum_info = ip.getEnum(enum_index);
switch (enum_info.fields.count()) { return switch (enum_info.fields.count()) {
0 => return Key{ .simple = .unreachable_value }, 0 => Index.unreachable_value,
1 => return ip.indexToKey(enum_info.values.keys()[0]), 1 => enum_info.values.keys()[0],
else => return null, else => Index.none,
} };
}, },
.function_type => return null, .function_type => Index.none,
.union_type => return panicOrElse("TODO", null), .union_type => panicOrElse("TODO", Index.none),
.tuple_type => return panicOrElse("TODO", null), .tuple_type => panicOrElse("TODO", Index.none),
.vector_type => |vector_info| { .vector_type => |vector_info| {
if (vector_info.len == 0) { if (vector_info.len == 0) {
return panicOrElse("TODO return empty array value", null); return panicOrElse("TODO return empty array value", Index.none);
} else if (ip.indexToKey(vector_info.child).onePossibleValue(ip)) |value| {
return value;
} }
return null; return ip.indexToKey(vector_info.child).onePossibleValue(ip);
}, },
.anyframe_type => return null, .anyframe_type => Index.none,
.simple_value,
.int_u64_value, .int_u64_value,
.int_i64_value, .int_i64_value,
.int_big_value, .int_big_value,
@ -636,7 +580,7 @@ pub const Key = union(enum) {
.aggregate, .aggregate,
.union_value, .union_value,
=> unreachable, => unreachable,
} };
} }
pub const TypeFormatContext = struct { pub const TypeFormatContext = struct {
@ -679,7 +623,7 @@ pub const Key = union(enum) {
fn printTypeInternal(ty: Key, ip: InternPool, writer: anytype) @TypeOf(writer).Error!?Index { fn printTypeInternal(ty: Key, ip: InternPool, writer: anytype) @TypeOf(writer).Error!?Index {
switch (ty) { switch (ty) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.f16, .f16,
.f32, .f32,
.f64, .f64,
@ -710,14 +654,6 @@ pub const Key = union(enum) {
.null_type => try writer.writeAll("@TypeOf(null)"), .null_type => try writer.writeAll("@TypeOf(null)"),
.undefined_type => try writer.writeAll("@TypeOf(undefined)"), .undefined_type => try writer.writeAll("@TypeOf(undefined)"),
.enum_literal_type => try writer.writeAll("@TypeOf(.enum_literal)"), .enum_literal_type => try writer.writeAll("@TypeOf(.enum_literal)"),
.undefined_value,
.void_value,
.unreachable_value,
.null_value,
.bool_true,
.bool_false,
=> unreachable,
}, },
.int_type => |int_info| switch (int_info.signedness) { .int_type => |int_info| switch (int_info.signedness) {
.signed => try writer.print("i{}", .{int_info.bits}), .signed => try writer.print("i{}", .{int_info.bits}),
@ -848,6 +784,7 @@ pub const Key = union(enum) {
return anyframe_info.child; return anyframe_info.child;
}, },
.simple_value,
.int_u64_value, .int_u64_value,
.int_i64_value, .int_i64_value,
.int_big_value, .int_big_value,
@ -884,7 +821,7 @@ pub const Key = union(enum) {
writer: anytype, writer: anytype,
) @TypeOf(writer).Error!void { ) @TypeOf(writer).Error!void {
switch (value) { switch (value) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.f16, .f16,
.f32, .f32,
.f64, .f64,
@ -915,8 +852,8 @@ pub const Key = union(enum) {
.null_type => try writer.writeAll("@TypeOf(null)"), .null_type => try writer.writeAll("@TypeOf(null)"),
.undefined_type => try writer.writeAll("@TypeOf(undefined)"), .undefined_type => try writer.writeAll("@TypeOf(undefined)"),
.enum_literal_type => try writer.writeAll("@TypeOf(.enum_literal)"), .enum_literal_type => try writer.writeAll("@TypeOf(.enum_literal)"),
},
// values .simple_value => |simple| switch (simple) {
.undefined_value => try writer.writeAll("@Type(.Undefined)"), .undefined_value => try writer.writeAll("@Type(.Undefined)"),
.void_value => try writer.writeAll("void"), .void_value => try writer.writeAll("void"),
.unreachable_value => try writer.writeAll("unreachable"), .unreachable_value => try writer.writeAll("unreachable"),
@ -1006,6 +943,44 @@ pub const Item = struct {
/// the same `InternPool`. /// the same `InternPool`.
/// TODO split this into an Optional and non-Optional Index /// TODO split this into an Optional and non-Optional Index
pub const Index = enum(u32) { pub const Index = enum(u32) {
f16,
f32,
f64,
f80,
f128,
usize,
isize,
c_short,
c_ushort,
c_int,
c_uint,
c_long,
c_ulong,
c_longlong,
c_ulonglong,
c_longdouble,
anyopaque,
bool,
void,
type,
anyerror,
comptime_int,
comptime_float,
noreturn,
@"anyframe",
null_type,
undefined_type,
enum_literal_type,
undefined_value,
void_value,
unreachable_value,
null_value,
bool_true,
bool_false,
zero,
none = std.math.maxInt(u32), none = std.math.maxInt(u32),
_, _,
@ -1031,9 +1006,12 @@ pub const NamespaceIndex = enum(u32) {
}; };
pub const Tag = enum(u8) { pub const Tag = enum(u8) {
/// A type or value that can be represented with only an enum tag. /// A type that can be represented with only an enum tag.
/// data is Simple enum value /// data is SimpleType enum value
simple, simple_type,
/// A value that can be represented with only an enum tag.
/// data is SimpleValue enum value
simple_value,
/// An integer type. /// An integer type.
/// data is number of bits /// data is number of bits
@ -1123,7 +1101,7 @@ pub const Tag = enum(u8) {
union_value, union_value,
}; };
pub const Simple = enum(u32) { pub const SimpleType = enum(u32) {
f16, f16,
f32, f32,
f64, f64,
@ -1152,57 +1130,72 @@ pub const Simple = enum(u32) {
null_type, null_type,
undefined_type, undefined_type,
enum_literal_type, enum_literal_type,
};
// values pub const SimpleValue = enum(u32) {
undefined_value, undefined_value,
void_value, void_value,
unreachable_value, unreachable_value,
null_value, null_value,
bool_true, bool_true,
bool_false, bool_false,
pub fn toType(self: Simple) Simple {
return switch (self) {
.f16,
.f32,
.f64,
.f80,
.f128,
.usize,
.isize,
.c_short,
.c_ushort,
.c_int,
.c_uint,
.c_long,
.c_ulong,
.c_longlong,
.c_ulonglong,
.c_longdouble,
.anyopaque,
.bool,
.void,
.type,
.anyerror,
.comptime_int,
.comptime_float,
.noreturn,
.@"anyframe",
.null_type,
.undefined_type,
.enum_literal_type,
=> .type,
// values
.undefined_value => .undefined_type,
.void_value => .void,
.unreachable_value => .noreturn,
.null_value => .null_type,
.bool_true => .bool,
.bool_false => .bool,
}; };
comptime {
std.debug.assert(@sizeOf(SimpleType) == @sizeOf(SimpleValue));
}
pub fn init(gpa: Allocator) Allocator.Error!InternPool {
var ip: InternPool = .{};
const simple_count = std.meta.fields(SimpleType).len + std.meta.fields(SimpleValue).len;
const count = simple_count + 1;
const extra_count = @sizeOf(u64);
try ip.map.ensureTotalCapacity(gpa, count);
try ip.items.ensureTotalCapacity(gpa, count);
try ip.extra.ensureTotalCapacity(gpa, extra_count);
_ = ip.get(undefined, .{ .simple_type = .f16 }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .f32 }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .f64 }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .f80 }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .f128 }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .usize }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .isize }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_short }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_ushort }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_int }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_uint }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_long }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_ulong }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_longlong }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_ulonglong }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .c_longdouble }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .anyopaque }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .bool }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .void }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .type }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .anyerror }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .comptime_int }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .comptime_float }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .noreturn }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .@"anyframe" }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .null_type }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .undefined_type }) catch unreachable;
_ = ip.get(undefined, .{ .simple_type = .enum_literal_type }) catch unreachable;
_ = ip.get(undefined, .{ .simple_value = .undefined_value }) catch unreachable;
_ = ip.get(undefined, .{ .simple_value = .void_value }) catch unreachable;
_ = ip.get(undefined, .{ .simple_value = .unreachable_value }) catch unreachable;
_ = ip.get(undefined, .{ .simple_value = .null_value }) catch unreachable;
_ = ip.get(undefined, .{ .simple_value = .bool_true }) catch unreachable;
_ = ip.get(undefined, .{ .simple_value = .bool_false }) catch unreachable;
_ = ip.get(undefined, .{ .int_u64_value = 0 }) catch unreachable;
return ip;
} }
};
pub fn deinit(ip: *InternPool, gpa: Allocator) void { pub fn deinit(ip: *InternPool, gpa: Allocator) void {
ip.map.deinit(gpa); ip.map.deinit(gpa);
@ -1232,7 +1225,8 @@ pub fn indexToKey(ip: InternPool, index: Index) Key {
const item = ip.items.get(@enumToInt(index)); const item = ip.items.get(@enumToInt(index));
const data = item.data; const data = item.data;
return switch (item.tag) { return switch (item.tag) {
.simple => .{ .simple = @intToEnum(Simple, data) }, .simple_type => .{ .simple_type = @intToEnum(SimpleType, data) },
.simple_value => .{ .simple_value = @intToEnum(SimpleValue, data) },
.type_int_signed => .{ .int_type = .{ .type_int_signed => .{ .int_type = .{
.signedness = .signed, .signedness = .signed,
@ -1280,7 +1274,50 @@ pub fn indexToKey(ip: InternPool, index: Index) Key {
}; };
} }
/// TODO rename to getOrPut pub fn indexToTag(ip: InternPool, index: Index) std.builtin.TypeId {
const item = ip.items.get(@enumToInt(index));
const data = item.data;
return switch (item.tag) {
.simple_type => {
const key = Key{ .simple_type = @intToEnum(SimpleType, data) };
return key.zigTypeTag();
},
.type_int_signed => .Int,
.type_int_unsigned => .Int,
.type_pointer => .Pointer,
.type_array => .Array,
.type_struct => .Struct,
.type_optional => .Optional,
.type_anyframe => .AnyFrame,
.type_error_union => .ErrorUnion,
.type_error_set => .ErrorSet,
.type_enum => .Enum,
.type_function => .Fn,
.type_union => .Union,
.type_tuple => .Struct,
.type_vector => .Vector,
.simple_value,
.int_u32,
.int_i32,
.int_u64,
.int_i64,
.int_big_positive,
.int_big_negative,
.float_f16,
.float_f32,
.float_f64,
.float_f80,
.float_f128,
=> unreachable,
.bytes => unreachable,
.aggregate => unreachable,
.union_value => unreachable,
};
}
pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
const adapter: KeyAdapter = .{ .ip = ip }; const adapter: KeyAdapter = .{ .ip = ip };
const gop = try ip.map.getOrPutAdapted(gpa, key, adapter); const gop = try ip.map.getOrPutAdapted(gpa, key, adapter);
@ -1288,7 +1325,8 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
const tag: Tag = key.tag(); const tag: Tag = key.tag();
const data: u32 = switch (key) { const data: u32 = switch (key) {
.simple => |simple| @enumToInt(simple), .simple_type => |simple| @enumToInt(simple),
.simple_value => |simple| @enumToInt(simple),
.int_type => |int_ty| int_ty.bits, .int_type => |int_ty| int_ty.bits,
.optional_type => |optional_ty| @enumToInt(optional_ty.payload_type), .optional_type => |optional_ty| @enumToInt(optional_ty.payload_type),
@ -1313,7 +1351,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
return @intToEnum(Index, ip.items.len - 1); return @intToEnum(Index, ip.items.len - 1);
} }
/// TODO rename to get
pub fn contains(ip: InternPool, key: Key) ?Index { pub fn contains(ip: InternPool, key: Key) ?Index {
const adapter: KeyAdapter = .{ .ip = &ip }; const adapter: KeyAdapter = .{ .ip = &ip };
const index = ip.map.getIndexAdapted(key, adapter) orelse return null; const index = ip.map.getIndexAdapted(key, adapter) orelse return null;
@ -1517,7 +1554,7 @@ pub fn cast(ip: *InternPool, gpa: Allocator, destination_ty: Index, source_ty: I
pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, target: std.Target) Allocator.Error!Index { pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, target: std.Target) Allocator.Error!Index {
switch (types.len) { switch (types.len) {
0 => return try ip.get(gpa, .{ .simple = .noreturn }), 0 => return Index.noreturn,
1 => return types[0], 1 => return types[0],
else => {}, else => {},
} }
@ -1554,9 +1591,9 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
} }
switch (candidate_key) { switch (candidate_key) {
.simple => |candidate_simple| switch (candidate_simple) { .simple_type => |candidate_simple| switch (candidate_simple) {
.f16, .f32, .f64, .f80, .f128 => switch (chosen_key) { .f16, .f32, .f64, .f80, .f128 => switch (chosen_key) {
.simple => |chosen_simple| switch (chosen_simple) { .simple_type => |chosen_simple| switch (chosen_simple) {
.f16, .f32, .f64, .f80, .f128 => { .f16, .f32, .f64, .f80, .f128 => {
if (chosen_key.floatBits(target) < candidate_key.floatBits(target)) { if (chosen_key.floatBits(target) < candidate_key.floatBits(target)) {
chosen = candidate; chosen = candidate;
@ -1586,7 +1623,7 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
.c_ulonglong, .c_ulonglong,
.c_longdouble, .c_longdouble,
=> switch (chosen_key) { => switch (chosen_key) {
.simple => |chosen_simple| switch (chosen_simple) { .simple_type => |chosen_simple| switch (chosen_simple) {
.usize, .usize,
.isize, .isize,
.c_short, .c_short,
@ -1629,7 +1666,7 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
.noreturn, .undefined_type => continue, .noreturn, .undefined_type => continue,
.comptime_int => switch (chosen_key) { .comptime_int => switch (chosen_key) {
.simple => |chosen_simple| switch (chosen_simple) { .simple_type => |chosen_simple| switch (chosen_simple) {
.f16, .f16,
.f32, .f32,
.f64, .f64,
@ -1656,7 +1693,7 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
else => {}, else => {},
}, },
.comptime_float => switch (chosen_key) { .comptime_float => switch (chosen_key) {
.simple => |chosen_simple| switch (chosen_simple) { .simple_type => |chosen_simple| switch (chosen_simple) {
.f16, .f32, .f64, .f80, .f128 => continue, .f16, .f32, .f64, .f80, .f128 => continue,
.comptime_int => { .comptime_int => {
chosen = candidate; chosen = candidate;
@ -1675,7 +1712,7 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
else => {}, else => {},
}, },
.int_type => |candidate_info| switch (chosen_key) { .int_type => |candidate_info| switch (chosen_key) {
.simple => |chosen_simple| switch (chosen_simple) { .simple_type => |chosen_simple| switch (chosen_simple) {
.usize, .usize,
.isize, .isize,
.c_short, .c_short,
@ -1715,7 +1752,7 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
else => {}, else => {},
}, },
.pointer_type => |candidate_info| switch (chosen_key) { .pointer_type => |candidate_info| switch (chosen_key) {
.simple => |chosen_simple| switch (chosen_simple) { .simple_type => |chosen_simple| switch (chosen_simple) {
.comptime_int => { .comptime_int => {
if (candidate_info.size == .C) { if (candidate_info.size == .C) {
chosen = candidate; chosen = candidate;
@ -1890,7 +1927,7 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
} }
switch (chosen_key) { switch (chosen_key) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.noreturn, .noreturn,
.undefined_type, .undefined_type,
=> { => {
@ -1982,7 +2019,7 @@ pub fn resolvePeerTypes(ip: *InternPool, gpa: Allocator, types: []const Index, t
if (any_are_null) { if (any_are_null) {
const opt_ty = switch (chosen_key) { const opt_ty = switch (chosen_key) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.null_type => chosen, .null_type => chosen,
else => try ip.get(gpa, .{ .optional_type = .{ .payload_type = chosen } }), else => try ip.get(gpa, .{ .optional_type = .{ .payload_type = chosen } }),
}, },
@ -2467,10 +2504,7 @@ fn coerceInMemoryAllowedFns(
} }; } };
} }
const return_type_key = ip.indexToKey(src_info.return_type); if (src_info.return_type != Index.noreturn) {
const is_noreturn = return_type_key == .simple and return_type_key.simple == .noreturn;
if (!is_noreturn) {
const rt = try ip.coerceInMemoryAllowed(gpa, arena, dest_info.return_type, src_info.return_type, true, target); const rt = try ip.coerceInMemoryAllowed(gpa, arena, dest_info.return_type, src_info.return_type, true, target);
if (rt != .ok) { if (rt != .ok) {
return InMemoryCoercionResult{ .fn_return_type = .{ return InMemoryCoercionResult{ .fn_return_type = .{
@ -2664,7 +2698,7 @@ fn optionalPtrTy(
if (child_ptr_key.is_allowzero) return Index.none; if (child_ptr_key.is_allowzero) return Index.none;
// optionals of zero sized types behave like bools, not pointers // optionals of zero sized types behave like bools, not pointers
if (child_key.onePossibleValue(ip) != null) return Index.none; if (child_key.onePossibleValue(ip) != Index.none) return Index.none;
return child_type; return child_type;
}, },
@ -2697,18 +2731,19 @@ fn testExpectFmtValue(ip: InternPool, val: Index, ty: Index, expected: []const u
test "simple types" { test "simple types" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const null_type = try ip.get(gpa, .{ .simple = .null_type }); const null_type = try ip.get(gpa, .{ .simple_type = .null_type });
const undefined_type = try ip.get(gpa, .{ .simple = .undefined_type }); const undefined_type = try ip.get(gpa, .{ .simple_type = .undefined_type });
const enum_literal_type = try ip.get(gpa, .{ .simple = .enum_literal_type }); const enum_literal_type = try ip.get(gpa, .{ .simple_type = .enum_literal_type });
const undefined_value = try ip.get(gpa, .{ .simple = .undefined_value });
const void_value = try ip.get(gpa, .{ .simple = .void_value }); const undefined_value = try ip.get(gpa, .{ .simple_value = .undefined_value });
const unreachable_value = try ip.get(gpa, .{ .simple = .unreachable_value }); const void_value = try ip.get(gpa, .{ .simple_value = .void_value });
const null_value = try ip.get(gpa, .{ .simple = .null_value }); const unreachable_value = try ip.get(gpa, .{ .simple_value = .unreachable_value });
const bool_true = try ip.get(gpa, .{ .simple = .bool_true }); const null_value = try ip.get(gpa, .{ .simple_value = .null_value });
const bool_false = try ip.get(gpa, .{ .simple = .bool_false }); const bool_true = try ip.get(gpa, .{ .simple_value = .bool_true });
const bool_false = try ip.get(gpa, .{ .simple_value = .bool_false });
try testExpectFmtType(ip, null_type, "@TypeOf(null)"); try testExpectFmtType(ip, null_type, "@TypeOf(null)");
try testExpectFmtType(ip, undefined_type, "@TypeOf(undefined)"); try testExpectFmtType(ip, undefined_type, "@TypeOf(undefined)");
@ -2725,7 +2760,7 @@ test "simple types" {
test "int type" { test "int type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
@ -2747,7 +2782,7 @@ test "int type" {
test "int value" { test "int value" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const unsigned_zero_value = try ip.get(gpa, .{ .int_u64_value = 0 }); const unsigned_zero_value = try ip.get(gpa, .{ .int_u64_value = 0 });
@ -2787,7 +2822,7 @@ test "int value" {
test "big int value" { test "big int value" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
var result = try std.math.big.int.Managed.init(gpa); var result = try std.math.big.int.Managed.init(gpa);
@ -2807,17 +2842,17 @@ test "big int value" {
test "float type" { test "float type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const f16_type = try ip.get(gpa, .{ .simple = .f16 }); const f16_type = try ip.get(gpa, .{ .simple_type = .f16 });
const f32_type = try ip.get(gpa, .{ .simple = .f32 }); const f32_type = try ip.get(gpa, .{ .simple_type = .f32 });
const f64_type = try ip.get(gpa, .{ .simple = .f64 }); const f64_type = try ip.get(gpa, .{ .simple_type = .f64 });
const f80_type = try ip.get(gpa, .{ .simple = .f80 }); const f80_type = try ip.get(gpa, .{ .simple_type = .f80 });
const f128_type = try ip.get(gpa, .{ .simple = .f128 }); const f128_type = try ip.get(gpa, .{ .simple_type = .f128 });
const another_f32_type = try ip.get(gpa, .{ .simple = .f32 }); const another_f32_type = try ip.get(gpa, .{ .simple_type = .f32 });
const another_f64_type = try ip.get(gpa, .{ .simple = .f64 }); const another_f64_type = try ip.get(gpa, .{ .simple_type = .f64 });
try std.testing.expect(f16_type != f32_type); try std.testing.expect(f16_type != f32_type);
try std.testing.expect(f32_type != f64_type); try std.testing.expect(f32_type != f64_type);
@ -2837,7 +2872,7 @@ test "float type" {
test "float value" { test "float value" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const f16_value = try ip.get(gpa, .{ .float_16_value = 0.25 }); const f16_value = try ip.get(gpa, .{ .float_16_value = 0.25 });
@ -2895,7 +2930,7 @@ test "float value" {
test "pointer type" { test "pointer type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
@ -2980,14 +3015,14 @@ test "pointer type" {
test "optional type" { test "optional type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i32_type_0 = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type_0 = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
const i32_type_1 = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type_1 = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
const null_value = try ip.get(gpa, .{ .simple = .null_value }); const null_value = try ip.get(gpa, .{ .simple_value = .null_value });
const u64_42_value = try ip.get(gpa, .{ .int_u64_value = 42 }); const u64_42_value = try ip.get(gpa, .{ .int_u64_value = 42 });
const i32_optional_type_0 = try ip.get(gpa, .{ .optional_type = .{ .payload_type = i32_type_0 } }); const i32_optional_type_0 = try ip.get(gpa, .{ .optional_type = .{ .payload_type = i32_type_0 } });
@ -3007,7 +3042,7 @@ test "optional type" {
test "error set type" { test "error set type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const foo_name = try ip.get(gpa, .{ .bytes = "foo" }); const foo_name = try ip.get(gpa, .{ .bytes = "foo" });
@ -3035,11 +3070,11 @@ test "error set type" {
test "error union type" { test "error union type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const empty_error_set = try ip.get(gpa, .{ .error_set_type = .{ .names = &.{} } }); const empty_error_set = try ip.get(gpa, .{ .error_set_type = .{ .names = &.{} } });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const @"error{}!bool" = try ip.get(gpa, .{ .error_union_type = .{ const @"error{}!bool" = try ip.get(gpa, .{ .error_union_type = .{
.error_set_type = empty_error_set, .error_set_type = empty_error_set,
@ -3052,7 +3087,7 @@ test "error union type" {
test "array type" { test "array type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i32_type_0 = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type_0 = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
@ -3084,11 +3119,11 @@ test "array type" {
test "struct value" { test "struct value" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const struct_index = try ip.createStruct(gpa, .{ const struct_index = try ip.createStruct(gpa, .{
.fields = .{}, .fields = .{},
@ -3103,7 +3138,7 @@ test "struct value" {
try struct_info.fields.put(gpa, "bar", .{ .ty = bool_type }); try struct_info.fields.put(gpa, "bar", .{ .ty = bool_type });
const one_value = try ip.get(gpa, .{ .int_i64_value = 1 }); const one_value = try ip.get(gpa, .{ .int_i64_value = 1 });
const true_value = try ip.get(gpa, .{ .simple = .bool_true }); const true_value = try ip.get(gpa, .{ .simple_value = .bool_true });
const aggregate_value = try ip.get(gpa, Key{ .aggregate = &.{ one_value, true_value } }); const aggregate_value = try ip.get(gpa, Key{ .aggregate = &.{ one_value, true_value } });
@ -3113,12 +3148,12 @@ test "struct value" {
test "function type" { test "function type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const type_type = try ip.get(gpa, .{ .simple = .type }); const type_type = try ip.get(gpa, .{ .simple_type = .type });
const @"fn(i32) bool" = try ip.get(gpa, .{ .function_type = .{ const @"fn(i32) bool" = try ip.get(gpa, .{ .function_type = .{
.args = &.{i32_type}, .args = &.{i32_type},
@ -3159,11 +3194,11 @@ test "function type" {
test "union value" { test "union value" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
const f16_type = try ip.get(gpa, .{ .simple = .f16 }); const f16_type = try ip.get(gpa, .{ .simple_type = .f16 });
const int_value = try ip.get(gpa, .{ .int_u64_value = 1 }); const int_value = try ip.get(gpa, .{ .int_u64_value = 1 });
const f16_value = try ip.get(gpa, .{ .float_16_value = 0.25 }); const f16_value = try ip.get(gpa, .{ .float_16_value = 0.25 });
@ -3196,11 +3231,11 @@ test "union value" {
test "anyframe type" { test "anyframe type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const @"anyframe->i32" = try ip.get(gpa, .{ .anyframe_type = .{ .child = i32_type } }); const @"anyframe->i32" = try ip.get(gpa, .{ .anyframe_type = .{ .child = i32_type } });
const @"anyframe->bool" = try ip.get(gpa, .{ .anyframe_type = .{ .child = bool_type } }); const @"anyframe->bool" = try ip.get(gpa, .{ .anyframe_type = .{ .child = bool_type } });
@ -3214,11 +3249,11 @@ test "anyframe type" {
test "vector type" { test "vector type" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const @"@Vector(2,u32)" = try ip.get(gpa, .{ .vector_type = .{ const @"@Vector(2,u32)" = try ip.get(gpa, .{ .vector_type = .{
.len = 2, .len = 2,
@ -3238,7 +3273,7 @@ test "vector type" {
test "bytes value" { test "bytes value" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
var str1: [43]u8 = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".*; var str1: [43]u8 = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".*;
@ -3274,7 +3309,7 @@ test "coerceInMemoryAllowed integers and floats" {
defer arena_allocator.deinit(); defer arena_allocator.deinit();
const arena = arena_allocator.allocator(); const arena = arena_allocator.allocator();
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
@ -3282,8 +3317,8 @@ test "coerceInMemoryAllowed integers and floats" {
const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } }); const i32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 32 } });
const i16_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 16 } }); const i16_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 16 } });
const f32_type = try ip.get(gpa, .{ .simple = .f32 }); const f32_type = try ip.get(gpa, .{ .simple_type = .f32 });
const f64_type = try ip.get(gpa, .{ .simple = .f64 }); const f64_type = try ip.get(gpa, .{ .simple_type = .f64 });
try std.testing.expect(try ip.coerceInMemoryAllowed(gpa, arena, u32_type, u32_type, true, builtin.target) == .ok); try std.testing.expect(try ip.coerceInMemoryAllowed(gpa, arena, u32_type, u32_type, true, builtin.target) == .ok);
try std.testing.expect(try ip.coerceInMemoryAllowed(gpa, arena, u32_type, u16_type, true, builtin.target) == .ok); try std.testing.expect(try ip.coerceInMemoryAllowed(gpa, arena, u32_type, u16_type, true, builtin.target) == .ok);
@ -3302,13 +3337,13 @@ test "coerceInMemoryAllowed integers and floats" {
test "resolvePeerTypes" { test "resolvePeerTypes" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const type_type = try ip.get(gpa, .{ .simple = .type }); const type_type = try ip.get(gpa, .{ .simple_type = .type });
const noreturn_type = try ip.get(gpa, .{ .simple = .noreturn }); const noreturn_type = try ip.get(gpa, .{ .simple_type = .noreturn });
const undefined_type = try ip.get(gpa, .{ .simple = .undefined_type }); const undefined_type = try ip.get(gpa, .{ .simple_type = .undefined_type });
try std.testing.expect(noreturn_type == try ip.resolvePeerTypes(std.testing.allocator, &.{}, builtin.target)); try std.testing.expect(noreturn_type == try ip.resolvePeerTypes(std.testing.allocator, &.{}, builtin.target));
try std.testing.expect(type_type == try ip.resolvePeerTypes(std.testing.allocator, &.{type_type}, builtin.target)); try std.testing.expect(type_type == try ip.resolvePeerTypes(std.testing.allocator, &.{type_type}, builtin.target));
@ -3324,7 +3359,7 @@ test "resolvePeerTypes" {
test "resolvePeerTypes integers and floats" { test "resolvePeerTypes integers and floats" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const i16_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 16 } }); const i16_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .signed, .bits = 16 } });
@ -3334,21 +3369,21 @@ test "resolvePeerTypes integers and floats" {
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
const u64_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 64 } }); const u64_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 64 } });
const usize_type = try ip.get(gpa, .{ .simple = .usize }); const usize_type = try ip.get(gpa, .{ .simple_type = .usize });
const isize_type = try ip.get(gpa, .{ .simple = .isize }); const isize_type = try ip.get(gpa, .{ .simple_type = .isize });
const c_short_type = try ip.get(gpa, .{ .simple = .c_short }); const c_short_type = try ip.get(gpa, .{ .simple_type = .c_short });
const c_int_type = try ip.get(gpa, .{ .simple = .c_int }); const c_int_type = try ip.get(gpa, .{ .simple_type = .c_int });
const c_long_type = try ip.get(gpa, .{ .simple = .c_long }); const c_long_type = try ip.get(gpa, .{ .simple_type = .c_long });
const comptime_int_type = try ip.get(gpa, .{ .simple = .comptime_int }); const comptime_int_type = try ip.get(gpa, .{ .simple_type = .comptime_int });
const comptime_float_type = try ip.get(gpa, .{ .simple = .comptime_float }); const comptime_float_type = try ip.get(gpa, .{ .simple_type = .comptime_float });
const f16_type = try ip.get(gpa, .{ .simple = .f16 }); const f16_type = try ip.get(gpa, .{ .simple_type = .f16 });
const f32_type = try ip.get(gpa, .{ .simple = .f32 }); const f32_type = try ip.get(gpa, .{ .simple_type = .f32 });
const f64_type = try ip.get(gpa, .{ .simple = .f64 }); const f64_type = try ip.get(gpa, .{ .simple_type = .f64 });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
try ip.testResolvePeerTypes(i16_type, i16_type, i16_type); try ip.testResolvePeerTypes(i16_type, i16_type, i16_type);
try ip.testResolvePeerTypes(i16_type, i32_type, i32_type); try ip.testResolvePeerTypes(i16_type, i32_type, i32_type);
@ -3432,12 +3467,12 @@ test "resolvePeerTypes integers and floats" {
test "resolvePeerTypes optionals" { test "resolvePeerTypes optionals" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
const null_type = try ip.get(gpa, .{ .simple = .null_type }); const null_type = try ip.get(gpa, .{ .simple_type = .null_type });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const @"?u32" = try ip.get(gpa, .{ .optional_type = .{ .payload_type = u32_type } }); const @"?u32" = try ip.get(gpa, .{ .optional_type = .{ .payload_type = u32_type } });
const @"?bool" = try ip.get(gpa, .{ .optional_type = .{ .payload_type = bool_type } }); const @"?bool" = try ip.get(gpa, .{ .optional_type = .{ .payload_type = bool_type } });
@ -3449,13 +3484,13 @@ test "resolvePeerTypes optionals" {
test "resolvePeerTypes pointers" { test "resolvePeerTypes pointers" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
const comptime_int_type = try ip.get(gpa, .{ .simple = .comptime_int }); const comptime_int_type = try ip.get(gpa, .{ .simple_type = .comptime_int });
const comptime_float_type = try ip.get(gpa, .{ .simple = .comptime_float }); const comptime_float_type = try ip.get(gpa, .{ .simple_type = .comptime_float });
const bool_type = try ip.get(gpa, .{ .simple = .bool }); const bool_type = try ip.get(gpa, .{ .simple_type = .bool });
const @"*u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .One } }); const @"*u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .One } });
const @"[*]u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .Many } }); const @"[*]u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .Many } });
@ -3547,10 +3582,10 @@ test "resolvePeerTypes pointers" {
test "resolvePeerTypes function pointers" { test "resolvePeerTypes function pointers" {
const gpa = std.testing.allocator; const gpa = std.testing.allocator;
var ip: InternPool = .{}; var ip = try InternPool.init(gpa);
defer ip.deinit(gpa); defer ip.deinit(gpa);
const void_type = try ip.get(gpa, .{ .simple = .void }); const void_type = try ip.get(gpa, .{ .simple_type = .void });
const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } }); const u32_type = try ip.get(gpa, .{ .int_type = .{ .signedness = .unsigned, .bits = 32 } });
const @"*u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .One } }); const @"*u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .One } });
const @"*const u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .One, .is_const = true } }); const @"*const u32" = try ip.get(gpa, .{ .pointer_type = .{ .elem_type = u32_type, .size = .One, .is_const = true } });

View File

@ -21,7 +21,7 @@ pub fn dotCompletions(
}; };
switch (inner_key) { switch (inner_key) {
.simple => |simple| switch (simple) { .simple_type => |simple| switch (simple) {
.type => { .type => {
const ty_key = ip.indexToKey(val); const ty_key = ip.indexToKey(val);
const namespace = ty_key.getNamespace(ip.*); const namespace = ty_key.getNamespace(ip.*);
@ -147,6 +147,7 @@ pub fn dotCompletions(
.anyframe_type, .anyframe_type,
=> {}, => {},
.simple_value,
.int_u64_value, .int_u64_value,
.int_i64_value, .int_i64_value,
.int_big_value, .int_big_value,

View File

@ -7,6 +7,7 @@ const URI = @import("uri.zig");
const log = std.log.scoped(.analysis); const log = std.log.scoped(.analysis);
const ast = @import("ast.zig"); const ast = @import("ast.zig");
const ComptimeInterpreter = @import("ComptimeInterpreter.zig"); const ComptimeInterpreter = @import("ComptimeInterpreter.zig");
const InternPool = ComptimeInterpreter.InternPool;
var using_trail: std.ArrayList([*]const u8) = undefined; var using_trail: std.ArrayList([*]const u8) = undefined;
var resolve_trail: std.ArrayList(NodeWithHandle) = undefined; var resolve_trail: std.ArrayList(NodeWithHandle) = undefined;
@ -765,14 +766,13 @@ pub fn resolveTypeOfNodeInternal(store: *DocumentStore, arena: *std.heap.ArenaAl
log.info("Invoking interpreter!", .{}); log.info("Invoking interpreter!", .{});
store.ensureInterpreterExists(handle.uri) catch |err| { const interpreter = store.ensureInterpreterExists(handle.uri) catch |err| {
log.err("Failed to interpret file: {s}", .{@errorName(err)}); log.err("Failed to interpret file: {s}", .{@errorName(err)});
if (@errorReturnTrace()) |trace| { if (@errorReturnTrace()) |trace| {
std.debug.dumpStackTrace(trace.*); std.debug.dumpStackTrace(trace.*);
} }
return null; return null;
}; };
var interpreter: *ComptimeInterpreter = handle.interpreter.?;
const root_namespace = @intToEnum(ComptimeInterpreter.Namespace.Index, 0); const root_namespace = @intToEnum(ComptimeInterpreter.Namespace.Index, 0);
@ -792,16 +792,13 @@ pub fn resolveTypeOfNodeInternal(store: *DocumentStore, arena: *std.heap.ArenaAl
return null; return null;
}; };
const type_type = try interpreter.ip.get(interpreter.allocator, ComptimeInterpreter.Key{ .simple = .type });
const is_type_val = value.ty == type_type;
return TypeWithHandle{ return TypeWithHandle{
.type = .{ .type = .{
.data = .{ .@"comptime" = .{ .data = .{ .@"comptime" = .{
.interpreter = interpreter, .interpreter = interpreter,
.value = value, .value = value,
} }, } },
.is_type_val = is_type_val, .is_type_val = value.ty == InternPool.Index.type,
}, },
.handle = node_handle.handle, .handle = node_handle.handle,
}; };

View File

@ -14,32 +14,32 @@ const offsets = zls.offsets;
const allocator: std.mem.Allocator = std.testing.allocator; const allocator: std.mem.Allocator = std.testing.allocator;
test "ComptimeInterpreter - primitive types" { test "ComptimeInterpreter - primitive types" {
try testExpr("true", .{ .simple = .bool }, .{ .simple = .bool_true }); try testExpr("true", .{ .simple_type = .bool }, .{ .simple_value = .bool_true });
try testExpr("false", .{ .simple = .bool }, .{ .simple = .bool_false }); try testExpr("false", .{ .simple_type = .bool }, .{ .simple_value = .bool_false });
try testExpr("5", .{ .simple = .comptime_int }, .{ .int_u64_value = 5 }); try testExpr("5", .{ .simple_type = .comptime_int }, .{ .int_u64_value = 5 });
// TODO try testExpr("-2", .{ .simple = .comptime_int }, .{ .int_i64_value = -2 }); // TODO try testExpr("-2", .{ .simple_type = .comptime_int }, .{ .int_i64_value = -2 });
try testExpr("3.0", .{ .simple = .comptime_float }, null); try testExpr("3.0", .{ .simple_type = .comptime_float }, null);
try testExpr("null", .{ .simple = .null_type }, .{ .simple = .null_value }); try testExpr("null", .{ .simple_type = .null_type }, .{ .simple_value = .null_value });
try testExpr("void", .{ .simple = .type }, .{ .simple = .void }); try testExpr("void", .{ .simple_type = .type }, .{ .simple_type = .void });
try testExpr("undefined", .{ .simple = .undefined_type }, .{ .simple = .undefined_value }); try testExpr("undefined", .{ .simple_type = .undefined_type }, .{ .simple_value = .undefined_value });
try testExpr("noreturn", .{ .simple = .type }, .{ .simple = .noreturn }); try testExpr("noreturn", .{ .simple_type = .type }, .{ .simple_type = .noreturn });
} }
test "ComptimeInterpreter - expressions" { test "ComptimeInterpreter - expressions" {
if (true) return error.SkipZigTest; // TODO if (true) return error.SkipZigTest; // TODO
try testExpr("5 + 3", .{ .simple = .comptime_int }, .{ .int_u64_value = 8 }); try testExpr("5 + 3", .{ .simple_type = .comptime_int }, .{ .int_u64_value = 8 });
try testExpr("5.2 + 4.2", .{ .simple = .comptime_float }, null); try testExpr("5.2 + 4.2", .{ .simple_type = .comptime_float }, null);
try testExpr("3 == 3", .{ .simple = .bool }, .{ .simple = .bool_true }); try testExpr("3 == 3", .{ .simple_type = .bool }, .{ .simple_valueclear = .bool_true });
try testExpr("5.2 == 2.1", .{ .simple = .bool }, .{ .simple = .bool_false }); try testExpr("5.2 == 2.1", .{ .simple_type = .bool }, .{ .simple_value = .bool_false });
try testExpr("@as(?bool, null) orelse true", .{ .simple = .bool }, .{ .simple = .bool_true }); try testExpr("@as(?bool, null) orelse true", .{ .simple_type = .bool }, .{ .simple_value = .bool_true });
} }
test "ComptimeInterpreter - builtins" { test "ComptimeInterpreter - builtins" {
if (true) return error.SkipZigTest; // TODO if (true) return error.SkipZigTest; // TODO
try testExpr("@as(bool, true)", .{ .simple = .bool }, .{ .simple = .bool_true }); try testExpr("@as(bool, true)", .{ .simple_type = .bool }, .{ .simple_value = .bool_true });
try testExpr("@as(u32, 3)", .{ .int_type = .{ try testExpr("@as(u32, 3)", .{ .int_type = .{
.signedness = .unsigned, .signedness = .unsigned,
.bits = 32, .bits = 32,
@ -64,12 +64,12 @@ test "ComptimeInterpreter - labeled block" {
\\blk: { \\blk: {
\\ break :blk true; \\ break :blk true;
\\} \\}
, .{ .simple = .bool }, .{ .simple = .bool_true }); , .{ .simple_type = .bool }, .{ .simple_value = .bool_true });
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ break :blk 3; \\ break :blk 3;
\\} \\}
, .{ .simple = .comptime_int }, .{ .int_u64_value = 3 }); , .{ .simple_type = .comptime_int }, .{ .int_u64_value = 3 });
} }
test "ComptimeInterpreter - if" { test "ComptimeInterpreter - if" {
@ -77,18 +77,18 @@ test "ComptimeInterpreter - if" {
\\blk: { \\blk: {
\\ break :blk if (true) true else false; \\ break :blk if (true) true else false;
\\} \\}
, .{ .simple = .bool }, .{ .simple = .bool_true }); , .{ .simple_type = .bool }, .{ .simple_value = .bool_true });
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ break :blk if (false) true else false; \\ break :blk if (false) true else false;
\\} \\}
, .{ .simple = .bool }, .{ .simple = .bool_false }); , .{ .simple_type = .bool }, .{ .simple_value = .bool_false });
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ if (false) break :blk true; \\ if (false) break :blk true;
\\ break :blk false; \\ break :blk false;
\\} \\}
, .{ .simple = .bool }, .{ .simple = .bool_false }); , .{ .simple_type = .bool }, .{ .simple_value = .bool_false });
// TODO // TODO
// try testExpr( // try testExpr(
// \\outer: { // \\outer: {
@ -97,7 +97,7 @@ test "ComptimeInterpreter - if" {
// \\ }) break :outer true; // \\ }) break :outer true;
// \\ break :outer false; // \\ break :outer false;
// \\} // \\}
// , .{ .simple = .bool }, .{ .simple = .bool_true }); // , .{ .simple_type = .bool }, .{ .simple_value = .bool_true });
} }
test "ComptimeInterpreter - variable lookup" { test "ComptimeInterpreter - variable lookup" {
@ -106,7 +106,7 @@ test "ComptimeInterpreter - variable lookup" {
\\ var foo = 42; \\ var foo = 42;
\\ break :blk foo; \\ break :blk foo;
\\} \\}
, .{ .simple = .comptime_int }, .{ .int_u64_value = 42 }); , .{ .simple_type = .comptime_int }, .{ .int_u64_value = 42 });
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ var foo = 1; \\ var foo = 1;
@ -114,7 +114,7 @@ test "ComptimeInterpreter - variable lookup" {
\\ var baz = 3; \\ var baz = 3;
\\ break :blk bar; \\ break :blk bar;
\\} \\}
, .{ .simple = .comptime_int }, .{ .int_u64_value = 2 }); , .{ .simple_type = .comptime_int }, .{ .int_u64_value = 2 });
var context = try Context.init( var context = try Context.init(
\\const bar = foo; \\const bar = foo;
@ -123,7 +123,7 @@ test "ComptimeInterpreter - variable lookup" {
defer context.deinit(); defer context.deinit();
const result = try context.interpret(context.findVar("bar")); const result = try context.interpret(context.findVar("bar"));
try expectEqualKey(context.interpreter.ip, .{ .simple = .comptime_int }, result.ty); try expectEqualKey(context.interpreter.ip, .{ .simple_type = .comptime_int }, result.ty);
try expectEqualKey(context.interpreter.ip, .{ .int_u64_value = 3 }, result.val); try expectEqualKey(context.interpreter.ip, .{ .int_u64_value = 3 }, result.val);
} }
@ -133,7 +133,7 @@ test "ComptimeInterpreter - field access" {
\\ const foo: struct {alpha: u64, beta: bool} = undefined; \\ const foo: struct {alpha: u64, beta: bool} = undefined;
\\ break :blk foo.beta; \\ break :blk foo.beta;
\\} \\}
, .{ .simple = .bool }, null); , .{ .simple_type = .bool }, null);
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ const foo: struct {alpha: u64, beta: bool} = undefined; \\ const foo: struct {alpha: u64, beta: bool} = undefined;
@ -152,13 +152,13 @@ test "ComptimeInterpreter - optional operations" {
\\ const foo: ?bool = true; \\ const foo: ?bool = true;
\\ break :blk foo.?; \\ break :blk foo.?;
\\} \\}
, .{ .simple = .bool }, .{ .simple = .bool_true }); , .{ .simple_type = .bool }, .{ .simple_value = .bool_true });
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ const foo: ?bool = true; \\ const foo: ?bool = true;
\\ break :blk foo == null; \\ break :blk foo == null;
\\} \\}
, .{ .simple = .bool }, .{ .simple = .bool_false }); , .{ .simple_type = .bool }, .{ .simple_value = .bool_false });
} }
test "ComptimeInterpreter - pointer operations" { test "ComptimeInterpreter - pointer operations" {
@ -168,20 +168,20 @@ test "ComptimeInterpreter - pointer operations" {
\\ const foo: []const u8 = ""; \\ const foo: []const u8 = "";
\\ break :blk foo.len; \\ break :blk foo.len;
\\} \\}
, .{ .simple = .usize }, .{ .bytes = "" }); , .{ .simple_type = .usize }, .{ .bytes = "" });
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ const foo = true; \\ const foo = true;
\\ break :blk &foo; \\ break :blk &foo;
\\} \\}
, @panic("TODO"), .{ .simple = .bool_true }); , @panic("TODO"), .{ .simple_value = .bool_true });
try testExpr( try testExpr(
\\blk: { \\blk: {
\\ const foo = true; \\ const foo = true;
\\ const bar = &foo; \\ const bar = &foo;
\\ break :blk bar.*; \\ break :blk bar.*;
\\} \\}
, @panic("TODO"), .{ .simple = .bool_true }); , @panic("TODO"), .{ .simple_value = .bool_true });
} }
test "ComptimeInterpreter - call return primitive type" { test "ComptimeInterpreter - call return primitive type" {
@ -189,7 +189,7 @@ test "ComptimeInterpreter - call return primitive type" {
\\pub fn Foo() type { \\pub fn Foo() type {
\\ return bool; \\ return bool;
\\} \\}
, &.{}, .{ .simple = .bool }); , &.{}, .{ .simple_type = .bool });
try testCall( try testCall(
\\pub fn Foo() type { \\pub fn Foo() type {
@ -223,17 +223,15 @@ test "ComptimeInterpreter - call return struct" {
defer context.deinit(); defer context.deinit();
const result = try context.call(context.findFn("Foo"), &.{}); const result = try context.call(context.findFn("Foo"), &.{});
try std.testing.expect(result.ty == .simple); try std.testing.expect(result.ty == .simple_type);
try std.testing.expect(result.ty.simple == .type); try std.testing.expect(result.ty.simple_type == .type);
const struct_info = context.interpreter.ip.getStruct(result.val.?.struct_type); const struct_info = context.interpreter.ip.getStruct(result.val.?.struct_type);
try std.testing.expectEqual(Index.none, struct_info.backing_int_ty); try std.testing.expectEqual(Index.none, struct_info.backing_int_ty);
try std.testing.expectEqual(std.builtin.Type.ContainerLayout.Auto, struct_info.layout); try std.testing.expectEqual(std.builtin.Type.ContainerLayout.Auto, struct_info.layout);
const bool_type = try context.interpreter.ip.get(allocator, .{ .simple = .bool });
try std.testing.expectEqual(@as(usize, 1), struct_info.fields.count()); try std.testing.expectEqual(@as(usize, 1), struct_info.fields.count());
try std.testing.expectEqualStrings("slay", struct_info.fields.keys()[0]); try std.testing.expectEqualStrings("slay", struct_info.fields.keys()[0]);
try std.testing.expect(struct_info.fields.values()[0].ty == bool_type); try std.testing.expect(struct_info.fields.values()[0].ty == Index.bool);
} }
test "ComptimeInterpreter - call comptime argument" { test "ComptimeInterpreter - call comptime argument" {
@ -247,19 +245,19 @@ test "ComptimeInterpreter - call comptime argument" {
defer context.deinit(); defer context.deinit();
const result1 = try context.call(context.findFn("Foo"), &.{KV{ const result1 = try context.call(context.findFn("Foo"), &.{KV{
.ty = .{ .simple = .bool }, .ty = .{ .simple_type = .bool },
.val = .{ .simple = .bool_true }, .val = .{ .simple_value = .bool_true },
}}); }});
try std.testing.expect(result1.ty == .simple); try std.testing.expect(result1.ty == .simple_type);
try std.testing.expect(result1.ty.simple == .type); try std.testing.expect(result1.ty.simple_type == .type);
try std.testing.expectEqual(Key{ .int_type = .{ .signedness = .unsigned, .bits = 8 } }, result1.val.?); try std.testing.expectEqual(Key{ .int_type = .{ .signedness = .unsigned, .bits = 8 } }, result1.val.?);
var result2 = try context.call(context.findFn("Foo"), &.{KV{ var result2 = try context.call(context.findFn("Foo"), &.{KV{
.ty = .{ .simple = .bool }, .ty = .{ .simple_type = .bool },
.val = .{ .simple = .bool_false }, .val = .{ .simple_value = .bool_false },
}}); }});
try std.testing.expect(result2.ty == .simple); try std.testing.expect(result2.ty == .simple_type);
try std.testing.expect(result2.ty.simple == .type); try std.testing.expect(result2.ty.simple_type == .type);
try std.testing.expectEqual(Key{ .int_type = .{ .signedness = .unsigned, .bits = 69 } }, result2.val.?); try std.testing.expectEqual(Key{ .int_type = .{ .signedness = .unsigned, .bits = 69 } }, result2.val.?);
} }
@ -303,6 +301,7 @@ const Context = struct {
interpreter.* = .{ interpreter.* = .{
.allocator = allocator, .allocator = allocator,
.ip = try InternPool.init(allocator),
.document_store = document_store, .document_store = document_store,
.uri = handle.uri, .uri = handle.uri,
}; };
@ -399,7 +398,7 @@ fn testCall(
const result = try context.call(context.findFn("Foo"), arguments); const result = try context.call(context.findFn("Foo"), arguments);
try expectEqualKey(context.interpreter.ip, Key{ .simple = .type }, result.ty); try expectEqualKey(context.interpreter.ip, Key{ .simple_type = .type }, result.ty);
try expectEqualKey(context.interpreter.ip, expected_ty, result.val); try expectEqualKey(context.interpreter.ip, expected_ty, result.val);
} }