From f5ba39cf70de72ae727c23df49b842a608dd4aaf Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 13 Jan 2024 17:31:04 +0000 Subject: Add TypeInfos.subtypes() --- .../java/org/shedlang/compiler/typechecker/TypeInfos.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/main/java') diff --git a/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java b/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java index 22f68b38..4547c895 100644 --- a/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java +++ b/src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java @@ -145,6 +145,21 @@ public class TypeInfos { return supertypes; } + /** + * Get the subtypes of a type, from most specific to least specific. + */ + public Set subtypes(Type type) { + var subtypes = P.orderedSet(type); + if (type instanceof SumType sumType) { + var sumTypeInfo = sumTypeInfo(sumType); + subtypes = subtypes.plusAll( + sumTypeInfo.orElseThrow().variants() + ); + } + subtypes = subtypes.plus(Types.BOTTOM); + return subtypes; + } + private Optional simpleSumTypeInfo(SimpleSumType type) { return Optional.ofNullable(sumTypeInfos.get(type)); } -- cgit v1.2.3