aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-01-13 17:31:04 +0000
committerMichael Williamson <mike@zwobble.org>2024-01-13 17:31:04 +0000
commitf5ba39cf70de72ae727c23df49b842a608dd4aaf (patch)
treeea8e7d7606e54309abeb89b7fadce2cc7e88b4bb /src/main/java
parent8f95317595dba4397c525b59cf8c2bae3658c03e (diff)
Add TypeInfos.subtypes()
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/shedlang/compiler/typechecker/TypeInfos.java15
1 files changed, 15 insertions, 0 deletions
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<Type> 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<SumTypeInfo> simpleSumTypeInfo(SimpleSumType type) {
return Optional.ofNullable(sumTypeInfos.get(type));
}