[dev.typeparams] cmd/compile: refactor import logic

This CL refactors noder's package import logic so it's easier to reuse
with types2 and gcimports. In particular, this allows the types2
integration to now support vendored packages.

Change-Id: I1fd98ad612b4683d2e1ac640839e64de1fa7324b
Reviewed-on: https://go-review.googlesource.com/c/go/+/282919
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Trust: Robert Griesemer <gri@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Matthew Dempsky 2021-01-08 23:44:31 -08:00
parent 106aa941df
commit 099599662d
5 changed files with 142 additions and 166 deletions

View file

@ -9,7 +9,6 @@ import (
"fmt"
"go/constant"
"go/token"
"io"
"os"
"path/filepath"
"runtime"
@ -20,7 +19,6 @@ import (
"cmd/compile/internal/base"
"cmd/compile/internal/dwarfgen"
"cmd/compile/internal/importer"
"cmd/compile/internal/ir"
"cmd/compile/internal/syntax"
"cmd/compile/internal/typecheck"
@ -126,13 +124,6 @@ func ParseFiles(filenames []string) (lines uint) {
},
Importer: &gcimports{
packages: make(map[string]*types2.Package),
lookup: func(path string) (io.ReadCloser, error) {
file, ok := findpkg(path)
if !ok {
return nil, fmt.Errorf("can't find import: %q", path)
}
return os.Open(file)
},
},
Sizes: &gcSizes{},
}
@ -255,23 +246,6 @@ func Package() {
}
// Temporary import helper to get type2-based type-checking going.
type gcimports struct {
packages map[string]*types2.Package
lookup func(path string) (io.ReadCloser, error)
}
func (m *gcimports) Import(path string) (*types2.Package, error) {
return m.ImportFrom(path, "" /* no vendoring */, 0)
}
func (m *gcimports) ImportFrom(path, srcDir string, mode types2.ImportMode) (*types2.Package, error) {
if mode != 0 {
panic("mode must be 0")
}
return importer.Import(m.packages, path, srcDir, m.lookup)
}
func (p *noder) errorAt(pos syntax.Pos, format string, args ...interface{}) {
base.ErrorfAt(p.makeXPos(pos), format, args...)
}
@ -483,7 +457,7 @@ func (p *noder) importDecl(imp *syntax.ImportDecl) {
p.checkUnused(pragma)
}
ipkg := importfile(p.basicLit(imp.Path))
ipkg := importfile(imp)
if ipkg == nil {
if base.Errors() == 0 {
base.Fatalf("phase error in import")
@ -498,11 +472,6 @@ func (p *noder) importDecl(imp *syntax.ImportDecl) {
p.importedEmbed = true
}
if !ipkg.Direct {
typecheck.Target.Imports = append(typecheck.Target.Imports, ipkg)
}
ipkg.Direct = true
var my *types.Sym
if imp.LocalPkgName != nil {
my = p.name(imp.LocalPkgName)