diff --git a/examples/simple/ClassOne/simple.h b/examples/simple/ClassOne/simple.h index 67cb248..5607b8c 100644 --- a/examples/simple/ClassOne/simple.h +++ b/examples/simple/ClassOne/simple.h @@ -10,7 +10,7 @@ struct stru {int a,b;}; int (*f)(); } -- (ClassOne*) init; +- (instancetype) init; - (int) geti1; - (int *) getp1; - (int (*)()) getf1; @@ -24,5 +24,5 @@ struct stru {int a,b;}; @interface ClassTwo : ClassOne { } -- (ClassTwo*) init; +- (instancetype) init; @end diff --git a/examples/simple/ClassOne/simple.m b/examples/simple/ClassOne/simple.m index 9570ffa..f6aa408 100644 --- a/examples/simple/ClassOne/simple.m +++ b/examples/simple/ClassOne/simple.m @@ -2,7 +2,7 @@ @implementation ClassOne -- (ClassOne*) init +- (instancetype) init { ClassOne *ret; ret = [ClassOne alloc]; @@ -65,7 +65,7 @@ @end @implementation ClassTwo -- (ClassTwo*) init +- (instancetype) init { return [super init]; } diff --git a/types/convert.go b/types/convert.go index 5bbb156..360b659 100644 --- a/types/convert.go +++ b/types/convert.go @@ -32,7 +32,8 @@ var TypeParameters map[string]map[string]string var typedefs map[string]*Type func (t *Type) Typedef() *Type { - return typedefs[t.BaseType().CType()] + //return typedefs[t.BaseType().CType()] + return typedefs[t.CType()] } func init() { @@ -59,46 +60,62 @@ func SetTypeParam(c, n, t string) { } func AddTypedef(n,t string) { + //fmt.Printf("AddTypedef(): %s -> %s\n",n,t) typedefs[n] = NewTypeFromString(t,"") } type Type struct { Node *Node Class string - ctype string + //ctype string Variadic bool } +func clean(n *Node,c string) (*Node,bool) { + ret := NewNode(n.Kind,n.Content) + ret.Children = n.Children + //fmt.Printf("clean(%s,%s)\n",n.Ctype(),c) + recur := false + if TypeParameters[c] != nil { + for k,v := range TypeParameters[c] { + 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 + } + return n,false +} + func NewType(n *Node, c string) *Type { + n2,_ := clean(n, c) return &Type{ - Node: n, + Node: n2, Class: c, - ctype: "", + //ctype: "", } } func NewTypeFromString(t,c string) *Type { + //fmt.Printf("t/c: %s/%s\n",t,c) n,err := Parse(t) + //fmt.Printf("%p %s",n,n.String()) if n.IsId() { - //if n.CtypeSimplified() == "id" { n,err = Parse("NSObject*") } if err != nil { return &Type{} } - if TypeParameters[c] != nil { - recur := false - for k,v := range TypeParameters[c] { - recur = n.renameTypedefs(k,v) - } - if recur { - return NewTypeFromString(n.Ctype(),c) - } + if n2,ok := clean(n, c); ok { + return NewTypeFromString(n2.Ctype(),c) } return &Type{ Node: n, Class: c, - ctype: "", + //ctype: "", } } @@ -166,9 +183,9 @@ func (t *Type) CTypeAttrib() string { func (t *Type) _CType(attrib bool) string { //if !attrib && c.ctype != "" ... FIXME? - if t.ctype != "" { // cache - return t.ctype - } + //if t.ctype != "" { // cache + // return t.ctype + //} var ct string if attrib { ignore := map[string]bool { "GenericList": true } @@ -176,19 +193,22 @@ func (t *Type) _CType(attrib bool) string { } else { ct = t.Node.CtypeSimplified() } + /* ct = strings.ReplaceAll(ct,"instancename",t.Class) ct = strings.ReplaceAll(ct,"instancetype",t.Class + " *") + */ if len(ct) > 1 && ct[:2] == "id" { ct = "NSObject*" + ct[2:] } + /* if len(ct) > 11 { if ct[:12] == "instancename" { ct = t.Class + ct[12:] } if ct[:12] == "instancetype" { ct = t.Class + ct[12:] + " *" } - } + }*/ if attrib { t._CType(false) - } else { - t.ctype = ct + //} else { + //t.ctype = ct } return ct } diff --git a/types/cparser.go b/types/cparser.go index 28b36ce..0171ec9 100644 --- a/types/cparser.go +++ b/types/cparser.go @@ -95,6 +95,7 @@ func init() { } return s2,n2 } + TypeName = _TypeName } func _TypeName(s string, n *Node) (string, *Node) { diff --git a/types/main.go b/types/main.go index 5053102..0e01f32 100644 --- a/types/main.go +++ b/types/main.go @@ -28,6 +28,7 @@ func (n *Node) HasFunc() bool { func Parse(s string) (*Node, error) { s2, n := TypeName(s,NewNode("AST")) + //fmt.Printf("%p Parsed %s\n",n,s) if s2 != "" { return n,fmt.Errorf("Parse failed or incomplete. Remainder: %s",s2) } diff --git a/wrap/main.go b/wrap/main.go index 93e1eaa..fcf3017 100644 --- a/wrap/main.go +++ b/wrap/main.go @@ -229,13 +229,14 @@ func (w *Wrapper) add(name string, ns []ast.Node) { i.Properties[p.Name] = p //} case *ast.ObjCMethodDecl: - //fmt.Printf("ObjCMethodDecl: %s\n",x.Name) + //fmt.Printf("ObjCMethodDecl: %s (%s) %s\n",x.Type,name,x.Name) m := Method{ Name: x.Name, Type: types.NewTypeFromString(x.Type,name), Class: name, ClassMethod: x.ClassMethod, } + //fmt.Println(m.Type.Node.String()) m.Parameters, avail = w.GetParms(x,name) if avail { i.Methods[m.Name] = m @@ -448,8 +449,9 @@ New%s() { grtype = "" } w.goCode.WriteString(fmt.Sprintf(` +//%s func %s(%s) %s { -`,gname,gplist,grtype)) +`,m.Type.CType(),gname,gplist,grtype)) lparm := len(tps)-1 if len(tps) > 0 && tps[lparm].Variadic { vn := ns[lparm]