forked from elliotchance/c2go
/
cast.go
597 lines (533 loc) · 15.3 KB
/
cast.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package types
import (
"fmt"
"go/token"
"strings"
goast "go/ast"
"strconv"
"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/util"
)
// GetArrayTypeAndSize returns the size and type of a fixed array. If the type
// is not an array with a fixed size then the the size will be -1 and the
// returned type should be ignored.
func GetArrayTypeAndSize(s string) (string, int) {
match := util.GetRegex(`([\w\* ]*)\[(\d+)\]((\[\d+\])*)`).FindStringSubmatch(s)
if len(match) > 0 {
var t = fmt.Sprintf("%s%s", match[1], match[3])
return strings.Trim(t, " "), util.Atoi(match[2])
}
return s, -1
}
// CastExpr returns an expression that casts one type to another. For
// reliability and flexability the existing type (fromType) must be structly
// provided.
//
// There are lots of rules about how an expression is cast, but here are some
// main points:
//
// 1. If fromType == toType (casting to the same type) OR toType == "void *",
// the original expression is returned unmodified.
//
// 2. There is a special type called "null" which is not defined in C, but
// rather an estimate of the NULL macro which evaluates to: (0). We cannot
// guarantee that original C used the NULL macro but it is a safe assumption
// for now.
//
// The reason why NULL is special (or at least seamingly) is that it is often
// used in different value contexts. As a number, testing pointers and
// strings. Being able to better understand the original purpose of the code
// helps to generate cleaner and more Go-like output.
//
// 3. There is a set of known primitive number types like "int", "float", etc.
// These we know can be safely cast between each other by using the data type
// as a function. For example, 3 (int) to a float would produce:
// "float32(3)".
//
// There are also some platform specific types and types that are shared in
// Go packages that are common aliases kept in this list.
//
// 4. If all else fails the fallback is to cast using a function. For example,
// Foo -> Bar, would return an expression similar to "noarch.FooToBar(expr)".
// This code would certainly fail with custom types, but that would likely be
// a bug. It is most useful to do this when dealing with compound types like
// FILE where those function probably exist (or should exist) in the noarch
// package.
func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (
_ goast.Expr, err2 error) {
defer func() {
if err2 != nil {
err2 = fmt.Errorf("Cannot casting {%s -> %s}. err = %v", cFromType, cToType, err2)
}
}()
cFromType = CleanCType(cFromType)
cToType = CleanCType(cToType)
fromType := cFromType
toType := cToType
if cFromType == cToType {
return expr, nil
}
if expr == nil {
return nil, fmt.Errorf("Expr is nil")
}
// Function casting
// Example :
// cFromType : double (int, float, double)
// cToType : double (*)(int, float, double)
if IsFunction(cFromType) {
if cToType == "void *" {
p.AddImport("github.com/elliotchance/c2go/noarch")
return util.NewCallExpr("noarch.CastInterfaceToPointer", expr), nil
}
return expr, nil
}
// Exceptions for stdout, stdin, stderr
if fromType == "FILE *" && toType == "struct _IO_FILE *" {
return expr, nil
}
if fromType == "struct _IO_FILE *" && toType == "FILE *" {
return expr, nil
}
// Exception for va_list:
// A pointer to struct __va_list_tag is always a variable called
// "c2goVaList" in go.
if fromType == "va_list" && toType == "struct __va_list_tag *" {
ret := &goast.BasicLit{Kind: token.STRING, Value: "c2goVaList"}
return ret, nil
}
// casting
if fromType == "void *" && toType[len(toType)-1] == '*' && !strings.Contains(toType, "FILE") {
toType, err := ResolveType(p, toType)
if err != nil {
return nil, err
}
return &goast.CallExpr{
Fun: &goast.ParenExpr{
X: util.NewTypeIdent(toType),
},
Args: []goast.Expr{expr},
}, nil
}
// Checking amount recursive typedef element
list := []string{fromType, toType}
for _, l := range list {
if v, ok := p.TypedefType[l]; ok {
var typedefs []string
for {
if vv, ok := p.TypedefType[v]; ok {
for i := range typedefs {
if vv == typedefs[i] {
return expr,
fmt.Errorf("recursive typedef %s in : %v", vv, typedefs)
}
}
v = vv
typedefs = append(typedefs, vv)
} else {
break
}
}
}
}
// Checking registered typedef types in program
if v, ok := p.TypedefType[toType]; ok {
if fromType == v {
toType, err := ResolveType(p, toType)
if err != nil {
return expr, err
}
return &goast.CallExpr{
Fun: &goast.Ident{
Name: toType,
},
Lparen: 1,
Args: []goast.Expr{
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
},
},
Rparen: 2,
}, nil
}
e, err := CastExpr(p, expr, fromType, v)
if err != nil {
return nil, err
}
return CastExpr(p, e, v, toType)
}
if v, ok := p.TypedefType[fromType]; ok {
t, err := ResolveType(p, v)
if err != nil {
return expr, err
}
expr = &goast.CallExpr{
Fun: &goast.Ident{
Name: t,
},
Lparen: 1,
Args: []goast.Expr{
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
},
},
Rparen: 2,
}
if toType == v {
return expr, nil
}
return CastExpr(p, expr, v, toType)
}
// C null pointer can cast to any pointer
if cFromType == NullPointer && len(cToType) > 0 {
if cToType[len(cToType)-1] == '*' {
return expr, nil
}
}
// Replace for specific case of fromType for darwin:
// Fo : union (anonymous union at sqlite3.c:619241696:3)
if strings.Contains(fromType, "anonymous union") {
// I don't understood - How to change correctly
// Try change to : `union` , but it is FAIL with that
fromType = ""
}
// convert enum to int and recursive
if strings.Contains(fromType, "enum") && !strings.Contains(toType, "enum") {
in := goast.CallExpr{
Fun: &goast.Ident{
Name: "int32",
},
Lparen: 1,
Args: []goast.Expr{
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
},
},
Rparen: 2,
}
return CastExpr(p, &in, "int", toType)
}
// convert int to enum and recursive
if !strings.Contains(fromType, "enum") && strings.Contains(toType, "enum") {
in := goast.CallExpr{
Fun: &goast.Ident{
Name: strings.TrimSpace(strings.Replace(toType, "enum", "", -1)),
},
Lparen: 1,
Args: []goast.Expr{
&goast.ParenExpr{
Lparen: 1,
X: expr,
Rparen: 2,
},
},
Rparen: 2,
}
return CastExpr(p, &in, toType, toType)
}
fromType, err := ResolveType(p, fromType)
if err != nil {
return expr, err
}
toType, err = ResolveType(p, toType)
if err != nil {
return expr, err
}
if toType == fromType {
return expr, nil
}
// Let's assume that anything can be converted to a void pointer.
if cToType == "void *" {
if strings.HasPrefix(fromType, "[]") {
cNewFromType := string(util.GetRegex(`\[(\d+)\]$`).ReplaceAllLiteral([]byte(cFromType), []byte("*")))
if cNewFromType != cFromType {
expr, err = CastExpr(p, expr, cFromType, cNewFromType)
if err != nil {
return expr, err
}
}
}
return util.NewCallExpr("unsafe.Pointer", expr), nil
}
if fromType == "null" && strings.HasPrefix(toType, "*") {
return util.NewNil(), nil
}
if fromType == "null" && toType == "float64" {
return util.NewFloatLit(0.0), nil
}
if fromType == "null" && toType == "bool" {
return util.NewIdent("false"), nil
}
// FIXME: This is a hack to avoid casting in some situations.
if fromType == "" || toType == "" {
return expr, nil
}
if fromType == "null" && toType == "[]byte" {
return util.NewNil(), nil
}
// This if for linux.
if fromType == "*_IO_FILE" && toType == "*noarch.File" {
return expr, nil
}
if strings.HasPrefix(fromType, "[]") && strings.HasPrefix(toType, "*") &&
fromType[2:] == toType[1:] {
match := util.GetRegex(`\[(\d*)\]$`).FindStringSubmatch(cFromType)
if strings.HasSuffix(cToType, "*") && len(match) > 0 {
// we need to convert from array to pointer
return &goast.UnaryExpr{
Op: token.AND,
X: &goast.IndexExpr{
X: expr,
Index: util.NewIntLit(0),
},
}, nil
}
return expr, nil
}
// Compatible integer types
types := []string{
// Integer types
"byte",
"int", "int8", "int16", "int32", "int64",
"uint8", "uint16", "uint32", "uint64",
// Floating-point types.
"float32", "float64",
// Known aliases
"__uint16_t", "size_t",
// Darwin specific
"__darwin_ct_rune_t", "darwin.CtRuneT",
}
unsigned := map[string]bool{"byte": true, "uint8": true, "uint16": true, "uint32": true, "uint64": true,
"__uint16_t": true, "size_t": true, "darwin_ct_rune_t": true, "darwin.CtRuneT": true}
var isFromNumber, isFromUnsigned, isToNumber, isToUnsigned bool
for _, v := range types {
if fromType == v {
isFromNumber = true
if b, ok := unsigned[v]; ok && b {
isFromUnsigned = true
}
}
if toType == v {
isToNumber = true
if b, ok := unsigned[v]; ok && b {
isToUnsigned = true
}
}
if fromType == v && toType == "bool" {
e := util.NewBinaryExpr(
expr,
token.NEQ,
util.NewIntLit(0),
toType,
false,
)
return e, nil
}
if fromType == "bool" && toType == v {
e := util.NewGoExpr(`func(val bool) int32 { if val { return 1 } else { return 0 } }(replaceme)`)
// Swap replaceme with the current expression
e.(*goast.CallExpr).Args = []goast.Expr{expr}
return CastExpr(p, e, "int", cToType)
}
}
if isFromNumber && isToNumber && isToUnsigned && !isFromUnsigned {
// To fix x overflows unsigned we swap cast and complement operator.
if e, ok := expr.(*goast.UnaryExpr); ok && e.Op == token.XOR {
c, err := CastExpr(p, e.X, cFromType, cToType)
e.X = c
return e, err
}
}
// In the forms of:
// - `string` -> `*byte`
// - `string` -> `char *[13]`
match1 := util.GetRegex(`\*byte`).FindStringSubmatch(toType)
match2 := util.GetRegex(`char \*\[(\d+)\]`).FindStringSubmatch(toType)
if fromType == "string" && (len(match1) > 0 || len(match2) > 0) {
// Construct a byte array from "first":
//
// var str []byte = []byte{'f','i','r','s','t'}
value := &goast.CompositeLit{
Type: &goast.ArrayType{
Elt: util.NewTypeIdent("byte"),
},
Elts: []goast.Expr{},
}
strValue, err := strconv.Unquote(expr.(*goast.BasicLit).Value)
if err != nil {
panic(fmt.Sprintf("Failed to Unquote %s\n", expr.(*goast.BasicLit).Value))
}
for _, c := range []byte(strValue) {
value.Elts = append(value.Elts, &goast.BasicLit{
Kind: token.CHAR,
Value: fmt.Sprintf("%q", c),
})
}
value.Elts = append(value.Elts, util.NewIntLit(0))
return &goast.UnaryExpr{
Op: token.AND,
X: &goast.IndexExpr{
X: value,
Index: util.NewIntLit(0),
},
}, nil
}
// In the forms of:
// - `[7]byte` -> `string`
// - `char *[12]` -> `string`
match1 = util.GetRegex(`\[(\d+)\]byte`).FindStringSubmatch(fromType)
match2 = util.GetRegex(`char \*\[(\d+)\]`).FindStringSubmatch(fromType)
if (len(match1) > 0 || len(match2) > 0) && toType == "string" {
size := 0
if len(match1) > 0 {
size = util.Atoi(match1[1])
} else {
size = util.Atoi(match2[1])
}
// The following code builds this:
//
// string(expr[:size - 1])
//
return util.NewCallExpr(
"string",
&goast.SliceExpr{
X: expr,
High: util.NewIntLit(size - 1),
},
), nil
}
// Anything that is a pointer can be compared to nil
if fromType[0] == '*' && toType == "bool" {
e := util.NewBinaryExpr(expr, token.NEQ, util.NewNil(), toType, false)
return e, nil
}
if fromType == "*byte" && toType == "bool" {
return util.NewUnaryExpr(
token.NOT, util.NewCallExpr("noarch.CStringIsNull", expr),
), nil
}
if fromType == "int" && toType == "*int" {
return util.NewNil(), nil
}
if fromType == "int" && toType == "*byte" {
return util.NewStringLit(`""`), nil
}
if fromType == "_Bool" && toType == "int" {
return expr, nil
}
if util.InStrings(fromType, types) && util.InStrings(toType, types) {
return util.NewCallExpr(toType, expr), nil
}
if strings.HasPrefix(toType, "*") && strings.HasPrefix(fromType, "*") {
return &goast.CallExpr{
Fun: &goast.ParenExpr{
X: util.NewTypeIdent(toType),
},
Args: []goast.Expr{
util.NewCallExpr("unsafe.Pointer", expr),
},
}, nil
}
p.AddImport("github.com/elliotchance/c2go/noarch")
if strings.HasPrefix(toType, "[]") && strings.HasPrefix(fromType, "*") && isArrayToPointerExpr(expr) {
expr = extractArrayFromPointer(expr)
fromType = "[]" + fromType[1:]
}
leftName := fromType
rightName := toType
if strings.Contains(leftName, ".") {
parts := strings.Split(leftName, ".")
leftName = parts[len(parts)-1]
}
if strings.Contains(rightName, ".") {
parts := strings.Split(rightName, ".")
rightName = parts[len(parts)-1]
}
if cFromType == "void *" && cToType == "char *" {
return expr, nil
}
exportedLeftName := util.GetExportedName(leftName)
exportedRightName := util.GetExportedName(rightName)
functionName := fmt.Sprintf("noarch.%sTo%s",
exportedLeftName, exportedRightName)
if strings.HasSuffix(exportedLeftName, "Slice") && strings.HasSuffix(exportedRightName, "Slice") {
p.AddMessage(fmt.Sprintf("// Warning: using unsafe slice cast to convert from %s to %s", fromType, toType))
fromSize, err := SizeOf(p, GetBaseType(cFromType))
if err != nil {
return nil, err
}
toSize, err := SizeOf(p, GetBaseType(cToType))
if err != nil {
return nil, err
}
if _, arrSize := GetArrayTypeAndSize(cFromType); arrSize > 0 {
expr = &goast.SliceExpr{X: expr}
}
return &goast.StarExpr{
X: &goast.CallExpr{
Fun: &goast.StarExpr{
X: &goast.Ident{
Name: toType,
},
},
Lparen: 1,
Args: []goast.Expr{
util.NewCallExpr("unsafe.Pointer",
util.NewCallExpr("noarch.UnsafeSliceToSlice", expr, util.NewIntLit(fromSize), util.NewIntLit(toSize))),
},
Rparen: 2,
},
}, nil
}
// FIXME: This is a hack to get SQLite3 to transpile.
if strings.Contains(functionName, "RowSetEntry") {
functionName = "FIXME111"
}
return util.NewCallExpr(functionName, expr), nil
}
func isArrayToPointerExpr(expr goast.Expr) bool {
if p1, ok := expr.(*goast.ParenExpr); ok {
if p2, ok := p1.X.(*goast.UnaryExpr); ok && p2.Op == token.AND {
if p3, ok := p2.X.(*goast.IndexExpr); ok {
if p4, ok := p3.Index.(*goast.BasicLit); ok &&
p4.Kind == token.INT &&
p4.Value == "0" {
return true
}
}
}
}
return false
}
func extractArrayFromPointer(expr goast.Expr) goast.Expr {
if p1, ok := expr.(*goast.ParenExpr); ok {
if p2, ok := p1.X.(*goast.UnaryExpr); ok && p2.Op == token.AND {
if p3, ok := p2.X.(*goast.IndexExpr); ok {
return p3.X
}
}
}
return nil
}
// IsNullExpr tries to determine if the expression is the result of the NULL
// macro. In C, NULL is actually a macro that produces an expression like "(0)".
//
// There are no guarantees if the original C code used the NULL macro, but it is
// usually a pretty good guess when we see this specific exression signature.
//
// Either way the return value from IsNullExpr should not change the
// functionality of the code but can lead to hints that allow the Go produced to
// be cleaner and more Go-like.
func IsNullExpr(n goast.Expr) bool {
if p1, ok := n.(*goast.ParenExpr); ok {
if p2, ok := p1.X.(*goast.BasicLit); ok && p2.Value == "0" {
return true
}
}
return false
}