Documentation Language: Swift

Framework

libgir2swift

The core library that parses GObject Introspection (GIR) XML and generates Swift wrapper code.

Overview

libgir2swift implements the full GIR-to-Swift transformation pipeline. It is used by the gir2swift command-line tool and can be embedded in other tools that need to process .gir files programmatically.

The library is organised into three layers:

Parsing a GIR file

The central class is GIR. Construct it by passing a memory buffer that contains the raw bytes of a .gir file:

import libgir2swift

let data = try Data(contentsOf: girURL, options: .alwaysMapped)
let gir: GIR? = data.withUnsafeBytes { bytes in
    GIR(buffer: bytes.bindMemory(to: CChar.self), quiet: false)
}

After construction, gir exposes typed collections for every GIR element kind: interfaces, records, unions, classes, functions, callbacks, and more.

GIR element hierarchy

All GIR elements derive from GIR.Thing. The inheritance hierarchy mirrors the GIR XML schema:

Thing
└── Datatype
    ├── CType
    │   ├── Alias
    │   ├── Constant
    │   ├── Argument
    │   ├── Property
    │   │   └── Field
    │   └── Record
    │       ├── Class
    │       │   └── Interface
    │       └── Union
    ├── Enumeration
    │   └── Bitfield
    └── Method
        ├── Function
        │   ├── Signal
        │   └── Callback

Type system

GIRType represents a resolved GIR type together with its Swift and C names, namespace, parent type, and the TypeConversion operations available to it. TypeReference tracks pointer levels and const qualifiers for a type used in a specific position (e.g., a function parameter or return value).

Topics

Command and configuration

S
Gir2Swift

Structure representing the gir2swift executable, including command line arguments

GIR document

C
GIR

Representation of a GIR file

GIR element models

C
GIR.Thing

GIR named thing class

C
GIR.Datatype

GIR type class

C
GIR.CType

a type with an underlying C type entry

C
GIR.Alias

a type alias is just a type with an underlying C type

C
GIR.Argument

data type representing a function/method argument or return type

C
GIR.Bitfield

a bitfield is defined akin to an enumeration

C
GIR.Callback

a callback is the same as a function, except that the type definition is a @convention(c) callback definition

C
GIR.Class

a class data type record

C
GIR.Constant

an entry for a constant

C
GIR.Enumeration

an enumeration entry

C
GIR.Field

a field is a Property

C
GIR.Function

a function is the same as a method

C
GIR.Interface

an inteface is similar to a class, but can be part of a more complex type graph

C
GIR.Method

data type representing a function/method

C
GIR.Property

a property is a C type

C
GIR.Record

a data type record to create a protocol/struct/class for

C
GIR.Signal

a signal is equivalent to a function

C
GIR.Union

a union data type record

Type system

C
GIRType

Representation of a fundamental type, its relationship to other types, and casting operations

C
GIRStringType

Representation of a string type, its relationship to other types, and casting operations

C
GIRRawPointerType

Representation of a raw pointer type, its relationship to other types, and casting operations

C
GIRRecordType

Representation of a record type (struct or class), its relationship to other types, and casting operations

C
GIRGenericType

Representation of a generic type (struct or class), its relationship to other types, and casting operations

C
GIROpaquePointerType

Representation of a opaque pointer type, its relationship to other types, and casting operations

C
TypeConversion

Type conversion operation. This root class is used for aliases/equal type conversions, i.e., casts are no-ops.

C
SubClassConversion

Parent/Child class conversion operation

C
OptionalSubClassConversion

Parent/Child class conversion operation with optional upcast

C
CustomConversion

Custom type conversion operation

C
NestedConversion

Nested type conversion operation

C
EnumTypeConversion

Enum type conversion operation.

C
BitfieldTypeConversion

Bit field (OptionSet) type conversion operation.

C
RawPointerConversion

Raw pointer conversion

S
TypeReference

Reference to a GIR type.

Structures

S
GetterSetterPair

a pair of getters and setters (both cannot be nil at the same time)

Functions

F
bitfieldTypeHead(_:enumRawType:indentation:)

Swift code type definition of a bitfield

F
bitfieldValueCode(_:_:)

Swift code representation of a bit field value

F
callSetter(_:_:ptr:)

Swift code for calling the underlying setter function and assigning the raw return value

F
callbackDecl(for:)

Swift code for a @convention(c) callback type declaration

F
callbackParameterCode(for:)

Swift code for a @convention(c) callback parameter

F
commentCode(_:indentation:)

SwiftDoc representation of comments

F
constantSwiftCode(indentedBy:scopePrefix:)

Swift code representation of a constant

F
constructorParam(_:prefix:)

Swift code for the parameters of a constructor

F
constructorPrefix(_:)

Swift code for constructor first argument prefix extracted from a method name

F
convertSetterArgumentToSwiftFor(_:ptr:)

Swift code for passing a setter to a method of a record / class

F
deprecatedCode(_:indentation:)

Swift representation of deprecation

F
fieldCode(_:record:avoiding:publicDesignation:ptr:)

Swift code for field properties

F
forceUnwrappedDecl(for:)

Swift code for a @convention(c) callback type declaration

F
functionCallCode(_:_:constructedRecord:ptr:rvVar:doThrow:isConstructor:useStruct:)

Swift code for calling the underlying function and assigning the raw return value

F
functionCode(_:indentation:initialIndentation:)

Default implementation for functions

F
getterSetterPairs(for:)

return setter/getter pairs from a list of methods

F
gtkDoc2SwiftDoc(for:_:linePrefix:)

Convert the given String to SwiftDoc

F
namespacedAliasCode(for:prefix:indentation:)

Namespaced typealias code representation for a given data type

F
namespacedAliasSwiftCode(prefixedWith:indentation:)

Swift code representation of a type alias

F
nullableRefParameterCode(for:)

Swift code for auto-prefixed arguments. This version will use Ref (struct) types instead of templats for nullable reference arguments with a default value of nil

F
prefixedNullableRefParameterCode(for:prefix:)

Swift code for method parameters

F
prefixedTemplatedParameterCode(for:prefix:)

Swift code for method parameters

F
recordClassCode(_:parent:indentation:ptr:)

Swift struct representation of a record/class as a wrapper of a pointer

F
recordProtocolCode(_:parent:indentation:ptr:)

Swift protocol representation of a record/class as a wrapper of a pointer

F
recordProtocolExtensionCode(_:_:indentation:ptr:)

Default implementation for record methods as protocol extension.

F
recordStructCode(_:indentation:ptr:)

Swift struct representation of a record/class as a wrapper of a pointer

F
returnCode(_:_:ptr:hasParent:useIdiomaticSwift:noCast:)

Return code for functions/methods/convenience constructors

F
returnCode(for:)

Swift code for auto-prefixed return values

F
returnCode(for:prefix:)

Swift code for method return values

F
returnDeclarationCode(_:useStruct:)

Return code declaration for functions/methods/convenience constructors

F
returnTypeCode(for:_:useIdiomaticSwift:useStruct:)

Return the return type of a method,

F
signalNameCode(indentation:convertName:)

Swift code for signal names without prefixes

F
signalNameCode(indentation:prefixes:convertName:)

Swift code for signal names with prefixes

F
subRecordProperty(_:ptr:_:indentation:publicDesignation:)

Property definition for sub-records

F
subTypeAlias(_:_:publicDesignation:)

Type alias for sub-records

F
swiftCallbackAliasCode(callback:)

Swift code representation of a callback as a type alias

F
swiftCode(_:)

Swift code representation of a free standing function

F
swiftCode(_:)

Swift code representation of an enum

F
swiftCode(_:)

Swift code representation of an enum

F
swiftCode(_:)

Swift code representation of a record

F
swiftCode(_:_:indentation:)

Swift code representation with code following the comments

F
swiftCode(alias:)

Swift code representation of a type alias

F
swiftCode(constant:)

Swift code representation of a constant

F
swiftUnionsConversion(_:)

Return a unions-to-swift conversion closure for the array of functions passed in

F
templatedParameterCode(for:)

Swift code for auto-prefixed arguments This version will use template types where possible, ignoring default values for those templates

F
toSwift(_:ptr:)

Swift code for passing an argument to a free standing function

F
typeAlias(_:)

Swift code type alias representation of an enum

F
typeAliasCode(for:prefix:)

Swift typealias code representation for a given data type

F
typeAliasSwiftCode(prefixedWith:)

Swift code representation of a type alias

F
typedCollection(for:containedTypes:unwrappedName:typeRef:)

Return a typed collection reference for the given, prefixed type name.

F
valueCode(_:)

Swift code representation of an enum value

Type Aliases

T
EmptyConversion

An empty conversion has no explicit casting