@@ -21,14 +21,15 @@ import (
21
21
)
22
22
23
23
type importer struct {
24
- imports map [string ]* types.Package
25
- data []byte
26
- path string
27
- buf []byte // for reading strings
28
- version int // export format version
24
+ imports map [string ]* types.Package
25
+ data []byte
26
+ importpath string
27
+ buf []byte // for reading strings
28
+ version int // export format version
29
29
30
30
// object lists
31
31
strList []string // in order of appearance
32
+ pathList []string // in order of appearance
32
33
pkgList []* types.Package // in order of appearance
33
34
typList []types.Type // in order of appearance
34
35
interfaceList []* types.Interface // for delayed completion only
@@ -62,13 +63,14 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []
62
63
}()
63
64
64
65
p := importer {
65
- imports : imports ,
66
- data : data ,
67
- path : path ,
68
- version : - 1 , // unknown version
69
- strList : []string {"" }, // empty string is mapped to 0
70
- fset : fset ,
71
- files : make (map [string ]* token.File ),
66
+ imports : imports ,
67
+ data : data ,
68
+ importpath : path ,
69
+ version : - 1 , // unknown version
70
+ strList : []string {"" }, // empty string is mapped to 0
71
+ pathList : []string {"" }, // empty string is mapped to 0
72
+ fset : fset ,
73
+ files : make (map [string ]* token.File ),
72
74
}
73
75
74
76
// read version info
@@ -102,10 +104,10 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []
102
104
103
105
// read version specific flags - extend as necessary
104
106
switch p .version {
105
- // case 5 :
107
+ // case 6 :
106
108
// ...
107
109
// fallthrough
108
- case 4 , 3 , 2 , 1 :
110
+ case 5 , 4 , 3 , 2 , 1 :
109
111
p .debugFormat = p .rawStringln (p .rawByte ()) == "debug"
110
112
p .trackAllTypes = p .int () != 0
111
113
p .posInfoFormat = p .int () != 0
@@ -171,12 +173,17 @@ func (p *importer) pkg() *types.Package {
171
173
172
174
// otherwise, i is the package tag (< 0)
173
175
if i != packageTag {
174
- errorf ("unexpected package tag %d" , i )
176
+ errorf ("unexpected package tag %d version %d " , i , p . version )
175
177
}
176
178
177
179
// read package data
178
180
name := p .string ()
179
- path := p .string ()
181
+ var path string
182
+ if p .version >= 5 {
183
+ path = p .path ()
184
+ } else {
185
+ path = p .string ()
186
+ }
180
187
181
188
// we should never see an empty package name
182
189
if name == "" {
@@ -191,7 +198,7 @@ func (p *importer) pkg() *types.Package {
191
198
192
199
// if the package was imported before, use that one; otherwise create a new one
193
200
if path == "" {
194
- path = p .path
201
+ path = p .importpath
195
202
}
196
203
pkg := p .imports [path ]
197
204
if pkg == nil {
@@ -285,22 +292,35 @@ func (p *importer) obj(tag int) {
285
292
}
286
293
}
287
294
295
+ const deltaNewFile = - 64 // see cmd/compile/internal/gc/bexport.go
296
+
288
297
func (p * importer ) pos () token.Pos {
289
298
if ! p .posInfoFormat {
290
299
return token .NoPos
291
300
}
292
301
293
302
file := p .prevFile
294
303
line := p .prevLine
295
- if delta := p .int (); delta != 0 {
296
- // line changed
297
- line += delta
298
- } else if n := p .int (); n >= 0 {
299
- // file changed
300
- file = p .prevFile [:n ] + p .string ()
301
- p .prevFile = file
302
- line = p .int ()
304
+ delta := p .int ()
305
+ line += delta
306
+ if p .version >= 5 {
307
+ if delta == deltaNewFile {
308
+ if n := p .int (); n >= 0 {
309
+ // file changed
310
+ file = p .path ()
311
+ line = n
312
+ }
313
+ }
314
+ } else {
315
+ if delta == 0 {
316
+ if n := p .int (); n >= 0 {
317
+ // file changed
318
+ file = p .prevFile [:n ] + p .string ()
319
+ line = p .int ()
320
+ }
321
+ }
303
322
}
323
+ p .prevFile = file
304
324
p .prevLine = line
305
325
306
326
// Synthesize a token.Pos
@@ -778,6 +798,26 @@ func (p *importer) int64() int64 {
778
798
return p .rawInt64 ()
779
799
}
780
800
801
+ func (p * importer ) path () string {
802
+ if p .debugFormat {
803
+ p .marker ('p' )
804
+ }
805
+ // if the path was seen before, i is its index (>= 0)
806
+ // (the empty string is at index 0)
807
+ i := p .rawInt64 ()
808
+ if i >= 0 {
809
+ return p .pathList [i ]
810
+ }
811
+ // otherwise, i is the negative path length (< 0)
812
+ a := make ([]string , - i )
813
+ for n := range a {
814
+ a [n ] = p .string ()
815
+ }
816
+ s := strings .Join (a , "/" )
817
+ p .pathList = append (p .pathList , s )
818
+ return s
819
+ }
820
+
781
821
func (p * importer ) string () string {
782
822
if p .debugFormat {
783
823
p .marker ('s' )
0 commit comments