enumtostr.nim 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import ast, idents, lineinfos, modulegraphs, magicsys
  2. when defined(nimPreviewSlimSystem):
  3. import std/assertions
  4. proc genEnumToStrProc*(t: PType; info: TLineInfo; g: ModuleGraph; idgen: IdGenerator): PSym =
  5. result = newSym(skProc, getIdent(g.cache, "$"), idgen, t.owner, info)
  6. let dest = newSym(skParam, getIdent(g.cache, "e"), idgen, result, info)
  7. dest.typ = t
  8. let res = newSym(skResult, getIdent(g.cache, "result"), idgen, result, info)
  9. res.typ = getSysType(g, info, tyString)
  10. result.typ = newType(tyProc, idgen, t.owner)
  11. result.typ.n = newNodeI(nkFormalParams, info)
  12. rawAddSon(result.typ, res.typ)
  13. result.typ.n.add newNodeI(nkEffectList, info)
  14. result.typ.addParam dest
  15. var body = newNodeI(nkStmtList, info)
  16. var caseStmt = newNodeI(nkCaseStmt, info)
  17. caseStmt.add(newSymNode dest)
  18. # copy the branches over, but replace the fields with the for loop body:
  19. for i in 0..<t.n.len:
  20. assert(t.n[i].kind == nkSym)
  21. var field = t.n[i].sym
  22. let val = if field.ast == nil: field.name.s else: field.ast.strVal
  23. caseStmt.add newTree(nkOfBranch, newIntTypeNode(field.position, t),
  24. newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res), newStrNode(val, info))))
  25. #newIntTypeNode(nkIntLit, field.position, t)
  26. body.add(caseStmt)
  27. var n = newNodeI(nkProcDef, info, bodyPos+2)
  28. for i in 0..<n.len: n[i] = newNodeI(nkEmpty, info)
  29. n[namePos] = newSymNode(result)
  30. n[paramsPos] = result.typ.n
  31. n[bodyPos] = body
  32. n[resultPos] = newSymNode(res)
  33. result.ast = n
  34. incl result.flags, sfFromGeneric
  35. incl result.flags, sfNeverRaises
  36. proc searchObjCaseImpl(obj: PNode; field: PSym): PNode =
  37. case obj.kind
  38. of nkSym:
  39. result = nil
  40. of nkElse, nkOfBranch:
  41. result = searchObjCaseImpl(obj.lastSon, field)
  42. else:
  43. if obj.kind == nkRecCase and obj[0].kind == nkSym and obj[0].sym == field:
  44. result = obj
  45. else:
  46. result = nil
  47. for x in obj:
  48. result = searchObjCaseImpl(x, field)
  49. if result != nil: break
  50. proc searchObjCase(t: PType; field: PSym): PNode =
  51. result = searchObjCaseImpl(t.n, field)
  52. if result == nil and t.baseClass != nil:
  53. result = searchObjCase(t.baseClass.skipTypes({tyAlias, tyGenericInst, tyRef, tyPtr}), field)
  54. doAssert result != nil
  55. proc genCaseObjDiscMapping*(t: PType; field: PSym; info: TLineInfo; g: ModuleGraph; idgen: IdGenerator): PSym =
  56. result = newSym(skProc, getIdent(g.cache, "objDiscMapping"), idgen, t.owner, info)
  57. let dest = newSym(skParam, getIdent(g.cache, "e"), idgen, result, info)
  58. dest.typ = field.typ
  59. let res = newSym(skResult, getIdent(g.cache, "result"), idgen, result, info)
  60. res.typ = getSysType(g, info, tyUInt8)
  61. result.typ = newType(tyProc, idgen, t.owner)
  62. result.typ.n = newNodeI(nkFormalParams, info)
  63. rawAddSon(result.typ, res.typ)
  64. result.typ.n.add newNodeI(nkEffectList, info)
  65. result.typ.addParam dest
  66. var body = newNodeI(nkStmtList, info)
  67. var caseStmt = newNodeI(nkCaseStmt, info)
  68. caseStmt.add(newSymNode dest)
  69. let subObj = searchObjCase(t, field)
  70. for i in 1..<subObj.len:
  71. let ofBranch = subObj[i]
  72. var newBranch = newNodeI(ofBranch.kind, ofBranch.info)
  73. for j in 0..<ofBranch.len-1:
  74. newBranch.add ofBranch[j]
  75. newBranch.add newTree(nkStmtList, newTree(nkFastAsgn, newSymNode(res), newIntNode(nkInt8Lit, i)))
  76. caseStmt.add newBranch
  77. body.add(caseStmt)
  78. var n = newNodeI(nkProcDef, info, bodyPos+2)
  79. for i in 0..<n.len: n[i] = newNodeI(nkEmpty, info)
  80. n[namePos] = newSymNode(result)
  81. n[paramsPos] = result.typ.n
  82. n[bodyPos] = body
  83. n[resultPos] = newSymNode(res)
  84. result.ast = n
  85. incl result.flags, sfFromGeneric
  86. incl result.flags, sfNeverRaises