Merge ../Nim into devel

This commit is contained in:
Charles Blake 2015-08-03 08:06:20 -04:00
commit 2d9a0c96c6
22 changed files with 875 additions and 657 deletions

View file

@ -6,7 +6,7 @@ Name: "Nim"
Version: "$version" Version: "$version"
Platforms: """ Platforms: """
windows: i386;amd64 windows: i386;amd64
linux: i386;amd64;powerpc64;arm;sparc;mips;powerpc linux: i386;amd64;powerpc64;arm;sparc;mips;mipsel;powerpc;powerpc64el;arm64
macosx: i386;amd64;powerpc64 macosx: i386;amd64;powerpc64
solaris: i386;amd64;sparc solaris: i386;amd64;sparc
freebsd: i386;amd64 freebsd: i386;amd64

View file

@ -729,14 +729,13 @@ proc genBreakStmt(p: PProc, n: PNode) =
p.blocks[idx].id = abs(p.blocks[idx].id) # label is used p.blocks[idx].id = abs(p.blocks[idx].id) # label is used
addf(p.body, "break L$1;$n" | "goto ::L$1::;$n", [rope(p.blocks[idx].id)]) addf(p.body, "break L$1;$n" | "goto ::L$1::;$n", [rope(p.blocks[idx].id)])
proc genAsmStmt(p: PProc, n: PNode) = proc genAsmOrEmitStmt(p: PProc, n: PNode) =
genLineDir(p, n) genLineDir(p, n)
assert(n.kind == nkAsmStmt)
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
case n.sons[i].kind case n.sons[i].kind
of nkStrLit..nkTripleStrLit: add(p.body, n.sons[i].strVal) of nkStrLit..nkTripleStrLit: add(p.body, n.sons[i].strVal)
of nkSym: add(p.body, mangleName(n.sons[i].sym)) of nkSym: add(p.body, mangleName(n.sons[i].sym))
else: internalError(n.sons[i].info, "jsgen: genAsmStmt()") else: internalError(n.sons[i].info, "jsgen: genAsmOrEmitStmt()")
proc genIf(p: PProc, n: PNode, r: var TCompRes) = proc genIf(p: PProc, n: PNode, r: var TCompRes) =
var cond, stmt: TCompRes var cond, stmt: TCompRes
@ -1578,6 +1577,12 @@ proc genStmt(p: PProc, n: PNode) =
gen(p, n, r) gen(p, n, r)
if r.res != nil: addf(p.body, "$#;$n", [r.res]) if r.res != nil: addf(p.body, "$#;$n", [r.res])
proc genPragma(p: PProc, n: PNode) =
for it in n.sons:
case whichPragma(it)
of wEmit: genAsmOrEmitStmt(p, it.sons[1])
else: discard
proc gen(p: PProc, n: PNode, r: var TCompRes) = proc gen(p: PProc, n: PNode, r: var TCompRes) =
r.typ = etyNone r.typ = etyNone
r.kind = resNone r.kind = resNone
@ -1677,12 +1682,13 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
genLineDir(p, n) genLineDir(p, n)
gen(p, n.sons[0], r) gen(p, n.sons[0], r)
of nkAsmStmt: genAsmStmt(p, n) of nkAsmStmt: genAsmOrEmitStmt(p, n)
of nkTryStmt: genTry(p, n, r) of nkTryStmt: genTry(p, n, r)
of nkRaiseStmt: genRaiseStmt(p, n) of nkRaiseStmt: genRaiseStmt(p, n)
of nkTypeSection, nkCommentStmt, nkIteratorDef, nkIncludeStmt, of nkTypeSection, nkCommentStmt, nkIteratorDef, nkIncludeStmt,
nkImportStmt, nkImportExceptStmt, nkExportStmt, nkExportExceptStmt, nkImportStmt, nkImportExceptStmt, nkExportStmt, nkExportExceptStmt,
nkFromStmt, nkTemplateDef, nkMacroDef, nkPragma: discard nkFromStmt, nkTemplateDef, nkMacroDef: discard
of nkPragma: genPragma(p, n)
of nkProcDef, nkMethodDef, nkConverterDef: of nkProcDef, nkMethodDef, nkConverterDef:
var s = n.sons[namePos].sym var s = n.sons[namePos].sym
if {sfExportc, sfCompilerProc} * s.flags == {sfExportc}: if {sfExportc, sfCompilerProc} * s.flags == {sfExportc}:

View file

@ -158,8 +158,8 @@ type
TSystemCPU* = enum # Also add CPU for in initialization section and TSystemCPU* = enum # Also add CPU for in initialization section and
# alias conditionals to condsyms (end of module). # alias conditionals to condsyms (end of module).
cpuNone, cpuI386, cpuM68k, cpuAlpha, cpuPowerpc, cpuPowerpc64, cpuNone, cpuI386, cpuM68k, cpuAlpha, cpuPowerpc, cpuPowerpc64,
cpuSparc, cpuVm, cpuIa64, cpuAmd64, cpuMips, cpuArm, cpuPowerpc64el, cpuSparc, cpuVm, cpuIa64, cpuAmd64, cpuMips, cpuMipsel,
cpuJS, cpuNimrodVM, cpuAVR cpuArm, cpuArm64, cpuJS, cpuNimrodVM, cpuAVR
type type
TEndian* = enum TEndian* = enum
@ -175,12 +175,15 @@ const
(name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64), (name: "alpha", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "powerpc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64), (name: "powerpc64", intSize: 64, endian: bigEndian, floatSize: 64,bit: 64),
(name: "powerpc64el", intSize: 64, endian: littleEndian, floatSize: 64,bit: 64),
(name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "sparc", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32), (name: "vm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
(name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64), (name: "ia64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64), (name: "amd64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "mips", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "mipsel", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
(name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32), (name: "arm", intSize: 32, endian: littleEndian, floatSize: 64, bit: 32),
(name: "arm64", intSize: 64, endian: littleEndian, floatSize: 64, bit: 64),
(name: "js", intSize: 32, endian: bigEndian,floatSize: 64,bit: 32), (name: "js", intSize: 32, endian: bigEndian,floatSize: 64,bit: 32),
(name: "nimrodvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32), (name: "nimrodvm", intSize: 32, endian: bigEndian, floatSize: 64, bit: 32),
(name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16)] (name: "avr", intSize: 16, endian: littleEndian, floatSize: 32, bit: 16)]

View file

@ -591,280 +591,282 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: int,
validPragmas: TSpecialWords): bool = validPragmas: TSpecialWords): bool =
var it = n.sons[i] var it = n.sons[i]
var key = if it.kind == nkExprColonExpr: it.sons[0] else: it var key = if it.kind == nkExprColonExpr: it.sons[0] else: it
if key.kind == nkIdent: if key.kind == nkBracketExpr:
var userPragma = strTableGet(c.userPragmas, key.ident) processNote(c, it)
if userPragma != nil: return
inc c.instCounter let ident = considerQuotedIdent(key)
if c.instCounter > 100: var userPragma = strTableGet(c.userPragmas, ident)
globalError(it.info, errRecursiveDependencyX, userPragma.name.s) if userPragma != nil:
pragma(c, sym, userPragma.ast, validPragmas) inc c.instCounter
# ensure the pragma is also remember for generic instantiations in other if c.instCounter > 100:
# modules: globalError(it.info, errRecursiveDependencyX, userPragma.name.s)
n.sons[i] = userPragma.ast pragma(c, sym, userPragma.ast, validPragmas)
dec c.instCounter # ensure the pragma is also remember for generic instantiations in other
else: # modules:
var k = whichKeyword(key.ident) n.sons[i] = userPragma.ast
if k in validPragmas: dec c.instCounter
case k else:
of wExportc: var k = whichKeyword(ident)
makeExternExport(sym, getOptionalStr(c, it, "$1"), it.info) if k in validPragmas:
incl(sym.flags, sfUsed) # avoid wrong hints case k
of wImportc: makeExternImport(sym, getOptionalStr(c, it, "$1")) of wExportc:
of wImportCompilerProc: makeExternExport(sym, getOptionalStr(c, it, "$1"), it.info)
processImportCompilerProc(sym, getOptionalStr(c, it, "$1")) incl(sym.flags, sfUsed) # avoid wrong hints
of wExtern: setExternName(sym, expectStrLit(c, it)) of wImportc: makeExternImport(sym, getOptionalStr(c, it, "$1"))
of wImmediate: of wImportCompilerProc:
if sym.kind in {skTemplate, skMacro}: incl(sym.flags, sfImmediate) processImportCompilerProc(sym, getOptionalStr(c, it, "$1"))
else: invalidPragma(it) of wExtern: setExternName(sym, expectStrLit(c, it))
of wDirty: of wImmediate:
if sym.kind == skTemplate: incl(sym.flags, sfDirty) if sym.kind in {skTemplate, skMacro}: incl(sym.flags, sfImmediate)
else: invalidPragma(it)
of wImportCpp:
processImportCpp(sym, getOptionalStr(c, it, "$1"))
of wImportObjC:
processImportObjC(sym, getOptionalStr(c, it, "$1"))
of wAlign:
if sym.typ == nil: invalidPragma(it)
var align = expectIntLit(c, it)
if (not isPowerOfTwo(align) and align != 0) or align >% high(int16):
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.align = align.int16
of wSize:
if sym.typ == nil: invalidPragma(it)
var size = expectIntLit(c, it)
if not isPowerOfTwo(size) or size <= 0 or size > 8:
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.size = size
of wNodecl:
noVal(it)
incl(sym.loc.flags, lfNoDecl)
of wPure, wAsmNoStackFrame:
noVal(it)
if sym != nil:
if k == wPure and sym.kind in routineKinds: invalidPragma(it)
else: incl(sym.flags, sfPure)
of wVolatile:
noVal(it)
incl(sym.flags, sfVolatile)
of wRegister:
noVal(it)
incl(sym.flags, sfRegister)
of wThreadVar:
noVal(it)
incl(sym.flags, sfThread)
of wDeadCodeElim: pragmaDeadCodeElim(c, it)
of wNoForward: pragmaNoForward(c, it)
of wMagic: processMagic(c, it, sym)
of wCompileTime:
noVal(it)
incl(sym.flags, sfCompileTime)
incl(sym.loc.flags, lfNoDecl)
of wGlobal:
noVal(it)
incl(sym.flags, sfGlobal)
incl(sym.flags, sfPure)
of wMerge:
# only supported for backwards compat, doesn't do anything anymore
noVal(it)
of wConstructor:
noVal(it)
incl(sym.flags, sfConstructor)
of wHeader:
var lib = getLib(c, libHeader, getStrLitNode(c, it))
addToLib(lib, sym)
incl(sym.flags, sfImportc)
incl(sym.loc.flags, lfHeader)
incl(sym.loc.flags, lfNoDecl)
# implies nodecl, because otherwise header would not make sense
if sym.loc.r == nil: sym.loc.r = rope(sym.name.s)
of wDestructor:
sym.flags.incl sfOverriden
if sym.name.s.normalize != "destroy":
localError(n.info, errGenerated, "destructor has to be named 'destroy'")
of wOverride:
sym.flags.incl sfOverriden
of wNosideeffect:
noVal(it)
incl(sym.flags, sfNoSideEffect)
if sym.typ != nil: incl(sym.typ.flags, tfNoSideEffect)
of wSideeffect:
noVal(it)
incl(sym.flags, sfSideEffect)
of wNoreturn:
noVal(it)
incl(sym.flags, sfNoReturn)
of wDynlib:
processDynLib(c, it, sym)
of wCompilerproc:
noVal(it) # compilerproc may not get a string!
if sfFromGeneric notin sym.flags: markCompilerProc(sym)
of wProcVar:
noVal(it)
incl(sym.flags, sfProcvar)
of wDeprecated:
if it.kind == nkExprColonExpr: deprecatedStmt(c, it)
elif sym != nil: incl(sym.flags, sfDeprecated)
else: incl(c.module.flags, sfDeprecated)
of wVarargs:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfVarargs)
of wBorrow:
if sym.kind == skType:
typeBorrow(sym, it)
else:
noVal(it)
incl(sym.flags, sfBorrow)
of wFinal:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfFinal)
of wInheritable:
noVal(it)
if sym.typ == nil or tfFinal in sym.typ.flags: invalidPragma(it)
else: incl(sym.typ.flags, tfInheritable)
of wAcyclic:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfAcyclic)
of wShallow:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfShallow)
of wThread:
noVal(it)
incl(sym.flags, sfThread)
incl(sym.flags, sfProcvar)
if sym.typ != nil: incl(sym.typ.flags, tfThread)
of wGcSafe:
noVal(it)
if sym.kind != skType: incl(sym.flags, sfThread)
if sym.typ != nil: incl(sym.typ.flags, tfGcSafe)
else: invalidPragma(it)
of wPacked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfPacked)
of wHint: message(it.info, hintUser, expectStrLit(c, it))
of wWarning: message(it.info, warnUser, expectStrLit(c, it))
of wError:
if sym != nil and sym.isRoutine:
# This is subtle but correct: the error *statement* is only
# allowed for top level statements. Seems to be easier than
# distinguishing properly between
# ``proc p() {.error}`` and ``proc p() = {.error: "msg".}``
noVal(it)
incl(sym.flags, sfError)
else:
localError(it.info, errUser, expectStrLit(c, it))
of wFatal: fatal(it.info, errUser, expectStrLit(c, it))
of wDefine: processDefine(c, it)
of wUndef: processUndef(c, it)
of wCompile: processCompile(c, it)
of wLink: processCommonLink(c, it, linkNormal)
of wLinksys: processCommonLink(c, it, linkSys)
of wPassl: extccomp.addLinkOption(expectStrLit(c, it))
of wPassc: extccomp.addCompileOption(expectStrLit(c, it))
of wBreakpoint: pragmaBreakpoint(c, it)
of wWatchPoint: pragmaWatchpoint(c, it)
of wPush:
processPush(c, n, i + 1)
result = true
of wPop: processPop(c, it)
of wPragma:
processPragma(c, n, i)
result = true
of wDiscardable:
noVal(it)
if sym != nil: incl(sym.flags, sfDiscardable)
of wNoInit:
noVal(it)
if sym != nil: incl(sym.flags, sfNoInit)
of wCodegenDecl: processCodegenDecl(c, it, sym)
of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks,
wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints,
wLinedir, wStacktrace, wLinetrace, wOptimization,
wCallconv,
wDebugger, wProfiler, wFloatchecks, wNanChecks, wInfChecks,
wPatterns:
if processOption(c, it):
# calling conventions (boring...):
localError(it.info, errOptionExpected)
of FirstCallConv..LastCallConv:
assert(sym != nil)
if sym.typ == nil: invalidPragma(it)
else: sym.typ.callConv = wordToCallConv(k)
of wEmit: pragmaEmit(c, it)
of wUnroll: pragmaUnroll(c, it)
of wLinearScanEnd, wComputedGoto: noVal(it)
of wEffects:
# is later processed in effect analysis:
noVal(it)
of wIncompleteStruct:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfIncompleteStruct)
of wUnchecked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUncheckedArray)
of wUnion:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUnion)
of wRequiresInit:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfNeedsInit)
of wByRef:
noVal(it)
if sym == nil or sym.typ == nil:
if processOption(c, it): localError(it.info, errOptionExpected)
else:
incl(sym.typ.flags, tfByRef)
of wByCopy:
noVal(it)
if sym.kind != skType or sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfByCopy)
of wInject, wGensym:
# We check for errors, but do nothing with these pragmas otherwise
# as they are handled directly in 'evalTemplate'.
noVal(it)
if sym == nil: invalidPragma(it)
of wLine: pragmaLine(c, it)
of wRaises, wTags: pragmaRaisesOrTags(c, it)
of wLocks:
if sym == nil: pragmaLockStmt(c, it)
elif sym.typ == nil: invalidPragma(it)
else: sym.typ.lockLevel = pragmaLocks(c, it)
of wGuard:
if sym == nil or sym.kind notin {skVar, skLet, skField}:
invalidPragma(it)
else:
sym.guard = pragmaGuard(c, it, sym.kind)
of wGoto:
if sym == nil or sym.kind notin {skVar, skLet}:
invalidPragma(it)
else:
sym.flags.incl sfGoto
of wInjectStmt:
if it.kind != nkExprColonExpr:
localError(it.info, errExprExpected)
else:
it.sons[1] = c.semExpr(c, it.sons[1])
of wExperimental:
noVal(it)
if isTopLevel(c):
c.module.flags.incl sfExperimental
else:
localError(it.info, "'experimental' pragma only valid as toplevel statement")
of wNoRewrite:
noVal(it)
else: invalidPragma(it) else: invalidPragma(it)
of wDirty:
if sym.kind == skTemplate: incl(sym.flags, sfDirty)
else: invalidPragma(it)
of wImportCpp:
processImportCpp(sym, getOptionalStr(c, it, "$1"))
of wImportObjC:
processImportObjC(sym, getOptionalStr(c, it, "$1"))
of wAlign:
if sym.typ == nil: invalidPragma(it)
var align = expectIntLit(c, it)
if (not isPowerOfTwo(align) and align != 0) or align >% high(int16):
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.align = align.int16
of wSize:
if sym.typ == nil: invalidPragma(it)
var size = expectIntLit(c, it)
if not isPowerOfTwo(size) or size <= 0 or size > 8:
localError(it.info, errPowerOfTwoExpected)
else:
sym.typ.size = size
of wNodecl:
noVal(it)
incl(sym.loc.flags, lfNoDecl)
of wPure, wAsmNoStackFrame:
noVal(it)
if sym != nil:
if k == wPure and sym.kind in routineKinds: invalidPragma(it)
else: incl(sym.flags, sfPure)
of wVolatile:
noVal(it)
incl(sym.flags, sfVolatile)
of wRegister:
noVal(it)
incl(sym.flags, sfRegister)
of wThreadVar:
noVal(it)
incl(sym.flags, sfThread)
of wDeadCodeElim: pragmaDeadCodeElim(c, it)
of wNoForward: pragmaNoForward(c, it)
of wMagic: processMagic(c, it, sym)
of wCompileTime:
noVal(it)
incl(sym.flags, sfCompileTime)
incl(sym.loc.flags, lfNoDecl)
of wGlobal:
noVal(it)
incl(sym.flags, sfGlobal)
incl(sym.flags, sfPure)
of wMerge:
# only supported for backwards compat, doesn't do anything anymore
noVal(it)
of wConstructor:
noVal(it)
incl(sym.flags, sfConstructor)
of wHeader:
var lib = getLib(c, libHeader, getStrLitNode(c, it))
addToLib(lib, sym)
incl(sym.flags, sfImportc)
incl(sym.loc.flags, lfHeader)
incl(sym.loc.flags, lfNoDecl)
# implies nodecl, because otherwise header would not make sense
if sym.loc.r == nil: sym.loc.r = rope(sym.name.s)
of wDestructor:
sym.flags.incl sfOverriden
if sym.name.s.normalize != "destroy":
localError(n.info, errGenerated, "destructor has to be named 'destroy'")
of wOverride:
sym.flags.incl sfOverriden
of wNosideeffect:
noVal(it)
incl(sym.flags, sfNoSideEffect)
if sym.typ != nil: incl(sym.typ.flags, tfNoSideEffect)
of wSideeffect:
noVal(it)
incl(sym.flags, sfSideEffect)
of wNoreturn:
noVal(it)
incl(sym.flags, sfNoReturn)
of wDynlib:
processDynLib(c, it, sym)
of wCompilerproc:
noVal(it) # compilerproc may not get a string!
if sfFromGeneric notin sym.flags: markCompilerProc(sym)
of wProcVar:
noVal(it)
incl(sym.flags, sfProcvar)
of wDeprecated:
if it.kind == nkExprColonExpr: deprecatedStmt(c, it)
elif sym != nil: incl(sym.flags, sfDeprecated)
else: incl(c.module.flags, sfDeprecated)
of wVarargs:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfVarargs)
of wBorrow:
if sym.kind == skType:
typeBorrow(sym, it)
else:
noVal(it)
incl(sym.flags, sfBorrow)
of wFinal:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfFinal)
of wInheritable:
noVal(it)
if sym.typ == nil or tfFinal in sym.typ.flags: invalidPragma(it)
else: incl(sym.typ.flags, tfInheritable)
of wAcyclic:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfAcyclic)
of wShallow:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfShallow)
of wThread:
noVal(it)
incl(sym.flags, sfThread)
incl(sym.flags, sfProcvar)
if sym.typ != nil: incl(sym.typ.flags, tfThread)
of wGcSafe:
noVal(it)
if sym.kind != skType: incl(sym.flags, sfThread)
if sym.typ != nil: incl(sym.typ.flags, tfGcSafe)
else: invalidPragma(it)
of wPacked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfPacked)
of wHint: message(it.info, hintUser, expectStrLit(c, it))
of wWarning: message(it.info, warnUser, expectStrLit(c, it))
of wError:
if sym != nil and sym.isRoutine:
# This is subtle but correct: the error *statement* is only
# allowed for top level statements. Seems to be easier than
# distinguishing properly between
# ``proc p() {.error}`` and ``proc p() = {.error: "msg".}``
noVal(it)
incl(sym.flags, sfError)
else:
localError(it.info, errUser, expectStrLit(c, it))
of wFatal: fatal(it.info, errUser, expectStrLit(c, it))
of wDefine: processDefine(c, it)
of wUndef: processUndef(c, it)
of wCompile: processCompile(c, it)
of wLink: processCommonLink(c, it, linkNormal)
of wLinksys: processCommonLink(c, it, linkSys)
of wPassl: extccomp.addLinkOption(expectStrLit(c, it))
of wPassc: extccomp.addCompileOption(expectStrLit(c, it))
of wBreakpoint: pragmaBreakpoint(c, it)
of wWatchPoint: pragmaWatchpoint(c, it)
of wPush:
processPush(c, n, i + 1)
result = true
of wPop: processPop(c, it)
of wPragma:
processPragma(c, n, i)
result = true
of wDiscardable:
noVal(it)
if sym != nil: incl(sym.flags, sfDiscardable)
of wNoInit:
noVal(it)
if sym != nil: incl(sym.flags, sfNoInit)
of wCodegenDecl: processCodegenDecl(c, it, sym)
of wChecks, wObjChecks, wFieldChecks, wRangechecks, wBoundchecks,
wOverflowchecks, wNilchecks, wAssertions, wWarnings, wHints,
wLinedir, wStacktrace, wLinetrace, wOptimization,
wCallconv,
wDebugger, wProfiler, wFloatchecks, wNanChecks, wInfChecks,
wPatterns:
if processOption(c, it):
# calling conventions (boring...):
localError(it.info, errOptionExpected)
of FirstCallConv..LastCallConv:
assert(sym != nil)
if sym.typ == nil: invalidPragma(it)
else: sym.typ.callConv = wordToCallConv(k)
of wEmit: pragmaEmit(c, it)
of wUnroll: pragmaUnroll(c, it)
of wLinearScanEnd, wComputedGoto: noVal(it)
of wEffects:
# is later processed in effect analysis:
noVal(it)
of wIncompleteStruct:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfIncompleteStruct)
of wUnchecked:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUncheckedArray)
of wUnion:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfUnion)
of wRequiresInit:
noVal(it)
if sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfNeedsInit)
of wByRef:
noVal(it)
if sym == nil or sym.typ == nil:
if processOption(c, it): localError(it.info, errOptionExpected)
else:
incl(sym.typ.flags, tfByRef)
of wByCopy:
noVal(it)
if sym.kind != skType or sym.typ == nil: invalidPragma(it)
else: incl(sym.typ.flags, tfByCopy)
of wInject, wGensym:
# We check for errors, but do nothing with these pragmas otherwise
# as they are handled directly in 'evalTemplate'.
noVal(it)
if sym == nil: invalidPragma(it)
of wLine: pragmaLine(c, it)
of wRaises, wTags: pragmaRaisesOrTags(c, it)
of wLocks:
if sym == nil: pragmaLockStmt(c, it)
elif sym.typ == nil: invalidPragma(it)
else: sym.typ.lockLevel = pragmaLocks(c, it)
of wGuard:
if sym == nil or sym.kind notin {skVar, skLet, skField}:
invalidPragma(it)
else:
sym.guard = pragmaGuard(c, it, sym.kind)
of wGoto:
if sym == nil or sym.kind notin {skVar, skLet}:
invalidPragma(it)
else:
sym.flags.incl sfGoto
of wInjectStmt:
if it.kind != nkExprColonExpr:
localError(it.info, errExprExpected)
else:
it.sons[1] = c.semExpr(c, it.sons[1])
of wExperimental:
noVal(it)
if isTopLevel(c):
c.module.flags.incl sfExperimental
else:
localError(it.info, "'experimental' pragma only valid as toplevel statement")
of wNoRewrite:
noVal(it)
else: invalidPragma(it) else: invalidPragma(it)
else: processNote(c, it) else: invalidPragma(it)
proc implicitPragmas*(c: PContext, sym: PSym, n: PNode, proc implicitPragmas*(c: PContext, sym: PSym, n: PNode,
validPragmas: TSpecialWords) = validPragmas: TSpecialWords) =

View file

@ -171,11 +171,15 @@ proc newSymS(kind: TSymKind, n: PNode, c: PContext): PSym =
result = newSym(kind, considerQuotedIdent(n), getCurrOwner(), n.info) result = newSym(kind, considerQuotedIdent(n), getCurrOwner(), n.info)
proc newSymG*(kind: TSymKind, n: PNode, c: PContext): PSym = proc newSymG*(kind: TSymKind, n: PNode, c: PContext): PSym =
proc `$`(kind: TSymKind): string = substr(system.`$`(kind), 2).toLower
# like newSymS, but considers gensym'ed symbols # like newSymS, but considers gensym'ed symbols
if n.kind == nkSym: if n.kind == nkSym:
# and sfGenSym in n.sym.flags: # and sfGenSym in n.sym.flags:
result = n.sym result = n.sym
internalAssert result.kind == kind if result.kind != kind:
localError(n.info, "cannot use symbol of kind '" &
$result.kind & "' as a '" & $kind & "'")
# when there is a nested proc inside a template, semtmpl # when there is a nested proc inside a template, semtmpl
# will assign a wrong owner during the first pass over the # will assign a wrong owner during the first pass over the
# template; we must fix it here: see #909 # template; we must fix it here: see #909

View file

@ -307,12 +307,12 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
of mToU8: result = newIntNodeT(getInt(a) and 0x000000FF, n) of mToU8: result = newIntNodeT(getInt(a) and 0x000000FF, n)
of mToU16: result = newIntNodeT(getInt(a) and 0x0000FFFF, n) of mToU16: result = newIntNodeT(getInt(a) and 0x0000FFFF, n)
of mToU32: result = newIntNodeT(getInt(a) and 0x00000000FFFFFFFF'i64, n) of mToU32: result = newIntNodeT(getInt(a) and 0x00000000FFFFFFFF'i64, n)
of mUnaryLt: result = newIntNodeT(getOrdValue(a) - 1, n) of mUnaryLt: result = newIntNodeT(getOrdValue(a) |-| 1, n)
of mSucc: result = newIntNodeT(getOrdValue(a) + getInt(b), n) of mSucc: result = newIntNodeT(getOrdValue(a) |+| getInt(b), n)
of mPred: result = newIntNodeT(getOrdValue(a) - getInt(b), n) of mPred: result = newIntNodeT(getOrdValue(a) |-| getInt(b), n)
of mAddI: result = newIntNodeT(getInt(a) + getInt(b), n) of mAddI: result = newIntNodeT(getInt(a) |+| getInt(b), n)
of mSubI: result = newIntNodeT(getInt(a) - getInt(b), n) of mSubI: result = newIntNodeT(getInt(a) |-| getInt(b), n)
of mMulI: result = newIntNodeT(getInt(a) * getInt(b), n) of mMulI: result = newIntNodeT(getInt(a) |*| getInt(b), n)
of mMinI: of mMinI:
if getInt(a) > getInt(b): result = newIntNodeT(getInt(b), n) if getInt(a) > getInt(b): result = newIntNodeT(getInt(b), n)
else: result = newIntNodeT(getInt(a), n) else: result = newIntNodeT(getInt(a), n)
@ -338,11 +338,11 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
of mDivI: of mDivI:
let y = getInt(b) let y = getInt(b)
if y != 0: if y != 0:
result = newIntNodeT(getInt(a) div y, n) result = newIntNodeT(`|div|`(getInt(a), y), n)
of mModI: of mModI:
let y = getInt(b) let y = getInt(b)
if y != 0: if y != 0:
result = newIntNodeT(getInt(a) mod y, n) result = newIntNodeT(`|mod|`(getInt(a), y), n)
of mAddF64: result = newFloatNodeT(getFloat(a) + getFloat(b), n) of mAddF64: result = newFloatNodeT(getFloat(a) + getFloat(b), n)
of mSubF64: result = newFloatNodeT(getFloat(a) - getFloat(b), n) of mSubF64: result = newFloatNodeT(getFloat(a) - getFloat(b), n)
of mMulF64: result = newFloatNodeT(getFloat(a) * getFloat(b), n) of mMulF64: result = newFloatNodeT(getFloat(a) * getFloat(b), n)

View file

@ -23,6 +23,9 @@ proc newConstraint(c: PContext, k: TTypeKind): PType =
proc semEnum(c: PContext, n: PNode, prev: PType): PType = proc semEnum(c: PContext, n: PNode, prev: PType): PType =
if n.sonsLen == 0: return newConstraint(c, tyEnum) if n.sonsLen == 0: return newConstraint(c, tyEnum)
elif n.sonsLen == 1:
# don't create an empty tyEnum; fixes #3052
return errorType(c)
var var
counter, x: BiggestInt counter, x: BiggestInt
e: PSym e: PSym

View file

@ -120,10 +120,10 @@ template decodeBx(k: expr) {.immediate, dirty.} =
template move(a, b: expr) {.immediate, dirty.} = system.shallowCopy(a, b) template move(a, b: expr) {.immediate, dirty.} = system.shallowCopy(a, b)
# XXX fix minor 'shallowCopy' overloading bug in compiler # XXX fix minor 'shallowCopy' overloading bug in compiler
proc createStrKeepNode(x: var TFullReg) = proc createStrKeepNode(x: var TFullReg; keepNode=true) =
if x.node.isNil: if x.node.isNil:
x.node = newNode(nkStrLit) x.node = newNode(nkStrLit)
elif x.node.kind == nkNilLit: elif x.node.kind == nkNilLit and keepNode:
when defined(useNodeIds): when defined(useNodeIds):
let id = x.node.id let id = x.node.id
system.reset(x.node[]) system.reset(x.node[])
@ -385,6 +385,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
#if c.traceActive: #if c.traceActive:
# echo "PC ", pc, " ", c.code[pc].opcode, " ra ", ra # echo "PC ", pc, " ", c.code[pc].opcode, " ra ", ra
# message(c.debug[pc], warnUser, "Trace") # message(c.debug[pc], warnUser, "Trace")
case instr.opcode case instr.opcode
of opcEof: return regs[ra] of opcEof: return regs[ra]
of opcRet: of opcRet:
@ -407,8 +408,8 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
decodeB(rkInt) decodeB(rkInt)
regs[ra].intVal = regs[rb].intVal regs[ra].intVal = regs[rb].intVal
of opcAsgnStr: of opcAsgnStr:
decodeB(rkNode) decodeBC(rkNode)
createStrKeepNode regs[ra] createStrKeepNode regs[ra], rc != 0
regs[ra].node.strVal = regs[rb].node.strVal regs[ra].node.strVal = regs[rb].node.strVal
of opcAsgnFloat: of opcAsgnFloat:
decodeB(rkFloat) decodeB(rkFloat)
@ -509,8 +510,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
of rkNode: of rkNode:
if regs[rb].node.kind == nkNilLit: if regs[rb].node.kind == nkNilLit:
stackTrace(c, tos, pc, errNilAccess) stackTrace(c, tos, pc, errNilAccess)
assert regs[rb].node.kind == nkRefTy if regs[rb].node.kind == nkRefTy:
regs[ra].node = regs[rb].node.sons[0] regs[ra].node = regs[rb].node.sons[0]
else:
stackTrace(c, tos, pc, errGenerated, "limited VM support for 'ref'")
else: else:
stackTrace(c, tos, pc, errNilAccess) stackTrace(c, tos, pc, errNilAccess)
of opcWrDeref: of opcWrDeref:

View file

@ -74,8 +74,9 @@ proc codeListing(c: PCtx, result: var string, start=0; last = -1) =
result.addf("\t$#\tr$#, L$#", ($opc).substr(3), x.regA, result.addf("\t$#\tr$#, L$#", ($opc).substr(3), x.regA,
i+x.regBx-wordExcess) i+x.regBx-wordExcess)
elif opc in {opcLdConst, opcAsgnConst}: elif opc in {opcLdConst, opcAsgnConst}:
result.addf("\t$#\tr$#, $#", ($opc).substr(3), x.regA, let idx = x.regBx-wordExcess
c.constants[x.regBx-wordExcess].renderTree) result.addf("\t$#\tr$#, $# ($#)", ($opc).substr(3), x.regA,
c.constants[idx].renderTree, $idx)
elif opc in {opcMarshalLoad, opcMarshalStore}: elif opc in {opcMarshalLoad, opcMarshalStore}:
let y = c.code[i+1] let y = c.code[i+1]
result.addf("\t$#\tr$#, r$#, $#", ($opc).substr(3), x.regA, x.regB, result.addf("\t$#\tr$#, r$#, $#", ($opc).substr(3), x.regA, x.regB,
@ -184,7 +185,7 @@ proc getTemp(cc: PCtx; typ: PType): TRegister =
return TRegister(i) return TRegister(i)
# if register pressure is high, we re-use more aggressively: # if register pressure is high, we re-use more aggressively:
if c.maxSlots >= HighRegisterPressure: if c.maxSlots >= HighRegisterPressure and false:
for i in 0 .. c.maxSlots-1: for i in 0 .. c.maxSlots-1:
if not c.slots[i].inUse: if not c.slots[i].inUse:
c.slots[i] = (inUse: true, kind: k) c.slots[i] = (inUse: true, kind: k)
@ -1073,6 +1074,7 @@ const
tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64} tyUInt, tyUInt8, tyUInt16, tyUInt32, tyUInt64}
proc fitsRegister*(t: PType): bool = proc fitsRegister*(t: PType): bool =
assert t != nil
t.skipTypes(abstractInst-{tyTypeDesc}).kind in { t.skipTypes(abstractInst-{tyTypeDesc}).kind in {
tyRange, tyEnum, tyBool, tyInt..tyUInt64, tyChar} tyRange, tyEnum, tyBool, tyInt..tyUInt64, tyChar}
@ -1110,6 +1112,7 @@ proc genAddrDeref(c: PCtx; n: PNode; dest: var TDest; opc: TOpcode;
if dest < 0: dest = c.getTemp(n.typ) if dest < 0: dest = c.getTemp(n.typ)
if not isAddr: if not isAddr:
gABC(c, n, opc, dest, tmp) gABC(c, n, opc, dest, tmp)
assert n.typ != nil
if gfAddrOf notin flags and fitsRegister(n.typ): if gfAddrOf notin flags and fitsRegister(n.typ):
c.gABC(n, opcNodeToReg, dest, dest) c.gABC(n, opcNodeToReg, dest, dest)
elif c.prc.slots[tmp].kind >= slotTempUnknown: elif c.prc.slots[tmp].kind >= slotTempUnknown:
@ -1143,7 +1146,7 @@ proc whichAsgnOpc(n: PNode; opc: TOpcode): TOpcode = opc
proc genAsgn(c: PCtx; dest: TDest; ri: PNode; requiresCopy: bool) = proc genAsgn(c: PCtx; dest: TDest; ri: PNode; requiresCopy: bool) =
let tmp = c.genx(ri) let tmp = c.genx(ri)
assert dest >= 0 assert dest >= 0
gABC(c, ri, whichAsgnOpc(ri), dest, tmp) gABC(c, ri, whichAsgnOpc(ri), dest, tmp, 1-ord(requiresCopy))
c.freeTemp(tmp) c.freeTemp(tmp)
proc setSlot(c: PCtx; v: PSym) = proc setSlot(c: PCtx; v: PSym) =
@ -1194,9 +1197,10 @@ proc preventFalseAlias(c: PCtx; n: PNode; opc: TOpcode;
# opcLdObj et al really means "load address". We sometimes have to create a # opcLdObj et al really means "load address". We sometimes have to create a
# copy in order to not introduce false aliasing: # copy in order to not introduce false aliasing:
# mylocal = a.b # needs a copy of the data! # mylocal = a.b # needs a copy of the data!
assert n.typ != nil
if needsAdditionalCopy(n): if needsAdditionalCopy(n):
var cc = c.getTemp(n.typ) var cc = c.getTemp(n.typ)
c.gABC(n, whichAsgnOpc(n), cc, value) c.gABC(n, whichAsgnOpc(n), cc, value, 0)
c.gABC(n, opc, dest, idx, cc) c.gABC(n, opc, dest, idx, cc)
c.freeTemp(cc) c.freeTemp(cc)
else: else:
@ -1241,10 +1245,11 @@ proc genAsgn(c: PCtx; le, ri: PNode; requiresCopy: bool) =
internalAssert s.position > 0 or (s.position == 0 and internalAssert s.position > 0 or (s.position == 0 and
s.kind in {skParam,skResult}) s.kind in {skParam,skResult})
var dest: TRegister = s.position + ord(s.kind == skParam) var dest: TRegister = s.position + ord(s.kind == skParam)
assert le.typ != nil
if needsAdditionalCopy(le) and s.kind in {skResult, skVar, skParam}: if needsAdditionalCopy(le) and s.kind in {skResult, skVar, skParam}:
var cc = c.getTemp(le.typ) var cc = c.getTemp(le.typ)
gen(c, ri, cc) gen(c, ri, cc)
c.gABC(le, whichAsgnOpc(le), dest, cc) c.gABC(le, whichAsgnOpc(le), dest, cc, 1)
c.freeTemp(cc) c.freeTemp(cc)
else: else:
gen(c, ri, dest) gen(c, ri, dest)
@ -1303,6 +1308,7 @@ proc genRdVar(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags) =
if sfImportc in s.flags: c.importcSym(n.info, s) if sfImportc in s.flags: c.importcSym(n.info, s)
else: genGlobalInit(c, n, s) else: genGlobalInit(c, n, s)
if dest < 0: dest = c.getTemp(n.typ) if dest < 0: dest = c.getTemp(n.typ)
assert s.typ != nil
if gfAddrOf notin flags and fitsRegister(s.typ): if gfAddrOf notin flags and fitsRegister(s.typ):
var cc = c.getTemp(n.typ) var cc = c.getTemp(n.typ)
c.gABx(n, opcLdGlobal, cc, s.position) c.gABx(n, opcLdGlobal, cc, s.position)
@ -1426,6 +1432,7 @@ proc getNullValue(typ: PType, info: TLineInfo): PNode =
globalError(info, "cannot create null element for: " & $t.kind) globalError(info, "cannot create null element for: " & $t.kind)
proc ldNullOpcode(t: PType): TOpcode = proc ldNullOpcode(t: PType): TOpcode =
assert t != nil
if fitsRegister(t): opcLdNullReg else: opcLdNull if fitsRegister(t): opcLdNullReg else: opcLdNull
proc genVarSection(c: PCtx; n: PNode) = proc genVarSection(c: PCtx; n: PNode) =
@ -1453,7 +1460,7 @@ proc genVarSection(c: PCtx; n: PNode) =
if a.sons[2].kind != nkEmpty: if a.sons[2].kind != nkEmpty:
let tmp = c.genx(a.sons[0], {gfAddrOf}) let tmp = c.genx(a.sons[0], {gfAddrOf})
let val = c.genx(a.sons[2]) let val = c.genx(a.sons[2])
c.preventFalseAlias(a, opcWrDeref, tmp, 0, val) c.preventFalseAlias(a.sons[2], opcWrDeref, tmp, 0, val)
c.freeTemp(val) c.freeTemp(val)
c.freeTemp(tmp) c.freeTemp(tmp)
else: else:
@ -1461,13 +1468,15 @@ proc genVarSection(c: PCtx; n: PNode) =
if a.sons[2].kind == nkEmpty: if a.sons[2].kind == nkEmpty:
c.gABx(a, ldNullOpcode(s.typ), s.position, c.genType(s.typ)) c.gABx(a, ldNullOpcode(s.typ), s.position, c.genType(s.typ))
else: else:
assert s.typ != nil
if not fitsRegister(s.typ): if not fitsRegister(s.typ):
c.gABx(a, ldNullOpcode(s.typ), s.position, c.genType(s.typ)) c.gABx(a, ldNullOpcode(s.typ), s.position, c.genType(s.typ))
let le = a.sons[0] let le = a.sons[0]
assert le.typ != nil
if not fitsRegister(le.typ) and s.kind in {skResult, skVar, skParam}: if not fitsRegister(le.typ) and s.kind in {skResult, skVar, skParam}:
var cc = c.getTemp(le.typ) var cc = c.getTemp(le.typ)
gen(c, a.sons[2], cc) gen(c, a.sons[2], cc)
c.gABC(le, whichAsgnOpc(le), s.position.TRegister, cc) c.gABC(le, whichAsgnOpc(le), s.position.TRegister, cc, 1)
c.freeTemp(cc) c.freeTemp(cc)
else: else:
gen(c, a.sons[2], s.position.TRegister) gen(c, a.sons[2], s.position.TRegister)
@ -1694,7 +1703,7 @@ proc gen(c: PCtx; n: PNode; dest: var TDest; flags: TGenFlags = {}) =
c.freeTemp(tmp1) c.freeTemp(tmp1)
c.freeTemp(tmp2) c.freeTemp(tmp2)
if dest >= 0: if dest >= 0:
gABC(c, n, whichAsgnOpc(n), dest, tmp0) gABC(c, n, whichAsgnOpc(n), dest, tmp0, 1)
c.freeTemp(tmp0) c.freeTemp(tmp0)
else: else:
dest = tmp0 dest = tmp0

View file

@ -142,7 +142,7 @@ When contributing new procedures, be sure to add documentation, especially if
the procedure is exported from the module. Documentation begins on the line the procedure is exported from the module. Documentation begins on the line
following the ``proc`` definition, and is prefixed by ``##`` on each line. following the ``proc`` definition, and is prefixed by ``##`` on each line.
Code examples are also encouraged. The RestructuredText Nim uses has a special Code examples are also encouraged. The RestructuredText Nim uses has a special
syntax for including examples. syntax for including examples.
.. code-block:: nim .. code-block:: nim
@ -155,8 +155,8 @@ syntax for including examples.
## echo someproc() # "something" ## echo someproc() # "something"
result = "something" # single-hash comments do not produce documentation result = "something" # single-hash comments do not produce documentation
The ``.. code-block:: nim`` followed by a newline and an indentation instructs the The ``.. code-block:: nim`` followed by a newline and an indentation instructs the
``nim doc`` and ``nim doc2`` commands to produce syntax-highlighted example code with ``nim doc`` and ``nim doc2`` commands to produce syntax-highlighted example code with
the documentation. the documentation.
When forward declaration is used, the documentation should be included with the When forward declaration is used, the documentation should be included with the
@ -186,7 +186,7 @@ or
proc hello*(): string = proc hello*(): string =
# says hello # says hello
result = "hello" result = "hello"
the first is preferred. the first is preferred.
The Git stuff The Git stuff
@ -216,3 +216,5 @@ General commit rules
git diff --check --cached || exit $? git diff --check --cached || exit $?
3. Describe your commit and use your common sense. 3. Describe your commit and use your common sense.
.. include:: styleguide.rst

View file

@ -588,7 +588,8 @@ can also be defined with indentation instead of ``[]``:
Objects provide many features that tuples do not. Object provide inheritance Objects provide many features that tuples do not. Object provide inheritance
and information hiding. Objects have access to their type at runtime, so that and information hiding. Objects have access to their type at runtime, so that
the ``of`` operator can be used to determine the object's type. the ``of`` operator can be used to determine the object's type. The ``of`` operator
is similar to the ``instanceof`` operator in Java.
.. code-block:: nim .. code-block:: nim
type type

140
docstyle.rst Normal file
View file

@ -0,0 +1,140 @@
Documentation Style
===================
General Guidelines
------------------
* Authors should document anything that is exported.
* Within documentation, a period (`.`) should follow each sentence (or sentence fragment) in a comment block. The documentation may be limited to one sentence fragment, but if multiple sentences are within the documentation, each sentence after the first should be complete and in present tense.
* Documentation is parsed as ReStructuredText (RST).
* Inline code should be surrounded by double tick marks ("``````"). If you would like a character to immediately follow inline code (e.g., "``int8``s are great!"), escape the following character with a backslash (``\``). The preceding is typed as ``` ``int8``\s are great!```.
Module-level documentation
--------------------------
Documentation of a module is placed at the top of the module itself. Each line of documentation begins with double hashes (``##``).
Code samples are encouraged, and should follow the general RST syntax:
.. code-block:: Nim
## The ``universe`` module computes the answer to life, the universe, and everything.
##
## .. code-block:: Nim
## echo computeAnswerString() # "42"
Within this top-level comment, you can indicate the authorship and copyright of the code, which will be featured in the produced documentation.
.. code-block:: Nim
## This is the best module ever. It provides answers to everything!
##
## :Author: Steve McQueen
## :Copyright: 1965
##
Leave a space between the last line of top-level documentation and the beginning of Nim code (the imports, etc.).
Procs, Templates, Macros, Converters, and Iterators
---------------------------------------------------
The documentation of a procedure should begin with a capital letter and should be in present tense. Variables referenced in the documentation should be surrounded by double tick marks (``````).
.. code-block:: Nim
proc example1*(x: int) =
## Prints the value of ``x``.
echo x
Whenever an example of usage would be helpful to the user, you should include one within the documentation in RST format as below.
.. code-block:: Nim
proc addThree*(x, y, z: int8): int =
## Adds three ``int8`` values, treating them as unsigned and
## truncating the result.
##
## .. code-block:: nim
## echo addThree(3, 125, 6) # -122
result = x +% y +% z
The commands ``nim doc`` and ``nim doc2`` will then correctly syntax highlight the Nim code within the documentation.
Types
-----
Exported types should also be documented. This documentation can also contain code samples, but those are better placed with the functions to which they refer.
.. code-block:: Nim
type
NamedQueue*[T] = object ## Provides a linked data structure with names
## throughout. It is named for convenience. I'm making
## this comment long to show how you can, too.
name*: string ## The name of the item
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
You have some flexibility when placing the documentation:
.. code-block:: Nim
type
NamedQueue*[T] = object
## Provides a linked data structure with names
## throughout. It is named for convenience. I'm making
## this comment long to show how you can, too.
name*: string ## The name of the item
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
Make sure to place the documentation beside or within the object.
.. code-block:: Nim
type
## This documentation disappears because it annotates the ``type`` keyword
## above, not ``NamedQueue``.
NamedQueue*[T] = object
name*: string ## This becomes the main documentation for the object, which
## is not what we want.
val*: T ## Its value
next*: ref NamedQueue[T] ## The next item in the queue
Var, Let, and Const
-------------------
When declaring module-wide constants and values, documentation is encouraged. The placement of doc comments is similar to the ``type`` sections.
.. code-block:: Nim
const
X* = 42 ## An awesome number.
SpreadArray* = [
[1,2,3],
[2,3,1],
[3,1,2],
] ## Doc comment for ``SpreadArray``.
Placement of comments in other areas is usually allowed, but will not become part of the documentation output and should therefore be prefaced by a single hash (``#``).
.. code-block:: Nim
const
BadMathVals* = [
3.14, # pi
2.72, # e
0.58, # gamma
] ## A bunch of badly rounded values.
Nim supports Unicode in comments, so the above can be replaced with the following:
.. code-block:: Nim
const
BadMathVals* = [
3.14, # π
2.72, # e
0.58, # γ
] ## A bunch of badly rounded values (including π!).

View file

@ -755,7 +755,7 @@ proc renderTocEntries*(d: var RstGenerator, j: var int, lvl: int,
proc renderImage(d: PDoc, n: PRstNode, result: var string) = proc renderImage(d: PDoc, n: PRstNode, result: var string) =
template valid(s): expr = template valid(s): expr =
s.len > 0 and allCharsInSet(s, {'/',':','%','_','\\','\128'..'\xFF'} + s.len > 0 and allCharsInSet(s, {'.','/',':','%','_','\\','\128'..'\xFF'} +
Digits + Letters + WhiteSpace) Digits + Letters + WhiteSpace)
var options = "" var options = ""

View file

@ -402,7 +402,7 @@ proc request*(url: string, httpMethod: string, extraHeaders = "",
headers.add(" HTTP/1.1\c\L") headers.add(" HTTP/1.1\c\L")
add(headers, "Host: " & r.hostname & "\c\L") add(headers, "Host: " & parseUri(url).hostname & "\c\L")
if userAgent != "": if userAgent != "":
add(headers, "User-Agent: " & userAgent & "\c\L") add(headers, "User-Agent: " & userAgent & "\c\L")
if proxy != nil and proxy.auth != "": if proxy != nil and proxy.auth != "":

View file

@ -176,10 +176,16 @@ proc new*[T](a: var ref T) {.magic: "New", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced) ## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``. ## reference to it in ``a``.
proc new*(T: typedesc): ref T = proc new*(T: typedesc): auto =
## creates a new object of type ``T`` and returns a safe (traced) ## creates a new object of type ``T`` and returns a safe (traced)
## reference to it as result value ## reference to it as result value
new(result) when (T is ref):
var r: T
else:
var r: ref T
new(r)
return r
proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.} proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.}
## leaked implementation detail. Do not use. ## leaked implementation detail. Do not use.
@ -481,6 +487,7 @@ type
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
ObjectConversionError* = object of Exception ## \ ObjectConversionError* = object of Exception ## \
## Raised if an object is converted to an incompatible object type. ## Raised if an object is converted to an incompatible object type.
## You can use ``of`` operator to check if conversion will succeed.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
FloatingPointError* = object of Exception ## \ FloatingPointError* = object of Exception ## \
@ -1150,7 +1157,8 @@ const
hostCPU* {.magic: "HostCPU".}: string = "" hostCPU* {.magic: "HostCPU".}: string = ""
## a string that describes the host CPU. Possible values: ## a string that describes the host CPU. Possible values:
## "i386", "alpha", "powerpc", "powerpc64", "sparc", "amd64", "mips", "arm". ## "i386", "alpha", "powerpc", "powerpc64", "powerpc64el", "sparc",
## "amd64", "mips", "mipsel", "arm", "arm64".
seqShallowFlag = low(int) seqShallowFlag = low(int)

View file

@ -18,11 +18,14 @@ type
alpha, ## Alpha processor alpha, ## Alpha processor
powerpc, ## 32 bit PowerPC powerpc, ## 32 bit PowerPC
powerpc64, ## 64 bit PowerPC powerpc64, ## 64 bit PowerPC
powerpc64el, ## Little Endian 64 bit PowerPC
sparc, ## Sparc based processor sparc, ## Sparc based processor
ia64, ## Intel Itanium ia64, ## Intel Itanium
amd64, ## x86_64 (AMD64); 64 bit x86 compatible CPU amd64, ## x86_64 (AMD64); 64 bit x86 compatible CPU
mips, ## Mips based processor mips, ## Mips based processor
mipsel, ## Little Endian Mips based processor
arm, ## ARM based processor arm, ## ARM based processor
arm64, ## ARM64 based processor
vm, ## Some Virtual machine: Nim's VM or JavaScript vm, ## Some Virtual machine: Nim's VM or JavaScript
avr ## AVR based processor avr ## AVR based processor
@ -63,11 +66,14 @@ const
elif defined(alpha): CpuPlatform.alpha elif defined(alpha): CpuPlatform.alpha
elif defined(powerpc): CpuPlatform.powerpc elif defined(powerpc): CpuPlatform.powerpc
elif defined(powerpc64): CpuPlatform.powerpc64 elif defined(powerpc64): CpuPlatform.powerpc64
elif defined(powerpc64el): CpuPlatform.powerpc64el
elif defined(sparc): CpuPlatform.sparc elif defined(sparc): CpuPlatform.sparc
elif defined(ia64): CpuPlatform.ia64 elif defined(ia64): CpuPlatform.ia64
elif defined(amd64): CpuPlatform.amd64 elif defined(amd64): CpuPlatform.amd64
elif defined(mips): CpuPlatform.mips elif defined(mips): CpuPlatform.mips
elif defined(mipsel): CpuPlatform.mipsel
elif defined(arm): CpuPlatform.arm elif defined(arm): CpuPlatform.arm
elif defined(arm64): CpuPlatform.arm64
elif defined(vm): CpuPlatform.vm elif defined(vm): CpuPlatform.vm
elif defined(avr): CpuPlatform.avr elif defined(avr): CpuPlatform.avr
else: CpuPlatform.none else: CpuPlatform.none

View file

@ -256,7 +256,10 @@ when not defined(useNimRtl):
of tyBool: add result, reprBool(cast[ptr bool](p)[]) of tyBool: add result, reprBool(cast[ptr bool](p)[])
of tyChar: add result, reprChar(cast[ptr char](p)[]) of tyChar: add result, reprChar(cast[ptr char](p)[])
of tyString: reprStrAux(result, cast[ptr string](p)[]) of tyString: reprStrAux(result, cast[ptr string](p)[])
of tyCString: reprStrAux(result, $(cast[ptr cstring](p)[])) of tyCString:
let cs = cast[ptr cstring](p)[]
if cs.isNil: add result, "nil"
else: reprStrAux(result, $cs)
of tyRange: reprAux(result, p, typ.base, cl) of tyRange: reprAux(result, p, typ.base, cl)
of tyProc, tyPointer: of tyProc, tyPointer:
if cast[PPointer](p)[] == nil: add result, "nil" if cast[PPointer](p)[] == nil: add result, "nil"

View file

@ -19,9 +19,9 @@ proc countBits32(n: int32): int {.compilerproc.} =
v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32) v = (v and 0x33333333'i32) +% ((v shr 2'i32) and 0x33333333'i32)
result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32 result = ((v +% (v shr 4'i32) and 0xF0F0F0F'i32) *% 0x1010101'i32) shr 24'i32
proc countBits64(n: int64): int {.compilerproc.} = proc countBits64(n: int64): int {.compilerproc.} =
result = countBits32(toU32(n and 0xffff'i64)) + result = countBits32(toU32(n and 0xffffffff'i64)) +
countBits32(toU32(n shr 16'i64)) countBits32(toU32(n shr 32'i64))
proc cardSet(s: NimSet, len: int): int {.compilerproc.} = proc cardSet(s: NimSet, len: int): int {.compilerproc.} =
result = 0 result = 0

View file

@ -0,0 +1,8 @@
# bug #3171
template newDataWindow(): stmt =
let eventClosure = proc (closure: pointer): bool {.closure, cdecl.} =
discard
newDataWindow()

View file

@ -2,344 +2,344 @@ discard """
output: "Success" output: "Success"
""" """
# Ref: # Ref:
# http://nim-lang.org/macros.html # http://nim-lang.org/macros.html
# http://nim-lang.org/parseutils.html # http://nim-lang.org/parseutils.html
# Imports # Imports
import tables, parseutils, macros, strutils import tables, parseutils, macros, strutils
import annotate import annotate
export annotate export annotate
# Fields # Fields
const identChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'} const identChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
# Procedure Declarations # Procedure Declarations
proc parse_template(node: NimNode, value: string) {.compiletime.} proc parse_template(node: NimNode, value: string) {.compiletime.}
# Procedure Definitions # Procedure Definitions
proc substring(value: string, index: int, length = -1): string {.compiletime.} = proc substring(value: string, index: int, length = -1): string {.compiletime.} =
## Returns a string at most `length` characters long, starting at `index`. ## Returns a string at most `length` characters long, starting at `index`.
return if length < 0: value.substr(index) return if length < 0: value.substr(index)
elif length == 0: "" elif length == 0: ""
else: value.substr(index, index + length-1) else: value.substr(index, index + length-1)
proc parse_thru_eol(value: string, index: int): int {.compiletime.} = proc parse_thru_eol(value: string, index: int): int {.compiletime.} =
## Reads until and past the end of the current line, unless ## Reads until and past the end of the current line, unless
## a non-whitespace character is encountered first ## a non-whitespace character is encountered first
var remainder: string var remainder: string
var read = value.parseUntil(remainder, {0x0A.char}, index) var read = value.parseUntil(remainder, {0x0A.char}, index)
if remainder.skipWhitespace() == read: if remainder.skipWhitespace() == read:
return read + 1 return read + 1
proc trim_after_eol(value: var string) {.compiletime.} = proc trim_after_eol(value: var string) {.compiletime.} =
## Trims any whitespace at end after \n ## Trims any whitespace at end after \n
var toTrim = 0 var toTrim = 0
for i in countdown(value.len-1, 0): for i in countdown(value.len-1, 0):
# If \n, return # If \n, return
if value[i] in [' ', '\t']: inc(toTrim) if value[i] in [' ', '\t']: inc(toTrim)
else: break else: break
if toTrim > 0: if toTrim > 0:
value = value.substring(0, value.len - toTrim) value = value.substring(0, value.len - toTrim)
proc trim_eol(value: var string) {.compiletime.} = proc trim_eol(value: var string) {.compiletime.} =
## Removes everything after the last line if it contains nothing but whitespace ## Removes everything after the last line if it contains nothing but whitespace
for i in countdown(value.len - 1, 0): for i in countdown(value.len - 1, 0):
# If \n, trim and return # If \n, trim and return
if value[i] == 0x0A.char: if value[i] == 0x0A.char:
value = value.substr(0, i) value = value.substr(0, i)
break break
# This is the first character # This is the first character
if i == 0: if i == 0:
value = "" value = ""
break break
# Skip change # Skip change
if not (value[i] in [' ', '\t']): break if not (value[i] in [' ', '\t']): break
proc detect_indent(value: string, index: int): int {.compiletime.} = proc detect_indent(value: string, index: int): int {.compiletime.} =
## Detects how indented the line at `index` is. ## Detects how indented the line at `index` is.
# Seek to the beginning of the line. # Seek to the beginning of the line.
var lastChar = index var lastChar = index
for i in countdown(index, 0): for i in countdown(index, 0):
if value[i] == 0x0A.char: if value[i] == 0x0A.char:
# if \n, return the indentation level # if \n, return the indentation level
return lastChar - i return lastChar - i
elif not (value[i] in [' ', '\t']): elif not (value[i] in [' ', '\t']):
# if non-whitespace char, decrement lastChar # if non-whitespace char, decrement lastChar
dec(lastChar) dec(lastChar)
proc parse_thru_string(value: string, i: var int, strType = '"') {.compiletime.} = proc parse_thru_string(value: string, i: var int, strType = '"') {.compiletime.} =
## Parses until ending " or ' is reached. ## Parses until ending " or ' is reached.
inc(i) inc(i)
if i < value.len-1: if i < value.len-1:
inc(i, value.skipUntil({'\\', strType}, i)) inc(i, value.skipUntil({'\\', strType}, i))
proc parse_to_close(value: string, index: int, open='(', close=')', opened=0): int {.compiletime.} = proc parse_to_close(value: string, index: int, open='(', close=')', opened=0): int {.compiletime.} =
## Reads until all opened braces are closed ## Reads until all opened braces are closed
## ignoring any strings "" or '' ## ignoring any strings "" or ''
var remainder = value.substring(index) var remainder = value.substring(index)
var open_braces = opened var open_braces = opened
result = 0 result = 0
while result < remainder.len: while result < remainder.len:
var c = remainder[result] var c = remainder[result]
if c == open: inc(open_braces) if c == open: inc(open_braces)
elif c == close: dec(open_braces) elif c == close: dec(open_braces)
elif c == '"': remainder.parse_thru_string(result) elif c == '"': remainder.parse_thru_string(result)
elif c == '\'': remainder.parse_thru_string(result, '\'') elif c == '\'': remainder.parse_thru_string(result, '\'')
if open_braces == 0: break if open_braces == 0: break
else: inc(result) else: inc(result)
iterator parse_stmt_list(value: string, index: var int): string = iterator parse_stmt_list(value: string, index: var int): string =
## Parses unguided ${..} block ## Parses unguided ${..} block
var read = value.parse_to_close(index, open='{', close='}') var read = value.parse_to_close(index, open='{', close='}')
var expressions = value.substring(index + 1, read - 1).split({ ';', 0x0A.char }) var expressions = value.substring(index + 1, read - 1).split({ ';', 0x0A.char })
for expression in expressions: for expression in expressions:
let value = expression.strip let value = expression.strip
if value.len > 0: if value.len > 0:
yield value yield value
#Increment index & parse thru EOL #Increment index & parse thru EOL
inc(index, read + 1) inc(index, read + 1)
inc(index, value.parse_thru_eol(index)) inc(index, value.parse_thru_eol(index))
iterator parse_compound_statements(value, identifier: string, index: int): string = iterator parse_compound_statements(value, identifier: string, index: int): string =
## Parses through several statements, i.e. if {} elif {} else {} ## Parses through several statements, i.e. if {} elif {} else {}
## and returns the initialization of each as an empty statement ## and returns the initialization of each as an empty statement
## i.e. if x == 5 { ... } becomes if x == 5: nil. ## i.e. if x == 5 { ... } becomes if x == 5: nil.
template get_next_ident(expected): stmt = template get_next_ident(expected): stmt =
var nextIdent: string var nextIdent: string
discard value.parseWhile(nextIdent, {'$'} + identChars, i) discard value.parseWhile(nextIdent, {'$'} + identChars, i)
var next: string var next: string
var read: int var read: int
if nextIdent == "case": if nextIdent == "case":
# We have to handle case a bit differently # We have to handle case a bit differently
read = value.parseUntil(next, '$', i) read = value.parseUntil(next, '$', i)
inc(i, read) inc(i, read)
yield next.strip(leading=false) & "\n" yield next.strip(leading=false) & "\n"
else: else:
read = value.parseUntil(next, '{', i) read = value.parseUntil(next, '{', i)
if nextIdent in expected: if nextIdent in expected:
inc(i, read) inc(i, read)
# Parse until closing }, then skip whitespace afterwards # Parse until closing }, then skip whitespace afterwards
read = value.parse_to_close(i, open='{', close='}') read = value.parse_to_close(i, open='{', close='}')
inc(i, read + 1) inc(i, read + 1)
inc(i, value.skipWhitespace(i)) inc(i, value.skipWhitespace(i))
yield next & ": nil\n" yield next & ": nil\n"
else: break else: break
var i = index var i = index
while true: while true:
# Check if next statement would be valid, given the identifier # Check if next statement would be valid, given the identifier
if identifier in ["if", "when"]: if identifier in ["if", "when"]:
get_next_ident([identifier, "$elif", "$else"]) get_next_ident([identifier, "$elif", "$else"])
elif identifier == "case": elif identifier == "case":
get_next_ident(["case", "$of", "$elif", "$else"]) get_next_ident(["case", "$of", "$elif", "$else"])
elif identifier == "try": elif identifier == "try":
get_next_ident(["try", "$except", "$finally"]) get_next_ident(["try", "$except", "$finally"])
proc parse_complex_stmt(value, identifier: string, index: var int): NimNode {.compiletime.} = proc parse_complex_stmt(value, identifier: string, index: var int): NimNode {.compiletime.} =
## Parses if/when/try /elif /else /except /finally statements ## Parses if/when/try /elif /else /except /finally statements
# Build up complex statement string # Build up complex statement string
var stmtString = newString(0) var stmtString = newString(0)
var numStatements = 0 var numStatements = 0
for statement in value.parse_compound_statements(identifier, index): for statement in value.parse_compound_statements(identifier, index):
if statement[0] == '$': stmtString.add(statement.substr(1)) if statement[0] == '$': stmtString.add(statement.substr(1))
else: stmtString.add(statement) else: stmtString.add(statement)
inc(numStatements) inc(numStatements)
# Parse stmt string # Parse stmt string
result = parseExpr(stmtString) result = parseExpr(stmtString)
var resultIndex = 0 var resultIndex = 0
# Fast forward a bit if this is a case statement # Fast forward a bit if this is a case statement
if identifier == "case": if identifier == "case":
inc(resultIndex) inc(resultIndex)
while resultIndex < numStatements: while resultIndex < numStatements:
# Detect indentation # Detect indentation
let indent = detect_indent(value, index) let indent = detect_indent(value, index)
# Parse until an open brace `{` # Parse until an open brace `{`
var read = value.skipUntil('{', index) var read = value.skipUntil('{', index)
inc(index, read + 1) inc(index, read + 1)
# Parse through EOL # Parse through EOL
inc(index, value.parse_thru_eol(index)) inc(index, value.parse_thru_eol(index))
# Parse through { .. } # Parse through { .. }
read = value.parse_to_close(index, open='{', close='}', opened=1) read = value.parse_to_close(index, open='{', close='}', opened=1)
# Add parsed sub-expression into body # Add parsed sub-expression into body
var body = newStmtList() var body = newStmtList()
var stmtString = value.substring(index, read) var stmtString = value.substring(index, read)
trim_after_eol(stmtString) trim_after_eol(stmtString)
stmtString = reindent(stmtString, indent) stmtString = reindent(stmtString, indent)
parse_template(body, stmtString) parse_template(body, stmtString)
inc(index, read + 1) inc(index, read + 1)
# Insert body into result # Insert body into result
var stmtIndex = macros.high(result[resultIndex]) var stmtIndex = result[resultIndex].len-1
result[resultIndex][stmtIndex] = body result[resultIndex][stmtIndex] = body
# Parse through EOL again & increment result index # Parse through EOL again & increment result index
inc(index, value.parse_thru_eol(index)) inc(index, value.parse_thru_eol(index))
inc(resultIndex) inc(resultIndex)
proc parse_simple_statement(value: string, index: var int): NimNode {.compiletime.} = proc parse_simple_statement(value: string, index: var int): NimNode {.compiletime.} =
## Parses for/while ## Parses for/while
# Detect indentation # Detect indentation
let indent = detect_indent(value, index) let indent = detect_indent(value, index)
# Parse until an open brace `{` # Parse until an open brace `{`
var splitValue: string var splitValue: string
var read = value.parseUntil(splitValue, '{', index) var read = value.parseUntil(splitValue, '{', index)
result = parseExpr(splitValue & ":nil") result = parseExpr(splitValue & ":nil")
inc(index, read + 1) inc(index, read + 1)
# Parse through EOL # Parse through EOL
inc(index, value.parse_thru_eol(index)) inc(index, value.parse_thru_eol(index))
# Parse through { .. } # Parse through { .. }
read = value.parse_to_close(index, open='{', close='}', opened=1) read = value.parse_to_close(index, open='{', close='}', opened=1)
# Add parsed sub-expression into body # Add parsed sub-expression into body
var body = newStmtList() var body = newStmtList()
var stmtString = value.substring(index, read) var stmtString = value.substring(index, read)
trim_after_eol(stmtString) trim_after_eol(stmtString)
stmtString = reindent(stmtString, indent) stmtString = reindent(stmtString, indent)
parse_template(body, stmtString) parse_template(body, stmtString)
inc(index, read + 1) inc(index, read + 1)
# Insert body into result # Insert body into result
var stmtIndex = macros.high(result) var stmtIndex = result.len-1
result[stmtIndex] = body result[stmtIndex] = body
# Parse through EOL again # Parse through EOL again
inc(index, value.parse_thru_eol(index)) inc(index, value.parse_thru_eol(index))
proc parse_until_symbol(node: NimNode, value: string, index: var int): bool {.compiletime.} = proc parse_until_symbol(node: NimNode, value: string, index: var int): bool {.compiletime.} =
## Parses a string until a $ symbol is encountered, if ## Parses a string until a $ symbol is encountered, if
## two $$'s are encountered in a row, a split will happen ## two $$'s are encountered in a row, a split will happen
## removing one of the $'s from the resulting output ## removing one of the $'s from the resulting output
var splitValue: string var splitValue: string
var read = value.parseUntil(splitValue, '$', index) var read = value.parseUntil(splitValue, '$', index)
var insertionPoint = node.len var insertionPoint = node.len
inc(index, read + 1) inc(index, read + 1)
if index < value.len: if index < value.len:
case value[index] case value[index]
of '$': of '$':
# Check for duplicate `$`, meaning this is an escaped $ # Check for duplicate `$`, meaning this is an escaped $
node.add newCall("add", ident("result"), newStrLitNode("$")) node.add newCall("add", ident("result"), newStrLitNode("$"))
inc(index) inc(index)
of '(': of '(':
# Check for open `(`, which means parse as simple single-line expression. # Check for open `(`, which means parse as simple single-line expression.
trim_eol(splitValue) trim_eol(splitValue)
read = value.parse_to_close(index) + 1 read = value.parse_to_close(index) + 1
node.add newCall("add", ident("result"), node.add newCall("add", ident("result"),
newCall(bindSym"strip", parseExpr("$" & value.substring(index, read))) newCall(bindSym"strip", parseExpr("$" & value.substring(index, read)))
) )
inc(index, read) inc(index, read)
of '{': of '{':
# Check for open `{`, which means open statement list # Check for open `{`, which means open statement list
trim_eol(splitValue) trim_eol(splitValue)
for s in value.parse_stmt_list(index): for s in value.parse_stmt_list(index):
node.add parseExpr(s) node.add parseExpr(s)
else: else:
# Otherwise parse while valid `identChars` and make expression w/ $ # Otherwise parse while valid `identChars` and make expression w/ $
var identifier: string var identifier: string
read = value.parseWhile(identifier, identChars, index) read = value.parseWhile(identifier, identChars, index)
if identifier in ["for", "while"]: if identifier in ["for", "while"]:
## for/while means open simple statement ## for/while means open simple statement
trim_eol(splitValue) trim_eol(splitValue)
node.add value.parse_simple_statement(index) node.add value.parse_simple_statement(index)
elif identifier in ["if", "when", "case", "try"]: elif identifier in ["if", "when", "case", "try"]:
## if/when/case/try means complex statement ## if/when/case/try means complex statement
trim_eol(splitValue) trim_eol(splitValue)
node.add value.parse_complex_stmt(identifier, index) node.add value.parse_complex_stmt(identifier, index)
elif identifier.len > 0: elif identifier.len > 0:
## Treat as simple variable ## Treat as simple variable
node.add newCall("add", ident("result"), newCall("$", ident(identifier))) node.add newCall("add", ident("result"), newCall("$", ident(identifier)))
inc(index, read) inc(index, read)
result = true result = true
# Insert # Insert
if splitValue.len > 0: if splitValue.len > 0:
node.insert insertionPoint, newCall("add", ident("result"), newStrLitNode(splitValue)) node.insert insertionPoint, newCall("add", ident("result"), newStrLitNode(splitValue))
proc parse_template(node: NimNode, value: string) = proc parse_template(node: NimNode, value: string) =
## Parses through entire template, outputing valid ## Parses through entire template, outputing valid
## Nim code into the input `node` AST. ## Nim code into the input `node` AST.
var index = 0 var index = 0
while index < value.len and while index < value.len and
parse_until_symbol(node, value, index): nil parse_until_symbol(node, value, index): nil
macro tmpli*(body: expr): stmt = macro tmpli*(body: expr): stmt =
result = newStmtList() result = newStmtList()
result.add parseExpr("result = \"\"") result.add parseExpr("result = \"\"")
var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal
else: body[1].strVal else: body[1].strVal
parse_template(result, reindent(value)) parse_template(result, reindent(value))
macro tmpl*(body: expr): stmt = macro tmpl*(body: expr): stmt =
result = newStmtList() result = newStmtList()
var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal var value = if body.kind in nnkStrLit..nnkTripleStrLit: body.strVal
else: body[1].strVal else: body[1].strVal
parse_template(result, reindent(value)) parse_template(result, reindent(value))
# Run tests # Run tests
when isMainModule: when isMainModule:
include otests include otests
echo "Success" echo "Success"

View file

@ -0,0 +1,20 @@
discard """
errormsg: "cannot use symbol of kind 'var' as a 'param'"
line: 20
"""
# bug #3158
type
MyData = object
x: int
template newDataWindow(data: ref MyData): stmt =
proc testProc(data: ref MyData) =
echo "Hello, ", data.x
testProc(data)
var d: ref MyData
new(d)
d.x = 10
newDataWindow(d)