Source file src/go/importer/importer.go

Documentation: go/importer

     1  // Copyright 2015 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 importer provides access to export data importers.
     6  package importer
     7  
     8  import (
     9  	"go/build"
    10  	"go/internal/gccgoimporter"
    11  	"go/internal/gcimporter"
    12  	"go/internal/srcimporter"
    13  	"go/token"
    14  	"go/types"
    15  	"io"
    16  	"runtime"
    17  )
    18  
    19  // A Lookup function returns a reader to access package data for
    20  // a given import path, or an error if no matching package is found.
    21  type Lookup func(path string) (io.ReadCloser, error)
    22  
    23  // ForCompiler returns an Importer for importing from installed packages
    24  // for the compilers "gc" and "gccgo", or for importing directly
    25  // from the source if the compiler argument is "source". In this
    26  // latter case, importing may fail under circumstances where the
    27  // exported API is not entirely defined in pure Go source code
    28  // (if the package API depends on cgo-defined entities, the type
    29  // checker won't have access to those).
    30  //
    31  // If lookup is nil, the default package lookup mechanism for the
    32  // given compiler is used, and the resulting importer attempts
    33  // to resolve relative and absolute import paths to canonical
    34  // import path IDs before finding the imported file.
    35  //
    36  // If lookup is non-nil, then the returned importer calls lookup
    37  // each time it needs to resolve an import path. In this mode
    38  // the importer can only be invoked with canonical import paths
    39  // (not relative or absolute ones); it is assumed that the translation
    40  // to canonical import paths is being done by the client of the
    41  // importer.
    42  func ForCompiler(fset *token.FileSet, compiler string, lookup Lookup) types.Importer {
    43  	switch compiler {
    44  	case "gc":
    45  		return &gcimports{
    46  			fset:     fset,
    47  			packages: make(map[string]*types.Package),
    48  			lookup:   lookup,
    49  		}
    50  
    51  	case "gccgo":
    52  		var inst gccgoimporter.GccgoInstallation
    53  		if err := inst.InitFromDriver("gccgo"); err != nil {
    54  			return nil
    55  		}
    56  		return &gccgoimports{
    57  			packages: make(map[string]*types.Package),
    58  			importer: inst.GetImporter(nil, nil),
    59  			lookup:   lookup,
    60  		}
    61  
    62  	case "source":
    63  		if lookup != nil {
    64  			panic("source importer for custom import path lookup not supported (issue #13847).")
    65  		}
    66  
    67  		return srcimporter.New(&build.Default, fset, make(map[string]*types.Package))
    68  	}
    69  
    70  	// compiler not supported
    71  	return nil
    72  }
    73  
    74  // For calls ForCompiler with a new FileSet.
    75  //
    76  // Deprecated: use ForCompiler, which populates a FileSet
    77  // with the positions of objects created by the importer.
    78  func For(compiler string, lookup Lookup) types.Importer {
    79  	return ForCompiler(token.NewFileSet(), compiler, lookup)
    80  }
    81  
    82  // Default returns an Importer for the compiler that built the running binary.
    83  // If available, the result implements types.ImporterFrom.
    84  func Default() types.Importer {
    85  	return For(runtime.Compiler, nil)
    86  }
    87  
    88  // gc importer
    89  
    90  type gcimports struct {
    91  	fset     *token.FileSet
    92  	packages map[string]*types.Package
    93  	lookup   Lookup
    94  }
    95  
    96  func (m *gcimports) Import(path string) (*types.Package, error) {
    97  	return m.ImportFrom(path, "" /* no vendoring */, 0)
    98  }
    99  
   100  func (m *gcimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
   101  	if mode != 0 {
   102  		panic("mode must be 0")
   103  	}
   104  	return gcimporter.Import(m.fset, m.packages, path, srcDir, m.lookup)
   105  }
   106  
   107  // gccgo importer
   108  
   109  type gccgoimports struct {
   110  	packages map[string]*types.Package
   111  	importer gccgoimporter.Importer
   112  	lookup   Lookup
   113  }
   114  
   115  func (m *gccgoimports) Import(path string) (*types.Package, error) {
   116  	return m.ImportFrom(path, "" /* no vendoring */, 0)
   117  }
   118  
   119  func (m *gccgoimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) {
   120  	if mode != 0 {
   121  		panic("mode must be 0")
   122  	}
   123  	return m.importer(m.packages, path, srcDir, m.lookup)
   124  }
   125  

View as plain text