2023-06-14 10:48:38 +00:00
/*
This file is part of solidity .
solidity is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
solidity is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with solidity . If not , see < http : //www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
# include <libsolidity/experimental/analysis/SyntaxRestrictor.h>
# include <libsolidity/experimental/analysis/Analysis.h>
# include <liblangutil/Exceptions.h>
using namespace solidity : : frontend ;
using namespace solidity : : frontend : : experimental ;
using namespace solidity : : langutil ;
SyntaxRestrictor : : SyntaxRestrictor ( Analysis & _analysis ) : m_errorReporter ( _analysis . errorReporter ( ) )
{ }
bool SyntaxRestrictor : : analyze ( ASTNode const & _astRoot )
{
_astRoot . accept ( * this ) ;
return ! Error : : containsErrors ( m_errorReporter . errors ( ) ) ;
}
bool SyntaxRestrictor : : visitNode ( ASTNode const & _node )
{
if ( ! _node . experimentalSolidityOnly ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 9282 _error , _node . location ( ) , " Unsupported AST node. " ) ;
2023-06-14 10:48:38 +00:00
return false ;
}
bool SyntaxRestrictor : : visit ( ContractDefinition const & _contractDefinition )
{
if ( _contractDefinition . contractKind ( ) ! = ContractKind : : Contract )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 9159 _error , _contractDefinition . location ( ) , " Only contracts are supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( ! _contractDefinition . baseContracts ( ) . empty ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 5731 _error , _contractDefinition . location ( ) , " Inheritance unsupported. " ) ;
2023-06-14 10:48:38 +00:00
return true ;
}
bool SyntaxRestrictor : : visit ( FunctionDefinition const & _functionDefinition )
{
if ( ! _functionDefinition . isImplemented ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 1741 _error , _functionDefinition . location ( ) , " Functions must be implemented. " ) ;
2023-06-14 10:48:38 +00:00
if ( ! _functionDefinition . modifiers ( ) . empty ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 9988 _error , _functionDefinition . location ( ) , " Function may not have modifiers. " ) ;
2023-06-14 10:48:38 +00:00
if ( _functionDefinition . overrides ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 5044 _error , _functionDefinition . location ( ) , " Function may not have override specifiers. " ) ;
2023-06-14 10:48:38 +00:00
solAssert ( ! _functionDefinition . returnParameterList ( ) ) ;
if ( _functionDefinition . isFree ( ) )
{
if ( _functionDefinition . stateMutability ( ) ! = StateMutability : : NonPayable )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 5714 _error , _functionDefinition . location ( ) , " Free functions may not have a mutability. " ) ;
2023-06-14 10:48:38 +00:00
}
else
{
if ( _functionDefinition . isFallback ( ) )
{
if ( _functionDefinition . visibility ( ) ! = Visibility : : External )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 7341 _error , _functionDefinition . location ( ) , " Fallback function must be external. " ) ;
2023-06-14 10:48:38 +00:00
}
else
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 4496 _error , _functionDefinition . location ( ) , " Only fallback functions are supported in contracts. " ) ;
2023-06-14 10:48:38 +00:00
}
return true ;
}
bool SyntaxRestrictor : : visit ( VariableDeclarationStatement const & _variableDeclarationStatement )
{
if ( _variableDeclarationStatement . declarations ( ) . size ( ) = = 1 )
{
if ( ! _variableDeclarationStatement . declarations ( ) . front ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 9658 _error , _variableDeclarationStatement . initialValue ( ) - > location ( ) , " Variable declaration has to declare a single variable. " ) ;
2023-06-14 10:48:38 +00:00
}
else
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 3520 _error , _variableDeclarationStatement . initialValue ( ) - > location ( ) , " Variable declarations can only declare a single variable. " ) ;
2023-06-14 10:48:38 +00:00
return true ;
}
bool SyntaxRestrictor : : visit ( VariableDeclaration const & _variableDeclaration )
{
if ( _variableDeclaration . value ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 1801 _error , _variableDeclaration . value ( ) - > location ( ) , " Variable declarations with initial value not supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( _variableDeclaration . isStateVariable ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 6388 _error , _variableDeclaration . location ( ) , " State variables are not supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( ! _variableDeclaration . isLocalVariable ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 8953 _error , _variableDeclaration . location ( ) , " Only local variables are supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( _variableDeclaration . mutability ( ) ! = VariableDeclaration : : Mutability : : Mutable )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 2934 _error , _variableDeclaration . location ( ) , " Only mutable variables are supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( _variableDeclaration . isIndexed ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 9603 _error , _variableDeclaration . location ( ) , " Indexed variables are not supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( ! _variableDeclaration . noVisibilitySpecified ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 8809 _error , _variableDeclaration . location ( ) , " Variables with visibility not supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( _variableDeclaration . overrides ( ) )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 6175 _error , _variableDeclaration . location ( ) , " Variables with override specifier not supported. " ) ;
2023-06-14 10:48:38 +00:00
if ( _variableDeclaration . referenceLocation ( ) ! = VariableDeclaration : : Location : : Unspecified )
2023-08-22 16:17:14 +00:00
m_errorReporter . syntaxError ( 5360 _error , _variableDeclaration . location ( ) , " Variables with reference location not supported. " ) ;
2023-06-14 10:48:38 +00:00
return true ;
}