Skip to content

Commit 2a1c141

Browse files
committed
RequirementMachine: Implement EquivalenceClass::getConcreteType()
1 parent c35aa93 commit 2a1c141

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/AST/RequirementMachine/EquivalenceClassMap.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,45 @@ static unsigned getGenericParamIndex(Type type) {
102102
return paramTy->getIndex();
103103
}
104104

105+
/// Reverses the transformation performed by
106+
/// RewriteSystemBuilder::getConcreteSubstitutionSchema().
107+
static Type getTypeFromSubstitutionSchema(Type schema,
108+
ArrayRef<Term> substitutions,
109+
TypeArrayView<GenericTypeParamType> genericParams,
110+
const ProtocolGraph &protos,
111+
RewriteContext &ctx) {
112+
assert(!schema->isTypeParameter() && "Must have a concrete type here");
113+
114+
if (!schema->hasTypeParameter())
115+
return schema;
116+
117+
return schema.transformRec([&](Type t) -> Optional<Type> {
118+
if (t->is<GenericTypeParamType>()) {
119+
auto index = getGenericParamIndex(t);
120+
121+
return ctx.getTypeForTerm(substitutions[index],
122+
genericParams, protos);
123+
}
124+
125+
assert(!t->isTypeParameter());
126+
return None;
127+
});
128+
}
129+
130+
/// Get the concrete type of this equivalence class.
131+
///
132+
/// Asserts if this equivalence class is not concrete.
133+
Type EquivalenceClass::getConcreteType(
134+
TypeArrayView<GenericTypeParamType> genericParams,
135+
const ProtocolGraph &protos,
136+
RewriteContext &ctx) const {
137+
return getTypeFromSubstitutionSchema(ConcreteType->getConcreteType(),
138+
ConcreteType->getSubstitutions(),
139+
genericParams,
140+
protos,
141+
ctx);
142+
}
143+
105144
/// Given a concrete type that is a structural sub-component of a concrete
106145
/// type produced by RewriteSystemBuilder::getConcreteSubstitutionSchema(),
107146
/// collect the subset of referenced substitutions and renumber the generic

lib/AST/RequirementMachine/EquivalenceClassMap.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class EquivalenceClass {
8686
return ConcreteType.hasValue();
8787
}
8888

89+
Type getConcreteType(
90+
TypeArrayView<GenericTypeParamType> genericParams,
91+
const ProtocolGraph &protos,
92+
RewriteContext &ctx) const;
93+
8994
LayoutConstraint getLayoutConstraint() const {
9095
return Layout;
9196
}

0 commit comments

Comments
 (0)