Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rejig typeconversionpolicy #1129

Closed
wants to merge 27 commits into from
Closed
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -139,6 +139,9 @@ jobs:
- name: Build non-trivial-type-on-stack example
working-directory: ./examples/non-trivial-type-on-stack
run: cargo build
- name: Build reference-wrappers example
working-directory: ./examples/reference-wrappers
run: cargo build
# We do not build the LLVM example because even 'apt-get install llvm-13-dev'
# does not work to install the LLVM 13 headers.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ moveit = { version = "0.5", features = [ "cxx" ] }

[workspace]
members = ["parser", "engine", "gen/cmd", "gen/build", "macro", "demo", "tools/reduce", "tools/mdbook-preprocessor", "integration-tests"]
exclude = ["examples/s2", "examples/steam-mini", "examples/subclass", "examples/chromium-fake-render-frame-host", "examples/pod", "examples/non-trivial-type-on-stack", "examples/llvm", "tools/stress-test"]
exclude = ["examples/s2", "examples/steam-mini", "examples/subclass", "examples/chromium-fake-render-frame-host", "examples/pod", "examples/non-trivial-type-on-stack", "examples/llvm", "examples/reference-wrappers", "tools/stress-test"]

#[patch.crates-io]
#cxx = { path="../cxx" }
102 changes: 78 additions & 24 deletions engine/src/conversion/analysis/fun/function_wrapper.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@ use crate::{
conversion::api::SubclassName,
types::{Namespace, QualifiedName},
};
use syn::{parse_quote, Ident, Type};
use quote::ToTokens;
use syn::{parse_quote, Ident, Type, TypeReference};

#[derive(Clone, Debug)]
pub(crate) enum CppConversionType {
@@ -23,6 +24,34 @@ pub(crate) enum CppConversionType {
/// Ignored in the sense that it isn't passed into the C++ function.
IgnoredPlacementPtrParameter,
FromReturnValueToPlacementPtr,
FromPointerToReference, // unwrapped_type is always Type::Ptr
FromReferenceToPointer, // unwrapped_type is always Type::Ptr
}

#[derive(Clone, Debug)]
pub(crate) enum RustConversionType {
None,
FromReturnValueToPlacementPtr, // like None
FromValueToValueToMove,
FromUniquePtrToUniquePtrToValue,
FromStrToUniquePtrToValue,
ToBoxedUpHolderToMove(SubclassName),
FromPinMaybeUninitToPtrToPtr,
FromPinMoveRefToPtrToPtr,
FromValueToPtrToPtr,
FromValueParamToPtrToValue,
FromPlacementParamToNewReturnToNone,
FromRValueParamToPtrToValue,
FromReferenceWrapperToPointerToReference, // unwrapped_type is always Type::Ptr
FromReferenceToPointerToReferenceWrapper, // unwrapped_type is always Type::Ptr
}

pub(crate) enum ConversionHint {
None,
FromValueToPtrToPtr,
FromPlacementParamToNewReturnToNone,
FromPinMaybeUninitToPtrToPtr,
FromPinMoveRefToPtrToPtr,
}

impl CppConversionType {
@@ -36,28 +65,17 @@ impl CppConversionType {
CppConversionType::FromValueToUniquePtr
}
CppConversionType::FromValueToUniquePtr => CppConversionType::FromUniquePtrToValue,
CppConversionType::FromPointerToReference => CppConversionType::FromReferenceToPointer,
CppConversionType::FromReferenceToPointer => CppConversionType::FromPointerToReference,
_ => panic!("Did not expect to have to invert this conversion"),
}
}
}

#[derive(Clone, Debug)]
pub(crate) enum RustConversionType {
None,
FromStr,
ToBoxedUpHolder(SubclassName),
FromPinMaybeUninitToPtr,
FromPinMoveRefToPtr,
FromTypeToPtr,
FromValueParamToPtr,
FromPlacementParamToNewReturn,
FromRValueParamToPtr,
}

impl RustConversionType {
pub(crate) fn requires_mutability(&self) -> Option<syn::token::Mut> {
match self {
Self::FromPinMoveRefToPtr => Some(parse_quote! { mut }),
Self::FromPinMoveRefToPtrToPtr => Some(parse_quote! { mut }),
_ => None,
}
}
@@ -74,27 +92,61 @@ impl RustConversionType {
/// * Finally, the actual C++ API receives a `std::string` by value.
/// The implementation here is distributed across this file, and
/// `function_wrapper_rs` and `function_wrapper_cpp`.
/// TODO: we should make this into a single enum, with the Type as enum
/// variant params. That would remove the possibility of various runtime
/// panics by enforcing (for example) that conversion from a pointer always
/// has a Type::Ptr.
#[derive(Clone)]
pub(crate) struct TypeConversionPolicy {
pub(crate) unwrapped_type: Type,
unwrapped_type: Type,
pub(crate) cpp_conversion: CppConversionType,
pub(crate) rust_conversion: RustConversionType,
}

impl TypeConversionPolicy {
pub(crate) fn new_unconverted(ty: Type) -> Self {
TypeConversionPolicy {
Self::new(ty, CppConversionType::None, RustConversionType::None)
}

pub(crate) fn new(
ty: Type,
cpp_conversion: CppConversionType,
rust_conversion: RustConversionType,
) -> Self {
Self {
unwrapped_type: ty,
cpp_conversion: CppConversionType::None,
rust_conversion: RustConversionType::None,
cpp_conversion,
rust_conversion,
}
}

pub(crate) fn cxxbridge_type(&self) -> &Type {
&self.unwrapped_type
}

pub(crate) fn return_reference_into_wrapper(ty: Type) -> Self {
let (unwrapped_type, is_mut) = match ty {
Type::Reference(TypeReference {
elem, mutability, ..
}) => (*elem, mutability.is_some()),
_ => panic!("Not a ptr: {}", ty.to_token_stream()),
};
TypeConversionPolicy {
unwrapped_type: if is_mut {
parse_quote! { *mut #unwrapped_type }
} else {
parse_quote! { *const #unwrapped_type }
},
cpp_conversion: CppConversionType::FromReferenceToPointer,
rust_conversion: RustConversionType::FromReferenceToPointerToReferenceWrapper,
}
}

pub(crate) fn new_to_unique_ptr(ty: Type) -> Self {
TypeConversionPolicy {
unwrapped_type: ty,
cpp_conversion: CppConversionType::FromValueToUniquePtr,
rust_conversion: RustConversionType::None,
rust_conversion: RustConversionType::FromUniquePtrToUniquePtrToValue,
}
}

@@ -105,7 +157,7 @@ impl TypeConversionPolicy {
// Rust conversion is marked as none here, since this policy
// will be applied to the return value, and the Rust-side
// shenanigans applies to the placement new *parameter*
rust_conversion: RustConversionType::None,
rust_conversion: RustConversionType::FromReturnValueToPlacementPtr,
}
}

@@ -158,9 +210,11 @@ impl TypeConversionPolicy {
pub(crate) fn bridge_unsafe_needed(&self) -> bool {
matches!(
self.rust_conversion,
RustConversionType::FromValueParamToPtr
| RustConversionType::FromRValueParamToPtr
| RustConversionType::FromPlacementParamToNewReturn
RustConversionType::FromValueParamToPtrToValue
| RustConversionType::FromRValueParamToPtrToValue
| RustConversionType::FromPlacementParamToNewReturnToNone
| RustConversionType::FromReferenceToPointerToReferenceWrapper { .. }
| RustConversionType::FromReferenceWrapperToPointerToReference { .. }
)
}

Loading