Commit eb79f21c authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile, go/importer: minor cleanups

Change-Id: Ic7a1fb0dbbf108052c970a4a830269a5673df7df
Reviewed-on: https://go-review.googlesource.com/21963Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent 7d0d1222
...@@ -59,9 +59,8 @@ Encoding format: ...@@ -59,9 +59,8 @@ Encoding format:
The export data starts with a single byte indicating the encoding format The export data starts with a single byte indicating the encoding format
(compact, or with debugging information), followed by a version string (compact, or with debugging information), followed by a version string
(so we can evolve the encoding if need be), the name of the imported (so we can evolve the encoding if need be), and then the package object
package, and a string containing platform-specific information for that for the exported package (with an empty path).
package.
After this header, two lists of objects and the list of inlined function After this header, two lists of objects and the list of inlined function
bodies follows. bodies follows.
......
...@@ -59,9 +59,6 @@ func Import(in *bufio.Reader) { ...@@ -59,9 +59,6 @@ func Import(in *bufio.Reader) {
// read package data // read package data
p.pkg() p.pkg()
if p.pkgList[0] != importpkg {
Fatalf("importer: imported package not found in pkgList[0]")
}
// defer some type-checking until all types are read in completely // defer some type-checking until all types are read in completely
// (parser.go:import_package) // (parser.go:import_package)
...@@ -193,7 +190,12 @@ func (p *importer) pkg() *Pkg { ...@@ -193,7 +190,12 @@ func (p *importer) pkg() *Pkg {
Fatalf("importer: bad path in import: %q", path) Fatalf("importer: bad path in import: %q", path)
} }
// an empty path denotes the package we are currently importing // an empty path denotes the package we are currently importing;
// it must be the first package we see
if (path == "") != (len(p.pkgList) == 0) {
panic(fmt.Sprintf("package path %q for pkg index %d", path, len(p.pkgList)))
}
pkg := importpkg pkg := importpkg
if path != "" { if path != "" {
pkg = mkpkg(path) pkg = mkpkg(path)
......
...@@ -16,12 +16,15 @@ import ( ...@@ -16,12 +16,15 @@ import (
) )
type importer struct { type importer struct {
imports map[string]*types.Package imports map[string]*types.Package
data []byte data []byte
path string
buf []byte // for reading strings buf []byte // for reading strings
bufarray [64]byte // initial underlying array for buf, large enough to avoid allocation when compiling std lib bufarray [64]byte // initial underlying array for buf, large enough to avoid allocation when compiling std lib
pkgList []*types.Package
typList []types.Type pkgList []*types.Package
typList []types.Type
debugFormat bool debugFormat bool
read int // bytes read read int // bytes read
...@@ -35,6 +38,7 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i ...@@ -35,6 +38,7 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i
p := importer{ p := importer{
imports: imports, imports: imports,
data: data, data: data,
path: path,
} }
p.buf = p.bufarray[:] p.buf = p.bufarray[:]
...@@ -58,25 +62,7 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i ...@@ -58,25 +62,7 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i
p.typList = append(p.typList, predeclared...) p.typList = append(p.typList, predeclared...)
// read package data // read package data
// TODO(gri) clean this up pkg := p.pkg()
i := p.tagOrIndex()
if i != packageTag {
panic(fmt.Sprintf("package tag expected, got %d", i))
}
name := p.string()
if s := p.string(); s != "" {
panic(fmt.Sprintf("empty path expected, got %s", s))
}
pkg := p.imports[path]
if pkg == nil {
pkg = types.NewPackage(path, name)
p.imports[path] = pkg
}
p.pkgList = append(p.pkgList, pkg)
if debug && p.pkgList[0] != pkg {
panic("imported packaged not found in pkgList[0]")
}
// read objects of phase 1 only (see cmd/compiler/internal/gc/bexport.go) // read objects of phase 1 only (see cmd/compiler/internal/gc/bexport.go)
objcount := 0 objcount := 0
...@@ -91,7 +77,7 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i ...@@ -91,7 +77,7 @@ func BImportData(imports map[string]*types.Package, data []byte, path string) (i
// self-verification // self-verification
if count := p.int(); count != objcount { if count := p.int(); count != objcount {
panic(fmt.Sprintf("importer: got %d objects; want %d", objcount, count)) panic(fmt.Sprintf("got %d objects; want %d", objcount, count))
} }
// ignore compiler-specific import data // ignore compiler-specific import data
...@@ -135,16 +121,22 @@ func (p *importer) pkg() *types.Package { ...@@ -135,16 +121,22 @@ func (p *importer) pkg() *types.Package {
panic("empty package name in import") panic("empty package name in import")
} }
// we should never see an empty import path // an empty path denotes the package we are currently importing;
if path == "" { // it must be the first package we see
panic("empty import path") if (path == "") != (len(p.pkgList) == 0) {
panic(fmt.Sprintf("package path %q for pkg index %d", path, len(p.pkgList)))
} }
// if the package was imported before, use that one; otherwise create a new one // if the package was imported before, use that one; otherwise create a new one
if path == "" {
path = p.path
}
pkg := p.imports[path] pkg := p.imports[path]
if pkg == nil { if pkg == nil {
pkg = types.NewPackage(path, name) pkg = types.NewPackage(path, name)
p.imports[path] = pkg p.imports[path] = pkg
} else if pkg.Name() != name {
panic(fmt.Sprintf("conflicting names %s and %s for package %q", pkg.Name(), name, path))
} }
p.pkgList = append(p.pkgList, pkg) p.pkgList = append(p.pkgList, pkg)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment