Skip to content

Fix for #138 : rework for elem_type() which now returns Option<Type> instead of Type #178

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 1 commit into from
Nov 1, 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
9 changes: 6 additions & 3 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,12 @@ impl Type {

/// Given that this type is an array, vector, or complex type, return the
/// type of its elements.
pub fn elem_type(&self) -> Type {
unsafe {
Type { x: clang_getElementType(self.x) }
pub fn elem_type(&self) -> Option<Type> {
let current_type = Type { x: unsafe { clang_getElementType(self.x) } };
if current_type.kind() != CXType_Invalid {
Some(current_type)
} else {
None
}
}

Expand Down
12 changes: 9 additions & 3 deletions src/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,9 @@ impl Type {
CXType_VariableArray |
CXType_DependentSizedArray |
CXType_IncompleteArray => {
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
let inner = Item::from_ty(ty.elem_type().as_ref()
.expect("Not an appropriate type?"),
location, parent_id, ctx)
.expect("Not able to resolve array element?");
TypeKind::Pointer(inner)
}
Expand Down Expand Up @@ -694,14 +696,18 @@ impl Type {
// That being said, that should be fixed eventually.
CXType_Vector |
CXType_ConstantArray => {
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
let inner = Item::from_ty(ty.elem_type().as_ref()
.expect("Not an appropriate type?"),
location, parent_id, ctx)
.expect("Not able to resolve array element?");
TypeKind::Array(inner, ty.num_elements().unwrap())
}
// A complex number is always a real and an imaginary part, so
// represent that as a two-item array.
CXType_Complex => {
let inner = Item::from_ty(&ty.elem_type(), location, parent_id, ctx)
let inner = Item::from_ty(ty.elem_type().as_ref()
.expect("Not an appropriate type?"),
location, parent_id, ctx)
.expect("Not able to resolve array element?");
TypeKind::Array(inner, 2)
}
Expand Down