summaryrefslogtreecommitdiff
path: root/libbindgen/src
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2016-11-16 19:43:09 -0600
committerGitHub <noreply@github.com>2016-11-16 19:43:09 -0600
commitb9e15e9fc7f7100aa03256b6997639142ab38e87 (patch)
tree272cd673fca2f27f413b051eba654866b474114d /libbindgen/src
parente74acce09bc0f643a0084334f56837bb09f9cf42 (diff)
parent47cb4e34df94480780ea2dc4d7070eb306f1193b (diff)
Auto merge of #273 - upsuper:const-bool, r=emilio
Generate bool value for bool constants This also includes an unrelated commit which ensures newly-added test would be included.
Diffstat (limited to 'libbindgen/src')
-rw-r--r--libbindgen/src/codegen/helpers.rs4
-rw-r--r--libbindgen/src/codegen/mod.rs4
-rw-r--r--libbindgen/src/ir/ty.rs8
-rw-r--r--libbindgen/src/ir/var.rs8
4 files changed, 23 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)