Add MatrixConstant

This commit is contained in:
Greg Fischer 2018-01-12 15:52:45 -07:00 committed by David Neto
parent 8cb0aec724
commit c2aadb02d9
2 changed files with 40 additions and 0 deletions

View File

@ -112,6 +112,10 @@ const Constant* ConstantManager::CreateConstant(
}))
return nullptr;
return new VectorConstant(vt, components);
} else if (auto* mt = type->AsMatrix()) {
auto components = GetConstantsFromIds(literal_words_or_ids);
if (components.empty()) return nullptr;
return new MatrixConstant(mt, components);
} else if (auto* st = type->AsStruct()) {
auto components = GetConstantsFromIds(literal_words_or_ids);
if (components.empty()) return nullptr;

View File

@ -43,6 +43,7 @@ class BoolConstant;
class CompositeConstant;
class StructConstant;
class VectorConstant;
class MatrixConstant;
class ArrayConstant;
class NullConstant;
@ -64,6 +65,7 @@ class Constant {
virtual CompositeConstant* AsCompositeConstant() { return nullptr; }
virtual StructConstant* AsStructConstant() { return nullptr; }
virtual VectorConstant* AsVectorConstant() { return nullptr; }
virtual MatrixConstant* AsMatrixConstant() { return nullptr; }
virtual ArrayConstant* AsArrayConstant() { return nullptr; }
virtual NullConstant* AsNullConstant() { return nullptr; }
@ -76,6 +78,7 @@ class Constant {
}
virtual const StructConstant* AsStructConstant() const { return nullptr; }
virtual const VectorConstant* AsVectorConstant() const { return nullptr; }
virtual const MatrixConstant* AsMatrixConstant() const { return nullptr; }
virtual const ArrayConstant* AsArrayConstant() const { return nullptr; }
virtual const NullConstant* AsNullConstant() const { return nullptr; }
@ -247,6 +250,39 @@ class VectorConstant : public CompositeConstant {
const Type* component_type_;
};
// Matrix type constant.
class MatrixConstant : public CompositeConstant {
public:
MatrixConstant(const Matrix* ty)
: CompositeConstant(ty), component_type_(ty->element_type()) {}
MatrixConstant(const Matrix* ty,
const std::vector<const Constant*>& components)
: CompositeConstant(ty, components),
component_type_(ty->element_type()) {}
MatrixConstant(const Vector* ty, std::vector<const Constant*>&& components)
: CompositeConstant(ty, std::move(components)),
component_type_(ty->element_type()) {}
MatrixConstant* AsMatrixConstant() override { return this; }
const MatrixConstant* AsMatrixConstant() const override { return this; }
// Make a copy of this MatrixConstant instance.
std::unique_ptr<MatrixConstant> CopyMatrixConstant() const {
auto another = MakeUnique<MatrixConstant>(type_->AsMatrix());
another->components_.insert(another->components_.end(), components_.begin(),
components_.end());
return another;
}
std::unique_ptr<Constant> Copy() const override {
return std::unique_ptr<Constant>(CopyMatrixConstant().release());
}
const Type* component_type() { return component_type_; }
private:
const Type* component_type_;
};
// Array type constant.
class ArrayConstant : public CompositeConstant {
public: