-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbuilder.go
329 lines (286 loc) · 8.83 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// builder builds packages
package main
import (
"github.com/DQNEO/minigo/stdlib/fmt"
"github.com/DQNEO/minigo/stdlib/io/ioutil"
"github.com/DQNEO/minigo/stdlib/path"
"github.com/DQNEO/minigo/stdlib/strings"
"os"
)
func getGOPATH() string {
envgopath := os.Getenv("GOPATH")
if envgopath == "" {
// default GOPATH
return os.Getenv("HOME") + "/go"
}
return envgopath
}
// "fmt" => "/stdlib/fmt"
// "github.com/DQNEO/minigo/stdlib/fmt" => "/stdlib/fmt"
// "./mylib" => "./mylib"
// "github.com/foo/bar" => "$GOPATH/src/github.com/foo/bar"
func normalizeImportPath(currentPath string, pth string) normalizedPackagePath {
if strings.HasPrefix(pth, "./") {
// parser relative pth
// "./mylib" => "/mylib"
return normalizedPackagePath("./" + currentPath + pth[1:])
} else if strings.HasPrefix(pth, "github.com/DQNEO/minigo/stdlib/") {
// Special treatment for stdlib
// "github.com/DQNEO/minigo/stdlib/fmt" => "/stdlib/fmt"
renamed := "/stdlib" + pth[len("github.com/DQNEO/minigo/stdlib/") -1:]
return normalizedPackagePath(renamed)
} else if strings.HasPrefix(pth, "github.com/") {
gopath := getGOPATH()
return normalizedPackagePath(gopath + "/src/" + pth)
} else {
// "io/ioutil" => "/stdlib/io/ioutil"
return normalizedPackagePath("/stdlib/" + pth)
}
}
func getParsingDir(sourceFile string) string {
found := strings.LastIndexByte(sourceFile, '/')
if found == -1 {
return "."
}
return path.Dir(sourceFile)
}
// analyze imports of a given go source
func parseImportsFromFile(sourceFile string) importMap {
p := &parser{
parsingDir: getParsingDir(sourceFile),
}
astFile := p.ParseFile(sourceFile, nil, true)
return astFile.imports
}
// analyze imports of given go files
func parseImports(sourceFiles []string) importMap {
var imported importMap = make(map[normalizedPackagePath]bool)
for _, sourceFile := range sourceFiles {
importsInFile := parseImportsFromFile(sourceFile)
for name, _ := range importsInFile {
imported[name] = true
}
}
return imported
}
// inject builtin functions into the universe scope
func compileUniverse(universe *Scope) *AstPackage {
p := &parser{
packagePath: normalizeImportPath("", "builtin"), // anything goes
packageName: identifier("builtin"),
}
f := p.ParseFile("stdlib/builtin/builtin.go", universe, false)
attachMethodsToTypes(f.methods, p.packageBlockScope)
inferTypes(f.uninferredGlobals, f.uninferredLocals)
calcStructSize(f.dynamicTypes)
return &AstPackage{
name: identifier(""),
files: []*AstFile{f},
stringLiterals: f.stringLiterals,
dynamicTypes: f.dynamicTypes,
}
}
// "path/dir" => {"path/dir/a.go", ...}
func getPackageFiles(pkgDir string) []string {
f, err := os.Open(pkgDir)
if err != nil {
panic(err)
}
names, err := f.Readdirnames(-1)
if err != nil {
panic(err)
}
var sourceFiles []string
for _, name := range names {
if !strings.HasSuffix(name, ".go") {
continue
}
// minigo ignores ignore.go
if name == "ignore.go" {
continue
}
sourceFiles = append(sourceFiles, pkgDir+"/"+name)
}
return sourceFiles
}
// inject unsafe package
func compileUnsafe(universe *Scope) *AstPackage {
pkgName := identifier("unsafe")
pkgPath := normalizeImportPath("", "unsafe") // need to be normalized because it's imported by iruntime
pkgScope := newScope(nil, pkgName)
symbolTable.allScopes[pkgPath] = pkgScope
sourceFiles := getPackageFiles(convertStdPath(pkgPath))
pkg := ParseFiles(pkgName, pkgPath, pkgScope, sourceFiles)
makePkg(pkg, universe)
return pkg
}
const IRuntimePath normalizedPackagePath = "iruntime"
const MainPath normalizedPackagePath = "main"
const IRuntimePkgName identifier = "iruntime"
var pkgIRuntime *AstPackage
// inject runtime things into the universe scope
func compileRuntime(universe *Scope) *AstPackage {
pkgName := IRuntimePkgName
pkgPath := IRuntimePath
pkgScope := newScope(nil, pkgName)
symbolTable.allScopes[pkgPath] = pkgScope
sourceFiles := getPackageFiles("internal/runtime")
pkg := ParseFiles(pkgName, pkgPath, universe, sourceFiles)
makePkg(pkg, nil) // avoid circulated reference
pkgIRuntime = pkg
// read asm files
for _, asmfile := range []string{"internal/runtime/asm_amd64.s", "internal/runtime/runtime.s"} {
buf, _ := ioutil.ReadFile(asmfile)
pkgIRuntime.asm = append(pkgIRuntime.asm, string(buf))
}
return pkg
}
func makePkg(pkg *AstPackage, universe *Scope) *AstPackage {
resolveIdents(pkg, universe)
attachMethodsToTypes(pkg.methods, pkg.scope)
inferTypes(pkg.uninferredGlobals, pkg.uninferredLocals)
calcStructSize(pkg.dynamicTypes)
return pkg
}
// compileMainFiles parses files into *AstPackage
func compileMainFiles(universe *Scope, sourceFiles []string) *AstPackage {
// compile the main package
pkgName := identifier("main")
pkgScope := newScope(nil, pkgName)
mainPkg := ParseFiles("main", MainPath, pkgScope, sourceFiles)
if parseOnly {
if debugAst {
mainPkg.dump()
}
return nil
}
mainPkg = makePkg(mainPkg, universe)
if debugAst {
mainPkg.dump()
}
if resolveOnly {
return nil
}
return mainPkg
}
type importMap map[normalizedPackagePath]bool
func parseImportRecursive(dep map[normalizedPackagePath]importMap, directDependencies importMap) {
for pth, _ := range directDependencies {
files := getPackageFiles(convertStdPath(pth))
var imports importMap = make(map[normalizedPackagePath]bool)
for _, file := range files {
imprts := parseImportsFromFile(file)
for k, _ := range imprts {
imports[k] = true
}
}
dep[pth] = imports
parseImportRecursive(dep, imports)
}
}
func removeNode(nodes map[normalizedPackagePath]importMap, pkgToRemove normalizedPackagePath) map[normalizedPackagePath]importMap {
var newNodes map[normalizedPackagePath]importMap = make(map[normalizedPackagePath]importMap)
for _path, imports := range nodes {
if _path == pkgToRemove {
continue
}
var newimports importMap = make(map[normalizedPackagePath]bool)
for pkg2, _ := range imports {
if pkg2 == pkgToRemove {
continue
}
newimports[pkg2] = true
}
newNodes[_path] = newimports
}
return newNodes
}
func dumpDep(dep map[string]importMap) {
debugf("#------------- dep -----------------")
for spkgName, imports := range dep {
debugf("# %s has %d imports:", spkgName, len(imports))
for sspkgName, _ := range imports {
debugf("# %s", sspkgName)
}
}
}
func resolveDependency(directDependencies importMap) []normalizedPackagePath {
var sortedUniqueImports []normalizedPackagePath
var nodes map[normalizedPackagePath]importMap = make(map[normalizedPackagePath]importMap)
parseImportRecursive(nodes, directDependencies)
for {
if len(nodes) == 0 {
return sortedUniqueImports
}
for node, children := range nodes {
if len(children) == 0 {
sortedUniqueImports = append(sortedUniqueImports, node)
nodes = removeNode(nodes, node)
}
}
}
}
// if "/stdlib/foo" => "./stdlib/foo"
func convertStdPath(pth normalizedPackagePath) string {
if strings.HasPrefix(string(pth), "/stdlib/") {
return fmt.Sprintf(".%s", string(pth))
}
return string(pth)
}
// Compile dependent packages (both of stdlib and 3rd party)
func compilePackages(universe *Scope, directDependencies importMap) map[normalizedPackagePath]*AstPackage {
sortedUniqueImports := resolveDependency(directDependencies)
var compiledPkgs map[normalizedPackagePath]*AstPackage = make(map[normalizedPackagePath]*AstPackage)
for _, pth := range sortedUniqueImports {
files := getPackageFiles(convertStdPath(pth))
pkgScope := newScope(nil, identifier(pth))
symbolTable.allScopes[pth] = pkgScope
pkgShortName := path.Base(string(pth))
pkg := ParseFiles(identifier(pkgShortName), pth, pkgScope, files)
pkg = makePkg(pkg, universe)
compiledPkgs[pth] = pkg
}
return compiledPkgs
}
type Program struct {
packages []*AstPackage
methodTable map[int][]string
}
func build(pkgUniverse *AstPackage, pkgUnsafe *AstPackage, pkgIRuntime *AstPackage, stdPkgs map[normalizedPackagePath]*AstPackage, pkgMain *AstPackage) *Program {
var packages []*AstPackage
packages = append(packages, pkgUniverse)
packages = append(packages, pkgUnsafe)
packages = append(packages, pkgIRuntime)
for _, pkg := range stdPkgs {
packages = append(packages, pkg)
}
packages = append(packages, pkgMain)
var dynamicTypes []*Gtype
var funcs []*DeclFunc
for _, pkg := range packages {
collectDecls(pkg)
if pkg == pkgUniverse {
setStringLables(pkg, "pkgUniverse")
} else {
setStringLables(pkg, pkg.name)
}
for _, dt := range pkg.dynamicTypes {
dynamicTypes = append(dynamicTypes, dt)
}
for _, fn := range pkg.funcs {
funcs = append(funcs, fn)
}
setTypeIds(pkg.namedTypes)
}
// Do restructuring of local nodes
for _, pkg := range packages {
for _, fnc := range pkg.funcs {
fnc = walkFunc(fnc)
}
}
symbolTable.uniquedDTypes = uniqueDynamicTypes(dynamicTypes)
program := &Program{}
program.packages = packages
program.methodTable = composeMethodTable(funcs)
return program
}