Merge pull request #3589 from ethereum/identifiers

Clean up type identifiers
This commit is contained in:
Alex Beregszaszi
2018-02-26 20:27:26 +01:00
committed by GitHub
3 changed files with 78 additions and 42 deletions
+34 -23
View File
@@ -131,28 +131,28 @@ namespace
string parenthesizeIdentifier(string const& _internal)
{
return "$_" + _internal + "_$";
return "(" + _internal + ")";
}
template <class Range>
string identifierList(Range const&& _list)
{
return parenthesizeIdentifier(boost::algorithm::join(_list, "_$_"));
return parenthesizeIdentifier(boost::algorithm::join(_list, ","));
}
string identifier(TypePointer const& _type)
string richIdentifier(TypePointer const& _type)
{
return _type ? _type->identifier() : "";
return _type ? _type->richIdentifier() : "";
}
string identifierList(vector<TypePointer> const& _list)
{
return identifierList(_list | boost::adaptors::transformed(identifier));
return identifierList(_list | boost::adaptors::transformed(richIdentifier));
}
string identifierList(TypePointer const& _type)
{
return parenthesizeIdentifier(identifier(_type));
return parenthesizeIdentifier(richIdentifier(_type));
}
string identifierList(TypePointer const& _type1, TypePointer const& _type2)
@@ -165,11 +165,22 @@ string identifierList(TypePointer const& _type1, TypePointer const& _type2)
string parenthesizeUserIdentifier(string const& _internal)
{
return parenthesizeIdentifier(boost::algorithm::replace_all_copy(_internal, "$", "$$$"));
return parenthesizeIdentifier(_internal);
}
}
string Type::escapeIdentifier(string const& _identifier)
{
string ret = _identifier;
// FIXME: should be _$$$_
boost::algorithm::replace_all(ret, "$", "$$$");
boost::algorithm::replace_all(ret, ",", "_$_");
boost::algorithm::replace_all(ret, "(", "$_");
boost::algorithm::replace_all(ret, ")", "_$");
return ret;
}
TypePointer Type::fromElementaryTypeName(ElementaryTypeNameToken const& _type)
{
solAssert(Token::isElementaryTypeName(_type.token()),
@@ -334,7 +345,7 @@ IntegerType::IntegerType(int _bits, IntegerType::Modifier _modifier):
);
}
string IntegerType::identifier() const
string IntegerType::richIdentifier() const
{
if (isAddress())
return "t_address";
@@ -504,7 +515,7 @@ FixedPointType::FixedPointType(int _totalBits, int _fractionalDigits, FixedPoint
);
}
string FixedPointType::identifier() const
string FixedPointType::richIdentifier() const
{
return "t_" + string(isSigned() ? "" : "u") + "fixed" + std::to_string(m_totalBits) + "x" + std::to_string(m_fractionalDigits);
}
@@ -936,7 +947,7 @@ TypePointer RationalNumberType::binaryOperatorResult(Token::Value _operator, Typ
}
}
string RationalNumberType::identifier() const
string RationalNumberType::richIdentifier() const
{
return "t_rational_" + m_value.numerator().str() + "_by_" + m_value.denominator().str();
}
@@ -1077,7 +1088,7 @@ bool StringLiteralType::isImplicitlyConvertibleTo(Type const& _convertTo) const
return false;
}
string StringLiteralType::identifier() const
string StringLiteralType::richIdentifier() const
{
// Since we have to return a valid identifier and the string itself may contain
// anything, we hash it.
@@ -1177,7 +1188,7 @@ MemberList::MemberMap FixedBytesType::nativeMembers(const ContractDefinition*) c
return MemberList::MemberMap{MemberList::Member{"length", make_shared<IntegerType>(8)}};
}
string FixedBytesType::identifier() const
string FixedBytesType::richIdentifier() const
{
return "t_bytes" + std::to_string(m_bytes);
}
@@ -1370,7 +1381,7 @@ bool ArrayType::isExplicitlyConvertibleTo(const Type& _convertTo) const
return true;
}
string ArrayType::identifier() const
string ArrayType::richIdentifier() const
{
string id;
if (isString())
@@ -1604,7 +1615,7 @@ TypePointer ArrayType::copyForLocation(DataLocation _location, bool _isPointer)
return copy;
}
string ContractType::identifier() const
string ContractType::richIdentifier() const
{
return (m_super ? "t_super" : "t_contract") + parenthesizeUserIdentifier(m_contract.name()) + std::to_string(m_contract.id());
}
@@ -1756,7 +1767,7 @@ bool StructType::isImplicitlyConvertibleTo(const Type& _convertTo) const
return this->m_struct == convertTo.m_struct;
}
string StructType::identifier() const
string StructType::richIdentifier() const
{
return "t_struct" + parenthesizeUserIdentifier(m_struct.name()) + std::to_string(m_struct.id()) + identifierLocationSuffix();
}
@@ -1988,7 +1999,7 @@ TypePointer EnumType::unaryOperatorResult(Token::Value _operator) const
return _operator == Token::Delete ? make_shared<TupleType>() : TypePointer();
}
string EnumType::identifier() const
string EnumType::richIdentifier() const
{
return "t_enum" + parenthesizeUserIdentifier(m_enum.name()) + std::to_string(m_enum.id());
}
@@ -2074,7 +2085,7 @@ bool TupleType::isImplicitlyConvertibleTo(Type const& _other) const
return false;
}
string TupleType::identifier() const
string TupleType::richIdentifier() const
{
return "t_tuple" + identifierList(components());
}
@@ -2309,7 +2320,7 @@ TypePointers FunctionType::parameterTypes() const
return TypePointers(m_parameterTypes.cbegin() + 1, m_parameterTypes.cend());
}
string FunctionType::identifier() const
string FunctionType::richIdentifier() const
{
string id = "t_function_";
switch (m_kind)
@@ -2802,7 +2813,7 @@ ASTPointer<ASTString> FunctionType::documentation() const
return ASTPointer<ASTString>();
}
string MappingType::identifier() const
string MappingType::richIdentifier() const
{
return "t_mapping" + identifierList(m_keyType, m_valueType);
}
@@ -2825,7 +2836,7 @@ string MappingType::canonicalName() const
return "mapping(" + keyType()->canonicalName() + " => " + valueType()->canonicalName() + ")";
}
string TypeType::identifier() const
string TypeType::richIdentifier() const
{
return "t_type" + identifierList(actualType());
}
@@ -2910,7 +2921,7 @@ u256 ModifierType::storageSize() const
solAssert(false, "Storage size of non-storable type type requested.");
}
string ModifierType::identifier() const
string ModifierType::richIdentifier() const
{
return "t_modifier" + identifierList(m_parameterTypes);
}
@@ -2939,7 +2950,7 @@ string ModifierType::toString(bool _short) const
return name + ")";
}
string ModuleType::identifier() const
string ModuleType::richIdentifier() const
{
return "t_module_" + std::to_string(m_sourceUnit.id());
}
@@ -2965,7 +2976,7 @@ string ModuleType::toString(bool) const
return string("module \"") + m_sourceUnit.annotation().path + string("\"");
}
string MagicType::identifier() const
string MagicType::richIdentifier() const
{
switch (m_kind)
{