trees.nim 7.5 KB

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