diff --git a/chore/llpyg/llpyg.go b/chore/llpyg/llpyg.go index c486dc17..0fb85473 100644 --- a/chore/llpyg/llpyg.go +++ b/chore/llpyg/llpyg.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "go/ast" + "go/token" "go/types" "log" "os" @@ -103,9 +104,13 @@ func (ctx *context) genFunc(pkg *gogen.Package, sym *symbol) { if c := name[0]; c >= 'a' && c <= 'z' { name = string(c+'A'-'a') + name[1:] } - fn := pkg.NewFunc(nil, name, params, ctx.ret, variadic) - fn.SetComments(pkg, ctx.genComment(sym.Doc)) - fn.BodyStart(pkg).End() + sig := types.NewSignatureType(nil, nil, nil, params, ctx.ret, variadic) + fn := pkg.NewFuncDecl(token.NoPos, name, sig) + list := ctx.genDoc(sym.Doc) + list = append(list, emptyCommentLine) + list = append(list, ctx.genLinkname(name, sym)) + fn.SetComments(pkg, &ast.CommentGroup{List: list}) + // fn.BodyStart(pkg).End() } func (ctx *context) genParams(pkg *gogen.Package, sig string) (*types.Tuple, bool, bool) { @@ -134,11 +139,19 @@ func (ctx *context) genParams(pkg *gogen.Package, sig string) (*types.Tuple, boo return types.NewTuple(list...), false, false } -func (ctx *context) genComment(doc string) *ast.CommentGroup { +func (ctx *context) genLinkname(name string, sym *symbol) *ast.Comment { + return &ast.Comment{Text: "//go:linkname " + name + " py." + sym.Name} +} + +func (ctx *context) genDoc(doc string) []*ast.Comment { lines := strings.Split(doc, "\n") - list := make([]*ast.Comment, len(lines)) + list := make([]*ast.Comment, len(lines), len(lines)+2) for i, line := range lines { list[i] = &ast.Comment{Text: "// " + line} } - return &ast.CommentGroup{List: list} + return list } + +var ( + emptyCommentLine = &ast.Comment{Text: "//"} +) diff --git a/py/module.go b/py/module.go index 7f7381bc..7166fd9d 100644 --- a/py/module.go +++ b/py/module.go @@ -25,6 +25,7 @@ import ( // https://docs.python.org/3/c-api/import.html // https://docs.python.org/3/c-api/module.html +/* // llgo:type C type ModuleDefBase struct { Unused [8]byte // TODO(xsw) @@ -35,6 +36,7 @@ type ModuleDef struct { Base ModuleDefBase // TODO(xsw) } +*/ // Return the module object corresponding to a module name. The name argument // may be of the form package.module. First check the modules dictionary if diff --git a/py/type.go b/py/type.go index cf6cc4e5..a78cb1c6 100644 --- a/py/type.go +++ b/py/type.go @@ -22,9 +22,8 @@ import ( // https://docs.python.org/3/c-api/type.html -type TypeObject struct { - Object -} +// TypeObject represents the Python type object. +type TypeObject = Object // Return the type’s name. Equivalent to getting the type’s __name__ attribute. // @@ -54,4 +53,4 @@ func (t *TypeObject) Flags() uint32 { return 0 } func (t *TypeObject) Module() *Object { return nil } // llgo:link (*TypeObject).ModuleByDef C.PyType_GetModuleByDef -func (t *TypeObject) ModuleByDef(def *ModuleDef) *Object { return nil } +// func (t *TypeObject) ModuleByDef(def *ModuleDef) *Object { return nil }