From 7cb7818ceaf8f2e50cdd66b33a4e0d17c2a0e879 Mon Sep 17 00:00:00 2001 From: chriseth Date: Sat, 5 Dec 2015 03:09:47 +0100 Subject: Source units are independent scopes. --- test/libsolidity/Imports.cpp | 83 +++++++++++++++++++++++++ test/libsolidity/SolidityExpressionCompiler.cpp | 25 +++++--- 2 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 test/libsolidity/Imports.cpp (limited to 'test/libsolidity') diff --git a/test/libsolidity/Imports.cpp b/test/libsolidity/Imports.cpp new file mode 100644 index 00000000..b5ff0c54 --- /dev/null +++ b/test/libsolidity/Imports.cpp @@ -0,0 +1,83 @@ +/* + This file is part of cpp-ethereum. + + cpp-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + cpp-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with cpp-ethereum. If not, see . +*/ +/** + * @author Christian + * @date 2015 + * Tests for high level features like import. + */ + +#include +#include +#include +#include + +using namespace std; + +namespace dev +{ +namespace solidity +{ +namespace test +{ + +BOOST_AUTO_TEST_SUITE(SolidityImports) + +BOOST_AUTO_TEST_CASE(smoke_test) +{ + CompilerStack c; + c.addSource("a", "contract C {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(regular_import) +{ + CompilerStack c; + c.addSource("a", "contract C {}"); + c.addSource("b", "import \"a\"; contract D is C {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(import_does_not_clutter_importee) +{ + CompilerStack c; + c.addSource("a", "contract C { D d; }"); + c.addSource("b", "import \"a\"; contract D is C {}"); + BOOST_CHECK(!c.compile()); +} + +BOOST_AUTO_TEST_CASE(import_is_transitive) +{ + CompilerStack c; + c.addSource("a", "contract C { }"); + c.addSource("b", "import \"a\";"); + c.addSource("c", "import \"b\"; contract D is C {}"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_CASE(circular_import) +{ + CompilerStack c; + c.addSource("a", "import \"b\"; contract C { D d; }"); + c.addSource("b", "import \"a\"; contract D { C c; }"); + BOOST_CHECK(c.compile()); +} + +BOOST_AUTO_TEST_SUITE_END() + +} +} +} // end namespaces diff --git a/test/libsolidity/SolidityExpressionCompiler.cpp b/test/libsolidity/SolidityExpressionCompiler.cpp index a622c738..0e814e56 100644 --- a/test/libsolidity/SolidityExpressionCompiler.cpp +++ b/test/libsolidity/SolidityExpressionCompiler.cpp @@ -71,18 +71,21 @@ private: }; Declaration const& resolveDeclaration( - vector const& _namespacedName, NameAndTypeResolver const& _resolver) + SourceUnit const& _sourceUnit, + vector const& _namespacedName, + NameAndTypeResolver const& _resolver +) { - Declaration const* declaration = nullptr; + ASTNode const* scope = &_sourceUnit; // bracers are required, cause msvc couldnt handle this macro in for statement for (string const& namePart: _namespacedName) { - auto declarations = _resolver.resolveName(namePart, declaration); + auto declarations = _resolver.resolveName(namePart, scope); BOOST_REQUIRE(!declarations.empty()); - BOOST_REQUIRE(declaration = *declarations.begin()); + BOOST_REQUIRE(scope = *declarations.begin()); } - BOOST_REQUIRE(declaration); - return *declaration; + BOOST_REQUIRE(scope); + return dynamic_cast(*scope); } bytes compileFirstExpression( @@ -140,13 +143,17 @@ bytes compileFirstExpression( unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack context.adjustStackOffset(parametersSize); for (vector const& variable: _localVariables) - context.addVariable(dynamic_cast(resolveDeclaration(variable, resolver)), - parametersSize--); + context.addVariable( + dynamic_cast(resolveDeclaration(*sourceUnit, variable, resolver)), + parametersSize-- + ); ExpressionCompiler(context).compile(*extractor.expression()); for (vector const& function: _functions) - context << context.functionEntryLabel(dynamic_cast(resolveDeclaration(function, resolver))); + context << context.functionEntryLabel(dynamic_cast( + resolveDeclaration(*sourceUnit, function, resolver) + )); bytes instructions = context.assembledObject().bytecode; // debug // cout << eth::disassemble(instructions) << endl; -- cgit