Skip to content
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

cgo: refactor Go types a little bit #3927

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
28 changes: 9 additions & 19 deletions cgo/cgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,10 @@ type bitfieldInfo struct {
}

// cgoAliases list type aliases between Go and C, for types that are equivalent
// in both languages. See addTypeAliases.
// in both languages.
var cgoAliases = map[string]string{
"C.int8_t": "int8",
"C.int16_t": "int16",
"C.int32_t": "int32",
"C.int64_t": "int64",
"C.uint8_t": "uint8",
"C.uint16_t": "uint16",
"C.uint32_t": "uint32",
"C.uint64_t": "uint64",
"C.uintptr_t": "uintptr",
"C.float": "float32",
"C.double": "float64",
"float": "float32",
"double": "float64",
}

// builtinAliases are handled specially because they only exist on the Go side
Expand Down Expand Up @@ -311,10 +302,11 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl
// Process CGo imports for each file.
for i, f := range files {
cf := p.newCGoFile(f, i)
// Float and double are aliased, meaning that C.float is the same thing
// as float32 in Go.
cf.names["float"] = clangCursor{}
cf.names["double"] = clangCursor{}
// These types are aliased with the corresponding types in C. For
// example, float in C is always float32 in Go.
for name := range cgoAliases {
cf.names[name] = clangCursor{}
}
// Now read all the names (identifies) that C defines in the header
// snippet.
cf.readNames(p.cgoHeaders[i], cflagsForCGo, filepath.Base(fset.File(f.Pos()).Name()), func(names map[string]clangCursor) {
Expand Down Expand Up @@ -1129,9 +1121,7 @@ func (p *cgoPackage) getUnnamedDeclName(prefix string, itf interface{}) string {
// getASTDeclName will declare the given C AST node (if not already defined) and
// will return its name, in the form of C.foo.
func (f *cgoFile) getASTDeclName(name string, found clangCursor, iscall bool) string {
// Some types are defined in stdint.h and map directly to a particular Go
// type.
if alias := cgoAliases["C."+name]; alias != "" {
if alias := cgoAliases[name]; alias != "" {
return alias
}
node := f.getASTDeclNode(name, found, iscall)
Expand Down
8 changes: 8 additions & 0 deletions cgo/testdata/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import "C"
// #warning another warning
import "C"

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

// Make sure that errors for the following lines won't change with future
// additions to the CGo preamble.
//
//line errors.go:100
var (
// constant too large
Expand All @@ -38,4 +42,8 @@ var (
_ byte = C.SOME_CONST_3

_ = C.SOME_CONST_4

// This must result in a type error. Previously, TinyGo would allow this
// code (which is not allowed by upstream Go).
_ int8 = C.int8_t(5)
)
3 changes: 3 additions & 0 deletions cgo/testdata/errors.out.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// testdata/errors.go:108: undefined: C.SOME_CONST_1
// testdata/errors.go:110: cannot use C.SOME_CONST_3 (untyped int constant 1234) as byte value in variable declaration (overflows)
// testdata/errors.go:112: undefined: C.SOME_CONST_4
// testdata/errors.go:116: cannot use C.int8_t(5) (constant 5 of type C.schar) as int8 value in variable declaration

package main

Expand Down Expand Up @@ -58,3 +59,5 @@ type C.struct_point_t struct {
type C.point_t = C.struct_point_t

const C.SOME_CONST_3 = 1234

type C.int8_t = C.schar
4 changes: 0 additions & 4 deletions testdata/cgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ func main() {
println("static headerfunc:", C.headerfunc_static(5))
headerfunc_2()

// equivalent types
var goInt8 int8 = 5
var _ C.int8_t = goInt8

// more globals
println("bool:", C.globalBool, C.globalBool2 == true)
println("float:", C.globalFloat)
Expand Down
Loading