1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::ffi::CStr;
use std::fmt;
use super::{BasicType, DescriptionLevel};
use super::stream::SBStream;
use sys;

#[allow(missing_docs)]
pub struct SBType {
    /// The underlying raw `SBTypeRef`.
    pub raw: sys::SBTypeRef,
}

impl SBType {
    /// Construct a new `SBType`.
    pub fn wrap(raw: sys::SBTypeRef) -> SBType {
        SBType { raw: raw }
    }

    /// Construct a new `Some(SBType)` or `None`.
    pub fn maybe_wrap(raw: sys::SBTypeRef) -> Option<SBType> {
        if unsafe { sys::SBTypeIsValid(raw) != 0 } {
            Some(SBType { raw: raw })
        } else {
            None
        }
    }

    /// Check whether or not this is a valid `SBType` value.
    pub fn is_valid(&self) -> bool {
        unsafe { sys::SBTypeIsValid(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn is_pointer_type(&self) -> bool {
        unsafe { sys::SBTypeIsPointerType(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn is_reference_type(&self) -> bool {
        unsafe { sys::SBTypeIsReferenceType(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn is_function_type(&self) -> bool {
        unsafe { sys::SBTypeIsFunctionType(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn is_polymorphic_class(&self) -> bool {
        unsafe { sys::SBTypeIsPolymorphicClass(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn is_array_type(&self) -> bool {
        unsafe { sys::SBTypeIsArrayType(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn is_vector_type(&self) -> bool {
        unsafe { sys::SBTypeIsVectorType(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn is_typedef_type(&self) -> bool {
        unsafe { sys::SBTypeIsTypedefType(self.raw) != 0 }
    }

    #[allow(missing_docs)]
    pub fn pointer_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetPointerType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn pointee_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetPointeeType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn reference_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetReferenceType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn typedefed_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetTypedefedType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn dereferenced_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetDereferencedType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn unqualified_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetUnqualifiedType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn array_element_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetArrayElementType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn vector_element_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetVectorElementType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn canonical_type(&self) -> Option<SBType> {
        SBType::maybe_wrap(unsafe { sys::SBTypeGetCanonicalType(self.raw) })
    }

    #[allow(missing_docs)]
    pub fn basic_type(&self) -> BasicType {
        unsafe { sys::SBTypeGetBasicType(self.raw) }
    }

    #[allow(missing_docs)]
    pub fn name(&self) -> &str {
        unsafe {
            match CStr::from_ptr(sys::SBTypeGetName(self.raw)).to_str() {
                Ok(s) => s,
                _ => panic!("Invalid string?"),
            }
        }
    }

    #[allow(missing_docs)]
    pub fn display_type_name(&self) -> &str {
        unsafe {
            match CStr::from_ptr(sys::SBTypeGetDisplayTypeName(self.raw)).to_str() {
                Ok(s) => s,
                _ => panic!("Invalid string?"),
            }
        }
    }
}

impl fmt::Debug for SBType {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        let stream = SBStream::new();
        unsafe { sys::SBTypeGetDescription(self.raw, stream.raw, DescriptionLevel::Brief) };
        write!(fmt, "SBType {{ {} }}", stream.data())
    }
}

impl Drop for SBType {
    fn drop(&mut self) {
        unsafe { sys::DisposeSBType(self.raw) };
    }
}

#[cfg(feature = "graphql")]
graphql_object!(SBType: super::debugger::SBDebugger | &self | {
    field is_valid() -> bool {
        self.is_valid()
    }

    field is_pointer_type() -> bool {
        self.is_pointer_type()
    }

    field is_reference_type() -> bool {
        self.is_reference_type()
    }

    field is_function_type() -> bool {
        self.is_function_type()
    }

    field is_polymorphic_class() -> bool {
        self.is_polymorphic_class()
    }

    field is_array_type() -> bool {
        self.is_array_type()
    }

    field is_vector_type() -> bool {
        self.is_vector_type()
    }

    field is_typedef_type() -> bool {
        self.is_typedef_type()
    }

    field pointer_type() -> Option<SBType> {
        self.pointer_type()
    }

    field pointee_type() -> Option<SBType> {
        self.pointee_type()
    }

    field reference_type() -> Option<SBType> {
        self.reference_type()
    }

    field typedefed_type() -> Option<SBType> {
        self.typedefed_type()
    }

    field dereferenced_type() -> Option<SBType> {
        self.dereferenced_type()
    }

    field unqualified_type() -> Option<SBType> {
        self.unqualified_type()
    }

    field array_element_type() -> Option<SBType> {
        self.array_element_type()
    }

    field vector_element_type() -> Option<SBType> {
        self.vector_element_type()
    }

    field canonical_type() -> Option<SBType> {
        self.canonical_type()
    }

    // TODO(bm) bind `basic_type`.

    field name() -> &str {
        self.name()
    }
});