Browse Source

Parsing of not fully implemented functions

- Adding the possibility of omitting a function body by simply ending a
  function definition with a semicolon

- Such a function is marked as not fully implemented and any contract
  that contains such a function is considered a not fully implemented contract
cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
742002b05a
  1. 4
      libsolidity/AST.cpp
  2. 85
      libsolidity/AST.h
  3. 6
      libsolidity/AST_accept.h
  4. 33
      libsolidity/Parser.cpp
  5. 15
      test/SolidityNameAndTypeResolution.cpp
  6. 8
      test/SolidityParser.cpp

4
libsolidity/AST.cpp

@ -311,8 +311,8 @@ void FunctionDefinition::checkTypeRequirements()
modifier->checkTypeRequirements(isConstructor() ? modifier->checkTypeRequirements(isConstructor() ?
dynamic_cast<ContractDefinition const&>(*getScope()).getBaseContracts() : dynamic_cast<ContractDefinition const&>(*getScope()).getBaseContracts() :
vector<ASTPointer<InheritanceSpecifier>>()); vector<ASTPointer<InheritanceSpecifier>>());
if (m_body)
m_body->checkTypeRequirements(); m_body->checkTypeRequirements();
} }
string FunctionDefinition::getCanonicalSignature() const string FunctionDefinition::getCanonicalSignature() const

85
libsolidity/AST.h

@ -196,6 +196,21 @@ protected:
ASTPointer<ASTString> m_documentation; ASTPointer<ASTString> m_documentation;
}; };
/**
* Abstract class that is added to AST nodes that can be marked as not being fully implemented
*/
class ImplementationOptional
{
public:
explicit ImplementationOptional(bool _implemented): m_implemented(_implemented) {}
/// @return whether this node is fully implemented or not
bool isFullyImplemented() const { return m_implemented; }
protected:
bool m_implemented;
};
/// @} /// @}
/** /**
@ -203,20 +218,25 @@ protected:
* document order. It first visits all struct declarations, then all variable declarations and * document order. It first visits all struct declarations, then all variable declarations and
* finally all function declarations. * finally all function declarations.
*/ */
class ContractDefinition: public Declaration, public Documented class ContractDefinition: public Declaration, public Documented, public ImplementationOptional
{ {
public: public:
ContractDefinition(SourceLocation const& _location, ContractDefinition(
ASTPointer<ASTString> const& _name, SourceLocation const& _location,
ASTPointer<ASTString> const& _documentation, ASTPointer<ASTString> const& _name,
std::vector<ASTPointer<InheritanceSpecifier>> const& _baseContracts, ASTPointer<ASTString> const& _documentation,
std::vector<ASTPointer<StructDefinition>> const& _definedStructs, std::vector<ASTPointer<InheritanceSpecifier>> const& _baseContracts,
std::vector<ASTPointer<EnumDefinition>> const& _definedEnums, std::vector<ASTPointer<StructDefinition>> const& _definedStructs,
std::vector<ASTPointer<VariableDeclaration>> const& _stateVariables, std::vector<ASTPointer<EnumDefinition>> const& _definedEnums,
std::vector<ASTPointer<FunctionDefinition>> const& _definedFunctions, std::vector<ASTPointer<VariableDeclaration>> const& _stateVariables,
std::vector<ASTPointer<ModifierDefinition>> const& _functionModifiers, std::vector<ASTPointer<FunctionDefinition>> const& _definedFunctions,
std::vector<ASTPointer<EventDefinition>> const& _events): std::vector<ASTPointer<ModifierDefinition>> const& _functionModifiers,
Declaration(_location, _name), Documented(_documentation), std::vector<ASTPointer<EventDefinition>> const& _events,
bool _isFullyImplemented
):
Declaration(_location, _name),
Documented(_documentation),
ImplementationOptional(_isFullyImplemented),
m_baseContracts(_baseContracts), m_baseContracts(_baseContracts),
m_definedStructs(_definedStructs), m_definedStructs(_definedStructs),
m_definedEnums(_definedEnums), m_definedEnums(_definedEnums),
@ -224,7 +244,7 @@ public:
m_definedFunctions(_definedFunctions), m_definedFunctions(_definedFunctions),
m_functionModifiers(_functionModifiers), m_functionModifiers(_functionModifiers),
m_events(_events) m_events(_events)
{} {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -378,25 +398,30 @@ private:
std::vector<ASTPointer<VariableDeclaration>> m_parameters; std::vector<ASTPointer<VariableDeclaration>> m_parameters;
}; };
class FunctionDefinition: public Declaration, public VariableScope, public Documented class FunctionDefinition: public Declaration, public VariableScope, public Documented, public ImplementationOptional
{ {
public: public:
FunctionDefinition(SourceLocation const& _location, ASTPointer<ASTString> const& _name, FunctionDefinition(
Declaration::Visibility _visibility, bool _isConstructor, SourceLocation const& _location,
ASTPointer<ASTString> const& _documentation, ASTPointer<ASTString> const& _name,
ASTPointer<ParameterList> const& _parameters, Declaration::Visibility _visibility, bool _isConstructor,
bool _isDeclaredConst, ASTPointer<ASTString> const& _documentation,
std::vector<ASTPointer<ModifierInvocation>> const& _modifiers, ASTPointer<ParameterList> const& _parameters,
ASTPointer<ParameterList> const& _returnParameters, bool _isDeclaredConst,
ASTPointer<Block> const& _body): std::vector<ASTPointer<ModifierInvocation>> const& _modifiers,
Declaration(_location, _name, _visibility), Documented(_documentation), ASTPointer<ParameterList> const& _returnParameters,
m_isConstructor(_isConstructor), ASTPointer<Block> const& _body
m_parameters(_parameters), ):
m_isDeclaredConst(_isDeclaredConst), Declaration(_location, _name, _visibility),
m_functionModifiers(_modifiers), Documented(_documentation),
m_returnParameters(_returnParameters), ImplementationOptional(_body != nullptr),
m_body(_body) m_isConstructor(_isConstructor),
{} m_parameters(_parameters),
m_isDeclaredConst(_isDeclaredConst),
m_functionModifiers(_modifiers),
m_returnParameters(_returnParameters),
m_body(_body)
{}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;

6
libsolidity/AST_accept.h

@ -175,7 +175,8 @@ void FunctionDefinition::accept(ASTVisitor& _visitor)
if (m_returnParameters) if (m_returnParameters)
m_returnParameters->accept(_visitor); m_returnParameters->accept(_visitor);
listAccept(m_functionModifiers, _visitor); listAccept(m_functionModifiers, _visitor);
m_body->accept(_visitor); if (m_body)
m_body->accept(_visitor);
} }
_visitor.endVisit(*this); _visitor.endVisit(*this);
} }
@ -188,7 +189,8 @@ void FunctionDefinition::accept(ASTConstVisitor& _visitor) const
if (m_returnParameters) if (m_returnParameters)
m_returnParameters->accept(_visitor); m_returnParameters->accept(_visitor);
listAccept(m_functionModifiers, _visitor); listAccept(m_functionModifiers, _visitor);
m_body->accept(_visitor); if (m_body)
m_body->accept(_visitor);
} }
_visitor.endVisit(*this); _visitor.endVisit(*this);
} }

33
libsolidity/Parser.cpp

@ -116,6 +116,7 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition()
{ {
ASTNodeFactory nodeFactory(*this); ASTNodeFactory nodeFactory(*this);
ASTPointer<ASTString> docString; ASTPointer<ASTString> docString;
bool contractFullyImplemented = true;
if (m_scanner->getCurrentCommentLiteral() != "") if (m_scanner->getCurrentCommentLiteral() != "")
docString = make_shared<ASTString>(m_scanner->getCurrentCommentLiteral()); docString = make_shared<ASTString>(m_scanner->getCurrentCommentLiteral());
expectToken(Token::Contract); expectToken(Token::Contract);
@ -141,7 +142,12 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition()
if (currentToken == Token::RBrace) if (currentToken == Token::RBrace)
break; break;
else if (currentToken == Token::Function) else if (currentToken == Token::Function)
functions.push_back(parseFunctionDefinition(name.get())); {
ASTPointer<FunctionDefinition> func = parseFunctionDefinition(name.get());
functions.push_back(func);
if (!func->isFullyImplemented())
contractFullyImplemented = false;
}
else if (currentToken == Token::Struct) else if (currentToken == Token::Struct)
structs.push_back(parseStructDefinition()); structs.push_back(parseStructDefinition());
else if (currentToken == Token::Enum) else if (currentToken == Token::Enum)
@ -164,8 +170,18 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition()
} }
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();
expectToken(Token::RBrace); expectToken(Token::RBrace);
return nodeFactory.createNode<ContractDefinition>(name, docString, baseContracts, structs, enums, return nodeFactory.createNode<ContractDefinition>(
stateVariables, functions, modifiers, events); name,
docString,
baseContracts,
structs,
enums,
stateVariables,
functions,
modifiers,
events,
contractFullyImplemented
);
} }
ASTPointer<InheritanceSpecifier> Parser::parseInheritanceSpecifier() ASTPointer<InheritanceSpecifier> Parser::parseInheritanceSpecifier()
@ -247,8 +263,15 @@ ASTPointer<FunctionDefinition> Parser::parseFunctionDefinition(ASTString const*
} }
else else
returnParameters = createEmptyParameterList(); returnParameters = createEmptyParameterList();
ASTPointer<Block> block = parseBlock(); ASTPointer<Block> block = ASTPointer<Block>();
nodeFactory.setEndPositionFromNode(block); nodeFactory.markEndPosition();
if (m_scanner->getCurrentToken() != Token::Semicolon)
{
block = parseBlock();
nodeFactory.setEndPositionFromNode(block);
}
else
m_scanner->next(); // just consume the ';'
bool const c_isConstructor = (_contractName && *name == *_contractName); bool const c_isConstructor = (_contractName && *name == *_contractName);
return nodeFactory.createNode<FunctionDefinition>(name, visibility, c_isConstructor, docstring, return nodeFactory.createNode<FunctionDefinition>(name, visibility, c_isConstructor, docstring,
parameters, isDeclaredConst, modifiers, parameters, isDeclaredConst, modifiers,

15
test/SolidityNameAndTypeResolution.cpp

@ -346,6 +346,21 @@ BOOST_AUTO_TEST_CASE(comparison_bitop_precedence)
ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed"); ETH_TEST_CHECK_NO_THROW(parseTextAndResolveNames(text), "Parsing and Name Resolving Failed");
} }
BOOST_AUTO_TEST_CASE(function_no_implementation)
{
ASTPointer<SourceUnit> sourceUnit;
char const* text = "contract test {\n"
" function functionName(bytes32 input) returns (bytes32 out);\n"
"}\n";
ETH_TEST_REQUIRE_NO_THROW(sourceUnit = parseTextAndResolveNames(text), "Parsing and name Resolving failed");
ContractDefinition* contract;
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
contract = dynamic_cast<ContractDefinition*>(node.get());
BOOST_CHECK(contract);
BOOST_CHECK(!contract->isFullyImplemented());
BOOST_CHECK(!contract->getDefinedFunctions()[0]->isFullyImplemented());
}
BOOST_AUTO_TEST_CASE(function_canonical_signature) BOOST_AUTO_TEST_CASE(function_canonical_signature)
{ {
ASTPointer<SourceUnit> sourceUnit; ASTPointer<SourceUnit> sourceUnit;

8
test/SolidityParser.cpp

@ -108,6 +108,14 @@ BOOST_AUTO_TEST_CASE(single_function_param)
ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed."); ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed.");
} }
BOOST_AUTO_TEST_CASE(function_no_body)
{
char const* text = "contract test {\n"
" function functionName(bytes32 input) returns (bytes32 out);\n"
"}\n";
ETH_TEST_CHECK_NO_THROW(parseText(text), "Parsing failed.");
}
BOOST_AUTO_TEST_CASE(missing_parameter_name_in_named_args) BOOST_AUTO_TEST_CASE(missing_parameter_name_in_named_args)
{ {
char const* text = "contract test {\n" char const* text = "contract test {\n"

Loading…
Cancel
Save