mirror of
https://github.com/apple/swift.git
synced 2026-03-04 18:24:35 +01:00
Release/retain functions for C++ foreign reference types might return a reference count as an integer value. Swift previously emitted an error for such functions, saying that the retain/release functions need to return void or a reference to the value. rdar://157853183
114 lines
3.1 KiB
C++
114 lines
3.1 KiB
C++
#ifndef TEST_INTEROP_CXX_FOREIGN_REFERENCE_INPUTS_REFERENCE_COUNTED_H
|
|
#define TEST_INTEROP_CXX_FOREIGN_REFERENCE_INPUTS_REFERENCE_COUNTED_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifdef __cplusplus
|
|
#include <new>
|
|
#endif
|
|
|
|
#include "visibility.h"
|
|
|
|
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
|
|
|
|
static int finalLocalRefCount = 100;
|
|
|
|
#ifdef __cplusplus
|
|
|
|
namespace NS {
|
|
|
|
struct __attribute__((swift_attr("import_as_ref")))
|
|
__attribute__((swift_attr("retain:LCRetain")))
|
|
__attribute__((swift_attr("release:LCRelease"))) LocalCount final {
|
|
int value = 0;
|
|
|
|
static LocalCount *create() {
|
|
return new (malloc(sizeof(LocalCount))) LocalCount();
|
|
}
|
|
|
|
int returns42() { return 42; }
|
|
int constMethod() const { return 42; }
|
|
};
|
|
|
|
}
|
|
|
|
inline void LCRetain(NS::LocalCount *x) {
|
|
x->value++;
|
|
finalLocalRefCount = x->value;
|
|
}
|
|
inline void LCRelease(NS::LocalCount *x) {
|
|
x->value--;
|
|
finalLocalRefCount = x->value;
|
|
}
|
|
|
|
static int globalCount = 0;
|
|
|
|
struct __attribute__((swift_attr("import_as_ref")))
|
|
__attribute__((swift_attr("retain:GCRetain")))
|
|
__attribute__((swift_attr("release:GCRelease"))) GlobalCount {
|
|
static GlobalCount *create() {
|
|
return new (malloc(sizeof(GlobalCount))) GlobalCount();
|
|
}
|
|
};
|
|
|
|
inline void GCRetain(GlobalCount *x) { globalCount++; }
|
|
inline void GCRelease(GlobalCount *x) { globalCount--; }
|
|
|
|
struct __attribute__((swift_attr("import_as_ref")))
|
|
__attribute__((swift_attr("retain:GCRetainNullableInit")))
|
|
__attribute__((swift_attr("release:GCReleaseNullableInit")))
|
|
GlobalCountNullableInit {
|
|
static GlobalCountNullableInit *_Nullable create(bool wantNullptr) {
|
|
if (wantNullptr)
|
|
return nullptr;
|
|
return new (malloc(sizeof(GlobalCountNullableInit)))
|
|
GlobalCountNullableInit();
|
|
}
|
|
};
|
|
|
|
inline void GCRetainNullableInit(GlobalCountNullableInit *x) { globalCount++; }
|
|
inline void GCReleaseNullableInit(GlobalCountNullableInit *x) { globalCount--; }
|
|
|
|
struct __attribute__((swift_attr("import_as_ref")))
|
|
__attribute__((swift_attr("retain:RCRetain")))
|
|
__attribute__((swift_attr("release:RCRelease"))) HasOpsReturningRefCount final {
|
|
int refCount = 0;
|
|
|
|
static HasOpsReturningRefCount *create() {
|
|
return new (malloc(sizeof(HasOpsReturningRefCount)))
|
|
HasOpsReturningRefCount();
|
|
}
|
|
};
|
|
|
|
inline unsigned RCRetain(HasOpsReturningRefCount *x) { return ++x->refCount; }
|
|
inline unsigned RCRelease(HasOpsReturningRefCount *x) { return --x->refCount; }
|
|
|
|
#endif
|
|
|
|
typedef struct __attribute__((swift_attr("import_as_ref")))
|
|
__attribute__((swift_attr("retain:INRetain")))
|
|
__attribute__((swift_attr("release:INRelease"))) IncompleteImpl *Incomplete;
|
|
|
|
typedef struct OpaqueRefImpl *OpaqueRef;
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
Incomplete Incomplete_create(double weight) __attribute__((swift_attr("returns_retained"))) __attribute__((swift_name("IncompleteImpl.init(weight:)")));
|
|
void INRetain(Incomplete i);
|
|
void INRelease(Incomplete i);
|
|
double Incomplete_getWeight(Incomplete i) __attribute__((swift_name("getter:IncompleteImpl.weight(self:)")));
|
|
|
|
OpaqueRef Opaque_create(void);
|
|
void OPRetain(OpaqueRef i);
|
|
void OPRelease(OpaqueRef i);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
SWIFT_END_NULLABILITY_ANNOTATIONS
|
|
|
|
#endif // TEST_INTEROP_CXX_FOREIGN_REFERENCE_INPUTS_REFERENCE_COUNTED_H
|