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 | |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 5 | use arc_anyhow::{Context, Result}; |
Lukasz Anforowicz | 434c469 | 2022-11-01 14:05:24 -0700 | [diff] [blame] | 6 | use code_gen_utils::{format_cc_includes, CcInclude}; |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 7 | use error_report::{anyhow, bail, ensure, ErrorReport, ErrorReporting, IgnoreErrors}; |
Marcel Hlopko | 884ae7f | 2021-08-18 13:58:22 +0000 | [diff] [blame] | 8 | use ffi_types::*; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 9 | use ir::*; |
| 10 | use itertools::Itertools; |
Michael VanBemmel | 5014b3e | 2022-08-03 16:27:34 -0700 | [diff] [blame] | 11 | use once_cell::sync::Lazy; |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 12 | use proc_macro2::{Ident, Literal, TokenStream}; |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 13 | use quote::{format_ident, quote, ToTokens}; |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 14 | use std::collections::{BTreeSet, HashMap, HashSet}; |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 15 | use std::ffi::{OsStr, OsString}; |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 16 | use std::fmt::Write as _; |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 17 | use std::iter::{self, Iterator}; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 18 | use std::panic::catch_unwind; |
| 19 | use std::process; |
Michael VanBemmel | 7a4d4c0 | 2022-07-27 13:21:47 -0700 | [diff] [blame] | 20 | use std::ptr; |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 21 | use std::rc::Rc; |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 22 | use token_stream_printer::{ |
| 23 | cc_tokens_to_formatted_string, rs_tokens_to_formatted_string, RustfmtConfig, |
| 24 | }; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 25 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 26 | /// FFI equivalent of `Bindings`. |
| 27 | #[repr(C)] |
| 28 | pub struct FfiBindings { |
| 29 | rs_api: FfiU8SliceBox, |
| 30 | rs_api_impl: FfiU8SliceBox, |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 31 | error_report: FfiU8SliceBox, |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// Deserializes IR from `json` and generates bindings source code. |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 35 | /// |
| 36 | /// This function panics on error. |
| 37 | /// |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 38 | /// # Safety |
| 39 | /// |
| 40 | /// Expectations: |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 41 | /// * `json` should be a FfiU8Slice for a valid array of bytes with the given |
| 42 | /// size. |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 43 | /// * `crubit_support_path` should be a FfiU8Slice for a valid array of bytes |
| 44 | /// representing an UTF8-encoded string |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 45 | /// * `rustfmt_exe_path` and `rustfmt_config_path` should both be a |
| 46 | /// FfiU8Slice for a valid array of bytes representing an UTF8-encoded |
| 47 | /// string (without the UTF-8 requirement, it seems that Rust doesn't offer |
| 48 | /// a way to convert to OsString on Windows) |
| 49 | /// * `json`, `crubit_support_path`, `rustfmt_exe_path`, and |
| 50 | /// `rustfmt_config_path` shouldn't change during the call. |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 51 | /// |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 52 | /// Ownership: |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 53 | /// * function doesn't take ownership of (in other words it borrows) the |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 54 | /// input params: `json`, `crubit_support_path`, `rustfmt_exe_path`, and |
| 55 | /// `rustfmt_config_path` |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 56 | /// * function passes ownership of the returned value to the caller |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 57 | #[no_mangle] |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 58 | pub unsafe extern "C" fn GenerateBindingsImpl( |
| 59 | json: FfiU8Slice, |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 60 | crubit_support_path: FfiU8Slice, |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 61 | rustfmt_exe_path: FfiU8Slice, |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 62 | rustfmt_config_path: FfiU8Slice, |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 63 | generate_error_report: bool, |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 64 | ) -> FfiBindings { |
| 65 | let json: &[u8] = json.as_slice(); |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 66 | let crubit_support_path: &str = std::str::from_utf8(crubit_support_path.as_slice()).unwrap(); |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 67 | let rustfmt_exe_path: OsString = |
| 68 | std::str::from_utf8(rustfmt_exe_path.as_slice()).unwrap().into(); |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 69 | let rustfmt_config_path: OsString = |
| 70 | std::str::from_utf8(rustfmt_config_path.as_slice()).unwrap().into(); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 71 | catch_unwind(|| { |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 72 | // It is ok to abort here. |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 73 | let mut error_report; |
| 74 | let mut ignore_errors; |
| 75 | let errors: &mut dyn ErrorReporting = if generate_error_report { |
| 76 | error_report = ErrorReport::new(); |
| 77 | &mut error_report |
| 78 | } else { |
| 79 | ignore_errors = IgnoreErrors; |
| 80 | &mut ignore_errors |
| 81 | }; |
| 82 | let Bindings { rs_api, rs_api_impl } = generate_bindings( |
| 83 | json, |
| 84 | crubit_support_path, |
| 85 | &rustfmt_exe_path, |
| 86 | &rustfmt_config_path, |
| 87 | errors, |
| 88 | ) |
| 89 | .unwrap(); |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 90 | FfiBindings { |
| 91 | rs_api: FfiU8SliceBox::from_boxed_slice(rs_api.into_bytes().into_boxed_slice()), |
| 92 | rs_api_impl: FfiU8SliceBox::from_boxed_slice( |
| 93 | rs_api_impl.into_bytes().into_boxed_slice(), |
| 94 | ), |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 95 | error_report: FfiU8SliceBox::from_boxed_slice( |
| 96 | errors.serialize_to_vec().unwrap().into_boxed_slice(), |
| 97 | ), |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 98 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 99 | }) |
| 100 | .unwrap_or_else(|_| process::abort()) |
| 101 | } |
| 102 | |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 103 | #[salsa::query_group(BindingsGeneratorStorage)] |
| 104 | trait BindingsGenerator { |
| 105 | #[salsa::input] |
| 106 | fn ir(&self) -> Rc<IR>; |
| 107 | |
Devin Jeanpierre | 3a0cc5a | 2022-07-12 09:36:34 -0700 | [diff] [blame] | 108 | fn rs_type_kind(&self, rs_type: RsType) -> Result<RsTypeKind>; |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 109 | |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 110 | fn generate_func( |
| 111 | &self, |
| 112 | func: Rc<Func>, |
Lukasz Anforowicz | f8cfa56 | 2022-09-22 11:06:43 -0700 | [diff] [blame] | 113 | ) -> Result<Option<Rc<(RsSnippet, RsSnippet, Rc<FunctionId>)>>>; |
Devin Jeanpierre | ab85d44 | 2022-06-29 19:16:41 -0700 | [diff] [blame] | 114 | |
| 115 | fn overloaded_funcs(&self) -> Rc<HashSet<Rc<FunctionId>>>; |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 116 | |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 117 | fn is_record_clonable(&self, record: Rc<Record>) -> bool; |
| 118 | |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 119 | // TODO(b/236687702): convert the get_binding function into a query once |
| 120 | // ImplKind implements Eq. |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | #[salsa::database(BindingsGeneratorStorage)] |
| 124 | #[derive(Default)] |
| 125 | struct Database { |
| 126 | storage: salsa::Storage<Self>, |
| 127 | } |
| 128 | |
| 129 | impl salsa::Database for Database {} |
| 130 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 131 | /// Source code for generated bindings. |
| 132 | struct Bindings { |
| 133 | // Rust source code. |
| 134 | rs_api: String, |
| 135 | // C++ source code. |
| 136 | rs_api_impl: String, |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 139 | /// Source code for generated bindings, as tokens. |
| 140 | struct BindingsTokens { |
| 141 | // Rust source code. |
| 142 | rs_api: TokenStream, |
| 143 | // C++ source code. |
| 144 | rs_api_impl: TokenStream, |
| 145 | } |
| 146 | |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 147 | fn generate_bindings( |
| 148 | json: &[u8], |
| 149 | crubit_support_path: &str, |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 150 | rustfmt_exe_path: &OsStr, |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 151 | rustfmt_config_path: &OsStr, |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 152 | errors: &mut dyn ErrorReporting, |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 153 | ) -> Result<Bindings> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 154 | let ir = Rc::new(deserialize_ir(json)?); |
Marcel Hlopko | ca84ff4 | 2021-12-09 14:15:14 +0000 | [diff] [blame] | 155 | |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 156 | let BindingsTokens { rs_api, rs_api_impl } = |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 157 | generate_bindings_tokens(ir.clone(), crubit_support_path, errors)?; |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 158 | let rs_api = { |
Lukasz Anforowicz | d7d68f0 | 2022-05-26 07:41:02 -0700 | [diff] [blame] | 159 | let rustfmt_config = RustfmtConfig::new(rustfmt_exe_path, rustfmt_config_path); |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 160 | rs_tokens_to_formatted_string(rs_api, &rustfmt_config)? |
| 161 | }; |
Lukasz Anforowicz | 19e057d | 2022-10-28 12:03:23 -0700 | [diff] [blame] | 162 | let rs_api_impl = cc_tokens_to_formatted_string(rs_api_impl)?; |
Lukasz Anforowicz | 54ff318 | 2022-05-06 07:17:58 -0700 | [diff] [blame] | 163 | |
Lukasz Anforowicz | 19e057d | 2022-10-28 12:03:23 -0700 | [diff] [blame] | 164 | // Add top-level comments that help identify where the generated bindings came |
| 165 | // from. |
| 166 | let top_level_comment = { |
| 167 | // The "@generated" marker is an informal convention for identifying |
| 168 | // automatically generated code. This marker is recognized by `rustfmt` |
| 169 | // (see the `format_generated_files` option [1]) and some other tools. |
| 170 | // For more info see https://generated.at/. |
| 171 | // |
| 172 | // [1] |
| 173 | // https://rust-lang.github.io/rustfmt/?version=v1.4.38&search=#format_generated_files |
| 174 | // |
| 175 | // TODO(b/255784681): It would be nice to include "by $argv[0]"" in the |
| 176 | // @generated comment below. OTOH, `std::env::current_exe()` in our |
| 177 | // current build environment returns a guid-like path... :-/ |
| 178 | // |
| 179 | // TODO(b/255784681): Consider including cmdline arguments. |
| 180 | let target = &ir.current_target().0; |
| 181 | format!( |
| 182 | "// Automatically @generated Rust bindings for the following C++ target:\n\ |
| 183 | // {target}\n" |
| 184 | ) |
| 185 | }; |
Lukasz Anforowicz | b46f5f4 | 2022-10-28 15:33:34 -0700 | [diff] [blame] | 186 | let rs_api = format!("{top_level_comment}\n{rs_api}"); |
| 187 | let rs_api_impl = format!("{top_level_comment}\n{rs_api_impl}"); |
Marcel Hlopko | ca84ff4 | 2021-12-09 14:15:14 +0000 | [diff] [blame] | 188 | |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 189 | Ok(Bindings { rs_api, rs_api_impl }) |
| 190 | } |
| 191 | |
Devin Jeanpierre | 6d5e7cc | 2021-10-21 12:56:07 +0000 | [diff] [blame] | 192 | /// Rust source code with attached information about how to modify the parent |
| 193 | /// crate. |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 194 | /// |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 195 | /// For example, the snippet `vec![].into_raw_parts()` is not valid unless the |
| 196 | /// `vec_into_raw_parts` feature is enabled. So such a snippet should be |
| 197 | /// represented as: |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 198 | /// |
| 199 | /// ``` |
| 200 | /// RsSnippet { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 201 | /// features: btree_set![make_rs_ident("vec_into_raw_parts")], |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 202 | /// tokens: quote!{vec![].into_raw_parts()}, |
| 203 | /// } |
| 204 | /// ``` |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 205 | #[derive(Clone, Debug)] |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 206 | struct RsSnippet { |
| 207 | /// Rust feature flags used by this snippet. |
| 208 | features: BTreeSet<Ident>, |
| 209 | /// The snippet itself, as a token stream. |
| 210 | tokens: TokenStream, |
| 211 | } |
| 212 | |
| 213 | impl From<TokenStream> for RsSnippet { |
| 214 | fn from(tokens: TokenStream) -> Self { |
| 215 | RsSnippet { features: BTreeSet::new(), tokens } |
| 216 | } |
| 217 | } |
| 218 | |
Lukasz Anforowicz | f8cfa56 | 2022-09-22 11:06:43 -0700 | [diff] [blame] | 219 | impl Eq for RsSnippet {} |
| 220 | |
| 221 | impl PartialEq for RsSnippet { |
| 222 | fn eq(&self, other: &Self) -> bool { |
| 223 | fn to_comparable_tuple(_x: &RsSnippet) -> (&BTreeSet<Ident>, String) { |
| 224 | // TokenStream doesn't implement `PartialEq`, so we convert to an equivalent |
| 225 | // `String`. This is a bit expensive, but should be okay (especially |
| 226 | // given that this code doesn't execute at this point). Having a |
| 227 | // working `impl PartialEq` helps `salsa` reuse unchanged memoized |
| 228 | // results of previous computations (although this is a bit |
| 229 | // theoretical, since right now we don't re-set `salsa`'s inputs - we only call |
| 230 | // `set_ir` once). |
| 231 | // |
| 232 | // TODO(lukasza): If incremental `salsa` computations are ever used in the |
| 233 | // future, we may end up hitting the `panic!` below. At that point |
| 234 | // it should be okay to just remove the `panic!`, but we should also |
| 235 | // 1) think about improving performance of comparing `TokenStream` |
| 236 | // for equality and 2) add unit tests covering this `PartialEq` `impl`. |
| 237 | panic!("This code is not expected to execute in practice"); |
| 238 | #[allow(unreachable_code)] |
| 239 | (&_x.features, _x.tokens.to_string()) |
| 240 | } |
| 241 | to_comparable_tuple(self) == to_comparable_tuple(other) |
| 242 | } |
| 243 | } |
| 244 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 245 | /// If we know the original C++ function is codegenned and already compatible |
| 246 | /// with `extern "C"` calling convention we skip creating/calling the C++ thunk |
| 247 | /// since we can call the original C++ directly. |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 248 | fn can_skip_cc_thunk(db: &dyn BindingsGenerator, func: &Func) -> bool { |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 249 | // ## Inline functions |
| 250 | // |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 251 | // Inline functions may not be codegenned in the C++ library since Clang doesn't |
| 252 | // know if Rust calls the function or not. Therefore in order to make inline |
| 253 | // functions callable from Rust we need to generate a C++ file that defines |
| 254 | // a thunk that delegates to the original inline function. When compiled, |
| 255 | // 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] | 256 | // thunk when the user wants to call the original inline function. |
| 257 | // |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 258 | // This is not great runtime-performance-wise in regular builds (inline function |
| 259 | // will not be inlined, there will always be a function call), but it is |
| 260 | // correct. ThinLTO builds will be able to see through the thunk and inline |
| 261 | // code across the language boundary. For non-ThinLTO builds we plan to |
| 262 | // implement <internal link> which removes the runtime performance overhead. |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 263 | if func.is_inline { |
| 264 | return false; |
| 265 | } |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 266 | // ## Member functions (or descendants) of class templates |
| 267 | // |
| 268 | // A thunk is required to force/guarantee template instantiation. |
| 269 | if func.is_member_or_descendant_of_class_template { |
| 270 | return false; |
| 271 | } |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 272 | // ## Virtual functions |
| 273 | // |
| 274 | // When calling virtual `A::Method()`, it's not necessarily the case that we'll |
| 275 | // specifically call the concrete `A::Method` impl. For example, if this is |
| 276 | // called on something whose dynamic type is some subclass `B` with an |
| 277 | // overridden `B::Method`, then we'll call that. |
| 278 | // |
| 279 | // We must reuse the C++ dynamic dispatching system. In this case, the easiest |
| 280 | // way to do it is by resorting to a C++ thunk, whose implementation will do |
| 281 | // the lookup. |
| 282 | // |
| 283 | // In terms of runtime performance, since this only occurs for virtual function |
| 284 | // calls, which are already slow, it may not be such a big deal. We can |
| 285 | // benchmark it later. :) |
| 286 | if let Some(meta) = &func.member_func_metadata { |
| 287 | if let Some(inst_meta) = &meta.instance_method_metadata { |
| 288 | if inst_meta.is_virtual { |
| 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | } |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 293 | // ## Custom calling convention requires a thunk. |
| 294 | // |
| 295 | // The thunk has the "C" calling convention, and internally can call the |
| 296 | // C++ function using any of the calling conventions supported by the C++ |
| 297 | // compiler (which might not always match the set supported by Rust - e.g., |
| 298 | // abi.rs doesn't contain "swiftcall" from |
| 299 | // clang::FunctionType::getNameForCallConv) |
| 300 | if !func.has_c_calling_convention { |
| 301 | return false; |
| 302 | } |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 303 | |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 304 | // ## Nontrivial return types. |
| 305 | // |
| 306 | // If the function returns a value which is nontrivial for the purpose of calls, |
| 307 | // then in the underlying ABI, it is actually returned via a hidden pointer |
| 308 | // parameter not exposed anywhere in the Clang AST or the Crubit IR. For |
| 309 | // now, this is worked around via an _explicit_ output parameter, used in |
| 310 | // the thunk, which cannot be skipped anymore. |
| 311 | // |
| 312 | // Note: if the RsTypeKind cannot be parsed / rs_type_kind returns Err, then |
| 313 | // bindings generation will fail for this function, so it doesn't really matter |
| 314 | // what we do here. |
| 315 | if let Ok(return_type) = db.rs_type_kind(func.return_type.rs_type.clone()) { |
| 316 | if !return_type.is_unpin() { |
| 317 | return false; |
| 318 | } |
| 319 | } |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 320 | // ## Nontrivial parameter types. |
| 321 | // |
| 322 | // If the function accepts a value which is nontrivial for the purpose of calls, |
| 323 | // then in the underlying ABI, it is actually passed by pointer. |
| 324 | // |
| 325 | // Because there's no way to upgrade an lvalue (e.g. pointer) to a prvalue, we |
| 326 | // cannot implement guaranteed copy/move elision for inline functions for |
| 327 | // now: any thunk we generate would need to invoke the correct function as |
| 328 | // if by magic. |
| 329 | // |
| 330 | // And so for now, we always use C++11 semantics, via an intermediate thunk. |
| 331 | // |
| 332 | // (As a side effect, this, like return values, means that support is |
| 333 | // ABI-agnostic.) |
| 334 | for param in &func.params { |
| 335 | if let Ok(param_type) = db.rs_type_kind(param.type_.rs_type.clone()) { |
| 336 | if !param_type.is_unpin() { |
| 337 | return false; |
| 338 | } |
| 339 | } |
| 340 | } |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 341 | |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 342 | true |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 345 | /// Uniquely identifies a generated Rust function. |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 346 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 347 | struct FunctionId { |
| 348 | // If the function is on a trait impl, contains the name of the Self type for |
| 349 | // which the trait is being implemented. |
| 350 | self_type: Option<syn::Path>, |
| 351 | // Fully qualified path of the function. For functions in impl blocks, this |
| 352 | // includes the name of the type or trait on which the function is being |
| 353 | // implemented, e.g. `Default::default`. |
| 354 | function_path: syn::Path, |
| 355 | } |
| 356 | |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 357 | /// Returns the name of `func` in C++ syntax. |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 358 | fn cxx_function_name(func: &Func, ir: &IR) -> Result<String> { |
Devin Jeanpierre | bdfb4d9 | 2022-06-17 01:17:01 -0700 | [diff] [blame] | 359 | let record: Option<&str> = ir.record_for_member_func(func)?.map(|r| &*r.cc_name); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 360 | |
| 361 | let func_name = match &func.name { |
| 362 | UnqualifiedIdentifier::Identifier(id) => id.identifier.clone(), |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 363 | UnqualifiedIdentifier::Operator(op) => op.cc_name(), |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 364 | UnqualifiedIdentifier::Destructor => { |
| 365 | format!("~{}", record.expect("destructor must be associated with a record")) |
| 366 | } |
| 367 | UnqualifiedIdentifier::Constructor => { |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 368 | record.expect("constructor must be associated with a record").to_string() |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 369 | } |
| 370 | }; |
| 371 | |
| 372 | if let Some(record_name) = record { |
| 373 | Ok(format!("{}::{}", record_name, func_name)) |
| 374 | } else { |
| 375 | Ok(func_name) |
| 376 | } |
| 377 | } |
| 378 | |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 379 | fn make_unsupported_fn(func: &Func, ir: &IR, message: impl ToString) -> Result<UnsupportedItem> { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 380 | Ok(UnsupportedItem::new_with_message( |
| 381 | cxx_function_name(func, ir)?, |
| 382 | message.to_string(), |
| 383 | func.source_loc.clone(), |
| 384 | func.id, |
| 385 | )) |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 388 | fn make_unsupported_nested_type_alias(type_alias: &TypeAlias) -> Result<UnsupportedItem> { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 389 | Ok(UnsupportedItem::new_with_message( |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 390 | // TODO(jeanpierreda): It would be nice to include the enclosing record name here too. |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 391 | type_alias.identifier.identifier.to_string(), |
| 392 | "Typedefs nested in classes are not supported yet".to_string(), |
| 393 | type_alias.source_loc.clone(), |
| 394 | type_alias.id, |
| 395 | )) |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 398 | /// The name of a one-function trait, with extra entries for |
| 399 | /// specially-understood traits and families of traits. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 400 | enum TraitName { |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 401 | /// The constructor trait for !Unpin types, with a list of parameter types. |
| 402 | /// For example, `CtorNew(vec![])` is the default constructor. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 403 | CtorNew(Vec<RsTypeKind>), |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 404 | /// An Unpin constructor trait, e.g. From or Clone, with a list of parameter |
| 405 | /// types. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 406 | UnpinConstructor { name: TokenStream, params: Vec<RsTypeKind> }, |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 407 | /// The PartialEq trait. |
| 408 | PartialEq { params: Vec<RsTypeKind> }, |
| 409 | /// The PartialOrd trait. |
| 410 | PartialOrd { params: Vec<RsTypeKind> }, |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 411 | /// Any other trait, e.g. Eq. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 412 | Other { name: TokenStream, params: Vec<RsTypeKind>, is_unsafe_fn: bool }, |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 413 | } |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 414 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 415 | impl TraitName { |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 416 | /// Returns the generic parameters in this trait name. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 417 | fn params(&self) -> impl Iterator<Item = &RsTypeKind> { |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 418 | match self { |
| 419 | Self::CtorNew(params) |
| 420 | | Self::UnpinConstructor { params, .. } |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 421 | | Self::PartialEq { params } |
| 422 | | Self::PartialOrd { params } |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 423 | | Self::Other { params, .. } => params.iter(), |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | /// Returns the lifetimes used in this trait name. |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 428 | pub fn lifetimes(&self) -> impl Iterator<Item = Lifetime> + '_ { |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 429 | self.params().flat_map(|p| p.lifetimes()) |
| 430 | } |
| 431 | } |
| 432 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 433 | impl ToTokens for TraitName { |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 434 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 435 | match self { |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 436 | Self::UnpinConstructor { name, params } | Self::Other { name, params, .. } => { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 437 | let params = format_generic_params(/* lifetimes= */ &[], params); |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 438 | quote! {#name #params}.to_tokens(tokens) |
| 439 | } |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 440 | Self::PartialEq { params } => { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 441 | let params = format_generic_params(/* lifetimes= */ &[], params); |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 442 | quote! {PartialEq #params}.to_tokens(tokens) |
| 443 | } |
| 444 | Self::PartialOrd { params } => { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 445 | let params = format_generic_params(/* lifetimes= */ &[], params); |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 446 | quote! {PartialOrd #params}.to_tokens(tokens) |
| 447 | } |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 448 | Self::CtorNew(arg_types) => { |
| 449 | let arg_types = format_tuple_except_singleton(arg_types); |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 450 | quote! { ::ctor::CtorNew < #arg_types > }.to_tokens(tokens) |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 451 | } |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | /// The kind of the `impl` block the function needs to be generated in. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 457 | enum ImplKind { |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 458 | /// Used for free functions for which we don't want the `impl` block. |
| 459 | None { is_unsafe: bool }, |
| 460 | /// Used for inherent methods for which we need an `impl SomeStruct { ... }` |
| 461 | /// block. |
| 462 | Struct { |
| 463 | /// For example, `SomeStruct`. Retrieved from |
| 464 | /// `func.member_func_metadata`. |
| 465 | record_name: Ident, |
| 466 | is_unsafe: bool, |
| 467 | /// Whether to format the first parameter as "self" (e.g. `__this: |
| 468 | /// &mut T` -> `&mut self`) |
| 469 | format_first_param_as_self: bool, |
| 470 | }, |
| 471 | /// Used for trait methods for which we need an `impl TraitName for |
| 472 | /// SomeStruct { ... }` block. |
| 473 | Trait { |
| 474 | /// For example, `SomeStruct`. |
| 475 | /// Note that `record_name` might *not* be from |
| 476 | /// `func.member_func_metadata`. |
| 477 | record_name: Ident, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 478 | record_qualifier: NamespaceQualifier, |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 479 | /// For example, `quote!{ From<i32> }`. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 480 | trait_name: TraitName, |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 481 | /// Reference style for the `impl` block and self parameters. |
| 482 | impl_for: ImplFor, |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 483 | |
Devin Jeanpierre | a04bce1 | 2022-08-01 13:39:48 -0700 | [diff] [blame] | 484 | /// The generic params of trait `impl` (e.g. `vec![quote!{'b}]`). These |
| 485 | /// start empty and only later are mutated into the correct value. |
| 486 | trait_generic_params: Vec<TokenStream>, |
| 487 | |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 488 | /// Whether to format the first parameter as "self" (e.g. `__this: |
| 489 | /// &mut T` -> `&mut self`) |
| 490 | format_first_param_as_self: bool, |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 491 | /// Whether to drop the C++ function's return value and return unit |
| 492 | /// instead. |
| 493 | drop_return: bool, |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 494 | |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 495 | /// If this trait's method returns an associated type, it has this name. |
| 496 | /// For example, this is `Output` on |
| 497 | /// [`Add`](https://doc.rust-lang.org/std/ops/trait.Add.html). |
| 498 | associated_return_type: Option<Ident>, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 499 | |
| 500 | /// Whether args should always be const references in Rust, even if they |
| 501 | /// are by value in C++. |
| 502 | /// |
| 503 | /// For example, the traits for == and < only accept const reference |
| 504 | /// parameters, but C++ allows values. |
| 505 | force_const_reference_params: bool, |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 506 | }, |
| 507 | } |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 508 | impl ImplKind { |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 509 | fn new_trait( |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 510 | trait_name: TraitName, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 511 | record: &Record, |
| 512 | ir: &IR, |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 513 | format_first_param_as_self: bool, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 514 | force_const_reference_params: bool, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 515 | ) -> Result<Self> { |
| 516 | Ok(ImplKind::Trait { |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 517 | trait_name, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 518 | record_name: make_rs_ident(&record.rs_name), |
| 519 | record_qualifier: NamespaceQualifier::new(record.id, ir)?, |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 520 | impl_for: ImplFor::T, |
Devin Jeanpierre | a04bce1 | 2022-08-01 13:39:48 -0700 | [diff] [blame] | 521 | trait_generic_params: vec![], |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 522 | format_first_param_as_self, |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 523 | drop_return: false, |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 524 | associated_return_type: None, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 525 | force_const_reference_params, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 526 | }) |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 527 | } |
| 528 | fn format_first_param_as_self(&self) -> bool { |
| 529 | matches!( |
| 530 | self, |
| 531 | Self::Trait { format_first_param_as_self: true, .. } |
| 532 | | Self::Struct { format_first_param_as_self: true, .. } |
| 533 | ) |
| 534 | } |
| 535 | /// Returns whether the function is defined as `unsafe fn ...`. |
| 536 | fn is_unsafe(&self) -> bool { |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 537 | matches!( |
| 538 | self, |
| 539 | Self::None { is_unsafe: true, .. } |
| 540 | | Self::Struct { is_unsafe: true, .. } |
| 541 | | Self::Trait { trait_name: TraitName::Other { is_unsafe_fn: true, .. }, .. } |
| 542 | ) |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 546 | /// Whether the impl block is for T, and the receivers take self by reference, |
| 547 | /// or the impl block is for a reference to T, and the method receivers take |
| 548 | /// self by value. |
| 549 | enum ImplFor { |
| 550 | /// Implement the trait for `T` directly. |
| 551 | /// |
| 552 | /// ``` |
| 553 | /// impl Trait for T { |
| 554 | /// fn const_method<'a>(&'a self); |
| 555 | /// fn mut_method<'a>(&'a mut self); |
| 556 | /// fn pin_method<'a>(Pin<&'a mut self>); |
| 557 | /// } |
| 558 | /// ``` |
| 559 | T, |
| 560 | /// Implement the trait for `&T`, `&mut T`, or `Pin<&mut T>`, depending on |
| 561 | /// the Rust type of the self parameter. |
| 562 | /// |
| 563 | /// ``` |
| 564 | /// impl<'a> Trait for &'a T { |
| 565 | /// fn const_method(self); |
| 566 | /// } |
| 567 | /// impl<'a> Trait for &'a mut UnpinT { |
| 568 | /// fn mut_method(self); |
| 569 | /// } |
| 570 | /// impl<'a> Trait for Pin<&'a mut NonUnpinT> { |
| 571 | /// fn pin_method(self); |
| 572 | /// } |
| 573 | /// ``` |
| 574 | RefT, |
| 575 | } |
| 576 | |
Michael VanBemmel | 7a4d4c0 | 2022-07-27 13:21:47 -0700 | [diff] [blame] | 577 | /// Returns whether an argument of this type causes ADL to include the `record`. |
| 578 | fn adl_expands_to(record: &Record, rs_type_kind: &RsTypeKind) -> bool { |
| 579 | match rs_type_kind { |
| 580 | RsTypeKind::Record { record: nested_record, .. } => ptr::eq(record, &**nested_record), |
| 581 | RsTypeKind::Reference { referent, .. } => adl_expands_to(record, &**referent), |
| 582 | RsTypeKind::RvalueReference { referent, .. } => adl_expands_to(record, &**referent), |
| 583 | _ => false, |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /// Returns whether any type in `param_types` causes ADL to include `record`. |
| 588 | /// |
| 589 | /// This is an under-approximation. Things not considered include class template |
| 590 | /// arguments and the parameters and return type of function types. |
| 591 | /// |
| 592 | /// See https://en.cppreference.com/w/cpp/language/adl |
| 593 | fn is_visible_by_adl(enclosing_record: &Record, param_types: &[RsTypeKind]) -> bool { |
| 594 | param_types.iter().any(|param_type| adl_expands_to(enclosing_record, param_type)) |
| 595 | } |
| 596 | |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 597 | #[derive(Debug)] |
| 598 | struct OperatorMetadata { |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 599 | by_cc_name_and_params: HashMap<(&'static str, usize), OperatorMetadataEntry>, |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | #[derive(Clone, Copy, Debug)] |
| 603 | struct OperatorMetadataEntry { |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 604 | cc_name: &'static str, |
| 605 | cc_params: usize, |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 606 | trait_name: &'static str, |
| 607 | method_name: &'static str, |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 608 | is_compound_assignment: bool, |
| 609 | } |
| 610 | |
| 611 | impl OperatorMetadataEntry { |
| 612 | const fn unary( |
| 613 | cc_name: &'static str, |
| 614 | trait_name: &'static str, |
| 615 | method_name: &'static str, |
| 616 | ) -> Self { |
| 617 | Self { cc_name, cc_params: 1, trait_name, method_name, is_compound_assignment: false } |
| 618 | } |
| 619 | |
| 620 | const fn binary( |
| 621 | cc_name: &'static str, |
| 622 | trait_name: &'static str, |
| 623 | method_name: &'static str, |
| 624 | ) -> Self { |
| 625 | Self { cc_name, cc_params: 2, trait_name, method_name, is_compound_assignment: false } |
| 626 | } |
| 627 | |
| 628 | const fn assign( |
| 629 | cc_name: &'static str, |
| 630 | trait_name: &'static str, |
| 631 | method_name: &'static str, |
| 632 | ) -> Self { |
| 633 | Self { cc_name, cc_params: 2, trait_name, method_name, is_compound_assignment: true } |
| 634 | } |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Michael VanBemmel | 5014b3e | 2022-08-03 16:27:34 -0700 | [diff] [blame] | 637 | static OPERATOR_METADATA: Lazy<OperatorMetadata> = Lazy::new(|| { |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 638 | const ENTRIES: &[OperatorMetadataEntry] = &[ |
| 639 | OperatorMetadataEntry::unary("-", "Neg", "neg"), |
| 640 | // The Rust `Not` trait matches with both the C++ `!` and `~` operators to some extent. The |
| 641 | // two operators appear with similar frequency in our target codebase so it's not clear |
| 642 | // which is better to map here. Mapping `operator!` to `Not` as chosen here means that a |
| 643 | // C++ `!` matches up with a Rust `!`. |
| 644 | OperatorMetadataEntry::unary("!", "Not", "not"), |
| 645 | OperatorMetadataEntry::binary("+", "Add", "add"), |
| 646 | OperatorMetadataEntry::binary("-", "Sub", "sub"), |
| 647 | OperatorMetadataEntry::binary("*", "Mul", "mul"), |
| 648 | OperatorMetadataEntry::binary("/", "Div", "div"), |
| 649 | OperatorMetadataEntry::binary("%", "Rem", "rem"), |
| 650 | OperatorMetadataEntry::binary("&", "BitAnd", "bitand"), |
| 651 | OperatorMetadataEntry::binary("|", "BitOr", "bitor"), |
| 652 | OperatorMetadataEntry::binary("^", "BitXor", "bitxor"), |
| 653 | OperatorMetadataEntry::binary("<<", "Shl", "shl"), |
| 654 | OperatorMetadataEntry::binary(">>", "Shr", "shr"), |
| 655 | OperatorMetadataEntry::assign("+=", "AddAssign", "add_assign"), |
| 656 | OperatorMetadataEntry::assign("-=", "SubAssign", "sub_assign"), |
| 657 | OperatorMetadataEntry::assign("*=", "MulAssign", "mul_assign"), |
| 658 | OperatorMetadataEntry::assign("/=", "DivAssign", "div_assign"), |
| 659 | OperatorMetadataEntry::assign("%=", "RemAssign", "rem_assign"), |
| 660 | OperatorMetadataEntry::assign("&=", "BitAndAssign", "bitand_assign"), |
| 661 | OperatorMetadataEntry::assign("|=", "BitOrAssign", "bitor_assign"), |
| 662 | OperatorMetadataEntry::assign("^=", "BitXorAssign", "bitxor_assign"), |
| 663 | OperatorMetadataEntry::assign("<<=", "ShlAssign", "shl_assign"), |
| 664 | OperatorMetadataEntry::assign(">>=", "ShrAssign", "shr_assign"), |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 665 | ]; |
Michael VanBemmel | 5014b3e | 2022-08-03 16:27:34 -0700 | [diff] [blame] | 666 | OperatorMetadata { |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 667 | by_cc_name_and_params: ENTRIES.iter().map(|e| ((e.cc_name, e.cc_params), *e)).collect(), |
Michael VanBemmel | 5014b3e | 2022-08-03 16:27:34 -0700 | [diff] [blame] | 668 | } |
| 669 | }); |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 670 | |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 671 | /// Returns the shape of the generated Rust API for a given function definition. |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 672 | /// |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 673 | /// If the shape is a trait, this also mutates the parameter types to be |
| 674 | /// trait-compatible. In particular, types which would be `impl Ctor<Output=T>` |
| 675 | /// become a `RvalueReference<'_, T>`. |
| 676 | /// |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 677 | /// Returns: |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 678 | /// |
| 679 | /// * `Err(_)`: something went wrong importing this function. |
| 680 | /// * `Ok(None)`: the function imported as "nothing". (For example, a defaulted |
| 681 | /// destructor might be mapped to no `Drop` impl at all.) |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 682 | /// * `Ok((func_name, impl_kind))`: The function name and ImplKind. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 683 | fn api_func_shape( |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 684 | db: &dyn BindingsGenerator, |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 685 | func: &Func, |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 686 | param_types: &mut [RsTypeKind], |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 687 | ) -> Result<Option<(Ident, ImplKind)>> { |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 688 | let ir = db.ir(); |
Michael VanBemmel | 5014b3e | 2022-08-03 16:27:34 -0700 | [diff] [blame] | 689 | let op_meta = &*OPERATOR_METADATA; |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 690 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 691 | let maybe_record: Option<&Rc<Record>> = ir.record_for_member_func(func)?; |
Devin Jeanpierre | 22f9149 | 2022-04-06 13:01:23 -0700 | [diff] [blame] | 692 | let has_pointer_params = param_types.iter().any(|p| matches!(p, RsTypeKind::Pointer { .. })); |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 693 | let impl_kind: ImplKind; |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 694 | let func_name: syn::Ident; |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 695 | |
| 696 | let adl_check_required_and_failed = if let Some(decl_id) = func.adl_enclosing_record { |
Lukasz Anforowicz | 42ab93b | 2022-08-31 11:17:59 -0700 | [diff] [blame] | 697 | let adl_enclosing_record = ir |
| 698 | .find_decl::<Rc<Record>>(decl_id) |
| 699 | .with_context(|| format!("Failed to look up `adl_enclosing_record` of {:?}", func))?; |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 700 | !is_visible_by_adl(adl_enclosing_record, param_types) |
| 701 | } else { |
| 702 | false |
| 703 | }; |
| 704 | |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 705 | match &func.name { |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 706 | UnqualifiedIdentifier::Operator(_) | UnqualifiedIdentifier::Identifier(_) |
| 707 | if adl_check_required_and_failed => |
| 708 | { |
| 709 | return Ok(None); |
| 710 | } |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 711 | UnqualifiedIdentifier::Operator(op) if op.name == "==" => { |
Devin Jeanpierre | 8dd193a | 2022-06-03 10:57:21 -0700 | [diff] [blame] | 712 | assert_eq!( |
| 713 | param_types.len(), |
| 714 | 2, |
| 715 | "Unexpected number of parameters in operator==: {func:?}" |
| 716 | ); |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 717 | let lhs_record = match ¶m_types[0] { |
| 718 | RsTypeKind::Reference { referent: lhs, mutability: Mutability::Const, .. } => { |
| 719 | if let RsTypeKind::Record { record: lhs_record, .. } = &**lhs { |
| 720 | lhs_record |
| 721 | } else { |
| 722 | bail!( |
| 723 | "operator== where lhs param is reference that doesn't refer to a record", |
| 724 | ); |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 725 | } |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 726 | } |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 727 | RsTypeKind::Record { record: lhs_record, .. } => lhs_record, |
| 728 | _ => bail!( |
| 729 | "operator== where lhs operand is not record nor const reference to record" |
| 730 | ), |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 731 | }; |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 732 | let params = match ¶m_types[1] { |
| 733 | RsTypeKind::Reference { referent: rhs, mutability: Mutability::Const, .. } => { |
| 734 | if let RsTypeKind::Record { .. } = &**rhs { |
| 735 | vec![(**rhs).clone()] |
| 736 | } else { |
| 737 | bail!( |
| 738 | "operator== where rhs param is reference that doesn't refer to a record", |
| 739 | ); |
| 740 | } |
| 741 | } |
| 742 | record @ RsTypeKind::Record { .. } => vec![record.clone()], |
| 743 | _ => bail!( |
| 744 | "operator== where rhs operand is not record nor const reference to record" |
| 745 | ), |
| 746 | }; |
| 747 | func_name = make_rs_ident("eq"); |
| 748 | impl_kind = ImplKind::new_trait( |
| 749 | TraitName::PartialEq { params }, |
| 750 | lhs_record, |
| 751 | &ir, |
| 752 | /* format_first_param_as_self= */ true, |
| 753 | /* force_const_reference_params= */ true, |
| 754 | )?; |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 755 | } |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 756 | UnqualifiedIdentifier::Operator(op) if op.name == "<=>" => { |
| 757 | bail!("Three-way comparison operator not yet supported (b/219827738)"); |
| 758 | } |
| 759 | UnqualifiedIdentifier::Operator(op) if op.name == "<" => { |
| 760 | assert_eq!( |
| 761 | param_types.len(), |
| 762 | 2, |
| 763 | "Unexpected number of parameters in operator<: {func:?}" |
| 764 | ); |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 765 | let lhs_record = match ¶m_types[0] { |
| 766 | RsTypeKind::Reference { referent: lhs, mutability: Mutability::Const, .. } => { |
| 767 | if let RsTypeKind::Record { record: lhs_record, .. } = &**lhs { |
| 768 | lhs_record |
| 769 | } else { |
| 770 | bail!( |
| 771 | "operator== where lhs param is reference that doesn't refer to a record", |
| 772 | ); |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 773 | } |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 774 | } |
| 775 | RsTypeKind::Record { record: lhs_record, .. } => lhs_record, |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 776 | _ => { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 777 | bail!("operator< where lhs operand is not record nor const reference to record") |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 778 | } |
| 779 | }; |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 780 | let (rhs_record, params) = match ¶m_types[1] { |
| 781 | RsTypeKind::Reference { referent: rhs, mutability: Mutability::Const, .. } => { |
| 782 | if let RsTypeKind::Record { record: rhs_record, .. } = &**rhs { |
| 783 | (rhs_record, vec![(**rhs).clone()]) |
| 784 | } else { |
| 785 | bail!( |
| 786 | "operator== where rhs param is reference that doesn't refer to a record", |
| 787 | ); |
| 788 | } |
| 789 | } |
| 790 | record @ RsTypeKind::Record { record: rhs_record, .. } => { |
| 791 | (rhs_record, vec![record.clone()]) |
| 792 | } |
| 793 | _ => { |
| 794 | bail!("operator< where rhs operand is not record nor const reference to record") |
| 795 | } |
| 796 | }; |
| 797 | // Even though Rust and C++ allow operator< to be implemented on different |
| 798 | // types, we don't generate bindings for them at this moment. The |
| 799 | // issue is that our canonical implementation of partial_cmp relies |
| 800 | // on transitivity. This would require checking that both lt(&T1, |
| 801 | // &T2) and lt(&T2, &T1) are implemented. In other words, both lt |
| 802 | // implementations would need to query for the existence of the other, which |
| 803 | // would create a cyclic dependency. |
| 804 | if lhs_record != rhs_record { |
| 805 | bail!("operator< where lhs and rhs are not the same type."); |
| 806 | } |
| 807 | // PartialOrd requires PartialEq, so we need to make sure operator== is |
| 808 | // implemented for this Record type. |
| 809 | match get_binding( |
| 810 | db, |
| 811 | UnqualifiedIdentifier::Operator(Operator { name: "==".to_string() }), |
| 812 | param_types, |
| 813 | ) { |
| 814 | Some((_, ImplKind::Trait { trait_name: TraitName::PartialEq { .. }, .. })) => { |
| 815 | func_name = make_rs_ident("lt"); |
| 816 | impl_kind = ImplKind::new_trait( |
| 817 | TraitName::PartialOrd { params }, |
| 818 | lhs_record, |
| 819 | &ir, |
| 820 | /* format_first_param_as_self= */ |
| 821 | true, |
| 822 | /* force_const_reference_params= */ true, |
| 823 | )?; |
| 824 | } |
| 825 | _ => bail!("operator< where operator== is missing."), |
| 826 | } |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 827 | } |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 828 | UnqualifiedIdentifier::Operator(op) if op.name == "=" => { |
| 829 | assert_eq!( |
| 830 | param_types.len(), |
| 831 | 2, |
| 832 | "Unexpected number of parameters in operator=: {func:?}" |
| 833 | ); |
| 834 | let record = |
| 835 | maybe_record.ok_or_else(|| anyhow!("operator= must be a member function."))?; |
| 836 | if record.is_unpin() { |
| 837 | bail!("operator= for Unpin types is not yet supported."); |
| 838 | } |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 839 | materialize_ctor_in_caller(func, param_types); |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 840 | let rhs = ¶m_types[1]; |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 841 | impl_kind = { |
| 842 | ImplKind::Trait { |
| 843 | trait_name: TraitName::Other { |
| 844 | name: quote! {::ctor::Assign}, |
| 845 | params: vec![rhs.clone()], |
| 846 | is_unsafe_fn: false, |
| 847 | }, |
| 848 | record_name: make_rs_ident(&record.rs_name), |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 849 | record_qualifier: NamespaceQualifier::new(record.id, &ir)?, |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 850 | impl_for: ImplFor::T, |
| 851 | trait_generic_params: vec![], |
| 852 | format_first_param_as_self: true, |
| 853 | drop_return: true, |
| 854 | associated_return_type: None, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 855 | force_const_reference_params: false, |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 856 | } |
| 857 | }; |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 858 | func_name = make_rs_ident("assign"); |
| 859 | } |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 860 | UnqualifiedIdentifier::Operator(op) => match op_meta |
| 861 | .by_cc_name_and_params |
| 862 | .get(&(op.name.as_str(), param_types.len())) |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 863 | { |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 864 | Some(OperatorMetadataEntry { |
| 865 | trait_name, |
| 866 | method_name, |
| 867 | is_compound_assignment: false, |
| 868 | .. |
| 869 | }) => { |
| 870 | materialize_ctor_in_caller(func, param_types); |
Lukasz Anforowicz | cb1f9c2 | 2022-09-02 12:40:01 -0700 | [diff] [blame] | 871 | let (record, impl_for) = match ¶m_types[0] { |
| 872 | RsTypeKind::Record { record, .. } => (&**record, ImplFor::T), |
| 873 | RsTypeKind::Reference { referent, .. } => ( |
| 874 | match &**referent { |
| 875 | RsTypeKind::Record { record, .. } => &**record, |
| 876 | _ => bail!("Expected first parameter referent to be a record"), |
| 877 | }, |
| 878 | ImplFor::RefT, |
| 879 | ), |
| 880 | RsTypeKind::RvalueReference { .. } => { |
| 881 | bail!("Not yet supported for rvalue references (b/219826128)") |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 882 | } |
Lukasz Anforowicz | cb1f9c2 | 2022-09-02 12:40:01 -0700 | [diff] [blame] | 883 | _ => bail!("Expected first parameter to be a record or reference"), |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 884 | }; |
| 885 | |
| 886 | let trait_name = make_rs_ident(trait_name); |
| 887 | impl_kind = ImplKind::Trait { |
| 888 | record_name: make_rs_ident(&record.rs_name), |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 889 | record_qualifier: NamespaceQualifier::new(record.id, &ir)?, |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 890 | trait_name: TraitName::Other { |
| 891 | name: quote! {::std::ops::#trait_name}, |
| 892 | params: param_types[1..].to_vec(), |
| 893 | is_unsafe_fn: false, |
| 894 | }, |
| 895 | impl_for, |
| 896 | trait_generic_params: vec![], |
| 897 | format_first_param_as_self: true, |
| 898 | drop_return: false, |
| 899 | associated_return_type: Some(make_rs_ident("Output")), |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 900 | force_const_reference_params: false, |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 901 | }; |
| 902 | func_name = make_rs_ident(method_name); |
| 903 | } |
| 904 | Some(OperatorMetadataEntry { |
| 905 | trait_name, |
| 906 | method_name, |
| 907 | is_compound_assignment: true, |
| 908 | .. |
| 909 | }) => { |
| 910 | materialize_ctor_in_caller(func, param_types); |
| 911 | let record = match ¶m_types[0] { |
| 912 | RsTypeKind::Record { .. } => { |
| 913 | bail!("Compound assignment with by-value left-hand side is not supported") |
| 914 | } |
| 915 | RsTypeKind::Reference { mutability: Mutability::Const, .. } => { |
| 916 | bail!("Compound assignment with const left-hand side is not supported") |
| 917 | } |
| 918 | RsTypeKind::Reference { referent, mutability: Mutability::Mut, .. } => { |
Michael VanBemmel | 83eca6b | 2022-07-20 10:16:38 -0700 | [diff] [blame] | 919 | match &**referent { |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 920 | RsTypeKind::Record { record, .. } => &**maybe_record.unwrap_or(record), |
Michael VanBemmel | 83eca6b | 2022-07-20 10:16:38 -0700 | [diff] [blame] | 921 | _ => bail!("Expected first parameter referent to be a record"), |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 922 | } |
| 923 | } |
Michael VanBemmel | 83eca6b | 2022-07-20 10:16:38 -0700 | [diff] [blame] | 924 | RsTypeKind::RvalueReference { .. } => { |
| 925 | bail!("Not yet supported for rvalue references (b/219826128)") |
| 926 | } |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 927 | RsTypeKind::Pointer { .. } => { |
| 928 | bail!("Not yet supported for pointers with unknown lifetime (b/219826128)") |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 929 | } |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 930 | _ => bail!("Expected first parameter to be a record or reference"), |
| 931 | }; |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 932 | |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 933 | let trait_name = make_rs_ident(trait_name); |
| 934 | impl_kind = ImplKind::Trait { |
| 935 | record_name: make_rs_ident(&record.rs_name), |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 936 | record_qualifier: NamespaceQualifier::new(record.id, &ir)?, |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 937 | trait_name: TraitName::Other { |
| 938 | name: quote! {::std::ops::#trait_name}, |
| 939 | params: param_types[1..].to_vec(), |
| 940 | is_unsafe_fn: false, |
| 941 | }, |
| 942 | impl_for: ImplFor::T, |
| 943 | trait_generic_params: vec![], |
| 944 | format_first_param_as_self: true, |
| 945 | drop_return: true, |
| 946 | associated_return_type: None, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 947 | force_const_reference_params: false, |
Michael VanBemmel | 5bde56f | 2022-08-17 12:05:37 -0700 | [diff] [blame] | 948 | }; |
| 949 | func_name = make_rs_ident(method_name); |
| 950 | } |
| 951 | None => { |
| 952 | bail!( |
| 953 | "Bindings for this kind of operator (operator {op} with {n} parameter(s)) are not supported", |
| 954 | op = &op.name, |
| 955 | n = param_types.len(), |
| 956 | ); |
| 957 | } |
| 958 | }, |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 959 | UnqualifiedIdentifier::Identifier(id) => { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 960 | func_name = make_rs_ident(&id.identifier); |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 961 | match maybe_record { |
| 962 | None => { |
Devin Jeanpierre | 7c3d8ed | 2022-03-29 03:02:04 -0700 | [diff] [blame] | 963 | impl_kind = ImplKind::None { is_unsafe: has_pointer_params }; |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 964 | } |
| 965 | Some(record) => { |
Devin Jeanpierre | d9a6e6c | 2022-03-29 02:55:41 -0700 | [diff] [blame] | 966 | let format_first_param_as_self = if func.is_instance_method() { |
Devin Jeanpierre | 22f9149 | 2022-04-06 13:01:23 -0700 | [diff] [blame] | 967 | let first_param = param_types.first().ok_or_else(|| { |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 968 | anyhow!("Missing `__this` parameter in an instance method: {:?}", func) |
| 969 | })?; |
Devin Jeanpierre | d9a6e6c | 2022-03-29 02:55:41 -0700 | [diff] [blame] | 970 | first_param.is_ref_to(record) |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 971 | } else { |
Devin Jeanpierre | d9a6e6c | 2022-03-29 02:55:41 -0700 | [diff] [blame] | 972 | false |
| 973 | }; |
Devin Jeanpierre | 6784e5e | 2022-03-29 02:59:01 -0700 | [diff] [blame] | 974 | impl_kind = ImplKind::Struct { |
| 975 | record_name: make_rs_ident(&record.rs_name), |
| 976 | format_first_param_as_self, |
Devin Jeanpierre | 7c3d8ed | 2022-03-29 03:02:04 -0700 | [diff] [blame] | 977 | is_unsafe: has_pointer_params, |
Devin Jeanpierre | 6784e5e | 2022-03-29 02:59:01 -0700 | [diff] [blame] | 978 | }; |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 979 | } |
| 980 | }; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 981 | } |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 982 | UnqualifiedIdentifier::Destructor => { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 983 | // Note: to avoid double-destruction of the fields, they are all wrapped in |
| 984 | // ManuallyDrop in this case. See `generate_record`. |
| 985 | let record = |
| 986 | maybe_record.ok_or_else(|| anyhow!("Destructors must be member functions."))?; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 987 | if !should_implement_drop(record) { |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 988 | return Ok(None); |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 989 | } |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 990 | if record.is_unpin() { |
| 991 | impl_kind = ImplKind::new_trait( |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 992 | TraitName::Other { name: quote! {Drop}, params: vec![], is_unsafe_fn: false }, |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 993 | record, |
| 994 | &ir, |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 995 | /* format_first_param_as_self= */ true, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 996 | /* force_const_reference_params= */ |
| 997 | false, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 998 | )?; |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 999 | func_name = make_rs_ident("drop"); |
| 1000 | } else { |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 1001 | materialize_ctor_in_caller(func, param_types); |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 1002 | impl_kind = ImplKind::new_trait( |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 1003 | TraitName::Other { |
| 1004 | name: quote! {::ctor::PinnedDrop}, |
| 1005 | params: vec![], |
| 1006 | is_unsafe_fn: true, |
| 1007 | }, |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 1008 | record, |
| 1009 | &ir, |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 1010 | /* format_first_param_as_self= */ true, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1011 | /* force_const_reference_params= */ false, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1012 | )?; |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 1013 | func_name = make_rs_ident("pinned_drop"); |
| 1014 | } |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 1015 | } |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 1016 | UnqualifiedIdentifier::Constructor => { |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 1017 | let member_func_metadata = func |
| 1018 | .member_func_metadata |
| 1019 | .as_ref() |
| 1020 | .ok_or_else(|| anyhow!("Constructors must be member functions."))?; |
| 1021 | let record = maybe_record |
| 1022 | .ok_or_else(|| anyhow!("Constructors must be associated with a record."))?; |
| 1023 | let instance_method_metadata = |
| 1024 | member_func_metadata |
| 1025 | .instance_method_metadata |
| 1026 | .as_ref() |
| 1027 | .ok_or_else(|| anyhow!("Constructors must be instance methods."))?; |
Devin Jeanpierre | 7c3d8ed | 2022-03-29 03:02:04 -0700 | [diff] [blame] | 1028 | if has_pointer_params { |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 1029 | // TODO(b/216648347): Allow this outside of traits (e.g. after supporting |
| 1030 | // translating C++ constructors into static methods in Rust). |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 1031 | bail!( |
Lukasz Anforowicz | eb19ac6 | 2022-02-05 00:10:16 +0000 | [diff] [blame] | 1032 | "Unsafe constructors (e.g. with no elided or explicit lifetimes) \ |
| 1033 | are intentionally not supported", |
| 1034 | ); |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 1037 | check_by_value(record)?; |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 1038 | materialize_ctor_in_caller(func, param_types); |
Devin Jeanpierre | 6784e5e | 2022-03-29 02:59:01 -0700 | [diff] [blame] | 1039 | let record_name = make_rs_ident(&record.rs_name); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1040 | if !record.is_unpin() { |
| 1041 | func_name = make_rs_ident("ctor_new"); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1042 | |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 1043 | match param_types { |
| 1044 | [] => bail!("Missing `__this` parameter in a constructor: {:?}", func), |
| 1045 | [_this, params @ ..] => { |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1046 | impl_kind = ImplKind::Trait { |
Devin Jeanpierre | 1b8f4a1 | 2022-03-22 21:29:42 +0000 | [diff] [blame] | 1047 | record_name, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1048 | record_qualifier: NamespaceQualifier::new(record.id, &ir)?, |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1049 | trait_name: TraitName::CtorNew(params.iter().cloned().collect()), |
| 1050 | impl_for: ImplFor::T, |
Devin Jeanpierre | a04bce1 | 2022-08-01 13:39:48 -0700 | [diff] [blame] | 1051 | trait_generic_params: vec![], |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1052 | format_first_param_as_self: false, |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 1053 | drop_return: false, |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1054 | associated_return_type: Some(make_rs_ident("CtorType")), |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1055 | force_const_reference_params: false, |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1056 | }; |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1057 | } |
Lukasz Anforowicz | 73326af | 2022-01-05 01:13:10 +0000 | [diff] [blame] | 1058 | } |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1059 | } else { |
| 1060 | match func.params.len() { |
| 1061 | 0 => bail!("Missing `__this` parameter in a constructor: {:?}", func), |
| 1062 | 1 => { |
| 1063 | impl_kind = ImplKind::new_trait( |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 1064 | TraitName::UnpinConstructor { name: quote! {Default}, params: vec![] }, |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 1065 | record, |
| 1066 | &ir, |
Devin Jeanpierre | d9a6e6c | 2022-03-29 02:55:41 -0700 | [diff] [blame] | 1067 | /* format_first_param_as_self= */ false, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1068 | /* force_const_reference_params= */ false, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1069 | )?; |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1070 | func_name = make_rs_ident("default"); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1071 | } |
| 1072 | 2 => { |
Devin Jeanpierre | 22f9149 | 2022-04-06 13:01:23 -0700 | [diff] [blame] | 1073 | if param_types[1].is_shared_ref_to(record) { |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1074 | // Copy constructor |
| 1075 | if should_derive_clone(record) { |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 1076 | return Ok(None); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1077 | } else { |
| 1078 | impl_kind = ImplKind::new_trait( |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 1079 | TraitName::UnpinConstructor { |
| 1080 | name: quote! {Clone}, |
| 1081 | params: vec![], |
| 1082 | }, |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 1083 | record, |
| 1084 | &ir, |
Devin Jeanpierre | d9a6e6c | 2022-03-29 02:55:41 -0700 | [diff] [blame] | 1085 | /* format_first_param_as_self= */ true, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1086 | /* force_const_reference_params= */ false, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1087 | )?; |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1088 | func_name = make_rs_ident("clone"); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } else if !instance_method_metadata.is_explicit_ctor { |
Devin Jeanpierre | 22f9149 | 2022-04-06 13:01:23 -0700 | [diff] [blame] | 1091 | let param_type = ¶m_types[1]; |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 1092 | impl_kind = ImplKind::new_trait( |
| 1093 | TraitName::UnpinConstructor { |
| 1094 | name: quote! {From}, |
| 1095 | params: vec![param_type.clone()], |
| 1096 | }, |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 1097 | record, |
| 1098 | &ir, |
Devin Jeanpierre | d9a6e6c | 2022-03-29 02:55:41 -0700 | [diff] [blame] | 1099 | /* format_first_param_as_self= */ false, |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1100 | /* force_const_reference_params= */ |
| 1101 | false, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1102 | )?; |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1103 | func_name = make_rs_ident("from"); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1104 | } else { |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 1105 | bail!("Not yet supported type of constructor parameter",); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | _ => { |
| 1109 | // TODO(b/216648347): Support bindings for other constructors. |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 1110 | bail!("More than 1 constructor parameter is not supported yet",); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1111 | } |
Lukasz Anforowicz | 73326af | 2022-01-05 01:13:10 +0000 | [diff] [blame] | 1112 | } |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | } |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 1116 | Ok(Some((func_name, impl_kind))) |
| 1117 | } |
| 1118 | |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 1119 | /// Returns the generated bindings for a function with the given name and param |
| 1120 | /// types. If none exists, returns None. |
| 1121 | fn get_binding( |
| 1122 | db: &dyn BindingsGenerator, |
| 1123 | expected_function_name: UnqualifiedIdentifier, |
| 1124 | expected_param_types: &[RsTypeKind], |
| 1125 | ) -> Option<(Ident, ImplKind)> { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1126 | db.ir() |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 1127 | // TODO(jeanpierreda): make this O(1) using a hash table lookup. |
| 1128 | .functions() |
| 1129 | .filter(|function| { |
| 1130 | function.name == expected_function_name |
| 1131 | && generate_func(db, (*function).clone()).ok().flatten().is_some() |
| 1132 | }) |
| 1133 | .find_map(|function| { |
| 1134 | let mut function_param_types = function |
| 1135 | .params |
| 1136 | .iter() |
| 1137 | .map(|param| db.rs_type_kind(param.type_.rs_type.clone())) |
| 1138 | .collect::<Result<Vec<_>>>() |
| 1139 | .ok()?; |
| 1140 | if !function_param_types.iter().eq(expected_param_types) { |
| 1141 | return None; |
| 1142 | } |
| 1143 | api_func_shape(db, function, &mut function_param_types).ok().flatten() |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1144 | }) |
| 1145 | } |
| 1146 | |
| 1147 | /// Returns whether the given record either implements or derives the Clone |
| 1148 | /// trait. |
| 1149 | fn is_record_clonable(db: &dyn BindingsGenerator, record: Rc<Record>) -> bool { |
| 1150 | if !record.is_unpin() { |
| 1151 | return false; |
| 1152 | } |
| 1153 | should_derive_clone(&record) |
| 1154 | || db |
| 1155 | .ir() |
| 1156 | // TODO(jeanpierreda): make this O(1) using a hash table lookup. |
| 1157 | .functions() |
| 1158 | .filter(|function| { |
| 1159 | function.name == UnqualifiedIdentifier::Constructor |
| 1160 | // __this is always the first parameter of constructors |
| 1161 | && function.params.len() == 2 |
| 1162 | }) |
| 1163 | .any(|function| { |
| 1164 | let mut function_param_types = function |
| 1165 | .params |
| 1166 | .iter() |
| 1167 | .map(|param| db.rs_type_kind(param.type_.rs_type.clone())) |
| 1168 | .collect::<Result<Vec<_>>>() |
| 1169 | .ok() |
| 1170 | .unwrap_or_default(); |
| 1171 | if function.params.len() != 2 || !function_param_types[1].is_shared_ref_to(&record) |
| 1172 | { |
| 1173 | return false; |
| 1174 | } |
| 1175 | api_func_shape(db, function, &mut function_param_types) |
| 1176 | .ok() |
| 1177 | .flatten() |
| 1178 | .map_or(false, |(func_name, _)| func_name == *"clone") |
| 1179 | }) |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 1182 | /// Mutates the provided parameters so that nontrivial by-value parameters are, |
| 1183 | /// instead, materialized in the caller and passed by rvalue reference. |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 1184 | fn materialize_ctor_in_caller(func: &Func, params: &mut [RsTypeKind]) { |
| 1185 | let mut existing_lifetime_params: HashSet<Rc<str>> = |
| 1186 | params.iter().flat_map(|param| param.lifetimes().map(|lifetime| lifetime.0)).collect(); |
| 1187 | let mut new_lifetime_param = |mut lifetime_name: String| { |
| 1188 | let suffix_start = lifetime_name.len(); |
| 1189 | let mut next_suffix = 2; |
| 1190 | loop { |
| 1191 | if !existing_lifetime_params.contains(&*lifetime_name) { |
| 1192 | let lifetime_name = <Rc<str>>::from(lifetime_name); |
| 1193 | existing_lifetime_params.insert(lifetime_name.clone()); |
| 1194 | return Lifetime(lifetime_name); |
| 1195 | } |
| 1196 | lifetime_name.truncate(suffix_start); |
| 1197 | write!(lifetime_name, "_{next_suffix}").unwrap(); |
| 1198 | next_suffix += 1; |
| 1199 | } |
| 1200 | }; |
| 1201 | for (func_param, param) in func.params.iter().zip(params.iter_mut()) { |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 1202 | if param.is_unpin() { |
| 1203 | continue; |
| 1204 | } |
| 1205 | let value = std::mem::replace(param, RsTypeKind::Unit); // Temporarily swap in a garbage value. |
| 1206 | *param = RsTypeKind::RvalueReference { |
| 1207 | referent: Rc::new(value), |
| 1208 | mutability: Mutability::Mut, |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 1209 | lifetime: new_lifetime_param(func_param.identifier.identifier.clone()), |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 1210 | }; |
| 1211 | } |
| 1212 | } |
| 1213 | |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 1214 | /// Generates Rust source code for a given `Func`. |
| 1215 | /// |
| 1216 | /// Returns: |
| 1217 | /// |
| 1218 | /// * `Err(_)`: couldn't import the function, emit an `UnsupportedItem`. |
| 1219 | /// * `Ok(None)`: the function imported as "nothing". (For example, a defaulted |
| 1220 | /// destructor might be mapped to no `Drop` impl at all.) |
| 1221 | /// * `Ok((rs_api, rs_thunk, function_id))`: The Rust function definition, |
| 1222 | /// thunk FFI definition, and function ID. |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 1223 | fn generate_func( |
| 1224 | db: &dyn BindingsGenerator, |
| 1225 | func: Rc<Func>, |
Lukasz Anforowicz | f8cfa56 | 2022-09-22 11:06:43 -0700 | [diff] [blame] | 1226 | ) -> Result<Option<Rc<(RsSnippet, RsSnippet, Rc<FunctionId>)>>> { |
Lukasz Anforowicz | 8d06420 | 2022-09-01 07:31:06 -0700 | [diff] [blame] | 1227 | let ir = db.ir(); |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1228 | let mut features = BTreeSet::new(); |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 1229 | let mut param_types = func |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 1230 | .params |
| 1231 | .iter() |
| 1232 | .map(|p| { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 1233 | db.rs_type_kind(p.type_.rs_type.clone()).with_context(|| { |
Devin Jeanpierre | 9c34da6 | 2022-03-31 04:57:16 -0700 | [diff] [blame] | 1234 | format!("Failed to process type of parameter {:?} on {:?}", p, func) |
| 1235 | }) |
| 1236 | }) |
| 1237 | .collect::<Result<Vec<_>>>()?; |
| 1238 | |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 1239 | let (func_name, mut impl_kind) = |
Michael VanBemmel | 32c26df | 2022-08-03 16:08:58 -0700 | [diff] [blame] | 1240 | if let Some(values) = api_func_shape(db, &func, &mut param_types)? { |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 1241 | values |
| 1242 | } else { |
| 1243 | return Ok(None); |
| 1244 | }; |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1245 | let namespace_qualifier = NamespaceQualifier::new(func.id, &ir)?.format_for_rs(); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1246 | |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1247 | let mut return_type = db |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 1248 | .rs_type_kind(func.return_type.rs_type.clone()) |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1249 | .with_context(|| format!("Failed to format return type for {:?}", &func))?; |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 1250 | return_type.check_by_value()?; |
Devin Jeanpierre | d9cecff | 2022-03-29 02:53:58 -0700 | [diff] [blame] | 1251 | let param_idents = |
| 1252 | func.params.iter().map(|p| make_rs_ident(&p.identifier.identifier)).collect_vec(); |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1253 | let thunk = generate_func_thunk(db, &func, ¶m_idents, ¶m_types, &return_type)?; |
Devin Jeanpierre | d9cecff | 2022-03-29 02:53:58 -0700 | [diff] [blame] | 1254 | |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1255 | // If the Rust trait require a function to take the params by const reference |
| 1256 | // and the thunk takes some of its params by value then we should add a const |
| 1257 | // reference around these Rust func params and clone the records when calling |
| 1258 | // the thunk. Since some params might require cloning while others don't, we |
| 1259 | // need to store this information for each param. |
| 1260 | let (mut param_types, clone_suffixes) = if let ImplKind::Trait { |
| 1261 | force_const_reference_params: true, |
| 1262 | .. |
| 1263 | } = impl_kind |
| 1264 | { |
| 1265 | let mut clone_suffixes = Vec::with_capacity(param_types.len()); |
| 1266 | ( |
| 1267 | param_types |
| 1268 | .into_iter() |
| 1269 | .map(|param_type| |
| 1270 | {if let RsTypeKind::Record { record: param_record, .. } = ¶m_type { |
| 1271 | if !is_record_clonable(db, param_record.clone()) { |
| 1272 | bail!( |
| 1273 | "function requires const ref params in Rust but C++ takes non-cloneable record {:?} by value {:?}", |
| 1274 | param_record, |
| 1275 | func, |
| 1276 | ); |
| 1277 | } |
| 1278 | clone_suffixes.push(quote!{.clone()}); |
| 1279 | Ok(RsTypeKind::Reference { |
| 1280 | referent: Rc::new(param_type.clone()), |
| 1281 | mutability: Mutability::Const, |
| 1282 | lifetime: Lifetime::new("_"), |
| 1283 | }) |
| 1284 | } else { |
| 1285 | clone_suffixes.push(quote!{}); |
| 1286 | Ok(param_type) |
| 1287 | }}) |
| 1288 | .collect::<Result<Vec<_>>>()?, |
| 1289 | clone_suffixes, |
| 1290 | ) |
| 1291 | } else { |
| 1292 | let param_len = param_types.len(); |
| 1293 | (param_types, vec![quote! {}; param_len]) |
| 1294 | }; |
| 1295 | |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1296 | let BindingsSignature { |
| 1297 | lifetimes, |
| 1298 | params: api_params, |
| 1299 | return_type_fragment: mut quoted_return_type, |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 1300 | thunk_prepare, |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1301 | thunk_args, |
| 1302 | } = function_signature( |
| 1303 | &mut features, |
| 1304 | &func, |
| 1305 | &impl_kind, |
| 1306 | ¶m_idents, |
| 1307 | &mut param_types, |
| 1308 | &mut return_type, |
| 1309 | )?; |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1310 | |
| 1311 | let api_func_def = { |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1312 | // TODO(b/200067242): the Pin-wrapping code doesn't know to wrap &mut |
| 1313 | // MaybeUninit<T> in Pin if T is !Unpin. It should understand |
| 1314 | // 'structural pinning', so that we do not need into_inner_unchecked() |
| 1315 | // here. |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 1316 | let thunk_ident = thunk_ident(&func); |
Devin Jeanpierre | 7bddfdb | 2022-03-14 11:04:40 +0000 | [diff] [blame] | 1317 | let func_body = match &impl_kind { |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 1318 | ImplKind::Trait { trait_name: TraitName::UnpinConstructor { .. }, .. } => { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1319 | // SAFETY: A user-defined constructor is not guaranteed to |
| 1320 | // initialize all the fields. To make the `assume_init()` call |
| 1321 | // below safe, the memory is zero-initialized first. This is a |
| 1322 | // bit safer, because zero-initialized memory represents a valid |
| 1323 | // value for the currently supported field types (this may |
| 1324 | // change once the bindings generator starts supporting |
| 1325 | // reference fields). TODO(b/213243309): Double-check if |
| 1326 | // zero-initialization is desirable here. |
| 1327 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 1328 | let mut tmp = ::std::mem::MaybeUninit::<Self>::zeroed(); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1329 | unsafe { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 1330 | crate::detail::#thunk_ident( &mut tmp #( , #thunk_args )* ); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1331 | tmp.assume_init() |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 1332 | } |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 1333 | } |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1334 | } |
Devin Jeanpierre | 7bddfdb | 2022-03-14 11:04:40 +0000 | [diff] [blame] | 1335 | _ => { |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1336 | // Note: for the time being, all !Unpin values are treated as if they were not |
| 1337 | // trivially relocatable. We could, in the special case of trivial !Unpin types, |
| 1338 | // not generate the thunk at all, but this would be a bit of extra work. |
| 1339 | // |
| 1340 | // TODO(jeanpierreda): separately handle non-Unpin and non-trivial types. |
| 1341 | let mut body = if return_type.is_unpin() { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1342 | quote! { crate::detail::#thunk_ident( #( #thunk_args #clone_suffixes ),* ) } |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1343 | } else { |
| 1344 | quote! { |
| 1345 | ::ctor::FnCtor::new(move |dest: ::std::pin::Pin<&mut ::std::mem::MaybeUninit<#return_type>>| { |
| 1346 | crate::detail::#thunk_ident(::std::pin::Pin::into_inner_unchecked(dest) #( , #thunk_args )*); |
| 1347 | }) |
| 1348 | } |
| 1349 | }; |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 1350 | // Discard the return value if requested (for example, when calling a C++ |
| 1351 | // operator that returns a value from a Rust trait that returns |
| 1352 | // unit). |
| 1353 | if let ImplKind::Trait { drop_return: true, .. } = impl_kind { |
| 1354 | if return_type.is_unpin() { |
| 1355 | // If it's unpin, just discard it: |
| 1356 | body = quote! { #body; }; |
| 1357 | } else { |
| 1358 | // Otherwise, in order to discard the return value and return void, we |
| 1359 | // need to run the constructor. |
| 1360 | body = quote! {let _ = ::ctor::emplace!(#body);}; |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 1361 | } |
Michael VanBemmel | f665130 | 2022-08-03 15:56:59 -0700 | [diff] [blame] | 1362 | |
| 1363 | // We would need to do this, but it's no longer used: |
| 1364 | // return_type = RsTypeKind::Unit; |
| 1365 | let _ = return_type; // proof that we don't need to update it. |
| 1366 | quoted_return_type = quote! {}; |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 1367 | } |
Devin Jeanpierre | 7bddfdb | 2022-03-14 11:04:40 +0000 | [diff] [blame] | 1368 | // Only need to wrap everything in an `unsafe { ... }` block if |
| 1369 | // the *whole* api function is safe. |
Devin Jeanpierre | 7c3d8ed | 2022-03-29 03:02:04 -0700 | [diff] [blame] | 1370 | if !impl_kind.is_unsafe() { |
Devin Jeanpierre | 7bddfdb | 2022-03-14 11:04:40 +0000 | [diff] [blame] | 1371 | body = quote! { unsafe { #body } }; |
| 1372 | } |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 1373 | quote! { |
| 1374 | #thunk_prepare |
| 1375 | #body |
| 1376 | } |
Devin Jeanpierre | 7bddfdb | 2022-03-14 11:04:40 +0000 | [diff] [blame] | 1377 | } |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1378 | }; |
| 1379 | |
Devin Jeanpierre | 7c3d8ed | 2022-03-29 03:02:04 -0700 | [diff] [blame] | 1380 | let pub_ = match impl_kind { |
| 1381 | ImplKind::None { .. } | ImplKind::Struct { .. } => quote! { pub }, |
| 1382 | ImplKind::Trait { .. } => quote! {}, |
| 1383 | }; |
| 1384 | let unsafe_ = if impl_kind.is_unsafe() { |
| 1385 | quote! { unsafe } |
| 1386 | } else { |
| 1387 | quote! {} |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1388 | }; |
| 1389 | |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 1390 | let fn_generic_params: TokenStream; |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 1391 | if let ImplKind::Trait { trait_name, trait_generic_params, impl_for, .. } = &mut impl_kind { |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1392 | // When the impl block is for some kind of reference to T, consider the lifetime |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1393 | // parameters on the self parameter to be trait lifetimes so they can be |
| 1394 | // introduced before they are used. |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1395 | let first_param_lifetimes = match (impl_for, param_types.first()) { |
| 1396 | (ImplFor::RefT, Some(first_param)) => Some(first_param.lifetimes()), |
| 1397 | _ => None, |
| 1398 | }; |
| 1399 | |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1400 | let trait_lifetimes: HashSet<Lifetime> = |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1401 | trait_name.lifetimes().chain(first_param_lifetimes.into_iter().flatten()).collect(); |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 1402 | fn_generic_params = format_generic_params( |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1403 | lifetimes.iter().filter(|lifetime| !trait_lifetimes.contains(lifetime)), |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1404 | std::iter::empty::<syn::Ident>(), |
Devin Jeanpierre | e77fa7e | 2022-06-02 14:05:58 -0700 | [diff] [blame] | 1405 | ); |
Devin Jeanpierre | a04bce1 | 2022-08-01 13:39:48 -0700 | [diff] [blame] | 1406 | *trait_generic_params = lifetimes |
| 1407 | .iter() |
| 1408 | .filter_map(|lifetime| { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1409 | if trait_lifetimes.contains(lifetime) { Some(quote! {#lifetime}) } else { None } |
Devin Jeanpierre | a04bce1 | 2022-08-01 13:39:48 -0700 | [diff] [blame] | 1410 | }) |
| 1411 | .collect(); |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 1412 | } else { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1413 | fn_generic_params = format_generic_params(&lifetimes, std::iter::empty::<syn::Ident>()); |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 1414 | } |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 1415 | |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1416 | let function_return_type = match &impl_kind { |
| 1417 | ImplKind::Trait { associated_return_type: Some(ident), .. } => quote! {Self::#ident}, |
| 1418 | _ => quoted_return_type.clone(), |
| 1419 | }; |
| 1420 | let arrow = if !function_return_type.is_empty() { |
| 1421 | quote! {->} |
| 1422 | } else { |
| 1423 | quote! {} |
| 1424 | }; |
| 1425 | |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1426 | quote! { |
| 1427 | #[inline(always)] |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 1428 | #pub_ #unsafe_ fn #func_name #fn_generic_params( |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1429 | #( #api_params ),* ) #arrow #function_return_type { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1430 | #func_body |
| 1431 | } |
Lukasz Anforowicz | 13cf749 | 2021-12-22 15:29:52 +0000 | [diff] [blame] | 1432 | } |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 1433 | }; |
| 1434 | |
Devin Jeanpierre | d9cecff | 2022-03-29 02:53:58 -0700 | [diff] [blame] | 1435 | let doc_comment = generate_doc_comment(&func.doc_comment); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1436 | let api_func: TokenStream; |
| 1437 | let function_id: FunctionId; |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1438 | match impl_kind { |
Devin Jeanpierre | 7c3d8ed | 2022-03-29 03:02:04 -0700 | [diff] [blame] | 1439 | ImplKind::None { .. } => { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1440 | api_func = quote! { #doc_comment #api_func_def }; |
Lukasz Anforowicz | 8d06420 | 2022-09-01 07:31:06 -0700 | [diff] [blame] | 1441 | function_id = FunctionId { |
| 1442 | self_type: None, |
| 1443 | function_path: syn::parse2(quote! { #namespace_qualifier #func_name }).unwrap(), |
| 1444 | }; |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1445 | } |
Devin Jeanpierre | 6784e5e | 2022-03-29 02:59:01 -0700 | [diff] [blame] | 1446 | ImplKind::Struct { record_name, .. } => { |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1447 | api_func = quote! { impl #record_name { #doc_comment #api_func_def } }; |
| 1448 | function_id = FunctionId { |
| 1449 | self_type: None, |
Lukasz Anforowicz | 8d06420 | 2022-09-01 07:31:06 -0700 | [diff] [blame] | 1450 | function_path: syn::parse2(quote! { |
| 1451 | #namespace_qualifier #record_name :: #func_name |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 1452 | }) |
| 1453 | .unwrap(), |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1454 | }; |
| 1455 | } |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1456 | ImplKind::Trait { |
| 1457 | trait_name, |
| 1458 | record_name, |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1459 | record_qualifier, |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1460 | impl_for, |
| 1461 | trait_generic_params, |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1462 | associated_return_type, |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1463 | .. |
| 1464 | } => { |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1465 | let extra_body = if let Some(name) = associated_return_type { |
| 1466 | let quoted_return_type = if quoted_return_type.is_empty() { |
| 1467 | quote! {()} |
| 1468 | } else { |
| 1469 | quoted_return_type |
| 1470 | }; |
| 1471 | quote! { |
| 1472 | type #name = #quoted_return_type; |
| 1473 | } |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1474 | } else if let TraitName::PartialOrd { ref params } = trait_name { |
| 1475 | let param = params.get(0).ok_or_else(|| anyhow!("No parameter to PartialOrd"))?; |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 1476 | quote! { |
| 1477 | #[inline(always)] |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1478 | fn partial_cmp(&self, other: & #param) -> Option<core::cmp::Ordering> { |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 1479 | if self == other { |
| 1480 | return Some(core::cmp::Ordering::Equal); |
| 1481 | } |
| 1482 | if self < other { |
| 1483 | return Some(core::cmp::Ordering::Less); |
| 1484 | } |
| 1485 | if other < self { |
| 1486 | return Some(core::cmp::Ordering::Greater); |
| 1487 | } |
| 1488 | None |
| 1489 | } |
| 1490 | } |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 1491 | } else { |
| 1492 | quote! {} |
| 1493 | }; |
| 1494 | |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1495 | let extra_items; |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1496 | let trait_generic_params = |
| 1497 | format_generic_params(/* lifetimes= */ &[], trait_generic_params); |
Devin Jeanpierre | 46d515c | 2022-05-03 02:36:54 -0700 | [diff] [blame] | 1498 | match &trait_name { |
| 1499 | TraitName::CtorNew(params) => { |
Devin Jeanpierre | 46d515c | 2022-05-03 02:36:54 -0700 | [diff] [blame] | 1500 | if let [single_param] = params.as_slice() { |
| 1501 | extra_items = quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 1502 | impl #trait_generic_params ::ctor::CtorNew<(#single_param,)> for #record_name { |
Devin Jeanpierre | 46d515c | 2022-05-03 02:36:54 -0700 | [diff] [blame] | 1503 | #extra_body |
| 1504 | |
| 1505 | #[inline (always)] |
| 1506 | fn ctor_new(args: (#single_param,)) -> Self::CtorType { |
| 1507 | let (arg,) = args; |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 1508 | <Self as ::ctor::CtorNew<#single_param>>::ctor_new(arg) |
Devin Jeanpierre | 46d515c | 2022-05-03 02:36:54 -0700 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | } else { |
| 1513 | extra_items = quote! {} |
| 1514 | } |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 1515 | } |
| 1516 | _ => { |
| 1517 | extra_items = quote! {}; |
| 1518 | } |
| 1519 | }; |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1520 | let impl_for = match impl_for { |
| 1521 | ImplFor::T => quote! { #record_name }, |
| 1522 | ImplFor::RefT => { |
| 1523 | let param = ¶m_types[0]; |
| 1524 | quote! { #param } |
| 1525 | } |
| 1526 | }; |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 1527 | api_func = quote! { |
| 1528 | #doc_comment |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1529 | impl #trait_generic_params #trait_name for #impl_for { |
Devin Jeanpierre | 46d515c | 2022-05-03 02:36:54 -0700 | [diff] [blame] | 1530 | #extra_body |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 1531 | #api_func_def |
| 1532 | } |
Devin Jeanpierre | 46d515c | 2022-05-03 02:36:54 -0700 | [diff] [blame] | 1533 | #extra_items |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 1534 | }; |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1535 | let record_qualifier = record_qualifier.format_for_rs(); |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1536 | function_id = FunctionId { |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1537 | self_type: Some(syn::parse2(quote! { #record_qualifier #record_name }).unwrap()), |
Devin Jeanpierre | 56fda1c | 2022-07-27 07:55:46 -0700 | [diff] [blame] | 1538 | function_path: syn::parse2(quote! { #trait_name :: #func_name }).unwrap(), |
Lukasz Anforowicz | ab65e29 | 2022-01-14 23:04:21 +0000 | [diff] [blame] | 1539 | }; |
| 1540 | } |
| 1541 | } |
| 1542 | |
Lukasz Anforowicz | f8cfa56 | 2022-09-22 11:06:43 -0700 | [diff] [blame] | 1543 | Ok(Some(Rc::new(( |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 1544 | RsSnippet { features, tokens: api_func }, |
| 1545 | thunk.into(), |
| 1546 | Rc::new(function_id), |
Devin Jeanpierre | ce54920 | 2022-07-25 08:54:34 -0700 | [diff] [blame] | 1547 | )))) |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1548 | } |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 1549 | |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1550 | /// The function signature for a function's bindings. |
| 1551 | struct BindingsSignature { |
| 1552 | /// The lifetime parameters for the Rust function. |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1553 | lifetimes: Vec<Lifetime>, |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1554 | |
| 1555 | /// The parameter list for the Rust function. |
| 1556 | /// |
| 1557 | /// For example, `vec![quote!{self}, quote!{x: &i32}]`. |
| 1558 | params: Vec<TokenStream>, |
| 1559 | |
| 1560 | /// The return type fragment of the Rust function, as a token stream. |
| 1561 | /// |
| 1562 | /// This is the same as the actual return type, except that () is the empty |
| 1563 | /// tokens, non-Unpin by-value types are `impl Ctor<Output=#return_type> + |
| 1564 | /// ...`, and wherever the type is the type of `Self`, it gets replaced by |
| 1565 | /// literal `Self`. |
| 1566 | return_type_fragment: TokenStream, |
| 1567 | |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 1568 | /// Any preparation code to define the arguments in `thunk_args`. |
| 1569 | thunk_prepare: TokenStream, |
| 1570 | |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1571 | /// The arguments passed to the thunk, expressed in terms of `params`. |
| 1572 | thunk_args: Vec<TokenStream>, |
| 1573 | } |
| 1574 | |
| 1575 | /// Reformats API parameters and return values to match Rust conventions and the |
| 1576 | /// trait requirements. |
| 1577 | /// |
| 1578 | /// For example: |
| 1579 | /// |
| 1580 | /// * Use the `self` keyword for the this pointer. |
| 1581 | /// * Use `Self` for the return value of constructor traits. |
| 1582 | /// * For C++ constructors, remove `self` from the Rust side (as it becomes the |
| 1583 | /// return value), retaining it on the C++ side / thunk args. |
| 1584 | /// * serialize a `()` as the empty string. |
| 1585 | fn function_signature( |
| 1586 | features: &mut BTreeSet<Ident>, |
| 1587 | func: &Func, |
| 1588 | impl_kind: &ImplKind, |
| 1589 | param_idents: &[Ident], |
| 1590 | param_types: &mut Vec<RsTypeKind>, |
| 1591 | return_type: &mut RsTypeKind, |
| 1592 | ) -> Result<BindingsSignature> { |
| 1593 | let mut api_params = Vec::with_capacity(func.params.len()); |
| 1594 | let mut thunk_args = Vec::with_capacity(func.params.len()); |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 1595 | let mut thunk_prepare = quote! {}; |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1596 | for (i, (ident, type_)) in param_idents.iter().zip(param_types.iter()).enumerate() { |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 1597 | type_.check_by_value()?; |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1598 | if !type_.is_unpin() { |
| 1599 | // `impl Ctor` will fail to compile in a trait. |
| 1600 | // This will only be hit if there was a bug in api_func_shape. |
| 1601 | if let ImplKind::Trait { .. } = &impl_kind { |
| 1602 | panic!( |
| 1603 | "non-Unpin types cannot work by value in traits; this should have instead \ |
| 1604 | become an rvalue reference to force the caller to materialize the Ctor." |
| 1605 | ); |
| 1606 | } |
| 1607 | // The generated bindings require a move constructor. |
| 1608 | if !type_.is_move_constructible() { |
| 1609 | bail!("Non-movable, non-trivial_abi type '{type}' is not supported by value as parameter #{i}", type=quote!{#type_}); |
| 1610 | } |
| 1611 | api_params.push(quote! {#ident: impl ::ctor::Ctor<Output=#type_>}); |
| 1612 | thunk_args |
| 1613 | .push(quote! {::std::pin::Pin::into_inner_unchecked(::ctor::emplace!(#ident))}); |
| 1614 | } else { |
| 1615 | api_params.push(quote! {#ident: #type_}); |
| 1616 | thunk_args.push(quote! {#ident}); |
| 1617 | } |
| 1618 | } |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 1619 | |
| 1620 | let mut lifetimes: Vec<Lifetime> = unique_lifetimes(&*param_types).collect(); |
| 1621 | |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1622 | let mut quoted_return_type = None; |
| 1623 | if let ImplKind::Trait { |
| 1624 | trait_name: trait_name @ (TraitName::UnpinConstructor { .. } | TraitName::CtorNew(..)), |
| 1625 | .. |
| 1626 | } = &impl_kind |
| 1627 | { |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1628 | // For constructors, we move the output parameter to be the return value. |
| 1629 | // The return value is "really" void. |
| 1630 | ensure!( |
| 1631 | func.return_type.rs_type.is_unit_type(), |
| 1632 | "Unexpectedly non-void return type of a constructor" |
| 1633 | ); |
| 1634 | |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1635 | // Presence of element #0 is indirectly verified by a `Constructor`-related |
| 1636 | // `match` branch a little bit above. |
| 1637 | *return_type = param_types[0] |
| 1638 | .referent() |
| 1639 | .ok_or_else(|| anyhow!("Expected pointer/reference for `__this` parameter"))? |
| 1640 | .clone(); |
| 1641 | quoted_return_type = Some(quote! {Self}); |
| 1642 | |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1643 | // Grab the `__this` lifetime to remove it from the lifetime parameters. |
| 1644 | let this_lifetime = param_types[0] |
| 1645 | .lifetime() |
| 1646 | .ok_or_else(|| anyhow!("Missing lifetime for `__this` parameter"))?; |
| 1647 | |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1648 | // Drop `__this` parameter from the public Rust API. |
| 1649 | api_params.remove(0); |
| 1650 | thunk_args.remove(0); |
| 1651 | param_types.remove(0); |
| 1652 | |
| 1653 | // Remove the lifetime associated with `__this`. |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1654 | lifetimes.retain(|l| l != &this_lifetime); |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1655 | if let Some(type_still_dependent_on_removed_lifetime) = param_types |
| 1656 | .iter() |
| 1657 | .flat_map(|t| t.lifetimes()) |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1658 | .find(|lifetime| lifetime == &this_lifetime) |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1659 | { |
| 1660 | bail!( |
| 1661 | "The lifetime of `__this` is unexpectedly also used by another \ |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 1662 | parameter: {type_still_dependent_on_removed_lifetime:?}", |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1663 | ); |
| 1664 | } |
| 1665 | |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 1666 | // CtorNew groups parameters into a tuple. |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1667 | if let TraitName::CtorNew(args_type) = trait_name { |
| 1668 | let args_type = format_tuple_except_singleton(args_type); |
| 1669 | api_params = vec![quote! {args: #args_type}]; |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 1670 | let thunk_vars = format_tuple_except_singleton(&thunk_args); |
| 1671 | thunk_prepare.extend(quote! {let #thunk_vars = args;}); |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | let return_type_fragment = if return_type == &RsTypeKind::Unit { |
| 1676 | quote! {} |
| 1677 | } else { |
| 1678 | let ty = quoted_return_type.unwrap_or_else(|| quote! {#return_type}); |
| 1679 | if return_type.is_unpin() { |
| 1680 | quote! {#ty} |
| 1681 | } else { |
| 1682 | // This feature seems destined for stabilization, and makes the code |
| 1683 | // simpler. We don't need it for simple functions, but if the return type is |
| 1684 | // used as an associated type for a trait. |
| 1685 | features.insert(make_rs_ident("type_alias_impl_trait")); |
| 1686 | // The returned lazy FnCtor depends on all inputs. |
| 1687 | let extra_lifetimes = lifetimes.iter().map(|a| quote! {+ ::ctor::Captures<#a>}); |
| 1688 | quote! {impl ::ctor::Ctor<Output=#ty> #(#extra_lifetimes)* } |
| 1689 | } |
| 1690 | }; |
| 1691 | |
| 1692 | // Change `__this: &'a SomeStruct` into `&'a self` if needed. |
| 1693 | if impl_kind.format_first_param_as_self() { |
| 1694 | let first_api_param = param_types |
| 1695 | .get(0) |
| 1696 | .ok_or_else(|| anyhow!("No parameter to format as 'self': {:?}", func))?; |
| 1697 | // If param_types[0] exists, so do api_params[0] and thunk_args[0]. |
| 1698 | match impl_kind { |
| 1699 | ImplKind::None { .. } => unreachable!(), |
| 1700 | ImplKind::Struct { .. } | ImplKind::Trait { impl_for: ImplFor::T, .. } => { |
| 1701 | // In the ImplFor::T reference style (which is implied for ImplKind::Struct) the |
| 1702 | // impl block is for `T`. The `self` parameter has a type determined by the |
| 1703 | // first parameter (typically a reference of some kind) and can be passed to a |
| 1704 | // thunk via the expression `self`. |
| 1705 | api_params[0] = first_api_param.format_as_self_param()?; |
| 1706 | thunk_args[0] = quote! { self }; |
| 1707 | } |
| 1708 | ImplKind::Trait { impl_for: ImplFor::RefT, .. } => { |
| 1709 | // In the ImplFor::RefT reference style the impl block is for a reference type |
| 1710 | // referring to T (`&T`, `&mut T`, or `Pin<&mut T>` so a bare `self` parameter |
| 1711 | // has that type and can be passed to a thunk via the expression `self`. |
| 1712 | api_params[0] = quote! { self }; |
| 1713 | thunk_args[0] = quote! { self }; |
| 1714 | } |
| 1715 | } |
| 1716 | } |
| 1717 | |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 1718 | Ok(BindingsSignature { |
| 1719 | lifetimes, |
| 1720 | params: api_params, |
| 1721 | return_type_fragment, |
| 1722 | thunk_prepare, |
| 1723 | thunk_args, |
| 1724 | }) |
Devin Jeanpierre | f5013fb | 2022-08-01 13:56:27 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1727 | fn generate_func_thunk( |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1728 | db: &dyn BindingsGenerator, |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1729 | func: &Func, |
| 1730 | param_idents: &[Ident], |
Devin Jeanpierre | 22f9149 | 2022-04-06 13:01:23 -0700 | [diff] [blame] | 1731 | param_types: &[RsTypeKind], |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1732 | return_type: &RsTypeKind, |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1733 | ) -> Result<TokenStream> { |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1734 | let thunk_attr = if can_skip_cc_thunk(db, func) { |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1735 | let mangled_name = &func.mangled_name; |
| 1736 | quote! {#[link_name = #mangled_name]} |
| 1737 | } else { |
| 1738 | quote! {} |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 1739 | }; |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 1740 | let lifetimes: Vec<_> = unique_lifetimes(param_types).collect(); |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 1741 | |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1742 | // The first parameter is the output parameter, if any. |
Devin Jeanpierre | 22f9149 | 2022-04-06 13:01:23 -0700 | [diff] [blame] | 1743 | let mut param_types = param_types.into_iter(); |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1744 | let mut param_idents = param_idents.into_iter(); |
| 1745 | let mut out_param = None; |
| 1746 | let mut out_param_ident = None; |
| 1747 | let mut return_type_fragment = return_type.format_as_return_type_fragment(); |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1748 | if func.name == UnqualifiedIdentifier::Constructor { |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1749 | // For constructors, inject MaybeUninit into the type of `__this_` parameter. |
Devin Jeanpierre | 22f9149 | 2022-04-06 13:01:23 -0700 | [diff] [blame] | 1750 | let first_param = param_types |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1751 | .next() |
| 1752 | .ok_or_else(|| anyhow!("Constructors should have at least one parameter (__this)"))?; |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1753 | out_param = Some(first_param.format_mut_ref_as_uninitialized().with_context(|| { |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1754 | format!( |
| 1755 | "Failed to format `__this` param for a constructor thunk: {:?}", |
| 1756 | func.params.get(0) |
| 1757 | ) |
| 1758 | })?); |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1759 | out_param_ident = Some(param_idents.next().unwrap().clone()); |
| 1760 | } else if !return_type.is_unpin() { |
| 1761 | // For nontrivial return types, create a new out parameter. |
| 1762 | // The lifetime doesn't matter, so we can insert a new anonymous lifetime here. |
| 1763 | out_param = Some(quote! { |
| 1764 | &mut ::std::mem::MaybeUninit< #return_type > |
| 1765 | }); |
| 1766 | out_param_ident = Some(make_rs_ident("__return")); |
| 1767 | return_type_fragment = quote! {}; |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 1770 | let thunk_ident = thunk_ident(&func); |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 1771 | |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1772 | let generic_params = format_generic_params(&lifetimes, std::iter::empty::<syn::Ident>()); |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 1773 | let param_idents = out_param_ident.as_ref().into_iter().chain(param_idents); |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 1774 | let param_types = out_param.into_iter().chain(param_types.map(|t| { |
| 1775 | if !t.is_unpin() { |
| 1776 | quote! {&mut #t} |
| 1777 | } else { |
| 1778 | quote! {#t} |
| 1779 | } |
| 1780 | })); |
Devin Jeanpierre | 3eb5bbd | 2022-04-06 12:56:19 -0700 | [diff] [blame] | 1781 | |
| 1782 | Ok(quote! { |
| 1783 | #thunk_attr |
| 1784 | pub(crate) fn #thunk_ident #generic_params( #( #param_idents: #param_types ),* |
| 1785 | ) #return_type_fragment ; |
| 1786 | }) |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 1787 | } |
| 1788 | |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 1789 | fn generate_doc_comment(comment: &Option<String>) -> TokenStream { |
| 1790 | match comment { |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 1791 | Some(text) => { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 1792 | // token_stream_printer (and rustfmt) don't put a space between /// and the doc |
| 1793 | // comment, let's add it here so our comments are pretty. |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 1794 | let doc = format!(" {}", text.replace('\n', "\n ")); |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 1795 | quote! {#[doc=#doc]} |
| 1796 | } |
| 1797 | None => quote! {}, |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 1798 | } |
| 1799 | } |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 1800 | |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1801 | fn format_generic_params<'a, T: ToTokens>( |
| 1802 | lifetimes: impl IntoIterator<Item = &'a Lifetime>, |
| 1803 | types: impl IntoIterator<Item = T>, |
| 1804 | ) -> TokenStream { |
| 1805 | let mut lifetimes = lifetimes.into_iter().filter(|lifetime| &*lifetime.0 != "_").peekable(); |
| 1806 | let mut types = types.into_iter().peekable(); |
| 1807 | if lifetimes.peek().is_none() && types.peek().is_none() { |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 1808 | quote! {} |
| 1809 | } else { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 1810 | quote! { < #( #lifetimes ),* #( #types ),*> } |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 1811 | } |
| 1812 | } |
| 1813 | |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 1814 | /// Formats singletons as themselves, and collections of n!=1 items as a tuple. |
| 1815 | /// |
| 1816 | /// In other words, this formats a collection of things as if via `#(#items),*`, |
| 1817 | /// but without lint warnings. |
| 1818 | /// |
| 1819 | /// For example: |
| 1820 | /// |
| 1821 | /// * [] => () |
| 1822 | /// * [x] => x // equivalent to (x), but lint-free. |
| 1823 | /// * [x, y] => (x, y) |
| 1824 | fn format_tuple_except_singleton<T: ToTokens>(items: &[T]) -> TokenStream { |
| 1825 | match items { |
| 1826 | [singleton] => quote! {#singleton}, |
| 1827 | items => quote! {(#(#items),*)}, |
| 1828 | } |
| 1829 | } |
| 1830 | |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1831 | fn should_implement_drop(record: &Record) -> bool { |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 1832 | match record.destructor { |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1833 | // TODO(b/202258760): Only omit destructor if `Copy` is specified. |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 1834 | SpecialMemberFunc::Trivial => false, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1835 | |
| 1836 | // TODO(b/212690698): Avoid calling into the C++ destructor (e.g. let |
| 1837 | // Rust drive `drop`-ing) to avoid (somewhat unergonomic) ManuallyDrop |
| 1838 | // if we can ask Rust to preserve C++ field destruction order in |
| 1839 | // NontrivialMembers case. |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 1840 | SpecialMemberFunc::NontrivialMembers => true, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1841 | |
| 1842 | // The `impl Drop` for NontrivialUserDefined needs to call into the |
| 1843 | // user-defined destructor on C++ side. |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 1844 | SpecialMemberFunc::NontrivialUserDefined => true, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1845 | |
| 1846 | // TODO(b/213516512): Today the IR doesn't contain Func entries for |
| 1847 | // deleted functions/destructors/etc. But, maybe we should generate |
| 1848 | // `impl Drop` in this case? With `unreachable!`? With |
| 1849 | // `std::mem::forget`? |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 1850 | SpecialMemberFunc::Unavailable => false, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | /// Returns whether fields of type `ty` need to be wrapped in `ManuallyDrop<T>` |
| 1855 | /// to prevent the fields from being destructed twice (once by the C++ |
| 1856 | /// destructor calkled from the `impl Drop` of the struct and once by `drop` on |
| 1857 | /// the Rust side). |
| 1858 | /// |
| 1859 | /// A type is safe to destroy twice if it implements `Copy`. Fields of such |
| 1860 | /// don't need to be wrapped in `ManuallyDrop<T>` even if the struct |
| 1861 | /// containing the fields provides an `impl Drop` that calles into a C++ |
| 1862 | /// destructor (in addition to dropping the fields on the Rust side). |
| 1863 | /// |
| 1864 | /// Note that it is not enough to just be `!needs_drop<T>()`: Rust only |
| 1865 | /// guarantees that it is safe to use-after-destroy for `Copy` types. See |
| 1866 | /// e.g. the documentation for |
| 1867 | /// [`drop_in_place`](https://doc.rust-lang.org/std/ptr/fn.drop_in_place.html): |
| 1868 | /// |
| 1869 | /// > if `T` is not `Copy`, using the pointed-to value after calling |
| 1870 | /// > `drop_in_place` can cause undefined behavior |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 1871 | /// |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 1872 | /// For non-Copy union fields, failing to use `ManuallyDrop<T>` would |
| 1873 | /// additionally cause a compile-time error until https://github.com/rust-lang/rust/issues/55149 is stabilized. |
Devin Jeanpierre | 45b0196 | 2022-07-07 06:12:11 -0700 | [diff] [blame] | 1874 | fn needs_manually_drop(db: &Database, ty: ir::RsType) -> Result<bool> { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 1875 | let ty_implements_copy = db.rs_type_kind(ty)?.implements_copy(); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 1876 | Ok(!ty_implements_copy) |
| 1877 | } |
| 1878 | |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1879 | #[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)] |
| 1880 | struct NamespaceQualifier(Vec<String>); |
| 1881 | |
| 1882 | impl NamespaceQualifier { |
| 1883 | fn new(item_id: ItemId, ir: &IR) -> Result<Self> { |
| 1884 | let mut namespaces = vec![]; |
| 1885 | let item: &Item = ir.find_decl(item_id)?; |
| 1886 | let mut enclosing_namespace_id = item.enclosing_namespace_id(); |
| 1887 | while let Some(parent_id) = enclosing_namespace_id { |
| 1888 | let namespace_item = ir.find_decl(parent_id)?; |
| 1889 | match namespace_item { |
| 1890 | Item::Namespace(ns) => { |
| 1891 | namespaces.push(ns.name.identifier.clone()); |
| 1892 | enclosing_namespace_id = ns.enclosing_namespace_id; |
| 1893 | } |
| 1894 | _ => { |
| 1895 | bail!("Expected namespace"); |
| 1896 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 1897 | } |
| 1898 | } |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1899 | Ok(Self(namespaces.into_iter().rev().collect_vec())) |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 1900 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 1901 | |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1902 | fn format_for_rs(&self) -> TokenStream { |
| 1903 | let namespace_rs_idents = self.0.iter().map(|ns| make_rs_ident(ns)); |
| 1904 | quote! { #(#namespace_rs_idents::)* } |
| 1905 | } |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 1906 | |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1907 | fn format_for_cc(&self) -> TokenStream { |
| 1908 | let namespace_cc_idents = self.0.iter().map(|ns| format_cc_ident(ns)); |
| 1909 | quote! { #(#namespace_cc_idents::)* } |
| 1910 | } |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 1911 | } |
| 1912 | |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 1913 | /// Generates Rust source code for a given incomplete record declaration. |
| 1914 | fn generate_incomplete_record(incomplete_record: &IncompleteRecord) -> Result<TokenStream> { |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 1915 | let ident = make_rs_ident(&incomplete_record.rs_name); |
| 1916 | let name = &incomplete_record.rs_name; |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 1917 | Ok(quote! { |
| 1918 | forward_declare::forward_declare!( |
| 1919 | pub #ident __SPACE__ = __SPACE__ forward_declare::symbol!(#name) |
| 1920 | ); |
| 1921 | }) |
| 1922 | } |
| 1923 | |
Lukasz Anforowicz | 0dbcf0e | 2022-05-17 06:46:24 -0700 | [diff] [blame] | 1924 | fn make_rs_field_ident(field: &Field, field_index: usize) -> Ident { |
| 1925 | match field.identifier.as_ref() { |
| 1926 | None => make_rs_ident(&format!("__unnamed_field{}", field_index)), |
| 1927 | Some(Identifier { identifier }) => make_rs_ident(identifier), |
| 1928 | } |
| 1929 | } |
| 1930 | |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 1931 | /// Gets the type of `field` for layout purposes. |
| 1932 | /// |
| 1933 | /// Note that `get_field_rs_type_for_layout` may return Err (for |
| 1934 | /// `is_no_unique_address` fields) even if `field.type_` is Ok. |
| 1935 | fn get_field_rs_type_for_layout(field: &Field) -> Result<&RsType, &str> { |
| 1936 | // [[no_unique_address]] fields are replaced by a type-less, unaligned block of |
| 1937 | // memory which fills space up to the next field. |
Lukasz Anforowicz | 5765bb8 | 2022-05-17 17:21:06 -0700 | [diff] [blame] | 1938 | // See: docs/struct_layout |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 1939 | if field.is_no_unique_address { |
| 1940 | return Err("`[[no_unique_address]]` attribute was present."); |
| 1941 | } |
| 1942 | |
| 1943 | field.type_.as_ref().map(|t| &t.rs_type).map_err(String::as_str) |
Lukasz Anforowicz | 5765bb8 | 2022-05-17 17:21:06 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 1946 | /// Returns the type of a type-less, unaligned block of memory that can hold a |
| 1947 | /// specified number of bits, rounded up to the next multiple of 8. |
| 1948 | fn bit_padding(padding_size_in_bits: usize) -> TokenStream { |
| 1949 | let padding_size = Literal::usize_unsuffixed((padding_size_in_bits + 7) / 8); |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 1950 | quote! { [::std::mem::MaybeUninit<u8>; #padding_size] } |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 1951 | } |
| 1952 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 1953 | /// Generates Rust source code for a given `Record` and associated assertions as |
| 1954 | /// a tuple. |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 1955 | fn generate_record( |
| 1956 | db: &Database, |
| 1957 | record: &Rc<Record>, |
| 1958 | errors: &mut dyn ErrorReporting, |
| 1959 | ) -> Result<GeneratedItem> { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 1960 | let ir = db.ir(); |
Lukasz Anforowicz | 4c3a2cc | 2022-03-11 00:24:49 +0000 | [diff] [blame] | 1961 | let ident = make_rs_ident(&record.rs_name); |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 1962 | let namespace_qualifier = NamespaceQualifier::new(record.id, &ir)?.format_for_rs(); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 1963 | let qualified_ident = { |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 1964 | quote! { crate:: #namespace_qualifier #ident } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 1965 | }; |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 1966 | let doc_comment = generate_doc_comment(&record.doc_comment); |
Lukasz Anforowicz | fed64e6 | 2022-03-22 22:39:04 +0000 | [diff] [blame] | 1967 | |
| 1968 | let mut field_copy_trait_assertions: Vec<TokenStream> = vec![]; |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 1969 | |
| 1970 | let fields_with_bounds = (record.fields.iter()) |
| 1971 | .map(|field| { |
| 1972 | ( |
| 1973 | // We don't represent bitfields directly in Rust. We drop the field itself here |
| 1974 | // and only retain the offset information. Adjacent bitfields then get merged in |
| 1975 | // the next step. |
| 1976 | if field.is_bitfield { None } else { Some(field) }, |
| 1977 | field.offset, |
| 1978 | // We retain the end offset of fields only if we have a matching Rust type |
| 1979 | // to represent them. Otherwise we'll fill up all the space to the next field. |
| 1980 | // See: docs/struct_layout |
Marcel Hlopko | f05621b | 2022-05-25 00:26:06 -0700 | [diff] [blame] | 1981 | match get_field_rs_type_for_layout(field) { |
| 1982 | // Regular field |
| 1983 | Ok(_rs_type) => Some(field.offset + field.size), |
| 1984 | // Opaque field |
Michael VanBemmel | 106f66c | 2022-07-13 09:48:48 -0700 | [diff] [blame] | 1985 | Err(_error) => { |
| 1986 | if record.is_union() { |
| 1987 | Some(field.size) |
| 1988 | } else { |
| 1989 | None |
| 1990 | } |
| 1991 | } |
Marcel Hlopko | f05621b | 2022-05-25 00:26:06 -0700 | [diff] [blame] | 1992 | }, |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 1993 | vec![format!( |
| 1994 | "{} : {} bits", |
| 1995 | field.identifier.as_ref().map(|i| i.identifier.clone()).unwrap_or("".into()), |
| 1996 | field.size |
| 1997 | )], |
| 1998 | ) |
| 1999 | }) |
| 2000 | // Merge consecutive bitfields. This is necessary, because they may share storage in the |
| 2001 | // same byte. |
| 2002 | .coalesce(|first, second| match (first, second) { |
| 2003 | ((None, offset, _, desc1), (None, _, end, desc2)) => { |
| 2004 | Ok((None, offset, end, [desc1, desc2].concat())) |
| 2005 | } |
| 2006 | pair => Err(pair), |
| 2007 | }); |
| 2008 | |
| 2009 | // Pair up fields with the preceeding and following fields (if any): |
| 2010 | // - the end offset of the previous field determines if we need to insert |
| 2011 | // padding. |
| 2012 | // - the start offset of the next field may be need to grow the current field to |
| 2013 | // there. |
| 2014 | // This uses two separate `map` invocations on purpose to limit available state. |
| 2015 | let field_definitions = iter::once(None) |
| 2016 | .chain(fields_with_bounds.clone().map(Some)) |
| 2017 | .chain(iter::once(None)) |
| 2018 | .tuple_windows() |
| 2019 | .map(|(prev, cur, next)| { |
| 2020 | let (field, offset, end, desc) = cur.unwrap(); |
| 2021 | let prev_end = prev.as_ref().map(|(_, _, e, _)| *e).flatten().unwrap_or(offset); |
| 2022 | let next_offset = next.map(|(_, o, _, _)| o); |
Kinuko Yasuda | 54b75d7 | 2022-08-18 10:09:54 -0700 | [diff] [blame] | 2023 | let end = end.or(next_offset).unwrap_or(record.size * 8); |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2024 | |
| 2025 | if let Some((Some(prev_field), _, Some(prev_end), _)) = prev { |
| 2026 | assert!( |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 2027 | record.is_union() || prev_end <= offset, |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2028 | "Unexpected offset+size for field {:?} in record {}", |
| 2029 | prev_field, |
| 2030 | record.cc_name |
| 2031 | ); |
| 2032 | } |
| 2033 | |
| 2034 | (field, prev_end, offset, end, desc) |
| 2035 | }) |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 2036 | .enumerate() |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2037 | .map(|(field_index, (field, prev_end, offset, end, desc))| { |
| 2038 | // `is_opaque_blob` and bitfield representations are always |
| 2039 | // unaligned, even though the actual C++ field might be aligned. |
| 2040 | // To put the current field at the right offset, we might need to |
| 2041 | // insert some extra padding. |
| 2042 | // |
| 2043 | // No padding should be needed if the type of the current field is |
| 2044 | // known (i.e. if the current field is correctly aligned based on |
| 2045 | // its original type). |
| 2046 | // |
| 2047 | // We also don't need padding if we're in a union. |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 2048 | let padding_size_in_bits = if record.is_union() |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2049 | || (field.is_some() && get_field_rs_type_for_layout(field.unwrap()).is_ok()) |
| 2050 | { |
| 2051 | 0 |
| 2052 | } else { |
| 2053 | let padding_start = (prev_end + 7) / 8 * 8; // round up to byte boundary |
| 2054 | offset - padding_start |
| 2055 | }; |
| 2056 | |
| 2057 | let padding = if padding_size_in_bits == 0 { |
| 2058 | quote! {} |
| 2059 | } else { |
| 2060 | let padding_name = make_rs_ident(&format!("__padding{}", field_index)); |
| 2061 | let padding_type = bit_padding(padding_size_in_bits); |
| 2062 | quote! { #padding_name: #padding_type, } |
| 2063 | }; |
| 2064 | |
| 2065 | // Bitfields get represented by private padding to ensure overall |
| 2066 | // struct layout is compatible. |
| 2067 | if field.is_none() { |
| 2068 | let name = make_rs_ident(&format!("__bitfields{}", field_index)); |
| 2069 | let bitfield_padding = bit_padding(end - offset); |
| 2070 | return Ok(quote! { |
| 2071 | __NEWLINE__ #( __COMMENT__ #desc )* |
| 2072 | #padding #name: #bitfield_padding |
| 2073 | }); |
| 2074 | } |
| 2075 | let field = field.unwrap(); |
| 2076 | |
Lukasz Anforowicz | 0dbcf0e | 2022-05-17 06:46:24 -0700 | [diff] [blame] | 2077 | let ident = make_rs_field_ident(field, field_index); |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 2078 | let doc_comment = match field.type_.as_ref() { |
| 2079 | Ok(_) => generate_doc_comment(&field.doc_comment), |
| 2080 | Err(msg) => { |
| 2081 | let supplemental_text = |
| 2082 | format!("Reason for representing this field as a blob of bytes:\n{}", msg); |
| 2083 | let new_text = match field.doc_comment.as_ref() { |
| 2084 | None => supplemental_text, |
| 2085 | Some(old_text) => format!("{}\n\n{}", old_text, supplemental_text), |
| 2086 | }; |
| 2087 | generate_doc_comment(&Some(new_text)) |
| 2088 | } |
| 2089 | }; |
| 2090 | let access = if field.access == AccessSpecifier::Public |
| 2091 | && get_field_rs_type_for_layout(field).is_ok() |
| 2092 | { |
Lukasz Anforowicz | 0dbcf0e | 2022-05-17 06:46:24 -0700 | [diff] [blame] | 2093 | quote! { pub } |
| 2094 | } else { |
Rosica Dejanovska | 6676ad8 | 2022-06-07 14:28:59 -0700 | [diff] [blame] | 2095 | quote! { pub(crate) } |
Lukasz Anforowicz | 0dbcf0e | 2022-05-17 06:46:24 -0700 | [diff] [blame] | 2096 | }; |
| 2097 | |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 2098 | let field_type = match get_field_rs_type_for_layout(field) { |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2099 | Err(_) => bit_padding(end - field.offset), |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 2100 | Ok(rs_type) => { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2101 | let type_kind = db.rs_type_kind(rs_type.clone()).with_context(|| { |
Lukasz Anforowicz | 0dbcf0e | 2022-05-17 06:46:24 -0700 | [diff] [blame] | 2102 | format!( |
| 2103 | "Failed to format type for field {:?} on record {:?}", |
| 2104 | field, record |
| 2105 | ) |
| 2106 | })?; |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2107 | let mut formatted = quote! {#type_kind}; |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 2108 | if should_implement_drop(record) || record.is_union() { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2109 | if needs_manually_drop(db, rs_type.clone())? { |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 2110 | // TODO(b/212690698): Avoid (somewhat unergonomic) ManuallyDrop |
| 2111 | // if we can ask Rust to preserve field destruction order if the |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 2112 | // destructor is the SpecialMemberFunc::NontrivialMembers |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 2113 | // case. |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 2114 | formatted = quote! { ::std::mem::ManuallyDrop<#formatted> } |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 2115 | } else { |
| 2116 | field_copy_trait_assertions.push(quote! { |
| 2117 | const _: () = { |
| 2118 | static_assertions::assert_impl_all!(#formatted: Copy); |
| 2119 | }; |
| 2120 | }); |
| 2121 | } |
| 2122 | }; |
| 2123 | formatted |
| 2124 | } |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 2125 | }; |
Lukasz Anforowicz | 0dbcf0e | 2022-05-17 06:46:24 -0700 | [diff] [blame] | 2126 | |
Lukasz Anforowicz | 5765bb8 | 2022-05-17 17:21:06 -0700 | [diff] [blame] | 2127 | Ok(quote! { #padding #doc_comment #access #ident: #field_type }) |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 2128 | }) |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 2129 | .collect::<Result<Vec<_>>>()?; |
Lukasz Anforowicz | fed64e6 | 2022-03-22 22:39:04 +0000 | [diff] [blame] | 2130 | |
Kinuko Yasuda | 54b75d7 | 2022-08-18 10:09:54 -0700 | [diff] [blame] | 2131 | let size = Literal::usize_unsuffixed(record.size); |
Lukasz Anforowicz | dc37d6d | 2022-05-17 08:20:13 -0700 | [diff] [blame] | 2132 | let alignment = Literal::usize_unsuffixed(record.alignment); |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 2133 | let field_offset_assertions = if record.is_union() { |
Marcel Hlopko | fa9a395 | 2022-05-10 01:34:13 -0700 | [diff] [blame] | 2134 | // TODO(https://github.com/Gilnaa/memoffset/issues/66): generate assertions for unions once |
| 2135 | // offsetof supports them. |
| 2136 | vec![] |
| 2137 | } else { |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2138 | fields_with_bounds |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 2139 | .enumerate() |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2140 | .map(|(field_index, (field, _, _, _))| { |
| 2141 | if let Some(field) = field { |
| 2142 | let field_ident = make_rs_field_ident(field, field_index); |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 2143 | |
| 2144 | // The assertion below reinforces that the division by 8 on the next line is |
| 2145 | // justified (because the bitfields have been coallesced / filtered out |
| 2146 | // earlier). |
| 2147 | assert_eq!(field.offset % 8, 0); |
| 2148 | let expected_offset = Literal::usize_unsuffixed(field.offset / 8); |
| 2149 | |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2150 | let actual_offset_expr = quote! { |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 2151 | memoffset::offset_of!(#qualified_ident, #field_ident) |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 2152 | }; |
| 2153 | quote! { |
| 2154 | const _: () = assert!(#actual_offset_expr == #expected_offset); |
| 2155 | } |
| 2156 | } else { |
| 2157 | quote! {} |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 2158 | } |
| 2159 | }) |
| 2160 | .collect_vec() |
Marcel Hlopko | fa9a395 | 2022-05-10 01:34:13 -0700 | [diff] [blame] | 2161 | }; |
Lukasz Anforowicz | b4d1778 | 2022-07-07 08:09:13 -0700 | [diff] [blame] | 2162 | // TODO(b/212696226): Generate `assert_impl_all!` or `assert_not_impl_any!` |
Lukasz Anforowicz | fed64e6 | 2022-03-22 22:39:04 +0000 | [diff] [blame] | 2163 | // assertions about the `Copy` trait - this trait should be implemented |
| 2164 | // iff `should_implement_drop(record)` is false. |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 2165 | let mut features = BTreeSet::new(); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2166 | |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2167 | let derives = generate_derives(record); |
Devin Jeanpierre | 9227d2c | 2021-10-06 12:26:05 +0000 | [diff] [blame] | 2168 | let derives = if derives.is_empty() { |
| 2169 | quote! {} |
| 2170 | } else { |
| 2171 | quote! {#[derive( #(#derives),* )]} |
| 2172 | }; |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 2173 | let record_kind = if record.is_union() { |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 2174 | quote! { union } |
| 2175 | } else { |
| 2176 | quote! { struct } |
| 2177 | }; |
Devin Jeanpierre | 190b90a | 2022-05-24 06:00:34 -0700 | [diff] [blame] | 2178 | |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 2179 | let recursively_pinned_attribute = if record.is_unpin() { |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 2180 | quote! {} |
Devin Jeanpierre | ea700d3 | 2021-10-06 11:33:56 +0000 | [diff] [blame] | 2181 | } else { |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 2182 | // negative_impls are necessary for universal initialization due to Rust's |
| 2183 | // coherence rules: PhantomPinned isn't enough to prove to Rust that a |
| 2184 | // blanket impl that requires Unpin doesn't apply. See http://<internal link>=h.f6jp8ifzgt3n |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 2185 | features.insert(make_rs_ident("negative_impls")); |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 2186 | if should_implement_drop(record) { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 2187 | quote! {#[::ctor::recursively_pinned(PinnedDrop)]} |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 2188 | } else { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 2189 | quote! {#[::ctor::recursively_pinned]} |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 2190 | } |
| 2191 | }; |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 2192 | |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2193 | let mut repr_attributes = vec![quote! {C}]; |
| 2194 | if record.override_alignment && record.alignment > 1 { |
| 2195 | let alignment = Literal::usize_unsuffixed(record.alignment); |
| 2196 | repr_attributes.push(quote! {align(#alignment)}); |
| 2197 | } |
| 2198 | |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 2199 | // Adjust the struct to also include base class subobjects, vtables, etc. |
| 2200 | let head_padding = if let Some(first_field) = record.fields.first() { |
| 2201 | first_field.offset / 8 |
| 2202 | } else { |
Kinuko Yasuda | 54b75d7 | 2022-08-18 10:09:54 -0700 | [diff] [blame] | 2203 | record.size |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 2204 | }; |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 2205 | // Prevent direct initialization for non-aggregate structs. |
| 2206 | // |
| 2207 | // Technically, any implicit-lifetime type is going to be fine to initialize |
| 2208 | // using direct initialization of the fields, even if it is not an aggregate, |
| 2209 | // because this is "just" setting memory to the appropriate values, and |
| 2210 | // implicit-lifetime types can automatically begin their lifetime without |
| 2211 | // running a constructor at all. |
| 2212 | // |
| 2213 | // However, not all types used in interop are implicit-lifetime. For example, |
| 2214 | // while any `Unpin` C++ value is, some `!Unpin` structs (e.g. `std::list`) |
| 2215 | // will not be. So for consistency, we apply the same rule for both |
| 2216 | // implicit-lifetime and non-implicit-lifetime types: the C++ rule, that the |
| 2217 | // type must be an *aggregate* type. |
| 2218 | // |
| 2219 | // TODO(b/232969667): Protect unions from direct initialization, too. |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 2220 | let allow_direct_init = record.is_aggregate || record.is_union(); |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 2221 | let head_padding = if head_padding > 0 || !allow_direct_init { |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 2222 | let n = proc_macro2::Literal::usize_unsuffixed(head_padding); |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2223 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 2224 | __non_field_data: [::std::mem::MaybeUninit<u8>; #n], |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2225 | } |
| 2226 | } else { |
| 2227 | quote! {} |
| 2228 | }; |
| 2229 | |
Devin Jeanpierre | 2745013 | 2022-04-11 13:52:01 -0700 | [diff] [blame] | 2230 | // TODO(b/227442773): After namespace support is added, use the fully-namespaced |
| 2231 | // name. |
| 2232 | let incomplete_symbol = &record.cc_name; |
| 2233 | let incomplete_definition = quote! { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 2234 | forward_declare::unsafe_define!(forward_declare::symbol!(#incomplete_symbol), #qualified_ident); |
Devin Jeanpierre | 2745013 | 2022-04-11 13:52:01 -0700 | [diff] [blame] | 2235 | }; |
| 2236 | |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2237 | let no_unique_address_accessors = cc_struct_no_unique_address_impl(db, record)?; |
Devin Jeanpierre | 8763a8e | 2022-04-26 15:45:13 -0700 | [diff] [blame] | 2238 | let mut record_generated_items = record |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2239 | .child_item_ids |
| 2240 | .iter() |
| 2241 | .map(|id| { |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 2242 | let item = ir.find_decl(*id).with_context(|| { |
| 2243 | format!("Failed to look up `record.child_item_ids` for {:?}", record) |
| 2244 | })?; |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2245 | generate_item(db, item, errors) |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2246 | }) |
| 2247 | .collect::<Result<Vec<_>>>()?; |
| 2248 | |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2249 | record_generated_items.push(cc_struct_upcast_impl(record, &ir)?); |
Devin Jeanpierre | 8763a8e | 2022-04-26 15:45:13 -0700 | [diff] [blame] | 2250 | |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2251 | let mut items = vec![]; |
| 2252 | let mut thunks_from_record_items = vec![]; |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2253 | let mut thunk_impls_from_record_items = vec![]; |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2254 | let mut assertions_from_record_items = vec![]; |
| 2255 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2256 | for generated in record_generated_items { |
| 2257 | items.push(generated.item); |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2258 | if !generated.thunks.is_empty() { |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2259 | thunks_from_record_items.push(generated.thunks); |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2260 | } |
| 2261 | if !generated.assertions.is_empty() { |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2262 | assertions_from_record_items.push(generated.assertions); |
| 2263 | } |
| 2264 | if !generated.thunk_impls.is_empty() { |
| 2265 | thunk_impls_from_record_items.push(generated.thunk_impls); |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2266 | } |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 2267 | features.extend(generated.features.clone()); |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2268 | } |
| 2269 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2270 | let record_tokens = quote! { |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 2271 | #doc_comment |
Devin Jeanpierre | 9227d2c | 2021-10-06 12:26:05 +0000 | [diff] [blame] | 2272 | #derives |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 2273 | #recursively_pinned_attribute |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 2274 | #[repr(#( #repr_attributes ),*)] |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 2275 | pub #record_kind #ident { |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 2276 | #head_padding |
Lukasz Anforowicz | 0dbcf0e | 2022-05-17 06:46:24 -0700 | [diff] [blame] | 2277 | #( #field_definitions, )* |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 2278 | } |
Googler | ec648ff | 2021-09-23 07:19:53 +0000 | [diff] [blame] | 2279 | |
Devin Jeanpierre | 2745013 | 2022-04-11 13:52:01 -0700 | [diff] [blame] | 2280 | #incomplete_definition |
| 2281 | |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 2282 | #no_unique_address_accessors |
| 2283 | |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2284 | __NEWLINE__ __NEWLINE__ |
| 2285 | #( #items __NEWLINE__ __NEWLINE__)* |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 2286 | }; |
| 2287 | |
Lukasz Anforowicz | 95938f8 | 2022-03-24 13:51:54 +0000 | [diff] [blame] | 2288 | let record_trait_assertions = { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2289 | let record_type_name = RsTypeKind::new_record(record.clone(), &ir)?.to_token_stream(); |
Lukasz Anforowicz | 95938f8 | 2022-03-24 13:51:54 +0000 | [diff] [blame] | 2290 | let mut assertions: Vec<TokenStream> = vec![]; |
| 2291 | let mut add_assertion = |assert_impl_macro: TokenStream, trait_name: TokenStream| { |
| 2292 | assertions.push(quote! { |
Lukasz Anforowicz | 768bba3 | 2022-04-11 14:06:13 -0700 | [diff] [blame] | 2293 | const _: () = { static_assertions::#assert_impl_macro (#record_type_name: #trait_name); }; |
Lukasz Anforowicz | 95938f8 | 2022-03-24 13:51:54 +0000 | [diff] [blame] | 2294 | }); |
| 2295 | }; |
| 2296 | if should_derive_clone(record) { |
| 2297 | add_assertion(quote! { assert_impl_all! }, quote! { Clone }); |
| 2298 | } else { |
Lukasz Anforowicz | b4d1778 | 2022-07-07 08:09:13 -0700 | [diff] [blame] | 2299 | // Can't `assert_not_impl_any!` here, because `Clone` may be |
Lukasz Anforowicz | 95938f8 | 2022-03-24 13:51:54 +0000 | [diff] [blame] | 2300 | // implemented rather than derived. |
| 2301 | } |
| 2302 | let mut add_conditional_assertion = |should_impl_trait: bool, trait_name: TokenStream| { |
| 2303 | let assert_impl_macro = if should_impl_trait { |
| 2304 | quote! { assert_impl_all! } |
| 2305 | } else { |
Lukasz Anforowicz | b4d1778 | 2022-07-07 08:09:13 -0700 | [diff] [blame] | 2306 | quote! { assert_not_impl_any! } |
Lukasz Anforowicz | 95938f8 | 2022-03-24 13:51:54 +0000 | [diff] [blame] | 2307 | }; |
| 2308 | add_assertion(assert_impl_macro, trait_name); |
| 2309 | }; |
| 2310 | add_conditional_assertion(should_derive_copy(record), quote! { Copy }); |
| 2311 | add_conditional_assertion(should_implement_drop(record), quote! { Drop }); |
| 2312 | assertions |
| 2313 | }; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2314 | let assertion_tokens = quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 2315 | const _: () = assert!(::std::mem::size_of::<#qualified_ident>() == #size); |
| 2316 | const _: () = assert!(::std::mem::align_of::<#qualified_ident>() == #alignment); |
Lukasz Anforowicz | 95938f8 | 2022-03-24 13:51:54 +0000 | [diff] [blame] | 2317 | #( #record_trait_assertions )* |
Lukasz Anforowicz | fed64e6 | 2022-03-22 22:39:04 +0000 | [diff] [blame] | 2318 | #( #field_offset_assertions )* |
| 2319 | #( #field_copy_trait_assertions )* |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2320 | #( #assertions_from_record_items )* |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2321 | }; |
| 2322 | |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2323 | let thunk_tokens = quote! { |
| 2324 | #( #thunks_from_record_items )* |
| 2325 | }; |
| 2326 | |
| 2327 | Ok(GeneratedItem { |
| 2328 | item: record_tokens, |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 2329 | features, |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2330 | assertions: assertion_tokens, |
| 2331 | thunks: thunk_tokens, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2332 | thunk_impls: quote! {#(#thunk_impls_from_record_items __NEWLINE__ __NEWLINE__)*}, |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2333 | has_record: true, |
| 2334 | }) |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 2337 | fn check_by_value(record: &Record) -> Result<()> { |
| 2338 | if record.destructor == SpecialMemberFunc::Unavailable { |
| 2339 | bail!( |
| 2340 | "Can't directly construct values of type `{}` as it has a non-public or deleted destructor", |
| 2341 | record.cc_name |
| 2342 | ) |
| 2343 | } |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 2344 | if record.is_abstract { |
| 2345 | bail!("Can't directly construct values of type `{}`: it is abstract", record.cc_name); |
| 2346 | } |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 2347 | Ok(()) |
| 2348 | } |
| 2349 | |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 2350 | fn should_derive_clone(record: &Record) -> bool { |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 2351 | if record.is_union() { |
Lukasz Anforowicz | 2469a5e | 2022-06-02 09:44:51 -0700 | [diff] [blame] | 2352 | // `union`s (unlike `struct`s) should only derive `Clone` if they are `Copy`. |
| 2353 | should_derive_copy(record) |
| 2354 | } else { |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 2355 | record.is_unpin() |
| 2356 | && record.copy_constructor == SpecialMemberFunc::Trivial |
| 2357 | && check_by_value(record).is_ok() |
Lukasz Anforowicz | 2469a5e | 2022-06-02 09:44:51 -0700 | [diff] [blame] | 2358 | } |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2361 | fn should_derive_copy(record: &Record) -> bool { |
| 2362 | // TODO(b/202258760): Make `Copy` inclusion configurable. |
Lukasz Anforowicz | 2469a5e | 2022-06-02 09:44:51 -0700 | [diff] [blame] | 2363 | record.is_unpin() |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 2364 | && record.copy_constructor == SpecialMemberFunc::Trivial |
| 2365 | && record.destructor == ir::SpecialMemberFunc::Trivial |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 2366 | && check_by_value(record).is_ok() |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | fn generate_derives(record: &Record) -> Vec<Ident> { |
| 2370 | let mut derives = vec![]; |
Lukasz Anforowicz | 2e41bb6 | 2022-01-11 18:23:07 +0000 | [diff] [blame] | 2371 | if should_derive_clone(record) { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 2372 | derives.push(make_rs_ident("Clone")); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2373 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2374 | if should_derive_copy(record) { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 2375 | derives.push(make_rs_ident("Copy")); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 2376 | } |
| 2377 | derives |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Devin Jeanpierre | 45b0196 | 2022-07-07 06:12:11 -0700 | [diff] [blame] | 2380 | fn generate_enum(db: &Database, enum_: &Enum) -> Result<TokenStream> { |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 2381 | let name = make_rs_ident(&enum_.identifier.identifier); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2382 | let underlying_type = db.rs_type_kind(enum_.underlying_type.rs_type.clone())?; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 2383 | let enumerator_names = |
| 2384 | enum_.enumerators.iter().map(|enumerator| make_rs_ident(&enumerator.identifier.identifier)); |
| 2385 | let enumerator_values = enum_.enumerators.iter().map(|enumerator| enumerator.value); |
| 2386 | Ok(quote! { |
| 2387 | #[repr(transparent)] |
| 2388 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 2389 | pub struct #name(#underlying_type); |
| 2390 | impl #name { |
| 2391 | #(pub const #enumerator_names: #name = #name(#enumerator_values);)* |
| 2392 | } |
| 2393 | impl From<#underlying_type> for #name { |
| 2394 | fn from(value: #underlying_type) -> #name { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 2395 | #name(value) |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | impl From<#name> for #underlying_type { |
| 2399 | fn from(value: #name) -> #underlying_type { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 2400 | value.0 |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | }) |
| 2404 | } |
| 2405 | |
Devin Jeanpierre | 45b0196 | 2022-07-07 06:12:11 -0700 | [diff] [blame] | 2406 | fn generate_type_alias(db: &Database, type_alias: &TypeAlias) -> Result<TokenStream> { |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 2407 | let ident = make_rs_ident(&type_alias.identifier.identifier); |
Lukasz Anforowicz | c503af4 | 2022-03-04 23:18:15 +0000 | [diff] [blame] | 2408 | let doc_comment = generate_doc_comment(&type_alias.doc_comment); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2409 | let underlying_type = db |
| 2410 | .rs_type_kind(type_alias.underlying_type.rs_type.clone()) |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 2411 | .with_context(|| format!("Failed to format underlying type for {:?}", type_alias))?; |
Lukasz Anforowicz | c503af4 | 2022-03-04 23:18:15 +0000 | [diff] [blame] | 2412 | Ok(quote! { |
| 2413 | #doc_comment |
| 2414 | pub type #ident = #underlying_type; |
| 2415 | }) |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 2418 | /// Generates Rust source code for a given `UnsupportedItem`. |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2419 | fn generate_unsupported( |
| 2420 | item: &UnsupportedItem, |
| 2421 | errors: &mut dyn ErrorReporting, |
| 2422 | ) -> Result<TokenStream> { |
| 2423 | errors.insert(item.cause()); |
| 2424 | |
Googler | 48a74dd | 2021-10-25 07:31:53 +0000 | [diff] [blame] | 2425 | let location = if item.source_loc.filename.is_empty() { |
| 2426 | "<unknown location>".to_string() |
| 2427 | } else { |
| 2428 | // TODO(forster): The "google3" prefix should probably come from a command line |
| 2429 | // argument. |
| 2430 | // TODO(forster): Consider linking to the symbol instead of to the line number |
| 2431 | // to avoid wrong links while generated files have not caught up. |
| 2432 | format!("google3/{};l={}", &item.source_loc.filename, &item.source_loc.line) |
| 2433 | }; |
Michael Forster | 6a184ad | 2021-10-12 13:04:05 +0000 | [diff] [blame] | 2434 | let message = format!( |
Googler | 48a74dd | 2021-10-25 07:31:53 +0000 | [diff] [blame] | 2435 | "{}\nError while generating bindings for item '{}':\n{}", |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2436 | &location, |
| 2437 | &item.name, |
| 2438 | item.message() |
Michael Forster | 6a184ad | 2021-10-12 13:04:05 +0000 | [diff] [blame] | 2439 | ); |
Michael Forster | 523dbd4 | 2021-10-12 11:05:44 +0000 | [diff] [blame] | 2440 | Ok(quote! { __COMMENT__ #message }) |
| 2441 | } |
| 2442 | |
Michael Forster | f1dce42 | 2021-10-13 09:50:16 +0000 | [diff] [blame] | 2443 | /// Generates Rust source code for a given `Comment`. |
| 2444 | fn generate_comment(comment: &Comment) -> Result<TokenStream> { |
| 2445 | let text = &comment.text; |
| 2446 | Ok(quote! { __COMMENT__ #text }) |
| 2447 | } |
| 2448 | |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2449 | fn generate_namespace( |
| 2450 | db: &Database, |
| 2451 | namespace: &Namespace, |
| 2452 | errors: &mut dyn ErrorReporting, |
| 2453 | ) -> Result<GeneratedItem> { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2454 | let ir = db.ir(); |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2455 | let mut items = vec![]; |
| 2456 | let mut thunks = vec![]; |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 2457 | let mut thunk_impls = vec![]; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2458 | let mut assertions = vec![]; |
| 2459 | let mut has_record = false; |
| 2460 | let mut features = BTreeSet::new(); |
| 2461 | |
| 2462 | for item_id in namespace.child_item_ids.iter() { |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 2463 | let item = ir.find_decl(*item_id).with_context(|| { |
| 2464 | format!("Failed to look up namespace.child_item_ids for {:?}", namespace) |
| 2465 | })?; |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2466 | let generated = generate_item(db, item, errors)?; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2467 | items.push(generated.item); |
| 2468 | if !generated.thunks.is_empty() { |
| 2469 | thunks.push(generated.thunks); |
| 2470 | } |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 2471 | if !generated.thunk_impls.is_empty() { |
| 2472 | thunk_impls.push(generated.thunk_impls); |
| 2473 | } |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2474 | if !generated.assertions.is_empty() { |
| 2475 | assertions.push(generated.assertions); |
| 2476 | } |
| 2477 | features.extend(generated.features); |
| 2478 | has_record = has_record || generated.has_record; |
| 2479 | } |
| 2480 | |
Rosica Dejanovska | 93aeafb | 2022-06-01 07:05:31 -0700 | [diff] [blame] | 2481 | let reopened_namespace_idx = ir.get_reopened_namespace_idx(namespace.id)?; |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 2482 | // True if this is actually the module with the name `#name`, rather than e.g. |
| 2483 | // `#name_0`, `#name_1`, etc. |
| 2484 | let is_canonical_namespace_module = |
Rosica Dejanovska | 93aeafb | 2022-06-01 07:05:31 -0700 | [diff] [blame] | 2485 | ir.is_last_reopened_namespace(namespace.id, namespace.canonical_namespace_id)?; |
| 2486 | |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 2487 | let name = if is_canonical_namespace_module { |
Rosica Dejanovska | 93aeafb | 2022-06-01 07:05:31 -0700 | [diff] [blame] | 2488 | make_rs_ident(&namespace.name.identifier) |
| 2489 | } else { |
| 2490 | make_rs_ident(&format!("{}_{}", &namespace.name.identifier, reopened_namespace_idx)) |
| 2491 | }; |
| 2492 | |
| 2493 | let use_stmt_for_previous_namespace = if reopened_namespace_idx == 0 { |
| 2494 | quote! {} |
| 2495 | } else { |
| 2496 | let previous_namespace_ident = make_rs_ident(&format!( |
| 2497 | "{}_{}", |
| 2498 | &namespace.name.identifier, |
| 2499 | reopened_namespace_idx - 1 |
| 2500 | )); |
| 2501 | quote! { pub use super::#previous_namespace_ident::*; __NEWLINE__ __NEWLINE__ } |
| 2502 | }; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2503 | |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 2504 | let use_stmt_for_inline_namespace = if namespace.is_inline && is_canonical_namespace_module { |
| 2505 | quote! {pub use #name::*; __NEWLINE__} |
| 2506 | } else { |
| 2507 | quote! {} |
| 2508 | }; |
| 2509 | |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2510 | let namespace_tokens = quote! { |
| 2511 | pub mod #name { |
Rosica Dejanovska | 93aeafb | 2022-06-01 07:05:31 -0700 | [diff] [blame] | 2512 | #use_stmt_for_previous_namespace |
| 2513 | |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2514 | #( #items __NEWLINE__ __NEWLINE__ )* |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2515 | } |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 2516 | __NEWLINE__ |
| 2517 | #use_stmt_for_inline_namespace |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2518 | }; |
| 2519 | |
| 2520 | Ok(GeneratedItem { |
| 2521 | item: namespace_tokens, |
| 2522 | features: features, |
| 2523 | has_record: has_record, |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 2524 | thunks: quote! { #( #thunks )* }, |
| 2525 | thunk_impls: quote! { #( #thunk_impls )* }, |
| 2526 | assertions: quote! { #( #assertions )* }, |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 2527 | ..Default::default() |
| 2528 | }) |
| 2529 | } |
| 2530 | |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2531 | #[derive(Clone, Debug, Default)] |
| 2532 | struct GeneratedItem { |
| 2533 | item: TokenStream, |
| 2534 | thunks: TokenStream, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2535 | // C++ source code for helper functions. |
| 2536 | thunk_impls: TokenStream, |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2537 | assertions: TokenStream, |
| 2538 | features: BTreeSet<Ident>, |
| 2539 | has_record: bool, |
| 2540 | } |
| 2541 | |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2542 | fn generate_item( |
| 2543 | db: &Database, |
| 2544 | item: &Item, |
| 2545 | errors: &mut dyn ErrorReporting, |
| 2546 | ) -> Result<GeneratedItem> { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2547 | let ir = db.ir(); |
Devin Jeanpierre | ab85d44 | 2022-06-29 19:16:41 -0700 | [diff] [blame] | 2548 | let overloaded_funcs = db.overloaded_funcs(); |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2549 | let generated_item = match item { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2550 | Item::Func(func) => match db.generate_func(func.clone()) { |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 2551 | Err(e) => GeneratedItem { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2552 | item: generate_unsupported( |
| 2553 | &make_unsupported_fn(func, &ir, format!("{e}"))?, |
| 2554 | errors, |
| 2555 | )?, |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 2556 | ..Default::default() |
| 2557 | }, |
| 2558 | Ok(None) => GeneratedItem::default(), |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2559 | Ok(Some(f)) => { |
Devin Jeanpierre | ce54920 | 2022-07-25 08:54:34 -0700 | [diff] [blame] | 2560 | let (api_func, thunk, function_id) = &*f; |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2561 | if overloaded_funcs.contains(function_id) { |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2562 | GeneratedItem { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2563 | item: generate_unsupported( |
| 2564 | &make_unsupported_fn( |
| 2565 | func, |
| 2566 | &ir, |
| 2567 | "Cannot generate bindings for overloaded function", |
| 2568 | )?, |
| 2569 | errors, |
| 2570 | )?, |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2571 | ..Default::default() |
| 2572 | } |
| 2573 | } else { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2574 | // TODO(b/236687702): Use Rc for these, or else split this into a non-query |
| 2575 | // and only use the query for Function IDs. |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2576 | GeneratedItem { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2577 | item: api_func.tokens.clone(), |
| 2578 | thunks: thunk.tokens.clone(), |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2579 | features: api_func.features.union(&thunk.features).cloned().collect(), |
| 2580 | ..Default::default() |
| 2581 | } |
| 2582 | } |
| 2583 | } |
| 2584 | }, |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 2585 | Item::IncompleteRecord(incomplete_record) => { |
| 2586 | if !ir.is_current_target(&incomplete_record.owning_target) |
| 2587 | && !ir.is_stdlib_target(&incomplete_record.owning_target) |
| 2588 | { |
Devin Jeanpierre | 090a987 | 2022-04-26 15:19:46 -0700 | [diff] [blame] | 2589 | GeneratedItem::default() |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 2590 | } else { |
| 2591 | GeneratedItem { |
| 2592 | item: generate_incomplete_record(incomplete_record)?, |
| 2593 | ..Default::default() |
| 2594 | } |
| 2595 | } |
| 2596 | } |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2597 | Item::Record(record) => { |
| 2598 | if !ir.is_current_target(&record.owning_target) |
| 2599 | && !ir.is_stdlib_target(&record.owning_target) |
| 2600 | { |
Devin Jeanpierre | 090a987 | 2022-04-26 15:19:46 -0700 | [diff] [blame] | 2601 | GeneratedItem::default() |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2602 | } else { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2603 | generate_record(db, record, errors)? |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | Item::Enum(enum_) => { |
| 2607 | if !ir.is_current_target(&enum_.owning_target) |
| 2608 | && !ir.is_stdlib_target(&enum_.owning_target) |
| 2609 | { |
Devin Jeanpierre | 090a987 | 2022-04-26 15:19:46 -0700 | [diff] [blame] | 2610 | GeneratedItem::default() |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2611 | } else { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2612 | GeneratedItem { item: generate_enum(db, enum_)?, ..Default::default() } |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2613 | } |
| 2614 | } |
| 2615 | Item::TypeAlias(type_alias) => { |
| 2616 | if !ir.is_current_target(&type_alias.owning_target) |
| 2617 | && !ir.is_stdlib_target(&type_alias.owning_target) |
| 2618 | { |
Devin Jeanpierre | 090a987 | 2022-04-26 15:19:46 -0700 | [diff] [blame] | 2619 | GeneratedItem::default() |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 2620 | } else if type_alias.enclosing_record_id.is_some() { |
| 2621 | // TODO(b/200067824): support nested type aliases. |
| 2622 | GeneratedItem { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2623 | item: generate_unsupported( |
| 2624 | &make_unsupported_nested_type_alias(type_alias)?, |
| 2625 | errors, |
| 2626 | )?, |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 2627 | ..Default::default() |
| 2628 | } |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2629 | } else { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2630 | GeneratedItem { item: generate_type_alias(db, type_alias)?, ..Default::default() } |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2631 | } |
| 2632 | } |
| 2633 | Item::UnsupportedItem(unsupported) => { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2634 | GeneratedItem { item: generate_unsupported(unsupported, errors)?, ..Default::default() } |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2635 | } |
| 2636 | Item::Comment(comment) => { |
| 2637 | GeneratedItem { item: generate_comment(comment)?, ..Default::default() } |
| 2638 | } |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2639 | Item::Namespace(namespace) => generate_namespace(db, namespace, errors)?, |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 2640 | Item::UseMod(use_mod) => { |
Devin Jeanpierre | d5a5362 | 2022-10-04 22:00:02 -0700 | [diff] [blame] | 2641 | let UseMod { path, mod_name, .. } = &**use_mod; |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 2642 | let mod_name = make_rs_ident(&mod_name.identifier); |
Devin Jeanpierre | d5a5362 | 2022-10-04 22:00:02 -0700 | [diff] [blame] | 2643 | GeneratedItem { |
Devin Jeanpierre | 96bf0bd | 2022-10-04 20:32:15 -0700 | [diff] [blame] | 2644 | item: quote! { |
| 2645 | #[path = #path] |
| 2646 | mod #mod_name; |
| 2647 | pub use #mod_name::*; |
| 2648 | }, |
| 2649 | ..Default::default() |
| 2650 | } |
| 2651 | } |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2652 | }; |
| 2653 | |
| 2654 | Ok(generated_item) |
| 2655 | } |
| 2656 | |
Devin Jeanpierre | ab85d44 | 2022-06-29 19:16:41 -0700 | [diff] [blame] | 2657 | /// Identifies all functions having overloads that we can't import (yet). |
| 2658 | /// |
| 2659 | /// TODO(b/213280424): Implement support for overloaded functions. |
| 2660 | fn overloaded_funcs(db: &dyn BindingsGenerator) -> Rc<HashSet<Rc<FunctionId>>> { |
| 2661 | let mut seen_funcs = HashSet::new(); |
| 2662 | let mut overloaded_funcs = HashSet::new(); |
| 2663 | for func in db.ir().functions() { |
| 2664 | if let Ok(Some(f)) = db.generate_func(func.clone()) { |
Devin Jeanpierre | ce54920 | 2022-07-25 08:54:34 -0700 | [diff] [blame] | 2665 | let (.., function_id) = &*f; |
Devin Jeanpierre | ab85d44 | 2022-06-29 19:16:41 -0700 | [diff] [blame] | 2666 | if !seen_funcs.insert(function_id.clone()) { |
| 2667 | overloaded_funcs.insert(function_id.clone()); |
| 2668 | } |
| 2669 | } |
| 2670 | } |
| 2671 | Rc::new(overloaded_funcs) |
| 2672 | } |
| 2673 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2674 | // Returns the Rust code implementing bindings, plus any auxiliary C++ code |
| 2675 | // needed to support it. |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2676 | fn generate_bindings_tokens( |
| 2677 | ir: Rc<IR>, |
| 2678 | crubit_support_path: &str, |
| 2679 | errors: &mut dyn ErrorReporting, |
| 2680 | ) -> Result<BindingsTokens> { |
Devin Jeanpierre | 52a14c3 | 2022-06-29 19:12:11 -0700 | [diff] [blame] | 2681 | let mut db = Database::default(); |
| 2682 | db.set_ir(ir.clone()); |
| 2683 | |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 2684 | let mut items = vec![]; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 2685 | let mut thunks = vec![]; |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 2686 | let mut thunk_impls = vec![generate_rs_api_impl(&mut db, crubit_support_path)?]; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2687 | let mut assertions = vec![]; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 2688 | |
Googler | 454f265 | 2021-12-06 12:53:12 +0000 | [diff] [blame] | 2689 | // We import nullable pointers as an Option<&T> and assume that at the ABI |
| 2690 | // level, None is represented as a zero pointer value whereas Some is |
| 2691 | // represented as as non-zero pointer value. This seems like a pretty safe |
| 2692 | // assumption to make, but to provide some safeguard, assert that |
| 2693 | // `Option<&i32>` and `&i32` have the same size. |
| 2694 | assertions.push(quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 2695 | const _: () = assert!(::std::mem::size_of::<Option<&i32>>() == ::std::mem::size_of::<&i32>()); |
Googler | 454f265 | 2021-12-06 12:53:12 +0000 | [diff] [blame] | 2696 | }); |
| 2697 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 2698 | // TODO(jeanpierreda): Delete has_record, either in favor of using RsSnippet, or not |
| 2699 | // having uses. See https://chat.google.com/room/AAAAnQmj8Qs/6QbkSvWcfhA |
Devin Jeanpierre | ea700d3 | 2021-10-06 11:33:56 +0000 | [diff] [blame] | 2700 | let mut has_record = false; |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 2701 | let mut features = BTreeSet::new(); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 2702 | |
Rosica Dejanovska | b2bd59e | 2022-04-11 09:02:03 -0700 | [diff] [blame] | 2703 | for top_level_item_id in ir.top_level_item_ids() { |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 2704 | let item = |
| 2705 | ir.find_decl(*top_level_item_id).context("Failed to look up ir.top_level_item_ids")?; |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 2706 | let generated = generate_item(&db, item, errors)?; |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2707 | items.push(generated.item); |
Devin Jeanpierre | b5ba140 | 2022-03-29 07:29:24 -0700 | [diff] [blame] | 2708 | if !generated.thunks.is_empty() { |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2709 | thunks.push(generated.thunks); |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 2710 | } |
Devin Jeanpierre | b5ba140 | 2022-03-29 07:29:24 -0700 | [diff] [blame] | 2711 | if !generated.assertions.is_empty() { |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2712 | assertions.push(generated.assertions); |
| 2713 | } |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2714 | if !generated.thunk_impls.is_empty() { |
| 2715 | thunk_impls.push(generated.thunk_impls); |
| 2716 | } |
Rosica Dejanovska | 8da8f79 | 2022-03-29 02:02:22 -0700 | [diff] [blame] | 2717 | features.extend(generated.features); |
| 2718 | has_record = has_record || generated.has_record; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 2721 | let mod_detail = if thunks.is_empty() { |
| 2722 | quote! {} |
| 2723 | } else { |
| 2724 | quote! { |
| 2725 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 2726 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 2727 | use super::*; |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 2728 | extern "C" { |
| 2729 | #( #thunks )* |
| 2730 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 2731 | } |
| 2732 | } |
| 2733 | }; |
| 2734 | |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 2735 | let features = if features.is_empty() { |
| 2736 | quote! {} |
| 2737 | } else { |
| 2738 | quote! { |
Lukasz Anforowicz | 16889da | 2022-10-06 13:37:48 -0700 | [diff] [blame] | 2739 | #![feature( #(#features),* )] __NEWLINE__ |
| 2740 | #![allow(stable_features)] |
Devin Jeanpierre | 273eeae | 2021-10-06 13:29:35 +0000 | [diff] [blame] | 2741 | } |
| 2742 | }; |
| 2743 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2744 | Ok(BindingsTokens { |
| 2745 | rs_api: quote! { |
| 2746 | #features __NEWLINE__ |
| 2747 | #![allow(non_camel_case_types)] __NEWLINE__ |
Lukasz Anforowicz | 945919c | 2022-05-12 13:08:47 -0700 | [diff] [blame] | 2748 | #![allow(non_snake_case)] __NEWLINE__ |
Lukasz Anforowicz | cc26263 | 2022-05-12 15:07:43 -0700 | [diff] [blame] | 2749 | #![allow(non_upper_case_globals)] __NEWLINE__ |
| 2750 | #![deny(warnings)] __NEWLINE__ __NEWLINE__ |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 2751 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2752 | #( #items __NEWLINE__ __NEWLINE__ )* |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 2753 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2754 | #mod_detail __NEWLINE__ __NEWLINE__ |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 2755 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 2756 | #( #assertions __NEWLINE__ __NEWLINE__ )* |
| 2757 | }, |
| 2758 | rs_api_impl: quote! {#(#thunk_impls __NEWLINE__ __NEWLINE__ )*}, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 2759 | }) |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 2762 | /// Makes an 'Ident' to be used in the Rust source code. Escapes Rust keywords. |
| 2763 | fn make_rs_ident(ident: &str) -> Ident { |
| 2764 | // TODO(https://github.com/dtolnay/syn/pull/1098): Remove the hardcoded list once syn recognizes |
| 2765 | // 2018 and 2021 keywords. |
| 2766 | if ["async", "await", "try", "dyn"].contains(&ident) { |
| 2767 | return format_ident!("r#{}", ident); |
| 2768 | } |
| 2769 | match syn::parse_str::<syn::Ident>(ident) { |
| 2770 | Ok(_) => format_ident!("{}", ident), |
| 2771 | Err(_) => format_ident!("r#{}", ident), |
| 2772 | } |
| 2773 | } |
| 2774 | |
Lukasz Anforowicz | 5561689 | 2022-10-06 09:16:57 -0700 | [diff] [blame] | 2775 | /// Formats a C++ identifier. Panics if `ident` is a C++ reserved keyword. |
| 2776 | fn format_cc_ident(ident: &str) -> TokenStream { |
| 2777 | code_gen_utils::format_cc_ident(ident).expect("IR should only contain valid C++ identifiers") |
| 2778 | } |
| 2779 | |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2780 | /// Returns Some(crate_ident) if this is an imported crate. |
| 2781 | fn rs_imported_crate_name(owning_target: &BazelLabel, ir: &IR) -> Option<Ident> { |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 2782 | if ir.is_current_target(owning_target) || ir.is_stdlib_target(owning_target) { |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2783 | None |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 2784 | } else { |
Devin Jeanpierre | 084ebbd | 2022-04-01 04:32:46 -0700 | [diff] [blame] | 2785 | let owning_crate_name = owning_target.target_name(); |
Marcel Hlopko | d906b89 | 2022-01-27 08:52:36 +0000 | [diff] [blame] | 2786 | // TODO(b/216587072): Remove this hacky escaping and use the import! macro once |
| 2787 | // available |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 2788 | let escaped_owning_crate_name = owning_crate_name.replace('-', "_"); |
Marcel Hlopko | d906b89 | 2022-01-27 08:52:36 +0000 | [diff] [blame] | 2789 | let owning_crate = make_rs_ident(&escaped_owning_crate_name); |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2790 | Some(owning_crate) |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | |
Devin Jeanpierre | 103cbb5 | 2022-04-06 12:55:16 -0700 | [diff] [blame] | 2794 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 2795 | enum Mutability { |
| 2796 | Const, |
| 2797 | Mut, |
| 2798 | } |
| 2799 | |
| 2800 | impl Mutability { |
| 2801 | fn format_for_pointer(&self) -> TokenStream { |
| 2802 | match self { |
| 2803 | Mutability::Mut => quote! {mut}, |
| 2804 | Mutability::Const => quote! {const}, |
| 2805 | } |
| 2806 | } |
| 2807 | |
| 2808 | fn format_for_reference(&self) -> TokenStream { |
| 2809 | match self { |
| 2810 | Mutability::Mut => quote! {mut}, |
| 2811 | Mutability::Const => quote! {}, |
| 2812 | } |
| 2813 | } |
| 2814 | } |
| 2815 | |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 2816 | /// Either a named lifetime, or the magic `'_` elided lifetime. |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2817 | /// |
| 2818 | /// Warning: elided lifetimes are not always valid, and sometimes named |
| 2819 | /// lifetimes are required. In particular, this should never be used for |
| 2820 | /// output lifetimes. |
| 2821 | /// |
| 2822 | /// However, because output lifetimes are never elided, a lifetime that only |
| 2823 | /// occurs in a single input position can always be elided. |
| 2824 | #[derive(Debug, PartialEq, Eq, Hash, Clone)] |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 2825 | pub struct Lifetime(pub Rc<str>); |
| 2826 | |
| 2827 | impl From<&ir::LifetimeName> for Lifetime { |
| 2828 | fn from(lifetime_name: &ir::LifetimeName) -> Self { |
| 2829 | Lifetime(lifetime_name.name.clone()) |
| 2830 | } |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2831 | } |
| 2832 | |
| 2833 | impl Lifetime { |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 2834 | pub fn new(name: &str) -> Self { |
| 2835 | Lifetime(Rc::from(name)) |
| 2836 | } |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2837 | /// Formats a lifetime for use as a reference lifetime parameter. |
| 2838 | /// |
| 2839 | /// In this case, elided lifetimes are empty. |
| 2840 | pub fn format_for_reference(&self) -> TokenStream { |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 2841 | match &*self.0 { |
| 2842 | "_" => quote! {}, |
| 2843 | _ => quote! {#self}, |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | /// Formats a lifetime for use anywhere. |
| 2849 | /// |
| 2850 | /// For the specific context of references, prefer `format_for_reference`, as it |
| 2851 | /// gives a more idiomatic formatting for elided lifetimes. |
| 2852 | impl ToTokens for Lifetime { |
| 2853 | fn to_tokens(&self, tokens: &mut TokenStream) { |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 2854 | let Self(name) = self; |
| 2855 | let lifetime = syn::Lifetime::new(&format!("'{name}"), proc_macro2::Span::call_site()); |
| 2856 | lifetime.to_tokens(tokens); |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2857 | } |
| 2858 | } |
| 2859 | |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 2860 | #[derive(Clone, Debug, PartialEq, Eq)] |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2861 | enum RsTypeKind { |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2862 | Pointer { |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 2863 | pointee: Rc<RsTypeKind>, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2864 | mutability: Mutability, |
| 2865 | }, |
| 2866 | Reference { |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 2867 | referent: Rc<RsTypeKind>, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2868 | mutability: Mutability, |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2869 | lifetime: Lifetime, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2870 | }, |
| 2871 | RvalueReference { |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 2872 | referent: Rc<RsTypeKind>, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2873 | mutability: Mutability, |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2874 | lifetime: Lifetime, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2875 | }, |
| 2876 | FuncPtr { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2877 | abi: Rc<str>, |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 2878 | return_type: Rc<RsTypeKind>, |
| 2879 | param_types: Rc<[RsTypeKind]>, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2880 | }, |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 2881 | /// An incomplete record type. |
| 2882 | IncompleteRecord { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2883 | incomplete_record: Rc<IncompleteRecord>, |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 2884 | |
| 2885 | /// The imported crate this comes from, or None if the current crate. |
| 2886 | crate_ident: Option<Ident>, |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 2887 | /// The namespace qualifier for this record. |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 2888 | namespace_qualifier: Rc<NamespaceQualifier>, |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 2889 | }, |
| 2890 | /// A complete record type. |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2891 | Record { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2892 | record: Rc<Record>, |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 2893 | /// The namespace qualifier for this record. |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 2894 | namespace_qualifier: Rc<NamespaceQualifier>, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2895 | /// The imported crate this comes from, or None if the current crate. |
| 2896 | crate_ident: Option<Ident>, |
| 2897 | }, |
| 2898 | TypeAlias { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2899 | type_alias: Rc<TypeAlias>, |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 2900 | underlying_type: Rc<RsTypeKind>, |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 2901 | /// The namespace qualifier for this alias. |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 2902 | namespace_qualifier: Rc<NamespaceQualifier>, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2903 | /// The imported crate this comes from, or None if the current crate. |
| 2904 | crate_ident: Option<Ident>, |
| 2905 | }, |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 2906 | Unit, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2907 | Other { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2908 | name: Rc<str>, |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 2909 | type_args: Rc<[RsTypeKind]>, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2910 | }, |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2913 | impl RsTypeKind { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2914 | pub fn new_record(record: Rc<Record>, ir: &IR) -> Result<Self> { |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 2915 | let namespace_qualifier = Rc::new(NamespaceQualifier::new(record.id, ir)?); |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 2916 | let crate_ident = rs_imported_crate_name(&record.owning_target, ir); |
| 2917 | Ok(RsTypeKind::Record { record, namespace_qualifier, crate_ident }) |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 2920 | /// Returns true if the type is known to be `Unpin`, false otherwise. |
Devin Jeanpierre | f85ae59 | 2022-04-01 04:33:57 -0700 | [diff] [blame] | 2921 | pub fn is_unpin(&self) -> bool { |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 2922 | match self { |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 2923 | RsTypeKind::IncompleteRecord { .. } => false, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 2924 | RsTypeKind::Record { record, .. } => record.is_unpin(), |
Devin Jeanpierre | f85ae59 | 2022-04-01 04:33:57 -0700 | [diff] [blame] | 2925 | RsTypeKind::TypeAlias { underlying_type, .. } => underlying_type.is_unpin(), |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 2926 | _ => true, |
| 2927 | } |
| 2928 | } |
| 2929 | |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 2930 | /// Returns true if the type is known to be move-constructible, false |
| 2931 | /// otherwise. |
| 2932 | /// |
| 2933 | /// For the purposes of this method, references are considered |
| 2934 | /// move-constructible (as if they were pointers). |
| 2935 | pub fn is_move_constructible(&self) -> bool { |
| 2936 | match self { |
| 2937 | RsTypeKind::IncompleteRecord { .. } => false, |
| 2938 | RsTypeKind::Record { record, .. } => { |
| 2939 | record.move_constructor != ir::SpecialMemberFunc::Unavailable |
| 2940 | } |
| 2941 | RsTypeKind::TypeAlias { underlying_type, .. } => { |
| 2942 | underlying_type.is_move_constructible() |
| 2943 | } |
| 2944 | _ => true, |
| 2945 | } |
| 2946 | } |
| 2947 | |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 2948 | /// Returns Ok if the type can be used by value, or an error describing why |
| 2949 | /// it can't. |
| 2950 | pub fn check_by_value(&self) -> Result<()> { |
| 2951 | match self { |
| 2952 | RsTypeKind::Record { record, .. } => check_by_value(record), |
| 2953 | RsTypeKind::TypeAlias { underlying_type, .. } => underlying_type.check_by_value(), |
| 2954 | _ => Ok(()), |
| 2955 | } |
| 2956 | } |
| 2957 | |
Devin Jeanpierre | 82e8e36 | 2022-04-05 11:04:55 -0700 | [diff] [blame] | 2958 | pub fn format_as_return_type_fragment(&self) -> TokenStream { |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 2959 | match self { |
Devin Jeanpierre | d2b7a8f | 2022-04-01 04:37:16 -0700 | [diff] [blame] | 2960 | RsTypeKind::Unit => quote! {}, |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 2961 | other_type => { |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 2962 | quote! { -> #other_type } |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 2963 | } |
| 2964 | } |
| 2965 | } |
| 2966 | |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 2967 | /// Formats this RsTypeKind as `&'a mut MaybeUninit<SomeStruct>`. This is |
| 2968 | /// used to format `__this` parameter in a constructor thunk. |
Devin Jeanpierre | 82e8e36 | 2022-04-05 11:04:55 -0700 | [diff] [blame] | 2969 | pub fn format_mut_ref_as_uninitialized(&self) -> Result<TokenStream> { |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 2970 | match self { |
Devin Jeanpierre | d2b7a8f | 2022-04-01 04:37:16 -0700 | [diff] [blame] | 2971 | RsTypeKind::Reference { referent, lifetime, mutability: Mutability::Mut } => { |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 2972 | let lifetime = lifetime.format_for_reference(); |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 2973 | Ok(quote! { & #lifetime mut ::std::mem::MaybeUninit< #referent > }) |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 2974 | } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 2975 | _ => bail!("Expected reference to format as MaybeUninit, got: {:?}", self), |
Lukasz Anforowicz | cde4b1b | 2022-02-03 21:20:55 +0000 | [diff] [blame] | 2976 | } |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 2979 | /// Formats this RsTypeKind as the `self` parameter: usually, `&'a self` or |
| 2980 | /// `&'a mut self`. |
| 2981 | /// |
| 2982 | /// If this is !Unpin, however, it uses `self: Pin<&mut Self>` instead. |
Devin Jeanpierre | 108e9c0 | 2022-06-02 07:10:09 -0700 | [diff] [blame] | 2983 | pub fn format_as_self_param(&self) -> Result<TokenStream> { |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 2984 | let referent; |
| 2985 | let mutability; |
| 2986 | let lifetime; |
| 2987 | match self { |
Devin Jeanpierre | 108e9c0 | 2022-06-02 07:10:09 -0700 | [diff] [blame] | 2988 | RsTypeKind::Pointer { .. } => { |
Devin Jeanpierre | 8a4fa20 | 2022-06-08 12:33:11 -0700 | [diff] [blame] | 2989 | // TODO(jeanpierreda): provide end-user-facing docs, and insert a link to e.g. |
| 2990 | // something like <internal link> |
| 2991 | bail!( |
| 2992 | "`self` has no lifetime. Use lifetime annotations or `#pragma clang lifetime_elision` to create bindings for this function." |
| 2993 | ) |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 2994 | } |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 2995 | RsTypeKind::Reference { |
| 2996 | referent: reference_pointee, |
| 2997 | lifetime: reference_lifetime, |
| 2998 | mutability: reference_mutability, |
| 2999 | } => { |
| 3000 | referent = reference_pointee; |
| 3001 | mutability = reference_mutability; |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 3002 | lifetime = reference_lifetime; |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 3003 | } |
Michael VanBemmel | 83eca6b | 2022-07-20 10:16:38 -0700 | [diff] [blame] | 3004 | RsTypeKind::Record { .. } => { |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 3005 | // This case doesn't happen for methods, but is needed for free functions mapped |
| 3006 | // to a trait impl that take the first argument by value. |
Michael VanBemmel | 83eca6b | 2022-07-20 10:16:38 -0700 | [diff] [blame] | 3007 | return Ok(quote! { self }); |
| 3008 | } |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 3009 | _ => bail!("Unexpected type of `self` parameter: {:?}", self), |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 3010 | } |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 3011 | let mut_ = mutability.format_for_reference(); |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 3012 | let lifetime = lifetime.format_for_reference(); |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 3013 | if mutability == &Mutability::Mut && !referent.is_unpin() { |
Devin Jeanpierre | 5d5ca7a | 2022-07-20 02:27:00 -0700 | [diff] [blame] | 3014 | // TODO(b/239661934): Add a `use ::std::pin::Pin` to the crate, and use |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 3015 | // `Pin`. |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 3016 | Ok(quote! {self: ::std::pin::Pin< & #lifetime #mut_ Self>}) |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 3017 | } else { |
| 3018 | Ok(quote! { & #lifetime #mut_ self }) |
| 3019 | } |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3022 | /// Returns whether the type represented by `self` implements the `Copy` |
| 3023 | /// trait. |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 3024 | pub fn implements_copy(&self) -> bool { |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3025 | // TODO(b/212696226): Verify results of `implements_copy` via static |
| 3026 | // assertions in the generated Rust code (because incorrect results |
| 3027 | // can silently lead to unsafe behavior). |
| 3028 | match self { |
| 3029 | RsTypeKind::Unit => true, |
| 3030 | RsTypeKind::Pointer { .. } => true, |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 3031 | RsTypeKind::FuncPtr { .. } => true, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3032 | RsTypeKind::Reference { mutability: Mutability::Const, .. } => true, |
| 3033 | RsTypeKind::Reference { mutability: Mutability::Mut, .. } => false, |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 3034 | RsTypeKind::RvalueReference { .. } => false, |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 3035 | RsTypeKind::IncompleteRecord { .. } => false, |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 3036 | RsTypeKind::Record { record, .. } => should_derive_copy(record), |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3037 | RsTypeKind::TypeAlias { underlying_type, .. } => underlying_type.implements_copy(), |
Lukasz Anforowicz | d81bea9 | 2022-02-11 08:57:58 +0000 | [diff] [blame] | 3038 | RsTypeKind::Other { type_args, .. } => { |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 3039 | // All types that may appear here without `type_args` (e.g. |
| 3040 | // primitive types like `i32`) implement `Copy`. Generic types |
| 3041 | // that may be present here (e.g. Option<...>) are `Copy` if all |
| 3042 | // of their `type_args` are `Copy`. |
| 3043 | type_args.iter().all(|t| t.implements_copy()) |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 3044 | } |
| 3045 | } |
| 3046 | } |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 3047 | |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 3048 | pub fn is_ref_to(&self, expected_record: &Record) -> bool { |
| 3049 | match self { |
| 3050 | RsTypeKind::Reference { referent, .. } => referent.is_record(expected_record), |
| 3051 | _ => false, |
| 3052 | } |
| 3053 | } |
| 3054 | |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 3055 | pub fn is_shared_ref_to(&self, expected_record: &Record) -> bool { |
| 3056 | match self { |
| 3057 | RsTypeKind::Reference { referent, mutability: Mutability::Const, .. } => { |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 3058 | referent.is_record(expected_record) |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 3059 | } |
| 3060 | _ => false, |
| 3061 | } |
| 3062 | } |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 3063 | |
| 3064 | pub fn is_record(&self, expected_record: &Record) -> bool { |
| 3065 | match self { |
Devin Jeanpierre | fec5bfe | 2022-04-05 10:59:10 -0700 | [diff] [blame] | 3066 | RsTypeKind::Record { record: actual_record, .. } => { |
| 3067 | actual_record.id == expected_record.id |
| 3068 | } |
Lukasz Anforowicz | f956462 | 2022-01-28 14:31:04 +0000 | [diff] [blame] | 3069 | _ => false, |
| 3070 | } |
| 3071 | } |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3072 | |
| 3073 | /// Iterates over `self` and all the nested types (e.g. pointees, generic |
| 3074 | /// type args, etc.) in DFS order. |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 3075 | pub fn dfs_iter<'ty>(&'ty self) -> impl Iterator<Item = &'ty RsTypeKind> + '_ { |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3076 | RsTypeKindIter::new(self) |
| 3077 | } |
| 3078 | |
| 3079 | /// Iterates over all `LifetimeId`s in `self` and in all the nested types. |
| 3080 | /// Note that the results might contain duplicate LifetimeId values (e.g. |
| 3081 | /// if the same LifetimeId is used in two `type_args`). |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 3082 | pub fn lifetimes(&self) -> impl Iterator<Item = Lifetime> + '_ { |
| 3083 | self.dfs_iter().filter_map(Self::lifetime) |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3084 | } |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 3085 | |
| 3086 | /// Returns the pointer or reference target. |
| 3087 | pub fn referent(&self) -> Option<&RsTypeKind> { |
| 3088 | match self { |
| 3089 | Self::Pointer { pointee: p, .. } |
| 3090 | | Self::Reference { referent: p, .. } |
| 3091 | | Self::RvalueReference { referent: p, .. } => Some(&**p), |
| 3092 | _ => None, |
| 3093 | } |
| 3094 | } |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 3095 | |
| 3096 | /// Returns the reference lifetime, or None if this is not a reference. |
| 3097 | pub fn lifetime(&self) -> Option<Lifetime> { |
| 3098 | match self { |
| 3099 | Self::Reference { lifetime, .. } | Self::RvalueReference { lifetime, .. } => { |
| 3100 | Some(lifetime.clone()) |
| 3101 | } |
| 3102 | _ => None, |
| 3103 | } |
| 3104 | } |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 3107 | impl ToTokens for RsTypeKind { |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3108 | fn to_tokens(&self, tokens: &mut TokenStream) { |
| 3109 | self.to_token_stream().to_tokens(tokens) |
| 3110 | } |
| 3111 | |
| 3112 | fn to_token_stream(&self) -> TokenStream { |
| 3113 | match self { |
| 3114 | RsTypeKind::Pointer { pointee, mutability } => { |
| 3115 | let mutability = mutability.format_for_pointer(); |
| 3116 | quote! {* #mutability #pointee} |
| 3117 | } |
| 3118 | RsTypeKind::Reference { referent, mutability, lifetime } => { |
| 3119 | let mut_ = mutability.format_for_reference(); |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 3120 | let lifetime = lifetime.format_for_reference(); |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3121 | let reference = quote! {& #lifetime #mut_ #referent}; |
| 3122 | if mutability == &Mutability::Mut && !referent.is_unpin() { |
Devin Jeanpierre | 5d5ca7a | 2022-07-20 02:27:00 -0700 | [diff] [blame] | 3123 | // TODO(b/239661934): Add a `use ::std::pin::Pin` to the crate, and use |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3124 | // `Pin`. This either requires deciding how to qualify pin at |
| 3125 | // RsTypeKind-creation time, or returning an RsSnippet from here (and not |
| 3126 | // implementing ToTokens, but instead some other interface.) |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 3127 | quote! {::std::pin::Pin< #reference >} |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3128 | } else { |
| 3129 | reference |
| 3130 | } |
| 3131 | } |
| 3132 | RsTypeKind::RvalueReference { referent, mutability, lifetime } => { |
Devin Jeanpierre | 5d5ca7a | 2022-07-20 02:27:00 -0700 | [diff] [blame] | 3133 | // TODO(b/239661934): Add a `use ::ctor::RvalueReference` (etc.) to the crate. |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3134 | if mutability == &Mutability::Mut { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 3135 | quote! {::ctor::RvalueReference<#lifetime, #referent>} |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3136 | } else { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 3137 | quote! {::ctor::ConstRvalueReference<#lifetime, #referent>} |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3138 | } |
| 3139 | } |
| 3140 | RsTypeKind::FuncPtr { abi, return_type, param_types } => { |
| 3141 | let return_frag = return_type.format_as_return_type_fragment(); |
| 3142 | quote! { extern #abi fn( #( #param_types ),* ) #return_frag } |
| 3143 | } |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 3144 | RsTypeKind::IncompleteRecord { |
| 3145 | incomplete_record, |
| 3146 | namespace_qualifier, |
| 3147 | crate_ident, |
| 3148 | } => { |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 3149 | let record_ident = make_rs_ident(&incomplete_record.rs_name); |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 3150 | let namespace_qualifier = namespace_qualifier.format_for_rs(); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3151 | match crate_ident { |
| 3152 | Some(ci) => { |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3153 | quote! { #ci #namespace_qualifier #record_ident } |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 3154 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3155 | None => { |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3156 | quote! { crate:: #namespace_qualifier #record_ident } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3157 | } |
| 3158 | } |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 3159 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3160 | RsTypeKind::Record { record, namespace_qualifier, crate_ident } => { |
| 3161 | let ident = make_rs_ident(&record.rs_name); |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 3162 | let namespace_qualifier = namespace_qualifier.format_for_rs(); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3163 | match crate_ident { |
| 3164 | Some(ci) => { |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3165 | quote! { #ci:: #namespace_qualifier #ident } |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 3166 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3167 | None => { |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3168 | quote! { crate:: #namespace_qualifier #ident } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3169 | } |
| 3170 | } |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3171 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3172 | RsTypeKind::TypeAlias { type_alias, namespace_qualifier, crate_ident, .. } => { |
| 3173 | let ident = make_rs_ident(&type_alias.identifier.identifier); |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 3174 | let namespace_qualifier = namespace_qualifier.format_for_rs(); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3175 | match crate_ident { |
| 3176 | Some(ci) => { |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3177 | quote! { #ci:: #namespace_qualifier #ident } |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 3178 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3179 | None => { |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3180 | quote! { crate:: #namespace_qualifier #ident } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3181 | } |
| 3182 | } |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3183 | } |
Googler | 7302168 | 2022-10-28 02:39:25 -0700 | [diff] [blame] | 3184 | // This doesn't affect void in function return values, as those are special-cased to be |
| 3185 | // omitted. |
| 3186 | RsTypeKind::Unit => quote!{::std::os::raw::c_void}, |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3187 | RsTypeKind::Other { name, type_args } => { |
| 3188 | let ident = make_rs_ident(name); |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 3189 | let generic_params = |
| 3190 | format_generic_params(/* lifetimes= */ &[], type_args.iter()); |
Devin Jeanpierre | 92ca261 | 2022-04-06 11:35:13 -0700 | [diff] [blame] | 3191 | quote! {#ident #generic_params} |
| 3192 | } |
| 3193 | } |
| 3194 | } |
| 3195 | } |
| 3196 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 3197 | struct RsTypeKindIter<'ty> { |
| 3198 | todo: Vec<&'ty RsTypeKind>, |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3199 | } |
| 3200 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 3201 | impl<'ty> RsTypeKindIter<'ty> { |
| 3202 | pub fn new(ty: &'ty RsTypeKind) -> Self { |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3203 | Self { todo: vec![ty] } |
| 3204 | } |
| 3205 | } |
| 3206 | |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 3207 | impl<'ty> Iterator for RsTypeKindIter<'ty> { |
| 3208 | type Item = &'ty RsTypeKind; |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3209 | |
| 3210 | fn next(&mut self) -> Option<Self::Item> { |
| 3211 | match self.todo.pop() { |
| 3212 | None => None, |
| 3213 | Some(curr) => { |
| 3214 | match curr { |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 3215 | RsTypeKind::Unit |
| 3216 | | RsTypeKind::IncompleteRecord { .. } |
| 3217 | | RsTypeKind::Record { .. } => {} |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3218 | RsTypeKind::Pointer { pointee, .. } => self.todo.push(pointee), |
| 3219 | RsTypeKind::Reference { referent, .. } => self.todo.push(referent), |
Devin Jeanpierre | deea789 | 2022-03-29 02:13:31 -0700 | [diff] [blame] | 3220 | RsTypeKind::RvalueReference { referent, .. } => self.todo.push(referent), |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3221 | RsTypeKind::TypeAlias { underlying_type: t, .. } => self.todo.push(t), |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 3222 | RsTypeKind::FuncPtr { return_type, param_types, .. } => { |
| 3223 | self.todo.push(return_type); |
| 3224 | self.todo.extend(param_types.iter().rev()); |
Devin Jeanpierre | 1b8f4a1 | 2022-03-22 21:29:42 +0000 | [diff] [blame] | 3225 | } |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 3226 | RsTypeKind::Other { type_args, .. } => self.todo.extend(type_args.iter().rev()), |
| 3227 | }; |
| 3228 | Some(curr) |
| 3229 | } |
| 3230 | } |
| 3231 | } |
Lukasz Anforowicz | 40c2eb8 | 2022-01-11 18:22:31 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 3234 | fn unique_lifetimes<'a>( |
| 3235 | types: impl IntoIterator<Item = &'a RsTypeKind> + 'a, |
| 3236 | ) -> impl Iterator<Item = Lifetime> + 'a { |
| 3237 | let mut unordered_lifetimes = HashSet::new(); |
| 3238 | types |
| 3239 | .into_iter() |
| 3240 | .flat_map(|ty| ty.lifetimes()) |
| 3241 | .filter(move |lifetime| unordered_lifetimes.insert(lifetime.clone())) |
| 3242 | } |
| 3243 | |
Devin Jeanpierre | 3a0cc5a | 2022-07-12 09:36:34 -0700 | [diff] [blame] | 3244 | fn rs_type_kind(db: &dyn BindingsGenerator, ty: ir::RsType) -> Result<RsTypeKind> { |
| 3245 | let ir = db.ir(); |
| 3246 | // The lambdas deduplicate code needed by multiple `match` branches. |
| 3247 | let get_type_args = || -> Result<Vec<RsTypeKind>> { |
| 3248 | ty.type_args.iter().map(|type_arg| db.rs_type_kind(type_arg.clone())).collect() |
| 3249 | }; |
| 3250 | let get_pointee = || -> Result<Rc<RsTypeKind>> { |
| 3251 | if ty.type_args.len() != 1 { |
| 3252 | bail!("Missing pointee/referent type (need exactly 1 type argument): {:?}", ty); |
| 3253 | } |
| 3254 | Ok(Rc::new(get_type_args()?.remove(0))) |
| 3255 | }; |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 3256 | let get_lifetime = || -> Result<Lifetime> { |
Devin Jeanpierre | 3a0cc5a | 2022-07-12 09:36:34 -0700 | [diff] [blame] | 3257 | if ty.lifetime_args.len() != 1 { |
| 3258 | bail!("Missing reference lifetime (need exactly 1 lifetime argument): {:?}", ty); |
| 3259 | } |
| 3260 | let lifetime_id = ty.lifetime_args[0]; |
| 3261 | ir.get_lifetime(lifetime_id) |
| 3262 | .ok_or_else(|| anyhow!("no known lifetime with id {lifetime_id:?}")) |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 3263 | .map(Lifetime::from) |
Devin Jeanpierre | 3a0cc5a | 2022-07-12 09:36:34 -0700 | [diff] [blame] | 3264 | }; |
| 3265 | |
| 3266 | let result = match ty.name.as_deref() { |
| 3267 | None => { |
| 3268 | ensure!( |
| 3269 | ty.type_args.is_empty(), |
| 3270 | "Type arguments on records nor type aliases are not yet supported: {:?}", |
| 3271 | ty |
| 3272 | ); |
| 3273 | match ir.item_for_type(&ty)? { |
| 3274 | Item::IncompleteRecord(incomplete_record) => RsTypeKind::IncompleteRecord { |
| 3275 | incomplete_record: incomplete_record.clone(), |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 3276 | namespace_qualifier: Rc::new(NamespaceQualifier::new( |
| 3277 | incomplete_record.id, |
| 3278 | &ir, |
| 3279 | )?), |
Devin Jeanpierre | 3a0cc5a | 2022-07-12 09:36:34 -0700 | [diff] [blame] | 3280 | crate_ident: rs_imported_crate_name(&incomplete_record.owning_target, &ir), |
| 3281 | }, |
| 3282 | Item::Record(record) => RsTypeKind::new_record(record.clone(), &ir)?, |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 3283 | Item::TypeAlias(type_alias) => { |
| 3284 | // TODO(b/200067824): support nested type aliases. |
| 3285 | if type_alias.enclosing_record_id.is_some() { |
| 3286 | // Until this is supported, we import this as the underlying type. |
| 3287 | db.rs_type_kind(type_alias.underlying_type.rs_type.clone())? |
| 3288 | } else { |
| 3289 | RsTypeKind::TypeAlias { |
| 3290 | type_alias: type_alias.clone(), |
| 3291 | namespace_qualifier: Rc::new(NamespaceQualifier::new( |
| 3292 | type_alias.id, |
| 3293 | &ir, |
| 3294 | )?), |
| 3295 | crate_ident: rs_imported_crate_name(&type_alias.owning_target, &ir), |
| 3296 | underlying_type: Rc::new( |
| 3297 | db.rs_type_kind(type_alias.underlying_type.rs_type.clone())?, |
| 3298 | ), |
| 3299 | } |
| 3300 | } |
| 3301 | } |
Devin Jeanpierre | 3a0cc5a | 2022-07-12 09:36:34 -0700 | [diff] [blame] | 3302 | other_item => bail!("Item does not define a type: {:?}", other_item), |
| 3303 | } |
| 3304 | } |
| 3305 | Some(name) => match name { |
| 3306 | "()" => { |
| 3307 | if !ty.type_args.is_empty() { |
| 3308 | bail!("Unit type must not have type arguments: {:?}", ty); |
| 3309 | } |
| 3310 | RsTypeKind::Unit |
| 3311 | } |
| 3312 | "*mut" => RsTypeKind::Pointer { pointee: get_pointee()?, mutability: Mutability::Mut }, |
| 3313 | "*const" => { |
| 3314 | RsTypeKind::Pointer { pointee: get_pointee()?, mutability: Mutability::Const } |
| 3315 | } |
| 3316 | "&mut" => RsTypeKind::Reference { |
| 3317 | referent: get_pointee()?, |
| 3318 | mutability: Mutability::Mut, |
| 3319 | lifetime: get_lifetime()?, |
| 3320 | }, |
| 3321 | "&" => RsTypeKind::Reference { |
| 3322 | referent: get_pointee()?, |
| 3323 | mutability: Mutability::Const, |
| 3324 | lifetime: get_lifetime()?, |
| 3325 | }, |
| 3326 | "#RvalueReference mut" => RsTypeKind::RvalueReference { |
| 3327 | referent: get_pointee()?, |
| 3328 | mutability: Mutability::Mut, |
| 3329 | lifetime: get_lifetime()?, |
| 3330 | }, |
| 3331 | "#RvalueReference const" => RsTypeKind::RvalueReference { |
| 3332 | referent: get_pointee()?, |
| 3333 | mutability: Mutability::Const, |
| 3334 | lifetime: get_lifetime()?, |
| 3335 | }, |
| 3336 | name => { |
| 3337 | let mut type_args = get_type_args()?; |
| 3338 | match name.strip_prefix("#funcPtr ") { |
| 3339 | None => RsTypeKind::Other { name: name.into(), type_args: Rc::from(type_args) }, |
| 3340 | Some(abi) => { |
| 3341 | // TODO(b/217419782): Consider enforcing `'static` lifetime. |
| 3342 | ensure!(!type_args.is_empty(), "No return type in fn type: {:?}", ty); |
| 3343 | RsTypeKind::FuncPtr { |
| 3344 | abi: abi.into(), |
| 3345 | return_type: Rc::new(type_args.remove(type_args.len() - 1)), |
| 3346 | param_types: Rc::from(type_args), |
| 3347 | } |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | }, |
| 3352 | }; |
| 3353 | Ok(result) |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3356 | fn cc_type_name_for_record(record: &Record, ir: &IR) -> Result<TokenStream> { |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 3357 | let tagless = cc_tagless_type_name_for_record(record, ir)?; |
| 3358 | let tag_kind = cc_tag_kind(record); |
| 3359 | Ok(quote! { #tag_kind #tagless }) |
| 3360 | } |
| 3361 | |
| 3362 | fn cc_tagless_type_name_for_record(record: &Record, ir: &IR) -> Result<TokenStream> { |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3363 | let ident = format_cc_ident(&record.cc_name); |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 3364 | let namespace_qualifier = NamespaceQualifier::new(record.id, ir)?.format_for_cc(); |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 3365 | Ok(quote! { #namespace_qualifier #ident }) |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3366 | } |
| 3367 | |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3368 | fn cc_type_name_for_item(item: &ir::Item, ir: &IR) -> Result<TokenStream> { |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3369 | match item { |
Lukasz Anforowicz | 8dd5179 | 2022-08-31 10:11:17 -0700 | [diff] [blame] | 3370 | Item::IncompleteRecord(incomplete_record) => { |
| 3371 | let ident = format_cc_ident(&incomplete_record.cc_name); |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 3372 | let namespace_qualifier = |
| 3373 | NamespaceQualifier::new(incomplete_record.id, ir)?.format_for_cc(); |
Lukasz Anforowicz | 8dd5179 | 2022-08-31 10:11:17 -0700 | [diff] [blame] | 3374 | let tag_kind = incomplete_record.record_type; |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3375 | Ok(quote! { #tag_kind #namespace_qualifier #ident }) |
Lukasz Anforowicz | 8dd5179 | 2022-08-31 10:11:17 -0700 | [diff] [blame] | 3376 | } |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3377 | Item::Record(record) => cc_type_name_for_record(record, ir), |
Lukasz Anforowicz | 4c3a2cc | 2022-03-11 00:24:49 +0000 | [diff] [blame] | 3378 | Item::TypeAlias(type_alias) => { |
| 3379 | let ident = format_cc_ident(&type_alias.identifier.identifier); |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 3380 | if let Some(record_id) = type_alias.enclosing_record_id { |
| 3381 | let parent = |
| 3382 | cc_tagless_type_name_for_record(ir.find_decl::<Rc<Record>>(record_id)?, ir)?; |
| 3383 | Ok(quote! { #parent :: #ident }) |
| 3384 | } else { |
| 3385 | let namespace_qualifier = |
| 3386 | NamespaceQualifier::new(type_alias.id, ir)?.format_for_cc(); |
| 3387 | Ok(quote! { #namespace_qualifier #ident }) |
| 3388 | } |
Devin Jeanpierre | 1b8f4a1 | 2022-03-22 21:29:42 +0000 | [diff] [blame] | 3389 | } |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3390 | _ => bail!("Item does not define a type: {:?}", item), |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3391 | } |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 3394 | fn cc_tag_kind(record: &ir::Record) -> TokenStream { |
Kinuko Yasuda | 8dd8464 | 2022-08-17 09:19:47 -0700 | [diff] [blame] | 3395 | if record.is_anon_record_with_typedef { |
| 3396 | quote! {} |
| 3397 | } else { |
Lukasz Anforowicz | 8dd5179 | 2022-08-31 10:11:17 -0700 | [diff] [blame] | 3398 | record.record_type.into_token_stream() |
Marcel Hlopko | 4c29c6f | 2022-05-04 00:49:14 -0700 | [diff] [blame] | 3399 | } |
| 3400 | } |
| 3401 | |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 3402 | // Maps a Rust ABI [1] into a Clang attribute. See also |
| 3403 | // `ConvertCcCallConvIntoRsApi` in importer.cc. |
| 3404 | // [1] |
| 3405 | // https://doc.rust-lang.org/reference/items/functions.html#extern-function-qualifier |
| 3406 | fn format_cc_call_conv_as_clang_attribute(rs_abi: &str) -> Result<TokenStream> { |
| 3407 | match rs_abi { |
| 3408 | "cdecl" => Ok(quote! {}), |
| 3409 | "fastcall" => Ok(quote! { __attribute__((fastcall)) }), |
| 3410 | "stdcall" => Ok(quote! { __attribute__((stdcall)) }), |
| 3411 | "thiscall" => Ok(quote! { __attribute__((thiscall)) }), |
| 3412 | "vectorcall" => Ok(quote! { __attribute__((vectorcall)) }), |
| 3413 | _ => bail!("Unsupported ABI: {}", rs_abi), |
| 3414 | } |
| 3415 | } |
| 3416 | |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 3417 | fn format_cc_type(ty: &ir::CcType, ir: &IR) -> Result<TokenStream> { |
Devin Jeanpierre | 69fe239 | 2022-10-04 22:01:24 -0700 | [diff] [blame] | 3418 | // Formatting *both* pointers *and* references as pointers, because: |
| 3419 | // - Pointers and references have the same representation in the ABI. |
| 3420 | // - Clang's `-Wreturn-type-c-linkage` warns when using references in C++ |
| 3421 | // function thunks declared as `extern "C"` (see b/238681766). |
| 3422 | format_cc_type_inner(ty, ir, /* references_ok= */ false) |
| 3423 | } |
| 3424 | fn format_cc_type_inner(ty: &ir::CcType, ir: &IR, references_ok: bool) -> Result<TokenStream> { |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 3425 | let const_fragment = if ty.is_const { |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 3426 | quote! {const} |
| 3427 | } else { |
| 3428 | quote! {} |
| 3429 | }; |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 3430 | if let Some(ref name) = ty.name { |
| 3431 | match name.as_str() { |
Devin Jeanpierre | 69fe239 | 2022-10-04 22:01:24 -0700 | [diff] [blame] | 3432 | mut name @ ("*" | "&" | "&&") => { |
Googler | ff7fc23 | 2021-12-02 09:43:00 +0000 | [diff] [blame] | 3433 | if ty.type_args.len() != 1 { |
| 3434 | bail!("Invalid pointer type (need exactly 1 type argument): {:?}", ty); |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 3435 | } |
Devin Jeanpierre | 69fe239 | 2022-10-04 22:01:24 -0700 | [diff] [blame] | 3436 | let nested_type = format_cc_type_inner(&ty.type_args[0], ir, references_ok)?; |
| 3437 | if !references_ok { |
| 3438 | name = "*"; |
| 3439 | } |
| 3440 | let ptr = match name { |
| 3441 | "*" => quote! {*}, |
| 3442 | "&" => quote! {&}, |
| 3443 | "&&" => quote! {&&}, |
| 3444 | _ => unreachable!(), |
| 3445 | }; |
| 3446 | Ok(quote! {#nested_type #ptr #const_fragment}) |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 3447 | } |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 3448 | cc_type_name => match cc_type_name.strip_prefix("#funcValue ") { |
| 3449 | None => { |
| 3450 | if !ty.type_args.is_empty() { |
| 3451 | bail!("Type not yet supported: {:?}", ty); |
| 3452 | } |
Lukasz Anforowicz | 20a3c69 | 2022-10-06 08:48:15 -0700 | [diff] [blame] | 3453 | // Not using `code_gen_utils::format_cc_ident`, because |
| 3454 | // `cc_type_name` may be a C++ reserved keyword (e.g. |
| 3455 | // `int`). |
| 3456 | let cc_ident: TokenStream = cc_type_name.parse().unwrap(); |
Googler | 84cd560 | 2022-10-07 08:25:46 -0700 | [diff] [blame] | 3457 | Ok(quote! { #cc_ident #const_fragment }) |
Devin Jeanpierre | 1b8f4a1 | 2022-03-22 21:29:42 +0000 | [diff] [blame] | 3458 | } |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 3459 | Some(abi) => match ty.type_args.split_last() { |
| 3460 | None => bail!("funcValue type without a return type: {:?}", ty), |
| 3461 | Some((ret_type, param_types)) => { |
Devin Jeanpierre | 69fe239 | 2022-10-04 22:01:24 -0700 | [diff] [blame] | 3462 | // Function pointer types don't ignore references, but luckily, |
| 3463 | // `-Wreturn-type-c-linkage` does. So we can just re-enable references now |
| 3464 | // so that the function type is exactly correct. |
| 3465 | let ret_type = |
| 3466 | format_cc_type_inner(ret_type, ir, /* references_ok= */ true)?; |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 3467 | let param_types = param_types |
| 3468 | .iter() |
Devin Jeanpierre | 69fe239 | 2022-10-04 22:01:24 -0700 | [diff] [blame] | 3469 | .map(|t| format_cc_type_inner(t, ir, /* references_ok= */ true)) |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 3470 | .collect::<Result<Vec<_>>>()?; |
| 3471 | let attr = format_cc_call_conv_as_clang_attribute(abi)?; |
| 3472 | // `type_identity_t` is used below to avoid having to |
| 3473 | // emit spiral-like syntax where some syntax elements of |
| 3474 | // an inner type (e.g. function type as below) can |
| 3475 | // surround syntax elements of an outer type (e.g. a |
| 3476 | // pointer type). Compare: `int (*foo)(int, int)` VS |
| 3477 | // `type_identity_t<int(int, int)>* foo`. |
Lukasz Anforowicz | 2317154 | 2022-04-11 15:26:17 -0700 | [diff] [blame] | 3478 | Ok(quote! { crubit::type_identity_t< |
Devin Jeanpierre | 1b8f4a1 | 2022-03-22 21:29:42 +0000 | [diff] [blame] | 3479 | #ret_type ( #( #param_types ),* ) #attr |
| 3480 | > }) |
| 3481 | } |
| 3482 | }, |
| 3483 | }, |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 3484 | } |
Marcel Hlopko | c0956cf | 2021-11-29 08:31:28 +0000 | [diff] [blame] | 3485 | } else { |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3486 | let item = ir.item_for_type(ty)?; |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3487 | let type_name = cc_type_name_for_item(item, ir)?; |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3488 | Ok(quote! {#const_fragment #type_name}) |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 3489 | } |
| 3490 | } |
| 3491 | |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3492 | fn cc_struct_layout_assertion(record: &Record, ir: &IR) -> Result<TokenStream> { |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 3493 | if !ir.is_current_target(&record.owning_target) && !ir.is_stdlib_target(&record.owning_target) { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3494 | return Ok(quote! {}); |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 3495 | } |
Lukasz Anforowicz | 4c3a2cc | 2022-03-11 00:24:49 +0000 | [diff] [blame] | 3496 | let record_ident = format_cc_ident(&record.cc_name); |
Lukasz Anforowicz | 92be29f | 2022-09-02 17:12:29 -0700 | [diff] [blame] | 3497 | let namespace_qualifier = NamespaceQualifier::new(record.id, ir)?.format_for_cc(); |
Kinuko Yasuda | 54b75d7 | 2022-08-18 10:09:54 -0700 | [diff] [blame] | 3498 | let cc_size = Literal::usize_unsuffixed(record.original_cc_size); |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 3499 | let alignment = Literal::usize_unsuffixed(record.alignment); |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 3500 | let tag_kind = cc_tag_kind(record); |
Devin Jeanpierre | 190b90a | 2022-05-24 06:00:34 -0700 | [diff] [blame] | 3501 | let field_assertions = record |
| 3502 | .fields |
| 3503 | .iter() |
| 3504 | .filter(|f| f.access == AccessSpecifier::Public && f.identifier.is_some()) |
| 3505 | // https://en.cppreference.com/w/cpp/types/offsetof points out that "if member is [...] |
| 3506 | // a bit-field [...] the behavior [of `offsetof` macro] is undefined.". In such |
| 3507 | // scenario clang reports an error: cannot compute offset of bit-field 'field_name'. |
| 3508 | .filter(|f| !f.is_bitfield) |
| 3509 | .map(|field| { |
| 3510 | // The IR contains the offset in bits, while `CRUBIT_OFFSET_OF` returns the |
| 3511 | // offset in bytes, so we need to convert. We can assert that |
| 3512 | // `field.offset` is always at field boundaries, because the |
| 3513 | // bitfields have been filtered out earlier. |
| 3514 | assert_eq!(field.offset % 8, 0); |
| 3515 | let expected_offset = Literal::usize_unsuffixed(field.offset / 8); |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 3516 | |
Devin Jeanpierre | 190b90a | 2022-05-24 06:00:34 -0700 | [diff] [blame] | 3517 | let field_ident = format_cc_ident(&field.identifier.as_ref().unwrap().identifier); |
| 3518 | let actual_offset = quote! { |
| 3519 | CRUBIT_OFFSET_OF(#field_ident, #tag_kind #namespace_qualifier #record_ident) |
| 3520 | }; |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 3521 | |
Devin Jeanpierre | 190b90a | 2022-05-24 06:00:34 -0700 | [diff] [blame] | 3522 | quote! { static_assert( #actual_offset == #expected_offset); } |
| 3523 | }); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3524 | Ok(quote! { |
Kinuko Yasuda | 54b75d7 | 2022-08-18 10:09:54 -0700 | [diff] [blame] | 3525 | static_assert(sizeof(#tag_kind #namespace_qualifier #record_ident) == #cc_size); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3526 | static_assert(alignof(#tag_kind #namespace_qualifier #record_ident) == #alignment); |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 3527 | #( #field_assertions )* |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3528 | }) |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 3529 | } |
| 3530 | |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 3531 | // Returns the accessor functions for no_unique_address member variables. |
Devin Jeanpierre | 45b0196 | 2022-07-07 06:12:11 -0700 | [diff] [blame] | 3532 | fn cc_struct_no_unique_address_impl(db: &Database, record: &Record) -> Result<TokenStream> { |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 3533 | let mut fields = vec![]; |
| 3534 | let mut types = vec![]; |
| 3535 | for field in &record.fields { |
| 3536 | if field.access != AccessSpecifier::Public || !field.is_no_unique_address { |
| 3537 | continue; |
| 3538 | } |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 3539 | // Can't use `get_field_rs_type_for_layout` here, because we want to dig into |
| 3540 | // no_unique_address fields, despite laying them out as opaque blobs of bytes. |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 3541 | if let Ok(rs_type) = field.type_.as_ref().map(|t| t.rs_type.clone()) { |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 3542 | fields.push(make_rs_ident( |
| 3543 | &field |
| 3544 | .identifier |
| 3545 | .as_ref() |
| 3546 | .expect("Unnamed fields can't be annotated with [[no_unique_address]]") |
| 3547 | .identifier, |
| 3548 | )); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 3549 | types.push(db.rs_type_kind(rs_type).with_context(|| { |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 3550 | format!("Failed to format type for field {:?} on record {:?}", field, record) |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 3551 | })?); |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 3552 | } |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | if fields.is_empty() { |
| 3556 | return Ok(quote! {}); |
| 3557 | } |
| 3558 | |
Lukasz Anforowicz | 4c3a2cc | 2022-03-11 00:24:49 +0000 | [diff] [blame] | 3559 | let ident = make_rs_ident(&record.rs_name); |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 3560 | Ok(quote! { |
| 3561 | impl #ident { |
| 3562 | #( |
| 3563 | pub fn #fields(&self) -> &#types { |
| 3564 | unsafe {&* (&self.#fields as *const _ as *const #types)} |
| 3565 | } |
| 3566 | )* |
| 3567 | } |
| 3568 | }) |
| 3569 | } |
| 3570 | |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3571 | /// Returns the implementation of base class conversions, for converting a type |
| 3572 | /// to its unambiguous public base classes. |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3573 | fn cc_struct_upcast_impl(record: &Rc<Record>, ir: &IR) -> Result<GeneratedItem> { |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3574 | let mut impls = Vec::with_capacity(record.unambiguous_public_bases.len()); |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3575 | let mut thunks = vec![]; |
| 3576 | let mut cc_impls = vec![]; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3577 | for base in &record.unambiguous_public_bases { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 3578 | let base_record: &Rc<Record> = ir |
Lukasz Anforowicz | 4404725 | 2022-03-23 13:04:48 +0000 | [diff] [blame] | 3579 | .find_decl(base.base_record_id) |
| 3580 | .with_context(|| format!("Can't find a base record of {:?}", record))?; |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 3581 | let base_name = RsTypeKind::new_record(base_record.clone(), ir)?.into_token_stream(); |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3582 | let derived_name = RsTypeKind::new_record(record.clone(), ir)?.into_token_stream(); |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3583 | let body; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3584 | if let Some(offset) = base.offset { |
| 3585 | let offset = Literal::i64_unsuffixed(offset); |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 3586 | body = quote! {(derived as *const _ as *const u8).offset(#offset) as *const #base_name}; |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3587 | } else { |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3588 | let cast_fn_name = make_rs_ident(&format!( |
| 3589 | "__crubit_dynamic_upcast__{}__to__{}", |
Lukasz Anforowicz | 3f13397 | 2022-09-01 09:01:02 -0700 | [diff] [blame] | 3590 | record.mangled_cc_name, base_record.mangled_cc_name |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3591 | )); |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 3592 | let base_cc_name = cc_type_name_for_record(base_record.as_ref(), ir)?; |
| 3593 | let derived_cc_name = cc_type_name_for_record(record.as_ref(), ir)?; |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3594 | cc_impls.push(quote! { |
| 3595 | extern "C" const #base_cc_name& #cast_fn_name(const #derived_cc_name& from) { |
| 3596 | return from; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3597 | } |
| 3598 | }); |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3599 | thunks.push(quote! { |
| 3600 | pub fn #cast_fn_name (from: *const #derived_name) -> *const #base_name; |
| 3601 | }); |
| 3602 | body = quote! { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 3603 | crate::detail::#cast_fn_name(derived) |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3604 | }; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3605 | } |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3606 | impls.push(quote! { |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 3607 | unsafe impl oops::Inherits<#base_name> for #derived_name { |
| 3608 | unsafe fn upcast_ptr(derived: *const Self) -> *const #base_name { |
| 3609 | #body |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3610 | } |
| 3611 | } |
| 3612 | }); |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3613 | } |
| 3614 | |
Devin Jeanpierre | 8763a8e | 2022-04-26 15:45:13 -0700 | [diff] [blame] | 3615 | Ok(GeneratedItem { |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 3616 | item: quote! {#(#impls)*}, |
| 3617 | thunks: quote! {#(#thunks)*}, |
| 3618 | thunk_impls: quote! {#(#cc_impls)*}, |
Devin Jeanpierre | 8763a8e | 2022-04-26 15:45:13 -0700 | [diff] [blame] | 3619 | ..Default::default() |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 3620 | }) |
| 3621 | } |
| 3622 | |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 3623 | fn thunk_ident(func: &Func) -> Ident { |
| 3624 | format_ident!("__rust_thunk__{}", func.mangled_name) |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3627 | fn generate_rs_api_impl(db: &mut Database, crubit_support_path: &str) -> Result<TokenStream> { |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 3628 | // This function uses quote! to generate C++ source code out of convenience. |
| 3629 | // This is a bold idea so we have to continously evaluate if it still makes |
| 3630 | // sense or the cost of working around differences in Rust and C++ tokens is |
| 3631 | // greather than the value added. |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3632 | // |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 3633 | // See rs_bindings_from_cc/ |
| 3634 | // token_stream_printer.rs for a list of supported placeholders. |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3635 | let mut thunks = vec![]; |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3636 | let ir = db.ir(); |
Michael Forster | 7ef8073 | 2021-10-01 18:12:19 +0000 | [diff] [blame] | 3637 | for func in ir.functions() { |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3638 | if can_skip_cc_thunk(db, func) { |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3639 | continue; |
| 3640 | } |
Devin Jeanpierre | d5a5362 | 2022-10-04 22:00:02 -0700 | [diff] [blame] | 3641 | match db.generate_func(func.clone()).unwrap_or_default() { |
| 3642 | None => { |
| 3643 | // No function was generated that will call this thunk. |
| 3644 | continue; |
| 3645 | } |
| 3646 | Some(generated) => { |
| 3647 | let (_, _, function_id) = &*generated; |
| 3648 | // TODO(jeanpierreda): this should be moved into can_skip_cc_thunk, but that'd be |
| 3649 | // cyclic right now, because overloaded_funcs calls generate_func calls |
| 3650 | // can_skip_cc_thunk. We probably need to break generate_func apart. |
| 3651 | if db.overloaded_funcs().contains(function_id) { |
| 3652 | continue; |
| 3653 | } |
| 3654 | } |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 3655 | } |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3656 | |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 3657 | let thunk_ident = thunk_ident(func); |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 3658 | let implementation_function = match &func.name { |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 3659 | UnqualifiedIdentifier::Operator(op) => { |
| 3660 | let name = syn::parse_str::<TokenStream>(&op.name)?; |
| 3661 | quote! { operator #name } |
| 3662 | } |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 3663 | UnqualifiedIdentifier::Identifier(id) => { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 3664 | let fn_ident = format_cc_ident(&id.identifier); |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 3665 | match func.member_func_metadata.as_ref() { |
Lukasz Anforowicz | aab8ad2 | 2021-12-19 20:29:26 +0000 | [diff] [blame] | 3666 | Some(meta) => { |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 3667 | if let Some(_) = meta.instance_method_metadata { |
| 3668 | quote! { #fn_ident } |
| 3669 | } else { |
Devin Jeanpierre | ab85d44 | 2022-06-29 19:16:41 -0700 | [diff] [blame] | 3670 | let record: &Rc<Record> = ir.find_decl(meta.record_id)?; |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 3671 | let record_ident = format_cc_ident(&record.cc_name); |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 3672 | let namespace_qualifier = |
| 3673 | NamespaceQualifier::new(record.id, &ir)?.format_for_cc(); |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3674 | quote! { #namespace_qualifier #record_ident :: #fn_ident } |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 3675 | } |
| 3676 | } |
| 3677 | None => { |
Devin Jeanpierre | 781eeb1 | 2022-09-06 19:23:25 -0700 | [diff] [blame] | 3678 | let namespace_qualifier = |
| 3679 | NamespaceQualifier::new(func.id, &ir)?.format_for_cc(); |
Lukasz Anforowicz | 4e2e016 | 2022-09-01 07:07:32 -0700 | [diff] [blame] | 3680 | quote! { #namespace_qualifier #fn_ident } |
Lukasz Anforowicz | aab8ad2 | 2021-12-19 20:29:26 +0000 | [diff] [blame] | 3681 | } |
| 3682 | } |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 3683 | } |
Lukasz Anforowicz | 7b0042d | 2022-01-06 23:00:19 +0000 | [diff] [blame] | 3684 | // 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] | 3685 | // use the name of the type itself, without namespace qualification, template |
| 3686 | // parameters, or aliases. We do not need to use that naming scheme anywhere else in |
| 3687 | // the bindings, and it can be difficult (impossible?) to spell in the general case. By |
| 3688 | // 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] | 3689 | // is. Similar arguments apply to `construct_at`. |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 3690 | UnqualifiedIdentifier::Constructor => { |
Lukasz Anforowicz | 2317154 | 2022-04-11 15:26:17 -0700 | [diff] [blame] | 3691 | quote! { crubit::construct_at } |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 3692 | } |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 3693 | UnqualifiedIdentifier::Destructor => quote! {std::destroy_at}, |
Devin Jeanpierre | f2ec871 | 2021-10-13 20:47:16 +0000 | [diff] [blame] | 3694 | }; |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3695 | |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3696 | let mut param_idents = |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 3697 | 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] | 3698 | |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3699 | let mut param_types = func |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 3700 | .params |
| 3701 | .iter() |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 3702 | .map(|p| { |
| 3703 | let formatted = format_cc_type(&p.type_.cc_type, &ir)?; |
| 3704 | if !db.rs_type_kind(p.type_.rs_type.clone())?.is_unpin() { |
| 3705 | // non-Unpin types are wrapped by a pointer in the thunk. |
| 3706 | Ok(quote! {#formatted *}) |
| 3707 | } else { |
| 3708 | Ok(formatted) |
| 3709 | } |
| 3710 | }) |
Devin Jeanpierre | 09c6f45 | 2021-09-29 07:34:24 +0000 | [diff] [blame] | 3711 | .collect::<Result<Vec<_>>>()?; |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3712 | |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 3713 | let arg_expressions = func |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 3714 | .params |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3715 | .iter() |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 3716 | .map(|p| { |
| 3717 | let ident = format_cc_ident(&p.identifier.identifier); |
| 3718 | match p.type_.cc_type.name.as_deref() { |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 3719 | Some("&") => Ok(quote! { * #ident }), |
| 3720 | Some("&&") => Ok(quote! { std::move(* #ident) }), |
| 3721 | _ => { |
| 3722 | // non-Unpin types are wrapped by a pointer in the thunk. |
| 3723 | if !db.rs_type_kind(p.type_.rs_type.clone())?.is_unpin() { |
| 3724 | Ok(quote! { std::move(* #ident) }) |
| 3725 | } else { |
| 3726 | Ok(quote! { #ident }) |
| 3727 | } |
| 3728 | } |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 3729 | } |
| 3730 | }) |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 3731 | .collect::<Result<Vec<_>>>()?; |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3732 | |
| 3733 | // Here, we add a __return parameter if the return type is not trivially |
| 3734 | // relocatable. (We do this after the arg_expressions computation, so |
| 3735 | // that it's only in the parameter list, not the argument list.) |
| 3736 | // |
| 3737 | // RsTypeKind is where, as much as anywhere, where the information about trivial |
| 3738 | // relocatability is stored. |
| 3739 | let is_trivial_return = db.rs_type_kind(func.return_type.rs_type.clone())?.is_unpin(); |
| 3740 | let mut return_type_name = format_cc_type(&func.return_type.cc_type, &ir)?; |
| 3741 | if !is_trivial_return { |
| 3742 | param_idents.insert(0, format_cc_ident("__return")); |
| 3743 | param_types.insert(0, quote! {#return_type_name *}); |
| 3744 | return_type_name = quote! {void}; |
| 3745 | } |
| 3746 | |
Felipe de A. Mello Pereira | 7baec6e | 2022-10-05 02:45:13 -0700 | [diff] [blame] | 3747 | let this_ref_qualification = |
| 3748 | func.member_func_metadata.as_ref().and_then(|meta| match &func.name { |
Devin Jeanpierre | 435d71f | 2022-10-04 22:18:50 -0700 | [diff] [blame] | 3749 | UnqualifiedIdentifier::Constructor | UnqualifiedIdentifier::Destructor => None, |
Felipe de A. Mello Pereira | 7baec6e | 2022-10-05 02:45:13 -0700 | [diff] [blame] | 3750 | UnqualifiedIdentifier::Identifier(_) | UnqualifiedIdentifier::Operator(_) => meta |
| 3751 | .instance_method_metadata |
| 3752 | .as_ref() |
| 3753 | .map(|instance_method| instance_method.reference), |
| 3754 | }); |
| 3755 | let (implementation_function, arg_expressions) = |
| 3756 | if let Some(this_ref_qualification) = this_ref_qualification { |
| 3757 | let this_param = func |
| 3758 | .params |
| 3759 | .first() |
| 3760 | .ok_or_else(|| anyhow!("Instance methods must have `__this` param."))?; |
Devin Jeanpierre | 435d71f | 2022-10-04 22:18:50 -0700 | [diff] [blame] | 3761 | |
Felipe de A. Mello Pereira | 7baec6e | 2022-10-05 02:45:13 -0700 | [diff] [blame] | 3762 | let this_arg = format_cc_ident(&this_param.identifier.identifier); |
| 3763 | let this_dot = if this_ref_qualification == ir::ReferenceQualification::RValue { |
| 3764 | quote! {std::move(*#this_arg).} |
| 3765 | } else { |
| 3766 | quote! {#this_arg->} |
| 3767 | }; |
| 3768 | ( |
| 3769 | quote! { #this_dot #implementation_function}, |
| 3770 | arg_expressions.iter().skip(1).cloned().collect_vec(), |
| 3771 | ) |
Devin Jeanpierre | 435d71f | 2022-10-04 22:18:50 -0700 | [diff] [blame] | 3772 | } else { |
Felipe de A. Mello Pereira | 7baec6e | 2022-10-05 02:45:13 -0700 | [diff] [blame] | 3773 | (implementation_function, arg_expressions.clone()) |
Devin Jeanpierre | 435d71f | 2022-10-04 22:18:50 -0700 | [diff] [blame] | 3774 | }; |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 3775 | |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3776 | let return_expr = quote! {#implementation_function( #( #arg_expressions ),* )}; |
| 3777 | let return_stmt = if !is_trivial_return { |
Devin Jeanpierre | 521b4ee | 2022-07-27 07:19:50 -0700 | [diff] [blame] | 3778 | // Explicitly use placement new so that we get guaranteed copy elision in C++17. |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3779 | let out_param = ¶m_idents[0]; |
Devin Jeanpierre | 521b4ee | 2022-07-27 07:19:50 -0700 | [diff] [blame] | 3780 | quote! {new(#out_param) auto(#return_expr)} |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3781 | } else { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 3782 | match func.return_type.cc_type.name.as_deref() { |
| 3783 | Some("void") => return_expr, |
| 3784 | Some("&") => quote! { return & #return_expr }, |
| 3785 | Some("&&") => { |
| 3786 | // The code below replicates bits of `format_cc_type`, but formats an rvalue |
| 3787 | // reference (which `format_cc_type` would format as a pointer). |
| 3788 | // `const_fragment` from `format_cc_type` is ignored - it is not applicable for |
| 3789 | // references. |
| 3790 | let ty = &func.return_type.cc_type; |
| 3791 | if ty.type_args.len() != 1 { |
| 3792 | bail!("Invalid reference type (need exactly 1 type argument): {:?}", ty); |
| 3793 | } |
| 3794 | let nested_type = format_cc_type(&ty.type_args[0], &ir)?; |
| 3795 | quote! { |
| 3796 | #nested_type && lvalue = #return_expr; |
| 3797 | return &lvalue |
| 3798 | } |
Devin Jeanpierre | fd4ff82 | 2022-07-15 01:43:55 -0700 | [diff] [blame] | 3799 | } |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 3800 | _ => quote! { return #return_expr }, |
| 3801 | } |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3802 | }; |
| 3803 | |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3804 | thunks.push(quote! { |
| 3805 | extern "C" #return_type_name #thunk_ident( #( #param_types #param_idents ),* ) { |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3806 | #return_stmt; |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3807 | } |
| 3808 | }); |
| 3809 | } |
| 3810 | |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3811 | let layout_assertions = ir |
| 3812 | .records() |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3813 | .map(|record| cc_struct_layout_assertion(record, &ir)) |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 3814 | .collect::<Result<Vec<_>>>()?; |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 3815 | |
Lukasz Anforowicz | 434c469 | 2022-11-01 14:05:24 -0700 | [diff] [blame] | 3816 | let mut internal_includes = BTreeSet::new(); |
| 3817 | internal_includes.insert(CcInclude::memory()); // ubiquitous. |
Devin Jeanpierre | 231ef8d | 2021-10-27 10:50:44 +0000 | [diff] [blame] | 3818 | if ir.records().next().is_some() { |
Lukasz Anforowicz | 434c469 | 2022-11-01 14:05:24 -0700 | [diff] [blame] | 3819 | internal_includes.insert(CcInclude::cstddef()); |
Devin Jeanpierre | 231ef8d | 2021-10-27 10:50:44 +0000 | [diff] [blame] | 3820 | }; |
Lukasz Anforowicz | 434c469 | 2022-11-01 14:05:24 -0700 | [diff] [blame] | 3821 | for crubit_header in ["cxx20_backports.h", "offsetof.h"] { |
| 3822 | internal_includes.insert(CcInclude::user_header( |
| 3823 | format!("{crubit_support_path}/{crubit_header}").into(), |
| 3824 | )); |
| 3825 | } |
| 3826 | let internal_includes = format_cc_includes(&internal_includes); |
Lukasz Anforowicz | 4457baf | 2021-12-23 17:24:04 +0000 | [diff] [blame] | 3827 | |
Michael Forster | bee8448 | 2021-10-13 08:35:38 +0000 | [diff] [blame] | 3828 | // In order to generate C++ thunk in all the cases Clang needs to be able to |
Lukasz Anforowicz | 434c469 | 2022-11-01 14:05:24 -0700 | [diff] [blame] | 3829 | // access declarations from public headers of the C++ library. We don't |
| 3830 | // process these includes via `format_cc_includes` to preserve their |
| 3831 | // original order (some libraries require certain headers to be included |
| 3832 | // first - e.g. `config.h`). |
| 3833 | let ir_includes = |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 3834 | ir.public_headers().map(|hdr| CcInclude::user_header(hdr.name.clone())).collect_vec(); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3835 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3836 | Ok(quote! { |
Lukasz Anforowicz | 434c469 | 2022-11-01 14:05:24 -0700 | [diff] [blame] | 3837 | #internal_includes |
Lukasz Anforowicz | 121338a | 2022-11-01 14:28:32 -0700 | [diff] [blame] | 3838 | __NEWLINE__ |
| 3839 | __COMMENT__ "Public headers of the C++ library being wrapped." |
Lukasz Anforowicz | 434c469 | 2022-11-01 14:05:24 -0700 | [diff] [blame] | 3840 | #( #ir_includes )* __NEWLINE__ |
Marcel Hlopko | b8069ae | 2022-02-19 09:31:00 +0000 | [diff] [blame] | 3841 | __HASH_TOKEN__ pragma clang diagnostic push __NEWLINE__ |
| 3842 | // Disable Clang thread-safety-analysis warnings that would otherwise |
| 3843 | // complain about thunks that call mutex locking functions in an unpaired way. |
| 3844 | __HASH_TOKEN__ pragma clang diagnostic ignored "-Wthread-safety-analysis" __NEWLINE__ |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3845 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3846 | #( #thunks )* __NEWLINE__ __NEWLINE__ |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 3847 | |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3848 | #( #layout_assertions __NEWLINE__ __NEWLINE__ )* |
Marcel Hlopko | c6b726c | 2021-10-07 06:53:09 +0000 | [diff] [blame] | 3849 | |
Marcel Hlopko | b8069ae | 2022-02-19 09:31:00 +0000 | [diff] [blame] | 3850 | __NEWLINE__ |
| 3851 | __HASH_TOKEN__ pragma clang diagnostic pop __NEWLINE__ |
Marcel Hlopko | c6b726c | 2021-10-07 06:53:09 +0000 | [diff] [blame] | 3852 | // To satisfy http://cs/symbol:devtools.metadata.Presubmit.CheckTerminatingNewline check. |
| 3853 | __NEWLINE__ |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3854 | }) |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3855 | } |
| 3856 | |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 3857 | #[cfg(test)] |
| 3858 | mod tests { |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 3859 | use super::*; |
Lukasz Anforowicz | efd635d | 2022-09-22 10:12:49 -0700 | [diff] [blame] | 3860 | use ir_matchers::assert_ir_matches; |
Lukasz Anforowicz | edabf00 | 2022-04-28 06:45:09 -0700 | [diff] [blame] | 3861 | use ir_testing::{ |
| 3862 | ir_from_cc, ir_from_cc_dependency, ir_record, make_ir_from_items, retrieve_func, |
Martin Brænne | e5ba6b6 | 2022-06-23 07:38:40 -0700 | [diff] [blame] | 3863 | with_lifetime_macros, |
Lukasz Anforowicz | edabf00 | 2022-04-28 06:45:09 -0700 | [diff] [blame] | 3864 | }; |
Lukasz Anforowicz | b4d1778 | 2022-07-07 08:09:13 -0700 | [diff] [blame] | 3865 | use static_assertions::{assert_impl_all, assert_not_impl_any}; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3866 | use token_stream_matchers::{ |
Lukasz Anforowicz | efd635d | 2022-09-22 10:12:49 -0700 | [diff] [blame] | 3867 | 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] | 3868 | }; |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 3869 | use token_stream_printer::rs_tokens_to_formatted_string_for_tests; |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 3870 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 3871 | fn generate_bindings_tokens(ir: Rc<IR>) -> Result<BindingsTokens> { |
Michael VanBemmel | f5cbdf4 | 2022-10-14 17:00:11 -0700 | [diff] [blame] | 3872 | super::generate_bindings_tokens(ir, "crubit/rs_bindings_support", &mut IgnoreErrors) |
Lukasz Anforowicz | dd90770 | 2022-05-06 09:24:07 -0700 | [diff] [blame] | 3873 | } |
| 3874 | |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 3875 | fn db_from_cc(cc_src: &str) -> Result<Database> { |
| 3876 | let mut db = Database::default(); |
| 3877 | db.set_ir(ir_from_cc(cc_src)?); |
| 3878 | Ok(db) |
| 3879 | } |
| 3880 | |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 3881 | #[test] |
Marcel Hlopko | b8069ae | 2022-02-19 09:31:00 +0000 | [diff] [blame] | 3882 | fn test_disable_thread_safety_warnings() -> Result<()> { |
| 3883 | let ir = ir_from_cc("inline void foo() {}")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 3884 | let rs_api_impl = generate_bindings_tokens(ir)?.rs_api_impl; |
Marcel Hlopko | b8069ae | 2022-02-19 09:31:00 +0000 | [diff] [blame] | 3885 | assert_cc_matches!( |
| 3886 | rs_api_impl, |
| 3887 | quote! { |
| 3888 | ... |
| 3889 | __HASH_TOKEN__ pragma clang diagnostic push |
| 3890 | __HASH_TOKEN__ pragma clang diagnostic ignored "-Wthread-safety-analysis" |
| 3891 | ... |
| 3892 | |
| 3893 | __HASH_TOKEN__ pragma clang diagnostic pop |
| 3894 | ... |
| 3895 | } |
| 3896 | ); |
| 3897 | Ok(()) |
| 3898 | } |
| 3899 | |
| 3900 | #[test] |
Marcel Hlopko | 3b9bf9e | 2021-11-29 08:25:14 +0000 | [diff] [blame] | 3901 | // TODO(hlopko): Move this test to a more principled place where it can access |
| 3902 | // `ir_testing`. |
| 3903 | fn test_duplicate_decl_ids_err() { |
| 3904 | let mut r1 = ir_record("R1"); |
Lukasz Anforowicz | 14732b2 | 2022-06-02 09:11:08 -0700 | [diff] [blame] | 3905 | r1.id = ItemId::new_for_testing(42); |
Marcel Hlopko | 3b9bf9e | 2021-11-29 08:25:14 +0000 | [diff] [blame] | 3906 | let mut r2 = ir_record("R2"); |
Lukasz Anforowicz | 14732b2 | 2022-06-02 09:11:08 -0700 | [diff] [blame] | 3907 | r2.id = ItemId::new_for_testing(42); |
Marcel Hlopko | 3b9bf9e | 2021-11-29 08:25:14 +0000 | [diff] [blame] | 3908 | let result = make_ir_from_items([r1.into(), r2.into()]); |
| 3909 | assert!(result.is_err()); |
| 3910 | assert!(result.unwrap_err().to_string().contains("Duplicate decl_id found in")); |
| 3911 | } |
| 3912 | |
| 3913 | #[test] |
Marcel Hlopko | 45fba97 | 2021-08-23 19:52:20 +0000 | [diff] [blame] | 3914 | fn test_simple_function() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3915 | let ir = ir_from_cc("int Add(int a, int b);")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 3916 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3917 | assert_rs_matches!( |
| 3918 | rs_api, |
| 3919 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3920 | #[inline(always)] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3921 | pub fn Add(a: i32, b: i32) -> i32 { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 3922 | unsafe { crate::detail::__rust_thunk___Z3Addii(a, b) } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | ); |
| 3926 | assert_rs_matches!( |
| 3927 | rs_api, |
| 3928 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3929 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 3930 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 3931 | use super::*; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3932 | extern "C" { |
| 3933 | #[link_name = "_Z3Addii"] |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 3934 | pub(crate) fn __rust_thunk___Z3Addii(a: i32, b: i32) -> i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 3938 | ); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3939 | |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 3940 | assert_cc_not_matches!(rs_api_impl, quote! {__rust_thunk___Z3Addii}); |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3941 | |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3942 | Ok(()) |
| 3943 | } |
| 3944 | |
| 3945 | #[test] |
| 3946 | fn test_inline_function() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3947 | let ir = ir_from_cc("inline int Add(int a, int b);")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 3948 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3949 | assert_rs_matches!( |
| 3950 | rs_api, |
| 3951 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3952 | #[inline(always)] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3953 | pub fn Add(a: i32, b: i32) -> i32 { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 3954 | unsafe { crate::detail::__rust_thunk___Z3Addii(a, b) } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3955 | } |
| 3956 | } |
| 3957 | ); |
| 3958 | assert_rs_matches!( |
| 3959 | rs_api, |
| 3960 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3961 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 3962 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 3963 | use super::*; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 3964 | extern "C" { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 3965 | pub(crate) fn __rust_thunk___Z3Addii(a: i32, b: i32) -> i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3966 | } |
| 3967 | } |
| 3968 | } |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3969 | ); |
| 3970 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3971 | assert_cc_matches!( |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 3972 | rs_api_impl, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3973 | quote! { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 3974 | extern "C" int __rust_thunk___Z3Addii(int a, int b) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 3975 | return Add(a, b); |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3976 | } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3977 | } |
Marcel Hlopko | 3164eee | 2021-08-24 20:09:22 +0000 | [diff] [blame] | 3978 | ); |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 3979 | Ok(()) |
| 3980 | } |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 3981 | |
| 3982 | #[test] |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 3983 | fn test_simple_function_with_types_from_other_target() -> Result<()> { |
| 3984 | let ir = ir_from_cc_dependency( |
| 3985 | "inline ReturnStruct DoSomething(ParamStruct param);", |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 3986 | "struct ReturnStruct final {}; struct ParamStruct final {};", |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 3987 | )?; |
| 3988 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 3989 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3990 | assert_rs_matches!( |
| 3991 | rs_api, |
| 3992 | quote! { |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 3993 | #[inline(always)] |
| 3994 | pub fn DoSomething(param: dependency::ParamStruct) |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3995 | -> dependency::ReturnStruct { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 3996 | unsafe { crate::detail::__rust_thunk___Z11DoSomething11ParamStruct(param) } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | ); |
| 4000 | assert_rs_matches!( |
| 4001 | rs_api, |
| 4002 | quote! { |
| 4003 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 4004 | #[allow(unused_imports)] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4005 | use super::*; |
| 4006 | extern "C" { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4007 | pub(crate) fn __rust_thunk___Z11DoSomething11ParamStruct( |
| 4008 | param: dependency::ParamStruct) -> dependency::ReturnStruct; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4009 | } |
| 4010 | }} |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 4011 | ); |
| 4012 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4013 | assert_cc_matches!( |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 4014 | rs_api_impl, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4015 | quote! { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4016 | extern "C" struct ReturnStruct __rust_thunk___Z11DoSomething11ParamStruct( |
| 4017 | struct ParamStruct param) { |
| 4018 | return DoSomething(param); |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 4019 | } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4020 | } |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 4021 | ); |
| 4022 | Ok(()) |
| 4023 | } |
| 4024 | |
| 4025 | #[test] |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4026 | fn test_template_in_dependency_and_alias_in_current_target() -> Result<()> { |
| 4027 | // See also the test with the same name in `ir_from_cc_test.rs`. |
| 4028 | let ir = { |
| 4029 | let dependency_src = r#" #pragma clang lifetime_elision |
| 4030 | template <typename T> |
| 4031 | struct MyTemplate { |
| 4032 | T GetValue() { return field; } |
| 4033 | T field; |
| 4034 | }; "#; |
| 4035 | let current_target_src = r#" #pragma clang lifetime_elision |
| 4036 | using MyAliasOfTemplate = MyTemplate<int>; "#; |
| 4037 | ir_from_cc_dependency(current_target_src, dependency_src)? |
| 4038 | }; |
| 4039 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4040 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4041 | assert_rs_matches!( |
| 4042 | rs_api, |
| 4043 | quote! { |
| 4044 | #[repr(C)] |
| 4045 | pub struct __CcTemplateInst10MyTemplateIiE { |
| 4046 | pub field: i32, |
| 4047 | } |
| 4048 | } |
| 4049 | ); |
| 4050 | assert_rs_matches!( |
| 4051 | rs_api, |
| 4052 | quote! { |
| 4053 | impl __CcTemplateInst10MyTemplateIiE { |
| 4054 | #[inline(always)] |
| 4055 | pub fn GetValue<'a>(self: ... Pin<&'a mut Self>) -> i32 { unsafe { |
Lukasz Anforowicz | 8d1e432 | 2022-06-08 07:56:06 -0700 | [diff] [blame] | 4056 | crate::detail::__rust_thunk___ZN10MyTemplateIiE8GetValueEv__2f_2ftest_3atesting_5ftarget( |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4057 | self) |
| 4058 | }} |
| 4059 | } |
| 4060 | } |
| 4061 | ); |
| 4062 | assert_rs_matches!( |
| 4063 | rs_api, |
| 4064 | quote! { |
| 4065 | pub type MyAliasOfTemplate = crate::__CcTemplateInst10MyTemplateIiE; |
| 4066 | } |
| 4067 | ); |
| 4068 | assert_rs_matches!( |
| 4069 | rs_api, |
| 4070 | quote! { |
Lukasz Anforowicz | 8d1e432 | 2022-06-08 07:56:06 -0700 | [diff] [blame] | 4071 | mod detail { ... extern "C" { |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4072 | ... |
Lukasz Anforowicz | 8d1e432 | 2022-06-08 07:56:06 -0700 | [diff] [blame] | 4073 | pub(crate) fn |
| 4074 | __rust_thunk___ZN10MyTemplateIiE8GetValueEv__2f_2ftest_3atesting_5ftarget<'a>( |
| 4075 | __this: ... Pin<&'a mut crate::__CcTemplateInst10MyTemplateIiE> |
| 4076 | ) -> i32; |
| 4077 | ... |
| 4078 | } } |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4079 | } |
| 4080 | ); |
| 4081 | assert_cc_matches!( |
| 4082 | rs_api_impl, |
| 4083 | quote! { |
Lukasz Anforowicz | 8d1e432 | 2022-06-08 07:56:06 -0700 | [diff] [blame] | 4084 | extern "C" |
| 4085 | int __rust_thunk___ZN10MyTemplateIiE8GetValueEv__2f_2ftest_3atesting_5ftarget( |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4086 | struct MyTemplate<int>* __this) { |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4087 | return __this->GetValue(); |
| 4088 | } |
| 4089 | } |
| 4090 | ); |
| 4091 | |
| 4092 | Ok(()) |
| 4093 | } |
| 4094 | |
| 4095 | #[test] |
| 4096 | fn test_template_with_out_of_line_definition() -> Result<()> { |
| 4097 | // See also an end-to-end test in the `test/templates/out_of_line_definition` |
| 4098 | // directory. |
| 4099 | let ir = ir_from_cc( |
| 4100 | r#" #pragma clang lifetime_elision |
| 4101 | template <typename T> |
| 4102 | class MyTemplate final { |
| 4103 | public: |
| 4104 | static MyTemplate Create(T value); |
| 4105 | const T& value() const; |
| 4106 | |
| 4107 | private: |
| 4108 | T value_; |
| 4109 | }; |
| 4110 | |
| 4111 | using MyTypeAlias = MyTemplate<int>; "#, |
| 4112 | )?; |
| 4113 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4114 | let BindingsTokens { rs_api_impl, .. } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4115 | |
| 4116 | // Even though the member functions above are *not* defined inline (e.g. |
| 4117 | // IR::Func::is_inline is false), they still need to have thunks generated for |
| 4118 | // them (to force/guarantee that the class template and its members get |
| 4119 | // instantiated). This is also covered in the following end-to-end |
| 4120 | // tests: |
| 4121 | // - test/templates/out_of_line_definition/ - without a thunk, the template |
| 4122 | // won't be instantiated and Rust bindings won't be able to call the member |
| 4123 | // function (there will be no instantiation of the member function in the C++ |
| 4124 | // object files) |
| 4125 | // - test/templates/definition_in_cc/ - the instantiation happens in the .cc |
| 4126 | // file and therefore the thunk is not *required* (but it doesn't hurt to have |
| 4127 | // the thunk) |
| 4128 | assert_cc_matches!( |
| 4129 | rs_api_impl, |
| 4130 | quote! { |
| 4131 | extern "C" class MyTemplate<int> |
Lukasz Anforowicz | 8d1e432 | 2022-06-08 07:56:06 -0700 | [diff] [blame] | 4132 | __rust_thunk___ZN10MyTemplateIiE6CreateEi__2f_2ftest_3atesting_5ftarget( |
| 4133 | int value) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4134 | return MyTemplate<int>::Create(value); |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4135 | } |
| 4136 | } |
| 4137 | ); |
| 4138 | assert_cc_matches!( |
| 4139 | rs_api_impl, |
| 4140 | quote! { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4141 | extern "C" int const* |
Lukasz Anforowicz | 8d1e432 | 2022-06-08 07:56:06 -0700 | [diff] [blame] | 4142 | __rust_thunk___ZNK10MyTemplateIiE5valueEv__2f_2ftest_3atesting_5ftarget( |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4143 | const class MyTemplate<int>*__this) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4144 | return &__this->value(); |
Lukasz Anforowicz | b1ff2e5 | 2022-05-16 10:54:23 -0700 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | ); |
| 4148 | Ok(()) |
| 4149 | } |
| 4150 | |
| 4151 | #[test] |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 4152 | fn test_simple_struct() -> Result<()> { |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 4153 | let ir = ir_from_cc( |
| 4154 | r#" |
| 4155 | #pragma clang lifetime_elision |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 4156 | struct SomeStruct final { |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 4157 | ~SomeStruct() {} |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4158 | int public_int; |
| 4159 | protected: |
| 4160 | int protected_int; |
| 4161 | private: |
| 4162 | int private_int; |
| 4163 | }; |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 4164 | "#, |
| 4165 | )?; |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 4166 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4167 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4168 | assert_rs_matches!( |
| 4169 | rs_api, |
| 4170 | quote! { |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 4171 | #[::ctor::recursively_pinned(PinnedDrop)] |
Lukasz Anforowicz | df8fcae | 2022-06-02 14:54:43 -0700 | [diff] [blame] | 4172 | #[repr(C, align(4))] |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 4173 | pub struct SomeStruct { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4174 | __non_field_data: [::std::mem::MaybeUninit<u8>; 0], |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 4175 | pub public_int: i32, |
Lukasz Anforowicz | df8fcae | 2022-06-02 14:54:43 -0700 | [diff] [blame] | 4176 | #[doc = " Reason for representing this field as a blob of bytes:\n Types of non-public C++ fields can be elided away"] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4177 | pub(crate) protected_int: [::std::mem::MaybeUninit<u8>; 4], |
Lukasz Anforowicz | df8fcae | 2022-06-02 14:54:43 -0700 | [diff] [blame] | 4178 | #[doc = " Reason for representing this field as a blob of bytes:\n Types of non-public C++ fields can be elided away"] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4179 | pub(crate) private_int: [::std::mem::MaybeUninit<u8>; 4], |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4180 | } |
| 4181 | } |
| 4182 | ); |
| 4183 | assert_rs_matches!( |
| 4184 | rs_api, |
| 4185 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4186 | const _: () = assert!(::std::mem::size_of::<Option<&i32>>() == ::std::mem::size_of::<&i32>()); |
| 4187 | const _: () = assert!(::std::mem::size_of::<crate::SomeStruct>() == 12); |
| 4188 | const _: () = assert!(::std::mem::align_of::<crate::SomeStruct>() == 4); |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 4189 | const _: () = { static_assertions::assert_not_impl_any!(crate::SomeStruct: Copy); }; |
| 4190 | const _: () = { static_assertions::assert_impl_all!(crate::SomeStruct: Drop); }; |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 4191 | const _: () = assert!(memoffset::offset_of!(crate::SomeStruct, public_int) == 0); |
| 4192 | const _: () = assert!(memoffset::offset_of!(crate::SomeStruct, protected_int) == 4); |
| 4193 | const _: () = assert!(memoffset::offset_of!(crate::SomeStruct, private_int) == 8); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4194 | } |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 4195 | ); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4196 | assert_cc_matches!( |
| 4197 | rs_api_impl, |
| 4198 | quote! { |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4199 | extern "C" void __rust_thunk___ZN10SomeStructD1Ev(struct SomeStruct * __this) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4200 | std::destroy_at(__this); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | ); |
| 4204 | assert_cc_matches!( |
| 4205 | rs_api_impl, |
| 4206 | quote! { |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4207 | static_assert(sizeof(struct SomeStruct) == 12); |
| 4208 | static_assert(alignof(struct SomeStruct) == 4); |
| 4209 | static_assert(CRUBIT_OFFSET_OF(public_int, struct SomeStruct) == 0); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4210 | } |
Googler | 5ea8864 | 2021-09-29 08:05:59 +0000 | [diff] [blame] | 4211 | ); |
Marcel Hlopko | b4b2874 | 2021-09-15 12:45:20 +0000 | [diff] [blame] | 4212 | Ok(()) |
| 4213 | } |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 4214 | |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4215 | #[test] |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4216 | fn test_struct_vs_class() -> Result<()> { |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 4217 | let ir = ir_from_cc( |
| 4218 | r#" |
| 4219 | #pragma clang lifetime_elision |
| 4220 | struct SomeStruct final { |
| 4221 | SomeStruct() {} |
| 4222 | int field; |
| 4223 | }; |
| 4224 | class SomeClass final { |
| 4225 | public: |
| 4226 | SomeClass() {} |
| 4227 | int field; |
| 4228 | }; |
| 4229 | "#, |
| 4230 | )?; |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4231 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 4232 | |
| 4233 | // A Rust `struct` is generated for both `SomeStruct` and `SomeClass`. |
| 4234 | assert_rs_matches!(rs_api, quote! { pub struct SomeStruct },); |
| 4235 | assert_rs_matches!(rs_api, quote! { pub struct SomeClass },); |
| 4236 | |
| 4237 | // But in C++ we still should refer to `struct SomeStruct` and `class |
| 4238 | // SomeClass`. See also b/238212337. |
| 4239 | assert_cc_matches!(rs_api_impl, quote! { struct SomeStruct * __this }); |
| 4240 | assert_cc_matches!(rs_api_impl, quote! { class SomeClass * __this }); |
| 4241 | assert_cc_matches!(rs_api_impl, quote! { static_assert(sizeof(struct SomeStruct) == 4); }); |
| 4242 | assert_cc_matches!(rs_api_impl, quote! { static_assert(sizeof(class SomeClass) == 4); }); |
| 4243 | Ok(()) |
| 4244 | } |
| 4245 | |
| 4246 | #[test] |
Kinuko Yasuda | 8dd8464 | 2022-08-17 09:19:47 -0700 | [diff] [blame] | 4247 | fn test_struct_vs_typedefed_struct() -> Result<()> { |
| 4248 | let ir = ir_from_cc( |
| 4249 | r#" |
| 4250 | #pragma clang lifetime_elision |
| 4251 | struct SomeStruct final { |
| 4252 | int x; |
| 4253 | } __attribute__((aligned(16))); |
| 4254 | typedef struct { |
| 4255 | int x; |
| 4256 | } SomeAnonStruct __attribute__((aligned(16))); |
| 4257 | "#, |
| 4258 | )?; |
| 4259 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 4260 | |
| 4261 | // A `struct` is generated for both `SomeStruct` and `SomeAnonStruct`, both |
| 4262 | // in Rust and in C++. |
| 4263 | assert_rs_matches!(rs_api, quote! { pub struct SomeStruct },); |
| 4264 | assert_rs_matches!(rs_api, quote! { pub struct SomeAnonStruct },); |
| 4265 | assert_rs_matches!(rs_api_impl, quote! { struct SomeStruct * __this },); |
| 4266 | assert_rs_matches!(rs_api_impl, quote! { SomeAnonStruct * __this },); |
| 4267 | |
| 4268 | // In C++, both have align == 16, but size for `SomeAnonStruct` is not aligned. |
| 4269 | // `SomeAnonStruct` won't have `struct` in the assert. |
| 4270 | assert_cc_matches!( |
| 4271 | rs_api_impl, |
| 4272 | quote! { static_assert(alignof(struct SomeStruct) == 16); } |
| 4273 | ); |
| 4274 | assert_cc_matches!(rs_api_impl, quote! { static_assert(alignof(SomeAnonStruct) == 16); }); |
| 4275 | assert_cc_matches!(rs_api_impl, quote! { static_assert(sizeof(struct SomeStruct) == 16); }); |
| 4276 | assert_cc_matches!(rs_api_impl, quote! { static_assert(sizeof(SomeAnonStruct) == 4); }); |
| 4277 | |
| 4278 | // In Rust, both have align == 16 and size == 16. |
| 4279 | assert_rs_matches!( |
| 4280 | rs_api, |
| 4281 | quote! { assert!(::std::mem::size_of::<crate::SomeStruct>() == 16); } |
| 4282 | ); |
| 4283 | assert_rs_matches!( |
| 4284 | rs_api, |
| 4285 | quote! { assert!(::std::mem::align_of::<crate::SomeStruct>() == 16); } |
| 4286 | ); |
| 4287 | assert_rs_matches!( |
| 4288 | rs_api, |
| 4289 | quote! { assert!(::std::mem::size_of::<crate::SomeAnonStruct>() == 16); } |
| 4290 | ); |
| 4291 | assert_rs_matches!( |
| 4292 | rs_api, |
| 4293 | quote! { assert!(::std::mem::align_of::<crate::SomeAnonStruct>() == 16); } |
| 4294 | ); |
| 4295 | |
| 4296 | Ok(()) |
| 4297 | } |
| 4298 | |
| 4299 | #[test] |
Devin Jeanpierre | 5fecde5 | 2022-09-14 06:53:39 -0700 | [diff] [blame] | 4300 | fn test_typedef_member() -> Result<()> { |
| 4301 | let ir = ir_from_cc( |
| 4302 | r#" |
| 4303 | struct SomeStruct final { |
| 4304 | typedef int Type; |
| 4305 | }; |
| 4306 | inline SomeStruct::Type Function() {return 0;} |
| 4307 | "#, |
| 4308 | )?; |
| 4309 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 4310 | // TODO(b/200067824): This should use the alias's real name in Rust, as well. |
| 4311 | assert_rs_matches!(rs_api, quote! { pub fn Function() -> i32 { ... } },); |
| 4312 | |
| 4313 | assert_cc_matches!( |
| 4314 | rs_api_impl, |
| 4315 | quote! { |
| 4316 | extern "C" SomeStruct::Type __rust_thunk___Z8Functionv(){ return Function(); } |
| 4317 | }, |
| 4318 | ); |
| 4319 | Ok(()) |
| 4320 | } |
| 4321 | |
| 4322 | #[test] |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 4323 | fn test_ref_to_struct_in_thunk_impls() -> Result<()> { |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4324 | let ir = ir_from_cc("struct S{}; inline void foo(S& s) {} ")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4325 | let rs_api_impl = generate_bindings_tokens(ir)?.rs_api_impl; |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 4326 | assert_cc_matches!( |
| 4327 | rs_api_impl, |
| 4328 | quote! { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4329 | extern "C" void __rust_thunk___Z3fooR1S(struct S* s) { |
| 4330 | foo(*s); |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 4331 | } |
| 4332 | } |
| 4333 | ); |
| 4334 | Ok(()) |
| 4335 | } |
| 4336 | |
| 4337 | #[test] |
| 4338 | fn test_const_ref_to_struct_in_thunk_impls() -> Result<()> { |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4339 | let ir = ir_from_cc("struct S{}; inline void foo(const S& s) {} ")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4340 | let rs_api_impl = generate_bindings_tokens(ir)?.rs_api_impl; |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 4341 | assert_cc_matches!( |
| 4342 | rs_api_impl, |
| 4343 | quote! { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4344 | extern "C" void __rust_thunk___Z3fooRK1S(const struct S* s) { |
| 4345 | foo(*s); |
Lukasz Anforowicz | 275fa92 | 2022-01-05 16:13:20 +0000 | [diff] [blame] | 4346 | } |
| 4347 | } |
| 4348 | ); |
| 4349 | Ok(()) |
| 4350 | } |
| 4351 | |
| 4352 | #[test] |
Lukasz Anforowicz | 957cbf2 | 2022-01-05 16:14:05 +0000 | [diff] [blame] | 4353 | fn test_unsigned_int_in_thunk_impls() -> Result<()> { |
| 4354 | let ir = ir_from_cc("inline void foo(unsigned int i) {} ")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4355 | let rs_api_impl = generate_bindings_tokens(ir)?.rs_api_impl; |
Lukasz Anforowicz | 957cbf2 | 2022-01-05 16:14:05 +0000 | [diff] [blame] | 4356 | assert_cc_matches!( |
| 4357 | rs_api_impl, |
| 4358 | quote! { |
| 4359 | extern "C" void __rust_thunk___Z3fooj(unsigned int i) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4360 | foo(i); |
Lukasz Anforowicz | 957cbf2 | 2022-01-05 16:14:05 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | ); |
| 4364 | Ok(()) |
| 4365 | } |
| 4366 | |
| 4367 | #[test] |
Marcel Hlopko | dd1fcb1 | 2021-12-22 14:13:59 +0000 | [diff] [blame] | 4368 | fn test_record_static_methods_qualify_call_in_thunk() -> Result<()> { |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 4369 | let ir = ir_from_cc( |
| 4370 | r#" |
Marcel Hlopko | dd1fcb1 | 2021-12-22 14:13:59 +0000 | [diff] [blame] | 4371 | struct SomeStruct { |
| 4372 | static inline int some_func() { return 42; } |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 4373 | }; "#, |
| 4374 | )?; |
Marcel Hlopko | dd1fcb1 | 2021-12-22 14:13:59 +0000 | [diff] [blame] | 4375 | |
| 4376 | assert_cc_matches!( |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4377 | generate_bindings_tokens(ir)?.rs_api_impl, |
Marcel Hlopko | dd1fcb1 | 2021-12-22 14:13:59 +0000 | [diff] [blame] | 4378 | quote! { |
| 4379 | extern "C" int __rust_thunk___ZN10SomeStruct9some_funcEv() { |
| 4380 | return SomeStruct::some_func(); |
| 4381 | } |
| 4382 | } |
| 4383 | ); |
| 4384 | Ok(()) |
| 4385 | } |
| 4386 | |
| 4387 | #[test] |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 4388 | fn test_record_instance_methods_deref_this_in_thunk() -> Result<()> { |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 4389 | let ir = ir_from_cc( |
| 4390 | r#" |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 4391 | struct SomeStruct { |
| 4392 | inline int some_func(int arg) const { return 42 + arg; } |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 4393 | }; "#, |
| 4394 | )?; |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 4395 | |
| 4396 | assert_cc_matches!( |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4397 | generate_bindings_tokens(ir)?.rs_api_impl, |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 4398 | quote! { |
| 4399 | extern "C" int __rust_thunk___ZNK10SomeStruct9some_funcEi( |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 4400 | const struct SomeStruct* __this, int arg) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4401 | return __this->some_func(arg); |
Lukasz Anforowicz | b3d89aa | 2022-01-12 14:35:52 +0000 | [diff] [blame] | 4402 | } |
| 4403 | } |
| 4404 | ); |
| 4405 | Ok(()) |
| 4406 | } |
| 4407 | |
| 4408 | #[test] |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 4409 | fn test_record_with_unsupported_field_type() -> Result<()> { |
| 4410 | // Using a nested struct because it's currently not supported. |
| 4411 | // But... any other unsupported type would also work for this test. |
| 4412 | let ir = ir_from_cc( |
| 4413 | r#" |
| 4414 | struct StructWithUnsupportedField { |
| 4415 | struct NestedStruct { |
| 4416 | int nested_field; |
| 4417 | }; |
| 4418 | |
| 4419 | // Doc comment for `my_field`. |
| 4420 | NestedStruct my_field; |
| 4421 | }; |
| 4422 | "#, |
| 4423 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4424 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 4425 | assert_rs_matches!( |
| 4426 | rs_api, |
| 4427 | quote! { |
| 4428 | #[repr(C, align(4))] |
| 4429 | pub struct StructWithUnsupportedField { |
| 4430 | #[doc = " Doc comment for `my_field`.\n \n Reason for representing this field as a blob of bytes:\n Unsupported type 'struct StructWithUnsupportedField::NestedStruct': No generated bindings found for 'NestedStruct'"] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4431 | pub(crate) my_field: [::std::mem::MaybeUninit<u8>; 4], |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 4432 | } |
| 4433 | ... |
| 4434 | const _: () = assert!( |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 4435 | memoffset::offset_of!(crate::StructWithUnsupportedField, my_field) == 0); |
Lukasz Anforowicz | fea0db9 | 2022-05-17 17:28:04 -0700 | [diff] [blame] | 4436 | } |
| 4437 | ); |
| 4438 | Ok(()) |
| 4439 | } |
| 4440 | |
| 4441 | #[test] |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4442 | fn test_struct_with_unnamed_bitfield_member() -> Result<()> { |
| 4443 | // This test input causes `field_decl->getName()` to return an empty string. |
| 4444 | // This example is based on `struct timex` from |
| 4445 | // /usr/grte/v5/include/bits/timex.h |
| 4446 | let ir = ir_from_cc( |
| 4447 | r#" |
| 4448 | struct SomeStruct { |
| 4449 | int first_field; |
| 4450 | int :32; |
| 4451 | int last_field; |
| 4452 | }; "#, |
| 4453 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4454 | let BindingsTokens { rs_api, .. } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4455 | assert_rs_matches!( |
| 4456 | rs_api, |
| 4457 | quote! { |
| 4458 | #[repr(C)] |
| 4459 | pub struct SomeStruct { |
Michael Forster | 82c02d3 | 2022-05-20 21:47:33 -0700 | [diff] [blame] | 4460 | pub first_field: i32, ... |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4461 | __bitfields1: [::std::mem::MaybeUninit<u8>; 4], |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4462 | pub last_field: i32, |
| 4463 | } |
| 4464 | ... |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 4465 | const _: () = assert!(memoffset::offset_of!(crate::SomeStruct, first_field) == 0); |
| 4466 | const _: () = assert!(memoffset::offset_of!(crate::SomeStruct, last_field) == 8); |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4467 | } |
| 4468 | ); |
| 4469 | Ok(()) |
| 4470 | } |
| 4471 | |
| 4472 | #[test] |
Kinuko Yasuda | 6ff59f1 | 2022-08-11 08:41:45 -0700 | [diff] [blame] | 4473 | fn test_struct_with_inheritable_field() -> Result<()> { |
| 4474 | let ir = ir_from_cc( |
| 4475 | r#" |
| 4476 | struct TrivialButInheritable { |
| 4477 | int x; |
| 4478 | }; |
| 4479 | struct StructWithInheritable final { |
| 4480 | TrivialButInheritable t; |
| 4481 | }; |
| 4482 | "#, |
| 4483 | )?; |
| 4484 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 4485 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Copy ... )}); |
| 4486 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Clone ... )}); |
| 4487 | Ok(()) |
| 4488 | } |
| 4489 | |
| 4490 | #[test] |
| 4491 | fn test_union_with_inheritable_field() -> Result<()> { |
| 4492 | let ir = ir_from_cc( |
| 4493 | r#" |
| 4494 | struct TrivialButInheritable { |
| 4495 | int x; |
| 4496 | }; |
| 4497 | union UnionWithInheritable { |
| 4498 | TrivialButInheritable t; |
| 4499 | }; |
| 4500 | "#, |
| 4501 | )?; |
| 4502 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 4503 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Copy ... )}); |
| 4504 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Clone ... )}); |
| 4505 | Ok(()) |
| 4506 | } |
| 4507 | |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4508 | /// Classes with a non-public destructor shouldn't be constructible, not |
| 4509 | /// even via Copy/Clone. |
| 4510 | #[test] |
| 4511 | fn test_trivial_nonpublic_destructor() -> Result<()> { |
| 4512 | let ir = ir_from_cc( |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4513 | r#"#pragma clang lifetime_elision |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4514 | struct Indestructible final { |
| 4515 | Indestructible() = default; |
| 4516 | Indestructible(int); |
| 4517 | Indestructible(const Indestructible&) = default; |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4518 | void Foo() const; |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4519 | private: |
| 4520 | ~Indestructible() = default; |
| 4521 | }; |
| 4522 | |
| 4523 | Indestructible ReturnsValue(); |
| 4524 | void TakesValue(Indestructible); |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4525 | void TakesReference(const Indestructible& x); |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4526 | "#, |
| 4527 | )?; |
| 4528 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4529 | // It isn't available by value: |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4530 | assert_rs_not_matches!(rs_api, quote! {Default}); |
| 4531 | assert_rs_not_matches!(rs_api, quote! {From}); |
| 4532 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Copy ... )}); |
| 4533 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Clone ... )}); |
| 4534 | assert_rs_not_matches!(rs_api, quote! {ReturnsValue}); |
| 4535 | assert_rs_not_matches!(rs_api, quote! {TakesValue}); |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4536 | // ... but it is otherwise available: |
| 4537 | assert_rs_matches!(rs_api, quote! {struct Indestructible}); |
| 4538 | assert_rs_matches!(rs_api, quote! {fn Foo<'a>(&'a self)}); |
| 4539 | assert_rs_matches!(rs_api, quote! {fn TakesReference<'a>(x: &'a crate::Indestructible)}); |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4540 | Ok(()) |
| 4541 | } |
| 4542 | |
| 4543 | #[test] |
| 4544 | fn test_nontrivial_nonpublic_destructor() -> Result<()> { |
| 4545 | let ir = ir_from_cc( |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4546 | r#"#pragma clang lifetime_elision |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4547 | struct Indestructible final { |
| 4548 | Indestructible() = default; |
| 4549 | Indestructible(int); |
| 4550 | Indestructible(const Indestructible&) = default; |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4551 | void Foo() const; |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4552 | private: |
| 4553 | ~Indestructible() {} |
| 4554 | }; |
| 4555 | |
| 4556 | Indestructible ReturnsValue(); |
| 4557 | void TakesValue(Indestructible); |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4558 | void TakesReference(const Indestructible& x); |
| 4559 | "#, |
| 4560 | )?; |
| 4561 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 4562 | // It isn't available by value: |
| 4563 | assert_rs_not_matches!(rs_api, quote! {CtorNew}); |
| 4564 | assert_rs_not_matches!(rs_api, quote! {ReturnsValue}); |
| 4565 | assert_rs_not_matches!(rs_api, quote! {TakesValue}); |
| 4566 | // ... but it is otherwise available: |
| 4567 | assert_rs_matches!(rs_api, quote! {struct Indestructible}); |
| 4568 | assert_rs_matches!(rs_api, quote! {fn Foo<'a>(&'a self)}); |
| 4569 | assert_rs_matches!(rs_api, quote! {fn TakesReference<'a>(x: &'a crate::Indestructible)}); |
| 4570 | Ok(()) |
| 4571 | } |
| 4572 | |
| 4573 | /// trivial abstract structs shouldn't be constructible, not even via |
| 4574 | /// Copy/Clone. |
| 4575 | /// |
| 4576 | /// Right now, a struct can only be Copy/Clone if it's final, but that |
| 4577 | /// restriction will likely be lifted later. |
| 4578 | #[test] |
| 4579 | fn test_trivial_abstract_by_value() -> Result<()> { |
| 4580 | let ir = ir_from_cc( |
| 4581 | r#"#pragma clang lifetime_elision |
| 4582 | struct Abstract final { |
| 4583 | Abstract() = default; |
| 4584 | Abstract(int); |
| 4585 | Abstract(const Abstract&) = default; |
| 4586 | virtual void Foo() const = 0; |
| 4587 | void Nonvirtual() const; |
| 4588 | }; |
| 4589 | void TakesAbstract(const Abstract& a); |
| 4590 | "#, |
| 4591 | )?; |
| 4592 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 4593 | // It isn't available by value: |
| 4594 | assert_rs_not_matches!(rs_api, quote! {Default}); |
| 4595 | assert_rs_not_matches!(rs_api, quote! {From}); |
| 4596 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Copy ... )}); |
| 4597 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Clone ... )}); |
| 4598 | // ... but it is otherwise available: |
| 4599 | assert_rs_matches!(rs_api, quote! {struct Abstract}); |
| 4600 | assert_rs_matches!(rs_api, quote! {fn Foo<'a>(&'a self)}); |
| 4601 | assert_rs_matches!(rs_api, quote! {fn Nonvirtual<'a>(&'a self)}); |
| 4602 | assert_rs_matches!(rs_api, quote! {fn TakesAbstract<'a>(a: &'a crate::Abstract)}); |
| 4603 | Ok(()) |
| 4604 | } |
| 4605 | |
| 4606 | #[test] |
| 4607 | fn test_nontrivial_abstract_by_value() -> Result<()> { |
| 4608 | let ir = ir_from_cc( |
| 4609 | r#"#pragma clang lifetime_elision |
| 4610 | struct Abstract final { |
| 4611 | Abstract() {}; |
| 4612 | Abstract(int); |
| 4613 | Abstract(const Abstract&) {} |
| 4614 | virtual void Foo() const = 0; |
| 4615 | void Nonvirtual() const; |
| 4616 | }; |
| 4617 | void TakesAbstract(const Abstract& a); |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4618 | "#, |
| 4619 | )?; |
| 4620 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 4621 | assert_rs_not_matches!(rs_api, quote! {CtorNew}); |
Devin Jeanpierre | ccb6767 | 2022-08-17 10:05:47 -0700 | [diff] [blame] | 4622 | // ... but it is otherwise available: |
| 4623 | assert_rs_matches!(rs_api, quote! {struct Abstract}); |
| 4624 | assert_rs_matches!(rs_api, quote! {fn Foo<'a>(&'a self)}); |
| 4625 | assert_rs_matches!(rs_api, quote! {fn Nonvirtual<'a>(&'a self)}); |
| 4626 | assert_rs_matches!(rs_api, quote! {fn TakesAbstract<'a>(a: &'a crate::Abstract)}); |
Devin Jeanpierre | a99e0a3 | 2022-08-17 01:36:58 -0700 | [diff] [blame] | 4627 | Ok(()) |
| 4628 | } |
| 4629 | |
Kinuko Yasuda | 6ff59f1 | 2022-08-11 08:41:45 -0700 | [diff] [blame] | 4630 | #[test] |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4631 | fn test_struct_with_unnamed_struct_and_union_members() -> Result<()> { |
| 4632 | // This test input causes `field_decl->getName()` to return an empty string. |
| 4633 | // See also: |
| 4634 | // - https://en.cppreference.com/w/c/language/struct: "[...] an unnamed member |
| 4635 | // of a struct whose type is a struct without name is known as anonymous |
| 4636 | // struct." |
| 4637 | // - https://rust-lang.github.io/rfcs/2102-unnamed-fields.html |
| 4638 | let ir = ir_from_cc( |
| 4639 | r#" |
| 4640 | struct StructWithUnnamedMembers { |
| 4641 | int first_field; |
| 4642 | |
| 4643 | struct { |
| 4644 | int anonymous_struct_field_1; |
| 4645 | int anonymous_struct_field_2; |
| 4646 | }; |
| 4647 | union { |
| 4648 | int anonymous_union_field_1; |
| 4649 | int anonymous_union_field_2; |
| 4650 | }; |
| 4651 | |
| 4652 | int last_field; |
| 4653 | }; "#, |
| 4654 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4655 | let BindingsTokens { rs_api, .. } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4656 | // TODO(b/200067824): Once nested structs anhd unions are supported, |
| 4657 | // `__unnamed_field1` and `__unnamed_field2` should have a real, usable |
| 4658 | // type. |
| 4659 | assert_rs_matches!( |
| 4660 | rs_api, |
| 4661 | quote! { |
| 4662 | #[repr(C, align(4))] |
| 4663 | pub struct StructWithUnnamedMembers { |
| 4664 | pub first_field: i32, |
Googler | 84cd560 | 2022-10-07 08:25:46 -0700 | [diff] [blame] | 4665 | #[doc=" Reason for representing this field as a blob of bytes:\n Unsupported type 'struct StructWithUnnamedMembers::(anonymous at ./ir_from_cc_virtual_header.h:7:15)': No generated bindings found for ''"] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4666 | pub(crate) __unnamed_field1: [::std::mem::MaybeUninit<u8>; 8], |
Googler | 84cd560 | 2022-10-07 08:25:46 -0700 | [diff] [blame] | 4667 | #[doc=" Reason for representing this field as a blob of bytes:\n Unsupported type 'union StructWithUnnamedMembers::(anonymous at ./ir_from_cc_virtual_header.h:11:15)': No generated bindings found for ''"] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 4668 | pub(crate) __unnamed_field2: [::std::mem::MaybeUninit<u8>; 4], |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4669 | pub last_field: i32, |
| 4670 | } |
| 4671 | ... |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 4672 | const _: () = assert!(memoffset::offset_of!( |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 4673 | crate::StructWithUnnamedMembers, first_field) == 0); |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 4674 | const _: () = assert!(memoffset::offset_of!( |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 4675 | crate::StructWithUnnamedMembers, __unnamed_field1) == 4); |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 4676 | const _: () = assert!(memoffset::offset_of!( |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 4677 | crate::StructWithUnnamedMembers, __unnamed_field2) == 12); |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 4678 | const _: () = assert!(memoffset::offset_of!( |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 4679 | crate::StructWithUnnamedMembers, last_field) == 16); |
Lukasz Anforowicz | e200e8a | 2022-05-18 12:36:33 -0700 | [diff] [blame] | 4680 | } |
| 4681 | ); |
| 4682 | Ok(()) |
| 4683 | } |
| 4684 | |
| 4685 | #[test] |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 4686 | fn test_struct_from_other_target() -> Result<()> { |
| 4687 | let ir = ir_from_cc_dependency("// intentionally empty", "struct SomeStruct {};")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4688 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 4689 | assert_rs_not_matches!(rs_api, quote! { SomeStruct }); |
| 4690 | assert_cc_not_matches!(rs_api_impl, quote! { SomeStruct }); |
Marcel Hlopko | a0f3866 | 2021-12-03 08:45:26 +0000 | [diff] [blame] | 4691 | Ok(()) |
| 4692 | } |
| 4693 | |
| 4694 | #[test] |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4695 | fn test_copy_derives() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 4696 | let record = ir_record("S"); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 4697 | assert_eq!(generate_derives(&record), &["Clone", "Copy"]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
| 4700 | #[test] |
| 4701 | fn test_copy_derives_not_is_trivial_abi() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 4702 | let mut record = ir_record("S"); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4703 | record.is_trivial_abi = false; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 4704 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 4707 | /// Even if it's trivially relocatable, !Unpin C++ type cannot be |
| 4708 | /// cloned/copied or otherwise used by value, because values would allow |
| 4709 | /// assignment into the Pin. |
| 4710 | /// |
| 4711 | /// All !Unpin C++ types, not just non trivially relocatable ones, are |
| 4712 | /// unsafe to assign in the Rust sense. |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 4713 | #[test] |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 4714 | fn test_copy_derives_not_final() { |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 4715 | let mut record = ir_record("S"); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 4716 | record.is_inheritable = true; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 4717 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4720 | #[test] |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4721 | fn test_copy_derives_ctor_deleted() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 4722 | let mut record = ir_record("S"); |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 4723 | record.copy_constructor = ir::SpecialMemberFunc::Unavailable; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 4724 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | #[test] |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 4728 | fn test_copy_derives_ctor_nontrivial_members() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 4729 | let mut record = ir_record("S"); |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 4730 | record.copy_constructor = ir::SpecialMemberFunc::NontrivialMembers; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 4731 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | be2f33b | 2021-10-21 12:54:19 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | #[test] |
| 4735 | fn test_copy_derives_ctor_nontrivial_self() { |
Devin Jeanpierre | ccfefc8 | 2021-10-27 10:54:00 +0000 | [diff] [blame] | 4736 | let mut record = ir_record("S"); |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 4737 | record.copy_constructor = ir::SpecialMemberFunc::NontrivialUserDefined; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 4738 | assert_eq!(generate_derives(&record), &[""; 0]); |
Devin Jeanpierre | 2ed14ec | 2021-10-06 11:32:19 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
Devin Jeanpierre | b1e816a | 2022-04-29 20:14:22 -0700 | [diff] [blame] | 4741 | /// In Rust, a Drop type cannot be Copy. |
| 4742 | #[test] |
| 4743 | fn test_copy_derives_dtor_nontrivial_self() { |
| 4744 | let mut record = ir_record("S"); |
Lukasz Anforowicz | ff7df4a | 2022-06-02 14:27:45 -0700 | [diff] [blame] | 4745 | for definition in |
| 4746 | [ir::SpecialMemberFunc::NontrivialUserDefined, ir::SpecialMemberFunc::NontrivialMembers] |
| 4747 | { |
| 4748 | record.destructor = definition; |
Devin Jeanpierre | b1e816a | 2022-04-29 20:14:22 -0700 | [diff] [blame] | 4749 | assert_eq!(generate_derives(&record), &["Clone"]); |
| 4750 | } |
| 4751 | } |
| 4752 | |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 4753 | #[test] |
| 4754 | fn test_ptr_func() -> Result<()> { |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 4755 | let ir = ir_from_cc(r#" inline int* Deref(int*const* p); "#)?; |
Devin Jeanpierre | d6da700 | 2021-10-21 12:55:20 +0000 | [diff] [blame] | 4756 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4757 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4758 | assert_rs_matches!( |
| 4759 | rs_api, |
| 4760 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 4761 | #[inline(always)] |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 4762 | pub unsafe fn Deref(p: *const *mut i32) -> *mut i32 { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 4763 | crate::detail::__rust_thunk___Z5DerefPKPi(p) |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4764 | } |
| 4765 | } |
| 4766 | ); |
| 4767 | assert_rs_matches!( |
| 4768 | rs_api, |
| 4769 | quote! { |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 4770 | mod detail { |
Googler | 5564714 | 2022-01-11 12:37:39 +0000 | [diff] [blame] | 4771 | #[allow(unused_imports)] |
Devin Jeanpierre | d4dde0e | 2021-10-13 20:48:25 +0000 | [diff] [blame] | 4772 | use super::*; |
Michael Forster | db8101a | 2021-10-08 06:56:03 +0000 | [diff] [blame] | 4773 | extern "C" { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 4774 | pub(crate) fn __rust_thunk___Z5DerefPKPi(p: *const *mut i32) -> *mut i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4775 | } |
| 4776 | } |
| 4777 | } |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 4778 | ); |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 4779 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4780 | assert_cc_matches!( |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 4781 | rs_api_impl, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4782 | quote! { |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 4783 | extern "C" int* __rust_thunk___Z5DerefPKPi(int* const * p) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4784 | return Deref(p); |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 4785 | } |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 4786 | } |
Devin Jeanpierre | 184f9ac | 2021-09-17 13:47:03 +0000 | [diff] [blame] | 4787 | ); |
Devin Jeanpierre | 7a7328e | 2021-09-17 07:10:08 +0000 | [diff] [blame] | 4788 | Ok(()) |
| 4789 | } |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 4790 | |
| 4791 | #[test] |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 4792 | fn test_const_char_ptr_func() -> Result<()> { |
| 4793 | // This is a regression test: We used to include the "const" in the name |
| 4794 | // of the CcType, which caused a panic in the code generator |
| 4795 | // ('"const char" is not a valid Ident'). |
| 4796 | // It's therefore important that f() is inline so that we need to |
| 4797 | // generate a thunk for it (where we then process the CcType). |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 4798 | let ir = ir_from_cc(r#" inline void f(const char *str); "#)?; |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 4799 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4800 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 4801 | assert_rs_matches!( |
| 4802 | rs_api, |
| 4803 | quote! { |
| 4804 | #[inline(always)] |
Lukasz Anforowicz | f7bdd39 | 2022-01-21 00:33:39 +0000 | [diff] [blame] | 4805 | pub unsafe fn f(str: *const i8) { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 4806 | crate::detail::__rust_thunk___Z1fPKc(str) |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 4807 | } |
| 4808 | } |
| 4809 | ); |
| 4810 | assert_rs_matches!( |
| 4811 | rs_api, |
| 4812 | quote! { |
| 4813 | extern "C" { |
| 4814 | pub(crate) fn __rust_thunk___Z1fPKc(str: *const i8); |
| 4815 | } |
| 4816 | } |
| 4817 | ); |
| 4818 | |
| 4819 | assert_cc_matches!( |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 4820 | rs_api_impl, |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 4821 | quote! { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 4822 | extern "C" void __rust_thunk___Z1fPKc(char const * str){ f(str); } |
Googler | db11153 | 2022-01-05 06:12:13 +0000 | [diff] [blame] | 4823 | } |
| 4824 | ); |
| 4825 | Ok(()) |
| 4826 | } |
| 4827 | |
| 4828 | #[test] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4829 | fn test_func_ptr_where_params_are_primitive_types() -> Result<()> { |
| 4830 | let ir = ir_from_cc(r#" int (*get_ptr_to_func())(float, double); "#)?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4831 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4832 | assert_rs_matches!( |
| 4833 | rs_api, |
| 4834 | quote! { |
| 4835 | #[inline(always)] |
| 4836 | pub fn get_ptr_to_func() -> Option<extern "C" fn (f32, f64) -> i32> { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 4837 | unsafe { crate::detail::__rust_thunk___Z15get_ptr_to_funcv() } |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4838 | } |
| 4839 | } |
| 4840 | ); |
| 4841 | assert_rs_matches!( |
| 4842 | rs_api, |
| 4843 | quote! { |
| 4844 | mod detail { |
| 4845 | #[allow(unused_imports)] |
| 4846 | use super::*; |
| 4847 | extern "C" { |
| 4848 | #[link_name = "_Z15get_ptr_to_funcv"] |
| 4849 | pub(crate) fn __rust_thunk___Z15get_ptr_to_funcv() |
| 4850 | -> Option<extern "C" fn(f32, f64) -> i32>; |
| 4851 | } |
| 4852 | } |
| 4853 | } |
| 4854 | ); |
| 4855 | // Verify that no C++ thunk got generated. |
| 4856 | assert_cc_not_matches!(rs_api_impl, quote! { __rust_thunk___Z15get_ptr_to_funcv }); |
| 4857 | |
| 4858 | // TODO(b/217419782): Add another test for more exotic calling conventions / |
| 4859 | // abis. |
| 4860 | |
| 4861 | // TODO(b/217419782): Add another test for pointer to a function that |
| 4862 | // takes/returns non-trivially-movable types by value. See also |
| 4863 | // <internal link> |
| 4864 | |
| 4865 | Ok(()) |
| 4866 | } |
| 4867 | |
| 4868 | #[test] |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 4869 | fn test_func_ref() -> Result<()> { |
| 4870 | let ir = ir_from_cc(r#" int (&get_ref_to_func())(float, double); "#)?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4871 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 4872 | assert_rs_matches!( |
| 4873 | rs_api, |
| 4874 | quote! { |
| 4875 | #[inline(always)] |
| 4876 | pub fn get_ref_to_func() -> extern "C" fn (f32, f64) -> i32 { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 4877 | unsafe { crate::detail::__rust_thunk___Z15get_ref_to_funcv() } |
Lukasz Anforowicz | 92c81c3 | 2022-03-04 19:03:56 +0000 | [diff] [blame] | 4878 | } |
| 4879 | } |
| 4880 | ); |
| 4881 | Ok(()) |
| 4882 | } |
| 4883 | |
| 4884 | #[test] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4885 | fn test_func_ptr_with_non_static_lifetime() -> Result<()> { |
Martin Brænne | e5ba6b6 | 2022-06-23 07:38:40 -0700 | [diff] [blame] | 4886 | let ir = ir_from_cc(&with_lifetime_macros( |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4887 | r#" |
Martin Brænne | e5ba6b6 | 2022-06-23 07:38:40 -0700 | [diff] [blame] | 4888 | int (* $a get_ptr_to_func())(float, double); "#, |
| 4889 | ))?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4890 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4891 | assert_rs_matches!( |
| 4892 | rs_api, |
| 4893 | quote! { |
| 4894 | // Error while generating bindings for item 'get_ptr_to_func': |
| 4895 | // Return type is not supported: Function pointers with non-'static lifetimes are not supported: int (*)(float, double) |
| 4896 | } |
| 4897 | ); |
| 4898 | Ok(()) |
| 4899 | } |
| 4900 | |
| 4901 | #[test] |
| 4902 | fn test_func_ptr_where_params_are_raw_ptrs() -> Result<()> { |
| 4903 | let ir = ir_from_cc(r#" const int* (*get_ptr_to_func())(const int*); "#)?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4904 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4905 | assert_rs_matches!( |
| 4906 | rs_api, |
| 4907 | quote! { |
| 4908 | #[inline(always)] |
| 4909 | pub fn get_ptr_to_func() -> Option<extern "C" fn (*const i32) -> *const i32> { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 4910 | unsafe { crate::detail::__rust_thunk___Z15get_ptr_to_funcv() } |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4911 | } |
| 4912 | } |
| 4913 | ); |
| 4914 | assert_rs_matches!( |
| 4915 | rs_api, |
| 4916 | quote! { |
| 4917 | mod detail { |
| 4918 | #[allow(unused_imports)] |
| 4919 | use super::*; |
| 4920 | extern "C" { |
| 4921 | #[link_name = "_Z15get_ptr_to_funcv"] |
| 4922 | pub(crate) fn __rust_thunk___Z15get_ptr_to_funcv() |
| 4923 | -> Option<extern "C" fn(*const i32) -> *const i32>; |
| 4924 | } |
| 4925 | } |
| 4926 | } |
| 4927 | ); |
| 4928 | // Verify that no C++ thunk got generated. |
| 4929 | assert_cc_not_matches!(rs_api_impl, quote! { __rust_thunk___Z15get_ptr_to_funcv }); |
| 4930 | |
| 4931 | // TODO(b/217419782): Add another test where params (and the return |
| 4932 | // type) are references with lifetimes. Something like this: |
| 4933 | // #pragma clang lifetime_elision |
| 4934 | // const int& (*get_ptr_to_func())(const int&, const int&); "#)?; |
| 4935 | // 1) Need to investigate why this fails - seeing raw pointers in Rust |
| 4936 | // seems to indicate that no lifetimes are present at the `importer.cc` |
| 4937 | // level. Maybe lifetime elision doesn't support this scenario? Unclear |
Googler | 53f6594 | 2022-02-23 11:23:30 +0000 | [diff] [blame] | 4938 | // how to explicitly apply [[clang::annotate("lifetimes", "a, b -> a")]] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 4939 | // to the _inner_ function. |
| 4940 | // 2) It is important to have 2 reference parameters, so see if the problem |
| 4941 | // of passing `lifetimes` by value would have been caught - see: |
| 4942 | // cl/428079010/depot/rs_bindings_from_cc/ |
| 4943 | // importer.cc?version=s6#823 |
| 4944 | |
| 4945 | // TODO(b/217419782): Decide what to do if the C++ pointer is *not* |
| 4946 | // annotated with a lifetime - emit `unsafe fn(...) -> ...` in that |
| 4947 | // case? |
| 4948 | |
| 4949 | Ok(()) |
| 4950 | } |
| 4951 | |
| 4952 | #[test] |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 4953 | fn test_func_ptr_with_custom_abi() -> Result<()> { |
| 4954 | let ir = ir_from_cc(r#" int (*get_ptr_to_func())(float, double) [[clang::vectorcall]]; "#)?; |
| 4955 | |
| 4956 | // Verify that the test input correctly represents what we intend to |
| 4957 | // test - we want [[clang::vectorcall]] to apply to the returned |
| 4958 | // function pointer, but *not* apply to the `get_ptr_to_func` function. |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 4959 | assert_ir_matches!( |
| 4960 | ir, |
| 4961 | quote! { |
| 4962 | Func(Func { |
| 4963 | name: "get_ptr_to_func", ... |
| 4964 | return_type: MappedType { |
| 4965 | rs_type: RsType { |
| 4966 | name: Some("Option"), ... |
| 4967 | type_args: [RsType { name: Some("#funcPtr vectorcall"), ... }], ... |
| 4968 | }, |
| 4969 | cc_type: CcType { |
| 4970 | name: Some("*"), ... |
| 4971 | type_args: [CcType { name: Some("#funcValue vectorcall"), ... }], ... |
| 4972 | }, |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 4973 | }, ... |
| 4974 | has_c_calling_convention: true, ... |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 4975 | }), |
| 4976 | } |
| 4977 | ); |
| 4978 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 4979 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 4980 | // Check that the custom "vectorcall" ABI gets propagated into the |
| 4981 | // return type (i.e. into `extern "vectorcall" fn`). |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 4982 | assert_rs_matches!( |
| 4983 | rs_api, |
| 4984 | quote! { |
| 4985 | #[inline(always)] |
| 4986 | pub fn get_ptr_to_func() -> Option<extern "vectorcall" fn (f32, f64) -> i32> { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 4987 | unsafe { crate::detail::__rust_thunk___Z15get_ptr_to_funcv() } |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 4988 | } |
| 4989 | } |
| 4990 | ); |
| 4991 | |
| 4992 | // The usual `extern "C"` ABI should be used for "get_ptr_to_func". |
| 4993 | assert_rs_matches!( |
| 4994 | rs_api, |
| 4995 | quote! { |
| 4996 | mod detail { |
| 4997 | #[allow(unused_imports)] |
| 4998 | use super::*; |
| 4999 | extern "C" { |
| 5000 | #[link_name = "_Z15get_ptr_to_funcv"] |
| 5001 | pub(crate) fn __rust_thunk___Z15get_ptr_to_funcv() |
| 5002 | -> Option<extern "vectorcall" fn(f32, f64) -> i32>; |
| 5003 | } |
| 5004 | } |
| 5005 | } |
| 5006 | ); |
| 5007 | |
| 5008 | // Verify that no C++ thunk got generated. |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 5009 | assert_cc_not_matches!(rs_api_impl, quote! { __rust_thunk___Z15get_ptr_to_funcv }); |
| 5010 | Ok(()) |
| 5011 | } |
| 5012 | |
| 5013 | #[test] |
| 5014 | fn test_func_ptr_thunk() -> Result<()> { |
| 5015 | // Using an `inline` keyword forces generation of a C++ thunk in |
| 5016 | // `rs_api_impl` (i.e. exercises `format_cc_type` and similar code). |
| 5017 | let ir = ir_from_cc( |
| 5018 | r#" |
| 5019 | int multiply(int x, int y); |
| 5020 | inline int (*inline_get_pointer_to_function())(int, int) { |
| 5021 | return multiply; |
| 5022 | } |
| 5023 | "#, |
| 5024 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5025 | let rs_api_impl = generate_bindings_tokens(ir)?.rs_api_impl; |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 5026 | assert_cc_matches!( |
| 5027 | rs_api_impl, |
| 5028 | quote! { |
Lukasz Anforowicz | 2317154 | 2022-04-11 15:26:17 -0700 | [diff] [blame] | 5029 | extern "C" crubit::type_identity_t<int(int , int)>* |
Lukasz Anforowicz | 71e9b59 | 2022-03-04 19:03:02 +0000 | [diff] [blame] | 5030 | __rust_thunk___Z30inline_get_pointer_to_functionv() { |
| 5031 | return inline_get_pointer_to_function(); |
| 5032 | } |
| 5033 | } |
| 5034 | ); |
| 5035 | Ok(()) |
| 5036 | } |
| 5037 | |
| 5038 | #[test] |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 5039 | fn test_func_ptr_with_custom_abi_thunk() -> Result<()> { |
| 5040 | // Using an `inline` keyword forces generation of a C++ thunk in |
| 5041 | // `rs_api_impl` (i.e. exercises `format_cc_type`, |
| 5042 | // `format_cc_call_conv_as_clang_attribute` and similar code). |
| 5043 | let ir = ir_from_cc( |
| 5044 | r#" |
| 5045 | inline int (*inline_get_ptr_to_func())(float, double) [[clang::vectorcall]]; |
| 5046 | "#, |
| 5047 | )?; |
| 5048 | |
| 5049 | // Verify that the test input correctly represents what we intend to |
| 5050 | // test - we want [[clang::vectorcall]] to apply to the returned |
| 5051 | // function pointer, but *not* apply to the `get_ptr_to_func` function. |
| 5052 | assert_ir_matches!( |
| 5053 | ir, |
| 5054 | quote! { |
| 5055 | Func(Func { |
| 5056 | name: "inline_get_ptr_to_func", ... |
| 5057 | return_type: MappedType { |
| 5058 | rs_type: RsType { |
| 5059 | name: Some("Option"), ... |
| 5060 | type_args: [RsType { name: Some("#funcPtr vectorcall"), ... }], ... |
| 5061 | }, |
| 5062 | cc_type: CcType { |
| 5063 | name: Some("*"), ... |
| 5064 | type_args: [CcType { name: Some("#funcValue vectorcall"), ... }], ... |
| 5065 | }, |
| 5066 | }, ... |
| 5067 | has_c_calling_convention: true, ... |
| 5068 | }), |
| 5069 | } |
| 5070 | ); |
| 5071 | |
| 5072 | // This test is quite similar to `test_func_ptr_thunk` - the main |
| 5073 | // difference is verification of the `__attribute__((vectorcall))` in |
| 5074 | // the expected signature of the generated thunk below. |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5075 | let rs_api_impl = generate_bindings_tokens(ir)?.rs_api_impl; |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 5076 | assert_cc_matches!( |
| 5077 | rs_api_impl, |
| 5078 | quote! { |
Lukasz Anforowicz | 2317154 | 2022-04-11 15:26:17 -0700 | [diff] [blame] | 5079 | extern "C" crubit::type_identity_t< |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 5080 | int(float , double) __attribute__((vectorcall)) |
| 5081 | >* __rust_thunk___Z22inline_get_ptr_to_funcv() { |
| 5082 | return inline_get_ptr_to_func(); |
| 5083 | } |
| 5084 | } |
| 5085 | ); |
| 5086 | Ok(()) |
| 5087 | } |
| 5088 | |
| 5089 | #[test] |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 5090 | fn test_item_order() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5091 | let ir = ir_from_cc( |
| 5092 | "int first_func(); |
| 5093 | struct FirstStruct {}; |
| 5094 | int second_func(); |
| 5095 | struct SecondStruct {};", |
| 5096 | )?; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 5097 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5098 | let rs_api = rs_tokens_to_formatted_string_for_tests(generate_bindings_tokens(ir)?.rs_api)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5099 | |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 5100 | 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] | 5101 | |
| 5102 | let f1 = idx("fn first_func")?; |
| 5103 | let f2 = idx("fn second_func")?; |
| 5104 | let s1 = idx("struct FirstStruct")?; |
| 5105 | let s2 = idx("struct SecondStruct")?; |
Googler | a675ae0 | 2021-12-07 08:04:59 +0000 | [diff] [blame] | 5106 | let t1 = idx("fn __rust_thunk___Z10first_funcv")?; |
| 5107 | let t2 = idx("fn __rust_thunk___Z11second_funcv")?; |
Michael Forster | ed64202 | 2021-10-04 09:48:25 +0000 | [diff] [blame] | 5108 | |
| 5109 | assert!(f1 < s1); |
| 5110 | assert!(s1 < f2); |
| 5111 | assert!(f2 < s2); |
| 5112 | assert!(s2 < t1); |
| 5113 | assert!(t1 < t2); |
| 5114 | |
| 5115 | Ok(()) |
| 5116 | } |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 5117 | |
| 5118 | #[test] |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5119 | fn test_base_class_subobject_layout() -> Result<()> { |
| 5120 | let ir = ir_from_cc( |
| 5121 | r#" |
| 5122 | // We use a class here to force `Derived::z` to live inside the tail padding of `Base`. |
| 5123 | // 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] | 5124 | class Base {__INT64_TYPE__ x; char y;}; |
| 5125 | struct Derived final : Base {__INT16_TYPE__ z;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5126 | "#, |
| 5127 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5128 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5129 | assert_rs_matches!( |
| 5130 | rs_api, |
| 5131 | quote! { |
| 5132 | #[repr(C, align(8))] |
| 5133 | pub struct Derived { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5134 | __non_field_data: [::std::mem::MaybeUninit<u8>; 10], |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5135 | pub z: i16, |
| 5136 | } |
| 5137 | } |
| 5138 | ); |
| 5139 | Ok(()) |
| 5140 | } |
| 5141 | |
| 5142 | /// The same as test_base_class_subobject_layout, but with multiple |
| 5143 | /// inheritance. |
| 5144 | #[test] |
| 5145 | fn test_base_class_multiple_inheritance_subobject_layout() -> Result<()> { |
| 5146 | let ir = ir_from_cc( |
| 5147 | r#" |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 5148 | class Base1 {__INT64_TYPE__ x;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5149 | class Base2 {char y;}; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 5150 | struct Derived final : Base1, Base2 {__INT16_TYPE__ z;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5151 | "#, |
| 5152 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5153 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5154 | assert_rs_matches!( |
| 5155 | rs_api, |
| 5156 | quote! { |
| 5157 | #[repr(C, align(8))] |
| 5158 | pub struct Derived { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5159 | __non_field_data: [::std::mem::MaybeUninit<u8>; 10], |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5160 | pub z: i16, |
| 5161 | } |
| 5162 | } |
| 5163 | ); |
| 5164 | Ok(()) |
| 5165 | } |
| 5166 | |
| 5167 | /// The same as test_base_class_subobject_layout, but with a chain of |
| 5168 | /// inheritance. |
| 5169 | #[test] |
| 5170 | fn test_base_class_deep_inheritance_subobject_layout() -> Result<()> { |
| 5171 | let ir = ir_from_cc( |
| 5172 | r#" |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 5173 | class Base1 {__INT64_TYPE__ x;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5174 | class Base2 : Base1 {char y;}; |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 5175 | struct Derived final : Base2 {__INT16_TYPE__ z;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5176 | "#, |
| 5177 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5178 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5179 | assert_rs_matches!( |
| 5180 | rs_api, |
| 5181 | quote! { |
| 5182 | #[repr(C, align(8))] |
| 5183 | pub struct Derived { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5184 | __non_field_data: [::std::mem::MaybeUninit<u8>; 10], |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5185 | pub z: i16, |
| 5186 | } |
| 5187 | } |
| 5188 | ); |
| 5189 | Ok(()) |
| 5190 | } |
| 5191 | |
| 5192 | /// For derived classes with no data members, we can't use the offset of the |
| 5193 | /// first member to determine the size of the base class subobjects. |
| 5194 | #[test] |
| 5195 | fn test_base_class_subobject_fieldless_layout() -> Result<()> { |
| 5196 | let ir = ir_from_cc( |
| 5197 | r#" |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 5198 | class Base {__INT64_TYPE__ x; char y;}; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5199 | struct Derived final : Base {}; |
| 5200 | "#, |
| 5201 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5202 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5203 | assert_rs_matches!( |
| 5204 | rs_api, |
| 5205 | quote! { |
| 5206 | #[repr(C, align(8))] |
| 5207 | pub struct Derived { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5208 | __non_field_data: [::std::mem::MaybeUninit<u8>; 16], |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5209 | } |
| 5210 | } |
| 5211 | ); |
| 5212 | Ok(()) |
| 5213 | } |
| 5214 | |
| 5215 | #[test] |
| 5216 | fn test_base_class_subobject_empty_fieldless() -> Result<()> { |
| 5217 | let ir = ir_from_cc( |
| 5218 | r#" |
| 5219 | class Base {}; |
| 5220 | struct Derived final : Base {}; |
| 5221 | "#, |
| 5222 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5223 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5224 | assert_rs_matches!( |
| 5225 | rs_api, |
| 5226 | quote! { |
| 5227 | #[repr(C)] |
| 5228 | pub struct Derived { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5229 | __non_field_data: [::std::mem::MaybeUninit<u8>; 1], |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5230 | } |
| 5231 | } |
| 5232 | ); |
| 5233 | Ok(()) |
| 5234 | } |
| 5235 | |
| 5236 | #[test] |
| 5237 | fn test_base_class_subobject_empty() -> Result<()> { |
| 5238 | let ir = ir_from_cc( |
| 5239 | r#" |
| 5240 | class Base {}; |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 5241 | struct Derived final : Base { |
| 5242 | __INT16_TYPE__ x; |
| 5243 | }; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5244 | "#, |
| 5245 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5246 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5247 | assert_rs_matches!( |
| 5248 | rs_api, |
| 5249 | quote! { |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5250 | pub struct Derived { |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 5251 | pub x: i16, |
| 5252 | } |
| 5253 | } |
| 5254 | ); |
| 5255 | Ok(()) |
| 5256 | } |
| 5257 | |
| 5258 | /// Non-aggregate structs can't be directly initialized, because we add |
| 5259 | /// a zero-sized private field to the bindings. |
| 5260 | #[test] |
| 5261 | fn test_non_aggregate_struct_private_field() -> Result<()> { |
| 5262 | let ir = ir_from_cc( |
| 5263 | r#" |
| 5264 | struct NonAggregate { |
| 5265 | NonAggregate() {} |
| 5266 | |
| 5267 | __INT16_TYPE__ x = 0; |
| 5268 | }; |
| 5269 | "#, |
| 5270 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5271 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | a2be2a2 | 2022-05-18 18:59:05 -0700 | [diff] [blame] | 5272 | assert_rs_matches!( |
| 5273 | rs_api, |
| 5274 | quote! { |
| 5275 | pub struct NonAggregate { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5276 | __non_field_data: [::std::mem::MaybeUninit<u8>; 0], |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 5277 | pub x: i16, |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5278 | } |
| 5279 | } |
| 5280 | ); |
| 5281 | Ok(()) |
| 5282 | } |
| 5283 | |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5284 | /// When a field is [[no_unique_address]], it occupies the space up to the |
| 5285 | /// next field. |
| 5286 | #[test] |
| 5287 | fn test_no_unique_address() -> Result<()> { |
| 5288 | let ir = ir_from_cc( |
| 5289 | r#" |
| 5290 | class Field1 {__INT64_TYPE__ x;}; |
| 5291 | class Field2 {char y;}; |
| 5292 | struct Struct final { |
| 5293 | [[no_unique_address]] Field1 field1; |
| 5294 | [[no_unique_address]] Field2 field2; |
| 5295 | __INT16_TYPE__ z; |
| 5296 | }; |
| 5297 | "#, |
| 5298 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5299 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5300 | assert_rs_matches!( |
| 5301 | rs_api, |
| 5302 | quote! { |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5303 | #[repr(C, align(8))] |
| 5304 | pub struct Struct { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5305 | pub(crate) field1: [::std::mem::MaybeUninit<u8>; 8], |
| 5306 | pub(crate) field2: [::std::mem::MaybeUninit<u8>; 2], |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5307 | pub z: i16, |
| 5308 | } |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 5309 | } |
| 5310 | ); |
Devin Jeanpierre | 2745013 | 2022-04-11 13:52:01 -0700 | [diff] [blame] | 5311 | assert_rs_matches!( |
| 5312 | rs_api, |
| 5313 | quote! { |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 5314 | impl Struct { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 5315 | pub fn field1(&self) -> &crate::Field1 { |
| 5316 | unsafe {&* (&self.field1 as *const _ as *const crate::Field1)} |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 5317 | } |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 5318 | pub fn field2(&self) -> &crate::Field2 { |
| 5319 | unsafe {&* (&self.field2 as *const _ as *const crate::Field2)} |
Devin Jeanpierre | 58181ac | 2022-02-14 21:30:05 +0000 | [diff] [blame] | 5320 | } |
| 5321 | } |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5322 | } |
| 5323 | ); |
| 5324 | Ok(()) |
| 5325 | } |
| 5326 | |
| 5327 | /// When a [[no_unique_address]] field is the last one, it occupies the rest |
| 5328 | /// of the object. |
| 5329 | #[test] |
| 5330 | fn test_no_unique_address_last_field() -> Result<()> { |
| 5331 | let ir = ir_from_cc( |
| 5332 | r#" |
| 5333 | class Field1 {__INT64_TYPE__ x;}; |
| 5334 | class Field2 {char y;}; |
| 5335 | struct Struct final { |
| 5336 | [[no_unique_address]] Field1 field1; |
| 5337 | [[no_unique_address]] Field2 field2; |
| 5338 | }; |
| 5339 | "#, |
| 5340 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5341 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5342 | assert_rs_matches!( |
| 5343 | rs_api, |
| 5344 | quote! { |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5345 | #[repr(C, align(8))] |
| 5346 | pub struct Struct { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5347 | pub(crate) field1: [::std::mem::MaybeUninit<u8>; 8], |
| 5348 | pub(crate) field2: [::std::mem::MaybeUninit<u8>; 8], |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5349 | } |
| 5350 | } |
| 5351 | ); |
| 5352 | Ok(()) |
| 5353 | } |
| 5354 | |
| 5355 | #[test] |
| 5356 | fn test_no_unique_address_empty() -> Result<()> { |
| 5357 | let ir = ir_from_cc( |
| 5358 | r#" |
| 5359 | class Field {}; |
| 5360 | struct Struct final { |
| 5361 | [[no_unique_address]] Field field; |
| 5362 | int x; |
| 5363 | }; |
| 5364 | "#, |
| 5365 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5366 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5367 | assert_rs_matches!( |
| 5368 | rs_api, |
| 5369 | quote! { |
| 5370 | #[repr(C, align(4))] |
| 5371 | pub struct Struct { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5372 | pub(crate) field: [::std::mem::MaybeUninit<u8>; 0], |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5373 | pub x: i32, |
| 5374 | } |
| 5375 | } |
| 5376 | ); |
| 5377 | Ok(()) |
| 5378 | } |
| 5379 | |
| 5380 | #[test] |
| 5381 | fn test_base_class_subobject_empty_last_field() -> Result<()> { |
| 5382 | let ir = ir_from_cc( |
| 5383 | r#" |
| 5384 | class Field {}; |
| 5385 | struct Struct final { |
| 5386 | [[no_unique_address]] Field field; |
| 5387 | }; |
| 5388 | "#, |
| 5389 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5390 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5391 | assert_rs_matches!( |
| 5392 | rs_api, |
| 5393 | quote! { |
| 5394 | #[repr(C)] |
| 5395 | pub struct Struct { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5396 | pub(crate) field: [::std::mem::MaybeUninit<u8>; 1], |
Devin Jeanpierre | b69bcae | 2022-02-03 09:45:50 +0000 | [diff] [blame] | 5397 | } |
| 5398 | } |
| 5399 | ); |
| 5400 | Ok(()) |
| 5401 | } |
| 5402 | |
Devin Jeanpierre | c80e624 | 2022-02-03 01:56:40 +0000 | [diff] [blame] | 5403 | #[test] |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5404 | fn test_generate_enum_basic() -> Result<()> { |
| 5405 | let ir = ir_from_cc("enum Color { kRed = 5, kBlue };")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5406 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5407 | assert_rs_matches!( |
| 5408 | rs_api, |
| 5409 | quote! { |
| 5410 | #[repr(transparent)] |
| 5411 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 5412 | pub struct Color(u32); |
| 5413 | impl Color { |
| 5414 | pub const kRed: Color = Color(5); |
| 5415 | pub const kBlue: Color = Color(6); |
| 5416 | } |
| 5417 | impl From<u32> for Color { |
| 5418 | fn from(value: u32) -> Color { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5419 | Color(value) |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5420 | } |
| 5421 | } |
| 5422 | impl From<Color> for u32 { |
| 5423 | fn from(value: Color) -> u32 { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5424 | value.0 |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5425 | } |
| 5426 | } |
| 5427 | } |
| 5428 | ); |
| 5429 | Ok(()) |
| 5430 | } |
| 5431 | |
| 5432 | #[test] |
| 5433 | fn test_generate_scoped_enum_basic() -> Result<()> { |
| 5434 | let ir = ir_from_cc("enum class Color { kRed = -5, kBlue };")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5435 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5436 | assert_rs_matches!( |
| 5437 | rs_api, |
| 5438 | quote! { |
| 5439 | #[repr(transparent)] |
| 5440 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 5441 | pub struct Color(i32); |
| 5442 | impl Color { |
| 5443 | pub const kRed: Color = Color(-5); |
| 5444 | pub const kBlue: Color = Color(-4); |
| 5445 | } |
| 5446 | impl From<i32> for Color { |
| 5447 | fn from(value: i32) -> Color { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5448 | Color(value) |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5449 | } |
| 5450 | } |
| 5451 | impl From<Color> for i32 { |
| 5452 | fn from(value: Color) -> i32 { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5453 | value.0 |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5454 | } |
| 5455 | } |
| 5456 | } |
| 5457 | ); |
| 5458 | Ok(()) |
| 5459 | } |
| 5460 | |
| 5461 | #[test] |
| 5462 | fn test_generate_enum_with_64_bit_signed_vals() -> Result<()> { |
| 5463 | let ir = ir_from_cc( |
| 5464 | "enum Color : long { kViolet = -9223372036854775807 - 1LL, kRed = -5, kBlue, kGreen = 3, kMagenta = 9223372036854775807 };", |
| 5465 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5466 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5467 | assert_rs_matches!( |
| 5468 | rs_api, |
| 5469 | quote! { |
| 5470 | #[repr(transparent)] |
| 5471 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 5472 | pub struct Color(i64); |
| 5473 | impl Color { |
| 5474 | pub const kViolet: Color = Color(-9223372036854775808); |
| 5475 | pub const kRed: Color = Color(-5); |
| 5476 | pub const kBlue: Color = Color(-4); |
| 5477 | pub const kGreen: Color = Color(3); |
| 5478 | pub const kMagenta: Color = Color(9223372036854775807); |
| 5479 | } |
| 5480 | impl From<i64> for Color { |
| 5481 | fn from(value: i64) -> Color { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5482 | Color(value) |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5483 | } |
| 5484 | } |
| 5485 | impl From<Color> for i64 { |
| 5486 | fn from(value: Color) -> i64 { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5487 | value.0 |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5488 | } |
| 5489 | } |
| 5490 | } |
| 5491 | ); |
| 5492 | Ok(()) |
| 5493 | } |
| 5494 | |
| 5495 | #[test] |
| 5496 | fn test_generate_enum_with_64_bit_unsigned_vals() -> Result<()> { |
| 5497 | let ir = ir_from_cc( |
| 5498 | "enum Color: unsigned long { kRed, kBlue, kLimeGreen = 18446744073709551615 };", |
| 5499 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5500 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5501 | assert_rs_matches!( |
| 5502 | rs_api, |
| 5503 | quote! { |
| 5504 | #[repr(transparent)] |
| 5505 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 5506 | pub struct Color(u64); |
| 5507 | impl Color { |
| 5508 | pub const kRed: Color = Color(0); |
| 5509 | pub const kBlue: Color = Color(1); |
| 5510 | pub const kLimeGreen: Color = Color(18446744073709551615); |
| 5511 | } |
| 5512 | impl From<u64> for Color { |
| 5513 | fn from(value: u64) -> Color { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5514 | Color(value) |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5515 | } |
| 5516 | } |
| 5517 | impl From<Color> for u64 { |
| 5518 | fn from(value: Color) -> u64 { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5519 | value.0 |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | } |
| 5523 | ); |
| 5524 | Ok(()) |
| 5525 | } |
| 5526 | |
| 5527 | #[test] |
| 5528 | fn test_generate_enum_with_32_bit_signed_vals() -> Result<()> { |
| 5529 | let ir = ir_from_cc( |
| 5530 | "enum Color { kViolet = -2147483647 - 1, kRed = -5, kBlue, kGreen = 3, kMagenta = 2147483647 };", |
| 5531 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5532 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5533 | assert_rs_matches!( |
| 5534 | rs_api, |
| 5535 | quote! { |
| 5536 | #[repr(transparent)] |
| 5537 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 5538 | pub struct Color(i32); |
| 5539 | impl Color { |
| 5540 | pub const kViolet: Color = Color(-2147483648); |
| 5541 | pub const kRed: Color = Color(-5); |
| 5542 | pub const kBlue: Color = Color(-4); |
| 5543 | pub const kGreen: Color = Color(3); |
| 5544 | pub const kMagenta: Color = Color(2147483647); |
| 5545 | } |
| 5546 | impl From<i32> for Color { |
| 5547 | fn from(value: i32) -> Color { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5548 | Color(value) |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5549 | } |
| 5550 | } |
| 5551 | impl From<Color> for i32 { |
| 5552 | fn from(value: Color) -> i32 { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5553 | value.0 |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5554 | } |
| 5555 | } |
| 5556 | } |
| 5557 | ); |
| 5558 | Ok(()) |
| 5559 | } |
| 5560 | |
| 5561 | #[test] |
| 5562 | fn test_generate_enum_with_32_bit_unsigned_vals() -> Result<()> { |
| 5563 | let ir = ir_from_cc("enum Color: unsigned int { kRed, kBlue, kLimeGreen = 4294967295 };")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5564 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5565 | assert_rs_matches!( |
| 5566 | rs_api, |
| 5567 | quote! { |
| 5568 | #[repr(transparent)] |
| 5569 | #[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)] |
| 5570 | pub struct Color(u32); |
| 5571 | impl Color { |
| 5572 | pub const kRed: Color = Color(0); |
| 5573 | pub const kBlue: Color = Color(1); |
| 5574 | pub const kLimeGreen: Color = Color(4294967295); |
| 5575 | } |
| 5576 | impl From<u32> for Color { |
| 5577 | fn from(value: u32) -> Color { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5578 | Color(value) |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5579 | } |
| 5580 | } |
| 5581 | impl From<Color> for u32 { |
| 5582 | fn from(value: Color) -> u32 { |
Marcel Hlopko | 872b41a | 2022-05-10 01:49:33 -0700 | [diff] [blame] | 5583 | value.0 |
Teddy Katz | 76fa42b | 2022-02-23 01:22:56 +0000 | [diff] [blame] | 5584 | } |
| 5585 | } |
| 5586 | } |
| 5587 | ); |
| 5588 | Ok(()) |
| 5589 | } |
| 5590 | |
| 5591 | #[test] |
Michael Forster | 409d941 | 2021-10-07 08:35:29 +0000 | [diff] [blame] | 5592 | fn test_doc_comment_func() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5593 | let ir = ir_from_cc( |
| 5594 | " |
| 5595 | // Doc Comment |
| 5596 | // with two lines |
| 5597 | int func();", |
| 5598 | )?; |
Michael Forster | 409d941 | 2021-10-07 08:35:29 +0000 | [diff] [blame] | 5599 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5600 | assert_rs_matches!( |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5601 | generate_bindings_tokens(ir)?.rs_api, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5602 | // leading space is intentional so there is a space between /// and the text of the |
| 5603 | // comment |
| 5604 | quote! { |
| 5605 | #[doc = " Doc Comment\n with two lines"] |
| 5606 | #[inline(always)] |
| 5607 | pub fn func |
| 5608 | } |
Michael Forster | 409d941 | 2021-10-07 08:35:29 +0000 | [diff] [blame] | 5609 | ); |
| 5610 | |
| 5611 | Ok(()) |
| 5612 | } |
| 5613 | |
| 5614 | #[test] |
| 5615 | fn test_doc_comment_record() -> Result<()> { |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5616 | let ir = ir_from_cc( |
| 5617 | "// Doc Comment\n\ |
| 5618 | //\n\ |
| 5619 | // * with bullet\n\ |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 5620 | struct SomeStruct final {\n\ |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5621 | // Field doc\n\ |
| 5622 | int field;\ |
| 5623 | };", |
| 5624 | )?; |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 5625 | |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5626 | assert_rs_matches!( |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5627 | generate_bindings_tokens(ir)?.rs_api, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 5628 | quote! { |
| 5629 | #[doc = " Doc Comment\n \n * with bullet"] |
| 5630 | #[derive(Clone, Copy)] |
| 5631 | #[repr(C)] |
| 5632 | pub struct SomeStruct { |
| 5633 | # [doc = " Field doc"] |
| 5634 | pub field: i32, |
| 5635 | } |
| 5636 | } |
Michael Forster | cc5941a | 2021-10-07 07:12:24 +0000 | [diff] [blame] | 5637 | ); |
Michael Forster | 028800b | 2021-10-05 12:39:59 +0000 | [diff] [blame] | 5638 | Ok(()) |
| 5639 | } |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 5640 | |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 5641 | #[test] |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5642 | fn test_basic_union() -> Result<()> { |
| 5643 | let ir = ir_from_cc( |
| 5644 | r#" |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 5645 | #pragma clang lifetime_elision |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5646 | union SomeUnion { |
| 5647 | int some_field; |
| 5648 | long long some_bigger_field; |
| 5649 | }; |
| 5650 | "#, |
| 5651 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5652 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5653 | |
| 5654 | assert_rs_matches!( |
| 5655 | rs_api, |
| 5656 | quote! { |
| 5657 | #[derive(Clone, Copy)] |
| 5658 | #[repr(C)] |
| 5659 | pub union SomeUnion { |
| 5660 | pub some_field: i32, |
| 5661 | pub some_bigger_field: i64, |
| 5662 | } |
| 5663 | } |
| 5664 | ); |
Marcel Hlopko | 4c29c6f | 2022-05-04 00:49:14 -0700 | [diff] [blame] | 5665 | assert_cc_matches!( |
| 5666 | rs_api_impl, |
| 5667 | quote! { |
| 5668 | extern "C" void __rust_thunk___ZN9SomeUnionC1Ev(union SomeUnion*__this) {...} |
| 5669 | } |
| 5670 | ); |
Marcel Hlopko | 4c29c6f | 2022-05-04 00:49:14 -0700 | [diff] [blame] | 5671 | assert_cc_matches!(rs_api_impl, quote! { static_assert(sizeof(union SomeUnion)==8) }); |
| 5672 | assert_cc_matches!(rs_api_impl, quote! { static_assert(alignof(union SomeUnion)==8) }); |
| 5673 | assert_cc_matches!( |
| 5674 | rs_api_impl, |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 5675 | quote! { static_assert(CRUBIT_OFFSET_OF(some_field, union SomeUnion)==0) } |
Marcel Hlopko | 4c29c6f | 2022-05-04 00:49:14 -0700 | [diff] [blame] | 5676 | ); |
| 5677 | assert_cc_matches!( |
| 5678 | rs_api_impl, |
Lukasz Anforowicz | 30360ba | 2022-05-23 12:15:12 -0700 | [diff] [blame] | 5679 | quote! { static_assert(CRUBIT_OFFSET_OF(some_bigger_field, union SomeUnion)==0) } |
Marcel Hlopko | 4c29c6f | 2022-05-04 00:49:14 -0700 | [diff] [blame] | 5680 | ); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5681 | Ok(()) |
| 5682 | } |
| 5683 | |
| 5684 | #[test] |
Marcel Hlopko | f05621b | 2022-05-25 00:26:06 -0700 | [diff] [blame] | 5685 | fn test_union_with_opaque_field() -> Result<()> { |
| 5686 | let ir = ir_from_cc( |
| 5687 | r#" |
| 5688 | union MyUnion { |
| 5689 | char first_field[56]; |
| 5690 | int second_field; |
| 5691 | }; |
| 5692 | "#, |
| 5693 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5694 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Marcel Hlopko | f05621b | 2022-05-25 00:26:06 -0700 | [diff] [blame] | 5695 | |
| 5696 | assert_rs_matches!( |
| 5697 | rs_api, |
| 5698 | quote! { |
| 5699 | #[repr(C, align(4))] |
| 5700 | pub union MyUnion { ... |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5701 | first_field: [::std::mem::MaybeUninit<u8>; 56], |
Marcel Hlopko | f05621b | 2022-05-25 00:26:06 -0700 | [diff] [blame] | 5702 | pub second_field: i32, |
| 5703 | } |
| 5704 | } |
| 5705 | ); |
| 5706 | |
| 5707 | assert_rs_matches!( |
| 5708 | rs_api, |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5709 | quote! { const _: () = assert!(::std::mem::size_of::<crate::MyUnion>() == 56); } |
Marcel Hlopko | f05621b | 2022-05-25 00:26:06 -0700 | [diff] [blame] | 5710 | ); |
| 5711 | assert_rs_matches!( |
| 5712 | rs_api, |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5713 | quote! { const _: () = assert!(::std::mem::align_of::<crate::MyUnion>() == 4); } |
Marcel Hlopko | f05621b | 2022-05-25 00:26:06 -0700 | [diff] [blame] | 5714 | ); |
| 5715 | Ok(()) |
| 5716 | } |
| 5717 | |
| 5718 | #[test] |
Marcel Hlopko | fa9a395 | 2022-05-10 01:34:13 -0700 | [diff] [blame] | 5719 | // TODO(https://github.com/Gilnaa/memoffset/issues/66): generate assertions for unions once |
| 5720 | // offsetof supports them. |
| 5721 | fn test_currently_no_offset_assertions_for_unions() -> Result<()> { |
| 5722 | let ir = ir_from_cc( |
| 5723 | r#" |
| 5724 | union SomeUnion { |
| 5725 | int some_field; |
| 5726 | long long some_bigger_field; |
| 5727 | }; |
| 5728 | "#, |
| 5729 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5730 | let BindingsTokens { rs_api, .. } = generate_bindings_tokens(ir)?; |
Marcel Hlopko | fa9a395 | 2022-05-10 01:34:13 -0700 | [diff] [blame] | 5731 | |
| 5732 | assert_rs_not_matches!(rs_api, quote! { offset_of! }); |
| 5733 | Ok(()) |
| 5734 | } |
| 5735 | |
| 5736 | #[test] |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5737 | fn test_union_with_private_fields() -> Result<()> { |
| 5738 | let ir = ir_from_cc( |
| 5739 | r#" |
| 5740 | union SomeUnionWithPrivateFields { |
| 5741 | public: |
| 5742 | int public_field; |
| 5743 | private: |
| 5744 | long long private_field; |
| 5745 | }; |
| 5746 | "#, |
| 5747 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5748 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5749 | |
| 5750 | assert_rs_matches!( |
| 5751 | rs_api, |
| 5752 | quote! { |
| 5753 | #[derive(Clone, Copy)] |
Lukasz Anforowicz | df8fcae | 2022-06-02 14:54:43 -0700 | [diff] [blame] | 5754 | #[repr(C, align(8))] |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5755 | pub union SomeUnionWithPrivateFields { |
| 5756 | pub public_field: i32, |
Lukasz Anforowicz | df8fcae | 2022-06-02 14:54:43 -0700 | [diff] [blame] | 5757 | #[doc = " Reason for representing this field as a blob of bytes:\n Types of non-public C++ fields can be elided away"] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5758 | pub(crate) private_field: [::std::mem::MaybeUninit<u8>; 8], |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | ); |
| 5762 | |
| 5763 | assert_rs_matches!( |
| 5764 | rs_api, |
| 5765 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5766 | const _: () = assert!(::std::mem::size_of::<crate::SomeUnionWithPrivateFields>() == 8); |
| 5767 | const _: () = assert!(::std::mem::align_of::<crate::SomeUnionWithPrivateFields>() == 8); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5768 | const _: () = { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 5769 | static_assertions::assert_impl_all!(crate::SomeUnionWithPrivateFields: Clone); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5770 | }; |
| 5771 | const _: () = { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 5772 | static_assertions::assert_impl_all!(crate::SomeUnionWithPrivateFields: Copy); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5773 | }; |
| 5774 | const _: () = { |
Lukasz Anforowicz | b4d1778 | 2022-07-07 08:09:13 -0700 | [diff] [blame] | 5775 | static_assertions::assert_not_impl_any!(crate::SomeUnionWithPrivateFields: Drop); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5776 | }; |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5777 | } |
| 5778 | ); |
| 5779 | Ok(()) |
| 5780 | } |
| 5781 | |
| 5782 | #[test] |
Marcel Hlopko | 4546573 | 2022-05-24 00:51:04 -0700 | [diff] [blame] | 5783 | fn test_nontrivial_unions() -> Result<()> { |
| 5784 | let ir = ir_from_cc_dependency( |
| 5785 | r#" |
| 5786 | union UnionWithNontrivialField { |
| 5787 | NonTrivialStruct my_field; |
| 5788 | }; |
| 5789 | "#, |
| 5790 | r#" |
| 5791 | struct NonTrivialStruct { |
| 5792 | NonTrivialStruct(NonTrivialStruct&&); |
| 5793 | }; |
| 5794 | "#, |
| 5795 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5796 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Marcel Hlopko | 4546573 | 2022-05-24 00:51:04 -0700 | [diff] [blame] | 5797 | |
| 5798 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Copy ... )}); |
| 5799 | assert_rs_not_matches!(rs_api, quote! {derive ( ... Clone ... )}); |
Devin Jeanpierre | 190b90a | 2022-05-24 06:00:34 -0700 | [diff] [blame] | 5800 | assert_rs_matches!( |
| 5801 | rs_api, |
| 5802 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5803 | #[::ctor::recursively_pinned] |
Devin Jeanpierre | 190b90a | 2022-05-24 06:00:34 -0700 | [diff] [blame] | 5804 | #[repr(C)] |
| 5805 | pub union UnionWithNontrivialField { ... } |
| 5806 | } |
| 5807 | ); |
Marcel Hlopko | 4546573 | 2022-05-24 00:51:04 -0700 | [diff] [blame] | 5808 | Ok(()) |
| 5809 | } |
| 5810 | |
| 5811 | #[test] |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 5812 | fn test_empty_struct() -> Result<()> { |
| 5813 | let ir = ir_from_cc( |
| 5814 | r#" |
| 5815 | struct EmptyStruct final {}; |
| 5816 | "#, |
| 5817 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5818 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 5819 | |
| 5820 | assert_rs_matches!( |
| 5821 | rs_api, |
| 5822 | quote! { |
| 5823 | #[derive(Clone, Copy)] |
| 5824 | #[repr(C)] |
| 5825 | pub struct EmptyStruct { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5826 | __non_field_data: [::std::mem::MaybeUninit<u8>; 1], |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 5827 | } |
| 5828 | } |
| 5829 | ); |
| 5830 | |
| 5831 | assert_rs_matches!( |
| 5832 | rs_api, |
| 5833 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5834 | const _: () = assert!(::std::mem::size_of::<crate::EmptyStruct>() == 1); |
| 5835 | const _: () = assert!(::std::mem::align_of::<crate::EmptyStruct>() == 1); |
Devin Jeanpierre | 1221c2a | 2022-05-05 22:36:22 -0700 | [diff] [blame] | 5836 | } |
| 5837 | ); |
| 5838 | |
| 5839 | Ok(()) |
| 5840 | } |
| 5841 | |
| 5842 | #[test] |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5843 | fn test_empty_union() -> Result<()> { |
| 5844 | let ir = ir_from_cc( |
| 5845 | r#" |
| 5846 | union EmptyUnion {}; |
| 5847 | "#, |
| 5848 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5849 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5850 | |
| 5851 | assert_rs_matches!( |
| 5852 | rs_api, |
| 5853 | quote! { |
| 5854 | #[derive(Clone, Copy)] |
| 5855 | #[repr(C)] |
| 5856 | pub union EmptyUnion { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5857 | __non_field_data: [::std::mem::MaybeUninit<u8>; 1], |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | ); |
| 5861 | |
| 5862 | assert_rs_matches!( |
| 5863 | rs_api, |
| 5864 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5865 | const _: () = assert!(::std::mem::size_of::<crate::EmptyUnion>() == 1); |
| 5866 | const _: () = assert!(::std::mem::align_of::<crate::EmptyUnion>() == 1); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5867 | } |
| 5868 | ); |
| 5869 | |
| 5870 | Ok(()) |
| 5871 | } |
| 5872 | |
| 5873 | #[test] |
| 5874 | fn test_union_field_with_nontrivial_destructor() -> Result<()> { |
| 5875 | let ir = ir_from_cc( |
| 5876 | r#" |
| 5877 | struct NontrivialStruct { ~NontrivialStruct(); }; |
| 5878 | union UnionWithNontrivialField { |
| 5879 | int trivial_field; |
| 5880 | NontrivialStruct nontrivial_field; |
| 5881 | }; |
| 5882 | "#, |
| 5883 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5884 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5885 | |
| 5886 | assert_rs_matches!( |
| 5887 | rs_api, |
| 5888 | quote! { |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5889 | #[repr(C)] |
| 5890 | pub union UnionWithNontrivialField { |
| 5891 | pub trivial_field: i32, |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5892 | pub nontrivial_field: ::std::mem::ManuallyDrop<crate::NontrivialStruct>, |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5893 | } |
| 5894 | } |
| 5895 | ); |
| 5896 | |
| 5897 | assert_rs_matches!( |
| 5898 | rs_api, |
| 5899 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5900 | const _: () = assert!(::std::mem::size_of::<crate::UnionWithNontrivialField>() == 4); |
| 5901 | const _: () = assert!(::std::mem::align_of::<crate::UnionWithNontrivialField>() == 4); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5902 | } |
| 5903 | ); |
| 5904 | Ok(()) |
| 5905 | } |
| 5906 | |
| 5907 | #[test] |
| 5908 | fn test_union_with_constructors() -> Result<()> { |
| 5909 | let ir = ir_from_cc( |
| 5910 | r#" |
| 5911 | #pragma clang lifetime_elision |
| 5912 | union UnionWithDefaultConstructors { |
| 5913 | int a; |
| 5914 | }; |
| 5915 | "#, |
| 5916 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5917 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5918 | |
| 5919 | assert_rs_matches!( |
| 5920 | rs_api, |
| 5921 | quote! { |
| 5922 | #[derive(Clone, Copy)] |
| 5923 | #[repr(C)] |
| 5924 | pub union UnionWithDefaultConstructors { |
| 5925 | pub a: i32, |
| 5926 | } |
| 5927 | } |
| 5928 | ); |
| 5929 | |
| 5930 | assert_rs_matches!( |
| 5931 | rs_api, |
| 5932 | quote! { |
| 5933 | impl Default for UnionWithDefaultConstructors { |
| 5934 | #[inline(always)] |
| 5935 | fn default() -> Self { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5936 | let mut tmp = ::std::mem::MaybeUninit::<Self>::zeroed(); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5937 | unsafe { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 5938 | crate::detail::__rust_thunk___ZN28UnionWithDefaultConstructorsC1Ev(&mut tmp); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5939 | tmp.assume_init() |
| 5940 | } |
| 5941 | } |
| 5942 | } |
| 5943 | } |
| 5944 | ); |
| 5945 | |
| 5946 | assert_rs_matches!( |
| 5947 | rs_api, |
| 5948 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5949 | impl<'b> From<::ctor::RvalueReference<'b, crate::UnionWithDefaultConstructors>> for UnionWithDefaultConstructors { |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5950 | #[inline(always)] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 5951 | fn from(__param_0: ::ctor::RvalueReference<'b, crate::UnionWithDefaultConstructors>) -> Self { |
| 5952 | let mut tmp = ::std::mem::MaybeUninit::<Self>::zeroed(); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5953 | unsafe { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 5954 | crate::detail::__rust_thunk___ZN28UnionWithDefaultConstructorsC1EOS_(&mut tmp, __param_0); |
Teddy Katz | d2cd142 | 2022-04-04 09:41:33 -0700 | [diff] [blame] | 5955 | tmp.assume_init() |
| 5956 | } |
| 5957 | } |
| 5958 | } |
| 5959 | } |
| 5960 | ); |
| 5961 | |
| 5962 | Ok(()) |
| 5963 | } |
| 5964 | |
| 5965 | #[test] |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 5966 | fn test_unambiguous_public_bases() -> Result<()> { |
| 5967 | let ir = ir_from_cc_dependency( |
| 5968 | " |
| 5969 | struct VirtualBase {}; |
| 5970 | struct PrivateBase {}; |
| 5971 | struct ProtectedBase {}; |
| 5972 | struct UnambiguousPublicBase {}; |
| 5973 | struct AmbiguousPublicBase {}; |
| 5974 | struct MultipleInheritance : UnambiguousPublicBase, AmbiguousPublicBase {}; |
| 5975 | struct Derived : private PrivateBase, protected ProtectedBase, MultipleInheritance, AmbiguousPublicBase, virtual VirtualBase {}; |
| 5976 | ", |
| 5977 | "", |
| 5978 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 5979 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 5980 | assert_rs_matches!( |
| 5981 | rs_api, |
| 5982 | quote! { |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 5983 | unsafe impl oops::Inherits<crate::VirtualBase> for crate::Derived { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 5984 | unsafe fn upcast_ptr(derived: *const Self) -> *const crate::VirtualBase { |
Lukasz Anforowicz | 3f13397 | 2022-09-01 09:01:02 -0700 | [diff] [blame] | 5985 | crate::detail::__crubit_dynamic_upcast__7Derived__to__11VirtualBase(derived) |
Devin Jeanpierre | f99db3e | 2022-04-27 23:10:33 -0700 | [diff] [blame] | 5986 | } |
| 5987 | } |
| 5988 | } |
| 5989 | ); |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 5990 | assert_rs_matches!( |
| 5991 | rs_api, |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 5992 | quote! { unsafe impl oops::Inherits<crate::UnambiguousPublicBase> for crate::Derived } |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 5993 | ); |
| 5994 | assert_rs_matches!( |
| 5995 | rs_api, |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 5996 | quote! { unsafe impl oops::Inherits<crate::MultipleInheritance> for crate::Derived } |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 5997 | ); |
| 5998 | assert_rs_not_matches!( |
| 5999 | rs_api, |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 6000 | quote! {unsafe impl oops::Inherits<crate::PrivateBase> for crate::Derived} |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 6001 | ); |
| 6002 | assert_rs_not_matches!( |
| 6003 | rs_api, |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 6004 | quote! {unsafe impl oops::Inherits<crate::ProtectedBase> for crate::Derived} |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 6005 | ); |
| 6006 | assert_rs_not_matches!( |
| 6007 | rs_api, |
Lukasz Anforowicz | 90bb746 | 2022-09-01 08:13:28 -0700 | [diff] [blame] | 6008 | quote! {unsafe impl oops::Inherits<crate::AmbiguousPublicBase> for crate::Derived} |
Devin Jeanpierre | b368e68 | 2022-05-03 02:23:44 -0700 | [diff] [blame] | 6009 | ); |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 6010 | Ok(()) |
| 6011 | } |
| 6012 | |
| 6013 | /// Contrary to intuitions: a base class conversion is ambiguous even if the |
| 6014 | /// ambiguity is from a private base class cast that you can't even |
| 6015 | /// perform. |
| 6016 | /// |
| 6017 | /// Explanation (courtesy James Dennett): |
| 6018 | /// |
| 6019 | /// > Once upon a time, there was a rule in C++ that changing all access |
| 6020 | /// > specifiers to "public" would not change the meaning of code. |
| 6021 | /// > That's no longer true, but some of its effects can still be seen. |
| 6022 | /// |
| 6023 | /// So, we need to be sure to not allow casting to privately-ambiguous |
| 6024 | /// bases. |
| 6025 | #[test] |
| 6026 | fn test_unambiguous_public_bases_private_ambiguity() -> Result<()> { |
| 6027 | let ir = ir_from_cc_dependency( |
| 6028 | " |
| 6029 | struct Base {}; |
| 6030 | struct Intermediate : public Base {}; |
| 6031 | struct Derived : Base, private Intermediate {}; |
| 6032 | ", |
| 6033 | "", |
| 6034 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6035 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6036 | assert_rs_not_matches!( |
| 6037 | rs_api, |
| 6038 | quote! { unsafe impl oops::Inherits<crate::Base> for Derived } |
| 6039 | ); |
Devin Jeanpierre | 5677702 | 2022-02-03 01:57:15 +0000 | [diff] [blame] | 6040 | Ok(()) |
| 6041 | } |
| 6042 | |
| 6043 | #[test] |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 6044 | fn test_virtual_thunk() -> Result<()> { |
| 6045 | let ir = ir_from_cc("struct Polymorphic { virtual void Foo(); };")?; |
| 6046 | |
| 6047 | assert_cc_matches!( |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6048 | generate_bindings_tokens(ir)?.rs_api_impl, |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 6049 | quote! { |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 6050 | extern "C" void __rust_thunk___ZN11Polymorphic3FooEv(struct Polymorphic * __this) |
Devin Jeanpierre | 96839c1 | 2021-12-14 00:27:38 +0000 | [diff] [blame] | 6051 | } |
| 6052 | ); |
| 6053 | Ok(()) |
| 6054 | } |
| 6055 | |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 6056 | #[test] |
| 6057 | fn test_custom_abi_thunk() -> Result<()> { |
| 6058 | let ir = ir_from_cc( |
| 6059 | r#" |
| 6060 | float f_vectorcall_calling_convention(float p1, float p2) [[clang::vectorcall]]; |
| 6061 | double f_c_calling_convention(double p1, double p2); |
| 6062 | "#, |
| 6063 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6064 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 6065 | assert_rs_matches!( |
| 6066 | rs_api, |
| 6067 | quote! { |
| 6068 | #[inline(always)] |
| 6069 | pub fn f_vectorcall_calling_convention(p1: f32, p2: f32) -> f32 { |
| 6070 | unsafe { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 6071 | crate::detail::__rust_thunk___Z31f_vectorcall_calling_conventionff(p1, p2) |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 6072 | } |
| 6073 | } |
| 6074 | } |
| 6075 | ); |
| 6076 | assert_rs_matches!( |
| 6077 | rs_api, |
| 6078 | quote! { |
| 6079 | #[inline(always)] |
| 6080 | pub fn f_c_calling_convention(p1: f64, p2: f64) -> f64 { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 6081 | unsafe { crate::detail::__rust_thunk___Z22f_c_calling_conventiondd(p1, p2) } |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 6082 | } |
| 6083 | } |
| 6084 | ); |
| 6085 | // `link_name` (i.e. no thunk) for `f_c_calling_convention`. No |
| 6086 | // `link_name` (i.e. indicates presence of a thunk) for |
| 6087 | // `f_vectorcall_calling_convention`. |
| 6088 | assert_rs_matches!( |
| 6089 | rs_api, |
| 6090 | quote! { |
| 6091 | mod detail { |
| 6092 | #[allow(unused_imports)] |
| 6093 | use super::*; |
| 6094 | extern "C" { |
| 6095 | pub(crate) fn __rust_thunk___Z31f_vectorcall_calling_conventionff( |
| 6096 | p1: f32, p2: f32) -> f32; |
| 6097 | #[link_name = "_Z22f_c_calling_conventiondd"] |
| 6098 | pub(crate) fn __rust_thunk___Z22f_c_calling_conventiondd( |
| 6099 | p1: f64, p2: f64) -> f64; |
| 6100 | } |
| 6101 | } |
| 6102 | } |
| 6103 | ); |
| 6104 | // C++ thunk needed for `f_vectorcall_calling_convention`. |
| 6105 | assert_cc_matches!( |
| 6106 | rs_api_impl, |
| 6107 | quote! { |
| 6108 | extern "C" float __rust_thunk___Z31f_vectorcall_calling_conventionff( |
| 6109 | float p1, float p2) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 6110 | return f_vectorcall_calling_convention(p1, p2); |
Lukasz Anforowicz | 0b6a6ac | 2022-03-22 22:32:23 +0000 | [diff] [blame] | 6111 | } |
| 6112 | } |
| 6113 | ); |
| 6114 | // No C++ thunk expected for `f_c_calling_convention`. |
| 6115 | assert_cc_not_matches!(rs_api_impl, quote! { f_c_calling_convention }); |
| 6116 | Ok(()) |
| 6117 | } |
| 6118 | |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 6119 | /// A trivially relocatable final struct is safe to use in Rust as normal, |
| 6120 | /// and is Unpin. |
| 6121 | #[test] |
| 6122 | fn test_no_negative_impl_unpin() -> Result<()> { |
| 6123 | let ir = ir_from_cc("struct Trivial final {};")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6124 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6125 | assert_rs_not_matches!(rs_api, quote! {#[::ctor::recursively_pinned]}); |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 6126 | Ok(()) |
| 6127 | } |
| 6128 | |
| 6129 | /// A non-final struct, even if it's trivial, is not usable by mut |
| 6130 | /// reference, and so is !Unpin. |
| 6131 | #[test] |
| 6132 | fn test_negative_impl_unpin_nonfinal() -> Result<()> { |
| 6133 | let ir = ir_from_cc("struct Nonfinal {};")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6134 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6135 | assert_rs_matches!(rs_api, quote! {#[::ctor::recursively_pinned]}); |
Devin Jeanpierre | e6e1665 | 2021-12-22 15:54:46 +0000 | [diff] [blame] | 6136 | Ok(()) |
| 6137 | } |
| 6138 | |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 6139 | /// At the least, a trivial type should have no drop impl if or until we add |
| 6140 | /// empty drop impls. |
| 6141 | #[test] |
| 6142 | fn test_no_impl_drop() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6143 | let ir = ir_from_cc("struct Trivial {};")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6144 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 6145 | assert_rs_not_matches!(rs_api, quote! {impl Drop}); |
| 6146 | assert_rs_not_matches!(rs_api, quote! {impl ::ctor::PinnedDrop}); |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 6147 | Ok(()) |
| 6148 | } |
| 6149 | |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6150 | /// User-defined destructors *must* become Drop impls with ManuallyDrop |
| 6151 | /// fields |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 6152 | #[test] |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6153 | fn test_impl_drop_user_defined_destructor() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6154 | let ir = ir_from_cc( |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 6155 | r#" struct NontrivialStruct { ~NontrivialStruct(); }; |
| 6156 | struct UserDefinedDestructor { |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 6157 | ~UserDefinedDestructor(); |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6158 | int x; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 6159 | NontrivialStruct nts; |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 6160 | };"#, |
| 6161 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6162 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6163 | assert_rs_matches!( |
| 6164 | rs_api, |
| 6165 | quote! { |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 6166 | impl ::ctor::PinnedDrop for UserDefinedDestructor { |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6167 | #[inline(always)] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6168 | unsafe fn pinned_drop<'a>(self: ::std::pin::Pin<&'a mut Self>) { |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 6169 | crate::detail::__rust_thunk___ZN21UserDefinedDestructorD1Ev(self) |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6170 | } |
| 6171 | } |
| 6172 | } |
| 6173 | ); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 6174 | assert_rs_matches!(rs_api, quote! {pub x: i32,}); |
Rosica Dejanovska | accf00b | 2022-04-01 13:28:04 -0700 | [diff] [blame] | 6175 | assert_rs_matches!( |
| 6176 | rs_api, |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6177 | quote! {pub nts: ::std::mem::ManuallyDrop<crate::NontrivialStruct>,} |
Rosica Dejanovska | accf00b | 2022-04-01 13:28:04 -0700 | [diff] [blame] | 6178 | ); |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6179 | Ok(()) |
| 6180 | } |
| 6181 | |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6182 | /// nontrivial types without user-defined destructors should invoke |
| 6183 | /// the C++ destructor to preserve the order of field destructions. |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6184 | #[test] |
| 6185 | fn test_impl_drop_nontrivial_member_destructor() -> Result<()> { |
| 6186 | // TODO(jeanpierreda): This would be cleaner if the UserDefinedDestructor code were |
| 6187 | // omitted. For example, we simulate it so that UserDefinedDestructor |
| 6188 | // comes from another library. |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6189 | let ir = ir_from_cc( |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 6190 | r#"struct UserDefinedDestructor final { |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6191 | ~UserDefinedDestructor(); |
| 6192 | }; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 6193 | struct TrivialStruct final { int i; }; |
| 6194 | struct NontrivialMembers final { |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6195 | UserDefinedDestructor udd; |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 6196 | TrivialStruct ts; |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6197 | int x; |
| 6198 | };"#, |
| 6199 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6200 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6201 | assert_rs_matches!( |
| 6202 | rs_api, |
| 6203 | quote! { |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 6204 | impl ::ctor::PinnedDrop for NontrivialMembers { |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6205 | #[inline(always)] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6206 | unsafe fn pinned_drop<'a>(self: ::std::pin::Pin<&'a mut Self>) { |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 6207 | crate::detail::__rust_thunk___ZN17NontrivialMembersD1Ev(self) |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6208 | } |
| 6209 | } |
| 6210 | } |
| 6211 | ); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 6212 | assert_rs_matches!(rs_api, quote! {pub x: i32,}); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6213 | assert_rs_matches!(rs_api, quote! {pub ts: crate::TrivialStruct,}); |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6214 | assert_rs_matches!( |
| 6215 | rs_api, |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6216 | quote! {pub udd: ::std::mem::ManuallyDrop<crate::UserDefinedDestructor>,} |
Lukasz Anforowicz | 6d55363 | 2022-01-06 21:36:14 +0000 | [diff] [blame] | 6217 | ); |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6218 | Ok(()) |
| 6219 | } |
| 6220 | |
| 6221 | /// Trivial types (at least those that are mapped to Copy rust types) do not |
| 6222 | /// get a Drop impl. |
| 6223 | #[test] |
| 6224 | fn test_impl_drop_trivial() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6225 | let ir = ir_from_cc( |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 6226 | r#"struct Trivial final { |
Devin Jeanpierre | 7e9a1de | 2021-12-03 08:04:22 +0000 | [diff] [blame] | 6227 | ~Trivial() = default; |
| 6228 | int x; |
| 6229 | };"#, |
| 6230 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6231 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6232 | assert_rs_not_matches!(rs_api, quote! {impl Drop}); |
Devin Jeanpierre | 215ee8e | 2022-05-16 16:09:41 -0700 | [diff] [blame] | 6233 | assert_rs_not_matches!(rs_api, quote! {impl ::ctor::PinnedDrop}); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6234 | assert_rs_matches!(rs_api, quote! {pub x: i32}); |
Devin Jeanpierre | e9be70a | 2022-07-25 08:58:54 -0700 | [diff] [blame] | 6235 | assert_cc_not_matches!(rs_api_impl, quote! { std::destroy_at }); |
Devin Jeanpierre | 91de701 | 2021-10-21 12:53:51 +0000 | [diff] [blame] | 6236 | Ok(()) |
| 6237 | } |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 6238 | |
| 6239 | #[test] |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 6240 | fn test_impl_default_explicitly_defaulted_constructor() -> Result<()> { |
| 6241 | let ir = ir_from_cc( |
Lukasz Anforowicz | 9555127 | 2022-01-20 00:02:24 +0000 | [diff] [blame] | 6242 | r#"#pragma clang lifetime_elision |
| 6243 | struct DefaultedConstructor final { |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 6244 | DefaultedConstructor() = default; |
| 6245 | };"#, |
| 6246 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6247 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 6248 | assert_rs_matches!( |
| 6249 | rs_api, |
| 6250 | quote! { |
| 6251 | impl Default for DefaultedConstructor { |
| 6252 | #[inline(always)] |
| 6253 | fn default() -> Self { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6254 | let mut tmp = ::std::mem::MaybeUninit::<Self>::zeroed(); |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 6255 | unsafe { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 6256 | crate::detail::__rust_thunk___ZN20DefaultedConstructorC1Ev(&mut tmp); |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 6257 | tmp.assume_init() |
| 6258 | } |
| 6259 | } |
| 6260 | } |
| 6261 | } |
| 6262 | ); |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 6263 | assert_cc_matches!( |
| 6264 | rs_api_impl, |
| 6265 | quote! { |
| 6266 | extern "C" void __rust_thunk___ZN20DefaultedConstructorC1Ev( |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 6267 | struct DefaultedConstructor* __this) { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 6268 | crubit::construct_at(__this); |
Lukasz Anforowicz | e643ec9 | 2021-12-22 15:45:15 +0000 | [diff] [blame] | 6269 | } |
| 6270 | } |
| 6271 | ); |
| 6272 | Ok(()) |
| 6273 | } |
| 6274 | |
| 6275 | #[test] |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 6276 | fn test_impl_clone_that_propagates_lifetime() -> Result<()> { |
| 6277 | // This test covers the case where a single lifetime applies to 1) |
| 6278 | // the `__this` parameter and 2) other constructor parameters. For |
| 6279 | // example, maybe the newly constructed object needs to have the |
| 6280 | // same lifetime as the constructor's parameter. (This might require |
| 6281 | // annotating the whole C++ struct with a lifetime, so maybe the |
| 6282 | // example below is not fully realistic/accurate...). |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 6283 | let ir = ir_from_cc(&with_lifetime_macros( |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 6284 | r#"#pragma clang lifetime_elision |
| 6285 | struct Foo final { |
Martin Brænne | e5ba6b6 | 2022-06-23 07:38:40 -0700 | [diff] [blame] | 6286 | Foo(const int& $a i) $a; |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 6287 | };"#, |
Martin Brænne | e5ba6b6 | 2022-06-23 07:38:40 -0700 | [diff] [blame] | 6288 | ))?; |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 6289 | let ctor: &Func = ir |
| 6290 | .items() |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 6291 | .filter_map(|item| match item { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 6292 | Item::Func(func) => Some(&**func), |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 6293 | _ => None, |
| 6294 | }) |
| 6295 | .find(|f| { |
| 6296 | matches!(&f.name, UnqualifiedIdentifier::Constructor) |
| 6297 | && f.params.get(1).map(|p| p.identifier.identifier == "i").unwrap_or_default() |
| 6298 | }) |
| 6299 | .unwrap(); |
| 6300 | { |
| 6301 | // Double-check that the test scenario set up above uses the same lifetime |
| 6302 | // for both of the constructor's parameters: `__this` and `i`. |
| 6303 | assert_eq!(ctor.params.len(), 2); |
| 6304 | let this_lifetime: LifetimeId = |
| 6305 | *ctor.params[0].type_.rs_type.lifetime_args.first().unwrap(); |
| 6306 | let i_lifetime: LifetimeId = |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 6307 | *ctor.params[1].type_.rs_type.lifetime_args.first().unwrap(); |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 6308 | assert_eq!(i_lifetime, this_lifetime); |
| 6309 | } |
| 6310 | |
| 6311 | // Before cl/423346348 the generated Rust code would incorrectly look |
| 6312 | // like this (note the mismatched 'a and 'b lifetimes): |
| 6313 | // fn from<'b>(i: &'a i32) -> Self |
| 6314 | // After this CL, this scenario will result in an explicit error. |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6315 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | d7b4810 | 2022-03-31 04:15:03 -0700 | [diff] [blame] | 6316 | assert_rs_not_matches!(rs_api, quote! {impl From}); |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 6317 | assert_rs_matches!(rs_api, { |
| 6318 | let txt = "google3/ir_from_cc_virtual_header.h;l=34\n\ |
| 6319 | Error while generating bindings for item 'Foo::Foo':\n\ |
| 6320 | The lifetime of `__this` is \ |
| 6321 | unexpectedly also used by another parameter: Lifetime(\"a\")"; |
| 6322 | quote! { __COMMENT__ #txt } |
| 6323 | }); |
Lukasz Anforowicz | 326c4e4 | 2022-01-27 14:43:00 +0000 | [diff] [blame] | 6324 | Ok(()) |
| 6325 | } |
| 6326 | |
| 6327 | #[test] |
Lukasz Anforowicz | 9bab835 | 2021-12-22 17:35:31 +0000 | [diff] [blame] | 6328 | fn test_impl_default_non_trivial_struct() -> Result<()> { |
| 6329 | let ir = ir_from_cc( |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 6330 | r#"#pragma clang lifetime_elision |
| 6331 | struct NonTrivialStructWithConstructors final { |
Lukasz Anforowicz | 9bab835 | 2021-12-22 17:35:31 +0000 | [diff] [blame] | 6332 | NonTrivialStructWithConstructors(); |
| 6333 | ~NonTrivialStructWithConstructors(); // Non-trivial |
| 6334 | };"#, |
| 6335 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6336 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 9bab835 | 2021-12-22 17:35:31 +0000 | [diff] [blame] | 6337 | assert_rs_not_matches!(rs_api, quote! {impl Default}); |
| 6338 | Ok(()) |
| 6339 | } |
| 6340 | |
| 6341 | #[test] |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 6342 | fn test_impl_from_for_explicit_conversion_constructor() -> Result<()> { |
| 6343 | let ir = ir_from_cc( |
| 6344 | r#"#pragma clang lifetime_elision |
| 6345 | struct SomeStruct final { |
| 6346 | explicit SomeStruct(int i); |
| 6347 | };"#, |
| 6348 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6349 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 6350 | // As discussed in b/214020567 for now we only generate `From::from` bindings |
| 6351 | // for *implicit* C++ conversion constructors. |
| 6352 | assert_rs_not_matches!(rs_api, quote! {impl From}); |
| 6353 | Ok(()) |
| 6354 | } |
| 6355 | |
| 6356 | #[test] |
| 6357 | fn test_impl_from_for_implicit_conversion_constructor() -> Result<()> { |
| 6358 | let ir = ir_from_cc( |
| 6359 | r#"#pragma clang lifetime_elision |
| 6360 | struct SomeStruct final { |
| 6361 | SomeStruct(int i); // implicit - no `explicit` keyword |
| 6362 | };"#, |
| 6363 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6364 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 6365 | // As discussed in b/214020567 we generate `From::from` bindings for |
| 6366 | // *implicit* C++ conversion constructors. |
| 6367 | assert_rs_matches!( |
| 6368 | rs_api, |
| 6369 | quote! { |
| 6370 | impl From<i32> for SomeStruct { |
| 6371 | #[inline(always)] |
| 6372 | fn from(i: i32) -> Self { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6373 | let mut tmp = ::std::mem::MaybeUninit::<Self>::zeroed(); |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 6374 | unsafe { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 6375 | crate::detail::__rust_thunk___ZN10SomeStructC1Ei(&mut tmp, i); |
Lukasz Anforowicz | 71716b7 | 2022-01-26 17:05:05 +0000 | [diff] [blame] | 6376 | tmp.assume_init() |
| 6377 | } |
| 6378 | } |
| 6379 | } |
| 6380 | } |
| 6381 | ); |
| 6382 | Ok(()) |
| 6383 | } |
| 6384 | |
| 6385 | #[test] |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 6386 | fn test_impl_from_for_implicit_conversion_from_reference() -> Result<()> { |
| 6387 | let ir = ir_from_cc( |
| 6388 | r#"#pragma clang lifetime_elision |
| 6389 | struct SomeOtherStruct final { int i; }; |
| 6390 | struct StructUnderTest final { |
| 6391 | StructUnderTest(const SomeOtherStruct& other); // implicit - no `explicit` keyword |
| 6392 | };"#, |
| 6393 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6394 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 6395 | // This is a regression test for b/223800038: We want to ensure that the |
| 6396 | // code says `impl<'b>` (instead of incorrectly declaring that lifetime |
| 6397 | // in `fn from<'b>`). |
| 6398 | assert_rs_matches!( |
| 6399 | rs_api, |
| 6400 | quote! { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6401 | impl<'b> From<&'b crate::SomeOtherStruct> for StructUnderTest { |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 6402 | #[inline(always)] |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6403 | fn from(other: &'b crate::SomeOtherStruct) -> Self { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6404 | let mut tmp = ::std::mem::MaybeUninit::<Self>::zeroed(); |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 6405 | unsafe { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 6406 | crate::detail::__rust_thunk___ZN15StructUnderTestC1ERK15SomeOtherStruct( |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 6407 | &mut tmp, other); |
| 6408 | tmp.assume_init() |
| 6409 | } |
| 6410 | } |
| 6411 | } |
| 6412 | }, |
| 6413 | ); |
| 6414 | Ok(()) |
| 6415 | } |
| 6416 | |
Devin Jeanpierre | 5ff493e | 2022-06-08 12:40:23 -0700 | [diff] [blame] | 6417 | /// Methods with missing lifetimes for `self` should give a useful error |
| 6418 | /// message. |
| 6419 | #[test] |
| 6420 | fn test_eq_nolifetime() -> Result<()> { |
| 6421 | // Missing lifetimes currently only causes hard errors for trait impls, |
| 6422 | // not For inherent methods. |
| 6423 | let ir = ir_from_cc("struct SomeStruct{SomeStruct& operator=(const SomeStruct&);};")?; |
| 6424 | |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6425 | let rs_api = rs_tokens_to_formatted_string_for_tests(generate_bindings_tokens(ir)?.rs_api)?; |
Devin Jeanpierre | 5ff493e | 2022-06-08 12:40:23 -0700 | [diff] [blame] | 6426 | assert!(rs_api.contains( |
| 6427 | "// Error while generating bindings for item 'SomeStruct::operator=':\n\ |
| 6428 | // `self` has no lifetime. Use lifetime annotations or \ |
| 6429 | `#pragma clang lifetime_elision` to create bindings for this function." |
| 6430 | )); |
| 6431 | Ok(()) |
| 6432 | } |
| 6433 | |
Lukasz Anforowicz | 5e62378 | 2022-03-14 16:52:23 +0000 | [diff] [blame] | 6434 | #[test] |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 6435 | fn test_impl_eq_for_member_function() -> Result<()> { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6436 | let ir = ir_from_cc( |
| 6437 | r#"#pragma clang lifetime_elision |
| 6438 | struct SomeStruct final { |
| 6439 | inline bool operator==(const SomeStruct& other) const { |
| 6440 | return i == other.i; |
| 6441 | } |
| 6442 | int i; |
| 6443 | };"#, |
| 6444 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6445 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6446 | assert_rs_matches!( |
| 6447 | rs_api, |
| 6448 | quote! { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6449 | impl PartialEq<crate::SomeStruct> for SomeStruct { |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6450 | #[inline(always)] |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6451 | fn eq<'a, 'b>(&'a self, other: &'b crate::SomeStruct) -> bool { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 6452 | unsafe { crate::detail::__rust_thunk___ZNK10SomeStructeqERKS_(self, other) } |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6453 | } |
| 6454 | } |
| 6455 | } |
| 6456 | ); |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6457 | assert_cc_matches!( |
| 6458 | rs_api_impl, |
| 6459 | quote! { |
| 6460 | extern "C" bool __rust_thunk___ZNK10SomeStructeqERKS_( |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 6461 | const struct SomeStruct* __this, const struct SomeStruct* other) { |
| 6462 | return __this->operator==(*other); |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6463 | } |
| 6464 | } |
| 6465 | ); |
| 6466 | Ok(()) |
| 6467 | } |
| 6468 | |
| 6469 | #[test] |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 6470 | fn test_impl_eq_for_free_function() -> Result<()> { |
| 6471 | let ir = ir_from_cc( |
| 6472 | r#"#pragma clang lifetime_elision |
| 6473 | struct SomeStruct final { int i; }; |
| 6474 | bool operator==(const SomeStruct& lhs, const SomeStruct& rhs) { |
| 6475 | return lhs.i == rhs.i; |
| 6476 | }"#, |
| 6477 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6478 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 6479 | assert_rs_matches!( |
| 6480 | rs_api, |
| 6481 | quote! { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6482 | impl PartialEq<crate::SomeStruct> for SomeStruct { |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 6483 | #[inline(always)] |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6484 | fn eq<'a, 'b>(&'a self, rhs: &'b crate::SomeStruct) -> bool { |
Rosica Dejanovska | 68c11ac | 2022-05-12 03:48:59 -0700 | [diff] [blame] | 6485 | unsafe { crate::detail::__rust_thunk___ZeqRK10SomeStructS1_(self, rhs) } |
Lukasz Anforowicz | 732ca64 | 2022-02-03 20:58:38 +0000 | [diff] [blame] | 6486 | } |
| 6487 | } |
| 6488 | } |
| 6489 | ); |
| 6490 | Ok(()) |
| 6491 | } |
| 6492 | |
| 6493 | #[test] |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 6494 | fn test_impl_eq_for_free_function_different_types() -> Result<()> { |
| 6495 | let ir = ir_from_cc( |
| 6496 | r#"#pragma clang lifetime_elision |
| 6497 | struct SomeStruct final { int i; }; |
| 6498 | struct SomeOtherStruct final { int i; }; |
| 6499 | bool operator==(const SomeStruct& lhs, const SomeOtherStruct& rhs) { |
| 6500 | return lhs.i == rhs.i; |
| 6501 | }"#, |
| 6502 | )?; |
| 6503 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6504 | assert_rs_matches!( |
| 6505 | rs_api, |
| 6506 | quote! { |
| 6507 | impl PartialEq<crate::SomeOtherStruct> for SomeStruct { |
| 6508 | #[inline(always)] |
| 6509 | fn eq<'a, 'b>(&'a self, rhs: &'b crate::SomeOtherStruct) -> bool { |
| 6510 | unsafe { crate::detail::__rust_thunk___ZeqRK10SomeStructRK15SomeOtherStruct(self, rhs) } |
| 6511 | } |
| 6512 | } |
| 6513 | } |
| 6514 | ); |
| 6515 | Ok(()) |
| 6516 | } |
| 6517 | |
| 6518 | #[test] |
| 6519 | fn test_impl_eq_for_free_function_by_value() -> Result<()> { |
| 6520 | let ir = ir_from_cc( |
| 6521 | r#"#pragma clang lifetime_elision |
| 6522 | struct SomeStruct final { int i; }; |
| 6523 | bool operator==(SomeStruct lhs, SomeStruct rhs) { |
| 6524 | return lhs.i == rhs.i; |
| 6525 | }"#, |
| 6526 | )?; |
| 6527 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6528 | assert_rs_matches!( |
| 6529 | rs_api, |
| 6530 | quote! { |
| 6531 | impl PartialEq<crate::SomeStruct> for SomeStruct { |
| 6532 | #[inline(always)] |
| 6533 | fn eq(& self, rhs: & crate::SomeStruct) -> bool { |
| 6534 | unsafe { crate::detail::__rust_thunk___Zeq10SomeStructS_(self.clone(), rhs.clone()) } |
| 6535 | } |
| 6536 | } |
| 6537 | } |
| 6538 | ); |
| 6539 | Ok(()) |
| 6540 | } |
| 6541 | |
| 6542 | #[test] |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 6543 | fn test_impl_lt_for_member_function() -> Result<()> { |
| 6544 | let ir = ir_from_cc( |
| 6545 | r#"#pragma clang lifetime_elision |
| 6546 | struct SomeStruct final { |
| 6547 | inline bool operator==(const SomeStruct& other) const { |
| 6548 | return i == other.i; |
| 6549 | } |
| 6550 | inline bool operator<(const SomeStruct& other) const { |
| 6551 | return i < other.i; |
| 6552 | } |
| 6553 | int i; |
| 6554 | };"#, |
| 6555 | )?; |
| 6556 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 6557 | assert_rs_matches!( |
| 6558 | rs_api, |
| 6559 | quote! { |
| 6560 | impl PartialOrd<crate::SomeStruct> for SomeStruct { |
| 6561 | #[inline(always)] |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 6562 | fn partial_cmp(&self, other: &crate::SomeStruct) -> Option<core::cmp::Ordering> { |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 6563 | if self == other { |
| 6564 | return Some(core::cmp::Ordering::Equal); |
| 6565 | } |
| 6566 | if self < other { |
| 6567 | return Some(core::cmp::Ordering::Less); |
| 6568 | } |
| 6569 | if other < self { |
| 6570 | return Some(core::cmp::Ordering::Greater); |
| 6571 | } |
| 6572 | None |
| 6573 | } |
| 6574 | #[inline(always)] |
| 6575 | fn lt<'a, 'b>(&'a self, other: &'b crate::SomeStruct) -> bool { |
| 6576 | unsafe { crate::detail::__rust_thunk___ZNK10SomeStructltERKS_(self, other) } |
| 6577 | } |
| 6578 | } |
| 6579 | } |
| 6580 | ); |
| 6581 | assert_cc_matches!( |
| 6582 | rs_api_impl, |
| 6583 | quote! { |
| 6584 | extern "C" bool __rust_thunk___ZNK10SomeStructltERKS_( |
| 6585 | const struct SomeStruct* __this, const struct SomeStruct* other) { |
| 6586 | return __this->operator<(*other); |
| 6587 | } |
| 6588 | } |
| 6589 | ); |
| 6590 | Ok(()) |
| 6591 | } |
| 6592 | |
| 6593 | #[test] |
| 6594 | fn test_impl_lt_for_free_function() -> Result<()> { |
| 6595 | let ir = ir_from_cc( |
| 6596 | r#"#pragma clang lifetime_elision |
| 6597 | struct SomeStruct final { |
| 6598 | inline bool operator==(const SomeStruct& other) const { |
| 6599 | return i == other.i; |
| 6600 | } |
| 6601 | int i; |
| 6602 | }; |
| 6603 | bool operator<(const SomeStruct& lhs, const SomeStruct& rhs) { |
| 6604 | return lhs.i < rhs.i; |
| 6605 | }"#, |
| 6606 | )?; |
| 6607 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6608 | assert_rs_matches!( |
| 6609 | rs_api, |
| 6610 | quote! { |
| 6611 | impl PartialOrd<crate::SomeStruct> for SomeStruct { |
| 6612 | #[inline(always)] |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 6613 | fn partial_cmp(&self, other: &crate::SomeStruct) -> Option<core::cmp::Ordering> { |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 6614 | if self == other { |
| 6615 | return Some(core::cmp::Ordering::Equal); |
| 6616 | } |
| 6617 | if self < other { |
| 6618 | return Some(core::cmp::Ordering::Less); |
| 6619 | } |
| 6620 | if other < self { |
| 6621 | return Some(core::cmp::Ordering::Greater); |
| 6622 | } |
| 6623 | None |
| 6624 | } |
| 6625 | #[inline(always)] |
| 6626 | fn lt<'a, 'b>(&'a self, rhs: &'b crate::SomeStruct) -> bool { |
| 6627 | unsafe { crate::detail::__rust_thunk___ZltRK10SomeStructS1_(self, rhs) } |
| 6628 | } |
| 6629 | } |
| 6630 | } |
| 6631 | ); |
| 6632 | Ok(()) |
| 6633 | } |
| 6634 | |
| 6635 | #[test] |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 6636 | fn test_impl_lt_for_free_function_by_value() -> Result<()> { |
| 6637 | let ir = ir_from_cc( |
| 6638 | r#"#pragma clang lifetime_elision |
| 6639 | struct SomeStruct final { int i; }; |
| 6640 | bool operator==(SomeStruct lhs, SomeStruct rhs) { |
| 6641 | return lhs.i == rhs.i; |
| 6642 | } |
| 6643 | bool operator<(SomeStruct lhs, SomeStruct rhs) { |
| 6644 | return lhs.i < rhs.i; |
| 6645 | }"#, |
| 6646 | )?; |
| 6647 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6648 | assert_rs_matches!( |
| 6649 | rs_api, |
| 6650 | quote! { |
| 6651 | impl PartialOrd<crate::SomeStruct> for SomeStruct { |
| 6652 | #[inline(always)] |
| 6653 | fn partial_cmp(&self, other: &crate::SomeStruct) -> Option<core::cmp::Ordering> { |
| 6654 | if self == other { |
| 6655 | return Some(core::cmp::Ordering::Equal); |
| 6656 | } |
| 6657 | if self < other { |
| 6658 | return Some(core::cmp::Ordering::Less); |
| 6659 | } |
| 6660 | if other < self { |
| 6661 | return Some(core::cmp::Ordering::Greater); |
| 6662 | } |
| 6663 | None |
| 6664 | } |
| 6665 | #[inline(always)] |
| 6666 | fn lt(& self, rhs: & crate::SomeStruct) -> bool { |
| 6667 | unsafe { crate::detail::__rust_thunk___Zlt10SomeStructS_(self.clone(), rhs.clone()) } |
| 6668 | } |
| 6669 | } |
| 6670 | } |
| 6671 | ); |
| 6672 | Ok(()) |
| 6673 | } |
| 6674 | |
| 6675 | #[test] |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 6676 | fn test_assign() -> Result<()> { |
| 6677 | let ir = ir_from_cc( |
| 6678 | r#" |
| 6679 | #pragma clang lifetime_elision |
| 6680 | struct SomeStruct { |
| 6681 | SomeStruct& operator=(const SomeStruct& other); |
| 6682 | };"#, |
| 6683 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6684 | let BindingsTokens { rs_api, .. } = generate_bindings_tokens(ir)?; |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 6685 | assert_rs_matches!( |
| 6686 | rs_api, |
| 6687 | quote! { |
| 6688 | impl<'b> ::ctor::Assign<&'b crate::SomeStruct> for SomeStruct { |
| 6689 | #[inline(always)] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6690 | fn assign<'a>(self: ::std::pin::Pin<&'a mut Self>, other: &'b crate::SomeStruct) { |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 6691 | unsafe { |
| 6692 | crate::detail::__rust_thunk___ZN10SomeStructaSERKS_(self, other); |
| 6693 | } |
| 6694 | } |
| 6695 | } |
| 6696 | } |
| 6697 | ); |
| 6698 | Ok(()) |
| 6699 | } |
| 6700 | |
| 6701 | #[test] |
| 6702 | fn test_assign_nonreference_other() -> Result<()> { |
| 6703 | let ir = ir_from_cc( |
| 6704 | r#" |
| 6705 | #pragma clang lifetime_elision |
| 6706 | struct SomeStruct { |
| 6707 | SomeStruct& operator=(int other); |
| 6708 | };"#, |
| 6709 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6710 | let BindingsTokens { rs_api, .. } = generate_bindings_tokens(ir)?; |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 6711 | assert_rs_matches!( |
| 6712 | rs_api, |
| 6713 | quote! { |
| 6714 | impl<'b> ::ctor::Assign<&'b crate::SomeStruct> for SomeStruct { |
| 6715 | #[inline(always)] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6716 | fn assign<'a>(self: ::std::pin::Pin<&'a mut Self>, __param_0: &'b crate::SomeStruct) { |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 6717 | unsafe { |
| 6718 | crate::detail::__rust_thunk___ZN10SomeStructaSERKS_(self, __param_0); |
| 6719 | } |
| 6720 | } |
| 6721 | } |
| 6722 | } |
| 6723 | ); |
| 6724 | Ok(()) |
| 6725 | } |
| 6726 | |
| 6727 | #[test] |
| 6728 | fn test_assign_nonreference_return() -> Result<()> { |
| 6729 | let ir = ir_from_cc( |
| 6730 | r#" |
| 6731 | #pragma clang lifetime_elision |
| 6732 | struct SomeStruct { |
| 6733 | int operator=(const SomeStruct& other); |
| 6734 | };"#, |
| 6735 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6736 | let BindingsTokens { rs_api, .. } = generate_bindings_tokens(ir)?; |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 6737 | assert_rs_matches!( |
| 6738 | rs_api, |
| 6739 | quote! { |
| 6740 | impl<'b> ::ctor::Assign<&'b crate::SomeStruct> for SomeStruct { |
| 6741 | #[inline(always)] |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 6742 | fn assign<'a>(self: ::std::pin::Pin<&'a mut Self>, other: &'b crate::SomeStruct) { |
Devin Jeanpierre | 9ced4ef | 2022-06-08 12:39:10 -0700 | [diff] [blame] | 6743 | unsafe { |
| 6744 | crate::detail::__rust_thunk___ZN10SomeStructaSERKS_(self, other); |
| 6745 | } |
| 6746 | } |
| 6747 | } |
| 6748 | } |
| 6749 | ); |
| 6750 | Ok(()) |
| 6751 | } |
| 6752 | |
| 6753 | #[test] |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6754 | fn test_impl_eq_non_const_member_function() -> Result<()> { |
| 6755 | let ir = ir_from_cc( |
| 6756 | r#"#pragma clang lifetime_elision |
| 6757 | struct SomeStruct final { |
| 6758 | bool operator==(const SomeStruct& other) /* no `const` here */; |
| 6759 | };"#, |
| 6760 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6761 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Lukasz Anforowicz | fae90a1 | 2022-02-03 20:58:15 +0000 | [diff] [blame] | 6762 | assert_rs_not_matches!(rs_api, quote! {impl PartialEq}); |
| 6763 | Ok(()) |
| 6764 | } |
| 6765 | |
| 6766 | #[test] |
Felipe de A. Mello Pereira | 6973629 | 2022-10-11 02:03:43 -0700 | [diff] [blame] | 6767 | fn test_impl_lt_different_operands() -> Result<()> { |
| 6768 | let ir = ir_from_cc( |
| 6769 | r#"#pragma clang lifetime_elision |
| 6770 | struct SomeStruct1 final { |
| 6771 | int i; |
| 6772 | }; |
| 6773 | struct SomeStruct2 final { |
| 6774 | inline bool operator==(const SomeStruct1& other) const { |
| 6775 | return i == other.i; |
| 6776 | } |
| 6777 | inline bool operator<(const SomeStruct1& other) const { |
| 6778 | return i < other.i; |
| 6779 | }; |
| 6780 | int i; |
| 6781 | };"#, |
| 6782 | )?; |
| 6783 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6784 | assert_rs_not_matches!(rs_api, quote! {impl PartialOrd}); |
| 6785 | Ok(()) |
| 6786 | } |
| 6787 | |
| 6788 | #[test] |
| 6789 | fn test_impl_lt_non_const_member_function() -> Result<()> { |
| 6790 | let ir = ir_from_cc( |
| 6791 | r#"#pragma clang lifetime_elision |
| 6792 | struct SomeStruct final { |
| 6793 | inline bool operator==(const SomeStruct& other) const { |
| 6794 | return i == other.i; |
| 6795 | } |
| 6796 | int i; |
| 6797 | bool operator<(const SomeStruct& other) /* no `const` here */; |
| 6798 | };"#, |
| 6799 | )?; |
| 6800 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6801 | assert_rs_not_matches!(rs_api, quote! {impl PartialOrd}); |
| 6802 | Ok(()) |
| 6803 | } |
| 6804 | |
| 6805 | #[test] |
| 6806 | fn test_impl_lt_rhs_by_value() -> Result<()> { |
| 6807 | let ir = ir_from_cc( |
| 6808 | r#"#pragma clang lifetime_elision |
| 6809 | struct SomeStruct final { |
| 6810 | inline bool operator==(const SomeStruct& other) const { |
| 6811 | return i == other.i; |
| 6812 | } |
| 6813 | int i; |
| 6814 | bool operator<(SomeStruct other) const; |
| 6815 | };"#, |
| 6816 | )?; |
| 6817 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6818 | assert_rs_not_matches!(rs_api, quote! {impl PartialOrd}); |
| 6819 | Ok(()) |
| 6820 | } |
| 6821 | |
| 6822 | #[test] |
| 6823 | fn test_impl_lt_missing_eq_impl() -> Result<()> { |
| 6824 | let ir = ir_from_cc( |
| 6825 | r#"#pragma clang lifetime_elision |
| 6826 | struct SomeStruct final { |
| 6827 | inline bool operator<(const SomeStruct& other) const { |
| 6828 | return i < other.i; |
| 6829 | } |
| 6830 | int i; |
| 6831 | };"#, |
| 6832 | )?; |
| 6833 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 6834 | assert_rs_not_matches!(rs_api, quote! {impl PartialOrd}); |
| 6835 | Ok(()) |
| 6836 | } |
| 6837 | |
| 6838 | #[test] |
Dmitri Gribenko | 67cbfd2 | 2022-03-24 13:39:34 +0000 | [diff] [blame] | 6839 | fn test_thunk_ident_function() -> Result<()> { |
| 6840 | let ir = ir_from_cc("inline int foo() {}")?; |
| 6841 | let func = retrieve_func(&ir, "foo"); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 6842 | assert_eq!(thunk_ident(&func), make_rs_ident("__rust_thunk___Z3foov")); |
Dmitri Gribenko | 67cbfd2 | 2022-03-24 13:39:34 +0000 | [diff] [blame] | 6843 | Ok(()) |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 6844 | } |
| 6845 | |
| 6846 | #[test] |
| 6847 | fn test_thunk_ident_special_names() { |
Marcel Hlopko | 4b13b96 | 2021-12-06 12:40:56 +0000 | [diff] [blame] | 6848 | let ir = ir_from_cc("struct Class {};").unwrap(); |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 6849 | |
Googler | 45ad275 | 2021-12-06 12:12:35 +0000 | [diff] [blame] | 6850 | let destructor = |
| 6851 | ir.functions().find(|f| f.name == UnqualifiedIdentifier::Destructor).unwrap(); |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 6852 | assert_eq!(thunk_ident(destructor), make_rs_ident("__rust_thunk___ZN5ClassD1Ev")); |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 6853 | |
Lukasz Anforowicz | 49b5bbc | 2022-02-04 23:40:10 +0000 | [diff] [blame] | 6854 | let default_constructor = ir |
| 6855 | .functions() |
| 6856 | .find(|f| f.name == UnqualifiedIdentifier::Constructor && f.params.len() == 1) |
| 6857 | .unwrap(); |
Lukasz Anforowicz | dd9ae0f | 2022-02-17 15:52:53 +0000 | [diff] [blame] | 6858 | assert_eq!(thunk_ident(default_constructor), make_rs_ident("__rust_thunk___ZN5ClassC1Ev")); |
Devin Jeanpierre | 45cb116 | 2021-10-27 10:54:28 +0000 | [diff] [blame] | 6859 | } |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6860 | |
| 6861 | #[test] |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6862 | fn test_elided_lifetimes() -> Result<()> { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6863 | let ir = ir_from_cc( |
| 6864 | r#"#pragma clang lifetime_elision |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 6865 | struct S final { |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6866 | int& f(int& i); |
| 6867 | };"#, |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6868 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6869 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6870 | assert_rs_matches!( |
| 6871 | rs_api, |
| 6872 | quote! { |
Lukasz Anforowicz | 231a3bb | 2022-01-12 14:05:59 +0000 | [diff] [blame] | 6873 | 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] | 6874 | } |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6875 | ); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6876 | assert_rs_matches!( |
| 6877 | rs_api, |
| 6878 | quote! { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 6879 | pub(crate) fn __rust_thunk___ZN1S1fERi<'a, 'b>(__this: &'a mut crate::S, i: &'b mut i32) |
Googler | 6804a01 | 2022-01-05 07:04:36 +0000 | [diff] [blame] | 6880 | -> &'a mut i32; |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6881 | } |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6882 | ); |
Marcel Hlopko | 8954775 | 2021-12-10 09:39:41 +0000 | [diff] [blame] | 6883 | Ok(()) |
Googler | 7cced42 | 2021-12-06 11:58:39 +0000 | [diff] [blame] | 6884 | } |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 6885 | |
| 6886 | #[test] |
Googler | 386e594 | 2022-02-24 08:53:29 +0000 | [diff] [blame] | 6887 | fn test_annotated_lifetimes() -> Result<()> { |
Martin Brænne | e5ba6b6 | 2022-06-23 07:38:40 -0700 | [diff] [blame] | 6888 | let ir = ir_from_cc(&with_lifetime_macros( |
| 6889 | r#" |
| 6890 | int& $a f(int& $a i1, int& $a i2); |
Googler | 386e594 | 2022-02-24 08:53:29 +0000 | [diff] [blame] | 6891 | "#, |
Martin Brænne | e5ba6b6 | 2022-06-23 07:38:40 -0700 | [diff] [blame] | 6892 | ))?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 6893 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Googler | 386e594 | 2022-02-24 08:53:29 +0000 | [diff] [blame] | 6894 | assert_rs_matches!( |
| 6895 | rs_api, |
| 6896 | quote! { |
| 6897 | pub fn f<'a>(i1: &'a mut i32, i2: &'a mut i32) -> &'a mut i32 { ... } |
| 6898 | } |
| 6899 | ); |
| 6900 | assert_rs_matches!( |
| 6901 | rs_api, |
| 6902 | quote! { |
| 6903 | pub(crate) fn __rust_thunk___Z1fRiS_<'a>(i1: &'a mut i32, i2: &'a mut i32) |
| 6904 | -> &'a mut i32; |
| 6905 | } |
| 6906 | ); |
| 6907 | Ok(()) |
| 6908 | } |
| 6909 | |
| 6910 | #[test] |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 6911 | fn test_format_generic_params() -> Result<()> { |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 6912 | assert_rs_matches!( |
| 6913 | format_generic_params(/* lifetimes= */ &[], std::iter::empty::<syn::Ident>()), |
| 6914 | quote! {} |
| 6915 | ); |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 6916 | |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 6917 | let idents = ["T1", "T2"].iter().map(|s| make_rs_ident(s)); |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 6918 | assert_rs_matches!( |
| 6919 | format_generic_params(/* lifetimes= */ &[], idents), |
| 6920 | quote! { < T1, T2 > } |
| 6921 | ); |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 6922 | |
Felipe de A. Mello Pereira | ec5f22c | 2022-11-02 04:04:01 -0700 | [diff] [blame^] | 6923 | let lifetimes = ["a", "b", "_"].iter().map(|s| Lifetime::new(s)).collect::<Vec<_>>(); |
| 6924 | assert_rs_matches!( |
| 6925 | format_generic_params(&lifetimes, std::iter::empty::<syn::Ident>()), |
| 6926 | quote! { < 'a, 'b > } |
| 6927 | ); |
Lukasz Anforowicz | daff040 | 2021-12-23 00:37:50 +0000 | [diff] [blame] | 6928 | |
| 6929 | Ok(()) |
| 6930 | } |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6931 | |
| 6932 | #[test] |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 6933 | fn test_format_tuple_except_singleton() { |
| 6934 | fn format(xs: &[TokenStream]) -> TokenStream { |
| 6935 | format_tuple_except_singleton(xs) |
| 6936 | } |
| 6937 | assert_rs_matches!(format(&[]), quote! {()}); |
| 6938 | assert_rs_matches!(format(&[quote! {a}]), quote! {a}); |
| 6939 | assert_rs_matches!(format(&[quote! {a}, quote! {b}]), quote! {(a, b)}); |
| 6940 | } |
| 6941 | |
| 6942 | #[test] |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6943 | fn test_overloaded_functions() -> Result<()> { |
| 6944 | // TODO(b/213280424): We don't support creating bindings for overloaded |
| 6945 | // functions yet, except in the case of overloaded constructors with a |
| 6946 | // single parameter. |
| 6947 | let ir = ir_from_cc( |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 6948 | r#" #pragma clang lifetime_elision |
Devin Jeanpierre | d5a5362 | 2022-10-04 22:00:02 -0700 | [diff] [blame] | 6949 | void f() {} |
| 6950 | void f(int i) {} |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 6951 | struct S1 final { |
Devin Jeanpierre | d5a5362 | 2022-10-04 22:00:02 -0700 | [diff] [blame] | 6952 | void f() {} |
| 6953 | void f(int i) {} |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6954 | }; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 6955 | struct S2 final { |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6956 | void f(); |
| 6957 | }; |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 6958 | struct S3 final { |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6959 | S3(int i); |
| 6960 | S3(double d); |
| 6961 | }; |
Lukasz Anforowicz | 8d06420 | 2022-09-01 07:31:06 -0700 | [diff] [blame] | 6962 | |
| 6963 | namespace foo { void not_overloaded(); } |
| 6964 | namespace bar { void not_overloaded(); } |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6965 | "#, |
| 6966 | )?; |
Devin Jeanpierre | d5a5362 | 2022-10-04 22:00:02 -0700 | [diff] [blame] | 6967 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6968 | |
| 6969 | // Cannot overload free functions. |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 6970 | assert_cc_matches!(rs_api, { |
| 6971 | let txt = "google3/ir_from_cc_virtual_header.h;l=4\n\ |
| 6972 | Error while generating bindings for item 'f':\n\ |
| 6973 | Cannot generate bindings for overloaded function"; |
| 6974 | quote! { __COMMENT__ #txt } |
| 6975 | }); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6976 | assert_rs_not_matches!(rs_api, quote! {pub fn f()}); |
| 6977 | assert_rs_not_matches!(rs_api, quote! {pub fn f(i: i32)}); |
| 6978 | |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 6979 | assert_cc_matches!(rs_api, { |
| 6980 | let txt = "google3/ir_from_cc_virtual_header.h;l=7\n\ |
| 6981 | Error while generating bindings for item 'S1::f':\n\ |
| 6982 | Cannot generate bindings for overloaded function"; |
| 6983 | quote! { __COMMENT__ #txt } |
| 6984 | }); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6985 | assert_rs_not_matches!(rs_api, quote! {pub fn f(... S1 ...)}); |
| 6986 | |
Devin Jeanpierre | d5a5362 | 2022-10-04 22:00:02 -0700 | [diff] [blame] | 6987 | // And thunks aren't generated for either. |
| 6988 | assert_cc_not_matches!(rs_api_impl, quote! {f}); |
| 6989 | |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6990 | // But we can import member functions that have the same name as a free |
| 6991 | // function. |
Lukasz Anforowicz | 55673c9 | 2022-01-27 19:37:26 +0000 | [diff] [blame] | 6992 | assert_rs_matches!(rs_api, quote! {pub fn f<'a>(&'a mut self)}); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 6993 | |
| 6994 | // We can also import overloaded single-parameter constructors. |
| 6995 | assert_rs_matches!(rs_api, quote! {impl From<i32> for S3}); |
| 6996 | assert_rs_matches!(rs_api, quote! {impl From<f64> for S3}); |
Lukasz Anforowicz | 8d06420 | 2022-09-01 07:31:06 -0700 | [diff] [blame] | 6997 | |
| 6998 | // And we can import functions that have the same name + signature, but that are |
| 6999 | // in 2 different namespaces. |
| 7000 | assert_rs_matches!(rs_api, quote! { pub fn not_overloaded() }); |
Googler | d03d05b | 2022-01-07 10:10:57 +0000 | [diff] [blame] | 7001 | Ok(()) |
| 7002 | } |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 7003 | |
| 7004 | #[test] |
| 7005 | fn test_type_alias() -> Result<()> { |
| 7006 | let ir = ir_from_cc( |
| 7007 | r#" |
Lukasz Anforowicz | c503af4 | 2022-03-04 23:18:15 +0000 | [diff] [blame] | 7008 | // MyTypedefDecl doc comment |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 7009 | typedef int MyTypedefDecl; |
Lukasz Anforowicz | c503af4 | 2022-03-04 23:18:15 +0000 | [diff] [blame] | 7010 | |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 7011 | using MyTypeAliasDecl = int; |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 7012 | using MyTypeAliasDecl_Alias = MyTypeAliasDecl; |
| 7013 | |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 7014 | struct S final {}; |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 7015 | using S_Alias = S; |
| 7016 | using S_Alias_Alias = S_Alias; |
| 7017 | |
| 7018 | inline void f(MyTypedefDecl t) {} |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 7019 | "#, |
| 7020 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7021 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
Lukasz Anforowicz | c503af4 | 2022-03-04 23:18:15 +0000 | [diff] [blame] | 7022 | assert_rs_matches!( |
| 7023 | rs_api, |
| 7024 | quote! { |
| 7025 | #[doc = " MyTypedefDecl doc comment"] |
| 7026 | pub type MyTypedefDecl = i32; |
| 7027 | } |
| 7028 | ); |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 7029 | assert_rs_matches!(rs_api, quote! { pub type MyTypeAliasDecl = i32; }); |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 7030 | assert_rs_matches!( |
| 7031 | rs_api, |
| 7032 | quote! { pub type MyTypeAliasDecl_Alias = crate::MyTypeAliasDecl; } |
| 7033 | ); |
| 7034 | assert_rs_matches!(rs_api, quote! { pub type S_Alias = crate::S; }); |
| 7035 | assert_rs_matches!(rs_api, quote! { pub type S_Alias_Alias = crate::S_Alias; }); |
| 7036 | assert_rs_matches!(rs_api, quote! { pub fn f(t: crate::MyTypedefDecl) }); |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 7037 | assert_cc_matches!( |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7038 | rs_api_impl, |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 7039 | quote! { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 7040 | extern "C" void __rust_thunk___Z1fi(MyTypedefDecl t) { f(t); } |
Googler | 6a0a525 | 2022-01-11 14:08:09 +0000 | [diff] [blame] | 7041 | } |
| 7042 | ); |
Googler | dcca7f7 | 2022-01-10 12:30:43 +0000 | [diff] [blame] | 7043 | Ok(()) |
| 7044 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 7045 | |
| 7046 | #[test] |
| 7047 | fn test_rs_type_kind_implements_copy() -> Result<()> { |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7048 | let template = r#" LIFETIMES |
Devin Jeanpierre | 88343c7 | 2022-01-15 01:10:23 +0000 | [diff] [blame] | 7049 | struct [[clang::trivial_abi]] TrivialStruct final { int i; }; |
| 7050 | struct [[clang::trivial_abi]] UserDefinedCopyConstructor final { |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 7051 | UserDefinedCopyConstructor(const UserDefinedCopyConstructor&); |
| 7052 | }; |
| 7053 | using IntAlias = int; |
| 7054 | using TrivialAlias = TrivialStruct; |
| 7055 | using NonTrivialAlias = UserDefinedCopyConstructor; |
| 7056 | void func(PARAM_TYPE some_param); |
| 7057 | "#; |
| 7058 | assert_impl_all!(i32: Copy); |
| 7059 | assert_impl_all!(&i32: Copy); |
Lukasz Anforowicz | b4d1778 | 2022-07-07 08:09:13 -0700 | [diff] [blame] | 7060 | assert_not_impl_any!(&mut i32: Copy); |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7061 | assert_impl_all!(Option<&i32>: Copy); |
Lukasz Anforowicz | b4d1778 | 2022-07-07 08:09:13 -0700 | [diff] [blame] | 7062 | assert_not_impl_any!(Option<&mut i32>: Copy); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 7063 | assert_impl_all!(*const i32: Copy); |
| 7064 | assert_impl_all!(*mut i32: Copy); |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7065 | struct Test { |
| 7066 | // Test inputs: |
| 7067 | cc: &'static str, |
| 7068 | lifetimes: bool, |
| 7069 | // Expected test outputs: |
| 7070 | rs: &'static str, |
| 7071 | is_copy: bool, |
| 7072 | } |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 7073 | let tests = vec![ |
| 7074 | // Validity of the next few tests is verified via |
| 7075 | // `assert_[not_]impl_all!` static assertions above. |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7076 | Test { cc: "int", lifetimes: true, rs: "i32", is_copy: true }, |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 7077 | Test { cc: "const int&", lifetimes: true, rs: "& 'a i32", is_copy: true }, |
| 7078 | Test { cc: "int&", lifetimes: true, rs: "& 'a mut i32", is_copy: false }, |
| 7079 | Test { cc: "const int*", lifetimes: true, rs: "Option < & 'a i32 >", is_copy: true }, |
| 7080 | Test { cc: "int*", lifetimes: true, rs: "Option < & 'a mut i32 >", is_copy: false }, |
| 7081 | Test { cc: "const int*", lifetimes: false, rs: "* const i32", is_copy: true }, |
| 7082 | Test { cc: "int*", lifetimes: false, rs: "* mut i32", is_copy: true }, |
Googler | 7302168 | 2022-10-28 02:39:25 -0700 | [diff] [blame] | 7083 | Test { |
| 7084 | cc: "void*", |
| 7085 | lifetimes: false, |
| 7086 | rs: "* mut :: std :: os :: raw :: c_void", |
| 7087 | is_copy: true, |
| 7088 | }, |
| 7089 | Test { |
| 7090 | cc: "const void*", |
| 7091 | lifetimes: false, |
| 7092 | rs: "* const :: std :: os :: raw :: c_void", |
| 7093 | is_copy: true, |
| 7094 | }, |
| 7095 | Test { |
| 7096 | cc: "void* const*", |
| 7097 | lifetimes: false, |
| 7098 | rs: "* const * mut :: std :: os :: raw :: c_void", |
| 7099 | is_copy: true, |
| 7100 | }, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 7101 | // Tests below have been thought-through and verified "manually". |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7102 | // TrivialStruct is expected to derive Copy. |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 7103 | Test { |
| 7104 | cc: "TrivialStruct", |
| 7105 | lifetimes: true, |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 7106 | rs: "crate :: TrivialStruct", |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 7107 | is_copy: true, |
| 7108 | }, |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7109 | Test { |
| 7110 | cc: "UserDefinedCopyConstructor", |
| 7111 | lifetimes: true, |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 7112 | rs: "crate :: UserDefinedCopyConstructor", |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7113 | is_copy: false, |
| 7114 | }, |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 7115 | Test { cc: "IntAlias", lifetimes: true, rs: "crate :: IntAlias", is_copy: true }, |
Googler | 84cd560 | 2022-10-07 08:25:46 -0700 | [diff] [blame] | 7116 | Test { |
| 7117 | cc: "TrivialAlias", |
| 7118 | lifetimes: true, |
| 7119 | rs: "crate :: TrivialAlias", |
| 7120 | is_copy: true, |
| 7121 | }, |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 7122 | Test { |
| 7123 | cc: "NonTrivialAlias", |
| 7124 | lifetimes: true, |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 7125 | rs: "crate :: NonTrivialAlias", |
Marcel Hlopko | 3623489 | 2022-05-10 00:39:54 -0700 | [diff] [blame] | 7126 | is_copy: false, |
| 7127 | }, |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 7128 | ]; |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7129 | for test in tests.iter() { |
| 7130 | let test_name = format!("cc='{}', lifetimes={}", test.cc, test.lifetimes); |
| 7131 | let cc_input = template.replace("PARAM_TYPE", test.cc).replace( |
| 7132 | "LIFETIMES", |
| 7133 | if test.lifetimes { "#pragma clang lifetime_elision" } else { "" }, |
| 7134 | ); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7135 | let db = db_from_cc(&cc_input)?; |
| 7136 | let ir = db.ir(); |
| 7137 | |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 7138 | let f = retrieve_func(&ir, "func"); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7139 | let t = db.rs_type_kind(f.params[0].type_.rs_type.clone())?; |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7140 | |
Lukasz Anforowicz | ae9be08 | 2022-10-05 07:33:57 -0700 | [diff] [blame] | 7141 | let fmt = t.to_token_stream().to_string(); |
Lukasz Anforowicz | 20651e3 | 2022-02-10 14:52:15 +0000 | [diff] [blame] | 7142 | assert_eq!(test.rs, fmt, "Testing: {}", test_name); |
| 7143 | |
| 7144 | assert_eq!(test.is_copy, t.implements_copy(), "Testing: {}", test_name); |
Lukasz Anforowicz | e57215c | 2022-01-12 14:54:16 +0000 | [diff] [blame] | 7145 | } |
| 7146 | Ok(()) |
| 7147 | } |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7148 | |
| 7149 | #[test] |
| 7150 | fn test_rs_type_kind_is_shared_ref_to_with_lifetimes() -> Result<()> { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7151 | let db = db_from_cc( |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7152 | "#pragma clang lifetime_elision |
| 7153 | struct SomeStruct {}; |
| 7154 | void foo(const SomeStruct& foo_param); |
| 7155 | void bar(SomeStruct& bar_param);", |
| 7156 | )?; |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7157 | let ir = db.ir(); |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7158 | let record = ir.records().next().unwrap(); |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 7159 | let foo_func = retrieve_func(&ir, "foo"); |
| 7160 | let bar_func = retrieve_func(&ir, "bar"); |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7161 | |
| 7162 | // const-ref + lifetimes in C++ ===> shared-ref in Rust |
| 7163 | assert_eq!(foo_func.params.len(), 1); |
| 7164 | let foo_param = &foo_func.params[0]; |
| 7165 | assert_eq!(&foo_param.identifier.identifier, "foo_param"); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7166 | let foo_type = db.rs_type_kind(foo_param.type_.rs_type.clone())?; |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7167 | assert!(foo_type.is_shared_ref_to(record)); |
| 7168 | assert!(matches!(foo_type, RsTypeKind::Reference { mutability: Mutability::Const, .. })); |
| 7169 | |
| 7170 | // non-const-ref + lifetimes in C++ ===> mutable-ref in Rust |
| 7171 | assert_eq!(bar_func.params.len(), 1); |
| 7172 | let bar_param = &bar_func.params[0]; |
| 7173 | assert_eq!(&bar_param.identifier.identifier, "bar_param"); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7174 | let bar_type = db.rs_type_kind(bar_param.type_.rs_type.clone())?; |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7175 | assert!(!bar_type.is_shared_ref_to(record)); |
| 7176 | assert!(matches!(bar_type, RsTypeKind::Reference { mutability: Mutability::Mut, .. })); |
| 7177 | |
| 7178 | Ok(()) |
| 7179 | } |
| 7180 | |
| 7181 | #[test] |
| 7182 | fn test_rs_type_kind_is_shared_ref_to_without_lifetimes() -> Result<()> { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7183 | let db = db_from_cc( |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7184 | "struct SomeStruct {}; |
| 7185 | void foo(const SomeStruct& foo_param);", |
| 7186 | )?; |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7187 | let ir = db.ir(); |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7188 | let record = ir.records().next().unwrap(); |
Lukasz Anforowicz | 9c663ca | 2022-02-09 01:33:31 +0000 | [diff] [blame] | 7189 | let foo_func = retrieve_func(&ir, "foo"); |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7190 | |
| 7191 | // const-ref + *no* lifetimes in C++ ===> const-pointer in Rust |
| 7192 | assert_eq!(foo_func.params.len(), 1); |
| 7193 | let foo_param = &foo_func.params[0]; |
| 7194 | assert_eq!(&foo_param.identifier.identifier, "foo_param"); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7195 | let foo_type = db.rs_type_kind(foo_param.type_.rs_type.clone())?; |
Lukasz Anforowicz | a94ab70 | 2022-01-14 22:40:25 +0000 | [diff] [blame] | 7196 | assert!(!foo_type.is_shared_ref_to(record)); |
| 7197 | assert!(matches!(foo_type, RsTypeKind::Pointer { mutability: Mutability::Const, .. })); |
| 7198 | |
| 7199 | Ok(()) |
| 7200 | } |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 7201 | |
| 7202 | #[test] |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7203 | fn test_rs_type_kind_dfs_iter_ordering() { |
| 7204 | // Set up a test input representing: A<B<C>, D<E>>. |
| 7205 | let a = { |
| 7206 | let b = { |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 7207 | let c = RsTypeKind::Other { name: "C".into(), type_args: Rc::from([]) }; |
| 7208 | RsTypeKind::Other { name: "B".into(), type_args: Rc::from([c]) } |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7209 | }; |
| 7210 | let d = { |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 7211 | let e = RsTypeKind::Other { name: "E".into(), type_args: Rc::from([]) }; |
| 7212 | RsTypeKind::Other { name: "D".into(), type_args: Rc::from([e]) } |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7213 | }; |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 7214 | RsTypeKind::Other { name: "A".into(), type_args: Rc::from([b, d]) } |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7215 | }; |
| 7216 | let dfs_names = a |
| 7217 | .dfs_iter() |
| 7218 | .map(|t| match t { |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 7219 | RsTypeKind::Other { name, .. } => &**name, |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7220 | _ => unreachable!("Only 'other' types are used in this test"), |
| 7221 | }) |
| 7222 | .collect_vec(); |
| 7223 | assert_eq!(vec!["A", "B", "C", "D", "E"], dfs_names); |
| 7224 | } |
| 7225 | |
| 7226 | #[test] |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 7227 | fn test_rs_type_kind_dfs_iter_ordering_for_func_ptr() { |
| 7228 | // Set up a test input representing: fn(A, B) -> C |
| 7229 | let f = { |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 7230 | let a = RsTypeKind::Other { name: "A".into(), type_args: Rc::from(&[][..]) }; |
| 7231 | let b = RsTypeKind::Other { name: "B".into(), type_args: Rc::from(&[][..]) }; |
| 7232 | let c = RsTypeKind::Other { name: "C".into(), type_args: Rc::from(&[][..]) }; |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 7233 | RsTypeKind::FuncPtr { |
| 7234 | abi: "blah".into(), |
Devin Jeanpierre | b2b6cf8 | 2022-07-07 01:49:27 -0700 | [diff] [blame] | 7235 | param_types: Rc::from([a, b]), |
| 7236 | return_type: Rc::new(c), |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 7237 | } |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 7238 | }; |
| 7239 | let dfs_names = f |
| 7240 | .dfs_iter() |
| 7241 | .map(|t| match t { |
| 7242 | RsTypeKind::FuncPtr { .. } => "fn", |
Devin Jeanpierre | 8cd8ae7 | 2022-06-29 18:59:40 -0700 | [diff] [blame] | 7243 | RsTypeKind::Other { name, .. } => &**name, |
Lukasz Anforowicz | cf230fd | 2022-02-18 19:20:39 +0000 | [diff] [blame] | 7244 | _ => unreachable!("Only FuncPtr and Other kinds are used in this test"), |
| 7245 | }) |
| 7246 | .collect_vec(); |
| 7247 | assert_eq!(vec!["fn", "A", "B", "C"], dfs_names); |
| 7248 | } |
| 7249 | |
| 7250 | #[test] |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7251 | fn test_rs_type_kind_lifetimes() -> Result<()> { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7252 | let db = db_from_cc( |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7253 | r#" |
| 7254 | #pragma clang lifetime_elision |
| 7255 | using TypeAlias = int&; |
| 7256 | struct SomeStruct {}; |
Devin Jeanpierre | 48cb5bc | 2022-06-02 00:50:43 -0700 | [diff] [blame] | 7257 | void foo(int a, int& b, int&& c, int* d, int** e, TypeAlias f, SomeStruct g); "#, |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7258 | )?; |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7259 | let ir = db.ir(); |
Devin Jeanpierre | 48cb5bc | 2022-06-02 00:50:43 -0700 | [diff] [blame] | 7260 | let func = retrieve_func(&ir, "foo"); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7261 | let ret = db.rs_type_kind(func.return_type.rs_type.clone())?; |
| 7262 | let a = db.rs_type_kind(func.params[0].type_.rs_type.clone())?; |
| 7263 | let b = db.rs_type_kind(func.params[1].type_.rs_type.clone())?; |
| 7264 | let c = db.rs_type_kind(func.params[2].type_.rs_type.clone())?; |
| 7265 | let d = db.rs_type_kind(func.params[3].type_.rs_type.clone())?; |
| 7266 | let e = db.rs_type_kind(func.params[4].type_.rs_type.clone())?; |
| 7267 | let f = db.rs_type_kind(func.params[5].type_.rs_type.clone())?; |
| 7268 | let g = db.rs_type_kind(func.params[6].type_.rs_type.clone())?; |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7269 | |
| 7270 | assert_eq!(0, ret.lifetimes().count()); // No lifetimes on `void`. |
| 7271 | assert_eq!(0, a.lifetimes().count()); // No lifetimes on `int`. |
| 7272 | assert_eq!(1, b.lifetimes().count()); // `&'a i32` has a single lifetime. |
Devin Jeanpierre | 48cb5bc | 2022-06-02 00:50:43 -0700 | [diff] [blame] | 7273 | assert_eq!(1, c.lifetimes().count()); // `RvalueReference<'a, i32>` has a single lifetime. |
| 7274 | assert_eq!(1, d.lifetimes().count()); // `Option<&'b i32>` has a single lifetime. |
| 7275 | assert_eq!(2, e.lifetimes().count()); // `&'c Option<&'d i32>` has two lifetimes. |
| 7276 | assert_eq!(1, f.lifetimes().count()); // Lifetime of underlying type should show through. |
| 7277 | assert_eq!(0, g.lifetimes().count()); // No lifetimes on structs (yet). |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7278 | Ok(()) |
| 7279 | } |
| 7280 | |
| 7281 | #[test] |
| 7282 | fn test_rs_type_kind_lifetimes_raw_ptr() -> Result<()> { |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7283 | let db = db_from_cc("void foo(int* a);")?; |
| 7284 | let ir = db.ir(); |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7285 | let f = retrieve_func(&ir, "foo"); |
Devin Jeanpierre | 409f6f6 | 2022-07-07 02:00:26 -0700 | [diff] [blame] | 7286 | let a = db.rs_type_kind(f.params[0].type_.rs_type.clone())?; |
Lukasz Anforowicz | 90bdb96 | 2022-02-14 21:07:45 +0000 | [diff] [blame] | 7287 | assert_eq!(0, a.lifetimes().count()); // No lifetimes on `int*`. |
| 7288 | Ok(()) |
| 7289 | } |
| 7290 | |
| 7291 | #[test] |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 7292 | fn test_rust_keywords_are_escaped_in_rs_api_file() -> Result<()> { |
| 7293 | let ir = ir_from_cc("struct type { int dyn; };")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7294 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 7295 | assert_rs_matches!(rs_api, quote! { struct r#type { ... r#dyn: i32 ... } }); |
| 7296 | Ok(()) |
| 7297 | } |
| 7298 | |
| 7299 | #[test] |
| 7300 | fn test_rust_keywords_are_not_escaped_in_rs_api_impl_file() -> Result<()> { |
| 7301 | let ir = ir_from_cc("struct type { int dyn; };")?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7302 | let rs_api_impl = generate_bindings_tokens(ir)?.rs_api_impl; |
Lukasz Anforowicz | 4ee9c22 | 2022-04-13 09:33:36 -0700 | [diff] [blame] | 7303 | assert_cc_matches!( |
| 7304 | rs_api_impl, |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 7305 | quote! { static_assert(CRUBIT_OFFSET_OF(dyn, struct type) ... ) } |
Lukasz Anforowicz | 4ee9c22 | 2022-04-13 09:33:36 -0700 | [diff] [blame] | 7306 | ); |
Marcel Hlopko | eaae9b7 | 2022-01-21 15:54:11 +0000 | [diff] [blame] | 7307 | Ok(()) |
| 7308 | } |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 7309 | |
| 7310 | #[test] |
| 7311 | fn test_no_aligned_attr() { |
| 7312 | let ir = ir_from_cc("struct SomeStruct {};").unwrap(); |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7313 | let rs_api = generate_bindings_tokens(ir).unwrap().rs_api; |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 7314 | |
| 7315 | assert_rs_matches! {rs_api, quote! { |
| 7316 | #[repr(C)] |
| 7317 | pub struct SomeStruct { ... } |
| 7318 | }}; |
| 7319 | } |
| 7320 | |
| 7321 | #[test] |
| 7322 | fn test_aligned_attr() { |
| 7323 | let ir = ir_from_cc("struct SomeStruct {} __attribute__((aligned(64)));").unwrap(); |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7324 | let rs_api = generate_bindings_tokens(ir).unwrap().rs_api; |
Marcel Hlopko | 14ee3c8 | 2022-02-09 09:46:23 +0000 | [diff] [blame] | 7325 | |
| 7326 | assert_rs_matches! {rs_api, quote! { |
| 7327 | #[repr(C, align(64))] |
| 7328 | pub struct SomeStruct { ... } |
| 7329 | } |
| 7330 | }; |
| 7331 | } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7332 | |
| 7333 | /// !Unpin references should not be pinned. |
| 7334 | #[test] |
| 7335 | fn test_nonunpin_ref_param() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7336 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7337 | r#" |
| 7338 | #pragma clang lifetime_elision |
| 7339 | struct S {~S();}; |
| 7340 | void Function(const S& s); |
| 7341 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7342 | )?)? |
| 7343 | .rs_api; |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7344 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7345 | rs_api, |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7346 | quote! { |
Rosica Dejanovska | 8283fe6 | 2022-05-09 10:04:28 -0700 | [diff] [blame] | 7347 | fn Function<'a>(s: &'a crate::S) { ... } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7348 | } |
| 7349 | ); |
| 7350 | Ok(()) |
| 7351 | } |
| 7352 | |
| 7353 | /// !Unpin mut references must be pinned. |
| 7354 | #[test] |
| 7355 | fn test_nonunpin_mut_param() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7356 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7357 | r#" |
| 7358 | #pragma clang lifetime_elision |
| 7359 | struct S {~S();}; |
| 7360 | void Function(S& s); |
| 7361 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7362 | )?)? |
| 7363 | .rs_api; |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7364 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7365 | rs_api, |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7366 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7367 | fn Function<'a>(s: ::std::pin::Pin<&'a mut crate::S>) { ... } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7368 | } |
| 7369 | ); |
| 7370 | Ok(()) |
| 7371 | } |
| 7372 | |
| 7373 | /// !Unpin &self should not be pinned. |
| 7374 | #[test] |
| 7375 | fn test_nonunpin_ref_self() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7376 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7377 | r#" |
| 7378 | #pragma clang lifetime_elision |
| 7379 | struct S { |
| 7380 | ~S(); |
| 7381 | void Function() const; |
| 7382 | }; |
| 7383 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7384 | )?)? |
| 7385 | .rs_api; |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7386 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7387 | rs_api, |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7388 | quote! { |
| 7389 | fn Function<'a>(&'a self) { ... } |
| 7390 | } |
| 7391 | ); |
| 7392 | Ok(()) |
| 7393 | } |
| 7394 | |
| 7395 | /// !Unpin &mut self must be pinned. |
| 7396 | #[test] |
| 7397 | fn test_nonunpin_mut_self() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7398 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7399 | r#" |
| 7400 | #pragma clang lifetime_elision |
| 7401 | struct S { |
| 7402 | ~S(); |
| 7403 | void Function(); |
| 7404 | }; |
| 7405 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7406 | )?)? |
| 7407 | .rs_api; |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7408 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7409 | rs_api, |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7410 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7411 | fn Function<'a>(self: ::std::pin::Pin<&'a mut Self>) { ... } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7412 | } |
| 7413 | ); |
| 7414 | Ok(()) |
| 7415 | } |
| 7416 | |
| 7417 | /// Drop::drop must not use self : Pin<...>. |
| 7418 | #[test] |
| 7419 | fn test_nonunpin_drop() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7420 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7421 | r#" |
| 7422 | struct S {~S();}; |
| 7423 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7424 | )?)? |
| 7425 | .rs_api; |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7426 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7427 | rs_api, |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7428 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7429 | unsafe fn pinned_drop<'a>(self: ::std::pin::Pin<&'a mut Self>) { ... } |
Devin Jeanpierre | 149950d | 2022-02-22 21:02:02 +0000 | [diff] [blame] | 7430 | } |
| 7431 | ); |
| 7432 | Ok(()) |
| 7433 | } |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 7434 | |
| 7435 | #[test] |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7436 | fn test_nonunpin_0_arg_constructor() -> Result<()> { |
| 7437 | let ir = ir_from_cc( |
| 7438 | r#"#pragma clang lifetime_elision |
| 7439 | // This type must be `!Unpin`. |
| 7440 | struct HasConstructor {explicit HasConstructor() {}};"#, |
| 7441 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7442 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7443 | assert_rs_matches!(rs_api, quote! {#[::ctor::recursively_pinned]}); |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7444 | assert_rs_matches!( |
| 7445 | rs_api, |
| 7446 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7447 | impl ::ctor::CtorNew<()> for HasConstructor { |
| 7448 | type CtorType = impl ::ctor::Ctor<Output = Self>; |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7449 | |
| 7450 | #[inline (always)] |
| 7451 | fn ctor_new(args: ()) -> Self::CtorType { |
| 7452 | let () = args; |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 7453 | unsafe { |
| 7454 | ::ctor::FnCtor::new(move |dest: ::std::pin::Pin<&mut ::std::mem::MaybeUninit<crate::HasConstructor>>| { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7455 | crate::detail::__rust_thunk___ZN14HasConstructorC1Ev(::std::pin::Pin::into_inner_unchecked(dest)); |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 7456 | }) |
| 7457 | } |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7458 | } |
| 7459 | } |
| 7460 | } |
| 7461 | ); |
| 7462 | Ok(()) |
| 7463 | } |
| 7464 | |
| 7465 | #[test] |
| 7466 | fn test_nonunpin_1_arg_constructor() -> Result<()> { |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 7467 | let ir = ir_from_cc( |
| 7468 | r#"#pragma clang lifetime_elision |
| 7469 | // This type must be `!Unpin`. |
| 7470 | struct HasConstructor {explicit HasConstructor(unsigned char input) {}};"#, |
| 7471 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7472 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7473 | assert_rs_matches!(rs_api, quote! {#[::ctor::recursively_pinned]}); |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 7474 | assert_rs_matches!( |
| 7475 | rs_api, |
| 7476 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7477 | impl ::ctor::CtorNew<u8> for HasConstructor { |
| 7478 | type CtorType = impl ::ctor::Ctor<Output = Self>; |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 7479 | |
| 7480 | #[inline (always)] |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7481 | fn ctor_new(args: u8) -> Self::CtorType { |
| 7482 | let input = args; |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 7483 | unsafe { |
| 7484 | ::ctor::FnCtor::new(move |dest: ::std::pin::Pin<&mut ::std::mem::MaybeUninit<crate::HasConstructor>>| { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7485 | crate::detail::__rust_thunk___ZN14HasConstructorC1Eh(::std::pin::Pin::into_inner_unchecked(dest), input); |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 7486 | }) |
| 7487 | } |
Devin Jeanpierre | 6f60737 | 2022-03-22 21:34:38 +0000 | [diff] [blame] | 7488 | } |
| 7489 | } |
| 7490 | } |
| 7491 | ); |
| 7492 | Ok(()) |
| 7493 | } |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7494 | |
| 7495 | #[test] |
| 7496 | fn test_nonunpin_2_arg_constructor() -> Result<()> { |
| 7497 | let ir = ir_from_cc( |
| 7498 | r#"#pragma clang lifetime_elision |
| 7499 | // This type must be `!Unpin`. |
| 7500 | struct HasConstructor {explicit HasConstructor(unsigned char input1, signed char input2) {}};"#, |
| 7501 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7502 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7503 | assert_rs_matches!(rs_api, quote! {#[::ctor::recursively_pinned]}); |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7504 | assert_rs_matches!( |
| 7505 | rs_api, |
| 7506 | quote! { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7507 | impl ::ctor::CtorNew<(u8, i8)> for HasConstructor { |
| 7508 | type CtorType = impl ::ctor::Ctor<Output = Self>; |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7509 | |
| 7510 | #[inline (always)] |
| 7511 | fn ctor_new(args: (u8, i8)) -> Self::CtorType { |
| 7512 | let (input1, input2) = args; |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 7513 | unsafe { |
| 7514 | ::ctor::FnCtor::new(move |dest: ::std::pin::Pin<&mut ::std::mem::MaybeUninit<crate::HasConstructor>>| { |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7515 | crate::detail::__rust_thunk___ZN14HasConstructorC1Eha(::std::pin::Pin::into_inner_unchecked(dest), input1, input2); |
Devin Jeanpierre | 93927e8 | 2022-08-01 14:05:54 -0700 | [diff] [blame] | 7516 | }) |
| 7517 | } |
Devin Jeanpierre | ad12574 | 2022-04-11 13:50:28 -0700 | [diff] [blame] | 7518 | } |
| 7519 | } |
| 7520 | } |
| 7521 | ); |
| 7522 | Ok(()) |
| 7523 | } |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7524 | |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 7525 | /// Traits which monomorphize the `Ctor` parameter into the caller must |
| 7526 | /// synthesize an RvalueReference parameter, with an appropriate |
| 7527 | /// lifetime parameter. |
| 7528 | #[test] |
| 7529 | fn test_nonunpin_by_value_params() -> Result<()> { |
| 7530 | let ir = ir_from_cc( |
| 7531 | r#"#pragma clang lifetime_elision |
| 7532 | // This type must be `!Unpin`. |
| 7533 | struct HasConstructor { |
| 7534 | // int& x is here to create a 'b lifetime, which collides with a synthesized |
| 7535 | // lifetime name. But that's OK! We handle collisions! |
| 7536 | // (`a` would also work, but that's just because the left hand doesn't know what |
| 7537 | // the right is doing: the `a` lifetime is present in some places, but eventually |
| 7538 | // removed from the public interface.) |
| 7539 | explicit HasConstructor(const int& x, HasConstructor y, HasConstructor b) {} |
| 7540 | };"#, |
| 7541 | )?; |
| 7542 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 7543 | assert_rs_matches!(rs_api, quote! {#[::ctor::recursively_pinned]}); |
| 7544 | assert_rs_matches!( |
| 7545 | rs_api, |
| 7546 | quote! { |
| 7547 | impl <'b, 'y, 'b_2> ::ctor::CtorNew<( |
| 7548 | &'b i32, |
| 7549 | ::ctor::RvalueReference<'y, crate::HasConstructor>, |
| 7550 | ::ctor::RvalueReference<'b_2, crate::HasConstructor>) |
| 7551 | > for HasConstructor { |
| 7552 | // The captures are why we need explicit lifetimes for the two rvalue reference |
| 7553 | // parameters. |
| 7554 | type CtorType = impl ::ctor::Ctor<Output = Self> |
| 7555 | + ::ctor::Captures<'b> |
| 7556 | + ::ctor::Captures<'y> |
| 7557 | + ::ctor::Captures<'b_2>; |
| 7558 | |
| 7559 | #[inline (always)] |
| 7560 | fn ctor_new(args: ( |
| 7561 | &'b i32, |
| 7562 | ::ctor::RvalueReference<'y, crate::HasConstructor>, |
| 7563 | ::ctor::RvalueReference<'b_2, crate::HasConstructor>) |
| 7564 | ) -> Self::CtorType { |
| 7565 | let (x, y, b) = args; |
| 7566 | unsafe { |
| 7567 | ::ctor::FnCtor::new(move |dest: ::std::pin::Pin<&mut ::std::mem::MaybeUninit<crate::HasConstructor>>| { |
| 7568 | crate::detail::__rust_thunk___ZN14HasConstructorC1ERKiS_S_(::std::pin::Pin::into_inner_unchecked(dest), x, y, b); |
| 7569 | }) |
| 7570 | } |
| 7571 | } |
| 7572 | } |
| 7573 | } |
| 7574 | ); |
| 7575 | Ok(()) |
| 7576 | } |
| 7577 | |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7578 | #[test] |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 7579 | fn test_nonunpin_return() -> Result<()> { |
| 7580 | let ir = ir_from_cc( |
| 7581 | r#"#pragma clang lifetime_elision |
| 7582 | // This type must be `!Unpin`. |
| 7583 | struct Nontrivial {~Nontrivial();}; |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 7584 | |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 7585 | Nontrivial ReturnsByValue(const int& x, const int& y); |
| 7586 | "#, |
| 7587 | )?; |
| 7588 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 7589 | assert_rs_matches!( |
| 7590 | rs_api, |
| 7591 | quote! { |
| 7592 | pub fn ReturnsByValue<'a, 'b>(x: &'a i32, y: &'b i32) |
Devin Jeanpierre | 83cb2dc | 2022-07-15 01:50:28 -0700 | [diff] [blame] | 7593 | -> impl ::ctor::Ctor<Output=crate::Nontrivial> |
| 7594 | + ::ctor::Captures<'a> |
| 7595 | + ::ctor::Captures<'b> { |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 7596 | unsafe { |
| 7597 | ::ctor::FnCtor::new(move |dest: ::std::pin::Pin<&mut ::std::mem::MaybeUninit<crate::Nontrivial>>| { |
| 7598 | crate::detail::__rust_thunk___Z14ReturnsByValueRKiS0_(::std::pin::Pin::into_inner_unchecked(dest), x, y); |
| 7599 | }) |
| 7600 | } |
| 7601 | |
| 7602 | } |
| 7603 | } |
| 7604 | ); |
| 7605 | |
| 7606 | assert_cc_matches!( |
| 7607 | rs_api_impl, |
| 7608 | quote! { |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 7609 | extern "C" void __rust_thunk___Z14ReturnsByValueRKiS0_( |
| 7610 | struct Nontrivial* __return, int const* x, int const* y) { |
Devin Jeanpierre | 521b4ee | 2022-07-27 07:19:50 -0700 | [diff] [blame] | 7611 | new(__return) auto(ReturnsByValue(*x, *y)); |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 7612 | } |
| 7613 | } |
| 7614 | ); |
| 7615 | Ok(()) |
| 7616 | } |
| 7617 | |
| 7618 | /// Assignment is special in that it discards the return type. |
| 7619 | /// So if the return type is !Unpin, it needs to emplace!() it. |
| 7620 | #[test] |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 7621 | fn test_nonunpin_return_assign() -> Result<()> { |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 7622 | let ir = ir_from_cc( |
| 7623 | r#"#pragma clang lifetime_elision |
| 7624 | // This type must be `!Unpin`. |
| 7625 | struct Nontrivial { |
| 7626 | ~Nontrivial(); |
| 7627 | Nontrivial operator=(const Nontrivial& other); |
| 7628 | }; |
| 7629 | "#, |
| 7630 | )?; |
| 7631 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 7632 | assert_rs_matches!( |
| 7633 | rs_api, |
| 7634 | quote! { |
| 7635 | impl<'b> ::ctor::Assign<&'b crate::Nontrivial> for Nontrivial { |
| 7636 | #[inline(always)] |
| 7637 | fn assign<'a>(self: ::std::pin::Pin<&'a mut Self>, other: &'b crate::Nontrivial) { |
| 7638 | unsafe { |
| 7639 | let _ = ::ctor::emplace!(::ctor::FnCtor::new( |
| 7640 | move |dest: ::std::pin::Pin<&mut ::std::mem::MaybeUninit<crate::Nontrivial>>| { |
| 7641 | crate::detail::__rust_thunk___ZN10NontrivialaSERKS_( |
| 7642 | ::std::pin::Pin::into_inner_unchecked(dest), |
| 7643 | self, |
| 7644 | other |
| 7645 | ); |
| 7646 | } |
| 7647 | )); |
| 7648 | } |
| 7649 | } |
| 7650 | } |
| 7651 | } |
| 7652 | ); |
| 7653 | |
| 7654 | assert_cc_matches!( |
| 7655 | rs_api_impl, |
| 7656 | quote! { |
| 7657 | extern "C" void __rust_thunk___ZN10NontrivialaSERKS_( |
| 7658 | struct Nontrivial* __return, struct Nontrivial* __this, |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 7659 | const struct Nontrivial* other |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 7660 | ) { |
Devin Jeanpierre | 521b4ee | 2022-07-27 07:19:50 -0700 | [diff] [blame] | 7661 | new(__return) auto(__this->operator=(*other)); |
Devin Jeanpierre | b8ce2c1 | 2022-07-13 10:34:01 -0700 | [diff] [blame] | 7662 | } |
| 7663 | } |
| 7664 | ); |
| 7665 | Ok(()) |
| 7666 | } |
| 7667 | |
| 7668 | #[test] |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 7669 | fn test_nonunpin_param() -> Result<()> { |
| 7670 | let ir = ir_from_cc( |
| 7671 | r#"#pragma clang lifetime_elision |
| 7672 | // This type must be `!Unpin`. |
| 7673 | struct Nontrivial { |
| 7674 | Nontrivial(Nontrivial&&); |
| 7675 | ~Nontrivial(); |
| 7676 | }; |
| 7677 | |
| 7678 | void TakesByValue(Nontrivial x); |
| 7679 | "#, |
| 7680 | )?; |
| 7681 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 7682 | assert_rs_matches!( |
| 7683 | rs_api, |
| 7684 | quote! { |
| 7685 | pub fn TakesByValue(x: impl ::ctor::Ctor<Output=crate::Nontrivial>) { |
| 7686 | unsafe { |
| 7687 | crate::detail::__rust_thunk___Z12TakesByValue10Nontrivial(::std::pin::Pin::into_inner_unchecked(::ctor::emplace!(x))) |
| 7688 | } |
| 7689 | } |
| 7690 | } |
| 7691 | ); |
| 7692 | |
| 7693 | assert_cc_matches!( |
| 7694 | rs_api_impl, |
| 7695 | quote! { |
| 7696 | extern "C" void __rust_thunk___Z12TakesByValue10Nontrivial(struct Nontrivial*x) { |
| 7697 | TakesByValue(std::move(*x)); |
| 7698 | } |
| 7699 | } |
| 7700 | ); |
| 7701 | Ok(()) |
| 7702 | } |
| 7703 | |
| 7704 | #[test] |
| 7705 | fn test_nonunpin_trait_param() -> Result<()> { |
| 7706 | let ir = ir_from_cc( |
| 7707 | r#"#pragma clang lifetime_elision |
| 7708 | // This type must be `!Unpin`. |
| 7709 | struct Nontrivial { |
| 7710 | Nontrivial(Nontrivial&&); |
| 7711 | Nontrivial& operator=(Nontrivial) {} |
| 7712 | ~Nontrivial(); |
| 7713 | }; |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 7714 | |
| 7715 | struct Trivial final { |
| 7716 | /*implicit*/ Trivial(Nontrivial) {} |
| 7717 | }; |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 7718 | "#, |
| 7719 | )?; |
| 7720 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 7721 | assert_rs_matches!( |
| 7722 | rs_api, |
| 7723 | quote! { |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 7724 | impl<'__param_0> From<::ctor::RvalueReference<'__param_0, crate::Nontrivial>> for Trivial { |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 7725 | #[inline(always)] |
Devin Jeanpierre | 6bb8180 | 2022-08-10 02:08:47 -0700 | [diff] [blame] | 7726 | fn from(__param_0: ::ctor::RvalueReference<'__param_0, crate::Nontrivial>) -> Self { |
Devin Jeanpierre | 2b1e46d | 2022-08-01 13:42:12 -0700 | [diff] [blame] | 7727 | let mut tmp = ::std::mem::MaybeUninit::<Self>::zeroed(); |
| 7728 | unsafe { |
| 7729 | crate::detail::__rust_thunk___ZN7TrivialC1E10Nontrivial( |
| 7730 | &mut tmp, |
| 7731 | __param_0 |
| 7732 | ); |
| 7733 | tmp.assume_init() |
| 7734 | } |
| 7735 | } |
| 7736 | } |
| 7737 | } |
| 7738 | ); |
Devin Jeanpierre | 11ce2b9 | 2022-07-27 07:54:21 -0700 | [diff] [blame] | 7739 | Ok(()) |
| 7740 | } |
| 7741 | |
| 7742 | #[test] |
| 7743 | fn test_nonmovable_param() -> Result<()> { |
| 7744 | let ir = ir_from_cc( |
| 7745 | r#"#pragma clang lifetime_elision |
| 7746 | // This type must be `!Unpin` and non-move constructible. |
| 7747 | struct Nonmovable { |
| 7748 | Nonmovable(Nonmovable&&) = delete; |
| 7749 | }; |
| 7750 | |
| 7751 | void TakesByValue(Nonmovable) {} |
| 7752 | "#, |
| 7753 | )?; |
| 7754 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 7755 | // Bindings for TakesByValue cannot be generated. |
| 7756 | assert_rs_not_matches!(rs_api, quote! {TakesByValue}); |
| 7757 | assert_cc_not_matches!(rs_api_impl, quote! {TakesByValue}); |
| 7758 | Ok(()) |
| 7759 | } |
| 7760 | |
| 7761 | #[test] |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 7762 | fn test_function_returning_rvalue_reference() -> Result<()> { |
| 7763 | let ir = ir_from_cc( |
| 7764 | r#"#pragma clang lifetime_elision |
| 7765 | struct SomeStruct final { |
| 7766 | // Inline to force generation (and test coverage) of C++ thunks. |
| 7767 | inline SomeStruct&& GetRValueReference() { |
| 7768 | return static_cast<SomeStruct&&>(*this); |
| 7769 | } |
| 7770 | int field; |
| 7771 | }; |
| 7772 | "#, |
| 7773 | )?; |
| 7774 | let BindingsTokens { rs_api, rs_api_impl } = generate_bindings_tokens(ir)?; |
| 7775 | assert_rs_matches!( |
| 7776 | rs_api, |
| 7777 | quote! { |
| 7778 | impl SomeStruct { |
| 7779 | ... |
| 7780 | #[inline(always)] |
| 7781 | pub fn GetRValueReference<'a>(&'a mut self) |
| 7782 | -> ::ctor::RvalueReference<'a, crate::SomeStruct> { |
| 7783 | unsafe { |
| 7784 | crate::detail::__rust_thunk___ZN10SomeStruct18GetRValueReferenceEv(self) |
| 7785 | } |
| 7786 | } |
| 7787 | } |
| 7788 | } |
| 7789 | ); |
| 7790 | assert_rs_matches!( |
| 7791 | rs_api, |
| 7792 | quote! { |
| 7793 | extern "C" { |
| 7794 | ... |
| 7795 | pub(crate) fn __rust_thunk___ZN10SomeStruct18GetRValueReferenceEv<'a>( |
| 7796 | __this: &'a mut crate::SomeStruct |
| 7797 | ) -> ::ctor::RvalueReference<'a, crate::SomeStruct>; |
| 7798 | ... |
| 7799 | } |
| 7800 | } |
| 7801 | ); |
| 7802 | |
| 7803 | // Note that you can't just convert directly from xvalue to lvalue: |
| 7804 | // |
| 7805 | // return &static_cast<SomeStruct&>(__this->GetRValueReference()); |
| 7806 | // |
| 7807 | // For the above, Clang will emit an error that "non-const lvalue reference to |
| 7808 | // type 'struct SomeStruct' cannot bind to a temporary of type |
| 7809 | // 'SomeStruct'" (This is somewhat misleading, because there are no |
| 7810 | // temporaries here). We must first bind the return value to a name |
| 7811 | // (`lvalue` below), so that it becomes an lvalue. Only then can it be |
| 7812 | // converted to a pointer. |
| 7813 | assert_cc_matches!( |
| 7814 | rs_api_impl, |
| 7815 | quote! { |
| 7816 | extern "C" struct SomeStruct* |
| 7817 | __rust_thunk___ZN10SomeStruct18GetRValueReferenceEv(struct SomeStruct* __this) { |
| 7818 | struct SomeStruct&& lvalue = __this->GetRValueReference(); |
| 7819 | return &lvalue; |
| 7820 | } |
| 7821 | } |
| 7822 | ); |
| 7823 | |
| 7824 | Ok(()) |
| 7825 | } |
| 7826 | |
| 7827 | #[test] |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 7828 | fn test_forward_declared() -> Result<()> { |
| 7829 | let ir = ir_from_cc( |
| 7830 | r#"#pragma clang lifetime_elision |
| 7831 | struct ForwardDeclared;"#, |
| 7832 | )?; |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7833 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
Devin Jeanpierre | c0543eb | 2022-04-20 16:00:34 -0700 | [diff] [blame] | 7834 | assert_rs_matches!( |
| 7835 | rs_api, |
| 7836 | quote! { |
| 7837 | forward_declare::forward_declare!(pub ForwardDeclared = forward_declare::symbol!("ForwardDeclared")); |
| 7838 | } |
| 7839 | ); |
| 7840 | assert_rs_not_matches!(rs_api, quote! {struct ForwardDeclared}); |
| 7841 | Ok(()) |
| 7842 | } |
| 7843 | |
| 7844 | #[test] |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7845 | fn test_namespace_module_items() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7846 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7847 | r#" |
| 7848 | namespace test_namespace_bindings { |
| 7849 | int func(); |
| 7850 | struct S {}; |
| 7851 | namespace inner { |
| 7852 | int inner_func(); |
| 7853 | struct InnerS {}; |
| 7854 | } |
| 7855 | } |
| 7856 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7857 | )?)? |
| 7858 | .rs_api; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7859 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7860 | rs_api, |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7861 | quote! { |
| 7862 | pub mod test_namespace_bindings { |
| 7863 | ... |
| 7864 | pub fn func() -> i32 { ... } |
| 7865 | ... |
| 7866 | pub struct S { ... } |
| 7867 | ... |
| 7868 | pub mod inner { |
| 7869 | ... |
| 7870 | pub fn inner_func() -> i32 { ... } |
| 7871 | ... |
| 7872 | pub struct InnerS { ... } |
| 7873 | ... |
| 7874 | } |
| 7875 | ... |
| 7876 | } |
| 7877 | } |
| 7878 | ); |
| 7879 | Ok(()) |
| 7880 | } |
| 7881 | |
| 7882 | #[test] |
Rosica Dejanovska | 5d9faaf | 2022-05-11 04:30:04 -0700 | [diff] [blame] | 7883 | fn test_detail_outside_of_namespace_module() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7884 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7885 | r#" |
| 7886 | namespace test_namespace_bindings { |
| 7887 | int f(); |
| 7888 | } |
| 7889 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7890 | )?)? |
| 7891 | .rs_api; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7892 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7893 | rs_api, |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7894 | quote! { |
| 7895 | pub mod test_namespace_bindings { |
| 7896 | ... |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7897 | } |
Rosica Dejanovska | 5d9faaf | 2022-05-11 04:30:04 -0700 | [diff] [blame] | 7898 | ... |
| 7899 | mod detail { |
| 7900 | #[allow(unused_imports)] |
| 7901 | use super::*; |
| 7902 | extern "C" { |
| 7903 | #[link_name = "_ZN23test_namespace_bindings1fEv"] |
| 7904 | pub(crate) fn __rust_thunk___ZN23test_namespace_bindings1fEv() -> i32; |
| 7905 | } |
| 7906 | } |
| 7907 | ... |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7908 | } |
| 7909 | ); |
| 7910 | Ok(()) |
| 7911 | } |
| 7912 | |
| 7913 | #[test] |
Rosica Dejanovska | 5d9faaf | 2022-05-11 04:30:04 -0700 | [diff] [blame] | 7914 | fn test_assertions_outside_of_namespace_module() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7915 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7916 | r#" |
| 7917 | namespace test_namespace_bindings { |
| 7918 | struct S { |
| 7919 | int i; |
| 7920 | }; |
| 7921 | } |
| 7922 | "#, |
Devin Jeanpierre | 4f06f83 | 2022-04-26 15:51:30 -0700 | [diff] [blame] | 7923 | )?)? |
| 7924 | .rs_api; |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7925 | assert_rs_matches!( |
Devin Jeanpierre | 448322b | 2022-04-26 15:43:40 -0700 | [diff] [blame] | 7926 | rs_api, |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7927 | quote! { |
| 7928 | pub mod test_namespace_bindings { |
| 7929 | ... |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7930 | } |
Rosica Dejanovska | 5d9faaf | 2022-05-11 04:30:04 -0700 | [diff] [blame] | 7931 | ... |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 7932 | const _: () = assert!(::std::mem::size_of::<crate::test_namespace_bindings::S>() == 4); |
| 7933 | const _: () = assert!(::std::mem::align_of::<crate::test_namespace_bindings::S>() == 4); |
Rosica Dejanovska | 5d9faaf | 2022-05-11 04:30:04 -0700 | [diff] [blame] | 7934 | ... |
Lukasz Anforowicz | ed94b24 | 2022-09-07 11:47:58 -0700 | [diff] [blame] | 7935 | const _: () = assert!(memoffset::offset_of!(crate::test_namespace_bindings::S, i) == 0); |
Rosica Dejanovska | dd9a903 | 2022-04-12 07:34:41 -0700 | [diff] [blame] | 7936 | } |
| 7937 | ); |
| 7938 | Ok(()) |
| 7939 | } |
Rosica Dejanovska | 93aeafb | 2022-06-01 07:05:31 -0700 | [diff] [blame] | 7940 | |
| 7941 | #[test] |
| 7942 | fn test_reopened_namespaces() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7943 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | 93aeafb | 2022-06-01 07:05:31 -0700 | [diff] [blame] | 7944 | r#" |
| 7945 | namespace test_namespace_bindings { |
| 7946 | namespace inner {} |
| 7947 | } // namespace test_namespace_bindings |
| 7948 | |
| 7949 | namespace test_namespace_bindings { |
| 7950 | namespace inner {} |
| 7951 | } // namespace test_namespace_bindings"#, |
| 7952 | )?)? |
| 7953 | .rs_api; |
| 7954 | |
| 7955 | assert_rs_matches!( |
| 7956 | rs_api, |
| 7957 | quote! { |
| 7958 | ... |
| 7959 | pub mod test_namespace_bindings_0 { |
| 7960 | pub mod inner_0 {} ... |
| 7961 | } |
| 7962 | ... |
| 7963 | pub mod test_namespace_bindings { |
| 7964 | pub use super::test_namespace_bindings_0::*; |
| 7965 | ... |
| 7966 | pub mod inner { |
| 7967 | pub use super::inner_0::*; |
| 7968 | ... |
| 7969 | } |
| 7970 | } |
| 7971 | ... |
| 7972 | } |
| 7973 | ); |
| 7974 | Ok(()) |
| 7975 | } |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 7976 | |
| 7977 | #[test] |
| 7978 | fn test_qualified_identifiers_in_impl_file() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 7979 | let rs_api_impl = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 7980 | r#" |
| 7981 | namespace test_namespace_bindings { |
| 7982 | inline void f() {}; |
Devin Jeanpierre | cc61dad | 2022-07-19 01:40:09 -0700 | [diff] [blame] | 7983 | struct S final {}; |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 7984 | } |
| 7985 | inline void useS(test_namespace_bindings::S s) {};"#, |
| 7986 | )?)? |
| 7987 | .rs_api_impl; |
| 7988 | |
| 7989 | assert_cc_matches!( |
| 7990 | rs_api_impl, |
| 7991 | quote! { |
| 7992 | extern "C" void __rust_thunk___ZN23test_namespace_bindings1fEv() { |
| 7993 | test_namespace_bindings::f(); |
| 7994 | } |
| 7995 | ... |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 7996 | extern "C" void __rust_thunk___Z4useSN23test_namespace_bindings1SE( |
Lukasz Anforowicz | 07b3390 | 2022-07-13 15:27:03 -0700 | [diff] [blame] | 7997 | struct test_namespace_bindings::S s) { useS(s); } |
Rosica Dejanovska | a08b386 | 2022-06-02 08:22:49 -0700 | [diff] [blame] | 7998 | ... |
| 7999 | } |
| 8000 | ); |
| 8001 | Ok(()) |
| 8002 | } |
Rosica Dejanovska | 4180ffe | 2022-06-09 06:01:05 -0700 | [diff] [blame] | 8003 | |
| 8004 | #[test] |
| 8005 | fn test_inline_namespace() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 8006 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | 4180ffe | 2022-06-09 06:01:05 -0700 | [diff] [blame] | 8007 | r#" |
| 8008 | namespace test_namespace_bindings { |
| 8009 | inline namespace inner { |
Devin Jeanpierre | cc61dad | 2022-07-19 01:40:09 -0700 | [diff] [blame] | 8010 | struct MyStruct final {}; |
Rosica Dejanovska | 4180ffe | 2022-06-09 06:01:05 -0700 | [diff] [blame] | 8011 | } |
| 8012 | void processMyStruct(MyStruct s); |
| 8013 | } |
| 8014 | void processMyStructOutsideNamespace(test_namespace_bindings::inner::MyStruct s); |
| 8015 | void processMyStructSkipInlineNamespaceQualifier(test_namespace_bindings::MyStruct s); |
| 8016 | "#, |
| 8017 | )?)? |
| 8018 | .rs_api; |
| 8019 | |
| 8020 | assert_rs_matches!( |
| 8021 | rs_api, |
| 8022 | quote! { |
| 8023 | ... |
| 8024 | pub mod test_namespace_bindings { |
| 8025 | ... |
| 8026 | pub mod inner { |
| 8027 | ... |
| 8028 | pub struct MyStruct {...} ... |
| 8029 | } |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 8030 | pub use inner::*; |
Rosica Dejanovska | 4180ffe | 2022-06-09 06:01:05 -0700 | [diff] [blame] | 8031 | ... |
| 8032 | pub fn processMyStruct(s: crate::test_namespace_bindings::inner::MyStruct) |
| 8033 | ... |
| 8034 | } |
| 8035 | ... |
| 8036 | pub fn processMyStructOutsideNamespace(s: crate::test_namespace_bindings::inner::MyStruct) |
| 8037 | ... |
| 8038 | pub fn processMyStructSkipInlineNamespaceQualifier(s: crate::test_namespace_bindings::inner::MyStruct) |
| 8039 | ... |
| 8040 | } |
| 8041 | ); |
| 8042 | Ok(()) |
| 8043 | } |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8044 | |
| 8045 | #[test] |
Devin Jeanpierre | fe6aaea | 2022-09-09 12:33:50 -0700 | [diff] [blame] | 8046 | fn test_inline_namespace_not_marked_inline() -> Result<()> { |
| 8047 | let rs_api = generate_bindings_tokens(ir_from_cc( |
| 8048 | r#" |
| 8049 | inline namespace my_inline {} |
| 8050 | namespace foo {} |
| 8051 | namespace my_inline { // still an inline namespace! |
| 8052 | struct MyStruct final {}; |
| 8053 | } |
| 8054 | "#, |
| 8055 | )?)? |
| 8056 | .rs_api; |
| 8057 | |
| 8058 | assert_rs_matches!( |
| 8059 | rs_api, |
| 8060 | quote! { |
| 8061 | ... |
| 8062 | pub mod my_inline_0 {} |
| 8063 | pub mod foo {} |
| 8064 | pub mod my_inline { |
| 8065 | pub use super::my_inline_0::*; |
| 8066 | ... |
| 8067 | pub struct MyStruct {...} |
| 8068 | ... |
| 8069 | } |
| 8070 | pub use my_inline::*; |
| 8071 | ... |
| 8072 | } |
| 8073 | ); |
| 8074 | Ok(()) |
| 8075 | } |
| 8076 | |
| 8077 | #[test] |
Devin Jeanpierre | 4223859 | 2022-10-04 20:27:44 -0700 | [diff] [blame] | 8078 | fn test_private_struct_not_present() -> Result<()> { |
| 8079 | let ir = ir_from_cc(&with_lifetime_macros( |
| 8080 | r#"#pragma clang lifetime_elision |
| 8081 | template <typename T> class MyTemplate {}; |
| 8082 | class HasPrivateType { |
| 8083 | private: |
| 8084 | struct PrivateType { |
| 8085 | using Foo = MyTemplate<PrivateType>; |
| 8086 | Foo* get(); |
| 8087 | }; |
| 8088 | protected: |
| 8089 | HasPrivateType(MyTemplate<PrivateType> x) {} |
| 8090 | };"#, |
| 8091 | ))?; |
| 8092 | let rs_api = generate_bindings_tokens(ir)?.rs_api; |
| 8093 | |
| 8094 | assert_rs_not_matches!( |
| 8095 | rs_api, |
| 8096 | quote! { __CcTemplateInst10MyTemplateIN14HasPrivateType11PrivateTypeEE } |
| 8097 | ); |
| 8098 | Ok(()) |
| 8099 | } |
| 8100 | |
| 8101 | #[test] |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8102 | fn test_implicit_template_specializations_are_sorted_by_mangled_name() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 8103 | let bindings = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8104 | r#" |
| 8105 | template <typename T> |
| 8106 | struct MyStruct { |
| 8107 | T getT(); |
| 8108 | }; |
| 8109 | |
| 8110 | using Alias1 = MyStruct<int>; |
| 8111 | using Alias2 = MyStruct<double>; |
| 8112 | |
| 8113 | namespace test_namespace_bindings { |
| 8114 | using Alias3 = MyStruct<bool>; |
| 8115 | } |
| 8116 | "#, |
| 8117 | )?)?; |
| 8118 | |
| 8119 | // Mangled name order: bool < double < int |
| 8120 | let my_struct_bool = make_rs_ident("__CcTemplateInst8MyStructIbE"); |
| 8121 | let my_struct_double = make_rs_ident("__CcTemplateInst8MyStructIdE"); |
| 8122 | let my_struct_int = make_rs_ident("__CcTemplateInst8MyStructIiE"); |
| 8123 | |
| 8124 | assert_rs_matches!( |
| 8125 | &bindings.rs_api, |
| 8126 | quote! { |
| 8127 | ... |
| 8128 | pub struct #my_struct_bool {...} |
| 8129 | ... |
| 8130 | pub struct #my_struct_double {...} |
| 8131 | ... |
| 8132 | pub struct #my_struct_int {...} |
| 8133 | ... |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 8134 | const _: () = assert!(::std::mem::size_of::<crate::#my_struct_bool>() == 1); |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8135 | ... |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 8136 | const _: () = assert!(::std::mem::size_of::<crate::#my_struct_double>() == 1); |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8137 | ... |
Devin Jeanpierre | cd5ebf3 | 2022-07-08 05:31:55 -0700 | [diff] [blame] | 8138 | const _: () = assert!(::std::mem::size_of::<crate::#my_struct_int>() == 1); |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8139 | ... |
| 8140 | } |
| 8141 | ); |
| 8142 | |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8143 | // User defined methods in mangled name order |
| 8144 | let my_struct_bool_method = |
| 8145 | make_rs_ident("__rust_thunk___ZN8MyStructIbE4getTEv__2f_2ftest_3atesting_5ftarget"); |
| 8146 | let my_struct_double_method = |
| 8147 | make_rs_ident("__rust_thunk___ZN8MyStructIdE4getTEv__2f_2ftest_3atesting_5ftarget"); |
| 8148 | let my_struct_int_method = |
| 8149 | make_rs_ident("__rust_thunk___ZN8MyStructIiE4getTEv__2f_2ftest_3atesting_5ftarget"); |
| 8150 | |
| 8151 | assert_cc_matches!( |
| 8152 | &bindings.rs_api_impl, |
| 8153 | quote! { |
| 8154 | ... |
Lukasz Anforowicz | d4742ff | 2022-07-11 17:05:02 -0700 | [diff] [blame] | 8155 | extern "C" bool #my_struct_bool_method(struct MyStruct<bool>*__this) {...} ... |
| 8156 | extern "C" double #my_struct_double_method(struct MyStruct<double>*__this) {...} ... |
| 8157 | extern "C" int #my_struct_int_method(struct MyStruct<int>*__this) {...} ... |
Rosica Dejanovska | 2708a5f | 2022-06-22 06:54:59 -0700 | [diff] [blame] | 8158 | } |
| 8159 | ); |
| 8160 | Ok(()) |
| 8161 | } |
Rosica Dejanovska | f9787c9 | 2022-06-22 12:14:57 -0700 | [diff] [blame] | 8162 | |
| 8163 | #[test] |
| 8164 | fn test_implicit_template_specialization_namespace_qualifier() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 8165 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | f9787c9 | 2022-06-22 12:14:57 -0700 | [diff] [blame] | 8166 | r#" #pragma clang lifetime_elision |
| 8167 | namespace test_namespace_bindings { |
| 8168 | template <typename T> |
| 8169 | struct MyTemplate final { |
| 8170 | T value_; |
| 8171 | }; |
| 8172 | |
| 8173 | using MyTypeAlias = MyTemplate<int>; |
| 8174 | }"#, |
| 8175 | )?)? |
| 8176 | .rs_api; |
| 8177 | |
| 8178 | assert_rs_matches!( |
| 8179 | rs_api, |
| 8180 | quote! { |
| 8181 | ... |
| 8182 | pub mod test_namespace_bindings { |
| 8183 | ... |
| 8184 | pub type MyTypeAlias = crate::__CcTemplateInstN23test_namespace_bindings10MyTemplateIiEE; |
| 8185 | ... |
| 8186 | } |
| 8187 | ... |
| 8188 | pub struct __CcTemplateInstN23test_namespace_bindings10MyTemplateIiEE { |
| 8189 | pub value_: i32, |
| 8190 | } |
| 8191 | ... |
| 8192 | } |
| 8193 | ); |
| 8194 | Ok(()) |
| 8195 | } |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 8196 | |
| 8197 | #[test] |
| 8198 | fn test_forward_declared_class_template_specialization_symbol() -> Result<()> { |
Devin Jeanpierre | e9850a7 | 2022-06-29 19:04:48 -0700 | [diff] [blame] | 8199 | let rs_api = generate_bindings_tokens(ir_from_cc( |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 8200 | r#" |
| 8201 | namespace test_namespace_bindings { |
| 8202 | template <typename T> |
| 8203 | struct MyTemplate { |
| 8204 | void processT(T t); |
| 8205 | }; |
| 8206 | |
| 8207 | struct Param {}; |
| 8208 | |
| 8209 | template<> struct MyTemplate<Param>; |
Lukasz Anforowicz | 38310f3 | 2022-09-09 11:17:52 -0700 | [diff] [blame] | 8210 | |
| 8211 | using MyTypeAlias = MyTemplate<Param>; |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 8212 | }"#, |
| 8213 | )?)? |
| 8214 | .rs_api; |
| 8215 | |
| 8216 | assert_rs_matches!( |
| 8217 | rs_api, |
| 8218 | quote! { |
| 8219 | ... |
Lukasz Anforowicz | 38310f3 | 2022-09-09 11:17:52 -0700 | [diff] [blame] | 8220 | forward_declare::forward_declare!(pub __CcTemplateInstN23test_namespace_bindings10MyTemplateINS_5ParamEEE = forward_declare::symbol!("__CcTemplateInstN23test_namespace_bindings10MyTemplateINS_5ParamEEE")); |
Rosica Dejanovska | e12d717 | 2022-06-22 12:20:17 -0700 | [diff] [blame] | 8221 | ... |
| 8222 | } |
| 8223 | ); |
| 8224 | Ok(()) |
| 8225 | } |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 8226 | |
| 8227 | #[test] |
| 8228 | fn test_lifetime_elision_for_references() { |
| 8229 | let type_args: &[RsTypeKind] = &[]; |
| 8230 | let referent = Rc::new(RsTypeKind::Other { name: "T".into(), type_args: type_args.into() }); |
| 8231 | let reference = RsTypeKind::Reference { |
| 8232 | referent: referent, |
| 8233 | mutability: Mutability::Const, |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 8234 | lifetime: Lifetime::new("_"), |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 8235 | }; |
| 8236 | assert_rs_matches!(quote! {#reference}, quote! {&T}); |
| 8237 | } |
| 8238 | |
| 8239 | #[test] |
| 8240 | fn test_lifetime_elision_for_rvalue_references() { |
| 8241 | let type_args: &[RsTypeKind] = &[]; |
| 8242 | let referent = Rc::new(RsTypeKind::Other { name: "T".into(), type_args: type_args.into() }); |
| 8243 | let reference = RsTypeKind::RvalueReference { |
| 8244 | referent: referent, |
| 8245 | mutability: Mutability::Mut, |
Devin Jeanpierre | 4e94a08 | 2022-08-10 01:58:35 -0700 | [diff] [blame] | 8246 | lifetime: Lifetime::new("_"), |
Devin Jeanpierre | 7ed8c6f | 2022-08-01 13:41:02 -0700 | [diff] [blame] | 8247 | }; |
| 8248 | assert_rs_matches!(quote! {#reference}, quote! {RvalueReference<'_, T>}); |
| 8249 | } |
Marcel Hlopko | 42abfc8 | 2021-08-09 07:03:17 +0000 | [diff] [blame] | 8250 | } |