From 304256b546a73783d4d7ea4bb4b57ea6cb1e5cb0 Mon Sep 17 00:00:00 2001 From: Liana Husikyan Date: Thu, 12 Mar 2015 14:15:04 +0100 Subject: [PATCH] - added isPartOfExternalInterface to Declaration - changed position for the constant specifier. now it goes after type: = - removed tests for constant functions, checkings for constant function doesn't belong to this story --- libsolidity/AST.cpp | 4 ++-- libsolidity/AST.h | 4 ++++ libsolidity/ExpressionCompiler.cpp | 2 ++ libsolidity/Parser.cpp | 18 ++++++++---------- libsolidity/Parser.h | 4 +--- test/SolidityEndToEndTest.cpp | 2 +- test/SolidityNameAndTypeResolution.cpp | 16 +++------------- test/SolidityNameAndTypeResolution.cpp.orig | 18 ++++-------------- 8 files changed, 25 insertions(+), 43 deletions(-) diff --git a/libsolidity/AST.cpp b/libsolidity/AST.cpp index b05602639..605f53521 100644 --- a/libsolidity/AST.cpp +++ b/libsolidity/AST.cpp @@ -189,7 +189,7 @@ vector, FunctionTypePointer>> const& ContractDefinition::getIn for (ContractDefinition const* contract: getLinearizedBaseContracts()) { for (ASTPointer const& f: contract->getDefinedFunctions()) - if (f->isPublic() && !f->isConstructor() && !f->getName().empty() && functionsSeen.count(f->getName()) == 0) + if (functionsSeen.count(f->getName()) == 0 && f->isPartOfExternalInterface()) { functionsSeen.insert(f->getName()); FixedHash<4> hash(dev::sha3(f->getCanonicalSignature())); @@ -197,7 +197,7 @@ vector, FunctionTypePointer>> const& ContractDefinition::getIn } for (ASTPointer const& v: contract->getStateVariables()) - if (v->isPublic() && functionsSeen.count(v->getName()) == 0 && !v->isConstant()) + if (functionsSeen.count(v->getName()) == 0 && v->isPartOfExternalInterface()) { FunctionType ftype(*v); functionsSeen.insert(v->getName()); diff --git a/libsolidity/AST.h b/libsolidity/AST.h index e959ba248..6a269e154 100644 --- a/libsolidity/AST.h +++ b/libsolidity/AST.h @@ -156,6 +156,7 @@ public: /// contract types. virtual TypePointer getType(ContractDefinition const* m_currentContract = nullptr) const = 0; virtual bool isLValue() const { return false; } + virtual bool isPartOfExternalInterface() const { return false; }; protected: virtual Visibility getDefaultVisibility() const { return Visibility::Public; } @@ -415,6 +416,7 @@ public: getVisibility() >= Visibility::Internal; } virtual TypePointer getType(ContractDefinition const*) const override; + virtual bool isPartOfExternalInterface() const override { return isPublic() && !m_isConstructor && !getName().empty(); } /// Checks that all parameters have allowed types and calls checkTypeRequirements on the body. void checkTypeRequirements(); @@ -468,6 +470,8 @@ public: void setType(std::shared_ptr const& _type) { m_type = _type; } virtual bool isLValue() const override; + virtual bool isPartOfExternalInterface() const override { return isPublic() && !m_isConstant; } + void checkTypeRequirements(); bool isLocalVariable() const { return !!dynamic_cast(getScope()); } diff --git a/libsolidity/ExpressionCompiler.cpp b/libsolidity/ExpressionCompiler.cpp index ce5f184b4..9391bc2a7 100644 --- a/libsolidity/ExpressionCompiler.cpp +++ b/libsolidity/ExpressionCompiler.cpp @@ -859,6 +859,8 @@ void ExpressionCompiler::endVisit(Identifier const& _identifier) { if (!variable->isConstant()) setLValueFromDeclaration(*declaration, _identifier); + else + variable->getValue()->accept(*this); } else if (dynamic_cast(declaration)) { diff --git a/libsolidity/Parser.cpp b/libsolidity/Parser.cpp index 05c05f632..9efa30061 100644 --- a/libsolidity/Parser.cpp +++ b/libsolidity/Parser.cpp @@ -135,7 +135,6 @@ ASTPointer Parser::parseContractDefinition() } while (m_scanner->getCurrentToken() == Token::Comma); expectToken(Token::LBrace); - bool isDeclaredConst = false; while (true) { Token::Value currentToken = m_scanner->getCurrentToken(); @@ -153,21 +152,13 @@ ASTPointer Parser::parseContractDefinition() VarDeclParserOptions options; options.isStateVariable = true; options.allowInitialValue = true; - options.isDeclaredConst = isDeclaredConst; stateVariables.push_back(parseVariableDeclaration(options)); - isDeclaredConst = false; expectToken(Token::Semicolon); } else if (currentToken == Token::Modifier) modifiers.push_back(parseModifierDefinition()); else if (currentToken == Token::Event) events.push_back(parseEventDefinition()); - else if (currentToken == Token::Const) - { - solAssert(Token::isElementaryTypeName(m_scanner->peekNextToken()), ""); - isDeclaredConst = true; - m_scanner->next(); - } else BOOST_THROW_EXCEPTION(createParserError("Function, variable, struct or modifier declaration expected.")); } @@ -326,6 +317,7 @@ ASTPointer Parser::parseVariableDeclaration( nodeFactory.setEndPositionFromNode(type); } bool isIndexed = false; + bool isDeclaredConst = false; ASTPointer identifier; Token::Value token = m_scanner->getCurrentToken(); Declaration::Visibility visibility(Declaration::Visibility::Default); @@ -336,7 +328,13 @@ ASTPointer Parser::parseVariableDeclaration( isIndexed = true; m_scanner->next(); } + if (token == Token::Const) + { + m_scanner->next(); + isDeclaredConst = true; + } nodeFactory.markEndPosition(); + if (_options.allowEmptyName && m_scanner->getCurrentToken() != Token::Identifier) { identifier = make_shared(""); @@ -357,7 +355,7 @@ ASTPointer Parser::parseVariableDeclaration( } return nodeFactory.createNode(type, identifier, value, visibility, _options.isStateVariable, - isIndexed, _options.isDeclaredConst); + isIndexed, isDeclaredConst); } ASTPointer Parser::parseModifierDefinition() diff --git a/libsolidity/Parser.h b/libsolidity/Parser.h index 469e446fd..08c47c252 100644 --- a/libsolidity/Parser.h +++ b/libsolidity/Parser.h @@ -54,7 +54,6 @@ private: bool allowIndexed = false; bool allowEmptyName = false; bool allowInitialValue = false; - bool isDeclaredConst = false; }; ///@{ @@ -67,8 +66,7 @@ private: ASTPointer parseStructDefinition(); ASTPointer parseEnumDefinition(); ASTPointer parseEnumValue(); - ASTPointer parseVariableDeclaration( - VarDeclParserOptions const& _options = VarDeclParserOptions(), + ASTPointer parseVariableDeclaration(VarDeclParserOptions const& _options = VarDeclParserOptions(), ASTPointer const& _lookAheadArrayType = ASTPointer()); ASTPointer parseModifierDefinition(); ASTPointer parseEventDefinition(); diff --git a/test/SolidityEndToEndTest.cpp b/test/SolidityEndToEndTest.cpp index 4fa98959f..e455dab0d 100644 --- a/test/SolidityEndToEndTest.cpp +++ b/test/SolidityEndToEndTest.cpp @@ -3212,7 +3212,7 @@ BOOST_AUTO_TEST_CASE(const_state_variable) char const* sourceCode = R"( contract Foo { function getX() constant returns (uint r) { return x; } - constant uint x = 56; + uint constant x = 56; })"; compileAndRun(sourceCode); BOOST_CHECK(callContractFunction("getX()") == encodeArgs(56)); diff --git a/test/SolidityNameAndTypeResolution.cpp b/test/SolidityNameAndTypeResolution.cpp index cc570ac8a..d01635569 100644 --- a/test/SolidityNameAndTypeResolution.cpp +++ b/test/SolidityNameAndTypeResolution.cpp @@ -1404,22 +1404,12 @@ BOOST_AUTO_TEST_CASE(test_byte_is_alias_of_byte1) ETH_TEST_REQUIRE_NO_THROW(parseTextAndResolveNames(text), "Type resolving failed"); } -BOOST_AUTO_TEST_CASE(constant_function_editing_state_variable) +BOOST_AUTO_TEST_CASE(const_state_variable) { char const* text = R"( contract Foo { - uint x = 56; - function editIt() constant { x = 2; } - })"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); -} - -BOOST_AUTO_TEST_CASE(constant_function_editing_const_state_variable) -{ - char const* text = R"( - contract Foo { - constant uint x = 56; - function editIt() constant { x = 2; } + function changeIt() { x = 9; } + uint constant x = 56; })"; BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); } diff --git a/test/SolidityNameAndTypeResolution.cpp.orig b/test/SolidityNameAndTypeResolution.cpp.orig index aa573a14e..4cc013c5f 100644 --- a/test/SolidityNameAndTypeResolution.cpp.orig +++ b/test/SolidityNameAndTypeResolution.cpp.orig @@ -1404,25 +1404,15 @@ BOOST_AUTO_TEST_CASE(test_byte_is_alias_of_byte1) })"; ETH_TEST_REQUIRE_NO_THROW(parseTextAndResolveNames(text), "Type resolving failed"); ======= -BOOST_AUTO_TEST_CASE(constant_function_editing_state_variable) +BOOST_AUTO_TEST_CASE(const_state_variable) { char const* text = R"( contract Foo { - uint x = 56; - function editIt() constant { x = 2; } + function changeIt() { x = 9; } + uint constant x = 56; })"; BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); -} - -BOOST_AUTO_TEST_CASE(constant_function_editing_const_state_variable) -{ - char const* text = R"( - contract Foo { - constant uint x = 56; - function editIt() constant { x = 2; } - })"; - BOOST_CHECK_THROW(parseTextAndResolveNames(text), TypeError); ->>>>>>> 0925feb... added parsing for constant variables +>>>>>>> 936dfba... one more test to check assigning the value to constant variable } BOOST_AUTO_TEST_SUITE_END()