Commit 4074a67c authored by Matthew Dempsky's avatar Matthew Dempsky

go/internal/gcimporter: extract ChanDir and fake FileSet logic

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: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 7ba12c16
...@@ -37,8 +37,7 @@ type importer struct { ...@@ -37,8 +37,7 @@ type importer struct {
posInfoFormat bool posInfoFormat bool
prevFile string prevFile string
prevLine int prevLine int
fset *token.FileSet fake fakeFileSet
files map[string]*token.File
// debugging support // debugging support
debugFormat bool debugFormat bool
...@@ -67,8 +66,10 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data [] ...@@ -67,8 +66,10 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []
version: -1, // unknown version version: -1, // unknown version
strList: []string{""}, // empty string is mapped to 0 strList: []string{""}, // empty string is mapped to 0
pathList: []string{""}, // empty string is mapped to 0 pathList: []string{""}, // empty string is mapped to 0
fset: fset, fake: fakeFileSet{
files: make(map[string]*token.File), fset: fset,
files: make(map[string]*token.File),
},
} }
// read version info // read version info
...@@ -324,15 +325,23 @@ func (p *importer) pos() token.Pos { ...@@ -324,15 +325,23 @@ func (p *importer) pos() token.Pos {
p.prevFile = file p.prevFile = file
p.prevLine = line 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 // Since we don't know the set of needed file positions, we
// reserve maxlines positions per file. // reserve maxlines positions per file.
const maxlines = 64 * 1024 const maxlines = 64 * 1024
f := p.files[file] f := s.files[file]
if f == nil { if f == nil {
f = p.fset.AddFile(file, -1, maxlines) f = s.fset.AddFile(file, -1, maxlines)
p.files[file] = f s.files[file] = f
// Allocate the fake linebreak indices on first use. // Allocate the fake linebreak indices on first use.
// TODO(adonovan): opt: save ~512KB using a more complex scheme? // TODO(adonovan): opt: save ~512KB using a more complex scheme?
fakeLinesOnce.Do(func() { fakeLinesOnce.Do(func() {
...@@ -546,18 +555,7 @@ func (p *importer) typ(parent *types.Package, tname *types.Named) types.Type { ...@@ -546,18 +555,7 @@ func (p *importer) typ(parent *types.Package, tname *types.Named) types.Type {
p.record(t) p.record(t)
} }
var dir types.ChanDir dir := chanDir(p.int())
// 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)
}
val := p.typ(parent, nil) val := p.typ(parent, nil)
*t = *types.NewChan(dir, val) *t = *types.NewChan(dir, val)
return t return t
...@@ -568,6 +566,21 @@ func (p *importer) typ(parent *types.Package, tname *types.Named) types.Type { ...@@ -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) { func (p *importer) fieldList(parent *types.Package) (fields []*types.Var, tags []string) {
if n := p.int(); n > 0 { if n := p.int(); n > 0 {
fields = make([]*types.Var, n) fields = make([]*types.Var, n)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment