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
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
2014-10-23 17:22:30 +00:00
|
|
|
#include <boost/noncopyable.hpp>
|
2014-12-17 15:23:18 +00:00
|
|
|
#include <libsolidity/Utils.h>
|
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-23 17:22:30 +00:00
|
|
|
#include <libsolidity/Exceptions.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-12-06 01:19:10 +00:00
|
|
|
class ASTConstVisitor;
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-10-16 21:49:45 +00:00
|
|
|
class ASTNode: private boost::noncopyable
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-22 22:24:07 +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;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const = 0;
|
2014-10-10 14:37:54 +00:00
|
|
|
template <class T>
|
2014-10-20 12:00:37 +00:00
|
|
|
static void listAccept(std::vector<ASTPointer<T>>& _list, ASTVisitor& _visitor)
|
2014-10-16 12:08:54 +00:00
|
|
|
{
|
2014-10-20 12:00:37 +00:00
|
|
|
for (ASTPointer<T>& element: _list)
|
2014-10-16 12:08:54 +00:00
|
|
|
element->accept(_visitor);
|
2014-10-10 14:37:54 +00:00
|
|
|
}
|
2014-12-05 16:29:20 +00:00
|
|
|
template <class T>
|
2014-12-06 01:19:10 +00:00
|
|
|
static void listAccept(std::vector<ASTPointer<T>> const& _list, ASTConstVisitor& _visitor)
|
2014-12-05 16:29:20 +00:00
|
|
|
{
|
2014-12-06 01:19:10 +00:00
|
|
|
for (ASTPointer<T> const& element: _list)
|
2014-12-05 16:29:20 +00:00
|
|
|
element->accept(_visitor);
|
|
|
|
}
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
/// Returns the source code location of this node.
|
2014-10-10 14:37:54 +00:00
|
|
|
Location const& getLocation() const { return m_location; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-24 10:42:44 +00:00
|
|
|
/// Creates a @ref TypeError exception and decorates it with the location of the node and
|
2014-10-23 17:22:30 +00:00
|
|
|
/// the given description
|
2014-11-20 17:33:23 +00:00
|
|
|
TypeError createTypeError(std::string const& _description) const;
|
2014-10-23 17:22:30 +00:00
|
|
|
|
2014-10-20 10:41:56 +00:00
|
|
|
///@{
|
2014-10-22 18:35:35 +00:00
|
|
|
///@name equality operators
|
2014-10-20 10:41:56 +00:00
|
|
|
/// 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 !operator==(_other); }
|
|
|
|
///@}
|
|
|
|
|
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-12-03 06:46:55 +00:00
|
|
|
/**
|
|
|
|
* Source unit containing import directives and contract definitions.
|
|
|
|
*/
|
|
|
|
class SourceUnit: public ASTNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SourceUnit(Location const& _location, std::vector<ASTPointer<ASTNode>> const& _nodes):
|
|
|
|
ASTNode(_location), m_nodes(_nodes) {}
|
|
|
|
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-12-03 06:46:55 +00:00
|
|
|
|
|
|
|
std::vector<ASTPointer<ASTNode>> getNodes() const { return m_nodes; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<ASTPointer<ASTNode>> m_nodes;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import directive for referencing other files / source objects.
|
2014-12-05 14:35:05 +00:00
|
|
|
* Example: import "abc.sol"
|
|
|
|
* Source objects are identified by a string which can be a file name but does not have to be.
|
2014-12-03 06:46:55 +00:00
|
|
|
*/
|
|
|
|
class ImportDirective: public ASTNode
|
|
|
|
{
|
|
|
|
public:
|
2014-12-05 14:35:05 +00:00
|
|
|
ImportDirective(Location const& _location, ASTPointer<ASTString> const& _identifier):
|
|
|
|
ASTNode(_location), m_identifier(_identifier) {}
|
2014-12-03 06:46:55 +00:00
|
|
|
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-12-03 06:46:55 +00:00
|
|
|
|
2014-12-05 14:35:05 +00:00
|
|
|
ASTString const& getIdentifier() const { return *m_identifier; }
|
2014-12-03 06:46:55 +00:00
|
|
|
|
|
|
|
private:
|
2014-12-05 14:35:05 +00:00
|
|
|
ASTPointer<ASTString> m_identifier;
|
2014-12-03 06:46:55 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Abstract AST class for a declaration (contract, function, struct, variable).
|
|
|
|
*/
|
2014-10-16 21:49:45 +00:00
|
|
|
class Declaration: public ASTNode
|
2014-10-13 16:22:15 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-01-22 00:02:38 +00:00
|
|
|
enum class LValueType { NONE, LOCAL, STORAGE };
|
2015-02-02 16:24:09 +00:00
|
|
|
enum class Visibility { DEFAULT, PUBLIC, PROTECTED, PRIVATE };
|
2015-01-22 00:02:38 +00:00
|
|
|
|
2015-02-02 16:24:09 +00:00
|
|
|
Declaration(Location const& _location, ASTPointer<ASTString> const& _name,
|
|
|
|
Visibility _visibility = Visibility::DEFAULT):
|
|
|
|
ASTNode(_location), m_name(_name), m_visibility(_visibility), m_scope(nullptr) {}
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-12-01 14:22:45 +00:00
|
|
|
/// @returns the declared name.
|
2014-10-29 18:41:07 +00:00
|
|
|
ASTString const& getName() const { return *m_name; }
|
2015-02-02 16:24:09 +00:00
|
|
|
Visibility getVisibility() const { return m_visibility == Visibility::DEFAULT ? getDefaultVisibility() : m_visibility; }
|
|
|
|
bool isPublic() const { return getVisibility() == Visibility::PUBLIC; }
|
|
|
|
|
2014-12-01 14:22:45 +00:00
|
|
|
/// @returns the scope this declaration resides in. Can be nullptr if it is the global scope.
|
|
|
|
/// Available only after name and type resolution step.
|
2014-12-06 01:19:10 +00:00
|
|
|
Declaration const* getScope() const { return m_scope; }
|
2014-12-06 01:32:51 +00:00
|
|
|
void setScope(Declaration const* _scope) { m_scope = _scope; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
/// @returns the type of expressions referencing this declaration.
|
|
|
|
/// The current contract has to be given since this context can change the type, especially of
|
|
|
|
/// contract types.
|
|
|
|
virtual TypePointer getType(ContractDefinition const* m_currentContract = nullptr) const = 0;
|
|
|
|
/// @returns the lvalue type of expressions referencing this declaration
|
|
|
|
virtual LValueType getLValueType() const { return LValueType::NONE; }
|
|
|
|
|
2015-02-02 16:24:09 +00:00
|
|
|
protected:
|
|
|
|
virtual Visibility getDefaultVisibility() const { return Visibility::PUBLIC; }
|
|
|
|
|
2014-10-13 16:22:15 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ASTString> m_name;
|
2015-02-02 16:24:09 +00:00
|
|
|
Visibility m_visibility;
|
2014-12-06 01:32:51 +00:00
|
|
|
Declaration const* m_scope;
|
2014-10-13 16:22:15 +00:00
|
|
|
};
|
|
|
|
|
2015-01-29 16:28:14 +00:00
|
|
|
/**
|
|
|
|
* Abstract class that is added to each AST node that can store local variables.
|
|
|
|
*/
|
|
|
|
class VariableScope
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void addLocalVariable(VariableDeclaration const& _localVariable) { m_localVariables.push_back(&_localVariable); }
|
|
|
|
std::vector<VariableDeclaration const*> const& getLocalVariables() const { return m_localVariables; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<VariableDeclaration const*> m_localVariables;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abstract class that is added to each AST node that can receive documentation.
|
|
|
|
*/
|
|
|
|
class Documented
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Documented(ASTPointer<ASTString> const& _documentation): m_documentation(_documentation) {}
|
|
|
|
|
|
|
|
/// @return A shared pointer of an ASTString.
|
|
|
|
/// Can contain a nullptr in which case indicates absence of documentation
|
|
|
|
ASTPointer<ASTString> const& getDocumentation() const { return m_documentation; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ASTPointer<ASTString> m_documentation;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2015-01-29 11:17:37 +00:00
|
|
|
class ContractDefinition: public Declaration, public Documented
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-09 10:28:37 +00:00
|
|
|
ContractDefinition(Location const& _location,
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ASTString> const& _name,
|
2014-12-10 12:13:12 +00:00
|
|
|
ASTPointer<ASTString> const& _documentation,
|
2015-01-19 20:05:47 +00:00
|
|
|
std::vector<ASTPointer<InheritanceSpecifier>> const& _baseContracts,
|
2014-10-20 12:00:37 +00:00
|
|
|
std::vector<ASTPointer<StructDefinition>> const& _definedStructs,
|
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& _stateVariables,
|
2015-01-21 10:16:18 +00:00
|
|
|
std::vector<ASTPointer<FunctionDefinition>> const& _definedFunctions,
|
2015-01-29 13:35:28 +00:00
|
|
|
std::vector<ASTPointer<ModifierDefinition>> const& _functionModifiers,
|
|
|
|
std::vector<ASTPointer<EventDefinition>> const& _events):
|
2015-01-29 11:17:37 +00:00
|
|
|
Declaration(_location, _name), Documented(_documentation),
|
2015-01-15 15:15:01 +00:00
|
|
|
m_baseContracts(_baseContracts),
|
2014-10-22 22:24:07 +00:00
|
|
|
m_definedStructs(_definedStructs),
|
|
|
|
m_stateVariables(_stateVariables),
|
2014-12-10 12:13:12 +00:00
|
|
|
m_definedFunctions(_definedFunctions),
|
2015-01-29 13:35:28 +00:00
|
|
|
m_functionModifiers(_functionModifiers),
|
|
|
|
m_events(_events)
|
2014-10-09 10:28:37 +00:00
|
|
|
{}
|
2014-10-07 16:25:04 +00:00
|
|
|
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2015-01-19 20:05:47 +00:00
|
|
|
std::vector<ASTPointer<InheritanceSpecifier>> const& getBaseContracts() const { return m_baseContracts; }
|
2014-11-07 01:06:37 +00:00
|
|
|
std::vector<ASTPointer<StructDefinition>> const& getDefinedStructs() const { return m_definedStructs; }
|
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& getStateVariables() const { return m_stateVariables; }
|
2015-01-22 00:02:38 +00:00
|
|
|
std::vector<ASTPointer<ModifierDefinition>> const& getFunctionModifiers() const { return m_functionModifiers; }
|
2014-11-07 01:06:37 +00:00
|
|
|
std::vector<ASTPointer<FunctionDefinition>> const& getDefinedFunctions() const { return m_definedFunctions; }
|
2015-01-29 13:35:28 +00:00
|
|
|
std::vector<ASTPointer<EventDefinition>> const& getEvents() const { return m_events; }
|
2015-01-31 13:41:11 +00:00
|
|
|
std::vector<ASTPointer<EventDefinition>> const& getInterfaceEvents() const;
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
virtual TypePointer getType(ContractDefinition const* m_currentContract) const override;
|
|
|
|
|
2015-01-16 16:50:10 +00:00
|
|
|
/// Checks that there are no illegal overrides, that the constructor does not have a "returns"
|
|
|
|
/// and calls checkTypeRequirements on all its functions.
|
2014-12-15 15:09:50 +00:00
|
|
|
void checkTypeRequirements();
|
|
|
|
|
2015-01-07 15:39:21 +00:00
|
|
|
/// @returns a map of canonical function signatures to FunctionDefinitions
|
|
|
|
/// as intended for use by the ABI.
|
2015-01-29 15:48:39 +00:00
|
|
|
std::map<FixedHash<4>, FunctionTypePointer> getInterfaceFunctions() const;
|
2014-11-23 23:00:46 +00:00
|
|
|
|
2015-01-15 19:04:06 +00:00
|
|
|
/// List of all (direct and indirect) base contracts in order from derived to base, including
|
|
|
|
/// the contract itself. Available after name resolution
|
|
|
|
std::vector<ContractDefinition const*> const& getLinearizedBaseContracts() const { return m_linearizedBaseContracts; }
|
|
|
|
void setLinearizedBaseContracts(std::vector<ContractDefinition const*> const& _bases) { m_linearizedBaseContracts = _bases; }
|
|
|
|
|
2015-01-29 21:50:20 +00:00
|
|
|
/// Returns the constructor or nullptr if no constructor was specified.
|
2014-12-12 15:49:26 +00:00
|
|
|
FunctionDefinition const* getConstructor() const;
|
2015-02-09 01:06:30 +00:00
|
|
|
/// Returns the fallback function or nullptr if no fallback function was specified.
|
2015-01-29 21:50:20 +00:00
|
|
|
FunctionDefinition const* getFallbackFunction() const;
|
2014-12-12 15:49:26 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2015-01-16 16:50:10 +00:00
|
|
|
void checkIllegalOverrides() const;
|
|
|
|
|
2015-01-29 15:48:39 +00:00
|
|
|
std::vector<std::pair<FixedHash<4>, FunctionTypePointer>> const& getInterfaceFunctionList() const;
|
2015-01-14 09:16:58 +00:00
|
|
|
|
2015-01-19 20:05:47 +00:00
|
|
|
std::vector<ASTPointer<InheritanceSpecifier>> m_baseContracts;
|
2014-10-20 12:00:37 +00:00
|
|
|
std::vector<ASTPointer<StructDefinition>> m_definedStructs;
|
|
|
|
std::vector<ASTPointer<VariableDeclaration>> m_stateVariables;
|
|
|
|
std::vector<ASTPointer<FunctionDefinition>> m_definedFunctions;
|
2015-01-21 10:16:18 +00:00
|
|
|
std::vector<ASTPointer<ModifierDefinition>> m_functionModifiers;
|
2015-01-29 13:35:28 +00:00
|
|
|
std::vector<ASTPointer<EventDefinition>> m_events;
|
2015-01-15 19:04:06 +00:00
|
|
|
|
|
|
|
std::vector<ContractDefinition const*> m_linearizedBaseContracts;
|
2015-01-29 15:48:39 +00:00
|
|
|
mutable std::unique_ptr<std::vector<std::pair<FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList;
|
2015-01-31 13:41:11 +00:00
|
|
|
mutable std::unique_ptr<std::vector<ASTPointer<EventDefinition>>> m_interfaceEvents;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2015-01-19 20:05:47 +00:00
|
|
|
class InheritanceSpecifier: public ASTNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InheritanceSpecifier(Location const& _location, ASTPointer<Identifier> const& _baseName,
|
|
|
|
std::vector<ASTPointer<Expression>> _arguments):
|
|
|
|
ASTNode(_location), m_baseName(_baseName), m_arguments(_arguments) {}
|
|
|
|
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
|
|
|
|
ASTPointer<Identifier> const& getName() const { return m_baseName; }
|
|
|
|
std::vector<ASTPointer<Expression>> const& getArguments() const { return m_arguments; }
|
|
|
|
|
|
|
|
void checkTypeRequirements();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ASTPointer<Identifier> m_baseName;
|
|
|
|
std::vector<ASTPointer<Expression>> m_arguments;
|
|
|
|
};
|
|
|
|
|
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-20 12:00:37 +00:00
|
|
|
ASTPointer<ASTString> const& _name,
|
2014-10-22 22:24:07 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& _members):
|
|
|
|
Declaration(_location, _name), m_members(_members) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-11-07 01:06:37 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& getMembers() const { return m_members; }
|
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
virtual TypePointer getType(ContractDefinition const*) const override;
|
|
|
|
|
2014-11-20 17:33:23 +00:00
|
|
|
/// Checks that the members do not include any recursive structs and have valid types
|
|
|
|
/// (e.g. no functions).
|
2014-12-06 01:19:10 +00:00
|
|
|
void checkValidityOfMembers() const;
|
2014-11-20 17:33:23 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-12-06 01:19:10 +00:00
|
|
|
void checkMemberTypes() const;
|
|
|
|
void checkRecursion() const;
|
2014-11-20 17:33:23 +00:00
|
|
|
|
2014-10-20 12:00:37 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> m_members;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Parameter list, used as function parameter list and return list.
|
|
|
|
* None of the parameters is allowed to contain mappings (not even recursively
|
2014-11-13 00:12:57 +00:00
|
|
|
* inside structs).
|
2014-10-28 15:51:26 +00:00
|
|
|
*/
|
2014-10-16 21:49:45 +00:00
|
|
|
class ParameterList: public ASTNode
|
2014-10-08 18:53:50 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-22 22:24:07 +00:00
|
|
|
ParameterList(Location const& _location,
|
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& _parameters):
|
|
|
|
ASTNode(_location), m_parameters(_parameters) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-13 13:07:21 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& getParameters() const { return m_parameters; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-08 18:53:50 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> m_parameters;
|
2014-10-08 18:53:50 +00:00
|
|
|
};
|
|
|
|
|
2015-01-29 11:17:37 +00:00
|
|
|
class FunctionDefinition: public Declaration, public VariableScope, public Documented
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-08 18:53:50 +00:00
|
|
|
public:
|
2014-11-28 00:26:37 +00:00
|
|
|
FunctionDefinition(Location const& _location, ASTPointer<ASTString> const& _name,
|
2015-02-02 16:24:09 +00:00
|
|
|
Declaration::Visibility _visibility, bool _isConstructor,
|
2014-11-28 00:26:37 +00:00
|
|
|
ASTPointer<ASTString> const& _documentation,
|
2014-11-27 14:21:22 +00:00
|
|
|
ASTPointer<ParameterList> const& _parameters,
|
|
|
|
bool _isDeclaredConst,
|
2015-01-22 00:02:38 +00:00
|
|
|
std::vector<ASTPointer<ModifierInvocation>> const& _modifiers,
|
2014-11-27 14:21:22 +00:00
|
|
|
ASTPointer<ParameterList> const& _returnParameters,
|
|
|
|
ASTPointer<Block> const& _body):
|
2015-02-02 16:24:09 +00:00
|
|
|
Declaration(_location, _name, _visibility), Documented(_documentation),
|
|
|
|
m_isConstructor(_isConstructor),
|
2014-11-27 14:21:22 +00:00
|
|
|
m_parameters(_parameters),
|
|
|
|
m_isDeclaredConst(_isDeclaredConst),
|
2015-01-22 00:02:38 +00:00
|
|
|
m_functionModifiers(_modifiers),
|
2014-11-27 14:21:22 +00:00
|
|
|
m_returnParameters(_returnParameters),
|
2015-01-29 11:17:37 +00:00
|
|
|
m_body(_body)
|
2014-11-28 00:26:37 +00:00
|
|
|
{}
|
|
|
|
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2015-01-20 14:58:04 +00:00
|
|
|
bool isConstructor() const { return m_isConstructor; }
|
2014-10-10 14:37:54 +00:00
|
|
|
bool isDeclaredConst() const { return m_isDeclaredConst; }
|
2015-01-22 00:02:38 +00:00
|
|
|
std::vector<ASTPointer<ModifierInvocation>> const& getModifiers() const { return m_functionModifiers; }
|
2014-10-20 12:00:37 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& getParameters() const { return m_parameters->getParameters(); }
|
2014-12-06 00:06:24 +00:00
|
|
|
ParameterList const& getParameterList() const { return *m_parameters; }
|
2014-10-30 00:20:32 +00:00
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& getReturnParameters() const { return m_returnParameters->getParameters(); }
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ParameterList> const& getReturnParameterList() const { return m_returnParameters; }
|
2014-12-06 00:06:24 +00:00
|
|
|
Block const& getBody() const { return *m_body; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
virtual TypePointer getType(ContractDefinition const*) const override;
|
2014-11-03 17:39:16 +00:00
|
|
|
|
2014-11-10 16:31:09 +00:00
|
|
|
/// Checks that all parameters have allowed types and calls checkTypeRequirements on the body.
|
|
|
|
void checkTypeRequirements();
|
2014-11-23 23:00:46 +00:00
|
|
|
|
2015-01-07 09:45:59 +00:00
|
|
|
/// @returns the canonical signature of the function
|
|
|
|
/// That consists of the name of the function followed by the types of the
|
|
|
|
/// arguments separated by commas all enclosed in parentheses without any spaces.
|
|
|
|
std::string getCanonicalSignature() const;
|
2015-01-06 16:42:38 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2015-01-20 14:58:04 +00:00
|
|
|
bool m_isConstructor;
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ParameterList> m_parameters;
|
2014-10-09 10:28:37 +00:00
|
|
|
bool m_isDeclaredConst;
|
2015-01-22 00:02:38 +00:00
|
|
|
std::vector<ASTPointer<ModifierInvocation>> m_functionModifiers;
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ParameterList> m_returnParameters;
|
|
|
|
ASTPointer<Block> m_body;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Declaration of a variable. This can be used in various places, e.g. in function parameter
|
|
|
|
* lists, struct definitions and even function bodys.
|
|
|
|
*/
|
2014-10-16 21:49:45 +00:00
|
|
|
class VariableDeclaration: public Declaration
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-20 12:00:37 +00:00
|
|
|
VariableDeclaration(Location const& _location, ASTPointer<TypeName> const& _type,
|
2015-02-02 16:24:09 +00:00
|
|
|
ASTPointer<ASTString> const& _name, Visibility _visibility,
|
|
|
|
bool _isStateVar = false, bool _isIndexed = false):
|
|
|
|
Declaration(_location, _name, _visibility), m_typeName(_type),
|
|
|
|
m_isStateVariable(_isStateVar), m_isIndexed(_isIndexed) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
TypeName const* getTypeName() const { return m_typeName.get(); }
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-10-28 08:25:01 +00:00
|
|
|
/// 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.
|
2015-01-22 00:02:38 +00:00
|
|
|
TypePointer getType(ContractDefinition const* = nullptr) const { return m_type; }
|
2014-10-20 12:00:37 +00:00
|
|
|
void setType(std::shared_ptr<Type const> const& _type) { m_type = _type; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
virtual LValueType getLValueType() const override;
|
2015-01-22 16:40:22 +00:00
|
|
|
bool isLocalVariable() const { return !!dynamic_cast<FunctionDefinition const*>(getScope()); }
|
2015-01-26 15:14:40 +00:00
|
|
|
bool isStateVariable() const { return m_isStateVariable; }
|
2015-01-29 13:35:28 +00:00
|
|
|
bool isIndexed() const { return m_isIndexed; }
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2015-02-02 16:24:09 +00:00
|
|
|
protected:
|
|
|
|
Visibility getDefaultVisibility() const override { return Visibility::PROTECTED; }
|
|
|
|
|
2015-01-22 16:40:22 +00:00
|
|
|
private:
|
|
|
|
ASTPointer<TypeName> m_typeName; ///< can be empty ("var")
|
2015-01-26 15:14:40 +00:00
|
|
|
bool m_isStateVariable; ///< Whether or not this is a contract state variable
|
2015-01-29 13:35:28 +00:00
|
|
|
bool m_isIndexed; ///< Whether this is an indexed variable (used by events).
|
2015-01-28 17:06:45 +00:00
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
std::shared_ptr<Type const> m_type; ///< derived type, initially empty
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2015-01-21 10:16:18 +00:00
|
|
|
/**
|
|
|
|
* Definition of a function modifier.
|
|
|
|
*/
|
2015-01-29 11:17:37 +00:00
|
|
|
class ModifierDefinition: public Declaration, public VariableScope, public Documented
|
2015-01-21 10:16:18 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ModifierDefinition(Location const& _location,
|
|
|
|
ASTPointer<ASTString> const& _name,
|
|
|
|
ASTPointer<ASTString> const& _documentation,
|
|
|
|
ASTPointer<ParameterList> const& _parameters,
|
|
|
|
ASTPointer<Block> const& _body):
|
2015-01-29 11:17:37 +00:00
|
|
|
Declaration(_location, _name), Documented(_documentation),
|
2015-01-21 10:16:18 +00:00
|
|
|
m_parameters(_parameters), m_body(_body) {}
|
|
|
|
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
|
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& getParameters() const { return m_parameters->getParameters(); }
|
|
|
|
ParameterList const& getParameterList() const { return *m_parameters; }
|
|
|
|
Block const& getBody() const { return *m_body; }
|
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
virtual TypePointer getType(ContractDefinition const* = nullptr) const override;
|
|
|
|
|
2015-01-21 10:16:18 +00:00
|
|
|
void checkTypeRequirements();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ASTPointer<ParameterList> m_parameters;
|
|
|
|
ASTPointer<Block> m_body;
|
|
|
|
};
|
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
/**
|
|
|
|
* Invocation/usage of a modifier in a function header.
|
|
|
|
*/
|
|
|
|
class ModifierInvocation: public ASTNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ModifierInvocation(Location const& _location, ASTPointer<Identifier> const& _name,
|
|
|
|
std::vector<ASTPointer<Expression>> _arguments):
|
|
|
|
ASTNode(_location), m_modifierName(_name), m_arguments(_arguments) {}
|
|
|
|
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
|
|
|
|
ASTPointer<Identifier> const& getName() const { return m_modifierName; }
|
|
|
|
std::vector<ASTPointer<Expression>> const& getArguments() const { return m_arguments; }
|
|
|
|
|
|
|
|
void checkTypeRequirements();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ASTPointer<Identifier> m_modifierName;
|
|
|
|
std::vector<ASTPointer<Expression>> m_arguments;
|
|
|
|
};
|
|
|
|
|
2015-01-29 13:35:28 +00:00
|
|
|
/**
|
|
|
|
* Definition of a (loggable) event.
|
|
|
|
*/
|
2015-01-31 15:50:33 +00:00
|
|
|
class EventDefinition: public Declaration, public VariableScope, public Documented
|
2015-01-29 13:35:28 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
EventDefinition(Location const& _location,
|
2015-01-29 18:32:32 +00:00
|
|
|
ASTPointer<ASTString> const& _name,
|
|
|
|
ASTPointer<ASTString> const& _documentation,
|
|
|
|
ASTPointer<ParameterList> const& _parameters):
|
2015-01-29 13:35:28 +00:00
|
|
|
Declaration(_location, _name), Documented(_documentation), m_parameters(_parameters) {}
|
|
|
|
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
|
|
|
|
std::vector<ASTPointer<VariableDeclaration>> const& getParameters() const { return m_parameters->getParameters(); }
|
|
|
|
ParameterList const& getParameterList() const { return *m_parameters; }
|
|
|
|
|
|
|
|
virtual TypePointer getType(ContractDefinition const* = nullptr) const override
|
|
|
|
{
|
|
|
|
return std::make_shared<FunctionType>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkTypeRequirements();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ASTPointer<ParameterList> m_parameters;
|
|
|
|
};
|
|
|
|
|
2014-11-21 18:14:56 +00:00
|
|
|
/**
|
2014-11-26 12:19:17 +00:00
|
|
|
* Pseudo AST node that is used as declaration for "this", "msg", "tx", "block" and the global
|
|
|
|
* functions when such an identifier is encountered. Will never have a valid location in the source code.
|
2014-11-21 18:14:56 +00:00
|
|
|
*/
|
|
|
|
class MagicVariableDeclaration: public Declaration
|
|
|
|
{
|
|
|
|
public:
|
2014-11-26 12:19:17 +00:00
|
|
|
MagicVariableDeclaration(ASTString const& _name, std::shared_ptr<Type const> const& _type):
|
|
|
|
Declaration(Location(), std::make_shared<ASTString>(_name)), m_type(_type) {}
|
2014-11-21 18:14:56 +00:00
|
|
|
virtual void accept(ASTVisitor&) override { BOOST_THROW_EXCEPTION(InternalCompilerError()
|
|
|
|
<< errinfo_comment("MagicVariableDeclaration used inside real AST.")); }
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor&) const override { BOOST_THROW_EXCEPTION(InternalCompilerError()
|
2014-12-05 16:29:20 +00:00
|
|
|
<< errinfo_comment("MagicVariableDeclaration used inside real AST.")); }
|
2014-11-21 18:14:56 +00:00
|
|
|
|
2015-01-22 00:02:38 +00:00
|
|
|
virtual TypePointer getType(ContractDefinition const* = nullptr) const override { return m_type; }
|
2014-11-21 18:14:56 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<Type const> m_type;
|
|
|
|
};
|
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
/// Types
|
2014-10-07 16:25:04 +00:00
|
|
|
/// @{
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Abstract base class of a type name, can be any built-in or user-defined type.
|
|
|
|
*/
|
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-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
/// 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.
|
2014-11-20 17:33:23 +00:00
|
|
|
/// If it returns an empty shared pointer after that, this indicates that the type was not found.
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual std::shared_ptr<Type const> toType() const = 0;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Any pre-defined type name represented by a single keyword, i.e. it excludes mappings,
|
|
|
|
* contracts, functions, etc.
|
|
|
|
*/
|
2014-10-16 21:49:45 +00:00
|
|
|
class ElementaryTypeName: public TypeName
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-22 22:24:07 +00:00
|
|
|
explicit ElementaryTypeName(Location const& _location, Token::Value _type):
|
2014-11-05 13:20:56 +00:00
|
|
|
TypeName(_location), m_type(_type)
|
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(Token::isElementaryTypeName(_type), "");
|
2014-11-05 13:20:56 +00:00
|
|
|
}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
virtual std::shared_ptr<Type const> toType() const override { return Type::fromElementaryTypeName(m_type); }
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
Token::Value getTypeName() const { return m_type; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
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-28 15:51:26 +00:00
|
|
|
/**
|
2014-11-20 17:33:23 +00:00
|
|
|
* Name referring to a user-defined type (i.e. a struct, contract, etc.).
|
2014-10-28 15:51:26 +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-22 22:24:07 +00:00
|
|
|
UserDefinedTypeName(Location const& _location, ASTPointer<ASTString> const& _name):
|
2014-12-06 01:32:51 +00:00
|
|
|
TypeName(_location), m_name(_name), m_referencedDeclaration(nullptr) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
virtual std::shared_ptr<Type const> toType() const override { return Type::fromUserDefinedTypeName(*this); }
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-10-29 18:41:07 +00:00
|
|
|
ASTString const& getName() const { return *m_name; }
|
2014-12-06 01:32:51 +00:00
|
|
|
void setReferencedDeclaration(Declaration const& _referencedDeclaration) { m_referencedDeclaration = &_referencedDeclaration; }
|
2014-11-20 17:33:23 +00:00
|
|
|
Declaration const* getReferencedDeclaration() const { return m_referencedDeclaration; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ASTString> m_name;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-12-06 01:32:51 +00:00
|
|
|
Declaration const* m_referencedDeclaration;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* A mapping type. Its source form is "mapping('keyType' => 'valueType')"
|
|
|
|
*/
|
2014-10-16 21:49:45 +00:00
|
|
|
class Mapping: public TypeName
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-20 12:00:37 +00:00
|
|
|
Mapping(Location const& _location, ASTPointer<ElementaryTypeName> const& _keyType,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTPointer<TypeName> const& _valueType):
|
|
|
|
TypeName(_location), m_keyType(_keyType), m_valueType(_valueType) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
virtual std::shared_ptr<Type const> toType() const override { return Type::fromMapping(*this); }
|
2014-11-10 16:31:09 +00:00
|
|
|
|
|
|
|
ElementaryTypeName const& getKeyType() const { return *m_keyType; }
|
|
|
|
TypeName const& getValueType() const { return *m_valueType; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ElementaryTypeName> m_keyType;
|
|
|
|
ASTPointer<TypeName> m_valueType;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// Statements
|
|
|
|
/// @{
|
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Abstract base class for 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-13 16:22:15 +00:00
|
|
|
|
2014-10-28 08:25:01 +00:00
|
|
|
/// 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.
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() = 0;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Brace-enclosed block containing zero or more statements.
|
|
|
|
*/
|
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-22 22:24:07 +00:00
|
|
|
Block(Location const& _location, std::vector<ASTPointer<Statement>> const& _statements):
|
|
|
|
Statement(_location), m_statements(_statements) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
std::vector<ASTPointer<Statement>> m_statements;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2015-01-21 10:16:18 +00:00
|
|
|
/**
|
|
|
|
* Special placeholder statement denoted by "_" used in function modifiers. This is replaced by
|
|
|
|
* the original function when the modifier is applied.
|
|
|
|
*/
|
|
|
|
class PlaceholderStatement: public Statement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PlaceholderStatement(Location const& _location): Statement(_location) {}
|
|
|
|
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
|
|
|
|
virtual void checkTypeRequirements() override { }
|
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* If-statement with an optional "else" part. Note that "else if" is modeled by having a new
|
|
|
|
* if-statement as the false (else) body.
|
|
|
|
*/
|
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:
|
2014-10-20 12:00:37 +00:00
|
|
|
IfStatement(Location const& _location, ASTPointer<Expression> const& _condition,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTPointer<Statement> const& _trueBody, ASTPointer<Statement> const& _falseBody):
|
|
|
|
Statement(_location),
|
|
|
|
m_condition(_condition), m_trueBody(_trueBody), m_falseBody(_falseBody) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getCondition() const { return *m_condition; }
|
|
|
|
Statement const& getTrueStatement() const { return *m_trueBody; }
|
2014-11-04 18:52:04 +00:00
|
|
|
/// @returns the "else" part of the if statement or nullptr if there is no "else" part.
|
2014-12-06 01:19:10 +00:00
|
|
|
Statement const* getFalseStatement() const { return m_falseBody.get(); }
|
2014-11-03 17:39:16 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_condition;
|
|
|
|
ASTPointer<Statement> m_trueBody;
|
2014-10-31 12:29:32 +00:00
|
|
|
ASTPointer<Statement> m_falseBody; ///< "else" part, optional
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
2014-12-05 16:29:20 +00:00
|
|
|
* Statement in which a break statement is legal (abstract class).
|
2014-10-28 15:51:26 +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-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:
|
2014-10-20 12:00:37 +00:00
|
|
|
WhileStatement(Location const& _location, ASTPointer<Expression> const& _condition,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTPointer<Statement> const& _body):
|
|
|
|
BreakableStatement(_location), m_condition(_condition), m_body(_body) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getCondition() const { return *m_condition; }
|
|
|
|
Statement const& getBody() const { return *m_body; }
|
2014-11-03 17:39:16 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_condition;
|
|
|
|
ASTPointer<Statement> m_body;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-12-16 13:46:17 +00:00
|
|
|
/**
|
|
|
|
* For loop statement
|
|
|
|
*/
|
2014-12-15 16:45:18 +00:00
|
|
|
class ForStatement: public BreakableStatement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ForStatement(Location const& _location,
|
2014-12-16 12:20:50 +00:00
|
|
|
ASTPointer<Statement> const& _initExpression,
|
2014-12-15 16:45:18 +00:00
|
|
|
ASTPointer<Expression> const& _conditionExpression,
|
|
|
|
ASTPointer<ExpressionStatement> const& _loopExpression,
|
|
|
|
ASTPointer<Statement> const& _body):
|
|
|
|
BreakableStatement(_location),
|
|
|
|
m_initExpression(_initExpression),
|
|
|
|
m_condExpression(_conditionExpression),
|
|
|
|
m_loopExpression(_loopExpression),
|
|
|
|
m_body(_body) {}
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
virtual void checkTypeRequirements() override;
|
|
|
|
|
2014-12-16 15:52:47 +00:00
|
|
|
Statement const* getInitializationExpression() const { return m_initExpression.get(); }
|
|
|
|
Expression const* getCondition() const { return m_condExpression.get(); }
|
|
|
|
ExpressionStatement const* getLoopExpression() const { return m_loopExpression.get(); }
|
|
|
|
Statement const& getBody() const { return *m_body; }
|
|
|
|
|
2014-12-15 16:45:18 +00:00
|
|
|
private:
|
2014-12-16 13:46:17 +00:00
|
|
|
/// For statement's initialization expresion. for(XXX; ; ). Can be empty
|
2014-12-16 12:20:50 +00:00
|
|
|
ASTPointer<Statement> m_initExpression;
|
2014-12-16 13:46:17 +00:00
|
|
|
/// For statement's condition expresion. for(; XXX ; ). Can be empty
|
2014-12-15 16:45:18 +00:00
|
|
|
ASTPointer<Expression> m_condExpression;
|
2014-12-16 13:46:17 +00:00
|
|
|
/// For statement's loop expresion. for(;;XXX). Can be empty
|
2014-12-15 16:45:18 +00:00
|
|
|
ASTPointer<ExpressionStatement> m_loopExpression;
|
2014-12-16 13:46:17 +00:00
|
|
|
/// The body of the loop
|
2014-12-15 16:45:18 +00:00
|
|
|
ASTPointer<Statement> m_body;
|
|
|
|
};
|
|
|
|
|
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-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-11-05 22:35:00 +00:00
|
|
|
virtual void 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-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-11-05 22:35:00 +00:00
|
|
|
virtual void 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:
|
2014-10-22 22:24:07 +00:00
|
|
|
Return(Location const& _location, ASTPointer<Expression> _expression):
|
2014-12-06 01:32:51 +00:00
|
|
|
Statement(_location), m_expression(_expression), m_returnParameters(nullptr) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2015-01-23 01:35:27 +00:00
|
|
|
void setFunctionReturnParameters(ParameterList const* _parameters) { m_returnParameters = _parameters; }
|
|
|
|
ParameterList const* getFunctionReturnParameters() const { return m_returnParameters; }
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const* getExpression() const { return m_expression.get(); }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-31 12:29:32 +00:00
|
|
|
ASTPointer<Expression> m_expression; ///< value to return, optional
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
/// Pointer to the parameter list of the function, filled by the @ref NameAndTypeResolver.
|
2015-01-19 18:18:34 +00:00
|
|
|
ParameterList const* m_returnParameters;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
*/
|
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:
|
2014-10-20 12:00:37 +00:00
|
|
|
VariableDefinition(Location const& _location, ASTPointer<VariableDeclaration> _variable,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTPointer<Expression> _value):
|
|
|
|
Statement(_location), m_variable(_variable), m_value(_value) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
VariableDeclaration const& getDeclaration() const { return *m_variable; }
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const* getExpression() const { return m_value.get(); }
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<VariableDeclaration> m_variable;
|
2014-10-22 18:35:35 +00:00
|
|
|
ASTPointer<Expression> m_value; ///< the assigned value, can be missing
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
/**
|
|
|
|
* A statement that contains only an expression (i.e. an assignment, function call, ...).
|
|
|
|
*/
|
|
|
|
class ExpressionStatement: public Statement
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ExpressionStatement(Location const& _location, ASTPointer<Expression> _expression):
|
|
|
|
Statement(_location), m_expression(_expression) {}
|
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-30 00:20:32 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getExpression() const { return *m_expression; }
|
2014-10-30 00:20:32 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ASTPointer<Expression> m_expression;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// Expressions
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An expression, i.e. something that has a value (which can also be of type "void" in case
|
|
|
|
* of some function calls).
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
class Expression: public ASTNode
|
2014-10-07 16:25:04 +00:00
|
|
|
{
|
2014-10-09 13:57:49 +00:00
|
|
|
public:
|
2015-01-22 00:02:38 +00:00
|
|
|
Expression(Location const& _location): ASTNode(_location) {}
|
2014-10-30 00:20:32 +00:00
|
|
|
virtual void checkTypeRequirements() = 0;
|
2014-10-22 18:35:35 +00:00
|
|
|
|
2014-10-20 12:00:37 +00:00
|
|
|
std::shared_ptr<Type const> const& getType() const { return m_type; }
|
2015-01-22 00:02:38 +00:00
|
|
|
bool isLValue() const { return m_lvalue != Declaration::LValueType::NONE; }
|
|
|
|
bool isLocalLValue() const { return m_lvalue == Declaration::LValueType::LOCAL; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
/// Helper function, infer the type via @ref checkTypeRequirements and then check that it
|
|
|
|
/// is implicitly convertible to @a _expectedType. If not, throw exception.
|
|
|
|
void expectType(Type const& _expectedType);
|
2014-11-10 16:31:09 +00:00
|
|
|
/// Checks that this expression is an lvalue and also registers that an address and
|
|
|
|
/// not a value is generated during compilation. Can be called after checkTypeRequirements()
|
|
|
|
/// by an enclosing expression.
|
|
|
|
void requireLValue();
|
|
|
|
/// Returns true if @a requireLValue was previously called on this expression.
|
|
|
|
bool lvalueRequested() const { return m_lvalueRequested; }
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2014-10-13 16:22:15 +00:00
|
|
|
protected:
|
2014-10-20 12:00:37 +00:00
|
|
|
//! Inferred type of the expression, only filled after a call to checkTypeRequirements().
|
|
|
|
std::shared_ptr<Type const> m_type;
|
2014-12-01 16:32:10 +00:00
|
|
|
//! If this expression is an lvalue (i.e. something that can be assigned to) and is stored
|
|
|
|
//! locally or in storage. This is set during calls to @a checkTypeRequirements()
|
2015-01-22 00:02:38 +00:00
|
|
|
Declaration::LValueType m_lvalue = Declaration::LValueType::NONE;
|
2014-11-10 16:31:09 +00:00
|
|
|
//! Whether the outer expression requested the address (true) or the value (false) of this expression.
|
2015-01-22 00:02:38 +00:00
|
|
|
bool m_lvalueRequested = false;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-22 18:35:35 +00:00
|
|
|
/// Assignment, can also be a compound assignment.
|
|
|
|
/// Examples: (a = 7 + 8) or (a *= 2)
|
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:
|
2014-10-20 12:00:37 +00:00
|
|
|
Assignment(Location const& _location, ASTPointer<Expression> const& _leftHandSide,
|
2014-10-22 22:24:07 +00:00
|
|
|
Token::Value _assignmentOperator, ASTPointer<Expression> const& _rightHandSide):
|
|
|
|
Expression(_location), m_leftHandSide(_leftHandSide),
|
2014-11-05 13:20:56 +00:00
|
|
|
m_assigmentOperator(_assignmentOperator), m_rightHandSide(_rightHandSide)
|
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(Token::isAssignmentOp(_assignmentOperator), "");
|
2014-11-05 13:20:56 +00:00
|
|
|
}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getLeftHandSide() const { return *m_leftHandSide; }
|
2014-10-10 14:37:54 +00:00
|
|
|
Token::Value getAssignmentOperator() const { return m_assigmentOperator; }
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getRightHandSide() const { return *m_rightHandSide; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_leftHandSide;
|
2014-10-09 10:28:37 +00:00
|
|
|
Token::Value m_assigmentOperator;
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_rightHandSide;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Operation involving a unary operator, pre- or postfix.
|
|
|
|
* Examples: ++i, delete x or !true
|
|
|
|
*/
|
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,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTPointer<Expression> const& _subExpression, bool _isPrefix):
|
|
|
|
Expression(_location), m_operator(_operator),
|
2014-11-05 13:20:56 +00:00
|
|
|
m_subExpression(_subExpression), m_isPrefix(_isPrefix)
|
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(Token::isUnaryOp(_operator), "");
|
2014-11-05 13:20:56 +00:00
|
|
|
}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void 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-12-06 01:19:10 +00:00
|
|
|
Expression const& getSubExpression() const { return *m_subExpression; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 10:28:37 +00:00
|
|
|
Token::Value m_operator;
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_subExpression;
|
2014-10-09 13:57:49 +00:00
|
|
|
bool m_isPrefix;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Operation involving a binary operator.
|
|
|
|
* Examples: 1 + 2, true && false or 1 <= 4
|
|
|
|
*/
|
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:
|
2014-10-20 12:00:37 +00:00
|
|
|
BinaryOperation(Location const& _location, ASTPointer<Expression> const& _left,
|
2014-10-22 22:24:07 +00:00
|
|
|
Token::Value _operator, ASTPointer<Expression> const& _right):
|
2014-11-05 13:20:56 +00:00
|
|
|
Expression(_location), m_left(_left), m_operator(_operator), m_right(_right)
|
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(Token::isBinaryOp(_operator) || Token::isCompareOp(_operator), "");
|
2014-11-05 13:20:56 +00:00
|
|
|
}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getLeftExpression() const { return *m_left; }
|
|
|
|
Expression const& getRightExpression() const { return *m_right; }
|
2014-10-10 14:37:54 +00:00
|
|
|
Token::Value getOperator() const { return m_operator; }
|
2014-11-04 14:01:07 +00:00
|
|
|
Type const& getCommonType() const { return *m_commonType; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_left;
|
2014-10-09 10:28:37 +00:00
|
|
|
Token::Value m_operator;
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_right;
|
2014-10-13 16:22:15 +00:00
|
|
|
|
2014-11-04 14:01:07 +00:00
|
|
|
/// The common type that is used for the operation, not necessarily the result type (e.g. for
|
|
|
|
/// comparisons, this is always bool).
|
2014-10-20 12:00:37 +00:00
|
|
|
std::shared_ptr<Type const> m_commonType;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +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:
|
2014-10-20 12:00:37 +00:00
|
|
|
FunctionCall(Location const& _location, ASTPointer<Expression> const& _expression,
|
2015-02-03 20:25:08 +00:00
|
|
|
std::vector<ASTPointer<Expression>> const& _arguments, std::vector<ASTPointer<ASTString>> const& _names):
|
2015-01-29 17:26:00 +00:00
|
|
|
Expression(_location), m_expression(_expression), m_arguments(_arguments), m_names(_names) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-22 18:35:35 +00:00
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getExpression() const { return *m_expression; }
|
|
|
|
std::vector<ASTPointer<Expression const>> getArguments() const { return {m_arguments.begin(), m_arguments.end()}; }
|
2015-02-03 20:25:08 +00:00
|
|
|
std::vector<ASTPointer<ASTString>> const& getNames() const { return m_names; }
|
2014-10-25 14:52:22 +00:00
|
|
|
|
2014-10-20 10:41:56 +00:00
|
|
|
/// Returns true if this is not an actual function call, but an explicit type conversion
|
|
|
|
/// or constructor call.
|
|
|
|
bool isTypeConversion() const;
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_expression;
|
|
|
|
std::vector<ASTPointer<Expression>> m_arguments;
|
2015-02-03 20:25:08 +00:00
|
|
|
std::vector<ASTPointer<ASTString>> m_names;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-12-12 15:49:26 +00:00
|
|
|
/**
|
2015-01-13 17:12:19 +00:00
|
|
|
* Expression that creates a new contract, e.g. the "new SomeContract" part in "new SomeContract(1, 2)".
|
2014-12-12 15:49:26 +00:00
|
|
|
*/
|
|
|
|
class NewExpression: public Expression
|
|
|
|
{
|
|
|
|
public:
|
2015-01-13 17:12:19 +00:00
|
|
|
NewExpression(Location const& _location, ASTPointer<Identifier> const& _contractName):
|
|
|
|
Expression(_location), m_contractName(_contractName) {}
|
2014-12-12 15:49:26 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
virtual void checkTypeRequirements() override;
|
|
|
|
|
|
|
|
/// Returns the referenced contract. Can only be called after type checking.
|
2014-12-17 15:23:18 +00:00
|
|
|
ContractDefinition const* getContract() const { solAssert(m_contract, ""); return m_contract; }
|
2014-12-12 15:49:26 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ASTPointer<Identifier> m_contractName;
|
|
|
|
|
|
|
|
ContractDefinition const* m_contract = nullptr;
|
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Access to a member of an object. Example: x.name
|
|
|
|
*/
|
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:
|
2014-10-20 12:00:37 +00:00
|
|
|
MemberAccess(Location const& _location, ASTPointer<Expression> _expression,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTPointer<ASTString> const& _memberName):
|
|
|
|
Expression(_location), m_expression(_expression), m_memberName(_memberName) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
|
|
|
Expression const& getExpression() const { return *m_expression; }
|
2014-10-29 18:41:07 +00:00
|
|
|
ASTString const& getMemberName() const { return *m_memberName; }
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_expression;
|
|
|
|
ASTPointer<ASTString> m_memberName;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Index access to an array. Example: a[2]
|
|
|
|
*/
|
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:
|
2014-10-20 12:00:37 +00:00
|
|
|
IndexAccess(Location const& _location, ASTPointer<Expression> const& _base,
|
2014-10-22 22:24:07 +00:00
|
|
|
ASTPointer<Expression> const& _index):
|
|
|
|
Expression(_location), m_base(_base), m_index(_index) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-12-06 01:19:10 +00:00
|
|
|
Expression const& getBaseExpression() const { return *m_base; }
|
|
|
|
Expression const& getIndexExpression() const { return *m_index; }
|
2014-11-13 00:12:57 +00:00
|
|
|
|
2014-10-09 13:57:49 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<Expression> m_base;
|
|
|
|
ASTPointer<Expression> m_index;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* Primary expression, i.e. an expression that cannot be divided any further. Examples are literals
|
|
|
|
* or variable references.
|
|
|
|
*/
|
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-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* An identifier, i.e. a reference to a declaration by name like a variable or function.
|
|
|
|
*/
|
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-22 22:24:07 +00:00
|
|
|
Identifier(Location const& _location, ASTPointer<ASTString> const& _name):
|
2015-01-19 18:18:34 +00:00
|
|
|
PrimaryExpression(_location), m_name(_name) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
ASTString const& getName() const { return *m_name; }
|
2014-10-22 18:35:35 +00:00
|
|
|
|
2015-01-19 18:18:34 +00:00
|
|
|
void setReferencedDeclaration(Declaration const& _referencedDeclaration,
|
|
|
|
ContractDefinition const* _currentContract = nullptr)
|
|
|
|
{
|
|
|
|
m_referencedDeclaration = &_referencedDeclaration;
|
|
|
|
m_currentContract = _currentContract;
|
|
|
|
}
|
2014-12-06 00:06:24 +00:00
|
|
|
Declaration const* getReferencedDeclaration() const { return m_referencedDeclaration; }
|
2015-01-19 18:18:34 +00:00
|
|
|
ContractDefinition const* getCurrentContract() const { return m_currentContract; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ASTString> m_name;
|
2014-10-13 13:07:21 +00:00
|
|
|
|
2014-10-28 08:25:01 +00:00
|
|
|
/// Declaration the name refers to.
|
2015-01-19 18:18:34 +00:00
|
|
|
Declaration const* m_referencedDeclaration = nullptr;
|
|
|
|
/// Stores a reference to the current contract. This is needed because types of base contracts
|
|
|
|
/// change depending on the context.
|
|
|
|
ContractDefinition const* m_currentContract = nullptr;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
2014-10-28 15:51:26 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-10-16 21:49:45 +00:00
|
|
|
class ElementaryTypeNameExpression: public PrimaryExpression
|
2014-10-09 13:57:49 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-10-22 22:24:07 +00:00
|
|
|
ElementaryTypeNameExpression(Location const& _location, Token::Value _typeToken):
|
2014-11-05 13:20:56 +00:00
|
|
|
PrimaryExpression(_location), m_typeToken(_typeToken)
|
|
|
|
{
|
2014-12-17 15:23:18 +00:00
|
|
|
solAssert(Token::isElementaryTypeName(_typeToken), "");
|
2014-11-05 13:20:56 +00:00
|
|
|
}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void 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-20 14:28:24 +00:00
|
|
|
|
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-28 15:51:26 +00:00
|
|
|
/**
|
2015-02-04 15:37:54 +00:00
|
|
|
* A literal string or number. @see ExpressionCompiler::endVisit() is used to actually parse its value.
|
2014-10-28 15:51:26 +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:
|
2015-02-05 21:38:07 +00:00
|
|
|
enum class SubDenomination
|
|
|
|
{
|
2015-02-05 22:03:24 +00:00
|
|
|
None = Token::ILLEGAL,
|
|
|
|
Wei = Token::SubWei,
|
|
|
|
Szabo = Token::SubSzabo,
|
|
|
|
Finney = Token::SubFinney,
|
|
|
|
Ether = Token::SubEther
|
2015-02-05 21:38:07 +00:00
|
|
|
};
|
2015-02-04 15:37:54 +00:00
|
|
|
Literal(Location const& _location, Token::Value _token,
|
|
|
|
ASTPointer<ASTString> const& _value,
|
2015-02-06 12:38:10 +00:00
|
|
|
SubDenomination _sub = SubDenomination::None):
|
|
|
|
PrimaryExpression(_location), m_token(_token), m_value(_value), m_subDenomination(_sub) {}
|
2014-10-10 14:37:54 +00:00
|
|
|
virtual void accept(ASTVisitor& _visitor) override;
|
2014-12-06 01:19:10 +00:00
|
|
|
virtual void accept(ASTConstVisitor& _visitor) const override;
|
2014-10-20 12:00:37 +00:00
|
|
|
virtual void checkTypeRequirements() override;
|
2014-10-10 14:37:54 +00:00
|
|
|
|
|
|
|
Token::Value getToken() const { return m_token; }
|
2014-10-22 18:35:35 +00:00
|
|
|
/// @returns the non-parsed value of the literal
|
2014-10-10 14:37:54 +00:00
|
|
|
ASTString const& getValue() const { return *m_value; }
|
2014-10-20 14:28:24 +00:00
|
|
|
|
2015-02-05 21:38:07 +00:00
|
|
|
SubDenomination getSubDenomination() const { return m_subDenomination; }
|
2015-02-04 15:37:54 +00:00
|
|
|
|
2014-10-07 16:25:04 +00:00
|
|
|
private:
|
2014-10-09 13:57:49 +00:00
|
|
|
Token::Value m_token;
|
2014-10-20 12:00:37 +00:00
|
|
|
ASTPointer<ASTString> m_value;
|
2015-02-05 21:38:07 +00:00
|
|
|
SubDenomination m_subDenomination;
|
2014-10-07 16:25:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
|
2015-01-22 16:40:22 +00:00
|
|
|
|
2014-10-16 12:08:54 +00:00
|
|
|
}
|
|
|
|
}
|