trees.nim 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. of nkBracketExpr:
  138. if n.kind notin nkPragmaCallKinds: return wInvalid
  139. result = whichPragma(key[0])
  140. if result notin {wHint, wHintAsError, wWarning, wWarningAsError}:
  141. # note bracket pragmas, see processNote
  142. result = wInvalid
  143. return
  144. else: return wInvalid
  145. if result in nonPragmaWordsLow..nonPragmaWordsHigh:
  146. result = wInvalid
  147. proc isNoSideEffectPragma*(n: PNode): bool =
  148. var k = whichPragma(n)
  149. if k == wCast:
  150. k = whichPragma(n[1])
  151. result = k == wNoSideEffect
  152. proc findPragma*(n: PNode, which: TSpecialWord): PNode =
  153. result = nil
  154. if n.kind == nkPragma:
  155. for son in n:
  156. if whichPragma(son) == which:
  157. return son
  158. proc effectSpec*(n: PNode, effectType: TSpecialWord): PNode =
  159. result = nil
  160. for i in 0..<n.len:
  161. var it = n[i]
  162. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  163. result = it[1]
  164. if result.kind notin {nkCurly, nkBracket}:
  165. result = newNodeI(nkCurly, result.info)
  166. result.add(it[1])
  167. return
  168. proc propSpec*(n: PNode, effectType: TSpecialWord): PNode =
  169. result = nil
  170. for i in 0..<n.len:
  171. var it = n[i]
  172. if it.kind == nkExprColonExpr and whichPragma(it) == effectType:
  173. return it[1]
  174. proc unnestStmts(n, result: PNode) =
  175. if n.kind == nkStmtList:
  176. for x in items(n): unnestStmts(x, result)
  177. elif n.kind notin {nkCommentStmt, nkNilLit}:
  178. result.add(n)
  179. proc flattenStmts*(n: PNode): PNode =
  180. result = newNodeI(nkStmtList, n.info)
  181. unnestStmts(n, result)
  182. if result.len == 1:
  183. result = result[0]
  184. proc extractRange*(k: TNodeKind, n: PNode, a, b: int): PNode =
  185. result = newNodeI(k, n.info, b-a+1)
  186. for i in 0..b-a: result[i] = n[i+a]
  187. proc getRoot*(n: PNode): PSym =
  188. ## ``getRoot`` takes a *path* ``n``. A path is an lvalue expression
  189. ## like ``obj.x[i].y``. The *root* of a path is the symbol that can be
  190. ## determined as the owner; ``obj`` in the example.
  191. case n.kind
  192. of nkSym:
  193. if n.sym.kind in {skVar, skResult, skTemp, skLet, skForVar, skParam}:
  194. result = n.sym
  195. else:
  196. result = nil
  197. of nkDotExpr, nkBracketExpr, nkHiddenDeref, nkDerefExpr,
  198. nkObjUpConv, nkObjDownConv, nkCheckedFieldExpr, nkHiddenAddr, nkAddr:
  199. result = getRoot(n[0])
  200. of nkHiddenStdConv, nkHiddenSubConv, nkConv:
  201. result = getRoot(n[1])
  202. of nkCallKinds:
  203. if getMagic(n) == mSlice: result = getRoot(n[1])
  204. else: result = nil
  205. else: result = nil
  206. proc stupidStmtListExpr*(n: PNode): bool =
  207. for i in 0..<n.len-1:
  208. if n[i].kind notin {nkEmpty, nkCommentStmt}: return false
  209. result = true
  210. proc dontInlineConstant*(orig, cnst: PNode): bool {.inline.} =
  211. # symbols that expand to a complex constant (array, etc.) should not be
  212. # inlined, unless it's the empty array:
  213. result = cnst.kind in {nkCurly, nkPar, nkTupleConstr, nkBracket, nkObjConstr} and
  214. cnst.len > ord(cnst.kind == nkObjConstr)
  215. proc isRunnableExamples*(n: PNode): bool =
  216. # Templates and generics don't perform symbol lookups.
  217. result = n.kind == nkSym and n.sym.magic == mRunnableExamples or
  218. n.kind == nkIdent and n.ident.id == ord(wRunnableExamples)
  219. proc skipAddr*(n: PNode): PNode {.inline.} =
  220. result = if n.kind in {nkAddr, nkHiddenAddr}: n[0] else: n