locals.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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(nkPar, 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 walkScopes(c.currentScope):
  20. if scope == c.topLevelScope: break
  21. for it in items(scope.symbols):
  22. if it.kind in skLocalVars and
  23. it.typ.skipTypes({tyGenericInst, tyVar}).kind notin
  24. {tyVarargs, tyOpenArray, tyTypeDesc, tyStatic, tyUntyped, tyTyped, tyEmpty}:
  25. if it.owner == owner:
  26. var field = newSym(skField, it.name, owner, n.info)
  27. field.typ = it.typ.skipTypes({tyVar})
  28. field.position = counter
  29. inc(counter)
  30. tupleType.n.add newSymNode(field)
  31. addSonSkipIntLit(tupleType, field.typ)
  32. var a = newSymNode(it, result.info)
  33. if it.typ.skipTypes({tyGenericInst}).kind == tyVar: a = newDeref(a)
  34. result.add(a)