locals.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## The builtin 'system.locals' implemented as a plugin.
  10. import ".." / [ast, astalgo,
  11. magicsys, lookups, semdata, lowerings]
  12. proc semLocals*(c: PContext, n: PNode): PNode =
  13. var counter = 0
  14. var tupleType = newTypeS(tyTuple, c)
  15. result = newNodeIT(nkTupleConstr, n.info, tupleType)
  16. tupleType.n = newNodeI(nkRecList, n.info)
  17. let owner = getCurrOwner(c)
  18. # for now we skip openarrays ...
  19. for scope in localScopesFrom(c, c.currentScope):
  20. for it in items(scope.symbols):
  21. if it.kind in skLocalVars and
  22. it.typ.skipTypes({tyGenericInst, tyVar}).kind notin
  23. {tyVarargs, tyOpenArray, tyTypeDesc, tyStatic, tyUntyped, tyTyped, tyEmpty}:
  24. if it.owner == owner:
  25. var field = newSym(skField, it.name, nextSymId c.idgen, owner, n.info)
  26. field.typ = it.typ.skipTypes({tyVar})
  27. field.position = counter
  28. inc(counter)
  29. tupleType.n.add newSymNode(field)
  30. addSonSkipIntLit(tupleType, field.typ, c.idgen)
  31. var a = newSymNode(it, result.info)
  32. if it.typ.skipTypes({tyGenericInst}).kind == tyVar: a = newDeref(a)
  33. result.add(a)