diff --git a/importer/importer.go b/importer/importer.go index 80f8250b..b681fc62 100644 --- a/importer/importer.go +++ b/importer/importer.go @@ -229,7 +229,7 @@ func (imp *Importer) importSource(path string, ii *importInfo) { } // Type-check the package. - ii.info = imp.typeCheck(path, files) + ii.info = imp.CreatePackage(path, files...) // We needn't wait for the prefetching goroutines to // finish. Each one either runs quickly and populates @@ -243,7 +243,8 @@ func (imp *Importer) importSource(path string, ii *importInfo) { } } -// typeCheck invokes the type-checker on files and returns a +// CreatePackage creates and type-checks a package from the specified +// list of parsed files, importing their dependencies. It returns a // PackageInfo containing the resulting types.Package, the ASTs, and // other type information. // @@ -254,13 +255,13 @@ func (imp *Importer) importSource(path string, ii *importInfo) { // be unique; for example, it is possible to construct two distinct // packages both named "main". // -// The resulting package is added to imp.allPackages, but is not -// importable unless it is inserted in the imp.imported map. +// The resulting package is accessible via AllPackages() but is not +// importable, i.e. no 'import' spec can resolve to it. // -// This function always succeeds, but the package may contain type +// CreatePackage never fails, but the resulting package may contain type // errors; the first of these is recorded in PackageInfo.Err. // -func (imp *Importer) typeCheck(path string, files []*ast.File) *PackageInfo { +func (imp *Importer) CreatePackage(path string, files ...*ast.File) *PackageInfo { info := &PackageInfo{ Files: files, Info: types.Info{ @@ -277,23 +278,10 @@ func (imp *Importer) typeCheck(path string, files []*ast.File) *PackageInfo { return info } -// LoadMainPackage creates and type-checks a package called "main" from -// the specified list of parsed files, importing its dependencies. -// -// The resulting package is not importable, i.e. no 'import' spec can -// resolve to it. LoadMainPackage is provided as an aid to testing. -// -// LoadMainPackage never fails, but the resulting package may contain -// type errors. -// -func (imp *Importer) LoadMainPackage(files ...*ast.File) *PackageInfo { - return imp.typeCheck("main", files) -} - // InitialPackagesUsage is a partial usage message that client // applications may wish to include in their -help output. const InitialPackagesUsage = ` - is a list of arguments denoting a set of initial pacakges. + is a list of arguments denoting a set of initial packages. Each argument may take one of two forms: 1. A comma-separated list of *.go source files. @@ -307,7 +295,7 @@ Each argument may take one of two forms: The package's directory is found relative to the $GOROOT and $GOPATH using similar logic to 'go build', and the *.go files in - that directory are loaded and parsed, and type-checked as a single + that directory are loaded, parsed and type-checked as a single package. In addition, all *_test.go files in the directory are then loaded @@ -438,7 +426,7 @@ func (imp *Importer) LoadInitialPackages(args []string) ([]*PackageInfo, []strin } } else { // create package - info = imp.typeCheck(pkg.path, pkg.files) + info = imp.CreatePackage(pkg.path, pkg.files...) } infos = append(infos, info) } diff --git a/importer/source_test.go b/importer/source_test.go index a9911f8a..161d8584 100644 --- a/importer/source_test.go +++ b/importer/source_test.go @@ -249,7 +249,7 @@ func TestEnclosingFunction(t *testing.T) { t.Errorf("EnclosingFunction(%q) not exact", test.substr) continue } - mainInfo := imp.LoadMainPackage(f) + mainInfo := imp.CreatePackage("main", f) prog := ssa.NewProgram(imp.Fset, 0) if err := prog.CreatePackages(imp); err != nil { t.Error(err) diff --git a/pointer/example_test.go b/pointer/example_test.go index 9f78b696..024dea42 100644 --- a/pointer/example_test.go +++ b/pointer/example_test.go @@ -51,8 +51,8 @@ func main() { return } - // Create a "main" package containing one file. - mainInfo := imp.LoadMainPackage(file) + // Create single-file main package and import its dependencies. + mainInfo := imp.CreatePackage("main", file) // Create SSA-form program representation. var mode ssa.BuilderMode diff --git a/pointer/pointer_test.go b/pointer/pointer_test.go index 3e48bb24..460cc8dd 100644 --- a/pointer/pointer_test.go +++ b/pointer/pointer_test.go @@ -177,8 +177,8 @@ func doOneInput(input, filename string) bool { return false } - // Load main package and its dependencies. - info := imp.LoadMainPackage(f) + // Create single-file main package and import its dependencies. + info := imp.CreatePackage("main", f) // SSA creation + building. prog := ssa.NewProgram(imp.Fset, ssa.SanityCheckFunctions) diff --git a/ssa/builder_test.go b/ssa/builder_test.go index 408a823a..402a856b 100644 --- a/ssa/builder_test.go +++ b/ssa/builder_test.go @@ -46,7 +46,7 @@ func main() { return } - mainInfo := imp.LoadMainPackage(f) + mainInfo := imp.CreatePackage("main", f) prog := ssa.NewProgram(imp.Fset, ssa.SanityCheckFunctions) if err := prog.CreatePackages(imp); err != nil { diff --git a/ssa/example_test.go b/ssa/example_test.go index d4c44ffe..efe85758 100644 --- a/ssa/example_test.go +++ b/ssa/example_test.go @@ -51,8 +51,8 @@ func main() { return } - // Create a "main" package containing one file. - mainInfo := imp.LoadMainPackage(file) + // Create single-file main package and import its dependencies. + mainInfo := imp.CreatePackage("main", file) // Create SSA-form program representation. var mode ssa.BuilderMode diff --git a/ssa/interp/interp_test.go b/ssa/interp/interp_test.go index 89411cb4..1fb4a8a4 100644 --- a/ssa/interp/interp_test.go +++ b/ssa/interp/interp_test.go @@ -188,7 +188,7 @@ func run(t *testing.T, dir, input string) bool { }() hint = fmt.Sprintf("To dump SSA representation, run:\n%% go build code.google.com/p/go.tools/cmd/ssadump && ./ssadump -build=CFP %s\n", input) - mainInfo := imp.LoadMainPackage(files...) + mainInfo := imp.CreatePackage("main", files...) prog := ssa.NewProgram(imp.Fset, ssa.SanityCheckFunctions) if err := prog.CreatePackages(imp); err != nil { diff --git a/ssa/source_test.go b/ssa/source_test.go index 36272cc3..3c123c20 100644 --- a/ssa/source_test.go +++ b/ssa/source_test.go @@ -46,7 +46,7 @@ func TestObjValueLookup(t *testing.T) { } } - mainInfo := imp.LoadMainPackage(f) + mainInfo := imp.CreatePackage("main", f) prog := ssa.NewProgram(imp.Fset, 0 /*|ssa.LogFunctions*/) if err := prog.CreatePackages(imp); err != nil { @@ -184,7 +184,7 @@ func TestValueForExpr(t *testing.T) { return } - mainInfo := imp.LoadMainPackage(f) + mainInfo := imp.CreatePackage("main", f) prog := ssa.NewProgram(imp.Fset, 0) if err := prog.CreatePackages(imp); err != nil {