1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 // pkgMap maps a package path to a package.
17 var pkgMap = make(map[string]*Pkg)
19 // MaxPkgHeight is a height greater than any likely package height.
20 const MaxPkgHeight = 1e9
23 Path string // string literal used in import statement, e.g. "runtime/internal/sys"
24 Name string // package name, e.g. "sys"
25 Prefix string // escaped path for use in symbol table
29 // Height is the package's height in the import graph. Leaf
30 // packages (i.e., packages with no imports) have height 0,
31 // and all other packages have height 1 plus the maximum
32 // height of their imported packages.
35 Direct bool // imported directly
38 // NewPkg returns a new Pkg for the given package path and name.
39 // Unless name is the empty string, if the package exists already,
40 // the existing package name and the provided name must match.
41 func NewPkg(path, name string) *Pkg {
42 if p := pkgMap[path]; p != nil {
43 if name != "" && p.Name != name {
44 panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
52 if path == "go.shape" {
53 // Don't escape "go.shape", since it's not needed (it's a builtin
54 // package), and we don't want escape codes showing up in shape type
55 // names, which also appear in names of function/method
59 p.Prefix = objabi.PathToPrefix(path)
61 p.Syms = make(map[string]*Sym)
67 // ImportedPkgList returns the list of directly imported packages.
68 // The list is sorted by package path.
69 func ImportedPkgList() []*Pkg {
71 for _, p := range pkgMap {
73 list = append(list, p)
76 sort.Sort(byPath(list))
82 func (a byPath) Len() int { return len(a) }
83 func (a byPath) Less(i, j int) bool { return a[i].Path < a[j].Path }
84 func (a byPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
87 Syms: make(map[string]*Sym),
90 func (pkg *Pkg) Lookup(name string) *Sym {
91 s, _ := pkg.LookupOK(name)
95 // LookupOK looks up name in pkg and reports whether it previously existed.
96 func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
97 // TODO(gri) remove this check in favor of specialized lookup
101 if s := pkg.Syms[name]; s != nil {
113 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
114 // TODO(gri) remove this check in favor of specialized lookup
118 if s := pkg.Syms[string(name)]; s != nil {
121 str := InternString(name)
122 return pkg.Lookup(str)
125 // LookupNum looks up the symbol starting with prefix and ending with
126 // the decimal n. If prefix is too long, LookupNum panics.
127 func (pkg *Pkg) LookupNum(prefix string, n int) *Sym {
128 var buf [20]byte // plenty long enough for all current users
130 b := strconv.AppendInt(buf[:len(prefix)], int64(n), 10)
131 return pkg.LookupBytes(b)
135 internedStringsmu sync.Mutex // protects internedStrings
136 internedStrings = map[string]string{}
139 func InternString(b []byte) string {
140 internedStringsmu.Lock()
141 s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
144 internedStrings[s] = s
146 internedStringsmu.Unlock()
150 // CleanroomDo invokes f in an environment with no preexisting packages.
151 // For testing of import/export only.
152 func CleanroomDo(f func()) {
154 pkgMap = make(map[string]*Pkg)