Skip to content

Commit bf97325

Browse files
committed
transpile: Split convert_pointer into two functions for easier reuse
1 parent edcefc2 commit bf97325

File tree

3 files changed

+39
-36
lines changed

3 files changed

+39
-36
lines changed

c2rust-transpile/src/convert_type.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -268,44 +268,49 @@ impl TypeConverter {
268268
Ok(mk().unsafe_().extern_("C").barefn_ty(fn_ty))
269269
}
270270

271+
/// Converts the qualified type of a pointer.
271272
pub fn convert_pointer(
272273
&mut self,
273274
ctxt: &TypedAstContext,
274275
qtype: CQualTypeId,
275276
) -> TranslationResult<Box<Type>> {
276-
let mutbl = if qtype.qualifiers.is_const {
277-
Mutability::Immutable
277+
let pointee_ty = self.convert_pointee(ctxt, qtype.ctype)?;
278+
279+
if let CTypeKind::Function(..) = ctxt.resolve_type(qtype.ctype).kind {
280+
// Function pointers are translated to Option applied to the function type
281+
// in order to support NULL function pointers natively
282+
let param = mk().angle_bracketed_args(vec![pointee_ty]);
283+
Ok(mk().path_ty(vec![mk().path_segment_with_args("Option", param)]))
278284
} else {
279-
Mutability::Mutable
280-
};
285+
let mutbl = if qtype.qualifiers.is_const {
286+
Mutability::Immutable
287+
} else {
288+
Mutability::Mutable
289+
};
281290

282-
match ctxt.resolve_type(qtype.ctype).kind {
291+
Ok(mk().set_mutbl(mutbl).ptr_ty(pointee_ty))
292+
}
293+
}
294+
295+
/// Converts the pointee type of a pointer.
296+
pub fn convert_pointee(
297+
&mut self,
298+
ctxt: &TypedAstContext,
299+
ctype: CTypeId,
300+
) -> TranslationResult<Box<Type>> {
301+
match ctxt.resolve_type(ctype).kind {
283302
// While void converts to () in function returns, it converts to c_void
284303
// in the case of pointers.
285-
CTypeKind::Void => Ok(mk()
286-
.set_mutbl(mutbl)
287-
.ptr_ty(mk().abs_path_ty(vec!["core", "ffi", "c_void"]))),
304+
CTypeKind::Void => Ok(mk().abs_path_ty(vec!["core", "ffi", "c_void"])),
288305

289306
CTypeKind::VariableArray(mut elt, _len) => {
290307
while let CTypeKind::VariableArray(elt_, _) = ctxt.resolve_type(elt).kind {
291308
elt = elt_
292309
}
293-
let child_ty = self.convert(ctxt, elt)?;
294-
Ok(mk().set_mutbl(mutbl).ptr_ty(child_ty))
295-
}
296-
297-
// Function pointers are translated to Option applied to the function type
298-
// in order to support NULL function pointers natively
299-
CTypeKind::Function(..) => {
300-
let fn_ty = self.convert(ctxt, qtype.ctype)?;
301-
let param = mk().angle_bracketed_args(vec![fn_ty]);
302-
Ok(mk().path_ty(vec![mk().path_segment_with_args("Option", param)]))
310+
self.convert(ctxt, elt)
303311
}
304312

305-
_ => {
306-
let child_ty = self.convert(ctxt, qtype.ctype)?;
307-
Ok(mk().set_mutbl(mutbl).ptr_ty(child_ty))
308-
}
313+
_ => self.convert(ctxt, ctype),
309314
}
310315
}
311316

c2rust-transpile/src/translator/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,6 +3071,14 @@ impl<'c> Translation<'c> {
30713071
.convert(&self.ast_context, type_id)
30723072
}
30733073

3074+
fn convert_pointee_type(&self, type_id: CTypeId) -> TranslationResult<Box<Type>> {
3075+
self.import_type(type_id);
3076+
3077+
self.type_converter
3078+
.borrow_mut()
3079+
.convert_pointee(&self.ast_context, type_id)
3080+
}
3081+
30743082
/// Construct an expression for a NULL at any type, including forward declarations,
30753083
/// function pointers, and normal pointers.
30763084
fn null_ptr(&self, type_id: CTypeId, is_static: bool) -> TranslationResult<Box<Expr>> {
@@ -3085,13 +3093,8 @@ impl<'c> Translation<'c> {
30853093
let ty = self.convert_type(type_id)?;
30863094
let mut zero = mk().lit_expr(mk().int_unsuffixed_lit(0));
30873095
if is_static && !pointee.qualifiers.is_const {
3088-
let mut qtype = pointee;
3089-
qtype.qualifiers.is_const = true;
3090-
let ty_ = self
3091-
.type_converter
3092-
.borrow_mut()
3093-
.convert_pointer(&self.ast_context, qtype)?;
3094-
zero = mk().cast_expr(zero, ty_);
3096+
let ty_ = self.convert_pointee_type(pointee.ctype)?;
3097+
zero = mk().cast_expr(zero, mk().ptr_ty(ty_));
30953098
}
30963099
Ok(mk().cast_expr(zero, ty))
30973100
}

c2rust-transpile/src/translator/operators.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,13 +1009,8 @@ impl<'c> Translation<'c> {
10091009
// through & to *const to *mut
10101010
addr_of_arg = mk().addr_of_expr(a);
10111011
if let Mutability::Mutable = mutbl {
1012-
let mut qtype = pointee_ty;
1013-
qtype.qualifiers.is_const = true;
1014-
let ty_ = self
1015-
.type_converter
1016-
.borrow_mut()
1017-
.convert_pointer(&self.ast_context, qtype)?;
1018-
addr_of_arg = mk().cast_expr(addr_of_arg, ty_);
1012+
let ty_ = self.convert_pointee_type(pointee_ty.ctype)?;
1013+
addr_of_arg = mk().cast_expr(addr_of_arg, mk().ptr_ty(ty_));
10191014
}
10201015
} else {
10211016
// Normal case is allowed to use &mut if needed

0 commit comments

Comments
 (0)