diff options
| author | Robert Griesemer <gri@golang.org> | 2024-08-20 10:35:23 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2024-08-20 22:55:24 +0000 |
| commit | 7dc1ee81f9be4bf3397646afcd3170e896389342 (patch) | |
| tree | 8624ab5da050b7b9fd5ab473ad82f4b7cbb9f809 /src/cmd/compile/internal/noder/writer.go | |
| parent | a5d61d75e3d314323d07914ae36daf69c96f0bf8 (diff) | |
| download | go-7dc1ee81f9be4bf3397646afcd3170e896389342.tar.xz | |
cmd/compile/internal/noder: reduce clutter a bit (cosmetic changes)
- introduce index alias
- inline the two short tables in stmt.go (removes a TODO)
- move assert out of stencil.go and remove that file
(we can always re-introduce it)
Also, replace two if's with a simpler switch.
Change-Id: I25c3104164574999dd9826dee6166dd8a8488908
Reviewed-on: https://go-review.googlesource.com/c/go/+/607236
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Tim King <taking@google.com>
Diffstat (limited to 'src/cmd/compile/internal/noder/writer.go')
| -rw-r--r-- | src/cmd/compile/internal/noder/writer.go | 80 |
1 files changed, 51 insertions, 29 deletions
diff --git a/src/cmd/compile/internal/noder/writer.go b/src/cmd/compile/internal/noder/writer.go index 9f862f9a4c..0d59d84b61 100644 --- a/src/cmd/compile/internal/noder/writer.go +++ b/src/cmd/compile/internal/noder/writer.go @@ -58,6 +58,10 @@ import ( // and better document the file format boundary between public and // private data. +type index = pkgbits.Index + +func assert(p bool) { base.Assert(p) } + // A pkgWriter constructs Unified IR export data from the results of // running the types2 type checker on a Go compilation unit. type pkgWriter struct { @@ -70,10 +74,10 @@ type pkgWriter struct { // Indices for previously written syntax and types2 things. - posBasesIdx map[*syntax.PosBase]pkgbits.Index - pkgsIdx map[*types2.Package]pkgbits.Index - typsIdx map[types2.Type]pkgbits.Index - objsIdx map[types2.Object]pkgbits.Index + posBasesIdx map[*syntax.PosBase]index + pkgsIdx map[*types2.Package]index + typsIdx map[types2.Type]index + objsIdx map[types2.Object]index // Maps from types2.Objects back to their syntax.Decl. @@ -100,11 +104,11 @@ func newPkgWriter(m posMap, pkg *types2.Package, info *types2.Info, otherInfo ma info: info, rangeFuncBodyClosures: otherInfo, - pkgsIdx: make(map[*types2.Package]pkgbits.Index), - objsIdx: make(map[types2.Object]pkgbits.Index), - typsIdx: make(map[types2.Type]pkgbits.Index), + pkgsIdx: make(map[*types2.Package]index), + objsIdx: make(map[types2.Object]index), + typsIdx: make(map[types2.Type]index), - posBasesIdx: make(map[*syntax.PosBase]pkgbits.Index), + posBasesIdx: make(map[*syntax.PosBase]index), funDecls: make(map[*types2.Func]*syntax.FuncDecl), typDecls: make(map[*types2.TypeName]typeDeclGen), @@ -202,7 +206,7 @@ type writerDict struct { // derivedIdx maps a Type to its corresponding index within the // derived slice, if present. - derivedIdx map[types2.Type]pkgbits.Index + derivedIdx map[types2.Type]index // These slices correspond to entries in the runtime dictionary. typeParamMethodExprs []writerMethodExprInfo @@ -232,7 +236,7 @@ func (dict *writerDict) typeParamIndex(typ *types2.TypeParam) int { // A derivedInfo represents a reference to an encoded generic Go type. type derivedInfo struct { - idx pkgbits.Index + idx index needed bool // TODO(mdempsky): Remove. } @@ -245,23 +249,23 @@ type derivedInfo struct { // Otherwise, the typeInfo represents a non-generic Go type, and idx // is an index into the reader.typs array instead. type typeInfo struct { - idx pkgbits.Index + idx index derived bool } // An objInfo represents a reference to an encoded, instantiated (if // applicable) Go object. type objInfo struct { - idx pkgbits.Index // index for the generic function declaration - explicits []typeInfo // info for the type arguments + idx index // index for the generic function declaration + explicits []typeInfo // info for the type arguments } // A selectorInfo represents a reference to an encoded field or method // name (i.e., objects that can only be accessed using selector // expressions). type selectorInfo struct { - pkgIdx pkgbits.Index - nameIdx pkgbits.Index + pkgIdx index + nameIdx index } // anyDerived reports whether any of info's explicit type arguments @@ -391,7 +395,7 @@ func (w *writer) posBase(b *syntax.PosBase) { } // posBaseIdx returns the index for the given PosBase. -func (pw *pkgWriter) posBaseIdx(b *syntax.PosBase) pkgbits.Index { +func (pw *pkgWriter) posBaseIdx(b *syntax.PosBase) index { if idx, ok := pw.posBasesIdx[b]; ok { return idx } @@ -417,14 +421,14 @@ func (w *writer) pkg(pkg *types2.Package) { w.pkgRef(w.p.pkgIdx(pkg)) } -func (w *writer) pkgRef(idx pkgbits.Index) { +func (w *writer) pkgRef(idx index) { w.Sync(pkgbits.SyncPkg) w.Reloc(pkgbits.RelocPkg, idx) } // pkgIdx returns the index for the given package, adding it to the // package export data if needed. -func (pw *pkgWriter) pkgIdx(pkg *types2.Package) pkgbits.Index { +func (pw *pkgWriter) pkgIdx(pkg *types2.Package) index { if idx, ok := pw.pkgsIdx[pkg]; ok { return idx } @@ -602,7 +606,7 @@ func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo { } if w.derived { - idx := pkgbits.Index(len(dict.derived)) + idx := index(len(dict.derived)) dict.derived = append(dict.derived, derivedInfo{idx: w.Flush()}) dict.derivedIdx[typ] = idx return typeInfo{idx: idx, derived: true} @@ -748,7 +752,7 @@ func (pw *pkgWriter) objInstIdx(obj types2.Object, explicits *types2.TypeList, d // objIdx returns the index for the given Object, adding it to the // export data as needed. -func (pw *pkgWriter) objIdx(obj types2.Object) pkgbits.Index { +func (pw *pkgWriter) objIdx(obj types2.Object) index { // TODO(mdempsky): Validate that obj is a global object (or a local // defined type, which we hoist to global scope anyway). @@ -757,7 +761,7 @@ func (pw *pkgWriter) objIdx(obj types2.Object) pkgbits.Index { } dict := &writerDict{ - derivedIdx: make(map[types2.Type]pkgbits.Index), + derivedIdx: make(map[types2.Type]index), } if isDefinedType(obj) && obj.Pkg() == pw.curpkg { @@ -1150,7 +1154,7 @@ func (w *writer) pragmaFlag(p ir.PragmaFlag) { // bodyIdx returns the index for the given function body (specified by // block), adding it to the export data -func (pw *pkgWriter) bodyIdx(sig *types2.Signature, block *syntax.BlockStmt, dict *writerDict) (idx pkgbits.Index, closureVars []posVar) { +func (pw *pkgWriter) bodyIdx(sig *types2.Signature, block *syntax.BlockStmt, dict *writerDict) (idx index, closureVars []posVar) { w := pw.newWriter(pkgbits.RelocBody, pkgbits.SyncFuncBody) w.sig = sig w.dict = dict @@ -1306,13 +1310,31 @@ func (w *writer) stmt1(stmt syntax.Stmt) { case *syntax.BranchStmt: w.Code(stmtBranch) w.pos(stmt) - w.op(branchOps[stmt.Tok]) + var op ir.Op + switch stmt.Tok { + case syntax.Break: + op = ir.OBREAK + case syntax.Continue: + op = ir.OCONTINUE + case syntax.Fallthrough: + op = ir.OFALL + case syntax.Goto: + op = ir.OGOTO + } + w.op(op) w.optLabel(stmt.Label) case *syntax.CallStmt: w.Code(stmtCall) w.pos(stmt) - w.op(callOps[stmt.Tok]) + var op ir.Op + switch stmt.Tok { + case syntax.Defer: + op = ir.ODEFER + case syntax.Go: + op = ir.OGO + } + w.op(op) w.expr(stmt.Call) if stmt.Tok == syntax.Defer { w.optExpr(stmt.DeferAt) @@ -2973,11 +2995,11 @@ func objTypeParams(obj types2.Object) *types2.TypeParamList { } return sig.TypeParams() case *types2.TypeName: - if !obj.IsAlias() { - return obj.Type().(*types2.Named).TypeParams() - } - if alias, ok := obj.Type().(*types2.Alias); ok { - return alias.TypeParams() + switch t := obj.Type().(type) { + case *types2.Named: + return t.TypeParams() + case *types2.Alias: + return t.TypeParams() } } return nil |
