[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
// of type typ. As a special case, 8-bit types are always encoded as a
// single byte.
//
// TODO(mdempsky): Is this level of complexity really worthwhile?
func (w *exportWriter) mpint(x constant.Value, typ *types.Type) {
signed, maxBytes := intSize(typ)
@ -1154,7 +1152,6 @@ func (w *exportWriter) stmt(n ir.Node) {
w.op(n.Op())
w.pos(n.Pos())
w.stmtList(n.Init())
w.exprsOrNil(nil, nil) // TODO(rsc): Delete (and fix importer).
w.caseList(n.Cases, false)
case ir.OSWITCH:
@ -1298,7 +1295,7 @@ func (w *exportWriter) expr(n ir.Node) {
s = n.Tag.Sym()
}
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:
// 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)
w.op(ir.OKEY)
w.pos(n.Pos())
w.exprsOrNil(n.Key, n.Value)
w.expr(n.Key)
w.expr(n.Value)
// case OSTRUCTKEY:
// unreachable - handled in case OSTRUCTLIT by elemList
@ -1397,8 +1395,8 @@ func (w *exportWriter) expr(n ir.Node) {
n := n.(*ir.ConvExpr)
w.op(ir.OCONV)
w.pos(n.Pos())
w.expr(n.X)
w.typ(n.Type())
w.expr(n.X)
case ir.OREAL, ir.OIMAG, ir.OCAP, ir.OCLOSE, ir.OLEN, ir.ONEW, ir.OPANIC:
n := n.(*ir.UnaryExpr)
@ -1529,6 +1527,7 @@ func (w *exportWriter) fieldList(list ir.Nodes) {
w.uint64(uint64(len(list)))
for _, n := range list {
n := n.(*ir.StructKeyExpr)
w.pos(n.Pos())
w.selector(n.Field)
w.expr(n.Value)
}

View file

@ -851,8 +851,7 @@ func (r *importReader) node() ir.Node {
if s := r.ident(); s != nil {
tag = ir.NewIdent(pos, s)
}
expr, _ := r.exprsOrNil()
return ir.NewTypeSwitchGuard(pos, tag, expr)
return ir.NewTypeSwitchGuard(pos, tag, r.expr())
// case OTARRAY, OTMAP, OTCHAN, OTSTRUCT, OTINTER, OTFUNC:
// 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
case ir.OSTRUCTLIT:
pos := r.pos()
return ir.NewCompLitExpr(pos, ir.OCOMPLIT, ir.TypeNode(r.typ()).(ir.Ntype), r.elemList(pos))
return ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()), r.fieldList())
// case OARRAYLIT, OSLICELIT, OMAPLIT:
// unreachable - mapped to case OCOMPLIT below by exporter
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:
pos := r.pos()
key, value := r.exprsOrNil()
return ir.NewKeyExpr(pos, key, value)
return ir.NewKeyExpr(r.pos(), r.expr(), r.expr())
// case OSTRUCTKEY:
// 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
case ir.OCONV:
pos := r.pos()
x := r.expr()
return ir.NewConvExpr(pos, ir.OCONV, r.typ(), x)
return ir.NewConvExpr(r.pos(), ir.OCONV, r.typ(), r.expr())
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)
@ -973,7 +967,6 @@ func (r *importReader) node() ir.Node {
pos := r.pos()
list := r.exprList()
x := list[0]
x.SetPos(pos) // TODO(mdempsky): Remove toolstash bandage.
for _, y := range list[1:] {
x = ir.NewBinaryExpr(pos, ir.OADD, x, y)
}
@ -1041,7 +1034,6 @@ func (r *importReader) node() ir.Node {
case ir.OSELECT:
pos := r.pos()
init := r.stmtList()
r.exprsOrNil() // TODO(rsc): Delete (and fix exporter). These are always nil.
n := ir.NewSelectStmt(pos, r.caseList(nil))
n.PtrInit().Set(init)
return n
@ -1088,12 +1080,10 @@ func (r *importReader) op() ir.Op {
return ir.Op(r.uint64())
}
func (r *importReader) elemList(pos src.XPos) []ir.Node {
c := r.uint64()
list := make([]ir.Node, c)
func (r *importReader) fieldList() []ir.Node {
list := make([]ir.Node, r.uint64())
for i := range list {
// TODO(mdempsky): Export position information for OSTRUCTKEY nodes.
list[i] = ir.NewStructKeyExpr(pos, r.ident(), r.expr())
list[i] = ir.NewStructKeyExpr(r.pos(), r.ident(), r.expr())
}
return list
}