Constructors are defined using the `constructor` keyword.

This commit is contained in:
Daniel Kirchner 2018-03-01 18:39:01 +01:00 committed by Alex Beregszaszi
parent 0edce4b570
commit d664a599e6
8 changed files with 80 additions and 13 deletions

View File

@ -7,6 +7,7 @@ Features:
* Interfaces: Allow overriding external functions in interfaces with public in an implementing contract.
* Optimizer: Optimize across ``mload`` if ``msize()`` is not used.
* Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature).
* General: Introduce new constructor syntax using the ``constructor`` keyword as experimental 0.5.0 feature.
Bugfixes:
* Code Generator: Allow ``block.blockhash`` without being called.

View File

@ -216,7 +216,22 @@ bool SyntaxChecker::visit(FunctionDefinition const& _function)
if (v050 && _function.noVisibilitySpecified())
m_errorReporter.syntaxError(_function.location(), "No visibility specified.");
if (_function.isOldStyleConstructor())
{
if (v050)
m_errorReporter.syntaxError(
_function.location(),
"Functions are not allowed to have the same name as the contract. "
"If you intend this to be a constructor, use \"constructor(...) { ... }\" to define it."
);
else
m_errorReporter.warning(
_function.location(),
"Defining constructors as functions with the same name as the contract is deprecated. "
"Use \"constructor(...) { ... }\" instead."
);
}
return true;
}

View File

@ -607,7 +607,8 @@ public:
StateMutability stateMutability() const { return m_stateMutability; }
bool isConstructor() const { return m_isConstructor; }
bool isFallback() const { return name().empty(); }
bool isOldStyleConstructor() const { return m_isConstructor && !name().empty(); }
bool isFallback() const { return !m_isConstructor && name().empty(); }
bool isPayable() const { return m_stateMutability == StateMutability::Payable; }
std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; }
std::vector<ASTPointer<VariableDeclaration>> const& returnParameters() const { return m_returnParameters->parameters(); }

View File

@ -238,7 +238,10 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition(Token::Value _exp
Token::Value currentTokenValue = m_scanner->currentToken();
if (currentTokenValue == Token::RBrace)
break;
else if (currentTokenValue == Token::Function)
else if (
currentTokenValue == Token::Function ||
(currentTokenValue == Token::Identifier && m_scanner->currentLiteral() == "constructor")
)
// This can be a function or a state variable of function type (especially
// complicated to distinguish fallback function from function type state variable)
subNodes.push_back(parseFunctionDefinitionOrFunctionTypeStateVariable(name.get()));
@ -333,9 +336,19 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader(bool _forceEmptyN
{
RecursionGuard recursionGuard(*this);
FunctionHeaderParserResult result;
expectToken(Token::Function);
if (_forceEmptyName || m_scanner->currentToken() == Token::LParen)
result.name = make_shared<ASTString>(); // anonymous function
if (m_scanner->currentToken() == Token::Function)
// In case of old style constructors, i.e. functions with the same name as the contract,
// this is set to true later in parseFunctionDefinitionOrFunctionTypeStateVariable.
result.isConstructor = false;
else if (m_scanner->currentToken() == Token::Identifier && m_scanner->currentLiteral() == "constructor")
result.isConstructor = true;
else
solAssert(false, "Function or constructor expected.");
m_scanner->next();
if (result.isConstructor || _forceEmptyName || m_scanner->currentToken() == Token::LParen)
result.name = make_shared<ASTString>();
else
result.name = expectIdentifierToken();
VarDeclParserOptions options;
@ -426,12 +439,13 @@ ASTPointer<ASTNode> Parser::parseFunctionDefinitionOrFunctionTypeStateVariable(A
}
else
m_scanner->next(); // just consume the ';'
bool const c_isConstructor = (_contractName && *header.name == *_contractName);
if (_contractName && *header.name == *_contractName)
header.isConstructor = true;
return nodeFactory.createNode<FunctionDefinition>(
header.name,
header.visibility,
header.stateMutability,
c_isConstructor,
header.isConstructor,
docstring,
header.parameters,
header.modifiers,

View File

@ -56,6 +56,7 @@ private:
/// This struct is shared for parsing a function header and a function type.
struct FunctionHeaderParserResult
{
bool isConstructor;
ASTPointer<ASTString> name;
ASTPointer<ParameterList> parameters;
ASTPointer<ParameterList> returnParameters;

View File

@ -962,6 +962,41 @@ BOOST_AUTO_TEST_CASE(base_constructor_arguments_override)
CHECK_SUCCESS(text);
}
BOOST_AUTO_TEST_CASE(new_constructor_syntax)
{
char const* text = R"(
contract A { constructor() public {} }
)";
CHECK_SUCCESS(text);
text = R"(
pragma experimental "v0.5.0";
contract A { constructor() public {} }
)";
CHECK_SUCCESS(text);
}
BOOST_AUTO_TEST_CASE(old_constructor_syntax)
{
char const* text = R"(
contract A { function A() public {} }
)";
CHECK_WARNING(
text,
"Defining constructors as functions with the same name as the contract is deprecated."
);
text = R"(
pragma experimental "v0.5.0";
contract A { function A() public {} }
)";
CHECK_ERROR(
text,
SyntaxError,
"Functions are not allowed to have the same name as the contract."
);
}
BOOST_AUTO_TEST_CASE(implicit_derived_to_base_conversion)
{
char const* text = R"(
@ -6916,7 +6951,7 @@ BOOST_AUTO_TEST_CASE(shadowing_builtins_ignores_constructor)
{
char const* text = R"(
contract C {
function C() public {}
constructor() public {}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
@ -7328,7 +7363,7 @@ BOOST_AUTO_TEST_CASE(using_this_in_constructor)
{
char const* text = R"(
contract C {
function C() public {
constructor() public {
this.f();
}
function f() pure public {

View File

@ -1,5 +1,5 @@
contract Base {
function Base(uint) public {}
constructor(uint) public {}
}
contract Derived is Base(2) { }
contract Derived2 is Base(), Derived() { }

View File

@ -1,9 +1,9 @@
contract Base {
function Base(uint, uint) public {}
constructor(uint, uint) public {}
}
contract Derived is Base(2) { }
contract Derived2 is Base {
function Derived2() Base(2) public { }
constructor() Base(2) public { }
}
// ----
// TypeError: Wrong argument count for constructor call: 1 arguments given but expected 2.