Skip to content

Commit c0809f5

Browse files
committed
transpile: get expected types for direct function calls from decl
1 parent 95b71c3 commit c0809f5

File tree

1 file changed

+32
-1
lines changed
  • c2rust-transpile/src/translator

1 file changed

+32
-1
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3776,6 +3776,31 @@ impl<'c> Translation<'c> {
37763776
Some(CTypeKind::Function(_, _, is_variadic, _, _)) => *is_variadic,
37773777
_ => false,
37783778
};
3779+
// Extract type from function declaraction for direct function calls.
3780+
let extract_direct_call_arg_tys = || -> Option<Vec<CQualTypeId>> {
3781+
use CastKind::FunctionToPointerDecay;
3782+
if let CExprKind::ImplicitCast(_, fexp, FunctionToPointerDecay, _, _) =
3783+
self.ast_context[func].kind
3784+
{
3785+
if let CExprKind::DeclRef(_ty, decl_id, _rv) = &self.ast_context[fexp].kind
3786+
{
3787+
if let CDeclKind::Function { parameters, .. } =
3788+
&self.ast_context.index(*decl_id).kind
3789+
{
3790+
let mut param_tys = vec![];
3791+
for p in parameters {
3792+
match self.ast_context.index(*p).kind {
3793+
CDeclKind::Variable { typ, .. } => param_tys.push(typ),
3794+
_ => return None,
3795+
}
3796+
}
3797+
return Some(param_tys);
3798+
}
3799+
}
3800+
}
3801+
None
3802+
};
3803+
let arg_tys = extract_direct_call_arg_tys();
37793804
let func = match self.ast_context[func].kind {
37803805
// Direct function call
37813806
CExprKind::ImplicitCast(_, fexp, CastKind::FunctionToPointerDecay, _, _)
@@ -3836,7 +3861,13 @@ impl<'c> Translation<'c> {
38363861
// We want to decay refs only when function is variadic
38373862
ctx.decay_ref = DecayRef::from(is_variadic);
38383863

3839-
let args = self.convert_exprs(ctx.used(), args, None)?;
3864+
let arg_tys = if let (false, Some(arg_tys)) = (is_variadic, &arg_tys) {
3865+
assert!(arg_tys.len() == args.len());
3866+
Some(arg_tys.as_slice())
3867+
} else {
3868+
None
3869+
};
3870+
let args = self.convert_exprs(ctx.used(), args, arg_tys)?;
38403871

38413872
let res: TranslationResult<_> = Ok(args.map(|args| mk().call_expr(func, args)));
38423873
res

0 commit comments

Comments
 (0)