Skip to content

[Swift] Fixes inlining & module importing #8637

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
Jul 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// swiftlint:disable all
// swiftformat:disable all

#if canImport(Common)
import Common
#endif

import FlatBuffers

public struct models_HelloReply: FlatBufferObject, Verifiable {
Expand Down
4 changes: 4 additions & 0 deletions samples/monster_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// swiftlint:disable all
// swiftformat:disable all

#if canImport(Common)
import Common
#endif

import FlatBuffers

public enum MyGame_Sample_Color: Int8, Enum, Verifiable {
Expand Down
10 changes: 6 additions & 4 deletions src/idl_gen_swift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ class SwiftGenerator : public BaseGenerator {
code_ += "// swiftlint:disable all";
code_ += "// swiftformat:disable all\n";
if (parser_.opts.include_dependence_headers || parser_.opts.generate_all) {
if (parser_.opts.swift_implementation_only)
code_ += "@_implementationOnly \\";

code_ += "import FlatBuffers\n";
code_.SetValue("IMPLEMENTONLY", parser_.opts.swift_implementation_only ? "@_implementationOnly " : "");
code_ += "#if canImport(Common)";
code_ += "{{IMPLEMENTONLY}}import Common";
code_ += "#endif";
code_ += "";
code_ += "{{IMPLEMENTONLY}}import FlatBuffers\n";
}

// Generate code for all the enum declarations.
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#if canImport(Common)
@_exported import Common
import Common
#endif
import Foundation

Expand Down
3 changes: 3 additions & 0 deletions swift/Sources/FlatBuffers/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down
3 changes: 3 additions & 0 deletions swift/Sources/FlatBuffers/FlatBufferBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down
3 changes: 3 additions & 0 deletions swift/Sources/FlatBuffers/Mutable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down
3 changes: 3 additions & 0 deletions swift/Sources/FlatBuffers/Struct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down
3 changes: 3 additions & 0 deletions swift/Sources/FlatBuffers/Table.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down
4 changes: 4 additions & 0 deletions swift/Sources/FlatBuffers/Verifiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/

#if canImport(Common)
import Common
#endif

import Foundation

/// Verifiable is a protocol all swift flatbuffers object should conform to,
Expand Down
4 changes: 4 additions & 0 deletions swift/Sources/FlatBuffers/_InternalByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/

#if canImport(Common)
import Common
#endif

import Foundation

/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlexBuffers/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#if canImport(Common)
@_exported import Common
import Common
#endif
import Foundation

Expand Down
3 changes: 3 additions & 0 deletions swift/Sources/FlexBuffers/Utils/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down
3 changes: 3 additions & 0 deletions swift/Sources/FlexBuffers/Utils/functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down
51 changes: 23 additions & 28 deletions swift/Sources/FlexBuffers/Writer/FlexBuffersWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if canImport(Common)
import Common
#endif

import Foundation

Expand Down Expand Up @@ -80,7 +83,6 @@ public struct FlexBuffersWriter {
}

// MARK: - Storing root
@inline(__always)
public mutating func finish() {
assert(stack.count == 1)

Expand Down Expand Up @@ -115,7 +117,6 @@ public struct FlexBuffersWriter {
}

@discardableResult
@inline(__always)
public mutating func endVector(
start: Int,
typed: Bool = false,
Expand Down Expand Up @@ -181,7 +182,6 @@ public struct FlexBuffersWriter {
}

@discardableResult
@inline(__always)
public mutating func endMap(start: Int) -> UInt64 {
let len = sortMapByKeys(start: start)

Expand Down Expand Up @@ -507,8 +507,8 @@ public struct FlexBuffersWriter {

// MARK: Writing to buffer

@inline(__always)
private mutating func write(value: Value, byteWidth: Int) {
@usableFromInline
mutating func write(value: Value, byteWidth: Int) {
switch value.type {
case .null, .int: write(value: value.i, byteWidth: byteWidth)
case .bool, .uint: write(value: value.u, byteWidth: byteWidth)
Expand All @@ -518,8 +518,8 @@ public struct FlexBuffersWriter {
}
}

@inline(__always)
private mutating func pushIndirect<T>(
@usableFromInline
mutating func pushIndirect<T>(
value: T,
type: FlexBufferType,
bitWidth: BitWidth)
Expand All @@ -542,8 +542,8 @@ public struct FlexBuffersWriter {
/// - Parameter str: String that will be added to the buffer
/// - Parameter len: length of the string
@discardableResult
@inline(__always)
private mutating func write(str: borrowing String, len: Int) -> UInt {
@usableFromInline
mutating func write(str: borrowing String, len: Int) -> UInt {
let resetTo = writerIndex
var sloc = str.withCString {
storeBlob(pointer: $0, len: len, trailing: 1, type: .string)
Expand Down Expand Up @@ -573,8 +573,8 @@ public struct FlexBuffersWriter {
}

@discardableResult
@inline(__always)
private mutating func add(key: borrowing String, len: Int) -> UInt {
@usableFromInline
mutating func add(key: borrowing String, len: Int) -> UInt {
_bb.ensureSpace(size: len)

var sloc: UInt = numericCast(writerIndex)
Expand All @@ -596,8 +596,8 @@ public struct FlexBuffersWriter {
}

// MARK: - Storing Blobs
@inline(__always)
private mutating func storeBlob<T>(
@usableFromInline
mutating func storeBlob<T>(
_ bytes: T,
len: Int,
type: FlexBufferType) -> UInt where T: ContiguousBytes
Expand All @@ -609,7 +609,6 @@ public struct FlexBuffersWriter {

@discardableResult
@usableFromInline
@inline(__always)
mutating func storeBlob(
pointer: borrowing UnsafeRawPointer,
len: Int,
Expand All @@ -636,8 +635,8 @@ public struct FlexBuffersWriter {

// MARK: Write Vectors
@discardableResult
@inline(__always)
private mutating func create<T>(vector: [T], fixed: Bool) -> Int
@usableFromInline
mutating func create<T>(vector: [T], fixed: Bool) -> Int
where T: Scalar
{
let length: UInt64 = numericCast(vector.count)
Expand Down Expand Up @@ -668,8 +667,8 @@ public struct FlexBuffersWriter {
return vloc
}

@inline(__always)
private mutating func createVector(
@usableFromInline
mutating func createVector(
start: Int,
count: Int,
step: Int,
Expand Down Expand Up @@ -800,8 +799,8 @@ public struct FlexBuffersWriter {
return bytes
}

@inline(__always)
private mutating func sortMapByKeys(start: Int) -> Int {
@usableFromInline
mutating func sortMapByKeys(start: Int) -> Int {
let len = mapElementCount(start: start)
for index in stride(from: start, to: stack.count, by: 2) {
assert(stack[index].type == .key)
Expand Down Expand Up @@ -838,19 +837,17 @@ public struct FlexBuffersWriter {
// MARK: - Vectors helper functions
extension FlexBuffersWriter {
@discardableResult
@inline(__always)
public mutating func vector(
key: String,
_ closure: @escaping FlexBuffersWriterBuilder) -> UInt64
_ closure: FlexBuffersWriterBuilder) -> UInt64
{
let start = startVector(key: key)
closure(&self)
return endVector(start: start)
}

@discardableResult
@inline(__always)
public mutating func vector(_ closure: @escaping FlexBuffersWriterBuilder)
public mutating func vector(_ closure: FlexBuffersWriterBuilder)
-> UInt64
{
let start = startVector()
Expand All @@ -862,19 +859,17 @@ extension FlexBuffersWriter {
// MARK: - Maps helper functions
extension FlexBuffersWriter {
@discardableResult
@inline(__always)
public mutating func map(
key: String,
_ closure: @escaping FlexBuffersWriterBuilder) -> UInt64
_ closure: FlexBuffersWriterBuilder) -> UInt64
{
let start = startMap(key: key)
closure(&self)
return endMap(start: start)
}

@discardableResult
@inline(__always)
public mutating func map(_ closure: @escaping FlexBuffersWriterBuilder)
public mutating func map(_ closure: FlexBuffersWriterBuilder)
-> UInt64
{
let start = startMap()
Expand Down
6 changes: 6 additions & 0 deletions swift/Sources/FlexBuffers/_InternalByteBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/

#if canImport(Common)
import Common
#endif

import Foundation

/// `ByteBuffer` is the interface that stores the data for a `Flatbuffers` object
Expand Down Expand Up @@ -152,6 +156,7 @@ struct _InternalByteBuffer {
ensureSpace(size: writerIndex)
}

@inline(__always)
mutating func writeBytes(_ ptr: UnsafeRawPointer, len: Int) {
memcpy(
_storage.memory.advanced(by: writerIndex),
Expand All @@ -160,6 +165,7 @@ struct _InternalByteBuffer {
writerIndex = writerIndex &+ len
}

@inline(__always)
mutating func write<T>(_ v: T, len: Int) {
withUnsafePointer(to: v) {
memcpy(
Expand Down
4 changes: 4 additions & 0 deletions tests/swift/Tests/Flatbuffers/MutatingBool_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// swiftlint:disable all
// swiftformat:disable all

#if canImport(Common)
import Common
#endif

import FlatBuffers

public struct Property: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject {
Expand Down
4 changes: 4 additions & 0 deletions tests/swift/Tests/Flatbuffers/monster_test_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// swiftlint:disable all
// swiftformat:disable all

#if canImport(Common)
import Common
#endif

import FlatBuffers

/// Composite components of Monster color.
Expand Down
4 changes: 4 additions & 0 deletions tests/swift/Tests/Flatbuffers/more_defaults_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// swiftlint:disable all
// swiftformat:disable all

#if canImport(Common)
import Common
#endif

import FlatBuffers

public enum ABC: Int32, Enum, Verifiable {
Expand Down
4 changes: 4 additions & 0 deletions tests/swift/Tests/Flatbuffers/nan_inf_test_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// swiftlint:disable all
// swiftformat:disable all

#if canImport(Common)
import Common
#endif

import FlatBuffers

public struct Swift_Tests_NanInfTable: FlatBufferObject, Verifiable {
Expand Down
Loading
Loading