forked from sbinet/go-clang
/
type.go
197 lines (174 loc) · 5.07 KB
/
type.go
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
package clang
// #include <stdlib.h>
// #include "clang-c/Index.h"
// inline static
// CXCursor _go_clang_ocursor_at(CXCursor *c, int idx) {
// return c[idx];
// }
//
import "C"
import (
//"unsafe"
)
// TypeKind describes the kind of a type
type TypeKind uint32
const (
// Represents an invalid type (e.g., where no type is available).
TK_Invalid TypeKind = C.CXType_Invalid
// A type whose specific kind is not exposed via this interface.
TK_Unexposed = C.CXType_Unexposed
TK_Void = C.CXType_Void
TK_Bool = C.CXType_Bool
TK_Char_U = C.CXType_Char_U
TK_UChar = C.CXType_UChar
TK_Char16 = C.CXType_Char16
TK_Char32 = C.CXType_Char32
TK_UShort = C.CXType_UShort
TK_UInt = C.CXType_UInt
TK_ULong = C.CXType_ULong
TK_ULongLong = C.CXType_ULongLong
TK_UInt128 = C.CXType_UInt128
TK_Char_S = C.CXType_Char_S
TK_SChar = C.CXType_SChar
TK_WChar = C.CXType_WChar
TK_Short = C.CXType_Short
TK_Int = C.CXType_Int
TK_Long = C.CXType_Long
TK_LongLong = C.CXType_LongLong
TK_Int128 = C.CXType_Int128
TK_Float = C.CXType_Float
TK_Double = C.CXType_Double
TK_LongDouble = C.CXType_LongDouble
TK_NullPtr = C.CXType_NullPtr
TK_Overload = C.CXType_Overload
TK_Dependent = C.CXType_Dependent
TK_ObjCId = C.CXType_ObjCId
TK_ObjCClass = C.CXType_ObjCClass
TK_ObjCSel = C.CXType_ObjCSel
TK_FirstBuiltin = C.CXType_FirstBuiltin
TK_LastBuiltin = C.CXType_LastBuiltin
TK_Complex = C.CXType_Complex
TK_Pointer = C.CXType_Pointer
TK_BlockPointer = C.CXType_BlockPointer
TK_LValueReference = C.CXType_LValueReference
TK_RValueReference = C.CXType_RValueReference
TK_Record = C.CXType_Record
TK_Enum = C.CXType_Enum
TK_Typedef = C.CXType_Typedef
TK_ObjCInterface = C.CXType_ObjCInterface
TK_ObjCObjectPointer = C.CXType_ObjCObjectPointer
TK_FunctionNoProto = C.CXType_FunctionNoProto
TK_FunctionProto = C.CXType_FunctionProto
TK_ConstantArray = C.CXType_ConstantArray
)
func (t TypeKind) to_c() uint32 {
return uint32(t)
}
// Type represents the type of an element in the abstract syntax tree.
type Type struct {
c C.CXType
}
func (c Type) Kind() TypeKind {
return TypeKind(c.c.kind)
}
// Spelling returns the spelling of a given TypeKind.
func (t TypeKind) Spelling() string {
cstr := cxstring{C.clang_getTypeKindSpelling(t.to_c())}
defer cstr.Dispose()
return cstr.String()
}
// EqualTypes determines whether two Types represent the same type.
func EqualTypes(t1, t2 Type) bool {
o := C.clang_equalTypes(t1.c, t2.c)
if o != C.uint(0) {
return true
}
return false
}
// CanonicalType returns the canonical type for a Type.
//
// Clang's type system explicitly models typedefs and all the ways
// a specific type can be represented. The canonical type is the underlying
// type with all the "sugar" removed. For example, if 'T' is a typedef
// for 'int', the canonical type for 'T' would be 'int'.
func (t Type) CanonicalType() Type {
o := C.clang_getCanonicalType(t.c)
return Type{o}
}
// IsConstQualified determines whether a Type has the "const" qualifier set,
// without looking through typedefs that may have added "const" at a
// different level.
func (t Type) IsConstQualified() bool {
o := C.clang_isConstQualifiedType(t.c)
if o != C.uint(0) {
return true
}
return false
}
// IsVolatileQualified determines whether a Type has the "volatile" qualifier
// set, without looking through typedefs that may have added "volatile" at a
// different level.
func (t Type) IsVolatileQualified() bool {
o := C.clang_isVolatileQualifiedType(t.c)
if o != C.uint(0) {
return true
}
return false
}
// IsRestrictQualified determines whether a Type has the "restrict" qualifier
// set, without looking through typedefs that may have added "restrict" at a
// different level.
func (t Type) IsRestrictQualified() bool {
o := C.clang_isRestrictQualifiedType(t.c)
if o != C.uint(0) {
return true
}
return false
}
// PointeeType (for pointer types), returns the type of the pointee.
func (t Type) PointeeType() Type {
o := C.clang_getPointeeType(t.c)
return Type{o}
}
// Declaration returns the cursor for the declaration of the given type.
func (t Type) Declaration() Cursor {
o := C.clang_getTypeDeclaration(t.c)
return Cursor{o}
}
/**
* \brief Retrieve the result type associated with a function type.
*/
func (t Type) ResultType() Type {
o := C.clang_getResultType(t.c)
return Type{o}
}
/**
* \brief Return 1 if the CXType is a POD (plain old data) type, and 0
* otherwise.
*/
func (t Type) IsPOD() bool {
o := C.clang_isPODType(t.c)
if o != C.uint(0) {
return true
}
return false
}
/**
* \brief Return the element type of an array type.
*
* If a non-array type is passed in, an invalid type is returned.
*/
func (t Type) ArrayElementType() Type {
o := C.clang_getArrayElementType(t.c)
return Type{o}
}
/**
* \brief Return the the array size of a constant array.
*
* If a non-array type is passed in, -1 is returned.
*/
func (t Type) ArraySize() int64 {
o := C.clang_getArraySize(t.c)
return int64(o)
}
// EOF