forked from paulsmith/gogeos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoscapi.py
157 lines (131 loc) · 4.25 KB
/
geoscapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import sys
from string import Template
from collections import namedtuple
from pycparser import c_parser, c_ast, parse_file
Func = namedtuple('Func', ('name', 'type', 'args'))
Arg = namedtuple('Arg', ('name', 'type'))
Type = namedtuple('Type', ('ptr', 'name', 'array'))
class FuncDeclVisitor(c_ast.NodeVisitor):
def __init__(self):
self.funcs = []
self.reset()
def reset(self):
self.name = None
self.ptr = ''
self.type = None
self.inargs = False
self.args = []
self.argname = None
self.array = False
def visit_Typedef(self, node):
# Prevent func decls in typedefs from being visited
pass
def visit_FuncDecl(self, node):
self.visit(node.type)
if node.args:
self.inargs = True
self.visit(node.args)
self.funcs.append(Func(self.name, self.type, self.args))
self.reset()
def visit_PtrDecl(self, node):
self.ptr += '*'
self.visit(node.type)
def visit_TypeDecl(self, node):
if node.type.__class__.__name__ == 'Struct':
return
if self.inargs:
self.argname = node.declname
else:
self.name = node.declname
self.visit(node.type)
def visit_ArrayDecl(self, node):
self.array = True
self.visit(node.type)
def visit_IdentifierType(self, node):
type_ = Type(self.ptr, ' '.join(node.names), self.array)
if self.inargs:
self.args.append(Arg(self.argname, type_))
else:
self.type = type_
self.ptr = ''
self.array = False
def cgo_func_wrappers(filename):
ast = parse_file(filename, use_cpp=True)
v = FuncDeclVisitor()
v.visit(ast)
funcnames = {}
threadsafe = []
for func in v.funcs:
funcnames[func.name] = func
for func in v.funcs:
if not func.name.endswith('_r'):
if func.name + '_r' in funcnames:
threadsafe.append(funcnames[func.name + '_r'])
else:
threadsafe.append(func)
print("""
package geos
// Created mechanically from C API header - DO NOT EDIT
/*
#include <geos_c.h>
*/
import "C"
import (
"unsafe"
)\
""")
typemap = {
"unsigned char": "uchar",
"unsigned int": "uint",
}
identmap = {
"type": "_type",
}
for func in threadsafe:
def gotype(ctype):
type_ = "C." + typemap.get(ctype.name, ctype.name)
if ctype.ptr:
type_ = ctype.ptr + type_
if ctype.array:
type_ = '[]' + type_
return type_
def goident(arg, inbody=True):
def voidptr(ctype):
return ctype.ptr and ctype.name == 'void'
ident = identmap.get(arg.name, arg.name)
if arg.type.array and inbody:
ident = '&' + ident + '[0]'
if voidptr(arg.type) and inbody:
ident = 'unsafe.Pointer(' + ident + ')'
return ident
# Go function signature
gosig = "func $name($parameters)"
if func.type.name != "void":
gosig += " $result"
gosig += " {"
t = Template(gosig)
params = ", ".join([goident(p, inbody=False) + " " + gotype(p.type) for p in func.args if p.type.name != 'GEOSContextHandle_t'])
result = gotype(func.type)
func_name = "c" + func.name
if func_name.endswith('_r'):
func_name = func_name[:-2]
print(t.substitute(name=func_name, parameters=params, result=result))
# Go function body
gobody = """\
\t${return_stmt}C.$name($args)
}
"""
if func.name.endswith("_r") and func.name != "initGEOS_r":
gobody = """\
\t${handle_lock}.Lock()
\tdefer ${handle_lock}.Unlock()
""" + gobody
t = Template(gobody)
args = ", ".join([goident(p) for p in func.args])
return_stmt = 'return ' if func.type.name != 'void' else ''
print(t.substitute(return_stmt=return_stmt, name=func.name, args=args, handle_lock='handlemu'))
if __name__ == "__main__":
cgo_func_wrappers(sys.argv[1])
#from pycparser.c_generator import CGenerator
#ast = parse_file(sys.argv[1], use_cpp=True)
#print(CGenerator().visit(ast))