2018-02-25 12:26:56 +00:00
|
|
|
package compiler
|
|
|
|
|
|
|
|
import (
|
2019-11-22 14:16:52 +00:00
|
|
|
"fmt"
|
2018-02-25 12:26:56 +00:00
|
|
|
"go/ast"
|
|
|
|
"go/constant"
|
|
|
|
"go/types"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/loader"
|
|
|
|
)
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
var (
|
2018-04-22 18:11:37 +00:00
|
|
|
// Go language builtin functions and custom builtin utility functions.
|
|
|
|
builtinFuncs = []string{
|
|
|
|
"len", "append", "SHA256",
|
2020-03-23 09:44:23 +00:00
|
|
|
"AppCall",
|
2018-08-23 17:44:17 +00:00
|
|
|
"FromAddress", "Equals",
|
2020-01-28 14:18:38 +00:00
|
|
|
"panic",
|
2020-04-28 12:46:03 +00:00
|
|
|
"ToBool", "ToByteArray", "ToInteger",
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// typeAndValueForField returns a zero initialized typeAndValue for the given type.Var.
|
2019-11-22 14:16:52 +00:00
|
|
|
func typeAndValueForField(fld *types.Var) (types.TypeAndValue, error) {
|
2018-02-27 09:04:24 +00:00
|
|
|
switch t := fld.Type().(type) {
|
|
|
|
case *types.Basic:
|
|
|
|
switch t.Kind() {
|
|
|
|
case types.Int:
|
|
|
|
return types.TypeAndValue{
|
|
|
|
Type: t,
|
|
|
|
Value: constant.MakeInt64(0),
|
2019-11-22 14:16:52 +00:00
|
|
|
}, nil
|
2018-02-27 09:04:24 +00:00
|
|
|
case types.String:
|
|
|
|
return types.TypeAndValue{
|
|
|
|
Type: t,
|
|
|
|
Value: constant.MakeString(""),
|
2019-11-22 14:16:52 +00:00
|
|
|
}, nil
|
2018-02-27 09:04:24 +00:00
|
|
|
case types.Bool, types.UntypedBool:
|
|
|
|
return types.TypeAndValue{
|
|
|
|
Type: t,
|
|
|
|
Value: constant.MakeBool(false),
|
2019-11-22 14:16:52 +00:00
|
|
|
}, nil
|
2018-02-27 09:04:24 +00:00
|
|
|
default:
|
2019-11-22 14:16:52 +00:00
|
|
|
return types.TypeAndValue{}, fmt.Errorf("could not initialize struct field %s to zero, type: %s", fld.Name(), t)
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-22 14:16:52 +00:00
|
|
|
return types.TypeAndValue{}, nil
|
2018-02-27 09:04:24 +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.
|
2019-02-09 15:53:58 +00:00
|
|
|
func countGlobals(f ast.Node) (i int64) {
|
2018-02-25 12:26:56 +00:00
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
|
|
|
switch node.(type) {
|
2019-02-13 18:01:10 +00:00
|
|
|
// Skip all function declarations.
|
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.
|
|
|
|
case *ast.ValueSpec:
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// isIdentBool looks if the given ident is a boolean.
|
|
|
|
func isIdentBool(ident *ast.Ident) bool {
|
|
|
|
return ident.Name == "true" || ident.Name == "false"
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2018-02-25 12:26:56 +00:00
|
|
|
// makeBoolFromIdent creates a bool type from an *ast.Ident.
|
2019-11-22 14:16:52 +00:00
|
|
|
func makeBoolFromIdent(ident *ast.Ident, tinfo *types.Info) (types.TypeAndValue, error) {
|
2018-02-25 12:26:56 +00:00
|
|
|
var b bool
|
2019-02-09 15:53:58 +00:00
|
|
|
switch ident.Name {
|
|
|
|
case "true":
|
2018-02-25 12:26:56 +00:00
|
|
|
b = true
|
2019-02-09 15:53:58 +00:00
|
|
|
case "false":
|
2018-02-25 12:26:56 +00:00
|
|
|
b = false
|
2019-02-09 15:53:58 +00:00
|
|
|
default:
|
2019-11-22 14:16:52 +00:00
|
|
|
return types.TypeAndValue{}, fmt.Errorf("givent identifier cannot be converted to a boolean => %s", ident.Name)
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
|
|
|
return types.TypeAndValue{
|
|
|
|
Type: tinfo.ObjectOf(ident).Type(),
|
|
|
|
Value: constant.MakeBool(b),
|
2019-11-22 14:16:52 +00:00
|
|
|
}, nil
|
2018-02-25 12:26:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// resolveEntryPoint returns the function declaration of the entrypoint and the corresponding file.
|
|
|
|
func resolveEntryPoint(entry string, pkg *loader.PackageInfo) (*ast.FuncDecl, *ast.File) {
|
|
|
|
var (
|
|
|
|
main *ast.FuncDecl
|
|
|
|
file *ast.File
|
|
|
|
)
|
|
|
|
for _, f := range pkg.Files {
|
|
|
|
ast.Inspect(f, func(n ast.Node) bool {
|
|
|
|
switch t := n.(type) {
|
|
|
|
case *ast.FuncDecl:
|
|
|
|
if t.Name.Name == entry {
|
|
|
|
main = t
|
|
|
|
file = f
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return main, file
|
|
|
|
}
|
2018-02-27 09:04:24 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-27 09:04:24 +00:00
|
|
|
func analyzeFuncUsage(pkgs map[*types.Package]*loader.PackageInfo) funcUsage {
|
|
|
|
usage := funcUsage{}
|
|
|
|
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
for _, f := range pkg.Files {
|
|
|
|
ast.Inspect(f, func(node ast.Node) bool {
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *ast.CallExpr:
|
|
|
|
switch t := n.Fun.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
usage[t.Name] = true
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
usage[t.Sel.Name] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return usage
|
|
|
|
}
|
|
|
|
|
2018-03-25 16:21:00 +00:00
|
|
|
func isBuiltin(expr ast.Expr) bool {
|
2018-04-22 18:11:37 +00:00
|
|
|
var name string
|
|
|
|
|
|
|
|
switch t := expr.(type) {
|
|
|
|
case *ast.Ident:
|
|
|
|
name = t.Name
|
|
|
|
case *ast.SelectorExpr:
|
|
|
|
name = t.Sel.Name
|
|
|
|
default:
|
2018-03-25 16:21:00 +00:00
|
|
|
return false
|
|
|
|
}
|
2018-04-22 18:11:37 +00:00
|
|
|
|
|
|
|
for _, n := range builtinFuncs {
|
|
|
|
if name == n {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:29:49 +00:00
|
|
|
func (c *codegen) isCompoundArrayType(t ast.Expr) bool {
|
|
|
|
switch s := t.(type) {
|
|
|
|
case *ast.ArrayType:
|
|
|
|
return true
|
|
|
|
case *ast.Ident:
|
|
|
|
arr, ok := c.typeInfo.Types[s].Type.Underlying().(*types.Slice)
|
|
|
|
return ok && !isByte(arr.Elem())
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func isByte(t types.Type) bool {
|
|
|
|
e, ok := t.(*types.Basic)
|
|
|
|
return ok && e.Kind() == types.Byte
|
|
|
|
}
|
|
|
|
|
2020-03-26 12:35:15 +00:00
|
|
|
func (c *codegen) isStructType(t ast.Expr) (int, bool) {
|
|
|
|
switch s := t.(type) {
|
|
|
|
case *ast.StructType:
|
|
|
|
return s.Fields.NumFields(), true
|
|
|
|
case *ast.Ident:
|
|
|
|
st, ok := c.typeInfo.Types[s].Type.Underlying().(*types.Struct)
|
|
|
|
if ok {
|
|
|
|
return st.NumFields(), true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
|
2018-02-27 09:04:24 +00:00
|
|
|
func isByteArray(lit *ast.CompositeLit, tInfo *types.Info) bool {
|
|
|
|
if len(lit.Elts) == 0 {
|
2020-02-11 10:15:08 +00:00
|
|
|
if typ, ok := lit.Type.(*ast.ArrayType); ok {
|
|
|
|
if name, ok := typ.Elt.(*ast.Ident); ok {
|
|
|
|
return name.Name == "byte" || name.Name == "uint8"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-27 09:04:24 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
typ := tInfo.Types[lit.Elts[0]].Type.Underlying()
|
2020-03-26 12:29:49 +00:00
|
|
|
return isByte(typ)
|
2018-02-27 09:04:24 +00:00
|
|
|
}
|
2018-03-25 16:21:00 +00:00
|
|
|
|
2018-08-22 07:51:35 +00:00
|
|
|
func isSyscall(fun *funcScope) bool {
|
|
|
|
if fun.selector == nil {
|
|
|
|
return false
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
2018-08-22 07:51:35 +00:00
|
|
|
_, ok := syscalls[fun.selector.Name][fun.name]
|
|
|
|
return ok
|
2018-03-25 16:21:00 +00:00
|
|
|
}
|
2018-04-04 19:41:19 +00:00
|
|
|
|
2019-09-12 14:39:23 +00:00
|
|
|
func isByteArrayType(t types.Type) bool {
|
|
|
|
return t.String() == "[]byte"
|
|
|
|
}
|
|
|
|
|
2018-04-04 19:41:19 +00:00
|
|
|
func isStringType(t types.Type) bool {
|
|
|
|
return t.String() == "string"
|
|
|
|
}
|