distinguish between 'defined' and 'declared'

This commit is contained in:
Araq 2014-08-11 20:38:28 +02:00
commit dd806cafa0
36 changed files with 155 additions and 96 deletions

View file

@ -39,7 +39,7 @@ var
c_stderr {.importc: "stderr", nodecl.}: C_TextFileStar
# constants faked as variables:
when not defined(SIGINT):
when not declared(SIGINT):
when NoFakeVars:
when defined(windows):
const
@ -132,7 +132,7 @@ proc c_realloc(p: pointer, newsize: int): pointer {.
importc: "realloc", header: "<stdlib.h>".}
when hostOS != "standalone":
when not defined(errno):
when not declared(errno):
when defined(NimrodVM):
var vmErrnoWrapper {.importc.}: ptr cint
template errno: expr =

View file

@ -241,26 +241,26 @@ elif false: # asmVersion and (defined(gcc) or defined(llvm_gcc)):
"""
# Platform independent versions of the above (slower!)
when not defined(addInt):
when not declared(addInt):
proc addInt(a, b: int): int {.compilerProc, inline.} =
result = a +% b
if (result xor a) >= 0 or (result xor b) >= 0:
return result
raiseOverflow()
when not defined(subInt):
when not declared(subInt):
proc subInt(a, b: int): int {.compilerProc, inline.} =
result = a -% b
if (result xor a) >= 0 or (result xor not b) >= 0:
return result
raiseOverflow()
when not defined(negInt):
when not declared(negInt):
proc negInt(a: int): int {.compilerProc, inline.} =
if a != low(int): return -a
raiseOverflow()
when not defined(divInt):
when not declared(divInt):
proc divInt(a, b: int): int {.compilerProc, inline.} =
if b == 0:
raiseDivByZero()
@ -268,13 +268,13 @@ when not defined(divInt):
raiseOverflow()
return a div b
when not defined(modInt):
when not declared(modInt):
proc modInt(a, b: int): int {.compilerProc, inline.} =
if b == 0:
raiseDivByZero()
return a mod b
when not defined(mulInt):
when not declared(mulInt):
#
# This code has been inspired by Python's source code.
# The native int product x*y is either exactly right or *way* off, being

View file

@ -174,7 +174,7 @@ proc atomicInc*(memLoc: var int, x: int = 1): int =
proc atomicDec*(memLoc: var int, x: int = 1): int =
when defined(gcc) and hasThreadSupport:
when defined(atomic_sub_fetch):
when declared(atomic_sub_fetch):
result = atomic_sub_fetch(memLoc.addr, x, ATOMIC_RELAXED)
else:
result = atomic_add_fetch(memLoc.addr, -x, ATOMIC_RELAXED)
@ -201,14 +201,14 @@ when (defined(x86) or defined(amd64)) and (defined(gcc) or defined(llvm_gcc)):
{.emit: """asm volatile("pause" ::: "memory");""".}
elif (defined(x86) or defined(amd64)) and defined(vcc):
proc cpuRelax {.importc: "YieldProcessor", header: "<windows.h>".}
elif defined(intelc):
elif defined(icl):
proc cpuRelax {.importc: "_mm_pause", header: "xmmintrin.h".}
elif false:
from os import sleep
proc cpuRelax {.inline.} = os.sleep(1)
when not defined(fence) and hasThreadSupport:
when not declared(fence) and hasThreadSupport:
# XXX fixme
proc fence*() {.inline.} =
var dummy: bool

View file

@ -14,7 +14,7 @@
## **Note:** The current implementation of message passing is slow and does
## not work with cyclic data structures.
when not defined(NimString):
when not declared(NimString):
{.error: "You must not import this module explicitly".}
type

View file

@ -93,7 +93,7 @@ proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
if s2 == nil:
unsureAsgnRef(cast[PPointer](dest), s2)
return
when defined(usrToCell):
when declared(usrToCell):
# unfortunately we only have cycle detection for our native GCs.
let x = usrToCell(s2)
let forw = cast[int](x.typ)

View file

@ -307,7 +307,7 @@ when not defined(noSignalHandler):
action("SIGBUS: Illegal storage access. (Attempt to read from nil?)\n")
else:
block platformSpecificSignal:
when defined(SIGPIPE):
when declared(SIGPIPE):
if s == SIGPIPE:
action("SIGPIPE: Pipe closed.\n")
break platformSpecificSignal
@ -336,7 +336,7 @@ when not defined(noSignalHandler):
c_signal(SIGFPE, signalHandler)
c_signal(SIGILL, signalHandler)
c_signal(SIGBUS, signalHandler)
when defined(SIGPIPE):
when declared(SIGPIPE):
c_signal(SIGPIPE, signalHandler)
registerSignalHandler() # call it in initialization section

View file

@ -30,7 +30,7 @@ const
# cycles instead of the complex
# algorithm
when withRealTime and not defined(getTicks):
when withRealTime and not declared(getTicks):
include "system/timers"
when defined(memProfiler):
proc nimProfile(requestedSize: int)
@ -413,7 +413,7 @@ proc addNewObjToZCT(res: PCell, gch: var TGcHeap) {.inline.} =
{.push stackTrace: off, profiler:off.}
proc gcInvariant*() =
sysAssert(allocInv(gch.region), "injected")
when defined(markForDebug):
when declared(markForDebug):
markForDebug(gch)
{.pop.}

View file

@ -26,7 +26,7 @@ const
# this seems to be a good value
withRealTime = defined(useRealtimeGC)
when withRealTime and not defined(getTicks):
when withRealTime and not declared(getTicks):
include "system/timers"
when defined(memProfiler):
proc nimProfile(requestedSize: int)

View file

@ -7,7 +7,7 @@
# distribution, for details about the copyright.
#
when defined(NimString):
when declared(NimString):
# we are in system module:
{.pragma: codegenType, compilerproc.}
else:

View file

@ -121,7 +121,7 @@ proc reprSet(p: pointer, typ: PNimType): string {.compilerRtl.} =
type
TReprClosure {.final.} = object # we cannot use a global variable here
# as this wouldn't be thread-safe
when defined(TCellSet):
when declared(TCellSet):
marked: TCellSet
recdepth: int # do not recurse endlessly
indent: int # indentation
@ -130,16 +130,16 @@ when not defined(useNimRtl):
proc initReprClosure(cl: var TReprClosure) =
# Important: cellsets does not lock the heap when doing allocations! We
# have to do it here ...
when hasThreadSupport and hasSharedHeap and defined(heapLock):
when hasThreadSupport and hasSharedHeap and declared(heapLock):
AcquireSys(HeapLock)
when defined(TCellSet):
when declared(TCellSet):
init(cl.marked)
cl.recdepth = -1 # default is to display everything!
cl.indent = 0
proc deinitReprClosure(cl: var TReprClosure) =
when defined(TCellSet): deinit(cl.marked)
when hasThreadSupport and hasSharedHeap and defined(heapLock):
when declared(TCellSet): deinit(cl.marked)
when hasThreadSupport and hasSharedHeap and declared(heapLock):
ReleaseSys(HeapLock)
proc reprBreak(result: var string, cl: TReprClosure) =
@ -201,7 +201,7 @@ when not defined(useNimRtl):
proc reprRef(result: var string, p: pointer, typ: PNimType,
cl: var TReprClosure) =
# we know that p is not nil here:
when defined(TCellSet):
when declared(TCellSet):
when defined(boehmGC) or defined(nogc):
var cell = cast[PCell](p)
else:

View file

@ -9,7 +9,7 @@
## Implements Nimrod's 'spawn'.
when not defined(NimString):
when not declared(NimString):
{.error: "You must not import this module explicitly".}
{.push stackTrace:off.}

View file

@ -32,7 +32,7 @@ proc eqStrings(a, b: NimString): bool {.inline, compilerProc.} =
return a.len == b.len and
c_memcmp(a.data, b.data, a.len * sizeof(char)) == 0'i32
when defined(allocAtomic):
when declared(allocAtomic):
template allocStr(size: expr): expr =
cast[NimString](allocAtomic(size))
else:
@ -85,7 +85,7 @@ proc copyStringRC1(src: NimString): NimString {.compilerRtl.} =
if src != nil:
var s = src.space
if s < 8: s = 7
when defined(newObjRC1):
when declared(newObjRC1):
result = cast[NimString](newObjRC1(addr(strDesc), sizeof(TGenericSeq) +
s+1))
else:

View file

@ -39,7 +39,7 @@
## createThread(thr[i], threadFunc, (i*10, i*10+5))
## joinThreads(thr)
when not defined(NimString):
when not declared(NimString):
{.error: "You must not import this module explicitly".}
const
@ -267,7 +267,7 @@ when not defined(boehmgc) and not hasSharedHeap:
proc deallocOsPages()
template threadProcWrapperBody(closure: expr) {.immediate.} =
when defined(globalsSlot): ThreadVarSetValue(globalsSlot, closure)
when declared(globalsSlot): ThreadVarSetValue(globalsSlot, closure)
var t = cast[ptr TThread[TArg]](closure)
when useStackMaskHack:
var tls: TThreadLocalStorage
@ -275,13 +275,13 @@ template threadProcWrapperBody(closure: expr) {.immediate.} =
# init the GC for this thread:
setStackBottom(addr(t))
initGC()
when defined(registerThread):
when declared(registerThread):
t.stackBottom = addr(t)
registerThread(t)
when TArg is void: t.dataFn()
else: t.dataFn(t.data)
when defined(registerThread): unregisterThread(t)
when defined(deallocOsPages): deallocOsPages()
when declared(registerThread): unregisterThread(t)
when declared(deallocOsPages): deallocOsPages()
# Since an unhandled exception terminates the whole process (!), there is
# no need for a ``try finally`` here, nor would it be correct: The current
# exception is tried to be re-raised by the code-gen after the ``finally``!
@ -332,7 +332,7 @@ when false:
discard TerminateThread(t.sys, 1'i32)
else:
discard pthread_cancel(t.sys)
when defined(registerThread): unregisterThread(addr(t))
when declared(registerThread): unregisterThread(addr(t))
t.dataFn = nil
proc createThread*[TArg](t: var TThread[TArg],

View file

@ -10,7 +10,7 @@
# Nimrod support for C/C++'s `wide strings`:idx:. This is part of the system
# module! Do not import it directly!
when not defined(NimString):
when not declared(NimString):
{.error: "You must not import this module explicitly".}
type
@ -103,7 +103,7 @@ proc newWideCString*(source: cstring, L: int): WideCString =
proc newWideCString*(s: cstring): WideCString =
if s.isNil: return nil
when not defined(c_strlen):
when not declared(c_strlen):
proc c_strlen(a: cstring): int {.
header: "<string.h>", noSideEffect, importc: "strlen".}