2014-10-07 16:25:04 +00:00
|
|
|
/*
|
2014-10-16 12:08:54 +00:00
|
|
|
This file is part of cpp-ethereum.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
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.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
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.
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-07 16:25:04 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Solidity abstract syntax tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2014-10-13 16:22:15 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
|
|
|
|
2014-10-10 14:37:54 +00:00
|
|
|
#include <libsolidity/ASTForward.h>
|
2014-10-07 16:25:04 +00:00
|
|
|
#include <libsolidity/BaseTypes.h>
|
|
|
|
#include <libsolidity/Token.h>
|
2014-10-13 16:22:15 +00:00
|
|
|
#include <libsolidity/Types.h>
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-10 14:37:54 +00:00
|
|
|
class ASTVisitor;
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class ASTNode: private boost::noncopyable
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-09 10:28:37 +00:00
|
|
|
explicit ASTNode(Location const& _location)
|
|
|
|
: m_location(_location)
|
|
|
|
{}
|
2014-10-08 18:53:50 +00:00
|
|
|
|
2014-10-09 13:57:49 +00:00
|
|
|
virtual ~ASTNode() {}
|
|
|
|
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) = 0;
|
|
|
|
template <class T>
|
2014-10-20 11:02:06 +00:00
|
|
|
static void listAccept(std::vector<ptr<T>>& _list, ASTVisitor& _visitor)
|
2014-10-16 12:08:54 +00:00
|
|
|
{
|
2014-10-16 21:49:45 +00:00
|
|
|
for (ptr<T>& element: _list)
|
2014-10-16 12:08:54 +00:00
|
|
|
element->accept(_visitor);
|
2014-10-10 14:37:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Location const& getLocation() const { return m_location; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
Location m_location;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Declaration: public ASTNode
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Declaration(Location const& _location, ptr<ASTString> const& _name)
|
|
|
|
: ASTNode(_location), m_name(_name) {}
|
|
|
|
|
|
|
|
const ASTString& getName() const { return *m_name; }
|
|
|
|
private:
|
|
|
|
ptr<ASTString> m_name;
|
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class ContractDefinition: public Declaration
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-09 10:28:37 +00:00
|
|
|
ContractDefinition(Location const& _location,
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> const& _name,
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<StructDefinition>> const& _definedStructs,
|
|
|
|
std::vector<ptr<VariableDeclaration>> const& _stateVariables,
|
|
|
|
std::vector<ptr<FunctionDefinition>> const& _definedFunctions)
|
2014-10-13 16:22:15 +00:00
|
|
|
: Declaration(_location, _name),
|
2014-10-09 10:28:37 +00:00
|
|
|
m_definedStructs(_definedStructs),
|
|
|
|
m_stateVariables(_stateVariables),
|
|
|
|
m_definedFunctions(_definedFunctions)
|
|
|
|
{}
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<StructDefinition>> const& getDefinedStructs() { return m_definedStructs; }
|
|
|
|
std::vector<ptr<VariableDeclaration>> const& getStateVariables() { return m_stateVariables; }
|
|
|
|
std::vector<ptr<FunctionDefinition>> const& getDefinedFunctions() { return m_definedFunctions; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<StructDefinition>> m_definedStructs;
|
|
|
|
std::vector<ptr<VariableDeclaration>> m_stateVariables;
|
|
|
|
std::vector<ptr<FunctionDefinition>> m_definedFunctions;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class StructDefinition: public Declaration
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-08 18:53:50 +00:00
|
|
|
public:
|
2014-10-09 10:28:37 +00:00
|
|
|
StructDefinition(Location const& _location,
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> const& _name,
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<VariableDeclaration>> const& _members)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Declaration(_location, _name), m_members(_members) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<VariableDeclaration>> m_members;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-08 18:53:50 +00:00
|
|
|
/// Used as function parameter list and return list
|
|
|
|
/// None of the parameters is allowed to contain mappings (not even recursively
|
|
|
|
/// inside structs)
|
2014-10-16 21:49:45 +00:00
|
|
|
class ParameterList: public ASTNode
|
2014-10-08 18:53:50 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-20 11:02:06 +00:00
|
|
|
ParameterList(Location const& _location, std::vector<ptr<VariableDeclaration>> const& _parameters)
|
2014-10-16 21:49:45 +00:00
|
|
|
: ASTNode(_location), m_parameters(_parameters) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 13:07:21 +00:00
|
|
|
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<VariableDeclaration>> const& getParameters() { return m_parameters; }
|
2014-10-08 18:53:50 +00:00
|
|
|
private:
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<VariableDeclaration>> m_parameters;
|
2014-10-08 18:53:50 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class FunctionDefinition: public Declaration
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-08 18:53:50 +00:00
|
|
|
public:
|
2014-10-10 14:37:54 +00:00
|
|
|
FunctionDefinition(Location const& _location, ptr<ASTString> const& _name, bool _isPublic,
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<ParameterList> const& _parameters,
|
|
|
|
bool _isDeclaredConst,
|
|
|
|
ptr<ParameterList> const& _returnParameters,
|
|
|
|
ptr<Block> const& _body)
|
2014-10-13 16:22:15 +00:00
|
|
|
: Declaration(_location, _name), m_isPublic(_isPublic), m_parameters(_parameters),
|
2014-10-09 10:28:37 +00:00
|
|
|
m_isDeclaredConst(_isDeclaredConst), m_returnParameters(_returnParameters),
|
2014-10-16 21:49:45 +00:00
|
|
|
m_body(_body) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
|
|
|
|
bool isPublic() const { return m_isPublic; }
|
|
|
|
bool isDeclaredConst() const { return m_isDeclaredConst; }
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<VariableDeclaration>> const& getParameters() const { return m_parameters->getParameters(); }
|
2014-10-13 16:22:15 +00:00
|
|
|
ParameterList& getParameterList() { return *m_parameters; }
|
|
|
|
ptr<ParameterList> const& getReturnParameterList() const { return m_returnParameters; }
|
2014-10-13 13:07:21 +00:00
|
|
|
Block& getBody() { return *m_body; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
bool m_isPublic;
|
|
|
|
ptr<ParameterList> m_parameters;
|
|
|
|
bool m_isDeclaredConst;
|
2014-10-13 16:22:15 +00:00
|
|
|
ptr<ParameterList> m_returnParameters;
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<Block> m_body;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class VariableDeclaration: public Declaration
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
VariableDeclaration(Location const& _location, ptr<TypeName> const& _type,
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> const& _name)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Declaration(_location, _name), m_typeName(_type) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
|
2014-10-13 16:22:15 +00:00
|
|
|
bool isTypeGivenExplicitly() const { return m_typeName.get() != nullptr; }
|
|
|
|
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.
|
|
|
|
ptr<Type> const& getType() const { return m_type; }
|
|
|
|
void setType(ptr<Type> const& _type) { m_type = _type; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-13 16:22:15 +00:00
|
|
|
ptr<TypeName> m_typeName; ///< can be empty ("var")
|
|
|
|
|
|
|
|
ptr<Type> m_type;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// types
|
|
|
|
/// @{
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class TypeName: public ASTNode
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
explicit TypeName(Location const& _location): ASTNode(_location) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
|
|
|
virtual ptr<Type> toType() = 0;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// any pre-defined type that is not a mapping
|
2014-10-16 21:49:45 +00:00
|
|
|
class ElementaryTypeName: public TypeName
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-09 10:28:37 +00:00
|
|
|
explicit ElementaryTypeName(Location const& _location, Token::Value _type)
|
2014-10-16 21:49:45 +00:00
|
|
|
: TypeName(_location), m_type(_type) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> toType() override { return Type::fromElementaryTypeName(m_type); }
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
Token::Value getType() const { return m_type; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
Token::Value m_type;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class UserDefinedTypeName: public TypeName
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-10 14:37:54 +00:00
|
|
|
UserDefinedTypeName(Location const& _location, ptr<ASTString> const& _name)
|
2014-10-16 21:49:45 +00:00
|
|
|
: TypeName(_location), m_name(_name) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> toType() override { return Type::fromUserDefinedTypeName(*this); }
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
const ASTString& getName() const { return *m_name; }
|
2014-10-13 16:22:15 +00:00
|
|
|
void setReferencedStruct(StructDefinition& _referencedStruct) { m_referencedStruct = &_referencedStruct; }
|
|
|
|
StructDefinition const* getReferencedStruct() const { return m_referencedStruct; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> m_name;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
|
|
|
StructDefinition* m_referencedStruct;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Mapping: public TypeName
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-09 10:28:37 +00:00
|
|
|
Mapping(Location const& _location, ptr<ElementaryTypeName> const& _keyType,
|
|
|
|
ptr<TypeName> const& _valueType)
|
2014-10-16 21:49:45 +00:00
|
|
|
: TypeName(_location), m_keyType(_keyType), m_valueType(_valueType) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> toType() override { return Type::fromMapping(*this); }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<ElementaryTypeName> m_keyType;
|
|
|
|
ptr<TypeName> m_valueType;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// Statements
|
|
|
|
/// @{
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Statement: public ASTNode
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-08 18:53:50 +00:00
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
explicit Statement(Location const& _location): ASTNode(_location) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
|
|
|
//! 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
|
|
|
|
//! statements, returns the empty pointer.
|
|
|
|
virtual ptr<Type> checkTypeRequirements() = 0;
|
|
|
|
protected:
|
|
|
|
//! Check that the inferred type for _expression is _expectedType or at least implicitly
|
|
|
|
//! convertible to _expectedType. If not, throw exception.
|
|
|
|
void expectType(Expression& _expression, Type const& _expectedType);
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Block: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-08 18:53:50 +00:00
|
|
|
public:
|
2014-10-20 11:02:06 +00:00
|
|
|
Block(Location const& _location, std::vector<ptr<Statement>> const& _statements)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Statement(_location), m_statements(_statements) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<Statement>> m_statements;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class IfStatement: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
IfStatement(Location const& _location, ptr<Expression> const& _condition,
|
|
|
|
ptr<Statement> const& _trueBody, ptr<Statement> const& _falseBody)
|
|
|
|
: Statement(_location), m_condition(_condition),
|
2014-10-16 21:49:45 +00:00
|
|
|
m_trueBody(_trueBody), m_falseBody(_falseBody) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<Expression> m_condition;
|
|
|
|
ptr<Statement> m_trueBody;
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<Statement> m_falseBody; //< "else" part, optional
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class BreakableStatement: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
BreakableStatement(Location const& _location): Statement(_location) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class WhileStatement: public BreakableStatement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
WhileStatement(Location const& _location, ptr<Expression> const& _condition,
|
|
|
|
ptr<Statement> const& _body)
|
2014-10-16 21:49:45 +00:00
|
|
|
: BreakableStatement(_location), m_condition(_condition), m_body(_body) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<Expression> m_condition;
|
|
|
|
ptr<Statement> m_body;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Continue: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
Continue(Location const& _location): Statement(_location) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Break: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
Break(Location const& _location): Statement(_location) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Return: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
Return(Location const& _location, ptr<Expression> _expression)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Statement(_location), m_expression(_expression) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
|
|
|
|
|
|
|
void setFunctionReturnParameters(ParameterList& _parameters) { m_returnParameters = &_parameters; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<Expression> m_expression; //< value to return, optional
|
2014-10-13 16:22:15 +00:00
|
|
|
|
|
|
|
ParameterList* m_returnParameters; //< extracted from the function declaration
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class VariableDefinition: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
VariableDefinition(Location const& _location, ptr<VariableDeclaration> _variable,
|
|
|
|
ptr<Expression> _value)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Statement(_location), m_variable(_variable), m_value(_value) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<VariableDeclaration> m_variable;
|
2014-10-09 13:57:49 +00:00
|
|
|
ptr<Expression> m_value; ///< can be missing
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Expression: public Statement
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
Expression(Location const& _location): Statement(_location) {}
|
2014-10-13 16:22:15 +00:00
|
|
|
ptr<Type> const& getType() { return m_type; }
|
|
|
|
protected:
|
|
|
|
ptr<Type> m_type;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// Expressions
|
|
|
|
/// @{
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Assignment: public Expression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
Assignment(Location const& _location, ptr<Expression> const& _leftHandSide,
|
|
|
|
Token::Value _assignmentOperator, ptr<Expression> const& _rightHandSide)
|
|
|
|
: Expression(_location), m_leftHandSide(_leftHandSide),
|
2014-10-16 21:49:45 +00:00
|
|
|
m_assigmentOperator(_assignmentOperator), m_rightHandSide(_rightHandSide) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
Token::Value getAssignmentOperator() const { return m_assigmentOperator; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<Expression> m_leftHandSide;
|
|
|
|
Token::Value m_assigmentOperator;
|
|
|
|
ptr<Expression> m_rightHandSide;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class UnaryOperation: public Expression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
UnaryOperation(Location const& _location, Token::Value _operator,
|
|
|
|
ptr<Expression> const& _subExpression, bool _isPrefix)
|
|
|
|
: Expression(_location), m_operator(_operator),
|
2014-10-16 21:49:45 +00:00
|
|
|
m_subExpression(_subExpression), m_isPrefix(_isPrefix) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-09 13:57:49 +00:00
|
|
|
|
2014-10-10 14:37:54 +00:00
|
|
|
Token::Value getOperator() const { return m_operator; }
|
|
|
|
bool isPrefixOperation() const { return m_isPrefix; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
Token::Value m_operator;
|
|
|
|
ptr<Expression> m_subExpression;
|
2014-10-09 13:57:49 +00:00
|
|
|
bool m_isPrefix;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class BinaryOperation: public Expression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
BinaryOperation(Location const& _location, ptr<Expression> const& _left,
|
|
|
|
Token::Value _operator, ptr<Expression> const& _right)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Expression(_location), m_left(_left), m_operator(_operator), m_right(_right) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
Token::Value getOperator() const { return m_operator; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<Expression> m_left;
|
|
|
|
Token::Value m_operator;
|
2014-10-09 13:57:49 +00:00
|
|
|
ptr<Expression> m_right;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
|
|
|
ptr<Type> m_commonType;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-08 18:53:50 +00:00
|
|
|
/// Can be ordinary function call, type cast or struct construction.
|
2014-10-16 21:49:45 +00:00
|
|
|
class FunctionCall: public Expression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
FunctionCall(Location const& _location, ptr<Expression> const& _expression,
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<Expression>> const& _arguments)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Expression(_location), m_expression(_expression), m_arguments(_arguments) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 13:57:49 +00:00
|
|
|
ptr<Expression> m_expression;
|
2014-10-20 11:02:06 +00:00
|
|
|
std::vector<ptr<Expression>> m_arguments;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class MemberAccess: public Expression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
MemberAccess(Location const& _location, ptr<Expression> _expression,
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> const& _memberName)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Expression(_location), m_expression(_expression), m_memberName(_memberName) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
const ASTString& getMemberName() const { return *m_memberName; }
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<Expression> m_expression;
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> m_memberName;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class IndexAccess: public Expression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
|
|
|
IndexAccess(Location const& _location, ptr<Expression> const& _base,
|
|
|
|
ptr<Expression> const& _index)
|
2014-10-16 21:49:45 +00:00
|
|
|
: Expression(_location), m_base(_base), m_index(_index) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-09 13:57:49 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
ptr<Expression> m_base;
|
|
|
|
ptr<Expression> m_index;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class PrimaryExpression: public Expression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2014-10-16 21:49:45 +00:00
|
|
|
PrimaryExpression(Location const& _location): Expression(_location) {}
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Identifier: public PrimaryExpression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2014-10-10 14:37:54 +00:00
|
|
|
Identifier(Location const& _location, ptr<ASTString> const& _name)
|
2014-10-09 13:57:49 +00:00
|
|
|
: PrimaryExpression(_location), m_name(_name) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
ASTString const& getName() const { return *m_name; }
|
2014-10-13 16:22:15 +00:00
|
|
|
void setReferencedDeclaration(Declaration& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; }
|
|
|
|
Declaration* getReferencedDeclaration() { return m_referencedDeclaration; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> m_name;
|
2014-10-13 13:07:21 +00:00
|
|
|
|
2014-10-13 16:22:15 +00:00
|
|
|
//! Declaration the name refers to.
|
|
|
|
Declaration* m_referencedDeclaration;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class ElementaryTypeNameExpression: public PrimaryExpression
|
2014-10-09 13:57:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-13 16:22:15 +00:00
|
|
|
ElementaryTypeNameExpression(Location const& _location, Token::Value _typeToken)
|
|
|
|
: PrimaryExpression(_location), m_typeToken(_typeToken) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-10-13 16:22:15 +00:00
|
|
|
Token::Value getTypeToken() const { return m_typeToken; }
|
2014-10-09 13:57:49 +00:00
|
|
|
private:
|
2014-10-13 16:22:15 +00:00
|
|
|
Token::Value m_typeToken;
|
2014-10-09 13:57:49 +00:00
|
|
|
};
|
|
|
|
|
2014-10-16 21:49:45 +00:00
|
|
|
class Literal: public PrimaryExpression
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2014-10-10 14:37:54 +00:00
|
|
|
Literal(Location const& _location, Token::Value _token, ptr<ASTString> const& _value)
|
2014-10-16 21:49:45 +00:00
|
|
|
: PrimaryExpression(_location), m_token(_token), m_value(_value) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-10-13 16:22:15 +00:00
|
|
|
virtual ptr<Type> checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
Token::Value getToken() const { return m_token; }
|
|
|
|
ASTString const& getValue() const { return *m_value; }
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 13:57:49 +00:00
|
|
|
Token::Value m_token;
|
2014-10-10 14:37:54 +00:00
|
|
|
ptr<ASTString> m_value;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
|
|
|
}
|