]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types/pkg.go
[dev.unified] all: merge master (5a1c5b8) into dev.unified
[gostls13.git] / src / cmd / compile / internal / types / pkg.go
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.
4
5 package types
6
7 import (
8         "cmd/internal/obj"
9         "cmd/internal/objabi"
10         "fmt"
11         "sort"
12         "strconv"
13         "sync"
14 )
15
16 // pkgMap maps a package path to a package.
17 var pkgMap = make(map[string]*Pkg)
18
19 type Pkg struct {
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
23         Syms    map[string]*Sym
24         Pathsym *obj.LSym
25
26         Direct bool // imported directly
27 }
28
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))
36                 }
37                 return p
38         }
39
40         p := new(Pkg)
41         p.Path = path
42         p.Name = name
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
47                 // instantiations.
48                 p.Prefix = path
49         } else {
50                 p.Prefix = objabi.PathToPrefix(path)
51         }
52         p.Syms = make(map[string]*Sym)
53         pkgMap[path] = p
54
55         return p
56 }
57
58 // ImportedPkgList returns the list of directly imported packages.
59 // The list is sorted by package path.
60 func ImportedPkgList() []*Pkg {
61         var list []*Pkg
62         for _, p := range pkgMap {
63                 if p.Direct {
64                         list = append(list, p)
65                 }
66         }
67         sort.Sort(byPath(list))
68         return list
69 }
70
71 type byPath []*Pkg
72
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] }
76
77 var nopkg = &Pkg{
78         Syms: make(map[string]*Sym),
79 }
80
81 func (pkg *Pkg) Lookup(name string) *Sym {
82         s, _ := pkg.LookupOK(name)
83         return s
84 }
85
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
89         if pkg == nil {
90                 pkg = nopkg
91         }
92         if s := pkg.Syms[name]; s != nil {
93                 return s, true
94         }
95
96         s = &Sym{
97                 Name: name,
98                 Pkg:  pkg,
99         }
100         pkg.Syms[name] = s
101         return s, false
102 }
103
104 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
105         // TODO(gri) remove this check in favor of specialized lookup
106         if pkg == nil {
107                 pkg = nopkg
108         }
109         if s := pkg.Syms[string(name)]; s != nil {
110                 return s
111         }
112         str := InternString(name)
113         return pkg.Lookup(str)
114 }
115
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
120         copy(buf[:], prefix)
121         b := strconv.AppendInt(buf[:len(prefix)], int64(n), 10)
122         return pkg.LookupBytes(b)
123 }
124
125 var (
126         internedStringsmu sync.Mutex // protects internedStrings
127         internedStrings   = map[string]string{}
128 )
129
130 func InternString(b []byte) string {
131         internedStringsmu.Lock()
132         s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
133         if !ok {
134                 s = string(b)
135                 internedStrings[s] = s
136         }
137         internedStringsmu.Unlock()
138         return s
139 }
140
141 // CleanroomDo invokes f in an environment with no preexisting packages.
142 // For testing of import/export only.
143 func CleanroomDo(f func()) {
144         saved := pkgMap
145         pkgMap = make(map[string]*Pkg)
146         f()
147         pkgMap = saved
148 }