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()) if (!hasUnbounds())
return true; // always true! return true; // always true!
} }
if (name == "unrealized_type")
return generics[0].type->getClass() != nullptr;
return std::all_of(generics.begin(), generics.end(), return std::all_of(generics.begin(), generics.end(),
[](auto &t) { return !t.type || t.type->canRealize(); }) && [](auto &t) { return !t.type || t.type->canRealize(); }) &&
std::all_of(hiddenGenerics.begin(), hiddenGenerics.end(), std::all_of(hiddenGenerics.begin(), hiddenGenerics.end(),

View File

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

View File

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