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
2 changes: 1 addition & 1 deletion cl/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func loadOutsideMethod(ctx *pkgCtx, outsideDecl clang.Cursor) {
if m, ok := ctx.fns[fnUSR]; ok {
m.decl = outsideDecl
} else {
log.Panicln("method undeclared -", funcDisplayName(outsideDecl))
ctx.panicf(outsideDecl, "method undeclared - %s", funcDisplayName(outsideDecl))
}
}

Expand Down
24 changes: 14 additions & 10 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ import (

const (
DbgFlagCompileDecl = 1 << iota
DbgFlagImport
DbgFlagAll = DbgFlagCompileDecl | DbgFlagImport
DbgFlagMajorProc
DbgFlagAll = DbgFlagCompileDecl | DbgFlagMajorProc
)

var (
debugCompileDecl bool
debugImport bool
debugMajorProc bool
)

func SetDebug(flags int) {
debugCompileDecl = (flags & DbgFlagCompileDecl) != 0
debugImport = (flags & DbgFlagImport) != 0
debugMajorProc = (flags & DbgFlagMajorProc) != 0
}

// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -119,9 +119,9 @@ type Config struct {
// name will be used in function names (optional).
TypeAbbr map[string]string

// TypePrefix specifies the prefix to remove from C/C++ type names when generating Go
// type names (optional).
TypePrefix []string
// TypePrefix/TypeSuffix specifies the prefix/suffix to remove from C/C++ type names
// when generating Go type names (optional).
TypePrefix, TypeSuffix []string

// EnumPrefix specifies the prefix to remove from C/C++ enum value names when generating
// Go const names (optional).
Expand Down Expand Up @@ -213,8 +213,8 @@ func NewPackage(pkgPath, pkgName string, files []Source, conf *Config) (ret Pack
overloads: make(map[string]*overloads), pkg: pkg, cb: pkg.CB(),
llgo: llgo, fset: pkg.Fset, c: c, lang: conf.Language,
keepDoc: !conf.DontKeepDoc, stdRecvName: conf.UseStdRecvName,
cflags: conf.CFlags, wrapFileHeader: conf.WrapFileHeader,
typeAbbr: conf.TypeAbbr, typePrefix: conf.TypePrefix, fnPrefix: conf.FuncPrefix,
cflags: conf.CFlags, wrapFileHeader: conf.WrapFileHeader, typeAbbr: conf.TypeAbbr,
typePrefix: conf.TypePrefix, typeSuffix: conf.TypeSuffix, fnPrefix: conf.FuncPrefix,
enumPrefix: conf.EnumPrefix, macroPrefix: conf.MacroPrefix, varPrefix: conf.VarPrefix,
nsIgnores: conf.NSIgnore, typeIgnores: conf.TypeIgnore, rename: rename,
classes: conf.Class, nonClasses: conf.NonClass, typdecls: typdecls,
Expand All @@ -224,12 +224,16 @@ func NewPackage(pkgPath, pkgName string, files []Source, conf *Config) (ret Pack
lastSeen: make(map[string]none), impPkgs: make(map[string]none),
}
loadFiles(ctx, files, pkgPath, conf.GoFileOf)
ctx.compile()
// NOTE(xsw): should complete uninitialized typDecls before compiling
if debugMajorProc {
log.Println("==> complete uninitialized type declarations")
}
for _, typDecl := range typdecls {
if !typDecl.Inited() {
typDecl.InitType(pkg, types.NewStruct(nil, nil))
}
}
ctx.compile()
ret.Package = pkg
ret.Wrap = ctx.wrap
ret.Public = ctx.pubs
Expand Down
17 changes: 16 additions & 1 deletion cl/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type pkgCtx struct {
enumPrefix []string
macroPrefix []string
typePrefix []string
typeSuffix []string
classes []string // typedef names to be treated as classes
nonClasses []string // typedef names to be treated as non-classes
typeAbbr map[string]string // Go type name => abbreviated name, used in function names
Expand Down Expand Up @@ -308,7 +309,12 @@ func (p *pkgCtx) enumvalName(name, ns string) string {
}

func (p *pkgCtx) typeName(name string, _ bool) string {
return p.globalName(name, p.typePrefix)
if v, ok := p.rename[name]; ok {
return v // special case
}
name = rmPrefix(name, p.typePrefix)
name = rmSuffix(name, p.typeSuffix)
return p.cstyleToGo(name, true)
}

func (p *pkgCtx) funcName(name string, order int, typName, typCName string, global, _ bool) string {
Expand Down Expand Up @@ -446,6 +452,15 @@ func rmPrefix(name string, prefix []string) string {
return name
}

func rmSuffix(name string, suffix []string) string {
for _, sfx := range suffix {
if strings.HasSuffix(name, sfx) {
return name[:len(name)-len(sfx)]
}
}
return name
}

func contains(v string, names []string) bool {
for _, name := range names {
if name == v {
Expand Down
41 changes: 13 additions & 28 deletions cl/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package cl

import (
"go/token"
"go/types"
"log"
"strconv"
Expand Down Expand Up @@ -124,7 +123,7 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *funcObj, this *classCtx) {
}
} else {
typNamed := this.typNamed
recv = types.NewParam(token.NoPos, pkgTypes, "this", types.NewPointer(typNamed))
recv = types.NewParam(goNodePos(ctx, fn), pkgTypes, "this", types.NewPointer(typNamed))
typName = typNamed.Obj().Name()
}
fnName := ctx.funcName(origName, obj.order(), typName, typCName, this == nil, true)
Expand All @@ -147,7 +146,7 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *funcObj, this *classCtx) {
sig := types.NewSignatureType(recv, nil, nil, types.NewTuple(params...), results, variadic)
f, err := pkg.NewFuncWith(goNodePos(ctx, fn), fnName, sig, nil)
if err != nil {
log.Panicln("compileFunc:", origName, err)
panic(err)
}

if recv == nil {
Expand Down Expand Up @@ -188,8 +187,7 @@ func existMember(typ *types.Named, name string) bool {
}

const (
llgoSupportAliasAsRecv = true
c2goMethodRecvName = "self"
c2goMethodRecvName = "self"
)

func tryToMethod(ctx *pkgCtx, pkgTypes *types.Package, params []*types.Var) ([]*types.Var, *types.Var, *types.Named, string) {
Expand All @@ -200,8 +198,9 @@ func tryToMethod(ctx *pkgCtx, pkgTypes *types.Package, params []*types.Var) ([]*
// don't convert to method if the first two params have the same type
return params, nil, nil, ""
}
if tp, ok := t.(*types.Pointer); ok {
t = tp.Elem()
tPtr, isPtr := t.(*types.Pointer)
if isPtr {
t = tPtr.Elem()
}
switch t := t.(type) {
case *types.Named:
Expand All @@ -216,30 +215,16 @@ func tryToMethod(ctx *pkgCtx, pkgTypes *types.Package, params []*types.Var) ([]*
break
}
ta := types.Unalias(t)
if tp, ok := ta.(*types.Pointer); ok {
ta = tp.Elem()
if !isPtr {
if tp, ok := ta.(*types.Pointer); ok {
ta = tp.Elem()
}
}
if tn, ok := ta.(*types.Named); ok && tn.Obj().Pkg() == pkgTypes {
if llgoSupportAliasAsRecv {
if ctx.stdRecvName {
first = types.NewParam(first.Pos(), pkgTypes, c2goMethodRecvName, first.Type())
}
return params[1:], first, tn, t.Obj().Name()
} else {
var recvName string
if ctx.stdRecvName {
recvName = c2goMethodRecvName
} else {
recvName = first.Name()
}
first = types.NewParam(first.Pos(), pkgTypes, recvName, types.Unalias(t))
tnObjName := tn.Obj().Name()
// add type abbreviation for alias type if not exists
if _, ok := ctx.typeAbbr[tnObjName]; !ok {
ctx.typeAbbr[tnObjName] = t.Obj().Name()
}
return params[1:], first, tn, tnObjName
if ctx.stdRecvName {
first = types.NewParam(first.Pos(), pkgTypes, c2goMethodRecvName, first.Type())
}
return params[1:], first, tn, t.Obj().Name()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cl/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func genUnionAccessor(ctx *pkgCtx, recvPtr types.Type, m clang.Cursor) {

f, err := pkg.NewFuncWith(goNodePos(ctx, m), name, sig, nil)
if err != nil {
log.Panicln("genUnionAccessor:", member, err)
ctx.panicf(m, "union field %s: genUnionAccessor failed - %v", member, err)
}
cb := f.BodyStart(pkg)
// return (*T)(unsafe.Pointer(p))
Expand Down
4 changes: 4 additions & 0 deletions cmd/llcppg/llcppg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"

"github.com/goplus/gogen"
"github.com/goplus/llcppg/cl"
"github.com/goplus/llcppg/clang"
"github.com/goplus/llcppg/tool"
Expand All @@ -47,8 +48,11 @@ func main() {
if *debug {
cl.SetDebug(cl.DbgFlagAll)
tool.SetDebug(tool.DbgFlagAll)
gogen.SetDebug(gogen.DbgFlagAll)
} else if *verbose {
cl.SetDebug(cl.DbgFlagMajorProc)
tool.SetDebug(tool.DbgFlagSettings)
gogen.SetDebug(gogen.DbgFlagInstruction)
}

idx := clang.CreateIndex(1, 1)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/goplus/llcppg
go 1.27.0 // llgo 1.0

require (
github.com/goplus/gogen v1.25.2
github.com/goplus/gogen v1.25.3
github.com/goplus/lib v0.5.4
github.com/goplus/mod v0.22.1
github.com/llarhub/clang-c v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/goplus/gogen v1.25.2 h1:e7rpGecLv77cTVORoBGmjZS9YBDqqTP2/Et9FsFnAnk=
github.com/goplus/gogen v1.25.2/go.mod h1:Y7ulYW3wonQ3d9er00b0uGFEV/IUZa6okWJZh892ACQ=
github.com/goplus/gogen v1.25.3 h1:g8VWgNcCwTG9DFs08umVNtxl/BSLHH1cbvqWzyxrPVk=
github.com/goplus/gogen v1.25.3/go.mod h1:Y7ulYW3wonQ3d9er00b0uGFEV/IUZa6okWJZh892ACQ=
github.com/goplus/lib v0.5.4 h1:Ltrj+lY844/jx0nJqCRQEq3YzBkHb3jBjCQZWFUdlTc=
github.com/goplus/lib v0.5.4/go.mod h1:SgJv3oPqLLHCu0gcL46ejOP3x7/2ry2Jtxu7ta32kp0=
github.com/goplus/mod v0.22.1 h1:3GQZqKlcQAiMpRUDAHvFryGYie+mT0JMjguEqOtIjkE=
Expand Down
Loading
Loading