reflect.odin 34 KB

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