Fix handling of wrapped types. Use new type system for go
function declarations. Working wrapper for simple test case.
This commit is contained in:
parent
e143dbb6a7
commit
98e19ca726
|
@ -89,13 +89,18 @@ func (t *Type) CGoType() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Type) GoType() string {
|
func (t *Type) GoType() string {
|
||||||
ct := swapstars(t.CType())
|
return _goType(t.CType())
|
||||||
|
}
|
||||||
|
|
||||||
|
func _goType(ct string) string {
|
||||||
|
ct = swapstars(ct)
|
||||||
ct = strings.Title(ct)
|
ct = strings.Title(ct)
|
||||||
ct = strings.ReplaceAll(ct," ","")
|
ct = strings.ReplaceAll(ct," ","")
|
||||||
ct = strings.ReplaceAll(ct,"Struct","")
|
ct = strings.ReplaceAll(ct,"Struct","")
|
||||||
return ct
|
return ct
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (t *Type) CType() string {
|
func (t *Type) CType() string {
|
||||||
if t.ctype != "" { // cache
|
if t.ctype != "" { // cache
|
||||||
return t.ctype
|
return t.ctype
|
||||||
|
@ -116,22 +121,32 @@ func (t *Type) CType() string {
|
||||||
|
|
||||||
func (t *Type) GoTypeDecl() string {
|
func (t *Type) GoTypeDecl() string {
|
||||||
if wrapped[t.GoType()] {
|
if wrapped[t.GoType()] {
|
||||||
fmt.Printf("%s -> %s: %s is wrapped\n",t.GoType(),t.CGoType(),t.Class)
|
|
||||||
return t.GoInterfaceDecl()
|
return t.GoInterfaceDecl()
|
||||||
}
|
}
|
||||||
|
tp := t.BaseType()
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
type %s %s
|
type %s %s
|
||||||
`,t.GoType(),t.CGoType())
|
`,tp.GoType(),tp.CGoType())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Type) GoInterfaceDecl() string {
|
func (t *Type) GoInterfaceDecl() string {
|
||||||
super := Super(t.Class)
|
return _goInterfaceDecl(t.GoType())
|
||||||
|
}
|
||||||
|
|
||||||
|
func _goInterfaceDecl(c string) string {
|
||||||
|
if c[0] == '*' {
|
||||||
|
c = c[1:] // dereference wrapped types
|
||||||
|
}
|
||||||
|
x := ""
|
||||||
|
super := Super(c)
|
||||||
if super == "" {
|
if super == "" {
|
||||||
super = "ptr unsafe.Pointer"
|
super = "ptr unsafe.Pointer"
|
||||||
|
} else {
|
||||||
|
x = _goInterfaceDecl(super) + "\n"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(`
|
return fmt.Sprintf(`
|
||||||
type %s struct { %s }
|
%stype %s struct { %s }
|
||||||
`,t.GoType(), super)
|
`,x,c,super)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Type) CToGo(cval string) string { // cast C value to CGo
|
func (t *Type) CToGo(cval string) string { // cast C value to CGo
|
||||||
|
|
25
wrap/main.go
25
wrap/main.go
|
@ -238,9 +238,13 @@ var goreserved map[string]bool = map[string]bool{
|
||||||
"range": true,
|
"range": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
func gpntp(m Method) ([]string,[]*types.Type) {
|
func (m *Method) gpntp() ([]string,[]*types.Type,string) {
|
||||||
ns := []string{}
|
ns := []string{}
|
||||||
tps := []*types.Type{}
|
tps := []*types.Type{}
|
||||||
|
if !m.ClassMethod {
|
||||||
|
ns = append(ns,"o")
|
||||||
|
tps = append(tps,types.NewTypeFromString(m.Class + "*",""))
|
||||||
|
}
|
||||||
for _,p := range m.Parameters {
|
for _,p := range m.Parameters {
|
||||||
gname := p.Vname
|
gname := p.Vname
|
||||||
if goreserved[gname] {
|
if goreserved[gname] {
|
||||||
|
@ -249,7 +253,13 @@ func gpntp(m Method) ([]string,[]*types.Type) {
|
||||||
ns = append(ns,gname)
|
ns = append(ns,gname)
|
||||||
tps = append(tps,p.Tp)
|
tps = append(tps,p.Tp)
|
||||||
}
|
}
|
||||||
return ns,tps
|
ret := []string{}
|
||||||
|
i := 0
|
||||||
|
if !m.ClassMethod { i = 1 }
|
||||||
|
for ; i < len(ns); i++ {
|
||||||
|
ret = append(ret,ns[i] + " " + tps[i].GoType())
|
||||||
|
}
|
||||||
|
return ns, tps, strings.Join(ret,", ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w Wrapper) goparamlist(m Method) (string,[]string,bool) {
|
func (w Wrapper) goparamlist(m Method) (string,[]string,bool) {
|
||||||
|
@ -475,14 +485,12 @@ func (w *Wrapper) pt2(tps ...*types.Type) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tp := tps[0].BaseType()
|
tp := tps[0].BaseType()
|
||||||
if w.Processed2[tp.GoType()] {
|
if w.Processed2[tp.GoType()] { return }
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Processed2[tp.GoType()] = true
|
w.Processed2[tp.GoType()] = true
|
||||||
if tp.Node.IsFunction() {
|
if tp.Node.IsFunction() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.goTypes.WriteString(tp.GoTypeDecl())
|
w.goTypes.WriteString(tps[0].GoTypeDecl())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Wrapper) ProcessType(gotype string) {
|
func (w *Wrapper) ProcessType(gotype string) {
|
||||||
|
@ -593,16 +601,15 @@ New%s() {
|
||||||
|
|
||||||
w.AddType(m.Type,i.Name)
|
w.AddType(m.Type,i.Name)
|
||||||
//cmtype := w.ctMap[w.goType(m.Type,i.Name)].CtypeSimplified()
|
//cmtype := w.ctMap[w.goType(m.Type,i.Name)].CtypeSimplified()
|
||||||
gplist,_,_ := w.goparamlist(m)
|
|
||||||
cmtype := m.Tp.CType()
|
cmtype := m.Tp.CType()
|
||||||
ns,tps := gpntp(m)
|
ns,tps,gplist := m.gpntp()
|
||||||
w.pt2(tps...)
|
w.pt2(tps...)
|
||||||
w.pt2(m.Tp)
|
w.pt2(m.Tp)
|
||||||
w.goCode.WriteString(fmt.Sprintf(`
|
w.goCode.WriteString(fmt.Sprintf(`
|
||||||
func (o *%s) %s(%s) %s {
|
func (o *%s) %s(%s) %s {
|
||||||
`,i.Name,gname,gplist,m.Tp.GoType()))
|
`,i.Name,gname,gplist,m.Tp.GoType()))
|
||||||
w.goCode.WriteString(
|
w.goCode.WriteString(
|
||||||
types.GoToC(cname,ns,m.Tp,tps) + "}\n")
|
types.GoToC(cname,ns,m.Tp,tps) + "}\n\n")
|
||||||
|
|
||||||
cret := ""
|
cret := ""
|
||||||
if !m.isVoid() {
|
if !m.isVoid() {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user