Files
llgo/chore/_xtool/llcppsigfetch/parse/dump.go

283 lines
8.4 KiB
Go
Raw Normal View History

2024-08-13 17:05:20 +08:00
package parse
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/cjson"
"github.com/goplus/llgo/chore/llcppg/ast"
)
2024-09-19 16:49:05 +08:00
func MarshalOutputASTFiles(files []*FileEntry) *cjson.JSON {
2024-09-19 15:35:42 +08:00
root := cjson.Array()
2024-09-19 16:49:05 +08:00
for _, entry := range files {
2024-09-19 15:35:42 +08:00
f := cjson.Object()
2024-09-19 16:49:05 +08:00
path := cjson.String(c.AllocaCStr(entry.Path))
2024-09-19 15:35:42 +08:00
f.SetItem(c.Str("path"), path)
2024-09-19 16:49:05 +08:00
f.SetItem(c.Str("doc"), MarshalASTFile(entry.Doc))
2024-09-19 15:35:42 +08:00
root.AddItem(f)
}
return root
}
2024-09-19 16:49:05 +08:00
func MarshalASTFiles(files []*FileEntry) *cjson.JSON {
2024-08-13 17:05:20 +08:00
root := cjson.Object()
2024-09-19 16:49:05 +08:00
for _, entry := range files {
root.SetItem(c.AllocaCStr(entry.Path), MarshalASTFile(entry.Doc))
2024-08-13 17:05:20 +08:00
}
return root
}
2024-09-02 15:47:05 +08:00
func MarshalDeclList(list []ast.Decl) *cjson.JSON {
if list == nil {
return cjson.Null()
}
root := cjson.Array()
for _, item := range list {
root.AddItem(MarshalASTDecl(item))
}
return root
}
2024-08-13 17:05:20 +08:00
2024-09-02 15:47:05 +08:00
func MarshalFieldList(list []*ast.Field) *cjson.JSON {
if list == nil {
return cjson.Null()
2024-08-13 17:05:20 +08:00
}
2024-09-02 15:47:05 +08:00
root := cjson.Array()
for _, item := range list {
root.AddItem(MarshalASTExpr(item))
}
return root
}
2024-08-16 11:52:22 +08:00
2024-09-02 15:47:05 +08:00
func MarshalIncludeList(list []*ast.Include) *cjson.JSON {
if list == nil {
return cjson.Null()
}
root := cjson.Array()
for _, item := range list {
include := cjson.Object()
include.SetItem(c.Str("_Type"), stringField("Include"))
include.SetItem(c.Str("Path"), stringField(item.Path))
root.AddItem(include)
2024-08-16 11:52:22 +08:00
}
2024-09-02 15:47:05 +08:00
return root
}
2024-08-16 11:52:22 +08:00
2024-09-02 15:47:05 +08:00
func MarshalMacroList(list []*ast.Macro) *cjson.JSON {
if list == nil {
return cjson.Null()
}
root := cjson.Array()
for _, item := range list {
macro := cjson.Object()
macro.SetItem(c.Str("_Type"), stringField("Macro"))
macro.SetItem(c.Str("Name"), stringField(item.Name))
macro.SetItem(c.Str("Tokens"), MarshalTokenList(item.Tokens))
root.AddItem(macro)
2024-08-16 11:52:22 +08:00
}
2024-08-16 18:39:09 +08:00
return root
}
2024-09-02 15:47:05 +08:00
func MarshalTokenList(list []*ast.Token) *cjson.JSON {
if list == nil {
return cjson.Null()
}
root := cjson.Array()
for _, item := range list {
root.AddItem(MarshalToken(item))
}
return root
}
func MarshalIdentList(list []*ast.Ident) *cjson.JSON {
if list == nil {
return cjson.Null()
}
root := cjson.Array()
for _, item := range list {
root.AddItem(MarshalASTExpr(item))
}
return root
}
func MarshalASTFile(file *ast.File) *cjson.JSON {
root := cjson.Object()
root.SetItem(c.Str("_Type"), stringField("File"))
root.SetItem(c.Str("decls"), MarshalDeclList(file.Decls))
root.SetItem(c.Str("includes"), MarshalIncludeList(file.Includes))
root.SetItem(c.Str("macros"), MarshalMacroList(file.Macros))
return root
}
func MarshalToken(tok *ast.Token) *cjson.JSON {
2024-08-16 18:39:09 +08:00
root := cjson.Object()
root.SetItem(c.Str("_Type"), stringField("Token"))
root.SetItem(c.Str("Token"), numberField(uint(tok.Token)))
root.SetItem(c.Str("Lit"), stringField(tok.Lit))
2024-08-15 09:27:41 +08:00
return root
2024-08-13 17:05:20 +08:00
}
2024-08-16 11:52:22 +08:00
func MarshalASTDecl(decl ast.Decl) *cjson.JSON {
2024-08-13 17:05:20 +08:00
if decl == nil {
return cjson.Null()
}
root := cjson.Object()
switch d := decl.(type) {
2024-08-19 12:07:30 +08:00
case *ast.EnumTypeDecl:
root.SetItem(c.Str("_Type"), stringField("EnumTypeDecl"))
2024-08-19 12:07:30 +08:00
MarshalASTDeclBase(d.DeclBase, root)
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))
2024-08-23 14:57:36 +08:00
root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type))
case *ast.TypedefDecl:
root.SetItem(c.Str("_Type"), stringField("TypedefDecl"))
MarshalASTDeclBase(d.DeclBase, root)
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))
root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type))
2024-08-13 17:05:20 +08:00
case *ast.FuncDecl:
root.SetItem(c.Str("_Type"), stringField("FuncDecl"))
2024-08-16 11:52:22 +08:00
MarshalASTDeclBase(d.DeclBase, root)
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))
2024-09-10 12:04:46 +08:00
root.SetItem(c.Str("MangledName"), stringField(d.MangledName))
2024-08-16 11:52:22 +08:00
root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type))
root.SetItem(c.Str("IsInline"), boolField(d.IsInline))
root.SetItem(c.Str("IsStatic"), boolField(d.IsStatic))
root.SetItem(c.Str("IsConst"), boolField(d.IsConst))
root.SetItem(c.Str("IsExplicit"), boolField(d.IsExplicit))
root.SetItem(c.Str("IsConstructor"), boolField(d.IsConstructor))
root.SetItem(c.Str("IsDestructor"), boolField(d.IsDestructor))
root.SetItem(c.Str("IsVirtual"), boolField(d.IsVirtual))
root.SetItem(c.Str("IsOverride"), boolField(d.IsOverride))
case *ast.TypeDecl:
root.SetItem(c.Str("_Type"), stringField("TypeDecl"))
2024-08-16 11:52:22 +08:00
MarshalASTDeclBase(d.DeclBase, root)
2024-08-20 14:19:48 +08:00
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))
2024-08-22 09:51:14 +08:00
root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type))
2024-08-13 17:05:20 +08:00
}
return root
}
2024-08-16 11:52:22 +08:00
func MarshalASTDeclBase(decl ast.DeclBase, root *cjson.JSON) {
loc := cjson.Object()
loc.SetItem(c.Str("_Type"), stringField("Location"))
loc.SetItem(c.Str("File"), stringField(decl.Loc.File))
2024-08-16 11:52:22 +08:00
root.SetItem(c.Str("Loc"), loc)
root.SetItem(c.Str("Doc"), MarshalASTExpr(decl.Doc))
root.SetItem(c.Str("Parent"), MarshalASTExpr(decl.Parent))
2024-08-13 17:05:20 +08:00
}
2024-08-16 11:52:22 +08:00
func MarshalASTExpr(t ast.Expr) *cjson.JSON {
2024-08-13 17:05:20 +08:00
if t == nil {
return cjson.Null()
}
root := cjson.Object()
switch d := t.(type) {
2024-08-23 14:57:36 +08:00
case *ast.EnumType:
root.SetItem(c.Str("_Type"), stringField("EnumType"))
2024-08-23 14:57:36 +08:00
items := cjson.Array()
for _, e := range d.Items {
items.AddItem(MarshalASTExpr(e))
}
root.SetItem(c.Str("Items"), items)
case *ast.EnumItem:
root.SetItem(c.Str("_Type"), stringField("EnumItem"))
2024-08-23 14:57:36 +08:00
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))
root.SetItem(c.Str("Value"), MarshalASTExpr(d.Value))
2024-08-22 09:51:14 +08:00
case *ast.RecordType:
root.SetItem(c.Str("_Type"), stringField("RecordType"))
root.SetItem(c.Str("Tag"), numberField(uint(d.Tag)))
2024-08-22 09:51:14 +08:00
root.SetItem(c.Str("Fields"), MarshalASTExpr(d.Fields))
methods := cjson.Array()
for _, m := range d.Methods {
methods.AddItem(MarshalASTDecl(m))
}
root.SetItem(c.Str("Methods"), methods)
2024-08-13 17:05:20 +08:00
case *ast.FuncType:
root.SetItem(c.Str("_Type"), stringField("FuncType"))
2024-08-16 11:52:22 +08:00
root.SetItem(c.Str("Params"), MarshalASTExpr(d.Params))
root.SetItem(c.Str("Ret"), MarshalASTExpr(d.Ret))
2024-08-13 17:05:20 +08:00
case *ast.FieldList:
root.SetItem(c.Str("_Type"), stringField("FieldList"))
2024-09-02 15:47:05 +08:00
root.SetItem(c.Str("List"), MarshalFieldList(d.List))
2024-08-13 17:05:20 +08:00
case *ast.Field:
root.SetItem(c.Str("_Type"), stringField("Field"))
2024-08-16 11:52:22 +08:00
root.SetItem(c.Str("Type"), MarshalASTExpr(d.Type))
root.SetItem(c.Str("Doc"), MarshalASTExpr(d.Doc))
root.SetItem(c.Str("Comment"), MarshalASTExpr(d.Comment))
root.SetItem(c.Str("IsStatic"), boolField(d.IsStatic))
root.SetItem(c.Str("Access"), numberField(uint(d.Access)))
2024-09-02 15:47:05 +08:00
root.SetItem(c.Str("Names"), MarshalIdentList(d.Names))
2024-08-27 16:18:29 +08:00
case *ast.Variadic:
root.SetItem(c.Str("_Type"), stringField("Variadic"))
2024-08-13 17:05:20 +08:00
case *ast.Ident:
root.SetItem(c.Str("_Type"), stringField("Ident"))
2024-08-21 14:50:42 +08:00
if d == nil {
return cjson.Null()
}
root.SetItem(c.Str("Name"), stringField(d.Name))
2024-08-22 15:57:21 +08:00
case *ast.TagExpr:
root.SetItem(c.Str("_Type"), stringField("TagExpr"))
2024-08-22 15:57:21 +08:00
root.SetItem(c.Str("Name"), MarshalASTExpr(d.Name))
root.SetItem(c.Str("Tag"), numberField(uint(d.Tag)))
case *ast.BasicLit:
root.SetItem(c.Str("_Type"), stringField("BasicLit"))
root.SetItem(c.Str("Kind"), numberField(uint(d.Kind)))
root.SetItem(c.Str("Value"), stringField(d.Value))
2024-08-22 12:15:06 +08:00
case *ast.LvalueRefType:
root.SetItem(c.Str("_Type"), stringField("LvalueRefType"))
2024-08-22 12:15:06 +08:00
root.SetItem(c.Str("X"), MarshalASTExpr(d.X))
case *ast.RvalueRefType:
root.SetItem(c.Str("_Type"), stringField("RvalueRefType"))
2024-08-22 12:15:06 +08:00
root.SetItem(c.Str("X"), MarshalASTExpr(d.X))
2024-08-13 17:05:20 +08:00
case *ast.PointerType:
root.SetItem(c.Str("_Type"), stringField("PointerType"))
2024-08-16 11:52:22 +08:00
root.SetItem(c.Str("X"), MarshalASTExpr(d.X))
case *ast.ArrayType:
root.SetItem(c.Str("_Type"), stringField("ArrayType"))
2024-08-16 11:52:22 +08:00
root.SetItem(c.Str("Elt"), MarshalASTExpr(d.Elt))
root.SetItem(c.Str("Len"), MarshalASTExpr(d.Len))
2024-08-13 17:05:20 +08:00
case *ast.BuiltinType:
root.SetItem(c.Str("_Type"), stringField("BuiltinType"))
root.SetItem(c.Str("Kind"), numberField(uint(d.Kind)))
root.SetItem(c.Str("Flags"), numberField(uint(d.Flags)))
2024-08-13 17:05:20 +08:00
case *ast.Comment:
root.SetItem(c.Str("_Type"), stringField("Comment"))
2024-08-21 14:50:42 +08:00
if d == nil {
return cjson.Null()
}
root.SetItem(c.Str("Text"), stringField(d.Text))
2024-08-13 17:05:20 +08:00
case *ast.CommentGroup:
root.SetItem(c.Str("_Type"), stringField("CommentGroup"))
2024-08-13 17:05:20 +08:00
if d == nil {
return cjson.Null()
}
list := cjson.Array()
for _, c := range d.List {
2024-08-16 11:52:22 +08:00
list.AddItem(MarshalASTExpr(c))
2024-08-13 17:05:20 +08:00
}
root.SetItem(c.Str("List"), list)
2024-08-15 11:26:34 +08:00
case *ast.ScopingExpr:
root.SetItem(c.Str("_Type"), stringField("ScopingExpr"))
2024-08-16 11:52:22 +08:00
root.SetItem(c.Str("X"), MarshalASTExpr(d.X))
root.SetItem(c.Str("Parent"), MarshalASTExpr(d.Parent))
2024-08-13 17:05:20 +08:00
default:
return cjson.Null()
}
return root
}
func stringField(s string) *cjson.JSON {
return cjson.String(c.AllocaCStr(s))
}
func numberField(n uint) *cjson.JSON {
return cjson.Number(float64(n))
}
func boolField(b bool) *cjson.JSON {
if b {
return cjson.True()
}
return cjson.False()
}