From f536b6b8b05c678a69006084d636701d0245f0bf Mon Sep 17 00:00:00 2001 From: xushiwei Date: Tue, 6 Oct 2026 19:16:02 +0800 Subject: [PATCH] cl compileFuncOrMethod: remove clsName; ctx.genAsMethod: self as recv --- cl/func.go | 10 ++-------- cl/logical.go | 8 ++++---- tool/_testc/python-3.14.8/include/pythread.h | 3 ++- tool/_testc/python-3.14.8/pythread.go | 12 ++++++++---- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/cl/func.go b/cl/func.go index f0761c5d..0137986e 100644 --- a/cl/func.go +++ b/cl/func.go @@ -128,7 +128,7 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *overloadObj, this *classCtx) { var recv *types.Var var typRecv *types.Named // if tryToMethod succeeded, this is the recv - var typName, typCName, clsName string + var typName, typCName string var nameInPkg string if this == nil { if ctx.lang == LanguageC { @@ -141,11 +141,6 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *overloadObj, this *classCtx) { recvCType = recvCType.Pointee() } typCName = trimTypeTag(clang.String(recvCType.Unqualified())) - if cls != "" { - clsName = cls - } else { - clsName = typName - } // When the function resolves to a logical class distinct from // the physical receiver type (for example PyList_GetItem // resolves to List while its receiver is the base class @@ -165,10 +160,9 @@ func compileFuncOrMethod(ctx *pkgCtx, obj *overloadObj, this *classCtx) { typNamed := this.typNamed recv = types.NewParam(goNodePos(ctx, fn), pkgTypes, "this", types.NewPointer(typNamed)) typName = typNamed.Obj().Name() - clsName = typName } - fnName := ctx.funcName(name, obj.order(), clsName, typCName, this == nil, true) + fnName := ctx.funcName(name, obj.order(), typName, typCName, this == nil, true) if recv == nil { nameInPkg = fnName diff --git a/cl/logical.go b/cl/logical.go index 74764aea..b5e36712 100644 --- a/cl/logical.go +++ b/cl/logical.go @@ -105,7 +105,7 @@ func (p *pkgCtx) newLogicalType(decl clang.Cursor, goName string, base *types.Na // genAsMethod emits the conversion method on the base class, for example // -// func (o *Object) AsList() *List { return (*List)(unsafe.Pointer(o)) } +// func (self *Object) AsList() *List { return (*List)(unsafe.Pointer(self)) } // // The conversion only reinterprets the pointer; it does not call into C and does // not check the object's real type. If a method of the same name already exists @@ -121,7 +121,7 @@ func (p *pkgCtx) genAsMethod(decl clang.Cursor, lc *logicalClass) { pkgTypes := pkg.Types recvType := types.NewPointer(lc.base) retType := types.NewPointer(lc.named) - recv := types.NewParam(goNodePos(p, decl), pkgTypes, "o", recvType) + recv := types.NewParam(goNodePos(p, decl), pkgTypes, c2goMethodRecvName, recvType) results := types.NewTuple(types.NewParam(token.NoPos, pkgTypes, "", retType)) sig := types.NewSignatureType(recv, nil, nil, nil, results, false) @@ -130,9 +130,9 @@ func (p *pkgCtx) genAsMethod(decl clang.Cursor, lc *logicalClass) { p.panicf(decl, "logical class %s: genAsMethod failed - %v", lc.named.Obj().Name(), err) } cb := f.BodyStart(pkg) - // return (*List)(unsafe.Pointer(o)) + // return (*List)(unsafe.Pointer(self)) cb.Typ(retType). - Typ(p.unsafePointer()).VarVal("o"). + Typ(p.unsafePointer()).Val(recv). Call(1). Call(1). Return(1).End() diff --git a/tool/_testc/python-3.14.8/include/pythread.h b/tool/_testc/python-3.14.8/include/pythread.h index 6faedb68..79b53e02 100644 --- a/tool/_testc/python-3.14.8/include/pythread.h +++ b/tool/_testc/python-3.14.8/include/pythread.h @@ -17,7 +17,7 @@ typedef enum Py_Lock_Status { typedef struct _Py_tss_t Py_tss_t; /* opaque */ -struct PyObject {}; +typedef struct PyObject {} PyObject; // Test if an object is the True singleton, the same as "x is True" in Python. PyAPI_FUNC(int) Py_IsTrue(PyObject *x); @@ -25,6 +25,7 @@ PyAPI_FUNC(int) Py_IsTrue(PyObject *x); PyAPI_FUNC(int) PyObject_IsTrue(PyObject *x); PyAPI_FUNC(PyObject *) PyList_GetItem(PyObject *x, PyObject *index); +PyAPI_FUNC(void) PyList_SetItem(PyObject *x, PyObject *index, PyObject *value); PyAPI_FUNC(PyObject *) PyDict_GetItem(PyObject *x, PyObject *index); diff --git a/tool/_testc/python-3.14.8/pythread.go b/tool/_testc/python-3.14.8/pythread.go index 86493b17..4ce2977e 100644 --- a/tool/_testc/python-3.14.8/pythread.go +++ b/tool/_testc/python-3.14.8/pythread.go @@ -45,8 +45,8 @@ type List struct { Object } -func (o *Object) AsList() *List { - return (*List)(unsafe.Pointer(o)) +func (self *Object) AsList() *List { + return (*List)(unsafe.Pointer(self)) } // llgo:link (*List).Item C.PyList_GetItem @@ -54,12 +54,16 @@ func (self *List) Item(index *Object) *Object { return nil } +// llgo:link (*List).SetItem C.PyList_SetItem +func (self *List) SetItem(index *Object, value *Object) { +} + type Dict struct { Object } -func (o *Object) AsDict() *Dict { - return (*Dict)(unsafe.Pointer(o)) +func (self *Object) AsDict() *Dict { + return (*Dict)(unsafe.Pointer(self)) } // llgo:link (*Dict).Item C.PyDict_GetItem