diff options
author | Emilio Cobos Álvarez <me@emiliocobos.me> | 2016-05-04 17:19:57 +0200 |
---|---|---|
committer | Emilio Cobos Álvarez <me@emiliocobos.me> | 2016-05-04 17:19:57 +0200 |
commit | eb64428b3efd78fcded7a299534da08785f3b7b4 (patch) | |
tree | 294942382a3a28350c90d8a2a693a098d2ce223d | |
parent | 9f78ea9c83b04dbf1b4ee5c11882ab5c878f7f90 (diff) |
clang: Add a fallible version of sizeof, and display_name()
-rw-r--r-- | src/clang.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/clang.rs b/src/clang.rs index a5cf1007..ed2ec3cd 100644 --- a/src/clang.rs +++ b/src/clang.rs @@ -25,6 +25,12 @@ impl Cursor { } } + pub fn display_name(&self) -> String { + unsafe { + String_ { x: clang_getCursorDisplayName(self.x) }.to_string() + } + } + pub fn mangling(&self) -> String { let mut mangling = unsafe { String_ { x: clang_Cursor_getMangling(self.x) }.to_string() @@ -268,6 +274,30 @@ pub struct Type { x: CXType } +#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)] +enum SizeofError { + Invalid, + Incomplete, + Dependent, + NotConstantSize, + InvalidFieldName, + Unknown, +} + +impl ::std::convert::From<i32> for SizeofError { + fn from(val: i32) -> Self { + use self::SizeofError::*; + match val { + CXTypeLayoutError_Invalid => Invalid, + CXTypeLayoutError_Incomplete => Incomplete, + CXTypeLayoutError_Dependent => Dependent, + CXTypeLayoutError_NotConstantSize => NotConstantSize, + CXTypeLayoutError_InvalidFieldName => InvalidFieldName, + _ => Unknown, + } + } +} + impl Type { // common pub fn kind(&self) -> Enum_CXTypeKind { @@ -286,6 +316,7 @@ impl Type { } } + // XXX make it more consistent // // This is currently only used to detect typedefs, @@ -315,6 +346,15 @@ impl Type { } } + pub fn fallible_size(&self) -> Result<usize, SizeofError> { + let val = unsafe { clang_Type_getSizeOf(self.x) }; + if val < 0 { + Err(SizeofError::from(val as i32)) + } else { + Ok(val as usize) + } + } + pub fn align(&self) -> usize { unsafe { let val = clang_Type_getAlignOf(self.x); |