Provide access to scoped structs.

This commit is contained in:
chriseth 2015-10-06 12:35:10 +02:00
parent bc609c55c0
commit bf5b387954
8 changed files with 72 additions and 21 deletions

View File

@ -712,17 +712,17 @@ private:
class UserDefinedTypeName: public TypeName
{
public:
UserDefinedTypeName(SourceLocation const& _location, ASTPointer<ASTString> const& _name):
TypeName(_location), m_name(_name) {}
UserDefinedTypeName(SourceLocation const& _location, std::vector<ASTString> const& _namePath):
TypeName(_location), m_namePath(_namePath) {}
virtual void accept(ASTVisitor& _visitor) 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;
private:
ASTPointer<ASTString> m_name;
std::vector<ASTString> m_namePath;
};
/**

View File

@ -21,6 +21,7 @@
*/
#include <libsolidity/ASTJsonConverter.h>
#include <boost/algorithm/string/join.hpp>
#include <libsolidity/AST.h>
using namespace std;
@ -144,7 +145,9 @@ bool ASTJsonConverter::visit(ElementaryTypeName 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;
}

View File

@ -21,6 +21,7 @@
*/
#include <libsolidity/ASTPrinter.h>
#include <boost/algorithm/string/join.hpp>
#include <libsolidity/AST.h>
using namespace std;
@ -151,7 +152,7 @@ bool ASTPrinter::visit(ElementaryTypeName const& _node)
bool ASTPrinter::visit(UserDefinedTypeName const& _node)
{
writeLine("UserDefinedTypeName \"" + _node.name() + "\"");
writeLine("UserDefinedTypeName \"" + boost::algorithm::join(_node.namePath(), ".") + "\"");
printSourcePart(_node);
return goDeeper();
}

View File

@ -130,6 +130,22 @@ vector<Declaration const*> NameAndTypeResolver::nameFromCurrentScope(ASTString c
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(
Identifier const& _identifier,
vector<Declaration const*> const& _declarations

View File

@ -36,9 +36,8 @@ namespace solidity
{
/**
* Resolves name references, types and checks types of all expressions.
* Specifically, it checks that all operations are valid for the inferred types.
* An exception is throw on the first error.
* Resolves name references, typenames and sets the (explicitly given) types for all variable
* declarations.
*/
class NameAndTypeResolver: private boost::noncopyable
{
@ -61,6 +60,11 @@ public:
/// resolving phase.
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
static std::vector<Declaration const*> cleanedDeclarations(
Identifier const& _identifier,

View File

@ -522,7 +522,14 @@ ASTPointer<TypeName> Parser::parseTypeName(bool _allowVar)
{
ASTNodeFactory nodeFactory(*this);
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
BOOST_THROW_EXCEPTION(createParserError("Expected type name"));
@ -1036,7 +1043,7 @@ ASTPointer<TypeName> Parser::typeNameIndexAccessStructure(
ASTNodeFactory nodeFactory(*this, _primary);
ASTPointer<TypeName> type;
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()))
type = nodeFactory.createNode<ElementaryTypeName>(typeName->typeToken());
else

View File

@ -54,20 +54,13 @@ bool ReferencesResolver::visit(Return const& _return)
bool ReferencesResolver::visit(UserDefinedTypeName const& _typeName)
{
auto declarations = m_resolver.nameFromCurrentScope(_typeName.name());
if (declarations.empty())
Declaration const* declaration = m_resolver.pathFromCurrentScope(_typeName.namePath());
if (!declaration)
BOOST_THROW_EXCEPTION(
DeclarationError() <<
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;
return true;
}

View File

@ -5383,6 +5383,33 @@ BOOST_AUTO_TEST_CASE(internal_types_in_library)
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)
{
// This test verifies that the byte array encoding that combines length and data works