diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/check_expr.cpp | 35 | ||||
| -rw-r--r-- | src/checker.cpp | 15 | ||||
| -rw-r--r-- | src/ir.cpp | 148 | ||||
| -rw-r--r-- | src/ir_print.cpp | 2 | ||||
| -rw-r--r-- | src/parser.cpp | 47 | ||||
| -rw-r--r-- | src/types.cpp | 5 |
6 files changed, 93 insertions, 159 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp index 4c52ca400..c59bd995c 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -3077,7 +3077,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id isize max_args = 1; if (is_type_slice(type)) { min_args = 2; - max_args = 3; + max_args = 2; } else if (is_type_map(type)) { min_args = 1; max_args = 2; @@ -6084,12 +6084,6 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t switch (t->kind) { case Type_Basic: if (is_type_string(t)) { - if (se->index3) { - error(node, "3-index slice on a string in not needed"); - o->mode = Addressing_Invalid; - o->expr = node; - return kind; - } valid = true; if (o->mode == Addressing_Constant) { max_count = o->value.value_string.len; @@ -6141,25 +6135,10 @@ ExprKind check_expr_base_internal(Checker *c, Operand *o, AstNode *node, Type *t // It is okay to continue as it will assume the 1st index is zero } - if (se->index3 && (se->high == nullptr || se->max == nullptr)) { - error(se->close, "2nd and 3rd indices are required in a 3-index slice"); - o->mode = Addressing_Invalid; - o->expr = node; - return kind; - } + TokenKind interval_kind = se->interval.kind; - if (se->index3 && se->interval0.kind != se->interval1.kind) { - error(se->close, "The interval separators for in a 3-index slice must be the same"); - o->mode = Addressing_Invalid; - o->expr = node; - return kind; - } - - - TokenKind interval_kind = se->interval0.kind; - - i64 indices[3] = {}; - AstNode *nodes[3] = {se->low, se->high, se->max}; + i64 indices[2] = {}; + AstNode *nodes[2] = {se->low, se->high}; for (isize i = 0; i < gb_count_of(nodes); i++) { i64 index = max_count; if (nodes[i] != nullptr) { @@ -6461,12 +6440,8 @@ gbString write_expr_to_string(gbString str, AstNode *node) { str = write_expr_to_string(str, se->expr); str = gb_string_append_rune(str, '['); str = write_expr_to_string(str, se->low); - str = gb_string_appendc(str, ".."); + str = string_append_token(str, se->interval); str = write_expr_to_string(str, se->high); - if (se->index3) { - str = gb_string_appendc(str, ".."); - str = write_expr_to_string(str, se->max); - } str = gb_string_append_rune(str, ']'); case_end; diff --git a/src/checker.cpp b/src/checker.cpp index 1ba84b5fd..88ad773c0 100644 --- a/src/checker.cpp +++ b/src/checker.cpp @@ -852,6 +852,9 @@ void add_global_string_constant(gbAllocator a, String name, String value) { } +void add_global_type_entity(gbAllocator a, String name, Type *type) { + add_global_entity(make_entity_type_name(a, nullptr, make_token_ident(name), type)); +} @@ -863,17 +866,9 @@ void init_universal_scope(void) { // Types for (isize i = 0; i < gb_count_of(basic_types); i++) { - add_global_entity(make_entity_type_name(a, nullptr, make_token_ident(basic_types[i].Basic.name), &basic_types[i])); + add_global_type_entity(a, basic_types[i].Basic.name, &basic_types[i]); } -#if 1 - // for (isize i = 0; i < gb_count_of(basic_type_aliases); i++) { - // add_global_entity(make_entity_type_name(a, nullptr, make_token_ident(basic_type_aliases[i].Basic.name), &basic_type_aliases[i])); - // } -#else - { - t_byte = add_global_type_alias(a, str_lit("byte"), &basic_types[Basic_u8]); - } -#endif + add_global_type_entity(a, str_lit("byte"), &basic_types[Basic_u8]); // Constants add_global_constant(a, str_lit("true"), t_untyped_bool, exact_value_bool(true)); diff --git a/src/ir.cpp b/src/ir.cpp index ea4d9c9a3..8b84f1e37 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -2674,7 +2674,6 @@ irValue *ir_emit_struct_ev(irProcedure *proc, irValue *s, i32 index) { switch (index) { case 0: result_type = make_type_pointer(a, t->Slice.elem); break; case 1: result_type = t_int; break; - case 2: result_type = t_int; break; } break; case Type_DynamicArray: @@ -2837,11 +2836,6 @@ irValue *ir_slice_count(irProcedure *proc, irValue *slice) { GB_ASSERT(is_type_slice(ir_type(slice))); return ir_emit_struct_ev(proc, slice, 1); } -irValue *ir_slice_capacity(irProcedure *proc, irValue *slice) { - GB_ASSERT(is_type_slice(ir_type(slice))); - return ir_emit_struct_ev(proc, slice, 2); -} - irValue *ir_dynamic_array_elem(irProcedure *proc, irValue *da) { GB_ASSERT(is_type_dynamic_array(ir_type(da))); return ir_emit_struct_ev(proc, da, 0); @@ -2873,14 +2867,13 @@ irValue *ir_string_len(irProcedure *proc, irValue *string) { } -void ir_fill_slice(irProcedure *proc, irValue *slice_ptr, irValue *data, irValue *len, irValue *cap) { +void ir_fill_slice(irProcedure *proc, irValue *slice_ptr, irValue *data, irValue *len) { Type *t = ir_type(slice_ptr); GB_ASSERT(is_type_pointer(t)); t = type_deref(t); GB_ASSERT(is_type_slice(t)); ir_emit_store(proc, ir_emit_struct_ep(proc, slice_ptr, 0), data); ir_emit_store(proc, ir_emit_struct_ep(proc, slice_ptr, 1), len); - ir_emit_store(proc, ir_emit_struct_ep(proc, slice_ptr, 2), cap); } void ir_fill_string(irProcedure *proc, irValue *string_ptr, irValue *data, irValue *len) { Type *t = ir_type(string_ptr); @@ -2900,7 +2893,7 @@ irValue *ir_emit_string(irProcedure *proc, irValue *elem, irValue *len) { -irValue *ir_add_local_slice(irProcedure *proc, Type *slice_type, irValue *base, irValue *low, irValue *high, irValue *max) { +irValue *ir_add_local_slice(irProcedure *proc, Type *slice_type, irValue *base, irValue *low, irValue *high) { // TODO(bill): array bounds checking for slice creation // TODO(bill): check that low < high <= max gbAllocator a = proc->module->allocator; @@ -2916,16 +2909,8 @@ irValue *ir_add_local_slice(irProcedure *proc, Type *slice_type, irValue *base, case Type_Pointer: high = v_one; break; } } - if (max == nullptr) { - switch (bt->kind) { - case Type_Array: high = ir_array_len(proc, base); break; - case Type_Slice: high = ir_slice_capacity(proc, base); break; - case Type_Pointer: high = v_one; break; - } - } irValue *len = ir_emit_arith(proc, Token_Sub, high, low, t_int); - irValue *cap = ir_emit_arith(proc, Token_Sub, max, low, t_int); irValue *elem = nullptr; switch (bt->kind) { @@ -2937,7 +2922,7 @@ irValue *ir_add_local_slice(irProcedure *proc, Type *slice_type, irValue *base, elem = ir_emit_ptr_offset(proc, elem, low); irValue *slice = ir_add_local_generated(proc, slice_type); - ir_fill_slice(proc, slice, elem, len, cap); + ir_fill_slice(proc, slice, elem, len); return slice; } @@ -3274,8 +3259,7 @@ irValue *ir_emit_conv(irProcedure *proc, irValue *value, Type *t) { ir_emit_store(proc, elem_ptr, elem); irValue *len = ir_string_len(proc, value); - irValue *cap = len; - irValue *slice = ir_add_local_slice(proc, t, elem_ptr, v_zero, len, cap); + irValue *slice = ir_add_local_slice(proc, t, elem_ptr, v_zero, len); return ir_emit_load(proc, slice); } @@ -3648,7 +3632,7 @@ void ir_emit_bounds_check(irProcedure *proc, Token token, irValue *index, irValu // ir_emit(proc, ir_instr_bounds_check(proc, token.pos, index, len)); } -void ir_emit_slice_bounds_check(irProcedure *proc, Token token, irValue *low, irValue *high, irValue *max, bool is_substring) { +void ir_emit_slice_bounds_check(irProcedure *proc, Token token, irValue *low, irValue *high, bool is_substring) { if ((proc->module->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) { return; } @@ -3660,22 +3644,38 @@ void ir_emit_slice_bounds_check(irProcedure *proc, Token token, irValue *low, ir low = ir_emit_conv(proc, low, t_int); high = ir_emit_conv(proc, high, t_int); - irValue **args = gb_alloc_array(a, irValue *, 6); + irValue **args = gb_alloc_array(a, irValue *, 5); args[0] = file; args[1] = line; args[2] = column; args[3] = low; args[4] = high; - args[5] = max; - if (is_substring) { - ir_emit_global_call(proc, "__substring_expr_error", args, 5); - } else { - ir_emit_global_call(proc, "__slice_expr_error", args, 6); + char *func = is_substring ? "__substring_expr_error" : "__slice_expr_error"; + ir_emit_global_call(proc, func, args, 5); +} + +void ir_emit_dynamic_array_bounds_check(irProcedure *proc, Token token, irValue *low, irValue *high, irValue *max) { + if ((proc->module->stmt_state_flags & StmtStateFlag_no_bounds_check) != 0) { + return; } + gbAllocator a = proc->module->allocator; + irValue *file = ir_find_or_add_entity_string(proc->module, token.pos.file); + irValue *line = ir_const_int(a, token.pos.line); + irValue *column = ir_const_int(a, token.pos.column); + low = ir_emit_conv(proc, low, t_int); + high = ir_emit_conv(proc, high, t_int); + + irValue **args = gb_alloc_array(a, irValue *, 6); + args[0] = file; + args[1] = line; + args[2] = column; + args[3] = low; + args[4] = high; + args[5] = max; - // ir_emit(proc, ir_instr_slice_bounds_check(proc, token.pos, low, high, max, is_substring)); + ir_emit_global_call(proc, "__dynamic_array_expr_error", args, 6); } @@ -4139,7 +4139,7 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv } else if (is_type_vector(t)) { GB_PANIC("Unreachable"); } else if (is_type_slice(t)) { - return ir_slice_capacity(proc, v); + return ir_slice_count(proc, v); } else if (is_type_dynamic_array(t)) { return ir_dynamic_array_capacity(proc, v); } else if (is_type_map(t)) { @@ -4212,17 +4212,12 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv irValue *elem_align = ir_const_int(a, eal); irValue *len = ir_emit_conv(proc, ir_build_expr(proc, ce->args[1]), t_int); - irValue *cap = len; - if (ce->args.count == 3) { - cap = ir_emit_conv(proc, ir_build_expr(proc, ce->args[2]), t_int); - } + ir_emit_slice_bounds_check(proc, ast_node_token(ce->args[1]), v_zero, len, false); - ir_emit_slice_bounds_check(proc, ast_node_token(ce->args[1]), v_zero, len, cap, false); - - irValue *slice_size = cap; + irValue *slice_size = len; if (eal != 1) { - slice_size = ir_emit_arith(proc, Token_Mul, elem_size, cap, t_int); + slice_size = ir_emit_arith(proc, Token_Mul, elem_size, len, t_int); } TokenPos pos = ast_node_token(ce->args[0]).pos; @@ -4240,7 +4235,7 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv } irValue *slice = ir_add_local_generated(proc, type); - ir_fill_slice(proc, slice, ptr, len, cap); + ir_fill_slice(proc, slice, ptr, len); return ir_emit_load(proc, slice); } else if (is_type_map(type)) { irValue *int_16 = ir_const_int(a, 16); @@ -4272,7 +4267,7 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv cap = ir_emit_conv(proc, ir_build_expr(proc, ce->args[2]), t_int); } - ir_emit_slice_bounds_check(proc, ast_node_token(ce->args[0]), v_zero, len, cap, false); + ir_emit_dynamic_array_bounds_check(proc, ast_node_token(ce->args[0]), v_zero, len, cap); irValue *array = ir_add_local_generated(proc, type); irValue **args = gb_alloc_array(a, irValue *, 6); @@ -5270,7 +5265,7 @@ irValue *ir_build_expr(irProcedure *proc, AstNode *expr) { irValue *base_elem = ir_emit_array_epi(proc, base_array, 0); irValue *len = ir_const_int(allocator, slice_len); - ir_fill_slice(proc, slice, base_elem, len, len); + ir_fill_slice(proc, slice, base_elem, len); } arg_count = param_count; @@ -5663,20 +5658,14 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { gbAllocator a = proc->module->allocator; irValue *low = v_zero; irValue *high = nullptr; - irValue *max = nullptr; - if (se->low != nullptr) low = ir_build_expr(proc, se->low); - if (se->high != nullptr) high = ir_build_expr(proc, se->high); - if (se->max != nullptr) max = ir_build_expr(proc, se->max); + if (se->low != nullptr) low = ir_build_expr(proc, se->low); + if (se->high != nullptr) high = ir_build_expr(proc, se->high); - if (high != nullptr && se->interval0.kind == Token_Ellipsis) { + if (high != nullptr && se->interval.kind == Token_Ellipsis) { high = ir_emit_arith(proc, Token_Add, high, v_one, t_int); } - if (max != nullptr && se->interval1.kind == Token_Ellipsis) { - max = ir_emit_arith(proc, Token_Add, max, v_one, t_int); - } - irValue *addr = ir_build_addr_ptr(proc, se->expr); irValue *base = ir_emit_load(proc, addr); Type *type = base_type(ir_type(base)); @@ -5693,16 +5682,14 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { Type *slice_type = type; if (high == nullptr) high = ir_slice_count(proc, base); - if (max == nullptr) max = ir_slice_capacity(proc, base); - ir_emit_slice_bounds_check(proc, se->open, low, high, max, false); + ir_emit_slice_bounds_check(proc, se->open, low, high, false); irValue *elem = ir_emit_ptr_offset(proc, ir_slice_elem(proc, base), low); irValue *len = ir_emit_arith(proc, Token_Sub, high, low, t_int); - irValue *cap = ir_emit_arith(proc, Token_Sub, max, low, t_int); irValue *slice = ir_add_local_generated(proc, slice_type); - ir_fill_slice(proc, slice, elem, len, cap); + ir_fill_slice(proc, slice, elem, len); return ir_addr(slice); } @@ -5711,16 +5698,15 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { Type *slice_type = make_type_slice(a, elem_type); if (high == nullptr) high = ir_dynamic_array_count(proc, base); - if (max == nullptr) max = ir_dynamic_array_capacity(proc, base); + irValue *cap = ir_dynamic_array_capacity(proc, base); - ir_emit_slice_bounds_check(proc, se->open, low, high, max, false); + ir_emit_dynamic_array_bounds_check(proc, se->open, low, high, cap); irValue *elem = ir_emit_ptr_offset(proc, ir_dynamic_array_elem(proc, base), low); irValue *len = ir_emit_arith(proc, Token_Sub, high, low, t_int); - irValue *cap = ir_emit_arith(proc, Token_Sub, max, low, t_int); irValue *slice = ir_add_local_generated(proc, slice_type); - ir_fill_slice(proc, slice, elem, len, cap); + ir_fill_slice(proc, slice, elem, len); return ir_addr(slice); } @@ -5729,21 +5715,18 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { Type *slice_type = make_type_slice(a, type->Array.elem); if (high == nullptr) high = ir_array_len(proc, base); - if (max == nullptr) max = ir_array_len(proc, base); bool low_const = type_and_value_of_expr(proc->module->info, se->low).mode == Addressing_Constant; bool high_const = type_and_value_of_expr(proc->module->info, se->high).mode == Addressing_Constant; - bool max_const = type_and_value_of_expr(proc->module->info, se->max).mode == Addressing_Constant; - if (!low_const || !high_const || !max_const) { - ir_emit_slice_bounds_check(proc, se->open, low, high, max, false); + if (!low_const || !high_const) { + ir_emit_slice_bounds_check(proc, se->open, low, high, false); } irValue *elem = ir_emit_ptr_offset(proc, ir_array_elem(proc, addr), low); irValue *len = ir_emit_arith(proc, Token_Sub, high, low, t_int); - irValue *cap = ir_emit_arith(proc, Token_Sub, max, low, t_int); irValue *slice = ir_add_local_generated(proc, slice_type); - ir_fill_slice(proc, slice, elem, len, cap); + ir_fill_slice(proc, slice, elem, len); return ir_addr(slice); } @@ -5752,7 +5735,7 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { if (high == nullptr) high = ir_string_len(proc, base); // if (max == nullptr) max = ir_string_len(proc, base); - ir_emit_slice_bounds_check(proc, se->open, low, high, nullptr, true); + ir_emit_slice_bounds_check(proc, se->open, low, high, true); irValue *elem = ir_emit_ptr_offset(proc, ir_string_elem(proc, base), low); irValue *len = ir_emit_arith(proc, Token_Sub, high, low, t_int); @@ -5760,7 +5743,6 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { irValue *str = ir_add_local_generated(proc, t_string); ir_fill_string(proc, str, elem, len); return ir_addr(str); - break; } } @@ -5984,7 +5966,7 @@ irAddr ir_build_addr(irProcedure *proc, AstNode *expr) { } irValue *count = ir_const_int(proc->module->allocator, slice->ConstantSlice.count); - ir_fill_slice(proc, v, data, count, count); + ir_fill_slice(proc, v, data, count); } break; } @@ -7746,7 +7728,7 @@ void ir_init_module(irModule *m, Checker *c) { { String name = str_lit(IR_TYPE_INFO_OFFSETS_NAME); Entity *e = make_entity_variable(m->allocator, nullptr, make_token_ident(name), - make_type_array(m->allocator, t_int, count), false); + make_type_array(m->allocator, t_uintptr, count), false); irValue *g = ir_value_global(m->allocator, e, nullptr); ir_module_add_value(m, e, g); map_set(&m->members, hash_string(name), g); @@ -7899,7 +7881,7 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info irValue *len = ir_const_int(proc->module->allocator, type->Array.count); ir_fill_slice(proc, global_type_table, ir_emit_array_epi(proc, ir_global_type_info_data, 0), - len, len); + len); } @@ -8098,8 +8080,8 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info } irValue *count = ir_const_int(a, t->Tuple.variables.count); - ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 0), memory_types, count, count); - ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 1), memory_names, count, count); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 0), memory_types, count); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 1), memory_names, count); break; } case Type_Enum: @@ -8138,11 +8120,11 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info irValue *names = ir_emit_struct_ep(proc, tag, 1); irValue *name_array_elem = ir_array_elem(proc, name_array); - ir_fill_slice(proc, names, name_array_elem, v_count, v_count); + ir_fill_slice(proc, names, name_array_elem, v_count); irValue *values = ir_emit_struct_ep(proc, tag, 2); irValue *value_array_elem = ir_array_elem(proc, value_array); - ir_fill_slice(proc, values, value_array_elem, v_count, v_count); + ir_fill_slice(proc, values, value_array_elem, v_count); } } break; @@ -8170,11 +8152,11 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info } irValue *count = ir_const_int(a, variant_count); - ir_fill_slice(proc, variant_types, memory_types, count, count); + ir_fill_slice(proc, variant_types, memory_types, count); - i64 tag_size = union_tag_size(t); + i64 tag_size = union_tag_size(t); i64 tag_offset = align_formula(t->Union.variant_block_size, tag_size); - ir_emit_store(proc, tag_offset_ptr, ir_const_int(a, tag_offset)); + ir_emit_store(proc, tag_offset_ptr, ir_const_uintptr(a, tag_offset)); ir_emit_store(proc, tag_type_ptr, ir_type_info(proc, union_tag_type(t))); } @@ -8224,15 +8206,15 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info irValue *name = ir_emit_ptr_offset(proc, memory_names, index); ir_emit_store(proc, name, ir_const_string(a, f->token.string)); } - ir_emit_store(proc, offset, ir_const_int(a, foffset)); + ir_emit_store(proc, offset, ir_const_uintptr(a, foffset)); ir_emit_store(proc, is_using, ir_const_bool(a, (f->flags&EntityFlag_Using) != 0)); } irValue *cv = ir_const_int(a, count); - ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 0), memory_types, cv, cv); - ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 1), memory_names, cv, cv); - ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 2), memory_offsets, cv, cv); - ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 3), memory_usings, cv, cv); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 0), memory_types, cv); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 1), memory_names, cv); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 2), memory_offsets, cv); + ir_fill_slice(proc, ir_emit_struct_ep(proc, tag, 3), memory_usings, cv); break; } case Type_Map: { @@ -8281,15 +8263,15 @@ void ir_setup_type_info_data(irProcedure *proc) { // NOTE(bill): Setup type_info irValue *names = ir_emit_struct_ep(proc, tag, 0); irValue *name_array_elem = ir_array_elem(proc, name_array); - ir_fill_slice(proc, names, name_array_elem, v_count, v_count); + ir_fill_slice(proc, names, name_array_elem, v_count); irValue *bits = ir_emit_struct_ep(proc, tag, 1); irValue *bit_array_elem = ir_array_elem(proc, bit_array); - ir_fill_slice(proc, bits, bit_array_elem, v_count, v_count); + ir_fill_slice(proc, bits, bit_array_elem, v_count); irValue *offsets = ir_emit_struct_ep(proc, tag, 2); irValue *offset_array_elem = ir_array_elem(proc, offset_array); - ir_fill_slice(proc, offsets, offset_array_elem, v_count, v_count); + ir_fill_slice(proc, offsets, offset_array_elem, v_count); } break; } diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 0c84b3567..0b0e5d97b 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -323,7 +323,7 @@ void ir_print_type(irFileBuffer *f, irModule *m, Type *t) { case Type_Slice: ir_write_byte(f, '{'); ir_print_type(f, m, t->Slice.elem); - ir_fprintf(f, "*, i%lld, i%lld}", word_bits, word_bits); + ir_fprintf(f, "*, i%lld}", word_bits); return; case Type_DynamicArray: ir_write_byte(f, '{'); diff --git a/src/parser.cpp b/src/parser.cpp index fc0dd13a0..d513bd79c 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -192,10 +192,8 @@ AST_NODE_KIND(_ExprBegin, "", i32) \ AST_NODE_KIND(SliceExpr, "slice expression", struct { \ AstNode *expr; \ Token open, close; \ - Token interval0; \ - Token interval1; \ - bool index3; \ - AstNode *low, *high, *max; \ + Token interval; \ + AstNode *low, *high; \ }) \ AST_NODE_KIND(CallExpr, "call expression", struct { \ AstNode * proc; \ @@ -714,7 +712,6 @@ AstNode *clone_ast_node(gbAllocator a, AstNode *node) { n->SliceExpr.expr = clone_ast_node(a, n->SliceExpr.expr); n->SliceExpr.low = clone_ast_node(a, n->SliceExpr.low); n->SliceExpr.high = clone_ast_node(a, n->SliceExpr.high); - n->SliceExpr.max = clone_ast_node(a, n->SliceExpr.max); break; case AstNode_CallExpr: n->CallExpr.proc = clone_ast_node(a, n->CallExpr.proc); @@ -1064,17 +1061,14 @@ AstNode *ast_index_expr(AstFile *f, AstNode *expr, AstNode *index, Token open, T } -AstNode *ast_slice_expr(AstFile *f, AstNode *expr, Token open, Token close, Token interval0, Token interval1, bool index3, AstNode *low, AstNode *high, AstNode *max) { +AstNode *ast_slice_expr(AstFile *f, AstNode *expr, Token open, Token close, Token interval, AstNode *low, AstNode *high) { AstNode *result = make_ast_node(f, AstNode_SliceExpr); result->SliceExpr.expr = expr; result->SliceExpr.open = open; result->SliceExpr.close = close; - result->SliceExpr.interval0 = interval0; - result->SliceExpr.interval1 = interval1; - result->SliceExpr.index3 = index3; + result->SliceExpr.interval = interval; result->SliceExpr.low = low; result->SliceExpr.high = high; - result->SliceExpr.max = max; return result; } @@ -2726,9 +2720,9 @@ AstNode *parse_atom_expr(AstFile *f, AstNode *operand, bool lhs) { f->allow_range = false; Token open = {}, close = {}, interval = {}; - AstNode *indices[3] = {}; - isize ellipsis_count = 0; - Token ellipses[2] = {}; + AstNode *indices[2] = {}; + Token ellipsis = {}; + bool is_ellipsis = false; f->expr_level++; open = expect_token(f, Token_OpenBracket); @@ -2739,15 +2733,15 @@ AstNode *parse_atom_expr(AstFile *f, AstNode *operand, bool lhs) { } bool is_index = true; - while ((f->curr_token.kind == Token_Ellipsis || - f->curr_token.kind == Token_HalfClosed) - && ellipsis_count < gb_count_of(ellipses)) { - ellipses[ellipsis_count++] = advance_token(f); + if ((f->curr_token.kind == Token_Ellipsis || + f->curr_token.kind == Token_HalfClosed)) { + ellipsis = advance_token(f); + is_ellipsis = true; if (f->curr_token.kind != Token_Ellipsis && f->curr_token.kind != Token_HalfClosed && f->curr_token.kind != Token_CloseBracket && f->curr_token.kind != Token_EOF) { - indices[ellipsis_count] = parse_expr(f, false); + indices[1] = parse_expr(f, false); } } @@ -2755,21 +2749,8 @@ AstNode *parse_atom_expr(AstFile *f, AstNode *operand, bool lhs) { f->expr_level--; close = expect_token(f, Token_CloseBracket); - if (ellipsis_count > 0) { - bool index3 = false; - if (ellipsis_count == 2) { - index3 = true; - // 2nd and 3rd index must be present - if (indices[1] == nullptr) { - syntax_error(ellipses[0], "2nd index required in 3-index slice expression"); - indices[1] = ast_bad_expr(f, ellipses[0], ellipses[1]); - } - if (indices[2] == nullptr) { - syntax_error(ellipses[1], "3rd index required in 3-index slice expression"); - indices[2] = ast_bad_expr(f, ellipses[1], close); - } - } - operand = ast_slice_expr(f, operand, open, close, ellipses[0], ellipses[1], index3, indices[0], indices[1], indices[2]); + if (is_ellipsis) { + operand = ast_slice_expr(f, operand, open, close, ellipsis, indices[0], indices[1]); } else { operand = ast_index_expr(f, operand, indices[0], open, close); } diff --git a/src/types.cpp b/src/types.cpp index f5acd073b..8dae03451 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -327,6 +327,7 @@ gb_global Type *t_untyped_nil = &basic_types[Basic_UntypedNil]; gb_global Type *t_untyped_undef = &basic_types[Basic_UntypedUndef]; + gb_global Type *t_u8_ptr = nullptr; gb_global Type *t_int_ptr = nullptr; gb_global Type *t_i64_ptr = nullptr; @@ -2127,8 +2128,8 @@ i64 type_size_of_internal(gbAllocator allocator, Type *t, TypePath *path) { } break; - case Type_Slice: // ptr + count - return 3 * build_context.word_size; + case Type_Slice: // ptr + len + return 2 * build_context.word_size; case Type_DynamicArray: // data + len + cap + allocator(procedure+data) |