From 0fbca186b186c3c7f5a554677005ce1207ebf7b6 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 10 Jun 2025 13:53:32 +0800 Subject: [PATCH 1/3] docs(design):construct anonymous functype in struct mapped named func type --- doc/en/dev/llcppg.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/doc/en/dev/llcppg.md b/doc/en/dev/llcppg.md index 7d9e57cc..a40b7c2a 100644 --- a/doc/en/dev/llcppg.md +++ b/doc/en/dev/llcppg.md @@ -83,7 +83,7 @@ func (recv_ *Sqlite3) Exec(sql *c.Char, callback func(c.Pointer, c.Int, **c.Char } ``` -For struct fields that are function pointers(both named and anonymous), the field type is replaced with a `c.Pointer` for description. +For struct fields that are named function pointer types, the field type is replaced with a `c.Pointer` for description. ```c typedef struct Stream { @@ -95,7 +95,13 @@ type Stream struct { Cb c.Pointer } ``` -anonymous function pointer +Due to the characteristics of LLGo, an anonymous function type cannot be directly declared as a Field Type. So to preserve as much information as possible, we need to use a `c.Pointer` to describe the field type. + +For anonymous function pointer types, llgo will build a public function type for them and reference it,and ensure that the anonymous type is unique, the naming rule for the corresponding type will be: +``` +LLGO___ +``` + ```c typedef struct Hooks { void *(*malloc_fn)(size_t sz); @@ -103,9 +109,14 @@ 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 } ``` From 2467df6ee511778fb6d5d8e7ad72a079a7d734e2 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Tue, 10 Jun 2025 14:35:27 +0800 Subject: [PATCH 2/3] docs(design):named fntype for nest anonymous field --- doc/en/dev/llcppg.md | 47 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/doc/en/dev/llcppg.md b/doc/en/dev/llcppg.md index a40b7c2a..6f719c19 100644 --- a/doc/en/dev/llcppg.md +++ b/doc/en/dev/llcppg.md @@ -99,7 +99,7 @@ Due to the characteristics of LLGo, an anonymous function type cannot be directl For anonymous function pointer types, llgo will build a public function type for them and reference it,and ensure that the anonymous type is unique, the naming rule for the corresponding type will be: ``` -LLGO___ +LLGO____ ``` ```c @@ -120,6 +120,51 @@ type Hooks struct { } ``` +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 +} +``` +with nested struct's function pointer +```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 + } +} +``` + ##### Array Arrays in C are mapped differently depending on their context - function parameters versus struct fields. From 545cb075e7a394e866542e5a967dc1be95aae0da Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Wed, 11 Jun 2025 10:51:25 +0800 Subject: [PATCH 3/3] docs(design):make autogen fntype private --- doc/en/dev/llcppg.md | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/doc/en/dev/llcppg.md b/doc/en/dev/llcppg.md index 5a00abcd..771de5c4 100644 --- a/doc/en/dev/llcppg.md +++ b/doc/en/dev/llcppg.md @@ -94,9 +94,10 @@ func (recv_ *Sqlite3) Exec(sql *c.Char, callback func(c.Pointer, c.Int, **c.Char } ``` -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: +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____ +llgo____ ``` ```c @@ -107,13 +108,13 @@ typedef struct Hooks { ``` ```go // llgo:type C -type LLGO_Hooks_MallocFn func(c.SizeT) c.Pointer +type llgo_Hooks_MallocFn func(c.SizeT) c.Pointer // llgo:type C -type LLGO_Hooks_FreeFn func(c.Pointer) +type llgo_Hooks_FreeFn func(c.Pointer) type Hooks struct { - MallocFn LLGO_Hooks_MallocFn - FreeFn LLGO_Hooks_FreeFn + MallocFn llgo_Hooks_MallocFn + FreeFn llgo_Hooks_FreeFn } ``` @@ -129,13 +130,13 @@ namespace A { ``` ```go // llgo:type C -type LLGO_A_Hooks_MallocFn func(c.SizeT) c.Pointer +type llgo_A_Hooks_MallocFn func(c.SizeT) c.Pointer // llgo:type C -type LLGO_A_Hooks_FreeFn func(c.Pointer) +type llgo_A_Hooks_FreeFn func(c.Pointer) type Hooks struct { - MallocFn LLGO_A_Hooks_MallocFn - FreeFn LLGO_A_Hooks_FreeFn + MallocFn llgo_A_Hooks_MallocFn + FreeFn llgo_A_Hooks_FreeFn } ``` in nested struct @@ -150,15 +151,15 @@ struct Foo { ``` ```go // llgo:type C -type LLGO_Foo_Hooks_MallocFn func(c.SizeT) c.Pointer +type llgo_Foo_Hooks_MallocFn func(c.SizeT) c.Pointer // llgo:type C -type LLGO_Foo_Hooks_FreeFn func(c.Pointer) +type llgo_Foo_Hooks_FreeFn func(c.Pointer) type Foo struct { Hooks struct { - MallocFn LLGO_Foo_Hooks_MallocFn - FreeFn LLGO_Foo_Hooks_FreeFn + MallocFn llgo_Foo_Hooks_MallocFn + FreeFn llgo_Foo_Hooks_FreeFn } } ```