forked from goose-lang/goose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
330 lines (297 loc) · 7.59 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package goose
import (
"fmt"
"go/ast"
"go/types"
"github.com/goose-lang/goose/glang"
)
// this file has the translations for types themselves
func (ctx Ctx) typeOf(e ast.Expr) types.Type {
return ctx.info.TypeOf(e)
}
func (ctx Ctx) getType(e ast.Expr) (typ types.Type, ok bool) {
typ = ctx.typeOf(e)
ok = typ != types.Typ[types.Invalid]
return
}
// for now only string and uint64 are supported
//
// literals structs with literals should also be fine
func supportedMapKey(keyTy types.Type) bool {
if isString(keyTy) {
return true
}
info, ok := getIntegerType(keyTy)
if ok && info.isUint64() {
return true
}
return false
}
func (ctx Ctx) selectorExprType(e *ast.SelectorExpr) glang.Expr {
if isIdent(e.X, "filesys") && isIdent(e.Sel, "File") {
return glang.TypeIdent("fileT")
}
if isIdent(e.X, "disk") && isIdent(e.Sel, "Block") {
return glang.TypeIdent("disk.blockT")
}
return ctx.glangType(e, ctx.typeOf(e))
}
func (ctx Ctx) glangTypeFromExpr(e ast.Expr) glang.Type {
return ctx.glangType(e, ctx.typeOf(e))
}
func (ctx Ctx) structType(t *types.Struct) glang.Type {
ty := glang.StructType{}
for i := range t.NumFields() {
fieldt := t.Field(i).Type()
ty.Fields = append(ty.Fields, glang.FieldDecl{
Name: t.Field(i).Name(),
Type: ctx.glangType(t.Field(i), fieldt),
})
}
return ty
}
func (ctx Ctx) glangType(n locatable, t types.Type) glang.Type {
if isProphId(t) {
return glang.TypeIdent("ProphIdT")
}
switch t := t.(type) {
case *types.Struct:
return ctx.structType(t)
case *types.TypeParam:
return glang.TypeIdent(t.Obj().Name())
case *types.Basic:
switch t.Name() {
case "uint64":
return glang.TypeIdent("uint64T")
case "uint32":
return glang.TypeIdent("uint32T")
case "byte":
return glang.TypeIdent("byteT")
case "bool":
return glang.TypeIdent("boolT")
case "string", "untyped string":
return glang.TypeIdent("stringT")
case "int":
return glang.TypeIdent("intT")
default:
ctx.unsupported(n, "basic type %s", t.Name())
}
case *types.Pointer:
return glang.PtrType{}
case *types.Named:
if t.Obj().Pkg() == nil {
if t.Obj().Name() == "error" {
return glang.TypeIdent("error")
} else {
ctx.unsupported(n, "unexpected built-in type %v", t.Obj())
}
}
if t.Obj().Pkg().Name() == "filesys" && t.Obj().Name() == "File" {
return glang.TypeIdent("fileT")
}
if t.Obj().Pkg().Name() == "disk" && t.Obj().Name() == "Disk" {
return glang.TypeIdent("disk.Disk")
}
if info, ok := ctx.getStructInfo(t); ok {
ctx.dep.addDep(info.name)
return glang.StructName(info.name)
}
ctx.dep.addDep(ctx.qualifiedName(t.Obj()))
return glang.TypeIdent(ctx.qualifiedName(t.Obj()))
case *types.Slice:
return glang.SliceType{Value: ctx.glangType(n, t.Elem())}
case *types.Map:
return glang.MapType{Key: ctx.glangType(n, t.Key()), Value: ctx.glangType(n, t.Elem())}
case *types.Signature:
return glang.FuncType{}
case *types.Interface:
return glang.TypeIdent("interfaceT")
}
// panic("unknown type")
ctx.unsupported(n, "unknown type %v", t)
return nil // unreachable
}
func sliceElem(t types.Type) types.Type {
if t, ok := t.Underlying().(*types.Slice); ok {
return t.Elem()
}
panic(fmt.Errorf("expected slice type, got %v", t))
}
func ptrElem(t types.Type) types.Type {
if t, ok := t.Underlying().(*types.Pointer); ok {
return t.Elem()
}
panic(fmt.Errorf("expected pointer type, got %v", t))
}
func (ctx Ctx) ptrType(_ *ast.StarExpr) glang.Type {
return glang.PtrType{}
}
func isEmptyInterface(e *ast.InterfaceType) bool {
return len(e.Methods.List) == 0
}
func isLockRef(t types.Type) bool {
if t, ok := t.(*types.Pointer); ok {
if t, ok := t.Elem().(*types.Named); ok {
name := t.Obj()
return name.Pkg().Name() == "sync" &&
name.Name() == "Mutex"
}
}
return false
}
func isCFMutexRef(t types.Type) bool {
if t, ok := t.(*types.Pointer); ok {
if t, ok := t.Elem().(*types.Named); ok {
name := t.Obj()
return name.Pkg().Name() == "cfmutex" &&
name.Name() == "CFMutex"
}
}
return false
}
func isCondVar(t types.Type) bool {
if t, ok := t.(*types.Pointer); ok {
if t, ok := t.Elem().(*types.Named); ok {
name := t.Obj()
return name.Pkg().Name() == "sync" &&
name.Name() == "Cond"
}
}
return false
}
func isWaitGroup(t types.Type) bool {
if t, ok := t.(*types.Pointer); ok {
if t, ok := t.Elem().(*types.Named); ok {
name := t.Obj()
return name.Pkg().Name() == "sync" &&
name.Name() == "WaitGroup"
}
}
return false
}
func isProphId(t types.Type) bool {
if t, ok := t.(*types.Pointer); ok {
if t, ok := t.Elem().(*types.Named); ok {
name := t.Obj()
return name.Pkg().Name() == "machine" &&
name.Name() == "prophId"
}
}
return false
}
func isByteSlice(t types.Type) bool {
if t, ok := t.(*types.Slice); ok {
if elTy, ok := t.Elem().Underlying().(*types.Basic); ok {
return elTy.Kind() == types.Byte
}
}
return false
}
func isString(t types.Type) bool {
if t, ok := t.(*types.Basic); ok {
return t.Kind() == types.String
}
return false
}
func isDisk(t types.Type) bool {
if t, ok := t.(*types.Named); ok {
obj := t.Obj()
if obj.Pkg().Path() == "github.com/goose-lang/goose/machine/disk" &&
obj.Name() == "Disk" {
return true
}
}
return false
}
type intTypeInfo struct {
width int
isUntyped bool
}
func (info intTypeInfo) isUint64() bool {
return info.width == 64 || info.isUntyped
}
func (info intTypeInfo) isUint32() bool {
return info.width == 32 || info.isUntyped
}
func (info intTypeInfo) isUint8() bool {
return info.width == 8 || info.isUntyped
}
func getIntegerType(t types.Type) (intTypeInfo, bool) {
basicTy, ok := t.Underlying().(*types.Basic)
if !ok {
return intTypeInfo{}, false
}
switch basicTy.Kind() {
// conversion from uint64 -> uint64 is possible if the conversion
// causes an untyped literal to become a uint64
case types.Uint, types.Int, types.Uint64:
return intTypeInfo{width: 64}, true
case types.UntypedInt:
return intTypeInfo{isUntyped: true}, true
case types.Uint32:
return intTypeInfo{width: 32}, true
case types.Uint8:
return intTypeInfo{width: 8}, true
default:
return intTypeInfo{}, false
}
}
type structTypeInfo struct {
name string
throughPointer bool
structType *types.Struct
}
func (ctx Ctx) getStructInfo(t types.Type) (structTypeInfo, bool) {
throughPointer := false
if pt, ok := t.(*types.Pointer); ok {
throughPointer = true
t = pt.Elem()
}
if t, ok := t.(*types.Named); ok {
name := ctx.qualifiedName(t.Obj())
if structType, ok := t.Underlying().(*types.Struct); ok {
return structTypeInfo{
name: name,
throughPointer: throughPointer,
structType: structType,
}, true
}
}
return structTypeInfo{}, false
}
type interfaceTypeInfo struct {
name string
interfaceType *types.Interface
}
func (ctx Ctx) getInterfaceInfo(t types.Type) (interfaceTypeInfo, bool) {
if pt, ok := t.(*types.Pointer); ok {
t = pt.Elem()
}
if t, ok := t.(*types.Named); ok {
name := ctx.qualifiedName(t.Obj())
if interfaceType, ok := t.Underlying().(*types.Interface); ok {
return interfaceTypeInfo{
name: name,
interfaceType: interfaceType,
}, true
}
}
return interfaceTypeInfo{}, false
}
func (info structTypeInfo) fields() []string {
var fields []string
for i := 0; i < info.structType.NumFields(); i++ {
fields = append(fields, info.structType.Field(i).Name())
}
return fields
}
func (ctx Ctx) typeList(n ast.Node, ts *types.TypeList) []glang.Expr {
var typeArgs []glang.Expr
if ts == nil {
return nil
}
for i := 0; i < ts.Len(); i++ {
typeArgs = append(typeArgs, ctx.glangType(n, ts.At(i)))
}
return typeArgs
}