]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types/pkg.go
cmd/compile: move typepkg back to gc package (cleanup)
[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 )
13
14 // pkgMap maps a package path to a package.
15 var pkgMap = make(map[string]*Pkg)
16
17 type Pkg struct {
18         Path     string // string literal used in import statement, e.g. "runtime/internal/sys"
19         Name     string // package name, e.g. "sys"
20         Pathsym  *obj.LSym
21         Prefix   string // escaped path for use in symbol table
22         Imported bool   // export data of this package was parsed
23         Direct   bool   // imported directly
24         Syms     map[string]*Sym
25 }
26
27 // NewPkg returns a new Pkg for the given package path and name.
28 // Unless name is the empty string, if the package exists already,
29 // the existing package name and the provided name must match.
30 func NewPkg(path, name string) *Pkg {
31         if p := pkgMap[path]; p != nil {
32                 if name != "" && p.Name != name {
33                         panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
34                 }
35                 return p
36         }
37
38         p := new(Pkg)
39         p.Path = path
40         p.Name = name
41         p.Prefix = objabi.PathToPrefix(path)
42         p.Syms = make(map[string]*Sym)
43         pkgMap[path] = p
44
45         return p
46 }
47
48 // ImportedPkgList returns the list of directly imported packages.
49 // The list is sorted by package path.
50 func ImportedPkgList() []*Pkg {
51         var list []*Pkg
52         for _, p := range pkgMap {
53                 if p.Direct {
54                         list = append(list, p)
55                 }
56         }
57         sort.Sort(byPath(list))
58         return list
59 }
60
61 type byPath []*Pkg
62
63 func (a byPath) Len() int           { return len(a) }
64 func (a byPath) Less(i, j int) bool { return a[i].Path < a[j].Path }
65 func (a byPath) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
66
67 var nopkg = &Pkg{
68         Syms: make(map[string]*Sym),
69 }
70
71 func (pkg *Pkg) Lookup(name string) *Sym {
72         s, _ := pkg.LookupOK(name)
73         return s
74 }
75
76 var InitSyms []*Sym
77
78 // LookupOK looks up name in pkg and reports whether it previously existed.
79 func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
80         // TODO(gri) remove this check in favor of specialized lookup
81         if pkg == nil {
82                 pkg = nopkg
83         }
84         if s := pkg.Syms[name]; s != nil {
85                 return s, true
86         }
87
88         s = &Sym{
89                 Name: name,
90                 Pkg:  pkg,
91         }
92         if name == "init" {
93                 InitSyms = append(InitSyms, s)
94         }
95         pkg.Syms[name] = s
96         return s, false
97 }
98
99 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
100         // TODO(gri) remove this check in favor of specialized lookup
101         if pkg == nil {
102                 pkg = nopkg
103         }
104         if s := pkg.Syms[string(name)]; s != nil {
105                 return s
106         }
107         str := InternString(name)
108         return pkg.Lookup(str)
109 }
110
111 var internedStrings = map[string]string{}
112
113 func InternString(b []byte) string {
114         s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
115         if !ok {
116                 s = string(b)
117                 internedStrings[s] = s
118         }
119         return s
120 }
121
122 // CleanroomDo invokes f in an environment with with no preexisting packages.
123 // For testing of import/export only.
124 func CleanroomDo(f func()) {
125         saved := pkgMap
126         pkgMap = make(map[string]*Pkg)
127         f()
128         pkgMap = saved
129 }