Skip to content

Commit 1e41cef

Browse files
committed
ast-builder: Add support for raw borrows
1 parent 5a55ba9 commit 1e41cef

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

c2rust-ast-builder/src/builder.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use syn::{__private::ToTokens, punctuated::Punctuated, *};
1010

1111
pub mod properties {
1212
use proc_macro2::Span;
13-
use syn::{StaticMutability, Token};
13+
use syn::{PointerMutability, StaticMutability, Token};
1414

1515
pub trait ToToken {
1616
type Token;
@@ -34,6 +34,13 @@ pub mod properties {
3434
}
3535

3636
impl Mutability {
37+
pub fn to_pointer_mutability(&self, span: Span) -> PointerMutability {
38+
match self {
39+
Mutability::Mutable => PointerMutability::Mut(Token![mut](span)),
40+
Mutability::Immutable => PointerMutability::Const(Token![const](span)),
41+
}
42+
}
43+
3744
pub fn to_static_mutability(&self, span: Span) -> StaticMutability {
3845
match self {
3946
Mutability::Mutable => StaticMutability::Mut(Token![mut](span)),
@@ -826,7 +833,7 @@ impl Builder {
826833
self.path_expr(vec![name])
827834
}
828835

829-
pub fn addr_of_expr(self, e: Box<Expr>) -> Box<Expr> {
836+
pub fn borrow_expr(self, e: Box<Expr>) -> Box<Expr> {
830837
Box::new(parenthesize_if_necessary(Expr::Reference(ExprReference {
831838
attrs: self.attrs,
832839
and_token: Token![&](self.span),
@@ -835,6 +842,16 @@ impl Builder {
835842
})))
836843
}
837844

845+
pub fn raw_borrow_expr(self, e: Box<Expr>) -> Box<Expr> {
846+
Box::new(parenthesize_if_necessary(Expr::RawAddr(ExprRawAddr {
847+
attrs: self.attrs,
848+
and_token: Token![&](self.span),
849+
raw: Token![raw](self.span),
850+
mutability: self.mutbl.to_pointer_mutability(self.span),
851+
expr: e,
852+
})))
853+
}
854+
838855
pub fn mac_expr(self, mac: Macro) -> Box<Expr> {
839856
Box::new(Expr::Macro(ExprMacro {
840857
attrs: self.attrs,

c2rust-transpile/src/rust_ast/set_span.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ impl SetSpan for Expr {
140140
RangeLimits::Closed(mut r) => r.spans[0] = s,
141141
RangeLimits::HalfOpen(mut r) => r.spans[0] = s,
142142
},
143+
Expr::RawAddr(e) => e.and_token.span = s,
143144
Expr::Reference(e) => e.and_token.span = s,
144145
Expr::Return(e) => e.return_token.span = s,
145146
Expr::Try(e) => e.question_token.span = s,

c2rust-transpile/src/translator/assembly.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ impl<'c> Translation<'c> {
883883
// c2rust-ast-exporter added it (there's no gcc equivalent);
884884
// in this case, we need to do what clang does and pass in
885885
// the operand by-address instead of by-value
886-
out_expr = mk().mutbl().addr_of_expr(out_expr);
886+
out_expr = mk().mutbl().borrow_expr(out_expr);
887887
}
888888

889889
if let Some(_tied_operand) = tied_operands.get(&(output_idx, true)) {
@@ -900,7 +900,7 @@ impl<'c> Translation<'c> {
900900
let output_local = mk().local(
901901
mk().ident_pat(&output_name),
902902
None,
903-
Some(mk().mutbl().addr_of_expr(out_expr)),
903+
Some(mk().mutbl().borrow_expr(out_expr)),
904904
);
905905
stmts.push(mk().local_stmt(Box::new(output_local)));
906906

@@ -924,7 +924,7 @@ impl<'c> Translation<'c> {
924924
let mut in_expr = in_expr.into_value();
925925

926926
if operand.mem_only {
927-
in_expr = mk().addr_of_expr(in_expr);
927+
in_expr = mk().borrow_expr(in_expr);
928928
}
929929
if let Some(tied_operand) = tied_operands.get(&(input_idx, false)) {
930930
self.use_crate(ExternCrate::C2RustAsmCasts);

c2rust-transpile/src/translator/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,7 +3126,7 @@ impl<'c> Translation<'c> {
31263126
}
31273127
}
31283128
_ => {
3129-
let addr_lhs = mk().set_mutbl(mutbl).addr_of_expr(lhs);
3129+
let addr_lhs = mk().set_mutbl(mutbl).borrow_expr(lhs);
31303130

31313131
let lhs_type = self.convert_type(lhs_type.ctype)?;
31323132
let ty = mk().set_mutbl(mutbl).ptr_ty(lhs_type);
@@ -4945,7 +4945,7 @@ impl<'c> Translation<'c> {
49454945
needs_cast = true;
49464946
}
49474947
} else {
4948-
val = val.map(|val| mk().set_mutbl(mutbl).addr_of_expr(val));
4948+
val = val.map(|val| mk().set_mutbl(mutbl).borrow_expr(val));
49494949

49504950
// Add an intermediate reference-to-pointer cast if the context needs
49514951
// reference-to-pointer decay, or if another cast follows.

0 commit comments

Comments
 (0)