From 18ebfb49e9114b98e5a66acae073f5514e383aba Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 23 Dec 2020 02:00:39 -0800 Subject: [dev.regabi] cmd/compile: cleanup noder Similar to previous CL: take advantage of better constructor APIs for translating ASTs from syntax to ir. Passes toolstash -cmp. Change-Id: I40970775e7dd5afe2a0b7593ce3bd73237562457 Reviewed-on: https://go-review.googlesource.com/c/go/+/279972 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/noder/noder.go | 96 ++++++++++++--------------------- 1 file changed, 33 insertions(+), 63 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index a684673c8f..c73e2d7fc5 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -377,11 +377,7 @@ func (p *noder) importDecl(imp *syntax.ImportDecl) { func (p *noder) varDecl(decl *syntax.VarDecl) []ir.Node { names := p.declNames(ir.ONAME, decl.NameList) typ := p.typeExprOrNil(decl.Type) - - var exprs []ir.Node - if decl.Values != nil { - exprs = p.exprList(decl.Values) - } + exprs := p.exprList(decl.Values) if pragma, ok := decl.Pragma.(*pragmas); ok { if len(pragma.Embeds) > 0 { @@ -620,10 +616,14 @@ func (p *noder) param(param *syntax.Field, dddOk, final bool) *ir.Field { } func (p *noder) exprList(expr syntax.Expr) []ir.Node { - if list, ok := expr.(*syntax.ListExpr); ok { - return p.exprs(list.ElemList) + switch expr := expr.(type) { + case nil: + return nil + case *syntax.ListExpr: + return p.exprs(expr.ElemList) + default: + return []ir.Node{p.expr(expr)} } - return []ir.Node{p.expr(expr)} } func (p *noder) exprs(exprs []syntax.Expr) []ir.Node { @@ -642,17 +642,14 @@ func (p *noder) expr(expr syntax.Expr) ir.Node { case *syntax.Name: return p.mkname(expr) case *syntax.BasicLit: - n := ir.NewLiteral(p.basicLit(expr)) + n := ir.NewBasicLit(p.pos(expr), p.basicLit(expr)) if expr.Kind == syntax.RuneLit { n.SetType(types.UntypedRune) } n.SetDiag(expr.Bad) // avoid follow-on errors if there was a syntax error return n case *syntax.CompositeLit: - n := ir.NewCompLitExpr(p.pos(expr), ir.OCOMPLIT, nil, nil) - if expr.Type != nil { - n.Ntype = ir.Node(p.expr(expr.Type)).(ir.Ntype) - } + n := ir.NewCompLitExpr(p.pos(expr), ir.OCOMPLIT, p.typeExpr(expr.Type), nil) l := p.exprs(expr.ElemList) for i, e := range l { l[i] = p.wrapname(expr.ElemList[i], e) @@ -695,7 +692,7 @@ func (p *noder) expr(expr syntax.Expr) ir.Node { n.SetSliceBounds(index[0], index[1], index[2]) return n case *syntax.AssertExpr: - return ir.NewTypeAssertExpr(p.pos(expr), p.expr(expr.X), p.typeExpr(expr.Type).(ir.Ntype)) + return ir.NewTypeAssertExpr(p.pos(expr), p.expr(expr.X), p.typeExpr(expr.Type)) case *syntax.Operation: if expr.Op == syntax.Add && expr.Y != nil { return p.sum(expr) @@ -719,8 +716,7 @@ func (p *noder) expr(expr syntax.Expr) ir.Node { } return ir.NewBinaryExpr(pos, op, x, y) case *syntax.CallExpr: - n := ir.NewCallExpr(p.pos(expr), ir.OCALL, p.expr(expr.Fun), nil) - n.Args.Set(p.exprs(expr.ArgList)) + n := ir.NewCallExpr(p.pos(expr), ir.OCALL, p.expr(expr.Fun), p.exprs(expr.ArgList)) n.IsDDD = expr.HasDots return n @@ -987,7 +983,7 @@ func (p *noder) stmt(stmt syntax.Stmt) ir.Node { func (p *noder) stmtFall(stmt syntax.Stmt, fallOK bool) ir.Node { p.setlineno(stmt) switch stmt := stmt.(type) { - case *syntax.EmptyStmt: + case nil, *syntax.EmptyStmt: return nil case *syntax.LabeledStmt: return p.labeledStmt(stmt, fallOK) @@ -1060,12 +1056,7 @@ func (p *noder) stmtFall(stmt syntax.Stmt, fallOK bool) ir.Node { } return ir.NewGoDeferStmt(p.pos(stmt), op, p.expr(stmt.Call)) case *syntax.ReturnStmt: - var results []ir.Node - if stmt.Results != nil { - results = p.exprList(stmt.Results) - } - n := ir.NewReturnStmt(p.pos(stmt), nil) - n.Results.Set(results) + n := ir.NewReturnStmt(p.pos(stmt), p.exprList(stmt.Results)) if len(n.Results) == 0 && ir.CurFunc != nil { for _, ln := range ir.CurFunc.Dcl { if ln.Class_ == ir.PPARAM { @@ -1159,14 +1150,9 @@ func (p *noder) blockStmt(stmt *syntax.BlockStmt) []ir.Node { func (p *noder) ifStmt(stmt *syntax.IfStmt) ir.Node { p.openScope(stmt.Pos()) - n := ir.NewIfStmt(p.pos(stmt), nil, nil, nil) - if stmt.Init != nil { - *n.PtrInit() = []ir.Node{p.stmt(stmt.Init)} - } - if stmt.Cond != nil { - n.Cond = p.expr(stmt.Cond) - } - n.Body.Set(p.blockStmt(stmt.Then)) + init := p.simpleStmt(stmt.Init) + n := ir.NewIfStmt(p.pos(stmt), p.expr(stmt.Cond), p.blockStmt(stmt.Then), nil) + *n.PtrInit() = init if stmt.Else != nil { e := p.stmt(stmt.Else) if e.Op() == ir.OBLOCK { @@ -1197,30 +1183,17 @@ func (p *noder) forStmt(stmt *syntax.ForStmt) ir.Node { return n } - n := ir.NewForStmt(p.pos(stmt), nil, nil, nil, nil) - if stmt.Init != nil { - *n.PtrInit() = []ir.Node{p.stmt(stmt.Init)} - } - if stmt.Cond != nil { - n.Cond = p.expr(stmt.Cond) - } - if stmt.Post != nil { - n.Post = p.stmt(stmt.Post) - } - n.Body.Set(p.blockStmt(stmt.Body)) + n := ir.NewForStmt(p.pos(stmt), p.simpleStmt(stmt.Init), p.expr(stmt.Cond), p.stmt(stmt.Post), p.blockStmt(stmt.Body)) p.closeAnotherScope() return n } func (p *noder) switchStmt(stmt *syntax.SwitchStmt) ir.Node { p.openScope(stmt.Pos()) - n := ir.NewSwitchStmt(p.pos(stmt), nil, nil) - if stmt.Init != nil { - *n.PtrInit() = []ir.Node{p.stmt(stmt.Init)} - } - if stmt.Tag != nil { - n.Tag = p.expr(stmt.Tag) - } + + init := p.simpleStmt(stmt.Init) + n := ir.NewSwitchStmt(p.pos(stmt), p.expr(stmt.Tag), nil) + *n.PtrInit() = init var tswitch *ir.TypeSwitchGuard if l := n.Tag; l != nil && l.Op() == ir.OTYPESW { @@ -1241,10 +1214,7 @@ func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitch } p.openScope(clause.Pos()) - n := ir.NewCaseStmt(p.pos(clause), nil, nil) - if clause.Cases != nil { - n.List.Set(p.exprList(clause.Cases)) - } + n := ir.NewCaseStmt(p.pos(clause), p.exprList(clause.Cases), nil) if tswitch != nil && tswitch.Tag != nil { nn := typecheck.NewName(tswitch.Tag.Sym()) typecheck.Declare(nn, typecheck.DeclContext) @@ -1283,13 +1253,18 @@ func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitch } func (p *noder) selectStmt(stmt *syntax.SelectStmt) ir.Node { - n := ir.NewSelectStmt(p.pos(stmt), nil) - n.Cases.Set(p.commClauses(stmt.Body, stmt.Rbrace)) - return n + return ir.NewSelectStmt(p.pos(stmt), p.commClauses(stmt.Body, stmt.Rbrace)) +} + +func (p *noder) simpleStmt(stmt syntax.SimpleStmt) []ir.Node { + if stmt == nil { + return nil + } + return []ir.Node{p.stmt(stmt)} } func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []ir.Node { - nodes := make([]ir.Node, 0, len(clauses)) + nodes := make([]ir.Node, len(clauses)) for i, clause := range clauses { p.setlineno(clause) if i > 0 { @@ -1297,12 +1272,7 @@ func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []i } p.openScope(clause.Pos()) - n := ir.NewCaseStmt(p.pos(clause), nil, nil) - if clause.Comm != nil { - n.List = []ir.Node{p.stmt(clause.Comm)} - } - n.Body.Set(p.stmts(clause.Body)) - nodes = append(nodes, n) + nodes[i] = ir.NewCaseStmt(p.pos(clause), p.simpleStmt(clause.Comm), p.stmts(clause.Body)) } if len(clauses) > 0 { p.closeScope(rbrace) -- cgit v1.3-5-g9baa From d1d64e4cea41bf908152e6a9c45980946e7825a2 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 23 Dec 2020 06:06:31 -0800 Subject: [dev.regabi] cmd/compile: split SliceExpr.List into separate fields Passes toolstash -cmp. Change-Id: I4e31154d04d99f2b80bec6a2c571a2a4a3f2ec99 Reviewed-on: https://go-review.googlesource.com/c/go/+/279959 Run-TryBot: Matthew Dempsky Reviewed-by: Cuong Manh Le TryBot-Result: Go Bot Trust: Matthew Dempsky --- src/cmd/compile/internal/escape/escape.go | 7 ++- src/cmd/compile/internal/ir/expr.go | 63 +++------------------------ src/cmd/compile/internal/ir/fmt.go | 13 +++--- src/cmd/compile/internal/ir/node_gen.go | 9 ++-- src/cmd/compile/internal/noder/noder.go | 11 +++-- src/cmd/compile/internal/ssagen/ssa.go | 24 +++++----- src/cmd/compile/internal/typecheck/expr.go | 22 ++++------ src/cmd/compile/internal/typecheck/iexport.go | 8 ++-- src/cmd/compile/internal/typecheck/iimport.go | 7 ++- src/cmd/compile/internal/walk/assign.go | 12 ++--- src/cmd/compile/internal/walk/builtin.go | 8 ++-- src/cmd/compile/internal/walk/complit.go | 2 +- src/cmd/compile/internal/walk/convert.go | 2 +- src/cmd/compile/internal/walk/expr.go | 24 +++++----- src/cmd/compile/internal/walk/order.go | 11 ++--- 15 files changed, 72 insertions(+), 151 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/escape/escape.go b/src/cmd/compile/internal/escape/escape.go index b7cb56b997..338b2e0680 100644 --- a/src/cmd/compile/internal/escape/escape.go +++ b/src/cmd/compile/internal/escape/escape.go @@ -559,10 +559,9 @@ func (e *escape) exprSkipInit(k hole, n ir.Node) { case ir.OSLICE, ir.OSLICEARR, ir.OSLICE3, ir.OSLICE3ARR, ir.OSLICESTR: n := n.(*ir.SliceExpr) e.expr(k.note(n, "slice"), n.X) - low, high, max := n.SliceBounds() - e.discard(low) - e.discard(high) - e.discard(max) + e.discard(n.Low) + e.discard(n.High) + e.discard(n.Max) case ir.OCONV, ir.OCONVNOP: n := n.(*ir.ConvExpr) diff --git a/src/cmd/compile/internal/ir/expr.go b/src/cmd/compile/internal/ir/expr.go index d862a645d0..4675966090 100644 --- a/src/cmd/compile/internal/ir/expr.go +++ b/src/cmd/compile/internal/ir/expr.go @@ -605,11 +605,13 @@ func (*SelectorExpr) CanBeNtype() {} type SliceExpr struct { miniExpr X Node - List Nodes // TODO(rsc): Use separate Nodes + Low Node + High Node + Max Node } -func NewSliceExpr(pos src.XPos, op Op, x Node) *SliceExpr { - n := &SliceExpr{X: x} +func NewSliceExpr(pos src.XPos, op Op, x, low, high, max Node) *SliceExpr { + n := &SliceExpr{X: x, Low: low, High: high, Max: max} n.pos = pos n.op = op return n @@ -624,61 +626,6 @@ func (n *SliceExpr) SetOp(op Op) { } } -// SliceBounds returns n's slice bounds: low, high, and max in expr[low:high:max]. -// n must be a slice expression. max is nil if n is a simple slice expression. -func (n *SliceExpr) SliceBounds() (low, high, max Node) { - if len(n.List) == 0 { - return nil, nil, nil - } - - switch n.Op() { - case OSLICE, OSLICEARR, OSLICESTR: - s := n.List - return s[0], s[1], nil - case OSLICE3, OSLICE3ARR: - s := n.List - return s[0], s[1], s[2] - } - base.Fatalf("SliceBounds op %v: %v", n.Op(), n) - return nil, nil, nil -} - -// SetSliceBounds sets n's slice bounds, where n is a slice expression. -// n must be a slice expression. If max is non-nil, n must be a full slice expression. -func (n *SliceExpr) SetSliceBounds(low, high, max Node) { - switch n.Op() { - case OSLICE, OSLICEARR, OSLICESTR: - if max != nil { - base.Fatalf("SetSliceBounds %v given three bounds", n.Op()) - } - s := n.List - if s == nil { - if low == nil && high == nil { - return - } - n.List = []Node{low, high} - return - } - s[0] = low - s[1] = high - return - case OSLICE3, OSLICE3ARR: - s := n.List - if s == nil { - if low == nil && high == nil && max == nil { - return - } - n.List = []Node{low, high, max} - return - } - s[0] = low - s[1] = high - s[2] = max - return - } - base.Fatalf("SetSliceBounds op %v: %v", n.Op(), n) -} - // IsSlice3 reports whether o is a slice3 op (OSLICE3, OSLICE3ARR). // o must be a slicing op. func (o Op) IsSlice3() bool { diff --git a/src/cmd/compile/internal/ir/fmt.go b/src/cmd/compile/internal/ir/fmt.go index 8cfc38a9ae..b882979aa4 100644 --- a/src/cmd/compile/internal/ir/fmt.go +++ b/src/cmd/compile/internal/ir/fmt.go @@ -782,18 +782,17 @@ func exprFmt(n Node, s fmt.State, prec int) { n := n.(*SliceExpr) exprFmt(n.X, s, nprec) fmt.Fprint(s, "[") - low, high, max := n.SliceBounds() - if low != nil { - fmt.Fprint(s, low) + if n.Low != nil { + fmt.Fprint(s, n.Low) } fmt.Fprint(s, ":") - if high != nil { - fmt.Fprint(s, high) + if n.High != nil { + fmt.Fprint(s, n.High) } if n.Op().IsSlice3() { fmt.Fprint(s, ":") - if max != nil { - fmt.Fprint(s, max) + if n.Max != nil { + fmt.Fprint(s, n.Max) } } fmt.Fprint(s, "]") diff --git a/src/cmd/compile/internal/ir/node_gen.go b/src/cmd/compile/internal/ir/node_gen.go index d11e7bf918..23205b61fe 100644 --- a/src/cmd/compile/internal/ir/node_gen.go +++ b/src/cmd/compile/internal/ir/node_gen.go @@ -838,20 +838,23 @@ func (n *SliceExpr) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *SliceExpr) copy() Node { c := *n c.init = c.init.Copy() - c.List = c.List.Copy() return &c } func (n *SliceExpr) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) err = maybeDo(n.X, err, do) - err = maybeDoList(n.List, err, do) + err = maybeDo(n.Low, err, do) + err = maybeDo(n.High, err, do) + err = maybeDo(n.Max, err, do) return err } func (n *SliceExpr) editChildren(edit func(Node) Node) { editList(n.init, edit) n.X = maybeEdit(n.X, edit) - editList(n.List, edit) + n.Low = maybeEdit(n.Low, edit) + n.High = maybeEdit(n.High, edit) + n.Max = maybeEdit(n.Max, edit) } func (n *SliceHeaderExpr) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index c73e2d7fc5..4789740bd1 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -682,15 +682,14 @@ func (p *noder) expr(expr syntax.Expr) ir.Node { if expr.Full { op = ir.OSLICE3 } - n := ir.NewSliceExpr(p.pos(expr), op, p.expr(expr.X)) + x := p.expr(expr.X) var index [3]ir.Node - for i, x := range &expr.Index { - if x != nil { - index[i] = p.expr(x) + for i, n := range &expr.Index { + if n != nil { + index[i] = p.expr(n) } } - n.SetSliceBounds(index[0], index[1], index[2]) - return n + return ir.NewSliceExpr(p.pos(expr), op, x, index[0], index[1], index[2]) case *syntax.AssertExpr: return ir.NewTypeAssertExpr(p.pos(expr), p.expr(expr.X), p.typeExpr(expr.Type)) case *syntax.Operation: diff --git a/src/cmd/compile/internal/ssagen/ssa.go b/src/cmd/compile/internal/ssagen/ssa.go index 6b2ba5a781..cf683e578d 100644 --- a/src/cmd/compile/internal/ssagen/ssa.go +++ b/src/cmd/compile/internal/ssagen/ssa.go @@ -1367,7 +1367,7 @@ func (s *state) stmt(n ir.Node) { // We're assigning a slicing operation back to its source. // Don't write back fields we aren't changing. See issue #14855. rhs := rhs.(*ir.SliceExpr) - i, j, k := rhs.SliceBounds() + i, j, k := rhs.Low, rhs.High, rhs.Max if i != nil && (i.Op() == ir.OLITERAL && i.Val().Kind() == constant.Int && ir.Int64Val(i) == 0) { // [0:...] is the same as [:...] i = nil @@ -2852,15 +2852,14 @@ func (s *state) expr(n ir.Node) *ssa.Value { n := n.(*ir.SliceExpr) v := s.expr(n.X) var i, j, k *ssa.Value - low, high, max := n.SliceBounds() - if low != nil { - i = s.expr(low) + if n.Low != nil { + i = s.expr(n.Low) } - if high != nil { - j = s.expr(high) + if n.High != nil { + j = s.expr(n.High) } - if max != nil { - k = s.expr(max) + if n.Max != nil { + k = s.expr(n.Max) } p, l, c := s.slice(v, i, j, k, n.Bounded()) return s.newValue3(ssa.OpSliceMake, n.Type(), p, l, c) @@ -2869,12 +2868,11 @@ func (s *state) expr(n ir.Node) *ssa.Value { n := n.(*ir.SliceExpr) v := s.expr(n.X) var i, j *ssa.Value - low, high, _ := n.SliceBounds() - if low != nil { - i = s.expr(low) + if n.Low != nil { + i = s.expr(n.Low) } - if high != nil { - j = s.expr(high) + if n.High != nil { + j = s.expr(n.High) } p, l, _ := s.slice(v, i, j, nil, n.Bounded()) return s.newValue2(ssa.OpStringMake, n.Type(), p, l) diff --git a/src/cmd/compile/internal/typecheck/expr.go b/src/cmd/compile/internal/typecheck/expr.go index 00615c506c..6bbb68550e 100644 --- a/src/cmd/compile/internal/typecheck/expr.go +++ b/src/cmd/compile/internal/typecheck/expr.go @@ -831,17 +831,11 @@ func tcSPtr(n *ir.UnaryExpr) ir.Node { // tcSlice typechecks an OSLICE or OSLICE3 node. func tcSlice(n *ir.SliceExpr) ir.Node { - n.X = Expr(n.X) - low, high, max := n.SliceBounds() + n.X = DefaultLit(Expr(n.X), nil) + n.Low = indexlit(Expr(n.Low)) + n.High = indexlit(Expr(n.High)) + n.Max = indexlit(Expr(n.Max)) hasmax := n.Op().IsSlice3() - low = Expr(low) - high = Expr(high) - max = Expr(max) - n.X = DefaultLit(n.X, nil) - low = indexlit(low) - high = indexlit(high) - max = indexlit(max) - n.SetSliceBounds(low, high, max) l := n.X if l.Type() == nil { n.SetType(nil) @@ -886,19 +880,19 @@ func tcSlice(n *ir.SliceExpr) ir.Node { return n } - if low != nil && !checksliceindex(l, low, tp) { + if n.Low != nil && !checksliceindex(l, n.Low, tp) { n.SetType(nil) return n } - if high != nil && !checksliceindex(l, high, tp) { + if n.High != nil && !checksliceindex(l, n.High, tp) { n.SetType(nil) return n } - if max != nil && !checksliceindex(l, max, tp) { + if n.Max != nil && !checksliceindex(l, n.Max, tp) { n.SetType(nil) return n } - if !checksliceconst(low, high) || !checksliceconst(low, max) || !checksliceconst(high, max) { + if !checksliceconst(n.Low, n.High) || !checksliceconst(n.Low, n.Max) || !checksliceconst(n.High, n.Max) { n.SetType(nil) return n } diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index 8ac791c036..365e4315bc 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1370,17 +1370,15 @@ func (w *exportWriter) expr(n ir.Node) { w.op(ir.OSLICE) w.pos(n.Pos()) w.expr(n.X) - low, high, _ := n.SliceBounds() - w.exprsOrNil(low, high) + w.exprsOrNil(n.Low, n.High) case ir.OSLICE3, ir.OSLICE3ARR: n := n.(*ir.SliceExpr) w.op(ir.OSLICE3) w.pos(n.Pos()) w.expr(n.X) - low, high, max := n.SliceBounds() - w.exprsOrNil(low, high) - w.expr(max) + w.exprsOrNil(n.Low, n.High) + w.expr(n.Max) case ir.OCOPY, ir.OCOMPLEX: // treated like other builtin calls (see e.g., OREAL) diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index c4d840d2ac..cc8646977d 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -902,14 +902,13 @@ func (r *importReader) node() ir.Node { return ir.NewIndexExpr(r.pos(), r.expr(), r.expr()) case ir.OSLICE, ir.OSLICE3: - n := ir.NewSliceExpr(r.pos(), op, r.expr()) + pos, x := r.pos(), r.expr() low, high := r.exprsOrNil() var max ir.Node - if n.Op().IsSlice3() { + if op.IsSlice3() { max = r.expr() } - n.SetSliceBounds(low, high, max) - return n + return ir.NewSliceExpr(pos, op, x, low, high, max) // case OCONV, OCONVIFACE, OCONVNOP, OBYTES2STR, ORUNES2STR, OSTR2BYTES, OSTR2RUNES, ORUNESTR: // unreachable - mapped to OCONV case below by exporter diff --git a/src/cmd/compile/internal/walk/assign.go b/src/cmd/compile/internal/walk/assign.go index 6b0e2b272c..99c1abd73f 100644 --- a/src/cmd/compile/internal/walk/assign.go +++ b/src/cmd/compile/internal/walk/assign.go @@ -700,17 +700,15 @@ func appendSlice(n *ir.CallExpr, init *ir.Nodes) ir.Node { nodes.Append(nif) // s = s[:n] - nt := ir.NewSliceExpr(base.Pos, ir.OSLICE, s) - nt.SetSliceBounds(nil, nn, nil) + nt := ir.NewSliceExpr(base.Pos, ir.OSLICE, s, nil, nn, nil) nt.SetBounded(true) nodes.Append(ir.NewAssignStmt(base.Pos, s, nt)) var ncopy ir.Node if elemtype.HasPointers() { // copy(s[len(l1):], l2) - slice := ir.NewSliceExpr(base.Pos, ir.OSLICE, s) + slice := ir.NewSliceExpr(base.Pos, ir.OSLICE, s, ir.NewUnaryExpr(base.Pos, ir.OLEN, l1), nil, nil) slice.SetType(s.Type()) - slice.SetSliceBounds(ir.NewUnaryExpr(base.Pos, ir.OLEN, l1), nil, nil) ir.CurFunc.SetWBPos(n.Pos()) @@ -724,9 +722,8 @@ func appendSlice(n *ir.CallExpr, init *ir.Nodes) ir.Node { // rely on runtime to instrument: // copy(s[len(l1):], l2) // l2 can be a slice or string. - slice := ir.NewSliceExpr(base.Pos, ir.OSLICE, s) + slice := ir.NewSliceExpr(base.Pos, ir.OSLICE, s, ir.NewUnaryExpr(base.Pos, ir.OLEN, l1), nil, nil) slice.SetType(s.Type()) - slice.SetSliceBounds(ir.NewUnaryExpr(base.Pos, ir.OLEN, l1), nil, nil) ptr1, len1 := backingArrayPtrLen(cheapExpr(slice, &nodes)) ptr2, len2 := backingArrayPtrLen(l2) @@ -870,8 +867,7 @@ func extendSlice(n *ir.CallExpr, init *ir.Nodes) ir.Node { nodes = append(nodes, nif) // s = s[:n] - nt := ir.NewSliceExpr(base.Pos, ir.OSLICE, s) - nt.SetSliceBounds(nil, nn, nil) + nt := ir.NewSliceExpr(base.Pos, ir.OSLICE, s, nil, nn, nil) nt.SetBounded(true) nodes = append(nodes, ir.NewAssignStmt(base.Pos, s, nt)) diff --git a/src/cmd/compile/internal/walk/builtin.go b/src/cmd/compile/internal/walk/builtin.go index 63f7925863..fe6045cbbd 100644 --- a/src/cmd/compile/internal/walk/builtin.go +++ b/src/cmd/compile/internal/walk/builtin.go @@ -95,8 +95,7 @@ func walkAppend(n *ir.CallExpr, init *ir.Nodes, dst ir.Node) ir.Node { nn := typecheck.Temp(types.Types[types.TINT]) l = append(l, ir.NewAssignStmt(base.Pos, nn, ir.NewUnaryExpr(base.Pos, ir.OLEN, ns))) // n = len(s) - slice := ir.NewSliceExpr(base.Pos, ir.OSLICE, ns) // ...s[:n+argc] - slice.SetSliceBounds(nil, ir.NewBinaryExpr(base.Pos, ir.OADD, nn, na), nil) + slice := ir.NewSliceExpr(base.Pos, ir.OSLICE, ns, nil, ir.NewBinaryExpr(base.Pos, ir.OADD, nn, na), nil) // ...s[:n+argc] slice.SetBounded(true) l = append(l, ir.NewAssignStmt(base.Pos, ns, slice)) // s = s[:n+argc] @@ -407,9 +406,8 @@ func walkMakeSlice(n *ir.MakeExpr, init *ir.Nodes) ir.Node { t = types.NewArray(t.Elem(), i) // [r]T var_ := typecheck.Temp(t) - appendWalkStmt(init, ir.NewAssignStmt(base.Pos, var_, nil)) // zero temp - r := ir.NewSliceExpr(base.Pos, ir.OSLICE, var_) // arr[:l] - r.SetSliceBounds(nil, l, nil) + appendWalkStmt(init, ir.NewAssignStmt(base.Pos, var_, nil)) // zero temp + r := ir.NewSliceExpr(base.Pos, ir.OSLICE, var_, nil, l, nil) // arr[:l] // The conv is necessary in case n.Type is named. return walkExpr(typecheck.Expr(typecheck.Conv(r, n.Type())), init) } diff --git a/src/cmd/compile/internal/walk/complit.go b/src/cmd/compile/internal/walk/complit.go index 6fbbee9284..b53fe2e935 100644 --- a/src/cmd/compile/internal/walk/complit.go +++ b/src/cmd/compile/internal/walk/complit.go @@ -425,7 +425,7 @@ func slicelit(ctxt initContext, n *ir.CompLitExpr, var_ ir.Node, init *ir.Nodes) } // make slice out of heap (6) - a = ir.NewAssignStmt(base.Pos, var_, ir.NewSliceExpr(base.Pos, ir.OSLICE, vauto)) + a = ir.NewAssignStmt(base.Pos, var_, ir.NewSliceExpr(base.Pos, ir.OSLICE, vauto, nil, nil, nil)) a = typecheck.Stmt(a) a = orderStmtInPlace(a, map[string][]*ir.Name{}) diff --git a/src/cmd/compile/internal/walk/convert.go b/src/cmd/compile/internal/walk/convert.go index 21426c9817..fd954d6113 100644 --- a/src/cmd/compile/internal/walk/convert.go +++ b/src/cmd/compile/internal/walk/convert.go @@ -260,7 +260,7 @@ func walkStringToBytes(n *ir.ConvExpr, init *ir.Nodes) ir.Node { } // Slice the [n]byte to a []byte. - slice := ir.NewSliceExpr(n.Pos(), ir.OSLICEARR, p) + slice := ir.NewSliceExpr(n.Pos(), ir.OSLICEARR, p, nil, nil, nil) slice.SetType(n.Type()) slice.SetTypecheck(1) return walkExpr(slice, init) diff --git a/src/cmd/compile/internal/walk/expr.go b/src/cmd/compile/internal/walk/expr.go index 4f57962205..658a579fda 100644 --- a/src/cmd/compile/internal/walk/expr.go +++ b/src/cmd/compile/internal/walk/expr.go @@ -786,21 +786,19 @@ func walkSlice(n *ir.SliceExpr, init *ir.Nodes) ir.Node { n.X = walkExpr(n.X, init) } - low, high, max := n.SliceBounds() - low = walkExpr(low, init) - if low != nil && ir.IsZero(low) { + n.Low = walkExpr(n.Low, init) + if n.Low != nil && ir.IsZero(n.Low) { // Reduce x[0:j] to x[:j] and x[0:j:k] to x[:j:k]. - low = nil + n.Low = nil } - high = walkExpr(high, init) - max = walkExpr(max, init) - n.SetSliceBounds(low, high, max) + n.High = walkExpr(n.High, init) + n.Max = walkExpr(n.Max, init) if checkSlice { - n.X = walkCheckPtrAlignment(n.X.(*ir.ConvExpr), init, max) + n.X = walkCheckPtrAlignment(n.X.(*ir.ConvExpr), init, n.Max) } if n.Op().IsSlice3() { - if max != nil && max.Op() == ir.OCAP && ir.SameSafeExpr(n.X, max.(*ir.UnaryExpr).X) { + if n.Max != nil && n.Max.Op() == ir.OCAP && ir.SameSafeExpr(n.X, n.Max.(*ir.UnaryExpr).X) { // Reduce x[i:j:cap(x)] to x[i:j]. if n.Op() == ir.OSLICE3 { n.SetOp(ir.OSLICE) @@ -824,13 +822,11 @@ func walkSliceHeader(n *ir.SliceHeaderExpr, init *ir.Nodes) ir.Node { // TODO(josharian): combine this with its caller and simplify func reduceSlice(n *ir.SliceExpr) ir.Node { - low, high, max := n.SliceBounds() - if high != nil && high.Op() == ir.OLEN && ir.SameSafeExpr(n.X, high.(*ir.UnaryExpr).X) { + if n.High != nil && n.High.Op() == ir.OLEN && ir.SameSafeExpr(n.X, n.High.(*ir.UnaryExpr).X) { // Reduce x[i:len(x)] to x[i:]. - high = nil + n.High = nil } - n.SetSliceBounds(low, high, max) - if (n.Op() == ir.OSLICE || n.Op() == ir.OSLICESTR) && low == nil && high == nil { + if (n.Op() == ir.OSLICE || n.Op() == ir.OSLICESTR) && n.Low == nil && n.High == nil { // Reduce x[:] to x. if base.Debug.Slice > 0 { base.Warn("slice: omit slice operation") diff --git a/src/cmd/compile/internal/walk/order.go b/src/cmd/compile/internal/walk/order.go index 03310a50c6..de6a3807e6 100644 --- a/src/cmd/compile/internal/walk/order.go +++ b/src/cmd/compile/internal/walk/order.go @@ -1296,14 +1296,9 @@ func (o *orderState) expr1(n, lhs ir.Node) ir.Node { case ir.OSLICE, ir.OSLICEARR, ir.OSLICESTR, ir.OSLICE3, ir.OSLICE3ARR: n := n.(*ir.SliceExpr) n.X = o.expr(n.X, nil) - low, high, max := n.SliceBounds() - low = o.expr(low, nil) - low = o.cheapExpr(low) - high = o.expr(high, nil) - high = o.cheapExpr(high) - max = o.expr(max, nil) - max = o.cheapExpr(max) - n.SetSliceBounds(low, high, max) + n.Low = o.cheapExpr(o.expr(n.Low, nil)) + n.High = o.cheapExpr(o.expr(n.High, nil)) + n.Max = o.cheapExpr(o.expr(n.Max, nil)) if lhs == nil || lhs.Op() != ir.ONAME && !ir.SameSafeExpr(lhs, n.X) { return o.copyExpr(n) } -- cgit v1.3-5-g9baa From 40818038bf513405bc988678a297a5a6d24f6513 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 23 Dec 2020 06:59:16 -0800 Subject: [dev.regabi] cmd/compile: change CaseStmt.Vars to Var There's only ever one variable implicitly declared by a CaseStmt. It's only a slice because we previous used Rlist for this. Passes toolstash -cmp. Change-Id: Idf747f3ec6dfbbe4e94d60546ba04a81754df3fe Reviewed-on: https://go-review.googlesource.com/c/go/+/280012 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/escape/escape.go | 2 +- src/cmd/compile/internal/ir/node_gen.go | 5 ++--- src/cmd/compile/internal/ir/stmt.go | 2 +- src/cmd/compile/internal/noder/noder.go | 2 +- src/cmd/compile/internal/typecheck/iexport.go | 2 +- src/cmd/compile/internal/typecheck/iimport.go | 2 +- src/cmd/compile/internal/typecheck/stmt.go | 6 +++--- src/cmd/compile/internal/walk/switch.go | 5 +---- 8 files changed, 11 insertions(+), 15 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/escape/escape.go b/src/cmd/compile/internal/escape/escape.go index 338b2e0680..7a52ff3b88 100644 --- a/src/cmd/compile/internal/escape/escape.go +++ b/src/cmd/compile/internal/escape/escape.go @@ -373,7 +373,7 @@ func (e *escape) stmt(n ir.Node) { for _, cas := range n.Cases { // cases cas := cas.(*ir.CaseStmt) if typesw && n.Tag.(*ir.TypeSwitchGuard).Tag != nil { - cv := cas.Vars[0] + cv := cas.Var k := e.dcl(cv) // type switch variables have no ODCL. if cv.Type().HasPointers() { ks = append(ks, k.dotType(cv.Type(), cas, "switch case")) diff --git a/src/cmd/compile/internal/ir/node_gen.go b/src/cmd/compile/internal/ir/node_gen.go index 23205b61fe..7d3488f3fd 100644 --- a/src/cmd/compile/internal/ir/node_gen.go +++ b/src/cmd/compile/internal/ir/node_gen.go @@ -230,7 +230,6 @@ func (n *CaseStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *CaseStmt) copy() Node { c := *n c.init = c.init.Copy() - c.Vars = c.Vars.Copy() c.List = c.List.Copy() c.Body = c.Body.Copy() return &c @@ -238,7 +237,7 @@ func (n *CaseStmt) copy() Node { func (n *CaseStmt) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) - err = maybeDoList(n.Vars, err, do) + err = maybeDo(n.Var, err, do) err = maybeDoList(n.List, err, do) err = maybeDo(n.Comm, err, do) err = maybeDoList(n.Body, err, do) @@ -246,7 +245,7 @@ func (n *CaseStmt) doChildren(do func(Node) error) error { } func (n *CaseStmt) editChildren(edit func(Node) Node) { editList(n.init, edit) - editList(n.Vars, edit) + n.Var = maybeEdit(n.Var, edit) editList(n.List, edit) n.Comm = maybeEdit(n.Comm, edit) editList(n.Body, edit) diff --git a/src/cmd/compile/internal/ir/stmt.go b/src/cmd/compile/internal/ir/stmt.go index ad6db436a7..c9988eba5c 100644 --- a/src/cmd/compile/internal/ir/stmt.go +++ b/src/cmd/compile/internal/ir/stmt.go @@ -176,7 +176,7 @@ func (n *BranchStmt) Sym() *types.Sym { return n.Label } // A CaseStmt is a case statement in a switch or select: case List: Body. type CaseStmt struct { miniStmt - Vars Nodes // declared variable for this case in type switch + Var Node // declared variable for this case in type switch List Nodes // list of expressions for switch, early select Comm Node // communication case (Exprs[0]) after select is type-checked Body Nodes diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index 4789740bd1..68a01612dc 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -1217,7 +1217,7 @@ func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitch if tswitch != nil && tswitch.Tag != nil { nn := typecheck.NewName(tswitch.Tag.Sym()) typecheck.Declare(nn, typecheck.DeclContext) - n.Vars = []ir.Node{nn} + n.Var = nn // keep track of the instances for reporting unused nn.Defn = tswitch } diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index 365e4315bc..4cb943daaf 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1196,7 +1196,7 @@ func (w *exportWriter) caseList(cases []ir.Node, namedTypeSwitch bool) { w.pos(cas.Pos()) w.stmtList(cas.List) if namedTypeSwitch { - w.localName(cas.Vars[0].(*ir.Name)) + w.localName(cas.Var.(*ir.Name)) } w.stmtList(cas.Body) } diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index cc8646977d..221229571c 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -780,7 +780,7 @@ func (r *importReader) caseList(switchExpr ir.Node) []ir.Node { // Sym for diagnostics anyway. caseVar := ir.NewNameAt(cas.Pos(), r.ident()) Declare(caseVar, DeclContext) - cas.Vars = []ir.Node{caseVar} + cas.Var = caseVar caseVar.Defn = switchExpr } cas.Body.Set(r.stmtList()) diff --git a/src/cmd/compile/internal/typecheck/stmt.go b/src/cmd/compile/internal/typecheck/stmt.go index bf3801eea2..133f93e53b 100644 --- a/src/cmd/compile/internal/typecheck/stmt.go +++ b/src/cmd/compile/internal/typecheck/stmt.go @@ -694,7 +694,7 @@ func tcSwitchType(n *ir.SwitchStmt) { ts.add(ncase.Pos(), n1.Type()) } - if len(ncase.Vars) != 0 { + if ncase.Var != nil { // Assign the clause variable's type. vt := t if len(ls) == 1 { @@ -707,7 +707,7 @@ func tcSwitchType(n *ir.SwitchStmt) { } } - nvar := ncase.Vars[0] + nvar := ncase.Var nvar.SetType(vt) if vt != nil { nvar = AssignExpr(nvar) @@ -716,7 +716,7 @@ func tcSwitchType(n *ir.SwitchStmt) { nvar.SetTypecheck(1) nvar.SetWalkdef(1) } - ncase.Vars[0] = nvar + ncase.Var = nvar } Stmts(ncase.Body) diff --git a/src/cmd/compile/internal/walk/switch.go b/src/cmd/compile/internal/walk/switch.go index 360086ec79..7829d93373 100644 --- a/src/cmd/compile/internal/walk/switch.go +++ b/src/cmd/compile/internal/walk/switch.go @@ -334,10 +334,7 @@ func walkSwitchType(sw *ir.SwitchStmt) { var body ir.Nodes for _, ncase := range sw.Cases { ncase := ncase.(*ir.CaseStmt) - var caseVar ir.Node - if len(ncase.Vars) != 0 { - caseVar = ncase.Vars[0] - } + caseVar := ncase.Var // For single-type cases with an interface type, // we initialize the case variable as part of the type assertion. -- cgit v1.3-5-g9baa From 27b248b307e6db463930231a7820d5335424c04e Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Thu, 24 Dec 2020 13:09:20 +0700 Subject: [dev.regabi] cmd/compile: separate range stmt Vars to Key, Value nodes Passes buildall w/ toolstash -cmp. Change-Id: I9738fcabc8ebf3afa34d102afadf1b474b50db35 Reviewed-on: https://go-review.googlesource.com/c/go/+/279435 Trust: Cuong Manh Le Run-TryBot: Cuong Manh Le Reviewed-by: Matthew Dempsky TryBot-Result: Go Bot --- src/cmd/compile/internal/escape/escape.go | 18 +++--- src/cmd/compile/internal/ir/fmt.go | 13 ++-- src/cmd/compile/internal/ir/node_gen.go | 7 ++- src/cmd/compile/internal/ir/stmt.go | 11 ++-- src/cmd/compile/internal/noder/noder.go | 8 ++- src/cmd/compile/internal/typecheck/iexport.go | 2 +- src/cmd/compile/internal/typecheck/iimport.go | 4 +- src/cmd/compile/internal/typecheck/stmt.go | 91 ++++++++++----------------- src/cmd/compile/internal/walk/order.go | 5 +- src/cmd/compile/internal/walk/range.go | 18 +----- 10 files changed, 73 insertions(+), 104 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/escape/escape.go b/src/cmd/compile/internal/escape/escape.go index 7a52ff3b88..31d157b165 100644 --- a/src/cmd/compile/internal/escape/escape.go +++ b/src/cmd/compile/internal/escape/escape.go @@ -347,21 +347,19 @@ func (e *escape) stmt(n ir.Node) { e.loopDepth-- case ir.ORANGE: - // for List = range Right { Nbody } + // for Key, Value = range X { Body } n := n.(*ir.RangeStmt) e.loopDepth++ - ks := e.addrs(n.Vars) + e.addr(n.Key) + k := e.addr(n.Value) e.block(n.Body) e.loopDepth-- - // Right is evaluated outside the loop. - k := e.discardHole() - if len(ks) >= 2 { - if n.X.Type().IsArray() { - k = ks[1].note(n, "range") - } else { - k = ks[1].deref(n, "range-deref") - } + // X is evaluated outside the loop. + if n.X.Type().IsArray() { + k = k.note(n, "range") + } else { + k = k.deref(n, "range-deref") } e.expr(e.later(k), n.X) diff --git a/src/cmd/compile/internal/ir/fmt.go b/src/cmd/compile/internal/ir/fmt.go index b882979aa4..2b73c5ac1b 100644 --- a/src/cmd/compile/internal/ir/fmt.go +++ b/src/cmd/compile/internal/ir/fmt.go @@ -444,12 +444,15 @@ func stmtFmt(n Node, s fmt.State) { break } - if len(n.Vars) == 0 { - fmt.Fprintf(s, "for range %v { %v }", n.X, n.Body) - break + fmt.Fprint(s, "for") + if n.Key != nil { + fmt.Fprintf(s, " %v", n.Key) + if n.Value != nil { + fmt.Fprintf(s, ", %v", n.Value) + } + fmt.Fprint(s, " =") } - - fmt.Fprintf(s, "for %.v = range %v { %v }", n.Vars, n.X, n.Body) + fmt.Fprintf(s, " range %v { %v }", n.X, n.Body) case OSELECT: n := n.(*SelectStmt) diff --git a/src/cmd/compile/internal/ir/node_gen.go b/src/cmd/compile/internal/ir/node_gen.go index 7d3488f3fd..ecb39563c4 100644 --- a/src/cmd/compile/internal/ir/node_gen.go +++ b/src/cmd/compile/internal/ir/node_gen.go @@ -724,22 +724,23 @@ func (n *RangeStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *RangeStmt) copy() Node { c := *n c.init = c.init.Copy() - c.Vars = c.Vars.Copy() c.Body = c.Body.Copy() return &c } func (n *RangeStmt) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) - err = maybeDoList(n.Vars, err, do) err = maybeDo(n.X, err, do) + err = maybeDo(n.Key, err, do) + err = maybeDo(n.Value, err, do) err = maybeDoList(n.Body, err, do) return err } func (n *RangeStmt) editChildren(edit func(Node) Node) { editList(n.init, edit) - editList(n.Vars, edit) n.X = maybeEdit(n.X, edit) + n.Key = maybeEdit(n.Key, edit) + n.Value = maybeEdit(n.Value, edit) editList(n.Body, edit) } diff --git a/src/cmd/compile/internal/ir/stmt.go b/src/cmd/compile/internal/ir/stmt.go index c9988eba5c..453153c024 100644 --- a/src/cmd/compile/internal/ir/stmt.go +++ b/src/cmd/compile/internal/ir/stmt.go @@ -290,25 +290,24 @@ func NewLabelStmt(pos src.XPos, label *types.Sym) *LabelStmt { func (n *LabelStmt) Sym() *types.Sym { return n.Label } -// A RangeStmt is a range loop: for Vars = range X { Stmts } -// Op can be OFOR or OFORUNTIL (!Cond). +// A RangeStmt is a range loop: for Key, Value = range X { Body } type RangeStmt struct { miniStmt Label *types.Sym - Vars Nodes // TODO(rsc): Replace with Key, Value Node Def bool X Node + Key Node + Value Node Body Nodes HasBreak bool typ *types.Type // TODO(rsc): Remove - use X.Type() instead Prealloc *Name } -func NewRangeStmt(pos src.XPos, vars []Node, x Node, body []Node) *RangeStmt { - n := &RangeStmt{X: x} +func NewRangeStmt(pos src.XPos, key, value, x Node, body []Node) *RangeStmt { + n := &RangeStmt{X: x, Key: key, Value: value} n.pos = pos n.op = ORANGE - n.Vars.Set(vars) n.Body.Set(body) return n } diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index 68a01612dc..ad66b6c850 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -1172,10 +1172,14 @@ func (p *noder) forStmt(stmt *syntax.ForStmt) ir.Node { panic("unexpected RangeClause") } - n := ir.NewRangeStmt(p.pos(r), nil, p.expr(r.X), nil) + n := ir.NewRangeStmt(p.pos(r), nil, nil, p.expr(r.X), nil) if r.Lhs != nil { n.Def = r.Def - n.Vars.Set(p.assignList(r.Lhs, n, n.Def)) + lhs := p.assignList(r.Lhs, n, n.Def) + n.Key = lhs[0] + if len(lhs) > 1 { + n.Value = lhs[1] + } } n.Body.Set(p.blockStmt(stmt.Body)) p.closeAnotherScope() diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index 4cb943daaf..449d99266d 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1143,7 +1143,7 @@ func (w *exportWriter) stmt(n ir.Node) { n := n.(*ir.RangeStmt) w.op(ir.ORANGE) w.pos(n.Pos()) - w.stmtList(n.Vars) + w.exprsOrNil(n.Key, n.Value) w.expr(n.X) w.stmtList(n.Body) diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index 221229571c..8285c418e9 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -1028,7 +1028,9 @@ func (r *importReader) node() ir.Node { return ir.NewForStmt(pos, init, cond, post, r.stmtList()) case ir.ORANGE: - return ir.NewRangeStmt(r.pos(), r.stmtList(), r.expr(), r.stmtList()) + pos := r.pos() + k, v := r.exprsOrNil() + return ir.NewRangeStmt(pos, k, v, r.expr(), r.stmtList()) case ir.OSELECT: pos := r.pos() diff --git a/src/cmd/compile/internal/typecheck/stmt.go b/src/cmd/compile/internal/typecheck/stmt.go index 133f93e53b..dfa224b318 100644 --- a/src/cmd/compile/internal/typecheck/stmt.go +++ b/src/cmd/compile/internal/typecheck/stmt.go @@ -19,19 +19,18 @@ func typecheckrangeExpr(n *ir.RangeStmt) { return } // delicate little dance. see typecheckas2 - ls := n.Vars - for i1, n1 := range ls { - if !ir.DeclaredBy(n1, n) { - ls[i1] = AssignExpr(ls[i1]) - } + if n.Key != nil && !ir.DeclaredBy(n.Key, n) { + n.Key = AssignExpr(n.Key) + } + if n.Value != nil && !ir.DeclaredBy(n.Value, n) { + n.Value = AssignExpr(n.Value) } - if t.IsPtr() && t.Elem().IsArray() { t = t.Elem() } n.SetType(t) - var t1, t2 *types.Type + var tk, tv *types.Type toomany := false switch t.Kind() { default: @@ -39,12 +38,12 @@ func typecheckrangeExpr(n *ir.RangeStmt) { return case types.TARRAY, types.TSLICE: - t1 = types.Types[types.TINT] - t2 = t.Elem() + tk = types.Types[types.TINT] + tv = t.Elem() case types.TMAP: - t1 = t.Key() - t2 = t.Elem() + tk = t.Key() + tv = t.Elem() case types.TCHAN: if !t.ChanDir().CanRecv() { @@ -52,61 +51,35 @@ func typecheckrangeExpr(n *ir.RangeStmt) { return } - t1 = t.Elem() - t2 = nil - if len(n.Vars) == 2 { + tk = t.Elem() + tv = nil + if n.Value != nil { toomany = true } case types.TSTRING: - t1 = types.Types[types.TINT] - t2 = types.RuneType + tk = types.Types[types.TINT] + tv = types.RuneType } - if len(n.Vars) > 2 || toomany { + if toomany { base.ErrorfAt(n.Pos(), "too many variables in range") } - var v1, v2 ir.Node - if len(n.Vars) != 0 { - v1 = n.Vars[0] - } - if len(n.Vars) > 1 { - v2 = n.Vars[1] - } - - // this is not only an optimization but also a requirement in the spec. - // "if the second iteration variable is the blank identifier, the range - // clause is equivalent to the same clause with only the first variable - // present." - if ir.IsBlank(v2) { - if v1 != nil { - n.Vars = []ir.Node{v1} - } - v2 = nil - } - - if v1 != nil { - if ir.DeclaredBy(v1, n) { - v1.SetType(t1) - } else if v1.Type() != nil { - if op, why := assignop(t1, v1.Type()); op == ir.OXXX { - base.ErrorfAt(n.Pos(), "cannot assign type %v to %L in range%s", t1, v1, why) - } - } - checkassign(n, v1) - } - - if v2 != nil { - if ir.DeclaredBy(v2, n) { - v2.SetType(t2) - } else if v2.Type() != nil { - if op, why := assignop(t2, v2.Type()); op == ir.OXXX { - base.ErrorfAt(n.Pos(), "cannot assign type %v to %L in range%s", t2, v2, why) + do := func(nn ir.Node, t *types.Type) { + if nn != nil { + if ir.DeclaredBy(nn, n) { + nn.SetType(t) + } else if nn.Type() != nil { + if op, why := assignop(t, nn.Type()); op == ir.OXXX { + base.ErrorfAt(n.Pos(), "cannot assign type %v to %L in range%s", t, nn, why) + } } + checkassign(n, nn) } - checkassign(n, v2) } + do(n.Key, tk) + do(n.Value, tv) } // type check assignment. @@ -399,11 +372,11 @@ func tcRange(n *ir.RangeStmt) { // second half of dance, the first half being typecheckrangeExpr n.SetTypecheck(1) - ls := n.Vars - for i1, n1 := range ls { - if n1.Typecheck() == 0 { - ls[i1] = AssignExpr(ls[i1]) - } + if n.Key != nil && n.Key.Typecheck() == 0 { + n.Key = AssignExpr(n.Key) + } + if n.Value != nil && n.Value.Typecheck() == 0 { + n.Value = AssignExpr(n.Value) } decldepth++ diff --git a/src/cmd/compile/internal/walk/order.go b/src/cmd/compile/internal/walk/order.go index de6a3807e6..1fcebf5194 100644 --- a/src/cmd/compile/internal/walk/order.go +++ b/src/cmd/compile/internal/walk/order.go @@ -848,7 +848,7 @@ func (o *orderState) stmt(n ir.Node) { base.Fatalf("order.stmt range %v", n.Type()) case types.TARRAY, types.TSLICE: - if len(n.Vars) < 2 || ir.IsBlank(n.Vars[1]) { + if n.Value == nil || ir.IsBlank(n.Value) { // for i := range x will only use x once, to compute len(x). // No need to copy it. break @@ -887,7 +887,8 @@ func (o *orderState) stmt(n ir.Node) { // hiter contains pointers and needs to be zeroed. n.Prealloc = o.newTemp(reflectdata.MapIterType(n.Type()), true) } - o.exprListInPlace(n.Vars) + n.Key = o.exprInPlace(n.Key) + n.Value = o.exprInPlace(n.Value) if orderBody { orderBlock(&n.Body, o.free) } diff --git a/src/cmd/compile/internal/walk/range.go b/src/cmd/compile/internal/walk/range.go index 98a3dc23f9..5ecd577f74 100644 --- a/src/cmd/compile/internal/walk/range.go +++ b/src/cmd/compile/internal/walk/range.go @@ -61,15 +61,7 @@ func walkRange(nrange *ir.RangeStmt) ir.Node { a := nrange.X lno := ir.SetPos(a) - var v1, v2 ir.Node - l := len(nrange.Vars) - if l > 0 { - v1 = nrange.Vars[0] - } - - if l > 1 { - v2 = nrange.Vars[1] - } + v1, v2 := nrange.Key, nrange.Value if ir.IsBlank(v2) { v2 = nil @@ -343,15 +335,11 @@ func isMapClear(n *ir.RangeStmt) bool { return false } - if n.Op() != ir.ORANGE || n.Type().Kind() != types.TMAP || len(n.Vars) != 1 { - return false - } - - k := n.Vars[0] - if k == nil || ir.IsBlank(k) { + if n.Op() != ir.ORANGE || n.Type().Kind() != types.TMAP || n.Key == nil || n.Value != nil { return false } + k := n.Key // Require k to be a new variable name. if !ir.DeclaredBy(k, n) { return false -- cgit v1.3-5-g9baa From a59d26603f0dffbe6e914bc9ab29a2f9f70e5408 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sat, 26 Dec 2020 22:23:45 -0800 Subject: [dev.regabi] cmd/compile: use []*CaseStmt in {Select,Switch}Stmt Select and switch statements only ever contain case statements, so change their Cases fields from Nodes to []*CaseStmt. This allows removing a bunch of type assertions throughout the compiler. CaseStmt should be renamed to CaseClause, and SelectStmt should probably have its own CommClause type instead (like in go/ast and cmd/compile/internal/syntax), but this is a good start. Passes toolstash -cmp. Change-Id: I2d41d616d44512c2be421e1e2ff13d0ee8b238ad Reviewed-on: https://go-review.googlesource.com/c/go/+/280442 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/escape/escape.go | 2 -- src/cmd/compile/internal/ir/mknode.go | 7 ++++ src/cmd/compile/internal/ir/node_gen.go | 12 +++---- src/cmd/compile/internal/ir/stmt.go | 43 +++++++++++++++++++++---- src/cmd/compile/internal/ir/visit.go | 3 +- src/cmd/compile/internal/noder/noder.go | 10 +++--- src/cmd/compile/internal/typecheck/iexport.go | 3 +- src/cmd/compile/internal/typecheck/iimport.go | 4 +-- src/cmd/compile/internal/typecheck/stmt.go | 4 --- src/cmd/compile/internal/typecheck/typecheck.go | 13 ++++---- src/cmd/compile/internal/walk/order.go | 6 +--- src/cmd/compile/internal/walk/select.go | 12 +++---- src/cmd/compile/internal/walk/switch.go | 7 ++-- 13 files changed, 73 insertions(+), 53 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/escape/escape.go b/src/cmd/compile/internal/escape/escape.go index 31d157b165..d8f0111d2d 100644 --- a/src/cmd/compile/internal/escape/escape.go +++ b/src/cmd/compile/internal/escape/escape.go @@ -369,7 +369,6 @@ func (e *escape) stmt(n ir.Node) { var ks []hole for _, cas := range n.Cases { // cases - cas := cas.(*ir.CaseStmt) if typesw && n.Tag.(*ir.TypeSwitchGuard).Tag != nil { cv := cas.Var k := e.dcl(cv) // type switch variables have no ODCL. @@ -391,7 +390,6 @@ func (e *escape) stmt(n ir.Node) { case ir.OSELECT: n := n.(*ir.SelectStmt) for _, cas := range n.Cases { - cas := cas.(*ir.CaseStmt) e.stmt(cas.Comm) e.block(cas.Body) } diff --git a/src/cmd/compile/internal/ir/mknode.go b/src/cmd/compile/internal/ir/mknode.go index f5dacee622..edf3ee501c 100644 --- a/src/cmd/compile/internal/ir/mknode.go +++ b/src/cmd/compile/internal/ir/mknode.go @@ -37,6 +37,7 @@ func main() { nodeType := lookup("Node") ntypeType := lookup("Ntype") nodesType := lookup("Nodes") + slicePtrCaseStmtType := types.NewSlice(types.NewPointer(lookup("CaseStmt"))) ptrFieldType := types.NewPointer(lookup("Field")) slicePtrFieldType := types.NewSlice(ptrFieldType) ptrIdentType := types.NewPointer(lookup("Ident")) @@ -76,6 +77,8 @@ func main() { switch { case is(nodesType): fmt.Fprintf(&buf, "c.%s = c.%s.Copy()\n", name, name) + case is(slicePtrCaseStmtType): + fmt.Fprintf(&buf, "c.%s = copyCases(c.%s)\n", name, name) case is(ptrFieldType): fmt.Fprintf(&buf, "if c.%s != nil { c.%s = c.%s.copy() }\n", name, name, name) case is(slicePtrFieldType): @@ -94,6 +97,8 @@ func main() { fmt.Fprintf(&buf, "err = maybeDo(n.%s, err, do)\n", name) case is(nodesType): fmt.Fprintf(&buf, "err = maybeDoList(n.%s, err, do)\n", name) + case is(slicePtrCaseStmtType): + fmt.Fprintf(&buf, "err = maybeDoCases(n.%s, err, do)\n", name) case is(ptrFieldType): fmt.Fprintf(&buf, "err = maybeDoField(n.%s, err, do)\n", name) case is(slicePtrFieldType): @@ -113,6 +118,8 @@ func main() { fmt.Fprintf(&buf, "n.%s = toNtype(maybeEdit(n.%s, edit))\n", name, name) case is(nodesType): fmt.Fprintf(&buf, "editList(n.%s, edit)\n", name) + case is(slicePtrCaseStmtType): + fmt.Fprintf(&buf, "editCases(n.%s, edit)\n", name) case is(ptrFieldType): fmt.Fprintf(&buf, "editField(n.%s, edit)\n", name) case is(slicePtrFieldType): diff --git a/src/cmd/compile/internal/ir/node_gen.go b/src/cmd/compile/internal/ir/node_gen.go index ecb39563c4..041855bbe9 100644 --- a/src/cmd/compile/internal/ir/node_gen.go +++ b/src/cmd/compile/internal/ir/node_gen.go @@ -781,20 +781,20 @@ func (n *SelectStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *SelectStmt) copy() Node { c := *n c.init = c.init.Copy() - c.Cases = c.Cases.Copy() + c.Cases = copyCases(c.Cases) c.Compiled = c.Compiled.Copy() return &c } func (n *SelectStmt) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) - err = maybeDoList(n.Cases, err, do) + err = maybeDoCases(n.Cases, err, do) err = maybeDoList(n.Compiled, err, do) return err } func (n *SelectStmt) editChildren(edit func(Node) Node) { editList(n.init, edit) - editList(n.Cases, edit) + editCases(n.Cases, edit) editList(n.Compiled, edit) } @@ -945,7 +945,7 @@ func (n *SwitchStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *SwitchStmt) copy() Node { c := *n c.init = c.init.Copy() - c.Cases = c.Cases.Copy() + c.Cases = copyCases(c.Cases) c.Compiled = c.Compiled.Copy() return &c } @@ -953,14 +953,14 @@ func (n *SwitchStmt) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) err = maybeDo(n.Tag, err, do) - err = maybeDoList(n.Cases, err, do) + err = maybeDoCases(n.Cases, err, do) err = maybeDoList(n.Compiled, err, do) return err } func (n *SwitchStmt) editChildren(edit func(Node) Node) { editList(n.init, edit) n.Tag = maybeEdit(n.Tag, edit) - editList(n.Cases, edit) + editCases(n.Cases, edit) editList(n.Compiled, edit) } diff --git a/src/cmd/compile/internal/ir/stmt.go b/src/cmd/compile/internal/ir/stmt.go index cfda6fd234..ce775a8529 100644 --- a/src/cmd/compile/internal/ir/stmt.go +++ b/src/cmd/compile/internal/ir/stmt.go @@ -191,6 +191,37 @@ func NewCaseStmt(pos src.XPos, list, body []Node) *CaseStmt { return n } +func copyCases(list []*CaseStmt) []*CaseStmt { + if list == nil { + return nil + } + c := make([]*CaseStmt, len(list)) + copy(c, list) + return c +} + +func maybeDoCases(list []*CaseStmt, err error, do func(Node) error) error { + if err != nil { + return err + } + for _, x := range list { + if x != nil { + if err := do(x); err != nil { + return err + } + } + } + return nil +} + +func editCases(list []*CaseStmt, edit func(Node) Node) { + for i, x := range list { + if x != nil { + list[i] = edit(x).(*CaseStmt) + } + } +} + // A ForStmt is a non-range for loop: for Init; Cond; Post { Body } // Op can be OFOR or OFORUNTIL (!Cond). type ForStmt struct { @@ -334,18 +365,18 @@ func (n *ReturnStmt) SetOrig(x Node) { n.orig = x } type SelectStmt struct { miniStmt Label *types.Sym - Cases Nodes + Cases []*CaseStmt HasBreak bool // TODO(rsc): Instead of recording here, replace with a block? Compiled Nodes // compiled form, after walkswitch } -func NewSelectStmt(pos src.XPos, cases []Node) *SelectStmt { +func NewSelectStmt(pos src.XPos, cases []*CaseStmt) *SelectStmt { n := &SelectStmt{} n.pos = pos n.op = OSELECT - n.Cases.Set(cases) + n.Cases = cases return n } @@ -367,7 +398,7 @@ func NewSendStmt(pos src.XPos, ch, value Node) *SendStmt { type SwitchStmt struct { miniStmt Tag Node - Cases Nodes // list of *CaseStmt + Cases []*CaseStmt Label *types.Sym HasBreak bool @@ -375,11 +406,11 @@ type SwitchStmt struct { Compiled Nodes // compiled form, after walkswitch } -func NewSwitchStmt(pos src.XPos, tag Node, cases []Node) *SwitchStmt { +func NewSwitchStmt(pos src.XPos, tag Node, cases []*CaseStmt) *SwitchStmt { n := &SwitchStmt{Tag: tag} n.pos = pos n.op = OSWITCH - n.Cases.Set(cases) + n.Cases = cases return n } diff --git a/src/cmd/compile/internal/ir/visit.go b/src/cmd/compile/internal/ir/visit.go index a1c345968f..8839e1664d 100644 --- a/src/cmd/compile/internal/ir/visit.go +++ b/src/cmd/compile/internal/ir/visit.go @@ -217,10 +217,9 @@ func EditChildren(n Node, edit func(Node) Node) { // Note that editList only calls edit on the nodes in the list, not their children. // If x's children should be processed, edit(x) must call EditChildren(x, edit) itself. func editList(list Nodes, edit func(Node) Node) { - s := list for i, x := range list { if x != nil { - s[i] = edit(x) + list[i] = edit(x) } } } diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index ad66b6c850..b974448338 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -1202,14 +1202,14 @@ func (p *noder) switchStmt(stmt *syntax.SwitchStmt) ir.Node { if l := n.Tag; l != nil && l.Op() == ir.OTYPESW { tswitch = l.(*ir.TypeSwitchGuard) } - n.Cases.Set(p.caseClauses(stmt.Body, tswitch, stmt.Rbrace)) + n.Cases = p.caseClauses(stmt.Body, tswitch, stmt.Rbrace) p.closeScope(stmt.Rbrace) return n } -func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitchGuard, rbrace syntax.Pos) []ir.Node { - nodes := make([]ir.Node, 0, len(clauses)) +func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitchGuard, rbrace syntax.Pos) []*ir.CaseStmt { + nodes := make([]*ir.CaseStmt, 0, len(clauses)) for i, clause := range clauses { p.setlineno(clause) if i > 0 { @@ -1266,8 +1266,8 @@ func (p *noder) simpleStmt(stmt syntax.SimpleStmt) []ir.Node { return []ir.Node{p.stmt(stmt)} } -func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []ir.Node { - nodes := make([]ir.Node, len(clauses)) +func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*ir.CaseStmt { + nodes := make([]*ir.CaseStmt, len(clauses)) for i, clause := range clauses { p.setlineno(clause) if i > 0 { diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index 0c813a71ef..19437a069e 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1181,10 +1181,9 @@ func isNamedTypeSwitch(x ir.Node) bool { return ok && guard.Tag != nil } -func (w *exportWriter) caseList(cases []ir.Node, namedTypeSwitch bool) { +func (w *exportWriter) caseList(cases []*ir.CaseStmt, namedTypeSwitch bool) { w.uint64(uint64(len(cases))) for _, cas := range cases { - cas := cas.(*ir.CaseStmt) w.pos(cas.Pos()) w.stmtList(cas.List) if namedTypeSwitch { diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index 8285c418e9..fd8314b662 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -767,10 +767,10 @@ func (r *importReader) stmtList() []ir.Node { return list } -func (r *importReader) caseList(switchExpr ir.Node) []ir.Node { +func (r *importReader) caseList(switchExpr ir.Node) []*ir.CaseStmt { namedTypeSwitch := isNamedTypeSwitch(switchExpr) - cases := make([]ir.Node, r.uint64()) + cases := make([]*ir.CaseStmt, r.uint64()) for i := range cases { cas := ir.NewCaseStmt(r.pos(), nil, nil) cas.List.Set(r.stmtList()) diff --git a/src/cmd/compile/internal/typecheck/stmt.go b/src/cmd/compile/internal/typecheck/stmt.go index 7e74b730bc..03c3e399eb 100644 --- a/src/cmd/compile/internal/typecheck/stmt.go +++ b/src/cmd/compile/internal/typecheck/stmt.go @@ -364,8 +364,6 @@ func tcSelect(sel *ir.SelectStmt) { lno := ir.SetPos(sel) Stmts(sel.Init()) for _, ncase := range sel.Cases { - ncase := ncase.(*ir.CaseStmt) - if len(ncase.List) == 0 { // default if def != nil { @@ -508,7 +506,6 @@ func tcSwitchExpr(n *ir.SwitchStmt) { var defCase ir.Node var cs constSet for _, ncase := range n.Cases { - ncase := ncase.(*ir.CaseStmt) ls := ncase.List if len(ls) == 0 { // default: if defCase != nil { @@ -577,7 +574,6 @@ func tcSwitchType(n *ir.SwitchStmt) { var defCase, nilCase ir.Node var ts typeSet for _, ncase := range n.Cases { - ncase := ncase.(*ir.CaseStmt) ls := ncase.List if len(ls) == 0 { // default: if defCase != nil { diff --git a/src/cmd/compile/internal/typecheck/typecheck.go b/src/cmd/compile/internal/typecheck/typecheck.go index b779f9ceb0..dabfee3bf9 100644 --- a/src/cmd/compile/internal/typecheck/typecheck.go +++ b/src/cmd/compile/internal/typecheck/typecheck.go @@ -2103,7 +2103,6 @@ func isTermNode(n ir.Node) bool { } def := false for _, cas := range n.Cases { - cas := cas.(*ir.CaseStmt) if !isTermNodes(cas.Body) { return false } @@ -2119,7 +2118,6 @@ func isTermNode(n ir.Node) bool { return false } for _, cas := range n.Cases { - cas := cas.(*ir.CaseStmt) if !isTermNodes(cas.Body) { return false } @@ -2218,9 +2216,6 @@ func deadcodeslice(nn *ir.Nodes) { case ir.OBLOCK: n := n.(*ir.BlockStmt) deadcodeslice(&n.List) - case ir.OCASE: - n := n.(*ir.CaseStmt) - deadcodeslice(&n.Body) case ir.OFOR: n := n.(*ir.ForStmt) deadcodeslice(&n.Body) @@ -2233,10 +2228,14 @@ func deadcodeslice(nn *ir.Nodes) { deadcodeslice(&n.Body) case ir.OSELECT: n := n.(*ir.SelectStmt) - deadcodeslice(&n.Cases) + for _, cas := range n.Cases { + deadcodeslice(&cas.Body) + } case ir.OSWITCH: n := n.(*ir.SwitchStmt) - deadcodeslice(&n.Cases) + for _, cas := range n.Cases { + deadcodeslice(&cas.Body) + } } if cut { diff --git a/src/cmd/compile/internal/walk/order.go b/src/cmd/compile/internal/walk/order.go index 1e41cfc6aa..ebbd467570 100644 --- a/src/cmd/compile/internal/walk/order.go +++ b/src/cmd/compile/internal/walk/order.go @@ -914,7 +914,6 @@ func (o *orderState) stmt(n ir.Node) { n := n.(*ir.SelectStmt) t := o.markTemp() for _, ncas := range n.Cases { - ncas := ncas.(*ir.CaseStmt) r := ncas.Comm ir.SetPos(ncas) @@ -996,7 +995,6 @@ func (o *orderState) stmt(n ir.Node) { // Also insert any ninit queued during the previous loop. // (The temporary cleaning must follow that ninit work.) for _, cas := range n.Cases { - cas := cas.(*ir.CaseStmt) orderBlock(&cas.Body, o.free) cas.Body.Prepend(o.cleanTempNoPop(t)...) @@ -1036,13 +1034,12 @@ func (o *orderState) stmt(n ir.Node) { n := n.(*ir.SwitchStmt) if base.Debug.Libfuzzer != 0 && !hasDefaultCase(n) { // Add empty "default:" case for instrumentation. - n.Cases.Append(ir.NewCaseStmt(base.Pos, nil, nil)) + n.Cases = append(n.Cases, ir.NewCaseStmt(base.Pos, nil, nil)) } t := o.markTemp() n.Tag = o.expr(n.Tag, nil) for _, ncas := range n.Cases { - ncas := ncas.(*ir.CaseStmt) o.exprListInPlace(ncas.List) orderBlock(&ncas.Body, o.free) } @@ -1056,7 +1053,6 @@ func (o *orderState) stmt(n ir.Node) { func hasDefaultCase(n *ir.SwitchStmt) bool { for _, ncas := range n.Cases { - ncas := ncas.(*ir.CaseStmt) if len(ncas.List) == 0 { return true } diff --git a/src/cmd/compile/internal/walk/select.go b/src/cmd/compile/internal/walk/select.go index 5e03732169..0b7e7e99fb 100644 --- a/src/cmd/compile/internal/walk/select.go +++ b/src/cmd/compile/internal/walk/select.go @@ -21,7 +21,7 @@ func walkSelect(sel *ir.SelectStmt) { sel.PtrInit().Set(nil) init = append(init, walkSelectCases(sel.Cases)...) - sel.Cases = ir.Nodes{} + sel.Cases = nil sel.Compiled.Set(init) walkStmtList(sel.Compiled) @@ -29,7 +29,7 @@ func walkSelect(sel *ir.SelectStmt) { base.Pos = lno } -func walkSelectCases(cases ir.Nodes) []ir.Node { +func walkSelectCases(cases []*ir.CaseStmt) []ir.Node { ncas := len(cases) sellineno := base.Pos @@ -40,7 +40,7 @@ func walkSelectCases(cases ir.Nodes) []ir.Node { // optimization: one-case select: single op. if ncas == 1 { - cas := cases[0].(*ir.CaseStmt) + cas := cases[0] ir.SetPos(cas) l := cas.Init() if cas.Comm != nil { // not default: @@ -75,7 +75,6 @@ func walkSelectCases(cases ir.Nodes) []ir.Node { // this rewrite is used by both the general code and the next optimization. var dflt *ir.CaseStmt for _, cas := range cases { - cas := cas.(*ir.CaseStmt) ir.SetPos(cas) n := cas.Comm if n == nil { @@ -99,9 +98,9 @@ func walkSelectCases(cases ir.Nodes) []ir.Node { // optimization: two-case select but one is default: single non-blocking op. if ncas == 2 && dflt != nil { - cas := cases[0].(*ir.CaseStmt) + cas := cases[0] if cas == dflt { - cas = cases[1].(*ir.CaseStmt) + cas = cases[1] } n := cas.Comm @@ -170,7 +169,6 @@ func walkSelectCases(cases ir.Nodes) []ir.Node { // register cases for _, cas := range cases { - cas := cas.(*ir.CaseStmt) ir.SetPos(cas) init = append(init, cas.Init()...) diff --git a/src/cmd/compile/internal/walk/switch.go b/src/cmd/compile/internal/walk/switch.go index 141d2e5e05..de0b471b34 100644 --- a/src/cmd/compile/internal/walk/switch.go +++ b/src/cmd/compile/internal/walk/switch.go @@ -71,7 +71,6 @@ func walkSwitchExpr(sw *ir.SwitchStmt) { var defaultGoto ir.Node var body ir.Nodes for _, ncase := range sw.Cases { - ncase := ncase.(*ir.CaseStmt) label := typecheck.AutoLabel(".s") jmp := ir.NewBranchStmt(ncase.Pos(), ir.OGOTO, label) @@ -96,7 +95,7 @@ func walkSwitchExpr(sw *ir.SwitchStmt) { body.Append(br) } } - sw.Cases.Set(nil) + sw.Cases = nil if defaultGoto == nil { br := ir.NewBranchStmt(base.Pos, ir.OBREAK, nil) @@ -259,7 +258,6 @@ func allCaseExprsAreSideEffectFree(sw *ir.SwitchStmt) bool { // enough. for _, ncase := range sw.Cases { - ncase := ncase.(*ir.CaseStmt) for _, v := range ncase.List { if v.Op() != ir.OLITERAL { return false @@ -325,7 +323,6 @@ func walkSwitchType(sw *ir.SwitchStmt) { var defaultGoto, nilGoto ir.Node var body ir.Nodes for _, ncase := range sw.Cases { - ncase := ncase.(*ir.CaseStmt) caseVar := ncase.Var // For single-type cases with an interface type, @@ -384,7 +381,7 @@ func walkSwitchType(sw *ir.SwitchStmt) { body.Append(ncase.Body...) body.Append(br) } - sw.Cases.Set(nil) + sw.Cases = nil if defaultGoto == nil { defaultGoto = br -- cgit v1.3-5-g9baa From 2ecf52b841cd48e76df1fe721d29a972c22bf93f Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sat, 26 Dec 2020 22:42:17 -0800 Subject: [dev.regabi] cmd/compile: separate CommStmt from CaseStmt Like go/ast and cmd/compile/internal/syntax before it, package ir now has separate concrete representations for switch-case clauses and select-communication clauses. Passes toolstash -cmp. Change-Id: I32667cbae251fe7881be0f434388478433b2414f Reviewed-on: https://go-review.googlesource.com/c/go/+/280443 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/ir/mknode.go | 7 +++ src/cmd/compile/internal/ir/node_gen.go | 31 +++++++++++--- src/cmd/compile/internal/ir/stmt.go | 62 +++++++++++++++++++++------ src/cmd/compile/internal/noder/noder.go | 6 +-- src/cmd/compile/internal/typecheck/iexport.go | 11 ++++- src/cmd/compile/internal/typecheck/iimport.go | 10 ++++- src/cmd/compile/internal/walk/select.go | 8 ++-- 7 files changed, 109 insertions(+), 26 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/ir/mknode.go b/src/cmd/compile/internal/ir/mknode.go index edf3ee501c..bc6fa3cd30 100644 --- a/src/cmd/compile/internal/ir/mknode.go +++ b/src/cmd/compile/internal/ir/mknode.go @@ -38,6 +38,7 @@ func main() { ntypeType := lookup("Ntype") nodesType := lookup("Nodes") slicePtrCaseStmtType := types.NewSlice(types.NewPointer(lookup("CaseStmt"))) + slicePtrCommStmtType := types.NewSlice(types.NewPointer(lookup("CommStmt"))) ptrFieldType := types.NewPointer(lookup("Field")) slicePtrFieldType := types.NewSlice(ptrFieldType) ptrIdentType := types.NewPointer(lookup("Ident")) @@ -79,6 +80,8 @@ func main() { fmt.Fprintf(&buf, "c.%s = c.%s.Copy()\n", name, name) case is(slicePtrCaseStmtType): fmt.Fprintf(&buf, "c.%s = copyCases(c.%s)\n", name, name) + case is(slicePtrCommStmtType): + fmt.Fprintf(&buf, "c.%s = copyComms(c.%s)\n", name, name) case is(ptrFieldType): fmt.Fprintf(&buf, "if c.%s != nil { c.%s = c.%s.copy() }\n", name, name, name) case is(slicePtrFieldType): @@ -99,6 +102,8 @@ func main() { fmt.Fprintf(&buf, "err = maybeDoList(n.%s, err, do)\n", name) case is(slicePtrCaseStmtType): fmt.Fprintf(&buf, "err = maybeDoCases(n.%s, err, do)\n", name) + case is(slicePtrCommStmtType): + fmt.Fprintf(&buf, "err = maybeDoComms(n.%s, err, do)\n", name) case is(ptrFieldType): fmt.Fprintf(&buf, "err = maybeDoField(n.%s, err, do)\n", name) case is(slicePtrFieldType): @@ -120,6 +125,8 @@ func main() { fmt.Fprintf(&buf, "editList(n.%s, edit)\n", name) case is(slicePtrCaseStmtType): fmt.Fprintf(&buf, "editCases(n.%s, edit)\n", name) + case is(slicePtrCommStmtType): + fmt.Fprintf(&buf, "editComms(n.%s, edit)\n", name) case is(ptrFieldType): fmt.Fprintf(&buf, "editField(n.%s, edit)\n", name) case is(slicePtrFieldType): diff --git a/src/cmd/compile/internal/ir/node_gen.go b/src/cmd/compile/internal/ir/node_gen.go index 041855bbe9..5796544b48 100644 --- a/src/cmd/compile/internal/ir/node_gen.go +++ b/src/cmd/compile/internal/ir/node_gen.go @@ -239,7 +239,6 @@ func (n *CaseStmt) doChildren(do func(Node) error) error { err = maybeDoList(n.init, err, do) err = maybeDo(n.Var, err, do) err = maybeDoList(n.List, err, do) - err = maybeDo(n.Comm, err, do) err = maybeDoList(n.Body, err, do) return err } @@ -247,7 +246,6 @@ func (n *CaseStmt) editChildren(edit func(Node) Node) { editList(n.init, edit) n.Var = maybeEdit(n.Var, edit) editList(n.List, edit) - n.Comm = maybeEdit(n.Comm, edit) editList(n.Body, edit) } @@ -295,6 +293,29 @@ func (n *ClosureReadExpr) editChildren(edit func(Node) Node) { editList(n.init, edit) } +func (n *CommStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } +func (n *CommStmt) copy() Node { + c := *n + c.init = c.init.Copy() + c.List = c.List.Copy() + c.Body = c.Body.Copy() + return &c +} +func (n *CommStmt) doChildren(do func(Node) error) error { + var err error + err = maybeDoList(n.init, err, do) + err = maybeDoList(n.List, err, do) + err = maybeDo(n.Comm, err, do) + err = maybeDoList(n.Body, err, do) + return err +} +func (n *CommStmt) editChildren(edit func(Node) Node) { + editList(n.init, edit) + editList(n.List, edit) + n.Comm = maybeEdit(n.Comm, edit) + editList(n.Body, edit) +} + func (n *CompLitExpr) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *CompLitExpr) copy() Node { c := *n @@ -781,20 +802,20 @@ func (n *SelectStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *SelectStmt) copy() Node { c := *n c.init = c.init.Copy() - c.Cases = copyCases(c.Cases) + c.Cases = copyComms(c.Cases) c.Compiled = c.Compiled.Copy() return &c } func (n *SelectStmt) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) - err = maybeDoCases(n.Cases, err, do) + err = maybeDoComms(n.Cases, err, do) err = maybeDoList(n.Compiled, err, do) return err } func (n *SelectStmt) editChildren(edit func(Node) Node) { editList(n.init, edit) - editCases(n.Cases, edit) + editComms(n.Cases, edit) editList(n.Compiled, edit) } diff --git a/src/cmd/compile/internal/ir/stmt.go b/src/cmd/compile/internal/ir/stmt.go index ce775a8529..181a0fd582 100644 --- a/src/cmd/compile/internal/ir/stmt.go +++ b/src/cmd/compile/internal/ir/stmt.go @@ -178,19 +178,17 @@ type CaseStmt struct { miniStmt Var Node // declared variable for this case in type switch List Nodes // list of expressions for switch, early select - Comm Node // communication case (Exprs[0]) after select is type-checked Body Nodes } func NewCaseStmt(pos src.XPos, list, body []Node) *CaseStmt { - n := &CaseStmt{} + n := &CaseStmt{List: list, Body: body} n.pos = pos n.op = OCASE - n.List.Set(list) - n.Body.Set(body) return n } +// TODO(mdempsky): Generate these with mknode.go. func copyCases(list []*CaseStmt) []*CaseStmt { if list == nil { return nil @@ -199,7 +197,6 @@ func copyCases(list []*CaseStmt) []*CaseStmt { copy(c, list) return c } - func maybeDoCases(list []*CaseStmt, err error, do func(Node) error) error { if err != nil { return err @@ -213,7 +210,6 @@ func maybeDoCases(list []*CaseStmt, err error, do func(Node) error) error { } return nil } - func editCases(list []*CaseStmt, edit func(Node) Node) { for i, x := range list { if x != nil { @@ -222,6 +218,50 @@ func editCases(list []*CaseStmt, edit func(Node) Node) { } } +type CommStmt struct { + miniStmt + List Nodes // list of expressions for switch, early select + Comm Node // communication case (Exprs[0]) after select is type-checked + Body Nodes +} + +func NewCommStmt(pos src.XPos, list, body []Node) *CommStmt { + n := &CommStmt{List: list, Body: body} + n.pos = pos + n.op = OCASE + return n +} + +// TODO(mdempsky): Generate these with mknode.go. +func copyComms(list []*CommStmt) []*CommStmt { + if list == nil { + return nil + } + c := make([]*CommStmt, len(list)) + copy(c, list) + return c +} +func maybeDoComms(list []*CommStmt, err error, do func(Node) error) error { + if err != nil { + return err + } + for _, x := range list { + if x != nil { + if err := do(x); err != nil { + return err + } + } + } + return nil +} +func editComms(list []*CommStmt, edit func(Node) Node) { + for i, x := range list { + if x != nil { + list[i] = edit(x).(*CommStmt) + } + } +} + // A ForStmt is a non-range for loop: for Init; Cond; Post { Body } // Op can be OFOR or OFORUNTIL (!Cond). type ForStmt struct { @@ -365,18 +405,17 @@ func (n *ReturnStmt) SetOrig(x Node) { n.orig = x } type SelectStmt struct { miniStmt Label *types.Sym - Cases []*CaseStmt + Cases []*CommStmt HasBreak bool // TODO(rsc): Instead of recording here, replace with a block? Compiled Nodes // compiled form, after walkswitch } -func NewSelectStmt(pos src.XPos, cases []*CaseStmt) *SelectStmt { - n := &SelectStmt{} +func NewSelectStmt(pos src.XPos, cases []*CommStmt) *SelectStmt { + n := &SelectStmt{Cases: cases} n.pos = pos n.op = OSELECT - n.Cases = cases return n } @@ -407,10 +446,9 @@ type SwitchStmt struct { } func NewSwitchStmt(pos src.XPos, tag Node, cases []*CaseStmt) *SwitchStmt { - n := &SwitchStmt{Tag: tag} + n := &SwitchStmt{Tag: tag, Cases: cases} n.pos = pos n.op = OSWITCH - n.Cases = cases return n } diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index b974448338..ff699cd54d 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -1266,8 +1266,8 @@ func (p *noder) simpleStmt(stmt syntax.SimpleStmt) []ir.Node { return []ir.Node{p.stmt(stmt)} } -func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*ir.CaseStmt { - nodes := make([]*ir.CaseStmt, len(clauses)) +func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*ir.CommStmt { + nodes := make([]*ir.CommStmt, len(clauses)) for i, clause := range clauses { p.setlineno(clause) if i > 0 { @@ -1275,7 +1275,7 @@ func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []* } p.openScope(clause.Pos()) - nodes[i] = ir.NewCaseStmt(p.pos(clause), p.simpleStmt(clause.Comm), p.stmts(clause.Body)) + nodes[i] = ir.NewCommStmt(p.pos(clause), p.simpleStmt(clause.Comm), p.stmts(clause.Body)) } if len(clauses) > 0 { p.closeScope(rbrace) diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index 19437a069e..ef2c4527a9 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1144,7 +1144,7 @@ func (w *exportWriter) stmt(n ir.Node) { w.op(n.Op()) w.pos(n.Pos()) w.stmtList(n.Init()) - w.caseList(n.Cases, false) + w.commList(n.Cases) case ir.OSWITCH: n := n.(*ir.SwitchStmt) @@ -1193,6 +1193,15 @@ func (w *exportWriter) caseList(cases []*ir.CaseStmt, namedTypeSwitch bool) { } } +func (w *exportWriter) commList(cases []*ir.CommStmt) { + w.uint64(uint64(len(cases))) + for _, cas := range cases { + w.pos(cas.Pos()) + w.stmtList(cas.List) + w.stmtList(cas.Body) + } +} + func (w *exportWriter) exprList(list ir.Nodes) { for _, n := range list { w.expr(n) diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index fd8314b662..ba7ea2f156 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -789,6 +789,14 @@ func (r *importReader) caseList(switchExpr ir.Node) []*ir.CaseStmt { return cases } +func (r *importReader) commList() []*ir.CommStmt { + cases := make([]*ir.CommStmt, r.uint64()) + for i := range cases { + cases[i] = ir.NewCommStmt(r.pos(), r.stmtList(), r.stmtList()) + } + return cases +} + func (r *importReader) exprList() []ir.Node { var list []ir.Node for { @@ -1035,7 +1043,7 @@ func (r *importReader) node() ir.Node { case ir.OSELECT: pos := r.pos() init := r.stmtList() - n := ir.NewSelectStmt(pos, r.caseList(nil)) + n := ir.NewSelectStmt(pos, r.commList()) n.PtrInit().Set(init) return n diff --git a/src/cmd/compile/internal/walk/select.go b/src/cmd/compile/internal/walk/select.go index 0b7e7e99fb..f51684c9b6 100644 --- a/src/cmd/compile/internal/walk/select.go +++ b/src/cmd/compile/internal/walk/select.go @@ -29,7 +29,7 @@ func walkSelect(sel *ir.SelectStmt) { base.Pos = lno } -func walkSelectCases(cases []*ir.CaseStmt) []ir.Node { +func walkSelectCases(cases []*ir.CommStmt) []ir.Node { ncas := len(cases) sellineno := base.Pos @@ -73,7 +73,7 @@ func walkSelectCases(cases []*ir.CaseStmt) []ir.Node { // convert case value arguments to addresses. // this rewrite is used by both the general code and the next optimization. - var dflt *ir.CaseStmt + var dflt *ir.CommStmt for _, cas := range cases { ir.SetPos(cas) n := cas.Comm @@ -146,7 +146,7 @@ func walkSelectCases(cases []*ir.CaseStmt) []ir.Node { if dflt != nil { ncas-- } - casorder := make([]*ir.CaseStmt, ncas) + casorder := make([]*ir.CommStmt, ncas) nsends, nrecvs := 0, 0 var init []ir.Node @@ -242,7 +242,7 @@ func walkSelectCases(cases []*ir.CaseStmt) []ir.Node { } // dispatch cases - dispatch := func(cond ir.Node, cas *ir.CaseStmt) { + dispatch := func(cond ir.Node, cas *ir.CommStmt) { cond = typecheck.Expr(cond) cond = typecheck.DefaultLit(cond, nil) -- cgit v1.3-5-g9baa From 3bdafb0d82c9908ae04d2765847754df0646df35 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sat, 26 Dec 2020 23:03:25 -0800 Subject: [dev.regabi] cmd/compile: remove CommStmt.List Package syntax's parser already ensures that select communication clauses only have one statement, so there's no need for ir's CommStmt to need to represent more than one. Instead, noder can just directly populate Comm in the first place. Incidentally, this also revealed a latent issue in the inline-body exporter: we were exporting List (where the case statement is before type-checking), rather than Comm (where the case statement would be after type-checking, when export happens). Passes toolstash -cmp. Change-Id: Ib4eb711527bed297c7332c79ed6e6562a1db2cfa Reviewed-on: https://go-review.googlesource.com/c/go/+/280444 Trust: Matthew Dempsky Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/ir/node_gen.go | 3 --- src/cmd/compile/internal/ir/stmt.go | 13 +++++++------ src/cmd/compile/internal/noder/noder.go | 23 ++++++++++------------- src/cmd/compile/internal/typecheck/iexport.go | 2 +- src/cmd/compile/internal/typecheck/iimport.go | 6 ++++-- src/cmd/compile/internal/typecheck/stmt.go | 18 ++++++------------ 6 files changed, 28 insertions(+), 37 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/ir/node_gen.go b/src/cmd/compile/internal/ir/node_gen.go index 5796544b48..7412969425 100644 --- a/src/cmd/compile/internal/ir/node_gen.go +++ b/src/cmd/compile/internal/ir/node_gen.go @@ -297,21 +297,18 @@ func (n *CommStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } func (n *CommStmt) copy() Node { c := *n c.init = c.init.Copy() - c.List = c.List.Copy() c.Body = c.Body.Copy() return &c } func (n *CommStmt) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) - err = maybeDoList(n.List, err, do) err = maybeDo(n.Comm, err, do) err = maybeDoList(n.Body, err, do) return err } func (n *CommStmt) editChildren(edit func(Node) Node) { editList(n.init, edit) - editList(n.List, edit) n.Comm = maybeEdit(n.Comm, edit) editList(n.Body, edit) } diff --git a/src/cmd/compile/internal/ir/stmt.go b/src/cmd/compile/internal/ir/stmt.go index 181a0fd582..0f44acd8b4 100644 --- a/src/cmd/compile/internal/ir/stmt.go +++ b/src/cmd/compile/internal/ir/stmt.go @@ -220,13 +220,12 @@ func editCases(list []*CaseStmt, edit func(Node) Node) { type CommStmt struct { miniStmt - List Nodes // list of expressions for switch, early select - Comm Node // communication case (Exprs[0]) after select is type-checked + Comm Node // communication case Body Nodes } -func NewCommStmt(pos src.XPos, list, body []Node) *CommStmt { - n := &CommStmt{List: list, Body: body} +func NewCommStmt(pos src.XPos, comm Node, body []Node) *CommStmt { + n := &CommStmt{Comm: comm, Body: body} n.pos = pos n.op = OCASE return n @@ -274,11 +273,13 @@ type ForStmt struct { HasBreak bool } -func NewForStmt(pos src.XPos, init []Node, cond, post Node, body []Node) *ForStmt { +func NewForStmt(pos src.XPos, init Node, cond, post Node, body []Node) *ForStmt { n := &ForStmt{Cond: cond, Post: post} n.pos = pos n.op = OFOR - n.init.Set(init) + if init != nil { + n.init = []Node{init} + } n.Body.Set(body) return n } diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index ff699cd54d..19a88e21a2 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -1149,9 +1149,11 @@ func (p *noder) blockStmt(stmt *syntax.BlockStmt) []ir.Node { func (p *noder) ifStmt(stmt *syntax.IfStmt) ir.Node { p.openScope(stmt.Pos()) - init := p.simpleStmt(stmt.Init) + init := p.stmt(stmt.Init) n := ir.NewIfStmt(p.pos(stmt), p.expr(stmt.Cond), p.blockStmt(stmt.Then), nil) - *n.PtrInit() = init + if init != nil { + *n.PtrInit() = []ir.Node{init} + } if stmt.Else != nil { e := p.stmt(stmt.Else) if e.Op() == ir.OBLOCK { @@ -1186,7 +1188,7 @@ func (p *noder) forStmt(stmt *syntax.ForStmt) ir.Node { return n } - n := ir.NewForStmt(p.pos(stmt), p.simpleStmt(stmt.Init), p.expr(stmt.Cond), p.stmt(stmt.Post), p.blockStmt(stmt.Body)) + n := ir.NewForStmt(p.pos(stmt), p.stmt(stmt.Init), p.expr(stmt.Cond), p.stmt(stmt.Post), p.blockStmt(stmt.Body)) p.closeAnotherScope() return n } @@ -1194,9 +1196,11 @@ func (p *noder) forStmt(stmt *syntax.ForStmt) ir.Node { func (p *noder) switchStmt(stmt *syntax.SwitchStmt) ir.Node { p.openScope(stmt.Pos()) - init := p.simpleStmt(stmt.Init) + init := p.stmt(stmt.Init) n := ir.NewSwitchStmt(p.pos(stmt), p.expr(stmt.Tag), nil) - *n.PtrInit() = init + if init != nil { + *n.PtrInit() = []ir.Node{init} + } var tswitch *ir.TypeSwitchGuard if l := n.Tag; l != nil && l.Op() == ir.OTYPESW { @@ -1259,13 +1263,6 @@ func (p *noder) selectStmt(stmt *syntax.SelectStmt) ir.Node { return ir.NewSelectStmt(p.pos(stmt), p.commClauses(stmt.Body, stmt.Rbrace)) } -func (p *noder) simpleStmt(stmt syntax.SimpleStmt) []ir.Node { - if stmt == nil { - return nil - } - return []ir.Node{p.stmt(stmt)} -} - func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*ir.CommStmt { nodes := make([]*ir.CommStmt, len(clauses)) for i, clause := range clauses { @@ -1275,7 +1272,7 @@ func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []* } p.openScope(clause.Pos()) - nodes[i] = ir.NewCommStmt(p.pos(clause), p.simpleStmt(clause.Comm), p.stmts(clause.Body)) + nodes[i] = ir.NewCommStmt(p.pos(clause), p.stmt(clause.Comm), p.stmts(clause.Body)) } if len(clauses) > 0 { p.closeScope(rbrace) diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index ef2c4527a9..bf093c60c7 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1197,7 +1197,7 @@ func (w *exportWriter) commList(cases []*ir.CommStmt) { w.uint64(uint64(len(cases))) for _, cas := range cases { w.pos(cas.Pos()) - w.stmtList(cas.List) + w.node(cas.Comm) w.stmtList(cas.Body) } } diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index ba7ea2f156..af2dd84a38 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -792,7 +792,7 @@ func (r *importReader) caseList(switchExpr ir.Node) []*ir.CaseStmt { func (r *importReader) commList() []*ir.CommStmt { cases := make([]*ir.CommStmt, r.uint64()) for i := range cases { - cases[i] = ir.NewCommStmt(r.pos(), r.stmtList(), r.stmtList()) + cases[i] = ir.NewCommStmt(r.pos(), r.node(), r.stmtList()) } return cases } @@ -1033,7 +1033,9 @@ func (r *importReader) node() ir.Node { case ir.OFOR: pos, init := r.pos(), r.stmtList() cond, post := r.exprsOrNil() - return ir.NewForStmt(pos, init, cond, post, r.stmtList()) + n := ir.NewForStmt(pos, nil, cond, post, r.stmtList()) + n.PtrInit().Set(init) + return n case ir.ORANGE: pos := r.pos() diff --git a/src/cmd/compile/internal/typecheck/stmt.go b/src/cmd/compile/internal/typecheck/stmt.go index 03c3e399eb..bfeea06e83 100644 --- a/src/cmd/compile/internal/typecheck/stmt.go +++ b/src/cmd/compile/internal/typecheck/stmt.go @@ -360,29 +360,23 @@ func tcReturn(n *ir.ReturnStmt) ir.Node { // select func tcSelect(sel *ir.SelectStmt) { - var def ir.Node + var def *ir.CommStmt lno := ir.SetPos(sel) Stmts(sel.Init()) for _, ncase := range sel.Cases { - if len(ncase.List) == 0 { + if ncase.Comm == nil { // default if def != nil { base.ErrorfAt(ncase.Pos(), "multiple defaults in select (first at %v)", ir.Line(def)) } else { def = ncase } - } else if len(ncase.List) > 1 { - base.ErrorfAt(ncase.Pos(), "select cases cannot be lists") } else { - ncase.List[0] = Stmt(ncase.List[0]) - n := ncase.List[0] + n := Stmt(ncase.Comm) ncase.Comm = n - ncase.List.Set(nil) - oselrecv2 := func(dst, recv ir.Node, colas bool) { - n := ir.NewAssignListStmt(n.Pos(), ir.OSELRECV2, nil, nil) - n.Lhs = []ir.Node{dst, ir.BlankNode} - n.Rhs = []ir.Node{recv} - n.Def = colas + oselrecv2 := func(dst, recv ir.Node, def bool) { + n := ir.NewAssignListStmt(n.Pos(), ir.OSELRECV2, []ir.Node{dst, ir.BlankNode}, []ir.Node{recv}) + n.Def = def n.SetTypecheck(1) ncase.Comm = n } -- cgit v1.3-5-g9baa From f8afb8216ad69ed0c4e5ac8b5ad86cc0cb78749d Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sat, 26 Dec 2020 23:21:20 -0800 Subject: [dev.regabi] cmd/compile: rename CommStmt and CaseStmt [generated] Rename these two AST nodes to match their cmd/compile/internal/syntax and go/ast counterparts. Passes toolstash -cmp. [git-generate] cd src/cmd/compile/internal/ir rf ' mv CaseStmt CaseClause mv CommStmt CommClause ' sed -E -i -e 's/(Case|Comm)Stmt/\1Clause/g' mknode.go Change-Id: I19fba0323a5de1e71346622857011b2f7879bcef Reviewed-on: https://go-review.googlesource.com/c/go/+/280446 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/ir/fmt.go | 2 +- src/cmd/compile/internal/ir/mknode.go | 16 +++++----- src/cmd/compile/internal/ir/node_gen.go | 16 +++++----- src/cmd/compile/internal/ir/stmt.go | 44 +++++++++++++-------------- src/cmd/compile/internal/noder/noder.go | 8 ++--- src/cmd/compile/internal/typecheck/iexport.go | 4 +-- src/cmd/compile/internal/typecheck/iimport.go | 8 ++--- src/cmd/compile/internal/typecheck/stmt.go | 2 +- src/cmd/compile/internal/walk/select.go | 8 ++--- 9 files changed, 54 insertions(+), 54 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/ir/fmt.go b/src/cmd/compile/internal/ir/fmt.go index f52c639c51..49f451a5d8 100644 --- a/src/cmd/compile/internal/ir/fmt.go +++ b/src/cmd/compile/internal/ir/fmt.go @@ -478,7 +478,7 @@ func stmtFmt(n Node, s fmt.State) { fmt.Fprintf(s, " { %v }", n.Cases) case OCASE: - n := n.(*CaseStmt) + n := n.(*CaseClause) if len(n.List) != 0 { fmt.Fprintf(s, "case %.v", n.List) } else { diff --git a/src/cmd/compile/internal/ir/mknode.go b/src/cmd/compile/internal/ir/mknode.go index 5c36b729c7..3b5da32d8c 100644 --- a/src/cmd/compile/internal/ir/mknode.go +++ b/src/cmd/compile/internal/ir/mknode.go @@ -37,8 +37,8 @@ func main() { nodeType := lookup("Node") ntypeType := lookup("Ntype") nodesType := lookup("Nodes") - slicePtrCaseStmtType := types.NewSlice(types.NewPointer(lookup("CaseStmt"))) - slicePtrCommStmtType := types.NewSlice(types.NewPointer(lookup("CommStmt"))) + slicePtrCaseClauseType := types.NewSlice(types.NewPointer(lookup("CaseClause"))) + slicePtrCommClauseType := types.NewSlice(types.NewPointer(lookup("CommClause"))) ptrFieldType := types.NewPointer(lookup("Field")) slicePtrFieldType := types.NewSlice(ptrFieldType) ptrIdentType := types.NewPointer(lookup("Ident")) @@ -78,9 +78,9 @@ func main() { switch { case is(nodesType): fmt.Fprintf(&buf, "c.%s = c.%s.Copy()\n", name, name) - case is(slicePtrCaseStmtType): + case is(slicePtrCaseClauseType): fmt.Fprintf(&buf, "c.%s = copyCases(c.%s)\n", name, name) - case is(slicePtrCommStmtType): + case is(slicePtrCommClauseType): fmt.Fprintf(&buf, "c.%s = copyComms(c.%s)\n", name, name) case is(ptrFieldType): fmt.Fprintf(&buf, "if c.%s != nil { c.%s = c.%s.copy() }\n", name, name, name) @@ -100,9 +100,9 @@ func main() { fmt.Fprintf(&buf, "err = maybeDo(n.%s, err, do)\n", name) case is(nodesType): fmt.Fprintf(&buf, "err = maybeDoList(n.%s, err, do)\n", name) - case is(slicePtrCaseStmtType): + case is(slicePtrCaseClauseType): fmt.Fprintf(&buf, "err = maybeDoCases(n.%s, err, do)\n", name) - case is(slicePtrCommStmtType): + case is(slicePtrCommClauseType): fmt.Fprintf(&buf, "err = maybeDoComms(n.%s, err, do)\n", name) case is(ptrFieldType): fmt.Fprintf(&buf, "err = maybeDoField(n.%s, err, do)\n", name) @@ -123,9 +123,9 @@ func main() { fmt.Fprintf(&buf, "n.%s = toNtype(maybeEdit(n.%s, edit))\n", name, name) case is(nodesType): fmt.Fprintf(&buf, "editList(n.%s, edit)\n", name) - case is(slicePtrCaseStmtType): + case is(slicePtrCaseClauseType): fmt.Fprintf(&buf, "editCases(n.%s, edit)\n", name) - case is(slicePtrCommStmtType): + case is(slicePtrCommClauseType): fmt.Fprintf(&buf, "editComms(n.%s, edit)\n", name) case is(ptrFieldType): fmt.Fprintf(&buf, "editField(n.%s, edit)\n", name) diff --git a/src/cmd/compile/internal/ir/node_gen.go b/src/cmd/compile/internal/ir/node_gen.go index 7412969425..27a5311748 100644 --- a/src/cmd/compile/internal/ir/node_gen.go +++ b/src/cmd/compile/internal/ir/node_gen.go @@ -226,15 +226,15 @@ func (n *CallPartExpr) editChildren(edit func(Node) Node) { n.X = maybeEdit(n.X, edit) } -func (n *CaseStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } -func (n *CaseStmt) copy() Node { +func (n *CaseClause) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } +func (n *CaseClause) copy() Node { c := *n c.init = c.init.Copy() c.List = c.List.Copy() c.Body = c.Body.Copy() return &c } -func (n *CaseStmt) doChildren(do func(Node) error) error { +func (n *CaseClause) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) err = maybeDo(n.Var, err, do) @@ -242,7 +242,7 @@ func (n *CaseStmt) doChildren(do func(Node) error) error { err = maybeDoList(n.Body, err, do) return err } -func (n *CaseStmt) editChildren(edit func(Node) Node) { +func (n *CaseClause) editChildren(edit func(Node) Node) { editList(n.init, edit) n.Var = maybeEdit(n.Var, edit) editList(n.List, edit) @@ -293,21 +293,21 @@ func (n *ClosureReadExpr) editChildren(edit func(Node) Node) { editList(n.init, edit) } -func (n *CommStmt) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } -func (n *CommStmt) copy() Node { +func (n *CommClause) Format(s fmt.State, verb rune) { FmtNode(n, s, verb) } +func (n *CommClause) copy() Node { c := *n c.init = c.init.Copy() c.Body = c.Body.Copy() return &c } -func (n *CommStmt) doChildren(do func(Node) error) error { +func (n *CommClause) doChildren(do func(Node) error) error { var err error err = maybeDoList(n.init, err, do) err = maybeDo(n.Comm, err, do) err = maybeDoList(n.Body, err, do) return err } -func (n *CommStmt) editChildren(edit func(Node) Node) { +func (n *CommClause) editChildren(edit func(Node) Node) { editList(n.init, edit) n.Comm = maybeEdit(n.Comm, edit) editList(n.Body, edit) diff --git a/src/cmd/compile/internal/ir/stmt.go b/src/cmd/compile/internal/ir/stmt.go index 0f44acd8b4..de152fec72 100644 --- a/src/cmd/compile/internal/ir/stmt.go +++ b/src/cmd/compile/internal/ir/stmt.go @@ -173,31 +173,31 @@ func NewBranchStmt(pos src.XPos, op Op, label *types.Sym) *BranchStmt { func (n *BranchStmt) Sym() *types.Sym { return n.Label } -// A CaseStmt is a case statement in a switch or select: case List: Body. -type CaseStmt struct { +// A CaseClause is a case statement in a switch or select: case List: Body. +type CaseClause struct { miniStmt Var Node // declared variable for this case in type switch List Nodes // list of expressions for switch, early select Body Nodes } -func NewCaseStmt(pos src.XPos, list, body []Node) *CaseStmt { - n := &CaseStmt{List: list, Body: body} +func NewCaseStmt(pos src.XPos, list, body []Node) *CaseClause { + n := &CaseClause{List: list, Body: body} n.pos = pos n.op = OCASE return n } // TODO(mdempsky): Generate these with mknode.go. -func copyCases(list []*CaseStmt) []*CaseStmt { +func copyCases(list []*CaseClause) []*CaseClause { if list == nil { return nil } - c := make([]*CaseStmt, len(list)) + c := make([]*CaseClause, len(list)) copy(c, list) return c } -func maybeDoCases(list []*CaseStmt, err error, do func(Node) error) error { +func maybeDoCases(list []*CaseClause, err error, do func(Node) error) error { if err != nil { return err } @@ -210,37 +210,37 @@ func maybeDoCases(list []*CaseStmt, err error, do func(Node) error) error { } return nil } -func editCases(list []*CaseStmt, edit func(Node) Node) { +func editCases(list []*CaseClause, edit func(Node) Node) { for i, x := range list { if x != nil { - list[i] = edit(x).(*CaseStmt) + list[i] = edit(x).(*CaseClause) } } } -type CommStmt struct { +type CommClause struct { miniStmt - Comm Node // communication case + Comm Node // communication case Body Nodes } -func NewCommStmt(pos src.XPos, comm Node, body []Node) *CommStmt { - n := &CommStmt{Comm: comm, Body: body} +func NewCommStmt(pos src.XPos, comm Node, body []Node) *CommClause { + n := &CommClause{Comm: comm, Body: body} n.pos = pos n.op = OCASE return n } // TODO(mdempsky): Generate these with mknode.go. -func copyComms(list []*CommStmt) []*CommStmt { +func copyComms(list []*CommClause) []*CommClause { if list == nil { return nil } - c := make([]*CommStmt, len(list)) + c := make([]*CommClause, len(list)) copy(c, list) return c } -func maybeDoComms(list []*CommStmt, err error, do func(Node) error) error { +func maybeDoComms(list []*CommClause, err error, do func(Node) error) error { if err != nil { return err } @@ -253,10 +253,10 @@ func maybeDoComms(list []*CommStmt, err error, do func(Node) error) error { } return nil } -func editComms(list []*CommStmt, edit func(Node) Node) { +func editComms(list []*CommClause, edit func(Node) Node) { for i, x := range list { if x != nil { - list[i] = edit(x).(*CommStmt) + list[i] = edit(x).(*CommClause) } } } @@ -406,14 +406,14 @@ func (n *ReturnStmt) SetOrig(x Node) { n.orig = x } type SelectStmt struct { miniStmt Label *types.Sym - Cases []*CommStmt + Cases []*CommClause HasBreak bool // TODO(rsc): Instead of recording here, replace with a block? Compiled Nodes // compiled form, after walkswitch } -func NewSelectStmt(pos src.XPos, cases []*CommStmt) *SelectStmt { +func NewSelectStmt(pos src.XPos, cases []*CommClause) *SelectStmt { n := &SelectStmt{Cases: cases} n.pos = pos n.op = OSELECT @@ -438,7 +438,7 @@ func NewSendStmt(pos src.XPos, ch, value Node) *SendStmt { type SwitchStmt struct { miniStmt Tag Node - Cases []*CaseStmt + Cases []*CaseClause Label *types.Sym HasBreak bool @@ -446,7 +446,7 @@ type SwitchStmt struct { Compiled Nodes // compiled form, after walkswitch } -func NewSwitchStmt(pos src.XPos, tag Node, cases []*CaseStmt) *SwitchStmt { +func NewSwitchStmt(pos src.XPos, tag Node, cases []*CaseClause) *SwitchStmt { n := &SwitchStmt{Tag: tag, Cases: cases} n.pos = pos n.op = OSWITCH diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index 19a88e21a2..7c1f7595b3 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -1212,8 +1212,8 @@ func (p *noder) switchStmt(stmt *syntax.SwitchStmt) ir.Node { return n } -func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitchGuard, rbrace syntax.Pos) []*ir.CaseStmt { - nodes := make([]*ir.CaseStmt, 0, len(clauses)) +func (p *noder) caseClauses(clauses []*syntax.CaseClause, tswitch *ir.TypeSwitchGuard, rbrace syntax.Pos) []*ir.CaseClause { + nodes := make([]*ir.CaseClause, 0, len(clauses)) for i, clause := range clauses { p.setlineno(clause) if i > 0 { @@ -1263,8 +1263,8 @@ func (p *noder) selectStmt(stmt *syntax.SelectStmt) ir.Node { return ir.NewSelectStmt(p.pos(stmt), p.commClauses(stmt.Body, stmt.Rbrace)) } -func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*ir.CommStmt { - nodes := make([]*ir.CommStmt, len(clauses)) +func (p *noder) commClauses(clauses []*syntax.CommClause, rbrace syntax.Pos) []*ir.CommClause { + nodes := make([]*ir.CommClause, len(clauses)) for i, clause := range clauses { p.setlineno(clause) if i > 0 { diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index bf093c60c7..3b071a61ab 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1181,7 +1181,7 @@ func isNamedTypeSwitch(x ir.Node) bool { return ok && guard.Tag != nil } -func (w *exportWriter) caseList(cases []*ir.CaseStmt, namedTypeSwitch bool) { +func (w *exportWriter) caseList(cases []*ir.CaseClause, namedTypeSwitch bool) { w.uint64(uint64(len(cases))) for _, cas := range cases { w.pos(cas.Pos()) @@ -1193,7 +1193,7 @@ func (w *exportWriter) caseList(cases []*ir.CaseStmt, namedTypeSwitch bool) { } } -func (w *exportWriter) commList(cases []*ir.CommStmt) { +func (w *exportWriter) commList(cases []*ir.CommClause) { w.uint64(uint64(len(cases))) for _, cas := range cases { w.pos(cas.Pos()) diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index af2dd84a38..cf2cf87492 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -767,10 +767,10 @@ func (r *importReader) stmtList() []ir.Node { return list } -func (r *importReader) caseList(switchExpr ir.Node) []*ir.CaseStmt { +func (r *importReader) caseList(switchExpr ir.Node) []*ir.CaseClause { namedTypeSwitch := isNamedTypeSwitch(switchExpr) - cases := make([]*ir.CaseStmt, r.uint64()) + cases := make([]*ir.CaseClause, r.uint64()) for i := range cases { cas := ir.NewCaseStmt(r.pos(), nil, nil) cas.List.Set(r.stmtList()) @@ -789,8 +789,8 @@ func (r *importReader) caseList(switchExpr ir.Node) []*ir.CaseStmt { return cases } -func (r *importReader) commList() []*ir.CommStmt { - cases := make([]*ir.CommStmt, r.uint64()) +func (r *importReader) commList() []*ir.CommClause { + cases := make([]*ir.CommClause, r.uint64()) for i := range cases { cases[i] = ir.NewCommStmt(r.pos(), r.node(), r.stmtList()) } diff --git a/src/cmd/compile/internal/typecheck/stmt.go b/src/cmd/compile/internal/typecheck/stmt.go index bfeea06e83..f5d36a663d 100644 --- a/src/cmd/compile/internal/typecheck/stmt.go +++ b/src/cmd/compile/internal/typecheck/stmt.go @@ -360,7 +360,7 @@ func tcReturn(n *ir.ReturnStmt) ir.Node { // select func tcSelect(sel *ir.SelectStmt) { - var def *ir.CommStmt + var def *ir.CommClause lno := ir.SetPos(sel) Stmts(sel.Init()) for _, ncase := range sel.Cases { diff --git a/src/cmd/compile/internal/walk/select.go b/src/cmd/compile/internal/walk/select.go index f51684c9b6..1c5e1d7e64 100644 --- a/src/cmd/compile/internal/walk/select.go +++ b/src/cmd/compile/internal/walk/select.go @@ -29,7 +29,7 @@ func walkSelect(sel *ir.SelectStmt) { base.Pos = lno } -func walkSelectCases(cases []*ir.CommStmt) []ir.Node { +func walkSelectCases(cases []*ir.CommClause) []ir.Node { ncas := len(cases) sellineno := base.Pos @@ -73,7 +73,7 @@ func walkSelectCases(cases []*ir.CommStmt) []ir.Node { // convert case value arguments to addresses. // this rewrite is used by both the general code and the next optimization. - var dflt *ir.CommStmt + var dflt *ir.CommClause for _, cas := range cases { ir.SetPos(cas) n := cas.Comm @@ -146,7 +146,7 @@ func walkSelectCases(cases []*ir.CommStmt) []ir.Node { if dflt != nil { ncas-- } - casorder := make([]*ir.CommStmt, ncas) + casorder := make([]*ir.CommClause, ncas) nsends, nrecvs := 0, 0 var init []ir.Node @@ -242,7 +242,7 @@ func walkSelectCases(cases []*ir.CommStmt) []ir.Node { } // dispatch cases - dispatch := func(cond ir.Node, cas *ir.CommStmt) { + dispatch := func(cond ir.Node, cas *ir.CommClause) { cond = typecheck.Expr(cond) cond = typecheck.DefaultLit(cond, nil) -- cgit v1.3-5-g9baa From fda7ec3a3f03f95854d33e344b41d52e017e88e0 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Sun, 27 Dec 2020 11:45:57 -0800 Subject: [dev.regabi] cmd/compile: remove Name.IsDDD, etc These are never used. Change-Id: I58f7359f20252ca942f59bc7593c615a7b9de105 Reviewed-on: https://go-review.googlesource.com/c/go/+/280514 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/ir/name.go | 3 --- src/cmd/compile/internal/noder/noder.go | 1 - src/cmd/compile/internal/typecheck/dcl.go | 2 -- 3 files changed, 6 deletions(-) (limited to 'src/cmd/compile/internal/noder/noder.go') diff --git a/src/cmd/compile/internal/ir/name.go b/src/cmd/compile/internal/ir/name.go index 93535f4cee..cc8e1b4cd1 100644 --- a/src/cmd/compile/internal/ir/name.go +++ b/src/cmd/compile/internal/ir/name.go @@ -268,7 +268,6 @@ const ( nameInlLocal // PAUTO created by inliner, derived from callee local nameOpenDeferSlot // if temporary var storing info for open-coded defers nameLibfuzzerExtraCounter // if PEXTERN should be assigned to __libfuzzer_extra_counters section - nameIsDDD // is function argument a ... nameAlias // is type name an alias ) @@ -286,7 +285,6 @@ func (n *Name) InlFormal() bool { return n.flags&nameInlFormal != 0 func (n *Name) InlLocal() bool { return n.flags&nameInlLocal != 0 } func (n *Name) OpenDeferSlot() bool { return n.flags&nameOpenDeferSlot != 0 } func (n *Name) LibfuzzerExtraCounter() bool { return n.flags&nameLibfuzzerExtraCounter != 0 } -func (n *Name) IsDDD() bool { return n.flags&nameIsDDD != 0 } func (n *Name) SetCaptured(b bool) { n.flags.set(nameCaptured, b) } func (n *Name) setReadonly(b bool) { n.flags.set(nameReadonly, b) } @@ -302,7 +300,6 @@ func (n *Name) SetInlFormal(b bool) { n.flags.set(nameInlFormal, b) func (n *Name) SetInlLocal(b bool) { n.flags.set(nameInlLocal, b) } func (n *Name) SetOpenDeferSlot(b bool) { n.flags.set(nameOpenDeferSlot, b) } func (n *Name) SetLibfuzzerExtraCounter(b bool) { n.flags.set(nameLibfuzzerExtraCounter, b) } -func (n *Name) SetIsDDD(b bool) { n.flags.set(nameIsDDD, b) } // MarkReadonly indicates that n is an ONAME with readonly contents. func (n *Name) MarkReadonly() { diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go index 7c1f7595b3..920f4839ad 100644 --- a/src/cmd/compile/internal/noder/noder.go +++ b/src/cmd/compile/internal/noder/noder.go @@ -1838,7 +1838,6 @@ func oldname(s *types.Sym) ir.Node { c = typecheck.NewName(s) c.Class_ = ir.PAUTOHEAP c.SetIsClosureVar(true) - c.SetIsDDD(n.IsDDD()) c.Defn = n // Link into list of active closure variables. diff --git a/src/cmd/compile/internal/typecheck/dcl.go b/src/cmd/compile/internal/typecheck/dcl.go index 0da0956c3a..36057ba2d1 100644 --- a/src/cmd/compile/internal/typecheck/dcl.go +++ b/src/cmd/compile/internal/typecheck/dcl.go @@ -447,7 +447,6 @@ func funcarg(n *ir.Field, ctxt ir.Class) { name := ir.NewNameAt(n.Pos, n.Sym) n.Decl = name name.Ntype = n.Ntype - name.SetIsDDD(n.IsDDD) Declare(name, ctxt) vargen++ @@ -461,7 +460,6 @@ func funcarg2(f *types.Field, ctxt ir.Class) { n := ir.NewNameAt(f.Pos, f.Sym) f.Nname = n n.SetType(f.Type) - n.SetIsDDD(f.IsDDD()) Declare(n, ctxt) } -- cgit v1.3-5-g9baa