Commit f81466ce authored by Robert Griesemer's avatar Robert Griesemer

go/importer: support importing directly from source

For #11415.

Change-Id: I5da39dad059113cfc4276152390aa4925bd18862
Reviewed-on: https://go-review.googlesource.com/37405
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent bab19104
...@@ -217,7 +217,7 @@ var pkgDeps = map[string][]string{ ...@@ -217,7 +217,7 @@ var pkgDeps = map[string][]string{
// Go type checking. // Go type checking.
"go/constant": {"L4", "go/token", "math/big"}, "go/constant": {"L4", "go/token", "math/big"},
"go/importer": {"L4", "go/internal/gcimporter", "go/internal/gccgoimporter", "go/types"}, "go/importer": {"L4", "go/build", "go/internal/gccgoimporter", "go/internal/gcimporter", "go/internal/srcimporter", "go/token", "go/types"},
"go/internal/gcimporter": {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"}, "go/internal/gcimporter": {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
"go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"}, "go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"},
"go/internal/srcimporter": {"L4", "fmt", "go/ast", "go/build", "go/parser", "go/token", "go/types", "path/filepath"}, "go/internal/srcimporter": {"L4", "fmt", "go/ast", "go/build", "go/parser", "go/token", "go/types", "path/filepath"},
......
...@@ -6,8 +6,11 @@ ...@@ -6,8 +6,11 @@
package importer package importer
import ( import (
"go/build"
"go/internal/gccgoimporter" "go/internal/gccgoimporter"
"go/internal/gcimporter" "go/internal/gcimporter"
"go/internal/srcimporter"
"go/token"
"go/types" "go/types"
"io" "io"
"runtime" "runtime"
...@@ -17,22 +20,30 @@ import ( ...@@ -17,22 +20,30 @@ import (
// a given import path, or an error if no matching package is found. // a given import path, or an error if no matching package is found.
type Lookup func(path string) (io.ReadCloser, error) type Lookup func(path string) (io.ReadCloser, error)
// For returns an Importer for the given compiler and lookup interface, // For returns an Importer for importing from installed packages
// or nil. Supported compilers are "gc", and "gccgo". If lookup is nil, // for the compilers "gc" and "gccgo", or for importing directly
// the default package lookup mechanism for the given compiler is used. // from the source if the compiler argument is "source". In this
// latter case, importing may fail under circumstances where the
// exported API is not entirely defined in pure Go source code
// (if the package API depends on cgo-defined entities, the type
// checker won't have access to those).
//
// If lookup is nil, the default package lookup mechanism for the
// given compiler is used.
//
// BUG(issue13847): For does not support non-nil lookup functions. // BUG(issue13847): For does not support non-nil lookup functions.
func For(compiler string, lookup Lookup) types.Importer { func For(compiler string, lookup Lookup) types.Importer {
switch compiler { switch compiler {
case "gc": case "gc":
if lookup != nil { if lookup != nil {
panic("gc importer for custom import path lookup not yet implemented") panic("gc importer for custom import path lookup not supported (issue #13847).")
} }
return make(gcimports) return make(gcimports)
case "gccgo": case "gccgo":
if lookup != nil { if lookup != nil {
panic("gccgo importer for custom import path lookup not yet implemented") panic("gccgo importer for custom import path lookup not supported (issue #13847).")
} }
var inst gccgoimporter.GccgoInstallation var inst gccgoimporter.GccgoInstallation
...@@ -43,6 +54,13 @@ func For(compiler string, lookup Lookup) types.Importer { ...@@ -43,6 +54,13 @@ func For(compiler string, lookup Lookup) types.Importer {
packages: make(map[string]*types.Package), packages: make(map[string]*types.Package),
importer: inst.GetImporter(nil, nil), importer: inst.GetImporter(nil, nil),
} }
case "source":
if lookup != nil {
panic("source importer for custom import path lookup not supported (issue #13847).")
}
return srcimporter.New(&build.Default, token.NewFileSet(), make(map[string]*types.Package))
} }
// compiler not supported // compiler not supported
...@@ -55,7 +73,7 @@ func Default() types.Importer { ...@@ -55,7 +73,7 @@ func Default() types.Importer {
return For(runtime.Compiler, nil) return For(runtime.Compiler, nil)
} }
// gc support // gc importer
type gcimports map[string]*types.Package type gcimports map[string]*types.Package
...@@ -70,7 +88,7 @@ func (m gcimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*type ...@@ -70,7 +88,7 @@ func (m gcimports) ImportFrom(path, srcDir string, mode types.ImportMode) (*type
return gcimporter.Import(m, path, srcDir) return gcimporter.Import(m, path, srcDir)
} }
// gccgo support // gccgo importer
type gccgoimports struct { type gccgoimports struct {
packages map[string]*types.Package packages map[string]*types.Package
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment