From de0182299939c559904cc29da372087498809b8e Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Tue, 14 Sep 2021 17:31:00 +0200 Subject: [PATCH] UserDefinedValueType: from simple name to canonical name. --- libsolidity/analysis/NameAndTypeResolver.cpp | 41 ++++++++++---------- libsolidity/analysis/NameAndTypeResolver.h | 3 -- libsolidity/ast/AST.cpp | 5 +++ libsolidity/ast/AST.h | 2 + libsolidity/ast/Types.cpp | 2 +- 5 files changed, 28 insertions(+), 25 deletions(-) diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp index 001221bc5..74834a0cb 100644 --- a/libsolidity/analysis/NameAndTypeResolver.cpp +++ b/libsolidity/analysis/NameAndTypeResolver.cpp @@ -612,13 +612,31 @@ bool DeclarationRegistrationHelper::visitNode(ASTNode& _node) if (auto* declaration = dynamic_cast(&_node)) registerDeclaration(*declaration); + + if (auto* annotation = dynamic_cast(&_node.annotation())) + { + string canonicalName = dynamic_cast(_node).name(); + solAssert(!canonicalName.empty(), ""); + + for ( + ASTNode const* scope = m_currentScope; + scope != nullptr; + scope = m_scopes[scope]->enclosingNode() + ) + if (auto decl = dynamic_cast(scope)) + { + solAssert(!decl->name().empty(), ""); + canonicalName = decl->name() + "." + canonicalName; + } + + annotation->canonicalName = canonicalName; + } + if (dynamic_cast(&_node)) enterNewSubScope(_node); if (auto* variableScope = dynamic_cast(&_node)) m_currentFunction = variableScope; - if (auto* annotation = dynamic_cast(&_node.annotation())) - annotation->canonicalName = currentCanonicalName(); return true; } @@ -668,23 +686,4 @@ void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaratio solAssert(_declaration.annotation().contract == m_currentContract, ""); } -string DeclarationRegistrationHelper::currentCanonicalName() const -{ - string ret; - for ( - ASTNode const* scope = m_currentScope; - scope != nullptr; - scope = m_scopes[scope]->enclosingNode() - ) - { - if (auto decl = dynamic_cast(scope)) - { - if (!ret.empty()) - ret = "." + ret; - ret = decl->name() + ret; - } - } - return ret; -} - } diff --git a/libsolidity/analysis/NameAndTypeResolver.h b/libsolidity/analysis/NameAndTypeResolver.h index 919e84a66..36a22ed2e 100644 --- a/libsolidity/analysis/NameAndTypeResolver.h +++ b/libsolidity/analysis/NameAndTypeResolver.h @@ -176,9 +176,6 @@ private: static bool isOverloadedFunction(Declaration const& _declaration1, Declaration const& _declaration2); - /// @returns the canonical name of the current scope. - std::string currentCanonicalName() const; - std::map>& m_scopes; ASTNode const* m_currentScope = nullptr; VariableScope* m_currentFunction = nullptr; diff --git a/libsolidity/ast/AST.cpp b/libsolidity/ast/AST.cpp index 3201d60ff..3e2376a4f 100644 --- a/libsolidity/ast/AST.cpp +++ b/libsolidity/ast/AST.cpp @@ -341,6 +341,11 @@ Type const* UserDefinedValueTypeDefinition::type() const return TypeProvider::typeType(TypeProvider::userDefinedValueType(*this)); } +TypeDeclarationAnnotation& UserDefinedValueTypeDefinition::annotation() const +{ + return initAnnotation(); +} + Type const* StructDefinition::type() const { solAssert(annotation().recursive.has_value(), "Requested struct type before DeclarationTypeChecker."); diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index dce384cdf..c229252a8 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -750,6 +750,8 @@ public: Type const* type() const override; + TypeDeclarationAnnotation& annotation() const override; + TypeName const* underlyingType() const { return m_underlyingType.get(); } bool isVisibleViaContractTypeAccess() const override { return true; } diff --git a/libsolidity/ast/Types.cpp b/libsolidity/ast/Types.cpp index da6f7d196..467d48c5b 100644 --- a/libsolidity/ast/Types.cpp +++ b/libsolidity/ast/Types.cpp @@ -2555,7 +2555,7 @@ bool UserDefinedValueType::operator==(Type const& _other) const string UserDefinedValueType::toString(bool /* _short */) const { - return "user defined type " + definition().name(); + return "user defined type " + *definition().annotation().canonicalName; } vector> UserDefinedValueType::makeStackItems() const