Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1 | // Part of the Crubit project, under the Apache License v2.0 with LLVM |
| 2 | // Exceptions. See /LICENSE for license information. |
| 3 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 4 | |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 5 | #[cfg(test)] |
| 6 | #[macro_use] |
| 7 | extern crate static_assertions; |
| 8 | |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 9 | use anyhow::{anyhow, bail, ensure, Context, Result}; |
Marcel Hlopko | 884ae7f | 2021-08-18 13:58:22 +0000 | [diff] [blame] | 10 | use ffi_types::*; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 11 | use ir::*; |
| 12 | use itertools::Itertools; |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 13 | use proc_macro2::{Ident, Literal, TokenStream}; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 14 | use quote::format_ident; |
| 15 | use quote::quote; |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 16 | use std::collections::{BTreeSet, HashMap, HashSet}; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 17 | use std::iter::Iterator; |
| 18 | use std::panic::catch_unwind; |
| 19 | use std::process; |
Marcel Hlopko | 65d05f0 | 2021-12-09 12:29:24 +0000 | [diff] [blame] | 20 | use token_stream_printer::{rs_tokens_to_formatted_string, tokens_to_string}; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 21 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 22 | /// FFI equivalent of `Bindings`. |
| 23 | #[repr(C)] |
| 24 | pub struct FfiBindings { |
| 25 | rs_api: FfiU8SliceBox, |
| 26 | rs_api_impl: FfiU8SliceBox, |
| 27 | } |
| 28 | |
| 29 | /// Deserializes IR from `json` and generates bindings source code. |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 30 | /// |
| 31 | /// This function panics on error. |
| 32 | /// |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 33 | /// # Safety |
| 34 | /// |
| 35 | /// Expectations: |
| 36 | /// * function expects that param `json` is a FfiU8Slice for a valid array of |
| 37 | /// bytes with the given size. |
| 38 | /// * function expects that param `json` doesn't change during the call. |
| 39 | /// |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 40 | /// Ownership: |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 41 | /// * function doesn't take ownership of (in other words it borrows) the |
| 42 | /// param `json` |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 43 | /// * function passes ownership of the returned value to the caller |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 44 | #[no_mangle] |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 45 | pub unsafe extern "C" fn GenerateBindingsImpl(json: FfiU8Slice) -> FfiBindings { |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 46 | catch_unwind(|| { |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 47 | // It is ok to abort here. |
| 48 | let Bindings { rs_api, rs_api_impl } = generate_bindings(json.as_slice()).unwrap(); |
| 49 | |
| 50 | FfiBindings { |
| 51 | rs_api: FfiU8SliceBox::from_boxed_slice(rs_api.into_bytes().into_boxed_slice()), |
| 52 | rs_api_impl: FfiU8SliceBox::from_boxed_slice( |
| 53 | rs_api_impl.into_bytes().into_boxed_slice(), |
| 54 | ), |
| 55 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 56 | }) |
| 57 | .unwrap_or_else(|_| process::abort()) |
| 58 | } |
| 59 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 60 | /// Source code for generated bindings. |
| 61 | struct Bindings { |
| 62 | // Rust source code. |
| 63 | rs_api: String, |
| 64 | // C++ source code. |
| 65 | rs_api_impl: String, |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 68 | fn generate_bindings(json: &[u8]) -> Result<Bindings> { |
| 69 | let ir = deserialize_ir(json)?; |
Marcel Hlopko | ca84ff4 | 2021-12-09 14:15:14 +0000 | [diff] [blame] | 70 | |
| 71 | // The code is formatted with a non-default rustfmt configuration. Prevent |
Lukasz Anforowicz | 5b3f530 | 2022-02-07 01:04:47 +0000 | [diff] [blame] | 72 | // downstream workflows from reformatting with a different configuration by |
| 73 | // marking the output with `@generated`. See also |
| 74 | // https://rust-lang.github.io/rustfmt/?version=v1.4.38&search=#format_generated_files |
| 75 | // |
| 76 | // TODO(lukasza): It would be nice to include "by $argv[0]"" in the |
| 77 | // @generated comment below. OTOH, `std::env::current_exe()` in our |
| 78 | // current build environment returns a guid-like path... :-/ |
Lukasz Anforowicz | 72c4d22 | 2022-02-18 19:07:28 +0000 | [diff] [blame] | 79 | // |
| 80 | // TODO(lukasza): Try to remove `#![rustfmt:skip]` - in theory it shouldn't |
| 81 | // be needed when `@generated` comment/keyword is present... |
Lukasz Anforowicz | 5b3f530 | 2022-02-07 01:04:47 +0000 | [diff] [blame] | 82 | let rs_api = format!( |
Lukasz Anforowicz | 72c4d22 | 2022-02-18 19:07:28 +0000 | [diff] [blame] | 83 | "// Automatically @generated Rust bindings for C++ target\n\ |
| 84 | // {target}\n\ |
| 85 | #![rustfmt::skip]\n\ |
| 86 | {code}", |
Lukasz Anforowicz | 5b3f530 | 2022-02-07 01:04:47 +0000 | [diff] [blame] | 87 | target = ir.current_target().0, |
| 88 | code = rs_tokens_to_formatted_string(generate_rs_api(&ir)?)? |
| 89 | ); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 90 | let rs_api_impl = tokens_to_string(generate_rs_api_impl(&ir)?)?; |
Marcel Hlopko | ca84ff4 | 2021-12-09 14:15:14 +0000 | [diff] [blame] | 91 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 92 | Ok(Bindings { rs_api, rs_api_impl }) |
| 93 | } |
| 94 | |
Devin Jeanpierre | 6d5e7cc | 2021-10-21 12:56:07 +0000 | [diff] [blame] | 95 | /// Rust source code with attached information about how to modify the parent |
| 96 | /// crate. |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 97 | /// |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 98 | /// For example, the snippet `vec![].into_raw_parts()` is not valid unless the |
| 99 | /// `vec_into_raw_parts` feature is enabled. So such a snippet should be |
| 100 | /// represented as: |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 101 | /// |
| 102 | /// ``` |
| 103 | /// RsSnippet { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 104 | /// features: btree_set![make_rs_ident("vec_into_raw_parts")], |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 105 | /// tokens: quote!{vec![].into_raw_parts()}, |
| 106 | /// } |
| 107 | /// ``` |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 108 | #[derive(Clone, Debug)] |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 109 | struct RsSnippet { |
| 110 | /// Rust feature flags used by this snippet. |
| 111 | features: BTreeSet<Ident>, |
| 112 | /// The snippet itself, as a token stream. |
| 113 | tokens: TokenStream, |
| 114 | } |
| 115 | |
| 116 | impl From<TokenStream> for RsSnippet { |
| 117 | fn from(tokens: TokenStream) -> Self { |
| 118 | RsSnippet { features: BTreeSet::new(), tokens } |
| 119 | } |
| 120 | } |
| 121 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 122 | /// If we know the original C++ function is codegenned and already compatible |
| 123 | /// with `extern "C"` calling convention we skip creating/calling the C++ thunk |
| 124 | /// since we can call the original C++ directly. |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 125 | fn can_skip_cc_thunk(func: &Func) -> bool { |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 126 | // ## Inline functions |
| 127 | // |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 128 | // Inline functions may not be codegenned in the C++ library since Clang doesn't |
| 129 | // know if Rust calls the function or not. Therefore in order to make inline |
| 130 | // functions callable from Rust we need to generate a C++ file that defines |
| 131 | // a thunk that delegates to the original inline function. When compiled, |
| 132 | // Clang will emit code for this thunk and Rust code will call the |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 133 | // thunk when the user wants to call the original inline function. |
| 134 | // |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 135 | // This is not great runtime-performance-wise in regular builds (inline function |
| 136 | // will not be inlined, there will always be a function call), but it is |
| 137 | // correct. ThinLTO builds will be able to see through the thunk and inline |
| 138 | // code across the language boundary. For non-ThinLTO builds we plan to |
| 139 | // implement <internal link> which removes the runtime performance overhead. |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 140 | if func.is_inline { |
| 141 | return false; |
| 142 | } |
| 143 | // ## Virtual functions |
| 144 | // |
| 145 | // When calling virtual `A::Method()`, it's not necessarily the case that we'll |
| 146 | // specifically call the concrete `A::Method` impl. For example, if this is |
| 147 | // called on something whose dynamic type is some subclass `B` with an |
| 148 | // overridden `B::Method`, then we'll call that. |
| 149 | // |
| 150 | // We must reuse the C++ dynamic dispatching system. In this case, the easiest |
| 151 | // way to do it is by resorting to a C++ thunk, whose implementation will do |
| 152 | // the lookup. |
| 153 | // |
| 154 | // In terms of runtime performance, since this only occurs for virtual function |
| 155 | // calls, which are already slow, it may not be such a big deal. We can |
| 156 | // benchmark it later. :) |
| 157 | if let Some(meta) = &func.member_func_metadata { |
| 158 | if let Some(inst_meta) = &meta.instance_method_metadata { |
| 159 | if inst_meta.is_virtual { |
| 160 | return false; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | true |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 168 | /// Uniquely identifies a generated Rust function. |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 169 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 170 | struct FunctionId { |
| 171 | // If the function is on a trait impl, contains the name of the Self type for |
| 172 | // which the trait is being implemented. |
| 173 | self_type: Option<syn::Path>, |
| 174 | // Fully qualified path of the function. For functions in impl blocks, this |
| 175 | // includes the name of the type or trait on which the function is being |
| 176 | // implemented, e.g. `Default::default`. |
| 177 | function_path: syn::Path, |
| 178 | } |
| 179 | |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 180 | /// Returns the name of `func` in C++ syntax. |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 181 | fn cxx_function_name(func: &Func, ir: &IR) -> Result<String> { |
| 182 | let record: Option<&str> = func |
| 183 | .member_func_metadata |
| 184 | .as_ref() |
| 185 | .map(|meta| meta.find_record(ir)) |
| 186 | .transpose()? |
| 187 | .map(|r| &*r.identifier.identifier); |
| 188 | |
| 189 | let func_name = match &func.name { |
| 190 | UnqualifiedIdentifier::Identifier(id) => id.identifier.clone(), |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 191 | UnqualifiedIdentifier::Operator(op) => op.cc_name(), |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 192 | UnqualifiedIdentifier::Destructor => { |
| 193 | format!("~{}", record.expect("destructor must be associated with a record")) |
| 194 | } |
| 195 | UnqualifiedIdentifier::Constructor => { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 196 | record.expect("constructor must be associated with a record").to_string() |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 197 | } |
| 198 | }; |
| 199 | |
| 200 | if let Some(record_name) = record { |
| 201 | Ok(format!("{}::{}", record_name, func_name)) |
| 202 | } else { |
| 203 | Ok(func_name) |
| 204 | } |
| 205 | } |
| 206 | |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 207 | fn make_unsupported_fn(func: &Func, ir: &IR, message: impl ToString) -> Result<UnsupportedItem> { |
| 208 | Ok(UnsupportedItem { |
| 209 | name: cxx_function_name(func, ir)?, |
| 210 | message: message.to_string(), |
| 211 | source_loc: func.source_loc.clone(), |
| 212 | }) |
| 213 | } |
| 214 | |
| 215 | #[derive(Clone, Debug)] |
| 216 | enum GeneratedFunc { |
| 217 | None, // No explicit function needed (e.g. when deriving Drop). |
| 218 | Unsupported(UnsupportedItem), |
| 219 | Some { api_func: RsSnippet, thunk: RsSnippet, function_id: FunctionId }, |
| 220 | } |
| 221 | |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 222 | /// Generates Rust source code for a given `Func`. |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 223 | fn generate_func(func: &Func, ir: &IR) -> Result<GeneratedFunc> { |
| 224 | let make_unsupported_result = |msg: &str| -> Result<GeneratedFunc> { |
| 225 | Ok(GeneratedFunc::Unsupported(make_unsupported_fn(func, ir, msg)?)) |
| 226 | }; |
| 227 | |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 228 | let mangled_name = &func.mangled_name; |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 229 | let thunk_ident = thunk_ident(func); |
Michael Forster | 409d941 | 2021-10-07 08:35:29 +0000 | [diff] [blame] | 230 | let doc_comment = generate_doc_comment(&func.doc_comment); |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 231 | let lifetime_to_name = HashMap::<LifetimeId, String>::from_iter( |
| 232 | func.lifetime_params.iter().map(|l| (l.id, l.name.clone())), |
| 233 | ); |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 234 | let return_type_fragment = RsTypeKind::new(&func.return_type.rs_type, ir) |
| 235 | .and_then(|t| t.format_as_return_type_fragment(ir, &lifetime_to_name)) |
Googler | b7e361d | 2022-01-04 14:02:59 +0000 | [diff] [blame] | 236 | .with_context(|| format!("Failed to format return type for {:?}", func))?; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 237 | |
| 238 | let param_idents = |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 239 | func.params.iter().map(|p| make_rs_ident(&p.identifier.identifier)).collect_vec(); |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 240 | |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 241 | let param_type_kinds = func |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 242 | .params |
| 243 | .iter() |
Googler | b7e361d | 2022-01-04 14:02:59 +0000 | [diff] [blame] | 244 | .map(|p| { |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 245 | RsTypeKind::new(&p.type_.rs_type, ir).with_context(|| { |
| 246 | format!("Failed to process type of parameter {:?} on {:?}", p, func) |
Googler | b7e361d | 2022-01-04 14:02:59 +0000 | [diff] [blame] | 247 | }) |
| 248 | }) |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 249 | .collect::<Result<Vec<_>>>()?; |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 250 | let param_types = param_type_kinds |
| 251 | .iter() |
| 252 | .map(|t| { |
| 253 | t.format(ir, &lifetime_to_name) |
| 254 | .with_context(|| format!("Failed to format parameter type {:?} on {:?}", t, func)) |
| 255 | }) |
| 256 | .collect::<Result<Vec<_>>>()?; |
| 257 | let is_unsafe = param_type_kinds.iter().any(|p| matches!(p, RsTypeKind::Pointer { .. })); |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 258 | |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 259 | let maybe_record: Option<&Record> = |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 260 | func.member_func_metadata.as_ref().map(|meta| meta.find_record(ir)).transpose()?; |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 261 | let maybe_record_name = maybe_record.map(|r| make_rs_ident(&r.identifier.identifier)); |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 262 | |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 263 | // Find 1) the `func_name` and `impl_kind` of the API function to generate |
| 264 | // and 2) whether to `format_first_param_as_self` (`&self` or `&mut self`). |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 265 | enum ImplKind { |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 266 | None, // No `impl` needed |
| 267 | Struct, // e.g. `impl SomeStruct { ... }` (SomeStruct based on func.member_func_metadata) |
| 268 | Trait { |
| 269 | trait_name: TokenStream, // e.g. quote!{ From<int> } |
| 270 | record_name: Ident, /* e.g. SomeStruct (might *not* be from |
| 271 | * func.member_func_metadata) */ |
| 272 | }, |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 273 | } |
| 274 | let impl_kind: ImplKind; |
| 275 | let func_name: syn::Ident; |
| 276 | let format_first_param_as_self: bool; |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 277 | match &func.name { |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 278 | UnqualifiedIdentifier::Operator(op) if op.name == "==" => { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 279 | if param_type_kinds.len() != 2 { |
| 280 | bail!("Unexpected number of parameters in operator==: {:?}", func); |
| 281 | } |
| 282 | match (¶m_type_kinds[0], ¶m_type_kinds[1]) { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 283 | ( |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 284 | RsTypeKind::Reference { referent: lhs, mutability: Mutability::Const, .. }, |
| 285 | RsTypeKind::Reference { referent: rhs, mutability: Mutability::Const, .. }, |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 286 | ) => match **lhs { |
| 287 | RsTypeKind::Record(lhs_record) => { |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 288 | let lhs: Ident = make_rs_ident(&lhs_record.identifier.identifier); |
| 289 | let rhs: TokenStream = rhs.format(ir, &lifetime_to_name)?; |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 290 | format_first_param_as_self = true; |
| 291 | func_name = make_rs_ident("eq"); |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 292 | impl_kind = ImplKind::Trait { |
| 293 | trait_name: quote! {PartialEq<#rhs>}, |
| 294 | record_name: lhs, |
| 295 | }; |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 296 | } |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 297 | _ => { |
| 298 | return make_unsupported_result( |
| 299 | "operator== where lhs doesn't refer to a record", |
| 300 | ); |
| 301 | } |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 302 | }, |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 303 | _ => { |
| 304 | return make_unsupported_result( |
| 305 | "operator== where operands are not const references", |
| 306 | ); |
| 307 | } |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 308 | }; |
| 309 | } |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 310 | UnqualifiedIdentifier::Operator(_) => { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 311 | return make_unsupported_result("Bindings for this kind of operator are not supported"); |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 312 | } |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 313 | UnqualifiedIdentifier::Identifier(id) => { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 314 | func_name = make_rs_ident(&id.identifier); |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 315 | match maybe_record { |
| 316 | None => { |
| 317 | impl_kind = ImplKind::None; |
| 318 | format_first_param_as_self = false; |
| 319 | } |
| 320 | Some(record) => { |
| 321 | impl_kind = ImplKind::Struct; |
| 322 | if func.is_instance_method() { |
| 323 | let first_param = param_type_kinds.first().ok_or_else(|| { |
| 324 | anyhow!("Missing `__this` parameter in an instance method: {:?}", func) |
| 325 | })?; |
| 326 | format_first_param_as_self = first_param.is_ref_to(record) |
| 327 | } else { |
| 328 | format_first_param_as_self = false; |
| 329 | } |
| 330 | } |
| 331 | }; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 332 | } |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 333 | UnqualifiedIdentifier::Destructor => { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 334 | // Note: to avoid double-destruction of the fields, they are all wrapped in |
| 335 | // ManuallyDrop in this case. See `generate_record`. |
| 336 | let record = |
| 337 | maybe_record.ok_or_else(|| anyhow!("Destructors must be member functions."))?; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 338 | if !should_implement_drop(record) { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 339 | return Ok(GeneratedFunc::None); |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 340 | } |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 341 | let record_name = maybe_record_name |
| 342 | .clone() |
| 343 | .ok_or_else(|| anyhow!("Destructors must be member functions."))?; |
| 344 | impl_kind = ImplKind::Trait { trait_name: quote! {Drop}, record_name }; |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 345 | func_name = make_rs_ident("drop"); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 346 | format_first_param_as_self = true; |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 347 | } |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 348 | UnqualifiedIdentifier::Constructor => { |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 349 | let member_func_metadata = func |
| 350 | .member_func_metadata |
| 351 | .as_ref() |
| 352 | .ok_or_else(|| anyhow!("Constructors must be member functions."))?; |
| 353 | let record = maybe_record |
| 354 | .ok_or_else(|| anyhow!("Constructors must be associated with a record."))?; |
| 355 | let instance_method_metadata = |
| 356 | member_func_metadata |
| 357 | .instance_method_metadata |
| 358 | .as_ref() |
| 359 | .ok_or_else(|| anyhow!("Constructors must be instance methods."))?; |
| 360 | |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 361 | if !record.is_unpin() { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 362 | // TODO: Handle <internal link> |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 363 | return make_unsupported_result( |
| 364 | "Bindings for constructors of non-trivial types are not supported yet", |
| 365 | ); |
Lukasz Anforowicz | 9bab835 | 2021-12-22 17:35:31 +0000 | [diff] [blame] | 366 | } |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 367 | if is_unsafe { |
| 368 | // TODO(b/216648347): Allow this outside of traits (e.g. after supporting |
| 369 | // translating C++ constructors into static methods in Rust). |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 370 | return make_unsupported_result( |
| 371 | "Unsafe constructors (e.g. with no elided or explicit lifetimes) \ |
| 372 | are intentionally not supported", |
| 373 | ); |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 376 | let record_name = maybe_record_name |
| 377 | .clone() |
| 378 | .ok_or_else(|| anyhow!("Constructors must be member functions."))?; |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 379 | match func.params.len() { |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 380 | 0 => bail!("Missing `__this` parameter in a constructor: {:?}", func), |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 381 | 1 => { |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 382 | impl_kind = ImplKind::Trait { trait_name: quote! {Default}, record_name }; |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 383 | func_name = make_rs_ident("default"); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 384 | format_first_param_as_self = false; |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 385 | } |
Lukasz Anforowicz | 73326af | 2022-01-05 01:13:10 +0000 | [diff] [blame] | 386 | 2 => { |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 387 | // TODO(lukasza): Do something smart with move constructor. |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 388 | if param_type_kinds[1].is_shared_ref_to(record) { |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 389 | // Copy constructor |
| 390 | if should_derive_clone(record) { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 391 | return Ok(GeneratedFunc::None); |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 392 | } else { |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 393 | impl_kind = ImplKind::Trait { trait_name: quote! {Clone}, record_name }; |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 394 | func_name = make_rs_ident("clone"); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 395 | format_first_param_as_self = true; |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 396 | } |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 397 | } else if !instance_method_metadata.is_explicit_ctor { |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 398 | let param_type = ¶m_types[1]; |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 399 | impl_kind = ImplKind::Trait { |
| 400 | trait_name: quote! {From< #param_type >}, |
| 401 | record_name, |
| 402 | }; |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 403 | func_name = make_rs_ident("from"); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 404 | format_first_param_as_self = false; |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 405 | } else { |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 406 | return make_unsupported_result( |
| 407 | "Not yet supported type of constructor parameter", |
| 408 | ); |
Lukasz Anforowicz | 73326af | 2022-01-05 01:13:10 +0000 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | _ => { |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 412 | // TODO(b/216648347): Support bindings for other constructors. |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 413 | return make_unsupported_result( |
| 414 | "More than 1 constructor parameter is not supported yet", |
| 415 | ); |
Lukasz Anforowicz | 73326af | 2022-01-05 01:13:10 +0000 | [diff] [blame] | 416 | } |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | let api_func_def = { |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 422 | // Clone params, return type, etc - we may need to mutate them in the |
| 423 | // API func, but we want to retain the originals for the thunk. |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 424 | let mut return_type_fragment = return_type_fragment.clone(); |
| 425 | let mut thunk_args = param_idents.iter().map(|id| quote! { #id}).collect_vec(); |
| 426 | let mut api_params = param_idents |
| 427 | .iter() |
| 428 | .zip(param_types.iter()) |
| 429 | .map(|(ident, type_)| quote! { #ident : #type_ }) |
| 430 | .collect_vec(); |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 431 | let mut lifetimes = func.lifetime_params.iter().collect_vec(); |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 432 | let mut maybe_first_api_param = param_type_kinds.get(0); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 433 | |
| 434 | if func.name == UnqualifiedIdentifier::Constructor { |
| 435 | return_type_fragment = quote! { -> Self }; |
| 436 | |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 437 | // Drop `__this` parameter from the public Rust API. Presence of |
| 438 | // element #0 is indirectly verified by a `Constructor`-related |
| 439 | // `match` branch a little bit above. |
| 440 | api_params.remove(0); |
| 441 | thunk_args.remove(0); |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 442 | |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 443 | // Remove the lifetime associated with `__this`. |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 444 | ensure!(func.return_type.rs_type.is_unit_type(), |
| 445 | "Unexpectedly non-void return type of a constructor: {:?}", func); |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 446 | let maybe_first_lifetime = func.params[0].type_.rs_type.lifetime_args.first(); |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 447 | let no_longer_needed_lifetime_id = maybe_first_lifetime |
| 448 | .ok_or_else(|| anyhow!("Missing lifetime on `__this` parameter: {:?}", func))?; |
| 449 | lifetimes.retain(|l| l.id != *no_longer_needed_lifetime_id); |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 450 | if let Some(type_still_dependent_on_removed_lifetime) = param_type_kinds |
| 451 | .iter() |
| 452 | .skip(1) // Skipping `__this` |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 453 | .flat_map(|t| t.lifetimes()) |
| 454 | .find(|lifetime_id| *lifetime_id == *no_longer_needed_lifetime_id) |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 455 | { |
| 456 | bail!( |
| 457 | "The lifetime of `__this` is unexpectedly also used by another \ |
| 458 | parameter {:?} in function {:?}", |
| 459 | type_still_dependent_on_removed_lifetime, |
| 460 | func.name |
| 461 | ); |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | // Rebind `maybe_first_api_param` to the next param after `__this`. |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 465 | maybe_first_api_param = param_type_kinds.get(1); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // Change `__this: &'a SomeStruct` into `&'a self` if needed. |
| 469 | if format_first_param_as_self { |
| 470 | let first_api_param = maybe_first_api_param |
| 471 | .ok_or_else(|| anyhow!("No parameter to format as 'self': {:?}", func))?; |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 472 | let self_decl = |
| 473 | first_api_param.format_as_self_param(func, ir, &lifetime_to_name).with_context( |
| 474 | || format!("Failed to format as `self` param: {:?}", first_api_param), |
| 475 | )?; |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 476 | // Presence of element #0 is verified by `ok_or_else` on |
| 477 | // `maybe_first_api_param` above. |
| 478 | api_params[0] = self_decl; |
| 479 | thunk_args[0] = quote! { self }; |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | let func_body = match &func.name { |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 483 | UnqualifiedIdentifier::Identifier(_) | UnqualifiedIdentifier::Operator(_) => { |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 484 | let mut body = quote! { crate::detail::#thunk_ident( #( #thunk_args ),* ) }; |
| 485 | // Only need to wrap everything in an `unsafe { ... }` block if |
| 486 | // the *whole* api function is safe. |
| 487 | if !is_unsafe { |
| 488 | body = quote! { unsafe { #body } }; |
| 489 | } |
| 490 | body |
| 491 | } |
| 492 | UnqualifiedIdentifier::Destructor => { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 493 | quote! { unsafe { crate::detail::#thunk_ident( #( #thunk_args ),* ) } } |
| 494 | } |
| 495 | UnqualifiedIdentifier::Constructor => { |
| 496 | // SAFETY: A user-defined constructor is not guaranteed to |
| 497 | // initialize all the fields. To make the `assume_init()` call |
| 498 | // below safe, the memory is zero-initialized first. This is a |
| 499 | // bit safer, because zero-initialized memory represents a valid |
| 500 | // value for the currently supported field types (this may |
| 501 | // change once the bindings generator starts supporting |
| 502 | // reference fields). TODO(b/213243309): Double-check if |
| 503 | // zero-initialization is desirable here. |
| 504 | quote! { |
| 505 | let mut tmp = std::mem::MaybeUninit::<Self>::zeroed(); |
| 506 | unsafe { |
| 507 | crate::detail::#thunk_ident( &mut tmp #( , #thunk_args )* ); |
| 508 | tmp.assume_init() |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 509 | } |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 510 | } |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 511 | } |
| 512 | }; |
| 513 | |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 514 | let (pub_, unsafe_) = match impl_kind { |
| 515 | ImplKind::None | ImplKind::Struct => ( |
| 516 | quote! { pub }, |
| 517 | if is_unsafe { |
| 518 | quote! {unsafe} |
| 519 | } else { |
| 520 | quote! {} |
| 521 | }, |
| 522 | ), |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 523 | ImplKind::Trait { .. } => { |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 524 | // Currently supported bindings have no unsafe trait functions. |
| 525 | assert!(!is_unsafe || func.name == UnqualifiedIdentifier::Destructor); |
| 526 | (quote! {}, quote! {}) |
| 527 | } |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 528 | }; |
| 529 | |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 530 | let lifetimes = lifetimes.into_iter().map(|l| format_lifetime_name(&l.name)); |
| 531 | let generic_params = format_generic_params(lifetimes); |
| 532 | |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 533 | quote! { |
| 534 | #[inline(always)] |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 535 | #pub_ #unsafe_ fn #func_name #generic_params( #( #api_params ),* ) #return_type_fragment { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 536 | #func_body |
| 537 | } |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 538 | } |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 539 | }; |
| 540 | |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 541 | let api_func: TokenStream; |
| 542 | let function_id: FunctionId; |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 543 | match impl_kind { |
| 544 | ImplKind::None => { |
| 545 | api_func = quote! { #doc_comment #api_func_def }; |
| 546 | function_id = FunctionId { self_type: None, function_path: func_name.into() }; |
| 547 | } |
| 548 | ImplKind::Struct => { |
| 549 | let record_name = |
| 550 | maybe_record_name.ok_or_else(|| anyhow!("Struct methods must have records"))?; |
| 551 | api_func = quote! { impl #record_name { #doc_comment #api_func_def } }; |
| 552 | function_id = FunctionId { |
| 553 | self_type: None, |
| 554 | function_path: syn::parse2(quote! { #record_name :: #func_name })?, |
| 555 | }; |
| 556 | } |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 557 | ImplKind::Trait { trait_name, record_name } => { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 558 | api_func = quote! { #doc_comment impl #trait_name for #record_name { #api_func_def } }; |
| 559 | function_id = FunctionId { |
| 560 | self_type: Some(record_name.into()), |
| 561 | function_path: syn::parse2(quote! { #trait_name :: #func_name })?, |
| 562 | }; |
| 563 | } |
| 564 | } |
| 565 | |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 566 | let thunk = { |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 567 | let thunk_attr = if can_skip_cc_thunk(func) { |
| 568 | quote! {#[link_name = #mangled_name]} |
| 569 | } else { |
| 570 | quote! {} |
| 571 | }; |
| 572 | |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 573 | // For constructors inject MaybeUninit into the type of `__this_` parameter. |
| 574 | let mut param_types = param_types; |
| 575 | if func.name == UnqualifiedIdentifier::Constructor { |
| 576 | if param_types.is_empty() || func.params.is_empty() { |
| 577 | bail!("Constructors should have at least one parameter (__this)"); |
| 578 | } |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 579 | param_types[0] = param_type_kinds[0] |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 580 | .format_mut_ref_as_uninitialized(ir, &lifetime_to_name) |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 581 | .with_context(|| { |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 582 | format!("Failed to format `__this` param for a constructor thunk: {:?}", func.params[0]) |
| 583 | })?; |
| 584 | } else if func.name == UnqualifiedIdentifier::Destructor { |
| 585 | if param_types.is_empty() || func.params.is_empty() { |
| 586 | bail!("Destructors should have at least one parameter (__this)"); |
| 587 | } |
| 588 | param_types[0] = param_type_kinds[0] |
| 589 | .format_ref_as_raw_ptr(ir, &lifetime_to_name) |
| 590 | .with_context(|| { |
| 591 | format!("Failed to format `__this` param for a destructor thunk: {:?}", func.params[0]) |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 592 | })?; |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 595 | let lifetimes = func.lifetime_params.iter().map(|l| format_lifetime_name(&l.name)); |
| 596 | let generic_params = format_generic_params(lifetimes); |
| 597 | |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 598 | quote! { |
| 599 | #thunk_attr |
Lukasz Anforowicz | 4ad012b | 2021-12-15 18:13:40 +0000 | [diff] [blame] | 600 | pub(crate) fn #thunk_ident #generic_params( #( #param_idents: #param_types ),* |
| 601 | ) #return_type_fragment ; |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 602 | } |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 603 | }; |
| 604 | |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 605 | Ok(GeneratedFunc::Some { api_func: api_func.into(), thunk: thunk.into(), function_id }) |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 608 | fn generate_doc_comment(comment: &Option<String>) -> TokenStream { |
| 609 | match comment { |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 610 | Some(text) => { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 611 | // token_stream_printer (and rustfmt) don't put a space between /// and the doc |
| 612 | // comment, let's add it here so our comments are pretty. |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 613 | let doc = format!(" {}", text.replace('\n', "\n ")); |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 614 | quote! {#[doc=#doc]} |
| 615 | } |
| 616 | None => quote! {}, |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 617 | } |
| 618 | } |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 619 | |
| 620 | fn format_generic_params<T: quote::ToTokens>(params: impl IntoIterator<Item = T>) -> TokenStream { |
| 621 | let mut params = params.into_iter().peekable(); |
| 622 | if params.peek().is_none() { |
| 623 | quote! {} |
| 624 | } else { |
| 625 | quote! { < #( #params ),* > } |
| 626 | } |
| 627 | } |
| 628 | |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 629 | fn should_implement_drop(record: &Record) -> bool { |
| 630 | match record.destructor.definition { |
| 631 | // TODO(b/202258760): Only omit destructor if `Copy` is specified. |
| 632 | SpecialMemberDefinition::Trivial => false, |
| 633 | |
| 634 | // TODO(b/212690698): Avoid calling into the C++ destructor (e.g. let |
| 635 | // Rust drive `drop`-ing) to avoid (somewhat unergonomic) ManuallyDrop |
| 636 | // if we can ask Rust to preserve C++ field destruction order in |
| 637 | // NontrivialMembers case. |
| 638 | SpecialMemberDefinition::NontrivialMembers => true, |
| 639 | |
| 640 | // The `impl Drop` for NontrivialUserDefined needs to call into the |
| 641 | // user-defined destructor on C++ side. |
| 642 | SpecialMemberDefinition::NontrivialUserDefined => true, |
| 643 | |
| 644 | // TODO(b/213516512): Today the IR doesn't contain Func entries for |
| 645 | // deleted functions/destructors/etc. But, maybe we should generate |
| 646 | // `impl Drop` in this case? With `unreachable!`? With |
| 647 | // `std::mem::forget`? |
| 648 | SpecialMemberDefinition::Deleted => false, |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /// Returns whether fields of type `ty` need to be wrapped in `ManuallyDrop<T>` |
| 653 | /// to prevent the fields from being destructed twice (once by the C++ |
| 654 | /// destructor calkled from the `impl Drop` of the struct and once by `drop` on |
| 655 | /// the Rust side). |
| 656 | /// |
| 657 | /// A type is safe to destroy twice if it implements `Copy`. Fields of such |
| 658 | /// don't need to be wrapped in `ManuallyDrop<T>` even if the struct |
| 659 | /// containing the fields provides an `impl Drop` that calles into a C++ |
| 660 | /// destructor (in addition to dropping the fields on the Rust side). |
| 661 | /// |
| 662 | /// Note that it is not enough to just be `!needs_drop<T>()`: Rust only |
| 663 | /// guarantees that it is safe to use-after-destroy for `Copy` types. See |
| 664 | /// e.g. the documentation for |
| 665 | /// [`drop_in_place`](https://doc.rust-lang.org/std/ptr/fn.drop_in_place.html): |
| 666 | /// |
| 667 | /// > if `T` is not `Copy`, using the pointed-to value after calling |
| 668 | /// > `drop_in_place` can cause undefined behavior |
| 669 | fn needs_manually_drop(ty: &ir::RsType, ir: &IR) -> Result<bool> { |
| 670 | let ty_implements_copy = RsTypeKind::new(ty, ir)?.implements_copy(); |
| 671 | Ok(!ty_implements_copy) |
| 672 | } |
| 673 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 674 | /// Generates Rust source code for a given `Record` and associated assertions as |
| 675 | /// a tuple. |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 676 | fn generate_record(record: &Record, ir: &IR) -> Result<(RsSnippet, RsSnippet)> { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 677 | let ident = make_rs_ident(&record.identifier.identifier); |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 678 | let doc_comment = generate_doc_comment(&record.doc_comment); |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 679 | let field_idents = |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 680 | record.fields.iter().map(|f| make_rs_ident(&f.identifier.identifier)).collect_vec(); |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 681 | let field_doc_coments = |
| 682 | record.fields.iter().map(|f| generate_doc_comment(&f.doc_comment)).collect_vec(); |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 683 | let field_types = record |
| 684 | .fields |
| 685 | .iter() |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 686 | .enumerate() |
| 687 | .map(|(i, f)| { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 688 | // [[no_unique_address]] fields are replaced by an unaligned block of memory |
| 689 | // which fills space up to the next field. |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 690 | // See: docs/struct_layout |
| 691 | if f.is_no_unique_address { |
| 692 | let next_offset = if let Some(next) = record.fields.get(i + 1) { |
| 693 | next.offset |
| 694 | } else { |
| 695 | record.size * 8 |
| 696 | }; |
| 697 | let width = Literal::usize_unsuffixed((next_offset - f.offset) / 8); |
| 698 | return Ok(quote! {[std::mem::MaybeUninit<u8>; #width]}); |
| 699 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 700 | let mut formatted = format_rs_type(&f.type_.rs_type, ir, &HashMap::new()) |
| 701 | .with_context(|| { |
Googler | b7e361d | 2022-01-04 14:02:59 +0000 | [diff] [blame] | 702 | format!("Failed to format type for field {:?} on record {:?}", f, record) |
| 703 | })?; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 704 | // TODO(b/212696226): Verify cases where ManuallyDrop<T> is skipped |
| 705 | // via static asserts in the generated code. |
| 706 | if should_implement_drop(record) && needs_manually_drop(&f.type_.rs_type, ir)? { |
| 707 | // TODO(b/212690698): Avoid (somewhat unergonomic) ManuallyDrop |
| 708 | // if we can ask Rust to preserve field destruction order if the |
| 709 | // destructor is the SpecialMemberDefinition::NontrivialMembers |
| 710 | // case. |
| 711 | formatted = quote! { std::mem::ManuallyDrop<#formatted> } |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 712 | }; |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 713 | Ok(formatted) |
| 714 | }) |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 715 | .collect::<Result<Vec<_>>>()?; |
Googler | ec589eb | 2021-09-17 07:45:39 +0000 | [diff] [blame] | 716 | let field_accesses = record |
| 717 | .fields |
| 718 | .iter() |
| 719 | .map(|f| { |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 720 | if f.access == AccessSpecifier::Public && !f.is_no_unique_address { |
Googler | ec589eb | 2021-09-17 07:45:39 +0000 | [diff] [blame] | 721 | quote! { pub } |
| 722 | } else { |
| 723 | quote! {} |
| 724 | } |
| 725 | }) |
| 726 | .collect_vec(); |
Googler | ec648ff | 2021-09-23 07:19:53 +0000 | [diff] [blame] | 727 | let size = record.size; |
| 728 | let alignment = record.alignment; |
Googler | aaa0a53 | 2021-10-01 09:11:27 +0000 | [diff] [blame] | 729 | let field_assertions = |
| 730 | record.fields.iter().zip(field_idents.iter()).map(|(field, field_ident)| { |
| 731 | let offset = field.offset; |
| 732 | quote! { |
| 733 | // The IR contains the offset in bits, while offset_of!() |
| 734 | // returns the offset in bytes, so we need to convert. |
Googler | 209b10a | 2021-12-06 09:11:57 +0000 | [diff] [blame] | 735 | const _: () = assert!(offset_of!(#ident, #field_ident) * 8 == #offset); |
Googler | aaa0a53 | 2021-10-01 09:11:27 +0000 | [diff] [blame] | 736 | } |
| 737 | }); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 738 | let mut record_features = BTreeSet::new(); |
| 739 | let mut assertion_features = BTreeSet::new(); |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 740 | |
| 741 | // TODO(mboehme): For the time being, we're using unstable features to |
| 742 | // be able to use offset_of!() in static assertions. This is fine for a |
| 743 | // prototype, but longer-term we want to either get those features |
| 744 | // stabilized or find an alternative. For more details, see |
| 745 | // b/200120034#comment15 |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 746 | assertion_features.insert(make_rs_ident("const_ptr_offset_from")); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 747 | |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 748 | let derives = generate_derives(record); |
Devin Jeanpierre | 9227d2c | 2021-10-06 12:26:05 +0000 | [diff] [blame] | 749 | let derives = if derives.is_empty() { |
| 750 | quote! {} |
| 751 | } else { |
| 752 | quote! {#[derive( #(#derives),* )]} |
| 753 | }; |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 754 | let unpin_impl = if record.is_unpin() { |
| 755 | quote! {} |
Devin Jeanpierre | ea700d3 | 2021-10-06 11:33:56 +0000 | [diff] [blame] | 756 | } else { |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 757 | // negative_impls are necessary for universal initialization due to Rust's |
| 758 | // coherence rules: PhantomPinned isn't enough to prove to Rust that a |
| 759 | // blanket impl that requires Unpin doesn't apply. See http://<internal link>=h.f6jp8ifzgt3n |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 760 | record_features.insert(make_rs_ident("negative_impls")); |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 761 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 762 | __NEWLINE__ __NEWLINE__ |
| 763 | impl !Unpin for #ident {} |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 764 | } |
| 765 | }; |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 766 | |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 767 | let mut repr_attributes = vec![quote! {C}]; |
| 768 | if record.override_alignment && record.alignment > 1 { |
| 769 | let alignment = Literal::usize_unsuffixed(record.alignment); |
| 770 | repr_attributes.push(quote! {align(#alignment)}); |
| 771 | } |
| 772 | |
| 773 | // Adjust the struct to also include base class subobjects. We use an opaque |
| 774 | // field because subobjects can live in the alignment of base class |
| 775 | // subobjects. |
| 776 | let base_subobjects_field = if let Some(base_size) = record.base_size { |
| 777 | let n = proc_macro2::Literal::usize_unsuffixed(base_size); |
| 778 | quote! { |
| 779 | __base_class_subobjects: [std::mem::MaybeUninit<u8>; #n], |
| 780 | } |
| 781 | } else { |
| 782 | quote! {} |
| 783 | }; |
| 784 | |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 785 | let empty_struct_placeholder_field = |
| 786 | if record.fields.is_empty() && record.base_size.unwrap_or(0) == 0 { |
| 787 | quote! { |
| 788 | /// Prevent empty C++ struct being zero-size in Rust. |
| 789 | placeholder: std::mem::MaybeUninit<u8>, |
| 790 | } |
| 791 | } else { |
| 792 | quote! {} |
| 793 | }; |
Googler | f479206 | 2021-10-20 07:21:21 +0000 | [diff] [blame] | 794 | |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 795 | let no_unique_address_accessors = cc_struct_no_unique_address_impl(record, ir)?; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 796 | let base_class_into = cc_struct_upcast_impl(record, ir)?; |
| 797 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 798 | let record_tokens = quote! { |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 799 | #doc_comment |
Devin Jeanpierre | 9227d2c | 2021-10-06 12:26:05 +0000 | [diff] [blame] | 800 | #derives |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 801 | #[repr(#( #repr_attributes ),*)] |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 802 | pub struct #ident { |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 803 | #base_subobjects_field |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 804 | #( #field_doc_coments #field_accesses #field_idents: #field_types, )* |
Googler | f479206 | 2021-10-20 07:21:21 +0000 | [diff] [blame] | 805 | #empty_struct_placeholder_field |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 806 | } |
Googler | ec648ff | 2021-09-23 07:19:53 +0000 | [diff] [blame] | 807 | |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 808 | #no_unique_address_accessors |
| 809 | |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 810 | #base_class_into |
| 811 | |
Devin Jeanpierre | ea700d3 | 2021-10-06 11:33:56 +0000 | [diff] [blame] | 812 | #unpin_impl |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 813 | }; |
| 814 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 815 | let assertion_tokens = quote! { |
Googler | 209b10a | 2021-12-06 09:11:57 +0000 | [diff] [blame] | 816 | const _: () = assert!(std::mem::size_of::<#ident>() == #size); |
| 817 | const _: () = assert!(std::mem::align_of::<#ident>() == #alignment); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 818 | #( #field_assertions )* |
| 819 | }; |
| 820 | |
| 821 | Ok(( |
| 822 | RsSnippet { features: record_features, tokens: record_tokens }, |
| 823 | RsSnippet { features: assertion_features, tokens: assertion_tokens }, |
| 824 | )) |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 825 | } |
| 826 | |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 827 | fn should_derive_clone(record: &Record) -> bool { |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 828 | record.is_unpin() |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 829 | && record.copy_constructor.access == ir::AccessSpecifier::Public |
| 830 | && record.copy_constructor.definition == SpecialMemberDefinition::Trivial |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 833 | fn should_derive_copy(record: &Record) -> bool { |
| 834 | // TODO(b/202258760): Make `Copy` inclusion configurable. |
| 835 | should_derive_clone(record) |
| 836 | } |
| 837 | |
| 838 | fn generate_derives(record: &Record) -> Vec<Ident> { |
| 839 | let mut derives = vec![]; |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 840 | if should_derive_clone(record) { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 841 | derives.push(make_rs_ident("Clone")); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 842 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 843 | if should_derive_copy(record) { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 844 | derives.push(make_rs_ident("Copy")); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 845 | } |
| 846 | derives |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 849 | fn generate_enum(enum_: &Enum, ir: &IR) -> Result<TokenStream> { |
| 850 | let name = make_rs_ident(&enum_.identifier.identifier); |
| 851 | let underlying_type = format_rs_type(&enum_.underlying_type.rs_type, ir, &HashMap::new())?; |
| 852 | let enumerator_names = |
| 853 | enum_.enumerators.iter().map(|enumerator| make_rs_ident(&enumerator.identifier.identifier)); |
| 854 | let enumerator_values = enum_.enumerators.iter().map(|enumerator| enumerator.value); |
| 855 | Ok(quote! { |
| 856 | #[repr(transparent)] |
| 857 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 858 | pub struct #name(#underlying_type); |
| 859 | impl #name { |
| 860 | #(pub const #enumerator_names: #name = #name(#enumerator_values);)* |
| 861 | } |
| 862 | impl From<#underlying_type> for #name { |
| 863 | fn from(value: #underlying_type) -> #name { |
| 864 | #name(v) |
| 865 | } |
| 866 | } |
| 867 | impl From<#name> for #underlying_type { |
| 868 | fn from(value: #name) -> #underlying_type { |
| 869 | v.0 |
| 870 | } |
| 871 | } |
| 872 | }) |
| 873 | } |
| 874 | |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 875 | fn generate_type_alias(type_alias: &TypeAlias, ir: &IR) -> Result<TokenStream> { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 876 | let ident = make_rs_ident(&type_alias.identifier.identifier); |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 877 | let underlying_type = format_rs_type(&type_alias.underlying_type.rs_type, ir, &HashMap::new()) |
| 878 | .with_context(|| format!("Failed to format underlying type for {:?}", type_alias))?; |
| 879 | Ok(quote! {pub type #ident = #underlying_type;}) |
| 880 | } |
| 881 | |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 882 | /// Generates Rust source code for a given `UnsupportedItem`. |
| 883 | fn generate_unsupported(item: &UnsupportedItem) -> Result<TokenStream> { |
Googler | 48a74dd | 2021-10-25 07:31:53 +0000 | [diff] [blame] | 884 | let location = if item.source_loc.filename.is_empty() { |
| 885 | "<unknown location>".to_string() |
| 886 | } else { |
| 887 | // TODO(forster): The "google3" prefix should probably come from a command line |
| 888 | // argument. |
| 889 | // TODO(forster): Consider linking to the symbol instead of to the line number |
| 890 | // to avoid wrong links while generated files have not caught up. |
| 891 | format!("google3/{};l={}", &item.source_loc.filename, &item.source_loc.line) |
| 892 | }; |
Michael Forster | 6a184ad | 2021-10-12 13:04:05 +0000 | [diff] [blame] | 893 | let message = format!( |
Googler | 48a74dd | 2021-10-25 07:31:53 +0000 | [diff] [blame] | 894 | "{}\nError while generating bindings for item '{}':\n{}", |
| 895 | &location, &item.name, &item.message |
Michael Forster | 6a184ad | 2021-10-12 13:04:05 +0000 | [diff] [blame] | 896 | ); |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 897 | Ok(quote! { __COMMENT__ #message }) |
| 898 | } |
| 899 | |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 900 | /// Generates Rust source code for a given `Comment`. |
| 901 | fn generate_comment(comment: &Comment) -> Result<TokenStream> { |
| 902 | let text = &comment.text; |
| 903 | Ok(quote! { __COMMENT__ #text }) |
| 904 | } |
| 905 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 906 | fn generate_rs_api(ir: &IR) -> Result<TokenStream> { |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 907 | let mut items = vec![]; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 908 | let mut thunks = vec![]; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 909 | let mut assertions = vec![]; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 910 | |
Googler | 454f265 | 2021-12-06 12:53:12 +0000 | [diff] [blame] | 911 | // We import nullable pointers as an Option<&T> and assume that at the ABI |
| 912 | // level, None is represented as a zero pointer value whereas Some is |
| 913 | // represented as as non-zero pointer value. This seems like a pretty safe |
| 914 | // assumption to make, but to provide some safeguard, assert that |
| 915 | // `Option<&i32>` and `&i32` have the same size. |
| 916 | assertions.push(quote! { |
| 917 | const _: () = assert!(std::mem::size_of::<Option<&i32>>() == std::mem::size_of::<&i32>()); |
| 918 | }); |
| 919 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 920 | // TODO(jeanpierreda): Delete has_record, either in favor of using RsSnippet, or not |
| 921 | // having uses. See https://chat.google.com/room/AAAAnQmj8Qs/6QbkSvWcfhA |
Devin Jeanpierre | ea700d3 | 2021-10-06 11:33:56 +0000 | [diff] [blame] | 922 | let mut has_record = false; |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 923 | let mut features = BTreeSet::new(); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 924 | |
Lukasz Anforowicz | 72c4d22 | 2022-02-18 19:07:28 +0000 | [diff] [blame] | 925 | // For #![rustfmt::skip]. |
| 926 | features.insert(make_rs_ident("custom_inner_attributes")); |
| 927 | |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 928 | // Identify all functions having overloads that we can't import (yet). |
| 929 | // TODO(b/213280424): Implement support for overloaded functions. |
| 930 | let mut seen_funcs = HashSet::new(); |
| 931 | let mut overloaded_funcs = HashSet::new(); |
| 932 | for func in ir.functions() { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 933 | if let GeneratedFunc::Some { function_id, .. } = generate_func(func, ir)? { |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 934 | if !seen_funcs.insert(function_id.clone()) { |
| 935 | overloaded_funcs.insert(function_id); |
| 936 | } |
| 937 | } |
| 938 | } |
| 939 | |
Marcel Hlopko | 3b9bf9e | 2021-11-29 08:25:14 +0000 | [diff] [blame] | 940 | for item in ir.items() { |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 941 | match item { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 942 | Item::Func(func) => match generate_func(func, ir)? { |
| 943 | GeneratedFunc::None => (), |
| 944 | GeneratedFunc::Unsupported(unsupported) => { |
| 945 | items.push(generate_unsupported(&unsupported)?) |
| 946 | } |
| 947 | GeneratedFunc::Some { api_func, thunk, function_id } => { |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 948 | if overloaded_funcs.contains(&function_id) { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 949 | items.push(generate_unsupported(&make_unsupported_fn( |
| 950 | func, |
| 951 | ir, |
| 952 | "Cannot generate bindings for overloaded function", |
| 953 | )?)?); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 954 | continue; |
| 955 | } |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 956 | features.extend(api_func.features); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 957 | features.extend(thunk.features); |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 958 | items.push(api_func.tokens); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 959 | thunks.push(thunk.tokens); |
| 960 | } |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 961 | }, |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 962 | Item::Record(record) => { |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 963 | if !ir.is_current_target(&record.owning_target) |
| 964 | && !ir.is_stdlib_target(&record.owning_target) |
| 965 | { |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 966 | continue; |
| 967 | } |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 968 | let (snippet, assertions_snippet) = generate_record(record, ir)?; |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 969 | features.extend(snippet.features); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 970 | features.extend(assertions_snippet.features); |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 971 | items.push(snippet.tokens); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 972 | assertions.push(assertions_snippet.tokens); |
Devin Jeanpierre | ea700d3 | 2021-10-06 11:33:56 +0000 | [diff] [blame] | 973 | has_record = true; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 974 | } |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 975 | Item::Enum(enum_) => { |
| 976 | if !ir.is_current_target(&enum_.owning_target) |
| 977 | && !ir.is_stdlib_target(&enum_.owning_target) |
| 978 | { |
| 979 | continue; |
| 980 | } |
| 981 | items.push(generate_enum(enum_, ir)?); |
| 982 | continue; |
| 983 | } |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 984 | Item::TypeAlias(type_alias) => { |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 985 | if !ir.is_current_target(&type_alias.owning_target) |
| 986 | && !ir.is_stdlib_target(&type_alias.owning_target) |
| 987 | { |
| 988 | continue; |
| 989 | } |
| 990 | items.push(generate_type_alias(type_alias, ir)?); |
Googler | 098c458 | 2022-01-10 12:29:34 +0000 | [diff] [blame] | 991 | } |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 992 | Item::UnsupportedItem(unsupported) => items.push(generate_unsupported(unsupported)?), |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 993 | Item::Comment(comment) => items.push(generate_comment(comment)?), |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 994 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 997 | let mod_detail = if thunks.is_empty() { |
| 998 | quote! {} |
| 999 | } else { |
| 1000 | quote! { |
| 1001 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 1002 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 1003 | use super::*; |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 1004 | extern "C" { |
| 1005 | #( #thunks )* |
| 1006 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | }; |
| 1010 | |
Devin Jeanpierre | ea700d3 | 2021-10-06 11:33:56 +0000 | [diff] [blame] | 1011 | let imports = if has_record { |
Googler | ec648ff | 2021-09-23 07:19:53 +0000 | [diff] [blame] | 1012 | quote! { |
Googler | aaa0a53 | 2021-10-01 09:11:27 +0000 | [diff] [blame] | 1013 | use memoffset_unstable_const::offset_of; |
Googler | ec648ff | 2021-09-23 07:19:53 +0000 | [diff] [blame] | 1014 | } |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 1015 | } else { |
| 1016 | quote! {} |
Googler | ec648ff | 2021-09-23 07:19:53 +0000 | [diff] [blame] | 1017 | }; |
| 1018 | |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 1019 | let features = if features.is_empty() { |
| 1020 | quote! {} |
| 1021 | } else { |
| 1022 | quote! { |
| 1023 | #![feature( #(#features),* )] |
| 1024 | } |
| 1025 | }; |
| 1026 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1027 | Ok(quote! { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 1028 | #features __NEWLINE__ |
| 1029 | #![allow(non_camel_case_types)] __NEWLINE__ |
| 1030 | #![allow(non_snake_case)] __NEWLINE__ __NEWLINE__ |
| 1031 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1032 | #imports __NEWLINE__ __NEWLINE__ |
Googler | ec648ff | 2021-09-23 07:19:53 +0000 | [diff] [blame] | 1033 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1034 | #( #items __NEWLINE__ __NEWLINE__ )* |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 1035 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1036 | #mod_detail __NEWLINE__ __NEWLINE__ |
| 1037 | |
| 1038 | #( #assertions __NEWLINE__ __NEWLINE__ )* |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1039 | }) |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 1042 | /// Makes an 'Ident' to be used in the Rust source code. Escapes Rust keywords. |
| 1043 | fn make_rs_ident(ident: &str) -> Ident { |
| 1044 | // TODO(https://github.com/dtolnay/syn/pull/1098): Remove the hardcoded list once syn recognizes |
| 1045 | // 2018 and 2021 keywords. |
| 1046 | if ["async", "await", "try", "dyn"].contains(&ident) { |
| 1047 | return format_ident!("r#{}", ident); |
| 1048 | } |
| 1049 | match syn::parse_str::<syn::Ident>(ident) { |
| 1050 | Ok(_) => format_ident!("{}", ident), |
| 1051 | Err(_) => format_ident!("r#{}", ident), |
| 1052 | } |
| 1053 | } |
| 1054 | |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1055 | /// Formats a C++ identifier. Does not escape C++ keywords. |
| 1056 | fn format_cc_ident(ident: &str) -> TokenStream { |
| 1057 | ident.parse().unwrap() |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 1060 | fn rs_type_name_for_target_and_identifier( |
| 1061 | owning_target: &BlazeLabel, |
| 1062 | identifier: &ir::Identifier, |
| 1063 | ir: &IR, |
| 1064 | ) -> Result<TokenStream> { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 1065 | let ident = make_rs_ident(identifier.identifier.as_str()); |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 1066 | |
| 1067 | if ir.is_current_target(owning_target) || ir.is_stdlib_target(owning_target) { |
| 1068 | Ok(quote! {#ident}) |
| 1069 | } else { |
Marcel Hlopko | d906b89 | 2022-01-27 08:52:36 +0000 | [diff] [blame] | 1070 | let owning_crate_name = owning_target.target_name()?; |
| 1071 | // TODO(b/216587072): Remove this hacky escaping and use the import! macro once |
| 1072 | // available |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 1073 | let escaped_owning_crate_name = owning_crate_name.replace('-', "_"); |
Marcel Hlopko | d906b89 | 2022-01-27 08:52:36 +0000 | [diff] [blame] | 1074 | let owning_crate = make_rs_ident(&escaped_owning_crate_name); |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 1075 | Ok(quote! {#owning_crate::#ident}) |
| 1076 | } |
| 1077 | } |
| 1078 | |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1079 | #[derive(Debug, Eq, PartialEq)] |
| 1080 | enum Mutability { |
| 1081 | Const, |
| 1082 | Mut, |
| 1083 | } |
| 1084 | |
| 1085 | impl Mutability { |
| 1086 | fn format_for_pointer(&self) -> TokenStream { |
| 1087 | match self { |
| 1088 | Mutability::Mut => quote! {mut}, |
| 1089 | Mutability::Const => quote! {const}, |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | fn format_for_reference(&self) -> TokenStream { |
| 1094 | match self { |
| 1095 | Mutability::Mut => quote! {mut}, |
| 1096 | Mutability::Const => quote! {}, |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | // TODO(b/213947473): Instead of having a separate RsTypeKind here, consider |
| 1102 | // changing ir::RsType into a similar `enum`, with fields that contain |
| 1103 | // references (e.g. &'ir Record`) instead of DeclIds. |
| 1104 | #[derive(Debug)] |
| 1105 | enum RsTypeKind<'ir> { |
| 1106 | Pointer { pointee: Box<RsTypeKind<'ir>>, mutability: Mutability }, |
| 1107 | Reference { referent: Box<RsTypeKind<'ir>>, mutability: Mutability, lifetime_id: LifetimeId }, |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 1108 | FuncPtr { abi: &'ir str, return_type: Box<RsTypeKind<'ir>>, param_types: Vec<RsTypeKind<'ir>> }, |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1109 | Record(&'ir Record), |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1110 | TypeAlias { type_alias: &'ir TypeAlias, underlying_type: Box<RsTypeKind<'ir>> }, |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1111 | Unit, |
| 1112 | Other { name: &'ir str, type_args: Vec<RsTypeKind<'ir>> }, |
| 1113 | } |
| 1114 | |
| 1115 | impl<'ir> RsTypeKind<'ir> { |
| 1116 | pub fn new(ty: &'ir ir::RsType, ir: &'ir IR) -> Result<Self> { |
| 1117 | // The lambdas deduplicate code needed by multiple `match` branches. |
| 1118 | let get_type_args = || -> Result<Vec<RsTypeKind<'ir>>> { |
| 1119 | ty.type_args.iter().map(|type_arg| RsTypeKind::<'ir>::new(type_arg, ir)).collect() |
| 1120 | }; |
| 1121 | let get_pointee = || -> Result<Box<RsTypeKind<'ir>>> { |
| 1122 | if ty.type_args.len() != 1 { |
| 1123 | bail!("Missing pointee/referent type (need exactly 1 type argument): {:?}", ty); |
| 1124 | } |
| 1125 | Ok(Box::new(get_type_args()?.remove(0))) |
| 1126 | }; |
| 1127 | let get_lifetime = || -> Result<LifetimeId> { |
| 1128 | if ty.lifetime_args.len() != 1 { |
| 1129 | bail!("Missing reference lifetime (need exactly 1 lifetime argument): {:?}", ty); |
| 1130 | } |
| 1131 | Ok(ty.lifetime_args[0]) |
| 1132 | }; |
| 1133 | |
| 1134 | let result = match ty.name.as_deref() { |
| 1135 | None => { |
| 1136 | ensure!( |
| 1137 | ty.type_args.is_empty(), |
| 1138 | "Type arguments on records nor type aliases are not yet supported: {:?}", |
| 1139 | ty |
| 1140 | ); |
| 1141 | match ir.item_for_type(ty)? { |
| 1142 | Item::Record(record) => RsTypeKind::Record(record), |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1143 | Item::TypeAlias(type_alias) => RsTypeKind::TypeAlias { |
| 1144 | type_alias, |
| 1145 | underlying_type: Box::new(RsTypeKind::new( |
| 1146 | &type_alias.underlying_type.rs_type, |
| 1147 | ir, |
| 1148 | )?), |
| 1149 | }, |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1150 | other_item => bail!("Item does not define a type: {:?}", other_item), |
| 1151 | } |
| 1152 | } |
| 1153 | Some(name) => match name { |
| 1154 | "()" => { |
| 1155 | if !ty.type_args.is_empty() { |
| 1156 | bail!("Unit type must not have type arguments: {:?}", ty); |
| 1157 | } |
| 1158 | RsTypeKind::Unit |
| 1159 | } |
| 1160 | "*mut" => { |
| 1161 | RsTypeKind::Pointer { pointee: get_pointee()?, mutability: Mutability::Mut } |
| 1162 | } |
| 1163 | "*const" => { |
| 1164 | RsTypeKind::Pointer { pointee: get_pointee()?, mutability: Mutability::Const } |
| 1165 | } |
| 1166 | "&mut" => RsTypeKind::Reference { |
| 1167 | referent: get_pointee()?, |
| 1168 | mutability: Mutability::Mut, |
| 1169 | lifetime_id: get_lifetime()?, |
| 1170 | }, |
| 1171 | "&" => RsTypeKind::Reference { |
| 1172 | referent: get_pointee()?, |
| 1173 | mutability: Mutability::Const, |
| 1174 | lifetime_id: get_lifetime()?, |
| 1175 | }, |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 1176 | name => { |
| 1177 | let mut type_args = get_type_args()?; |
| 1178 | match name.strip_prefix("#funcPtr ") { |
| 1179 | None => RsTypeKind::Other { name, type_args }, |
| 1180 | Some(abi) => { |
| 1181 | // TODO(b/217419782): Consider enforcing `'static` lifetime. |
| 1182 | ensure!(!type_args.is_empty(), "No return type in fn type: {:?}", ty); |
| 1183 | RsTypeKind::FuncPtr { |
| 1184 | abi, |
| 1185 | return_type: Box::new(type_args.remove(type_args.len() - 1)), |
| 1186 | param_types: type_args, |
| 1187 | } |
| 1188 | }, |
| 1189 | } |
| 1190 | }, |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1191 | }, |
| 1192 | }; |
| 1193 | Ok(result) |
| 1194 | } |
| 1195 | |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 1196 | /// Returns true if the type is known to be `Unpin`, false otherwise. |
| 1197 | pub fn is_unpin(&self, ir: &IR) -> bool { |
| 1198 | match self { |
| 1199 | RsTypeKind::Record(record) => record.is_unpin(), |
| 1200 | RsTypeKind::TypeAlias { underlying_type, .. } => underlying_type.is_unpin(ir), |
| 1201 | _ => true, |
| 1202 | } |
| 1203 | } |
| 1204 | |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1205 | pub fn format( |
| 1206 | &self, |
| 1207 | ir: &IR, |
| 1208 | lifetime_to_name: &HashMap<LifetimeId, String>, |
| 1209 | ) -> Result<TokenStream> { |
| 1210 | let result = match self { |
| 1211 | RsTypeKind::Pointer { pointee, mutability } => { |
| 1212 | let mutability = mutability.format_for_pointer(); |
| 1213 | let nested_type = pointee.format(ir, lifetime_to_name)?; |
| 1214 | quote! {* #mutability #nested_type} |
| 1215 | } |
| 1216 | RsTypeKind::Reference { referent, mutability, lifetime_id } => { |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 1217 | let mut_ = mutability.format_for_reference(); |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1218 | let lifetime = Self::format_lifetime(lifetime_id, lifetime_to_name)?; |
| 1219 | let nested_type = referent.format(ir, lifetime_to_name)?; |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 1220 | let reference = quote! {& #lifetime #mut_ #nested_type}; |
| 1221 | if mutability == &Mutability::Mut && !referent.is_unpin(ir) { |
| 1222 | // TODO(b/200067242): Add a `use std::pin::Pin` to the crate, and use `Pin`. |
| 1223 | // Probably format needs to return an RsSnippet, and RsSnippet needs a `uses` |
| 1224 | // field. |
| 1225 | quote! {std::pin::Pin< #reference >} |
| 1226 | } else { |
| 1227 | reference |
| 1228 | } |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1229 | } |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 1230 | RsTypeKind::FuncPtr { abi, return_type, param_types } => { |
| 1231 | let return_frag = return_type.format_as_return_type_fragment(ir, lifetime_to_name)?; |
| 1232 | let param_types = param_types |
| 1233 | .iter() |
| 1234 | .map(|t| t.format(ir, lifetime_to_name)) |
| 1235 | .collect::<Result<Vec<_>>>()?; |
| 1236 | quote!{ extern #abi fn( #( #param_types ),* ) #return_frag } |
| 1237 | }, |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1238 | RsTypeKind::Record(record) => rs_type_name_for_target_and_identifier( |
| 1239 | &record.owning_target, |
| 1240 | &record.identifier, |
| 1241 | ir, |
| 1242 | )?, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1243 | RsTypeKind::TypeAlias { type_alias, .. } => rs_type_name_for_target_and_identifier( |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1244 | &type_alias.owning_target, |
| 1245 | &type_alias.identifier, |
| 1246 | ir, |
| 1247 | )?, |
| 1248 | RsTypeKind::Unit => quote! {()}, |
| 1249 | RsTypeKind::Other { name, type_args } => { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 1250 | let ident = make_rs_ident(name); |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1251 | let generic_params = format_generic_params( |
| 1252 | type_args |
| 1253 | .iter() |
| 1254 | .map(|type_arg| type_arg.format(ir, lifetime_to_name)) |
| 1255 | .collect::<Result<Vec<_>>>()?, |
| 1256 | ); |
| 1257 | quote! {#ident #generic_params} |
| 1258 | } |
| 1259 | }; |
| 1260 | Ok(result) |
| 1261 | } |
| 1262 | |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 1263 | pub fn format_as_return_type_fragment( |
| 1264 | &self, |
| 1265 | ir: &IR, |
| 1266 | lifetime_to_name: &HashMap<LifetimeId, String>, |
| 1267 | ) -> Result<TokenStream> { |
| 1268 | match self { |
| 1269 | RsTypeKind::Unit => Ok(quote! {}), |
| 1270 | other_type => { |
| 1271 | let return_type = other_type.format(ir, lifetime_to_name)?; |
| 1272 | Ok(quote! { -> #return_type }) |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 1277 | /// Formats this RsTypeKind as `&'a mut MaybeUninit<SomeStruct>`. This is |
| 1278 | /// used to format `__this` parameter in a constructor thunk. |
| 1279 | pub fn format_mut_ref_as_uninitialized( |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1280 | &self, |
| 1281 | ir: &IR, |
| 1282 | lifetime_to_name: &HashMap<LifetimeId, String>, |
| 1283 | ) -> Result<TokenStream> { |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 1284 | match self { |
| 1285 | RsTypeKind::Reference { referent, lifetime_id, mutability: Mutability::Mut } => { |
| 1286 | let nested_type = referent.format(ir, lifetime_to_name)?; |
| 1287 | let lifetime = Self::format_lifetime(lifetime_id, lifetime_to_name)?; |
| 1288 | Ok(quote! { & #lifetime mut std::mem::MaybeUninit< #nested_type > }) |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1289 | } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 1290 | _ => bail!("Expected reference to format as MaybeUninit, got: {:?}", self), |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 1291 | } |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 1294 | /// Formats a reference or pointer as a raw pointer. |
| 1295 | pub fn format_ref_as_raw_ptr( |
| 1296 | &self, |
| 1297 | ir: &IR, |
| 1298 | lifetime_to_name: &HashMap<LifetimeId, String>, |
| 1299 | ) -> Result<TokenStream> { |
| 1300 | match self { |
| 1301 | RsTypeKind::Reference { referent: pointee, mutability, .. } |
| 1302 | | RsTypeKind::Pointer { pointee, mutability } => { |
| 1303 | let nested_type = pointee.format(ir, lifetime_to_name)?; |
| 1304 | let mut_ = mutability.format_for_pointer(); |
| 1305 | Ok(quote! { * #mut_ #nested_type }) |
| 1306 | } |
| 1307 | _ => bail!("Expected reference to format as raw ptr, got: {:?}", self), |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | /// Formats this RsTypeKind as the `self` parameter: usually, `&'a self` or |
| 1312 | /// `&'a mut self`. |
| 1313 | /// |
| 1314 | /// If this is !Unpin, however, it uses `self: Pin<&mut Self>` instead. |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 1315 | pub fn format_as_self_param( |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 1316 | &self, |
Lukasz Anforowicz | ce34539 | 2022-01-14 22:41:16 +0000 | [diff] [blame] | 1317 | func: &Func, |
| 1318 | ir: &IR, |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 1319 | lifetime_to_name: &HashMap<LifetimeId, String>, |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 1320 | ) -> Result<TokenStream> { |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 1321 | if func.name == UnqualifiedIdentifier::Destructor { |
| 1322 | let record = func |
| 1323 | .member_func_metadata |
| 1324 | .as_ref() |
| 1325 | .ok_or_else(|| anyhow!("Destructors must be member functions: {:?}", func))? |
| 1326 | .find_record(ir)?; |
| 1327 | if self.is_mut_ptr_to(record) { |
| 1328 | // Even in C++ it is UB to retain `this` pointer and dereference it |
| 1329 | // after a destructor runs. Therefore it is safe to use `&self` or |
| 1330 | // `&mut self` in Rust even if IR represents `__this` as a Rust |
| 1331 | // pointer (e.g. when lifetime annotations are missing - lifetime |
| 1332 | // annotations are required to represent it as a Rust reference). |
| 1333 | return Ok(quote! { &mut self }); |
| 1334 | } |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | match self { |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 1338 | RsTypeKind::Reference { referent, lifetime_id, mutability } => { |
| 1339 | let mut_ = mutability.format_for_reference(); |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 1340 | let lifetime = Self::format_lifetime(lifetime_id, lifetime_to_name)?; |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 1341 | if mutability == &Mutability::Mut && !referent.is_unpin(ir) && func.name != UnqualifiedIdentifier::Destructor { |
| 1342 | // TODO(b/200067242): Add a `use std::pin::Pin` to the crate, and use `Pin`. |
| 1343 | Ok(quote! {self: std::pin::Pin< & #lifetime #mut_ Self>}) |
| 1344 | } else { |
| 1345 | Ok(quote! { & #lifetime #mut_ self }) |
| 1346 | } |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 1347 | } |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 1348 | _ => bail!("Unexpected type of `self` parameter: {:?}", self), |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1352 | fn format_lifetime( |
| 1353 | lifetime_id: &LifetimeId, |
| 1354 | lifetime_to_name: &HashMap<LifetimeId, String>, |
| 1355 | ) -> Result<TokenStream> { |
| 1356 | let lifetime_name = lifetime_to_name.get(lifetime_id).ok_or_else(|| { |
| 1357 | anyhow!("`lifetime_to_name` doesn't have an entry for {:?}", lifetime_id) |
| 1358 | })?; |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 1359 | Ok(format_lifetime_name(lifetime_name)) |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1360 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1361 | |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 1362 | /// Returns whether the type represented by `self` implements the `Copy` |
| 1363 | /// trait. |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 1364 | pub fn implements_copy(&self) -> bool { |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1365 | // TODO(b/212696226): Verify results of `implements_copy` via static |
| 1366 | // assertions in the generated Rust code (because incorrect results |
| 1367 | // can silently lead to unsafe behavior). |
| 1368 | match self { |
| 1369 | RsTypeKind::Unit => true, |
| 1370 | RsTypeKind::Pointer { .. } => true, |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 1371 | RsTypeKind::FuncPtr { .. } => true, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1372 | RsTypeKind::Reference { mutability: Mutability::Const, .. } => true, |
| 1373 | RsTypeKind::Reference { mutability: Mutability::Mut, .. } => false, |
| 1374 | RsTypeKind::Record(record) => should_derive_copy(record), |
| 1375 | RsTypeKind::TypeAlias { underlying_type, .. } => underlying_type.implements_copy(), |
Lukasz Anforowicz | d81bea9 | 2022-02-11 08:57:58 +0000 | [diff] [blame] | 1376 | RsTypeKind::Other { type_args, .. } => { |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 1377 | // All types that may appear here without `type_args` (e.g. |
| 1378 | // primitive types like `i32`) implement `Copy`. Generic types |
| 1379 | // that may be present here (e.g. Option<...>) are `Copy` if all |
| 1380 | // of their `type_args` are `Copy`. |
| 1381 | type_args.iter().all(|t| t.implements_copy()) |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | } |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 1385 | |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 1386 | pub fn is_mut_ptr_to(&self, expected_record: &Record) -> bool { |
| 1387 | match self { |
| 1388 | RsTypeKind::Pointer { pointee, mutability: Mutability::Mut, .. } => { |
| 1389 | pointee.is_record(expected_record) |
| 1390 | } |
| 1391 | _ => false, |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | pub fn is_ref_to(&self, expected_record: &Record) -> bool { |
| 1396 | match self { |
| 1397 | RsTypeKind::Reference { referent, .. } => referent.is_record(expected_record), |
| 1398 | _ => false, |
| 1399 | } |
| 1400 | } |
| 1401 | |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 1402 | pub fn is_shared_ref_to(&self, expected_record: &Record) -> bool { |
| 1403 | match self { |
| 1404 | RsTypeKind::Reference { referent, mutability: Mutability::Const, .. } => { |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 1405 | referent.is_record(expected_record) |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 1406 | } |
| 1407 | _ => false, |
| 1408 | } |
| 1409 | } |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 1410 | |
| 1411 | pub fn is_record(&self, expected_record: &Record) -> bool { |
| 1412 | match self { |
| 1413 | RsTypeKind::Record(actual_record) => actual_record.id == expected_record.id, |
| 1414 | _ => false, |
| 1415 | } |
| 1416 | } |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 1417 | |
| 1418 | /// Iterates over `self` and all the nested types (e.g. pointees, generic |
| 1419 | /// type args, etc.) in DFS order. |
| 1420 | pub fn dfs_iter<'ty>(&'ty self) -> impl Iterator<Item = &'ty RsTypeKind<'ir>> + '_ { |
| 1421 | RsTypeKindIter::new(self) |
| 1422 | } |
| 1423 | |
| 1424 | /// Iterates over all `LifetimeId`s in `self` and in all the nested types. |
| 1425 | /// Note that the results might contain duplicate LifetimeId values (e.g. |
| 1426 | /// if the same LifetimeId is used in two `type_args`). |
| 1427 | pub fn lifetimes(&self) -> impl Iterator<Item = LifetimeId> + '_ { |
| 1428 | self.dfs_iter().filter_map(|t| match t { |
| 1429 | RsTypeKind::Reference { lifetime_id, .. } => Some(*lifetime_id), |
| 1430 | _ => None, |
| 1431 | }) |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | struct RsTypeKindIter<'ty, 'ir> { |
| 1436 | todo: Vec<&'ty RsTypeKind<'ir>>, |
| 1437 | } |
| 1438 | |
| 1439 | impl<'ty, 'ir> RsTypeKindIter<'ty, 'ir> { |
| 1440 | pub fn new(ty: &'ty RsTypeKind<'ir>) -> Self { |
| 1441 | Self { todo: vec![ty] } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | impl<'ty, 'ir> Iterator for RsTypeKindIter<'ty, 'ir> { |
| 1446 | type Item = &'ty RsTypeKind<'ir>; |
| 1447 | |
| 1448 | fn next(&mut self) -> Option<Self::Item> { |
| 1449 | match self.todo.pop() { |
| 1450 | None => None, |
| 1451 | Some(curr) => { |
| 1452 | match curr { |
| 1453 | RsTypeKind::Unit | RsTypeKind::Record(_) => (), |
| 1454 | RsTypeKind::Pointer { pointee, .. } => self.todo.push(pointee), |
| 1455 | RsTypeKind::Reference { referent, .. } => self.todo.push(referent), |
| 1456 | RsTypeKind::TypeAlias { underlying_type: t, .. } => self.todo.push(t), |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 1457 | RsTypeKind::FuncPtr { return_type, param_types, .. } => { |
| 1458 | self.todo.push(return_type); |
| 1459 | self.todo.extend(param_types.iter().rev()); |
| 1460 | }, |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 1461 | RsTypeKind::Other { type_args, .. } => self.todo.extend(type_args.iter().rev()), |
| 1462 | }; |
| 1463 | Some(curr) |
| 1464 | } |
| 1465 | } |
| 1466 | } |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 1469 | fn format_lifetime_name(lifetime_name: &str) -> TokenStream { |
| 1470 | let lifetime = |
| 1471 | syn::Lifetime::new(&format!("'{}", lifetime_name), proc_macro2::Span::call_site()); |
| 1472 | quote! { #lifetime } |
| 1473 | } |
| 1474 | |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 1475 | fn format_rs_type( |
| 1476 | ty: &ir::RsType, |
| 1477 | ir: &IR, |
| 1478 | lifetime_to_name: &HashMap<LifetimeId, String>, |
| 1479 | ) -> Result<TokenStream> { |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 1480 | RsTypeKind::new(ty, ir) |
| 1481 | .and_then(|kind| kind.format(ir, lifetime_to_name)) |
| 1482 | .with_context(|| format!("Failed to format Rust type {:?}", ty)) |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 1485 | fn cc_type_name_for_item(item: &ir::Item) -> Result<TokenStream> { |
| 1486 | let (disambiguator_fragment, identifier) = match item { |
| 1487 | Item::Record(record) => (quote! { class }, &record.identifier), |
| 1488 | Item::TypeAlias(type_alias) => (quote! {}, &type_alias.identifier), |
| 1489 | _ => bail!("Item does not define a type: {:?}", item), |
| 1490 | }; |
| 1491 | |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1492 | let ident = format_cc_ident(identifier.identifier.as_str()); |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 1493 | Ok(quote! { #disambiguator_fragment #ident }) |
| 1494 | } |
| 1495 | |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1496 | fn format_cc_type(ty: &ir::CcType, ir: &IR) -> Result<TokenStream> { |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 1497 | let const_fragment = if ty.is_const { |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 1498 | quote! {const} |
| 1499 | } else { |
| 1500 | quote! {} |
| 1501 | }; |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1502 | if let Some(ref name) = ty.name { |
| 1503 | match name.as_str() { |
| 1504 | "*" => { |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 1505 | if ty.type_args.len() != 1 { |
| 1506 | bail!("Invalid pointer type (need exactly 1 type argument): {:?}", ty); |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1507 | } |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 1508 | assert_eq!(ty.type_args.len(), 1); |
| 1509 | let nested_type = format_cc_type(&ty.type_args[0], ir)?; |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1510 | Ok(quote! {#nested_type * #const_fragment}) |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 1511 | } |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 1512 | "&" => { |
| 1513 | if ty.type_args.len() != 1 { |
| 1514 | bail!("Invalid reference type (need exactly 1 type argument): {:?}", ty); |
| 1515 | } |
| 1516 | let nested_type = format_cc_type(&ty.type_args[0], ir)?; |
| 1517 | Ok(quote! {#nested_type &}) |
| 1518 | } |
Lukasz Anforowicz | 957cbf2 | 2022-01-05 16:14:05 +0000 | [diff] [blame] | 1519 | cc_type_name => { |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 1520 | if !ty.type_args.is_empty() { |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1521 | bail!("Type not yet supported: {:?}", ty); |
| 1522 | } |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1523 | let idents = cc_type_name.split_whitespace().map(format_cc_ident); |
Lukasz Anforowicz | 957cbf2 | 2022-01-05 16:14:05 +0000 | [diff] [blame] | 1524 | Ok(quote! {#( #idents )* #const_fragment}) |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 1525 | } |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 1526 | } |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1527 | } else { |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 1528 | let item = ir.item_for_type(ty)?; |
| 1529 | let type_name = cc_type_name_for_item(item)?; |
| 1530 | Ok(quote! {#const_fragment #type_name}) |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1534 | fn cc_struct_layout_assertion(record: &Record, ir: &IR) -> TokenStream { |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 1535 | if !ir.is_current_target(&record.owning_target) && !ir.is_stdlib_target(&record.owning_target) { |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1536 | return quote! {}; |
| 1537 | } |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1538 | let record_ident = format_cc_ident(&record.identifier.identifier); |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1539 | let size = Literal::usize_unsuffixed(record.size); |
| 1540 | let alignment = Literal::usize_unsuffixed(record.alignment); |
Lukasz Anforowicz | 7470471 | 2021-12-22 15:30:31 +0000 | [diff] [blame] | 1541 | let field_assertions = |
| 1542 | record.fields.iter().filter(|f| f.access == AccessSpecifier::Public).map(|field| { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1543 | let field_ident = format_cc_ident(&field.identifier.identifier); |
Lukasz Anforowicz | 7470471 | 2021-12-22 15:30:31 +0000 | [diff] [blame] | 1544 | let offset = Literal::usize_unsuffixed(field.offset); |
| 1545 | // The IR contains the offset in bits, while C++'s offsetof() |
| 1546 | // returns the offset in bytes, so we need to convert. |
| 1547 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1548 | static_assert(offsetof(class #record_ident, #field_ident) * 8 == #offset); |
Lukasz Anforowicz | 7470471 | 2021-12-22 15:30:31 +0000 | [diff] [blame] | 1549 | } |
| 1550 | }); |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1551 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1552 | static_assert(sizeof(class #record_ident) == #size); |
| 1553 | static_assert(alignof(class #record_ident) == #alignment); |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1554 | #( #field_assertions )* |
| 1555 | } |
| 1556 | } |
| 1557 | |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 1558 | // Returns the accessor functions for no_unique_address member variables. |
| 1559 | fn cc_struct_no_unique_address_impl(record: &Record, ir: &IR) -> Result<TokenStream> { |
| 1560 | let mut fields = vec![]; |
| 1561 | let mut types = vec![]; |
| 1562 | for field in &record.fields { |
| 1563 | if field.access != AccessSpecifier::Public || !field.is_no_unique_address { |
| 1564 | continue; |
| 1565 | } |
| 1566 | fields.push(make_rs_ident(&field.identifier.identifier)); |
| 1567 | types.push(format_rs_type(&field.type_.rs_type, ir, &HashMap::new()).with_context( |
| 1568 | || format!("Failed to format type for field {:?} on record {:?}", field, record), |
| 1569 | )?) |
| 1570 | } |
| 1571 | |
| 1572 | if fields.is_empty() { |
| 1573 | return Ok(quote! {}); |
| 1574 | } |
| 1575 | |
| 1576 | let ident = make_rs_ident(&record.identifier.identifier); |
| 1577 | Ok(quote! { |
| 1578 | impl #ident { |
| 1579 | #( |
| 1580 | pub fn #fields(&self) -> &#types { |
| 1581 | unsafe {&* (&self.#fields as *const _ as *const #types)} |
| 1582 | } |
| 1583 | )* |
| 1584 | } |
| 1585 | }) |
| 1586 | } |
| 1587 | |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 1588 | /// Returns the implementation of base class conversions, for converting a type |
| 1589 | /// to its unambiguous public base classes. |
| 1590 | /// |
| 1591 | /// TODO(b/216195042): Implement this in terms of a supporting trait which casts |
| 1592 | /// raw pointers. Then, we would have blanket impls for reference, pinned mut |
| 1593 | /// reference, etc. conversion. The current version is just enough to test the |
| 1594 | /// logic in importer. |
| 1595 | // |
| 1596 | // TODO(b/216195042): Should this use, like, AsRef/AsMut (and some equivalent |
| 1597 | // for Pin)? |
| 1598 | fn cc_struct_upcast_impl(record: &Record, ir: &IR) -> Result<TokenStream> { |
| 1599 | let mut impls = Vec::with_capacity(record.unambiguous_public_bases.len()); |
| 1600 | for base in &record.unambiguous_public_bases { |
| 1601 | let base_record: &Record = ir.find_decl(base.base_record_id)?.try_into()?; |
| 1602 | if let Some(offset) = base.offset { |
| 1603 | let offset = Literal::i64_unsuffixed(offset); |
| 1604 | // TODO(b/216195042): Correctly handle imported records, lifetimes. |
| 1605 | let base_name = make_rs_ident(&base_record.identifier.identifier); |
| 1606 | let derived_name = make_rs_ident(&record.identifier.identifier); |
| 1607 | impls.push(quote! { |
| 1608 | impl<'a> From<&'a #derived_name> for &'a #base_name { |
| 1609 | fn from(x: &'a #derived_name) -> Self { |
| 1610 | unsafe { |
| 1611 | &*((x as *const _ as *const u8).offset(#offset) as *const #base_name) |
| 1612 | } |
| 1613 | } |
| 1614 | } |
| 1615 | }); |
| 1616 | } else { |
| 1617 | // TODO(b/216195042): determine offset dynamically / use a dynamic |
| 1618 | // cast. This requires a new C++ function to be |
| 1619 | // generated, so that we have something to call. |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | Ok(quote! { |
| 1624 | #(#impls)* |
| 1625 | }) |
| 1626 | } |
| 1627 | |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1628 | fn thunk_ident(func: &Func) -> Ident { |
| 1629 | format_ident!("__rust_thunk__{}", func.mangled_name) |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1632 | fn generate_rs_api_impl(ir: &IR) -> Result<TokenStream> { |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 1633 | // This function uses quote! to generate C++ source code out of convenience. |
| 1634 | // This is a bold idea so we have to continously evaluate if it still makes |
| 1635 | // sense or the cost of working around differences in Rust and C++ tokens is |
| 1636 | // greather than the value added. |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1637 | // |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 1638 | // See rs_bindings_from_cc/ |
| 1639 | // token_stream_printer.rs for a list of supported placeholders. |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1640 | let mut thunks = vec![]; |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 1641 | for func in ir.functions() { |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 1642 | if can_skip_cc_thunk(func) { |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1643 | continue; |
| 1644 | } |
| 1645 | |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1646 | let thunk_ident = thunk_ident(func); |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 1647 | let implementation_function = match &func.name { |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 1648 | UnqualifiedIdentifier::Operator(op) => { |
| 1649 | let name = syn::parse_str::<TokenStream>(&op.name)?; |
| 1650 | quote! { operator #name } |
| 1651 | } |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 1652 | UnqualifiedIdentifier::Identifier(id) => { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1653 | let fn_ident = format_cc_ident(&id.identifier); |
Lukasz Anforowicz | aab8ad2 | 2021-12-19 20:29:26 +0000 | [diff] [blame] | 1654 | let static_method_metadata = func |
| 1655 | .member_func_metadata |
| 1656 | .as_ref() |
| 1657 | .filter(|meta| meta.instance_method_metadata.is_none()); |
| 1658 | match static_method_metadata { |
| 1659 | None => quote! {#fn_ident}, |
| 1660 | Some(meta) => { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 1661 | let record_ident = |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1662 | format_cc_ident(&meta.find_record(ir)?.identifier.identifier); |
Lukasz Anforowicz | aab8ad2 | 2021-12-19 20:29:26 +0000 | [diff] [blame] | 1663 | quote! { #record_ident :: #fn_ident } |
| 1664 | } |
| 1665 | } |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 1666 | } |
Lukasz Anforowicz | 7b0042d | 2022-01-06 23:00:19 +0000 | [diff] [blame] | 1667 | // Use `destroy_at` to avoid needing to spell out the class name. Destructor identiifers |
Devin Jeanpierre | cc6cf09 | 2021-12-16 04:31:14 +0000 | [diff] [blame] | 1668 | // use the name of the type itself, without namespace qualification, template |
| 1669 | // parameters, or aliases. We do not need to use that naming scheme anywhere else in |
| 1670 | // the bindings, and it can be difficult (impossible?) to spell in the general case. By |
| 1671 | // using destroy_at, we avoid needing to determine or remember what the correct spelling |
Lukasz Anforowicz | 7b0042d | 2022-01-06 23:00:19 +0000 | [diff] [blame] | 1672 | // is. Similar arguments apply to `construct_at`. |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 1673 | UnqualifiedIdentifier::Constructor => { |
Lukasz Anforowicz | 7b0042d | 2022-01-06 23:00:19 +0000 | [diff] [blame] | 1674 | quote! { rs_api_impl_support::construct_at } |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 1675 | } |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 1676 | UnqualifiedIdentifier::Destructor => quote! {std::destroy_at}, |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 1677 | }; |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1678 | let return_type_name = format_cc_type(&func.return_type.cc_type, ir)?; |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 1679 | let return_stmt = if func.return_type.cc_type.is_void() { |
| 1680 | quote! {} |
| 1681 | } else { |
| 1682 | quote! { return } |
| 1683 | }; |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1684 | |
| 1685 | let param_idents = |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1686 | func.params.iter().map(|p| format_cc_ident(&p.identifier.identifier)).collect_vec(); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1687 | |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 1688 | let param_types = func |
| 1689 | .params |
| 1690 | .iter() |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 1691 | .map(|p| format_cc_type(&p.type_.cc_type, ir)) |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 1692 | .collect::<Result<Vec<_>>>()?; |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1693 | |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 1694 | let needs_this_deref = match &func.member_func_metadata { |
| 1695 | None => false, |
| 1696 | Some(meta) => match &func.name { |
| 1697 | UnqualifiedIdentifier::Constructor | UnqualifiedIdentifier::Destructor => false, |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 1698 | UnqualifiedIdentifier::Identifier(_) | UnqualifiedIdentifier::Operator(_) => { |
| 1699 | meta.instance_method_metadata.is_some() |
| 1700 | } |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 1701 | }, |
| 1702 | }; |
| 1703 | let (implementation_function, arg_expressions) = if !needs_this_deref { |
| 1704 | (implementation_function, param_idents.clone()) |
| 1705 | } else { |
| 1706 | let this_param = func |
| 1707 | .params |
| 1708 | .first() |
| 1709 | .ok_or_else(|| anyhow!("Instance methods must have `__this` param."))?; |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1710 | let this_arg = format_cc_ident(&this_param.identifier.identifier); |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 1711 | ( |
| 1712 | quote! { #this_arg -> #implementation_function}, |
| 1713 | param_idents.iter().skip(1).cloned().collect_vec(), |
| 1714 | ) |
| 1715 | }; |
| 1716 | |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1717 | thunks.push(quote! { |
| 1718 | extern "C" #return_type_name #thunk_ident( #( #param_types #param_idents ),* ) { |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 1719 | #return_stmt #implementation_function( #( #arg_expressions ),* ); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1720 | } |
| 1721 | }); |
| 1722 | } |
| 1723 | |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1724 | let layout_assertions = ir.records().map(|record| cc_struct_layout_assertion(record, ir)); |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1725 | |
Devin Jeanpierre | 231ef8d | 2021-10-27 10:50:44 +0000 | [diff] [blame] | 1726 | let mut standard_headers = <BTreeSet<Ident>>::new(); |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1727 | standard_headers.insert(format_ident!("memory")); // ubiquitous. |
Devin Jeanpierre | 231ef8d | 2021-10-27 10:50:44 +0000 | [diff] [blame] | 1728 | if ir.records().next().is_some() { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 1729 | standard_headers.insert(format_ident!("cstddef")); |
Devin Jeanpierre | 231ef8d | 2021-10-27 10:50:44 +0000 | [diff] [blame] | 1730 | }; |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1731 | |
Lukasz Anforowicz | 4457baf | 2021-12-23 17:24:04 +0000 | [diff] [blame] | 1732 | let mut includes = |
| 1733 | vec!["rs_bindings_from_cc/support/cxx20_backports.h"]; |
| 1734 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 1735 | // In order to generate C++ thunk in all the cases Clang needs to be able to |
| 1736 | // access declarations from public headers of the C++ library. |
Lukasz Anforowicz | 4457baf | 2021-12-23 17:24:04 +0000 | [diff] [blame] | 1737 | includes.extend(ir.used_headers().map(|i| &i.name as &str)); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1738 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1739 | Ok(quote! { |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1740 | #( __HASH_TOKEN__ include <#standard_headers> __NEWLINE__)* |
Devin Jeanpierre | 7c74f84 | 2022-02-03 07:08:06 +0000 | [diff] [blame] | 1741 | __NEWLINE__ |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1742 | #( __HASH_TOKEN__ include #includes __NEWLINE__)* __NEWLINE__ |
Marcel Hlopko | b8069ae | 2022-02-19 09:31:00 +0000 | [diff] [blame] | 1743 | __HASH_TOKEN__ pragma clang diagnostic push __NEWLINE__ |
| 1744 | // Disable Clang thread-safety-analysis warnings that would otherwise |
| 1745 | // complain about thunks that call mutex locking functions in an unpaired way. |
| 1746 | __HASH_TOKEN__ pragma clang diagnostic ignored "-Wthread-safety-analysis" __NEWLINE__ |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1747 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1748 | #( #thunks )* __NEWLINE__ __NEWLINE__ |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1749 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1750 | #( #layout_assertions __NEWLINE__ __NEWLINE__ )* |
Marcel Hlopko | c6b726c | 2021-10-07 06:53:09 +0000 | [diff] [blame] | 1751 | |
Marcel Hlopko | b8069ae | 2022-02-19 09:31:00 +0000 | [diff] [blame] | 1752 | __NEWLINE__ |
| 1753 | __HASH_TOKEN__ pragma clang diagnostic pop __NEWLINE__ |
Marcel Hlopko | c6b726c | 2021-10-07 06:53:09 +0000 | [diff] [blame] | 1754 | // To satisfy http://cs/symbol:devtools.metadata.Presubmit.CheckTerminatingNewline check. |
| 1755 | __NEWLINE__ |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1756 | }) |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1759 | #[cfg(test)] |
| 1760 | mod tests { |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 1761 | use super::*; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 1762 | use anyhow::anyhow; |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 1763 | use ir_testing::{ir_from_cc, ir_from_cc_dependency, ir_func, ir_record, retrieve_func}; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1764 | use token_stream_matchers::{ |
| 1765 | assert_cc_matches, assert_cc_not_matches, assert_rs_matches, assert_rs_not_matches, |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 1766 | }; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1767 | use token_stream_printer::tokens_to_string; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1768 | |
| 1769 | #[test] |
Marcel Hlopko | b8069ae | 2022-02-19 09:31:00 +0000 | [diff] [blame] | 1770 | fn test_disable_thread_safety_warnings() -> Result<()> { |
| 1771 | let ir = ir_from_cc("inline void foo() {}")?; |
| 1772 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 1773 | assert_cc_matches!( |
| 1774 | rs_api_impl, |
| 1775 | quote! { |
| 1776 | ... |
| 1777 | __HASH_TOKEN__ pragma clang diagnostic push |
| 1778 | __HASH_TOKEN__ pragma clang diagnostic ignored "-Wthread-safety-analysis" |
| 1779 | ... |
| 1780 | |
| 1781 | __HASH_TOKEN__ pragma clang diagnostic pop |
| 1782 | ... |
| 1783 | } |
| 1784 | ); |
| 1785 | Ok(()) |
| 1786 | } |
| 1787 | |
| 1788 | #[test] |
Marcel Hlopko | 3b9bf9e | 2021-11-29 08:25:14 +0000 | [diff] [blame] | 1789 | // TODO(hlopko): Move this test to a more principled place where it can access |
| 1790 | // `ir_testing`. |
| 1791 | fn test_duplicate_decl_ids_err() { |
| 1792 | let mut r1 = ir_record("R1"); |
Marcel Hlopko | 264b9ad | 2021-12-02 21:06:44 +0000 | [diff] [blame] | 1793 | r1.id = DeclId(42); |
Marcel Hlopko | 3b9bf9e | 2021-11-29 08:25:14 +0000 | [diff] [blame] | 1794 | let mut r2 = ir_record("R2"); |
Marcel Hlopko | 264b9ad | 2021-12-02 21:06:44 +0000 | [diff] [blame] | 1795 | r2.id = DeclId(42); |
Marcel Hlopko | 3b9bf9e | 2021-11-29 08:25:14 +0000 | [diff] [blame] | 1796 | let result = make_ir_from_items([r1.into(), r2.into()]); |
| 1797 | assert!(result.is_err()); |
| 1798 | assert!(result.unwrap_err().to_string().contains("Duplicate decl_id found in")); |
| 1799 | } |
| 1800 | |
| 1801 | #[test] |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 1802 | fn test_simple_function() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1803 | let ir = ir_from_cc("int Add(int a, int b);")?; |
| 1804 | let rs_api = generate_rs_api(&ir)?; |
| 1805 | assert_rs_matches!( |
| 1806 | rs_api, |
| 1807 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1808 | #[inline(always)] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1809 | pub fn Add(a: i32, b: i32) -> i32 { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1810 | unsafe { crate::detail::__rust_thunk___Z3Addii(a, b) } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | ); |
| 1814 | assert_rs_matches!( |
| 1815 | rs_api, |
| 1816 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1817 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 1818 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 1819 | use super::*; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1820 | extern "C" { |
| 1821 | #[link_name = "_Z3Addii"] |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1822 | pub(crate) fn __rust_thunk___Z3Addii(a: i32, b: i32) -> i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1823 | } |
| 1824 | } |
| 1825 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1826 | ); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1827 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1828 | assert_cc_not_matches!(generate_rs_api_impl(&ir)?, quote! {__rust_thunk___Z3Addii}); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1829 | |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1830 | Ok(()) |
| 1831 | } |
| 1832 | |
| 1833 | #[test] |
| 1834 | fn test_inline_function() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1835 | let ir = ir_from_cc("inline int Add(int a, int b);")?; |
| 1836 | let rs_api = generate_rs_api(&ir)?; |
| 1837 | assert_rs_matches!( |
| 1838 | rs_api, |
| 1839 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1840 | #[inline(always)] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1841 | pub fn Add(a: i32, b: i32) -> i32 { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1842 | unsafe { crate::detail::__rust_thunk___Z3Addii(a, b) } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1843 | } |
| 1844 | } |
| 1845 | ); |
| 1846 | assert_rs_matches!( |
| 1847 | rs_api, |
| 1848 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1849 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 1850 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 1851 | use super::*; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1852 | extern "C" { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1853 | pub(crate) fn __rust_thunk___Z3Addii(a: i32, b: i32) -> i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | } |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1857 | ); |
| 1858 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1859 | assert_cc_matches!( |
| 1860 | generate_rs_api_impl(&ir)?, |
| 1861 | quote! { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1862 | extern "C" int __rust_thunk___Z3Addii(int a, int b) { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1863 | return Add(a, b); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1864 | } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1865 | } |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 1866 | ); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 1867 | Ok(()) |
| 1868 | } |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 1869 | |
| 1870 | #[test] |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1871 | fn test_simple_function_with_types_from_other_target() -> Result<()> { |
| 1872 | let ir = ir_from_cc_dependency( |
| 1873 | "inline ReturnStruct DoSomething(ParamStruct param);", |
| 1874 | "struct ReturnStruct {}; struct ParamStruct {};", |
| 1875 | )?; |
| 1876 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1877 | let rs_api = generate_rs_api(&ir)?; |
| 1878 | assert_rs_matches!( |
| 1879 | rs_api, |
| 1880 | quote! { |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1881 | #[inline(always)] |
| 1882 | pub fn DoSomething(param: dependency::ParamStruct) |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1883 | -> dependency::ReturnStruct { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 1884 | unsafe { crate::detail::__rust_thunk___Z11DoSomething11ParamStruct(param) } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1885 | } |
| 1886 | } |
| 1887 | ); |
| 1888 | assert_rs_matches!( |
| 1889 | rs_api, |
| 1890 | quote! { |
| 1891 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 1892 | #[allow(unused_imports)] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1893 | use super::*; |
| 1894 | extern "C" { |
| 1895 | pub(crate) fn __rust_thunk___Z11DoSomething11ParamStruct(param: dependency::ParamStruct) |
| 1896 | -> dependency::ReturnStruct; |
| 1897 | } |
| 1898 | }} |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1899 | ); |
| 1900 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1901 | assert_cc_matches!( |
| 1902 | generate_rs_api_impl(&ir)?, |
| 1903 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1904 | extern "C" class ReturnStruct __rust_thunk___Z11DoSomething11ParamStruct(class ParamStruct param) { |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1905 | return DoSomething(param); |
| 1906 | } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1907 | } |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 1908 | ); |
| 1909 | Ok(()) |
| 1910 | } |
| 1911 | |
| 1912 | #[test] |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 1913 | fn test_simple_struct() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1914 | let ir = ir_from_cc(&tokens_to_string(quote! { |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 1915 | struct SomeStruct final { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1916 | int public_int; |
| 1917 | protected: |
| 1918 | int protected_int; |
| 1919 | private: |
| 1920 | int private_int; |
| 1921 | }; |
| 1922 | })?)?; |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 1923 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1924 | let rs_api = generate_rs_api(&ir)?; |
| 1925 | assert_rs_matches!( |
| 1926 | rs_api, |
| 1927 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 1928 | #[derive(Clone, Copy)] |
| 1929 | #[repr(C)] |
| 1930 | pub struct SomeStruct { |
| 1931 | pub public_int: i32, |
| 1932 | protected_int: i32, |
| 1933 | private_int: i32, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1934 | } |
| 1935 | } |
| 1936 | ); |
| 1937 | assert_rs_matches!( |
| 1938 | rs_api, |
| 1939 | quote! { |
Googler | 454f265 | 2021-12-06 12:53:12 +0000 | [diff] [blame] | 1940 | const _: () = assert!(std::mem::size_of::<Option<&i32>>() == std::mem::size_of::<&i32>()); |
Googler | 209b10a | 2021-12-06 09:11:57 +0000 | [diff] [blame] | 1941 | const _: () = assert!(std::mem::size_of::<SomeStruct>() == 12usize); |
| 1942 | const _: () = assert!(std::mem::align_of::<SomeStruct>() == 4usize); |
| 1943 | const _: () = assert!(offset_of!(SomeStruct, public_int) * 8 == 0usize); |
| 1944 | const _: () = assert!(offset_of!(SomeStruct, protected_int) * 8 == 32usize); |
| 1945 | const _: () = assert!(offset_of!(SomeStruct, private_int) * 8 == 64usize); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1946 | } |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 1947 | ); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1948 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 1949 | assert_cc_matches!( |
| 1950 | rs_api_impl, |
| 1951 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1952 | extern "C" void __rust_thunk___ZN10SomeStructD1Ev(class SomeStruct * __this) { |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 1953 | std :: destroy_at (__this) ; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1954 | } |
| 1955 | } |
| 1956 | ); |
| 1957 | assert_cc_matches!( |
| 1958 | rs_api_impl, |
| 1959 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1960 | static_assert(sizeof(class SomeStruct) == 12); |
| 1961 | static_assert(alignof(class SomeStruct) == 4); |
| 1962 | static_assert(offsetof(class SomeStruct, public_int) * 8 == 0); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1963 | } |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 1964 | ); |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 1965 | Ok(()) |
| 1966 | } |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 1967 | |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 1968 | #[test] |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 1969 | fn test_ref_to_struct_in_thunk_impls() -> Result<()> { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1970 | let ir = ir_from_cc("struct S{}; inline void foo(class S& s) {} ")?; |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 1971 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 1972 | assert_cc_matches!( |
| 1973 | rs_api_impl, |
| 1974 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1975 | extern "C" void __rust_thunk___Z3fooR1S(class S& s) { |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 1976 | foo(s); |
| 1977 | } |
| 1978 | } |
| 1979 | ); |
| 1980 | Ok(()) |
| 1981 | } |
| 1982 | |
| 1983 | #[test] |
| 1984 | fn test_const_ref_to_struct_in_thunk_impls() -> Result<()> { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1985 | let ir = ir_from_cc("struct S{}; inline void foo(const class S& s) {} ")?; |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 1986 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 1987 | assert_cc_matches!( |
| 1988 | rs_api_impl, |
| 1989 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 1990 | extern "C" void __rust_thunk___Z3fooRK1S(const class S& s) { |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 1991 | foo(s); |
| 1992 | } |
| 1993 | } |
| 1994 | ); |
| 1995 | Ok(()) |
| 1996 | } |
| 1997 | |
| 1998 | #[test] |
Lukasz Anforowicz | 957cbf2 | 2022-01-05 16:14:05 +0000 | [diff] [blame] | 1999 | fn test_unsigned_int_in_thunk_impls() -> Result<()> { |
| 2000 | let ir = ir_from_cc("inline void foo(unsigned int i) {} ")?; |
| 2001 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 2002 | assert_cc_matches!( |
| 2003 | rs_api_impl, |
| 2004 | quote! { |
| 2005 | extern "C" void __rust_thunk___Z3fooj(unsigned int i) { |
| 2006 | foo(i); |
| 2007 | } |
| 2008 | } |
| 2009 | ); |
| 2010 | Ok(()) |
| 2011 | } |
| 2012 | |
| 2013 | #[test] |
Marcel Hlopko | dd1fcb1 | 2021-12-22 14:13:59 +0000 | [diff] [blame] | 2014 | fn test_record_static_methods_qualify_call_in_thunk() -> Result<()> { |
| 2015 | let ir = ir_from_cc(&tokens_to_string(quote! { |
| 2016 | struct SomeStruct { |
| 2017 | static inline int some_func() { return 42; } |
| 2018 | }; |
| 2019 | })?)?; |
| 2020 | |
| 2021 | assert_cc_matches!( |
| 2022 | generate_rs_api_impl(&ir)?, |
| 2023 | quote! { |
| 2024 | extern "C" int __rust_thunk___ZN10SomeStruct9some_funcEv() { |
| 2025 | return SomeStruct::some_func(); |
| 2026 | } |
| 2027 | } |
| 2028 | ); |
| 2029 | Ok(()) |
| 2030 | } |
| 2031 | |
| 2032 | #[test] |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 2033 | fn test_record_instance_methods_deref_this_in_thunk() -> Result<()> { |
| 2034 | let ir = ir_from_cc(&tokens_to_string(quote! { |
| 2035 | struct SomeStruct { |
| 2036 | inline int some_func(int arg) const { return 42 + arg; } |
| 2037 | }; |
| 2038 | })?)?; |
| 2039 | |
| 2040 | assert_cc_matches!( |
| 2041 | generate_rs_api_impl(&ir)?, |
| 2042 | quote! { |
| 2043 | extern "C" int __rust_thunk___ZNK10SomeStruct9some_funcEi( |
| 2044 | const class SomeStruct* __this, int arg) { |
| 2045 | return __this->some_func(arg); |
| 2046 | } |
| 2047 | } |
| 2048 | ); |
| 2049 | Ok(()) |
| 2050 | } |
| 2051 | |
| 2052 | #[test] |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 2053 | fn test_struct_from_other_target() -> Result<()> { |
| 2054 | let ir = ir_from_cc_dependency("// intentionally empty", "struct SomeStruct {};")?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2055 | assert_rs_not_matches!(generate_rs_api(&ir)?, quote! { SomeStruct }); |
| 2056 | assert_cc_not_matches!(generate_rs_api_impl(&ir)?, quote! { SomeStruct }); |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 2057 | Ok(()) |
| 2058 | } |
| 2059 | |
| 2060 | #[test] |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2061 | fn test_copy_derives() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 2062 | let record = ir_record("S"); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2063 | assert_eq!(generate_derives(&record), &["Clone", "Copy"]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | #[test] |
| 2067 | fn test_copy_derives_not_is_trivial_abi() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 2068 | let mut record = ir_record("S"); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2069 | record.is_trivial_abi = false; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2070 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 2073 | /// Even if it's trivially relocatable, !Unpin C++ type cannot be |
| 2074 | /// cloned/copied or otherwise used by value, because values would allow |
| 2075 | /// assignment into the Pin. |
| 2076 | /// |
| 2077 | /// All !Unpin C++ types, not just non trivially relocatable ones, are |
| 2078 | /// unsafe to assign in the Rust sense. |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 2079 | #[test] |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 2080 | fn test_copy_derives_not_final() { |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 2081 | let mut record = ir_record("S"); |
| 2082 | record.is_final = false; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 2083 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2086 | #[test] |
| 2087 | fn test_copy_derives_ctor_nonpublic() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 2088 | let mut record = ir_record("S"); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2089 | for access in [ir::AccessSpecifier::Protected, ir::AccessSpecifier::Private] { |
| 2090 | record.copy_constructor.access = access; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2091 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | #[test] |
| 2096 | fn test_copy_derives_ctor_deleted() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 2097 | let mut record = ir_record("S"); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2098 | record.copy_constructor.definition = ir::SpecialMemberDefinition::Deleted; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2099 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | #[test] |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 2103 | fn test_copy_derives_ctor_nontrivial_members() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 2104 | let mut record = ir_record("S"); |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 2105 | record.copy_constructor.definition = ir::SpecialMemberDefinition::NontrivialMembers; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2106 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | #[test] |
| 2110 | fn test_copy_derives_ctor_nontrivial_self() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 2111 | let mut record = ir_record("S"); |
Devin Jeanpierre | 7b62e95 | 2021-12-08 21:43:30 +0000 | [diff] [blame] | 2112 | record.copy_constructor.definition = ir::SpecialMemberDefinition::NontrivialUserDefined; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2113 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 2116 | #[test] |
| 2117 | fn test_ptr_func() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2118 | let ir = ir_from_cc(&tokens_to_string(quote! { |
| 2119 | inline int* Deref(int*const* p); |
| 2120 | })?)?; |
Devin Jeanpierre | d6da700 | 2021-10-21 12:55:20 +0000 | [diff] [blame] | 2121 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2122 | let rs_api = generate_rs_api(&ir)?; |
| 2123 | assert_rs_matches!( |
| 2124 | rs_api, |
| 2125 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2126 | #[inline(always)] |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 2127 | pub unsafe fn Deref(p: *const *mut i32) -> *mut i32 { |
| 2128 | crate::detail::__rust_thunk___Z5DerefPKPi(p) |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2129 | } |
| 2130 | } |
| 2131 | ); |
| 2132 | assert_rs_matches!( |
| 2133 | rs_api, |
| 2134 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2135 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 2136 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 2137 | use super::*; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2138 | extern "C" { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 2139 | pub(crate) fn __rust_thunk___Z5DerefPKPi(p: *const *mut i32) -> *mut i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2140 | } |
| 2141 | } |
| 2142 | } |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 2143 | ); |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 2144 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2145 | assert_cc_matches!( |
| 2146 | generate_rs_api_impl(&ir)?, |
| 2147 | quote! { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 2148 | extern "C" int* __rust_thunk___Z5DerefPKPi(int* const * p) { |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 2149 | return Deref(p); |
| 2150 | } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2151 | } |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 2152 | ); |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 2153 | Ok(()) |
| 2154 | } |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 2155 | |
| 2156 | #[test] |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 2157 | fn test_const_char_ptr_func() -> Result<()> { |
| 2158 | // This is a regression test: We used to include the "const" in the name |
| 2159 | // of the CcType, which caused a panic in the code generator |
| 2160 | // ('"const char" is not a valid Ident'). |
| 2161 | // It's therefore important that f() is inline so that we need to |
| 2162 | // generate a thunk for it (where we then process the CcType). |
| 2163 | let ir = ir_from_cc(&tokens_to_string(quote! { |
| 2164 | inline void f(const char *str); |
| 2165 | })?)?; |
| 2166 | |
| 2167 | let rs_api = generate_rs_api(&ir)?; |
| 2168 | assert_rs_matches!( |
| 2169 | rs_api, |
| 2170 | quote! { |
| 2171 | #[inline(always)] |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 2172 | pub unsafe fn f(str: *const i8) { |
| 2173 | crate::detail::__rust_thunk___Z1fPKc(str) |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 2174 | } |
| 2175 | } |
| 2176 | ); |
| 2177 | assert_rs_matches!( |
| 2178 | rs_api, |
| 2179 | quote! { |
| 2180 | extern "C" { |
| 2181 | pub(crate) fn __rust_thunk___Z1fPKc(str: *const i8); |
| 2182 | } |
| 2183 | } |
| 2184 | ); |
| 2185 | |
| 2186 | assert_cc_matches!( |
| 2187 | generate_rs_api_impl(&ir)?, |
| 2188 | quote! { |
| 2189 | extern "C" void __rust_thunk___Z1fPKc(char const * str){ f(str) ; } |
| 2190 | } |
| 2191 | ); |
| 2192 | Ok(()) |
| 2193 | } |
| 2194 | |
| 2195 | #[test] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 2196 | fn test_func_ptr_where_params_are_primitive_types() -> Result<()> { |
| 2197 | let ir = ir_from_cc(r#" int (*get_ptr_to_func())(float, double); "#)?; |
| 2198 | let rs_api = generate_rs_api(&ir)?; |
| 2199 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 2200 | assert_rs_matches!( |
| 2201 | rs_api, |
| 2202 | quote! { |
| 2203 | #[inline(always)] |
| 2204 | pub fn get_ptr_to_func() -> Option<extern "C" fn (f32, f64) -> i32> { |
| 2205 | unsafe { crate::detail::__rust_thunk___Z15get_ptr_to_funcv() } |
| 2206 | } |
| 2207 | } |
| 2208 | ); |
| 2209 | assert_rs_matches!( |
| 2210 | rs_api, |
| 2211 | quote! { |
| 2212 | mod detail { |
| 2213 | #[allow(unused_imports)] |
| 2214 | use super::*; |
| 2215 | extern "C" { |
| 2216 | #[link_name = "_Z15get_ptr_to_funcv"] |
| 2217 | pub(crate) fn __rust_thunk___Z15get_ptr_to_funcv() |
| 2218 | -> Option<extern "C" fn(f32, f64) -> i32>; |
| 2219 | } |
| 2220 | } |
| 2221 | } |
| 2222 | ); |
| 2223 | // Verify that no C++ thunk got generated. |
| 2224 | assert_cc_not_matches!(rs_api_impl, quote! { __rust_thunk___Z15get_ptr_to_funcv }); |
| 2225 | |
| 2226 | // TODO(b/217419782): Add another test for more exotic calling conventions / |
| 2227 | // abis. |
| 2228 | |
| 2229 | // TODO(b/217419782): Add another test for pointer to a function that |
| 2230 | // takes/returns non-trivially-movable types by value. See also |
| 2231 | // <internal link> |
| 2232 | |
| 2233 | Ok(()) |
| 2234 | } |
| 2235 | |
| 2236 | #[test] |
| 2237 | fn test_func_ptr_with_non_static_lifetime() -> Result<()> { |
| 2238 | let ir = ir_from_cc( |
| 2239 | r#" |
Googler | 53f6594 | 2022-02-23 11:23:30 +0000 | [diff] [blame^] | 2240 | [[clang::annotate("lifetimes", "-> a")]] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 2241 | int (*get_ptr_to_func())(float, double); "#, |
| 2242 | )?; |
| 2243 | let rs_api = generate_rs_api(&ir)?; |
| 2244 | assert_rs_matches!( |
| 2245 | rs_api, |
| 2246 | quote! { |
| 2247 | // Error while generating bindings for item 'get_ptr_to_func': |
| 2248 | // Return type is not supported: Function pointers with non-'static lifetimes are not supported: int (*)(float, double) |
| 2249 | } |
| 2250 | ); |
| 2251 | Ok(()) |
| 2252 | } |
| 2253 | |
| 2254 | #[test] |
| 2255 | fn test_func_ptr_where_params_are_raw_ptrs() -> Result<()> { |
| 2256 | let ir = ir_from_cc(r#" const int* (*get_ptr_to_func())(const int*); "#)?; |
| 2257 | let rs_api = generate_rs_api(&ir)?; |
| 2258 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 2259 | assert_rs_matches!( |
| 2260 | rs_api, |
| 2261 | quote! { |
| 2262 | #[inline(always)] |
| 2263 | pub fn get_ptr_to_func() -> Option<extern "C" fn (*const i32) -> *const i32> { |
| 2264 | unsafe { crate::detail::__rust_thunk___Z15get_ptr_to_funcv() } |
| 2265 | } |
| 2266 | } |
| 2267 | ); |
| 2268 | assert_rs_matches!( |
| 2269 | rs_api, |
| 2270 | quote! { |
| 2271 | mod detail { |
| 2272 | #[allow(unused_imports)] |
| 2273 | use super::*; |
| 2274 | extern "C" { |
| 2275 | #[link_name = "_Z15get_ptr_to_funcv"] |
| 2276 | pub(crate) fn __rust_thunk___Z15get_ptr_to_funcv() |
| 2277 | -> Option<extern "C" fn(*const i32) -> *const i32>; |
| 2278 | } |
| 2279 | } |
| 2280 | } |
| 2281 | ); |
| 2282 | // Verify that no C++ thunk got generated. |
| 2283 | assert_cc_not_matches!(rs_api_impl, quote! { __rust_thunk___Z15get_ptr_to_funcv }); |
| 2284 | |
| 2285 | // TODO(b/217419782): Add another test where params (and the return |
| 2286 | // type) are references with lifetimes. Something like this: |
| 2287 | // #pragma clang lifetime_elision |
| 2288 | // const int& (*get_ptr_to_func())(const int&, const int&); "#)?; |
| 2289 | // 1) Need to investigate why this fails - seeing raw pointers in Rust |
| 2290 | // seems to indicate that no lifetimes are present at the `importer.cc` |
| 2291 | // level. Maybe lifetime elision doesn't support this scenario? Unclear |
Googler | 53f6594 | 2022-02-23 11:23:30 +0000 | [diff] [blame^] | 2292 | // how to explicitly apply [[clang::annotate("lifetimes", "a, b -> a")]] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 2293 | // to the _inner_ function. |
| 2294 | // 2) It is important to have 2 reference parameters, so see if the problem |
| 2295 | // of passing `lifetimes` by value would have been caught - see: |
| 2296 | // cl/428079010/depot/rs_bindings_from_cc/ |
| 2297 | // importer.cc?version=s6#823 |
| 2298 | |
| 2299 | // TODO(b/217419782): Decide what to do if the C++ pointer is *not* |
| 2300 | // annotated with a lifetime - emit `unsafe fn(...) -> ...` in that |
| 2301 | // case? |
| 2302 | |
| 2303 | Ok(()) |
| 2304 | } |
| 2305 | |
| 2306 | #[test] |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 2307 | fn test_item_order() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2308 | let ir = ir_from_cc( |
| 2309 | "int first_func(); |
| 2310 | struct FirstStruct {}; |
| 2311 | int second_func(); |
| 2312 | struct SecondStruct {};", |
| 2313 | )?; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 2314 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2315 | let rs_api = rs_tokens_to_formatted_string(generate_rs_api(&ir)?)?; |
| 2316 | |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 2317 | let idx = |s: &str| rs_api.find(s).ok_or_else(|| anyhow!("'{}' missing", s)); |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 2318 | |
| 2319 | let f1 = idx("fn first_func")?; |
| 2320 | let f2 = idx("fn second_func")?; |
| 2321 | let s1 = idx("struct FirstStruct")?; |
| 2322 | let s2 = idx("struct SecondStruct")?; |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 2323 | let t1 = idx("fn __rust_thunk___Z10first_funcv")?; |
| 2324 | let t2 = idx("fn __rust_thunk___Z11second_funcv")?; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 2325 | |
| 2326 | assert!(f1 < s1); |
| 2327 | assert!(s1 < f2); |
| 2328 | assert!(f2 < s2); |
| 2329 | assert!(s2 < t1); |
| 2330 | assert!(t1 < t2); |
| 2331 | |
| 2332 | Ok(()) |
| 2333 | } |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 2334 | |
| 2335 | #[test] |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2336 | fn test_base_class_subobject_layout() -> Result<()> { |
| 2337 | let ir = ir_from_cc( |
| 2338 | r#" |
| 2339 | // We use a class here to force `Derived::z` to live inside the tail padding of `Base`. |
| 2340 | // On the Itanium ABI, this would not happen if `Base` were a POD type. |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 2341 | class Base {__INT64_TYPE__ x; char y;}; |
| 2342 | struct Derived final : Base {__INT16_TYPE__ z;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2343 | "#, |
| 2344 | )?; |
| 2345 | let rs_api = generate_rs_api(&ir)?; |
| 2346 | assert_rs_matches!( |
| 2347 | rs_api, |
| 2348 | quote! { |
| 2349 | #[repr(C, align(8))] |
| 2350 | pub struct Derived { |
| 2351 | __base_class_subobjects: [std::mem::MaybeUninit<u8>; 10], |
| 2352 | pub z: i16, |
| 2353 | } |
| 2354 | } |
| 2355 | ); |
| 2356 | Ok(()) |
| 2357 | } |
| 2358 | |
| 2359 | /// The same as test_base_class_subobject_layout, but with multiple |
| 2360 | /// inheritance. |
| 2361 | #[test] |
| 2362 | fn test_base_class_multiple_inheritance_subobject_layout() -> Result<()> { |
| 2363 | let ir = ir_from_cc( |
| 2364 | r#" |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 2365 | class Base1 {__INT64_TYPE__ x;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2366 | class Base2 {char y;}; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 2367 | struct Derived final : Base1, Base2 {__INT16_TYPE__ z;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2368 | "#, |
| 2369 | )?; |
| 2370 | let rs_api = generate_rs_api(&ir)?; |
| 2371 | assert_rs_matches!( |
| 2372 | rs_api, |
| 2373 | quote! { |
| 2374 | #[repr(C, align(8))] |
| 2375 | pub struct Derived { |
| 2376 | __base_class_subobjects: [std::mem::MaybeUninit<u8>; 10], |
| 2377 | pub z: i16, |
| 2378 | } |
| 2379 | } |
| 2380 | ); |
| 2381 | Ok(()) |
| 2382 | } |
| 2383 | |
| 2384 | /// The same as test_base_class_subobject_layout, but with a chain of |
| 2385 | /// inheritance. |
| 2386 | #[test] |
| 2387 | fn test_base_class_deep_inheritance_subobject_layout() -> Result<()> { |
| 2388 | let ir = ir_from_cc( |
| 2389 | r#" |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 2390 | class Base1 {__INT64_TYPE__ x;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2391 | class Base2 : Base1 {char y;}; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 2392 | struct Derived final : Base2 {__INT16_TYPE__ z;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2393 | "#, |
| 2394 | )?; |
| 2395 | let rs_api = generate_rs_api(&ir)?; |
| 2396 | assert_rs_matches!( |
| 2397 | rs_api, |
| 2398 | quote! { |
| 2399 | #[repr(C, align(8))] |
| 2400 | pub struct Derived { |
| 2401 | __base_class_subobjects: [std::mem::MaybeUninit<u8>; 10], |
| 2402 | pub z: i16, |
| 2403 | } |
| 2404 | } |
| 2405 | ); |
| 2406 | Ok(()) |
| 2407 | } |
| 2408 | |
| 2409 | /// For derived classes with no data members, we can't use the offset of the |
| 2410 | /// first member to determine the size of the base class subobjects. |
| 2411 | #[test] |
| 2412 | fn test_base_class_subobject_fieldless_layout() -> Result<()> { |
| 2413 | let ir = ir_from_cc( |
| 2414 | r#" |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 2415 | class Base {__INT64_TYPE__ x; char y;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2416 | struct Derived final : Base {}; |
| 2417 | "#, |
| 2418 | )?; |
| 2419 | let rs_api = generate_rs_api(&ir)?; |
| 2420 | assert_rs_matches!( |
| 2421 | rs_api, |
| 2422 | quote! { |
| 2423 | #[repr(C, align(8))] |
| 2424 | pub struct Derived { |
| 2425 | __base_class_subobjects: [std::mem::MaybeUninit<u8>; 9], |
| 2426 | } |
| 2427 | } |
| 2428 | ); |
| 2429 | Ok(()) |
| 2430 | } |
| 2431 | |
| 2432 | #[test] |
| 2433 | fn test_base_class_subobject_empty_fieldless() -> Result<()> { |
| 2434 | let ir = ir_from_cc( |
| 2435 | r#" |
| 2436 | class Base {}; |
| 2437 | struct Derived final : Base {}; |
| 2438 | "#, |
| 2439 | )?; |
| 2440 | let rs_api = generate_rs_api(&ir)?; |
| 2441 | assert_rs_matches!( |
| 2442 | rs_api, |
| 2443 | quote! { |
| 2444 | #[repr(C)] |
| 2445 | pub struct Derived { |
| 2446 | __base_class_subobjects: [std::mem::MaybeUninit<u8>; 0], |
| 2447 | /// Prevent empty C++ struct being zero-size in Rust. |
| 2448 | placeholder: std::mem::MaybeUninit<u8>, |
| 2449 | } |
| 2450 | } |
| 2451 | ); |
| 2452 | Ok(()) |
| 2453 | } |
| 2454 | |
| 2455 | #[test] |
| 2456 | fn test_base_class_subobject_empty() -> Result<()> { |
| 2457 | let ir = ir_from_cc( |
| 2458 | r#" |
| 2459 | class Base {}; |
| 2460 | struct Derived final : Base {}; |
| 2461 | "#, |
| 2462 | )?; |
| 2463 | let rs_api = generate_rs_api(&ir)?; |
| 2464 | assert_rs_matches!( |
| 2465 | rs_api, |
| 2466 | quote! { |
| 2467 | #[repr(C)] |
| 2468 | pub struct Derived { |
| 2469 | __base_class_subobjects: [std::mem::MaybeUninit<u8>; 0], |
| 2470 | /// Prevent empty C++ struct being zero-size in Rust. |
| 2471 | placeholder: std::mem::MaybeUninit<u8>, |
| 2472 | } |
| 2473 | } |
| 2474 | ); |
| 2475 | Ok(()) |
| 2476 | } |
| 2477 | |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 2478 | /// When a field is [[no_unique_address]], it occupies the space up to the |
| 2479 | /// next field. |
| 2480 | #[test] |
| 2481 | fn test_no_unique_address() -> Result<()> { |
| 2482 | let ir = ir_from_cc( |
| 2483 | r#" |
| 2484 | class Field1 {__INT64_TYPE__ x;}; |
| 2485 | class Field2 {char y;}; |
| 2486 | struct Struct final { |
| 2487 | [[no_unique_address]] Field1 field1; |
| 2488 | [[no_unique_address]] Field2 field2; |
| 2489 | __INT16_TYPE__ z; |
| 2490 | }; |
| 2491 | "#, |
| 2492 | )?; |
| 2493 | let rs_api = generate_rs_api(&ir)?; |
| 2494 | assert_rs_matches!( |
| 2495 | rs_api, |
| 2496 | quote! { |
| 2497 | #[derive(Clone, Copy)] |
| 2498 | #[repr(C, align(8))] |
| 2499 | pub struct Struct { |
| 2500 | field1: [std::mem::MaybeUninit<u8>; 8], |
| 2501 | field2: [std::mem::MaybeUninit<u8>; 2], |
| 2502 | pub z: i16, |
| 2503 | } |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 2504 | |
| 2505 | impl Struct { |
| 2506 | pub fn field1(&self) -> &Field1 { |
| 2507 | unsafe {&* (&self.field1 as *const _ as *const Field1)} |
| 2508 | } |
| 2509 | pub fn field2(&self) -> &Field2 { |
| 2510 | unsafe {&* (&self.field2 as *const _ as *const Field2)} |
| 2511 | } |
| 2512 | } |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 2513 | } |
| 2514 | ); |
| 2515 | Ok(()) |
| 2516 | } |
| 2517 | |
| 2518 | /// When a [[no_unique_address]] field is the last one, it occupies the rest |
| 2519 | /// of the object. |
| 2520 | #[test] |
| 2521 | fn test_no_unique_address_last_field() -> Result<()> { |
| 2522 | let ir = ir_from_cc( |
| 2523 | r#" |
| 2524 | class Field1 {__INT64_TYPE__ x;}; |
| 2525 | class Field2 {char y;}; |
| 2526 | struct Struct final { |
| 2527 | [[no_unique_address]] Field1 field1; |
| 2528 | [[no_unique_address]] Field2 field2; |
| 2529 | }; |
| 2530 | "#, |
| 2531 | )?; |
| 2532 | let rs_api = generate_rs_api(&ir)?; |
| 2533 | assert_rs_matches!( |
| 2534 | rs_api, |
| 2535 | quote! { |
| 2536 | #[derive(Clone, Copy)] |
| 2537 | #[repr(C, align(8))] |
| 2538 | pub struct Struct { |
| 2539 | field1: [std::mem::MaybeUninit<u8>; 8], |
| 2540 | field2: [std::mem::MaybeUninit<u8>; 8], |
| 2541 | } |
| 2542 | } |
| 2543 | ); |
| 2544 | Ok(()) |
| 2545 | } |
| 2546 | |
| 2547 | #[test] |
| 2548 | fn test_no_unique_address_empty() -> Result<()> { |
| 2549 | let ir = ir_from_cc( |
| 2550 | r#" |
| 2551 | class Field {}; |
| 2552 | struct Struct final { |
| 2553 | [[no_unique_address]] Field field; |
| 2554 | int x; |
| 2555 | }; |
| 2556 | "#, |
| 2557 | )?; |
| 2558 | let rs_api = generate_rs_api(&ir)?; |
| 2559 | assert_rs_matches!( |
| 2560 | rs_api, |
| 2561 | quote! { |
| 2562 | #[repr(C, align(4))] |
| 2563 | pub struct Struct { |
| 2564 | field: [std::mem::MaybeUninit<u8>; 0], |
| 2565 | pub x: i32, |
| 2566 | } |
| 2567 | } |
| 2568 | ); |
| 2569 | Ok(()) |
| 2570 | } |
| 2571 | |
| 2572 | #[test] |
| 2573 | fn test_base_class_subobject_empty_last_field() -> Result<()> { |
| 2574 | let ir = ir_from_cc( |
| 2575 | r#" |
| 2576 | class Field {}; |
| 2577 | struct Struct final { |
| 2578 | [[no_unique_address]] Field field; |
| 2579 | }; |
| 2580 | "#, |
| 2581 | )?; |
| 2582 | let rs_api = generate_rs_api(&ir)?; |
| 2583 | assert_rs_matches!( |
| 2584 | rs_api, |
| 2585 | quote! { |
| 2586 | #[repr(C)] |
| 2587 | pub struct Struct { |
| 2588 | field: [std::mem::MaybeUninit<u8>; 1], |
| 2589 | } |
| 2590 | } |
| 2591 | ); |
| 2592 | Ok(()) |
| 2593 | } |
| 2594 | |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2595 | #[test] |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 2596 | fn test_generate_enum_basic() -> Result<()> { |
| 2597 | let ir = ir_from_cc("enum Color { kRed = 5, kBlue };")?; |
| 2598 | let rs_api = generate_rs_api(&ir)?; |
| 2599 | assert_rs_matches!( |
| 2600 | rs_api, |
| 2601 | quote! { |
| 2602 | #[repr(transparent)] |
| 2603 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 2604 | pub struct Color(u32); |
| 2605 | impl Color { |
| 2606 | pub const kRed: Color = Color(5); |
| 2607 | pub const kBlue: Color = Color(6); |
| 2608 | } |
| 2609 | impl From<u32> for Color { |
| 2610 | fn from(value: u32) -> Color { |
| 2611 | Color(v) |
| 2612 | } |
| 2613 | } |
| 2614 | impl From<Color> for u32 { |
| 2615 | fn from(value: Color) -> u32 { |
| 2616 | v.0 |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | ); |
| 2621 | Ok(()) |
| 2622 | } |
| 2623 | |
| 2624 | #[test] |
| 2625 | fn test_generate_scoped_enum_basic() -> Result<()> { |
| 2626 | let ir = ir_from_cc("enum class Color { kRed = -5, kBlue };")?; |
| 2627 | let rs_api = generate_rs_api(&ir)?; |
| 2628 | assert_rs_matches!( |
| 2629 | rs_api, |
| 2630 | quote! { |
| 2631 | #[repr(transparent)] |
| 2632 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 2633 | pub struct Color(i32); |
| 2634 | impl Color { |
| 2635 | pub const kRed: Color = Color(-5); |
| 2636 | pub const kBlue: Color = Color(-4); |
| 2637 | } |
| 2638 | impl From<i32> for Color { |
| 2639 | fn from(value: i32) -> Color { |
| 2640 | Color(v) |
| 2641 | } |
| 2642 | } |
| 2643 | impl From<Color> for i32 { |
| 2644 | fn from(value: Color) -> i32 { |
| 2645 | v.0 |
| 2646 | } |
| 2647 | } |
| 2648 | } |
| 2649 | ); |
| 2650 | Ok(()) |
| 2651 | } |
| 2652 | |
| 2653 | #[test] |
| 2654 | fn test_generate_enum_with_64_bit_signed_vals() -> Result<()> { |
| 2655 | let ir = ir_from_cc( |
| 2656 | "enum Color : long { kViolet = -9223372036854775807 - 1LL, kRed = -5, kBlue, kGreen = 3, kMagenta = 9223372036854775807 };", |
| 2657 | )?; |
| 2658 | let rs_api = generate_rs_api(&ir)?; |
| 2659 | assert_rs_matches!( |
| 2660 | rs_api, |
| 2661 | quote! { |
| 2662 | #[repr(transparent)] |
| 2663 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 2664 | pub struct Color(i64); |
| 2665 | impl Color { |
| 2666 | pub const kViolet: Color = Color(-9223372036854775808); |
| 2667 | pub const kRed: Color = Color(-5); |
| 2668 | pub const kBlue: Color = Color(-4); |
| 2669 | pub const kGreen: Color = Color(3); |
| 2670 | pub const kMagenta: Color = Color(9223372036854775807); |
| 2671 | } |
| 2672 | impl From<i64> for Color { |
| 2673 | fn from(value: i64) -> Color { |
| 2674 | Color(v) |
| 2675 | } |
| 2676 | } |
| 2677 | impl From<Color> for i64 { |
| 2678 | fn from(value: Color) -> i64 { |
| 2679 | v.0 |
| 2680 | } |
| 2681 | } |
| 2682 | } |
| 2683 | ); |
| 2684 | Ok(()) |
| 2685 | } |
| 2686 | |
| 2687 | #[test] |
| 2688 | fn test_generate_enum_with_64_bit_unsigned_vals() -> Result<()> { |
| 2689 | let ir = ir_from_cc( |
| 2690 | "enum Color: unsigned long { kRed, kBlue, kLimeGreen = 18446744073709551615 };", |
| 2691 | )?; |
| 2692 | let rs_api = generate_rs_api(&ir)?; |
| 2693 | assert_rs_matches!( |
| 2694 | rs_api, |
| 2695 | quote! { |
| 2696 | #[repr(transparent)] |
| 2697 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 2698 | pub struct Color(u64); |
| 2699 | impl Color { |
| 2700 | pub const kRed: Color = Color(0); |
| 2701 | pub const kBlue: Color = Color(1); |
| 2702 | pub const kLimeGreen: Color = Color(18446744073709551615); |
| 2703 | } |
| 2704 | impl From<u64> for Color { |
| 2705 | fn from(value: u64) -> Color { |
| 2706 | Color(v) |
| 2707 | } |
| 2708 | } |
| 2709 | impl From<Color> for u64 { |
| 2710 | fn from(value: Color) -> u64 { |
| 2711 | v.0 |
| 2712 | } |
| 2713 | } |
| 2714 | } |
| 2715 | ); |
| 2716 | Ok(()) |
| 2717 | } |
| 2718 | |
| 2719 | #[test] |
| 2720 | fn test_generate_enum_with_32_bit_signed_vals() -> Result<()> { |
| 2721 | let ir = ir_from_cc( |
| 2722 | "enum Color { kViolet = -2147483647 - 1, kRed = -5, kBlue, kGreen = 3, kMagenta = 2147483647 };", |
| 2723 | )?; |
| 2724 | let rs_api = generate_rs_api(&ir)?; |
| 2725 | assert_rs_matches!( |
| 2726 | rs_api, |
| 2727 | quote! { |
| 2728 | #[repr(transparent)] |
| 2729 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 2730 | pub struct Color(i32); |
| 2731 | impl Color { |
| 2732 | pub const kViolet: Color = Color(-2147483648); |
| 2733 | pub const kRed: Color = Color(-5); |
| 2734 | pub const kBlue: Color = Color(-4); |
| 2735 | pub const kGreen: Color = Color(3); |
| 2736 | pub const kMagenta: Color = Color(2147483647); |
| 2737 | } |
| 2738 | impl From<i32> for Color { |
| 2739 | fn from(value: i32) -> Color { |
| 2740 | Color(v) |
| 2741 | } |
| 2742 | } |
| 2743 | impl From<Color> for i32 { |
| 2744 | fn from(value: Color) -> i32 { |
| 2745 | v.0 |
| 2746 | } |
| 2747 | } |
| 2748 | } |
| 2749 | ); |
| 2750 | Ok(()) |
| 2751 | } |
| 2752 | |
| 2753 | #[test] |
| 2754 | fn test_generate_enum_with_32_bit_unsigned_vals() -> Result<()> { |
| 2755 | let ir = ir_from_cc("enum Color: unsigned int { kRed, kBlue, kLimeGreen = 4294967295 };")?; |
| 2756 | let rs_api = generate_rs_api(&ir)?; |
| 2757 | assert_rs_matches!( |
| 2758 | rs_api, |
| 2759 | quote! { |
| 2760 | #[repr(transparent)] |
| 2761 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 2762 | pub struct Color(u32); |
| 2763 | impl Color { |
| 2764 | pub const kRed: Color = Color(0); |
| 2765 | pub const kBlue: Color = Color(1); |
| 2766 | pub const kLimeGreen: Color = Color(4294967295); |
| 2767 | } |
| 2768 | impl From<u32> for Color { |
| 2769 | fn from(value: u32) -> Color { |
| 2770 | Color(v) |
| 2771 | } |
| 2772 | } |
| 2773 | impl From<Color> for u32 { |
| 2774 | fn from(value: Color) -> u32 { |
| 2775 | v.0 |
| 2776 | } |
| 2777 | } |
| 2778 | } |
| 2779 | ); |
| 2780 | Ok(()) |
| 2781 | } |
| 2782 | |
| 2783 | #[test] |
Michael Forster | 409d941 | 2021-10-07 08:35:29 +0000 | [diff] [blame] | 2784 | fn test_doc_comment_func() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2785 | let ir = ir_from_cc( |
| 2786 | " |
| 2787 | // Doc Comment |
| 2788 | // with two lines |
| 2789 | int func();", |
| 2790 | )?; |
Michael Forster | 409d941 | 2021-10-07 08:35:29 +0000 | [diff] [blame] | 2791 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2792 | assert_rs_matches!( |
| 2793 | generate_rs_api(&ir)?, |
| 2794 | // leading space is intentional so there is a space between /// and the text of the |
| 2795 | // comment |
| 2796 | quote! { |
| 2797 | #[doc = " Doc Comment\n with two lines"] |
| 2798 | #[inline(always)] |
| 2799 | pub fn func |
| 2800 | } |
Michael Forster | 409d941 | 2021-10-07 08:35:29 +0000 | [diff] [blame] | 2801 | ); |
| 2802 | |
| 2803 | Ok(()) |
| 2804 | } |
| 2805 | |
| 2806 | #[test] |
| 2807 | fn test_doc_comment_record() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2808 | let ir = ir_from_cc( |
| 2809 | "// Doc Comment\n\ |
| 2810 | //\n\ |
| 2811 | // * with bullet\n\ |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 2812 | struct SomeStruct final {\n\ |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2813 | // Field doc\n\ |
| 2814 | int field;\ |
| 2815 | };", |
| 2816 | )?; |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 2817 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2818 | assert_rs_matches!( |
| 2819 | generate_rs_api(&ir)?, |
| 2820 | quote! { |
| 2821 | #[doc = " Doc Comment\n \n * with bullet"] |
| 2822 | #[derive(Clone, Copy)] |
| 2823 | #[repr(C)] |
| 2824 | pub struct SomeStruct { |
| 2825 | # [doc = " Field doc"] |
| 2826 | pub field: i32, |
| 2827 | } |
| 2828 | } |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 2829 | ); |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 2830 | Ok(()) |
| 2831 | } |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 2832 | |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 2833 | #[test] |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 2834 | fn test_unambiguous_public_bases() -> Result<()> { |
| 2835 | let ir = ir_from_cc_dependency( |
| 2836 | " |
| 2837 | struct VirtualBase {}; |
| 2838 | struct PrivateBase {}; |
| 2839 | struct ProtectedBase {}; |
| 2840 | struct UnambiguousPublicBase {}; |
| 2841 | struct AmbiguousPublicBase {}; |
| 2842 | struct MultipleInheritance : UnambiguousPublicBase, AmbiguousPublicBase {}; |
| 2843 | struct Derived : private PrivateBase, protected ProtectedBase, MultipleInheritance, AmbiguousPublicBase, virtual VirtualBase {}; |
| 2844 | ", |
| 2845 | "", |
| 2846 | )?; |
| 2847 | let rs_api = generate_rs_api(&ir)?; |
| 2848 | // TODO(b/216195042): virtual bases. |
| 2849 | assert_rs_not_matches!(rs_api, quote! { From<&'a Derived> for &'a VirtualBase }); |
| 2850 | assert_rs_matches!(rs_api, quote! { From<&'a Derived> for &'a UnambiguousPublicBase }); |
| 2851 | assert_rs_matches!(rs_api, quote! { From<&'a Derived> for &'a MultipleInheritance }); |
| 2852 | assert_rs_not_matches!(rs_api, quote! {From<&'a Derived> for &'a PrivateBase}); |
| 2853 | assert_rs_not_matches!(rs_api, quote! {From<&'a Derived> for &'a ProtectedBase}); |
| 2854 | assert_rs_not_matches!(rs_api, quote! {From<&'a Derived> for &'a AmbiguousPublicBase}); |
| 2855 | Ok(()) |
| 2856 | } |
| 2857 | |
| 2858 | /// Contrary to intuitions: a base class conversion is ambiguous even if the |
| 2859 | /// ambiguity is from a private base class cast that you can't even |
| 2860 | /// perform. |
| 2861 | /// |
| 2862 | /// Explanation (courtesy James Dennett): |
| 2863 | /// |
| 2864 | /// > Once upon a time, there was a rule in C++ that changing all access |
| 2865 | /// > specifiers to "public" would not change the meaning of code. |
| 2866 | /// > That's no longer true, but some of its effects can still be seen. |
| 2867 | /// |
| 2868 | /// So, we need to be sure to not allow casting to privately-ambiguous |
| 2869 | /// bases. |
| 2870 | #[test] |
| 2871 | fn test_unambiguous_public_bases_private_ambiguity() -> Result<()> { |
| 2872 | let ir = ir_from_cc_dependency( |
| 2873 | " |
| 2874 | struct Base {}; |
| 2875 | struct Intermediate : public Base {}; |
| 2876 | struct Derived : Base, private Intermediate {}; |
| 2877 | ", |
| 2878 | "", |
| 2879 | )?; |
| 2880 | let rs_api = generate_rs_api(&ir)?; |
| 2881 | assert_rs_not_matches!(rs_api, quote! { From<&'a Derived> for &'a Base }); |
| 2882 | Ok(()) |
| 2883 | } |
| 2884 | |
| 2885 | #[test] |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 2886 | fn test_virtual_thunk() -> Result<()> { |
| 2887 | let ir = ir_from_cc("struct Polymorphic { virtual void Foo(); };")?; |
| 2888 | |
| 2889 | assert_cc_matches!( |
| 2890 | generate_rs_api_impl(&ir)?, |
| 2891 | quote! { |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 2892 | extern "C" void __rust_thunk___ZN11Polymorphic3FooEv(class Polymorphic * __this) |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 2893 | } |
| 2894 | ); |
| 2895 | Ok(()) |
| 2896 | } |
| 2897 | |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 2898 | /// A trivially relocatable final struct is safe to use in Rust as normal, |
| 2899 | /// and is Unpin. |
| 2900 | #[test] |
| 2901 | fn test_no_negative_impl_unpin() -> Result<()> { |
| 2902 | let ir = ir_from_cc("struct Trivial final {};")?; |
| 2903 | let rs_api = generate_rs_api(&ir)?; |
| 2904 | assert_rs_not_matches!(rs_api, quote! {impl !Unpin}); |
| 2905 | Ok(()) |
| 2906 | } |
| 2907 | |
| 2908 | /// A non-final struct, even if it's trivial, is not usable by mut |
| 2909 | /// reference, and so is !Unpin. |
| 2910 | #[test] |
| 2911 | fn test_negative_impl_unpin_nonfinal() -> Result<()> { |
| 2912 | let ir = ir_from_cc("struct Nonfinal {};")?; |
| 2913 | let rs_api = generate_rs_api(&ir)?; |
| 2914 | assert_rs_matches!(rs_api, quote! {impl !Unpin for Nonfinal {}}); |
| 2915 | Ok(()) |
| 2916 | } |
| 2917 | |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 2918 | /// At the least, a trivial type should have no drop impl if or until we add |
| 2919 | /// empty drop impls. |
| 2920 | #[test] |
| 2921 | fn test_no_impl_drop() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 2922 | let ir = ir_from_cc("struct Trivial {};")?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2923 | let rs_api = rs_tokens_to_formatted_string(generate_rs_api(&ir)?)?; |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 2924 | assert!(!rs_api.contains("impl Drop")); |
| 2925 | Ok(()) |
| 2926 | } |
| 2927 | |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2928 | /// User-defined destructors *must* become Drop impls with ManuallyDrop |
| 2929 | /// fields |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 2930 | #[test] |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2931 | fn test_impl_drop_user_defined_destructor() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 2932 | let ir = ir_from_cc( |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2933 | r#" struct NontrivialStruct { ~NontrivialStruct(); }; |
| 2934 | struct UserDefinedDestructor { |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 2935 | ~UserDefinedDestructor(); |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2936 | int x; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2937 | NontrivialStruct nts; |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 2938 | };"#, |
| 2939 | )?; |
| 2940 | let rs_api = generate_rs_api(&ir)?; |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 2941 | assert_rs_matches!( |
| 2942 | rs_api, |
| 2943 | quote! { |
| 2944 | impl Drop for UserDefinedDestructor { |
| 2945 | #[inline(always)] |
| 2946 | fn drop(&mut self) { |
| 2947 | unsafe { crate::detail::__rust_thunk___ZN21UserDefinedDestructorD1Ev(self) } |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | ); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2952 | assert_rs_matches!(rs_api, quote! {pub x: i32,}); |
| 2953 | assert_rs_matches!(rs_api, quote! {pub nts: std::mem::ManuallyDrop<NontrivialStruct>,}); |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2954 | Ok(()) |
| 2955 | } |
| 2956 | |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 2957 | /// nontrivial types without user-defined destructors should invoke |
| 2958 | /// the C++ destructor to preserve the order of field destructions. |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2959 | #[test] |
| 2960 | fn test_impl_drop_nontrivial_member_destructor() -> Result<()> { |
| 2961 | // TODO(jeanpierreda): This would be cleaner if the UserDefinedDestructor code were |
| 2962 | // omitted. For example, we simulate it so that UserDefinedDestructor |
| 2963 | // comes from another library. |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 2964 | let ir = ir_from_cc( |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 2965 | r#"struct UserDefinedDestructor final { |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2966 | ~UserDefinedDestructor(); |
| 2967 | }; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 2968 | struct TrivialStruct final { int i; }; |
| 2969 | struct NontrivialMembers final { |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2970 | UserDefinedDestructor udd; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2971 | TrivialStruct ts; |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2972 | int x; |
| 2973 | };"#, |
| 2974 | )?; |
| 2975 | let rs_api = generate_rs_api(&ir)?; |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 2976 | assert_rs_matches!( |
| 2977 | rs_api, |
| 2978 | quote! { |
| 2979 | impl Drop for NontrivialMembers { |
| 2980 | #[inline(always)] |
| 2981 | fn drop(&mut self) { |
| 2982 | unsafe { crate::detail::__rust_thunk___ZN17NontrivialMembersD1Ev(self) } |
| 2983 | } |
| 2984 | } |
| 2985 | } |
| 2986 | ); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2987 | assert_rs_matches!(rs_api, quote! {pub x: i32,}); |
| 2988 | assert_rs_matches!(rs_api, quote! {pub ts: TrivialStruct,}); |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 2989 | assert_rs_matches!( |
| 2990 | rs_api, |
| 2991 | quote! {pub udd: std::mem::ManuallyDrop<UserDefinedDestructor>,} |
| 2992 | ); |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2993 | Ok(()) |
| 2994 | } |
| 2995 | |
| 2996 | /// Trivial types (at least those that are mapped to Copy rust types) do not |
| 2997 | /// get a Drop impl. |
| 2998 | #[test] |
| 2999 | fn test_impl_drop_trivial() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 3000 | let ir = ir_from_cc( |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 3001 | r#"struct Trivial final { |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 3002 | ~Trivial() = default; |
| 3003 | int x; |
| 3004 | };"#, |
| 3005 | )?; |
| 3006 | let rs_api = generate_rs_api(&ir)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3007 | assert_rs_not_matches!(rs_api, quote! {impl Drop}); |
| 3008 | assert_rs_matches!(rs_api, quote! {pub x: i32}); |
Lukasz Anforowicz | 2f07416 | 2022-01-06 22:50:51 +0000 | [diff] [blame] | 3009 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 3010 | // TODO(b/213326125): Avoid generating thunk impls that are never called. |
| 3011 | // (The test assertion below should be reversed once this bug is fixed.) |
| 3012 | assert_cc_matches!(rs_api_impl, quote! { std::destroy_at }); |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 3013 | Ok(()) |
| 3014 | } |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 3015 | |
| 3016 | #[test] |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 3017 | fn test_impl_default_explicitly_defaulted_constructor() -> Result<()> { |
| 3018 | let ir = ir_from_cc( |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 3019 | r#"#pragma clang lifetime_elision |
| 3020 | struct DefaultedConstructor final { |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 3021 | DefaultedConstructor() = default; |
| 3022 | };"#, |
| 3023 | )?; |
| 3024 | let rs_api = generate_rs_api(&ir)?; |
| 3025 | assert_rs_matches!( |
| 3026 | rs_api, |
| 3027 | quote! { |
| 3028 | impl Default for DefaultedConstructor { |
| 3029 | #[inline(always)] |
| 3030 | fn default() -> Self { |
Lukasz Anforowicz | bedbdee | 2022-01-05 01:14:52 +0000 | [diff] [blame] | 3031 | let mut tmp = std::mem::MaybeUninit::<Self>::zeroed(); |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 3032 | unsafe { |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 3033 | crate::detail::__rust_thunk___ZN20DefaultedConstructorC1Ev(&mut tmp); |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 3034 | tmp.assume_init() |
| 3035 | } |
| 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | ); |
| 3040 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 3041 | assert_cc_matches!( |
| 3042 | rs_api_impl, |
| 3043 | quote! { |
| 3044 | extern "C" void __rust_thunk___ZN20DefaultedConstructorC1Ev( |
Googler | 972d358 | 2022-01-11 10:17:22 +0000 | [diff] [blame] | 3045 | class DefaultedConstructor* __this) { |
Lukasz Anforowicz | 4457baf | 2021-12-23 17:24:04 +0000 | [diff] [blame] | 3046 | rs_api_impl_support::construct_at (__this) ; |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | ); |
| 3050 | Ok(()) |
| 3051 | } |
| 3052 | |
| 3053 | #[test] |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 3054 | fn test_impl_clone_that_propagates_lifetime() -> Result<()> { |
| 3055 | // This test covers the case where a single lifetime applies to 1) |
| 3056 | // the `__this` parameter and 2) other constructor parameters. For |
| 3057 | // example, maybe the newly constructed object needs to have the |
| 3058 | // same lifetime as the constructor's parameter. (This might require |
| 3059 | // annotating the whole C++ struct with a lifetime, so maybe the |
| 3060 | // example below is not fully realistic/accurate...). |
| 3061 | let mut ir = ir_from_cc( |
| 3062 | r#"#pragma clang lifetime_elision |
| 3063 | struct Foo final { |
Googler | 53f6594 | 2022-02-23 11:23:30 +0000 | [diff] [blame^] | 3064 | [[clang::annotate("lifetimes", "a: a")]] |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 3065 | Foo(const int& i); |
| 3066 | };"#, |
| 3067 | )?; |
| 3068 | let ctor: &mut Func = ir |
| 3069 | .items_mut() |
| 3070 | .filter_map(|item| match item { |
| 3071 | Item::Func(func) => Some(func), |
| 3072 | _ => None, |
| 3073 | }) |
| 3074 | .find(|f| { |
| 3075 | matches!(&f.name, UnqualifiedIdentifier::Constructor) |
| 3076 | && f.params.get(1).map(|p| p.identifier.identifier == "i").unwrap_or_default() |
| 3077 | }) |
| 3078 | .unwrap(); |
| 3079 | { |
| 3080 | // Double-check that the test scenario set up above uses the same lifetime |
| 3081 | // for both of the constructor's parameters: `__this` and `i`. |
| 3082 | assert_eq!(ctor.params.len(), 2); |
| 3083 | let this_lifetime: LifetimeId = |
| 3084 | *ctor.params[0].type_.rs_type.lifetime_args.first().unwrap(); |
| 3085 | let i_lifetime: LifetimeId = |
| 3086 | *ctor.params[1].type_.rs_type.lifetime_args.first_mut().unwrap(); |
| 3087 | assert_eq!(i_lifetime, this_lifetime); |
| 3088 | } |
| 3089 | |
| 3090 | // Before cl/423346348 the generated Rust code would incorrectly look |
| 3091 | // like this (note the mismatched 'a and 'b lifetimes): |
| 3092 | // fn from<'b>(i: &'a i32) -> Self |
| 3093 | // After this CL, this scenario will result in an explicit error. |
| 3094 | let err = generate_rs_api(&ir).unwrap_err(); |
| 3095 | let msg = format!("{}", err); |
| 3096 | assert!( |
| 3097 | msg.contains("The lifetime of `__this` is unexpectedly also used by another parameter") |
| 3098 | ); |
| 3099 | Ok(()) |
| 3100 | } |
| 3101 | |
| 3102 | #[test] |
Lukasz Anforowicz | 9bab835 | 2021-12-22 17:35:31 +0000 | [diff] [blame] | 3103 | fn test_impl_default_non_trivial_struct() -> Result<()> { |
| 3104 | let ir = ir_from_cc( |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 3105 | r#"#pragma clang lifetime_elision |
| 3106 | struct NonTrivialStructWithConstructors final { |
Lukasz Anforowicz | 9bab835 | 2021-12-22 17:35:31 +0000 | [diff] [blame] | 3107 | NonTrivialStructWithConstructors(); |
| 3108 | ~NonTrivialStructWithConstructors(); // Non-trivial |
| 3109 | };"#, |
| 3110 | )?; |
| 3111 | let rs_api = generate_rs_api(&ir)?; |
| 3112 | assert_rs_not_matches!(rs_api, quote! {impl Default}); |
| 3113 | Ok(()) |
| 3114 | } |
| 3115 | |
| 3116 | #[test] |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 3117 | fn test_impl_from_for_explicit_conversion_constructor() -> Result<()> { |
| 3118 | let ir = ir_from_cc( |
| 3119 | r#"#pragma clang lifetime_elision |
| 3120 | struct SomeStruct final { |
| 3121 | explicit SomeStruct(int i); |
| 3122 | };"#, |
| 3123 | )?; |
| 3124 | let rs_api = generate_rs_api(&ir)?; |
| 3125 | // As discussed in b/214020567 for now we only generate `From::from` bindings |
| 3126 | // for *implicit* C++ conversion constructors. |
| 3127 | assert_rs_not_matches!(rs_api, quote! {impl From}); |
| 3128 | Ok(()) |
| 3129 | } |
| 3130 | |
| 3131 | #[test] |
| 3132 | fn test_impl_from_for_implicit_conversion_constructor() -> Result<()> { |
| 3133 | let ir = ir_from_cc( |
| 3134 | r#"#pragma clang lifetime_elision |
| 3135 | struct SomeStruct final { |
| 3136 | SomeStruct(int i); // implicit - no `explicit` keyword |
| 3137 | };"#, |
| 3138 | )?; |
| 3139 | let rs_api = generate_rs_api(&ir)?; |
| 3140 | // As discussed in b/214020567 we generate `From::from` bindings for |
| 3141 | // *implicit* C++ conversion constructors. |
| 3142 | assert_rs_matches!( |
| 3143 | rs_api, |
| 3144 | quote! { |
| 3145 | impl From<i32> for SomeStruct { |
| 3146 | #[inline(always)] |
| 3147 | fn from(i: i32) -> Self { |
| 3148 | let mut tmp = std::mem::MaybeUninit::<Self>::zeroed(); |
| 3149 | unsafe { |
| 3150 | crate::detail::__rust_thunk___ZN10SomeStructC1Ei(&mut tmp, i); |
| 3151 | tmp.assume_init() |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | } |
| 3156 | ); |
| 3157 | Ok(()) |
| 3158 | } |
| 3159 | |
| 3160 | #[test] |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 3161 | fn test_impl_eq_for_member_function() -> Result<()> { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 3162 | let ir = ir_from_cc( |
| 3163 | r#"#pragma clang lifetime_elision |
| 3164 | struct SomeStruct final { |
| 3165 | inline bool operator==(const SomeStruct& other) const { |
| 3166 | return i == other.i; |
| 3167 | } |
| 3168 | int i; |
| 3169 | };"#, |
| 3170 | )?; |
| 3171 | let rs_api = generate_rs_api(&ir)?; |
| 3172 | assert_rs_matches!( |
| 3173 | rs_api, |
| 3174 | quote! { |
| 3175 | impl PartialEq<SomeStruct> for SomeStruct { |
| 3176 | #[inline(always)] |
| 3177 | fn eq<'a, 'b>(&'a self, other: &'b SomeStruct) -> bool { |
| 3178 | unsafe { crate::detail::__rust_thunk___ZNK10SomeStructeqERKS_(self, other) } |
| 3179 | } |
| 3180 | } |
| 3181 | } |
| 3182 | ); |
| 3183 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 3184 | assert_cc_matches!( |
| 3185 | rs_api_impl, |
| 3186 | quote! { |
| 3187 | extern "C" bool __rust_thunk___ZNK10SomeStructeqERKS_( |
| 3188 | const class SomeStruct* __this, const class SomeStruct& other) { |
| 3189 | return __this->operator==(other); |
| 3190 | } |
| 3191 | } |
| 3192 | ); |
| 3193 | Ok(()) |
| 3194 | } |
| 3195 | |
| 3196 | #[test] |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 3197 | fn test_impl_eq_for_free_function() -> Result<()> { |
| 3198 | let ir = ir_from_cc( |
| 3199 | r#"#pragma clang lifetime_elision |
| 3200 | struct SomeStruct final { int i; }; |
| 3201 | bool operator==(const SomeStruct& lhs, const SomeStruct& rhs) { |
| 3202 | return lhs.i == rhs.i; |
| 3203 | }"#, |
| 3204 | )?; |
| 3205 | let rs_api = generate_rs_api(&ir)?; |
| 3206 | assert_rs_matches!( |
| 3207 | rs_api, |
| 3208 | quote! { |
| 3209 | impl PartialEq<SomeStruct> for SomeStruct { |
| 3210 | #[inline(always)] |
| 3211 | fn eq<'a, 'b>(&'a self, rhs: &'b SomeStruct) -> bool { |
| 3212 | unsafe { crate::detail::__rust_thunk___ZeqRK10SomeStructS1_(self, rhs) } |
| 3213 | } |
| 3214 | } |
| 3215 | } |
| 3216 | ); |
| 3217 | Ok(()) |
| 3218 | } |
| 3219 | |
| 3220 | #[test] |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 3221 | fn test_impl_eq_non_const_member_function() -> Result<()> { |
| 3222 | let ir = ir_from_cc( |
| 3223 | r#"#pragma clang lifetime_elision |
| 3224 | struct SomeStruct final { |
| 3225 | bool operator==(const SomeStruct& other) /* no `const` here */; |
| 3226 | };"#, |
| 3227 | )?; |
| 3228 | let rs_api = generate_rs_api(&ir)?; |
| 3229 | assert_rs_not_matches!(rs_api, quote! {impl PartialEq}); |
| 3230 | Ok(()) |
| 3231 | } |
| 3232 | |
| 3233 | #[test] |
| 3234 | fn test_impl_eq_rhs_by_value() -> Result<()> { |
| 3235 | let ir = ir_from_cc( |
| 3236 | r#"#pragma clang lifetime_elision |
| 3237 | struct SomeStruct final { |
| 3238 | bool operator==(SomeStruct other) const; |
| 3239 | };"#, |
| 3240 | )?; |
| 3241 | let rs_api = generate_rs_api(&ir)?; |
| 3242 | assert_rs_not_matches!(rs_api, quote! {impl PartialEq}); |
| 3243 | Ok(()) |
| 3244 | } |
| 3245 | |
| 3246 | #[test] |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 3247 | fn test_thunk_ident_function() { |
| 3248 | let func = ir_func("foo"); |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 3249 | assert_eq!(thunk_ident(&func), make_rs_ident("__rust_thunk___Z3foov")); |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | #[test] |
| 3253 | fn test_thunk_ident_special_names() { |
Marcel Hlopko | 4b13b96 | 2021-12-06 12:40:56 +0000 | [diff] [blame] | 3254 | let ir = ir_from_cc("struct Class {};").unwrap(); |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 3255 | |
Googler | 45ad275 | 2021-12-06 12:12:35 +0000 | [diff] [blame] | 3256 | let destructor = |
| 3257 | ir.functions().find(|f| f.name == UnqualifiedIdentifier::Destructor).unwrap(); |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 3258 | assert_eq!(thunk_ident(destructor), make_rs_ident("__rust_thunk___ZN5ClassD1Ev")); |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 3259 | |
Lukasz Anforowicz | 49b5bbc | 2022-02-04 23:40:10 +0000 | [diff] [blame] | 3260 | let default_constructor = ir |
| 3261 | .functions() |
| 3262 | .find(|f| f.name == UnqualifiedIdentifier::Constructor && f.params.len() == 1) |
| 3263 | .unwrap(); |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 3264 | assert_eq!(thunk_ident(default_constructor), make_rs_ident("__rust_thunk___ZN5ClassC1Ev")); |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 3265 | } |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 3266 | |
| 3267 | #[test] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3268 | fn test_elided_lifetimes() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 3269 | let ir = ir_from_cc( |
| 3270 | r#"#pragma clang lifetime_elision |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 3271 | struct S final { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 3272 | int& f(int& i); |
| 3273 | };"#, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3274 | )?; |
| 3275 | let rs_api = generate_rs_api(&ir)?; |
| 3276 | assert_rs_matches!( |
| 3277 | rs_api, |
| 3278 | quote! { |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 3279 | pub fn f<'a, 'b>(&'a mut self, i: &'b mut i32) -> &'a mut i32 { ... } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3280 | } |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 3281 | ); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3282 | assert_rs_matches!( |
| 3283 | rs_api, |
| 3284 | quote! { |
Googler | 6804a01 | 2022-01-05 07:04:36 +0000 | [diff] [blame] | 3285 | pub(crate) fn __rust_thunk___ZN1S1fERi<'a, 'b>(__this: &'a mut S, i: &'b mut i32) |
| 3286 | -> &'a mut i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3287 | } |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 3288 | ); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3289 | Ok(()) |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 3290 | } |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 3291 | |
| 3292 | #[test] |
| 3293 | fn test_format_generic_params() -> Result<()> { |
| 3294 | assert_rs_matches!(format_generic_params(std::iter::empty::<syn::Ident>()), quote! {}); |
| 3295 | |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 3296 | let idents = ["T1", "T2"].iter().map(|s| make_rs_ident(s)); |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 3297 | assert_rs_matches!(format_generic_params(idents), quote! { < T1, T2 > }); |
| 3298 | |
| 3299 | let lifetimes = ["a", "b"] |
| 3300 | .iter() |
| 3301 | .map(|s| syn::Lifetime::new(&format!("'{}", s), proc_macro2::Span::call_site())); |
| 3302 | assert_rs_matches!(format_generic_params(lifetimes), quote! { < 'a, 'b > }); |
| 3303 | |
| 3304 | Ok(()) |
| 3305 | } |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 3306 | |
| 3307 | #[test] |
| 3308 | fn test_overloaded_functions() -> Result<()> { |
| 3309 | // TODO(b/213280424): We don't support creating bindings for overloaded |
| 3310 | // functions yet, except in the case of overloaded constructors with a |
| 3311 | // single parameter. |
| 3312 | let ir = ir_from_cc( |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 3313 | r#" #pragma clang lifetime_elision |
| 3314 | void f(); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 3315 | void f(int i); |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 3316 | struct S1 final { |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 3317 | void f(); |
| 3318 | void f(int i); |
| 3319 | }; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 3320 | struct S2 final { |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 3321 | void f(); |
| 3322 | }; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 3323 | struct S3 final { |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 3324 | S3(int i); |
| 3325 | S3(double d); |
| 3326 | }; |
| 3327 | "#, |
| 3328 | )?; |
| 3329 | let rs_api = generate_rs_api(&ir)?; |
| 3330 | let rs_api_str = tokens_to_string(rs_api.clone())?; |
| 3331 | |
| 3332 | // Cannot overload free functions. |
| 3333 | assert!(rs_api_str.contains("Error while generating bindings for item 'f'")); |
| 3334 | assert_rs_not_matches!(rs_api, quote! {pub fn f()}); |
| 3335 | assert_rs_not_matches!(rs_api, quote! {pub fn f(i: i32)}); |
| 3336 | |
| 3337 | // Cannot overload member functions. |
| 3338 | assert!(rs_api_str.contains("Error while generating bindings for item 'S1::f'")); |
| 3339 | assert_rs_not_matches!(rs_api, quote! {pub fn f(... S1 ...)}); |
| 3340 | |
| 3341 | // But we can import member functions that have the same name as a free |
| 3342 | // function. |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 3343 | assert_rs_matches!(rs_api, quote! {pub fn f<'a>(&'a mut self)}); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 3344 | |
| 3345 | // We can also import overloaded single-parameter constructors. |
| 3346 | assert_rs_matches!(rs_api, quote! {impl From<i32> for S3}); |
| 3347 | assert_rs_matches!(rs_api, quote! {impl From<f64> for S3}); |
| 3348 | Ok(()) |
| 3349 | } |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 3350 | |
| 3351 | #[test] |
| 3352 | fn test_type_alias() -> Result<()> { |
| 3353 | let ir = ir_from_cc( |
| 3354 | r#" |
| 3355 | typedef int MyTypedefDecl; |
| 3356 | using MyTypeAliasDecl = int; |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3357 | using MyTypeAliasDecl_Alias = MyTypeAliasDecl; |
| 3358 | |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 3359 | struct S final {}; |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3360 | using S_Alias = S; |
| 3361 | using S_Alias_Alias = S_Alias; |
| 3362 | |
| 3363 | inline void f(MyTypedefDecl t) {} |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 3364 | "#, |
| 3365 | )?; |
| 3366 | let rs_api = generate_rs_api(&ir)?; |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3367 | assert_rs_matches!(rs_api, quote! { pub type MyTypedefDecl = i32; }); |
| 3368 | assert_rs_matches!(rs_api, quote! { pub type MyTypeAliasDecl = i32; }); |
| 3369 | assert_rs_matches!(rs_api, quote! { pub type MyTypeAliasDecl_Alias = MyTypeAliasDecl; }); |
| 3370 | assert_rs_matches!(rs_api, quote! { pub type S_Alias = S; }); |
| 3371 | assert_rs_matches!(rs_api, quote! { pub type S_Alias_Alias = S_Alias; }); |
| 3372 | assert_rs_matches!(rs_api, quote! { pub fn f(t: MyTypedefDecl) }); |
| 3373 | assert_cc_matches!( |
| 3374 | generate_rs_api_impl(&ir)?, |
| 3375 | quote! { |
| 3376 | extern "C" void __rust_thunk___Z1fi(MyTypedefDecl t){ f (t) ; } |
| 3377 | } |
| 3378 | ); |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 3379 | Ok(()) |
| 3380 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3381 | |
| 3382 | #[test] |
| 3383 | fn test_rs_type_kind_implements_copy() -> Result<()> { |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3384 | let template = r#" LIFETIMES |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 3385 | struct [[clang::trivial_abi]] TrivialStruct final { int i; }; |
| 3386 | struct [[clang::trivial_abi]] UserDefinedCopyConstructor final { |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3387 | UserDefinedCopyConstructor(const UserDefinedCopyConstructor&); |
| 3388 | }; |
| 3389 | using IntAlias = int; |
| 3390 | using TrivialAlias = TrivialStruct; |
| 3391 | using NonTrivialAlias = UserDefinedCopyConstructor; |
| 3392 | void func(PARAM_TYPE some_param); |
| 3393 | "#; |
| 3394 | assert_impl_all!(i32: Copy); |
| 3395 | assert_impl_all!(&i32: Copy); |
| 3396 | assert_not_impl_all!(&mut i32: Copy); |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3397 | assert_impl_all!(Option<&i32>: Copy); |
| 3398 | assert_not_impl_all!(Option<&mut i32>: Copy); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3399 | assert_impl_all!(*const i32: Copy); |
| 3400 | assert_impl_all!(*mut i32: Copy); |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3401 | struct Test { |
| 3402 | // Test inputs: |
| 3403 | cc: &'static str, |
| 3404 | lifetimes: bool, |
| 3405 | // Expected test outputs: |
| 3406 | rs: &'static str, |
| 3407 | is_copy: bool, |
| 3408 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3409 | let tests = vec![ |
| 3410 | // Validity of the next few tests is verified via |
| 3411 | // `assert_[not_]impl_all!` static assertions above. |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3412 | Test { cc: "int", lifetimes: true, rs: "i32", is_copy: true }, |
| 3413 | Test { cc: "const int&", lifetimes: true, rs: "&'a i32", is_copy: true }, |
| 3414 | Test { cc: "int&", lifetimes: true, rs: "&'a mut i32", is_copy: false }, |
| 3415 | Test { cc: "const int*", lifetimes: true, rs: "Option<&'a i32>", is_copy: true }, |
| 3416 | Test { cc: "int*", lifetimes: true, rs: "Option<&'a mut i32>", is_copy: false }, |
| 3417 | Test { cc: "const int*", lifetimes: false, rs: "*const i32", is_copy: true }, |
| 3418 | Test { cc: "int*", lifetimes: false, rs: "*mut i32", is_copy: true }, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3419 | // Tests below have been thought-through and verified "manually". |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3420 | // TrivialStruct is expected to derive Copy. |
| 3421 | Test { cc: "TrivialStruct", lifetimes: true, rs: "TrivialStruct", is_copy: true }, |
| 3422 | Test { |
| 3423 | cc: "UserDefinedCopyConstructor", |
| 3424 | lifetimes: true, |
| 3425 | rs: "UserDefinedCopyConstructor", |
| 3426 | is_copy: false, |
| 3427 | }, |
| 3428 | Test { cc: "IntAlias", lifetimes: true, rs: "IntAlias", is_copy: true }, |
| 3429 | Test { cc: "TrivialAlias", lifetimes: true, rs: "TrivialAlias", is_copy: true }, |
| 3430 | Test { cc: "NonTrivialAlias", lifetimes: true, rs: "NonTrivialAlias", is_copy: false }, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3431 | ]; |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3432 | for test in tests.iter() { |
| 3433 | let test_name = format!("cc='{}', lifetimes={}", test.cc, test.lifetimes); |
| 3434 | let cc_input = template.replace("PARAM_TYPE", test.cc).replace( |
| 3435 | "LIFETIMES", |
| 3436 | if test.lifetimes { "#pragma clang lifetime_elision" } else { "" }, |
| 3437 | ); |
| 3438 | let ir = ir_from_cc(&cc_input)?; |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 3439 | let f = retrieve_func(&ir, "func"); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3440 | let t = RsTypeKind::new(&f.params[0].type_.rs_type, &ir)?; |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3441 | |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3442 | let lifetime_to_name: HashMap::<LifetimeId, String> = t.lifetimes().map( |
| 3443 | |lifetime_id| (lifetime_id, "a".to_string())).collect(); |
| 3444 | |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3445 | let fmt = tokens_to_string(t.format(&ir, &lifetime_to_name)?)?; |
| 3446 | assert_eq!(test.rs, fmt, "Testing: {}", test_name); |
| 3447 | |
| 3448 | assert_eq!(test.is_copy, t.implements_copy(), "Testing: {}", test_name); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3449 | } |
| 3450 | Ok(()) |
| 3451 | } |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 3452 | |
| 3453 | #[test] |
| 3454 | fn test_rs_type_kind_is_shared_ref_to_with_lifetimes() -> Result<()> { |
| 3455 | let ir = ir_from_cc( |
| 3456 | "#pragma clang lifetime_elision |
| 3457 | struct SomeStruct {}; |
| 3458 | void foo(const SomeStruct& foo_param); |
| 3459 | void bar(SomeStruct& bar_param);", |
| 3460 | )?; |
| 3461 | let record = ir.records().next().unwrap(); |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 3462 | let foo_func = retrieve_func(&ir, "foo"); |
| 3463 | let bar_func = retrieve_func(&ir, "bar"); |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 3464 | |
| 3465 | // const-ref + lifetimes in C++ ===> shared-ref in Rust |
| 3466 | assert_eq!(foo_func.params.len(), 1); |
| 3467 | let foo_param = &foo_func.params[0]; |
| 3468 | assert_eq!(&foo_param.identifier.identifier, "foo_param"); |
| 3469 | let foo_type = RsTypeKind::new(&foo_param.type_.rs_type, &ir)?; |
| 3470 | assert!(foo_type.is_shared_ref_to(record)); |
| 3471 | assert!(matches!(foo_type, RsTypeKind::Reference { mutability: Mutability::Const, .. })); |
| 3472 | |
| 3473 | // non-const-ref + lifetimes in C++ ===> mutable-ref in Rust |
| 3474 | assert_eq!(bar_func.params.len(), 1); |
| 3475 | let bar_param = &bar_func.params[0]; |
| 3476 | assert_eq!(&bar_param.identifier.identifier, "bar_param"); |
| 3477 | let bar_type = RsTypeKind::new(&bar_param.type_.rs_type, &ir)?; |
| 3478 | assert!(!bar_type.is_shared_ref_to(record)); |
| 3479 | assert!(matches!(bar_type, RsTypeKind::Reference { mutability: Mutability::Mut, .. })); |
| 3480 | |
| 3481 | Ok(()) |
| 3482 | } |
| 3483 | |
| 3484 | #[test] |
| 3485 | fn test_rs_type_kind_is_shared_ref_to_without_lifetimes() -> Result<()> { |
| 3486 | let ir = ir_from_cc( |
| 3487 | "struct SomeStruct {}; |
| 3488 | void foo(const SomeStruct& foo_param);", |
| 3489 | )?; |
| 3490 | let record = ir.records().next().unwrap(); |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 3491 | let foo_func = retrieve_func(&ir, "foo"); |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 3492 | |
| 3493 | // const-ref + *no* lifetimes in C++ ===> const-pointer in Rust |
| 3494 | assert_eq!(foo_func.params.len(), 1); |
| 3495 | let foo_param = &foo_func.params[0]; |
| 3496 | assert_eq!(&foo_param.identifier.identifier, "foo_param"); |
| 3497 | let foo_type = RsTypeKind::new(&foo_param.type_.rs_type, &ir)?; |
| 3498 | assert!(!foo_type.is_shared_ref_to(record)); |
| 3499 | assert!(matches!(foo_type, RsTypeKind::Pointer { mutability: Mutability::Const, .. })); |
| 3500 | |
| 3501 | Ok(()) |
| 3502 | } |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 3503 | |
| 3504 | #[test] |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3505 | fn test_rs_type_kind_dfs_iter_ordering() { |
| 3506 | // Set up a test input representing: A<B<C>, D<E>>. |
| 3507 | let a = { |
| 3508 | let b = { |
| 3509 | let c = RsTypeKind::Other { name: "C", type_args: vec![] }; |
| 3510 | RsTypeKind::Other { name: "B", type_args: vec![c] } |
| 3511 | }; |
| 3512 | let d = { |
| 3513 | let e = RsTypeKind::Other { name: "E", type_args: vec![] }; |
| 3514 | RsTypeKind::Other { name: "D", type_args: vec![e] } |
| 3515 | }; |
| 3516 | RsTypeKind::Other { name: "A", type_args: vec![b, d] } |
| 3517 | }; |
| 3518 | let dfs_names = a |
| 3519 | .dfs_iter() |
| 3520 | .map(|t| match t { |
| 3521 | RsTypeKind::Other { name, .. } => *name, |
| 3522 | _ => unreachable!("Only 'other' types are used in this test"), |
| 3523 | }) |
| 3524 | .collect_vec(); |
| 3525 | assert_eq!(vec!["A", "B", "C", "D", "E"], dfs_names); |
| 3526 | } |
| 3527 | |
| 3528 | #[test] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 3529 | fn test_rs_type_kind_dfs_iter_ordering_for_func_ptr() { |
| 3530 | // Set up a test input representing: fn(A, B) -> C |
| 3531 | let f = { |
| 3532 | let a = RsTypeKind::Other { name: "A", type_args: vec![] }; |
| 3533 | let b = RsTypeKind::Other { name: "B", type_args: vec![] }; |
| 3534 | let c = RsTypeKind::Other { name: "C", type_args: vec![] }; |
| 3535 | RsTypeKind::FuncPtr { abi: "blah", param_types: vec![a, b], return_type: Box::new(c) } |
| 3536 | }; |
| 3537 | let dfs_names = f |
| 3538 | .dfs_iter() |
| 3539 | .map(|t| match t { |
| 3540 | RsTypeKind::FuncPtr { .. } => "fn", |
| 3541 | RsTypeKind::Other { name, .. } => *name, |
| 3542 | _ => unreachable!("Only FuncPtr and Other kinds are used in this test"), |
| 3543 | }) |
| 3544 | .collect_vec(); |
| 3545 | assert_eq!(vec!["fn", "A", "B", "C"], dfs_names); |
| 3546 | } |
| 3547 | |
| 3548 | #[test] |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3549 | fn test_rs_type_kind_lifetimes() -> Result<()> { |
| 3550 | let ir = ir_from_cc( |
| 3551 | r#" |
| 3552 | #pragma clang lifetime_elision |
| 3553 | using TypeAlias = int&; |
| 3554 | struct SomeStruct {}; |
| 3555 | void foo(int a, int& b, int* c, int** d, TypeAlias e, SomeStruct f); "#, |
| 3556 | )?; |
| 3557 | let f = retrieve_func(&ir, "foo"); |
| 3558 | let ret = RsTypeKind::new(&f.return_type.rs_type, &ir)?; |
| 3559 | let a = RsTypeKind::new(&f.params[0].type_.rs_type, &ir)?; |
| 3560 | let b = RsTypeKind::new(&f.params[1].type_.rs_type, &ir)?; |
| 3561 | let c = RsTypeKind::new(&f.params[2].type_.rs_type, &ir)?; |
| 3562 | let d = RsTypeKind::new(&f.params[3].type_.rs_type, &ir)?; |
| 3563 | let e = RsTypeKind::new(&f.params[4].type_.rs_type, &ir)?; |
| 3564 | let f = RsTypeKind::new(&f.params[5].type_.rs_type, &ir)?; |
| 3565 | |
| 3566 | assert_eq!(0, ret.lifetimes().count()); // No lifetimes on `void`. |
| 3567 | assert_eq!(0, a.lifetimes().count()); // No lifetimes on `int`. |
| 3568 | assert_eq!(1, b.lifetimes().count()); // `&'a i32` has a single lifetime. |
| 3569 | assert_eq!(1, c.lifetimes().count()); // `Option<&'b i32>` has a single lifetime. |
| 3570 | assert_eq!(2, d.lifetimes().count()); // `&'c Option<&'d i32>` has two lifetimes. |
| 3571 | assert_eq!(1, e.lifetimes().count()); // Lifetime of underlying type should show through. |
| 3572 | assert_eq!(0, f.lifetimes().count()); // No lifetimes on structs (yet). |
| 3573 | Ok(()) |
| 3574 | } |
| 3575 | |
| 3576 | #[test] |
| 3577 | fn test_rs_type_kind_lifetimes_raw_ptr() -> Result<()> { |
| 3578 | let ir = ir_from_cc("void foo(int* a);")?; |
| 3579 | let f = retrieve_func(&ir, "foo"); |
| 3580 | let a = RsTypeKind::new(&f.params[0].type_.rs_type, &ir)?; |
| 3581 | assert_eq!(0, a.lifetimes().count()); // No lifetimes on `int*`. |
| 3582 | Ok(()) |
| 3583 | } |
| 3584 | |
| 3585 | #[test] |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 3586 | fn test_rust_keywords_are_escaped_in_rs_api_file() -> Result<()> { |
| 3587 | let ir = ir_from_cc("struct type { int dyn; };")?; |
| 3588 | let rs_api = generate_rs_api(&ir)?; |
| 3589 | assert_rs_matches!(rs_api, quote! { struct r#type { ... r#dyn: i32 ... } }); |
| 3590 | Ok(()) |
| 3591 | } |
| 3592 | |
| 3593 | #[test] |
| 3594 | fn test_rust_keywords_are_not_escaped_in_rs_api_impl_file() -> Result<()> { |
| 3595 | let ir = ir_from_cc("struct type { int dyn; };")?; |
| 3596 | let rs_api_impl = generate_rs_api_impl(&ir)?; |
| 3597 | assert_cc_matches!(rs_api_impl, quote! { static_assert(offsetof(class type, dyn) ... ) }); |
| 3598 | Ok(()) |
| 3599 | } |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 3600 | |
| 3601 | #[test] |
| 3602 | fn test_no_aligned_attr() { |
| 3603 | let ir = ir_from_cc("struct SomeStruct {};").unwrap(); |
| 3604 | let rs_api = generate_rs_api(&ir).unwrap(); |
| 3605 | |
| 3606 | assert_rs_matches! {rs_api, quote! { |
| 3607 | #[repr(C)] |
| 3608 | pub struct SomeStruct { ... } |
| 3609 | }}; |
| 3610 | } |
| 3611 | |
| 3612 | #[test] |
| 3613 | fn test_aligned_attr() { |
| 3614 | let ir = ir_from_cc("struct SomeStruct {} __attribute__((aligned(64)));").unwrap(); |
| 3615 | let rs_api = generate_rs_api(&ir).unwrap(); |
| 3616 | |
| 3617 | assert_rs_matches! {rs_api, quote! { |
| 3618 | #[repr(C, align(64))] |
| 3619 | pub struct SomeStruct { ... } |
| 3620 | } |
| 3621 | }; |
| 3622 | } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 3623 | |
| 3624 | /// !Unpin references should not be pinned. |
| 3625 | #[test] |
| 3626 | fn test_nonunpin_ref_param() -> Result<()> { |
| 3627 | let rs_api_impl = generate_rs_api(&ir_from_cc( |
| 3628 | r#" |
| 3629 | #pragma clang lifetime_elision |
| 3630 | struct S {~S();}; |
| 3631 | void Function(const S& s); |
| 3632 | "#, |
| 3633 | )?)?; |
| 3634 | assert_rs_matches!( |
| 3635 | rs_api_impl, |
| 3636 | quote! { |
| 3637 | fn Function<'a>(s: &'a S) { ... } |
| 3638 | } |
| 3639 | ); |
| 3640 | Ok(()) |
| 3641 | } |
| 3642 | |
| 3643 | /// !Unpin mut references must be pinned. |
| 3644 | #[test] |
| 3645 | fn test_nonunpin_mut_param() -> Result<()> { |
| 3646 | let rs_api_impl = generate_rs_api(&ir_from_cc( |
| 3647 | r#" |
| 3648 | #pragma clang lifetime_elision |
| 3649 | struct S {~S();}; |
| 3650 | void Function(S& s); |
| 3651 | "#, |
| 3652 | )?)?; |
| 3653 | assert_rs_matches!( |
| 3654 | rs_api_impl, |
| 3655 | quote! { |
| 3656 | fn Function<'a>(s: std::pin::Pin<&'a mut S>) { ... } |
| 3657 | } |
| 3658 | ); |
| 3659 | Ok(()) |
| 3660 | } |
| 3661 | |
| 3662 | /// !Unpin &self should not be pinned. |
| 3663 | #[test] |
| 3664 | fn test_nonunpin_ref_self() -> Result<()> { |
| 3665 | let rs_api_impl = generate_rs_api(&ir_from_cc( |
| 3666 | r#" |
| 3667 | #pragma clang lifetime_elision |
| 3668 | struct S { |
| 3669 | ~S(); |
| 3670 | void Function() const; |
| 3671 | }; |
| 3672 | "#, |
| 3673 | )?)?; |
| 3674 | assert_rs_matches!( |
| 3675 | rs_api_impl, |
| 3676 | quote! { |
| 3677 | fn Function<'a>(&'a self) { ... } |
| 3678 | } |
| 3679 | ); |
| 3680 | Ok(()) |
| 3681 | } |
| 3682 | |
| 3683 | /// !Unpin &mut self must be pinned. |
| 3684 | #[test] |
| 3685 | fn test_nonunpin_mut_self() -> Result<()> { |
| 3686 | let rs_api_impl = generate_rs_api(&ir_from_cc( |
| 3687 | r#" |
| 3688 | #pragma clang lifetime_elision |
| 3689 | struct S { |
| 3690 | ~S(); |
| 3691 | void Function(); |
| 3692 | }; |
| 3693 | "#, |
| 3694 | )?)?; |
| 3695 | assert_rs_matches!( |
| 3696 | rs_api_impl, |
| 3697 | quote! { |
| 3698 | fn Function<'a>(self: std::pin::Pin<&'a mut Self>) { ... } |
| 3699 | } |
| 3700 | ); |
| 3701 | Ok(()) |
| 3702 | } |
| 3703 | |
| 3704 | /// Drop::drop must not use self : Pin<...>. |
| 3705 | #[test] |
| 3706 | fn test_nonunpin_drop() -> Result<()> { |
| 3707 | let rs_api_impl = generate_rs_api(&ir_from_cc( |
| 3708 | r#" |
| 3709 | struct S {~S();}; |
| 3710 | "#, |
| 3711 | )?)?; |
| 3712 | assert_rs_matches!( |
| 3713 | rs_api_impl, |
| 3714 | quote! { |
| 3715 | fn drop(&mut self) { ... } |
| 3716 | } |
| 3717 | ); |
| 3718 | Ok(()) |
| 3719 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 3720 | } |