chcks.nim 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. import system/helpers2
  11. proc raiseRangeError(val: BiggestInt) {.compilerproc, noinline.} =
  12. when hostOS == "standalone":
  13. sysFatal(RangeError, "value out of range")
  14. else:
  15. sysFatal(RangeError, "value out of range: ", $val)
  16. proc raiseIndexError3(i, a, b: int) {.compilerproc, noinline.} =
  17. sysFatal(IndexError, formatErrorIndexBound(i, a, b))
  18. proc raiseIndexError2(i, n: int) {.compilerproc, noinline.} =
  19. sysFatal(IndexError, formatErrorIndexBound(i, n))
  20. proc raiseIndexError() {.compilerproc, noinline.} =
  21. sysFatal(IndexError, "index out of bounds")
  22. proc raiseFieldError(f: string) {.compilerproc, noinline.} =
  23. sysFatal(FieldError, f, " is not accessible")
  24. proc chckIndx(i, a, b: int): int =
  25. if i >= a and i <= b:
  26. return i
  27. else:
  28. raiseIndexError3(i, a, b)
  29. proc chckRange(i, a, b: int): int =
  30. if i >= a and i <= b:
  31. return i
  32. else:
  33. raiseRangeError(i)
  34. proc chckRange64(i, a, b: int64): int64 {.compilerproc.} =
  35. if i >= a and i <= b:
  36. return i
  37. else:
  38. raiseRangeError(i)
  39. proc chckRangeF(x, a, b: float): float =
  40. if x >= a and x <= b:
  41. return x
  42. else:
  43. when hostOS == "standalone":
  44. sysFatal(RangeError, "value out of range")
  45. else:
  46. sysFatal(RangeError, "value out of range: ", $x)
  47. proc chckNil(p: pointer) =
  48. if p == nil:
  49. sysFatal(NilAccessError, "attempt to write to a nil address")
  50. when defined(nimNewRuntime):
  51. proc chckMove(b: bool) {.compilerproc.} =
  52. if not b:
  53. sysFatal(MoveError, "attempt to access an object that was moved")
  54. proc chckNilDisp(p: pointer) {.compilerproc.} =
  55. if p == nil:
  56. sysFatal(NilAccessError, "cannot dispatch; dispatcher is nil")
  57. proc chckObj(obj, subclass: PNimType) {.compilerproc.} =
  58. # checks if obj is of type subclass:
  59. var x = obj
  60. if x == subclass: return # optimized fast path
  61. while x != subclass:
  62. if x == nil:
  63. sysFatal(ObjectConversionError, "invalid object conversion")
  64. x = x.base
  65. proc chckObjAsgn(a, b: PNimType) {.compilerproc, inline.} =
  66. if a != b:
  67. sysFatal(ObjectAssignmentError, "invalid object assignment")
  68. type ObjCheckCache = array[0..1, PNimType]
  69. proc isObjSlowPath(obj, subclass: PNimType;
  70. cache: var ObjCheckCache): bool {.noinline.} =
  71. # checks if obj is of type subclass:
  72. var x = obj.base
  73. while x != subclass:
  74. if x == nil:
  75. cache[0] = obj
  76. return false
  77. x = x.base
  78. cache[1] = obj
  79. return true
  80. proc isObjWithCache(obj, subclass: PNimType;
  81. cache: var ObjCheckCache): bool {.compilerProc, inline.} =
  82. if obj == subclass: return true
  83. if obj.base == subclass: return true
  84. if cache[0] == obj: return false
  85. if cache[1] == obj: return true
  86. return isObjSlowPath(obj, subclass, cache)
  87. proc isObj(obj, subclass: PNimType): bool {.compilerproc.} =
  88. # checks if obj is of type subclass:
  89. var x = obj
  90. if x == subclass: return true # optimized fast path
  91. while x != subclass:
  92. if x == nil: return false
  93. x = x.base
  94. return true