-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtypes.go
292 lines (238 loc) · 6.94 KB
/
types.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
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"fmt"
gotypes "go/types"
"math/rand"
irtypes "github.com/llir/llvm/ir/types"
)
func (t *translator) goToIRType(typ gotypes.Type) irtypes.Type {
x, ok := t.goToIRTypeCache[typ]
if ok {
return x
}
var namedStruct *irtypes.StructType
var namedSig *irtypes.StructType
if isNamedStruct(typ) {
// If the type is named, it might be recursive and need to refer to itself.
namedStruct = &irtypes.StructType{}
t.goToIRTypeCache[typ] = t.m.NewTypeDef(getTypeName(typ), namedStruct)
} else if isNamedSignature(typ) {
namedSig = &irtypes.StructType{}
t.goToIRTypeCache[typ] = t.m.NewTypeDef(getTypeName(typ), namedSig)
}
x = t.goToIRTypeImpl(typ)
if namedStruct != nil {
namedStruct.Fields = x.(*irtypes.StructType).Fields
x = namedStruct
} else if namedSig != nil {
*namedSig = *x.(*irtypes.StructType)
x = namedSig
x.SetName(getTypeName(typ))
}
t.goToIRTypeCache[typ] = x
return x
}
func isNamedStruct(typ gotypes.Type) bool {
named, ok := typ.(*gotypes.Named)
if !ok {
return false
}
_, ok = named.Underlying().(*gotypes.Struct)
return ok
}
func isNamedSignature(typ gotypes.Type) bool {
named, ok := typ.(*gotypes.Named)
if !ok {
return false
}
_, ok = named.Underlying().(*gotypes.Signature)
return ok
}
// getTypeName makes a unique type for a name. Note that the 'qualified' type
// name might not be globally unique because named types may be defined within
// an inner scope (such as a function, or if block)
func getTypeName(typ gotypes.Type) string {
// TODO(pwaller): Something better than a random int, which is a bit messy.
return fmt.Sprintf("%s-%d", typ.String(), rand.Int())
}
func (t *translator) goToIRTypeImpl(typ gotypes.Type) irtypes.Type {
typ = typ.Underlying()
switch typ := typ.(type) {
case *gotypes.Array:
irElemType := t.goToIRType(typ.Elem())
return irtypes.NewArray(uint64(typ.Len()), irElemType)
case *gotypes.Basic:
return t.goBasicToIRType(typ)
case *gotypes.Chan:
return irtypes.NewPointer(&irtypes.StructType{})
case *gotypes.Interface:
return irtypes.NewStruct(irtypes.I8Ptr, irtypes.I8Ptr)
case *gotypes.Map:
return irtypes.NewPointer(&irtypes.StructType{})
// case *gotypes.Named:
case *gotypes.Pointer:
return irtypes.NewPointer(t.goToIRType(typ.Elem()))
case *gotypes.Signature:
var irRetType irtypes.Type = irtypes.Void
goResults := typ.Results()
switch {
case goResults.Len() == 1:
// Special case single-parameter to avoid the tuple in the result.
irRetType = t.goToIRType(goResults.At(0).Type())
case goResults.Len() > 1:
irRetType = t.goToIRType(goResults)
}
var irParamTypes []irtypes.Type
irParamTypes = append(irParamTypes, irtypes.I8Ptr)
if typ.Recv() != nil {
irParamTypes = append(irParamTypes, t.goToIRType(typ.Recv().Type()))
}
for i, n := 0, typ.Params().Len(); i < n; i++ {
irParamTypes = append(irParamTypes, t.goToIRType(typ.Params().At(i).Type()))
}
irFunc := irtypes.NewFunc(irRetType, irParamTypes...)
irFuncPtr := irtypes.NewPointer(irFunc)
// { %funcType FuncPtr, i8* ClosureEnv }
return irtypes.NewStruct(irFuncPtr, irtypes.I8Ptr)
case *gotypes.Slice:
irElemType := t.goToIRType(typ.Elem())
return irtypes.NewStruct(
irtypes.NewPointer(irElemType),
irtypes.I64,
irtypes.I64,
)
case *gotypes.Struct:
var irFieldTypes []irtypes.Type
for i, n := 0, typ.NumFields(); i < n; i++ {
goFieldType := typ.Field(i).Type()
irFieldType := t.goToIRType(goFieldType)
irFieldTypes = append(irFieldTypes, irFieldType)
}
return irtypes.NewStruct(irFieldTypes...)
case *gotypes.Tuple:
var fields []irtypes.Type
for i, n := 0, typ.Len(); i < n; i++ {
fields = append(fields, t.goToIRType(typ.At(i).Type()))
}
return irtypes.NewStruct(fields...)
default:
panic(fmt.Sprintf("unimplemented type: %T: %s", typ, typ))
}
}
var basicToIR = map[gotypes.BasicKind]irtypes.Type{
gotypes.Invalid: irtypes.NewPointer(&irtypes.StructType{}),
gotypes.Bool: irtypes.I1,
gotypes.Int: irtypes.I64,
gotypes.Int8: irtypes.I8,
gotypes.Int16: irtypes.I16,
gotypes.Int32: irtypes.I32,
gotypes.Int64: irtypes.I64,
gotypes.Uint: irtypes.I64,
gotypes.Uint8: irtypes.I8,
gotypes.Uint16: irtypes.I16,
gotypes.Uint32: irtypes.I32,
gotypes.Uint64: irtypes.I64,
gotypes.Uintptr: irtypes.I64,
gotypes.Float32: irtypes.Float,
gotypes.Float64: irtypes.Double,
gotypes.Complex64: irtypes.NewStruct(irtypes.Float, irtypes.Float),
gotypes.Complex128: irtypes.NewStruct(irtypes.Double, irtypes.Double),
gotypes.String: irtypes.NewStruct(irtypes.I8Ptr, irtypes.I64),
gotypes.UnsafePointer: irtypes.I8Ptr,
}
func (t *translator) goBasicToIRType(typ *gotypes.Basic) irtypes.Type {
irTyp, ok := basicToIR[typ.Kind()]
if !ok {
panic(fmt.Sprintf("unknown kind %v: %v", typ.Kind(), typ))
}
return irTyp
}
func isString(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if !ok {
return false
}
return basic.Kind() == gotypes.String || basic.Kind() == gotypes.UntypedString
}
func isFloat(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if !ok {
return false
}
return basic.Info()&gotypes.IsFloat != 0
}
func isComplex(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if !ok {
return false
}
return basic.Info()&gotypes.IsComplex != 0
}
func isInteger(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if !ok {
return false
}
return basic.Info()&gotypes.IsInteger != 0
}
func isSigned(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if !ok {
return false
}
return basic.Info()&gotypes.IsUnsigned == 0
}
func isBool(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if !ok {
return false
}
return basic.Info()&gotypes.IsBoolean != 0
}
func isPointer(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if ok {
return basic.Kind() == gotypes.UnsafePointer
}
_, ok = typ.Underlying().(*gotypes.Pointer)
return ok
}
func isSlice(typ gotypes.Type) bool {
_, ok := typ.Underlying().(*gotypes.Slice)
return ok
}
func isInterface(typ gotypes.Type) bool {
_, ok := typ.Underlying().(*gotypes.Interface)
return ok
}
func isStruct(typ gotypes.Type) bool {
_, ok := typ.Underlying().(*gotypes.Struct)
return ok
}
func isArray(typ gotypes.Type) bool {
_, ok := typ.Underlying().(*gotypes.Array)
return ok
}
func isPtrToArray(typ gotypes.Type) bool {
return isPointer(typ) && isArray(typ.(*gotypes.Pointer).Elem())
}
func isUnsafePointer(typ gotypes.Type) bool {
basic, ok := typ.Underlying().(*gotypes.Basic)
if !ok {
return false
}
return basic.Kind() == gotypes.UnsafePointer
}
func isSignature(typ gotypes.Type) bool {
_, ok := typ.Underlying().(*gotypes.Signature)
return ok
}
func isChan(typ gotypes.Type) bool {
_, ok := typ.Underlying().(*gotypes.Chan)
return ok
}
func isMap(typ gotypes.Type) bool {
_, ok := typ.Underlying().(*gotypes.Map)
return ok
}
var sizeof = gotypes.SizesFor("gc", "amd64").Sizeof