Skip to content

Commit e8fb122

Browse files
authored
docs: add nested enum (#530)
* docs: add nested enum * docs: add NOTE for nested enum
1 parent 260fe55 commit e8fb122

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

doc/en/dev/llcppg.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,89 @@ struct struct2 {
215215
};
216216
```
217217

218+
##### Nested Enum
219+
220+
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.
221+
222+
###### Anonymous Nested Enum
223+
224+
Anonymous nested enums are converted to inline enum constants within the parent struct context, with the enum values defaulting to `c.Int` type.
225+
226+
```c
227+
typedef struct Config {
228+
int version;
229+
enum {
230+
MODE_DEBUG = 0,
231+
MODE_RELEASE = 1
232+
} mode;
233+
} Config;
234+
```
235+
236+
**Generated Go code**:
237+
```go
238+
type Config struct {
239+
Version c.Int
240+
Mode c.Int
241+
}
242+
243+
const (
244+
MODE_DEBUG c.Int = 0
245+
MODE_RELEASE c.Int = 1
246+
)
247+
```
248+
249+
###### Named Nested Enum
250+
251+
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.
252+
253+
**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.
254+
255+
**NOTE:** Should we add type alias here in the future? See discussion: [#530](https://github.com/goplus/llcppg/pull/530)
256+
257+
```c
258+
typedef struct Config {
259+
int version;
260+
enum LogLevel {
261+
LOG_DEBUG = 0,
262+
LOG_INFO = 1,
263+
LOG_ERROR = 2
264+
} level;
265+
} Config;
266+
267+
// This is valid C - LogLevel is in global scope
268+
struct Config cfg;
269+
cfg.level = LOG_INFO;
270+
```
271+
272+
**Generated Go code**:
273+
```go
274+
type LogLevel c.Int
275+
276+
const (
277+
LOG_DEBUG LogLevel = 0
278+
LOG_INFO LogLevel = 1
279+
LOG_ERROR LogLevel = 2
280+
)
281+
282+
type Config struct {
283+
Version c.Int
284+
Level LogLevel
285+
}
286+
```
287+
288+
This is equivalent to:
289+
```c
290+
enum LogLevel {
291+
LOG_DEBUG = 0,
292+
LOG_INFO = 1,
293+
LOG_ERROR = 2
294+
};
295+
struct Config {
296+
int version;
297+
enum LogLevel level;
298+
};
299+
```
300+
218301
##### Function
219302

220303
###### To Normal Function

0 commit comments

Comments
 (0)