chcks.nim 4.9 KB

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