Files
llgo/chore/_xtool/llcppsymg/parse/parse.go

183 lines
4.4 KiB
Go
Raw Normal View History

package parse
2024-07-26 13:45:08 +08:00
import (
"errors"
2024-07-26 13:45:08 +08:00
"strconv"
2024-08-08 14:14:20 +08:00
"strings"
2024-07-26 13:45:08 +08:00
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/clang"
2024-09-09 10:18:59 +08:00
"github.com/goplus/llgo/chore/_xtool/llcppsymg/clangutils"
2024-07-26 13:45:08 +08:00
)
type SymbolInfo struct {
GoName string
ProtoName string
}
2024-07-26 13:45:08 +08:00
type Context struct {
2024-09-10 16:16:51 +08:00
prefixes []string
symbolMap map[string]*SymbolInfo
currentFile string
nameCounts map[string]int
2024-07-26 13:45:08 +08:00
}
2024-08-08 14:14:20 +08:00
func newContext(prefixes []string) *Context {
2024-07-26 13:45:08 +08:00
return &Context{
2024-08-08 14:14:20 +08:00
prefixes: prefixes,
symbolMap: make(map[string]*SymbolInfo),
2024-08-08 14:14:20 +08:00
nameCounts: make(map[string]int),
2024-07-26 13:45:08 +08:00
}
}
func (c *Context) setCurrentFile(filename string) {
2024-07-26 17:09:26 +08:00
c.currentFile = filename
2024-07-26 15:56:47 +08:00
}
2024-08-08 14:14:20 +08:00
func (c *Context) removePrefix(str string) string {
for _, prefix := range c.prefixes {
if strings.HasPrefix(str, prefix) {
return strings.TrimPrefix(str, prefix)
}
2024-07-26 13:45:08 +08:00
}
2024-08-08 14:14:20 +08:00
return str
}
2024-07-26 13:45:08 +08:00
2024-09-20 10:16:00 +08:00
func toTitle(s string) string {
if s == "" {
return ""
}
return strings.ToUpper(s[:1]) + strings.ToLower(s[1:])
}
func toCamel(originName string) string {
if originName == "" {
return ""
}
subs := strings.Split(string(originName), "_")
name := ""
for _, sub := range subs {
name += toTitle(sub)
}
return name
}
// 1. remove prefix from config
// 2. convert to camel case
func (c *Context) toGoName(name string) string {
2024-08-08 14:14:20 +08:00
name = c.removePrefix(name)
2024-09-20 10:16:00 +08:00
return toCamel(name)
}
2024-09-10 16:16:51 +08:00
func (p *Context) genGoName(cursor clang.Cursor) string {
funcName := cursor.String()
defer funcName.Dispose()
2024-07-26 14:37:26 +08:00
2024-09-10 16:16:51 +08:00
name := p.toGoName(c.GoString(funcName.CStr()))
if parent := cursor.SemanticParent(); parent.Kind == clang.CursorClassDecl {
parentName := parent.String()
defer parentName.Dispose()
class := p.toGoName(c.GoString(parentName.CStr()))
return p.addSuffix(p.genMethodName(class, name))
2024-07-26 14:37:26 +08:00
}
2024-07-26 13:45:08 +08:00
2024-09-10 16:16:51 +08:00
return p.addSuffix(name)
2024-08-08 14:14:20 +08:00
}
2024-07-26 13:45:08 +08:00
2024-08-08 14:14:20 +08:00
func (c *Context) genMethodName(class, name string) string {
prefix := "(*" + class + ")."
if class == name {
return prefix + "Init"
}
if name == "~"+class {
return prefix + "Dispose"
2024-07-26 13:45:08 +08:00
}
2024-08-08 14:14:20 +08:00
return prefix + name
}
func (p *Context) genProtoName(cursor clang.Cursor) string {
displayName := cursor.DisplayName()
defer displayName.Dispose()
scopingParts := clangutils.BuildScopingParts(cursor.SemanticParent())
var builder strings.Builder
for _, part := range scopingParts {
builder.WriteString(part)
builder.WriteString("::")
}
builder.WriteString(c.GoString(displayName.CStr()))
return builder.String()
}
2024-08-08 14:14:20 +08:00
func (c *Context) addSuffix(name string) string {
c.nameCounts[name]++
count := c.nameCounts[name]
if count > 1 {
return name + "__" + strconv.Itoa(count-1)
2024-07-26 13:45:08 +08:00
}
2024-08-08 14:14:20 +08:00
return name
}
2024-07-26 13:45:08 +08:00
2024-08-08 14:14:20 +08:00
var context = newContext([]string{})
func collectFuncInfo(cursor clang.Cursor) {
symbol := cursor.Mangling()
2024-07-26 13:45:08 +08:00
2024-08-08 14:14:20 +08:00
symbolName := c.GoString(symbol.CStr())
if len(symbolName) >= 1 && symbolName[0] == '_' {
symbolName = symbolName[1:]
2024-07-26 13:45:08 +08:00
}
2024-08-08 14:14:20 +08:00
defer symbol.Dispose()
2024-07-26 13:45:08 +08:00
context.symbolMap[symbolName] = &SymbolInfo{
2024-09-10 16:16:51 +08:00
GoName: context.genGoName(cursor),
ProtoName: context.genProtoName(cursor),
}
2024-07-26 13:45:08 +08:00
}
func visit(cursor, parent clang.Cursor, clientData c.Pointer) clang.ChildVisitResult {
2024-09-10 14:32:41 +08:00
switch cursor.Kind {
case clang.CursorNamespace, clang.CursorClassDecl:
2024-07-26 13:45:08 +08:00
clang.VisitChildren(cursor, visit, nil)
2024-09-10 14:32:41 +08:00
case clang.CursorCXXMethod, clang.CursorFunctionDecl, clang.CursorConstructor, clang.CursorDestructor:
2024-07-26 15:56:47 +08:00
loc := cursor.Location()
var file clang.File
2024-09-10 16:16:51 +08:00
loc.SpellingLocation(&file, nil, nil, nil)
2024-07-26 15:56:47 +08:00
filename := file.FileName()
2024-09-10 14:32:41 +08:00
defer filename.Dispose()
isCurrentFile := c.Strcmp(filename.CStr(), c.AllocaCStr(context.currentFile)) == 0
isPublicMethod := (cursor.CXXAccessSpecifier() == clang.CXXPublic) && cursor.Kind == clang.CursorCXXMethod || cursor.Kind == clang.CursorConstructor || cursor.Kind == clang.CursorDestructor
2024-07-26 15:56:47 +08:00
2024-09-10 14:32:41 +08:00
if isCurrentFile && (cursor.Kind == clang.CursorFunctionDecl || isPublicMethod) {
2024-08-08 14:14:20 +08:00
collectFuncInfo(cursor)
2024-07-26 15:56:47 +08:00
}
2024-07-26 13:45:08 +08:00
}
return clang.ChildVisit_Continue
}
func ParseHeaderFile(filepaths []string, prefixes []string, isCpp bool) (map[string]*SymbolInfo, error) {
2024-08-08 14:14:20 +08:00
context = newContext(prefixes)
2024-09-09 10:42:01 +08:00
index := clang.CreateIndex(0, 0)
for _, filename := range filepaths {
2024-09-09 10:42:01 +08:00
_, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
2024-09-09 10:18:59 +08:00
File: filename,
Temp: false,
IsCpp: isCpp,
2024-09-09 10:42:01 +08:00
Index: index,
2024-09-09 10:18:59 +08:00
})
if err != nil {
return nil, errors.New("Unable to parse translation unit for file " + filename)
2024-07-26 17:09:26 +08:00
}
2024-07-26 13:45:08 +08:00
2024-07-26 17:09:26 +08:00
cursor := unit.Cursor()
context.setCurrentFile(filename)
clang.VisitChildren(cursor, visit, nil)
unit.Dispose()
}
2024-07-26 13:45:08 +08:00
index.Dispose()
2024-08-08 14:14:20 +08:00
return context.symbolMap, nil
2024-07-26 13:45:08 +08:00
}