Skip to content

Commit b3a4d03

Browse files
committed
Use ConvertPointeeType instead of Convert(t->getPointeeType())
1 parent 60deba8 commit b3a4d03

7 files changed

Lines changed: 88 additions & 9 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ bool Converter::ConvertMappedType(clang::QualType qual_type) {
8686
return true;
8787
}
8888

89+
std::string Converter::ConvertPointeeType(clang::QualType ptr_type) {
90+
assert(!ptr_type.isNull() && ptr_type->isPointerType());
91+
auto pointee = ptr_type->getPointeeType();
92+
if (!pointee->isRecordType()) {
93+
return ToString(pointee);
94+
}
95+
96+
auto str = ToString(ptr_type);
97+
Unwrap(str, "*mut ", "");
98+
Unwrap(str, "*const ", "");
99+
return str;
100+
}
101+
89102
bool Converter::VisitBuiltinType(clang::BuiltinType *type) {
90103
switch (type->getKind()) {
91104
case clang::BuiltinType::Bool:
@@ -216,9 +229,8 @@ bool Converter::VisitIncompleteArrayType(clang::IncompleteArrayType *type) {
216229
}
217230

218231
bool Converter::VisitLValueReferenceType(clang::LValueReferenceType *type) {
219-
StrCat(token::kStar);
220232
auto pointee_type = type->getPointeeType();
221-
StrCat(pointee_type.isConstQualified() ? keyword::kConst : keyword_mut_);
233+
StrCat(pointee_type.isConstQualified() ? "*const" : "*mut");
222234
return Convert(pointee_type);
223235
}
224236

@@ -250,9 +262,8 @@ bool Converter::VisitPointerType(clang::PointerType *type) {
250262
return false;
251263
}
252264

253-
StrCat(token::kStar);
254265
auto pointee_type = type->getPointeeType();
255-
StrCat(pointee_type.isConstQualified() ? keyword::kConst : keyword_mut_);
266+
StrCat(pointee_type.isConstQualified() ? "*const" : "*mut");
256267
if (pointee_type->isRecordType() &&
257268
abstract_structs_.contains(GetID(pointee_type->getAsRecordDecl()))) {
258269
StrCat(keyword::kDyn);
@@ -1850,7 +1861,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
18501861
if (type->isVoidPointerType()) {
18511862
StrCat(keyword::kAs,
18521863
type->getPointeeType().isConstQualified() ? "*const" : "*mut");
1853-
Convert(sub_expr->getType()->getPointeeType());
1864+
StrCat(ConvertPointeeType(sub_expr->getType()));
18541865
}
18551866
ConvertCast(type);
18561867
break;
@@ -2109,7 +2120,7 @@ void Converter::ConvertBinaryOperator(clang::BinaryOperator *expr) {
21092120
StrCat(keyword::kAs, "usize");
21102121
}
21112122
StrCat(token::kDiv);
2112-
auto pointee_type_as_string = ToString(lhs_type->getPointeeType());
2123+
auto pointee_type_as_string = ConvertPointeeType(lhs_type);
21132124
auto size_of_as_string =
21142125
std::format("::std::mem::size_of::<{}>()", pointee_type_as_string);
21152126
StrCat(size_of_as_string);

cpp2rust/converter/converter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
398398
virtual bool Convert(clang::QualType qual_type);
399399
virtual bool ConvertMappedType(clang::QualType qual_type);
400400

401+
virtual std::string ConvertPointeeType(clang::QualType ptr_type);
402+
401403
virtual bool Convert(clang::Decl *decl);
402404
virtual bool Convert(clang::Stmt *stmt);
403405
virtual bool Convert(clang::Expr *expr);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,9 +1080,9 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
10801080

10811081
if (pointee_type && abstract_structs_.contains(GetID(pointee_type))) {
10821082
PushConversionKind push(*this, ConversionKind::Unboxed);
1083-
StrCat(std::format("({}.to_strong() as Value<dyn {}>).as_pointer_dyn()",
1083+
StrCat(std::format("({}.to_strong() as Value<{}>).as_pointer_dyn()",
10841084
ToString(sub_expr->IgnoreCasts()),
1085-
ToString(expr->getType()->getPointeeType())));
1085+
ConvertPointeeType(expr->getType())));
10861086
computed_expr_type_ = ComputedExprType::FreshPointer;
10871087
return false;
10881088
}
@@ -1223,6 +1223,12 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
12231223
StrCat(std::format(".cast::<{}>().expect(\"ub:wrong type\")",
12241224
ConvertPointeeType(expr->getType())));
12251225
return false;
1226+
} else if (expr->getType()->isVoidPointerType() &&
1227+
expr->getSubExpr()->getType()->isPointerType()) {
1228+
StrCat(
1229+
std::format("{}.to_any()", ConvertFreshPointer(expr->getSubExpr())));
1230+
computed_expr_type_ = ComputedExprType::FreshPointer;
1231+
return false;
12261232
} else if (expr->getSubExpr()->getType()->isPointerType() &&
12271233
!expr->getSubExpr()->isNullPointerConstant(
12281234
ctx_, clang::Expr::NPC_ValueDependentIsNull)) {
@@ -2321,6 +2327,7 @@ std::string ConverterRefCount::ConvertPointeeType(clang::QualType ptr_type) {
23212327
// not have a translation rule. Hence ToString(ptr_type->getPointeeType()) is
23222328
// not enough
23232329
auto str = ToString(ptr_type);
2330+
Unwrap(str, "PtrDyn<", ">");
23242331
Unwrap(str, "Ptr<", ">");
23252332
return str;
23262333
}

cpp2rust/converter/models/converter_refcount.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ class ConverterRefCount final : public Converter {
216216
std::string ConvertFreshPointer(clang::Expr *expr) override;
217217

218218
std::string ConvertPtrType(clang::QualType type);
219-
std::string ConvertPointeeType(clang::QualType ptr_type);
219+
std::string ConvertPointeeType(clang::QualType ptr_type) override;
220220

221221
/// The kind of conversion that should be performed.
222222
enum class ConversionKind : uint8_t {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
let fp: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new((libcc2rs::cout()).clone()));
14+
let p: Value<AnyPtr> = Rc::new(RefCell::new((*fp.borrow()).clone().to_any()));
15+
let fp2: Value<Ptr<::std::fs::File>> = Rc::new(RefCell::new(
16+
((*p.borrow())
17+
.cast::<::std::fs::File>()
18+
.expect("ub:wrong type"))
19+
.clone(),
20+
));
21+
assert!(
22+
((({
23+
let _lhs = (*fp.borrow()).clone();
24+
_lhs == (*fp2.borrow()).clone()
25+
}) as i32)
26+
!= 0)
27+
);
28+
return 0;
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
let mut fp: *mut ::libc::FILE = libcc2rs::stdout_unsafe();
16+
let mut p: *mut ::libc::c_void = (fp as *mut ::libc::c_void);
17+
let mut fp2: *mut ::libc::FILE = (p as *mut ::libc::FILE);
18+
assert!(((((fp) == (fp2)) as i32) != 0));
19+
return 0;
20+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <assert.h>
2+
#include <stdio.h>
3+
4+
int main(void) {
5+
FILE *fp = stdout;
6+
void *p = (void *)fp;
7+
FILE *fp2 = (FILE *)p;
8+
assert(fp == fp2);
9+
return 0;
10+
}

0 commit comments

Comments
 (0)