Fix type wrapping and procesing so duplicate types do not get

declared.
This commit is contained in:
Greg 2019-04-27 22:24:05 -04:00
parent 977a09e77e
commit 500b457d32
4 changed files with 51 additions and 27 deletions

View File

@ -9,10 +9,13 @@ import (
func main() { func main() {
fmt.Println("Started") fmt.Println("Started")
n := ns.StringWithUTF8String(ns.CharFromString("hi there")) n1 := ns.StringWithUTF8String(ns.CharFromString("hi there"))
c := n.CapitalizedString() c := n1.CapitalizedString()
gstring := c.UTF8String().String() gstring := c.UTF8String().String()
fmt.Println(gstring) fmt.Println(gstring)
n2 := ns.StringWithUTF8String(ns.CharFromString("ok bye"))
n3 := ns.StringWithUTF8String(ns.CharFromString("one two three"))
a := ns.ArrayWithObjects(n1)
fmt.Println("ok") fmt.Println("ok")
} }

View File

@ -1,10 +1,20 @@
InputFiles = [ InputFiles = [
"/System/Library/Frameworks/Foundation.framework/Headers/NSDictionary.h", "/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h",
"/System/Library/Frameworks/Foundation.framework/Headers/NSString.h", # "/System/Library/Frameworks/Foundation.framework/Headers/NSDictionary.h",
# "/System/Library/Frameworks/Foundation.framework/Headers/NSString.h",
] ]
Classes = [ Classes = [
"NSString", "NSArray",
"NSDictionary", "NSDictionary",
"NSSet",
"NSDate",
"NSTimeZone",
"NSCalendar",
"NSLocale",
"NSCharacterSet",
"NSString",
"NSScanner",
"NSFileManager",
] ]
SysImports = [ "Foundation/Foundation.h" ] SysImports = [ "Foundation/Foundation.h" ]
Pragma = [ 'clang diagnostic ignored "-Wformat-security"' ] Pragma = [ 'clang diagnostic ignored "-Wformat-security"' ]

View File

@ -92,12 +92,16 @@ func (t *Type) String() string {
return t.Node.String() return t.Node.String()
} }
func (t *Type) PointsTo() *Type {
return NewType(t.Node.PointsTo(), t.Class)
}
func Wrap(s string) { func Wrap(s string) {
if wrapped == nil { if wrapped == nil {
wrapped = make(map[string]bool) wrapped = make(map[string]bool)
} }
// it is the pointers to this type that get wrapped // it is the pointers to this type that get wrapped
wrapped["*" + s] = true wrapped[s] = true
} }
func (t *Type) BaseType() *Type { func (t *Type) BaseType() *Type {
@ -105,9 +109,9 @@ func (t *Type) BaseType() *Type {
t.Node.BaseType(), t.Node.BaseType(),
t.Class, t.Class,
) )
if ret.CType() == ret.Class + " *" { // "instancename" // if ret.CType() == ret.Class + " *" { // "instancename"
ret.ctype = ret.Class // ret.ctype = ret.Class
} // }
return ret return ret
} }
@ -203,15 +207,14 @@ func (t *Type) GoInterfaceDecl() string {
if gt[0] == '*' { if gt[0] == '*' {
gt = gt[1:] // dereference wrapped types gt = gt[1:] // dereference wrapped types
} }
x := ""
super := Super(gt) super := Super(gt)
if super == "" { if super == "" {
super = "ptr unsafe.Pointer" super = "ptr unsafe.Pointer"
} }
return fmt.Sprintf(` return fmt.Sprintf(`
//%s //%s (%s)
%stype %s struct { %s } type %s struct { %s }
`,t.CTypeAttrib(),x,gt,super) `,t.Node.Ctype(),t.BaseType().GoType(),gt,super)
} }
func (t *Type) IsFunction() bool { func (t *Type) IsFunction() bool {
@ -239,8 +242,11 @@ func (t *Type) CToGo(cval string) string { // cast C value to CGo
func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string { func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
var ret strings.Builder var ret strings.Builder
rt := rtype.CType() rt := rtype.CType()
wrap := func(gt string) bool {
return gt != "" && gt[0] == '*' && wrapped[gt[1:]]
}
if rt != "void" { if rt != "void" {
if wrapped[rtype.GoType()] { if wrap(rtype.GoType()) {
ret.WriteString(" ret := &" + rtype.GoType()[1:] + "{}\n") ret.WriteString(" ret := &" + rtype.GoType()[1:] + "{}\n")
ret.WriteString(" ret.ptr = unsafe.Pointer(") ret.WriteString(" ret.ptr = unsafe.Pointer(")
} else { } else {
@ -255,7 +261,7 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
for i := 0; i < len(pnames); i++ { for i := 0; i < len(pnames); i++ {
pn,pt := pnames[i],ptypes[i] pn,pt := pnames[i],ptypes[i]
p := pn p := pn
if wrapped[pt.GoType()] { if wrap(pt.GoType()) {
p = pn + ".ptr" p = pn + ".ptr"
} else { } else {
if pt.Node.IsPointer() { if pt.Node.IsPointer() {
@ -269,7 +275,7 @@ func GoToC(name string, pnames []string, rtype *Type, ptypes []*Type) string {
ret.WriteString(strings.Join(parms,", ")) ret.WriteString(strings.Join(parms,", "))
ret.WriteString(")") ret.WriteString(")")
if rt != "void" { if rt != "void" {
if wrapped[rtype.GoType()] { if wrap(rtype.GoType()) {
ret.WriteString(`) ret.WriteString(`)
return ret return ret
`) `)

View File

@ -299,7 +299,6 @@ func (w *Wrapper) GetParms(n *ast.ObjCMethodDecl,class string) ([]Parameter,bool
return ret, true return ret, true
} }
// new version of ProcessType
func (w *Wrapper) processTypes(tps []*types.Type) { func (w *Wrapper) processTypes(tps []*types.Type) {
switch len(tps) { switch len(tps) {
case 0: case 0:
@ -315,21 +314,27 @@ func (w *Wrapper) processTypes(tps []*types.Type) {
} }
func (w *Wrapper) processType(tp *types.Type) { func (w *Wrapper) processType(tp *types.Type) {
bt := tp.BaseType() gt := tp.GoType()
if w.Processed[bt.GoType()] { return } if gt == "" {
w.Processed[bt.GoType()] = true return
if bt.IsFunction() { }
if gt[0] == '*' {
w.processType(tp.PointsTo())
return
}
if w.Processed[gt] { return }
w.Processed[gt] = true
if gt == "Char" {
w.CharHelpers()
}
if tp.IsFunction() {
return return
} }
w.goTypes.WriteString(tp.GoTypeDecl()) w.goTypes.WriteString(tp.GoTypeDecl())
if tp.GoType() == "*Char" { super := types.Super(gt)
w.CharHelpers()
}
super := types.Super(bt.GoType())
if super != "" { if super != "" {
types.Wrap(super) types.Wrap(super)
pt := types.NewTypeFromString(super + "*","") w.processType(types.NewTypeFromString(super,""))
w.processType(pt)
} }
} }