From 8d70aba1f53e1032f5923b7cf83941f2776ef1dc Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 7 Aug 2024 20:07:27 +0800 Subject: [PATCH] castdump:funcproto type & enum value --- c/clang/_wrap/cursor.cpp | 12 +++++-- c/clang/clang.go | 59 +++++++++++++++++++++++++++++++ chore/_xtool/castdump/castdump.go | 43 +++++++++++++++------- 3 files changed, 100 insertions(+), 14 deletions(-) diff --git a/c/clang/_wrap/cursor.cpp b/c/clang/_wrap/cursor.cpp index 45b514b8..378c0a09 100644 --- a/c/clang/_wrap/cursor.cpp +++ b/c/clang/_wrap/cursor.cpp @@ -31,6 +31,16 @@ void wrap_clang_getTranslationUnitCursor(CXTranslationUnit uint, CXCursor *cur) void wrap_clang_getCursorType(CXCursor *cur, CXType *typ) { *typ = clang_getCursorType(*cur); } +void wrap_clang_getCursorResultType(CXCursor *cur, CXType *typ) { *typ = clang_getCursorResultType(*cur); } + +void wrap_clang_getResultType(CXType *typ, CXType *resultTyp) { *resultTyp = clang_getResultType(*typ); } + +int wrap_clang_getNumArgTypes(CXType *typ) { return clang_getNumArgTypes(*typ); } + +void wrap_clang_getArgType(CXType *typ, unsigned i, CXType *argTyp) { *argTyp = clang_getArgType(*typ, i); } + +long long wrap_clang_getEnumConstantDeclValue(CXCursor *cur) { return clang_getEnumConstantDeclValue(*cur); } + void wrap_clang_getPointeeType(CXType *pointerTyp, CXType *pointeeTyp) { *pointeeTyp = clang_getPointeeType(*pointerTyp); } @@ -39,8 +49,6 @@ void wrap_clang_getArrayElementType(CXType *arrayTyp, CXType *elemTyp) { *elemTyp = clang_getArrayElementType(*arrayTyp); } -void wrap_clang_getCursorResultType(CXCursor *cur, CXType *typ) { *typ = clang_getCursorResultType(*cur); } - void wrap_clang_getCanonicalType(CXType *typ, CXType *canonicalType) { *canonicalType = clang_getCanonicalType(*typ); } CXString wrap_clang_getTypeSpelling(CXType *typ) { return clang_getTypeSpelling(*typ); } diff --git a/c/clang/clang.go b/c/clang/clang.go index e06a24c1..69a550bd 100644 --- a/c/clang/clang.go +++ b/c/clang/clang.go @@ -1608,6 +1608,22 @@ func (c Cursor) ResultType() (ret Type) { return } +/** + * Retrieve the integer value of an enum constant declaration as a signed + * long long. + * + * If the cursor does not reference an enum constant declaration, LLONG_MIN is + * returned. Since this is also potentially a valid constant value, the kind of + * the cursor must be verified before calling this function. + */ +// llgo:link (*Cursor).wrapEnumConstantDeclValue C.wrap_clang_getEnumConstantDeclValue +func (*Cursor) wrapEnumConstantDeclValue() (ret c.LongLong) { + return 0 +} +func (c Cursor) EnumConstantDeclValue() (ret c.LongLong) { + return c.wrapEnumConstantDeclValue() +} + /** * Retrieve the number of non-variadic arguments associated with a given * cursor. @@ -1788,6 +1804,47 @@ func (t Type) String() (ret String) { return t.wrapString() } +/** + * Retrieve the return type associated with a function type. + * + * If a non-function type is passed in, an invalid type is returned. + */ +// llgo:link (*Type).wrapResultType C.wrap_clang_getResultType +func (t *Type) wrapResultType(ret *Type) { return } + +func (t Type) ResultType() (ret Type) { + t.wrapResultType(&ret) + return +} + +/** + * Retrieve the number of non-variadic parameters associated with a + * function type. + * + * If a non-function type is passed in, -1 is returned. + */ +// llgo:link (*Type).wrapNumArgTypes C.wrap_clang_getNumArgTypes +func (t *Type) wrapNumArgTypes() (num c.Int) { return 0 } + +func (t Type) NumArgTypes() (num c.Int) { + return t.wrapNumArgTypes() +} + +// void wrap_clang_getArgType(CXType *typ, unsigned i, CXType *argTyp) { *argTyp = clang_getArgType(*typ, i); } +/** + * Retrieve the type of a parameter of a function type. + * + * If a non-function type is passed in or the function does not have enough + * parameters, an invalid type is returned. + */ +// llgo:link (*Type).wrapArgType C.wrap_clang_getArgType +func (t *Type) wrapArgType(index c.Uint, argTyp *Type) { return } + +func (t Type) ArgType(index c.Uint) (ret Type) { + t.wrapArgType(index, &ret) + return +} + /** * For pointer types, returns the type of the pointee. */ @@ -1806,6 +1863,7 @@ func (t Type) PointeeType() (ret Type) { */ // llgo:link (*Type).wrapArrayElementType C.wrap_clang_getArrayElementType func (t *Type) wrapArrayElementType(ret *Type) { return } + func (t Type) ArrayElementType() (ret Type) { t.wrapArrayElementType(&ret) return @@ -1821,6 +1879,7 @@ func (t Type) ArrayElementType() (ret Type) { */ // llgo:link (*Type).wrapCanonicalType C.wrap_clang_getCanonicalType func (t *Type) wrapCanonicalType(ret *Type) { return } + func (t Type) CanonicalType() (ret Type) { t.wrapCanonicalType(&ret) return diff --git a/chore/_xtool/castdump/castdump.go b/chore/_xtool/castdump/castdump.go index 0cc9207b..5b8ac793 100644 --- a/chore/_xtool/castdump/castdump.go +++ b/chore/_xtool/castdump/castdump.go @@ -23,7 +23,7 @@ var accessMap = map[clang.CXXAccessSpecifier]string{ } func printIndent(depth c.Uint) { - fmt.Print(strings.Repeat(" ", int(depth))) + fmt.Print(strings.Repeat(" ", int(depth))) } func accessToString(spec clang.CXXAccessSpecifier) string { @@ -44,7 +44,11 @@ func printType(t clang.Type, data *Data) { typeSpell := t.String() typeKind := t.Kind.String() - if t.Kind > clang.TypeFirstBuiltin && t.Kind < clang.TypeLastBuiltin { + + if t.Kind == clang.TypeInvalid { + } else if t.Kind == clang.TypeUnexposed { + c.Printf(c.Str(": %s\n"), typeKind.CStr(), typeSpell.CStr()) + } else if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { c.Printf(c.Str(": %s\n"), typeKind.CStr(), typeSpell.CStr()) } else if t.Kind > clang.TypeComplex { c.Printf(c.Str(": %s\n"), typeKind.CStr(), typeSpell.CStr()) @@ -58,6 +62,11 @@ func printType(t clang.Type, data *Data) { printType(t.ArrayElementType(), data) case clang.TypeTypedef: printType(t.CanonicalType(), data) + case clang.TypeFunctionProto: + printType(t.ResultType(), data) + for i := 0; i < int(t.NumArgTypes()); i++ { + printType(t.ArgType(c.Uint(i)), data) + } } data.Depth-- @@ -83,7 +92,8 @@ func printAccess(cursor clang.Cursor) { defer kind.Dispose() defer spell.Dispose() - c.Printf(c.Str("%s: %s %s\n"), kind.CStr(), spell.CStr(), c.AllocaCStr(accessToString(cursor.CXXAccessSpecifier()))) + c.Printf(c.Str("%s: %s %s"), kind.CStr(), spell.CStr(), c.AllocaCStr(accessToString(cursor.CXXAccessSpecifier()))) + printLocation(cursor) } func printMacro(cursor clang.Cursor, unit *clang.TranslationUnit) { @@ -101,20 +111,30 @@ func printMacro(cursor clang.Cursor, unit *clang.TranslationUnit) { c.Printf(c.Str("%s "), tokStr.CStr()) tokStr.Dispose() } - c.Printf(c.Str("\n")) + printLocation(cursor) } -func printFunc(cursor clang.Cursor) { +func printFunc(cursor clang.Cursor, data *Data) { kind := cursor.Kind.String() spell := cursor.String() symbol := cursor.Mangling() - typ := cursor.Type().String() defer symbol.Dispose() defer kind.Dispose() defer spell.Dispose() - defer typ.Dispose() - c.Printf(c.Str("%s: %s (Type: %s)(Symbol: %s)"), kind.CStr(), spell.CStr(), typ.CStr(), symbol.CStr()) + c.Printf(c.Str("%s: %s (Symbol: %s)"), kind.CStr(), spell.CStr(), symbol.CStr()) + printLocation(cursor) + printType(cursor.Type(), data) +} + +func printEnumConstant(cursor clang.Cursor) { + kind := cursor.Kind.String() + spell := cursor.String() + defer kind.Dispose() + defer spell.Dispose() + + c.Printf(c.Str("%s: %s:%lld"), kind.CStr(), spell.CStr(), cursor.EnumConstantDeclValue()) + printLocation(cursor) } func printDefault(cursor clang.Cursor, data *Data) { @@ -142,13 +162,12 @@ func printAST(cursor clang.Cursor, data *Data) { switch cursor.Kind { case clang.CursorCXXAccessSpecifier: printAccess(cursor) - printLocation(cursor) case clang.CursorMacroDefinition: printMacro(cursor, data.Unit) - printLocation(cursor) case clang.CursorFunctionDecl, clang.CursorCXXMethod, clang.CursorConstructor, clang.CursorDestructor: - printFunc(cursor) - printLocation(cursor) + printFunc(cursor, data) + case clang.CursorEnumConstantDecl: + printEnumConstant(cursor) default: printDefault(cursor, data) }