2019-04-26 09:04:53 -04:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-26 10:07:34 -04:00
|
|
|
//super is a map recording which class is the parent of each other class
|
2019-04-26 09:04:53 -04:00
|
|
|
var super map[string]string
|
|
|
|
|
2019-04-26 10:07:34 -04:00
|
|
|
//wrapped is a map recording whether a given GoType is to be "wrapped" in a
|
|
|
|
//go struct.
|
|
|
|
var wrapped map[string]bool
|
|
|
|
|
2019-04-26 09:04:53 -04:00
|
|
|
func Super(c string) string {
|
|
|
|
if super == nil {
|
|
|
|
super = make(map[string]string)
|
|
|
|
}
|
|
|
|
return super[c]
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetSuper(c, p string) {
|
|
|
|
if super == nil {
|
|
|
|
super = make(map[string]string)
|
|
|
|
}
|
|
|
|
super[c] = p
|
|
|
|
}
|
|
|
|
|
|
|
|
type Type struct {
|
|
|
|
Node *Node
|
|
|
|
Class string
|
|
|
|
ctype string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewType(n *Node, c string) *Type {
|
|
|
|
return &Type{
|
|
|
|
Node: n,
|
|
|
|
Class: c,
|
|
|
|
ctype: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTypeFromString(t,c string) *Type {
|
|
|
|
n,err := Parse(t)
|
2019-04-26 14:08:43 -04:00
|
|
|
if n.CtypeSimplified() == "id" {
|
|
|
|
n,err = Parse("NSObject*")
|
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return &Type{}
|
|
|
|
}
|
|
|
|
return &Type{
|
|
|
|
Node: n,
|
|
|
|
Class: c,
|
|
|
|
ctype: "",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) String() string {
|
|
|
|
return t.Node.String()
|
|
|
|
}
|
|
|
|
|
2019-04-26 14:08:43 -04:00
|
|
|
func Wrap(s string) {
|
2019-04-26 10:07:34 -04:00
|
|
|
if wrapped == nil {
|
|
|
|
wrapped = make(map[string]bool)
|
|
|
|
}
|
|
|
|
// it is the pointers to this type that get wrapped
|
2019-04-26 14:08:43 -04:00
|
|
|
wrapped["*" + s] = true
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) BaseType() *Type {
|
|
|
|
ret := NewType(
|
|
|
|
t.Node.BaseType(),
|
|
|
|
t.Class,
|
|
|
|
)
|
|
|
|
if ret.CType() == ret.Class + " *" { // "instancename"
|
|
|
|
ret.ctype = ret.Class
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func swapstars(s string) string {
|
|
|
|
for i := len(s) - 1; i > 0 && s[i] == '*'; {
|
|
|
|
s = "*" + s[:i]
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) CGoType() string {
|
|
|
|
ct := swapstars("C." + t.CType())
|
2019-04-26 14:15:18 -04:00
|
|
|
ct = strings.ReplaceAll(ct,"unsigned ","u")
|
|
|
|
ct = strings.ReplaceAll(ct,"signed ","u")
|
|
|
|
ct = strings.ReplaceAll(ct,"long ","long")
|
|
|
|
ct = strings.ReplaceAll(ct,"complex ","complex")
|
2019-04-26 09:04:53 -04:00
|
|
|
ct = strings.ReplaceAll(ct," ","_")
|
|
|
|
return ct
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) GoType() string {
|
2019-04-26 10:56:00 -04:00
|
|
|
return _goType(t.CType())
|
|
|
|
}
|
|
|
|
|
|
|
|
func _goType(ct string) string {
|
|
|
|
ct = swapstars(ct)
|
2019-04-26 09:04:53 -04:00
|
|
|
ct = strings.Title(ct)
|
|
|
|
ct = strings.ReplaceAll(ct," ","")
|
|
|
|
ct = strings.ReplaceAll(ct,"Struct","")
|
|
|
|
return ct
|
|
|
|
}
|
|
|
|
|
2019-04-26 10:56:00 -04:00
|
|
|
|
2019-04-26 09:04:53 -04:00
|
|
|
func (t *Type) CType() string {
|
2019-04-26 15:08:44 -04:00
|
|
|
return t._CType(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) CTypeAttrib() string {
|
|
|
|
return t._CType(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) _CType(attrib bool) string {
|
2019-04-26 09:04:53 -04:00
|
|
|
if t.ctype != "" { // cache
|
|
|
|
return t.ctype
|
|
|
|
}
|
2019-04-26 15:08:44 -04:00
|
|
|
var ct string
|
|
|
|
if attrib {
|
|
|
|
ct = t.Node.Ctype()
|
|
|
|
} else {
|
|
|
|
ct = t.Node.CtypeSimplified()
|
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
ct = strings.ReplaceAll(ct,"instancename",t.Class)
|
|
|
|
ct = strings.ReplaceAll(ct,"instancetype",t.Class + " *")
|
|
|
|
if len(ct) > 1 && ct[:2] == "id" {
|
|
|
|
ct = "NSObject *" + ct[2:]
|
|
|
|
}
|
|
|
|
if len(ct) > 11 {
|
2019-04-26 15:08:44 -04:00
|
|
|
if ct[:12] == "instancename" { ct = t.Class + ct[12:] }
|
|
|
|
if ct[:12] == "instancetype" { ct = t.Class + ct[12:] + " *" }
|
|
|
|
}
|
|
|
|
if attrib {
|
|
|
|
t._CType(false)
|
|
|
|
} else {
|
|
|
|
t.ctype = ct
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
return ct
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) GoTypeDecl() string {
|
2019-04-26 10:07:34 -04:00
|
|
|
if wrapped[t.GoType()] {
|
2019-04-26 09:04:53 -04:00
|
|
|
return t.GoInterfaceDecl()
|
|
|
|
}
|
2019-04-26 10:56:00 -04:00
|
|
|
tp := t.BaseType()
|
2019-04-26 14:08:43 -04:00
|
|
|
gt := tp.GoType()
|
|
|
|
switch gt {
|
|
|
|
case "", "Void":
|
|
|
|
return ""
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf(`
|
2019-04-26 09:04:53 -04:00
|
|
|
type %s %s
|
2019-04-26 14:08:43 -04:00
|
|
|
`,gt,tp.CGoType())
|
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) GoInterfaceDecl() string {
|
2019-04-26 14:08:43 -04:00
|
|
|
gt := t.GoType()
|
|
|
|
if gt[0] == '*' {
|
|
|
|
gt = gt[1:] // dereference wrapped types
|
2019-04-26 10:56:00 -04:00
|
|
|
}
|
|
|
|
x := ""
|
2019-04-26 14:08:43 -04:00
|
|
|
super := Super(gt)
|
2019-04-26 09:04:53 -04:00
|
|
|
if super == "" {
|
|
|
|
super = "ptr unsafe.Pointer"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(`
|
2019-04-26 10:56:00 -04:00
|
|
|
%stype %s struct { %s }
|
2019-04-26 14:08:43 -04:00
|
|
|
`,x,gt,super)
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) CToGo(cval string) string { // cast C value to CGo
|
|
|
|
if t.Node.IsPointer() {
|
|
|
|
cval = "unsafe.Pointer(" + cval + ")"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("(%s)(%s)",t.GoType(),cval)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call a C function from Go with a given return type and parameter types
|
|
|
|
func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
|
|
|
|
var ret strings.Builder
|
|
|
|
rt := rtype.CType()
|
|
|
|
if rt != "void" {
|
2019-04-26 10:07:34 -04:00
|
|
|
if wrapped[rtype.GoType()] {
|
|
|
|
ret.WriteString(" ret := &" + rtype.GoType()[1:] + "{}\n")
|
|
|
|
ret.WriteString(" ret.ptr = unsafe.Pointer(")
|
2019-04-26 09:04:53 -04:00
|
|
|
} else {
|
2019-04-26 10:07:34 -04:00
|
|
|
ret.WriteString(" return (" + rtype.GoType() + ")(")
|
2019-04-26 09:04:53 -04:00
|
|
|
if rtype.Node.IsPointer() {
|
|
|
|
ret.WriteString("unsafe.Pointer(")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.WriteString("C." + name + "(")
|
|
|
|
parms := []string{}
|
|
|
|
for i := 0; i < len(pnames); i++ {
|
|
|
|
pn,pt := pnames[i],ptypes[i]
|
|
|
|
p := pn
|
2019-04-26 10:07:34 -04:00
|
|
|
if wrapped[pt.GoType()] {
|
2019-04-26 09:04:53 -04:00
|
|
|
p = pn + ".ptr"
|
|
|
|
} else {
|
2019-04-26 14:08:43 -04:00
|
|
|
if pt.Node.IsPointer() {
|
|
|
|
p = "unsafe.Pointer(" + pn + ")"
|
|
|
|
} else {
|
|
|
|
p = "(" + pt.CGoType() + ")(" + pn + ")"
|
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
parms = append(parms,p)
|
|
|
|
}
|
|
|
|
ret.WriteString(strings.Join(parms,", "))
|
|
|
|
ret.WriteString(")")
|
|
|
|
if rt != "void" {
|
2019-04-26 10:07:34 -04:00
|
|
|
if wrapped[rtype.GoType()] {
|
2019-04-26 09:04:53 -04:00
|
|
|
ret.WriteString(`)
|
2019-04-26 10:07:34 -04:00
|
|
|
return ret
|
2019-04-26 09:04:53 -04:00
|
|
|
`)
|
|
|
|
} else {
|
|
|
|
ret.WriteString(")")
|
|
|
|
if rtype.Node.IsPointer() {
|
|
|
|
ret.WriteString(")")
|
|
|
|
}
|
2019-04-26 10:07:34 -04:00
|
|
|
ret.WriteString("\n")
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret.String()
|
|
|
|
}
|
|
|
|
|