diff --git a/libcc2rs/build.rs b/libcc2rs/build.rs index f5c6251b89..32ae7df7f0 100644 --- a/libcc2rs/build.rs +++ b/libcc2rs/build.rs @@ -41,7 +41,7 @@ fn main() { ); buf.push_str(&format!( - "#[path = r#\"{}\"#]\nmod {};\npub use {}::*;\n", + "#[path = r#\"{}\"#]\nmod {};\n#[allow(unused_imports)]\npub use {}::*;\n", shim.display(), module_name, module_name diff --git a/libcc2rs/src/reinterpret.rs b/libcc2rs/src/reinterpret.rs index 9e8e494081..eaa392bc31 100644 --- a/libcc2rs/src/reinterpret.rs +++ b/libcc2rs/src/reinterpret.rs @@ -116,8 +116,6 @@ impl ByteRepr for bool { } impl ByteRepr for () {} -impl ByteRepr for std::fs::File {} -impl ByteRepr for Vec {} impl ByteRepr for Option {} // The bytes of a Value, like a field stored in one, are those of its value. impl ByteRepr for std::rc::Rc { @@ -192,9 +190,6 @@ impl ByteRepr for Box<[T]> { impl ByteRepr for Box {} impl ByteRepr for *const T {} impl ByteRepr for *mut T {} -impl ByteRepr for (A, B) {} -impl ByteRepr for std::collections::BTreeMap {} -impl ByteRepr for std::rc::Rc {} // Runs `f` with a zeroed scratch buffer of `len` elements. Small buffers live // on the stack so that accessing memory through a reinterpreted pointer does diff --git a/rules/any/shim.rs b/rules/any/shim.rs new file mode 100644 index 0000000000..c17aa79222 --- /dev/null +++ b/rules/any/shim.rs @@ -0,0 +1,6 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use crate::ByteRepr; + +impl ByteRepr for std::rc::Rc {} diff --git a/rules/compare/shim.rs b/rules/compare/shim.rs new file mode 100644 index 0000000000..a614f8870f --- /dev/null +++ b/rules/compare/shim.rs @@ -0,0 +1,16 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use crate::{ArgRepr, ByteRepr, FnPtrArg, record_from_repr}; + +impl ByteRepr for std::cmp::Ordering {} + +impl FnPtrArg for std::cmp::Ordering { + #[inline] + fn to_repr(self) -> ArgRepr { + ArgRepr::Record(Box::new(self)) + } + fn from_repr(r: ArgRepr) -> Self { + record_from_repr(r) + } +} diff --git a/rules/fstream/shim.rs b/rules/fstream/shim.rs new file mode 100644 index 0000000000..3333da0194 --- /dev/null +++ b/rules/fstream/shim.rs @@ -0,0 +1,6 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use crate::ByteRepr; + +impl ByteRepr for std::fs::File {} diff --git a/rules/map/shim.rs b/rules/map/shim.rs index 5bad8832e1..2372f11aa6 100644 --- a/rules/map/shim.rs +++ b/rules/map/shim.rs @@ -12,6 +12,8 @@ use crate::{ UnsafeBidirectionalIterator, UnsafeCppIterator, Value, cpp_cmp, }; +impl ByteRepr for BTreeMap {} + // A map keeps a single copy of each key, as C++ does, since copying a key may // have side effects. Its index points to the keys of the entries, is ordered // by them with operator< (CppLess), and is searched for a key through a diff --git a/rules/pair/shim.rs b/rules/pair/shim.rs index c439c21214..bb5269cd5b 100644 --- a/rules/pair/shim.rs +++ b/rules/pair/shim.rs @@ -1,7 +1,7 @@ // Copyright (c) 2022-present INESC-ID. // Distributed under the MIT license that can be found in the LICENSE file. -use crate::Value; +use crate::{ArgRepr, ByteRepr, FnPtrArg, Value, record_from_repr}; pub trait TupleElement { type Type; @@ -76,3 +76,15 @@ macro_rules! impl_tuple_get { } impl_tuple_get!((T0, T1); 0 T0, 1 T1); + +impl ByteRepr for (A, B) {} + +impl FnPtrArg for (A, B) { + #[inline] + fn to_repr(self) -> ArgRepr { + ArgRepr::Record(Box::new(self)) + } + fn from_repr(r: ArgRepr) -> Self { + record_from_repr(r) + } +} diff --git a/rules/vector/shim.rs b/rules/vector/shim.rs new file mode 100644 index 0000000000..40c224d6eb --- /dev/null +++ b/rules/vector/shim.rs @@ -0,0 +1,16 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use crate::{ArgRepr, ByteRepr, FnPtrArg, record_from_repr}; + +impl ByteRepr for Vec {} + +impl FnPtrArg for Vec { + #[inline] + fn to_repr(self) -> ArgRepr { + ArgRepr::Record(Box::new(self)) + } + fn from_repr(r: ArgRepr) -> Self { + record_from_repr(r) + } +} diff --git a/tests/unit/fn_ptr.cpp b/tests/unit/fn_ptr.cpp index cfe3d2a6ef..c2497c6935 100644 --- a/tests/unit/fn_ptr.cpp +++ b/tests/unit/fn_ptr.cpp @@ -1,7 +1,12 @@ +// ADDITIONAL_COMPILE_FLAGS: -std=c++20 #include +#include #include #include #include +#include +#include +#include typedef int (*foo_t)(void *); @@ -33,6 +38,14 @@ static int total = 0; void add_to_total(int x) { total += x; } +int pair_sum(std::pair p) { return p.first + p.second; } + +size_t string_size(std::string s) { return s.size(); } + +int vector_sum(std::vector v) { return v[0] + v[1]; } + +bool is_less(std::strong_ordering o) { return o < 0; } + int main() { foo_t fn = nullptr; assert(fn == nullptr); @@ -65,5 +78,22 @@ int main() { handler(2); handler(3); assert(total == 5); + + int (*pf)(std::pair) = pair_sum; + assert(pf({1, 2}) == 3); + + size_t (*sf)(std::string) = string_size; + assert(sf(std::string("abc")) == 3); + + int (*vf)(std::vector) = vector_sum; + assert(vf({1, 2}) == 3); + + bool (*of)(std::strong_ordering) = is_less; + assert(of(1 <=> 2)); + assert(!of(2 <=> 1)); + + std::strong_ordering o = 1 <=> 2; + std::strong_ordering *op = &o; + assert(*op == std::strong_ordering::less); return 0; } diff --git a/tests/unit/out/refcount/fn_ptr.rs b/tests/unit/out/refcount/fn_ptr.rs index 1008d385b7..4fcc389a67 100644 --- a/tests/unit/out/refcount/fn_ptr.rs +++ b/tests/unit/out/refcount/fn_ptr.rs @@ -47,6 +47,20 @@ thread_local!( pub fn add_to_total_8(mut x: i32) { total_7.with(|rc| *rc.borrow_mut() += x); } +pub fn pair_sum_9(p: (Value, Value)) -> i32 { + let p: Value<(Value, Value)> = Rc::new(RefCell::new(p)); + return ((*(*p.borrow()).0.borrow()) + (*(*p.borrow()).1.borrow())); +} +pub fn string_size_10(mut s: Vec) -> usize { + return (s.len() - 1); +} +pub fn vector_sum_11(mut v: Vec) -> i32 { + return (v[0_usize] + v[1_usize]); +} +pub fn is_less_12(o: std::cmp::Ordering) -> bool { + let o: Value = Rc::new(RefCell::new(o)); + return ({ (*o.borrow()) }) == std::cmp::Ordering::Less; +} pub fn main() { __cpp2rust_init_globals(); std::process::exit(main_0()); @@ -82,6 +96,28 @@ fn main_0() -> i32 { ({ handler.call(2) }); ({ handler.call(3) }); assert!((total_7.with(|rc| *rc.borrow()) == 5)); + let mut pf: FnPtr, Value)) -> i32> = + FnPtr::, Value)) -> i32>::new(pair_sum_9); + assert!((({ pf.call((Rc::new(RefCell::new(1)), Rc::new(RefCell::new(2))),) }) == 3)); + let mut sf: FnPtr) -> usize> = FnPtr::) -> usize>::new(string_size_10); + assert!( + (({ + sf.call({ + let mut __bytes = Ptr::::from_string_literal(b"abc").to_c_bytes(); + __bytes.push(0); + __bytes + }) + }) == 3_usize) + ); + let mut vf: FnPtr) -> i32> = FnPtr::) -> i32>::new(vector_sum_11); + assert!((({ vf.call(Vec::from([1, 2,]),) }) == 3)); + let mut of: FnPtr bool> = + FnPtr:: bool>::new(is_less_12); + assert!(({ of.call(std::cmp::Ord::cmp(&(1), &(2)),) })); + assert!(!({ of.call(std::cmp::Ord::cmp(&(2), &(1)),) })); + let o: Value = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2)))); + let mut op: Ptr = (o.as_pointer()); + assert!(({ (*op.upgrade().deref()) }) == std::cmp::Ordering::Less); return 0; } pub fn __cpp2rust_init_globals() { diff --git a/tests/unit/out/unsafe/fn_ptr.rs b/tests/unit/out/unsafe/fn_ptr.rs index 1ef79431be..6454351520 100644 --- a/tests/unit/out/unsafe/fn_ptr.rs +++ b/tests/unit/out/unsafe/fn_ptr.rs @@ -47,6 +47,18 @@ pub static mut total_7: std::cell::LazyCell = std::cell::LazyCell::new(|| u pub unsafe fn add_to_total_8(mut x: i32) { (*std::cell::LazyCell::force_mut(&mut *&raw mut total_7)) += x; } +pub unsafe fn pair_sum_9(mut p: (i32, i32)) -> i32 { + return ((p.0) + (p.1)); +} +pub unsafe fn string_size_10(mut s: Vec) -> usize { + return (s.len() - 1); +} +pub unsafe fn vector_sum_11(mut v: Vec) -> i32 { + return ((v[(0_usize)]) + (v[(1_usize)])); +} +pub unsafe fn is_less_12(mut o: std::cmp::Ordering) -> bool { + return o == std::cmp::Ordering::Less; +} pub fn main() { unsafe { __cpp2rust_init_globals(); @@ -85,6 +97,28 @@ unsafe fn main_0() -> i32 { (unsafe { (handler).unwrap()(2) }); (unsafe { (handler).unwrap()(3) }); assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut total_7)) == (5))); + let mut pf: Option i32> = Some(pair_sum_9); + assert!(((unsafe { (pf).unwrap()((1, 2),) }) == (3))); + let mut sf: Option) -> usize> = Some(string_size_10); + assert!( + ((unsafe { + (sf).unwrap()( + std::slice::from_raw_parts( + c"abc".as_ptr(), + (0..).take_while(|&i| *c"abc".as_ptr().add(i) != 0).count() + 1, + ) + .to_vec(), + ) + }) == (3_usize)) + ); + let mut vf: Option) -> i32> = Some(vector_sum_11); + assert!(((unsafe { (vf).unwrap()(Vec::from([1, 2,]),) }) == (3))); + let mut of: Option bool> = Some(is_less_12); + assert!((unsafe { (of).unwrap()(std::cmp::Ord::cmp(&(1), &(2)),) })); + assert!(!(unsafe { (of).unwrap()(std::cmp::Ord::cmp(&(2), &(1)),) })); + let mut o: std::cmp::Ordering = std::cmp::Ord::cmp(&(1), &(2)); + let mut op: *mut std::cmp::Ordering = (&mut o as *mut std::cmp::Ordering); + assert!((*op) == std::cmp::Ordering::Less); return 0; } pub unsafe fn __cpp2rust_init_globals() {