[WIP] slowly getting typechecking to work

This commit is contained in:
2017-04-28 23:37:06 -04:00
parent 259f092143
commit 87b6ed7f4c
2 changed files with 46 additions and 0 deletions

View File

@@ -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<PolyType>(t);
if (pt) {
return (restriction == pt->restriction ||
restriction == TYPE_NUMBER);
}
const AtomicType *at = CastType<AtomicType>(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<PolyType>(t0);
const PolyType *pyt1 = CastType<PolyType>(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);

2
type.h
View File

@@ -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;