gopkg.in/alecthomas/gometalinter.v3@v3.0.0/_linters/src/golang.org/x/tools/go/gcexportdata/gcexportdata.go (about)

     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  	"bytes"
    29  	"fmt"
    30  	"go/token"
    31  	"go/types"
    32  	"io"
    33  	"io/ioutil"
    34  
    35  	gcimporter "golang.org/x/tools/go/gcimporter15"
    36  )
    37  
    38  // Find returns the name of an object (.o) or archive (.a) file
    39  // containing type information for the specified import path,
    40  // using the workspace layout conventions of go/build.
    41  // If no file was found, an empty filename is returned.
    42  //
    43  // A relative srcDir is interpreted relative to the current working directory.
    44  //
    45  // Find also returns the package's resolved (canonical) import path,
    46  // reflecting the effects of srcDir and vendoring on importPath.
    47  func Find(importPath, srcDir string) (filename, path string) {
    48  	return gcimporter.FindPkg(importPath, srcDir)
    49  }
    50  
    51  // NewReader returns a reader for the export data section of an object
    52  // (.o) or archive (.a) file read from r.  The new reader may provide
    53  // additional trailing data beyond the end of the export data.
    54  func NewReader(r io.Reader) (io.Reader, error) {
    55  	buf := bufio.NewReader(r)
    56  	_, err := gcimporter.FindExportData(buf)
    57  	// If we ever switch to a zip-like archive format with the ToC
    58  	// at the end, we can return the correct portion of export data,
    59  	// but for now we must return the entire rest of the file.
    60  	return buf, err
    61  }
    62  
    63  // Read reads export data from in, decodes it, and returns type
    64  // information for the package.
    65  // The package name is specified by path.
    66  // File position information is added to fset.
    67  //
    68  // Read may inspect and add to the imports map to ensure that references
    69  // within the export data to other packages are consistent.  The caller
    70  // must ensure that imports[path] does not exist, or exists but is
    71  // incomplete (see types.Package.Complete), and Read inserts the
    72  // resulting package into this map entry.
    73  //
    74  // On return, the state of the reader is undefined.
    75  func Read(in io.Reader, fset *token.FileSet, imports map[string]*types.Package, path string) (*types.Package, error) {
    76  	data, err := ioutil.ReadAll(in)
    77  	if err != nil {
    78  		return nil, fmt.Errorf("reading export data for %q: %v", path, err)
    79  	}
    80  
    81  	if bytes.HasPrefix(data, []byte("!<arch>")) {
    82  		return nil, fmt.Errorf("can't read export data for %q directly from an archive file (call gcexportdata.NewReader first to extract export data)", path)
    83  	}
    84  
    85  	// The App Engine Go runtime v1.6 uses the old export data format.
    86  	// TODO(adonovan): delete once v1.7 has been around for a while.
    87  	if bytes.HasPrefix(data, []byte("package ")) {
    88  		return gcimporter.ImportData(imports, path, path, bytes.NewReader(data))
    89  	}
    90  
    91  	_, pkg, err := gcimporter.BImportData(fset, imports, data, path)
    92  	return pkg, err
    93  }
    94  
    95  // Write writes encoded type information for the specified package to out.
    96  // The FileSet provides file position information for named objects.
    97  func Write(out io.Writer, fset *token.FileSet, pkg *types.Package) error {
    98  	_, err := out.Write(gcimporter.BExportData(fset, pkg))
    99  	return err
   100  }