Compare commits
No commits in common. "e3e0cd9f9f8e735cb69f521eaf5e3e40601f9fec" and "5ed26f41392c043d4213827430561443f82cf984" have entirely different histories.
e3e0cd9f9f
...
5ed26f4139
@ -2,42 +2,6 @@
|
|||||||
#include "typechecker.h"
|
#include "typechecker.h"
|
||||||
#include "ast.h"
|
#include "ast.h"
|
||||||
#include "errors.h"
|
#include "errors.h"
|
||||||
#include "result.h"
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
enum class TypecheckRes {
|
|
||||||
Ok,
|
|
||||||
Castable,
|
|
||||||
};
|
|
||||||
|
|
||||||
Result<TypecheckRes, std::string> check_type(std::shared_ptr<types::Type> checked, std::shared_ptr<types::Type> target) {
|
|
||||||
if (types::types_equal(checked, target)) {
|
|
||||||
return TypecheckRes::Ok;
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::string{ "Types " + checked->formatted() + " and " + target->formatted() + " incompatible" };
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<AST::Expression> handle_res(
|
|
||||||
std::unique_ptr<AST::Expression> expr,
|
|
||||||
Result<TypecheckRes, std::string> res,
|
|
||||||
typecheck::State& state) {
|
|
||||||
if (res.ok()) {
|
|
||||||
auto result = res.unwrap();
|
|
||||||
if (result == TypecheckRes::Ok) {
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
state.errors.push_back(CompileError("Casting not yet implemented", expr->m_meta));
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
state.errors.push_back(CompileError(res.unwrap_err(), expr->m_meta));
|
|
||||||
return expr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace AST {
|
namespace AST {
|
||||||
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
|
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
|
||||||
@ -63,20 +27,13 @@ namespace AST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<types::Type> ValueReferenceExpression::typecheck(
|
std::shared_ptr<types::Type> ValueReferenceExpression::typecheck(
|
||||||
typecheck::State& state,
|
typecheck::State&,
|
||||||
typecheck::Scope& scope,
|
typecheck::Scope& scope,
|
||||||
std::optional<std::shared_ptr<types::Type>>
|
std::optional<std::shared_ptr<types::Type>>
|
||||||
) {
|
) {
|
||||||
if (scope.symbols.find(this->m_name) != scope.symbols.end()) {
|
|
||||||
return scope.symbols[this->m_name];
|
return scope.symbols[this->m_name];
|
||||||
}
|
}
|
||||||
|
|
||||||
state.errors.push_back(CompileError("Value " + this->m_name + " not defined", this->m_meta));
|
|
||||||
return std::shared_ptr<types::Type>{
|
|
||||||
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<types::Type> BinaryOperationExpression::typecheck(
|
std::shared_ptr<types::Type> BinaryOperationExpression::typecheck(
|
||||||
typecheck::State& state,
|
typecheck::State& state,
|
||||||
typecheck::Scope& scope,
|
typecheck::Scope& scope,
|
||||||
@ -97,12 +54,7 @@ namespace AST {
|
|||||||
) {
|
) {
|
||||||
auto expr_ty = this->m_fn_expr->typecheck(state, scope, {});
|
auto expr_ty = this->m_fn_expr->typecheck(state, scope, {});
|
||||||
|
|
||||||
if (expr_ty->m_kind != types::TypeKind::Function) {
|
// TODO make sure function_ty really is a function type
|
||||||
state.errors.push_back(CompileError("Tried calling a non-function", this->m_meta));
|
|
||||||
return std::shared_ptr<types::Type> {
|
|
||||||
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto fn_ty = dynamic_cast<types::FunctionType*>(expr_ty.get());
|
auto fn_ty = dynamic_cast<types::FunctionType*>(expr_ty.get());
|
||||||
|
|
||||||
@ -113,17 +65,11 @@ namespace AST {
|
|||||||
state.errors.push_back(CompileError("too many arguments", this->m_meta));
|
state.errors.push_back(CompileError("too many arguments", this->m_meta));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (int i = 0; i < static_cast<int>(this->m_args.size()); i++) {
|
for (int i = 0; i < static_cast<int>(fn_ty->m_param_tys.size()); i++) {
|
||||||
if (i < static_cast<int>(fn_ty->m_param_tys.size())) {
|
|
||||||
auto expected_param_ty = fn_ty->m_param_tys[i];
|
auto expected_param_ty = fn_ty->m_param_tys[i];
|
||||||
auto param_ty = this->m_args[i]->typecheck(state, scope, expected_param_ty);
|
auto param_ty = this->m_args[i]->typecheck(state, scope, expected_param_ty);
|
||||||
|
|
||||||
auto check_res = check_type(param_ty, expected_param_ty);
|
// TODO make sure types actually match
|
||||||
this->m_args[i] = handle_res(std::move(this->m_args[i]), check_res, state);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
this->m_args[i]->typecheck(state, scope, {});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,11 +77,7 @@ namespace AST {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
||||||
auto res_ty = this->m_expr->typecheck(state, scope, scope.return_ty);
|
this->m_expr->typecheck(state, scope, scope.return_ty);
|
||||||
if (scope.return_ty) {
|
|
||||||
auto check_res = check_type(res_ty, *scope.return_ty);
|
|
||||||
this->m_expr = handle_res(std::move(this->m_expr), check_res, state);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitializationStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
void InitializationStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
||||||
@ -153,8 +95,6 @@ namespace AST {
|
|||||||
auto bool_ty_ptr = new types::FundamentalType{ types::FundamentalTypeKind::Bool };
|
auto bool_ty_ptr = new types::FundamentalType{ types::FundamentalTypeKind::Bool };
|
||||||
this->m_condition->typecheck(state, scope, std::shared_ptr<types::Type>{ bool_ty_ptr });
|
this->m_condition->typecheck(state, scope, std::shared_ptr<types::Type>{ bool_ty_ptr });
|
||||||
|
|
||||||
// TODO check that condition really is a boolean
|
|
||||||
|
|
||||||
this->m_then->typecheck(state, scope);
|
this->m_then->typecheck(state, scope);
|
||||||
if (this->m_else) {
|
if (this->m_else) {
|
||||||
(*this->m_else)->typecheck(state, scope);
|
(*this->m_else)->typecheck(state, scope);
|
||||||
@ -172,13 +112,6 @@ namespace AST {
|
|||||||
scope.symbols[this->m_name] = std::shared_ptr<types::Type>{ function_ty };
|
scope.symbols[this->m_name] = std::shared_ptr<types::Type>{ function_ty };
|
||||||
|
|
||||||
typecheck::Scope inner{ scope };
|
typecheck::Scope inner{ scope };
|
||||||
inner.return_ty = return_ty;
|
|
||||||
|
|
||||||
for (auto& param : this->m_params) {
|
|
||||||
if (param.first) {
|
|
||||||
inner.symbols[*param.first] = param.second;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this->m_statements) {
|
if (this->m_statements) {
|
||||||
for (auto& statement : *this->m_statements) {
|
for (auto& statement : *this->m_statements) {
|
||||||
|
|||||||
@ -165,44 +165,4 @@ namespace types {
|
|||||||
this->m_inner
|
this->m_inner
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool types_equal(std::shared_ptr<types::Type> type1, std::shared_ptr<types::Type> type2) {
|
|
||||||
if (type1->m_kind != type2->m_kind)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (type1->m_kind == TypeKind::Fundamental) {
|
|
||||||
auto ty1 = dynamic_cast<FundamentalType*>(type1.get());
|
|
||||||
auto ty2 = dynamic_cast<FundamentalType*>(type2.get());
|
|
||||||
return ty1->m_ty == ty2->m_ty;
|
|
||||||
}
|
|
||||||
else if (type1->m_kind == TypeKind::Function) {
|
|
||||||
auto ty1 = dynamic_cast<FunctionType*>(type1.get());
|
|
||||||
auto ty2 = dynamic_cast<FunctionType*>(type2.get());
|
|
||||||
|
|
||||||
if (!types_equal(ty1->m_ret_ty, ty2->m_ret_ty))
|
|
||||||
return false;
|
|
||||||
if (ty1->m_vararg != ty2->m_vararg)
|
|
||||||
return false;
|
|
||||||
if (ty1->m_param_tys.size() != ty2->m_param_tys.size())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
for (int i = 0; i < static_cast<int>(ty1->m_param_tys.size()); i++) {
|
|
||||||
auto param1 = ty1->m_param_tys[i];
|
|
||||||
auto param2 = ty2->m_param_tys[i];
|
|
||||||
if (!types_equal(param1, param2))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (type1->m_kind == TypeKind::Pointer) {
|
|
||||||
auto ty1 = dynamic_cast<PointerType*>(type1.get());
|
|
||||||
auto ty2 = dynamic_cast<PointerType*>(type2.get());
|
|
||||||
|
|
||||||
return types_equal(ty1->m_inner, ty2->m_inner);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
25
src/types.h
25
src/types.h
@ -18,12 +18,6 @@ namespace types {
|
|||||||
int operator_precedence(BinOp& op);
|
int operator_precedence(BinOp& op);
|
||||||
std::string format_operator(BinOp& op);
|
std::string format_operator(BinOp& op);
|
||||||
|
|
||||||
enum class TypeKind {
|
|
||||||
Fundamental,
|
|
||||||
Function,
|
|
||||||
Pointer,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum FundamentalTypeKind {
|
enum FundamentalTypeKind {
|
||||||
Int,
|
Int,
|
||||||
Bool,
|
Bool,
|
||||||
@ -33,8 +27,6 @@ namespace types {
|
|||||||
|
|
||||||
class Type {
|
class Type {
|
||||||
public:
|
public:
|
||||||
TypeKind m_kind;
|
|
||||||
Type(TypeKind kind) : m_kind{ kind } {}
|
|
||||||
virtual ~Type() = default;
|
virtual ~Type() = default;
|
||||||
virtual std::string formatted() = 0;
|
virtual std::string formatted() = 0;
|
||||||
virtual llvm::Type* codegen(codegen::Builder& builder) = 0;
|
virtual llvm::Type* codegen(codegen::Builder& builder) = 0;
|
||||||
@ -47,9 +39,10 @@ namespace types {
|
|||||||
};
|
};
|
||||||
|
|
||||||
class FundamentalType : public Type {
|
class FundamentalType : public Type {
|
||||||
public:
|
private:
|
||||||
FundamentalTypeKind m_ty;
|
FundamentalTypeKind m_ty;
|
||||||
FundamentalType(FundamentalTypeKind kind) : Type(TypeKind::Fundamental), m_ty{ kind } {}
|
public:
|
||||||
|
FundamentalType(FundamentalTypeKind kind) : m_ty{ kind } {}
|
||||||
virtual ~FundamentalType() override = default;
|
virtual ~FundamentalType() override = default;
|
||||||
virtual std::string formatted() override;
|
virtual std::string formatted() override;
|
||||||
virtual llvm::Type* codegen(codegen::Builder& builder) override;
|
virtual llvm::Type* codegen(codegen::Builder& builder) override;
|
||||||
@ -67,10 +60,7 @@ namespace types {
|
|||||||
std::vector<std::shared_ptr<Type>> m_param_tys;
|
std::vector<std::shared_ptr<Type>> m_param_tys;
|
||||||
bool m_vararg;
|
bool m_vararg;
|
||||||
FunctionType(std::shared_ptr<Type> ret_ty, std::vector<std::shared_ptr<Type>> param_tys, bool vararg)
|
FunctionType(std::shared_ptr<Type> ret_ty, std::vector<std::shared_ptr<Type>> param_tys, bool vararg)
|
||||||
: Type(TypeKind::Function)
|
: m_ret_ty{ std::move(ret_ty) }, m_param_tys{ std::move(param_tys) }, m_vararg{ vararg } {
|
||||||
, m_ret_ty{ std::move(ret_ty) }
|
|
||||||
, m_param_tys{ std::move(param_tys) }
|
|
||||||
, m_vararg{ vararg } {
|
|
||||||
}
|
}
|
||||||
virtual ~FunctionType() override = default;
|
virtual ~FunctionType() override = default;
|
||||||
virtual std::string formatted() override;
|
virtual std::string formatted() override;
|
||||||
@ -82,18 +72,17 @@ namespace types {
|
|||||||
|
|
||||||
|
|
||||||
class PointerType : public Type {
|
class PointerType : public Type {
|
||||||
public:
|
private:
|
||||||
std::shared_ptr<Type> m_inner;
|
std::shared_ptr<Type> m_inner;
|
||||||
|
public:
|
||||||
PointerType(std::shared_ptr<Type> inner)
|
PointerType(std::shared_ptr<Type> inner)
|
||||||
: Type(TypeKind::Pointer), m_inner{ std::move(inner) } {
|
: m_inner{ std::move(inner) } {
|
||||||
}
|
}
|
||||||
virtual ~PointerType() override = default;
|
virtual ~PointerType() override = default;
|
||||||
virtual std::string formatted() override;
|
virtual std::string formatted() override;
|
||||||
virtual llvm::Type* codegen(codegen::Builder& builder) override;
|
virtual llvm::Type* codegen(codegen::Builder& builder) override;
|
||||||
virtual std::pair<llvm::Value*, std::shared_ptr<Type>> load(codegen::Builder& builder, llvm::Value* ptr) override;
|
virtual std::pair<llvm::Value*, std::shared_ptr<Type>> load(codegen::Builder& builder, llvm::Value* ptr) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool types_equal(std::shared_ptr<types::Type> type1, std::shared_ptr<types::Type> type2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Reference in New Issue
Block a user