go.tools/go/importer: more cleanups

R=adonovan
CC=golang-codereviews
https://golang.org/cl/34660046
This commit is contained in:
Robert Griesemer 2014-01-03 09:12:32 -08:00
parent 53273c1d55
commit 6770b5cdca
3 changed files with 27 additions and 36 deletions

View File

@ -65,10 +65,8 @@ const (
func ExportData(pkg *types.Package) []byte { func ExportData(pkg *types.Package) []byte {
p := exporter{ p := exporter{
data: []byte(magic), data: []byte(magic),
// TODO(gri) If we can't have nil packages pkgIndex: make(map[*types.Package]int),
// or types, remove nil entries at index 0. typIndex: make(map[types.Type]int),
pkgIndex: map[*types.Package]int{nil: 0},
typIndex: map[types.Type]int{nil: 0},
} }
// populate typIndex with predeclared types // populate typIndex with predeclared types
@ -119,6 +117,10 @@ func (p *exporter) pkg(pkg *types.Package) {
defer p.tracef("} ") defer p.tracef("} ")
} }
if pkg == nil {
panic("unexpected nil pkg")
}
// if the package was seen before, write its index (>= 0) // if the package was seen before, write its index (>= 0)
if i, ok := p.pkgIndex[pkg]; ok { if i, ok := p.pkgIndex[pkg]; ok {
p.int(i) p.int(i)
@ -368,15 +370,23 @@ func (p *exporter) signature(sig *types.Signature) {
// for interface methods if we flatten them // for interface methods if we flatten them
// out. If we track embedded types instead, // out. If we track embedded types instead,
// the information is already present. // 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 { if recv := sig.Recv(); recv != nil {
p.bool(true) // 1-element tuple
p.int(1)
p.param(recv) p.param(recv)
} else { } else {
p.bool(false) // 0-element tuple
p.int(0)
} }
p.tuple(sig.Params()) p.tuple(sig.Params())
p.tuple(sig.Results()) 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) { func (p *exporter) param(v *types.Var) {
@ -395,17 +405,8 @@ func (p *exporter) tuple(t *types.Tuple) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// encoders // encoders
func (p *exporter) bool(b bool) {
var x int64
if b {
x = 1
}
p.int64(x)
}
func (p *exporter) string(s string) { func (p *exporter) string(s string) {
// TODO(gri) consider inlining this to avoid an extra allocation p.bytes([]byte(s)) // (could be inlined if extra allocation matters)
p.bytes([]byte(s))
} }
func (p *exporter) int(x int) { func (p *exporter) int(x int) {

View File

@ -31,8 +31,6 @@ func ImportData(imports map[string]*types.Package, data []byte) (*types.Package,
p := importer{ p := importer{
data: data[len(magic):], data: data[len(magic):],
imports: imports, imports: imports,
pkgList: []*types.Package{nil},
typList: []types.Type{nil},
consumed: len(magic), // for debugging only consumed: len(magic), // for debugging only
} }
@ -47,8 +45,8 @@ func ImportData(imports map[string]*types.Package, data []byte) (*types.Package,
} }
pkg := p.pkg() pkg := p.pkg()
if debug && p.pkgList[1] != pkg { if debug && p.pkgList[0] != pkg {
panic("imported packaged not found in pkgList[1]") panic("imported packaged not found in pkgList[0]")
} }
// read objects // read objects
@ -337,7 +335,6 @@ func (p *importer) field() *types.Var {
name = typ.Name() name = typ.Name()
case *types.Named: case *types.Named:
obj := typ.Obj() obj := typ.Obj()
pkg = obj.Pkg() // TODO(gri) is this still correct?
name = obj.Name() name = obj.Name()
default: default:
panic("anonymous field expected") panic("anonymous field expected")
@ -350,22 +347,19 @@ func (p *importer) field() *types.Var {
func (p *importer) qualifiedName() (*types.Package, string) { func (p *importer) qualifiedName() (*types.Package, string) {
name := p.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) { if !exported(name) {
pkg = p.pkg() pkg = p.pkg()
if pkg == nil {
panic(fmt.Sprintf("nil package for unexported qualified name %q", name))
}
} }
return pkg, name return pkg, name
} }
func (p *importer) signature() *types.Signature { func (p *importer) signature() *types.Signature {
var recv *types.Var var recv *types.Var
if p.bool() { if p.int() != 0 {
recv = p.param() 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 { func (p *importer) param() *types.Var {
@ -383,10 +377,6 @@ func (p *importer) tuple() *types.Tuple {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// decoders // decoders
func (p *importer) bool() bool {
return p.int64() != 0
}
func (p *importer) string() string { func (p *importer) string() string {
return string(p.bytes()) return string(p.bytes())
} }

View File

@ -105,7 +105,7 @@ func TestImportStdLib(t *testing.T) {
// can be compared reasonably well // can be compared reasonably well
types.GcCompatibilityMode = true types.GcCompatibilityMode = true
var totSize, totGcsize int var totSize, totGcSize int
for _, lib := range libs { for _, lib := range libs {
// limit run time for short tests // limit run time for short tests
if testing.Short() && time.Since(start) >= 750*time.Millisecond { 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))) fmt.Printf("%s\t%d\t%d\t%d%%\n", lib, size, gcsize, int(float64(size)*100/float64(gcsize)))
} }
totSize += size totSize += size
totGcsize += gcsize totGcSize += gcsize
} }
if testing.Verbose() { 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 types.GcCompatibilityMode = false