Skip to content

Commit b98a76c

Browse files
committed
loader: load packages using Go modules
This commit replaces the existing ad-hoc package loader with a package loader that uses the x/tools/go/packages package to find all to-be-loaded packages.
1 parent 522f228 commit b98a76c

File tree

5 files changed

+141
-287
lines changed

5 files changed

+141
-287
lines changed

compiler/compiler.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func Compile(pkgName string, machine llvm.TargetMachine, config *compileopts.Con
156156
Compiler: "gc", // must be one of the recognized compilers
157157
BuildTags: c.BuildTags(),
158158
},
159+
Tests: c.TestConfig.CompileTestBinary,
159160
TypeChecker: types.Config{
160161
Sizes: &stdSizes{
161162
IntSize: int64(c.targetData.TypeAllocSize(c.intType)),
@@ -169,33 +170,17 @@ func Compile(pkgName string, machine llvm.TargetMachine, config *compileopts.Con
169170
ClangHeaders: c.ClangHeaders,
170171
}
171172

172-
if strings.HasSuffix(pkgName, ".go") {
173-
_, err = lprogram.ImportFile(pkgName)
174-
if err != nil {
175-
return c.mod, nil, []error{err}
176-
}
177-
} else {
178-
_, err = lprogram.Import(pkgName, wd, token.Position{
179-
Filename: "build command-line-arguments",
180-
})
181-
if err != nil {
182-
return c.mod, nil, []error{err}
183-
}
184-
}
185-
186-
_, err = lprogram.Import("runtime", "", token.Position{
187-
Filename: "build default import",
188-
})
173+
err = lprogram.Load(pkgName)
189174
if err != nil {
190175
return c.mod, nil, []error{err}
191176
}
192177

193-
err = lprogram.Parse(c.TestConfig.CompileTestBinary)
178+
err = lprogram.Parse()
194179
if err != nil {
195180
return c.mod, nil, []error{err}
196181
}
197182

198-
c.ir = ir.NewProgram(lprogram, pkgName)
183+
c.ir = ir.NewProgram(lprogram)
199184

200185
// Run a simple dead code elimination pass.
201186
err = c.ir.SimpleDCE()
@@ -339,8 +324,11 @@ func Compile(pkgName string, machine llvm.TargetMachine, config *compileopts.Con
339324
// Gather the list of (C) file paths that should be included in the build.
340325
var extraFiles []string
341326
for _, pkg := range c.ir.LoaderProgram.Sorted() {
342-
for _, file := range pkg.CFiles {
343-
extraFiles = append(extraFiles, filepath.Join(pkg.Package.Dir, file))
327+
for _, file := range pkg.OtherFiles {
328+
switch strings.ToLower(filepath.Ext(file)) {
329+
case ".c":
330+
extraFiles = append(extraFiles, file)
331+
}
344332
}
345333
}
346334

ir/ir.go

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,45 +63,24 @@ const (
6363
)
6464

6565
// Create and initialize a new *Program from a *ssa.Program.
66-
func NewProgram(lprogram *loader.Program, mainPath string) *Program {
66+
func NewProgram(lprogram *loader.Program) *Program {
6767
program := lprogram.LoadSSA()
6868
program.Build()
6969

7070
// Find the main package, which is a bit difficult when running a .go file
7171
// directly.
72-
mainPkg := program.ImportedPackage(mainPath)
73-
if mainPkg == nil {
74-
for _, pkgInfo := range program.AllPackages() {
75-
if pkgInfo.Pkg.Name() == "main" {
76-
if mainPkg != nil {
77-
panic("more than one main package found")
78-
}
79-
mainPkg = pkgInfo
80-
}
81-
}
82-
}
72+
mainPkg := program.ImportedPackage(lprogram.MainPkg.PkgPath)
8373
if mainPkg == nil {
8474
panic("could not find main package")
8575
}
8676

8777
// Make a list of packages in import order.
8878
packageList := []*ssa.Package{}
8979
packageSet := map[string]struct{}{}
90-
worklist := []string{"runtime", mainPath}
80+
worklist := []string{"runtime", lprogram.MainPkg.PkgPath}
9181
for len(worklist) != 0 {
9282
pkgPath := worklist[0]
93-
var pkg *ssa.Package
94-
if pkgPath == mainPath {
95-
pkg = mainPkg // necessary for compiling individual .go files
96-
} else {
97-
pkg = program.ImportedPackage(pkgPath)
98-
}
99-
if pkg == nil {
100-
// Non-SSA package (e.g. cgo).
101-
packageSet[pkgPath] = struct{}{}
102-
worklist = worklist[1:]
103-
continue
104-
}
83+
pkg := program.ImportedPackage(pkgPath)
10584
if _, ok := packageSet[pkgPath]; ok {
10685
// Package already in the final package list.
10786
worklist = worklist[1:]

loader/errors.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package loader
22

3-
import (
4-
"go/token"
5-
"strings"
6-
)
7-
83
// Errors contains a list of parser errors or a list of typechecker errors for
94
// the given package.
105
type Errors struct {
@@ -15,25 +10,3 @@ type Errors struct {
1510
func (e Errors) Error() string {
1611
return "could not compile: " + e.Errs[0].Error()
1712
}
18-
19-
// ImportCycleErrors is returned when encountering an import cycle. The list of
20-
// packages is a list from the root package to the leaf package that imports one
21-
// of the packages in the list.
22-
type ImportCycleError struct {
23-
Packages []string
24-
ImportPositions []token.Position
25-
}
26-
27-
func (e *ImportCycleError) Error() string {
28-
var msg strings.Builder
29-
msg.WriteString("import cycle:\n\t")
30-
msg.WriteString(strings.Join(e.Packages, "\n\t"))
31-
msg.WriteString("\n at ")
32-
for i, pos := range e.ImportPositions {
33-
if i > 0 {
34-
msg.WriteString(", ")
35-
}
36-
msg.WriteString(pos.String())
37-
}
38-
return msg.String()
39-
}

0 commit comments

Comments
 (0)