reflect.odin 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. package reflect
  2. import "core:runtime"
  3. import "core:intrinsics"
  4. import "core:mem"
  5. _ :: mem
  6. _ :: intrinsics
  7. Type_Info :: runtime.Type_Info
  8. Type_Info_Named :: runtime.Type_Info_Named
  9. Type_Info_Integer :: runtime.Type_Info_Integer
  10. Type_Info_Rune :: runtime.Type_Info_Rune
  11. Type_Info_Float :: runtime.Type_Info_Float
  12. Type_Info_Complex :: runtime.Type_Info_Complex
  13. Type_Info_Quaternion :: runtime.Type_Info_Quaternion
  14. Type_Info_String :: runtime.Type_Info_String
  15. Type_Info_Boolean :: runtime.Type_Info_Boolean
  16. Type_Info_Any :: runtime.Type_Info_Any
  17. Type_Info_Type_Id :: runtime.Type_Info_Type_Id
  18. Type_Info_Pointer :: runtime.Type_Info_Pointer
  19. Type_Info_Multi_Pointer :: runtime.Type_Info_Multi_Pointer
  20. Type_Info_Procedure :: runtime.Type_Info_Procedure
  21. Type_Info_Array :: runtime.Type_Info_Array
  22. Type_Info_Enumerated_Array :: runtime.Type_Info_Enumerated_Array
  23. Type_Info_Dynamic_Array :: runtime.Type_Info_Dynamic_Array
  24. Type_Info_Slice :: runtime.Type_Info_Slice
  25. Type_Info_Parameters :: runtime.Type_Info_Parameters
  26. Type_Info_Tuple :: runtime.Type_Info_Parameters
  27. Type_Info_Struct :: runtime.Type_Info_Struct
  28. Type_Info_Union :: runtime.Type_Info_Union
  29. Type_Info_Enum :: runtime.Type_Info_Enum
  30. Type_Info_Map :: runtime.Type_Info_Map
  31. Type_Info_Bit_Set :: runtime.Type_Info_Bit_Set
  32. Type_Info_Simd_Vector :: runtime.Type_Info_Simd_Vector
  33. Type_Info_Relative_Pointer :: runtime.Type_Info_Relative_Pointer
  34. Type_Info_Relative_Slice :: runtime.Type_Info_Relative_Slice
  35. Type_Info_Matrix :: runtime.Type_Info_Matrix
  36. Type_Info_Soa_Pointer :: runtime.Type_Info_Soa_Pointer
  37. Type_Info_Enum_Value :: runtime.Type_Info_Enum_Value
  38. Type_Kind :: enum {
  39. Invalid,
  40. Named,
  41. Integer,
  42. Rune,
  43. Float,
  44. Complex,
  45. Quaternion,
  46. String,
  47. Boolean,
  48. Any,
  49. Type_Id,
  50. Pointer,
  51. Multi_Pointer,
  52. Procedure,
  53. Array,
  54. Enumerated_Array,
  55. Dynamic_Array,
  56. Slice,
  57. Tuple,
  58. Struct,
  59. Union,
  60. Enum,
  61. Map,
  62. Bit_Set,
  63. Simd_Vector,
  64. Relative_Pointer,
  65. Relative_Slice,
  66. Matrix,
  67. Soa_Pointer,
  68. }
  69. @(require_results)
  70. type_kind :: proc(T: typeid) -> Type_Kind {
  71. ti := type_info_of(T)
  72. if ti != nil {
  73. switch _ in ti.variant {
  74. case Type_Info_Named: return .Named
  75. case Type_Info_Integer: return .Integer
  76. case Type_Info_Rune: return .Rune
  77. case Type_Info_Float: return .Float
  78. case Type_Info_Complex: return .Complex
  79. case Type_Info_Quaternion: return .Quaternion
  80. case Type_Info_String: return .String
  81. case Type_Info_Boolean: return .Boolean
  82. case Type_Info_Any: return .Any
  83. case Type_Info_Type_Id: return .Type_Id
  84. case Type_Info_Pointer: return .Pointer
  85. case Type_Info_Multi_Pointer: return .Multi_Pointer
  86. case Type_Info_Procedure: return .Procedure
  87. case Type_Info_Array: return .Array
  88. case Type_Info_Enumerated_Array: return .Enumerated_Array
  89. case Type_Info_Dynamic_Array: return .Dynamic_Array
  90. case Type_Info_Slice: return .Slice
  91. case Type_Info_Parameters: return .Tuple
  92. case Type_Info_Struct: return .Struct
  93. case Type_Info_Union: return .Union
  94. case Type_Info_Enum: return .Enum
  95. case Type_Info_Map: return .Map
  96. case Type_Info_Bit_Set: return .Bit_Set
  97. case Type_Info_Simd_Vector: return .Simd_Vector
  98. case Type_Info_Relative_Pointer: return .Relative_Pointer
  99. case Type_Info_Relative_Slice: return .Relative_Slice
  100. case Type_Info_Matrix: return .Matrix
  101. case Type_Info_Soa_Pointer: return .Soa_Pointer
  102. }
  103. }
  104. return .Invalid
  105. }
  106. // TODO(bill): Better name
  107. @(require_results)
  108. underlying_type_kind :: proc(T: typeid) -> Type_Kind {
  109. return type_kind(runtime.typeid_base(T))
  110. }
  111. // TODO(bill): Better name
  112. @(require_results)
  113. backing_type_kind :: proc(T: typeid) -> Type_Kind {
  114. return type_kind(runtime.typeid_core(T))
  115. }
  116. type_info_base :: runtime.type_info_base
  117. type_info_core :: runtime.type_info_core
  118. type_info_base_without_enum :: type_info_core
  119. when !ODIN_DISALLOW_RTTI {
  120. typeid_base :: runtime.typeid_base
  121. typeid_core :: runtime.typeid_core
  122. typeid_base_without_enum :: typeid_core
  123. }
  124. @(require_results)
  125. any_base :: proc(v: any) -> any {
  126. v := v
  127. if v != nil {
  128. v.id = typeid_base(v.id)
  129. }
  130. return v
  131. }
  132. @(require_results)
  133. any_core :: proc(v: any) -> any {
  134. v := v
  135. if v != nil {
  136. v.id = typeid_core(v.id)
  137. }
  138. return v
  139. }
  140. @(require_results)
  141. typeid_elem :: proc(id: typeid) -> typeid {
  142. ti := type_info_of(id)
  143. if ti == nil { return nil }
  144. bits := 8*ti.size
  145. #partial switch v in ti.variant {
  146. case Type_Info_Complex:
  147. switch bits {
  148. case 64: return f32
  149. case 128: return f64
  150. }
  151. case Type_Info_Quaternion:
  152. switch bits {
  153. case 128: return f32
  154. case 256: return f64
  155. }
  156. case Type_Info_Pointer: return v.elem.id
  157. case Type_Info_Multi_Pointer: return v.elem.id
  158. case Type_Info_Soa_Pointer: return v.elem.id
  159. case Type_Info_Array: return v.elem.id
  160. case Type_Info_Enumerated_Array: return v.elem.id
  161. case Type_Info_Slice: return v.elem.id
  162. case Type_Info_Dynamic_Array: return v.elem.id
  163. }
  164. return id
  165. }
  166. @(require_results)
  167. size_of_typeid :: proc(T: typeid) -> int {
  168. if ti := type_info_of(T); ti != nil {
  169. return ti.size
  170. }
  171. return 0
  172. }
  173. @(require_results)
  174. align_of_typeid :: proc(T: typeid) -> int {
  175. if ti := type_info_of(T); ti != nil {
  176. return ti.align
  177. }
  178. return 1
  179. }
  180. @(require_results)
  181. as_bytes :: proc(v: any) -> []byte {
  182. if v != nil {
  183. sz := size_of_typeid(v.id)
  184. return ([^]byte)(v.data)[:sz]
  185. }
  186. return nil
  187. }
  188. @(require_results)
  189. any_data :: #force_inline proc(v: any) -> (data: rawptr, id: typeid) {
  190. return v.data, v.id
  191. }
  192. @(require_results)
  193. is_nil :: proc(v: any) -> bool {
  194. if v == nil {
  195. return true
  196. }
  197. data := as_bytes(v)
  198. if data == nil {
  199. return true
  200. }
  201. for v in data {
  202. if v != 0 {
  203. return false
  204. }
  205. }
  206. return true
  207. }
  208. @(require_results)
  209. length :: proc(val: any) -> int {
  210. if val == nil { return 0 }
  211. #partial switch a in type_info_of(val.id).variant {
  212. case Type_Info_Named:
  213. return length({val.data, a.base.id})
  214. case Type_Info_Pointer:
  215. return length({val.data, a.elem.id})
  216. case Type_Info_Array:
  217. return a.count
  218. case Type_Info_Enumerated_Array:
  219. return a.count
  220. case Type_Info_Slice:
  221. return (^runtime.Raw_Slice)(val.data).len
  222. case Type_Info_Dynamic_Array:
  223. return (^runtime.Raw_Dynamic_Array)(val.data).len
  224. case Type_Info_Map:
  225. return runtime.map_len((^runtime.Raw_Map)(val.data)^)
  226. case Type_Info_String:
  227. if a.is_cstring {
  228. return len((^cstring)(val.data)^)
  229. } else {
  230. return (^runtime.Raw_String)(val.data).len
  231. }
  232. }
  233. return 0
  234. }
  235. @(require_results)
  236. capacity :: proc(val: any) -> int {
  237. if val == nil { return 0 }
  238. #partial switch a in type_info_of(val.id).variant {
  239. case Type_Info_Named:
  240. return capacity({val.data, a.base.id})
  241. case Type_Info_Pointer:
  242. return capacity({val.data, a.elem.id})
  243. case Type_Info_Array:
  244. return a.count
  245. case Type_Info_Enumerated_Array:
  246. return a.count
  247. case Type_Info_Dynamic_Array:
  248. return (^runtime.Raw_Dynamic_Array)(val.data).cap
  249. case Type_Info_Map:
  250. return runtime.map_cap((^runtime.Raw_Map)(val.data)^)
  251. }
  252. return 0
  253. }
  254. @(require_results)
  255. index :: proc(val: any, i: int, loc := #caller_location) -> any {
  256. if val == nil { return nil }
  257. #partial switch a in type_info_of(val.id).variant {
  258. case Type_Info_Named:
  259. return index({val.data, a.base.id}, i, loc)
  260. case Type_Info_Pointer:
  261. ptr := (^rawptr)(val.data)^
  262. if ptr == nil {
  263. return nil
  264. }
  265. return index({ptr, a.elem.id}, i, loc)
  266. case Type_Info_Multi_Pointer:
  267. ptr := (^rawptr)(val.data)^
  268. if ptr == nil {
  269. return nil
  270. }
  271. return index({ptr, a.elem.id}, i, loc)
  272. case Type_Info_Array:
  273. runtime.bounds_check_error_loc(loc, i, a.count)
  274. offset := uintptr(a.elem.size * i)
  275. data := rawptr(uintptr(val.data) + offset)
  276. return any{data, a.elem.id}
  277. case Type_Info_Enumerated_Array:
  278. runtime.bounds_check_error_loc(loc, i, a.count)
  279. offset := uintptr(a.elem.size * i)
  280. data := rawptr(uintptr(val.data) + offset)
  281. return any{data, a.elem.id}
  282. case Type_Info_Slice:
  283. raw := (^runtime.Raw_Slice)(val.data)
  284. runtime.bounds_check_error_loc(loc, i, raw.len)
  285. offset := uintptr(a.elem.size * i)
  286. data := rawptr(uintptr(raw.data) + offset)
  287. return any{data, a.elem.id}
  288. case Type_Info_Dynamic_Array:
  289. raw := (^runtime.Raw_Dynamic_Array)(val.data)
  290. runtime.bounds_check_error_loc(loc, i, raw.len)
  291. offset := uintptr(a.elem.size * i)
  292. data := rawptr(uintptr(raw.data) + offset)
  293. return any{data, a.elem.id}
  294. case Type_Info_String:
  295. if a.is_cstring { return nil }
  296. raw := (^runtime.Raw_String)(val.data)
  297. runtime.bounds_check_error_loc(loc, i, raw.len)
  298. offset := uintptr(size_of(u8) * i)
  299. data := rawptr(uintptr(raw.data) + offset)
  300. return any{data, typeid_of(u8)}
  301. }
  302. return nil
  303. }
  304. @(require_results)
  305. deref :: proc(val: any) -> any {
  306. if val != nil {
  307. ti := type_info_base(type_info_of(val.id))
  308. if info, ok := ti.variant.(Type_Info_Pointer); ok {
  309. return any{
  310. (^rawptr)(val.data)^,
  311. info.elem.id,
  312. }
  313. }
  314. }
  315. return val
  316. }
  317. // Struct_Tag represents the type of the string of a struct field
  318. //
  319. // Through convention, tags are the concatenation of optionally space separationed key:"value" pairs.
  320. // Each key is a non-empty string which contains no control characters other than space, quotes, and colon.
  321. Struct_Tag :: distinct string
  322. Struct_Field :: struct {
  323. name: string,
  324. type: ^Type_Info,
  325. tag: Struct_Tag,
  326. offset: uintptr,
  327. is_using: bool,
  328. }
  329. @(require_results)
  330. struct_field_at :: proc(T: typeid, i: int) -> (field: Struct_Field) {
  331. ti := runtime.type_info_base(type_info_of(T))
  332. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  333. if 0 <= i && i < len(s.names) {
  334. field.name = s.names[i]
  335. field.type = s.types[i]
  336. field.tag = Struct_Tag(s.tags[i])
  337. field.offset = s.offsets[i]
  338. field.is_using = s.usings[i]
  339. }
  340. }
  341. return
  342. }
  343. @(require_results)
  344. struct_field_by_name :: proc(T: typeid, name: string) -> (field: Struct_Field) {
  345. ti := runtime.type_info_base(type_info_of(T))
  346. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  347. for fname, i in s.names {
  348. if fname == name {
  349. field.name = s.names[i]
  350. field.type = s.types[i]
  351. field.tag = Struct_Tag(s.tags[i])
  352. field.offset = s.offsets[i]
  353. field.is_using = s.usings[i]
  354. break
  355. }
  356. }
  357. }
  358. return
  359. }
  360. @(require_results)
  361. struct_field_value_by_name :: proc(a: any, field: string, allow_using := false) -> any {
  362. if a == nil { return nil }
  363. ti := runtime.type_info_base(type_info_of(a.id))
  364. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  365. for name, i in s.names {
  366. if name == field {
  367. return any{
  368. rawptr(uintptr(a.data) + s.offsets[i]),
  369. s.types[i].id,
  370. }
  371. }
  372. if allow_using && s.usings[i] {
  373. f := any{
  374. rawptr(uintptr(a.data) + s.offsets[i]),
  375. s.types[i].id,
  376. }
  377. if res := struct_field_value_by_name(f, field, allow_using); res != nil {
  378. return res
  379. }
  380. }
  381. }
  382. }
  383. return nil
  384. }
  385. @(require_results)
  386. struct_field_names :: proc(T: typeid) -> []string {
  387. ti := runtime.type_info_base(type_info_of(T))
  388. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  389. return s.names
  390. }
  391. return nil
  392. }
  393. @(require_results)
  394. struct_field_types :: proc(T: typeid) -> []^Type_Info {
  395. ti := runtime.type_info_base(type_info_of(T))
  396. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  397. return s.types
  398. }
  399. return nil
  400. }
  401. @(require_results)
  402. struct_field_tags :: proc(T: typeid) -> []Struct_Tag {
  403. ti := runtime.type_info_base(type_info_of(T))
  404. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  405. return transmute([]Struct_Tag)s.tags
  406. }
  407. return nil
  408. }
  409. @(require_results)
  410. struct_field_offsets :: proc(T: typeid) -> []uintptr {
  411. ti := runtime.type_info_base(type_info_of(T))
  412. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  413. return s.offsets
  414. }
  415. return nil
  416. }
  417. @(require_results)
  418. struct_fields_zipped :: proc(T: typeid) -> (fields: #soa[]Struct_Field) {
  419. ti := runtime.type_info_base(type_info_of(T))
  420. if s, ok := ti.variant.(runtime.Type_Info_Struct); ok {
  421. return soa_zip(
  422. name = s.names,
  423. type = s.types,
  424. tag = transmute([]Struct_Tag)s.tags,
  425. offset = s.offsets,
  426. is_using = s.usings,
  427. )
  428. }
  429. return nil
  430. }
  431. @(require_results)
  432. struct_tag_get :: proc(tag: Struct_Tag, key: string) -> (value: Struct_Tag) {
  433. value, _ = struct_tag_lookup(tag, key)
  434. return
  435. }
  436. @(require_results)
  437. struct_tag_lookup :: proc(tag: Struct_Tag, key: string) -> (value: Struct_Tag, ok: bool) {
  438. for t := tag; t != ""; /**/ {
  439. i := 0
  440. for i < len(t) && t[i] == ' ' { // Skip whitespace
  441. i += 1
  442. }
  443. t = t[i:]
  444. if len(t) == 0 {
  445. break
  446. }
  447. i = 0
  448. loop: for i < len(t) {
  449. switch t[i] {
  450. case ':', '"':
  451. break loop
  452. case 0x00 ..< ' ', 0x7f ..= 0x9f: // break if control character is found
  453. break loop
  454. }
  455. i += 1
  456. }
  457. if i == 0 {
  458. break
  459. }
  460. if i+1 >= len(t) {
  461. break
  462. }
  463. if t[i] != ':' || t[i+1] != '"' {
  464. break
  465. }
  466. name := string(t[:i])
  467. t = t[i+1:]
  468. i = 1
  469. for i < len(t) && t[i] != '"' { // find closing quote
  470. if t[i] == '\\' {
  471. i += 1 // Skip escaped characters
  472. }
  473. i += 1
  474. }
  475. if i >= len(t) {
  476. break
  477. }
  478. val := string(t[:i+1])
  479. t = t[i+1:]
  480. if key == name {
  481. return Struct_Tag(val[1:i]), true
  482. }
  483. }
  484. return
  485. }
  486. @(require_results)
  487. enum_string :: proc(a: any) -> string {
  488. if a == nil { return "" }
  489. ti := runtime.type_info_base(type_info_of(a.id))
  490. if e, ok := ti.variant.(runtime.Type_Info_Enum); ok {
  491. v, _ := as_i64(a)
  492. for value, i in e.values {
  493. if value == Type_Info_Enum_Value(v) {
  494. return e.names[i]
  495. }
  496. }
  497. } else {
  498. panic("expected an enum to reflect.enum_string")
  499. }
  500. return ""
  501. }
  502. // Given a enum type and a value name, get the enum value.
  503. @(require_results)
  504. enum_from_name :: proc($Enum_Type: typeid, name: string) -> (value: Enum_Type, ok: bool) {
  505. ti := type_info_base(type_info_of(Enum_Type))
  506. if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
  507. for value_name, i in eti.names {
  508. if value_name != name {
  509. continue
  510. }
  511. v := eti.values[i]
  512. value = Enum_Type(v)
  513. ok = true
  514. return
  515. }
  516. }
  517. return
  518. }
  519. @(require_results)
  520. enum_from_name_any :: proc(Enum_Type: typeid, name: string) -> (value: Type_Info_Enum_Value, ok: bool) {
  521. ti := runtime.type_info_base(type_info_of(Enum_Type))
  522. if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
  523. for value_name, i in eti.names {
  524. if value_name != name {
  525. continue
  526. }
  527. value = eti.values[i]
  528. ok = true
  529. return
  530. }
  531. }
  532. return
  533. }
  534. @(require_results)
  535. enum_field_names :: proc(Enum_Type: typeid) -> []string {
  536. ti := runtime.type_info_base(type_info_of(Enum_Type))
  537. if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
  538. return eti.names
  539. }
  540. return nil
  541. }
  542. @(require_results)
  543. enum_field_values :: proc(Enum_Type: typeid) -> []Type_Info_Enum_Value {
  544. ti := runtime.type_info_base(type_info_of(Enum_Type))
  545. if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
  546. return eti.values
  547. }
  548. return nil
  549. }
  550. Enum_Field :: struct {
  551. name: string,
  552. value: Type_Info_Enum_Value,
  553. }
  554. @(require_results)
  555. enum_fields_zipped :: proc(Enum_Type: typeid) -> (fields: #soa[]Enum_Field) {
  556. ti := runtime.type_info_base(type_info_of(Enum_Type))
  557. if eti, eti_ok := ti.variant.(runtime.Type_Info_Enum); eti_ok {
  558. return soa_zip(name=eti.names, value=eti.values)
  559. }
  560. return nil
  561. }
  562. @(require_results)
  563. union_variant_type_info :: proc(a: any) -> ^Type_Info {
  564. id := union_variant_typeid(a)
  565. return type_info_of(id)
  566. }
  567. @(require_results)
  568. type_info_union_is_pure_maybe :: proc(info: runtime.Type_Info_Union) -> bool {
  569. return len(info.variants) == 1 && is_pointer(info.variants[0])
  570. }
  571. @(require_results)
  572. union_variant_typeid :: proc(a: any) -> typeid {
  573. if a == nil { return nil }
  574. ti := runtime.type_info_base(type_info_of(a.id))
  575. if info, ok := ti.variant.(runtime.Type_Info_Union); ok {
  576. if type_info_union_is_pure_maybe(info) {
  577. if a.data != nil {
  578. return info.variants[0].id
  579. }
  580. return nil
  581. }
  582. tag_ptr := uintptr(a.data) + info.tag_offset
  583. tag_any := any{rawptr(tag_ptr), info.tag_type.id}
  584. tag: i64 = ---
  585. switch i in tag_any {
  586. case u8: tag = i64(i)
  587. case i8: tag = i64(i)
  588. case u16: tag = i64(i)
  589. case i16: tag = i64(i)
  590. case u32: tag = i64(i)
  591. case i32: tag = i64(i)
  592. case u64: tag = i64(i)
  593. case i64: tag = i
  594. case: unimplemented()
  595. }
  596. if info.no_nil {
  597. return info.variants[tag].id
  598. } else if tag != 0 {
  599. return info.variants[tag-1].id
  600. }
  601. return nil
  602. }
  603. panic("expected a union to reflect.union_variant_typeid")
  604. }
  605. @(require_results)
  606. get_union_variant_raw_tag :: proc(a: any) -> i64 {
  607. if a == nil { return -1 }
  608. ti := runtime.type_info_base(type_info_of(a.id))
  609. if info, ok := ti.variant.(runtime.Type_Info_Union); ok {
  610. if type_info_union_is_pure_maybe(info) {
  611. return 1 if a.data != nil else 0
  612. }
  613. tag_ptr := uintptr(a.data) + info.tag_offset
  614. tag_any := any{rawptr(tag_ptr), info.tag_type.id}
  615. tag: i64 = ---
  616. switch i in tag_any {
  617. case u8: tag = i64(i)
  618. case i8: tag = i64(i)
  619. case u16: tag = i64(i)
  620. case i16: tag = i64(i)
  621. case u32: tag = i64(i)
  622. case i32: tag = i64(i)
  623. case u64: tag = i64(i)
  624. case i64: tag = i
  625. case: unimplemented()
  626. }
  627. return tag
  628. }
  629. panic("expected a union to reflect.get_union_variant_raw_tag")
  630. }
  631. @(require_results)
  632. get_union_variant :: proc(a: any) -> any {
  633. if a == nil {
  634. return nil
  635. }
  636. id := union_variant_typeid(a)
  637. if id == nil {
  638. return nil
  639. }
  640. return any{a.data, id}
  641. }
  642. @(require_results)
  643. get_union_as_ptr_variants :: proc(val: ^$T) -> (res: intrinsics.type_convert_variants_to_pointers(T)) where intrinsics.type_is_union(T) {
  644. ptr := rawptr(val)
  645. tag := get_union_variant_raw_tag(val^)
  646. mem.copy(&res, &ptr, size_of(ptr))
  647. set_union_variant_raw_tag(res, tag)
  648. return
  649. }
  650. set_union_variant_raw_tag :: proc(a: any, tag: i64) {
  651. if a == nil { return }
  652. ti := runtime.type_info_base(type_info_of(a.id))
  653. if info, ok := ti.variant.(runtime.Type_Info_Union); ok {
  654. if type_info_union_is_pure_maybe(info) {
  655. // Cannot do anything
  656. return
  657. }
  658. tag_ptr := uintptr(a.data) + info.tag_offset
  659. tag_any := any{rawptr(tag_ptr), info.tag_type.id}
  660. switch i in &tag_any {
  661. case u8: i = u8(tag)
  662. case i8: i = i8(tag)
  663. case u16: i = u16(tag)
  664. case i16: i = i16(tag)
  665. case u32: i = u32(tag)
  666. case i32: i = i32(tag)
  667. case u64: i = u64(tag)
  668. case i64: i = tag
  669. case: unimplemented()
  670. }
  671. return
  672. }
  673. panic("expected a union to reflect.set_union_variant_raw_tag")
  674. }
  675. set_union_variant_typeid :: proc(a: any, id: typeid) {
  676. if a == nil { return }
  677. ti := runtime.type_info_base(type_info_of(a.id))
  678. if info, ok := ti.variant.(runtime.Type_Info_Union); ok {
  679. if type_info_union_is_pure_maybe(info) {
  680. // Cannot do anything
  681. return
  682. }
  683. if id == nil && !info.no_nil {
  684. set_union_variant_raw_tag(a, 0)
  685. return
  686. }
  687. for variant, i in info.variants {
  688. if variant.id == id {
  689. tag := i64(i)
  690. if !info.no_nil {
  691. tag += 1
  692. }
  693. set_union_variant_raw_tag(a, tag)
  694. return
  695. }
  696. }
  697. return
  698. }
  699. panic("expected a union to reflect.set_union_variant_typeid")
  700. }
  701. set_union_variant_type_info :: proc(a: any, tag_ti: ^Type_Info) {
  702. if a == nil { return }
  703. ti := runtime.type_info_base(type_info_of(a.id))
  704. if info, ok := ti.variant.(runtime.Type_Info_Union); ok {
  705. if type_info_union_is_pure_maybe(info) {
  706. // Cannot do anything
  707. return
  708. }
  709. if tag_ti == nil && !info.no_nil {
  710. set_union_variant_raw_tag(a, 0)
  711. return
  712. }
  713. for variant, i in info.variants {
  714. if variant == tag_ti {
  715. tag := i64(i)
  716. if !info.no_nil {
  717. tag += 1
  718. }
  719. set_union_variant_raw_tag(a, tag)
  720. return
  721. }
  722. }
  723. return
  724. }
  725. panic("expected a union to reflect.set_union_variant_type_info")
  726. }
  727. set_union_value :: proc(dst: any, value: any) -> bool {
  728. if dst == nil { return false }
  729. ti := runtime.type_info_base(type_info_of(dst.id))
  730. if info, ok := ti.variant.(runtime.Type_Info_Union); ok {
  731. if value.id == nil {
  732. intrinsics.mem_zero(dst.data, ti.size)
  733. return true
  734. }
  735. if ti.id == runtime.typeid_base(value.id) {
  736. intrinsics.mem_copy(dst.data, value.data, ti.size)
  737. return true
  738. }
  739. if type_info_union_is_pure_maybe(info) {
  740. if variant := info.variants[0]; variant.id == value.id {
  741. intrinsics.mem_copy(dst.data, value.data, variant.size)
  742. return true
  743. }
  744. return false
  745. }
  746. for variant, i in info.variants {
  747. if variant.id == value.id {
  748. tag := i64(i)
  749. if !info.no_nil {
  750. tag += 1
  751. }
  752. intrinsics.mem_copy(dst.data, value.data, variant.size)
  753. set_union_variant_raw_tag(dst, tag)
  754. return true
  755. }
  756. }
  757. return false
  758. }
  759. panic("expected a union to reflect.set_union_variant_typeid")
  760. }
  761. @(require_results)
  762. as_bool :: proc(a: any) -> (value: bool, valid: bool) {
  763. if a == nil { return }
  764. a := a
  765. ti := runtime.type_info_core(type_info_of(a.id))
  766. a.id = ti.id
  767. #partial switch info in ti.variant {
  768. case Type_Info_Boolean:
  769. valid = true
  770. switch v in a {
  771. case bool: value = v
  772. case b8: value = bool(v)
  773. case b16: value = bool(v)
  774. case b32: value = bool(v)
  775. case b64: value = bool(v)
  776. case: valid = false
  777. }
  778. }
  779. return
  780. }
  781. @(require_results)
  782. as_int :: proc(a: any) -> (value: int, valid: bool) {
  783. v: i64
  784. v, valid = as_i64(a)
  785. value = int(v)
  786. return
  787. }
  788. @(require_results)
  789. as_uint :: proc(a: any) -> (value: uint, valid: bool) {
  790. v: u64
  791. v, valid = as_u64(a)
  792. value = uint(v)
  793. return
  794. }
  795. @(require_results)
  796. as_i64 :: proc(a: any) -> (value: i64, valid: bool) {
  797. if a == nil { return }
  798. a := a
  799. ti := runtime.type_info_core(type_info_of(a.id))
  800. a.id = ti.id
  801. #partial switch info in ti.variant {
  802. case Type_Info_Integer:
  803. valid = true
  804. switch v in a {
  805. case i8: value = i64(v)
  806. case i16: value = i64(v)
  807. case i32: value = i64(v)
  808. case i64: value = v
  809. case i128: value = i64(v)
  810. case int: value = i64(v)
  811. case u8: value = i64(v)
  812. case u16: value = i64(v)
  813. case u32: value = i64(v)
  814. case u64: value = i64(v)
  815. case u128: value = i64(v)
  816. case uint: value = i64(v)
  817. case uintptr: value = i64(v)
  818. case u16le: value = i64(v)
  819. case u32le: value = i64(v)
  820. case u64le: value = i64(v)
  821. case u128le: value = i64(v)
  822. case i16le: value = i64(v)
  823. case i32le: value = i64(v)
  824. case i64le: value = i64(v)
  825. case i128le: value = i64(v)
  826. case u16be: value = i64(v)
  827. case u32be: value = i64(v)
  828. case u64be: value = i64(v)
  829. case u128be: value = i64(v)
  830. case i16be: value = i64(v)
  831. case i32be: value = i64(v)
  832. case i64be: value = i64(v)
  833. case i128be: value = i64(v)
  834. case: valid = false
  835. }
  836. case Type_Info_Rune:
  837. r := a.(rune)
  838. value = i64(r)
  839. valid = true
  840. case Type_Info_Float:
  841. valid = true
  842. switch v in a {
  843. case f32: value = i64(v)
  844. case f64: value = i64(v)
  845. case f32le: value = i64(v)
  846. case f64le: value = i64(v)
  847. case f32be: value = i64(v)
  848. case f64be: value = i64(v)
  849. case: valid = false
  850. }
  851. case Type_Info_Boolean:
  852. valid = true
  853. switch v in a {
  854. case bool: value = i64(v)
  855. case b8: value = i64(v)
  856. case b16: value = i64(v)
  857. case b32: value = i64(v)
  858. case b64: value = i64(v)
  859. case: valid = false
  860. }
  861. case Type_Info_Complex:
  862. switch v in a {
  863. case complex64:
  864. if imag(v) == 0 {
  865. value = i64(real(v))
  866. valid = true
  867. }
  868. case complex128:
  869. if imag(v) == 0 {
  870. value = i64(real(v))
  871. valid = true
  872. }
  873. }
  874. case Type_Info_Quaternion:
  875. switch v in a {
  876. case quaternion128:
  877. if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
  878. value = i64(real(v))
  879. valid = true
  880. }
  881. case quaternion256:
  882. if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
  883. value = i64(real(v))
  884. valid = true
  885. }
  886. }
  887. }
  888. return
  889. }
  890. @(require_results)
  891. as_u64 :: proc(a: any) -> (value: u64, valid: bool) {
  892. if a == nil { return }
  893. a := a
  894. ti := runtime.type_info_core(type_info_of(a.id))
  895. a.id = ti.id
  896. #partial switch info in ti.variant {
  897. case Type_Info_Integer:
  898. valid = true
  899. switch v in a {
  900. case i8: value = u64(v)
  901. case i16: value = u64(v)
  902. case i32: value = u64(v)
  903. case i64: value = u64(v)
  904. case i128: value = u64(v)
  905. case int: value = u64(v)
  906. case u8: value = u64(v)
  907. case u16: value = u64(v)
  908. case u32: value = u64(v)
  909. case u64: value = (v)
  910. case u128: value = u64(v)
  911. case uint: value = u64(v)
  912. case uintptr:value = u64(v)
  913. case u16le: value = u64(v)
  914. case u32le: value = u64(v)
  915. case u64le: value = u64(v)
  916. case u128le: value = u64(v)
  917. case i16le: value = u64(v)
  918. case i32le: value = u64(v)
  919. case i64le: value = u64(v)
  920. case i128le: value = u64(v)
  921. case u16be: value = u64(v)
  922. case u32be: value = u64(v)
  923. case u64be: value = u64(v)
  924. case u128be: value = u64(v)
  925. case i16be: value = u64(v)
  926. case i32be: value = u64(v)
  927. case i64be: value = u64(v)
  928. case i128be: value = u64(v)
  929. case: valid = false
  930. }
  931. case Type_Info_Rune:
  932. r := a.(rune)
  933. value = u64(r)
  934. valid = true
  935. case Type_Info_Float:
  936. valid = true
  937. switch v in a {
  938. case f16: value = u64(v)
  939. case f32: value = u64(v)
  940. case f64: value = u64(v)
  941. case f32le: value = u64(v)
  942. case f64le: value = u64(v)
  943. case f32be: value = u64(v)
  944. case f64be: value = u64(v)
  945. case: valid = false
  946. }
  947. case Type_Info_Boolean:
  948. valid = true
  949. switch v in a {
  950. case bool: value = u64(v)
  951. case b8: value = u64(v)
  952. case b16: value = u64(v)
  953. case b32: value = u64(v)
  954. case b64: value = u64(v)
  955. case: valid = false
  956. }
  957. case Type_Info_Complex:
  958. switch v in a {
  959. case complex64:
  960. if imag(v) == 0 {
  961. value = u64(real(v))
  962. valid = true
  963. }
  964. case complex128:
  965. if imag(v) == 0 {
  966. value = u64(real(v))
  967. valid = true
  968. }
  969. }
  970. case Type_Info_Quaternion:
  971. switch v in a {
  972. case quaternion128:
  973. if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
  974. value = u64(real(v))
  975. valid = true
  976. }
  977. case quaternion256:
  978. if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
  979. value = u64(real(v))
  980. valid = true
  981. }
  982. }
  983. }
  984. return
  985. }
  986. @(require_results)
  987. as_f64 :: proc(a: any) -> (value: f64, valid: bool) {
  988. if a == nil { return }
  989. a := a
  990. ti := runtime.type_info_core(type_info_of(a.id))
  991. a.id = ti.id
  992. #partial switch info in ti.variant {
  993. case Type_Info_Integer:
  994. valid = true
  995. switch v in a {
  996. case i8: value = f64(v)
  997. case i16: value = f64(v)
  998. case i32: value = f64(v)
  999. case i64: value = f64(v)
  1000. case i128: value = f64(v)
  1001. case u8: value = f64(v)
  1002. case u16: value = f64(v)
  1003. case u32: value = f64(v)
  1004. case u64: value = f64(v)
  1005. case u128: value = f64(v)
  1006. case u16le: value = f64(v)
  1007. case u32le: value = f64(v)
  1008. case u64le: value = f64(v)
  1009. case u128le:value = f64(v)
  1010. case i16le: value = f64(v)
  1011. case i32le: value = f64(v)
  1012. case i64le: value = f64(v)
  1013. case i128le:value = f64(v)
  1014. case u16be: value = f64(v)
  1015. case u32be: value = f64(v)
  1016. case u64be: value = f64(v)
  1017. case u128be:value = f64(v)
  1018. case i16be: value = f64(v)
  1019. case i32be: value = f64(v)
  1020. case i64be: value = f64(v)
  1021. case i128be:value = f64(v)
  1022. case: valid = false
  1023. }
  1024. case Type_Info_Rune:
  1025. r := a.(rune)
  1026. value = f64(i32(r))
  1027. valid = true
  1028. case Type_Info_Float:
  1029. valid = true
  1030. switch v in a {
  1031. case f16: value = f64(v)
  1032. case f32: value = f64(v)
  1033. case f64: value = (v)
  1034. case f32le: value = f64(v)
  1035. case f64le: value = f64(v)
  1036. case f32be: value = f64(v)
  1037. case f64be: value = f64(v)
  1038. case: valid = false
  1039. }
  1040. case Type_Info_Boolean:
  1041. valid = true
  1042. switch v in a {
  1043. case bool: value = f64(i32(v))
  1044. case b8: value = f64(i32(v))
  1045. case b16: value = f64(i32(v))
  1046. case b32: value = f64(i32(v))
  1047. case b64: value = f64(i32(v))
  1048. case: valid = false
  1049. }
  1050. case Type_Info_Complex:
  1051. switch v in a {
  1052. case complex64:
  1053. if imag(v) == 0 {
  1054. value = f64(real(v))
  1055. valid = true
  1056. }
  1057. case complex128:
  1058. if imag(v) == 0 {
  1059. value = real(v)
  1060. valid = true
  1061. }
  1062. }
  1063. case Type_Info_Quaternion:
  1064. switch v in a {
  1065. case quaternion128:
  1066. if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
  1067. value = f64(real(v))
  1068. valid = true
  1069. }
  1070. case quaternion256:
  1071. if imag(v) == 0 && jmag(v) == 0 && kmag(v) == 0 {
  1072. value = real(v)
  1073. valid = true
  1074. }
  1075. }
  1076. }
  1077. return
  1078. }
  1079. @(require_results)
  1080. as_string :: proc(a: any) -> (value: string, valid: bool) {
  1081. if a == nil { return }
  1082. a := a
  1083. ti := runtime.type_info_core(type_info_of(a.id))
  1084. a.id = ti.id
  1085. #partial switch info in ti.variant {
  1086. case Type_Info_String:
  1087. valid = true
  1088. switch v in a {
  1089. case string: value = v
  1090. case cstring: value = string(v)
  1091. case: valid = false
  1092. }
  1093. }
  1094. return
  1095. }
  1096. @(require_results)
  1097. relative_pointer_to_absolute :: proc(a: any) -> rawptr {
  1098. if a == nil { return nil }
  1099. a := a
  1100. ti := runtime.type_info_core(type_info_of(a.id))
  1101. a.id = ti.id
  1102. #partial switch info in ti.variant {
  1103. case Type_Info_Relative_Pointer:
  1104. return relative_pointer_to_absolute_raw(a.data, info.base_integer.id)
  1105. }
  1106. return nil
  1107. }
  1108. @(require_results)
  1109. relative_pointer_to_absolute_raw :: proc(data: rawptr, base_integer_id: typeid) -> rawptr {
  1110. _handle :: proc(ptr: ^$T) -> rawptr where intrinsics.type_is_integer(T) {
  1111. if ptr^ == 0 {
  1112. return nil
  1113. }
  1114. when intrinsics.type_is_unsigned(T) {
  1115. return rawptr(uintptr(ptr) + uintptr(ptr^))
  1116. } else {
  1117. return rawptr(uintptr(ptr) + uintptr(i64(ptr^)))
  1118. }
  1119. }
  1120. ptr_any := any{data, base_integer_id}
  1121. ptr: rawptr
  1122. switch i in &ptr_any {
  1123. case u8: ptr = _handle(&i)
  1124. case u16: ptr = _handle(&i)
  1125. case u32: ptr = _handle(&i)
  1126. case u64: ptr = _handle(&i)
  1127. case i8: ptr = _handle(&i)
  1128. case i16: ptr = _handle(&i)
  1129. case i32: ptr = _handle(&i)
  1130. case i64: ptr = _handle(&i)
  1131. case u16le: ptr = _handle(&i)
  1132. case u32le: ptr = _handle(&i)
  1133. case u64le: ptr = _handle(&i)
  1134. case i16le: ptr = _handle(&i)
  1135. case i32le: ptr = _handle(&i)
  1136. case i64le: ptr = _handle(&i)
  1137. case u16be: ptr = _handle(&i)
  1138. case u32be: ptr = _handle(&i)
  1139. case u64be: ptr = _handle(&i)
  1140. case i16be: ptr = _handle(&i)
  1141. case i32be: ptr = _handle(&i)
  1142. case i64be: ptr = _handle(&i)
  1143. }
  1144. return ptr
  1145. }
  1146. @(require_results)
  1147. as_pointer :: proc(a: any) -> (value: rawptr, valid: bool) {
  1148. if a == nil { return }
  1149. a := a
  1150. ti := runtime.type_info_core(type_info_of(a.id))
  1151. a.id = ti.id
  1152. #partial switch info in ti.variant {
  1153. case Type_Info_Pointer:
  1154. valid = true
  1155. value = a.data
  1156. case Type_Info_String:
  1157. valid = true
  1158. switch v in a {
  1159. case cstring: value = rawptr(v)
  1160. case: valid = false
  1161. }
  1162. case Type_Info_Relative_Pointer:
  1163. valid = true
  1164. value = relative_pointer_to_absolute_raw(a.data, info.base_integer.id)
  1165. }
  1166. return
  1167. }
  1168. @(require_results)
  1169. as_raw_data :: proc(a: any) -> (value: rawptr, valid: bool) {
  1170. if a == nil { return }
  1171. a := a
  1172. ti := runtime.type_info_core(type_info_of(a.id))
  1173. a.id = ti.id
  1174. #partial switch info in ti.variant {
  1175. case Type_Info_String:
  1176. valid = true
  1177. switch v in a {
  1178. case string: value = raw_data(v)
  1179. case cstring: value = rawptr(v) // just in case
  1180. case: valid = false
  1181. }
  1182. case Type_Info_Array:
  1183. valid = true
  1184. value = a.data
  1185. case Type_Info_Slice:
  1186. valid = true
  1187. value = (^runtime.Raw_Slice)(a.data).data
  1188. case Type_Info_Dynamic_Array:
  1189. valid = true
  1190. value = (^runtime.Raw_Dynamic_Array)(a.data).data
  1191. }
  1192. return
  1193. }
  1194. eq :: equal
  1195. ne :: not_equal
  1196. DEFAULT_EQUAL_MAX_RECURSION_LEVEL :: 32
  1197. @(require_results)
  1198. not_equal :: proc(a, b: any, including_indirect_array_recursion := false, recursion_level := 0) -> bool {
  1199. return !equal(a, b, including_indirect_array_recursion, recursion_level)
  1200. }
  1201. @(require_results)
  1202. equal :: proc(a, b: any, including_indirect_array_recursion := false, recursion_level := 0) -> bool {
  1203. if a == nil && b == nil {
  1204. return true
  1205. }
  1206. if a.id != b.id {
  1207. return false
  1208. }
  1209. if a.data == b.data {
  1210. return true
  1211. }
  1212. including_indirect_array_recursion := including_indirect_array_recursion
  1213. if recursion_level >= DEFAULT_EQUAL_MAX_RECURSION_LEVEL {
  1214. including_indirect_array_recursion = false
  1215. }
  1216. t := type_info_of(a.id)
  1217. if .Comparable not_in t.flags && !including_indirect_array_recursion {
  1218. return false
  1219. }
  1220. if t.size == 0 {
  1221. return true
  1222. }
  1223. if .Simple_Compare in t.flags {
  1224. return runtime.memory_compare(a.data, b.data, t.size) == 0
  1225. }
  1226. t = runtime.type_info_core(t)
  1227. switch v in t.variant {
  1228. case Type_Info_Named:
  1229. unreachable()
  1230. case Type_Info_Parameters:
  1231. unreachable()
  1232. case Type_Info_Any:
  1233. if !including_indirect_array_recursion {
  1234. return false
  1235. }
  1236. va := (^any)(a.data)
  1237. vb := (^any)(b.data)
  1238. return equal(va, vb, including_indirect_array_recursion, recursion_level+1)
  1239. case Type_Info_Map:
  1240. return false
  1241. case Type_Info_Relative_Slice:
  1242. return false
  1243. case
  1244. Type_Info_Boolean,
  1245. Type_Info_Integer,
  1246. Type_Info_Rune,
  1247. Type_Info_Float,
  1248. Type_Info_Complex,
  1249. Type_Info_Quaternion,
  1250. Type_Info_Type_Id,
  1251. Type_Info_Pointer,
  1252. Type_Info_Multi_Pointer,
  1253. Type_Info_Procedure,
  1254. Type_Info_Bit_Set,
  1255. Type_Info_Enum,
  1256. Type_Info_Simd_Vector,
  1257. Type_Info_Relative_Pointer,
  1258. Type_Info_Soa_Pointer,
  1259. Type_Info_Matrix:
  1260. return runtime.memory_compare(a.data, b.data, t.size) == 0
  1261. case Type_Info_String:
  1262. if v.is_cstring {
  1263. x := string((^cstring)(a.data)^)
  1264. y := string((^cstring)(b.data)^)
  1265. return x == y
  1266. } else {
  1267. x := (^string)(a.data)^
  1268. y := (^string)(b.data)^
  1269. return x == y
  1270. }
  1271. return true
  1272. case Type_Info_Array:
  1273. for i in 0..<v.count {
  1274. x := rawptr(uintptr(a.data) + uintptr(v.elem_size*i))
  1275. y := rawptr(uintptr(b.data) + uintptr(v.elem_size*i))
  1276. if !equal(any{x, v.elem.id}, any{y, v.elem.id}, including_indirect_array_recursion, recursion_level) {
  1277. return false
  1278. }
  1279. }
  1280. return true
  1281. case Type_Info_Enumerated_Array:
  1282. for i in 0..<v.count {
  1283. x := rawptr(uintptr(a.data) + uintptr(v.elem_size*i))
  1284. y := rawptr(uintptr(b.data) + uintptr(v.elem_size*i))
  1285. if !equal(any{x, v.elem.id}, any{y, v.elem.id}, including_indirect_array_recursion, recursion_level) {
  1286. return false
  1287. }
  1288. }
  1289. return true
  1290. case Type_Info_Struct:
  1291. if v.equal != nil {
  1292. return v.equal(a.data, b.data)
  1293. } else {
  1294. for offset, i in v.offsets {
  1295. x := rawptr(uintptr(a.data) + offset)
  1296. y := rawptr(uintptr(b.data) + offset)
  1297. id := v.types[i].id
  1298. if !equal(any{x, id}, any{y, id}, including_indirect_array_recursion, recursion_level) {
  1299. return false
  1300. }
  1301. }
  1302. return true
  1303. }
  1304. case Type_Info_Union:
  1305. if v.equal != nil {
  1306. return v.equal(a.data, b.data)
  1307. }
  1308. return false
  1309. case Type_Info_Slice:
  1310. if !including_indirect_array_recursion {
  1311. return false
  1312. }
  1313. array_a := (^runtime.Raw_Slice)(a.data)
  1314. array_b := (^runtime.Raw_Slice)(b.data)
  1315. if array_a.len != array_b.len {
  1316. return false
  1317. }
  1318. if array_a.data == array_b.data {
  1319. return true
  1320. }
  1321. for i in 0..<array_a.len {
  1322. x := rawptr(uintptr(array_a.data) + uintptr(v.elem_size*i))
  1323. y := rawptr(uintptr(array_b.data) + uintptr(v.elem_size*i))
  1324. if !equal(any{x, v.elem.id}, any{y, v.elem.id}, including_indirect_array_recursion, recursion_level+1) {
  1325. return false
  1326. }
  1327. }
  1328. return true
  1329. case Type_Info_Dynamic_Array:
  1330. if !including_indirect_array_recursion {
  1331. return false
  1332. }
  1333. array_a := (^runtime.Raw_Dynamic_Array)(a.data)
  1334. array_b := (^runtime.Raw_Dynamic_Array)(b.data)
  1335. if array_a.len != array_b.len {
  1336. return false
  1337. }
  1338. if array_a.data == array_b.data {
  1339. return true
  1340. }
  1341. if .Simple_Compare in v.elem.flags {
  1342. return runtime.memory_compare((^byte)(array_a.data), (^byte)(array_b.data), array_a.len * v.elem.size) == 0
  1343. }
  1344. for i in 0..<array_a.len {
  1345. x := rawptr(uintptr(array_a.data) + uintptr(v.elem_size*i))
  1346. y := rawptr(uintptr(array_b.data) + uintptr(v.elem_size*i))
  1347. if !equal(any{x, v.elem.id}, any{y, v.elem.id}, including_indirect_array_recursion, recursion_level+1) {
  1348. return false
  1349. }
  1350. }
  1351. return true
  1352. }
  1353. runtime.print_typeid(a.id)
  1354. runtime.print_string("\n")
  1355. return true
  1356. }