]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/cmd/compile/internal/types/pkg.go
cmd/compile/internal/types: remove dead function
[gostls13.git] / src / cmd / compile / internal / types / pkg.go
index 46a5606fc2807f58a192cbad3bfd161ee32e2ce2..c6ce7889afbb7bd7abc4d32a30e53278bda7b44f 100644 (file)
@@ -8,26 +8,28 @@ import (
        "cmd/internal/obj"
        "cmd/internal/objabi"
        "fmt"
+       "strconv"
+       "sync"
 )
 
+// pkgMap maps a package path to a package.
+var pkgMap = make(map[string]*Pkg)
+
 type Pkg struct {
-       Path     string // string literal used in import statement, e.g. "runtime/internal/sys"
-       Name     string // package name, e.g. "sys"
-       Pathsym  *obj.LSym
-       Prefix   string // escaped path for use in symbol table
-       Imported bool   // export data of this package was parsed
-       Direct   bool   // imported directly
-       Syms     map[string]*Sym
-}
+       Path    string // string literal used in import statement, e.g. "runtime/internal/sys"
+       Name    string // package name, e.g. "sys"
+       Prefix  string // escaped path for use in symbol table
+       Syms    map[string]*Sym
+       Pathsym *obj.LSym
 
-var PkgMap = make(map[string]*Pkg)
-var PkgList []*Pkg
+       Direct bool // imported directly
+}
 
 // NewPkg returns a new Pkg for the given package path and name.
 // Unless name is the empty string, if the package exists already,
 // the existing package name and the provided name must match.
 func NewPkg(path, name string) *Pkg {
-       if p := PkgMap[path]; p != nil {
+       if p := pkgMap[path]; p != nil {
                if name != "" && p.Name != name {
                        panic(fmt.Sprintf("conflicting package names %s and %s for path %q", p.Name, name, path))
                }
@@ -37,15 +39,26 @@ func NewPkg(path, name string) *Pkg {
        p := new(Pkg)
        p.Path = path
        p.Name = name
-       p.Prefix = objabi.PathToPrefix(path)
+       if path == "go.shape" {
+               // Don't escape "go.shape", since it's not needed (it's a builtin
+               // package), and we don't want escape codes showing up in shape type
+               // names, which also appear in names of function/method
+               // instantiations.
+               p.Prefix = path
+       } else {
+               p.Prefix = objabi.PathToPrefix(path)
+       }
        p.Syms = make(map[string]*Sym)
-       PkgMap[path] = p
-       PkgList = append(PkgList, p)
+       pkgMap[path] = p
 
        return p
 }
 
-var Nopkg = &Pkg{
+func PkgMap() map[string]*Pkg {
+       return pkgMap
+}
+
+var nopkg = &Pkg{
        Syms: make(map[string]*Sym),
 }
 
@@ -54,12 +67,11 @@ func (pkg *Pkg) Lookup(name string) *Sym {
        return s
 }
 
-var InitSyms []*Sym
-
 // LookupOK looks up name in pkg and reports whether it previously existed.
 func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
+       // TODO(gri) remove this check in favor of specialized lookup
        if pkg == nil {
-               pkg = Nopkg
+               pkg = nopkg
        }
        if s := pkg.Syms[name]; s != nil {
                return s, true
@@ -69,16 +81,14 @@ func (pkg *Pkg) LookupOK(name string) (s *Sym, existed bool) {
                Name: name,
                Pkg:  pkg,
        }
-       if name == "init" {
-               InitSyms = append(InitSyms, s)
-       }
        pkg.Syms[name] = s
        return s, false
 }
 
 func (pkg *Pkg) LookupBytes(name []byte) *Sym {
+       // TODO(gri) remove this check in favor of specialized lookup
        if pkg == nil {
-               pkg = Nopkg
+               pkg = nopkg
        }
        if s := pkg.Syms[string(name)]; s != nil {
                return s
@@ -87,13 +97,35 @@ func (pkg *Pkg) LookupBytes(name []byte) *Sym {
        return pkg.Lookup(str)
 }
 
-var internedStrings = map[string]string{}
+// LookupNum looks up the symbol starting with prefix and ending with
+// the decimal n. If prefix is too long, LookupNum panics.
+func (pkg *Pkg) LookupNum(prefix string, n int) *Sym {
+       var buf [20]byte // plenty long enough for all current users
+       copy(buf[:], prefix)
+       b := strconv.AppendInt(buf[:len(prefix)], int64(n), 10)
+       return pkg.LookupBytes(b)
+}
+
+// Selector looks up a selector identifier.
+func (pkg *Pkg) Selector(name string) *Sym {
+       if IsExported(name) {
+               pkg = LocalPkg
+       }
+       return pkg.Lookup(name)
+}
+
+var (
+       internedStringsmu sync.Mutex // protects internedStrings
+       internedStrings   = map[string]string{}
+)
 
 func InternString(b []byte) string {
+       internedStringsmu.Lock()
        s, ok := internedStrings[string(b)] // string(b) here doesn't allocate
        if !ok {
                s = string(b)
                internedStrings[s] = s
        }
+       internedStringsmu.Unlock()
        return s
 }