]> Cypherpunks repositories - gostls13.git/commitdiff
go/internal/gcimporter: extract ChanDir and fake FileSet logic
authorMatthew Dempsky <mdempsky@google.com>
Tue, 17 Apr 2018 21:54:06 +0000 (14:54 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 17 Apr 2018 23:47:04 +0000 (23:47 +0000)
This code will be useful for the indexed format importer, so break it
out to be easier to reuse separately.

Change-Id: Ie7e6b2ed89770e1ed9aa1edf11682fe35d6bb373
Reviewed-on: https://go-review.googlesource.com/107617
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/go/internal/gcimporter/bimport.go

index 5c98da43040f89b7baa6d398af213634f7ead8d4..98fc2ae387d293fc22f2c6e81b1945262925e027 100644 (file)
@@ -37,8 +37,7 @@ type importer struct {
        posInfoFormat bool
        prevFile      string
        prevLine      int
-       fset          *token.FileSet
-       files         map[string]*token.File
+       fake          fakeFileSet
 
        // debugging support
        debugFormat bool
@@ -67,8 +66,10 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []
                version:    -1,           // unknown version
                strList:    []string{""}, // empty string is mapped to 0
                pathList:   []string{""}, // empty string is mapped to 0
-               fset:       fset,
-               files:      make(map[string]*token.File),
+               fake: fakeFileSet{
+                       fset:  fset,
+                       files: make(map[string]*token.File),
+               },
        }
 
        // read version info
@@ -324,15 +325,23 @@ func (p *importer) pos() token.Pos {
        p.prevFile = file
        p.prevLine = line
 
-       // Synthesize a token.Pos
+       return p.fake.pos(file, line)
+}
+
+// Synthesize a token.Pos
+type fakeFileSet struct {
+       fset  *token.FileSet
+       files map[string]*token.File
+}
 
+func (s *fakeFileSet) pos(file string, line int) token.Pos {
        // Since we don't know the set of needed file positions, we
        // reserve maxlines positions per file.
        const maxlines = 64 * 1024
-       f := p.files[file]
+       f := s.files[file]
        if f == nil {
-               f = p.fset.AddFile(file, -1, maxlines)
-               p.files[file] = f
+               f = s.fset.AddFile(file, -1, maxlines)
+               s.files[file] = f
                // Allocate the fake linebreak indices on first use.
                // TODO(adonovan): opt: save ~512KB using a more complex scheme?
                fakeLinesOnce.Do(func() {
@@ -546,18 +555,7 @@ func (p *importer) typ(parent *types.Package, tname *types.Named) types.Type {
                        p.record(t)
                }
 
-               var dir types.ChanDir
-               // tag values must match the constants in cmd/compile/internal/gc/go.go
-               switch d := p.int(); d {
-               case 1 /* Crecv */ :
-                       dir = types.RecvOnly
-               case 2 /* Csend */ :
-                       dir = types.SendOnly
-               case 3 /* Cboth */ :
-                       dir = types.SendRecv
-               default:
-                       errorf("unexpected channel dir %d", d)
-               }
+               dir := chanDir(p.int())
                val := p.typ(parent, nil)
                *t = *types.NewChan(dir, val)
                return t
@@ -568,6 +566,21 @@ func (p *importer) typ(parent *types.Package, tname *types.Named) types.Type {
        }
 }
 
+func chanDir(d int) types.ChanDir {
+       // tag values must match the constants in cmd/compile/internal/gc/go.go
+       switch d {
+       case 1 /* Crecv */ :
+               return types.RecvOnly
+       case 2 /* Csend */ :
+               return types.SendOnly
+       case 3 /* Cboth */ :
+               return types.SendRecv
+       default:
+               errorf("unexpected channel dir %d", d)
+               return 0
+       }
+}
+
 func (p *importer) fieldList(parent *types.Package) (fields []*types.Var, tags []string) {
        if n := p.int(); n > 0 {
                fields = make([]*types.Var, n)