github.com/zebozhuang/go@v0.0.0-20200207033046-f8a98f6f5c5d/src/go/importer/importer.go (about) 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 // For 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. 33 // 34 // BUG(issue13847): For does not support non-nil lookup functions. 35 func For(compiler string, lookup Lookup) types.Importer { 36 switch compiler { 37 case "gc": 38 if lookup != nil { 39 panic("gc importer for custom import path lookup not supported (issue #13847).") 40 } 41 42 return make(gcimports) 43 44 case "gccgo": 45 if lookup != nil { 46 panic("gccgo importer for custom import path lookup not supported (issue #13847).") 47 } 48 49 var inst gccgoimporter.GccgoInstallation 50 if err := inst.InitFromDriver("gccgo"); err != nil { 51 return nil 52 } 53 return &gccgoimports{ 54 packages: make(map[string]*types.Package), 55 importer: inst.GetImporter(nil, nil), 56 } 57 58 case "source": 59 if lookup != nil { 60 panic("source importer for custom import path lookup not supported (issue #13847).") 61 } 62 63 return srcimporter.New(&build.Default, token.NewFileSet(), make(map[string]*types.Package)) 64 } 65 66 // compiler not supported 67 return nil 68 } 69 70 // Default returns an Importer for the compiler that built the running binary. 71 // If available, the result implements types.ImporterFrom. 72 func Default() types.Importer { 73 return For(runtime.Compiler, nil) 74 } 75 76 // gc importer 77 78 type gcimports map[string]*types.Package 79 80 func (m gcimports) Import(path string) (*types.Package, error) { 81 return m.ImportFrom(path, "" /* no vendoring */, 0) 82 } 83 84 func (m gcimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) { 85 if mode != 0 { 86 panic("mode must be 0") 87 } 88 return gcimporter.Import(m, path, srcDir) 89 } 90 91 // gccgo importer 92 93 type gccgoimports struct { 94 packages map[string]*types.Package 95 importer gccgoimporter.Importer 96 } 97 98 func (m *gccgoimports) Import(path string) (*types.Package, error) { 99 return m.ImportFrom(path, "" /* no vendoring */, 0) 100 } 101 102 func (m *gccgoimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*types.Package, error) { 103 if mode != 0 { 104 panic("mode must be 0") 105 } 106 // TODO(gri) pass srcDir 107 return m.importer(m.packages, path) 108 }