Skip to content

Add clang::Type::is_valid and use it instead of checking self.kind() against CXType_Invalid #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}

Expand All @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
///
Expand Down
9 changes: 9 additions & 0 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down