Skip to content

go/types: support cgo semantics #23090

@hirochachacha

Description

@hirochachacha

I was thinking about #975.
My idea is guessing argument types and making specialized wrappers.
For example,
We can convert following code

package main

// #include <stdio.h>
import "C"

func main() {
	C.printf(C.CString("hello: %d"), 10)
	C.fflush(C.stdout)
}

into

package main

// #include <stdio.h>
// int call_printf_with_int(const char* s, int i) {
//   return printf(s, i);
// }
import "C"

func main() {
	C.call_printf_with_int(C.CString("hello: %d"), 10)
	C.fflush(C.stdout)
}

I'd like to leave these translations to cgo.

To promote this idea, we have to make go/types understand some special cgo semantics.

  • all cgo objects are exportable even if the first letter is lower case (e.g. C.char, C.int, ...)
  • cgo functions behave like unsafe.Pointer in the expression context (e.g. var xxx unsafe.Pointer = C.puts)
  • cgo functions can return the optional error argument in the assignment context (e.g _, err := C.puts(C.CString("x"))

Is this acceptable?

FWIW, https://go-review.googlesource.com/c/go/+/83215 is the attempt.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions