// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gcexportdata

import (
	
	
	
	
)

// NewImporter returns a new instance of the types.Importer interface
// that reads type information from export data files written by gc.
// The Importer also satisfies types.ImporterFrom.
//
// Export data files are located using "go build" workspace conventions
// and the build.Default context.
//
// Use this importer instead of go/importer.For("gc", ...) to avoid the
// version-skew problems described in the documentation of this package,
// or to control the FileSet or access the imports map populated during
// package loading.
//
// Deprecated: Use the higher-level API in golang.org/x/tools/go/packages,
// which is more efficient.
func ( *token.FileSet,  map[string]*types.Package) types.ImporterFrom {
	return importer{, }
}

type importer struct {
	fset    *token.FileSet
	imports map[string]*types.Package
}

func ( importer) ( string) (*types.Package, error) {
	return .ImportFrom(, "", 0)
}

func ( importer) (,  string,  types.ImportMode) ( *types.Package,  error) {
	,  := Find(, )
	if  == "" {
		if  == "unsafe" {
			// Even for unsafe, call Find first in case
			// the package was vendored.
			return types.Unsafe, nil
		}
		return nil, fmt.Errorf("can't find import: %s", )
	}

	if ,  := .imports[];  && .Complete() {
		return , nil // cache hit
	}

	// open file
	,  := os.Open()
	if  != nil {
		return nil, 
	}
	defer func() {
		.Close()
		if  != nil {
			// add file name to error
			 = fmt.Errorf("reading export data: %s: %v", , )
		}
	}()

	,  := NewReader()
	if  != nil {
		return nil, 
	}

	return Read(, .fset, .imports, )
}