From 5796c413571140798d9ca597cda4d34c6762eabe Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Mon, 6 Feb 2017 22:53:48 +0000 Subject: `map` immutable fields: count, capacity, allocator --- src/types.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/types.c') diff --git a/src/types.c b/src/types.c index 0402b2470..be28c82f0 100644 --- a/src/types.c +++ b/src/types.c @@ -1073,6 +1073,10 @@ gb_global Entity *entity__dynamic_array_count = NULL; gb_global Entity *entity__dynamic_array_capacity = NULL; gb_global Entity *entity__dynamic_array_allocator = NULL; +gb_global Entity *entity__dynamic_map_count = NULL; +gb_global Entity *entity__dynamic_map_capacity = NULL; +gb_global Entity *entity__dynamic_map_allocator = NULL; + Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_name, bool is_type, Selection sel); Selection lookup_field(gbAllocator a, Type *type_, String field_name, bool is_type) { @@ -1240,7 +1244,7 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n sel.entity = entity__slice_count; return sel; } - } else if (type->kind == Type_DynamicArray) { + } else if (type->kind == Type_DynamicArray) { String data_str = str_lit("data"); String count_str = str_lit("count"); String capacity_str = str_lit("capacity"); @@ -1273,6 +1277,36 @@ Selection lookup_field_with_selection(gbAllocator a, Type *type_, String field_n sel.entity = entity__dynamic_array_allocator; return sel; } + } else if (type->kind == Type_Map) { + String count_str = str_lit("count"); + String capacity_str = str_lit("capacity"); + String allocator_str = str_lit("allocator"); + + if (str_eq(field_name, count_str)) { + selection_add_index(&sel, 0); + if (entity__dynamic_map_count == NULL) { + entity__dynamic_map_count = make_entity_field(a, NULL, make_token_ident(count_str), t_int, false, 0); + entity__dynamic_map_count->Variable.is_immutable = true; + } + sel.entity = entity__dynamic_map_count; + return sel; + } else if (str_eq(field_name, capacity_str)) { + selection_add_index(&sel, 1); + if (entity__dynamic_map_capacity == NULL) { + entity__dynamic_map_capacity = make_entity_field(a, NULL, make_token_ident(capacity_str), t_int, false, 1); + entity__dynamic_map_capacity->Variable.is_immutable = true; + } + sel.entity = entity__dynamic_map_capacity; + return sel; + } else if (str_eq(field_name, allocator_str)) { + selection_add_index(&sel, 2); + if (entity__dynamic_map_allocator == NULL) { + entity__dynamic_map_allocator = make_entity_field(a, NULL, make_token_ident(allocator_str), t_allocator, false, 2); + entity__dynamic_map_allocator->Variable.is_immutable = true; + } + sel.entity = entity__dynamic_map_allocator; + return sel; + } } if (type->kind != Type_Record) { -- cgit v1.2.3 From 219ca0ac4677235d595d9bd6e1be08eedfdf7d66 Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Tue, 7 Feb 2017 00:10:58 +0000 Subject: Map type info and fmt printing --- code/demo.odin | 6 ++--- core/_preload.odin | 74 +++++++++++++++++++++++++++++++++++------------------- core/fmt.odin | 49 ++++++++++++++++++++++++++++++++++-- src/check_expr.c | 16 ++++++------ src/checker.c | 14 ++++++++--- src/ir.c | 24 +++++++++++++++--- src/types.c | 8 +++--- 7 files changed, 139 insertions(+), 52 deletions(-) (limited to 'src/types.c') diff --git a/code/demo.odin b/code/demo.odin index d91d1d5c7..45ae781a2 100644 --- a/code/demo.odin +++ b/code/demo.odin @@ -12,7 +12,7 @@ main :: proc() { { - m := map[f32]int{}; + m: map[f32]int; reserve(^m, 16); defer free(m); @@ -39,9 +39,7 @@ main :: proc() { _, ok := m["c"]; assert(ok && c == 7654); - for val, key in m { - fmt.printf("m[\"%s\"] == %v\n", key, val); - } + fmt.println(m); } diff --git a/core/_preload.odin b/core/_preload.odin index ec473348e..1eb8a2d06 100644 --- a/core/_preload.odin +++ b/core/_preload.odin @@ -20,11 +20,12 @@ Type_Info_Member :: struct #ordered { offset: int, // offsets are not used in tuples } Type_Info_Record :: struct #ordered { - fields: []Type_Info_Member, - size: int, // in bytes - align: int, // in bytes - packed: bool, - ordered: bool, + fields: []Type_Info_Member, + size: int, // in bytes + align: int, // in bytes + packed: bool, + ordered: bool, + custom_align: bool, } Type_Info_Enum_Value :: raw_union { f: f64, @@ -90,10 +91,16 @@ Type_Info :: union { Union: Type_Info_Record, Raw_Union: Type_Info_Record, Enum: struct #ordered { - base: ^Type_Info, - names: []string, + base: ^Type_Info, + names: []string, values: []Type_Info_Enum_Value, }, + Map: struct #ordered { + key: ^Type_Info, + value: ^Type_Info, + generated_struct: ^Type_Info, + count: int, // == 0 if dynamic + }, } // // NOTE(bill): only the ones that are needed (not all types) @@ -113,6 +120,21 @@ type_info_base :: proc(info: ^Type_Info) -> ^Type_Info { } +type_info_base_without_enum :: proc(info: ^Type_Info) -> ^Type_Info { + if info == nil { + return nil; + } + base := info; + match type i in base { + case Type_Info.Named: + base = i.base; + case Type_Info.Enum: + base = i.base; + } + return base; +} + + assume :: proc(cond: bool) #foreign __llvm_core "llvm.assume"; @@ -444,26 +466,26 @@ __default_hash_string :: proc(s: string) -> u64 { return __default_hash(cast([]byte)s); } -Map_Key :: struct #ordered { +__Map_Key :: struct #ordered { hash: u64, str: string, } -Map_Find_Result :: struct #ordered { +__Map_Find_Result :: struct #ordered { hash_index: int, entry_prev: int, entry_index: int, } -Map_Entry_Header :: struct #ordered { - key: Map_Key, +__Map_Entry_Header :: struct #ordered { + key: __Map_Key, next: int, /* value: Value_Type, */ } -Map_Header :: struct #ordered { +__Map_Header :: struct #ordered { m: ^Raw_Dynamic_Map, is_key_string: bool, entry_size: int, @@ -471,13 +493,13 @@ Map_Header :: struct #ordered { value_offset: int, } -__dynamic_map_reserve :: proc(using header: Map_Header, capacity: int) -> bool { +__dynamic_map_reserve :: proc(using header: __Map_Header, capacity: int) -> bool { h := __dynamic_array_reserve(^m.hashes, size_of(int), align_of(int), capacity); e := __dynamic_array_reserve(^m.entries, entry_size, entry_align, capacity); return h && e; } -__dynamic_map_rehash :: proc(using header: Map_Header, new_count: int) { +__dynamic_map_rehash :: proc(using header: __Map_Header, new_count: int) { new_header := header; nm: Raw_Dynamic_Map; new_header.m = ^nm; @@ -519,7 +541,7 @@ __dynamic_map_rehash :: proc(using header: Map_Header, new_count: int) { header.m^ = nm; } -__dynamic_map_get :: proc(h: Map_Header, key: Map_Key) -> rawptr { +__dynamic_map_get :: proc(h: __Map_Header, key: __Map_Key) -> rawptr { index := __dynamic_map_find(h, key).entry_index; if index >= 0 { data := cast(^byte)__dynamic_map_get_entry(h, index); @@ -529,7 +551,7 @@ __dynamic_map_get :: proc(h: Map_Header, key: Map_Key) -> rawptr { return nil; } -__dynamic_map_set :: proc(using h: Map_Header, key: Map_Key, value: rawptr) { +__dynamic_map_set :: proc(using h: __Map_Header, key: __Map_Key, value: rawptr) { index: int; if m.hashes.count == 0 { @@ -559,17 +581,17 @@ __dynamic_map_set :: proc(using h: Map_Header, key: Map_Key, value: rawptr) { } -__dynamic_map_grow :: proc(using h: Map_Header) { +__dynamic_map_grow :: proc(using h: __Map_Header) { new_count := 2*m.entries.count + 8; __dynamic_map_rehash(h, new_count); } -__dynamic_map_full :: proc(using h: Map_Header) -> bool { +__dynamic_map_full :: proc(using h: __Map_Header) -> bool { return cast(int)(0.75 * cast(f64)m.hashes.count) <= m.entries.count; } -__dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Key) -> bool { +__dynamic_map_hash_equal :: proc(h: __Map_Header, a, b: __Map_Key) -> bool { if a.hash == b.hash { if h.is_key_string { return a.str == b.str; @@ -579,8 +601,8 @@ __dynamic_map_hash_equal :: proc(h: Map_Header, a, b: Map_Key) -> bool { return false; } -__dynamic_map_find :: proc(using h: Map_Header, key: Map_Key) -> Map_Find_Result { - fr := Map_Find_Result{-1, -1, -1}; +__dynamic_map_find :: proc(using h: __Map_Header, key: __Map_Key) -> __Map_Find_Result { + fr := __Map_Find_Result{-1, -1, -1}; if m.hashes.count > 0 { fr.hash_index = cast(int)(key.hash % cast(u64)m.hashes.count); fr.entry_index = m.hashes[fr.hash_index]; @@ -596,7 +618,7 @@ __dynamic_map_find :: proc(using h: Map_Header, key: Map_Key) -> Map_Find_Result return fr; } -__dynamic_map_add_entry :: proc(using h: Map_Header, key: Map_Key) -> int { +__dynamic_map_add_entry :: proc(using h: __Map_Header, key: __Map_Key) -> int { prev := m.entries.count; c := __dynamic_array_append_nothing(^m.entries, entry_size, entry_align); if c != prev { @@ -608,19 +630,19 @@ __dynamic_map_add_entry :: proc(using h: Map_Header, key: Map_Key) -> int { } -__dynamic_map_remove :: proc(using h: Map_Header, key: Map_Key) { +__dynamic_map_remove :: proc(using h: __Map_Header, key: __Map_Key) { fr := __dynamic_map_find(h, key); if fr.entry_index >= 0 { __dynamic_map_erase(h, fr); } } -__dynamic_map_get_entry :: proc(using h: Map_Header, index: int) -> ^Map_Entry_Header { +__dynamic_map_get_entry :: proc(using h: __Map_Header, index: int) -> ^__Map_Entry_Header { data := cast(^byte)m.entries.data + index*entry_size; - return cast(^Map_Entry_Header)data; + return cast(^__Map_Entry_Header)data; } -__dynamic_map_erase :: proc(using h: Map_Header, fr: Map_Find_Result) { +__dynamic_map_erase :: proc(using h: __Map_Header, fr: __Map_Find_Result) { if fr.entry_prev < 0 { m.hashes[fr.hash_index] = __dynamic_map_get_entry(h, fr.entry_index).next; } else { diff --git a/core/fmt.odin b/core/fmt.odin index 105888caf..91519167b 100644 --- a/core/fmt.odin +++ b/core/fmt.odin @@ -191,18 +191,30 @@ buffer_write_type :: proc(buf: ^Buffer, ti: ^Type_Info) { buffer_write_string(buf, "]"); buffer_write_type(buf, info.elem); + case Map: + buffer_write_string(buf, "map["); + buffer_write_type(buf, info.key); + buffer_write_byte(buf, ']'); + buffer_write_type(buf, info.value); + case Struct: buffer_write_string(buf, "struct "); if info.packed { buffer_write_string(buf, "#packed "); } if info.ordered { buffer_write_string(buf, "#ordered "); } - buffer_write_string(buf, "{"); + if info.custom_align { + buffer_write_string(buf, "#align "); + fi := Fmt_Info{buf = buf}; + fmt_int(^fi, cast(u64)info.align, false, 'd'); + buffer_write_byte(buf, ' '); + } + buffer_write_byte(buf, '{'); for field, i in info.fields { buffer_write_string(buf, field.name); buffer_write_string(buf, ": "); buffer_write_type(buf, field.type_info); buffer_write_byte(buf, ','); } - buffer_write_string(buf, "}"); + buffer_write_byte(buf, '}'); case Union: buffer_write_string(buf, "union {"); @@ -778,6 +790,39 @@ fmt_value :: proc(fi: ^Fmt_Info, v: any, verb: rune) { fmt_arg(fi, any{info.elem, cast(rawptr)data}, 'v'); } + case Map: + if verb != 'v' { + fmt_bad_verb(fi, verb); + return; + } + + buffer_write_string(fi.buf, "map["); + defer buffer_write_byte(fi.buf, ']'); + entries := ^(cast(^Raw_Dynamic_Map)v.data).entries; + gs, _ := union_cast(^Struct)info.generated_struct; + ed, _ := union_cast(^Dynamic_Array)gs.fields[1].type_info; + entry_type, _ := union_cast(^Struct)ed.elem; + entry_size := ed.elem_size; + for i in 0.. 0 { + buffer_write_string(fi.buf, ", "); + } + data := cast(^byte)entries.data + i*entry_size; + + header := cast(^__Map_Entry_Header)data; + if types.is_string(info.key) { + buffer_write_string(fi.buf, header.key.str); + } else { + fi := Fmt_Info{buf = fi.buf}; + fmt_arg(^fi, any{info.key, cast(rawptr)^header.key.hash}, 'v'); + } + + buffer_write_string(fi.buf, "="); + + value := data + entry_type.fields[2].offset; + fmt_arg(fi, any{info.value, cast(rawptr)value}, 'v'); + } + case Slice: if verb != 'v' { fmt_bad_verb(fi, verb); diff --git a/src/check_expr.c b/src/check_expr.c index 13f66d060..a8f5fe55b 100644 --- a/src/check_expr.c +++ b/src/check_expr.c @@ -1097,8 +1097,8 @@ Type *make_map_tuple_type(gbAllocator a, Type *value) { Type *t = make_type_tuple(a); t->Tuple.variables = gb_alloc_array(a, Entity *, 2); t->Tuple.variable_count = 2; - t->Tuple.variables[0] = make_entity_param(a, NULL, blank_token, value, false, false); - t->Tuple.variables[1] = make_entity_param(a, NULL, blank_token, t_bool, false, false); + t->Tuple.variables[0] = make_entity_field(a, NULL, blank_token, value, false, 0); + t->Tuple.variables[1] = make_entity_field(a, NULL, blank_token, t_bool, false, 1); return t; } @@ -1148,9 +1148,9 @@ void check_map_type(Checker *c, Type *type, AstNode *node) { isize field_count = 3; Entity **fields = gb_alloc_array(a, Entity *, field_count); - fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("key")), t_map_key, false, false); - fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("next")), t_int, false, false); - fields[2] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("value")), value, false, false); + fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("key")), t_map_key, false, 0); + fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("next")), t_int, false, 1); + fields[2] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("value")), value, false, 2); check_close_scope(c); @@ -1159,7 +1159,6 @@ void check_map_type(Checker *c, Type *type, AstNode *node) { entry_type->Record.field_count = field_count; type_set_offsets(c->sizes, a, entry_type); - type->Map.entry_type = entry_type; } @@ -1181,8 +1180,8 @@ void check_map_type(Checker *c, Type *type, AstNode *node) { isize field_count = 2; Entity **fields = gb_alloc_array(a, Entity *, field_count); - fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("hashes")), hashes_type, false, false); - fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("entries")), entries_type, false, false); + fields[0] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("hashes")), hashes_type, false, 0); + fields[1] = make_entity_field(a, c->context.scope, make_token_ident(str_lit("entries")), entries_type, false, 1); check_close_scope(c); @@ -1191,7 +1190,6 @@ void check_map_type(Checker *c, Type *type, AstNode *node) { generated_struct_type->Record.field_count = field_count; type_set_offsets(c->sizes, a, generated_struct_type); - type->Map.generated_struct_type = generated_struct_type; } diff --git a/src/checker.c b/src/checker.c index 12915197a..78643e03c 100644 --- a/src/checker.c +++ b/src/checker.c @@ -958,6 +958,12 @@ void add_type_info_type(Checker *c, Type *t) { } } break; + case Type_Map: { + add_type_info_type(c, bt->Map.key); + add_type_info_type(c, bt->Map.value); + add_type_info_type(c, bt->Map.generated_struct_type); + } break; + case Type_Tuple: for (isize i = 0; i < bt->Tuple.variable_count; i++) { Entity *var = bt->Tuple.variables[i]; @@ -1093,7 +1099,7 @@ void init_preload(Checker *c) { - if (record->field_count != 19) { + if (record->field_count != 20) { compiler_error("Invalid `Type_Info` layout"); } t_type_info_named = record->fields[ 1]->type; @@ -1114,6 +1120,7 @@ void init_preload(Checker *c) { t_type_info_union = record->fields[16]->type; t_type_info_raw_union = record->fields[17]->type; t_type_info_enum = record->fields[18]->type; + t_type_info_map = record->fields[19]->type; t_type_info_named_ptr = make_type_pointer(heap_allocator(), t_type_info_named); t_type_info_integer_ptr = make_type_pointer(heap_allocator(), t_type_info_integer); @@ -1133,6 +1140,7 @@ void init_preload(Checker *c) { t_type_info_union_ptr = make_type_pointer(heap_allocator(), t_type_info_union); t_type_info_raw_union_ptr = make_type_pointer(heap_allocator(), t_type_info_raw_union); t_type_info_enum_ptr = make_type_pointer(heap_allocator(), t_type_info_enum); + t_type_info_map_ptr = make_type_pointer(heap_allocator(), t_type_info_map); } if (t_allocator == NULL) { @@ -1155,12 +1163,12 @@ void init_preload(Checker *c) { } if (t_map_key == NULL) { - Entity *e = find_core_entity(c, str_lit("Map_Key")); + Entity *e = find_core_entity(c, str_lit("__Map_Key")); t_map_key = e->type; } if (t_map_header == NULL) { - Entity *e = find_core_entity(c, str_lit("Map_Header")); + Entity *e = find_core_entity(c, str_lit("__Map_Header")); t_map_header = e->type; } diff --git a/src/ir.c b/src/ir.c index c6c834068..084191192 100644 --- a/src/ir.c +++ b/src/ir.c @@ -6286,14 +6286,16 @@ void ir_gen_tree(irGen *s) { tag = ir_emit_conv(proc, ti_ptr, t_type_info_struct_ptr); { - irValue *packed = ir_make_const_bool(a, t->Record.struct_is_packed); - irValue *ordered = ir_make_const_bool(a, t->Record.struct_is_ordered); - irValue *size = ir_make_const_int(a, type_size_of(m->sizes, a, t)); - irValue *align = ir_make_const_int(a, type_align_of(m->sizes, a, t)); + irValue *size = ir_make_const_int(a, type_size_of(m->sizes, a, t)); + irValue *align = ir_make_const_int(a, type_align_of(m->sizes, a, t)); + irValue *packed = ir_make_const_bool(a, t->Record.struct_is_packed); + irValue *ordered = ir_make_const_bool(a, t->Record.struct_is_ordered); + irValue *custom_align = ir_make_const_bool(a, t->Record.custom_align); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 1), size); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 2), align); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 3), packed); ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 4), ordered); + ir_emit_store(proc, ir_emit_struct_ep(proc, tag, 5), custom_align); } irValue *memory = ir_type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index); @@ -6516,6 +6518,20 @@ void ir_gen_tree(irGen *s) { // TODO(bill): Type_Info for procedures } break; + + case Type_Map: { + tag = ir_emit_conv(proc, ti_ptr, t_type_info_map_ptr); + + irValue *key = ir_emit_struct_ep(proc, tag, 0); + irValue *value = ir_emit_struct_ep(proc, tag, 1); + irValue *generated_struct = ir_emit_struct_ep(proc, tag, 2); + irValue *count = ir_emit_struct_ep(proc, tag, 3); + + ir_emit_store(proc, key, ir_get_type_info_ptr(proc, type_info_data, t->Map.key)); + ir_emit_store(proc, value, ir_get_type_info_ptr(proc, type_info_data, t->Map.value)); + ir_emit_store(proc, generated_struct, ir_get_type_info_ptr(proc, type_info_data, t->Map.generated_struct_type)); + ir_emit_store(proc, count, ir_make_const_int(a, t->Map.count)); + } break; } if (tag != NULL) { diff --git a/src/types.c b/src/types.c index be28c82f0..eae9ec556 100644 --- a/src/types.c +++ b/src/types.c @@ -300,6 +300,7 @@ gb_global Type *t_type_info_struct = NULL; gb_global Type *t_type_info_union = NULL; gb_global Type *t_type_info_raw_union = NULL; gb_global Type *t_type_info_enum = NULL; +gb_global Type *t_type_info_map = NULL; gb_global Type *t_type_info_named_ptr = NULL; @@ -320,6 +321,7 @@ gb_global Type *t_type_info_struct_ptr = NULL; gb_global Type *t_type_info_union_ptr = NULL; gb_global Type *t_type_info_raw_union_ptr = NULL; gb_global Type *t_type_info_enum_ptr = NULL; +gb_global Type *t_type_info_map_ptr = NULL; @@ -1561,8 +1563,7 @@ i64 type_align_of_internal(BaseTypeSizes s, gbAllocator allocator, Type *t, Type case Type_Map: { if (t->Map.count == 0) { // Dynamic - // NOTE(bill): same as a dynamic array - return s.word_size; + return type_align_of_internal(s, allocator, t->Map.generated_struct_type, path); } GB_PANIC("TODO(bill): Fixed map alignment"); } break; @@ -1778,8 +1779,7 @@ i64 type_size_of_internal(BaseTypeSizes s, gbAllocator allocator, Type *t, TypeP case Type_Map: { if (t->Map.count == 0) { // Dynamic - // NOTE(bill): same as a two dynamic arrays - return 2 * type_size_of_internal(s, allocator, t_raw_dynamic_array, path); + return type_size_of_internal(s, allocator, t->Map.generated_struct_type, path); } GB_PANIC("TODO(bill): Fixed map size"); } -- cgit v1.2.3 From 454d0b5cf5b109fda01b3380b1fab0434d7ff51d Mon Sep 17 00:00:00 2001 From: Ginger Bill Date: Tue, 7 Feb 2017 18:13:37 +0000 Subject: Fix global maps and initialize the preload types before --- code/demo.odin | 12 +++++++++--- src/check_expr.c | 6 ++++++ src/check_stmt.c | 1 - src/ir.c | 13 ++++--------- src/types.c | 8 ++++++++ 5 files changed, 27 insertions(+), 13 deletions(-) (limited to 'src/types.c') diff --git a/code/demo.odin b/code/demo.odin index 45ae781a2..bc193ef1f 100644 --- a/code/demo.odin +++ b/code/demo.odin @@ -9,7 +9,6 @@ #import "utf8.odin"; #import ht "http_test.odin"; - main :: proc() { { m: map[f32]int; @@ -21,11 +20,18 @@ main :: proc() { m[3.0] = 564; c := m[3.0]; _, ok := m[3.0]; - assert(ok && c == 564); + // assert(ok && c == 564); + fmt.print("map["); + i := 0; for val, key in m { - fmt.printf("m[%f] == %v\n", key, val); + if i > 0 { + fmt.print(", "); + } + fmt.printf("%f=%v", key, val); + i += 1; } + fmt.println("]"); } { m := map[string]u32{ diff --git a/src/check_expr.c b/src/check_expr.c index a8f5fe55b..04f246532 100644 --- a/src/check_expr.c +++ b/src/check_expr.c @@ -1132,6 +1132,12 @@ void check_map_type(Checker *c, Type *type, AstNode *node) { gbAllocator a = c->allocator; { + // NOTE(bill): The preload types may have not been set yet + if (t_map_key == NULL) { + init_preload(c); + } + GB_ASSERT(t_map_key != NULL); + Type *entry_type = make_type_struct(a); /* diff --git a/src/check_stmt.c b/src/check_stmt.c index a77120737..9155947fc 100644 --- a/src/check_stmt.c +++ b/src/check_stmt.c @@ -261,7 +261,6 @@ Type *check_assignment_variable(Checker *c, Operand *op_a, AstNode *lhs) { case Addressing_Invalid: return NULL; case Addressing_Variable: - break; case Addressing_MapIndex: break; default: { diff --git a/src/ir.c b/src/ir.c index 084191192..117173ab3 100644 --- a/src/ir.c +++ b/src/ir.c @@ -1395,7 +1395,7 @@ irValue *ir_emit_struct_ep(irProcedure *proc, irValue *s, i32 index); irValue *ir_emit_comp(irProcedure *proc, TokenKind op_kind, irValue *left, irValue *right); irValue *ir_gen_map_header(irProcedure *proc, irValue *map_val, Type *map_type) { - GB_ASSERT(is_type_pointer(ir_type(map_val))); + GB_ASSERT_MSG(is_type_pointer(ir_type(map_val)), "%s", type_to_string(ir_type(map_val))); gbAllocator a = proc->module->allocator; irValue *h = ir_add_local_generated(proc, t_map_header); @@ -3027,7 +3027,7 @@ irValue *ir_build_single_expr(irProcedure *proc, AstNode *expr, TypeAndValue *tv case_ast_node(cl, CompoundLit, expr); - return ir_emit_load(proc, ir_build_addr(proc, expr).addr); + return ir_addr_load(proc, ir_build_addr(proc, expr)); case_end; @@ -3597,11 +3597,11 @@ irValue *ir_build_single_expr(irProcedure *proc, AstNode *expr, TypeAndValue *tv case_end; case_ast_node(de, DemaybeExpr, expr); - return ir_emit_load(proc, ir_build_addr(proc, expr).addr); + return ir_addr_load(proc, ir_build_addr(proc, expr)); case_end; case_ast_node(se, SliceExpr, expr); - return ir_emit_load(proc, ir_build_addr(proc, expr).addr); + return ir_addr_load(proc, ir_build_addr(proc, expr)); case_end; case_ast_node(ie, IndexExpr, expr); @@ -3824,17 +3824,12 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { Type *t = base_type(type_of_expr(proc->module->info, ie->expr)); gbAllocator a = proc->module->allocator; - bool deref = is_type_pointer(t); t = base_type(type_deref(t)); if (is_type_map(t)) { irAddr map_addr = ir_build_addr(proc, ie->expr); irValue *map_val = map_addr.addr; - if (deref) { - map_val = ir_addr_load(proc, map_addr); - } - irValue *key = ir_build_expr(proc, ie->index); key = ir_emit_conv(proc, key, t->Map.key); diff --git a/src/types.c b/src/types.c index eae9ec556..dbded4a73 100644 --- a/src/types.c +++ b/src/types.c @@ -923,6 +923,14 @@ bool are_types_identical(Type *x, Type *y) { are_types_identical(x->Proc.results, y->Proc.results); } break; + + case Type_Map: + if (y->kind == Type_Map) { + return x->Map.count == y->Map.count && + are_types_identical(x->Map.key, y->Map.key) && + are_types_identical(x->Map.value, y->Map.value); + } + break; } -- cgit v1.2.3