go.tools/go/importer: more cleanups
R=adonovan CC=golang-codereviews https://golang.org/cl/34660046
This commit is contained in:
parent
53273c1d55
commit
6770b5cdca
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue