tmangle.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. discard """
  2. output: '''true
  3. true
  4. true
  5. true
  6. true
  7. true
  8. true'''
  9. """
  10. # Test not mangled:
  11. block:
  12. type T = object
  13. a: int
  14. b: cstring
  15. proc test(): bool =
  16. let obj = T(a: 11, b: "foo")
  17. {. emit: [result, " = (", obj, ".a == 11);"] .}
  18. {. emit: [result, " = ", result, " && (", obj, ".b == \"foo\");"] .}
  19. echo test()
  20. # Test indirect (fields in genAddr):
  21. block:
  22. type T = object
  23. a: int
  24. b: cstring
  25. var global = T(a: 11, b: "foo")
  26. proc test(): bool =
  27. var obj = T(a: 11, b: "foo")
  28. {. emit: [result, " = (", obj.addr[], ".a == 11);"] .}
  29. {. emit: [result, " = ", result, " && (", obj.addr[], ".b == \"foo\");"] .}
  30. {. emit: [result, " = ", result, " && (", global, ".a == 11);"] .}
  31. {. emit: [result, " = ", result, " && (", global, ".b == \"foo\");"] .}
  32. echo test()
  33. # Test addr of field:
  34. block:
  35. type T = object
  36. a: int
  37. b: cstring
  38. proc test(): bool =
  39. var obj = T(a: 11, b: "foo")
  40. result = obj.a.addr[] == 11
  41. result = result and obj.b.addr[] == "foo".cstring
  42. echo test()
  43. # Test reserved words:
  44. block:
  45. type T = ref object
  46. `if`: int
  47. `for`: int
  48. `==`: cstring
  49. `&&`: cstring
  50. proc test(): bool =
  51. var
  52. obj1 = T(`if`: 11, `for`: 22, `==`: "foo", `&&`: "bar")
  53. obj2: T
  54. new obj2 # Test behaviour for createRecordVarAux.
  55. result = obj1.`if` == 11
  56. result = result and obj1.addr[].`for` == 22
  57. result = result and obj1.`==` == "foo".cstring
  58. result = result and obj1.`&&`.addr[] == "bar".cstring
  59. result = result and obj2.`if` == 0
  60. result = result and obj2.`for` == 0
  61. result = result and obj2.`==`.isNil()
  62. result = result and obj2.`&&`.isNil()
  63. echo test()
  64. # Test codegen for fields with uppercase letters:
  65. block:
  66. type MyObj = object
  67. mField: int
  68. proc test(): bool =
  69. var a: MyObj
  70. var b = a
  71. result = b.mField == 0
  72. echo test()
  73. # Test tuples
  74. block:
  75. type T = tuple
  76. a: int
  77. b: int
  78. proc test(): bool =
  79. var a: T = (a: 1, b: 1)
  80. result = a.a == 1
  81. result = result and a.b == 1
  82. echo test()
  83. # Test importc / exportc fields:
  84. block:
  85. type T = object
  86. a: int
  87. b {. importc: "notB" .}: cstring
  88. type U = object
  89. a: int
  90. b {. exportc: "notB" .}: cstring
  91. proc test(): bool =
  92. var
  93. obj1 = T(a: 11, b: "foo")
  94. obj2 = U(a: 11, b: "foo")
  95. {. emit: [result, " = (", obj1, ".a == 11);"] .}
  96. {. emit: [result, " = ", result, " && (", obj1, ".notB == \"foo\");"] .}
  97. {. emit: [result, " = (", obj2, ".a == 11);"] .}
  98. {. emit: [result, " = ", result, " && (", obj2, ".notB == \"foo\");"] .}
  99. echo test()