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
ast.txt
*.ast
examples/app/app
examples/app/ns
examples/bluetooth/bluetooth
examples/bluetooth/ble/main.go
examples/foundation/foundation

View File

@ -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<position>.*)>
(?P<inherited> Inherited)?
(?P<default> Default)?
(?P<hidden> 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,
}
}

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<len(n.Children) &&
n.Children[i].Kind == "KindQualifier"; i++ {}
return n.Children[i].Kind == "Struct"
}
func (n *Node) IsFunction() bool {
@ -146,17 +149,23 @@ func (n *Node) IsId() bool {
if n == nil || len(n.Children) < 1 {
return false
}
i := 0
for ; i < len(n.Children) &&
n.Children[i].Kind == "KindQualifier"; i++ {}
return !n.IsFunction() &&
n.Children[0].Kind == "TypedefName" &&
n.Children[0].Content == "id"
n.Children[i].Kind == "TypedefName" &&
n.Children[i].Content == "id"
}
func (n *Node) IsInstancetype() bool {
if n == nil || len(n.Children) < 1 {
return false
}
return n.Children[0].Kind == "TypedefName" &&
n.Children[0].Content == "instancetype"
i := 0
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

View File

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

View File

@ -289,7 +289,7 @@ func (w *Wrapper) AddEnum(n *ast.EnumDecl,rs []string) {
tp = nil
} else {
tp = types.NewTypeFromString(n.Type,"")
//fmt.Printf(" type: %s\n",tp.CType())
//fmt.Printf(" type: %s -> %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