Skip to content

Commit f337ac8

Browse files
committed
cgo: refactor
This is a large refactor of the cgo package. It should fix a number of smaller problems and be a bit more strict (like upstream CGo): it for example requires every Go file in a package to include the header files it needs instead of piggybacking on imports in earlier files. The main benefit is that it should be a bit more maintainable and easier to add new features in the future (like static functions). This breaks the tinygo.org/x/bluetooth package, which should be updated before this change lands.
1 parent 7f507a7 commit f337ac8

File tree

9 files changed

+916
-936
lines changed

9 files changed

+916
-936
lines changed

cgo/cgo.go

Lines changed: 365 additions & 531 deletions
Large diffs are not rendered by default.

cgo/libclang.go

Lines changed: 383 additions & 199 deletions
Large diffs are not rendered by default.

cgo/testdata/basic.out.go

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,16 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2424
return C.__GoBytes(ptr, uintptr(length))
2525
}
2626

27-
type C.int16_t = int16
28-
type C.int32_t = int32
29-
type C.int64_t = int64
30-
type C.int8_t = int8
31-
type C.uint16_t = uint16
32-
type C.uint32_t = uint32
33-
type C.uint64_t = uint64
34-
type C.uint8_t = uint8
35-
type C.uintptr_t = uintptr
36-
type C.char uint8
37-
type C.int int32
38-
type C.long int32
39-
type C.longlong int64
40-
type C.schar int8
41-
type C.short int16
42-
type C.uchar uint8
43-
type C.uint uint32
44-
type C.ulong uint32
45-
type C.ulonglong uint64
46-
type C.ushort uint16
27+
type (
28+
C.char uint8
29+
C.schar int8
30+
C.uchar uint8
31+
C.short int16
32+
C.ushort uint16
33+
C.int int32
34+
C.uint uint32
35+
C.long int32
36+
C.ulong uint32
37+
C.longlong int64
38+
C.ulonglong uint64
39+
)

cgo/testdata/const.out.go

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,19 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2424
return C.__GoBytes(ptr, uintptr(length))
2525
}
2626

27-
const C.bar = C.foo
28-
const C.foo = 3
27+
type (
28+
C.char uint8
29+
C.schar int8
30+
C.uchar uint8
31+
C.short int16
32+
C.ushort uint16
33+
C.int int32
34+
C.uint uint32
35+
C.long int32
36+
C.ulong uint32
37+
C.longlong int64
38+
C.ulonglong uint64
39+
)
2940

30-
type C.int16_t = int16
31-
type C.int32_t = int32
32-
type C.int64_t = int64
33-
type C.int8_t = int8
34-
type C.uint16_t = uint16
35-
type C.uint32_t = uint32
36-
type C.uint64_t = uint64
37-
type C.uint8_t = uint8
38-
type C.uintptr_t = uintptr
39-
type C.char uint8
40-
type C.int int32
41-
type C.long int32
42-
type C.longlong int64
43-
type C.schar int8
44-
type C.short int16
45-
type C.uchar uint8
46-
type C.uint uint32
47-
type C.ulong uint32
48-
type C.ulonglong uint64
49-
type C.ushort uint16
41+
const C.foo = 3
42+
const C.bar = C.foo

cgo/testdata/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import "C"
2727
//line errors.go:100
2828
var (
2929
// constant too large
30-
_ C.uint8_t = 2 << 10
30+
_ C.char = 2 << 10
3131

3232
// z member does not exist
3333
_ C.point_t = C.point_t{z: 3}

cgo/testdata/errors.out.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// testdata/errors.go:19:26: unexpected token ), expected end of expression
77

88
// Type checking errors after CGo processing:
9-
// testdata/errors.go:102: cannot use 2 << 10 (untyped int constant 2048) as uint8 value in variable declaration (overflows)
9+
// testdata/errors.go:102: cannot use 2 << 10 (untyped int constant 2048) as C.char value in variable declaration (overflows)
1010
// testdata/errors.go:105: unknown field z in struct literal
1111
// testdata/errors.go:108: undeclared name: C.SOME_CONST_1
1212
// testdata/errors.go:110: cannot use C.SOME_CONST_3 (untyped int constant 1234) as byte value in variable declaration (overflows)
@@ -38,29 +38,23 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
3838
return C.__GoBytes(ptr, uintptr(length))
3939
}
4040

41-
const C.SOME_CONST_3 = 1234
42-
43-
type C.int16_t = int16
44-
type C.int32_t = int32
45-
type C.int64_t = int64
46-
type C.int8_t = int8
47-
type C.uint16_t = uint16
48-
type C.uint32_t = uint32
49-
type C.uint64_t = uint64
50-
type C.uint8_t = uint8
51-
type C.uintptr_t = uintptr
52-
type C.char uint8
53-
type C.int int32
54-
type C.long int32
55-
type C.longlong int64
56-
type C.schar int8
57-
type C.short int16
58-
type C.uchar uint8
59-
type C.uint uint32
60-
type C.ulong uint32
61-
type C.ulonglong uint64
62-
type C.ushort uint16
63-
type C.point_t = struct {
41+
type (
42+
C.char uint8
43+
C.schar int8
44+
C.uchar uint8
45+
C.short int16
46+
C.ushort uint16
47+
C.int int32
48+
C.uint uint32
49+
C.long int32
50+
C.ulong uint32
51+
C.longlong int64
52+
C.ulonglong uint64
53+
)
54+
type C._Ctype_struct___0 struct {
6455
x C.int
6556
y C.int
6657
}
58+
type C.point_t = C._Ctype_struct___0
59+
60+
const C.SOME_CONST_3 = 1234

cgo/testdata/flags.out.go

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,19 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2929
return C.__GoBytes(ptr, uintptr(length))
3030
}
3131

32+
type (
33+
C.char uint8
34+
C.schar int8
35+
C.uchar uint8
36+
C.short int16
37+
C.ushort uint16
38+
C.int int32
39+
C.uint uint32
40+
C.long int32
41+
C.ulong uint32
42+
C.longlong int64
43+
C.ulonglong uint64
44+
)
45+
3246
const C.BAR = 3
3347
const C.FOO_H = 1
34-
35-
type C.int16_t = int16
36-
type C.int32_t = int32
37-
type C.int64_t = int64
38-
type C.int8_t = int8
39-
type C.uint16_t = uint16
40-
type C.uint32_t = uint32
41-
type C.uint64_t = uint64
42-
type C.uint8_t = uint8
43-
type C.uintptr_t = uintptr
44-
type C.char uint8
45-
type C.int int32
46-
type C.long int32
47-
type C.longlong int64
48-
type C.schar int8
49-
type C.short int16
50-
type C.uchar uint8
51-
type C.uint uint32
52-
type C.ulong uint32
53-
type C.ulonglong uint64
54-
type C.ushort uint16

cgo/testdata/symbols.out.go

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,36 @@ func C.GoBytes(ptr unsafe.Pointer, length C.int) []byte {
2424
return C.__GoBytes(ptr, uintptr(length))
2525
}
2626

27+
type (
28+
C.char uint8
29+
C.schar int8
30+
C.uchar uint8
31+
C.short int16
32+
C.ushort uint16
33+
C.int int32
34+
C.uint uint32
35+
C.long int32
36+
C.ulong uint32
37+
C.longlong int64
38+
C.ulonglong uint64
39+
)
40+
2741
//export foo
2842
func C.foo(a C.int, b C.int) C.int
2943

44+
var C.foo$funcaddr unsafe.Pointer
45+
3046
//export variadic0
3147
//go:variadic
3248
func C.variadic0()
3349

50+
var C.variadic0$funcaddr unsafe.Pointer
51+
3452
//export variadic2
3553
//go:variadic
3654
func C.variadic2(x C.int, y C.int)
3755

38-
var C.foo$funcaddr unsafe.Pointer
39-
var C.variadic0$funcaddr unsafe.Pointer
4056
var C.variadic2$funcaddr unsafe.Pointer
4157

4258
//go:extern someValue
4359
var C.someValue C.int
44-
45-
type C.int16_t = int16
46-
type C.int32_t = int32
47-
type C.int64_t = int64
48-
type C.int8_t = int8
49-
type C.uint16_t = uint16
50-
type C.uint32_t = uint32
51-
type C.uint64_t = uint64
52-
type C.uint8_t = uint8
53-
type C.uintptr_t = uintptr
54-
type C.char uint8
55-
type C.int int32
56-
type C.long int32
57-
type C.longlong int64
58-
type C.schar int8
59-
type C.short int16
60-
type C.uchar uint8
61-
type C.uint uint32
62-
type C.ulong uint32
63-
type C.ulonglong uint64
64-
type C.ushort uint16

0 commit comments

Comments
 (0)