Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #204 from xushiwei/gox
Browse files Browse the repository at this point in the history
gox v1.12.5
  • Loading branch information
xushiwei authored Nov 2, 2023
2 parents 5727382 + 99785aa commit 4d12585
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
Test:
strategy:
matrix:
go-version: [1.17.x]
go-version: [1.18.x]
os: [ubuntu-latest, macos-11]
runs-on: ${{ matrix.os }}
steps:
Expand Down
14 changes: 9 additions & 5 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@ func (p *nodeInterp) Caller(v goast.Node) string {
return "the function call"
}

func (p *nodeInterp) LoadExpr(v goast.Node) (code string, pos token.Position) {
func (p *nodeInterp) LoadExpr(v goast.Node) string {
src := v.(*node)
ctx := src.ctx
start := src.Pos()
n := int(src.End() - start)
pos = ctx.file.Position(start)
code = string(ctx.src[pos.Offset : pos.Offset+n])
return
pos := ctx.file.Position(start)
return string(ctx.src[pos.Offset : pos.Offset+n])
}

// -----------------------------------------------------------------------------
Expand All @@ -111,8 +110,13 @@ const (

// -----------------------------------------------------------------------------

type typeDecl struct {
*gox.TypeDecl
defineHere func()
}

type PkgInfo struct {
typdecls map[string]*gox.TypeDecl
typdecls map[string]typeDecl
extfns map[string]none // external functions which are used
}

Expand Down
12 changes: 5 additions & 7 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var (

func init() {
SetDebug(DbgFlagAll)
gox.SetDebug(gox.DbgFlagAll)
preprocessor.SetDebug(preprocessor.DbgFlagAll)

home, err := os.UserHomeDir()
Expand Down Expand Up @@ -93,7 +94,7 @@ func newTestEnv(code string) *testEnv {
pkg: p, cb: p.CB(), fset: p.Fset, src: src,
unnameds: make(map[ast.ID]unnamedType),
}
ctx.typdecls = make(map[string]*gox.TypeDecl)
ctx.typdecls = make(map[string]typeDecl)
ctx.initCTypes()
return &testEnv{doc: doc, pkg: p, ctx: ctx, json: json}
}
Expand Down Expand Up @@ -216,14 +217,11 @@ void test(int var) {
interp := &nodeInterp{fset: fset}
base := token.Pos(ctx.file.Base())
v := &node{ctx: ctx, pos: base, end: base}
src, pos := interp.LoadExpr(v)
if src != "" || pos.String() != "1:1" {
t.Fatal("interp.LoadExpr:", src, pos)
src := interp.LoadExpr(v)
if src != "" {
t.Fatal("interp.LoadExpr:", src)
}
interp.Caller(v)
if ret := interp.Position(v.Pos()); ret != pos {
t.Fatal("interp.Position:", ret, "expected:", pos)
}
}

func TestComplicatedFor(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func compileCompoundLiteralExpr(ctx *blockCtx, expr *ast.Node) {
case ast.InitListExpr:
var tyAnonym types.Type
if fld := inner.Field; fld != nil {
tyAnonym = toAnonymType(ctx, ctx.goNodePos(expr), fld)
tyAnonym = toAnonymType(ctx, ctx.goNode(expr), fld)
}
typ, _ := toTypeEx(ctx, ctx.cb.Scope(), tyAnonym, inner.Type, 0, false)
initLit(ctx, typ, inner)
Expand Down
4 changes: 2 additions & 2 deletions cl/multifiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (p *multiFileCtl) initMultiFileCtl(pkg *gox.Package, baseDir string, conf *
pi := reused.pkg.pi
if pi == nil {
pi = new(PkgInfo)
pi.typdecls = make(map[string]*gox.TypeDecl)
pi.typdecls = make(map[string]typeDecl)
pi.extfns = make(map[string]none)
reused.pkg.pi = pi
reused.pkg.Package = pkg
Expand All @@ -59,7 +59,7 @@ func (p *multiFileCtl) initMultiFileCtl(pkg *gox.Package, baseDir string, conf *
p.incs = reused.deps.incs
p.skipLibcH = reused.deps.skipLibcH
} else {
p.typdecls = make(map[string]*gox.TypeDecl)
p.typdecls = make(map[string]typeDecl)
p.extfns = make(map[string]none)
p.base = new(int)
}
Expand Down
20 changes: 14 additions & 6 deletions cl/pkginfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ const (
depsFile = "deps"
)

type undefStruct struct {
name string
typeDecl
}

func (p Package) InitDependencies() {
if p.pi == nil {
panic("Please set conf.NeedPkgInfo = true")
Expand All @@ -31,13 +36,15 @@ func (p Package) InitDependencies() {
return
}

var uds []string
var uds []undefStruct
for name, tdecl := range p.pi.typdecls {
if tdecl.State() == gox.TyStateUninited {
uds = append(uds, name)
uds = append(uds, undefStruct{name, tdecl})
}
}
sort.Strings(uds)
sort.Slice(uds, func(i, j int) bool {
return uds[i].name < uds[j].name
})
extfns := make([]string, 0, len(p.pi.extfns))
for name := range p.pi.extfns {
extfns = append(extfns, name)
Expand All @@ -51,7 +58,8 @@ func (p Package) InitDependencies() {
scope := pkg.Types.Scope()
empty := types.NewStruct(nil, nil)
for _, us := range uds {
p.pi.typdecls[us].InitType(pkg, empty)
us.defineHere()
us.InitType(pkg, empty)
}
vPanic := types.Universe.Lookup("panic")
for _, uf := range extfns {
Expand Down Expand Up @@ -103,12 +111,12 @@ func ensureSig(sig *types.Signature) *types.Signature {

func (p Package) WriteDepTo(dst io.Writer) error {
p.InitDependencies()
return gox.WriteTo(dst, p.Package, depsFile)
return p.Package.WriteTo(dst, depsFile)
}

func (p Package) WriteDepFile(file string) error {
p.InitDependencies()
return gox.WriteFile(file, p.Package, depsFile)
return p.Package.WriteFile(file, depsFile)
}

// -----------------------------------------------------------------------------
Expand Down
6 changes: 4 additions & 2 deletions cl/pkginfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ func TestPkgInfo_Basic(t *testing.T) {
pkg := testFunc(t, "Basic", `
void f();
void test(struct foo* in) {
void test(struct foo* in, struct bar* out) {
f();
}
`, `func test(in *struct_foo) {
`, `func test(in *struct_foo, out *struct_bar) {
f()
}`)
var out bytes.Buffer
pkg.WriteDepTo(&out)
deps := out.String()
if deps != `package main
type struct_bar struct {
}
type struct_foo struct {
}
Expand Down
40 changes: 24 additions & 16 deletions cl/type_and_var.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cl

import (
goast "go/ast"
"go/constant"
"go/token"
"go/types"
Expand Down Expand Up @@ -41,7 +42,7 @@ retry:
if pub {
name = gox.CPubName(name)
}
newStructOrUnionType(ctx, token.NoPos, name)
newStructOrUnionType(ctx, nil, name)
if pub {
substObj(ctx.pkg.Types, scope, e.Literal, scope.Lookup(name))
}
Expand All @@ -51,15 +52,15 @@ retry:
return
}

func toAnonymType(ctx *blockCtx, pos token.Pos, decl *ast.Node) (ret *types.Named) {
func toAnonymType(ctx *blockCtx, src goast.Node, decl *ast.Node) (ret *types.Named) {
scope := types.NewScope(ctx.cb.Scope(), token.NoPos, token.NoPos, "")
switch decl.Kind {
case ast.FieldDecl:
pkg := ctx.pkg
typ, _ := toTypeEx(ctx, scope, nil, decl.Type, 0, false)
fld := types.NewField(ctx.goNodePos(decl), pkg.Types, decl.Name, typ, false)
struc := types.NewStruct([]*types.Var{fld}, nil)
ret = pkg.NewType(ctx.getAnonyName(), pos).InitType(pkg, struc)
ret = pkg.NewTypeDefs().NewType(ctx.getAnonyName(), src).InitType(pkg, struc)
default:
log.Panicln("toAnonymType: unknown kind -", decl.Kind)
}
Expand Down Expand Up @@ -212,7 +213,7 @@ func compileTypedef(ctx *blockCtx, decl *ast.Node, global, pub bool) types.Type
} else {
log.Panicf("compileTypedef %v: unknown id = %v\n", name, owned.ID)
}
ctx.cb.AliasType(name, typ, ctx.goNodePos(decl))
ctx.cb.AliasType(name, typ, ctx.goNode(decl))
return typ
}
}
Expand All @@ -230,19 +231,22 @@ func compileTypedef(ctx *blockCtx, decl *ast.Node, global, pub bool) types.Type
}
}
if scope == ctx.pkg.Types.Scope() {
ctx.cb.AliasType(name, typ, ctx.goNodePos(decl))
ctx.cb.AliasType(name, typ, ctx.goNode(decl))
} else {
aliasType(scope, ctx.pkg.Types, name, typ)
}
return typ
}

func newStructOrUnionType(ctx *blockCtx, pos token.Pos, name string) (t *gox.TypeDecl) {
t, decled := ctx.typdecls[name]
if !decled {
t = ctx.cb.NewType(name, pos)
ctx.typdecls[name] = t
func newStructOrUnionType(ctx *blockCtx, src goast.Node, name string) (ret typeDecl) {
ret, decled := ctx.typdecls[name]
if decled {
return
}
decls, defineHere := ctx.cb.NewTypeDecls()
t := decls.NewType(name, src)
ret = typeDecl{t, defineHere}
ctx.typdecls[name] = ret
return
}

Expand All @@ -251,24 +255,28 @@ func compileStructOrUnion(ctx *blockCtx, name string, decl *ast.Node, pub bool)
log.Println(decl.TagUsed, name, "-", decl.Loc.PresumedLine)
}
var t *gox.TypeDecl
pos := ctx.goNodePos(decl)
src := ctx.goNode(decl)
pkg := ctx.pkg
if ctx.inSrcFile() && decl.Name != "" {
realName := ctx.autoStaticName(decl.Name)
var scope = ctx.cb.Scope()
t = ctx.cb.NewType(realName, pos)
t = ctx.cb.NewType(realName, src)
substObj(pkg.Types, scope, name, t.Type().Obj())
} else {
t = newStructOrUnionType(ctx, pos, name)
ret := newStructOrUnionType(ctx, src, name)
t = ret.TypeDecl
if decl.CompleteDefinition {
ret.defineHere()
}
}
if decl.CompleteDefinition {
var inner types.Type
var del delfunc
switch decl.TagUsed {
case "struct":
inner, del = toStructType(ctx, t.Type(), decl, pub)
default:
case "union":
inner, del = toUnionType(ctx, t.Type(), decl, pub)
default:
inner, del = toStructType(ctx, t.Type(), decl, pub)
}
ret := t.InitType(pkg, inner)
if pub {
Expand Down
36 changes: 18 additions & 18 deletions cl/type_and_var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,13 @@ void test() {
};
}
`, `func test() {
type _cgoa_1 struct {
v float64
}
type struct_foo struct {
a int32
_cgoa_1
}
type _cgoa_1 struct {
v float64
}
}`)
testFunc(t, "testAnonymousVar", `
void test() {
Expand All @@ -439,13 +439,13 @@ void test() {
};
}
`, `func test() {
type _cgoa_1 struct {
v float64
}
type struct_foo struct {
a int32
b _cgoa_1
}
type _cgoa_1 struct {
v float64
}
}`)
testFunc(t, "testNestStruct", `
void test() {
Expand All @@ -457,13 +457,13 @@ void test() {
};
}
`, `func test() {
type struct_bar struct {
v float64
}
type struct_foo struct {
a int32
b struct_bar
}
type struct_bar struct {
v float64
}
}`)
testFunc(t, "testUnion", `
void test() {
Expand All @@ -489,13 +489,13 @@ void test() {
};
}
`, `func test() {
type union_foo struct {
c struct_bar
}
type struct_bar struct {
x int32
y float64
}
type union_foo struct {
c struct_bar
}
}`)
testFunc(t, "testUnionAnonymous", `
void test() {
Expand All @@ -509,13 +509,13 @@ void test() {
};
}
`, `func test() {
type union_foo struct {
_cgoa_1
}
type _cgoa_1 struct {
x int32
y float64
}
type union_foo struct {
_cgoa_1
}
}`)
testFunc(t, "testUnionAnonymousVar", `
void test() {
Expand All @@ -529,13 +529,13 @@ void test() {
};
}
`, `func test() {
type union_foo struct {
c _cgoa_1
}
type _cgoa_1 struct {
x int32
y float64
}
type union_foo struct {
c _cgoa_1
}
}`)
testFunc(t, "testTypedef", `
void test() {
Expand Down
Loading

0 comments on commit 4d12585

Please sign in to comment.