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
10 changes: 10 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ bool Converter::VisitTranslationUnitDecl(clang::TranslationUnitDecl *decl) {
if (IsUserDefinedDecl(child) &&
(IsInMainFile(child) || !decl_ids_.contains(GetID(child)))) {
Convert(child);
if (!hoisted_records_.empty()) {
StrCat(hoisted_records_);
hoisted_records_.clear();
}
}
}
return false;
Expand Down Expand Up @@ -1257,6 +1261,12 @@ bool Converter::VisitCompoundStmt(clang::CompoundStmt *stmt) {

bool Converter::VisitDeclStmt(clang::DeclStmt *stmt) {
for (auto *decl : stmt->decls()) {
if (clang::isa<clang::TagDecl>(decl)) {
Buffer buf(*this);
Convert(decl);
hoisted_records_ += std::move(buf).str();
continue;
}
Convert(decl);
StrCat(token::kSemiColon);
}
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
// translation units.
static std::map<std::string, MethodsOnPtr> methods_on_ptr_;

std::string hoisted_records_;

enum class ExprKind : uint8_t {
Callee,
LValue,
Expand Down
19 changes: 17 additions & 2 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,24 @@ static std::string GetParamSignature(const clang::Decl *decl) {
}

static std::string GetLexicalSpecializationID(const clang::Decl *decl) {
std::string id;
if (const auto *spec =
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(
decl->getLexicalDeclContext());
spec && decl->getLexicalDeclContext() != decl->getDeclContext()) {
return Mapper::ToString(Mapper::GetTypeForDecl(spec));
id += Mapper::ToString(Mapper::GetTypeForDecl(spec));
}
return {};
for (const auto *dc = decl->getDeclContext(); dc; dc = dc->getParent()) {
if (const auto *spec =
clang::dyn_cast<clang::ClassTemplateSpecializationDecl>(dc)) {
id += Mapper::ToString(Mapper::GetTypeForDecl(spec));
}
if (const auto *fn = clang::dyn_cast<clang::FunctionDecl>(dc);
fn && fn->getTemplateSpecializationArgs()) {
id += clang::ASTNameGenerator(fn->getASTContext()).getName(fn);
}
}
return id;
}

std::string GetID(const clang::Decl *decl) {
Expand Down Expand Up @@ -623,6 +634,10 @@ std::string GetNamedDeclAsString(const clang::NamedDecl *decl) {
var && (var->isFileVarDecl() || var->isStaticLocal())) {
id = GetDeclId(var->getCanonicalDecl(),
var->getFormalLinkage() != clang::Linkage::External);
} else if (auto *tag = clang::dyn_cast<clang::TagDecl>(decl);
tag && tag->getDeclContext()->isFunctionOrMethod()) {
id =
type_mapping.try_emplace(GetID(tag), type_mapping.size()).first->second;
}
if (id) {
name += '_';
Expand Down
6 changes: 6 additions & 0 deletions cpp2rust/converter/mapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@ std::string ToString(clang::QualType qual_type, ScalarSugar sugar) {
return ToString(clang::cast<clang::NamedDecl>(tag));
}

if (auto *tag = qual_type->getAsTagDecl();
tag && tag->getIdentifier() &&
tag->getDeclContext()->isFunctionOrMethod()) {
return GetNamedDeclAsString(tag);
}

if (auto renamed = DisambiguateAnonymousTag(qual_type->getAsTagDecl());
!renamed.empty()) {
return renamed;
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/local_record_template.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <assert.h>

template <typename T> int get(T t) { return t.x; }

namespace ns {
template <typename T> int twice(T t) { return t.x * 2; }
} // namespace ns

template <typename T> int wrap(T v) {
struct Local {
T x;
};
Local l{v};
return get(l);
}

int other() {
struct Local {
long x;
long y;
};
Local l{3, 4};
return get(l) + (int)l.y;
}

int main() {
struct Local {
int x;
};
Local l{7};
assert(get(l) == 7);
assert(ns::twice(l) == 14);
assert(other() == 7);
assert(wrap(5) == 5);
assert(wrap(6L) == 6);
return 0;
}
60 changes: 30 additions & 30 deletions tests/unit/out/refcount/anonymous-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,36 +321,6 @@ fn main_0() -> i32 {
.borrow())
== 11)
);
#[derive(Default)]
pub struct anon_6 {
pub x: Value<i32>,
pub z: Value<i32>,
}
impl Clone for anon_6 {
fn clone(&self) -> Self {
let __this: Value<anon_6> = Rc::new(RefCell::new(Self {
x: Rc::new(RefCell::new((*self.x.borrow()))),
z: Rc::new(RefCell::new((*self.z.borrow()))),
}));
let this: Ptr<anon_6> = __this.as_pointer();
Rc::try_unwrap(__this).ok().unwrap().into_inner()
}
}
impl ByteRepr for anon_6 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
(*self.z.borrow()).to_bytes(&mut buf[4..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
z: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
}
}
};
let s: Value<anon_6> = Rc::new(RefCell::new(<anon_6>::default()));
(*(*s.borrow()).x.borrow_mut()) = 1;
(*(*s.borrow()).z.borrow_mut()) = 2;
Expand All @@ -368,3 +338,33 @@ fn main_0() -> i32 {
);
return 0;
}
#[derive(Default)]
pub struct anon_6 {
pub x: Value<i32>,
pub z: Value<i32>,
}
impl Clone for anon_6 {
fn clone(&self) -> Self {
let __this: Value<anon_6> = Rc::new(RefCell::new(Self {
x: Rc::new(RefCell::new((*self.x.borrow()))),
z: Rc::new(RefCell::new((*self.z.borrow()))),
}));
let this: Ptr<anon_6> = __this.as_pointer();
Rc::try_unwrap(__this).ok().unwrap().into_inner()
}
}
impl ByteRepr for anon_6 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
(*self.z.borrow()).to_bytes(&mut buf[4..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
z: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
}
}
}
56 changes: 28 additions & 28 deletions tests/unit/out/refcount/anonymous-struct_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,34 +290,6 @@ fn main_0() -> i32 {
== 11) as i32)
!= 0)
);
#[derive(Default)]
pub struct anon_6 {
pub x: Value<i32>,
pub z: Value<i32>,
}
impl Clone for anon_6 {
fn clone(&self) -> Self {
Self {
x: Rc::new(RefCell::new((*self.x.borrow()).clone())),
z: Rc::new(RefCell::new((*self.z.borrow()).clone())),
}
}
}
impl ByteRepr for anon_6 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
(*self.z.borrow()).to_bytes(&mut buf[4..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
z: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
}
}
};
let s: Value<anon_6> = <Value<anon_6>>::default();
(*(*s.borrow()).x.borrow_mut()) = 1;
(*(*s.borrow()).z.borrow_mut()) = 2;
Expand All @@ -335,3 +307,31 @@ fn main_0() -> i32 {
);
return 0;
}
#[derive(Default)]
pub struct anon_6 {
pub x: Value<i32>,
pub z: Value<i32>,
}
impl Clone for anon_6 {
fn clone(&self) -> Self {
Self {
x: Rc::new(RefCell::new((*self.x.borrow()).clone())),
z: Rc::new(RefCell::new((*self.z.borrow()).clone())),
}
}
}
impl ByteRepr for anon_6 {
fn byte_size() -> usize {
8
}
fn to_bytes(&self, buf: &mut [u8]) {
(*self.x.borrow()).to_bytes(&mut buf[0..4]);
(*self.z.borrow()).to_bytes(&mut buf[4..8]);
}
fn from_bytes(buf: &[u8]) -> Self {
Self {
x: Rc::new(RefCell::new(<i32>::from_bytes(&buf[0..4]))),
z: Rc::new(RefCell::new(<i32>::from_bytes(&buf[4..8]))),
}
}
}
6 changes: 3 additions & 3 deletions tests/unit/out/refcount/anonymous_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
pub type anon_3 = u32;
pub const anon_3_THIRD_A: anon_3 = 0;
pub const anon_3_THIRD_B: anon_3 = 1;;
assert!(((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32)));
assert!(((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32)));
assert!(((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32)));
Expand All @@ -95,3 +92,6 @@ fn main_0() -> i32 {
assert!((((*(*w.borrow()).field.borrow()) as i32) == (anon_2_FIELD_B as i32)));
return 0;
}
pub type anon_3 = u32;
pub const anon_3_THIRD_A: anon_3 = 0;
pub const anon_3_THIRD_B: anon_3 = 1;
6 changes: 3 additions & 3 deletions tests/unit/out/refcount/anonymous_enum_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ pub fn main() {
std::process::exit(main_0());
}
fn main_0() -> i32 {
pub type anon_3 = u32;
pub const anon_3_THIRD_A: anon_3 = 0;
pub const anon_3_THIRD_B: anon_3 = 1;;
assert!(((((anon_0_FIRST_A as i32) != (anon_0_FIRST_B as i32)) as i32) != 0));
assert!(((((anon_1_SECOND_A as i32) != (anon_1_SECOND_B as i32)) as i32) != 0));
assert!(((((anon_3_THIRD_A as i32) != (anon_3_THIRD_B as i32)) as i32) != 0));
Expand All @@ -97,3 +94,6 @@ fn main_0() -> i32 {
);
return 0;
}
pub type anon_3 = u32;
pub const anon_3_THIRD_A: anon_3 = 0;
pub const anon_3_THIRD_B: anon_3 = 1;
Loading
Loading