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
|
//! Helpers for code generation that don't need macro expansion.
use aster;
use ir::layout::Layout;
use syntax::ast;
use syntax::ptr::P;
pub mod attributes {
use aster;
use syntax::ast;
pub fn repr(which: &str) -> ast::Attribute {
aster::AstBuilder::new().attr().list("repr").words(&[which]).build()
}
pub fn repr_list(which_ones: &[&str]) -> ast::Attribute {
aster::AstBuilder::new().attr().list("repr").words(which_ones).build()
}
pub fn derives(which_ones: &[&str]) -> ast::Attribute {
aster::AstBuilder::new().attr().list("derive").words(which_ones).build()
}
pub fn inline() -> ast::Attribute {
aster::AstBuilder::new().attr().word("inline")
}
pub fn doc(comment: &str) -> ast::Attribute {
aster::AstBuilder::new().attr().doc(comment)
}
pub fn link_name(name: &str) -> ast::Attribute {
aster::AstBuilder::new().attr().name_value("link_name").str(name)
}
}
/// Generates a proper type for a field or type with a given `Layout`, that is,
/// a type with the correct size and alignment restrictions.
pub struct BlobTyBuilder {
layout: Layout,
}
impl BlobTyBuilder {
pub fn new(layout: Layout) -> Self {
BlobTyBuilder {
layout: layout,
}
}
pub fn build(self) -> P<ast::Ty> {
use std::cmp;
let ty_name = match self.layout.align {
8 => "u64",
4 => "u32",
2 => "u16",
1 | _ => "u8",
};
let data_len = if ty_name == "u8" {
self.layout.size
} else {
self.layout.size / cmp::max(self.layout.align, 1)
};
let inner_ty = aster::AstBuilder::new().ty().path().id(ty_name).build();
if data_len == 1 {
inner_ty
} else {
aster::ty::TyBuilder::new().array(data_len).build(inner_ty)
}
}
}
pub mod ast_ty {
use aster;
use ir::context::BindgenContext;
use ir::function::FunctionSig;
use ir::ty::FloatKind;
use syntax::ast;
use syntax::ptr::P;
pub fn raw_type(ctx: &BindgenContext, name: &str) -> P<ast::Ty> {
let ident = ctx.rust_ident_raw(&name);
match ctx.options().ctypes_prefix {
Some(ref prefix) => {
let prefix = ctx.rust_ident_raw(prefix);
quote_ty!(ctx.ext_cx(), $prefix::$ident)
}
None => quote_ty!(ctx.ext_cx(), ::std::os::raw::$ident),
}
}
pub fn float_kind_rust_type(ctx: &BindgenContext,
fk: FloatKind)
-> P<ast::Ty> {
// TODO: we probably should just take the type layout into
// account?
//
// Also, maybe this one shouldn't be the default?
//
// FIXME: `c_longdouble` doesn't seem to be defined in some
// systems, so we use `c_double` directly.
match (fk, ctx.options().convert_floats) {
(FloatKind::Float, true) => aster::ty::TyBuilder::new().f32(),
(FloatKind::Double, true) |
(FloatKind::LongDouble, true) => aster::ty::TyBuilder::new().f64(),
(FloatKind::Float, false) => raw_type(ctx, "c_float"),
(FloatKind::Double, false) |
(FloatKind::LongDouble, false) => raw_type(ctx, "c_double"),
(FloatKind::Float128, _) => {
aster::ty::TyBuilder::new().array(16).u8()
}
}
}
pub fn int_expr(val: i64) -> P<ast::Expr> {
use std::i64;
let expr = aster::AstBuilder::new().expr();
// This is not representable as an i64 if it's negative, so we
// special-case it.
//
// Fix in aster incoming.
if val == i64::MIN {
expr.neg().uint(1u64 << 63)
} else {
expr.int(val)
}
}
pub fn bool_expr(val: bool) -> P<ast::Expr> {
aster::AstBuilder::new().expr().bool(val)
}
pub fn byte_array_expr(bytes: &[u8]) -> P<ast::Expr> {
let mut vec = Vec::with_capacity(bytes.len() + 1);
for byte in bytes {
vec.push(int_expr(*byte as i64));
}
vec.push(int_expr(0));
let kind = ast::ExprKind::Vec(vec);
aster::AstBuilder::new().expr().build_expr_kind(kind)
}
pub fn cstr_expr(mut string: String) -> P<ast::Expr> {
string.push('\0');
aster::AstBuilder::new()
.expr()
.build_lit(aster::AstBuilder::new().lit().byte_str(string))
}
pub fn float_expr(f: f64) -> P<ast::Expr> {
use aster::symbol::ToSymbol;
let mut string = f.to_string();
// So it gets properly recognised as a floating point constant.
if !string.contains('.') {
string.push('.');
}
let kind = ast::LitKind::FloatUnsuffixed(string.as_str().to_symbol());
aster::AstBuilder::new().expr().lit().build_lit(kind)
}
pub fn arguments_from_signature(signature: &FunctionSig,
ctx: &BindgenContext)
-> Vec<P<ast::Expr>> {
// TODO: We need to keep in sync the argument names, so we should unify
// this with the other loop that decides them.
let mut unnamed_arguments = 0;
signature.argument_types()
.iter()
.map(|&(ref name, _ty)| {
let arg_name = match *name {
Some(ref name) => ctx.rust_mangle(name).into_owned(),
None => {
unnamed_arguments += 1;
format!("arg{}", unnamed_arguments)
}
};
aster::expr::ExprBuilder::new().id(arg_name)
})
.collect::<Vec<_>>()
}
}
|