Skip to content

Commit 2970c3c

Browse files
committed
Sema: Small optimization for StoredPropertiesRequest
If the nominal type was imported or deserialized, we don't have to worry about unlowered lazy properties or property wrappers. Just scan through the members looking for stored properties and that's it.
1 parent 3e45b32 commit 2970c3c

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -946,56 +946,44 @@ static bool contextAllowsPatternBindingWithoutVariables(DeclContext *dc) {
946946
return true;
947947
}
948948

949-
static VarDecl *getStoredPropertyFor(VarDecl *var) {
950-
if (var->isStatic())
951-
return nullptr;
952-
953-
if (var->isLazyStorageProperty())
954-
return nullptr;
949+
static bool hasStoredProperties(NominalTypeDecl *decl) {
950+
return (isa<StructDecl>(decl) ||
951+
(isa<ClassDecl>(decl) && !decl->hasClangNode()));
952+
}
955953

956-
if (var->getOriginalWrappedProperty())
957-
return nullptr;
954+
static void computeLoweredStoredProperties(NominalTypeDecl *decl) {
955+
// Just walk over the members of the type, forcing backing storage
956+
// for lazy properties and property wrappers to be synthesized.
957+
for (auto *member : decl->getMembers()) {
958+
auto *var = dyn_cast<VarDecl>(member);
959+
if (!var || var->isStatic())
960+
continue;
958961

959-
if (var->getAttrs().hasAttribute<LazyAttr>()) {
960-
if (auto *storage = var->getLazyStorageProperty()) {
961-
assert(storage->hasStorage());
962-
return storage;
963-
}
962+
if (var->getAttrs().hasAttribute<LazyAttr>())
963+
(void) var->getLazyStorageProperty();
964964

965-
return nullptr;
965+
if (var->hasAttachedPropertyWrapper())
966+
(void) var->getPropertyWrapperBackingProperty();
966967
}
967-
968-
if (var->hasAttachedPropertyWrapper()) {
969-
if (auto *storage = var->getPropertyWrapperBackingProperty()) {
970-
assert(storage->hasStorage());
971-
return storage;
972-
}
973-
974-
return nullptr;
975-
}
976-
977-
if (var->getAttrs().hasAttribute<NSManagedAttr>())
978-
return nullptr;
979-
980-
if (!var->hasStorage())
981-
return nullptr;
982-
983-
return var;
984968
}
985969

986970
llvm::Expected<ArrayRef<VarDecl *>>
987971
StoredPropertiesRequest::evaluate(Evaluator &evaluator,
988972
NominalTypeDecl *decl) const {
989-
if (isa<EnumDecl>(decl) ||
990-
isa<ProtocolDecl>(decl) ||
991-
(isa<ClassDecl>(decl) && decl->hasClangNode()))
973+
if (!hasStoredProperties(decl))
992974
return ArrayRef<VarDecl *>();
993975

994976
SmallVector<VarDecl *, 4> results;
977+
978+
// Unless we're in a source file we don't have to do anything
979+
// special to lower lazy properties and property wrappers.
980+
if (isa<SourceFile>(decl->getModuleScopeContext()))
981+
computeLoweredStoredProperties(decl);
982+
995983
for (auto *member : decl->getMembers()) {
996984
if (auto *var = dyn_cast<VarDecl>(member))
997-
if (auto *storage = getStoredPropertyFor(var))
998-
results.push_back(storage);
985+
if (!var->isStatic() && var->hasStorage())
986+
results.push_back(var);
999987
}
1000988

1001989
return decl->getASTContext().AllocateCopy(results);
@@ -1004,16 +992,20 @@ StoredPropertiesRequest::evaluate(Evaluator &evaluator,
1004992
llvm::Expected<ArrayRef<Decl *>>
1005993
StoredPropertiesAndMissingMembersRequest::evaluate(Evaluator &evaluator,
1006994
NominalTypeDecl *decl) const {
1007-
if (isa<EnumDecl>(decl) ||
1008-
isa<ProtocolDecl>(decl) ||
1009-
(isa<ClassDecl>(decl) && decl->hasClangNode()))
995+
if (!hasStoredProperties(decl))
1010996
return ArrayRef<Decl *>();
1011997

1012998
SmallVector<Decl *, 4> results;
999+
1000+
// Unless we're in a source file we don't have to do anything
1001+
// special to lower lazy properties and property wrappers.
1002+
if (isa<SourceFile>(decl->getModuleScopeContext()))
1003+
computeLoweredStoredProperties(decl);
1004+
10131005
for (auto *member : decl->getMembers()) {
10141006
if (auto *var = dyn_cast<VarDecl>(member))
1015-
if (auto *storage = getStoredPropertyFor(var))
1016-
results.push_back(storage);
1007+
if (!var->isStatic() && var->hasStorage())
1008+
results.push_back(var);
10171009

10181010
if (auto missing = dyn_cast<MissingMemberDecl>(member))
10191011
if (missing->getNumberOfFieldOffsetVectorEntries() > 0)

0 commit comments

Comments
 (0)