Skip to content

Commit a328a7a

Browse files
committed
transpile: Remove override_ty parameter from convert_expr
1 parent 733a25f commit a328a7a

12 files changed

Lines changed: 145 additions & 197 deletions

File tree

c2rust-transpile/src/cfg/mod.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -630,9 +630,7 @@ impl Cfg<Label, StmtOrDecl> {
630630
wip.body.push(StmtOrDecl::Stmt(mk().semi_stmt(ret_expr)));
631631
}
632632
ImplicitReturnType::StmtExpr(ctx, expr_id, brk_label) => {
633-
let (stmts, val) = translator
634-
.convert_expr(ctx, expr_id, None)?
635-
.discard_unsafe();
633+
let (stmts, val) = translator.convert_expr(ctx, expr_id)?.discard_unsafe();
636634

637635
wip.body.extend(stmts.into_iter().map(StmtOrDecl::Stmt));
638636
wip.body.push(StmtOrDecl::Stmt(mk().semi_stmt(
@@ -1437,7 +1435,7 @@ impl CfgBuilder {
14371435
}
14381436

14391437
CStmtKind::Return(expr) => {
1440-
let val = match expr.map(|i| translator.convert_expr(ctx.used(), i, ret_ty)) {
1438+
let val = match expr.map(|i| translator.convert_expr(ctx.used(), i)) {
14411439
Some(r) => Some(r?),
14421440
None => None,
14431441
};
@@ -1701,9 +1699,8 @@ impl CfgBuilder {
17011699
match increment {
17021700
None => slf.add_block(incr_entry, BasicBlock::new_jump(cond_entry)),
17031701
Some(incr) => {
1704-
let incr_stmts = translator
1705-
.convert_expr(ctx.unused(), incr, None)?
1706-
.into_stmts();
1702+
let incr_stmts =
1703+
translator.convert_expr(ctx.unused(), incr)?.into_stmts();
17071704
let mut incr_wip = slf.new_wip_block(incr_entry);
17081705
incr_wip.extend(incr_stmts);
17091706
slf.add_wip_block(incr_wip, Jump(cond_entry));
@@ -1814,11 +1811,7 @@ impl CfgBuilder {
18141811
match blk_or_wip {
18151812
Ok(blk) => Ok(blk),
18161813
Err(mut wip) => {
1817-
wip.extend(
1818-
translator
1819-
.convert_expr(ctx.unused(), expr, None)?
1820-
.into_stmts(),
1821-
);
1814+
wip.extend(translator.convert_expr(ctx.unused(), expr)?.into_stmts());
18221815

18231816
// If we can tell the expression is going to diverge, there is no falling through to
18241817
// the next block.
@@ -1878,7 +1871,7 @@ impl CfgBuilder {
18781871
let branch = match translator.ast_context.index(resolved).kind {
18791872
CExprKind::Literal(..) | CExprKind::ConstantExpr(_, _, Some(_)) => {
18801873
match translator
1881-
.convert_expr(ctx.used(), resolved, None)?
1874+
.convert_expr(ctx.used(), resolved)?
18821875
.to_pure_expr()
18831876
{
18841877
Some(expr) => match *expr {
@@ -1956,7 +1949,7 @@ impl CfgBuilder {
19561949

19571950
// Convert the condition
19581951
let (stmts, val) = translator
1959-
.convert_expr(ctx.used(), scrutinee, None)?
1952+
.convert_expr(ctx.used(), scrutinee)?
19601953
.discard_unsafe();
19611954
wip.extend(stmts);
19621955

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

@@ -919,7 +919,7 @@ impl<'c> Translation<'c> {
919919

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

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
@@ -29,8 +29,8 @@ impl<'c> Translation<'c> {
2929
rotate_method_name: &'static str,
3030
) -> TranslationResult<WithStmts<Box<Expr>>> {
3131
// Emit `arg0.{method_name}(arg1)`
32-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
33-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
32+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
33+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
3434
arg0.and_then(|arg0| {
3535
arg1.and_then(|arg1| {
3636
let arg1 = mk().cast_expr(arg1, mk().path_ty(vec!["u32"]));
@@ -105,7 +105,7 @@ impl<'c> Translation<'c> {
105105
});
106106
}
107107

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

110110
Ok(val.map(|v| {
111111
let val = mk().method_call_expr(v, "is_sign_negative", vec![]);
@@ -114,7 +114,7 @@ impl<'c> Translation<'c> {
114114
}))
115115
}
116116
"__builtin_ffs" | "__builtin_ffsl" | "__builtin_ffsll" => {
117-
let val = self.convert_expr(ctx.used(), args[0], None)?;
117+
let val = self.convert_expr(ctx.used(), args[0])?;
118118

119119
Ok(val.map(|x| {
120120
let add = BinOp::Add(Default::default());
@@ -131,29 +131,29 @@ impl<'c> Translation<'c> {
131131
}))
132132
}
133133
"__builtin_clz" | "__builtin_clzl" | "__builtin_clzll" => {
134-
let val = self.convert_expr(ctx.used(), args[0], None)?;
134+
let val = self.convert_expr(ctx.used(), args[0])?;
135135
Ok(val.map(|x| {
136136
let zeros = mk().method_call_expr(x, "leading_zeros", vec![]);
137137
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
138138
}))
139139
}
140140
"__builtin_ctz" | "__builtin_ctzl" | "__builtin_ctzll" => {
141-
let val = self.convert_expr(ctx.used(), args[0], None)?;
141+
let val = self.convert_expr(ctx.used(), args[0])?;
142142
Ok(val.map(|x| {
143143
let zeros = mk().method_call_expr(x, "trailing_zeros", vec![]);
144144
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
145145
}))
146146
}
147147
"__builtin_bswap16" | "__builtin_bswap32" | "__builtin_bswap64" => {
148-
let val = self.convert_expr(ctx.used(), args[0], None)?;
148+
let val = self.convert_expr(ctx.used(), args[0])?;
149149
Ok(val.map(|x| mk().method_call_expr(x, "swap_bytes", vec![])))
150150
}
151151
"__builtin_fabs" | "__builtin_fabsf" | "__builtin_fabsl" => {
152-
let val = self.convert_expr(ctx.used(), args[0], None)?;
152+
let val = self.convert_expr(ctx.used(), args[0])?;
153153
Ok(val.map(|x| mk().method_call_expr(x, "abs", vec![])))
154154
}
155155
"__builtin_isfinite" | "__builtin_isnan" => {
156-
let val = self.convert_expr(ctx.used(), args[0], None)?;
156+
let val = self.convert_expr(ctx.used(), args[0])?;
157157

158158
let seg = match builtin_name {
159159
"__builtin_isfinite" => "is_finite",
@@ -167,7 +167,7 @@ impl<'c> Translation<'c> {
167167
}
168168
"__builtin_isinf_sign" => {
169169
// isinf_sign(x) -> fabs(x) == infinity ? (signbit(x) ? -1 : 1) : 0
170-
let val = self.convert_expr(ctx.used(), args[0], None)?;
170+
let val = self.convert_expr(ctx.used(), args[0])?;
171171
Ok(val.map(|x| {
172172
let inner_cond = mk().method_call_expr(x.clone(), "is_sign_positive", vec![]);
173173
let one = mk().lit_expr(mk().int_lit(1, ""));
@@ -189,18 +189,18 @@ impl<'c> Translation<'c> {
189189
// https://github.com/llvm-mirror/llvm/blob/master/lib/CodeGen/IntrinsicLowering.cpp#L470
190190
Ok(WithStmts::new_val(mk().lit_expr(mk().int_lit(1, "i32"))))
191191
}
192-
"__builtin_expect" => self.convert_expr(ctx.used(), args[0], None),
192+
"__builtin_expect" => self.convert_expr(ctx.used(), args[0]),
193193

194194
"__builtin_popcount" | "__builtin_popcountl" | "__builtin_popcountll" => {
195-
let val = self.convert_expr(ctx.used(), args[0], None)?;
195+
let val = self.convert_expr(ctx.used(), args[0])?;
196196
Ok(val.map(|x| {
197197
let zeros = mk().method_call_expr(x, "count_ones", vec![]);
198198
mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
199199
}))
200200
}
201201
"__builtin_bzero" => {
202-
let ptr_stmts = self.convert_expr(ctx.used(), args[0], None)?;
203-
let n_stmts = self.convert_expr(ctx.used(), args[1], None)?;
202+
let ptr_stmts = self.convert_expr(ctx.used(), args[0])?;
203+
let n_stmts = self.convert_expr(ctx.used(), args[1])?;
204204
let write_bytes = mk().abs_path_expr(vec!["core", "ptr", "write_bytes"]);
205205
let zero = mk().lit_expr(mk().int_lit(0, "u8"));
206206
ptr_stmts.and_then(|ptr| {
@@ -211,7 +211,7 @@ impl<'c> Translation<'c> {
211211
// If the target does not support data prefetch, the address expression is evaluated if
212212
// it includes side effects but no other code is generated and GCC does not issue a warning.
213213
// void __builtin_prefetch (const void *addr, ...);
214-
"__builtin_prefetch" => self.convert_expr(ctx.unused(), args[0], None),
214+
"__builtin_prefetch" => self.convert_expr(ctx.unused(), args[0]),
215215

216216
"__builtin_memcpy" | "__builtin_memcmp" | "__builtin_memmove" | "__builtin_strncmp"
217217
| "__builtin_strncpy" | "__builtin_strncat" => self.convert_libc_fns(
@@ -289,8 +289,8 @@ impl<'c> Translation<'c> {
289289
// We can't convert this to Rust, but it should be safe to always return -1/0
290290
// (depending on the value of `type`), so we emit the following:
291291
// `(if (type & 2) == 0 { -1isize } else { 0isize }) as libc::size_t`
292-
let ptr_arg = self.convert_expr(ctx.unused(), args[0], None)?;
293-
let type_arg = self.convert_expr(ctx.used(), args[1], None)?;
292+
let ptr_arg = self.convert_expr(ctx.unused(), args[0])?;
293+
let type_arg = self.convert_expr(ctx.used(), args[1])?;
294294
ptr_arg.and_then(|_| {
295295
Ok(type_arg.map(|type_arg| {
296296
let type_and_2 = mk().binary_expr(
@@ -323,7 +323,7 @@ impl<'c> Translation<'c> {
323323
if ctx.is_unused() && args.len() == 2 {
324324
if let Some(va_id) = self.match_vastart(args[0]) {
325325
if self.ast_context.get_decl(&va_id).is_some() {
326-
let dst = self.convert_expr(ctx.used(), args[0], None)?;
326+
let dst = self.convert_expr(ctx.used(), args[0])?;
327327
let fn_ctx = self.function_context.borrow();
328328
let src = fn_ctx.get_va_list_arg_name();
329329

@@ -344,8 +344,8 @@ impl<'c> Translation<'c> {
344344
"__builtin_va_copy" => {
345345
if ctx.is_unused() && args.len() == 2 {
346346
if let Some((_dst_va_id, _src_va_id)) = self.match_vacopy(args[0], args[1]) {
347-
let dst = self.convert_expr(ctx.used(), args[0], None)?;
348-
let src = self.convert_expr(ctx.used(), args[1], None)?;
347+
let dst = self.convert_expr(ctx.used(), args[0])?;
348+
let src = self.convert_expr(ctx.used(), args[1])?;
349349

350350
let call_expr = mk().method_call_expr(src.to_expr(), "clone", vec![]);
351351
let assign_expr = mk().assign_expr(dst.to_expr(), call_expr);
@@ -370,7 +370,7 @@ impl<'c> Translation<'c> {
370370
}
371371

372372
"__builtin_alloca" => {
373-
let count = self.convert_expr(ctx.used(), args[0], None)?;
373+
let count = self.convert_expr(ctx.used(), args[0])?;
374374
count.and_then(|count| {
375375
// Get `alloca` allocation storage.
376376
let mut fn_ctx = self.function_context.borrow_mut();
@@ -536,9 +536,9 @@ impl<'c> Translation<'c> {
536536
| "__sync_bool_compare_and_swap_4"
537537
| "__sync_bool_compare_and_swap_8"
538538
| "__sync_bool_compare_and_swap_16" => {
539-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
540-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
541-
let arg2 = self.convert_expr(ctx.used(), args[2], None)?;
539+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
540+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
541+
let arg2 = self.convert_expr(ctx.used(), args[2])?;
542542
arg0.and_then(|arg0| {
543543
arg1.and_then(|arg1| {
544544
arg2.and_then(|arg2| {
@@ -632,8 +632,8 @@ impl<'c> Translation<'c> {
632632
"and"
633633
};
634634

635-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
636-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
635+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
636+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
637637
let fetch_first = builtin_name.starts_with("__sync_fetch");
638638
arg0.and_then(|arg0| {
639639
arg1.and_then(|arg1| {
@@ -659,8 +659,8 @@ impl<'c> Translation<'c> {
659659
| "__sync_lock_test_and_set_16" => {
660660
// Emit `atomic_xchg_acquire(arg0, arg1)`
661661
let atomic_func = self.atomic_intrinsic_expr("xchg", &[Acquire]);
662-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
663-
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
662+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
663+
let arg1 = self.convert_expr(ctx.used(), args[1])?;
664664
arg0.and_then(|arg0| {
665665
arg1.and_then(|arg1| {
666666
let call_expr = mk().call_expr(atomic_func, vec![arg0, arg1]);
@@ -680,7 +680,7 @@ impl<'c> Translation<'c> {
680680
| "__sync_lock_release_16" => {
681681
// Emit `atomic_store_release(arg0, 0)`
682682
let atomic_func = self.atomic_intrinsic_expr("store", &[Release]);
683-
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
683+
let arg0 = self.convert_expr(ctx.used(), args[0])?;
684684
arg0.and_then(|arg0| {
685685
let zero = mk().lit_expr(mk().int_lit(0, ""));
686686
let call_expr = mk().call_expr(atomic_func, vec![arg0, zero]);
@@ -693,7 +693,7 @@ impl<'c> Translation<'c> {
693693
}
694694
// There's currently no way to replicate this functionality in Rust, so we just
695695
// pass the ptr input param in its place.
696-
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0], None)?),
696+
"__builtin_assume_aligned" => Ok(self.convert_expr(ctx.used(), args[0])?),
697697
// Skip over, there's no way to implement it in Rust
698698
"__builtin_unwind_init" => Ok(WithStmts::new_val(self.panic_or_err("no value"))),
699699
"__builtin_unreachable" => Ok(WithStmts::new(
@@ -731,7 +731,7 @@ impl<'c> Translation<'c> {
731731
method_name: &str,
732732
args: &[CExprId],
733733
) -> TranslationResult<WithStmts<Box<Expr>>> {
734-
let args = self.convert_exprs(ctx.used(), args, None)?;
734+
let args = self.convert_exprs(ctx.used(), args)?;
735735
args.and_then(|args| {
736736
let [a, b, c]: [_; 3] = args
737737
.try_into()
@@ -771,7 +771,7 @@ impl<'c> Translation<'c> {
771771
let name = &builtin_name[10..];
772772
self.use_crate(ExternCrate::Libc);
773773
let mem = mk().abs_path_expr(vec!["libc", name]);
774-
let args = self.convert_exprs(ctx.used(), args, None)?;
774+
let args = self.convert_exprs(ctx.used(), args)?;
775775
args.and_then(|args| {
776776
if args.len() != arg_types.len() {
777777
// This should not generally happen, as the C frontend checks these first

c2rust-transpile/src/translator/literals.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'c> Translation<'c> {
137137
// Convert all of the provided initializer values
138138

139139
let to_array_element = |id: CExprId| -> TranslationResult<_> {
140-
self.convert_expr(ctx.used(), id, None)?.result_map(|x| {
140+
self.convert_expr(ctx.used(), id)?.result_map(|x| {
141141
// Array literals require all of their elements to be
142142
// the correct type; they will not use implicit casts to
143143
// change mut to const. This becomes a problem when an
@@ -201,7 +201,7 @@ impl<'c> Translation<'c> {
201201
// * `ptr_extra_braces`
202202
// * `array_of_ptrs`
203203
// * `array_of_arrays`
204-
self.convert_expr(ctx.used(), single, None)
204+
self.convert_expr(ctx.used(), single)
205205
}
206206
&[single] if is_zero_literal(single) && n > 1 => {
207207
// This was likely a C array of the form `int x[16] = { 0 }`.
@@ -233,18 +233,18 @@ impl<'c> Translation<'c> {
233233
}
234234
CTypeKind::Pointer(_) => {
235235
let id = ids.first().unwrap();
236-
self.convert_expr(ctx.used(), *id, None)
236+
self.convert_expr(ctx.used(), *id)
237237
}
238238
CTypeKind::Enum(_) => {
239239
let id = ids.first().unwrap();
240-
self.convert_expr(ctx.used(), *id, None)
240+
self.convert_expr(ctx.used(), *id)
241241
}
242242
CTypeKind::Vector(CQualTypeId { ctype, .. }, len) => {
243243
self.vector_list_initializer(ctx, ids, ctype, len)
244244
}
245245
ref kind if kind.is_integral_type() => {
246246
let id = ids.first().unwrap();
247-
self.convert_expr(ctx.used(), *id, None)
247+
self.convert_expr(ctx.used(), *id)
248248
}
249249
ref t => Err(format_err!("Init list not implemented for {:?}", t).into()),
250250
}

0 commit comments

Comments
 (0)