From 511f2f19686c07a3c53093fd5575feda070db5cc Mon Sep 17 00:00:00 2001 From: Greg Date: Mon, 6 May 2019 13:21:36 -0400 Subject: [PATCH] Improve type handling for enums. Handle "__kindof" qualifiers. Add examples/app. Allow VisibilityAttr "Hidden". --- .gitignore | 2 ++ ast/visibility_attr.go | 3 +++ examples/app/main.go | 22 +++++++++++++++++++ examples/app/nswrap.toml | 39 +++++++++++++++++++++++++++++++++ examples/foundation/main.go | 3 ++- examples/foundation/nswrap.toml | 1 + examples/simple/main.go | 4 ++-- types/convert.go | 5 +++-- types/cparser.go | 1 + types/main.go | 19 +++++++++++----- types/node.go | 1 + wrap/main.go | 29 ++++++++++++------------ 12 files changed, 105 insertions(+), 24 deletions(-) create mode 100644 examples/app/main.go create mode 100644 examples/app/nswrap.toml diff --git a/.gitignore b/.gitignore index d3a2cb1..fb29ff4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ clast ast.txt *.ast +examples/app/app +examples/app/ns examples/bluetooth/bluetooth examples/bluetooth/ble/main.go examples/foundation/foundation diff --git a/ast/visibility_attr.go b/ast/visibility_attr.go index 0d91743..7549d48 100644 --- a/ast/visibility_attr.go +++ b/ast/visibility_attr.go @@ -7,6 +7,7 @@ type VisibilityAttr struct { ChildNodes []Node IsDefault bool IsInherited bool + IsHidden bool } func parseVisibilityAttr(line string) *VisibilityAttr { @@ -14,6 +15,7 @@ func parseVisibilityAttr(line string) *VisibilityAttr { `<(?P.*)> (?P Inherited)? (?P Default)? + (?P Hidden)? `, line, ) @@ -24,6 +26,7 @@ func parseVisibilityAttr(line string) *VisibilityAttr { ChildNodes: []Node{}, IsDefault: len(groups["default"]) > 0, IsInherited: len(groups["inherited"]) > 0, + IsHidden: len(groups["hidden"]) > 0, } } diff --git a/examples/app/main.go b/examples/app/main.go new file mode 100644 index 0000000..2a0a928 --- /dev/null +++ b/examples/app/main.go @@ -0,0 +1,22 @@ +package main + +import ( + "gitlab.wow.st/gmp/nswrap/examples/app/ns" +) + +func main() { + a := ns.NSApplicationSharedApplication() + a.SetActivationPolicy(ns.NSApplicationActivationPolicyRegular) + //w := ns.NSWindowAlloc() + w := ns.NSWindowAlloc().InitWithContentRect( + ns.NSMakeRect(200,200,600,600), + ns.NSWindowStyleMaskTitled, + ns.NSBackingStoreBuffered, + 0, + nil, + ) + w.SetTitle(ns.NSStringWithGoString("Hi World")) + w.MakeKeyAndOrderFront(w) + a.Run() +} + diff --git a/examples/app/nswrap.toml b/examples/app/nswrap.toml new file mode 100644 index 0000000..0458761 --- /dev/null +++ b/examples/app/nswrap.toml @@ -0,0 +1,39 @@ +InputFiles = [ + "/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h", + "/System/Library/Frameworks/AppKit.framework/Headers/AppKit.h", +] +Classes = [ + "NSArray", + "NSMutableArray", + "NSDictionary", + "NSEnumerator", + "NSSet", + "NSDate", + "NSTimeZone", + "NSCalendar", + "NSLocale", + "NSCharacterSet", + "NSString", + "NSScanner", + "NSFileManager", + "NSApplication", + "NSBundle", + "NSApp", + "NSWindow", + "NSScreen", + "NSEvent", + "NSResponder", +] +Functions = [ + "NSApplicationMain", + "NSMake.*", +] +Enums = [ + "CF.*", + "NSApplication.*", + "NSBackingStore.*", + "NSWindowStyleMask.*", +] +Frameworks = [ "Foundation", "AppKit", "CoreGraphics" ] +Pragma = [ 'clang diagnostic ignored "-Wformat-security"' ] +VaArgs = 32 diff --git a/examples/foundation/main.go b/examples/foundation/main.go index 8cae95e..ca683c4 100644 --- a/examples/foundation/main.go +++ b/examples/foundation/main.go @@ -12,7 +12,8 @@ func main() { c1 := n1.CapitalizedString() gs := c1.UTF8String().String() fmt.Println(gs) - n2 := ns.NSStringWithGoString("hi world") + n2 := ns.NSStringAlloc() + n2 = n2.InitWithGoString("hi world") n3 := ns.NSStringWithGoString("ok bye") a := ns.NSMutableArrayWithObjects(n1,n2,n3) fmt.Println("Length(a) = ",a.Count()) diff --git a/examples/foundation/nswrap.toml b/examples/foundation/nswrap.toml index decb2c1..35b7c01 100644 --- a/examples/foundation/nswrap.toml +++ b/examples/foundation/nswrap.toml @@ -20,6 +20,7 @@ Functions = [ "NSMakeRange", ] Enums = [ + "P_ALL", "CF.*", ] Frameworks = [ "Foundation" ] diff --git a/examples/simple/main.go b/examples/simple/main.go index 4bb933d..00bb9ae 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -7,7 +7,7 @@ import ( ) func main() { - o := ns.NewClassOne().Init() + o := ns.ClassOneAlloc().Init() fmt.Println("i1 = ",o.Geti1()) fmt.Println("p1 = ",o.Getp1()) p1 := o.Getp1() @@ -18,7 +18,7 @@ func main() { np := o.Nstru2() fmt.Println(o.Hi1(ns1)) fmt.Println(o.Hi2(np)) - o2 := ns.NewClassTwo().Init() + o2 := ns.ClassTwoAlloc().Init() fmt.Println(o2.Hi1(ns1)) } diff --git a/types/convert.go b/types/convert.go index 7f42661..721c7bf 100644 --- a/types/convert.go +++ b/types/convert.go @@ -103,8 +103,6 @@ func clean(n *Node,c string) (*Node,bool) { recur = ret.renameTypedefs(k,v) } } -// recur = recur || ret.renameTypedefs("instancename",c) -// recur = recur || ret.renameTypedefs("instancetype",c + "*") if recur { clean(n, c) return ret,true @@ -199,6 +197,9 @@ func _goType(ct string) string { if ct == "Id" { ct = "*Id" } + if len(ct) > 4 && ct[len(ct)-4:len(ct)] == "Void" { + ct = ct[:len(ct)-5] + "unsafe.Pointer" + } return ct } diff --git a/types/cparser.go b/types/cparser.go index d7e0015..cb2a695 100644 --- a/types/cparser.go +++ b/types/cparser.go @@ -329,6 +329,7 @@ func BareTypedefName(s string, n *Node) (string, *Node) { func TypedefName(s string, n *Node) (string, *Node) { return Seq( + Opt(NodeNamed("KindQualifier",Lit("__kindof"))), BareTypedefName, Opt(AngBracketed(GenericList)), Opt(NullableAnnotation), diff --git a/types/main.go b/types/main.go index 817c574..c2a4fad 100644 --- a/types/main.go +++ b/types/main.go @@ -120,7 +120,10 @@ func (n *Node) IsStruct() bool { if n == nil || len(n.Children) < 1 { return false } - return n.Children[0].Kind == "Struct" + i := 0 + for ; i %s\n",n.Type,tp.CType()) } e := &Enum{ Name: n.Name, // NOTE: may be empty string @@ -490,16 +490,8 @@ func (w *Wrapper) GetParms(n ast.Node,class string) ([]*Parameter,bool) { } func (w *Wrapper) processTypes(tps []*types.Type) { - switch len(tps) { - case 0: - return - case 1: - w.processType(tps[0]) - default: - for _,tp := range tps { - w.processType(tp) - } - return + for _,tp := range tps { + w.processType(tp) } } @@ -704,17 +696,26 @@ func gStringToNsstring(s string) string { func (w *Wrapper) ProcessEnum(e *Enum) { + //fmt.Printf("Processing enum (%s)\n",e.Name) gtp := "" - if e.Type != nil { + ctp := "" + if e.Name != "" { + gtp = e.Name + ctp = "C." + e.Name + } else { gtp = e.Type.GoType() + ctp = e.Type.CGoType() + } + if e.Type != nil { if !w.Processed[gtp] { w.goTypes.WriteString(fmt.Sprintf(` -type %s C.%s -`,gtp,e.Type.CType())) +type %s %s +`,gtp,ctp)) w.Processed[gtp] = true } } gtp = gtp + " " + //fmt.Printf(" gtp = %s; ctp = %s\n",gtp,ctp) for _,c := range e.Constants { w.goConst.WriteString(fmt.Sprintf(` const %s %s= C.%s