enumtostr.nim 3.5 KB

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