|
52 | 52 | # define SWIFT_END_NULLABILITY_ANNOTATIONS
|
53 | 53 | #endif
|
54 | 54 |
|
| 55 | +#define SWIFT_MACRO_CONCAT(A, B) A ## B |
| 56 | +#define SWIFT_MACRO_IF_0(IF_TRUE, IF_FALSE) IF_FALSE |
| 57 | +#define SWIFT_MACRO_IF_1(IF_TRUE, IF_FALSE) IF_TRUE |
| 58 | +#define SWIFT_MACRO_IF(COND, IF_TRUE, IF_FALSE) \ |
| 59 | + SWIFT_MACRO_CONCAT(SWIFT_MACRO_IF_, COND)(IF_TRUE, IF_FALSE) |
| 60 | + |
55 | 61 | #if __has_attribute(pure)
|
56 | 62 | #define SWIFT_READONLY __attribute__((__pure__))
|
57 | 63 | #else
|
|
94 | 100 | #define SWIFT_ATTRIBUTE_UNAVAILABLE
|
95 | 101 | #endif
|
96 | 102 |
|
97 |
| -// TODO: support using shims headers in overlays by parameterizing |
98 |
| -// SWIFT_RUNTIME_EXPORT on the library it's exported from. |
99 |
| - |
100 |
| -/// Attribute used to export symbols from the runtime. |
| 103 | +// Define the appropriate attributes for sharing symbols across |
| 104 | +// image (executable / shared-library) boundaries. |
| 105 | +// |
| 106 | +// SWIFT_ATTRIBUTE_FOR_EXPORTS will be placed on declarations that |
| 107 | +// are known to be exported from the current image. Typically, they |
| 108 | +// are placed on header declarations and then inherited by the actual |
| 109 | +// definitions. |
| 110 | +// |
| 111 | +// SWIFT_ATTRIBUTE_FOR_IMPORTS will be placed on declarations that |
| 112 | +// are known to be exported from a different image. This never |
| 113 | +// includes a definition. |
| 114 | +// |
| 115 | +// Getting the right attribute on a declaratioon can be pretty awkward, |
| 116 | +// but it's necessary under the C translation model. All of this |
| 117 | +// ceremony is familiar to Windows programmers; C/C++ programmers |
| 118 | +// everywhere else usually don't bother, but since we have to get it |
| 119 | +// right for Windows, we have everything set up to get it right on |
| 120 | +// other targets as well, and doing so lets the compiler use more |
| 121 | +// efficient symbol access patterns. |
101 | 122 | #if defined(__MACH__) || defined(__wasi__)
|
102 | 123 |
|
103 |
| -# define SWIFT_EXPORT_ATTRIBUTE __attribute__((__visibility__("default"))) |
| 124 | +// On Mach-O and WebAssembly, we use non-hidden visibility. We just use |
| 125 | +// default visibility on both imports and exports, both because these |
| 126 | +// targets don't support protected visibility but because they don't |
| 127 | +// need it: symbols are not interposable outside the current image |
| 128 | +// by default. |
| 129 | +# define SWIFT_ATTRIBUTE_FOR_EXPORTS __attribute__((__visibility__("default"))) |
| 130 | +# define SWIFT_ATTRIBUTE_FOR_IMPORTS __attribute__((__visibility__("default"))) |
104 | 131 |
|
105 | 132 | #elif defined(__ELF__)
|
106 | 133 |
|
107 |
| -// We make assumptions that the runtime and standard library can refer to each |
108 |
| -// other's symbols as DSO-local, which means we can't allow the dynamic linker |
109 |
| -// to relocate these symbols. We must give them protected visibility while |
110 |
| -// building the standard library and runtime. |
111 |
| -# if defined(swiftCore_EXPORTS) |
112 |
| -# define SWIFT_EXPORT_ATTRIBUTE __attribute__((__visibility__("protected"))) |
113 |
| -# else |
114 |
| -# define SWIFT_EXPORT_ATTRIBUTE __attribute__((__visibility__("default"))) |
115 |
| -# endif |
| 134 | +// On ELF, we use non-hidden visibility. For exports, we must use |
| 135 | +// protected visibility to tell the compiler and linker that the symbols |
| 136 | +// can't be interposed outside the current image. For imports, we must |
| 137 | +// use default visibility because protected visibility guarantees that |
| 138 | +// the symbol is defined in the current library, which isn't true for |
| 139 | +// an import. |
| 140 | +// |
| 141 | +// The compiler does assume that the runtime and standard library can |
| 142 | +// refer to each other's symbols as DSO-local, so it's important that |
| 143 | +// we get this right or we can get linker errors. |
| 144 | +# define SWIFT_ATTRIBUTE_FOR_EXPORTS __attribute__((__visibility__("protected"))) |
| 145 | +# define SWIFT_ATTRIBUTE_FOR_IMPORTS __attribute__((__visibility__("default"))) |
| 146 | + |
| 147 | +#elif defined(__CYGWIN__) |
| 148 | + |
| 149 | +// For now, we ignore all this on Cygwin. |
| 150 | +# define SWIFT_ATTRIBUTE_FOR_EXPORTS |
| 151 | +# define SWIFT_ATTRIBUTE_FOR_IMPORTS |
116 | 152 |
|
117 | 153 | // FIXME: this #else should be some sort of #elif Windows
|
118 | 154 | #else // !__MACH__ && !__ELF__
|
119 | 155 |
|
120 |
| -# if defined(__CYGWIN__) |
121 |
| -# define SWIFT_EXPORT_ATTRIBUTE |
122 |
| -# else |
| 156 | +// On PE/COFF, we use dllimport and dllexport. |
| 157 | +# define SWIFT_ATTRIBUTE_FOR_EXPORTS __declspec(dllexport) |
| 158 | +# define SWIFT_ATTRIBUTE_FOR_IMPORTS __declspec(dllimport) |
123 | 159 |
|
124 |
| -# if defined(swiftCore_EXPORTS) |
125 |
| -# define SWIFT_EXPORT_ATTRIBUTE __declspec(dllexport) |
126 |
| -# else |
127 |
| -# define SWIFT_EXPORT_ATTRIBUTE __declspec(dllimport) |
128 |
| -# endif |
129 |
| - |
130 |
| -# endif |
| 160 | +#endif |
131 | 161 |
|
| 162 | +// CMake conventionally passes -DlibraryName_EXPORTS when building |
| 163 | +// code that goes into libraryName. This isn't the best macro name, |
| 164 | +// but it's conventional. We do have to pass it explicitly in a few |
| 165 | +// places in the build system for a variety of reasons. |
| 166 | +// |
| 167 | +// Unfortunately, defined(D) is a special function you can use in |
| 168 | +// preprocessor conditions, not a macro you can use anywhere, so we |
| 169 | +// need to manually check for all the libraries we know about so that |
| 170 | +// we can use them in our condition below.s |
| 171 | +#if defined(swiftCore_EXPORTS) |
| 172 | +#define SWIFT_IMAGE_EXPORTS_swiftCore 1 |
| 173 | +#else |
| 174 | +#define SWIFT_IMAGE_EXPORTS_swiftCore 0 |
132 | 175 | #endif
|
133 | 176 |
|
| 177 | +#define SWIFT_EXPORT_FROM_ATTRIBUTE(LIBRARY) \ |
| 178 | + SWIFT_MACRO_IF(SWIFT_IMAGE_EXPORTS_##LIBRARY, \ |
| 179 | + SWIFT_ATTRIBUTE_FOR_EXPORTS, \ |
| 180 | + SWIFT_ATTRIBUTE_FOR_IMPORTS) |
| 181 | + |
| 182 | +// SWIFT_EXPORT_FROM(LIBRARY) declares something to be a C-linkage |
| 183 | +// entity exported by the given library. |
| 184 | +// |
| 185 | +// SWIFT_RUNTIME_EXPORT is just SWIFT_EXPORT_FROM(swiftCore). |
| 186 | +// |
| 187 | +// TODO: use this in shims headers in overlays. |
134 | 188 | #if defined(__cplusplus)
|
135 |
| -#define SWIFT_RUNTIME_EXPORT extern "C" SWIFT_EXPORT_ATTRIBUTE |
| 189 | +#define SWIFT_EXPORT_FROM(LIBRARY) extern "C" SWIFT_EXPORT_FROM_ATTRIBUTE(LIBRARY) |
136 | 190 | #else
|
137 |
| -#define SWIFT_RUNTIME_EXPORT SWIFT_EXPORT_ATTRIBUTE |
| 191 | +#define SWIFT_EXPORT_FROM(LIBRARY) SWIFT_EXPORT_FROM_ATTRIBUTE(LIBRARY) |
138 | 192 | #endif
|
| 193 | +#define SWIFT_RUNTIME_EXPORT SWIFT_EXPORT_FROM(swiftCore) |
139 | 194 |
|
140 | 195 | #if __cplusplus > 201402l && __has_cpp_attribute(fallthrough)
|
141 | 196 | #define SWIFT_FALLTHROUGH [[fallthrough]]
|
|
0 commit comments