2019-04-26 09:04:53 -04:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-06-13 16:47:36 -04:00
|
|
|
"os"
|
2019-05-01 10:58:29 -04:00
|
|
|
"regexp"
|
2019-04-26 09:04:53 -04:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-06-27 11:41:58 -04:00
|
|
|
var Gogc bool
|
2020-06-24 13:03:53 -04:00
|
|
|
var Typesubs map[string]string
|
2019-06-27 11:41:58 -04:00
|
|
|
|
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-06-13 16:47:36 -04:00
|
|
|
var tdptr map[string]bool
|
2019-04-26 10:07:34 -04:00
|
|
|
|
2019-05-23 16:31:47 -04:00
|
|
|
func ShouldWrap(gt string) bool {
|
|
|
|
return wrapped[gt]
|
2019-04-29 11:46:48 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
func PtrShouldWrap(gt string) bool {
|
|
|
|
return gt != "" && gt[0] == '*' && wrapped[gt[1:]]
|
|
|
|
}
|
|
|
|
|
2019-06-13 16:47:36 -04:00
|
|
|
func TypedefShouldWrap(gt string) bool {
|
|
|
|
return tdptr[gt]
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:31:47 -04:00
|
|
|
//goInterfaces records the names of top level Go interfaces.
|
2019-04-29 11:46:48 -04:00
|
|
|
var goInterfaces map[string]bool
|
|
|
|
|
2019-05-01 12:32:42 -04:00
|
|
|
func IsGoInterface(gt string) bool {
|
2019-04-29 11:46:48 -04:00
|
|
|
return goInterfaces[gt]
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
func PtrIsGoInterface(gt string) bool {
|
|
|
|
return gt != "" && gt[0] == '*' && goInterfaces[gt[1:]]
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:44:30 -04:00
|
|
|
//TypeParameters maps, for each class, a TypedefName to a type, representing
|
|
|
|
//the Objective-C type parameters for that class
|
|
|
|
var TypeParameters map[string]map[string]string
|
|
|
|
|
2019-04-29 13:31:13 -04:00
|
|
|
//Typedefs maps from C types to the Type of a typedef with that name.
|
|
|
|
var typedefs map[string]*Type
|
|
|
|
|
|
|
|
func (t *Type) Typedef() *Type {
|
2019-04-30 06:59:05 -04:00
|
|
|
return typedefs[t.CType()]
|
2019-04-29 13:31:13 -04:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
var (
|
2019-06-11 12:38:22 -04:00
|
|
|
r_id *regexp.Regexp
|
2019-05-01 10:58:29 -04:00
|
|
|
r_instancename *regexp.Regexp
|
|
|
|
r_instancetype *regexp.Regexp
|
|
|
|
)
|
|
|
|
|
2019-04-29 13:31:13 -04:00
|
|
|
func init() {
|
|
|
|
super = make(map[string]string)
|
|
|
|
wrapped = make(map[string]bool)
|
2019-06-13 16:47:36 -04:00
|
|
|
tdptr = make(map[string]bool)
|
2019-04-29 13:31:13 -04:00
|
|
|
goInterfaces = make(map[string]bool)
|
|
|
|
TypeParameters = make(map[string]map[string]string)
|
|
|
|
typedefs = make(map[string]*Type)
|
2019-05-01 10:58:29 -04:00
|
|
|
|
2019-05-03 13:14:30 -04:00
|
|
|
r_id = regexp.MustCompile(`\bid\b`)
|
2019-05-01 10:58:29 -04:00
|
|
|
r_instancename = regexp.MustCompile(`\binstancename\b`)
|
|
|
|
r_instancetype = regexp.MustCompile(`\binstancetype\b`)
|
2019-04-29 13:31:13 -04:00
|
|
|
}
|
2019-04-26 22:44:30 -04:00
|
|
|
|
2019-04-26 09:04:53 -04:00
|
|
|
func Super(c string) string {
|
|
|
|
return super[c]
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetSuper(c, p string) {
|
|
|
|
super[c] = p
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:44:30 -04:00
|
|
|
func SetTypeParam(c, n, t string) {
|
|
|
|
if TypeParameters[c] == nil {
|
|
|
|
TypeParameters[c] = make(map[string]string)
|
|
|
|
}
|
|
|
|
TypeParameters[c][n] = t
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:31:47 -04:00
|
|
|
func AddTypedef(n string, tp *Type) {
|
2019-04-30 06:59:05 -04:00
|
|
|
//fmt.Printf("AddTypedef(): %s -> %s\n",n,t)
|
2019-05-23 16:31:47 -04:00
|
|
|
typedefs[n] = tp
|
2019-04-26 22:44:30 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 09:04:53 -04:00
|
|
|
type Type struct {
|
2019-06-11 12:38:22 -04:00
|
|
|
Node *Node
|
|
|
|
Class string
|
2019-04-29 16:14:45 -04:00
|
|
|
Variadic bool
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
func (t *Type) CloneToClass(c string) *Type {
|
|
|
|
return &Type{
|
2019-06-11 12:38:22 -04:00
|
|
|
Node: t.Node,
|
|
|
|
Class: c,
|
2019-05-01 10:58:29 -04:00
|
|
|
Variadic: t.Variadic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func clean(n *Node, c string) (*Node, bool) {
|
2019-04-30 09:14:25 -04:00
|
|
|
if n == nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
return nil, false
|
2019-04-30 09:14:25 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ret := NewNode(n.Kind, n.Content)
|
2019-04-30 06:59:05 -04:00
|
|
|
ret.Children = n.Children
|
2019-05-11 23:03:56 -04:00
|
|
|
//fmt.Printf("clean(%s,%s)\n",n.CType(),c)
|
2019-04-30 06:59:05 -04:00
|
|
|
recur := false
|
|
|
|
if TypeParameters[c] != nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
for k, v := range TypeParameters[c] {
|
|
|
|
recur = ret.renameTypedefs(k, v)
|
2019-04-30 06:59:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if recur {
|
|
|
|
clean(n, c)
|
2019-06-11 12:38:22 -04:00
|
|
|
return ret, true
|
2019-04-30 06:59:05 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
return n, false
|
2019-04-30 06:59:05 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 09:04:53 -04:00
|
|
|
func NewType(n *Node, c string) *Type {
|
2019-06-11 12:38:22 -04:00
|
|
|
n2, _ := clean(n, c)
|
2019-04-26 09:04:53 -04:00
|
|
|
return &Type{
|
2019-06-11 12:38:22 -04:00
|
|
|
Node: n2,
|
2019-04-26 09:04:53 -04:00
|
|
|
Class: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func NewTypeFromString(t, c string) *Type {
|
2019-04-30 06:59:05 -04:00
|
|
|
//fmt.Printf("t/c: %s/%s\n",t,c)
|
2019-06-11 12:38:22 -04:00
|
|
|
n, err := Parse(t)
|
2019-04-30 06:59:05 -04:00
|
|
|
//fmt.Printf("%p %s",n,n.String())
|
2019-04-26 09:04:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return &Type{}
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if n2, ok := clean(n, c); ok {
|
2019-05-03 13:14:30 -04:00
|
|
|
//found type parameters, re-parse
|
2019-06-11 12:38:22 -04:00
|
|
|
return NewTypeFromString(n2.CType(), c)
|
2019-04-26 22:44:30 -04:00
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
return &Type{
|
2019-06-11 12:38:22 -04:00
|
|
|
Node: n,
|
2019-04-26 09:04:53 -04:00
|
|
|
Class: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) String() string {
|
|
|
|
return t.Node.String()
|
|
|
|
}
|
|
|
|
|
2019-04-27 22:24:05 -04:00
|
|
|
func (t *Type) PointsTo() *Type {
|
2019-05-01 10:58:29 -04:00
|
|
|
if td := t.Typedef(); td != nil {
|
|
|
|
return td.PointsTo()
|
|
|
|
}
|
|
|
|
if pt := t.Node.PointsTo(); pt != nil {
|
|
|
|
return NewType(t.Node.PointsTo(), t.Class)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
2019-04-27 22:24:05 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 14:08:43 -04:00
|
|
|
func Wrap(s string) {
|
2019-06-13 09:25:53 -04:00
|
|
|
//fmt.Printf("wrapped[%s] = true\n",s)
|
2019-04-27 22:24:05 -04:00
|
|
|
wrapped[s] = true
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) BaseType() *Type {
|
2019-05-01 10:58:29 -04:00
|
|
|
if t == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ret := NewType(
|
2019-04-26 09:04:53 -04:00
|
|
|
t.Node.BaseType(),
|
|
|
|
t.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-06-11 12:38:22 -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")
|
|
|
|
ct = strings.ReplaceAll(ct, " ", "_")
|
2019-04-26 09:04:53 -04:00
|
|
|
return ct
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Type) GoType() string {
|
2019-04-26 10:56:00 -04:00
|
|
|
return _goType(t.CType())
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
func typeSub(gt string) string {
|
|
|
|
if Typesubs == nil {
|
|
|
|
return gt
|
|
|
|
}
|
|
|
|
i := 0
|
|
|
|
for ; i < len(gt); i++ {
|
|
|
|
if gt[i] != '*' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gtp := gt[:i]
|
|
|
|
gte := gt[i:]
|
|
|
|
for k, v := range Typesubs {
|
|
|
|
if gte == k {
|
|
|
|
return gtp + v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return gt
|
|
|
|
}
|
|
|
|
|
2019-04-26 10:56:00 -04:00
|
|
|
func _goType(ct string) string {
|
2019-04-26 09:04:53 -04:00
|
|
|
ct = strings.Title(ct)
|
2019-06-11 12:38:22 -04:00
|
|
|
ct = strings.ReplaceAll(ct, " ", "")
|
2019-06-13 09:25:53 -04:00
|
|
|
ct = strings.TrimPrefix(ct, "Struct")
|
|
|
|
ct = swapstars(ct)
|
|
|
|
if len(ct) > 0 && ct[0] == '*' && IsGoInterface(ct[1:]) {
|
2020-06-24 13:03:53 -04:00
|
|
|
return typeSub(ct)
|
2019-04-29 11:46:48 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
if ct == "Id" {
|
|
|
|
ct = "*Id"
|
|
|
|
}
|
|
|
|
if ShouldWrap(ct) {
|
2020-06-24 13:03:53 -04:00
|
|
|
return typeSub(ct)
|
2019-05-24 00:12:46 -04:00
|
|
|
}
|
2019-05-06 13:21:36 -04:00
|
|
|
if len(ct) > 4 && ct[len(ct)-4:len(ct)] == "Void" {
|
|
|
|
ct = ct[:len(ct)-5] + "unsafe.Pointer"
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
return typeSub(ct)
|
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-05-01 10:58:29 -04:00
|
|
|
if t == nil {
|
2019-05-01 11:01:50 -04:00
|
|
|
//fmt.Println("nil sent to _CType()")
|
2019-05-01 10:58:29 -04:00
|
|
|
return ""
|
|
|
|
}
|
2019-04-26 15:08:44 -04:00
|
|
|
var ct string
|
|
|
|
if attrib {
|
2019-06-11 12:38:22 -04:00
|
|
|
ignore := map[string]bool{"GenericList": true}
|
2019-05-11 23:03:56 -04:00
|
|
|
ct = t.Node._CType(ignore)
|
2019-04-26 15:08:44 -04:00
|
|
|
} else {
|
2019-05-11 23:03:56 -04:00
|
|
|
ct = t.Node.CTypeSimplified()
|
2019-04-26 15:08:44 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ct = r_id.ReplaceAllString(ct, "NSObject*")
|
|
|
|
ct = r_instancename.ReplaceAllString(ct, t.Class)
|
|
|
|
ct = r_instancetype.ReplaceAllString(ct, t.Class+"*")
|
2019-04-26 09:04:53 -04:00
|
|
|
return ct
|
|
|
|
}
|
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
func (t *Type) GoTypeDecl(fin bool) string {
|
2019-06-13 16:47:36 -04:00
|
|
|
gt := t.GoType()
|
|
|
|
if wrapped[gt] {
|
2019-06-18 09:15:46 -04:00
|
|
|
return t.GoInterfaceDecl(fin)
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
if t.Node.IsId() {
|
2019-04-26 22:44:30 -04:00
|
|
|
return ""
|
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
if td := t.Typedef(); td != nil {
|
|
|
|
tdgt := td.GoType()
|
|
|
|
if ShouldWrap(tdgt) || PtrShouldWrap(tdgt) { // type alias
|
|
|
|
if PtrIsGoInterface(tdgt) {
|
|
|
|
tdgt = "*Id"
|
|
|
|
}
|
|
|
|
if IsGoInterface(tdgt) {
|
|
|
|
tdgt = "Id"
|
|
|
|
}
|
|
|
|
Wrap(gt)
|
|
|
|
tdptr[gt] = true
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
type %s = %s
|
|
|
|
`, gt, tdgt)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
cgt := td.CGoType()
|
|
|
|
eq := ""
|
|
|
|
if len(cgt) > 9 && cgt[:9] == "C.struct_" {
|
|
|
|
//cgt = "C." + cgt[9:]
|
|
|
|
eq = "= "
|
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
return fmt.Sprintf(`
|
2020-06-24 13:03:53 -04:00
|
|
|
type %s %s%s
|
|
|
|
`, gt, eq, cgt)
|
2019-06-13 16:47:36 -04:00
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
if Debug {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf(" writing GoTypeDecl for %s\n", gt)
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
2019-04-26 14:08:43 -04:00
|
|
|
switch gt {
|
|
|
|
case "", "Void":
|
|
|
|
return ""
|
|
|
|
default:
|
2020-06-24 13:03:53 -04:00
|
|
|
cgt := t.CGoType()
|
|
|
|
eq := ""
|
|
|
|
if len(cgt) > 9 && cgt[:9] == "C.struct_" {
|
|
|
|
//cgt = "C." + cgt[9:]
|
|
|
|
eq = "= "
|
|
|
|
}
|
2019-04-26 14:08:43 -04:00
|
|
|
return fmt.Sprintf(`
|
2020-06-24 13:03:53 -04:00
|
|
|
type %s %s%s
|
|
|
|
`, gt, eq, cgt)
|
2019-04-26 14:08:43 -04:00
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
func (t *Type) GoInterfaceDecl(fin bool) string {
|
2019-05-02 14:14:29 -04:00
|
|
|
ct := t.CType()
|
2019-04-26 14:08:43 -04:00
|
|
|
gt := t.GoType()
|
2019-06-13 09:25:53 -04:00
|
|
|
if Debug {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf(" writing GoInterfaceDecl for %s\n", gt)
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
|
|
|
if gt[0] == '*' {
|
|
|
|
gt = gt[1:] // dereference wrapped types
|
|
|
|
ct = ct[:len(ct)-1]
|
2020-06-24 13:03:53 -04:00
|
|
|
//fmt.Printf(" dereferenced %s\n", gt)
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
2019-05-02 14:14:29 -04:00
|
|
|
super := Super(ct)
|
2019-04-26 09:04:53 -04:00
|
|
|
if super == "" {
|
2019-06-13 09:25:53 -04:00
|
|
|
//fmt.Printf("goInterfaces[%s] = true\n",gt)
|
2019-04-29 11:46:48 -04:00
|
|
|
goInterfaces[gt] = true
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
type %s interface {
|
|
|
|
Ptr() unsafe.Pointer
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, gt)
|
2019-04-29 11:46:48 -04:00
|
|
|
}
|
2019-05-01 12:32:42 -04:00
|
|
|
if IsGoInterface(super) {
|
2019-04-29 11:46:48 -04:00
|
|
|
super = "Id"
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
return fmt.Sprintf(`
|
2019-04-27 22:24:05 -04:00
|
|
|
type %s struct { %s }
|
2019-06-13 09:25:53 -04:00
|
|
|
func (o *%s) Ptr() unsafe.Pointer { if o == nil { return nil }; return o.ptr }
|
|
|
|
func (o *Id) %s() *%s {
|
2019-06-19 14:07:33 -04:00
|
|
|
return (*%s)(unsafe.Pointer(o))
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, gt, super, gt, gt, gt, gt)
|
2019-04-26 22:44:30 -04:00
|
|
|
}
|
|
|
|
|
2019-05-03 13:14:30 -04:00
|
|
|
func (t *Type) IsFunctionPtr() bool {
|
|
|
|
if t == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if td := t.Typedef(); td != nil {
|
|
|
|
return td.IsFunctionPtr()
|
|
|
|
}
|
2019-05-11 23:03:56 -04:00
|
|
|
if pt := t.PointsTo(); pt != nil {
|
2019-05-03 13:14:30 -04:00
|
|
|
return pt.IsFunction()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:44:30 -04:00
|
|
|
func (t *Type) IsFunction() bool {
|
2019-05-01 10:58:29 -04:00
|
|
|
if t == nil {
|
2019-06-04 11:27:46 -04:00
|
|
|
//fmt.Println("nil sent to IsFunction()")
|
2019-05-01 10:58:29 -04:00
|
|
|
return false
|
|
|
|
}
|
2019-04-29 13:31:13 -04:00
|
|
|
if td := t.Typedef(); td != nil {
|
2019-04-26 22:44:30 -04:00
|
|
|
return td.IsFunction()
|
|
|
|
}
|
|
|
|
return t.Node.IsFunction()
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:04:39 -04:00
|
|
|
func (t *Type) IsValist() bool {
|
|
|
|
if t == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if t.GoType() == "__va_list_tag" { // OS dependent
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
func (t *Type) ReturnType() *Type {
|
2019-06-04 11:27:46 -04:00
|
|
|
if td := t.Typedef(); td != nil {
|
|
|
|
return td.ReturnType()
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
if rt := t.Node.ReturnType(); rt != nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
return NewType(rt, t.Class)
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 22:44:30 -04:00
|
|
|
func (t *Type) IsPointer() bool {
|
2019-06-04 11:27:46 -04:00
|
|
|
if t == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-04-29 13:31:13 -04:00
|
|
|
if td := t.Typedef(); td != nil {
|
2019-04-26 22:44:30 -04:00
|
|
|
return td.IsPointer()
|
|
|
|
}
|
|
|
|
return t.Node.IsPointer()
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
2019-05-11 23:03:56 -04:00
|
|
|
// cast C value to CGo
|
|
|
|
func (t *Type) CToGo(cval string) string {
|
2019-04-26 22:44:30 -04:00
|
|
|
if t.IsPointer() {
|
2019-04-26 09:04:53 -04:00
|
|
|
cval = "unsafe.Pointer(" + cval + ")"
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
return fmt.Sprintf("(%s)(%s)", t.GoType(), cval)
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call a C function from Go with a given return type and parameter types
|
2020-06-24 13:03:53 -04:00
|
|
|
func GoToC(sname, name string, pnames, snames []string, rtype *Type, ptypes []*Type, fun, fin bool, cm bool, goImports map[string]bool) string {
|
2019-05-01 10:58:29 -04:00
|
|
|
if rtype == nil {
|
2019-06-04 11:27:46 -04:00
|
|
|
//fmt.Println("nil sent to GoToC")
|
2019-05-01 10:58:29 -04:00
|
|
|
return ""
|
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
var ret strings.Builder
|
|
|
|
rt := rtype.CType()
|
2019-05-23 16:31:47 -04:00
|
|
|
rtgt := rtype.GoType()
|
2019-06-13 09:25:53 -04:00
|
|
|
//fmt.Printf("GoToC(%s): rtgt == %s\n",name,rtgt)
|
|
|
|
if PtrIsGoInterface(rtgt) {
|
|
|
|
//fmt.Printf(" PtrIsGoInterface(%s) = true\n",rtgt)
|
|
|
|
rtgt = "*Id"
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
sw := PtrShouldWrap(rtgt) || rtgt == "*Id" || TypedefShouldWrap(rtgt)
|
|
|
|
isptr := rtype.IsPointer()
|
2019-04-26 09:04:53 -04:00
|
|
|
if rt != "void" {
|
2019-06-13 16:47:36 -04:00
|
|
|
switch {
|
|
|
|
case PtrShouldWrap(rtgt), rtgt == "*Id":
|
|
|
|
isptr = true
|
|
|
|
if PtrIsGoInterface(rtgt) {
|
|
|
|
rtgt = "*Id"
|
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
ret.WriteString(fmt.Sprintf(
|
2020-06-24 13:03:53 -04:00
|
|
|
`ret := &%s{}
|
2019-06-13 16:47:36 -04:00
|
|
|
ret.ptr = unsafe.Pointer(`, rtgt[1:]))
|
|
|
|
case TypedefShouldWrap(rtgt):
|
|
|
|
isptr = true
|
|
|
|
rtgt := rtype.Typedef().GoType()
|
|
|
|
if PtrIsGoInterface(rtgt) {
|
|
|
|
rtgt = "*Id"
|
|
|
|
}
|
|
|
|
ret.WriteString(fmt.Sprintf(
|
2020-06-24 13:03:53 -04:00
|
|
|
`ret := &%s{}
|
2019-06-13 16:47:36 -04:00
|
|
|
ret.ptr = unsafe.Pointer(`, rtgt[1:]))
|
|
|
|
default:
|
2019-05-23 16:31:47 -04:00
|
|
|
if rtgt == "BOOL" {
|
2021-11-16 16:01:03 -05:00
|
|
|
ret.WriteString("ret := (bool)(")
|
2019-05-23 16:31:47 -04:00
|
|
|
rtgt = "bool"
|
|
|
|
} else {
|
2019-05-28 15:52:16 -04:00
|
|
|
ret.WriteString("ret := (" + rtgt + ")(")
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
if isptr {
|
2019-05-23 16:31:47 -04:00
|
|
|
ret.WriteString("unsafe.Pointer(")
|
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.WriteString("C." + name + "(")
|
|
|
|
parms := []string{}
|
|
|
|
for i := 0; i < len(pnames); i++ {
|
2019-06-11 12:38:22 -04:00
|
|
|
pn, pt := pnames[i], ptypes[i]
|
2019-06-13 16:47:36 -04:00
|
|
|
ptgt := pt.GoType()
|
2019-04-26 09:04:53 -04:00
|
|
|
p := pn
|
2019-06-13 16:47:36 -04:00
|
|
|
switch {
|
|
|
|
case (PtrShouldWrap(ptgt) || PtrIsGoInterface(ptgt)) && !pt.Variadic:
|
2019-04-29 11:46:48 -04:00
|
|
|
p = pn + ".Ptr()"
|
2019-06-13 16:47:36 -04:00
|
|
|
case TypedefShouldWrap(ptgt) && !pt.Variadic && fun:
|
|
|
|
p = "(" + pt.CGoType() + ")(" + pn + ".Ptr())"
|
|
|
|
case TypedefShouldWrap(ptgt) && !pt.Variadic && !fun:
|
|
|
|
p = pn + ".Ptr()"
|
|
|
|
case snames[i] != "":
|
2020-06-24 13:03:53 -04:00
|
|
|
cast := pt.CGoType()
|
|
|
|
if len(ptgt) > 2 && ptgt[:1] == "*" && PtrShouldWrap(ptgt[1:]) {
|
|
|
|
cast = "*unsafe.Pointer"
|
|
|
|
}
|
|
|
|
p = fmt.Sprintf("(%s)(unsafe.Pointer(&%s[0]))", cast, snames[i])
|
2019-06-13 16:47:36 -04:00
|
|
|
case pt.Variadic:
|
|
|
|
p = "unsafe.Pointer(&" + p + ")"
|
|
|
|
case pt.IsPointer() && !fun:
|
|
|
|
p = "unsafe.Pointer(" + pn + ")"
|
|
|
|
case pt.IsPointer() && fun && pt.BaseType().CType() == "void":
|
|
|
|
p = pn
|
|
|
|
default:
|
|
|
|
p = "(" + pt.CGoType() + ")(" + pn + ")"
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
parms = append(parms, p)
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ret.WriteString(strings.Join(parms, ", "))
|
2019-04-26 09:04:53 -04:00
|
|
|
ret.WriteString(")")
|
2019-05-23 16:31:47 -04:00
|
|
|
if rt != "void" && !sw {
|
2019-04-30 09:14:25 -04:00
|
|
|
ret.WriteString(")")
|
2019-06-13 16:47:36 -04:00
|
|
|
}
|
|
|
|
if isptr {
|
|
|
|
ret.WriteString(")")
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for i, sname := range snames {
|
|
|
|
if sname == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
ptgt := ptypes[i].GoType()
|
2020-06-24 13:03:53 -04:00
|
|
|
if !(len(ptgt) > 2 && ptgt[:1] == "*" && PtrShouldWrap(ptgt[1:])) {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
if len(ptgt) < 2 {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("Error in function translation -- argument %s to %s should be pointer to pointer\n", pnames[i], name)
|
2019-06-13 16:47:36 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
ptgt = ptgt[2:]
|
2019-06-13 09:25:53 -04:00
|
|
|
if IsGoInterface(ptgt) {
|
|
|
|
ptgt = "Id"
|
|
|
|
}
|
2019-06-27 11:41:58 -04:00
|
|
|
dogc := ""
|
|
|
|
if Gogc {
|
2020-06-24 13:03:53 -04:00
|
|
|
goImports["runtime"] = true
|
2019-06-27 11:41:58 -04:00
|
|
|
dogc = fmt.Sprintf(`
|
|
|
|
runtime.SetFinalizer((*%s)[i], func(o *%s) {
|
|
|
|
o.Release()
|
|
|
|
})`, pnames[i], ptgt)
|
|
|
|
}
|
2019-05-28 15:52:16 -04:00
|
|
|
ret.WriteString(fmt.Sprintf(`
|
2019-05-29 00:02:27 -04:00
|
|
|
(*%s) = (*%s)[:cap(*%s)]
|
2019-05-28 15:52:16 -04:00
|
|
|
for i := 0; i < len(*%s); i++ {
|
|
|
|
if %s[i] == nil {
|
|
|
|
(*%s) = (*%s)[:i]
|
|
|
|
break
|
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
if (*%s)[i] == nil {
|
2019-06-27 11:41:58 -04:00
|
|
|
(*%s)[i] = &%s{}%s
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
2019-05-28 15:52:16 -04:00
|
|
|
(*%s)[i].ptr = %s[i]
|
2019-06-27 11:41:58 -04:00
|
|
|
}`, pnames[i], pnames[i], pnames[i], pnames[i], sname, pnames[i], pnames[i], pnames[i], pnames[i], ptgt, dogc, pnames[i], sname))
|
2019-05-28 15:52:16 -04:00
|
|
|
}
|
|
|
|
if rt != "void" {
|
2019-06-18 17:29:52 -04:00
|
|
|
cmp := ""
|
|
|
|
if sw {
|
2020-06-24 13:03:53 -04:00
|
|
|
ka := ""
|
|
|
|
if !cm {
|
|
|
|
goImports["runtime"] = true
|
|
|
|
ka = "runtime.KeepAlive(o); "
|
|
|
|
}
|
2019-06-18 17:29:52 -04:00
|
|
|
if !cm && sname != "copy" && sname != "mutableCopy" {
|
|
|
|
cmp = fmt.Sprintf(`
|
2020-06-24 13:03:53 -04:00
|
|
|
if ret.ptr == o.ptr { %sreturn (%s)(unsafe.Pointer(o)) }`, ka, rtgt)
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
2019-06-18 17:29:52 -04:00
|
|
|
ret.WriteString(fmt.Sprintf(`
|
2020-06-24 13:03:53 -04:00
|
|
|
if ret.ptr == nil { %sreturn ret }%s`, ka, cmp))
|
2019-06-18 17:29:52 -04:00
|
|
|
}
|
|
|
|
if fin {
|
2019-06-18 09:15:46 -04:00
|
|
|
dbg := ""
|
|
|
|
dbg2 := ""
|
|
|
|
if Debug {
|
|
|
|
dbg = fmt.Sprintf(`fmt.Printf("Setting finalizer (%s): %%p -> %%p\n", ret, ret.ptr)
|
|
|
|
`, rtgt)
|
|
|
|
dbg2 = fmt.Sprintf(`fmt.Printf("Finalizer (%s): release %%p -> %%p\n", o, o.ptr)
|
|
|
|
`, rtgt)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
goImports["runtime"] = true
|
2019-06-13 09:25:53 -04:00
|
|
|
ret.WriteString(fmt.Sprintf(`
|
2019-06-18 17:29:52 -04:00
|
|
|
%sruntime.SetFinalizer(ret, func(o %s) {
|
2019-06-18 09:15:46 -04:00
|
|
|
%so.Release()
|
2019-06-18 17:29:52 -04:00
|
|
|
})`, dbg, rtgt, dbg2))
|
2020-06-24 13:03:53 -04:00
|
|
|
}
|
|
|
|
if !cm {
|
|
|
|
goImports["runtime"] = true
|
|
|
|
ret.WriteString(`
|
|
|
|
runtime.KeepAlive(o)`)
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
ret.WriteString(`
|
|
|
|
return ret`)
|
2020-06-24 13:03:53 -04:00
|
|
|
} else {
|
|
|
|
if !cm {
|
|
|
|
goImports["runtime"] = true
|
|
|
|
ret.WriteString(`
|
|
|
|
runtime.KeepAlive(o)`)
|
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
2019-04-26 09:04:53 -04:00
|
|
|
return ret.String()
|
|
|
|
}
|