Browse Source

Merge pull request #1132 from LefterisJP/sol_DebuggingInformation

Location information in AssemblyItems, filled by Solidity.
cl-refactor
chriseth 10 years ago
parent
commit
fc1281229b
  1. 3
      libevmcore/Assembly.cpp
  2. 13
      libevmcore/Assembly.h
  3. 24
      libevmcore/SourceLocation.h
  4. 96
      libsolidity/AST.h
  5. 2
      libsolidity/ASTPrinter.cpp
  6. 25
      libsolidity/Compiler.cpp
  7. 6
      libsolidity/Compiler.h
  8. 37
      libsolidity/CompilerContext.cpp
  9. 28
      libsolidity/CompilerContext.h
  10. 4
      libsolidity/Exceptions.h
  11. 20
      libsolidity/ExpressionCompiler.cpp
  12. 10
      libsolidity/ExpressionCompiler.h
  13. 16
      libsolidity/Parser.cpp
  14. 4
      libsolidity/Parser.h
  15. 10
      libsolidity/Scanner.h
  16. 4
      libsolidity/SourceReferenceFormatter.cpp
  17. 4
      libsolidity/SourceReferenceFormatter.h
  18. 4
      mix/CodeHighlighter.cpp
  19. 5
      mix/CodeHighlighter.h
  20. 3
      mix/CodeModel.cpp
  21. 129
      test/Assembly.cpp
  22. 1
      test/SolidityExpressionCompiler.cpp
  23. 2
      test/SolidityInterface.cpp

3
libevmcore/Assembly.cpp

@ -229,10 +229,11 @@ ostream& Assembly::streamRLP(ostream& _out, string const& _prefix) const
return _out; return _out;
} }
AssemblyItem const& Assembly::append(AssemblyItem const& _i) AssemblyItem const& Assembly::append(AssemblyItem const& _i, SourceLocation const& _location)
{ {
m_deposit += _i.deposit(); m_deposit += _i.deposit();
m_items.push_back(_i); m_items.push_back(_i);
m_items.back().setLocation(_location);
return back(); return back();
} }

13
libevmcore/Assembly.h

@ -24,6 +24,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libevmcore/SourceLocation.h>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
#include "Exceptions.h" #include "Exceptions.h"
@ -57,10 +58,13 @@ public:
int deposit() const; int deposit() const;
bool match(AssemblyItem const& _i) const { return _i.m_type == UndefinedItem || (m_type == _i.m_type && (m_type != Operation || m_data == _i.m_data)); } bool match(AssemblyItem const& _i) const { return _i.m_type == UndefinedItem || (m_type == _i.m_type && (m_type != Operation || m_data == _i.m_data)); }
void setLocation(SourceLocation const& _location) { m_location = _location;}
SourceLocation const& getLocation() const { return m_location; }
private: private:
AssemblyItemType m_type; AssemblyItemType m_type;
u256 m_data; u256 m_data;
SourceLocation m_location;
}; };
using AssemblyItems = std::vector<AssemblyItem>; using AssemblyItems = std::vector<AssemblyItem>;
@ -84,9 +88,9 @@ public:
AssemblyItem append() { return append(newTag()); } AssemblyItem append() { return append(newTag()); }
void append(Assembly const& _a); void append(Assembly const& _a);
void append(Assembly const& _a, int _deposit); void append(Assembly const& _a, int _deposit);
AssemblyItem const& append(AssemblyItem const& _i); AssemblyItem const& append(AssemblyItem const& _i, SourceLocation const& _location = SourceLocation());
AssemblyItem const& append(std::string const& _data) { return append(newPushString(_data)); } AssemblyItem const& append(std::string const& _data, SourceLocation const& _location = SourceLocation()) { return append(newPushString(_data), _location); }
AssemblyItem const& append(bytes const& _data) { return append(newData(_data)); } AssemblyItem const& append(bytes const& _data, SourceLocation const& _location = SourceLocation()) { return append(newData(_data), _location); }
AssemblyItem appendSubSize(Assembly const& _a) { auto ret = newSub(_a); append(newPushSubSize(ret.data())); return ret; } AssemblyItem appendSubSize(Assembly const& _a) { auto ret = newSub(_a); append(newPushSubSize(ret.data())); return ret; }
/// Pushes the final size of the current assembly itself. Use this when the code is modified /// Pushes the final size of the current assembly itself. Use this when the code is modified
/// after compilation and CODESIZE is not an option. /// after compilation and CODESIZE is not an option.
@ -99,7 +103,8 @@ public:
template <class T> Assembly& operator<<(T const& _d) { append(_d); return *this; } template <class T> Assembly& operator<<(T const& _d) { append(_d); return *this; }
AssemblyItem const& back() { return m_items.back(); } AssemblyItems const& getItems() const { return m_items; }
AssemblyItem const& back() const { return m_items.back(); }
std::string backString() const { return m_items.size() && m_items.back().m_type == PushString ? m_strings.at((h256)m_items.back().m_data) : std::string(); } std::string backString() const { return m_items.size() && m_items.back().m_type == PushString ? m_strings.at((h256)m_items.back().m_data) : std::string(); }
void onePath() { if (asserts(!m_totalDeposit && !m_baseDeposit)) BOOST_THROW_EXCEPTION(InvalidDeposit()); m_baseDeposit = m_deposit; m_totalDeposit = INT_MAX; } void onePath() { if (asserts(!m_totalDeposit && !m_baseDeposit)) BOOST_THROW_EXCEPTION(InvalidDeposit()); m_baseDeposit = m_deposit; m_totalDeposit = INT_MAX; }

24
libsolidity/BaseTypes.h → libevmcore/SourceLocation.h

@ -15,9 +15,9 @@
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* @author Christian <c@ethdev.com> * @author Lefteris Karapetsas <lefteris@ethdev.com>
* @date 2014 * @date 2015
* Some elementary types for the parser. * Represents a location in a source file
*/ */
#pragma once #pragma once
@ -28,18 +28,23 @@
namespace dev namespace dev
{ {
namespace solidity
{
/** /**
* Representation of an interval of source positions. * Representation of an interval of source positions.
* The interval includes start and excludes end. * The interval includes start and excludes end.
*/ */
struct Location struct SourceLocation
{ {
Location(int _start, int _end, std::shared_ptr<std::string const> _sourceName): SourceLocation(int _start, int _end, std::shared_ptr<std::string const> _sourceName):
start(_start), end(_end), sourceName(_sourceName) { } start(_start), end(_end), sourceName(_sourceName) { }
Location(): start(-1), end(-1) { } SourceLocation(): start(-1), end(-1) { }
SourceLocation(SourceLocation const& _other):
start(_other.start), end(_other.end), sourceName(_other.sourceName) {}
SourceLocation& operator=(SourceLocation const& _other) { start = _other.start; end = _other.end; sourceName = _other.sourceName; return *this;}
bool operator==(SourceLocation const& _other) const { return start == _other.start && end == _other.end;}
bool operator!=(SourceLocation const& _other) const { return !operator==(_other); }
bool isEmpty() const { return start == -1 && end == -1; } bool isEmpty() const { return start == -1 && end == -1; }
@ -49,7 +54,7 @@ struct Location
}; };
/// Stream output for Location (used e.g. in boost exceptions). /// Stream output for Location (used e.g. in boost exceptions).
inline std::ostream& operator<<(std::ostream& _out, Location const& _location) inline std::ostream& operator<<(std::ostream& _out, SourceLocation const& _location)
{ {
if (_location.isEmpty()) if (_location.isEmpty())
return _out << "NO_LOCATION_SPECIFIED"; return _out << "NO_LOCATION_SPECIFIED";
@ -57,4 +62,3 @@ inline std::ostream& operator<<(std::ostream& _out, Location const& _location)
} }
} }
}

96
libsolidity/AST.h

@ -27,9 +27,9 @@
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <libevmcore/SourceLocation.h>
#include <libsolidity/Utils.h> #include <libsolidity/Utils.h>
#include <libsolidity/ASTForward.h> #include <libsolidity/ASTForward.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> #include <libsolidity/Exceptions.h>
@ -51,7 +51,7 @@ class ASTConstVisitor;
class ASTNode: private boost::noncopyable class ASTNode: private boost::noncopyable
{ {
public: public:
explicit ASTNode(Location const& _location): m_location(_location) {} explicit ASTNode(SourceLocation const& _location): m_location(_location) {}
virtual ~ASTNode() {} virtual ~ASTNode() {}
@ -71,7 +71,7 @@ public:
} }
/// Returns the source code location of this node. /// Returns the source code location of this node.
Location const& getLocation() const { return m_location; } SourceLocation const& getLocation() const { return m_location; }
/// Creates a @ref TypeError exception and decorates it with the location of the node and /// Creates a @ref TypeError exception and decorates it with the location of the node and
/// the given description /// the given description
@ -85,7 +85,7 @@ public:
///@} ///@}
private: private:
Location m_location; SourceLocation m_location;
}; };
/** /**
@ -94,7 +94,7 @@ private:
class SourceUnit: public ASTNode class SourceUnit: public ASTNode
{ {
public: public:
SourceUnit(Location const& _location, std::vector<ASTPointer<ASTNode>> const& _nodes): SourceUnit(SourceLocation const& _location, std::vector<ASTPointer<ASTNode>> const& _nodes):
ASTNode(_location), m_nodes(_nodes) {} ASTNode(_location), m_nodes(_nodes) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -114,7 +114,7 @@ private:
class ImportDirective: public ASTNode class ImportDirective: public ASTNode
{ {
public: public:
ImportDirective(Location const& _location, ASTPointer<ASTString> const& _identifier): ImportDirective(SourceLocation const& _location, ASTPointer<ASTString> const& _identifier):
ASTNode(_location), m_identifier(_identifier) {} ASTNode(_location), m_identifier(_identifier) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -135,7 +135,7 @@ public:
/// Visibility ordered from restricted to unrestricted. /// Visibility ordered from restricted to unrestricted.
enum class Visibility { Default, Private, Internal, Public, External }; enum class Visibility { Default, Private, Internal, Public, External };
Declaration(Location const& _location, ASTPointer<ASTString> const& _name, Declaration(SourceLocation const& _location, ASTPointer<ASTString> const& _name,
Visibility _visibility = Visibility::Default): Visibility _visibility = Visibility::Default):
ASTNode(_location), m_name(_name), m_visibility(_visibility), m_scope(nullptr) {} ASTNode(_location), m_name(_name), m_visibility(_visibility), m_scope(nullptr) {}
@ -205,7 +205,7 @@ protected:
class ContractDefinition: public Declaration, public Documented class ContractDefinition: public Declaration, public Documented
{ {
public: public:
ContractDefinition(Location const& _location, ContractDefinition(SourceLocation const& _location,
ASTPointer<ASTString> const& _name, ASTPointer<ASTString> const& _name,
ASTPointer<ASTString> const& _documentation, ASTPointer<ASTString> const& _documentation,
std::vector<ASTPointer<InheritanceSpecifier>> const& _baseContracts, std::vector<ASTPointer<InheritanceSpecifier>> const& _baseContracts,
@ -278,7 +278,7 @@ private:
class InheritanceSpecifier: public ASTNode class InheritanceSpecifier: public ASTNode
{ {
public: public:
InheritanceSpecifier(Location const& _location, ASTPointer<Identifier> const& _baseName, InheritanceSpecifier(SourceLocation const& _location, ASTPointer<Identifier> const& _baseName,
std::vector<ASTPointer<Expression>> _arguments): std::vector<ASTPointer<Expression>> _arguments):
ASTNode(_location), m_baseName(_baseName), m_arguments(_arguments) {} ASTNode(_location), m_baseName(_baseName), m_arguments(_arguments) {}
@ -298,7 +298,7 @@ private:
class StructDefinition: public Declaration class StructDefinition: public Declaration
{ {
public: public:
StructDefinition(Location const& _location, StructDefinition(SourceLocation const& _location,
ASTPointer<ASTString> const& _name, ASTPointer<ASTString> const& _name,
std::vector<ASTPointer<VariableDeclaration>> const& _members): std::vector<ASTPointer<VariableDeclaration>> const& _members):
Declaration(_location, _name), m_members(_members) {} Declaration(_location, _name), m_members(_members) {}
@ -323,7 +323,7 @@ private:
class EnumDefinition: public Declaration class EnumDefinition: public Declaration
{ {
public: public:
EnumDefinition(Location const& _location, EnumDefinition(SourceLocation const& _location,
ASTPointer<ASTString> const& _name, ASTPointer<ASTString> const& _name,
std::vector<ASTPointer<EnumValue>> const& _members): std::vector<ASTPointer<EnumValue>> const& _members):
Declaration(_location, _name), m_members(_members) {} Declaration(_location, _name), m_members(_members) {}
@ -344,7 +344,7 @@ private:
class EnumValue: public Declaration class EnumValue: public Declaration
{ {
public: public:
EnumValue(Location const& _location, EnumValue(SourceLocation const& _location,
ASTPointer<ASTString> const& _name): ASTPointer<ASTString> const& _name):
Declaration(_location, _name) {} Declaration(_location, _name) {}
@ -361,7 +361,7 @@ class EnumValue: public Declaration
class ParameterList: public ASTNode class ParameterList: public ASTNode
{ {
public: public:
ParameterList(Location const& _location, ParameterList(SourceLocation const& _location,
std::vector<ASTPointer<VariableDeclaration>> const& _parameters): std::vector<ASTPointer<VariableDeclaration>> const& _parameters):
ASTNode(_location), m_parameters(_parameters) {} ASTNode(_location), m_parameters(_parameters) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -376,7 +376,7 @@ private:
class FunctionDefinition: public Declaration, public VariableScope, public Documented class FunctionDefinition: public Declaration, public VariableScope, public Documented
{ {
public: public:
FunctionDefinition(Location const& _location, ASTPointer<ASTString> const& _name, FunctionDefinition(SourceLocation const& _location, ASTPointer<ASTString> const& _name,
Declaration::Visibility _visibility, bool _isConstructor, Declaration::Visibility _visibility, bool _isConstructor,
ASTPointer<ASTString> const& _documentation, ASTPointer<ASTString> const& _documentation,
ASTPointer<ParameterList> const& _parameters, ASTPointer<ParameterList> const& _parameters,
@ -431,7 +431,7 @@ private:
class VariableDeclaration: public Declaration class VariableDeclaration: public Declaration
{ {
public: public:
VariableDeclaration(Location const& _location, ASTPointer<TypeName> const& _type, VariableDeclaration(SourceLocation const& _location, ASTPointer<TypeName> const& _type,
ASTPointer<ASTString> const& _name, ASTPointer<Expression> _value, ASTPointer<ASTString> const& _name, ASTPointer<Expression> _value,
Visibility _visibility, Visibility _visibility,
bool _isStateVar = false, bool _isIndexed = false): bool _isStateVar = false, bool _isIndexed = false):
@ -476,7 +476,7 @@ private:
class ModifierDefinition: public Declaration, public VariableScope, public Documented class ModifierDefinition: public Declaration, public VariableScope, public Documented
{ {
public: public:
ModifierDefinition(Location const& _location, ModifierDefinition(SourceLocation const& _location,
ASTPointer<ASTString> const& _name, ASTPointer<ASTString> const& _name,
ASTPointer<ASTString> const& _documentation, ASTPointer<ASTString> const& _documentation,
ASTPointer<ParameterList> const& _parameters, ASTPointer<ParameterList> const& _parameters,
@ -506,7 +506,7 @@ private:
class ModifierInvocation: public ASTNode class ModifierInvocation: public ASTNode
{ {
public: public:
ModifierInvocation(Location const& _location, ASTPointer<Identifier> const& _name, ModifierInvocation(SourceLocation const& _location, ASTPointer<Identifier> const& _name,
std::vector<ASTPointer<Expression>> _arguments): std::vector<ASTPointer<Expression>> _arguments):
ASTNode(_location), m_modifierName(_name), m_arguments(_arguments) {} ASTNode(_location), m_modifierName(_name), m_arguments(_arguments) {}
@ -529,7 +529,7 @@ private:
class EventDefinition: public Declaration, public VariableScope, public Documented class EventDefinition: public Declaration, public VariableScope, public Documented
{ {
public: public:
EventDefinition(Location const& _location, EventDefinition(SourceLocation const& _location,
ASTPointer<ASTString> const& _name, ASTPointer<ASTString> const& _name,
ASTPointer<ASTString> const& _documentation, ASTPointer<ASTString> const& _documentation,
ASTPointer<ParameterList> const& _parameters): ASTPointer<ParameterList> const& _parameters):
@ -560,7 +560,7 @@ class MagicVariableDeclaration: public Declaration
{ {
public: public:
MagicVariableDeclaration(ASTString const& _name, std::shared_ptr<Type const> const& _type): MagicVariableDeclaration(ASTString const& _name, std::shared_ptr<Type const> const& _type):
Declaration(Location(), std::make_shared<ASTString>(_name)), m_type(_type) {} Declaration(SourceLocation(), std::make_shared<ASTString>(_name)), m_type(_type) {}
virtual void accept(ASTVisitor&) override { BOOST_THROW_EXCEPTION(InternalCompilerError() virtual void accept(ASTVisitor&) override { BOOST_THROW_EXCEPTION(InternalCompilerError()
<< errinfo_comment("MagicVariableDeclaration used inside real AST.")); } << errinfo_comment("MagicVariableDeclaration used inside real AST.")); }
virtual void accept(ASTConstVisitor&) const override { BOOST_THROW_EXCEPTION(InternalCompilerError() virtual void accept(ASTConstVisitor&) const override { BOOST_THROW_EXCEPTION(InternalCompilerError()
@ -581,7 +581,7 @@ private:
class TypeName: public ASTNode class TypeName: public ASTNode
{ {
public: public:
explicit TypeName(Location const& _location): ASTNode(_location) {} explicit TypeName(SourceLocation const& _location): ASTNode(_location) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -598,7 +598,7 @@ public:
class ElementaryTypeName: public TypeName class ElementaryTypeName: public TypeName
{ {
public: public:
explicit ElementaryTypeName(Location const& _location, Token::Value _type): explicit ElementaryTypeName(SourceLocation const& _location, Token::Value _type):
TypeName(_location), m_type(_type) TypeName(_location), m_type(_type)
{ {
solAssert(Token::isElementaryTypeName(_type), ""); solAssert(Token::isElementaryTypeName(_type), "");
@ -619,7 +619,7 @@ private:
class UserDefinedTypeName: public TypeName class UserDefinedTypeName: public TypeName
{ {
public: public:
UserDefinedTypeName(Location const& _location, ASTPointer<ASTString> const& _name): UserDefinedTypeName(SourceLocation const& _location, ASTPointer<ASTString> const& _name):
TypeName(_location), m_name(_name), m_referencedDeclaration(nullptr) {} TypeName(_location), m_name(_name), m_referencedDeclaration(nullptr) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -641,7 +641,7 @@ private:
class Mapping: public TypeName class Mapping: public TypeName
{ {
public: public:
Mapping(Location const& _location, ASTPointer<ElementaryTypeName> const& _keyType, Mapping(SourceLocation const& _location, ASTPointer<ElementaryTypeName> const& _keyType,
ASTPointer<TypeName> const& _valueType): ASTPointer<TypeName> const& _valueType):
TypeName(_location), m_keyType(_keyType), m_valueType(_valueType) {} TypeName(_location), m_keyType(_keyType), m_valueType(_valueType) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -662,7 +662,7 @@ private:
class ArrayTypeName: public TypeName class ArrayTypeName: public TypeName
{ {
public: public:
ArrayTypeName(Location const& _location, ASTPointer<TypeName> const& _baseType, ArrayTypeName(SourceLocation const& _location, ASTPointer<TypeName> const& _baseType,
ASTPointer<Expression> const& _length): ASTPointer<Expression> const& _length):
TypeName(_location), m_baseType(_baseType), m_length(_length) {} TypeName(_location), m_baseType(_baseType), m_length(_length) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -689,7 +689,7 @@ private:
class Statement: public ASTNode class Statement: public ASTNode
{ {
public: public:
explicit Statement(Location const& _location): ASTNode(_location) {} explicit Statement(SourceLocation const& _location): ASTNode(_location) {}
/// Check all type requirements, throws exception if some requirement is not met. /// Check all type requirements, throws exception if some requirement is not met.
/// This includes checking that operators are applicable to their arguments but also that /// This includes checking that operators are applicable to their arguments but also that
@ -703,7 +703,7 @@ public:
class Block: public Statement class Block: public Statement
{ {
public: public:
Block(Location const& _location, std::vector<ASTPointer<Statement>> const& _statements): Block(SourceLocation const& _location, std::vector<ASTPointer<Statement>> const& _statements):
Statement(_location), m_statements(_statements) {} Statement(_location), m_statements(_statements) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -721,7 +721,7 @@ private:
class PlaceholderStatement: public Statement class PlaceholderStatement: public Statement
{ {
public: public:
PlaceholderStatement(Location const& _location): Statement(_location) {} PlaceholderStatement(SourceLocation const& _location): Statement(_location) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -736,7 +736,7 @@ public:
class IfStatement: public Statement class IfStatement: public Statement
{ {
public: public:
IfStatement(Location const& _location, ASTPointer<Expression> const& _condition, IfStatement(SourceLocation const& _location, ASTPointer<Expression> const& _condition,
ASTPointer<Statement> const& _trueBody, ASTPointer<Statement> const& _falseBody): ASTPointer<Statement> const& _trueBody, ASTPointer<Statement> const& _falseBody):
Statement(_location), Statement(_location),
m_condition(_condition), m_trueBody(_trueBody), m_falseBody(_falseBody) {} m_condition(_condition), m_trueBody(_trueBody), m_falseBody(_falseBody) {}
@ -761,13 +761,13 @@ private:
class BreakableStatement: public Statement class BreakableStatement: public Statement
{ {
public: public:
BreakableStatement(Location const& _location): Statement(_location) {} BreakableStatement(SourceLocation const& _location): Statement(_location) {}
}; };
class WhileStatement: public BreakableStatement class WhileStatement: public BreakableStatement
{ {
public: public:
WhileStatement(Location const& _location, ASTPointer<Expression> const& _condition, WhileStatement(SourceLocation const& _location, ASTPointer<Expression> const& _condition,
ASTPointer<Statement> const& _body): ASTPointer<Statement> const& _body):
BreakableStatement(_location), m_condition(_condition), m_body(_body) {} BreakableStatement(_location), m_condition(_condition), m_body(_body) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -788,7 +788,7 @@ private:
class ForStatement: public BreakableStatement class ForStatement: public BreakableStatement
{ {
public: public:
ForStatement(Location const& _location, ForStatement(SourceLocation const& _location,
ASTPointer<Statement> const& _initExpression, ASTPointer<Statement> const& _initExpression,
ASTPointer<Expression> const& _conditionExpression, ASTPointer<Expression> const& _conditionExpression,
ASTPointer<ExpressionStatement> const& _loopExpression, ASTPointer<ExpressionStatement> const& _loopExpression,
@ -821,7 +821,7 @@ private:
class Continue: public Statement class Continue: public Statement
{ {
public: public:
Continue(Location const& _location): Statement(_location) {} Continue(SourceLocation const& _location): Statement(_location) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
virtual void checkTypeRequirements() override {} virtual void checkTypeRequirements() override {}
@ -830,7 +830,7 @@ public:
class Break: public Statement class Break: public Statement
{ {
public: public:
Break(Location const& _location): Statement(_location) {} Break(SourceLocation const& _location): Statement(_location) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
virtual void checkTypeRequirements() override {} virtual void checkTypeRequirements() override {}
@ -839,7 +839,7 @@ public:
class Return: public Statement class Return: public Statement
{ {
public: public:
Return(Location const& _location, ASTPointer<Expression> _expression): Return(SourceLocation const& _location, ASTPointer<Expression> _expression):
Statement(_location), m_expression(_expression), m_returnParameters(nullptr) {} Statement(_location), m_expression(_expression), m_returnParameters(nullptr) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -864,7 +864,7 @@ private:
class VariableDeclarationStatement: public Statement class VariableDeclarationStatement: public Statement
{ {
public: public:
VariableDeclarationStatement(Location const& _location, ASTPointer<VariableDeclaration> _variable): VariableDeclarationStatement(SourceLocation const& _location, ASTPointer<VariableDeclaration> _variable):
Statement(_location), m_variable(_variable) {} Statement(_location), m_variable(_variable) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -883,7 +883,7 @@ private:
class ExpressionStatement: public Statement class ExpressionStatement: public Statement
{ {
public: public:
ExpressionStatement(Location const& _location, ASTPointer<Expression> _expression): ExpressionStatement(SourceLocation const& _location, ASTPointer<Expression> _expression):
Statement(_location), m_expression(_expression) {} Statement(_location), m_expression(_expression) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -908,7 +908,7 @@ private:
class Expression: public ASTNode class Expression: public ASTNode
{ {
public: public:
Expression(Location const& _location): ASTNode(_location) {} Expression(SourceLocation const& _location): ASTNode(_location) {}
virtual void checkTypeRequirements() = 0; virtual void checkTypeRequirements() = 0;
std::shared_ptr<Type const> const& getType() const { return m_type; } std::shared_ptr<Type const> const& getType() const { return m_type; }
@ -939,7 +939,7 @@ protected:
class Assignment: public Expression class Assignment: public Expression
{ {
public: public:
Assignment(Location const& _location, ASTPointer<Expression> const& _leftHandSide, Assignment(SourceLocation const& _location, ASTPointer<Expression> const& _leftHandSide,
Token::Value _assignmentOperator, ASTPointer<Expression> const& _rightHandSide): Token::Value _assignmentOperator, ASTPointer<Expression> const& _rightHandSide):
Expression(_location), m_leftHandSide(_leftHandSide), Expression(_location), m_leftHandSide(_leftHandSide),
m_assigmentOperator(_assignmentOperator), m_rightHandSide(_rightHandSide) m_assigmentOperator(_assignmentOperator), m_rightHandSide(_rightHandSide)
@ -967,7 +967,7 @@ private:
class UnaryOperation: public Expression class UnaryOperation: public Expression
{ {
public: public:
UnaryOperation(Location const& _location, Token::Value _operator, UnaryOperation(SourceLocation const& _location, Token::Value _operator,
ASTPointer<Expression> const& _subExpression, bool _isPrefix): ASTPointer<Expression> const& _subExpression, bool _isPrefix):
Expression(_location), m_operator(_operator), Expression(_location), m_operator(_operator),
m_subExpression(_subExpression), m_isPrefix(_isPrefix) m_subExpression(_subExpression), m_isPrefix(_isPrefix)
@ -995,7 +995,7 @@ private:
class BinaryOperation: public Expression class BinaryOperation: public Expression
{ {
public: public:
BinaryOperation(Location const& _location, ASTPointer<Expression> const& _left, BinaryOperation(SourceLocation const& _location, ASTPointer<Expression> const& _left,
Token::Value _operator, ASTPointer<Expression> const& _right): Token::Value _operator, ASTPointer<Expression> const& _right):
Expression(_location), m_left(_left), m_operator(_operator), m_right(_right) Expression(_location), m_left(_left), m_operator(_operator), m_right(_right)
{ {
@ -1026,7 +1026,7 @@ private:
class FunctionCall: public Expression class FunctionCall: public Expression
{ {
public: public:
FunctionCall(Location const& _location, ASTPointer<Expression> const& _expression, FunctionCall(SourceLocation const& _location, ASTPointer<Expression> const& _expression,
std::vector<ASTPointer<Expression>> const& _arguments, std::vector<ASTPointer<ASTString>> const& _names): std::vector<ASTPointer<Expression>> const& _arguments, std::vector<ASTPointer<ASTString>> const& _names):
Expression(_location), m_expression(_expression), m_arguments(_arguments), m_names(_names) {} Expression(_location), m_expression(_expression), m_arguments(_arguments), m_names(_names) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -1053,7 +1053,7 @@ private:
class NewExpression: public Expression class NewExpression: public Expression
{ {
public: public:
NewExpression(Location const& _location, ASTPointer<Identifier> const& _contractName): NewExpression(SourceLocation const& _location, ASTPointer<Identifier> const& _contractName):
Expression(_location), m_contractName(_contractName) {} Expression(_location), m_contractName(_contractName) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -1074,7 +1074,7 @@ private:
class MemberAccess: public Expression class MemberAccess: public Expression
{ {
public: public:
MemberAccess(Location const& _location, ASTPointer<Expression> _expression, MemberAccess(SourceLocation const& _location, ASTPointer<Expression> _expression,
ASTPointer<ASTString> const& _memberName): ASTPointer<ASTString> const& _memberName):
Expression(_location), m_expression(_expression), m_memberName(_memberName) {} Expression(_location), m_expression(_expression), m_memberName(_memberName) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -1094,7 +1094,7 @@ private:
class IndexAccess: public Expression class IndexAccess: public Expression
{ {
public: public:
IndexAccess(Location const& _location, ASTPointer<Expression> const& _base, IndexAccess(SourceLocation const& _location, ASTPointer<Expression> const& _base,
ASTPointer<Expression> const& _index): ASTPointer<Expression> const& _index):
Expression(_location), m_base(_base), m_index(_index) {} Expression(_location), m_base(_base), m_index(_index) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
@ -1116,7 +1116,7 @@ private:
class PrimaryExpression: public Expression class PrimaryExpression: public Expression
{ {
public: public:
PrimaryExpression(Location const& _location): Expression(_location) {} PrimaryExpression(SourceLocation const& _location): Expression(_location) {}
}; };
/** /**
@ -1125,7 +1125,7 @@ public:
class Identifier: public PrimaryExpression class Identifier: public PrimaryExpression
{ {
public: public:
Identifier(Location const& _location, ASTPointer<ASTString> const& _name): Identifier(SourceLocation const& _location, ASTPointer<ASTString> const& _name):
PrimaryExpression(_location), m_name(_name) {} PrimaryExpression(_location), m_name(_name) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void accept(ASTConstVisitor& _visitor) const override; virtual void accept(ASTConstVisitor& _visitor) const override;
@ -1160,7 +1160,7 @@ private:
class ElementaryTypeNameExpression: public PrimaryExpression class ElementaryTypeNameExpression: public PrimaryExpression
{ {
public: public:
ElementaryTypeNameExpression(Location const& _location, Token::Value _typeToken): ElementaryTypeNameExpression(SourceLocation const& _location, Token::Value _typeToken):
PrimaryExpression(_location), m_typeToken(_typeToken) PrimaryExpression(_location), m_typeToken(_typeToken)
{ {
solAssert(Token::isElementaryTypeName(_typeToken), ""); solAssert(Token::isElementaryTypeName(_typeToken), "");
@ -1189,7 +1189,7 @@ public:
Finney = Token::SubFinney, Finney = Token::SubFinney,
Ether = Token::SubEther Ether = Token::SubEther
}; };
Literal(Location const& _location, Token::Value _token, Literal(SourceLocation const& _location, Token::Value _token,
ASTPointer<ASTString> const& _value, ASTPointer<ASTString> const& _value,
SubDenomination _sub = SubDenomination::None): SubDenomination _sub = SubDenomination::None):
PrimaryExpression(_location), m_token(_token), m_value(_value), m_subDenomination(_sub) {} PrimaryExpression(_location), m_token(_token), m_value(_value), m_subDenomination(_sub) {}

2
libsolidity/ASTPrinter.cpp

@ -555,7 +555,7 @@ void ASTPrinter::printSourcePart(ASTNode const& _node)
{ {
if (!m_source.empty()) if (!m_source.empty())
{ {
Location const& location(_node.getLocation()); SourceLocation const& location(_node.getLocation());
*m_ostream << getIndentation() << " Source: " *m_ostream << getIndentation() << " Source: "
<< escaped(m_source.substr(location.start, location.end - location.start), false) << endl; << escaped(m_source.substr(location.start, location.end - location.start), false) << endl;
} }

25
libsolidity/Compiler.cpp

@ -74,6 +74,7 @@ void Compiler::initializeContext(ContractDefinition const& _contract,
m_context.setCompiledContracts(_contracts); m_context.setCompiledContracts(_contracts);
m_context.setInheritanceHierarchy(_contract.getLinearizedBaseContracts()); m_context.setInheritanceHierarchy(_contract.getLinearizedBaseContracts());
registerStateVariables(_contract); registerStateVariables(_contract);
m_context.resetVisitedNodes(&_contract);
} }
void Compiler::packIntoContractCreator(ContractDefinition const& _contract, CompilerContext const& _runtimeContext) void Compiler::packIntoContractCreator(ContractDefinition const& _contract, CompilerContext const& _runtimeContext)
@ -128,6 +129,7 @@ void Compiler::packIntoContractCreator(ContractDefinition const& _contract, Comp
void Compiler::appendBaseConstructorCall(FunctionDefinition const& _constructor, void Compiler::appendBaseConstructorCall(FunctionDefinition const& _constructor,
vector<ASTPointer<Expression>> const& _arguments) vector<ASTPointer<Expression>> const& _arguments)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_constructor);
FunctionType constructorType(_constructor); FunctionType constructorType(_constructor);
eth::AssemblyItem returnLabel = m_context.pushNewTag(); eth::AssemblyItem returnLabel = m_context.pushNewTag();
for (unsigned i = 0; i < _arguments.size(); ++i) for (unsigned i = 0; i < _arguments.size(); ++i)
@ -138,6 +140,7 @@ void Compiler::appendBaseConstructorCall(FunctionDefinition const& _constructor,
void Compiler::appendConstructorCall(FunctionDefinition const& _constructor) void Compiler::appendConstructorCall(FunctionDefinition const& _constructor)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_constructor);
eth::AssemblyItem returnTag = m_context.pushNewTag(); eth::AssemblyItem returnTag = m_context.pushNewTag();
// copy constructor arguments from code to memory and then to stack, they are supplied after the actual program // copy constructor arguments from code to memory and then to stack, they are supplied after the actual program
unsigned argumentSize = 0; unsigned argumentSize = 0;
@ -273,6 +276,7 @@ void Compiler::initializeStateVariables(ContractDefinition const& _contract)
bool Compiler::visit(VariableDeclaration const& _variableDeclaration) bool Compiler::visit(VariableDeclaration const& _variableDeclaration)
{ {
solAssert(_variableDeclaration.isStateVariable(), "Compiler visit to non-state variable declaration."); solAssert(_variableDeclaration.isStateVariable(), "Compiler visit to non-state variable declaration.");
CompilerContext::LocationSetter locationSetter(m_context, &_variableDeclaration);
m_context.startFunction(_variableDeclaration); m_context.startFunction(_variableDeclaration);
m_breakTags.clear(); m_breakTags.clear();
@ -286,6 +290,7 @@ bool Compiler::visit(VariableDeclaration const& _variableDeclaration)
bool Compiler::visit(FunctionDefinition const& _function) bool Compiler::visit(FunctionDefinition const& _function)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_function);
//@todo to simplify this, the calling convention could by changed such that //@todo to simplify this, the calling convention could by changed such that
// caller puts: [retarg0] ... [retargm] [return address] [arg0] ... [argn] // caller puts: [retarg0] ... [retargm] [return address] [arg0] ... [argn]
// although note that this reduces the size of the visible stack // although note that this reduces the size of the visible stack
@ -355,7 +360,7 @@ bool Compiler::visit(FunctionDefinition const& _function)
bool Compiler::visit(IfStatement const& _ifStatement) bool Compiler::visit(IfStatement const& _ifStatement)
{ {
StackHeightChecker checker(m_context); StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, &_ifStatement);
compileExpression(_ifStatement.getCondition()); compileExpression(_ifStatement.getCondition());
eth::AssemblyItem trueTag = m_context.appendConditionalJump(); eth::AssemblyItem trueTag = m_context.appendConditionalJump();
if (_ifStatement.getFalseStatement()) if (_ifStatement.getFalseStatement())
@ -372,7 +377,7 @@ bool Compiler::visit(IfStatement const& _ifStatement)
bool Compiler::visit(WhileStatement const& _whileStatement) bool Compiler::visit(WhileStatement const& _whileStatement)
{ {
StackHeightChecker checker(m_context); StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, &_whileStatement);
eth::AssemblyItem loopStart = m_context.newTag(); eth::AssemblyItem loopStart = m_context.newTag();
eth::AssemblyItem loopEnd = m_context.newTag(); eth::AssemblyItem loopEnd = m_context.newTag();
m_continueTags.push_back(loopStart); m_continueTags.push_back(loopStart);
@ -398,7 +403,7 @@ bool Compiler::visit(WhileStatement const& _whileStatement)
bool Compiler::visit(ForStatement const& _forStatement) bool Compiler::visit(ForStatement const& _forStatement)
{ {
StackHeightChecker checker(m_context); StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, &_forStatement);
eth::AssemblyItem loopStart = m_context.newTag(); eth::AssemblyItem loopStart = m_context.newTag();
eth::AssemblyItem loopEnd = m_context.newTag(); eth::AssemblyItem loopEnd = m_context.newTag();
m_continueTags.push_back(loopStart); m_continueTags.push_back(loopStart);
@ -433,15 +438,17 @@ bool Compiler::visit(ForStatement const& _forStatement)
return false; return false;
} }
bool Compiler::visit(Continue const&) bool Compiler::visit(Continue const& _continueStatement)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_continueStatement);
if (!m_continueTags.empty()) if (!m_continueTags.empty())
m_context.appendJumpTo(m_continueTags.back()); m_context.appendJumpTo(m_continueTags.back());
return false; return false;
} }
bool Compiler::visit(Break const&) bool Compiler::visit(Break const& _breakStatement)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_breakStatement);
if (!m_breakTags.empty()) if (!m_breakTags.empty())
m_context.appendJumpTo(m_breakTags.back()); m_context.appendJumpTo(m_breakTags.back());
return false; return false;
@ -449,6 +456,7 @@ bool Compiler::visit(Break const&)
bool Compiler::visit(Return const& _return) bool Compiler::visit(Return const& _return)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_return);
//@todo modifications are needed to make this work with functions returning multiple values //@todo modifications are needed to make this work with functions returning multiple values
if (Expression const* expression = _return.getExpression()) if (Expression const* expression = _return.getExpression())
{ {
@ -467,6 +475,7 @@ bool Compiler::visit(Return const& _return)
bool Compiler::visit(VariableDeclarationStatement const& _variableDeclarationStatement) bool Compiler::visit(VariableDeclarationStatement const& _variableDeclarationStatement)
{ {
StackHeightChecker checker(m_context); StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, &_variableDeclarationStatement);
if (Expression const* expression = _variableDeclarationStatement.getExpression()) if (Expression const* expression = _variableDeclarationStatement.getExpression())
{ {
compileExpression(*expression, _variableDeclarationStatement.getDeclaration().getType()); compileExpression(*expression, _variableDeclarationStatement.getDeclaration().getType());
@ -479,6 +488,7 @@ bool Compiler::visit(VariableDeclarationStatement const& _variableDeclarationSta
bool Compiler::visit(ExpressionStatement const& _expressionStatement) bool Compiler::visit(ExpressionStatement const& _expressionStatement)
{ {
StackHeightChecker checker(m_context); StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, &_expressionStatement);
Expression const& expression = _expressionStatement.getExpression(); Expression const& expression = _expressionStatement.getExpression();
compileExpression(expression); compileExpression(expression);
CompilerUtils(m_context).popStackElement(*expression.getType()); CompilerUtils(m_context).popStackElement(*expression.getType());
@ -486,9 +496,10 @@ bool Compiler::visit(ExpressionStatement const& _expressionStatement)
return false; return false;
} }
bool Compiler::visit(PlaceholderStatement const&) bool Compiler::visit(PlaceholderStatement const& _placeholderStatement)
{ {
StackHeightChecker checker(m_context); StackHeightChecker checker(m_context);
CompilerContext::LocationSetter locationSetter(m_context, &_placeholderStatement);
++m_modifierDepth; ++m_modifierDepth;
appendModifierOrFunctionCode(); appendModifierOrFunctionCode();
--m_modifierDepth; --m_modifierDepth;
@ -504,8 +515,8 @@ void Compiler::appendModifierOrFunctionCode()
else else
{ {
ASTPointer<ModifierInvocation> const& modifierInvocation = m_currentFunction->getModifiers()[m_modifierDepth]; ASTPointer<ModifierInvocation> const& modifierInvocation = m_currentFunction->getModifiers()[m_modifierDepth];
ModifierDefinition const& modifier = m_context.getFunctionModifier(modifierInvocation->getName()->getName()); ModifierDefinition const& modifier = m_context.getFunctionModifier(modifierInvocation->getName()->getName());
CompilerContext::LocationSetter locationSetter(m_context, &modifier);
solAssert(modifier.getParameters().size() == modifierInvocation->getArguments().size(), ""); solAssert(modifier.getParameters().size() == modifierInvocation->getArguments().size(), "");
for (unsigned i = 0; i < modifier.getParameters().size(); ++i) for (unsigned i = 0; i < modifier.getParameters().size(); ++i)
{ {

6
libsolidity/Compiler.h

@ -26,6 +26,7 @@
#include <functional> #include <functional>
#include <libsolidity/ASTVisitor.h> #include <libsolidity/ASTVisitor.h>
#include <libsolidity/CompilerContext.h> #include <libsolidity/CompilerContext.h>
#include <libevmcore/Assembly.h>
namespace dev { namespace dev {
namespace solidity { namespace solidity {
@ -42,6 +43,11 @@ public:
bytes getRuntimeBytecode() { return m_runtimeContext.getAssembledBytecode(m_optimize);} bytes getRuntimeBytecode() { return m_runtimeContext.getAssembledBytecode(m_optimize);}
void streamAssembly(std::ostream& _stream) const { m_context.streamAssembly(_stream); } void streamAssembly(std::ostream& _stream) const { m_context.streamAssembly(_stream); }
/// @returns Assembly items of the normal compiler context
eth::AssemblyItems const& getAssemblyItems() const { return m_context.getAssembly().getItems(); }
/// @returns Assembly items of the runtime compiler context
eth::AssemblyItems const& getRuntimeAssemblyItems() const { return m_runtimeContext.getAssembly().getItems(); }
private: private:
/// Registers the non-function objects inside the contract with the context. /// Registers the non-function objects inside the contract with the context.
void initializeContext(ContractDefinition const& _contract, void initializeContext(ContractDefinition const& _contract,

37
libsolidity/CompilerContext.cpp

@ -65,8 +65,8 @@ void CompilerContext::addVariable(VariableDeclaration const& _declaration,
void CompilerContext::addAndInitializeVariable(VariableDeclaration const& _declaration) void CompilerContext::addAndInitializeVariable(VariableDeclaration const& _declaration)
{ {
LocationSetter locationSetter(*this, &_declaration);
addVariable(_declaration); addVariable(_declaration);
int const size = _declaration.getType()->getSizeOnStack(); int const size = _declaration.getType()->getSizeOnStack();
for (int i = 0; i < size; ++i) for (int i = 0; i < size; ++i)
*this << u256(0); *this << u256(0);
@ -166,5 +166,40 @@ u256 CompilerContext::getStorageLocationOfVariable(const Declaration& _declarati
return it->second; return it->second;
} }
void CompilerContext::resetVisitedNodes(ASTNode const* _node)
{
stack<ASTNode const*> newStack;
newStack.push(_node);
std::swap(m_visitedNodes, newStack);
}
CompilerContext& CompilerContext::operator<<(eth::AssemblyItem const& _item)
{
solAssert(!m_visitedNodes.empty(), "No node on the visited stack");
m_asm.append(_item, m_visitedNodes.top()->getLocation());
return *this;
}
CompilerContext& CompilerContext::operator<<(eth::Instruction _instruction)
{
solAssert(!m_visitedNodes.empty(), "No node on the visited stack");
m_asm.append(_instruction, m_visitedNodes.top()->getLocation());
return *this;
}
CompilerContext& CompilerContext::operator<<(u256 const& _value)
{
solAssert(!m_visitedNodes.empty(), "No node on the visited stack");
m_asm.append(_value, m_visitedNodes.top()->getLocation());
return *this;
}
CompilerContext& CompilerContext::operator<<(bytes const& _data)
{
solAssert(!m_visitedNodes.empty(), "No node on the visited stack");
m_asm.append(_data, m_visitedNodes.top()->getLocation());
return *this;
}
} }
} }

28
libsolidity/CompilerContext.h

@ -23,6 +23,7 @@
#pragma once #pragma once
#include <ostream> #include <ostream>
#include <stack>
#include <libevmcore/Instruction.h> #include <libevmcore/Instruction.h>
#include <libevmcore/Assembly.h> #include <libevmcore/Assembly.h>
#include <libsolidity/ASTForward.h> #include <libsolidity/ASTForward.h>
@ -99,19 +100,34 @@ public:
void appendProgramSize() { return m_asm.appendProgramSize(); } void appendProgramSize() { return m_asm.appendProgramSize(); }
/// Adds data to the data section, pushes a reference to the stack /// Adds data to the data section, pushes a reference to the stack
eth::AssemblyItem appendData(bytes const& _data) { return m_asm.append(_data); } eth::AssemblyItem appendData(bytes const& _data) { return m_asm.append(_data); }
/// Resets the stack of visited nodes with a new stack having only @c _node
void resetVisitedNodes(ASTNode const* _node);
/// Pops the stack of visited nodes
void popVisitedNodes() { m_visitedNodes.pop(); }
/// Pushes an ASTNode to the stack of visited nodes
void pushVisitedNodes(ASTNode const* _node) { m_visitedNodes.push(_node); }
/// Append elements to the current instruction list and adjust @a m_stackOffset. /// Append elements to the current instruction list and adjust @a m_stackOffset.
CompilerContext& operator<<(eth::AssemblyItem const& _item) { m_asm.append(_item); return *this; } CompilerContext& operator<<(eth::AssemblyItem const& _item);
CompilerContext& operator<<(eth::Instruction _instruction) { m_asm.append(_instruction); return *this; } CompilerContext& operator<<(eth::Instruction _instruction);
CompilerContext& operator<<(u256 const& _value) { m_asm.append(_value); return *this; } CompilerContext& operator<<(u256 const& _value);
CompilerContext& operator<<(bytes const& _data) { m_asm.append(_data); return *this; } CompilerContext& operator<<(bytes const& _data);
eth::Assembly const& getAssembly() const { return m_asm; } eth::Assembly const& getAssembly() const { return m_asm; }
void streamAssembly(std::ostream& _stream) const { _stream << m_asm; } void streamAssembly(std::ostream& _stream) const { _stream << m_asm; }
bytes getAssembledBytecode(bool _optimize = false) { return m_asm.optimise(_optimize).assemble(); } bytes getAssembledBytecode(bool _optimize = false) { return m_asm.optimise(_optimize).assemble(); }
private: /**
* Helper class to pop the visited nodes stack when a scope closes
*/
class LocationSetter: public ScopeGuard
{
public:
LocationSetter(CompilerContext& _compilerContext, ASTNode const* _node):
ScopeGuard(std::bind(&CompilerContext::popVisitedNodes, _compilerContext)) { _compilerContext.pushVisitedNodes(_node); }
};
eth::Assembly m_asm; eth::Assembly m_asm;
private:
/// Magic global variables like msg, tx or this, distinguished by type. /// Magic global variables like msg, tx or this, distinguished by type.
std::set<Declaration const*> m_magicGlobals; std::set<Declaration const*> m_magicGlobals;
@ -129,6 +145,8 @@ private:
std::set<Declaration const*> m_functionsWithCode; std::set<Declaration const*> m_functionsWithCode;
/// List of current inheritance hierarchy from derived to base. /// List of current inheritance hierarchy from derived to base.
std::vector<ContractDefinition const*> m_inheritanceHierarchy; std::vector<ContractDefinition const*> m_inheritanceHierarchy;
/// Stack of current visited AST nodes, used for location attachment
std::stack<ASTNode const*> m_visitedNodes;
}; };
} }

4
libsolidity/Exceptions.h

@ -24,7 +24,7 @@
#include <string> #include <string>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include <libsolidity/BaseTypes.h> #include <libevmcore/SourceLocation.h>
namespace dev namespace dev
{ {
@ -38,7 +38,7 @@ struct CompilerError: virtual Exception {};
struct InternalCompilerError: virtual Exception {}; struct InternalCompilerError: virtual Exception {};
struct DocstringParsingError: virtual Exception {}; struct DocstringParsingError: virtual Exception {};
using errinfo_sourceLocation = boost::error_info<struct tag_sourceLocation, Location>; using errinfo_sourceLocation = boost::error_info<struct tag_sourceLocation, SourceLocation>;
} }
} }

20
libsolidity/ExpressionCompiler.cpp

@ -68,6 +68,7 @@ void ExpressionCompiler::appendStateVariableInitialization(CompilerContext& _con
void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration const& _varDecl) void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration const& _varDecl)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_varDecl);
LValue var = LValue(m_context); LValue var = LValue(m_context);
var.fromDeclaration(_varDecl, _varDecl.getValue()->getLocation()); var.fromDeclaration(_varDecl, _varDecl.getValue()->getLocation());
var.storeValue(*_varDecl.getType(), _varDecl.getLocation()); var.storeValue(*_varDecl.getType(), _varDecl.getLocation());
@ -75,6 +76,7 @@ void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration c
bool ExpressionCompiler::visit(Assignment const& _assignment) bool ExpressionCompiler::visit(Assignment const& _assignment)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_assignment);
_assignment.getRightHandSide().accept(*this); _assignment.getRightHandSide().accept(*this);
if (_assignment.getType()->isValueType()) if (_assignment.getType()->isValueType())
appendTypeConversion(*_assignment.getRightHandSide().getType(), *_assignment.getType()); appendTypeConversion(*_assignment.getRightHandSide().getType(), *_assignment.getType());
@ -99,6 +101,7 @@ bool ExpressionCompiler::visit(Assignment const& _assignment)
bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation) bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_unaryOperation);
//@todo type checking and creating code for an operator should be in the same place: //@todo type checking and creating code for an operator should be in the same place:
// the operator should know how to convert itself and to which types it applies, so // the operator should know how to convert itself and to which types it applies, so
// put this code together with "Type::acceptsBinary/UnaryOperator" into a class that // put this code together with "Type::acceptsBinary/UnaryOperator" into a class that
@ -163,6 +166,7 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation) bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_binaryOperation);
Expression const& leftExpression = _binaryOperation.getLeftExpression(); Expression const& leftExpression = _binaryOperation.getLeftExpression();
Expression const& rightExpression = _binaryOperation.getRightExpression(); Expression const& rightExpression = _binaryOperation.getRightExpression();
Type const& commonType = _binaryOperation.getCommonType(); Type const& commonType = _binaryOperation.getCommonType();
@ -209,6 +213,7 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
bool ExpressionCompiler::visit(FunctionCall const& _functionCall) bool ExpressionCompiler::visit(FunctionCall const& _functionCall)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_functionCall);
using Location = FunctionType::Location; using Location = FunctionType::Location;
if (_functionCall.isTypeConversion()) if (_functionCall.isTypeConversion())
{ {
@ -426,6 +431,7 @@ bool ExpressionCompiler::visit(NewExpression const&)
void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess) void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_memberAccess);
ASTString const& member = _memberAccess.getMemberName(); ASTString const& member = _memberAccess.getMemberName();
switch (_memberAccess.getExpression().getType()->getCategory()) switch (_memberAccess.getExpression().getType()->getCategory())
{ {
@ -562,6 +568,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
bool ExpressionCompiler::visit(IndexAccess const& _indexAccess) bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_indexAccess);
_indexAccess.getBaseExpression().accept(*this); _indexAccess.getBaseExpression().accept(*this);
Type const& baseType = *_indexAccess.getBaseExpression().getType(); Type const& baseType = *_indexAccess.getBaseExpression().getType();
@ -993,6 +1000,7 @@ void ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType,
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl) void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl)
{ {
CompilerContext::LocationSetter locationSetter(m_context, &_varDecl);
FunctionType accessorType(_varDecl); FunctionType accessorType(_varDecl);
unsigned length = 0; unsigned length = 0;
@ -1029,7 +1037,7 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const&
<< structType->getStorageOffsetOfMember(names[i]) << structType->getStorageOffsetOfMember(names[i])
<< eth::Instruction::ADD; << eth::Instruction::ADD;
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, types[i]); m_currentLValue = LValue(m_context, LValue::LValueType::Storage, types[i]);
m_currentLValue.retrieveValue(Location(), true); m_currentLValue.retrieveValue(SourceLocation(), true);
solAssert(types[i]->getSizeOnStack() == 1, "Returning struct elements with stack size != 1 not yet implemented."); solAssert(types[i]->getSizeOnStack() == 1, "Returning struct elements with stack size != 1 not yet implemented.");
m_context << eth::Instruction::SWAP1; m_context << eth::Instruction::SWAP1;
retSizeOnStack += types[i]->getSizeOnStack(); retSizeOnStack += types[i]->getSizeOnStack();
@ -1041,7 +1049,7 @@ void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const&
// simple value // simple value
solAssert(accessorType.getReturnParameterTypes().size() == 1, ""); solAssert(accessorType.getReturnParameterTypes().size() == 1, "");
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, returnType); m_currentLValue = LValue(m_context, LValue::LValueType::Storage, returnType);
m_currentLValue.retrieveValue(Location(), true); m_currentLValue.retrieveValue(SourceLocation(), true);
retSizeOnStack = returnType->getSizeOnStack(); retSizeOnStack = returnType->getSizeOnStack();
} }
solAssert(retSizeOnStack <= 15, "Stack too deep."); solAssert(retSizeOnStack <= 15, "Stack too deep.");
@ -1062,7 +1070,7 @@ ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType
m_size = unsigned(m_dataType->getSizeOnStack()); m_size = unsigned(m_dataType->getSizeOnStack());
} }
void ExpressionCompiler::LValue::fromDeclaration(Declaration const& _declaration, Location const& _location) void ExpressionCompiler::LValue::fromDeclaration(Declaration const& _declaration, SourceLocation const& _location)
{ {
if (m_context->isLocalVariable(&_declaration)) if (m_context->isLocalVariable(&_declaration))
{ {
@ -1085,7 +1093,7 @@ void ExpressionCompiler::LValue::fromDeclaration(Declaration const& _declaration
<< errinfo_comment("Identifier type not supported or identifier not found.")); << errinfo_comment("Identifier type not supported or identifier not found."));
} }
void ExpressionCompiler::LValue::retrieveValue(Location const& _location, bool _remove) const void ExpressionCompiler::LValue::retrieveValue(SourceLocation const& _location, bool _remove) const
{ {
switch (m_type) switch (m_type)
{ {
@ -1134,7 +1142,7 @@ void ExpressionCompiler::LValue::retrieveValueFromStorage(bool _remove) const
} }
} }
void ExpressionCompiler::LValue::storeValue(Type const& _sourceType, Location const& _location, bool _move) const void ExpressionCompiler::LValue::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
{ {
switch (m_type) switch (m_type)
{ {
@ -1237,7 +1245,7 @@ void ExpressionCompiler::LValue::storeValue(Type const& _sourceType, Location co
} }
} }
void ExpressionCompiler::LValue::setToZero(Location const& _location) const void ExpressionCompiler::LValue::setToZero(SourceLocation const& _location) const
{ {
switch (m_type) switch (m_type)
{ {

10
libsolidity/ExpressionCompiler.h

@ -25,7 +25,7 @@
#include <memory> #include <memory>
#include <boost/noncopyable.hpp> #include <boost/noncopyable.hpp>
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libsolidity/BaseTypes.h> #include <libevmcore/SourceLocation.h>
#include <libsolidity/ASTVisitor.h> #include <libsolidity/ASTVisitor.h>
namespace dev { namespace dev {
@ -133,7 +133,7 @@ private:
/// Set type according to the declaration and retrieve the reference. /// Set type according to the declaration and retrieve the reference.
/// @a _location is the current location /// @a _location is the current location
void fromDeclaration(Declaration const& _declaration, Location const& _location); void fromDeclaration(Declaration const& _declaration, SourceLocation const& _location);
void reset() { m_type = LValueType::None; m_dataType.reset(); m_baseStackOffset = 0; m_size = 0; } void reset() { m_type = LValueType::None; m_dataType.reset(); m_baseStackOffset = 0; m_size = 0; }
@ -148,15 +148,15 @@ private:
/// Copies the value of the current lvalue to the top of the stack and, if @a _remove is true, /// Copies the value of the current lvalue to the top of the stack and, if @a _remove is true,
/// also removes the reference from the stack (note that is does not reset the type to @a NONE). /// also removes the reference from the stack (note that is does not reset the type to @a NONE).
/// @a _location source location of the current expression, used for error reporting. /// @a _location source location of the current expression, used for error reporting.
void retrieveValue(Location const& _location, bool _remove = false) const; void retrieveValue(SourceLocation const& _location, bool _remove = false) const;
/// Moves a value from the stack to the lvalue. Removes the value if @a _move is true. /// Moves a value from the stack to the lvalue. Removes the value if @a _move is true.
/// @a _location is the source location of the expression that caused this operation. /// @a _location is the source location of the expression that caused this operation.
/// Stack pre: value [lvalue_ref] /// Stack pre: value [lvalue_ref]
/// Stack post if !_move: value_of(lvalue_ref) /// Stack post if !_move: value_of(lvalue_ref)
void storeValue(Type const& _sourceType, Location const& _location = Location(), bool _move = false) const; void storeValue(Type const& _sourceType, SourceLocation const& _location = SourceLocation(), bool _move = false) const;
/// Stores zero in the lvalue. /// Stores zero in the lvalue.
/// @a _location is the source location of the requested operation /// @a _location is the source location of the requested operation
void setToZero(Location const& _location = Location()) const; void setToZero(SourceLocation const& _location = SourceLocation()) const;
/// Convenience function to convert the stored reference to a value and reset type to NONE if /// Convenience function to convert the stored reference to a value and reset type to NONE if
/// the reference was not requested by @a _expression. /// the reference was not requested by @a _expression.
void retrieveValueIfLValueNotRequested(Expression const& _expression); void retrieveValueIfLValueNotRequested(Expression const& _expression);

16
libsolidity/Parser.cpp

@ -22,7 +22,7 @@
#include <vector> #include <vector>
#include <libdevcore/Log.h> #include <libdevcore/Log.h>
#include <libsolidity/BaseTypes.h> #include <libevmcore/SourceLocation.h>
#include <libsolidity/Parser.h> #include <libsolidity/Parser.h>
#include <libsolidity/Scanner.h> #include <libsolidity/Scanner.h>
#include <libsolidity/Exceptions.h> #include <libsolidity/Exceptions.h>
@ -45,7 +45,7 @@ public:
m_parser(_parser), m_location(_childNode->getLocation()) {} m_parser(_parser), m_location(_childNode->getLocation()) {}
void markEndPosition() { m_location.end = m_parser.getEndPosition(); } void markEndPosition() { m_location.end = m_parser.getEndPosition(); }
void setLocation(Location const& _location) { m_location = _location; } void setLocation(SourceLocation const& _location) { m_location = _location; }
void setLocationEmpty() { m_location.end = m_location.start; } void setLocationEmpty() { m_location.end = m_location.start; }
/// Set the end position to the one of the given node. /// Set the end position to the one of the given node.
void setEndPositionFromNode(ASTPointer<ASTNode> const& _node) { m_location.end = _node->getLocation().end; } void setEndPositionFromNode(ASTPointer<ASTNode> const& _node) { m_location.end = _node->getLocation().end; }
@ -60,7 +60,7 @@ public:
private: private:
Parser const& m_parser; Parser const& m_parser;
Location m_location; SourceLocation m_location;
}; };
ASTPointer<SourceUnit> Parser::parse(shared_ptr<Scanner> const& _scanner) ASTPointer<SourceUnit> Parser::parse(shared_ptr<Scanner> const& _scanner)
@ -646,9 +646,9 @@ ASTPointer<Statement> Parser::parseSimpleStatement()
primary = ASTNodeFactory(*this).createNode<ElementaryTypeNameExpression>(m_scanner->getCurrentToken()); primary = ASTNodeFactory(*this).createNode<ElementaryTypeNameExpression>(m_scanner->getCurrentToken());
m_scanner->next(); m_scanner->next();
} }
vector<pair<ASTPointer<Expression>, Location>> indices; vector<pair<ASTPointer<Expression>, SourceLocation>> indices;
solAssert(m_scanner->getCurrentToken() == Token::LBrack, ""); solAssert(m_scanner->getCurrentToken() == Token::LBrack, "");
Location indexLocation = primary->getLocation(); SourceLocation indexLocation = primary->getLocation();
do do
{ {
expectToken(Token::LBrack); expectToken(Token::LBrack);
@ -913,7 +913,7 @@ Parser::LookAheadInfo Parser::peekStatementType() const
} }
ASTPointer<TypeName> Parser::typeNameIndexAccessStructure( ASTPointer<TypeName> Parser::typeNameIndexAccessStructure(
ASTPointer<PrimaryExpression> const& _primary, vector<pair<ASTPointer<Expression>, Location>> const& _indices) ASTPointer<PrimaryExpression> const& _primary, vector<pair<ASTPointer<Expression>, SourceLocation>> const& _indices)
{ {
ASTNodeFactory nodeFactory(*this, _primary); ASTNodeFactory nodeFactory(*this, _primary);
ASTPointer<TypeName> type; ASTPointer<TypeName> type;
@ -932,7 +932,7 @@ ASTPointer<TypeName> Parser::typeNameIndexAccessStructure(
} }
ASTPointer<Expression> Parser::expressionFromIndexAccessStructure( ASTPointer<Expression> Parser::expressionFromIndexAccessStructure(
ASTPointer<PrimaryExpression> const& _primary, vector<pair<ASTPointer<Expression>, Location>> const& _indices) ASTPointer<PrimaryExpression> const& _primary, vector<pair<ASTPointer<Expression>, SourceLocation>> const& _indices)
{ {
ASTNodeFactory nodeFactory(*this, _primary); ASTNodeFactory nodeFactory(*this, _primary);
ASTPointer<Expression> expression(_primary); ASTPointer<Expression> expression(_primary);
@ -983,7 +983,7 @@ ASTPointer<ParameterList> Parser::createEmptyParameterList()
ParserError Parser::createParserError(string const& _description) const ParserError Parser::createParserError(string const& _description) const
{ {
return ParserError() << errinfo_sourceLocation(Location(getPosition(), getPosition(), getSourceName())) return ParserError() << errinfo_sourceLocation(SourceLocation(getPosition(), getPosition(), getSourceName()))
<< errinfo_comment(_description); << errinfo_comment(_description);
} }

4
libsolidity/Parser.h

@ -114,11 +114,11 @@ private:
/// Returns a typename parsed in look-ahead fashion from something like "a[8][2**70]". /// Returns a typename parsed in look-ahead fashion from something like "a[8][2**70]".
ASTPointer<TypeName> typeNameIndexAccessStructure( ASTPointer<TypeName> typeNameIndexAccessStructure(
ASTPointer<PrimaryExpression> const& _primary, ASTPointer<PrimaryExpression> const& _primary,
std::vector<std::pair<ASTPointer<Expression>, Location>> const& _indices); std::vector<std::pair<ASTPointer<Expression>, SourceLocation>> const& _indices);
/// Returns an expression parsed in look-ahead fashion from something like "a[8][2**70]". /// Returns an expression parsed in look-ahead fashion from something like "a[8][2**70]".
ASTPointer<Expression> expressionFromIndexAccessStructure( ASTPointer<Expression> expressionFromIndexAccessStructure(
ASTPointer<PrimaryExpression> const& _primary, ASTPointer<PrimaryExpression> const& _primary,
std::vector<std::pair<ASTPointer<Expression>, Location>> const& _indices); std::vector<std::pair<ASTPointer<Expression>, SourceLocation>> const& _indices);
/// If current token value is not _value, throw exception otherwise advance token. /// If current token value is not _value, throw exception otherwise advance token.
void expectToken(Token::Value _value); void expectToken(Token::Value _value);
Token::Value expectAssignmentOperator(); Token::Value expectAssignmentOperator();

10
libsolidity/Scanner.h

@ -55,7 +55,7 @@
#include <libdevcore/Common.h> #include <libdevcore/Common.h>
#include <libdevcore/Log.h> #include <libdevcore/Log.h>
#include <libdevcore/CommonData.h> #include <libdevcore/CommonData.h>
#include <libsolidity/BaseTypes.h> #include <libevmcore/SourceLocation.h>
#include <libsolidity/Token.h> #include <libsolidity/Token.h>
namespace dev namespace dev
@ -120,14 +120,14 @@ public:
return m_currentToken.token; return m_currentToken.token;
} }
Location getCurrentLocation() const { return m_currentToken.location; } SourceLocation getCurrentLocation() const { return m_currentToken.location; }
std::string const& getCurrentLiteral() const { return m_currentToken.literal; } std::string const& getCurrentLiteral() const { return m_currentToken.literal; }
///@} ///@}
///@{ ///@{
///@name Information about the current comment token ///@name Information about the current comment token
Location getCurrentCommentLocation() const { return m_skippedComment.location; } SourceLocation getCurrentCommentLocation() const { return m_skippedComment.location; }
std::string const& getCurrentCommentLiteral() const { return m_skippedComment.literal; } std::string const& getCurrentCommentLiteral() const { return m_skippedComment.literal; }
/// Called by the parser during FunctionDefinition parsing to clear the current comment /// Called by the parser during FunctionDefinition parsing to clear the current comment
void clearCurrentCommentLiteral() { m_skippedComment.literal.clear(); } void clearCurrentCommentLiteral() { m_skippedComment.literal.clear(); }
@ -139,7 +139,7 @@ public:
/// Returns the next token without advancing input. /// Returns the next token without advancing input.
Token::Value peekNextToken() const { return m_nextToken.token; } Token::Value peekNextToken() const { return m_nextToken.token; }
Location peekLocation() const { return m_nextToken.location; } SourceLocation peekLocation() const { return m_nextToken.location; }
std::string const& peekLiteral() const { return m_nextToken.literal; } std::string const& peekLiteral() const { return m_nextToken.literal; }
///@} ///@}
@ -158,7 +158,7 @@ private:
struct TokenDesc struct TokenDesc
{ {
Token::Value token; Token::Value token;
Location location; SourceLocation location;
std::string literal; std::string literal;
}; };

4
libsolidity/SourceReferenceFormatter.cpp

@ -33,7 +33,7 @@ namespace solidity
{ {
void SourceReferenceFormatter::printSourceLocation(ostream& _stream, void SourceReferenceFormatter::printSourceLocation(ostream& _stream,
Location const& _location, SourceLocation const& _location,
Scanner const& _scanner) Scanner const& _scanner)
{ {
int startLine; int startLine;
@ -63,7 +63,7 @@ void SourceReferenceFormatter::printExceptionInformation(ostream& _stream,
string const& _name, string const& _name,
CompilerStack const& _compiler) CompilerStack const& _compiler)
{ {
Location const* location = boost::get_error_info<errinfo_sourceLocation>(_exception); SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
Scanner const* scanner; Scanner const* scanner;
if (location) if (location)

4
libsolidity/SourceReferenceFormatter.h

@ -23,7 +23,7 @@
#pragma once #pragma once
#include <ostream> #include <ostream>
#include <libsolidity/BaseTypes.h> #include <libevmcore/SourceLocation.h>
namespace dev namespace dev
{ {
@ -39,7 +39,7 @@ class CompilerStack; // forward
struct SourceReferenceFormatter struct SourceReferenceFormatter
{ {
public: public:
static void printSourceLocation(std::ostream& _stream, Location const& _location, Scanner const& _scanner); static void printSourceLocation(std::ostream& _stream, SourceLocation const& _location, Scanner const& _scanner);
static void printExceptionInformation(std::ostream& _stream, Exception const& _exception, static void printExceptionInformation(std::ostream& _stream, Exception const& _exception,
std::string const& _name, CompilerStack const& _compiler); std::string const& _name, CompilerStack const& _compiler);
}; };

4
mix/CodeHighlighter.cpp

@ -64,7 +64,7 @@ namespace
}; };
} }
CodeHighlighter::FormatRange::FormatRange(CodeHighlighterSettings::Token _t, dev::solidity::Location const& _location): CodeHighlighter::FormatRange::FormatRange(CodeHighlighterSettings::Token _t, dev::SourceLocation const& _location):
token(_t), start(_location.start), length(_location.end - _location.start) token(_t), start(_location.start), length(_location.end - _location.start)
{} {}
@ -101,7 +101,7 @@ void CodeHighlighter::processAST(dev::solidity::ASTNode const& _ast)
void CodeHighlighter::processError(dev::Exception const& _exception) void CodeHighlighter::processError(dev::Exception const& _exception)
{ {
Location const* location = boost::get_error_info<errinfo_sourceLocation>(_exception); SourceLocation const* location = boost::get_error_info<errinfo_sourceLocation>(_exception);
if (location) if (location)
m_formats.push_back(FormatRange(CodeHighlighterSettings::CompilationError, *location)); m_formats.push_back(FormatRange(CodeHighlighterSettings::CompilationError, *location));
} }

5
mix/CodeHighlighter.h

@ -32,11 +32,10 @@ namespace dev
{ {
struct Exception; struct Exception;
struct SourceLocation;
namespace solidity namespace solidity
{ {
class ASTNode; class ASTNode;
struct Location;
} }
namespace mix namespace mix
@ -74,7 +73,7 @@ public:
struct FormatRange struct FormatRange
{ {
FormatRange(CodeHighlighterSettings::Token _t, int _start, int _length): token(_t), start(_start), length(_length) {} FormatRange(CodeHighlighterSettings::Token _t, int _start, int _length): token(_t), start(_start), length(_length) {}
FormatRange(CodeHighlighterSettings::Token _t, solidity::Location const& _location); FormatRange(CodeHighlighterSettings::Token _t, SourceLocation const& _location);
bool operator<(FormatRange const& _other) const { return start < _other.start || (start == _other.start && length < _other.length); } bool operator<(FormatRange const& _other) const { return start < _other.start || (start == _other.start && length < _other.length); }
CodeHighlighterSettings::Token token; CodeHighlighterSettings::Token token;

3
mix/CodeModel.cpp

@ -25,6 +25,7 @@
#include <QDebug> #include <QDebug>
#include <QApplication> #include <QApplication>
#include <QtQml> #include <QtQml>
#include <libevmcore/SourceLocation.h>
#include <libsolidity/CompilerStack.h> #include <libsolidity/CompilerStack.h>
#include <libsolidity/SourceReferenceFormatter.h> #include <libsolidity/SourceReferenceFormatter.h>
#include <libsolidity/InterfaceHandler.h> #include <libsolidity/InterfaceHandler.h>
@ -212,7 +213,7 @@ void CodeModel::runCompilationJob(int _jobId)
{ {
std::ostringstream error; std::ostringstream error;
solidity::SourceReferenceFormatter::printExceptionInformation(error, _exception, "Error", cs); solidity::SourceReferenceFormatter::printExceptionInformation(error, _exception, "Error", cs);
solidity::Location const* location = boost::get_error_info<solidity::errinfo_sourceLocation>(_exception); SourceLocation const* location = boost::get_error_info<solidity::errinfo_sourceLocation>(_exception);
QString message = QString::fromStdString(error.str()); QString message = QString::fromStdString(error.str());
CompiledContract* contract = nullptr; CompiledContract* contract = nullptr;
if (location && location->sourceName.get() && (contract = contractByDocumentId(QString::fromStdString(*location->sourceName)))) if (location && location->sourceName.get() && (contract = contractByDocumentId(QString::fromStdString(*location->sourceName))))

129
test/Assembly.cpp

@ -0,0 +1,129 @@
/*
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 Lefteris Karapetsas <lefteris@ethdev.com>
* @date 2015
* Unit tests for Assembly Items from evmcore/Assembly.h
*/
#include <string>
#include <iostream>
#include <boost/test/unit_test.hpp>
#include <libdevcore/Log.h>
#include <libevmcore/SourceLocation.h>
#include <libsolidity/Scanner.h>
#include <libsolidity/Parser.h>
#include <libsolidity/NameAndTypeResolver.h>
#include <libsolidity/Compiler.h>
#include <libsolidity/AST.h>
#include <libevmcore/Assembly.h>
using namespace std;
using namespace dev::eth;
namespace dev
{
namespace solidity
{
namespace test
{
namespace
{
eth::AssemblyItems compileContract(const string& _sourceCode)
{
Parser parser;
ASTPointer<SourceUnit> sourceUnit;
BOOST_REQUIRE_NO_THROW(sourceUnit = parser.parse(make_shared<Scanner>(CharStream(_sourceCode))));
NameAndTypeResolver resolver({});
resolver.registerDeclarations(*sourceUnit);
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
BOOST_REQUIRE_NO_THROW(resolver.resolveNamesAndTypes(*contract));
}
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
BOOST_REQUIRE_NO_THROW(resolver.checkTypeRequirements(*contract));
}
for (ASTPointer<ASTNode> const& node: sourceUnit->getNodes())
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
{
Compiler compiler;
compiler.compileContract(*contract, map<ContractDefinition const*, bytes const*>{});
return compiler.getRuntimeAssemblyItems();
}
BOOST_FAIL("No contract found in source.");
return AssemblyItems();
}
void checkAssemblyLocations(AssemblyItems const& _items, std::vector<SourceLocation> _locations)
{
size_t i = 0;
BOOST_CHECK_EQUAL(_items.size(), _locations.size());
for (auto const& it: _items)
{
BOOST_CHECK_MESSAGE(it.getLocation() == _locations[i],
std::string("Location mismatch for assembly item ") + std::to_string(i));
++i;
}
}
} // end anonymous namespace
BOOST_AUTO_TEST_SUITE(Assembly)
BOOST_AUTO_TEST_CASE(location_test)
{
char const* sourceCode = "contract test {\n"
" function f() returns (uint256 a)\n"
" {\n"
" return 16;\n"
" }\n"
"}\n";
std::shared_ptr<std::string const> n = make_shared<std::string>("source");
AssemblyItems items = compileContract(sourceCode);
std::vector<SourceLocation> locations {
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(), SourceLocation(),
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(), SourceLocation(), SourceLocation(),
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(0, 77, n), SourceLocation(0, 77, n),
SourceLocation(0, 77, n),
SourceLocation(18, 75, n), SourceLocation(40, 49, n),
SourceLocation(61, 70, n), SourceLocation(61, 70, n), SourceLocation(61, 70, n),
SourceLocation(), SourceLocation(),
SourceLocation(61, 70, n), SourceLocation(61, 70, n), SourceLocation(61, 70, n)
};
checkAssemblyLocations(items, locations);
}
BOOST_AUTO_TEST_SUITE_END()
}
}
} // end namespaces

1
test/SolidityExpressionCompiler.cpp

@ -127,6 +127,7 @@ bytes compileFirstExpression(const string& _sourceCode, vector<vector<string>> _
BOOST_REQUIRE(extractor.getExpression() != nullptr); BOOST_REQUIRE(extractor.getExpression() != nullptr);
CompilerContext context; CompilerContext context;
context.resetVisitedNodes(contract);
context.setInheritanceHierarchy(inheritanceHierarchy); context.setInheritanceHierarchy(inheritanceHierarchy);
unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack unsigned parametersSize = _localVariables.size(); // assume they are all one slot on the stack
context.adjustStackOffset(parametersSize); context.adjustStackOffset(parametersSize);

2
test/SolidityInterface.cpp

@ -50,7 +50,7 @@ public:
string getSourcePart(ASTNode const& _node) const string getSourcePart(ASTNode const& _node) const
{ {
Location location = _node.getLocation(); SourceLocation location = _node.getLocation();
BOOST_REQUIRE(!location.isEmpty()); BOOST_REQUIRE(!location.isEmpty());
return m_interface.substr(location.start, location.end - location.start); return m_interface.substr(location.start, location.end - location.start);
} }

Loading…
Cancel
Save