1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00

Add unrealized_type

This commit is contained in:
Ibrahim Numanagić 2024-03-21 14:42:43 -07:00
parent b15b70875a
commit af9a74301a
3 changed files with 25 additions and 29 deletions

View File

@ -101,6 +101,8 @@ bool ClassType::canRealize() const {
if (!hasUnbounds())
return true; // always true!
}
if (name == "unrealized_type")
return generics[0].type->getClass() != nullptr;
return std::all_of(generics.begin(), generics.end(),
[](auto &t) { return !t.type || t.type->canRealize(); }) &&
std::all_of(hiddenGenerics.begin(), hiddenGenerics.end(),

View File

@ -138,13 +138,8 @@ types::TypePtr TypecheckVisitor::realize(types::TypePtr typ) {
realizeType(ret->getClass().get());
// Needed for return type unification
unify(f->getRetType(), ret->getClass()->generics[1].type);
// return unify(ret, typ); // Needed for return type unification
return ret;
}
} else if (auto p = typ->getPartial()) {
TypePtr t = realizeType(p.get());
t = std::make_shared<PartialType>(t->getClass().get(), p->func);
return t;
} else if (auto c = typ->getClass()) {
auto t = realizeType(c.get());
return t;
@ -209,9 +204,6 @@ types::TypePtr TypecheckVisitor::realizeType(types::ClassType *type) {
return (*r)->type->getClass();
}
if (type->getPartial())
LOG("");
auto realized = type->getClass();
if (auto s = type->getStatic())
realized = ctx->getType(s->name)->getClass();
@ -222,10 +214,13 @@ types::TypePtr TypecheckVisitor::realizeType(types::ClassType *type) {
}
// Realize generics
for (auto &e : realized->generics) {
if (!realize(e.type))
return nullptr;
}
if (type->is("unrealized_type"))
type->generics[0].type->generalize(ctx->typecheckLevel);
else
for (auto &e : realized->generics) {
if (!realize(e.type))
return nullptr;
}
// LOG("[realize] T {} -> {}", realized->debugString(2), realized->realizedName());
@ -274,16 +269,6 @@ types::TypePtr TypecheckVisitor::realizeType(types::ClassType *type) {
}
}
// Fix for partial types
// if (auto p = type->getPartial()) {
// auto pt = std::make_shared<PartialType>(realized.get(), p->func);
// auto val =
// std::make_shared<TypecheckItem>(pt->realizedName(), "", ctx->getModule(), pt);
// ctx->addAlwaysVisible(val);
// ctx->cache->classes[pt->name].realizations[pt->realizedName()] =
// ctx->cache->classes[realized->name].realizations[realized->realizedName()];
// }
return realized;
}
@ -685,11 +670,15 @@ ir::types::Type *TypecheckVisitor::makeIRType(types::ClassType *t) {
// Prepare generics and statics
std::vector<ir::types::Type *> types;
std::vector<types::StaticTypePtr> statics;
for (auto &m : t->generics) {
if (auto s = m.type->getStatic())
statics.push_back(s);
types.push_back(forceFindIRType(m.type));
}
if (t->is("unrealized_type"))
types.push_back(nullptr);
else
for (auto &m : t->generics) {
if (auto s = m.type->getStatic())
statics.push_back(s);
else
types.push_back(forceFindIRType(m.type));
}
// Get the IR type
auto *module = ctx->cache->module;

View File

@ -11,6 +11,11 @@ class type[T]:
# __doc__
pass
@tuple
@__internal__
class unrealized_type[T]:
pass
@__internal__
class __internal__:
pass
@ -263,17 +268,17 @@ class NamedTuple:
s = ', '.join(f"{keys[i]}: {values[i]}" for i in range(len(keys)))
return f"({s})"
@__internal__
@tuple
class Partial:
args: T # format: (arg1, arg2, ..., (star_args...))
kwargs: K
# Function argument is handled in Codon
M: Static[str] # mask
T: type # Tuple
K: type # NamedTuple
# F: type # generic function; handled specially in Codon
# def __new__(args: T, kwargs: K, M: Static[str], T: type, K: type, F: type) -> Partial[M, T, K, F]:
@llvm
def __new__(args: T, kwargs: K, M: Static[str], T: type, K: type) -> Partial[M, T, K]: