trees.nim 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. case key.kind
  121. of nkIdent: result = whichKeyword(key.ident)
  122. of nkSym: result = whichKeyword(key.sym.name)
  123. of nkCast: result = wCast
  124. of nkClosedSymChoice, nkOpenSymChoice:
  125. result = whichPragma(key[0])
  126. else: result = wInvalid
  127. proc isNoSideEffectPragma*(n: PNode): bool =
  128. var k = whichPragma(n)
  129. if k == wCast:
  130. k = whichPragma(n[1])
  131. result = k == wNoSideEffect
  132. proc findPragma*(n: PNode, which: TSpecialWord): PNode =
  133. if n.kind == nkPragma:
  134. for son in n:
  135. if whichPragma(son) == which:
  136. return son
  137. proc effectSpec*(n: PNode, effectType: TSpecialWord): PNode =
  138. for i in 0..<n.len:
  139. var it = n[i]
  140. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  141. result = it[1]
  142. if result.kind notin {nkCurly, nkBracket}:
  143. result = newNodeI(nkCurly, result.info)
  144. result.add(it[1])
  145. return
  146. proc propSpec*(n: PNode, effectType: TSpecialWord): PNode =
  147. for i in 0..<n.len:
  148. var it = n[i]
  149. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  150. return it[1]
  151. proc unnestStmts(n, result: PNode) =
  152. if n.kind == nkStmtList:
  153. for x in items(n): unnestStmts(x, result)
  154. elif n.kind notin {nkCommentStmt, nkNilLit}:
  155. result.add(n)
  156. proc flattenStmts*(n: PNode): PNode =
  157. result = newNodeI(nkStmtList, n.info)
  158. unnestStmts(n, result)
  159. if result.len == 1:
  160. result = result[0]
  161. proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
  162. result = newNodeI(k, n.info, b-a+1)
  163. for i in 0..b-a: result[i] = n[i+a]
  164. proc isTrue*(n: PNode): bool =
  165. n.kind == nkSym and n.sym.kind == skEnumField and n.sym.position != 0 or
  166. n.kind == nkIntLit and n.intVal != 0
  167. proc getRoot*(n: PNode): PSym =
  168. ## ``getRoot`` takes a *path* ``n``. A path is an lvalue expression
  169. ## like ``obj.x[i].y``. The *root* of a path is the symbol that can be
  170. ## determined as the owner; ``obj`` in the example.
  171. case n.kind
  172. of nkSym:
  173. if n.sym.kind in {skVar, skResult, skTemp, skLet, skForVar, skParam}:
  174. result = n.sym
  175. of nkDotExpr, nkBracketExpr, nkHiddenDeref, nkDerefExpr,
  176. nkObjUpConv, nkObjDownConv, nkCheckedFieldExpr, nkHiddenAddr, nkAddr:
  177. result = getRoot(n[0])
  178. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  179. result = getRoot(n[1])
  180. of nkCallKinds:
  181. if getMagic(n) == mSlice: result = getRoot(n[1])
  182. else: discard
  183. proc stupidStmtListExpr*(n: PNode): bool =
  184. for i in 0..<n.len-1:
  185. if n[i].kind notin {nkEmpty, nkCommentStmt}: return false
  186. result = true
  187. proc dontInlineConstant*(orig, cnst: PNode): bool {.inline.} =
  188. # symbols that expand to a complex constant (array, etc.) should not be
  189. # inlined, unless it's the empty array:
  190. result = orig.kind != cnst.kind and
  191. cnst.kind in {nkCurly, nkPar, nkTupleConstr, nkBracket, nkObjConstr} and
  192. cnst.len > ord(cnst.kind == nkObjConstr)