summaryrefslogtreecommitdiff
path: root/src/codegen/mod.rs
diff options
context:
space:
mode:
authorChih-Hung Hsieh <chh@google.com>2020-08-08 16:20:47 -0700
committerEmilio Cobos Álvarez <emilio@crisal.io>2020-08-09 13:01:46 +0200
commit53290e8f3535f118bcc06c048d34740233ef7821 (patch)
tree1e347f692a9ca8c3fe7cac7384273cea8dc7b98b /src/codegen/mod.rs
parent1127561bb232fe0aa3ef48cc8404b26ed3b7116c (diff)
Add --default-macro-constant-type
* --default-macro-constant-type could be 'signed' or 'unsigned' * Its default value is 'unsigned' to use u32/u64 for C macro constants that fit into the u32/u64 ranges. * For old C libraries that use macros as int/long parameter and/or return value types, their macros are better declared as i32/i64 if the values fit the i32/i64 ranges, to be compatible with c_int/c_long types. They can use "--default-macro-constant-type signed"
Diffstat (limited to 'src/codegen/mod.rs')
-rw-r--r--src/codegen/mod.rs44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 203da256..de46bb22 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -3048,6 +3048,50 @@ impl CodeGenerator for Enum {
}
}
+/// Enum for the default type of macro constants.
+#[derive(Copy, Clone, PartialEq, Debug)]
+pub enum MacroTypeVariation {
+ /// Use i32 or i64
+ Signed,
+ /// Use u32 or u64
+ Unsigned,
+}
+
+impl MacroTypeVariation {
+ /// Convert a `MacroTypeVariation` to its str representation.
+ pub fn as_str(&self) -> &str {
+ match self {
+ MacroTypeVariation::Signed => "signed",
+ MacroTypeVariation::Unsigned => "unsigned",
+ }
+ }
+}
+
+impl Default for MacroTypeVariation {
+ fn default() -> MacroTypeVariation {
+ MacroTypeVariation::Unsigned
+ }
+}
+
+impl std::str::FromStr for MacroTypeVariation {
+ type Err = std::io::Error;
+
+ /// Create a `MacroTypeVariation` from a string.
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ match s {
+ "signed" => Ok(MacroTypeVariation::Signed),
+ "unsigned" => Ok(MacroTypeVariation::Unsigned),
+ _ => Err(std::io::Error::new(
+ std::io::ErrorKind::InvalidInput,
+ concat!(
+ "Got an invalid MacroTypeVariation. Accepted values ",
+ "are 'signed' and 'unsigned'"
+ ),
+ )),
+ }
+ }
+}
+
/// Enum for how aliases should be translated.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum AliasVariation {