# # # The Nim Compiler # (c) Copyright 2015 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## This module implements the '.liftLocals' pragma. import strutils, options, ast, msgs, idents, renderer, types, lowerings, lineinfos from pragmas import getPragmaVal from wordrecg import wLiftLocals type Ctx = object partialParam: PSym objType: PType cache: IdentCache idgen: IdGenerator proc interestingVar(s: PSym): bool {.inline.} = result = s.kind in {skVar, skLet, skTemp, skForVar, skResult} and sfGlobal notin s.flags proc lookupOrAdd(c: var Ctx; s: PSym; info: TLineInfo): PNode = let field = addUniqueField(c.objType, s, c.cache, c.idgen) var deref = newNodeI(nkHiddenDeref, info) deref.typ = c.objType deref.add(newSymNode(c.partialParam, info)) result = newNodeI(nkDotExpr, info) result.add(deref) result.add(newSymNode(field)) result.typ = field.typ proc liftLocals(n: PNode; i: int; c: var Ctx) = let it = n[i] case it.kind of nkSym: if interestingVar(it.sym): n[i] = lookupOrAdd(c, it.sym, it.info) of procDefs, nkTypeSection, nkMixinStmt, nkBindStmt: discard else: for i in 0..