| 1 |
//! Intermediate representation for C/C++ functions and methods. |
| 2 |
|
| 3 |
use super::comp::MethodKind; |
| 4 |
use super::context::{BindgenContext, TypeId}; |
| 5 |
use super::dot::DotAttributes; |
| 6 |
use super::item::Item; |
| 7 |
use super::traversal::{EdgeKind, Trace, Tracer}; |
| 8 |
use super::ty::TypeKind; |
| 9 |
use crate::callbacks::{ItemInfo, ItemKind}; |
| 10 |
use crate::clang::{self, Attribute}; |
| 11 |
use crate::parse::{ClangSubItemParser, ParseError, ParseResult}; |
| 12 |
use clang_sys::{self, CXCallingConv}; |
| 13 |
|
| 14 |
use quote::TokenStreamExt; |
| 15 |
use std::io; |
| 16 |
use std::str::FromStr; |
| 17 |
|
| 18 |
const RUST_DERIVE_FUNPTR_LIMIT: usize = 12; |
| 19 |
|
| 20 |
/// What kind of a function are we looking at? |
| 21 |
#[derive(Debug, Copy, Clone, PartialEq, Eq)] |
| 22 |
pub(crate) enum FunctionKind { |
| 23 |
/// A plain, free function. |
| 24 |
Function, |
| 25 |
/// A method of some kind. |
| 26 |
Method(MethodKind), |
| 27 |
} |
| 28 |
|
| 29 |
impl FunctionKind { |
| 30 |
/// Given a clang cursor, return the kind of function it represents, or |
| 31 |
/// `None` otherwise. |
| 32 |
pub(crate) fn from_cursor(cursor: &clang::Cursor) -> Option<FunctionKind> { |
| 33 |
// FIXME(emilio): Deduplicate logic with `ir::comp`. |
| 34 |
Some(match cursor.kind() { |
| 35 |
clang_sys::CXCursor_FunctionDecl => FunctionKind::Function, |
| 36 |
clang_sys::CXCursor_Constructor => { |
| 37 |
FunctionKind::Method(MethodKind::Constructor) |
| 38 |
} |
| 39 |
clang_sys::CXCursor_Destructor => { |
| 40 |
FunctionKind::Method(if cursor.method_is_virtual() { |
| 41 |
MethodKind::VirtualDestructor { |
| 42 |
pure_virtual: cursor.method_is_pure_virtual(), |
| 43 |
} |
| 44 |
} else { |
| 45 |
MethodKind::Destructor |
| 46 |
}) |
| 47 |
} |
| 48 |
clang_sys::CXCursor_CXXMethod => { |
| 49 |
if cursor.method_is_virtual() { |
| 50 |
FunctionKind::Method(MethodKind::Virtual { |
| 51 |
pure_virtual: cursor.method_is_pure_virtual(), |
| 52 |
}) |
| 53 |
} else if cursor.method_is_static() { |
| 54 |
FunctionKind::Method(MethodKind::Static) |
| 55 |
} else { |
| 56 |
FunctionKind::Method(MethodKind::Normal) |
| 57 |
} |
| 58 |
} |
| 59 |
_ => return None, |
| 60 |
}) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/// The style of linkage |
| 65 |
#[derive(Debug, Clone, Copy)] |
| 66 |
pub(crate) enum Linkage { |
| 67 |
/// Externally visible and can be linked against |
| 68 |
External, |
| 69 |
/// Not exposed externally. 'static inline' functions will have this kind of linkage |
| 70 |
Internal, |
| 71 |
} |
| 72 |
|
| 73 |
/// A function declaration, with a signature, arguments, and argument names. |
| 74 |
/// |
| 75 |
/// The argument names vector must be the same length as the ones in the |
| 76 |
/// signature. |
| 77 |
#[derive(Debug)] |
| 78 |
pub(crate) struct Function { |
| 79 |
/// The name of this function. |
| 80 |
name: String, |
| 81 |
|
| 82 |
/// The mangled name, that is, the symbol. |
| 83 |
mangled_name: Option<String>, |
| 84 |
|
| 85 |
/// The link name. If specified, overwrite mangled_name. |
| 86 |
link_name: Option<String>, |
| 87 |
|
| 88 |
/// The ID pointing to the current function signature. |
| 89 |
signature: TypeId, |
| 90 |
|
| 91 |
/// The kind of function this is. |
| 92 |
kind: FunctionKind, |
| 93 |
|
| 94 |
/// The linkage of the function. |
| 95 |
linkage: Linkage, |
| 96 |
} |
| 97 |
|
| 98 |
impl Function { |
| 99 |
/// Construct a new function. |
| 100 |
pub(crate) fn new( |
| 101 |
name: String, |
| 102 |
mangled_name: Option<String>, |
| 103 |
link_name: Option<String>, |
| 104 |
signature: TypeId, |
| 105 |
kind: FunctionKind, |
| 106 |
linkage: Linkage, |
| 107 |
) -> Self { |
| 108 |
Function { |
| 109 |
name, |
| 110 |
mangled_name, |
| 111 |
link_name, |
| 112 |
signature, |
| 113 |
kind, |
| 114 |
linkage, |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/// Get this function's name. |
| 119 |
pub(crate) fn name(&self) -> &str { |
| 120 |
&self.name |
| 121 |
} |
| 122 |
|
| 123 |
/// Get this function's name. |
| 124 |
pub(crate) fn mangled_name(&self) -> Option<&str> { |
| 125 |
self.mangled_name.as_deref() |
| 126 |
} |
| 127 |
|
| 128 |
/// Get this function's link name. |
| 129 |
pub fn link_name(&self) -> Option<&str> { |
| 130 |
self.link_name.as_deref() |
| 131 |
} |
| 132 |
|
| 133 |
/// Get this function's signature type. |
| 134 |
pub(crate) fn signature(&self) -> TypeId { |
| 135 |
self.signature |
| 136 |
} |
| 137 |
|
| 138 |
/// Get this function's kind. |
| 139 |
pub(crate) fn kind(&self) -> FunctionKind { |
| 140 |
self.kind |
| 141 |
} |
| 142 |
|
| 143 |
/// Get this function's linkage. |
| 144 |
pub(crate) fn linkage(&self) -> Linkage { |
| 145 |
self.linkage |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
impl DotAttributes for Function { |
| 150 |
fn dot_attributes<W>( |
| 151 |
&self, |
| 152 |
_ctx: &BindgenContext, |
| 153 |
out: &mut W, |
| 154 |
) -> io::Result<()> |
| 155 |
where |
| 156 |
W: io::Write, |
| 157 |
{ |
| 158 |
if let Some(ref mangled) = self.mangled_name { |
| 159 |
let mangled: String = |
| 160 |
mangled.chars().flat_map(|c| c.escape_default()).collect(); |
| 161 |
writeln!( |
| 162 |
out, |
| 163 |
"<tr><td>mangled name</td><td>{}</td></tr>", |
| 164 |
mangled |
| 165 |
)?; |
| 166 |
} |
| 167 |
|
| 168 |
Ok(()) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/// A valid rust ABI. |
| 173 |
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)] |
| 174 |
pub enum Abi { |
| 175 |
/// The default C ABI. |
| 176 |
C, |
| 177 |
/// The "stdcall" ABI. |
| 178 |
Stdcall, |
| 179 |
/// The "efiapi" ABI. |
| 180 |
EfiApi, |
| 181 |
/// The "fastcall" ABI. |
| 182 |
Fastcall, |
| 183 |
/// The "thiscall" ABI. |
| 184 |
ThisCall, |
| 185 |
/// The "vectorcall" ABI. |
| 186 |
Vectorcall, |
| 187 |
/// The "aapcs" ABI. |
| 188 |
Aapcs, |
| 189 |
/// The "win64" ABI. |
| 190 |
Win64, |
| 191 |
/// The "C-unwind" ABI. |
| 192 |
CUnwind, |
| 193 |
} |
| 194 |
|
| 195 |
impl FromStr for Abi { |
| 196 |
type Err = String; |
| 197 |
|
| 198 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 199 |
match s { |
| 200 |
"C" => Ok(Self::C), |
| 201 |
"stdcall" => Ok(Self::Stdcall), |
| 202 |
"efiapi" => Ok(Self::EfiApi), |
| 203 |
"fastcall" => Ok(Self::Fastcall), |
| 204 |
"thiscall" => Ok(Self::ThisCall), |
| 205 |
"vectorcall" => Ok(Self::Vectorcall), |
| 206 |
"aapcs" => Ok(Self::Aapcs), |
| 207 |
"win64" => Ok(Self::Win64), |
| 208 |
"C-unwind" => Ok(Self::CUnwind), |
| 209 |
_ => Err(format!("Invalid or unknown ABI {:?}", s)), |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
impl std::fmt::Display for Abi { |
| 215 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 216 |
let s = match *self { |
| 217 |
Self::C => "C", |
| 218 |
Self::Stdcall => "stdcall", |
| 219 |
Self::EfiApi => "efiapi", |
| 220 |
Self::Fastcall => "fastcall", |
| 221 |
Self::ThisCall => "thiscall", |
| 222 |
Self::Vectorcall => "vectorcall", |
| 223 |
Self::Aapcs => "aapcs", |
| 224 |
Self::Win64 => "win64", |
| 225 |
Self::CUnwind => "C-unwind", |
| 226 |
}; |
| 227 |
|
| 228 |
s.fmt(f) |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
impl quote::ToTokens for Abi { |
| 233 |
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 234 |
let abi = self.to_string(); |
| 235 |
tokens.append_all(quote! { #abi }); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/// An ABI extracted from a clang cursor. |
| 240 |
#[derive(Debug, Copy, Clone)] |
| 241 |
pub(crate) enum ClangAbi { |
| 242 |
/// An ABI known by Rust. |
| 243 |
Known(Abi), |
| 244 |
/// An unknown or invalid ABI. |
| 245 |
Unknown(CXCallingConv), |
| 246 |
} |
| 247 |
|
| 248 |
impl ClangAbi { |
| 249 |
/// Returns whether this Abi is known or not. |
| 250 |
fn is_unknown(&self) -> bool { |
| 251 |
matches!(*self, ClangAbi::Unknown(..)) |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
impl quote::ToTokens for ClangAbi { |
| 256 |
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 257 |
match *self { |
| 258 |
Self::Known(abi) => abi.to_tokens(tokens), |
| 259 |
Self::Unknown(cc) => panic!( |
| 260 |
"Cannot turn unknown calling convention to tokens: {:?}", |
| 261 |
cc |
| 262 |
), |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/// A function signature. |
| 268 |
#[derive(Debug)] |
| 269 |
pub(crate) struct FunctionSig { |
| 270 |
/// The name of this function signature. |
| 271 |
name: String, |
| 272 |
|
| 273 |
/// The return type of the function. |
| 274 |
return_type: TypeId, |
| 275 |
|
| 276 |
/// The type of the arguments, optionally with the name of the argument when |
| 277 |
/// declared. |
| 278 |
argument_types: Vec<(Option<String>, TypeId)>, |
| 279 |
|
| 280 |
/// Whether this function is variadic. |
| 281 |
is_variadic: bool, |
| 282 |
is_divergent: bool, |
| 283 |
|
| 284 |
/// Whether this function's return value must be used. |
| 285 |
must_use: bool, |
| 286 |
|
| 287 |
/// The ABI of this function. |
| 288 |
abi: ClangAbi, |
| 289 |
} |
| 290 |
|
| 291 |
fn get_abi(cc: CXCallingConv) -> ClangAbi { |
| 292 |
use clang_sys::*; |
| 293 |
match cc { |
| 294 |
CXCallingConv_Default => ClangAbi::Known(Abi::C), |
| 295 |
CXCallingConv_C => ClangAbi::Known(Abi::C), |
| 296 |
CXCallingConv_X86StdCall => ClangAbi::Known(Abi::Stdcall), |
| 297 |
CXCallingConv_X86FastCall => ClangAbi::Known(Abi::Fastcall), |
| 298 |
CXCallingConv_X86ThisCall => ClangAbi::Known(Abi::ThisCall), |
| 299 |
CXCallingConv_X86VectorCall => ClangAbi::Known(Abi::Vectorcall), |
| 300 |
CXCallingConv_AAPCS => ClangAbi::Known(Abi::Aapcs), |
| 301 |
CXCallingConv_X86_64Win64 => ClangAbi::Known(Abi::Win64), |
| 302 |
other => ClangAbi::Unknown(other), |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/// Get the mangled name for the cursor's referent. |
| 307 |
pub(crate) fn cursor_mangling( |
| 308 |
ctx: &BindgenContext, |
| 309 |
cursor: &clang::Cursor, |
| 310 |
) -> Option<String> { |
| 311 |
if !ctx.options().enable_mangling { |
| 312 |
return None; |
| 313 |
} |
| 314 |
|
| 315 |
// We early return here because libclang may crash in some case |
| 316 |
// if we pass in a variable inside a partial specialized template. |
| 317 |
// See rust-lang/rust-bindgen#67, and rust-lang/rust-bindgen#462. |
| 318 |
if cursor.is_in_non_fully_specialized_template() { |
| 319 |
return None; |
| 320 |
} |
| 321 |
|
| 322 |
let is_destructor = cursor.kind() == clang_sys::CXCursor_Destructor; |
| 323 |
if let Ok(mut manglings) = cursor.cxx_manglings() { |
| 324 |
while let Some(m) = manglings.pop() { |
| 325 |
// Only generate the destructor group 1, see below. |
| 326 |
if is_destructor && !m.ends_with("D1Ev") { |
| 327 |
continue; |
| 328 |
} |
| 329 |
|
| 330 |
return Some(m); |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
let mut mangling = cursor.mangling(); |
| 335 |
if mangling.is_empty() { |
| 336 |
return None; |
| 337 |
} |
| 338 |
|
| 339 |
if is_destructor { |
| 340 |
// With old (3.8-) libclang versions, and the Itanium ABI, clang returns |
| 341 |
// the "destructor group 0" symbol, which means that it'll try to free |
| 342 |
// memory, which definitely isn't what we want. |
| 343 |
// |
| 344 |
// Explicitly force the destructor group 1 symbol. |
| 345 |
// |
| 346 |
// See http://refspecs.linuxbase.org/cxxabi-1.83.html#mangling-special |
| 347 |
// for the reference, and http://stackoverflow.com/a/6614369/1091587 for |
| 348 |
// a more friendly explanation. |
| 349 |
// |
| 350 |
// We don't need to do this for constructors since clang seems to always |
| 351 |
// have returned the C1 constructor. |
| 352 |
// |
| 353 |
// FIXME(emilio): Can a legit symbol in other ABIs end with this string? |
| 354 |
// I don't think so, but if it can this would become a linker error |
| 355 |
// anyway, not an invalid free at runtime. |
| 356 |
// |
| 357 |
// TODO(emilio, #611): Use cpp_demangle if this becomes nastier with |
| 358 |
// time. |
| 359 |
if mangling.ends_with("D0Ev") { |
| 360 |
let new_len = mangling.len() - 4; |
| 361 |
mangling.truncate(new_len); |
| 362 |
mangling.push_str("D1Ev"); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
Some(mangling) |
| 367 |
} |
| 368 |
|
| 369 |
fn args_from_ty_and_cursor( |
| 370 |
ty: &clang::Type, |
| 371 |
cursor: &clang::Cursor, |
| 372 |
ctx: &mut BindgenContext, |
| 373 |
) -> Vec<(Option<String>, TypeId)> { |
| 374 |
let cursor_args = cursor.args().unwrap_or_default().into_iter(); |
| 375 |
let type_args = ty.args().unwrap_or_default().into_iter(); |
| 376 |
|
| 377 |
// Argument types can be found in either the cursor or the type, but argument names may only be |
| 378 |
// found on the cursor. We often have access to both a type and a cursor for each argument, but |
| 379 |
// in some cases we may only have one. |
| 380 |
// |
| 381 |
// Prefer using the type as the source of truth for the argument's type, but fall back to |
| 382 |
// inspecting the cursor (this happens for Objective C interfaces). |
| 383 |
// |
| 384 |
// Prefer using the cursor for the argument's type, but fall back to using the parent's cursor |
| 385 |
// (this happens for function pointer return types). |
| 386 |
cursor_args |
| 387 |
.map(Some) |
| 388 |
.chain(std::iter::repeat(None)) |
| 389 |
.zip(type_args.map(Some).chain(std::iter::repeat(None))) |
| 390 |
.take_while(|(cur, ty)| cur.is_some() || ty.is_some()) |
| 391 |
.map(|(arg_cur, arg_ty)| { |
| 392 |
let name = arg_cur.map(|a| a.spelling()).and_then(|name| { |
| 393 |
if name.is_empty() { |
| 394 |
None |
| 395 |
} else { |
| 396 |
Some(name) |
| 397 |
} |
| 398 |
}); |
| 399 |
|
| 400 |
let cursor = arg_cur.unwrap_or(*cursor); |
| 401 |
let ty = arg_ty.unwrap_or_else(|| cursor.cur_type()); |
| 402 |
(name, Item::from_ty_or_ref(ty, cursor, None, ctx)) |
| 403 |
}) |
| 404 |
.collect() |
| 405 |
} |
| 406 |
|
| 407 |
impl FunctionSig { |
| 408 |
/// Construct a new function signature from the given Clang type. |
| 409 |
pub(crate) fn from_ty( |
| 410 |
ty: &clang::Type, |
| 411 |
cursor: &clang::Cursor, |
| 412 |
ctx: &mut BindgenContext, |
| 413 |
) -> Result<Self, ParseError> { |
| 414 |
use clang_sys::*; |
| 415 |
debug!("FunctionSig::from_ty {:?} {:?}", ty, cursor); |
| 416 |
|
| 417 |
// Skip function templates |
| 418 |
let kind = cursor.kind(); |
| 419 |
if kind == CXCursor_FunctionTemplate { |
| 420 |
return Err(ParseError::Continue); |
| 421 |
} |
| 422 |
|
| 423 |
let spelling = cursor.spelling(); |
| 424 |
|
| 425 |
// Don't parse operatorxx functions in C++ |
| 426 |
let is_operator = |spelling: &str| { |
| 427 |
spelling.starts_with("operator") && |
| 428 |
!clang::is_valid_identifier(spelling) |
| 429 |
}; |
| 430 |
if is_operator(&spelling) { |
| 431 |
return Err(ParseError::Continue); |
| 432 |
} |
| 433 |
|
| 434 |
// Constructors of non-type template parameter classes for some reason |
| 435 |
// include the template parameter in their name. Just skip them, since |
| 436 |
// we don't handle well non-type template parameters anyway. |
| 437 |
if (kind == CXCursor_Constructor || kind == CXCursor_Destructor) && |
| 438 |
spelling.contains('<') |
| 439 |
{ |
| 440 |
return Err(ParseError::Continue); |
| 441 |
} |
| 442 |
|
| 443 |
let cursor = if cursor.is_valid() { |
| 444 |
*cursor |
| 445 |
} else { |
| 446 |
ty.declaration() |
| 447 |
}; |
| 448 |
|
| 449 |
let mut args = match kind { |
| 450 |
CXCursor_FunctionDecl | |
| 451 |
CXCursor_Constructor | |
| 452 |
CXCursor_CXXMethod | |
| 453 |
CXCursor_ObjCInstanceMethodDecl | |
| 454 |
CXCursor_ObjCClassMethodDecl => { |
| 455 |
args_from_ty_and_cursor(ty, &cursor, ctx) |
| 456 |
} |
| 457 |
_ => { |
| 458 |
// For non-CXCursor_FunctionDecl, visiting the cursor's children |
| 459 |
// is the only reliable way to get parameter names. |
| 460 |
let mut args = vec![]; |
| 461 |
cursor.visit(|c| { |
| 462 |
if c.kind() == CXCursor_ParmDecl { |
| 463 |
let ty = |
| 464 |
Item::from_ty_or_ref(c.cur_type(), c, None, ctx); |
| 465 |
let name = c.spelling(); |
| 466 |
let name = |
| 467 |
if name.is_empty() { None } else { Some(name) }; |
| 468 |
args.push((name, ty)); |
| 469 |
} |
| 470 |
CXChildVisit_Continue |
| 471 |
}); |
| 472 |
|
| 473 |
if args.is_empty() { |
| 474 |
// FIXME(emilio): Sometimes libclang doesn't expose the |
| 475 |
// right AST for functions tagged as stdcall and such... |
| 476 |
// |
| 477 |
// https://bugs.llvm.org/show_bug.cgi?id=45919 |
| 478 |
args_from_ty_and_cursor(ty, &cursor, ctx) |
| 479 |
} else { |
| 480 |
args |
| 481 |
} |
| 482 |
} |
| 483 |
}; |
| 484 |
|
| 485 |
let (must_use, mut is_divergent) = |
| 486 |
if ctx.options().enable_function_attribute_detection { |
| 487 |
let [must_use, no_return, no_return_cpp] = cursor.has_attrs(&[ |
| 488 |
Attribute::MUST_USE, |
| 489 |
Attribute::NO_RETURN, |
| 490 |
Attribute::NO_RETURN_CPP, |
| 491 |
]); |
| 492 |
(must_use, no_return || no_return_cpp) |
| 493 |
} else { |
| 494 |
Default::default() |
| 495 |
}; |
| 496 |
|
| 497 |
// This looks easy to break but the clang parser keeps the type spelling clean even if |
| 498 |
// other attributes are added. |
| 499 |
is_divergent = |
| 500 |
is_divergent || ty.spelling().contains("__attribute__((noreturn))"); |
| 501 |
|
| 502 |
let is_method = kind == CXCursor_CXXMethod; |
| 503 |
let is_constructor = kind == CXCursor_Constructor; |
| 504 |
let is_destructor = kind == CXCursor_Destructor; |
| 505 |
if (is_constructor || is_destructor || is_method) && |
| 506 |
cursor.lexical_parent() != cursor.semantic_parent() |
| 507 |
{ |
| 508 |
// Only parse constructors once. |
| 509 |
return Err(ParseError::Continue); |
| 510 |
} |
| 511 |
|
| 512 |
if is_method || is_constructor || is_destructor { |
| 513 |
let is_const = is_method && cursor.method_is_const(); |
| 514 |
let is_virtual = is_method && cursor.method_is_virtual(); |
| 515 |
let is_static = is_method && cursor.method_is_static(); |
| 516 |
if !is_static && !is_virtual { |
| 517 |
let parent = cursor.semantic_parent(); |
| 518 |
let class = Item::parse(parent, None, ctx) |
| 519 |
.expect("Expected to parse the class"); |
| 520 |
// The `class` most likely is not finished parsing yet, so use |
| 521 |
// the unchecked variant. |
| 522 |
let class = class.as_type_id_unchecked(); |
| 523 |
|
| 524 |
let class = if is_const { |
| 525 |
let const_class_id = ctx.next_item_id(); |
| 526 |
ctx.build_const_wrapper( |
| 527 |
const_class_id, |
| 528 |
class, |
| 529 |
None, |
| 530 |
&parent.cur_type(), |
| 531 |
) |
| 532 |
} else { |
| 533 |
class |
| 534 |
}; |
| 535 |
|
| 536 |
let ptr = |
| 537 |
Item::builtin_type(TypeKind::Pointer(class), false, ctx); |
| 538 |
args.insert(0, (Some("this".into()), ptr)); |
| 539 |
} else if is_virtual { |
| 540 |
let void = Item::builtin_type(TypeKind::Void, false, ctx); |
| 541 |
let ptr = |
| 542 |
Item::builtin_type(TypeKind::Pointer(void), false, ctx); |
| 543 |
args.insert(0, (Some("this".into()), ptr)); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
let ty_ret_type = if kind == CXCursor_ObjCInstanceMethodDecl || |
| 548 |
kind == CXCursor_ObjCClassMethodDecl |
| 549 |
{ |
| 550 |
ty.ret_type() |
| 551 |
.or_else(|| cursor.ret_type()) |
| 552 |
.ok_or(ParseError::Continue)? |
| 553 |
} else { |
| 554 |
ty.ret_type().ok_or(ParseError::Continue)? |
| 555 |
}; |
| 556 |
|
| 557 |
let ret = if is_constructor && ctx.is_target_wasm32() { |
| 558 |
// Constructors in Clang wasm32 target return a pointer to the object |
| 559 |
// being constructed. |
| 560 |
let void = Item::builtin_type(TypeKind::Void, false, ctx); |
| 561 |
Item::builtin_type(TypeKind::Pointer(void), false, ctx) |
| 562 |
} else { |
| 563 |
Item::from_ty_or_ref(ty_ret_type, cursor, None, ctx) |
| 564 |
}; |
| 565 |
|
| 566 |
// Clang plays with us at "find the calling convention", see #549 and |
| 567 |
// co. This seems to be a better fix than that commit. |
| 568 |
let mut call_conv = ty.call_conv(); |
| 569 |
if let Some(ty) = cursor.cur_type().canonical_type().pointee_type() { |
| 570 |
let cursor_call_conv = ty.call_conv(); |
| 571 |
if cursor_call_conv != CXCallingConv_Invalid { |
| 572 |
call_conv = cursor_call_conv; |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
let abi = get_abi(call_conv); |
| 577 |
|
| 578 |
if abi.is_unknown() { |
| 579 |
warn!("Unknown calling convention: {:?}", call_conv); |
| 580 |
} |
| 581 |
|
| 582 |
Ok(Self { |
| 583 |
name: spelling, |
| 584 |
return_type: ret, |
| 585 |
argument_types: args, |
| 586 |
is_variadic: ty.is_variadic(), |
| 587 |
is_divergent, |
| 588 |
must_use, |
| 589 |
abi, |
| 590 |
}) |
| 591 |
} |
| 592 |
|
| 593 |
/// Get this function signature's return type. |
| 594 |
pub(crate) fn return_type(&self) -> TypeId { |
| 595 |
self.return_type |
| 596 |
} |
| 597 |
|
| 598 |
/// Get this function signature's argument (name, type) pairs. |
| 599 |
pub(crate) fn argument_types(&self) -> &[(Option<String>, TypeId)] { |
| 600 |
&self.argument_types |
| 601 |
} |
| 602 |
|
| 603 |
/// Get this function signature's ABI. |
| 604 |
pub(crate) fn abi( |
| 605 |
&self, |
| 606 |
ctx: &BindgenContext, |
| 607 |
name: Option<&str>, |
| 608 |
) -> ClangAbi { |
| 609 |
// FIXME (pvdrz): Try to do this check lazily instead. Maybe store the ABI inside `ctx` |
| 610 |
// instead?. |
| 611 |
if let Some(name) = name { |
| 612 |
if let Some((abi, _)) = ctx |
| 613 |
.options() |
| 614 |
.abi_overrides |
| 615 |
.iter() |
| 616 |
.find(|(_, regex_set)| regex_set.matches(name)) |
| 617 |
{ |
| 618 |
ClangAbi::Known(*abi) |
| 619 |
} else { |
| 620 |
self.abi |
| 621 |
} |
| 622 |
} else if let Some((abi, _)) = ctx |
| 623 |
.options() |
| 624 |
.abi_overrides |
| 625 |
.iter() |
| 626 |
.find(|(_, regex_set)| regex_set.matches(&self.name)) |
| 627 |
{ |
| 628 |
ClangAbi::Known(*abi) |
| 629 |
} else { |
| 630 |
self.abi |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
/// Is this function signature variadic? |
| 635 |
pub(crate) fn is_variadic(&self) -> bool { |
| 636 |
// Clang reports some functions as variadic when they *might* be |
| 637 |
// variadic. We do the argument check because rust doesn't codegen well |
| 638 |
// variadic functions without an initial argument. |
| 639 |
self.is_variadic && !self.argument_types.is_empty() |
| 640 |
} |
| 641 |
|
| 642 |
/// Must this function's return value be used? |
| 643 |
pub(crate) fn must_use(&self) -> bool { |
| 644 |
self.must_use |
| 645 |
} |
| 646 |
|
| 647 |
/// Are function pointers with this signature able to derive Rust traits? |
| 648 |
/// Rust only supports deriving traits for function pointers with a limited |
| 649 |
/// number of parameters and a couple ABIs. |
| 650 |
/// |
| 651 |
/// For more details, see: |
| 652 |
/// |
| 653 |
/// * <https://github.com/rust-lang/rust-bindgen/issues/547>, |
| 654 |
/// * <https://github.com/rust-lang/rust/issues/38848>, |
| 655 |
/// * and <https://github.com/rust-lang/rust/issues/40158> |
| 656 |
pub(crate) fn function_pointers_can_derive(&self) -> bool { |
| 657 |
if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT { |
| 658 |
return false; |
| 659 |
} |
| 660 |
|
| 661 |
matches!(self.abi, ClangAbi::Known(Abi::C) | ClangAbi::Unknown(..)) |
| 662 |
} |
| 663 |
|
| 664 |
/// Whether this function has attributes marking it as divergent. |
| 665 |
pub(crate) fn is_divergent(&self) -> bool { |
| 666 |
self.is_divergent |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
impl ClangSubItemParser for Function { |
| 671 |
fn parse( |
| 672 |
cursor: clang::Cursor, |
| 673 |
context: &mut BindgenContext, |
| 674 |
) -> Result<ParseResult<Self>, ParseError> { |
| 675 |
use clang_sys::*; |
| 676 |
|
| 677 |
let kind = match FunctionKind::from_cursor(&cursor) { |
| 678 |
None => return Err(ParseError::Continue), |
| 679 |
Some(k) => k, |
| 680 |
}; |
| 681 |
|
| 682 |
debug!("Function::parse({:?}, {:?})", cursor, cursor.cur_type()); |
| 683 |
let visibility = cursor.visibility(); |
| 684 |
if visibility != CXVisibility_Default { |
| 685 |
return Err(ParseError::Continue); |
| 686 |
} |
| 687 |
|
| 688 |
if cursor.access_specifier() == CX_CXXPrivate { |
| 689 |
return Err(ParseError::Continue); |
| 690 |
} |
| 691 |
|
| 692 |
let linkage = cursor.linkage(); |
| 693 |
let linkage = match linkage { |
| 694 |
CXLinkage_External | CXLinkage_UniqueExternal => Linkage::External, |
| 695 |
CXLinkage_Internal => Linkage::Internal, |
| 696 |
_ => return Err(ParseError::Continue), |
| 697 |
}; |
| 698 |
|
| 699 |
if cursor.is_inlined_function() || |
| 700 |
cursor |
| 701 |
.definition() |
| 702 |
.map_or(false, |x| x.is_inlined_function()) |
| 703 |
{ |
| 704 |
if !context.options().generate_inline_functions && |
| 705 |
!context.options().wrap_static_fns |
| 706 |
{ |
| 707 |
return Err(ParseError::Continue); |
| 708 |
} |
| 709 |
|
| 710 |
if cursor.is_deleted_function() { |
| 711 |
return Err(ParseError::Continue); |
| 712 |
} |
| 713 |
|
| 714 |
// We cannot handle `inline` functions that are not `static`. |
| 715 |
if context.options().wrap_static_fns && |
| 716 |
cursor.is_inlined_function() && |
| 717 |
matches!(linkage, Linkage::External) |
| 718 |
{ |
| 719 |
return Err(ParseError::Continue); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
// Grab the signature using Item::from_ty. |
| 724 |
let sig = Item::from_ty(&cursor.cur_type(), cursor, None, context)?; |
| 725 |
|
| 726 |
let mut name = cursor.spelling(); |
| 727 |
assert!(!name.is_empty(), "Empty function name?"); |
| 728 |
|
| 729 |
if cursor.kind() == CXCursor_Destructor { |
| 730 |
// Remove the leading `~`. The alternative to this is special-casing |
| 731 |
// code-generation for destructor functions, which seems less than |
| 732 |
// ideal. |
| 733 |
if name.starts_with('~') { |
| 734 |
name.remove(0); |
| 735 |
} |
| 736 |
|
| 737 |
// Add a suffix to avoid colliding with constructors. This would be |
| 738 |
// technically fine (since we handle duplicated functions/methods), |
| 739 |
// but seems easy enough to handle it here. |
| 740 |
name.push_str("_destructor"); |
| 741 |
} |
| 742 |
if let Some(nm) = context.options().last_callback(|callbacks| { |
| 743 |
callbacks.generated_name_override(ItemInfo { |
| 744 |
name: name.as_str(), |
| 745 |
kind: ItemKind::Function, |
| 746 |
}) |
| 747 |
}) { |
| 748 |
name = nm; |
| 749 |
} |
| 750 |
assert!(!name.is_empty(), "Empty function name."); |
| 751 |
|
| 752 |
let mangled_name = cursor_mangling(context, &cursor); |
| 753 |
|
| 754 |
let link_name = context.options().last_callback(|callbacks| { |
| 755 |
callbacks.generated_link_name_override(ItemInfo { |
| 756 |
name: name.as_str(), |
| 757 |
kind: ItemKind::Function, |
| 758 |
}) |
| 759 |
}); |
| 760 |
|
| 761 |
let function = Self::new( |
| 762 |
name.clone(), |
| 763 |
mangled_name, |
| 764 |
link_name, |
| 765 |
sig, |
| 766 |
kind, |
| 767 |
linkage, |
| 768 |
); |
| 769 |
|
| 770 |
Ok(ParseResult::New(function, Some(cursor))) |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
impl Trace for FunctionSig { |
| 775 |
type Extra = (); |
| 776 |
|
| 777 |
fn trace<T>(&self, _: &BindgenContext, tracer: &mut T, _: &()) |
| 778 |
where |
| 779 |
T: Tracer, |
| 780 |
{ |
| 781 |
tracer.visit_kind(self.return_type().into(), EdgeKind::FunctionReturn); |
| 782 |
|
| 783 |
for &(_, ty) in self.argument_types() { |
| 784 |
tracer.visit_kind(ty.into(), EdgeKind::FunctionParameter); |
| 785 |
} |
| 786 |
} |
| 787 |
} |