Skip to content

Commit f447e1f

Browse files
committed
Introduce typeWhenAttached helper
1 parent afe04e5 commit f447e1f

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
@@ -532,6 +532,12 @@ Type const* FunctionDefinition::typeViaContractName(ContractNameAccessKind _acce
532532
}
533533
}
534534

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

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

988990
FunctionType const* BinaryOperation::userDefinedFunctionType() const
@@ -991,11 +993,7 @@ FunctionType const* BinaryOperation::userDefinedFunctionType() const
991993
return nullptr;
992994

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

1001999
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)