2015-02-25 19:27:55 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2015-02-25 19:27:55 +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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2015-02-25 19:27:55 +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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2015-02-25 19:27:55 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2015
|
|
|
|
* Code generation utils that handle arrays.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-01-24 11:06:11 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
|
|
|
|
class CompilerContext;
|
|
|
|
class Type;
|
|
|
|
class ArrayType;
|
2017-01-24 11:06:11 +00:00
|
|
|
using TypePointer = std::shared_ptr<Type const>;
|
2015-02-25 19:27:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class that provides code generation for handling arrays.
|
|
|
|
*/
|
|
|
|
class ArrayUtils
|
|
|
|
{
|
|
|
|
public:
|
2017-08-21 14:42:17 +00:00
|
|
|
explicit ArrayUtils(CompilerContext& _context): m_context(_context) {}
|
2015-02-25 19:27:55 +00:00
|
|
|
|
2015-02-27 21:52:02 +00:00
|
|
|
/// Copies an array to an array in storage. The arrays can be of different types only if
|
|
|
|
/// their storage representation is the same.
|
2015-07-14 09:58:16 +00:00
|
|
|
/// Stack pre: source_reference [source_length] target_reference
|
|
|
|
/// Stack post: target_reference
|
2015-02-25 19:27:55 +00:00
|
|
|
void copyArrayToStorage(ArrayType const& _targetType, ArrayType const& _sourceType) const;
|
2015-06-25 15:51:01 +00:00
|
|
|
/// Copies the data part of an array (which cannot be dynamically nested) from anywhere
|
|
|
|
/// to a given position in memory.
|
|
|
|
/// This always copies contained data as is (i.e. structs and fixed-size arrays are copied in
|
|
|
|
/// place as required by the ABI encoding). Use CompilerUtils::convertType if you want real
|
|
|
|
/// memory copies of nested arrays.
|
2015-06-23 12:55:33 +00:00
|
|
|
/// Stack pre: memory_offset source_item
|
|
|
|
/// Stack post: memory_offest + length(padded)
|
|
|
|
void copyArrayToMemory(ArrayType const& _sourceType, bool _padToWordBoundaries = true) const;
|
2015-02-25 19:27:55 +00:00
|
|
|
/// Clears the given dynamic or static array.
|
2015-03-11 17:09:35 +00:00
|
|
|
/// Stack pre: storage_ref storage_byte_offset
|
2015-02-25 19:27:55 +00:00
|
|
|
/// Stack post:
|
|
|
|
void clearArray(ArrayType const& _type) const;
|
|
|
|
/// Clears the length and data elements of the array referenced on the stack.
|
2015-03-11 17:09:35 +00:00
|
|
|
/// Stack pre: reference (excludes byte offset)
|
2015-02-25 19:27:55 +00:00
|
|
|
/// Stack post:
|
|
|
|
void clearDynamicArray(ArrayType const& _type) const;
|
|
|
|
/// Changes the size of a dynamic array and clears the tail if it is shortened.
|
2015-03-11 17:09:35 +00:00
|
|
|
/// Stack pre: reference (excludes byte offset) new_length
|
2015-02-25 19:27:55 +00:00
|
|
|
/// Stack post:
|
|
|
|
void resizeDynamicArray(ArrayType const& _type) const;
|
2018-03-08 14:38:14 +00:00
|
|
|
/// Increments the size of a dynamic array by one.
|
|
|
|
/// Does not touch the new data element. In case of a byte array, this might move the
|
|
|
|
/// data.
|
|
|
|
/// Stack pre: reference (excludes byte offset)
|
|
|
|
/// Stack post: new_length
|
|
|
|
void incrementDynamicArraySize(ArrayType const& _type) const;
|
2018-05-16 15:18:30 +00:00
|
|
|
/// Decrements the size of a dynamic array by one if length is nonzero. Causes an invalid instruction otherwise.
|
2018-03-09 16:46:24 +00:00
|
|
|
/// Clears the removed data element. In case of a byte array, this might move the data.
|
|
|
|
/// Stack pre: reference
|
|
|
|
/// Stack post:
|
|
|
|
void popStorageArrayElement(ArrayType const& _type) const;
|
2015-02-25 19:27:55 +00:00
|
|
|
/// Appends a loop that clears a sequence of storage slots of the given type (excluding end).
|
|
|
|
/// Stack pre: end_ref start_ref
|
|
|
|
/// Stack post: end_ref
|
2017-01-24 11:06:11 +00:00
|
|
|
void clearStorageLoop(TypePointer const& _type) const;
|
2015-03-05 17:22:17 +00:00
|
|
|
/// Converts length to size (number of storage slots or calldata/memory bytes).
|
|
|
|
/// if @a _pad then add padding to multiples of 32 bytes for calldata/memory.
|
2015-02-25 19:27:55 +00:00
|
|
|
/// Stack pre: length
|
|
|
|
/// Stack post: size
|
2015-03-05 17:22:17 +00:00
|
|
|
void convertLengthToSize(ArrayType const& _arrayType, bool _pad = false) const;
|
2015-02-27 21:52:02 +00:00
|
|
|
/// Retrieves the length (number of elements) of the array ref on the stack. This also
|
|
|
|
/// works for statically-sized arrays.
|
2015-09-25 15:13:29 +00:00
|
|
|
/// @param _stackDepth number of stack elements between top of stack and top (!) of reference
|
2015-03-11 17:09:35 +00:00
|
|
|
/// Stack pre: reference (excludes byte offset for dynamic storage arrays)
|
2015-02-27 21:52:02 +00:00
|
|
|
/// Stack post: reference length
|
2015-09-25 15:13:29 +00:00
|
|
|
void retrieveLength(ArrayType const& _arrayType, unsigned _stackDepth = 0) const;
|
|
|
|
/// Stores the length of an array of type @a _arrayType in storage. The length itself is stored
|
|
|
|
/// on the stack at position @a _stackDepthLength and the storage reference at @a _stackDepthRef.
|
|
|
|
/// If @a _arrayType is a byte array, takes tight coding into account.
|
|
|
|
void storeLength(ArrayType const& _arrayType, unsigned _stackDepthLength = 0, unsigned _stackDepthRef = 1) const;
|
2019-02-13 08:22:45 +00:00
|
|
|
/// Checks whether the index is out of range and returns the absolute offset of the element reference[index]
|
|
|
|
/// (i.e. reference + index * size_of_base_type).
|
|
|
|
/// If @a _keepReference is true, the base reference to the beginning of the array is kept on the stack.
|
2015-03-30 17:31:57 +00:00
|
|
|
/// Stack pre: reference [length] index
|
2019-02-13 08:22:45 +00:00
|
|
|
/// Stack post (storage): [reference] storage_slot byte_offset
|
|
|
|
/// Stack post: [reference] memory/calldata_offset
|
|
|
|
void accessIndex(ArrayType const& _arrayType, bool _doBoundsCheck = true, bool _keepReference = false) const;
|
2015-02-25 19:27:55 +00:00
|
|
|
|
|
|
|
private:
|
2015-03-16 16:52:19 +00:00
|
|
|
/// Adds the given number of bytes to a storage byte offset counter and also increments
|
|
|
|
/// the storage offset if adding this number again would increase the counter over 32.
|
|
|
|
/// @param byteOffsetPosition the stack offset of the storage byte offset
|
|
|
|
/// @param storageOffsetPosition the stack offset of the storage slot offset
|
2015-03-19 17:15:16 +00:00
|
|
|
void incrementByteOffset(unsigned _byteSize, unsigned _byteOffsetPosition, unsigned _storageOffsetPosition) const;
|
2015-03-16 16:52:19 +00:00
|
|
|
|
2015-02-25 19:27:55 +00:00
|
|
|
CompilerContext& m_context;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|