From 6770b5cdca06dba0636252221ed77a8125dabec2 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Fri, 3 Jan 2014 09:12:32 -0800 Subject: [PATCH] go.tools/go/importer: more cleanups R=adonovan CC=golang-codereviews https://golang.org/cl/34660046 --- go/importer/export.go | 37 +++++++++++++++++++------------------ go/importer/import.go | 20 +++++--------------- go/importer/import_test.go | 6 +++--- 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/go/importer/export.go b/go/importer/export.go index 4723a164..9c5eddd5 100644 --- a/go/importer/export.go +++ b/go/importer/export.go @@ -64,11 +64,9 @@ const ( // format is described elsewhere (TODO). func ExportData(pkg *types.Package) []byte { p := exporter{ - data: []byte(magic), - // TODO(gri) If we can't have nil packages - // or types, remove nil entries at index 0. - pkgIndex: map[*types.Package]int{nil: 0}, - typIndex: map[types.Type]int{nil: 0}, + data: []byte(magic), + pkgIndex: make(map[*types.Package]int), + typIndex: make(map[types.Type]int), } // populate typIndex with predeclared types @@ -119,6 +117,10 @@ func (p *exporter) pkg(pkg *types.Package) { defer p.tracef("} ") } + if pkg == nil { + panic("unexpected nil pkg") + } + // if the package was seen before, write its index (>= 0) if i, ok := p.pkgIndex[pkg]; ok { p.int(i) @@ -368,15 +370,23 @@ func (p *exporter) signature(sig *types.Signature) { // for interface methods if we flatten them // out. If we track embedded types instead, // the information is already present. + // We do need the receiver information (T vs *T) + // for methods associated with named types. if recv := sig.Recv(); recv != nil { - p.bool(true) + // 1-element tuple + p.int(1) p.param(recv) } else { - p.bool(false) + // 0-element tuple + p.int(0) } p.tuple(sig.Params()) p.tuple(sig.Results()) - p.bool(sig.IsVariadic()) + if sig.IsVariadic() { + p.int(1) + } else { + p.int(0) + } } func (p *exporter) param(v *types.Var) { @@ -395,17 +405,8 @@ func (p *exporter) tuple(t *types.Tuple) { // ---------------------------------------------------------------------------- // encoders -func (p *exporter) bool(b bool) { - var x int64 - if b { - x = 1 - } - p.int64(x) -} - func (p *exporter) string(s string) { - // TODO(gri) consider inlining this to avoid an extra allocation - p.bytes([]byte(s)) + p.bytes([]byte(s)) // (could be inlined if extra allocation matters) } func (p *exporter) int(x int) { diff --git a/go/importer/import.go b/go/importer/import.go index 7e0e5b57..afc430f8 100644 --- a/go/importer/import.go +++ b/go/importer/import.go @@ -31,8 +31,6 @@ func ImportData(imports map[string]*types.Package, data []byte) (*types.Package, p := importer{ data: data[len(magic):], imports: imports, - pkgList: []*types.Package{nil}, - typList: []types.Type{nil}, consumed: len(magic), // for debugging only } @@ -47,8 +45,8 @@ func ImportData(imports map[string]*types.Package, data []byte) (*types.Package, } pkg := p.pkg() - if debug && p.pkgList[1] != pkg { - panic("imported packaged not found in pkgList[1]") + if debug && p.pkgList[0] != pkg { + panic("imported packaged not found in pkgList[0]") } // read objects @@ -337,7 +335,6 @@ func (p *importer) field() *types.Var { name = typ.Name() case *types.Named: obj := typ.Obj() - pkg = obj.Pkg() // TODO(gri) is this still correct? name = obj.Name() default: panic("anonymous field expected") @@ -350,22 +347,19 @@ func (p *importer) field() *types.Var { func (p *importer) qualifiedName() (*types.Package, string) { name := p.string() - pkg := p.pkgList[1] // exported names assume current package + pkg := p.pkgList[0] // exported names assume current package if !exported(name) { pkg = p.pkg() - if pkg == nil { - panic(fmt.Sprintf("nil package for unexported qualified name %q", name)) - } } return pkg, name } func (p *importer) signature() *types.Signature { var recv *types.Var - if p.bool() { + if p.int() != 0 { recv = p.param() } - return types.NewSignature(nil, recv, p.tuple(), p.tuple(), p.bool()) + return types.NewSignature(nil, recv, p.tuple(), p.tuple(), p.int() != 0) } func (p *importer) param() *types.Var { @@ -383,10 +377,6 @@ func (p *importer) tuple() *types.Tuple { // ---------------------------------------------------------------------------- // decoders -func (p *importer) bool() bool { - return p.int64() != 0 -} - func (p *importer) string() string { return string(p.bytes()) } diff --git a/go/importer/import_test.go b/go/importer/import_test.go index 6bee004f..cd2af5d2 100644 --- a/go/importer/import_test.go +++ b/go/importer/import_test.go @@ -105,7 +105,7 @@ func TestImportStdLib(t *testing.T) { // can be compared reasonably well types.GcCompatibilityMode = true - var totSize, totGcsize int + var totSize, totGcSize int for _, lib := range libs { // limit run time for short tests if testing.Short() && time.Since(start) >= 750*time.Millisecond { @@ -129,11 +129,11 @@ func TestImportStdLib(t *testing.T) { fmt.Printf("%s\t%d\t%d\t%d%%\n", lib, size, gcsize, int(float64(size)*100/float64(gcsize))) } totSize += size - totGcsize += gcsize + totGcSize += gcsize } if testing.Verbose() { - fmt.Printf("\n%d\t%d\t%d%%\n", totSize, totGcsize, int(float64(totSize)*100/float64(totGcsize))) + fmt.Printf("\n%d\t%d\t%d%%\n", totSize, totGcSize, int(float64(totSize)*100/float64(totGcSize))) } types.GcCompatibilityMode = false