Skip to content

Commit

Permalink
Kinda proper fix for #42 related issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlab committed Jan 5, 2018
1 parent de96262 commit f5ababa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion generator/gen_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ func (gen *Generator) writeFunctionBody(wr io.Writer, decl *tl.CDecl) {
ptrTip = tl.TipPtrRef
}
goSpec := gen.tr.TranslateSpec((*spec).Return, ptrTip, typeTip)
cgoSpec := gen.tr.CGoSpec((*spec).Return, true) // asArg?
cgoSpec := gen.tr.CGoSpec((*spec).Return, false)

retProxy, nillable := gen.proxyRetToGo(memTipRx.Self(), "__v", "__ret", goSpec, cgoSpec)
if nillable {
Expand Down
2 changes: 1 addition & 1 deletion generator/gen_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (gen *Generator) writeCallbackProxyFunc(wr io.Writer, decl *tl.CDecl) {
var returnRef string
funcSpec := decl.Spec.(*tl.CFunctionSpec)
if funcSpec.Return != nil {
cgoSpec := gen.tr.CGoSpec(funcSpec.Return, true) // asArg?
cgoSpec := gen.tr.CGoSpec(funcSpec.Return, false)
returnRef = cgoSpec.String()
}
fmt.Fprintf(wr, "func %s", decl.Name)
Expand Down
9 changes: 9 additions & 0 deletions translator/ast_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,13 @@ func (t *Translator) typeSpec(typ cc.Type, deep int, isRet bool) CType {
spec.AddOuterArr(uint64(size))
}
}
var isVoidPtr bool
for typ.Kind() == cc.Ptr {
if next := typ.Element(); next.Kind() == cc.Void {
isVoidPtr = true
spec.Base = "void*"
break
}
typ = typ.Element()
spec.Pointers++
}
Expand All @@ -287,6 +293,9 @@ func (t *Translator) typeSpec(typ cc.Type, deep int, isRet bool) CType {
spec.AddInnerArr(uint64(size))
}
}
if isVoidPtr {
return spec
}

switch typ.Kind() {
case cc.Void:
Expand Down
7 changes: 3 additions & 4 deletions translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,12 @@ func (t *Translator) CGoSpec(spec CType, asArg bool) CGoSpec {
cgo.Pointers = spec.GetPointers() - decl.Spec.GetPointers()
}
if typ, ok := spec.(*CTypeSpec); ok {
if typ.Base == "void" {
if cgo.Pointers > 0 {
cgo.Pointers--
}
if typ.Base == "void*" {
if len(typ.Raw) == 0 {
cgo.Base = "unsafe.Pointer"
return cgo
} else if !asArg {
cgo.Pointers++
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions translator/type_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ var builtinCTypeMap = CTypeMap{
// long complex double -> complex128
CTypeSpec{Base: "double", Long: true, Complex: true}: Complex128Spec,
// void* -> unsafe.Pointer
CTypeSpec{Base: "void*"}: UnsafePointerSpec,
// void* -> unsafe.Pointer
CTypeSpec{Base: "void", Pointers: 1}: UnsafePointerSpec,
// void -> [0]byte
CTypeSpec{Base: "void"}: VoidSpec,
Expand Down

0 comments on commit f5ababa

Please sign in to comment.