2019-04-09 11:52:21 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-05-09 12:25:45 -04:00
|
|
|
"io/ioutil"
|
2019-05-11 23:03:56 -04:00
|
|
|
"log"
|
2019-04-09 11:52:21 -04:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-05-01 10:58:29 -04:00
|
|
|
"regexp"
|
2019-04-09 11:52:21 -04:00
|
|
|
"runtime"
|
2019-05-11 23:03:56 -04:00
|
|
|
"runtime/pprof"
|
2019-04-09 11:52:21 -04:00
|
|
|
"strings"
|
|
|
|
|
2019-05-09 12:25:45 -04:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-05-29 12:57:53 -04:00
|
|
|
"git.wow.st/gmp/nswrap/ast"
|
|
|
|
"git.wow.st/gmp/nswrap/wrap"
|
2019-04-09 11:52:21 -04:00
|
|
|
)
|
|
|
|
|
2019-04-26 15:34:36 -04:00
|
|
|
var Debug = false
|
2019-05-11 23:03:56 -04:00
|
|
|
var Profile = false
|
2019-04-09 11:52:21 -04:00
|
|
|
|
2019-04-26 15:34:36 -04:00
|
|
|
type conf struct {
|
2019-05-11 23:03:56 -04:00
|
|
|
Positions bool
|
2019-04-26 22:44:30 -04:00
|
|
|
Package string
|
2019-05-09 12:25:45 -04:00
|
|
|
Inputfiles []string
|
2019-06-01 15:10:51 -04:00
|
|
|
Astfile string
|
2019-06-03 11:07:12 -04:00
|
|
|
Debugast bool
|
2019-04-26 15:34:36 -04:00
|
|
|
Classes []string
|
2019-05-01 10:58:29 -04:00
|
|
|
Functions []string
|
2019-05-02 14:14:29 -04:00
|
|
|
Enums []string
|
2019-05-21 16:26:52 -04:00
|
|
|
Delegates map[string]map[string][]string
|
|
|
|
Subclasses map[string]map[string][]string
|
2019-05-02 14:14:29 -04:00
|
|
|
Frameworks []string
|
2019-04-26 15:34:36 -04:00
|
|
|
Imports []string
|
2019-05-09 12:25:45 -04:00
|
|
|
Sysimports []string
|
2019-04-26 15:41:29 -04:00
|
|
|
Pragma []string
|
2019-05-09 12:25:45 -04:00
|
|
|
Vaargs int
|
2019-05-31 12:42:52 -04:00
|
|
|
//Arc flag for debugging only, builds will break
|
|
|
|
Arc bool
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:34:36 -04:00
|
|
|
var Config conf
|
2019-04-09 11:52:21 -04:00
|
|
|
|
|
|
|
func readAST(data []byte) []string {
|
|
|
|
return strings.Split(string(data), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
type treeNode struct {
|
|
|
|
indent int
|
|
|
|
node ast.Node
|
|
|
|
}
|
|
|
|
|
2019-06-03 11:07:12 -04:00
|
|
|
func printLinesWithContext(lines []string,i int) {
|
|
|
|
b := i - 3
|
|
|
|
if b < 0 { b = 0 }
|
|
|
|
var flag string
|
|
|
|
for x := b; (x < b + 6) && (x < len(lines)); x++ {
|
|
|
|
if x == i {
|
|
|
|
flag = "--> "
|
|
|
|
} else {
|
|
|
|
flag = " "
|
|
|
|
}
|
|
|
|
fmt.Printf("%s%s\n",flag,lines[x])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:52:21 -04:00
|
|
|
func convertLinesToNodes(lines []string) []treeNode {
|
|
|
|
nodes := make([]treeNode, len(lines))
|
|
|
|
var counter int
|
2019-06-03 11:07:12 -04:00
|
|
|
unknowns := 0
|
|
|
|
for i, line := range lines {
|
2019-04-09 11:52:21 -04:00
|
|
|
if strings.TrimSpace(line) == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// It is tempting to discard null AST nodes, but these may
|
|
|
|
// have semantic importance: for example, they represent omitted
|
|
|
|
// for-loop conditions, as in for(;;).
|
|
|
|
line = strings.Replace(line, "<<<NULL>>>", "NullStmt", 1)
|
|
|
|
trimmed := strings.TrimLeft(line, "|\\- `")
|
|
|
|
node := ast.Parse(trimmed)
|
|
|
|
indentLevel := (len(line) - len(trimmed)) / 2
|
|
|
|
nodes[counter] = treeNode{indentLevel, node}
|
|
|
|
counter++
|
2019-06-03 11:07:12 -04:00
|
|
|
if !Config.Debugast {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch node.(type) {
|
|
|
|
case *ast.Unknown:
|
|
|
|
fmt.Printf("Unrecognized node:\n")
|
|
|
|
printLinesWithContext(lines,i)
|
|
|
|
fmt.Printf("\n")
|
|
|
|
unknowns++
|
|
|
|
if unknowns > 5 {
|
|
|
|
fmt.Printf("nswrap failed due to unrecognized nodes.\n")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
nodes = nodes[0:counter]
|
|
|
|
|
2019-06-03 11:07:12 -04:00
|
|
|
if Config.Debugast && unknowns > 0 {
|
|
|
|
fmt.Printf("\nswrap failed due to unrecognized nodes.\n")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertLinesToNodesParallel(lines []string) []treeNode {
|
|
|
|
// function f separate full list on 2 parts and
|
|
|
|
// then each part can recursive run function f
|
|
|
|
var f func([]string, int) []treeNode
|
|
|
|
|
|
|
|
f = func(lines []string, deep int) []treeNode {
|
|
|
|
deep = deep - 2
|
|
|
|
part := len(lines) / 2
|
|
|
|
|
|
|
|
var tr1 = make(chan []treeNode)
|
|
|
|
var tr2 = make(chan []treeNode)
|
|
|
|
|
|
|
|
go func(lines []string, deep int) {
|
|
|
|
if deep <= 0 || len(lines) < deep {
|
|
|
|
tr1 <- convertLinesToNodes(lines)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tr1 <- f(lines, deep)
|
|
|
|
}(lines[0:part], deep)
|
|
|
|
|
|
|
|
go func(lines []string, deep int) {
|
|
|
|
if deep <= 0 || len(lines) < deep {
|
|
|
|
tr2 <- convertLinesToNodes(lines)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tr2 <- f(lines, deep)
|
|
|
|
}(lines[part:], deep)
|
|
|
|
|
|
|
|
defer close(tr1)
|
|
|
|
defer close(tr2)
|
|
|
|
|
|
|
|
return append(<-tr1, <-tr2...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parameter of deep - can be any, but effective to use
|
|
|
|
// same amount of CPU
|
|
|
|
return f(lines, runtime.NumCPU())
|
|
|
|
}
|
|
|
|
|
|
|
|
// buildTree converts an array of nodes, each prefixed with a depth into a tree.
|
|
|
|
func buildTree(nodes []treeNode, depth int) []ast.Node {
|
|
|
|
if len(nodes) == 0 {
|
|
|
|
return []ast.Node{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split the list into sections, treat each section as a tree with its own
|
|
|
|
// root.
|
|
|
|
sections := [][]treeNode{}
|
|
|
|
for _, node := range nodes {
|
|
|
|
if node.indent == depth {
|
|
|
|
sections = append(sections, []treeNode{node})
|
|
|
|
} else {
|
|
|
|
sections[len(sections)-1] = append(sections[len(sections)-1], node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
results := []ast.Node{}
|
|
|
|
for _, section := range sections {
|
|
|
|
slice := []treeNode{}
|
|
|
|
for _, n := range section {
|
|
|
|
if n.indent > depth {
|
|
|
|
slice = append(slice, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
children := buildTree(slice, depth+1)
|
|
|
|
for _, child := range children {
|
|
|
|
section[0].node.AddChild(child)
|
|
|
|
}
|
|
|
|
results = append(results, section[0].node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
func matches(x string, rs []string) bool {
|
|
|
|
for _,r := range rs {
|
|
|
|
if m, _ := regexp.MatchString("^" + r + "$",x); m {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:52:21 -04:00
|
|
|
// Start begins transpiling an input file.
|
2019-04-26 15:34:36 -04:00
|
|
|
func Start() (err error) {
|
2019-06-01 15:10:51 -04:00
|
|
|
astPP := []byte{}
|
|
|
|
if Config.Astfile != "" {
|
|
|
|
fmt.Printf("Reading ast file %s\n",Config.Astfile)
|
|
|
|
_, err = os.Stat(Config.Astfile)
|
2019-04-09 11:52:21 -04:00
|
|
|
if err != nil {
|
2019-06-01 15:10:51 -04:00
|
|
|
return fmt.Errorf("Input AST file %s not found",Config.Astfile)
|
|
|
|
}
|
|
|
|
astPP, err = ioutil.ReadFile(Config.Astfile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for _, in := range Config.Inputfiles {
|
|
|
|
_, err = os.Stat(in)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Input file %s is not found", in)
|
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
|
2019-06-01 15:10:51 -04:00
|
|
|
// Generate AST
|
|
|
|
cargs := []string{"-xobjective-c", "-Xclang", "-ast-dump",
|
|
|
|
"-fsyntax-only","-fno-color-diagnostics"}
|
|
|
|
if Config.Arc {
|
|
|
|
cargs = append(cargs,"-fobjc-arc")
|
|
|
|
}
|
|
|
|
cargs = append(cargs,Config.Inputfiles...)
|
|
|
|
fmt.Printf("Generating AST\n")
|
|
|
|
astPP, err = exec.Command("clang",cargs...).Output()
|
|
|
|
if err != nil {
|
|
|
|
// If clang fails it still prints out the AST, so we have to run it
|
|
|
|
// again to get the real error.
|
|
|
|
//errBody, _ := exec.Command("clang", cargs...).CombinedOutput()
|
|
|
|
var txt string
|
|
|
|
switch x := err.(type) {
|
|
|
|
case *exec.ExitError:
|
|
|
|
txt = string(x.Stderr)
|
|
|
|
default:
|
|
|
|
txt = err.Error()
|
|
|
|
}
|
|
|
|
fmt.Printf("clang failed:\n%s\n", txt)
|
|
|
|
os.Exit(-1)
|
2019-05-06 09:00:38 -04:00
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lines := readAST(astPP)
|
|
|
|
|
|
|
|
// Converting to nodes
|
2019-05-03 13:14:30 -04:00
|
|
|
fmt.Printf("Building nodes\n")
|
2019-05-11 23:03:56 -04:00
|
|
|
if Config.Positions {
|
|
|
|
ast.TrackPositions = true
|
|
|
|
}
|
2019-05-31 12:42:52 -04:00
|
|
|
if Config.Arc {
|
|
|
|
wrap.Arc = true
|
|
|
|
}
|
2019-05-11 23:03:56 -04:00
|
|
|
//NOTE: converting in parallel is slower on my system
|
|
|
|
//nodes := convertLinesToNodesParallel(lines)
|
|
|
|
nodes := convertLinesToNodes(lines)
|
2019-04-09 11:52:21 -04:00
|
|
|
|
|
|
|
// build tree
|
2019-05-03 13:14:30 -04:00
|
|
|
fmt.Printf("Assembling tree\n")
|
2019-04-09 11:52:21 -04:00
|
|
|
tree := buildTree(nodes, 0)
|
2019-04-26 15:34:36 -04:00
|
|
|
w := wrap.NewWrapper(Debug)
|
2019-04-26 22:44:30 -04:00
|
|
|
w.Package = Config.Package
|
2019-05-02 14:14:29 -04:00
|
|
|
w.Frameworks(Config.Frameworks)
|
2019-04-26 15:34:36 -04:00
|
|
|
w.Import(Config.Imports)
|
2019-05-09 12:25:45 -04:00
|
|
|
w.SysImport(Config.Sysimports)
|
2019-04-26 15:41:29 -04:00
|
|
|
w.Pragma(Config.Pragma)
|
2019-05-09 22:05:04 -04:00
|
|
|
w.Delegate(Config.Delegates)
|
2019-05-21 16:26:52 -04:00
|
|
|
w.Subclass(Config.Subclasses)
|
2019-05-09 12:25:45 -04:00
|
|
|
if Config.Vaargs == 0 {
|
|
|
|
Config.Vaargs = 16
|
2019-04-29 16:14:45 -04:00
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
w.Vaargs = Config.Vaargs
|
2019-04-26 22:44:30 -04:00
|
|
|
for _, u := range tree {
|
2019-05-03 13:14:30 -04:00
|
|
|
fmt.Printf("--processing translation unit\n")
|
2019-04-26 22:44:30 -04:00
|
|
|
for _, n := range(u.Children()) {
|
|
|
|
switch x := n.(type) {
|
|
|
|
case *ast.ObjCInterfaceDecl:
|
|
|
|
w.AddInterface(x)
|
2019-05-21 16:26:52 -04:00
|
|
|
for _,ss := range Config.Subclasses {
|
2019-05-24 00:12:46 -04:00
|
|
|
if sc,ok := ss["superclass"]; ok {
|
|
|
|
if matches(x.Name,sc) {
|
2019-05-21 16:26:52 -04:00
|
|
|
Config.Classes = append(Config.Classes,x.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 22:44:30 -04:00
|
|
|
case *ast.ObjCCategoryDecl:
|
|
|
|
w.AddCategory(x)
|
|
|
|
case *ast.TypedefDecl:
|
2019-05-23 16:31:47 -04:00
|
|
|
w.AddTypedef(x.Name,x.Type)
|
2019-05-01 10:58:29 -04:00
|
|
|
case *ast.FunctionDecl:
|
|
|
|
if matches(x.Name,Config.Functions) {
|
|
|
|
w.AddFunction(x)
|
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
case *ast.ObjCProtocolDecl:
|
2019-05-23 16:31:47 -04:00
|
|
|
w.AddProtocol(x)
|
2019-05-02 14:14:29 -04:00
|
|
|
case *ast.EnumDecl:
|
|
|
|
w.AddEnum(x,Config.Enums)
|
2019-04-26 22:44:30 -04:00
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-26 15:34:36 -04:00
|
|
|
w.Wrap(Config.Classes)
|
2019-04-09 11:52:21 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2019-05-11 23:03:56 -04:00
|
|
|
if Profile {
|
|
|
|
f1, err := os.Create("cpuprofile.pprof")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not create CPU profile: ", err)
|
|
|
|
}
|
|
|
|
defer f1.Close()
|
|
|
|
if err := pprof.StartCPUProfile(f1); err != nil {
|
|
|
|
log.Fatal("could not start CPU profile: ", err)
|
|
|
|
}
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2019-05-09 12:25:45 -04:00
|
|
|
confbytes, err := ioutil.ReadFile("nswrap.yaml")
|
|
|
|
if err != nil {
|
2019-05-29 22:36:49 -04:00
|
|
|
fmt.Printf("%s\n\nFATAL ERROR: Configuration file must be present in directory where nswrap\nis invoked.\n",err)
|
2019-05-09 12:25:45 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-06-03 11:07:12 -04:00
|
|
|
if err = yaml.UnmarshalStrict(confbytes,&Config); err != nil {
|
2019-05-09 12:25:45 -04:00
|
|
|
fmt.Printf("Cannot decode config file nswrap.yaml. %s\n",err)
|
2019-04-26 15:34:36 -04:00
|
|
|
os.Exit(-1)
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
2019-04-26 15:34:36 -04:00
|
|
|
if err := Start(); err != nil {
|
2019-04-09 11:52:21 -04:00
|
|
|
fmt.Printf("Error: %v\n", err)
|
2019-04-26 15:34:36 -04:00
|
|
|
os.Exit(-1)
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
2019-05-11 23:03:56 -04:00
|
|
|
if Profile {
|
|
|
|
f2, err := os.Create("memprofile.pprof")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("could not create memory profile: ", err)
|
|
|
|
}
|
|
|
|
defer f2.Close()
|
|
|
|
runtime.GC() // get up-to-date statistics
|
|
|
|
if err := pprof.WriteHeapProfile(f2); err != nil {
|
|
|
|
log.Fatal("could not write memory profile: ", err)
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|