aboutsummaryrefslogtreecommitdiff
path: root/src/check_type.cpp
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2021-10-26 21:08:08 +0100
committerGitHub <noreply@github.com>2021-10-26 21:08:08 +0100
commitc4d2aae0ed55d972b0074031ac82db6f9546447e (patch)
treec23fe528ddaee43ea2c9ecfd5b95d93ef7fba467 /src/check_type.cpp
parentc722665c3239019fe9f90d247726cc42c921e1db (diff)
parent549a383cf06ad45edd634e67c27a1246323a9d8c (diff)
Merge pull request #1245 from odin-lang/new-matrix-type
`matrix` type
Diffstat (limited to 'src/check_type.cpp')
-rw-r--r--src/check_type.cpp68
1 files changed, 65 insertions, 3 deletions
diff --git a/src/check_type.cpp b/src/check_type.cpp
index 0d5c0f977..813990020 100644
--- a/src/check_type.cpp
+++ b/src/check_type.cpp
@@ -997,8 +997,8 @@ void check_bit_set_type(CheckerContext *c, Type *type, Type *named_type, Ast *no
GB_ASSERT(lower <= upper);
- i64 bits = MAX_BITS;
- if (bs->underlying != nullptr) {
+ i64 bits = MAX_BITS
+; if (bs->underlying != nullptr) {
Type *u = check_type(c, bs->underlying);
if (!is_type_integer(u)) {
gbString ts = type_to_string(u);
@@ -1154,7 +1154,11 @@ Type *determine_type_from_polymorphic(CheckerContext *ctx, Type *poly_type, Oper
bool show_error = modify_type && !ctx->hide_polymorphic_errors;
if (!is_operand_value(operand)) {
if (show_error) {
- error(operand.expr, "Cannot determine polymorphic type from parameter");
+ gbString pts = type_to_string(poly_type);
+ gbString ots = type_to_string(operand.type);
+ defer (gb_string_free(pts));
+ defer (gb_string_free(ots));
+ error(operand.expr, "Cannot determine polymorphic type from parameter: '%s' to '%s'", ots, pts);
}
return t_invalid;
}
@@ -2200,6 +2204,57 @@ void check_map_type(CheckerContext *ctx, Type *type, Ast *node) {
// error(node, "'map' types are not yet implemented");
}
+void check_matrix_type(CheckerContext *ctx, Type **type, Ast *node) {
+ ast_node(mt, MatrixType, node);
+
+ Operand row = {};
+ Operand column = {};
+
+ i64 row_count = check_array_count(ctx, &row, mt->row_count);
+ i64 column_count = check_array_count(ctx, &column, mt->column_count);
+
+ Type *elem = check_type_expr(ctx, mt->elem, nullptr);
+
+ Type *generic_row = nullptr;
+ Type *generic_column = nullptr;
+
+ if (row.mode == Addressing_Type && row.type->kind == Type_Generic) {
+ generic_row = row.type;
+ }
+
+ if (column.mode == Addressing_Type && column.type->kind == Type_Generic) {
+ generic_column = column.type;
+ }
+
+ if (row_count < MATRIX_ELEMENT_COUNT_MIN && generic_row == nullptr) {
+ gbString s = expr_to_string(row.expr);
+ error(row.expr, "Invalid matrix row count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
+ gb_string_free(s);
+ }
+
+ if (column_count < MATRIX_ELEMENT_COUNT_MIN && generic_column == nullptr) {
+ gbString s = expr_to_string(column.expr);
+ error(column.expr, "Invalid matrix column count, expected %d+ rows, got %s", MATRIX_ELEMENT_COUNT_MIN, s);
+ gb_string_free(s);
+ }
+
+ if (row_count*column_count > MATRIX_ELEMENT_COUNT_MAX) {
+ i64 element_count = row_count*column_count;
+ error(column.expr, "Matrix types are limited to a maximum of %d elements, got %lld", MATRIX_ELEMENT_COUNT_MAX, cast(long long)element_count);
+ }
+
+ if (!is_type_valid_for_matrix_elems(elem)) {
+ gbString s = type_to_string(elem);
+ error(column.expr, "Matrix elements types are limited to integers, floats, and complex, got %s", s);
+ gb_string_free(s);
+ }
+
+ *type = alloc_type_matrix(elem, row_count, column_count, generic_row, generic_column);
+
+ return;
+}
+
+
Type *make_soa_struct_internal(CheckerContext *ctx, Ast *array_typ_expr, Ast *elem_expr, Type *elem, i64 count, Type *generic_type, StructSoaKind soa_kind) {
Type *bt_elem = base_type(elem);
@@ -2785,6 +2840,13 @@ bool check_type_internal(CheckerContext *ctx, Ast *e, Type **type, Type *named_t
return true;
}
case_end;
+
+
+ case_ast_node(mt, MatrixType, e);
+ check_matrix_type(ctx, type, e);
+ set_base_type(named_type, *type);
+ return true;
+ case_end;
}
*type = t_invalid;