go.tools/importer: rename Context to Config for consistency with go/types.

R=gri
CC=golang-dev
https://golang.org/cl/11485046
This commit is contained in:
Alan Donovan 2013-07-19 11:02:27 -04:00
Родитель 9f26deaa1d
Коммит 8a9eca10cd
6 изменённых файлов: 20 добавлений и 19 удалений

Просмотреть файл

@ -17,15 +17,15 @@ import (
// An Importer's methods are not thread-safe. // An Importer's methods are not thread-safe.
type Importer struct { type Importer struct {
context *Context // the client context config *Config // the client configuration
Fset *token.FileSet // position info for all files seen Fset *token.FileSet // position info for all files seen
Packages map[string]*PackageInfo // keys are import paths Packages map[string]*PackageInfo // keys are import paths
errors map[string]error // cache of errors by import path errors map[string]error // cache of errors by import path
} }
// Context specifies the supporting context for the importer. // Config specifies the configuration for the importer.
// //
type Context struct { type Config struct {
// TypeChecker contains options relating to the type checker. // TypeChecker contains options relating to the type checker.
// The Importer will override any user-supplied values for its // The Importer will override any user-supplied values for its
// Expr, Ident, ImplicitObj and Import fields; other fields // Expr, Ident, ImplicitObj and Import fields; other fields
@ -59,16 +59,16 @@ type Context struct {
type SourceLoader func(fset *token.FileSet, path string) (files []*ast.File, err error) type SourceLoader func(fset *token.FileSet, path string) (files []*ast.File, err error)
// New returns a new, empty Importer using configuration options // New returns a new, empty Importer using configuration options
// specified by context. // specified by config.
// //
func New(context *Context) *Importer { func New(config *Config) *Importer {
imp := &Importer{ imp := &Importer{
context: context, config: config,
Fset: token.NewFileSet(), Fset: token.NewFileSet(),
Packages: make(map[string]*PackageInfo), Packages: make(map[string]*PackageInfo),
errors: make(map[string]error), errors: make(map[string]error),
} }
imp.context.TypeChecker.Import = imp.doImport imp.config.TypeChecker.Import = imp.doImport
return imp return imp
} }
@ -95,7 +95,7 @@ func (imp *Importer) doImport(imports map[string]*types.Package, path string) (p
// a PackageInfo and update our map (imp.Packages) and the // a PackageInfo and update our map (imp.Packages) and the
// type-checker's map (imports). // type-checker's map (imports).
var info *PackageInfo var info *PackageInfo
if imp.context.Loader != nil { if imp.config.Loader != nil {
info, err = imp.LoadPackage(path) info, err = imp.LoadPackage(path)
} else if pkg, err = types.GcImport(imports, path); err == nil { } else if pkg, err = types.GcImport(imports, path); err == nil {
info = &PackageInfo{Pkg: pkg} info = &PackageInfo{Pkg: pkg}
@ -119,15 +119,17 @@ func (imp *Importer) doImport(imports map[string]*types.Package, path string) (p
// PackageInfo. // PackageInfo.
// //
// Not idempotent! // Not idempotent!
// Precondition: Importer.context.Loader != nil. // Precondition: Importer.config.Loader != nil.
// TODO(adonovan): fix: violated in call from CreatePackageFromArgs!
// Not thread-safe! // Not thread-safe!
// TODO(adonovan): rethink this API. // TODO(adonovan): rethink this API.
// //
//
func (imp *Importer) LoadPackage(importPath string) (*PackageInfo, error) { func (imp *Importer) LoadPackage(importPath string) (*PackageInfo, error) {
if imp.context.Loader == nil { if imp.config.Loader == nil {
panic("Importer.LoadPackage without a SourceLoader") panic("Importer.LoadPackage without a SourceLoader")
} }
files, err := imp.context.Loader(imp.Fset, importPath) files, err := imp.config.Loader(imp.Fset, importPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -163,7 +165,7 @@ func (imp *Importer) CreateSourcePackage(importPath string, files []*ast.File) *
Implicits: make(map[ast.Node]types.Object), Implicits: make(map[ast.Node]types.Object),
}, },
} }
pkgInfo.Pkg, pkgInfo.Err = imp.context.TypeChecker.Check(importPath, imp.Fset, files, &pkgInfo.Info) pkgInfo.Pkg, pkgInfo.Err = imp.config.TypeChecker.Check(importPath, imp.Fset, files, &pkgInfo.Info)
imp.Packages[importPath] = pkgInfo imp.Packages[importPath] = pkgInfo
return pkgInfo return pkgInfo
} }

Просмотреть файл

@ -235,7 +235,7 @@ func TestEnclosingFunction(t *testing.T) {
"900", "func@2.27"}, "900", "func@2.27"},
} }
for _, test := range tests { for _, test := range tests {
imp := importer.New(new(importer.Context)) // (NB: no Loader) imp := importer.New(new(importer.Config)) // (NB: no Loader)
f, start, end := findInterval(t, imp.Fset, test.input, test.substr) f, start, end := findInterval(t, imp.Fset, test.input, test.substr)
if f == nil { if f == nil {
continue continue

Просмотреть файл

@ -34,7 +34,7 @@ func main() {
w.Write(nil) // interface invoke of external declared method w.Write(nil) // interface invoke of external declared method
} }
` `
imp := importer.New(new(importer.Context)) // no Loader; uses GC importer imp := importer.New(new(importer.Config)) // no Loader; uses GC importer
f, err := parser.ParseFile(imp.Fset, "<input>", test, parser.DeclarationErrors) f, err := parser.ParseFile(imp.Fset, "<input>", test, parser.DeclarationErrors)
if err != nil { if err != nil {

Просмотреть файл

@ -37,7 +37,7 @@ func main() {
} }
` `
// Construct an importer. Imports will be loaded as if by 'go build'. // Construct an importer. Imports will be loaded as if by 'go build'.
imp := importer.New(&importer.Context{Loader: importer.MakeGoBuildLoader(nil)}) imp := importer.New(&importer.Config{Loader: importer.MakeGoBuildLoader(nil)})
// Parse the input file. // Parse the input file.
file, err := parser.ParseFile(imp.Fset, "hello.go", hello, parser.DeclarationErrors) file, err := parser.ParseFile(imp.Fset, "hello.go", hello, parser.DeclarationErrors)

Просмотреть файл

@ -146,10 +146,9 @@ func run(t *testing.T, dir, input string) bool {
inputs = append(inputs, dir+i) inputs = append(inputs, dir+i)
} }
impctx := &importer.Context{ imp := importer.New(&importer.Config{
Loader: importer.MakeGoBuildLoader(nil), Loader: importer.MakeGoBuildLoader(nil),
} })
imp := importer.New(impctx)
files, err := importer.ParseFiles(imp.Fset, ".", inputs...) files, err := importer.ParseFiles(imp.Fset, ".", inputs...)
if err != nil { if err != nil {
t.Errorf("ssa.ParseFiles(%s) failed: %s", inputs, err.Error()) t.Errorf("ssa.ParseFiles(%s) failed: %s", inputs, err.Error())

Просмотреть файл

@ -17,7 +17,7 @@ import (
) )
func TestObjValueLookup(t *testing.T) { func TestObjValueLookup(t *testing.T) {
imp := importer.New(new(importer.Context)) // (uses GCImporter) imp := importer.New(new(importer.Config)) // (uses GCImporter)
f, err := parser.ParseFile(imp.Fset, "testdata/objlookup.go", nil, parser.DeclarationErrors|parser.ParseComments) f, err := parser.ParseFile(imp.Fset, "testdata/objlookup.go", nil, parser.DeclarationErrors|parser.ParseComments)
if err != nil { if err != nil {
t.Errorf("parse error: %s", err) t.Errorf("parse error: %s", err)