swift_bridge_ir/
lib.rs

1//! An intermediate representation of the FFI layer.
2//!
3//! Things annotated with the `#[swift_bridge::bridge]` attribute get parsed into this IR.
4//!
5//! This IR is then used to generate the C header files, Objective-C bridging headers, Swift code,
6//! and Rust code needed to power Rust + Swift interop.
7
8#![deny(missing_docs)]
9
10use proc_macro2::Ident;
11use syn::{Path, Visibility};
12
13use crate::bridge_module_attributes::CfgAttr;
14use crate::parse::TypeDeclarations;
15use crate::parsed_extern_fn::ParsedExternFn;
16
17pub use self::bridge_macro_attributes::{SwiftBridgeModuleAttr, SwiftBridgeModuleAttrs};
18pub use self::codegen::CodegenConfig;
19
20mod errors;
21mod parse;
22
23mod bridge_macro_attributes;
24mod bridge_module_attributes;
25mod bridged_type;
26mod parsed_extern_fn;
27
28mod codegen;
29
30#[cfg(test)]
31mod test_utils;
32
33const SWIFT_BRIDGE_PREFIX: &'static str = "__swift_bridge__";
34
35/// Represents a type definition within an `extern "Rust"` module, as well as all of its methods.
36///
37/// ```no_run,ignore
38/// #[swift_bridge::bridge]
39/// mod ffi {
40///     extern "Rust" {
41///         type Stack;
42///
43///         fn push(&mut self, val: u8);
44///
45///         fn pop(self: &mut Stack) -> Option<u8>;
46///
47///         fn as_ptr(&self) -> *const u8;
48///
49///         fn len(self: &Stack) -> usize;
50///
51///         fn consume(self);
52///     }
53///
54///     extern "Swift" {
55///         // TODO: Examples
56///     }
57/// }
58/// ```
59pub struct SwiftBridgeModule {
60    name: Ident,
61    vis: Visibility,
62    types: TypeDeclarations,
63    functions: Vec<ParsedExternFn>,
64    swift_bridge_path: Path,
65    cfg_attrs: Vec<CfgAttr>,
66}
67
68impl SwiftBridgeModule {
69    /// Set the path used for `swift_bridge` types such as `swift_bridge::RustString`.
70    /// We set this to `crate` when we're inside of the `swift_bridge` crate.
71    pub fn set_swift_bridge_path(&mut self, path: Path) {
72        self.swift_bridge_path = path;
73    }
74}
75
76#[cfg(test)]
77mod tests {
78
79    #[test]
80    fn foo() {
81        //
82    }
83}