diff --git a/cl/_mod/go.mod b/cl/_mod/go.mod new file mode 100644 index 00000000..87fb2ade --- /dev/null +++ b/cl/_mod/go.mod @@ -0,0 +1,5 @@ +module foo + +go 1.20 + +require github.com/goplus/lib v0.5.2 diff --git a/cl/_mod/go.sum b/cl/_mod/go.sum new file mode 100644 index 00000000..8fad59c3 --- /dev/null +++ b/cl/_mod/go.sum @@ -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= diff --git a/cl/_mod/stub.go b/cl/_mod/stub.go new file mode 100644 index 00000000..1b5378c5 --- /dev/null +++ b/cl/_mod/stub.go @@ -0,0 +1,3 @@ +package foo + +import _ "github.com/goplus/lib/c" diff --git a/cl/blockctx.go b/cl/blockctx.go index 147e7197..7b1e5919 100644 --- a/cl/blockctx.go +++ b/cl/blockctx.go @@ -29,6 +29,7 @@ type blockCtx struct { pkg *gogen.Package cb *gogen.CodeBuilder fset *token.FileSet + c gogen.PkgRef } /* diff --git a/cl/compile.go b/cl/compile.go index f0baef1c..d13a4050 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -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") 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 { @@ -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 { diff --git a/cl/compile_test.go b/cl/compile_test.go index 9ec44369..cb9b1ab1 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -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" @@ -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 { @@ -71,7 +74,7 @@ func testFromDir(t *testing.T, sel, relDir string) { } func _TestMockC(t *testing.T) { - testFromDir(t, "", "./_testmockc") + testFromDir(t, "", "./_testmockc", "c") } // ----------------------------------------------------------------------------- diff --git a/cl/type_and_var.go b/cl/type_and_var.go index 66aa249b..89f03f6d 100644 --- a/cl/type_and_var.go +++ b/cl/type_and_var.go @@ -22,6 +22,7 @@ import ( "log" "github.com/goplus/gogen" + "github.com/goplus/llcppg/clang" lc "github.com/goplus/llcppg/lib/clang" ) @@ -35,8 +36,41 @@ const ( flagRetType ) +var ( + tyVoid = types.Typ[types.UntypedNil] +) + +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() + case lc.TypePointer: + pointee := toType(ctx, typ.PointeeType(), flags) + return newPointer(pointee) + } + panic("todo: toType " + clang.String(typ)) } // -----------------------------------------------------------------------------