]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/types/pkg.go
cmd/compile/internal/types: don't export Nopkg anymore
[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 )
12
13 type Pkg struct {
14         Path     string // string literal used in import statement, e.g. "runtime/internal/sys"
15         Name     string // package name, e.g. "sys"
16         Pathsym  *obj.LSym
17         Prefix   string // escaped path for use in symbol table
18         Imported bool   // export data of this package was parsed
19         Direct   bool   // imported directly
20         Syms     map[string]*Sym
21 }
22
23 var PkgMap = make(map[string]*Pkg)
24 var PkgList []*Pkg
25
26 // NewPkg returns a new Pkg for the given package path and name.
27 // Unless name is the empty string, if the package exists already,
28 // the existing package name and the provided name must match.
29 func NewPkg(path, name string) *Pkg {
30         if p := PkgMap[path]; p != nil {
31                 if name != "" && p.Name != name {
32                         panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
33                 }
34                 return p
35         }
36
37         p := new(Pkg)
38         p.Path = path
39         p.Name = name
40         p.Prefix = objabi.PathToPrefix(path)
41         p.Syms = make(map[string]*Sym)
42         PkgMap[path] = p
43         PkgList = append(PkgList, p)
44
45         return p
46 }
47
48 var nopkg = &Pkg{
49         Syms: make(map[string]*Sym),
50 }
51
52 func (pkg *Pkg) Lookup(name string) *Sym {
53         s, _ := pkg.LookupOK(name)
54         return s
55 }
56
57 var InitSyms []*Sym
58
59 // LookupOK looks up name in pkg and reports whether it previously existed.
60 func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
61         // TODO(gri) remove this check in favor of specialized lookup
62         if pkg == nil {
63                 pkg = nopkg
64         }
65         if s := pkg.Syms[name]; s != nil {
66                 return s, true
67         }
68
69         s = &Sym{
70                 Name: name,
71                 Pkg:  pkg,
72         }
73         if name == "init" {
74                 InitSyms = append(InitSyms, s)
75         }
76         pkg.Syms[name] = s
77         return s, false
78 }
79
80 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
81         // TODO(gri) remove this check in favor of specialized lookup
82         if pkg == nil {
83                 pkg = nopkg
84         }
85         if s := pkg.Syms[string(name)]; s != nil {
86                 return s
87         }
88         str := InternString(name)
89         return pkg.Lookup(str)
90 }
91
92 var internedStrings = map[string]string{}
93
94 func InternString(b []byte) string {
95         s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
96         if !ok {
97                 s = string(b)
98                 internedStrings[s] = s
99         }
100         return s
101 }