Skip to content

Commit

Permalink
Merge pull request #4 from luoliwoshang/gogensig/incomplete
Browse files Browse the repository at this point in the history
gogensig:implict forward decl
  • Loading branch information
xushiwei authored Nov 28, 2024
2 parents b590856 + 97bc323 commit 77c6e56
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/gogensig/convert/_testdata/forwarddecl/conf/llcppg.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "forwarddecl",
"include": ["temp.h"],
"trimPrefixes": ["sqlite3_"],
"trimPrefixes": ["sqlite3_","lua_"],
"cplusplus":false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"mangle": "lua_getstack",
"c++": "lua_getstack",
"go": "Getstack"
}
]

31 changes: 31 additions & 0 deletions cmd/gogensig/convert/_testdata/forwarddecl/gogensig.expect
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,38 @@ type PcacheMethods2 struct {
XShrink func(*Pcache)
}

type State struct {
Unused [8]uint8
}

type Debug struct {
Event c.Int
Name *int8
Namewhat *int8
What *int8
Source *int8
Currentline c.Int
Linedefined c.Int
Lastlinedefined c.Int
Nups int8
Nparams int8
Isvararg int8
Istailcall int8
Ftransfer uint16
Ntransfer uint16
ShortSrc [60]int8
ICi *CallInfo
}
//go:linkname Getstack C.lua_getstack
func Getstack(L *State, level c.Int, ar *Debug) c.Int

type CallInfo struct {
Unused [8]uint8
}

===== llcppg.pub =====
lua_Debug Debug
lua_State State
sqlite3_pcache Pcache
sqlite3_pcache_methods2 PcacheMethods2
sqlite3_pcache_page PcachePage
30 changes: 29 additions & 1 deletion cmd/gogensig/convert/_testdata/forwarddecl/hfile/temp.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,32 @@ struct sqlite3_pcache_methods2 {
void (*xTruncate)(sqlite3_pcache *, unsigned iLimit);
void (*xDestroy)(sqlite3_pcache *);
void (*xShrink)(sqlite3_pcache *);
};
};

#define LUA_IDSIZE 60

typedef struct lua_State lua_State;

typedef struct lua_Debug lua_Debug;

int(lua_getstack)(lua_State *L, int level, lua_Debug *ar);

struct lua_Debug {
int event;
const char *name;
const char *namewhat;
const char *what;
const char *source;
int currentline;
int linedefined;
int lastlinedefined;
unsigned char nups;
unsigned char nparams;
char isvararg;
char istailcall;
unsigned short ftransfer;
unsigned short ntransfer;
char short_src[LUA_IDSIZE];
/* private part */
struct CallInfo *i_ci; /* active function */
};
15 changes: 5 additions & 10 deletions cmd/gogensig/convert/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,13 @@ func (p *Package) NewTypeDecl(typeDecl *ast.TypeDecl) error {

// handleTypeDecl creates a new type declaration or retrieves existing one
func (p *Package) handleTypeDecl(name string, typeDecl *ast.TypeDecl, changed bool) *gogen.TypeDecl {
var decl *gogen.TypeDecl
if !p.cvt.inComplete(typeDecl.Type) {
if existDecl, exists := p.incomplete[name]; exists {
decl = existDecl
} else {
decl = p.emptyTypeDecl(name, typeDecl.Doc)
}
} else {
decl = p.emptyTypeDecl(name, typeDecl.Doc)
if existDecl, exists := p.incomplete[name]; exists {
return existDecl
}
decl := p.emptyTypeDecl(name, typeDecl.Doc)
if p.cvt.inComplete(typeDecl.Type) {
p.incomplete[name] = decl
}

if changed {
substObj(p.p.Types, p.p.Types.Scope(), typeDecl.Name.Name, decl.Type().Obj())
}
Expand Down
22 changes: 18 additions & 4 deletions cmd/gogensig/convert/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1447,9 +1447,24 @@ const (
}

func TestIdentRefer(t *testing.T) {
t.Run("undef ident ref", func(t *testing.T) {
pkg := createTestPkg(t, &convert.PackageConfig{})
pkg := createTestPkg(t, &convert.PackageConfig{})
pkg.SetCurFile("/path/to/stdio.h", "stdio.h", true, false, true)
pkg.NewTypedefDecl(&ast.TypedefDecl{
DeclBase: ast.DeclBase{
Loc: &ast.Location{File: "/path/to/stdio.h"},
},
Name: &ast.Ident{Name: "undefType"},
Type: &ast.BuiltinType{
Kind: ast.Char,
Flags: ast.Signed,
},
})
pkg.SetCurFile("/path/to/notsys.h", "notsys.h", true, true, false)
t.Run("undef sys ident ref", func(t *testing.T) {
err := pkg.NewTypeDecl(&ast.TypeDecl{
DeclBase: ast.DeclBase{
Loc: &ast.Location{File: "/path/to/notsys.h"},
},
Name: &ast.Ident{Name: "Foo"},
Type: &ast.RecordType{
Tag: ast.Struct,
Expand All @@ -1468,9 +1483,8 @@ func TestIdentRefer(t *testing.T) {
compareError(t, err, "undefType not found")
})
t.Run("undef tag ident ref", func(t *testing.T) {
pkg := createTestPkg(t, &convert.PackageConfig{})
err := pkg.NewTypeDecl(&ast.TypeDecl{
Name: &ast.Ident{Name: "Foo"},
Name: &ast.Ident{Name: "Bar"},
Type: &ast.RecordType{
Tag: ast.Struct,
Fields: &ast.FieldList{
Expand Down
6 changes: 5 additions & 1 deletion cmd/gogensig/convert/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,17 @@ func (p *TypeConv) handleIdentRefer(t ast.Expr) (types.Type, error) {
if err != nil {
return nil, err
}
// system type
if obj != nil {
return obj.Type(), nil
}

obj = gogen.Lookup(p.Types.Scope(), name)
if obj == nil {
return nil, fmt.Errorf("%s not found", name)
// implicit forward decl
decl := p.conf.Package.emptyTypeDecl(name, nil)
p.conf.Package.incomplete[name] = decl
return decl.Type(), nil
}
return obj.Type(), nil
}
Expand Down

0 comments on commit 77c6e56

Please sign in to comment.