chcks.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Implementation of some runtime checks.
  10. include system/indexerrors
  11. proc raiseRangeError(val: BiggestInt) {.compilerproc, noinline.} =
  12. when hostOS == "standalone":
  13. sysFatal(RangeDefect, "value out of range")
  14. else:
  15. sysFatal(RangeDefect, "value out of range: ", $val)
  16. proc raiseIndexError3(i, a, b: int) {.compilerproc, noinline.} =
  17. sysFatal(IndexDefect, formatErrorIndexBound(i, a, b))
  18. proc raiseIndexError2(i, n: int) {.compilerproc, noinline.} =
  19. sysFatal(IndexDefect, formatErrorIndexBound(i, n))
  20. proc raiseIndexError() {.compilerproc, noinline.} =
  21. sysFatal(IndexDefect, "index out of bounds")
  22. proc raiseFieldError(f: string) {.compilerproc, noinline.} =
  23. sysFatal(FieldDefect, f)
  24. proc raiseRangeErrorI(i, a, b: BiggestInt) {.compilerproc, noinline.} =
  25. sysFatal(RangeDefect, "value out of range: " & $i & " notin " & $a & " .. " & $b)
  26. proc raiseRangeErrorF(i, a, b: float) {.compilerproc, noinline.} =
  27. sysFatal(RangeDefect, "value out of range: " & $i & " notin " & $a & " .. " & $b)
  28. proc raiseRangeErrorU(i, a, b: uint64) {.compilerproc, noinline.} =
  29. # todo: better error reporting
  30. sysFatal(RangeDefect, "value out of range")
  31. proc raiseRangeErrorNoArgs() {.compilerproc, noinline.} =
  32. sysFatal(RangeDefect, "value out of range")
  33. proc raiseObjectConversionError() {.compilerproc, noinline.} =
  34. sysFatal(ObjectConversionDefect, "invalid object conversion")
  35. proc chckIndx(i, a, b: int): int =
  36. if i >= a and i <= b:
  37. return i
  38. else:
  39. raiseIndexError3(i, a, b)
  40. proc chckRange(i, a, b: int): int =
  41. if i >= a and i <= b:
  42. return i
  43. else:
  44. raiseRangeError(i)
  45. proc chckRange64(i, a, b: int64): int64 {.compilerproc.} =
  46. if i >= a and i <= b:
  47. return i
  48. else:
  49. raiseRangeError(i)
  50. proc chckRangeU(i, a, b: uint64): uint64 {.compilerproc.} =
  51. if i >= a and i <= b:
  52. return i
  53. else:
  54. sysFatal(RangeDefect, "value out of range")
  55. proc chckRangeF(x, a, b: float): float =
  56. if x >= a and x <= b:
  57. return x
  58. else:
  59. when hostOS == "standalone":
  60. sysFatal(RangeDefect, "value out of range")
  61. else:
  62. sysFatal(RangeDefect, "value out of range: ", $x)
  63. proc chckNil(p: pointer) =
  64. if p == nil:
  65. sysFatal(NilAccessDefect, "attempt to write to a nil address")
  66. proc chckNilDisp(p: pointer) {.compilerproc.} =
  67. if p == nil:
  68. sysFatal(NilAccessDefect, "cannot dispatch; dispatcher is nil")
  69. when not defined(nimV2):
  70. proc chckObj(obj, subclass: PNimType) {.compilerproc.} =
  71. # checks if obj is of type subclass:
  72. var x = obj
  73. if x == subclass: return # optimized fast path
  74. while x != subclass:
  75. if x == nil:
  76. sysFatal(ObjectConversionDefect, "invalid object conversion")
  77. x = x.base
  78. proc chckObjAsgn(a, b: PNimType) {.compilerproc, inline.} =
  79. if a != b:
  80. sysFatal(ObjectAssignmentDefect, "invalid object assignment")
  81. type ObjCheckCache = array[0..1, PNimType]
  82. proc isObjSlowPath(obj, subclass: PNimType;
  83. cache: var ObjCheckCache): bool {.noinline.} =
  84. # checks if obj is of type subclass:
  85. var x = obj.base
  86. while x != subclass:
  87. if x == nil:
  88. cache[0] = obj
  89. return false
  90. x = x.base
  91. cache[1] = obj
  92. return true
  93. proc isObjWithCache(obj, subclass: PNimType;
  94. cache: var ObjCheckCache): bool {.compilerproc, inline.} =
  95. if obj == subclass: return true
  96. if obj.base == subclass: return true
  97. if cache[0] == obj: return false
  98. if cache[1] == obj: return true
  99. return isObjSlowPath(obj, subclass, cache)
  100. proc isObj(obj, subclass: PNimType): bool {.compilerproc.} =
  101. # checks if obj is of type subclass:
  102. var x = obj
  103. if x == subclass: return true # optimized fast path
  104. while x != subclass:
  105. if x == nil: return false
  106. x = x.base
  107. return true
  108. when defined(nimV2):
  109. proc raiseObjectCaseTransition() {.compilerproc.} =
  110. sysFatal(FieldDefect, "assignment to discriminant changes object branch")