diff options
author | Xidorn Quan <me@upsuper.org> | 2016-11-17 11:43:10 +1100 |
---|---|---|
committer | Xidorn Quan <me@upsuper.org> | 2016-11-17 12:42:28 +1100 |
commit | 47cb4e34df94480780ea2dc4d7070eb306f1193b (patch) | |
tree | 272cd673fca2f27f413b051eba654866b474114d | |
parent | ea1d6449ec1b0fde9eef2d3f2fe9846c1ac336ff (diff) |
Generate bool value for bool constants
This fixes #272.
-rw-r--r-- | libbindgen/src/codegen/helpers.rs | 4 | ||||
-rw-r--r-- | libbindgen/src/codegen/mod.rs | 4 | ||||
-rw-r--r-- | libbindgen/src/ir/ty.rs | 8 | ||||
-rw-r--r-- | libbindgen/src/ir/var.rs | 8 | ||||
-rw-r--r-- | libbindgen/tests/expectations/tests/const_bool.rs | 23 | ||||
-rw-r--r-- | libbindgen/tests/headers/const_bool.hpp | 9 |
6 files changed, 55 insertions, 1 deletions
diff --git a/libbindgen/src/codegen/helpers.rs b/libbindgen/src/codegen/helpers.rs index 8c3d3cea..7284ab80 100644 --- a/libbindgen/src/codegen/helpers.rs +++ b/libbindgen/src/codegen/helpers.rs @@ -133,6 +133,10 @@ pub mod ast_ty { } } + pub fn bool_expr(val: bool) -> P<ast::Expr> { + aster::AstBuilder::new().expr().bool(val) + } + pub fn byte_array_expr(bytes: &[u8]) -> P<ast::Expr> { let mut vec = Vec::with_capacity(bytes.len() + 1); for byte in bytes { diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index ceb023f7..f15b92d1 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -323,6 +323,10 @@ impl CodeGenerator for Var { .const_(canonical_name) .expr(); let item = match *val { + VarType::Bool(val) => { + const_item.build(helpers::ast_ty::bool_expr(val)) + .build(ty) + } VarType::Int(val) => { const_item.build(helpers::ast_ty::int_expr(val)) .build(ty) diff --git a/libbindgen/src/ir/ty.rs b/libbindgen/src/ir/ty.rs index 1cd255a5..b87dd11b 100644 --- a/libbindgen/src/ir/ty.rs +++ b/libbindgen/src/ir/ty.rs @@ -131,6 +131,14 @@ impl Type { } } + /// Is this a boolean type? + pub fn is_bool(&self) -> bool { + match self.kind { + TypeKind::Int(IntKind::Bool) => true, + _ => false, + } + } + /// Is this an integer type? pub fn is_integer(&self) -> bool { match self.kind { diff --git a/libbindgen/src/ir/var.rs b/libbindgen/src/ir/var.rs index 3270b332..98b94da3 100644 --- a/libbindgen/src/ir/var.rs +++ b/libbindgen/src/ir/var.rs @@ -13,6 +13,8 @@ use super::ty::{FloatKind, TypeKind}; /// The type for a constant variable. #[derive(Debug)] pub enum VarType { + /// An boolean. + Bool(bool), /// An integer. Int(i64), /// A floating point number. @@ -194,6 +196,7 @@ impl ClangSubItemParser for Var { let canonical_ty = ctx.safe_resolve_type(ty) .and_then(|t| t.safe_canonical_type(ctx)); + let is_bool = canonical_ty.map_or(false, |t| t.is_bool()); let is_integer = canonical_ty.map_or(false, |t| t.is_integer()); let is_float = canonical_ty.map_or(false, |t| t.is_float()); @@ -201,7 +204,10 @@ impl ClangSubItemParser for Var { // TODO: Strings, though the lookup is a bit more hard (we need // to look at the canonical type of the pointee too, and check // is char, u8, or i8 I guess). - let value = if is_integer { + let value = if is_bool { + cursor.evaluate().as_int() + .map(|val| VarType::Bool(val != 0)) + } else if is_integer { cursor.evaluate() .as_int() .map(|val| val as i64) diff --git a/libbindgen/tests/expectations/tests/const_bool.rs b/libbindgen/tests/expectations/tests/const_bool.rs new file mode 100644 index 00000000..8a50a094 --- /dev/null +++ b/libbindgen/tests/expectations/tests/const_bool.rs @@ -0,0 +1,23 @@ +/* automatically generated by rust-bindgen */ + + +#![allow(non_snake_case)] + + +pub const k: bool = true; +#[repr(C)] +#[derive(Debug, Copy)] +pub struct A { + pub _address: u8, +} +pub const A_k: bool = false; +#[test] +fn bindgen_test_layout_A() { + assert_eq!(::std::mem::size_of::<A>() , 1usize); + assert_eq!(::std::mem::align_of::<A>() , 1usize); +} +impl Clone for A { + fn clone(&self) -> Self { *self } +} +pub type foo = bool; +pub const k2: foo = true; diff --git a/libbindgen/tests/headers/const_bool.hpp b/libbindgen/tests/headers/const_bool.hpp new file mode 100644 index 00000000..633a7c90 --- /dev/null +++ b/libbindgen/tests/headers/const_bool.hpp @@ -0,0 +1,9 @@ +// bindgen-unstable + +const bool k = true; +struct A { + static const bool k = false; +}; + +typedef bool foo; +const foo k2 = true; |