From 4c8a02165eb2d419d94cc82c3bdae0bf23a4a495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Tue, 11 Jun 2019 16:49:56 +0200 Subject: [PATCH] [refactoring] refactor the compiler and stdlib to deprecation warnings (#11419) (cherry picked from commit c7e1c665a1d399272bef35395a6c364b9f98d64a) --- compiler/bitsets.nim | 34 ++++++++++++++++------------ compiler/ccgexprs.nim | 24 ++++++++++++-------- compiler/lexer.nim | 12 +++++----- lib/pure/collections/sets.nim | 4 ++-- lib/system.nim | 16 ++++++------- lib/system/repr.nim | 4 ++-- tests/errmsgs/tproper_stacktrace.nim | 2 +- 7 files changed, 52 insertions(+), 44 deletions(-) diff --git a/compiler/bitsets.nim b/compiler/bitsets.nim index d03a5915ed..01f8ca95e8 100644 --- a/compiler/bitsets.nim +++ b/compiler/bitsets.nim @@ -11,12 +11,17 @@ # the code here should be reused in the Nim standard library 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 # however - 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 bitSetUnion*(x: var TBitSet, y: TBitSet) @@ -32,17 +37,16 @@ proc bitSetCard*(x: TBitSet): BiggestInt # implementation proc bitSetIn(x: TBitSet, e: BiggestInt): bool = - result = (x[int(e div ElemSize)] and toU8(int(1 shl (e mod ElemSize)))) != - toU8(0) + result = (x[int(e.divElemSize)] and (One shl e.modElemSize)) != Zero proc bitSetIncl(x: var TBitSet, elem: BiggestInt) = assert(elem >= 0) - x[int(elem div ElemSize)] = x[int(elem div ElemSize)] or - toU8(int(1 shl (elem mod ElemSize))) + x[int(elem.divElemSize)] = x[int(elem.divElemSize)] or + (One shl elem.modElemSize) proc bitSetExcl(x: var TBitSet, elem: BiggestInt) = - x[int(elem div ElemSize)] = x[int(elem div ElemSize)] and - not toU8(int(1 shl (elem mod ElemSize))) + x[int(elem.divElemSize)] = x[int(elem.divElemSize)] and + not(One shl elem.modElemSize) proc bitSetInit(b: var TBitSet, length: int) = newSeq(b, length) @@ -67,13 +71,13 @@ proc bitSetEquals(x, y: TBitSet): bool = proc bitSetContains(x, y: TBitSet): bool = 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 result = true # Number of set bits for all values of int8 -const populationCount: array[low(int8)..high(int8), int8] = block: - var arr: array[low(int8)..high(int8), int8] +const populationCount: array[uint8, uint8] = block: + var arr: array[uint8, uint8] proc countSetBits(x: uint8): uint8 = return @@ -87,11 +91,11 @@ const populationCount: array[low(int8)..high(int8), int8] = block: ((x and 0b10000000'u8) shr 7) - for it in low(int8)..high(int8): - arr[it] = cast[int8](countSetBits(cast[uint8](it))) + for it in low(uint8)..high(uint8): + arr[it] = countSetBits(cast[uint8](it)) arr proc bitSetCard(x: TBitSet): BiggestInt = for it in x: - result.inc populationCount[it] + result.inc int(populationCount[it]) diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index a1a9ffe418..bf39788fa2 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -93,26 +93,30 @@ proc genLiteral(p: BProc, n: PNode, ty: PType): Rope = proc genLiteral(p: BProc, n: PNode): Rope = result = genLiteral(p, n, n.typ) -proc bitSetToWord(s: TBitSet, size: int): BiggestInt = +proc bitSetToWord(s: TBitSet, size: int): BiggestUInt = result = 0 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 = if size > 8: - result = "{$n" % [] + var res = "{\n" 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: - # not last iteration? - if (i + 1) mod 8 == 0: - addf(result, "0x$1,$n", [rope(toHex(ze64(cs[i]), 2))]) + # not last iteration + if i mod 8 == 7: + res.add ",\n" else: - addf(result, "0x$1, ", [rope(toHex(ze64(cs[i]), 2))]) + res.add ", " else: - addf(result, "0x$1}$n", [rope(toHex(ze64(cs[i]), 2))]) + res.add "}\n" + + result = rope(res) else: - result = intLiteral(bitSetToWord(cs, size)) - # result := rope('0x' + ToHex(bitSetToWord(cs, size), size * 2)) + result = intLiteral(cast[BiggestInt](bitSetToWord(cs, size))) proc genSetNode(p: BProc, n: PNode): Rope = var cs: TBitSet diff --git a/compiler/lexer.nim b/compiler/lexer.nim index 7533fb8305..421a83c19d 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -534,13 +534,13 @@ proc getNumber(L: var TLexer, result: var TToken) = case result.tokType of tkIntLit, tkInt64Lit: result.iNumber = xi - of tkInt8Lit: result.iNumber = BiggestInt(int8(toU8(int(xi)))) - of tkInt16Lit: result.iNumber = BiggestInt(int16(toU16(int(xi)))) - of tkInt32Lit: result.iNumber = BiggestInt(int32(toU32(int64(xi)))) + of tkInt8Lit: result.iNumber = ashr(xi shl 56, 56) + of tkInt16Lit: result.iNumber = ashr(xi shl 48, 48) + of tkInt32Lit: result.iNumber = ashr(xi shl 32, 32) of tkUIntLit, tkUInt64Lit: result.iNumber = xi - of tkUInt8Lit: result.iNumber = BiggestInt(cast[uint8](toU8(int(xi)))) - of tkUInt16Lit: result.iNumber = BiggestInt(cast[uint16](toU16(int(xi)))) - of tkUInt32Lit: result.iNumber = BiggestInt(cast[uint32](toU32(int64(xi)))) + of tkUInt8Lit: result.iNumber = xi and 0xff + of tkUInt16Lit: result.iNumber = xi and 0xffff + of tkUInt32Lit: result.iNumber = xi and 0xffffffff of tkFloat32Lit: result.fNumber = (cast[PFloat32](addr(xi)))[] # note: this code is endian neutral! diff --git a/lib/pure/collections/sets.nim b/lib/pure/collections/sets.nim index 14f7dad94a..c4991ed3db 100644 --- a/lib/pure/collections/sets.nim +++ b/lib/pure/collections/sets.nim @@ -61,7 +61,7 @@ when not defined(nimhygiene): type KeyValuePair[A] = tuple[hcode: Hash, key: A] KeyValuePairSeq[A] = seq[KeyValuePair[A]] - HashSet* {.myShallow.} [A] = object ## \ + HashSet*[A] {.myShallow.} = object ## \ ## A generic hash set. ## ## Use `init proc <#init,HashSet[A],int>`_ or `initHashSet proc <#initHashSet,int>`_ @@ -73,7 +73,7 @@ type OrderedKeyValuePair[A] = tuple[ hcode: Hash, next: int, key: A] OrderedKeyValuePairSeq[A] = seq[OrderedKeyValuePair[A]] - OrderedSet* {.myShallow.} [A] = object ## \ + OrderedSet*[A] {.myShallow.} = object ## \ ## A generic hash set that remembers insertion order. ## ## Use `init proc <#init,OrderedSet[A],int>`_ or `initOrderedSet proc diff --git a/lib/system.nim b/lib/system.nim index d20beabf0e..5a500f147c 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -3646,21 +3646,21 @@ when not defined(JS): #and not defined(nimscript): const GenericSeqSize = (2 * sizeof(int)) 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") - var d: int - var a = cast[ByteAddress](aa) + var d: uint + var a = cast[uint](aa) case n.typ.size - of 1: d = ze(cast[ptr int8](a +% n.offset)[]) - of 2: d = ze(cast[ptr int16](a +% n.offset)[]) - of 4: d = int(cast[ptr int32](a +% n.offset)[]) - of 8: d = int(cast[ptr int64](a +% n.offset)[]) + of 1: d = uint(cast[ptr uint8](a + uint(n.offset))[]) + of 2: d = uint(cast[ptr uint16](a + uint(n.offset))[]) + of 4: d = uint(cast[ptr uint32](a + uint(n.offset))[]) + of 8: d = uint(cast[ptr uint64](a + uint(n.offset))[]) else: sysAssert(false, "getDiscriminant: invalid n.typ.size") return d proc selectBranch(aa: pointer, n: ptr TNimNode): ptr TNimNode = var discr = getDiscriminant(aa, n) - if discr <% n.len: + if discr < cast[uint](n.len): result = n.sons[discr] if result == nil: result = n.sons[n.len] # n.sons[n.len] contains the ``else`` part (but may be nil) diff --git a/lib/system/repr.nim b/lib/system/repr.nim index 67e625a8e1..f276853cd0 100644 --- a/lib/system/repr.nim +++ b/lib/system/repr.nim @@ -78,7 +78,7 @@ proc reprEnum(e: int, typ: PNimType): string {.compilerRtl.} = result = $e & " (invalid data!)" type - PByteArray = ptr array[0xffff, int8] + PByteArray = ptr array[0xffff, byte] proc addSetElem(result: var string, elem: int, typ: PNimType) {.benign.} = case typ.kind @@ -104,7 +104,7 @@ proc reprSetAux(result: var string, p: pointer, typ: PNimType) = else: var a = cast[PByteArray](p) 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, ", " addSetElem(result, i+typ.node.len, typ.base) inc(elemCounter) diff --git a/tests/errmsgs/tproper_stacktrace.nim b/tests/errmsgs/tproper_stacktrace.nim index c0090a5957..c7dfbaf2a7 100644 --- a/tests/errmsgs/tproper_stacktrace.nim +++ b/tests/errmsgs/tproper_stacktrace.nim @@ -22,7 +22,7 @@ proc matchStackTrace(actualEntries: openarray[StackTraceEntry], expected: string var line: int if not scanf(l, "$s$w.nim($i) $w", filename, line, procname): 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: checkEqual(actualEntries[i].line, line, "line number") checkEqual($actualEntries[i].procname, procname, "proc name")