aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <c@ethdev.com>2015-06-09 20:26:08 +0800
committerchriseth <c@ethdev.com>2015-06-15 20:40:41 +0800
commit258b1a74e214a69b06e603849c76362aecfae0d5 (patch)
tree9d6794a4e0c2af5175f22bb5b2cdd258f0323031
parentd60ef3f2d792989ebcbf3326bcf8fced6031b5b9 (diff)
downloaddexon-solidity-258b1a74e214a69b06e603849c76362aecfae0d5.tar.gz
dexon-solidity-258b1a74e214a69b06e603849c76362aecfae0d5.tar.zst
dexon-solidity-258b1a74e214a69b06e603849c76362aecfae0d5.zip
Distinction between storage pointer and storage ref and type checking for conversion between storage and memory.
-rw-r--r--AST.cpp38
-rw-r--r--AST.h80
-rw-r--r--Compiler.cpp45
-rw-r--r--Compiler.h5
-rw-r--r--ExpressionCompiler.cpp29
-rw-r--r--InterfaceHandler.cpp2
-rw-r--r--LValue.cpp6
-rw-r--r--NameAndTypeResolver.cpp14
-rw-r--r--Types.cpp156
-rw-r--r--Types.h103
10 files changed, 327 insertions, 151 deletions
diff --git a/AST.cpp b/AST.cpp
index 5be23b7c..dbeec858 100644
--- a/AST.cpp
+++ b/AST.cpp
@@ -488,7 +488,7 @@ string FunctionDefinition::externalSignature() const
bool VariableDeclaration::isLValue() const
{
// External function parameters and constant declared variables are Read-Only
- return !isExternalFunctionParameter() && !m_isConstant;
+ return !isExternalCallableParameter() && !m_isConstant;
}
void VariableDeclaration::checkTypeRequirements()
@@ -516,39 +516,41 @@ void VariableDeclaration::checkTypeRequirements()
BOOST_THROW_EXCEPTION(createTypeError("Assignment necessary for type detection."));
m_value->checkTypeRequirements(nullptr);
- TypePointer type = m_value->getType();
- if (type->getCategory() == Type::Category::IntegerConstant)
- {
- auto intType = dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType();
- if (!intType)
- BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString() + "."));
- type = intType;
- }
+ TypePointer const& type = m_value->getType();
+ if (
+ type->getCategory() == Type::Category::IntegerConstant &&
+ !dynamic_pointer_cast<IntegerConstantType const>(type)->getIntegerType()
+ )
+ BOOST_THROW_EXCEPTION(m_value->createTypeError("Invalid integer constant " + type->toString() + "."));
else if (type->getCategory() == Type::Category::Void)
BOOST_THROW_EXCEPTION(createTypeError("Variable cannot have void type."));
- m_type = type;
+ m_type = type->mobileType();
}
if (m_isStateVariable && getVisibility() >= Visibility::Public && !FunctionType(*this).externalType())
BOOST_THROW_EXCEPTION(createTypeError("Internal type is not allowed for public state variables."));
}
-bool VariableDeclaration::isFunctionParameter() const
+bool VariableDeclaration::isCallableParameter() const
{
- auto const* function = dynamic_cast<FunctionDefinition const*>(getScope());
- if (!function)
+ auto const* callable = dynamic_cast<CallableDeclaration const*>(getScope());
+ if (!callable)
return false;
- for (auto const& variable: function->getParameters() + function->getReturnParameters())
+ for (auto const& variable: callable->getParameters())
if (variable.get() == this)
return true;
+ if (callable->getReturnParameterList())
+ for (auto const& variable: callable->getReturnParameterList()->getParameters())
+ if (variable.get() == this)
+ return true;
return false;
}
-bool VariableDeclaration::isExternalFunctionParameter() const
+bool VariableDeclaration::isExternalCallableParameter() const
{
- auto const* function = dynamic_cast<FunctionDefinition const*>(getScope());
- if (!function || function->getVisibility() != Declaration::Visibility::External)
+ auto const* callable = dynamic_cast<CallableDeclaration const*>(getScope());
+ if (!callable || callable->getVisibility() != Declaration::Visibility::External)
return false;
- for (auto const& variable: function->getParameters())
+ for (auto const& variable: callable->getParameters())
if (variable.get() == this)
return true;
return false;
diff --git a/AST.h b/AST.h
index b3984840..5f1c1281 100644
--- a/AST.h
+++ b/AST.h
@@ -406,13 +406,43 @@ private:
std::vector<ASTPointer<VariableDeclaration>> m_parameters;
};
-class FunctionDefinition: public Declaration, public VariableScope, public Documented, public ImplementationOptional
+/**
+ * Base class for all nodes that define function-like objects, i.e. FunctionDefinition,
+ * EventDefinition and ModifierDefinition.
+ */
+class CallableDeclaration: public Declaration, public VariableScope
+{
+public:
+ CallableDeclaration(
+ SourceLocation const& _location,
+ ASTPointer<ASTString> const& _name,
+ Declaration::Visibility _visibility,
+ ASTPointer<ParameterList> const& _parameters,
+ ASTPointer<ParameterList> const& _returnParameters = ASTPointer<ParameterList>()
+ ):
+ Declaration(_location, _name, _visibility),
+ m_parameters(_parameters),
+ m_returnParameters(_returnParameters)
+ {
+ }
+
+ std::vector<ASTPointer<VariableDeclaration>> const& getParameters() const { return m_parameters->getParameters(); }
+ ParameterList const& getParameterList() const { return *m_parameters; }
+ ASTPointer<ParameterList> const& getReturnParameterList() const { return m_returnParameters; }
+
+protected:
+ ASTPointer<ParameterList> m_parameters;
+ ASTPointer<ParameterList> m_returnParameters;
+};
+
+class FunctionDefinition: public CallableDeclaration, public Documented, public ImplementationOptional
{
public:
FunctionDefinition(
SourceLocation const& _location,
ASTPointer<ASTString> const& _name,
- Declaration::Visibility _visibility, bool _isConstructor,
+ Declaration::Visibility _visibility,
+ bool _isConstructor,
ASTPointer<ASTString> const& _documentation,
ASTPointer<ParameterList> const& _parameters,
bool _isDeclaredConst,
@@ -420,14 +450,12 @@ public:
ASTPointer<ParameterList> const& _returnParameters,
ASTPointer<Block> const& _body
):
- Declaration(_location, _name, _visibility),
+ CallableDeclaration(_location, _name, _visibility, _parameters, _returnParameters),
Documented(_documentation),
ImplementationOptional(_body != nullptr),
m_isConstructor(_isConstructor),
- m_parameters(_parameters),
m_isDeclaredConst(_isDeclaredConst),
m_functionModifiers(_modifiers),
- m_returnParameters(_returnParameters),
m_body(_body)
{}
@@ -437,10 +465,7 @@ public:
bool isConstructor() const { return m_isConstructor; }
bool isDeclaredConst() const { return m_isDeclaredConst; }
std::vector<ASTPointer<ModifierInvocation>> const& getModifiers() const { return m_functionModifiers; }
- std::vector<ASTPointer<VariableDeclaration>> const& getParameters() const { return m_parameters->getParameters(); }
- ParameterList const& getParameterList() const { return *m_parameters; }
std::vector<ASTPointer<VariableDeclaration>> const& getReturnParameters() const { return m_returnParameters->getParameters(); }
- ASTPointer<ParameterList> const& getReturnParameterList() const { return m_returnParameters; }
Block const& getBody() const { return *m_body; }
virtual bool isVisibleInContract() const override
@@ -460,10 +485,8 @@ public:
private:
bool m_isConstructor;
- ASTPointer<ParameterList> m_parameters;
bool m_isDeclaredConst;
std::vector<ASTPointer<ModifierInvocation>> m_functionModifiers;
- ASTPointer<ParameterList> m_returnParameters;
ASTPointer<Block> m_body;
};
@@ -512,9 +535,9 @@ public:
void checkTypeRequirements();
bool isLocalVariable() const { return !!dynamic_cast<FunctionDefinition const*>(getScope()); }
/// @returns true if this variable is a parameter or return parameter of a function.
- bool isFunctionParameter() const;
+ bool isCallableParameter() const;
/// @returns true if this variable is a parameter (not return parameter) of an external function.
- bool isExternalFunctionParameter() const;
+ bool isExternalCallableParameter() const;
bool isStateVariable() const { return m_isStateVariable; }
bool isIndexed() const { return m_isIndexed; }
bool isConstant() const { return m_isConstant; }
@@ -537,22 +560,24 @@ private:
/**
* Definition of a function modifier.
*/
-class ModifierDefinition: public Declaration, public VariableScope, public Documented
+class ModifierDefinition: public CallableDeclaration, public Documented
{
public:
ModifierDefinition(SourceLocation const& _location,
- ASTPointer<ASTString> const& _name,
- ASTPointer<ASTString> const& _documentation,
- ASTPointer<ParameterList> const& _parameters,
- ASTPointer<Block> const& _body):
- Declaration(_location, _name), Documented(_documentation),
- m_parameters(_parameters), m_body(_body) {}
+ ASTPointer<ASTString> const& _name,
+ ASTPointer<ASTString> const& _documentation,
+ ASTPointer<ParameterList> const& _parameters,
+ ASTPointer<Block> const& _body
+ ):
+ CallableDeclaration(_location, _name, Visibility::Default, _parameters),
+ Documented(_documentation),
+ 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; }
virtual TypePointer getType(ContractDefinition const* = nullptr) const override;
@@ -560,7 +585,6 @@ public:
void checkTypeRequirements();
private:
- ASTPointer<ParameterList> m_parameters;
ASTPointer<Block> m_body;
};
@@ -591,7 +615,7 @@ private:
/**
* Definition of a (loggable) event.
*/
-class EventDefinition: public Declaration, public VariableScope, public Documented
+class EventDefinition: public CallableDeclaration, public Documented
{
public:
EventDefinition(
@@ -601,16 +625,15 @@ public:
ASTPointer<ParameterList> const& _parameters,
bool _anonymous = false
):
- Declaration(_location, _name),
+ CallableDeclaration(_location, _name, Visibility::Default, _parameters),
Documented(_documentation),
- m_parameters(_parameters),
- m_anonymous(_anonymous){}
+ m_anonymous(_anonymous)
+ {
+ }
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; }
bool isAnonymous() const { return m_anonymous; }
virtual TypePointer getType(ContractDefinition const* = nullptr) const override
@@ -621,7 +644,6 @@ public:
void checkTypeRequirements();
private:
- ASTPointer<ParameterList> m_parameters;
bool m_anonymous = false;
};
diff --git a/Compiler.cpp b/Compiler.cpp
index b2b8bfa4..b55ae7d3 100644
--- a/Compiler.cpp
+++ b/Compiler.cpp
@@ -170,11 +170,17 @@ void Compiler::appendConstructor(FunctionDefinition const& _constructor)
if (argumentSize > 0)
{
- m_context << u256(argumentSize);
+ CompilerUtils(m_context).fetchFreeMemoryPointer();
+ m_context << u256(argumentSize) << eth::Instruction::DUP1;
m_context.appendProgramSize();
- m_context << u256(CompilerUtils::dataStartOffset); // copy it to byte four as expected for ABI calls
- m_context << eth::Instruction::CODECOPY;
- appendCalldataUnpacker(FunctionType(_constructor).getParameterTypes(), true);
+ m_context << eth::Instruction::DUP4 << eth::Instruction::CODECOPY;
+ m_context << eth::Instruction::ADD;
+ CompilerUtils(m_context).storeFreeMemoryPointer();
+ appendCalldataUnpacker(
+ FunctionType(_constructor).getParameterTypes(),
+ true,
+ CompilerUtils::freeMemoryPointer + 0x20
+ );
}
_constructor.accept(*this);
}
@@ -232,26 +238,35 @@ void Compiler::appendFunctionSelector(ContractDefinition const& _contract)
}
}
-void Compiler::appendCalldataUnpacker(TypePointers const& _typeParameters, bool _fromMemory)
+void Compiler::appendCalldataUnpacker(
+ TypePointers const& _typeParameters,
+ bool _fromMemory,
+ u256 _startOffset
+)
{
// We do not check the calldata size, everything is zero-paddedd
- m_context << u256(CompilerUtils::dataStartOffset);
+ if (_startOffset == u256(-1))
+ _startOffset = u256(CompilerUtils::dataStartOffset);
+
+ m_context << _startOffset;
for (TypePointer const& type: _typeParameters)
{
- if (type->getCategory() == Type::Category::Array)
+ switch (type->getCategory())
+ {
+ case Type::Category::Array:
{
auto const& arrayType = dynamic_cast<ArrayType const&>(*type);
if (arrayType.location() == ReferenceType::Location::CallData)
{
+ solAssert(!_fromMemory, "");
if (type->isDynamicallySized())
{
// put on stack: data_pointer length
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(256), !_fromMemory);
// stack: data_offset next_pointer
//@todo once we support nested arrays, this offset needs to be dynamic.
- m_context << eth::Instruction::SWAP1 << u256(CompilerUtils::dataStartOffset);
- m_context << eth::Instruction::ADD;
+ m_context << eth::Instruction::SWAP1 << _startOffset << eth::Instruction::ADD;
// stack: next_pointer data_pointer
// retrieve length
CompilerUtils(m_context).loadFromMemoryDynamic(IntegerType(256), !_fromMemory, true);
@@ -268,13 +283,15 @@ void Compiler::appendCalldataUnpacker(TypePointers const& _typeParameters, bool
else
{
solAssert(arrayType.location() == ReferenceType::Location::Memory, "");
- CompilerUtils(m_context).fetchFreeMemoryPointer();
- CompilerUtils(m_context).storeInMemoryDynamic(*type);
- CompilerUtils(m_context).storeFreeMemoryPointer();
+ // compute data pointer
+ m_context << eth::Instruction::DUP1 << _startOffset << eth::Instruction::ADD;
+ if (!_fromMemory)
+ solAssert(false, "Not yet implemented.");
+ m_context << eth::Instruction::SWAP1 << u256(0x20) << eth::Instruction::ADD;
}
+ break;
}
- else
- {
+ default:
solAssert(!type->isDynamicallySized(), "Unknown dynamically sized type: " + type->toString());
CompilerUtils(m_context).loadFromMemoryDynamic(*type, !_fromMemory, true);
}
diff --git a/Compiler.h b/Compiler.h
index 670c7467..589efbff 100644
--- a/Compiler.h
+++ b/Compiler.h
@@ -73,7 +73,10 @@ private:
void appendFunctionSelector(ContractDefinition const& _contract);
/// Creates code that unpacks the arguments for the given function represented by a vector of TypePointers.
/// From memory if @a _fromMemory is true, otherwise from call data.
- void appendCalldataUnpacker(TypePointers const& _typeParameters, bool _fromMemory = false);
+ /// Expects source offset on the stack.
+ void appendCalldataUnpacker(TypePointers const& _typeParameters,
+ bool _fromMemory = false,
+ u256 _startOffset = u256(-1));
void appendReturnValuePacker(TypePointers const& _typeParameters);
void registerStateVariables(ContractDefinition const& _contract);
diff --git a/ExpressionCompiler.cpp b/ExpressionCompiler.cpp
index d9b6da14..811ee60e 100644
--- a/ExpressionCompiler.cpp
+++ b/ExpressionCompiler.cpp
@@ -204,7 +204,7 @@ void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type con
}
else if (targetTypeCategory == Type::Category::Enum)
// just clean
- appendTypeConversion(_typeOnStack, *_typeOnStack.getRealType(), true);
+ appendTypeConversion(_typeOnStack, *_typeOnStack.mobileType(), true);
else
{
solAssert(targetTypeCategory == Type::Category::Integer || targetTypeCategory == Type::Category::Contract, "");
@@ -232,6 +232,25 @@ void ExpressionCompiler::appendTypeConversion(Type const& _typeOnStack, Type con
}
}
break;
+ case Type::Category::Array:
+ //@TODO
+ break;
+ case Type::Category::Struct:
+ {
+ solAssert(targetTypeCategory == stackTypeCategory, "");
+ auto& targetType = dynamic_cast<StructType const&>(_targetType);
+ auto& stackType = dynamic_cast<StructType const&>(_typeOnStack);
+ solAssert(
+ targetType.location() == ReferenceType::Location::Storage &&
+ stackType.location() == ReferenceType::Location::Storage,
+ "Non-storage structs not yet implemented."
+ );
+ solAssert(
+ targetType.isPointer(),
+ "Type conversion to non-pointer struct requested."
+ );
+ break;
+ }
default:
// All other types should not be convertible to non-equal types.
solAssert(_typeOnStack == _targetType, "Invalid type conversion requested.");
@@ -771,7 +790,7 @@ void ExpressionCompiler::endVisit(MemberAccess const& _memberAccess)
TypeType const& type = dynamic_cast<TypeType const&>(*_memberAccess.getExpression().getType());
solAssert(
!type.getMembers().membersByName(_memberAccess.getMemberName()).empty(),
- "Invalid member access to " + type.toString()
+ "Invalid member access to " + type.toString(false)
);
if (dynamic_cast<ContractType const*>(type.getActualType().get()))
@@ -1101,7 +1120,7 @@ void ExpressionCompiler::appendExternalFunctionCall(
bool manualFunctionId =
(funKind == FunctionKind::Bare || funKind == FunctionKind::BareCallCode) &&
!_arguments.empty() &&
- _arguments.front()->getType()->getRealType()->getCalldataEncodedSize(false) ==
+ _arguments.front()->getType()->mobileType()->getCalldataEncodedSize(false) ==
CompilerUtils::dataStartOffset;
if (manualFunctionId)
{
@@ -1225,7 +1244,7 @@ void ExpressionCompiler::encodeToMemory(
TypePointers targetTypes = _targetTypes.empty() ? _givenTypes : _targetTypes;
solAssert(targetTypes.size() == _givenTypes.size(), "");
for (TypePointer& t: targetTypes)
- t = t->getRealType()->externalType();
+ t = t->mobileType()->externalType();
// Stack during operation:
// <v1> <v2> ... <vn> <mem_start> <dyn_head_1> ... <dyn_head_r> <end_of_mem>
@@ -1325,7 +1344,7 @@ void ExpressionCompiler::appendExpressionCopyToMemory(Type const& _expectedType,
appendTypeMoveToMemory(_expectedType);
}
else
- appendTypeMoveToMemory(*_expression.getType()->getRealType());
+ appendTypeMoveToMemory(*_expression.getType()->mobileType());
}
void ExpressionCompiler::setLValueFromDeclaration(Declaration const& _declaration, Expression const& _expression)
diff --git a/InterfaceHandler.cpp b/InterfaceHandler.cpp
index a9d54cdc..23b7ff82 100644
--- a/InterfaceHandler.cpp
+++ b/InterfaceHandler.cpp
@@ -96,7 +96,7 @@ unique_ptr<string> InterfaceHandler::getABIInterface(ContractDefinition const& _
{
Json::Value input;
input["name"] = p->getName();
- input["type"] = p->getType()->toString();
+ input["type"] = p->getType()->toString(true);
input["indexed"] = p->isIndexed();
params.append(input);
}
diff --git a/LValue.cpp b/LValue.cpp
index b684e55a..1acf0a3e 100644
--- a/LValue.cpp
+++ b/LValue.cpp
@@ -198,7 +198,11 @@ void StorageItem::storeValue(Type const& _sourceType, SourceLocation const& _loc
// stack layout: source_ref source_offset target_ref target_offset
// note that we have structs, so offsets should be zero and are ignored
auto const& structType = dynamic_cast<StructType const&>(m_dataType);
- solAssert(structType == _sourceType, "Struct assignment with conversion.");
+ solAssert(
+ structType.structDefinition() ==
+ dynamic_cast<StructType const&>(_sourceType).structDefinition(),
+ "Struct assignment with conversion."
+ );
for (auto const& member: structType.getMembers())
{
// assign each member that is not a mapping
diff --git a/NameAndTypeResolver.cpp b/NameAndTypeResolver.cpp
index 22232014..e6079796 100644
--- a/NameAndTypeResolver.cpp
+++ b/NameAndTypeResolver.cpp
@@ -431,7 +431,7 @@ void ReferencesResolver::endVisit(VariableDeclaration& _variable)
// They default to memory for function parameters and storage for local variables.
if (auto ref = dynamic_cast<ReferenceType const*>(type.get()))
{
- if (_variable.isExternalFunctionParameter())
+ if (_variable.isExternalCallableParameter())
{
// force location of external function parameters (not return) to calldata
if (loc != Location::Default)
@@ -439,9 +439,9 @@ void ReferencesResolver::endVisit(VariableDeclaration& _variable)
"Location has to be calldata for external functions "
"(remove the \"memory\" or \"storage\" keyword)."
));
- type = ref->copyForLocation(ReferenceType::Location::CallData);
+ type = ref->copyForLocation(ReferenceType::Location::CallData, true);
}
- else if (_variable.isFunctionParameter() && _variable.getScope()->isPublic())
+ else if (_variable.isCallableParameter() && _variable.getScope()->isPublic())
{
// force locations of public or external function (return) parameters to memory
if (loc == VariableDeclaration::Location::Storage)
@@ -449,16 +449,18 @@ void ReferencesResolver::endVisit(VariableDeclaration& _variable)
"Location has to be memory for publicly visible functions "
"(remove the \"storage\" keyword)."
));
- type = ref->copyForLocation(ReferenceType::Location::Memory);
+ type = ref->copyForLocation(ReferenceType::Location::Memory, true);
}
else
{
if (loc == Location::Default)
- loc = _variable.isFunctionParameter() ? Location::Memory : Location::Storage;
+ loc = _variable.isCallableParameter() ? Location::Memory : Location::Storage;
+ bool isPointer = !_variable.isStateVariable();
type = ref->copyForLocation(
loc == Location::Memory ?
ReferenceType::Location::Memory :
- ReferenceType::Location::Storage
+ ReferenceType::Location::Storage,
+ isPointer
);
}
}
diff --git a/Types.cpp b/Types.cpp
index 6f16f519..65acba84 100644
--- a/Types.cpp
+++ b/Types.cpp
@@ -179,6 +179,8 @@ TypePointer Type::fromMapping(ElementaryTypeName& _keyType, TypeName& _valueType
TypePointer valueType = _valueType.toType();
if (!valueType)
BOOST_THROW_EXCEPTION(_valueType.createTypeError("Invalid type name."));
+ // Convert value type to storage reference.
+ valueType = ReferenceType::copyForLocationIfReference(ReferenceType::Location::Storage, valueType);
return make_shared<MappingType>(keyType, valueType);
}
@@ -288,7 +290,7 @@ bool IntegerType::operator==(Type const& _other) const
return other.m_bits == m_bits && other.m_modifier == m_modifier;
}
-string IntegerType::toString() const
+string IntegerType::toString(bool) const
{
if (isAddress())
return "address";
@@ -488,7 +490,7 @@ bool IntegerConstantType::operator==(Type const& _other) const
return m_value == dynamic_cast<IntegerConstantType const&>(_other).m_value;
}
-string IntegerConstantType::toString() const
+string IntegerConstantType::toString(bool) const
{
return "int_const " + m_value.str();
}
@@ -508,10 +510,10 @@ u256 IntegerConstantType::literalValue(Literal const*) const
return value;
}
-TypePointer IntegerConstantType::getRealType() const
+TypePointer IntegerConstantType::mobileType() const
{
auto intType = getIntegerType();
- solAssert(!!intType, "getRealType called with invalid integer constant " + toString());
+ solAssert(!!intType, "mobileType called with invalid integer constant " + toString(false));
return intType;
}
@@ -668,21 +670,65 @@ TypePointer ContractType::unaryOperatorResult(Token::Value _operator) const
return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer();
}
+TypePointer ReferenceType::copyForLocationIfReference(Location _location, TypePointer const& _type)
+{
+ if (auto type = dynamic_cast<ReferenceType const*>(_type.get()))
+ return type->copyForLocation(_location, false);
+ return _type;
+}
+
+TypePointer ReferenceType::copyForLocationIfReference(TypePointer const& _type) const
+{
+ return copyForLocationIfReference(m_location, _type);
+}
+
+string ReferenceType::stringForReferencePart() const
+{
+ switch (m_location)
+ {
+ case Location::Storage:
+ return string("storage ") + (m_isPointer ? "pointer" : "ref");
+ case Location::CallData:
+ return "calldata";
+ case Location::Memory:
+ return "memory";
+ }
+}
+
bool ArrayType::isImplicitlyConvertibleTo(const Type& _convertTo) const
{
if (_convertTo.getCategory() != getCategory())
return false;
auto& convertTo = dynamic_cast<ArrayType const&>(_convertTo);
- // let us not allow assignment to memory arrays for now
- if (convertTo.location() != Location::Storage)
- return false;
if (convertTo.isByteArray() != isByteArray() || convertTo.isString() != isString())
return false;
- if (!getBaseType()->isImplicitlyConvertibleTo(*convertTo.getBaseType()))
+ // memory/calldata to storage can be converted, but only to a direct storage reference
+ if (convertTo.location() == Location::Storage && location() != Location::Storage && convertTo.isPointer())
return false;
- if (convertTo.isDynamicallySized())
+ if (convertTo.location() == Location::CallData && location() != convertTo.location())
+ return false;
+ if (convertTo.location() == Location::Storage && !convertTo.isPointer())
+ {
+ // Less restrictive conversion, since we need to copy anyway.
+ if (!getBaseType()->isImplicitlyConvertibleTo(*convertTo.getBaseType()))
+ return false;
+ if (convertTo.isDynamicallySized())
+ return true;
+ return !isDynamicallySized() && convertTo.getLength() >= getLength();
+ }
+ else
+ {
+ // Require that the base type is the same, not only convertible.
+ // This disallows assignment of nested arrays from storage to memory for now.
+ if (*getBaseType() != *convertTo.getBaseType())
+ return false;
+ if (isDynamicallySized() != convertTo.isDynamicallySized())
+ return false;
+ // We also require that the size is the same.
+ if (!isDynamicallySized() && getLength() != convertTo.getLength())
+ return false;
return true;
- return !isDynamicallySized() && convertTo.getLength() >= getLength();
+ }
}
TypePointer ArrayType::unaryOperatorResult(Token::Value _operator) const
@@ -698,7 +744,7 @@ bool ArrayType::operator==(Type const& _other) const
return false;
ArrayType const& other = dynamic_cast<ArrayType const&>(_other);
if (
- other.m_location != m_location ||
+ !ReferenceType::operator==(other) ||
other.isByteArray() != isByteArray() ||
other.isString() != isString() ||
other.isDynamicallySized() != isDynamicallySized()
@@ -751,16 +797,23 @@ unsigned ArrayType::getSizeOnStack() const
return 1;
}
-string ArrayType::toString() const
+string ArrayType::toString(bool _short) const
{
+ string ret;
if (isString())
- return "string";
+ ret = "string";
else if (isByteArray())
- return "bytes";
- string ret = getBaseType()->toString() + "[";
- if (!isDynamicallySized())
- ret += getLength().str();
- return ret + "]";
+ ret = "bytes";
+ else
+ {
+ ret = getBaseType()->toString(_short) + "[";
+ if (!isDynamicallySized())
+ ret += getLength().str();
+ ret += "]";
+ }
+ if (!_short)
+ ret += " " + stringForReferencePart();
+ return ret;
}
TypePointer ArrayType::externalType() const
@@ -778,14 +831,12 @@ TypePointer ArrayType::externalType() const
return std::make_shared<ArrayType>(Location::CallData, m_baseType->externalType(), m_length);
}
-TypePointer ArrayType::copyForLocation(ReferenceType::Location _location) const
+TypePointer ArrayType::copyForLocation(ReferenceType::Location _location, bool _isPointer) const
{
auto copy = make_shared<ArrayType>(_location);
+ copy->m_isPointer = _isPointer;
copy->m_arrayKind = m_arrayKind;
- if (auto ref = dynamic_cast<ReferenceType const*>(m_baseType.get()))
- copy->m_baseType = ref->copyForLocation(_location);
- else
- copy->m_baseType = m_baseType;
+ copy->m_baseType = copy->copyForLocationIfReference(m_baseType);
copy->m_hasDynamicLength = m_hasDynamicLength;
copy->m_length = m_length;
return copy;
@@ -801,7 +852,7 @@ bool ContractType::operator==(Type const& _other) const
return other.m_contract == m_contract && other.m_super == m_super;
}
-string ContractType::toString() const
+string ContractType::toString(bool) const
{
return "contract " + string(m_super ? "super " : "") + m_contract.getName();
}
@@ -890,6 +941,19 @@ vector<tuple<VariableDeclaration const*, u256, unsigned>> ContractType::getState
return variablesAndOffsets;
}
+bool StructType::isImplicitlyConvertibleTo(const Type& _convertTo) const
+{
+ if (_convertTo.getCategory() != getCategory())
+ return false;
+ auto& convertTo = dynamic_cast<StructType const&>(_convertTo);
+ // memory/calldata to storage can be converted, but only to a direct storage reference
+ if (convertTo.location() == Location::Storage && location() != Location::Storage && convertTo.isPointer())
+ return false;
+ if (convertTo.location() == Location::CallData && location() != convertTo.location())
+ return false;
+ return this->m_struct == convertTo.m_struct;
+}
+
TypePointer StructType::unaryOperatorResult(Token::Value _operator) const
{
return _operator == Token::Delete ? make_shared<VoidType>() : TypePointer();
@@ -900,7 +964,7 @@ bool StructType::operator==(Type const& _other) const
if (_other.getCategory() != getCategory())
return false;
StructType const& other = dynamic_cast<StructType const&>(_other);
- return other.m_struct == m_struct;
+ return ReferenceType::operator==(other) && other.m_struct == m_struct;
}
u256 StructType::getStorageSize() const
@@ -916,9 +980,12 @@ bool StructType::canLiveOutsideStorage() const
return true;
}
-string StructType::toString() const
+string StructType::toString(bool _short) const
{
- return string("struct ") + m_struct.getName();
+ string ret = "struct " + m_struct.getName();
+ if (!_short)
+ ret += " " + stringForReferencePart();
+ return ret;
}
MemberList const& StructType::getMembers() const
@@ -928,16 +995,23 @@ MemberList const& StructType::getMembers() const
{
MemberList::MemberMap members;
for (ASTPointer<VariableDeclaration> const& variable: m_struct.getMembers())
- members.push_back(MemberList::Member(variable->getName(), variable->getType(), variable.get()));
+ {
+ members.push_back(MemberList::Member(
+ variable->getName(),
+ copyForLocationIfReference(variable->getType()),
+ variable.get())
+ );
+ }
m_members.reset(new MemberList(members));
}
return *m_members;
}
-TypePointer StructType::copyForLocation(ReferenceType::Location _location) const
+TypePointer StructType::copyForLocation(ReferenceType::Location _location, bool _isPointer) const
{
auto copy = make_shared<StructType>(m_struct);
copy->m_location = _location;
+ copy->m_isPointer = _isPointer;
return copy;
}
@@ -970,7 +1044,7 @@ unsigned EnumType::getStorageBytes() const
return dev::bytesRequired(elements - 1);
}
-string EnumType::toString() const
+string EnumType::toString(bool) const
{
return string("enum ") + m_enum.getName();
}
@@ -1114,14 +1188,14 @@ bool FunctionType::operator==(Type const& _other) const
return true;
}
-string FunctionType::toString() const
+string FunctionType::toString(bool _short) const
{
string name = "function (";
for (auto it = m_parameterTypes.begin(); it != m_parameterTypes.end(); ++it)
- name += (*it)->toString() + (it + 1 == m_parameterTypes.end() ? "" : ",");
+ name += (*it)->toString(_short) + (it + 1 == m_parameterTypes.end() ? "" : ",");
name += ") returns (";
for (auto it = m_returnParameterTypes.begin(); it != m_returnParameterTypes.end(); ++it)
- name += (*it)->toString() + (it + 1 == m_returnParameterTypes.end() ? "" : ",");
+ name += (*it)->toString(_short) + (it + 1 == m_returnParameterTypes.end() ? "" : ",");
return name + ")";
}
@@ -1289,7 +1363,7 @@ string FunctionType::externalSignature(std::string const& _name) const
for (auto it = externalParameterTypes.cbegin(); it != externalParameterTypes.cend(); ++it)
{
solAssert(!!(*it), "Parameter should have external type");
- ret += (*it)->toString() + (it + 1 == externalParameterTypes.cend() ? "" : ",");
+ ret += (*it)->toString(true) + (it + 1 == externalParameterTypes.cend() ? "" : ",");
}
return ret + ")";
@@ -1327,7 +1401,7 @@ vector<string> const FunctionType::getParameterTypeNames() const
{
vector<string> names;
for (TypePointer const& t: m_parameterTypes)
- names.push_back(t->toString());
+ names.push_back(t->toString(true));
return names;
}
@@ -1336,7 +1410,7 @@ vector<string> const FunctionType::getReturnParameterTypeNames() const
{
vector<string> names;
for (TypePointer const& t: m_returnParameterTypes)
- names.push_back(t->toString());
+ names.push_back(t->toString(true));
return names;
}
@@ -1358,9 +1432,9 @@ bool MappingType::operator==(Type const& _other) const
return *other.m_keyType == *m_keyType && *other.m_valueType == *m_valueType;
}
-string MappingType::toString() const
+string MappingType::toString(bool _short) const
{
- return "mapping(" + getKeyType()->toString() + " => " + getValueType()->toString() + ")";
+ return "mapping(" + getKeyType()->toString(_short) + " => " + getValueType()->toString(_short) + ")";
}
u256 VoidType::getStorageSize() const
@@ -1445,11 +1519,11 @@ bool ModifierType::operator==(Type const& _other) const
return true;
}
-string ModifierType::toString() const
+string ModifierType::toString(bool _short) const
{
string name = "modifier (";
for (auto it = m_parameterTypes.begin(); it != m_parameterTypes.end(); ++it)
- name += (*it)->toString() + (it + 1 == m_parameterTypes.end() ? "" : ",");
+ name += (*it)->toString(_short) + (it + 1 == m_parameterTypes.end() ? "" : ",");
return name + ")";
}
@@ -1496,7 +1570,7 @@ bool MagicType::operator==(Type const& _other) const
return other.m_kind == m_kind;
}
-string MagicType::toString() const
+string MagicType::toString(bool) const
{
switch (m_kind)
{
diff --git a/Types.h b/Types.h
index 17d30ea6..0f86ac95 100644
--- a/Types.h
+++ b/Types.h
@@ -198,19 +198,24 @@ public:
/// i.e. it behaves differently in lvalue context and in value context.
virtual bool isValueType() const { return false; }
virtual unsigned getSizeOnStack() const { return 1; }
- /// @returns the real type of some types, like e.g: IntegerConstant
- virtual TypePointer getRealType() const { return shared_from_this(); }
+ /// @returns the mobile (in contrast to static) type corresponding to the given type.
+ /// This returns the corresponding integer type for IntegerConstantTypes and the pointer type
+ /// for storage reference types.
+ virtual TypePointer mobileType() const { return shared_from_this(); }
/// Returns the list of all members of this type. Default implementation: no members.
virtual MemberList const& getMembers() const { return EmptyMemberList; }
/// Convenience method, returns the type of the given named member or an empty pointer if no such member exists.
TypePointer getMemberType(std::string const& _name) const { return getMembers().getMemberType(_name); }
- virtual std::string toString() const = 0;
+ virtual std::string toString(bool _short) const = 0;
+ std::string toString() const { return toString(false); }
virtual u256 literalValue(Literal const*) const
{
- BOOST_THROW_EXCEPTION(InternalCompilerError() << errinfo_comment("Literal value requested "
- "for type without literals."));
+ BOOST_THROW_EXCEPTION(
+ InternalCompilerError() <<
+ errinfo_comment("Literal value requested for type without literals.")
+ );
}
/// @returns a type suitable for outside of Solidity, i.e. for contract types it returns address.
@@ -249,7 +254,7 @@ public:
virtual MemberList const& getMembers() const override { return isAddress() ? AddressMemberList : EmptyMemberList; }
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual TypePointer externalType() const override { return shared_from_this(); }
@@ -287,9 +292,9 @@ public:
virtual bool canLiveOutsideStorage() const override { return false; }
virtual unsigned getSizeOnStack() const override { return 1; }
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual u256 literalValue(Literal const* _literal) const override;
- virtual TypePointer getRealType() const override;
+ virtual TypePointer mobileType() const override;
/// @returns the smallest integer type that can hold the value or an empty pointer if not possible.
std::shared_ptr<IntegerType const> getIntegerType() const;
@@ -322,7 +327,7 @@ public:
virtual unsigned getStorageBytes() const override { return m_bytes; }
virtual bool isValueType() const override { return true; }
- virtual std::string toString() const override { return "bytes" + dev::toString(m_bytes); }
+ virtual std::string toString(bool) const override { return "bytes" + dev::toString(m_bytes); }
virtual u256 literalValue(Literal const* _literal) const override;
virtual TypePointer externalType() const override { return shared_from_this(); }
@@ -348,27 +353,51 @@ public:
virtual unsigned getStorageBytes() const override { return 1; }
virtual bool isValueType() const override { return true; }
- virtual std::string toString() const override { return "bool"; }
+ virtual std::string toString(bool) const override { return "bool"; }
virtual u256 literalValue(Literal const* _literal) const override;
virtual TypePointer externalType() const override { return shared_from_this(); }
};
/**
- * Trait used by types which are not value types and can be stored either in storage, memory
+ * Base class 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
+class ReferenceType: public Type
{
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;
+ /// @returns a copy of this type with location (recursively) changed to @a _location,
+ /// whereas isPointer is only shallowly changed - the deep copy is always a bound reference.
+ virtual TypePointer copyForLocation(Location _location, bool _isPointer) const = 0;
+
+ virtual TypePointer mobileType() const override { return copyForLocation(m_location, true); }
+
+ /// Storage references can be pointers or bound references. In general, local variables are of
+ /// pointer type, state variables are bound references. Assignments to pointers or deleting
+ /// them will not modify storage (that will only change the pointer). Assignment from
+ /// non-storage objects to a variable of storage pointer type is not possible.
+ bool isPointer() const { return m_isPointer; }
+
+ bool operator==(ReferenceType const& _other) const
+ {
+ return location() == _other.location() && isPointer() == _other.isPointer();
+ }
+
+ /// @returns a copy of @a _type having the same location as this (and is not a pointer type)
+ /// if _type is a reference type and an unmodified copy of _type otherwise.
+ /// This function is mostly useful to modify inner types appropriately.
+ static TypePointer copyForLocationIfReference(Location _location, TypePointer const& _type);
protected:
+ TypePointer copyForLocationIfReference(TypePointer const& _type) const;
+ /// @returns a human-readable description of the reference part of the type.
+ std::string stringForReferencePart() const;
+
Location m_location = Location::Storage;
+ bool m_isPointer = true;
};
/**
@@ -378,10 +407,9 @@ protected:
* 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, public ReferenceType
+class ArrayType: public ReferenceType
{
public:
-
virtual Category getCategory() const override { return Category::Array; }
/// Constructor for a byte array ("bytes") and string.
@@ -389,16 +417,18 @@ public:
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):
+ ArrayType(Location _location, TypePointer const& _baseType):
ReferenceType(_location),
- m_baseType(_baseType)
- {}
+ m_baseType(copyForLocationIfReference(_baseType))
+ {
+ }
/// Constructor for a fixed-size array type ("type[20]")
- ArrayType(Location _location, const TypePointer &_baseType, u256 const& _length):
+ ArrayType(Location _location, TypePointer const& _baseType, u256 const& _length):
ReferenceType(_location),
- m_baseType(_baseType),
+ m_baseType(copyForLocationIfReference(_baseType)),
m_hasDynamicLength(false),
m_length(_length)
{}
@@ -410,7 +440,7 @@ public:
virtual bool isDynamicallySized() const override { return m_hasDynamicLength; }
virtual u256 getStorageSize() const override;
virtual unsigned getSizeOnStack() const override;
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual MemberList const& getMembers() const override
{
return isString() ? EmptyMemberList : s_arrayTypeMemberList;
@@ -424,7 +454,7 @@ public:
TypePointer const& getBaseType() const { solAssert(!!m_baseType, ""); return m_baseType;}
u256 const& getLength() const { return m_length; }
- TypePointer copyForLocation(Location _location) const override;
+ TypePointer copyForLocation(Location _location, bool _isPointer) const override;
private:
/// String is interpreted as a subtype of Bytes.
@@ -460,7 +490,7 @@ public:
virtual unsigned getStorageBytes() const override { return 20; }
virtual bool canLiveOutsideStorage() const override { return true; }
virtual bool isValueType() const override { return true; }
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual MemberList const& getMembers() const override;
virtual TypePointer externalType() const override
@@ -497,26 +527,29 @@ private:
/**
* The type of a struct instance, there is one distinct type per struct definition.
*/
-class StructType: public Type, public ReferenceType
+class StructType: public ReferenceType
{
public:
virtual Category getCategory() const override { return Category::Struct; }
explicit StructType(StructDefinition const& _struct):
//@todo only storage until we have non-storage structs
ReferenceType(Location::Storage), m_struct(_struct) {}
+ virtual bool isImplicitlyConvertibleTo(const Type& _convertTo) const override;
virtual TypePointer unaryOperatorResult(Token::Value _operator) const override;
virtual bool operator==(Type const& _other) const override;
virtual u256 getStorageSize() const override;
virtual bool canLiveOutsideStorage() const override;
virtual unsigned getSizeOnStack() const override { return 2; }
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual MemberList const& getMembers() const override;
- TypePointer copyForLocation(Location _location) const override;
+ TypePointer copyForLocation(Location _location, bool _isPointer) const override;
std::pair<u256, unsigned> const& getStorageOffsetsOfMember(std::string const& _name) const;
+ StructDefinition const& structDefinition() const { return m_struct; }
+
private:
StructDefinition const& m_struct;
/// List of member types, will be lazy-initialized because of recursive references.
@@ -540,7 +573,7 @@ public:
virtual unsigned getSizeOnStack() const override { return 1; }
virtual unsigned getStorageBytes() const override;
virtual bool canLiveOutsideStorage() const override { return true; }
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual bool isValueType() const override { return true; }
virtual bool isExplicitlyConvertibleTo(Type const& _convertTo) const override;
@@ -649,7 +682,7 @@ public:
std::vector<std::string> const getReturnParameterTypeNames() const;
virtual bool operator==(Type const& _other) const override;
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual bool canBeStored() const override { return false; }
virtual u256 getStorageSize() const override;
virtual bool canLiveOutsideStorage() const override { return false; }
@@ -721,7 +754,7 @@ public:
m_keyType(_keyType), m_valueType(_valueType) {}
virtual bool operator==(Type const& _other) const override;
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
virtual unsigned getSizeOnStack() const override { return 2; }
virtual bool canLiveOutsideStorage() const override { return false; }
@@ -744,7 +777,7 @@ public:
VoidType() {}
virtual TypePointer binaryOperatorResult(Token::Value, TypePointer const&) const override { return TypePointer(); }
- virtual std::string toString() const override { return "void"; }
+ virtual std::string toString(bool) const override { return "void"; }
virtual bool canBeStored() const override { return false; }
virtual u256 getStorageSize() const override;
virtual bool canLiveOutsideStorage() const override { return false; }
@@ -769,7 +802,7 @@ public:
virtual u256 getStorageSize() const override;
virtual bool canLiveOutsideStorage() const override { return false; }
virtual unsigned getSizeOnStack() const override { return 0; }
- virtual std::string toString() const override { return "type(" + m_actualType->toString() + ")"; }
+ virtual std::string toString(bool _short) const override { return "type(" + m_actualType->toString(_short) + ")"; }
virtual MemberList const& getMembers() const override;
private:
@@ -796,7 +829,7 @@ public:
virtual bool canLiveOutsideStorage() const override { return false; }
virtual unsigned getSizeOnStack() const override { return 0; }
virtual bool operator==(Type const& _other) const override;
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
private:
TypePointers m_parameterTypes;
@@ -826,7 +859,7 @@ public:
virtual unsigned getSizeOnStack() const override { return 0; }
virtual MemberList const& getMembers() const override { return m_members; }
- virtual std::string toString() const override;
+ virtual std::string toString(bool _short) const override;
private:
Kind m_kind;