Skip to content

Commit fccdc87

Browse files
committed
transpile: Remove override_ty param from convert_expr
1 parent 99678f2 commit fccdc87

13 files changed

Lines changed: 141 additions & 184 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

@@ -244,7 +244,7 @@ impl<'c> Translation<'c> {
244244
if name == "__atomic_exchange" {
245245
// LLVM stores the ret pointer in the order_fail slot
246246
order_fail_id
247-
.map(|x| self.convert_expr(ctx.used(), x, None))
247+
.map(|x| self.convert_expr(ctx.used(), x))
248248
.transpose()?
249249
.expect("__atomic_exchange must have a ret pointer argument")
250250
.and_then(|ret| {

c2rust-transpile/src/translator/builtins.rs

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

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

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

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

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

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

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

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

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

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

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

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

382382
"__builtin_alloca" => {
383-
let count = self.convert_expr(ctx.used(), args[0], None)?;
383+
let count = self.convert_expr(ctx.used(), args[0])?;
384384
count.and_then(|count| {
385385
// Get `alloca` allocation storage.
386386
let mut fn_ctx = self.function_context.borrow_mut();
@@ -459,9 +459,9 @@ impl<'c> Translation<'c> {
459459
| "__sync_bool_compare_and_swap_4"
460460
| "__sync_bool_compare_and_swap_8"
461461
| "__sync_bool_compare_and_swap_16" => {
462-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
463-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
464-
let arg2 = self.convert_expr(ctx.used(), args[2], None)?;
462+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
463+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
464+
let arg2 = self.convert_expr(ctx.used(), args[2])?;
465465
arg0.and_then(|arg0| {
466466
arg1.and_then(|arg1| {
467467
arg2.and_then(|arg2| {
@@ -498,8 +498,8 @@ impl<'c> Translation<'c> {
498498
| "__sync_lock_test_and_set_16" => {
499499
// Emit `atomic_xchg_acquire(arg0, arg1)`
500500
let atomic_func = self.atomic_intrinsic_expr("xchg", &[Acquire]);
501-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
502-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
501+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
502+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
503503
arg0.and_then(|arg0| {
504504
arg1.and_then(|arg1| {
505505
let call_expr = mk().call_expr(atomic_func, vec![arg0, arg1]);
@@ -519,7 +519,7 @@ impl<'c> Translation<'c> {
519519
| "__sync_lock_release_16" => {
520520
// Emit `atomic_store_release(arg0, 0)`
521521
let atomic_func = self.atomic_intrinsic_expr("store", &[Release]);
522-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
522+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
523523
arg0.and_then(|arg0| {
524524
let zero = mk().lit_expr(mk().int_lit(0, ""));
525525
let call_expr = mk().call_expr(atomic_func, vec![arg0, zero]);
@@ -532,7 +532,7 @@ impl<'c> Translation<'c> {
532532
}
533533
// There's currently no way to replicate this functionality in Rust, so we just
534534
// pass the ptr input param in its place.
535-
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0], None)?),
535+
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0])?),
536536
// Skip over, there's no way to implement it in Rust
537537
"__builtin_unwind_init" => Ok(WithStmts::new_val(self.panic_or_err("no value"))),
538538
"__builtin_unreachable" => Ok(WithStmts::new(
@@ -556,8 +556,8 @@ impl<'c> Translation<'c> {
556556

557557
_ => {
558558
if let Some(atomic_op) = CAtomicBinOp::from_sync_builtin_fn(builtin_name) {
559-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
560-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
559+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
560+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
561561
let arg1_type_id = self.ast_context[args[1]]
562562
.kind
563563
.get_qual_type()
@@ -608,7 +608,7 @@ impl<'c> Translation<'c> {
608608
method_name: &str,
609609
args: &[CExprId],
610610
) -> TranslationResult<WithStmts<Box<Expr>>> {
611-
let args = self.convert_exprs(ctx.used(), args, None)?;
611+
let args = self.convert_exprs(ctx.used(), args)?;
612612
args.and_then(|args| {
613613
let [a, b, c]: [_; 3] = args
614614
.try_into()
@@ -648,7 +648,7 @@ impl<'c> Translation<'c> {
648648
let name = &builtin_name[10..];
649649
self.use_crate(ExternCrate::Libc);
650650
let mem = mk().abs_path_expr(vec!["libc", name]);
651-
let args = self.convert_exprs(ctx.used(), args, None)?;
651+
let args = self.convert_exprs(ctx.used(), args)?;
652652
args.and_then(|args| {
653653
if args.len() != arg_types.len() {
654654
// This should not generally happen, as the C frontend checks these first

c2rust-transpile/src/translator/literals.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'c> Translation<'c> {
156156
// Convert all of the provided initializer values
157157

158158
let to_array_element = |id: CExprId| -> TranslationResult<_> {
159-
self.convert_expr(ctx.used(), id, None)?.result_map(|x| {
159+
self.convert_expr(ctx.used(), id)?.result_map(|x| {
160160
// Array literals require all of their elements to be
161161
// the correct type; they will not use implicit casts to
162162
// change mut to const. This becomes a problem when an
@@ -220,7 +220,7 @@ impl<'c> Translation<'c> {
220220
// * `ptr_extra_braces`
221221
// * `array_of_ptrs`
222222
// * `array_of_arrays`
223-
self.convert_expr(ctx.used(), single, None)
223+
self.convert_expr(ctx.used(), single)
224224
}
225225
&[single] if is_zero_literal(single) && n > 1 => {
226226
// This was likely a C array of the form `int x[16] = { 0 }`.
@@ -255,7 +255,7 @@ impl<'c> Translation<'c> {
255255
}
256256
ref kind if kind.is_scalar() => {
257257
if let Some(&first) = ids.first() {
258-
self.convert_expr(ctx.used(), first, None)
258+
self.convert_expr(ctx.used(), first)
259259
} else {
260260
self.implicit_default_expr(ctx.used(), ty.ctype)
261261
}

c2rust-transpile/src/translator/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ impl<'c> Translation<'c> {
7474
.try_fold::<Option<(WithStmts<Box<Expr>>, CTypeId)>, _, _>(None, |canonical, &id| {
7575
self.can_convert_const_macro_expansion(id)?;
7676

77+
let expr = self.convert_expr(ctx, id)?;
7778
let override_ty = self.expr_override_types.get(&id).copied();
78-
let expr = self.convert_expr(ctx, id, override_ty)?;
7979
let ty = override_ty
8080
.or_else(|| self.ast_context[id].kind.get_qual_type())
8181
.ok_or_else(|| format_err!("Invalid expression type"))?

0 commit comments

Comments
 (0)