Skip to content

Commit a331ba0

Browse files
[ConstraintSystem] Abstract common logic for various get property wrappers get information
1 parent 042939e commit a331ba0

File tree

1 file changed

+60
-37
lines changed

1 file changed

+60
-37
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -928,59 +928,82 @@ TypeVariableType *ConstraintSystem::isRepresentativeFor(
928928
return *member;
929929
}
930930

931-
Optional<std::pair<VarDecl *, Type>>
932-
ConstraintSystem::getStorageWrapperInformation(SelectedOverload resolvedOverload) {
931+
static Optional<std::pair<VarDecl *, Type>>
932+
getPropertyWrappersInformationFromOverload(
933+
SelectedOverload resolvedOverload, DeclContext *DC,
934+
llvm::function_ref<Optional<std::pair<VarDecl *, Type>>(VarDecl *)>
935+
getInformation) {
933936
if (resolvedOverload.choice.isDecl()) {
934937
if (auto *decl = dyn_cast<VarDecl>(resolvedOverload.choice.getDecl())) {
935-
if (decl->hasAttachedPropertyWrapper()) {
936-
if (auto storageWrapper = decl->getPropertyWrapperStorageWrapper()) {
937-
Type type = storageWrapper->getInterfaceType();
938-
if (Type baseType = resolvedOverload.choice.getBaseType()) {
939-
type = baseType->getTypeOfMember(DC->getParentModule(),
940-
storageWrapper, type);
941-
}
942-
return std::make_pair(decl, type);
938+
if (auto declInformation = getInformation(decl)) {
939+
Type type;
940+
VarDecl *memberDecl;
941+
std::tie(memberDecl, type) = *declInformation;
942+
if (Type baseType = resolvedOverload.choice.getBaseType()) {
943+
type = baseType->getTypeOfMember(DC->getParentModule(), memberDecl,
944+
type);
943945
}
946+
return std::make_pair(decl, type);
944947
}
945948
}
946949
}
947950
return None;
948951
}
949952

953+
static Optional<std::pair<VarDecl *, Type>>
954+
getStorageWrapperInformationFromDecl(VarDecl *decl) {
955+
if (!decl->hasAttachedPropertyWrapper())
956+
return None;
957+
958+
auto storageWrapper = decl->getPropertyWrapperStorageWrapper();
959+
if (!storageWrapper)
960+
return None;
961+
962+
return std::make_pair(storageWrapper, storageWrapper->getInterfaceType());
963+
}
964+
950965
Optional<std::pair<VarDecl *, Type>>
951-
ConstraintSystem::getPropertyWrapperInformation(SelectedOverload resolvedOverload) {
952-
if (resolvedOverload.choice.isDecl()) {
953-
if (auto *decl = dyn_cast<VarDecl>(resolvedOverload.choice.getDecl())) {
954-
if (decl->hasAttachedPropertyWrapper()) {
955-
auto wrapperTy = decl->getPropertyWrapperBackingPropertyType();
956-
if (Type baseType = resolvedOverload.choice.getBaseType()) {
957-
wrapperTy = baseType->getTypeOfMember(DC->getParentModule(),
958-
decl, wrapperTy);
959-
}
960-
return std::make_pair(decl, wrapperTy);
961-
}
962-
}
963-
}
964-
return None;
966+
ConstraintSystem::getStorageWrapperInformation(
967+
SelectedOverload resolvedOverload) {
968+
return getPropertyWrappersInformationFromOverload(
969+
resolvedOverload, DC,
970+
[](VarDecl *decl) { return getStorageWrapperInformationFromDecl(decl); });
971+
}
972+
973+
static Optional<std::pair<VarDecl *, Type>>
974+
getPropertyWrapperInformationFromDecl(VarDecl *decl) {
975+
if (!decl->hasAttachedPropertyWrapper())
976+
return None;
977+
978+
return std::make_pair(decl, decl->getPropertyWrapperBackingPropertyType());
965979
}
966980

967981
Optional<std::pair<VarDecl *, Type>>
968-
ConstraintSystem::getWrappedPropertyInformation(SelectedOverload resolvedOverload) {
969-
if (resolvedOverload.choice.isDecl()) {
970-
if (auto *decl = dyn_cast<VarDecl>(resolvedOverload.choice.getDecl())) {
971-
if (auto wrapped = decl->getOriginalWrappedProperty()) {
972-
Type type = wrapped->getInterfaceType();
973-
if (Type baseType = resolvedOverload.choice.getBaseType()) {
974-
type = baseType->getTypeOfMember(DC->getParentModule(),
975-
wrapped, type);
976-
}
977-
return std::make_pair(decl, type);
978-
}
979-
}
980-
}
982+
ConstraintSystem::getPropertyWrapperInformation(
983+
SelectedOverload resolvedOverload) {
984+
return getPropertyWrappersInformationFromOverload(
985+
resolvedOverload, DC, [](VarDecl *decl) {
986+
return getPropertyWrapperInformationFromDecl(decl);
987+
});
988+
}
989+
990+
static Optional<std::pair<VarDecl *, Type>>
991+
getWrappedPropertyInformationFromDecl(VarDecl *decl) {
992+
if (auto wrapped = decl->getOriginalWrappedProperty())
993+
return std::make_pair(decl, wrapped->getInterfaceType());
994+
981995
return None;
982996
}
983997

998+
Optional<std::pair<VarDecl *, Type>>
999+
ConstraintSystem::getWrappedPropertyInformation(
1000+
SelectedOverload resolvedOverload) {
1001+
return getPropertyWrappersInformationFromOverload(
1002+
resolvedOverload, DC, [](VarDecl *decl) {
1003+
return getWrappedPropertyInformationFromDecl(decl);
1004+
});
1005+
}
1006+
9841007
/// Does a var or subscript produce an l-value?
9851008
///
9861009
/// \param baseType - the type of the base on which this object

0 commit comments

Comments
 (0)