Skip to content
Open
Changes from 3 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
60 changes: 57 additions & 3 deletions doc/en/dev/llcppg.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ 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.
LLGo cannot use anonymous function types directly as field types. To preserve type information, llcppg automatically generates named function types and references them in the field and following this naming convention:
```
LLGO_<namespaces>_<typename>_<nested_field_typename>_<fieldname>
```
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Based on our discussion in the meeting: In C, an anonymous function's type is not accessible from the outside. To follow this same rule in Go, we'll make the auto-generated type unexported, which still allows the field to be assigned correctly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done in 545cb07


```c
typedef struct Hooks {
Expand All @@ -103,9 +106,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 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 c.Pointer
FreeFn c.Pointer
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