Skip to content

Commit 9dc4051

Browse files
committed
Runtime: split out extern "C" marker from SWIFT_RUNTIME_EXPORT
Although the runtime exported symbols must be exported with "C" linkage and decoration, we split out the externalisation in the C++ mode. This is required to ensure that we are able to attribute the functions without following the more stringent rules that clang now enforces which place the attributes following the identifier to which they appertain, resulting in the attributes interleaving within the signature. The resulting spelling is more difficult to process for humans as well as more difficult to identify as function declarations making code perusal a challenge, particularly for the uninitiated.
1 parent b0fd4c3 commit 9dc4051

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

include/swift/Runtime/Heap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "swift/shims/Visibility.h"
2626

2727
namespace swift {
28+
SWIFT_BEGIN_DECLS
2829
// Allocate plain old memory. This is the generalized entry point
2930
// Never returns nil. The returned memory is uninitialized.
3031
//
@@ -37,6 +38,8 @@ void *swift_slowAlloc(size_t bytes, size_t alignMask);
3738
SWIFT_RUNTIME_EXPORT
3839
void swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask);
3940

41+
SWIFT_END_DECLS
42+
4043
/// Allocate and construct an instance of type \c T.
4144
///
4245
/// \param args The arguments to pass to the constructor for \c T.

include/swift/Runtime/HeapObject.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ using HeapMetadata = TargetHeapMetadata<InProcess>;
4242

4343
struct OpaqueValue;
4444

45+
SWIFT_BEGIN_DECLS
46+
4547
/// Allocates a new heap object. The returned memory is
4648
/// uninitialized outside of the heap-object header. The object
4749
/// has an initial retain count of 1, and its metadata is set to
@@ -92,11 +94,15 @@ HeapObject *swift_initStaticObject(HeapMetadata const *metadata,
9294
SWIFT_RUNTIME_EXPORT
9395
void swift_verifyEndOfLifetime(HeapObject *object);
9496

97+
SWIFT_END_DECLS
98+
9599
struct BoxPair {
96100
HeapObject *object;
97101
OpaqueValue *buffer;
98102
};
99103

104+
SWIFT_BEGIN_DECLS
105+
100106
/// Allocates a heap object that can contain a value of the given type.
101107
/// Returns a Box structure containing a HeapObject* pointer to the
102108
/// allocated object, and a pointer to the value inside the heap object.
@@ -344,6 +350,8 @@ void swift_deallocBox(HeapObject *object);
344350
SWIFT_RUNTIME_EXPORT
345351
OpaqueValue *swift_projectBox(HeapObject *object);
346352

353+
SWIFT_END_DECLS
354+
347355
/// RAII object that wraps a Swift heap object and releases it upon
348356
/// destruction.
349357
class SwiftRAII {
@@ -395,6 +403,8 @@ struct UnownedReference {
395403
HeapObject *Value;
396404
};
397405

406+
SWIFT_BEGIN_DECLS
407+
398408
/// Increment the unowned retain count.
399409
SWIFT_RUNTIME_EXPORT
400410
HeapObject *swift_unownedRetain(HeapObject *value);
@@ -453,6 +463,8 @@ void swift_nonatomic_unownedRetainStrongAndRelease(HeapObject *value);
453463
SWIFT_RUNTIME_EXPORT
454464
void swift_unownedCheck(HeapObject *value);
455465

466+
SWIFT_END_DECLS
467+
456468
static inline void swift_unownedInit(UnownedReference *ref, HeapObject *value) {
457469
ref->Value = value;
458470
swift_unownedRetain(value);
@@ -529,6 +541,8 @@ static inline bool swift_unownedIsEqual(UnownedReference *ref,
529541
// Defined in Runtime/WeakReference.h
530542
class WeakReference;
531543

544+
SWIFT_BEGIN_DECLS
545+
532546
/// Initialize a weak reference.
533547
///
534548
/// \param ref - never null
@@ -1073,6 +1087,8 @@ static inline bool swift_unknownObjectUnownedIsEqual(UnownedReference *ref,
10731087

10741088
#endif // SWIFT_OBJC_INTEROP
10751089

1090+
SWIFT_END_DECLS
1091+
10761092
struct TypeNamePair {
10771093
const char *data;
10781094
uintptr_t length;

include/swift/Runtime/Metadata.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,17 @@ struct YieldOnceCoroutine<ResultTy(ArgTys...)> {
101101

102102
#endif
103103

104+
SWIFT_BEGIN_DECLS
105+
104106
/// A standard routine, suitable for placement in the value witness
105107
/// table, for copying an opaque POD object.
106108
SWIFT_RUNTIME_EXPORT
107109
OpaqueValue *swift_copyPOD(OpaqueValue *dest,
108110
OpaqueValue *src,
109111
const Metadata *self);
110112

113+
SWIFT_END_DECLS
114+
111115
template <>
112116
inline void ValueWitnessTable::publishLayout(const TypeLayout &layout) {
113117
size = layout.size;
@@ -131,6 +135,8 @@ template <> inline bool ValueWitnessTable::checkIsComplete() const {
131135

132136
// Standard value-witness tables.
133137

138+
SWIFT_BEGIN_DECLS
139+
134140
#define BUILTIN_TYPE(Symbol, _) \
135141
SWIFT_RUNTIME_EXPORT const ValueWitnessTable VALUE_WITNESS_SYM(Symbol);
136142
#define BUILTIN_POINTER_TYPE(Symbol, _) \
@@ -166,6 +172,8 @@ const ValueWitnessTable VALUE_WITNESS_SYM(EMPTY_TUPLE_MANGLING); // ()
166172
SWIFT_RUNTIME_EXPORT
167173
const ValueWitnessTable METATYPE_VALUE_WITNESS_SYM(Bo); // Builtin.NativeObject.Type
168174

175+
SWIFT_END_DECLS
176+
169177
/// Return the value witnesses for unmanaged pointers.
170178
static inline const ValueWitnessTable &getUnmanagedPointerValueWitnesses() {
171179
#if __POINTER_WIDTH__ == 64
@@ -184,6 +192,8 @@ getUnmanagedPointerPointerValueWitnesses() {
184192

185193
using OpaqueMetadata = TargetOpaqueMetadata<InProcess>;
186194

195+
SWIFT_BEGIN_DECLS
196+
187197
// Standard POD opaque metadata.
188198
// The "Int" metadata are used for arbitrary POD data with the
189199
// matching characteristics.
@@ -209,11 +219,15 @@ SWIFT_RUNTIME_EXPORT
209219
const
210220
FullMetadata<ExistentialTypeMetadata> METADATA_SYM(ANYOBJECT_MANGLING);
211221

222+
SWIFT_END_DECLS
223+
212224

213225
/// True if two context descriptors in the currently running program describe
214226
/// the same context.
215227
bool equalContexts(const ContextDescriptor *a, const ContextDescriptor *b);
216228

229+
SWIFT_BEGIN_DECLS
230+
217231
/// Determines whether two type context descriptors describe the same type
218232
/// context.
219233
///
@@ -228,11 +242,15 @@ SWIFT_CC(swift)
228242
bool swift_compareTypeContextDescriptors(const TypeContextDescriptor *lhs,
229243
const TypeContextDescriptor *rhs);
230244

245+
SWIFT_END_DECLS
246+
231247
/// Compute the bounds of class metadata with a resilient superclass.
232248
ClassMetadataBounds getResilientMetadataBounds(
233249
const ClassDescriptor *descriptor);
234250
int32_t getResilientImmediateMembersOffset(const ClassDescriptor *descriptor);
235251

252+
SWIFT_BEGIN_DECLS
253+
236254
/// Fetch a uniqued metadata object for a nominal type which requires
237255
/// singleton metadata initialization.
238256
SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
@@ -555,8 +573,12 @@ size_t swift_getTupleTypeLayout2(TypeLayout *tupleLayout,
555573
const TypeLayout *elt0,
556574
const TypeLayout *elt1);
557575

576+
SWIFT_END_DECLS
577+
558578
struct OffsetPair { size_t First; size_t Second; };
559579

580+
SWIFT_BEGIN_DECLS
581+
560582
/// Perform layout as if for a three-element tuple whose elements have
561583
/// the given layouts.
562584
///
@@ -723,6 +745,8 @@ OpaqueValue *swift_assignExistentialWithCopy(OpaqueValue *dest,
723745
const OpaqueValue *src,
724746
const Metadata *type);
725747

748+
SWIFT_END_DECLS
749+
726750
/// Perform a copy-assignment from one existential container to another.
727751
/// Both containers must be of the same existential type representable with no
728752
/// witness tables.
@@ -828,6 +852,8 @@ inline constexpr unsigned swift_getFunctionPointerExtraInhabitantCount() {
828852
std::string nameForMetadata(const Metadata *type,
829853
bool qualified = true);
830854

855+
SWIFT_BEGIN_DECLS
856+
831857
/// Register a block of protocol records for dynamic lookup.
832858
SWIFT_RUNTIME_EXPORT
833859
void swift_registerProtocols(const ProtocolRecord *begin,
@@ -843,6 +869,8 @@ SWIFT_RUNTIME_EXPORT
843869
void swift_registerTypeMetadataRecords(const TypeMetadataRecord *begin,
844870
const TypeMetadataRecord *end);
845871

872+
SWIFT_END_DECLS
873+
846874
/// Return the superclass, if any. The result is nullptr for root
847875
/// classes and class protocol types.
848876
SWIFT_CC(swift)
@@ -862,6 +890,8 @@ void verifyMangledNameRoundtrip(const Metadata *metadata);
862890
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_API
863891
const TypeContextDescriptor *swift_getTypeContextDescriptor(const Metadata *type);
864892

893+
SWIFT_BEGIN_DECLS
894+
865895
// Defined in KeyPath.swift in the standard library.
866896
SWIFT_RUNTIME_EXPORT
867897
const HeapObject *swift_getKeyPath(const void *pattern, const void *arguments);
@@ -900,6 +930,8 @@ void swift_enableDynamicReplacementScope(const DynamicReplacementScope *scope);
900930
SWIFT_RUNTIME_EXPORT
901931
void swift_disableDynamicReplacementScope(const DynamicReplacementScope *scope);
902932

933+
SWIFT_END_DECLS
934+
903935
#pragma clang diagnostic pop
904936

905937
} // end namespace swift

stdlib/public/SwiftShims/swift/shims/Visibility.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,18 @@
203203
SWIFT_ATTRIBUTE_FOR_EXPORTS, \
204204
SWIFT_ATTRIBUTE_FOR_IMPORTS)
205205

206-
// SWIFT_EXPORT_FROM(LIBRARY) declares something to be a C-linkage
207-
// entity exported by the given library.
208-
//
209-
// SWIFT_RUNTIME_EXPORT is just SWIFT_EXPORT_FROM(swiftCore).
210-
//
211-
// TODO: use this in shims headers in overlays.
212206
#if defined(__cplusplus)
213-
#define SWIFT_EXPORT_FROM(LIBRARY) extern "C" SWIFT_EXPORT_FROM_ATTRIBUTE(LIBRARY)
207+
#define SWIFT_BEGIN_DECLS extern "C" {
208+
#define SWIFT_END_DECLS }
214209
#else
215-
#define SWIFT_EXPORT_FROM(LIBRARY) SWIFT_EXPORT_FROM_ATTRIBUTE(LIBRARY)
210+
#define SWIFT_BEGIN_DECLS
211+
#define SWIFT_END_DECLS
216212
#endif
217-
#define SWIFT_RUNTIME_EXPORT SWIFT_EXPORT_FROM(swiftCore)
213+
214+
// SWIFT_RUNTIME_EXPORT is just SWIFT_EXPORT_FROM(swiftCore).
215+
//
216+
// TODO: use this in shims headers in overlays.
217+
#define SWIFT_RUNTIME_EXPORT SWIFT_EXPORT_FROM_ATTRIBUTE(LIBRARY)
218218

219219
#if __cplusplus > 201402l && __has_cpp_attribute(fallthrough)
220220
#define SWIFT_FALLTHROUGH [[fallthrough]]

0 commit comments

Comments
 (0)