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

224 lines
5.6 KiB
Go
Raw Normal View History

package parse
2024-07-26 13:45:08 +08:00
import (
"errors"
2024-10-08 11:17:33 +08:00
"fmt"
"os"
"path/filepath"
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
}
type SymbolProcessor struct {
Prefixes []string
SymbolMap map[string]*SymbolInfo
CurrentFile string
NameCounts map[string]int
2024-07-26 13:45:08 +08:00
}
func NewSymbolProcessor(Prefixes []string) *SymbolProcessor {
return &SymbolProcessor{
Prefixes: Prefixes,
SymbolMap: make(map[string]*SymbolInfo),
NameCounts: make(map[string]int),
2024-07-26 13:45:08 +08:00
}
}
func (p *SymbolProcessor) setCurrentFile(filename string) {
p.CurrentFile = filename
2024-07-26 15:56:47 +08:00
}
func (p *SymbolProcessor) TrimPrefixes(str string) string {
for _, prefix := range p.Prefixes {
2024-08-08 14:14:20 +08:00
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]) + (s[1:])
2024-09-20 10:16:00 +08:00
}
func toUpperCamelCase(originName string) string {
2024-09-20 10:16:00 +08:00
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 (p *SymbolProcessor) ToGoName(name string) string {
return toUpperCamelCase(p.TrimPrefixes(name))
2024-09-20 10:16:00 +08:00
}
func (p *SymbolProcessor) GenMethodName(class, name string, isDestructor bool) string {
prefix := "(*" + class + ")."
if isDestructor {
return prefix + "Dispose"
}
if class == name {
return prefix + "Init"
}
return prefix + name
}
func (p *SymbolProcessor) genGoName(cursor clang.Cursor) string {
2024-09-10 16:16:51 +08:00
funcName := cursor.String()
defer funcName.Dispose()
2024-07-26 14:37:26 +08:00
originName := c.GoString(funcName.CStr())
isDestructor := cursor.Kind == clang.CursorDestructor
var convertedName string
if isDestructor {
convertedName = p.ToGoName(originName[1:])
} else {
convertedName = p.ToGoName(originName)
}
2024-09-10 16:16:51 +08:00
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, convertedName, isDestructor))
2024-07-26 14:37:26 +08:00
}
return p.AddSuffix(convertedName)
2024-08-08 14:14:20 +08:00
}
2024-07-26 13:45:08 +08:00
func (p *SymbolProcessor) 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()
}
func (p *SymbolProcessor) AddSuffix(name string) string {
p.NameCounts[name]++
if count := p.NameCounts[name]; count > 1 {
2024-08-08 14:14:20 +08:00
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
func (p *SymbolProcessor) collectFuncInfo(cursor clang.Cursor) {
2024-08-08 14:14:20 +08:00
symbol := cursor.Mangling()
defer symbol.Dispose()
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
}
p.SymbolMap[symbolName] = &SymbolInfo{
GoName: p.genGoName(cursor),
ProtoName: p.genProtoName(cursor),
}
2024-07-26 13:45:08 +08:00
}
func (p *SymbolProcessor) visitTop(cursor, parent clang.Cursor) clang.ChildVisitResult {
2024-09-10 14:32:41 +08:00
switch cursor.Kind {
case clang.CursorNamespace, clang.CursorClassDecl:
clangutils.VisitChildren(cursor, p.visitTop)
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()
2024-10-08 11:17:33 +08:00
file, _, _, _ := clangutils.GetLocation(loc)
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(p.CurrentFile)) == 0
2024-09-10 14:32:41 +08:00
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) {
p.collectFuncInfo(cursor)
2024-07-26 15:56:47 +08:00
}
2024-07-26 13:45:08 +08:00
}
return clang.ChildVisit_Continue
}
func ParseHeaderFile(files []string, Prefixes []string, isCpp bool, isTemp bool) (map[string]*SymbolInfo, error) {
processer := NewSymbolProcessor(Prefixes)
2024-09-09 10:42:01 +08:00
index := clang.CreateIndex(0, 0)
for _, file := range files {
2024-09-09 10:42:01 +08:00
_, unit, err := clangutils.CreateTranslationUnit(&clangutils.Config{
File: file,
Temp: isTemp,
2024-09-09 10:18:59 +08:00
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 " + file)
2024-07-26 17:09:26 +08:00
}
cursor := unit.Cursor()
if isTemp {
processer.setCurrentFile(clangutils.TEMP_FILE)
} else {
processer.setCurrentFile(file)
}
clangutils.VisitChildren(cursor, processer.visitTop)
2024-07-26 17:09:26 +08:00
unit.Dispose()
}
2024-07-26 13:45:08 +08:00
index.Dispose()
return processer.SymbolMap, nil
2024-07-26 13:45:08 +08:00
}
2024-10-08 11:17:33 +08:00
func GenHeaderFilePath(cflags string, files []string) ([]string, error) {
prefixPath := strings.TrimPrefix(cflags, "-I")
var validPaths []string
var errs []string
for _, file := range files {
if file == "" {
continue
}
fullPath := filepath.Join(prefixPath, file)
if f, err := os.Open(fullPath); err != nil {
if os.IsNotExist(err) {
errs = append(errs, fmt.Sprintf("file not found: %s", file))
} else {
errs = append(errs, fmt.Sprintf("error accessing file %s: %v", file, err))
}
} else {
f.Close()
validPaths = append(validPaths, fullPath)
}
}
if len(validPaths) == 0 && len(errs) == 0 {
return nil, fmt.Errorf("no valid header files")
}
if len(errs) > 0 {
return validPaths, fmt.Errorf("some files not found or inaccessible: %v", errs)
}
return validPaths, nil
}