Improve type handling for enums. Handle "__kindof" qualifiers.

Add examples/app. Allow VisibilityAttr "Hidden".
This commit is contained in:
Greg 2019-05-06 13:21:36 -04:00
parent 959b87342e
commit 511f2f1968
12 changed files with 105 additions and 24 deletions

2
.gitignore vendored
View File

@ -1,6 +1,8 @@
clast clast
ast.txt ast.txt
*.ast *.ast
examples/app/app
examples/app/ns
examples/bluetooth/bluetooth examples/bluetooth/bluetooth
examples/bluetooth/ble/main.go examples/bluetooth/ble/main.go
examples/foundation/foundation examples/foundation/foundation

View File

@ -7,6 +7,7 @@ type VisibilityAttr struct {
ChildNodes []Node ChildNodes []Node
IsDefault bool IsDefault bool
IsInherited bool IsInherited bool
IsHidden bool
} }
func parseVisibilityAttr(line string) *VisibilityAttr { func parseVisibilityAttr(line string) *VisibilityAttr {
@ -14,6 +15,7 @@ func parseVisibilityAttr(line string) *VisibilityAttr {
`<(?P<position>.*)> `<(?P<position>.*)>
(?P<inherited> Inherited)? (?P<inherited> Inherited)?
(?P<default> Default)? (?P<default> Default)?
(?P<hidden> Hidden)?
`, `,
line, line,
) )
@ -24,6 +26,7 @@ func parseVisibilityAttr(line string) *VisibilityAttr {
ChildNodes: []Node{}, ChildNodes: []Node{},
IsDefault: len(groups["default"]) > 0, IsDefault: len(groups["default"]) > 0,
IsInherited: len(groups["inherited"]) > 0, IsInherited: len(groups["inherited"]) > 0,
IsHidden: len(groups["hidden"]) > 0,
} }
} }

22
examples/app/main.go Normal file
View File

@ -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()
}

39
examples/app/nswrap.toml Normal file
View File

@ -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

View File

@ -12,7 +12,8 @@ func main() {
c1 := n1.CapitalizedString() c1 := n1.CapitalizedString()
gs := c1.UTF8String().String() gs := c1.UTF8String().String()
fmt.Println(gs) fmt.Println(gs)
n2 := ns.NSStringWithGoString("hi world") n2 := ns.NSStringAlloc()
n2 = n2.InitWithGoString("hi world")
n3 := ns.NSStringWithGoString("ok bye") n3 := ns.NSStringWithGoString("ok bye")
a := ns.NSMutableArrayWithObjects(n1,n2,n3) a := ns.NSMutableArrayWithObjects(n1,n2,n3)
fmt.Println("Length(a) = ",a.Count()) fmt.Println("Length(a) = ",a.Count())

View File

@ -20,6 +20,7 @@ Functions = [
"NSMakeRange", "NSMakeRange",
] ]
Enums = [ Enums = [
"P_ALL",
"CF.*", "CF.*",
] ]
Frameworks = [ "Foundation" ] Frameworks = [ "Foundation" ]

View File

@ -7,7 +7,7 @@ import (
) )
func main() { func main() {
o := ns.NewClassOne().Init() o := ns.ClassOneAlloc().Init()
fmt.Println("i1 = ",o.Geti1()) fmt.Println("i1 = ",o.Geti1())
fmt.Println("p1 = ",o.Getp1()) fmt.Println("p1 = ",o.Getp1())
p1 := o.Getp1() p1 := o.Getp1()
@ -18,7 +18,7 @@ func main() {
np := o.Nstru2() np := o.Nstru2()
fmt.Println(o.Hi1(ns1)) fmt.Println(o.Hi1(ns1))
fmt.Println(o.Hi2(np)) fmt.Println(o.Hi2(np))
o2 := ns.NewClassTwo().Init() o2 := ns.ClassTwoAlloc().Init()
fmt.Println(o2.Hi1(ns1)) fmt.Println(o2.Hi1(ns1))
} }

View File

@ -103,8 +103,6 @@ func clean(n *Node,c string) (*Node,bool) {
recur = ret.renameTypedefs(k,v) recur = ret.renameTypedefs(k,v)
} }
} }
// recur = recur || ret.renameTypedefs("instancename",c)
// recur = recur || ret.renameTypedefs("instancetype",c + "*")
if recur { if recur {
clean(n, c) clean(n, c)
return ret,true return ret,true
@ -199,6 +197,9 @@ func _goType(ct string) string {
if ct == "Id" { if ct == "Id" {
ct = "*Id" ct = "*Id"
} }
if len(ct) > 4 && ct[len(ct)-4:len(ct)] == "Void" {
ct = ct[:len(ct)-5] + "unsafe.Pointer"
}
return ct return ct
} }

View File

@ -329,6 +329,7 @@ func BareTypedefName(s string, n *Node) (string, *Node) {
func TypedefName(s string, n *Node) (string, *Node) { func TypedefName(s string, n *Node) (string, *Node) {
return Seq( return Seq(
Opt(NodeNamed("KindQualifier",Lit("__kindof"))),
BareTypedefName, BareTypedefName,
Opt(AngBracketed(GenericList)), Opt(AngBracketed(GenericList)),
Opt(NullableAnnotation), Opt(NullableAnnotation),

View File

@ -120,7 +120,10 @@ func (n *Node) IsStruct() bool {
if n == nil || len(n.Children) < 1 { if n == nil || len(n.Children) < 1 {
return false return false
} }
return n.Children[0].Kind == "Struct" i := 0
for ; i<len(n.Children) &&
n.Children[i].Kind == "KindQualifier"; i++ {}
return n.Children[i].Kind == "Struct"
} }
func (n *Node) IsFunction() bool { func (n *Node) IsFunction() bool {
@ -146,17 +149,23 @@ func (n *Node) IsId() bool {
if n == nil || len(n.Children) < 1 { if n == nil || len(n.Children) < 1 {
return false return false
} }
i := 0
for ; i < len(n.Children) &&
n.Children[i].Kind == "KindQualifier"; i++ {}
return !n.IsFunction() && return !n.IsFunction() &&
n.Children[0].Kind == "TypedefName" && n.Children[i].Kind == "TypedefName" &&
n.Children[0].Content == "id" n.Children[i].Content == "id"
} }
func (n *Node) IsInstancetype() bool { func (n *Node) IsInstancetype() bool {
if n == nil || len(n.Children) < 1 { if n == nil || len(n.Children) < 1 {
return false return false
} }
return n.Children[0].Kind == "TypedefName" && i := 0
n.Children[0].Content == "instancetype" for ; i < len(n.Children) &&
n.Children[i].Kind == "KindQualifier"; i++ {}
return n.Children[i].Kind == "TypedefName" &&
n.Children[i].Content == "instancetype"
} }
//BaseType strips off all layers of pointer indirection //BaseType strips off all layers of pointer indirection

View File

@ -88,6 +88,7 @@ func (n *Node) renameTypedefs(a,b string) (ret bool) {
func (n *Node) CtypeSimplified() string { func (n *Node) CtypeSimplified() string {
ignore := map[string]bool{ ignore := map[string]bool{
"NullableAnnotation": true, "NullableAnnotation": true,
"KindQualifier": true,
"TypeQualifier": true, "TypeQualifier": true,
"GenericList": true, "GenericList": true,
} }

View File

@ -289,7 +289,7 @@ func (w *Wrapper) AddEnum(n *ast.EnumDecl,rs []string) {
tp = nil tp = nil
} else { } else {
tp = types.NewTypeFromString(n.Type,"") tp = types.NewTypeFromString(n.Type,"")
//fmt.Printf(" type: %s\n",tp.CType()) //fmt.Printf(" type: %s -> %s\n",n.Type,tp.CType())
} }
e := &Enum{ e := &Enum{
Name: n.Name, // NOTE: may be empty string 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) { func (w *Wrapper) processTypes(tps []*types.Type) {
switch len(tps) { for _,tp := range tps {
case 0: w.processType(tp)
return
case 1:
w.processType(tps[0])
default:
for _,tp := range tps {
w.processType(tp)
}
return
} }
} }
@ -704,17 +696,26 @@ func gStringToNsstring(s string) string {
func (w *Wrapper) ProcessEnum(e *Enum) { func (w *Wrapper) ProcessEnum(e *Enum) {
//fmt.Printf("Processing enum (%s)\n",e.Name)
gtp := "" gtp := ""
if e.Type != nil { ctp := ""
if e.Name != "" {
gtp = e.Name
ctp = "C." + e.Name
} else {
gtp = e.Type.GoType() gtp = e.Type.GoType()
ctp = e.Type.CGoType()
}
if e.Type != nil {
if !w.Processed[gtp] { if !w.Processed[gtp] {
w.goTypes.WriteString(fmt.Sprintf(` w.goTypes.WriteString(fmt.Sprintf(`
type %s C.%s type %s %s
`,gtp,e.Type.CType())) `,gtp,ctp))
w.Processed[gtp] = true w.Processed[gtp] = true
} }
} }
gtp = gtp + " " gtp = gtp + " "
//fmt.Printf(" gtp = %s; ctp = %s\n",gtp,ctp)
for _,c := range e.Constants { for _,c := range e.Constants {
w.goConst.WriteString(fmt.Sprintf(` w.goConst.WriteString(fmt.Sprintf(`
const %s %s= C.%s const %s %s= C.%s