loader/cgo: add support for function pointers

This commit is contained in:
Ayke van Laethem
2019-02-07 13:50:09 +01:00
parent 35fb594f8f
commit 95d895646a
8 changed files with 134 additions and 7 deletions
+24 -2
View File
@@ -6,6 +6,8 @@ package loader
import (
"errors"
"go/ast"
"go/token"
"strconv"
"strings"
"unsafe"
)
@@ -90,13 +92,16 @@ func tinygo_clang_visitor(c, parent C.CXCursor, client_data C.CXClientData) C.in
if C.clang_isFunctionTypeVariadic(cursorType) != 0 {
return C.CXChildVisit_Continue // not supported
}
numArgs := C.clang_Cursor_getNumArguments(c)
numArgs := int(C.clang_Cursor_getNumArguments(c))
fn := &functionInfo{}
info.functions[name] = fn
for i := C.int(0); i < numArgs; i++ {
for i := 0; i < numArgs; i++ {
arg := C.clang_Cursor_getArgument(c, C.uint(i))
argName := getString(C.clang_getCursorSpelling(arg))
argType := C.clang_getArgType(cursorType, C.uint(i))
if argName == "" {
argName = "$" + strconv.Itoa(i)
}
fn.args = append(fn.args, paramInfo{
name: argName,
typeExpr: info.makeASTType(argType),
@@ -196,6 +201,23 @@ func (info *fileInfo) makeASTType(typ C.CXType) ast.Expr {
Star: info.importCPos,
X: info.makeASTType(C.clang_getPointeeType(typ)),
}
case C.CXType_FunctionProto:
// Be compatible with gc, which uses the *[0]byte type for function
// pointer types.
// Return type [0]byte because this is a function type, not a pointer to
// this function type.
return &ast.ArrayType{
Lbrack: info.importCPos,
Len: &ast.BasicLit{
ValuePos: info.importCPos,
Kind: token.INT,
Value: "0",
},
Elt: &ast.Ident{
NamePos: info.importCPos,
Name: "byte",
},
}
default:
// Fallback, probably incorrect but at least the error points to an odd
// type name.