Skip to content

Commit 76af5c5

Browse files
committed
[silgen] Centralize SwitchEnumBuilder and SwitchCaseFullExpr into the same file: SwitchEnumBuilder.{h,cpp}.
Previously SwitchEnumBuilder was in SILGenBuilder.{h,cpp} and SwitchEnumCaseFullExpr was in its own file. This really made no sense since: 1. The two classes are inherently related to each other so really should be together in the source base. 2. SwitchEnumBuilder uses a SILGenBuilder, but is really a separate independent concept/entity, so there really is no reason to keep it in SILGenBuilder.cpp. Just a quick fix to eliminate something that was bugging me. NFC.
1 parent 3802c98 commit 76af5c5

10 files changed

+297
-296
lines changed

lib/SILGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ add_swift_library(swiftSILGen STATIC
77
ResultPlan.cpp
88
RValue.cpp
99
Scope.cpp
10-
SwitchCaseFullExpr.cpp
10+
SwitchEnumBuilder.cpp
1111
SILGen.cpp
1212
SILGenApply.cpp
1313
SILGenBridging.cpp

lib/SILGen/SILGenBuilder.cpp

Lines changed: 1 addition & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "RValue.h"
1616
#include "SILGenFunction.h"
1717
#include "Scope.h"
18-
#include "SwitchCaseFullExpr.h"
18+
#include "SwitchEnumBuilder.h"
1919
#include "swift/AST/GenericSignature.h"
2020
#include "swift/AST/SubstitutionMap.h"
2121

@@ -833,120 +833,3 @@ ManagedValue SILGenBuilder::createTuple(SILLocation loc, SILType type,
833833
});
834834
return cloner.clone(createTuple(loc, type, forwardedValues));
835835
}
836-
837-
//===----------------------------------------------------------------------===//
838-
// Switch Enum Builder
839-
//===----------------------------------------------------------------------===//
840-
841-
void SwitchEnumBuilder::emit() && {
842-
bool isAddressOnly = optional.getType().isAddressOnly(builder.getModule()) &&
843-
getSGF().silConv.useLoweredAddresses();
844-
using DeclBlockPair = std::pair<EnumElementDecl *, SILBasicBlock *>;
845-
{
846-
// TODO: We could store the data in CaseBB form and not have to do this.
847-
llvm::SmallVector<DeclBlockPair, 8> caseBlocks;
848-
llvm::SmallVector<ProfileCounter, 8> caseBlockCounts;
849-
std::transform(caseDataArray.begin(), caseDataArray.end(),
850-
std::back_inserter(caseBlocks),
851-
[](NormalCaseData &caseData) -> DeclBlockPair {
852-
return {caseData.decl, caseData.block};
853-
});
854-
std::transform(caseDataArray.begin(), caseDataArray.end(),
855-
std::back_inserter(caseBlockCounts),
856-
[](NormalCaseData &caseData) -> ProfileCounter {
857-
return caseData.count;
858-
});
859-
SILBasicBlock *defaultBlock =
860-
defaultBlockData ? defaultBlockData->block : nullptr;
861-
ProfileCounter defaultBlockCount =
862-
defaultBlockData ? defaultBlockData->count : ProfileCounter();
863-
ArrayRef<ProfileCounter> caseBlockCountsRef = caseBlockCounts;
864-
if (isAddressOnly) {
865-
builder.createSwitchEnumAddr(loc, optional.getValue(), defaultBlock,
866-
caseBlocks, caseBlockCountsRef,
867-
defaultBlockCount);
868-
} else {
869-
if (optional.getType().isAddress()) {
870-
// TODO: Refactor this into a maybe load.
871-
if (optional.hasCleanup()) {
872-
optional = builder.createLoadTake(loc, optional);
873-
} else {
874-
optional = builder.createLoadCopy(loc, optional);
875-
}
876-
}
877-
builder.createSwitchEnum(loc, optional.forward(getSGF()), defaultBlock,
878-
caseBlocks, caseBlockCountsRef,
879-
defaultBlockCount);
880-
}
881-
}
882-
883-
// If we are asked to create a default block and it is specified that the
884-
// default block should be emitted before normal cases, emit it now.
885-
if (defaultBlockData &&
886-
defaultBlockData->dispatchTime ==
887-
DefaultDispatchTime::BeforeNormalCases) {
888-
SILBasicBlock *defaultBlock = defaultBlockData->block;
889-
NullablePtr<SILBasicBlock> contBB = defaultBlockData->contBlock;
890-
DefaultCaseHandler handler = defaultBlockData->handler;
891-
892-
// Don't allow cleanups to escape the conditional block.
893-
SwitchCaseFullExpr presentScope(builder.getSILGenFunction(),
894-
CleanupLocation::get(loc),
895-
contBB.getPtrOrNull());
896-
builder.emitBlock(defaultBlock);
897-
ManagedValue input = optional;
898-
if (!isAddressOnly) {
899-
input = builder.createOwnedPHIArgument(optional.getType());
900-
}
901-
handler(input, presentScope);
902-
assert(!builder.hasValidInsertionPoint());
903-
}
904-
905-
for (NormalCaseData &caseData : caseDataArray) {
906-
EnumElementDecl *decl = caseData.decl;
907-
SILBasicBlock *caseBlock = caseData.block;
908-
NullablePtr<SILBasicBlock> contBlock = caseData.contBlock;
909-
NormalCaseHandler handler = caseData.handler;
910-
911-
// Don't allow cleanups to escape the conditional block.
912-
SwitchCaseFullExpr presentScope(builder.getSILGenFunction(),
913-
CleanupLocation::get(loc),
914-
contBlock.getPtrOrNull());
915-
916-
builder.emitBlock(caseBlock);
917-
918-
ManagedValue input;
919-
if (decl->hasAssociatedValues()) {
920-
// Pull the payload out if we have one.
921-
SILType inputType =
922-
optional.getType().getEnumElementType(decl, builder.getModule());
923-
input = optional;
924-
if (!isAddressOnly) {
925-
input = builder.createOwnedPHIArgument(inputType);
926-
}
927-
}
928-
handler(input, presentScope);
929-
assert(!builder.hasValidInsertionPoint());
930-
}
931-
932-
// If we are asked to create a default block and it is specified that the
933-
// default block should be emitted after normal cases, emit it now.
934-
if (defaultBlockData &&
935-
defaultBlockData->dispatchTime == DefaultDispatchTime::AfterNormalCases) {
936-
SILBasicBlock *defaultBlock = defaultBlockData->block;
937-
NullablePtr<SILBasicBlock> contBB = defaultBlockData->contBlock;
938-
DefaultCaseHandler handler = defaultBlockData->handler;
939-
940-
// Don't allow cleanups to escape the conditional block.
941-
SwitchCaseFullExpr presentScope(builder.getSILGenFunction(),
942-
CleanupLocation::get(loc),
943-
contBB.getPtrOrNull());
944-
builder.emitBlock(defaultBlock);
945-
ManagedValue input = optional;
946-
if (!isAddressOnly) {
947-
input = builder.createOwnedPHIArgument(optional.getType());
948-
}
949-
handler(input, presentScope);
950-
assert(!builder.hasValidInsertionPoint());
951-
}
952-
}

lib/SILGen/SILGenBuilder.h

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -372,86 +372,6 @@ class SILGenBuilder : public SILBuilder {
372372
ReturnInst *createReturn(SILLocation Loc, ManagedValue ReturnValue);
373373
};
374374

375-
class SwitchCaseFullExpr;
376-
377-
/// A class for building switch enums that handles all of the ownership
378-
/// requirements for the user.
379-
///
380-
/// It assumes that the user passes in a block that takes in a ManagedValue and
381-
/// returns a ManagedValue for the blocks exit argument. Should return an empty
382-
/// ManagedValue to signal no result.
383-
class SwitchEnumBuilder {
384-
public:
385-
using NormalCaseHandler =
386-
std::function<void(ManagedValue, SwitchCaseFullExpr &)>;
387-
using DefaultCaseHandler =
388-
std::function<void(ManagedValue, SwitchCaseFullExpr &)>;
389-
390-
enum class DefaultDispatchTime { BeforeNormalCases, AfterNormalCases };
391-
392-
private:
393-
struct NormalCaseData {
394-
EnumElementDecl *decl;
395-
SILBasicBlock *block;
396-
NullablePtr<SILBasicBlock> contBlock;
397-
NormalCaseHandler handler;
398-
ProfileCounter count;
399-
400-
NormalCaseData(EnumElementDecl *decl, SILBasicBlock *block,
401-
NullablePtr<SILBasicBlock> contBlock,
402-
NormalCaseHandler handler, ProfileCounter count)
403-
: decl(decl), block(block), contBlock(contBlock), handler(handler),
404-
count(count) {}
405-
~NormalCaseData() = default;
406-
};
407-
408-
struct DefaultCaseData {
409-
SILBasicBlock *block;
410-
NullablePtr<SILBasicBlock> contBlock;
411-
DefaultCaseHandler handler;
412-
DefaultDispatchTime dispatchTime;
413-
ProfileCounter count;
414-
415-
DefaultCaseData(SILBasicBlock *block, NullablePtr<SILBasicBlock> contBlock,
416-
DefaultCaseHandler handler,
417-
DefaultDispatchTime dispatchTime, ProfileCounter count)
418-
: block(block), contBlock(contBlock), handler(handler),
419-
dispatchTime(dispatchTime), count(count) {}
420-
~DefaultCaseData() = default;
421-
};
422-
423-
SILGenBuilder &builder;
424-
SILLocation loc;
425-
ManagedValue optional;
426-
llvm::Optional<DefaultCaseData> defaultBlockData;
427-
llvm::SmallVector<NormalCaseData, 8> caseDataArray;
428-
429-
public:
430-
SwitchEnumBuilder(SILGenBuilder &builder, SILLocation loc,
431-
ManagedValue optional)
432-
: builder(builder), loc(loc), optional(optional) {}
433-
434-
void addDefaultCase(
435-
SILBasicBlock *defaultBlock, NullablePtr<SILBasicBlock> contBlock,
436-
DefaultCaseHandler handle,
437-
DefaultDispatchTime dispatchTime = DefaultDispatchTime::AfterNormalCases,
438-
ProfileCounter count = ProfileCounter()) {
439-
defaultBlockData.emplace(defaultBlock, contBlock, handle, dispatchTime,
440-
count);
441-
}
442-
443-
void addCase(EnumElementDecl *decl, SILBasicBlock *caseBlock,
444-
NullablePtr<SILBasicBlock> contBlock, NormalCaseHandler handle,
445-
ProfileCounter count = ProfileCounter()) {
446-
caseDataArray.emplace_back(decl, caseBlock, contBlock, handle, count);
447-
}
448-
449-
void emit() &&;
450-
451-
private:
452-
SILGenFunction &getSGF() const { return builder.getSILGenFunction(); }
453-
};
454-
455375
} // namespace Lowering
456376
} // namespace swift
457377

lib/SILGen/SILGenConvert.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "LValue.h"
1818
#include "RValue.h"
1919
#include "Scope.h"
20-
#include "SwitchCaseFullExpr.h"
20+
#include "SwitchEnumBuilder.h"
2121
#include "swift/AST/ASTContext.h"
2222
#include "swift/AST/Decl.h"
2323
#include "swift/AST/ProtocolConformance.h"

lib/SILGen/SILGenDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "SILGen.h"
1717
#include "SILGenDynamicCast.h"
1818
#include "Scope.h"
19-
#include "SwitchCaseFullExpr.h"
19+
#include "SwitchEnumBuilder.h"
2020
#include "swift/AST/ASTMangler.h"
2121
#include "swift/AST/GenericEnvironment.h"
2222
#include "swift/AST/Module.h"

lib/SILGen/SILGenStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "RValue.h"
1717
#include "SILGen.h"
1818
#include "Scope.h"
19-
#include "SwitchCaseFullExpr.h"
19+
#include "SwitchEnumBuilder.h"
2020
#include "swift/AST/DiagnosticsSIL.h"
2121
#include "swift/Basic/ProfileCounter.h"
2222
#include "swift/SIL/SILArgument.h"

lib/SILGen/SwitchCaseFullExpr.cpp

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/SILGen/SwitchCaseFullExpr.h

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)