-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathutilast_gengo.go
More file actions
245 lines (219 loc) · 6.62 KB
/
Copy pathutilast_gengo.go
File metadata and controls
245 lines (219 loc) · 6.62 KB
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
//go:build !genjs
/*
Copyright 2026 The XGo Authors (xgo.dev)
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 gogen
import (
"go/ast"
"go/token"
"go/types"
"syscall"
)
// ----------------------------------------------------------------------------
// termChecker checks whether statements are terminating.
//
// Based on go/types/return.go from the Go standard library, see
// https://github.com/golang/go/blob/927c89bbc5cc7366e86ecbb0f77267435b1d6d2c/src/go/types/return.go#L14-L17.
type termChecker struct {
panicCalls map[*ast.CallExpr]none
}
// isTerminating reports whether s is a terminating statement.
func (c *termChecker) isTerminating(s ast.Stmt, label string) bool {
switch s := s.(type) {
case *ast.BadStmt, *ast.DeclStmt, *ast.EmptyStmt, *ast.SendStmt,
*ast.IncDecStmt, *ast.AssignStmt, *ast.GoStmt, *ast.DeferStmt,
*ast.RangeStmt:
return false
case *ast.LabeledStmt:
return c.isTerminating(s.Stmt, s.Label.Name)
case *ast.ExprStmt:
return c.isPanicCall(s.X)
case *ast.ReturnStmt:
return true
case *ast.BranchStmt:
return s.Tok == token.GOTO || s.Tok == token.FALLTHROUGH
case *ast.BlockStmt:
return c.isTerminatingList(s.List, "")
case *ast.IfStmt:
return s.Else != nil &&
c.isTerminating(s.Body, "") &&
c.isTerminating(s.Else, "")
case *ast.SwitchStmt:
return c.isTerminatingSwitch(s.Body, label)
case *ast.TypeSwitchStmt:
return c.isTerminatingSwitch(s.Body, label)
case *ast.SelectStmt:
for _, cc := range s.Body.List {
body := cc.(*ast.CommClause).Body
if !c.isTerminatingList(body, "") || hasBreakList(body, label, true) {
return false
}
}
return true
case *ast.ForStmt:
return s.Cond == nil && !hasBreak(s.Body, label, true)
}
return false
}
// isTerminatingList reports whether the last non-empty statement in list is terminating.
func (c *termChecker) isTerminatingList(list []ast.Stmt, label string) bool {
// Trailing empty statements are permitted, skip them.
for i := len(list) - 1; i >= 0; i-- {
if _, ok := list[i].(*ast.EmptyStmt); !ok {
return c.isTerminating(list[i], label)
}
}
return false
}
// isTerminatingSwitch reports whether body is a terminating switch body.
func (c *termChecker) isTerminatingSwitch(body *ast.BlockStmt, label string) bool {
hasDefault := false
for _, cc := range body.List {
cc := cc.(*ast.CaseClause)
if cc.List == nil {
hasDefault = true
}
if !c.isTerminatingList(cc.Body, "") || hasBreakList(cc.Body, label, true) {
return false
}
}
return hasDefault
}
// isPanicCall reports whether x is a call to the builtin panic function.
// It checks the panicCalls map which was populated during code generation,
// ensuring that shadowed panic calls are not incorrectly identified.
func (c *termChecker) isPanicCall(x ast.Expr) bool {
call, ok := unparen(x).(*ast.CallExpr)
if !ok {
return false
}
_, ok = c.panicCalls[call]
return ok
}
// hasBreak reports whether s contains a break statement referring to the label
// or (if isTarget) an unlabeled break.
func hasBreak(s ast.Stmt, label string, isTarget bool) bool {
switch s := s.(type) {
case *ast.BranchStmt:
if s.Tok == token.BREAK {
if s.Label == nil {
return isTarget
}
return s.Label.Name == label
}
case *ast.BlockStmt:
return hasBreakList(s.List, label, isTarget)
case *ast.IfStmt:
return hasBreak(s.Body, label, isTarget) || (s.Else != nil && hasBreak(s.Else, label, isTarget))
case *ast.SwitchStmt:
if label != "" && hasBreak(s.Body, label, false) {
return true
}
case *ast.TypeSwitchStmt:
if label != "" && hasBreak(s.Body, label, false) {
return true
}
case *ast.SelectStmt:
if label != "" && hasBreak(s.Body, label, false) {
return true
}
case *ast.ForStmt:
if label != "" && hasBreak(s.Body, label, false) {
return true
}
case *ast.RangeStmt:
if label != "" && hasBreak(s.Body, label, false) {
return true
}
case *ast.LabeledStmt:
return hasBreak(s.Stmt, label, isTarget)
case *ast.CaseClause:
return hasBreakList(s.Body, label, isTarget)
case *ast.CommClause:
return hasBreakList(s.Body, label, isTarget)
}
return false
}
// hasBreakList reports whether any statement in list contains a qualifying break.
func hasBreakList(list []ast.Stmt, label string, isTarget bool) bool {
for _, s := range list {
if hasBreak(s, label, isTarget) {
return true
}
}
return false
}
// unparen returns the expression with any enclosing parentheses removed.
func unparen(x ast.Expr) ast.Expr {
for {
p, ok := x.(*ast.ParenExpr)
if !ok {
return x
}
x = p.X
}
}
// ----------------------------------------------------------------------------
type setTyper struct {
}
func (setTyper) setType(i int, name string, typ types.Type) {
// no-op
}
type fileDecls struct {
goDecls []ast.Decl
}
type (
funcDecl = ast.FuncDecl
typeDecl = ast.GenDecl
valDecl = ast.GenDecl
valueSpec = ast.ValueSpec
)
func newValueSpec(pkg *Package, decl *valDecl) (*valueSpec, setTyper) {
spec := &valueSpec{}
decl.Specs = append(decl.Specs, spec)
return spec, setTyper{}
}
// deleteValueSpec deletes an uninitialized variable.
// If the variable is initialized, it fails to delete and returns `syscall.EACCES`.
// If the variable is not found, it returns `syscall.ENOENT`.
func deleteValueSpec(decl *valDecl, name string) error {
for i, spec := range decl.Specs {
vspec := spec.(*valueSpec)
for j, ident := range vspec.Names {
if ident.Name == name {
if vspec.Values != nil { // can't remove an initialized variable
return syscall.EACCES
}
if len(vspec.Names) == 1 {
decl.Specs = append(decl.Specs[:i], decl.Specs[i+1:]...)
return nil
}
vspec.Names = append(vspec.Names[:j], vspec.Names[j+1:]...)
return nil
}
}
}
return syscall.ENOENT
}
func (p *fileDecls) appendFuncDecl(decl *funcDecl, _ *types.Signature) {
p.goDecls = append(p.goDecls, decl)
}
func (p *fileDecls) appendValDecl(decl *valDecl) {
p.goDecls = append(p.goDecls, decl)
}
func startValDeclStmtAt(cb *CodeBuilder, decl *valDecl) int {
return cb.startStmtAt(&ast.DeclStmt{Decl: decl})
}
func (p *fileDecls) appendTypeDecl(decl *typeDecl) {
p.goDecls = append(p.goDecls, decl)
}
// ----------------------------------------------------------------------------