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
11 changes: 11 additions & 0 deletions cl/_testcpp/template_class/in.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
template <typename T>
class Foo {
private:
T value;

public:
Foo(T val) : value(val) {}
~Foo() {}

void g(T val) {}
};
5 changes: 5 additions & 0 deletions cl/_testcpp/template_class/out.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package foo

type Foo[T any] struct {
value T
}
Comment thread
xushiwei marked this conversation as resolved.
6 changes: 3 additions & 3 deletions cl/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,11 @@ func baseClass(ctx *pkgCtx, decl clang.Cursor) *types.TypeName {
}

func loadOutsideMethod(ctx *pkgCtx, outsideDecl clang.Cursor) {
manglingName := clang.Mangling(outsideDecl)
if m, ok := ctx.funcs[manglingName]; ok {
fnUSR := funcUSR(outsideDecl)
if m, ok := ctx.fns[fnUSR]; ok {
m.decl = outsideDecl
} else {
log.Panicln("method undeclared -", clang.DisplayName(outsideDecl))
log.Panicln("method undeclared -", funcDisplayName(outsideDecl))
}
}

Expand Down
2 changes: 1 addition & 1 deletion cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func NewPackage(pkgPath, pkgName string, files []Source, conf *Config) (ret Pack
nsIgnores: conf.NSIgnore, typeIgnores: conf.TypeIgnore, rename: rename,
classes: conf.Class, nonClasses: conf.NonClass, typdecls: typdecls,
pkgOf: conf.PackageOf, nameLookup: nameLookup, pubLookup: conf.PubFileLookup,
fileBases: make(map[clang.File]int), funcs: make(map[string]*funcObj),
fileBases: make(map[clang.File]int), fns: make(map[string]*funcObj),
macroVals: make(map[string]any), types: make(map[string]typeObj),
lastSeen: make(map[string]none), impPkgs: make(map[string]none),
}
Expand Down
28 changes: 17 additions & 11 deletions cl/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type pkgCtx struct {
fileBases map[clang.File]int // clang.File => base

macroVals map[string]any // macroName => value
funcs map[string]*funcObj // manglingName => func object
fns map[string]*funcObj // fnUSR => func object
types map[string]typeObj // c/c++ fullName => type name object (include external types)
typdecls map[string]typDecl // c/c++ fullName => type declaration object (only local types)
impPkgs map[string]none // imported package path set
Expand Down Expand Up @@ -469,8 +469,7 @@ type funcObj struct {
decl clang.Cursor // AST object
overloads *overloads

manglingName string // c/c++ mangling name
isOperator bool
isOperator bool
}

// order returns the order of the object in the overloads list.
Expand Down Expand Up @@ -549,23 +548,22 @@ func (p *scopeCtx) addTemplate(_ *pkgCtx, name string, decl clang.Cursor, isClas
}

func (p *scopeCtx) addFunc(ctx *pkgCtx, name string, decl clang.Cursor, isOp bool) (*funcObj, bool) {
fnUSR := funcUSR(decl)
Comment thread
xushiwei marked this conversation as resolved.
if debugCompileDecl {
log.Println("==> addFunc", name, clang.String(decl.Type()))
log.Println("==> addFunc", funcDisplayName(decl), "- USR:", fnUSR)
}

manglingName := clang.Mangling(decl)
if fn, ok := ctx.funcs[manglingName]; ok { // re-declared
if fn, ok := ctx.fns[fnUSR]; ok { // re-declared
if decl.IsFunctionInlined() != 0 {
fn.decl = decl // use the latest inline decl
}
return fn, false
}

obj := &funcObj{
name: name,
decl: decl,
manglingName: manglingName,
isOperator: isOp,
name: name,
decl: decl,
isOperator: isOp,
}
ovs, ok := p.overloads[name]
if ok {
Expand All @@ -575,7 +573,7 @@ func (p *scopeCtx) addFunc(ctx *pkgCtx, name string, decl clang.Cursor, isOp boo
p.overloads[name] = ovs
}
obj.overloads = ovs
ctx.funcs[manglingName] = obj
ctx.fns[fnUSR] = obj

return obj, true
}
Expand All @@ -586,6 +584,14 @@ func (p *scopeCtx) reorder() {
}
}

func funcUSR(decl clang.Cursor) string {
return clang.GoStringAndDispose(decl.USR())
}

func funcDisplayName(decl clang.Cursor) string {
return clang.DisplayName(decl)
}

/*
func substObj(pkg *types.Package, scope *types.Scope, origName string, real types.Object) {
old := scope.Insert(gogen.NewSubst(0, pkg, origName, real))
Expand Down
20 changes: 13 additions & 7 deletions cl/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,21 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *funcObj, this *classCtx) {
fn := obj.decl
origName := obj.name
isOp := obj.isOperator
deleted := fn.CXXMethodIsDeleted()
if debugCompileDecl {
fnType := clang.String(fn.Type())
log.Println("func", origName, "-", fnType, "- isOp:", isOp, "deleted:", deleted)
log.Println("func", origName, "-", fnType, "- isOp:", isOp)
}

if isOp {
return // TODO(xsw): support operator
}

manglingName := clang.Mangling(fn)
if manglingName == "" {
log.Println("func skipped: no mangling name for", origName)
return
}

pkg := ctx.pkg
pkgTypes := pkg.Types

Expand All @@ -91,7 +97,6 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *funcObj, this *classCtx) {
return
}

manglingName := clang.Mangling(fn)
if fn.IsFunctionInlined() != 0 {
if ctx.cflags == "" {
if debugCompileDecl {
Expand Down Expand Up @@ -270,19 +275,20 @@ func newParam(ctx *pkgCtx, pkg *types.Package, decl clang.Cursor, i c.Int, feats
}
typ := toTypeEx(ctx, pkg, declTyp, flagIsParam, feats, scope)
if declName != "" {
avoidKeyword(&declName)
declName = avoidKeyword(declName)
} else {
declName = "_llcppg_param" + strconv.Itoa(int(i)+1)
}
return types.NewParam(goNodePos(ctx, decl), pkg, declName, typ)
}

func avoidKeyword(name *string) {
switch *name {
func avoidKeyword(name string) string {
switch name {
case "map", "type", "range", "chan", "var", "func", "go", "select",
"defer", "package", "import", "interface", "fallthrough":
*name += "_"
return name + "_"
}
return name
}

// -----------------------------------------------------------------------------
6 changes: 3 additions & 3 deletions cl/vtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ func overriddenRoots(m clang.Cursor) []clang.Cursor {
}

func vtableMethodName(ctx *pkgCtx, m clang.Cursor) string {
manglingName := clang.Mangling(m)
if fn, ok := ctx.funcs[manglingName]; ok {
fnUSR := funcUSR(m)
if fn, ok := ctx.fns[fnUSR]; ok {
return ctx.funcName(fn.name, fn.order(), "", "", false, true)
}
panic("vtableMethodName: method not found - " + manglingName)
panic("vtableMethodName: method not found - " + funcDisplayName(m))
}

// placeholderSlotName returns the field name of an anonymous (reserved) slot at
Expand Down
3 changes: 3 additions & 0 deletions clang/mangling_withprefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func Mangling(fn Cursor) string {
m := fn.Mangling()
manglingName := c.GoString(m.CStr())
m.Dispose()
if manglingName == "" {
return ""
}
return manglingName[1:]
}

Expand Down
Loading