Explorer: Support class subtyping (#2460)

Features:
* Adds support for [subtyping](https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/classes.md#subtyping) for local variables, and function parameters

Changes:
* Update function parameter `Deduce` to handle subtyping
* Update `InstantiateType` to support `PointerType`
* Update `Convert` to support convertion from child class to a base class

Relates to #1881 

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
Co-authored-by: Geoff Romer <gromer@google.com>
This commit is contained in:
Adrien Leravat
2022-12-19 11:32:32 -08:00
committed by GitHub
co-authored by Jon Ross-Perkins Geoff Romer
parent 8e6d48004b
commit 9299e51511
10 changed files with 245 additions and 10 deletions
+16
View File
@@ -1183,4 +1183,20 @@ void ImplBinding::PrintID(llvm::raw_ostream& out) const {
out << *type_var_ << " as " << **iface_;
}
auto NominalClassType::InheritsClass(Nonnull<const Value*> other) const
-> bool {
const auto* other_class = dyn_cast<NominalClassType>(other);
if (!other_class) {
return false;
}
std::optional<Nonnull<const NominalClassType*>> ancestor_class = this;
while (ancestor_class) {
if (TypeEqual(*ancestor_class, other_class, std::nullopt)) {
return true;
}
ancestor_class = (*ancestor_class)->base();
}
return false;
}
} // namespace Carbon