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/_testcpp/template_class/in.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class Bar {
Bar(T1 val1, T2 val2, int val3) : v1(val1), v2(val2), v3(val3) {}
virtual ~Bar() {}
virtual T2 g(T1 val1, T2 val2, int val3) {
return val2
return val2;
}
};
12 changes: 8 additions & 4 deletions cl/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ func newTemplateParams(ctx *pkgCtx, pkg *types.Package, cls clang.Cursor, feats

func loadTemplateClass(ctx *pkgCtx, cls clang.Cursor, ns string, parent *scopeCtx) {
origName := nameWithNS(clang.String(cls), ns)
if obj, ok := ctx.addTemplate(ctx, origName, cls, true); ok {
if obj, ok := ctx.addTemplateClass(ctx, origName, cls); ok {
ctx.addCompileUnit(func(ctx *pkgCtx) {
compileTemplateClass(ctx, obj, parent)
})
}
}

func compileTemplateClass(ctx *pkgCtx, obj *templateObj, parent *scopeCtx) {
func compileTemplateClass(ctx *pkgCtx, obj *templateClass, parent *scopeCtx) {
origName := obj.name
cls := obj.decl
if debugCompileDecl {
Expand All @@ -128,6 +128,9 @@ func compileTemplateClass(ctx *pkgCtx, obj *templateObj, parent *scopeCtx) {
var clsName string
var typDecl, ok = ctx.typdecls[origName]
if !ok {
if cls.IsCursorDefinition() == 0 || cls.NumTemplateArguments() > 0 {
return
}
clsName = ctx.typeName(origName, true)
typDecl = newType(ctx, cls, clsName, tagClass)
ctx.typdecls[origName] = typDecl
Expand Down Expand Up @@ -233,7 +236,8 @@ func isPublic(decl clang.Cursor) bool {

func loadClassMember(ctx *pkgCtx, pkg *types.Package, this *classCtx, clsName string, decl clang.Cursor) {
switch decl.Kind {
case lc.Cursor_CXXMethod, lc.Cursor_Constructor, lc.Cursor_Destructor:
case lc.Cursor_CXXMethod, lc.Cursor_FunctionTemplate,
lc.Cursor_Constructor, lc.Cursor_Destructor, lc.Cursor_ConversionFunction:
var name string
switch decl.Kind {
case lc.Cursor_Constructor:
Expand Down Expand Up @@ -351,7 +355,7 @@ func loadClassMember(ctx *pkgCtx, pkg *types.Package, this *classCtx, clsName st
}

case lc.Cursor_TemplateTypeParameter, lc.Cursor_NonTypeTemplateParameter,
lc.Cursor_TemplateTemplateParameter:
lc.Cursor_TemplateTemplateParameter, lc.Cursor_TypeRef:
// noop

case lc.Cursor_UnexposedAttr:
Expand Down
33 changes: 17 additions & 16 deletions cl/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ func (p *pkgCtx) panicf(decl clang.Cursor, format string, args ...any) {
log.Panicf("%s: %s", pos, fmt.Sprintf(format, args...))
}

func (p *pkgCtx) logtf(typ lc.Type, format string, args ...any) {
p.logf(typ.Declaration(), format, args...)
}

const (
anonPrefix = "_llcppg_anon_"
)
Expand Down Expand Up @@ -453,18 +457,16 @@ func contains(v string, names []string) bool {

// -----------------------------------------------------------------------------

type templateObj struct {
type templateClass struct {
name string // go name
decl clang.Cursor // AST object
overloads *overloads

isClass bool
}

// order returns the order of the template object in the overloads list.
// order returns the order of the template class in the overloads list.
// -1 means no order (only one overload, or not found).
func (p *templateObj) order() int {
items := p.overloads.tos
func (p *templateClass) order() int {
items := p.overloads.classes
if len(items) > 1 {
for i, obj := range items {
if obj == p {
Expand Down Expand Up @@ -498,8 +500,8 @@ func (p *funcObj) order() int {
}

type overloads struct {
fns []*funcObj
tos []*templateObj
fns []*funcObj
classes []*templateClass
}

func (p *overloads) reorder() {
Expand Down Expand Up @@ -540,21 +542,20 @@ func (p *scopeCtx) lookupType(name string) (types.Type, bool) {
return nil, false
}

func (p *scopeCtx) addTemplate(_ *pkgCtx, name string, decl clang.Cursor, isClass bool) (*templateObj, bool) {
func (p *scopeCtx) addTemplateClass(_ *pkgCtx, name string, decl clang.Cursor) (*templateClass, bool) {
if debugCompileDecl {
log.Println("==> addTemplate", name, "-", clang.DisplayName(decl))
log.Println("==> addTemplateClass", name, "-", clang.DisplayName(decl))
}
// TODO(xsw): check if the class is already added
obj := &templateObj{
name: name,
decl: decl,
isClass: isClass,
obj := &templateClass{
name: name,
decl: decl,
}
ovs, ok := p.overloads[name]
if ok {
ovs.tos = append(ovs.tos, obj)
ovs.classes = append(ovs.classes, obj)
} else {
ovs = &overloads{tos: []*templateObj{obj}}
ovs = &overloads{classes: []*templateClass{obj}}
p.overloads[name] = ovs
}
obj.overloads = ovs
Expand Down
15 changes: 10 additions & 5 deletions cl/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package cl
import (
"go/token"
"go/types"
"log"
"strconv"
"strings"

Expand Down Expand Up @@ -64,7 +63,11 @@ const (

func toType(ctx *pkgCtx, pkg *types.Package, typ lc.Type, flags int, scope *scopeCtx) types.Type {
var feats int
return toTypeEx(ctx, pkg, typ, flags, &feats, scope)
ret := toTypeEx(ctx, pkg, typ, flags, &feats, scope)
if feats&featIgnored != 0 {
panic("unsupported type - " + clang.String(typ))
}
return ret
}

func toTypeEx(ctx *pkgCtx, pkg *types.Package, typ lc.Type, flags int, feats *int, scope *scopeCtx) types.Type {
Expand Down Expand Up @@ -162,13 +165,15 @@ func toTypeEx(ctx *pkgCtx, pkg *types.Package, typ lc.Type, flags int, feats *in
return t
}
case lc.Type_BlockPointer:
log.Println("==> toType: C blocks is unsupported, use void* as work around")
ctx.logtf(typ, "C blocks (closures) are unsupported, ignored")
*feats |= featIgnored
return types.Typ[types.UnsafePointer]
default:
log.Println("==> toType: unknown Kind -", typ.Kind)
ctx.logtf(typ, "toType: unknown kind - %v", typ.Kind)
}
panic("todo: toType " + clang.String(typ))
ctx.logtf(typ, "unsupported type - %s", clang.String(typ))
*feats |= featIgnored
return types.Typ[types.Invalid]
Comment thread
xushiwei marked this conversation as resolved.
}

func toFuncType(ctx *pkgCtx, pkg *types.Package, fn lc.Type, feats *int) *types.Signature {
Expand Down
Loading
Loading