added system.locals

This commit is contained in:
Araq 2013-03-21 21:28:46 +01:00
commit c9ed846a43
7 changed files with 64 additions and 18 deletions

View file

@ -479,7 +479,7 @@ type
mNIntVal, mNFloatVal, mNSymbol, mNIdent, mNGetType, mNStrVal, mNSetIntVal, mNIntVal, mNFloatVal, mNSymbol, mNIdent, mNGetType, mNStrVal, mNSetIntVal,
mNSetFloatVal, mNSetSymbol, mNSetIdent, mNSetType, mNSetStrVal, mNLineInfo, mNSetFloatVal, mNSetSymbol, mNSetIdent, mNSetType, mNSetStrVal, mNLineInfo,
mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mIdentToStr, mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mIdentToStr,
mNBindSym, mNCallSite, mNBindSym, mLocals, mNCallSite,
mEqIdent, mEqNimrodNode, mNHint, mNWarning, mNError, mEqIdent, mEqNimrodNode, mNHint, mNWarning, mNError,
mInstantiationInfo, mGetTypeInfo mInstantiationInfo, mGetTypeInfo

View file

@ -26,6 +26,7 @@ proc semExprWithType(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
#raiseRecoverableError("") #raiseRecoverableError("")
result = errorNode(c, n) result = errorNode(c, n)
if result.typ != nil: if result.typ != nil:
# XXX tyGenericInst here?
if result.typ.kind == tyVar: result = newDeref(result) if result.typ.kind == tyVar: result = newDeref(result)
else: else:
LocalError(n.info, errExprXHasNoType, LocalError(n.info, errExprXHasNoType,

View file

@ -1,7 +1,7 @@
# #
# #
# The Nimrod Compiler # The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf # (c) Copyright 2013 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
@ -73,6 +73,33 @@ proc semBindSym(c: PContext, n: PNode): PNode =
else: else:
LocalError(n.sons[1].info, errUndeclaredIdentifier, sl.strVal) LocalError(n.sons[1].info, errUndeclaredIdentifier, sl.strVal)
proc semLocals(c: PContext, n: PNode): PNode =
var counter = 0
var tupleType = newTypeS(tyTuple, c)
result = newNodeIT(nkPar, n.info, tupleType)
tupleType.n = newNodeI(nkRecList, n.info)
# for now we skip openarrays ...
for i in countdown(c.tab.tos-1, ModuleTablePos+1):
for it in items(c.tab.stack[i]):
# XXX parameters' owners are wrong for generics; this caused some pain
# for closures too; we should finally fix it.
#if it.owner != c.p.owner: return result
if it.kind in skLocalVars and
it.typ.skipTypes({tyGenericInst, tyVar}).kind notin
{tyVarargs, tyOpenArray, tyTypeDesc, tyExpr, tyStmt, tyEmpty}:
var field = newSym(skField, it.name, getCurrOwner(), n.info)
field.typ = it.typ.skipTypes({tyGenericInst, tyVar})
field.position = counter
inc(counter)
addSon(tupleType.n, newSymNode(field))
addSonSkipIntLit(tupleType, field.typ)
var a = newSymNode(it, result.info)
if it.typ.skipTypes({tyGenericInst}).kind == tyVar: a = newDeref(a)
result.add(a)
proc semShallowCopy(c: PContext, n: PNode, flags: TExprFlags): PNode proc semShallowCopy(c: PContext, n: PNode, flags: TExprFlags): PNode
proc magicsAfterOverloadResolution(c: PContext, n: PNode, proc magicsAfterOverloadResolution(c: PContext, n: PNode,
flags: TExprFlags): PNode = flags: TExprFlags): PNode =
@ -86,5 +113,6 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
of mOrd: result = semOrd(c, n) of mOrd: result = semOrd(c, n)
of mShallowCopy: result = semShallowCopy(c, n, flags) of mShallowCopy: result = semShallowCopy(c, n, flags)
of mNBindSym: result = semBindSym(c, n) of mNBindSym: result = semBindSym(c, n)
of mLocals: result = semLocals(c, n)
else: result = n else: result = n

View file

@ -2548,3 +2548,10 @@ proc safeAdd*(x: var string, y: string) =
if x == nil: x = y if x == nil: x = y
else: x.add(y) else: x.add(y)
proc locals*(): TObject {.magic: "Locals", noSideEffect.} =
## generates a tuple constructor expression listing all the local variables
## in the current scope. This is quite fast as it does not rely
## on any debug or runtime information. Note that in constrast to what
## the official signature says, the return type is not ``TObject`` but a
## tuple of a structure that depends on the current scope.
nil

11
tests/run/tlocals.nim Normal file
View file

@ -0,0 +1,11 @@
discard """
output: "(x: string here, a: 1, b: 3)"
"""
proc simple[T](a, b: T) =
var
x = "string here"
echo locals()
simple(1, 3)

View file

@ -13,7 +13,6 @@ version 0.9.2
- acyclic vs prunable; introduce GC hints - acyclic vs prunable; introduce GC hints
- CGEN: ``restrict`` pragma + backend support; computed goto support - CGEN: ``restrict`` pragma + backend support; computed goto support
- document NimMain and check whether it works for threading - document NimMain and check whether it works for threading
- implement ``system.locals`` magic iterator
Bugs Bugs
@ -27,7 +26,6 @@ Bugs
- aporia.nim(968, 5) Error: ambiguous identifier: 'DELETE' -- - aporia.nim(968, 5) Error: ambiguous identifier: 'DELETE' --
use a qualifier use a qualifier
- blocks can "export" an identifier but the CCG generates {} for them ... - blocks can "export" an identifier but the CCG generates {} for them ...
- JS gen: fix exception handling
- osproc execProcesses can deadlock if all processes fail (as experienced - osproc execProcesses can deadlock if all processes fail (as experienced
in c++ mode) in c++ mode)
- bootstrapping does not work in C++ mode - bootstrapping does not work in C++ mode

View file

@ -24,6 +24,7 @@ Library Additions
- There is a new experimental mark&sweep GC which can be faster (or much - There is a new experimental mark&sweep GC which can be faster (or much
slower) than the default GC. Enable with ``--gc:markAndSweep``. slower) than the default GC. Enable with ``--gc:markAndSweep``.
- Added ``system.onRaise`` to support a condition system. - Added ``system.onRaise`` to support a condition system.
- Added ``system.locals`` that provides access to a proc's locals.
- Added ``macros.quote`` for AST quasi-quoting. - Added ``macros.quote`` for AST quasi-quoting.
- Added ``system.unsafeNew`` to support hacky variable length objects. - Added ``system.unsafeNew`` to support hacky variable length objects.
- ``system.fields`` and ``system.fieldPairs`` support ``object`` too; they - ``system.fields`` and ``system.fieldPairs`` support ``object`` too; they