New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/types: support cgo semantics #23090

Open
hirochachacha opened this Issue Dec 11, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@hirochachacha
Contributor

hirochachacha commented Dec 11, 2017

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.

@ianlancetaylor

This comment has been minimized.

Contributor

ianlancetaylor commented Dec 11, 2017

@griesemer

This comment has been minimized.

Contributor

griesemer commented May 1, 2018

Too late for 1.11.

@griesemer griesemer modified the milestones: Go1.11, Go1.12 May 1, 2018

@griesemer griesemer modified the milestones: Go1.12, Go1.13 Dec 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment