aboutsummaryrefslogtreecommitdiffstats
path: root/Types.h
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2015-06-05 17:07:50 +0800
committerchriseth <c@ethdev.com>2015-06-05 20:44:05 +0800
commitf4d1acc563a972ee4f5a44c690cd3fdd1783ae97 (patch)
treeb2814613dff69ffe8f1e14ef6160dcc0902eb3c4 /Types.h
parent4987eec3d1e87868e091850d31af58e054ab5ee5 (diff)
downloaddexon-solidity-f4d1acc563a972ee4f5a44c690cd3fdd1783ae97.tar.gz
dexon-solidity-f4d1acc563a972ee4f5a44c690cd3fdd1783ae97.tar.zst
dexon-solidity-f4d1acc563a972ee4f5a44c690cd3fdd1783ae97.zip
Ability to specify the storage location of a reference type.
Diffstat (limited to 'Types.h')
-rw-r--r--Types.h41
1 files changed, 29 insertions, 12 deletions
diff --git a/Types.h b/Types.h
index 65f6e447..3ec92539 100644
--- a/Types.h
+++ b/Types.h
@@ -354,33 +354,50 @@ public:
};
/**
+ * Trait used by types which are not value types and can be stored either in storage, memory
+ * or calldata. This is currently used by arrays and structs.
+ */
+class ReferenceType
+{
+public:
+ enum class Location { Storage, CallData, Memory };
+ explicit ReferenceType(Location _location): m_location(_location) {}
+ Location location() const { return m_location; }
+
+ /// @returns a copy of this type with location (recursively) changed to @a _location.
+ virtual TypePointer copyForLocation(Location _location) const = 0;
+
+protected:
+ Location m_location = Location::Storage;
+};
+
+/**
* The type of an array. The flavours are byte array (bytes), statically- (<type>[<length>])
* and dynamically-sized array (<type>[]).
* In storage, all arrays are packed tightly (as long as more than one elementary type fits in
* one slot). Dynamically sized arrays (including byte arrays) start with their size as a uint and
* thus start on their own slot.
*/
-class ArrayType: public Type
+class ArrayType: public Type, public ReferenceType
{
public:
- enum class Location { Storage, CallData, Memory };
virtual Category getCategory() const override { return Category::Array; }
/// Constructor for a byte array ("bytes") and string.
explicit ArrayType(Location _location, bool _isString = false):
- m_location(_location),
+ ReferenceType(_location),
m_arrayKind(_isString ? ArrayKind::String : ArrayKind::Bytes),
m_baseType(std::make_shared<FixedBytesType>(1))
{}
/// Constructor for a dynamically sized array type ("type[]")
ArrayType(Location _location, const TypePointer &_baseType):
- m_location(_location),
+ ReferenceType(_location),
m_baseType(_baseType)
{}
/// Constructor for a fixed-size array type ("type[20]")
ArrayType(Location _location, const TypePointer &_baseType, u256 const& _length):
- m_location(_location),
+ ReferenceType(_location),
m_baseType(_baseType),
m_hasDynamicLength(false),
m_length(_length)
@@ -400,7 +417,6 @@ public:
}
virtual TypePointer externalType() const override;
- Location getLocation() const { return m_location; }
/// @returns true if this is a byte array or a string
bool isByteArray() const { return m_arrayKind != ArrayKind::Ordinary; }
/// @returns true if this is a string
@@ -408,15 +424,12 @@ public:
TypePointer const& getBaseType() const { solAssert(!!m_baseType, ""); return m_baseType;}
u256 const& getLength() const { return m_length; }
- /// @returns a copy of this type with location changed to @a _location
- /// @todo this might move as far up as Type later
- std::shared_ptr<ArrayType> copyForLocation(Location _location) const;
+ TypePointer copyForLocation(Location _location) const override;
private:
/// String is interpreted as a subtype of Bytes.
enum class ArrayKind { Ordinary, Bytes, String };
- Location m_location;
///< Byte arrays ("bytes") and strings have different semantics from ordinary arrays.
ArrayKind m_arrayKind = ArrayKind::Ordinary;
TypePointer m_baseType;
@@ -484,11 +497,13 @@ private:
/**
* The type of a struct instance, there is one distinct type per struct definition.
*/
-class StructType: public Type
+class StructType: public Type, public ReferenceType
{
public:
virtual Category getCategory() const override { return Category::Struct; }
- explicit StructType(StructDefinition const& _struct): m_struct(_struct) {}
+ explicit StructType(StructDefinition const& _struct):
+ //@todo only storage until we have non-storage structs
+ ReferenceType(Location::Storage), m_struct(_struct) {}
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
virtual bool operator==(Type const& _other) const override;
virtual u256 getStorageSize() const override;
@@ -498,6 +513,8 @@ public:
virtual MemberList const& getMembers() const override;
+ TypePointer copyForLocation(Location _location) const override;
+
std::pair<u256, unsigned> const& getStorageOffsetsOfMember(std::string const& _name) const;
private: