llcppsigfetch:refine cjson dump logic

This commit is contained in:
luoliwoshang
2024-08-13 17:05:20 +08:00
parent 6297f69e70
commit 1996db4b95
2 changed files with 112 additions and 73 deletions

View File

@@ -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)))
}
}

View File

@@ -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
}