From 92b13598665da0de423b279dd111441b6f614632 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 26 Oct 2018 13:33:56 +0900 Subject: [PATCH] Replace exported C types with unsafe.Pointer. (#101) This PR replaces exported C types with unsafe.Pointer, so that gl will not expose C types. This PR also adds comments describing the original C types. Example output: // Parameter context has type *C.struct__cl_context. // Parameter event has type *C.struct__cl_event. func CreateSyncFromCLeventARB(context unsafe.Pointer, event unsafe.Pointer, flags uint32) uintptr { ... } Updates go-gl/gl#113. --- package.go | 22 ++++++++++++++++++++++ tmpl/package.tmpl | 2 +- type.go | 4 ++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/package.go b/package.go index 49b809f..4c0d64f 100644 --- a/package.go +++ b/package.go @@ -31,6 +31,28 @@ type PackageFunction struct { Doc string } +// Comment returns the comment explaining the function. +func (f *PackageFunction) Comment() string { + var lines []string + if f.Doc != "" { + lines = append(lines, "// " + f.Doc) + } + + // Adds explanations about C types that are unsafe.Pointer in Go world. + // See also https://github.com/go-gl/gl/issues/113. + for _, p := range f.Function.Parameters { + if t := p.Type; t.GoType() == "unsafe.Pointer" && t.Name != "void" && t.Name != "GLvoid" { + lines = append(lines, fmt.Sprintf("// Parameter %s has type %s.", p.Name, t.GoCType())) + } + } + + if r := f.Function.Return; r.GoType() == "unsafe.Pointer" && r.Name != "void" && r.Name != "GLvoid" { + lines = append(lines, fmt.Sprintf("// Return value has type %s.", r.GoCType())) + } + + return strings.Join(lines, "\n") +} + // UniqueName returns a globally unique Go-compatible name for this package. func (pkg *Package) UniqueName() string { version := strings.Replace(pkg.Version.String(), ".", "", -1) diff --git a/tmpl/package.tmpl b/tmpl/package.tmpl index 91b7cbb..de79fc4 100644 --- a/tmpl/package.tmpl +++ b/tmpl/package.tmpl @@ -94,7 +94,7 @@ func boolToInt(b bool) int { {{define "bridgeCall"}}C.glow{{.GoName}}(gp{{.GoName}}{{if ge (len .Parameters) 1}}, {{end}}{{template "paramsGoCall" .Parameters}}){{end}} {{range .Functions}} -{{with .Doc}}// {{.}}{{end}} +{{.Comment}} func {{.GoName}}({{template "paramsGoDecl" .Parameters}}){{if not .Return.IsVoid}} {{.Return.GoType}}{{end}} { {{range .Parameters}} {{if .Type.IsDebugProc}}userDebugCallback = {{.GoName}}{{end}} diff --git a/type.go b/type.go index 73c72a5..036f975 100644 --- a/type.go +++ b/type.go @@ -113,7 +113,7 @@ func (t Type) GoType() string { // Special case mapping to the type defined in debug.tmpl return "DebugProc" } - return t.GoCType() + return "unsafe.Pointer" } // ConvertGoToC returns an expression that converts a variable from the Go type to the C type. @@ -128,7 +128,7 @@ func (t Type) ConvertGoToC(name string) string { case "GLDEBUGPROC", "GLDEBUGPROCARB", "GLDEBUGPROCKHR": return fmt.Sprintf("(C.%s)(unsafe.Pointer(&%s))", t.Name, name) } - if t.PointerLevel >= 1 { + if t.PointerLevel >= 1 && t.GoType() != "unsafe.Pointer" { return fmt.Sprintf("(%s)(unsafe.Pointer(%s))", t.GoCType(), name) } return fmt.Sprintf("(%s)(%s)", t.GoCType(), name)