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
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
return true;
}

bool TraverseExplicitInstantiationDecl(clang::ExplicitInstantiationDecl *) {
return true;
}

virtual bool
VisitCXXStdInitializerListExpr(clang::CXXStdInitializerListExpr *expr);

Expand Down
10 changes: 9 additions & 1 deletion cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,15 @@ bool IsCodeUnitStringLiteral(const clang::StringLiteral *expr) {
bool IsUserDefinedDecl(const clang::Decl *decl) {
const auto &ctx = decl->getASTContext();
const auto &src_mgr = ctx.getSourceManager();
const auto src_loc = decl->getLocation();
auto src_loc = decl->getLocation();
if (auto *spec =
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(decl);
spec && (spec->getSpecializationKind() ==
clang::TSK_ExplicitInstantiationDeclaration ||
spec->getSpecializationKind() ==
clang::TSK_ExplicitInstantiationDefinition)) {
src_loc = spec->getSpecializedTemplate()->getLocation();
}
return !decl->getBeginLoc().isInvalid() && !decl->isImplicit() &&
!src_mgr.isInSystemHeader(src_loc) &&
!src_mgr.isInSystemMacro(src_loc);
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/class_templates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,20 @@ template <typename T> struct Outer {
Inner<int> with(int n) const { return Inner<int>{v, n}; }
};

struct Elem {
int i;
};

template class std::vector<Elem>;

template struct Boxed<short>;

int main() {
std::vector<Elem> ev;
ev.push_back(Elem{2});
assert(ev[0].i == 2);
assert(Boxed<short>::twice(2) == 4);

Outer<int> oi{3};
assert(oi.with(4).sum() == 7);
Outer<long> ol{5};
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/out/refcount/class_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ pub struct MyContainer_float_ {
vec_: Value<Vec<f32>>,
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(2)]
pub struct Boxed_short_ {
#[offset(0)]
pub value: i16,
}
impl Boxed_short_ {
pub fn twice(mut v: i16) -> i16 {
return (((v as i32) + (v as i32)) as i16);
}
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(4)]
pub struct Boxed_int_ {
#[offset(0)]
Expand Down Expand Up @@ -85,11 +96,24 @@ pub struct Outer_long_ {
#[offset(0)]
pub v: i64,
}
#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, Default)]
#[byte_size(4)]
pub struct Elem {
#[offset(0)]
pub i: i32,
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
}
fn main_0() -> i32 {
let mut ev: Vec<Elem> = Vec::new();
{
let __a1 = Elem { i: 2 };
ev.push(__a1)
};
assert!(({ ev[0_usize].i } == 2));
assert!(((({ Boxed_short_::twice(2_i16,) }) as i32) == 4));
let oi: Value<Outer_int_> = Rc::new(RefCell::new(Outer_int_ { v: 3 }));
assert!(
(({
Expand Down Expand Up @@ -181,6 +205,14 @@ impl Boxed_long_Impl for Ptr<Boxed_long_> {
return ((*self).with(|__s| __s.value) + other);
}
}
pub trait Boxed_short_Impl {
fn plus(&self, other: i16) -> i16;
}
impl Boxed_short_Impl for Ptr<Boxed_short_> {
fn plus(&self, mut other: i16) -> i16 {
return ((((*self).with(|__s| __s.value) as i32) + (other as i32)) as i16);
}
}
pub trait MyContainer_char_Impl {
fn empty(&self) -> bool;
fn size(&self) -> usize;
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/out/unsafe/class_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ impl MyContainer_float_ {
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Boxed_short_ {
pub value: i16,
}
impl Boxed_short_ {
pub unsafe fn twice(mut v: i16) -> i16 {
return (((v as i32) + (v as i32)) as i16);
}
pub unsafe fn plus(&self, mut other: i16) -> i16 {
return (((self.value as i32) + (other as i32)) as i16);
}
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Boxed_int_ {
pub value: i32,
}
Expand Down Expand Up @@ -163,13 +176,25 @@ impl Outer_long_ {
return Outer_long__Inner_int_ { t: self.v, u: n };
}
}
#[repr(C)]
#[derive(Copy, Clone, VaArg, FnPtrArg, Default)]
pub struct Elem {
pub i: i32,
}
pub fn main() {
unsafe {
__cpp2rust_init_globals();
std::process::exit(main_0() as i32);
}
}
unsafe fn main_0() -> i32 {
let mut ev: Vec<Elem> = Vec::new();
{
let __a1 = Elem { i: 2 };
ev.push(__a1)
};
assert!(((ev[(0_usize)].i) == (2)));
assert!((((unsafe { Boxed_short_::twice(2_i16,) }) as i32) == (4)));
let mut oi: Outer_int_ = Outer_int_ { v: 3 };
assert!(
((unsafe { Outer_int__Inner_int_::sum(&(unsafe { Outer_int_::with(&oi, 4,) }),) }) == (7))
Expand Down
Loading