Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 12 additions & 8 deletions compiler/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,19 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
of wSize:
if sym.typ == nil: invalidPragma(c, it)
var size = expectIntLit(c, it)
case size
of 1, 2, 4:
sym.typ.size = size
sym.typ.align = int16 size
of 8:
sym.typ.size = 8
sym.typ.align = floatInt64Align(c.config)
if sfImportc in sym.flags:
# no restrictions on size for imported types
setImportedTypeSize(c.config, sym.typ, size)
else:
localError(c.config, it.info, "size may only be 1, 2, 4 or 8")
case size
of 1, 2, 4:
sym.typ.size = size
sym.typ.align = int16 size
of 8:
sym.typ.size = 8
sym.typ.align = floatInt64Align(c.config)
else:
localError(c.config, it.info, "size may only be 1, 2, 4 or 8")
of wAlign:
let alignment = expectIntLit(c, it)
if isPowerOfTwo(alignment) and alignment > 0:
Expand Down
11 changes: 11 additions & 0 deletions compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,17 @@ proc getSize*(conf: ConfigRef; typ: PType): BiggestInt =
computeSizeAlign(conf, typ)
result = typ.size

proc setImportedTypeSize*(conf: ConfigRef, t: PType, size: int) =
t.size = size
if tfPacked in t.flags or size <= 1:
t.align = 1
elif size <= 2:
t.align = 2
elif size <= 4:
t.align = 4
else:
t.align = floatInt64Align(conf)

proc isConcept*(t: PType): bool=
case t.kind
of tyConcept: true
Expand Down
4 changes: 2 additions & 2 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -7815,6 +7815,8 @@ The `size pragma` allows specifying the size of the enum type.
doAssert sizeof(EventType) == sizeof(uint32)
```

When used for enum types, the `size pragma` accepts only the values 1, 2, 4 or 8.

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.

Expand All @@ -7827,8 +7829,6 @@ so that one can get the size of it at compile time even if it was declared witho
echo sizeof(AtomicFlag)
```

The `size pragma` accepts only the values 1, 2, 4 or 8.


Align pragma
------------
Expand Down
10 changes: 10 additions & 0 deletions tests/c/timportedsize.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{.emit: """
typedef struct Foo {
NI64 a;
NI64 b;
} Foo;
""".}

type Foo {.importc: "Foo", size: 16.} = object

var x: Foo