Skip to content

Import _CSwiftSyntax using @_implementationOnly. #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 2 additions & 26 deletions Sources/SwiftSyntax/AtomicCounter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,12 @@
//
//===----------------------------------------------------------------------===//

// This is using the C function from '_CSwiftSyntax/src/atomic-counter.c' by
// dynamically loading it using `dlsym`. The reason this mechanism is used
// instead of importing a header is so that we preserve the property that
// SwiftSyntax is a self-contained Swift module.
// (also see '_CSwiftSyntax/include/README.md')

#if os(Linux)
import Glibc
#else
import Darwin.C
#endif

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
fileprivate let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
#elseif os(Linux)
fileprivate let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0)
#endif

fileprivate typealias get_unique_counter_ty = (@convention(c) ()->UInt64)
fileprivate let get_unique_counter_str = "cswiftsyntax_get_unique_counter"
@_implementationOnly import _CSwiftSyntax

/// Provides API to get an atomically increasing counter value.
struct AtomicCounter {
/// Get an atomically increasing counter value.
static func next() -> UInt64 {
return AtomicCounter.cswiftsyntax_get_unique_counter()
return cswiftsyntax_get_unique_counter()
}

static fileprivate let cswiftsyntax_get_unique_counter: get_unique_counter_ty = { ()->get_unique_counter_ty in
let ptr = dlsym(RTLD_DEFAULT, get_unique_counter_str)!
return unsafeBitCast(ptr, to: get_unique_counter_ty.self)
}()
}
9 changes: 9 additions & 0 deletions Sources/_CSwiftSyntax/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This module is an implementation detail of SwiftSyntax and does not export
any public API. As such, any uses of it inside SwiftSyntax should import it
with the `@_implementationOnly` attribute (available since Swift 5.1).

This preserves the property that SwiftSyntax is a self-contained Swift
module, meaning that when distributing it, we do not also have to bundle
these private headers or use any dynamic symbol loading tricks that require
additional linker flags to be passed on some platforms (see
[SR-11293](https://bugs.swift.org/browse/SR-11293), for example).
10 changes: 0 additions & 10 deletions Sources/_CSwiftSyntax/include/README.md

This file was deleted.

3 changes: 3 additions & 0 deletions Sources/_CSwiftSyntax/include/atomic-counter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include <stdint.h>

uint64_t cswiftsyntax_get_unique_counter(void);
2 changes: 1 addition & 1 deletion Sources/_CSwiftSyntax/src/atomic-counter.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <stdint.h>
#include "atomic-counter.h"

uint64_t cswiftsyntax_get_unique_counter(void) {
static _Atomic uint64_t counter = 0;
Expand Down