Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion libcc2rs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions libcc2rs/src/reinterpret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ impl ByteRepr for bool {
}

impl ByteRepr for () {}
impl ByteRepr for std::fs::File {}
impl<T: ByteRepr> ByteRepr for Vec<T> {}
impl<T: ByteRepr> ByteRepr for Option<T> {}
// The bytes of a Value, like a field stored in one, are those of its value.
impl<T: ByteRepr> ByteRepr for std::rc::Rc<T> {
Expand Down Expand Up @@ -192,9 +190,6 @@ impl<T: ByteRepr> ByteRepr for Box<[T]> {
impl<T: ByteRepr> ByteRepr for Box<T> {}
impl<T: 'static> ByteRepr for *const T {}
impl<T: 'static> ByteRepr for *mut T {}
impl<A: ByteRepr, B: ByteRepr> ByteRepr for (A, B) {}
impl<K: 'static, V: 'static> ByteRepr for std::collections::BTreeMap<K, V> {}
impl ByteRepr for std::rc::Rc<dyn std::any::Any> {}

// 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
Expand Down
6 changes: 6 additions & 0 deletions rules/any/shim.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::any::Any> {}
16 changes: 16 additions & 0 deletions rules/compare/shim.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions rules/fstream/shim.rs
Original file line number Diff line number Diff line change
@@ -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 {}
2 changes: 2 additions & 0 deletions rules/map/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use crate::{
UnsafeBidirectionalIterator, UnsafeCppIterator, Value, cpp_cmp,
};

impl<K: 'static, V: 'static> ByteRepr for BTreeMap<K, V> {}

// 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
Expand Down
14 changes: 13 additions & 1 deletion rules/pair/shim.rs
Original file line number Diff line number Diff line change
@@ -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<const N: usize> {
type Type;
Expand Down Expand Up @@ -76,3 +76,15 @@ macro_rules! impl_tuple_get {
}

impl_tuple_get!((T0, T1); 0 T0, 1 T1);

impl<A: ByteRepr, B: ByteRepr> ByteRepr for (A, B) {}

impl<A: 'static, B: 'static> 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)
}
}
16 changes: 16 additions & 0 deletions rules/vector/shim.rs
Original file line number Diff line number Diff line change
@@ -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<T: ByteRepr> ByteRepr for Vec<T> {}

impl<T: 'static> FnPtrArg for Vec<T> {
#[inline]
fn to_repr(self) -> ArgRepr {
ArgRepr::Record(Box::new(self))
}
fn from_repr(r: ArgRepr) -> Self {
record_from_repr(r)
}
}
30 changes: 30 additions & 0 deletions tests/unit/fn_ptr.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++20
#include <assert.h>
#include <compare>
#include <cstddef>
#include <signal.h>
#include <stddef.h>
#include <string>
#include <utility>
#include <vector>

typedef int (*foo_t)(void *);

Expand Down Expand Up @@ -33,6 +38,14 @@ static int total = 0;

void add_to_total(int x) { total += x; }

int pair_sum(std::pair<int, int> p) { return p.first + p.second; }

size_t string_size(std::string s) { return s.size(); }

int vector_sum(std::vector<int> 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);
Expand Down Expand Up @@ -65,5 +78,22 @@ int main() {
handler(2);
handler(3);
assert(total == 5);

int (*pf)(std::pair<int, int>) = pair_sum;
assert(pf({1, 2}) == 3);

size_t (*sf)(std::string) = string_size;
assert(sf(std::string("abc")) == 3);

int (*vf)(std::vector<int>) = 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;
}
36 changes: 36 additions & 0 deletions tests/unit/out/refcount/fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<i32>, Value<i32>)) -> i32 {
let p: Value<(Value<i32>, Value<i32>)> = Rc::new(RefCell::new(p));
return ((*(*p.borrow()).0.borrow()) + (*(*p.borrow()).1.borrow()));
}
pub fn string_size_10(mut s: Vec<i8>) -> usize {
return (s.len() - 1);
}
pub fn vector_sum_11(mut v: Vec<i32>) -> i32 {
return (v[0_usize] + v[1_usize]);
}
pub fn is_less_12(o: std::cmp::Ordering) -> bool {
let o: Value<std::cmp::Ordering> = Rc::new(RefCell::new(o));
return ({ (*o.borrow()) }) == std::cmp::Ordering::Less;
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand Down Expand Up @@ -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<fn((Value<i32>, Value<i32>)) -> i32> =
FnPtr::<fn((Value<i32>, Value<i32>)) -> i32>::new(pair_sum_9);
assert!((({ pf.call((Rc::new(RefCell::new(1)), Rc::new(RefCell::new(2))),) }) == 3));
let mut sf: FnPtr<fn(Vec<i8>) -> usize> = FnPtr::<fn(Vec<i8>) -> usize>::new(string_size_10);
assert!(
(({
sf.call({
let mut __bytes = Ptr::<i8>::from_string_literal(b"abc").to_c_bytes();
__bytes.push(0);
__bytes
})
}) == 3_usize)
);
let mut vf: FnPtr<fn(Vec<i32>) -> i32> = FnPtr::<fn(Vec<i32>) -> i32>::new(vector_sum_11);
assert!((({ vf.call(Vec::from([1, 2,]),) }) == 3));
let mut of: FnPtr<fn(std::cmp::Ordering) -> bool> =
FnPtr::<fn(std::cmp::Ordering) -> 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<std::cmp::Ordering> = Rc::new(RefCell::new(std::cmp::Ord::cmp(&(1), &(2))));
let mut op: Ptr<std::cmp::Ordering> = (o.as_pointer());
assert!(({ (*op.upgrade().deref()) }) == std::cmp::Ordering::Less);
return 0;
}
pub fn __cpp2rust_init_globals() {
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/out/unsafe/fn_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ pub static mut total_7: std::cell::LazyCell<i32> = 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<libc::c_char>) -> usize {
return (s.len() - 1);
}
pub unsafe fn vector_sum_11(mut v: Vec<i32>) -> 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();
Expand Down Expand Up @@ -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<unsafe fn((i32, i32)) -> i32> = Some(pair_sum_9);
assert!(((unsafe { (pf).unwrap()((1, 2),) }) == (3)));
let mut sf: Option<unsafe fn(Vec<libc::c_char>) -> 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<unsafe fn(Vec<i32>) -> i32> = Some(vector_sum_11);
assert!(((unsafe { (vf).unwrap()(Vec::from([1, 2,]),) }) == (3)));
let mut of: Option<unsafe fn(std::cmp::Ordering) -> 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() {
Expand Down
Loading