Skip to content

Struct/class with callback-pointer fields is missing // llgo:type C directive #952

Description

@xushiwei

Description

When llcppg converts a C/C++ class or struct that has member variables acting as callback (function) pointers, the generated Go type should carry the // llgo:type C directive, the same way it is already applied to converted typedefs of function pointer types. Currently the directive is omitted, so the callbacks inside such types are not treated with the C calling convention.

Affected Case

TestPython in the tool package. The converted MemAllocatorEx contains multiple callback fields but has no // llgo:type C directive:

type MemAllocatorEx struct {
	Ctx    c.Pointer
	Malloc func(c.Pointer, uintptr) c.Pointer
	Calloc func(c.Pointer, uintptr, uintptr) c.Pointer
	Realloc func(c.Pointer, c.Pointer, uintptr) c.Pointer
	Free   func(c.Pointer, c.Pointer)
}

The expected output is:

// llgo:type C
type MemAllocatorEx struct {
	// ...
}

The expected output for TestPython is therefore currently wrong and needs to be corrected.

Expected Behavior

If a class/struct has any member variable that is a callback (function pointer), llcppg should add // llgo:type C to the generated type. The logic should be consistent with how function-pointer typedefs are handled.

Proposed Fix

  1. In the type conversion path for records (struct/class), detect whether any field is a function pointer type.
  2. If so, emit // llgo:type C on the generated type, reusing the existing typedef logic.
  3. Update the TestPython expected output (and any other affected test cases) accordingly.

Note: the struct body above is illustrative, so please replace it with the actual generated output from the TestPython expected file before filing.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions