| 1 | // Copyright 2024 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package pkgbits |
| 6 | |
| 7 | // Version indicates a version of a unified IR bitstream. |
| 8 | // Each Version indicates the addition, removal, or change of |
| 9 | // new data in the bitstream. |
| 10 | // |
| 11 | // These are serialized to disk and the interpretation remains fixed. |
| 12 | type Version uint32 |
| 13 | |
| 14 | const ( |
| 15 | // V0: initial prototype. |
| 16 | // |
| 17 | // All data that is not assigned a Field is in version V0 |
| 18 | // and has not been deprecated. |
| 19 | V0 Version = iota |
| 20 | |
| 21 | // V1: adds the Flags uint32 word |
| 22 | V1 |
| 23 | |
| 24 | // V2: removes unused legacy fields and supports type parameters for aliases. |
| 25 | // - remove the legacy "has init" bool from the public root |
| 26 | // - remove obj's "derived func instance" bool |
| 27 | // - add a TypeParamNames field to ObjAlias |
| 28 | // - remove derived info "needed" bool |
| 29 | V2 |
| 30 | |
| 31 | // V3: introduces a more compact format for composite literal element lists |
| 32 | // - negative lengths indicate that (some) elements may have keys |
| 33 | // - positive lengths indicate that no element has a key |
| 34 | // - a negative struct field index indicates an embedded field |
| 35 | V3 |
| 36 | |
| 37 | // V4: encodes generic methods as standalone function objects |
| 38 | V4 |
| 39 | |
| 40 | numVersions = iota |
| 41 | ) |
| 42 | |
| 43 | // Field denotes a unit of data in the serialized unified IR bitstream. |
| 44 | // It is conceptually a like field in a structure. |
| 45 | // |
| 46 | // We only really need Fields when the data may or may not be present |
| 47 | // in a stream based on the Version of the bitstream. |
| 48 | // |
| 49 | // Unlike much of pkgbits, Fields are not serialized and |
| 50 | // can change values as needed. |
| 51 | type Field int |
| 52 | |
| 53 | const ( |
| 54 | // Flags in a uint32 in the header of a bitstream |
| 55 | // that is used to indicate whether optional features are enabled. |
| 56 | Flags Field = iota |
| 57 | |
| 58 | // Deprecated: HasInit was a bool indicating whether a package |
| 59 | // has any init functions. |
| 60 | HasInit |
| 61 | |
| 62 | // Deprecated: DerivedFuncInstance was a bool indicating |
| 63 | // whether an object was a function instance. |
| 64 | DerivedFuncInstance |
| 65 | |
| 66 | // ObjAlias has a list of TypeParamNames. |
| 67 | AliasTypeParamNames |
| 68 | |
| 69 | // Deprecated: DerivedInfoNeeded was a bool indicating |
| 70 | // whether a type was a derived type. |
| 71 | DerivedInfoNeeded |
| 72 | |
| 73 | // Composite literals use a more compact format for element lists. |
| 74 | CompactCompLiterals |
| 75 | |
| 76 | // Generic methods may appear as standalone function objects. |
| 77 | GenericMethods |
| 78 | |
| 79 | numFields = iota |
| 80 | ) |
| 81 | |
| 82 | // introduced is the version a field was added. |
| 83 | var introduced = [numFields]Version{ |
| 84 | Flags: V1, |
| 85 | AliasTypeParamNames: V2, |
| 86 | CompactCompLiterals: V3, |
| 87 | GenericMethods: V4, |
| 88 | } |
| 89 | |
| 90 | // removed is the version a field was removed in or 0 for fields |
| 91 | // that have not yet been deprecated. |
| 92 | // (So removed[f]-1 is the last version it is included in.) |
| 93 | var removed = [numFields]Version{ |
| 94 | HasInit: V2, |
| 95 | DerivedFuncInstance: V2, |
| 96 | DerivedInfoNeeded: V2, |
| 97 | } |
| 98 | |
| 99 | // Has reports whether field f is present in a bitstream at version v. |
| 100 | func (v Version) Has(f Field) bool { |
| 101 | return introduced[f] <= v && (v < removed[f] || removed[f] == V0) |
| 102 | } |