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.
14 Path string // string literal used in import statement, e.g. "runtime/internal/sys"
15 Name string // package name, e.g. "sys"
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
23 var PkgMap = make(map[string]*Pkg)
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))
40 p.Prefix = objabi.PathToPrefix(path)
41 p.Syms = make(map[string]*Sym)
43 PkgList = append(PkgList, p)
49 Syms: make(map[string]*Sym),
52 func (pkg *Pkg) Lookup(name string) *Sym {
53 s, _ := pkg.LookupOK(name)
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
65 if s := pkg.Syms[name]; s != nil {
74 InitSyms = append(InitSyms, s)
80 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
81 // TODO(gri) remove this check in favor of specialized lookup
85 if s := pkg.Syms[string(name)]; s != nil {
88 str := InternString(name)
89 return pkg.Lookup(str)
92 var internedStrings = map[string]string{}
94 func InternString(b []byte) string {
95 s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
98 internedStrings[s] = s