Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions c2rust-transpile/src/c_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,35 @@ impl TypedAstContext {
}
false
}

/// Return whether the literal can be directly translated as this type.
pub fn literal_matches_ty(&self, lit: &CLiteral, ty: CQualTypeId, is_negated: bool) -> bool {
let ty_kind = &self.resolve_type(ty.ctype).kind;
match *lit {
CLiteral::Integer(value, _) | CLiteral::Character(value)
if ty_kind.is_integral_type() && !ty_kind.is_bool() =>
{
ty_kind.guaranteed_integer_in_range(value)
&& (!is_negated || ty_kind.is_signed_integral_type())
}
CLiteral::Floating(value, _) if ty_kind.is_floating_type() => {
ty_kind.guaranteed_float_in_range(value)
}
_ => false,
}
}

/// Same as the `Index` trait, but doesn't bypass `Paren`.
pub fn index_expr_raw(&self, index: CExprId) -> &CExpr {
static BADEXPR: CExpr = Located {
loc: None,
kind: CExprKind::BadExpr,
};
match self.c_exprs.get(&index) {
None => &BADEXPR,
Some(e) => e,
}
}
}

impl CommentContext {
Expand Down
21 changes: 7 additions & 14 deletions c2rust-transpile/src/cfg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,7 @@ impl Cfg<Label, StmtOrDecl> {
wip.body.push(StmtOrDecl::Stmt(mk().semi_stmt(ret_expr)));
}
ImplicitReturnType::StmtExpr(ctx, expr_id, brk_label) => {
let (stmts, val) = translator
.convert_expr(ctx, expr_id, None)?
.discard_unsafe();
let (stmts, val) = translator.convert_expr(ctx, expr_id)?.discard_unsafe();

wip.body.extend(stmts.into_iter().map(StmtOrDecl::Stmt));
wip.body.push(StmtOrDecl::Stmt(mk().semi_stmt(
Expand Down Expand Up @@ -1420,7 +1418,7 @@ impl CfgBuilder {
}

CStmtKind::Return(expr) => {
let val = match expr.map(|i| translator.convert_expr(ctx.used(), i, ret_ty)) {
let val = match expr.map(|i| translator.convert_expr(ctx.used(), i)) {
Some(r) => Some(r?),
None => None,
};
Expand Down Expand Up @@ -1684,9 +1682,8 @@ impl CfgBuilder {
match increment {
None => slf.add_block(incr_entry, BasicBlock::new_jump(cond_entry)),
Some(incr) => {
let incr_stmts = translator
.convert_expr(ctx.unused(), incr, None)?
.into_stmts();
let incr_stmts =
translator.convert_expr(ctx.unused(), incr)?.into_stmts();
let mut incr_wip = slf.new_wip_block(incr_entry);
incr_wip.extend(incr_stmts);
slf.add_wip_block(incr_wip, Jump(cond_entry));
Expand Down Expand Up @@ -1797,11 +1794,7 @@ impl CfgBuilder {
match blk_or_wip {
Ok(blk) => Ok(blk),
Err(mut wip) => {
wip.extend(
translator
.convert_expr(ctx.unused(), expr, None)?
.into_stmts(),
);
wip.extend(translator.convert_expr(ctx.unused(), expr)?.into_stmts());

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

// Convert the condition
let (stmts, val) = translator
.convert_expr(ctx.used(), scrutinee, None)?
.convert_expr(ctx.used(), scrutinee)?
.discard_unsafe();
wip.extend(stmts);

Expand Down
4 changes: 2 additions & 2 deletions c2rust-transpile/src/translator/assembly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ impl<'c> Translation<'c> {

// First, convert output expr if present
let out_expr = if let Some((output_idx, out_expr)) = operand.out_expr {
let mut out_expr = self.convert_expr(ctx.used(), out_expr, None)?;
let mut out_expr = self.convert_expr(ctx.used(), out_expr)?;
stmts.append(out_expr.stmts_mut());
let mut out_expr = out_expr.into_value();

Expand Down Expand Up @@ -921,7 +921,7 @@ impl<'c> Translation<'c> {

// Then, handle input expr if present
let in_expr = if let Some((input_idx, in_expr)) = operand.in_expr {
let mut in_expr = self.convert_expr(ctx.used(), in_expr, None)?;
let mut in_expr = self.convert_expr(ctx.used(), in_expr)?;
stmts.append(in_expr.stmts_mut());
let mut in_expr = in_expr.into_value();

Expand Down
8 changes: 4 additions & 4 deletions c2rust-transpile/src/translator/atomics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ impl<'c> Translation<'c> {
val2_id: Option<CExprId>,
weak_id: Option<CExprId>,
) -> TranslationResult<WithStmts<Box<Expr>>> {
let ptr = self.convert_expr(ctx.used(), ptr_id, None)?;
let ptr = self.convert_expr(ctx.used(), ptr_id)?;
let order = self.convert_memordering(order_id);
let val1 = val1_id
.map(|x| self.convert_expr(ctx.used(), x, None))
.map(|x| self.convert_expr(ctx.used(), x))
.transpose()?;
let order_fail = order_fail_id.and_then(|x| self.convert_memordering(x));
let val2 = val2_id
.map(|x| self.convert_expr(ctx.used(), x, None))
.map(|x| self.convert_expr(ctx.used(), x))
.transpose()?;
let weak = weak_id.and_then(|x| self.convert_constant_bool(x));

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