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"
|
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"
|
2020-08-05 08:14:43 +00:00
|
|
|
"golang.org/x/tools/go/loader"
|
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{
|
2020-09-15 07:05:41 +00:00
|
|
|
"FromAddress", "Equals", "Remove",
|
2020-04-28 12:46:03 +00:00
|
|
|
"ToBool", "ToByteArray", "ToInteger",
|
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.
|
2020-10-06 15:25:40 +00:00
|
|
|
// and returns number of variables initialized.
|
|
|
|
// Second return value is -1 if no `init()` functions were encountered
|
|
|
|
// and number of maximum amount of locals in any of init functions otherwise.
|
2020-10-02 10:16:02 +00:00
|
|
|
// Same for `_deploy()` functions (see docs/compiler.md).
|
|
|
|
func (c *codegen) traverseGlobals() (int, int, int) {
|
2020-08-21 12:37:46 +00:00
|
|
|
var hasDefer bool
|
2020-12-08 12:24:01 +00:00
|
|
|
var n, nConst int
|
2020-10-06 15:25:40 +00:00
|
|
|
initLocals := -1
|
2020-10-02 10:16:02 +00:00
|
|
|
deployLocals := -1
|
2020-07-28 15:40:41 +00:00
|
|
|
c.ForEachFile(func(f *ast.File, _ *types.Package) {
|
2020-12-08 12:24:01 +00:00
|
|
|
nv, nc := countGlobals(f)
|
|
|
|
n += nv
|
|
|
|
nConst += nc
|
2020-10-02 10:16:02 +00:00
|
|
|
if initLocals == -1 || deployLocals == -1 || !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:
|
2020-08-05 09:59:50 +00:00
|
|
|
if isInitFunc(n) {
|
2020-10-06 15:25:40 +00:00
|
|
|
c, _ := countLocals(n)
|
|
|
|
if c > initLocals {
|
|
|
|
initLocals = c
|
|
|
|
}
|
2020-10-02 10:16:02 +00:00
|
|
|
} else if isDeployFunc(n) {
|
|
|
|
c, _ := countLocals(n)
|
|
|
|
if c > deployLocals {
|
|
|
|
deployLocals = c
|
|
|
|
}
|
2020-08-05 09:59:50 +00:00
|
|
|
}
|
2020-08-21 12:37:46 +00:00
|
|
|
return !hasDefer
|
|
|
|
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++
|
|
|
|
}
|
2020-12-08 12:24:01 +00:00
|
|
|
if n+nConst != 0 || initLocals > -1 {
|
2020-05-07 08:54:35 +00:00
|
|
|
if n > 255 {
|
|
|
|
c.prog.BinWriter.Err = errors.New("too many global variables")
|
2020-10-02 10:16:02 +00:00
|
|
|
return 0, initLocals, deployLocals
|
2020-08-05 09:59:50 +00:00
|
|
|
}
|
|
|
|
if n != 0 {
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.INITSSLOT, []byte{byte(n)})
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2020-10-06 15:25:40 +00:00
|
|
|
if initLocals > 0 {
|
|
|
|
emit.Instruction(c.prog.BinWriter, opcode.INITSLOT, []byte{byte(initLocals), 0})
|
|
|
|
}
|
|
|
|
seenBefore := false
|
2020-08-05 08:14:43 +00:00
|
|
|
c.ForEachPackage(func(pkg *loader.PackageInfo) {
|
2020-12-08 12:24:01 +00:00
|
|
|
if n+nConst > 0 {
|
2020-08-05 09:59:50 +00:00
|
|
|
for _, f := range pkg.Files {
|
|
|
|
c.fillImportMap(f, pkg.Pkg)
|
|
|
|
c.convertGlobals(f, pkg.Pkg)
|
|
|
|
}
|
2020-08-05 08:14:43 +00:00
|
|
|
}
|
2020-10-06 15:25:40 +00:00
|
|
|
if initLocals > -1 {
|
2020-08-05 09:59:50 +00:00
|
|
|
for _, f := range pkg.Files {
|
|
|
|
c.fillImportMap(f, pkg.Pkg)
|
2020-10-06 15:25:40 +00:00
|
|
|
seenBefore = c.convertInitFuncs(f, pkg.Pkg, seenBefore) || seenBefore
|
2020-08-05 09:59:50 +00:00
|
|
|
}
|
2020-08-05 08:14:43 +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
|
|
|
|
})
|
2020-08-21 12:37:46 +00:00
|
|
|
// store auxiliary variables after all others.
|
|
|
|
if hasDefer {
|
|
|
|
c.exceptionIndex = len(c.globals)
|
|
|
|
c.globals["<exception>"] = c.exceptionIndex
|
|
|
|
}
|
2020-05-07 08:54:35 +00:00
|
|
|
}
|
2020-10-02 10:16:02 +00:00
|
|
|
return n, initLocals, deployLocals
|
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..
|
|
|
|
func lastStmtIsReturn(decl *ast.FuncDecl) (b bool) {
|
|
|
|
if l := len(decl.Body.List); l != 0 {
|
|
|
|
_, ok := decl.Body.List[l-1].(*ast.ReturnStmt)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
info := c.buildInfo.program.Package(c.buildInfo.initialPackage)
|
|
|
|
c.visitPkg(info.Pkg, seen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *codegen) visitPkg(pkg *types.Package, seen map[string]bool) {
|
|
|
|
pkgPath := pkg.Path()
|
|
|
|
if seen[pkgPath] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, imp := range pkg.Imports() {
|
|
|
|
c.visitPkg(imp, seen)
|
|
|
|
}
|
|
|
|
seen[pkgPath] = true
|
|
|
|
c.packages = append(c.packages, pkgPath)
|
|
|
|
}
|
|
|
|
|
2020-08-10 10:10:35 +00:00
|
|
|
func (c *codegen) fillDocumentInfo() {
|
|
|
|
fset := c.buildInfo.program.Fset
|
|
|
|
fset.Iterate(func(f *token.File) bool {
|
|
|
|
filePath := f.Position(f.Pos(0)).Filename
|
|
|
|
c.docIndex[filePath] = len(c.documents)
|
|
|
|
c.documents = append(c.documents, filePath)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:40:41 +00:00
|
|
|
func (c *codegen) analyzeFuncUsage() funcUsage {
|
2018-02-27 09:04:24 +00:00
|
|
|
usage := funcUsage{}
|
|
|
|
|
2020-07-28 15:40:41 +00:00
|
|
|
c.ForEachFile(func(f *ast.File, pkg *types.Package) {
|
|
|
|
isMain := pkg == c.mainPkg.Pkg
|
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
switch t := n.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
2020-07-29 14:20:00 +00:00
|
|
|
usage[c.getIdentName("", t.Name)] = true
|
2020-07-28 15:40:41 +00:00
|
|
|
case *ast.SelectorExpr:
|
2020-07-29 14:20:00 +00:00
|
|
|
name, _ := c.getFuncNameFromSelector(t)
|
|
|
|
usage[name] = true
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
2020-07-28 15:40:41 +00:00
|
|
|
case *ast.FuncDecl:
|
|
|
|
// exported functions are always assumed to be used
|
|
|
|
if isMain && n.Name.IsExported() {
|
2020-07-29 14:20:00 +00:00
|
|
|
usage[c.getFuncNameFromDecl(pkg.Path(), n)] = true
|
2020-07-28 15:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
})
|
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
|
|
|
}
|
2020-08-19 07:10:40 +00:00
|
|
|
_, ok := syscalls[fun.pkg.Name()][fun.name]
|
2018-08-22 07:51:35 +00:00
|
|
|
return ok
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isNativeHelpersPath(s string) bool {
|
|
|
|
return strings.HasPrefix(s, interopPrefix+"/native")
|
2020-06-08 11:16:41 +00:00
|
|
|
}
|
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" &&
|
|
|
|
s != "/native/management.Contract"
|
2021-02-05 10:06:42 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|