cgo: do not rely on stdint.h to be available
Этот коммит содержится в:
родитель
0af7da9bff
коммит
c6069476a7
4 изменённых файлов: 40 добавлений и 24 удалений
|
@ -142,13 +142,13 @@ func (info *fileInfo) addFuncDecls() {
|
||||||
for _, fn := range info.functions {
|
for _, fn := range info.functions {
|
||||||
obj := &ast.Object{
|
obj := &ast.Object{
|
||||||
Kind: ast.Fun,
|
Kind: ast.Fun,
|
||||||
Name: "C." + fn.name,
|
Name: mapCgoType(fn.name),
|
||||||
}
|
}
|
||||||
args := make([]*ast.Field, len(fn.args))
|
args := make([]*ast.Field, len(fn.args))
|
||||||
decl := &ast.FuncDecl{
|
decl := &ast.FuncDecl{
|
||||||
Name: &ast.Ident{
|
Name: &ast.Ident{
|
||||||
NamePos: info.importCPos,
|
NamePos: info.importCPos,
|
||||||
Name: "C." + fn.name,
|
Name: mapCgoType(fn.name),
|
||||||
Obj: obj,
|
Obj: obj,
|
||||||
},
|
},
|
||||||
Type: &ast.FuncType{
|
Type: &ast.FuncType{
|
||||||
|
@ -163,7 +163,7 @@ func (info *fileInfo) addFuncDecls() {
|
||||||
&ast.Field{
|
&ast.Field{
|
||||||
Type: &ast.Ident{
|
Type: &ast.Ident{
|
||||||
NamePos: info.importCPos,
|
NamePos: info.importCPos,
|
||||||
Name: "C." + fn.result,
|
Name: mapCgoType(fn.result),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -179,14 +179,14 @@ func (info *fileInfo) addFuncDecls() {
|
||||||
Name: arg.name,
|
Name: arg.name,
|
||||||
Obj: &ast.Object{
|
Obj: &ast.Object{
|
||||||
Kind: ast.Var,
|
Kind: ast.Var,
|
||||||
Name: "C." + arg.name,
|
Name: mapCgoType(arg.name),
|
||||||
Decl: decl,
|
Decl: decl,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Type: &ast.Ident{
|
Type: &ast.Ident{
|
||||||
NamePos: info.importCPos,
|
NamePos: info.importCPos,
|
||||||
Name: "C." + arg.typeName,
|
Name: mapCgoType(arg.typeName),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -244,11 +244,11 @@ func (info *fileInfo) addTypedefs() {
|
||||||
Tok: token.TYPE,
|
Tok: token.TYPE,
|
||||||
}
|
}
|
||||||
for _, typedef := range info.typedefs {
|
for _, typedef := range info.typedefs {
|
||||||
newType := "C." + typedef.newName
|
newType := mapCgoType(typedef.newName)
|
||||||
oldType := "C." + typedef.oldName
|
oldType := mapCgoType(typedef.oldName)
|
||||||
switch oldType {
|
switch oldType {
|
||||||
// TODO: plain char (may be signed or unsigned)
|
// TODO: plain char (may be signed or unsigned)
|
||||||
case "C.signed char", "C.short", "C.int", "C.long", "C.long long":
|
case "C.schar", "C.short", "C.int", "C.long", "C.longlong":
|
||||||
switch typedef.size {
|
switch typedef.size {
|
||||||
case 1:
|
case 1:
|
||||||
oldType = "int8"
|
oldType = "int8"
|
||||||
|
@ -259,7 +259,7 @@ func (info *fileInfo) addTypedefs() {
|
||||||
case 8:
|
case 8:
|
||||||
oldType = "int64"
|
oldType = "int64"
|
||||||
}
|
}
|
||||||
case "C.unsigned char", "C.unsigned short", "C.unsigned int", "C.unsigned long", "C.unsigned long long":
|
case "C.uchar", "C.ushort", "C.uint", "C.ulong", "C.ulonglong":
|
||||||
switch typedef.size {
|
switch typedef.size {
|
||||||
case 1:
|
case 1:
|
||||||
oldType = "uint8"
|
oldType = "uint8"
|
||||||
|
@ -317,7 +317,7 @@ func (info *fileInfo) walker(node ast.Node) bool {
|
||||||
if x.Name == "C" {
|
if x.Name == "C" {
|
||||||
node.Fun = &ast.Ident{
|
node.Fun = &ast.Ident{
|
||||||
NamePos: x.NamePos,
|
NamePos: x.NamePos,
|
||||||
Name: "C." + fun.Sel.Name,
|
Name: mapCgoType(fun.Sel.Name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *ast.ValueSpec:
|
case *ast.ValueSpec:
|
||||||
|
@ -332,9 +332,31 @@ func (info *fileInfo) walker(node ast.Node) bool {
|
||||||
if x.Name == "C" {
|
if x.Name == "C" {
|
||||||
node.Type = &ast.Ident{
|
node.Type = &ast.Ident{
|
||||||
NamePos: x.NamePos,
|
NamePos: x.NamePos,
|
||||||
Name: "C." + typ.Sel.Name,
|
Name: mapCgoType(typ.Sel.Name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// mapCgoType converts a C type name into a Go type name with a "C." prefix.
|
||||||
|
func mapCgoType(t string) string {
|
||||||
|
switch t {
|
||||||
|
case "signed char":
|
||||||
|
return "C.schar"
|
||||||
|
case "long long":
|
||||||
|
return "C.longlong"
|
||||||
|
case "unsigned char":
|
||||||
|
return "C.schar"
|
||||||
|
case "unsigned short":
|
||||||
|
return "C.ushort"
|
||||||
|
case "unsigned int":
|
||||||
|
return "C.uint"
|
||||||
|
case "unsigned long":
|
||||||
|
return "C.ulong"
|
||||||
|
case "unsigned long long":
|
||||||
|
return "C.ulonglong"
|
||||||
|
default:
|
||||||
|
return "C." + t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
8
testdata/cgo/main.c
предоставленный
8
testdata/cgo/main.c
предоставленный
|
@ -1,13 +1,7 @@
|
||||||
#include <stdint.h>
|
int fortytwo() {
|
||||||
|
|
||||||
int32_t fortytwo() {
|
|
||||||
return 42;
|
return 42;
|
||||||
}
|
}
|
||||||
|
|
||||||
int add(int a, int b) {
|
int add(int a, int b) {
|
||||||
return a + b;
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t mul(int32_t a, int32_t b) {
|
|
||||||
return a * b;
|
|
||||||
}
|
|
||||||
|
|
10
testdata/cgo/main.go
предоставленный
10
testdata/cgo/main.go
предоставленный
|
@ -1,20 +1,20 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#include <stdint.h>
|
int fortytwo(void);
|
||||||
int32_t fortytwo(void);
|
typedef short myint;
|
||||||
int32_t mul(int32_t a, int32_t b);
|
|
||||||
typedef int32_t myint;
|
|
||||||
int add(int a, int b);
|
int add(int a, int b);
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
import "unsafe"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
println("fortytwo:", C.fortytwo())
|
println("fortytwo:", C.fortytwo())
|
||||||
println("mul:", C.mul(C.int32_t(3), 5))
|
|
||||||
println("add:", C.add(C.int(3), 5))
|
println("add:", C.add(C.int(3), 5))
|
||||||
var x C.myint = 3
|
var x C.myint = 3
|
||||||
println("myint:", x, C.myint(5))
|
println("myint:", x, C.myint(5))
|
||||||
|
println("myint size:", int(unsafe.Sizeof(x)))
|
||||||
var y C.longlong = -(1 << 40)
|
var y C.longlong = -(1 << 40)
|
||||||
println("longlong:", y)
|
println("longlong:", y)
|
||||||
}
|
}
|
||||||
|
|
2
testdata/cgo/out.txt
предоставленный
2
testdata/cgo/out.txt
предоставленный
|
@ -1,5 +1,5 @@
|
||||||
fortytwo: 42
|
fortytwo: 42
|
||||||
mul: 15
|
|
||||||
add: 8
|
add: 8
|
||||||
myint: 3 5
|
myint: 3 5
|
||||||
|
myint size: 2
|
||||||
longlong: -1099511627776
|
longlong: -1099511627776
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче