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 {
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)
......
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