summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xsrc/clang.rs14
-rw-r--r--src/ir/enum_ty.rs2
-rw-r--r--src/ir/item.rs11
-rw-r--r--src/ir/ty.rs9
4 files changed, 30 insertions, 6 deletions
diff --git a/src/clang.rs b/src/clang.rs
index 3ea4ad2f..c6af517c 100755
--- a/src/clang.rs
+++ b/src/clang.rs
@@ -361,11 +361,15 @@ impl Cursor {
/// Get the unsigned constant value for this cursor's enum variant referent.
///
- /// Returns `ULLONG_MAX` if the cursor's referent is not an enum variant,
- /// which is also a valid enum value, so callers should check the cursor
- /// kind before calling this method (see issue #128).
- pub fn enum_val_unsigned(&self) -> u64 {
- unsafe { clang_getEnumConstantDeclUnsignedValue(self.x) as u64 }
+ /// Returns None if the cursor's referent is not an enum variant.
+ pub fn enum_val_unsigned(&self) -> Option<u64> {
+ unsafe {
+ if self.kind() == CXCursor_EnumConstantDecl {
+ Some(clang_getEnumConstantDeclUnsignedValue(self.x) as u64)
+ } else {
+ None
+ }
+ }
}
/// Given that this cursor's referent is a `typedef`, get the `Type` that is
diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs
index 1bcd5b14..8efd9e3b 100644
--- a/src/ir/enum_ty.rs
+++ b/src/ir/enum_ty.rs
@@ -75,7 +75,7 @@ impl Enum {
let value = if is_signed {
cursor.enum_val_signed().map(EnumVariantValue::Signed)
} else {
- Some(EnumVariantValue::Unsigned(cursor.enum_val_unsigned()))
+ cursor.enum_val_unsigned().map(EnumVariantValue::Unsigned)
};
if let Some(val) = value {
let name = cursor.spelling();
diff --git a/src/ir/item.rs b/src/ir/item.rs
index a07ee1f3..c6d80a08 100644
--- a/src/ir/item.rs
+++ b/src/ir/item.rs
@@ -331,6 +331,12 @@ impl Item {
self.kind().expect_type()
}
+ /// Get a reference to this item's underlying `Type`, or `None` if this is
+ /// some other kind of item.
+ pub fn as_type(&self) -> Option<&Type> {
+ self.kind().as_type()
+ }
+
/// Get a reference to this item's underlying `Function`. Panic if this is
/// some other kind of item.
pub fn expect_function(&self) -> &Function {
@@ -531,6 +537,11 @@ impl Item {
ctx.opaque_by_name(&self.real_canonical_name(ctx, false, true))
}
+ /// Is this a reference to another type?
+ pub fn is_type_ref(&self) -> bool {
+ self.as_type().map_or(false, |ty| ty.is_type_ref())
+ }
+
/// Get the canonical name without taking into account the replaces
/// annotation.
///
diff --git a/src/ir/ty.rs b/src/ir/ty.rs
index be749268..2c1e5f8e 100644
--- a/src/ir/ty.rs
+++ b/src/ir/ty.rs
@@ -136,6 +136,15 @@ impl Type {
self.is_const
}
+ /// Is this a reference to another type?
+ pub fn is_type_ref(&self) -> bool {
+ match self.kind {
+ TypeKind::ResolvedTypeRef(_) |
+ TypeKind::UnresolvedTypeRef(_, _, _) => true,
+ _ => false,
+ }
+ }
+
/// What is the layout of this type?
pub fn layout(&self, ctx: &BindgenContext) -> Option<Layout> {
use std::mem;