-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtype.go
216 lines (194 loc) · 5.6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package clang
// #include <stdlib.h>
// #include "go-clang.h"
import "C"
import (
"unsafe"
)
// 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)
}
// 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
}
/**
* \brief Pretty-print the underlying type using the rules of the
* language of the translation unit from which it came.
*
* If the type is invalid, an empty string is returned.
*/
func (t Type) TypeSpelling() string {
o := cxstring{C.clang_getTypeSpelling(t.c)}
defer o.Dispose()
return o.String()
}
// 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)
}
/**
* \brief Return the alignment of a type in bytes as per C++[expr.alignof]
* standard.
*
* If the type declaration is invalid, CXTypeLayoutError_Invalid is returned.
* If the type declaration is an incomplete type, CXTypeLayoutError_Incomplete
* is returned.
* If the type declaration is a dependent type, CXTypeLayoutError_Dependent is
* returned.
* If the type declaration is not a constant size type,
* CXTypeLayoutError_NotConstantSize is returned.
*/
func (t Type) AlignOf() (int, error) {
o := C.clang_Type_getAlignOf(t.c)
if o < 0 {
return int(o), TypeLayoutError(o)
}
return int(o), nil
}
/**
* \brief Return the class type of an member pointer type.
*
* If a non-member-pointer type is passed in, an invalid type is returned.
*/
func (t Type) ClassType() Type {
return Type{C.clang_Type_getClassType(t.c)}
}
/**
* \brief Return the size of a type in bytes as per C++[expr.sizeof] standard.
*
* If the type declaration is invalid, CXTypeLayoutError_Invalid is returned.
* If the type declaration is an incomplete type, CXTypeLayoutError_Incomplete
* is returned.
* If the type declaration is a dependent type, CXTypeLayoutError_Dependent is
* returned.
*/
func (t Type) SizeOf() (int, error) {
o := C.clang_Type_getSizeOf(t.c)
if o < 0 {
return int(o), TypeLayoutError(o)
}
return int(o), nil
}
/**
* \brief Return the offset of a field named S in a record of type T in bits
* as it would be returned by __offsetof__ as per C++11[18.2p4]
*
* If the cursor is not a record field declaration, CXTypeLayoutError_Invalid
* is returned.
* If the field's type declaration is an incomplete type,
* CXTypeLayoutError_Incomplete is returned.
* If the field's type declaration is a dependent type,
* CXTypeLayoutError_Dependent is returned.
* If the field's name S is not found,
* CXTypeLayoutError_InvalidFieldName is returned.
*/
func (t Type) OffsetOf(s string) (int, error) {
c_str := C.CString(s)
defer C.free(unsafe.Pointer(c_str))
o := C.clang_Type_getOffsetOf(t.c, c_str)
if o < 0 {
return int(o), TypeLayoutError(o)
}
return int(o), nil
}
/**
* \brief Retrieve the ref-qualifier kind of a function or method.
*
* The ref-qualifier is returned for C++ functions or methods. For other types
* or non-C++ declarations, CXRefQualifier_None is returned.
*/
func (t Type) CXXRefQualifier() RefQualifierKind {
return RefQualifierKind(C.clang_Type_getCXXRefQualifier(t.c))
}
// EOF