forked from gopherjs/gopherjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incdecstmt.go
42 lines (35 loc) · 849 Bytes
/
incdecstmt.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
package filter
import (
"go/ast"
"go/token"
"github.com/gopherjs/gopherjs/compiler/analysis"
"golang.org/x/tools/go/exact"
"golang.org/x/tools/go/types"
)
func IncDecStmt(stmt ast.Stmt, info *analysis.Info) ast.Stmt {
if s, ok := stmt.(*ast.IncDecStmt); ok {
t := info.Types[s.X].Type
if iExpr, isIExpr := s.X.(*ast.IndexExpr); isIExpr {
switch u := info.Types[iExpr.X].Type.Underlying().(type) {
case *types.Array:
t = u.Elem()
case *types.Slice:
t = u.Elem()
case *types.Map:
t = u.Elem()
}
}
tok := token.ADD_ASSIGN
if s.Tok == token.DEC {
tok = token.SUB_ASSIGN
}
one := &ast.BasicLit{Kind: token.INT}
info.Types[one] = types.TypeAndValue{Type: t, Value: exact.MakeInt64(1)}
return &ast.AssignStmt{
Lhs: []ast.Expr{s.X},
Tok: tok,
Rhs: []ast.Expr{one},
}
}
return stmt
}