From 0e2fa39fadb9ccf45408e047c2b85d62847eb9f2 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 21 Dec 2015 18:44:21 +0100 Subject: [PATCH] Use paths instead of simple identifiers wherever possible. --- libsolidity/analysis/NameAndTypeResolver.cpp | 2 +- libsolidity/ast/AST.h | 12 ++++---- libsolidity/ast/ASTPrinter.cpp | 2 +- libsolidity/parsing/Parser.cpp | 32 +++++++++++--------- libsolidity/parsing/Parser.h | 1 + test/libsolidity/Imports.cpp | 2 +- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/libsolidity/analysis/NameAndTypeResolver.cpp b/libsolidity/analysis/NameAndTypeResolver.cpp index 92347bfc3..96ffdd6ee 100644 --- a/libsolidity/analysis/NameAndTypeResolver.cpp +++ b/libsolidity/analysis/NameAndTypeResolver.cpp @@ -264,7 +264,7 @@ void NameAndTypeResolver::linearizeBaseContracts(ContractDefinition& _contract) list> input(1, {}); for (ASTPointer const& baseSpecifier: _contract.baseContracts()) { - Identifier const& baseName = baseSpecifier->name(); + UserDefinedTypeName const& baseName = baseSpecifier->name(); auto base = dynamic_cast(baseName.annotation().referencedDeclaration); if (!base) reportFatalTypeError(baseName.createTypeError("Contract expected.")); diff --git a/libsolidity/ast/AST.h b/libsolidity/ast/AST.h index 84e9e7064..604a12a02 100644 --- a/libsolidity/ast/AST.h +++ b/libsolidity/ast/AST.h @@ -340,7 +340,7 @@ class InheritanceSpecifier: public ASTNode public: InheritanceSpecifier( SourceLocation const& _location, - ASTPointer const& _baseName, + ASTPointer const& _baseName, std::vector> _arguments ): ASTNode(_location), m_baseName(_baseName), m_arguments(_arguments) {} @@ -348,11 +348,11 @@ public: virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; - Identifier const& name() const { return *m_baseName; } + UserDefinedTypeName const& name() const { return *m_baseName; } std::vector> const& arguments() const { return m_arguments; } private: - ASTPointer m_baseName; + ASTPointer m_baseName; std::vector> m_arguments; }; @@ -366,7 +366,7 @@ class UsingForDirective: public ASTNode public: UsingForDirective( SourceLocation const& _location, - ASTPointer const& _libraryName, + ASTPointer const& _libraryName, ASTPointer const& _typeName ): ASTNode(_location), m_libraryName(_libraryName), m_typeName(_typeName) {} @@ -374,12 +374,12 @@ public: virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTConstVisitor& _visitor) const override; - Identifier const& libraryName() const { return *m_libraryName; } + UserDefinedTypeName const& libraryName() const { return *m_libraryName; } /// @returns the type name the library is attached to, null for `*`. TypeName const* typeName() const { return m_typeName.get(); } private: - ASTPointer m_libraryName; + ASTPointer m_libraryName; ASTPointer m_typeName; }; diff --git a/libsolidity/ast/ASTPrinter.cpp b/libsolidity/ast/ASTPrinter.cpp index b2ce1c262..d4f13e478 100644 --- a/libsolidity/ast/ASTPrinter.cpp +++ b/libsolidity/ast/ASTPrinter.cpp @@ -63,7 +63,7 @@ bool ASTPrinter::visit(ContractDefinition const& _node) bool ASTPrinter::visit(InheritanceSpecifier const& _node) { - writeLine("InheritanceSpecifier \"" + _node.name().name() + "\""); + writeLine("InheritanceSpecifier"); printSourcePart(_node); return goDeeper(); } diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index d53f825f8..7dd3564d0 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -238,7 +238,7 @@ ASTPointer Parser::parseContractDefinition(bool _isLibrary) ASTPointer Parser::parseInheritanceSpecifier() { ASTNodeFactory nodeFactory(*this); - ASTPointer name(parseIdentifier()); + ASTPointer name(parseUserDefinedTypeName()); vector> arguments; if (m_scanner->currentToken() == Token::LParen) { @@ -533,8 +533,7 @@ ASTPointer Parser::parseUsingDirective() ASTNodeFactory nodeFactory(*this); expectToken(Token::Using); - //@todo this should actually parse a full path. - ASTPointer library(parseIdentifier()); + ASTPointer library(parseUserDefinedTypeName()); ASTPointer typeName; expectToken(Token::For); if (m_scanner->currentToken() == Token::Mul) @@ -570,6 +569,20 @@ ASTPointer Parser::parseIdentifier() return nodeFactory.createNode(expectIdentifierToken()); } +ASTPointer Parser::parseUserDefinedTypeName() +{ + ASTNodeFactory nodeFactory(*this); + nodeFactory.markEndPosition(); + vector identifierPath{*expectIdentifierToken()}; + while (m_scanner->currentToken() == Token::Period) + { + m_scanner->next(); + nodeFactory.markEndPosition(); + identifierPath.push_back(*expectIdentifierToken()); + } + return nodeFactory.createNode(identifierPath); +} + ASTPointer Parser::parseTypeName(bool _allowVar) { ASTNodeFactory nodeFactory(*this); @@ -589,18 +602,7 @@ ASTPointer Parser::parseTypeName(bool _allowVar) else if (token == Token::Mapping) type = parseMapping(); else if (token == Token::Identifier) - { - ASTNodeFactory nodeFactory(*this); - nodeFactory.markEndPosition(); - vector identifierPath{*expectIdentifierToken()}; - while (m_scanner->currentToken() == Token::Period) - { - m_scanner->next(); - nodeFactory.markEndPosition(); - identifierPath.push_back(*expectIdentifierToken()); - } - type = nodeFactory.createNode(identifierPath); - } + type = parseUserDefinedTypeName(); else fatalParserError(string("Expected type name")); diff --git a/libsolidity/parsing/Parser.h b/libsolidity/parsing/Parser.h index 5ff462427..eb1aa5871 100644 --- a/libsolidity/parsing/Parser.h +++ b/libsolidity/parsing/Parser.h @@ -77,6 +77,7 @@ private: ASTPointer parseUsingDirective(); ASTPointer parseModifierInvocation(); ASTPointer parseIdentifier(); + ASTPointer parseUserDefinedTypeName(); ASTPointer parseTypeName(bool _allowVar); ASTPointer parseMapping(); ASTPointer parseParameterList( diff --git a/test/libsolidity/Imports.cpp b/test/libsolidity/Imports.cpp index 57239cb91..f0f67785f 100644 --- a/test/libsolidity/Imports.cpp +++ b/test/libsolidity/Imports.cpp @@ -97,7 +97,7 @@ BOOST_AUTO_TEST_CASE(simple_alias) { CompilerStack c; c.addSource("a", "contract A {}"); - c.addSource("dir/a/b/c", "import \"../../.././a\" as x; contract B { function() { x.A r = x.A(20); } }"); + c.addSource("dir/a/b/c", "import \"../../.././a\" as x; contract B is x.A { function() { x.A r = x.A(20); } }"); BOOST_CHECK(c.compile()); }