Skip to content

Commit

Permalink
Merge pull request #14730 from ethereum/new-analysis-rename-type-anno…
Browse files Browse the repository at this point in the history
…tation-to-type

Rename `TypeInference::typeAnnotation()` back to `type()`
  • Loading branch information
cameel authored Dec 13, 2023
2 parents 2414fc7 + 602c6e2 commit f2dbadb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
58 changes: 29 additions & 29 deletions libsolidity/experimental/analysis/TypeInference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ bool TypeInference::visit(FunctionDefinition const& _functionDefinition)


_functionDefinition.parameterList().accept(*this);
unify(argumentsType, typeAnnotation(_functionDefinition.parameterList()), _functionDefinition.parameterList().location());
unify(argumentsType, type(_functionDefinition.parameterList()), _functionDefinition.parameterList().location());
if (_functionDefinition.experimentalReturnExpression())
{
ScopedSaveAndRestore expressionContext{m_expressionContext, ExpressionContext::Type};
_functionDefinition.experimentalReturnExpression()->accept(*this);
unify(
returnType,
typeAnnotation(*_functionDefinition.experimentalReturnExpression()),
type(*_functionDefinition.experimentalReturnExpression()),
_functionDefinition.experimentalReturnExpression()->location()
);
}
Expand All @@ -171,7 +171,7 @@ void TypeInference::endVisit(Return const& _return)
solAssert(m_currentFunctionType);
Type functionReturnType = std::get<1>(TypeSystemHelpers{m_typeSystem}.destFunctionType(*m_currentFunctionType));
if (_return.expression())
unify(functionReturnType, typeAnnotation(*_return.expression()), _return.location());
unify(functionReturnType, type(*_return.expression()), _return.location());
else
unify(functionReturnType, m_unitType, _return.location());
}
Expand All @@ -181,7 +181,7 @@ void TypeInference::endVisit(ParameterList const& _parameterList)
auto& listAnnotation = annotation(_parameterList);
solAssert(!listAnnotation.type);
listAnnotation.type = TypeSystemHelpers{m_typeSystem}.tupleType(
_parameterList.parameters() | ranges::views::transform([&](auto _arg) { return typeAnnotation(*_arg); }) | ranges::to<std::vector<Type>>
_parameterList.parameters() | ranges::views::transform([&](auto _arg) { return type(*_arg); }) | ranges::to<std::vector<Type>>
);
}

Expand Down Expand Up @@ -211,7 +211,7 @@ bool TypeInference::visit(TypeClassDefinition const& _typeClassDefinition)
subNode->accept(*this);
auto const* functionDefinition = dynamic_cast<FunctionDefinition const*>(subNode.get());
solAssert(functionDefinition);
auto functionType = typeAnnotation(*functionDefinition);
auto functionType = type(*functionDefinition);
if (!functionTypes.emplace(functionDefinition->name(), functionType).second)
m_errorReporter.fatalTypeError(3195_error, functionDefinition->location(), "Function in type class declared multiple times.");
auto typeVars = TypeEnvironmentHelpers{*m_env}.typeVars(functionType);
Expand All @@ -235,7 +235,7 @@ bool TypeInference::visit(TypeClassDefinition const& _typeClassDefinition)
m_errorReporter.typeError(1807_error, _typeClassDefinition.location(), "Function " + functionName + " depends on invalid type variable.");
}

unify(typeAnnotation(_typeClassDefinition.typeVariable()), m_typeSystem.freshTypeVariable({{typeClass}}), _typeClassDefinition.location());
unify(type(_typeClassDefinition.typeVariable()), m_typeSystem.freshTypeVariable({{typeClass}}), _typeClassDefinition.location());
for (auto instantiation: m_analysis.annotation<TypeRegistration>(_typeClassDefinition).instantiations | ranges::views::values)
// TODO: recursion-safety? Order of instantiation?
instantiation->accept(*this);
Expand Down Expand Up @@ -268,7 +268,7 @@ bool TypeInference::visit(InlineAssembly const& _inlineAssembly)
solAssert(!!declaration, "");
solAssert(identifierInfo->suffix == "", "");

unify(typeAnnotation(*declaration), m_wordType, originLocationOf(_identifier));
unify(type(*declaration), m_wordType, originLocationOf(_identifier));
identifierInfo->valueSize = 1;
return true;
};
Expand Down Expand Up @@ -302,7 +302,7 @@ bool TypeInference::visit(BinaryOperation const& _binaryOperation)
_binaryOperation.leftExpression().accept(*this);
_binaryOperation.rightExpression().accept(*this);

Type argTuple = helper.tupleType({typeAnnotation(_binaryOperation.leftExpression()), typeAnnotation(_binaryOperation.rightExpression())});
Type argTuple = helper.tupleType({type(_binaryOperation.leftExpression()), type(_binaryOperation.rightExpression())});
Type resultType = m_typeSystem.freshTypeVariable({});
Type genericFunctionType = helper.functionType(argTuple, resultType);
unify(functionType, genericFunctionType, _binaryOperation.location());
Expand All @@ -316,8 +316,8 @@ bool TypeInference::visit(BinaryOperation const& _binaryOperation)
ScopedSaveAndRestore expressionContext{m_expressionContext, ExpressionContext::Type};
_binaryOperation.rightExpression().accept(*this);
}
Type leftType = typeAnnotation(_binaryOperation.leftExpression());
unify(leftType, typeAnnotation(_binaryOperation.rightExpression()), _binaryOperation.location());
Type leftType = type(_binaryOperation.leftExpression());
unify(leftType, type(_binaryOperation.rightExpression()), _binaryOperation.location());
operationAnnotation.type = leftType;
}
else
Expand All @@ -334,21 +334,21 @@ bool TypeInference::visit(BinaryOperation const& _binaryOperation)
ScopedSaveAndRestore expressionContext{m_expressionContext, ExpressionContext::Sort};
_binaryOperation.rightExpression().accept(*this);
}
Type leftType = typeAnnotation(_binaryOperation.leftExpression());
unify(leftType, typeAnnotation(_binaryOperation.rightExpression()), _binaryOperation.location());
Type leftType = type(_binaryOperation.leftExpression());
unify(leftType, type(_binaryOperation.rightExpression()), _binaryOperation.location());
operationAnnotation.type = leftType;
}
else if (_binaryOperation.getOperator() == Token::RightArrow)
{
_binaryOperation.leftExpression().accept(*this);
_binaryOperation.rightExpression().accept(*this);
operationAnnotation.type = helper.functionType(typeAnnotation(_binaryOperation.leftExpression()), typeAnnotation(_binaryOperation.rightExpression()));
operationAnnotation.type = helper.functionType(type(_binaryOperation.leftExpression()), type(_binaryOperation.rightExpression()));
}
else if (_binaryOperation.getOperator() == Token::BitOr)
{
_binaryOperation.leftExpression().accept(*this);
_binaryOperation.rightExpression().accept(*this);
operationAnnotation.type = helper.sumType({typeAnnotation(_binaryOperation.leftExpression()), typeAnnotation(_binaryOperation.rightExpression())});
operationAnnotation.type = helper.sumType({type(_binaryOperation.leftExpression()), type(_binaryOperation.rightExpression())});
}
else
{
Expand All @@ -372,9 +372,9 @@ void TypeInference::endVisit(VariableDeclarationStatement const& _variableDeclar
m_errorReporter.typeError(2655_error, _variableDeclarationStatement.location(), "Multi variable declaration not supported.");
return;
}
Type variableType = typeAnnotation(*_variableDeclarationStatement.declarations().front());
Type variableType = type(*_variableDeclarationStatement.declarations().front());
if (_variableDeclarationStatement.initialValue())
unify(variableType, typeAnnotation(*_variableDeclarationStatement.initialValue()), _variableDeclarationStatement.location());
unify(variableType, type(*_variableDeclarationStatement.initialValue()), _variableDeclarationStatement.location());
}

bool TypeInference::visit(VariableDeclaration const& _variableDeclaration)
Expand All @@ -390,7 +390,7 @@ bool TypeInference::visit(VariableDeclaration const& _variableDeclaration)
{
ScopedSaveAndRestore expressionContext{m_expressionContext, ExpressionContext::Type};
_variableDeclaration.typeExpression()->accept(*this);
variableAnnotation.type = typeAnnotation(*_variableDeclaration.typeExpression());
variableAnnotation.type = type(*_variableDeclaration.typeExpression());
return false;
}
variableAnnotation.type = m_typeSystem.freshTypeVariable({});
Expand All @@ -401,7 +401,7 @@ bool TypeInference::visit(VariableDeclaration const& _variableDeclaration)
{
ScopedSaveAndRestore expressionContext{m_expressionContext, ExpressionContext::Sort};
_variableDeclaration.typeExpression()->accept(*this);
unify(*variableAnnotation.type, typeAnnotation(*_variableDeclaration.typeExpression()), _variableDeclaration.typeExpression()->location());
unify(*variableAnnotation.type, type(*_variableDeclaration.typeExpression()), _variableDeclaration.typeExpression()->location());
}
return false;
case ExpressionContext::Sort:
Expand All @@ -424,7 +424,7 @@ void TypeInference::endVisit(IfStatement const& _ifStatement)
return;
}

unify(typeAnnotation(_ifStatement.condition()), m_boolType, _ifStatement.condition().location());
unify(type(_ifStatement.condition()), m_boolType, _ifStatement.condition().location());

ifAnnotation.type = m_unitType;
}
Expand All @@ -441,8 +441,8 @@ void TypeInference::endVisit(Assignment const& _assignment)
return;
}

Type leftType = typeAnnotation(_assignment.leftHandSide());
unify(leftType, typeAnnotation(_assignment.rightHandSide()), _assignment.location());
Type leftType = type(_assignment.leftHandSide());
unify(leftType, type(_assignment.rightHandSide()), _assignment.location());
assignmentAnnotation.type = leftType;
}

Expand Down Expand Up @@ -676,7 +676,7 @@ bool TypeInference::visit(TypeClassInstantiation const& _typeClassInstantiation)
}
}

Type type{TypeConstant{*typeConstructor, arguments}};
Type instanceType{TypeConstant{*typeConstructor, arguments}};

std::map<std::string, Type> functionTypes;

Expand All @@ -685,17 +685,17 @@ bool TypeInference::visit(TypeClassInstantiation const& _typeClassInstantiation)
auto const* functionDefinition = dynamic_cast<FunctionDefinition const*>(subNode.get());
solAssert(functionDefinition);
subNode->accept(*this);
if (!functionTypes.emplace(functionDefinition->name(), typeAnnotation(*functionDefinition)).second)
if (!functionTypes.emplace(functionDefinition->name(), type(*functionDefinition)).second)
m_errorReporter.typeError(3654_error, subNode->location(), "Duplicate definition of function " + functionDefinition->name() + " during type class instantiation.");
}

if (auto error = m_typeSystem.instantiateClass(type, arity))
if (auto error = m_typeSystem.instantiateClass(instanceType, arity))
m_errorReporter.typeError(5094_error, _typeClassInstantiation.location(), *error);

auto const& classFunctions = annotation().typeClassFunctions.at(*typeClass);

TypeEnvironment newEnv = m_env->clone();
if (!newEnv.unify(m_typeSystem.typeClassVariable(*typeClass), type).empty())
if (!newEnv.unify(m_typeSystem.typeClassVariable(*typeClass), instanceType).empty())
{
m_errorReporter.typeError(4686_error, _typeClassInstantiation.location(), "Unification of class and instance variable failed.");
return false;
Expand Down Expand Up @@ -758,7 +758,7 @@ void TypeInference::endVisit(MemberAccess const& _memberAccess)
{
auto& memberAccessAnnotation = annotation(_memberAccess);
solAssert(!memberAccessAnnotation.type);
Type expressionType = typeAnnotation(_memberAccess.expression());
Type expressionType = type(_memberAccess.expression());
memberAccessAnnotation.type = memberType(expressionType, _memberAccess.memberName(), _memberAccess.location());
}

Expand Down Expand Up @@ -824,7 +824,7 @@ void TypeInference::endVisit(FunctionCall const& _functionCall)
auto& functionCallAnnotation = annotation(_functionCall);
solAssert(!functionCallAnnotation.type);

Type functionType = typeAnnotation(_functionCall.expression());
Type functionType = type(_functionCall.expression());

TypeSystemHelpers helper{m_typeSystem};
std::vector<Type> argTypes;
Expand All @@ -834,7 +834,7 @@ void TypeInference::endVisit(FunctionCall const& _functionCall)
{
case ExpressionContext::Term:
case ExpressionContext::Type:
argTypes.emplace_back(typeAnnotation(*arg));
argTypes.emplace_back(type(*arg));
break;
case ExpressionContext::Sort:
m_errorReporter.typeError(9173_error, _functionCall.location(), "Function call in sort context.");
Expand Down Expand Up @@ -1171,7 +1171,7 @@ void TypeInference::unify(Type _a, Type _b, langutil::SourceLocation _location)
}
}

experimental::Type TypeInference::typeAnnotation(ASTNode const& _node) const
experimental::Type TypeInference::type(ASTNode const& _node) const
{
auto result = annotation(_node).type;
solAssert(result);
Expand Down
2 changes: 1 addition & 1 deletion libsolidity/experimental/analysis/TypeInference.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class TypeInference: public ASTConstVisitor
Type m_boolType;
std::optional<Type> m_currentFunctionType;

Type typeAnnotation(ASTNode const& _node) const;
Type type(ASTNode const& _node) const;

Annotation& annotation(ASTNode const& _node);
Annotation const& annotation(ASTNode const& _node) const;
Expand Down

0 comments on commit f2dbadb

Please sign in to comment.