Skip to content

Commit b0748d9

Browse files
committed
Delete StripFunctionPointerDecay
1 parent 77430e4 commit b0748d9

6 files changed

Lines changed: 27 additions & 23 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,8 +1455,8 @@ void Converter::ConvertGenericCallExpr(clang::CallExpr *expr) {
14551455
if (proto && !function) {
14561456
EmitFnPtrCall(callee);
14571457
} else {
1458-
PushExprKind push(*this, ExprKind::RValue);
1459-
Convert(StripFunctionPointerDecay(callee));
1458+
PushExprKind push(*this, ExprKind::Callee);
1459+
Convert(callee);
14601460
}
14611461
StrCat(token::kOpenParen);
14621462
for (unsigned i = 0; i < num_named_params && i < num_args; ++i) {
@@ -1674,8 +1674,12 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
16741674
}
16751675
case clang::CastKind::CK_FunctionToPointerDecay:
16761676
case clang::CastKind::CK_BuiltinFnToFnPtr: {
1677-
PushExprKind push(*this, ExprKind::AddrOf);
1678-
Convert(sub_expr);
1677+
if (isCallee()) {
1678+
Convert(sub_expr);
1679+
} else {
1680+
PushExprKind push(*this, ExprKind::AddrOf);
1681+
Convert(sub_expr);
1682+
}
16791683
break;
16801684
}
16811685
case clang::CastKind::CK_ConstructorConversion:
@@ -3234,7 +3238,12 @@ void Converter::PlaceholderCtx::dump() const {
32343238

32353239
std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
32363240
const PlaceholderCtx &ph_ctx) {
3237-
arg = StripFunctionPointerDecay(arg);
3241+
if (arg->getType()->isFunctionPointerType()) {
3242+
PushExprKind push(*this, ExprKind::Callee);
3243+
Buffer buf(*this);
3244+
Convert(arg);
3245+
return std::move(buf).str();
3246+
}
32383247

32393248
if (ph_ctx.needs_materialization()) {
32403249
auto materialized = ph_ctx.materialize_ctx->GetOrMaterialize(
@@ -3383,6 +3392,10 @@ bool Converter::isVoid() const {
33833392
return curr_expr_kind_.empty() || curr_expr_kind_.back() == ExprKind::Void;
33843393
}
33853394

3395+
bool Converter::isCallee() const {
3396+
return !curr_expr_kind_.empty() && curr_expr_kind_.back() == ExprKind::Callee;
3397+
}
3398+
33863399
void Converter::SetFresh() {
33873400
switch (computed_expr_type_) {
33883401
case ComputedExprType::Value:

cpp2rust/converter/converter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
477477
static std::unordered_set<std::string> abstract_structs_;
478478

479479
enum class ExprKind : uint8_t {
480+
Callee,
480481
LValue,
481482
RValue,
482483
XValue,
@@ -487,6 +488,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
487488

488489
inline std::string expr_kind_to_string(ExprKind kind) {
489490
switch (kind) {
491+
case ExprKind::Callee:
492+
return "Callee";
490493
case ExprKind::LValue:
491494
return "LValue";
492495
case ExprKind::RValue:
@@ -510,6 +513,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
510513
bool isAddrOf() const;
511514
bool isObject() const;
512515
bool isVoid() const;
516+
bool isCallee() const;
513517

514518
void dump_expr_kinds();
515519

cpp2rust/converter/converter_lib.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,6 @@ clang::QualType GetReturnTypeOfFunction(const clang::CallExpr *expr) {
338338
return {};
339339
}
340340

341-
clang::Expr *StripFunctionPointerDecay(clang::Expr *expr) {
342-
if (auto *ice = clang::dyn_cast<clang::ImplicitCastExpr>(expr)) {
343-
auto ck = ice->getCastKind();
344-
if (ck == clang::CK_FunctionToPointerDecay ||
345-
ck == clang::CK_BuiltinFnToFnPtr) {
346-
return ice->getSubExpr();
347-
}
348-
}
349-
return expr;
350-
}
351-
352341
std::string GetOverloadedOperator(const clang::FunctionDecl *decl) {
353342
switch (decl->getOverloadedOperator()) {
354343
case clang::OO_Less:

cpp2rust/converter/converter_lib.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ template <class T> llvm::SmallString<16> GetNumAsString(const T &num) {
9191

9292
clang::QualType GetReturnTypeOfFunction(const clang::CallExpr *expr);
9393

94-
clang::Expr *StripFunctionPointerDecay(clang::Expr *expr);
95-
9694
std::string GetOverloadedOperator(const clang::FunctionDecl *decl);
9795

9896
bool IsOverloadedComparisonOperator(const clang::CXXMethodDecl *decl);

tests/unit/out/refcount/lambda_capture_pass.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn apply_0(fn_: impl Fn(i32) -> i32, x: i32) -> i32 {
1212
let x: Value<i32> = Rc::new(RefCell::new(x));
1313
return ({
1414
let _x: i32 = (*x.borrow());
15-
(*fn_.borrow())(_x)
15+
(*fn_.borrow_mut())(_x)
1616
})
1717
.clone();
1818
}
@@ -21,7 +21,7 @@ pub fn apply_1(fn_: impl Fn(i32) -> i32, x: i32) -> i32 {
2121
let x: Value<i32> = Rc::new(RefCell::new(x));
2222
return ({
2323
let _x: i32 = (*x.borrow());
24-
(*fn_.borrow())(_x)
24+
(*fn_.borrow_mut())(_x)
2525
})
2626
.clone();
2727
}

tests/unit/out/refcount/lambda_nested.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,22 @@ fn main_0() -> i32 {
2323
));
2424
return ({
2525
let _z: i32 = 1;
26-
(*inner.borrow())(_z)
26+
(*inner.borrow_mut())(_z)
2727
})
2828
.clone();
2929
}),
3030
));
3131
assert!(
3232
(({
3333
let _y: i32 = 20;
34-
(*outer.borrow())(_y)
34+
(*outer.borrow_mut())(_y)
3535
}) == 31)
3636
);
3737
(*x.borrow_mut()) = 100;
3838
assert!(
3939
(({
4040
let _y: i32 = 20;
41-
(*outer.borrow())(_y)
41+
(*outer.borrow_mut())(_y)
4242
}) == 121)
4343
);
4444
return 0;

0 commit comments

Comments
 (0)