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
24 changes: 24 additions & 0 deletions cpp2rust/converter/rules/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,35 @@ void checkGenerics(const std::string &dir, const std::string &name,
}
}

void checkReturn(const std::string &dir, const std::string &name, Model target,
const clang::NamedDecl *decl,
const TranslationRule::ExprRule &rule) {
auto function = decl->getAsFunction();
auto type =
function ? function->getType()
: llvm::cast<clang::VarDecl>(decl)->getType()->getPointeeType();
auto function_type = type->getAs<clang::FunctionType>();
assert(function_type &&
"expression rule is neither a function nor a function pointer");
if (function_type->getReturnType()->isReferenceType() &&
!rule.return_type.is_pointer() && !rule.return_type.is_reference &&
!rule.returns_place) {
llvm::errs() << "rules/" << dir << ' ' << name
<< ": C++ returns a reference but the "
<< (target == Model::kRefCount ? "refcount" : "unsafe")
<< " target returns by value\n";
llvm::report_fatal_error("Expr rule returns a reference by value");
}
}

void addRule(clang::ASTContext &ctx, const std::string &dir,
const std::string &name, const clang::NamedDecl *decl) {
auto rule = findRule(Model::kUnsafe, dir, name);
auto target = Model::kUnsafe;
if (model_ == Model::kRefCount) {
if (auto refcount_rule = findRule(model_, dir, name)) {
rule = refcount_rule;
target = model_;
}
}
assert(rule && "rule has no target");
Expand All @@ -185,6 +208,7 @@ void addRule(clang::ASTContext &ctx, const std::string &dir,
assert(llvm::isa<clang::VarDecl>(decl) &&
"expression rule is neither a function nor a variable");
}
checkReturn(dir, name, target, decl, expr_rule);
auto keys = RuleKeys::GetExprKeys(*sema_, decl);
assert(!keys.empty() && "expression rule has no key");
for (const auto &key : keys) {
Expand Down
6 changes: 6 additions & 0 deletions cpp2rust/converter/translation_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ TypeInfo ParseTypeInfoJSON(const llvm::json::Object &obj) {
info.is_refcount_pointer = *v;
if (auto v = obj.getBoolean("is_unsafe_pointer"))
info.is_unsafe_pointer = *v;
if (auto v = obj.getBoolean("is_reference"))
info.is_reference = *v;
if (auto v = obj.getInteger("generic"))
info.generic = *v;
assert(!(info.is_refcount_pointer && info.is_unsafe_pointer));
Expand Down Expand Up @@ -438,6 +440,10 @@ ExprRule ParseExprRuleJSON(const llvm::json::Object &obj) {
ir.is_extern = *v;
}

if (auto v = obj.getBoolean("returns_place")) {
ir.returns_place = *v;
}

if (auto *generics = obj.getObject("generics")) {
for (auto &[key, val] : *generics) {
auto *arr = val.getAsArray();
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/translation_rule.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ struct TypeInfo {
std::string cpp_type;
bool is_refcount_pointer = false;
bool is_unsafe_pointer = false;
bool is_reference = false;
std::optional<Elems> elems;
unsigned generic = 0;

Expand All @@ -125,6 +126,7 @@ struct ExprRule {
std::vector<BodyFragment> body;
bool multi_statement = false;
bool is_extern = false;
bool returns_place = false;

void dump() const;
bool usesInit() const;
Expand Down
4 changes: 4 additions & 0 deletions rule-preprocessor/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ pub struct TypeInfo {
pub is_refcount_pointer: bool,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub is_unsafe_pointer: bool,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub is_reference: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub derives: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -105,6 +107,8 @@ pub struct FnIr {
pub return_type: Option<TypeInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_extern: Option<bool>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub returns_place: bool,
}

impl FnIr {
Expand Down
38 changes: 38 additions & 0 deletions rule-preprocessor/src/syntactic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ impl<'a> FnIrBuilder<'a> {
ty: ty_str,
is_refcount_pointer,
is_unsafe_pointer,
is_reference: matches!(&ty, ast::Type::RefType(_)),
derives: Vec::new(),
cpp_type: None,
elems: None,
Expand Down Expand Up @@ -635,6 +636,40 @@ impl<'a> FnIrBuilder<'a> {
.any(|a| a.kind() == SyntaxKind::EXTERN_BLOCK)
}

fn returns_place(&self, params: &[ParamInfo]) -> bool {
fn is_place(expr: ast::Expr, params: &[ParamInfo], projected: bool) -> bool {
match expr {
ast::Expr::FieldExpr(e) => {
e.expr().is_some_and(|base| is_place(base, params, true))
}
ast::Expr::IndexExpr(e) => {
e.base().is_some_and(|base| is_place(base, params, true))
}
ast::Expr::ParenExpr(e) => e
.expr()
.is_some_and(|inner| is_place(inner, params, projected)),
ast::Expr::PrefixExpr(e) => {
e.op_kind() == Some(ast::UnaryOp::Deref)
&& e.expr()
.is_some_and(|inner| is_place(inner, params, projected))
}
ast::Expr::PathExpr(e) => {
projected
&& e.path()
.and_then(|path| path.as_single_name_ref())
.is_some_and(|name| params.iter().any(|p| p.name == name.text()))
}
_ => false,
}
}
self.fn_item
.body()
.and_then(|body| body.stmt_list())
.filter(|stmt_list| stmt_list.statements().next().is_none())
.and_then(|stmt_list| stmt_list.tail_expr())
.is_some_and(|tail| is_place(tail, params, false))
}

fn returns_mut_ref(&self) -> bool {
self.fn_item
.ret_type()
Expand Down Expand Up @@ -728,6 +763,7 @@ impl<'a> FnIrBuilder<'a> {
ty: p.ty.clone(),
is_refcount_pointer: p.is_refcount_pointer,
is_unsafe_pointer: p.is_unsafe_pointer,
is_reference: false,
derives: Vec::new(),
cpp_type: p.cpp_type.clone(),
elems: None,
Expand Down Expand Up @@ -767,6 +803,7 @@ impl<'a> FnIrBuilder<'a> {
multi_statement,
body,
is_extern: self.is_extern().then_some(true),
returns_place: self.returns_place(&params),
};
ir.validate(&format!("{}:{}", path.display(), fn_name));
ir
Expand Down Expand Up @@ -840,6 +877,7 @@ impl<'a> TypeIrBuilder<'a> {
ty: ty.syntax().text().to_string(),
is_refcount_pointer,
is_unsafe_pointer,
is_reference: false,
derives: Vec::new(),
cpp_type: None,
elems: elems(&ty),
Expand Down
16 changes: 12 additions & 4 deletions rules/array/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,28 @@ fn f4<T1: MoveCtor, const T2: usize>(a0: Ptr<T1>) -> Box<[T1]> {
(0..T2).map(|__i| T1::move_ctor(a0.offset(__i))).collect()
}

fn f5<T1: MoveAssign, const T2: usize>(a0: Ptr<T1>, a1: Ptr<T1>) {
fn f5<T1: MoveAssign + ByteRepr, const T2: usize>(
a0: Ptr<Box<[T1]>>,
a1: Ptr<T1>,
) -> Ptr<Box<[T1]>> {
for __i in 0..T2 {
T1::move_assign(&a0.offset(__i), a1.offset(__i));
T1::move_assign(&a0.decay().offset(__i), a1.offset(__i));
}
a0
}

fn f6<T1: Clone + ByteRepr, const T2: usize>(a0: Ptr<T1>) -> Box<[T1]> {
(0..T2).map(|__i| a0.read_at(__i)).collect()
}

fn f7<T1: CopyAssign, const T2: usize>(a0: Ptr<T1>, a1: Ptr<T1>) {
fn f7<T1: CopyAssign + ByteRepr, const T2: usize>(
a0: Ptr<Box<[T1]>>,
a1: Ptr<T1>,
) -> Ptr<Box<[T1]>> {
for __i in 0..T2 {
T1::copy_assign(&a0.offset(__i), a1.offset(__i));
T1::copy_assign(&a0.decay().offset(__i), a1.offset(__i));
}
a0
}

fn f8<T1, const T2: usize>(a0: [T1; T2]) -> Box<[T1]> {
Expand Down
16 changes: 12 additions & 4 deletions rules/array/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,28 @@ unsafe fn f4<T1: MoveCtorUnsafe, const T2: usize>(a0: &mut [T1; T2]) -> [T1; T2]
std::array::from_fn(|__i| T1::move_ctor(&mut a0[__i]))
}

unsafe fn f5<T1: MoveAssignUnsafe, const T2: usize>(a0: &mut [T1; T2], a1: &mut [T1; T2]) {
unsafe fn f5<T1: MoveAssignUnsafe, const T2: usize>(
a0: *mut [T1; T2],
a1: &mut [T1; T2],
) -> *mut [T1; T2] {
for __i in 0..T2 {
T1::move_assign(&mut a0[__i], &mut a1[__i]);
T1::move_assign(&mut (*a0)[__i], &mut a1[__i]);
}
a0
}

unsafe fn f6<T1: Clone, const T2: usize>(a0: [T1; T2]) -> [T1; T2] {
a0.clone()
}

unsafe fn f7<T1: CopyAssignUnsafe, const T2: usize>(a0: &mut [T1; T2], a1: &[T1; T2]) {
unsafe fn f7<T1: CopyAssignUnsafe, const T2: usize>(
a0: *mut [T1; T2],
a1: &[T1; T2],
) -> *mut [T1; T2] {
for __i in 0..T2 {
T1::copy_assign(&mut a0[__i], &a1[__i]);
T1::copy_assign(&mut (*a0)[__i], &a1[__i]);
}
a0
}

unsafe fn f8<T1, const T2: usize>(a0: [T1; T2]) -> [T1; T2] {
Expand Down
10 changes: 6 additions & 4 deletions rules/deque/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ fn f2<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}

fn f10<T1: Clone + ByteRepr>(a0: Ptr<Vec<T1>>, a1: Vec<T1>) {
a0.write(a1)
fn f10<T1: Clone + ByteRepr>(a0: Ptr<Vec<T1>>, a1: Vec<T1>) -> Ptr<Vec<T1>> {
a0.write(a1);
a0
}

fn f11<T1: ByteRepr + Clone>(a0: Ptr<Vec<T1>>, a1: &mut Vec<T1>) {
a0.write(std::mem::take(&mut *a1))
fn f11<T1: ByteRepr + Clone>(a0: Ptr<Vec<T1>>, a1: &mut Vec<T1>) -> Ptr<Vec<T1>> {
a0.write(std::mem::take(&mut *a1));
a0
}
10 changes: 6 additions & 4 deletions rules/deque/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ unsafe fn f9<T1>(a0: &mut Vec<T1>) -> Vec<T1> {
std::mem::take(&mut *a0)
}

unsafe fn f10<T1: Clone>(a0: &mut Vec<T1>, a1: Vec<T1>) {
*a0 = a1.clone()
unsafe fn f10<T1: Clone>(a0: *mut Vec<T1>, a1: Vec<T1>) -> *mut Vec<T1> {
*a0 = a1.clone();
a0
}

unsafe fn f11<T1>(a0: &mut Vec<T1>, a1: &mut Vec<T1>) {
*a0 = std::mem::take(&mut *a1)
unsafe fn f11<T1>(a0: *mut Vec<T1>, a1: &mut Vec<T1>) -> *mut Vec<T1> {
*a0 = std::mem::take(&mut *a1);
a0
}

unsafe fn f12<T1>(a0: &mut Vec<T1>, init: T1) {
Expand Down
28 changes: 16 additions & 12 deletions rules/iterator_category/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ fn f2<T1: CppIterator<T2>, T2>(a0: T1) -> T1 {
a0
}

fn f3<T1: CppIterator<T2>, T2>(a0: &mut T1, a1: T1) {
*a0 = a1.clone()
fn f3<T1: CppIterator<T2> + ByteRepr, T2>(a0: Ptr<T1>, a1: T1) -> Ptr<T1> {
a0.write(a1.clone());
a0
}

fn f4<T1: CppIterator<T2>, T2>(a0: &mut T1, a1: T1) {
*a0 = a1
fn f4<T1: CppIterator<T2> + ByteRepr, T2>(a0: Ptr<T1>, a1: T1) -> Ptr<T1> {
a0.write(a1);
a0
}

fn f5<T1: CppIterator<T2>, T2>(a0: T1) -> Ptr<T2> {
Expand Down Expand Up @@ -131,16 +133,18 @@ fn f24<T1: From<T2>, T2>(a0: T2) -> T1 {
<T1>::from(a0)
}

fn f25<T1: CppIterator<T2>, T2>(a0: T1) -> T1 {
let mut __it = a0;
__it.iter_inc();
__it
fn f25<T1: CppIterator<T2> + ByteRepr, T2>(a0: Ptr<T1>) -> Ptr<T1> {
a0.with_mut(|__v| {
__v.iter_inc();
});
a0
}

fn f26<T1: CppBidirectionalIterator<T2>, T2>(a0: T1) -> T1 {
let mut __it = a0;
__it.iter_dec();
__it
fn f26<T1: CppBidirectionalIterator<T2> + ByteRepr, T2>(a0: Ptr<T1>) -> Ptr<T1> {
a0.with_mut(|__v| {
__v.iter_dec();
});
a0
}

fn f27<T1: CppInputIterator<T3> + PartialEq<T2>, T2: CppInputIterator<T3>, T3>(a0: T1, a1: T2) -> bool {
Expand Down
24 changes: 12 additions & 12 deletions rules/iterator_category/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ unsafe fn f2<T1: UnsafeCppIterator<T2>, T2>(a0: T1) -> T1 {
a0
}

unsafe fn f3<T1: UnsafeCppIterator<T2>, T2>(a0: &mut T1, a1: T1) {
*a0 = a1.clone()
unsafe fn f3<T1: UnsafeCppIterator<T2>, T2>(a0: *mut T1, a1: T1) -> *mut T1 {
*a0 = a1.clone();
a0
}

unsafe fn f4<T1: UnsafeCppIterator<T2>, T2>(a0: &mut T1, a1: T1) {
*a0 = a1
unsafe fn f4<T1: UnsafeCppIterator<T2>, T2>(a0: *mut T1, a1: T1) -> *mut T1 {
*a0 = a1;
a0
}

unsafe fn f5<T1: UnsafeCppIterator<T2>, T2>(a0: T1) -> *mut T2 {
Expand Down Expand Up @@ -123,16 +125,14 @@ unsafe fn f24<T1: From<T2>, T2>(a0: T2) -> T1 {
<T1>::from(a0)
}

unsafe fn f25<T1: UnsafeCppIterator<T2>, T2>(a0: T1) -> T1 {
let mut __it = a0;
__it.iter_inc();
__it
unsafe fn f25<T1: UnsafeCppIterator<T2>, T2>(a0: *mut T1) -> *mut T1 {
(*a0).iter_inc();
a0
}

unsafe fn f26<T1: UnsafeCppBidirectionalIterator<T2>, T2>(a0: T1) -> T1 {
let mut __it = a0;
__it.iter_dec();
__it
unsafe fn f26<T1: UnsafeCppBidirectionalIterator<T2>, T2>(a0: *mut T1) -> *mut T1 {
(*a0).iter_dec();
a0
}

unsafe fn f27<T1: UnsafeCppInputIterator<T3> + PartialEq<T2>, T2: UnsafeCppInputIterator<T3>, T3>(a0: T1, a1: T2) -> bool {
Expand Down
10 changes: 6 additions & 4 deletions rules/map/tgt_refcount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ fn f24<T1: 'static, T2: 'static>(a0: Ptr<Map<T1, (Value<T1>, Value<T2>)>>) -> Ma
a0.with_mut(|__m: &mut Map<T1, (Value<T1>, Value<T2>)>| std::mem::take(__m))
}

fn f25<T1: 'static, T2: 'static>(a0: Ptr<Map<T1, (Value<T1>, Value<T2>)>>, a1: Ptr<Map<T1, (Value<T1>, Value<T2>)>>) {
fn f25<T1: 'static, T2: 'static>(a0: Ptr<Map<T1, (Value<T1>, Value<T2>)>>, a1: Ptr<Map<T1, (Value<T1>, Value<T2>)>>) -> Ptr<Map<T1, (Value<T1>, Value<T2>)>> {
let __src = a1.with_mut(|__m: &mut Map<T1, (Value<T1>, Value<T2>)>| std::mem::take(__m));
a0.write(__src)
a0.write(__src);
a0
}

fn f26<T1: CppLess + Clone + 'static, T2: Clone + 'static>(a0: Ptr<Map<T1, (Value<T1>, Value<T2>)>>, a1: Map<T1, (Value<T1>, Value<T2>)>) {
a0.write(a1.clone())
fn f26<T1: CppLess + Clone + 'static, T2: Clone + 'static>(a0: Ptr<Map<T1, (Value<T1>, Value<T2>)>>, a1: Map<T1, (Value<T1>, Value<T2>)>) -> Ptr<Map<T1, (Value<T1>, Value<T2>)>> {
a0.write(a1.clone());
a0
}

fn f27<T1: CppLess + 'static, T2: 'static>(
Expand Down
10 changes: 6 additions & 4 deletions rules/map/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ unsafe fn f24<T1, T2>(a0: &mut UnsafeMap<T1, (T1, T2)>) -> UnsafeMap<T1, (T1, T2
std::mem::take(&mut *a0)
}

unsafe fn f25<T1, T2>(a0: &mut UnsafeMap<T1, (T1, T2)>, a1: &mut UnsafeMap<T1, (T1, T2)>) {
*a0 = std::mem::take(&mut *a1)
unsafe fn f25<T1, T2>(a0: *mut UnsafeMap<T1, (T1, T2)>, a1: &mut UnsafeMap<T1, (T1, T2)>) -> *mut UnsafeMap<T1, (T1, T2)> {
*a0 = std::mem::take(&mut *a1);
a0
}

unsafe fn f26<T1: Ord + Clone, T2: Clone>(a0: &mut UnsafeMap<T1, (T1, T2)>, a1: UnsafeMap<T1, (T1, T2)>) {
*a0 = a1.clone()
unsafe fn f26<T1: Ord + Clone, T2: Clone>(a0: *mut UnsafeMap<T1, (T1, T2)>, a1: UnsafeMap<T1, (T1, T2)>) -> *mut UnsafeMap<T1, (T1, T2)> {
*a0 = a1.clone();
a0
}

unsafe fn f27<T1: Ord, T2>(
Expand Down
Loading
Loading