1
0
mirror of https://github.com/golang/go synced 2024-11-13 13:40:22 -07:00

[dev.regabi] cmd/compile: cleanup export code further

This CL rips off a number of toolstash bandages:

- Fixes position information for string concatenation.

- Adds position information for struct literal fields.

- Removes unnecessary exprsOrNil calls or replaces them with plain
  expr calls when possible.

- Reorders conversion expressions to put type first, which matches
  source order and also the order the importer needs for calling the
  ConvExpr constructor.

Change-Id: I44cdc6035540d9ecefd9c1bcd92b8711d6ed813c
Reviewed-on: https://go-review.googlesource.com/c/go/+/279957
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
This commit is contained in:
Matthew Dempsky 2020-12-23 03:33:03 -08:00
parent 31267f82e1
commit 53f082b0ee
2 changed files with 13 additions and 24 deletions

View File

@ -858,8 +858,6 @@ func intSize(typ *types.Type) (signed bool, maxBytes uint) {
// according to the maximum number of bytes needed to encode a value // according to the maximum number of bytes needed to encode a value
// of type typ. As a special case, 8-bit types are always encoded as a // of type typ. As a special case, 8-bit types are always encoded as a
// single byte. // single byte.
//
// TODO(mdempsky): Is this level of complexity really worthwhile?
func (w *exportWriter) mpint(x constant.Value, typ *types.Type) { func (w *exportWriter) mpint(x constant.Value, typ *types.Type) {
signed, maxBytes := intSize(typ) signed, maxBytes := intSize(typ)
@ -1154,7 +1152,6 @@ func (w *exportWriter) stmt(n ir.Node) {
w.op(n.Op()) w.op(n.Op())
w.pos(n.Pos()) w.pos(n.Pos())
w.stmtList(n.Init()) w.stmtList(n.Init())
w.exprsOrNil(nil, nil) // TODO(rsc): Delete (and fix importer).
w.caseList(n.Cases, false) w.caseList(n.Cases, false)
case ir.OSWITCH: case ir.OSWITCH:
@ -1298,7 +1295,7 @@ func (w *exportWriter) expr(n ir.Node) {
s = n.Tag.Sym() s = n.Tag.Sym()
} }
w.localIdent(s, 0) // declared pseudo-variable, if any w.localIdent(s, 0) // declared pseudo-variable, if any
w.exprsOrNil(n.X, nil) w.expr(n.X)
// case OTARRAY, OTMAP, OTCHAN, OTSTRUCT, OTINTER, OTFUNC: // case OTARRAY, OTMAP, OTCHAN, OTSTRUCT, OTINTER, OTFUNC:
// should have been resolved by typechecking - handled by default case // should have been resolved by typechecking - handled by default case
@ -1333,7 +1330,8 @@ func (w *exportWriter) expr(n ir.Node) {
n := n.(*ir.KeyExpr) n := n.(*ir.KeyExpr)
w.op(ir.OKEY) w.op(ir.OKEY)
w.pos(n.Pos()) w.pos(n.Pos())
w.exprsOrNil(n.Key, n.Value) w.expr(n.Key)
w.expr(n.Value)
// case OSTRUCTKEY: // case OSTRUCTKEY:
// unreachable - handled in case OSTRUCTLIT by elemList // unreachable - handled in case OSTRUCTLIT by elemList
@ -1397,8 +1395,8 @@ func (w *exportWriter) expr(n ir.Node) {
n := n.(*ir.ConvExpr) n := n.(*ir.ConvExpr)
w.op(ir.OCONV) w.op(ir.OCONV)
w.pos(n.Pos()) w.pos(n.Pos())
w.expr(n.X)
w.typ(n.Type()) w.typ(n.Type())
w.expr(n.X)
case ir.OREAL, ir.OIMAG, ir.OCAP, ir.OCLOSE, ir.OLEN, ir.ONEW, ir.OPANIC: case ir.OREAL, ir.OIMAG, ir.OCAP, ir.OCLOSE, ir.OLEN, ir.ONEW, ir.OPANIC:
n := n.(*ir.UnaryExpr) n := n.(*ir.UnaryExpr)
@ -1529,6 +1527,7 @@ func (w *exportWriter) fieldList(list ir.Nodes) {
w.uint64(uint64(len(list))) w.uint64(uint64(len(list)))
for _, n := range list { for _, n := range list {
n := n.(*ir.StructKeyExpr) n := n.(*ir.StructKeyExpr)
w.pos(n.Pos())
w.selector(n.Field) w.selector(n.Field)
w.expr(n.Value) w.expr(n.Value)
} }

View File

@ -851,8 +851,7 @@ func (r *importReader) node() ir.Node {
if s := r.ident(); s != nil { if s := r.ident(); s != nil {
tag = ir.NewIdent(pos, s) tag = ir.NewIdent(pos, s)
} }
expr, _ := r.exprsOrNil() return ir.NewTypeSwitchGuard(pos, tag, r.expr())
return ir.NewTypeSwitchGuard(pos, tag, expr)
// case OTARRAY, OTMAP, OTCHAN, OTSTRUCT, OTINTER, OTFUNC: // case OTARRAY, OTMAP, OTCHAN, OTSTRUCT, OTINTER, OTFUNC:
// unreachable - should have been resolved by typechecking // unreachable - should have been resolved by typechecking
@ -864,19 +863,16 @@ func (r *importReader) node() ir.Node {
// unreachable - mapped to case OADDR below by exporter // unreachable - mapped to case OADDR below by exporter
case ir.OSTRUCTLIT: case ir.OSTRUCTLIT:
pos := r.pos() return ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()), r.fieldList())
return ir.NewCompLitExpr(pos, ir.OCOMPLIT, ir.TypeNode(r.typ()).(ir.Ntype), r.elemList(pos))
// case OARRAYLIT, OSLICELIT, OMAPLIT: // case OARRAYLIT, OSLICELIT, OMAPLIT:
// unreachable - mapped to case OCOMPLIT below by exporter // unreachable - mapped to case OCOMPLIT below by exporter
case ir.OCOMPLIT: case ir.OCOMPLIT:
return ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()).(ir.Ntype), r.exprList()) return ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()), r.exprList())
case ir.OKEY: case ir.OKEY:
pos := r.pos() return ir.NewKeyExpr(r.pos(), r.expr(), r.expr())
key, value := r.exprsOrNil()
return ir.NewKeyExpr(pos, key, value)
// case OSTRUCTKEY: // case OSTRUCTKEY:
// unreachable - handled in case OSTRUCTLIT by elemList // unreachable - handled in case OSTRUCTLIT by elemList
@ -919,9 +915,7 @@ func (r *importReader) node() ir.Node {
// unreachable - mapped to OCONV case below by exporter // unreachable - mapped to OCONV case below by exporter
case ir.OCONV: case ir.OCONV:
pos := r.pos() return ir.NewConvExpr(r.pos(), ir.OCONV, r.typ(), r.expr())
x := r.expr()
return ir.NewConvExpr(pos, ir.OCONV, r.typ(), x)
case ir.OCOPY, ir.OCOMPLEX, ir.OREAL, ir.OIMAG, ir.OAPPEND, ir.OCAP, ir.OCLOSE, ir.ODELETE, ir.OLEN, ir.OMAKE, ir.ONEW, ir.OPANIC, ir.ORECOVER, ir.OPRINT, ir.OPRINTN: case ir.OCOPY, ir.OCOMPLEX, ir.OREAL, ir.OIMAG, ir.OAPPEND, ir.OCAP, ir.OCLOSE, ir.ODELETE, ir.OLEN, ir.OMAKE, ir.ONEW, ir.OPANIC, ir.ORECOVER, ir.OPRINT, ir.OPRINTN:
n := builtinCall(r.pos(), op) n := builtinCall(r.pos(), op)
@ -973,7 +967,6 @@ func (r *importReader) node() ir.Node {
pos := r.pos() pos := r.pos()
list := r.exprList() list := r.exprList()
x := list[0] x := list[0]
x.SetPos(pos) // TODO(mdempsky): Remove toolstash bandage.
for _, y := range list[1:] { for _, y := range list[1:] {
x = ir.NewBinaryExpr(pos, ir.OADD, x, y) x = ir.NewBinaryExpr(pos, ir.OADD, x, y)
} }
@ -1041,7 +1034,6 @@ func (r *importReader) node() ir.Node {
case ir.OSELECT: case ir.OSELECT:
pos := r.pos() pos := r.pos()
init := r.stmtList() init := r.stmtList()
r.exprsOrNil() // TODO(rsc): Delete (and fix exporter). These are always nil.
n := ir.NewSelectStmt(pos, r.caseList(nil)) n := ir.NewSelectStmt(pos, r.caseList(nil))
n.PtrInit().Set(init) n.PtrInit().Set(init)
return n return n
@ -1088,12 +1080,10 @@ func (r *importReader) op() ir.Op {
return ir.Op(r.uint64()) return ir.Op(r.uint64())
} }
func (r *importReader) elemList(pos src.XPos) []ir.Node { func (r *importReader) fieldList() []ir.Node {
c := r.uint64() list := make([]ir.Node, r.uint64())
list := make([]ir.Node, c)
for i := range list { for i := range list {
// TODO(mdempsky): Export position information for OSTRUCTKEY nodes. list[i] = ir.NewStructKeyExpr(r.pos(), r.ident(), r.expr())
list[i] = ir.NewStructKeyExpr(pos, r.ident(), r.expr())
} }
return list return list
} }