Skip to content

Commit 56515ee

Browse files
authored
Merge pull request swiftlang#26119 from slavapestov/stored-properties-request
Re-implement NominalTypeDecl::getStoredProperties() using request evaluator
2 parents 7e63560 + d0240cc commit 56515ee

33 files changed

+230
-185
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
//===----------------------------------------------------------------------===//
1717
SWIFT_TYPEID_NAMED(NominalTypeDecl *, NominalTypeDecl)
1818
SWIFT_TYPEID_NAMED(VarDecl *, VarDecl)
19+
SWIFT_TYPEID_NAMED(Decl *, Decl)
1920
SWIFT_TYPEID(Type)
2021
SWIFT_TYPEID(PropertyWrapperBackingPropertyInfo)
2122
SWIFT_TYPEID(PropertyWrapperTypeInfo)

include/swift/AST/Decl.h

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,40 +3451,12 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
34513451
/// Retrieve information about this type as a property wrapper.
34523452
PropertyWrapperTypeInfo getPropertyWrapperTypeInfo() const;
34533453

3454-
private:
3455-
/// Predicate used to filter StoredPropertyRange.
3456-
struct ToStoredProperty {
3457-
ToStoredProperty() {}
3458-
Optional<VarDecl *> operator()(Decl *decl) const;
3459-
};
3460-
3461-
public:
3462-
/// A range for iterating the stored member variables of a structure.
3463-
using StoredPropertyRange = OptionalTransformRange<DeclRange,
3464-
ToStoredProperty>;
3465-
34663454
/// Return a collection of the stored member variables of this type.
3467-
StoredPropertyRange getStoredProperties() const;
3455+
ArrayRef<VarDecl *> getStoredProperties() const;
34683456

3469-
private:
3470-
/// Predicate used to filter StoredPropertyRange.
3471-
struct ToStoredPropertyOrMissingMemberPlaceholder {
3472-
Optional<Decl *> operator()(Decl *decl) const;
3473-
};
3474-
3475-
public:
3476-
/// A range for iterating the stored member variables of a structure.
3477-
using StoredPropertyOrMissingMemberPlaceholderRange
3478-
= OptionalTransformRange<DeclRange,
3479-
ToStoredPropertyOrMissingMemberPlaceholder>;
3480-
34813457
/// Return a collection of the stored member variables of this type, along
34823458
/// with placeholders for unimportable stored properties.
3483-
StoredPropertyOrMissingMemberPlaceholderRange
3484-
getStoredPropertiesAndMissingMemberPlaceholders() const {
3485-
return StoredPropertyOrMissingMemberPlaceholderRange(getMembers(),
3486-
ToStoredPropertyOrMissingMemberPlaceholder());
3487-
}
3459+
ArrayRef<Decl *> getStoredPropertiesAndMissingMemberPlaceholders() const;
34883460

34893461
// Implement isa/cast/dyncast/etc.
34903462
static bool classof(const Decl *D) {

include/swift/AST/TypeCheckRequests.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,52 @@ class TypeCheckFunctionBodyUntilRequest :
713713
bool isCached() const { return true; }
714714
};
715715

716+
/// Request to obtain a list of stored properties in a nominal type.
717+
///
718+
/// This will include backing storage for lazy properties and
719+
/// property wrappers, synthesizing them if necessary.
720+
class StoredPropertiesRequest :
721+
public SimpleRequest<StoredPropertiesRequest,
722+
ArrayRef<VarDecl *>(NominalTypeDecl *),
723+
CacheKind::Cached> {
724+
public:
725+
using SimpleRequest::SimpleRequest;
726+
727+
private:
728+
friend SimpleRequest;
729+
730+
// Evaluation.
731+
llvm::Expected<ArrayRef<VarDecl *>>
732+
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
733+
734+
public:
735+
bool isCached() const { return true; }
736+
};
737+
738+
/// Request to obtain a list of stored properties in a nominal type,
739+
/// together with any missing members corresponding to stored
740+
/// properties that could not be deserialized.
741+
///
742+
/// This will include backing storage for lazy properties and
743+
/// property wrappers, synthesizing them if necessary.
744+
class StoredPropertiesAndMissingMembersRequest :
745+
public SimpleRequest<StoredPropertiesAndMissingMembersRequest,
746+
ArrayRef<Decl *>(NominalTypeDecl *),
747+
CacheKind::Cached> {
748+
public:
749+
using SimpleRequest::SimpleRequest;
750+
751+
private:
752+
friend SimpleRequest;
753+
754+
// Evaluation.
755+
llvm::Expected<ArrayRef<Decl *>>
756+
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
757+
758+
public:
759+
bool isCached() const { return true; }
760+
};
761+
716762
// Allow AnyValue to compare two Type values, even though Type doesn't
717763
// support ==.
718764
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ SWIFT_TYPEID(IsSetterMutatingRequest)
4141
SWIFT_TYPEID(OpaqueReadOwnershipRequest)
4242
SWIFT_TYPEID(LazyStoragePropertyRequest)
4343
SWIFT_TYPEID(TypeCheckFunctionBodyUntilRequest)
44+
SWIFT_TYPEID(StoredPropertiesRequest)
45+
SWIFT_TYPEID(StoredPropertiesAndMissingMembersRequest)

include/swift/Basic/CTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@ SWIFT_TYPEID_TEMPLATE1_NAMED(std::vector, Vector, typename T, T)
3939

4040
// LLVM ADT types.
4141
SWIFT_TYPEID_TEMPLATE1_NAMED(llvm::TinyPtrVector, TinyPtrVector, typename T, T)
42-
42+
SWIFT_TYPEID_TEMPLATE1_NAMED(llvm::ArrayRef, ArrayRef, typename T, T)

include/swift/Basic/SimpleDisplay.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ namespace swift {
107107
}
108108
out << "}";
109109
}
110+
111+
template<typename T>
112+
void simple_display(llvm::raw_ostream &out,
113+
const llvm::ArrayRef<T> &array) {
114+
out << "{";
115+
bool first = true;
116+
for (const T &value : array) {
117+
if (first) first = false;
118+
else out << ", ";
119+
120+
simple_display(out, value);
121+
}
122+
out << "}";
123+
}
110124
}
111125

112126
#endif // SWIFT_BASIC_SIMPLE_DISPLAY_H

include/swift/Basic/Statistics.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ FRONTEND_STATISTIC(AST, NumPrefixOperators)
138138
/// Number of precedence groups in the AST context.
139139
FRONTEND_STATISTIC(AST, NumPrecedenceGroups)
140140

141-
/// Number of precedence groups in the AST context.
142-
FRONTEND_STATISTIC(AST, NumStoredPropertiesQueries)
143-
144141
/// Number of full function bodies parsed.
145142
FRONTEND_STATISTIC(Parse, NumFunctionsParsed)
146143

include/swift/SIL/Projection.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,18 +317,14 @@ class Projection {
317317
llvm_unreachable("Unhandled ProjectionKind in switch.");
318318
}
319319

320-
/// WARNING: This is not a constant time operation because it requests all
321-
/// BaseType's stored properties.
322320
VarDecl *getVarDecl(SILType BaseType) const {
323321
assert(isValid());
324322
assert((getKind() == ProjectionKind::Struct ||
325323
getKind() == ProjectionKind::Class));
326324
assert(BaseType.getNominalOrBoundGenericNominal() &&
327325
"This should only be called with a nominal type");
328326
auto *NDecl = BaseType.getNominalOrBoundGenericNominal();
329-
auto Iter = NDecl->getStoredProperties().begin();
330-
std::advance(Iter, getIndex());
331-
return *Iter;
327+
return NDecl->getStoredProperties()[getIndex()];
332328
}
333329

334330
EnumElementDecl *getEnumElementDecl(SILType BaseType) const {

include/swift/SIL/SILInstruction.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4824,11 +4824,10 @@ class StructInst final : public InstructionBaseWithTrailingOperands<
48244824

48254825
StructDecl *S = getStructDecl();
48264826

4827-
NominalTypeDecl::StoredPropertyRange Range = S->getStoredProperties();
4828-
unsigned Index = 0;
4829-
for (auto I = Range.begin(), E = Range.end(); I != E; ++I, ++Index)
4830-
if (V == *I)
4831-
return &getAllOperands()[Index];
4827+
auto Props = S->getStoredProperties();
4828+
for (unsigned I = 0, E = Props.size(); I < E; ++I)
4829+
if (V == Props[I])
4830+
return &getAllOperands()[I];
48324831

48334832
// Did not find a matching VarDecl, return nullptr.
48344833
return nullptr;

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,16 +1172,13 @@ FuncDecl *ASTContext::getArrayReserveCapacityDecl() const {
11721172

11731173
StructDecl *IntDecl = IntType->getDecl();
11741174
auto StoredProperties = IntDecl->getStoredProperties();
1175-
auto FieldIter = StoredProperties.begin();
1176-
if (FieldIter == StoredProperties.end())
1175+
if (StoredProperties.size() != 1)
11771176
return nullptr;
1178-
VarDecl *field = *FieldIter;
1177+
VarDecl *field = StoredProperties[0];
11791178
if (field->hasClangNode())
11801179
return nullptr;
11811180
if (!field->getInterfaceType()->is<BuiltinIntegerType>())
11821181
return nullptr;
1183-
if (std::next(FieldIter) != StoredProperties.end())
1184-
return nullptr;
11851182

11861183
if (!FnDecl->getResultInterfaceType()->isVoid())
11871184
return nullptr;

0 commit comments

Comments
 (0)