trees.nim 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # tree helper routines
  10. import
  11. ast, astalgo, lexer, msgs, strutils, wordrecg, idents
  12. proc cyclicTreeAux(n: PNode, visited: var seq[PNode]): bool =
  13. if n == nil: return
  14. for v in visited:
  15. if v == n: return true
  16. if not (n.kind in {nkEmpty..nkNilLit}):
  17. visited.add(n)
  18. for nSon in n.sons:
  19. if cyclicTreeAux(nSon, visited): return true
  20. discard visited.pop()
  21. proc cyclicTree*(n: PNode): bool =
  22. var visited: seq[PNode] = @[]
  23. cyclicTreeAux(n, visited)
  24. proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool =
  25. if a == b:
  26. result = true
  27. elif (a != nil) and (b != nil) and (a.kind == b.kind):
  28. case a.kind
  29. of nkSym:
  30. if strictSymEquality:
  31. result = a.sym == b.sym
  32. else:
  33. # don't go nuts here: same symbol as string is enough:
  34. result = a.sym.name.id == b.sym.name.id
  35. of nkIdent: result = a.ident.id == b.ident.id
  36. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  37. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  38. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  39. of nkCommentStmt: result = a.comment == b.comment
  40. of nkEmpty, nkNilLit, nkType: result = true
  41. else:
  42. if sonsLen(a) == sonsLen(b):
  43. for i in countup(0, sonsLen(a) - 1):
  44. if not exprStructuralEquivalent(a.sons[i], b.sons[i],
  45. strictSymEquality): return
  46. result = true
  47. proc sameTree*(a, b: PNode): bool =
  48. if a == b:
  49. result = true
  50. elif a != nil and b != nil and a.kind == b.kind:
  51. if a.flags != b.flags: return
  52. if a.info.line != b.info.line: return
  53. if a.info.col != b.info.col:
  54. return #if a.info.fileIndex <> b.info.fileIndex then exit;
  55. case a.kind
  56. of nkSym:
  57. # don't go nuts here: same symbol as string is enough:
  58. result = a.sym.name.id == b.sym.name.id
  59. of nkIdent: result = a.ident.id == b.ident.id
  60. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  61. of nkFloatLit..nkFloat64Lit: result = a.floatVal == b.floatVal
  62. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  63. of nkEmpty, nkNilLit, nkType: result = true
  64. else:
  65. if sonsLen(a) == sonsLen(b):
  66. for i in countup(0, sonsLen(a) - 1):
  67. if not sameTree(a.sons[i], b.sons[i]): return
  68. result = true
  69. proc getMagic*(op: PNode): TMagic =
  70. case op.kind
  71. of nkCallKinds:
  72. case op.sons[0].kind
  73. of nkSym: result = op.sons[0].sym.magic
  74. else: result = mNone
  75. else: result = mNone
  76. proc isConstExpr*(n: PNode): bool =
  77. const atomKinds = {nkCharLit..nkNilLit} # Char, Int, UInt, Str, Float and Nil literals
  78. n.kind in atomKinds or nfAllConst in n.flags
  79. proc isCaseObj*(n: PNode): bool =
  80. if n.kind == nkRecCase: return true
  81. for i in 0..<safeLen(n):
  82. if n[i].isCaseObj: return true
  83. proc isDeepConstExpr*(n: PNode): bool =
  84. case n.kind
  85. of nkCharLit..nkInt64Lit, nkStrLit..nkTripleStrLit,
  86. nkFloatLit..nkFloat64Lit, nkNilLit:
  87. result = true
  88. of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv:
  89. result = isDeepConstExpr(n.sons[1])
  90. of nkCurly, nkBracket, nkPar, nkTupleConstr, nkObjConstr, nkClosure, nkRange:
  91. for i in ord(n.kind == nkObjConstr) ..< n.len:
  92. if not isDeepConstExpr(n.sons[i]): return false
  93. if n.typ.isNil: result = true
  94. else:
  95. let t = n.typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink})
  96. if t.kind in {tyRef, tyPtr}: return false
  97. if t.kind != tyObject or not isCaseObj(t.n):
  98. result = true
  99. else: discard
  100. proc isRange*(n: PNode): bool {.inline.} =
  101. if n.kind in nkCallKinds:
  102. let callee = n[0]
  103. if (callee.kind == nkIdent and callee.ident.id == ord(wDotDot)) or
  104. (callee.kind == nkSym and callee.sym.name.id == ord(wDotDot)) or
  105. (callee.kind in {nkClosedSymChoice, nkOpenSymChoice} and
  106. callee[1].sym.name.id == ord(wDotDot)):
  107. result = true
  108. proc whichPragma*(n: PNode): TSpecialWord =
  109. let key = if n.kind in nkPragmaCallKinds and n.len > 0: n.sons[0] else: n
  110. if key.kind == nkIdent: result = whichKeyword(key.ident)
  111. proc unnestStmts(n, result: PNode) =
  112. if n.kind == nkStmtList:
  113. for x in items(n): unnestStmts(x, result)
  114. elif n.kind notin {nkCommentStmt, nkNilLit}:
  115. result.add(n)
  116. proc flattenStmts*(n: PNode): PNode =
  117. result = newNodeI(nkStmtList, n.info)
  118. unnestStmts(n, result)
  119. if result.len == 1:
  120. result = result.sons[0]
  121. proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
  122. result = newNodeI(k, n.info, b-a+1)
  123. for i in 0 .. b-a: result.sons[i] = n.sons[i+a]