Skip to content

Commit f22a7e2

Browse files
committed
[AST] Drop makeIterator reference from ForEachStmt.
Rather than having the type checker form the ConcreteDeclRef for makeIterator, have SILGen do it, because it's fairly trivial. Eliminates some redundant state from the AST.
1 parent 9be16b2 commit f22a7e2

File tree

4 files changed

+13
-25
lines changed

4 files changed

+13
-25
lines changed

include/swift/AST/Stmt.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,6 @@ class ForEachStmt : public LabeledStmt {
808808

809809
// Set by Sema:
810810
ProtocolConformanceRef sequenceConformance = ProtocolConformanceRef();
811-
ConcreteDeclRef makeIterator;
812811
ConcreteDeclRef iteratorNext;
813812
VarDecl *iteratorVar = nullptr;
814813
Expr *iteratorVarRef = nullptr;
@@ -838,9 +837,6 @@ class ForEachStmt : public LabeledStmt {
838837
void setConvertElementExpr(Expr *expr) { convertElementExpr = expr; }
839838
Expr *getConvertElementExpr() const { return convertElementExpr; }
840839

841-
void setMakeIterator(ConcreteDeclRef declRef) { makeIterator = declRef; }
842-
ConcreteDeclRef getMakeIterator() const { return makeIterator; }
843-
844840
void setIteratorNext(ConcreteDeclRef declRef) { iteratorNext = declRef; }
845841
ConcreteDeclRef getIteratorNext() const { return iteratorNext; }
846842

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,9 +1593,6 @@ class PrintStmt : public StmtVisitor<PrintStmt> {
15931593
}
15941594
void visitForEachStmt(ForEachStmt *S) {
15951595
printCommon(S, "for_each_stmt");
1596-
PrintWithColorRAII(OS, LiteralValueColor) << " make_generator=";
1597-
S->getMakeIterator().dump(
1598-
PrintWithColorRAII(OS, LiteralValueColor).getOS());
15991596
PrintWithColorRAII(OS, LiteralValueColor) << " next=";
16001597
S->getIteratorNext().dump(
16011598
PrintWithColorRAII(OS, LiteralValueColor).getOS());

lib/SILGen/SILGenStmt.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,20 @@ void StmtEmitter::visitForEachStmt(ForEachStmt *S) {
909909
auto initialization =
910910
SGF.emitInitializationForVarDecl(S->getIteratorVar(), false);
911911
SILLocation loc = SILLocation(S->getSequence());
912+
913+
// Compute the reference to the Sequence's makeIterator().
914+
FuncDecl *makeIteratorReq = SGF.getASTContext().getSequenceMakeIterator();
915+
auto sequenceProto =
916+
SGF.getASTContext().getProtocol(KnownProtocolKind::Sequence);
917+
auto sequenceConformance = S->getSequenceConformance();
918+
Type sequenceType = S->getSequence()->getType();
919+
auto sequenceSubs = SubstitutionMap::getProtocolSubstitutions(
920+
sequenceProto, sequenceType, sequenceConformance);
921+
ConcreteDeclRef makeIteratorRef(makeIteratorReq, sequenceSubs);
922+
923+
// Call makeIterator().
912924
RValue result = SGF.emitApplyMethod(
913-
loc, S->getMakeIterator(), ArgumentSource(S->getSequence()),
925+
loc, makeIteratorRef, ArgumentSource(S->getSequence()),
914926
PreparedArguments(ArrayRef<AnyFunctionType::Param>({})),
915927
SGFContext(initialization.get()));
916928
if (!result.isInContext()) {

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,17 +3013,8 @@ auto TypeChecker::typeCheckForEachBinding(
30133013

30143014
// Perform any necessary conversions of the sequence (e.g. [T]! -> [T]).
30153015
expr = solution.coerceToType(expr, SequenceType, Locator);
3016-
30173016
if (!expr) return nullptr;
30183017

3019-
// Convert the sequence as appropriate for the makeIterator() call.
3020-
auto makeIteratorOverload = solution.getOverloadChoice(ContextualLocator);
3021-
auto makeIteratorSelfType = solution.simplifyType(
3022-
makeIteratorOverload.openedFullType
3023-
)->castTo<AnyFunctionType>()->getParams()[0].getPlainType();
3024-
expr = solution.coerceToType(expr, makeIteratorSelfType,
3025-
ContextualLocator);
3026-
30273018
cs.cacheExprTypes(expr);
30283019
Stmt->setSequence(expr);
30293020

@@ -3054,14 +3045,6 @@ auto TypeChecker::typeCheckForEachBinding(
30543045
ConformanceCheckFlags::InExpression,
30553046
expr->getLoc());
30563047

3057-
// Record the makeIterator declaration we used.
3058-
auto makeIteratorDecl = makeIteratorOverload.choice.getDecl();
3059-
auto makeIteratorSubs = SequenceType->getMemberSubstitutionMap(
3060-
cs.DC->getParentModule(), makeIteratorDecl);
3061-
auto makeIteratorDeclRef =
3062-
ConcreteDeclRef(makeIteratorDecl, makeIteratorSubs);
3063-
Stmt->setMakeIterator(makeIteratorDeclRef);
3064-
30653048
solution.setExprTypes(expr);
30663049
return expr;
30673050
}

0 commit comments

Comments
 (0)