This commit is contained in:
David Gonzalez Martin 2025-04-19 20:47:54 -06:00
parent c7c5b509f2
commit 63f2cd5a7c
3 changed files with 65 additions and 1 deletions

View File

@ -207,6 +207,7 @@ pub const ResolvedType = struct {
pub const Enumerator = struct {
fields: []const Enumerator.Field,
string_to_enum: ?StringToEnum = null,
enum_to_string: ?*llvm.Function = null,
backing_type: *Type,
line: u32,
implicit_backing_type: bool,
@ -813,6 +814,7 @@ pub const Value = struct {
const Intrinsic = union(Id) {
byte_size: *Type,
enum_name: *Value,
extend: *Value,
integer_max: *Type,
int_from_enum: *Value,
@ -829,6 +831,7 @@ pub const Value = struct {
const Id = enum {
byte_size,
enum_name,
extend,
integer_max,
int_from_enum,
@ -2944,6 +2947,20 @@ pub const Module = struct {
},
};
},
.enum_name => blk: {
module.skip_space();
module.expect_character(left_parenthesis);
module.skip_space();
const arg_value = module.parse_value(function, scope, .{});
module.expect_character(right_parenthesis);
break :blk .{
.bb = .{
.intrinsic = .{
.enum_name = arg_value,
},
},
};
},
.extend => blk: {
module.skip_space();
module.expect_character(left_parenthesis);
@ -5483,6 +5500,20 @@ pub const Module = struct {
break :blk expected_type;
},
.enum_name => |enum_value| blk: {
const string_type = module.get_slice_type(.{ .type = module.integer_type(8, false) });
module.typecheck(analysis, string_type);
module.analyze_value_type(function, enum_value, .{});
if (enum_value.type.?.bb != .enumerator) {
module.report_error();
}
// TODO: share globals for enum names between string to enum and enum to string functions
if (enum_value.type.?.bb.enumerator.enum_to_string == null) {
@trap();
}
break :blk string_type;
},
.extend => |extended_value| blk: {
const expected_type = analysis.type orelse module.report_error();
module.analyze_value_type(function, extended_value, .{});

View File

@ -277,7 +277,7 @@ arena_join_string = fn (arena: &Arena, pieces: [][]u8) []u8
assert(i == size);
pointer[i] = 0;
return pointer[0..size];
return pointer[..size];
}
GlobalState = struct
@ -325,6 +325,8 @@ CompileFile = struct
silent: u1,
}
base_cache_dir = "bb-cache";
compile_file = fn (arena: &Arena, compile: CompileFile) void
{
>relative_file_path = compile.relative_file_path;
@ -354,6 +356,13 @@ compile_file = fn (arena: &Arena, compile: CompileFile) void
>base_name = relative_file_path[base_start..extension_start];
>is_compiler = string_equal(relative_file_path, "src/compiler.bbb");
>outputh_path_dir = arena_join_string(arena, [
base_cache_dir,
#select(is_compiler, "/compiler/", "/"),
#enum_name(compile.build_mode),
"_",
#select(compile.has_debug_info, "di", "nodi"),
][..]);
}
[export] main = fn [cc(c)] (argument_count: u32, argv: &&u8) s32

24
tests/enum_name.bbb Normal file
View File

@ -0,0 +1,24 @@
E = enum
{
my_expected_result,
a,
b,
}
[extern] memcmp = fn [cc(c)] (a: &u8, b: &u8, byte_count: u64) s32;
string_equal = fn (slice_a: []u8, slice_b: []u8) u1
{
>result = slice_a.length == slice_b.length;
if (result)
{
result = memcmp(slice_a.pointer, slice_b.pointer, slice_a.length) == 0;
}
return result;
}
[export] main = fn [cc(c)] () s32
{
>some_enum: E = .my_expected_result;
return #extend(!string_equal(#enum_name(some_enum), "my_expected_result"));
}