2018-02-25 12:26:56 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
2020-05-07 08:54:35 +00:00
|
|
|
"errors"
|
2018-02-25 12:26:56 +00:00
|
|
|
"go/ast"
|
2020-08-10 10:10:35 +00:00
|
|
|
"go/token"
|
2018-02-25 12:26:56 +00:00
|
|
|
"go/types"
|
2021-12-02 14:44:53 +00:00
|
|
|
"path/filepath"
|
2020-06-08 11:16:41 +00:00
|
|
|
"strings"
|
2018-02-25 12:26:56 +00:00
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
2021-12-02 14:44:53 +00:00
|
|
|
"golang.org/x/tools/go/packages"
|
2018-02-25 12:26:56 +00:00
|
|
|
)
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
var (
|
2020-06-08 11:16:41 +00:00
|
|
|
// Go language builtin functions.
|
2020-09-06 12:49:41 +00:00
|
|
|
goBuiltins = []string{"len", "append", "panic", "make", "copy", "recover", "delete"}
|
2020-06-08 11:16:41 +00:00
|
|
|
// Custom builtin utility functions.
|
|
|
|
customBuiltins = []string{
|
2021-10-23 13:12:42 +00:00
|
|
|
"FromAddress",
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
// newGlobal creates new global variable.
|
2020-07-28 16:35:41 +00:00
|
|
|
func (c *codegen) newGlobal(pkg string, name string) {
|
|
|
|
name = c.getIdentName(pkg, name)
|
2020-05-07 08:54:35 +00:00
|
|
|
c.globals[name] = len(c.globals)
|
|
|
|
}
|
|
|
|
|
2020-07-28 16:35:41 +00:00
|
|
|
// getIdentName returns fully-qualified name for a variable.
|
|
|
|
func (c *codegen) getIdentName(pkg string, name string) string {
|
|
|
|
if fullName, ok := c.importMap[pkg]; ok {
|
|
|
|
pkg = fullName
|
|
|
|
}
|
|
|
|
return pkg + "." + name
|
|
|
|
}
|
|
|
|
|
2020-05-07 08:54:35 +00:00
|
|
|
// traverseGlobals visits and initializes global variables.
|
2021-04-28 13:10:44 +00:00
|
|
|
// It returns `true` if contract has `_deploy` function.
|
|
|
|
func (c *codegen) traverseGlobals() bool {
|
2020-08-21 12:37:46 +00:00
|
|
|
var hasDefer bool
|
2020-12-08 12:24:01 +00:00
|
|
|
var n, nConst int
|
2021-04-28 13:10:44 +00:00
|
|
|
var hasDeploy bool
|
2021-02-25 12:12:16 +00:00
|
|
|
c.ForEachFile(func(f *ast.File, pkg *types.Package) {
|
2020-12-08 12:24:01 +00:00
|
|
|
nv, nc := countGlobals(f)
|
|
|
|
n += nv
|
|
|
|
nConst += nc
|
2021-04-28 13:10:44 +00:00
|
|
|
if !hasDeploy || !hasDefer {
|
2020-08-05 09:59:50 +00:00
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
2020-08-21 12:37:46 +00:00
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.FuncDecl:
|
2021-04-28 13:10:44 +00:00
|
|
|
hasDeploy = hasDeploy || isDeployFunc(n)
|
2020-08-21 12:37:46 +00:00
|
|
|
case *ast.DeferStmt:
|
|
|
|
hasDefer = true
|
2020-08-05 09:59:50 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
2020-07-28 07:59:21 +00:00
|
|
|
})
|
2020-08-21 12:37:46 +00:00
|
|
|
if hasDefer {
|
|
|
|
n++
|
|
|
|
}
|
2021-04-28 13:10:44 +00:00
|
|
|
|
|
|
|
if n > 255 {
|
|
|
|
c.prog.BinWriter.Err = errors.New("too many global variables")
|
|
|
|
return hasDeploy
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != 0 {
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.INITSSLOT, []byte{byte(n)})
|
|
|
|
}
|
|
|
|
|
|
|
|
initOffset := c.prog.Len()
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.INITSLOT, []byte{0, 0})
|
|
|
|
|
|
|
|
lastCnt, maxCnt := -1, -1
|
2021-12-02 14:44:53 +00:00
|
|
|
c.ForEachPackage(func(pkg *packages.Package) {
|
2021-04-28 13:10:44 +00:00
|
|
|
if n+nConst > 0 {
|
2021-12-02 14:44:53 +00:00
|
|
|
for _, f := range pkg.Syntax {
|
|
|
|
c.fillImportMap(f, pkg)
|
|
|
|
c.convertGlobals(f, pkg.Types)
|
2021-04-28 13:10:44 +00:00
|
|
|
}
|
2020-10-06 15:25:40 +00:00
|
|
|
}
|
2021-12-02 14:44:53 +00:00
|
|
|
for _, f := range pkg.Syntax {
|
|
|
|
c.fillImportMap(f, pkg)
|
2021-04-28 13:10:44 +00:00
|
|
|
|
|
|
|
var currMax int
|
2021-12-02 14:44:53 +00:00
|
|
|
lastCnt, currMax = c.convertInitFuncs(f, pkg.Types, lastCnt)
|
2021-04-28 13:10:44 +00:00
|
|
|
if currMax > maxCnt {
|
|
|
|
maxCnt = currMax
|
2020-08-05 08:14:43 +00:00
|
|
|
}
|
2021-04-28 13:10:44 +00:00
|
|
|
}
|
|
|
|
// because we reuse `convertFuncDecl` for init funcs,
|
|
|
|
// we need to cleare scope, so that global variables
|
|
|
|
// encountered after will be recognized as globals.
|
|
|
|
c.scope = nil
|
|
|
|
})
|
|
|
|
|
2021-04-28 13:37:31 +00:00
|
|
|
if c.globalInlineCount > maxCnt {
|
|
|
|
maxCnt = c.globalInlineCount
|
|
|
|
}
|
|
|
|
|
2021-04-28 13:10:44 +00:00
|
|
|
// Here we remove `INITSLOT` if no code was emitted for `init` function.
|
|
|
|
// Note that the `INITSSLOT` must stay in place.
|
|
|
|
hasNoInit := initOffset+3 == c.prog.Len()
|
|
|
|
if hasNoInit {
|
|
|
|
buf := c.prog.Bytes()
|
|
|
|
c.prog.Reset()
|
|
|
|
c.prog.WriteBytes(buf[:initOffset])
|
|
|
|
}
|
|
|
|
|
|
|
|
if initOffset != 0 || !hasNoInit { // if there are some globals or `init()`.
|
|
|
|
c.initEndOffset = c.prog.Len()
|
|
|
|
emit.Opcodes(c.prog.BinWriter, opcode.RET)
|
|
|
|
|
|
|
|
if maxCnt >= 0 {
|
|
|
|
c.reverseOffsetMap[initOffset] = nameWithLocals{
|
|
|
|
name: "init",
|
|
|
|
count: maxCnt,
|
2020-08-05 08:14:43 +00:00
|
|
|
}
|
2020-08-21 12:37:46 +00:00
|
|
|
}
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2021-04-28 13:10:44 +00:00
|
|
|
|
|
|
|
// store auxiliary variables after all others.
|
|
|
|
if hasDefer {
|
|
|
|
c.exceptionIndex = len(c.globals)
|
|
|
|
c.globals[exceptionVarName] = c.exceptionIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasDeploy
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// countGlobals counts the global variables in the program to add
|
2019-02-13 18:01:10 +00:00
|
|
|
// them with the stack size of the function.
|
2020-12-08 12:24:01 +00:00
|
|
|
// Second returned argument contains amount of global constants.
|
|
|
|
func countGlobals(f ast.Node) (int, int) {
|
|
|
|
var numVar, numConst int
|
2018-02-25 12:26:56 +00:00
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
2020-05-19 13:47:43 +00:00
|
|
|
switch n := node.(type) {
|
2020-08-21 12:37:46 +00:00
|
|
|
// Skip all function declarations if we have already encountered `defer`.
|
2018-02-25 12:26:56 +00:00
|
|
|
case *ast.FuncDecl:
|
|
|
|
return false
|
|
|
|
// After skipping all funcDecls we are sure that each value spec
|
|
|
|
// is a global declared variable or constant.
|
2020-09-06 12:20:15 +00:00
|
|
|
case *ast.GenDecl:
|
2020-12-08 12:24:01 +00:00
|
|
|
isVar := n.Tok == token.VAR
|
|
|
|
if isVar || n.Tok == token.CONST {
|
2020-09-06 12:20:15 +00:00
|
|
|
for _, s := range n.Specs {
|
2020-09-06 12:26:03 +00:00
|
|
|
for _, id := range s.(*ast.ValueSpec).Names {
|
|
|
|
if id.Name != "_" {
|
2020-12-08 12:24:01 +00:00
|
|
|
if isVar {
|
|
|
|
numVar++
|
|
|
|
} else {
|
|
|
|
numConst++
|
|
|
|
}
|
2020-09-06 12:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 12:20:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
2020-12-08 12:24:01 +00:00
|
|
|
return numVar, numConst
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 14:18:38 +00:00
|
|
|
// isExprNil looks if the given expression is a `nil`.
|
|
|
|
func isExprNil(e ast.Expr) bool {
|
|
|
|
v, ok := e.(*ast.Ident)
|
|
|
|
return ok && v.Name == "nil"
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// indexOfStruct returns the index of the given field inside that struct.
|
2018-02-27 09:04:24 +00:00
|
|
|
// If the struct does not contain that field it will return -1.
|
|
|
|
func indexOfStruct(strct *types.Struct, fldName string) int {
|
|
|
|
for i := 0; i < strct.NumFields(); i++ {
|
|
|
|
if strct.Field(i).Name() == fldName {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
type funcUsage map[string]bool
|
|
|
|
|
|
|
|
func (f funcUsage) funcUsed(name string) bool {
|
|
|
|
_, ok := f[name]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2020-05-06 12:39:25 +00:00
|
|
|
// lastStmtIsReturn checks if last statement of the declaration was return statement..
|
2021-02-05 11:31:45 +00:00
|
|
|
func lastStmtIsReturn(body *ast.BlockStmt) (b bool) {
|
|
|
|
if l := len(body.List); l != 0 {
|
|
|
|
switch inner := body.List[l-1].(type) {
|
|
|
|
case *ast.BlockStmt:
|
|
|
|
return lastStmtIsReturn(inner)
|
|
|
|
case *ast.ReturnStmt:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
2020-05-06 12:39:25 +00:00
|
|
|
}
|
|
|
|
return false
|
2018-04-10 09:45:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 07:56:36 +00:00
|
|
|
// analyzePkgOrder sets the order in which packages should be processed.
|
|
|
|
// From Go spec:
|
|
|
|
// A package with no imports is initialized by assigning initial values to all its package-level variables
|
|
|
|
// followed by calling all init functions in the order they appear in the source, possibly in multiple files,
|
|
|
|
// as presented to the compiler. If a package has imports, the imported packages are initialized before
|
|
|
|
// initializing the package itself. If multiple packages import a package, the imported package
|
|
|
|
// will be initialized only once. The importing of packages, by construction, guarantees
|
|
|
|
// that there can be no cyclic initialization dependencies.
|
|
|
|
func (c *codegen) analyzePkgOrder() {
|
|
|
|
seen := make(map[string]bool)
|
2021-12-02 14:44:53 +00:00
|
|
|
info := c.buildInfo.program[0]
|
|
|
|
c.visitPkg(info, seen)
|
2020-08-05 07:56:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-02 14:44:53 +00:00
|
|
|
func (c *codegen) visitPkg(pkg *packages.Package, seen map[string]bool) {
|
|
|
|
if seen[pkg.PkgPath] {
|
2020-08-05 07:56:36 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-02 14:44:53 +00:00
|
|
|
for _, imp := range pkg.Types.Imports() {
|
|
|
|
c.visitPkg(pkg.Imports[imp.Path()], seen)
|
2020-08-05 07:56:36 +00:00
|
|
|
}
|
2021-12-02 14:44:53 +00:00
|
|
|
seen[pkg.PkgPath] = true
|
|
|
|
c.packages = append(c.packages, pkg.PkgPath)
|
|
|
|
c.packageCache[pkg.PkgPath] = pkg
|
2020-08-05 07:56:36 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 10:10:35 +00:00
|
|
|
func (c *codegen) fillDocumentInfo() {
|
2021-12-02 14:44:53 +00:00
|
|
|
fset := c.buildInfo.config.Fset
|
2020-08-10 10:10:35 +00:00
|
|
|
fset.Iterate(func(f *token.File) bool {
|
|
|
|
filePath := f.Position(f.Pos(0)).Filename
|
compiler: use full paths for debug info files if relative can't be constructed
Firstly introduced in 9871dc8f5aa33c17651848df637ed1185f43aa18. It's OK
if relative path can't be constructed e.g. for interop dependencies. Otherwice
leads to failing tests on Windows with the following error:
```
2022-02-09T09:39:31.3307626Z panic: Rel: can't make D:\a\neo-go\neo-go\pkg\interop\neogointernal\syscall.go relative to C:\Users\RUNNER~1\AppData\Local\Temp\TestContractInitAndCompile1998984267\001\testcontract [recovered]
2022-02-09T09:39:31.3308830Z panic: Rel: can't make D:\a\neo-go\neo-go\pkg\interop\neogointernal\syscall.go relative to C:\Users\RUNNER~1\AppData\Local\Temp\TestContractInitAndCompile1998984267\001\testcontract
2022-02-09T09:39:31.3309285Z
2022-02-09T09:39:31.3309390Z goroutine 302 [running]:
2022-02-09T09:39:31.3309725Z testing.tRunner.func1.2({0x14dbfe0, 0xc00051bdb0})
2022-02-09T09:39:31.3310187Z C:/hostedtoolcache/windows/go/1.17.6/x64/src/testing/testing.go:1209 +0x36c
2022-02-09T09:39:31.3310538Z testing.tRunner.func1()
2022-02-09T09:39:31.3310937Z C:/hostedtoolcache/windows/go/1.17.6/x64/src/testing/testing.go:1212 +0x3b6
2022-02-09T09:39:31.3311285Z panic({0x14dbfe0, 0xc00051bdb0})
2022-02-09T09:39:31.3311689Z C:/hostedtoolcache/windows/go/1.17.6/x64/src/runtime/panic.go:1047 +0x266
2022-02-09T09:39:31.3312319Z github.com/nspcc-dev/neo-go/pkg/compiler.(*codegen).fillDocumentInfo.func1(0xc0002d0b40)
2022-02-09T09:39:31.3312885Z D:/a/neo-go/neo-go/pkg/compiler/analysis.go:240 +0x34d
2022-02-09T09:39:31.3313397Z go/token.(*FileSet).Iterate(0xc0000a69c0, 0xc0004898b0)
2022-02-09T09:39:31.3313839Z C:/hostedtoolcache/windows/go/1.17.6/x64/src/go/token/position.go:463 +0xde
2022-02-09T09:39:31.3314404Z github.com/nspcc-dev/neo-go/pkg/compiler.(*codegen).fillDocumentInfo(0xc0002e4d20)
2022-02-09T09:39:31.3314947Z D:/a/neo-go/neo-go/pkg/compiler/analysis.go:236 +0x97
2022-02-09T09:39:31.3315530Z github.com/nspcc-dev/neo-go/pkg/compiler.(*codegen).compile(0xc0002e4d20, 0xc00009a690, 0xc000101540)
2022-02-09T09:39:31.3316064Z D:/a/neo-go/neo-go/pkg/compiler/codegen.go:2088 +0x192
2022-02-09T09:39:31.3316563Z github.com/nspcc-dev/neo-go/pkg/compiler.codeGen(0xc00009a690)
2022-02-09T09:39:31.3317066Z D:/a/neo-go/neo-go/pkg/compiler/codegen.go:2167 +0x7cd
2022-02-09T09:39:31.3317679Z github.com/nspcc-dev/neo-go/pkg/compiler.CompileWithOptions({0xc0000435e0, 0x62}, {0x0, 0x0}, 0xc0004bcf70)
2022-02-09T09:39:31.3318233Z D:/a/neo-go/neo-go/pkg/compiler/compiler.go:225 +0xde
2022-02-09T09:39:31.3318804Z github.com/nspcc-dev/neo-go/pkg/compiler.CompileAndSave({0xc0000435e0, 0x62}, 0xc0004bcf70)
2022-02-09T09:39:31.3319353Z D:/a/neo-go/neo-go/pkg/compiler/compiler.go:241 +0x3a5
2022-02-09T09:39:31.3319889Z github.com/nspcc-dev/neo-go/cli/smartcontract.contractCompile(0xc0004e42c0)
2022-02-09T09:39:31.3320456Z D:/a/neo-go/neo-go/cli/smartcontract/smart_contract.go:520 +0xc2d
2022-02-09T09:39:31.3320873Z github.com/urfave/cli.HandleAction({0x14b6fa0, 0x17b8d60}, 0x7)
2022-02-09T09:39:31.3321344Z C:/Users/runneradmin/go/pkg/mod/github.com/urfave/cli@v1.22.5/app.go:524 +0xf4
2022-02-09T09:39:31.3321931Z github.com/urfave/cli.Command.Run({{0x16460d2, 0x7}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0x166fe9e, 0x27}, {0x0, ...}, ...}, ...)
2022-02-09T09:39:31.3322443Z C:/Users/runneradmin/go/pkg/mod/github.com/urfave/cli@v1.22.5/command.go:173 +0xc09
2022-02-09T09:39:31.3322894Z github.com/urfave/cli.(*App).RunAsSubcommand(0xc0000b7340, 0xc0004e4000)
2022-02-09T09:39:31.3323375Z C:/Users/runneradmin/go/pkg/mod/github.com/urfave/cli@v1.22.5/app.go:405 +0x106c
2022-02-09T09:39:31.3323852Z github.com/urfave/cli.Command.startApp({{0x1647536, 0x8}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0x1671070, 0x28}, {0x0, ...}, ...}, ...)
2022-02-09T09:39:31.3324365Z C:/Users/runneradmin/go/pkg/mod/github.com/urfave/cli@v1.22.5/command.go:372 +0x108c
2022-02-09T09:39:31.3324838Z github.com/urfave/cli.Command.Run({{0x1647536, 0x8}, {0x0, 0x0}, {0x0, 0x0, 0x0}, {0x1671070, 0x28}, {0x0, ...}, ...}, ...)
2022-02-09T09:39:31.3325327Z C:/Users/runneradmin/go/pkg/mod/github.com/urfave/cli@v1.22.5/command.go:102 +0xe8c
2022-02-09T09:39:31.3325744Z github.com/urfave/cli.(*App).Run(0xc000323880, {0xc0004237a0, 0xb, 0x12})
2022-02-09T09:39:31.3326198Z C:/Users/runneradmin/go/pkg/mod/github.com/urfave/cli@v1.22.5/app.go:277 +0xa8c
2022-02-09T09:39:31.3326770Z github.com/nspcc-dev/neo-go/cli.(*executor).run(0xc0000709c0, {0xc0004237a0, 0xb, 0x12})
2022-02-09T09:39:31.3327275Z D:/a/neo-go/neo-go/cli/executor_test.go:273 +0x3a5
2022-02-09T09:39:31.3327812Z github.com/nspcc-dev/neo-go/cli.(*executor).Run(0x14aa020, 0xc000493950, {0xc0004237a0, 0xb, 0x12})
2022-02-09T09:39:31.3328324Z D:/a/neo-go/neo-go/cli/executor_test.go:263 +0x10f
2022-02-09T09:39:31.3328844Z github.com/nspcc-dev/neo-go/cli.TestContractInitAndCompile(0xc0001d69c0)
2022-02-09T09:39:31.3329377Z D:/a/neo-go/neo-go/cli/contract_test.go:155 +0x147f
2022-02-09T09:39:31.3329694Z testing.tRunner(0xc0001d69c0, 0x17b8be8)
2022-02-09T09:39:31.3330123Z C:/hostedtoolcache/windows/go/1.17.6/x64/src/testing/testing.go:1259 +0x230
2022-02-09T09:39:31.3330470Z created by testing.(*T).Run
2022-02-09T09:39:31.3330862Z C:/hostedtoolcache/windows/go/1.17.6/x64/src/testing/testing.go:1306 +0x727
2022-02-09T09:39:31.3331325Z FAIL github.com/nspcc-dev/neo-go/cli 5.552s
```
2022-02-09 15:17:47 +00:00
|
|
|
rel, err := filepath.Rel(c.buildInfo.config.Dir, filePath)
|
|
|
|
// It's OK if we can't construct relative path, e.g. for interop dependencies.
|
|
|
|
if err == nil {
|
|
|
|
filePath = rel
|
2021-12-02 14:44:53 +00:00
|
|
|
}
|
2020-08-10 10:10:35 +00:00
|
|
|
c.docIndex[filePath] = len(c.documents)
|
|
|
|
c.documents = append(c.documents, filePath)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-30 08:48:18 +00:00
|
|
|
// analyzeFuncUsage traverses all code and returns map with functions
|
|
|
|
// which should be present in the emitted code.
|
|
|
|
// This is done using BFS starting from exported functions or
|
|
|
|
// function used in variable declarations (graph edge corresponds to
|
|
|
|
// function being called in declaration).
|
2020-07-28 15:40:41 +00:00
|
|
|
func (c *codegen) analyzeFuncUsage() funcUsage {
|
2021-09-30 08:48:18 +00:00
|
|
|
type declPair struct {
|
|
|
|
decl *ast.FuncDecl
|
|
|
|
importMap map[string]string
|
|
|
|
path string
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
|
2021-09-30 08:48:18 +00:00
|
|
|
// nodeCache contains top-level function declarations .
|
|
|
|
nodeCache := make(map[string]declPair)
|
|
|
|
diff := funcUsage{}
|
2020-07-28 15:40:41 +00:00
|
|
|
c.ForEachFile(func(f *ast.File, pkg *types.Package) {
|
2021-09-30 08:48:18 +00:00
|
|
|
var pkgPath string
|
2021-12-02 14:44:53 +00:00
|
|
|
isMain := pkg == c.mainPkg.Types
|
2021-09-30 08:48:18 +00:00
|
|
|
if !isMain {
|
|
|
|
pkgPath = pkg.Path()
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:40:41 +00:00
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.CallExpr:
|
2021-09-30 08:48:18 +00:00
|
|
|
// functions invoked in variable declarations in imported packages
|
|
|
|
// are marked as used.
|
|
|
|
var name string
|
2020-07-28 15:40:41 +00:00
|
|
|
switch t := n.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
2021-09-30 08:48:18 +00:00
|
|
|
name = c.getIdentName(pkgPath, t.Name)
|
2020-07-28 15:40:41 +00:00
|
|
|
case *ast.SelectorExpr:
|
2021-09-30 08:48:18 +00:00
|
|
|
name, _ = c.getFuncNameFromSelector(t)
|
|
|
|
default:
|
|
|
|
return true
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
2021-09-30 08:48:18 +00:00
|
|
|
diff[name] = true
|
2020-07-28 15:40:41 +00:00
|
|
|
case *ast.FuncDecl:
|
2021-09-30 08:48:18 +00:00
|
|
|
name := c.getFuncNameFromDecl(pkgPath, n)
|
|
|
|
|
2020-07-28 15:40:41 +00:00
|
|
|
// exported functions are always assumed to be used
|
2021-09-30 08:48:18 +00:00
|
|
|
if isMain && n.Name.IsExported() || isInitFunc(n) || isDeployFunc(n) {
|
|
|
|
diff[name] = true
|
2020-07-28 15:40:41 +00:00
|
|
|
}
|
2021-09-30 08:48:18 +00:00
|
|
|
nodeCache[name] = declPair{n, c.importMap, pkgPath}
|
|
|
|
return false // will be processed in the next stage
|
2020-07-28 15:40:41 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
2021-09-30 08:48:18 +00:00
|
|
|
|
|
|
|
usage := funcUsage{}
|
|
|
|
for len(diff) != 0 {
|
|
|
|
nextDiff := funcUsage{}
|
|
|
|
for name := range diff {
|
|
|
|
fd, ok := nodeCache[name]
|
|
|
|
if !ok || usage[name] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
usage[name] = true
|
|
|
|
|
2021-10-25 07:52:15 +00:00
|
|
|
pkg := c.mainPkg
|
|
|
|
if fd.path != "" {
|
2021-12-02 14:44:53 +00:00
|
|
|
pkg = c.packageCache[fd.path]
|
2021-10-25 07:52:15 +00:00
|
|
|
}
|
2021-12-02 14:44:53 +00:00
|
|
|
c.typeInfo = pkg.TypesInfo
|
|
|
|
c.currPkg = pkg
|
2021-09-30 08:48:18 +00:00
|
|
|
c.importMap = fd.importMap
|
|
|
|
ast.Inspect(fd.decl, func(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
switch t := n.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
nextDiff[c.getIdentName(fd.path, t.Name)] = true
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
name, _ := c.getFuncNameFromSelector(t)
|
|
|
|
nextDiff[name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
diff = nextDiff
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
return usage
|
|
|
|
}
|
|
|
|
|
2020-06-08 11:16:41 +00:00
|
|
|
func isGoBuiltin(name string) bool {
|
|
|
|
for i := range goBuiltins {
|
|
|
|
if name == goBuiltins[i] {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-04-22 18:11:37 +00:00
|
|
|
|
2020-06-08 11:16:41 +00:00
|
|
|
func isCustomBuiltin(f *funcScope) bool {
|
|
|
|
if !isInteropPath(f.pkg.Path()) {
|
2018-03-25 16:21:00 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-06-08 11:16:41 +00:00
|
|
|
for _, n := range customBuiltins {
|
|
|
|
if f.name == n {
|
2018-04-22 18:11:37 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-22 07:51:35 +00:00
|
|
|
func isSyscall(fun *funcScope) bool {
|
2020-06-08 11:16:41 +00:00
|
|
|
if fun.selector == nil || fun.pkg == nil || !isInteropPath(fun.pkg.Path()) {
|
2018-08-22 07:51:35 +00:00
|
|
|
return false
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
2021-03-02 11:15:25 +00:00
|
|
|
return fun.pkg.Name() == "neogointernal" && (strings.HasPrefix(fun.name, "Syscall") ||
|
2021-12-08 19:33:03 +00:00
|
|
|
strings.HasPrefix(fun.name, "Opcode") || strings.HasPrefix(fun.name, "CallWithToken"))
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
2020-06-08 11:16:41 +00:00
|
|
|
|
2021-02-08 08:06:01 +00:00
|
|
|
const interopPrefix = "github.com/nspcc-dev/neo-go/pkg/interop"
|
|
|
|
|
2020-06-08 11:16:41 +00:00
|
|
|
func isInteropPath(s string) bool {
|
2021-02-08 08:06:01 +00:00
|
|
|
return strings.HasPrefix(s, interopPrefix)
|
|
|
|
}
|
|
|
|
|
2021-02-05 10:06:42 +00:00
|
|
|
// canConvert returns true if type doesn't need to be converted on type assertion.
|
|
|
|
func canConvert(s string) bool {
|
2021-02-05 10:15:39 +00:00
|
|
|
if len(s) != 0 && s[0] == '*' {
|
|
|
|
s = s[1:]
|
|
|
|
}
|
2021-02-05 10:06:42 +00:00
|
|
|
if isInteropPath(s) {
|
|
|
|
s = s[len(interopPrefix):]
|
2021-02-05 10:15:39 +00:00
|
|
|
return s != "/iterator.Iterator" && s != "/storage.Context" &&
|
|
|
|
s != "/native/ledger.Block" && s != "/native/ledger.Transaction" &&
|
2021-05-25 14:54:57 +00:00
|
|
|
s != "/native/management.Contract" && s != "/native/neo.AccountState"
|
2021-02-05 10:06:42 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2021-02-04 12:41:00 +00:00
|
|
|
|
|
|
|
// canInline returns true if function is to be inlined.
|
|
|
|
// Currently there is a static list of function which are inlined,
|
|
|
|
// this may change in future.
|
2021-10-23 13:12:42 +00:00
|
|
|
func canInline(s string, name string) bool {
|
2021-02-05 16:02:09 +00:00
|
|
|
if strings.HasPrefix(s, "github.com/nspcc-dev/neo-go/pkg/compiler/testdata/inline") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !isInteropPath(s) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !strings.HasPrefix(s[len(interopPrefix):], "/neogointernal") &&
|
2021-10-23 13:12:42 +00:00
|
|
|
!(strings.HasPrefix(s[len(interopPrefix):], "/util") && name == "FromAddress")
|
2021-02-04 12:41:00 +00:00
|
|
|
}
|