aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types/pkg.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2017-04-19 11:32:09 -0700
committerRobert Griesemer <gri@golang.org>2017-04-19 21:19:29 +0000
commit39a132cb2fe448079324db3c77f15c46a43c8d86 (patch)
tree894102decc6b78cb5f9762dcd840224b23946949 /src/cmd/compile/internal/types/pkg.go
parent62a2bee7a5706e9b37a6bdff3ee2cc422912f463 (diff)
downloadgo-39a132cb2fe448079324db3c77f15c46a43c8d86.tar.gz
go-39a132cb2fe448079324db3c77f15c46a43c8d86.zip
cmd/compile/internal/types: unexport PkgMap, remove PkgList
- PkgMap was only needed to test import/export in a "cleanroom" environment, with debugFormat set. Provided helper function instead. - PkgList was only used to identify directly imported packages. Instead, compute that list explicitly from the package map. It happens only once, the list is small, and it's more robust than keeping two data structures in sync. Change-Id: I82dce3c0b5cb816faae58708e877799359c20fcb Reviewed-on: https://go-review.googlesource.com/41078 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types/pkg.go')
-rw-r--r--src/cmd/compile/internal/types/pkg.go40
1 files changed, 34 insertions, 6 deletions
diff --git a/src/cmd/compile/internal/types/pkg.go b/src/cmd/compile/internal/types/pkg.go
index b43f13e5d1..1fe49bd142 100644
--- a/src/cmd/compile/internal/types/pkg.go
+++ b/src/cmd/compile/internal/types/pkg.go
@@ -8,8 +8,12 @@ import (
"cmd/internal/obj"
"cmd/internal/objabi"
"fmt"
+ "sort"
)
+// pkgMap maps a package path to a package.
+var pkgMap = make(map[string]*Pkg)
+
type Pkg struct {
Path string // string literal used in import statement, e.g. "runtime/internal/sys"
Name string // package name, e.g. "sys"
@@ -20,14 +24,11 @@ type Pkg struct {
Syms map[string]*Sym
}
-var PkgMap = make(map[string]*Pkg)
-var PkgList []*Pkg
-
// NewPkg returns a new Pkg for the given package path and name.
// Unless name is the empty string, if the package exists already,
// the existing package name and the provided name must match.
func NewPkg(path, name string) *Pkg {
- if p := PkgMap[path]; p != nil {
+ if p := pkgMap[path]; p != nil {
if name != "" && p.Name != name {
panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
}
@@ -39,12 +40,30 @@ func NewPkg(path, name string) *Pkg {
p.Name = name
p.Prefix = objabi.PathToPrefix(path)
p.Syms = make(map[string]*Sym)
- PkgMap[path] = p
- PkgList = append(PkgList, p)
+ pkgMap[path] = p
return p
}
+// ImportedPkgList returns the list of directly imported packages.
+// The list is sorted by package path.
+func ImportedPkgList() []*Pkg {
+ var list []*Pkg
+ for _, p := range pkgMap {
+ if p.Direct {
+ list = append(list, p)
+ }
+ }
+ sort.Sort(byPath(list))
+ return list
+}
+
+type byPath []*Pkg
+
+func (a byPath) Len() int { return len(a) }
+func (a byPath) Less(i, j int) bool { return a[i].Path < a[j].Path }
+func (a byPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+
var nopkg = &Pkg{
Syms: make(map[string]*Sym),
}
@@ -99,3 +118,12 @@ func InternString(b []byte) string {
}
return s
}
+
+// CleanroomDo invokes f in an environment with with no preexisting packages.
+// For testing of import/export only.
+func CleanroomDo(f func()) {
+ saved := pkgMap
+ pkgMap = make(map[string]*Pkg)
+ f()
+ pkgMap = saved
+}