2019-04-15 13:33:39 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
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,
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace solidity;
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
BoolType const TypeProvider::m_boolean{};
|
|
|
|
InaccessibleDynamicType const TypeProvider::m_inaccessibleDynamic{};
|
2019-04-16 17:45:25 +00:00
|
|
|
|
|
|
|
/// The string and bytes unique_ptrs are initialized when they are first used because
|
|
|
|
/// they rely on `byte` being available which we cannot guarantee in the static init context.
|
2019-04-17 12:45:29 +00:00
|
|
|
unique_ptr<ArrayType> TypeProvider::m_bytesStorage;
|
|
|
|
unique_ptr<ArrayType> TypeProvider::m_bytesMemory;
|
2019-09-03 16:30:00 +00:00
|
|
|
unique_ptr<ArrayType> TypeProvider::m_bytesCalldata;
|
2019-04-17 12:45:29 +00:00
|
|
|
unique_ptr<ArrayType> TypeProvider::m_stringStorage;
|
|
|
|
unique_ptr<ArrayType> TypeProvider::m_stringMemory;
|
2019-04-16 17:45:25 +00:00
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
TupleType const TypeProvider::m_emptyTuple{};
|
|
|
|
AddressType const TypeProvider::m_payableAddress{StateMutability::Payable};
|
|
|
|
AddressType const TypeProvider::m_address{StateMutability::NonPayable};
|
2019-04-15 16:00:08 +00:00
|
|
|
|
2019-04-16 16:11:43 +00:00
|
|
|
array<unique_ptr<IntegerType>, 32> const TypeProvider::m_intM{{
|
|
|
|
{make_unique<IntegerType>(8 * 1, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 2, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 3, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 4, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 5, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 6, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 7, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 8, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 9, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 10, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 11, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 12, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 13, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 14, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 15, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 16, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 17, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 18, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 19, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 20, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 21, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 22, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 23, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 24, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 25, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 26, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 27, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 28, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 29, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 30, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 31, IntegerType::Modifier::Signed)},
|
|
|
|
{make_unique<IntegerType>(8 * 32, IntegerType::Modifier::Signed)}
|
|
|
|
}};
|
|
|
|
|
|
|
|
array<unique_ptr<IntegerType>, 32> const TypeProvider::m_uintM{{
|
|
|
|
{make_unique<IntegerType>(8 * 1, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 2, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 3, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 4, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 5, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 6, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 7, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 8, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 9, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 10, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 11, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 12, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 13, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 14, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 15, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 16, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 17, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 18, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 19, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 20, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 21, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 22, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 23, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 24, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 25, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 26, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 27, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 28, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 29, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 30, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 31, IntegerType::Modifier::Unsigned)},
|
|
|
|
{make_unique<IntegerType>(8 * 32, IntegerType::Modifier::Unsigned)}
|
|
|
|
}};
|
|
|
|
|
|
|
|
array<unique_ptr<FixedBytesType>, 32> const TypeProvider::m_bytesM{{
|
|
|
|
{make_unique<FixedBytesType>(1)},
|
|
|
|
{make_unique<FixedBytesType>(2)},
|
|
|
|
{make_unique<FixedBytesType>(3)},
|
|
|
|
{make_unique<FixedBytesType>(4)},
|
|
|
|
{make_unique<FixedBytesType>(5)},
|
|
|
|
{make_unique<FixedBytesType>(6)},
|
|
|
|
{make_unique<FixedBytesType>(7)},
|
|
|
|
{make_unique<FixedBytesType>(8)},
|
|
|
|
{make_unique<FixedBytesType>(9)},
|
|
|
|
{make_unique<FixedBytesType>(10)},
|
|
|
|
{make_unique<FixedBytesType>(11)},
|
|
|
|
{make_unique<FixedBytesType>(12)},
|
|
|
|
{make_unique<FixedBytesType>(13)},
|
|
|
|
{make_unique<FixedBytesType>(14)},
|
|
|
|
{make_unique<FixedBytesType>(15)},
|
|
|
|
{make_unique<FixedBytesType>(16)},
|
|
|
|
{make_unique<FixedBytesType>(17)},
|
|
|
|
{make_unique<FixedBytesType>(18)},
|
|
|
|
{make_unique<FixedBytesType>(19)},
|
|
|
|
{make_unique<FixedBytesType>(20)},
|
|
|
|
{make_unique<FixedBytesType>(21)},
|
|
|
|
{make_unique<FixedBytesType>(22)},
|
|
|
|
{make_unique<FixedBytesType>(23)},
|
|
|
|
{make_unique<FixedBytesType>(24)},
|
|
|
|
{make_unique<FixedBytesType>(25)},
|
|
|
|
{make_unique<FixedBytesType>(26)},
|
|
|
|
{make_unique<FixedBytesType>(27)},
|
|
|
|
{make_unique<FixedBytesType>(28)},
|
|
|
|
{make_unique<FixedBytesType>(29)},
|
|
|
|
{make_unique<FixedBytesType>(30)},
|
|
|
|
{make_unique<FixedBytesType>(31)},
|
|
|
|
{make_unique<FixedBytesType>(32)}
|
|
|
|
}};
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
array<unique_ptr<MagicType>, 4> const TypeProvider::m_magics{{
|
2019-04-16 16:11:43 +00:00
|
|
|
{make_unique<MagicType>(MagicType::Kind::Block)},
|
|
|
|
{make_unique<MagicType>(MagicType::Kind::Message)},
|
|
|
|
{make_unique<MagicType>(MagicType::Kind::Transaction)},
|
|
|
|
{make_unique<MagicType>(MagicType::Kind::ABI)}
|
2019-04-15 13:33:39 +00:00
|
|
|
// MetaType is stored separately
|
2019-04-16 16:11:43 +00:00
|
|
|
}};
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
inline void clearCache(Type const& type)
|
|
|
|
{
|
|
|
|
type.clearCache();
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:11:43 +00:00
|
|
|
template <typename T>
|
|
|
|
inline void clearCache(unique_ptr<T> const& type)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
2019-04-16 17:45:25 +00:00
|
|
|
// Some lazy-initialized types might not exist yet.
|
|
|
|
if (type)
|
|
|
|
type->clearCache();
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Container>
|
2019-04-16 16:11:43 +00:00
|
|
|
inline void clearCaches(Container& container)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
2019-04-16 16:11:43 +00:00
|
|
|
for (auto const& e: container)
|
|
|
|
clearCache(e);
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TypeProvider::reset()
|
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
clearCache(m_boolean);
|
|
|
|
clearCache(m_inaccessibleDynamic);
|
|
|
|
clearCache(m_bytesStorage);
|
|
|
|
clearCache(m_bytesMemory);
|
2019-09-03 16:30:00 +00:00
|
|
|
clearCache(m_bytesCalldata);
|
2019-04-17 11:40:50 +00:00
|
|
|
clearCache(m_stringStorage);
|
|
|
|
clearCache(m_stringMemory);
|
|
|
|
clearCache(m_emptyTuple);
|
|
|
|
clearCache(m_payableAddress);
|
|
|
|
clearCache(m_address);
|
2019-04-16 16:11:43 +00:00
|
|
|
clearCaches(instance().m_intM);
|
|
|
|
clearCaches(instance().m_uintM);
|
|
|
|
clearCaches(instance().m_bytesM);
|
2019-04-17 11:40:50 +00:00
|
|
|
clearCaches(instance().m_magics);
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
instance().m_generalTypes.clear();
|
|
|
|
instance().m_stringLiteralTypes.clear();
|
|
|
|
instance().m_ufixedMxN.clear();
|
|
|
|
instance().m_fixedMxN.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename... Args>
|
|
|
|
inline T const* TypeProvider::createAndGet(Args&& ... _args)
|
|
|
|
{
|
|
|
|
instance().m_generalTypes.emplace_back(make_unique<T>(std::forward<Args>(_args)...));
|
|
|
|
return static_cast<T const*>(instance().m_generalTypes.back().get());
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:45:12 +00:00
|
|
|
Type const* TypeProvider::fromElementaryTypeName(ElementaryTypeNameToken const& _type, boost::optional<StateMutability> _stateMutability)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
solAssert(
|
|
|
|
TokenTraits::isElementaryTypeName(_type.token()),
|
|
|
|
"Expected an elementary type name but got " + _type.toString()
|
|
|
|
);
|
|
|
|
|
|
|
|
unsigned const m = _type.firstNumber();
|
|
|
|
unsigned const n = _type.secondNumber();
|
|
|
|
|
|
|
|
switch (_type.token())
|
|
|
|
{
|
|
|
|
case Token::IntM:
|
2019-04-17 11:40:50 +00:00
|
|
|
return integer(m, IntegerType::Modifier::Signed);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::UIntM:
|
2019-04-17 11:40:50 +00:00
|
|
|
return integer(m, IntegerType::Modifier::Unsigned);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::Byte:
|
2019-04-17 11:40:50 +00:00
|
|
|
return byte();
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::BytesM:
|
2019-04-17 11:40:50 +00:00
|
|
|
return fixedBytes(m);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::FixedMxN:
|
2019-04-17 11:40:50 +00:00
|
|
|
return fixedPoint(m, n, FixedPointType::Modifier::Signed);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::UFixedMxN:
|
2019-04-17 11:40:50 +00:00
|
|
|
return fixedPoint(m, n, FixedPointType::Modifier::Unsigned);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::Int:
|
2019-04-17 11:40:50 +00:00
|
|
|
return integer(256, IntegerType::Modifier::Signed);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::UInt:
|
2019-04-17 11:40:50 +00:00
|
|
|
return integer(256, IntegerType::Modifier::Unsigned);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::Fixed:
|
2019-04-17 11:40:50 +00:00
|
|
|
return fixedPoint(128, 18, FixedPointType::Modifier::Signed);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::UFixed:
|
2019-04-17 11:40:50 +00:00
|
|
|
return fixedPoint(128, 18, FixedPointType::Modifier::Unsigned);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::Address:
|
2019-09-04 15:45:12 +00:00
|
|
|
{
|
|
|
|
if (_stateMutability)
|
|
|
|
{
|
|
|
|
solAssert(*_stateMutability == StateMutability::Payable, "");
|
|
|
|
return payableAddress();
|
|
|
|
}
|
2019-04-17 11:40:50 +00:00
|
|
|
return address();
|
2019-09-04 15:45:12 +00:00
|
|
|
}
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::Bool:
|
2019-04-17 11:40:50 +00:00
|
|
|
return boolean();
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::Bytes:
|
2019-04-17 11:40:50 +00:00
|
|
|
return bytesStorage();
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::String:
|
2019-04-17 11:40:50 +00:00
|
|
|
return stringStorage();
|
2019-04-15 13:33:39 +00:00
|
|
|
default:
|
|
|
|
solAssert(
|
|
|
|
false,
|
|
|
|
"Unable to convert elementary typename " + _type.toString() + " to type."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TypePointer TypeProvider::fromElementaryTypeName(string const& _name)
|
|
|
|
{
|
|
|
|
vector<string> nameParts;
|
|
|
|
boost::split(nameParts, _name, boost::is_any_of(" "));
|
|
|
|
solAssert(nameParts.size() == 1 || nameParts.size() == 2, "Cannot parse elementary type: " + _name);
|
|
|
|
|
|
|
|
Token token;
|
|
|
|
unsigned short firstNum, secondNum;
|
|
|
|
tie(token, firstNum, secondNum) = TokenTraits::fromIdentifierOrKeyword(nameParts[0]);
|
|
|
|
|
|
|
|
auto t = fromElementaryTypeName(ElementaryTypeNameToken(token, firstNum, secondNum));
|
|
|
|
if (auto* ref = dynamic_cast<ReferenceType const*>(t))
|
|
|
|
{
|
|
|
|
DataLocation location = DataLocation::Storage;
|
|
|
|
if (nameParts.size() == 2)
|
|
|
|
{
|
|
|
|
if (nameParts[1] == "storage")
|
|
|
|
location = DataLocation::Storage;
|
|
|
|
else if (nameParts[1] == "calldata")
|
|
|
|
location = DataLocation::CallData;
|
|
|
|
else if (nameParts[1] == "memory")
|
|
|
|
location = DataLocation::Memory;
|
|
|
|
else
|
|
|
|
solAssert(false, "Unknown data location: " + nameParts[1]);
|
|
|
|
}
|
|
|
|
return withLocation(ref, location, true);
|
|
|
|
}
|
|
|
|
else if (t->category() == Type::Category::Address)
|
|
|
|
{
|
|
|
|
if (nameParts.size() == 2)
|
|
|
|
{
|
|
|
|
if (nameParts[1] == "payable")
|
2019-04-17 11:40:50 +00:00
|
|
|
return payableAddress();
|
2019-04-15 13:33:39 +00:00
|
|
|
else
|
|
|
|
solAssert(false, "Invalid state mutability for address type: " + nameParts[1]);
|
|
|
|
}
|
2019-04-17 11:40:50 +00:00
|
|
|
return address();
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
solAssert(nameParts.size() == 1, "Storage location suffix only allowed for reference types");
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayType const* TypeProvider::bytesStorage()
|
2019-04-16 17:45:25 +00:00
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
if (!m_bytesStorage)
|
|
|
|
m_bytesStorage = make_unique<ArrayType>(DataLocation::Storage, false);
|
|
|
|
return m_bytesStorage.get();
|
2019-04-16 17:45:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayType const* TypeProvider::bytesMemory()
|
2019-04-16 17:45:25 +00:00
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
if (!m_bytesMemory)
|
|
|
|
m_bytesMemory = make_unique<ArrayType>(DataLocation::Memory, false);
|
|
|
|
return m_bytesMemory.get();
|
2019-04-16 17:45:25 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 16:30:00 +00:00
|
|
|
ArrayType const* TypeProvider::bytesCalldata()
|
|
|
|
{
|
|
|
|
if (!m_bytesCalldata)
|
|
|
|
m_bytesCalldata = make_unique<ArrayType>(DataLocation::CallData, false);
|
|
|
|
return m_bytesCalldata.get();
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayType const* TypeProvider::stringStorage()
|
2019-04-16 17:45:25 +00:00
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
if (!m_stringStorage)
|
|
|
|
m_stringStorage = make_unique<ArrayType>(DataLocation::Storage, true);
|
|
|
|
return m_stringStorage.get();
|
2019-04-16 17:45:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayType const* TypeProvider::stringMemory()
|
2019-04-16 17:45:25 +00:00
|
|
|
{
|
2019-04-17 11:40:50 +00:00
|
|
|
if (!m_stringMemory)
|
|
|
|
m_stringMemory = make_unique<ArrayType>(DataLocation::Memory, true);
|
|
|
|
return m_stringMemory.get();
|
2019-04-16 17:45:25 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 13:33:39 +00:00
|
|
|
TypePointer TypeProvider::forLiteral(Literal const& _literal)
|
|
|
|
{
|
|
|
|
switch (_literal.token())
|
|
|
|
{
|
|
|
|
case Token::TrueLiteral:
|
|
|
|
case Token::FalseLiteral:
|
2019-04-17 11:40:50 +00:00
|
|
|
return boolean();
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::Number:
|
2019-04-17 11:40:50 +00:00
|
|
|
return rationalNumber(_literal);
|
2019-04-15 13:33:39 +00:00
|
|
|
case Token::StringLiteral:
|
2019-04-17 11:40:50 +00:00
|
|
|
return stringLiteral(_literal.value());
|
2019-04-15 13:33:39 +00:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
RationalNumberType const* TypeProvider::rationalNumber(Literal const& _literal)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
solAssert(_literal.token() == Token::Number, "");
|
|
|
|
std::tuple<bool, rational> validLiteral = RationalNumberType::isValidLiteral(_literal);
|
|
|
|
if (std::get<0>(validLiteral))
|
|
|
|
{
|
|
|
|
TypePointer compatibleBytesType = nullptr;
|
|
|
|
if (_literal.isHexNumber())
|
|
|
|
{
|
|
|
|
size_t const digitCount = _literal.valueWithoutUnderscores().length() - 2;
|
|
|
|
if (digitCount % 2 == 0 && (digitCount / 2) <= 32)
|
2019-04-17 11:40:50 +00:00
|
|
|
compatibleBytesType = fixedBytes(digitCount / 2);
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
return rationalNumber(std::get<1>(validLiteral), compatibleBytesType);
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
StringLiteralType const* TypeProvider::stringLiteral(string const& literal)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
auto i = instance().m_stringLiteralTypes.find(literal);
|
|
|
|
if (i != instance().m_stringLiteralTypes.end())
|
|
|
|
return i->second.get();
|
|
|
|
else
|
|
|
|
return instance().m_stringLiteralTypes.emplace(literal, make_unique<StringLiteralType>(literal)).first->second.get();
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
FixedPointType const* TypeProvider::fixedPoint(unsigned m, unsigned n, FixedPointType::Modifier _modifier)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
auto& map = _modifier == FixedPointType::Modifier::Unsigned ? instance().m_ufixedMxN : instance().m_fixedMxN;
|
|
|
|
|
|
|
|
auto i = map.find(make_pair(m, n));
|
|
|
|
if (i != map.end())
|
|
|
|
return i->second.get();
|
|
|
|
|
|
|
|
return map.emplace(
|
|
|
|
make_pair(m, n),
|
|
|
|
make_unique<FixedPointType>(m, n, _modifier)
|
|
|
|
).first->second.get();
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
TupleType const* TypeProvider::tuple(vector<Type const*> members)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
if (members.empty())
|
2019-04-17 11:40:50 +00:00
|
|
|
return &m_emptyTuple;
|
2019-04-15 13:33:39 +00:00
|
|
|
|
|
|
|
return createAndGet<TupleType>(move(members));
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferenceType const* TypeProvider::withLocation(ReferenceType const* _type, DataLocation _location, bool _isPointer)
|
|
|
|
{
|
|
|
|
if (_type->location() == _location && _type->isPointer() == _isPointer)
|
|
|
|
return _type;
|
|
|
|
|
|
|
|
instance().m_generalTypes.emplace_back(_type->copyForLocation(_location, _isPointer));
|
|
|
|
return static_cast<ReferenceType const*>(instance().m_generalTypes.back().get());
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
FunctionType const* TypeProvider::function(FunctionDefinition const& _function, bool _isInternal)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<FunctionType>(_function, _isInternal);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
FunctionType const* TypeProvider::function(VariableDeclaration const& _varDecl)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<FunctionType>(_varDecl);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
FunctionType const* TypeProvider::function(EventDefinition const& _def)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<FunctionType>(_def);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
FunctionType const* TypeProvider::function(FunctionTypeName const& _typeName)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<FunctionType>(_typeName);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
FunctionType const* TypeProvider::function(
|
2019-04-15 13:33:39 +00:00
|
|
|
strings const& _parameterTypes,
|
|
|
|
strings const& _returnParameterTypes,
|
|
|
|
FunctionType::Kind _kind,
|
|
|
|
bool _arbitraryParameters,
|
|
|
|
StateMutability _stateMutability
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return createAndGet<FunctionType>(
|
|
|
|
_parameterTypes, _returnParameterTypes,
|
|
|
|
_kind, _arbitraryParameters, _stateMutability
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
FunctionType const* TypeProvider::function(
|
2019-04-15 13:33:39 +00:00
|
|
|
TypePointers const& _parameterTypes,
|
|
|
|
TypePointers const& _returnParameterTypes,
|
|
|
|
strings _parameterNames,
|
|
|
|
strings _returnParameterNames,
|
|
|
|
FunctionType::Kind _kind,
|
|
|
|
bool _arbitraryParameters,
|
|
|
|
StateMutability _stateMutability,
|
|
|
|
Declaration const* _declaration,
|
|
|
|
bool _gasSet,
|
|
|
|
bool _valueSet,
|
|
|
|
bool _bound
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return createAndGet<FunctionType>(
|
|
|
|
_parameterTypes,
|
|
|
|
_returnParameterTypes,
|
|
|
|
_parameterNames,
|
|
|
|
_returnParameterNames,
|
|
|
|
_kind,
|
|
|
|
_arbitraryParameters,
|
|
|
|
_stateMutability,
|
|
|
|
_declaration,
|
|
|
|
_gasSet,
|
|
|
|
_valueSet,
|
|
|
|
_bound
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
RationalNumberType const* TypeProvider::rationalNumber(rational const& _value, Type const* _compatibleBytesType)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<RationalNumberType>(_value, _compatibleBytesType);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayType const* TypeProvider::array(DataLocation _location, bool _isString)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
if (_isString)
|
|
|
|
{
|
|
|
|
if (_location == DataLocation::Storage)
|
2019-04-17 11:40:50 +00:00
|
|
|
return stringStorage();
|
2019-04-15 13:33:39 +00:00
|
|
|
if (_location == DataLocation::Memory)
|
2019-04-17 11:40:50 +00:00
|
|
|
return stringMemory();
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_location == DataLocation::Storage)
|
2019-04-17 11:40:50 +00:00
|
|
|
return bytesStorage();
|
2019-04-15 13:33:39 +00:00
|
|
|
if (_location == DataLocation::Memory)
|
2019-04-17 11:40:50 +00:00
|
|
|
return bytesMemory();
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
return createAndGet<ArrayType>(_location, _isString);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayType const* TypeProvider::array(DataLocation _location, Type const* _baseType)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<ArrayType>(_location, _baseType);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ArrayType const* TypeProvider::array(DataLocation _location, Type const* _baseType, u256 const& _length)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<ArrayType>(_location, _baseType, _length);
|
|
|
|
}
|
|
|
|
|
2019-09-03 16:30:00 +00:00
|
|
|
ArraySliceType const* TypeProvider::arraySlice(ArrayType const& _arrayType)
|
|
|
|
{
|
|
|
|
return createAndGet<ArraySliceType>(_arrayType);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ContractType const* TypeProvider::contract(ContractDefinition const& _contractDef, bool _isSuper)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<ContractType>(_contractDef, _isSuper);
|
|
|
|
}
|
|
|
|
|
|
|
|
EnumType const* TypeProvider::enumType(EnumDefinition const& _enumDef)
|
|
|
|
{
|
|
|
|
return createAndGet<EnumType>(_enumDef);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ModuleType const* TypeProvider::module(SourceUnit const& _source)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<ModuleType>(_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
TypeType const* TypeProvider::typeType(Type const* _actualType)
|
|
|
|
{
|
|
|
|
return createAndGet<TypeType>(_actualType);
|
|
|
|
}
|
|
|
|
|
|
|
|
StructType const* TypeProvider::structType(StructDefinition const& _struct, DataLocation _location)
|
|
|
|
{
|
|
|
|
return createAndGet<StructType>(_struct, _location);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
ModifierType const* TypeProvider::modifier(ModifierDefinition const& _def)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<ModifierType>(_def);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
MagicType const* TypeProvider::magic(MagicType::Kind _kind)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
solAssert(_kind != MagicType::Kind::MetaType, "MetaType is handled separately");
|
2019-04-17 11:40:50 +00:00
|
|
|
return m_magics.at(static_cast<size_t>(_kind)).get();
|
2019-04-15 13:33:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
MagicType const* TypeProvider::meta(Type const* _type)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
solAssert(_type && _type->category() == Type::Category::Contract, "Only contracts supported for now.");
|
|
|
|
return createAndGet<MagicType>(_type);
|
|
|
|
}
|
|
|
|
|
2019-04-17 11:40:50 +00:00
|
|
|
MappingType const* TypeProvider::mapping(Type const* _keyType, Type const* _valueType)
|
2019-04-15 13:33:39 +00:00
|
|
|
{
|
|
|
|
return createAndGet<MappingType>(_keyType, _valueType);
|
|
|
|
}
|