-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathpointers.rs
More file actions
575 lines (512 loc) · 22.1 KB
/
Copy pathpointers.rs
File metadata and controls
575 lines (512 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use c2rust_ast_builder::{mk, properties::Mutability};
use c2rust_ast_exporter::clang_ast::LRValue;
use failure::{err_msg, format_err};
use syn::{BinOp, Expr, Type, UnOp};
use crate::c_ast::CUnOp;
use crate::{
diagnostics::{TranslationError, TranslationErrorKind, TranslationResult},
format_translation_err,
translator::{
cast_int, neg_expr, transmute_expr, unwrap_function_pointer, ExprContext, Translation,
},
with_stmts::WithStmts,
CExprId, CExprKind, CLiteral, CQualTypeId, CTypeId, CTypeKind, CastKind, ExternCrate,
};
impl<'c> Translation<'c> {
pub fn convert_address_of(
&self,
mut ctx: ExprContext,
cqual_type: CQualTypeId,
arg: CExprId,
) -> TranslationResult<WithStmts<Box<Expr>>> {
let arg_kind = &self.ast_context.index_unwrap_parens(arg).kind;
match arg_kind {
// C99 6.5.3.2 para 4
CExprKind::Unary(_, CUnOp::Deref, target, _) => {
return self.convert_expr(ctx, *target, None)
}
// Array subscript functions as a deref too.
&CExprKind::ArraySubscript(_, lhs, rhs, _) => {
return self.convert_array_subscript(
ctx.used().set_needs_address(true),
lhs,
rhs,
LRValue::RValue, // if we bypass the deref, we stay an RValue
Some(cqual_type),
false, // don't deref, keep as pointer
);
}
// An AddrOf DeclRef/Member is safe to not decay
// if the translator isn't already giving a hard yes to decaying (ie, BitCasts).
// So we only convert default to no decay.
CExprKind::DeclRef(..) | CExprKind::Member(..) => ctx.decay_ref.set_default_to_no(),
_ => (),
}
let val = self.convert_expr(ctx.used().set_needs_address(true), arg, None)?;
// & becomes a no-op when applied to a function.
if self.ast_context.is_function_pointer(cqual_type.ctype) {
return Ok(val.map(|x| mk().call_expr(mk().ident_expr("Some"), vec![x])));
}
let arg_cty = arg_kind
.get_qual_type()
.ok_or_else(|| format_err!("bad source type"))?;
self.convert_address_of_common(ctx, Some(arg), arg_cty, cqual_type, val, false)
}
pub fn convert_array_to_pointer_decay(
&self,
ctx: ExprContext,
source_cty: CQualTypeId,
target_cty: CQualTypeId,
val: WithStmts<Box<Expr>>,
expr: Option<CExprId>,
) -> TranslationResult<WithStmts<Box<Expr>>> {
// Because va_list is sometimes defined as a single-element
// array in order for it to allocate memory as a local variable
// and to be a pointer as a function argument we would get
// spurious casts when trying to treat it like a VaList which
// has reference semantics.
if self.ast_context.is_va_list(target_cty.ctype) {
return Ok(val);
}
let source_ty_kind = &self.ast_context.resolve_type(source_cty.ctype).kind;
// Variable length arrays are already represented as pointers.
if let CTypeKind::VariableArray(..) = source_ty_kind {
return Ok(val);
}
self.convert_address_of_common(ctx, expr, source_cty, target_cty, val, true)
}
fn convert_address_of_common(
&self,
ctx: ExprContext,
arg: Option<CExprId>,
arg_cty: CQualTypeId,
pointer_cty: CQualTypeId,
mut val: WithStmts<Box<Expr>>,
is_array_decay: bool,
) -> TranslationResult<WithStmts<Box<Expr>>> {
let arg_expr_kind = arg.map(|arg| {
let arg = self.ast_context.unwrap_predefined_ident(arg);
&self.ast_context.index_unwrap_parens(arg).kind
});
let pointee_cty = self
.ast_context
.get_pointee_qual_type(pointer_cty.ctype)
.ok_or_else(|| TranslationError::generic("Address-of should return a pointer"))?;
let arg_is_macro = arg.map_or(false, |arg| self.expr_is_expanded_macro(ctx, arg, None));
let mut needs_cast = false;
let mut ref_cast_pointee_ty = None;
let mutbl = if pointee_cty.qualifiers.is_const {
Mutability::Immutable
} else if ctx.is_const {
// const contexts aren't able to use &mut, so we work around that
// by using & and an extra cast through & to *const to *mut
// TODO: Rust 1.83: Allowed, so this can be removed.
needs_cast = true;
Mutability::Immutable
} else {
Mutability::Mutable
};
// Narrow string literals are translated directly as `[u8; N]` literals when their address
// is taken, without the transmute. String/byte literals are already references in Rust.
if let (
Some(&CExprKind::Literal(literal_cty, CLiteral::String(_, element_size @ 1))),
false,
) = (arg_expr_kind, arg_is_macro)
{
if is_array_decay {
val = val.map(|val| mk().method_call_expr(val, "as_ptr", vec![]));
} else {
let size = self.ast_context.array_len(literal_cty.ctype) * element_size as usize;
ref_cast_pointee_ty =
Some(mk().array_ty(mk().ident_ty("u8"), mk().lit_expr(size as u128)));
}
needs_cast = true;
}
// Values that translate into const temporaries can't be raw-borrowed in Rust.
// They must be regular-borrowed first, which will extend the lifetime to static.
else if arg_is_macro || matches!(arg_expr_kind, Some(CExprKind::Literal(..))) {
let arg_cty_kind = &self.ast_context.resolve_type(arg_cty.ctype).kind;
if is_array_decay {
let method = match mutbl {
Mutability::Mutable => "as_mut_ptr",
Mutability::Immutable => "as_ptr",
};
val = val.map(|val| mk().method_call_expr(val, method, vec![]));
// If the target pointee type is different from the source element type,
// then we need to cast the ptr type as well.
if arg_cty_kind.element_ty().map_or(false, |arg_element_cty| {
arg_element_cty != pointee_cty.ctype
}) {
needs_cast = true;
}
} else {
val = val.map(|val| mk().set_mutbl(mutbl).borrow_expr(val));
// Add an intermediate reference-to-pointer cast if the context needs
// reference-to-pointer decay, or if another cast follows.
if ctx.decay_ref.is_yes() || needs_cast {
ref_cast_pointee_ty = Some(self.convert_pointee_type(arg_cty.ctype)?);
}
}
} else {
self.use_feature("raw_ref_op");
val = val.map(|val| mk().set_mutbl(mutbl).raw_borrow_expr(val));
if is_array_decay {
// TODO: Call `ptr::as_[mut]_ptr` instead once that is available.
// (`array_ptr_get` feature added to nightly in January 2024)
needs_cast = true;
}
}
// Perform an intermediate reference-to-pointer cast if needed.
// TODO: Rust 1.76: Use `ptr::from_ref`.
if let Some(pointee_ty) = ref_cast_pointee_ty {
val = val.map(|val| mk().cast_expr(val, mk().set_mutbl(mutbl).ptr_ty(pointee_ty)));
}
// Perform a final cast to the target type if needed.
if needs_cast {
let pointer_ty = self.convert_type(pointer_cty.ctype)?;
val = val.map(|val| mk().cast_expr(val, pointer_ty));
}
Ok(val)
}
pub fn convert_deref(
&self,
ctx: ExprContext,
cqual_type: CQualTypeId,
arg: CExprId,
) -> TranslationResult<WithStmts<Box<Expr>>> {
let arg_expr_kind = &self.ast_context.index_unwrap_parens(arg).kind;
if let &CExprKind::Unary(_, CUnOp::AddressOf, arg, _) = arg_expr_kind {
return self.convert_expr(ctx.used(), arg, None);
}
self.convert_expr(ctx.used(), arg, None)?
.try_map(|val: Box<Expr>| {
if let CTypeKind::Function(..) =
self.ast_context.resolve_type(cqual_type.ctype).kind
{
Ok(unwrap_function_pointer(val))
} else if let Some(_vla) = self.compute_size_of_expr(cqual_type.ctype) {
Ok(val)
} else {
Ok(mk().unary_expr(UnOp::Deref(Default::default()), val))
}
})
}
pub fn convert_array_subscript(
&self,
ctx: ExprContext,
lhs: CExprId,
rhs: CExprId,
lrvalue: LRValue,
override_ty: Option<CQualTypeId>,
deref: bool,
) -> TranslationResult<WithStmts<Box<Expr>>> {
let lhs_node = &self.ast_context.index_unwrap_parens(lhs).kind;
let rhs_node = &self.ast_context.index_unwrap_parens(rhs).kind;
let lhs_node_type = lhs_node
.get_type()
.ok_or_else(|| format_err!("lhs node bad type"))?;
let lhs_node_kind = &self.ast_context.resolve_type(lhs_node_type).kind;
let lhs_is_indexable = lhs_node_kind.is_pointer() || lhs_node_kind.is_vector();
// From here on in, the LHS is the pointer/array and the RHS the index
let (lhs, rhs, lhs_node) = if lhs_is_indexable {
(lhs, rhs, lhs_node)
} else {
(rhs, lhs, rhs_node)
};
let lhs_node_type = lhs_node
.get_type()
.ok_or_else(|| format_err!("lhs node bad type"))?;
if self
.ast_context
.resolve_type(lhs_node_type)
.kind
.is_vector()
{
return Err(TranslationError::from(
err_msg("Attempting to index a vector type")
.context(TranslationErrorKind::OldLLVMSimd),
));
}
let rhs = self.convert_expr(ctx.used(), rhs, None)?;
rhs.and_then_try(|rhs| {
let simple_index_array = if ctx.needs_address() {
// We can't necessarily index into an array if we're using
// that element to compute an address.
None
} else {
match lhs_node {
&CExprKind::ImplicitCast(_, arr, CastKind::ArrayToPointerDecay, _, _) => {
match self.ast_context.index_unwrap_parens(arr).kind {
CExprKind::Member(_, _, field_decl, _, _)
if self
.potential_flexible_array_members
.borrow()
.contains(&field_decl) =>
{
None
}
ref kind => {
let arr_type =
kind.get_type().ok_or_else(|| format_err!("bad arr type"))?;
match self.ast_context.resolve_type(arr_type).kind {
// These get translated to 0-element arrays, this avoids the bounds check
// that using an array subscript in Rust would cause
CTypeKind::IncompleteArray(_) => None,
_ => Some(arr),
}
}
}
}
_ => None,
}
};
if let Some(arr) = simple_index_array {
// If the LHS just underwent an implicit cast from array to pointer, bypass that
// to make an actual Rust indexing operation
let t = self
.ast_context
.index_unwrap_parens(arr)
.kind
.get_type()
.ok_or_else(|| format_err!("bad arr type"))?;
let var_elt_type_id = match self.ast_context.resolve_type(t).kind {
CTypeKind::ConstantArray(..) => None,
CTypeKind::IncompleteArray(..) => None,
CTypeKind::VariableArray(elt, _) => Some(elt),
ref other => panic!("Unexpected array type {:?}", other),
};
let lhs = self.convert_expr(ctx.used(), arr, None)?;
Ok(lhs.and_then(|lhs| {
// stmts.extend(lhs.stmts_mut());
// is_unsafe = is_unsafe || lhs.is_unsafe();
// Don't dereference the offset if we're still within the variable portion
if let Some(elt_type_id) = var_elt_type_id {
self.convert_pointer_offset(lhs, rhs, elt_type_id, false, deref)
} else {
WithStmts::new_val(mk().index_expr(lhs, cast_int(rhs, "usize", false)))
}
}))
} else {
// LHS must be ref decayed for the offset method call's self param
let lhs = self.convert_expr(ctx.used().decay_ref(), lhs, None)?;
lhs.and_then_try(|lhs| {
// stmts.extend(lhs.stmts_mut());
// is_unsafe = is_unsafe || lhs.is_unsafe();
let lhs_type_id = lhs_node
.get_type()
.ok_or_else(|| format_err!("bad lhs type"))?;
// Determine the type of element being indexed
let pointee_type_id = match self.ast_context.resolve_type(lhs_type_id).kind {
CTypeKind::Pointer(pointee_id) => pointee_id,
_ => {
return Err(
format_err!("Subscript applied to non-pointer: {:?}", lhs).into()
);
}
};
let mut val =
self.convert_pointer_offset(lhs, rhs, pointee_type_id.ctype, false, deref);
// if the context wants a different type, add a cast
if let Some(expected_ty) = override_ty {
if lrvalue.is_rvalue() && expected_ty != pointee_type_id {
let ty = self.convert_type(expected_ty.ctype)?;
val = val.map(|val| mk().cast_expr(val, ty));
}
}
Ok(val)
})
}
})
}
/// Pointer offset that casts its argument to isize
pub fn convert_pointer_offset(
&self,
ptr: Box<Expr>,
offset: Box<Expr>,
pointee_cty: CTypeId,
neg: bool,
mut deref: bool,
) -> WithStmts<Box<Expr>> {
let mut offset = cast_int(offset, "isize", false);
if let Some(mul) = self.compute_size_of_expr(pointee_cty) {
let mul = cast_int(mul, "isize", false);
offset = mk().binary_expr(BinOp::Mul(Default::default()), offset, mul);
deref = false;
}
if neg {
offset = neg_expr(offset);
}
let mut res = mk().method_call_expr(ptr, "offset", vec![offset]);
if deref {
res = mk().unary_expr(UnOp::Deref(Default::default()), res);
}
WithStmts::new_val(res).set_unsafe()
}
/// Creates a pointer difference expression. Returns an expression of type `isize`.
pub fn make_pointer_difference(
&self,
lhs_rs: Box<Expr>,
rhs_rs: Box<Expr>,
pointee_type_id: CTypeId,
) -> WithStmts<Box<Expr>> {
let mut expr_rs = mk().method_call_expr(lhs_rs, "offset_from", vec![rhs_rs]);
if let Some(sz) = self.compute_size_of_expr(pointee_type_id) {
let div_rs = cast_int(sz, "isize", false);
expr_rs = mk().binary_expr(BinOp::Div(Default::default()), expr_rs, div_rs);
}
WithStmts::new_val(expr_rs).set_unsafe()
}
/// Construct an expression for a NULL at any type, including forward declarations,
/// function pointers, and normal pointers.
pub fn null_ptr(&self, type_id: CTypeId) -> TranslationResult<Box<Expr>> {
if self.ast_context.is_function_pointer(type_id) {
return Ok(mk().path_expr(vec!["None"]));
}
let pointer_qty = self
.ast_context
.get_pointee_qual_type(type_id)
.ok_or_else(|| TranslationError::generic("null_ptr requires a pointer"))?;
let func = if pointer_qty.qualifiers.is_const {
"null"
} else {
"null_mut"
};
let pointee_ty = self.convert_pointee_type(pointer_qty.ctype)?;
let type_args = mk().angle_bracketed_args(vec![pointee_ty.clone()]);
let val = mk().call_expr(
mk().abs_path_expr(vec![
mk().path_segment("core"),
mk().path_segment("ptr"),
mk().path_segment_with_args(func, type_args),
]),
vec![],
);
Ok(val)
}
fn convert_pointee_type(&self, type_id: CTypeId) -> TranslationResult<Box<Type>> {
self.import_type(type_id);
self.type_converter
.borrow_mut()
.convert_pointee(&self.ast_context, type_id)
}
pub fn convert_pointer_to_pointer_cast(
&self,
source_cty: CTypeId,
target_cty: CTypeId,
val: WithStmts<Box<Expr>>,
) -> TranslationResult<WithStmts<Box<Expr>>> {
if self.ast_context.is_function_pointer(target_cty)
|| self.ast_context.is_function_pointer(source_cty)
{
let source_ty = self
.type_converter
.borrow_mut()
.convert(&self.ast_context, source_cty)?;
let target_ty = self
.type_converter
.borrow_mut()
.convert(&self.ast_context, target_cty)?;
if source_ty == target_ty {
return Ok(val);
}
self.import_type(source_cty);
self.import_type(target_cty);
Ok(val.and_then(|val| {
WithStmts::new_val(transmute_expr(source_ty, target_ty, val)).set_unsafe()
}))
} else {
// Normal case
let target_ty = self.convert_type(target_cty)?;
Ok(val.map(|val| mk().cast_expr(val, target_ty)))
}
}
pub fn convert_integral_to_pointer_cast(
&self,
ctx: ExprContext,
source_cty: CTypeId,
target_cty: CTypeId,
val: WithStmts<Box<Expr>>,
) -> TranslationResult<WithStmts<Box<Expr>>> {
let source_ty_kind = &self.ast_context.resolve_type(source_cty).kind;
let target_ty = self.convert_type(target_cty)?;
if self.ast_context.is_function_pointer(target_cty) {
if ctx.is_const {
return Err(format_translation_err!(
None,
"cannot transmute integers to Option<fn ...> in `const` context",
));
}
self.use_crate(ExternCrate::Libc);
Ok(val.and_then(|mut val| {
// First cast the integer to pointer size
let intptr_t = mk().abs_path_ty(vec!["libc", "intptr_t"]);
val = mk().cast_expr(val, intptr_t.clone());
WithStmts::new_val(transmute_expr(intptr_t, target_ty, val)).set_unsafe()
}))
} else if source_ty_kind.is_bool() {
self.use_crate(ExternCrate::Libc);
Ok(val.map(|mut val| {
// First cast the boolean to pointer size
val = mk().cast_expr(val, mk().abs_path_ty(vec!["libc", "size_t"]));
mk().cast_expr(val, target_ty)
}))
} else if let &CTypeKind::Enum(..) = source_ty_kind {
val.try_map(|val| self.convert_cast_from_enum(target_cty, val))
} else {
Ok(val.map(|val| mk().cast_expr(val, target_ty)))
}
}
pub fn convert_pointer_to_integral_cast(
&self,
ctx: ExprContext,
source_cty: CTypeId,
target_cty: CTypeId,
val: WithStmts<Box<Expr>>,
expr: Option<CExprId>,
) -> TranslationResult<WithStmts<Box<Expr>>> {
if ctx.is_const {
return Err(format_translation_err!(
None,
"cannot observe pointer values in `const` context",
));
}
let target_ty = self.convert_type(target_cty)?;
let source_ty = self.convert_type(source_cty)?;
let target_ty_kind = &self.ast_context.resolve_type(target_cty).kind;
if self.ast_context.is_function_pointer(source_cty) {
Ok(val.and_then(|val| {
WithStmts::new_val(transmute_expr(source_ty, target_ty, val)).set_unsafe()
}))
} else if let &CTypeKind::Enum(enum_decl_id) = target_ty_kind {
val.try_map(|val| self.convert_cast_to_enum(ctx, target_cty, enum_decl_id, expr, val))
} else {
Ok(val.map(|val| mk().cast_expr(val, target_ty)))
}
}
pub fn convert_pointer_is_null(
&self,
ctx: ExprContext,
ptr_type: CTypeId,
val: Box<Expr>,
is_null: bool,
) -> TranslationResult<Box<Expr>> {
Ok(if self.ast_context.is_function_pointer(ptr_type) {
let method = if is_null { "is_none" } else { "is_some" };
mk().method_call_expr(val, method, vec![])
} else {
// TODO: `pointer::is_null` becomes stably const in Rust 1.84.
if ctx.is_const {
return Err(format_translation_err!(
None,
"cannot check nullity of pointer in `const` context",
));
}
let val = mk().method_call_expr(val, "is_null", vec![]);
if !is_null {
mk().unary_expr(UnOp::Not(Default::default()), val)
} else {
val
}
})
}
}