Merge remote-tracking branch 'ethereum/develop' into sol_contractCompiler

Conflicts:
	libsolidity/AST.cpp
	libsolidity/AST.h
	libsolidity/Compiler.cpp
	libsolidity/Compiler.h
	libsolidity/NameAndTypeResolver.h
	libsolidity/Types.cpp
	solc/main.cpp
	test/solidityCompiler.cpp
This commit is contained in:
Christian 2014-10-31 17:20:27 +01:00
commit 25c0e08bdf
11 changed files with 179 additions and 100 deletions

150
AST.h
View File

@ -41,9 +41,11 @@ namespace solidity
class ASTVisitor;
/// The root (abstract) class of the AST inheritance tree.
/// It is possible to traverse all direct and indirect children of an AST node by calling
/// accept, providing an ASTVisitor.
/**
* The root (abstract) class of the AST inheritance tree.
* It is possible to traverse all direct and indirect children of an AST node by calling
* accept, providing an ASTVisitor.
*/
class ASTNode: private boost::noncopyable
{
public:
@ -77,7 +79,9 @@ private:
Location m_location;
};
/// Abstract AST class for a declaration (contract, function, struct, variable).
/**
* Abstract AST class for a declaration (contract, function, struct, variable).
*/
class Declaration: public ASTNode
{
public:
@ -85,15 +89,17 @@ public:
ASTNode(_location), m_name(_name) {}
/// Returns the declared name.
const ASTString& getName() const { return *m_name; }
ASTString const& getName() const { return *m_name; }
private:
ASTPointer<ASTString> m_name;
};
/// Definition of a contract. This is the only AST nodes where child nodes are not visited in
/// document order. It first visits all struct declarations, then all variable declarations and
/// finally all function declarations.
/**
* Definition of a contract. This is the only AST nodes where child nodes are not visited in
* document order. It first visits all struct declarations, then all variable declarations and
* finally all function declarations.
*/
class ContractDefinition: public Declaration
{
public:
@ -133,9 +139,11 @@ private:
std::vector<ASTPointer<VariableDeclaration>> m_members;
};
/// Parameter list, used as function parameter list and return list.
/// None of the parameters is allowed to contain mappings (not even recursively
/// inside structs), but (@todo) this is not yet enforced.
/**
* Parameter list, used as function parameter list and return list.
* None of the parameters is allowed to contain mappings (not even recursively
* inside structs), but (@todo) this is not yet enforced.
*/
class ParameterList: public ASTNode
{
public:
@ -183,8 +191,10 @@ private:
std::vector<VariableDeclaration const*> m_localVariables;
};
/// Declaration of a variable. This can be used in various places, e.g. in function parameter
/// lists, struct definitions and even function bodys.
/**
* Declaration of a variable. This can be used in various places, e.g. in function parameter
* lists, struct definitions and even function bodys.
*/
class VariableDeclaration: public Declaration
{
public:
@ -196,8 +206,8 @@ public:
bool isTypeGivenExplicitly() const { return bool(m_typeName); }
TypeName* getTypeName() const { return m_typeName.get(); }
//! Returns the declared or inferred type. Can be an empty pointer if no type was explicitly
//! declared and there is no assignment to the variable that fixes the type.
/// Returns the declared or inferred type. Can be an empty pointer if no type was explicitly
/// declared and there is no assignment to the variable that fixes the type.
std::shared_ptr<Type const> const& getType() const { return m_type; }
void setType(std::shared_ptr<Type const> const& _type) { m_type = _type; }
@ -210,7 +220,9 @@ private:
/// Types
/// @{
/// Abstract base class of a type name, can be any built-in or user-defined type.
/**
* Abstract base class of a type name, can be any built-in or user-defined type.
*/
class TypeName: public ASTNode
{
public:
@ -222,8 +234,10 @@ public:
virtual std::shared_ptr<Type> toType() = 0;
};
/// Any pre-defined type name represented by a single keyword, i.e. it excludes mappings,
/// contracts, functions, etc.
/**
* Any pre-defined type name represented by a single keyword, i.e. it excludes mappings,
* contracts, functions, etc.
*/
class ElementaryTypeName: public TypeName
{
public:
@ -238,8 +252,10 @@ private:
Token::Value m_type;
};
/// Name referring to a user-defined type (i.e. a struct).
/// @todo some changes are necessary if this is also used to refer to contract types later
/**
* Name referring to a user-defined type (i.e. a struct).
* @todo some changes are necessary if this is also used to refer to contract types later
*/
class UserDefinedTypeName: public TypeName
{
public:
@ -248,7 +264,7 @@ public:
virtual void accept(ASTVisitor& _visitor) override;
virtual std::shared_ptr<Type> toType() override { return Type::fromUserDefinedTypeName(*this); }
const ASTString& getName() const { return *m_name; }
ASTString const& getName() const { return *m_name; }
void setReferencedStruct(StructDefinition& _referencedStruct) { m_referencedStruct = &_referencedStruct; }
StructDefinition const* getReferencedStruct() const { return m_referencedStruct; }
@ -258,7 +274,9 @@ private:
StructDefinition* m_referencedStruct;
};
/// A mapping type. Its source form is "mapping('keyType' => 'valueType')"
/**
* A mapping type. Its source form is "mapping('keyType' => 'valueType')"
*/
class Mapping: public TypeName
{
public:
@ -279,20 +297,24 @@ private:
/// @{
/// Abstract base class for statements.
/**
* Abstract base class for statements.
*/
class Statement: public ASTNode
{
public:
explicit Statement(Location const& _location): ASTNode(_location) {}
virtual void accept(ASTVisitor& _visitor) override;
//! 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
//! the number of function call arguments matches the number of formal parameters and so forth.
/// 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
/// the number of function call arguments matches the number of formal parameters and so forth.
virtual void checkTypeRequirements() = 0;
};
/// Brace-enclosed block containing zero or more statements.
/**
* Brace-enclosed block containing zero or more statements.
*/
class Block: public Statement
{
public:
@ -306,8 +328,10 @@ private:
std::vector<ASTPointer<Statement>> m_statements;
};
/// If-statement with an optional "else" part. Note that "else if" is modeled by having a new
/// if-statement as the false (else) body.
/**
* If-statement with an optional "else" part. Note that "else if" is modeled by having a new
* if-statement as the false (else) body.
*/
class IfStatement: public Statement
{
public:
@ -324,11 +348,13 @@ public:
private:
ASTPointer<Expression> m_condition;
ASTPointer<Statement> m_trueBody;
ASTPointer<Statement> m_falseBody; //< "else" part, optional
ASTPointer<Statement> m_falseBody; ///< "else" part, optional
};
/// Statement in which a break statement is legal.
/// @todo actually check this requirement.
/**
* Statement in which a break statement is legal.
* @todo actually check this requirement.
*/
class BreakableStatement: public Statement
{
public:
@ -381,15 +407,17 @@ public:
Expression* getExpression() const { return m_expression.get(); }
private:
ASTPointer<Expression> m_expression; //< value to return, optional
ASTPointer<Expression> m_expression; ///< value to return, optional
/// Pointer to the parameter list of the function, filled by the @ref NameAndTypeResolver.
ParameterList* m_returnParameters;
};
/// Definition of a variable as a statement inside a function. It requires a type name (which can
/// also be "var") but the actual assignment can be missing.
/// Examples: var a = 2; uint256 a;
/**
* Definition of a variable as a statement inside a function. It requires a type name (which can
* also be "var") but the actual assignment can be missing.
* Examples: var a = 2; uint256 a;
*/
class VariableDefinition: public Statement
{
public:
@ -477,8 +505,10 @@ private:
ASTPointer<Expression> m_rightHandSide;
};
/// Operation involving a unary operator, pre- or postfix.
/// Examples: ++i, delete x or !true
/**
* Operation involving a unary operator, pre- or postfix.
* Examples: ++i, delete x or !true
*/
class UnaryOperation: public Expression
{
public:
@ -498,8 +528,10 @@ private:
bool m_isPrefix;
};
/// Operation involving a binary operator.
/// Examples: 1 + 2, true && false or 1 <= 4
/**
* Operation involving a binary operator.
* Examples: 1 + 2, true && false or 1 <= 4
*/
class BinaryOperation: public Expression
{
public:
@ -521,7 +553,9 @@ private:
std::shared_ptr<Type const> m_commonType;
};
/// Can be ordinary function call, type cast or struct construction.
/**
* Can be ordinary function call, type cast or struct construction.
*/
class FunctionCall: public Expression
{
public:
@ -543,7 +577,9 @@ private:
std::vector<ASTPointer<Expression>> m_arguments;
};
/// Access to a member of an object. Example: x.name
/**
* Access to a member of an object. Example: x.name
*/
class MemberAccess: public Expression
{
public:
@ -551,7 +587,7 @@ public:
ASTPointer<ASTString> const& _memberName):
Expression(_location), m_expression(_expression), m_memberName(_memberName) {}
virtual void accept(ASTVisitor& _visitor) override;
const ASTString& getMemberName() const { return *m_memberName; }
ASTString const& getMemberName() const { return *m_memberName; }
virtual void checkTypeRequirements() override;
private:
@ -559,7 +595,9 @@ private:
ASTPointer<ASTString> m_memberName;
};
/// Index access to an array. Example: a[2]
/**
* Index access to an array. Example: a[2]
*/
class IndexAccess: public Expression
{
public:
@ -574,15 +612,19 @@ private:
ASTPointer<Expression> m_index;
};
/// Primary expression, i.e. an expression that do not be divided any further like a literal or
/// a variable reference.
/**
* Primary expression, i.e. an expression that cannot be divided any further. Examples are literals
* or variable references.
*/
class PrimaryExpression: public Expression
{
public:
PrimaryExpression(Location const& _location): Expression(_location) {}
};
/// An identifier, i.e. a reference to a declaration by name like a variable or function.
/**
* An identifier, i.e. a reference to a declaration by name like a variable or function.
*/
class Identifier: public PrimaryExpression
{
public:
@ -599,13 +641,15 @@ public:
private:
ASTPointer<ASTString> m_name;
//! Declaration the name refers to.
/// Declaration the name refers to.
Declaration* m_referencedDeclaration;
};
/// An elementary type name expression is used in expressions like "a = uint32(2)" to change the
/// type of an expression explicitly. Here, "uint32" is the elementary type name expression and
/// "uint32(2)" is a @ref FunctionCall.
/**
* An elementary type name expression is used in expressions like "a = uint32(2)" to change the
* type of an expression explicitly. Here, "uint32" is the elementary type name expression and
* "uint32(2)" is a @ref FunctionCall.
*/
class ElementaryTypeNameExpression: public PrimaryExpression
{
public:
@ -620,7 +664,9 @@ private:
Token::Value m_typeToken;
};
/// A literal string or number. @see Type::literalToBigEndian is used to actually parse its value.
/**
* A literal string or number. @see Type::literalToBigEndian is used to actually parse its value.
*/
class Literal: public PrimaryExpression
{
public:

View File

@ -30,13 +30,15 @@ namespace dev
namespace solidity
{
/// Pretty-printer for the abstract syntax tree (the "pretty" is arguable) for debugging purposes.
/**
* Pretty-printer for the abstract syntax tree (the "pretty" is arguable) for debugging purposes.
*/
class ASTPrinter: public ASTVisitor
{
public:
/// Create a printer for the given abstract syntax tree. If the source is specified,
/// the corresponding parts of the source are printed with each node.
ASTPrinter(ASTPointer<ASTNode> const& _ast, const std::string& _source = std::string());
ASTPrinter(ASTPointer<ASTNode> const& _ast, std::string const& _source = std::string());
/// Output the string representation of the AST to _stream.
void print(std::ostream& _stream);

View File

@ -30,12 +30,14 @@ namespace dev
namespace solidity
{
/// Visitor interface for the abstract syntax tree. This class is tightly bound to the
/// implementation of @ref ASTNode::accept and its overrides. After a call to
/// @ref ASTNode::accept, the function visit for the appropriate parameter is called and then
/// (if it returns true) this continues recursively for all child nodes in document order
/// (there is an exception for contracts). After all child nodes have been visited, endVisit is
/// called for the node.
/**
* Visitor interface for the abstract syntax tree. This class is tightly bound to the
* implementation of @ref ASTNode::accept and its overrides. After a call to
* @ref ASTNode::accept, the function visit for the appropriate parameter is called and then
* (if it returns true) this continues recursively for all child nodes in document order
* (there is an exception for contracts). After all child nodes have been visited, endVisit is
* called for the node.
*/
class ASTVisitor
{
public:

View File

@ -29,8 +29,10 @@ namespace dev
namespace solidity
{
/// Representation of an interval of source positions.
/// The interval includes start and excludes end.
/**
* Representation of an interval of source positions.
* The interval includes start and excludes end.
*/
struct Location
{
Location(int _start, int _end): start(_start), end(_end) { }

View File

@ -243,7 +243,7 @@ bool Compiler::visit(WhileStatement& _whileStatement)
m_context << loopStart;
ExpressionCompiler::compileExpression(m_context, _whileStatement.getCondition());
m_context << eth::Instruction::NOT;
m_context << eth::Instruction::ISZERO;
m_context.appendConditionalJumpTo(loopEnd);
_whileStatement.getBody().accept(*this);

View File

@ -71,10 +71,10 @@ void ExpressionCompiler::endVisit(UnaryOperation& _unaryOperation)
switch (_unaryOperation.getOperator())
{
case Token::NOT: // !
m_context << eth::Instruction::NOT;
m_context << eth::Instruction::ISZERO;
break;
case Token::BIT_NOT: // ~
m_context << eth::Instruction::BNOT;
m_context << eth::Instruction::NOT;
break;
case Token::DELETE: // delete
{
@ -262,7 +262,7 @@ void ExpressionCompiler::appendAndOrOperatorCode(BinaryOperation& _binaryOperati
_binaryOperation.getLeftExpression().accept(*this);
m_context << eth::Instruction::DUP1;
if (op == Token::AND)
m_context << eth::Instruction::NOT;
m_context << eth::Instruction::ISZERO;
eth::AssemblyItem endLabel = m_context.appendConditionalJump();
_binaryOperation.getRightExpression().accept(*this);
m_context << endLabel;
@ -274,7 +274,7 @@ void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type
{
m_context << eth::Instruction::EQ;
if (_operator == Token::NE)
m_context << eth::Instruction::NOT;
m_context << eth::Instruction::ISZERO;
}
else
{
@ -288,11 +288,11 @@ void ExpressionCompiler::appendCompareOperatorCode(Token::Value _operator, Type
{
case Token::GTE:
m_context << (isSigned ? eth::Instruction::SGT : eth::Instruction::GT)
<< eth::Instruction::NOT;
<< eth::Instruction::ISZERO;
break;
case Token::LTE:
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT)
<< eth::Instruction::NOT;
<< eth::Instruction::ISZERO;
break;
case Token::GT:
m_context << (isSigned ? eth::Instruction::SLT : eth::Instruction::LT);

View File

@ -33,8 +33,11 @@ namespace dev
namespace solidity
{
//! Resolves name references, resolves all types and checks that all operations are valid for the
//! inferred types. An exception is throw on the first error.
/**
* Resolves name references, types and checks types of all expressions.
* Specifically, it checks that all operations are valid for the inferred types.
* An exception is throw on the first error.
*/
class NameAndTypeResolver: private boost::noncopyable
{
public:
@ -54,15 +57,17 @@ public:
private:
void reset();
//! Maps nodes declaring a scope to scopes, i.e. ContractDefinition, FunctionDeclaration and
//! StructDefinition (@todo not yet implemented), where nullptr denotes the global scope.
/// Maps nodes declaring a scope to scopes, i.e. ContractDefinition, FunctionDeclaration and
/// StructDefinition (@todo not yet implemented), where nullptr denotes the global scope.
std::map<ASTNode const*, Scope> m_scopes;
Scope* m_currentScope;
};
//! Traverses the given AST upon construction and fills _scopes with all declarations inside the
//! AST.
/**
* Traverses the given AST upon construction and fills _scopes with all declarations inside the
* AST.
*/
class DeclarationRegistrationHelper: private ASTVisitor
{
public:
@ -87,8 +92,10 @@ private:
FunctionDefinition* m_currentFunction;
};
//! Resolves references to declarations (of variables and types) and also establishes the link
//! between a return statement and the return parameter list.
/**
* Resolves references to declarations (of variables and types) and also establishes the link
* between a return statement and the return parameter list.
*/
class ReferencesResolver: private ASTVisitor
{
public:

View File

@ -125,7 +125,7 @@ public:
/// Returns the current token
Token::Value getCurrentToken() { return m_current_token.token; }
Location getCurrentLocation() const { return m_current_token.location; }
const std::string& getCurrentLiteral() const { return m_current_token.literal; }
std::string const& getCurrentLiteral() const { return m_current_token.literal; }
///@}
///@{
@ -134,7 +134,7 @@ public:
/// Returns the next token without advancing input.
Token::Value peekNextToken() const { return m_next_token.token; }
Location peekLocation() const { return m_next_token.location; }
const std::string& peekLiteral() const { return m_next_token.literal; }
std::string const& peekLiteral() const { return m_next_token.literal; }
///@}
///@{

View File

@ -32,8 +32,10 @@ namespace dev
namespace solidity
{
/// Container that stores mappings betwee names and declarations. It also contains a link to the
/// enclosing scope.
/**
* Container that stores mappings betwee names and declarations. It also contains a link to the
* enclosing scope.
*/
class Scope
{
public:

View File

@ -143,7 +143,7 @@ bool IntegerType::acceptsUnaryOperator(Token::Value _operator) const
_operator == Token::INC || _operator == Token::DEC;
}
bool IntegerType::operator==(const Type& _other) const
bool IntegerType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;
@ -159,7 +159,7 @@ std::string IntegerType::toString() const
return prefix + dev::toString(m_bits);
}
u256 IntegerType::literalValue(const Literal& _literal) const
u256 IntegerType::literalValue(Literal const& _literal) const
{
bigint value(_literal.getValue());
//@todo check that the number is not too large
@ -180,7 +180,7 @@ bool BoolType::isExplicitlyConvertibleTo(Type const& _convertTo) const
return isImplicitlyConvertibleTo(_convertTo);
}
u256 BoolType::literalValue(const Literal& _literal) const
u256 BoolType::literalValue(Literal const& _literal) const
{
if (_literal.getToken() == Token::TRUE_LITERAL)
return u256(1);
@ -190,7 +190,7 @@ u256 BoolType::literalValue(const Literal& _literal) const
assert(false);
}
bool ContractType::operator==(const Type& _other) const
bool ContractType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;
@ -198,7 +198,7 @@ bool ContractType::operator==(const Type& _other) const
return other.m_contract == m_contract;
}
bool StructType::operator==(const Type& _other) const
bool StructType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;
@ -206,7 +206,7 @@ bool StructType::operator==(const Type& _other) const
return other.m_struct == m_struct;
}
bool FunctionType::operator==(const Type& _other) const
bool FunctionType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;
@ -214,7 +214,7 @@ bool FunctionType::operator==(const Type& _other) const
return other.m_function == m_function;
}
bool MappingType::operator==(const Type& _other) const
bool MappingType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;
@ -222,7 +222,7 @@ bool MappingType::operator==(const Type& _other) const
return *other.m_keyType == *m_keyType && *other.m_valueType == *m_valueType;
}
bool TypeType::operator==(const Type& _other) const
bool TypeType::operator==(Type const& _other) const
{
if (_other.getCategory() != getCategory())
return false;

40
Types.h
View File

@ -36,7 +36,9 @@ namespace solidity
// @todo realMxN, string<N>, mapping
/// Abstract base class that forms the root of the type hierarchy.
/**
* Abstract base class that forms the root of the type hierarchy.
*/
class Type: private boost::noncopyable
{
public:
@ -76,7 +78,9 @@ public:
virtual u256 literalValue(Literal const&) const { assert(false); }
};
/// Any kind of integer type including hash and address.
/**
* Any kind of integer type including hash and address.
*/
class IntegerType: public Type
{
public:
@ -112,7 +116,9 @@ private:
Modifier m_modifier;
};
/// The boolean type.
/**
* The boolean type.
*/
class BoolType: public Type
{
public:
@ -133,7 +139,9 @@ public:
virtual u256 literalValue(Literal const& _literal) const override;
};
/// The type of a contract instance, there is one distinct type for each contract definition.
/**
* The type of a contract instance, there is one distinct type for each contract definition.
*/
class ContractType: public Type
{
public:
@ -148,7 +156,9 @@ private:
ContractDefinition const& m_contract;
};
/// The type of a struct instance, there is one distinct type per struct definition.
/**
* The type of a struct instance, there is one distinct type per struct definition.
*/
class StructType: public Type
{
public:
@ -167,7 +177,9 @@ private:
StructDefinition const& m_struct;
};
/// The type of a function, there is one distinct type per function definition.
/**
* The type of a function, there is one distinct type per function definition.
*/
class FunctionType: public Type
{
public:
@ -184,7 +196,9 @@ private:
FunctionDefinition const& m_function;
};
/// The type of a mapping, there is one distinct type per key/value type pair.
/**
* The type of a mapping, there is one distinct type per key/value type pair.
*/
class MappingType: public Type
{
public:
@ -199,8 +213,10 @@ private:
std::shared_ptr<Type const> m_valueType;
};
/// The void type, can only be implicitly used as the type that is returned by functions without
/// return parameters.
/**
* The void type, can only be implicitly used as the type that is returned by functions without
* return parameters.
*/
class VoidType: public Type
{
public:
@ -210,8 +226,10 @@ public:
virtual std::string toString() const override { return "void"; }
};
/// The type of a type reference. The type of "uint32" when used in "a = uint32(2)" is an example
/// of a TypeType.
/**
* The type of a type reference. The type of "uint32" when used in "a = uint32(2)" is an example
* of a TypeType.
*/
class TypeType: public Type
{
public: