diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 99d7b036..ed91bb4b 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -363,6 +363,10 @@ bool Converter::VisitTranslationUnitDecl(clang::TranslationUnitDecl *decl) { if (IsUserDefinedDecl(child) && (IsInMainFile(child) || !decl_ids_.contains(GetID(child)))) { Convert(child); + if (!hoisted_records_.empty()) { + StrCat(hoisted_records_); + hoisted_records_.clear(); + } } } return false; @@ -1257,6 +1261,12 @@ bool Converter::VisitCompoundStmt(clang::CompoundStmt *stmt) { bool Converter::VisitDeclStmt(clang::DeclStmt *stmt) { for (auto *decl : stmt->decls()) { + if (clang::isa(decl)) { + Buffer buf(*this); + Convert(decl); + hoisted_records_ += std::move(buf).str(); + continue; + } Convert(decl); StrCat(token::kSemiColon); } diff --git a/cpp2rust/converter/converter.h b/cpp2rust/converter/converter.h index 6a4996cf..61e28044 100644 --- a/cpp2rust/converter/converter.h +++ b/cpp2rust/converter/converter.h @@ -880,6 +880,8 @@ class Converter : public clang::RecursiveASTVisitor { // translation units. static std::map methods_on_ptr_; + std::string hoisted_records_; + enum class ExprKind : uint8_t { Callee, LValue, diff --git a/cpp2rust/converter/converter_lib.cpp b/cpp2rust/converter/converter_lib.cpp index 7a031734..dc9f75c8 100644 --- a/cpp2rust/converter/converter_lib.cpp +++ b/cpp2rust/converter/converter_lib.cpp @@ -535,13 +535,24 @@ static std::string GetParamSignature(const clang::Decl *decl) { } static std::string GetLexicalSpecializationID(const clang::Decl *decl) { + std::string id; if (const auto *spec = clang::dyn_cast( decl->getLexicalDeclContext()); spec && decl->getLexicalDeclContext() != decl->getDeclContext()) { - return Mapper::ToString(Mapper::GetTypeForDecl(spec)); + id += Mapper::ToString(Mapper::GetTypeForDecl(spec)); } - return {}; + for (const auto *dc = decl->getDeclContext(); dc; dc = dc->getParent()) { + if (const auto *spec = + clang::dyn_cast(dc)) { + id += Mapper::ToString(Mapper::GetTypeForDecl(spec)); + } + if (const auto *fn = clang::dyn_cast(dc); + fn && fn->getTemplateSpecializationArgs()) { + id += clang::ASTNameGenerator(fn->getASTContext()).getName(fn); + } + } + return id; } std::string GetID(const clang::Decl *decl) { @@ -623,6 +634,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) { var && (var->isFileVarDecl() || var->isStaticLocal())) { id = GetDeclId(var->getCanonicalDecl(), var->getFormalLinkage() != clang::Linkage::External); + } else if (auto *tag = clang::dyn_cast(decl); + tag && tag->getDeclContext()->isFunctionOrMethod()) { + id = + type_mapping.try_emplace(GetID(tag), type_mapping.size()).first->second; } if (id) { name += '_'; diff --git a/cpp2rust/converter/mapper.cpp b/cpp2rust/converter/mapper.cpp index 4def2784..a5699070 100644 --- a/cpp2rust/converter/mapper.cpp +++ b/cpp2rust/converter/mapper.cpp @@ -882,6 +882,12 @@ std::string ToString(clang::QualType qual_type, ScalarSugar sugar) { return ToString(clang::cast(tag)); } + if (auto *tag = qual_type->getAsTagDecl(); + tag && tag->getIdentifier() && + tag->getDeclContext()->isFunctionOrMethod()) { + return GetNamedDeclAsString(tag); + } + if (auto renamed = DisambiguateAnonymousTag(qual_type->getAsTagDecl()); !renamed.empty()) { return renamed; diff --git a/tests/unit/local_record_template.cpp b/tests/unit/local_record_template.cpp new file mode 100644 index 00000000..53a79e62 --- /dev/null +++ b/tests/unit/local_record_template.cpp @@ -0,0 +1,37 @@ +#include + +template int get(T t) { return t.x; } + +namespace ns { +template int twice(T t) { return t.x * 2; } +} // namespace ns + +template int wrap(T v) { + struct Local { + T x; + }; + Local l{v}; + return get(l); +} + +int other() { + struct Local { + long x; + long y; + }; + Local l{3, 4}; + return get(l) + (int)l.y; +} + +int main() { + struct Local { + int x; + }; + Local l{7}; + assert(get(l) == 7); + assert(ns::twice(l) == 14); + assert(other() == 7); + assert(wrap(5) == 5); + assert(wrap(6L) == 6); + return 0; +} diff --git a/tests/unit/out/refcount/anonymous-struct.rs b/tests/unit/out/refcount/anonymous-struct.rs index 64d82f14..3ef5f74c 100644 --- a/tests/unit/out/refcount/anonymous-struct.rs +++ b/tests/unit/out/refcount/anonymous-struct.rs @@ -321,36 +321,6 @@ fn main_0() -> i32 { .borrow()) == 11) ); - #[derive(Default)] - pub struct anon_6 { - pub x: Value, - pub z: Value, - } - impl Clone for anon_6 { - fn clone(&self) -> Self { - let __this: Value = Rc::new(RefCell::new(Self { - x: Rc::new(RefCell::new((*self.x.borrow()))), - z: Rc::new(RefCell::new((*self.z.borrow()))), - })); - let this: Ptr = __this.as_pointer(); - Rc::try_unwrap(__this).ok().unwrap().into_inner() - } - } - impl ByteRepr for anon_6 { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.z.borrow()).to_bytes(&mut buf[4..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - } - } - }; let s: Value = Rc::new(RefCell::new(::default())); (*(*s.borrow()).x.borrow_mut()) = 1; (*(*s.borrow()).z.borrow_mut()) = 2; @@ -368,3 +338,33 @@ fn main_0() -> i32 { ); return 0; } +#[derive(Default)] +pub struct anon_6 { + pub x: Value, + pub z: Value, +} +impl Clone for anon_6 { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: Rc::new(RefCell::new((*self.x.borrow()))), + z: Rc::new(RefCell::new((*self.z.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for anon_6 { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.z.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} diff --git a/tests/unit/out/refcount/anonymous-struct_c.rs b/tests/unit/out/refcount/anonymous-struct_c.rs index 065fd2cc..5473ea07 100644 --- a/tests/unit/out/refcount/anonymous-struct_c.rs +++ b/tests/unit/out/refcount/anonymous-struct_c.rs @@ -290,34 +290,6 @@ fn main_0() -> i32 { == 11) as i32) != 0) ); - #[derive(Default)] - pub struct anon_6 { - pub x: Value, - pub z: Value, - } - impl Clone for anon_6 { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - z: Rc::new(RefCell::new((*self.z.borrow()).clone())), - } - } - } - impl ByteRepr for anon_6 { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.z.borrow()).to_bytes(&mut buf[4..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - } - } - }; let s: Value = >::default(); (*(*s.borrow()).x.borrow_mut()) = 1; (*(*s.borrow()).z.borrow_mut()) = 2; @@ -335,3 +307,31 @@ fn main_0() -> i32 { ); return 0; } +#[derive(Default)] +pub struct anon_6 { + pub x: Value, + pub z: Value, +} +impl Clone for anon_6 { + fn clone(&self) -> Self { + Self { + x: Rc::new(RefCell::new((*self.x.borrow()).clone())), + z: Rc::new(RefCell::new((*self.z.borrow()).clone())), + } + } +} +impl ByteRepr for anon_6 { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.z.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + z: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), + } + } +} diff --git a/tests/unit/out/refcount/anonymous_enum.rs b/tests/unit/out/refcount/anonymous_enum.rs index 2e954d73..f630eea9 100644 --- a/tests/unit/out/refcount/anonymous_enum.rs +++ b/tests/unit/out/refcount/anonymous_enum.rs @@ -78,9 +78,6 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - pub type anon_3 = u32; - pub const anon_3_THIRD_A: anon_3 = 0; - pub const anon_3_THIRD_B: anon_3 = 1;; assert!(((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32))); assert!(((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32))); assert!(((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32))); @@ -95,3 +92,6 @@ fn main_0() -> i32 { assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2_FIELD_B as i32))); return 0; } +pub type anon_3 = u32; +pub const anon_3_THIRD_A: anon_3 = 0; +pub const anon_3_THIRD_B: anon_3 = 1; diff --git a/tests/unit/out/refcount/anonymous_enum_c.rs b/tests/unit/out/refcount/anonymous_enum_c.rs index 82240be2..dd356a26 100644 --- a/tests/unit/out/refcount/anonymous_enum_c.rs +++ b/tests/unit/out/refcount/anonymous_enum_c.rs @@ -74,9 +74,6 @@ pub fn main() { std::process::exit(main_0()); } fn main_0() -> i32 { - pub type anon_3 = u32; - pub const anon_3_THIRD_A: anon_3 = 0; - pub const anon_3_THIRD_B: anon_3 = 1;; assert!(((((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32)) as i32) != 0)); assert!(((((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32)) as i32) != 0)); assert!(((((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32)) as i32) != 0)); @@ -97,3 +94,6 @@ fn main_0() -> i32 { ); return 0; } +pub type anon_3 = u32; +pub const anon_3_THIRD_A: anon_3 = 0; +pub const anon_3_THIRD_B: anon_3 = 1; diff --git a/tests/unit/out/refcount/local_anon_struct_collision.rs b/tests/unit/out/refcount/local_anon_struct_collision.rs index d717fab0..f9deb5f2 100644 --- a/tests/unit/out/refcount/local_anon_struct_collision.rs +++ b/tests/unit/out/refcount/local_anon_struct_collision.rs @@ -7,73 +7,73 @@ use std::io::{Read, Seek, Write}; use std::os::fd::AsFd; use std::rc::{Rc, Weak}; pub fn first_0() -> i32 { - #[derive(Default)] - pub struct anon_1 { - pub x: Value, - pub y: Value, - } - impl Clone for anon_1 { - fn clone(&self) -> Self { - Self { - x: Rc::new(RefCell::new((*self.x.borrow()).clone())), - y: Rc::new(RefCell::new((*self.y.borrow()).clone())), - } - } - } - impl ByteRepr for anon_1 { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.x.borrow()).to_bytes(&mut buf[0..4]); - (*self.y.borrow()).to_bytes(&mut buf[4..8]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), - y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), - } - } - }; let p: Value = >::default(); (*(*p.borrow()).x.borrow_mut()) = 1; (*(*p.borrow()).y.borrow_mut()) = 2; return ((*(*p.borrow()).x.borrow()) + (*(*p.borrow()).y.borrow())); } -pub fn second_2() -> i32 { - #[derive(Default)] - pub struct anon_3 { - pub a: Value, - pub b: Value, - } - impl Clone for anon_3 { - fn clone(&self) -> Self { - Self { - a: Rc::new(RefCell::new((*self.a.borrow()).clone())), - b: Rc::new(RefCell::new((*self.b.borrow()).clone())), - } +#[derive(Default)] +pub struct anon_1 { + pub x: Value, + pub y: Value, +} +impl Clone for anon_1 { + fn clone(&self) -> Self { + Self { + x: Rc::new(RefCell::new((*self.x.borrow()).clone())), + y: Rc::new(RefCell::new((*self.y.borrow()).clone())), } } - impl ByteRepr for anon_3 { - fn byte_size() -> usize { - 16 - } - fn to_bytes(&self, buf: &mut [u8]) { - (*self.a.borrow()).to_bytes(&mut buf[0..8]); - (*self.b.borrow()).to_bytes(&mut buf[8..16]); - } - fn from_bytes(buf: &[u8]) -> Self { - Self { - a: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), - b: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), - } +} +impl ByteRepr for anon_1 { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + (*self.y.borrow()).to_bytes(&mut buf[4..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[4..8]))), } - }; + } +} +pub fn second_2() -> i32 { let q: Value = >::default(); (*(*q.borrow()).a.borrow_mut()) = 10_i64; (*(*q.borrow()).b.borrow_mut()) = 20_i64; return (((*(*q.borrow()).a.borrow()) + (*(*q.borrow()).b.borrow())) as i32); } +#[derive(Default)] +pub struct anon_3 { + pub a: Value, + pub b: Value, +} +impl Clone for anon_3 { + fn clone(&self) -> Self { + Self { + a: Rc::new(RefCell::new((*self.a.borrow()).clone())), + b: Rc::new(RefCell::new((*self.b.borrow()).clone())), + } + } +} +impl ByteRepr for anon_3 { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.a.borrow()).to_bytes(&mut buf[0..8]); + (*self.b.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + a: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + b: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + } + } +} pub fn main() { std::process::exit(main_0()); } diff --git a/tests/unit/out/refcount/local_record_template.rs b/tests/unit/out/refcount/local_record_template.rs new file mode 100644 index 00000000..b7170eaf --- /dev/null +++ b/tests/unit/out/refcount/local_record_template.rs @@ -0,0 +1,171 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn get_0(t: Local_1) -> i32 { + let t: Value = Rc::new(RefCell::new(t)); + return ((*(*t.borrow()).x.borrow()) as i32); +} +pub fn get_2(t: Local_3) -> i32 { + let t: Value = Rc::new(RefCell::new(t)); + return (*(*t.borrow()).x.borrow()); +} +pub fn get_4(t: Local_5) -> i32 { + let t: Value = Rc::new(RefCell::new(t)); + return (*(*t.borrow()).x.borrow()); +} +pub fn get_6(t: Local_7) -> i32 { + let t: Value = Rc::new(RefCell::new(t)); + return ((*(*t.borrow()).x.borrow()) as i32); +} +pub fn twice_8(t: Local_3) -> i32 { + let t: Value = Rc::new(RefCell::new(t)); + return ((*(*t.borrow()).x.borrow()) * 2); +} +pub fn wrap_9(v: i32) -> i32 { + let v: Value = Rc::new(RefCell::new(v)); + let l: Value = Rc::new(RefCell::new(Local_5 { + x: Rc::new(RefCell::new((*v.borrow()))), + })); + return ({ get_4((*l.borrow()).clone()) }); +} +pub fn wrap_10(v: i64) -> i32 { + let v: Value = Rc::new(RefCell::new(v)); + let l: Value = Rc::new(RefCell::new(Local_7 { + x: Rc::new(RefCell::new((*v.borrow()))), + })); + return ({ get_6((*l.borrow()).clone()) }); +} +#[derive(Default)] +pub struct Local_5 { + pub x: Value, +} +impl Clone for Local_5 { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: Rc::new(RefCell::new((*self.x.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Local_5 { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} +#[derive(Default)] +pub struct Local_7 { + pub x: Value, +} +impl Clone for Local_7 { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: Rc::new(RefCell::new((*self.x.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Local_7 { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + } + } +} +pub fn other_11() -> i32 { + let l: Value = Rc::new(RefCell::new(Local_1 { + x: Rc::new(RefCell::new(3_i64)), + y: Rc::new(RefCell::new(4_i64)), + })); + return (({ get_0((*l.borrow()).clone()) }) + ((*(*l.borrow()).y.borrow()) as i32)); +} +#[derive(Default)] +pub struct Local_1 { + pub x: Value, + pub y: Value, +} +impl Clone for Local_1 { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: Rc::new(RefCell::new((*self.x.borrow()))), + y: Rc::new(RefCell::new((*self.y.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Local_1 { + fn byte_size() -> usize { + 16 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..8]); + (*self.y.borrow()).to_bytes(&mut buf[8..16]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..8]))), + y: Rc::new(RefCell::new(::from_bytes(&buf[8..16]))), + } + } +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let l: Value = Rc::new(RefCell::new(Local_3 { + x: Rc::new(RefCell::new(7)), + })); + assert!((({ get_2((*l.borrow()).clone(),) }) == 7)); + assert!((({ twice_8((*l.borrow()).clone(),) }) == 14)); + assert!((({ other_11() }) == 7)); + assert!((({ wrap_9(5,) }) == 5)); + assert!((({ wrap_10(6_i64,) }) == 6)); + return 0; +} +#[derive(Default)] +pub struct Local_3 { + pub x: Value, +} +impl Clone for Local_3 { + fn clone(&self) -> Self { + let __this: Value = Rc::new(RefCell::new(Self { + x: Rc::new(RefCell::new((*self.x.borrow()))), + })); + let this: Ptr = __this.as_pointer(); + Rc::try_unwrap(__this).ok().unwrap().into_inner() + } +} +impl ByteRepr for Local_3 { + fn byte_size() -> usize { + 4 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.x.borrow()).to_bytes(&mut buf[0..4]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + x: Rc::new(RefCell::new(::from_bytes(&buf[0..4]))), + } + } +} diff --git a/tests/unit/out/refcount/union_pointer_pun_address.rs b/tests/unit/out/refcount/union_pointer_pun_address.rs index 4a0cb1bb..464f78bd 100644 --- a/tests/unit/out/refcount/union_pointer_pun_address.rs +++ b/tests/unit/out/refcount/union_pointer_pun_address.rs @@ -65,44 +65,6 @@ fn main_0() -> i32 { let a: Value = Rc::new(RefCell::new(node_a { n: Rc::new(RefCell::new(123)), })); - pub struct anon_0 { - __bytes: Value>, - } - impl anon_0 { - pub fn to_a(&self) -> Ptr> { - (self.__bytes.as_pointer() as Ptr).reinterpret_cast() - } - pub fn to_b(&self) -> Ptr> { - (self.__bytes.as_pointer() as Ptr).reinterpret_cast() - } - } - impl Clone for anon_0 { - fn clone(&self) -> Self { - anon_0 { - __bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())), - } - } - } - impl Default for anon_0 { - fn default() -> Self { - anon_0 { - __bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))), - } - } - } - impl ByteRepr for anon_0 { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - buf.copy_from_slice(&self.__bytes.borrow()); - } - fn from_bytes(buf: &[u8]) -> Self { - anon_0 { - __bytes: Rc::new(RefCell::new(Box::from(buf))), - } - } - }; let ptr: Value = >::default(); (*ptr.borrow_mut()).to_a().write((a.as_pointer())); let out: Value> = Rc::new(RefCell::new(((*ptr.borrow()).to_b().read()).clone())); @@ -115,3 +77,41 @@ fn main_0() -> i32 { ); return 0; } +pub struct anon_0 { + __bytes: Value>, +} +impl anon_0 { + pub fn to_a(&self) -> Ptr> { + (self.__bytes.as_pointer() as Ptr).reinterpret_cast() + } + pub fn to_b(&self) -> Ptr> { + (self.__bytes.as_pointer() as Ptr).reinterpret_cast() + } +} +impl Clone for anon_0 { + fn clone(&self) -> Self { + anon_0 { + __bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())), + } + } +} +impl Default for anon_0 { + fn default() -> Self { + anon_0 { + __bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))), + } + } +} +impl ByteRepr for anon_0 { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + buf.copy_from_slice(&self.__bytes.borrow()); + } + fn from_bytes(buf: &[u8]) -> Self { + anon_0 { + __bytes: Rc::new(RefCell::new(Box::from(buf))), + } + } +} diff --git a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs index dd2f9369..3d470727 100644 --- a/tests/unit/out/refcount/union_pointer_pun_writethrough.rs +++ b/tests/unit/out/refcount/union_pointer_pun_writethrough.rs @@ -11,47 +11,47 @@ pub fn main() { } fn main_0() -> i32 { let x: Value = Rc::new(RefCell::new((-1_i32 as i64))); - pub struct anon_0 { - __bytes: Value>, + let pp: Value = >::default(); + (*pp.borrow_mut()).as_signed().write((x.as_pointer())); + ((*pp.borrow()).as_unsigned().read()).write(42_u64); + assert!(((((*x.borrow()) == 42_i64) as i32) != 0)); + return 0; +} +pub struct anon_0 { + __bytes: Value>, +} +impl anon_0 { + pub fn as_unsigned(&self) -> Ptr> { + (self.__bytes.as_pointer() as Ptr).reinterpret_cast() } - impl anon_0 { - pub fn as_unsigned(&self) -> Ptr> { - (self.__bytes.as_pointer() as Ptr).reinterpret_cast() - } - pub fn as_signed(&self) -> Ptr> { - (self.__bytes.as_pointer() as Ptr).reinterpret_cast() - } + pub fn as_signed(&self) -> Ptr> { + (self.__bytes.as_pointer() as Ptr).reinterpret_cast() } - impl Clone for anon_0 { - fn clone(&self) -> Self { - anon_0 { - __bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())), - } +} +impl Clone for anon_0 { + fn clone(&self) -> Self { + anon_0 { + __bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())), } } - impl Default for anon_0 { - fn default() -> Self { - anon_0 { - __bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))), - } +} +impl Default for anon_0 { + fn default() -> Self { + anon_0 { + __bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))), } } - impl ByteRepr for anon_0 { - fn byte_size() -> usize { - 8 - } - fn to_bytes(&self, buf: &mut [u8]) { - buf.copy_from_slice(&self.__bytes.borrow()); - } - fn from_bytes(buf: &[u8]) -> Self { - anon_0 { - __bytes: Rc::new(RefCell::new(Box::from(buf))), - } +} +impl ByteRepr for anon_0 { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + buf.copy_from_slice(&self.__bytes.borrow()); + } + fn from_bytes(buf: &[u8]) -> Self { + anon_0 { + __bytes: Rc::new(RefCell::new(Box::from(buf))), } - }; - let pp: Value = >::default(); - (*pp.borrow_mut()).as_signed().write((x.as_pointer())); - ((*pp.borrow()).as_unsigned().read()).write(42_u64); - assert!(((((*x.borrow()) == 42_i64) as i32) != 0)); - return 0; + } } diff --git a/tests/unit/out/unsafe/anonymous-struct.rs b/tests/unit/out/unsafe/anonymous-struct.rs index 59c69a7f..705075f8 100644 --- a/tests/unit/out/unsafe/anonymous-struct.rs +++ b/tests/unit/out/unsafe/anonymous-struct.rs @@ -95,12 +95,6 @@ unsafe fn main_0() -> i32 { assert!(((o.anon_3.i) == (9))); assert!(((o.anon_3.inner_named.j) == (10))); assert!(((o.anon_3.anon_5.k) == (11))); - #[repr(C)] - #[derive(Copy, Clone, Default)] - pub struct anon_6 { - pub x: i32, - pub z: i32, - }; let mut s: anon_6 = ::default(); s.x = 1; s.z = 2; @@ -118,3 +112,9 @@ unsafe fn main_0() -> i32 { ); return 0; } +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct anon_6 { + pub x: i32, + pub z: i32, +} diff --git a/tests/unit/out/unsafe/anonymous-struct_c.rs b/tests/unit/out/unsafe/anonymous-struct_c.rs index 1c9915d9..5de7e6b6 100644 --- a/tests/unit/out/unsafe/anonymous-struct_c.rs +++ b/tests/unit/out/unsafe/anonymous-struct_c.rs @@ -91,12 +91,6 @@ unsafe fn main_0() -> i32 { assert!(((((o.anon_3.i) == (9)) as i32) != 0)); assert!(((((o.anon_3.inner_named.j) == (10)) as i32) != 0)); assert!(((((o.anon_3.anon_5.k) == (11)) as i32) != 0)); - #[repr(C)] - #[derive(Copy, Clone, Default)] - pub struct anon_6 { - pub x: i32, - pub z: i32, - }; let mut s: anon_6 = ::default(); s.x = 1; s.z = 2; @@ -114,3 +108,9 @@ unsafe fn main_0() -> i32 { ); return 0; } +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct anon_6 { + pub x: i32, + pub z: i32, +} diff --git a/tests/unit/out/unsafe/anonymous_enum.rs b/tests/unit/out/unsafe/anonymous_enum.rs index 02028c3c..e5e75562 100644 --- a/tests/unit/out/unsafe/anonymous_enum.rs +++ b/tests/unit/out/unsafe/anonymous_enum.rs @@ -35,9 +35,6 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - pub type anon_3 = u32; - pub const anon_3_THIRD_A: anon_3 = 0; - pub const anon_3_THIRD_B: anon_3 = 1;; assert!(((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32))); assert!(((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32))); assert!(((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32))); @@ -52,3 +49,6 @@ unsafe fn main_0() -> i32 { assert!(((w.field as i32) == (anon_2_FIELD_B as i32))); return 0; } +pub type anon_3 = u32; +pub const anon_3_THIRD_A: anon_3 = 0; +pub const anon_3_THIRD_B: anon_3 = 1; diff --git a/tests/unit/out/unsafe/anonymous_enum_c.rs b/tests/unit/out/unsafe/anonymous_enum_c.rs index 9b405228..3913926e 100644 --- a/tests/unit/out/unsafe/anonymous_enum_c.rs +++ b/tests/unit/out/unsafe/anonymous_enum_c.rs @@ -35,9 +35,6 @@ pub fn main() { } } unsafe fn main_0() -> i32 { - pub type anon_3 = u32; - pub const anon_3_THIRD_A: anon_3 = 0; - pub const anon_3_THIRD_B: anon_3 = 1;; assert!(((((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32)) as i32) != 0)); assert!(((((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32)) as i32) != 0)); assert!(((((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32)) as i32) != 0)); @@ -52,3 +49,6 @@ unsafe fn main_0() -> i32 { assert!(((((w.field as u32) == ((anon_2_FIELD_B as i32) as u32)) as i32) != 0)); return 0; } +pub type anon_3 = u32; +pub const anon_3_THIRD_A: anon_3 = 0; +pub const anon_3_THIRD_B: anon_3 = 1; diff --git a/tests/unit/out/unsafe/local_anon_struct_collision.rs b/tests/unit/out/unsafe/local_anon_struct_collision.rs index 4e0a72bd..2ab7395e 100644 --- a/tests/unit/out/unsafe/local_anon_struct_collision.rs +++ b/tests/unit/out/unsafe/local_anon_struct_collision.rs @@ -7,29 +7,29 @@ use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; pub unsafe fn first_0() -> i32 { - #[repr(C)] - #[derive(Copy, Clone, Default)] - pub struct anon_1 { - pub x: i32, - pub y: i32, - }; let mut p: anon_1 = ::default(); p.x = 1; p.y = 2; return ((p.x) + (p.y)); } +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct anon_1 { + pub x: i32, + pub y: i32, +} pub unsafe fn second_2() -> i32 { - #[repr(C)] - #[derive(Copy, Clone, Default)] - pub struct anon_3 { - pub a: i64, - pub b: i64, - }; let mut q: anon_3 = ::default(); q.a = 10_i64; q.b = 20_i64; return (((q.a) + (q.b)) as i32); } +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct anon_3 { + pub a: i64, + pub b: i64, +} pub fn main() { unsafe { std::process::exit(main_0() as i32); diff --git a/tests/unit/out/unsafe/local_record_template.rs b/tests/unit/out/unsafe/local_record_template.rs new file mode 100644 index 00000000..dbc6b2bf --- /dev/null +++ b/tests/unit/out/unsafe/local_record_template.rs @@ -0,0 +1,70 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub unsafe fn get_0(mut t: Local_1) -> i32 { + return (t.x as i32); +} +pub unsafe fn get_2(mut t: Local_3) -> i32 { + return t.x; +} +pub unsafe fn get_4(mut t: Local_5) -> i32 { + return t.x; +} +pub unsafe fn get_6(mut t: Local_7) -> i32 { + return (t.x as i32); +} +pub unsafe fn twice_8(mut t: Local_3) -> i32 { + return ((t.x) * (2)); +} +pub unsafe fn wrap_9(mut v: i32) -> i32 { + let mut l: Local_5 = Local_5 { x: v }; + return (unsafe { get_4(l) }); +} +pub unsafe fn wrap_10(mut v: i64) -> i32 { + let mut l: Local_7 = Local_7 { x: v }; + return (unsafe { get_6(l) }); +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Local_5 { + pub x: i32, +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Local_7 { + pub x: i64, +} +pub unsafe fn other_11() -> i32 { + let mut l: Local_1 = Local_1 { x: 3_i64, y: 4_i64 }; + return ((unsafe { get_0(l) }) + (l.y as i32)); +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Local_1 { + pub x: i64, + pub y: i64, +} +pub fn main() { + unsafe { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut l: Local_3 = Local_3 { x: 7 }; + assert!(((unsafe { get_2(l,) }) == (7))); + assert!(((unsafe { twice_8(l,) }) == (14))); + assert!(((unsafe { other_11() }) == (7))); + assert!(((unsafe { wrap_9(5,) }) == (5))); + assert!(((unsafe { wrap_10(6_i64,) }) == (6))); + return 0; +} +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct Local_3 { + pub x: i32, +} diff --git a/tests/unit/out/unsafe/union_pointer_pun_address.rs b/tests/unit/out/unsafe/union_pointer_pun_address.rs index 858e04b8..7b3e50ab 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_address.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_address.rs @@ -24,17 +24,6 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut a: node_a = node_a { n: 123 }; - #[repr(C)] - #[derive(Copy, Clone)] - pub union anon_0 { - pub to_a: *mut node_a, - pub to_b: *mut node_b, - } - impl Default for anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } - }; let mut ptr: anon_0 = ::default(); ptr.to_a = (&mut a as *mut node_a); let mut out: *mut node_b = ptr.to_b; @@ -45,3 +34,14 @@ unsafe fn main_0() -> i32 { ); return 0; } +#[repr(C)] +#[derive(Copy, Clone)] +pub union anon_0 { + pub to_a: *mut node_a, + pub to_b: *mut node_b, +} +impl Default for anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +} diff --git a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs index 172ad396..7cdcc2aa 100644 --- a/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs +++ b/tests/unit/out/unsafe/union_pointer_pun_writethrough.rs @@ -13,20 +13,20 @@ pub fn main() { } unsafe fn main_0() -> i32 { let mut x: i64 = (-1_i32 as i64); - #[repr(C)] - #[derive(Copy, Clone)] - pub union anon_0 { - pub as_unsigned: *mut u64, - pub as_signed: *mut i64, - } - impl Default for anon_0 { - fn default() -> Self { - unsafe { std::mem::zeroed() } - } - }; let mut pp: anon_0 = ::default(); pp.as_signed = (&mut x as *mut i64); (*pp.as_unsigned) = 42_u64; assert!(((((x) == (42_i64)) as i32) != 0)); return 0; } +#[repr(C)] +#[derive(Copy, Clone)] +pub union anon_0 { + pub as_unsigned: *mut u64, + pub as_signed: *mut i64, +} +impl Default for anon_0 { + fn default() -> Self { + unsafe { std::mem::zeroed() } + } +}