From d664a599e68291166c47fcece464cb8d0af31df8 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Thu, 1 Mar 2018 18:39:01 +0100 Subject: Constructors are defined using the ``constructor`` keyword. --- libsolidity/parsing/Parser.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 8c97f55f..e5cc69d8 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -238,7 +238,10 @@ ASTPointer 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(); // 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(); else result.name = expectIdentifierToken(); VarDeclParserOptions options; @@ -426,12 +439,13 @@ ASTPointer 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( header.name, header.visibility, header.stateMutability, - c_isConstructor, + header.isConstructor, docstring, header.parameters, header.modifiers, -- cgit v1.2.3 From e2dac9ed397c29bfe426912c28ef2d419b2324c8 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Wed, 7 Mar 2018 16:06:09 +0100 Subject: Set header.isConstructor for old style constructors in parseFunctionHeader as well. --- libsolidity/parsing/Parser.cpp | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index e5cc69d8..6ae66eee 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -332,14 +332,18 @@ StateMutability Parser::parseStateMutability(Token::Value _token) return stateMutability; } -Parser::FunctionHeaderParserResult Parser::parseFunctionHeader(bool _forceEmptyName, bool _allowModifiers) +Parser::FunctionHeaderParserResult Parser::parseFunctionHeader( + bool _forceEmptyName, + bool _allowModifiers, + ASTString const* _contractName +) { RecursionGuard recursionGuard(*this); FunctionHeaderParserResult result; 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. + // this is set to true below. result.isConstructor = false; else if (m_scanner->currentToken() == Token::Identifier && m_scanner->currentLiteral() == "constructor") result.isConstructor = true; @@ -351,6 +355,10 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader(bool _forceEmptyN result.name = make_shared(); else result.name = expectIdentifierToken(); + + if (!result.name->empty() && _contractName && *result.name == *_contractName) + result.isConstructor = true; + VarDeclParserOptions options; options.allowLocationSpecifier = true; result.parameters = parseParameterList(options); @@ -420,7 +428,7 @@ ASTPointer Parser::parseFunctionDefinitionOrFunctionTypeStateVariable(A if (m_scanner->currentCommentLiteral() != "") docstring = make_shared(m_scanner->currentCommentLiteral()); - FunctionHeaderParserResult header = parseFunctionHeader(false, true); + FunctionHeaderParserResult header = parseFunctionHeader(false, true, _contractName); if ( !header.modifiers.empty() || @@ -439,8 +447,6 @@ ASTPointer Parser::parseFunctionDefinitionOrFunctionTypeStateVariable(A } else m_scanner->next(); // just consume the ';' - if (_contractName && *header.name == *_contractName) - header.isConstructor = true; return nodeFactory.createNode( header.name, header.visibility, -- cgit v1.2.3 From 8f66390f56c866f70ab6617dac1f0713ace2d7ff Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 9 Mar 2018 14:23:48 +0100 Subject: Set isConstructor to false unconditionally and update to true later for constructors. --- libsolidity/parsing/Parser.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 6ae66eee..3dbd4c8f 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -341,13 +341,11 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader( RecursionGuard recursionGuard(*this); FunctionHeaderParserResult result; - 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 below. - result.isConstructor = false; - else if (m_scanner->currentToken() == Token::Identifier && m_scanner->currentLiteral() == "constructor") + result.isConstructor = false; + + if (m_scanner->currentToken() == Token::Identifier && m_scanner->currentLiteral() == "constructor") result.isConstructor = true; - else + else if (m_scanner->currentToken() != Token::Function) solAssert(false, "Function or constructor expected."); m_scanner->next(); -- cgit v1.2.3 From 96eff0ff6abc614cb44a01137dfd0df1ef750088 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Wed, 4 Apr 2018 20:53:23 +0200 Subject: Error when using empty parenthesis for base class constructors that require arguments. --- libsolidity/parsing/Parser.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 3dbd4c8f..9a7731d8 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -286,17 +286,17 @@ ASTPointer Parser::parseInheritanceSpecifier() RecursionGuard recursionGuard(*this); ASTNodeFactory nodeFactory(*this); ASTPointer name(parseUserDefinedTypeName()); - vector> arguments; + unique_ptr>> arguments; if (m_scanner->currentToken() == Token::LParen) { m_scanner->next(); - arguments = parseFunctionCallListArguments(); + arguments.reset(new vector>(parseFunctionCallListArguments())); nodeFactory.markEndPosition(); expectToken(Token::RParen); } else nodeFactory.setEndPositionFromNode(name); - return nodeFactory.createNode(name, arguments); + return nodeFactory.createNode(name, std::move(arguments)); } Declaration::Visibility Parser::parseVisibilitySpecifier(Token::Value _token) -- cgit v1.2.3 From 3eedbc6a9c60888dd967d6673a34511947da4aa1 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Tue, 10 Apr 2018 11:22:26 +0200 Subject: Error when using no parentheses in modifier-style constructor calls. --- libsolidity/parsing/Parser.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 9a7731d8..18ef740a 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -701,17 +701,17 @@ ASTPointer Parser::parseModifierInvocation() RecursionGuard recursionGuard(*this); ASTNodeFactory nodeFactory(*this); ASTPointer name(parseIdentifier()); - vector> arguments; + unique_ptr>> arguments; if (m_scanner->currentToken() == Token::LParen) { m_scanner->next(); - arguments = parseFunctionCallListArguments(); + arguments.reset(new vector>(parseFunctionCallListArguments())); nodeFactory.markEndPosition(); expectToken(Token::RParen); } else nodeFactory.setEndPositionFromNode(name); - return nodeFactory.createNode(name, arguments); + return nodeFactory.createNode(name, move(arguments)); } ASTPointer Parser::parseIdentifier() -- cgit v1.2.3 From 6d289783b41109b445fec924354580a79b65a94a Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 16 Mar 2018 11:02:35 +0100 Subject: Fix state variable parsing. --- libsolidity/parsing/Parser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 18ef740a..bdb46ad6 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -380,6 +380,14 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader( { if (result.visibility != Declaration::Visibility::Default) { + // There is the special case of a public state variable of function type. + // Detect this and return early. + if ( + (result.visibility == Declaration::Visibility::External || result.visibility == Declaration::Visibility::Internal) && + result.modifiers.empty() && + result.name->empty() + ) + break; parserError(string( "Visibility already specified as \"" + Declaration::visibilityToString(result.visibility) + -- cgit v1.2.3 From 2ad1acaf721072d27783d586048d6377be6c3f99 Mon Sep 17 00:00:00 2001 From: chriseth Date: Fri, 16 Mar 2018 12:39:30 +0100 Subject: Warn if modifiers are applied to functions without implementation. --- libsolidity/parsing/Parser.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index bdb46ad6..ea092a74 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -365,12 +365,12 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader( Token::Value token = m_scanner->currentToken(); if (_allowModifiers && token == Token::Identifier) { - // This can either be a modifier (function declaration) or the name of the - // variable (function type name plus variable). - if ( + // If the name is empty, then this can either be a modifier (fallback function declaration) + // or the name of the state variable (function type name plus variable). + if (result.name->empty() && ( m_scanner->peekNextToken() == Token::Semicolon || m_scanner->peekNextToken() == Token::Assign - ) + )) // Variable declaration, break here. break; else -- cgit v1.2.3 From b5a696ad48780bf0614eef2a737a2e89963d4640 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 11 Apr 2018 17:44:44 +0200 Subject: Properly cope with constructor headers. --- libsolidity/parsing/Parser.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index ea092a74..2d8ca7d3 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -365,9 +365,10 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader( Token::Value token = m_scanner->currentToken(); if (_allowModifiers && token == Token::Identifier) { - // If the name is empty, then this can either be a modifier (fallback function declaration) + // If the name is empty (and this is not a constructor), + // then this can either be a modifier (fallback function declaration) // or the name of the state variable (function type name plus variable). - if (result.name->empty() && ( + if ((result.name->empty() && !result.isConstructor) && ( m_scanner->peekNextToken() == Token::Semicolon || m_scanner->peekNextToken() == Token::Assign )) @@ -385,7 +386,7 @@ Parser::FunctionHeaderParserResult Parser::parseFunctionHeader( if ( (result.visibility == Declaration::Visibility::External || result.visibility == Declaration::Visibility::Internal) && result.modifiers.empty() && - result.name->empty() + (result.name->empty() && !result.isConstructor) ) break; parserError(string( @@ -437,6 +438,7 @@ ASTPointer Parser::parseFunctionDefinitionOrFunctionTypeStateVariable(A FunctionHeaderParserResult header = parseFunctionHeader(false, true, _contractName); if ( + header.isConstructor || !header.modifiers.empty() || !header.name->empty() || m_scanner->currentToken() == Token::Semicolon || @@ -802,6 +804,7 @@ ASTPointer Parser::parseFunctionType() RecursionGuard recursionGuard(*this); ASTNodeFactory nodeFactory(*this); FunctionHeaderParserResult header = parseFunctionHeader(true, false); + solAssert(!header.isConstructor, "Tried to parse type as constructor."); return nodeFactory.createNode( header.parameters, header.returnParameters, -- cgit v1.2.3 From 6862f2294357d18a4640e95e3f9235692e6c055e Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Thu, 12 Apr 2018 14:57:14 +0200 Subject: Fix internal compiler error when parsing ``var`` declaration without identifier. --- libsolidity/parsing/Parser.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'libsolidity/parsing/Parser.cpp') diff --git a/libsolidity/parsing/Parser.cpp b/libsolidity/parsing/Parser.cpp index 2d8ca7d3..618a0896 100644 --- a/libsolidity/parsing/Parser.cpp +++ b/libsolidity/parsing/Parser.cpp @@ -607,8 +607,10 @@ ASTPointer Parser::parseVariableDeclaration( if (_options.allowEmptyName && m_scanner->currentToken() != Token::Identifier) { identifier = make_shared(""); - solAssert(type != nullptr, ""); - nodeFactory.setEndPositionFromNode(type); + solAssert(!_options.allowVar, ""); // allowEmptyName && allowVar makes no sense + if (type) + nodeFactory.setEndPositionFromNode(type); + // if type is null this has already caused an error } else identifier = expectIdentifierToken(); -- cgit v1.2.3