more uint related fixes

This commit is contained in:
Zahary Karadjov 2012-06-14 17:33:00 +03:00
commit b11fe5d0b4
12 changed files with 151 additions and 139 deletions

View file

@ -329,7 +329,8 @@ type
tfEnumHasHoles, # enum cannot be mapped into a range tfEnumHasHoles, # enum cannot be mapped into a range
tfShallow, # type can be shallow copied on assignment tfShallow, # type can be shallow copied on assignment
tfThread, # proc type is marked as ``thread`` tfThread, # proc type is marked as ``thread``
tfLiteral # type represents literal value tfUniIntLit # type represents literal value that could be either
# singed or unsigned integer (e.g. 100)
tfFromGeneric # type is an instantiation of a generic; this is needed tfFromGeneric # type is an instantiation of a generic; this is needed
# because for instantiations of objects, structural # because for instantiations of objects, structural
# type equality has to be used # type equality has to be used

View file

@ -19,9 +19,10 @@ proc AccessThreadLocalVar(p: BProc, s: PSym) =
if emulatedThreadVars() and not p.ThreadVarAccessed: if emulatedThreadVars() and not p.ThreadVarAccessed:
p.ThreadVarAccessed = true p.ThreadVarAccessed = true
p.module.usesThreadVars = true p.module.usesThreadVars = true
lineF(p, cpsLocals, "NimThreadVars* NimTV;$n") appf(p.procSec(cpsLocals), "\tNimThreadVars* NimTV;$n")
lineCg(p, cpsInit, "NimTV = (NimThreadVars*) #GetThreadLocalVars();$n") app(p.procSec(cpsInit),
ropecg(p.module, "\tNimTV = (NimThreadVars*) #GetThreadLocalVars();$n"))
var var
nimtv: PRope # nimrod thread vars; the struct body nimtv: PRope # nimrod thread vars; the struct body
nimtvDeps: seq[PType] = @[] # type deps: every module needs whole struct nimtvDeps: seq[PType] = @[] # type deps: every module needs whole struct

View file

@ -403,6 +403,10 @@ proc GetNumber(L: var TLexer): TToken =
of tkInt8Lit: result.iNumber = biggestInt(int8(toU8(int(xi)))) of tkInt8Lit: result.iNumber = biggestInt(int8(toU8(int(xi))))
of tkInt16Lit: result.iNumber = biggestInt(toU16(int(xi))) of tkInt16Lit: result.iNumber = biggestInt(toU16(int(xi)))
of tkInt32Lit: result.iNumber = biggestInt(toU32(xi)) of tkInt32Lit: result.iNumber = biggestInt(toU32(xi))
of tkUIntLit, tkUInt64Lit: result.iNumber = xi
of tkUInt8Lit: result.iNumber = biggestInt(int8(toU8(int(xi))))
of tkUInt16Lit: result.iNumber = biggestInt(toU16(int(xi)))
of tkUInt32Lit: result.iNumber = biggestInt(toU32(xi))
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

@ -1283,9 +1283,9 @@ proc semMacroStmt(c: PContext, n: PNode, semCheck = true): PNode =
GlobalError(n.info, errInvalidExpressionX, GlobalError(n.info, errInvalidExpressionX,
renderTree(a, {renderNoComments})) renderTree(a, {renderNoComments}))
proc litIntType(kind: TTypeKind): PType = proc uniIntType(kind: TTypeKind): PType =
result = getSysType(kind).copyType(getCurrOwner(), true) result = getSysType(kind).copyType(getCurrOwner(), true)
result.flags.incl(tfLiteral) result.flags.incl(tfUniIntLit)
template memoize(e: expr): expr = template memoize(e: expr): expr =
var `*guard` {.global.} = false var `*guard` {.global.} = false
@ -1317,9 +1317,15 @@ proc semExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
if result.typ == nil: if result.typ == nil:
let i = result.intVal let i = result.intVal
if i >= low(int32) and i <= high(int32): if i >= low(int32) and i <= high(int32):
result.typ = litIntType(tyInt).memoize if i >= 0:
result.typ = uniIntType(tyInt).memoize
else:
result.typ = getSysType(tyInt)
else: else:
result.typ = litIntType(tyInt64).memoize if i >= 0:
result.typ = uniIntType(tyInt64).memoize
else:
result.typ = getSysType(tyInt64)
of nkInt8Lit: of nkInt8Lit:
if result.typ == nil: result.typ = getSysType(tyInt8) if result.typ == nil: result.typ = getSysType(tyInt8)
of nkInt16Lit: of nkInt16Lit:

View file

@ -164,7 +164,7 @@ proc handleRange(f, a: PType, min, max: TTypeKind): TTypeRelation =
if k == f.kind: result = isSubtype if k == f.kind: result = isSubtype
elif f.kind == tyInt and k in {tyInt..tyInt32}: result = isIntConv elif f.kind == tyInt and k in {tyInt..tyInt32}: result = isIntConv
elif f.kind == tyUInt and k in {tyUInt..tyUInt32}: result = isIntConv elif f.kind == tyUInt and k in {tyUInt..tyUInt32}: result = isIntConv
elif f.kind in {tyUInt..tyUInt64} and k == tyInt and tfLiteral in a.flags: elif f.kind in {tyUInt..tyUInt64} and k == tyInt and tfUniIntLit in a.flags:
result = isIntConv result = isIntConv
elif k >= min and k <= max: result = isConvertible elif k >= min and k <= max: result = isConvertible
else: result = isNone else: result = isNone

View file

@ -43,8 +43,13 @@ type
akFloat = 36, ## any represents a float akFloat = 36, ## any represents a float
akFloat32 = 37, ## any represents a float32 akFloat32 = 37, ## any represents a float32
akFloat64 = 38, ## any represents a float64 akFloat64 = 38, ## any represents a float64
akFloat128 = 39 ## any represents a float128 akFloat128 = 39, ## any represents a float128
akUInt = 40, ## any represents an unsigned int
akUInt8 = 41, ## any represents an unsigned int8
akUInt16 = 42, ## any represents an unsigned in16
akUInt32 = 43, ## any represents an unsigned int32
akUInt64 = 44, ## any represents an unsigned int64
TAny* = object {.pure.} ## can represent any nimrod value; NOTE: the wrapped TAny* = object {.pure.} ## can represent any nimrod value; NOTE: the wrapped
## value can be modified with its wrapper! This means ## value can be modified with its wrapper! This means
## that ``TAny`` keeps a non-traced pointer to its ## that ``TAny`` keeps a non-traced pointer to its

View file

@ -8,25 +8,25 @@
# #
## This module contains procs for serialization and deseralization of ## This module contains procs for serialization and deseralization of
## arbitrary Nimrod data structures. The serialization format uses JSON. ## arbitrary Nimrod data structures. The serialization format uses JSON.
## ##
## **Restriction**: For objects their type is **not** serialized. This means ## **Restriction**: For objects their type is **not** serialized. This means
## essentially that it does not work if the object has some other runtime ## essentially that it does not work if the object has some other runtime
## type than its compiletime type: ## type than its compiletime type:
## ##
## .. code-block:: nimrod ## .. code-block:: nimrod
## ##
## type ## type
## TA = object ## TA = object
## TB = object of TA ## TB = object of TA
## f: int ## f: int
## ##
## var ## var
## a: ref TA ## a: ref TA
## b: ref TB ## b: ref TB
## ##
## new(b) ## new(b)
## a = b ## a = b
## echo($$a[]) # produces "{}", not "{f: 0}" ## echo($$a[]) # produces "{}", not "{f: 0}"
import streams, typeinfo, json, intsets, tables import streams, typeinfo, json, intsets, tables
@ -86,7 +86,7 @@ proc storeAny(s: PStream, a: TAny, stored: var TIntSet) =
var x = getString(a) var x = getString(a)
if IsNil(x): s.write("null") if IsNil(x): s.write("null")
else: s.write(escapeJson(x)) else: s.write(escapeJson(x))
of akInt..akInt64: s.write($getBiggestInt(a)) of akInt..akInt64, akUInt..akUInt64: s.write($getBiggestInt(a))
of akFloat..akFloat128: s.write($getBiggestFloat(a)) of akFloat..akFloat128: s.write($getBiggestFloat(a))
proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) = proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) =
@ -128,18 +128,18 @@ proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) =
next(p) next(p)
of jsonArrayStart: of jsonArrayStart:
next(p) next(p)
invokeNewSeq(a, 0) invokeNewSeq(a, 0)
var i = 0 var i = 0
while p.kind != jsonArrayEnd and p.kind != jsonEof: while p.kind != jsonArrayEnd and p.kind != jsonEof:
extendSeq(a) extendSeq(a)
loadAny(p, a[i], t) loadAny(p, a[i], t)
inc(i) inc(i)
if p.kind == jsonArrayEnd: next(p) if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "") else: raiseParseErr(p, "")
else: else:
raiseParseErr(p, "'[' expected for a seq") raiseParseErr(p, "'[' expected for a seq")
of akObject, akTuple: of akObject, akTuple:
if a.kind == akObject: setObjectRuntimeType(a) if a.kind == akObject: setObjectRuntimeType(a)
if p.kind != jsonObjectStart: raiseParseErr(p, "'{' expected for an object") if p.kind != jsonObjectStart: raiseParseErr(p, "'{' expected for an object")
next(p) next(p)
while p.kind != jsonObjectEnd and p.kind != jsonEof: while p.kind != jsonObjectEnd and p.kind != jsonEof:
@ -169,13 +169,13 @@ proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) =
next(p) next(p)
of jsonArrayStart: of jsonArrayStart:
next(p) next(p)
if a.kind == akRef: invokeNew(a) if a.kind == akRef: invokeNew(a)
else: setPointer(a, alloc0(a.baseTypeSize)) else: setPointer(a, alloc0(a.baseTypeSize))
if p.kind == jsonInt: if p.kind == jsonInt:
t[p.getInt] = getPointer(a) t[p.getInt] = getPointer(a)
next(p) next(p)
else: raiseParseErr(p, "index for ref type expected") else: raiseParseErr(p, "index for ref type expected")
loadAny(p, a[], t) loadAny(p, a[], t)
if p.kind == jsonArrayEnd: next(p) if p.kind == jsonArrayEnd: next(p)
else: raiseParseErr(p, "']' end of ref-address pair expected") else: raiseParseErr(p, "']' end of ref-address pair expected")
else: raiseParseErr(p, "int for pointer type expected") else: raiseParseErr(p, "int for pointer type expected")
@ -197,7 +197,7 @@ proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) =
setString(a, p.str) setString(a, p.str)
next(p) next(p)
else: raiseParseErr(p, "string expected") else: raiseParseErr(p, "string expected")
of akInt..akInt64: of akInt..akInt64, akUInt..akUInt64:
if p.kind == jsonInt: if p.kind == jsonInt:
setBiggestInt(a, getInt(p)) setBiggestInt(a, getInt(p))
next(p) next(p)
@ -208,7 +208,7 @@ proc loadAny(p: var TJsonParser, a: TAny, t: var TTable[biggestInt, pointer]) =
setBiggestFloat(a, getFloat(p)) setBiggestFloat(a, getFloat(p))
next(p) next(p)
return return
raiseParseErr(p, "float expected") raiseParseErr(p, "float expected")
of akRange: loadAny(p, a.skipRange, t) of akRange: loadAny(p, a.skipRange, t)
proc loadAny(s: PStream, a: TAny, t: var TTable[biggestInt, pointer]) = proc loadAny(s: PStream, a: TAny, t: var TTable[biggestInt, pointer]) =
@ -220,7 +220,7 @@ proc loadAny(s: PStream, a: TAny, t: var TTable[biggestInt, pointer]) =
proc load*[T](s: PStream, data: var T) = proc load*[T](s: PStream, data: var T) =
## loads `data` from the stream `s`. Raises `EIO` in case of an error. ## loads `data` from the stream `s`. Raises `EIO` in case of an error.
var tab = initTable[biggestInt, pointer]() var tab = initTable[biggestInt, pointer]()
loadAny(s, toAny(data), tab) loadAny(s, toAny(data), tab)
proc store*[T](s: PStream, data: T) = proc store*[T](s: PStream, data: T) =
@ -229,91 +229,91 @@ proc store*[T](s: PStream, data: T) =
var d: T var d: T
shallowCopy(d, data) shallowCopy(d, data)
storeAny(s, toAny(d), stored) storeAny(s, toAny(d), stored)
proc `$$`*[T](x: T): string = proc `$$`*[T](x: T): string =
## returns a string representation of `x`. ## returns a string representation of `x`.
var stored = initIntSet() var stored = initIntSet()
var d: T var d: T
shallowCopy(d, x) shallowCopy(d, x)
var s = newStringStream() var s = newStringStream()
storeAny(s, toAny(d), stored) storeAny(s, toAny(d), stored)
result = s.data result = s.data
proc to*[T](data: string): T = proc to*[T](data: string): T =
## reads data and transforms it to a ``T``. ## reads data and transforms it to a ``T``.
var tab = initTable[biggestInt, pointer]() var tab = initTable[biggestInt, pointer]()
loadAny(newStringStream(data), toAny(result), tab) loadAny(newStringStream(data), toAny(result), tab)
when isMainModule: when isMainModule:
template testit(x: expr) = echo($$to[type(x)]($$x)) template testit(x: expr) = echo($$to[type(x)]($$x))
var x: array[0..4, array[0..4, string]] = [ var x: array[0..4, array[0..4, string]] = [
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"], ["test", "1", "2", "3", "4"],
["test", "1", "2", "3", "4"]] ["test", "1", "2", "3", "4"]]
testit(x) testit(x)
var test2: tuple[name: string, s: int] = ("tuple test", 56) var test2: tuple[name: string, s: int] = ("tuple test", 56)
testit(test2) testit(test2)
type type
TE = enum TE = enum
blah, blah2 blah, blah2
TestObj = object TestObj = object
test, asd: int test, asd: int
case test2: TE case test2: TE
of blah: of blah:
help: string help: string
else: else:
nil nil
PNode = ref TNode PNode = ref TNode
TNode = object TNode = object
next, prev: PNode next, prev: PNode
data: string data: string
proc buildList(): PNode = proc buildList(): PNode =
new(result) new(result)
new(result.next) new(result.next)
new(result.prev) new(result.prev)
result.data = "middle" result.data = "middle"
result.next.data = "next" result.next.data = "next"
result.prev.data = "prev" result.prev.data = "prev"
result.next.next = result.prev result.next.next = result.prev
result.next.prev = result result.next.prev = result
result.prev.next = result result.prev.next = result
result.prev.prev = result.next result.prev.prev = result.next
var test3: TestObj var test3: TestObj
test3.test = 42 test3.test = 42
test3.test2 = blah test3.test2 = blah
testit(test3) testit(test3)
var test4: ref tuple[a, b: string] var test4: ref tuple[a, b: string]
new(test4) new(test4)
test4.a = "ref string test: A" test4.a = "ref string test: A"
test4.b = "ref string test: B" test4.b = "ref string test: B"
testit(test4) testit(test4)
var test5 = @[(0,1),(2,3),(4,5)] var test5 = @[(0,1),(2,3),(4,5)]
testit(test5) testit(test5)
var test6: set[char] = {'A'..'Z', '_'} var test6: set[char] = {'A'..'Z', '_'}
testit(test6) testit(test6)
var test7 = buildList() var test7 = buildList()
echo($$test7) echo($$test7)
testit(test7) testit(test7)
type type
TA = object TA = object
TB = object of TA TB = object of TA
f: int f: int
var var
a: ref TA a: ref TA
b: ref TB b: ref TB
new(b) new(b)
a = b a = b
echo($$a[]) # produces "{}", not "{f: 0}" echo($$a[]) # produces "{}", not "{f: 0}"

View file

@ -18,9 +18,9 @@ type
TRune* = distinct irune ## type that can hold any Unicode character TRune* = distinct irune ## type that can hold any Unicode character
TRune16* = distinct int16 ## 16 bit Unicode character TRune16* = distinct int16 ## 16 bit Unicode character
proc `<=%`*(a, b: TRune): bool {.borrow.} proc `<=%`*(a, b: TRune): bool = return int(a) <=% int(b)
proc `<%`*(a, b: TRune): bool {.borrow.} proc `<%`*(a, b: TRune): bool = return int(a) <% int(b)
proc `==`*(a, b: TRune): bool {.borrow.} proc `==`*(a, b: TRune): bool = return int(a) == int(b)
template ones(n: expr): expr = ((1 shl n)-1) template ones(n: expr): expr = ((1 shl n)-1)

View file

@ -37,7 +37,9 @@ type # This should be he same as ast.TTypeKind
tyPointer, tyOpenArray, tyPointer, tyOpenArray,
tyString, tyCString, tyForward, tyString, tyCString, tyForward,
tyInt, tyInt8, tyInt16, tyInt32, tyInt64, tyInt, tyInt8, tyInt16, tyInt32, tyInt64,
tyFloat, tyFloat32, tyFloat64, tyFloat128 tyFloat, tyFloat32, tyFloat64, tyFloat128,
tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64,
tyBigNum,
TNimNodeKind = enum nkNone, nkSlot, nkList, nkCase TNimNodeKind = enum nkNone, nkSlot, nkList, nkCase
TNimNode {.codegenType, final.} = object TNimNode {.codegenType, final.} = object

View file

@ -41,10 +41,6 @@ type # WinNT.h -- Defines the 32-Bit Windows types and constants
type # BaseTsd.h -- Type definitions for the basic sized types type # BaseTsd.h -- Type definitions for the basic sized types
# Give here only the bare minimum, to be expanded as needs arise # Give here only the bare minimum, to be expanded as needs arise
UINT8* = int8
UINT16* = int16
UINT32* = int32
UINT64* = int64
LONG32* = int32 LONG32* = int32
ULONG32* = int32 ULONG32* = int32
DWORD32* = int32 DWORD32* = int32
@ -95,7 +91,6 @@ type # WinDef.h -- Basic Windows Type Definitions
LPCVOID* = pointer LPCVOID* = pointer
# INT* = int # Cannot work and not necessary anyway # INT* = int # Cannot work and not necessary anyway
UINT* = int
PUINT* = ptr int PUINT* = ptr int
WPARAM* = LONG_PTR WPARAM* = LONG_PTR
@ -18518,9 +18513,9 @@ proc DisableThreadLibraryCalls*(hLibModule: HMODULE): WINBOOL{.stdcall,
proc GetProcAddress*(hModule: HINST, lpProcName: LPCSTR): FARPROC{.stdcall, proc GetProcAddress*(hModule: HINST, lpProcName: LPCSTR): FARPROC{.stdcall,
dynlib: "kernel32", importc: "GetProcAddress".} dynlib: "kernel32", importc: "GetProcAddress".}
proc GetVersion*(): DWORD{.stdcall, dynlib: "kernel32", importc: "GetVersion".} proc GetVersion*(): DWORD{.stdcall, dynlib: "kernel32", importc: "GetVersion".}
proc GlobalAlloc*(uFlags: UINT, dwBytes: DWORD): HGLOBAL{.stdcall, proc GlobalAlloc*(uFlags: INT, dwBytes: DWORD): HGLOBAL{.stdcall,
dynlib: "kernel32", importc: "GlobalAlloc".} dynlib: "kernel32", importc: "GlobalAlloc".}
proc GlobalReAlloc*(hMem: HGLOBAL, dwBytes: DWORD, uFlags: UINT): HGLOBAL{. proc GlobalReAlloc*(hMem: HGLOBAL, dwBytes: DWORD, uFlags: INT): HGLOBAL{.
stdcall, dynlib: "kernel32", importc: "GlobalReAlloc".} stdcall, dynlib: "kernel32", importc: "GlobalReAlloc".}
proc GlobalSize*(hMem: HGLOBAL): DWORD{.stdcall, dynlib: "kernel32", proc GlobalSize*(hMem: HGLOBAL): DWORD{.stdcall, dynlib: "kernel32",
importc: "GlobalSize".} importc: "GlobalSize".}
@ -23620,8 +23615,8 @@ proc ListView_SetItemPosition32(hwndLV: HWND, i, x, y: int32): LRESULT =
proc ListView_SetItemState(hwndLV: HWND, i, data, mask: int32): LRESULT = proc ListView_SetItemState(hwndLV: HWND, i, data, mask: int32): LRESULT =
var gnu_lvi: LV_ITEM var gnu_lvi: LV_ITEM
gnu_lvi.stateMask = mask gnu_lvi.stateMask = uint(mask)
gnu_lvi.state = data gnu_lvi.state = uint(data)
result = SendMessage(hwndLV, LVM_SETITEMSTATE, WPARAM(i), result = SendMessage(hwndLV, LVM_SETITEMSTATE, WPARAM(i),
cast[LPARAM](addr(gnu_lvi))) cast[LPARAM](addr(gnu_lvi)))

View file

@ -756,9 +756,7 @@ type
PUInt8Array* = ptr TUInt8Array PUInt8Array* = ptr TUInt8Array
TUInt8Array* = array[0..high(int) shr 1, byte] TUInt8Array* = array[0..high(int) shr 1, byte]
PUInt16* = ptr UInt16 PUInt16* = ptr UInt16
UInt16* = int16 PUInt32* = ptr UInt32
PUInt32* = ptr int32
UInt32* = int32
PUInt64* = ptr UInt64 PUInt64* = ptr UInt64
UInt64*{.final.} = object UInt64*{.final.} = object
hi*: int32 hi*: int32

View file

@ -1,6 +1,6 @@
discard """ discard """
file: "system.nim" file: "system.nim"
line: 640 line: 678
errormsg: "type mismatch" errormsg: "type mismatch"
""" """