]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types/pkg.go
cmd/compile: simplify code from CL 398474
[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 // MaxPkgHeight is a height greater than any likely package height.
20 const MaxPkgHeight = 1e9
21
22 type Pkg struct {
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
26         Syms    map[string]*Sym
27         Pathsym *obj.LSym
28
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.
33         Height int
34
35         Direct bool // imported directly
36 }
37
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))
45                 }
46                 return p
47         }
48
49         p := new(Pkg)
50         p.Path = path
51         p.Name = name
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
56                 // instantiations.
57                 p.Prefix = path
58         } else {
59                 p.Prefix = objabi.PathToPrefix(path)
60         }
61         p.Syms = make(map[string]*Sym)
62         pkgMap[path] = p
63
64         return p
65 }
66
67 // ImportedPkgList returns the list of directly imported packages.
68 // The list is sorted by package path.
69 func ImportedPkgList() []*Pkg {
70         var list []*Pkg
71         for _, p := range pkgMap {
72                 if p.Direct {
73                         list = append(list, p)
74                 }
75         }
76         sort.Sort(byPath(list))
77         return list
78 }
79
80 type byPath []*Pkg
81
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] }
85
86 var nopkg = &Pkg{
87         Syms: make(map[string]*Sym),
88 }
89
90 func (pkg *Pkg) Lookup(name string) *Sym {
91         s, _ := pkg.LookupOK(name)
92         return s
93 }
94
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
98         if pkg == nil {
99                 pkg = nopkg
100         }
101         if s := pkg.Syms[name]; s != nil {
102                 return s, true
103         }
104
105         s = &Sym{
106                 Name: name,
107                 Pkg:  pkg,
108         }
109         pkg.Syms[name] = s
110         return s, false
111 }
112
113 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
114         // TODO(gri) remove this check in favor of specialized lookup
115         if pkg == nil {
116                 pkg = nopkg
117         }
118         if s := pkg.Syms[string(name)]; s != nil {
119                 return s
120         }
121         str := InternString(name)
122         return pkg.Lookup(str)
123 }
124
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
129         copy(buf[:], prefix)
130         b := strconv.AppendInt(buf[:len(prefix)], int64(n), 10)
131         return pkg.LookupBytes(b)
132 }
133
134 var (
135         internedStringsmu sync.Mutex // protects internedStrings
136         internedStrings   = map[string]string{}
137 )
138
139 func InternString(b []byte) string {
140         internedStringsmu.Lock()
141         s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
142         if !ok {
143                 s = string(b)
144                 internedStrings[s] = s
145         }
146         internedStringsmu.Unlock()
147         return s
148 }
149
150 // CleanroomDo invokes f in an environment with no preexisting packages.
151 // For testing of import/export only.
152 func CleanroomDo(f func()) {
153         saved := pkgMap
154         pkgMap = make(map[string]*Pkg)
155         f()
156         pkgMap = saved
157 }