mirror of
https://github.com/odin-lang/Odin.git
synced 2026-06-08 11:34:20 +00:00
Integer Enumerations
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
#load "file.odin"
|
||||
|
||||
print_string :: proc(s: string) {
|
||||
file_write(file_get_standard(FILE_STANDARD_OUTPUT), ^s[0], len(s));
|
||||
file_write(file_get_standard(FileStandard.OUTPUT), ^s[0], len(s));
|
||||
}
|
||||
|
||||
byte_reverse :: proc(b: []byte) {
|
||||
|
||||
@@ -42,7 +42,7 @@ variables :: proc() {
|
||||
x, y: int = 1, 2;
|
||||
|
||||
// Type inference
|
||||
apple, banana, carrot := true, 123, "carrot";
|
||||
apple, banana, 世界 := true, 123, "world";
|
||||
|
||||
|
||||
// Basic Types of the Language
|
||||
@@ -73,7 +73,7 @@ variables :: proc() {
|
||||
// untyped rune - rune/i32
|
||||
|
||||
|
||||
// // Zero values
|
||||
// Zero values
|
||||
zero_numeric := 0;
|
||||
zero_boolean := false;
|
||||
zero_pointer := null;
|
||||
@@ -324,7 +324,8 @@ types :: proc() {
|
||||
return y;
|
||||
}
|
||||
|
||||
// transmute only works if the size of the types are equal
|
||||
// NOTE(bill): transmute only works if the size of the types are equal
|
||||
|
||||
/*
|
||||
// in C
|
||||
union {
|
||||
@@ -334,6 +335,32 @@ types :: proc() {
|
||||
*/
|
||||
}
|
||||
|
||||
{ // Enumeration
|
||||
Thing :: type enum {
|
||||
APPLE,
|
||||
FROG,
|
||||
TREE,
|
||||
TOMB,
|
||||
}
|
||||
a := Thing.APPLE;
|
||||
|
||||
Sized :: type enum u64 {
|
||||
APPLE,
|
||||
FROG,
|
||||
TREE,
|
||||
TOMB,
|
||||
}
|
||||
static_assert(size_of(Sized) == size_of(u64));
|
||||
|
||||
Certain :: type enum {
|
||||
APPLE = 3,
|
||||
FROG,
|
||||
TREE = 7,
|
||||
TOMB,
|
||||
}
|
||||
static_assert(Certain.TOMB == 8);
|
||||
}
|
||||
|
||||
|
||||
|
||||
{ // Compound Literals
|
||||
|
||||
@@ -9,10 +9,9 @@ File :: type struct {
|
||||
file_open :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte;
|
||||
_ = copy(buf[:], name as []byte);
|
||||
handle := CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null);
|
||||
|
||||
f: File;
|
||||
f.handle = handle as FileHandle;
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_READ, FILE_SHARE_READ, null, OPEN_EXISTING, 0, null),
|
||||
};
|
||||
success := f.handle != INVALID_HANDLE_VALUE as FileHandle;
|
||||
return f, success;
|
||||
}
|
||||
@@ -20,10 +19,9 @@ file_open :: proc(name: string) -> (File, bool) {
|
||||
file_create :: proc(name: string) -> (File, bool) {
|
||||
buf: [300]byte;
|
||||
_ = copy(buf[:], name as []byte);
|
||||
handle := CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null);
|
||||
|
||||
f: File;
|
||||
f.handle = handle as FileHandle;
|
||||
f := File{
|
||||
handle = CreateFileA(^buf[0], FILE_GENERIC_WRITE, FILE_SHARE_READ, null, CREATE_ALWAYS, 0, null),
|
||||
};
|
||||
success := f.handle != INVALID_HANDLE_VALUE as FileHandle;
|
||||
return f, success;
|
||||
}
|
||||
@@ -38,23 +36,24 @@ file_write :: proc(f: ^File, buf: rawptr, len: int) -> bool {
|
||||
return WriteFile(f.handle, buf, len as i32, ^bytes_written, null) != 0;
|
||||
}
|
||||
|
||||
FileStandardType :: type int;
|
||||
FILE_STANDARD_INPUT : FileStandardType : 0;
|
||||
FILE_STANDARD_OUTPUT : FileStandardType : 1;
|
||||
FILE_STANDARD_ERROR : FileStandardType : 2;
|
||||
FILE_STANDARD__COUNT : FileStandardType : 3;
|
||||
FileStandard :: type enum {
|
||||
INPUT,
|
||||
OUTPUT,
|
||||
ERROR,
|
||||
COUNT,
|
||||
}
|
||||
|
||||
__std_file_set := false;
|
||||
__std_files: [FILE_STANDARD__COUNT]File;
|
||||
__std_files: [FileStandard.COUNT as int]File;
|
||||
|
||||
file_get_standard :: proc(std: FileStandardType) -> ^File {
|
||||
file_get_standard :: proc(std: FileStandard) -> ^File {
|
||||
if (!__std_file_set) {
|
||||
__std_files[FILE_STANDARD_INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE);
|
||||
__std_files[FILE_STANDARD_OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
__std_files[FILE_STANDARD_ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
__std_files[FileStandard.INPUT] .handle = GetStdHandle(STD_INPUT_HANDLE);
|
||||
__std_files[FileStandard.OUTPUT].handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
__std_files[FileStandard.ERROR] .handle = GetStdHandle(STD_ERROR_HANDLE);
|
||||
__std_file_set = true;
|
||||
}
|
||||
return ^__std_files[std as int];
|
||||
return ^__std_files[std];
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ win32_print_last_error :: proc() {
|
||||
|
||||
// Yuk!
|
||||
to_c_string :: proc(s: string) -> ^u8 {
|
||||
c_str: ^u8 = heap_alloc(len(s)+1);
|
||||
c_str: ^u8 = alloc(len(s)+1);
|
||||
memory_copy(c_str, ^s[0], len(s));
|
||||
c_str[len(s)] = 0;
|
||||
return c_str;
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#load "win32.odin"
|
||||
|
||||
debug_trap :: proc() #foreign "llvm.debugtrap"
|
||||
|
||||
// TODO(bill): make custom heap procedures
|
||||
heap_alloc :: proc(len: int) -> rawptr {
|
||||
heap_alloc :: proc(len: int) -> rawptr {
|
||||
return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
|
||||
}
|
||||
heap_free :: proc(ptr: rawptr) {
|
||||
@@ -9,7 +11,6 @@ heap_free :: proc(ptr: rawptr) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
memory_compare :: proc(dst, src: rawptr, len: int) -> int {
|
||||
s1, s2: ^u8 = dst, src;
|
||||
for i := 0; i < len; i++ {
|
||||
@@ -226,6 +227,7 @@ __string_cmp :: proc(a, b : string) -> int {
|
||||
return +1;
|
||||
}
|
||||
}
|
||||
|
||||
if len(a) < len(b) {
|
||||
return -1;
|
||||
} else if len(a) > len(b) {
|
||||
@@ -240,11 +242,15 @@ __string_le :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) <
|
||||
__string_ge :: proc(a, b : string) -> bool #inline { return __string_cmp(a, b) >= 0; }
|
||||
|
||||
|
||||
AllocationMode :: type int;
|
||||
ALLOCATION_ALLOC :: 0;
|
||||
ALLOCATION_DEALLOC :: 1;
|
||||
ALLOCATION_DEALLOC_ALL :: 2;
|
||||
ALLOCATION_RESIZE :: 3;
|
||||
|
||||
|
||||
AllocationMode :: type enum {
|
||||
ALLOC,
|
||||
DEALLOC,
|
||||
DEALLOC_ALL,
|
||||
RESIZE,
|
||||
}
|
||||
|
||||
|
||||
|
||||
AllocatorProc :: type proc(allocator_data: rawptr, mode: AllocationMode,
|
||||
@@ -272,11 +278,14 @@ DEFAULT_ALIGNMENT :: 2*size_of(int);
|
||||
|
||||
|
||||
__check_context :: proc() {
|
||||
static_assert(AllocationMode.ALLOC == 0);
|
||||
static_assert(AllocationMode.DEALLOC == 1);
|
||||
static_assert(AllocationMode.DEALLOC_ALL == 2);
|
||||
static_assert(AllocationMode.RESIZE == 3);
|
||||
|
||||
if context.allocator.procedure == null {
|
||||
context.allocator = __default_allocator();
|
||||
}
|
||||
|
||||
ptr := __check_context as rawptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -285,18 +294,18 @@ alloc :: proc(size: int) -> rawptr #inline { return alloc_align(size, DEFAULT_AL
|
||||
alloc_align :: proc(size, alignment: int) -> rawptr #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, ALLOCATION_ALLOC, size, alignment, null, 0, 0);
|
||||
return a.procedure(a.data, AllocationMode.ALLOC, size, alignment, null, 0, 0);
|
||||
}
|
||||
|
||||
dealloc :: proc(ptr: rawptr) #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
_ = a.procedure(a.data, ALLOCATION_DEALLOC, 0, 0, ptr, 0, 0);
|
||||
_ = a.procedure(a.data, AllocationMode.DEALLOC, 0, 0, ptr, 0, 0);
|
||||
}
|
||||
dealloc_all :: proc(ptr: rawptr) #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
_ = a.procedure(a.data, ALLOCATION_DEALLOC_ALL, 0, 0, ptr, 0, 0);
|
||||
_ = a.procedure(a.data, AllocationMode.DEALLOC_ALL, 0, 0, ptr, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -304,7 +313,7 @@ resize :: proc(ptr: rawptr, old_size, new_size: int) -> rawptr #inline { r
|
||||
resize_align :: proc(ptr: rawptr, old_size, new_size, alignment: int) -> rawptr #inline {
|
||||
__check_context();
|
||||
a := context.allocator;
|
||||
return a.procedure(a.data, ALLOCATION_RESIZE, new_size, alignment, ptr, old_size, 0);
|
||||
return a.procedure(a.data, AllocationMode.RESIZE, new_size, alignment, ptr, old_size, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -340,13 +349,13 @@ default_resize_align :: proc(old_memory: rawptr, old_size, new_size, alignment:
|
||||
__default_allocator_proc :: proc(allocator_data: rawptr, mode: AllocationMode,
|
||||
size, alignment: int,
|
||||
old_memory: rawptr, old_size: int, flags: u64) -> rawptr {
|
||||
if mode == ALLOCATION_ALLOC {
|
||||
if mode == AllocationMode.ALLOC {
|
||||
return heap_alloc(size);
|
||||
} else if mode == ALLOCATION_RESIZE {
|
||||
} else if mode == AllocationMode.RESIZE {
|
||||
return default_resize_align(old_memory, old_size, size, alignment);
|
||||
} else if mode == ALLOCATION_DEALLOC {
|
||||
} else if mode == AllocationMode.DEALLOC {
|
||||
heap_free(old_memory);
|
||||
} else if mode == ALLOCATION_DEALLOC_ALL {
|
||||
} else if mode == AllocationMode.DEALLOC_ALL {
|
||||
// NOTE(bill): Does nothing
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,73 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) {
|
||||
struct_type->structure.is_packed = st->is_packed;
|
||||
}
|
||||
|
||||
void check_enum_type(Checker *c, Type *enum_type, AstNode *node) {
|
||||
GB_ASSERT(node->kind == AstNode_EnumType);
|
||||
GB_ASSERT(enum_type->kind == Type_Enumeration);
|
||||
ast_node(et, EnumType, node);
|
||||
|
||||
Map<Entity *> entity_map = {};
|
||||
map_init(&entity_map, gb_heap_allocator());
|
||||
defer (map_destroy(&entity_map));
|
||||
|
||||
Type *base_type = t_int;
|
||||
if (et->base_type != NULL) {
|
||||
base_type = check_type(c, et->base_type);
|
||||
}
|
||||
|
||||
if (base_type == NULL || !is_type_integer(base_type)) {
|
||||
error(&c->error_collector, et->token, "Base type for enumeration must be an integer");
|
||||
return;
|
||||
} else
|
||||
if (base_type == NULL) {
|
||||
base_type = t_int;
|
||||
}
|
||||
enum_type->enumeration.base = base_type;
|
||||
|
||||
Entity **fields = gb_alloc_array(c->allocator, Entity *, et->field_count);
|
||||
isize field_index = 0;
|
||||
ExactValue iota = make_exact_value_integer(-1);
|
||||
for (AstNode *field = et->field_list; field != NULL; field = field->next) {
|
||||
ast_node(f, FieldValue, field);
|
||||
Token name_token = f->field->Ident.token;
|
||||
|
||||
Operand o = {};
|
||||
if (f->value != NULL) {
|
||||
check_expr(c, &o, f->value);
|
||||
if (o.mode != Addressing_Constant) {
|
||||
error(&c->error_collector, ast_node_token(f->value), "Enumeration value must be a constant integer");
|
||||
o.mode = Addressing_Invalid;
|
||||
}
|
||||
if (o.mode != Addressing_Invalid) {
|
||||
check_assignment(c, &o, base_type, make_string("enumeration"));
|
||||
}
|
||||
if (o.mode != Addressing_Invalid) {
|
||||
iota = o.value;
|
||||
} else {
|
||||
Token add_token = {Token_Add};
|
||||
iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
|
||||
}
|
||||
} else {
|
||||
Token add_token = {Token_Add};
|
||||
iota = exact_binary_operator_value(add_token, iota, make_exact_value_integer(1));
|
||||
}
|
||||
|
||||
Entity *e = make_entity_constant(c->allocator, c->context.scope, name_token, enum_type, iota);
|
||||
|
||||
HashKey key = hash_string(name_token.string);
|
||||
if (map_get(&entity_map, key)) {
|
||||
// TODO(bill): Scope checking already checks the declaration
|
||||
error(&c->error_collector, name_token, "`%.*s` is already declared in this enumeration", LIT(name_token.string));
|
||||
} else {
|
||||
map_set(&entity_map, key, e);
|
||||
fields[field_index++] = e;
|
||||
}
|
||||
add_entity_use(&c->info, f->field, e);
|
||||
}
|
||||
enum_type->enumeration.fields = fields;
|
||||
enum_type->enumeration.field_count = et->field_count;
|
||||
}
|
||||
|
||||
Type *check_get_params(Checker *c, Scope *scope, AstNode *field_list, isize field_count) {
|
||||
if (field_list == NULL || field_count == 0)
|
||||
return NULL;
|
||||
@@ -399,6 +466,13 @@ Type *check_type(Checker *c, AstNode *e, Type *named_type) {
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
case_ast_node(et, EnumType, e);
|
||||
type = make_type_enumeration(c->allocator);
|
||||
set_base_type(named_type, type);
|
||||
check_enum_type(c, type, e);
|
||||
goto end;
|
||||
case_end;
|
||||
|
||||
case_ast_node(pt, PointerType, e);
|
||||
type = make_type_pointer(c->allocator, check_type(c, pt->type));
|
||||
set_base_type(named_type, type);
|
||||
@@ -584,6 +658,7 @@ b32 check_value_is_expressible(Checker *c, ExactValue in_value, Type *type, Exac
|
||||
if (out_value) *out_value = in_value;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -821,8 +896,8 @@ b32 check_castable_to(Checker *c, Operand *operand, Type *y) {
|
||||
return true;
|
||||
|
||||
Type *x = operand->type;
|
||||
Type *xb = get_base_type(x);
|
||||
Type *yb = get_base_type(y);
|
||||
Type *xb = get_enum_base_type(get_base_type(x));
|
||||
Type *yb = get_enum_base_type(get_base_type(y));
|
||||
if (are_types_identical(xb, yb))
|
||||
return true;
|
||||
|
||||
@@ -870,13 +945,12 @@ b32 check_castable_to(Checker *c, Operand *operand, Type *y) {
|
||||
}
|
||||
|
||||
// proc <-> proc
|
||||
if (is_type_proc(xb), is_type_proc(yb)) {
|
||||
if (is_type_proc(xb) && is_type_proc(yb)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// proc -> rawptr
|
||||
if (is_type_proc(xb), is_type_rawptr(yb)) {
|
||||
if (is_type_proc(xb) && is_type_rawptr(yb)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -903,6 +977,7 @@ void check_binary_expr(Checker *c, Operand *x, AstNode *node) {
|
||||
|
||||
Type *base_type = get_base_type(type);
|
||||
if (is_const_expr && is_type_constant_type(base_type)) {
|
||||
|
||||
if (base_type->kind == Type_Basic) {
|
||||
if (check_value_is_expressible(c, x->value, base_type, &x->value)) {
|
||||
can_convert = true;
|
||||
@@ -1167,7 +1242,7 @@ void convert_to_typed(Checker *c, Operand *operand, Type *target_type) {
|
||||
return;
|
||||
}
|
||||
|
||||
Type *t = get_base_type(target_type);
|
||||
Type *t = get_enum_base_type(get_base_type(target_type));
|
||||
switch (t->kind) {
|
||||
case Type_Basic:
|
||||
if (operand->mode == Addressing_Constant) {
|
||||
@@ -1239,7 +1314,7 @@ b32 check_index_value(Checker *c, AstNode *index_value, i64 max_count, i64 *valu
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_type_integer(operand.type)) {
|
||||
if (!is_type_integer(get_enum_base_type(operand.type))) {
|
||||
gbString expr_str = expr_to_string(operand.expr);
|
||||
error(&c->error_collector, ast_node_token(operand.expr),
|
||||
"Index `%s` must be an integer", expr_str);
|
||||
@@ -1300,6 +1375,18 @@ Entity *lookup_field(Type *type, AstNode *field_node, isize *index = NULL) {
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Type_Enumeration:
|
||||
for (isize i = 0; i < type->enumeration.field_count; i++) {
|
||||
Entity *f = type->enumeration.fields[i];
|
||||
GB_ASSERT(f->kind == Entity_Constant);
|
||||
String str = f->token.string;
|
||||
if (are_strings_equal(field_str, str)) {
|
||||
if (index) *index = i;
|
||||
return f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
break;
|
||||
// TODO(bill): Other types and extra "hidden" fields (e.g. introspection stuff)
|
||||
// TODO(bill): Allow for access of field through index? e.g. `x.3` will get member of index 3
|
||||
// Or is this only suitable if tuples are first-class?
|
||||
@@ -1328,10 +1415,17 @@ void check_selector(Checker *c, Operand *operand, AstNode *node) {
|
||||
}
|
||||
add_entity_use(&c->info, selector, entity);
|
||||
|
||||
operand->type = entity->type;
|
||||
operand->expr = node;
|
||||
if (operand->mode != Addressing_Variable)
|
||||
operand->mode = Addressing_Value;
|
||||
if (is_type_enum(operand->type)) {
|
||||
operand->type = entity->type;
|
||||
operand->expr = node;
|
||||
operand->mode = Addressing_Constant;
|
||||
operand->value = entity->Constant.value;
|
||||
} else {
|
||||
operand->type = entity->type;
|
||||
operand->expr = node;
|
||||
if (operand->mode != Addressing_Variable)
|
||||
operand->mode = Addressing_Value;
|
||||
}
|
||||
} else {
|
||||
operand->mode = Addressing_Invalid;
|
||||
operand->expr = node;
|
||||
@@ -2046,7 +2140,7 @@ ExpressionKind check__expr_base(Checker *c, Operand *o, AstNode *node, Type *typ
|
||||
} break;
|
||||
|
||||
default: {
|
||||
gbString str = type_to_string(t);
|
||||
gbString str = type_to_string(type);
|
||||
error(&c->error_collector, ast_node_token(node), "Invalid compound literal type `%s`", str);
|
||||
gb_string_free(str);
|
||||
goto error;
|
||||
|
||||
@@ -62,6 +62,7 @@ struct BasicType {
|
||||
TYPE_KIND(Vector), \
|
||||
TYPE_KIND(Slice), \
|
||||
TYPE_KIND(Structure), \
|
||||
TYPE_KIND(Enumeration), \
|
||||
TYPE_KIND(Pointer), \
|
||||
TYPE_KIND(Named), \
|
||||
TYPE_KIND(Tuple), \
|
||||
@@ -126,6 +127,11 @@ struct Type {
|
||||
isize param_count;
|
||||
isize result_count;
|
||||
} proc;
|
||||
struct {
|
||||
Type * base; // Default is `int`
|
||||
Entity **fields; // Entity_Constant
|
||||
isize field_count;
|
||||
} enumeration;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -181,6 +187,11 @@ Type *make_type_structure(gbAllocator a) {
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_enumeration(gbAllocator a) {
|
||||
Type *t = alloc_type(a, Type_Enumeration);
|
||||
return t;
|
||||
}
|
||||
|
||||
Type *make_type_pointer(gbAllocator a, Type *elem) {
|
||||
Type *t = alloc_type(a, Type_Pointer);
|
||||
t->pointer.elem = elem;
|
||||
@@ -396,6 +407,17 @@ Type *base_vector_type(Type *t) {
|
||||
}
|
||||
return t;
|
||||
}
|
||||
b32 is_type_enum(Type *t) {
|
||||
t = get_base_type(t);
|
||||
return t->kind == Type_Enumeration;
|
||||
}
|
||||
Type *get_enum_base_type(Type *t) {
|
||||
Type *bt = get_base_type(t);
|
||||
if (is_type_enum(bt)) {
|
||||
return bt->enumeration.base;
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -417,6 +439,8 @@ b32 is_type_comparable(Type *t) {
|
||||
return is_type_comparable(t->array.elem);
|
||||
case Type_Vector:
|
||||
return is_type_comparable(t->vector.elem);
|
||||
case Type_Enumeration:
|
||||
return is_type_comparable(t->enumeration.base);
|
||||
case Type_Proc:
|
||||
return true;
|
||||
}
|
||||
@@ -581,6 +605,9 @@ i64 type_align_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
return max;
|
||||
}
|
||||
} break;
|
||||
|
||||
case Type_Enumeration:
|
||||
return type_align_of(s, allocator, t->enumeration.base);
|
||||
}
|
||||
|
||||
return gb_clamp(next_pow2(type_size_of(s, allocator, t)), 1, s.max_align);
|
||||
@@ -671,6 +698,9 @@ i64 type_size_of(BaseTypeSizes s, gbAllocator allocator, Type *t) {
|
||||
type_set_offsets(s, allocator, t);
|
||||
return t->structure.offsets[count-1] + type_size_of(s, allocator, t->structure.fields[count-1]->type);
|
||||
} break;
|
||||
|
||||
case Type_Enumeration:
|
||||
return type_size_of(s, allocator, t->enumeration.base);
|
||||
}
|
||||
|
||||
// Catch all
|
||||
@@ -727,6 +757,11 @@ gbString write_type_to_string(gbString str, Type *type) {
|
||||
str = gb_string_appendc(str, "}");
|
||||
} break;
|
||||
|
||||
case Type_Enumeration: {
|
||||
str = gb_string_appendc(str, "enum ");
|
||||
str = write_type_to_string(str, type->enumeration.base);
|
||||
} break;
|
||||
|
||||
case Type_Pointer:
|
||||
str = gb_string_appendc(str, "^");
|
||||
str = write_type_to_string(str, type->pointer.elem);
|
||||
|
||||
@@ -156,6 +156,9 @@ void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
ssa_fprintf(f, ">");
|
||||
}
|
||||
|
||||
break;
|
||||
case Type_Enumeration:
|
||||
ssa_print_type(f, s, t->enumeration.base);
|
||||
break;
|
||||
case Type_Pointer:
|
||||
ssa_print_type(f, s, t->pointer.elem);
|
||||
|
||||
@@ -1237,10 +1237,11 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
|
||||
}
|
||||
|
||||
|
||||
Type *src = get_base_type(src_type);
|
||||
Type *dst = get_base_type(t);
|
||||
if (are_types_identical(t, src_type))
|
||||
Type *src = get_enum_base_type(get_base_type(src_type));
|
||||
Type *dst = get_enum_base_type(get_base_type(t));
|
||||
if (are_types_identical(src, dst)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (value->kind == ssaValue_Constant) {
|
||||
if (dst->kind == Type_Basic) {
|
||||
@@ -1383,7 +1384,6 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
gb_printf_err("Not Identical %s != %s\n", type_to_string(src_type), type_to_string(t));
|
||||
gb_printf_err("Not Identical %s != %s\n", type_to_string(src), type_to_string(dst));
|
||||
|
||||
|
||||
@@ -218,6 +218,12 @@ AST_NODE_KIND(_TypeBegin, struct{}) \
|
||||
isize field_count; \
|
||||
b32 is_packed; \
|
||||
}) \
|
||||
AST_NODE_KIND(EnumType, struct { \
|
||||
Token token; \
|
||||
AstNode *base_type; \
|
||||
AstNode *field_list; \
|
||||
isize field_count; \
|
||||
}) \
|
||||
AST_NODE_KIND(_TypeEnd, struct{}) \
|
||||
AST_NODE_KIND(Count, struct{})
|
||||
|
||||
@@ -361,6 +367,8 @@ Token ast_node_token(AstNode *node) {
|
||||
return node->VectorType.token;
|
||||
case AstNode_StructType:
|
||||
return node->StructType.token;
|
||||
case AstNode_EnumType:
|
||||
return node->EnumType.token;
|
||||
}
|
||||
|
||||
return empty_token;
|
||||
@@ -763,6 +771,16 @@ gb_inline AstNode *make_struct_type(AstFile *f, Token token, AstNode *field_list
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
gb_inline AstNode *make_enum_type(AstFile *f, Token token, AstNode *base_type, AstNode *field_list, isize field_count) {
|
||||
AstNode *result = make_node(f, AstNode_EnumType);
|
||||
result->EnumType.token = token;
|
||||
result->EnumType.base_type = base_type;
|
||||
result->EnumType.field_list = field_list;
|
||||
result->EnumType.field_count = field_count;
|
||||
return result;
|
||||
}
|
||||
|
||||
gb_inline AstNode *make_type_decl(AstFile *f, Token token, AstNode *name, AstNode *type) {
|
||||
AstNode *result = make_node(f, AstNode_TypeDecl);
|
||||
result->TypeDecl.token = token;
|
||||
@@ -1616,6 +1634,43 @@ AstNode *parse_identifier_or_type(AstFile *f) {
|
||||
return make_struct_type(f, token, params, param_count, is_packed);
|
||||
}
|
||||
|
||||
case Token_enum: {
|
||||
Token token = expect_token(f, Token_enum);
|
||||
AstNode *base_type = NULL;
|
||||
Token open, close;
|
||||
|
||||
if (f->cursor[0].kind != Token_OpenBrace) {
|
||||
base_type = parse_type(f);
|
||||
}
|
||||
|
||||
AstNode *root = NULL;
|
||||
AstNode *curr = NULL;
|
||||
isize field_count = 0;
|
||||
|
||||
open = expect_token(f, Token_OpenBrace);
|
||||
|
||||
while (f->cursor[0].kind != Token_CloseBrace &&
|
||||
f->cursor[0].kind != Token_EOF) {
|
||||
AstNode *name = parse_identifier(f);
|
||||
AstNode *value = NULL;
|
||||
Token eq = empty_token;
|
||||
if (f->cursor[0].kind == Token_Eq) {
|
||||
eq = expect_token(f, Token_Eq);
|
||||
value = parse_value(f);
|
||||
}
|
||||
AstNode *field = make_field_value(f, name, value, eq);
|
||||
DLIST_APPEND(root, curr, field);
|
||||
field_count++;
|
||||
if (f->cursor[0].kind != Token_Comma)
|
||||
break;
|
||||
next_token(f);
|
||||
}
|
||||
|
||||
close = expect_token(f, Token_CloseBrace);
|
||||
|
||||
return make_enum_type(f, token, base_type, root, field_count);
|
||||
}
|
||||
|
||||
case Token_proc:
|
||||
return parse_proc_type(f, NULL);
|
||||
|
||||
@@ -1763,9 +1818,6 @@ AstNode *parse_decl(AstFile *f, AstNode *name_list, isize name_count) {
|
||||
}
|
||||
|
||||
AstNode *type = parse_type(f);
|
||||
// if (type->kind != AstNode_StructType) {
|
||||
// expect_token(f, Token_Semicolon);
|
||||
// }
|
||||
return make_type_decl(f, token, name_list, type);
|
||||
} else if (f->cursor[0].kind == Token_proc &&
|
||||
declaration_kind == Declaration_Immutable) {
|
||||
@@ -1980,6 +2032,7 @@ AstNode *parse_stmt(AstFile *f) {
|
||||
if (s->kind != AstNode_ProcDecl &&
|
||||
(s->kind == AstNode_TypeDecl &&
|
||||
s->TypeDecl.type->kind != AstNode_StructType &&
|
||||
s->TypeDecl.type->kind != AstNode_EnumType &&
|
||||
s->TypeDecl.type->kind != AstNode_ProcType) &&
|
||||
!allow_token(f, Token_Semicolon)) {
|
||||
// CLEANUP(bill): Semicolon handling in parser
|
||||
|
||||
Reference in New Issue
Block a user