2019-02-11 10:47:24 +00:00
|
|
|
// 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
|
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
// 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) {
|
2019-02-11 10:47:24 +00:00
|
|
|
if !sym.isType() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sym.isBasic() && !sym.isNamed() {
|
|
|
|
return
|
|
|
|
}
|
2019-02-13 21:13:10 +00:00
|
|
|
|
2019-02-15 07:47:01 +00:00
|
|
|
if sym.isNamedBasic() {
|
|
|
|
// todo: could have methods!
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:13:10 +00:00
|
|
|
// todo: not handling yet:
|
|
|
|
if sym.isSignature() {
|
2019-02-13 08:02:26 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-11 10:47:24 +00:00
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
if !pyWrapOnly {
|
|
|
|
if sym.isPointer() || sym.isInterface() {
|
|
|
|
g.genTypeHandlePtr(sym)
|
|
|
|
} else {
|
|
|
|
g.genTypeHandle(sym)
|
|
|
|
}
|
2019-02-11 10:47:24 +00:00
|
|
|
}
|
2019-02-15 07:47:01 +00:00
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
if extTypes {
|
2019-02-23 10:26:05 +00:00
|
|
|
if sym.isSlice() || sym.isArray() {
|
2019-03-02 10:30:58 +00:00
|
|
|
g.genSlice(sym, extTypes, pyWrapOnly, nil)
|
2019-02-23 10:26:05 +00:00
|
|
|
} else if sym.isMap() {
|
2019-03-02 10:30:58 +00:00
|
|
|
g.genMap(sym, extTypes, pyWrapOnly, nil)
|
2019-02-22 10:33:05 +00:00
|
|
|
} else if sym.isInterface() || sym.isStruct() {
|
|
|
|
if pyWrapOnly {
|
|
|
|
g.genExtClass(sym)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-02 10:30:58 +00:00
|
|
|
if g.pkg == goPackage {
|
|
|
|
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)
|
2019-02-21 09:52:10 +00:00
|
|
|
}
|
2019-02-15 07:47:01 +00:00
|
|
|
}
|
2019-02-11 10:47:24 +00:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:09:23 +00:00
|
|
|
func (g *pyGen) genTypeHandlePtr(sym *symbol) {
|
2019-03-04 10:17:26 +00:00
|
|
|
if sym.goname == "interface{}" {
|
|
|
|
return
|
|
|
|
}
|
2019-02-19 09:46:15 +00:00
|
|
|
gonm := sym.gofmt()
|
|
|
|
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)
|
2019-02-11 10:47:24 +00:00
|
|
|
g.gofile.Indent()
|
2019-02-21 09:52:10 +00:00
|
|
|
g.gofile.Printf("p := gopyh.VarFmHandle((gopyh.CGoHandle)(h), %[1]q)\n", gonm)
|
2019-02-13 05:53:20 +00:00
|
|
|
g.gofile.Printf("if p == nil {\n")
|
|
|
|
g.gofile.Indent()
|
|
|
|
g.gofile.Printf("return nil\n")
|
|
|
|
g.gofile.Outdent()
|
|
|
|
g.gofile.Printf("}\n")
|
2019-02-27 10:31:46 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-02-13 08:02:26 +00:00
|
|
|
g.gofile.Outdent()
|
|
|
|
g.gofile.Printf("}\n")
|
2019-02-24 09:22:19 +00:00
|
|
|
g.gofile.Printf("func %s(p interface{})%s CGoHandle {\n", sym.go2py, sym.go2pyParenEx)
|
2019-02-13 08:02:26 +00:00
|
|
|
g.gofile.Indent()
|
2019-02-21 09:52:10 +00:00
|
|
|
g.gofile.Printf("return CGoHandle(gopyh.Register(\"%s\", p))\n", gonm)
|
2019-02-13 08:02:26 +00:00
|
|
|
g.gofile.Outdent()
|
|
|
|
g.gofile.Printf("}\n")
|
|
|
|
}
|
|
|
|
|
2019-02-22 10:33:05 +00:00
|
|
|
func nonPtrName(nm string) string {
|
|
|
|
if nm[0] == '*' {
|
|
|
|
return nm[1:]
|
|
|
|
}
|
|
|
|
return nm
|
|
|
|
}
|
|
|
|
|
2019-02-20 11:09:23 +00:00
|
|
|
func (g *pyGen) genTypeHandle(sym *symbol) {
|
2019-02-19 09:46:15 +00:00
|
|
|
gonm := sym.gofmt()
|
2019-02-20 03:57:42 +00:00
|
|
|
ptrnm := gonm
|
2019-02-25 07:01:51 +00:00
|
|
|
if ptrnm[0] != '*' {
|
2019-02-20 03:57:42 +00:00
|
|
|
ptrnm = "*" + ptrnm
|
|
|
|
}
|
2019-02-22 10:33:05 +00:00
|
|
|
py2go := nonPtrName(sym.py2go)
|
2019-02-19 09:46:15 +00:00
|
|
|
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)
|
2019-02-13 08:02:26 +00:00
|
|
|
g.gofile.Indent()
|
2019-02-21 09:52:10 +00:00
|
|
|
g.gofile.Printf("p := gopyh.VarFmHandle((gopyh.CGoHandle)(h), %[1]q)\n", gonm)
|
2019-02-13 08:02:26 +00:00
|
|
|
g.gofile.Printf("if p == nil {\n")
|
|
|
|
g.gofile.Indent()
|
|
|
|
g.gofile.Printf("return nil\n")
|
|
|
|
g.gofile.Outdent()
|
|
|
|
g.gofile.Printf("}\n")
|
2019-02-27 10:31:46 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-02-11 10:47:24 +00:00
|
|
|
g.gofile.Outdent()
|
|
|
|
g.gofile.Printf("}\n")
|
2019-02-24 09:22:19 +00:00
|
|
|
g.gofile.Printf("func %s(p interface{})%s CGoHandle {\n", sym.go2py, sym.go2pyParenEx)
|
2019-02-11 10:47:24 +00:00
|
|
|
g.gofile.Indent()
|
2019-02-21 09:52:10 +00:00
|
|
|
g.gofile.Printf("return CGoHandle(gopyh.Register(\"%s\", p))\n", gonm)
|
2019-02-11 10:47:24 +00:00
|
|
|
g.gofile.Outdent()
|
|
|
|
g.gofile.Printf("}\n")
|
|
|
|
}
|
2019-02-21 09:52:10 +00:00
|
|
|
|
|
|
|
// genExtClass generates minimal python wrappers for external classes (struct, interface, etc)
|
|
|
|
func (g *pyGen) genExtClass(sym *symbol) {
|
|
|
|
pkgname := sym.gopkg.Name()
|
2019-03-02 10:30:58 +00:00
|
|
|
// note: all external wrapper classes are defined in base go.py module, so we exclude go.
|
2019-02-21 09:52:10 +00:00
|
|
|
g.pywrap.Printf(`
|
2019-02-22 10:33:05 +00:00
|
|
|
# Python type for %[4]s
|
2019-03-02 10:30:58 +00:00
|
|
|
class %[2]s(GoClass):
|
2019-02-21 09:52:10 +00:00
|
|
|
""%[3]q""
|
|
|
|
`,
|
|
|
|
pkgname,
|
2019-02-21 21:42:44 +00:00
|
|
|
sym.id,
|
2019-02-21 09:52:10 +00:00
|
|
|
sym.doc,
|
2019-02-22 10:33:05 +00:00
|
|
|
sym.goname,
|
2019-02-21 09:52:10 +00:00
|
|
|
)
|
|
|
|
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.Outdent()
|
2019-03-02 10:30:58 +00:00
|
|
|
g.pywrap.Printf("elif len(args) == 1 and isinstance(args[0], GoClass):\n")
|
2019-02-23 09:20:04 +00:00
|
|
|
g.pywrap.Indent()
|
|
|
|
g.pywrap.Printf("self.handle = args[0].handle\n")
|
|
|
|
g.pywrap.Outdent()
|
2019-03-02 10:30:58 +00:00
|
|
|
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.Outdent()
|
2019-02-21 09:52:10 +00:00
|
|
|
g.pywrap.Printf("else:\n")
|
|
|
|
g.pywrap.Indent()
|
|
|
|
g.pywrap.Printf("self.handle = 0\n")
|
|
|
|
g.pywrap.Outdent()
|
|
|
|
g.pywrap.Outdent()
|
|
|
|
g.pywrap.Printf("\n")
|
|
|
|
g.pywrap.Outdent()
|
|
|
|
}
|