|
| 1 | +// Copyright 2016 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 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 | +// This package replaces the deprecated golang.org/x/tools/go/gcimporter15 |
| 11 | +// package, which will be deleted in October 2017. |
| 12 | +// |
| 13 | +// Although it might seem convenient for this package to live alongside |
| 14 | +// go/types in the standard library, this would cause version skew |
| 15 | +// problems for developer tools that use it, since they must be able to |
| 16 | +// consume the outputs of the gc compiler both before and after a Go |
| 17 | +// update such as from Go 1.7 to Go 1.8. Because this package lives in |
| 18 | +// golang.org/x/tools, sites can update their version of this repo some |
| 19 | +// time before the Go 1.8 release and rebuild and redeploy their |
| 20 | +// developer tools, which will then be able to consume both Go 1.7 and |
| 21 | +// Go 1.8 export data files, so they will work before and after the |
| 22 | +// Go update. (See discussion at https://github.com/golang/go/issues/15651.) |
| 23 | +// |
| 24 | +package gcexportdata // import "golang.org/x/tools/go/gcexportdata" |
| 25 | + |
| 26 | +import ( |
| 27 | + "bufio" |
| 28 | + "fmt" |
| 29 | + "go/token" |
| 30 | + "go/types" |
| 31 | + "io" |
| 32 | + "io/ioutil" |
| 33 | + |
| 34 | + gcimporter "golang.org/x/tools/go/gcimporter15" |
| 35 | +) |
| 36 | + |
| 37 | +// Find returns the name of an object (.o) or archive (.a) file |
| 38 | +// containing type information for the specified import path, |
| 39 | +// using the workspace layout conventions of go/build. |
| 40 | +// If no file was found, an empty filename is returned. |
| 41 | +// |
| 42 | +// A relative srcDir is interpreted relative to the current working directory. |
| 43 | +// |
| 44 | +// Find also returns the package's resolved (canonical) import path, |
| 45 | +// reflecting the effects of srcDir and vendoring on importPath. |
| 46 | +func Find(importPath string, srcDir string) (filename, path string) { |
| 47 | + return gcimporter.FindPkg(importPath, srcDir) |
| 48 | +} |
| 49 | + |
| 50 | +// NewReader returns a reader for the export data section of an object |
| 51 | +// (.o) or archive (.a) file read from r. The new reader may provide |
| 52 | +// additional trailing data beyond the end of the export data. |
| 53 | +func NewReader(r io.Reader) (io.Reader, error) { |
| 54 | + buf := bufio.NewReader(r) |
| 55 | + _, err := gcimporter.FindExportData(buf) |
| 56 | + // If we ever switch to a zip-like archive format with the ToC |
| 57 | + // at the end, we can return the correct portion of export data, |
| 58 | + // but for now we must return the entire rest of the file. |
| 59 | + return buf, err |
| 60 | +} |
| 61 | + |
| 62 | +// Read reads export data from in, decodes it, and returns type |
| 63 | +// information for the package. |
| 64 | +// The package name is specified by path. |
| 65 | +// File position information is added to fset. |
| 66 | +// |
| 67 | +// Read may inspect and add to the imports map to ensure that references |
| 68 | +// within the export data to other packages are consistent. The caller |
| 69 | +// must ensure that imports[path] does not exist, or exists but is |
| 70 | +// incomplete (see types.Package.Complete), and Read inserts the |
| 71 | +// resulting package into this map entry. |
| 72 | +// |
| 73 | +// On return, the state of the reader is undefined. |
| 74 | +func Read(in io.Reader, fset *token.FileSet, imports map[string]*types.Package, path string) (*types.Package, error) { |
| 75 | + data, err := ioutil.ReadAll(in) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("reading export data for %q: %v", path, err) |
| 78 | + } |
| 79 | + _, pkg, err := gcimporter.BImportData(fset, imports, data, path) |
| 80 | + return pkg, err |
| 81 | +} |
| 82 | + |
| 83 | +// Write writes encoded type information for the specified package to out. |
| 84 | +// The FileSet provides file position information for named objects. |
| 85 | +func Write(out io.Writer, fset *token.FileSet, pkg *types.Package) error { |
| 86 | + _, err := out.Write(gcimporter.BExportData(fset, pkg)) |
| 87 | + return err |
| 88 | +} |
0 commit comments