forked from dglo/java2go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypedata.go
365 lines (320 loc) · 6.97 KB
/
typedata.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package parser
import (
"fmt"
"go/ast"
"strings"
)
type VarType int
// bool
// byte rune char
// uint8 uint16 uint32 uint64
// int8 int16 int32 int64
// uintptr
// float32 float64
// complex64 complex128
// string
const (
VT_ILLEGAL VarType = iota
VT_VOID
VT_BOOL
VT_BYTE
VT_CHAR
VT_INT16
VT_INT
VT_INT64
VT_FLOAT32
VT_FLOAT64
VT_STRING
VT_GENERIC_OBJECT
VT_ARRAY
VT_MAP
VT_INTERFACE
VT_CLASS
VT_INT_P
)
func (vt VarType) String() string {
switch vt {
case VT_VOID:
return "void"
case VT_BOOL:
return "bool"
case VT_BYTE:
return "byte"
case VT_CHAR:
return "char"
case VT_INT16:
return "int16"
case VT_INT:
return "int"
case VT_INT64:
return "int64"
case VT_FLOAT32:
return "float32"
case VT_FLOAT64:
return "float64"
case VT_STRING:
return "string"
case VT_GENERIC_OBJECT:
return "{}interface"
case VT_ARRAY:
return "??array??"
case VT_MAP:
return "??map??"
case VT_INTERFACE:
return "??interface??"
case VT_CLASS:
return "??class??"
case VT_INT_P:
return "*int"
}
return fmt.Sprintf("??VarType#%d??", vt)
}
type TypeDictionary interface {
ImportedType(string) string
IsInterface(string) bool
}
type TypeData struct {
Vtype VarType
vclass string
array_dims int
type1 *TypeData
type2 *TypeData
}
var genericObject = &TypeData{Vtype: VT_GENERIC_OBJECT}
var voidType = &TypeData{Vtype: VT_VOID}
var boolType = &TypeData{Vtype: VT_BOOL}
var byteType = &TypeData{Vtype: VT_BYTE}
var charType = &TypeData{Vtype: VT_CHAR}
var shortType = &TypeData{Vtype: VT_INT16}
var intType = &TypeData{Vtype: VT_INT}
var longType = &TypeData{Vtype: VT_INT64}
var floatType = &TypeData{Vtype: VT_FLOAT32}
var doubleType = &TypeData{Vtype: VT_FLOAT64}
var stringType = &TypeData{Vtype: VT_STRING}
var intTypeP = &TypeData{Vtype: VT_INT_P}
func NewTypeDataPrimitive(typename string, dims int) *TypeData {
var noDimType *TypeData
switch strings.ToLower(typename) {
case "void":
noDimType = voidType
case "boolean":
noDimType = boolType
case "byte":
noDimType = byteType
case "char":
noDimType = charType
case "short":
noDimType = shortType
case "int":
noDimType = intType
case "long":
noDimType = longType
case "float":
noDimType = floatType
case "double":
noDimType = doubleType
case "string":
noDimType = stringType
case "*int":
noDimType = intTypeP
default:
panic(fmt.Sprintf("Unrecognized primitive type %v", typename))
}
if dims == 0 {
return noDimType
}
return &TypeData{Vtype: VT_ARRAY, type1: noDimType, array_dims: dims}
}
func NewTypeDataObject(tdict TypeDictionary, typename string, dims int) *TypeData {
var vtype VarType
imptype := tdict.ImportedType(typename)
if imptype == "" {
imptype = typename
vtype = VT_CLASS
} else if tdict.IsInterface(imptype) {
vtype = VT_INTERFACE
} else {
vtype = VT_CLASS
}
td := &TypeData{Vtype: vtype, vclass: imptype}
if dims > 0 {
return &TypeData{Vtype: VT_ARRAY, array_dims: dims, type1: td}
}
return td
}
var identBool = ast.NewIdent("bool")
var identByte = ast.NewIdent("byte")
var identChar = ast.NewIdent("char")
var identInt16 = ast.NewIdent("int16")
var identInt = ast.NewIdent("int")
var identInt64 = ast.NewIdent("int64")
var identFloat32 = ast.NewIdent("float32")
var identFloat64 = ast.NewIdent("float64")
var identString = ast.NewIdent("string")
var identIntP = ast.NewIdent("*int")
func (vdata *TypeData) Decl() ast.Expr {
return vdata.Expr()
}
func (vdata *TypeData) Equals(odata *TypeData) bool {
if vdata == nil || odata == nil {
return false
}
if vdata.Vtype != odata.Vtype || vdata.array_dims != odata.array_dims {
return false
}
if vdata.type1 != nil || odata.type1 != nil {
if (vdata.type1 == nil && odata.type1 != nil) ||
(vdata.type1 != nil && odata.type1 == nil) {
return false
}
if !vdata.type1.Equals(odata.type1) {
return false
}
}
if vdata.type2 != nil || odata.type2 != nil {
if (vdata.type2 == nil && odata.type2 != nil) ||
(vdata.type2 != nil && odata.type2 == nil) {
return false
}
if !vdata.type2.Equals(odata.type2) {
return false
}
}
return true
}
func (vdata *TypeData) Expr() ast.Expr {
if vdata.Vtype != VT_ARRAY {
typename, is_nil := vdata.TypeName()
if is_nil {
return nil
}
expr := typename
for i := 0; i < vdata.array_dims; i++ {
expr = &ast.ArrayType{Elt: typename}
}
return expr
}
if vdata.type1 != nil {
return &ast.ArrayType{Elt: vdata.type1.Expr()}
}
return &ast.ArrayType{Elt: genericObject.Expr()}
}
func (vdata *TypeData) IsClass(name string) bool {
return vdata.Vtype == VT_CLASS && vdata.vclass == name
}
func (vdata *TypeData) isObject() bool {
return vdata.Vtype == VT_INTERFACE || vdata.Vtype == VT_CLASS
}
func (vdata *TypeData) Name() string {
switch vdata.Vtype {
case VT_ARRAY:
tstr := "array"
if vdata.type1 == nil {
tstr += "_Object"
} else {
tstr += "_"
tstr += vdata.type1.String()
}
if vdata.array_dims > 0 {
tstr += fmt.Sprintf("_dim%d", vdata.array_dims)
}
return tstr
case VT_MAP:
kstr := "map"
if vdata.type1 == nil {
kstr += "_Object"
} else {
kstr += "_"
kstr += vdata.type1.String()
}
if vdata.type2 == nil {
kstr += "_Object"
} else {
kstr += "_"
kstr += vdata.type2.String()
}
return kstr
case VT_INTERFACE:
return vdata.vclass
case VT_CLASS:
return vdata.vclass
default:
break
}
return vdata.Vtype.String()
}
func (vdata *TypeData) String() string {
switch vdata.Vtype {
case VT_ARRAY:
var tstr string
if vdata.type1 == nil {
tstr = genericObject.String()
} else {
tstr = vdata.type1.String()
}
for i := 0; i < vdata.array_dims; i++ {
tstr = "[]" + tstr
}
return tstr
case VT_MAP:
var kstr string
if vdata.type1 == nil {
kstr = genericObject.String()
} else {
kstr = vdata.type1.String()
}
var vstr string
if vdata.type2 == nil {
vstr = genericObject.String()
} else {
vstr = vdata.type2.String()
}
return "map[" + kstr + "]" + vstr
case VT_INTERFACE:
return vdata.vclass
case VT_CLASS:
return "*" + vdata.vclass
default:
break
}
return vdata.Vtype.String()
}
func (vdata *TypeData) TypeName() (ast.Expr, bool) {
switch vdata.Vtype {
case VT_VOID:
return nil, true
case VT_BOOL:
return identBool, false
case VT_BYTE:
return identByte, false
case VT_CHAR:
return identChar, false
case VT_INT16:
return identInt16, false
case VT_INT:
return identInt, false
case VT_INT64:
return identInt64, false
case VT_FLOAT32:
return identFloat32, false
case VT_FLOAT64:
return identFloat64, false
case VT_STRING:
return identString, false
case VT_GENERIC_OBJECT:
return &ast.InterfaceType{Methods: &ast.FieldList{}}, false
case VT_ARRAY:
return ast.NewIdent("<<array>>"), false
case VT_MAP:
return ast.NewIdent("<<map>>"), false
case VT_INTERFACE:
return ast.NewIdent(vdata.vclass), false
case VT_CLASS:
return &ast.StarExpr{X: ast.NewIdent(vdata.vclass)}, false
case VT_INT_P:
return identIntP, false
default:
break
}
panic(fmt.Sprintf("Unknown VarType %v", vdata.Vtype))
}