From cc6282a6e3463dd2b0192789fbd373a6d8f59a3d Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Tue, 2 May 2017 21:16:09 +0100 Subject: [PATCH] Fix alignment and size bug of enums; Remove #ordered and make the default #ordered. --- code/demo.odin | 1 - core/_preload.odin | 14 +++++++------- core/os_linux.odin | 4 ++-- core/os_x.odin | 4 ++-- core/raw.odin | 10 +++++----- core/sys/wgl.odin | 6 +++--- core/sys/windows.odin | 30 +++++++++++++++--------------- src/check_expr.c | 2 +- src/parser.c | 10 +++++----- src/types.c | 5 +++++ 10 files changed, 45 insertions(+), 41 deletions(-) diff --git a/code/demo.odin b/code/demo.odin index 657e66b59..323754a23 100644 --- a/code/demo.odin +++ b/code/demo.odin @@ -1,5 +1,4 @@ #import "fmt.odin"; -#import "math.odin"; main :: proc() { immutable program := "+ + * - /"; diff --git a/core/_preload.odin b/core/_preload.odin index 90ebcde56..ebdf55702 100644 --- a/core/_preload.odin +++ b/core/_preload.odin @@ -25,7 +25,7 @@ Calling_Convention :: enum { FAST = 3, } -Type_Info_Record :: struct #ordered { +Type_Info_Record :: struct { types: []^Type_Info, names: []string, offsets: []int, // offsets may not be used in tuples @@ -144,13 +144,13 @@ Allocator_Mode :: enum u8 { Allocator_Proc :: #type proc(allocator_data: rawptr, mode: Allocator_Mode, size, alignment: int, old_memory: rawptr, old_size: int, flags: u64) -> rawptr; -Allocator :: struct #ordered { +Allocator :: struct { procedure: Allocator_Proc, data: rawptr, } -Context :: struct #ordered { +Context :: struct { thread_id: int, allocator: Allocator, @@ -559,18 +559,18 @@ __default_hash_string :: proc(s: string) -> u64 { __INITIAL_MAP_CAP :: 16; -__Map_Key :: struct #ordered { +__Map_Key :: struct { hash: u64, str: string, } -__Map_Find_Result :: struct #ordered { +__Map_Find_Result :: struct { hash_index: int, entry_prev: int, entry_index: int, } -__Map_Entry_Header :: struct #ordered { +__Map_Entry_Header :: struct { key: __Map_Key, next: int, /* @@ -578,7 +578,7 @@ __Map_Entry_Header :: struct #ordered { */ } -__Map_Header :: struct #ordered { +__Map_Header :: struct { m: ^raw.Dynamic_Map, is_key_string: bool, entry_size: int, diff --git a/core/os_linux.odin b/core/os_linux.odin index 0ac376d69..6654ab6a1 100644 --- a/core/os_linux.odin +++ b/core/os_linux.odin @@ -36,7 +36,7 @@ RTLD_GLOBAL :: 0x100; // "Argv" arguments converted to Odin strings immutable args := _alloc_command_line_arguments(); -_File_Time :: struct #ordered { +_File_Time :: struct { seconds: i64, nanoseconds: i32, reserved: i32, @@ -46,7 +46,7 @@ _File_Time :: struct #ordered { // https://android.googlesource.com/platform/prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.7-4.6/+/jb-dev/sysroot/usr/include/bits/stat.h // Validity is not guaranteed. -Stat :: struct #ordered { +Stat :: struct { device_id: u64, // ID of device containing file serial: u64, // File serial number nlink: u32, // Number of hard links diff --git a/core/os_x.odin b/core/os_x.odin index 9fb138ae5..0e3b7d115 100644 --- a/core/os_x.odin +++ b/core/os_x.odin @@ -41,12 +41,12 @@ RTLD_FIRST :: 0x100; args: [dynamic]string; -FileTime :: struct #ordered { +FileTime :: struct { seconds: i64, nanoseconds: i64 } -Stat :: struct #ordered { +Stat :: struct { device_id : i32, // ID of device containing file mode : u16, // Mode of the file nlink : u16, // Number of hard links diff --git a/core/raw.odin b/core/raw.odin index 0ad7af7b8..82937ad41 100644 --- a/core/raw.odin +++ b/core/raw.odin @@ -1,27 +1,27 @@ -Any :: struct #ordered { +Any :: struct { data: rawptr, type_info: ^Type_Info, } -String :: struct #ordered { +String :: struct { data: ^byte, len: int, }; -Slice :: struct #ordered { +Slice :: struct { data: rawptr, len: int, cap: int, }; -Dynamic_Array :: struct #ordered { +Dynamic_Array :: struct { data: rawptr, len: int, cap: int, allocator: Allocator, }; -Dynamic_Map :: struct #ordered { +Dynamic_Map :: struct { hashes: [dynamic]int, entries: Dynamic_Array, }; diff --git a/core/sys/wgl.odin b/core/sys/wgl.odin index 4cca783bc..0d8dce7fe 100644 --- a/core/sys/wgl.odin +++ b/core/sys/wgl.odin @@ -12,7 +12,7 @@ CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB :: 0x00000002; Hglrc :: Handle; Color_Ref :: u32; -Layer_Plane_Descriptor :: struct #ordered { +Layer_Plane_Descriptor :: struct { size: u16, version: u16, flags: u32, @@ -39,11 +39,11 @@ Layer_Plane_Descriptor :: struct #ordered { transparent: Color_Ref, } -Point_Float :: struct #ordered { +Point_Float :: struct { x, y: f32, } -Glyph_Metrics_Float :: struct #ordered { +Glyph_Metrics_Float :: struct { black_box_x: f32, black_box_y: f32, glyph_origin: Point_Float, diff --git a/core/sys/windows.odin b/core/sys/windows.odin index 80466570a..53e27252c 100644 --- a/core/sys/windows.odin +++ b/core/sys/windows.odin @@ -65,11 +65,11 @@ SM_CYSCREEN :: 1; SW_SHOW :: 5; -Point :: struct #ordered { +Point :: struct { x, y: i32, } -WndClassExA :: struct #ordered { +WndClassExA :: struct { size, style: u32, wnd_proc: Wnd_Proc, cls_extra, wnd_extra: i32, @@ -81,7 +81,7 @@ WndClassExA :: struct #ordered { sm: Hicon, } -Msg :: struct #ordered { +Msg :: struct { hwnd: Hwnd, message: u32, wparam: Wparam, @@ -90,24 +90,24 @@ Msg :: struct #ordered { pt: Point, } -Rect :: struct #ordered { +Rect :: struct { left: i32, top: i32, right: i32, bottom: i32, } -Filetime :: struct #ordered { +Filetime :: struct { lo, hi: u32, } -Systemtime :: struct #ordered { +Systemtime :: struct { year, month: u16, day_of_week, day: u16, hour, minute, second, millisecond: u16, } -By_Handle_File_Information :: struct #ordered { +By_Handle_File_Information :: struct { file_attributes: u32, creation_time, last_access_time, @@ -120,7 +120,7 @@ By_Handle_File_Information :: struct #ordered { file_index_low: u32, } -File_Attribute_Data :: struct #ordered { +File_Attribute_Data :: struct { file_attributes: u32, creation_time, last_access_time, @@ -275,7 +275,7 @@ HEAP_ZERO_MEMORY :: 0x00000008; // Synchronization -Security_Attributes :: struct #ordered { +Security_Attributes :: struct { length: u32, security_descriptor: rawptr, inherit_handle: Bool, @@ -326,14 +326,14 @@ SWP_NOSIZE :: 0x0001; SWP_NOMOVE :: 0x0002; -Monitor_Info :: struct #ordered { +Monitor_Info :: struct { size: u32, monitor: Rect, work: Rect, flags: u32, } -Window_Placement :: struct #ordered { +Window_Placement :: struct { length: u32, flags: u32, show_cmd: u32, @@ -370,7 +370,7 @@ LOWORD :: proc(lParam: Lparam) -> u16 { return u16(lParam); } -Bitmap_Info_Header :: struct #ordered { +Bitmap_Info_Header :: struct { size: u32, width, height: i32, planes, bit_count: i16, @@ -381,13 +381,13 @@ Bitmap_Info_Header :: struct #ordered { clr_used: u32, clr_important: u32, } -Bitmap_Info :: struct #ordered { +Bitmap_Info :: struct { using header: Bitmap_Info_Header, colors: [1]Rgb_Quad, } -Rgb_Quad :: struct #ordered { blue, green, red, reserved: byte } +Rgb_Quad :: struct { blue, green, red, reserved: byte } BI_RGB :: 0; DIB_RGB_COLORS :: 0x00; @@ -433,7 +433,7 @@ PFD_DOUBLEBUFFER_DONTCARE :: 0x40000000; PFD_STEREO_DONTCARE :: 0x80000000; -PIXELFORMATDESCRIPTOR :: struct #ordered { +PIXELFORMATDESCRIPTOR :: struct { size, version, flags: u32, diff --git a/src/check_expr.c b/src/check_expr.c index 74cda433b..eef6d1483 100644 --- a/src/check_expr.c +++ b/src/check_expr.c @@ -534,7 +534,7 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node) { isize field_count = 0; for_array(field_index, st->fields) { - AstNode *field = st->fields.e[field_index]; + AstNode *field = st->fields.e[field_index]; switch (field->kind) { case_ast_node(f, Field, field); field_count += f->names.count; diff --git a/src/parser.c b/src/parser.c index cd8f65652..efa4acb83 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2745,11 +2745,11 @@ AstNode *parse_type_or_ident(AstFile *f) { syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string)); } is_packed = true; - } else if (str_eq(tag.string, str_lit("ordered"))) { - if (is_ordered) { - syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string)); - } - is_ordered = true; + // } else if (str_eq(tag.string, str_lit("ordered"))) { + // if (is_ordered) { + // syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string)); + // } + // is_ordered = true; } else if (str_eq(tag.string, str_lit("align"))) { if (align) { syntax_error(tag, "Duplicate struct tag `#%.*s`", LIT(tag.string)); diff --git a/src/types.c b/src/types.c index 5c60f19b7..711a74456 100644 --- a/src/types.c +++ b/src/types.c @@ -1595,6 +1595,8 @@ i64 type_align_of_internal(gbAllocator allocator, Type *t, TypePath *path) { case Type_Record: { switch (t->Record.kind) { + case TypeRecord_Enum: + return type_align_of_internal(allocator, t->Record.enum_base_type, path); case TypeRecord_Struct: if (t->Record.custom_align > 0) { return gb_clamp(t->Record.custom_align, 1, build_context.max_align); @@ -1844,6 +1846,9 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) { case Type_Record: { switch (t->Record.kind) { + case TypeRecord_Enum: + return type_size_of_internal(allocator, t->Record.enum_base_type, path); + case TypeRecord_Struct: { i64 count = t->Record.field_count; if (count == 0) {