Browse Source

Documentation for AST, Parser, Scanner and other classes.

cl-refactor
Christian 10 years ago
parent
commit
2c5b1c5262
  1. 76
      libsolidity/AST.h
  2. 2
      libsolidity/ASTPrinter.cpp
  3. 4
      libsolidity/Compiler.h
  4. 13
      libsolidity/Parser.h
  5. 34
      libsolidity/Scanner.h

76
libsolidity/AST.h

@ -40,6 +40,10 @@ namespace solidity
class ASTVisitor; 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.
class ASTNode: private boost::noncopyable class ASTNode: private boost::noncopyable
{ {
public: public:
@ -55,6 +59,7 @@ public:
element->accept(_visitor); element->accept(_visitor);
} }
/// Returns the source code location of this node.
Location const& getLocation() const { return m_location; } Location const& getLocation() const { return m_location; }
/// Creates a @ref TypeError exception and decorates it with the location of the node and /// Creates a @ref TypeError exception and decorates it with the location of the node and
@ -62,6 +67,7 @@ public:
TypeError createTypeError(std::string const& _description); TypeError createTypeError(std::string const& _description);
///@{ ///@{
///@name equality operators
/// Equality relies on the fact that nodes cannot be copied. /// Equality relies on the fact that nodes cannot be copied.
bool operator==(ASTNode const& _other) const { return this == &_other; } bool operator==(ASTNode const& _other) const { return this == &_other; }
bool operator!=(ASTNode const& _other) const { return !operator==(_other); } bool operator!=(ASTNode const& _other) const { return !operator==(_other); }
@ -71,18 +77,23 @@ private:
Location m_location; Location m_location;
}; };
/// Abstract AST class for a declaration (contract, function, struct, variable).
class Declaration: public ASTNode class Declaration: public ASTNode
{ {
public: public:
Declaration(Location const& _location, ASTPointer<ASTString> const& _name): Declaration(Location const& _location, ASTPointer<ASTString> const& _name):
ASTNode(_location), m_name(_name) {} ASTNode(_location), m_name(_name) {}
/// Returns the declared name.
const ASTString& getName() const { return *m_name; } const ASTString& getName() const { return *m_name; }
private: private:
ASTPointer<ASTString> m_name; 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.
class ContractDefinition: public Declaration class ContractDefinition: public Declaration
{ {
public: public:
@ -122,9 +133,9 @@ private:
std::vector<ASTPointer<VariableDeclaration>> m_members; std::vector<ASTPointer<VariableDeclaration>> m_members;
}; };
/// Used as function parameter list and return list /// Parameter list, used as function parameter list and return list.
/// None of the parameters is allowed to contain mappings (not even recursively /// None of the parameters is allowed to contain mappings (not even recursively
/// inside structs) /// inside structs), but (@todo) this is not yet enforced.
class ParameterList: public ASTNode class ParameterList: public ASTNode
{ {
public: public:
@ -167,6 +178,8 @@ private:
ASTPointer<Block> m_body; ASTPointer<Block> m_body;
}; };
/// 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 class VariableDeclaration: public Declaration
{ {
public: public:
@ -186,22 +199,26 @@ public:
private: private:
ASTPointer<TypeName> m_typeName; ///< can be empty ("var") ASTPointer<TypeName> m_typeName; ///< can be empty ("var")
std::shared_ptr<Type const> m_type; std::shared_ptr<Type const> m_type; ///< derived type, initially empty
}; };
/// types /// Types
/// @{ /// @{
/// Abstract base class of a type name, can be any built-in or user-defined type.
class TypeName: public ASTNode class TypeName: public ASTNode
{ {
public: public:
explicit TypeName(Location const& _location): ASTNode(_location) {} explicit TypeName(Location const& _location): ASTNode(_location) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
/// Retrieve the element of the type hierarchy this node refers to. Can return an empty shared
/// pointer until the types have been resolved using the @ref NameAndTypeResolver.
virtual std::shared_ptr<Type> toType() = 0; virtual std::shared_ptr<Type> toType() = 0;
}; };
/// any pre-defined type that is not a mapping /// Any pre-defined type name represented by a single keyword, i.e. it excludes mappings,
/// contracts, functions, etc.
class ElementaryTypeName: public TypeName class ElementaryTypeName: public TypeName
{ {
public: public:
@ -210,12 +227,14 @@ public:
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual std::shared_ptr<Type> toType() override { return Type::fromElementaryTypeName(m_type); } virtual std::shared_ptr<Type> toType() override { return Type::fromElementaryTypeName(m_type); }
Token::Value getType() const { return m_type; } Token::Value getTypeName() const { return m_type; }
private: private:
Token::Value m_type; 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
class UserDefinedTypeName: public TypeName class UserDefinedTypeName: public TypeName
{ {
public: public:
@ -234,6 +253,7 @@ private:
StructDefinition* m_referencedStruct; StructDefinition* m_referencedStruct;
}; };
/// A mapping type. Its source form is "mapping('keyType' => 'valueType')"
class Mapping: public TypeName class Mapping: public TypeName
{ {
public: public:
@ -253,6 +273,8 @@ private:
/// Statements /// Statements
/// @{ /// @{
/// Abstract base class for statements.
class Statement: public ASTNode class Statement: public ASTNode
{ {
public: public:
@ -260,16 +282,17 @@ public:
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
//! Check all type requirements, throws exception if some requirement is not met. //! Check all type requirements, throws exception if some requirement is not met.
//! For expressions, this also returns the inferred type of the expression. For other //! This includes checking that operators are applicable to their arguments but also that
//! statements, returns the empty pointer. //! the number of function call arguments matches the number of formal parameters and so forth.
virtual void checkTypeRequirements() = 0; virtual void checkTypeRequirements() = 0;
protected: protected:
//! Check that the inferred type for _expression is _expectedType or at least implicitly //! Helper function, check that the inferred type for @a _expression is @a _expectedType or at
//! convertible to _expectedType. If not, throw exception. //! least implicitly convertible to @a _expectedType. If not, throw exception.
void expectType(Expression& _expression, Type const& _expectedType); void expectType(Expression& _expression, Type const& _expectedType);
}; };
/// Brace-enclosed block containing zero or more statements.
class Block: public Statement class Block: public Statement
{ {
public: public:
@ -283,6 +306,8 @@ private:
std::vector<ASTPointer<Statement>> m_statements; 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.
class IfStatement: public Statement class IfStatement: public Statement
{ {
public: public:
@ -299,6 +324,8 @@ private:
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.
class BreakableStatement: public Statement class BreakableStatement: public Statement
{ {
public: public:
@ -349,9 +376,13 @@ public:
private: private:
ASTPointer<Expression> m_expression; //< value to return, optional ASTPointer<Expression> m_expression; //< value to return, optional
ParameterList* m_returnParameters; //< extracted from the function declaration /// 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;
class VariableDefinition: public Statement class VariableDefinition: public Statement
{ {
public: public:
@ -363,13 +394,16 @@ public:
private: private:
ASTPointer<VariableDeclaration> m_variable; ASTPointer<VariableDeclaration> m_variable;
ASTPointer<Expression> m_value; ///< can be missing ASTPointer<Expression> m_value; ///< the assigned value, can be missing
}; };
/// An expression, i.e. something that has a value (which can also be of type "void" in case
/// of function calls).
class Expression: public Statement class Expression: public Statement
{ {
public: public:
Expression(Location const& _location): Statement(_location) {} Expression(Location const& _location): Statement(_location) {}
std::shared_ptr<Type const> const& getType() const { return m_type; } std::shared_ptr<Type const> const& getType() const { return m_type; }
protected: protected:
@ -382,6 +416,8 @@ protected:
/// Expressions /// Expressions
/// @{ /// @{
/// Assignment, can also be a compound assignment.
/// Examples: (a = 7 + 8) or (a *= 2)
class Assignment: public Expression class Assignment: public Expression
{ {
public: public:
@ -402,6 +438,8 @@ private:
ASTPointer<Expression> m_rightHandSide; ASTPointer<Expression> m_rightHandSide;
}; };
/// Operation involving a unary operator, pre- or postfix.
/// Examples: ++i, delete x or !true
class UnaryOperation: public Expression class UnaryOperation: public Expression
{ {
public: public:
@ -421,6 +459,8 @@ private:
bool m_isPrefix; bool m_isPrefix;
}; };
/// Operation involving a binary operator.
/// Examples: 1 + 2, true && false or 1 <= 4
class BinaryOperation: public Expression class BinaryOperation: public Expression
{ {
public: public:
@ -451,6 +491,7 @@ public:
Expression(_location), m_expression(_expression), m_arguments(_arguments) {} Expression(_location), m_expression(_expression), m_arguments(_arguments) {}
virtual void accept(ASTVisitor& _visitor) override; virtual void accept(ASTVisitor& _visitor) override;
virtual void checkTypeRequirements() override; virtual void checkTypeRequirements() override;
/// Returns true if this is not an actual function call, but an explicit type conversion /// Returns true if this is not an actual function call, but an explicit type conversion
/// or constructor call. /// or constructor call.
bool isTypeConversion() const; bool isTypeConversion() const;
@ -460,6 +501,7 @@ private:
std::vector<ASTPointer<Expression>> m_arguments; std::vector<ASTPointer<Expression>> m_arguments;
}; };
/// Access to a member of an object. Example: x.name
class MemberAccess: public Expression class MemberAccess: public Expression
{ {
public: public:
@ -475,6 +517,7 @@ private:
ASTPointer<ASTString> m_memberName; ASTPointer<ASTString> m_memberName;
}; };
/// Index access to an array. Example: a[2]
class IndexAccess: public Expression class IndexAccess: public Expression
{ {
public: public:
@ -489,12 +532,15 @@ private:
ASTPointer<Expression> m_index; ASTPointer<Expression> m_index;
}; };
/// Primary expression, i.e. an expression that do not be divided any further like a literal or
/// a variable reference.
class PrimaryExpression: public Expression class PrimaryExpression: public Expression
{ {
public: public:
PrimaryExpression(Location const& _location): Expression(_location) {} PrimaryExpression(Location const& _location): Expression(_location) {}
}; };
/// An identifier, i.e. a reference to a declaration by name like a variable or function.
class Identifier: public PrimaryExpression class Identifier: public PrimaryExpression
{ {
public: public:
@ -504,6 +550,7 @@ public:
virtual void checkTypeRequirements() override; virtual void checkTypeRequirements() override;
ASTString const& getName() const { return *m_name; } ASTString const& getName() const { return *m_name; }
void setReferencedDeclaration(Declaration& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; } void setReferencedDeclaration(Declaration& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; }
Declaration* getReferencedDeclaration() { return m_referencedDeclaration; } Declaration* getReferencedDeclaration() { return m_referencedDeclaration; }
@ -514,6 +561,9 @@ private:
Declaration* m_referencedDeclaration; 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.
class ElementaryTypeNameExpression: public PrimaryExpression class ElementaryTypeNameExpression: public PrimaryExpression
{ {
public: public:
@ -528,6 +578,7 @@ private:
Token::Value m_typeToken; Token::Value m_typeToken;
}; };
/// A literal string or number. @see Type::literalToBigEndian is used to actually parse its value.
class Literal: public PrimaryExpression class Literal: public PrimaryExpression
{ {
public: public:
@ -537,6 +588,7 @@ public:
virtual void checkTypeRequirements() override; virtual void checkTypeRequirements() override;
Token::Value getToken() const { return m_token; } Token::Value getToken() const { return m_token; }
/// @returns the non-parsed value of the literal
ASTString const& getValue() const { return *m_value; } ASTString const& getValue() const { return *m_value; }
private: private:

2
libsolidity/ASTPrinter.cpp

@ -87,7 +87,7 @@ bool ASTPrinter::visit(TypeName& _node)
bool ASTPrinter::visit(ElementaryTypeName& _node) bool ASTPrinter::visit(ElementaryTypeName& _node)
{ {
writeLine(std::string("ElementaryTypeName ") + Token::toString(_node.getType())); writeLine(std::string("ElementaryTypeName ") + Token::toString(_node.getTypeName()));
printSourcePart(_node); printSourcePart(_node);
return goDeeper(); return goDeeper();
} }

4
libsolidity/Compiler.h

@ -110,8 +110,8 @@ private:
/// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type. /// Appends code to remove dirty higher order bits in case of an implicit promotion to a wider type.
void cleanHigherOrderBitsIfNeeded(Type const& _typeOnStack, Type const& _targetType); void cleanHigherOrderBitsIfNeeded(Type const& _typeOnStack, Type const& _targetType);
/// Append code for various operator types ///@{
/// @{ ///@name Append code for various operator types
void appendAndOrOperatorCode(BinaryOperation& _binaryOperation); void appendAndOrOperatorCode(BinaryOperation& _binaryOperation);
void appendCompareOperatorCode(Token::Value _operator, Type const& _type); void appendCompareOperatorCode(Token::Value _operator, Type const& _type);
void appendOrdinaryBinaryOperatorCode(Token::Value _operator, Type const& _type); void appendOrdinaryBinaryOperatorCode(Token::Value _operator, Type const& _type);

13
libsolidity/Parser.h

@ -44,8 +44,8 @@ private:
/// End position of the current token /// End position of the current token
int getEndPosition() const; int getEndPosition() const;
/// Parsing functions for the AST nodes ///@{
/// @{ ///@name Parsing functions for the AST nodes
ASTPointer<ContractDefinition> parseContractDefinition(); ASTPointer<ContractDefinition> parseContractDefinition();
ASTPointer<FunctionDefinition> parseFunctionDefinition(bool _isPublic); ASTPointer<FunctionDefinition> parseFunctionDefinition(bool _isPublic);
ASTPointer<StructDefinition> parseStructDefinition(); ASTPointer<StructDefinition> parseStructDefinition();
@ -64,16 +64,17 @@ private:
ASTPointer<Expression> parseLeftHandSideExpression(); ASTPointer<Expression> parseLeftHandSideExpression();
ASTPointer<Expression> parsePrimaryExpression(); ASTPointer<Expression> parsePrimaryExpression();
std::vector<ASTPointer<Expression>> parseFunctionCallArguments(); std::vector<ASTPointer<Expression>> parseFunctionCallArguments();
/// @} ///@}
///@{
///@name Helper functions
/// Helper functions
/// @{
/// 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();
ASTPointer<ASTString> expectIdentifierToken(); ASTPointer<ASTString> expectIdentifierToken();
ASTPointer<ASTString> getLiteralAndAdvance(); ASTPointer<ASTString> getLiteralAndAdvance();
/// @} ///@}
/// Creates a @ref ParserError exception and annotates it with the current position and the /// Creates a @ref ParserError exception and annotates it with the current position and the
/// given @a _description. /// given @a _description.

34
libsolidity/Scanner.h

@ -81,12 +81,13 @@ public:
char advanceAndGet(); char advanceAndGet();
char rollback(size_t _amount); char rollback(size_t _amount);
///@{
///@name Error printing helper functions
/// Functions that help pretty-printing parse errors /// Functions that help pretty-printing parse errors
/// Do only use in error cases, they are quite expensive. /// Do only use in error cases, they are quite expensive.
/// @{
std::string getLineAtPosition(int _position) const; std::string getLineAtPosition(int _position) const;
std::tuple<int, int> translatePositionToLineColumn(int _position) const; std::tuple<int, int> translatePositionToLineColumn(int _position) const;
/// @} ///@}
private: private:
std::string m_source; std::string m_source;
@ -119,29 +120,31 @@ public:
/// Returns the next token and advances input. /// Returns the next token and advances input.
Token::Value next(); Token::Value next();
/// Information about the current token ///@{
/// @{ ///@name Information about the current token
/// Returns the current token /// Returns the current token
Token::Value getCurrentToken() { return m_current_token.token; } Token::Value getCurrentToken() { return m_current_token.token; }
Location getCurrentLocation() const { return m_current_token.location; } Location getCurrentLocation() const { return m_current_token.location; }
const std::string& getCurrentLiteral() const { return m_current_token.literal; } const std::string& getCurrentLiteral() const { return m_current_token.literal; }
/// @} ///@}
///@{
///@name Information about the next token
/// Information about the next token
/// @{
/// Returns the next token without advancing input. /// Returns the next token without advancing input.
Token::Value peekNextToken() const { return m_next_token.token; } Token::Value peekNextToken() const { return m_next_token.token; }
Location peekLocation() const { return m_next_token.location; } Location peekLocation() const { return m_next_token.location; }
const std::string& peekLiteral() const { return m_next_token.literal; } const std::string& peekLiteral() const { return m_next_token.literal; }
/// @} ///@}
/// Functions that help pretty-printing parse errors. ///@{
///@name Error printing helper functions
/// Functions that help pretty-printing parse errors
/// Do only use in error cases, they are quite expensive. /// Do only use in error cases, they are quite expensive.
/// @{
std::string getLineAtPosition(int _position) const { return m_source.getLineAtPosition(_position); } std::string getLineAtPosition(int _position) const { return m_source.getLineAtPosition(_position); }
std::tuple<int, int> translatePositionToLineColumn(int _position) const { return m_source.translatePositionToLineColumn(_position); } std::tuple<int, int> translatePositionToLineColumn(int _position) const { return m_source.translatePositionToLineColumn(_position); }
/// @} ///@}
private: private:
// Used for the current and look-ahead token. // Used for the current and look-ahead token.
@ -152,13 +155,13 @@ private:
std::string literal; std::string literal;
}; };
/// Literal buffer support ///@{
/// @{ ///@name Literal buffer support
inline void startNewLiteral() { m_next_token.literal.clear(); } inline void startNewLiteral() { m_next_token.literal.clear(); }
inline void addLiteralChar(char c) { m_next_token.literal.push_back(c); } inline void addLiteralChar(char c) { m_next_token.literal.push_back(c); }
inline void dropLiteral() { m_next_token.literal.clear(); } inline void dropLiteral() { m_next_token.literal.clear(); }
inline void addLiteralCharAndAdvance() { addLiteralChar(m_char); advance(); } inline void addLiteralCharAndAdvance() { addLiteralChar(m_char); advance(); }
/// @} ///@}
bool advance() { m_char = m_source.advanceAndGet(); return !m_source.isPastEndOfInput(); } bool advance() { m_char = m_source.advanceAndGet(); return !m_source.isPastEndOfInput(); }
void rollback(int _amount) { m_char = m_source.rollback(_amount); } void rollback(int _amount) { m_char = m_source.rollback(_amount); }
@ -169,9 +172,10 @@ private:
bool scanHexNumber(char& o_scannedNumber, int _expectedLength); bool scanHexNumber(char& o_scannedNumber, int _expectedLength);
// Scans a single JavaScript token. /// Scans a single JavaScript token.
void scanToken(); void scanToken();
/// Skips all whitespace and @returns true if something was skipped.
bool skipWhitespace(); bool skipWhitespace();
Token::Value skipSingleLineComment(); Token::Value skipSingleLineComment();
Token::Value skipMultiLineComment(); Token::Value skipMultiLineComment();

Loading…
Cancel
Save