chcks.nim 4.7 KB

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