Files
swift-mirror/test/Interop/Cxx/class/Inputs/closure.h
Gabor Horvath f26749245b [SILGen] Fix the type of closure thunks that are passed const reference structs
This PR is another attempt at landing #76903. The changes compared to
the original PR:
* Instead of increasing the size of SILDeclRef, store the necessary type
  information in a side channel using withClosureTypeInfo.
* Rely on SGFContext to get the right ClangType
* Extend BridgingConversion with an AbstractionPattern to store the
  original clang type.
* The PR above introduced a crash during indexing system modules that
  references foreign types coming from modules imported as
  implementation only. These entities are implementation details so they
  do not need to be included during serialization. This PR adds a test
  and adds logic to exclude such clang types in the serialization
  process.

rdar://131321096&141786724
2025-09-09 12:07:52 +01:00

92 lines
2.3 KiB
C++

#ifndef __CLOSURE__
#define __CLOSURE__
struct NonTrivial {
NonTrivial() noexcept { p = new int(123); }
~NonTrivial() { delete p; }
NonTrivial(const NonTrivial &other) noexcept {
p = new int(*other.p);
}
int *p;
};
struct Trivial {
int i;
};
void cfunc(void (^ _Nonnull block)(NonTrivial)) noexcept {
block(NonTrivial());
}
void cfunc2(void (*_Nonnull fp)(NonTrivial)) noexcept { (*fp)(NonTrivial()); }
NonTrivial cfunc3(NonTrivial, int, NonTrivial);
#if __OBJC__
struct ARCStrong {
id a;
};
void cfuncARCStrong(void (*_Nonnull)(ARCStrong)) noexcept ;
#endif
void cfuncReturnNonTrivial(NonTrivial (^_Nonnull)()) noexcept;
void cfuncReturnNonTrivial2(NonTrivial (*_Nonnull)()) noexcept;
struct ARCWeak {
#if __OBJC__
__weak _Nullable id m;
#endif
};
void cfuncARCWeak(void (^ _Nonnull block)(ARCWeak)) noexcept {
block(ARCWeak());
}
void cfunc(NonTrivial) noexcept;
void cfuncARCWeak(ARCWeak) noexcept;
void (* _Nonnull getFnPtr() noexcept)(NonTrivial) noexcept;
void (* _Nonnull getFnPtr2() noexcept)(ARCWeak) noexcept;
class SharedRef {
public:
static SharedRef *_Nonnull makeSharedRef() { return new SharedRef(); }
int _refCount = 1;
private:
SharedRef() = default;
SharedRef(const SharedRef &other) = delete;
SharedRef &operator=(const SharedRef &other) = delete;
SharedRef(SharedRef &&other) = delete;
SharedRef &operator=(SharedRef &&other) = delete;
} __attribute__((swift_attr("import_reference")))
__attribute__((swift_attr("retain:retainSharedRef")))
__attribute__((swift_attr("release:releaseSharedRef")));
inline void
cppGo(void (*_Nonnull takeConstSharedRef)(const SharedRef *_Nonnull x)) {
SharedRef *ref = SharedRef::makeSharedRef();
takeConstSharedRef(ref);
}
inline void retainSharedRef(SharedRef *_Nonnull x) { x->_refCount += 1; }
inline void releaseSharedRef(SharedRef *_Nonnull x) {
x->_refCount -= 1;
if (x->_refCount == 0) {
delete x;
}
}
void cfuncConstRefNonTrivial(void (*_Nonnull)(const NonTrivial &));
void cfuncConstRefTrivial(void (*_Nonnull)(const Trivial &));
void blockConstRefNonTrivial(void (^_Nonnull)(const NonTrivial &));
void blockConstRefTrivial(void (^_Nonnull)(const Trivial &));
#if __OBJC__
void cfuncConstRefStrong(void (*_Nonnull)(const ARCStrong &));
void blockConstRefStrong(void (^_Nonnull)(const ARCStrong &));
#endif
#endif // __CLOSURE__