compiler: emit bytecode for unused exported functions

Exported functions should always be present in byte-code.
This will be needed later when arbitrary method calls are allowed.
This commit is contained in:
Evgenii Stratonikov 2020-07-23 17:40:49 +03:00
parent 54d7882acf
commit e87eba51f9
2 changed files with 8 additions and 2 deletions

View file

@ -113,10 +113,11 @@ func lastStmtIsReturn(decl *ast.FuncDecl) (b bool) {
return false
}
func analyzeFuncUsage(pkgs map[*types.Package]*loader.PackageInfo) funcUsage {
func analyzeFuncUsage(mainPkg *loader.PackageInfo, pkgs map[*types.Package]*loader.PackageInfo) funcUsage {
usage := funcUsage{}
for _, pkg := range pkgs {
isMain := pkg == mainPkg
for _, f := range pkg.Files {
ast.Inspect(f, func(node ast.Node) bool {
switch n := node.(type) {
@ -127,6 +128,11 @@ func analyzeFuncUsage(pkgs map[*types.Package]*loader.PackageInfo) funcUsage {
case *ast.SelectorExpr:
usage[t.Sel.Name] = true
}
case *ast.FuncDecl:
// exported functions are always assumed to be used
if isMain && n.Name.IsExported() {
usage[n.Name.Name] = true
}
}
return true
})

View file

@ -1426,7 +1426,7 @@ func (c *codegen) compile(info *buildInfo, pkg *loader.PackageInfo) error {
return c.prog.Err
}
funUsage := analyzeFuncUsage(info.program.AllPackages)
funUsage := analyzeFuncUsage(pkg, info.program.AllPackages)
// Bring all imported functions into scope.
for _, pkg := range info.program.AllPackages {