...
1
2
3
4
5 package types
6
7 import (
8 "cmd/internal/obj"
9 "cmd/internal/objabi"
10 "fmt"
11 "strconv"
12 "sync"
13 )
14
15
16 var pkgMap = make(map[string]*Pkg)
17
18 type Pkg struct {
19 Path string
20 Name string
21 Prefix string
22 Syms map[string]*Sym
23 Pathsym *obj.LSym
24
25 Direct bool
26 }
27
28
29
30
31 func NewPkg(path, name string) *Pkg {
32 if p := pkgMap[path]; p != nil {
33 if name != "" && p.Name != name {
34 panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
35 }
36 return p
37 }
38
39 p := new(Pkg)
40 p.Path = path
41 p.Name = name
42 if path == "go.shape" {
43
44
45
46
47 p.Prefix = path
48 } else {
49 p.Prefix = objabi.PathToPrefix(path)
50 }
51 p.Syms = make(map[string]*Sym)
52 pkgMap[path] = p
53
54 return p
55 }
56
57 func PkgMap() map[string]*Pkg {
58 return pkgMap
59 }
60
61 var nopkg = &Pkg{
62 Syms: make(map[string]*Sym),
63 }
64
65 func (pkg *Pkg) Lookup(name string) *Sym {
66 s, _ := pkg.LookupOK(name)
67 return s
68 }
69
70
71 func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
72
73 if pkg == nil {
74 pkg = nopkg
75 }
76 if s := pkg.Syms[name]; s != nil {
77 return s, true
78 }
79
80 s = &Sym{
81 Name: name,
82 Pkg: pkg,
83 }
84 pkg.Syms[name] = s
85 return s, false
86 }
87
88 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
89
90 if pkg == nil {
91 pkg = nopkg
92 }
93 if s := pkg.Syms[string(name)]; s != nil {
94 return s
95 }
96 str := InternString(name)
97 return pkg.Lookup(str)
98 }
99
100
101
102 func (pkg *Pkg) LookupNum(prefix string, n int) *Sym {
103 var buf [20]byte
104 copy(buf[:], prefix)
105 b := strconv.AppendInt(buf[:len(prefix)], int64(n), 10)
106 return pkg.LookupBytes(b)
107 }
108
109
110 func (pkg *Pkg) Selector(name string) *Sym {
111 if IsExported(name) {
112 pkg = LocalPkg
113 }
114 return pkg.Lookup(name)
115 }
116
117 var (
118 internedStringsmu sync.Mutex
119 internedStrings = map[string]string{}
120 )
121
122 func InternString(b []byte) string {
123 internedStringsmu.Lock()
124 s, ok := internedStrings[string(b)]
125 if !ok {
126 s = string(b)
127 internedStrings[s] = s
128 }
129 internedStringsmu.Unlock()
130 return s
131 }
132
View as plain text