Skip to content

Commit f0513ae

Browse files
committed
[Clang importer] Lazily synthesize error domain getter body
1 parent 55f619b commit f0513ae

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,26 @@ static void inferProtocolMemberAvailability(ClangImporter::Implementation &impl,
19151915
applyAvailableAttribute(valueDecl, requiredRange, C);
19161916
}
19171917

1918+
/// Synthesizer callback for the error domain property getter.
1919+
static std::pair<BraceStmt *, bool>
1920+
synthesizeErrorDomainGetterBody(AbstractFunctionDecl *afd, void *context) {
1921+
auto getterDecl = cast<AccessorDecl>(afd);
1922+
ASTContext &ctx = getterDecl->getASTContext();
1923+
1924+
auto contextData =
1925+
llvm::PointerIntPair<ValueDecl *, 1, bool>::getFromOpaqueValue(context);
1926+
auto swiftValueDecl = contextData.getPointer();
1927+
bool isImplicit = contextData.getInt();
1928+
DeclRefExpr *domainDeclRef = new (ctx)
1929+
DeclRefExpr(ConcreteDeclRef(swiftValueDecl), {}, isImplicit);
1930+
domainDeclRef->setType(
1931+
getterDecl->mapTypeIntoContext(swiftValueDecl->getInterfaceType()));
1932+
1933+
auto ret = new (ctx) ReturnStmt(SourceLoc(), domainDeclRef);
1934+
return { BraceStmt::create(ctx, SourceLoc(), {ret}, SourceLoc(), isImplicit),
1935+
/*isTypeChecked=*/true };
1936+
}
1937+
19181938
/// Add a domain error member, as required by conformance to
19191939
/// _BridgedStoredNSError.
19201940
/// \returns true on success, false on failure
@@ -1942,10 +1962,6 @@ static bool addErrorDomain(NominalTypeDecl *swiftDecl,
19421962
errorDomainPropertyDecl->setValidationToChecked();
19431963
errorDomainPropertyDecl->setAccess(AccessLevel::Public);
19441964

1945-
DeclRefExpr *domainDeclRef = new (C)
1946-
DeclRefExpr(ConcreteDeclRef(swiftValueDecl), {}, isImplicit);
1947-
domainDeclRef->setType(swiftValueDecl->getInterfaceType());
1948-
19491965
auto *params = ParameterList::createEmpty(C);
19501966

19511967
auto getterDecl = AccessorDecl::create(C,
@@ -1974,10 +1990,10 @@ static bool addErrorDomain(NominalTypeDecl *swiftDecl,
19741990
getterDecl->setStatic(isStatic);
19751991
getterDecl->setAccess(AccessLevel::Public);
19761992

1977-
auto ret = new (C) ReturnStmt(SourceLoc(), domainDeclRef);
1978-
getterDecl->setBody(
1979-
BraceStmt::create(C, SourceLoc(), {ret}, SourceLoc(), isImplicit),
1980-
AbstractFunctionDecl::BodyKind::TypeChecked);
1993+
llvm::PointerIntPair<ValueDecl *, 1, bool> contextData(swiftValueDecl,
1994+
isImplicit);
1995+
getterDecl->setBodySynthesizer(synthesizeErrorDomainGetterBody,
1996+
contextData.getOpaqueValue());
19811997

19821998
return true;
19831999
}

0 commit comments

Comments
 (0)