646 lines
25 KiB
C++
646 lines
25 KiB
C++
|
|
#include "typechecker.h"
|
|
#include "ast.h"
|
|
#include "errors.h"
|
|
#include "result.h"
|
|
|
|
namespace {
|
|
enum class TypecheckResKind {
|
|
Ok,
|
|
Castable,
|
|
};
|
|
|
|
struct TypecheckRes {
|
|
TypecheckResKind kind;
|
|
std::shared_ptr<types::Type> result;
|
|
};
|
|
|
|
Result<TypecheckRes, std::string> check_type(
|
|
typecheck::State& state,
|
|
std::shared_ptr<types::Type> checked,
|
|
std::shared_ptr<types::Type> target) {
|
|
auto potential_cast = types::find_cast(state.casts, checked, target);
|
|
|
|
|
|
if (types::types_equal(checked, target)) {
|
|
return TypecheckRes{ TypecheckResKind::Ok, target };
|
|
}
|
|
else if (potential_cast.has_value()) {
|
|
if (potential_cast->allow_implicit)
|
|
return TypecheckRes{ TypecheckResKind::Castable, target };
|
|
|
|
return std::string{ "Type " + checked->formatted() + " not implicitly castable to " + target->formatted() };
|
|
}
|
|
|
|
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.kind == TypecheckResKind::Ok) {
|
|
return expr;
|
|
}
|
|
else {
|
|
return std::unique_ptr<AST::Expression> {
|
|
new AST::CastExpression{ expr->m_meta, result.result, std::move(expr) }
|
|
};
|
|
}
|
|
}
|
|
else {
|
|
state.errors.push_back(CompileError(res.unwrap_err(), expr->m_meta));
|
|
return expr;
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<types::Type> refresh_type(typecheck::Scope& scope, std::shared_ptr<types::Type> ty) {
|
|
if (ty->m_kind == types::TypeKind::Fundamental) {
|
|
return ty;
|
|
}
|
|
else if (ty->m_kind == types::TypeKind::Array) {
|
|
auto array_ty = dynamic_cast<types::ArrayType*>(ty.get());
|
|
array_ty->m_inner = refresh_type(scope, array_ty->m_inner);
|
|
return ty;
|
|
}
|
|
else if (ty->m_kind == types::TypeKind::Function) {
|
|
auto function_ty = dynamic_cast<types::FunctionType*>(ty.get());
|
|
function_ty->m_ret_ty = refresh_type(scope, function_ty->m_ret_ty);
|
|
for (int i = 0; i < static_cast<int>(function_ty->m_param_tys.size()); i++) {
|
|
function_ty->m_param_tys[i] = refresh_type(scope, function_ty->m_param_tys[i]);
|
|
}
|
|
return ty;
|
|
}
|
|
else if (ty->m_kind == types::TypeKind::Pointer) {
|
|
auto ptr_ty = dynamic_cast<types::PointerType*>(ty.get());
|
|
ptr_ty->m_inner = refresh_type(scope, ptr_ty->m_inner);
|
|
return ty;
|
|
}
|
|
else if (ty->m_kind == types::TypeKind::Struct) {
|
|
auto struct_ty = dynamic_cast<types::StructType*>(ty.get());
|
|
if (struct_ty->m_is_ref || struct_ty->m_is_def) {
|
|
if (scope.structs.find(*struct_ty->m_name) != scope.structs.end()) {
|
|
auto pre_existing = dynamic_cast<types::StructType*>(scope.structs[*struct_ty->m_name].get());
|
|
struct_ty->m_fields = pre_existing->m_fields;
|
|
}
|
|
}
|
|
if (struct_ty->m_fields) {
|
|
for (int i = 0; i < static_cast<int>((*struct_ty->m_fields).size()); i++) {
|
|
(*struct_ty->m_fields)[i].second = refresh_type(scope, (*struct_ty->m_fields)[i].second);
|
|
}
|
|
}
|
|
return ty;
|
|
}
|
|
else {
|
|
return ty;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace AST {
|
|
|
|
void IntLiteralExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_ty = refresh_type(scope, this->m_ty);
|
|
}
|
|
|
|
std::shared_ptr<types::Type> IntLiteralExpression::typecheck(
|
|
typecheck::State&,
|
|
typecheck::Scope&,
|
|
std::optional<std::shared_ptr<types::Type>> expected_ty
|
|
) {
|
|
// Allow implicitly converting IntLiteralExpression to other types
|
|
// representable by integers.
|
|
if (expected_ty) {
|
|
if ((*expected_ty)->m_kind == types::TypeKind::Fundamental) {
|
|
auto ty = dynamic_cast<types::FundamentalType*>((*expected_ty).get());
|
|
if (
|
|
ty->m_ty == types::FundamentalTypeKind::Bool
|
|
|| ty->m_ty == types::FundamentalTypeKind::Char
|
|
|| ty->m_ty == types::FundamentalTypeKind::Int
|
|
) {
|
|
this->m_ty = *expected_ty;
|
|
}
|
|
}
|
|
}
|
|
|
|
return this->m_ty;
|
|
}
|
|
|
|
void StringLiteralExpression::typecheck_preprocess(typecheck::Scope&) {}
|
|
|
|
std::shared_ptr<types::Type> StringLiteralExpression::typecheck(
|
|
typecheck::State&,
|
|
typecheck::Scope&,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
auto char_ty = std::shared_ptr<types::Type>{
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Char }
|
|
};
|
|
auto ptr_ty = new types::ArrayType{ char_ty, static_cast<uint32_t>(this->m_value.size()) + 1 };
|
|
return std::shared_ptr<types::Type>{ptr_ty};
|
|
}
|
|
|
|
void ValueReferenceExpression::typecheck_preprocess(typecheck::Scope&) {}
|
|
|
|
std::shared_ptr<types::Type> ValueReferenceExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
if (scope.symbols.find(this->m_name) != scope.symbols.end()) {
|
|
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 }
|
|
};
|
|
}
|
|
|
|
void BinaryOperationExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_lhs->typecheck_preprocess(scope);
|
|
this->m_rhs->typecheck_preprocess(scope);
|
|
}
|
|
|
|
std::shared_ptr<types::Type> BinaryOperationExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>> expected_ty
|
|
) {
|
|
auto lhs_ty = this->m_lhs->typecheck(state, scope, {});
|
|
auto rhs_ty = this->m_rhs->typecheck(state, scope, {});
|
|
|
|
if (this->m_binop == types::BinOp::Assignment) {
|
|
// Re-typecheck rhs to actually match lhs
|
|
auto rhs_ty = this->m_rhs->typecheck(state, scope, lhs_ty);
|
|
auto rhs_ty_res = check_type(state, rhs_ty, lhs_ty);
|
|
this->m_rhs = handle_res(std::move(this->m_rhs), rhs_ty_res, state);
|
|
return lhs_ty;
|
|
}
|
|
|
|
// Try to find a binop that matches exactly
|
|
auto binop = types::find_binop(
|
|
state.binops,
|
|
lhs_ty,
|
|
this->m_binop,
|
|
rhs_ty
|
|
);
|
|
|
|
if (binop) {
|
|
return binop->result(*binop, lhs_ty, rhs_ty);
|
|
}
|
|
|
|
// If that fails, try to find binop that matches on one side perfectly
|
|
// and is castable on the other side, and would also be perfectly
|
|
// assignable to the expected value.
|
|
for (auto& binop : state.binops) {
|
|
if (expected_ty) {
|
|
// Skip any binops that would not be immediately assignable to
|
|
// the expected type
|
|
if (!types::types_equal(binop.result(binop, lhs_ty, rhs_ty), *expected_ty)) {
|
|
continue;
|
|
}
|
|
}
|
|
if (types::types_equal(binop.lhs, lhs_ty)) {
|
|
auto rhs_res = check_type(state, rhs_ty, binop.rhs);
|
|
if (!rhs_res.ok())
|
|
// Skip if not implicitly castable to lhs
|
|
continue;
|
|
this->m_rhs = handle_res(std::move(this->m_rhs), rhs_res, state);
|
|
return binop.result(binop, lhs_ty, rhs_ty);
|
|
}
|
|
else if (types::types_equal(binop.rhs, rhs_ty)) {
|
|
auto lhs_res = check_type(state, lhs_ty, binop.lhs);
|
|
if (!lhs_res.ok())
|
|
// Skip if not implicitly castable to rhs
|
|
continue;
|
|
this->m_lhs = handle_res(std::move(this->m_lhs), lhs_res, state);
|
|
return binop.result(binop, lhs_ty, rhs_ty);
|
|
}
|
|
}
|
|
|
|
// Finally check for any binop that allows the result to be implicitly
|
|
// casted to the result
|
|
for (auto& binop : state.binops) {
|
|
if (expected_ty) {
|
|
// Skip any binops that would not even be implicitly castable to
|
|
// the expected result
|
|
auto result_res = check_type(state, binop.result(binop, lhs_ty, rhs_ty), *expected_ty);
|
|
if (!result_res.ok())
|
|
continue;
|
|
}
|
|
auto lhs_result = check_type(state, lhs_ty, binop.lhs);
|
|
auto rhs_result = check_type(state, rhs_ty, binop.rhs);
|
|
this->m_lhs = handle_res(std::move(this->m_lhs), lhs_result, state);
|
|
this->m_rhs = handle_res(std::move(this->m_rhs), lhs_result, state);
|
|
return binop.result(binop, lhs_ty, rhs_ty);
|
|
}
|
|
|
|
// No suitable binops found :(
|
|
state.errors.push_back(CompileError(
|
|
"No suitable binop between "
|
|
+ lhs_ty->formatted() + " "
|
|
+ types::format_operator(this->m_binop) + " "
|
|
+ rhs_ty->formatted(),
|
|
this->m_meta));
|
|
|
|
return std::shared_ptr<types::Type>{
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void } };
|
|
}
|
|
|
|
void FunctionCallExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_fn_expr->typecheck_preprocess(scope);
|
|
for (auto& expr : this->m_args) {
|
|
expr->typecheck_preprocess(scope);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<types::Type> FunctionCallExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
auto expr_ty = this->m_fn_expr->typecheck(state, scope, {});
|
|
|
|
if (expr_ty->m_kind != types::TypeKind::Function) {
|
|
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());
|
|
|
|
if (this->m_args.size() < fn_ty->m_param_tys.size()) {
|
|
state.errors.push_back(CompileError("too few arguments", this->m_meta));
|
|
}
|
|
else if (this->m_args.size() > fn_ty->m_param_tys.size() && !fn_ty->m_vararg) {
|
|
state.errors.push_back(CompileError("too many arguments", this->m_meta));
|
|
}
|
|
else {
|
|
for (int i = 0; i < static_cast<int>(this->m_args.size()); i++) {
|
|
if (i < static_cast<int>(fn_ty->m_param_tys.size())) {
|
|
auto expected_param_ty = fn_ty->m_param_tys[i];
|
|
auto param_ty = this->m_args[i]->typecheck(state, scope, expected_param_ty);
|
|
|
|
auto check_res = check_type(state, param_ty, expected_param_ty);
|
|
this->m_args[i] = handle_res(std::move(this->m_args[i]), check_res, state);
|
|
}
|
|
else {
|
|
this->m_args[i]->typecheck(state, scope, {});
|
|
}
|
|
}
|
|
}
|
|
|
|
return fn_ty->m_ret_ty;
|
|
}
|
|
|
|
void CastExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_ty = refresh_type(scope, this->m_ty);
|
|
this->m_expr->typecheck_preprocess(scope);
|
|
}
|
|
|
|
std::shared_ptr<types::Type> CastExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
auto expr_ty = this->m_expr->typecheck(state, scope, {});
|
|
auto cast = types::find_cast(state.casts, expr_ty, this->m_ty);
|
|
if (cast) {
|
|
return cast->target_ty;
|
|
}
|
|
state.errors.push_back(CompileError("Cast from type "
|
|
+ expr_ty->formatted() + "to type " + this->m_ty->formatted()
|
|
+ " is not permitted", this->m_meta));
|
|
return std::shared_ptr<types::Type> { new types::FundamentalType{
|
|
types::FundamentalTypeKind::Void
|
|
} };
|
|
}
|
|
|
|
void RefExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_expr->typecheck_preprocess(scope);
|
|
}
|
|
|
|
std::shared_ptr<types::Type> RefExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
auto expr_ty = this->m_expr->typecheck(state, scope, {});
|
|
return std::shared_ptr<types::Type> {
|
|
new types::PointerType{ expr_ty }
|
|
};
|
|
}
|
|
|
|
void DerefExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_expr->typecheck_preprocess(scope);
|
|
}
|
|
|
|
std::shared_ptr<types::Type> DerefExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
auto expr_ty = this->m_expr->typecheck(state, scope, {});
|
|
if (expr_ty->m_kind != types::TypeKind::Pointer) {
|
|
state.errors.push_back(
|
|
CompileError("Tried to deref " + expr_ty->formatted(), this->m_meta));
|
|
return std::shared_ptr<types::Type> {
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
};
|
|
}
|
|
auto ptr_ty = dynamic_cast<types::PointerType*>(expr_ty.get());
|
|
return ptr_ty->m_inner;
|
|
}
|
|
|
|
void IndexAccessExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_expr->typecheck_preprocess(scope);
|
|
}
|
|
|
|
std::shared_ptr<types::Type> IndexAccessExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
auto expr_ty = this->m_expr->typecheck(state, scope, {});
|
|
if (expr_ty->m_kind != types::TypeKind::Pointer && expr_ty->m_kind != types::TypeKind::Array) {
|
|
state.errors.push_back(
|
|
CompileError("Tried to index " + expr_ty->formatted(), this->m_meta));
|
|
return std::shared_ptr<types::Type> {
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
};
|
|
}
|
|
if (expr_ty->m_kind == types::TypeKind::Pointer) {
|
|
auto ptr_ty = dynamic_cast<types::PointerType*>(expr_ty.get());
|
|
return ptr_ty->m_inner;
|
|
}
|
|
else if (expr_ty->m_kind == types::TypeKind::Array) {
|
|
auto ptr_ty = dynamic_cast<types::ArrayType*>(expr_ty.get());
|
|
return ptr_ty->m_inner;
|
|
}
|
|
|
|
// Default return type
|
|
return std::shared_ptr<types::Type> {
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
};
|
|
}
|
|
|
|
void FieldAccessExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_expr->typecheck_preprocess(scope);
|
|
}
|
|
|
|
std::shared_ptr<types::Type> FieldAccessExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>>
|
|
) {
|
|
auto expr_ty = this->m_expr->typecheck(state, scope, {});
|
|
if (expr_ty->m_kind != types::TypeKind::Struct) {
|
|
state.errors.push_back(
|
|
CompileError("Tried to access " + expr_ty->formatted() + "." + this->m_field, this->m_meta));
|
|
return std::shared_ptr<types::Type> {
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
};
|
|
}
|
|
|
|
auto struct_ty = dynamic_cast<types::StructType*>(expr_ty.get());
|
|
if (struct_ty->m_fields) {
|
|
for (auto& field : *struct_ty->m_fields) {
|
|
if (field.first == this->m_field) {
|
|
return field.second;
|
|
}
|
|
}
|
|
state.errors.push_back(CompileError("No such field", this->m_meta));
|
|
return std::shared_ptr<types::Type> {
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
};
|
|
}
|
|
|
|
state.errors.push_back(CompileError("Cannot access fields of opaque struct", this->m_meta));
|
|
return std::shared_ptr<types::Type> {
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
};
|
|
}
|
|
|
|
void ListInitializerExpression::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_ty = refresh_type(scope, this->m_ty);
|
|
for (auto& expr : this->m_expressions) {
|
|
expr->typecheck_preprocess(scope);
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<types::Type> ListInitializerExpression::typecheck(
|
|
typecheck::State& state,
|
|
typecheck::Scope& scope,
|
|
std::optional<std::shared_ptr<types::Type>> expected_ty
|
|
) {
|
|
if (expected_ty) {
|
|
if ((*expected_ty)->m_kind == types::TypeKind::Array) {
|
|
auto array_ty = dynamic_cast<types::ArrayType*>(expected_ty->get());
|
|
for (auto& expr : this->m_expressions) {
|
|
auto expr_ty = expr->typecheck(state, scope, array_ty->m_inner);
|
|
auto expr_res = check_type(state, expr_ty, array_ty->m_inner);
|
|
expr = handle_res(std::move(expr), expr_res, state);
|
|
}
|
|
|
|
this->m_ty = std::shared_ptr<types::Type>{
|
|
new types::ArrayType{
|
|
array_ty->m_inner,
|
|
static_cast<uint32_t>(this->m_expressions.size())
|
|
}
|
|
};
|
|
}
|
|
else if ((*expected_ty)->m_kind == types::TypeKind::Struct) {
|
|
auto struct_ty = dynamic_cast<types::StructType*>(expected_ty->get());
|
|
|
|
if (struct_ty->m_fields) {
|
|
if (this->m_expressions.size() > struct_ty->m_fields->size()) {
|
|
state.errors.push_back(CompileError(
|
|
"Too many initializer values for " + struct_ty->formatted(),
|
|
this->m_meta));
|
|
return *expected_ty;
|
|
}
|
|
|
|
for (int i = 0; i < static_cast<int>(this->m_expressions.size()); i++) {
|
|
auto expected_field = (*struct_ty->m_fields)[i];
|
|
auto expr_ty = this->m_expressions[i]->typecheck(state, scope, expected_field.second);
|
|
auto res = check_type(state, expr_ty, expected_field.second);
|
|
this->m_expressions[i] = handle_res(std::move(this->m_expressions[i]), res, state);
|
|
}
|
|
|
|
this->m_ty = *expected_ty;
|
|
return this->m_ty;
|
|
}
|
|
else {
|
|
if (this->m_expressions.size() > 0) {
|
|
state.errors.push_back(CompileError(
|
|
"Too many initializer values for " + struct_ty->formatted(),
|
|
this->m_meta));
|
|
return *expected_ty;
|
|
}
|
|
else {
|
|
this->m_ty = *expected_ty;
|
|
return this->m_ty;
|
|
}
|
|
}
|
|
|
|
}
|
|
else {
|
|
return std::shared_ptr<types::Type> {
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Void }
|
|
};
|
|
}
|
|
|
|
return this->m_ty;
|
|
}
|
|
|
|
// No expected ty, try to infer array type from elements
|
|
|
|
if (this->m_expressions.size() == 0) {
|
|
this->m_ty = std::shared_ptr<types::Type>{
|
|
new types::ArrayType{
|
|
std::make_shared<types::FundamentalType>(types::FundamentalTypeKind::Void),
|
|
0
|
|
}
|
|
};
|
|
return this->m_ty;
|
|
}
|
|
else {
|
|
auto first_expr_ty = this->m_expressions[0]->typecheck(state, scope, {});
|
|
for (int i = 1; i < static_cast<int>(this->m_expressions.size()); i++) {
|
|
auto expr_ty = this->m_expressions[i]->typecheck(state, scope, first_expr_ty);
|
|
auto expr_res = check_type(state, expr_ty, first_expr_ty);
|
|
this->m_expressions[i] = handle_res(std::move(this->m_expressions[i]), expr_res, state);
|
|
}
|
|
this->m_ty = std::shared_ptr<types::Type>{
|
|
new types::ArrayType{
|
|
first_expr_ty,
|
|
static_cast<uint32_t>(this->m_expressions.size())
|
|
}
|
|
};
|
|
return this->m_ty;
|
|
}
|
|
}
|
|
|
|
void ReturnStatement::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_expr->typecheck_preprocess(scope);
|
|
}
|
|
|
|
void ReturnStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
|
auto res_ty = this->m_expr->typecheck(state, scope, scope.return_ty);
|
|
if (scope.return_ty) {
|
|
auto check_res = check_type(state, res_ty, *scope.return_ty);
|
|
this->m_expr = handle_res(std::move(this->m_expr), check_res, state);
|
|
}
|
|
}
|
|
|
|
void InitializationStatement::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_type = refresh_type(scope, this->m_type);
|
|
if (this->m_expr)
|
|
(*this->m_expr)->typecheck_preprocess(scope);
|
|
}
|
|
|
|
void InitializationStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
|
if (this->m_expr) {
|
|
auto expr_ty = (*this->m_expr)->typecheck(state, scope, this->m_type);
|
|
auto check_res = check_type(state, expr_ty, this->m_type);
|
|
this->m_expr = handle_res(std::move(*this->m_expr), check_res, state);
|
|
}
|
|
scope.symbols[this->m_name] = this->m_type;
|
|
}
|
|
|
|
void ExpressionStatement::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_expr->typecheck_preprocess(scope);
|
|
}
|
|
|
|
void ExpressionStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
|
this->m_expr->typecheck(state, scope, {});
|
|
}
|
|
|
|
void IfStatement::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_condition->typecheck_preprocess(scope);
|
|
this->m_then->typecheck_preprocess(scope);
|
|
if (this->m_else)
|
|
(*this->m_else)->typecheck_preprocess(scope);
|
|
}
|
|
|
|
void IfStatement::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
|
auto bool_ty = std::shared_ptr<types::Type>{
|
|
new types::FundamentalType{ types::FundamentalTypeKind::Bool } };
|
|
auto expr_ty = this->m_condition->typecheck(state, scope, bool_ty);
|
|
|
|
auto check_res = check_type(state, expr_ty, bool_ty);
|
|
this->m_condition = handle_res(std::move(this->m_condition), check_res, state);
|
|
|
|
this->m_then->typecheck(state, scope);
|
|
if (this->m_else) {
|
|
(*this->m_else)->typecheck(state, scope);
|
|
}
|
|
}
|
|
|
|
void Function::typecheck_preprocess(typecheck::Scope& scope) {
|
|
this->m_return_ty = refresh_type(scope, this->m_return_ty);
|
|
for (auto& param : this->m_params) {
|
|
param.second = refresh_type(scope, param.second);
|
|
}
|
|
|
|
if (this->m_statements) {
|
|
for (auto& statement : *this->m_statements) {
|
|
statement->typecheck_preprocess(scope);
|
|
}
|
|
}
|
|
}
|
|
|
|
void Function::typecheck(typecheck::State& state, typecheck::Scope& scope) {
|
|
auto return_ty = this->m_return_ty;
|
|
std::vector<std::shared_ptr<types::Type>> param_tys{};
|
|
for (auto& param : this->m_params) {
|
|
param_tys.push_back(param.second);
|
|
}
|
|
|
|
auto function_ty = new types::FunctionType{ return_ty, param_tys, this->m_is_vararg };
|
|
scope.symbols[this->m_name] = std::shared_ptr<types::Type>{ function_ty };
|
|
|
|
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) {
|
|
for (auto& statement : *this->m_statements) {
|
|
statement->typecheck(state, inner);
|
|
}
|
|
}
|
|
}
|
|
|
|
void TopLevelTypedef::typecheck_preprocess(typecheck::Scope& scope) {
|
|
if (this->m_ty->m_kind == types::TypeKind::Struct) {
|
|
auto struct_ty = dynamic_cast<types::StructType*>(this->m_ty.get());
|
|
if (struct_ty->m_is_ref || struct_ty->m_is_def) {
|
|
return;
|
|
}
|
|
if (struct_ty->m_name) {
|
|
scope.structs[*struct_ty->m_name] = this->m_ty;
|
|
}
|
|
}
|
|
}
|
|
|
|
void TopLevelTypedef::typecheck(typecheck::State&, typecheck::Scope& scope) {
|
|
if (this->m_ty->m_kind == types::TypeKind::Struct) {
|
|
auto struct_ty = dynamic_cast<types::StructType*>(this->m_ty.get());
|
|
if (struct_ty->m_is_ref || struct_ty->m_is_def) {
|
|
return;
|
|
}
|
|
if (struct_ty->m_name) {
|
|
scope.structs[*struct_ty->m_name] = this->m_ty;
|
|
}
|
|
}
|
|
}
|
|
} |