trees.nim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 0 ..< sonsLen(a):
  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 0 ..< sonsLen(a):
  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..nkNilLit:
  86. result = true
  87. of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv:
  88. result = isDeepConstExpr(n.sons[1])
  89. of nkCurly, nkBracket, nkPar, nkTupleConstr, nkObjConstr, nkClosure, nkRange:
  90. for i in ord(n.kind == nkObjConstr) ..< n.len:
  91. if not isDeepConstExpr(n.sons[i]): return false
  92. if n.typ.isNil: result = true
  93. else:
  94. let t = n.typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink, tyOwned})
  95. if t.kind in {tyRef, tyPtr}: return false
  96. if t.kind != tyObject or not isCaseObj(t.n):
  97. result = true
  98. else: discard
  99. proc isRange*(n: PNode): bool {.inline.} =
  100. if n.kind in nkCallKinds:
  101. let callee = n[0]
  102. if (callee.kind == nkIdent and callee.ident.id == ord(wDotDot)) or
  103. (callee.kind == nkSym and callee.sym.name.id == ord(wDotDot)) or
  104. (callee.kind in {nkClosedSymChoice, nkOpenSymChoice} and
  105. callee[1].sym.name.id == ord(wDotDot)):
  106. result = true
  107. proc whichPragma*(n: PNode): TSpecialWord =
  108. let key = if n.kind in nkPragmaCallKinds and n.len > 0: n.sons[0] else: n
  109. if key.kind == nkIdent: result = whichKeyword(key.ident)
  110. proc findPragma*(n: PNode, which: TSpecialWord): PNode =
  111. if n.kind == nkPragma:
  112. for son in n:
  113. if whichPragma(son) == which:
  114. return son
  115. proc effectSpec*(n: PNode, effectType: TSpecialWord): PNode =
  116. for i in 0 ..< sonsLen(n):
  117. var it = n.sons[i]
  118. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  119. result = it.sons[1]
  120. if result.kind notin {nkCurly, nkBracket}:
  121. result = newNodeI(nkCurly, result.info)
  122. result.add(it.sons[1])
  123. return
  124. proc unnestStmts(n, result: PNode) =
  125. if n.kind == nkStmtList:
  126. for x in items(n): unnestStmts(x, result)
  127. elif n.kind notin {nkCommentStmt, nkNilLit}:
  128. result.add(n)
  129. proc flattenStmts*(n: PNode): PNode =
  130. result = newNodeI(nkStmtList, n.info)
  131. unnestStmts(n, result)
  132. if result.len == 1:
  133. result = result.sons[0]
  134. proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
  135. result = newNodeI(k, n.info, b-a+1)
  136. for i in 0 .. b-a: result.sons[i] = n.sons[i+a]