mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #3635 from ethereum/constructor-modifier
Constructors are defined using the ``constructor`` keyword.
This commit is contained in:
commit
0695ffe51d
@ -7,6 +7,7 @@ Features:
|
|||||||
* Interfaces: Allow overriding external functions in interfaces with public in an implementing contract.
|
* Interfaces: Allow overriding external functions in interfaces with public in an implementing contract.
|
||||||
* Optimizer: Optimize across ``mload`` if ``msize()`` is not used.
|
* Optimizer: Optimize across ``mload`` if ``msize()`` is not used.
|
||||||
* Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature).
|
* 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:
|
Bugfixes:
|
||||||
* Code Generator: Allow ``block.blockhash`` without being called.
|
* Code Generator: Allow ``block.blockhash`` without being called.
|
||||||
|
@ -40,7 +40,7 @@ This means that cyclic creation dependencies are impossible.
|
|||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
pragma solidity ^0.4.16;
|
pragma solidity >0.4.21;
|
||||||
|
|
||||||
contract OwnedToken {
|
contract OwnedToken {
|
||||||
// TokenCreator is a contract type that is defined below.
|
// TokenCreator is a contract type that is defined below.
|
||||||
@ -52,7 +52,7 @@ This means that cyclic creation dependencies are impossible.
|
|||||||
|
|
||||||
// This is the constructor which registers the
|
// This is the constructor which registers the
|
||||||
// creator and the assigned name.
|
// creator and the assigned name.
|
||||||
function OwnedToken(bytes32 _name) public {
|
constructor(bytes32 _name) public {
|
||||||
// State variables are accessed via their name
|
// State variables are accessed via their name
|
||||||
// and not via e.g. this.owner. This also applies
|
// and not via e.g. this.owner. This also applies
|
||||||
// to functions and especially in the constructors,
|
// to functions and especially in the constructors,
|
||||||
@ -976,8 +976,31 @@ virtual method lookup.
|
|||||||
|
|
||||||
Constructors
|
Constructors
|
||||||
============
|
============
|
||||||
A constructor is an optional function with the same name as the contract which is executed upon contract creation.
|
A constructor is an optional function declared with the ``constructor`` keyword which is executed upon contract creation.
|
||||||
Constructor functions can be either ``public`` or ``internal``.
|
Constructor functions can be either ``public`` or ``internal``. If there is no constructor, the contract will assume the
|
||||||
|
default constructor: ``contructor() public {}``.
|
||||||
|
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
pragma solidity >0.4.21;
|
||||||
|
|
||||||
|
contract A {
|
||||||
|
uint public a;
|
||||||
|
|
||||||
|
constructor(uint _a) internal {
|
||||||
|
a = _a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract B is A(1) {
|
||||||
|
constructor() public {}
|
||||||
|
}
|
||||||
|
|
||||||
|
A constructor set as ``internal`` causes the contract to be marked as :ref:`abstract <abstract-contract>`.
|
||||||
|
|
||||||
|
.. note ::
|
||||||
|
Prior to version 0.4.22, constructors were defined as functions with the same name as the contract. This syntax is now deprecated.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
@ -995,7 +1018,6 @@ Constructor functions can be either ``public`` or ``internal``.
|
|||||||
function B() public {}
|
function B() public {}
|
||||||
}
|
}
|
||||||
|
|
||||||
A constructor set as ``internal`` causes the contract to be marked as :ref:`abstract <abstract-contract>`.
|
|
||||||
|
|
||||||
.. index:: ! base;constructor
|
.. index:: ! base;constructor
|
||||||
|
|
||||||
@ -1009,11 +1031,11 @@ the base constructors. This can be done in two ways::
|
|||||||
|
|
||||||
contract Base {
|
contract Base {
|
||||||
uint x;
|
uint x;
|
||||||
function Base(uint _x) public { x = _x; }
|
constructor(uint _x) public { x = _x; }
|
||||||
}
|
}
|
||||||
|
|
||||||
contract Derived is Base(7) {
|
contract Derived is Base(7) {
|
||||||
function Derived(uint _y) Base(_y * _y) public {
|
constructor(uint _y) Base(_y * _y) public {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -216,7 +216,22 @@ bool SyntaxChecker::visit(FunctionDefinition const& _function)
|
|||||||
|
|
||||||
if (v050 && _function.noVisibilitySpecified())
|
if (v050 && _function.noVisibilitySpecified())
|
||||||
m_errorReporter.syntaxError(_function.location(), "No visibility specified.");
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -607,7 +607,8 @@ public:
|
|||||||
|
|
||||||
StateMutability stateMutability() const { return m_stateMutability; }
|
StateMutability stateMutability() const { return m_stateMutability; }
|
||||||
bool isConstructor() const { return m_isConstructor; }
|
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; }
|
bool isPayable() const { return m_stateMutability == StateMutability::Payable; }
|
||||||
std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; }
|
std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; }
|
||||||
std::vector<ASTPointer<VariableDeclaration>> const& returnParameters() const { return m_returnParameters->parameters(); }
|
std::vector<ASTPointer<VariableDeclaration>> const& returnParameters() const { return m_returnParameters->parameters(); }
|
||||||
|
@ -238,7 +238,10 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition(Token::Value _exp
|
|||||||
Token::Value currentTokenValue = m_scanner->currentToken();
|
Token::Value currentTokenValue = m_scanner->currentToken();
|
||||||
if (currentTokenValue == Token::RBrace)
|
if (currentTokenValue == Token::RBrace)
|
||||||
break;
|
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
|
// This can be a function or a state variable of function type (especially
|
||||||
// complicated to distinguish fallback function from function type state variable)
|
// complicated to distinguish fallback function from function type state variable)
|
||||||
subNodes.push_back(parseFunctionDefinitionOrFunctionTypeStateVariable(name.get()));
|
subNodes.push_back(parseFunctionDefinitionOrFunctionTypeStateVariable(name.get()));
|
||||||
@ -329,15 +332,31 @@ StateMutability Parser::parseStateMutability(Token::Value _token)
|
|||||||
return stateMutability;
|
return stateMutability;
|
||||||
}
|
}
|
||||||
|
|
||||||
Parser::FunctionHeaderParserResult Parser::parseFunctionHeader(bool _forceEmptyName, bool _allowModifiers)
|
Parser::FunctionHeaderParserResult Parser::parseFunctionHeader(
|
||||||
|
bool _forceEmptyName,
|
||||||
|
bool _allowModifiers,
|
||||||
|
ASTString const* _contractName
|
||||||
|
)
|
||||||
{
|
{
|
||||||
RecursionGuard recursionGuard(*this);
|
RecursionGuard recursionGuard(*this);
|
||||||
FunctionHeaderParserResult result;
|
FunctionHeaderParserResult result;
|
||||||
expectToken(Token::Function);
|
|
||||||
if (_forceEmptyName || m_scanner->currentToken() == Token::LParen)
|
result.isConstructor = false;
|
||||||
result.name = make_shared<ASTString>(); // anonymous function
|
|
||||||
|
if (m_scanner->currentToken() == Token::Identifier && m_scanner->currentLiteral() == "constructor")
|
||||||
|
result.isConstructor = true;
|
||||||
|
else if (m_scanner->currentToken() != Token::Function)
|
||||||
|
solAssert(false, "Function or constructor expected.");
|
||||||
|
m_scanner->next();
|
||||||
|
|
||||||
|
if (result.isConstructor || _forceEmptyName || m_scanner->currentToken() == Token::LParen)
|
||||||
|
result.name = make_shared<ASTString>();
|
||||||
else
|
else
|
||||||
result.name = expectIdentifierToken();
|
result.name = expectIdentifierToken();
|
||||||
|
|
||||||
|
if (!result.name->empty() && _contractName && *result.name == *_contractName)
|
||||||
|
result.isConstructor = true;
|
||||||
|
|
||||||
VarDeclParserOptions options;
|
VarDeclParserOptions options;
|
||||||
options.allowLocationSpecifier = true;
|
options.allowLocationSpecifier = true;
|
||||||
result.parameters = parseParameterList(options);
|
result.parameters = parseParameterList(options);
|
||||||
@ -407,7 +426,7 @@ ASTPointer<ASTNode> Parser::parseFunctionDefinitionOrFunctionTypeStateVariable(A
|
|||||||
if (m_scanner->currentCommentLiteral() != "")
|
if (m_scanner->currentCommentLiteral() != "")
|
||||||
docstring = make_shared<ASTString>(m_scanner->currentCommentLiteral());
|
docstring = make_shared<ASTString>(m_scanner->currentCommentLiteral());
|
||||||
|
|
||||||
FunctionHeaderParserResult header = parseFunctionHeader(false, true);
|
FunctionHeaderParserResult header = parseFunctionHeader(false, true, _contractName);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!header.modifiers.empty() ||
|
!header.modifiers.empty() ||
|
||||||
@ -426,12 +445,11 @@ ASTPointer<ASTNode> Parser::parseFunctionDefinitionOrFunctionTypeStateVariable(A
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
m_scanner->next(); // just consume the ';'
|
m_scanner->next(); // just consume the ';'
|
||||||
bool const c_isConstructor = (_contractName && *header.name == *_contractName);
|
|
||||||
return nodeFactory.createNode<FunctionDefinition>(
|
return nodeFactory.createNode<FunctionDefinition>(
|
||||||
header.name,
|
header.name,
|
||||||
header.visibility,
|
header.visibility,
|
||||||
header.stateMutability,
|
header.stateMutability,
|
||||||
c_isConstructor,
|
header.isConstructor,
|
||||||
docstring,
|
docstring,
|
||||||
header.parameters,
|
header.parameters,
|
||||||
header.modifiers,
|
header.modifiers,
|
||||||
|
@ -56,6 +56,7 @@ private:
|
|||||||
/// This struct is shared for parsing a function header and a function type.
|
/// This struct is shared for parsing a function header and a function type.
|
||||||
struct FunctionHeaderParserResult
|
struct FunctionHeaderParserResult
|
||||||
{
|
{
|
||||||
|
bool isConstructor;
|
||||||
ASTPointer<ASTString> name;
|
ASTPointer<ASTString> name;
|
||||||
ASTPointer<ParameterList> parameters;
|
ASTPointer<ParameterList> parameters;
|
||||||
ASTPointer<ParameterList> returnParameters;
|
ASTPointer<ParameterList> returnParameters;
|
||||||
@ -73,7 +74,11 @@ private:
|
|||||||
ASTPointer<InheritanceSpecifier> parseInheritanceSpecifier();
|
ASTPointer<InheritanceSpecifier> parseInheritanceSpecifier();
|
||||||
Declaration::Visibility parseVisibilitySpecifier(Token::Value _token);
|
Declaration::Visibility parseVisibilitySpecifier(Token::Value _token);
|
||||||
StateMutability parseStateMutability(Token::Value _token);
|
StateMutability parseStateMutability(Token::Value _token);
|
||||||
FunctionHeaderParserResult parseFunctionHeader(bool _forceEmptyName, bool _allowModifiers);
|
FunctionHeaderParserResult parseFunctionHeader(
|
||||||
|
bool _forceEmptyName,
|
||||||
|
bool _allowModifiers,
|
||||||
|
ASTString const* _contractName = nullptr
|
||||||
|
);
|
||||||
ASTPointer<ASTNode> parseFunctionDefinitionOrFunctionTypeStateVariable(ASTString const* _contractName);
|
ASTPointer<ASTNode> parseFunctionDefinitionOrFunctionTypeStateVariable(ASTString const* _contractName);
|
||||||
ASTPointer<FunctionDefinition> parseFunctionDefinition(ASTString const* _contractName);
|
ASTPointer<FunctionDefinition> parseFunctionDefinition(ASTString const* _contractName);
|
||||||
ASTPointer<StructDefinition> parseStructDefinition();
|
ASTPointer<StructDefinition> parseStructDefinition();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
pragma solidity ^0.4.0;
|
pragma solidity >0.4.21;
|
||||||
|
|
||||||
import "./Token.sol";
|
import "./Token.sol";
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ contract StandardToken is Token {
|
|||||||
mapping (address =>
|
mapping (address =>
|
||||||
mapping (address => uint256)) m_allowance;
|
mapping (address => uint256)) m_allowance;
|
||||||
|
|
||||||
function StandardToken(address _initialOwner, uint256 _supply) public {
|
constructor(address _initialOwner, uint256 _supply) public {
|
||||||
supply = _supply;
|
supply = _supply;
|
||||||
balance[_initialOwner] = _supply;
|
balance[_initialOwner] = _supply;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
pragma solidity ^0.4.0;
|
pragma solidity >0.4.21;
|
||||||
|
|
||||||
contract owned {
|
contract owned {
|
||||||
address owner;
|
address owner;
|
||||||
@ -9,7 +9,7 @@ contract owned {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function owned() public {
|
constructor() public {
|
||||||
owner = msg.sender;
|
owner = msg.sender;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -962,6 +962,35 @@ BOOST_AUTO_TEST_CASE(base_constructor_arguments_override)
|
|||||||
CHECK_SUCCESS(text);
|
CHECK_SUCCESS(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(new_constructor_syntax)
|
||||||
|
{
|
||||||
|
char const* text = R"(
|
||||||
|
contract A { constructor() public {} }
|
||||||
|
)";
|
||||||
|
CHECK_SUCCESS_NO_WARNINGS(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)
|
BOOST_AUTO_TEST_CASE(implicit_derived_to_base_conversion)
|
||||||
{
|
{
|
||||||
char const* text = R"(
|
char const* text = R"(
|
||||||
@ -6916,7 +6945,7 @@ BOOST_AUTO_TEST_CASE(shadowing_builtins_ignores_constructor)
|
|||||||
{
|
{
|
||||||
char const* text = R"(
|
char const* text = R"(
|
||||||
contract C {
|
contract C {
|
||||||
function C() public {}
|
constructor() public {}
|
||||||
}
|
}
|
||||||
)";
|
)";
|
||||||
CHECK_SUCCESS_NO_WARNINGS(text);
|
CHECK_SUCCESS_NO_WARNINGS(text);
|
||||||
@ -7328,7 +7357,7 @@ BOOST_AUTO_TEST_CASE(using_this_in_constructor)
|
|||||||
{
|
{
|
||||||
char const* text = R"(
|
char const* text = R"(
|
||||||
contract C {
|
contract C {
|
||||||
function C() public {
|
constructor() public {
|
||||||
this.f();
|
this.f();
|
||||||
}
|
}
|
||||||
function f() pure public {
|
function f() pure public {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
contract Base {
|
contract Base {
|
||||||
function Base(uint) public {}
|
constructor(uint) public {}
|
||||||
}
|
}
|
||||||
contract Derived is Base(2) { }
|
contract Derived is Base(2) { }
|
||||||
contract Derived2 is Base(), Derived() { }
|
contract Derived2 is Base(), Derived() { }
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
contract Base {
|
contract Base {
|
||||||
function Base(uint, uint) public {}
|
constructor(uint, uint) public {}
|
||||||
}
|
}
|
||||||
contract Derived is Base(2) { }
|
contract Derived is Base(2) { }
|
||||||
contract Derived2 is Base {
|
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.
|
// TypeError: Wrong argument count for constructor call: 1 arguments given but expected 2.
|
||||||
|
Loading…
Reference in New Issue
Block a user