Skip to content

Commit d91a29e

Browse files
committed
transpile: Remove override_ty param from convert_expr
1 parent 05a6dec commit d91a29e

14 files changed

Lines changed: 129 additions & 169 deletions

File tree

c2rust-transpile/src/cfg/mod.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,7 @@ impl Cfg<Label, StmtOrDecl> {
616616
wip.body.push(StmtOrDecl::Stmt(mk().semi_stmt(ret_expr)));
617617
}
618618
ImplicitReturnType::StmtExpr(ctx, expr_id, brk_label) => {
619-
let (stmts, val) = translator
620-
.convert_expr(ctx, expr_id, None)?
621-
.discard_unsafe();
619+
let (stmts, val) = translator.convert_expr(ctx, expr_id)?.discard_unsafe();
622620

623621
wip.body.extend(stmts.into_iter().map(StmtOrDecl::Stmt));
624622
wip.body.push(StmtOrDecl::Stmt(mk().semi_stmt(
@@ -1420,7 +1418,7 @@ impl CfgBuilder {
14201418
}
14211419

14221420
CStmtKind::Return(expr) => {
1423-
let val = match expr.map(|i| translator.convert_expr(ctx.used(), i, ret_ty)) {
1421+
let val = match expr.map(|i| translator.convert_expr(ctx.used(), i)) {
14241422
Some(r) => Some(r?),
14251423
None => None,
14261424
};
@@ -1684,9 +1682,8 @@ impl CfgBuilder {
16841682
match increment {
16851683
None => slf.add_block(incr_entry, BasicBlock::new_jump(cond_entry)),
16861684
Some(incr) => {
1687-
let incr_stmts = translator
1688-
.convert_expr(ctx.unused(), incr, None)?
1689-
.into_stmts();
1685+
let incr_stmts =
1686+
translator.convert_expr(ctx.unused(), incr)?.into_stmts();
16901687
let mut incr_wip = slf.new_wip_block(incr_entry);
16911688
incr_wip.extend(incr_stmts);
16921689
slf.add_wip_block(incr_wip, Jump(cond_entry));
@@ -1797,11 +1794,7 @@ impl CfgBuilder {
17971794
match blk_or_wip {
17981795
Ok(blk) => Ok(blk),
17991796
Err(mut wip) => {
1800-
wip.extend(
1801-
translator
1802-
.convert_expr(ctx.unused(), expr, None)?
1803-
.into_stmts(),
1804-
);
1797+
wip.extend(translator.convert_expr(ctx.unused(), expr)?.into_stmts());
18051798

18061799
// If we can tell the expression is going to diverge, there is no falling through to
18071800
// the next block.
@@ -1861,7 +1854,7 @@ impl CfgBuilder {
18611854
let branch = match translator.ast_context.index(resolved).kind {
18621855
CExprKind::Literal(..) | CExprKind::ConstantExpr(_, _, Some(_)) => {
18631856
match translator
1864-
.convert_expr(ctx.used(), resolved, None)?
1857+
.convert_expr(ctx.used(), resolved)?
18651858
.to_pure_expr()
18661859
{
18671860
Some(expr) => match *expr {
@@ -1939,7 +1932,7 @@ impl CfgBuilder {
19391932

19401933
// Convert the condition
19411934
let (stmts, val) = translator
1942-
.convert_expr(ctx.used(), scrutinee, None)?
1935+
.convert_expr(ctx.used(), scrutinee)?
19431936
.discard_unsafe();
19441937
wip.extend(stmts);
19451938

c2rust-transpile/src/translator/assembly.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ impl<'c> Translation<'c> {
874874

875875
// First, convert output expr if present
876876
let out_expr = if let Some((output_idx, out_expr)) = operand.out_expr {
877-
let mut out_expr = self.convert_expr(ctx.used(), out_expr, None)?;
877+
let mut out_expr = self.convert_expr(ctx.used(), out_expr)?;
878878
stmts.append(out_expr.stmts_mut());
879879
let mut out_expr = out_expr.into_value();
880880

@@ -921,7 +921,7 @@ impl<'c> Translation<'c> {
921921

922922
// Then, handle input expr if present
923923
let in_expr = if let Some((input_idx, in_expr)) = operand.in_expr {
924-
let mut in_expr = self.convert_expr(ctx.used(), in_expr, None)?;
924+
let mut in_expr = self.convert_expr(ctx.used(), in_expr)?;
925925
stmts.append(in_expr.stmts_mut());
926926
let mut in_expr = in_expr.into_value();
927927

c2rust-transpile/src/translator/atomics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,14 @@ impl<'c> Translation<'c> {
144144
val2_id: Option<CExprId>,
145145
weak_id: Option<CExprId>,
146146
) -> TranslationResult<WithStmts<Box<Expr>>> {
147-
let ptr = self.convert_expr(ctx.used(), ptr_id, None)?;
147+
let ptr = self.convert_expr(ctx.used(), ptr_id)?;
148148
let order = self.convert_memordering(order_id);
149149
let val1 = val1_id
150-
.map(|x| self.convert_expr(ctx.used(), x, None))
150+
.map(|x| self.convert_expr(ctx.used(), x))
151151
.transpose()?;
152152
let order_fail = order_fail_id.and_then(|x| self.convert_memordering(x));
153153
let val2 = val2_id
154-
.map(|x| self.convert_expr(ctx.used(), x, None))
154+
.map(|x| self.convert_expr(ctx.used(), x))
155155
.transpose()?;
156156
let weak = weak_id.and_then(|x| self.convert_constant_bool(x));
157157

@@ -237,7 +237,7 @@ impl<'c> Translation<'c> {
237237
if name == "__atomic_exchange" {
238238
// LLVM stores the ret pointer in the order_fail slot
239239
order_fail_id
240-
.map(|x| self.convert_expr(ctx.used(), x, None))
240+
.map(|x| self.convert_expr(ctx.used(), x))
241241
.transpose()?
242242
.expect("__atomic_exchange must have a ret pointer argument")
243243
.and_then_try(|ret| {

c2rust-transpile/src/translator/builtins.rs

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl<'c> Translation<'c> {
3232
rotate_method_name: &'static str,
3333
) -> TranslationResult<WithStmts<Box<Expr>>> {
3434
// Emit `arg0.{method_name}(arg1)`
35-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
36-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
35+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
36+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
3737
arg0.zip(arg1).and_then_try(|(arg0, arg1)| {
3838
let arg1 = mk().cast_expr(arg1, mk().path_ty(vec!["u32"]));
3939
let method_call_expr = mk().method_call_expr(arg0, rotate_method_name, vec![arg1]);
@@ -115,15 +115,15 @@ impl<'c> Translation<'c> {
115115
"__builtin_signbit" | "__builtin_signbitf" | "__builtin_signbitl" => {
116116
self.import_num_traits(args[0])?;
117117

118-
let val = self.convert_expr(ctx.used(), args[0], None)?;
118+
let val = self.convert_expr(ctx.used(), args[0])?;
119119
Ok(val.map(|v| {
120120
let val = mk().method_call_expr(v, "is_sign_negative", vec![]);
121121

122122
mk().cast_expr(val, mk().abs_path_ty(vec!["core", "ffi", "c_int"]))
123123
}))
124124
}
125125
"__builtin_ffs" | "__builtin_ffsl" | "__builtin_ffsll" => {
126-
let val = self.convert_expr(ctx.used(), args[0], None)?;
126+
let val = self.convert_expr(ctx.used(), args[0])?;
127127

128128
Ok(val.map(|x| {
129129
let add = BinOp::Add(Default::default());
@@ -140,33 +140,33 @@ impl<'c> Translation<'c> {
140140
}))
141141
}
142142
"__builtin_clz" | "__builtin_clzl" | "__builtin_clzll" => {
143-
let val = self.convert_expr(ctx.used(), args[0], None)?;
143+
let val = self.convert_expr(ctx.used(), args[0])?;
144144
Ok(val.map(|x| {
145145
let zeros = mk().method_call_expr(x, "leading_zeros", vec![]);
146146
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
147147
}))
148148
}
149149
"__builtin_ctz" | "__builtin_ctzl" | "__builtin_ctzll" => {
150-
let val = self.convert_expr(ctx.used(), args[0], None)?;
150+
let val = self.convert_expr(ctx.used(), args[0])?;
151151
Ok(val.map(|x| {
152152
let zeros = mk().method_call_expr(x, "trailing_zeros", vec![]);
153153
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
154154
}))
155155
}
156156
"__builtin_bswap16" | "__builtin_bswap32" | "__builtin_bswap64" => {
157-
let val = self.convert_expr(ctx.used(), args[0], None)?;
157+
let val = self.convert_expr(ctx.used(), args[0])?;
158158
Ok(val.map(|x| mk().method_call_expr(x, "swap_bytes", vec![])))
159159
}
160160
"__builtin_fabs" | "__builtin_fabsf" | "__builtin_fabsl" => {
161161
self.import_num_traits(args[0])?;
162162

163-
let val = self.convert_expr(ctx.used(), args[0], None)?;
163+
let val = self.convert_expr(ctx.used(), args[0])?;
164164
Ok(val.map(|x| mk().method_call_expr(x, "abs", vec![])))
165165
}
166166
"__builtin_isfinite" | "__builtin_isnan" => {
167167
self.import_num_traits(args[0])?;
168168

169-
let val = self.convert_expr(ctx.used(), args[0], None)?;
169+
let val = self.convert_expr(ctx.used(), args[0])?;
170170

171171
let seg = match builtin_name {
172172
"__builtin_isfinite" => "is_finite",
@@ -182,7 +182,7 @@ impl<'c> Translation<'c> {
182182
self.import_num_traits(args[0])?;
183183

184184
// isinf_sign(x) -> fabs(x) == infinity ? (signbit(x) ? -1 : 1) : 0
185-
let val = self.convert_expr(ctx.used(), args[0], None)?;
185+
let val = self.convert_expr(ctx.used(), args[0])?;
186186
Ok(val.map(|x| {
187187
let inner_cond = mk().method_call_expr(x.clone(), "is_sign_positive", vec![]);
188188
let one = mk().lit_expr(mk().int_lit(1, ""));
@@ -201,18 +201,18 @@ impl<'c> Translation<'c> {
201201
// https://github.com/llvm-mirror/llvm/blob/master/lib/CodeGen/IntrinsicLowering.cpp#L470
202202
Ok(WithStmts::new_val(mk().lit_expr(mk().int_lit(1, "i32"))))
203203
}
204-
"__builtin_expect" => self.convert_expr(ctx.used(), args[0], None),
204+
"__builtin_expect" => self.convert_expr(ctx.used(), args[0]),
205205

206206
"__builtin_popcount" | "__builtin_popcountl" | "__builtin_popcountll" => {
207-
let val = self.convert_expr(ctx.used(), args[0], None)?;
207+
let val = self.convert_expr(ctx.used(), args[0])?;
208208
Ok(val.map(|x| {
209209
let zeros = mk().method_call_expr(x, "count_ones", vec![]);
210210
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
211211
}))
212212
}
213213
"__builtin_bzero" => {
214-
let ptr_stmts = self.convert_expr(ctx.used(), args[0], None)?;
215-
let n_stmts = self.convert_expr(ctx.used(), args[1], None)?;
214+
let ptr_stmts = self.convert_expr(ctx.used(), args[0])?;
215+
let n_stmts = self.convert_expr(ctx.used(), args[1])?;
216216
let write_bytes = mk().abs_path_expr(vec!["core", "ptr", "write_bytes"]);
217217
let zero = mk().lit_expr(mk().int_lit(0, "u8"));
218218
Ok(ptr_stmts.and_then(|ptr| {
@@ -223,7 +223,7 @@ impl<'c> Translation<'c> {
223223
// If the target does not support data prefetch, the address expression is evaluated if
224224
// it includes side effects but no other code is generated and GCC does not issue a warning.
225225
// void __builtin_prefetch (const void *addr, ...);
226-
"__builtin_prefetch" => self.convert_expr(ctx.unused(), args[0], None),
226+
"__builtin_prefetch" => self.convert_expr(ctx.unused(), args[0]),
227227

228228
"__builtin_memcpy" | "__builtin_memcmp" | "__builtin_memmove" | "__builtin_strncmp"
229229
| "__builtin_strncpy" | "__builtin_strncat" => self.convert_libc_fns(
@@ -301,8 +301,8 @@ impl<'c> Translation<'c> {
301301
// We can't convert this to Rust, but it should be safe to always return -1/0
302302
// (depending on the value of `type`), so we emit the following:
303303
// `(if (type & 2) == 0 { -1isize } else { 0isize }) as libc::size_t`
304-
let ptr_arg = self.convert_expr(ctx.unused(), args[0], None)?;
305-
let type_arg = self.convert_expr(ctx.used(), args[1], None)?;
304+
let ptr_arg = self.convert_expr(ctx.unused(), args[0])?;
305+
let type_arg = self.convert_expr(ctx.used(), args[1])?;
306306
Ok(ptr_arg.and_then(|_| {
307307
type_arg.map(|type_arg| {
308308
let type_and_2 = mk().binary_expr(
@@ -332,7 +332,7 @@ impl<'c> Translation<'c> {
332332
if ctx.is_unused() && args.len() == 2 {
333333
if let Some(va_id) = self.match_vastart(args[0]) {
334334
if self.ast_context.get_decl(&va_id).is_some() {
335-
let dst = self.convert_expr(ctx.used(), args[0], None)?;
335+
let dst = self.convert_expr(ctx.used(), args[0])?;
336336
let fn_ctx = self.function_context.borrow();
337337
let src = fn_ctx.get_va_list_arg_name();
338338

@@ -353,8 +353,8 @@ impl<'c> Translation<'c> {
353353
"__builtin_va_copy" => {
354354
if ctx.is_unused() && args.len() == 2 {
355355
if let Some((_dst_va_id, _src_va_id)) = self.match_vacopy(args[0], args[1]) {
356-
let dst = self.convert_expr(ctx.used(), args[0], None)?;
357-
let src = self.convert_expr(ctx.used(), args[1], None)?;
356+
let dst = self.convert_expr(ctx.used(), args[0])?;
357+
let src = self.convert_expr(ctx.used(), args[1])?;
358358

359359
let call_expr = mk().method_call_expr(src.to_expr(), "clone", vec![]);
360360
let assign_expr = mk().assign_expr(dst.to_expr(), call_expr);
@@ -379,7 +379,7 @@ impl<'c> Translation<'c> {
379379
}
380380

381381
"__builtin_alloca" => {
382-
let count = self.convert_expr(ctx.used(), args[0], None)?;
382+
let count = self.convert_expr(ctx.used(), args[0])?;
383383
Ok(count.and_then(|count| {
384384
// Get `alloca` allocation storage.
385385
let mut fn_ctx = self.function_context.borrow_mut();
@@ -426,7 +426,7 @@ impl<'c> Translation<'c> {
426426
} else {
427427
warn!("{builtin_name} has no Rust equivalent; emitting null pointer");
428428
}
429-
let level = self.convert_expr(ctx.unused(), args[0], None)?;
429+
let level = self.convert_expr(ctx.unused(), args[0])?;
430430
Ok(level.and_then(|_| {
431431
let void_ty = mk().abs_path_ty(vec!["core", "ffi", "c_void"]);
432432
let type_args = mk().angle_bracketed_args(vec![void_ty]);
@@ -447,7 +447,7 @@ impl<'c> Translation<'c> {
447447
// architectures (only used to mask/unmask hardware-specific
448448
// bits like the ARM Thumb mode bit). Pass the argument
449449
// through unchanged.
450-
self.convert_expr(ctx, args[0], None)
450+
self.convert_expr(ctx, args[0])
451451
}
452452

453453
"__builtin_ia32_pause" => {
@@ -495,9 +495,9 @@ impl<'c> Translation<'c> {
495495
| "__sync_bool_compare_and_swap_4"
496496
| "__sync_bool_compare_and_swap_8"
497497
| "__sync_bool_compare_and_swap_16" => {
498-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
499-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
500-
let arg2 = self.convert_expr(ctx.used(), args[2], None)?;
498+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
499+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
500+
let arg2 = self.convert_expr(ctx.used(), args[2])?;
501501
arg0.zip(arg1)
502502
.zip(arg2)
503503
.and_then_try(|((arg0, arg1), arg2)| {
@@ -532,8 +532,8 @@ impl<'c> Translation<'c> {
532532
| "__sync_lock_test_and_set_16" => {
533533
// Emit `atomic_xchg_acquire(arg0, arg1)`
534534
let atomic_func = self.atomic_intrinsic_expr("xchg", &[Acquire]);
535-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
536-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
535+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
536+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
537537
arg0.zip(arg1).and_then_try(|(arg0, arg1)| {
538538
let call_expr = mk().call_expr(atomic_func, vec![arg0, arg1]);
539539
self.convert_side_effects_expr(
@@ -551,7 +551,7 @@ impl<'c> Translation<'c> {
551551
| "__sync_lock_release_16" => {
552552
// Emit `atomic_store_release(arg0, 0)`
553553
let atomic_func = self.atomic_intrinsic_expr("store", &[Release]);
554-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
554+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
555555
arg0.and_then_try(|arg0| {
556556
let zero = mk().lit_expr(mk().int_lit(0, ""));
557557
let call_expr = mk().call_expr(atomic_func, vec![arg0, zero]);
@@ -564,7 +564,7 @@ impl<'c> Translation<'c> {
564564
}
565565
// There's currently no way to replicate this functionality in Rust, so we just
566566
// pass the ptr input param in its place.
567-
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0], None)?),
567+
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0])?),
568568
// Skip over, there's no way to implement it in Rust
569569
"__builtin_unwind_init" => Ok(WithStmts::new_val(self.panic_or_err("no value"))),
570570
"__builtin_unreachable" => Ok(WithStmts::new(
@@ -588,8 +588,8 @@ impl<'c> Translation<'c> {
588588

589589
_ => {
590590
if let Some(atomic_op) = CAtomicBinOp::from_sync_builtin_fn(builtin_name) {
591-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
592-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
591+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
592+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
593593
let arg1_type_id = self.ast_context[args[1]]
594594
.kind
595595
.get_qual_type()
@@ -638,7 +638,7 @@ impl<'c> Translation<'c> {
638638
method_name: &str,
639639
args: &[CExprId],
640640
) -> TranslationResult<WithStmts<Box<Expr>>> {
641-
let args = self.convert_exprs(ctx.used(), args, None)?;
641+
let args = self.convert_exprs(ctx.used(), args)?;
642642
args.and_then_try(|args| {
643643
let [a, b, c]: [_; 3] = args
644644
.try_into()
@@ -678,7 +678,7 @@ impl<'c> Translation<'c> {
678678
let name = &builtin_name[10..];
679679
self.use_crate(ExternCrate::Libc);
680680
let mem = mk().abs_path_expr(vec!["libc", name]);
681-
let args = self.convert_exprs(ctx.used(), args, None)?;
681+
let args = self.convert_exprs(ctx.used(), args)?;
682682
args.and_then_try(|args| {
683683
if args.len() != arg_types.len() {
684684
// This should not generally happen, as the C frontend checks these first

0 commit comments

Comments
 (0)