From 87a592b35602e89c55218d2a54a1e0dade5db7e2 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 23 Dec 2020 01:15:58 -0800 Subject: [PATCH] [dev.regabi] cmd/compile: cleanup import/export code Now that we have concrete AST node types and better constructor APIs, we can more cleanup a lot of the import code and some export code too. Passes toolstash -cmp. Change-Id: Ie3425d9dac11ac4245e5da675dd298984a926df4 Reviewed-on: https://go-review.googlesource.com/c/go/+/279954 Trust: Matthew Dempsky Run-TryBot: Matthew Dempsky TryBot-Result: Go Bot Reviewed-by: Cuong Manh Le --- src/cmd/compile/internal/typecheck/iexport.go | 27 +---- src/cmd/compile/internal/typecheck/iimport.go | 114 +++++++----------- 2 files changed, 49 insertions(+), 92 deletions(-) diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index 4ddee01b5a..95a100e6a5 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -1155,7 +1155,7 @@ func (w *exportWriter) stmt(n ir.Node) { w.pos(n.Pos()) w.stmtList(n.Init()) w.exprsOrNil(nil, nil) // TODO(rsc): Delete (and fix importer). - w.caseList(n) + w.caseList(n.Cases, false) case ir.OSWITCH: n := n.(*ir.SwitchStmt) @@ -1163,7 +1163,7 @@ func (w *exportWriter) stmt(n ir.Node) { w.pos(n.Pos()) w.stmtList(n.Init()) w.exprsOrNil(n.Tag, nil) - w.caseList(n) + w.caseList(n.Cases, isNamedTypeSwitch(n.Tag)) // case OCASE: // handled by caseList @@ -1187,27 +1187,12 @@ func (w *exportWriter) stmt(n ir.Node) { } } -func isNamedTypeSwitch(n ir.Node) bool { - if n.Op() != ir.OSWITCH { - return false - } - sw := n.(*ir.SwitchStmt) - if sw.Tag == nil || sw.Tag.Op() != ir.OTYPESW { - return false - } - guard := sw.Tag.(*ir.TypeSwitchGuard) - return guard.Tag != nil +func isNamedTypeSwitch(x ir.Node) bool { + guard, ok := x.(*ir.TypeSwitchGuard) + return ok && guard.Tag != nil } -func (w *exportWriter) caseList(sw ir.Node) { - namedTypeSwitch := isNamedTypeSwitch(sw) - - var cases []ir.Node - if sw.Op() == ir.OSWITCH { - cases = sw.(*ir.SwitchStmt).Cases - } else { - cases = sw.(*ir.SelectStmt).Cases - } +func (w *exportWriter) caseList(cases []ir.Node, namedTypeSwitch bool) { w.uint64(uint64(len(cases))) for _, cas := range cases { cas := cas.(*ir.CaseStmt) diff --git a/src/cmd/compile/internal/typecheck/iimport.go b/src/cmd/compile/internal/typecheck/iimport.go index ab43d4f71b..3c7dde5506 100644 --- a/src/cmd/compile/internal/typecheck/iimport.go +++ b/src/cmd/compile/internal/typecheck/iimport.go @@ -767,8 +767,8 @@ func (r *importReader) stmtList() []ir.Node { return list } -func (r *importReader) caseList(sw ir.Node) []ir.Node { - namedTypeSwitch := isNamedTypeSwitch(sw) +func (r *importReader) caseList(switchExpr ir.Node) []ir.Node { + namedTypeSwitch := isNamedTypeSwitch(switchExpr) cases := make([]ir.Node, r.uint64()) for i := range cases { @@ -781,7 +781,7 @@ func (r *importReader) caseList(sw ir.Node) []ir.Node { caseVar := ir.NewNameAt(cas.Pos(), r.ident()) Declare(caseVar, DeclContext) cas.Vars = []ir.Node{caseVar} - caseVar.Defn = sw.(*ir.SwitchStmt).Tag + caseVar.Defn = switchExpr } cas.Body.Set(r.stmtList()) cases[i] = cas @@ -821,7 +821,7 @@ func (r *importReader) node() ir.Node { pos := r.pos() typ := r.typ() - n := npos(pos, NodNil()) + n := ir.NewNilExpr(pos) n.SetType(typ) return n @@ -829,7 +829,7 @@ func (r *importReader) node() ir.Node { pos := r.pos() typ := r.typ() - n := npos(pos, ir.NewLiteral(r.value(typ))) + n := ir.NewBasicLit(pos, r.value(typ)) n.SetType(typ) return n @@ -864,26 +864,19 @@ func (r *importReader) node() ir.Node { // unreachable - mapped to case OADDR below by exporter case ir.OSTRUCTLIT: - // TODO(mdempsky): Export position information for OSTRUCTKEY nodes. - savedlineno := base.Pos - base.Pos = r.pos() - n := ir.NewCompLitExpr(base.Pos, ir.OCOMPLIT, ir.TypeNode(r.typ()).(ir.Ntype), nil) - n.List.Set(r.elemList()) // special handling of field names - base.Pos = savedlineno - return n + pos := r.pos() + return ir.NewCompLitExpr(pos, ir.OCOMPLIT, ir.TypeNode(r.typ()).(ir.Ntype), r.elemList(pos)) // case OARRAYLIT, OSLICELIT, OMAPLIT: // unreachable - mapped to case OCOMPLIT below by exporter case ir.OCOMPLIT: - n := ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()).(ir.Ntype), nil) - n.List.Set(r.exprList()) - return n + return ir.NewCompLitExpr(r.pos(), ir.OCOMPLIT, ir.TypeNode(r.typ()).(ir.Ntype), r.exprList()) case ir.OKEY: pos := r.pos() - left, right := r.exprsOrNil() - return ir.NewKeyExpr(pos, left, right) + key, value := r.exprsOrNil() + return ir.NewKeyExpr(pos, key, value) // case OSTRUCTKEY: // unreachable - handled in case OSTRUCTLIT by elemList @@ -926,9 +919,9 @@ func (r *importReader) node() ir.Node { // unreachable - mapped to OCONV case below by exporter case ir.OCONV: - n := ir.NewConvExpr(r.pos(), ir.OCONV, nil, r.expr()) - n.SetType(r.typ()) - return n + pos := r.pos() + 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: n := builtinCall(r.pos(), op) @@ -942,10 +935,10 @@ func (r *importReader) node() ir.Node { // unreachable - mapped to OCALL case below by exporter case ir.OCALL: - n := ir.NewCallExpr(r.pos(), ir.OCALL, nil, nil) - n.PtrInit().Set(r.stmtList()) - n.X = r.expr() - n.Args.Set(r.exprList()) + pos := r.pos() + init := r.stmtList() + n := ir.NewCallExpr(pos, ir.OCALL, r.expr(), r.exprList()) + n.PtrInit().Set(init) n.IsDDD = r.bool() return n @@ -979,7 +972,8 @@ func (r *importReader) node() ir.Node { case ir.OADDSTR: pos := r.pos() list := r.exprList() - x := npos(pos, list[0]) + x := list[0] + x.SetPos(pos) // TODO(mdempsky): Remove toolstash bandage. for _, y := range list[1:] { x = ir.NewBinaryExpr(pos, ir.OADD, x, y) } @@ -1006,9 +1000,7 @@ func (r *importReader) node() ir.Node { return ir.NewAssignStmt(r.pos(), r.expr(), r.expr()) case ir.OASOP: - n := ir.NewAssignOpStmt(r.pos(), ir.OXXX, nil, nil) - n.AsOp = r.op() - n.X = r.expr() + n := ir.NewAssignOpStmt(r.pos(), r.op(), r.expr(), nil) if !r.bool() { n.Y = ir.NewInt(1) n.IncDec = true @@ -1021,15 +1013,10 @@ func (r *importReader) node() ir.Node { // unreachable - mapped to OAS2 case below by exporter case ir.OAS2: - n := ir.NewAssignListStmt(r.pos(), ir.OAS2, nil, nil) - n.Lhs.Set(r.exprList()) - n.Rhs.Set(r.exprList()) - return n + return ir.NewAssignListStmt(r.pos(), ir.OAS2, r.exprList(), r.exprList()) case ir.ORETURN: - n := ir.NewReturnStmt(r.pos(), nil) - n.Results.Set(r.exprList()) - return n + return ir.NewReturnStmt(r.pos(), r.exprList()) // case ORETJMP: // unreachable - generated by compiler for trampolin routines (not exported) @@ -1038,57 +1025,47 @@ func (r *importReader) node() ir.Node { return ir.NewGoDeferStmt(r.pos(), op, r.expr()) case ir.OIF: - n := ir.NewIfStmt(r.pos(), nil, nil, nil) - n.PtrInit().Set(r.stmtList()) - n.Cond = r.expr() - n.Body.Set(r.stmtList()) - n.Else.Set(r.stmtList()) + pos, init := r.pos(), r.stmtList() + n := ir.NewIfStmt(pos, r.expr(), r.stmtList(), r.stmtList()) + n.PtrInit().Set(init) return n case ir.OFOR: - n := ir.NewForStmt(r.pos(), nil, nil, nil, nil) - n.PtrInit().Set(r.stmtList()) - left, right := r.exprsOrNil() - n.Cond = left - n.Post = right - n.Body.Set(r.stmtList()) - return n + pos, init := r.pos(), r.stmtList() + cond, post := r.exprsOrNil() + return ir.NewForStmt(pos, init, cond, post, r.stmtList()) case ir.ORANGE: - n := ir.NewRangeStmt(r.pos(), nil, nil, nil) - n.Vars.Set(r.stmtList()) - n.X = r.expr() - n.Body.Set(r.stmtList()) - return n + return ir.NewRangeStmt(r.pos(), r.stmtList(), r.expr(), r.stmtList()) case ir.OSELECT: - n := ir.NewSelectStmt(r.pos(), nil) - n.PtrInit().Set(r.stmtList()) + pos := r.pos() + init := r.stmtList() r.exprsOrNil() // TODO(rsc): Delete (and fix exporter). These are always nil. - n.Cases.Set(r.caseList(n)) + n := ir.NewSelectStmt(pos, r.caseList(nil)) + n.PtrInit().Set(init) return n case ir.OSWITCH: - n := ir.NewSwitchStmt(r.pos(), nil, nil) - n.PtrInit().Set(r.stmtList()) - left, _ := r.exprsOrNil() - n.Tag = left - n.Cases.Set(r.caseList(n)) + pos := r.pos() + init := r.stmtList() + x, _ := r.exprsOrNil() + n := ir.NewSwitchStmt(pos, x, r.caseList(x)) + n.PtrInit().Set(init) return n // case OCASE: // handled by caseList case ir.OFALL: - n := ir.NewBranchStmt(r.pos(), ir.OFALL, nil) - return n + return ir.NewBranchStmt(r.pos(), ir.OFALL, nil) // case OEMPTY: // unreachable - not emitted by exporter case ir.OBREAK, ir.OCONTINUE, ir.OGOTO: - var sym *types.Sym pos := r.pos() + var sym *types.Sym if label := r.string(); label != "" { sym = Lookup(label) } @@ -1111,12 +1088,12 @@ func (r *importReader) op() ir.Op { return ir.Op(r.uint64()) } -func (r *importReader) elemList() []ir.Node { +func (r *importReader) elemList(pos src.XPos) []ir.Node { c := r.uint64() list := make([]ir.Node, c) for i := range list { - s := r.ident() - list[i] = ir.NewStructKeyExpr(base.Pos, s, r.expr()) + // TODO(mdempsky): Export position information for OSTRUCTKEY nodes. + list[i] = ir.NewStructKeyExpr(pos, r.ident(), r.expr()) } return list } @@ -1135,8 +1112,3 @@ func (r *importReader) exprsOrNil() (a, b ir.Node) { func builtinCall(pos src.XPos, op ir.Op) *ir.CallExpr { return ir.NewCallExpr(pos, ir.OCALL, ir.NewIdent(base.Pos, types.BuiltinPkg.Lookup(ir.OpNames[op])), nil) } - -func npos(pos src.XPos, n ir.Node) ir.Node { - n.SetPos(pos) - return n -} -- 2.48.1