Skip to content

Commit 90af2f4

Browse files
authored
fixes #1360; importc makes fields exportc by default (#1532)
fixes #1360 ref #1516 As in Nim, the fields of `importc` should be `exportc` by default, which could be convenient without excess `exportc` pragmas on each fields
1 parent 2bfa402 commit 90af2f4

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

src/hexer/nifcgen.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type
9191
IsNodecl
9292
IsInheritable
9393
IsUnion
94+
IsImportExternal
9495

9596
proc trType(c: var EContext; n: var Cursor; flags: set[TypeFlag] = {})
9697

@@ -124,6 +125,11 @@ proc trField(c: var EContext; n: var Cursor; flags: set[TypeFlag] = {}) =
124125

125126
c.dest.addDotToken() # adds pragmas
126127

128+
if IsImportExternal in flags and prag.externName.len == 0:
129+
var baseName = pool.syms[s]
130+
extractBasename baseName
131+
prag.externName = baseName
132+
127133
if prag.externName.len > 0:
128134
c.registerMangle(s, c.toExtern(s, prag.externName))
129135

@@ -1046,6 +1052,8 @@ proc trTypeDecl(c: var EContext; n: var Cursor; mode: TraverseMode) =
10461052
flags.incl IsInheritable
10471053
if UnionP in prag.flags:
10481054
flags.incl IsUnion
1055+
if {ImportcP, ImportcppP} * prag.flags != {}:
1056+
flags.incl IsImportExternal
10491057
trType c, n, flags
10501058
takeParRi c, n
10511059
swap dst, c.dest

tests/nimony/ffi/bar.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
typedef struct {} Bar;
2+
3+
typedef struct {
4+
unsigned char r;
5+
unsigned char g;
6+
unsigned char b;
7+
unsigned char a;
8+
} Color;

tests/nimony/ffi/theader.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@ type Bar1 {.importc: "Bar1", header: "${path}/bar1.h".} = object
33

44
const header = "bar.h"
55
type Bar2 {.importc: "Bar", header: header.} = object
6+
7+
import std/assertions
8+
9+
type
10+
Color* {.bycopy, importc, header: header.} = object
11+
r*: uint8
12+
g*: uint8
13+
b*: uint8
14+
a*: uint8
15+
16+
var color = Color(r: 8, g: 8, b: 8, a: 8)
17+
assert color.r == 8

0 commit comments

Comments
 (0)