Skip to content

Commit e6a5b18

Browse files
committed
Allow enums to have byterepr
1 parent 1b1bc4c commit e6a5b18

6 files changed

Lines changed: 161 additions & 29 deletions

File tree

cpp2rust/converter/converter_lib.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,7 @@ bool IsMut(clang::QualType qual_type) {
201201
}
202202

203203
bool TypeImplementsByteRepr(clang::QualType qt) {
204-
if (qt->isEnumeralType()) {
205-
return false;
206-
}
207-
if (qt->isIntegerType() || qt->isFloatingType()) {
204+
if (qt->isIntegerType() || qt->isFloatingType() || qt->isEnumeralType()) {
208205
return true;
209206
}
210207
if (const auto *arr = qt->getAsArrayTypeUnsafe()) {

libcc2rs/src/rc.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ impl<T> fmt::Debug for PtrKind<T> {
8484
PtrKind::HeapSingle(w) => write!(f, "HeapSingle({:?})", w.as_ptr()),
8585
PtrKind::StackArray(w) => write!(f, "StackArray({:?})", w.as_ptr()),
8686
PtrKind::HeapArray(w) => write!(f, "HeapArray({:?})", w.as_ptr()),
87-
PtrKind::Reinterpreted(view) => {
88-
write!(f, "Reinterpreted(0x{:x})", view.alloc.address())
87+
PtrKind::Reinterpreted(data) => {
88+
write!(f, "Reinterpreted(0x{:x})", data.alloc.address())
8989
}
9090
}
9191
}
@@ -100,7 +100,7 @@ impl<T> Clone for PtrKind<T> {
100100
PtrKind::HeapSingle(weak) => PtrKind::HeapSingle(weak.clone()),
101101
PtrKind::StackArray(weak) => PtrKind::StackArray(weak.clone()),
102102
PtrKind::HeapArray(weak) => PtrKind::HeapArray(weak.clone()),
103-
PtrKind::Reinterpreted(view) => PtrKind::Reinterpreted(Rc::clone(view)),
103+
PtrKind::Reinterpreted(data) => PtrKind::Reinterpreted(Rc::clone(data)),
104104
}
105105
}
106106
}
@@ -112,7 +112,7 @@ impl<T> PtrKind<T> {
112112
PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => w.as_ptr() as usize,
113113
PtrKind::Vec(w) => w.as_ptr() as usize,
114114
PtrKind::StackArray(w) | PtrKind::HeapArray(w) => w.as_ptr() as usize,
115-
PtrKind::Reinterpreted(view) => view.alloc.address(),
115+
PtrKind::Reinterpreted(data) => data.alloc.address(),
116116
}
117117
}
118118
}
@@ -258,7 +258,7 @@ impl<T> Ptr<T> {
258258
#[inline]
259259
fn elem_step(&self) -> usize {
260260
match &self.kind {
261-
PtrKind::Reinterpreted(view) => view.elem_byte_size,
261+
PtrKind::Reinterpreted(data) => data.elem_byte_size,
262262
_ => 1,
263263
}
264264
}
@@ -272,7 +272,7 @@ impl<T> Ptr<T> {
272272
PtrKind::StackArray(weak) | PtrKind::HeapArray(weak) => {
273273
weak.upgrade().expect("ub: dangling pointer").borrow().len()
274274
}
275-
PtrKind::Reinterpreted(view) => view.alloc.total_byte_len() / view.elem_byte_size,
275+
PtrKind::Reinterpreted(data) => data.alloc.total_byte_len() / data.elem_byte_size,
276276
}
277277
}
278278

@@ -291,7 +291,7 @@ impl<T> Ptr<T> {
291291
.expect("ub: dangling pointer")
292292
.borrow()
293293
.is_empty(),
294-
PtrKind::Reinterpreted(view) => self.offset >= view.alloc.total_byte_len(),
294+
PtrKind::Reinterpreted(data) => self.offset >= data.alloc.total_byte_len(),
295295
}
296296
}
297297

@@ -346,8 +346,8 @@ impl<T> Ptr<T> {
346346
rc: weak.upgrade().expect("ub: dangling pointer"),
347347
offset: self.offset,
348348
},
349-
PtrKind::Reinterpreted(view) => StrongPtr::Reinterpreted {
350-
alloc: Rc::clone(&view.alloc),
349+
PtrKind::Reinterpreted(data) => StrongPtr::Reinterpreted {
350+
alloc: Rc::clone(&data.alloc),
351351
byte_offset: self.offset,
352352
cell: RefCell::new(None),
353353
},
@@ -372,10 +372,10 @@ impl<T> Ptr<T> {
372372
let rc = weak.upgrade().expect("ub: dangling pointer");
373373
rc.borrow_mut()[self.offset] = value;
374374
}
375-
PtrKind::Reinterpreted(view) => {
375+
PtrKind::Reinterpreted(data) => {
376376
let mut buf = vec![0u8; T::byte_size()];
377377
value.to_bytes(&mut buf);
378-
view.alloc.write_bytes(self.offset, &buf);
378+
data.alloc.write_bytes(self.offset, &buf);
379379
}
380380
}
381381
}
@@ -417,7 +417,7 @@ impl<T> Ptr<T> {
417417
Rc::new(SliceOriginalAlloc { weak: weak.clone() }),
418418
src_byte_off,
419419
),
420-
PtrKind::Reinterpreted(view) => (Rc::clone(&view.alloc), self.offset),
420+
PtrKind::Reinterpreted(data) => (Rc::clone(&data.alloc), self.offset),
421421
};
422422

423423
Ptr {
@@ -452,13 +452,13 @@ impl<T> Ptr<T> {
452452
let mut borrow = rc.borrow_mut();
453453
f(&mut borrow[self.offset])
454454
}
455-
PtrKind::Reinterpreted(view) => {
455+
PtrKind::Reinterpreted(data) => {
456456
let mut buf = vec![0u8; T::byte_size()];
457-
view.alloc.read_bytes(self.offset, &mut buf);
457+
data.alloc.read_bytes(self.offset, &mut buf);
458458
let mut val = T::from_bytes(&buf);
459459
let ret = f(&mut val);
460460
val.to_bytes(&mut buf);
461-
view.alloc.write_bytes(self.offset, &buf);
461+
data.alloc.write_bytes(self.offset, &buf);
462462
ret
463463
}
464464
}
@@ -485,9 +485,9 @@ impl<T> Ptr<T> {
485485
let borrow = rc.borrow();
486486
f(&borrow[self.offset])
487487
}
488-
PtrKind::Reinterpreted(view) => {
488+
PtrKind::Reinterpreted(data) => {
489489
let mut buf = vec![0u8; T::byte_size()];
490-
view.alloc.read_bytes(self.offset, &mut buf);
490+
data.alloc.read_bytes(self.offset, &mut buf);
491491
let val = T::from_bytes(&buf);
492492
f(&val)
493493
}
@@ -512,9 +512,9 @@ impl<T: Clone + ByteRepr> Ptr<T> {
512512
PtrKind::StackArray(ref weak) | PtrKind::HeapArray(ref weak) => {
513513
weak.upgrade().expect("ub: dangling pointer").borrow()[self.offset].clone()
514514
}
515-
PtrKind::Reinterpreted(ref view) => {
515+
PtrKind::Reinterpreted(ref data) => {
516516
let mut buf = vec![0u8; T::byte_size()];
517-
view.alloc.read_bytes(self.offset, &mut buf);
517+
data.alloc.read_bytes(self.offset, &mut buf);
518518
T::from_bytes(&buf)
519519
}
520520
}
@@ -909,7 +909,7 @@ impl<T> fmt::Debug for Ptr<T> {
909909
(Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset())
910910
}
911911
PtrKind::Vec(w) => (Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset()),
912-
PtrKind::Reinterpreted(view) => view.alloc.address().wrapping_add(self.byte_offset()),
912+
PtrKind::Reinterpreted(data) => data.alloc.address().wrapping_add(self.byte_offset()),
913913
};
914914
write!(f, "0x{:x}", addr)
915915
}
@@ -1005,9 +1005,9 @@ impl Ptr<u8> {
10051005
let raw = strong.borrow();
10061006
raw[start..end].to_vec()
10071007
}
1008-
PtrKind::Reinterpreted(ref view) => {
1008+
PtrKind::Reinterpreted(ref data) => {
10091009
let mut buf = vec![0u8; end.wrapping_sub(start)];
1010-
view.alloc.read_bytes(start, &mut buf);
1010+
data.alloc.read_bytes(start, &mut buf);
10111011
buf
10121012
}
10131013
}

tests/unit/out/refcount/anonymous_enum.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ impl From<i32> for anon_0 {
2222
}
2323
}
2424
libcc2rs::impl_enum_inc_dec!(anon_0);
25+
impl ByteRepr for anon_0 {
26+
fn to_bytes(&self, buf: &mut [u8]) {
27+
(*self as i32).to_bytes(buf);
28+
}
29+
fn from_bytes(buf: &[u8]) -> Self {
30+
<anon_0>::from(i32::from_bytes(buf))
31+
}
32+
}
2533
#[derive(Clone, Copy, PartialEq, Debug, Default)]
2634
enum anon_1 {
2735
#[default]
@@ -38,6 +46,14 @@ impl From<i32> for anon_1 {
3846
}
3947
}
4048
libcc2rs::impl_enum_inc_dec!(anon_1);
49+
impl ByteRepr for anon_1 {
50+
fn to_bytes(&self, buf: &mut [u8]) {
51+
(*self as i32).to_bytes(buf);
52+
}
53+
fn from_bytes(buf: &[u8]) -> Self {
54+
<anon_1>::from(i32::from_bytes(buf))
55+
}
56+
}
4157
#[derive(Default)]
4258
pub struct S {
4359
pub a: Value<i32>,
@@ -79,6 +95,14 @@ impl From<i32> for TdEnum {
7995
}
8096
}
8197
libcc2rs::impl_enum_inc_dec!(TdEnum);
98+
impl ByteRepr for TdEnum {
99+
fn to_bytes(&self, buf: &mut [u8]) {
100+
(*self as i32).to_bytes(buf);
101+
}
102+
fn from_bytes(buf: &[u8]) -> Self {
103+
<TdEnum>::from(i32::from_bytes(buf))
104+
}
105+
}
82106
#[derive(Clone, Copy, PartialEq, Debug, Default)]
83107
enum anon_2 {
84108
#[default]
@@ -95,6 +119,14 @@ impl From<i32> for anon_2 {
95119
}
96120
}
97121
libcc2rs::impl_enum_inc_dec!(anon_2);
122+
impl ByteRepr for anon_2 {
123+
fn to_bytes(&self, buf: &mut [u8]) {
124+
(*self as i32).to_bytes(buf);
125+
}
126+
fn from_bytes(buf: &[u8]) -> Self {
127+
<anon_2>::from(i32::from_bytes(buf))
128+
}
129+
}
98130
#[derive(Default)]
99131
pub struct WithAnonField {
100132
pub a: Value<i32>,
@@ -109,7 +141,21 @@ impl Clone for WithAnonField {
109141
this
110142
}
111143
}
112-
impl ByteRepr for WithAnonField {}
144+
impl ByteRepr for WithAnonField {
145+
fn byte_size() -> usize {
146+
8
147+
}
148+
fn to_bytes(&self, buf: &mut [u8]) {
149+
(*self.a.borrow()).to_bytes(&mut buf[0..4]);
150+
(*self.field.borrow()).to_bytes(&mut buf[4..8]);
151+
}
152+
fn from_bytes(buf: &[u8]) -> Self {
153+
Self {
154+
a: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
155+
field: Rc::new(RefCell::new(<anon_2>::from_bytes(&buf[4..8]))),
156+
}
157+
}
158+
}
113159
pub fn main() {
114160
std::process::exit(main_0());
115161
}
@@ -130,6 +176,14 @@ fn main_0() -> i32 {
130176
}
131177
}
132178
libcc2rs::impl_enum_inc_dec!(anon_3);
179+
impl ByteRepr for anon_3 {
180+
fn to_bytes(&self, buf: &mut [u8]) {
181+
(*self as i32).to_bytes(buf);
182+
}
183+
fn from_bytes(buf: &[u8]) -> Self {
184+
<anon_3>::from(i32::from_bytes(buf))
185+
}
186+
};
133187
assert!(((anon_0::FIRST_A as i32) != (anon_0::FIRST_B as i32)));
134188
assert!(((anon_1::SECOND_A as i32) != (anon_1::SECOND_B as i32)));
135189
assert!(((anon_3::THIRD_A as i32) != (anon_3::THIRD_B as i32)));

tests/unit/out/refcount/anonymous_enum_c.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ impl From<i32> for anon_0 {
2222
}
2323
}
2424
libcc2rs::impl_enum_inc_dec!(anon_0);
25+
impl ByteRepr for anon_0 {
26+
fn to_bytes(&self, buf: &mut [u8]) {
27+
(*self as i32).to_bytes(buf);
28+
}
29+
fn from_bytes(buf: &[u8]) -> Self {
30+
<anon_0>::from(i32::from_bytes(buf))
31+
}
32+
}
2533
#[derive(Clone, Copy, PartialEq, Debug, Default)]
2634
enum anon_1 {
2735
#[default]
@@ -38,6 +46,14 @@ impl From<i32> for anon_1 {
3846
}
3947
}
4048
libcc2rs::impl_enum_inc_dec!(anon_1);
49+
impl ByteRepr for anon_1 {
50+
fn to_bytes(&self, buf: &mut [u8]) {
51+
(*self as i32).to_bytes(buf);
52+
}
53+
fn from_bytes(buf: &[u8]) -> Self {
54+
<anon_1>::from(i32::from_bytes(buf))
55+
}
56+
}
4157
#[derive(Default)]
4258
pub struct S {
4359
pub a: Value<i32>,
@@ -71,6 +87,14 @@ impl From<i32> for TdEnum_enum {
7187
}
7288
}
7389
libcc2rs::impl_enum_inc_dec!(TdEnum_enum);
90+
impl ByteRepr for TdEnum_enum {
91+
fn to_bytes(&self, buf: &mut [u8]) {
92+
(*self as i32).to_bytes(buf);
93+
}
94+
fn from_bytes(buf: &[u8]) -> Self {
95+
<TdEnum_enum>::from(i32::from_bytes(buf))
96+
}
97+
}
7498
#[derive(Clone, Copy, PartialEq, Debug, Default)]
7599
enum anon_2 {
76100
#[default]
@@ -87,12 +111,34 @@ impl From<i32> for anon_2 {
87111
}
88112
}
89113
libcc2rs::impl_enum_inc_dec!(anon_2);
114+
impl ByteRepr for anon_2 {
115+
fn to_bytes(&self, buf: &mut [u8]) {
116+
(*self as i32).to_bytes(buf);
117+
}
118+
fn from_bytes(buf: &[u8]) -> Self {
119+
<anon_2>::from(i32::from_bytes(buf))
120+
}
121+
}
90122
#[derive(Default)]
91123
pub struct WithAnonField {
92124
pub a: Value<i32>,
93125
pub field: Value<anon_2>,
94126
}
95-
impl ByteRepr for WithAnonField {}
127+
impl ByteRepr for WithAnonField {
128+
fn byte_size() -> usize {
129+
8
130+
}
131+
fn to_bytes(&self, buf: &mut [u8]) {
132+
(*self.a.borrow()).to_bytes(&mut buf[0..4]);
133+
(*self.field.borrow()).to_bytes(&mut buf[4..8]);
134+
}
135+
fn from_bytes(buf: &[u8]) -> Self {
136+
Self {
137+
a: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
138+
field: Rc::new(RefCell::new(<anon_2>::from_bytes(&buf[4..8]))),
139+
}
140+
}
141+
}
96142
pub fn main() {
97143
std::process::exit(main_0());
98144
}
@@ -113,6 +159,14 @@ fn main_0() -> i32 {
113159
}
114160
}
115161
libcc2rs::impl_enum_inc_dec!(anon_3);
162+
impl ByteRepr for anon_3 {
163+
fn to_bytes(&self, buf: &mut [u8]) {
164+
(*self as i32).to_bytes(buf);
165+
}
166+
fn from_bytes(buf: &[u8]) -> Self {
167+
<anon_3>::from(i32::from_bytes(buf))
168+
}
169+
};
116170
assert!(((((anon_0::FIRST_A as i32) != (anon_0::FIRST_B as i32)) as i32) != 0));
117171
assert!(((((anon_1::SECOND_A as i32) != (anon_1::SECOND_B as i32)) as i32) != 0));
118172
assert!(((((anon_3::THIRD_A as i32) != (anon_3::THIRD_B as i32)) as i32) != 0));

tests/unit/out/refcount/c_struct.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ impl From<i32> for Color {
7070
}
7171
}
7272
libcc2rs::impl_enum_inc_dec!(Color);
73+
impl ByteRepr for Color {
74+
fn to_bytes(&self, buf: &mut [u8]) {
75+
(*self as i32).to_bytes(buf);
76+
}
77+
fn from_bytes(buf: &[u8]) -> Self {
78+
<Color>::from(i32::from_bytes(buf))
79+
}
80+
}
7381
#[derive(Default)]
7482
pub struct Inner {
7583
pub a: Value<i32>,
@@ -96,7 +104,23 @@ pub struct Container {
96104
pub color: Value<Color>,
97105
pub count: Value<i32>,
98106
}
99-
impl ByteRepr for Container {}
107+
impl ByteRepr for Container {
108+
fn byte_size() -> usize {
109+
16
110+
}
111+
fn to_bytes(&self, buf: &mut [u8]) {
112+
(*self.inner.borrow()).to_bytes(&mut buf[0..8]);
113+
(*self.color.borrow()).to_bytes(&mut buf[8..12]);
114+
(*self.count.borrow()).to_bytes(&mut buf[12..16]);
115+
}
116+
fn from_bytes(buf: &[u8]) -> Self {
117+
Self {
118+
inner: Rc::new(RefCell::new(<Inner>::from_bytes(&buf[0..8]))),
119+
color: Rc::new(RefCell::new(<Color>::from_bytes(&buf[8..12]))),
120+
count: Rc::new(RefCell::new(<i32>::from_bytes(&buf[12..16]))),
121+
}
122+
}
123+
}
100124
pub fn main() {
101125
std::process::exit(main_0());
102126
}

0 commit comments

Comments
 (0)