diff --git a/compiler/typecheck/common.jou b/compiler/typecheck/common.jou index 2cda6377..da990019 100644 --- a/compiler/typecheck/common.jou +++ b/compiler/typecheck/common.jou @@ -224,23 +224,3 @@ def type_from_ast(ft: FileTypes*, asttype: AstType*) -> Type*: return tmp->array_type(len) assert False - - -def short_type_description(t: Type*) -> byte*: - if t->kind == TypeKind::OpaqueClass or t->kind == TypeKind::Class: - return "a class" - if t->kind == TypeKind::Enum: - return "an enum" - if t->kind == TypeKind::VoidPointer or t->kind == TypeKind::Pointer: - return "a pointer type" - if ( - t->kind == TypeKind::SignedInteger - or t->kind == TypeKind::UnsignedInteger - or t->kind == TypeKind::FloatingPoint - ): - return "a number type" - if t->kind == TypeKind::Array: - return "an array type" - if t->kind == TypeKind::Bool: - return "the built-in bool type" - assert False diff --git a/compiler/typecheck/step1_create_types.jou b/compiler/typecheck/step1_create_types.jou index 0c8e4795..ce22950d 100644 --- a/compiler/typecheck/step1_create_types.jou +++ b/compiler/typecheck/step1_create_types.jou @@ -40,7 +40,7 @@ def typecheck_step1_create_types(ft: FileTypes*, ast: AstFile*) -> ExportSymbol* existing = ft->find_type(name) if existing != NULL: msg: byte[500] - snprintf(msg, sizeof(msg), "%s named '%s' already exists", short_type_description(existing), name) + snprintf(msg, sizeof(msg), "%s named '%s' already exists", existing->short_description(), name) fail(stmt->location, msg) ft->types = realloc(ft->types, sizeof(ft->types[0]) * (ft->ntypes + 1)) diff --git a/compiler/typecheck/step3_function_and_method_bodies.jou b/compiler/typecheck/step3_function_and_method_bodies.jou index ca13b064..f1dc4d03 100644 --- a/compiler/typecheck/step3_function_and_method_bodies.jou +++ b/compiler/typecheck/step3_function_and_method_bodies.jou @@ -572,7 +572,7 @@ def typecheck_function_or_method_call(ft: FileTypes*, call: AstCall*, self_type: snprintf( msg, sizeof(msg), "type %s does not have any methods because it is %s, not a class", - self_type->name, short_type_description(self_type)) + self_type->name, self_type->short_description()) fail(location, msg) @@ -658,7 +658,7 @@ def typecheck_instantiation(ft: FileTypes*, inst: AstInstantiation*, location: L snprintf( msg, sizeof(msg), "the %s{...} syntax is only for classes, but %s is %s", - t->name, t->name, short_type_description(t)) + t->name, t->name, t->short_description()) fail(location, msg) specified_fields: ClassField** = malloc(sizeof(specified_fields[0]) * inst->nfields) @@ -789,7 +789,7 @@ def typecheck_expression(ft: FileTypes*, expr: AstExpression*) -> ExpressionType msg, sizeof(msg), "the '::' syntax is only for enums, but %s is %s", expr->enum_member.enum_name, - short_type_description(result), + result->short_description(), ) fail(expr->location, msg) if not enum_member_exists(result, expr->enum_member.member_name): diff --git a/compiler/types.jou b/compiler/types.jou index 4c942b07..bbeb70f1 100644 --- a/compiler/types.jou +++ b/compiler/types.jou @@ -33,7 +33,7 @@ enum TypeKind: VoidPointer Array Class - OpaqueClass # class with unknown members. TODO when used? + OpaqueClass # class with unknown members, used temporarily during compiling Enum class Type: @@ -88,6 +88,25 @@ class Type: info->arrays[info->narrays++] = arr return &arr->type + def short_description(self) -> byte*: + if self->kind == TypeKind::OpaqueClass or self->kind == TypeKind::Class: + return "a class" + if self->kind == TypeKind::Enum: + return "an enum" + if self->kind == TypeKind::VoidPointer or self->kind == TypeKind::Pointer: + return "a pointer type" + if ( + self->kind == TypeKind::SignedInteger + or self->kind == TypeKind::UnsignedInteger + or self->kind == TypeKind::FloatingPoint + ): + return "a number type" + if self->kind == TypeKind::Array: + return "an array type" + if self->kind == TypeKind::Bool: + return "the built-in bool type" + assert False + def find_method(self, name: byte*) -> Signature*: if self->kind != TypeKind::Class: return NULL