Skip to content

Commit

Permalink
Replace exported C types with unsafe.Pointer. (#101)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hajimehoshi authored and dmitshur committed Oct 26, 2018
1 parent 37a41ef commit 92b1359
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
22 changes: 22 additions & 0 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tmpl/package.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
4 changes: 2 additions & 2 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down

0 comments on commit 92b1359

Please sign in to comment.