chcks.nim 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 raiseIndexError4(l1, h1, h2: int) {.compilerproc, noinline.} =
  17. sysFatal(IndexDefect, "index out of bounds: " & $l1 & ".." & $h1 & " notin 0.." & $(h2 - 1))
  18. proc raiseIndexError3(i, a, b: int) {.compilerproc, noinline.} =
  19. sysFatal(IndexDefect, formatErrorIndexBound(i, a, b))
  20. proc raiseIndexError2(i, n: int) {.compilerproc, noinline.} =
  21. sysFatal(IndexDefect, formatErrorIndexBound(i, n))
  22. proc raiseIndexError() {.compilerproc, noinline.} =
  23. sysFatal(IndexDefect, "index out of bounds")
  24. proc raiseFieldError(f: string) {.compilerproc, noinline.} =
  25. ## remove after bootstrap > 1.5.1
  26. sysFatal(FieldDefect, f)
  27. when defined(gcdestructors):
  28. proc raiseFieldError2(f: string, discVal: int) {.compilerproc, noinline.} =
  29. ## raised when field is inaccessible given runtime value of discriminant
  30. sysFatal(FieldError, f & $discVal & "'")
  31. else:
  32. proc raiseFieldError2(f: string, discVal: string) {.compilerproc, noinline.} =
  33. ## raised when field is inaccessible given runtime value of discriminant
  34. sysFatal(FieldError, formatFieldDefect(f, discVal))
  35. proc raiseRangeErrorI(i, a, b: BiggestInt) {.compilerproc, noinline.} =
  36. when defined(standalone):
  37. sysFatal(RangeDefect, "value out of range")
  38. else:
  39. sysFatal(RangeDefect, "value out of range: " & $i & " notin " & $a & " .. " & $b)
  40. proc raiseRangeErrorF(i, a, b: float) {.compilerproc, noinline.} =
  41. when defined(standalone):
  42. sysFatal(RangeDefect, "value out of range")
  43. else:
  44. sysFatal(RangeDefect, "value out of range: " & $i & " notin " & $a & " .. " & $b)
  45. proc raiseRangeErrorU(i, a, b: uint64) {.compilerproc, noinline.} =
  46. # todo: better error reporting
  47. sysFatal(RangeDefect, "value out of range")
  48. proc raiseRangeErrorNoArgs() {.compilerproc, noinline.} =
  49. sysFatal(RangeDefect, "value out of range")
  50. proc raiseObjectConversionError() {.compilerproc, noinline.} =
  51. sysFatal(ObjectConversionDefect, "invalid object conversion")
  52. proc chckIndx(i, a, b: int): int =
  53. if i >= a and i <= b:
  54. return i
  55. else:
  56. raiseIndexError3(i, a, b)
  57. proc chckRange(i, a, b: int): int =
  58. if i >= a and i <= b:
  59. return i
  60. else:
  61. raiseRangeError(i)
  62. proc chckRange64(i, a, b: int64): int64 {.compilerproc.} =
  63. if i >= a and i <= b:
  64. return i
  65. else:
  66. raiseRangeError(i)
  67. proc chckRangeU(i, a, b: uint64): uint64 {.compilerproc.} =
  68. if i >= a and i <= b:
  69. return i
  70. else:
  71. sysFatal(RangeDefect, "value out of range")
  72. proc chckRangeF(x, a, b: float): float =
  73. if x >= a and x <= b:
  74. return x
  75. else:
  76. when hostOS == "standalone":
  77. sysFatal(RangeDefect, "value out of range")
  78. else:
  79. sysFatal(RangeDefect, "value out of range: ", $x)
  80. proc chckNil(p: pointer) =
  81. if p == nil:
  82. sysFatal(NilAccessDefect, "attempt to write to a nil address")
  83. proc chckNilDisp(p: pointer) {.compilerproc.} =
  84. if p == nil:
  85. sysFatal(NilAccessDefect, "cannot dispatch; dispatcher is nil")
  86. when not defined(nimV2):
  87. proc chckObj(obj, subclass: PNimType) {.compilerproc.} =
  88. # checks if obj is of type subclass:
  89. var x = obj
  90. if x == subclass: return # optimized fast path
  91. while x != subclass:
  92. if x == nil:
  93. sysFatal(ObjectConversionDefect, "invalid object conversion")
  94. x = x.base
  95. proc chckObjAsgn(a, b: PNimType) {.compilerproc, inline.} =
  96. if a != b:
  97. sysFatal(ObjectAssignmentDefect, "invalid object assignment")
  98. type ObjCheckCache = array[0..1, PNimType]
  99. proc isObjSlowPath(obj, subclass: PNimType;
  100. cache: var ObjCheckCache): bool {.noinline.} =
  101. # checks if obj is of type subclass:
  102. var x = obj.base
  103. while x != subclass:
  104. if x == nil:
  105. cache[0] = obj
  106. return false
  107. x = x.base
  108. cache[1] = obj
  109. return true
  110. proc isObjWithCache(obj, subclass: PNimType;
  111. cache: var ObjCheckCache): bool {.compilerproc, inline.} =
  112. if obj == subclass: return true
  113. if obj.base == subclass: return true
  114. if cache[0] == obj: return false
  115. if cache[1] == obj: return true
  116. return isObjSlowPath(obj, subclass, cache)
  117. proc isObj(obj, subclass: PNimType): bool {.compilerproc.} =
  118. # checks if obj is of type subclass:
  119. var x = obj
  120. if x == subclass: return true # optimized fast path
  121. while x != subclass:
  122. if x == nil: return false
  123. x = x.base
  124. return true
  125. when defined(nimV2):
  126. proc raiseObjectCaseTransition() {.compilerproc.} =
  127. sysFatal(FieldDefect, "assignment to discriminant changes object branch")