From 65deed8743f3da1bf3746e391c18efef8b22b85f Mon Sep 17 00:00:00 2001 From: Haolan Date: Tue, 5 Aug 2025 15:09:42 +0800 Subject: [PATCH 1/2] docs: add nested enum --- doc/en/dev/llcppg.md | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/doc/en/dev/llcppg.md b/doc/en/dev/llcppg.md index cdb0184c..e7284969 100644 --- a/doc/en/dev/llcppg.md +++ b/doc/en/dev/llcppg.md @@ -215,6 +215,86 @@ struct struct2 { }; ``` +##### Nested Enum + +Similar to nested structs, nested enums can also be accessed in the global scope. llcppg handles named nested enums by creating separate type declarations that are accessible globally. + +###### Anonymous Nested Enum + +Anonymous nested enums are converted to inline enum constants within the parent struct context, with the enum values defaulting to `c.Int` type. + +```c +typedef struct Config { + int version; + enum { + MODE_DEBUG = 0, + MODE_RELEASE = 1 + } mode; +} Config; +``` + +**Generated Go code**: +```go +type Config struct { + Version c.Int + Mode c.Int +} + +const ( + MODE_DEBUG c.Int = 0 + MODE_RELEASE c.Int = 1 +) +``` + +###### Named Nested Enum + +Named nested enums in C are accessible in the global scope, not just within the context of the outer struct. llcppg handles this by creating separate enum type declarations. + +**Reason**: In C, named nested enums are declared in the global scope and can be used independently. This means the enum type can be used anywhere in the code, not just within the context of the outer struct. + +```c +typedef struct Config { + int version; + enum LogLevel { + LOG_DEBUG = 0, + LOG_INFO = 1, + LOG_ERROR = 2 + } level; +} Config; + +// This is valid C - LogLevel is in global scope +enum LogLevel current_level = LOG_INFO; +``` + +**Generated Go code**: +```go +type LogLevel c.Int + +const ( + LOG_DEBUG LogLevel = 0 + LOG_INFO LogLevel = 1 + LOG_ERROR LogLevel = 2 +) + +type Config struct { + Version c.Int + Level LogLevel +} +``` + +This is equivalent to: +```c +enum LogLevel { + LOG_DEBUG = 0, + LOG_INFO = 1, + LOG_ERROR = 2 +}; +struct Config { + int version; + enum LogLevel level; +}; +``` + ##### Function ###### To Normal Function From 3bb12f6c675462a6834e2b397c7b7622a6fc6559 Mon Sep 17 00:00:00 2001 From: Haolan Date: Fri, 15 Aug 2025 14:43:44 +0800 Subject: [PATCH 2/2] docs: add NOTE for nested enum --- doc/en/dev/llcppg.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/en/dev/llcppg.md b/doc/en/dev/llcppg.md index 2cdd5eec..ee70c63d 100644 --- a/doc/en/dev/llcppg.md +++ b/doc/en/dev/llcppg.md @@ -252,6 +252,8 @@ Named nested enums in C are accessible in the global scope, not just within the **Reason**: In C, named nested enums are declared in the global scope and can be used independently. This means the enum type can be used anywhere in the code, not just within the context of the outer struct. +**NOTE:** Should we add type alias here in the future? See discussion: [#530](https://github.com/goplus/llcppg/pull/530) + ```c typedef struct Config { int version; @@ -263,7 +265,8 @@ typedef struct Config { } Config; // This is valid C - LogLevel is in global scope -enum LogLevel current_level = LOG_INFO; +struct Config cfg; +cfg.level = LOG_INFO; ``` **Generated Go code**: