暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

050-TestStructsLayout.cs 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // NOTE: Please read this before adding or changing anything in this file.
  2. //
  3. // This file doesn't contain any actual tests. It only contains structs.
  4. // Tests are automatically generated from all structs in this file,
  5. // which test:
  6. // - the size of the struct
  7. // - the offsets of each field
  8. //
  9. // When a struct contains a pointer, the test needs to use
  10. // OverrideOn32BitNative so that wasm tests can compare with the correct
  11. // values when testing 32-bit wasm on a 64-bit host platform.
  12. // While it would be possible to use Roslyn to calculate these
  13. // values automatically, for simplicity we use a couple of
  14. // generator-specific attributes to set these manually:
  15. // - [TestGeneratorOverride32BitSize(20)] should be set on a struct
  16. // - [TestGeneratorOverride32BitOffset(12)] should be set on a field
  17. // See the file below for examples.
  18. //
  19. // The test generation code lives in Burst.Compiler.IL.Tests.CodeGen.
  20. // After making changes to this file, please run that project.
  21. //
  22. // The generated tests are in 050-TestStructsLayout.Generated.cs.
  23. using System;
  24. using System.Runtime.InteropServices;
  25. namespace Burst.Compiler.IL.Tests
  26. {
  27. partial class TestStructsLayout
  28. {
  29. [StructLayout(LayoutKind.Explicit, Size = 8)]
  30. private unsafe struct CheckHoleInner
  31. {
  32. [FieldOffset(0)]
  33. public byte* m_Ptr;
  34. }
  35. [TestGeneratorOverride32BitSize(20)]
  36. private struct CheckHoleOuter
  37. {
  38. public CheckHoleInner a;
  39. public int b;
  40. [TestGeneratorOverride32BitOffset(12)]
  41. public CheckHoleInner c;
  42. }
  43. [StructLayout(LayoutKind.Explicit)]
  44. private struct ExplicitStructWithoutSize2
  45. {
  46. [FieldOffset(0)] public long a;
  47. [FieldOffset(8)] public sbyte b;
  48. [FieldOffset(9)] public int c;
  49. }
  50. [StructLayout(LayoutKind.Explicit)]
  51. private struct ExplicitStructWithoutSize
  52. {
  53. [FieldOffset(0)] public int a;
  54. [FieldOffset(4)] public sbyte b;
  55. [FieldOffset(5)] public int c;
  56. }
  57. [StructLayout(LayoutKind.Sequential, Size = 12)]
  58. private struct SequentialStructWithSize3
  59. {
  60. public int a;
  61. public int b;
  62. public sbyte c;
  63. }
  64. [StructLayout(LayoutKind.Sequential)]
  65. private struct SequentialStructWithoutSize
  66. {
  67. public int a;
  68. public int b;
  69. public sbyte c;
  70. }
  71. private struct SequentialStructEmptyNoAttributes { }
  72. [StructLayout(LayoutKind.Explicit)]
  73. private struct ExplicitStructWithEmptySequentialFields
  74. {
  75. [FieldOffset(0)] public SequentialStructEmptyNoAttributes FieldA;
  76. [FieldOffset(0)] public SequentialStructEmptyNoAttributes FieldB;
  77. }
  78. [StructLayout(LayoutKind.Explicit)]
  79. private struct ExplicitStrictWithEmptyAndNonEmptySequentialFields
  80. {
  81. [FieldOffset(0)] public SequentialStructEmptyNoAttributes FieldA;
  82. [FieldOffset(0)] public SequentialStructWithoutSize FieldB;
  83. }
  84. [StructLayout(LayoutKind.Sequential, Pack = 8)]
  85. private struct StructWithPack8
  86. {
  87. public int FieldA;
  88. public int FieldB;
  89. }
  90. [StructLayout(LayoutKind.Sequential, Pack = 2)]
  91. private struct StructPack2WithBytesAndInt
  92. {
  93. public byte FieldA;
  94. public byte FieldB;
  95. public int FieldC;
  96. }
  97. [StructLayout(LayoutKind.Sequential, Pack = 2)]
  98. private struct StructPack2WithBytesAndInts
  99. {
  100. public byte FieldA;
  101. public byte FieldB;
  102. public int FieldC;
  103. public int FieldD;
  104. }
  105. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  106. private struct StructPack1WithBytesAndInt
  107. {
  108. public byte FieldA;
  109. public byte FieldB;
  110. public int FieldC;
  111. }
  112. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  113. private struct StructPack1WithByteAndInt
  114. {
  115. public byte FieldA;
  116. public int FieldB;
  117. }
  118. private struct StructPack1WithByteAndIntWrapper
  119. {
  120. public StructPack1WithByteAndInt FieldA;
  121. public StructPack1WithByteAndInt FieldB;
  122. }
  123. private struct StructPack1WithByteAndIntWrapper2
  124. {
  125. public StructPack1WithByteAndIntWrapper FieldA;
  126. public StructPack1WithByteAndIntWrapper FieldB;
  127. }
  128. [StructLayout(LayoutKind.Sequential, Size = 12, Pack = 1)]
  129. private struct StructWithSizeAndPack
  130. {
  131. public double FieldA;
  132. public int FieldB;
  133. }
  134. private struct StructWithSizeAndPackWrapper
  135. {
  136. public byte FieldA;
  137. public StructWithSizeAndPack FieldB;
  138. }
  139. [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
  140. private struct StructWithSizeAndPack4
  141. {
  142. [FieldOffset(0)]
  143. public double FieldA;
  144. [FieldOffset(8)]
  145. public int FieldB;
  146. }
  147. private struct StructWithSizeAndPack4Wrapper
  148. {
  149. public byte FieldA;
  150. public StructWithSizeAndPack4 FieldB;
  151. }
  152. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  153. private struct StructExplicitPack1WithByteAndInt
  154. {
  155. [FieldOffset(0)]
  156. public byte FieldA;
  157. [FieldOffset(1)]
  158. public int FieldB;
  159. }
  160. private struct StructExplicitPack1WithByteAndIntWrapper
  161. {
  162. public StructExplicitPack1WithByteAndInt FieldA;
  163. public StructExplicitPack1WithByteAndInt FieldB;
  164. }
  165. private struct StructExplicitPack1WithByteAndIntWrapper2
  166. {
  167. public StructExplicitPack1WithByteAndIntWrapper FieldA;
  168. public StructExplicitPack1WithByteAndIntWrapper FieldB;
  169. }
  170. [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 1)]
  171. private struct StructExplicitWithSizeAndPack
  172. {
  173. [FieldOffset(0)]
  174. public double FieldA;
  175. [FieldOffset(8)]
  176. public int FieldB;
  177. }
  178. private struct StructExplicitWithSizeAndPackWrapper
  179. {
  180. public byte FieldA;
  181. public StructExplicitWithSizeAndPack FieldB;
  182. }
  183. [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
  184. private struct StructExplicitWithSizeAndPack4
  185. {
  186. [FieldOffset(0)]
  187. public double FieldA;
  188. [FieldOffset(8)]
  189. public int FieldB;
  190. }
  191. private struct StructExplicitWithSizeAndPack4Wrapper
  192. {
  193. public byte FieldA;
  194. public StructExplicitWithSizeAndPack4 FieldB;
  195. }
  196. }
  197. [AttributeUsage(AttributeTargets.Struct)]
  198. internal sealed class TestGeneratorOverride32BitSizeAttribute : Attribute
  199. {
  200. public readonly int Size;
  201. public TestGeneratorOverride32BitSizeAttribute(int size)
  202. {
  203. Size = size;
  204. }
  205. }
  206. [AttributeUsage(AttributeTargets.Field)]
  207. internal sealed class TestGeneratorOverride32BitOffsetAttribute : Attribute
  208. {
  209. public readonly int Offset;
  210. public TestGeneratorOverride32BitOffsetAttribute(int offset)
  211. {
  212. Offset = offset;
  213. }
  214. }
  215. }