forked from go-python/gopy
/
gen_type.go
219 lines (205 loc) · 6.15 KB
/
gen_type.go
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2019 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bind
// extTypes = these are types external to any targeted packages
// pyWrapOnly = only generate python wrapper code, not go code
func (g *pyGen) genType(sym *symbol, extTypes, pyWrapOnly bool) {
if !sym.isType() {
return
}
if sym.isBasic() && !sym.isNamed() {
return
}
if sym.isNamedBasic() {
// TODO: could have methods!
return
}
// TODO: not handling yet:
if sym.isSignature() {
return
}
if !pyWrapOnly {
switch {
case sym.isPointer() || sym.isInterface():
g.genTypeHandlePtr(sym)
case sym.isSlice() || sym.isMap() || sym.isArray():
g.genTypeHandleImplPtr(sym)
default:
g.genTypeHandle(sym)
}
}
if extTypes {
if sym.isSlice() || sym.isArray() {
g.genSlice(sym, extTypes, pyWrapOnly, nil)
} else if sym.isMap() {
g.genMap(sym, extTypes, pyWrapOnly, nil)
} else if sym.isInterface() || sym.isStruct() {
if pyWrapOnly {
g.genExtClass(sym)
}
}
} else {
if g.pkg == goPackage || !sym.isNamed() { // only named types are generated separately
if sym.isSlice() {
g.genSlice(sym, extTypes, pyWrapOnly, nil)
} else if sym.isMap() {
g.genMap(sym, extTypes, pyWrapOnly, nil)
}
}
if sym.isArray() {
g.genSlice(sym, extTypes, pyWrapOnly, nil)
}
}
}
func (g *pyGen) genTypeHandlePtr(sym *symbol) {
if sym.goname == "interface{}" {
return
}
gonm := sym.goname
g.gofile.Printf("\n// Converters for pointer handles for type: %s\n", gonm)
g.gofile.Printf("func %s(h CGoHandle) %s {\n", sym.py2go, gonm)
g.gofile.Indent()
g.gofile.Printf("p := gopyh.VarFromHandle((gopyh.CGoHandle)(h), %[1]q)\n", gonm)
g.gofile.Printf("if p == nil {\n")
g.gofile.Indent()
g.gofile.Printf("return nil\n")
g.gofile.Outdent()
g.gofile.Printf("}\n")
if sym.isStruct() {
g.gofile.Printf("return gopyh.Embed(p, reflect.TypeOf(%s{})).(%s)\n", nonPtrName(gonm), gonm)
} else {
g.gofile.Printf("return p.(%s)\n", gonm)
}
g.gofile.Outdent()
g.gofile.Printf("}\n")
g.gofile.Printf("func %s(p interface{})%s CGoHandle {\n", sym.go2py, sym.go2pyParenEx)
g.gofile.Indent()
g.gofile.Printf("return CGoHandle(gopyh.Register(\"%s\", p))\n", gonm)
g.gofile.Outdent()
g.gofile.Printf("}\n")
}
// implicit pointer types: slice, map, array
func (g *pyGen) genTypeHandleImplPtr(sym *symbol) {
gonm := sym.goname
ptrnm := gonm
nptrnm := gonm
if ptrnm[0] != '*' {
ptrnm = "*" + ptrnm
} else {
nptrnm = gonm[1:]
}
g.gofile.Printf("\n// Converters for implicit pointer handles for type: %s\n", gonm)
g.gofile.Printf("func ptrFromHandle_%s(h CGoHandle) %s {\n", sym.id, ptrnm)
g.gofile.Indent()
g.gofile.Printf("p := gopyh.VarFromHandle((gopyh.CGoHandle)(h), %[1]q)\n", gonm)
g.gofile.Printf("if p == nil {\n")
g.gofile.Indent()
g.gofile.Printf("return nil\n")
g.gofile.Outdent()
g.gofile.Printf("}\n")
g.gofile.Printf("return p.(%s)\n", ptrnm)
g.gofile.Outdent()
g.gofile.Printf("}\n")
g.gofile.Printf("func deptrFromHandle_%s(h CGoHandle) %s {\n", sym.id, nptrnm)
g.gofile.Indent()
g.gofile.Printf("p := ptrFromHandle_%s(h)\n", sym.id)
if !sym.isArray() {
g.gofile.Printf("if p == nil {\n")
g.gofile.Indent()
g.gofile.Printf("return nil\n")
g.gofile.Outdent()
g.gofile.Printf("}\n")
}
g.gofile.Printf("return *p\n")
g.gofile.Outdent()
g.gofile.Printf("}\n")
g.gofile.Printf("func %s(p interface{})%s CGoHandle {\n", sym.go2py, sym.go2pyParenEx)
g.gofile.Indent()
g.gofile.Printf("return CGoHandle(gopyh.Register(\"%s\", p))\n", gonm)
g.gofile.Outdent()
g.gofile.Printf("}\n")
}
func nonPtrName(nm string) string {
if nm[0] == '*' {
return nm[1:]
}
return nm
}
func (g *pyGen) genTypeHandle(sym *symbol) {
gonm := sym.goname
ptrnm := gonm
if ptrnm[0] != '*' {
ptrnm = "*" + ptrnm
}
py2go := nonPtrName(sym.py2go)
g.gofile.Printf("\n// Converters for non-pointer handles for type: %s\n", gonm)
g.gofile.Printf("func %s(h CGoHandle) %s {\n", py2go, ptrnm)
g.gofile.Indent()
g.gofile.Printf("p := gopyh.VarFromHandle((gopyh.CGoHandle)(h), %[1]q)\n", gonm)
g.gofile.Printf("if p == nil {\n")
g.gofile.Indent()
g.gofile.Printf("return nil\n")
g.gofile.Outdent()
g.gofile.Printf("}\n")
if sym.isStruct() {
g.gofile.Printf("return gopyh.Embed(p, reflect.TypeOf(%s{})).(%s)\n", nonPtrName(gonm), ptrnm)
} else {
g.gofile.Printf("return p.(%s)\n", ptrnm)
}
g.gofile.Outdent()
g.gofile.Printf("}\n")
g.gofile.Printf("func %s(p interface{})%s CGoHandle {\n", sym.go2py, sym.go2pyParenEx)
g.gofile.Indent()
g.gofile.Printf("return CGoHandle(gopyh.Register(\"%s\", p))\n", gonm)
g.gofile.Outdent()
g.gofile.Printf("}\n")
}
// genExtClass generates minimal python wrappers for external classes (struct, interface, etc)
func (g *pyGen) genExtClass(sym *symbol) {
pkgname := sym.gopkg.Name()
// note: all external wrapper classes are defined in base go.py module, so we exclude go.
g.pywrap.Printf(`
# Python type for %[4]s
class %[2]s(GoClass):
""%[3]q""
`,
pkgname,
sym.id,
sym.doc,
sym.goname,
)
g.pywrap.Indent()
g.pywrap.Printf("def __init__(self, *args, **kwargs):\n")
g.pywrap.Indent()
g.pywrap.Printf(`"""
handle=A Go-side object is always initialized with an explicit handle=arg
"""
`)
g.pywrap.Printf("if len(kwargs) == 1 and 'handle' in kwargs:\n")
g.pywrap.Indent()
g.pywrap.Printf("self.handle = kwargs['handle']\n")
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
g.pywrap.Outdent()
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], GoClass):\n")
g.pywrap.Indent()
g.pywrap.Printf("self.handle = args[0].handle\n")
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
g.pywrap.Outdent()
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], int):\n")
g.pywrap.Indent()
g.pywrap.Printf("self.handle = args[0]\n")
g.pywrap.Printf("_%s.IncRef(self.handle)\n", g.pypkgname)
g.pywrap.Outdent()
g.pywrap.Printf("else:\n")
g.pywrap.Indent()
g.pywrap.Printf("self.handle = 0\n")
g.pywrap.Outdent()
g.pywrap.Outdent()
g.pywrap.Printf("def __del__(self):\n")
g.pywrap.Indent()
g.pywrap.Printf("_%s.DecRef(self.handle)\n", g.pypkgname)
g.pywrap.Outdent()
g.pywrap.Printf("\n")
g.pywrap.Outdent()
}