More flexible array literals.

This commit is contained in:
chriseth
2021-09-15 18:11:21 +02:00
parent 8735d3fb6c
commit 83e7f5e83e
4 changed files with 66 additions and 8 deletions
+24 -4
View File
@@ -2565,8 +2565,26 @@ vector<tuple<string, Type const*>> UserDefinedValueType::makeStackItems() const
BoolResult TupleType::isImplicitlyConvertibleTo(Type const& _other) const
{
if (auto tupleType = dynamic_cast<TupleType const*>(&_other))
if (auto arrayType = dynamic_cast<ArrayType const*>(&_other))
{
if (!m_isArrayLiteral)
return false;
if (!(
arrayType->isDynamicallySized() ||
arrayType->length() == m_components.size()
))
return false;
for (Type const* t: m_components)
if (!t->isImplicitlyConvertibleTo(arrayType->baseType()))
return false;
return true;
}
else if (auto tupleType = dynamic_cast<TupleType const*>(&_other))
{
if (m_isArrayLiteral != tupleType->m_isArrayLiteral)
return false;
TypePointers const& targets = tupleType->components();
if (targets.empty())
return components().empty();
@@ -2585,19 +2603,20 @@ BoolResult TupleType::isImplicitlyConvertibleTo(Type const& _other) const
string TupleType::richIdentifier() const
{
return "t_tuple" + identifierList(components());
return (m_isArrayLiteral ? "t_arrayLiteral" : "t_tuple") + identifierList(components());
}
bool TupleType::operator==(Type const& _other) const
{
if (auto tupleType = dynamic_cast<TupleType const*>(&_other))
return components() == tupleType->components();
return components() == tupleType->components() && m_isArrayLiteral == tupleType->m_isArrayLiteral;
else
return false;
}
string TupleType::toString(bool _short) const
{
// TODO
if (components().empty())
return "tuple()";
string str = "tuple(";
@@ -2640,7 +2659,8 @@ Type const* TupleType::mobileType() const
else
mobiles.push_back(nullptr);
}
return TypeProvider::tuple(move(mobiles));
// TODO correct?
return TypeProvider::tuple(move(mobiles), m_isArrayLiteral);
}
FunctionType::FunctionType(FunctionDefinition const& _function, Kind _kind):
+6 -1
View File
@@ -1136,7 +1136,10 @@ private:
class TupleType: public CompositeType
{
public:
explicit TupleType(std::vector<Type const*> _types = {}): m_components(std::move(_types)) {}
explicit TupleType(std::vector<Type const*> _types = {}, bool _arrayLiteral = false):
m_components(std::move(_types)),
m_isArrayLiteral(_arrayLiteral)
{}
Category category() const override { return Category::Tuple; }
@@ -1150,6 +1153,7 @@ public:
bool hasSimpleZeroValueInMemory() const override { return false; }
Type const* mobileType() const override;
bool isArrayLiteral() const { return m_isArrayLiteral; }
std::vector<Type const*> const& components() const { return m_components; }
protected:
@@ -1166,6 +1170,7 @@ protected:
private:
std::vector<Type const*> const m_components;
bool const m_isArrayLiteral;
};
/**