Skip to content

Commit 284f92a

Browse files
committed
Translate inner templates
1 parent 7691542 commit 284f92a

4 files changed

Lines changed: 56 additions & 5 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,12 @@ void Converter::EmitRustStructOrUnion(clang::RecordDecl *decl) {
823823
}
824824
}
825825
}
826+
if (auto *nested_tmpl = clang::dyn_cast<clang::ClassTemplateDecl>(d)) {
827+
for (auto *spec : nested_tmpl->specializations()) {
828+
inner_structs_[GetID(spec)] = GetRecordName(spec);
829+
VisitCXXRecordDecl(spec);
830+
}
831+
}
826832
}
827833

828834
if (decl->isUnion()) {

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,10 @@ static std::string GetLexicalSpecializationID(const clang::Decl *decl) {
595595
clang::dyn_cast<clang::VarTemplateSpecializationDecl>(decl)) {
596596
id += clang::ASTNameGenerator(var->getASTContext()).getName(var);
597597
}
598+
if (const auto *self =
599+
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(decl)) {
600+
id += Mapper::ToString(Mapper::GetTypeForDecl(self));
601+
}
598602
if (const auto *spec =
599603
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(
600604
decl->getLexicalDeclContext());

‎tests/unit/out/refcount/class_templates.rs‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,36 @@ impl ByteRepr for Outer_long__Inner_int_ {
235235
}
236236
}
237237
#[derive(Default)]
238+
pub struct Outer_long__Inner_char_ {
239+
pub t: Value<i64>,
240+
pub u: Value<u8>,
241+
}
242+
impl Clone for Outer_long__Inner_char_ {
243+
fn clone(&self) -> Self {
244+
let __this: Value<Outer_long__Inner_char_> = Rc::new(RefCell::new(Self {
245+
t: Rc::new(RefCell::new((*self.t.borrow()))),
246+
u: Rc::new(RefCell::new((*self.u.borrow()))),
247+
}));
248+
let this: Ptr<Outer_long__Inner_char_> = __this.as_pointer();
249+
Rc::try_unwrap(__this).ok().unwrap().into_inner()
250+
}
251+
}
252+
impl ByteRepr for Outer_long__Inner_char_ {
253+
fn byte_size() -> usize {
254+
16
255+
}
256+
fn to_bytes(&self, buf: &mut [u8]) {
257+
(*self.t.borrow()).to_bytes(&mut buf[0..8]);
258+
(*self.u.borrow()).to_bytes(&mut buf[8..9]);
259+
}
260+
fn from_bytes(buf: &[u8]) -> Self {
261+
Self {
262+
t: Rc::new(RefCell::new(<i64>::from_bytes(&buf[0..8]))),
263+
u: Rc::new(RefCell::new(<u8>::from_bytes(&buf[8..9]))),
264+
}
265+
}
266+
}
267+
#[derive(Default)]
238268
pub struct Outer_long_ {
239269
pub v: Value<i64>,
240270
}
@@ -296,7 +326,7 @@ fn main_0() -> i32 {
296326
}) == 7)
297327
);
298328
assert!(
299-
(({ Outer_long__Inner_int_Impl::sum(&ic.as_pointer(),) }) == (6 + (('a' as u8) as i32)))
329+
(({ Outer_long__Inner_char_Impl::sum(&ic.as_pointer(),) }) == (6 + (('a' as u8) as i32)))
300330
);
301331
assert!((({ Boxed_int_::twice(3,) }) == 6));
302332
let bi: Value<Boxed_int_> = Rc::new(RefCell::new(Boxed_int_ {
@@ -491,14 +521,19 @@ impl Outer_long_Impl for Ptr<Outer_long_> {
491521
};
492522
}
493523
}
494-
pub trait Outer_long__Inner_int_Impl {
524+
pub trait Outer_long__Inner_char_Impl {
495525
fn sum(&self) -> i32;
496526
}
497-
impl Outer_long__Inner_int_Impl for Ptr<Outer_long__Inner_int_> {
527+
impl Outer_long__Inner_char_Impl for Ptr<Outer_long__Inner_char_> {
498528
fn sum(&self) -> i32 {
499529
return (((*(*(*self).upgrade().deref()).t.borrow()) as i32)
500530
+ ((*(*(*self).upgrade().deref()).u.borrow()) as i32));
501531
}
532+
}
533+
pub trait Outer_long__Inner_int_Impl {
534+
fn sum(&self) -> i32;
535+
}
536+
impl Outer_long__Inner_int_Impl for Ptr<Outer_long__Inner_int_> {
502537
fn sum(&self) -> i32 {
503538
return (((*(*(*self).upgrade().deref()).t.borrow()) as i32)
504539
+ ((*(*(*self).upgrade().deref()).u.borrow()) as i32));

‎tests/unit/out/unsafe/class_templates.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,13 @@ impl Outer_long__Inner_int_ {
142142
return ((self.t as i32) + (self.u as i32));
143143
}
144144
}
145-
impl Outer_long__Inner_int_ {
145+
#[repr(C)]
146+
#[derive(Copy, Clone, Default)]
147+
pub struct Outer_long__Inner_char_ {
148+
pub t: i64,
149+
pub u: libc::c_char,
150+
}
151+
impl Outer_long__Inner_char_ {
146152
pub unsafe fn sum(&self) -> i32 {
147153
return ((self.t as i32) + (self.u as i32));
148154
}
@@ -178,7 +184,7 @@ unsafe fn main_0() -> i32 {
178184
== (7))
179185
);
180186
assert!(
181-
((unsafe { Outer_long__Inner_int_::sum(&ic,) }) == ((6) + (('a' as libc::c_char) as i32)))
187+
((unsafe { Outer_long__Inner_char_::sum(&ic,) }) == ((6) + (('a' as libc::c_char) as i32)))
182188
);
183189
assert!(((unsafe { Boxed_int_::twice(3,) }) == (6)));
184190
let mut bi: Boxed_int_ = Boxed_int_ { value: 4 };

0 commit comments

Comments
 (0)