1use super::comp::MethodKind;
4use super::context::{BindgenContext, TypeId};
5use super::dot::DotAttributes;
6use super::item::Item;
7use super::traversal::{EdgeKind, Trace, Tracer};
8use super::ty::TypeKind;
9use crate::callbacks::{ItemInfo, ItemKind};
10use crate::clang::{self, ABIKind, Attribute};
11use crate::parse::{ClangSubItemParser, ParseError, ParseResult};
12use clang_sys::CXCallingConv;
13
14use quote::TokenStreamExt;
15use std::io;
16use std::str::FromStr;
17
18const RUST_DERIVE_FUNPTR_LIMIT: usize = 12;
19
20#[derive(Debug, Copy, Clone, PartialEq, Eq)]
22pub(crate) enum FunctionKind {
23 Function,
25 Method(MethodKind),
27}
28
29impl FunctionKind {
30 pub(crate) fn from_cursor(cursor: &clang::Cursor) -> Option<FunctionKind> {
33 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#[derive(Debug, Clone, Copy)]
66pub(crate) enum Linkage {
67 External,
69 Internal,
71}
72
73#[derive(Debug)]
78pub(crate) struct Function {
79 name: String,
81
82 mangled_name: Option<String>,
84
85 link_name: Option<String>,
87
88 signature: TypeId,
90
91 kind: FunctionKind,
93
94 linkage: Linkage,
96}
97
98impl Function {
99 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 pub(crate) fn name(&self) -> &str {
120 &self.name
121 }
122
123 pub(crate) fn mangled_name(&self) -> Option<&str> {
125 self.mangled_name.as_deref()
126 }
127
128 pub fn link_name(&self) -> Option<&str> {
130 self.link_name.as_deref()
131 }
132
133 pub(crate) fn signature(&self) -> TypeId {
135 self.signature
136 }
137
138 pub(crate) fn kind(&self) -> FunctionKind {
140 self.kind
141 }
142
143 pub(crate) fn linkage(&self) -> Linkage {
145 self.linkage
146 }
147}
148
149impl 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!(out, "<tr><td>mangled name</td><td>{mangled}</td></tr>")?;
162 }
163
164 Ok(())
165 }
166}
167
168#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
170pub enum Abi {
171 C,
173 Stdcall,
175 EfiApi,
177 Fastcall,
179 ThisCall,
181 Vectorcall,
183 Aapcs,
185 Win64,
187 CUnwind,
189 System,
191}
192
193impl FromStr for Abi {
194 type Err = String;
195
196 fn from_str(s: &str) -> Result<Self, Self::Err> {
197 match s {
198 "C" => Ok(Self::C),
199 "stdcall" => Ok(Self::Stdcall),
200 "efiapi" => Ok(Self::EfiApi),
201 "fastcall" => Ok(Self::Fastcall),
202 "thiscall" => Ok(Self::ThisCall),
203 "vectorcall" => Ok(Self::Vectorcall),
204 "aapcs" => Ok(Self::Aapcs),
205 "win64" => Ok(Self::Win64),
206 "C-unwind" => Ok(Self::CUnwind),
207 "system" => Ok(Self::System),
208 _ => Err(format!("Invalid or unknown ABI {s:?}")),
209 }
210 }
211}
212
213impl std::fmt::Display for Abi {
214 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
215 let s = match *self {
216 Self::C => "C",
217 Self::Stdcall => "stdcall",
218 Self::EfiApi => "efiapi",
219 Self::Fastcall => "fastcall",
220 Self::ThisCall => "thiscall",
221 Self::Vectorcall => "vectorcall",
222 Self::Aapcs => "aapcs",
223 Self::Win64 => "win64",
224 Self::CUnwind => "C-unwind",
225 Abi::System => "system",
226 };
227
228 s.fmt(f)
229 }
230}
231
232impl 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#[derive(Debug, Copy, Clone)]
241pub(crate) enum ClangAbi {
242 Known(Abi),
244 Unknown(CXCallingConv),
246}
247
248impl ClangAbi {
249 fn is_unknown(self) -> bool {
251 matches!(self, ClangAbi::Unknown(..))
252 }
253}
254
255impl 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: {cc:?}"
261 ),
262 }
263 }
264}
265
266#[derive(Debug)]
268pub(crate) struct FunctionSig {
269 name: String,
271
272 return_type: TypeId,
274
275 argument_types: Vec<(Option<String>, TypeId)>,
278
279 is_variadic: bool,
281 is_divergent: bool,
282
283 must_use: bool,
285
286 abi: ClangAbi,
288}
289
290fn get_abi(cc: CXCallingConv) -> ClangAbi {
291 use clang_sys::*;
292 match cc {
293 CXCallingConv_Default | CXCallingConv_C => ClangAbi::Known(Abi::C),
294 CXCallingConv_X86StdCall => ClangAbi::Known(Abi::Stdcall),
295 CXCallingConv_X86FastCall => ClangAbi::Known(Abi::Fastcall),
296 CXCallingConv_X86ThisCall => ClangAbi::Known(Abi::ThisCall),
297 CXCallingConv_X86VectorCall | CXCallingConv_AArch64VectorCall => {
298 ClangAbi::Known(Abi::Vectorcall)
299 }
300 CXCallingConv_AAPCS => ClangAbi::Known(Abi::Aapcs),
301 CXCallingConv_X86_64Win64 => ClangAbi::Known(Abi::Win64),
302 other => ClangAbi::Unknown(other),
303 }
304}
305
306pub(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 if cursor.is_in_non_fully_specialized_template() {
319 return None;
320 }
321
322 let is_itanium_abi = ctx.abi_kind() == ABIKind::GenericItanium;
323 let is_destructor = cursor.kind() == clang_sys::CXCursor_Destructor;
324 if let Ok(mut manglings) = cursor.cxx_manglings() {
325 while let Some(m) = manglings.pop() {
326 if is_itanium_abi && is_destructor && !m.ends_with("D1Ev") {
328 continue;
329 }
330
331 return Some(m);
332 }
333 }
334
335 let mut mangling = cursor.mangling();
336 if mangling.is_empty() {
337 return None;
338 }
339
340 if is_itanium_abi && is_destructor {
341 if mangling.ends_with("D0Ev") {
361 let new_len = mangling.len() - 4;
362 mangling.truncate(new_len);
363 mangling.push_str("D1Ev");
364 }
365 }
366
367 Some(mangling)
368}
369
370fn args_from_ty_and_cursor(
371 ty: &clang::Type,
372 cursor: &clang::Cursor,
373 ctx: &mut BindgenContext,
374) -> Vec<(Option<String>, TypeId)> {
375 let cursor_args = cursor.args().unwrap_or_default().into_iter();
376 let type_args = ty.args().unwrap_or_default().into_iter();
377
378 cursor_args
388 .map(Some)
389 .chain(std::iter::repeat(None))
390 .zip(type_args.map(Some).chain(std::iter::repeat(None)))
391 .take_while(|(cur, ty)| cur.is_some() || ty.is_some())
392 .map(|(arg_cur, arg_ty)| {
393 let name = arg_cur.map(|a| a.spelling()).and_then(|name| {
394 if name.is_empty() {
395 None
396 } else {
397 Some(name)
398 }
399 });
400
401 let cursor = arg_cur.unwrap_or(*cursor);
402 let ty = arg_ty.unwrap_or_else(|| cursor.cur_type());
403 (name, Item::from_ty_or_ref(ty, cursor, None, ctx))
404 })
405 .collect()
406}
407
408impl FunctionSig {
409 pub(crate) fn name(&self) -> &str {
411 &self.name
412 }
413
414 pub(crate) fn from_ty(
416 ty: &clang::Type,
417 cursor: &clang::Cursor,
418 ctx: &mut BindgenContext,
419 ) -> Result<Self, ParseError> {
420 use clang_sys::*;
421 debug!("FunctionSig::from_ty {ty:?} {cursor:?}");
422
423 let kind = cursor.kind();
425 if kind == CXCursor_FunctionTemplate {
426 return Err(ParseError::Continue);
427 }
428
429 let spelling = cursor.spelling();
430
431 let is_operator = |spelling: &str| {
433 spelling.starts_with("operator") &&
434 !clang::is_valid_identifier(spelling)
435 };
436 if is_operator(&spelling) && !ctx.options().represent_cxx_operators {
437 return Err(ParseError::Continue);
438 }
439
440 if (kind == CXCursor_Constructor || kind == CXCursor_Destructor) &&
444 spelling.contains('<')
445 {
446 return Err(ParseError::Continue);
447 }
448
449 let cursor = if cursor.is_valid() {
450 *cursor
451 } else {
452 ty.declaration()
453 };
454
455 let mut args = match kind {
456 CXCursor_FunctionDecl |
457 CXCursor_Constructor |
458 CXCursor_CXXMethod |
459 CXCursor_ObjCInstanceMethodDecl |
460 CXCursor_ObjCClassMethodDecl => {
461 args_from_ty_and_cursor(ty, &cursor, ctx)
462 }
463 _ => {
464 let mut args = vec![];
467 cursor.visit(|c| {
468 if c.kind() == CXCursor_ParmDecl {
469 let ty =
470 Item::from_ty_or_ref(c.cur_type(), c, None, ctx);
471 let name = c.spelling();
472 let name =
473 if name.is_empty() { None } else { Some(name) };
474 args.push((name, ty));
475 }
476 CXChildVisit_Continue
477 });
478
479 if args.is_empty() {
480 args_from_ty_and_cursor(ty, &cursor, ctx)
485 } else {
486 args
487 }
488 }
489 };
490
491 let (must_use, mut is_divergent) =
492 if ctx.options().enable_function_attribute_detection {
493 let [must_use, no_return, no_return_cpp] = cursor.has_attrs(&[
494 Attribute::MUST_USE,
495 Attribute::NO_RETURN,
496 Attribute::NO_RETURN_CPP,
497 ]);
498 (must_use, no_return || no_return_cpp)
499 } else {
500 Default::default()
501 };
502
503 let ty_spelling = ty.spelling();
507 let has_attribute_noreturn = ty_spelling
508 .match_indices("__attribute__((noreturn))")
509 .any(|(i, _)| {
510 let depth = ty_spelling[..i]
511 .bytes()
512 .filter_map(|ch| match ch {
513 b'(' => Some(1),
514 b')' => Some(-1),
515 _ => None,
516 })
517 .sum::<isize>();
518 depth == 0
519 });
520 is_divergent = is_divergent || has_attribute_noreturn;
521
522 let is_method = kind == CXCursor_CXXMethod;
523 let is_constructor = kind == CXCursor_Constructor;
524 let is_destructor = kind == CXCursor_Destructor;
525 if (is_constructor || is_destructor || is_method) &&
526 cursor.lexical_parent() != cursor.semantic_parent()
527 {
528 return Err(ParseError::Continue);
530 }
531
532 if is_method || is_constructor || is_destructor {
533 let is_const = is_method && cursor.method_is_const();
534 let is_virtual = is_method && cursor.method_is_virtual();
535 let is_static = is_method && cursor.method_is_static();
536 if !is_static &&
537 (!is_virtual ||
538 ctx.options().use_specific_virtual_function_receiver)
539 {
540 let parent = cursor.semantic_parent();
541 let class = Item::parse(parent, None, ctx)
542 .expect("Expected to parse the class");
543 let class = class.as_type_id_unchecked();
546
547 let class = if is_const {
548 let const_class_id = ctx.next_item_id();
549 ctx.build_const_wrapper(
550 const_class_id,
551 class,
552 None,
553 &parent.cur_type(),
554 )
555 } else {
556 class
557 };
558
559 let ptr =
560 Item::builtin_type(TypeKind::Pointer(class), false, ctx);
561 args.insert(0, (Some("this".into()), ptr));
562 } else if is_virtual {
563 let void = Item::builtin_type(TypeKind::Void, false, ctx);
564 let ptr =
565 Item::builtin_type(TypeKind::Pointer(void), false, ctx);
566 args.insert(0, (Some("this".into()), ptr));
567 }
568 }
569
570 let ty_ret_type = if kind == CXCursor_ObjCInstanceMethodDecl ||
571 kind == CXCursor_ObjCClassMethodDecl
572 {
573 ty.ret_type()
574 .or_else(|| cursor.ret_type())
575 .ok_or(ParseError::Continue)?
576 } else {
577 ty.ret_type().ok_or(ParseError::Continue)?
578 };
579
580 let ret = if is_constructor && ctx.is_target_wasm32() {
581 let void = Item::builtin_type(TypeKind::Void, false, ctx);
584 Item::builtin_type(TypeKind::Pointer(void), false, ctx)
585 } else {
586 Item::from_ty_or_ref(ty_ret_type, cursor, None, ctx)
587 };
588
589 let mut call_conv = ty.call_conv();
592 if let Some(ty) = cursor.cur_type().canonical_type().pointee_type() {
593 let cursor_call_conv = ty.call_conv();
594 if cursor_call_conv != CXCallingConv_Invalid {
595 call_conv = cursor_call_conv;
596 }
597 }
598
599 let abi = get_abi(call_conv);
600
601 if abi.is_unknown() {
602 warn!("Unknown calling convention: {call_conv:?}");
603 }
604
605 Ok(Self {
606 name: spelling,
607 return_type: ret,
608 argument_types: args,
609 is_variadic: ty.is_variadic(),
610 is_divergent,
611 must_use,
612 abi,
613 })
614 }
615
616 pub(crate) fn return_type(&self) -> TypeId {
618 self.return_type
619 }
620
621 pub(crate) fn argument_types(&self) -> &[(Option<String>, TypeId)] {
623 &self.argument_types
624 }
625
626 pub(crate) fn abi(
628 &self,
629 ctx: &BindgenContext,
630 name: Option<&str>,
631 ) -> crate::codegen::error::Result<ClangAbi> {
632 let abi = if let Some(name) = name {
635 if let Some((abi, _)) = ctx
636 .options()
637 .abi_overrides
638 .iter()
639 .find(|(_, regex_set)| regex_set.matches(name))
640 {
641 ClangAbi::Known(*abi)
642 } else {
643 self.abi
644 }
645 } else if let Some((abi, _)) = ctx
646 .options()
647 .abi_overrides
648 .iter()
649 .find(|(_, regex_set)| regex_set.matches(&self.name))
650 {
651 ClangAbi::Known(*abi)
652 } else {
653 self.abi
654 };
655
656 match abi {
657 ClangAbi::Known(Abi::ThisCall)
658 if !ctx.options().rust_features().thiscall_abi =>
659 {
660 Err(crate::codegen::error::Error::UnsupportedAbi("thiscall"))
661 }
662 ClangAbi::Known(Abi::Vectorcall)
663 if !ctx.options().rust_features().vectorcall_abi =>
664 {
665 Err(crate::codegen::error::Error::UnsupportedAbi("vectorcall"))
666 }
667 ClangAbi::Known(Abi::CUnwind)
668 if !ctx.options().rust_features().c_unwind_abi =>
669 {
670 Err(crate::codegen::error::Error::UnsupportedAbi("C-unwind"))
671 }
672 ClangAbi::Known(Abi::EfiApi)
673 if !ctx.options().rust_features().abi_efiapi =>
674 {
675 Err(crate::codegen::error::Error::UnsupportedAbi("efiapi"))
676 }
677 ClangAbi::Known(Abi::Win64) if self.is_variadic() => {
678 Err(crate::codegen::error::Error::UnsupportedAbi("Win64"))
679 }
680 abi => Ok(abi),
681 }
682 }
683
684 pub(crate) fn is_variadic(&self) -> bool {
686 self.is_variadic && !self.argument_types.is_empty()
690 }
691
692 pub(crate) fn must_use(&self) -> bool {
694 self.must_use
695 }
696
697 pub(crate) fn function_pointers_can_derive(&self) -> bool {
707 if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
708 return false;
709 }
710
711 matches!(self.abi, ClangAbi::Known(Abi::C) | ClangAbi::Unknown(..))
712 }
713
714 pub(crate) fn is_divergent(&self) -> bool {
716 self.is_divergent
717 }
718}
719
720impl ClangSubItemParser for Function {
721 fn parse(
722 cursor: clang::Cursor,
723 context: &mut BindgenContext,
724 ) -> Result<ParseResult<Self>, ParseError> {
725 use clang_sys::*;
726
727 let kind = match FunctionKind::from_cursor(&cursor) {
728 None => return Err(ParseError::Continue),
729 Some(k) => k,
730 };
731
732 debug!("Function::parse({cursor:?}, {:?})", cursor.cur_type());
733 let visibility = cursor.visibility();
734 if visibility != CXVisibility_Default {
735 return Err(ParseError::Continue);
736 }
737 if cursor.access_specifier() == CX_CXXPrivate &&
738 !context.options().generate_private_functions
739 {
740 return Err(ParseError::Continue);
741 }
742
743 let linkage = cursor.linkage();
744 let linkage = match linkage {
745 CXLinkage_External | CXLinkage_UniqueExternal => Linkage::External,
746 CXLinkage_Internal => Linkage::Internal,
747 _ => return Err(ParseError::Continue),
748 };
749
750 if cursor.is_inlined_function() ||
751 cursor.definition().is_some_and(|x| x.is_inlined_function())
752 {
753 if !context.options().generate_inline_functions &&
754 !context.options().wrap_static_fns
755 {
756 return Err(ParseError::Continue);
757 }
758
759 if cursor.is_deleted_function() &&
760 !context.options().generate_deleted_functions
761 {
762 return Err(ParseError::Continue);
763 }
764
765 if context.options().wrap_static_fns &&
767 cursor.is_inlined_function() &&
768 matches!(linkage, Linkage::External)
769 {
770 return Err(ParseError::Continue);
771 }
772 }
773
774 let sig = Item::from_ty(&cursor.cur_type(), cursor, None, context)?;
776
777 let mut name = cursor.spelling();
778 assert!(!name.is_empty(), "Empty function name?");
779
780 if cursor.kind() == CXCursor_Destructor {
781 if name.starts_with('~') {
785 name.remove(0);
786 }
787
788 name.push_str("_destructor");
792 }
793 if let Some(nm) = context.options().last_callback(|callbacks| {
794 callbacks.generated_name_override(ItemInfo {
795 name: name.as_str(),
796 kind: ItemKind::Function,
797 })
798 }) {
799 name = nm;
800 }
801 assert!(!name.is_empty(), "Empty function name.");
802
803 let mangled_name = cursor_mangling(context, &cursor);
804
805 let link_name = context.options().last_callback(|callbacks| {
806 callbacks.generated_link_name_override(ItemInfo {
807 name: name.as_str(),
808 kind: ItemKind::Function,
809 })
810 });
811
812 let function = Self::new(
813 name.clone(),
814 mangled_name,
815 link_name,
816 sig,
817 kind,
818 linkage,
819 );
820
821 Ok(ParseResult::New(function, Some(cursor)))
822 }
823}
824
825impl Trace for FunctionSig {
826 type Extra = ();
827
828 fn trace<T>(&self, _: &BindgenContext, tracer: &mut T, _: &())
829 where
830 T: Tracer,
831 {
832 tracer.visit_kind(self.return_type().into(), EdgeKind::FunctionReturn);
833
834 for &(_, ty) in self.argument_types() {
835 tracer.visit_kind(ty.into(), EdgeKind::FunctionParameter);
836 }
837 }
838}