From 1996db4b95b9de7ec1b48f27e25e7a97b00091d4 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 13 Aug 2024 17:05:20 +0800 Subject: [PATCH] llcppsigfetch:refine cjson dump logic --- chore/_xtool/llcppsigfetch/parse/cvt.go | 74 +-------------- chore/_xtool/llcppsigfetch/parse/dump.go | 111 +++++++++++++++++++++++ 2 files changed, 112 insertions(+), 73 deletions(-) create mode 100644 chore/_xtool/llcppsigfetch/parse/dump.go diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index de9965d0..94e4ff32 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -7,15 +7,12 @@ 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" ) type Converter struct { - files map[string]*ast.File - // typeMap map[clang.Type]ast.Expr // todo(zzy):cache type - // declMap map[clang.Cursor]ast.Decl + files map[string]*ast.File curLoc ast.Location curFile *ast.File index *clang.Index @@ -60,8 +57,6 @@ func NewConverter(file string, temp bool) (*Converter, error) { } return &Converter{ - // typeMap: make(map[clang.Type]ast.Expr), - // declMap: make(map[clang.Cursor]ast.Decl), files: make(map[string]*ast.File), index: index, unit: unit, @@ -139,10 +134,6 @@ func (ct *Converter) UpdateCurFile(cursor clang.Cursor) { } func (ct *Converter) ProcessType(t clang.Type) ast.Expr { - // todo(zzy):cache type - // if cache, ok := ct.typeMap[t]; ok { - // return cache - // } var expr ast.Expr if t.Kind >= clang.TypeFirstBuiltin && t.Kind <= clang.TypeLastBuiltin { return ct.ProcessBuiltinType(t) @@ -167,7 +158,6 @@ func (ct *Converter) ProcessType(t clang.Type) ast.Expr { } // todo(zzy):array length } - // ct.typeMap[t] = expr return expr } @@ -260,65 +250,3 @@ 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))) - } -} diff --git a/chore/_xtool/llcppsigfetch/parse/dump.go b/chore/_xtool/llcppsigfetch/parse/dump.go new file mode 100644 index 00000000..7c1dc9d7 --- /dev/null +++ b/chore/_xtool/llcppsigfetch/parse/dump.go @@ -0,0 +1,111 @@ +package parse + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/cjson" + "github.com/goplus/llgo/chore/llcppg/ast" +) + +func (ct *Converter) GetFilesJSON() *cjson.JSON { + root := cjson.Object() + for _, file := range ct.files { + f := cjson.Object() + f.SetItem(c.Str("Path"), cjson.String(c.AllocaCStr(file.Path))) + ct.FileJSON(file, f) + root.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 { + decls.AddItem(ct.DeclJSON(decl)) + } + + 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) *cjson.JSON { + if decl == nil { + return cjson.Null() + } + root := cjson.Object() + switch d := decl.(type) { + case *ast.FuncDecl: + ct.DeclBaseJSON(d.DeclBase, root) + root.SetItem(c.Str("Name"), ct.TypeJSON(d.Name)) + root.SetItem(c.Str("Type"), ct.TypeJSON(d.Type)) + } + return root +} +func (ct *Converter) DeclBaseJSON(decl ast.DeclBase, root *cjson.JSON) { + if decl.Loc == nil { + root.SetItem(c.Str("Loc"), cjson.Null()) + } else { + loc := cjson.Object() + loc.SetItem(c.Str("File"), cjson.String(c.AllocaCStr(decl.Loc.File))) + root.SetItem(c.Str("Loc"), loc) + } + root.SetItem(c.Str("Doc"), ct.TypeJSON(decl.Doc)) + root.SetItem(c.Str("Parent"), ct.TypeJSON(decl.Parent)) +} + +func (ct *Converter) TypeJSON(t ast.Expr) *cjson.JSON { + if t == nil { + return cjson.Null() + } + + root := cjson.Object() + + switch d := t.(type) { + case *ast.FuncType: + root.SetItem(c.Str("Params"), ct.TypeJSON(d.Params)) + root.SetItem(c.Str("Ret"), ct.TypeJSON(d.Ret)) + case *ast.FieldList: + if d == nil { + return cjson.Null() + } + list := cjson.Array() + for _, f := range d.List { + list.AddItem(ct.TypeJSON(f)) + } + root.SetItem(c.Str("List"), list) + case *ast.Field: + root.SetItem(c.Str("Type"), ct.TypeJSON(d.Type)) + root.SetItem(c.Str("Doc"), ct.TypeJSON(d.Doc)) + root.SetItem(c.Str("Comment"), ct.TypeJSON(d.Comment)) + names := cjson.Array() + for _, n := range d.Names { + names.AddItem(ct.TypeJSON(n)) + } + root.SetItem(c.Str("Names"), names) + case *ast.Ident: + root.SetItem(c.Str("Name"), cjson.String(c.AllocaCStr(d.Name))) + case *ast.PointerType: + root.SetItem(c.Str("X"), ct.TypeJSON(d.X)) + case *ast.BuiltinType: + root.SetItem(c.Str("Kind"), cjson.Number(float64(d.Kind))) + root.SetItem(c.Str("Flags"), cjson.Number(float64(d.Flags))) + case *ast.Comment: + root.SetItem(c.Str("Text"), cjson.String(c.AllocaCStr(d.Text))) + case *ast.CommentGroup: + if d == nil { + return cjson.Null() + } + list := cjson.Array() + for _, c := range d.List { + println(c.Text) + list.AddItem(ct.TypeJSON(c)) + } + root.SetItem(c.Str("List"), list) + default: + return cjson.Null() + } + return root +}