Skip to content

Commit 4446f2a

Browse files
committed
RequirementMachine: Move some code out of RuleBuilder and into RequirementSignatureRequest
1 parent 466d6a9 commit 4446f2a

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,22 +167,24 @@ RequirementMachine::initWithGenericSignature(CanGenericSignature sig) {
167167
/// Returns failure if completion fails within the configured number of steps.
168168
std::pair<CompletionResult, unsigned>
169169
RequirementMachine::initWithProtocolWrittenRequirements(
170-
ArrayRef<const ProtocolDecl *> protos) {
170+
ArrayRef<const ProtocolDecl *> component,
171+
const llvm::DenseMap<const ProtocolDecl *,
172+
SmallVector<StructuralRequirement, 4>> protos) {
171173
FrontendStatsTracer tracer(Stats, "build-rewrite-system");
172174

173175
if (Dump) {
174176
llvm::dbgs() << "Adding protocols";
175-
for (auto *proto : protos) {
177+
for (auto *proto : component) {
176178
llvm::dbgs() << " " << proto->getName();
177179
}
178180
llvm::dbgs() << " {\n";
179181
}
180182

181183
RuleBuilder builder(Context, System.getReferencedProtocols());
182-
builder.initWithProtocolWrittenRequirements(protos);
184+
builder.initWithProtocolWrittenRequirements(component, protos);
183185

184186
// Add the initial set of rewrite rules to the rewrite system.
185-
System.initialize(/*recordLoops=*/true, protos,
187+
System.initialize(/*recordLoops=*/true, component,
186188
std::move(builder.WrittenRequirements),
187189
std::move(builder.ImportedRules),
188190
std::move(builder.PermanentRules),

lib/AST/RequirementMachine/RequirementMachine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ class RequirementMachine final {
9999

100100
std::pair<CompletionResult, unsigned>
101101
initWithProtocolWrittenRequirements(
102-
ArrayRef<const ProtocolDecl *> protos);
102+
ArrayRef<const ProtocolDecl *> component,
103+
const llvm::DenseMap<const ProtocolDecl *,
104+
SmallVector<StructuralRequirement, 4>> protos);
103105

104106
std::pair<CompletionResult, unsigned>
105107
initWithWrittenRequirements(

lib/AST/RequirementMachine/RequirementMachineRequests.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,23 @@ RequirementSignatureRequestRQM::evaluate(Evaluator &evaluator,
185185
// component at the same time.
186186
auto component = ctx.getRewriteContext().getProtocolComponent(proto);
187187

188+
// Collect user-written requirements from the protocols in this connected
189+
// component.
190+
llvm::DenseMap<const ProtocolDecl *,
191+
SmallVector<StructuralRequirement, 4>> protos;
192+
for (const auto *proto : component) {
193+
auto &requirements = protos[proto];
194+
for (auto req : proto->getStructuralRequirements())
195+
requirements.push_back(req);
196+
for (auto req : proto->getTypeAliasRequirements())
197+
requirements.push_back({req, SourceLoc(), /*inferred=*/false});
198+
}
199+
188200
// Heap-allocate the requirement machine to save stack space.
189201
std::unique_ptr<RequirementMachine> machine(new RequirementMachine(
190202
ctx.getRewriteContext()));
191203

192-
SmallVector<RequirementError, 4> errors;
193-
194-
auto status = machine->initWithProtocolWrittenRequirements(component);
204+
auto status = machine->initWithProtocolWrittenRequirements(component, protos);
195205
if (status.first != CompletionResult::Success) {
196206
// All we can do at this point is diagnose and give each protocol an empty
197207
// requirement signature.
@@ -260,6 +270,7 @@ RequirementSignatureRequestRQM::evaluate(Evaluator &evaluator,
260270

261271
if (ctx.LangOpts.RequirementMachineProtocolSignatures ==
262272
RequirementMachineMode::Enabled) {
273+
SmallVector<RequirementError, 4> errors;
263274
machine->System.computeRedundantRequirementDiagnostics(errors);
264275
diagnoseRequirementErrors(ctx, errors,
265276
/*allowConcreteGenericParams=*/false);

lib/AST/RequirementMachine/RuleBuilder.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,28 +123,31 @@ void RuleBuilder::initWithProtocolSignatureRequirements(
123123
/// user-written requirements. Used when actually building requirement
124124
/// signatures.
125125
void RuleBuilder::initWithProtocolWrittenRequirements(
126-
ArrayRef<const ProtocolDecl *> protos) {
126+
ArrayRef<const ProtocolDecl *> component,
127+
const llvm::DenseMap<const ProtocolDecl *,
128+
SmallVector<StructuralRequirement, 4>> protos) {
127129
assert(!Initialized);
128130
Initialized = 1;
129131

130132
// Add all protocols to the referenced set, so that subsequent calls
131133
// to addReferencedProtocol() with one of these protocols don't add
132134
// them to the import list.
133-
for (auto *proto : protos) {
135+
for (const auto *proto : component)
134136
ReferencedProtocols.insert(proto);
135-
}
136137

137-
for (auto *proto : protos) {
138+
for (const auto *proto : component) {
139+
auto found = protos.find(proto);
140+
assert(found != protos.end());
141+
const auto &reqs = found->second;
142+
138143
if (Dump) {
139144
llvm::dbgs() << "protocol " << proto->getName() << " {\n";
140145
}
141146

142147
addPermanentProtocolRules(proto);
143148

144-
for (auto req : proto->getStructuralRequirements())
149+
for (auto req : reqs)
145150
addRequirement(req, proto);
146-
for (auto req : proto->getTypeAliasRequirements())
147-
addRequirement(req.getCanonical(), proto);
148151

149152
for (auto *otherProto : proto->getProtocolDependencies())
150153
addReferencedProtocol(otherProto);

lib/AST/RequirementMachine/RuleBuilder.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ struct RuleBuilder {
9898
void initWithGenericSignatureRequirements(ArrayRef<Requirement> requirements);
9999
void initWithWrittenRequirements(ArrayRef<StructuralRequirement> requirements);
100100
void initWithProtocolSignatureRequirements(ArrayRef<const ProtocolDecl *> proto);
101-
void initWithProtocolWrittenRequirements(ArrayRef<const ProtocolDecl *> proto);
101+
void initWithProtocolWrittenRequirements(
102+
ArrayRef<const ProtocolDecl *> component,
103+
const llvm::DenseMap<const ProtocolDecl *,
104+
SmallVector<StructuralRequirement, 4>> protos);
102105
void initWithConditionalRequirements(ArrayRef<Requirement> requirements,
103106
ArrayRef<Term> substitutions);
104107
void addReferencedProtocol(const ProtocolDecl *proto);

0 commit comments

Comments
 (0)