Skip to content

Commit 3e25311

Browse files
committed
llcppsymg: go symbol name for go struct methodname
1 parent e2ad6a1 commit 3e25311

File tree

8 files changed

+185
-110
lines changed

8 files changed

+185
-110
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.dll
88
*.so
99
*.dylib
10+
*.DS_Store
1011

1112
# Test binary, built with `go test -c`
1213
*.test
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#stdout
2+
=== Test ToGoName ===
3+
Before: lua_closethread After: Closethread
4+
Before: luaL_checknumber After: Checknumber
5+
Before: sqlite3_close_v2 After: CloseV2
6+
Before: sqlite3_callback After: Callback
7+
Before: GetReal After: GetReal
8+
Before: GetBoolean After: GetBoolean
9+
Before: INIReader After: Reader
10+
11+
12+
#stderr
13+
14+
#exit 0
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/goplus/llcppg/_xtool/llcppsymg/names"
7+
"github.com/goplus/llcppg/_xtool/llcppsymg/parse"
8+
)
9+
10+
func main() {
11+
TestToGoName()
12+
}
13+
14+
func TestToGoName() {
15+
fmt.Println("=== Test ToGoName ===")
16+
process1 := parse.NewSymbolProcessor([]string{}, []string{"lua_", "luaL_"})
17+
process2 := parse.NewSymbolProcessor([]string{}, []string{"sqlite3_", "sqlite3_"})
18+
process3 := parse.NewSymbolProcessor([]string{}, []string{"INI"})
19+
20+
testCases := []struct {
21+
processor *parse.SymbolProcessor
22+
input string
23+
}{
24+
{process1, "lua_closethread"},
25+
{process1, "luaL_checknumber"},
26+
{process2, "sqlite3_close_v2"},
27+
{process2, "sqlite3_callback"},
28+
{process3, "GetReal"},
29+
{process3, "GetBoolean"},
30+
{process3, "INIReader"},
31+
}
32+
33+
for _, tc := range testCases {
34+
result := names.GoName(tc.input, tc.processor.Prefixes, true)
35+
fmt.Printf("Before: %s After: %s\n", tc.input, result)
36+
}
37+
fmt.Println()
38+
}

_xtool/llcppsymg/_cmptest/parse_test/llgo.expect

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22
=== Test NewSymbolProcessor ===
33
Before: No prefixes After: Prefixes: [lua_ luaL_]
44

5-
=== Test RemovePrefix ===
6-
Before: lua_closethread After: closethread
7-
Before: luaL_checknumber After: checknumber
8-
9-
=== Test ToGoName ===
10-
Before: lua_closethread After: Closethread
11-
Before: luaL_checknumber After: Checknumber
12-
Before: sqlite3_close_v2 After: CloseV2
13-
Before: sqlite3_callback After: Callback
14-
Before: GetReal After: GetReal
15-
Before: GetBoolean After: GetBoolean
16-
Before: INIReader After: Reader
17-
185
=== Test GenMethodName ===
196
Before: Class: INIReader, Name: INIReader After: (*INIReader).Init
207
Before: Class: INIReader, Name: INIReader After: (*INIReader).Dispose
@@ -35,8 +22,9 @@ Symbol Map GoName: (*Reader).ParseError, ProtoName In HeaderFile: INIReader::Par
3522

3623
=== Test Case: C Functions ===
3724
Parsed Symbols:
38-
Symbol Map GoName: Compare, ProtoName In HeaderFile: lua_compare(lua_State *, int, int, int), MangledName: lua_compare
39-
Symbol Map GoName: Rawequal, ProtoName In HeaderFile: lua_rawequal(lua_State *, int, int), MangledName: lua_rawequal
25+
Symbol Map GoName: (*State).Compare, ProtoName In HeaderFile: lua_compare(lua_State *, int, int, int), MangledName: lua_compare
26+
Symbol Map GoName: (*State).Rawequal, ProtoName In HeaderFile: lua_rawequal(lua_State *, int, int), MangledName: lua_rawequal
27+
Symbol Map GoName: Sizecomp, ProtoName In HeaderFile: lua_sizecomp(int, int, int, int), MangledName: lua_sizecomp
4028

4129

4230
#stderr

_xtool/llcppsymg/_cmptest/parse_test/parse.go

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,24 @@ import (
44
"fmt"
55
"sort"
66

7+
"github.com/goplus/llcppg/_xtool/llcppsymg/names"
78
"github.com/goplus/llcppg/_xtool/llcppsymg/parse"
89
)
910

1011
func main() {
1112
TestNewSymbolProcessor()
12-
TestRemovePrefix()
13-
TestToGoName()
1413
TestGenMethodName()
1514
TestAddSuffix()
1615
TestParseHeaderFile()
1716
}
1817

1918
func TestNewSymbolProcessor() {
2019
fmt.Println("=== Test NewSymbolProcessor ===")
21-
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
20+
process := parse.NewSymbolProcessor([]string{}, []string{"lua_", "luaL_"})
2221
fmt.Printf("Before: No prefixes After: Prefixes: %v\n", process.Prefixes)
2322
fmt.Println()
2423
}
2524

26-
func TestRemovePrefix() {
27-
fmt.Println("=== Test RemovePrefix ===")
28-
process := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
29-
30-
testCases := []string{"lua_closethread", "luaL_checknumber"}
31-
32-
for _, input := range testCases {
33-
result := process.TrimPrefixes(input)
34-
fmt.Printf("Before: %s After: %s\n", input, result)
35-
}
36-
fmt.Println()
37-
}
38-
39-
func TestToGoName() {
40-
fmt.Println("=== Test ToGoName ===")
41-
process1 := parse.NewSymbolProcessor([]string{"lua_", "luaL_"})
42-
process2 := parse.NewSymbolProcessor([]string{"sqlite3_", "sqlite3_"})
43-
process3 := parse.NewSymbolProcessor([]string{"INI"})
44-
45-
testCases := []struct {
46-
processor *parse.SymbolProcessor
47-
input string
48-
}{
49-
{process1, "lua_closethread"},
50-
{process1, "luaL_checknumber"},
51-
{process2, "sqlite3_close_v2"},
52-
{process2, "sqlite3_callback"},
53-
{process3, "GetReal"},
54-
{process3, "GetBoolean"},
55-
{process3, "INIReader"},
56-
}
57-
58-
for _, tc := range testCases {
59-
result := tc.processor.ToGoName(tc.input)
60-
fmt.Printf("Before: %s After: %s\n", tc.input, result)
61-
}
62-
fmt.Println()
63-
}
64-
6525
func TestGenMethodName() {
6626
fmt.Println("=== Test GenMethodName ===")
6727
process := &parse.SymbolProcessor{}
@@ -77,25 +37,25 @@ func TestGenMethodName() {
7737
}
7838
for _, tc := range testCases {
7939
input := fmt.Sprintf("Class: %s, Name: %s", tc.class, tc.name)
80-
result := process.GenMethodName(tc.class, tc.name, tc.isDestructor)
40+
result := process.GenMethodName(tc.class, tc.name, tc.isDestructor, true)
8141
fmt.Printf("Before: %s After: %s\n", input, result)
8242
}
8343
fmt.Println()
8444
}
8545

8646
func TestAddSuffix() {
8747
fmt.Println("=== Test AddSuffix ===")
88-
process := parse.NewSymbolProcessor([]string{"INI"})
48+
process := parse.NewSymbolProcessor([]string{}, []string{"INI"})
8949
methods := []string{
9050
"INIReader",
9151
"INIReader",
9252
"ParseError",
9353
"HasValue",
9454
}
9555
for _, method := range methods {
96-
goName := process.ToGoName(method)
97-
className := process.ToGoName("INIReader")
98-
methodName := process.GenMethodName(className, goName, false)
56+
goName := names.GoName(method, process.Prefixes, true)
57+
className := names.GoName("INIReader", process.Prefixes, true)
58+
methodName := process.GenMethodName(className, goName, false, true)
9959
finalName := process.AddSuffix(methodName)
10060
input := fmt.Sprintf("Class: INIReader, Method: %s", method)
10161
fmt.Printf("Before: %s After: %s\n", input, finalName)
@@ -132,6 +92,7 @@ class INIReader {
13292
typedef struct lua_State lua_State;
13393
int(lua_rawequal)(lua_State *L, int idx1, int idx2);
13494
int(lua_compare)(lua_State *L, int idx1, int idx2, int op);
95+
int(lua_sizecomp)(size_t s, int idx1, int idx2, int op);
13596
`,
13697
isCpp: false,
13798
prefixes: []string{"lua_"},

_xtool/llcppsymg/_cmptest/symg_test/llgo.expect

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@
2525
[{
2626
"mangle": "lua_error",
2727
"c++": "lua_error(lua_State *)",
28-
"go": "Error"
28+
"go": "(*State).Error"
2929
}, {
3030
"mangle": "lua_next",
3131
"c++": "lua_next(lua_State *, int)",
32-
"go": "Next"
32+
"go": "(*State).Next"
3333
}, {
3434
"mangle": "lua_concat",
3535
"c++": "lua_concat(lua_State *, int)",
36-
"go": "Concat"
36+
"go": "(*State).Concat"
3737
}, {
3838
"mangle": "lua_stringtonumber",
3939
"c++": "lua_stringtonumber(lua_State *, const char *)",
40-
"go": "Stringtonumber"
40+
"go": "(*State).Stringtonumber"
4141
}]
4242

4343
#stderr

_xtool/llcppsymg/names/names.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package names
2+
3+
import (
4+
"strings"
5+
)
6+
7+
func GoName(name string, trimPrefixes []string, inCurPkg bool) string {
8+
if inCurPkg {
9+
name = removePrefixedName(name, trimPrefixes)
10+
}
11+
return pubName(name)
12+
}
13+
14+
func removePrefixedName(name string, trimPrefixes []string) string {
15+
if len(trimPrefixes) == 0 {
16+
return name
17+
}
18+
for _, prefix := range trimPrefixes {
19+
if strings.HasPrefix(name, prefix) {
20+
return strings.TrimPrefix(name, prefix)
21+
}
22+
}
23+
return name
24+
}
25+
26+
func pubName(name string) string {
27+
if len(name) == 0 {
28+
return name
29+
}
30+
toCamelCase := func(s string) string {
31+
parts := strings.Split(s, "_")
32+
for i := 0; i < len(parts); i++ {
33+
if len(parts[i]) > 0 {
34+
parts[i] = strings.ToUpper(parts[i][:1]) + parts[i][1:]
35+
}
36+
}
37+
return strings.Join(parts, "")
38+
}
39+
if name[0] == '_' {
40+
i := 0
41+
for i < len(name) && name[i] == '_' {
42+
i++
43+
}
44+
prefix := name[:i]
45+
return "X" + prefix + toCamelCase(name[i:])
46+
}
47+
return toCamelCase(name)
48+
}

0 commit comments

Comments
 (0)