Skip to content

Commit 2bbdb45

Browse files
committed
go/gcimporter15: update to match std lib gcimporter (fix build)
TBR=adonovan Change-Id: Ib2464def48932e0d0fc24f67c76a10e8918acb9d Reviewed-on: https://go-review.googlesource.com/27235 Reviewed-by: Robert Griesemer <[email protected]>
1 parent 8ea9d46 commit 2bbdb45

File tree

2 files changed

+57
-55
lines changed

2 files changed

+57
-55
lines changed

go/gcimporter15/bexport.go

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ const debugFormat = false // default: false
4040
// If trace is set, debugging output is printed to std out.
4141
const trace = false // default: false
4242

43-
// This version doesn't write the nointerface flag for exported methods.
44-
// The corresponding importer handles both "v0" and "v1".
45-
// See also issues #16243, #16244.
46-
const exportVersion = "v0"
43+
// Current export format version.
44+
// Must not start with 'c' or 'd' (initials of prior format).
45+
const exportVersion = "version 1"
4746

4847
// trackAllTypes enables cycle tracking for all types, not just named
4948
// types. The existing compiler invariants assume that unnamed types
@@ -86,39 +85,18 @@ func BExportData(fset *token.FileSet, pkg *types.Package) []byte {
8685
posInfoFormat: true, // TODO(gri) might become a flag, eventually
8786
}
8887

89-
// first byte indicates low-level encoding format
90-
var format byte = 'c' // compact
88+
// write version info
89+
p.rawStringln(exportVersion)
90+
var debug string
9191
if debugFormat {
92-
format = 'd'
92+
debug = "debug"
9393
}
94-
p.rawByte(format)
95-
96-
format = 'n' // track named types only
97-
if trackAllTypes {
98-
format = 'a'
99-
}
100-
p.rawByte(format)
101-
102-
// posInfo exported or not?
94+
p.rawStringln(debug) // cannot use p.bool since it's affected by debugFormat; also want to see this clearly
95+
p.bool(trackAllTypes)
10396
p.bool(p.posInfoFormat)
10497

10598
// --- generic export data ---
10699

107-
if trace {
108-
p.tracef("\n--- generic export data ---\n")
109-
if p.indent != 0 {
110-
log.Fatalf("gcimporter: incorrect indentation %d", p.indent)
111-
}
112-
}
113-
114-
if trace {
115-
p.tracef("version = ")
116-
}
117-
p.string(exportVersion)
118-
if trace {
119-
p.tracef("\n")
120-
}
121-
122100
// populate type map with predeclared "known" types
123101
for index, typ := range predeclared {
124102
p.typIndex[typ] = index
@@ -401,6 +379,7 @@ func (p *exporter) assocMethods(named *types.Named) {
401379
p.paramList(types.NewTuple(sig.Recv()), false)
402380
p.paramList(sig.Params(), sig.Variadic())
403381
p.paramList(sig.Results(), false)
382+
p.int(0) // dummy value for go:nointerface pragma - ignored by importer
404383
}
405384

406385
if trace && methods != nil {
@@ -719,7 +698,7 @@ func (p *exporter) marker(m byte) {
719698
p.rawInt64(int64(p.written))
720699
}
721700

722-
// rawInt64 should only be used by low-level encoders
701+
// rawInt64 should only be used by low-level encoders.
723702
func (p *exporter) rawInt64(x int64) {
724703
var tmp [binary.MaxVarintLen64]byte
725704
n := binary.PutVarint(tmp[:], x)
@@ -728,6 +707,14 @@ func (p *exporter) rawInt64(x int64) {
728707
}
729708
}
730709

710+
// rawStringln should only be used to emit the initial version string.
711+
func (p *exporter) rawStringln(s string) {
712+
for i := 0; i < len(s); i++ {
713+
p.rawByte(s[i])
714+
}
715+
p.rawByte('\n')
716+
}
717+
731718
// rawByte is the bottleneck interface to write to p.out.
732719
// rawByte escapes b as follows (any encoding does that
733720
// hides '$'):

go/gcimporter15/bimport.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type importer struct {
2626
data []byte
2727
path string
2828
buf []byte // for reading strings
29-
version string
3029

3130
// object lists
3231
strList []string // in order of appearance
@@ -60,27 +59,36 @@ func BImportData(fset *token.FileSet, imports map[string]*types.Package, data []
6059
files: make(map[string]*token.File),
6160
}
6261

63-
// read low-level encoding format
64-
switch format := p.rawByte(); format {
65-
case 'c':
66-
// compact format - nothing to do
67-
case 'd':
68-
p.debugFormat = true
69-
default:
70-
return p.read, nil, fmt.Errorf("invalid encoding format in export data: got %q; want 'c' or 'd'", format)
62+
// read version info
63+
if b := p.rawByte(); b == 'c' || b == 'd' {
64+
// Go1.7 encoding; first byte encodes low-level
65+
// encoding format (compact vs debug).
66+
// For backward-compatibility only (avoid problems with
67+
// old installed packages). Newly compiled packages use
68+
// the extensible format string.
69+
// TODO(gri) Remove this support eventually; after Go1.8.
70+
if b == 'd' {
71+
p.debugFormat = true
72+
}
73+
p.trackAllTypes = p.rawByte() == 'a'
74+
p.posInfoFormat = p.int() != 0
75+
const go17version = "v1"
76+
if s := p.string(); s != go17version {
77+
return p.read, nil, fmt.Errorf("importer: unknown export data format: %s (imported package compiled with old compiler?)", s)
78+
}
79+
} else {
80+
// Go1.8 extensible encoding
81+
const exportVersion = "version 1"
82+
if s := p.rawStringln(b); s != exportVersion {
83+
return p.read, nil, fmt.Errorf("importer: unknown export data format: %s (imported package compiled with old compiler?)", s)
84+
}
85+
p.debugFormat = p.rawStringln(p.rawByte()) == "debug"
86+
p.trackAllTypes = p.int() != 0
87+
p.posInfoFormat = p.int() != 0
7188
}
7289

73-
p.trackAllTypes = p.rawByte() == 'a'
74-
75-
p.posInfoFormat = p.int() != 0
76-
7790
// --- generic export data ---
7891

79-
p.version = p.string()
80-
if p.version != "v0" && p.version != "v1" {
81-
return p.read, nil, fmt.Errorf("unknown export data version: %s", p.version)
82-
}
83-
8492
// populate typList with predeclared "known" types
8593
p.typList = append(p.typList, predeclared...)
8694

@@ -345,10 +353,7 @@ func (p *importer) typ(parent *types.Package) types.Type {
345353
recv, _ := p.paramList() // TODO(gri) do we need a full param list for the receiver?
346354
params, isddd := p.paramList()
347355
result, _ := p.paramList()
348-
349-
if p.version == "v1" {
350-
p.int() // nointerface flag - discarded
351-
}
356+
p.int() // go:nointerface pragma - discarded
352357

353358
sig := types.NewSignature(recv.At(0), params, result, isddd)
354359
t0.AddMethod(types.NewFunc(pos, parent, name, sig))
@@ -729,7 +734,7 @@ func (p *importer) marker(want byte) {
729734
}
730735
}
731736

732-
// rawInt64 should only be used by low-level decoders
737+
// rawInt64 should only be used by low-level decoders.
733738
func (p *importer) rawInt64() int64 {
734739
i, err := binary.ReadVarint(p)
735740
if err != nil {
@@ -738,6 +743,16 @@ func (p *importer) rawInt64() int64 {
738743
return i
739744
}
740745

746+
// rawStringln should only be used to read the initial version string.
747+
func (p *importer) rawStringln(b byte) string {
748+
p.buf = p.buf[:0]
749+
for b != '\n' {
750+
p.buf = append(p.buf, b)
751+
b = p.rawByte()
752+
}
753+
return string(p.buf)
754+
}
755+
741756
// needed for binary.ReadVarint in rawInt64
742757
func (p *importer) ReadByte() (byte, error) {
743758
return p.rawByte(), nil

0 commit comments

Comments
 (0)