1
0
Fork 0
gobject-generator/Sources/GObjectGeneratorCore/IRModel.swift

1133 lines
54 KiB
Swift

/// A complete GIR repository, containing one or more namespaces.
///
/// Corresponds to the root `<repository>` element in a GIR XML file. A single
/// `.gir` file produces one `Repository` holding all namespaces defined within it.
public struct Repository {
/// The namespaces contained in this repository.
public var namespaces: [Namespace]
/// The C header include path from `<c:include>` (e.g. `"gtk/gtk.h"`).
public var cHeaderPath: String
/// Link names for GIR dependencies derived from `<include>` elements.
/// Derived at parse time from the library name and version (e.g. `"Gdk-4.0"` becomes `"gdk-4"`).
public var includedLibraryLinks: [String]
/// Package name from `<package>` element (e.g. `"gtk4"`).
public var packageName: String
/// Package entries from `<include>` elements for Swift dependency resolution.
/// Each entry records the included GIR namespace name and version (e.g. `"Gdk"`, `"4.0"`).
public var includedPackages: [IncludeEntry]
/// Creates a new repository.
/// - Parameter namespaces: The namespaces contained in this repository.
public init(namespaces: [Namespace] = [], cHeaderPath: String = "",
includedLibraryLinks: [String] = [], packageName: String = "",
includedPackages: [IncludeEntry] = []) {
self.namespaces = namespaces
self.cHeaderPath = cHeaderPath
self.includedLibraryLinks = includedLibraryLinks
self.packageName = packageName
self.includedPackages = includedPackages
}
}
/// A GIR namespace, grouping related type definitions within a repository.
///
/// Corresponds to the `<namespace>` element in a GIR XML file. A namespace
/// holds all type definitions classes, interfaces, records, enumerations,
/// bitfields, callbacks, global functions, constants, and type aliases that
/// belong to a single GIR namespace such as `Gtk` or `GObject`.
public struct Namespace {
/// The namespace name, e.g. `"Gtk"`.
public let name: String
/// The namespace version string, e.g. `"4.0"`.
public let version: String
/// The shared library name from the GIR file (e.g. "libgtk-4.so.1").
public var cSharedLibrary: String
/// The C identifier prefix (e.g. "Gtk", "G").
public var cIdentifierPrefix: String
/// The GObject classes defined in this namespace.
public var classes: [Class]
/// The GObject interfaces defined in this namespace.
public var interfaces: [Interface]
/// The plain C records (structs) defined in this namespace.
public var records: [Record]
/// The enumerations defined in this namespace.
public var enumerations: [Enumeration]
/// The bitfield (flags) types defined in this namespace.
public var bitfields: [Bitfield]
/// The callback function types defined in this namespace.
public var callbacks: [Callback]
/// The global (namespace-level) functions defined in this namespace.
public var functions: [GlobalFunction]
/// The constants defined in this namespace.
public var constants: [Constant]
/// The type aliases defined in this namespace.
public var aliases: [Alias]
/// Creates a new namespace.
/// - Parameters:
/// - name: The namespace name, e.g. `"Gtk"`.
/// - version: The namespace version string, e.g. `"4.0"`.
/// - cSharedLibrary: The shared library name (e.g. "libgtk-4.so.1").
/// - cIdentifierPrefix: The C identifier prefix (e.g. "Gtk", "G").
/// - classes: The GObject classes in the namespace.
/// - interfaces: The GObject interfaces in the namespace.
/// - records: The plain C records in the namespace.
/// - enumerations: The enumerations in the namespace.
/// - bitfields: The bitfield types in the namespace.
/// - callbacks: The callback types in the namespace.
/// - functions: The global functions in the namespace.
/// - constants: The constants in the namespace.
/// - aliases: The type aliases in the namespace.
public init(name: String, version: String, cSharedLibrary: String = "", cIdentifierPrefix: String = "",
classes: [Class] = [], interfaces: [Interface] = [],
records: [Record] = [], enumerations: [Enumeration] = [], bitfields: [Bitfield] = [],
callbacks: [Callback] = [], functions: [GlobalFunction] = [], constants: [Constant] = [],
aliases: [Alias] = []) {
self.name = name; self.version = version
self.cSharedLibrary = cSharedLibrary; self.cIdentifierPrefix = cIdentifierPrefix
self.classes = classes; self.interfaces = interfaces; self.records = records
self.enumerations = enumerations; self.bitfields = bitfields; self.callbacks = callbacks
self.functions = functions; self.constants = constants; self.aliases = aliases
}
}
/// A GObject class definition.
///
/// Corresponds to the `<class>` element in a GIR XML file. Models a GObject
/// class with its parent class, implemented interfaces, constructors, methods,
/// properties, signals, and associated functions.
public struct Class {
/// The class name, e.g. `"Widget"`.
public let name: String
/// The corresponding C type name, e.g. `"GtkWidget"`.
public let cType: String
/// The name of the parent class, or `nil` for the root `GObject` class.
public let parent: String?
/// Whether this class is abstract and cannot be instantiated directly.
///
/// Abstract classes are emitted without constructors: the C library
/// provides no way to instantiate them directly.
public var isAbstract: Bool
/// Whether GIR marks the class final (`final="1"`), forbidding subclassing.
public var isFinal: Bool
/// The `glib:get-type` function registering this class's GType,
/// e.g. `"gtk_widget_get_type"`.
public var getTypeFunction: String?
/// The registered GType name from `glib:type-name`, e.g. `"GtkWidget"`.
public var typeName: String?
/// GIR metadata governing whether this class should be bound at all.
public var symbolInfo: SymbolInfo
/// The names of interfaces this class implements.
public var implements: [String]
/// The constructors for this class.
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
public var constructors: [Constructor]
/// The methods of this class.
public var methods: [Method]
/// The GObject properties of this class.
public var properties: [Property]
/// The signals emitted by this class.
public var signals: [Signal]
/// The functions associated with this class.
public var functions: [GlobalFunction]
/// The GIR `glib:ref-func` attribute, present only on the root class of a
/// non-`GObject` fundamental type hierarchy (e.g. `GParamSpec`'s
/// `g_param_spec_ref_sink`). `nil` for ordinary `GObject`-derived classes,
/// which use `g_object_ref`/`g_object_ref_sink` instead.
public var refFunc: String?
/// The GIR `glib:unref-func` attribute see `refFunc`. `nil` for ordinary
/// `GObject`-derived classes, which use `g_object_unref` instead.
public var unrefFunc: String?
/// Creates a new class definition.
/// - Parameters:
/// - name: The class name, e.g. `"Widget"`.
/// - cType: The corresponding C type name, e.g. `"GtkWidget"`.
/// - parent: The name of the parent class, or `nil` if root.
/// - isAbstract: Whether the class is abstract. Defaults to `false`.
/// - isFinal: Whether GIR marks the class final. Defaults to `false`.
/// - getTypeFunction: The `glib:get-type` function name, if any.
/// - typeName: The registered GType name, if any.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - implements: The names of implemented interfaces. Defaults to empty.
/// - constructors: The constructors. Defaults to empty.
/// - methods: The methods. Defaults to empty.
/// - properties: The properties. Defaults to empty.
/// - signals: The signals. Defaults to empty.
/// - functions: The associated functions. Defaults to empty.
/// - doc: Documentation comment from the GIR XML.
/// - refFunc: The GIR `glib:ref-func` override, if any.
/// - unrefFunc: The GIR `glib:unref-func` override, if any.
public init(name: String, cType: String, parent: String?, isAbstract: Bool = false,
isFinal: Bool = false, getTypeFunction: String? = nil, typeName: String? = nil,
symbolInfo: SymbolInfo = SymbolInfo(),
implements: [String] = [], constructors: [Constructor] = [], methods: [Method] = [],
properties: [Property] = [], signals: [Signal] = [], functions: [GlobalFunction] = [],
doc: String? = nil, refFunc: String? = nil, unrefFunc: String? = nil) {
self.name = name; self.cType = cType; self.parent = parent
self.isAbstract = isAbstract; self.isFinal = isFinal
self.getTypeFunction = getTypeFunction; self.typeName = typeName
self.symbolInfo = symbolInfo; self.implements = implements
self.constructors = constructors; self.methods = methods
self.properties = properties; self.signals = signals; self.functions = functions
self.doc = doc
self.refFunc = refFunc; self.unrefFunc = unrefFunc
}
}
/// A GObject interface definition.
///
/// Corresponds to the `<interface>` element in a GIR XML file. An interface
/// declares methods, properties, and signals that implementing classes must
/// provide, along with prerequisite types that must be satisfied first.
public struct Interface {
/// The interface name, e.g. `"Buildable"`.
public let name: String
/// The corresponding C type name, e.g. `"GtkBuildable"`.
public let cType: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The methods declared by this interface.
public var methods: [Method]
/// The properties declared by this interface.
public var properties: [Property]
/// The signals declared by this interface.
public var signals: [Signal]
/// The functions associated with this interface (namespace-level `<function>`
/// elements that reference the interface's type).
public var functions: [GlobalFunction]
/// The prerequisite types a class must satisfy to implement this interface.
public var prereqs: [String]
/// The `glib:get-type` function registering this interface's GType.
public var getTypeFunction: String?
/// The registered GType name from `glib:type-name`, e.g. `"GtkBuildable"`.
public var typeName: String?
/// GIR metadata governing whether this interface should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new interface definition.
/// - Parameters:
/// - name: The interface name.
/// - cType: The corresponding C type name.
/// - methods: The methods declared by the interface. Defaults to empty.
/// - properties: The properties declared by the interface. Defaults to empty.
/// - signals: The signals declared by the interface. Defaults to empty.
/// - functions: The functions associated with this interface. Defaults to empty.
/// - prereqs: The prerequisite types. Defaults to empty.
/// - getTypeFunction: The `glib:get-type` function name, if any.
/// - typeName: The registered GType name, if any.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cType: String, methods: [Method] = [], properties: [Property] = [],
signals: [Signal] = [], functions: [GlobalFunction] = [], prereqs: [String] = [],
getTypeFunction: String? = nil, typeName: String? = nil,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.cType = cType; self.methods = methods
self.properties = properties; self.signals = signals; self.functions = functions; self.prereqs = prereqs
self.getTypeFunction = getTypeFunction; self.typeName = typeName
self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A plain C record (struct) definition.
///
/// Corresponds to the `<record>` element in a GIR XML file. Records are
/// value types in C and may be opaque (no fields exposed), disguised
/// (typedef'd without `struct` keyword), or have fully accessible fields.
public struct Record {
/// The record name.
public let name: String
/// The corresponding C type name.
public let cType: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// Whether the record is opaque (fields are not introspectable).
public var isOpaque: Bool
/// Whether the record is disguised (typedef'd without the `struct` keyword).
public var isDisguised: Bool
/// The class or interface whose GObject type struct this record is
/// (`glib:is-gtype-struct-for`), e.g. `"Widget"` for `GtkWidgetClass`.
///
/// Type structs are implementation details of the GObject type system and
/// are never bound.
public var isGTypeStructFor: String?
/// The `glib:get-type` function registering this record's boxed GType.
///
/// Its presence is what makes a record a *boxed* type safely copyable
/// and freeable via `g_boxed_copy`/`g_boxed_free`, and therefore bindable
/// as an opaque pointer wrapper. Records without it are skipped.
public var getTypeFunction: String?
/// The registered GType name from `glib:type-name`, e.g. `"GdkRGBA"`.
public var typeName: String?
/// An explicit copy function from `copy-function`, if the GIR states one.
public var copyFunction: String?
/// An explicit free function from `free-function`, if the GIR states one.
public var freeFunction: String?
/// GIR metadata governing whether this record should be bound at all.
public var symbolInfo: SymbolInfo
/// The fields of the record, if introspectable.
public var fields: [Field]
/// The methods operating on this record.
public var methods: [Method]
/// The constructors for this record.
public var constructors: [Constructor]
/// The functions associated with this record.
public var functions: [GlobalFunction]
/// Creates a new record definition.
/// - Parameters:
/// - name: The record name.
/// - cType: The corresponding C type name.
/// - isOpaque: Whether the record is opaque. Defaults to `false`.
/// - isDisguised: Whether the record is disguised. Defaults to `false`.
/// - isGTypeStructFor: The type this record is the GObject type struct for, if any.
/// - getTypeFunction: The `glib:get-type` function name, if any.
/// - typeName: The registered GType name, if any.
/// - copyFunction: An explicit copy function, if stated.
/// - freeFunction: An explicit free function, if stated.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - fields: The fields of the record. Defaults to empty.
/// - methods: The record methods. Defaults to empty.
/// - constructors: The record constructors. Defaults to empty.
/// - functions: The associated functions. Defaults to empty.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cType: String, isOpaque: Bool = false, isDisguised: Bool = false,
isGTypeStructFor: String? = nil, getTypeFunction: String? = nil, typeName: String? = nil,
copyFunction: String? = nil, freeFunction: String? = nil,
symbolInfo: SymbolInfo = SymbolInfo(),
fields: [Field] = [], methods: [Method] = [], constructors: [Constructor] = [],
functions: [GlobalFunction] = [], doc: String? = nil) {
self.name = name; self.cType = cType; self.isOpaque = isOpaque
self.isDisguised = isDisguised; self.isGTypeStructFor = isGTypeStructFor
self.getTypeFunction = getTypeFunction; self.typeName = typeName
self.copyFunction = copyFunction; self.freeFunction = freeFunction
self.symbolInfo = symbolInfo
self.fields = fields; self.methods = methods
self.constructors = constructors; self.functions = functions; self.doc = doc
}
/// Whether this record is a boxed type with a registered GType.
///
/// Boxed records can be wrapped as opaque pointer classes with
/// `g_boxed_copy`/`g_boxed_free` lifetimes. Non-boxed records are skipped
/// with ``SkipReason/plainRecord``.
public var isBoxed: Bool { getTypeFunction != nil }
}
/// A field within a C record.
///
/// Corresponds to the `<field>` element in a GIR XML file. Describes a named
/// member of a C struct, including its type and read/write permissions.
public struct Field {
/// The field name.
public let name: String
/// The GIR type of the field.
public let type: GIRType
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// Whether the field is readable (accessible for reading).
public let isReadable: Bool
/// Whether the field is writable (accessible for writing).
public let isWritable: Bool
/// Creates a new field.
/// - Parameters:
/// - name: The field name.
/// - type: The GIR type of the field.
/// - isReadable: Whether the field is readable. Defaults to `true`.
/// - isWritable: Whether the field is writable. Defaults to `false`.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, type: GIRType, isReadable: Bool = true, isWritable: Bool = false, doc: String? = nil) {
self.name = name; self.type = type; self.isReadable = isReadable; self.isWritable = isWritable; self.doc = doc
}
}
/// A GObject enumeration type.
///
/// Corresponds to the `<enumeration>` element in a GIR XML file. Defines a
/// set of named integer constants with their C identifiers and numeric values.
public struct Enumeration {
/// The enumeration name.
public let name: String
/// The corresponding C type name.
public let cType: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The members (enum values) of this enumeration.
public var members: [EnumMember]
/// The `glib:get-type` function registering this enum's GType, if any.
///
/// Its presence selects `g_value_get_enum`/`g_value_set_enum` for
/// property access; plain C enums without a GType cannot go through GValue.
public var getTypeFunction: String?
/// The registered GType name from `glib:type-name`, e.g. `"GtkAlign"`.
public var typeName: String?
/// The GLib error domain this enumeration defines, if it is an error enum.
public var errorDomain: String?
/// GIR metadata governing whether this enumeration should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new enumeration.
/// - Parameters:
/// - name: The enumeration name.
/// - cType: The corresponding C type name.
/// - members: The enum members. Defaults to empty.
/// - getTypeFunction: The `glib:get-type` function name, if any.
/// - typeName: The registered GType name, if any.
/// - errorDomain: The GLib error domain, if this is an error enum.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cType: String, members: [EnumMember] = [],
getTypeFunction: String? = nil, typeName: String? = nil, errorDomain: String? = nil,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.cType = cType; self.members = members
self.getTypeFunction = getTypeFunction; self.typeName = typeName
self.errorDomain = errorDomain; self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A single member (value) of an enumeration or bitfield.
///
/// Corresponds to the `<member>` element in a GIR XML file. Each member
/// has a name, its associated numeric value, and the full C identifier.
public struct EnumMember {
/// The member name, e.g. `"visible"`.
public let name: String
/// The numeric value as a string, e.g. `"1"`.
public let value: String
/// The full C identifier, e.g. `"GTK_WIDGET_VISIBLE"`.
public let cIdentifier: String
/// Creates a new enum member.
/// - Parameters:
/// - name: The member name.
/// - value: The numeric value as a string.
/// - cIdentifier: The full C identifier.
public init(name: String, value: String, cIdentifier: String) {
self.name = name; self.value = value; self.cIdentifier = cIdentifier
}
}
/// A GObject bitfield (flags) type.
///
/// Corresponds to the `<bitfield>` element in a GIR XML file. Defines a set
/// of named flags that can be combined with bitwise operations. Each member
/// represents a single bit in the flags value.
public struct Bitfield {
/// The bitfield type name.
public let name: String
/// The corresponding C type name.
public let cType: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The individual flag members.
///
/// Member values are the flag's actual numeric value (e.g. `4`), not a bit
/// position they are used verbatim as `OptionSet` raw values.
public var members: [EnumMember]
/// The `glib:get-type` function registering this bitfield's GType, if any.
///
/// Its presence selects `g_value_get_flags`/`g_value_set_flags` for
/// property access.
public var getTypeFunction: String?
/// The registered GType name from `glib:type-name`, e.g. `"GtkStateFlags"`.
public var typeName: String?
/// GIR metadata governing whether this bitfield should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new bitfield type.
/// - Parameters:
/// - name: The bitfield type name.
/// - cType: The corresponding C type name.
/// - members: The flag members. Defaults to empty.
/// - getTypeFunction: The `glib:get-type` function name, if any.
/// - typeName: The registered GType name, if any.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cType: String, members: [EnumMember] = [],
getTypeFunction: String? = nil, typeName: String? = nil,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.cType = cType; self.members = members
self.getTypeFunction = getTypeFunction; self.typeName = typeName
self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A callback function type.
///
/// Corresponds to the `<callback>` element in a GIR XML file. Describes the
/// function signature parameters and return type for a C callback used
/// in signal handlers, virtual functions, or asynchronous operations.
public struct Callback {
/// The callback type name.
public let name: String
/// The corresponding C type name.
public let cType: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The parameters of the callback function.
public var parameters: [Parameter]
/// The return value of the callback function.
public var returnValue: ReturnValue
/// Whether the callback takes a trailing `GError**`.
public var throwsGError: Bool
/// GIR metadata governing whether this callback should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new callback type.
/// - Parameters:
/// - name: The callback type name.
/// - cType: The corresponding C type name.
/// - parameters: The callback parameters. Defaults to empty.
/// - returnValue: The return value. Defaults to a `void`, non-transferring return.
/// - throwsGError: Whether the callback takes a `GError**`. Defaults to `false`.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cType: String, parameters: [Parameter] = [],
returnValue: ReturnValue = ReturnValue(), throwsGError: Bool = false,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.cType = cType; self.parameters = parameters
self.returnValue = returnValue; self.throwsGError = throwsGError
self.symbolInfo = symbolInfo; self.doc = doc
}
/// The index of the parameter carrying user data, if the callback has one.
///
/// Conventionally the trailing `gpointer user_data`. A callback without
/// such a slot cannot carry a Swift closure.
public var userDataParameterIndex: Int? {
parameters.lastIndex { $0.type == .pointer && $0.name.contains("data") }
}
}
/// A constructor for a GObject class.
///
/// Corresponds to the `<constructor>` element in a GIR XML file. Constructors
/// are special methods that create new instances of a GObject type, typically
/// wrapping C functions like `gtk_widget_new()`.
public struct Constructor {
/// The constructor name.
public let name: String
/// The corresponding C function identifier.
public let cIdentifier: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The parameters accepted by the constructor.
public var parameters: [Parameter]
/// The return value typically the constructed object type.
///
/// Note that GIR declares most widget constructors `transfer-ownership="none"`
/// even though they return a *floating* reference. The planner therefore
/// derives sinking from the class's `InitiallyUnowned` ancestry rather than
/// from this transfer annotation alone.
public var returnValue: ReturnValue
/// Whether the constructor takes a trailing `GError**` and can fail.
public var throwsGError: Bool
/// GIR metadata governing whether this constructor should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new constructor definition.
/// - Parameters:
/// - name: The constructor name.
/// - cIdentifier: The corresponding C function identifier.
/// - parameters: The constructor parameters. Defaults to empty.
/// - returnValue: The return value. Defaults to a `void`, non-transferring return.
/// - throwsGError: Whether the constructor takes a `GError**`. Defaults to `false`.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cIdentifier: String, parameters: [Parameter] = [],
returnValue: ReturnValue = ReturnValue(), throwsGError: Bool = false,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.cIdentifier = cIdentifier
self.parameters = parameters; self.returnValue = returnValue
self.throwsGError = throwsGError; self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A method of a GObject class, interface, or record.
///
/// Corresponds to the `<method>` element in a GIR XML file. Methods are
/// instance functions that operate on a particular type, identified by their
/// C function name.
public struct Method {
/// The method name, e.g. `"set_visible"`.
public let name: String
/// The corresponding C function identifier, e.g. `"gtk_widget_set_visible"`.
public let cIdentifier: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The parameters of the method, typically excluding the instance parameter.
public var parameters: [Parameter]
/// The return value of the method, with its ownership and nullability.
public var returnValue: ReturnValue
/// Whether the method takes a trailing `GError**` and can fail.
public var throwsGError: Bool
/// GIR metadata governing whether this method should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new method definition.
/// - Parameters:
/// - name: The method name.
/// - cIdentifier: The corresponding C function identifier.
/// - parameters: The method parameters. Defaults to empty.
/// - returnValue: The return value. Defaults to a `void`, non-transferring return.
/// - throwsGError: Whether the method takes a `GError**`. Defaults to `false`.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cIdentifier: String, parameters: [Parameter] = [],
returnValue: ReturnValue = ReturnValue(), throwsGError: Bool = false,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.cIdentifier = cIdentifier
self.parameters = parameters; self.returnValue = returnValue
self.throwsGError = throwsGError; self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A GObject property definition.
///
/// Corresponds to the `<property>` element in a GIR XML file. Properties are
/// named, typed attributes on GObject classes with configurable read/write
/// access and construct-time-only semantics.
public struct Property {
/// The property name, e.g. `"label"`.
public let name: String
/// The GIR type of the property.
public var type: GIRType
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// Whether the property is readable (has a getter).
public var isReadable: Bool
/// Whether the property is writable (has a setter).
public var isWritable: Bool
/// Whether the property can only be set during object construction.
public var isConstructOnly: Bool
/// Whether the property may hold `NULL`.
public var isNullable: Bool
/// How ownership transfers when reading or writing the property.
public var transferOwnership: TransferOwnership
/// The name of the method implementing this property's getter, if GIR
/// states one via the `getter` attribute (e.g. `"get_label"`).
///
/// When present, the accessor delegates to that already-planned method
/// instead of going through the GValue machinery simpler and correct by
/// construction.
public var getter: String?
/// The name of the method implementing this property's setter, if GIR
/// states one via the `setter` attribute (e.g. `"set_label"`).
public var setter: String?
/// GIR metadata governing whether this property should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new property definition.
/// - Parameters:
/// - name: The property name.
/// - type: The GIR type of the property.
/// - isReadable: Whether the property is readable. Defaults to `true`.
/// - isWritable: Whether the property is writable. Defaults to `false`.
/// - isConstructOnly: Whether the property is construct-only. Defaults to `false`.
/// - isNullable: Whether the property may be `NULL`. Defaults to `false`.
/// - transferOwnership: Ownership transfer semantics. Defaults to `.none`.
/// - getter: The name of the getter method, if stated.
/// - setter: The name of the setter method, if stated.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, type: GIRType, isReadable: Bool = true, isWritable: Bool = false,
isConstructOnly: Bool = false, isNullable: Bool = false,
transferOwnership: TransferOwnership = .none,
getter: String? = nil, setter: String? = nil,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.type = type
self.isReadable = isReadable; self.isWritable = isWritable
self.isConstructOnly = isConstructOnly; self.isNullable = isNullable
self.transferOwnership = transferOwnership
self.getter = getter; self.setter = setter
self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A GObject signal definition.
///
/// Corresponds to the `<signal>` element in a GIR XML file. Signals are
/// typed event emitters on GObject classes. Each signal has a parameter list,
/// a return value, and may support detailed (string-parameterized) connections.
public struct Signal {
/// The signal name, e.g. `"clicked"`.
public let name: String
/// The parameters emitted with the signal.
public var parameters: [Parameter]
/// The return value of the signal handler.
public var returnValue: ReturnValue
/// Whether the signal supports detail strings (e.g. `"notify::label"`).
public var isDetailed: Bool
/// GIR metadata governing whether this signal should be bound at all.
public var symbolInfo: SymbolInfo
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// Creates a new signal definition.
/// - Parameters:
/// - name: The signal name.
/// - parameters: The signal parameters. Defaults to empty.
/// - returnValue: The handler return value. Defaults to a `void`, non-transferring return.
/// - isDetailed: Whether the signal supports detail strings. Defaults to `false`.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, parameters: [Parameter] = [], returnValue: ReturnValue = ReturnValue(),
isDetailed: Bool = false, symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.parameters = parameters; self.returnValue = returnValue
self.isDetailed = isDetailed; self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A global (namespace-level) function.
///
/// Corresponds to the `<function>` element at the namespace level in a GIR
/// XML file. These are free functions not associated with any particular
/// type, such as utility or factory functions.
public struct GlobalFunction {
/// The function name.
public let name: String
/// The corresponding C function identifier.
public let cIdentifier: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The parameters of the function.
public var parameters: [Parameter]
/// The return value of the function, with its ownership and nullability.
public var returnValue: ReturnValue
/// Whether the function takes a trailing `GError**` and can fail.
public var throwsGError: Bool
/// GIR metadata governing whether this function should be bound at all.
public var symbolInfo: SymbolInfo
/// Creates a new global function definition.
/// - Parameters:
/// - name: The function name.
/// - cIdentifier: The corresponding C function identifier.
/// - parameters: The function parameters. Defaults to empty.
/// - returnValue: The return value. Defaults to a `void`, non-transferring return.
/// - throwsGError: Whether the function takes a `GError**`. Defaults to `false`.
/// - symbolInfo: GIR binding metadata. Defaults to introspectable and bindable.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cIdentifier: String, parameters: [Parameter] = [],
returnValue: ReturnValue = ReturnValue(), throwsGError: Bool = false,
symbolInfo: SymbolInfo = SymbolInfo(), doc: String? = nil) {
self.name = name; self.cIdentifier = cIdentifier
self.parameters = parameters; self.returnValue = returnValue
self.throwsGError = throwsGError; self.symbolInfo = symbolInfo; self.doc = doc
}
}
/// A constant value definition.
///
/// Corresponds to the `<constant>` element in a GIR XML file. Constants are
/// named immutable values with a specific GIR type, such as enum defaults or
/// version numbers.
public struct Constant {
/// The constant name.
public let name: String
/// The constant value as a string representation.
public let value: String
/// The GIR type of the constant.
public var type: GIRType
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// Creates a new constant definition.
/// - Parameters:
/// - name: The constant name.
/// - value: The constant value as a string representation.
/// - type: The GIR type of the constant.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, value: String, type: GIRType, doc: String? = nil) {
self.name = name; self.value = value; self.type = type; self.doc = doc
}
}
/// A type alias definition.
///
/// Corresponds to the `<alias>` element in a GIR XML file. Provides an
/// alternative name (with an optional C type) for an existing GIR type,
/// useful for platform-specific or convenience typedefs.
public struct Alias {
/// The alias name.
public let name: String
/// The corresponding C type name.
public let cType: String
/// The underlying GIR type this alias refers to.
public var target: GIRType
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// Creates a new type alias.
/// - Parameters:
/// - name: The alias name.
/// - cType: The corresponding C type name.
/// - target: The underlying GIR type to alias.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, cType: String, target: GIRType, doc: String? = nil) {
self.name = name; self.cType = cType; self.target = target; self.doc = doc
}
}
// MARK: - Shared Types
/// A parameter of a function, method, constructor, callback, or signal.
///
/// Corresponds to the `<parameter>` element in a GIR XML file. Describes the
/// parameter's name, type, C type name, nullability, optionality, ownership
/// transfer rules, and whether it is the implicit instance parameter
/// (equivalent to `self`).
public struct Parameter {
/// The parameter name.
public let name: String
/// The GIR type of the parameter.
public var type: GIRType
/// The C type name (e.g. "GtkWidget", "GObject"). Empty string if not available.
public var cType: String
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// Whether the parameter may be `nil` (NULL).
public var isNullable: Bool
/// Whether the parameter is optional (may be omitted at the call site).
public var isOptional: Bool
/// How ownership is transferred for this parameter.
public var transferOwnership: TransferOwnership
/// Whether this is the implicit instance parameter (self) of a method.
public var isInstanceParameter: Bool
/// The direction of data flow for this parameter.
public var direction: ParameterDirection
/// Whether the caller allocates the storage an out parameter writes into.
///
/// Only meaningful when ``direction`` is `.out`. Caller-allocated out
/// parameters take a pointer to existing storage; callee-allocated ones
/// take a pointer to a pointer the callee fills in.
public var callerAllocates: Bool
/// The lifetime of this parameter's callback, when it is a callback.
public var scope: CallbackScope?
/// Index of the parameter carrying this callback's user data, if any.
///
/// From the GIR `closure` attribute. A callback without a user-data slot
/// cannot carry a Swift closure and forces its callable to be skipped.
public var closureIndex: Int?
/// Index of the parameter carrying this callback's `DestroyNotify`, if any.
///
/// From the GIR `destroy` attribute.
public var destroyIndex: Int?
/// Creates a new parameter definition.
/// - Parameters:
/// - name: The parameter name.
/// - type: The GIR type of the parameter.
/// - cType: The corresponding C type name. Defaults to `""`.
/// - isNullable: Whether the parameter may be nil. Defaults to `false`.
/// - isOptional: Whether the parameter is optional. Defaults to `false`.
/// - transferOwnership: How ownership is transferred. Defaults to `.none`.
/// - isInstanceParameter: Whether this is the instance parameter. Defaults to `false`.
/// - direction: The direction of data flow. Defaults to `.in`.
/// - callerAllocates: Whether the caller allocates out-parameter storage.
/// Defaults to `false`.
/// - scope: The callback lifetime, when this parameter is a callback.
/// - closureIndex: Index of the user-data parameter, if any.
/// - destroyIndex: Index of the `DestroyNotify` parameter, if any.
/// - doc: Documentation comment from the GIR XML.
public init(name: String, type: GIRType, cType: String = "", isNullable: Bool = false, isOptional: Bool = false,
transferOwnership: TransferOwnership = .none, isInstanceParameter: Bool = false,
direction: ParameterDirection = .in, callerAllocates: Bool = false,
scope: CallbackScope? = nil, closureIndex: Int? = nil, destroyIndex: Int? = nil,
doc: String? = nil) {
self.name = name; self.type = type; self.cType = cType; self.isNullable = isNullable
self.isOptional = isOptional; self.transferOwnership = transferOwnership
self.isInstanceParameter = isInstanceParameter
self.direction = direction; self.callerAllocates = callerAllocates
self.scope = scope; self.closureIndex = closureIndex; self.destroyIndex = destroyIndex
self.doc = doc
}
}
/// Describes how ownership of a value is transferred between caller and callee.
///
/// Maps to the `transfer-ownership` attribute in GIR XML. Controls memory
/// management semantics: whether the caller must free the returned value
/// (`.full`), whether only the container is owned (`.container`), or whether
/// no ownership transfer occurs (`.none`).
public enum TransferOwnership: String, Sendable {
/// No transfer; the caller does not own the value and must not free it.
case none
/// Full transfer; the caller owns the value and is responsible for freeing it.
case full
/// Container transfer; the caller owns the container but not its elements.
case container
}
/// The direction of data flow for a parameter.
///
/// Maps to the `direction` attribute in GIR XML. Out and in-out parameters are
/// passed as pointers in C and require dedicated marshalling in Swift.
public enum ParameterDirection: String, Sendable {
/// The value flows from caller to callee (the default).
case `in`
/// The value flows from callee to caller via a pointer.
case out
/// The value flows in both directions via a pointer.
case inout_ = "inout"
}
/// The lifetime of a callback parameter relative to the call it is passed to.
///
/// Maps to the `scope` attribute in GIR XML. Determines how the generator must
/// box and release the Swift closure backing a C callback.
public enum CallbackScope: String, Sendable {
/// The callback is only invoked during the call; no box retention needed.
case call
/// The callback is invoked exactly once, later; the box is consumed then.
case async
/// The callback lives until its `DestroyNotify` fires.
case notified
/// The callback lives forever; the box is never released.
case forever
}
/// Metadata describing how a GIR `<array>` determines its length.
///
/// Derived from the `length`, `fixed-size`, and `zero-terminated` attributes on
/// the GIR `<array>` element. Without one of these, a C array cannot be safely
/// bridged and the enclosing callable must be skipped.
public struct ArrayInfo: Equatable, Sendable {
/// Index of the parameter carrying the array length, if length-delimited.
///
/// The index refers to the enclosing callable's GIR parameter list,
/// excluding the instance parameter matching GIR's own numbering.
public var lengthParameterIndex: Int?
/// The compile-time element count, if the array is fixed-size.
public var fixedSize: Int?
/// Whether the array is terminated by a `NULL`/zero element.
public var isZeroTerminated: Bool
/// The C type spelling of the array itself (e.g. `"char**"`), when present.
public var cType: String
/// Creates array length metadata.
///
/// - Parameters:
/// - lengthParameterIndex: Index of the length parameter, if any.
/// - fixedSize: The fixed element count, if any.
/// - isZeroTerminated: Whether a zero/NULL terminator delimits the array.
/// Defaults to `false`.
/// - cType: The C type spelling of the array. Defaults to `""`.
public init(lengthParameterIndex: Int? = nil, fixedSize: Int? = nil,
isZeroTerminated: Bool = false, cType: String = "") {
self.lengthParameterIndex = lengthParameterIndex
self.fixedSize = fixedSize
self.isZeroTerminated = isZeroTerminated
self.cType = cType
}
/// Whether the array's length can be determined at all.
///
/// Arrays failing this check cannot be bridged and cause their enclosing
/// callable to be skipped with ``SkipReason/arrayWithoutLength``.
public var hasKnownLength: Bool {
lengthParameterIndex != nil || fixedSize != nil || isZeroTerminated
}
}
/// The return value of a callable, with its ownership and nullability.
///
/// Corresponds to the `<return-value>` element in GIR XML. Bundling the type
/// with its `transfer-ownership` and `nullable` attributes keeps the semantics
/// the binding planner needs attached to the type, rather than discarded.
public struct ReturnValue: Equatable, Sendable {
/// The GIR type of the returned value.
public var type: GIRType
/// Whether the callee may return `NULL`.
public var isNullable: Bool
/// How ownership of the returned value transfers to the caller.
public var transferOwnership: TransferOwnership
/// Documentation comment from the GIR XML `<doc>` element.
public var doc: String?
/// The raw GIR `c:type` attribute, e.g. `"const guint8*"`. Empty when
/// the GIR omitted it. Used to detect scalar types returned through a
/// pointer (no `<array>` length) that the type name alone hides.
public var cType: String
/// Creates a return value description.
///
/// - Parameters:
/// - type: The GIR type returned. Defaults to `.void`.
/// - isNullable: Whether `NULL` may be returned. Defaults to `false`.
/// - transferOwnership: Ownership transfer to the caller. Defaults to `.none`.
/// - doc: Documentation comment from the GIR XML.
/// - cType: The raw GIR `c:type` attribute. Defaults to `""`.
public init(type: GIRType = .void, isNullable: Bool = false,
transferOwnership: TransferOwnership = .none, doc: String? = nil, cType: String = "") {
self.type = type
self.isNullable = isNullable
self.transferOwnership = transferOwnership
self.doc = doc
self.cType = cType
}
}
/// GIR metadata shared by every bindable symbol.
///
/// Captures the attributes that determine whether a symbol should be bound at
/// all, independent of its type signature. The binding planner consults these
/// before attempting to plan a symbol.
public struct SymbolInfo: Equatable, Sendable {
/// Whether the symbol is introspectable (`introspectable="0"` means no).
///
/// Non-introspectable symbols are outside the GIR ABI contract and are
/// never bound.
public var isIntrospectable: Bool
/// Whether the symbol is marked deprecated.
public var isDeprecated: Bool
/// The version in which the symbol was deprecated, if stated.
public var deprecatedVersion: String?
/// The name of the symbol that shadows this one, if any.
///
/// GIR marks the lower-fidelity of two overlapping symbols with
/// `shadowed-by`; only the shadowing symbol should be bound.
public var shadowedBy: String?
/// The name this symbol shadows, if any.
public var shadows: String?
/// The symbol this one was renamed to (`moved-to`), if any.
public var movedTo: String?
/// Creates symbol metadata.
///
/// - Parameters:
/// - isIntrospectable: Whether the symbol is introspectable. Defaults to `true`.
/// - isDeprecated: Whether the symbol is deprecated. Defaults to `false`.
/// - deprecatedVersion: The deprecation version, if stated.
/// - shadowedBy: The name of the shadowing symbol, if any.
/// - shadows: The name of the shadowed symbol, if any.
/// - movedTo: The rename target, if any.
public init(isIntrospectable: Bool = true, isDeprecated: Bool = false,
deprecatedVersion: String? = nil, shadowedBy: String? = nil,
shadows: String? = nil, movedTo: String? = nil) {
self.isIntrospectable = isIntrospectable
self.isDeprecated = isDeprecated
self.deprecatedVersion = deprecatedVersion
self.shadowedBy = shadowedBy
self.shadows = shadows
self.movedTo = movedTo
}
/// Whether the symbol is a candidate for binding at all.
///
/// False for non-introspectable symbols, symbols shadowed by a
/// higher-fidelity variant, and symbols that have moved elsewhere.
public var isBindable: Bool {
isIntrospectable && shadowedBy == nil && movedTo == nil
}
}
/// A GIR type reference, covering primitives, named type references, arrays, and optionals.
///
/// Corresponds to the `<type>` element in GIR XML. This recursive enum models
/// the full GIR type system: scalar primitives, named type references pointing
/// to other GIR types, arrays (both GArray and C-style fixed arrays), and
/// nullable/optional wrappers.
public indirect enum GIRType: Equatable, Sendable {
/// No return value (void).
case void
/// A boolean value, mapped from `gboolean`.
case boolean
/// A signed 8-bit integer, mapped from `gint8`.
case int8
/// A signed 16-bit integer, mapped from `gint16`.
case int16
/// A signed 32-bit integer, mapped from `gint32`.
case int32
/// A signed 64-bit integer, mapped from `gint64`.
case int64
/// An unsigned 8-bit integer, mapped from `guint8`.
case uint8
/// An unsigned 16-bit integer, mapped from `guint16`.
case uint16
/// An unsigned 32-bit integer, mapped from `guint32`.
case uint32
/// An unsigned 64-bit integer, mapped from `guint64`.
case uint64
/// A platform-width signed integer, mapped from `glong`.
case long
/// A platform-width unsigned integer, mapped from `gulong`.
case ulong
/// A pointer-width unsigned size, mapped from `gsize`.
case size
/// A pointer-width signed size, mapped from `gssize`.
case ssize
/// A single C character, mapped from `gchar`.
case char
/// A single unsigned C character, mapped from `guchar`.
case uchar
/// A UCS-4 code point, mapped from `gunichar`.
case unichar
/// A GObject type identifier, mapped from `GType`.
case gtype
/// A single-precision floating-point value, mapped from `gfloat`.
case float
/// A double-precision floating-point value, mapped from `gdouble`.
case double
/// A null-terminated UTF-8 string, mapped from `utf8`.
case string
/// A filename string (platform-dependent encoding), mapped from `filename`.
case filename
/// An opaque pointer, mapped from `gpointer`.
case pointer
/// A variadic argument list, mapped from `va_list`. Never bindable.
case vaList
/// A reference to a named type, possibly from another namespace.
/// - Parameters:
/// - String: The type name, e.g. `"Widget"`.
/// - namespace: The namespace qualifier, or `nil` for the current namespace.
case typeRef(String, namespace: String?)
/// A GLib container type (`GList`, `GSList`, `GHashTable`, ) with its
/// element types, in GIR declaration order.
///
/// Kept distinct from ``typeRef(_:namespace:)`` so the planner can reason
/// about element bridging rather than treating a container as an opaque
/// named type.
case container(ContainerKind, elements: [GIRType])
/// A C array of the given element type, with its length metadata.
case cArray(GIRType, ArrayInfo)
/// An optional (nullable) value of the given type.
case `optional`(GIRType)
/// Creates a type reference in the current namespace.
/// - Parameter name: The unqualified type name.
/// - Returns: A `typeRef` with no namespace qualifier.
public static func typeRef(_ name: String) -> GIRType {
.typeRef(name, namespace: nil)
}
/// Creates a C array with no length metadata.
///
/// Such arrays cannot be bridged; the planner skips callables using them
/// with ``SkipReason/arrayWithoutLength``.
///
/// - Parameter element: The array's element type.
/// - Returns: A `cArray` with empty ``ArrayInfo``.
public static func cArray(_ element: GIRType) -> GIRType {
.cArray(element, ArrayInfo())
}
}
/// The kind of a GLib container type.
///
/// Containers carry their elements' types separately from their own identity,
/// which is what lets the planner decide whether the elements can be bridged.
public enum ContainerKind: String, Equatable, Sendable {
/// A doubly-linked `GList`.
case list
/// A singly-linked `GSList`.
case slist
/// A `GHashTable` mapping keys to values.
case hashTable
/// A `GArray` of elements.
case array
/// A `GPtrArray` of pointers.
case ptrArray
/// A `GByteArray` of bytes.
case byteArray
}
extension GIRType {
/// True for primitive types that don't need C enum/string/pointer bridging.
/// These can be used in function parameters without the complex C type
/// resolution that Phase C1 will provide.
var isSimplePrimitive: Bool {
switch self {
case .void, .boolean, .int8, .int16, .int32, .int64,
.uint8, .uint16, .uint32, .uint64,
.long, .ulong, .size, .ssize,
.float, .double:
return true
default:
return false
}
}
}