Skip to content

Commit 77c6e56

Browse files
authored
Merge pull request #4 from luoliwoshang/gogensig/incomplete
gogensig:implict forward decl
2 parents b590856 + 97bc323 commit 77c6e56

File tree

7 files changed

+97
-17
lines changed

7 files changed

+97
-17
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "forwarddecl",
33
"include": ["temp.h"],
4-
"trimPrefixes": ["sqlite3_"],
4+
"trimPrefixes": ["sqlite3_","lua_"],
55
"cplusplus":false
66
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"mangle": "lua_getstack",
4+
"c++": "lua_getstack",
5+
"go": "Getstack"
6+
}
7+
]
8+

cmd/gogensig/convert/_testdata/forwarddecl/gogensig.expect

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,38 @@ type PcacheMethods2 struct {
3131
XShrink func(*Pcache)
3232
}
3333

34+
type State struct {
35+
Unused [8]uint8
36+
}
37+
38+
type Debug struct {
39+
Event c.Int
40+
Name *int8
41+
Namewhat *int8
42+
What *int8
43+
Source *int8
44+
Currentline c.Int
45+
Linedefined c.Int
46+
Lastlinedefined c.Int
47+
Nups int8
48+
Nparams int8
49+
Isvararg int8
50+
Istailcall int8
51+
Ftransfer uint16
52+
Ntransfer uint16
53+
ShortSrc [60]int8
54+
ICi *CallInfo
55+
}
56+
//go:linkname Getstack C.lua_getstack
57+
func Getstack(L *State, level c.Int, ar *Debug) c.Int
58+
59+
type CallInfo struct {
60+
Unused [8]uint8
61+
}
62+
3463
===== llcppg.pub =====
64+
lua_Debug Debug
65+
lua_State State
3566
sqlite3_pcache Pcache
3667
sqlite3_pcache_methods2 PcacheMethods2
3768
sqlite3_pcache_page PcachePage

cmd/gogensig/convert/_testdata/forwarddecl/hfile/temp.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,32 @@ struct sqlite3_pcache_methods2 {
2121
void (*xTruncate)(sqlite3_pcache *, unsigned iLimit);
2222
void (*xDestroy)(sqlite3_pcache *);
2323
void (*xShrink)(sqlite3_pcache *);
24-
};
24+
};
25+
26+
#define LUA_IDSIZE 60
27+
28+
typedef struct lua_State lua_State;
29+
30+
typedef struct lua_Debug lua_Debug;
31+
32+
int(lua_getstack)(lua_State *L, int level, lua_Debug *ar);
33+
34+
struct lua_Debug {
35+
int event;
36+
const char *name;
37+
const char *namewhat;
38+
const char *what;
39+
const char *source;
40+
int currentline;
41+
int linedefined;
42+
int lastlinedefined;
43+
unsigned char nups;
44+
unsigned char nparams;
45+
char isvararg;
46+
char istailcall;
47+
unsigned short ftransfer;
48+
unsigned short ntransfer;
49+
char short_src[LUA_IDSIZE];
50+
/* private part */
51+
struct CallInfo *i_ci; /* active function */
52+
};

cmd/gogensig/convert/package.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,18 +216,13 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {
216216

217217
// handleTypeDecl creates a new type declaration or retrieves existing one
218218
func (p *Package) handleTypeDecl(name string, typeDecl *ast.TypeDecl, changed bool) *gogen.TypeDecl {
219-
var decl *gogen.TypeDecl
220-
if !p.cvt.inComplete(typeDecl.Type) {
221-
if existDecl, exists := p.incomplete[name]; exists {
222-
decl = existDecl
223-
} else {
224-
decl = p.emptyTypeDecl(name, typeDecl.Doc)
225-
}
226-
} else {
227-
decl = p.emptyTypeDecl(name, typeDecl.Doc)
219+
if existDecl, exists := p.incomplete[name]; exists {
220+
return existDecl
221+
}
222+
decl := p.emptyTypeDecl(name, typeDecl.Doc)
223+
if p.cvt.inComplete(typeDecl.Type) {
228224
p.incomplete[name] = decl
229225
}
230-
231226
if changed {
232227
substObj(p.p.Types, p.p.Types.Scope(), typeDecl.Name.Name, decl.Type().Obj())
233228
}

cmd/gogensig/convert/package_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,9 +1447,24 @@ const (
14471447
}
14481448

14491449
func TestIdentRefer(t *testing.T) {
1450-
t.Run("undef ident ref", func(t *testing.T) {
1451-
pkg := createTestPkg(t, &convert.PackageConfig{})
1450+
pkg := createTestPkg(t, &convert.PackageConfig{})
1451+
pkg.SetCurFile("/path/to/stdio.h", "stdio.h", true, false, true)
1452+
pkg.NewTypedefDecl(&ast.TypedefDecl{
1453+
DeclBase: ast.DeclBase{
1454+
Loc: &ast.Location{File: "/path/to/stdio.h"},
1455+
},
1456+
Name: &ast.Ident{Name: "undefType"},
1457+
Type: &ast.BuiltinType{
1458+
Kind: ast.Char,
1459+
Flags: ast.Signed,
1460+
},
1461+
})
1462+
pkg.SetCurFile("/path/to/notsys.h", "notsys.h", true, true, false)
1463+
t.Run("undef sys ident ref", func(t *testing.T) {
14521464
err := pkg.NewTypeDecl(&ast.TypeDecl{
1465+
DeclBase: ast.DeclBase{
1466+
Loc: &ast.Location{File: "/path/to/notsys.h"},
1467+
},
14531468
Name: &ast.Ident{Name: "Foo"},
14541469
Type: &ast.RecordType{
14551470
Tag: ast.Struct,
@@ -1468,9 +1483,8 @@ func TestIdentRefer(t *testing.T) {
14681483
compareError(t, err, "undefType not found")
14691484
})
14701485
t.Run("undef tag ident ref", func(t *testing.T) {
1471-
pkg := createTestPkg(t, &convert.PackageConfig{})
14721486
err := pkg.NewTypeDecl(&ast.TypeDecl{
1473-
Name: &ast.Ident{Name: "Foo"},
1487+
Name: &ast.Ident{Name: "Bar"},
14741488
Type: &ast.RecordType{
14751489
Tag: ast.Struct,
14761490
Fields: &ast.FieldList{

cmd/gogensig/convert/type.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,17 @@ func (p *TypeConv) handleIdentRefer(t ast.Expr) (types.Type, error) {
130130
if err != nil {
131131
return nil, err
132132
}
133+
// system type
133134
if obj != nil {
134135
return obj.Type(), nil
135136
}
136137

137138
obj = gogen.Lookup(p.Types.Scope(), name)
138139
if obj == nil {
139-
return nil, fmt.Errorf("%s not found", name)
140+
// implicit forward decl
141+
decl := p.conf.Package.emptyTypeDecl(name, nil)
142+
p.conf.Package.incomplete[name] = decl
143+
return decl.Type(), nil
140144
}
141145
return obj.Type(), nil
142146
}

0 commit comments

Comments
 (0)