暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

050-TestStructsLayout.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. using Unity.Burst.Intrinsics;
  26. namespace Burst.Compiler.IL.Tests
  27. {
  28. partial class TestStructsLayout
  29. {
  30. [StructLayout(LayoutKind.Explicit, Size = 8)]
  31. private unsafe struct CheckHoleInner
  32. {
  33. [FieldOffset(0)]
  34. public byte* m_Ptr;
  35. }
  36. [TestGeneratorOverride32BitSize(20)]
  37. private struct CheckHoleOuter
  38. {
  39. public CheckHoleInner a;
  40. public int b;
  41. [TestGeneratorOverride32BitOffset(12)]
  42. public CheckHoleInner c;
  43. }
  44. [StructLayout(LayoutKind.Explicit)]
  45. private struct ExplicitStructWithoutSize2
  46. {
  47. [FieldOffset(0)] public long a;
  48. [FieldOffset(8)] public sbyte b;
  49. [FieldOffset(9)] public int c;
  50. }
  51. [StructLayout(LayoutKind.Explicit)]
  52. private struct ExplicitStructWithoutSize
  53. {
  54. [FieldOffset(0)] public int a;
  55. [FieldOffset(4)] public sbyte b;
  56. [FieldOffset(5)] public int c;
  57. }
  58. [StructLayout(LayoutKind.Sequential, Size = 12)]
  59. private struct SequentialStructWithSize3
  60. {
  61. public int a;
  62. public int b;
  63. public sbyte c;
  64. }
  65. [StructLayout(LayoutKind.Sequential)]
  66. private struct SequentialStructWithoutSize
  67. {
  68. public int a;
  69. public int b;
  70. public sbyte c;
  71. }
  72. private struct SequentialStructEmptyNoAttributes { }
  73. [StructLayout(LayoutKind.Explicit)]
  74. private struct ExplicitStructWithEmptySequentialFields
  75. {
  76. [FieldOffset(0)] public SequentialStructEmptyNoAttributes FieldA;
  77. [FieldOffset(0)] public SequentialStructEmptyNoAttributes FieldB;
  78. }
  79. [StructLayout(LayoutKind.Explicit)]
  80. private struct ExplicitStrictWithEmptyAndNonEmptySequentialFields
  81. {
  82. [FieldOffset(0)] public SequentialStructEmptyNoAttributes FieldA;
  83. [FieldOffset(0)] public SequentialStructWithoutSize FieldB;
  84. }
  85. [StructLayout(LayoutKind.Sequential, Pack = 8)]
  86. private struct StructWithPack8
  87. {
  88. public int FieldA;
  89. public int FieldB;
  90. }
  91. [StructLayout(LayoutKind.Sequential, Pack = 2)]
  92. private struct StructPack2WithBytesAndInt
  93. {
  94. public byte FieldA;
  95. public byte FieldB;
  96. public int FieldC;
  97. }
  98. [StructLayout(LayoutKind.Sequential, Pack = 2)]
  99. private struct StructPack2WithBytesAndInts
  100. {
  101. public byte FieldA;
  102. public byte FieldB;
  103. public int FieldC;
  104. public int FieldD;
  105. }
  106. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  107. private struct StructPack1WithBytesAndInt
  108. {
  109. public byte FieldA;
  110. public byte FieldB;
  111. public int FieldC;
  112. }
  113. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  114. private struct StructPack1WithByteAndInt
  115. {
  116. public byte FieldA;
  117. public int FieldB;
  118. }
  119. private struct StructPack1WithByteAndIntWrapper
  120. {
  121. public StructPack1WithByteAndInt FieldA;
  122. public StructPack1WithByteAndInt FieldB;
  123. }
  124. private struct StructPack1WithByteAndIntWrapper2
  125. {
  126. public StructPack1WithByteAndIntWrapper FieldA;
  127. public StructPack1WithByteAndIntWrapper FieldB;
  128. }
  129. [StructLayout(LayoutKind.Sequential, Size = 12, Pack = 1)]
  130. private struct StructWithSizeAndPack
  131. {
  132. public double FieldA;
  133. public int FieldB;
  134. }
  135. private struct StructWithSizeAndPackWrapper
  136. {
  137. public byte FieldA;
  138. public StructWithSizeAndPack FieldB;
  139. }
  140. [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
  141. private struct StructWithSizeAndPack4
  142. {
  143. [FieldOffset(0)]
  144. public double FieldA;
  145. [FieldOffset(8)]
  146. public int FieldB;
  147. }
  148. private struct StructWithSizeAndPack4Wrapper
  149. {
  150. public byte FieldA;
  151. public StructWithSizeAndPack4 FieldB;
  152. }
  153. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  154. private struct StructExplicitPack1WithByteAndInt
  155. {
  156. [FieldOffset(0)]
  157. public byte FieldA;
  158. [FieldOffset(1)]
  159. public int FieldB;
  160. }
  161. private struct StructExplicitPack1WithByteAndIntWrapper
  162. {
  163. public StructExplicitPack1WithByteAndInt FieldA;
  164. public StructExplicitPack1WithByteAndInt FieldB;
  165. }
  166. private struct StructExplicitPack1WithByteAndIntWrapper2
  167. {
  168. public StructExplicitPack1WithByteAndIntWrapper FieldA;
  169. public StructExplicitPack1WithByteAndIntWrapper FieldB;
  170. }
  171. [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 1)]
  172. private struct StructExplicitWithSizeAndPack
  173. {
  174. [FieldOffset(0)]
  175. public double FieldA;
  176. [FieldOffset(8)]
  177. public int FieldB;
  178. }
  179. private struct StructExplicitWithSizeAndPackWrapper
  180. {
  181. public byte FieldA;
  182. public StructExplicitWithSizeAndPack FieldB;
  183. }
  184. [StructLayout(LayoutKind.Explicit, Size = 12, Pack = 4)]
  185. private struct StructExplicitWithSizeAndPack4
  186. {
  187. [FieldOffset(0)]
  188. public double FieldA;
  189. [FieldOffset(8)]
  190. public int FieldB;
  191. }
  192. private struct StructExplicitWithSizeAndPack4Wrapper
  193. {
  194. public byte FieldA;
  195. public StructExplicitWithSizeAndPack4 FieldB;
  196. }
  197. private struct Vector64Container
  198. {
  199. public byte Byte;
  200. public v64 Vector;
  201. }
  202. private struct Vector128Container
  203. {
  204. public byte Byte;
  205. public v128 Vector;
  206. }
  207. private struct Vector256Container
  208. {
  209. public byte Byte;
  210. public v256 Vector;
  211. }
  212. }
  213. [AttributeUsage(AttributeTargets.Struct)]
  214. internal sealed class TestGeneratorOverride32BitSizeAttribute : Attribute
  215. {
  216. public readonly int Size;
  217. public TestGeneratorOverride32BitSizeAttribute(int size)
  218. {
  219. Size = size;
  220. }
  221. }
  222. [AttributeUsage(AttributeTargets.Field)]
  223. internal sealed class TestGeneratorOverride32BitOffsetAttribute : Attribute
  224. {
  225. public readonly int Offset;
  226. public TestGeneratorOverride32BitOffsetAttribute(int offset)
  227. {
  228. Offset = offset;
  229. }
  230. }
  231. }