From 830621bc09c175d1b87496fd1be79bdcd1ce27c8 Mon Sep 17 00:00:00 2001 From: Tim King Date: Wed, 14 Aug 2024 12:34:32 -0700 Subject: internal/pkgbits: add Version type Adds a new Version type to pkgbits to represent the version of the bitstream. Versions let readers and writers know when different data is expected to be present or not in the bitstream. These different pieces of data are called Fields, as an analogy with fields of a struct. Fields can be added, removed or changed in a Version. Extends Encoder and Decoder to report which version they are. Updates #68778 Change-Id: Iaffa1828544fb4cbc47a905de853449bc8e5b91f Reviewed-on: https://go-review.googlesource.com/c/go/+/605655 LUCI-TryBot-Result: Go LUCI Reviewed-by: David Chase Reviewed-by: Cuong Manh Le --- src/internal/pkgbits/encoder.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/internal/pkgbits/encoder.go') diff --git a/src/internal/pkgbits/encoder.go b/src/internal/pkgbits/encoder.go index e52bc85014..a1489c88d0 100644 --- a/src/internal/pkgbits/encoder.go +++ b/src/internal/pkgbits/encoder.go @@ -15,20 +15,15 @@ import ( "strings" ) -// currentVersion is the current version number. -// -// - v0: initial prototype -// -// - v1: adds the flags uint32 word -// -// TODO(mdempsky): For the next version bump: -// - remove the legacy "has init" bool from the public root -// - remove obj's "derived func instance" bool -const currentVersion uint32 = 1 +// currentVersion is the current version number written. +const currentVersion = V1 // A PkgEncoder provides methods for encoding a package's Unified IR // export data. type PkgEncoder struct { + // version of the bitstream. + version Version + // elems holds the bitstream for previously encoded elements. elems [numRelocs][]string @@ -54,6 +49,8 @@ func (pw *PkgEncoder) SyncMarkers() bool { return pw.syncFrames >= 0 } // negative, then sync markers are omitted entirely. func NewPkgEncoder(syncFrames int) PkgEncoder { return PkgEncoder{ + // TODO(taking): Change NewPkgEncoder to take a version as an argument, and remove currentVersion. + version: currentVersion, stringsIdx: make(map[string]Index), syncFrames: syncFrames, } @@ -69,7 +66,7 @@ func (pw *PkgEncoder) DumpTo(out0 io.Writer) (fingerprint [8]byte) { assert(binary.Write(out, binary.LittleEndian, x) == nil) } - writeUint32(currentVersion) + writeUint32(uint32(pw.version)) var flags uint32 if pw.SyncMarkers() { @@ -392,3 +389,6 @@ func (w *Encoder) bigFloat(v *big.Float) { b := v.Append(nil, 'p', -1) w.String(string(b)) // TODO: More efficient encoding. } + +// Version reports the version of the bitstream. +func (w *Encoder) Version() Version { return w.p.version } -- cgit v1.3