A motivating example for the new bindSym behavior.

The example is a proof-of-concept logging library, allowing you to define
lexically-scoped environments where certain logging attributes are applied
automatically to all logging statements.

fixes tmacro1 (use of `bindSym` inside static blocks)
This commit is contained in:
Zahary Karadjov 2018-04-19 19:16:05 +03:00 • committed by Andreas Rumpf
commit 2b8bf8fc4a
5 changed files with 160 additions and 1 deletions

View file

@ -89,6 +89,7 @@ type
ambiguousSymbols*: IntSet # ids of all ambiguous symbols (cannot
# store this info in the syms themselves!)
inGenericContext*: int # > 0 if we are in a generic type
inStaticContext*: int # > 0 if we are inside a static: block
inUnrolledContext*: int # > 0 if we are unrolling a loop
compilesContextId*: int # > 0 if we are in a ``compiles`` magic
compilesContextIdGenerator*: int

View file

@ -1780,6 +1780,7 @@ proc tryExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
let oldInGenericContext = c.inGenericContext
let oldInUnrolledContext = c.inUnrolledContext
let oldInGenericInst = c.inGenericInst
let oldInStaticContext = c.inStaticContext
let oldProcCon = c.p
c.generics = @[]
var err: string
@ -1794,6 +1795,7 @@ proc tryExpr(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
c.inGenericContext = oldInGenericContext
c.inUnrolledContext = oldInUnrolledContext
c.inGenericInst = oldInGenericInst
c.inStaticContext = oldInStaticContext
c.p = oldProcCon
msgs.setInfoContextLen(oldContextLen)
setLen(c.graph.owners, oldOwnerLen)

View file

@ -199,7 +199,7 @@ proc semBindSym(c: PContext, n: PNode): PNode =
if s != nil:
# we need to mark all symbols:
var sc = symChoice(c, id, s, TSymChoiceRule(isMixin.intVal))
if not getCurrOwner(c).isCompileTimeProc:
if not (c.inStaticContext > 0 or getCurrOwner(c).isCompileTimeProc):
# inside regular code, bindSym resolves to the sym-choice
# nodes (see tinspectsymbol)
return sc

View file

@ -1754,7 +1754,9 @@ proc semPragmaBlock(c: PContext, n: PNode): PNode =
proc semStaticStmt(c: PContext, n: PNode): PNode =
#echo "semStaticStmt"
#writeStackTrace()
inc c.inStaticContext
let a = semStmt(c, n.sons[0])
dec c.inStaticContext
n.sons[0] = a
evalStaticStmt(c.module, c.cache, c.graph.config, a, c.p.owner)
result = newNodeI(nkDiscardStmt, n.info, 1)