llcppsigfetch:basic cjson dump for ast.File

This commit is contained in:
luoliwoshang
2024-08-12 18:20:15 +08:00
parent a796f9f8a8
commit 6297f69e70
3 changed files with 124 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ import (
"unsafe"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
"github.com/goplus/llgo/c/clang"
"github.com/goplus/llgo/chore/llcppg/ast"
)
@@ -79,19 +80,19 @@ func visit(cursor, parent clang.Cursor, clientData unsafe.Pointer) clang.ChildVi
switch cursor.Kind {
case clang.CursorInclusionDirective:
fmt.Println("todo: Process include")
// todo(zzy)
return clang.ChildVisit_Continue
case clang.CursorMacroDefinition:
fmt.Println("todo: Process macro")
// todo(zzy)
return clang.ChildVisit_Continue
case clang.CursorEnumDecl:
fmt.Println("todo: Process enum")
// todo(zzy)
return clang.ChildVisit_Continue
case clang.CursorClassDecl:
ct.ProcessClass(cursor)
return clang.ChildVisit_Continue
case clang.CursorStructDecl:
fmt.Println("todo: Process struct")
// todo(zzy)
return clang.ChildVisit_Continue
case clang.CursorFunctionDecl:
ct.ProcessFunc(cursor)
@@ -117,8 +118,7 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) {
defer filename.Dispose()
if filename.CStr() == nil {
// For some built-in macros, there is no file.
println("todo: filename is empty")
//todo(zzy): For some built-in macros, there is no file.
return
}
@@ -260,3 +260,65 @@ func IsExplicitUnsigned(t clang.Type) bool {
t.Kind == clang.TypeULong || t.Kind == clang.TypeULongLong ||
t.Kind == clang.TypeUInt128
}
func (ct *Converter) GetFilesJSON() *cjson.JSON {
root := cjson.Object()
Files := cjson.Object()
root.SetItem(c.Str("Files"), Files)
for _, file := range ct.files {
f := cjson.Object()
f.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(file.Path)))
ct.FileJSON(file, f)
Files.SetItem(c.AllocaCStr(file.Path), f)
}
return root
}
func (ct *Converter) FileJSON(file *ast.File, root *cjson.JSON) {
decls := cjson.Array()
includes := cjson.Array()
macros := cjson.Array()
for _, decl := range file.Decls {
ct.DeclJSON(decl, decls)
}
root.SetItem(c.Str("decls"), decls)
root.SetItem(c.Str("includes"), includes)
root.SetItem(c.Str("macros"), macros)
}
func (ct *Converter) DeclJSON(decl ast.Decl, root *cjson.JSON) {
switch d := decl.(type) {
case *ast.FuncDecl:
fn := cjson.Object()
fntype := cjson.Object()
fn.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name.Name)))
ct.TypeJSON(d.Type, fntype)
fn.SetItem(c.Str("Type"), fntype)
root.AddItem(fn)
}
}
func (ct *Converter) TypeJSON(t ast.Expr, root *cjson.JSON) {
switch d := t.(type) {
case *ast.FuncType:
params := cjson.Array()
for _, p := range d.Params.List {
param := cjson.Object()
ct.TypeJSON(p.Type, param)
params.AddItem(param)
}
root.SetItem(c.Str("Params"), params)
ret := cjson.Object()
ct.TypeJSON(d.Ret, ret)
root.SetItem(c.Str("Ret"), ret)
case *ast.BuiltinType:
root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind)))
root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags)))
}
}