| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::io::Write; |
| 6 |
|
| 7 |
use crate::bindgen::config::Layout; |
| 8 |
use crate::bindgen::declarationtyperesolver::DeclarationType; |
| 9 |
use crate::bindgen::ir::{ConstExpr, Function, GenericArgument, Type}; |
| 10 |
use crate::bindgen::writer::{ListType, SourceWriter}; |
| 11 |
use crate::bindgen::{Config, Language}; |
| 12 |
|
| 13 |
// This code is for translating Rust types into C declarations. |
| 14 |
// See Section 6.7, Declarations, in the C standard for background. |
| 15 |
// http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf |
| 16 |
|
| 17 |
enum CDeclarator { |
| 18 |
Ptr { |
| 19 |
is_const: bool, |
| 20 |
is_nullable: bool, |
| 21 |
is_ref: bool, |
| 22 |
}, |
| 23 |
Array(String), |
| 24 |
Func { |
| 25 |
args: Vec<(Option<String>, CDecl)>, |
| 26 |
layout: Layout, |
| 27 |
never_return: bool, |
| 28 |
}, |
| 29 |
} |
| 30 |
|
| 31 |
impl CDeclarator { |
| 32 |
fn is_ptr(&self) -> bool { |
| 33 |
matches!(self, CDeclarator::Ptr { .. } | CDeclarator::Func { .. }) |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
struct CDecl { |
| 38 |
type_qualifers: String, |
| 39 |
type_name: String, |
| 40 |
type_generic_args: Vec<GenericArgument>, |
| 41 |
declarators: Vec<CDeclarator>, |
| 42 |
type_ctype: Option<DeclarationType>, |
| 43 |
} |
| 44 |
|
| 45 |
impl CDecl { |
| 46 |
fn new() -> CDecl { |
| 47 |
CDecl { |
| 48 |
type_qualifers: String::new(), |
| 49 |
type_name: String::new(), |
| 50 |
type_generic_args: Vec::new(), |
| 51 |
declarators: Vec::new(), |
| 52 |
type_ctype: None, |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
fn from_type(t: &Type, config: &Config) -> CDecl { |
| 57 |
let mut cdecl = CDecl::new(); |
| 58 |
cdecl.build_type(t, false, config); |
| 59 |
cdecl |
| 60 |
} |
| 61 |
|
| 62 |
fn from_func_arg(t: &Type, array_length: Option<&str>, config: &Config) -> CDecl { |
| 63 |
let mut cdecl = CDecl::new(); |
| 64 |
let length = match array_length { |
| 65 |
Some(l) => l, |
| 66 |
None => return CDecl::from_type(t, config), |
| 67 |
}; |
| 68 |
let (ty, is_const) = match t { |
| 69 |
Type::Ptr { ty, is_const, .. } => (ty, is_const), |
| 70 |
_ => unreachable!( |
| 71 |
"Should never have an array length for a non pointer type {:?}", |
| 72 |
t |
| 73 |
), |
| 74 |
}; |
| 75 |
let ptr_as_array = Type::Array(ty.clone(), ConstExpr::Value(length.to_string())); |
| 76 |
cdecl.build_type(&ptr_as_array, *is_const, config); |
| 77 |
cdecl |
| 78 |
} |
| 79 |
|
| 80 |
fn from_func(f: &Function, layout: Layout, config: &Config) -> CDecl { |
| 81 |
let mut cdecl = CDecl::new(); |
| 82 |
cdecl.build_func(f, layout, config); |
| 83 |
cdecl |
| 84 |
} |
| 85 |
|
| 86 |
fn build_func(&mut self, f: &Function, layout: Layout, config: &Config) { |
| 87 |
let args = f |
| 88 |
.args |
| 89 |
.iter() |
| 90 |
.map(|arg| { |
| 91 |
( |
| 92 |
arg.name.clone(), |
| 93 |
CDecl::from_func_arg(&arg.ty, arg.array_length.as_deref(), config), |
| 94 |
) |
| 95 |
}) |
| 96 |
.collect(); |
| 97 |
self.declarators.push(CDeclarator::Func { |
| 98 |
args, |
| 99 |
layout, |
| 100 |
never_return: f.never_return, |
| 101 |
}); |
| 102 |
self.build_type(&f.ret, false, config); |
| 103 |
} |
| 104 |
|
| 105 |
fn build_type(&mut self, t: &Type, is_const: bool, config: &Config) { |
| 106 |
match t { |
| 107 |
Type::Path(ref generic) => { |
| 108 |
if is_const { |
| 109 |
assert!( |
| 110 |
self.type_qualifers.is_empty(), |
| 111 |
"error generating cdecl for {:?}", |
| 112 |
t |
| 113 |
); |
| 114 |
self.type_qualifers = "const".to_owned(); |
| 115 |
} |
| 116 |
|
| 117 |
assert!( |
| 118 |
self.type_name.is_empty(), |
| 119 |
"error generating cdecl for {:?}", |
| 120 |
t |
| 121 |
); |
| 122 |
self.type_name = generic.export_name().to_owned(); |
| 123 |
assert!( |
| 124 |
self.type_generic_args.is_empty(), |
| 125 |
"error generating cdecl for {:?}", |
| 126 |
t |
| 127 |
); |
| 128 |
self.type_generic_args = generic.generics().to_owned(); |
| 129 |
self.type_ctype = generic.ctype().cloned(); |
| 130 |
} |
| 131 |
Type::Primitive(ref p) => { |
| 132 |
if is_const { |
| 133 |
assert!( |
| 134 |
self.type_qualifers.is_empty(), |
| 135 |
"error generating cdecl for {:?}", |
| 136 |
t |
| 137 |
); |
| 138 |
self.type_qualifers = "const".to_owned(); |
| 139 |
} |
| 140 |
|
| 141 |
assert!( |
| 142 |
self.type_name.is_empty(), |
| 143 |
"error generating cdecl for {:?}", |
| 144 |
t |
| 145 |
); |
| 146 |
self.type_name = p.to_repr_c(config).to_string(); |
| 147 |
} |
| 148 |
Type::Ptr { |
| 149 |
ref ty, |
| 150 |
is_nullable, |
| 151 |
is_const: ptr_is_const, |
| 152 |
is_ref, |
| 153 |
} => { |
| 154 |
self.declarators.push(CDeclarator::Ptr { |
| 155 |
is_const, |
| 156 |
is_nullable: *is_nullable, |
| 157 |
is_ref: *is_ref, |
| 158 |
}); |
| 159 |
self.build_type(ty, *ptr_is_const, config); |
| 160 |
} |
| 161 |
Type::Array(ref t, ref constant) => { |
| 162 |
let len = constant.as_str().to_owned(); |
| 163 |
self.declarators.push(CDeclarator::Array(len)); |
| 164 |
self.build_type(t, is_const, config); |
| 165 |
} |
| 166 |
Type::FuncPtr { |
| 167 |
ref ret, |
| 168 |
ref args, |
| 169 |
is_nullable: _, |
| 170 |
never_return, |
| 171 |
} => { |
| 172 |
let args = args |
| 173 |
.iter() |
| 174 |
.map(|(ref name, ref ty)| (name.clone(), CDecl::from_type(ty, config))) |
| 175 |
.collect(); |
| 176 |
self.declarators.push(CDeclarator::Ptr { |
| 177 |
is_const: false, |
| 178 |
is_nullable: true, |
| 179 |
is_ref: false, |
| 180 |
}); |
| 181 |
self.declarators.push(CDeclarator::Func { |
| 182 |
args, |
| 183 |
layout: config.function.args.clone(), |
| 184 |
never_return: *never_return, |
| 185 |
}); |
| 186 |
self.build_type(ret, false, config); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
fn write<F: Write>(&self, out: &mut SourceWriter<F>, ident: Option<&str>, config: &Config) { |
| 192 |
// Write the type-specifier and type-qualifier first |
| 193 |
if !self.type_qualifers.is_empty() { |
| 194 |
write!(out, "{} ", self.type_qualifers); |
| 195 |
} |
| 196 |
|
| 197 |
if config.language != Language::Cython { |
| 198 |
if let Some(ref ctype) = self.type_ctype { |
| 199 |
write!(out, "{} ", ctype.to_str()); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
write!(out, "{}", self.type_name); |
| 204 |
|
| 205 |
if !self.type_generic_args.is_empty() { |
| 206 |
out.write("<"); |
| 207 |
out.write_horizontal_source_list(&self.type_generic_args, ListType::Join(", ")); |
| 208 |
out.write(">"); |
| 209 |
} |
| 210 |
|
| 211 |
// When we have an identifier, put a space between the type and the declarators |
| 212 |
if ident.is_some() { |
| 213 |
out.write(" "); |
| 214 |
} |
| 215 |
|
| 216 |
// Write the left part of declarators before the identifier |
| 217 |
let mut iter_rev = self.declarators.iter().rev().peekable(); |
| 218 |
|
| 219 |
#[allow(clippy::while_let_on_iterator)] |
| 220 |
while let Some(declarator) = iter_rev.next() { |
| 221 |
let next_is_pointer = iter_rev.peek().map_or(false, |x| x.is_ptr()); |
| 222 |
|
| 223 |
match *declarator { |
| 224 |
CDeclarator::Ptr { |
| 225 |
is_const, |
| 226 |
is_nullable, |
| 227 |
is_ref, |
| 228 |
} => { |
| 229 |
out.write(if is_ref { "&" } else { "*" }); |
| 230 |
if is_const { |
| 231 |
out.write("const "); |
| 232 |
} |
| 233 |
if !is_nullable && !is_ref && config.language != Language::Cython { |
| 234 |
if let Some(attr) = &config.pointer.non_null_attribute { |
| 235 |
write!(out, "{} ", attr); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
CDeclarator::Array(..) => { |
| 240 |
if next_is_pointer { |
| 241 |
out.write("("); |
| 242 |
} |
| 243 |
} |
| 244 |
CDeclarator::Func { .. } => { |
| 245 |
if next_is_pointer { |
| 246 |
out.write("("); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// Write the identifier |
| 253 |
if let Some(ident) = ident { |
| 254 |
write!(out, "{}", ident); |
| 255 |
} |
| 256 |
|
| 257 |
// Write the right part of declarators after the identifier |
| 258 |
let mut iter = self.declarators.iter(); |
| 259 |
let mut last_was_pointer = false; |
| 260 |
|
| 261 |
#[allow(clippy::while_let_on_iterator)] |
| 262 |
while let Some(declarator) = iter.next() { |
| 263 |
match *declarator { |
| 264 |
CDeclarator::Ptr { .. } => { |
| 265 |
last_was_pointer = true; |
| 266 |
} |
| 267 |
CDeclarator::Array(ref constant) => { |
| 268 |
if last_was_pointer { |
| 269 |
out.write(")"); |
| 270 |
} |
| 271 |
write!(out, "[{}]", constant); |
| 272 |
|
| 273 |
last_was_pointer = false; |
| 274 |
} |
| 275 |
CDeclarator::Func { |
| 276 |
ref args, |
| 277 |
ref layout, |
| 278 |
never_return, |
| 279 |
} => { |
| 280 |
if last_was_pointer { |
| 281 |
out.write(")"); |
| 282 |
} |
| 283 |
|
| 284 |
out.write("("); |
| 285 |
if args.is_empty() && config.language == Language::C { |
| 286 |
out.write("void"); |
| 287 |
} |
| 288 |
|
| 289 |
fn write_vertical<F: Write>( |
| 290 |
out: &mut SourceWriter<F>, |
| 291 |
config: &Config, |
| 292 |
args: &[(Option<String>, CDecl)], |
| 293 |
) { |
| 294 |
let align_length = out.line_length_for_align(); |
| 295 |
out.push_set_spaces(align_length); |
| 296 |
for (i, (arg_ident, arg_ty)) in args.iter().enumerate() { |
| 297 |
if i != 0 { |
| 298 |
out.write(","); |
| 299 |
out.new_line(); |
| 300 |
} |
| 301 |
|
| 302 |
// Convert &Option<String> to Option<&str> |
| 303 |
let arg_ident = arg_ident.as_ref().map(|x| x.as_ref()); |
| 304 |
|
| 305 |
arg_ty.write(out, arg_ident, config); |
| 306 |
} |
| 307 |
out.pop_tab(); |
| 308 |
} |
| 309 |
|
| 310 |
fn write_horizontal<F: Write>( |
| 311 |
out: &mut SourceWriter<F>, |
| 312 |
config: &Config, |
| 313 |
args: &[(Option<String>, CDecl)], |
| 314 |
) { |
| 315 |
for (i, (arg_ident, arg_ty)) in args.iter().enumerate() { |
| 316 |
if i != 0 { |
| 317 |
out.write(", "); |
| 318 |
} |
| 319 |
|
| 320 |
// Convert &Option<String> to Option<&str> |
| 321 |
let arg_ident = arg_ident.as_ref().map(|x| x.as_ref()); |
| 322 |
|
| 323 |
arg_ty.write(out, arg_ident, config); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
match layout { |
| 328 |
Layout::Vertical => write_vertical(out, config, args), |
| 329 |
Layout::Horizontal => write_horizontal(out, config, args), |
| 330 |
Layout::Auto => { |
| 331 |
if !out.try_write( |
| 332 |
|out| write_horizontal(out, config, args), |
| 333 |
config.line_length, |
| 334 |
) { |
| 335 |
write_vertical(out, config, args) |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
out.write(")"); |
| 340 |
|
| 341 |
if never_return && config.language != Language::Cython { |
| 342 |
if let Some(ref no_return_attr) = config.function.no_return { |
| 343 |
out.write_fmt(format_args!(" {}", no_return_attr)); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
last_was_pointer = true; |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
pub fn write_func<F: Write>( |
| 355 |
out: &mut SourceWriter<F>, |
| 356 |
f: &Function, |
| 357 |
layout: Layout, |
| 358 |
config: &Config, |
| 359 |
) { |
| 360 |
CDecl::from_func(f, layout, config).write(out, Some(f.path().name()), config); |
| 361 |
} |
| 362 |
|
| 363 |
pub fn write_field<F: Write>(out: &mut SourceWriter<F>, t: &Type, ident: &str, config: &Config) { |
| 364 |
CDecl::from_type(t, config).write(out, Some(ident), config); |
| 365 |
} |
| 366 |
|
| 367 |
pub fn write_type<F: Write>(out: &mut SourceWriter<F>, t: &Type, config: &Config) { |
| 368 |
CDecl::from_type(t, config).write(out, None, config); |
| 369 |
} |