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)
20 Path string // string literal used in import statement, e.g. "runtime/internal/sys"
21 Name string // package name, e.g. "sys"
22 Prefix string // escaped path for use in symbol table
26 Direct bool // imported directly
29 // NewPkg returns a new Pkg for the given package path and name.
30 // Unless name is the empty string, if the package exists already,
31 // the existing package name and the provided name must match.
32 func NewPkg(path, name string) *Pkg {
33 if p := pkgMap[path]; p != nil {
34 if name != "" && p.Name != name {
35 panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
43 if path == "go.shape" {
44 // Don't escape "go.shape", since it's not needed (it's a builtin
45 // package), and we don't want escape codes showing up in shape type
46 // names, which also appear in names of function/method
50 p.Prefix = objabi.PathToPrefix(path)
52 p.Syms = make(map[string]*Sym)
58 // ImportedPkgList returns the list of directly imported packages.
59 // The list is sorted by package path.
60 func ImportedPkgList() []*Pkg {
62 for _, p := range pkgMap {
64 list = append(list, p)
67 sort.Sort(byPath(list))
73 func (a byPath) Len() int { return len(a) }
74 func (a byPath) Less(i, j int) bool { return a[i].Path < a[j].Path }
75 func (a byPath) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
78 Syms: make(map[string]*Sym),
81 func (pkg *Pkg) Lookup(name string) *Sym {
82 s, _ := pkg.LookupOK(name)
86 // LookupOK looks up name in pkg and reports whether it previously existed.
87 func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
88 // TODO(gri) remove this check in favor of specialized lookup
92 if s := pkg.Syms[name]; s != nil {
104 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
105 // TODO(gri) remove this check in favor of specialized lookup
109 if s := pkg.Syms[string(name)]; s != nil {
112 str := InternString(name)
113 return pkg.Lookup(str)
116 // LookupNum looks up the symbol starting with prefix and ending with
117 // the decimal n. If prefix is too long, LookupNum panics.
118 func (pkg *Pkg) LookupNum(prefix string, n int) *Sym {
119 var buf [20]byte // plenty long enough for all current users
121 b := strconv.AppendInt(buf[:len(prefix)], int64(n), 10)
122 return pkg.LookupBytes(b)
126 internedStringsmu sync.Mutex // protects internedStrings
127 internedStrings = map[string]string{}
130 func InternString(b []byte) string {
131 internedStringsmu.Lock()
132 s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
135 internedStrings[s] = s
137 internedStringsmu.Unlock()
141 // CleanroomDo invokes f in an environment with no preexisting packages.
142 // For testing of import/export only.
143 func CleanroomDo(f func()) {
145 pkgMap = make(map[string]*Pkg)