Provide access to scoped structs.

This commit is contained in:
chriseth
2015-10-06 14:20:06 +02:00
parent bc609c55c0
commit bf5b387954
8 changed files with 72 additions and 21 deletions
+4 -4
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;
};
/**
+4 -1
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;
}
+2 -1
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();
}
+16
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
+7 -3
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,
+9 -2
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
+3 -10
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;
}