Skip to content
Open
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
61 changes: 58 additions & 3 deletions doc/en/dev/llcppg.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func (recv_ *Sqlite3) Exec(sql *c.Char, callback func(c.Pointer, c.Int, **c.Char
}
```

For struct fields that are anonymous function pointers, the field type is replaced with a `c.Pointer` for description.
To work around LLGo's inability to use anonymous function types directly in struct fields, llcppg automatically generates a corresponding named function type. This generated type is intentionally made unexported (private) to mirror C/C++ semantics, where anonymous function types are not externally accessible. Crucially, even though the type itself is private, this does not affect the ability to assign a compatible function to the field during normal use in Go. All such function types follow the specific naming convention:

```
llgo_<namespaces>_<typename>_<nested_field_typename>_<fieldname>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the design llgo_<namespaces>_<typename>_<nested_field_typename>_<fieldname> improves code readability to some extent, it compromises the original data structure. This is because the original code features a function pointer within a nested struct.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in llgo, when you refer a func type which is comment with llgo:type C,it is actual a pointer in data structure

```

```c
typedef struct Hooks {
Expand All @@ -103,9 +107,60 @@ typedef struct Hooks {
} Hooks;
```
```go
// llgo:type C
type llgo_Hooks_MallocFn func(c.SizeT) c.Pointer
// llgo:type C
type llgo_Hooks_FreeFn func(c.Pointer)

type Hooks struct {
MallocFn c.Pointer
FreeFn c.Pointer
MallocFn llgo_Hooks_MallocFn
FreeFn llgo_Hooks_FreeFn
}
```

with namespace

```c
namespace A {
struct Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} Hooks;
}
```
```go
// llgo:type C
type llgo_A_Hooks_MallocFn func(c.SizeT) c.Pointer
// llgo:type C
type llgo_A_Hooks_FreeFn func(c.Pointer)

type Hooks struct {
MallocFn llgo_A_Hooks_MallocFn
FreeFn llgo_A_Hooks_FreeFn
}
```
in nested struct

```c
struct Foo {
struct {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} Hooks;
};
```
```go
// llgo:type C
type llgo_Foo_Hooks_MallocFn func(c.SizeT) c.Pointer

// llgo:type C
type llgo_Foo_Hooks_FreeFn func(c.Pointer)

type Foo struct {
Hooks struct {
MallocFn llgo_Foo_Hooks_MallocFn
FreeFn llgo_Foo_Hooks_FreeFn
}
}
```

Expand Down
Loading