Skip to content

Commit b405fb9

Browse files
committed
Introduce typeWhenAttached helper
1 parent 20e97be commit b405fb9

4 files changed

Lines changed: 12 additions & 23 deletions

File tree

libsolidity/analysis/TypeChecker.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,12 +4052,7 @@ void TypeChecker::endVisit(UsingForDirective const& _usingFor)
40524052
FunctionDefinition const& functionDefinition =
40534053
dynamic_cast<FunctionDefinition const&>(*path->annotation().referencedDeclaration);
40544054

4055-
FunctionType const* functionType = dynamic_cast<FunctionType const*>(
4056-
functionDefinition.libraryFunction() ?
4057-
functionDefinition.typeViaContractName(Declaration::ContractNameAccessKind::Library) :
4058-
functionDefinition.type()
4059-
);
4060-
4055+
FunctionType const* functionType = dynamic_cast<FunctionType const*>(functionDefinition.typeWhenAttached());
40614056
solAssert(functionType);
40624057

40634058
if (functionDefinition.parameters().empty())

libsolidity/ast/AST.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,12 @@ Type const* FunctionDefinition::typeViaContractName(ContractNameAccessKind _acce
533533
util::unreachable();
534534
}
535535

536+
Type const* FunctionDefinition::typeWhenAttached() const
537+
{
538+
solAssert(isFree() || libraryFunction());
539+
return libraryFunction() ? typeViaContractName(ContractNameAccessKind::Library) : type();
540+
}
541+
536542
std::string FunctionDefinition::externalSignature() const
537543
{
538544
return TypeProvider::function(*this)->externalSignature();
@@ -979,11 +985,7 @@ FunctionType const* UnaryOperation::userDefinedFunctionType() const
979985
return nullptr;
980986

981987
FunctionDefinition const* userDefinedFunction = *annotation().userDefinedFunction;
982-
return dynamic_cast<FunctionType const*>(
983-
userDefinedFunction->libraryFunction() ?
984-
userDefinedFunction->typeViaContractName(Declaration::ContractNameAccessKind::Library) :
985-
userDefinedFunction->type()
986-
);
988+
return dynamic_cast<FunctionType const*>(userDefinedFunction->typeWhenAttached());
987989
}
988990

989991
FunctionType const* BinaryOperation::userDefinedFunctionType() const
@@ -992,11 +994,7 @@ FunctionType const* BinaryOperation::userDefinedFunctionType() const
992994
return nullptr;
993995

994996
FunctionDefinition const* userDefinedFunction = *annotation().userDefinedFunction;
995-
return dynamic_cast<FunctionType const*>(
996-
userDefinedFunction->libraryFunction() ?
997-
userDefinedFunction->typeViaContractName(Declaration::ContractNameAccessKind::Library) :
998-
userDefinedFunction->type()
999-
);
997+
return dynamic_cast<FunctionType const*>(userDefinedFunction->typeWhenAttached());
1000998
}
1001999

10021000
BinaryOperationAnnotation& BinaryOperation::annotation() const

libsolidity/ast/AST.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ class FunctionDefinition: public CallableDeclaration, public StructurallyDocumen
10621062

10631063
Type const* type() const override;
10641064
Type const* typeViaContractName(ContractNameAccessKind _accessKind) const override;
1065+
Type const* typeWhenAttached() const;
10651066

10661067
/// @param _internal false indicates external interface is concerned, true indicates internal interface is concerned.
10671068
/// @returns null when it is not accessible as a function.

libsolidity/ast/Types.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,7 @@ std::set<FunctionDefinition const*, ASTNode::CompareByID> Type::operatorDefiniti
403403
auto const& functionDefinition = dynamic_cast<FunctionDefinition const&>(
404404
*identifierPath->annotation().referencedDeclaration
405405
);
406-
auto const* functionType = dynamic_cast<FunctionType const*>(
407-
functionDefinition.libraryFunction() ?
408-
functionDefinition.typeViaContractName(Declaration::ContractNameAccessKind::Library) :
409-
functionDefinition.type()
410-
);
406+
auto const* functionType = dynamic_cast<FunctionType const*>(functionDefinition.typeWhenAttached());
411407
solAssert(functionType && !functionType->parameterTypes().empty());
412408

413409
size_t parameterCount = functionDefinition.parameterList().parameters().size();
@@ -427,8 +423,7 @@ MemberList::MemberMap Type::attachedFunctions(Type const& _type, ASTNode const&
427423
{
428424
if (!_name)
429425
_name = _function.name();
430-
Type const* functionType =
431-
_function.libraryFunction() ? _function.typeViaContractName(Declaration::ContractNameAccessKind::Library) : _function.type();
426+
Type const* functionType = _function.typeWhenAttached();
432427
solAssert(functionType, "");
433428
FunctionType const* withBoundFirstArgument =
434429
dynamic_cast<FunctionType const&>(*functionType).withBoundFirstArgument();

0 commit comments

Comments
 (0)