msizeof5.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. ## tests for -d:checkAbi used by addAbiCheck via NIM_STATIC_ASSERT
  2. {.emit:"""/*TYPESECTION*/
  3. struct Foo1{
  4. int a;
  5. };
  6. struct Foo2{
  7. int a;
  8. };
  9. enum Foo3{k1, k2};
  10. typedef enum Foo3 Foo3b;
  11. typedef enum Foo4{k3, k4} Foo4;
  12. typedef int Foo5[3];
  13. typedef struct Foo6{
  14. int a1;
  15. bool a2;
  16. double a3;
  17. struct Foo6* a4;
  18. } Foo6;
  19. """.}
  20. template ensureCgen(T: typedesc) =
  21. ## ensures cgen
  22. var a {.volatile.}: T
  23. block:
  24. type Foo1Alias{.importc: "struct Foo1", size: sizeof(cint).} = object
  25. a: cint
  26. ensureCgen Foo1Alias
  27. block:
  28. type Foo3Alias{.importc: "enum Foo3", size: sizeof(cint).} = enum
  29. k1, k2
  30. ensureCgen Foo3Alias
  31. block:
  32. type Foo3bAlias{.importc: "Foo3b", size: sizeof(cint).} = enum
  33. k1, k2
  34. ensureCgen Foo3bAlias
  35. block:
  36. type Foo3b{.importc, size: sizeof(cint).} = enum
  37. k1, k2
  38. ensureCgen Foo3b
  39. static:
  40. doAssert Foo3b.sizeof == cint.sizeof
  41. block:
  42. type Foo4{.importc, size: sizeof(cint).} = enum
  43. k3, k4
  44. # adding entries should not yield duplicate ABI checks, as enforced by
  45. # `typeABICache`.
  46. # Currently the test doesn't check for this but you can inspect the cgen'd file
  47. ensureCgen Foo4
  48. ensureCgen Foo4
  49. ensureCgen Foo4
  50. block:
  51. type Foo5{.importc.} = array[3, cint]
  52. ensureCgen Foo5
  53. block:
  54. type Foo5{.importc.} = array[3, cint]
  55. ensureCgen Foo5
  56. block: # CT sizeof
  57. type Foo6GT = object # grountruth
  58. a1: cint
  59. a2: bool
  60. a3: cfloat
  61. a4: ptr Foo6GT
  62. type Foo6{.importc, completeStruct.} = object
  63. a1: cint
  64. a2: bool
  65. a3: cfloat
  66. a4: ptr Foo6
  67. static: doAssert compiles(static(Foo6.sizeof))
  68. static: doAssert Foo6.sizeof == Foo6GT.sizeof
  69. static: doAssert (Foo6, int, array[2, Foo6]).sizeof ==
  70. (Foo6GT, int, array[2, Foo6GT]).sizeof
  71. block:
  72. type GoodImportcType {.importc: "signed char", nodecl.} = char
  73. # "good" in sense the sizeof will match
  74. ensureCgen GoodImportcType
  75. block:
  76. type Foo6{.importc.} = object
  77. a1: cint
  78. doAssert compiles(Foo6.sizeof)
  79. static: doAssert not compiles(static(Foo6.sizeof))
  80. when defined caseBad:
  81. # Each case below should give a static cgen assert fail message
  82. block:
  83. type BadImportcType {.importc: "unsigned char", nodecl.} = uint64
  84. # "sizeof" check will fail
  85. ensureCgen BadImportcType
  86. block:
  87. type Foo2AliasBad{.importc: "struct Foo2", size: 1.} = object
  88. a: cint
  89. ensureCgen Foo2AliasBad
  90. block:
  91. type Foo5{.importc.} = array[4, cint]
  92. ensureCgen Foo5
  93. block:
  94. type Foo5{.importc.} = array[3, bool]
  95. ensureCgen Foo5
  96. block:
  97. type Foo6{.importc:"struct Foo6", completeStruct.} = object
  98. a1: cint
  99. # a2: bool # missing this should trigger assert fail
  100. a3: cfloat
  101. a4: ptr Foo6
  102. ensureCgen Foo6
  103. when false:
  104. block:
  105. # pre-existing BUG: this should give a CT error in semcheck because `size`
  106. # disagrees with `array[3, cint]`
  107. type Foo5{.importc, size: 1.} = array[3, cint]
  108. ensureCgen Foo5