2019-04-09 11:52:21 -04:00
|
|
|
package wrap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-26 22:44:30 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-06-06 00:22:07 -04:00
|
|
|
"reflect"
|
2019-05-02 14:14:29 -04:00
|
|
|
"regexp"
|
2019-05-28 13:03:23 -04:00
|
|
|
"sort"
|
2019-04-29 16:14:45 -04:00
|
|
|
"strconv"
|
2019-04-09 23:19:49 -04:00
|
|
|
"strings"
|
2019-04-09 11:52:21 -04:00
|
|
|
|
2019-05-29 12:57:53 -04:00
|
|
|
"git.wow.st/gmp/nswrap/ast"
|
|
|
|
"git.wow.st/gmp/nswrap/types"
|
2019-04-09 11:52:21 -04:00
|
|
|
)
|
|
|
|
|
2019-04-10 14:00:48 -04:00
|
|
|
var (
|
|
|
|
Debug = false
|
2019-06-11 12:38:22 -04:00
|
|
|
// Arc flag is for debugging only, your builds will break if you turn it on
|
|
|
|
Arc = false
|
2019-06-06 00:22:07 -04:00
|
|
|
Autorelease = false
|
2019-06-11 12:38:22 -04:00
|
|
|
Gogc = false
|
2021-11-16 16:01:03 -05:00
|
|
|
dbclass = ""
|
2019-04-10 14:00:48 -04:00
|
|
|
)
|
|
|
|
|
2019-04-09 11:52:21 -04:00
|
|
|
type Wrapper struct {
|
2019-06-11 12:38:22 -04:00
|
|
|
Package string
|
|
|
|
Interfaces map[string]*Interface
|
|
|
|
Functions map[string]*Method
|
|
|
|
NamedEnums map[string]*Enum
|
|
|
|
AnonEnums []*Enum
|
|
|
|
Delegates map[string]map[string][]string
|
|
|
|
Subclasses map[string]*Subclass
|
|
|
|
Protocols map[string]*Protocol
|
|
|
|
Frameworks []string
|
2020-06-24 13:03:53 -04:00
|
|
|
Libraries []string
|
2019-06-04 21:18:06 -04:00
|
|
|
Frameworkdirs []string
|
2019-06-11 12:38:22 -04:00
|
|
|
Pragmas []string
|
|
|
|
|
|
|
|
cgoFlags strings.Builder // put cGo directives here
|
|
|
|
cImports strings.Builder // put C imports and sysimports here
|
|
|
|
goImports map[string]bool // put required go imports here
|
|
|
|
cCode strings.Builder // put cGo code here
|
|
|
|
goTypes strings.Builder // put Go type declarations here
|
|
|
|
goConst strings.Builder // put Go constants (from C enums) here
|
|
|
|
goCode strings.Builder // put Go code here
|
2019-05-09 12:25:45 -04:00
|
|
|
goExports strings.Builder // put exported Go functions here
|
2019-04-26 14:08:43 -04:00
|
|
|
goHelpers strings.Builder // put Go helper functions here
|
2019-05-23 16:31:47 -04:00
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
ProcessedTypes map[string]bool
|
2019-05-23 16:31:47 -04:00
|
|
|
ProcessedClassMethods map[string]bool
|
2019-06-11 12:38:22 -04:00
|
|
|
Vaargs int
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
|
2019-04-11 11:46:24 -04:00
|
|
|
func NewWrapper(debug bool) *Wrapper {
|
|
|
|
Debug = debug
|
2020-06-24 13:03:53 -04:00
|
|
|
//types.Debug = Debug
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Println("// Debug mode")
|
|
|
|
}
|
2019-04-26 15:34:36 -04:00
|
|
|
ret := &Wrapper{
|
2019-06-11 12:38:22 -04:00
|
|
|
Interfaces: map[string]*Interface{},
|
|
|
|
Functions: map[string]*Method{},
|
|
|
|
NamedEnums: map[string]*Enum{},
|
|
|
|
AnonEnums: []*Enum{},
|
|
|
|
Protocols: map[string]*Protocol{},
|
|
|
|
Subclasses: map[string]*Subclass{},
|
|
|
|
ProcessedTypes: map[string]bool{},
|
2019-05-23 16:31:47 -04:00
|
|
|
ProcessedClassMethods: map[string]bool{},
|
2019-06-11 12:38:22 -04:00
|
|
|
Vaargs: 16,
|
|
|
|
goImports: map[string]bool{},
|
|
|
|
}
|
|
|
|
ret.goImports["unsafe"] = true
|
|
|
|
if Gogc {
|
2019-06-27 11:41:58 -04:00
|
|
|
types.Gogc = true
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
2019-04-29 11:46:48 -04:00
|
|
|
ret.goTypes.WriteString(`
|
2019-05-23 16:31:47 -04:00
|
|
|
type Id struct {
|
|
|
|
ptr unsafe.Pointer
|
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
func (o *Id) Ptr() unsafe.Pointer { if o == nil { return nil }; return o.ptr }
|
2019-04-26 15:34:36 -04:00
|
|
|
`)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wrapper) Import(ss []string) {
|
2020-06-24 13:03:53 -04:00
|
|
|
if len(ss) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, s := range ss {
|
2019-05-10 02:00:56 -04:00
|
|
|
w.cImports.WriteString("\n#import \"" + s + "\"\n")
|
2019-04-26 15:34:36 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
w.cImports.WriteString("\n")
|
2019-04-26 15:34:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wrapper) SysImport(ss []string) {
|
2020-06-24 13:03:53 -04:00
|
|
|
if len(ss) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, s := range ss {
|
2019-05-10 02:00:56 -04:00
|
|
|
w.cImports.WriteString("\n#import <" + s + ">\n")
|
2019-04-26 15:41:29 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
w.cImports.WriteString("\n")
|
2019-04-26 15:41:29 -04:00
|
|
|
}
|
|
|
|
|
2019-05-21 16:26:52 -04:00
|
|
|
func (w *Wrapper) Delegate(ds map[string]map[string][]string) {
|
2019-05-09 22:05:04 -04:00
|
|
|
w.Delegates = ds
|
|
|
|
}
|
|
|
|
|
2019-05-21 16:26:52 -04:00
|
|
|
func (w *Wrapper) Subclass(ds map[string]map[string][]string) {
|
2019-06-11 12:38:22 -04:00
|
|
|
for k, v := range ds {
|
2019-05-24 12:36:01 -04:00
|
|
|
sc := &Subclass{
|
2019-06-11 12:38:22 -04:00
|
|
|
Overrides: []string{},
|
2019-05-24 12:36:01 -04:00
|
|
|
NewMethods: []string{},
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if len(v) == 0 {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("No superclass specified for subclass %s\n", k)
|
2019-05-24 12:36:01 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if len(v) > 1 {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Multiple inheritance not permitted for subclass %s\n", k)
|
2019-05-24 12:36:01 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
sc.Name = k
|
2019-06-11 12:38:22 -04:00
|
|
|
for x, y := range v {
|
2019-05-24 12:36:01 -04:00
|
|
|
sc.Super = x
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, m := range y {
|
2019-05-24 12:36:01 -04:00
|
|
|
switch m[0] {
|
2019-06-11 12:38:22 -04:00
|
|
|
case '-', '+':
|
|
|
|
sc.NewMethods = append(sc.NewMethods, m)
|
2019-05-24 12:36:01 -04:00
|
|
|
default:
|
2019-06-11 12:38:22 -04:00
|
|
|
sc.Overrides = append(sc.Overrides, m)
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Subclasses[sc.Name] = sc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Subclass struct {
|
2019-06-11 12:38:22 -04:00
|
|
|
Name, Super string
|
|
|
|
Overrides []string
|
|
|
|
NewMethods []string
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 23:19:49 -04:00
|
|
|
type Property struct {
|
2019-06-18 09:15:46 -04:00
|
|
|
Name, Attr string
|
|
|
|
Type *types.Type
|
|
|
|
retained, notretained bool
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Parameter struct {
|
2019-04-26 15:08:44 -04:00
|
|
|
Pname, Vname string
|
2019-06-11 12:38:22 -04:00
|
|
|
Type *types.Type
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Method struct {
|
2019-05-28 13:03:23 -04:00
|
|
|
Name, GoName, Class, GoClass string
|
2019-06-11 12:38:22 -04:00
|
|
|
Type *types.Type
|
|
|
|
ClassMethod bool
|
|
|
|
Parameters []*Parameter
|
2019-06-11 16:41:21 -04:00
|
|
|
Unavailable bool
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-27 11:41:58 -04:00
|
|
|
// ShouldFinalize returns true on a method that returns an object that should
|
|
|
|
// have a GC finalizer.
|
2019-06-13 09:25:53 -04:00
|
|
|
func (m *Method) ShouldFinalize() bool {
|
2019-06-19 14:07:33 -04:00
|
|
|
grtype := m.Type.GoType()
|
|
|
|
return Gogc && grtype != "NSAutoreleasePool" &&
|
|
|
|
(types.PtrShouldWrap(grtype) || grtype == "*Id") &&
|
|
|
|
(!m.ClassMethod || IsRetained(m.Name))
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:07:33 -04:00
|
|
|
// IsRetained returns true if a given instance method returns a retained object.
|
2019-06-27 11:41:58 -04:00
|
|
|
// NSWrap will not send a 'retain' message to these objects before returning
|
|
|
|
// them to Go.
|
2019-06-19 14:07:33 -04:00
|
|
|
func IsRetained(name string) bool {
|
2020-06-24 13:03:53 -04:00
|
|
|
return ((len(name) >= 3 && name[:3] == "new") ||
|
2019-06-19 14:07:33 -04:00
|
|
|
(len(name) >= 4 && name[:4] == "init") ||
|
|
|
|
(len(name) >= 4 && name[:4] == "copy") ||
|
|
|
|
(len(name) >= 5 && name[:5] == "alloc") ||
|
|
|
|
(len(name) >= 11 && name[:11] == "mutableCopy"))
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:07:33 -04:00
|
|
|
// IsRetainedProperty returns true if the name matches a retained property for
|
|
|
|
// the given interface.
|
|
|
|
func (i *Interface) IsRetainedProperty(name string) bool {
|
2019-06-18 09:15:46 -04:00
|
|
|
if p, ok := i.Properties[name]; ok {
|
|
|
|
if p.retained {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if p.notretained {
|
|
|
|
return false
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
attrs := strings.Split(p.Attr, " ")
|
2019-06-18 09:15:46 -04:00
|
|
|
for _, a := range attrs {
|
|
|
|
if a == "retain" {
|
|
|
|
p.retained = true // cache this result
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.notretained = true // cache this result
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:03:23 -04:00
|
|
|
//Fully disambiguated method name (m.GoName + all parameter names)
|
|
|
|
func (m *Method) LongName() string {
|
|
|
|
ret := m.GoName
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, p := range m.Parameters {
|
2019-05-28 13:03:23 -04:00
|
|
|
ret = ret + p.Pname
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:04:39 -04:00
|
|
|
func (m *Method) HasUnsupportedType() bool {
|
2019-06-11 12:38:22 -04:00
|
|
|
return m.Type.IsFunction() ||
|
2019-05-28 20:04:39 -04:00
|
|
|
m.Type.IsFunctionPtr() ||
|
2020-06-24 13:03:53 -04:00
|
|
|
m.Type.CGoType() == "C.longdouble" ||
|
2019-05-28 20:04:39 -04:00
|
|
|
m.hasUnsupportedParam()
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
type EnumConstant struct {
|
|
|
|
name string
|
|
|
|
tp *types.Type
|
|
|
|
}
|
|
|
|
|
2019-05-02 14:14:29 -04:00
|
|
|
type Enum struct {
|
2019-06-11 12:38:22 -04:00
|
|
|
Name string
|
|
|
|
Type *types.Type
|
2020-06-24 13:03:53 -04:00
|
|
|
Constants []EnumConstant
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 12:25:45 -04:00
|
|
|
type Protocol struct {
|
2019-05-28 13:03:23 -04:00
|
|
|
InstanceMethods, ClassMethods *MethodCollection
|
2019-05-09 12:25:45 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:03:23 -04:00
|
|
|
type MethodCollection struct {
|
2020-06-24 13:03:53 -04:00
|
|
|
Class, GoClass string
|
|
|
|
Methods []*Method
|
2021-11-16 16:01:03 -05:00
|
|
|
Unavailable map[string]bool
|
2019-05-28 13:03:23 -04:00
|
|
|
}
|
2019-05-27 13:47:59 -04:00
|
|
|
|
2019-05-28 13:03:23 -04:00
|
|
|
func NewMethodCollection(class string) *MethodCollection {
|
|
|
|
ret := &MethodCollection{
|
2019-06-11 12:38:22 -04:00
|
|
|
Class: class,
|
2019-06-18 09:15:46 -04:00
|
|
|
GoClass: strings.Title(class),
|
2019-05-28 13:03:23 -04:00
|
|
|
Methods: []*Method{},
|
2021-11-16 16:01:03 -05:00
|
|
|
Unavailable: make(map[string]bool),
|
2019-05-28 13:03:23 -04:00
|
|
|
}
|
|
|
|
return ret
|
2019-05-27 13:47:59 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:03:23 -04:00
|
|
|
type ByParams []*Method
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func (a ByParams) Len() int { return len(a) }
|
|
|
|
func (a ByParams) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
2019-05-28 13:03:23 -04:00
|
|
|
func (a ByParams) Less(i, j int) bool { return len(a[i].Parameters) < len(a[j].Parameters) }
|
|
|
|
|
2019-05-28 20:04:39 -04:00
|
|
|
//Disambiguate overloaded method names
|
2019-05-28 13:03:23 -04:00
|
|
|
func Disambiguate(mc *MethodCollection) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
|
|
|
if mc.Class == dbclass {
|
|
|
|
prt = true
|
|
|
|
fmt.Printf("Disambiguate(%s)\n", mc.Class)
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
lst := map[string][]*Method{}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, m := range mc.Methods {
|
2019-05-28 13:03:23 -04:00
|
|
|
lst2 := lst[m.Name]
|
|
|
|
if lst2 == nil {
|
|
|
|
lst2 = []*Method{m}
|
2019-06-11 12:38:22 -04:00
|
|
|
} else {
|
|
|
|
lst2 = append(lst2, m)
|
2019-05-28 13:03:23 -04:00
|
|
|
}
|
|
|
|
lst[m.Name] = lst2
|
|
|
|
}
|
|
|
|
mc.Methods = []*Method{}
|
|
|
|
used := map[string]bool{}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, v := range lst {
|
2019-05-28 13:03:23 -04:00
|
|
|
sort.Sort(ByParams(v))
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, m := range v {
|
2019-05-28 13:03:23 -04:00
|
|
|
if len(v) < 2 || len(m.Parameters) < 2 {
|
2019-06-04 21:18:06 -04:00
|
|
|
if !used[m.Name] {
|
2019-06-11 12:38:22 -04:00
|
|
|
mc.Methods = append(mc.Methods, m)
|
2019-06-04 21:18:06 -04:00
|
|
|
used[m.Name] = true
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
i := 2
|
|
|
|
pname := m.Name + strings.Title(m.Parameters[1].Pname)
|
|
|
|
for ; used[pname] && i < len(m.Parameters); i++ {
|
|
|
|
pname = pname + strings.Title(m.Parameters[i].Pname)
|
|
|
|
}
|
|
|
|
used[pname] = true
|
|
|
|
m.GoName = strings.Title(pname)
|
2021-11-16 16:01:03 -05:00
|
|
|
if prt {
|
|
|
|
fmt.Printf(" -> %s(%s)\n", m.Name, pname)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
mc.Methods = append(mc.Methods, m)
|
2019-05-28 13:03:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 15:08:44 -04:00
|
|
|
//isVoid() returns true if the method has no return value.
|
2019-04-10 14:00:48 -04:00
|
|
|
func (m Method) isVoid() bool {
|
2019-04-26 15:08:44 -04:00
|
|
|
return m.Type.CType() == "void"
|
2019-04-10 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 20:04:39 -04:00
|
|
|
//hasUnsupportedParam() returns true if a method has a function as a parameter.
|
|
|
|
func (m Method) hasUnsupportedParam() bool {
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, p := range m.Parameters {
|
2019-05-03 13:14:30 -04:00
|
|
|
if p.Type.IsFunction() || p.Type.IsFunctionPtr() {
|
2019-04-26 14:08:43 -04:00
|
|
|
return true
|
|
|
|
}
|
2019-05-28 20:04:39 -04:00
|
|
|
if pt := p.Type.PointsTo(); pt.IsValist() {
|
|
|
|
return true
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if p.Type.CGoType() == "C.longdouble" {
|
|
|
|
return true
|
|
|
|
}
|
2019-04-26 14:08:43 -04:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func (w Wrapper) cparamlist(m *Method) (string, string, string) {
|
|
|
|
ns := make([]string, 0)
|
|
|
|
ret := make([]string, 0)
|
2019-05-09 22:05:04 -04:00
|
|
|
tps := []string{"void*"}
|
2019-04-11 17:00:36 -04:00
|
|
|
if !m.ClassMethod {
|
2019-06-11 12:38:22 -04:00
|
|
|
ret = append(ret, "void* o")
|
2019-04-11 17:00:36 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, p := range m.Parameters {
|
2019-04-26 14:08:43 -04:00
|
|
|
var tp string
|
2019-06-27 11:41:58 -04:00
|
|
|
gt := p.Type.GoType()
|
|
|
|
wp := types.ShouldWrap(gt)
|
|
|
|
switch {
|
|
|
|
case len(gt) > 2 && gt[:1] == "*" && types.PtrShouldWrap(gt[1:]):
|
|
|
|
tp = "void**"
|
2020-06-24 13:03:53 -04:00
|
|
|
case len(gt) > 3 && gt[:2] == "**":
|
|
|
|
tp = p.Type.CType()
|
2019-06-27 11:41:58 -04:00
|
|
|
case wp || p.Type.IsPointer() || p.Type.Variadic:
|
2019-04-11 13:09:02 -04:00
|
|
|
tp = "void*"
|
2019-06-27 11:41:58 -04:00
|
|
|
default:
|
2019-04-26 15:08:44 -04:00
|
|
|
tp = p.Type.CType()
|
2019-04-11 13:09:02 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ns = append(ns, p.Vname)
|
|
|
|
ret = append(ret, fmt.Sprintf("%s %s", tp, p.Vname))
|
|
|
|
tps = append(tps, tp)
|
2019-04-10 14:00:48 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
return strings.Join(ns, ", "), strings.Join(ret, ", "), strings.Join(tps, ", ")
|
2019-04-10 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
func (w Wrapper) objcparamlist(m *Method) string {
|
2019-04-10 14:00:48 -04:00
|
|
|
if len(m.Parameters) == 0 {
|
|
|
|
return m.Name
|
|
|
|
}
|
2019-04-11 11:46:24 -04:00
|
|
|
first := true
|
|
|
|
ret := []string{}
|
2019-06-27 11:41:58 -04:00
|
|
|
pname := ""
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, p := range m.Parameters {
|
2019-06-27 11:41:58 -04:00
|
|
|
gt := p.Type.GoType()
|
|
|
|
if first {
|
2019-04-11 11:46:24 -04:00
|
|
|
first = false
|
2019-06-27 11:41:58 -04:00
|
|
|
pname = m.Name
|
2019-04-11 11:46:24 -04:00
|
|
|
} else {
|
2019-06-27 11:41:58 -04:00
|
|
|
pname = p.Pname
|
|
|
|
}
|
|
|
|
switch {
|
|
|
|
case len(gt) > 2 && gt[:1] == "*" && types.PtrShouldWrap(gt[1:]):
|
|
|
|
ret = append(ret, pname+":("+p.Type.Node.CType()+")"+p.Vname)
|
|
|
|
case !p.Type.Variadic:
|
|
|
|
ret = append(ret, pname+":"+p.Vname)
|
|
|
|
first = false
|
|
|
|
case p.Type.Variadic:
|
|
|
|
str := []string{p.Pname + ", arr[0]"}
|
|
|
|
for i := 1; i < w.Vaargs; i++ {
|
|
|
|
str = append(str, "arr["+strconv.Itoa(i)+"]")
|
2019-04-29 16:14:45 -04:00
|
|
|
}
|
2019-06-27 11:41:58 -04:00
|
|
|
str = append(str, "nil")
|
|
|
|
ret = append(ret, strings.Join(str, ", "))
|
2019-04-11 11:46:24 -04:00
|
|
|
}
|
2019-04-10 14:00:48 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
return strings.Join(ret, " ")
|
2019-04-10 14:00:48 -04:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:48:11 -04:00
|
|
|
//goreserved is a map telling whether a word is a go reserved word that is not
|
|
|
|
//also a C/Objective-C reserved word.
|
2019-04-11 11:46:24 -04:00
|
|
|
var goreserved map[string]bool = map[string]bool{
|
|
|
|
"range": true,
|
2019-06-11 12:38:22 -04:00
|
|
|
"type": true,
|
|
|
|
"len": true,
|
2019-04-11 11:46:24 -04:00
|
|
|
}
|
|
|
|
|
2019-06-27 11:41:58 -04:00
|
|
|
func (w *Wrapper) gpntp(m *Method) ([]string, []string, []string, []*types.Type, string) {
|
2019-04-26 09:04:53 -04:00
|
|
|
ns := []string{}
|
2019-06-27 11:41:58 -04:00
|
|
|
pnames := []string{}
|
2019-04-26 09:04:53 -04:00
|
|
|
tps := []*types.Type{}
|
2019-04-26 10:56:00 -04:00
|
|
|
if !m.ClassMethod {
|
2019-06-11 12:38:22 -04:00
|
|
|
ns = append(ns, "o")
|
2019-06-27 11:41:58 -04:00
|
|
|
pnames = append(pnames, m.Name)
|
2019-06-11 12:38:22 -04:00
|
|
|
tps = append(tps, types.NewTypeFromString(m.Class+"*", ""))
|
2019-04-26 10:56:00 -04:00
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
for i, p := range m.Parameters {
|
2019-04-26 09:04:53 -04:00
|
|
|
gname := p.Vname
|
|
|
|
if goreserved[gname] {
|
|
|
|
gname = gname + "_"
|
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
if gname == "" {
|
2020-06-24 13:03:53 -04:00
|
|
|
gname = fmt.Sprintf("p%d", i)
|
2019-06-13 16:47:36 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ns = append(ns, gname)
|
2019-06-27 11:41:58 -04:00
|
|
|
pnames = append(pnames, p.Pname)
|
2019-06-11 12:38:22 -04:00
|
|
|
tps = append(tps, p.Type)
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
2019-05-01 11:01:50 -04:00
|
|
|
w.processTypes(tps)
|
2019-04-26 10:56:00 -04:00
|
|
|
ret := []string{}
|
2019-06-11 12:38:22 -04:00
|
|
|
snames := make([]string, len(ns))
|
2019-04-26 10:56:00 -04:00
|
|
|
i := 0
|
2019-06-11 12:38:22 -04:00
|
|
|
if !m.ClassMethod {
|
|
|
|
i = 1
|
|
|
|
}
|
2019-04-26 10:56:00 -04:00
|
|
|
for ; i < len(ns); i++ {
|
2019-04-26 14:08:43 -04:00
|
|
|
gt := tps[i].GoType()
|
|
|
|
if gt == "*Void" {
|
|
|
|
gt = "unsafe.Pointer"
|
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
if types.PtrIsGoInterface(gt) {
|
|
|
|
gt = gt[1:]
|
|
|
|
}
|
2019-04-29 16:14:45 -04:00
|
|
|
if tps[i].Variadic {
|
|
|
|
gt = "..." + gt
|
|
|
|
ns[i] = ns[i] + "s"
|
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
if len(gt) > 2 && gt[:1] == "*" && types.PtrShouldWrap(gt[1:]) {
|
2020-06-24 13:03:53 -04:00
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
x := gt[1:]
|
|
|
|
if types.PtrIsGoInterface(x) {
|
|
|
|
x = "*Id"
|
2019-05-28 15:52:16 -04:00
|
|
|
}
|
|
|
|
gt = "*[]" + x
|
|
|
|
snames[i] = "goSlice" + strconv.Itoa(i)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if len(gt) > 3 && gt[:2] == "**" {
|
|
|
|
gt = "[]" + gt[1:]
|
|
|
|
snames[i] = ns[i]
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ret = append(ret, ns[i]+" "+gt)
|
2019-04-26 10:56:00 -04:00
|
|
|
}
|
2019-06-27 11:41:58 -04:00
|
|
|
return ns, pnames, snames, tps, strings.Join(ret, ", ")
|
2019-04-26 09:04:53 -04:00
|
|
|
}
|
|
|
|
|
2019-04-09 23:19:49 -04:00
|
|
|
type Interface struct {
|
2019-06-11 12:38:22 -04:00
|
|
|
Name, GoName string
|
2019-05-28 13:03:23 -04:00
|
|
|
InstanceMethods, ClassMethods *MethodCollection
|
2019-06-11 12:38:22 -04:00
|
|
|
Properties map[string]*Property
|
|
|
|
Protocols []string // Protocols impelemented by this Interface
|
|
|
|
ProcessedInstanceMethods map[string]bool
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 00:22:07 -04:00
|
|
|
//AddInterface adds an Objective-C interface to a Wrapper.
|
2019-04-09 23:19:49 -04:00
|
|
|
func (w *Wrapper) AddInterface(n *ast.ObjCInterfaceDecl) {
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("ast.ObjCInterfaceDecl: %s\n", n.Name)
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
w.addIntCat(n.Name, n.Children())
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-06 00:22:07 -04:00
|
|
|
//AddCategory adds an Objective-C category to a Wrapper.
|
|
|
|
//the first child node of an ObjCCategoryDecl is always an ObjCInterface
|
|
|
|
//indicating which interface is being extended by this category.
|
2019-04-09 23:19:49 -04:00
|
|
|
func (w *Wrapper) AddCategory(n *ast.ObjCCategoryDecl) {
|
|
|
|
ns := n.Children()
|
2019-04-23 15:48:11 -04:00
|
|
|
if len(ns) > 0 {
|
|
|
|
switch x := ns[0].(type) {
|
|
|
|
case *ast.ObjCInterface:
|
2019-06-11 16:41:21 -04:00
|
|
|
//fmt.Printf("AddCategory(%s) -> %s\n",n.Name, x.Name)
|
2019-06-06 00:22:07 -04:00
|
|
|
w.addIntCat(x.Name, ns[1:])
|
2019-04-23 15:48:11 -04:00
|
|
|
return
|
|
|
|
}
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Not adding methods for %s: interface name not found in first child node of category defclaration\n", n.Name)
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
func (w *Wrapper) AddFunction(n *ast.FunctionDecl) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
2019-05-01 10:58:29 -04:00
|
|
|
//treat functions as class methods with no class
|
2019-06-11 12:38:22 -04:00
|
|
|
tp := types.NewTypeFromString(n.Type, "")
|
2019-05-01 10:58:29 -04:00
|
|
|
m := &Method{
|
2019-06-11 12:38:22 -04:00
|
|
|
Name: n.Name,
|
|
|
|
GoName: strings.Title(n.Name),
|
|
|
|
Type: tp.ReturnType(),
|
|
|
|
Class: "",
|
2019-05-01 10:58:29 -04:00
|
|
|
ClassMethod: true,
|
2019-06-11 12:38:22 -04:00
|
|
|
Parameters: []*Parameter{},
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
f := tp.Node.Children[len(tp.Node.Children)-1] // Function node
|
|
|
|
if f.Kind != "Function" {
|
|
|
|
//fmt.Printf("AddFunction(%s): not a function -- Node type is %s\n%s",n.Name,f.Kind,tp.String())
|
|
|
|
return
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("FunctionDecl: %s (%s) %s\n", n.Type, m.Type.CType(), n.Name)
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
i := 0
|
2020-06-24 13:03:53 -04:00
|
|
|
a := (*Avail)(&[]AvailAttr{})
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, c := range n.Children() {
|
2019-05-01 10:58:29 -04:00
|
|
|
switch x := c.(type) {
|
|
|
|
case *ast.ParmVarDecl:
|
2020-06-24 13:03:53 -04:00
|
|
|
if x.Type == "va_list" {
|
|
|
|
return // skip functions taking a va_list
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
p := &Parameter{
|
|
|
|
Vname: x.Name,
|
2019-06-11 12:38:22 -04:00
|
|
|
Type: types.NewTypeFromString(x.Type, ""),
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
m.Parameters = append(m.Parameters, p)
|
2019-05-01 10:58:29 -04:00
|
|
|
i++
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" %s\n", p.Type.CType())
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
case *ast.FormatAttr:
|
|
|
|
return // skip C variadic functions
|
2019-05-28 20:04:39 -04:00
|
|
|
case *ast.Variadic:
|
|
|
|
p := &Parameter{
|
|
|
|
Vname: "object",
|
2019-06-11 12:38:22 -04:00
|
|
|
Type: types.NewTypeFromString("NSObject*", ""),
|
2019-05-28 20:04:39 -04:00
|
|
|
}
|
|
|
|
p.Type.Variadic = true
|
2019-06-11 12:38:22 -04:00
|
|
|
m.Parameters = append(m.Parameters, p)
|
2019-05-28 20:04:39 -04:00
|
|
|
i++
|
2020-06-24 13:03:53 -04:00
|
|
|
case *ast.AvailabilityAttr, *ast.UnavailableAttr, *ast.DeprecatedAttr:
|
2021-11-16 16:01:03 -05:00
|
|
|
a.Add(x, prt)
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if a.Available() {
|
|
|
|
w.Functions[n.Name] = m
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 12:25:45 -04:00
|
|
|
func (w *Wrapper) AddProtocol(n *ast.ObjCProtocolDecl) {
|
2019-05-09 22:05:04 -04:00
|
|
|
p := w.Protocols[n.Name]
|
|
|
|
if p == nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Adding protocol %s\n", n.Name)
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
p = &Protocol{
|
2019-06-11 12:38:22 -04:00
|
|
|
ClassMethods: NewMethodCollection(n.Name),
|
2019-06-06 00:22:07 -04:00
|
|
|
InstanceMethods: NewMethodCollection(n.Name),
|
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Protocol %s\n", n.Name)
|
|
|
|
}
|
|
|
|
for _, c := range n.Children() {
|
2019-05-09 12:25:45 -04:00
|
|
|
switch x := c.(type) {
|
|
|
|
case *ast.ObjCMethodDecl:
|
2019-05-31 12:42:52 -04:00
|
|
|
if Arc {
|
|
|
|
switch x.Name {
|
2019-06-11 12:38:22 -04:00
|
|
|
case "retain", "release", "autorelease":
|
2019-05-31 12:42:52 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
if x.ClassMethod {
|
2019-06-11 12:38:22 -04:00
|
|
|
w.AddMethod(p.ClassMethods, x)
|
2019-05-28 13:03:23 -04:00
|
|
|
} else {
|
2019-06-11 12:38:22 -04:00
|
|
|
w.AddMethod(p.InstanceMethods, x)
|
2019-05-28 13:03:23 -04:00
|
|
|
}
|
2019-05-27 13:47:59 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
Disambiguate(p.InstanceMethods)
|
|
|
|
Disambiguate(p.ClassMethods)
|
2019-05-27 13:47:59 -04:00
|
|
|
w.Protocols[n.Name] = p
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:03:23 -04:00
|
|
|
func (w *Wrapper) AddMethod(p *MethodCollection, x *ast.ObjCMethodDecl) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
|
|
|
if p.Class == dbclass {
|
|
|
|
prt = true
|
|
|
|
fmt.Printf("%s -> adding %s\n", dbclass, x.Name)
|
|
|
|
}
|
|
|
|
tp := types.NewTypeFromString(x.Type, p.Class)
|
|
|
|
if tp.String() == "" {
|
|
|
|
fmt.Printf("AddMethod(%s): type is empty.\n", x.Name)
|
|
|
|
fmt.Printf("x.Type = %s\n", x.Type)
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
m := &Method{
|
2019-06-11 12:38:22 -04:00
|
|
|
Name: x.Name,
|
|
|
|
GoName: strings.Title(x.Name),
|
|
|
|
Type: types.NewTypeFromString(x.Type, p.Class),
|
|
|
|
Class: p.Class,
|
|
|
|
GoClass: strings.Title(p.Class),
|
2019-05-28 13:03:23 -04:00
|
|
|
ClassMethod: x.ClassMethod,
|
2019-06-11 16:41:21 -04:00
|
|
|
Unavailable: false,
|
2019-05-28 13:03:23 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
2021-11-16 16:01:03 -05:00
|
|
|
fmt.Printf(" -- Method %s type %s\n", m.Name, m.Type.CType())
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
var avail bool
|
2021-11-16 16:01:03 -05:00
|
|
|
m.Parameters, avail, m.Unavailable = w.GetParms(x, p.Class, prt)
|
2019-05-28 13:03:23 -04:00
|
|
|
if avail {
|
2021-11-16 16:01:03 -05:00
|
|
|
if Debug || prt {
|
|
|
|
fmt.Printf("%s: Adding %s (%d) type %s\n", p.Class, m.Name, len(m.Parameters), m.Type.GoType())
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
// p.Methods = append(p.Methods, m)
|
2019-06-06 00:22:07 -04:00
|
|
|
} else {
|
2021-11-16 16:01:03 -05:00
|
|
|
m.Unavailable = true
|
|
|
|
p.Unavailable[m.LongName()] = true
|
|
|
|
if m.Name == "init" {
|
|
|
|
y := w.Interfaces[p.Class]
|
|
|
|
if y != nil {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" Marking new method as unavailable for %s\n", p.Class)
|
|
|
|
}
|
|
|
|
y.ClassMethods.Unavailable["New"] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if Debug || prt {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("--Method %s is not available\n", m.Name)
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
p.Methods = append(p.Methods, m)
|
2019-05-09 12:25:45 -04:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:14:29 -04:00
|
|
|
//FIXME: copied from nswrap/main.go, should put this in a utils package
|
|
|
|
func matches(x string, rs []string) bool {
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, r := range rs {
|
|
|
|
if m, _ := regexp.MatchString("^"+r+"$", x); m {
|
2019-05-02 14:14:29 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func (w *Wrapper) AddEnum(n *ast.EnumDecl, rs []string) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
2019-06-11 12:38:22 -04:00
|
|
|
if n.Name != "" && !matches(n.Name, rs) {
|
2019-05-02 14:14:29 -04:00
|
|
|
return
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Adding enum: (%s) %s\n", n.Type, n.Name)
|
|
|
|
}
|
2019-05-02 14:14:29 -04:00
|
|
|
var tp *types.Type
|
|
|
|
a := (*Avail)(&[]AvailAttr{})
|
|
|
|
if n.Type == "" {
|
|
|
|
tp = nil
|
|
|
|
} else {
|
2019-06-11 12:38:22 -04:00
|
|
|
tp = types.NewTypeFromString(n.Type, "")
|
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" type: %s -> %s\n", n.Type, tp.CType())
|
|
|
|
}
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
e := &Enum{
|
2019-06-11 12:38:22 -04:00
|
|
|
Name: n.Name, // NOTE: may be empty string
|
|
|
|
Type: tp,
|
2020-06-24 13:03:53 -04:00
|
|
|
Constants: []EnumConstant{},
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, c := range n.Children() {
|
2019-05-02 14:14:29 -04:00
|
|
|
switch x := c.(type) {
|
2019-05-10 02:23:07 -04:00
|
|
|
case *ast.AvailabilityAttr, *ast.UnavailableAttr, *ast.DeprecatedAttr:
|
2021-11-16 16:01:03 -05:00
|
|
|
a.Add(x, prt)
|
2019-05-02 14:14:29 -04:00
|
|
|
case *ast.EnumConstantDecl:
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("*ast.EnumConstantDecl: (%s) '%s': %s\n", n.Type, n.Name, x.Name)
|
|
|
|
}
|
|
|
|
if n.Name == "" && !matches(x.Name, rs) {
|
2019-05-02 14:14:29 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
tp := types.NewTypeFromString(x.Type, "")
|
|
|
|
e.Constants = append(e.Constants,
|
|
|
|
EnumConstant{name: x.Name, tp: tp})
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if a.Available() && len(e.Constants) > 0 {
|
|
|
|
if e.Name == "" {
|
2019-06-11 12:38:22 -04:00
|
|
|
w.AnonEnums = append(w.AnonEnums, e)
|
2019-05-02 14:14:29 -04:00
|
|
|
} else {
|
|
|
|
w.NamedEnums[e.Name] = e
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" added\n")
|
|
|
|
}
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 13:03:23 -04:00
|
|
|
//Add an Interface or add a Category to an Interface
|
2019-06-06 00:22:07 -04:00
|
|
|
func (w *Wrapper) addIntCat(name string, ns []ast.Node) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
|
|
|
if name == dbclass {
|
|
|
|
prt = true
|
|
|
|
}
|
|
|
|
if Debug || prt {
|
|
|
|
fmt.Printf("addIntCat(%s)\n", name)
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
var i *Interface
|
2019-04-09 23:19:49 -04:00
|
|
|
var ok bool
|
2019-06-18 09:15:46 -04:00
|
|
|
goname := strings.Title(name)
|
2019-05-02 14:14:29 -04:00
|
|
|
types.Wrap(goname)
|
2019-06-11 12:38:22 -04:00
|
|
|
if i, ok = w.Interfaces[name]; !ok {
|
|
|
|
i = &Interface{}
|
2019-05-28 13:03:23 -04:00
|
|
|
i.Name = name
|
|
|
|
i.GoName = goname
|
|
|
|
i.InstanceMethods = NewMethodCollection(name)
|
|
|
|
i.ClassMethods = NewMethodCollection(name)
|
2019-06-18 09:15:46 -04:00
|
|
|
i.Properties = map[string]*Property{}
|
2019-05-28 13:03:23 -04:00
|
|
|
i.Protocols = []string{}
|
|
|
|
i.ProcessedInstanceMethods = map[string]bool{}
|
2021-11-16 16:01:03 -05:00
|
|
|
w.Interfaces[name] = i
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
avail := (*Avail)(&[]AvailAttr{})
|
|
|
|
mcc := NewMethodCollection(name)
|
|
|
|
mci := NewMethodCollection(name)
|
|
|
|
prots := []string{}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, c := range ns {
|
2019-04-09 11:52:21 -04:00
|
|
|
switch x := c.(type) {
|
2019-04-09 23:19:49 -04:00
|
|
|
case *ast.ObjCPropertyDecl:
|
2019-06-18 09:15:46 -04:00
|
|
|
p := &Property{
|
|
|
|
Name: x.Name,
|
2020-06-24 13:03:53 -04:00
|
|
|
Type: types.NewTypeFromString(x.Type, name),
|
2019-06-18 09:15:46 -04:00
|
|
|
Attr: x.Attr,
|
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("ObjCPropertyDecl: %s %s (%s)\n", x.Type, x.Name, p.Type.GoType())
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
i.Properties[x.Name] = p
|
2019-04-09 11:52:21 -04:00
|
|
|
case *ast.ObjCMethodDecl:
|
2021-11-16 16:01:03 -05:00
|
|
|
if Debug || prt {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("ObjCMethodDecl: %s (%s) %s\n", x.Type, name, x.Name)
|
2021-11-16 16:01:03 -05:00
|
|
|
fmt.Printf(" type = %s\n", x.Type)
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-10 02:23:07 -04:00
|
|
|
if name == "NSObject" && x.Name == "initialize" {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
if x.ClassMethod {
|
2019-06-11 12:38:22 -04:00
|
|
|
w.AddMethod(mcc, x)
|
2019-05-28 13:03:23 -04:00
|
|
|
} else {
|
2019-06-11 12:38:22 -04:00
|
|
|
w.AddMethod(mci, x)
|
2019-04-11 11:46:24 -04:00
|
|
|
}
|
2019-04-09 23:19:49 -04:00
|
|
|
case *ast.ObjCProtocol:
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("ast.ObjCProtocol: %s\n", x.Name)
|
|
|
|
}
|
|
|
|
prots = append(prots, x.Name)
|
2019-04-09 23:19:49 -04:00
|
|
|
case *ast.ObjCInterface:
|
2019-04-10 14:00:48 -04:00
|
|
|
if x.Super {
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("ast.ObjCInterface: %s inherits from %s\n", name, x.Name)
|
|
|
|
}
|
|
|
|
types.SetSuper(name, x.Name)
|
2019-04-10 14:00:48 -04:00
|
|
|
}
|
2019-04-26 22:44:30 -04:00
|
|
|
case *ast.ObjCTypeParamDecl:
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("ObjCTypeParamDecl: %s = %s\n", x.Name, x.Type)
|
|
|
|
}
|
|
|
|
types.SetTypeParam(name, x.Name, x.Type)
|
2019-06-06 00:22:07 -04:00
|
|
|
case *ast.AvailabilityAttr, *ast.UnavailableAttr, *ast.DeprecatedAttr:
|
2021-11-16 16:01:03 -05:00
|
|
|
avail.Add(x, prt)
|
2019-04-09 23:19:49 -04:00
|
|
|
case *ast.Unknown:
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("(*ast.Unkonwn %s: %s)\n", x.Name, x.Content)
|
|
|
|
}
|
|
|
|
case *ast.ObjCRootClassAttr, *ast.VisibilityAttr,
|
2019-06-06 00:22:07 -04:00
|
|
|
*ast.ObjCIvarDecl, *ast.ArcWeakrefUnavailableAttr,
|
2021-11-16 16:01:03 -05:00
|
|
|
*ast.ObjCExceptionAttr, *ast.SwiftNameAttr,
|
|
|
|
*ast.FullComment:
|
2019-04-09 11:52:21 -04:00
|
|
|
default:
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("AST parse error: node type is %s\n", reflect.TypeOf(x).String())
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
if !avail.Available() {
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("-- %s is not available\n", i.Name)
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
i.ClassMethods.Methods = append(i.ClassMethods.Methods, mcc.Methods...)
|
2021-11-16 16:01:03 -05:00
|
|
|
for k,v := range mcc.Unavailable {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" class method %s: %t\n", k, v)
|
|
|
|
}
|
|
|
|
i.ClassMethods.Unavailable[k] = v
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
i.InstanceMethods.Methods = append(i.InstanceMethods.Methods, mci.Methods...)
|
2021-11-16 16:01:03 -05:00
|
|
|
for k,v := range mci.Unavailable {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" instance method %s: %t\n", k, v)
|
|
|
|
}
|
|
|
|
i.InstanceMethods.Unavailable[k] = v
|
|
|
|
if k == "init" {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" Marking new method as unavailable for %s\n", i.Name)
|
|
|
|
}
|
|
|
|
i.ClassMethods.Unavailable["New"] = true
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
i.Protocols = append(i.Protocols, prots...)
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Println("Add interface ", i.Name)
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
//fmt.Printf("Interface = %s %d class methods, %d instance methods\n", i.Name, len(i.ClassMethods.Methods), len(i.InstanceMethods.Methods))
|
2019-05-28 13:03:23 -04:00
|
|
|
Disambiguate(i.ClassMethods)
|
|
|
|
Disambiguate(i.InstanceMethods)
|
2019-06-11 16:41:21 -04:00
|
|
|
//fmt.Printf("Interface = %s (disambiguated) %d class methods, %d instance methods\n", i.Name, len(i.ClassMethods.Methods), len(i.InstanceMethods.Methods))
|
2019-04-09 23:19:49 -04:00
|
|
|
w.Interfaces[i.Name] = i
|
|
|
|
}
|
|
|
|
|
2019-04-11 11:46:24 -04:00
|
|
|
type AvailAttr struct {
|
|
|
|
OS, Version string
|
2019-06-11 12:38:22 -04:00
|
|
|
Deprecated bool
|
2019-04-11 11:46:24 -04:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:14:29 -04:00
|
|
|
type Avail []AvailAttr
|
|
|
|
|
2021-11-16 16:01:03 -05:00
|
|
|
func (a *Avail) Add(n ast.Node, prt bool) {
|
2019-05-02 14:14:29 -04:00
|
|
|
switch x := n.(type) {
|
|
|
|
case *ast.AvailabilityAttr:
|
2021-11-16 16:01:03 -05:00
|
|
|
if Debug || prt {
|
|
|
|
fmt.Printf(" AvailabilityAttr: OS: %s, Version: %s, Deprecated: %t\n", x.OS, x.Version, x.OS == "macos" && ((x.Unknown1 != "0") || x.IsUnavailable))
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
|
|
|
if x.OS != "macos" {
|
|
|
|
return
|
|
|
|
}
|
2019-05-02 14:14:29 -04:00
|
|
|
*a = append(*a, AvailAttr{
|
2019-06-11 12:38:22 -04:00
|
|
|
OS: x.OS,
|
|
|
|
Version: x.Version,
|
2019-05-02 14:14:29 -04:00
|
|
|
Deprecated: (x.Unknown1 != "0") || x.IsUnavailable,
|
|
|
|
})
|
2019-05-10 02:23:07 -04:00
|
|
|
case *ast.UnavailableAttr, *ast.DeprecatedAttr:
|
2021-11-16 16:01:03 -05:00
|
|
|
if Debug || prt {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf(" DeprecatedAttr\n")
|
|
|
|
}
|
2019-05-02 14:14:29 -04:00
|
|
|
*a = append(*a, AvailAttr{
|
2019-06-11 12:38:22 -04:00
|
|
|
OS: "macos", Deprecated: true})
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Avail) Available() bool {
|
|
|
|
if len(*a) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, x := range *a {
|
2019-05-02 14:14:29 -04:00
|
|
|
if x.OS == "macos" && x.Deprecated == false {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-23 15:48:11 -04:00
|
|
|
//GetParms returns the parameters of a method declaration and a bool
|
|
|
|
//indicating whether the given method is available on MacOS and not
|
|
|
|
//deprecated.
|
2021-11-16 16:01:03 -05:00
|
|
|
func (w *Wrapper) GetParms(n ast.Node, class string, prt bool) ([]*Parameter, bool, bool) {
|
2019-06-11 12:38:22 -04:00
|
|
|
ret := make([]*Parameter, 0)
|
2019-06-11 16:41:21 -04:00
|
|
|
unavail := false
|
2021-11-16 16:01:03 -05:00
|
|
|
variadic := false
|
2019-05-02 14:14:29 -04:00
|
|
|
avail := (*Avail)(&[]AvailAttr{})
|
2019-05-01 10:58:29 -04:00
|
|
|
var parms []string
|
|
|
|
switch x := n.(type) {
|
|
|
|
case *ast.ObjCMethodDecl:
|
|
|
|
parms = x.Parameters
|
2021-11-16 16:01:03 -05:00
|
|
|
for _,z := range x.Attr {
|
|
|
|
if z == "variadic" {
|
|
|
|
variadic = true
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
case *ast.FunctionDecl:
|
|
|
|
default:
|
|
|
|
panic("GetParms called with wrong node type")
|
|
|
|
}
|
2019-04-11 11:46:24 -04:00
|
|
|
j := 0
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, c := range n.Children() {
|
2019-04-09 23:19:49 -04:00
|
|
|
switch x := c.(type) {
|
|
|
|
case *ast.ParmVarDecl:
|
2019-05-01 10:58:29 -04:00
|
|
|
p := &Parameter{
|
2019-04-11 11:46:24 -04:00
|
|
|
Vname: x.Name,
|
2019-06-11 12:38:22 -04:00
|
|
|
Type: types.NewTypeFromString(x.Type, class),
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
if parms != nil {
|
|
|
|
p.Pname = parms[j]
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ret = append(ret, p)
|
2019-04-11 11:46:24 -04:00
|
|
|
j++
|
2019-04-29 16:14:45 -04:00
|
|
|
case *ast.Variadic:
|
2021-11-16 16:01:03 -05:00
|
|
|
variadic = true
|
2019-05-28 20:04:39 -04:00
|
|
|
j++
|
2019-06-11 16:41:21 -04:00
|
|
|
case *ast.UnavailableAttr:
|
2021-11-16 16:01:03 -05:00
|
|
|
if prt {
|
|
|
|
fmt.Printf(" UnavailableAttr\n")
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
unavail = true
|
|
|
|
case *ast.AvailabilityAttr, *ast.DeprecatedAttr:
|
2021-11-16 16:01:03 -05:00
|
|
|
avail.Add(x, prt)
|
2019-04-11 11:46:24 -04:00
|
|
|
case *ast.Unknown:
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("GetParms(): ast.Unknown: %s\n", x.Name)
|
|
|
|
}
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if variadic {
|
|
|
|
p := &Parameter{
|
|
|
|
Vname: "object",
|
|
|
|
Type: types.NewTypeFromString("NSObject*", ""),
|
|
|
|
}
|
|
|
|
p.Type.Variadic = true
|
|
|
|
ret = append(ret, p)
|
|
|
|
}
|
|
|
|
|
2019-04-18 09:38:46 -04:00
|
|
|
// check that the method is available for this OS and not deprecated
|
2021-11-16 16:01:03 -05:00
|
|
|
if unavail || !avail.Available() {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" it's unavailable\n")
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
return nil, false, unavail
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
return ret, true, unavail
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func (w *Wrapper) AddTypedef(n, t string) {
|
|
|
|
tp := types.NewTypeFromString(t, "")
|
2019-05-23 16:31:47 -04:00
|
|
|
gt := tp.GoType()
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Typedef %s -> %s\n", n, t)
|
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
if types.ShouldWrap(gt) {
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
2019-06-13 09:25:53 -04:00
|
|
|
fmt.Printf(" processing wrapped type for %s (%s)\n", n, gt)
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
types.Wrap(n)
|
2019-06-11 12:38:22 -04:00
|
|
|
types.SetSuper(n, gt)
|
2019-06-13 09:25:53 -04:00
|
|
|
w._processType(tp)
|
2019-05-23 16:31:47 -04:00
|
|
|
} else {
|
2019-06-13 09:25:53 -04:00
|
|
|
cgt := tp.CGoType()
|
2020-06-25 16:56:55 -04:00
|
|
|
if Debug {
|
2019-06-13 09:25:53 -04:00
|
|
|
fmt.Printf(" processing un-wrapped type for %s -> %s\n", n, cgt)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
types.AddTypedef(n, tp)
|
2020-06-24 13:03:53 -04:00
|
|
|
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 15:08:44 -04:00
|
|
|
func (w *Wrapper) processTypes(tps []*types.Type) {
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, tp := range tps {
|
2019-05-06 13:21:36 -04:00
|
|
|
w.processType(tp)
|
2019-04-26 10:07:34 -04:00
|
|
|
}
|
2019-04-26 14:08:43 -04:00
|
|
|
}
|
|
|
|
|
2019-04-26 15:08:44 -04:00
|
|
|
func (w *Wrapper) processType(tp *types.Type) {
|
2019-04-29 10:37:43 -04:00
|
|
|
bt := tp.BaseType()
|
2019-06-13 09:25:53 -04:00
|
|
|
w._processType(bt)
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
//func (w *Wrapper) _processType(bt *types.Type, gt string) {
|
|
|
|
func (w *Wrapper) _processType(bt *types.Type) {
|
|
|
|
gt := bt.GoType()
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("processType: gt = %s bt = %s\n", gt, bt)
|
|
|
|
}
|
2019-04-27 22:24:05 -04:00
|
|
|
if gt == "" {
|
2019-04-26 10:07:34 -04:00
|
|
|
return
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if gt == "LongDouble" { // not supported by cgo
|
|
|
|
return
|
|
|
|
}
|
2019-04-27 22:24:05 -04:00
|
|
|
if gt[0] == '*' {
|
2019-04-29 10:37:43 -04:00
|
|
|
w.processType(bt.PointsTo())
|
2019-04-27 22:24:05 -04:00
|
|
|
return
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if w.ProcessedTypes[gt] {
|
2020-06-24 13:03:53 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" -- already seen\n")
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
return
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" -- not yet seen\n")
|
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
w.ProcessedTypes[gt] = true
|
2019-04-27 22:24:05 -04:00
|
|
|
if gt == "Char" {
|
2019-04-26 14:08:43 -04:00
|
|
|
w.CharHelpers()
|
|
|
|
}
|
2019-05-07 15:17:23 -04:00
|
|
|
if gt == "SEL" {
|
|
|
|
w.SelectorHelpers()
|
|
|
|
}
|
2019-05-09 13:33:28 -04:00
|
|
|
if gt == "NSAutoreleasePool" {
|
|
|
|
w.AutoreleaseHelpers()
|
|
|
|
}
|
2019-05-03 13:14:30 -04:00
|
|
|
if bt.IsFunction() || bt.IsFunctionPtr() {
|
2019-04-27 22:24:05 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
super := types.Super(gt)
|
2019-04-26 14:08:43 -04:00
|
|
|
if super != "" {
|
2019-06-11 12:38:22 -04:00
|
|
|
tp := types.NewTypeFromString(super, "")
|
2019-05-02 14:14:29 -04:00
|
|
|
types.Wrap(tp.GoType())
|
|
|
|
w.processType(tp)
|
2019-04-26 14:08:43 -04:00
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
if Debug {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("Writing go type for %s -> %s\n", bt.CType(), gt)
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
w.goTypes.WriteString(bt.GoTypeDecl(Gogc))
|
2019-04-26 14:08:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wrapper) CharHelpers() {
|
|
|
|
w.goHelpers.WriteString(`
|
2019-05-03 13:14:30 -04:00
|
|
|
func CharWithGoString(s string) *Char {
|
2019-04-26 14:08:43 -04:00
|
|
|
return (*Char)(unsafe.Pointer(C.CString(s)))
|
|
|
|
}
|
|
|
|
|
2019-05-23 16:31:47 -04:00
|
|
|
func CharWithBytes(b []byte) *Char {
|
2019-04-26 14:08:43 -04:00
|
|
|
return (*Char)(unsafe.Pointer(C.CString(string(b))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Char) String() string {
|
|
|
|
return C.GoString((*C.char)(c))
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
|
|
|
|
func (c *Char) Free() {
|
|
|
|
C.free(unsafe.Pointer(c))
|
|
|
|
}
|
2019-04-26 14:08:43 -04:00
|
|
|
`)
|
2019-04-26 10:07:34 -04:00
|
|
|
}
|
|
|
|
|
2019-05-24 15:40:01 -04:00
|
|
|
func (w *Wrapper) StringHelpers() {
|
2019-06-27 11:41:58 -04:00
|
|
|
ufree := ""
|
|
|
|
if Gogc {
|
2020-06-24 13:03:53 -04:00
|
|
|
w.goImports["runtime"] = true
|
|
|
|
ufree = "utf8.Free()\n\truntime.KeepAlive(o)\n\t"
|
2019-06-27 11:41:58 -04:00
|
|
|
}
|
|
|
|
w.goHelpers.WriteString(fmt.Sprintf(`
|
2019-06-13 09:25:53 -04:00
|
|
|
func (o *NSString) String() string {
|
2019-06-27 11:41:58 -04:00
|
|
|
utf8 := o.UTF8String()
|
|
|
|
ret := utf8.String()
|
|
|
|
%sreturn ret
|
2019-05-24 15:40:01 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
`, ufree))
|
2019-05-24 15:40:01 -04:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:14:29 -04:00
|
|
|
func (w *Wrapper) EnumeratorHelpers() {
|
2019-06-18 09:15:46 -04:00
|
|
|
var re1, re2 string
|
|
|
|
if Gogc && false { // FIXME: don't need this
|
|
|
|
re1 = "o.Release(); "
|
|
|
|
re2 = `
|
|
|
|
o.Release()`
|
|
|
|
}
|
|
|
|
w.goHelpers.WriteString(fmt.Sprintf(`
|
2019-06-13 09:25:53 -04:00
|
|
|
func (e *NSEnumerator) ForIn(f func(*Id) bool) {
|
2019-05-23 16:31:47 -04:00
|
|
|
for o := e.NextObject(); o.Ptr() != nil; o = e.NextObject() {
|
2019-06-18 09:15:46 -04:00
|
|
|
if !f(o) { %sbreak }%s
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
2019-05-03 13:14:30 -04:00
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
`, re1, re2))
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 13:33:28 -04:00
|
|
|
func (w *Wrapper) AutoreleaseHelpers() {
|
|
|
|
w.goHelpers.WriteString(`
|
2019-05-11 23:03:56 -04:00
|
|
|
func Autoreleasepool(f func()) {
|
2019-05-09 13:33:28 -04:00
|
|
|
pool := NSAutoreleasePoolAlloc().Init()
|
|
|
|
f()
|
|
|
|
pool.Drain()
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2019-05-07 15:17:23 -04:00
|
|
|
func (w *Wrapper) SelectorHelpers() {
|
|
|
|
w.cCode.WriteString(`
|
|
|
|
void*
|
|
|
|
selectorFromString(char *s) {
|
|
|
|
return NSSelectorFromString([NSString stringWithUTF8String:s]);
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
w.goHelpers.WriteString(`
|
|
|
|
func Selector(s string) SEL {
|
|
|
|
return (SEL)(unsafe.Pointer(C.selectorFromString(C.CString(s))))
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
func (w *Wrapper) ProcessMethod(m *Method) {
|
2019-06-11 12:38:22 -04:00
|
|
|
w._processMethod(m, false)
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
|
2019-05-23 16:31:47 -04:00
|
|
|
func (w *Wrapper) ProcessMethodForClass(m *Method, class string) {
|
2019-06-18 09:15:46 -04:00
|
|
|
goclass := strings.Title(class)
|
2019-05-23 16:31:47 -04:00
|
|
|
m2 := &Method{
|
2019-05-28 13:03:23 -04:00
|
|
|
Name: m.Name, GoName: m.GoName, Class: class, GoClass: goclass,
|
2019-06-11 12:38:22 -04:00
|
|
|
Type: m.Type.CloneToClass(class),
|
2019-05-23 16:31:47 -04:00
|
|
|
ClassMethod: m.ClassMethod,
|
2019-06-11 12:38:22 -04:00
|
|
|
Parameters: make([]*Parameter, len(m.Parameters)),
|
2019-06-18 09:15:46 -04:00
|
|
|
Unavailable: m.Unavailable,
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for i, p := range m.Parameters {
|
2019-05-23 16:31:47 -04:00
|
|
|
m2.Parameters[i] = &Parameter{
|
|
|
|
Pname: p.Pname, Vname: p.Vname,
|
|
|
|
Type: p.Type.CloneToClass(class),
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
w._processMethod(m2, false)
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
func (w *Wrapper) ProcessFunction(m *Method) {
|
2019-05-07 14:48:06 -04:00
|
|
|
if m.Type.Node.IsId() {
|
2019-06-11 12:38:22 -04:00
|
|
|
//do not wrap functions that return ID because of CGo struct size bug
|
2019-05-07 14:48:06 -04:00
|
|
|
return
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
w._processMethod(m, true)
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func (w *Wrapper) _processMethod(m *Method, fun bool) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
|
|
|
if m.Class == dbclass && m.Name == "new" {
|
|
|
|
prt = true
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
2021-11-16 16:01:03 -05:00
|
|
|
fmt.Printf(" (%s) method: %s (%s)\n", m.Class, m.Name, m.Type)
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-28 20:04:39 -04:00
|
|
|
if m.HasUnsupportedType() {
|
2019-05-01 10:58:29 -04:00
|
|
|
return
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
if m.Unavailable {
|
2021-11-16 16:01:03 -05:00
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s unavailable\n")
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
return
|
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if x := w.Interfaces[m.Class]; x != nil {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf("processMethod (%s) %s\n", m.Class, m.Name)
|
|
|
|
}
|
|
|
|
if m.ClassMethod {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s class method", m.Name)
|
|
|
|
}
|
|
|
|
y := x.ClassMethods
|
|
|
|
if y != nil {
|
|
|
|
if y.Unavailable[m.LongName()] {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" marked unavailable\n")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s instance method", m.Name)
|
|
|
|
}
|
|
|
|
y := x.InstanceMethods
|
|
|
|
if y != nil {
|
|
|
|
if y.Unavailable[m.LongName()] {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" marked unavailable")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if prt {
|
|
|
|
fmt.Printf("\n")
|
|
|
|
}
|
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
w.processType(m.Type)
|
2019-05-28 13:03:23 -04:00
|
|
|
gname := m.GoName
|
2019-06-11 12:38:22 -04:00
|
|
|
gname = strings.ReplaceAll(gname, "_", " ")
|
2019-05-09 22:33:06 -04:00
|
|
|
gname = strings.Title(gname)
|
2019-06-11 12:38:22 -04:00
|
|
|
gname = strings.ReplaceAll(gname, " ", "")
|
2019-05-09 08:52:35 -04:00
|
|
|
receiver := ""
|
2019-06-19 14:07:33 -04:00
|
|
|
constructor := false // this is an autoreleased object constructor
|
2019-05-28 13:03:23 -04:00
|
|
|
var cname string
|
|
|
|
if fun {
|
|
|
|
cname = m.Name
|
|
|
|
} else {
|
|
|
|
cname = gname
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Method %s (GoClass %s)\n", cname, m.GoClass)
|
|
|
|
}
|
2019-06-19 14:07:33 -04:00
|
|
|
grtype := m.Type.GoType()
|
|
|
|
if grtype == "Void" {
|
|
|
|
grtype = ""
|
|
|
|
}
|
|
|
|
if types.PtrIsGoInterface(grtype) {
|
|
|
|
grtype = "*Id"
|
|
|
|
}
|
|
|
|
if grtype == "BOOL" { // convert objective-c bools to Go bools
|
|
|
|
grtype = "bool"
|
|
|
|
}
|
2019-05-01 12:32:42 -04:00
|
|
|
switch {
|
|
|
|
case !m.ClassMethod:
|
2019-05-09 08:52:35 -04:00
|
|
|
if types.IsGoInterface(m.GoClass) {
|
2019-06-13 09:25:53 -04:00
|
|
|
receiver = "(o *Id) "
|
2019-05-09 08:52:35 -04:00
|
|
|
} else {
|
2019-06-13 09:25:53 -04:00
|
|
|
receiver = "(o *" + m.GoClass + ") "
|
2019-05-09 08:52:35 -04:00
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
//Disambiguate instance methods with same name as a class method
|
|
|
|
cname = "inst_" + cname
|
2019-06-19 14:07:33 -04:00
|
|
|
case m.ClassMethod:
|
2019-05-07 14:48:06 -04:00
|
|
|
//Shorten class method names
|
2019-05-01 12:32:42 -04:00
|
|
|
lens1 := len(m.Class)
|
|
|
|
i := 0
|
2019-06-19 14:07:33 -04:00
|
|
|
if len(gname) < lens1 {
|
2019-06-11 12:38:22 -04:00
|
|
|
i = lens1 - len(gname)
|
|
|
|
}
|
2019-05-01 12:32:42 -04:00
|
|
|
for ; i < lens1; i++ {
|
2019-06-11 12:38:22 -04:00
|
|
|
if m.Class[i:] == gname[:lens1-i] {
|
2019-06-19 14:07:33 -04:00
|
|
|
if Gogc &&
|
2020-06-24 13:03:53 -04:00
|
|
|
(types.PtrShouldWrap(grtype) || grtype == "*Id") {
|
2019-06-19 14:07:33 -04:00
|
|
|
constructor = true
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
break
|
|
|
|
}
|
2019-05-01 12:32:42 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if lens1-i >= len(gname) {
|
2019-05-02 14:14:29 -04:00
|
|
|
gname = m.GoClass + gname
|
2019-05-01 12:32:42 -04:00
|
|
|
} else {
|
2019-05-02 14:14:29 -04:00
|
|
|
gname = m.GoClass + gname[lens1-i:]
|
2019-05-01 12:32:42 -04:00
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
if m.Class != "" {
|
|
|
|
cname = m.Class + "_" + cname
|
|
|
|
}
|
2019-05-07 14:48:06 -04:00
|
|
|
var cmtype string
|
2019-05-23 16:31:47 -04:00
|
|
|
if m.Type.IsPointer() || types.ShouldWrap(m.Type.GoType()) {
|
2019-05-07 14:48:06 -04:00
|
|
|
// work around cgo bugs with struct size calculation
|
|
|
|
cmtype = "void*"
|
|
|
|
if x := m.Type.Node.Qualifiers(); x != "" {
|
|
|
|
cmtype = x + " " + cmtype
|
|
|
|
}
|
|
|
|
if x := m.Type.Node.Annotations(); x != "" {
|
|
|
|
cmtype = cmtype + " " + x
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cmtype = m.Type.CTypeAttrib()
|
|
|
|
}
|
2019-06-27 11:41:58 -04:00
|
|
|
ns, pnames, snames, tps, gplist := w.gpntp(m)
|
2019-05-02 14:14:29 -04:00
|
|
|
if gname == grtype { // avoid name conflicts between methods and types
|
|
|
|
gname = "Get" + gname
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
var inter *Interface
|
2019-05-23 16:31:47 -04:00
|
|
|
if m.ClassMethod {
|
|
|
|
if w.ProcessedClassMethods[gname] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.ProcessedClassMethods[gname] = true
|
|
|
|
} else {
|
2019-06-18 09:15:46 -04:00
|
|
|
var ok bool
|
2020-06-24 13:03:53 -04:00
|
|
|
inter, ok = w.Interfaces[m.Class]
|
2019-05-23 16:31:47 -04:00
|
|
|
if !ok {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Can't find interface %s for method %s\n", m.Class, m.Name)
|
2019-05-23 16:31:47 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
if inter.ProcessedInstanceMethods[gname] {
|
2019-05-23 16:31:47 -04:00
|
|
|
return
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
inter.ProcessedInstanceMethods[gname] = true
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
|
|
|
|
2019-05-01 10:58:29 -04:00
|
|
|
w.goCode.WriteString(fmt.Sprintf(`
|
2019-05-03 13:14:30 -04:00
|
|
|
func %s%s(%s) %s {
|
2019-06-11 12:38:22 -04:00
|
|
|
`, receiver, gname, gplist, grtype))
|
|
|
|
lparm := len(tps) - 1
|
2019-05-01 10:58:29 -04:00
|
|
|
if len(tps) > 0 && tps[lparm].Variadic {
|
|
|
|
vn := ns[lparm]
|
|
|
|
vn = vn[:len(vn)-1]
|
|
|
|
ns[lparm] = vn
|
2020-06-24 13:03:53 -04:00
|
|
|
dotptr := ""
|
|
|
|
if !fun {
|
|
|
|
dotptr = ".Ptr()"
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
w.goCode.WriteString(fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
` var %s [%d]unsafe.Pointer
|
2019-05-01 10:58:29 -04:00
|
|
|
for i,o := range %ss {
|
2020-06-24 13:03:53 -04:00
|
|
|
%s[i] = o%s
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
`, vn, w.Vaargs, vn, vn, dotptr))
|
|
|
|
if fun {
|
|
|
|
cname = "_" + cname
|
|
|
|
}
|
2019-05-28 15:52:16 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for i, n := range ns {
|
2019-05-28 15:52:16 -04:00
|
|
|
if snames[i] == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
gt := tps[i].GoType()
|
|
|
|
if !(len(gt) > 2 && gt[:1] == "*" && types.PtrShouldWrap(gt[1:])) {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-28 15:52:16 -04:00
|
|
|
w.goCode.WriteString(fmt.Sprintf(`
|
2019-05-29 00:02:27 -04:00
|
|
|
%s := make([]unsafe.Pointer,cap(*%s))
|
2019-05-28 15:52:16 -04:00
|
|
|
for i := 0; i < len(*%s); i++ {
|
|
|
|
%s[i] = (*%s)[i].Ptr()
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, snames[i], n, n, snames[i], n))
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
|
|
|
w.goCode.WriteString(` ` +
|
2020-06-24 13:03:53 -04:00
|
|
|
types.GoToC(m.Name, cname, ns, snames, m.Type, tps, fun, constructor || m.ShouldFinalize(), m.ClassMethod, w.goImports) + "\n}\n")
|
2019-05-01 10:58:29 -04:00
|
|
|
|
|
|
|
cret := ""
|
|
|
|
if !m.isVoid() {
|
|
|
|
cret = "return "
|
|
|
|
}
|
|
|
|
var cobj string
|
|
|
|
if m.ClassMethod {
|
|
|
|
cobj = m.Class
|
|
|
|
} else {
|
2019-05-02 14:14:29 -04:00
|
|
|
cobj = "(" + m.Class + "*)o"
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
cns, cntps, _ := w.cparamlist(m)
|
2019-05-01 10:58:29 -04:00
|
|
|
if fun {
|
2020-06-24 13:03:53 -04:00
|
|
|
// return
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if cmtype == "" {
|
|
|
|
fmt.Printf("Method: %s type: '%s'\n", cname, cmtype)
|
|
|
|
fmt.Printf("Node: %s\n", m.Type.Node)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if !fun || len(tps) > 0 && tps[lparm].Variadic {
|
|
|
|
w.cCode.WriteString(fmt.Sprintf(`
|
2019-05-01 10:58:29 -04:00
|
|
|
%s
|
|
|
|
%s(%s) {
|
|
|
|
`, cmtype, cname, cntps))
|
2020-06-24 13:03:53 -04:00
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
if len(tps) > 0 && tps[lparm].Variadic {
|
|
|
|
w.cCode.WriteString(fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
` %s* arr = %s;
|
2019-05-01 10:58:29 -04:00
|
|
|
`, tps[lparm].CType(), ns[lparm]))
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
switch {
|
2020-06-24 13:03:53 -04:00
|
|
|
case fun && len(tps) > 0 && tps[lparm].Variadic:
|
2019-05-01 10:58:29 -04:00
|
|
|
w.cCode.WriteString(fmt.Sprintf(` %s%s(%s);
|
|
|
|
}`, cret, m.Name, cns))
|
2020-06-24 13:03:53 -04:00
|
|
|
case !fun && len(m.Name) >= 5 && m.Name[:5] == "alloc" && m.Class != "NSAutoreleasePool":
|
2019-06-11 12:38:22 -04:00
|
|
|
if Autorelease {
|
|
|
|
w.cCode.WriteString(fmt.Sprintf(` %s[[%s %s] autorelease];
|
|
|
|
}`, cret, cobj, w.objcparamlist(m)))
|
|
|
|
} else {
|
2019-06-06 00:22:07 -04:00
|
|
|
w.cCode.WriteString(fmt.Sprintf(` %s[%s %s];
|
|
|
|
}`, cret, cobj, w.objcparamlist(m)))
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
case !fun:
|
2019-06-27 11:41:58 -04:00
|
|
|
//if Gogc && !m.isVoid() {
|
|
|
|
if Gogc {
|
2019-06-11 12:38:22 -04:00
|
|
|
rtn := ""
|
2019-06-18 09:15:46 -04:00
|
|
|
if types.PtrShouldWrap(m.Type.GoType()) {
|
|
|
|
switch {
|
|
|
|
case m.ClassMethod:
|
2019-06-19 14:07:33 -04:00
|
|
|
if grtype != "*NSAutoreleasePool" && constructor {
|
2020-06-24 13:03:53 -04:00
|
|
|
// retain objects returned by class constructor methods
|
2019-06-18 09:15:46 -04:00
|
|
|
rtn = `
|
2019-06-27 11:41:58 -04:00
|
|
|
if(ret != nil) { [ret retain]; }`
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
|
|
|
|
2019-06-19 14:07:33 -04:00
|
|
|
// do not retain new, alloc, init and copy methods
|
|
|
|
case IsRetained(m.Name):
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
default:
|
2020-06-24 13:03:53 -04:00
|
|
|
// by default, for instance methods, retain
|
|
|
|
// if returning a new object
|
2019-06-18 09:15:46 -04:00
|
|
|
rtn = `
|
2019-06-27 11:41:58 -04:00
|
|
|
if (ret != nil && ret != o) { [ret retain]; }`
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-06-27 11:41:58 -04:00
|
|
|
rtns := []string{}
|
2020-06-24 13:03:53 -04:00
|
|
|
// for pointers to pointers, assume length 1 unless there is a
|
|
|
|
// parameter named "range" or "count".
|
2019-06-27 11:41:58 -04:00
|
|
|
rlength := "i<1"
|
2020-06-24 13:03:53 -04:00
|
|
|
for i, n := range pnames {
|
|
|
|
vn := strings.ReplaceAll(ns[i], "_", "")
|
2019-06-27 11:41:58 -04:00
|
|
|
if n == "range" {
|
|
|
|
rlength = "i<" + vn + ".length"
|
|
|
|
}
|
|
|
|
if n == "count" {
|
|
|
|
rlength = "i<" + vn
|
|
|
|
}
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
for i, n := range ns {
|
2019-06-27 11:41:58 -04:00
|
|
|
if snames[i] == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
rtns = append(rtns, fmt.Sprintf(`
|
2019-06-27 11:41:58 -04:00
|
|
|
for(int i=0;%s;i++) {
|
|
|
|
if(%s[i] == 0) { break; }
|
|
|
|
[(id)%s[i] retain];
|
|
|
|
}
|
|
|
|
`, rlength, n, n))
|
|
|
|
}
|
|
|
|
var retdecl, reteq, retretn, dup1, dup2 string
|
|
|
|
if !m.isVoid() {
|
|
|
|
retdecl = fmt.Sprintf("%s ret;\n\t", m.Type.CTypeAttrib())
|
|
|
|
reteq = "ret = "
|
|
|
|
retretn = "\n\treturn ret;\n"
|
|
|
|
if m.Type.CType() == "char*" {
|
|
|
|
dup1 = "strdup("
|
|
|
|
dup2 = ")"
|
|
|
|
}
|
2019-06-19 14:07:33 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
w.cCode.WriteString(fmt.Sprintf(
|
2020-06-24 13:03:53 -04:00
|
|
|
` %s@autoreleasepool {
|
2019-06-27 11:41:58 -04:00
|
|
|
%s%s[%s %s]%s;%s%s
|
|
|
|
}%s
|
2020-06-24 13:03:53 -04:00
|
|
|
}`, retdecl, reteq, dup1, cobj, w.objcparamlist(m), dup2, rtn, strings.Join(rtns, "\n\t"), retretn))
|
2019-06-11 12:38:22 -04:00
|
|
|
} else {
|
|
|
|
w.cCode.WriteString(fmt.Sprintf(` %s[%s %s];
|
2019-05-01 10:58:29 -04:00
|
|
|
}`, cret, cobj, w.objcparamlist(m)))
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
2019-05-03 13:14:30 -04:00
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
// create SetFinalizer methods when we see an alloc function:
|
|
|
|
if Gogc && m.Name == "alloc" {
|
|
|
|
cls := m.GoClass
|
|
|
|
if types.IsGoInterface(cls) {
|
|
|
|
cls = "Id"
|
|
|
|
}
|
|
|
|
dbg := ""
|
|
|
|
dbg2 := ""
|
|
|
|
if Debug {
|
|
|
|
dbg = fmt.Sprintf(`fmt.Printf("Setting GC finalizer (%s): %%p -> %%p\n", o, o.ptr)
|
2020-06-24 13:03:53 -04:00
|
|
|
`, cls)
|
2019-06-18 17:29:52 -04:00
|
|
|
dbg2 = fmt.Sprintf(`fmt.Printf("GC finalizer (%s): release %%p -> %%p\n", o, o.ptr)
|
|
|
|
`, cls)
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
2019-06-27 11:41:58 -04:00
|
|
|
w.goImports["runtime"] = true
|
2019-06-18 09:15:46 -04:00
|
|
|
w.goCode.WriteString(fmt.Sprintf(`
|
|
|
|
func (o *%s) GC() {
|
|
|
|
if o.ptr == nil { return }
|
|
|
|
%sruntime.SetFinalizer(o, func(o *%s) {
|
|
|
|
%so.Release()
|
|
|
|
})
|
2020-06-24 13:03:53 -04:00
|
|
|
runtime.KeepAlive(o)
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
|
|
|
`, cls, dbg, cls, dbg2))
|
|
|
|
}
|
2019-05-03 13:14:30 -04:00
|
|
|
// create GoString helper method
|
2019-06-11 12:38:22 -04:00
|
|
|
if ok, _ := regexp.MatchString("WithString$", m.Name); ok {
|
|
|
|
if Debug {
|
|
|
|
fmt.Printf("--%s\n", gname)
|
|
|
|
}
|
2019-06-06 00:22:07 -04:00
|
|
|
cvts := ""
|
2019-05-03 13:14:30 -04:00
|
|
|
gname2 := gname[:len(gname)-6] + "GoString"
|
|
|
|
gps := []string{}
|
|
|
|
i := 0
|
2019-06-11 12:38:22 -04:00
|
|
|
if !m.ClassMethod {
|
|
|
|
i = 1
|
|
|
|
}
|
2019-05-03 13:14:30 -04:00
|
|
|
for ; i < len(ns); i++ {
|
|
|
|
gt := tps[i].GoType()
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" %s\n", gt)
|
|
|
|
}
|
2019-05-03 13:14:30 -04:00
|
|
|
ns2 := ns[i]
|
2019-06-13 09:25:53 -04:00
|
|
|
if gt == "*NSString" {
|
2019-05-03 13:14:30 -04:00
|
|
|
gt = "string"
|
2019-06-06 00:22:07 -04:00
|
|
|
//ns[i] = gStringToNsstring(ns[i])
|
2019-06-13 09:25:53 -04:00
|
|
|
cvts = gStringToNSString(ns[i])
|
2019-06-06 00:22:07 -04:00
|
|
|
ns[i] = "NSStringWithUTF8String(" + ns[i] + "_chr)"
|
2019-05-03 13:14:30 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
gps = append(gps, ns2+" "+gt)
|
2019-05-03 13:14:30 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
gplist = strings.Join(gps, ", ")
|
2019-05-03 13:14:30 -04:00
|
|
|
obj := ""
|
|
|
|
if !m.ClassMethod {
|
|
|
|
obj = "o."
|
|
|
|
ns = ns[1:]
|
|
|
|
}
|
|
|
|
w.goCode.WriteString(fmt.Sprintf(`
|
|
|
|
func %s%s(%s) %s {
|
2019-06-13 09:25:53 -04:00
|
|
|
%sret := %s%s(%s)
|
2019-06-18 09:15:46 -04:00
|
|
|
return ret
|
2019-05-03 13:14:30 -04:00
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
`, receiver, gname2, gplist, grtype, cvts, obj, gname, strings.Join(ns, ", ")))
|
2019-05-03 13:14:30 -04:00
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
}
|
2019-04-10 14:00:48 -04:00
|
|
|
|
2019-06-06 00:22:07 -04:00
|
|
|
func gStringToNSString(s string) string {
|
|
|
|
return fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
`%s_chr := CharWithGoString(%s)
|
2019-06-06 00:22:07 -04:00
|
|
|
defer %s_chr.Free()
|
2019-06-11 12:38:22 -04:00
|
|
|
`, s, s, s)
|
2019-05-03 13:14:30 -04:00
|
|
|
}
|
|
|
|
|
2019-05-02 14:14:29 -04:00
|
|
|
func (w *Wrapper) ProcessEnum(e *Enum) {
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Processing enum (%s)\n", e.Name)
|
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
w.processType(e.Type)
|
2019-05-02 14:14:29 -04:00
|
|
|
gtp := ""
|
2020-06-24 13:03:53 -04:00
|
|
|
if e.Type != nil {
|
2019-05-02 14:14:29 -04:00
|
|
|
gtp = e.Type.GoType()
|
2019-05-06 13:21:36 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
ctp := e.Type.CGoType()
|
2019-05-06 13:21:36 -04:00
|
|
|
if e.Type != nil {
|
2019-05-23 16:31:47 -04:00
|
|
|
if !w.ProcessedTypes[gtp] {
|
2019-05-02 14:14:29 -04:00
|
|
|
w.goTypes.WriteString(fmt.Sprintf(`
|
2019-05-06 13:21:36 -04:00
|
|
|
type %s %s
|
2019-06-11 12:38:22 -04:00
|
|
|
`, gtp, ctp))
|
2019-05-23 16:31:47 -04:00
|
|
|
w.ProcessedTypes[gtp] = true
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" gtp = %s; ctp = %s\n", gtp, ctp)
|
|
|
|
}
|
|
|
|
for _, c := range e.Constants {
|
2020-06-25 16:56:55 -04:00
|
|
|
w.goConst.WriteString(fmt.Sprintf(`const %s %s = C.%s
|
2020-06-24 13:03:53 -04:00
|
|
|
`, c.name, gtp, c.name))
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
w.goConst.WriteString("\n")
|
2019-05-02 14:14:29 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
func (w *Wrapper) MethodFromSig(sig, class string) *Method {
|
|
|
|
ret := &Method{Parameters: []*Parameter{}}
|
2019-05-24 12:36:01 -04:00
|
|
|
if len(sig) == 0 {
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
if sig[0] == '+' {
|
|
|
|
ret.ClassMethod = true
|
|
|
|
}
|
|
|
|
sig = sig[1:]
|
2019-06-11 12:38:22 -04:00
|
|
|
rem, n := types.MethodSignature(sig, types.NewNode("AST"))
|
2021-06-11 11:24:25 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Println(n.String())
|
|
|
|
}
|
2019-05-24 12:36:01 -04:00
|
|
|
if len(rem) > 0 {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Failed to parse method signature %s (%s)\n", sig, rem)
|
2019-05-24 12:36:01 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-06-04 13:01:51 -04:00
|
|
|
i := 0 // count MethodParameters
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, c := range n.Children {
|
2019-05-24 12:36:01 -04:00
|
|
|
switch c.Kind {
|
|
|
|
case "TypeName":
|
2019-06-11 12:38:22 -04:00
|
|
|
tp := types.NewType(c, class)
|
2019-05-24 12:36:01 -04:00
|
|
|
ret.Type = tp
|
|
|
|
case "Identifier":
|
|
|
|
ret.Name = c.Content
|
2019-05-28 13:03:23 -04:00
|
|
|
ret.GoName = strings.Title(c.Content)
|
2019-05-24 12:36:01 -04:00
|
|
|
case "MethodParameter":
|
|
|
|
p := &Parameter{}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, d := range c.Children {
|
2019-05-24 12:36:01 -04:00
|
|
|
switch d.Kind {
|
|
|
|
case "TypeName":
|
2019-06-11 12:38:22 -04:00
|
|
|
tp := types.NewType(d, class)
|
2019-05-24 12:36:01 -04:00
|
|
|
p.Type = tp
|
|
|
|
case "Identifier":
|
2019-06-04 13:01:51 -04:00
|
|
|
if i == 0 || p.Pname != "" {
|
|
|
|
p.Vname = d.Content
|
|
|
|
} else {
|
|
|
|
p.Pname = d.Content
|
|
|
|
}
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-04 13:01:51 -04:00
|
|
|
i++
|
2019-06-11 12:38:22 -04:00
|
|
|
ret.Parameters = append(ret.Parameters, p)
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-06-18 09:15:46 -04:00
|
|
|
func (mc *MethodCollection) AddMethod(m *Method) {
|
|
|
|
m2 := &Method{
|
|
|
|
Name: m.Name,
|
|
|
|
GoName: m.GoName,
|
|
|
|
Class: mc.Class,
|
|
|
|
GoClass: mc.GoClass,
|
|
|
|
Type: m.Type.CloneToClass(mc.Class),
|
|
|
|
ClassMethod: m.ClassMethod,
|
|
|
|
Parameters: []*Parameter{},
|
|
|
|
Unavailable: m.Unavailable,
|
|
|
|
}
|
|
|
|
for _, p := range m.Parameters {
|
|
|
|
p2 := &Parameter{
|
|
|
|
Pname: p.Pname,
|
|
|
|
Vname: p.Vname,
|
|
|
|
Type: p.Type.CloneToClass(mc.Class),
|
|
|
|
}
|
|
|
|
m2.Parameters = append(m2.Parameters, p2)
|
|
|
|
}
|
|
|
|
mc.Methods = append(mc.Methods, m2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MethodCollection) AddMethods(smc *MethodCollection) {
|
|
|
|
for _, m := range smc.Methods {
|
|
|
|
mc.AddMethod(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-24 12:36:01 -04:00
|
|
|
func (w *Wrapper) ProcessSubclass(sname string, sc *Subclass) {
|
2019-06-18 09:15:46 -04:00
|
|
|
i := &Interface{
|
|
|
|
ProcessedInstanceMethods: map[string]bool{},
|
2020-06-24 13:03:53 -04:00
|
|
|
Properties: map[string]*Property{},
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
|
|
|
w.Interfaces[sname] = i
|
2019-05-24 15:40:01 -04:00
|
|
|
gname := strings.Title(sname)
|
|
|
|
types.Wrap(gname)
|
2019-06-11 12:38:22 -04:00
|
|
|
types.SetSuper(gname, sc.Super)
|
2019-05-24 12:36:01 -04:00
|
|
|
ps := map[string][]string{}
|
|
|
|
ps[sc.Super] = sc.Overrides
|
2019-06-11 12:38:22 -04:00
|
|
|
nms := make([]*Method, len(sc.NewMethods))
|
|
|
|
for i, sig := range sc.NewMethods {
|
|
|
|
nms[i] = w.MethodFromSig(sig, sname)
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
2019-06-13 09:25:53 -04:00
|
|
|
if Debug {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("ProcessSubclass(%s)\n", sname)
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
w._ProcessDelSub(sname, ps, nms, true)
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wrapper) ProcessDelegate(dname string, ps map[string][]string) {
|
2019-06-18 09:15:46 -04:00
|
|
|
i := &Interface{
|
|
|
|
ProcessedInstanceMethods: map[string]bool{},
|
2020-06-24 13:03:53 -04:00
|
|
|
Properties: map[string]*Property{},
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
|
|
|
w.Interfaces[dname] = i
|
2019-06-11 12:38:22 -04:00
|
|
|
w._ProcessDelSub(dname, ps, nil, false)
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:04 -04:00
|
|
|
//NOTE: The delegate wrapper does not support variadic callback functions.
|
2019-05-24 12:36:01 -04:00
|
|
|
func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string, nms []*Method, sub bool) {
|
2019-05-09 22:05:04 -04:00
|
|
|
//To create (per delegate):
|
|
|
|
//1. ObjC interface
|
|
|
|
//2. ObjC implementation
|
|
|
|
//3. ObjC constructor function
|
|
|
|
//4. Go type
|
|
|
|
//5. Go constructor
|
|
|
|
//6. Go dispatch database for callbacks
|
2019-05-09 12:25:45 -04:00
|
|
|
//To create (per method):
|
|
|
|
//1. ObjC function prototypes for go exports
|
2019-05-09 22:05:04 -04:00
|
|
|
//2. Go callback registration functions
|
2019-05-09 12:25:45 -04:00
|
|
|
//3. Go exported callback function wrappers
|
2019-05-21 16:26:52 -04:00
|
|
|
//4. Go wrapper functions for superclass methods
|
2019-05-09 12:25:45 -04:00
|
|
|
|
2019-05-09 22:05:04 -04:00
|
|
|
//organize output into string builders
|
|
|
|
var cprotos, ccode, gotypes, gocode, goexports strings.Builder
|
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
//set up array of methods for this delegate or subclass
|
2019-05-09 22:05:04 -04:00
|
|
|
methods := []*Method{}
|
2019-06-11 12:38:22 -04:00
|
|
|
sms := 0 // the number of methods that have super-methods
|
2019-05-10 02:00:56 -04:00
|
|
|
gnames := []string{} // go names for methods
|
2019-06-11 12:38:22 -04:00
|
|
|
pnames := make([]string, len(ps))
|
2019-06-18 09:15:46 -04:00
|
|
|
supmeths := []*Method{}
|
2019-05-21 16:26:52 -04:00
|
|
|
var supr string
|
|
|
|
i := 0
|
2019-06-11 12:38:22 -04:00
|
|
|
for pname, pats := range ps {
|
2019-05-21 16:26:52 -04:00
|
|
|
pnames[i] = pname
|
|
|
|
i++
|
2019-05-28 13:03:23 -04:00
|
|
|
var ms []*Method
|
2019-05-21 16:26:52 -04:00
|
|
|
if sub {
|
2019-05-24 15:40:01 -04:00
|
|
|
interf := w.Interfaces[pname]
|
|
|
|
supr = interf.GoName
|
2019-05-24 00:12:46 -04:00
|
|
|
if i > 1 {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Multiple inheritance is not permitted:\n subclass %s already inherits from %s\n", dname, supr)
|
2019-05-24 00:12:46 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
if interf == nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Failed to find interface %s for subclass %s\n", pname, dname)
|
2019-05-21 16:26:52 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
//if Debug {
|
|
|
|
fmt.Printf(" subclass for %s\n", pname)
|
|
|
|
//}
|
2019-06-04 21:18:06 -04:00
|
|
|
mc := NewMethodCollection(dname)
|
2019-05-24 00:12:46 -04:00
|
|
|
var addmeths func(s string)
|
|
|
|
addmeths = func(s string) {
|
|
|
|
if sup := types.Super(s); w.Interfaces[sup] != nil {
|
|
|
|
addmeths(sup)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
//if Debug {
|
|
|
|
fmt.Printf("Adding methods for interface %s\n", s)
|
|
|
|
//}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, m := range w.Interfaces[s].InstanceMethods.Methods {
|
2020-06-24 13:03:53 -04:00
|
|
|
if m.Unavailable {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if x := w.Interfaces[dname]; x != nil && x.InstanceMethods != nil && x.InstanceMethods.Unavailable[m.LongName()] {
|
|
|
|
continue
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if matches(string(m.Name[0])+m.GoName[1:], pats) {
|
|
|
|
mc.Methods = append(mc.Methods, m)
|
|
|
|
}
|
2019-06-04 21:18:06 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, p := range w.Interfaces[s].Protocols {
|
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Adding methods for protocol %s\n", p)
|
|
|
|
}
|
|
|
|
for _, m := range w.Protocols[p].InstanceMethods.Methods {
|
2020-06-24 13:03:53 -04:00
|
|
|
if m.Unavailable {
|
|
|
|
continue
|
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if x := w.Interfaces[dname]; x != nil && x.InstanceMethods != nil && x.InstanceMethods.Unavailable[m.LongName()] {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" -> %s\n", m.Name)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if matches(string(m.Name[0])+m.GoName[1:], pats) {
|
|
|
|
mc.Methods = append(mc.Methods, m)
|
|
|
|
}
|
2019-06-04 21:18:06 -04:00
|
|
|
}
|
2019-05-24 00:12:46 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
//for subclasses, add all superclass methods, depth first
|
2019-05-24 00:12:46 -04:00
|
|
|
addmeths(interf.Name)
|
2019-06-04 21:18:06 -04:00
|
|
|
Disambiguate(mc)
|
|
|
|
ms = mc.Methods
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("METHODS:\n")
|
|
|
|
for _, m := range ms {
|
|
|
|
fmt.Printf(" -> %s\n", m.Name)
|
|
|
|
}
|
2019-05-24 12:36:01 -04:00
|
|
|
} else { // not a subclass
|
2019-05-21 16:26:52 -04:00
|
|
|
proto := w.Protocols[pname]
|
|
|
|
if proto == nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Failed to find protocol %s for delegate %s\n", pname, dname)
|
2019-05-21 16:26:52 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" proto %s\n", pname)
|
|
|
|
}
|
2019-05-28 13:03:23 -04:00
|
|
|
ms = proto.InstanceMethods.Methods
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Protocol %s\n", pname)
|
|
|
|
}
|
|
|
|
types.SetSuper(dname, "Id")
|
2019-05-21 16:26:52 -04:00
|
|
|
supr = "Id"
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, m := range ms {
|
|
|
|
//note:we may have capitalized the first character to make a GoName...
|
2020-06-24 13:03:53 -04:00
|
|
|
if m.HasUnsupportedType() {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("--Method: %s\n", m.Name)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if sub || !matches(string(m.Name[0])+m.GoName[1:], pats) {
|
|
|
|
//methods from superclass that we are not overriding
|
|
|
|
supmeths = append(supmeths, m)
|
|
|
|
if !sub {
|
|
|
|
continue
|
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
methods = append(methods, m)
|
|
|
|
gnames = append(gnames, m.GoName)
|
|
|
|
if sub {
|
|
|
|
sms = len(methods)
|
2020-06-24 13:03:53 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf("sms = %d\n", sms)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//add new methods being defined for the subclass
|
|
|
|
if sub {
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, m := range nms {
|
2020-06-24 13:03:53 -04:00
|
|
|
//if Debug {
|
|
|
|
fmt.Printf("Adding method %s to subclass\n", m.Name)
|
|
|
|
//}
|
2019-06-11 12:38:22 -04:00
|
|
|
methods = append(methods, m)
|
|
|
|
gnames = append(gnames, strings.Title(m.Name))
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
} else {
|
|
|
|
nms = methods
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 12:38:22 -04:00
|
|
|
methprotos := make([]string, len(methods)) // objc method prototypes
|
|
|
|
smethprotos := make([]string, sms) // super method prototypes
|
|
|
|
sfunprotos := make([]string, sms) // super method prototypes
|
|
|
|
gname := strings.Title(dname) // go name for this Delegate
|
|
|
|
vnames := make([][]string, len(methods)) // objc variable names
|
|
|
|
vpnames := make([][]string, len(methods)) // objc parameter:variable names
|
|
|
|
gtypes := make([][]string, len(methods)) // go parameter types for each method
|
|
|
|
getypes := make([][]string, len(methods)) // parameter types for go export
|
|
|
|
grtypes := make([]string, len(methods)) // go retrun types for each method
|
|
|
|
cgtypes := make([]string, len(methods)) // cgo return types
|
|
|
|
crtypes := make([]string, len(methods)) // c return types for each method
|
2019-05-09 22:05:04 -04:00
|
|
|
|
|
|
|
//1. ObjC interface
|
2019-05-21 16:26:52 -04:00
|
|
|
if sub {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Subclass %s <%s>: %d overrides, %d new methods\n", dname, strings.Join(pnames, ", "), sms, len(nms))
|
2019-05-21 16:26:52 -04:00
|
|
|
} else {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Delegate %s <%s>: %d methods\n", dname, strings.Join(pnames, ", "), len(methods))
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for i, m := range methods {
|
2019-05-09 22:05:04 -04:00
|
|
|
w.processType(m.Type)
|
2019-06-11 12:38:22 -04:00
|
|
|
vnames[i] = make([]string, len(m.Parameters)+1)
|
|
|
|
vpnames[i] = make([]string, len(m.Parameters))
|
|
|
|
getypes[i] = make([]string, len(m.Parameters)+1)
|
2019-05-09 22:05:04 -04:00
|
|
|
vnames[i][0] = "self"
|
|
|
|
getypes[i][0] = "unsafe.Pointer"
|
2020-06-24 13:03:53 -04:00
|
|
|
//if m.Name == "dealloc" {
|
|
|
|
// gtypes[i] = make([]string, len(m.Parameters))
|
|
|
|
//} else {
|
|
|
|
gtypes[i] = make([]string, len(m.Parameters)+2)
|
|
|
|
gtypes[i][0] = gname // self
|
|
|
|
gtypes[i][1] = gname + "Supermethods"
|
|
|
|
//}
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("len gtypes[%d] = %d\n", i, len(gtypes[i]))
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("%s: %s\n", dname, m.Name)
|
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
var parms string
|
2019-05-21 16:26:52 -04:00
|
|
|
var cparms string
|
2019-05-09 22:05:04 -04:00
|
|
|
if len(m.Parameters) == 0 {
|
|
|
|
parms = ""
|
2019-05-21 16:26:52 -04:00
|
|
|
cparms = "void* self"
|
|
|
|
vpnames[i] = []string{m.Name}
|
2019-05-09 22:05:04 -04:00
|
|
|
} else {
|
|
|
|
pm := m.Parameters[0]
|
|
|
|
w.processType(pm.Type)
|
2019-06-11 12:38:22 -04:00
|
|
|
parms = fmt.Sprintf(":(%s)%s", pm.Type.Node.CType(), pm.Vname)
|
|
|
|
cparms = fmt.Sprintf("void* self, %s %s", pm.Type.Node.CType(), pm.Vname)
|
2019-05-09 22:05:04 -04:00
|
|
|
vnames[i][1] = pm.Vname
|
2019-05-21 16:26:52 -04:00
|
|
|
vpnames[i][0] = pm.Pname + ":" + pm.Vname
|
2020-06-24 13:03:53 -04:00
|
|
|
//if m.Name == "dealloc" {
|
|
|
|
// gtypes[i][1] = pm.Type.GoType()
|
|
|
|
//} else {
|
|
|
|
gtypes[i][2] = pm.Type.GoType()
|
|
|
|
//}
|
2019-05-09 22:05:04 -04:00
|
|
|
if pm.Type.IsPointer() {
|
|
|
|
getypes[i][1] = "unsafe.Pointer"
|
|
|
|
} else {
|
2020-06-24 13:03:53 -04:00
|
|
|
//if m.Name == "dealloc" {
|
|
|
|
// getypes[i][1] = gtypes[i][1]
|
|
|
|
//} else {
|
|
|
|
getypes[i][1] = gtypes[i][2]
|
|
|
|
//}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for j := 1; j < len(m.Parameters); j++ {
|
|
|
|
pm := m.Parameters[j]
|
|
|
|
w.processType(pm.Type)
|
2019-06-11 12:38:22 -04:00
|
|
|
parms = parms + fmt.Sprintf(" %s:(%s)%s", pm.Pname, pm.Type.Node.CType(), pm.Vname)
|
|
|
|
cparms = cparms + fmt.Sprintf(", %s %s", pm.Type.Node.CType(), pm.Vname)
|
2019-05-09 22:05:04 -04:00
|
|
|
vnames[i][j+1] = pm.Vname
|
2019-05-21 16:26:52 -04:00
|
|
|
vpnames[i][j] = pm.Pname + ":" + pm.Vname
|
2020-06-24 13:03:53 -04:00
|
|
|
//if m.Name == "dealloc" {
|
|
|
|
// gtypes[i][j+1] = pm.Type.GoType()
|
|
|
|
//} else {
|
|
|
|
gtypes[i][j+2] = pm.Type.GoType()
|
|
|
|
//}
|
2019-05-09 22:05:04 -04:00
|
|
|
var getp string
|
|
|
|
if pm.Type.IsPointer() {
|
|
|
|
getp = "unsafe.Pointer"
|
|
|
|
} else {
|
2020-06-24 13:03:53 -04:00
|
|
|
//if m.Name == "dealloc" {
|
|
|
|
// getp = gtypes[i][j+1]
|
|
|
|
//} else {
|
|
|
|
getp = gtypes[i][j+2]
|
|
|
|
//}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
|
|
|
getypes[i][j+1] = getp
|
|
|
|
}
|
|
|
|
methprotos[i] = fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
`- (%s)%s%s;`, m.Type.Node.CType(), m.Name, parms)
|
2019-06-06 00:22:07 -04:00
|
|
|
if x := m.Type.GoType(); x == "Void" {
|
|
|
|
grtypes[i] = ""
|
|
|
|
} else {
|
|
|
|
grtypes[i] = " " + x
|
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
ct := m.Type.Node.CType()
|
2019-06-06 00:22:07 -04:00
|
|
|
if ct == "instancetype" {
|
|
|
|
ct = dname + "*"
|
|
|
|
}
|
|
|
|
if ct == "id" {
|
|
|
|
ct = "void*"
|
|
|
|
grtypes[i] = " Id"
|
|
|
|
}
|
2019-05-24 12:36:01 -04:00
|
|
|
if i < sms {
|
|
|
|
smethprotos[i] = fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
`- (%s)super_%s%s;`, ct, m.Name, parms)
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
|
|
|
if i < sms {
|
2019-06-11 12:38:22 -04:00
|
|
|
_, cntps, _ := w.cparamlist(m)
|
2019-05-24 12:36:01 -04:00
|
|
|
sfunprotos[i] = fmt.Sprintf(
|
2020-06-24 13:03:53 -04:00
|
|
|
`%s %s_super_%s(%s);`, ct, dname, m.GoName, cntps)
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
|
|
|
crtypes[i] = m.Type.CTypeAttrib()
|
|
|
|
if m.Type.IsPointer() {
|
|
|
|
cgtypes[i] = "unsafe.Pointer"
|
|
|
|
} else {
|
|
|
|
crtypes[i] = m.Type.CTypeAttrib()
|
|
|
|
cgtypes[i] = m.Type.CGoType()
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
var supcls string
|
|
|
|
var protos string
|
|
|
|
if sub {
|
|
|
|
supcls = pnames[0]
|
|
|
|
protos = ""
|
|
|
|
} else {
|
|
|
|
supcls = "NSObject"
|
2019-06-11 12:38:22 -04:00
|
|
|
protos = "<" + strings.Join(pnames, ", ") + ">"
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
ccode.WriteString(fmt.Sprintf(`
|
2019-05-21 16:26:52 -04:00
|
|
|
@interface %s : %s %s
|
2019-05-09 22:05:04 -04:00
|
|
|
{ }
|
|
|
|
%s
|
2019-06-11 12:38:22 -04:00
|
|
|
`, dname, supcls, protos, strings.Join(methprotos, "\n")))
|
2020-06-24 13:03:53 -04:00
|
|
|
//havesupmethods := sms > 0
|
2019-05-21 16:26:52 -04:00
|
|
|
if sub {
|
2020-06-24 13:03:53 -04:00
|
|
|
for _, sp := range smethprotos {
|
|
|
|
//if methods[i].Name != "dealloc" {
|
|
|
|
ccode.WriteString(sp + "\n")
|
|
|
|
//} else {
|
|
|
|
// if sms == 1 {
|
|
|
|
// havesupmethods = false
|
|
|
|
// }
|
|
|
|
//}
|
2019-06-06 00:22:07 -04:00
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
|
|
|
ccode.WriteString(`
|
2019-05-09 22:05:04 -04:00
|
|
|
@end
|
2019-05-21 16:26:52 -04:00
|
|
|
`)
|
|
|
|
if sub {
|
2020-06-24 13:03:53 -04:00
|
|
|
for _, sf := range sfunprotos {
|
|
|
|
//if methods[i].Name != "dealloc" {
|
|
|
|
ccode.WriteString(sf + "\n")
|
|
|
|
//}
|
2019-06-06 00:22:07 -04:00
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
|
|
|
|
//2. ObjC implementation
|
2019-06-11 12:38:22 -04:00
|
|
|
methdecls := make([]string, len(methods))
|
|
|
|
smethdecls := make([]string, len(methods))
|
|
|
|
sfundecls := make([]string, len(methods))
|
|
|
|
for i, mp := range methprotos {
|
2019-05-21 16:26:52 -04:00
|
|
|
mp := mp[:len(mp)-1]
|
2019-06-06 00:22:07 -04:00
|
|
|
var smp, sfp, superdealloc string
|
2019-05-24 12:36:01 -04:00
|
|
|
if sub && i < sms {
|
|
|
|
smp = smethprotos[i][:len(smethprotos[i])-1]
|
|
|
|
sfp = sfunprotos[i][:len(sfunprotos[i])-1]
|
2019-06-06 00:22:07 -04:00
|
|
|
if methods[i].Name == "dealloc" {
|
|
|
|
superdealloc = "\n\t[super dealloc];"
|
|
|
|
}
|
2019-05-24 12:36:01 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
var ret string
|
|
|
|
if crtypes[i] != "void" {
|
|
|
|
ret = "return "
|
|
|
|
}
|
|
|
|
methdecls[i] = fmt.Sprintf(`
|
|
|
|
%s
|
|
|
|
{
|
|
|
|
%s%s(%s);
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, mp, ret, gname+gnames[i], strings.Join(vnames[i], ", "))
|
2019-05-21 16:26:52 -04:00
|
|
|
methdecls[i] = fmt.Sprintf(`
|
|
|
|
%s
|
|
|
|
{
|
2019-06-06 00:22:07 -04:00
|
|
|
%s%s(%s);%s
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, mp, ret, gname+gnames[i], strings.Join(vnames[i], ", "), superdealloc)
|
2019-05-24 12:36:01 -04:00
|
|
|
if sub && i < sms {
|
2019-05-21 16:26:52 -04:00
|
|
|
smethdecls[i] = fmt.Sprintf(`
|
|
|
|
%s
|
|
|
|
{
|
|
|
|
%s[super %s];
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
`, smp, ret, strings.Join(vpnames[i], " "))
|
|
|
|
var arp1, arp2 string
|
|
|
|
if vpnames[i][0] != "alloc" {
|
|
|
|
arp1 = "@autoreleasepool {\n\t\t"
|
|
|
|
arp2 = "\t}\n"
|
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
sfundecls[i] = fmt.Sprintf(`
|
|
|
|
%s
|
|
|
|
{
|
2020-06-24 13:03:53 -04:00
|
|
|
%s%s[(%s*)o super_%s];
|
|
|
|
%s}
|
|
|
|
`, sfp, arp1, ret, dname, strings.Join(vpnames[i], " "), arp2)
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
ccode.WriteString(fmt.Sprintf(`
|
2019-05-09 22:05:04 -04:00
|
|
|
@implementation %s
|
|
|
|
%s
|
2019-06-11 12:38:22 -04:00
|
|
|
`, dname, strings.Join(methdecls, "\n")))
|
2019-05-21 16:26:52 -04:00
|
|
|
if sub {
|
2020-06-24 13:03:53 -04:00
|
|
|
for _, sm := range smethdecls {
|
|
|
|
//if methods[i].Name != "dealloc" {
|
|
|
|
ccode.WriteString(sm + "\n")
|
|
|
|
//}
|
2019-06-06 00:22:07 -04:00
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
|
|
|
ccode.WriteString(`
|
|
|
|
@end
|
|
|
|
`)
|
|
|
|
if sub {
|
2020-06-24 13:03:53 -04:00
|
|
|
for _, sf := range sfundecls {
|
|
|
|
//if methods[i].Name != "dealloc" {
|
|
|
|
ccode.WriteString(sf + "\n")
|
|
|
|
//}
|
2019-06-06 00:22:07 -04:00
|
|
|
}
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
|
|
|
|
//3. ObjC constructor function
|
|
|
|
ccode.WriteString(fmt.Sprintf(`
|
|
|
|
void*
|
|
|
|
%sAlloc() {
|
2019-06-11 12:38:22 -04:00
|
|
|
`, dname))
|
|
|
|
if Autorelease {
|
|
|
|
ccode.WriteString(fmt.Sprintf(
|
|
|
|
` return [[%s alloc] autorelease];
|
2019-06-06 00:22:07 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, dname))
|
|
|
|
} else {
|
|
|
|
ccode.WriteString(fmt.Sprintf(
|
|
|
|
` return [%s alloc];
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, dname))
|
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
|
|
|
|
//4. Go type
|
2019-06-18 09:15:46 -04:00
|
|
|
if !w.ProcessedTypes[gname] {
|
|
|
|
gotypes.WriteString(
|
2020-06-24 13:03:53 -04:00
|
|
|
types.NewTypeFromString(gname, supr).GoInterfaceDecl(Gogc))
|
2019-05-09 22:05:04 -04:00
|
|
|
|
2020-06-24 13:03:53 -04:00
|
|
|
//5. Go constructor
|
2019-06-18 09:15:46 -04:00
|
|
|
var finalizer string
|
|
|
|
dbg := ""
|
|
|
|
dbg2 := ""
|
|
|
|
if Debug {
|
|
|
|
dbg = fmt.Sprintf(`fmt.Printf("Setting finalizer (%s): %%p -> %%p\n", ret, ret.ptr)
|
|
|
|
`, gname)
|
|
|
|
dbg2 = fmt.Sprintf(`fmt.Printf("Finalizer (%s): release %%p -> %%p\n", o, o.ptr)
|
2019-06-18 17:29:52 -04:00
|
|
|
`, gname)
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
|
|
|
if Gogc {
|
|
|
|
w.goImports["runtime"] = true
|
2020-06-24 13:03:53 -04:00
|
|
|
if Debug {
|
|
|
|
w.goImports["fmt"] = true
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
finalizer = fmt.Sprintf(
|
2020-06-24 13:03:53 -04:00
|
|
|
`if ret.ptr == nil { return ret }
|
2019-06-18 09:15:46 -04:00
|
|
|
%sruntime.SetFinalizer(ret,func(o *%s) {
|
|
|
|
%so.Release()
|
2019-06-11 12:38:22 -04:00
|
|
|
})
|
2019-06-18 09:15:46 -04:00
|
|
|
`, dbg, gname, dbg2)
|
|
|
|
}
|
|
|
|
gocode.WriteString(fmt.Sprintf(`
|
2019-06-13 09:25:53 -04:00
|
|
|
func %sAlloc() *%s {
|
|
|
|
ret := &%s{}
|
2019-05-23 16:31:47 -04:00
|
|
|
ret.ptr = unsafe.Pointer(C.%sAlloc())
|
2019-06-11 12:38:22 -04:00
|
|
|
%sreturn ret
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, gname, gname, gname, dname, finalizer))
|
2019-06-18 09:15:46 -04:00
|
|
|
if Gogc {
|
2019-06-18 17:29:52 -04:00
|
|
|
if Debug {
|
|
|
|
dbg = fmt.Sprintf(`fmt.Printf("Setting finalizer (%s): %%p -> %%p\n", o, o.ptr)
|
|
|
|
`, gname)
|
|
|
|
}
|
2019-06-18 09:15:46 -04:00
|
|
|
gocode.WriteString(fmt.Sprintf(`
|
|
|
|
func (o *%s) GC() {
|
|
|
|
if o.ptr == nil { return }
|
|
|
|
%sruntime.SetFinalizer(o,func(o *%s) {
|
|
|
|
%so.Release()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
`, gname, dbg, gname, dbg2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.ProcessedTypes[gname] = true
|
2019-05-09 22:05:04 -04:00
|
|
|
|
|
|
|
//6. Go dispatch database for callbacks
|
2019-06-11 12:38:22 -04:00
|
|
|
dispitems := make([]string, len(gnames))
|
|
|
|
sdispitems := make([]string, sms)
|
|
|
|
for i, n := range gnames {
|
2020-06-24 13:03:53 -04:00
|
|
|
if !sub || sms == 0 { // || !havesupmethods {
|
|
|
|
gtypes[i] = append(gtypes[i][0:1], gtypes[i][2:]...)
|
|
|
|
//if sub && !havesupmethods {
|
|
|
|
// gtypes[i] = append(gtypes[i][:1],gtypes[i][2:]...)
|
|
|
|
// fmt.Printf("len gtypes[%d] = %d\n", i, len(gtypes[i]))
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
dispitems[i] = fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
` %s func(%s)%s`, n, strings.Join(gtypes[i], ", "), grtypes[i])
|
2020-06-24 13:03:53 -04:00
|
|
|
if sub && i < sms { // && methods[i].Name != "dealloc" {
|
2019-05-21 16:26:52 -04:00
|
|
|
sdispitems[i] = fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
` %s func(%s)%s
|
2020-06-24 13:03:53 -04:00
|
|
|
`, n, strings.Join(gtypes[i][2:], ", "), grtypes[i])
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
gocode.WriteString(fmt.Sprintf(`
|
2019-05-09 22:05:04 -04:00
|
|
|
type %sDispatch struct {
|
|
|
|
%s
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
var %sLookup = map[unsafe.Pointer]%sDispatch{}
|
|
|
|
var %sMux sync.RWMutex
|
|
|
|
`, gname, strings.Join(dispitems, "\n"), gname, gname, gname))
|
|
|
|
w.goImports["sync"] = true
|
2020-06-24 13:03:53 -04:00
|
|
|
if sub && sms > 0 { // && havesupmethods {
|
2019-05-21 16:26:52 -04:00
|
|
|
gocode.WriteString(fmt.Sprintf(`
|
|
|
|
type %sSupermethods struct {
|
|
|
|
%s
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
`, gname, strings.Join(sdispitems, "")))
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
//To create (per method):
|
2019-05-09 22:05:04 -04:00
|
|
|
cprotos.WriteString("\n\n")
|
2019-06-11 12:38:22 -04:00
|
|
|
for i, m := range methods {
|
2019-05-09 12:25:45 -04:00
|
|
|
//1. ObjC function prototypes for go exports
|
2019-06-11 12:38:22 -04:00
|
|
|
_, _, ctps := w.cparamlist(m)
|
2019-05-09 22:05:04 -04:00
|
|
|
cprotos.WriteString(fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
`%s %s%s(%s);
|
|
|
|
`, crtypes[i], gname, gnames[i], ctps))
|
2019-05-09 22:05:04 -04:00
|
|
|
//2. Go callback registration functions
|
|
|
|
gocode.WriteString(fmt.Sprintf(`
|
2019-05-24 12:36:01 -04:00
|
|
|
func (d %s) %sCallback(f func(%s)%s) {
|
2019-06-11 12:38:22 -04:00
|
|
|
%sMux.Lock()
|
2019-05-09 22:05:04 -04:00
|
|
|
dispatch := %sLookup[d.Ptr()]
|
|
|
|
dispatch.%s = f
|
2019-05-21 16:26:52 -04:00
|
|
|
%sLookup[d.Ptr()] = dispatch
|
2019-06-11 12:38:22 -04:00
|
|
|
%sMux.Unlock()
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, gname, gnames[i], strings.Join(gtypes[i], ", "), grtypes[i], gname, gname, gnames[i], gname, gname))
|
2019-05-09 12:25:45 -04:00
|
|
|
//3. Go exported callback function wrappers
|
2019-05-21 16:26:52 -04:00
|
|
|
earglist := []string{"o unsafe.Pointer"}
|
2020-06-24 13:03:53 -04:00
|
|
|
garglist := []string{"self"}
|
2019-05-23 16:31:47 -04:00
|
|
|
gargconv := []string{}
|
2020-06-24 13:03:53 -04:00
|
|
|
if sub && sms > 0 { // && m.Name != "dealloc" {
|
|
|
|
garglist = []string{"self", "super"}
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
for j := 1; j < len(vnames[i]); j++ {
|
2019-06-11 12:38:22 -04:00
|
|
|
earglist = append(earglist, vnames[i][j]+" "+getypes[i][j])
|
2019-05-21 16:26:52 -04:00
|
|
|
var gt2 string
|
|
|
|
if sub {
|
2020-06-24 13:03:53 -04:00
|
|
|
//fmt.Println(gtypes)
|
|
|
|
//fmt.Printf("%d %d\n",i,j)
|
|
|
|
gt2 = gtypes[i][j+1]
|
2019-05-21 16:26:52 -04:00
|
|
|
} else {
|
2020-06-24 13:03:53 -04:00
|
|
|
gt2 = gtypes[i][j]
|
|
|
|
//gt2 = gtypes[i][j]
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
if types.PtrIsGoInterface(gt2) {
|
2019-06-13 09:25:53 -04:00
|
|
|
gt2 = "*Id"
|
2019-05-10 02:00:56 -04:00
|
|
|
}
|
2019-06-13 16:47:36 -04:00
|
|
|
if gt2 == "*Id" || types.PtrShouldWrap(gt2) {
|
2019-06-11 12:38:22 -04:00
|
|
|
garglist = append(garglist, fmt.Sprintf(
|
|
|
|
`a%d`, j))
|
|
|
|
gargconv = append(gargconv, fmt.Sprintf(
|
2019-06-13 16:47:36 -04:00
|
|
|
` a%d := &%s{}; a%d.ptr = %s`, j, gt2[1:], j, vnames[i][j]))
|
2019-05-23 16:31:47 -04:00
|
|
|
} else {
|
2019-06-11 12:38:22 -04:00
|
|
|
garglist = append(garglist, fmt.Sprintf(
|
|
|
|
`(%s)(%s)`, gt2, vnames[i][j]))
|
2019-05-23 16:31:47 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
|
|
|
retdecl := ""
|
|
|
|
retname := ""
|
|
|
|
retn := ""
|
|
|
|
retnparen := ""
|
|
|
|
crtype := ""
|
|
|
|
if cgtypes[i] != "C.void" {
|
|
|
|
retdecl = "var ret " + cgtypes[i] + "\n\t"
|
|
|
|
retname = " ret"
|
|
|
|
if cgtypes[i] == "unsafe.Pointer" {
|
|
|
|
retn = "return unsafe.Pointer("
|
|
|
|
crtype = " unsafe.Pointer"
|
2019-05-24 12:36:01 -04:00
|
|
|
if types.ShouldWrap(m.Type.GoType()) {
|
|
|
|
retnparen = ".Ptr()"
|
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
} else {
|
|
|
|
retn = "return (" + cgtypes[i] + ")("
|
|
|
|
crtype = " " + cgtypes[i]
|
|
|
|
}
|
2019-05-24 12:36:01 -04:00
|
|
|
retnparen = retnparen + ")"
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
sdispentries := make([]string, sms)
|
|
|
|
for i, _ := range sdispentries {
|
2020-06-24 13:03:53 -04:00
|
|
|
//if methods[i].Name != "dealloc" {
|
|
|
|
sdispentries[i] = fmt.Sprintf(
|
|
|
|
` self.Super%s,
|
2019-06-11 12:38:22 -04:00
|
|
|
`, gnames[i])
|
2020-06-24 13:03:53 -04:00
|
|
|
//}
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
|
|
|
sper := ""
|
2020-06-24 13:03:53 -04:00
|
|
|
if sub && sms > 0 { //&& m.Name != "dealloc" {
|
2019-05-21 16:26:52 -04:00
|
|
|
sper = fmt.Sprintf(
|
2020-06-24 13:03:53 -04:00
|
|
|
` super := %sSupermethods{
|
2019-06-06 00:22:07 -04:00
|
|
|
%s }
|
2020-06-24 13:03:53 -04:00
|
|
|
`, gname, strings.Join(sdispentries, ""))
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
if len(gargconv) > 0 {
|
|
|
|
retn = "\n " + retn
|
|
|
|
} else {
|
|
|
|
retn = " " + retn
|
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
goexports.WriteString(fmt.Sprintf(`
|
2019-05-09 22:05:04 -04:00
|
|
|
//export %s%s
|
|
|
|
func %s%s(%s)%s {
|
2019-06-11 12:38:22 -04:00
|
|
|
%sMux.RLock()
|
2019-05-21 16:26:52 -04:00
|
|
|
%scb := %sLookup[o].%s
|
2019-06-11 12:38:22 -04:00
|
|
|
%sMux.RUnlock()
|
2019-05-09 22:05:04 -04:00
|
|
|
if cb == nil { return%s }
|
2020-06-24 13:03:53 -04:00
|
|
|
self := %s{}
|
|
|
|
self.ptr = o
|
2019-05-23 16:31:47 -04:00
|
|
|
%s%s%scb(%s)%s
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
`, gname, gnames[i], gname, gnames[i], strings.Join(earglist, ", "), crtype, gname, retdecl, gname, gnames[i], gname, retname, gname, sper, strings.Join(gargconv, "\n"), retn, strings.Join(garglist, ", "), retnparen))
|
2019-05-21 16:26:52 -04:00
|
|
|
//4. Go wrapper functions for superclass methods
|
2019-06-11 12:38:22 -04:00
|
|
|
if !sub || i >= sms {
|
|
|
|
continue
|
|
|
|
} // for subclasses only
|
2020-06-24 13:03:53 -04:00
|
|
|
//if m.Name == "dealloc" {
|
|
|
|
// continue
|
|
|
|
//}
|
2019-05-21 16:26:52 -04:00
|
|
|
grtype := m.Type.GoType()
|
|
|
|
if grtype == "Void" {
|
|
|
|
grtype = ""
|
|
|
|
}
|
|
|
|
if types.IsGoInterface(grtype) {
|
2019-06-13 09:25:53 -04:00
|
|
|
grtype = "*Id"
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
|
|
|
if grtype == "BOOL" {
|
|
|
|
grtype = "bool"
|
|
|
|
}
|
|
|
|
if sub {
|
|
|
|
gocode.WriteString(fmt.Sprintf(`
|
2019-06-13 09:25:53 -04:00
|
|
|
func (o *%s) Super%s(%s) %s {
|
2019-06-11 12:38:22 -04:00
|
|
|
`, gname, gnames[i], strings.Join(earglist[1:], ", "), grtype))
|
2019-06-27 11:41:58 -04:00
|
|
|
ns, _, snames, tps, _ := w.gpntp(m)
|
2019-06-11 12:38:22 -04:00
|
|
|
lparm := len(tps) - 1
|
2019-05-21 16:26:52 -04:00
|
|
|
if len(tps) > 0 && tps[lparm].Variadic {
|
|
|
|
vn := ns[lparm]
|
|
|
|
vn = vn[:len(vn)-1]
|
|
|
|
ns[lparm] = vn
|
|
|
|
gocode.WriteString(fmt.Sprintf(
|
2019-06-11 12:38:22 -04:00
|
|
|
` var %s [%d]unsafe.Pointer
|
2019-05-21 16:26:52 -04:00
|
|
|
for i,o := range %ss {
|
|
|
|
%s[i] = o.Ptr()
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
`, vn, w.Vaargs, vn, vn))
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
gocode.WriteString("\t" + types.GoToC(m.Name, dname+"_super_"+m.GoName, ns, snames, m.Type, tps, false, m.ShouldFinalize(), m.ClassMethod, w.goImports) + "\n}\n")
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
|
|
|
w.cCode.WriteString(cprotos.String())
|
|
|
|
w.cCode.WriteString(ccode.String())
|
|
|
|
w.goTypes.WriteString(gotypes.String())
|
|
|
|
w.goCode.WriteString(gocode.String())
|
|
|
|
w.goExports.WriteString(goexports.String())
|
2019-06-18 09:15:46 -04:00
|
|
|
|
|
|
|
// add methods from parent class that we are not overriding
|
2020-06-24 13:03:53 -04:00
|
|
|
for _, m := range supmeths {
|
|
|
|
w.ProcessMethodForClass(m, dname)
|
2019-06-18 09:15:46 -04:00
|
|
|
}
|
2019-05-09 12:25:45 -04:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:41:21 -04:00
|
|
|
//Add class and instance methods from super class
|
|
|
|
func (w *Wrapper) AddSupermethods(i *Interface) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
|
|
|
if i.Name == dbclass {
|
|
|
|
prt = true
|
|
|
|
fmt.Printf("%s\n", dbclass)
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
var supmethods func(string)
|
2021-11-16 16:01:03 -05:00
|
|
|
|
2019-06-11 16:41:21 -04:00
|
|
|
procsups := func(mc, smc *MethodCollection) {
|
|
|
|
for _, m := range smc.Methods {
|
2021-11-16 16:01:03 -05:00
|
|
|
if mc.Unavailable[m.LongName()] {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s (from %s): previously marked unavailable\n", m.Name, smc.Class)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if m.Unavailable {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s (from %s): marked unavailable\n", m.Name, smc.Class)
|
|
|
|
}
|
|
|
|
mc.Unavailable[m.LongName()] = true
|
|
|
|
if m.Name == "init" {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" Marking new method unavailable in %s\n", mc.Class)
|
|
|
|
}
|
|
|
|
w.Interfaces[mc.Class].ClassMethods.Unavailable["New"] = true
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s (from %s): available\n", m.Name, smc.Class)
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
m2 := &Method{
|
|
|
|
Name: m.Name,
|
|
|
|
GoName: m.GoName,
|
2019-06-18 09:15:46 -04:00
|
|
|
Class: mc.Class,
|
|
|
|
GoClass: mc.GoClass,
|
2019-06-11 16:41:21 -04:00
|
|
|
Type: m.Type.CloneToClass(i.Name),
|
|
|
|
ClassMethod: m.ClassMethod,
|
|
|
|
Parameters: []*Parameter{},
|
|
|
|
Unavailable: m.Unavailable,
|
|
|
|
}
|
|
|
|
for _, p := range m.Parameters {
|
|
|
|
p2 := &Parameter{
|
|
|
|
Pname: p.Pname,
|
|
|
|
Vname: p.Vname,
|
|
|
|
Type: p.Type.CloneToClass(i.Name),
|
|
|
|
}
|
|
|
|
m2.Parameters = append(m2.Parameters, p2)
|
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if prt {
|
|
|
|
fmt.Printf(" -> adding method %s\n", m.Name)
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
mc.Methods = append(mc.Methods, m2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
supmethods = func(s string) {
|
|
|
|
if sup, ok := w.Interfaces[s]; !ok {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
procsups(i.ClassMethods, sup.ClassMethods)
|
|
|
|
procsups(i.InstanceMethods, sup.InstanceMethods)
|
|
|
|
// depth last
|
|
|
|
supmethods(types.Super(s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
supmethods(types.Super(i.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add methods from a Protocol
|
|
|
|
func (w *Wrapper) AddProtocolMethods(i *Interface, p *Protocol) {
|
2021-11-16 16:01:03 -05:00
|
|
|
prt := false
|
|
|
|
if i.Name == dbclass {
|
|
|
|
prt = true
|
|
|
|
fmt.Printf("%s\n",dbclass)
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
procmeths := func(mc, pmc *MethodCollection) {
|
|
|
|
for _, m := range pmc.Methods {
|
2021-11-16 16:01:03 -05:00
|
|
|
if mc.Unavailable[m.LongName()] {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s: previously marked unavailable\n", m.Name)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if m.Unavailable {
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s: marked unavailable\n", m.Name)
|
|
|
|
}
|
|
|
|
mc.Unavailable[m.LongName()] = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if prt {
|
|
|
|
fmt.Printf(" %s: available\n", m.Name)
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
m2 := &Method{
|
2020-06-24 13:03:53 -04:00
|
|
|
Name: m.Name,
|
|
|
|
GoName: m.GoName,
|
|
|
|
Class: i.Name,
|
|
|
|
GoClass: i.GoName,
|
|
|
|
Type: m.Type.CloneToClass(i.Name),
|
2019-06-11 16:41:21 -04:00
|
|
|
ClassMethod: m.ClassMethod,
|
2020-06-24 13:03:53 -04:00
|
|
|
Parameters: []*Parameter{},
|
2019-06-11 16:41:21 -04:00
|
|
|
Unavailable: m.Unavailable,
|
|
|
|
}
|
|
|
|
for _, p := range m.Parameters {
|
|
|
|
p2 := &Parameter{
|
|
|
|
Pname: p.Pname,
|
|
|
|
Vname: p.Vname,
|
2020-06-24 13:03:53 -04:00
|
|
|
Type: p.Type.CloneToClass(i.Name),
|
2019-06-11 16:41:21 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
m2.Parameters = append(m2.Parameters, p2)
|
2019-06-11 16:41:21 -04:00
|
|
|
}
|
2021-11-16 16:01:03 -05:00
|
|
|
if prt {
|
|
|
|
fmt.Printf(" -> adding method %s\n", m.Name)
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
mc.Methods = append(mc.Methods, m2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
procmeths(i.ClassMethods, p.ClassMethods)
|
|
|
|
procmeths(i.InstanceMethods, p.InstanceMethods)
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:25:53 -04:00
|
|
|
func printDebug() {
|
2020-06-24 13:03:53 -04:00
|
|
|
fmt.Printf("ShouldWrap(NSString) = %t\n", types.ShouldWrap("NSString"))
|
|
|
|
fmt.Printf("ShouldWrap(*NSString) = %t\n", types.ShouldWrap("*NSString"))
|
|
|
|
fmt.Printf("IsGoInterface(NSObject) = %t\n", types.IsGoInterface("NSObject"))
|
|
|
|
fmt.Printf("IsGoInterface(*NSObject) = %t\n", types.IsGoInterface("*NSObject"))
|
|
|
|
fmt.Printf("IsGoInterface(NSString) = %t\n", types.IsGoInterface("NSString"))
|
|
|
|
fmt.Printf("IsGoInterface(*NSString) = %t\n", types.IsGoInterface("*NSString"))
|
|
|
|
fmt.Printf("PtrShouldWrap(NSString) = %t\n", types.PtrShouldWrap("NSString"))
|
|
|
|
fmt.Printf("PtrShouldWrap(*NSString) = %t\n", types.PtrShouldWrap("*NSString"))
|
|
|
|
fmt.Printf("PtrIsGoInterface(NSObject) = %t\n", types.PtrIsGoInterface("NSObject"))
|
|
|
|
fmt.Printf("PtrIsGoInterface(*NSObject) = %t\n", types.PtrIsGoInterface("*NSObject"))
|
|
|
|
fmt.Printf("PtrIsGoInterface(NSString) = %t\n", types.PtrIsGoInterface("NSString"))
|
|
|
|
fmt.Printf("PtrIsGoInterface(*NSString) = %t\n", types.PtrIsGoInterface("*NSString"))
|
|
|
|
fmt.Printf("Super(NSString) = %s\n", types.Super("NSString"))
|
|
|
|
fmt.Printf("Super(*NSString) = %s\n", types.Super("*NSString"))
|
|
|
|
fmt.Printf("Super(NSObject) = %s\n", types.Super("NSObject"))
|
|
|
|
fmt.Printf("Super(*NSObject) = %s\n", types.Super("*NSObject"))
|
|
|
|
fmt.Printf("Super(NSString*) = %s\n", types.Super("NSString*"))
|
|
|
|
fmt.Printf("Super(NSObject*) = %s\n", types.Super("NSObject*"))
|
2019-06-13 09:25:53 -04:00
|
|
|
}
|
|
|
|
|
2019-04-23 15:48:11 -04:00
|
|
|
func (w *Wrapper) Wrap(toproc []string) {
|
2019-06-11 12:38:22 -04:00
|
|
|
if w.Package == "" {
|
|
|
|
w.Package = "ns"
|
|
|
|
}
|
|
|
|
err := os.MkdirAll(w.Package, 0755)
|
2019-04-26 22:44:30 -04:00
|
|
|
if err != nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Error creating directory '%s'\n%s\n", w.Package, err)
|
2019-04-26 22:44:30 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
of, err := os.Create(path.Join(w.Package, "main.go"))
|
2019-04-26 22:44:30 -04:00
|
|
|
if err != nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Error opening file %s\n%s\n", path.Join(w.Package, "main.go"), err)
|
2019-04-26 22:44:30 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ef, err := os.Create(path.Join(w.Package, "exports.go"))
|
2019-05-09 22:05:04 -04:00
|
|
|
if err != nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Error opening file %s\n%s\n", path.Join(w.Package, "exports.go"), err)
|
2019-05-09 22:05:04 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Writing output to %s\n", path.Join(w.Package, "main.go"))
|
2019-05-01 10:58:29 -04:00
|
|
|
pInterfaces := map[string]*Interface{}
|
2019-05-21 16:26:52 -04:00
|
|
|
//Note: the following code eliminates duplicates, so it is acceptable
|
|
|
|
//to have duplicate interfaces in 'toproc'
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, iface := range toproc {
|
2019-04-23 15:48:11 -04:00
|
|
|
pInterfaces[iface] = w.Interfaces[iface]
|
|
|
|
}
|
2019-04-26 15:08:44 -04:00
|
|
|
//FIXME: sort pInterfaces
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, i := range pInterfaces {
|
2019-05-02 14:14:29 -04:00
|
|
|
if i == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
w.processType(types.NewTypeFromString(i.GoName, ""))
|
2019-05-28 15:52:16 -04:00
|
|
|
if i.Name == "NSString" {
|
|
|
|
w.StringHelpers()
|
|
|
|
}
|
2019-05-24 15:40:01 -04:00
|
|
|
if i.Name == "NSEnumerator" {
|
|
|
|
w.EnumeratorHelpers()
|
|
|
|
}
|
2019-06-11 16:41:21 -04:00
|
|
|
w.AddSupermethods(i)
|
2019-04-10 14:00:48 -04:00
|
|
|
|
2019-04-26 15:08:44 -04:00
|
|
|
//FIXME: sort properties
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, p := range i.Properties {
|
2019-06-06 00:22:07 -04:00
|
|
|
//Properties are not supported, use getter/setter
|
|
|
|
//methods instead.
|
2019-06-11 12:38:22 -04:00
|
|
|
if Debug {
|
|
|
|
fmt.Printf(" property: %s (%s)\n", p.Name, p.Type.CType())
|
|
|
|
}
|
2019-04-09 23:19:49 -04:00
|
|
|
}
|
2019-05-23 16:31:47 -04:00
|
|
|
// add methods for Protocols that this interface implements
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, p := range i.Protocols {
|
|
|
|
prot, ok := w.Protocols[p]
|
2019-05-23 16:31:47 -04:00
|
|
|
if !ok {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Failed to find protocol %s for interface %s\n", p, i.Name)
|
2019-05-23 16:31:47 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
w.AddProtocolMethods(i, prot)
|
2019-06-11 16:41:21 -04:00
|
|
|
}
|
|
|
|
Disambiguate(i.ClassMethods)
|
|
|
|
Disambiguate(i.InstanceMethods)
|
|
|
|
fmt.Printf("Interface %s: %d properties, %d class methods, %d instance methods\n", i.Name, len(i.Properties), len(i.ClassMethods.Methods), len(i.InstanceMethods.Methods))
|
|
|
|
for _, m := range i.ClassMethods.Methods {
|
|
|
|
w.ProcessMethod(m)
|
|
|
|
}
|
|
|
|
for _, m := range i.InstanceMethods.Methods {
|
|
|
|
w.ProcessMethod(m)
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, m := range w.Functions {
|
|
|
|
if Debug {
|
|
|
|
fmt.Printf("Processing function %s %s\n", m.Type.CType(), m.Name)
|
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
w.ProcessFunction(m)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, e := range w.NamedEnums {
|
2019-05-02 14:14:29 -04:00
|
|
|
w.ProcessEnum(e)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, e := range w.AnonEnums {
|
2019-05-02 14:14:29 -04:00
|
|
|
w.ProcessEnum(e)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for n, p := range w.Delegates {
|
|
|
|
w.ProcessDelegate(n, p)
|
2019-05-09 12:25:45 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for n, s := range w.Subclasses {
|
|
|
|
w.ProcessSubclass(n, s)
|
2019-05-21 16:26:52 -04:00
|
|
|
}
|
2019-05-01 10:58:29 -04:00
|
|
|
fmt.Printf("%d functions\n", len(w.Functions))
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("%d enums\n", len(w.NamedEnums)+len(w.AnonEnums))
|
2019-04-26 22:44:30 -04:00
|
|
|
of.WriteString("package " + w.Package + "\n\n")
|
2019-05-09 22:05:04 -04:00
|
|
|
|
2019-06-04 21:18:06 -04:00
|
|
|
arc := " -fno-objc-arc"
|
|
|
|
if Arc {
|
|
|
|
arc = " -fobjc-arc"
|
|
|
|
}
|
|
|
|
w.cgoFlags.WriteString(fmt.Sprintf(`
|
|
|
|
/*
|
2019-06-11 12:38:22 -04:00
|
|
|
#cgo CFLAGS: -x objective-c%s`, arc))
|
2019-06-04 21:18:06 -04:00
|
|
|
ldflags := ""
|
|
|
|
if w.Frameworks != nil && len(w.Frameworks) > 0 {
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, s := range w.Frameworks {
|
|
|
|
w.cImports.WriteString(fmt.Sprintf("#import <%s/%s.h>\n", s, s))
|
2019-06-04 21:18:06 -04:00
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
ldflags = "-framework " + strings.Join(w.Frameworks, " -framework ")
|
2019-06-04 21:18:06 -04:00
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
if len(w.Frameworks) > 0 {
|
|
|
|
w.cImports.WriteString("")
|
|
|
|
}
|
|
|
|
if w.Libraries != nil && len(w.Libraries) > 0 {
|
|
|
|
ldflags = ldflags + "-l" + strings.Join(w.Libraries, " -l")
|
|
|
|
}
|
|
|
|
|
2019-06-04 21:18:06 -04:00
|
|
|
if w.Frameworkdirs != nil && len(w.Frameworkdirs) > 0 {
|
2019-06-11 12:38:22 -04:00
|
|
|
s := strings.Join(w.Frameworkdirs, " -F")
|
2019-06-04 21:18:06 -04:00
|
|
|
w.cgoFlags.WriteString(" -F" + s)
|
|
|
|
ldflags = ldflags + " -F" + s
|
|
|
|
}
|
|
|
|
if ldflags != "" {
|
|
|
|
w.cgoFlags.WriteString(`
|
|
|
|
#cgo LDFLAGS: ` + ldflags)
|
|
|
|
}
|
2019-06-11 12:38:22 -04:00
|
|
|
for _, s := range w.Pragmas {
|
2019-06-04 21:18:06 -04:00
|
|
|
w.cgoFlags.WriteString("\n#pragma " + s + "\n")
|
|
|
|
}
|
|
|
|
|
2019-05-02 14:14:29 -04:00
|
|
|
of.WriteString(w.cgoFlags.String() + "\n")
|
2020-06-24 13:03:53 -04:00
|
|
|
of.WriteString(w.cImports.String())
|
2019-05-09 22:05:04 -04:00
|
|
|
|
2019-04-26 22:44:30 -04:00
|
|
|
of.WriteString(w.cCode.String())
|
2019-06-11 12:38:22 -04:00
|
|
|
imports := []string{}
|
|
|
|
for k := range w.goImports {
|
2020-06-24 13:03:53 -04:00
|
|
|
imports = append(imports, "\t\""+k+"\"")
|
2019-06-11 12:38:22 -04:00
|
|
|
}
|
2019-06-27 11:41:58 -04:00
|
|
|
startThread := ""
|
|
|
|
goInit := ""
|
|
|
|
if w.Interfaces["NSThread"] != nil {
|
|
|
|
startThread = `
|
|
|
|
void
|
|
|
|
NSWrap_init() {
|
|
|
|
[[NSThread new] start]; // put the runtime into multi-threaded mode
|
|
|
|
}
|
|
|
|
`
|
|
|
|
goInit = `
|
|
|
|
func init() {
|
|
|
|
C.NSWrap_init()
|
|
|
|
}
|
|
|
|
`
|
|
|
|
}
|
2020-06-24 13:03:53 -04:00
|
|
|
of.WriteString(fmt.Sprintf(
|
|
|
|
`%s
|
2019-04-10 14:00:48 -04:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
2019-06-11 12:38:22 -04:00
|
|
|
%s
|
2019-04-10 14:00:48 -04:00
|
|
|
)
|
2020-06-24 13:03:53 -04:00
|
|
|
%s`, startThread, strings.Join(imports, "\n"), goInit))
|
2019-04-26 22:44:30 -04:00
|
|
|
of.WriteString(w.goTypes.String())
|
2019-05-02 14:14:29 -04:00
|
|
|
of.WriteString(w.goConst.String())
|
2019-04-26 22:44:30 -04:00
|
|
|
of.WriteString(w.goHelpers.String())
|
|
|
|
of.WriteString(w.goCode.String())
|
|
|
|
of.Close()
|
2019-05-09 22:05:04 -04:00
|
|
|
|
2019-05-24 00:12:46 -04:00
|
|
|
if len(w.Delegates) > 0 || len(w.Subclasses) > 0 {
|
2019-05-10 02:23:07 -04:00
|
|
|
ef.WriteString("package " + w.Package + "\n\n")
|
|
|
|
ef.WriteString(w.cgoFlags.String() + "\n")
|
|
|
|
ef.WriteString(w.cImports.String() + "\n")
|
|
|
|
ef.WriteString(`
|
2019-05-09 22:05:04 -04:00
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
`)
|
2019-05-10 02:23:07 -04:00
|
|
|
ef.WriteString(w.goExports.String())
|
|
|
|
ef.Close()
|
|
|
|
} else {
|
|
|
|
ef.Close()
|
2019-06-11 12:38:22 -04:00
|
|
|
err := os.Remove(path.Join(w.Package, "exports.go"))
|
2019-05-10 02:23:07 -04:00
|
|
|
if err != nil {
|
2019-06-11 12:38:22 -04:00
|
|
|
fmt.Printf("Error removing 'exports.go'. %s\n", err)
|
2019-05-10 02:23:07 -04:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 11:52:21 -04:00
|
|
|
}
|