diff options
author | Zahary Karadjov <zahary@gmail.com> | 2012-04-05 19:20:52 +0300 |
---|---|---|
committer | Zahary Karadjov <zahary@gmail.com> | 2012-04-05 20:43:22 +0300 |
commit | 6d80583d5a0e776fc9f4214fb96a532f3ebd5550 (patch) | |
tree | df8b965fc8a48c8656f4acc7fc70940646b3cc60 | |
parent | 8d698b2bdd63cb7390a418d9ebb3ee7fdc7ea3b5 (diff) | |
download | Nim-6d80583d5a0e776fc9f4214fb96a532f3ebd5550.tar.gz |
Added ``global`` pragma that can be used to introduce new global variables from within procs
-rwxr-xr-x | compiler/ccgstmts.nim | 13 | ||||
-rwxr-xr-x | compiler/options.nim | 2 | ||||
-rwxr-xr-x | compiler/pragmas.nim | 5 | ||||
-rwxr-xr-x | compiler/semexprs.nim | 6 | ||||
-rwxr-xr-x | compiler/wordrecg.nim | 4 | ||||
-rwxr-xr-x | web/news.txt | 2 |
6 files changed, 22 insertions, 10 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim index 9c0d52221..f627ee036 100755 --- a/compiler/ccgstmts.nim +++ b/compiler/ccgstmts.nim @@ -7,6 +7,8 @@ # distribution, for details about the copyright. # +# included from cgen.nim + const RangeExpandLimit = 256 # do not generate ranges # over 'RangeExpandLimit' elements @@ -48,16 +50,19 @@ proc loadInto(p: BProc, le, ri: PNode, a: var TLoc) {.inline.} = proc genSingleVar(p: BProc, a: PNode) = var v = a.sons[0].sym if sfCompileTime in v.flags: return + var targetProc = p var immediateAsgn = a.sons[2].kind != nkEmpty if sfGlobal in v.flags: - assignGlobalVar(p, v) - genObjectInit(p, cpsInit, v.typ, v.loc, true) + targetProc = p.module.initProc + assignGlobalVar(targetProc, v) + genObjectInit(targetProc, cpsInit, v.typ, v.loc, true) else: assignLocalVar(p, v) initLocalVar(p, v, immediateAsgn) + if immediateAsgn: - genLineDir(p, a) - loadInto(p, a.sons[0], a.sons[2], v.loc) + genLineDir(targetProc, a) + loadInto(targetProc, a.sons[0], a.sons[2], v.loc) proc genClosureVar(p: BProc, a: PNode) = var immediateAsgn = a.sons[2].kind != nkEmpty diff --git a/compiler/options.nim b/compiler/options.nim index edea2288d..0d7763be0 100755 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -221,4 +221,4 @@ proc binaryStrSearch*(x: openarray[string], y: string): int = # Can we keep this? I'm using it all the time template nimdbg*: expr = c.filename.endsWith"nimdbg.nim" - +template cnimdbg*: expr = p.module.filename.endsWith"nimdbg.nim" diff --git a/compiler/pragmas.nim b/compiler/pragmas.nim index 1ceacfc1c..7e632d858 100755 --- a/compiler/pragmas.nim +++ b/compiler/pragmas.nim @@ -51,7 +51,7 @@ const wImportcpp, wImportobjc, wError} varPragmas* = {wImportc, wExportc, wVolatile, wRegister, wThreadVar, wNodecl, wMagic, wHeader, wDeprecated, wCompilerProc, wDynLib, wExtern, - wImportcpp, wImportobjc, wError, wNoInit, wCompileTime} + wImportcpp, wImportobjc, wError, wNoInit, wCompileTime, wGlobal} constPragmas* = {wImportc, wExportc, wHeader, wDeprecated, wMagic, wNodecl, wExtern, wImportcpp, wImportobjc, wError} letPragmas* = varPragmas @@ -494,6 +494,9 @@ proc pragma(c: PContext, sym: PSym, n: PNode, validPragmas: TSpecialWords) = noVal(it) incl(sym.flags, sfCompileTime) incl(sym.loc.Flags, lfNoDecl) + of wGlobal: + noVal(it) + incl(sym.flags, sfGlobal) of wMerge: noval(it) incl(sym.flags, sfMerge) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 0282c6c53..2475492cc 100755 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -409,8 +409,10 @@ proc isAssignable(c: PContext, n: PNode): TAssignableResult = of nkSym: # don't list 'skLet' here: if n.sym.kind in {skVar, skResult, skTemp}: - if c.p.owner.id == n.sym.owner.id: result = arLocalLValue - else: result = arLValue + if c.p.owner.id == n.sym.owner.id and sfGlobal notin n.sym.flags: + result = arLocalLValue + else: + result = arLValue of nkDotExpr: if skipTypes(n.sons[0].typ, abstractInst).kind in {tyVar, tyPtr, tyRef}: result = arLValue diff --git a/compiler/wordrecg.nim b/compiler/wordrecg.nim index a29f3e762..f96ba7751 100755 --- a/compiler/wordrecg.nim +++ b/compiler/wordrecg.nim @@ -58,7 +58,7 @@ type wWatchPoint, wSubsChar, wAcyclic, wShallow, wUnroll, wLinearScanEnd, wWrite, wPutEnv, wPrependEnv, wAppendEnv, wThreadVar, wEmit, wNoStackFrame, - wImplicitStatic + wImplicitStatic, wGlobal TSpecialWords* = set[TSpecialWord] @@ -107,7 +107,7 @@ const "watchpoint", "subschar", "acyclic", "shallow", "unroll", "linearscanend", "write", "putenv", "prependenv", "appendenv", "threadvar", "emit", - "nostackframe", "implicitstatic"] + "nostackframe", "implicitstatic", "global"] proc findStr*(a: openarray[string], s: string): int = for i in countup(low(a), high(a)): diff --git a/web/news.txt b/web/news.txt index 897961c25..fd2a50606 100755 --- a/web/news.txt +++ b/web/news.txt @@ -57,6 +57,8 @@ Language Additions - Added explicit ``static`` sections for enforced compile time evaluation. - ``addr`` is now treated like a prefix operator syntactically. +- Added ``global`` pragma that can be used to introduce new global variables + from within procs. 2012-02-09 Version 0.8.14 released |