| ... | @@ -633,20 +633,27 @@ TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t | ... | @@ -633,20 +633,27 @@ TypeTableEntry *get_array_type(CodeGen *g, TypeTableEntry *child_type, uint64_t |
| 633 | | 633 | |
| 634 | static void slice_type_common_init(CodeGen *g, TypeTableEntry *pointer_type, TypeTableEntry *entry) { | 634 | static void slice_type_common_init(CodeGen *g, TypeTableEntry *pointer_type, TypeTableEntry *entry) { |
| 635 | unsigned element_count = 2; | 635 | unsigned element_count = 2; |
| | 636 | Buf *ptr_field_name = buf_create_from_str("ptr"); |
| | 637 | Buf *len_field_name = buf_create_from_str("len"); |
| | 638 | |
| 636 | entry->data.structure.layout = ContainerLayoutAuto; | 639 | entry->data.structure.layout = ContainerLayoutAuto; |
| 637 | entry->data.structure.is_slice = true; | 640 | entry->data.structure.is_slice = true; |
| 638 | entry->data.structure.src_field_count = element_count; | 641 | entry->data.structure.src_field_count = element_count; |
| 639 | entry->data.structure.gen_field_count = element_count; | 642 | entry->data.structure.gen_field_count = element_count; |
| 640 | entry->data.structure.fields = allocate<TypeStructField>(element_count); | 643 | entry->data.structure.fields = allocate<TypeStructField>(element_count); |
| 641 | entry->data.structure.fields[slice_ptr_index].name = buf_create_from_str("ptr"); | 644 | entry->data.structure.fields_by_name.init(element_count); |
| | 645 | entry->data.structure.fields[slice_ptr_index].name = ptr_field_name; |
| 642 | entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type; | 646 | entry->data.structure.fields[slice_ptr_index].type_entry = pointer_type; |
| 643 | entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index; | 647 | entry->data.structure.fields[slice_ptr_index].src_index = slice_ptr_index; |
| 644 | entry->data.structure.fields[slice_ptr_index].gen_index = 0; | 648 | entry->data.structure.fields[slice_ptr_index].gen_index = 0; |
| 645 | entry->data.structure.fields[slice_len_index].name = buf_create_from_str("len"); | 649 | entry->data.structure.fields[slice_len_index].name = len_field_name; |
| 646 | entry->data.structure.fields[slice_len_index].type_entry = g->builtin_types.entry_usize; | 650 | entry->data.structure.fields[slice_len_index].type_entry = g->builtin_types.entry_usize; |
| 647 | entry->data.structure.fields[slice_len_index].src_index = slice_len_index; | 651 | entry->data.structure.fields[slice_len_index].src_index = slice_len_index; |
| 648 | entry->data.structure.fields[slice_len_index].gen_index = 1; | 652 | entry->data.structure.fields[slice_len_index].gen_index = 1; |
| 649 | | 653 | |
| | 654 | entry->data.structure.fields_by_name.put(ptr_field_name, &entry->data.structure.fields[slice_ptr_index]); |
| | 655 | entry->data.structure.fields_by_name.put(len_field_name, &entry->data.structure.fields[slice_len_index]); |
| | 656 | |
| 650 | assert(type_has_zero_bits_known(pointer_type->data.pointer.child_type)); | 657 | assert(type_has_zero_bits_known(pointer_type->data.pointer.child_type)); |
| 651 | if (pointer_type->data.pointer.child_type->zero_bits) { | 658 | if (pointer_type->data.pointer.child_type->zero_bits) { |
| 652 | entry->data.structure.gen_field_count = 1; | 659 | entry->data.structure.gen_field_count = 1; |
| ... | @@ -1555,6 +1562,7 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f | ... | @@ -1555,6 +1562,7 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f |
| 1555 | struct_type->data.structure.zero_bits_known = true; | 1562 | struct_type->data.structure.zero_bits_known = true; |
| 1556 | struct_type->data.structure.complete = true; | 1563 | struct_type->data.structure.complete = true; |
| 1557 | struct_type->data.structure.fields = allocate<TypeStructField>(field_count); | 1564 | struct_type->data.structure.fields = allocate<TypeStructField>(field_count); |
| | 1565 | struct_type->data.structure.fields_by_name.init(field_count); |
| 1558 | | 1566 | |
| 1559 | ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(field_count); | 1567 | ZigLLVMDIType **di_element_types = allocate<ZigLLVMDIType*>(field_count); |
| 1560 | LLVMTypeRef *element_types = allocate<LLVMTypeRef>(field_count); | 1568 | LLVMTypeRef *element_types = allocate<LLVMTypeRef>(field_count); |
| ... | @@ -1566,6 +1574,9 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f | ... | @@ -1566,6 +1574,9 @@ TypeTableEntry *get_struct_type(CodeGen *g, const char *type_name, const char *f |
| 1566 | field->type_entry = field_types[i]; | 1574 | field->type_entry = field_types[i]; |
| 1567 | field->src_index = i; | 1575 | field->src_index = i; |
| 1568 | field->gen_index = i; | 1576 | field->gen_index = i; |
| | 1577 | |
| | 1578 | auto prev_entry = struct_type->data.structure.fields_by_name.put_unique(field->name, field); |
| | 1579 | assert(prev_entry == nullptr); |
| 1569 | } | 1580 | } |
| 1570 | | 1581 | |
| 1571 | struct_type->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), type_name); | 1582 | struct_type->type_ref = LLVMStructCreateNamed(LLVMGetGlobalContext(), type_name); |
| ... | @@ -2102,6 +2113,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { | ... | @@ -2102,6 +2113,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { |
| 2102 | | 2113 | |
| 2103 | enum_type->data.enumeration.src_field_count = field_count; | 2114 | enum_type->data.enumeration.src_field_count = field_count; |
| 2104 | enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count); | 2115 | enum_type->data.enumeration.fields = allocate<TypeEnumField>(field_count); |
| | 2116 | enum_type->data.enumeration.fields_by_name.init(field_count); |
| 2105 | | 2117 | |
| 2106 | Scope *scope = &enum_type->data.enumeration.decls_scope->base; | 2118 | Scope *scope = &enum_type->data.enumeration.decls_scope->base; |
| 2107 | | 2119 | |
| ... | @@ -2139,6 +2151,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { | ... | @@ -2139,6 +2151,7 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { |
| 2139 | TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_i]; | 2151 | TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[field_i]; |
| 2140 | type_enum_field->name = field_node->data.struct_field.name; | 2152 | type_enum_field->name = field_node->data.struct_field.name; |
| 2141 | type_enum_field->decl_index = field_i; | 2153 | type_enum_field->decl_index = field_i; |
| | 2154 | type_enum_field->decl_node = field_node; |
| 2142 | | 2155 | |
| 2143 | if (field_node->data.struct_field.type != nullptr) { | 2156 | if (field_node->data.struct_field.type != nullptr) { |
| 2144 | ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.type, | 2157 | ErrorMsg *msg = add_node_error(g, field_node->data.struct_field.type, |
| ... | @@ -2147,6 +2160,15 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { | ... | @@ -2147,6 +2160,15 @@ static void resolve_enum_zero_bits(CodeGen *g, TypeTableEntry *enum_type) { |
| 2147 | buf_sprintf("consider 'union(enum)' here")); | 2160 | buf_sprintf("consider 'union(enum)' here")); |
| 2148 | } | 2161 | } |
| 2149 | | 2162 | |
| | 2163 | auto field_entry = enum_type->data.enumeration.fields_by_name.put_unique(type_enum_field->name, type_enum_field); |
| | 2164 | if (field_entry != nullptr) { |
| | 2165 | ErrorMsg *msg = add_node_error(g, field_node, |
| | 2166 | buf_sprintf("duplicate enum field: '%s'", buf_ptr(type_enum_field->name))); |
| | 2167 | add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); |
| | 2168 | enum_type->data.enumeration.is_invalid = true; |
| | 2169 | continue; |
| | 2170 | } |
| | 2171 | |
| 2150 | AstNode *tag_value = field_node->data.struct_field.value; | 2172 | AstNode *tag_value = field_node->data.struct_field.value; |
| 2151 | | 2173 | |
| 2152 | // In this first pass we resolve explicit tag values. | 2174 | // In this first pass we resolve explicit tag values. |
| ... | @@ -2242,6 +2264,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { | ... | @@ -2242,6 +2264,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { |
| 2242 | size_t field_count = decl_node->data.container_decl.fields.length; | 2264 | size_t field_count = decl_node->data.container_decl.fields.length; |
| 2243 | struct_type->data.structure.src_field_count = (uint32_t)field_count; | 2265 | struct_type->data.structure.src_field_count = (uint32_t)field_count; |
| 2244 | struct_type->data.structure.fields = allocate<TypeStructField>(field_count); | 2266 | struct_type->data.structure.fields = allocate<TypeStructField>(field_count); |
| | 2267 | struct_type->data.structure.fields_by_name.init(field_count); |
| 2245 | | 2268 | |
| 2246 | Scope *scope = &struct_type->data.structure.decls_scope->base; | 2269 | Scope *scope = &struct_type->data.structure.decls_scope->base; |
| 2247 | | 2270 | |
| ... | @@ -2250,6 +2273,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { | ... | @@ -2250,6 +2273,7 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { |
| 2250 | AstNode *field_node = decl_node->data.container_decl.fields.at(i); | 2273 | AstNode *field_node = decl_node->data.container_decl.fields.at(i); |
| 2251 | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; | 2274 | TypeStructField *type_struct_field = &struct_type->data.structure.fields[i]; |
| 2252 | type_struct_field->name = field_node->data.struct_field.name; | 2275 | type_struct_field->name = field_node->data.struct_field.name; |
| | 2276 | type_struct_field->decl_node = field_node; |
| 2253 | | 2277 | |
| 2254 | if (field_node->data.struct_field.type == nullptr) { | 2278 | if (field_node->data.struct_field.type == nullptr) { |
| 2255 | add_node_error(g, field_node, buf_sprintf("struct field missing type")); | 2279 | add_node_error(g, field_node, buf_sprintf("struct field missing type")); |
| ... | @@ -2257,6 +2281,15 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { | ... | @@ -2257,6 +2281,15 @@ static void resolve_struct_zero_bits(CodeGen *g, TypeTableEntry *struct_type) { |
| 2257 | continue; | 2281 | continue; |
| 2258 | } | 2282 | } |
| 2259 | | 2283 | |
| | 2284 | auto field_entry = struct_type->data.structure.fields_by_name.put_unique(type_struct_field->name, type_struct_field); |
| | 2285 | if (field_entry != nullptr) { |
| | 2286 | ErrorMsg *msg = add_node_error(g, field_node, |
| | 2287 | buf_sprintf("duplicate struct field: '%s'", buf_ptr(type_struct_field->name))); |
| | 2288 | add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); |
| | 2289 | struct_type->data.structure.is_invalid = true; |
| | 2290 | continue; |
| | 2291 | } |
| | 2292 | |
| 2260 | TypeTableEntry *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type); | 2293 | TypeTableEntry *field_type = analyze_type_expr(g, scope, field_node->data.struct_field.type); |
| 2261 | type_struct_field->type_entry = field_type; | 2294 | type_struct_field->type_entry = field_type; |
| 2262 | type_struct_field->src_index = i; | 2295 | type_struct_field->src_index = i; |
| ... | @@ -2344,6 +2377,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { | ... | @@ -2344,6 +2377,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { |
| 2344 | } | 2377 | } |
| 2345 | union_type->data.unionation.src_field_count = field_count; | 2378 | union_type->data.unionation.src_field_count = field_count; |
| 2346 | union_type->data.unionation.fields = allocate<TypeUnionField>(field_count); | 2379 | union_type->data.unionation.fields = allocate<TypeUnionField>(field_count); |
| | 2380 | union_type->data.unionation.fields_by_name.init(field_count); |
| 2347 | | 2381 | |
| 2348 | uint32_t biggest_align_bytes = 0; | 2382 | uint32_t biggest_align_bytes = 0; |
| 2349 | | 2383 | |
| ... | @@ -2395,6 +2429,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { | ... | @@ -2395,6 +2429,7 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { |
| 2395 | tag_type->data.enumeration.layout = ContainerLayoutAuto; | 2429 | tag_type->data.enumeration.layout = ContainerLayoutAuto; |
| 2396 | tag_type->data.enumeration.src_field_count = field_count; | 2430 | tag_type->data.enumeration.src_field_count = field_count; |
| 2397 | tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count); | 2431 | tag_type->data.enumeration.fields = allocate<TypeEnumField>(field_count); |
| | 2432 | tag_type->data.enumeration.fields_by_name.init(field_count); |
| 2398 | tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope; | 2433 | tag_type->data.enumeration.decls_scope = union_type->data.unionation.decls_scope; |
| 2399 | tag_type->data.enumeration.complete = true; | 2434 | tag_type->data.enumeration.complete = true; |
| 2400 | } else if (enum_type_node != nullptr) { | 2435 | } else if (enum_type_node != nullptr) { |
| ... | @@ -2424,6 +2459,16 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { | ... | @@ -2424,6 +2459,16 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { |
| 2424 | Buf *field_name = field_node->data.struct_field.name; | 2459 | Buf *field_name = field_node->data.struct_field.name; |
| 2425 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; | 2460 | TypeUnionField *union_field = &union_type->data.unionation.fields[i]; |
| 2426 | union_field->name = field_node->data.struct_field.name; | 2461 | union_field->name = field_node->data.struct_field.name; |
| | 2462 | union_field->decl_node = field_node; |
| | 2463 | |
| | 2464 | auto field_entry = union_type->data.unionation.fields_by_name.put_unique(union_field->name, union_field); |
| | 2465 | if (field_entry != nullptr) { |
| | 2466 | ErrorMsg *msg = add_node_error(g, field_node, |
| | 2467 | buf_sprintf("duplicate union field: '%s'", buf_ptr(union_field->name))); |
| | 2468 | add_error_note(g, msg, field_entry->value->decl_node, buf_sprintf("other field here")); |
| | 2469 | union_type->data.unionation.is_invalid = true; |
| | 2470 | continue; |
| | 2471 | } |
| 2427 | | 2472 | |
| 2428 | TypeTableEntry *field_type; | 2473 | TypeTableEntry *field_type; |
| 2429 | if (field_node->data.struct_field.type == nullptr) { | 2474 | if (field_node->data.struct_field.type == nullptr) { |
| ... | @@ -2456,6 +2501,10 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { | ... | @@ -2456,6 +2501,10 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) { |
| 2456 | union_field->enum_field = &tag_type->data.enumeration.fields[i]; | 2501 | union_field->enum_field = &tag_type->data.enumeration.fields[i]; |
| 2457 | union_field->enum_field->name = field_name; | 2502 | union_field->enum_field->name = field_name; |
| 2458 | union_field->enum_field->decl_index = i; | 2503 | union_field->enum_field->decl_index = i; |
| | 2504 | union_field->enum_field->decl_node = field_node; |
| | 2505 | |
| | 2506 | auto prev_entry = tag_type->data.enumeration.fields_by_name.put_unique(union_field->enum_field->name, union_field->enum_field); |
| | 2507 | assert(prev_entry == nullptr); // caught by union de-duplicator above |
| 2459 | | 2508 | |
| 2460 | AstNode *tag_value = field_node->data.struct_field.value; | 2509 | AstNode *tag_value = field_node->data.struct_field.value; |
| 2461 | // In this first pass we resolve explicit tag values. | 2510 | // In this first pass we resolve explicit tag values. |
| ... | @@ -3499,37 +3548,35 @@ FnTableEntry *scope_get_fn_if_root(Scope *scope) { | ... | @@ -3499,37 +3548,35 @@ FnTableEntry *scope_get_fn_if_root(Scope *scope) { |
| 3499 | } | 3548 | } |
| 3500 | | 3549 | |
| 3501 | TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name) { | 3550 | TypeEnumField *find_enum_type_field(TypeTableEntry *enum_type, Buf *name) { |
| 3502 | for (uint32_t i = 0; i < enum_type->data.enumeration.src_field_count; i += 1) { | 3551 | assert(enum_type->id == TypeTableEntryIdEnum); |
| 3503 | TypeEnumField *type_enum_field = &enum_type->data.enumeration.fields[i]; | 3552 | if (enum_type->data.enumeration.src_field_count == 0) |
| 3504 | if (buf_eql_buf(type_enum_field->name, name)) { | 3553 | return nullptr; |
| 3505 | return type_enum_field; | 3554 | auto entry = enum_type->data.enumeration.fields_by_name.maybe_get(name); |
| 3506 | } | 3555 | if (entry == nullptr) |
| 3507 | } | 3556 | return nullptr; |
| 3508 | return nullptr; | 3557 | return entry->value; |
| 3509 | } | 3558 | } |
| 3510 | | 3559 | |
| 3511 | TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name) { | 3560 | TypeStructField *find_struct_type_field(TypeTableEntry *type_entry, Buf *name) { |
| 3512 | assert(type_entry->id == TypeTableEntryIdStruct); | 3561 | assert(type_entry->id == TypeTableEntryIdStruct); |
| 3513 | assert(type_entry->data.structure.complete); | 3562 | assert(type_entry->data.structure.complete); |
| 3514 | for (uint32_t i = 0; i < type_entry->data.structure.src_field_count; i += 1) { | 3563 | if (type_entry->data.structure.src_field_count == 0) |
| 3515 | TypeStructField *field = &type_entry->data.structure.fields[i]; | 3564 | return nullptr; |
| 3516 | if (buf_eql_buf(field->name, name)) { | 3565 | auto entry = type_entry->data.structure.fields_by_name.maybe_get(name); |
| 3517 | return field; | 3566 | if (entry == nullptr) |
| 3518 | } | 3567 | return nullptr; |
| 3519 | } | 3568 | return entry->value; |
| 3520 | return nullptr; | | |
| 3521 | } | 3569 | } |
| 3522 | | 3570 | |
| 3523 | TypeUnionField *find_union_type_field(TypeTableEntry *type_entry, Buf *name) { | 3571 | TypeUnionField *find_union_type_field(TypeTableEntry *type_entry, Buf *name) { |
| 3524 | assert(type_entry->id == TypeTableEntryIdUnion); | 3572 | assert(type_entry->id == TypeTableEntryIdUnion); |
| 3525 | assert(type_entry->data.unionation.zero_bits_known); | 3573 | assert(type_entry->data.unionation.zero_bits_known); |
| 3526 | for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) { | 3574 | if (type_entry->data.unionation.src_field_count == 0) |
| 3527 | TypeUnionField *field = &type_entry->data.unionation.fields[i]; | 3575 | return nullptr; |
| 3528 | if (buf_eql_buf(field->enum_field->name, name)) { | 3576 | auto entry = type_entry->data.unionation.fields_by_name.maybe_get(name); |
| 3529 | return field; | 3577 | if (entry == nullptr) |
| 3530 | } | 3578 | return nullptr; |
| 3531 | } | 3579 | return entry->value; |
| 3532 | return nullptr; | | |
| 3533 | } | 3580 | } |
| 3534 | | 3581 | |
| 3535 | TypeUnionField *find_union_field_by_tag(TypeTableEntry *type_entry, const BigInt *tag) { | 3582 | TypeUnionField *find_union_field_by_tag(TypeTableEntry *type_entry, const BigInt *tag) { |