| 1 |
package astrewrite |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"go/ast" |
| 6 |
"go/constant" |
| 7 |
"go/token" |
| 8 |
"go/types" |
| 9 |
) |
| 10 |
|
| 11 |
type simplifyContext struct { |
| 12 |
info *types.Info |
| 13 |
varCounter int |
| 14 |
simplifyCalls bool |
| 15 |
} |
| 16 |
|
| 17 |
func Simplify(file *ast.File, info *types.Info, simplifyCalls bool) *ast.File { |
| 18 |
c := &simplifyContext{info: info, simplifyCalls: simplifyCalls} |
| 19 |
|
| 20 |
decls := make([]ast.Decl, len(file.Decls)) |
| 21 |
for i, decl := range file.Decls { |
| 22 |
c.varCounter = 0 |
| 23 |
switch decl := decl.(type) { |
| 24 |
case *ast.GenDecl: |
| 25 |
decls[i] = c.simplifyGenDecl(nil, decl) |
| 26 |
|
| 27 |
case *ast.FuncDecl: |
| 28 |
decls[i] = &ast.FuncDecl{ |
| 29 |
Doc: decl.Doc, |
| 30 |
Recv: decl.Recv, |
| 31 |
Name: decl.Name, |
| 32 |
Type: decl.Type, |
| 33 |
Body: c.simplifyBlock(decl.Body), |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
newFile := &ast.File{ |
| 39 |
Doc: file.Doc, |
| 40 |
Package: file.Package, |
| 41 |
Name: file.Name, |
| 42 |
Decls: decls, |
| 43 |
Scope: file.Scope, |
| 44 |
Imports: file.Imports, |
| 45 |
Unresolved: file.Unresolved, |
| 46 |
Comments: file.Comments, |
| 47 |
} |
| 48 |
c.info.Scopes[newFile] = c.info.Scopes[file] |
| 49 |
return newFile |
| 50 |
} |
| 51 |
|
| 52 |
func (c *simplifyContext) simplifyStmtList(stmts []ast.Stmt) []ast.Stmt { |
| 53 |
var newStmts []ast.Stmt |
| 54 |
for _, s := range stmts { |
| 55 |
c.simplifyStmt(&newStmts, s) |
| 56 |
} |
| 57 |
return newStmts |
| 58 |
} |
| 59 |
|
| 60 |
func (c *simplifyContext) simplifyGenDecl(stmts *[]ast.Stmt, decl *ast.GenDecl) *ast.GenDecl { |
| 61 |
if decl.Tok != token.VAR { |
| 62 |
return decl |
| 63 |
} |
| 64 |
|
| 65 |
specs := make([]ast.Spec, len(decl.Specs)) |
| 66 |
for j, spec := range decl.Specs { |
| 67 |
switch spec := spec.(type) { |
| 68 |
case *ast.ValueSpec: |
| 69 |
var values []ast.Expr |
| 70 |
if spec.Values != nil { |
| 71 |
values = make([]ast.Expr, len(spec.Values)) |
| 72 |
for i, v := range spec.Values { |
| 73 |
v2 := c.simplifyExpr(stmts, v) |
| 74 |
for _, initializer := range c.info.InitOrder { |
| 75 |
if initializer.Rhs == v { |
| 76 |
initializer.Rhs = v2 |
| 77 |
} |
| 78 |
} |
| 79 |
values[i] = v2 |
| 80 |
} |
| 81 |
} |
| 82 |
specs[j] = &ast.ValueSpec{ |
| 83 |
Doc: spec.Doc, |
| 84 |
Names: spec.Names, |
| 85 |
Type: spec.Type, |
| 86 |
Values: values, |
| 87 |
Comment: spec.Comment, |
| 88 |
} |
| 89 |
default: |
| 90 |
specs[j] = spec |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
return &ast.GenDecl{ |
| 95 |
Doc: decl.Doc, |
| 96 |
TokPos: decl.TokPos, |
| 97 |
Tok: token.VAR, |
| 98 |
Lparen: decl.Lparen, |
| 99 |
Specs: specs, |
| 100 |
Rparen: decl.Rparen, |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func (c *simplifyContext) simplifyStmt(stmts *[]ast.Stmt, s ast.Stmt) { |
| 105 |
if s == nil { |
| 106 |
return |
| 107 |
} |
| 108 |
|
| 109 |
switch s := s.(type) { |
| 110 |
case *ast.ExprStmt: |
| 111 |
*stmts = append(*stmts, &ast.ExprStmt{ |
| 112 |
X: c.simplifyExpr2(stmts, s.X, true), |
| 113 |
}) |
| 114 |
|
| 115 |
case *ast.BlockStmt: |
| 116 |
*stmts = append(*stmts, c.simplifyBlock(s)) |
| 117 |
|
| 118 |
case *ast.LabeledStmt: |
| 119 |
c.simplifyStmt(stmts, s.Stmt) |
| 120 |
(*stmts)[len(*stmts)-1] = &ast.LabeledStmt{ |
| 121 |
Label: s.Label, |
| 122 |
Colon: s.Colon, |
| 123 |
Stmt: (*stmts)[len(*stmts)-1], |
| 124 |
} |
| 125 |
|
| 126 |
case *ast.AssignStmt: |
| 127 |
lhs := make([]ast.Expr, len(s.Lhs)) |
| 128 |
for i, x := range s.Lhs { |
| 129 |
lhs[i] = c.simplifyExpr(stmts, x) |
| 130 |
} |
| 131 |
rhs := make([]ast.Expr, len(s.Rhs)) |
| 132 |
for i, x := range s.Rhs { |
| 133 |
rhs[i] = c.simplifyExpr2(stmts, x, true) |
| 134 |
} |
| 135 |
*stmts = append(*stmts, &ast.AssignStmt{ |
| 136 |
Lhs: lhs, |
| 137 |
Tok: s.Tok, |
| 138 |
TokPos: s.TokPos, |
| 139 |
Rhs: rhs, |
| 140 |
}) |
| 141 |
|
| 142 |
case *ast.DeclStmt: |
| 143 |
*stmts = append(*stmts, &ast.DeclStmt{ |
| 144 |
Decl: c.simplifyGenDecl(stmts, s.Decl.(*ast.GenDecl)), |
| 145 |
}) |
| 146 |
|
| 147 |
case *ast.IfStmt: |
| 148 |
if s.Init != nil { |
| 149 |
block := &ast.BlockStmt{} |
| 150 |
*stmts = append(*stmts, block) |
| 151 |
stmts = &block.List |
| 152 |
c.simplifyStmt(stmts, s.Init) |
| 153 |
} |
| 154 |
newS := &ast.IfStmt{ |
| 155 |
If: s.If, |
| 156 |
Cond: c.simplifyExpr(stmts, s.Cond), |
| 157 |
Body: c.simplifyBlock(s.Body), |
| 158 |
Else: c.toElseBranch(c.simplifyToStmtList(s.Else), c.info.Scopes[s.Else]), |
| 159 |
} |
| 160 |
c.info.Scopes[newS] = c.info.Scopes[s] |
| 161 |
*stmts = append(*stmts, newS) |
| 162 |
|
| 163 |
case *ast.SwitchStmt: |
| 164 |
c.simplifySwitch(stmts, s) |
| 165 |
|
| 166 |
case *ast.TypeSwitchStmt: |
| 167 |
if s.Init != nil { |
| 168 |
block := &ast.BlockStmt{} |
| 169 |
*stmts = append(*stmts, block) |
| 170 |
stmts = &block.List |
| 171 |
c.simplifyStmt(stmts, s.Init) |
| 172 |
} |
| 173 |
var assign ast.Stmt |
| 174 |
switch a := s.Assign.(type) { |
| 175 |
case *ast.ExprStmt: |
| 176 |
ta := a.X.(*ast.TypeAssertExpr) |
| 177 |
assign = &ast.ExprStmt{ |
| 178 |
X: &ast.TypeAssertExpr{ |
| 179 |
X: c.simplifyExpr(stmts, ta.X), |
| 180 |
Lparen: ta.Lparen, |
| 181 |
Type: ta.Type, |
| 182 |
Rparen: ta.Rparen, |
| 183 |
}, |
| 184 |
} |
| 185 |
case *ast.AssignStmt: |
| 186 |
ta := a.Rhs[0].(*ast.TypeAssertExpr) |
| 187 |
assign = &ast.AssignStmt{ |
| 188 |
Lhs: a.Lhs, |
| 189 |
Tok: a.Tok, |
| 190 |
TokPos: a.TokPos, |
| 191 |
Rhs: []ast.Expr{ |
| 192 |
&ast.TypeAssertExpr{ |
| 193 |
X: c.simplifyExpr(stmts, ta.X), |
| 194 |
Lparen: ta.Lparen, |
| 195 |
Type: ta.Type, |
| 196 |
Rparen: ta.Rparen, |
| 197 |
}, |
| 198 |
}, |
| 199 |
} |
| 200 |
default: |
| 201 |
panic("unexpected type switch assign") |
| 202 |
} |
| 203 |
clauses := make([]ast.Stmt, len(s.Body.List)) |
| 204 |
for i, ccs := range s.Body.List { |
| 205 |
cc := ccs.(*ast.CaseClause) |
| 206 |
newClause := &ast.CaseClause{ |
| 207 |
Case: cc.Case, |
| 208 |
List: cc.List, |
| 209 |
Colon: cc.Colon, |
| 210 |
Body: c.simplifyStmtList(cc.Body), |
| 211 |
} |
| 212 |
if implicit, ok := c.info.Implicits[cc]; ok { |
| 213 |
c.info.Implicits[newClause] = implicit |
| 214 |
} |
| 215 |
clauses[i] = newClause |
| 216 |
} |
| 217 |
newS := &ast.TypeSwitchStmt{ |
| 218 |
Switch: s.Switch, |
| 219 |
Assign: assign, |
| 220 |
Body: &ast.BlockStmt{ |
| 221 |
List: clauses, |
| 222 |
}, |
| 223 |
} |
| 224 |
c.info.Scopes[newS] = c.info.Scopes[s] |
| 225 |
*stmts = append(*stmts, newS) |
| 226 |
|
| 227 |
case *ast.ForStmt: |
| 228 |
newS := &ast.ForStmt{ |
| 229 |
For: s.For, |
| 230 |
Init: s.Init, |
| 231 |
Cond: s.Cond, |
| 232 |
Post: s.Post, |
| 233 |
Body: c.simplifyBlock(s.Body), |
| 234 |
} |
| 235 |
c.info.Scopes[newS] = c.info.Scopes[s] |
| 236 |
*stmts = append(*stmts, newS) |
| 237 |
|
| 238 |
// case *ast.ForStmt: |
| 239 |
// c.simplifyStmt(stmts, s.Init) |
| 240 |
// var condStmts []ast.Stmt |
| 241 |
// cond := c.newVar(&condStmts, s.Cond) |
| 242 |
// bodyStmts := s.Body.List |
| 243 |
// if len(condStmts) != 0 { |
| 244 |
// bodyStmts = append(append(condStmts, &ast.IfStmt{ |
| 245 |
// Cond: &ast.UnaryExpr{ |
| 246 |
// Op: token.NOT, |
| 247 |
// X: cond, |
| 248 |
// }, |
| 249 |
// Body: &ast.BlockStmt{ |
| 250 |
// List: []ast.Stmt{&ast.BranchStmt{ |
| 251 |
// Tok: token.BREAK, |
| 252 |
// }}, |
| 253 |
// }, |
| 254 |
// }), bodyStmts...) |
| 255 |
// cond = nil |
| 256 |
// } |
| 257 |
// *stmts = append(*stmts, &ast.ForStmt{ |
| 258 |
// For: s.For, |
| 259 |
// Cond: cond, |
| 260 |
// Post: s.Post, |
| 261 |
// Body: &ast.BlockStmt{ |
| 262 |
// List: bodyStmts, |
| 263 |
// }, |
| 264 |
// }) |
| 265 |
|
| 266 |
case *ast.RangeStmt: |
| 267 |
var newS ast.Stmt |
| 268 |
switch t := c.info.TypeOf(s.X).Underlying().(type) { |
| 269 |
case *types.Chan: |
| 270 |
key := s.Key |
| 271 |
tok := s.Tok |
| 272 |
if key == nil { |
| 273 |
key = ast.NewIdent("_") |
| 274 |
tok = token.DEFINE |
| 275 |
} |
| 276 |
okVar := c.newIdent(types.Typ[types.Bool]) |
| 277 |
if s.Tok == token.ASSIGN { |
| 278 |
*stmts = append(*stmts, &ast.DeclStmt{ |
| 279 |
Decl: &ast.GenDecl{ |
| 280 |
Tok: token.VAR, |
| 281 |
Specs: []ast.Spec{&ast.ValueSpec{ |
| 282 |
Names: []*ast.Ident{okVar}, |
| 283 |
Type: ast.NewIdent("bool"), |
| 284 |
}}, |
| 285 |
}, |
| 286 |
}) |
| 287 |
} |
| 288 |
newS = &ast.ForStmt{ |
| 289 |
For: s.For, |
| 290 |
Body: &ast.BlockStmt{ |
| 291 |
Lbrace: s.Body.Lbrace, |
| 292 |
List: append([]ast.Stmt{ |
| 293 |
&ast.AssignStmt{ |
| 294 |
Lhs: []ast.Expr{key, okVar}, |
| 295 |
TokPos: s.TokPos, |
| 296 |
Tok: tok, |
| 297 |
Rhs: []ast.Expr{c.setType(&ast.UnaryExpr{ |
| 298 |
Op: token.ARROW, |
| 299 |
X: c.newVar(stmts, s.X), |
| 300 |
}, types.NewTuple( |
| 301 |
types.NewVar(token.NoPos, nil, "", t.Elem()), |
| 302 |
types.NewVar(token.NoPos, nil, "", types.Typ[types.Bool]), |
| 303 |
))}, |
| 304 |
}, |
| 305 |
&ast.IfStmt{ |
| 306 |
Cond: c.setType(&ast.UnaryExpr{ |
| 307 |
Op: token.NOT, |
| 308 |
X: okVar, |
| 309 |
}, types.Typ[types.Bool]), |
| 310 |
Body: &ast.BlockStmt{ |
| 311 |
List: []ast.Stmt{ |
| 312 |
&ast.BranchStmt{Tok: token.BREAK}, |
| 313 |
}, |
| 314 |
}, |
| 315 |
}, |
| 316 |
}, c.simplifyStmtList(s.Body.List)...), |
| 317 |
Rbrace: s.Body.Rbrace, |
| 318 |
}, |
| 319 |
} |
| 320 |
|
| 321 |
default: |
| 322 |
newS = &ast.RangeStmt{ |
| 323 |
For: s.For, |
| 324 |
Key: s.Key, |
| 325 |
Value: s.Value, |
| 326 |
TokPos: s.TokPos, |
| 327 |
Tok: s.Tok, |
| 328 |
X: s.X, |
| 329 |
Body: c.simplifyBlock(s.Body), |
| 330 |
} |
| 331 |
} |
| 332 |
c.info.Scopes[newS] = c.info.Scopes[s] |
| 333 |
*stmts = append(*stmts, newS) |
| 334 |
|
| 335 |
case *ast.IncDecStmt: |
| 336 |
*stmts = append(*stmts, &ast.IncDecStmt{ |
| 337 |
X: c.simplifyExpr(stmts, s.X), |
| 338 |
TokPos: s.TokPos, |
| 339 |
Tok: s.Tok, |
| 340 |
}) |
| 341 |
|
| 342 |
case *ast.GoStmt: |
| 343 |
*stmts = append(*stmts, &ast.GoStmt{ |
| 344 |
Go: s.Go, |
| 345 |
Call: c.simplifyCall(stmts, s.Call), |
| 346 |
}) |
| 347 |
|
| 348 |
case *ast.SelectStmt: |
| 349 |
clauses := make([]ast.Stmt, len(s.Body.List)) |
| 350 |
for i, entry := range s.Body.List { |
| 351 |
cc := entry.(*ast.CommClause) |
| 352 |
var newComm ast.Stmt |
| 353 |
var bodyPrefix []ast.Stmt |
| 354 |
switch comm := cc.Comm.(type) { |
| 355 |
case *ast.ExprStmt: |
| 356 |
recv := comm.X.(*ast.UnaryExpr) |
| 357 |
if recv.Op != token.ARROW { |
| 358 |
panic("unexpected comm clause") |
| 359 |
} |
| 360 |
newComm = &ast.ExprStmt{ |
| 361 |
X: &ast.UnaryExpr{ |
| 362 |
Op: token.ARROW, |
| 363 |
OpPos: recv.OpPos, |
| 364 |
X: c.simplifyExpr(stmts, recv.X), |
| 365 |
}, |
| 366 |
} |
| 367 |
case *ast.AssignStmt: |
| 368 |
recv := comm.Rhs[0].(*ast.UnaryExpr) |
| 369 |
if recv.Op != token.ARROW { |
| 370 |
panic("unexpected comm clause") |
| 371 |
} |
| 372 |
simplifyLhs := false |
| 373 |
for _, x := range comm.Lhs { |
| 374 |
if c.simplifyCalls && ContainsCall(x) { |
| 375 |
simplifyLhs = true |
| 376 |
} |
| 377 |
} |
| 378 |
lhs := comm.Lhs |
| 379 |
tok := comm.Tok |
| 380 |
if simplifyLhs { |
| 381 |
for i, x := range lhs { |
| 382 |
id := c.newIdent(c.info.TypeOf(x)) |
| 383 |
bodyPrefix = append(bodyPrefix, simpleAssign(c.simplifyExpr(&bodyPrefix, x), comm.Tok, id)) |
| 384 |
lhs[i] = id |
| 385 |
} |
| 386 |
tok = token.DEFINE |
| 387 |
} |
| 388 |
newComm = &ast.AssignStmt{ |
| 389 |
Lhs: lhs, |
| 390 |
Tok: tok, |
| 391 |
Rhs: []ast.Expr{c.simplifyExpr(stmts, recv)}, |
| 392 |
} |
| 393 |
case *ast.SendStmt: |
| 394 |
newComm = &ast.SendStmt{ |
| 395 |
Chan: c.simplifyExpr(stmts, comm.Chan), |
| 396 |
Arrow: comm.Arrow, |
| 397 |
Value: c.simplifyExpr(stmts, comm.Value), |
| 398 |
} |
| 399 |
case nil: |
| 400 |
newComm = nil |
| 401 |
default: |
| 402 |
panic("unexpected comm clause") |
| 403 |
} |
| 404 |
newCC := &ast.CommClause{ |
| 405 |
Case: cc.Case, |
| 406 |
Comm: newComm, |
| 407 |
Colon: cc.Colon, |
| 408 |
Body: append(bodyPrefix, c.simplifyStmtList(cc.Body)...), |
| 409 |
} |
| 410 |
c.info.Scopes[newCC] = c.info.Scopes[cc] |
| 411 |
clauses[i] = newCC |
| 412 |
} |
| 413 |
*stmts = append(*stmts, &ast.SelectStmt{ |
| 414 |
Select: s.Select, |
| 415 |
Body: &ast.BlockStmt{ |
| 416 |
List: clauses, |
| 417 |
}, |
| 418 |
}) |
| 419 |
|
| 420 |
case *ast.DeferStmt: |
| 421 |
*stmts = append(*stmts, &ast.DeferStmt{ |
| 422 |
Defer: s.Defer, |
| 423 |
Call: c.simplifyCall(stmts, s.Call), |
| 424 |
}) |
| 425 |
|
| 426 |
case *ast.SendStmt: |
| 427 |
*stmts = append(*stmts, &ast.SendStmt{ |
| 428 |
Chan: c.simplifyExpr(stmts, s.Chan), |
| 429 |
Arrow: s.Arrow, |
| 430 |
Value: c.simplifyExpr(stmts, s.Value), |
| 431 |
}) |
| 432 |
|
| 433 |
case *ast.ReturnStmt: |
| 434 |
*stmts = append(*stmts, &ast.ReturnStmt{ |
| 435 |
Return: s.Return, |
| 436 |
Results: c.simplifyExprList(stmts, s.Results), |
| 437 |
}) |
| 438 |
|
| 439 |
default: |
| 440 |
*stmts = append(*stmts, s) |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
func (c *simplifyContext) simplifyBlock(s *ast.BlockStmt) *ast.BlockStmt { |
| 445 |
if s == nil { |
| 446 |
return nil |
| 447 |
} |
| 448 |
newS := &ast.BlockStmt{ |
| 449 |
Lbrace: s.Lbrace, |
| 450 |
List: c.simplifyStmtList(s.List), |
| 451 |
Rbrace: s.Rbrace, |
| 452 |
} |
| 453 |
c.info.Scopes[newS] = c.info.Scopes[s] |
| 454 |
return newS |
| 455 |
} |
| 456 |
|
| 457 |
func (c *simplifyContext) simplifySwitch(stmts *[]ast.Stmt, s *ast.SwitchStmt) { |
| 458 |
wrapClause := &ast.CaseClause{} |
| 459 |
newS := &ast.SwitchStmt{ |
| 460 |
Switch: s.Switch, |
| 461 |
Body: &ast.BlockStmt{List: []ast.Stmt{wrapClause}}, |
| 462 |
} |
| 463 |
c.info.Scopes[newS] = c.info.Scopes[s] |
| 464 |
c.info.Scopes[wrapClause] = c.info.Scopes[s] |
| 465 |
*stmts = append(*stmts, newS) |
| 466 |
stmts = &wrapClause.Body |
| 467 |
|
| 468 |
c.simplifyStmt(stmts, s.Init) |
| 469 |
|
| 470 |
nonDefaultClauses, defaultClause := c.simplifyCaseClauses(s.Body.List) |
| 471 |
tag := c.makeTag(stmts, s.Tag, len(nonDefaultClauses) != 0) |
| 472 |
*stmts = append(*stmts, unwrapBlock(c.switchToIfElse(tag, nonDefaultClauses, defaultClause))...) |
| 473 |
} |
| 474 |
|
| 475 |
func (c *simplifyContext) makeTag(stmts *[]ast.Stmt, tag ast.Expr, needsTag bool) ast.Expr { |
| 476 |
if tag == nil { |
| 477 |
id := ast.NewIdent("true") |
| 478 |
c.info.Types[id] = types.TypeAndValue{Type: types.Typ[types.Bool], Value: constant.MakeBool(true)} |
| 479 |
return id |
| 480 |
} |
| 481 |
if !needsTag { |
| 482 |
*stmts = append(*stmts, simpleAssign(ast.NewIdent("_"), token.ASSIGN, tag)) |
| 483 |
return nil |
| 484 |
} |
| 485 |
return c.newVar(stmts, tag) |
| 486 |
} |
| 487 |
|
| 488 |
func (c *simplifyContext) simplifyCaseClauses(clauses []ast.Stmt) (nonDefaultClauses []*ast.CaseClause, defaultClause *ast.CaseClause) { |
| 489 |
var openClauses []*ast.CaseClause |
| 490 |
for _, cc := range clauses { |
| 491 |
clause := cc.(*ast.CaseClause) |
| 492 |
newClause := &ast.CaseClause{ |
| 493 |
Case: clause.Case, |
| 494 |
List: clause.List, |
| 495 |
Colon: clause.Colon, |
| 496 |
} |
| 497 |
c.info.Scopes[newClause] = c.info.Scopes[clause] |
| 498 |
|
| 499 |
body := clause.Body |
| 500 |
hasFallthrough := false |
| 501 |
if len(body) != 0 { |
| 502 |
if b, isBranchStmt := body[len(body)-1].(*ast.BranchStmt); isBranchStmt && b.Tok == token.FALLTHROUGH { |
| 503 |
body = body[:len(body)-1] |
| 504 |
hasFallthrough = true |
| 505 |
} |
| 506 |
} |
| 507 |
openClauses = append(openClauses, newClause) |
| 508 |
for _, openClause := range openClauses { |
| 509 |
openClause.Body = append(openClause.Body, body...) |
| 510 |
} |
| 511 |
if !hasFallthrough { |
| 512 |
openClauses = nil |
| 513 |
} |
| 514 |
|
| 515 |
if len(clause.List) == 0 { |
| 516 |
defaultClause = newClause |
| 517 |
continue |
| 518 |
} |
| 519 |
nonDefaultClauses = append(nonDefaultClauses, newClause) |
| 520 |
} |
| 521 |
return |
| 522 |
} |
| 523 |
|
| 524 |
func (c *simplifyContext) switchToIfElse(tag ast.Expr, nonDefaultClauses []*ast.CaseClause, defaultClause *ast.CaseClause) ast.Stmt { |
| 525 |
if len(nonDefaultClauses) == 0 { |
| 526 |
if defaultClause != nil { |
| 527 |
return c.toElseBranch(c.simplifyStmtList(defaultClause.Body), c.info.Scopes[defaultClause]) |
| 528 |
} |
| 529 |
return nil |
| 530 |
} |
| 531 |
|
| 532 |
clause := nonDefaultClauses[0] |
| 533 |
conds := make([]ast.Expr, len(clause.List)) |
| 534 |
for i, cond := range clause.List { |
| 535 |
conds[i] = c.setType(&ast.BinaryExpr{ |
| 536 |
X: tag, |
| 537 |
Op: token.EQL, |
| 538 |
Y: c.setType(&ast.ParenExpr{X: cond}, c.info.TypeOf(cond)), |
| 539 |
}, types.Typ[types.Bool]) |
| 540 |
} |
| 541 |
|
| 542 |
var stmts []ast.Stmt |
| 543 |
ifStmt := &ast.IfStmt{ |
| 544 |
If: clause.Case, |
| 545 |
Cond: c.simplifyExpr(&stmts, c.disjunction(conds)), |
| 546 |
Body: &ast.BlockStmt{List: c.simplifyStmtList(clause.Body)}, |
| 547 |
Else: c.switchToIfElse(tag, nonDefaultClauses[1:], defaultClause), |
| 548 |
} |
| 549 |
c.info.Scopes[ifStmt] = c.info.Scopes[clause] |
| 550 |
stmts = append(stmts, ifStmt) |
| 551 |
return c.toElseBranch(stmts, c.info.Scopes[clause]) |
| 552 |
} |
| 553 |
|
| 554 |
func (c *simplifyContext) disjunction(conds []ast.Expr) ast.Expr { |
| 555 |
if len(conds) == 1 { |
| 556 |
return conds[0] |
| 557 |
} |
| 558 |
return c.setType(&ast.BinaryExpr{ |
| 559 |
X: conds[0], |
| 560 |
Op: token.LOR, |
| 561 |
Y: c.disjunction(conds[1:]), |
| 562 |
}, types.Typ[types.Bool]) |
| 563 |
} |
| 564 |
|
| 565 |
func (c *simplifyContext) simplifyToStmtList(s ast.Stmt) (stmts []ast.Stmt) { |
| 566 |
c.simplifyStmt(&stmts, s) |
| 567 |
return |
| 568 |
} |
| 569 |
|
| 570 |
func (c *simplifyContext) toElseBranch(stmts []ast.Stmt, scope *types.Scope) ast.Stmt { |
| 571 |
if len(stmts) == 0 { |
| 572 |
return nil |
| 573 |
} |
| 574 |
if len(stmts) == 1 { |
| 575 |
switch stmt := stmts[0].(type) { |
| 576 |
case *ast.IfStmt, *ast.BlockStmt: |
| 577 |
c.info.Scopes[stmt] = scope |
| 578 |
return stmt |
| 579 |
} |
| 580 |
} |
| 581 |
block := &ast.BlockStmt{ |
| 582 |
List: stmts, |
| 583 |
} |
| 584 |
c.info.Scopes[block] = scope |
| 585 |
return block |
| 586 |
} |
| 587 |
|
| 588 |
func unwrapBlock(s ast.Stmt) []ast.Stmt { |
| 589 |
if s == nil { |
| 590 |
return nil |
| 591 |
} |
| 592 |
if block, ok := s.(*ast.BlockStmt); ok { |
| 593 |
return block.List |
| 594 |
} |
| 595 |
return []ast.Stmt{s} |
| 596 |
} |
| 597 |
|
| 598 |
func (c *simplifyContext) simplifyExpr(stmts *[]ast.Stmt, x ast.Expr) ast.Expr { |
| 599 |
return c.simplifyExpr2(stmts, x, false) |
| 600 |
} |
| 601 |
|
| 602 |
func (c *simplifyContext) simplifyExpr2(stmts *[]ast.Stmt, x ast.Expr, callOK bool) ast.Expr { |
| 603 |
x2 := c.simplifyExpr3(stmts, x, callOK) |
| 604 |
if t, ok := c.info.Types[x]; ok { |
| 605 |
c.info.Types[x2] = t |
| 606 |
} |
| 607 |
return x2 |
| 608 |
} |
| 609 |
|
| 610 |
func (c *simplifyContext) simplifyExpr3(stmts *[]ast.Stmt, x ast.Expr, callOK bool) ast.Expr { |
| 611 |
switch x := x.(type) { |
| 612 |
case *ast.FuncLit: |
| 613 |
return &ast.FuncLit{ |
| 614 |
Type: x.Type, |
| 615 |
Body: &ast.BlockStmt{ |
| 616 |
List: c.simplifyStmtList(x.Body.List), |
| 617 |
}, |
| 618 |
} |
| 619 |
|
| 620 |
case *ast.CompositeLit: |
| 621 |
elts := make([]ast.Expr, len(x.Elts)) |
| 622 |
for i, elt := range x.Elts { |
| 623 |
if kv, ok := elt.(*ast.KeyValueExpr); ok { |
| 624 |
elts[i] = &ast.KeyValueExpr{ |
| 625 |
Key: kv.Key, |
| 626 |
Colon: kv.Colon, |
| 627 |
Value: c.simplifyExpr(stmts, kv.Value), |
| 628 |
} |
| 629 |
continue |
| 630 |
} |
| 631 |
elts[i] = c.simplifyExpr(stmts, elt) |
| 632 |
} |
| 633 |
return &ast.CompositeLit{ |
| 634 |
Type: x.Type, |
| 635 |
Lbrace: x.Lbrace, |
| 636 |
Elts: elts, |
| 637 |
Rbrace: x.Rbrace, |
| 638 |
} |
| 639 |
|
| 640 |
case *ast.ParenExpr: |
| 641 |
return &ast.ParenExpr{ |
| 642 |
Lparen: x.Lparen, |
| 643 |
X: c.simplifyExpr(stmts, x.X), |
| 644 |
Rparen: x.Rparen, |
| 645 |
} |
| 646 |
|
| 647 |
case *ast.SelectorExpr: |
| 648 |
selExpr := &ast.SelectorExpr{ |
| 649 |
X: c.simplifyExpr(stmts, x.X), |
| 650 |
Sel: x.Sel, |
| 651 |
} |
| 652 |
if sel, ok := c.info.Selections[x]; ok { |
| 653 |
c.info.Selections[selExpr] = sel |
| 654 |
} |
| 655 |
return selExpr |
| 656 |
|
| 657 |
case *ast.IndexExpr: |
| 658 |
return &ast.IndexExpr{ |
| 659 |
X: c.simplifyExpr(stmts, x.X), |
| 660 |
Lbrack: x.Lbrack, |
| 661 |
Index: c.simplifyExpr(stmts, x.Index), |
| 662 |
Rbrack: x.Rbrack, |
| 663 |
} |
| 664 |
|
| 665 |
case *ast.SliceExpr: |
| 666 |
return &ast.SliceExpr{ |
| 667 |
X: c.simplifyExpr(stmts, x.X), |
| 668 |
Lbrack: x.Lbrack, |
| 669 |
Low: c.simplifyExpr(stmts, x.Low), |
| 670 |
High: c.simplifyExpr(stmts, x.High), |
| 671 |
Max: c.simplifyExpr(stmts, x.Max), |
| 672 |
Slice3: x.Slice3, |
| 673 |
Rbrack: x.Rbrack, |
| 674 |
} |
| 675 |
|
| 676 |
case *ast.TypeAssertExpr: |
| 677 |
return &ast.TypeAssertExpr{ |
| 678 |
X: c.simplifyExpr(stmts, x.X), |
| 679 |
Lparen: x.Lparen, |
| 680 |
Type: x.Type, |
| 681 |
Rparen: x.Rparen, |
| 682 |
} |
| 683 |
|
| 684 |
case *ast.CallExpr: |
| 685 |
call := c.simplifyCall(stmts, x) |
| 686 |
if callOK || !c.simplifyCalls { |
| 687 |
return call |
| 688 |
} |
| 689 |
return c.newVar(stmts, call) |
| 690 |
|
| 691 |
case *ast.StarExpr: |
| 692 |
return &ast.StarExpr{ |
| 693 |
Star: x.Star, |
| 694 |
X: c.simplifyExpr(stmts, x.X), |
| 695 |
} |
| 696 |
|
| 697 |
case *ast.UnaryExpr: |
| 698 |
return &ast.UnaryExpr{ |
| 699 |
OpPos: x.OpPos, |
| 700 |
Op: x.Op, |
| 701 |
X: c.simplifyExpr(stmts, x.X), |
| 702 |
} |
| 703 |
|
| 704 |
case *ast.BinaryExpr: |
| 705 |
if (x.Op == token.LAND || x.Op == token.LOR) && c.simplifyCalls && ContainsCall(x.Y) { |
| 706 |
v := c.newVar(stmts, x.X) |
| 707 |
cond := v |
| 708 |
if x.Op == token.LOR { |
| 709 |
cond = &ast.UnaryExpr{ |
| 710 |
Op: token.NOT, |
| 711 |
X: cond, |
| 712 |
} |
| 713 |
} |
| 714 |
var ifBody []ast.Stmt |
| 715 |
ifBody = append(ifBody, simpleAssign(v, token.ASSIGN, c.simplifyExpr2(&ifBody, x.Y, true))) |
| 716 |
*stmts = append(*stmts, &ast.IfStmt{ |
| 717 |
Cond: cond, |
| 718 |
Body: &ast.BlockStmt{ |
| 719 |
List: ifBody, |
| 720 |
}, |
| 721 |
}) |
| 722 |
return v |
| 723 |
} |
| 724 |
return &ast.BinaryExpr{ |
| 725 |
X: c.simplifyExpr(stmts, x.X), |
| 726 |
OpPos: x.OpPos, |
| 727 |
Op: x.Op, |
| 728 |
Y: c.simplifyExpr(stmts, x.Y), |
| 729 |
} |
| 730 |
|
| 731 |
default: |
| 732 |
return x |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
func (c *simplifyContext) simplifyCall(stmts *[]ast.Stmt, x *ast.CallExpr) *ast.CallExpr { |
| 737 |
return &ast.CallExpr{ |
| 738 |
Fun: c.simplifyExpr(stmts, x.Fun), |
| 739 |
Lparen: x.Lparen, |
| 740 |
Args: c.simplifyArgs(stmts, x.Args), |
| 741 |
Ellipsis: x.Ellipsis, |
| 742 |
Rparen: x.Rparen, |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
func (c *simplifyContext) simplifyArgs(stmts *[]ast.Stmt, args []ast.Expr) []ast.Expr { |
| 747 |
if len(args) == 1 { |
| 748 |
if tuple, ok := c.info.TypeOf(args[0]).(*types.Tuple); ok && c.simplifyCalls { |
| 749 |
call := c.simplifyExpr2(stmts, args[0], true) |
| 750 |
vars := make([]ast.Expr, tuple.Len()) |
| 751 |
for i := range vars { |
| 752 |
vars[i] = c.newIdent(tuple.At(i).Type()) |
| 753 |
} |
| 754 |
*stmts = append(*stmts, &ast.AssignStmt{ |
| 755 |
Lhs: vars, |
| 756 |
Tok: token.DEFINE, |
| 757 |
Rhs: []ast.Expr{call}, |
| 758 |
}) |
| 759 |
return vars |
| 760 |
} |
| 761 |
} |
| 762 |
return c.simplifyExprList(stmts, args) |
| 763 |
} |
| 764 |
|
| 765 |
func (c *simplifyContext) simplifyExprList(stmts *[]ast.Stmt, exprs []ast.Expr) []ast.Expr { |
| 766 |
if exprs == nil { |
| 767 |
return nil |
| 768 |
} |
| 769 |
simplifiedExprs := make([]ast.Expr, len(exprs)) |
| 770 |
for i, expr := range exprs { |
| 771 |
simplifiedExprs[i] = c.simplifyExpr(stmts, expr) |
| 772 |
} |
| 773 |
return simplifiedExprs |
| 774 |
} |
| 775 |
|
| 776 |
func (c *simplifyContext) newVar(stmts *[]ast.Stmt, x ast.Expr) ast.Expr { |
| 777 |
id := c.newIdent(c.info.TypeOf(x)) |
| 778 |
*stmts = append(*stmts, simpleAssign(id, token.DEFINE, x)) |
| 779 |
return id |
| 780 |
} |
| 781 |
|
| 782 |
func (c *simplifyContext) newIdent(t types.Type) *ast.Ident { |
| 783 |
c.varCounter++ |
| 784 |
id := ast.NewIdent(fmt.Sprintf("_%d", c.varCounter)) |
| 785 |
c.info.Types[id] = types.TypeAndValue{Type: t} // TODO remove? |
| 786 |
c.info.Uses[id] = types.NewVar(token.NoPos, nil, id.Name, t) |
| 787 |
return id |
| 788 |
} |
| 789 |
|
| 790 |
func (c *simplifyContext) setType(x ast.Expr, t types.Type) ast.Expr { |
| 791 |
c.info.Types[x] = types.TypeAndValue{Type: t} |
| 792 |
return x |
| 793 |
} |
| 794 |
|
| 795 |
func simpleAssign(lhs ast.Expr, tok token.Token, rhs ast.Expr) *ast.AssignStmt { |
| 796 |
return &ast.AssignStmt{ |
| 797 |
Lhs: []ast.Expr{lhs}, |
| 798 |
Tok: tok, |
| 799 |
Rhs: []ast.Expr{rhs}, |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
func ContainsCall(x ast.Expr) bool { |
| 804 |
switch x := x.(type) { |
| 805 |
case *ast.CallExpr: |
| 806 |
return true |
| 807 |
case *ast.CompositeLit: |
| 808 |
for _, elt := range x.Elts { |
| 809 |
if ContainsCall(elt) { |
| 810 |
return true |
| 811 |
} |
| 812 |
} |
| 813 |
return false |
| 814 |
case *ast.KeyValueExpr: |
| 815 |
return ContainsCall(x.Key) || ContainsCall(x.Value) |
| 816 |
case *ast.ParenExpr: |
| 817 |
return ContainsCall(x.X) |
| 818 |
case *ast.SelectorExpr: |
| 819 |
return ContainsCall(x.X) |
| 820 |
case *ast.IndexExpr: |
| 821 |
return ContainsCall(x.X) || ContainsCall(x.Index) |
| 822 |
case *ast.SliceExpr: |
| 823 |
return ContainsCall(x.X) || ContainsCall(x.Low) || ContainsCall(x.High) || ContainsCall(x.Max) |
| 824 |
case *ast.TypeAssertExpr: |
| 825 |
return ContainsCall(x.X) |
| 826 |
case *ast.StarExpr: |
| 827 |
return ContainsCall(x.X) |
| 828 |
case *ast.UnaryExpr: |
| 829 |
return ContainsCall(x.X) |
| 830 |
case *ast.BinaryExpr: |
| 831 |
return ContainsCall(x.X) || ContainsCall(x.Y) |
| 832 |
default: |
| 833 |
return false |
| 834 |
} |
| 835 |
} |