[refactoring] refactor the compiler and stdlib to deprecation warnings (#11419)

This commit is contained in:
Arne Döring 2019-06-11 16:49:56 +02:00 • committed by Andreas Rumpf
commit c7e1c665a1
7 changed files with 52 additions and 44 deletions

View file

@ -11,12 +11,17 @@
# the code here should be reused in the Nim standard library # the code here should be reused in the Nim standard library
type type
TBitSet* = seq[int8] # we use byte here to avoid issues with ElemType = byte
TBitSet* = seq[ElemType] # we use byte here to avoid issues with
# cross-compiling; uint would be more efficient # cross-compiling; uint would be more efficient
# however # however
const const
ElemSize* = sizeof(int8) * 8 ElemSize* = 8
One = ElemType(1)
Zero = ElemType(0)
template modElemSize(arg: untyped): untyped = arg and 7
template divElemSize(arg: untyped): untyped = arg shr 3
proc bitSetInit*(b: var TBitSet, length: int) proc bitSetInit*(b: var TBitSet, length: int)
proc bitSetUnion*(x: var TBitSet, y: TBitSet) proc bitSetUnion*(x: var TBitSet, y: TBitSet)
@ -32,17 +37,16 @@ proc bitSetCard*(x: TBitSet): BiggestInt
# implementation # implementation
proc bitSetIn(x: TBitSet, e: BiggestInt): bool = proc bitSetIn(x: TBitSet, e: BiggestInt): bool =
result = (x[int(e div ElemSize)] and toU8(int(1 shl (e mod ElemSize)))) != result = (x[int(e.divElemSize)] and (One shl e.modElemSize)) != Zero
toU8(0)
proc bitSetIncl(x: var TBitSet, elem: BiggestInt) = proc bitSetIncl(x: var TBitSet, elem: BiggestInt) =
assert(elem >= 0) assert(elem >= 0)
x[int(elem div ElemSize)] = x[int(elem div ElemSize)] or x[int(elem.divElemSize)] = x[int(elem.divElemSize)] or
toU8(int(1 shl (elem mod ElemSize))) (One shl elem.modElemSize)
proc bitSetExcl(x: var TBitSet, elem: BiggestInt) = proc bitSetExcl(x: var TBitSet, elem: BiggestInt) =
x[int(elem div ElemSize)] = x[int(elem div ElemSize)] and x[int(elem.divElemSize)] = x[int(elem.divElemSize)] and
not toU8(int(1 shl (elem mod ElemSize))) not(One shl elem.modElemSize)
proc bitSetInit(b: var TBitSet, length: int) = proc bitSetInit(b: var TBitSet, length: int) =
newSeq(b, length) newSeq(b, length)
@ -67,13 +71,13 @@ proc bitSetEquals(x, y: TBitSet): bool =
proc bitSetContains(x, y: TBitSet): bool = proc bitSetContains(x, y: TBitSet): bool =
for i in 0 .. high(x): for i in 0 .. high(x):
if (x[i] and not y[i]) != int8(0): if (x[i] and not y[i]) != Zero:
return false return false
result = true result = true
# Number of set bits for all values of int8 # Number of set bits for all values of int8
const populationCount: array[low(int8)..high(int8), int8] = block: const populationCount: array[uint8, uint8] = block:
var arr: array[low(int8)..high(int8), int8] var arr: array[uint8, uint8]
proc countSetBits(x: uint8): uint8 = proc countSetBits(x: uint8): uint8 =
return return
@ -87,11 +91,11 @@ const populationCount: array[low(int8)..high(int8), int8] = block:
((x and 0b10000000'u8) shr 7) ((x and 0b10000000'u8) shr 7)
for it in low(int8)..high(int8): for it in low(uint8)..high(uint8):
arr[it] = cast[int8](countSetBits(cast[uint8](it))) arr[it] = countSetBits(cast[uint8](it))
arr arr
proc bitSetCard(x: TBitSet): BiggestInt = proc bitSetCard(x: TBitSet): BiggestInt =
for it in x: for it in x:
result.inc populationCount[it] result.inc int(populationCount[it])

View file

@ -93,26 +93,30 @@ proc genLiteral(p: BProc, n: PNode, ty: PType): Rope =
proc genLiteral(p: BProc, n: PNode): Rope = proc genLiteral(p: BProc, n: PNode): Rope =
result = genLiteral(p, n, n.typ) result = genLiteral(p, n, n.typ)
proc bitSetToWord(s: TBitSet, size: int): BiggestInt = proc bitSetToWord(s: TBitSet, size: int): BiggestUInt =
result = 0 result = 0
for j in 0 ..< size: for j in 0 ..< size:
if j < len(s): result = result or (ze64(s[j]) shl (j * 8)) if j < len(s): result = result or (BiggestUInt(s[j]) shl (j * 8))
proc genRawSetData(cs: TBitSet, size: int): Rope = proc genRawSetData(cs: TBitSet, size: int): Rope =
if size > 8: if size > 8:
result = "{$n" % [] var res = "{\n"
for i in 0 ..< size: for i in 0 ..< size:
res.add "0x"
res.add "0123456789abcdef"[cs[i] div 16]
res.add "0123456789abcdef"[cs[i] mod 16]
if i < size - 1: if i < size - 1:
# not last iteration? # not last iteration
if (i + 1) mod 8 == 0: if i mod 8 == 7:
addf(result, "0x$1,$n", [rope(toHex(ze64(cs[i]), 2))]) res.add ",\n"
else: else:
addf(result, "0x$1, ", [rope(toHex(ze64(cs[i]), 2))]) res.add ", "
else: else:
addf(result, "0x$1}$n", [rope(toHex(ze64(cs[i]), 2))]) res.add "}\n"
result = rope(res)
else: else:
result = intLiteral(bitSetToWord(cs, size)) result = intLiteral(cast[BiggestInt](bitSetToWord(cs, size)))
# result := rope('0x' + ToHex(bitSetToWord(cs, size), size * 2))
proc genSetNode(p: BProc, n: PNode): Rope = proc genSetNode(p: BProc, n: PNode): Rope =
var cs: TBitSet var cs: TBitSet

View file

@ -534,13 +534,13 @@ proc getNumber(L: var TLexer, result: var TToken) =
case result.tokType case result.tokType
of tkIntLit, tkInt64Lit: result.iNumber = xi of tkIntLit, tkInt64Lit: result.iNumber = xi
of tkInt8Lit: result.iNumber = BiggestInt(int8(toU8(int(xi)))) of tkInt8Lit: result.iNumber = ashr(xi shl 56, 56)
of tkInt16Lit: result.iNumber = BiggestInt(int16(toU16(int(xi)))) of tkInt16Lit: result.iNumber = ashr(xi shl 48, 48)
of tkInt32Lit: result.iNumber = BiggestInt(int32(toU32(int64(xi)))) of tkInt32Lit: result.iNumber = ashr(xi shl 32, 32)
of tkUIntLit, tkUInt64Lit: result.iNumber = xi of tkUIntLit, tkUInt64Lit: result.iNumber = xi
of tkUInt8Lit: result.iNumber = BiggestInt(cast[uint8](toU8(int(xi)))) of tkUInt8Lit: result.iNumber = xi and 0xff
of tkUInt16Lit: result.iNumber = BiggestInt(cast[uint16](toU16(int(xi)))) of tkUInt16Lit: result.iNumber = xi and 0xffff
of tkUInt32Lit: result.iNumber = BiggestInt(cast[uint32](toU32(int64(xi)))) of tkUInt32Lit: result.iNumber = xi and 0xffffffff
of tkFloat32Lit: of tkFloat32Lit:
result.fNumber = (cast[PFloat32](addr(xi)))[] result.fNumber = (cast[PFloat32](addr(xi)))[]
# note: this code is endian neutral! # note: this code is endian neutral!

View file

@ -61,7 +61,7 @@ when not defined(nimhygiene):
type type
KeyValuePair[A] = tuple[hcode: Hash, key: A] KeyValuePair[A] = tuple[hcode: Hash, key: A]
KeyValuePairSeq[A] = seq[KeyValuePair[A]] KeyValuePairSeq[A] = seq[KeyValuePair[A]]
HashSet* {.myShallow.} [A] = object ## \ HashSet*[A] {.myShallow.} = object ## \
## A generic hash set. ## A generic hash set.
## ##
## Use `init proc <#init,HashSet[A],int>`_ or `initHashSet proc <#initHashSet,int>`_ ## Use `init proc <#init,HashSet[A],int>`_ or `initHashSet proc <#initHashSet,int>`_
@ -73,7 +73,7 @@ type
OrderedKeyValuePair[A] = tuple[ OrderedKeyValuePair[A] = tuple[
hcode: Hash, next: int, key: A] hcode: Hash, next: int, key: A]
OrderedKeyValuePairSeq[A] = seq[OrderedKeyValuePair[A]] OrderedKeyValuePairSeq[A] = seq[OrderedKeyValuePair[A]]
OrderedSet* {.myShallow.} [A] = object ## \ OrderedSet*[A] {.myShallow.} = object ## \
## A generic hash set that remembers insertion order. ## A generic hash set that remembers insertion order.
## ##
## Use `init proc <#init,OrderedSet[A],int>`_ or `initOrderedSet proc ## Use `init proc <#init,OrderedSet[A],int>`_ or `initOrderedSet proc

View file

@ -3646,21 +3646,21 @@ when not defined(JS): #and not defined(nimscript):
const GenericSeqSize = (2 * sizeof(int)) const GenericSeqSize = (2 * sizeof(int))
when not defined(nimV2): when not defined(nimV2):
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int = proc getDiscriminant(aa: pointer, n: ptr TNimNode): uint =
sysAssert(n.kind == nkCase, "getDiscriminant: node != nkCase") sysAssert(n.kind == nkCase, "getDiscriminant: node != nkCase")
var d: int var d: uint
var a = cast[ByteAddress](aa) var a = cast[uint](aa)
case n.typ.size case n.typ.size
of 1: d = ze(cast[ptr int8](a +% n.offset)[]) of 1: d = uint(cast[ptr uint8](a + uint(n.offset))[])
of 2: d = ze(cast[ptr int16](a +% n.offset)[]) of 2: d = uint(cast[ptr uint16](a + uint(n.offset))[])
of 4: d = int(cast[ptr int32](a +% n.offset)[]) of 4: d = uint(cast[ptr uint32](a + uint(n.offset))[])
of 8: d = int(cast[ptr int64](a +% n.offset)[]) of 8: d = uint(cast[ptr uint64](a + uint(n.offset))[])
else: sysAssert(false, "getDiscriminant: invalid n.typ.size") else: sysAssert(false, "getDiscriminant: invalid n.typ.size")
return d return d
proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode = proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode =
var discr = getDiscriminant(aa, n) var discr = getDiscriminant(aa, n)
if discr <% n.len: if discr < cast[uint](n.len):
result = n.sons[discr] result = n.sons[discr]
if result == nil: result = n.sons[n.len] if result == nil: result = n.sons[n.len]
# n.sons[n.len] contains the ``else`` part (but may be nil) # n.sons[n.len] contains the ``else`` part (but may be nil)

View file

@ -78,7 +78,7 @@ proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} =
result = $e & " (invalid data!)" result = $e & " (invalid data!)"
type type
PByteArray = ptr array[0xffff, int8] PByteArray = ptr array[0xffff, byte]
proc addSetElem(result: var string, elem: int, typ: PNimType) {.benign.} = proc addSetElem(result: var string, elem: int, typ: PNimType) {.benign.} =
case typ.kind case typ.kind
@ -104,7 +104,7 @@ proc reprSetAux(result: var string, p: pointer, typ: PNimType) =
else: else:
var a = cast[PByteArray](p) var a = cast[PByteArray](p)
for i in 0 .. typ.size*8-1: for i in 0 .. typ.size*8-1:
if (ze(a[i div 8]) and (1 shl (i mod 8))) != 0: if (uint(a[i shr 3]) and (1'u shl (i and 7))) != 0:
if elemCounter > 0: add result, ", " if elemCounter > 0: add result, ", "
addSetElem(result, i+typ.node.len, typ.base) addSetElem(result, i+typ.node.len, typ.base)
inc(elemCounter) inc(elemCounter)

View file

@ -22,7 +22,7 @@ proc matchStackTrace(actualEntries: openarray[StackTraceEntry], expected: string
var line: int var line: int
if not scanf(l, "$s$w.nim($i) $w", filename, line, procname): if not scanf(l, "$s$w.nim($i) $w", filename, line, procname):
doAssert(false, "Wrong expected stack trace") doAssert(false, "Wrong expected stack trace")
checkEqual($actualEntries[i].filename, filename & ".nim", "file name") checkEqual(actualEntries[i].filename.`$`.split('/')[^1], filename & ".nim", "file name")
if line != 0: if line != 0:
checkEqual(actualEntries[i].line, line, "line number") checkEqual(actualEntries[i].line, line, "line number")
checkEqual($actualEntries[i].procname, procname, "proc name") checkEqual($actualEntries[i].procname, procname, "proc name")