diff --git a/codon/parser/ast/types/class.cpp b/codon/parser/ast/types/class.cpp index 264fb047..304f35ae 100644 --- a/codon/parser/ast/types/class.cpp +++ b/codon/parser/ast/types/class.cpp @@ -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(), diff --git a/codon/parser/visitors/typecheck/infer.cpp b/codon/parser/visitors/typecheck/infer.cpp index 721c9857..cfb0b137 100644 --- a/codon/parser/visitors/typecheck/infer.cpp +++ b/codon/parser/visitors/typecheck/infer.cpp @@ -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(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(realized.get(), p->func); - // auto val = - // std::make_shared(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 types; std::vector 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; diff --git a/stdlib/internal/core.codon b/stdlib/internal/core.codon index b509f9a3..dbf846d5 100644 --- a/stdlib/internal/core.codon +++ b/stdlib/internal/core.codon @@ -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]: