doc_format.odin 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package odin_doc_format
  2. import "core:mem"
  3. Array :: struct($T: typeid) {
  4. offset: u32le,
  5. length: u32le,
  6. }
  7. String :: distinct Array(byte)
  8. Version_Type_Major :: 0
  9. Version_Type_Minor :: 2
  10. Version_Type_Patch :: 1
  11. Version_Type :: struct {
  12. major, minor, patch: u8,
  13. _: u8,
  14. }
  15. Version_Type_Default :: Version_Type{
  16. major=Version_Type_Major,
  17. minor=Version_Type_Minor,
  18. patch=Version_Type_Patch,
  19. }
  20. Magic_String :: "odindoc\x00"
  21. Header_Base :: struct {
  22. magic: [8]byte,
  23. _: u32le, // padding
  24. version: Version_Type,
  25. total_size: u32le, // in bytes
  26. header_size: u32le, // in bytes
  27. hash: u32le, // hash of the data after the header (header_size)
  28. }
  29. Header :: struct {
  30. using base: Header_Base,
  31. // NOTE: These arrays reserve the zero element as a sentinel value
  32. files: Array(File),
  33. pkgs: Array(Pkg),
  34. entities: Array(Entity),
  35. types: Array(Type),
  36. }
  37. File_Index :: distinct u32le
  38. Pkg_Index :: distinct u32le
  39. Entity_Index :: distinct u32le
  40. Type_Index :: distinct u32le
  41. Position :: struct {
  42. file: File_Index,
  43. line: u32le,
  44. column: u32le,
  45. offset: u32le,
  46. }
  47. File :: struct {
  48. pkg: Pkg_Index,
  49. name: String,
  50. }
  51. Pkg_Flag :: enum u32le {
  52. Builtin = 0,
  53. Runtime = 1,
  54. Init = 2,
  55. }
  56. Pkg_Flags :: distinct bit_set[Pkg_Flag; u32le]
  57. Pkg :: struct {
  58. fullpath: String,
  59. name: String,
  60. flags: Pkg_Flags,
  61. docs: String,
  62. files: Array(File_Index),
  63. entities: Array(Entity_Index),
  64. }
  65. Entity_Kind :: enum u32le {
  66. Invalid = 0,
  67. Constant = 1,
  68. Variable = 2,
  69. Type_Name = 3,
  70. Procedure = 4,
  71. Proc_Group = 5,
  72. Import_Name = 6,
  73. Library_Name = 7,
  74. }
  75. Entity_Flag :: enum u32le {
  76. Foreign = 0,
  77. Export = 1,
  78. Param_Using = 2, // using
  79. Param_Const = 3, // #const
  80. Param_Auto_Cast = 4, // auto_cast
  81. Param_Ellipsis = 5, // Variadic parameter
  82. Param_CVararg = 6, // #c_vararg
  83. Param_No_Alias = 7, // #no_alias
  84. Param_Any_Int = 8, // #any_int
  85. Type_Alias = 20,
  86. Var_Thread_Local = 40,
  87. Var_Static = 41,
  88. }
  89. Entity_Flags :: distinct bit_set[Entity_Flag; u64le]
  90. Entity :: struct {
  91. kind: Entity_Kind,
  92. _: u32le, // reserved
  93. flags: Entity_Flags,
  94. pos: Position,
  95. name: String,
  96. type: Type_Index,
  97. init_string: String,
  98. _: u32le, // reserved for init
  99. comment: String,
  100. docs: String,
  101. // May used by:
  102. // .Variable
  103. // .Procedure
  104. foreign_library: Entity_Index,
  105. // May used by:
  106. // .Variable
  107. // .Procedure
  108. link_name: String,
  109. attributes: Array(Attribute),
  110. // Used by: .Proc_Group
  111. grouped_entities: Array(Entity_Index),
  112. // May used by: .Procedure
  113. where_clauses: Array(String),
  114. }
  115. Attribute :: struct {
  116. name: String,
  117. value: String,
  118. }
  119. Type_Kind :: enum u32le {
  120. Invalid = 0,
  121. Basic = 1,
  122. Named = 2,
  123. Generic = 3,
  124. Pointer = 4,
  125. Array = 5,
  126. Enumerated_Array = 6,
  127. Slice = 7,
  128. Dynamic_Array = 8,
  129. Map = 9,
  130. Struct = 10,
  131. Union = 11,
  132. Enum = 12,
  133. Tuple = 13,
  134. Proc = 14,
  135. Bit_Set = 15,
  136. Simd_Vector = 16,
  137. SOA_Struct_Fixed = 17,
  138. SOA_Struct_Slice = 18,
  139. SOA_Struct_Dynamic = 19,
  140. Relative_Pointer = 20,
  141. Relative_Slice = 21,
  142. Multi_Pointer = 22,
  143. Matrix = 23,
  144. }
  145. Type_Elems_Cap :: 4
  146. Type :: struct {
  147. kind: Type_Kind,
  148. // Type_Kind specific used by some types
  149. // Underlying flag types:
  150. // .Basic - Type_Flags_Basic
  151. // .Struct - Type_Flags_Struct
  152. // .Union - Type_Flags_Union
  153. // .Proc - Type_Flags_Proc
  154. // .Bit_Set - Type_Flags_Bit_Set
  155. flags: u32le,
  156. // Used by:
  157. // .Basic
  158. // .Named
  159. // .Generic
  160. name: String,
  161. // Used By: .Struct, .Union
  162. custom_align: String,
  163. // Used by:
  164. // .Array - 1 count: 0=len
  165. // .Enumerated_Array - 1 count: 0=len
  166. // .SOA_Struct_Fixed - 1 count: 0=len
  167. // .Bit_Set - 2 count: 0=lower, 1=upper
  168. // .Simd_Vector - 1 count: 0=len
  169. // .Matrix - 2 count: 0=row_count, 1=column_count
  170. elem_count_len: u32le,
  171. elem_counts: [Type_Elems_Cap]i64le,
  172. // Used by: .Procedures
  173. // blank implies the "odin" calling convention
  174. calling_convention: String,
  175. // Used by:
  176. // .Named - 1 type: 0=base type
  177. // .Generic - <1 type: 0=specialization
  178. // .Pointer - 1 type: 0=element
  179. // .Array - 1 type: 0=element
  180. // .Enumerated_Array - 2 types: 0=index and 1=element
  181. // .Slice - 1 type: 0=element
  182. // .Dynamic_Array - 1 type: 0=element
  183. // .Map - 2 types: 0=key, 1=value
  184. // .SOA_Struct_Fixed - 1 type: underlying SOA struct element
  185. // .SOA_Struct_Slice - 1 type: underlying SOA struct element
  186. // .SOA_Struct_Dynamic - 1 type: underlying SOA struct element
  187. // .Union - 0+ types: variants
  188. // .Enum - <1 type: 0=base type
  189. // .Proc - 2 types: 0=parameters, 1=results
  190. // .Bit_Set - <=2 types: 0=element type, 1=underlying type (Underlying_Type flag will be set)
  191. // .Simd_Vector - 1 type: 0=element
  192. // .Relative_Pointer - 2 types: 0=pointer type, 1=base integer
  193. // .Relative_Slice - 2 types: 0=slice type, 1=base integer
  194. // .Multi_Pointer - 1 type: 0=element
  195. // .Matrix - 1 type: 0=element
  196. types: Array(Type_Index),
  197. // Used by:
  198. // .Named - 1 field for the definition
  199. // .Struct - fields
  200. // .Enum - fields
  201. // .Tuple - parameters (procedures only)
  202. entities: Array(Entity_Index),
  203. // Used By: .Struct, .Union
  204. polymorphic_params: Type_Index,
  205. // Used By: .Struct, .Union
  206. where_clauses: Array(String),
  207. }
  208. Type_Flags_Basic :: distinct bit_set[Type_Flag_Basic; u32le]
  209. Type_Flag_Basic :: enum u32le {
  210. Untyped = 1,
  211. }
  212. Type_Flags_Struct :: distinct bit_set[Type_Flag_Struct; u32le]
  213. Type_Flag_Struct :: enum u32le {
  214. Polymorphic = 0,
  215. Packed = 1,
  216. Raw_Union = 2,
  217. }
  218. Type_Flags_Union :: distinct bit_set[Type_Flag_Union; u32le]
  219. Type_Flag_Union :: enum u32le {
  220. Polymorphic = 0,
  221. No_Nil = 1,
  222. Maybe = 2,
  223. }
  224. Type_Flags_Proc :: distinct bit_set[Type_Flag_Proc; u32le]
  225. Type_Flag_Proc :: enum u32le {
  226. Polymorphic = 0,
  227. Diverging = 1,
  228. Optional_Ok = 2,
  229. Variadic = 3,
  230. C_Vararg = 4,
  231. }
  232. Type_Flags_Bit_Set :: distinct bit_set[Type_Flag_Bit_Set; u32le]
  233. Type_Flag_Bit_Set :: enum u32le {
  234. Range = 1,
  235. Op_Lt = 2,
  236. Op_Lt_Eq = 3,
  237. Underlying_Type = 4,
  238. }
  239. from_array :: proc(base: ^Header_Base, a: $A/Array($T)) -> []T {
  240. s: mem.Raw_Slice
  241. s.data = rawptr(uintptr(base) + uintptr(a.offset))
  242. s.len = int(a.length)
  243. return transmute([]T)s
  244. }
  245. from_string :: proc(base: ^Header_Base, s: String) -> string {
  246. return string(from_array(base, s))
  247. }
  248. Reader_Error :: enum {
  249. None,
  250. Header_Too_Small,
  251. Invalid_Magic,
  252. Data_Too_Small,
  253. Invalid_Version,
  254. }
  255. read_from_bytes :: proc(data: []byte) -> (h: ^Header, err: Reader_Error) {
  256. if len(data) < size_of(Header_Base) {
  257. err = .Header_Too_Small
  258. return
  259. }
  260. header_base := (^Header_Base)(raw_data(data))
  261. if header_base.magic != Magic_String {
  262. err = .Invalid_Magic
  263. return
  264. }
  265. if len(data) < int(header_base.total_size) {
  266. err = .Data_Too_Small
  267. return
  268. }
  269. if header_base.version != Version_Type_Default {
  270. err = .Invalid_Version
  271. return
  272. }
  273. h = (^Header)(header_base)
  274. return
  275. }