Skip to content
Closed
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
15 changes: 12 additions & 3 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ bool Converter::VisitFunctionDecl(clang::FunctionDecl *decl) {
}
// main_0 should be static
if (!decl->isMain()) {
StrCat(AccessSpecifierAsString(decl->getAccess()));
StrCat(InlineAttribute(decl), AccessSpecifierAsString(decl->getAccess()));
}
StrCat(keyword_unsafe_, keyword::kFn, std::move(function_name));
{
Expand Down Expand Up @@ -1143,11 +1143,20 @@ Converter::GetConversionName(const clang::CXXConversionDecl *decl) const {
: name;
}

std::string Converter::InlineAttribute(const clang::FunctionDecl *decl) {
return decl->isInlined() && decl->doesThisDeclarationHaveABody()
? "#[inline] "
: "";
}

bool Converter::ConvertCXXMethodDecl(clang::CXXMethodDecl *decl) {
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(decl)) {
return VisitCXXConstructorDecl(ctor);
}

if (method_target_ != MethodTarget::TraitDecl) {
StrCat(InlineAttribute(decl));
}
if (method_target_ == MethodTarget::ValueImpl &&
(decl->isStatic() ||
(!decl->isVirtual() && !decl->getParent()->isAbstract()))) {
Expand Down Expand Up @@ -1204,8 +1213,8 @@ bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) {
return false;
}

StrCat(AccessSpecifierAsString(decl->getAccess()), keyword_unsafe_,
keyword::kFn, GetCtorName(decl));
StrCat(InlineAttribute(decl), AccessSpecifierAsString(decl->getAccess()),
keyword_unsafe_, keyword::kFn, GetCtorName(decl));
{
PushParen paren(*this);
ConvertFunctionParameters(decl);
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual bool ConvertOutOfLineMethod(clang::CXXMethodDecl *decl);

std::string InlineAttribute(const clang::FunctionDecl *decl);

bool ConvertCXXMethodDecl(clang::CXXMethodDecl *decl);

std::string GetMethodName(const clang::CXXMethodDecl *decl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ pub struct S {
pub n: Box<[i32]>,
}
impl S {
#[inline]
pub fn new(mut x: i32) -> Self {
Self {
v: vec![x; (x as usize) as usize],
n: Box::new([x, (x + 1)]),
}
}
#[inline]
pub fn move_from(_a0: Ptr<S>) -> Self {
Self {
v: field!(_a0, v).with_mut(std::mem::take),
Expand Down Expand Up @@ -51,6 +53,7 @@ pub struct T {
pub v: Vec<i32>,
}
impl T {
#[inline]
pub fn move_from(_a0: Ptr<T>) -> Self {
Self {
v: field!(_a0, v).with_mut(std::mem::take),
Expand Down Expand Up @@ -96,6 +99,7 @@ pub trait SImpl {
fn move_assign(&self, _a0: Ptr<S>) -> Ptr<S>;
}
impl SImpl for Ptr<S> {
#[inline]
fn move_assign(&self, _a0: Ptr<S>) -> Ptr<S> {
field_ref!((*self), v)
.as_pointer()
Expand All @@ -117,9 +121,11 @@ pub trait TImpl {
fn move_assign(&self, _a0: Ptr<T>) -> Ptr<T>;
}
impl TImpl for Ptr<T> {
#[inline]
fn size(&self) -> i32 {
return ((*(*self).upgrade().deref()).v.len() as i32);
}
#[inline]
fn move_assign(&self, _a0: Ptr<T>) -> Ptr<T> {
field_ref!((*self), v)
.as_pointer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ pub struct S {
pub n: [i32; 2],
}
impl S {
#[inline]
pub unsafe fn new(mut x: i32) -> Self {
let mut this = Self {
v: vec![x; (x as usize) as usize],
n: [x, ((x) + (1))],
};
this
}
#[inline]
pub unsafe fn move_from(_a0: *mut S) -> Self {
let mut this = Self {
v: std::mem::take(&mut (*_a0).v),
n: std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).n[(__i)]),
};
this
}
#[inline]
pub unsafe fn move_assign(&mut self, _a0: *mut S) -> *mut S {
self.v = std::mem::take(&mut (*_a0).v);
match (
Expand All @@ -50,15 +53,18 @@ pub struct T {
pub v: Vec<i32>,
}
impl T {
#[inline]
pub unsafe fn size(&self) -> i32 {
return (self.v.len() as i32);
}
#[inline]
pub unsafe fn move_from(_a0: *mut T) -> Self {
let mut this = Self {
v: std::mem::take(&mut (*_a0).v),
};
this
}
#[inline]
pub unsafe fn move_assign(&mut self, _a0: *mut T) -> *mut T {
self.v = std::mem::take(&mut (*_a0).v);
return &mut (*(self as *mut T));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Pair {
#[offset(4)]
pub second: i32,
}
#[inline]
pub fn pair_diff_0(mut p: Ptr<Pair>) -> i32 {
return ({ p.with(|__s| __s.first) } - { p.with(|__s| __s.second) });
}
Expand Down Expand Up @@ -88,6 +89,7 @@ pub trait SImpl {
}
}
impl SImpl for Ptr<S> {
#[inline]
fn get(&self) -> i32 {
return (*self).with(|__s| __s.v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct S {
pub v: i32,
}
impl S {
#[inline]
pub unsafe fn get(&self) -> i32 {
return self.v;
}
Expand All @@ -31,6 +32,7 @@ pub struct Pair {
pub first: i32,
pub second: i32,
}
#[inline]
pub unsafe fn pair_diff_0(mut p: *const Pair) -> i32 {
return (((*p).first) - ((*p).second));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ pub trait S_int_Impl {
}
}
impl S_int_Impl for Ptr<S_int_> {
#[inline]
fn set(&self, mut v: i32) {
field!((*self), x).write(v);
}
#[inline]
fn get(&self) -> i32 {
return (*self).with(|__s| __s.x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct S_int_ {
pub x: i32,
}
impl S_int_ {
#[inline]
pub unsafe fn set(&mut self, mut v: i32) {
self.x = v;
}
Expand All @@ -34,6 +35,7 @@ unsafe fn main_0() -> i32 {
return 0;
}
impl S_int_ {
#[inline]
pub unsafe fn get(&mut self) -> i32 {
return self.x;
}
Expand Down
1 change: 1 addition & 0 deletions tests/ub/out/refcount/ctor_ref_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct S {
pub r: Ptr<i32>,
}
impl S {
#[inline]
pub fn new(x: Ptr<i32>) -> Self {
Self { r: (x).clone() }
}
Expand Down
1 change: 1 addition & 0 deletions tests/ub/out/unsafe/ctor_ref_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct S {
pub r: *const i32,
}
impl S {
#[inline]
pub unsafe fn new(x: *const i32) -> Self {
let mut this = Self { r: x };
this
Expand Down
65 changes: 65 additions & 0 deletions tests/unit/inline_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <cassert>

inline int inlined_free(int a) { return a + 1; }

constexpr int inlined_constexpr_free(int a) { return a + 2; }

inline int inlined_declared_free(int a);

int inlined_declared_free(int a) { return a + 3; }

int not_inlined_free(int a) { return a + 4; }

static int not_inlined_static_free(int a) { return a + 5; }

class Base {
public:
virtual int inlined_override(int a) const = 0;
};

class Derived : public Base {
public:
explicit Derived(int v) : v_(v) {}

int inlined_override(int a) const override { return v_ + a; }

int inlined_in_class() const { return v_ * 2; }

static int inlined_static_in_class(int a) { return a * 3; }

inline int inlined_declared_in_class() const;

int inlined_defined_out_of_line() const;

int not_inlined_out_of_line() const;

static int not_inlined_static_out_of_line(int a);

private:
int v_;
};

int Derived::inlined_declared_in_class() const { return v_ * 4; }

inline int Derived::inlined_defined_out_of_line() const { return v_ * 5; }

int Derived::not_inlined_out_of_line() const { return v_ * 6; }

int Derived::not_inlined_static_out_of_line(int a) { return a * 7; }

int main() {
Derived d(3);
assert(inlined_free(1) == 2);
assert(inlined_constexpr_free(1) == 3);
assert(inlined_declared_free(1) == 4);
assert(not_inlined_free(1) == 5);
assert(not_inlined_static_free(1) == 6);
assert(d.inlined_override(1) == 4);
assert(d.inlined_in_class() == 6);
assert(Derived::inlined_static_in_class(2) == 6);
assert(d.inlined_declared_in_class() == 12);
assert(d.inlined_defined_out_of_line() == 15);
assert(d.not_inlined_out_of_line() == 18);
assert(Derived::not_inlined_static_out_of_line(2) == 14);
return 0;
}
1 change: 1 addition & 0 deletions tests/unit/out/refcount/05_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub trait DImpl {
fn operator_call(&self, ptr: Ptr<A>);
}
impl DImpl for Ptr<D> {
#[inline]
fn operator_call(&self, mut ptr: Ptr<A>) {
ptr.delete();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/out/refcount/10_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct Partial {
pub p: Ptr<i32>,
}
impl Partial {
#[inline]
pub fn new_1(mut q: Ptr<i32>) -> Self {
Self { p: (q).clone() }
}
Expand Down Expand Up @@ -89,6 +90,7 @@ pub trait GraphImpl {
fn push(&self, src: u32, dst: u32);
}
impl GraphImpl for Ptr<Graph> {
#[inline]
fn push(&self, mut src: u32, mut dst: u32) {
let __rhs = Ptr::alloc(GraphNode {
dst: dst,
Expand Down
1 change: 1 addition & 0 deletions tests/unit/out/refcount/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Point {
pub y: i32,
}
impl Point {
#[inline]
pub fn new(mut x: i32, mut y: i32) -> Self {
Self { x: x, y: y }
}
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/out/refcount/array_of_noncopy_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct NonCopy {
pub tag: i32,
}
impl NonCopy {
#[inline]
pub fn move_from(_a0: Ptr<NonCopy>) -> Self {
Self {
data: field!(_a0, data).with_mut(std::mem::take),
Expand Down Expand Up @@ -55,6 +56,7 @@ pub trait NonCopyImpl {
fn move_assign(&self, _a0: Ptr<NonCopy>) -> Ptr<NonCopy>;
}
impl NonCopyImpl for Ptr<NonCopy> {
#[inline]
fn move_assign(&self, _a0: Ptr<NonCopy>) -> Ptr<NonCopy> {
field_ref!((*self), data)
.as_pointer()
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/out/refcount/as_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,19 @@ pub trait SImpl {
fn value_ref_4(&self) -> Ptr<i32>;
}
impl SImpl for Ptr<S> {
#[inline]
fn f_1(&self) -> Overload {
return Overload_kMutableOverload;
}
#[inline]
fn f_2(&self) -> Overload {
return Overload_kConstOverload;
}
#[inline]
fn value_ref_3(&self) -> Ptr<i32> {
return field_ptr!((*self), v);
}
#[inline]
fn value_ref_4(&self) -> Ptr<i32> {
return field_ptr!((*self), v);
}
Expand Down
Loading
Loading