Source file src/internal/pkgbits/version.go

     1  // Copyright 2021 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  	numVersions = iota
    32  )
    33  
    34  // Field denotes a unit of data in the serialized unified IR bitstream.
    35  // It is conceptually a like field in a structure.
    36  //
    37  // We only really need Fields when the data may or may not be present
    38  // in a stream based on the Version of the bitstream.
    39  //
    40  // Unlike much of pkgbits, Fields are not serialized and
    41  // can change values as needed.
    42  type Field int
    43  
    44  const (
    45  	// Flags in a uint32 in the header of a bitstream
    46  	// that is used to indicate whether optional features are enabled.
    47  	Flags Field = iota
    48  
    49  	// Deprecated: HasInit was a bool indicating whether a package
    50  	// has any init functions.
    51  	HasInit
    52  
    53  	// Deprecated: DerivedFuncInstance was a bool indicating
    54  	// whether an object was a function instance.
    55  	DerivedFuncInstance
    56  
    57  	// ObjAlias has a list of TypeParamNames.
    58  	AliasTypeParamNames
    59  
    60  	// Deprecated: DerivedInfoNeeded was a bool indicating
    61  	// whether a type was a derived type.
    62  	DerivedInfoNeeded
    63  
    64  	numFields = iota
    65  )
    66  
    67  // introduced is the version a field was added.
    68  var introduced = [numFields]Version{
    69  	Flags:               V1,
    70  	AliasTypeParamNames: V2,
    71  }
    72  
    73  // removed is the version a field was removed in or 0 for fields
    74  // that have not yet been deprecated.
    75  // (So removed[f]-1 is the last version it is included in.)
    76  var removed = [numFields]Version{
    77  	HasInit:             V2,
    78  	DerivedFuncInstance: V2,
    79  	DerivedInfoNeeded:   V2,
    80  }
    81  
    82  // Has reports whether field f is present in a bitstream at version v.
    83  func (v Version) Has(f Field) bool {
    84  	return introduced[f] <= v && (v < removed[f] || removed[f] == V0)
    85  }
    86  

View as plain text