From 24bb8e17f3564ad99650c69c9af46ac4456d93ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Normand?= Date: Mon, 31 Oct 2016 23:30:34 +0100 Subject: [PATCH] rework for elem_type() which now returns Option instead of Type --- src/clang.rs | 9 ++++++--- src/ir/ty.rs | 12 +++++++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/clang.rs b/src/clang.rs index 32eab97893..f9b3e0833f 100755 --- a/src/clang.rs +++ b/src/clang.rs @@ -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 { + let current_type = Type { x: unsafe { clang_getElementType(self.x) } }; + if current_type.kind() != CXType_Invalid { + Some(current_type) + } else { + None } } diff --git a/src/ir/ty.rs b/src/ir/ty.rs index 9881c6537f..25b119275a 100644 --- a/src/ir/ty.rs +++ b/src/ir/ty.rs @@ -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) } @@ -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) }