chcks.nim 5.1 KB

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