Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion generator/gen_bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"bytes"
"fmt"
"github.com/xlab/c-for-go/utils"
"io"
"strings"

Expand Down Expand Up @@ -1033,7 +1034,7 @@ func (gen *Generator) writeFunctionBody(wr io.Writer, decl *tl.CDecl) {
if spec.Return != nil {
fmt.Fprint(wr, "__ret := ")
}
fmt.Fprintf(wr, "C.%s", decl.Name)
fmt.Fprintf(wr, utils.CTypeString(decl.Name))
writeStartParams(wr)
for i := range spec.Params {
fmt.Fprint(wr, from[i].Name)
Expand Down
9 changes: 5 additions & 4 deletions generator/gen_callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package generator
import (
"bytes"
"fmt"
"github.com/xlab/c-for-go/utils"
"io"
"strings"

Expand Down Expand Up @@ -78,8 +79,8 @@ func (gen *Generator) getCallbackHelpers(goFuncName, cFuncName string, spec tl.C
if %sFunc == nil {
%sFunc = x
}
return (*%s)(C.%s), nil
}`, cbGoName, cbGoName, cgoSpec, cbCName)
return (*%s)(%s), nil
}`, cbGoName, cbGoName, cgoSpec, utils.CTypeString(cbCName))
helpers = append(helpers, &Helper{
Name: fmt.Sprintf("%s.PassRef", goFuncName),
Source: buf.String(),
Expand All @@ -95,8 +96,8 @@ func (gen *Generator) getCallbackHelpers(goFuncName, cFuncName string, spec tl.C
if %sFunc == nil {
%sFunc = x
}
return (%s)(C.%s), nil
}`, cbGoName, cbGoName, cgoSpec, cbCName)
return (%s)(%s), nil
}`, cbGoName, cbGoName, cgoSpec, utils.CTypeString(cbCName))
helpers = append(helpers, &Helper{
Name: fmt.Sprintf("%s.PassValue", goFuncName),
Source: buf.String(),
Expand Down
9 changes: 5 additions & 4 deletions generator/gen_typedef.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package generator

import (
"fmt"
tl "github.com/xlab/c-for-go/translator"
"github.com/xlab/c-for-go/utils"
"io"
"path/filepath"

tl "github.com/xlab/c-for-go/translator"
)

func (gen *Generator) writeTypeTypedef(wr io.Writer, decl *tl.CDecl, seenNames map[string]bool) {
Expand Down Expand Up @@ -107,7 +107,8 @@ func (gen *Generator) writeStructTypedef(wr io.Writer, decl *tl.CDecl, raw bool,
// opaque struct
fmt.Fprintf(wr, "// %s as declared in %s\n", goName,
filepath.ToSlash(gen.tr.SrcLocation(tl.TargetType, cName, decl.Position)))
fmt.Fprintf(wr, "type %s C.%s", goName, decl.Spec.CGoName())
cgoName := decl.Spec.CGoName()
fmt.Fprintf(wr, "type %s %s", goName, utils.CTypeString(cgoName))
writeSpace(wr, 1)
for _, helper := range gen.getRawStructHelpers(goName, cName, decl.Spec) {
gen.submitHelper(helper)
Expand Down Expand Up @@ -139,7 +140,7 @@ func (gen *Generator) writeUnionTypedef(wr io.Writer, decl *tl.CDecl) {
if typeName := string(goName); typeName != typeRef {
fmt.Fprintf(wr, "// %s as declared in %s\n", goName,
filepath.ToSlash(gen.tr.SrcLocation(tl.TargetType, cName, decl.Position)))
fmt.Fprintf(wr, "const sizeof%s = unsafe.Sizeof(C.%s{})\n", goName, decl.Spec.CGoName())
fmt.Fprintf(wr, "const sizeof%s = unsafe.Sizeof(%s{})\n", goName, utils.CTypeString(decl.Spec.CGoName()))
fmt.Fprintf(wr, "type %s [sizeof%s]byte\n", goName, goName)
writeSpace(wr, 1)
return
Expand Down
3 changes: 2 additions & 1 deletion translator/ast_walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package translator

import (
"fmt"
"github.com/xlab/c-for-go/utils"
"strings"

"modernc.org/cc/v4"
Expand Down Expand Up @@ -129,7 +130,7 @@ func (t *Translator) enumSpec(base *CTypeSpec, typ cc.Type) *CEnumSpec {
}
switch {
case t.constRules[ConstEnum] == ConstCGOAlias:
m.Expression = fmt.Sprintf("C.%s", name)
m.Expression = utils.CTypeString(name)
case t.constRules[ConstEnum] == ConstExpand:
enTokens := cc.NodeTokens(en.ConstantExpression)
srcParts := make([]string, 0, len(enTokens))
Expand Down
5 changes: 3 additions & 2 deletions translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package translator
import (
"bytes"
"fmt"
"github.com/xlab/c-for-go/utils"
"regexp"
"sort"
"strings"
Expand Down Expand Up @@ -881,7 +882,7 @@ func (t *Translator) TranslateSpec(spec CType, tips ...Tip) GoTypeSpec {
if base := spec.GetBase(); len(base) > 0 {
wrapper.Raw = string(t.TransformName(TargetType, base))
} else if cgoName := spec.CGoName(); len(cgoName) > 0 {
wrapper.Raw = "C." + cgoName
wrapper.Raw = utils.CTypeString(cgoName)
}
return wrapper
}
Expand Down Expand Up @@ -919,7 +920,7 @@ func (t *Translator) CGoSpec(spec CType, asArg bool) CGoSpec {
}
}
}
cgo.Base = "C." + spec.CGoName()
cgo.Base = utils.CTypeString(spec.CGoName())
return cgo
}

Expand Down
13 changes: 13 additions & 0 deletions utils/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import (
"fmt"
"strings"
)

func CTypeString(name string) string {
if strings.HasPrefix(name, "enum") {
return "uint32"
}
return fmt.Sprintf("C.%s", name)
}