From 87b6ed7f4c4d2a466092ddd91649534b1aebf7b3 Mon Sep 17 00:00:00 2001 From: Aaron Gutierrez Date: Fri, 28 Apr 2017 23:37:06 -0400 Subject: [PATCH] [WIP] slowly getting typechecking to work --- type.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ type.h | 2 ++ 2 files changed, 46 insertions(+) diff --git a/type.cpp b/type.cpp index f42fdd9d..9078b1fb 100644 --- a/type.cpp +++ b/type.cpp @@ -840,6 +840,32 @@ PolyType::Quantify(int q) const { return new PolyType(restriction, variability, isConst, q); } +bool +PolyType::CanBeType(const Type *t) const { + const PolyType *pt = CastType(t); + if (pt) { + return (restriction == pt->restriction || + restriction == TYPE_NUMBER); + } + + const AtomicType *at = CastType(t); + + switch (restriction) { + case TYPE_INTEGER: + return at->IsIntType(); + case TYPE_FLOATING: + return at->IsFloatType(); + case TYPE_NUMBER: + return at->IsIntType() || at->IsFloatType(); + default: + FATAL("Unmatched case for polymorphic restriction"); + } + + // not an atomic type or polymorphic type + return false; +} + + std::string PolyType::GetString() const { std::string ret; @@ -3887,6 +3913,24 @@ Type::MoreGeneralType(const Type *t0, const Type *t1, SourcePos pos, const char } } + const PolyType *pyt0 = CastType(t0); + const PolyType *pyt1 = CastType(t1); + + if (pyt0 || pyt1) { + // one of the types is polymorphic + if (pyt0 && pyt0->CanBeType(t1)) { + return pyt0; + } else if (pyt1 && pyt1->CanBeType(t0)) { + return pyt1; + } else { + // polymorphic type cannot represent the other type + // this is most likely bad + Error(pos, "Polymorphic type incompatible for \"%s\" and \"%s\"" + " for %s.", t0->GetString().c_str(), + t1->GetString().c_str(), reason); + } + } + // Now all we can do is promote atomic types... if (at0 == NULL || at1 == NULL) { Assert(reason != NULL); diff --git a/type.h b/type.h index 71693ec7..7f2fc9bd 100644 --- a/type.h +++ b/type.h @@ -389,6 +389,8 @@ public: const PolyType *Quantify(int quant) const; + bool CanBeType(const Type *t) const; + std::string GetString() const; std::string Mangle() const; std::string GetCDeclaration(const std::string &name) const;