-
Notifications
You must be signed in to change notification settings - Fork 17.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
- Loading branch information
1 parent
54c948d
commit 830621b
Showing
4 changed files
with
169 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2024 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package pkgbits_test | ||
|
||
import ( | ||
"internal/pkgbits" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestRoundTrip(t *testing.T) { | ||
pw := pkgbits.NewPkgEncoder(-1) | ||
w := pw.NewEncoder(pkgbits.RelocMeta, pkgbits.SyncPublic) | ||
w.Flush() | ||
|
||
var b strings.Builder | ||
_ = pw.DumpTo(&b) | ||
input := b.String() | ||
|
||
pr := pkgbits.NewPkgDecoder("package_id", input) | ||
r := pr.NewDecoder(pkgbits.RelocMeta, pkgbits.PublicRootIdx, pkgbits.SyncPublic) | ||
|
||
if r.Version() != w.Version() { | ||
t.Errorf("Expected reader version %q to be the writer version %q", r.Version(), w.Version()) | ||
} | ||
} | ||
|
||
// Type checker to enforce that know V* have the constant values they must have. | ||
var _ [0]bool = [pkgbits.V0]bool{} | ||
var _ [1]bool = [pkgbits.V1]bool{} | ||
|
||
func TestVersions(t *testing.T) { | ||
type vfpair struct { | ||
v pkgbits.Version | ||
f pkgbits.Field | ||
} | ||
|
||
// has field tests | ||
for _, c := range []vfpair{ | ||
{pkgbits.V1, pkgbits.Flags}, | ||
{pkgbits.V2, pkgbits.Flags}, | ||
{pkgbits.V0, pkgbits.HasInit}, | ||
{pkgbits.V1, pkgbits.HasInit}, | ||
{pkgbits.V0, pkgbits.DerivedFuncInstance}, | ||
{pkgbits.V1, pkgbits.DerivedFuncInstance}, | ||
{pkgbits.V2, pkgbits.AliasTypeParamNames}, | ||
} { | ||
if !c.v.Has(c.f) { | ||
t.Errorf("Expected version %v to have field %v", c.v, c.f) | ||
} | ||
} | ||
|
||
// does not have field tests | ||
for _, c := range []vfpair{ | ||
{pkgbits.V0, pkgbits.Flags}, | ||
{pkgbits.V2, pkgbits.HasInit}, | ||
{pkgbits.V2, pkgbits.DerivedFuncInstance}, | ||
{pkgbits.V0, pkgbits.AliasTypeParamNames}, | ||
{pkgbits.V1, pkgbits.AliasTypeParamNames}, | ||
} { | ||
if c.v.Has(c.f) { | ||
t.Errorf("Expected version %v to not have field %v", c.v, c.f) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2021 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package pkgbits | ||
|
||
// Version indicates a version of a unified IR bitstream. | ||
// Each Version indicates the addition, removal, or change of | ||
// new data in the bitstream. | ||
// | ||
// These are serialized to disk and the interpretation remains fixed. | ||
type Version uint32 | ||
|
||
const ( | ||
// V0: initial prototype. | ||
// | ||
// All data that is not assigned a Field is in version V0 | ||
// and has not been deprecated. | ||
V0 Version = iota | ||
|
||
// V1: adds the Flags uint32 word | ||
V1 | ||
|
||
// V2: removes unused legacy fields and supports type parameters for aliases. | ||
// - remove the legacy "has init" bool from the public root | ||
// - remove obj's "derived func instance" bool | ||
// - add a TypeParamNames field to ObjAlias | ||
V2 | ||
|
||
numVersions = iota | ||
) | ||
|
||
// Field denotes a unit of data in the serialized unified IR bitstream. | ||
// It is conceptually a like field in a structure. | ||
// | ||
// We only really need Fields when the data may or may not be present | ||
// in a stream based on the Version of the bitstream. | ||
// | ||
// Unlike much of pkgbits, Fields are not serialized and | ||
// can change values as needed. | ||
type Field int | ||
|
||
const ( | ||
// Flags in a uint32 in the header of a bitstream | ||
// that is used to indicate whether optional features are enabled. | ||
Flags Field = iota | ||
|
||
// Deprecated: HasInit was a bool indicating whether a package | ||
// has any init functions. | ||
HasInit | ||
|
||
// Deprecated: DerivedFuncInstance was a bool indicating | ||
// whether an object was a function instance. | ||
DerivedFuncInstance | ||
|
||
// ObjAlias has a list of TypeParamNames. | ||
AliasTypeParamNames | ||
|
||
numFields = iota | ||
) | ||
|
||
// introduced is the version a field was added. | ||
var introduced = [numFields]Version{ | ||
Flags: V1, | ||
AliasTypeParamNames: V2, | ||
} | ||
|
||
// removed is the version a field was removed in or 0 for fields | ||
// that have not yet been deprecated. | ||
// (So removed[f]-1 is the last version it is included in.) | ||
var removed = [numFields]Version{ | ||
HasInit: V2, | ||
DerivedFuncInstance: V2, | ||
} | ||
|
||
// Has reports whether field f is present in a bitstream at version v. | ||
func (v Version) Has(f Field) bool { | ||
return introduced[f] <= v && (v < removed[f] || removed[f] == V0) | ||
} |