forked from go-clang/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
279 lines (220 loc) · 6.25 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package gen
import (
"fmt"
"strings"
"unicode"
"github.com/go-clang/bootstrap/clang"
)
// Defines all available Go types.
const (
GoByte = "byte"
GoInt8 = "int8"
GoUInt8 = "uint8"
GoInt16 = "int16"
GoUInt16 = "uint16"
GoInt32 = "int32"
GoUInt32 = "uint32"
GoInt64 = "int64"
GoUInt64 = "uint64"
GoFloat32 = "float32"
GoFloat64 = "float64"
GoBool = "bool"
GoInterface = "interface"
GoPointer = "unsafe.Pointer"
)
// Defines all available C types.
const (
CChar = "char"
CSChar = "schar"
CUChar = "uchar"
CShort = "short"
CUShort = "ushort"
CInt = "int"
CUInt = "uint"
CLongInt = "long"
CULongInt = "ulong"
CLongLong = "longlong"
CULongLong = "ulonglong"
CFloat = "float"
CDouble = "double"
)
// Type represents a generation type.
type Type struct {
// CName C Type name
CName string
// CGoName Cgo Type name
CGoName string
// GoName Go Type name
GoName string
// LengthOfSlice length of slice
LengthOfSlice string
// ArraySize size of array
ArraySize int64
// PointerLevel level of pointer
PointerLevel int
// IsPrimitive whether the this Type is primitive
IsPrimitive bool
// IsArray whether the this Type is array
IsArray bool
// IsEnumLiteral whether the this Type is enum literal
IsEnumLiteral bool
// IsFunctionPointer whether the this Type is function pointer
IsFunctionPointer bool
// IsReturnArgument whether the this Type is return argument
IsReturnArgument bool
// IsSlice whether the this Type is slice
IsSlice bool
// IsPointerComposition whether the this Type is pointer composition
IsPointerComposition bool
}
// TypeFromClangType returns the Type from Clang type.
func TypeFromClangType(cType clang.Type) (Type, error) {
typ := Type{
CName: cType.Spelling(),
PointerLevel: 0,
IsPrimitive: true,
IsArray: false,
ArraySize: -1,
IsEnumLiteral: false,
IsFunctionPointer: false,
}
switch cType.Kind() {
case clang.Type_Char_S:
typ.CGoName = CSChar
typ.GoName = GoInt8
case clang.Type_Char_U:
typ.CGoName = CUChar
typ.GoName = GoUInt8
case clang.Type_Int:
typ.CGoName = CInt
typ.GoName = GoInt32
case clang.Type_Short:
typ.CGoName = CShort
typ.GoName = GoInt16
case clang.Type_UShort:
typ.CGoName = CUShort
typ.GoName = GoUInt16
case clang.Type_UInt:
typ.CGoName = CUInt
typ.GoName = GoUInt32
case clang.Type_Long:
typ.CGoName = CLongInt
typ.GoName = GoInt64
case clang.Type_ULong:
typ.CGoName = CULongInt
typ.GoName = GoUInt64
case clang.Type_LongLong:
typ.CGoName = CLongLong
typ.GoName = GoInt64
case clang.Type_ULongLong:
typ.CGoName = CULongLong
typ.GoName = GoUInt64
case clang.Type_Float:
typ.CGoName = CFloat
typ.GoName = GoFloat32
case clang.Type_Double:
typ.CGoName = CDouble
typ.GoName = GoFloat64
case clang.Type_Bool:
typ.GoName = GoBool
case clang.Type_Void:
// TODO(go-clang): does not exist in Go, what should we do with it? https://github.com/go-clang/gen/issues/50
typ.CGoName = "void"
typ.GoName = "void"
case clang.Type_ConstantArray:
subTyp, err := TypeFromClangType(cType.ArrayElementType())
if err != nil {
return Type{}, err
}
typ.CGoName = subTyp.CGoName
typ.GoName = subTyp.GoName
typ.PointerLevel += subTyp.PointerLevel
typ.IsArray = true
typ.ArraySize = cType.ArraySize()
case clang.Type_Typedef:
typ.IsPrimitive = false
typeStr := cType.Spelling()
switch typeStr {
case "CXString": // TODO(go-clang): eliminate CXString from the generic code https://github.com/go-clang/gen/issues/25
typeStr = "cxstring"
case "time_t":
typ.CGoName = typeStr
typeStr = "time.Time"
typ.IsPrimitive = true
default:
typeStr = TrimLanguagePrefix(cType.Declaration().Type().Spelling())
}
typ.CGoName = cType.Declaration().Type().Spelling()
typ.GoName = typeStr
if cType.CanonicalType().Kind() == clang.Type_Enum {
typ.IsEnumLiteral = true
typ.IsPrimitive = true
}
case clang.Type_Pointer:
typ.PointerLevel++
if cType.PointeeType().CanonicalType().Kind() == clang.Type_FunctionProto {
typ.IsFunctionPointer = true
}
subTyp, err := TypeFromClangType(cType.PointeeType())
if err != nil {
return Type{}, err
}
typ.CGoName = subTyp.CGoName
typ.GoName = subTyp.GoName
typ.PointerLevel += subTyp.PointerLevel
typ.IsPrimitive = subTyp.IsPrimitive
case clang.Type_Record:
typ.CGoName = cType.Declaration().Type().Spelling()
typ.GoName = TrimLanguagePrefix(typ.CGoName)
typ.IsPrimitive = false
case clang.Type_FunctionProto:
typ.IsFunctionPointer = true
typ.CGoName = cType.Declaration().Type().Spelling()
typ.GoName = TrimLanguagePrefix(typ.CGoName)
case clang.Type_Enum:
typ.GoName = TrimLanguagePrefix(cType.Declaration().DisplayName())
typ.IsEnumLiteral = true
typ.IsPrimitive = true
case clang.Type_Elaborated:
return TypeFromClangType(cType.CanonicalType())
case clang.Type_Unexposed: // there is a bug in clang for enums the kind is set to unexposed dunno why, bug persisted since 2013: https://llvm.org/bugs/show_bug.cgi?id=15089
subTyp, err := TypeFromClangType(cType.CanonicalType())
if err != nil {
return Type{}, err
}
typ.CGoName = subTyp.CGoName
typ.GoName = subTyp.GoName
typ.PointerLevel += subTyp.PointerLevel
typ.IsPrimitive = subTyp.IsPrimitive
default:
return Type{}, fmt.Errorf("unhandled type %q of kind %q", cType.Spelling(), cType.Kind().Spelling())
}
return typ, nil
}
// ArrayNameFromLength returns the array name from lengthCName length naming.
func ArrayNameFromLength(lengthCName string) string {
switch {
case strings.HasPrefix(lengthCName, "num_"):
return strings.TrimPrefix(lengthCName, "num_")
case strings.HasPrefix(lengthCName, "num"):
return strings.TrimPrefix(lengthCName, "num")
case strings.HasPrefix(lengthCName, "_size"):
return strings.TrimSuffix(lengthCName, "_size")
default:
if strings.HasPrefix(lengthCName, "Num") {
pan := strings.TrimPrefix(lengthCName, "Num")
if unicode.IsUpper(rune(pan[0])) {
return pan
}
}
}
return ""
}
// IsInteger reports whether the typ is Go integer type.
func IsInteger(typ *Type) bool {
switch typ.GoName {
case GoInt8, GoUInt8, GoInt16, GoUInt16, GoInt32, GoUInt32, GoInt64, GoUInt64:
return true
}
return false
}