1 //! A public API for more fine-grained customization of bindgen behavior.
2 
3 pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
4 pub use crate::ir::int::IntKind;
5 use std::fmt;
6 use std::panic::UnwindSafe;
7 
8 /// An enum to allow ignoring parsing of macros.
9 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
10 pub enum MacroParsingBehavior {
11     /// Ignore the macro, generating no code for it, or anything that depends on
12     /// it.
13     Ignore,
14     /// The default behavior bindgen would have otherwise.
15     Default,
16 }
17 
18 impl Default for MacroParsingBehavior {
default() -> Self19     fn default() -> Self {
20         MacroParsingBehavior::Default
21     }
22 }
23 
24 /// A trait to allow configuring different kinds of types in different
25 /// situations.
26 pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
27     /// This function will be run on every macro that is identified.
will_parse_macro(&self, _name: &str) -> MacroParsingBehavior28     fn will_parse_macro(&self, _name: &str) -> MacroParsingBehavior {
29         MacroParsingBehavior::Default
30     }
31 
32     /// The integer kind an integer macro should have, given a name and the
33     /// value of that macro, or `None` if you want the default to be chosen.
int_macro(&self, _name: &str, _value: i64) -> Option<IntKind>34     fn int_macro(&self, _name: &str, _value: i64) -> Option<IntKind> {
35         None
36     }
37 
38     /// This will be run on every string macro. The callback cannot influence the further
39     /// treatment of the macro, but may use the value to generate additional code or configuration.
str_macro(&self, _name: &str, _value: &[u8])40     fn str_macro(&self, _name: &str, _value: &[u8]) {}
41 
42     /// This will be run on every function-like macro. The callback cannot
43     /// influence the further treatment of the macro, but may use the value to
44     /// generate additional code or configuration.
45     ///
46     /// The first parameter represents the name and argument list (including the
47     /// parentheses) of the function-like macro. The second parameter represents
48     /// the expansion of the macro as a sequence of tokens.
func_macro(&self, _name: &str, _value: &[&[u8]])49     fn func_macro(&self, _name: &str, _value: &[&[u8]]) {}
50 
51     /// This function should return whether, given an enum variant
52     /// 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>53     fn enum_variant_behavior(
54         &self,
55         _enum_name: Option<&str>,
56         _original_variant_name: &str,
57         _variant_value: EnumVariantValue,
58     ) -> Option<EnumVariantCustomBehavior> {
59         None
60     }
61 
62     /// 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>63     fn enum_variant_name(
64         &self,
65         _enum_name: Option<&str>,
66         _original_variant_name: &str,
67         _variant_value: EnumVariantValue,
68     ) -> Option<String> {
69         None
70     }
71 
72     /// Allows to rename an item, replacing `_original_item_name`.
item_name(&self, _original_item_name: &str) -> Option<String>73     fn item_name(&self, _original_item_name: &str) -> Option<String> {
74         None
75     }
76 
77     /// This will be called on every file inclusion, with the full path of the included file.
include_file(&self, _filename: &str)78     fn include_file(&self, _filename: &str) {}
79 }
80