Merge remote-tracking branch 'upstream/develop' into evmjit

This commit is contained in:
Paweł Bylica 2015-02-27 08:59:49 +01:00
commit 0ae47c927a
17 changed files with 731 additions and 683 deletions

96
AST.h
View File

@ -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) {}

View File

@ -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;
} }

View File

@ -1,60 +0,0 @@
/*
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
* Some elementary types for the parser.
*/
#pragma once
#include <memory>
#include <string>
#include <ostream>
namespace dev
{
namespace solidity
{
/**
* Representation of an interval of source positions.
* The interval includes start and excludes end.
*/
struct Location
{
Location(int _start, int _end, std::shared_ptr<std::string const> _sourceName):
start(_start), end(_end), sourceName(_sourceName) { }
Location(): start(-1), end(-1) { }
bool isEmpty() const { return start == -1 && end == -1; }
int start;
int end;
std::shared_ptr<std::string const> sourceName;
};
/// Stream output for Location (used e.g. in boost exceptions).
inline std::ostream& operator<<(std::ostream& _out, Location const& _location)
{
if (_location.isEmpty())
return _out << "NO_LOCATION_SPECIFIED";
return _out << *_location.sourceName << "[" << _location.start << "," << _location.end << ")";
}
}
}

View File

@ -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;
@ -247,7 +250,7 @@ void Compiler::appendReturnValuePacker(TypePointers const& _typeParameters)
for (TypePointer const& type: _typeParameters) for (TypePointer const& type: _typeParameters)
{ {
CompilerUtils(m_context).copyToStackTop(stackDepth, *type); CompilerUtils(m_context).copyToStackTop(stackDepth, *type);
ExpressionCompiler::appendTypeConversion(m_context, *type, *type, true); ExpressionCompiler(m_context, m_optimize).appendTypeConversion(*type, *type, true);
bool const c_padToWords = true; bool const c_padToWords = true;
dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, *type, c_padToWords); dataOffset += CompilerUtils(m_context).storeInMemory(dataOffset, *type, c_padToWords);
stackDepth -= type->getSizeOnStack(); stackDepth -= type->getSizeOnStack();
@ -267,25 +270,27 @@ void Compiler::initializeStateVariables(ContractDefinition const& _contract)
{ {
for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables()) for (ASTPointer<VariableDeclaration> const& variable: _contract.getStateVariables())
if (variable->getValue()) if (variable->getValue())
ExpressionCompiler::appendStateVariableInitialization(m_context, *variable); ExpressionCompiler(m_context, m_optimize).appendStateVariableInitialization(*variable);
} }
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();
m_continueTags.clear(); m_continueTags.clear();
m_context << m_context.getFunctionEntryLabel(_variableDeclaration); m_context << m_context.getFunctionEntryLabel(_variableDeclaration);
ExpressionCompiler::appendStateVariableAccessor(m_context, _variableDeclaration); ExpressionCompiler(m_context, m_optimize).appendStateVariableAccessor(_variableDeclaration);
return false; return false;
} }
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)
{ {
@ -530,9 +541,10 @@ void Compiler::appendModifierOrFunctionCode()
void Compiler::compileExpression(Expression const& _expression, TypePointer const& _targetType) void Compiler::compileExpression(Expression const& _expression, TypePointer const& _targetType)
{ {
ExpressionCompiler::compileExpression(m_context, _expression, m_optimize); ExpressionCompiler expressionCompiler(m_context, m_optimize);
expressionCompiler.compile(_expression);
if (_targetType) if (_targetType)
ExpressionCompiler::appendTypeConversion(m_context, *_expression.getType(), *_targetType); expressionCompiler.appendTypeConversion(*_expression.getType(), *_targetType);
} }
} }

View File

@ -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,

View File

@ -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;
}
} }
} }

View File

@ -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;
}; };
} }

View File

@ -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>;
} }
} }

View File

@ -29,6 +29,7 @@
#include <libsolidity/ExpressionCompiler.h> #include <libsolidity/ExpressionCompiler.h>
#include <libsolidity/CompilerContext.h> #include <libsolidity/CompilerContext.h>
#include <libsolidity/CompilerUtils.h> #include <libsolidity/CompilerUtils.h>
#include <libsolidity/LValue.h>
using namespace std; using namespace std;
@ -37,68 +38,198 @@ namespace dev
namespace solidity namespace solidity
{ {
void ExpressionCompiler::compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize) void ExpressionCompiler::compile(Expression const& _expression)
{ {
ExpressionCompiler compiler(_context, _optimize); _expression.accept(*this);
_expression.accept(compiler);
}
void ExpressionCompiler::appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack,
Type const& _targetType, bool _cleanupNeeded)
{
ExpressionCompiler compiler(_context);
compiler.appendTypeConversion(_typeOnStack, _targetType, _cleanupNeeded);
}
void ExpressionCompiler::appendStateVariableAccessor(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize)
{
ExpressionCompiler compiler(_context, _optimize);
compiler.appendStateVariableAccessor(_varDecl);
}
void ExpressionCompiler::appendStateVariableInitialization(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize)
{
compileExpression(_context, *(_varDecl.getValue()), _optimize);
if (_varDecl.getValue()->getType())
appendTypeConversion(_context, *(_varDecl.getValue())->getType(), *(_varDecl.getValue())->getType());
ExpressionCompiler compiler(_context, _optimize);
compiler.appendStateVariableInitialization(_varDecl);
} }
void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration const& _varDecl) void ExpressionCompiler::appendStateVariableInitialization(VariableDeclaration const& _varDecl)
{ {
LValue var = LValue(m_context); if (!_varDecl.getValue())
var.fromDeclaration(_varDecl, _varDecl.getValue()->getLocation()); return;
var.storeValue(*_varDecl.getType(), _varDecl.getLocation()); solAssert(!!_varDecl.getValue()->getType(), "Type information not available.");
CompilerContext::LocationSetter locationSetter(m_context, &_varDecl);
_varDecl.getValue()->accept(*this);
appendTypeConversion(*_varDecl.getValue()->getType(), *_varDecl.getType(), true);
StorageItem(m_context, _varDecl).storeValue(*_varDecl.getType(), _varDecl.getLocation(), true);
}
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl)
{
CompilerContext::LocationSetter locationSetter(m_context, &_varDecl);
FunctionType accessorType(_varDecl);
unsigned length = 0;
TypePointers const& paramTypes = accessorType.getParameterTypes();
// move arguments to memory
for (TypePointer const& paramType: boost::adaptors::reverse(paramTypes))
length += CompilerUtils(m_context).storeInMemory(length, *paramType, true);
// retrieve the position of the variable
m_context << m_context.getStorageLocationOfVariable(_varDecl);
TypePointer returnType = _varDecl.getType();
for (TypePointer const& paramType: paramTypes)
{
// move offset to memory
CompilerUtils(m_context).storeInMemory(length);
unsigned argLen = CompilerUtils::getPaddedSize(paramType->getCalldataEncodedSize());
length -= argLen;
m_context << u256(argLen + 32) << u256(length) << eth::Instruction::SHA3;
returnType = dynamic_cast<MappingType const&>(*returnType).getValueType();
}
unsigned retSizeOnStack = 0;
solAssert(accessorType.getReturnParameterTypes().size() >= 1, "");
if (StructType const* structType = dynamic_cast<StructType const*>(returnType.get()))
{
auto const& names = accessorType.getReturnParameterNames();
auto const& types = accessorType.getReturnParameterTypes();
// struct
for (size_t i = 0; i < names.size(); ++i)
{
m_context << eth::Instruction::DUP1
<< structType->getStorageOffsetOfMember(names[i])
<< eth::Instruction::ADD;
StorageItem(m_context, types[i]).retrieveValue(SourceLocation(), true);
solAssert(types[i]->getSizeOnStack() == 1, "Returning struct elements with stack size != 1 not yet implemented.");
m_context << eth::Instruction::SWAP1;
retSizeOnStack += types[i]->getSizeOnStack();
}
m_context << eth::Instruction::POP;
}
else
{
// simple value
solAssert(accessorType.getReturnParameterTypes().size() == 1, "");
StorageItem(m_context, returnType).retrieveValue(SourceLocation(), true);
retSizeOnStack = returnType->getSizeOnStack();
}
solAssert(retSizeOnStack <= 15, "Stack too deep.");
m_context << eth::dupInstruction(retSizeOnStack + 1) << eth::Instruction::JUMP;
}
void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded)
{
// For a type extension, we need to remove all higher-order bits that we might have ignored in
// previous operations.
// @todo: store in the AST whether the operand might have "dirty" higher order bits
if (_typeOnStack == _targetType && !_cleanupNeeded)
return;
Type::Category stackTypeCategory = _typeOnStack.getCategory();
Type::Category targetTypeCategory = _targetType.getCategory();
if (stackTypeCategory == Type::Category::String)
{
StaticStringType const& typeOnStack = dynamic_cast<StaticStringType const&>(_typeOnStack);
if (targetTypeCategory == Type::Category::Integer)
{
// conversion from string to hash. no need to clean the high bit
// only to shift right because of opposite alignment
IntegerType const& targetIntegerType = dynamic_cast<IntegerType const&>(_targetType);
solAssert(targetIntegerType.isHash(), "Only conversion between String and Hash is allowed.");
solAssert(targetIntegerType.getNumBits() == typeOnStack.getNumBytes() * 8, "The size should be the same.");
m_context << (u256(1) << (256 - typeOnStack.getNumBytes() * 8)) << eth::Instruction::SWAP1 << eth::Instruction::DIV;
}
else
{
// clear lower-order bytes for conversion to shorter strings - we always clean
solAssert(targetTypeCategory == Type::Category::String, "Invalid type conversion requested.");
StaticStringType const& targetType = dynamic_cast<StaticStringType const&>(_targetType);
if (targetType.getNumBytes() < typeOnStack.getNumBytes())
{
if (targetType.getNumBytes() == 0)
m_context << eth::Instruction::DUP1 << eth::Instruction::XOR;
else
m_context << (u256(1) << (256 - targetType.getNumBytes() * 8))
<< eth::Instruction::DUP1 << eth::Instruction::SWAP2
<< eth::Instruction::DIV << eth::Instruction::MUL;
}
}
}
else if (stackTypeCategory == Type::Category::Enum)
solAssert(targetTypeCategory == Type::Category::Integer ||
targetTypeCategory == Type::Category::Enum, "");
else if (stackTypeCategory == Type::Category::Integer ||
stackTypeCategory == Type::Category::Contract ||
stackTypeCategory == Type::Category::IntegerConstant)
{
if (targetTypeCategory == Type::Category::String && stackTypeCategory == Type::Category::Integer)
{
// conversion from hash to string. no need to clean the high bit
// only to shift left because of opposite alignment
StaticStringType const& targetStringType = dynamic_cast<StaticStringType const&>(_targetType);
IntegerType const& typeOnStack = dynamic_cast<IntegerType const&>(_typeOnStack);
solAssert(typeOnStack.isHash(), "Only conversion between String and Hash is allowed.");
solAssert(typeOnStack.getNumBits() == targetStringType.getNumBytes() * 8, "The size should be the same.");
m_context << (u256(1) << (256 - typeOnStack.getNumBits())) << eth::Instruction::MUL;
}
else if (targetTypeCategory == Type::Category::Enum)
// just clean
appendTypeConversion(_typeOnStack, *_typeOnStack.getRealType(), true);
else
{
solAssert(targetTypeCategory == Type::Category::Integer || targetTypeCategory == Type::Category::Contract, "");
IntegerType addressType(0, IntegerType::Modifier::Address);
IntegerType const& targetType = targetTypeCategory == Type::Category::Integer
? dynamic_cast<IntegerType const&>(_targetType) : addressType;
if (stackTypeCategory == Type::Category::IntegerConstant)
{
IntegerConstantType const& constType = dynamic_cast<IntegerConstantType const&>(_typeOnStack);
// We know that the stack is clean, we only have to clean for a narrowing conversion
// where cleanup is forced.
if (targetType.getNumBits() < constType.getIntegerType()->getNumBits() && _cleanupNeeded)
appendHighBitsCleanup(targetType);
}
else
{
IntegerType const& typeOnStack = stackTypeCategory == Type::Category::Integer
? dynamic_cast<IntegerType const&>(_typeOnStack) : addressType;
// Widening: clean up according to source type width
// Non-widening and force: clean up according to target type bits
if (targetType.getNumBits() > typeOnStack.getNumBits())
appendHighBitsCleanup(typeOnStack);
else if (_cleanupNeeded)
appendHighBitsCleanup(targetType);
}
}
}
else if (_typeOnStack != _targetType)
// All other types should not be convertible to non-equal types.
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid type conversion requested."));
} }
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());
_assignment.getLeftHandSide().accept(*this); _assignment.getLeftHandSide().accept(*this);
solAssert(m_currentLValue.isValid(), "LValue not retrieved."); solAssert(!!m_currentLValue, "LValue not retrieved.");
Token::Value op = _assignment.getAssignmentOperator(); Token::Value op = _assignment.getAssignmentOperator();
if (op != Token::Assign) // compound assignment if (op != Token::Assign) // compound assignment
{ {
solAssert(_assignment.getType()->isValueType(), "Compound operators not implemented for non-value types."); solAssert(_assignment.getType()->isValueType(), "Compound operators not implemented for non-value types.");
if (m_currentLValue.storesReferenceOnStack()) if (m_currentLValue->storesReferenceOnStack())
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2; m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
m_currentLValue.retrieveValue(_assignment.getLocation(), true); m_currentLValue->retrieveValue(_assignment.getLocation(), true);
appendOrdinaryBinaryOperatorCode(Token::AssignmentToBinaryOp(op), *_assignment.getType()); appendOrdinaryBinaryOperatorCode(Token::AssignmentToBinaryOp(op), *_assignment.getType());
if (m_currentLValue.storesReferenceOnStack()) if (m_currentLValue->storesReferenceOnStack())
m_context << eth::Instruction::SWAP1; m_context << eth::Instruction::SWAP1;
} }
m_currentLValue.storeValue(*_assignment.getRightHandSide().getType(), _assignment.getLocation()); m_currentLValue->storeValue(*_assignment.getRightHandSide().getType(), _assignment.getLocation());
m_currentLValue.reset(); m_currentLValue.reset();
return false; return false;
} }
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
@ -120,17 +251,17 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
m_context << eth::Instruction::NOT; m_context << eth::Instruction::NOT;
break; break;
case Token::Delete: // delete case Token::Delete: // delete
solAssert(m_currentLValue.isValid(), "LValue not retrieved."); solAssert(!!m_currentLValue, "LValue not retrieved.");
m_currentLValue.setToZero(_unaryOperation.getLocation()); m_currentLValue->setToZero(_unaryOperation.getLocation());
m_currentLValue.reset(); m_currentLValue.reset();
break; break;
case Token::Inc: // ++ (pre- or postfix) case Token::Inc: // ++ (pre- or postfix)
case Token::Dec: // -- (pre- or postfix) case Token::Dec: // -- (pre- or postfix)
solAssert(m_currentLValue.isValid(), "LValue not retrieved."); solAssert(!!m_currentLValue, "LValue not retrieved.");
m_currentLValue.retrieveValue(_unaryOperation.getLocation()); m_currentLValue->retrieveValue(_unaryOperation.getLocation());
if (!_unaryOperation.isPrefixOperation()) if (!_unaryOperation.isPrefixOperation())
{ {
if (m_currentLValue.storesReferenceOnStack()) if (m_currentLValue->storesReferenceOnStack())
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2; m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2;
else else
m_context << eth::Instruction::DUP1; m_context << eth::Instruction::DUP1;
@ -142,10 +273,11 @@ bool ExpressionCompiler::visit(UnaryOperation const& _unaryOperation)
m_context << eth::Instruction::SWAP1 << eth::Instruction::SUB; // @todo avoid the swap m_context << eth::Instruction::SWAP1 << eth::Instruction::SUB; // @todo avoid the swap
// Stack for prefix: [ref] (*ref)+-1 // Stack for prefix: [ref] (*ref)+-1
// Stack for postfix: *ref [ref] (*ref)+-1 // Stack for postfix: *ref [ref] (*ref)+-1
if (m_currentLValue.storesReferenceOnStack()) if (m_currentLValue->storesReferenceOnStack())
m_context << eth::Instruction::SWAP1; m_context << eth::Instruction::SWAP1;
m_currentLValue.storeValue(*_unaryOperation.getType(), _unaryOperation.getLocation(), m_currentLValue->storeValue(
!_unaryOperation.isPrefixOperation()); *_unaryOperation.getType(), _unaryOperation.getLocation(),
!_unaryOperation.isPrefixOperation());
m_currentLValue.reset(); m_currentLValue.reset();
break; break;
case Token::Add: // + case Token::Add: // +
@ -163,6 +295,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();
@ -175,7 +308,7 @@ bool ExpressionCompiler::visit(BinaryOperation const& _binaryOperation)
else else
{ {
bool cleanupNeeded = commonType.getCategory() == Type::Category::Integer && bool cleanupNeeded = commonType.getCategory() == Type::Category::Integer &&
(Token::isCompareOp(c_op) || c_op == Token::Div || c_op == Token::Mod); (Token::isCompareOp(c_op) || c_op == Token::Div || c_op == Token::Mod);
// for commutative operators, push the literal as late as possible to allow improved optimization // for commutative operators, push the literal as late as possible to allow improved optimization
auto isLiteral = [](Expression const& _e) auto isLiteral = [](Expression const& _e)
@ -209,6 +342,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 +560,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())
{ {
@ -499,8 +634,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
{ {
StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.getExpression().getType()); StructType const& type = dynamic_cast<StructType const&>(*_memberAccess.getExpression().getType());
m_context << type.getStorageOffsetOfMember(member) << eth::Instruction::ADD; m_context << type.getStorageOffsetOfMember(member) << eth::Instruction::ADD;
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, _memberAccess.getType()); setLValueToStorageItem(_memberAccess);
m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess);
break; break;
} }
case Type::Category::Enum: case Type::Category::Enum:
@ -546,8 +680,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
m_context << eth::Instruction::SWAP1 << eth::Instruction::POP; m_context << eth::Instruction::SWAP1 << eth::Instruction::POP;
break; break;
case ArrayType::Location::Storage: case ArrayType::Location::Storage:
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, _memberAccess.getType()); setLValueToStorageItem(_memberAccess);
m_currentLValue.retrieveValueIfLValueNotRequested(_memberAccess);
break; break;
default: default:
solAssert(false, "Unsupported array location."); solAssert(false, "Unsupported array location.");
@ -562,6 +695,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();
@ -576,8 +710,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
m_context << eth::Instruction::SWAP1; m_context << eth::Instruction::SWAP1;
appendTypeMoveToMemory(IntegerType(256)); appendTypeMoveToMemory(IntegerType(256));
m_context << u256(0) << eth::Instruction::SHA3; m_context << u256(0) << eth::Instruction::SHA3;
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, _indexAccess.getType()); setLValueToStorageItem( _indexAccess);
m_currentLValue.retrieveValueIfLValueNotRequested(_indexAccess);
} }
else if (baseType.getCategory() == Type::Category::Array) else if (baseType.getCategory() == Type::Category::Array)
{ {
@ -609,8 +742,7 @@ bool ExpressionCompiler::visit(IndexAccess const& _indexAccess)
CompilerUtils(m_context).computeHashStatic(); CompilerUtils(m_context).computeHashStatic();
} }
m_context << eth::Instruction::ADD; m_context << eth::Instruction::ADD;
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, _indexAccess.getType()); setLValueToStorageItem(_indexAccess);
m_currentLValue.retrieveValueIfLValueNotRequested(_indexAccess);
} }
else else
solAssert(false, "Index access only allowed for mappings or arrays."); solAssert(false, "Index access only allowed for mappings or arrays.");
@ -631,10 +763,7 @@ void ExpressionCompiler::endVisit(Identifier const& _identifier)
else if (FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(declaration)) else if (FunctionDefinition const* functionDef = dynamic_cast<FunctionDefinition const*>(declaration))
m_context << m_context.getVirtualFunctionEntryLabel(*functionDef).pushTag(); m_context << m_context.getVirtualFunctionEntryLabel(*functionDef).pushTag();
else if (dynamic_cast<VariableDeclaration const*>(declaration)) else if (dynamic_cast<VariableDeclaration const*>(declaration))
{ setLValueFromDeclaration(*declaration, _identifier);
m_currentLValue.fromDeclaration(*declaration, _identifier.getLocation());
m_currentLValue.retrieveValueIfLValueNotRequested(_identifier);
}
else if (dynamic_cast<ContractDefinition const*>(declaration)) else if (dynamic_cast<ContractDefinition const*>(declaration))
{ {
// no-op // no-op
@ -791,96 +920,6 @@ void ExpressionCompiler::appendShiftOperatorCode(Token::Value _operator)
} }
} }
void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded)
{
// For a type extension, we need to remove all higher-order bits that we might have ignored in
// previous operations.
// @todo: store in the AST whether the operand might have "dirty" higher order bits
if (_typeOnStack == _targetType && !_cleanupNeeded)
return;
Type::Category stackTypeCategory = _typeOnStack.getCategory();
Type::Category targetTypeCategory = _targetType.getCategory();
if (stackTypeCategory == Type::Category::String)
{
StaticStringType const& typeOnStack = dynamic_cast<StaticStringType const&>(_typeOnStack);
if (targetTypeCategory == Type::Category::Integer)
{
// conversion from string to hash. no need to clean the high bit
// only to shift right because of opposite alignment
IntegerType const& targetIntegerType = dynamic_cast<IntegerType const&>(_targetType);
solAssert(targetIntegerType.isHash(), "Only conversion between String and Hash is allowed.");
solAssert(targetIntegerType.getNumBits() == typeOnStack.getNumBytes() * 8, "The size should be the same.");
m_context << (u256(1) << (256 - typeOnStack.getNumBytes() * 8)) << eth::Instruction::SWAP1 << eth::Instruction::DIV;
}
else
{
// clear lower-order bytes for conversion to shorter strings - we always clean
solAssert(targetTypeCategory == Type::Category::String, "Invalid type conversion requested.");
StaticStringType const& targetType = dynamic_cast<StaticStringType const&>(_targetType);
if (targetType.getNumBytes() < typeOnStack.getNumBytes())
{
if (targetType.getNumBytes() == 0)
m_context << eth::Instruction::DUP1 << eth::Instruction::XOR;
else
m_context << (u256(1) << (256 - targetType.getNumBytes() * 8))
<< eth::Instruction::DUP1 << eth::Instruction::SWAP2
<< eth::Instruction::DIV << eth::Instruction::MUL;
}
}
}
else if (stackTypeCategory == Type::Category::Enum)
solAssert(targetTypeCategory == Type::Category::Integer ||
targetTypeCategory == Type::Category::Enum, "");
else if (stackTypeCategory == Type::Category::Integer || stackTypeCategory == Type::Category::Contract ||
stackTypeCategory == Type::Category::IntegerConstant)
{
if (targetTypeCategory == Type::Category::String && stackTypeCategory == Type::Category::Integer)
{
// conversion from hash to string. no need to clean the high bit
// only to shift left because of opposite alignment
StaticStringType const& targetStringType = dynamic_cast<StaticStringType const&>(_targetType);
IntegerType const& typeOnStack = dynamic_cast<IntegerType const&>(_typeOnStack);
solAssert(typeOnStack.isHash(), "Only conversion between String and Hash is allowed.");
solAssert(typeOnStack.getNumBits() == targetStringType.getNumBytes() * 8, "The size should be the same.");
m_context << (u256(1) << (256 - typeOnStack.getNumBits())) << eth::Instruction::MUL;
}
else if (targetTypeCategory == Type::Category::Enum)
// just clean
appendTypeConversion(_typeOnStack, *_typeOnStack.getRealType(), true);
else
{
solAssert(targetTypeCategory == Type::Category::Integer || targetTypeCategory == Type::Category::Contract, "");
IntegerType addressType(0, IntegerType::Modifier::Address);
IntegerType const& targetType = targetTypeCategory == Type::Category::Integer
? dynamic_cast<IntegerType const&>(_targetType) : addressType;
if (stackTypeCategory == Type::Category::IntegerConstant)
{
IntegerConstantType const& constType = dynamic_cast<IntegerConstantType const&>(_typeOnStack);
// We know that the stack is clean, we only have to clean for a narrowing conversion
// where cleanup is forced.
if (targetType.getNumBits() < constType.getIntegerType()->getNumBits() && _cleanupNeeded)
appendHighBitsCleanup(targetType);
}
else
{
IntegerType const& typeOnStack = stackTypeCategory == Type::Category::Integer
? dynamic_cast<IntegerType const&>(_typeOnStack) : addressType;
// Widening: clean up according to source type width
// Non-widening and force: clean up according to target type bits
if (targetType.getNumBits() > typeOnStack.getNumBits())
appendHighBitsCleanup(typeOnStack);
else if (_cleanupNeeded)
appendHighBitsCleanup(targetType);
}
}
}
else if (_typeOnStack != _targetType)
// All other types should not be convertible to non-equal types.
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Invalid type conversion requested."));
}
void ExpressionCompiler::appendHighBitsCleanup(IntegerType const& _typeOnStack) void ExpressionCompiler::appendHighBitsCleanup(IntegerType const& _typeOnStack)
{ {
if (_typeOnStack.getNumBits() == 256) if (_typeOnStack.getNumBits() == 256)
@ -991,318 +1030,21 @@ void ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType,
appendTypeMoveToMemory(_expectedType); appendTypeMoveToMemory(_expectedType);
} }
void ExpressionCompiler::appendStateVariableAccessor(VariableDeclaration const& _varDecl) void ExpressionCompiler::setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression)
{ {
FunctionType accessorType(_varDecl); if (m_context.isLocalVariable(&_declaration))
setLValue<StackVariable>(_expression, _declaration);
unsigned length = 0; else if (m_context.isStateVariable(&_declaration))
TypePointers const& paramTypes = accessorType.getParameterTypes(); setLValue<StorageItem>(_expression, _declaration);
// move arguments to memory
for (TypePointer const& paramType: boost::adaptors::reverse(paramTypes))
length += CompilerUtils(m_context).storeInMemory(length, *paramType, true);
// retrieve the position of the variable
m_context << m_context.getStorageLocationOfVariable(_varDecl);
TypePointer returnType = _varDecl.getType();
for (TypePointer const& paramType: paramTypes)
{
// move offset to memory
CompilerUtils(m_context).storeInMemory(length);
unsigned argLen = CompilerUtils::getPaddedSize(paramType->getCalldataEncodedSize());
length -= argLen;
m_context << u256(argLen + 32) << u256(length) << eth::Instruction::SHA3;
returnType = dynamic_cast<MappingType const&>(*returnType).getValueType();
}
unsigned retSizeOnStack = 0;
solAssert(accessorType.getReturnParameterTypes().size() >= 1, "");
if (StructType const* structType = dynamic_cast<StructType const*>(returnType.get()))
{
auto const& names = accessorType.getReturnParameterNames();
auto const& types = accessorType.getReturnParameterTypes();
// struct
for (size_t i = 0; i < names.size(); ++i)
{
m_context << eth::Instruction::DUP1
<< structType->getStorageOffsetOfMember(names[i])
<< eth::Instruction::ADD;
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, types[i]);
m_currentLValue.retrieveValue(Location(), true);
solAssert(types[i]->getSizeOnStack() == 1, "Returning struct elements with stack size != 1 not yet implemented.");
m_context << eth::Instruction::SWAP1;
retSizeOnStack += types[i]->getSizeOnStack();
}
m_context << eth::Instruction::POP;
}
else else
{ BOOST_THROW_EXCEPTION(InternalCompilerError()
// simple value << errinfo_sourceLocation(_expression.getLocation())
solAssert(accessorType.getReturnParameterTypes().size() == 1, ""); << errinfo_comment("Identifier type not supported or identifier not found."));
m_currentLValue = LValue(m_context, LValue::LValueType::Storage, returnType);
m_currentLValue.retrieveValue(Location(), true);
retSizeOnStack = returnType->getSizeOnStack();
}
solAssert(retSizeOnStack <= 15, "Stack too deep.");
m_context << eth::dupInstruction(retSizeOnStack + 1) << eth::Instruction::JUMP;
} }
ExpressionCompiler::LValue::LValue(CompilerContext& _compilerContext, LValueType _type, void ExpressionCompiler::setLValueToStorageItem(Expression const& _expression)
TypePointer const& _dataType, unsigned _baseStackOffset):
m_context(&_compilerContext), m_type(_type), m_dataType(_dataType),
m_baseStackOffset(_baseStackOffset)
{ {
//@todo change the type cast for arrays setLValue<StorageItem>(_expression, _expression.getType());
solAssert(m_dataType->getStorageSize() <= numeric_limits<unsigned>::max(),
"The storage size of " + m_dataType->toString() + " should fit in unsigned");
if (m_type == LValueType::Storage)
m_size = unsigned(m_dataType->getStorageSize());
else
m_size = unsigned(m_dataType->getSizeOnStack());
}
void ExpressionCompiler::LValue::fromDeclaration(Declaration const& _declaration, Location const& _location)
{
if (m_context->isLocalVariable(&_declaration))
{
m_type = LValueType::Stack;
m_dataType = _declaration.getType();
m_size = m_dataType->getSizeOnStack();
m_baseStackOffset = m_context->getBaseStackOffsetOfVariable(_declaration);
}
else if (m_context->isStateVariable(&_declaration))
{
*m_context << m_context->getStorageLocationOfVariable(_declaration);
m_type = LValueType::Storage;
m_dataType = _declaration.getType();
solAssert(m_dataType->getStorageSize() <= numeric_limits<unsigned>::max(),
"The storage size of " + m_dataType->toString() + " should fit in an unsigned");
m_size = unsigned(m_dataType->getStorageSize());
}
else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Identifier type not supported or identifier not found."));
}
void ExpressionCompiler::LValue::retrieveValue(Location const& _location, bool _remove) const
{
switch (m_type)
{
case LValueType::Stack:
{
unsigned stackPos = m_context->baseToCurrentStackOffset(m_baseStackOffset);
if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Stack too deep."));
for (unsigned i = 0; i < m_size; ++i)
*m_context << eth::dupInstruction(stackPos + 1);
break;
}
case LValueType::Storage:
retrieveValueFromStorage(_remove);
break;
case LValueType::Memory:
if (!m_dataType->isValueType())
break; // no distinction between value and reference for non-value types
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Location type not yet implemented."));
break;
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Unsupported location type."));
break;
}
}
void ExpressionCompiler::LValue::retrieveValueFromStorage(bool _remove) const
{
if (!m_dataType->isValueType())
return; // no distinction between value and reference for non-value types
if (!_remove)
*m_context << eth::Instruction::DUP1;
if (m_size == 1)
*m_context << eth::Instruction::SLOAD;
else
for (unsigned i = 0; i < m_size; ++i)
{
*m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1;
if (i + 1 < m_size)
*m_context << u256(1) << eth::Instruction::ADD;
else
*m_context << eth::Instruction::POP;
}
}
void ExpressionCompiler::LValue::storeValue(Type const& _sourceType, Location const& _location, bool _move) const
{
switch (m_type)
{
case LValueType::Stack:
{
unsigned stackDiff = m_context->baseToCurrentStackOffset(m_baseStackOffset) - m_size + 1;
if (stackDiff > 16)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Stack too deep."));
else if (stackDiff > 0)
for (unsigned i = 0; i < m_size; ++i)
*m_context << eth::swapInstruction(stackDiff) << eth::Instruction::POP;
if (!_move)
retrieveValue(_location);
break;
}
case LValueType::Storage:
// stack layout: value value ... value target_ref
if (m_dataType->isValueType())
{
if (!_move) // copy values
{
if (m_size + 1 > 16)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Stack too deep."));
for (unsigned i = 0; i < m_size; ++i)
*m_context << eth::dupInstruction(m_size + 1) << eth::Instruction::SWAP1;
}
if (m_size > 0) // store high index value first
*m_context << u256(m_size - 1) << eth::Instruction::ADD;
for (unsigned i = 0; i < m_size; ++i)
{
if (i + 1 >= m_size)
*m_context << eth::Instruction::SSTORE;
else
// stack here: value value ... value value (target_ref+offset)
*m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2
<< eth::Instruction::SSTORE
<< u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB;
}
}
else
{
solAssert(_sourceType.getCategory() == m_dataType->getCategory(), "Wrong type conversation for assignment.");
if (m_dataType->getCategory() == Type::Category::Array)
{
CompilerUtils(*m_context).copyByteArrayToStorage(
dynamic_cast<ArrayType const&>(*m_dataType),
dynamic_cast<ArrayType const&>(_sourceType));
if (_move)
*m_context << eth::Instruction::POP;
}
else if (m_dataType->getCategory() == Type::Category::Struct)
{
// stack layout: source_ref target_ref
auto const& structType = dynamic_cast<StructType const&>(*m_dataType);
solAssert(structType == _sourceType, "Struct assignment with conversion.");
for (auto const& member: structType.getMembers())
{
// assign each member that is not a mapping
TypePointer const& memberType = member.second;
if (memberType->getCategory() == Type::Category::Mapping)
continue;
*m_context << structType.getStorageOffsetOfMember(member.first)
<< eth::Instruction::DUP3 << eth::Instruction::DUP2
<< eth::Instruction::ADD;
// stack: source_ref target_ref member_offset source_member_ref
LValue rightHandSide(*m_context, LValueType::Storage, memberType);
rightHandSide.retrieveValue(_location, true);
// stack: source_ref target_ref member_offset source_value...
*m_context << eth::dupInstruction(2 + memberType->getSizeOnStack())
<< eth::dupInstruction(2 + memberType->getSizeOnStack())
<< eth::Instruction::ADD;
// stack: source_ref target_ref member_offset source_value... target_member_ref
LValue memberLValue(*m_context, LValueType::Storage, memberType);
memberLValue.storeValue(*memberType, _location, true);
*m_context << eth::Instruction::POP;
}
if (_move)
*m_context << eth::Instruction::POP;
else
*m_context << eth::Instruction::SWAP1;
*m_context << eth::Instruction::POP;
}
else
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Invalid non-value type for assignment."));
}
break;
case LValueType::Memory:
if (!m_dataType->isValueType())
break; // no distinction between value and reference for non-value types
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Location type not yet implemented."));
break;
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Unsupported location type."));
break;
}
}
void ExpressionCompiler::LValue::setToZero(Location const& _location) const
{
switch (m_type)
{
case LValueType::Stack:
{
unsigned stackDiff = m_context->baseToCurrentStackOffset(m_baseStackOffset);
if (stackDiff > 16)
BOOST_THROW_EXCEPTION(CompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Stack too deep."));
solAssert(stackDiff >= m_size - 1, "");
for (unsigned i = 0; i < m_size; ++i)
*m_context << u256(0) << eth::swapInstruction(stackDiff + 1 - i)
<< eth::Instruction::POP;
break;
}
case LValueType::Storage:
if (m_dataType->getCategory() == Type::Category::Array)
CompilerUtils(*m_context).clearByteArray(dynamic_cast<ArrayType const&>(*m_dataType));
else if (m_dataType->getCategory() == Type::Category::Struct)
{
// stack layout: ref
auto const& structType = dynamic_cast<StructType const&>(*m_dataType);
for (auto const& member: structType.getMembers())
{
// zero each member that is not a mapping
TypePointer const& memberType = member.second;
if (memberType->getCategory() == Type::Category::Mapping)
continue;
*m_context << structType.getStorageOffsetOfMember(member.first)
<< eth::Instruction::DUP2 << eth::Instruction::ADD;
LValue memberValue(*m_context, LValueType::Storage, memberType);
memberValue.setToZero();
}
*m_context << eth::Instruction::POP;
}
else
{
if (m_size == 0)
*m_context << eth::Instruction::POP;
for (unsigned i = 0; i < m_size; ++i)
if (i + 1 >= m_size)
*m_context << u256(0) << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
else
*m_context << u256(0) << eth::Instruction::DUP2 << eth::Instruction::SSTORE
<< u256(1) << eth::Instruction::ADD;
}
break;
case LValueType::Memory:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Location type not yet implemented."));
break;
default:
BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_sourceLocation(_location)
<< errinfo_comment("Unsupported location type."));
break;
}
}
void ExpressionCompiler::LValue::retrieveValueIfLValueNotRequested(Expression const& _expression)
{
if (!_expression.lvalueRequested())
{
retrieveValue(_expression.getLocation(), true);
reset();
}
} }
} }

View File

@ -25,8 +25,10 @@
#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/Utils.h>
#include <libsolidity/ASTVisitor.h> #include <libsolidity/ASTVisitor.h>
#include <libsolidity/LValue.h>
namespace dev { namespace dev {
namespace eth namespace eth
@ -50,22 +52,28 @@ class StaticStringType;
class ExpressionCompiler: private ASTConstVisitor class ExpressionCompiler: private ASTConstVisitor
{ {
public: public:
/// Compile the given @a _expression into the @a _context.
static void compileExpression(CompilerContext& _context, Expression const& _expression, bool _optimize = false);
/// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type.
static void appendTypeConversion(CompilerContext& _context, Type const& _typeOnStack,
Type const& _targetType, bool _cleanupNeeded = false);
/// Appends code for a State Variable accessor function /// Appends code for a State Variable accessor function
static void appendStateVariableAccessor(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize = false); static void appendStateVariableAccessor(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize = false);
/// Appends code for a State Variable Initialization function explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false):
static void appendStateVariableInitialization(CompilerContext& _context, VariableDeclaration const& _varDecl, bool _optimize = false); m_optimize(_optimize), m_context(_compilerContext) {}
/// Compile the given @a _expression and leave its value on the stack.
void compile(Expression const& _expression);
/// Appends code to set a state variable to its initial value/expression.
void appendStateVariableInitialization(VariableDeclaration const& _varDecl);
/// Appends code for a State Variable accessor function
void appendStateVariableAccessor(VariableDeclaration const& _varDecl);
/// Appends an implicit or explicit type conversion. For now this comprises only erasing
/// higher-order bits (@see appendHighBitCleanup) when widening integer.
/// If @a _cleanupNeeded, high order bits cleanup is also done if no type conversion would be
/// necessary.
void appendTypeConversion(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded = false);
private: private:
explicit ExpressionCompiler(CompilerContext& _compilerContext, bool _optimize = false):
m_optimize(_optimize), m_context(_compilerContext), m_currentLValue(m_context) {}
virtual bool visit(Assignment const& _assignment) override; virtual bool visit(Assignment const& _assignment) override;
virtual bool visit(UnaryOperation const& _unaryOperation) override; virtual bool visit(UnaryOperation const& _unaryOperation) override;
virtual bool visit(BinaryOperation const& _binaryOperation) override; virtual bool visit(BinaryOperation const& _binaryOperation) override;
@ -87,11 +95,6 @@ private:
void appendShiftOperatorCode(Token::Value _operator); void appendShiftOperatorCode(Token::Value _operator);
/// @} /// @}
/// Appends an implicit or explicit type conversion. For now this comprises only erasing
/// higher-order bits (@see appendHighBitCleanup) when widening integer.
/// If @a _cleanupNeeded, high order bits cleanup is also done if no type conversion would be
/// necessary.
void appendTypeConversion(Type const& _typeOnStack, Type const& _targetType, bool _cleanupNeeded = false);
//// Appends code that cleans higher-order bits for integer types. //// Appends code that cleans higher-order bits for integer types.
void appendHighBitsCleanup(IntegerType const& _typeOnStack); void appendHighBitsCleanup(IntegerType const& _typeOnStack);
@ -111,77 +114,34 @@ private:
/// expected to be on the stack and is updated by this call. /// expected to be on the stack and is updated by this call.
void appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression); void appendExpressionCopyToMemory(Type const& _expectedType, Expression const& _expression);
/// Appends code for a State Variable accessor function /// Sets the current LValue to a new one (of the appropriate type) from the given declaration.
void appendStateVariableAccessor(VariableDeclaration const& _varDecl); /// Also retrieves the value if it was not requested by @a _expression.
void setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression);
/// Appends code for a State Variable initialization /// Sets the current LValue to a StorageItem holding the type of @a _expression. The reference is assumed
void appendStateVariableInitialization(VariableDeclaration const& _varDecl); /// to be on the stack.
/// Also retrieves the value if it was not requested by @a _expression.
/** void setLValueToStorageItem(Expression const& _expression);
* Helper class to store and retrieve lvalues to and from various locations. /// Sets the current LValue to a new LValue constructed from the arguments.
* All types except STACK store a reference in a slot on the stack, STACK just /// Also retrieves the value if it was not requested by @a _expression.
* stores the base stack offset of the variable in @a m_baseStackOffset. template <class _LValueType, class... _Arguments>
*/ void setLValue(Expression const& _expression, _Arguments const&... _arguments);
class LValue
{
public:
enum class LValueType { None, Stack, Memory, Storage };
explicit LValue(CompilerContext& _compilerContext): m_context(&_compilerContext) { reset(); }
LValue(CompilerContext& _compilerContext, LValueType _type,
std::shared_ptr<Type const> const& _dataType, unsigned _baseStackOffset = 0);
/// Set type according to the declaration and retrieve the reference.
/// @a _location is the current location
void fromDeclaration(Declaration const& _declaration, Location const& _location);
void reset() { m_type = LValueType::None; m_dataType.reset(); m_baseStackOffset = 0; m_size = 0; }
bool isValid() const { return m_type != LValueType::None; }
bool isInOnStack() const { return m_type == LValueType::Stack; }
bool isInMemory() const { return m_type == LValueType::Memory; }
bool isInStorage() const { return m_type == LValueType::Storage; }
/// @returns true if this lvalue reference type occupies a slot on the stack.
bool storesReferenceOnStack() const { return m_type == LValueType::Storage || m_type == LValueType::Memory; }
/// 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).
/// @a _location source location of the current expression, used for error reporting.
void retrieveValue(Location const& _location, bool _remove = false) const;
/// 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.
/// Stack pre: value [lvalue_ref]
/// Stack post if !_move: value_of(lvalue_ref)
void storeValue(Type const& _sourceType, Location const& _location = Location(), bool _move = false) const;
/// Stores zero in the lvalue.
/// @a _location is the source location of the requested operation
void setToZero(Location const& _location = Location()) const;
/// Convenience function to convert the stored reference to a value and reset type to NONE if
/// the reference was not requested by @a _expression.
void retrieveValueIfLValueNotRequested(Expression const& _expression);
private:
/// Convenience function to retrieve Value from Storage. Specific version of @ref retrieveValue
void retrieveValueFromStorage(bool _remove = false) const;
/// Copies from a byte array to a byte array in storage, both references on the stack.
void copyByteArrayToStorage(ArrayType const& _targetType, ArrayType const& _sourceType) const;
CompilerContext* m_context;
LValueType m_type = LValueType::None;
std::shared_ptr<Type const> m_dataType;
/// If m_type is STACK, this is base stack offset (@see
/// CompilerContext::getBaseStackOffsetOfVariable) of a local variable.
unsigned m_baseStackOffset = 0;
/// Size of the value of this lvalue on the stack or the storage.
unsigned m_size = 0;
};
bool m_optimize; bool m_optimize;
CompilerContext& m_context; CompilerContext& m_context;
LValue m_currentLValue; std::unique_ptr<LValue> m_currentLValue;
}; };
template <class _LValueType, class... _Arguments>
void ExpressionCompiler::setLValue(Expression const& _expression, _Arguments const&... _arguments)
{
solAssert(!m_currentLValue, "Current LValue not reset before trying to set new one.");
std::unique_ptr<_LValueType> lvalue(new _LValueType(m_context, _arguments...));
if (_expression.lvalueRequested())
m_currentLValue = move(lvalue);
else
lvalue->retrieveValue(_expression.getLocation(), true);
}
} }
} }

223
LValue.cpp Normal file
View File

@ -0,0 +1,223 @@
/*
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 2015
* LValues for use in the expresison compiler.
*/
#include <libsolidity/LValue.h>
#include <libevmcore/Instruction.h>
#include <libsolidity/Types.h>
#include <libsolidity/AST.h>
#include <libsolidity/CompilerUtils.h>
using namespace std;
using namespace dev;
using namespace solidity;
StackVariable::StackVariable(CompilerContext& _compilerContext, Declaration const& _declaration):
LValue(_compilerContext, _declaration.getType()),
m_baseStackOffset(m_context.getBaseStackOffsetOfVariable(_declaration)),
m_size(m_dataType->getSizeOnStack())
{
}
void StackVariable::retrieveValue(SourceLocation const& _location, bool _remove) const
{
(void)_remove;
unsigned stackPos = m_context.baseToCurrentStackOffset(m_baseStackOffset);
if (stackPos >= 15) //@todo correct this by fetching earlier or moving to memory
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
for (unsigned i = 0; i < m_size; ++i)
m_context << eth::dupInstruction(stackPos + 1);
}
void StackVariable::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
{
(void)_sourceType;
unsigned stackDiff = m_context.baseToCurrentStackOffset(m_baseStackOffset) - m_size + 1;
if (stackDiff > 16)
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
else if (stackDiff > 0)
for (unsigned i = 0; i < m_size; ++i)
m_context << eth::swapInstruction(stackDiff) << eth::Instruction::POP;
if (!_move)
retrieveValue(_location);
}
void StackVariable::setToZero(SourceLocation const& _location) const
{
unsigned stackDiff = m_context.baseToCurrentStackOffset(m_baseStackOffset);
if (stackDiff > 16)
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
solAssert(stackDiff >= m_size - 1, "");
for (unsigned i = 0; i < m_size; ++i)
m_context << u256(0) << eth::swapInstruction(stackDiff + 1 - i)
<< eth::Instruction::POP;
}
StorageItem::StorageItem(CompilerContext& _compilerContext, Declaration const& _declaration):
StorageItem(_compilerContext, _declaration.getType())
{
m_context << m_context.getStorageLocationOfVariable(_declaration);
}
StorageItem::StorageItem(CompilerContext& _compilerContext, TypePointer const& _type):
LValue(_compilerContext, _type)
{
if (m_dataType->isValueType())
{
solAssert(m_dataType->getStorageSize() == m_dataType->getSizeOnStack(), "");
solAssert(m_dataType->getStorageSize() <= numeric_limits<unsigned>::max(),
"The storage size of " + m_dataType->toString() + " should fit in an unsigned");
m_size = unsigned(m_dataType->getStorageSize());
}
else
m_size = 0; // unused
}
void StorageItem::retrieveValue(SourceLocation const&, bool _remove) const
{
if (!m_dataType->isValueType())
return; // no distinction between value and reference for non-value types
if (!_remove)
m_context << eth::Instruction::DUP1;
if (m_size == 1)
m_context << eth::Instruction::SLOAD;
else
for (unsigned i = 0; i < m_size; ++i)
{
m_context << eth::Instruction::DUP1 << eth::Instruction::SLOAD << eth::Instruction::SWAP1;
if (i + 1 < m_size)
m_context << u256(1) << eth::Instruction::ADD;
else
m_context << eth::Instruction::POP;
}
}
void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _location, bool _move) const
{
// stack layout: value value ... value target_ref
if (m_dataType->isValueType())
{
if (!_move) // copy values
{
if (m_size + 1 > 16)
BOOST_THROW_EXCEPTION(CompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Stack too deep."));
for (unsigned i = 0; i < m_size; ++i)
m_context << eth::dupInstruction(m_size + 1) << eth::Instruction::SWAP1;
}
if (m_size > 1) // store high index value first
m_context << u256(m_size - 1) << eth::Instruction::ADD;
for (unsigned i = 0; i < m_size; ++i)
{
if (i + 1 >= m_size)
m_context << eth::Instruction::SSTORE;
else
// stack here: value value ... value value (target_ref+offset)
m_context << eth::Instruction::SWAP1 << eth::Instruction::DUP2
<< eth::Instruction::SSTORE
<< u256(1) << eth::Instruction::SWAP1 << eth::Instruction::SUB;
}
}
else
{
solAssert(_sourceType.getCategory() == m_dataType->getCategory(),
"Wrong type conversation for assignment.");
if (m_dataType->getCategory() == Type::Category::Array)
{
CompilerUtils(m_context).copyByteArrayToStorage(
dynamic_cast<ArrayType const&>(*m_dataType),
dynamic_cast<ArrayType const&>(_sourceType));
if (_move)
m_context << eth::Instruction::POP;
}
else if (m_dataType->getCategory() == Type::Category::Struct)
{
// stack layout: source_ref target_ref
auto const& structType = dynamic_cast<StructType const&>(*m_dataType);
solAssert(structType == _sourceType, "Struct assignment with conversion.");
for (auto const& member: structType.getMembers())
{
// assign each member that is not a mapping
TypePointer const& memberType = member.second;
if (memberType->getCategory() == Type::Category::Mapping)
continue;
m_context << structType.getStorageOffsetOfMember(member.first)
<< eth::Instruction::DUP3 << eth::Instruction::DUP2 << eth::Instruction::ADD;
// stack: source_ref target_ref member_offset source_member_ref
StorageItem(m_context, memberType).retrieveValue(_location, true);
// stack: source_ref target_ref member_offset source_value...
m_context << eth::dupInstruction(2 + memberType->getSizeOnStack())
<< eth::dupInstruction(2 + memberType->getSizeOnStack()) << eth::Instruction::ADD;
// stack: source_ref target_ref member_offset source_value... target_member_ref
StorageItem(m_context, memberType).storeValue(*memberType, _location, true);
m_context << eth::Instruction::POP;
}
if (_move)
m_context << eth::Instruction::POP;
else
m_context << eth::Instruction::SWAP1;
m_context << eth::Instruction::POP;
}
else
BOOST_THROW_EXCEPTION(InternalCompilerError()
<< errinfo_sourceLocation(_location) << errinfo_comment("Invalid non-value type for assignment."));
}
}
void StorageItem::setToZero(SourceLocation const& _location) const
{
(void)_location;
if (m_dataType->getCategory() == Type::Category::Array)
CompilerUtils(m_context).clearByteArray(dynamic_cast<ArrayType const&>(*m_dataType));
else if (m_dataType->getCategory() == Type::Category::Struct)
{
// stack layout: ref
auto const& structType = dynamic_cast<StructType const&>(*m_dataType);
for (auto const& member: structType.getMembers())
{
// zero each member that is not a mapping
TypePointer const& memberType = member.second;
if (memberType->getCategory() == Type::Category::Mapping)
continue;
m_context << structType.getStorageOffsetOfMember(member.first)
<< eth::Instruction::DUP2 << eth::Instruction::ADD;
StorageItem(m_context, memberType).setToZero();
}
m_context << eth::Instruction::POP;
}
else
{
if (m_size == 0)
m_context << eth::Instruction::POP;
for (unsigned i = 0; i < m_size; ++i)
if (i + 1 >= m_size)
m_context << u256(0) << eth::Instruction::SWAP1 << eth::Instruction::SSTORE;
else
m_context << u256(0) << eth::Instruction::DUP2 << eth::Instruction::SSTORE
<< u256(1) << eth::Instruction::ADD;
}
}

112
LValue.h Normal file
View File

@ -0,0 +1,112 @@
/*
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 2015
* LValues for use in the expresison compiler.
*/
#pragma once
#include <memory>
#include <libevmcore/SourceLocation.h>
namespace dev
{
namespace solidity
{
class Declaration;
class Type;
class CompilerContext;
/**
* Abstract class used to retrieve, delete and store data in lvalues/variables.
*/
class LValue
{
protected:
LValue(CompilerContext& _compilerContext, std::shared_ptr<Type const> const& _dataType):
m_context(_compilerContext), m_dataType(_dataType) {}
public:
/// @returns true if this lvalue reference type occupies a slot on the stack.
virtual bool storesReferenceOnStack() const = 0;
/// 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.
/// @a _location source location of the current expression, used for error reporting.
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const = 0;
/// 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.
/// Stack pre: value [lvalue_ref]
/// Stack post: if !_move: value_of(lvalue_ref)
virtual void storeValue(Type const& _sourceType,
SourceLocation const& _location = SourceLocation(), bool _move = false) const = 0;
/// Stores zero in the lvalue.
/// @a _location is the source location of the requested operation
virtual void setToZero(SourceLocation const& _location = SourceLocation()) const = 0;
protected:
CompilerContext& m_context;
std::shared_ptr<Type const> m_dataType;
};
/**
* Local variable that is completely stored on the stack.
*/
class StackVariable: public LValue
{
public:
explicit StackVariable(CompilerContext& _compilerContext, Declaration const& _declaration);
virtual bool storesReferenceOnStack() const { return false; }
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
virtual void storeValue(Type const& _sourceType,
SourceLocation const& _location = SourceLocation(), bool _move = false) const override;
virtual void setToZero(SourceLocation const& _location = SourceLocation()) const override;
private:
/// Base stack offset (@see CompilerContext::getBaseStackOffsetOfVariable) of the local variable.
unsigned m_baseStackOffset;
/// Number of stack elements occupied by the value (not the reference).
unsigned m_size;
};
/**
* Reference to some item in storage. The (starting) position of the item is stored on the stack.
*/
class StorageItem: public LValue
{
public:
/// Constructs the LValue and pushes the location of @a _declaration onto the stack.
explicit StorageItem(CompilerContext& _compilerContext, Declaration const& _declaration);
/// Constructs the LValue and assumes that the storage reference is already on the stack.
explicit StorageItem(CompilerContext& _compilerContext, std::shared_ptr<Type const> const& _type);
virtual bool storesReferenceOnStack() const { return true; }
virtual void retrieveValue(SourceLocation const& _location, bool _remove = false) const override;
virtual void storeValue(Type const& _sourceType,
SourceLocation const& _location = SourceLocation(), bool _move = false) const override;
virtual void setToZero(SourceLocation const& _location = SourceLocation()) const override;
private:
/// Number of stack elements occupied by the value (not the reference).
/// Only used for value types.
unsigned m_size;
};
}
}

View File

@ -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);
} }

View File

@ -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();

View File

@ -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;
}; };

View File

@ -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)

View File

@ -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);
}; };