/
pkg.go
135 lines (116 loc) · 3.06 KB
/
pkg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import (
"cmd/internal/obj"
"cmd/internal/objabi"
"fmt"
"sort"
"sync"
)
// 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"
Pathsym *obj.LSym
Prefix string // escaped path for use in symbol table
Imported bool // export data of this package was parsed
Direct bool // imported directly
Syms map[string]*Sym
}
// 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 name != "" && p.Name != name {
panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
}
return p
}
p := new(Pkg)
p.Path = path
p.Name = name
p.Prefix = objabi.PathToPrefix(path)
p.Syms = make(map[string]*Sym)
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),
}
func (pkg *Pkg) Lookup(name string) *Sym {
s, _ := pkg.LookupOK(name)
return s
}
var InitSyms []*Sym
// LookupOK looks up name in pkg and reports whether it previously existed.
func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
// TODO(gri) remove this check in favor of specialized lookup
if pkg == nil {
pkg = nopkg
}
if s := pkg.Syms[name]; s != nil {
return s, true
}
s = &Sym{
Name: name,
Pkg: pkg,
}
if name == "init" {
InitSyms = append(InitSyms, s)
}
pkg.Syms[name] = s
return s, false
}
func (pkg *Pkg) LookupBytes(name []byte) *Sym {
// TODO(gri) remove this check in favor of specialized lookup
if pkg == nil {
pkg = nopkg
}
if s := pkg.Syms[string(name)]; s != nil {
return s
}
str := InternString(name)
return pkg.Lookup(str)
}
var (
internedStringsmu sync.Mutex // protects internedStrings
internedStrings = map[string]string{}
)
func InternString(b []byte) string {
internedStringsmu.Lock()
s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
if !ok {
s = string(b)
internedStrings[s] = s
}
internedStringsmu.Unlock()
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
}