| 1 |
use std::io::Write; |
| 2 |
|
| 3 |
use crate::callbacks::IntKind; |
| 4 |
|
| 5 |
use crate::ir::comp::CompKind; |
| 6 |
use crate::ir::context::{BindgenContext, TypeId}; |
| 7 |
use crate::ir::function::{Function, FunctionKind}; |
| 8 |
use crate::ir::item::Item; |
| 9 |
use crate::ir::item::ItemCanonicalName; |
| 10 |
use crate::ir::item_kind::ItemKind; |
| 11 |
use crate::ir::ty::{FloatKind, Type, TypeKind}; |
| 12 |
|
| 13 |
use super::CodegenError; |
| 14 |
|
| 15 |
fn get_loc(item: &Item) -> String { |
| 16 |
item.location() |
| 17 |
.map(|x| x.to_string()) |
| 18 |
.unwrap_or_else(|| "unknown".to_owned()) |
| 19 |
} |
| 20 |
|
| 21 |
pub(crate) trait CSerialize<'a> { |
| 22 |
type Extra; |
| 23 |
|
| 24 |
fn serialize<W: Write>( |
| 25 |
&self, |
| 26 |
ctx: &BindgenContext, |
| 27 |
extra: Self::Extra, |
| 28 |
stack: &mut Vec<String>, |
| 29 |
writer: &mut W, |
| 30 |
) -> Result<(), CodegenError>; |
| 31 |
} |
| 32 |
|
| 33 |
impl<'a> CSerialize<'a> for Item { |
| 34 |
type Extra = (); |
| 35 |
|
| 36 |
fn serialize<W: Write>( |
| 37 |
&self, |
| 38 |
ctx: &BindgenContext, |
| 39 |
(): Self::Extra, |
| 40 |
stack: &mut Vec<String>, |
| 41 |
writer: &mut W, |
| 42 |
) -> Result<(), CodegenError> { |
| 43 |
match self.kind() { |
| 44 |
ItemKind::Function(func) => { |
| 45 |
func.serialize(ctx, self, stack, writer) |
| 46 |
} |
| 47 |
kind => Err(CodegenError::Serialize { |
| 48 |
msg: format!("Cannot serialize item kind {:?}", kind), |
| 49 |
loc: get_loc(self), |
| 50 |
}), |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
impl<'a> CSerialize<'a> for Function { |
| 56 |
type Extra = &'a Item; |
| 57 |
|
| 58 |
fn serialize<W: Write>( |
| 59 |
&self, |
| 60 |
ctx: &BindgenContext, |
| 61 |
item: Self::Extra, |
| 62 |
stack: &mut Vec<String>, |
| 63 |
writer: &mut W, |
| 64 |
) -> Result<(), CodegenError> { |
| 65 |
if self.kind() != FunctionKind::Function { |
| 66 |
return Err(CodegenError::Serialize { |
| 67 |
msg: format!( |
| 68 |
"Cannot serialize function kind {:?}", |
| 69 |
self.kind(), |
| 70 |
), |
| 71 |
loc: get_loc(item), |
| 72 |
}); |
| 73 |
} |
| 74 |
|
| 75 |
let signature = match ctx.resolve_type(self.signature()).kind() { |
| 76 |
TypeKind::Function(signature) => signature, |
| 77 |
_ => unreachable!(), |
| 78 |
}; |
| 79 |
|
| 80 |
let name = self.name(); |
| 81 |
|
| 82 |
// Function argoments stored as `(name, type_id)` tuples. |
| 83 |
let args = { |
| 84 |
let mut count = 0; |
| 85 |
|
| 86 |
signature |
| 87 |
.argument_types() |
| 88 |
.iter() |
| 89 |
.cloned() |
| 90 |
.map(|(opt_name, type_id)| { |
| 91 |
( |
| 92 |
opt_name.unwrap_or_else(|| { |
| 93 |
let name = format!("arg_{}", count); |
| 94 |
count += 1; |
| 95 |
name |
| 96 |
}), |
| 97 |
type_id, |
| 98 |
) |
| 99 |
}) |
| 100 |
.collect::<Vec<_>>() |
| 101 |
}; |
| 102 |
|
| 103 |
// The name used for the wrapper self. |
| 104 |
let wrap_name = format!("{}{}", name, ctx.wrap_static_fns_suffix()); |
| 105 |
// The function's return type |
| 106 |
let ret_ty = signature.return_type(); |
| 107 |
|
| 108 |
// Write `ret_ty wrap_name(args) { return name(arg_names)' }` |
| 109 |
ret_ty.serialize(ctx, (), stack, writer)?; |
| 110 |
write!(writer, " {}(", wrap_name)?; |
| 111 |
serialize_args(&args, ctx, writer)?; |
| 112 |
write!(writer, ") {{ return {}(", name)?; |
| 113 |
serialize_sep(", ", args.iter(), ctx, writer, |(name, _), _, buf| { |
| 114 |
write!(buf, "{}", name).map_err(From::from) |
| 115 |
})?; |
| 116 |
writeln!(writer, "); }}")?; |
| 117 |
|
| 118 |
Ok(()) |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
impl<'a> CSerialize<'a> for TypeId { |
| 123 |
type Extra = (); |
| 124 |
|
| 125 |
fn serialize<W: Write>( |
| 126 |
&self, |
| 127 |
ctx: &BindgenContext, |
| 128 |
(): Self::Extra, |
| 129 |
stack: &mut Vec<String>, |
| 130 |
writer: &mut W, |
| 131 |
) -> Result<(), CodegenError> { |
| 132 |
let item = ctx.resolve_item(*self); |
| 133 |
item.expect_type().serialize(ctx, item, stack, writer) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
impl<'a> CSerialize<'a> for Type { |
| 138 |
type Extra = &'a Item; |
| 139 |
|
| 140 |
fn serialize<W: Write>( |
| 141 |
&self, |
| 142 |
ctx: &BindgenContext, |
| 143 |
item: Self::Extra, |
| 144 |
stack: &mut Vec<String>, |
| 145 |
writer: &mut W, |
| 146 |
) -> Result<(), CodegenError> { |
| 147 |
match self.kind() { |
| 148 |
TypeKind::Void => { |
| 149 |
if self.is_const() { |
| 150 |
write!(writer, "const ")?; |
| 151 |
} |
| 152 |
write!(writer, "void")? |
| 153 |
} |
| 154 |
TypeKind::NullPtr => { |
| 155 |
if self.is_const() { |
| 156 |
write!(writer, "const ")?; |
| 157 |
} |
| 158 |
write!(writer, "nullptr_t")? |
| 159 |
} |
| 160 |
TypeKind::Int(int_kind) => { |
| 161 |
if self.is_const() { |
| 162 |
write!(writer, "const ")?; |
| 163 |
} |
| 164 |
match int_kind { |
| 165 |
IntKind::Bool => write!(writer, "bool")?, |
| 166 |
IntKind::SChar => write!(writer, "signed char")?, |
| 167 |
IntKind::UChar => write!(writer, "unsigned char")?, |
| 168 |
IntKind::WChar => write!(writer, "wchar_t")?, |
| 169 |
IntKind::Short => write!(writer, "short")?, |
| 170 |
IntKind::UShort => write!(writer, "unsigned short")?, |
| 171 |
IntKind::Int => write!(writer, "int")?, |
| 172 |
IntKind::UInt => write!(writer, "unsigned int")?, |
| 173 |
IntKind::Long => write!(writer, "long")?, |
| 174 |
IntKind::ULong => write!(writer, "unsigned long")?, |
| 175 |
IntKind::LongLong => write!(writer, "long long")?, |
| 176 |
IntKind::ULongLong => write!(writer, "unsigned long long")?, |
| 177 |
IntKind::Char { .. } => write!(writer, "char")?, |
| 178 |
int_kind => { |
| 179 |
return Err(CodegenError::Serialize { |
| 180 |
msg: format!( |
| 181 |
"Cannot serialize integer kind {:?}", |
| 182 |
int_kind |
| 183 |
), |
| 184 |
loc: get_loc(item), |
| 185 |
}) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
TypeKind::Float(float_kind) => { |
| 190 |
if self.is_const() { |
| 191 |
write!(writer, "const ")?; |
| 192 |
} |
| 193 |
match float_kind { |
| 194 |
FloatKind::Float => write!(writer, "float")?, |
| 195 |
FloatKind::Double => write!(writer, "double")?, |
| 196 |
FloatKind::LongDouble => write!(writer, "long double")?, |
| 197 |
FloatKind::Float128 => write!(writer, "__float128")?, |
| 198 |
} |
| 199 |
} |
| 200 |
TypeKind::Complex(float_kind) => { |
| 201 |
if self.is_const() { |
| 202 |
write!(writer, "const ")?; |
| 203 |
} |
| 204 |
match float_kind { |
| 205 |
FloatKind::Float => write!(writer, "float complex")?, |
| 206 |
FloatKind::Double => write!(writer, "double complex")?, |
| 207 |
FloatKind::LongDouble => { |
| 208 |
write!(writer, "long double complex")? |
| 209 |
} |
| 210 |
FloatKind::Float128 => write!(writer, "__complex128")?, |
| 211 |
} |
| 212 |
} |
| 213 |
TypeKind::Alias(type_id) => { |
| 214 |
if let Some(name) = self.name() { |
| 215 |
if self.is_const() { |
| 216 |
write!(writer, "const {}", name)?; |
| 217 |
} else { |
| 218 |
write!(writer, "{}", name)?; |
| 219 |
} |
| 220 |
} else { |
| 221 |
type_id.serialize(ctx, (), stack, writer)?; |
| 222 |
} |
| 223 |
} |
| 224 |
TypeKind::Array(type_id, length) => { |
| 225 |
type_id.serialize(ctx, (), stack, writer)?; |
| 226 |
write!(writer, " [{}]", length)? |
| 227 |
} |
| 228 |
TypeKind::Function(signature) => { |
| 229 |
if self.is_const() { |
| 230 |
stack.push("const ".to_string()); |
| 231 |
} |
| 232 |
|
| 233 |
signature.return_type().serialize( |
| 234 |
ctx, |
| 235 |
(), |
| 236 |
&mut vec![], |
| 237 |
writer, |
| 238 |
)?; |
| 239 |
|
| 240 |
write!(writer, " (")?; |
| 241 |
while let Some(item) = stack.pop() { |
| 242 |
write!(writer, "{}", item)?; |
| 243 |
} |
| 244 |
write!(writer, ")")?; |
| 245 |
|
| 246 |
write!(writer, " (")?; |
| 247 |
serialize_sep( |
| 248 |
", ", |
| 249 |
signature.argument_types().iter(), |
| 250 |
ctx, |
| 251 |
writer, |
| 252 |
|(name, type_id), ctx, buf| { |
| 253 |
let mut stack = vec![]; |
| 254 |
if let Some(name) = name { |
| 255 |
stack.push(name.clone()); |
| 256 |
} |
| 257 |
type_id.serialize(ctx, (), &mut stack, buf) |
| 258 |
}, |
| 259 |
)?; |
| 260 |
write!(writer, ")")? |
| 261 |
} |
| 262 |
TypeKind::ResolvedTypeRef(type_id) => { |
| 263 |
if self.is_const() { |
| 264 |
write!(writer, "const ")?; |
| 265 |
} |
| 266 |
type_id.serialize(ctx, (), stack, writer)? |
| 267 |
} |
| 268 |
TypeKind::Pointer(type_id) => { |
| 269 |
if self.is_const() { |
| 270 |
stack.push("*const ".to_owned()); |
| 271 |
} else { |
| 272 |
stack.push("*".to_owned()); |
| 273 |
} |
| 274 |
type_id.serialize(ctx, (), stack, writer)? |
| 275 |
} |
| 276 |
TypeKind::Comp(comp_info) => { |
| 277 |
if self.is_const() { |
| 278 |
write!(writer, "const ")?; |
| 279 |
} |
| 280 |
|
| 281 |
let name = item.canonical_name(ctx); |
| 282 |
|
| 283 |
match comp_info.kind() { |
| 284 |
CompKind::Struct => write!(writer, "struct {}", name)?, |
| 285 |
CompKind::Union => write!(writer, "union {}", name)?, |
| 286 |
}; |
| 287 |
} |
| 288 |
TypeKind::Enum(_enum_ty) => { |
| 289 |
if self.is_const() { |
| 290 |
write!(writer, "const ")?; |
| 291 |
} |
| 292 |
|
| 293 |
let name = item.canonical_name(ctx); |
| 294 |
write!(writer, "enum {}", name)?; |
| 295 |
} |
| 296 |
ty => { |
| 297 |
return Err(CodegenError::Serialize { |
| 298 |
msg: format!("Cannot serialize type kind {:?}", ty), |
| 299 |
loc: get_loc(item), |
| 300 |
}) |
| 301 |
} |
| 302 |
}; |
| 303 |
|
| 304 |
if !stack.is_empty() { |
| 305 |
write!(writer, " ")?; |
| 306 |
while let Some(item) = stack.pop() { |
| 307 |
write!(writer, "{}", item)?; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
Ok(()) |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
fn serialize_args<W: Write>( |
| 316 |
args: &[(String, TypeId)], |
| 317 |
ctx: &BindgenContext, |
| 318 |
writer: &mut W, |
| 319 |
) -> Result<(), CodegenError> { |
| 320 |
if args.is_empty() { |
| 321 |
write!(writer, "void")?; |
| 322 |
} else { |
| 323 |
serialize_sep( |
| 324 |
", ", |
| 325 |
args.iter(), |
| 326 |
ctx, |
| 327 |
writer, |
| 328 |
|(name, type_id), ctx, buf| { |
| 329 |
type_id.serialize(ctx, (), &mut vec![name.clone()], buf) |
| 330 |
}, |
| 331 |
)?; |
| 332 |
} |
| 333 |
|
| 334 |
Ok(()) |
| 335 |
} |
| 336 |
|
| 337 |
fn serialize_sep< |
| 338 |
W: Write, |
| 339 |
F: FnMut(I::Item, &BindgenContext, &mut W) -> Result<(), CodegenError>, |
| 340 |
I: Iterator, |
| 341 |
>( |
| 342 |
sep: &str, |
| 343 |
mut iter: I, |
| 344 |
ctx: &BindgenContext, |
| 345 |
buf: &mut W, |
| 346 |
mut f: F, |
| 347 |
) -> Result<(), CodegenError> { |
| 348 |
if let Some(item) = iter.next() { |
| 349 |
f(item, ctx, buf)?; |
| 350 |
let sep = sep.as_bytes(); |
| 351 |
for item in iter { |
| 352 |
buf.write_all(sep)?; |
| 353 |
f(item, ctx, buf)?; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
Ok(()) |
| 358 |
} |