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
7 changes: 7 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,13 @@ bool Converter::VisitFunctionTemplateDecl(clang::FunctionTemplateDecl *decl) {
return false;
}

bool Converter::VisitVarTemplateDecl(clang::VarTemplateDecl *decl) {
for (auto *var_decl : decl->specializations()) {
VisitVarDecl(var_decl);
}
return false;
}

void Converter::ConvertVaListVarDecl(clang::VarDecl *decl) {
if (clang::isa<clang::ParmVarDecl>(decl)) {
// va_list parameter (decayed to __va_list_tag *)
Expand Down
1 change: 1 addition & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
void EmitHoistedDecls(clang::CompoundStmt *body);

virtual bool VisitFunctionTemplateDecl(clang::FunctionTemplateDecl *decl);
bool VisitVarTemplateDecl(clang::VarTemplateDecl *decl);

virtual bool VisitVarDecl(clang::VarDecl *decl);
virtual bool LazyStaticInit() const { return true; }
Expand Down
4 changes: 4 additions & 0 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ static std::string GetParamSignature(const clang::Decl *decl) {

static std::string GetLexicalSpecializationID(const clang::Decl *decl) {
std::string id;
if (const auto *var =
clang::dyn_cast<clang::VarTemplateSpecializationDecl>(decl)) {
id += clang::ASTNameGenerator(var->getASTContext()).getName(var);
}
if (const auto *spec =
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(
decl->getLexicalDeclContext());
Expand Down
16 changes: 15 additions & 1 deletion tests/unit/out/refcount/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ pub fn func_5(x1: f64, x2: i32, x3: f64) -> i32 {
let x3: Value<f64> = Rc::new(RefCell::new(x3));
return ((((*x1.borrow()) + ((*x2.borrow()) as f64)) + (*x3.borrow())) as i32);
}
thread_local!(
pub static half_6: Value<i32> = Rc::new(RefCell::new((1 / 2)));
);
thread_local!(
pub static half_7: Value<f64> = Rc::new(RefCell::new((1_f64 / 2_f64)));
);
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -59,6 +65,14 @@ fn main_0() -> i32 {
+ (({ func_5(2.0E+0, (*x.borrow()), (*y.borrow()),) }) as f64))
== 68_f64)
);
assert!((half_6.with(|rc| rc.borrow().clone()) == 0));
assert!((half_7.with(|rc| rc.borrow().clone()) == 5.0E-1));
(*half_6.with(Value::clone).borrow_mut()) = 7;
assert!((half_6.with(|rc| rc.borrow().clone()) == 7));
assert!((half_7.with(|rc| rc.borrow().clone()) == 5.0E-1));
return 0;
}
pub fn __cpp2rust_init_globals() {}
pub fn __cpp2rust_init_globals() {
let _ = half_6.with(|_| ());
let _ = half_7.with(|_| ());
}
14 changes: 13 additions & 1 deletion tests/unit/out/unsafe/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ pub unsafe fn func_4(mut x1: i32, mut x2: i32, mut x3: i32) -> i32 {
pub unsafe fn func_5(mut x1: f64, mut x2: i32, mut x3: f64) -> i32 {
return ((((x1) + (x2 as f64)) + (x3)) as i32);
}
pub static mut half_6: std::cell::LazyCell<i32> =
std::cell::LazyCell::new(|| unsafe { ((1) / (2)) });
pub static mut half_7: std::cell::LazyCell<f64> =
std::cell::LazyCell::new(|| unsafe { ((1_f64) / (2_f64)) });
pub fn main() {
unsafe {
__cpp2rust_init_globals();
Expand All @@ -41,6 +45,14 @@ unsafe fn main_0() -> i32 {
+ ((unsafe { func_5(2.0E+0, x, y,) }) as f64))
== (68_f64))
);
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut half_6)) == (0)));
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut half_7)) == (5.0E-1)));
(*std::cell::LazyCell::force_mut(&mut *&raw mut half_6)) = 7;
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut half_6)) == (7)));
assert!(((*std::cell::LazyCell::force_mut(&mut *&raw mut half_7)) == (5.0E-1)));
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
pub unsafe fn __cpp2rust_init_globals() {
std::cell::LazyCell::force(&*&raw const half_6);
std::cell::LazyCell::force(&*&raw const half_7);
}
7 changes: 7 additions & 0 deletions tests/unit/templates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ template <typename T1, typename T2, typename T3> int func(T1 x1, T2 x2, T3 x3) {
return x1 + x2 + x3;
}

template <typename T> T half = T(1) / T(2);

int main() {
int x = 10;
double y = x;
assert(foo(x) + foo(y) + *bar(&x, true) + *bar(&y, true) + func(1, 2, 3) +
func(2.0, x, y) ==
68);
assert(half<int> == 0);
assert(half<double> == 0.5);
half<int> = 7;
assert(half<int> == 7);
assert(half<double> == 0.5);
return 0;
}
Loading