-
Notifications
You must be signed in to change notification settings - Fork 546
/
stmt.go
675 lines (644 loc) · 15.3 KB
/
stmt.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
/*
* Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cl
import (
"fmt"
"go/constant"
"log"
"path/filepath"
"reflect"
goast "go/ast"
gotoken "go/token"
"go/types"
"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
"github.com/goplus/gox"
)
func relFile(dir string, file string) string {
if rel, err := filepath.Rel(dir, file); err == nil {
if rel[0] == '.' {
return rel
}
return "./" + rel
}
return file
}
func commentStmt(ctx *blockCtx, stmt ast.Stmt) {
if ctx.fileLine {
start := stmt.Pos()
pos := ctx.fset.Position(start)
if ctx.relativePath {
pos.Filename = relFile(ctx.targetDir, pos.Filename)
}
line := fmt.Sprintf("\n//line %s:%d", pos.Filename, pos.Line)
comments := &goast.CommentGroup{
List: []*goast.Comment{{Text: line}},
}
ctx.cb.SetComments(comments, false)
}
}
func compileStmts(ctx *blockCtx, body []ast.Stmt) {
for _, stmt := range body {
if v, ok := stmt.(*ast.LabeledStmt); ok {
l := v.Label
ctx.cb.NewLabel(l.Pos(), l.Name)
}
}
for _, stmt := range body {
compileStmt(ctx, stmt)
}
}
func compileStmt(ctx *blockCtx, stmt ast.Stmt) {
if enableRecover {
defer func() {
if e := recover(); e != nil {
ctx.handleRecover(e)
ctx.cb.ResetStmt()
}
}()
}
commentStmt(ctx, stmt)
switch v := stmt.(type) {
case *ast.ExprStmt:
compileExpr(ctx, v.X)
if canAutoCall(v.X) && isFunc(ctx.cb.InternalStack().Get(-1).Type) {
ctx.cb.Call(0)
}
case *ast.AssignStmt:
compileAssignStmt(ctx, v)
case *ast.ReturnStmt:
compileReturnStmt(ctx, v)
case *ast.IfStmt:
compileIfStmt(ctx, v)
case *ast.SwitchStmt:
compileSwitchStmt(ctx, v)
case *ast.RangeStmt:
compileRangeStmt(ctx, v)
case *ast.ForStmt:
compileForStmt(ctx, v)
case *ast.ForPhraseStmt:
compileForPhraseStmt(ctx, v)
case *ast.IncDecStmt:
compileIncDecStmt(ctx, v)
case *ast.DeferStmt:
compileDeferStmt(ctx, v)
case *ast.GoStmt:
compileGoStmt(ctx, v)
case *ast.DeclStmt:
compileDeclStmt(ctx, v)
case *ast.TypeSwitchStmt:
compileTypeSwitchStmt(ctx, v)
case *ast.SendStmt:
compileSendStmt(ctx, v)
case *ast.BranchStmt:
compileBranchStmt(ctx, v)
case *ast.LabeledStmt:
compileLabeledStmt(ctx, v)
case *ast.BlockStmt:
ctx.cb.Block()
compileStmts(ctx, v.List)
ctx.cb.End()
return
case *ast.SelectStmt:
compileSelectStmt(ctx, v)
case *ast.EmptyStmt:
// do nothing
default:
log.Panicln("TODO - compileStmt failed: unknown -", reflect.TypeOf(v))
}
ctx.cb.EndStmt()
}
func canAutoCall(x ast.Expr) bool {
retry:
switch t := x.(type) {
case *ast.Ident:
return true
case *ast.SelectorExpr:
x = t.X
goto retry
}
return false
}
func compileReturnStmt(ctx *blockCtx, expr *ast.ReturnStmt) {
var n = -1
var results *types.Tuple
for i, ret := range expr.Results {
if c, ok := ret.(*ast.CompositeLit); ok && c.Type == nil {
if n < 0 {
results = ctx.cb.Func().Type().(*types.Signature).Results()
n = results.Len()
}
var typ types.Type
if i < n {
typ = results.At(i).Type()
}
compileCompositeLit(ctx, c, typ, true)
} else {
twoValue := false
if len(expr.Results) == 1 {
if _, ok := ret.(*ast.ComprehensionExpr); ok {
results = ctx.cb.Func().Type().(*types.Signature).Results()
twoValue = (results.Len() == 2)
}
}
compileExpr(ctx, ret, twoValue)
}
}
ctx.cb.Return(len(expr.Results), expr)
}
func compileIncDecStmt(ctx *blockCtx, expr *ast.IncDecStmt) {
compileExprLHS(ctx, expr.X)
ctx.cb.IncDec(gotoken.Token(expr.Tok))
}
func compileSendStmt(ctx *blockCtx, expr *ast.SendStmt) {
compileExpr(ctx, expr.Chan)
compileExpr(ctx, expr.Value)
ctx.cb.Send()
}
func compileAssignStmt(ctx *blockCtx, expr *ast.AssignStmt) {
tok := expr.Tok
twoValue := (len(expr.Lhs) == 2 && len(expr.Rhs) == 1)
if tok == token.DEFINE {
names := make([]string, len(expr.Lhs))
for i, lhs := range expr.Lhs {
if v, ok := lhs.(*ast.Ident); ok {
names[i] = v.Name
} else {
log.Panicln("TODO: non-name $v on left side of :=")
}
}
ctx.cb.DefineVarStart(expr.Pos(), names...)
if enableRecover {
defer func() {
if e := recover(); e != nil {
ctx.cb.ResetInit()
panic(e)
}
}()
}
for _, rhs := range expr.Rhs {
compileExpr(ctx, rhs, twoValue)
}
ctx.cb.EndInit(len(expr.Rhs))
return
}
for _, lhs := range expr.Lhs {
compileExprLHS(ctx, lhs)
}
for _, rhs := range expr.Rhs {
compileExpr(ctx, rhs, twoValue)
}
if tok == token.ASSIGN {
ctx.cb.AssignWith(len(expr.Lhs), len(expr.Rhs), expr)
return
}
if len(expr.Lhs) != 1 || len(expr.Rhs) != 1 {
panic("TODO: invalid syntax of assign by operator")
}
ctx.cb.AssignOp(gotoken.Token(tok), expr)
}
// forRange(names...) x rangeAssignThen
// body
// end
// forRange k v x rangeAssignThen
// body
// end
func compileRangeStmt(ctx *blockCtx, v *ast.RangeStmt) {
cb := ctx.cb
comments := cb.Comments()
if v.Tok == token.DEFINE {
names := make([]string, 1, 2)
if v.Key == nil {
names[0] = "_"
} else {
names[0] = v.Key.(*ast.Ident).Name
}
if v.Value != nil {
names = append(names, v.Value.(*ast.Ident).Name)
}
cb.ForRange(names...)
compileExpr(ctx, v.X)
} else {
cb.ForRange()
n := 0
if v.Key == nil {
if v.Value != nil {
ctx.cb.VarRef(nil) // underscore
n++
}
} else {
compileExprLHS(ctx, v.Key)
n++
}
if v.Value != nil {
compileExprLHS(ctx, v.Value)
n++
}
compileExpr(ctx, v.X)
}
pos := v.TokPos
if pos == 0 {
pos = v.For
}
cb.RangeAssignThen(pos)
compileStmts(ctx, v.Body.List)
cb.SetComments(comments, true)
setBodyHandler(ctx)
cb.End()
}
func compileForPhraseStmt(ctx *blockCtx, v *ast.ForPhraseStmt) {
cb := ctx.cb
comments := cb.Comments()
names := make([]string, 1, 2)
if v.Key == nil {
names[0] = "_"
} else {
names[0] = v.Key.Name
}
if v.Value != nil {
names = append(names, v.Value.Name)
}
cb.ForRange(names...)
compileExpr(ctx, v.X)
cb.RangeAssignThen(v.TokPos)
if v.Cond != nil {
cb.If()
compileExpr(ctx, v.Cond)
cb.Then()
compileStmts(ctx, v.Body.List)
cb.SetComments(comments, true)
cb.End()
} else {
compileStmts(ctx, v.Body.List)
}
cb.SetComments(comments, true)
setBodyHandler(ctx)
cb.End()
}
// for init; cond then
// body
// post
// end
func compileForStmt(ctx *blockCtx, v *ast.ForStmt) {
cb := ctx.cb
comments := cb.Comments()
cb.For()
if v.Init != nil {
compileStmt(ctx, v.Init)
}
if v.Cond != nil {
compileExpr(ctx, v.Cond)
} else {
cb.None()
}
cb.Then()
compileStmts(ctx, v.Body.List)
if v.Post != nil {
cb.Post()
compileStmt(ctx, v.Post)
}
cb.SetComments(comments, true)
setBodyHandler(ctx)
cb.End()
}
// if init; cond then
// body
// end
func compileIfStmt(ctx *blockCtx, v *ast.IfStmt) {
cb := ctx.cb
comments := cb.Comments()
cb.If()
if v.Init != nil {
compileStmt(ctx, v.Init)
}
compileExpr(ctx, v.Cond)
cb.Then()
compileStmts(ctx, v.Body.List)
if e := v.Else; e != nil {
cb.Else()
if stmts, ok := e.(*ast.BlockStmt); ok {
compileStmts(ctx, stmts.List)
} else {
compileStmt(ctx, e)
}
}
cb.SetComments(comments, true)
cb.End()
}
// typeSwitch(name) init; expr typeAssertThen()
// type1 type2 ... typeN typeCase(N)
// ...
// end
// type1 type2 ... typeM typeCase(M)
// ...
// end
// end
func compileTypeSwitchStmt(ctx *blockCtx, v *ast.TypeSwitchStmt) {
var cb = ctx.cb
comments := cb.Comments()
var name string
var ta *ast.TypeAssertExpr
switch stmt := v.Assign.(type) {
case *ast.AssignStmt:
if stmt.Tok != token.DEFINE || len(stmt.Lhs) != 1 || len(stmt.Rhs) != 1 {
panic("TODO: type switch syntax error")
}
name = stmt.Lhs[0].(*ast.Ident).Name
ta = stmt.Rhs[0].(*ast.TypeAssertExpr)
case *ast.ExprStmt:
ta = stmt.X.(*ast.TypeAssertExpr)
}
if ta.Type != nil {
panic("TODO: type switch syntax error, please use x.(type)")
}
cb.TypeSwitch(name)
if v.Init != nil {
compileStmt(ctx, v.Init)
}
compileExpr(ctx, ta.X)
cb.TypeAssertThen()
seen := make(map[types.Type]ast.Expr)
var firstDefault ast.Stmt
for _, stmt := range v.Body.List {
c, ok := stmt.(*ast.CaseClause)
if !ok {
log.Panicln("TODO: compile TypeSwitchStmt failed - case clause expected.")
}
for _, citem := range c.List {
compileExpr(ctx, citem)
T := cb.Get(-1).Type
if tt, ok := T.(*gox.TypeType); ok {
T = tt.Type()
}
var haserr bool
for t, other := range seen {
if T == nil && t == nil || T != nil && t != nil && types.Identical(T, t) {
haserr = true
pos := ctx.Position(citem.Pos())
if T == types.Typ[types.UntypedNil] {
ctx.handleCodeErrorf(&pos, "multiple nil cases in type switch (first at %v)", ctx.Position(other.Pos()))
} else {
ctx.handleCodeErrorf(&pos, "duplicate case %s in type switch\n\tprevious case at %v", T, ctx.Position(other.Pos()))
}
}
}
if !haserr {
seen[T] = citem
}
}
if c.List == nil {
if firstDefault != nil {
pos := ctx.Position(c.Pos())
ctx.handleCodeErrorf(&pos, "multiple defaults in type switch (first at %v)", ctx.Position(firstDefault.Pos()))
} else {
firstDefault = c
}
}
cb.TypeCase(len(c.List)) // TypeCase(0) means default case
compileStmts(ctx, c.Body)
commentStmt(ctx, stmt)
cb.End()
}
cb.SetComments(comments, true)
cb.End()
}
// switch init; tag then
// expr1 expr2 ... exprN case(N)
// ...
// end
// expr1 expr2 ... exprM case(M)
// ...
// end
// end
func compileSwitchStmt(ctx *blockCtx, v *ast.SwitchStmt) {
cb := ctx.cb
comments := cb.Comments()
cb.Switch()
if v.Init != nil {
compileStmt(ctx, v.Init)
}
if v.Tag != nil { // switch tag {....}
compileExpr(ctx, v.Tag)
} else {
cb.None() // switch {...}
}
cb.Then()
seen := make(valueMap)
var firstDefault ast.Stmt
for _, stmt := range v.Body.List {
c, ok := stmt.(*ast.CaseClause)
if !ok {
log.Panicln("TODO: compile SwitchStmt failed - case clause expected.")
}
for _, citem := range c.List {
compileExpr(ctx, citem)
v := cb.Get(-1)
if val := goVal(v.CVal); val != nil {
// look for duplicate types for a given value
// (quadratic algorithm, but these lists tend to be very short)
typ := types.Default(v.Type)
var haserr bool
for _, vt := range seen[val] {
if types.Identical(typ, vt.typ) {
haserr = true
src, pos := ctx.LoadExpr(v.Src)
if _, ok := v.Src.(*ast.BasicLit); ok {
ctx.handleCodeErrorf(&pos, "duplicate case %s in switch\n\tprevious case at %v",
src, ctx.Position(vt.pos))
} else {
ctx.handleCodeErrorf(&pos, "duplicate case %s (value %#v) in switch\n\tprevious case at %v",
src, val, ctx.Position(vt.pos))
}
}
}
if !haserr {
seen[val] = append(seen[val], valueType{v.Src.Pos(), typ})
}
}
}
if c.List == nil {
if firstDefault != nil {
pos := ctx.Position(c.Pos())
ctx.handleCodeErrorf(&pos, "multiple defaults in switch (first at %v)", ctx.Position(firstDefault.Pos()))
} else {
firstDefault = c
}
}
cb.Case(len(c.List)) // Case(0) means default case
body, has := hasFallthrough(c.Body)
compileStmts(ctx, body)
if has {
cb.Fallthrough()
}
commentStmt(ctx, stmt)
cb.End()
}
cb.SetComments(comments, true)
cb.End()
}
func hasFallthrough(body []ast.Stmt) ([]ast.Stmt, bool) {
if n := len(body); n > 0 {
if bs, ok := body[n-1].(*ast.BranchStmt); ok && bs.Tok == token.FALLTHROUGH {
return body[:n-1], true
}
}
return body, false
}
// select
// stmt1 commCase(1)
// ...
// end
// stmt2 commCase(1)
// ...
// end
// ...
// commCase(0)
// ...
// end
// end
func compileSelectStmt(ctx *blockCtx, v *ast.SelectStmt) {
cb := ctx.cb
comments := cb.Comments()
cb.Select()
for _, stmt := range v.Body.List {
c, ok := stmt.(*ast.CommClause)
if !ok {
log.Panicln("TODO: compile SelectStmt failed - comm clause expected.")
}
var n int
if c.Comm != nil {
compileStmt(ctx, c.Comm)
n = 1
}
cb.CommCase(n) // CommCase(0) means default case
compileStmts(ctx, c.Body)
commentStmt(ctx, stmt)
cb.End()
}
cb.SetComments(comments, true)
cb.End()
}
func compileBranchStmt(ctx *blockCtx, v *ast.BranchStmt) {
label := v.Label
switch v.Tok {
case token.GOTO:
cb := ctx.cb
if l, ok := cb.LookupLabel(label.Name); ok {
cb.Goto(l)
return
}
compileCallExpr(ctx, &ast.CallExpr{
Fun: &ast.Ident{NamePos: v.TokPos, Name: "goto", Obj: &ast.Object{Data: label}},
Args: []ast.Expr{label},
}, clIdentGoto)
case token.BREAK:
ctx.cb.Break(getLabel(ctx, label))
case token.CONTINUE:
ctx.cb.Continue(getLabel(ctx, label))
case token.FALLTHROUGH:
pos := ctx.Position(v.Pos())
ctx.handleCodeErrorf(&pos, "fallthrough statement out of place")
default:
panic("unknown branch statement")
}
}
func getLabel(ctx *blockCtx, label *ast.Ident) *gox.Label {
if label != nil {
if l, ok := ctx.cb.LookupLabel(label.Name); ok {
return l
}
pos := ctx.Position(label.Pos())
ctx.handleCodeErrorf(&pos, "label %v is not defined", label.Name)
}
return nil
}
func compileLabeledStmt(ctx *blockCtx, v *ast.LabeledStmt) {
l, _ := ctx.cb.LookupLabel(v.Label.Name)
ctx.cb.Label(l)
compileStmt(ctx, v.Stmt)
}
func compileGoStmt(ctx *blockCtx, v *ast.GoStmt) {
compileCallExpr(ctx, v.Call, 0)
ctx.cb.Go()
}
func compileDeferStmt(ctx *blockCtx, v *ast.DeferStmt) {
compileCallExpr(ctx, v.Call, 0)
ctx.cb.Defer()
}
func compileDeclStmt(ctx *blockCtx, expr *ast.DeclStmt) {
switch d := expr.Decl.(type) {
case *ast.GenDecl:
switch d.Tok {
case token.TYPE:
for _, spec := range d.Specs {
compileType(ctx, spec.(*ast.TypeSpec))
}
case token.CONST:
cdecl := ctx.pkg.NewConstDecl(ctx.cb.Scope())
loadConstSpecs(ctx, cdecl, d.Specs)
case token.VAR:
for _, spec := range d.Specs {
v := spec.(*ast.ValueSpec)
loadVars(ctx, v, false)
}
default:
log.Panicln("TODO: compileDeclStmt - unknown")
}
}
}
func compileType(ctx *blockCtx, t *ast.TypeSpec) {
name := t.Name.Name
if t.Assign != token.NoPos { // alias type
ctx.cb.AliasType(name, toType(ctx, t.Type))
} else {
ctx.cb.NewType(name).InitType(ctx.pkg, toType(ctx, t.Type))
}
}
type (
valueMap map[interface{}][]valueType // underlying Go value -> valueType
valueType struct {
pos token.Pos
typ types.Type
}
)
// goVal returns the Go value for val, or nil.
func goVal(val constant.Value) interface{} {
// val should exist, but be conservative and check
if val == nil {
return nil
}
// Match implementation restriction of other compilers.
// gc only checks duplicates for integer, floating-point
// and string values, so only create Go values for these
// types.
switch val.Kind() {
case constant.Int:
if x, ok := constant.Int64Val(val); ok {
return x
}
if x, ok := constant.Uint64Val(val); ok {
return x
}
case constant.Float:
if x, ok := constant.Float64Val(val); ok {
return x
}
case constant.String:
return constant.StringVal(val)
}
return nil
}
// -----------------------------------------------------------------------------