mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Provide access to scoped structs.
This commit is contained in:
parent
bc609c55c0
commit
bf5b387954
@ -712,17 +712,17 @@ private:
|
|||||||
class UserDefinedTypeName: public TypeName
|
class UserDefinedTypeName: public TypeName
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UserDefinedTypeName(SourceLocation const& _location, ASTPointer<ASTString> const& _name):
|
UserDefinedTypeName(SourceLocation const& _location, std::vector<ASTString> const& _namePath):
|
||||||
TypeName(_location), m_name(_name) {}
|
TypeName(_location), m_namePath(_namePath) {}
|
||||||
virtual void accept(ASTVisitor& _visitor) override;
|
virtual void accept(ASTVisitor& _visitor) override;
|
||||||
virtual void accept(ASTConstVisitor& _visitor) const override;
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
||||||
|
|
||||||
ASTString const& name() const { return *m_name; }
|
std::vector<ASTString> const& namePath() const { return m_namePath; }
|
||||||
|
|
||||||
virtual UserDefinedTypeNameAnnotation& annotation() const override;
|
virtual UserDefinedTypeNameAnnotation& annotation() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ASTPointer<ASTString> m_name;
|
std::vector<ASTString> m_namePath;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <libsolidity/ASTJsonConverter.h>
|
#include <libsolidity/ASTJsonConverter.h>
|
||||||
|
#include <boost/algorithm/string/join.hpp>
|
||||||
#include <libsolidity/AST.h>
|
#include <libsolidity/AST.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -144,7 +145,9 @@ bool ASTJsonConverter::visit(ElementaryTypeName const& _node)
|
|||||||
|
|
||||||
bool ASTJsonConverter::visit(UserDefinedTypeName const& _node)
|
bool ASTJsonConverter::visit(UserDefinedTypeName const& _node)
|
||||||
{
|
{
|
||||||
addJsonNode("UserDefinedTypeName", { make_pair("name", _node.name()) });
|
addJsonNode("UserDefinedTypeName", {
|
||||||
|
make_pair("name", boost::algorithm::join(_node.namePath(), "."))
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <libsolidity/ASTPrinter.h>
|
#include <libsolidity/ASTPrinter.h>
|
||||||
|
#include <boost/algorithm/string/join.hpp>
|
||||||
#include <libsolidity/AST.h>
|
#include <libsolidity/AST.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -151,7 +152,7 @@ bool ASTPrinter::visit(ElementaryTypeName const& _node)
|
|||||||
|
|
||||||
bool ASTPrinter::visit(UserDefinedTypeName const& _node)
|
bool ASTPrinter::visit(UserDefinedTypeName const& _node)
|
||||||
{
|
{
|
||||||
writeLine("UserDefinedTypeName \"" + _node.name() + "\"");
|
writeLine("UserDefinedTypeName \"" + boost::algorithm::join(_node.namePath(), ".") + "\"");
|
||||||
printSourcePart(_node);
|
printSourcePart(_node);
|
||||||
return goDeeper();
|
return goDeeper();
|
||||||
}
|
}
|
||||||
|
@ -130,6 +130,22 @@ vector<Declaration const*> NameAndTypeResolver::nameFromCurrentScope(ASTString c
|
|||||||
return m_currentScope->resolveName(_name, _recursive);
|
return m_currentScope->resolveName(_name, _recursive);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Declaration const* NameAndTypeResolver::pathFromCurrentScope(vector<ASTString> const& _path, bool _recursive)
|
||||||
|
{
|
||||||
|
solAssert(!_path.empty(), "");
|
||||||
|
vector<Declaration const*> candidates = m_currentScope->resolveName(_path.front(), _recursive);
|
||||||
|
for (size_t i = 1; i < _path.size() && candidates.size() == 1; i++)
|
||||||
|
{
|
||||||
|
if (!m_scopes.count(candidates.front()))
|
||||||
|
return nullptr;
|
||||||
|
candidates = m_scopes.at(candidates.front()).resolveName(_path[i], false);
|
||||||
|
}
|
||||||
|
if (candidates.size() == 1)
|
||||||
|
return candidates.front();
|
||||||
|
else
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
vector<Declaration const*> NameAndTypeResolver::cleanedDeclarations(
|
vector<Declaration const*> NameAndTypeResolver::cleanedDeclarations(
|
||||||
Identifier const& _identifier,
|
Identifier const& _identifier,
|
||||||
vector<Declaration const*> const& _declarations
|
vector<Declaration const*> const& _declarations
|
||||||
|
@ -36,9 +36,8 @@ namespace solidity
|
|||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves name references, types and checks types of all expressions.
|
* Resolves name references, typenames and sets the (explicitly given) types for all variable
|
||||||
* Specifically, it checks that all operations are valid for the inferred types.
|
* declarations.
|
||||||
* An exception is throw on the first error.
|
|
||||||
*/
|
*/
|
||||||
class NameAndTypeResolver: private boost::noncopyable
|
class NameAndTypeResolver: private boost::noncopyable
|
||||||
{
|
{
|
||||||
@ -61,6 +60,11 @@ public:
|
|||||||
/// resolving phase.
|
/// resolving phase.
|
||||||
std::vector<Declaration const*> nameFromCurrentScope(ASTString const& _name, bool _recursive = true);
|
std::vector<Declaration const*> nameFromCurrentScope(ASTString const& _name, bool _recursive = true);
|
||||||
|
|
||||||
|
/// Resolves a path starting from the "current" scope. Should only be called during the initial
|
||||||
|
/// resolving phase.
|
||||||
|
/// @note Returns a null pointer if any component in the path was not unique or not found.
|
||||||
|
Declaration const* pathFromCurrentScope(std::vector<ASTString> const& _path, bool _recursive = true);
|
||||||
|
|
||||||
/// returns the vector of declarations without repetitions
|
/// returns the vector of declarations without repetitions
|
||||||
static std::vector<Declaration const*> cleanedDeclarations(
|
static std::vector<Declaration const*> cleanedDeclarations(
|
||||||
Identifier const& _identifier,
|
Identifier const& _identifier,
|
||||||
|
@ -522,7 +522,14 @@ ASTPointer<TypeName> Parser::parseTypeName(bool _allowVar)
|
|||||||
{
|
{
|
||||||
ASTNodeFactory nodeFactory(*this);
|
ASTNodeFactory nodeFactory(*this);
|
||||||
nodeFactory.markEndPosition();
|
nodeFactory.markEndPosition();
|
||||||
type = nodeFactory.createNode<UserDefinedTypeName>(expectIdentifierToken());
|
vector<ASTString> identifierPath{*expectIdentifierToken()};
|
||||||
|
while (m_scanner->currentToken() == Token::Period)
|
||||||
|
{
|
||||||
|
m_scanner->next();
|
||||||
|
nodeFactory.markEndPosition();
|
||||||
|
identifierPath.push_back(*expectIdentifierToken());
|
||||||
|
}
|
||||||
|
type = nodeFactory.createNode<UserDefinedTypeName>(identifierPath);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
BOOST_THROW_EXCEPTION(createParserError("Expected type name"));
|
BOOST_THROW_EXCEPTION(createParserError("Expected type name"));
|
||||||
@ -1036,7 +1043,7 @@ ASTPointer<TypeName> Parser::typeNameIndexAccessStructure(
|
|||||||
ASTNodeFactory nodeFactory(*this, _primary);
|
ASTNodeFactory nodeFactory(*this, _primary);
|
||||||
ASTPointer<TypeName> type;
|
ASTPointer<TypeName> type;
|
||||||
if (auto identifier = dynamic_cast<Identifier const*>(_primary.get()))
|
if (auto identifier = dynamic_cast<Identifier const*>(_primary.get()))
|
||||||
type = nodeFactory.createNode<UserDefinedTypeName>(make_shared<ASTString>(identifier->name()));
|
type = nodeFactory.createNode<UserDefinedTypeName>(vector<ASTString>{identifier->name()});
|
||||||
else if (auto typeName = dynamic_cast<ElementaryTypeNameExpression const*>(_primary.get()))
|
else if (auto typeName = dynamic_cast<ElementaryTypeNameExpression const*>(_primary.get()))
|
||||||
type = nodeFactory.createNode<ElementaryTypeName>(typeName->typeToken());
|
type = nodeFactory.createNode<ElementaryTypeName>(typeName->typeToken());
|
||||||
else
|
else
|
||||||
|
@ -54,20 +54,13 @@ bool ReferencesResolver::visit(Return const& _return)
|
|||||||
|
|
||||||
bool ReferencesResolver::visit(UserDefinedTypeName const& _typeName)
|
bool ReferencesResolver::visit(UserDefinedTypeName const& _typeName)
|
||||||
{
|
{
|
||||||
auto declarations = m_resolver.nameFromCurrentScope(_typeName.name());
|
Declaration const* declaration = m_resolver.pathFromCurrentScope(_typeName.namePath());
|
||||||
if (declarations.empty())
|
if (!declaration)
|
||||||
BOOST_THROW_EXCEPTION(
|
BOOST_THROW_EXCEPTION(
|
||||||
DeclarationError() <<
|
DeclarationError() <<
|
||||||
errinfo_sourceLocation(_typeName.location()) <<
|
errinfo_sourceLocation(_typeName.location()) <<
|
||||||
errinfo_comment("Undeclared identifier.")
|
errinfo_comment("Identifier not found or not unique.")
|
||||||
);
|
);
|
||||||
else if (declarations.size() > 1)
|
|
||||||
BOOST_THROW_EXCEPTION(
|
|
||||||
DeclarationError() <<
|
|
||||||
errinfo_sourceLocation(_typeName.location()) <<
|
|
||||||
errinfo_comment("Duplicate identifier.")
|
|
||||||
);
|
|
||||||
Declaration const* declaration = *declarations.begin();
|
|
||||||
_typeName.annotation().referencedDeclaration = declaration;
|
_typeName.annotation().referencedDeclaration = declaration;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -5383,6 +5383,33 @@ BOOST_AUTO_TEST_CASE(internal_types_in_library)
|
|||||||
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(4), u256(17)));
|
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(4), u256(17)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(using_library_structs)
|
||||||
|
{
|
||||||
|
char const* sourceCode = R"(
|
||||||
|
library Lib {
|
||||||
|
struct Data { uint a; uint[] b; }
|
||||||
|
function set(Data storage _s)
|
||||||
|
{
|
||||||
|
_s.a = 7;
|
||||||
|
_s.b.length = 20;
|
||||||
|
_s.b[19] = 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contract Test {
|
||||||
|
mapping(string => Lib.Data) data;
|
||||||
|
function f() returns (uint a, uint b)
|
||||||
|
{
|
||||||
|
Lib.set(data["abc"]);
|
||||||
|
a = data["abc"].a;
|
||||||
|
b = data["abc"].b[19];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
compileAndRun(sourceCode, 0, "Lib");
|
||||||
|
compileAndRun(sourceCode, 0, "Test", bytes(), map<string, Address>{{"Lib", m_contractAddress}});
|
||||||
|
BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(7), u256(8)));
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(short_strings)
|
BOOST_AUTO_TEST_CASE(short_strings)
|
||||||
{
|
{
|
||||||
// This test verifies that the byte array encoding that combines length and data works
|
// This test verifies that the byte array encoding that combines length and data works
|
||||||
|
Loading…
Reference in New Issue
Block a user