From 38c7856b8931ff7038793a16046ec4fd55b990e3 Mon Sep 17 00:00:00 2001 From: Greg Date: Fri, 24 May 2019 00:12:46 -0400 Subject: [PATCH] Bug fix and start improvements to subclassing. --- cmd/nswrap/main.go | 4 ++-- examples/simple/main.go | 10 +++++++++- examples/simple/nswrap.yaml | 4 +++- types/convert.go | 4 ++++ wrap/main.go | 27 +++++++++++++++++++++++---- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/cmd/nswrap/main.go b/cmd/nswrap/main.go index a29d16e..9603f96 100644 --- a/cmd/nswrap/main.go +++ b/cmd/nswrap/main.go @@ -216,8 +216,8 @@ func Start() (err error) { case *ast.ObjCInterfaceDecl: w.AddInterface(x) for _,ss := range Config.Subclasses { - for ps,_ := range ss { - if matches(x.Name,[]string{ps}) { + if sc,ok := ss["superclass"]; ok { + if matches(x.Name,sc) { Config.Classes = append(Config.Classes,x.Name) } } diff --git a/examples/simple/main.go b/examples/simple/main.go index 29acf4e..47ef3b8 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -6,6 +6,11 @@ import ( ns "gitlab.wow.st/gmp/nswrap/examples/simple/ClassOne" ) +func cb(super ns.ClassThreeSupermethods) ns.Int { + fmt.Printf("In Go callback\n") + return 0 +} + func main() { o := ns.ClassOneAlloc().Init() fmt.Println("i1 = ",o.Geti1()) @@ -20,7 +25,10 @@ func main() { fmt.Println(o.Hi2(np)) o2 := ns.ClassTwoAlloc().Init() fmt.Println(o2.Hi1(ns1)) - o3 := ns.ClassThreeAlloc().Init() + o3 := ns.ClassThreeAlloc() + o3.Init() + o3.Geti1Callback(cb) fmt.Println(o3.Hi2(np)) + fmt.Println(o3.Geti1()) } diff --git a/examples/simple/nswrap.yaml b/examples/simple/nswrap.yaml index 36ebeba..4b9fea9 100644 --- a/examples/simple/nswrap.yaml +++ b/examples/simple/nswrap.yaml @@ -6,6 +6,8 @@ classes: subclasses: ClassThree: ClassTwo: - - .* + - geti1 + - (void)myMethod:int + imports: [ simple.h ] frameworks: [ Foundation ] diff --git a/types/convert.go b/types/convert.go index 13c243b..fc5fac2 100644 --- a/types/convert.go +++ b/types/convert.go @@ -24,6 +24,7 @@ func IsGoInterface(gt string) bool { return goInterfaces[gt] } + //TypeParameters maps, for each class, a TypedefName to a type, representing //the Objective-C type parameters for that class var TypeParameters map[string]map[string]string @@ -194,6 +195,9 @@ func _goType(ct string) string { if ct == "Id" { ct = "Id" } + if len(ct) > 1 && ShouldWrap(ct[1:]) { + return ct[1:] + } if len(ct) > 4 && ct[len(ct)-4:len(ct)] == "Void" { ct = ct[:len(ct)-5] + "unsafe.Pointer" } diff --git a/wrap/main.go b/wrap/main.go index 23a3162..20ada68 100644 --- a/wrap/main.go +++ b/wrap/main.go @@ -981,14 +981,30 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string,sub bool) i++ var ms map[string]*Method if sub { + if i > 1 { + fmt.Printf("Multiple inheritance is not permitted:\n subclass %s already inherits from %s\n",dname,supr) + os.Exit(-1) + } interf := w.Interfaces[pname] if interf == nil { fmt.Printf("Failed to find interface %s for subclass %s\n",pname,dname) os.Exit(-1) } - //fmt.Printf(" subclass for %s\n",pname) - ms = interf.InstanceMethods supr = interf.GoName + //fmt.Printf(" subclass for %s\n",pname) + ms = map[string]*Method{} + var addmeths func(s string) + addmeths = func(s string) { + if sup := types.Super(s); w.Interfaces[sup] != nil { + addmeths(sup) + } + fmt.Printf("Adding methods for %s\n",s) + for k,v := range w.Interfaces[s].InstanceMethods { + ms[k] = v + } + } + //for subclasses, add all superclass methods, depth first + addmeths(interf.Name) } else { proto := w.Protocols[pname] if proto == nil { @@ -1003,6 +1019,9 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string,sub bool) //note:we may have capitalized the first character to make a goname, //but m.Name is not disambiguated for polymorphics... if !matches(string(m.Name[0])+gname[1:],pats) { + if sub { + //Add newly defined methods that don't match anything... + } continue } if m.Type.IsFunction() || m.Type.IsFunctionPtr() || m.hasFunctionParam() { @@ -1299,7 +1318,7 @@ func (d *%s) %sCallback(f func(%s)%s) { sper := "" if sub && len(gnames) > 0 { sper = fmt.Sprintf( -`self := (*%s)(o) +` self := (*%s)(o) super := %sSupermethods{ %s, } @@ -1486,7 +1505,7 @@ import ( of.WriteString(w.goCode.String()) of.Close() - if len(w.Delegates) > 0 { + if len(w.Delegates) > 0 || len(w.Subclasses) > 0 { ef.WriteString("package " + w.Package + "\n\n") ef.WriteString(w.cgoFlags.String() + "\n") ef.WriteString(w.cImports.String() + "\n")