This commit is contained in:
Daniel Kirchner
2023-06-20 22:26:52 +02:00
parent 3249979969
commit 1c1110f734
7 changed files with 59 additions and 7 deletions
@@ -52,6 +52,7 @@ private:
bool visit(VariableDeclaration const&) override;
bool visit(ElementaryTypeName const&) override { return true; }
bool visit(ParameterList const&) override { return true; }
bool visit(Return const&) override { return true; }
langutil::ErrorReporter& m_errorReporter;
};
@@ -58,6 +58,7 @@ bool TypeInference::analyze(SourceUnit const& _sourceUnit)
bool TypeInference::visit(FunctionDefinition const& _functionDefinition)
{
ScopedSaveAndRestore signatureRestore(m_currentFunctionType, nullopt);
auto& functionAnnotation = annotation(_functionDefinition);
if (functionAnnotation.type)
return false;
@@ -66,8 +67,6 @@ bool TypeInference::visit(FunctionDefinition const& _functionDefinition)
if (_functionDefinition.returnParameterList())
_functionDefinition.returnParameterList()->accept(*this);
_functionDefinition.body().accept(*this);
auto typeFromParameterList = [&](ParameterList const* _list) {
if (!_list)
return m_typeSystem.builtinType(BuiltinType::Unit, {});
@@ -78,11 +77,17 @@ bool TypeInference::visit(FunctionDefinition const& _functionDefinition)
}) | ranges::to<std::vector<Type>>);
};
Type argType = typeFromParameterList(&_functionDefinition.parameterList());
Type resultType = typeFromParameterList(_functionDefinition.returnParameterList().get());
Type functionType = TypeSystemHelpers{m_typeSystem}.functionType(
typeFromParameterList(&_functionDefinition.parameterList()),
typeFromParameterList(_functionDefinition.returnParameterList().get())
);
m_currentFunctionType = functionType;
_functionDefinition.body().accept(*this);
functionAnnotation.type = m_typeSystem.fresh(
TypeSystemHelpers{m_typeSystem}.functionType(argType, resultType),
functionType,
true
);
@@ -91,6 +96,18 @@ bool TypeInference::visit(FunctionDefinition const& _functionDefinition)
return false;
}
void TypeInference::endVisit(Return const& _return)
{
solAssert(m_currentFunctionType);
if (_return.expression())
{
auto& returnExpressionAnnotation = annotation(*_return.expression());
solAssert(returnExpressionAnnotation.type);
Type functionReturnType = get<1>(TypeSystemHelpers{m_typeSystem}.destFunctionType(*m_currentFunctionType));
unify(functionReturnType, *returnExpressionAnnotation.type);
}
}
bool TypeInference::visit(ParameterList const&)
{
return true;
@@ -54,6 +54,8 @@ private:
bool visit(Identifier const&) override;
bool visit(FunctionCall const& _functionCall) override;
void endVisit(FunctionCall const& _functionCall) override;
bool visit(Return const&) override { return true; }
void endVisit(Return const& _return) override;
bool visitNode(ASTNode const& _node) override;
@@ -63,6 +65,7 @@ private:
TypeSystem m_typeSystem;
Type m_voidType;
Type m_wordType;
std::optional<Type> m_currentFunctionType;
struct TypeAnnotation
{