• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! A public API for more fine-grained customization of bindgen behavior.
2 
3 pub use crate::ir::analysis::DeriveTrait;
4 pub use crate::ir::derive::CanDerive as ImplementsTrait;
5 pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
6 pub use crate::ir::int::IntKind;
7 use std::fmt;
8 use std::panic::UnwindSafe;
9 
10 /// An enum to allow ignoring parsing of macros.
11 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
12 pub enum MacroParsingBehavior {
13     /// Ignore the macro, generating no code for it, or anything that depends on
14     /// it.
15     Ignore,
16     /// The default behavior bindgen would have otherwise.
17     Default,
18 }
19 
20 impl Default for MacroParsingBehavior {
default() -> Self21     fn default() -> Self {
22         MacroParsingBehavior::Default
23     }
24 }
25 
26 /// A trait to allow configuring different kinds of types in different
27 /// situations.
28 pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
29     /// This function will be run on every macro that is identified.
will_parse_macro(&self, _name: &str) -> MacroParsingBehavior30     fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
31         MacroParsingBehavior::Default
32     }
33 
34     /// The integer kind an integer macro should have, given a name and the
35     /// value of that macro, or `None` if you want the default to be chosen.
int_macro(&self, _name: &str, _value: i64) -> Option<IntKind>36     fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {
37         None
38     }
39 
40     /// This will be run on every string macro. The callback cannot influence the further
41     /// treatment of the macro, but may use the value to generate additional code or configuration.
str_macro(&self, _name: &str, _value: &[u8])42     fn str_macro(&self, _name: &str, _value: &[u8]) {}
43 
44     /// This will be run on every function-like macro. The callback cannot
45     /// influence the further treatment of the macro, but may use the value to
46     /// generate additional code or configuration.
47     ///
48     /// The first parameter represents the name and argument list (including the
49     /// parentheses) of the function-like macro. The second parameter represents
50     /// the expansion of the macro as a sequence of tokens.
func_macro(&self, _name: &str, _value: &[&[u8]])51     fn func_macro(&self, _name: &str, _value: &[&[u8]]) {}
52 
53     /// This function should return whether, given an enum variant
54     /// name, and value, this enum variant will forcibly be a constant.
enum_variant_behavior( &self, _enum_name: Option<&str>, _original_variant_name: &str, _variant_value: EnumVariantValue, ) -> Option<EnumVariantCustomBehavior>55     fn enum_variant_behavior(
56         &self,
57         _enum_name: Option<&str>,
58         _original_variant_name: &str,
59         _variant_value: EnumVariantValue,
60     ) -> Option<EnumVariantCustomBehavior> {
61         None
62     }
63 
64     /// Allows to rename an enum variant, replacing `_original_variant_name`.
enum_variant_name( &self, _enum_name: Option<&str>, _original_variant_name: &str, _variant_value: EnumVariantValue, ) -> Option<String>65     fn enum_variant_name(
66         &self,
67         _enum_name: Option<&str>,
68         _original_variant_name: &str,
69         _variant_value: EnumVariantValue,
70     ) -> Option<String> {
71         None
72     }
73 
74     /// Allows to rename an item, replacing `_original_item_name`.
item_name(&self, _original_item_name: &str) -> Option<String>75     fn item_name(&self, _original_item_name: &str) -> Option<String> {
76         None
77     }
78 
79     /// This will be called on every file inclusion, with the full path of the included file.
include_file(&self, _filename: &str)80     fn include_file(&self, _filename: &str) {}
81 
82     /// This will be called to determine whether a particular blocklisted type
83     /// implements a trait or not. This will be used to implement traits on
84     /// other types containing the blocklisted type.
85     ///
86     /// * `None`: use the default behavior
87     /// * `Some(ImplementsTrait::Yes)`: `_name` implements `_derive_trait`
88     /// * `Some(ImplementsTrait::Manually)`: any type including `_name` can't
89     ///   derive `_derive_trait` but can implemented it manually
90     /// * `Some(ImplementsTrait::No)`: `_name` doesn't implement `_derive_trait`
blocklisted_type_implements_trait( &self, _name: &str, _derive_trait: DeriveTrait, ) -> Option<ImplementsTrait>91     fn blocklisted_type_implements_trait(
92         &self,
93         _name: &str,
94         _derive_trait: DeriveTrait,
95     ) -> Option<ImplementsTrait> {
96         None
97     }
98 }
99