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

@ -102,7 +102,7 @@ proc newAny(value: pointer, rawType: PNimType): TAny =
result.value = value
result.rawType = rawType
when defined(system.TVarSlot):
when declared(system.TVarSlot):
proc toAny*(x: TVarSlot): TAny {.inline.} =
## constructs a ``TAny`` object from a variable slot ``x``.
## This captures `x`'s address, so `x` can be modified with its

View file

@ -243,7 +243,7 @@ template `=~` *(s: string, pattern: TRegex): expr =
## echo("syntax error")
##
bind maxSubPatterns
when not definedInScope(matches):
when not declaredInScope(matches):
var matches {.inject.}: array[0..MaxSubpatterns-1, string]
match(s, pattern, matches)

View file

@ -219,7 +219,7 @@ when not defined(JS):
proc randomize(seed: int) =
srand(cint(seed))
when defined(srand48): srand48(seed)
when declared(srand48): srand48(seed)
proc random(max: int): int =
result = int(rand()) mod max

View file

@ -26,7 +26,7 @@ const
withThreads = compileOption("threads")
tickCountCorrection = 50_000
when not defined(system.TStackTrace):
when not declared(system.TStackTrace):
type TStackTrace = array [0..20, cstring]
# We use a simple hash table of bounded size to keep track of the stack traces:
@ -146,7 +146,7 @@ proc `//`(a, b: int): string =
result = format("$1/$2 = $3%", a, b, formatFloat(a / b * 100.0, ffDefault, 2))
proc writeProfile() {.noconv.} =
when defined(system.TStackTrace):
when declared(system.TStackTrace):
system.profilerHook = nil
const filename = "profile_results.txt"
echo "writing " & filename & "..."
@ -189,16 +189,16 @@ var
disabled: int
proc disableProfiling*() =
when defined(system.TStackTrace):
when declared(system.TStackTrace):
atomicDec disabled
system.profilerHook = nil
proc enableProfiling*() =
when defined(system.TStackTrace):
when declared(system.TStackTrace):
if atomicInc(disabled) >= 0:
system.profilerHook = hook
when defined(system.TStackTrace):
when declared(system.TStackTrace):
system.profilerHook = hook
addQuitProc(writeProfile)

View file

@ -997,7 +997,7 @@ proc moveFile*(source, dest: string) {.rtl, extern: "nos$1",
if c_rename(source, dest) != 0'i32:
raise newException(EOS, $strerror(errno))
when not defined(ENOENT) and not defined(Windows):
when not declared(ENOENT) and not defined(Windows):
when NoFakeVars:
const ENOENT = cint(2) # 2 on most systems including Solaris
else:
@ -1615,11 +1615,11 @@ when defined(nimdoc):
##
## **Availability**: On Posix there is no portable way to get the command
## line from a DLL and thus the proc isn't defined in this environment. You
## can test for its availability with `defined() <system.html#defined>`_.
## can test for its availability with `declared() <system.html#declared>`_.
## Example:
##
## .. code-block:: nimrod
## when defined(paramCount):
## when declared(paramCount):
## # Use paramCount() here
## else:
## # Do something else!
@ -1638,11 +1638,11 @@ when defined(nimdoc):
##
## **Availability**: On Posix there is no portable way to get the command
## line from a DLL and thus the proc isn't defined in this environment. You
## can test for its availability with `defined() <system.html#defined>`_.
## can test for its availability with `declared() <system.html#declared>`_.
## Example:
##
## .. code-block:: nimrod
## when defined(paramStr):
## when declared(paramStr):
## # Use paramStr() here
## else:
## # Do something else!
@ -1682,7 +1682,7 @@ elif not defined(createNimRtl):
# Docstring in nimdoc block.
result = cmdCount-1
when defined(paramCount) or defined(nimdoc):
when declared(paramCount) or defined(nimdoc):
proc commandLineParams*(): seq[TaintedString] =
## Convenience proc which returns the command line parameters.
##
@ -1691,11 +1691,11 @@ when defined(paramCount) or defined(nimdoc):
##
## **Availability**: On Posix there is no portable way to get the command
## line from a DLL and thus the proc isn't defined in this environment. You
## can test for its availability with `defined() <system.html#defined>`_.
## can test for its availability with `declared() <system.html#declared>`_.
## Example:
##
## .. code-block:: nimrod
## when defined(commandLineParams):
## when declared(commandLineParams):
## # Use commandLineParams() here
## else:
## # Do something else!
@ -1714,7 +1714,7 @@ when defined(linux) or defined(solaris) or defined(bsd) or defined(aix):
when not (defined(windows) or defined(macosx)):
proc getApplHeuristic(): string =
when defined(paramStr):
when declared(paramStr):
result = string(paramStr(0))
# POSIX guaranties that this contains the executable
# as it has been executed by the calling process

View file

@ -643,7 +643,7 @@ elif not defined(useNimRtl):
data.workingDir = workingDir
when defined(posix_spawn) and not defined(useFork) and
when declared(posix_spawn) and not defined(useFork) and
not defined(useClone) and not defined(linux):
pid = startProcessAuxSpawn(data)
else:

View file

@ -37,7 +37,7 @@ type
## or the argument, ``value`` is not "" if
## the option was given a value
when defined(os.paramCount):
when declared(os.paramCount):
# we cannot provide this for NimRtl creation on Posix, because we can't
# access the command line arguments then!
@ -127,7 +127,7 @@ proc cmdLineRest*(p: TOptParser): TaintedString {.
## retrieves the rest of the command line that has not been parsed yet.
result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)).TaintedString
when defined(initOptParser):
when declared(initOptParser):
iterator getopt*(): tuple[kind: TCmdLineKind, key, val: TaintedString] =
## This is an convenience iterator for iterating over the command line.

View file

@ -119,7 +119,7 @@ proc cmdLineRest*(p: TOptParser): TaintedString {.rtl, extern: "npo$1", deprecat
type
TGetoptResult* = tuple[kind: TCmdLineKind, key, val: TaintedString]
when defined(paramCount):
when declared(paramCount):
iterator getopt*(): TGetoptResult =
## This is an convenience iterator for iterating over the command line.
## This uses the TOptParser object. Example:

View file

@ -870,7 +870,7 @@ template `=~`*(s: string, pattern: TPeg): bool =
## echo("syntax error")
##
bind maxSubpatterns
when not definedInScope(matches):
when not declaredInScope(matches):
var matches {.inject.}: array[0..MaxSubpatterns-1, string]
match(s, pattern, matches)

View file

@ -19,7 +19,7 @@
import
macros
when defined(stdout):
when declared(stdout):
import os
when not defined(ECMAScript):
@ -99,7 +99,7 @@ template fail* =
when not defined(ECMAScript):
if AbortOnError: quit(1)
when defined(TestStatusIMPL):
when declared(TestStatusIMPL):
TestStatusIMPL = FAILED
else:
program_result += 1
@ -188,7 +188,7 @@ macro expect*(exceptions: varargs[expr], body: stmt): stmt {.immediate.} =
result = getAst(expectBody(errorTypes, exp.lineinfo, body))
when defined(stdout):
when declared(stdout):
## Reading settings
var envOutLvl = os.getEnv("NIMTEST_OUTPUT_LVL").string

View file

@ -98,16 +98,8 @@ type
proc defined*(x: expr): bool {.magic: "Defined", noSideEffect.}
## Special compile-time procedure that checks whether `x` is
## defined. `x` has to be an identifier or a qualified identifier.
## This can be used to check whether a library provides a certain
## feature or not:
##
## .. code-block:: Nimrod
## when not defined(strutils.toUpper):
## # provide our own toUpper proc here, because strutils is
## # missing it.
##
## You can also check external symbols introduced through the compiler's
## defined.
## `x` is an external symbol introduced through the compiler's
## `-d:x switch <nimrodc.html#compile-time-symbols>`_ to enable build time
## conditionals:
##
@ -116,13 +108,28 @@ proc defined*(x: expr): bool {.magic: "Defined", noSideEffect.}
## # Do here programmer friendly expensive sanity checks.
## # Put here the normal code
proc declared*(x: expr): bool {.magic: "Defined", noSideEffect.}
## Special compile-time procedure that checks whether `x` is
## declared. `x` has to be an identifier or a qualified identifier.
## This can be used to check whether a library provides a certain
## feature or not:
##
## .. code-block:: Nimrod
## when not defined(strutils.toUpper):
## # provide our own toUpper proc here, because strutils is
## # missing it.
when defined(useNimRtl):
{.deadCodeElim: on.}
proc definedInScope*(x: expr): bool {.
magic: "DefinedInScope", noSideEffect.}
magic: "DefinedInScope", noSideEffect, deprecated.}
## **Deprecated since version 0.9.6**: Use ``declaredInScope`` instead.
proc declaredInScope*(x: expr): bool {.
magic: "DefinedInScope", noSideEffect, deprecated.}
## Special compile-time procedure that checks whether `x` is
## defined in the current scope. `x` has to be an identifier.
## declared in the current scope. `x` has to be an identifier.
proc `not` *(x: bool): bool {.magic: "Not", noSideEffect.}
## Boolean not; returns true iff ``x == false``.
@ -2118,7 +2125,7 @@ template newException*(exceptn: typedesc, message: string): expr =
when hostOS == "standalone":
include panicoverride
when not defined(sysFatal):
when not declared(sysFatal):
template sysFatal(exceptn: typedesc, message: string) =
when hostOS == "standalone":
panic(message)
@ -2170,7 +2177,7 @@ when not defined(JS): #and not defined(NimrodVM):
# WARNING: This is very fragile! An array size of 8 does not work on my
# Linux 64bit system. -- That's because the stack direction is the other
# way round.
when defined(setStackBottom):
when declared(setStackBottom):
var locals {.volatile.}: pointer
locals = addr(locals)
setStackBottom(locals)
@ -2443,7 +2450,7 @@ when not defined(JS): #and not defined(NimrodVM):
hasRaiseAction: bool
raiseAction: proc (e: ref E_Base): bool {.closure.}
when defined(initAllocator):
when declared(initAllocator):
initAllocator()
when hasThreadSupport:
include "system/syslocks"
@ -3016,7 +3023,7 @@ proc compiles*(x): bool {.magic: "Compiles", noSideEffect.} =
## echo "'+' for integers is available"
discard
when defined(initDebugger):
when declared(initDebugger):
initDebugger()
when hostOS != "standalone":

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".}

View file

@ -333,11 +333,5 @@ proc VERSION*(X: var sdl.Tversion) =
X.patch = PATCHLEVEL
when not (defined(Workaround_RenderText_Solid)):
proc RenderText_Solid*(font: PFont, text: cstring, fg: TColor): PSurface{.
cdecl, importc: "TTF_RenderText_Solid", dynlib: ttfLibName.}
else:
proc RenderText_Solid(font: PFont, text: cstring, fg: TColor): PSurface =
var Black: TColor # initialized to zero
result = RenderText_Shaded(font, text, fg, Black)
proc RenderText_Solid*(font: PFont, text: cstring, fg: TColor): PSurface{.
cdecl, importc: "TTF_RenderText_Solid", dynlib: ttfLibName.}