locals.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 ".." / [pluginsupport, 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. # for now we skip openarrays ...
  18. for scope in walkScopes(c.currentScope):
  19. if scope == c.topLevelScope: break
  20. for it in items(scope.symbols):
  21. # XXX parameters' owners are wrong for generics; this caused some pain
  22. # for closures too; we should finally fix it.
  23. #if it.owner != c.p.owner: return result
  24. if it.kind in skLocalVars and
  25. it.typ.skipTypes({tyGenericInst, tyVar}).kind notin
  26. {tyVarargs, tyOpenArray, tyTypeDesc, tyStatic, tyExpr, tyStmt, tyEmpty}:
  27. var field = newSym(skField, it.name, getCurrOwner(c), n.info)
  28. field.typ = it.typ.skipTypes({tyVar})
  29. field.position = counter
  30. inc(counter)
  31. addSon(tupleType.n, newSymNode(field))
  32. addSonSkipIntLit(tupleType, field.typ)
  33. var a = newSymNode(it, result.info)
  34. if it.typ.skipTypes({tyGenericInst}).kind == tyVar: a = newDeref(a)
  35. result.add(a)