trees.nim 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. result = false
  14. if n == nil: return
  15. for v in visited:
  16. if v == n: return true
  17. if not (n.kind in {nkEmpty..nkNilLit}):
  18. visited.add(n)
  19. for nSon in n.sons:
  20. if cyclicTreeAux(nSon, visited): return true
  21. discard visited.pop()
  22. proc cyclicTree*(n: PNode): bool =
  23. var visited: seq[PNode] = @[]
  24. cyclicTreeAux(n, visited)
  25. proc sameFloatIgnoreNan(a, b: BiggestFloat): bool {.inline.} =
  26. ## ignores NaN semantics, but ensures 0.0 == -0.0, see #13730
  27. cast[uint64](a) == cast[uint64](b) or a == b
  28. proc exprStructuralEquivalent*(a, b: PNode; strictSymEquality=false): bool =
  29. if a == b:
  30. result = true
  31. elif (a != nil) and (b != nil) and (a.kind == b.kind):
  32. case a.kind
  33. of nkSym:
  34. if strictSymEquality:
  35. result = a.sym == b.sym
  36. else:
  37. # don't go nuts here: same symbol as string is enough:
  38. result = a.sym.name.id == b.sym.name.id
  39. of nkIdent: result = a.ident.id == b.ident.id
  40. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  41. of nkFloatLit..nkFloat64Lit: result = sameFloatIgnoreNan(a.floatVal, b.floatVal)
  42. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  43. of nkCommentStmt: result = a.comment == b.comment
  44. of nkEmpty, nkNilLit, nkType: result = true
  45. else:
  46. if a.len == b.len:
  47. for i in 0..<a.len:
  48. if not exprStructuralEquivalent(a[i], b[i],
  49. strictSymEquality): return
  50. result = true
  51. else:
  52. result = false
  53. else:
  54. result = false
  55. proc sameTree*(a, b: PNode): bool =
  56. result = false
  57. if a == b:
  58. result = true
  59. elif a != nil and b != nil and a.kind == b.kind:
  60. if a.flags != b.flags: return
  61. if a.info.line != b.info.line: return
  62. if a.info.col != b.info.col:
  63. return #if a.info.fileIndex <> b.info.fileIndex then exit;
  64. case a.kind
  65. of nkSym:
  66. # don't go nuts here: same symbol as string is enough:
  67. result = a.sym.name.id == b.sym.name.id
  68. of nkIdent: result = a.ident.id == b.ident.id
  69. of nkCharLit..nkUInt64Lit: result = a.intVal == b.intVal
  70. of nkFloatLit..nkFloat64Lit: result = sameFloatIgnoreNan(a.floatVal, b.floatVal)
  71. of nkStrLit..nkTripleStrLit: result = a.strVal == b.strVal
  72. of nkEmpty, nkNilLit, nkType: result = true
  73. else:
  74. if a.len == b.len:
  75. for i in 0..<a.len:
  76. if not sameTree(a[i], b[i]): return
  77. result = true
  78. proc getMagic*(op: PNode): TMagic =
  79. if op == nil: return mNone
  80. case op.kind
  81. of nkCallKinds:
  82. case op[0].kind
  83. of nkSym: result = op[0].sym.magic
  84. else: result = mNone
  85. else: result = mNone
  86. proc isConstExpr*(n: PNode): bool =
  87. const atomKinds = {nkCharLit..nkNilLit} # Char, Int, UInt, Str, Float and Nil literals
  88. n.kind in atomKinds or nfAllConst in n.flags
  89. proc isCaseObj*(n: PNode): bool =
  90. result = false
  91. if n.kind == nkRecCase: return true
  92. for i in 0..<n.safeLen:
  93. if n[i].isCaseObj: return true
  94. proc isDeepConstExpr*(n: PNode; preventInheritance = false): bool =
  95. case n.kind
  96. of nkCharLit..nkNilLit:
  97. result = true
  98. of nkExprEqExpr, nkExprColonExpr, nkHiddenStdConv, nkHiddenSubConv:
  99. result = isDeepConstExpr(n[1], preventInheritance)
  100. of nkCurly, nkBracket, nkPar, nkTupleConstr, nkObjConstr, nkClosure, nkRange:
  101. for i in ord(n.kind == nkObjConstr)..<n.len:
  102. if not isDeepConstExpr(n[i], preventInheritance): return false
  103. if n.typ.isNil: result = true
  104. else:
  105. let t = n.typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink, tyOwned})
  106. if t.kind in {tyRef, tyPtr} or tfUnion in t.flags: return false
  107. if t.kind == tyObject:
  108. if preventInheritance and t.baseClass != nil:
  109. result = false
  110. elif isCaseObj(t.n):
  111. result = false
  112. else:
  113. result = true
  114. else:
  115. result = true
  116. else: result = false
  117. proc isRange*(n: PNode): bool {.inline.} =
  118. if n.kind in nkCallKinds:
  119. let callee = n[0]
  120. if (callee.kind == nkIdent and callee.ident.id == ord(wDotDot)) or
  121. (callee.kind == nkSym and callee.sym.name.id == ord(wDotDot)) or
  122. (callee.kind in {nkClosedSymChoice, nkOpenSymChoice} and
  123. callee[1].sym.name.id == ord(wDotDot)):
  124. result = true
  125. else:
  126. result = false
  127. else:
  128. result = false
  129. proc whichPragma*(n: PNode): TSpecialWord =
  130. let key = if n.kind in nkPragmaCallKinds and n.len > 0: n[0] else: n
  131. case key.kind
  132. of nkIdent: result = whichKeyword(key.ident)
  133. of nkSym: result = whichKeyword(key.sym.name)
  134. of nkCast: return wCast
  135. of nkClosedSymChoice, nkOpenSymChoice:
  136. return whichPragma(key[0])
  137. else: return wInvalid
  138. if result in nonPragmaWordsLow..nonPragmaWordsHigh:
  139. result = wInvalid
  140. proc isNoSideEffectPragma*(n: PNode): bool =
  141. var k = whichPragma(n)
  142. if k == wCast:
  143. k = whichPragma(n[1])
  144. result = k == wNoSideEffect
  145. proc findPragma*(n: PNode, which: TSpecialWord): PNode =
  146. result = nil
  147. if n.kind == nkPragma:
  148. for son in n:
  149. if whichPragma(son) == which:
  150. return son
  151. proc effectSpec*(n: PNode, effectType: TSpecialWord): PNode =
  152. result = nil
  153. for i in 0..<n.len:
  154. var it = n[i]
  155. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  156. result = it[1]
  157. if result.kind notin {nkCurly, nkBracket}:
  158. result = newNodeI(nkCurly, result.info)
  159. result.add(it[1])
  160. return
  161. proc propSpec*(n: PNode, effectType: TSpecialWord): PNode =
  162. result = nil
  163. for i in 0..<n.len:
  164. var it = n[i]
  165. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  166. return it[1]
  167. proc unnestStmts(n, result: PNode) =
  168. if n.kind == nkStmtList:
  169. for x in items(n): unnestStmts(x, result)
  170. elif n.kind notin {nkCommentStmt, nkNilLit}:
  171. result.add(n)
  172. proc flattenStmts*(n: PNode): PNode =
  173. result = newNodeI(nkStmtList, n.info)
  174. unnestStmts(n, result)
  175. if result.len == 1:
  176. result = result[0]
  177. proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
  178. result = newNodeI(k, n.info, b-a+1)
  179. for i in 0..b-a: result[i] = n[i+a]
  180. proc getRoot*(n: PNode): PSym =
  181. ## ``getRoot`` takes a *path* ``n``. A path is an lvalue expression
  182. ## like ``obj.x[i].y``. The *root* of a path is the symbol that can be
  183. ## determined as the owner; ``obj`` in the example.
  184. case n.kind
  185. of nkSym:
  186. if n.sym.kind in {skVar, skResult, skTemp, skLet, skForVar, skParam}:
  187. result = n.sym
  188. else:
  189. result = nil
  190. of nkDotExpr, nkBracketExpr, nkHiddenDeref, nkDerefExpr,
  191. nkObjUpConv, nkObjDownConv, nkCheckedFieldExpr, nkHiddenAddr, nkAddr:
  192. result = getRoot(n[0])
  193. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  194. result = getRoot(n[1])
  195. of nkCallKinds:
  196. if getMagic(n) == mSlice: result = getRoot(n[1])
  197. else: result = nil
  198. else: result = nil
  199. proc stupidStmtListExpr*(n: PNode): bool =
  200. for i in 0..<n.len-1:
  201. if n[i].kind notin {nkEmpty, nkCommentStmt}: return false
  202. result = true
  203. proc dontInlineConstant*(orig, cnst: PNode): bool {.inline.} =
  204. # symbols that expand to a complex constant (array, etc.) should not be
  205. # inlined, unless it's the empty array:
  206. result = cnst.kind in {nkCurly, nkPar, nkTupleConstr, nkBracket, nkObjConstr} and
  207. cnst.len > ord(cnst.kind == nkObjConstr)
  208. proc isRunnableExamples*(n: PNode): bool =
  209. # Templates and generics don't perform symbol lookups.
  210. result = n.kind == nkSym and n.sym.magic == mRunnableExamples or
  211. n.kind == nkIdent and n.ident.id == ord(wRunnableExamples)
  212. proc skipAddr*(n: PNode): PNode {.inline.} =
  213. result = if n.kind in {nkAddr, nkHiddenAddr}: n[0] else: n