solidity/libsolidity/codegen/LValue.h

198 lines
6.6 KiB
C
Raw Normal View History

2015-02-25 14:14:22 +00:00
/*
This file is part of solidity.
2015-02-25 14:14:22 +00:00
solidity is free software: you can redistribute it and/or modify
2015-02-25 14:14:22 +00:00
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.
solidity is distributed in the hope that it will be useful,
2015-02-25 14:14:22 +00:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
2015-02-25 14:14:22 +00:00
*/
/**
* @author Christian <c@ethdev.com>
* @date 2015
2016-07-14 20:56:45 +00:00
* LValues for use in the expression compiler.
2015-02-25 14:14:22 +00:00
*/
#pragma once
2018-12-17 16:06:11 +00:00
#include <libsolidity/codegen/ArrayUtils.h>
#include <liblangutil/SourceLocation.h>
2015-02-25 14:14:22 +00:00
#include <memory>
2015-10-14 13:19:50 +00:00
#include <vector>
2015-02-25 14:14:22 +00:00
2019-12-11 16:31:36 +00:00
namespace solidity::frontend
2015-02-25 14:14:22 +00:00
{
class Declaration;
class Type;
2015-10-14 13:19:50 +00:00
class TupleType;
2015-02-25 19:27:55 +00:00
class ArrayType;
2015-02-25 14:14:22 +00:00
class CompilerContext;
2015-09-21 16:55:58 +00:00
class VariableDeclaration;
2015-02-25 14:14:22 +00:00
/**
* Abstract class used to retrieve, delete and store data in lvalues/variables.
*/
class LValue
{
protected:
2015-10-14 13:19:50 +00:00
explicit LValue(CompilerContext& _compilerContext, Type const* _dataType = nullptr):
2015-02-25 14:14:22 +00:00
m_context(_compilerContext), m_dataType(_dataType) {}
public:
virtual ~LValue() = default;
2015-03-03 16:55:28 +00:00
/// @returns the number of stack slots occupied by the lvalue reference
virtual unsigned sizeOnStack() const { return 1; }
2015-02-25 14:14:22 +00:00
/// Copies the value of the current lvalue to the top of the stack and, if @a _remove is true,
/// also removes the reference from the stack.
/// @a _location source location of the current expression, used for error reporting.
virtual void retrieveValue(langutil::SourceLocation const& _location, bool _remove = false) const = 0;
2015-02-25 14:14:22 +00:00
/// Moves a value from the stack to the lvalue. Removes the value if @a _move is true.
/// @a _location is the source location of the expression that caused this operation.
/// Stack pre: value [lvalue_ref]
2015-02-25 15:00:23 +00:00
/// Stack post: if !_move: value_of(lvalue_ref)
2015-02-25 14:14:22 +00:00
virtual void storeValue(Type const& _sourceType,
langutil::SourceLocation const& _location = {}, bool _move = false) const = 0;
2015-02-25 19:27:55 +00:00
/// Stores zero in the lvalue. Removes the reference from the stack if @a _removeReference is true.
2015-02-25 14:14:22 +00:00
/// @a _location is the source location of the requested operation
2015-02-25 19:27:55 +00:00
virtual void setToZero(
langutil::SourceLocation const& _location = {},
2015-03-05 14:41:39 +00:00
bool _removeReference = true
) const = 0;
2015-02-25 14:14:22 +00:00
protected:
CompilerContext& m_context;
2015-10-14 13:19:50 +00:00
Type const* m_dataType;
2015-02-25 14:14:22 +00:00
};
/**
* Local variable that is completely stored on the stack.
*/
class StackVariable: public LValue
{
public:
2015-09-21 16:55:58 +00:00
StackVariable(CompilerContext& _compilerContext, VariableDeclaration const& _declaration);
2015-02-25 14:14:22 +00:00
unsigned sizeOnStack() const override { return 0; }
void retrieveValue(langutil::SourceLocation const& _location, bool _remove = false) const override;
2015-03-05 14:41:39 +00:00
virtual void storeValue(
Type const& _sourceType,
langutil::SourceLocation const& _location = {},
2015-03-05 14:41:39 +00:00
bool _move = false
) const override;
2015-02-25 19:27:55 +00:00
virtual void setToZero(
langutil::SourceLocation const& _location = {},
2015-03-05 14:41:39 +00:00
bool _removeReference = true
) const override;
2015-02-25 14:14:22 +00:00
private:
2015-08-31 16:44:29 +00:00
/// Base stack offset (@see CompilerContext::baseStackOffsetOfVariable) of the local variable.
2015-02-25 14:14:22 +00:00
unsigned m_baseStackOffset;
/// Number of stack elements occupied by the value (not the reference).
unsigned m_size;
};
/**
* Reference to some item in memory.
*/
class MemoryItem: public LValue
{
public:
2015-06-30 09:54:51 +00:00
MemoryItem(CompilerContext& _compilerContext, Type const& _type, bool _padded = true);
unsigned sizeOnStack() const override { return 1; }
void retrieveValue(langutil::SourceLocation const& _location, bool _remove = false) const override;
virtual void storeValue(
Type const& _sourceType,
langutil::SourceLocation const& _location = {},
bool _move = false
) const override;
virtual void setToZero(
langutil::SourceLocation const& _location = {},
bool _removeReference = true
) const override;
private:
/// Special flag to deal with byte array elements.
bool m_padded = false;
};
2015-02-25 14:14:22 +00:00
/**
* Reference to some item in storage. On the stack this is <storage key> <offset_inside_value>,
* where 0 <= offset_inside_value < 32 and an offset of i means that the value is multiplied
* by 2**i before storing it.
2015-02-25 14:14:22 +00:00
*/
class StorageItem: public LValue
{
public:
/// Constructs the LValue and pushes the location of @a _declaration onto the stack.
2015-09-21 16:55:58 +00:00
StorageItem(CompilerContext& _compilerContext, VariableDeclaration const& _declaration);
2015-02-25 14:14:22 +00:00
/// Constructs the LValue and assumes that the storage reference is already on the stack.
2015-02-25 19:27:55 +00:00
StorageItem(CompilerContext& _compilerContext, Type const& _type);
unsigned sizeOnStack() const override { return 2; }
void retrieveValue(langutil::SourceLocation const& _location, bool _remove = false) const override;
2015-03-05 14:41:39 +00:00
virtual void storeValue(
Type const& _sourceType,
langutil::SourceLocation const& _location = {},
2015-03-05 14:41:39 +00:00
bool _move = false
) const override;
2015-02-25 19:27:55 +00:00
virtual void setToZero(
langutil::SourceLocation const& _location = {},
2015-03-05 14:41:39 +00:00
bool _removeReference = true
) const override;
2015-02-25 14:14:22 +00:00
};
2015-03-03 16:55:28 +00:00
/**
* Reference to a single byte inside a storage byte array.
* Stack: <storage_ref> <byte_number>
*/
class StorageByteArrayElement: public LValue
{
public:
/// Constructs the LValue and assumes that the storage reference is already on the stack.
StorageByteArrayElement(CompilerContext& _compilerContext);
unsigned sizeOnStack() const override { return 2; }
void retrieveValue(langutil::SourceLocation const& _location, bool _remove = false) const override;
2015-03-05 14:41:39 +00:00
virtual void storeValue(
Type const& _sourceType,
langutil::SourceLocation const& _location = {},
2015-03-05 14:41:39 +00:00
bool _move = false
) const override;
2015-03-03 16:55:28 +00:00
virtual void setToZero(
langutil::SourceLocation const& _location = {},
2015-03-05 14:41:39 +00:00
bool _removeReference = true
) const override;
2015-03-03 16:55:28 +00:00
};
2015-10-14 13:19:50 +00:00
/**
* Tuple object that can itself hold several LValues.
*/
class TupleObject: public LValue
{
public:
/// Constructs the LValue assuming that the other LValues are present on the stack.
/// Empty unique_ptrs are possible if e.g. some values should be ignored during assignment.
TupleObject(CompilerContext& _compilerContext, std::vector<std::unique_ptr<LValue>>&& _lvalues);
unsigned sizeOnStack() const override;
void retrieveValue(langutil::SourceLocation const& _location, bool _remove = false) const override;
2015-10-14 13:19:50 +00:00
virtual void storeValue(
Type const& _sourceType,
langutil::SourceLocation const& _location = {},
2015-10-14 13:19:50 +00:00
bool _move = false
) const override;
virtual void setToZero(
langutil::SourceLocation const& _location = {},
2015-10-14 13:19:50 +00:00
bool _removeReference = true
) const override;
private:
std::vector<std::unique_ptr<LValue>> m_lvalues;
};
2015-02-25 14:14:22 +00:00
}