Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cl/_mod/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module foo

go 1.20

require github.com/goplus/lib v0.5.2
2 changes: 2 additions & 0 deletions cl/_mod/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/goplus/lib v0.5.2 h1:BUd3mUwTajDRBHVxMfS/y/hDJ6n/Pxwf6z7ikrOXvkE=
github.com/goplus/lib v0.5.2/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
3 changes: 3 additions & 0 deletions cl/_mod/stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package foo

import _ "github.com/goplus/lib/c"
1 change: 1 addition & 0 deletions cl/blockctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type blockCtx struct {
pkg *gogen.Package
cb *gogen.CodeBuilder
fset *token.FileSet
c gogen.PkgRef
}

/*
Expand Down
11 changes: 7 additions & 4 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ func NewPackage(pkgPath, pkgName string, file Source, conf *Config) (pkg Package
// -----------------------------------------------------------------------------

func loadFile(p *gogen.Package, conf *Config, file Source) (pi *PkgInfo, err error) {
c := p.Import("github.com/lib/c")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P0] Wrong import path breaks c package resolution

This imports github.com/lib/c, but the package is github.com/goplus/lib/c everywhere else — the file's own import at compile.go:27, the test stub cl/_mod/stub.go:3, and cl/_mod/go.mod (require github.com/goplus/lib v0.5.2).

This PkgRef is stored in blockCtx.c and used by toType to resolve Int/UInt, so the mismatched path prevents type resolution from working.

c := p.Import("github.com/goplus/lib/c")

ctx := &blockCtx{
pkg: p, cb: p.CB(), fset: p.Fset,
pkg: p, cb: p.CB(), fset: p.Fset, c: c,
}
_ = conf
clang.VisitChildren(file.TU.Cursor(), func(decl, parent clang.Cursor) clang.ChildVisitResult {
Expand Down Expand Up @@ -236,9 +237,11 @@ func compileFunc(ctx *blockCtx, fn clang.Cursor) {
params = append(params, newVariadicParam(ctx))
}
pkg := ctx.pkg
retType := fn.ResultType() // TODO(xsw): return void
tyRet := toType(ctx, retType, flagRetType)
results = types.NewTuple(pkg.NewParam(token.NoPos, "", tyRet, false))
retType := fn.ResultType()
if retType.Kind != lc.TypeVoid {
tyRet := toType(ctx, retType, flagRetType)
results = types.NewTuple(pkg.NewParam(token.NoPos, "", tyRet, false))
}
sig := types.NewSignatureType(nil, nil, nil, types.NewTuple(params...), results, variadic)
f := types.NewFunc(ctx.goNodePos(fn), pkg.Types, fnName, sig)
if old := pkg.Types.Scope().Insert(f); old != nil {
Expand Down
9 changes: 6 additions & 3 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/goplus/gogen"
"github.com/goplus/gogen/packages"
"github.com/goplus/llcppg/cl"
"github.com/goplus/llcppg/cl/cltest"
"github.com/goplus/llcppg/clang"
Expand Down Expand Up @@ -50,15 +51,17 @@ func testGenGo(t *testing.T, pkg *gogen.Package, dir string, exp any) {
testDiff(t, dir, "/result.txt", &b, exp)
}

func testFromDir(t *testing.T, sel, relDir string) {
func testFromDir(t *testing.T, sel, relDir, lang string) {
cltest.TestFromDir(t, sel, relDir, func(t *testing.T, pkgDir string) {
idx := clang.CreateIndex(0, 0)
defer idx.Dispose()

u := idx.ParseTranslationUnit(0, pkgDir+"/in.h", "-x", "c")
u := idx.ParseTranslationUnit(0, pkgDir+"/in.h", "-x", lang)
defer u.Dispose()

imp := packages.NewImporter(nil, "./_mod")
pkg, err := cl.NewPackage("", "foo", cl.Source{TU: u}, &cl.Config{
Importer: imp,
NameLookup: cltest.MockNameLookup,
})
if err != nil {
Expand All @@ -71,7 +74,7 @@ func testFromDir(t *testing.T, sel, relDir string) {
}

func _TestMockC(t *testing.T) {
testFromDir(t, "", "./_testmockc")
testFromDir(t, "", "./_testmockc", "c")
}

// -----------------------------------------------------------------------------
36 changes: 35 additions & 1 deletion cl/type_and_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"log"

"github.com/goplus/gogen"
"github.com/goplus/llcppg/clang"
lc "github.com/goplus/llcppg/lib/clang"
)

Expand All @@ -35,8 +36,41 @@ const (
flagRetType
)

var (
tyVoid = types.Typ[types.UntypedNil]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] tyVoid bound to UntypedNil is surprising

tyVoid is bound to types.Typ[types.UntypedNil] and compared in newPointer to emit unsafe.Pointer. This works, but the name suggests a distinct void type rather than the untyped-nil sentinel. A short comment explaining that untyped-nil is used as the void sentinel would prevent future confusion. Minor / WIP, non-blocking.

)

func newPointer(typ types.Type) types.Type {
switch t := typ.(type) {
case *types.Basic:
if t == tyVoid {
return types.Typ[types.UnsafePointer]
}
case *types.Signature:
panic("todo: newPointer for signature")
/* if gogen.IsCSignature(t) {
return types.NewSignature(nil, t.Params(), t.Results(), t.Variadic())
} */
case *types.Named:
panic("todo: newPointer for named type")
/* if typ == ValistTag {
return Valist
} */
}
return types.NewPointer(typ)
}

func toType(ctx *blockCtx, typ lc.Type, flags int) types.Type {
panic("todo: toType")
switch typ.Kind {
case lc.TypeInt:
return ctx.c.Ref("Int").Type()
case lc.TypeUInt:
return ctx.c.Ref("UInt").Type()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P0] Ref("UInt") does not exist; symbol is Uint

github.com/goplus/lib/c v0.5.2 defines the unsigned-int alias as Uint (Uint = uint32 in c/c.go:38), not UInt. ctx.c.Ref("UInt") will fail to resolve the object.

		return ctx.c.Ref("Uint").Type()

The adjacent Ref("Int") on line 66 is correct since Int exists.

case lc.TypePointer:
pointee := toType(ctx, typ.PointeeType(), flags)
return newPointer(pointee)
}
panic("todo: toType " + clang.String(typ))
}

// -----------------------------------------------------------------------------
Expand Down
Loading