|
2 | 2 | // Use of this source code is governed by a BSD-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -// Package gcexportdata provides functions for locating, reading, and |
6 |
| -// writing export data files containing type information produced by the |
7 |
| -// gc compiler. This package supports go1.7 export data format and all |
8 |
| -// later versions. |
9 |
| -// |
10 |
| -// Although it might seem convenient for this package to live alongside |
11 |
| -// go/types in the standard library, this would cause version skew |
12 |
| -// problems for developer tools that use it, since they must be able to |
13 |
| -// consume the outputs of the gc compiler both before and after a Go |
14 |
| -// update such as from Go 1.7 to Go 1.8. Because this package lives in |
15 |
| -// golang.org/x/tools, sites can update their version of this repo some |
16 |
| -// time before the Go 1.8 release and rebuild and redeploy their |
17 |
| -// developer tools, which will then be able to consume both Go 1.7 and |
18 |
| -// Go 1.8 export data files, so they will work before and after the |
19 |
| -// Go update. (See discussion at https://golang.org/issue/15651.) |
20 |
| -package gcexportdata // import "golang.org/x/tools/go/gcexportdata" |
| 5 | +// Package gcexportdata provides functions for reading and writing |
| 6 | +// export data, which is a serialized description of the API of a Go |
| 7 | +// package including the names, kinds, types, and locations of all |
| 8 | +// exported declarations. |
| 9 | +// |
| 10 | +// The standard Go compiler (cmd/compile) writes an export data file |
| 11 | +// for each package it compiles, which it later reads when compiling |
| 12 | +// packages that import the earlier one. The compiler must thus |
| 13 | +// contain logic to both write and read export data. |
| 14 | +// (See the "Export" section in the cmd/compile/README file.) |
| 15 | +// |
| 16 | +// The [Read] function in this package can read files produced by the |
| 17 | +// compiler, producing [go/types] data structures. As a matter of |
| 18 | +// policy, Read supports export data files produced by only the last |
| 19 | +// two Go releases plus tip; see https://go.dev/issue/68898. The |
| 20 | +// export data files produced by the compiler contain additional |
| 21 | +// details related to generics, inlining, and other optimizations that |
| 22 | +// cannot be decoded by the [Read] function. |
| 23 | +// |
| 24 | +// In files written by the compiler, the export data is not at the |
| 25 | +// start of the file. Before calling Read, use [NewReader] to locate |
| 26 | +// the desired portion of the file. |
| 27 | +// |
| 28 | +// The [Write] function in this package encodes the exported API of a |
| 29 | +// Go package ([types.Package]) as a file. Such files can be later |
| 30 | +// decoded by Read, but cannot be consumed by the compiler. |
| 31 | +// |
| 32 | +// # Future changes |
| 33 | +// |
| 34 | +// Although Read supports the formats written by both Write and the |
| 35 | +// compiler, the two are quite different, and there is an open |
| 36 | +// proposal (https://go.dev/issue/69491) to separate these APIs. |
| 37 | +// |
| 38 | +// Under that proposal, this package would ultimately provide only the |
| 39 | +// Read operation for compiler export data, which must be defined in |
| 40 | +// this module (golang.org/x/tools), not in the standard library, to |
| 41 | +// avoid version skew for developer tools that need to read compiler |
| 42 | +// export data both before and after a Go release, such as from Go |
| 43 | +// 1.23 to Go 1.24. Because this package lives in the tools module, |
| 44 | +// clients can update their version of the module some time before the |
| 45 | +// Go 1.24 release and rebuild and redeploy their tools, which will |
| 46 | +// then be able to consume both Go 1.23 and Go 1.24 export data files, |
| 47 | +// so they will work before and after the Go update. (See discussion |
| 48 | +// at https://go.dev/issue/15651.) |
| 49 | +// |
| 50 | +// The operations to import and export [go/types] data structures |
| 51 | +// would be defined in the go/types package as Import and Export. |
| 52 | +// [Write] would (eventually) delegate to Export, |
| 53 | +// and [Read], when it detects a file produced by Export, |
| 54 | +// would delegate to Import. |
| 55 | +// |
| 56 | +// # Deprecations |
| 57 | +// |
| 58 | +// The [NewImporter] and [Find] functions are deprecated and should |
| 59 | +// not be used in new code. The [WriteBundle] and [ReadBundle] |
| 60 | +// functions are experimental, and there is an open proposal to |
| 61 | +// deprecate them (https://go.dev/issue/69573). |
| 62 | +package gcexportdata |
21 | 63 |
|
22 | 64 | import (
|
23 | 65 | "bufio"
|
@@ -100,6 +142,11 @@ func readAll(r io.Reader) ([]byte, error) {
|
100 | 142 | // Read reads export data from in, decodes it, and returns type
|
101 | 143 | // information for the package.
|
102 | 144 | //
|
| 145 | +// Read is capable of reading export data produced by [Write] at the |
| 146 | +// same source code version, or by the last two Go releases (plus tip) |
| 147 | +// of the standard Go compiler. Reading files from older compilers may |
| 148 | +// produce an error. |
| 149 | +// |
103 | 150 | // The package path (effectively its linker symbol prefix) is
|
104 | 151 | // specified by path, since unlike the package name, this information
|
105 | 152 | // may not be recorded in the export data.
|
@@ -128,14 +175,26 @@ func Read(in io.Reader, fset *token.FileSet, imports map[string]*types.Package,
|
128 | 175 | // (from "version"). Select appropriate importer.
|
129 | 176 | if len(data) > 0 {
|
130 | 177 | switch data[0] {
|
131 |
| - case 'v', 'c', 'd': // binary, till go1.10 |
| 178 | + case 'v', 'c', 'd': |
| 179 | + // binary, produced by cmd/compile till go1.10 |
132 | 180 | return nil, fmt.Errorf("binary (%c) import format is no longer supported", data[0])
|
133 | 181 |
|
134 |
| - case 'i': // indexed, till go1.19 |
| 182 | + case 'i': |
| 183 | + // indexed, produced by cmd/compile till go1.19, |
| 184 | + // and also by [Write]. |
| 185 | + // |
| 186 | + // If proposal #69491 is accepted, go/types |
| 187 | + // serialization will be implemented by |
| 188 | + // types.Export, to which Write would eventually |
| 189 | + // delegate (explicitly dropping any pretence at |
| 190 | + // inter-version Write-Read compatibility). |
| 191 | + // This [Read] function would delegate to types.Import |
| 192 | + // when it detects that the file was produced by Export. |
135 | 193 | _, pkg, err := gcimporter.IImportData(fset, imports, data[1:], path)
|
136 | 194 | return pkg, err
|
137 | 195 |
|
138 |
| - case 'u': // unified, from go1.20 |
| 196 | + case 'u': |
| 197 | + // unified, produced by cmd/compile since go1.20 |
139 | 198 | _, pkg, err := gcimporter.UImportData(fset, imports, data[1:], path)
|
140 | 199 | return pkg, err
|
141 | 200 |
|
|
0 commit comments