/
unary.go
767 lines (684 loc) · 19 KB
/
unary.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// This file contains functions for transpiling unary operator expressions.
package transpiler
import (
"fmt"
"strings"
"github.com/elliotchance/c2go/ast"
"github.com/elliotchance/c2go/program"
"github.com/elliotchance/c2go/types"
"github.com/elliotchance/c2go/util"
goast "go/ast"
"go/token"
)
func transpileUnaryOperatorInc(n *ast.UnaryOperator, p *program.Program, operator token.Token, exprIsStmt bool) (
expr goast.Expr, eType string, preStmts []goast.Stmt, postStmts []goast.Stmt, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("Cannot transpileUnaryOperatorInc. err = %v", err)
}
}()
if !(operator == token.INC || operator == token.DEC) {
err = fmt.Errorf("not acceptable operator '%v'", operator)
return
}
if types.IsPointer(p, n.Type) {
switch operator {
case token.INC:
operator = token.ADD
case token.DEC:
operator = token.SUB
}
var e ast.Node
e, err = getSoleChildIncrementable(n)
if err != nil {
return
}
var left goast.Expr
var leftType string
var newPre, newPost []goast.Stmt
left, leftType, newPre, newPost, err = transpileToExpr(e, p, false)
if err != nil {
return
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
rightType := "int"
right := &goast.BasicLit{
Kind: token.INT,
Value: "1",
}
expr, eType, newPre, newPost, err = pointerArithmetic(p, left, leftType, right, rightType, operator)
if err != nil {
return
}
if expr == nil {
return nil, "", nil, nil, fmt.Errorf("Expr is nil")
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
expr = &goast.BinaryExpr{
X: left,
Op: token.ASSIGN,
Y: expr,
}
if !exprIsStmt {
var lType string
lType, err = types.ResolveType(p, leftType)
if err != nil {
return
}
expr = util.NewAnonymousFunction([]goast.Stmt{&goast.ExprStmt{
X: expr,
}}, nil, left, lType)
}
return
}
if v, ok := n.Children()[0].(*ast.DeclRefExpr); ok {
switch n.Operator {
case "++":
return &goast.BinaryExpr{
X: util.NewIdent(v.Name),
Op: token.ADD_ASSIGN,
Y: &goast.BasicLit{Kind: token.INT, Value: "1"},
}, n.Type, nil, nil, nil
case "--":
return &goast.BinaryExpr{
X: util.NewIdent(v.Name),
Op: token.SUB_ASSIGN,
Y: &goast.BasicLit{Kind: token.INT, Value: "1"},
}, n.Type, nil, nil, nil
}
}
// Unfortunately we cannot use the Go increment operators because we are not
// providing any position information for tokens. This means that the ++/--
// would be placed before the expression and would be invalid in Go.
//
// Until it can be properly fixed (can we trick Go into to placing it after
// the expression with a magic position?) we will have to return a
// BinaryExpr with the same functionality.
binaryOperator := "+="
if operator == token.DEC {
binaryOperator = "-="
}
return transpileBinaryOperator(&ast.BinaryOperator{
Type: n.Type,
Operator: binaryOperator,
ChildNodes: []ast.Node{
n.Children()[0], &ast.IntegerLiteral{
Type: "int",
Value: "1",
ChildNodes: []ast.Node{},
},
},
}, p, exprIsStmt)
}
func getSoleChildIncrementable(n ast.Node) (result ast.Node, err error) {
children := n.Children()
if len(children) != 1 {
return nil, fmt.Errorf("expected one child node, got %d", len(children))
}
switch c := children[0].(type) {
case *ast.ParenExpr:
return getSoleChildIncrementable(c)
case *ast.DeclRefExpr, *ast.MemberExpr, *ast.UnaryOperator:
return c, nil
default:
return nil, fmt.Errorf("unsupported type %T", c)
}
}
func getSoleChildDeclRefExpr(n *ast.UnaryOperator) (result *ast.DeclRefExpr, err error) {
var inspect ast.Node = n
for {
if inspect == nil {
break
}
if ret, ok := inspect.Children()[0].(*ast.DeclRefExpr); ok {
return ret, nil
}
if len(inspect.Children()) > 1 {
return nil, fmt.Errorf("node has to many children: %T", inspect)
} else if len(inspect.Children()) == 1 {
if _, ok := inspect.Children()[0].(*ast.ParenExpr); !ok {
err = fmt.Errorf("unsupported type %T", n.Children()[0])
return
}
inspect = inspect.Children()[0]
} else {
break
}
}
return nil, fmt.Errorf("could not find supported type DeclRefExpr")
}
func transpileUnaryOperatorNot(n *ast.UnaryOperator, p *program.Program) (
goast.Expr, string, []goast.Stmt, []goast.Stmt, error) {
e, eType, preStmts, postStmts, err := transpileToExpr(n.Children()[0], p, false)
if err != nil {
return nil, "", nil, nil, err
}
// null in C is zero
if eType == types.NullPointer {
e = &goast.BasicLit{
Kind: token.INT,
Value: "0",
}
eType = "int"
}
if eType == "bool" {
return &goast.UnaryExpr{
X: e,
Op: token.NOT,
}, "bool", preStmts, postStmts, nil
}
if strings.HasSuffix(eType, "*") {
// `!pointer` has to be converted to `pointer == nil`
return &goast.BinaryExpr{
X: e,
Op: token.EQL,
Y: util.NewIdent("nil"),
}, "bool", preStmts, postStmts, nil
}
t, err := types.ResolveType(p, eType)
p.AddMessage(p.GenerateWarningMessage(err, n))
if t == "*byte" {
return util.NewUnaryExpr(
token.NOT, util.NewCallExpr("noarch.CStringIsNull", e),
), "bool", preStmts, postStmts, nil
}
// only if added "stdbool.h"
if p.IncludeHeaderIsExists("stdbool.h") {
if t == "_Bool" {
t = "int8"
e = &goast.CallExpr{
Fun: goast.NewIdent("int8"),
Lparen: 1,
Args: []goast.Expr{e},
}
}
}
p.AddImport("github.com/elliotchance/c2go/noarch")
functionName := fmt.Sprintf("noarch.Not%s",
util.GetExportedName(t))
return util.NewCallExpr(functionName, e),
eType, preStmts, postStmts, nil
}
// tranpileUnaryOperatorAmpersant - operator ampersant &
// Example of AST:
// `-ImplicitCastExpr 0x2d0fe38 <col:9, col:10> 'int *' <BitCast>
// `-UnaryOperator 0x2d0fe18 <col:9, col:10> 'int (*)[5]' prefix '&'
// `-DeclRefExpr 0x2d0fdc0 <col:10> 'int [5]' lvalue Var 0x2d0fb20 'arr' 'int [5]'
func transpileUnaryOperatorAmpersant(n *ast.UnaryOperator, p *program.Program) (
expr goast.Expr, eType string, preStmts []goast.Stmt, postStmts []goast.Stmt, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("Cannot transpileUnaryOperatorAmpersant : err = %v", err)
}
}()
expr, eType, preStmts, postStmts, err = transpileToExpr(n.Children()[0], p, false)
if err != nil {
return
}
if expr == nil {
err = fmt.Errorf("Expr is nil")
return
}
if types.IsFunction(eType) {
return
}
if types.IsLastArray(eType) {
// In : eType = 'int [5]'
// Out: eType = 'int *'
f := strings.Index(eType, "[")
e := strings.Index(eType, "]")
if e == len(eType)-1 {
eType = eType[:f] + "*"
} else {
eType = eType[:f] + "*" + eType[e+1:]
}
expr = &goast.UnaryExpr{
X: &goast.IndexExpr{
X: expr,
Index: util.NewIntLit(0),
},
Op: token.AND,
}
return
}
// In : eType = 'int'
// Out: eType = 'int *'
// FIXME: This will need to use a real slice to reference the original
// value.
_, err = types.ResolveType(p, eType)
if err != nil {
p.AddMessage(p.GenerateWarningMessage(err, n))
return
}
p.AddImport("unsafe")
expr = &goast.UnaryExpr{
X: expr,
Op: token.AND,
}
// We now have a pointer to the original type.
eType += " *"
return
}
// transpilePointerArith - transpile pointer arithmetic
// Example of using:
// *(t + 1) = ...
func transpilePointerArith(n *ast.UnaryOperator, p *program.Program) (
expr goast.Expr, eType string, preStmts []goast.Stmt, postStmts []goast.Stmt, err error) {
// pointer - expression with name of array pointer
var pointer interface{}
// locationPointer
var locPointer ast.Node
var locPosition int
// counter - count of amount of changes in AST tree
var counter int
var parents []ast.Node
var found bool
var f func(ast.Node)
f = func(n ast.Node) {
for i := range n.Children() {
switch v := n.Children()[i].(type) {
case *ast.ArraySubscriptExpr,
*ast.UnaryOperator,
*ast.DeclRefExpr:
counter++
if counter > 1 {
err = fmt.Errorf("Not acceptable : change counter is more then 1. found = %T,%T", pointer, v)
return
}
// found pointer
pointer = v
// Replace pointer to zero
var zero ast.IntegerLiteral
zero.Type = "int"
zero.Value = "0"
locPointer = n
locPosition = i
n.Children()[i] = &zero
found = true
return
case *ast.CStyleCastExpr:
if v.Type == "int" {
continue
}
counter++
if counter > 1 {
err = fmt.Errorf("Not acceptable : change counter is more then 1. found = %T,%T", pointer, v)
return
}
// found pointer
pointer = v
// Replace pointer to zero
var zero ast.IntegerLiteral
zero.Type = "int"
zero.Value = "0"
locPointer = n
locPosition = i
n.Children()[i] = &zero
found = true
return
case *ast.MemberExpr:
// check - if member of union
a := n.Children()[i]
var isUnion bool
for {
if a == nil {
break
}
if len(a.Children()) == 0 {
break
}
switch vv := a.Children()[0].(type) {
case *ast.MemberExpr, *ast.DeclRefExpr:
var typeVV string
if vvv, ok := vv.(*ast.MemberExpr); ok {
typeVV = vvv.Type
}
if vvv, ok := vv.(*ast.DeclRefExpr); ok {
typeVV = vvv.Type
}
typeVV = types.GetBaseType(typeVV)
if _, ok := p.Structs[typeVV]; ok {
isUnion = true
}
if _, ok := p.Structs["struct "+typeVV]; ok {
isUnion = true
}
if strings.HasPrefix(typeVV, "union ") || strings.HasPrefix(typeVV, "struct ") {
isUnion = true
}
if isUnion {
break
}
a = vv
continue
case *ast.ImplicitCastExpr, *ast.CStyleCastExpr:
a = vv
continue
}
break
}
if isUnion {
counter++
if counter > 1 {
err = fmt.Errorf("Not acceptable : change counter is more then 1. found = %v,%v", pointer, v)
return
}
// found pointer
pointer = v
// Replace pointer to zero
var zero ast.IntegerLiteral
zero.Type = "int"
zero.Value = "0"
locPointer = n
locPosition = i
n.Children()[i] = &zero
found = true
return
}
// member of struct
f(v)
case *ast.CallExpr:
if v.Type == "int" {
continue
}
counter++
if counter > 1 {
err = fmt.Errorf("Not acceptable : change counter is more then 1. found = %T,%T", pointer, v)
return
}
// found pointer
pointer = v
// Replace pointer to zero
var zero ast.IntegerLiteral
zero.Type = "int"
zero.Value = "0"
locPointer = n
locPosition = i
n.Children()[i] = &zero
found = true
return
default:
if found {
break
}
if len(v.Children()) > 0 {
if found {
break
}
parents = append(parents, v)
deep := true
if vv, ok := v.(*ast.ImplicitCastExpr); ok && types.IsCInteger(p, vv.Type) {
deep = false
}
if vv, ok := v.(*ast.CStyleCastExpr); ok && types.IsCInteger(p, vv.Type) {
deep = false
}
if deep {
f(v)
}
if !found {
parents = parents[:len(parents)-1]
}
}
}
}
}
f(n)
if err != nil {
return
}
if pointer == nil {
err = fmt.Errorf("pointer is nil")
return
}
defer func() {
if pointer != nil && locPointer != nil {
locPointer.Children()[locPosition] = pointer.(ast.Node)
}
}()
var typesParentBefore []string
for i := range parents {
switch v := parents[i].(type) {
case *ast.ParenExpr:
typesParentBefore = append(typesParentBefore, v.Type)
v.Type = "int"
case *ast.BinaryOperator:
typesParentBefore = append(typesParentBefore, v.Type)
v.Type = "int"
case *ast.ImplicitCastExpr:
typesParentBefore = append(typesParentBefore, v.Type)
v.Type = "int"
case *ast.CStyleCastExpr:
typesParentBefore = append(typesParentBefore, v.Type)
v.Type = "int"
case *ast.VAArgExpr:
typesParentBefore = append(typesParentBefore, v.Type)
v.Type = "int"
case *ast.MemberExpr:
typesParentBefore = append(typesParentBefore, v.Type)
v.Type = "int"
default:
panic(fmt.Errorf("Not support parent type %T in pointer searching", v))
}
}
defer func() {
for i := range parents {
switch v := parents[i].(type) {
case *ast.ParenExpr:
v.Type = typesParentBefore[i]
case *ast.BinaryOperator:
v.Type = typesParentBefore[i]
case *ast.ImplicitCastExpr:
v.Type = typesParentBefore[i]
case *ast.CStyleCastExpr:
v.Type = typesParentBefore[i]
case *ast.VAArgExpr:
v.Type = typesParentBefore[i]
case *ast.MemberExpr:
v.Type = typesParentBefore[i]
default:
panic(fmt.Errorf("Not support parent type %T in pointer searching", v))
}
}
}()
e, eType, newPre, newPost, err := transpileToExpr(n.Children()[0], p, false)
if err != nil {
return
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
eType = n.Type
switch v := pointer.(type) {
case *ast.MemberExpr:
arr, arrType, newPre, newPost, err2 := transpileToExpr(v, p, false)
if err2 != nil {
return
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
isConst, indexInt := util.EvaluateConstExpr(e)
if isConst && indexInt == 0 {
// nop
} else if isConst && indexInt < 0 {
indexInt = -indexInt
arr, _, newPre, newPost, err =
pointerArithmetic(p, arr, arrType, util.NewIntLit(int(indexInt)), "int", token.SUB)
} else {
arr, _, newPre, newPost, err =
pointerArithmetic(p, arr, arrType, e, eType, token.ADD)
}
return &goast.StarExpr{
X: arr,
}, eType, preStmts, postStmts, err
case *ast.DeclRefExpr:
var ident goast.Expr
ident = util.NewIdent(v.Name)
isConst, indexInt := util.EvaluateConstExpr(e)
if isConst && indexInt == 0 {
if strings.HasSuffix(v.Type, "]") {
return &goast.IndexExpr{
X: ident,
Index: util.NewIntLit(0),
}, eType, preStmts, postStmts, err
}
} else if isConst && indexInt < 0 {
indexInt = -indexInt
ident, _, newPre, newPost, err =
pointerArithmetic(p, ident, n.Type+" *", util.NewIntLit(int(indexInt)), "int", token.SUB)
} else {
ident, _, newPre, newPost, err =
pointerArithmetic(p, ident, n.Type+" *", e, eType, token.ADD)
}
return &goast.StarExpr{
X: ident,
}, eType, preStmts, postStmts, err
case *ast.ArraySubscriptExpr, *ast.CallExpr, *ast.CStyleCastExpr:
arr, arrType, newPre, newPost, err2 := transpileToExpr(v.(ast.Node), p, false)
if err2 != nil {
return
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
isConst, indexInt := util.EvaluateConstExpr(e)
if isConst && indexInt == 0 {
// nop
} else if isConst && indexInt < 0 {
indexInt = -indexInt
arr, _, newPre, newPost, err =
pointerArithmetic(p, arr, arrType, util.NewIntLit(int(indexInt)), "int", token.SUB)
} else {
arr, _, newPre, newPost, err =
pointerArithmetic(p, arr, arrType, e, eType, token.ADD)
}
return &goast.StarExpr{
X: arr,
}, eType, preStmts, postStmts, err
case *ast.UnaryOperator:
arr, arrType, newPre, newPost, err2 := atomicOperation(v, p)
if err2 != nil {
return
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
if memberName, ok := getMemberName(n.Children()[0]); ok {
return &goast.StarExpr{
X: &goast.SelectorExpr{
X: arr,
Sel: util.NewIdent(memberName),
},
}, eType, preStmts, postStmts, err
}
isConst, indexInt := util.EvaluateConstExpr(e)
if isConst && indexInt == 0 {
// nop
} else if isConst && indexInt < 0 {
indexInt = -indexInt
arr, _, newPre, newPost, err =
pointerArithmetic(p, arr, arrType, util.NewIntLit(int(indexInt)), "int", token.SUB)
} else {
arr, _, newPre, newPost, err =
pointerArithmetic(p, arr, arrType, e, eType, token.ADD)
}
return &goast.StarExpr{
X: arr,
}, eType, preStmts, postStmts, err
}
return nil, "", nil, nil, fmt.Errorf("Cannot found : %#v", pointer)
}
func transpileUnaryOperator(n *ast.UnaryOperator, p *program.Program, exprIsStmt bool) (
_ goast.Expr, theType string, preStmts []goast.Stmt, postStmts []goast.Stmt, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("Cannot transpile UnaryOperator: err = %v", err)
p.AddMessage(p.GenerateWarningMessage(err, n))
}
}()
operator := getTokenForOperator(n.Operator)
switch operator {
case token.MUL: // *
// Prefix "*" is not a multiplication.
// Prefix "*" used for pointer arithmetic
// Example of using:
// *(t + 1) = ...
return transpilePointerArith(n, p)
case token.INC, token.DEC: // ++, --
return transpileUnaryOperatorInc(n, p, operator, exprIsStmt)
case token.NOT: // !
return transpileUnaryOperatorNot(n, p)
case token.AND: // &
return transpileUnaryOperatorAmpersant(n, p)
}
// Otherwise handle like a unary operator.
e, eType, newPre, newPost, err := transpileToExpr(n.Children()[0], p, exprIsStmt)
if err != nil {
return nil, "", nil, nil, err
}
preStmts, postStmts = combinePreAndPostStmts(preStmts, postStmts, newPre, newPost)
return &goast.UnaryExpr{
Op: operator,
X: e,
}, eType, preStmts, postStmts, nil
}
func transpileUnaryExprOrTypeTraitExpr(n *ast.UnaryExprOrTypeTraitExpr, p *program.Program) (
*goast.BasicLit, string, []goast.Stmt, []goast.Stmt, error) {
t := n.Type2
// It will have children if the sizeof() is referencing a variable.
// Fortunately clang already has the type in the AST for us.
if len(n.Children()) > 0 {
var realFirstChild interface{}
t = ""
switch c := n.Children()[0].(type) {
case *ast.ParenExpr:
realFirstChild = c.Children()[0]
case *ast.DeclRefExpr:
t = c.Type
default:
panic(fmt.Sprintf("cannot find first child from: %#v", n.Children()[0]))
}
if t == "" {
switch ty := realFirstChild.(type) {
case *ast.DeclRefExpr:
t = ty.Type2
case *ast.ArraySubscriptExpr:
t = ty.Type
case *ast.MemberExpr:
t = ty.Type
case *ast.UnaryOperator:
t = ty.Type
case *ast.ParenExpr:
t = ty.Type
case *ast.CallExpr:
t = ty.Type
case *ast.CStyleCastExpr:
t = ty.Type
case *ast.ConditionalOperator:
t = ty.Type
case *ast.BinaryOperator:
t = ty.Type
case *ast.IntegerLiteral:
t = ty.Type
case *ast.StringLiteral:
t = ty.Type
default:
panic(fmt.Sprintf("cannot do unary on: %#v", ty))
}
}
}
sizeInBytes, err := types.SizeOf(p, t)
p.AddMessage(p.GenerateWarningMessage(err, n))
return util.NewIntLit(sizeInBytes), n.Type1, nil, nil, nil
}
func transpileStmtExpr(n *ast.StmtExpr, p *program.Program) (
*goast.CallExpr, string, []goast.Stmt, []goast.Stmt, error) {
returnType, err := types.ResolveType(p, n.Type)
if err != nil {
return nil, "", nil, nil, err
}
body, pre, post, err := transpileCompoundStmt(n.Children()[0].(*ast.CompoundStmt), p)
if err != nil {
return nil, "", pre, post, err
}
// The body of the StmtExpr is always a CompoundStmt. However, the last
// statement needs to be transformed into an explicit return statement.
lastStmt := body.List[len(body.List)-1]
body.List[len(body.List)-1] = &goast.ReturnStmt{
Results: []goast.Expr{lastStmt.(*goast.ExprStmt).X},
}
return util.NewFuncClosure(returnType, body.List...), n.Type, pre, post, nil
}