diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index 459b46e7..13c22898 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -217,8 +217,7 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { expr = &ast.LvalueRefType{X: ct.ProcessType(t.NonReferenceType())} case clang.TypeFunctionProto: // function type will only collect return type, params will be collected in ProcessFuncDecl - ret := ct.ProcessType(t.ResultType()) - expr = &ast.FuncType{Ret: ret} + expr = ct.ProcessFunctionType(t) case clang.TypeConstantArray, clang.TypeIncompleteArray, clang.TypeVariableArray, clang.TypeDependentSizedArray: if t.Kind == clang.TypeConstantArray { len := (*c.Char)(c.Malloc(unsafe.Sizeof(c.Char(0)) * 20)) @@ -238,6 +237,30 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { return expr } +// For function types, we can only obtain the parameter types, but not the parameter names. +// This is because we cannot reverse-lookup the corresponding declaration node from a function type. +// Note: For function declarations, parameter names are collected in the ProcessFuncDecl method. +func (ct *Converter) ProcessFunctionType(t clang.Type) *ast.FuncType { + // Note: Attempting to get the type declaration for a function type will result in CursorNoDeclFound + // cursor := t.TypeDeclaration() + // This would return CursorNoDeclFound + + ret := ct.ProcessType(t.ResultType()) + params := &ast.FieldList{List: make([]*ast.Field, 0)} + numArgs := t.NumArgTypes() + for i := 0; i < int(numArgs); i++ { + argType := t.ArgType(c.Uint(i)) + params.List = append(params.List, &ast.Field{ + Type: ct.ProcessType(argType), + }) + } + + return &ast.FuncType{ + Ret: ret, + Params: params, + } +} + func (ct *Converter) ProcessTypeDefDecl(cursor clang.Cursor) *ast.TypedefDecl { name := cursor.String() defer name.Dispose() @@ -260,7 +283,6 @@ func (ct *Converter) ProcessFuncDecl(cursor clang.Cursor) *ast.FuncDecl { fmt.Println("failed to process function type") return nil } - params := ct.ProcessFieldList(cursor) funcType.Params = params fn := &ast.FuncDecl{ diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect index 03962fa5..8d208c32 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/llgo.expect @@ -148,6 +148,84 @@ TestStructDecl Case 3: } } +TestStructDecl Case 4: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "A" + }, + "Type": { + "Tag": 0, + "Fields": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "a" + }] + }, { + "Type": { + "X": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }] + }, + "Ret": { + "Kind": 6, + "Flags": 0 + } + } + }, + "Doc": { + "List": [] + }, + "Comment": { + "List": [] + }, + "Names": [{ + "Name": "Foo" + }] + }] + }, + "Methods": [] + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go index d73a46bf..6e05aae3 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/struct_test/struct.go @@ -18,6 +18,10 @@ func TestStructDecl() { `struct A { int a, b; };`, + `struct A { + int a; + int (*Foo)(int, int); + };`, } test.RunTest("TestStructDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect index 064cb627..98f60b2c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/llgo.expect @@ -61,6 +61,53 @@ TestTypeDefDecl Case 2: } } +TestTypeDefDecl Case 3: +{ + "temp.h": { + "decls": [{ + "Loc": { + "File": "temp.h" + }, + "Doc": { + "List": [] + }, + "Parent": null, + "Name": { + "Name": "Foo" + }, + "Type": { + "X": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }, { + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }] + }, + "Ret": { + "Kind": 6, + "Flags": 0 + } + } + } + }], + "includes": [], + "macros": [] + } +} + #stderr diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go index 5700e11f..7b2548cf 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/decl_test/typedef_test/typedef.go @@ -12,6 +12,8 @@ func TestTypeDefDecl() { `typedef int INT; typedef INT STANDARD_INT;`, + + `typedef int (*Foo)(int, int);`, } test.RunTest("TestTypeDefDecl", testCases) } diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect index 0f8b23b8..7adead79 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/llgo.expect @@ -169,6 +169,34 @@ Type: class a::b::c: }, "Tag": 3 } +Type: int (*)(int, char): +{ + "X": { + "Params": { + "List": [{ + "Type": { + "Kind": 6, + "Flags": 0 + }, + "Doc": null, + "Comment": null, + "Names": [] + }, { + "Type": { + "Kind": 2, + "Flags": 1 + }, + "Doc": null, + "Comment": null, + "Names": [] + }] + }, + "Ret": { + "Kind": 6, + "Flags": 0 + } + } +} #stderr todo: unknown builtin type: Ibm128 diff --git a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go index c4c51a35..789dfa3c 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt_test/type_test/type.go @@ -111,6 +111,8 @@ func TestNonBuiltinTypes() { } } class a::b::c`, + + `int (*p)(int, char);`, } for _, t := range tests {