forked from jackspirou/grind
/
ast.go
190 lines (177 loc) · 3.93 KB
/
ast.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package grinder
import (
"go/ast"
"go/token"
"go/types"
"github.com/jackspirou/grind/block"
)
func Unlabel(x ast.Stmt) ast.Stmt {
for {
y, ok := x.(*ast.LabeledStmt)
if !ok {
return x
}
x = y.Stmt
}
}
func IsGotoTarget(blocks *block.Graph, x ast.Stmt) bool {
for {
y, ok := x.(*ast.LabeledStmt)
if !ok {
return false
}
if len(blocks.Goto[y.Label.Name]) > 0 {
return true
}
x = y.Stmt
}
}
func IsTerminatingStmt(blocks *block.Graph, x ast.Stmt) bool {
// Like http://golang.org/ref/spec#Terminating_statements
// but added break and continue for use in non-end-of-function
// contexts.
label := ""
for {
y, ok := x.(*ast.LabeledStmt)
if !ok {
break
}
label = y.Label.Name
x = y.Stmt
}
switch x := x.(type) {
case *ast.ReturnStmt:
return true
case *ast.BranchStmt:
switch x.Tok {
case token.BREAK, token.CONTINUE, token.GOTO:
return true
}
case *ast.IfStmt:
return x.Else != nil && IsTerminatingStmt(blocks, x.Body) && IsTerminatingStmt(blocks, x.Else)
case *ast.ForStmt:
return x.Cond == nil && len(blocks.Break[label]) == 0 && !hasBreak(x.Body)
case *ast.SwitchStmt:
if len(blocks.Break[label]) > 0 || hasBreak(x.Body) {
return false
}
hasDefault := false
for _, cas := range x.Body.List {
cas := cas.(*ast.CaseClause)
if cas.List == nil {
hasDefault = true
}
if len(cas.Body) == 0 {
return false
}
last := cas.Body[len(cas.Body)-1]
if !IsTerminatingStmt(blocks, last) && !isFallthrough(last) {
return false
}
}
if !hasDefault {
return false
}
return true
case *ast.TypeSwitchStmt:
if len(blocks.Break[label]) > 0 || hasBreak(x.Body) {
return false
}
hasDefault := false
for _, cas := range x.Body.List {
cas := cas.(*ast.CaseClause)
if cas.List == nil {
hasDefault = true
}
if len(cas.Body) == 0 {
return false
}
last := cas.Body[len(cas.Body)-1]
if !IsTerminatingStmt(blocks, last) && !isFallthrough(last) {
return false
}
}
if !hasDefault {
return false
}
return true
case *ast.SelectStmt:
if len(blocks.Break[label]) > 0 || hasBreak(x.Body) {
return false
}
for _, cas := range x.Body.List {
cas := cas.(*ast.CommClause)
if len(cas.Body) == 0 {
return false
}
last := cas.Body[len(cas.Body)-1]
if !IsTerminatingStmt(blocks, last) && !isFallthrough(last) {
return false
}
}
return true
}
return false
}
func isFallthrough(x ast.Stmt) bool {
xx, ok := x.(*ast.BranchStmt)
return ok && xx.Tok == token.FALLTHROUGH
}
func hasBreak(x ast.Stmt) bool {
found := false
ast.Inspect(x, func(x ast.Node) bool {
switch x := x.(type) {
case *ast.ForStmt, *ast.RangeStmt, *ast.SelectStmt, *ast.SwitchStmt, *ast.TypeSwitchStmt:
return false
case *ast.BranchStmt:
if x.Tok == token.BREAK && x.Label == nil {
found = true
}
case ast.Expr:
return false
}
return !found
})
return found
}
func (pkg *Package) LookupAtPos(fn *ast.FuncDecl, pos token.Pos, name string) types.Object {
scope := pkg.Info.Scopes[fn.Type]
ast.Inspect(fn.Body, func(x ast.Node) bool {
if x == nil {
return false
}
if pos < x.Pos() || x.End() <= pos {
return false
}
s := pkg.Info.Scopes[x]
if s != nil {
scope = s
}
return true
})
pkgScope := pkg.Types.Scope()
for s := scope; s != nil; s = s.Parent() {
obj := s.Lookup(name)
if obj != nil && (s == pkgScope || obj.Pos() < pos) {
return obj
}
}
return nil
}
// BlockList returns the list of statements contained by the block x,
// when x is an *ast.BlockStmt, *ast.CommClause, or *ast.CaseClause.
// Otherwise BlockList returns nil.
func BlockList(x ast.Node) []ast.Stmt {
switch x := x.(type) {
case *ast.BlockStmt:
return x.List
case *ast.CommClause:
return x.Body
case *ast.CaseClause:
return x.Body
}
return nil
}