trees.nim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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, 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 sameFloatIgnoreNan(a, b: BiggestFloat): bool {.inline.} =
  25. ## ignores NaN semantics, but ensures 0.0 == -0.0, see #13730
  26. cast[uint64](a) == cast[uint64](b) or a == b
  27. proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool =
  28. if a == b:
  29. result = true
  30. elif (a != nil) and (b != nil) and (a.kind == b.kind):
  31. case a.kind
  32. of nkSym:
  33. if strictSymEquality:
  34. result = a.sym == b.sym
  35. else:
  36. # don't go nuts here: same symbol as string is enough:
  37. result = a.sym.name.id == b.sym.name.id
  38. of nkIdent: result = a.ident.id == b.ident.id
  39. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  40. of nkFloatLit..nkFloat64Lit: result = sameFloatIgnoreNan(a.floatVal, b.floatVal)
  41. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  42. of nkCommentStmt: result = a.comment == b.comment
  43. of nkEmpty, nkNilLit, nkType: result = true
  44. else:
  45. if a.len == b.len:
  46. for i in 0..<a.len:
  47. if not exprStructuralEquivalent(a[i], b[i],
  48. strictSymEquality): return
  49. result = true
  50. proc sameTree*(a, b: PNode): bool =
  51. if a == b:
  52. result = true
  53. elif a != nil and b != nil and a.kind == b.kind:
  54. if a.flags != b.flags: return
  55. if a.info.line != b.info.line: return
  56. if a.info.col != b.info.col:
  57. return #if a.info.fileIndex <> b.info.fileIndex then exit;
  58. case a.kind
  59. of nkSym:
  60. # don't go nuts here: same symbol as string is enough:
  61. result = a.sym.name.id == b.sym.name.id
  62. of nkIdent: result = a.ident.id == b.ident.id
  63. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  64. of nkFloatLit..nkFloat64Lit: result = sameFloatIgnoreNan(a.floatVal, b.floatVal)
  65. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  66. of nkEmpty, nkNilLit, nkType: result = true
  67. else:
  68. if a.len == b.len:
  69. for i in 0..<a.len:
  70. if not sameTree(a[i], b[i]): return
  71. result = true
  72. proc getMagic*(op: PNode): TMagic =
  73. if op == nil: return mNone
  74. case op.kind
  75. of nkCallKinds:
  76. case op[0].kind
  77. of nkSym: result = op[0].sym.magic
  78. else: result = mNone
  79. else: result = mNone
  80. proc isConstExpr*(n: PNode): bool =
  81. const atomKinds = {nkCharLit..nkNilLit} # Char, Int, UInt, Str, Float and Nil literals
  82. n.kind in atomKinds or nfAllConst in n.flags
  83. proc isCaseObj*(n: PNode): bool =
  84. if n.kind == nkRecCase: return true
  85. for i in 0..<n.safeLen:
  86. if n[i].isCaseObj: return true
  87. proc isDeepConstExpr*(n: PNode; preventInheritance = false): bool =
  88. case n.kind
  89. of nkCharLit..nkNilLit:
  90. result = true
  91. of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv:
  92. result = isDeepConstExpr(n[1], preventInheritance)
  93. of nkCurly, nkBracket, nkPar, nkTupleConstr, nkObjConstr, nkClosure, nkRange:
  94. for i in ord(n.kind == nkObjConstr)..<n.len:
  95. if not isDeepConstExpr(n[i], preventInheritance): return false
  96. if n.typ.isNil: result = true
  97. else:
  98. let t = n.typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink, tyOwned})
  99. if t.kind in {tyRef, tyPtr} or tfUnion in t.flags: return false
  100. if t.kind == tyObject:
  101. if preventInheritance and t[0] != nil:
  102. result = false
  103. elif isCaseObj(t.n):
  104. result = false
  105. else:
  106. result = true
  107. else:
  108. result = true
  109. else: discard
  110. proc isRange*(n: PNode): bool {.inline.} =
  111. if n.kind in nkCallKinds:
  112. let callee = n[0]
  113. if (callee.kind == nkIdent and callee.ident.id == ord(wDotDot)) or
  114. (callee.kind == nkSym and callee.sym.name.id == ord(wDotDot)) or
  115. (callee.kind in {nkClosedSymChoice, nkOpenSymChoice} and
  116. callee[1].sym.name.id == ord(wDotDot)):
  117. result = true
  118. proc whichPragma*(n: PNode): TSpecialWord =
  119. let key = if n.kind in nkPragmaCallKinds and n.len > 0: n[0] else: n
  120. if key.kind == nkIdent: result = whichKeyword(key.ident)
  121. proc findPragma*(n: PNode, which: TSpecialWord): PNode =
  122. if n.kind == nkPragma:
  123. for son in n:
  124. if whichPragma(son) == which:
  125. return son
  126. proc effectSpec*(n: PNode, effectType: TSpecialWord): PNode =
  127. for i in 0..<n.len:
  128. var it = n[i]
  129. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  130. result = it[1]
  131. if result.kind notin {nkCurly, nkBracket}:
  132. result = newNodeI(nkCurly, result.info)
  133. result.add(it[1])
  134. return
  135. proc propSpec*(n: PNode, effectType: TSpecialWord): PNode =
  136. for i in 0..<n.len:
  137. var it = n[i]
  138. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  139. return it[1]
  140. proc unnestStmts(n, result: PNode) =
  141. if n.kind == nkStmtList:
  142. for x in items(n): unnestStmts(x, result)
  143. elif n.kind notin {nkCommentStmt, nkNilLit}:
  144. result.add(n)
  145. proc flattenStmts*(n: PNode): PNode =
  146. result = newNodeI(nkStmtList, n.info)
  147. unnestStmts(n, result)
  148. if result.len == 1:
  149. result = result[0]
  150. proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
  151. result = newNodeI(k, n.info, b-a+1)
  152. for i in 0..b-a: result[i] = n[i+a]
  153. proc isTrue*(n: PNode): bool =
  154. n.kind == nkSym and n.sym.kind == skEnumField and n.sym.position != 0 or
  155. n.kind == nkIntLit and n.intVal != 0
  156. proc getRoot*(n: PNode): PSym =
  157. ## ``getRoot`` takes a *path* ``n``. A path is an lvalue expression
  158. ## like ``obj.x[i].y``. The *root* of a path is the symbol that can be
  159. ## determined as the owner; ``obj`` in the example.
  160. case n.kind
  161. of nkSym:
  162. if n.sym.kind in {skVar, skResult, skTemp, skLet, skForVar, skParam}:
  163. result = n.sym
  164. of nkDotExpr, nkBracketExpr, nkHiddenDeref, nkDerefExpr,
  165. nkObjUpConv, nkObjDownConv, nkCheckedFieldExpr:
  166. result = getRoot(n[0])
  167. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  168. result = getRoot(n[1])
  169. of nkCallKinds:
  170. if getMagic(n) == mSlice: result = getRoot(n[1])
  171. else: discard