|
24 | 24 | #include "swift/AST/Stmt.h" |
25 | 25 | #include "swift/AST/TypeCheckRequests.h" |
26 | 26 | #include "swift/Basic/Assertions.h" |
| 27 | +#include "swift/Basic/SourceLoc.h" |
27 | 28 | #include "swift/ClangImporter/ClangImporterRequests.h" |
| 29 | +#include "clang/AST/DeclCXX.h" |
| 30 | +#include "clang/AST/DeclarationName.h" |
| 31 | +#include "clang/AST/Expr.h" |
| 32 | +#include "clang/AST/ExprCXX.h" |
28 | 33 | #include "clang/AST/Mangle.h" |
| 34 | +#include "clang/AST/Stmt.h" |
| 35 | +#include "clang/AST/Type.h" |
| 36 | +#include "clang/Basic/SourceLocation.h" |
29 | 37 | #include "clang/Sema/DelayedDiagnostic.h" |
30 | 38 |
|
31 | 39 | using namespace swift; |
@@ -749,6 +757,53 @@ ConstructorDecl *SwiftDeclSynthesizer::createRawValueBridgingConstructor( |
749 | 757 | return init; |
750 | 758 | } |
751 | 759 |
|
| 760 | +static std::pair<BraceStmt *, bool> |
| 761 | +synthesizeAsReferenceBody(AbstractFunctionDecl *afd, void *context) { |
| 762 | + auto getterDecl = cast<AccessorDecl>(afd); |
| 763 | + auto getterImpl = static_cast<FuncDecl *>(context); |
| 764 | + |
| 765 | + ASTContext &ctx = getterDecl->getASTContext(); |
| 766 | + |
| 767 | + auto selfArg = createSelfArg(getterDecl); |
| 768 | + auto *getterImplCallExpr = |
| 769 | + createAccessorImplCallExpr(getterImpl, selfArg, {}); |
| 770 | + |
| 771 | + auto *returnStmt = ReturnStmt::createImplicit(ctx, getterImplCallExpr); |
| 772 | + |
| 773 | + auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(), |
| 774 | + /*implicit*/ true); |
| 775 | + return {body, /*isTypeChecked*/ true}; |
| 776 | +} |
| 777 | + |
| 778 | +VarDecl *SwiftDeclSynthesizer::createSmartPtrBridgingProperty( |
| 779 | + FuncDecl *bridgingFunction) { |
| 780 | + auto smartPtrType = bridgingFunction->getDeclContext(); |
| 781 | + auto referenceType = bridgingFunction->getResultInterfaceType(); |
| 782 | + auto result = new (ImporterImpl.SwiftContext) VarDecl( |
| 783 | + /*isStatic*/ false, VarDecl::Introducer::Var, |
| 784 | + bridgingFunction->getStartLoc(), |
| 785 | + ImporterImpl.SwiftContext.getIdentifier("asReference"), smartPtrType); |
| 786 | + result->setInterfaceType(referenceType); |
| 787 | + result->copyFormalAccessFrom(bridgingFunction); |
| 788 | + |
| 789 | + AccessorDecl *getterDecl = AccessorDecl::create( |
| 790 | + ImporterImpl.SwiftContext, bridgingFunction->getLoc(), |
| 791 | + bridgingFunction->getLoc(), AccessorKind::Get, result, |
| 792 | + /*async*/ false, SourceLoc(), |
| 793 | + /*throws*/ false, SourceLoc(), /*ThrownType=*/TypeLoc(), |
| 794 | + ParameterList::createEmpty(ImporterImpl.SwiftContext), referenceType, |
| 795 | + smartPtrType); |
| 796 | + getterDecl->copyFormalAccessFrom(bridgingFunction); |
| 797 | + getterDecl->setImplicit(); |
| 798 | + getterDecl->setIsDynamic(false); |
| 799 | + getterDecl->setIsTransparent(true); |
| 800 | + getterDecl->setBodySynthesizer(synthesizeAsReferenceBody, bridgingFunction); |
| 801 | + getterDecl->setSelfAccessKind(SelfAccessKind::NonMutating); |
| 802 | + result->setIsGetterMutating(false); |
| 803 | + ImporterImpl.makeComputed(result, getterDecl, nullptr); |
| 804 | + return result; |
| 805 | +} |
| 806 | + |
752 | 807 | void SwiftDeclSynthesizer::makeStructRawValuedWithBridge( |
753 | 808 | StructDecl *structDecl, Type storedUnderlyingType, Type bridgedType, |
754 | 809 | ArrayRef<KnownProtocolKind> synthesizedProtocolAttrs, |
|
0 commit comments