From de0182299939c559904cc29da372087498809b8e Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Tue, 14 Sep 2021 17:31:00 +0200 Subject: [PATCH 1/2] 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 From 42c8310a6382f76af9c299574d0b34cdcc6e944d Mon Sep 17 00:00:00 2001 From: hrkrshnn Date: Tue, 14 Sep 2021 17:36:18 +0200 Subject: [PATCH 2/2] Updated tests. --- test/libsolidity/ABIJson/user_defined_value_type.sol | 12 ++++++------ test/libsolidity/ASTJSON/userDefinedValueType.json | 8 ++++---- .../wrap_unwrap_via_contract_name_different.sol | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/test/libsolidity/ABIJson/user_defined_value_type.sol b/test/libsolidity/ABIJson/user_defined_value_type.sol index da10f7c44..b5e565dbb 100644 --- a/test/libsolidity/ABIJson/user_defined_value_type.sol +++ b/test/libsolidity/ABIJson/user_defined_value_type.sol @@ -36,7 +36,7 @@ contract C { // "outputs": // [ // { -// "internalType": "user defined type MyAddress", +// "internalType": "user defined type C.MyAddress", // "name": "", // "type": "address" // } @@ -64,7 +64,7 @@ contract C { // "outputs": // [ // { -// "internalType": "user defined type MyBytes32", +// "internalType": "user defined type C.MyBytes32", // "name": "", // "type": "bytes32" // } @@ -92,7 +92,7 @@ contract C { // "outputs": // [ // { -// "internalType": "user defined type MyUInt8", +// "internalType": "user defined type C.MyUInt8", // "name": "", // "type": "uint8" // } @@ -104,7 +104,7 @@ contract C { // "inputs": // [ // { -// "internalType": "user defined type MyAddress", +// "internalType": "user defined type C.MyAddress", // "name": "a", // "type": "address" // } @@ -132,7 +132,7 @@ contract C { // "inputs": // [ // { -// "internalType": "user defined type MyBytes32", +// "internalType": "user defined type C.MyBytes32", // "name": "a", // "type": "bytes32" // } @@ -160,7 +160,7 @@ contract C { // "inputs": // [ // { -// "internalType": "user defined type MyUInt8", +// "internalType": "user defined type C.MyUInt8", // "name": "a", // "type": "uint8" // } diff --git a/test/libsolidity/ASTJSON/userDefinedValueType.json b/test/libsolidity/ASTJSON/userDefinedValueType.json index f929122b1..4f8dc9306 100644 --- a/test/libsolidity/ASTJSON/userDefinedValueType.json +++ b/test/libsolidity/ASTJSON/userDefinedValueType.json @@ -269,7 +269,7 @@ "typeDescriptions": { "typeIdentifier": "t_mapping$_t_userDefinedValueType$_MyAddress_$18_$_t_userDefinedValueType$_MyUInt_$20_$", - "typeString": "mapping(user defined type MyAddress => user defined type MyUInt)" + "typeString": "mapping(user defined type C.MyAddress => user defined type C.MyUInt)" }, "typeName": { @@ -291,7 +291,7 @@ "typeDescriptions": { "typeIdentifier": "t_userDefinedValueType$_MyAddress_$18", - "typeString": "user defined type MyAddress" + "typeString": "user defined type C.MyAddress" } }, "nodeType": "Mapping", @@ -299,7 +299,7 @@ "typeDescriptions": { "typeIdentifier": "t_mapping$_t_userDefinedValueType$_MyAddress_$18_$_t_userDefinedValueType$_MyUInt_$20_$", - "typeString": "mapping(user defined type MyAddress => user defined type MyUInt)" + "typeString": "mapping(user defined type C.MyAddress => user defined type C.MyUInt)" }, "valueType": { @@ -318,7 +318,7 @@ "typeDescriptions": { "typeIdentifier": "t_userDefinedValueType$_MyUInt_$20", - "typeString": "user defined type MyUInt" + "typeString": "user defined type C.MyUInt" } } }, diff --git a/test/libsolidity/syntaxTests/userDefinedValueType/wrap_unwrap_via_contract_name_different.sol b/test/libsolidity/syntaxTests/userDefinedValueType/wrap_unwrap_via_contract_name_different.sol index f983a9d20..64d19c627 100644 --- a/test/libsolidity/syntaxTests/userDefinedValueType/wrap_unwrap_via_contract_name_different.sol +++ b/test/libsolidity/syntaxTests/userDefinedValueType/wrap_unwrap_via_contract_name_different.sol @@ -5,4 +5,4 @@ contract D C.T x = L.T.wrap(uint(1)); } // ---- -// TypeError 7407: (86-103): Type user defined type T is not implicitly convertible to expected type user defined type T. +// TypeError 7407: (86-103): Type user defined type L.T is not implicitly convertible to expected type user defined type C.T.