Skip to content

Commit 68e0293

Browse files
committed
ast-builder: Add support for raw borrows
1 parent 41a4826 commit 68e0293

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3032,7 +3032,7 @@ impl<'c> Translation<'c> {
30323032
}
30333033
}
30343034
_ => {
3035-
let addr_lhs = mk().set_mutbl(mutbl).addr_of_expr(lhs);
3035+
let addr_lhs = mk().set_mutbl(mutbl).borrow_expr(lhs);
30363036

30373037
let lhs_type = self.convert_type(lhs_type.ctype)?;
30383038
let ty = mk().set_mutbl(mutbl).ptr_ty(lhs_type);

c2rust-transpile/src/translator/operators.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,7 +963,7 @@ impl<'c> Translation<'c> {
963963
// static variable initializers aren't able to use &mut,
964964
// so we work around that by using & and an extra cast
965965
// through & to *const to *mut
966-
addr_of_arg = mk().addr_of_expr(a);
966+
addr_of_arg = mk().borrow_expr(a);
967967
if let Mutability::Mutable = mutbl {
968968
let mut qtype = pointee_ty;
969969
qtype.qualifiers.is_const = true;
@@ -975,7 +975,7 @@ impl<'c> Translation<'c> {
975975
}
976976
} else {
977977
// Normal case is allowed to use &mut if needed
978-
addr_of_arg = mk().set_mutbl(mutbl).addr_of_expr(a);
978+
addr_of_arg = mk().set_mutbl(mutbl).borrow_expr(a);
979979

980980
// Avoid unnecessary reference to pointer decay in fn call args:
981981
if ctx.decay_ref.is_no() {

0 commit comments

Comments
 (0)