From cc82b839b164f38513dcdcc93a63d654c82ca80c Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 7 Nov 2016 13:43:34 -0800 Subject: [PATCH 1/3] Useful little helpers for Item and Type --- src/ir/item.rs | 11 +++++++++++ src/ir/ty.rs | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/src/ir/item.rs b/src/ir/item.rs index a07ee1f36f..c6d80a0833 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 be74926816..2c1e5f8efa 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 { use std::mem; From 4d45f83d1ed0c768795080cd0af87f5d06089da0 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 7 Nov 2016 14:09:40 -0800 Subject: [PATCH 2/3] Add `clang::Type::is_valid` A helper for checking if the type's kind is not `CXType_Invalid`. --- src/clang.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/clang.rs b/src/clang.rs index a11e2924ed..b250e5487a 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -715,6 +715,11 @@ impl Type { } } } + + /// Is this a valid type? + pub fn is_valid(&self) -> bool { + self.kind() != CXType_Invalid + } } /// An iterator for a type's template arguments. From d5ee20c9e566db5fc8141a43f886c03377c4389e Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 7 Nov 2016 14:14:33 -0800 Subject: [PATCH 3/3] Use `clang::Type::is_valid` instead of checking self.kind() against CXType_Invalid --- src/clang.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/clang.rs b/src/clang.rs index b250e5487a..d9fbd7b86d 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -640,7 +640,7 @@ impl Type { let ret = Type { x: unsafe { clang_getPointeeType(self.x) }, }; - debug_assert!(ret.kind() != CXType_Invalid); + debug_assert!(ret.is_valid()); Some(ret) } _ => None, @@ -653,7 +653,7 @@ impl Type { let current_type = Type { x: unsafe { clang_getElementType(self.x) }, }; - if current_type.kind() != CXType_Invalid { + if current_type.is_valid() { Some(current_type) } else { None @@ -692,10 +692,10 @@ impl Type { let rt = Type { x: unsafe { clang_getResultType(self.x) }, }; - if rt.kind() == CXType_Invalid { - None - } else { + if rt.is_valid() { Some(rt) + } else { + None } }