Make for in logic a bit more generic

This commit is contained in:
gingerBill
2021-03-24 12:31:05 +00:00
parent 0e3ecc350a
commit d969d0b264
2 changed files with 37 additions and 32 deletions
+9 -2
View File
@@ -102,13 +102,20 @@ Slice<T> slice_clone_from_array(gbAllocator const &allocator, Array<T> const &a)
} }
template <typename T>
void slice_copy(Slice<T> *slice, Slice<T> const &data) {
isize n = gb_min(slice->count, data.count);
gb_memmove(slice->data, data.data, gb_size_of(T)*n);
}
template <typename T> template <typename T>
void slice_copy(Slice<T> *slice, Slice<T> const &data, isize offset) { void slice_copy(Slice<T> *slice, Slice<T> const &data, isize offset) {
gb_memmove(slice->data+offset, data.data, gb_size_of(T)*data.count); isize n = gb_clamp(slice->count-offset, 0, data.count);
gb_memmove(slice->data+offset, data.data, gb_size_of(T)*n);
} }
template <typename T> template <typename T>
void slice_copy(Slice<T> *slice, Slice<T> const &data, isize offset, isize count) { void slice_copy(Slice<T> *slice, Slice<T> const &data, isize offset, isize count) {
gb_memmove(slice->data+offset, data.data, gb_size_of(T)*gb_min(data.count, count)); isize n = gb_clamp(slice->count-offset, 0, gb_min(data.count, count));
gb_memmove(slice->data+offset, data.data, gb_size_of(T)*n);
} }
+28 -30
View File
@@ -1657,10 +1657,8 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
check_open_scope(ctx, node); check_open_scope(ctx, node);
check_label(ctx, rs->label, node); check_label(ctx, rs->label, node);
Type *val0 = nullptr; auto vals = array_make<Type *>(temporary_allocator(), 0, 2);
Type *val1 = nullptr; auto entities = array_make<Entity *>(temporary_allocator(), 0, 2);
Entity *entities[2] = {};
isize entity_count = 0;
bool is_map = false; bool is_map = false;
bool use_by_reference_for_value = false; bool use_by_reference_for_value = false;
@@ -1676,8 +1674,8 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
if (!ok) { if (!ok) {
goto skip_expr_range_stmt; goto skip_expr_range_stmt;
} }
val0 = x.type; array_add(&vals, x.type);
val1 = t_int; array_add(&vals, t_int);
} else { } else {
Operand operand = {Addressing_Invalid}; Operand operand = {Addressing_Invalid};
check_expr_base(ctx, &operand, expr, nullptr); check_expr_base(ctx, &operand, expr, nullptr);
@@ -1690,8 +1688,8 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
gb_string_free(t); gb_string_free(t);
goto skip_expr_range_stmt; goto skip_expr_range_stmt;
} else { } else {
val0 = operand.type; array_add(&vals, operand.type);
val1 = t_int; array_add(&vals, t_int);
add_type_info_type(ctx, operand.type); add_type_info_type(ctx, operand.type);
goto skip_expr_range_stmt; goto skip_expr_range_stmt;
} }
@@ -1701,41 +1699,41 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
switch (t->kind) { switch (t->kind) {
case Type_Basic: case Type_Basic:
if (is_type_string(t) && t->Basic.kind != Basic_cstring) { if (is_type_string(t) && t->Basic.kind != Basic_cstring) {
val0 = t_rune; array_add(&vals, t_rune);
val1 = t_int; array_add(&vals, t_int);
add_package_dependency(ctx, "runtime", "string_decode_rune"); add_package_dependency(ctx, "runtime", "string_decode_rune");
} }
break; break;
case Type_EnumeratedArray: case Type_EnumeratedArray:
if (is_ptr) use_by_reference_for_value = true; if (is_ptr) use_by_reference_for_value = true;
val0 = t->EnumeratedArray.elem; array_add(&vals, t->EnumeratedArray.elem);
val1 = t->EnumeratedArray.index; array_add(&vals, t->EnumeratedArray.index);
break; break;
case Type_Array: case Type_Array:
if (is_ptr) use_by_reference_for_value = true; if (is_ptr) use_by_reference_for_value = true;
val0 = t->Array.elem; array_add(&vals, t->Array.elem);
val1 = t_int; array_add(&vals, t_int);
break; break;
case Type_DynamicArray: case Type_DynamicArray:
if (is_ptr) use_by_reference_for_value = true; if (is_ptr) use_by_reference_for_value = true;
val0 = t->DynamicArray.elem; array_add(&vals, t->DynamicArray.elem);
val1 = t_int; array_add(&vals, t_int);
break; break;
case Type_Slice: case Type_Slice:
if (is_ptr) use_by_reference_for_value = true; if (is_ptr) use_by_reference_for_value = true;
val0 = t->Slice.elem; array_add(&vals, t->Slice.elem);
val1 = t_int; array_add(&vals, t_int);
break; break;
case Type_Map: case Type_Map:
if (is_ptr) use_by_reference_for_value = true; if (is_ptr) use_by_reference_for_value = true;
is_map = true; is_map = true;
val0 = t->Map.key; array_add(&vals, t->Map.key);
val1 = t->Map.value; array_add(&vals, t->Map.value);
break; break;
case Type_Tuple: case Type_Tuple:
@@ -1754,8 +1752,9 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
break; break;
} }
if (count > 1) val0 = t->Tuple.variables[0]->type; for_array(ti, t->Tuple.variables) {
if (count > 2) val1 = t->Tuple.variables[1]->type; array_add(&vals, t->Tuple.variables[ti]->type);
}
if (rs->vals.count > 1 && rs->vals[1] != nullptr && count < 3) { if (rs->vals.count > 1 && rs->vals[1] != nullptr && count < 3) {
gbString s = type_to_string(t); gbString s = type_to_string(t);
@@ -1782,7 +1781,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
} }
} }
if (val0 == nullptr) { if (vals.count == 0 || vals[0] == nullptr) {
gbString s = expr_to_string(operand.expr); gbString s = expr_to_string(operand.expr);
gbString t = type_to_string(operand.type); gbString t = type_to_string(operand.type);
defer (gb_string_free(s)); defer (gb_string_free(s));
@@ -1807,12 +1806,11 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
error(rs->vals[max_val_count], "Expected a maximum of %td identifier%s, got %td", max_val_count, max_val_count == 1 ? "" : "s", rs->vals.count); error(rs->vals[max_val_count], "Expected a maximum of %td identifier%s, got %td", max_val_count, max_val_count == 1 ? "" : "s", rs->vals.count);
} }
Ast * lhs[2] = {}; auto rhs = slice_from_array(vals);
Type *rhs[2] = {val0, val1}; auto lhs = slice_make<Ast *>(temporary_allocator(), rhs.count);
if (rs->vals.count > 1) { lhs[1] = rs->vals[1]; } slice_copy(&lhs, rs->vals);
if (rs->vals.count > 0) { lhs[0] = rs->vals[0]; }
for (isize i = 0; i < 2; i++) { for_array(i, rhs) {
if (lhs[i] == nullptr) { if (lhs[i] == nullptr) {
continue; continue;
} }
@@ -1856,7 +1854,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
entity = alloc_entity_dummy_variable(builtin_pkg->scope, ast_token(name)); entity = alloc_entity_dummy_variable(builtin_pkg->scope, ast_token(name));
} }
entities[entity_count++] = entity; array_add(&entities, entity);
if (type == nullptr) { if (type == nullptr) {
entity->type = t_invalid; entity->type = t_invalid;
@@ -1864,7 +1862,7 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) {
} }
} }
for (isize i = 0; i < entity_count; i++) { for_array(i, entities) {
Entity *e = entities[i]; Entity *e = entities[i];
DeclInfo *d = decl_info_of_entity(e); DeclInfo *d = decl_info_of_entity(e);
GB_ASSERT(d == nullptr); GB_ASSERT(d == nullptr);