Skip to content

Commit b18d168

Browse files
committed
SILGen: Emit accessors as part of their storage
This removes an intricate set of invariants that must be kept consistent between Parse and SILGen. It will also make it easier to implement local variables with 'lazy' and property wrappers in the future.
1 parent 0f2b753 commit b18d168

File tree

4 files changed

+17
-38
lines changed

4 files changed

+17
-38
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,48 +1316,17 @@ void SILGenModule::visitVarDecl(VarDecl *vd) {
13161316
if (vd->hasStorage())
13171317
addGlobalVariable(vd);
13181318

1319-
// Emit the variable's opaque accessors.
1320-
vd->visitExpectedOpaqueAccessors([&](AccessorKind kind) {
1321-
auto accessor = vd->getAccessor(kind);
1322-
if (!accessor) return;
1323-
1324-
// Only emit the accessor if it wasn't added to the surrounding decl
1325-
// list by the parser. We can test that easily by looking at the impl
1326-
// info, since all of these accessors have a corresponding access kind
1327-
// whose impl should definitely point at the accessor if it was parsed.
1328-
//
1329-
// This is an unfortunate formation rule, but it's easier than messing
1330-
// with the invariants for now.
1331-
bool shouldEmit = [&] {
1332-
auto impl = vd->getImplInfo();
1333-
switch (kind) {
1334-
case AccessorKind::Get:
1335-
return impl.getReadImpl() != ReadImplKind::Get &&
1336-
!(impl.getReadImpl() == ReadImplKind::Stored &&
1337-
impl.getWriteImpl() == WriteImplKind::StoredWithObservers);
1338-
case AccessorKind::Read:
1339-
return impl.getReadImpl() != ReadImplKind::Read;
1340-
case AccessorKind::Set:
1341-
return impl.getWriteImpl() != WriteImplKind::Set &&
1342-
impl.getWriteImpl() != WriteImplKind::StoredWithObservers;
1343-
case AccessorKind::Modify:
1344-
return impl.getReadWriteImpl() != ReadWriteImplKind::Modify;
1345-
#define ACCESSOR(ID) \
1346-
case AccessorKind::ID:
1347-
#define OPAQUE_ACCESSOR(ID, KEYWORD)
1348-
#include "swift/AST/AccessorKinds.def"
1349-
llvm_unreachable("not an opaque accessor");
1350-
}
1351-
llvm_unreachable("covered switch");
1352-
}();
1353-
if (!shouldEmit) return;
1354-
1319+
for (auto *accessor : vd->getAllAccessors())
13551320
emitFunction(accessor);
1356-
});
13571321

13581322
tryEmitPropertyDescriptor(vd);
13591323
}
13601324

1325+
void SILGenModule::visitSubscriptDecl(SubscriptDecl *sd) {
1326+
for (auto *accessor : sd->getAllAccessors())
1327+
emitFunction(accessor);
1328+
}
1329+
13611330
bool
13621331
SILGenModule::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl,
13631332
ResilienceExpansion expansion) {

lib/SILGen/SILGen.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
188188
void visitTypeAliasDecl(TypeAliasDecl *d) {}
189189
void visitOpaqueTypeDecl(OpaqueTypeDecl *d) {}
190190
void visitAbstractTypeParamDecl(AbstractTypeParamDecl *d) {}
191-
void visitSubscriptDecl(SubscriptDecl *d) {}
192191
void visitConstructorDecl(ConstructorDecl *d) {}
193192
void visitDestructorDecl(DestructorDecl *d) {}
194193
void visitModuleDecl(ModuleDecl *d) { }
195194
void visitMissingMemberDecl(MissingMemberDecl *d) {}
196195

196+
// Emitted as part of its storage.
197+
void visitAccessorDecl(AccessorDecl *ad) {}
198+
197199
void visitFuncDecl(FuncDecl *fd);
198200
void visitPatternBindingDecl(PatternBindingDecl *vd);
199201
void visitTopLevelCodeDecl(TopLevelCodeDecl *td);
@@ -202,6 +204,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
202204
void visitNominalTypeDecl(NominalTypeDecl *ntd);
203205
void visitExtensionDecl(ExtensionDecl *ed);
204206
void visitVarDecl(VarDecl *vd);
207+
void visitSubscriptDecl(SubscriptDecl *sd);
205208

206209
void emitAbstractFuncDecl(AbstractFunctionDecl *AFD);
207210

lib/SILGen/SILGenDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,10 @@ void SILGenFunction::visitPatternBindingDecl(PatternBindingDecl *PBD) {
11921192

11931193
void SILGenFunction::visitVarDecl(VarDecl *D) {
11941194
// We handle emitting the variable storage when we see the pattern binding.
1195+
1196+
// Emit the variable's accessors.
1197+
for (auto *accessor : D->getAllAccessors())
1198+
SGM.emitFunction(accessor);
11951199
}
11961200

11971201
/// Emit literals for the major, minor, and subminor components of the version

lib/SILGen/SILGenFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,9 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
17871787
llvm_unreachable("Not yet implemented");
17881788
}
17891789

1790+
// Emitted as part of its storage.
1791+
void visitAccessorDecl(AccessorDecl *D) {}
1792+
17901793
void visitFuncDecl(FuncDecl *D);
17911794
void visitPatternBindingDecl(PatternBindingDecl *D);
17921795

0 commit comments

Comments
 (0)