Document about size pragma (#21794)

* Document about size pragma

* Fix typos

* Fix manual.md

* Update doc/manual.md

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
Tomohiro
2023-05-06 19:03:45 +09:00
committed by GitHub
parent 85dbfc68b5
commit 10328e50a5

View File

@@ -7485,6 +7485,37 @@ generates:
```
size pragma
-----------
Nim automatically determines the size of an enum.
But when wrapping a C enum type, it needs to be of a specific size.
The `size pragma` allows specifying the size of the enum type.
```Nim
type
EventType* {.size: sizeof(uint32).} = enum
QuitEvent,
AppTerminating,
AppLowMemory
doAssert sizeof(EventType) == sizeof(uint32)
```
The `size pragma` can also specify the size of an `importc` incomplete object type
so that one can get the size of it at compile time even if it was declared without fields.
```Nim
type
AtomicFlag* {.importc: "atomic_flag", header: "<stdatomic.h>", size: 1.} = object
static:
# if AtomicFlag didn't have the size pragma, this code would result in a compile time error.
echo sizeof(AtomicFlag)
```
The `size pragma` accepts only the values 1, 2, 4 or 8.
Align pragma
------------