diff --git a/importer/importer.go b/importer/importer.go index 099dadd48..ec5aafaea 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -17,15 +17,15 @@ import ( // An Importer's methods are not thread-safe. type Importer struct { - context *Context // the client context + config *Config // the client configuration Fset *token.FileSet // position info for all files seen Packages map[string]*PackageInfo // keys are import paths 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. // The Importer will override any user-supplied values for its // 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) // 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{ - context: context, + config: config, Fset: token.NewFileSet(), Packages: make(map[string]*PackageInfo), errors: make(map[string]error), } - imp.context.TypeChecker.Import = imp.doImport + imp.config.TypeChecker.Import = imp.doImport 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 // type-checker's map (imports). var info *PackageInfo - if imp.context.Loader != nil { + if imp.config.Loader != nil { info, err = imp.LoadPackage(path) } else if pkg, err = types.GcImport(imports, path); err == nil { info = &PackageInfo{Pkg: pkg} @@ -119,15 +119,17 @@ func (imp *Importer) doImport(imports map[string]*types.Package, path string) (p // PackageInfo. // // Not idempotent! -// Precondition: Importer.context.Loader != nil. +// Precondition: Importer.config.Loader != nil. +// TODO(adonovan): fix: violated in call from CreatePackageFromArgs! // Not thread-safe! // TODO(adonovan): rethink this API. // +// func (imp *Importer) LoadPackage(importPath string) (*PackageInfo, error) { - if imp.context.Loader == nil { + if imp.config.Loader == nil { 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 { return nil, err } @@ -163,7 +165,7 @@ func (imp *Importer) CreateSourcePackage(importPath string, files []*ast.File) * 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 return pkgInfo } diff --git a/importer/source_test.go b/importer/source_test.go index 821d18880..3be56cd7a 100644 --- a/importer/source_test.go +++ b/importer/source_test.go @@ -235,7 +235,7 @@ func TestEnclosingFunction(t *testing.T) { "900", "func@2.27"}, } 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) if f == nil { continue diff --git a/ssa/builder_test.go b/ssa/builder_test.go index 98cd2f60e..e33f81da6 100644 --- a/ssa/builder_test.go +++ b/ssa/builder_test.go @@ -34,7 +34,7 @@ func main() { 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, "", test, parser.DeclarationErrors) if err != nil { diff --git a/ssa/example_test.go b/ssa/example_test.go index b6c96d995..48fa8237f 100644 --- a/ssa/example_test.go +++ b/ssa/example_test.go @@ -37,7 +37,7 @@ func main() { } ` // 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. file, err := parser.ParseFile(imp.Fset, "hello.go", hello, parser.DeclarationErrors) diff --git a/ssa/interp/interp_test.go b/ssa/interp/interp_test.go index 12d1cb6fe..553ae679e 100644 --- a/ssa/interp/interp_test.go +++ b/ssa/interp/interp_test.go @@ -146,10 +146,9 @@ func run(t *testing.T, dir, input string) bool { inputs = append(inputs, dir+i) } - impctx := &importer.Context{ + imp := importer.New(&importer.Config{ Loader: importer.MakeGoBuildLoader(nil), - } - imp := importer.New(impctx) + }) files, err := importer.ParseFiles(imp.Fset, ".", inputs...) if err != nil { t.Errorf("ssa.ParseFiles(%s) failed: %s", inputs, err.Error()) diff --git a/ssa/source_test.go b/ssa/source_test.go index 9bc07662c..3af1fd99c 100644 --- a/ssa/source_test.go +++ b/ssa/source_test.go @@ -17,7 +17,7 @@ import ( ) 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) if err != nil { t.Errorf("parse error: %s", err)