Browse Source

Merge pull request #410 from chriseth/sol_exceptions

Improved exceptions and reporting exceptions for command-line compiler.
cl-refactor
Gav Wood 10 years ago
parent
commit
45f66e1180
  1. 58
      libsolidity/AST.cpp
  2. 10
      libsolidity/AST.h
  3. 6
      libsolidity/ASTPrinter.cpp
  4. 7
      libsolidity/BaseTypes.h
  5. 5
      libsolidity/Exceptions.h
  6. 26
      libsolidity/NameAndTypeResolver.cpp
  7. 29
      libsolidity/Parser.cpp
  8. 5
      libsolidity/Parser.h
  9. 2
      libsolidity/Scope.cpp
  10. 93
      libsolidity/SourceReferenceFormatter.cpp
  11. 48
      libsolidity/SourceReferenceFormatter.h
  12. 2
      libsolidity/Types.cpp
  13. 60
      solc/main.cpp

58
libsolidity/AST.cpp

@ -248,12 +248,16 @@ void Literal::accept(ASTVisitor& _visitor)
_visitor.endVisit(*this); _visitor.endVisit(*this);
} }
TypeError ASTNode::createTypeError(std::string const& _description)
{
return TypeError() << errinfo_sourceLocation(getLocation()) << errinfo_comment(_description);
}
void Statement::expectType(Expression& _expression, const Type& _expectedType) void Statement::expectType(Expression& _expression, const Type& _expectedType)
{ {
_expression.checkTypeRequirements(); _expression.checkTypeRequirements();
if (!_expression.getType()->isImplicitlyConvertibleTo(_expectedType)) if (!_expression.getType()->isImplicitlyConvertibleTo(_expectedType))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Type not implicitly convertible " BOOST_THROW_EXCEPTION(_expression.createTypeError("Type not implicitly convertible to expected type."));
"to expected type."));
//@todo provide more information to the exception //@todo provide more information to the exception
} }
@ -287,11 +291,10 @@ void Break::checkTypeRequirements()
void Return::checkTypeRequirements() void Return::checkTypeRequirements()
{ {
BOOST_ASSERT(m_returnParameters != nullptr); BOOST_ASSERT(m_returnParameters);
if (m_returnParameters->getParameters().size() != 1) if (m_returnParameters->getParameters().size() != 1)
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Different number of arguments in " BOOST_THROW_EXCEPTION(createTypeError("Different number of arguments in return statement "
"return statement than in returns " "than in returns declaration."));
"declaration."));
// this could later be changed such that the paramaters type is an anonymous struct type, // this could later be changed such that the paramaters type is an anonymous struct type,
// but for now, we only allow one return parameter // but for now, we only allow one return parameter
expectType(*m_expression, *m_returnParameters->getParameters().front()->getType()); expectType(*m_expression, *m_returnParameters->getParameters().front()->getType());
@ -327,7 +330,7 @@ void Assignment::checkTypeRequirements()
{ {
// complex assignment // complex assignment
if (!m_type->acceptsBinaryOperator(Token::AssignmentToBinaryOp(m_assigmentOperator))) if (!m_type->acceptsBinaryOperator(Token::AssignmentToBinaryOp(m_assigmentOperator)))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Operator not compatible with type.")); BOOST_THROW_EXCEPTION(createTypeError("Operator not compatible with type."));
} }
} }
@ -337,7 +340,7 @@ void UnaryOperation::checkTypeRequirements()
m_subExpression->checkTypeRequirements(); m_subExpression->checkTypeRequirements();
m_type = m_subExpression->getType(); m_type = m_subExpression->getType();
if (m_type->acceptsUnaryOperator(m_operator)) if (m_type->acceptsUnaryOperator(m_operator))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Unary operator not compatible with type.")); BOOST_THROW_EXCEPTION(createTypeError("Unary operator not compatible with type."));
} }
void BinaryOperation::checkTypeRequirements() void BinaryOperation::checkTypeRequirements()
@ -349,7 +352,7 @@ void BinaryOperation::checkTypeRequirements()
else if (m_left->getType()->isImplicitlyConvertibleTo(*m_right->getType())) else if (m_left->getType()->isImplicitlyConvertibleTo(*m_right->getType()))
m_commonType = m_right->getType(); m_commonType = m_right->getType();
else else
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("No common type found in binary operation.")); BOOST_THROW_EXCEPTION(createTypeError("No common type found in binary operation."));
if (Token::isCompareOp(m_operator)) if (Token::isCompareOp(m_operator))
m_type = std::make_shared<BoolType>(); m_type = std::make_shared<BoolType>();
else else
@ -357,7 +360,7 @@ void BinaryOperation::checkTypeRequirements()
BOOST_ASSERT(Token::isBinaryOp(m_operator)); BOOST_ASSERT(Token::isBinaryOp(m_operator));
m_type = m_commonType; m_type = m_commonType;
if (!m_commonType->acceptsBinaryOperator(m_operator)) if (!m_commonType->acceptsBinaryOperator(m_operator))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Operator not compatible with type.")); BOOST_THROW_EXCEPTION(createTypeError("Operator not compatible with type."));
} }
} }
@ -371,15 +374,14 @@ void FunctionCall::checkTypeRequirements()
if (category == Type::Category::TYPE) if (category == Type::Category::TYPE)
{ {
TypeType const* type = dynamic_cast<TypeType const*>(&expressionType); TypeType const* type = dynamic_cast<TypeType const*>(&expressionType);
BOOST_ASSERT(type != nullptr); BOOST_ASSERT(type);
//@todo for structs, we have to check the number of arguments to be equal to the //@todo for structs, we have to check the number of arguments to be equal to the
// number of non-mapping members // number of non-mapping members
if (m_arguments.size() != 1) if (m_arguments.size() != 1)
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("More than one argument for " BOOST_THROW_EXCEPTION(createTypeError("More than one argument for "
"explicit type conersion.")); "explicit type conersion."));
if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type->getActualType())) if (!m_arguments.front()->getType()->isExplicitlyConvertibleTo(*type->getActualType()))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Explicit type conversion not " BOOST_THROW_EXCEPTION(createTypeError("Explicit type conversion not allowed."));
"allowed."));
m_type = type->getActualType(); m_type = type->getActualType();
} }
else if (category == Type::Category::FUNCTION) else if (category == Type::Category::FUNCTION)
@ -388,16 +390,14 @@ void FunctionCall::checkTypeRequirements()
// and then ask if that is implicitly convertible to the struct represented by the // and then ask if that is implicitly convertible to the struct represented by the
// function parameters // function parameters
FunctionType const* function = dynamic_cast<FunctionType const*>(&expressionType); FunctionType const* function = dynamic_cast<FunctionType const*>(&expressionType);
BOOST_ASSERT(function != nullptr); BOOST_ASSERT(function);
FunctionDefinition const& fun = function->getFunction(); FunctionDefinition const& fun = function->getFunction();
std::vector<ASTPointer<VariableDeclaration>> const& parameters = fun.getParameters(); std::vector<ASTPointer<VariableDeclaration>> const& parameters = fun.getParameters();
if (parameters.size() != m_arguments.size()) if (parameters.size() != m_arguments.size())
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Wrong argument count for " BOOST_THROW_EXCEPTION(createTypeError("Wrong argument count for function call."));
"function call."));
for (size_t i = 0; i < m_arguments.size(); ++i) for (size_t i = 0; i < m_arguments.size(); ++i)
if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameters[i]->getType())) if (!m_arguments[i]->getType()->isImplicitlyConvertibleTo(*parameters[i]->getType()))
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Invalid type for argument in " BOOST_THROW_EXCEPTION(createTypeError("Invalid type for argument in function call."));
"function call."));
// @todo actually the return type should be an anonymous struct, // @todo actually the return type should be an anonymous struct,
// but we change it to the type of the first return value until we have structs // but we change it to the type of the first return value until we have structs
if (fun.getReturnParameterList()->getParameters().empty()) if (fun.getReturnParameterList()->getParameters().empty())
@ -406,9 +406,7 @@ void FunctionCall::checkTypeRequirements()
m_type = fun.getReturnParameterList()->getParameters().front()->getType(); m_type = fun.getReturnParameterList()->getParameters().front()->getType();
} }
else else
{ BOOST_THROW_EXCEPTION(createTypeError("Type does not support invocation."));
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Type does not support invocation."));
}
} }
void MemberAccess::checkTypeRequirements() void MemberAccess::checkTypeRequirements()
@ -425,7 +423,7 @@ void IndexAccess::checkTypeRequirements()
void Identifier::checkTypeRequirements() void Identifier::checkTypeRequirements()
{ {
BOOST_ASSERT(m_referencedDeclaration != nullptr); BOOST_ASSERT(m_referencedDeclaration);
//@todo these dynamic casts here are not really nice... //@todo these dynamic casts here are not really nice...
// is i useful to have an AST visitor here? // is i useful to have an AST visitor here?
// or can this already be done in NameAndTypeResolver? // or can this already be done in NameAndTypeResolver?
@ -435,24 +433,24 @@ void Identifier::checkTypeRequirements()
// var y = x; // var y = x;
// the type of x is not yet determined. // the type of x is not yet determined.
VariableDeclaration* variable = dynamic_cast<VariableDeclaration*>(m_referencedDeclaration); VariableDeclaration* variable = dynamic_cast<VariableDeclaration*>(m_referencedDeclaration);
if (variable != nullptr) if (variable)
{ {
if (!variable->getType()) if (!variable->getType())
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Variable referenced before type " BOOST_THROW_EXCEPTION(createTypeError("Variable referenced before type "
"could be determined.")); "could be determined."));
m_type = variable->getType(); m_type = variable->getType();
return; return;
} }
//@todo can we unify these with TypeName::toType()? //@todo can we unify these with TypeName::toType()?
StructDefinition* structDef = dynamic_cast<StructDefinition*>(m_referencedDeclaration); StructDefinition* structDef = dynamic_cast<StructDefinition*>(m_referencedDeclaration);
if (structDef != nullptr) if (structDef)
{ {
// note that we do not have a struct type here // note that we do not have a struct type here
m_type = std::make_shared<TypeType>(std::make_shared<StructType>(*structDef)); m_type = std::make_shared<TypeType>(std::make_shared<StructType>(*structDef));
return; return;
} }
FunctionDefinition* functionDef = dynamic_cast<FunctionDefinition*>(m_referencedDeclaration); FunctionDefinition* functionDef = dynamic_cast<FunctionDefinition*>(m_referencedDeclaration);
if (functionDef != nullptr) if (functionDef)
{ {
// a function reference is not a TypeType, because calling a TypeType converts to the type. // a function reference is not a TypeType, because calling a TypeType converts to the type.
// Calling a function (e.g. function(12), otherContract.function(34)) does not do a type // Calling a function (e.g. function(12), otherContract.function(34)) does not do a type
@ -461,7 +459,7 @@ void Identifier::checkTypeRequirements()
return; return;
} }
ContractDefinition* contractDef = dynamic_cast<ContractDefinition*>(m_referencedDeclaration); ContractDefinition* contractDef = dynamic_cast<ContractDefinition*>(m_referencedDeclaration);
if (contractDef != nullptr) if (contractDef)
{ {
m_type = std::make_shared<TypeType>(std::make_shared<ContractType>(*contractDef)); m_type = std::make_shared<TypeType>(std::make_shared<ContractType>(*contractDef));
return; return;

10
libsolidity/AST.h

@ -22,16 +22,16 @@
#pragma once #pragma once
#include <boost/noncopyable.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <boost/noncopyable.hpp>
#include <libsolidity/ASTForward.h> #include <libsolidity/ASTForward.h>
#include <libsolidity/BaseTypes.h> #include <libsolidity/BaseTypes.h>
#include <libsolidity/Token.h> #include <libsolidity/Token.h>
#include <libsolidity/Types.h> #include <libsolidity/Types.h>
#include <libsolidity/Exceptions.h>
namespace dev namespace dev
{ {
@ -57,6 +57,10 @@ public:
Location const& getLocation() const { return m_location; } Location const& getLocation() const { return m_location; }
/// Creates a @ref TypeError exception and decorates it with the location of the node and
/// the given description
TypeError createTypeError(std::string const& _description);
private: private:
Location m_location; Location m_location;
}; };
@ -165,7 +169,7 @@ public:
Declaration(_location, _name), m_typeName(_type) {} Declaration(_location, _name), m_typeName(_type) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
bool isTypeGivenExplicitly() const { return m_typeName.get() != nullptr; } bool isTypeGivenExplicitly() const { return bool(m_typeName); }
TypeName* getTypeName() const { return m_typeName.get(); } TypeName* getTypeName() const { return m_typeName.get(); }
//! Returns the declared or inferred type. Can be an empty pointer if no type was explicitly //! Returns the declared or inferred type. Can be an empty pointer if no type was explicitly

6
libsolidity/ASTPrinter.cpp

@ -253,7 +253,7 @@ bool ASTPrinter::visit(ElementaryTypeNameExpression& _node)
bool ASTPrinter::visit(Literal& _node) bool ASTPrinter::visit(Literal& _node)
{ {
char const* tokenString = Token::toString(_node.getToken()); char const* tokenString = Token::toString(_node.getToken());
if (tokenString == nullptr) if (!tokenString)
tokenString = "[no token]"; tokenString = "[no token]";
writeLine(std::string("Literal, token: ") + tokenString + " value: " + _node.getValue()); writeLine(std::string("Literal, token: ") + tokenString + " value: " + _node.getValue());
printType(_node); printType(_node);
@ -417,7 +417,7 @@ void ASTPrinter::printSourcePart(ASTNode const& _node)
{ {
Location const& location(_node.getLocation()); Location const& location(_node.getLocation());
*m_ostream << getIndentation() << " Source: |" *m_ostream << getIndentation() << " Source: |"
<< m_source.substr(location.start, location.end - location.start) << "|\n"; << m_source.substr(location.start, location.end - location.start) << "|" << std::endl;
} }
} }
@ -436,7 +436,7 @@ std::string ASTPrinter::getIndentation() const
void ASTPrinter::writeLine(std::string const& _line) void ASTPrinter::writeLine(std::string const& _line)
{ {
*m_ostream << getIndentation() << _line << '\n'; *m_ostream << getIndentation() << _line << std::endl;
} }
} }

7
libsolidity/BaseTypes.h

@ -22,6 +22,7 @@
#pragma once #pragma once
#include <ostream>
namespace dev namespace dev
{ {
@ -41,5 +42,11 @@ struct Location
int end; int end;
}; };
/// Stream output for Location (used e.g. in boost exceptions).
inline std::ostream& operator<<(std::ostream& _out, Location const& _location)
{
return _out << "[" << _location.start << "," << _location.end << ")";
}
} }
} }

5
libsolidity/Exceptions.h

@ -22,7 +22,9 @@
#pragma once #pragma once
#include <string>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include <libsolidity/BaseTypes.h>
namespace dev namespace dev
{ {
@ -33,5 +35,8 @@ struct ParserError: virtual Exception {};
struct TypeError: virtual Exception {}; struct TypeError: virtual Exception {};
struct DeclarationError: virtual Exception {}; struct DeclarationError: virtual Exception {};
typedef boost::error_info<struct tag_sourcePosition, int> errinfo_sourcePosition;
typedef boost::error_info<struct tag_sourceLocation, Location> errinfo_sourceLocation;
} }
} }

26
libsolidity/NameAndTypeResolver.cpp

@ -129,15 +129,17 @@ void DeclarationRegistrationHelper::enterNewSubScope(ASTNode& _node)
void DeclarationRegistrationHelper::closeCurrentScope() void DeclarationRegistrationHelper::closeCurrentScope()
{ {
BOOST_ASSERT(m_currentScope != nullptr); BOOST_ASSERT(m_currentScope);
m_currentScope = m_currentScope->getOuterScope(); m_currentScope = m_currentScope->getOuterScope();
} }
void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration, bool _opensScope) void DeclarationRegistrationHelper::registerDeclaration(Declaration& _declaration, bool _opensScope)
{ {
BOOST_ASSERT(m_currentScope != nullptr); BOOST_ASSERT(m_currentScope);
if (!m_currentScope->registerDeclaration(_declaration)) if (!m_currentScope->registerDeclaration(_declaration))
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_comment("Identifier already declared.")); BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_declaration.getLocation())
<< errinfo_comment("Identifier already declared."));
//@todo the exception should also contain the location of the first declaration
if (_opensScope) if (_opensScope)
enterNewSubScope(_declaration); enterNewSubScope(_declaration);
} }
@ -153,14 +155,14 @@ void ReferencesResolver::endVisit(VariableDeclaration& _variable)
{ {
// endVisit because the internal type needs resolving if it is a user defined type // endVisit because the internal type needs resolving if it is a user defined type
// or mapping // or mapping
if (_variable.getTypeName() != nullptr) if (_variable.getTypeName())
_variable.setType(_variable.getTypeName()->toType()); _variable.setType(_variable.getTypeName()->toType());
// otherwise we have a "var"-declaration whose type is resolved by the first assignment // otherwise we have a "var"-declaration whose type is resolved by the first assignment
} }
bool ReferencesResolver::visit(Return& _return) bool ReferencesResolver::visit(Return& _return)
{ {
BOOST_ASSERT(m_returnParameters != nullptr); BOOST_ASSERT(m_returnParameters);
_return.setFunctionReturnParameters(*m_returnParameters); _return.setFunctionReturnParameters(*m_returnParameters);
return true; return true;
} }
@ -174,12 +176,13 @@ bool ReferencesResolver::visit(Mapping&)
bool ReferencesResolver::visit(UserDefinedTypeName& _typeName) bool ReferencesResolver::visit(UserDefinedTypeName& _typeName)
{ {
Declaration* declaration = m_resolver.getNameFromCurrentScope(_typeName.getName()); Declaration* declaration = m_resolver.getNameFromCurrentScope(_typeName.getName());
if (declaration == nullptr) if (!declaration)
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_comment("Undeclared identifier.")); BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_typeName.getLocation())
<< errinfo_comment("Undeclared identifier."));
StructDefinition* referencedStruct = dynamic_cast<StructDefinition*>(declaration); StructDefinition* referencedStruct = dynamic_cast<StructDefinition*>(declaration);
//@todo later, contracts are also valid types //@todo later, contracts are also valid types
if (referencedStruct == nullptr) if (!referencedStruct)
BOOST_THROW_EXCEPTION(TypeError() << errinfo_comment("Identifier does not name a type name.")); BOOST_THROW_EXCEPTION(_typeName.createTypeError("Identifier does not name a type name."));
_typeName.setReferencedStruct(*referencedStruct); _typeName.setReferencedStruct(*referencedStruct);
return false; return false;
} }
@ -187,8 +190,9 @@ bool ReferencesResolver::visit(UserDefinedTypeName& _typeName)
bool ReferencesResolver::visit(Identifier& _identifier) bool ReferencesResolver::visit(Identifier& _identifier)
{ {
Declaration* declaration = m_resolver.getNameFromCurrentScope(_identifier.getName()); Declaration* declaration = m_resolver.getNameFromCurrentScope(_identifier.getName());
if (declaration == nullptr) if (!declaration)
BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_comment("Undeclared identifier.")); BOOST_THROW_EXCEPTION(DeclarationError() << errinfo_sourceLocation(_identifier.getLocation())
<< errinfo_comment("Undeclared identifier."));
_identifier.setReferencedDeclaration(*declaration); _identifier.setReferencedDeclaration(*declaration);
return false; return false;
} }

29
libsolidity/Parser.cpp

@ -106,7 +106,7 @@ ASTPointer<ContractDefinition> Parser::parseContractDefinition()
expectToken(Token::SEMICOLON); expectToken(Token::SEMICOLON);
} }
else else
throwExpectationError("Function, variable or struct declaration expected."); BOOST_THROW_EXCEPTION(createParserError("Function, variable or struct declaration expected."));
} }
nodeFactory.markEndPosition(); nodeFactory.markEndPosition();
expectToken(Token::RBRACE); expectToken(Token::RBRACE);
@ -184,7 +184,7 @@ ASTPointer<TypeName> Parser::parseTypeName(bool _allowVar)
else if (token == Token::VAR) else if (token == Token::VAR)
{ {
if (!_allowVar) if (!_allowVar)
throwExpectationError("Expected explicit type name."); BOOST_THROW_EXCEPTION(createParserError("Expected explicit type name."));
m_scanner->next(); m_scanner->next();
} }
else if (token == Token::MAPPING) else if (token == Token::MAPPING)
@ -198,7 +198,7 @@ ASTPointer<TypeName> Parser::parseTypeName(bool _allowVar)
type = nodeFactory.createNode<UserDefinedTypeName>(expectIdentifierToken()); type = nodeFactory.createNode<UserDefinedTypeName>(expectIdentifierToken());
} }
else else
throwExpectationError("Expected type name"); BOOST_THROW_EXCEPTION(createParserError("Expected type name"));
return type; return type;
} }
@ -208,7 +208,7 @@ ASTPointer<Mapping> Parser::parseMapping()
expectToken(Token::MAPPING); expectToken(Token::MAPPING);
expectToken(Token::LPAREN); expectToken(Token::LPAREN);
if (!Token::isElementaryTypeName(m_scanner->getCurrentToken())) if (!Token::isElementaryTypeName(m_scanner->getCurrentToken()))
throwExpectationError("Expected elementary type name for mapping key type"); BOOST_THROW_EXCEPTION(createParserError("Expected elementary type name for mapping key type"));
ASTPointer<ElementaryTypeName> keyType; ASTPointer<ElementaryTypeName> keyType;
keyType = ASTNodeFactory(*this).createNode<ElementaryTypeName>(m_scanner->getCurrentToken()); keyType = ASTNodeFactory(*this).createNode<ElementaryTypeName>(m_scanner->getCurrentToken());
m_scanner->next(); m_scanner->next();
@ -481,7 +481,7 @@ ASTPointer<Expression> Parser::parsePrimaryExpression()
} }
else else
{ {
throwExpectationError("Expected primary expression."); BOOST_THROW_EXCEPTION(createParserError("Expected primary expression."));
return ASTPointer<Expression>(); // this is not reached return ASTPointer<Expression>(); // this is not reached
} }
break; break;
@ -507,7 +507,7 @@ std::vector<ASTPointer<Expression>> Parser::parseFunctionCallArguments()
void Parser::expectToken(Token::Value _value) void Parser::expectToken(Token::Value _value)
{ {
if (m_scanner->getCurrentToken() != _value) if (m_scanner->getCurrentToken() != _value)
throwExpectationError(std::string("Expected token ") + std::string(Token::getName(_value))); BOOST_THROW_EXCEPTION(createParserError(std::string("Expected token ") + std::string(Token::getName(_value))));
m_scanner->next(); m_scanner->next();
} }
@ -515,7 +515,7 @@ Token::Value Parser::expectAssignmentOperator()
{ {
Token::Value op = m_scanner->getCurrentToken(); Token::Value op = m_scanner->getCurrentToken();
if (!Token::isAssignmentOp(op)) if (!Token::isAssignmentOp(op))
throwExpectationError(std::string("Expected assignment operator")); BOOST_THROW_EXCEPTION(createParserError("Expected assignment operator"));
m_scanner->next(); m_scanner->next();
return op; return op;
} }
@ -523,7 +523,7 @@ Token::Value Parser::expectAssignmentOperator()
ASTPointer<ASTString> Parser::expectIdentifierToken() ASTPointer<ASTString> Parser::expectIdentifierToken()
{ {
if (m_scanner->getCurrentToken() != Token::IDENTIFIER) if (m_scanner->getCurrentToken() != Token::IDENTIFIER)
throwExpectationError("Expected identifier"); BOOST_THROW_EXCEPTION(createParserError("Expected identifier"));
return getLiteralAndAdvance(); return getLiteralAndAdvance();
} }
@ -534,18 +534,9 @@ ASTPointer<ASTString> Parser::getLiteralAndAdvance()
return identifier; return identifier;
} }
void Parser::throwExpectationError(std::string const& _description) ParserError Parser::createParserError(std::string const& _description) const
{ {
//@todo put some of this stuff into ParserError return ParserError() << errinfo_sourcePosition(getPosition()) << errinfo_comment(_description);
int line, column;
std::tie(line, column) = m_scanner->translatePositionToLineColumn(getPosition());
std::stringstream buf;
buf << "Solidity parser error: " << _description
<< " at line " << (line + 1)
<< ", column " << (column + 1) << "\n"
<< m_scanner->getLineAtPosition(getPosition()) << "\n"
<< std::string(column, ' ') << "^";
BOOST_THROW_EXCEPTION(ParserError() << errinfo_comment(buf.str()));
} }

5
libsolidity/Parser.h

@ -73,9 +73,12 @@ private:
Token::Value expectAssignmentOperator(); Token::Value expectAssignmentOperator();
ASTPointer<ASTString> expectIdentifierToken(); ASTPointer<ASTString> expectIdentifierToken();
ASTPointer<ASTString> getLiteralAndAdvance(); ASTPointer<ASTString> getLiteralAndAdvance();
void throwExpectationError(std::string const& _description);
/// @} /// @}
/// Creates a @ref ParserError exception and annotates it with the current position and the
/// given @a _description.
ParserError createParserError(std::string const& _description) const;
std::shared_ptr<Scanner> m_scanner; std::shared_ptr<Scanner> m_scanner;
}; };

2
libsolidity/Scope.cpp

@ -41,7 +41,7 @@ Declaration* Scope::resolveName(ASTString const& _name, bool _recursive) const
auto result = m_declarations.find(_name); auto result = m_declarations.find(_name);
if (result != m_declarations.end()) if (result != m_declarations.end())
return result->second; return result->second;
if (_recursive && m_outerScope != nullptr) if (_recursive && m_outerScope)
return m_outerScope->resolveName(_name, true); return m_outerScope->resolveName(_name, true);
return nullptr; return nullptr;
} }

93
libsolidity/SourceReferenceFormatter.cpp

@ -0,0 +1,93 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Formatting functions for errors referencing positions and locations in the source.
*/
#include <libsolidity/SourceReferenceFormatter.h>
#include <libsolidity/Scanner.h>
#include <libsolidity/Exceptions.h>
namespace dev
{
namespace solidity
{
void SourceReferenceFormatter::printSourceLocation(std::ostream& _stream,
Location const& _location,
Scanner const& _scanner)
{
int startLine;
int startColumn;
std::tie(startLine, startColumn) = _scanner.translatePositionToLineColumn(_location.start);
_stream << "starting at line " << (startLine + 1) << ", column " << (startColumn + 1) << "\n";
int endLine;
int endColumn;
std::tie(endLine, endColumn) = _scanner.translatePositionToLineColumn(_location.end);
if (startLine == endLine)
{
_stream << _scanner.getLineAtPosition(_location.start) << std::endl
<< std::string(startColumn, ' ') << "^";
if (endColumn > startColumn + 2)
_stream << std::string(endColumn - startColumn - 2, '-');
if (endColumn > startColumn + 1)
_stream << "^";
_stream << std::endl;
}
else
_stream << _scanner.getLineAtPosition(_location.start) << std::endl
<< std::string(startColumn, ' ') << "^\n"
<< "Spanning multiple lines.\n";
}
void SourceReferenceFormatter::printSourcePosition(std::ostream& _stream,
int _position,
const Scanner& _scanner)
{
int line;
int column;
std::tie(line, column) = _scanner.translatePositionToLineColumn(_position);
_stream << "at line " << (line + 1) << ", column " << (column + 1) << std::endl
<< _scanner.getLineAtPosition(_position) << std::endl
<< std::string(column, ' ') << "^" << std::endl;
}
void SourceReferenceFormatter::printExceptionInformation(std::ostream& _stream,
Exception const& _exception,
std::string const& _name,
Scanner const& _scanner)
{
_stream << _name;
if (std::string const* description = boost::get_error_info<errinfo_comment>(_exception))
_stream << ": " << *description;
if (int const* position = boost::get_error_info<errinfo_sourcePosition>(_exception))
{
_stream << " ";
printSourcePosition(_stream, *position, _scanner);
}
if (Location const* location = boost::get_error_info<errinfo_sourceLocation>(_exception))
{
_stream << " ";
printSourceLocation(_stream, *location, _scanner);
}
}
}
}

48
libsolidity/SourceReferenceFormatter.h

@ -0,0 +1,48 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum 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.
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @author Christian <c@ethdev.com>
* @date 2014
* Formatting functions for errors referencing positions and locations in the source.
*/
#pragma once
#include <ostream>
#include <libsolidity/BaseTypes.h>
namespace dev
{
class Exception; // forward
namespace solidity
{
class Scanner; // forward
struct SourceReferenceFormatter
{
public:
static void printSourceLocation(std::ostream& _stream, Location const& _location, Scanner const& _scanner);
static void printSourcePosition(std::ostream& _stream, int _position, Scanner const& _scanner);
static void printExceptionInformation(std::ostream& _stream, Exception const& _exception,
std::string const& _name, Scanner const& _scanner);
};
}
}

2
libsolidity/Types.cpp

@ -89,9 +89,9 @@ std::shared_ptr<IntegerType> IntegerType::smallestTypeForLiteral(std::string con
IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier): IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier):
m_bits(_bits), m_modifier(_modifier) m_bits(_bits), m_modifier(_modifier)
{ {
BOOST_ASSERT(_bits > 0 && _bits <= 256 && _bits % 8 == 0);
if (isAddress()) if (isAddress())
_bits = 160; _bits = 160;
BOOST_ASSERT(_bits > 0 && _bits <= 256 && _bits % 8 == 0);
} }
bool IntegerType::isImplicitlyConvertibleTo(Type const& _convertTo) const bool IntegerType::isImplicitlyConvertibleTo(Type const& _convertTo) const

60
solc/main.cpp

@ -9,21 +9,11 @@
#include <libsolidity/Parser.h> #include <libsolidity/Parser.h>
#include <libsolidity/ASTPrinter.h> #include <libsolidity/ASTPrinter.h>
#include <libsolidity/NameAndTypeResolver.h> #include <libsolidity/NameAndTypeResolver.h>
#include <libsolidity/Exceptions.h>
#include <libsolidity/SourceReferenceFormatter.h>
namespace dev using namespace dev;
{ using namespace solidity;
namespace solidity
{
ASTPointer<ContractDefinition> parseAST(std::string const& _source)
{
ASTPointer<Scanner> scanner = std::make_shared<Scanner>(CharStream(_source));
Parser parser;
return parser.parse(scanner);
}
}
} // end namespaces
void help() void help()
{ {
@ -57,28 +47,50 @@ int main(int argc, char** argv)
else else
infile = argv[i]; infile = argv[i];
} }
std::string src; std::string sourceCode;
if (infile.empty()) if (infile.empty())
{ {
std::string s; std::string s;
while (!std::cin.eof()) while (!std::cin.eof())
{ {
getline(std::cin, s); getline(std::cin, s);
src.append(s); sourceCode.append(s);
} }
} }
else else
sourceCode = asString(dev::contents(infile));
ASTPointer<ContractDefinition> ast;
std::shared_ptr<Scanner> scanner = std::make_shared<Scanner>(CharStream(sourceCode));
Parser parser;
try
{
ast = parser.parse(scanner);
}
catch (ParserError const& exception)
{
SourceReferenceFormatter::printExceptionInformation(std::cerr, exception, "Parser error", *scanner);
return -1;
}
dev::solidity::NameAndTypeResolver resolver;
try
{ {
src = dev::asString(dev::contents(infile)); resolver.resolveNamesAndTypes(*ast.get());
} }
std::cout << "Parsing..." << std::endl; catch (DeclarationError const& exception)
// @todo catch exception {
dev::solidity::ASTPointer<dev::solidity::ContractDefinition> ast = dev::solidity::parseAST(src); SourceReferenceFormatter::printExceptionInformation(std::cerr, exception, "Declaration error", *scanner);
return -1;
}
catch (TypeError const& exception)
{
SourceReferenceFormatter::printExceptionInformation(std::cerr, exception, "Type error", *scanner);
return -1;
}
std::cout << "Syntax tree for the contract:" << std::endl; std::cout << "Syntax tree for the contract:" << std::endl;
dev::solidity::ASTPrinter printer(ast, src); dev::solidity::ASTPrinter printer(ast, sourceCode);
printer.print(std::cout); printer.print(std::cout);
std::cout << "Resolving identifiers..." << std::endl;
dev::solidity::NameAndTypeResolver resolver;
resolver.resolveNamesAndTypes(*ast.get());
return 0; return 0;
} }

Loading…
Cancel
Save