-
Notifications
You must be signed in to change notification settings - Fork 262
/
Copy pathconvert_type.rs
456 lines (411 loc) · 14.6 KB
/
convert_type.rs
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
use crate::c_ast::CDeclId;
use crate::c_ast::*;
use crate::diagnostics::TranslationResult;
use crate::renamer::*;
use c2rust_ast_builder::{mk, properties::*};
use failure::format_err;
use std::collections::{HashMap, HashSet};
use std::ops::Index;
use syn::*;
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
enum FieldKey {
Field(CFieldId),
Padding(usize),
}
pub struct TypeConverter {
pub translate_valist: bool,
renamer: Renamer<CDeclId>,
fields: HashMap<CDeclId, Renamer<FieldKey>>,
suffix_names: HashMap<(CDeclId, &'static str), String>,
features: HashSet<&'static str>,
}
pub const RESERVED_NAMES: [&str; 103] = [
// Keywords currently in use
"as",
"break",
"const",
"continue",
"crate",
"else",
"enum",
"extern",
"false",
"fn",
"for",
"if",
"impl",
"in",
"let",
"loop",
"match",
"mod",
"move",
"mut",
"pub",
"ref",
"return",
"Self",
"self",
"static",
"struct",
"super",
"trait",
"true",
"type",
"unsafe",
"use",
"where",
"while",
"dyn",
// Keywords reserved for future use
"abstract",
"alignof",
"become",
"box",
"do",
"final",
"macro",
"offsetof",
"override",
"priv",
"proc",
"pure",
"sizeof",
"typeof",
"unsized",
"virtual",
"yield",
"async",
"try",
// Types exported in prelude
"Copy",
"Send",
"Sized",
"Sync",
"Drop",
"Fn",
"FnMut",
"FnOnce",
"Box",
"ToOwned",
"Clone",
"PartialEq",
"PartialOrd",
"Eq",
"Ord",
"AsRef",
"AsMut",
"Into",
"From",
"Default",
"Iterator",
"Extend",
"IntoIterator",
"DoubleEndedIterator",
"ExactSizeIterator",
"Option",
"Result",
"SliceConcatExt",
"String",
"ToString",
"Vec",
"bool",
"char",
"f32",
"f64",
"i8",
"i16",
"i32",
"i64",
"i128",
"isize",
"u8",
"u16",
"u32",
"u64",
"u128",
"usize",
"str",
];
impl TypeConverter {
// We don't provide a `Default` impl to simplify future compatibility:
// if `TypeConverter` ever gets fields incompatible with `Default`, then
// cleaning out the uses of `impl Default for TypeConverter` can be a pain.
// More practically, there is a single use of `TypeConverter::new` and no
// current plans to use a `Default` impl, so providing it isn't worth the
// potential breakage.
#[allow(clippy::new_without_default)]
pub fn new() -> TypeConverter {
TypeConverter {
translate_valist: false,
renamer: Renamer::new(&RESERVED_NAMES),
fields: HashMap::new(),
suffix_names: HashMap::new(),
features: HashSet::new(),
}
}
pub fn features_used(&self) -> &HashSet<&'static str> {
&self.features
}
pub fn declare_decl_name(&mut self, decl_id: CDeclId, name: &str) -> String {
self.renamer
.insert(decl_id, name)
.expect("Name already assigned")
}
pub fn alias_decl_name(&mut self, new_decl_id: CDeclId, old_decl_id: CDeclId) {
self.renamer.alias(new_decl_id, &old_decl_id)
}
pub fn resolve_decl_name(&self, decl_id: CDeclId) -> Option<String> {
self.renamer.get(&decl_id)
}
pub fn resolve_decl_suffix_name(&mut self, decl_id: CDeclId, suffix: &'static str) -> &str {
let key = (decl_id, suffix);
self.suffix_names.entry(key).or_insert_with(|| {
let mut suffix_name = self
.renamer
.get(&decl_id)
.unwrap_or_else(|| "C2RustUnnamed".to_string());
suffix_name += suffix;
self.renamer.pick_name(&suffix_name)
})
}
pub fn declare_field_name(
&mut self,
record_id: CRecordId,
field_id: CFieldId,
name: &str,
) -> String {
let name = if name.is_empty() {
"c2rust_unnamed"
} else {
name
};
self.fields
.entry(record_id)
.or_insert_with(|| Renamer::new(&RESERVED_NAMES))
.insert(FieldKey::Field(field_id), name)
.expect("Field already declared")
}
pub fn declare_padding(&mut self, record_id: CRecordId, padding_idx: usize) -> String {
let field = self
.fields
.entry(record_id)
.or_insert_with(|| Renamer::new(&RESERVED_NAMES));
let key = FieldKey::Padding(padding_idx);
if let Some(name) = field.get(&key) {
name
} else {
field.insert(key, "c2rust_padding").unwrap()
}
}
/** Resolve the Rust name associated with a field declaration. The optional record_id
is used as a hint to speed up the process of finding the field's name.
*/
pub fn resolve_field_name(
&self,
record_id: Option<CRecordId>,
field_id: CFieldId,
) -> Option<String> {
let key = FieldKey::Field(field_id);
match record_id {
Some(record_id) => self.fields.get(&record_id).and_then(|x| x.get(&key)),
None => self.fields.values().flat_map(|x| x.get(&key)).next(),
}
}
/// Helper function handling conversion of function types in `convert`.
/// Optional return type excludes a ty when a function doesn't return.
pub fn convert_function(
&mut self,
ctxt: &TypedAstContext,
ret: Option<CQualTypeId>,
params: &[CQualTypeId],
is_variadic: bool,
) -> TranslationResult<Box<Type>> {
let barefn_inputs = params
.iter()
.map(|x| mk().bare_arg(self.convert(ctxt, x.ctype).unwrap(), None::<Box<Ident>>))
.collect::<Vec<_>>();
let output = match ret {
None => mk().never_ty(),
Some(ret) => self.convert(ctxt, ret.ctype)?,
};
let variadic = is_variadic.then(|| mk().variadic_arg(vec![]));
let fn_ty = (
barefn_inputs,
variadic,
ReturnType::Type(Default::default(), output),
);
Ok(mk().unsafe_().extern_("C").barefn_ty(fn_ty))
}
pub fn convert_pointer(
&mut self,
ctxt: &TypedAstContext,
qtype: CQualTypeId,
) -> TranslationResult<Box<Type>> {
let mutbl = if qtype.qualifiers.is_const {
Mutability::Immutable
} else {
Mutability::Mutable
};
match ctxt.resolve_type(qtype.ctype).kind {
// While void converts to () in function returns, it converts to c_void
// in the case of pointers.
CTypeKind::Void => Ok(mk()
.set_mutbl(mutbl)
.ptr_ty(mk().path_ty(vec!["libc", "c_void"]))),
CTypeKind::VariableArray(mut elt, _len) => {
while let CTypeKind::VariableArray(elt_, _) = ctxt.resolve_type(elt).kind {
elt = elt_
}
let child_ty = self.convert(ctxt, elt)?;
Ok(mk().set_mutbl(mutbl).ptr_ty(child_ty))
}
// Function pointers are translated to Option applied to the function type
// in order to support NULL function pointers natively
CTypeKind::Function(..) => {
let fn_ty = self.convert(ctxt, qtype.ctype)?;
let param = mk().angle_bracketed_args(vec![fn_ty]);
Ok(mk().path_ty(vec![mk().path_segment_with_args("Option", param)]))
}
_ => {
let child_ty = self.convert(ctxt, qtype.ctype)?;
Ok(mk().set_mutbl(mutbl).ptr_ty(child_ty))
}
}
}
/// Convert a `C` type to a `Rust` one. For the moment, these are expected to have compatible
/// memory layouts.
pub fn convert(
&mut self,
ctxt: &TypedAstContext,
ctype: CTypeId,
) -> TranslationResult<Box<Type>> {
if self.translate_valist && ctxt.is_va_list(ctype) {
let path = vec!["core", "ffi", "VaList"];
let ty = mk().path_ty(mk().abs_path(path));
return Ok(ty);
}
match ctxt.index(ctype).kind {
CTypeKind::Void => Ok(mk().tuple_ty(vec![])),
CTypeKind::Bool => Ok(mk().path_ty(mk().path(vec!["bool"]))),
CTypeKind::Short => Ok(mk().path_ty(mk().path(vec!["libc", "c_short"]))),
CTypeKind::Int => Ok(mk().path_ty(mk().path(vec!["libc", "c_int"]))),
CTypeKind::Long => Ok(mk().path_ty(mk().path(vec!["libc", "c_long"]))),
CTypeKind::LongLong => Ok(mk().path_ty(mk().path(vec!["libc", "c_longlong"]))),
CTypeKind::UShort => Ok(mk().path_ty(mk().path(vec!["libc", "c_ushort"]))),
CTypeKind::UInt => Ok(mk().path_ty(mk().path(vec!["libc", "c_uint"]))),
CTypeKind::ULong => Ok(mk().path_ty(mk().path(vec!["libc", "c_ulong"]))),
CTypeKind::ULongLong => Ok(mk().path_ty(mk().path(vec!["libc", "c_ulonglong"]))),
CTypeKind::SChar => Ok(mk().path_ty(mk().path(vec!["libc", "c_schar"]))),
CTypeKind::UChar => Ok(mk().path_ty(mk().path(vec!["libc", "c_uchar"]))),
CTypeKind::Char => Ok(mk().path_ty(mk().path(vec!["libc", "c_char"]))),
CTypeKind::Double => Ok(mk().path_ty(mk().path(vec!["libc", "c_double"]))),
CTypeKind::LongDouble => Ok(mk().path_ty(mk().path(vec!["f128", "f128"]))),
CTypeKind::Float => Ok(mk().path_ty(mk().path(vec!["libc", "c_float"]))),
CTypeKind::Int128 => Ok(mk().path_ty(mk().path(vec!["i128"]))),
CTypeKind::UInt128 => Ok(mk().path_ty(mk().path(vec!["u128"]))),
CTypeKind::BFloat16 => Ok(mk().path_ty(mk().path(vec!["bf16"]))),
CTypeKind::Pointer(qtype) => self.convert_pointer(ctxt, qtype),
CTypeKind::Elaborated(ref ctype) => self.convert(ctxt, *ctype),
CTypeKind::Decayed(ref ctype) => self.convert(ctxt, *ctype),
CTypeKind::Paren(ref ctype) => self.convert(ctxt, *ctype),
CTypeKind::Struct(decl_id) => {
let new_name = self
.resolve_decl_name(decl_id)
.ok_or_else(|| format_err!("Unknown decl id {:?}", decl_id))?;
Ok(mk().path_ty(mk().path(vec![new_name])))
}
CTypeKind::Union(decl_id) => {
let new_name = self.resolve_decl_name(decl_id).unwrap();
Ok(mk().path_ty(mk().path(vec![new_name])))
}
CTypeKind::Enum(decl_id) => {
let new_name = self.resolve_decl_name(decl_id).unwrap();
Ok(mk().path_ty(mk().path(vec![new_name])))
}
CTypeKind::Typedef(decl_id) => {
let new_name = self.resolve_decl_name(decl_id).unwrap();
Ok(mk().path_ty(mk().path(vec![new_name])))
}
CTypeKind::ConstantArray(element, count) => {
let ty = self.convert(ctxt, element)?;
Ok(mk().array_ty(ty, mk().lit_expr(mk().int_unsuffixed_lit(count as u128))))
}
CTypeKind::IncompleteArray(element) => {
let ty = self.convert(ctxt, element)?;
let zero_lit = mk().int_unsuffixed_lit(0);
let zero = mk().lit_expr(zero_lit);
Ok(mk().array_ty(ty, zero))
}
CTypeKind::VariableArray(mut elt, _) => {
while let CTypeKind::VariableArray(elt_, _) = ctxt.resolve_type(elt).kind {
elt = elt_
}
let child_ty = self.convert(ctxt, elt)?;
Ok(mk().mutbl().ptr_ty(child_ty))
}
CTypeKind::Attributed(ty, _) => self.convert(ctxt, ty.ctype),
// ANSI/ISO C-style function
CTypeKind::Function(ret, ref params, is_var, is_noreturn, true) => {
let opt_ret = if is_noreturn { None } else { Some(ret) };
let fn_ty = self.convert_function(ctxt, opt_ret, params, is_var)?;
Ok(fn_ty)
}
// K&R-style function
CTypeKind::Function(ret, _, is_var, is_noreturn, false) => {
let opt_ret = if is_noreturn { None } else { Some(ret) };
let fn_ty = self.convert_function(ctxt, opt_ret, &[], is_var)?;
Ok(fn_ty)
}
CTypeKind::TypeOf(ty) => self.convert(ctxt, ty),
ref t => Err(format_err!("Unsupported type {:?}", t).into()),
}
}
/// Add the given parameters to a K&R function pointer type,
/// returning a full signature or `None` if the function isn't K&R.
pub fn knr_function_type_with_parameters(
&mut self,
ctxt: &TypedAstContext,
ctype: CTypeId,
params: &Vec<CParamId>,
) -> TranslationResult<Option<Box<Type>>> {
match ctxt.index(ctype).kind {
// ANSI/ISO C-style function
CTypeKind::Function(.., true) => Ok(None),
// K&R-style function
CTypeKind::Function(ret, ref _params, is_var, is_noreturn, false) => {
// _params is empty here -> get params from function definition instead
let params = params
.iter()
.map(|p| {
let decl = &ctxt.get_decl(p).unwrap().kind;
match decl {
CDeclKind::Variable { typ, .. } => *typ,
_ => panic!("parameter referenced non-variable decl."),
}
})
.collect::<Vec<_>>();
let opt_ret = if is_noreturn { None } else { Some(ret) };
let fn_ty = self.convert_function(ctxt, opt_ret, ¶ms, is_var)?;
Ok(Some(fn_ty))
}
CTypeKind::Elaborated(ref ctype) => {
self.knr_function_type_with_parameters(ctxt, *ctype, params)
}
CTypeKind::Decayed(ref ctype) => {
self.knr_function_type_with_parameters(ctxt, *ctype, params)
}
CTypeKind::Paren(ref ctype) => {
self.knr_function_type_with_parameters(ctxt, *ctype, params)
}
CTypeKind::TypeOf(ty) => self.knr_function_type_with_parameters(ctxt, ty, params),
CTypeKind::Typedef(decl) => match &ctxt.index(decl).kind {
CDeclKind::Typedef { typ, .. } => {
self.knr_function_type_with_parameters(ctxt, typ.ctype, params)
}
_ => panic!("Typedef decl did not point to a typedef"),
},
ref kind => panic!("ctype parameter must be a function instead of {:?}", kind),
}
}
}