|
| 1 | +// SPDX-FileCopyrightText: 2021 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]> |
| 2 | +// SPDX-FileContributor: Andrew Hayzen <[email protected]> |
| 3 | +// SPDX-FileContributor: Gerhard de Clercq <[email protected]> |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT OR Apache-2.0 |
| 6 | + |
| 7 | +#![deny(missing_docs)] |
| 8 | +//! The cxx-qt-macro crate provides the procedural attribute macros which are used with cxx-qt. |
| 9 | +
|
| 10 | +use proc_macro::TokenStream; |
| 11 | +use syn::{parse_macro_input, ItemMod}; |
| 12 | + |
| 13 | +use cxx_qt_gen::{write_rust, GeneratedRustBlocks, Parser}; |
| 14 | + |
| 15 | +/// A procedural macro which generates a QObject for a struct inside a module. |
| 16 | +/// |
| 17 | +/// # Example |
| 18 | +/// |
| 19 | +/// ```rust |
| 20 | +/// #[cxx_qt::bridge(namespace = "cxx_qt::my_object")] |
| 21 | +/// mod my_object { |
| 22 | +/// #[cxx_qt::qobject] |
| 23 | +/// #[derive(Default)] |
| 24 | +/// # // Note that we can't use properties as this confuses the linker on Windows |
| 25 | +/// pub struct MyObject; |
| 26 | +/// |
| 27 | +/// impl qobject::MyObject { |
| 28 | +/// #[qinvokable] |
| 29 | +/// fn invokable(&self, a: i32, b: i32) -> i32 { |
| 30 | +/// a + b |
| 31 | +/// } |
| 32 | +/// } |
| 33 | +/// } |
| 34 | +/// |
| 35 | +/// # // Note that we need a fake main for doc tests to build |
| 36 | +/// # fn main() {} |
| 37 | +/// ``` |
| 38 | +#[proc_macro_attribute] |
| 39 | +pub fn bridge(args: TokenStream, input: TokenStream) -> TokenStream { |
| 40 | + // Parse the TokenStream of a macro |
| 41 | + // this triggers a compile failure if the tokens fail to parse. |
| 42 | + let mut module = parse_macro_input!(input as ItemMod); |
| 43 | + |
| 44 | + // Macros do not typically need to do anything with their own attribute name, |
| 45 | + // so rustc does not include that in the `args` or `input` TokenStreams. |
| 46 | + // |
| 47 | + // However, other code paths that use the parser do not enter from a macro invocation, |
| 48 | + // so they rely on parsing the `cxx_qt::bridge` attribute to identify where to start parsing. |
| 49 | + // |
| 50 | + // To keep the inputs to the parser consistent for all code paths, |
| 51 | + // add the attribute to the module before giving it to the parser. |
| 52 | + let args_input = format!("#[cxx_qt::bridge({args})] mod dummy;"); |
| 53 | + let attrs = syn::parse_str::<ItemMod>(&args_input).unwrap().attrs; |
| 54 | + module.attrs = attrs.into_iter().chain(module.attrs.into_iter()).collect(); |
| 55 | + |
| 56 | + // Extract and generate the rust code |
| 57 | + extract_and_generate(module) |
| 58 | +} |
| 59 | + |
| 60 | +/// A macro which describes that an enum defines the signals for a QObject. |
| 61 | +/// |
| 62 | +/// It should not be used by itself and instead should be used inside a cxx_qt::bridge definition. |
| 63 | +/// |
| 64 | +/// # Example |
| 65 | +/// |
| 66 | +/// ```rust |
| 67 | +/// #[cxx_qt::bridge] |
| 68 | +/// mod my_object { |
| 69 | +/// #[cxx_qt::qobject] |
| 70 | +/// #[derive(Default)] |
| 71 | +/// # // Note that we can't use properties as this confuses the linker on Windows |
| 72 | +/// pub struct MyObject; |
| 73 | +/// |
| 74 | +/// #[cxx_qt::qsignals(MyObject)] |
| 75 | +/// pub enum MySignals { |
| 76 | +/// Ready, |
| 77 | +/// } |
| 78 | +/// } |
| 79 | +/// |
| 80 | +/// # // Note that we need a fake main for doc tests to build |
| 81 | +/// # fn main() {} |
| 82 | +/// ``` |
| 83 | +#[proc_macro_attribute] |
| 84 | +pub fn qsignals(_args: TokenStream, _input: TokenStream) -> TokenStream { |
| 85 | + unreachable!("cxx_qt::qsignals should not be used as a macro by itself. Instead it should be used within a cxx_qt::bridge definition") |
| 86 | +} |
| 87 | + |
| 88 | +/// A macro which describes that a struct should be made into a QObject. |
| 89 | +/// |
| 90 | +/// It should not be used by itself and instead should be used inside a cxx_qt::bridge definition. |
| 91 | +/// |
| 92 | +/// # Example |
| 93 | +/// |
| 94 | +/// ```rust |
| 95 | +/// #[cxx_qt::bridge] |
| 96 | +/// mod my_object { |
| 97 | +/// #[cxx_qt::qobject] |
| 98 | +/// #[derive(Default)] |
| 99 | +/// # // Note that we can't use properties as this confuses the linker on Windows |
| 100 | +/// pub struct MyObject; |
| 101 | +/// } |
| 102 | +/// |
| 103 | +/// # // Note that we need a fake main for doc tests to build |
| 104 | +/// # fn main() {} |
| 105 | +/// ``` |
| 106 | +/// |
| 107 | +/// You can also specify a custom base class by using `#[cxx_qt::qobject(base = "QStringListModel")]`, you must then use CXX to add any includes needed. |
| 108 | +/// |
| 109 | +/// # Example |
| 110 | +/// |
| 111 | +/// ```rust |
| 112 | +/// #[cxx_qt::bridge] |
| 113 | +/// mod my_object { |
| 114 | +/// #[cxx_qt::qobject(base = "QStringListModel")] |
| 115 | +/// #[derive(Default)] |
| 116 | +/// # // Note that we can't use properties as this confuses the linker on Windows |
| 117 | +/// pub struct MyModel; |
| 118 | +/// |
| 119 | +/// unsafe extern "C++" { |
| 120 | +/// include!(<QtCore/QStringListModel>); |
| 121 | +/// } |
| 122 | +/// } |
| 123 | +/// |
| 124 | +/// # // Note that we need a fake main for doc tests to build |
| 125 | +/// # fn main() {} |
| 126 | +/// ``` |
| 127 | +#[proc_macro_attribute] |
| 128 | +pub fn qobject(_args: TokenStream, _input: TokenStream) -> TokenStream { |
| 129 | + unreachable!("cxx_qt::qobject should not be used as a macro by itself. Instead it should be used within a cxx_qt::bridge definition") |
| 130 | +} |
| 131 | + |
| 132 | +/// A macro which allows you to access base class methods from within Rust. |
| 133 | +/// |
| 134 | +/// It should not be used by itself and instead should be used inside a cxx_qt::bridge definition. |
| 135 | +/// Furthermore, the macro must be placed within the `impl` block of a `qobject::T`. |
| 136 | +/// See [the book page](https://kdab.github.io/cxx-qt/book/concepts/inheritance.html) for more |
| 137 | +/// details. |
| 138 | +/// |
| 139 | +/// # Example |
| 140 | +/// ``` rust |
| 141 | +/// #[cxx_qt::bridge] |
| 142 | +/// mod my_object { |
| 143 | +/// extern "C++" { |
| 144 | +/// include!("cxx-qt-lib/qmodelindex.h"); |
| 145 | +/// type QModelIndex = cxx_qt_lib::QModelIndex; |
| 146 | +/// } |
| 147 | +/// |
| 148 | +/// #[cxx_qt::qobject(base="QAbstractListModel")] |
| 149 | +/// #[derive(Default)] |
| 150 | +/// # // Note that we can't use properties as this confuses the linker on Windows |
| 151 | +/// pub struct MyObject; |
| 152 | +/// |
| 153 | +/// #[cxx_qt::inherit] |
| 154 | +/// extern "C++" { |
| 155 | +/// // Unsafe to call |
| 156 | +/// unsafe fn begin_insert_rows(self: Pin<&mut qobject::MyObject>, parent: &QModelIndex, first: i32, last: i32); |
| 157 | +/// } |
| 158 | +/// |
| 159 | +/// #[cxx_qt::inherit] |
| 160 | +/// unsafe extern "C++" { |
| 161 | +/// // Safe to call - you are responsible to ensure this is true! |
| 162 | +/// fn end_insert_rows(self: Pin<&mut qobject::MyObject>); |
| 163 | +/// } |
| 164 | +/// } |
| 165 | +/// |
| 166 | +/// # // Note that we need a fake main for doc tests to build |
| 167 | +/// # fn main() {} |
| 168 | +/// ``` |
| 169 | +#[proc_macro_attribute] |
| 170 | +pub fn inherit(_args: TokenStream, _input: TokenStream) -> TokenStream { |
| 171 | + unreachable!("cxx_qt::inherit should not be used as a macro by itself. Instead it should be used within a cxx_qt::bridge definition") |
| 172 | +} |
| 173 | + |
| 174 | +// Take the module and C++ namespace and generate the rust code |
| 175 | +fn extract_and_generate(module: ItemMod) -> TokenStream { |
| 176 | + Parser::from(module) |
| 177 | + .and_then(|parser| GeneratedRustBlocks::from(&parser)) |
| 178 | + .map(|generated_rust| write_rust(&generated_rust)) |
| 179 | + .unwrap_or_else(|err| err.to_compile_error()) |
| 180 | + .into() |
| 181 | +} |
0 commit comments