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.
14 // NewImporter returns a new instance of the types.Importer interface
15 // that reads type information from export data files written by gc.
16 // The Importer also satisfies types.ImporterFrom.
18 // Export data files are located using "go build" workspace conventions
19 // and the build.Default context.
21 // Use this importer instead of go/importer.For("gc", ...) to avoid the
22 // version-skew problems described in the documentation of this package,
23 // or to control the FileSet or access the imports map populated during
26 func NewImporter(fset *token.FileSet, imports map[string]*types.Package) types.ImporterFrom {
27 return importer{fset, imports}
30 type importer struct {
32 imports map[string]*types.Package
35 func (imp importer) Import(importPath string) (*types.Package, error) {
36 return imp.ImportFrom(importPath, "", 0)
39 func (imp importer) ImportFrom(importPath, srcDir string, mode types.ImportMode) (_ *types.Package, err error) {
40 filename, path := Find(importPath, srcDir)
42 if importPath == "unsafe" {
43 // Even for unsafe, call Find first in case
44 // the package was vendored.
45 return types.Unsafe, nil
47 return nil, fmt.Errorf("can't find import: %s", importPath)
50 if pkg, ok := imp.imports[path]; ok && pkg.Complete() {
51 return pkg, nil // cache hit
55 f, err := os.Open(filename)
62 // add file name to error
63 err = fmt.Errorf("reading export data: %s: %v", filename, err)
67 r, err := NewReader(f)
72 return Read(r, imp.fset, imp.imports, path)