vmdeps.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. import ast, types, msgs, os, streams, options, idents, lineinfos
  10. proc opSlurp*(file: string, info: TLineInfo, module: PSym; conf: ConfigRef): string =
  11. try:
  12. var filename = parentDir(toFullPath(conf, info)) / file
  13. if not fileExists(filename):
  14. filename = findFile(conf, file).string
  15. result = readFile(filename)
  16. # we produce a fake include statement for every slurped filename, so that
  17. # the module dependencies are accurate:
  18. appendToModule(module, newNode(nkIncludeStmt, info, @[
  19. newStrNode(nkStrLit, filename)]))
  20. except IOError:
  21. localError(conf, info, "cannot open file: " & file)
  22. result = ""
  23. proc atomicTypeX(cache: IdentCache; name: string; m: TMagic; t: PType; info: TLineInfo): PNode =
  24. let sym = newSym(skType, getIdent(cache, name), t.owner, info)
  25. sym.magic = m
  26. sym.typ = t
  27. result = newSymNode(sym)
  28. result.typ = t
  29. proc atomicTypeX(s: PSym; info: TLineInfo): PNode =
  30. result = newSymNode(s)
  31. result.info = info
  32. proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
  33. inst=false; allowRecursionX=false): PNode
  34. proc mapTypeToBracketX(cache: IdentCache; name: string; m: TMagic; t: PType; info: TLineInfo;
  35. inst=false): PNode =
  36. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  37. result.add atomicTypeX(cache, name, m, t, info)
  38. for i in 0 ..< t.len:
  39. if t.sons[i] == nil:
  40. let void = atomicTypeX(cache, "void", mVoid, t, info)
  41. void.typ = newType(tyVoid, t.owner)
  42. result.add void
  43. else:
  44. result.add mapTypeToAstX(cache, t.sons[i], info, inst)
  45. proc objectNode(cache: IdentCache; n: PNode): PNode =
  46. if n.kind == nkSym:
  47. result = newNodeI(nkIdentDefs, n.info)
  48. result.add n # name
  49. result.add mapTypeToAstX(cache, n.sym.typ, n.info, true, false) # type
  50. result.add newNodeI(nkEmpty, n.info) # no assigned value
  51. else:
  52. result = copyNode(n)
  53. for i in 0 ..< n.safeLen:
  54. result.add objectNode(cache, n[i])
  55. proc mapTypeToAstX(cache: IdentCache; t: PType; info: TLineInfo;
  56. inst=false; allowRecursionX=false): PNode =
  57. var allowRecursion = allowRecursionX
  58. template atomicType(name, m): untyped = atomicTypeX(cache, name, m, t, info)
  59. template atomicType(s): untyped = atomicTypeX(s, info)
  60. template mapTypeToAst(t,info): untyped = mapTypeToAstX(cache, t, info, inst)
  61. template mapTypeToAstR(t,info): untyped = mapTypeToAstX(cache, t, info, inst, true)
  62. template mapTypeToAst(t,i,info): untyped =
  63. if i<t.len and t.sons[i]!=nil: mapTypeToAstX(cache, t.sons[i], info, inst)
  64. else: newNodeI(nkEmpty, info)
  65. template mapTypeToBracket(name, m, t, info): untyped =
  66. mapTypeToBracketX(cache, name, m, t, info, inst)
  67. template newNodeX(kind): untyped =
  68. newNodeIT(kind, if t.n.isNil: info else: t.n.info, t)
  69. template newIdentDefs(n,t): untyped =
  70. var id = newNodeX(nkIdentDefs)
  71. id.add n # name
  72. id.add mapTypeToAst(t, info) # type
  73. id.add newNodeI(nkEmpty, info) # no assigned value
  74. id
  75. template newIdentDefs(s): untyped = newIdentDefs(s, s.typ)
  76. if inst:
  77. if t.sym != nil: # if this node has a symbol
  78. if not allowRecursion: # getTypeInst behavior: return symbol
  79. return atomicType(t.sym)
  80. #else: # getTypeImpl behavior: turn off recursion
  81. # allowRecursion = false
  82. case t.kind
  83. of tyNone: result = atomicType("none", mNone)
  84. of tyBool: result = atomicType("bool", mBool)
  85. of tyChar: result = atomicType("char", mChar)
  86. of tyNil: result = atomicType("nil", mNil)
  87. of tyExpr: result = atomicType("expr", mExpr)
  88. of tyStmt: result = atomicType("stmt", mStmt)
  89. of tyVoid: result = atomicType("void", mVoid)
  90. of tyEmpty: result = atomicType("empty", mNone)
  91. of tyUncheckedArray:
  92. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  93. result.add atomicType("UncheckedArray", mUncheckedArray)
  94. result.add mapTypeToAst(t.sons[0], info)
  95. of tyArray:
  96. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  97. result.add atomicType("array", mArray)
  98. if inst and t.sons[0].kind == tyRange:
  99. var rng = newNodeX(nkInfix)
  100. rng.add newIdentNode(getIdent(cache, ".."), info)
  101. rng.add t.sons[0].n.sons[0].copyTree
  102. rng.add t.sons[0].n.sons[1].copyTree
  103. result.add rng
  104. else:
  105. result.add mapTypeToAst(t.sons[0], info)
  106. result.add mapTypeToAst(t.sons[1], info)
  107. of tyTypeDesc:
  108. if t.base != nil:
  109. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  110. result.add atomicType("typeDesc", mTypeDesc)
  111. result.add mapTypeToAst(t.base, info)
  112. else:
  113. result = atomicType("typeDesc", mTypeDesc)
  114. of tyGenericInvocation:
  115. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  116. for i in 0 ..< t.len:
  117. result.add mapTypeToAst(t.sons[i], info)
  118. of tyGenericInst:
  119. if inst:
  120. if allowRecursion:
  121. result = mapTypeToAstR(t.lastSon, info)
  122. else:
  123. result = newNodeX(nkBracketExpr)
  124. #result.add mapTypeToAst(t.lastSon, info)
  125. result.add mapTypeToAst(t[0], info)
  126. for i in 1 ..< t.len-1:
  127. result.add mapTypeToAst(t.sons[i], info)
  128. else:
  129. result = mapTypeToAstX(cache, t.lastSon, info, inst, allowRecursion)
  130. of tyGenericBody:
  131. if inst:
  132. result = mapTypeToAstR(t.lastSon, info)
  133. else:
  134. result = mapTypeToAst(t.lastSon, info)
  135. of tyAlias:
  136. result = mapTypeToAstX(cache, t.lastSon, info, inst, allowRecursion)
  137. of tyOrdinal:
  138. result = mapTypeToAst(t.lastSon, info)
  139. of tyDistinct:
  140. if inst:
  141. result = newNodeX(nkDistinctTy)
  142. result.add mapTypeToAst(t.sons[0], info)
  143. else:
  144. if allowRecursion or t.sym == nil:
  145. result = mapTypeToBracket("distinct", mDistinct, t, info)
  146. else:
  147. result = atomicType(t.sym)
  148. of tyGenericParam, tyForward:
  149. result = atomicType(t.sym)
  150. of tyObject:
  151. if inst:
  152. result = newNodeX(nkObjectTy)
  153. result.add newNodeI(nkEmpty, info) # pragmas not reconstructed yet
  154. if t.sons[0] == nil: result.add newNodeI(nkEmpty, info) # handle parent object
  155. else:
  156. var nn = newNodeX(nkOfInherit)
  157. nn.add mapTypeToAst(t.sons[0], info)
  158. result.add nn
  159. if t.n.len > 0:
  160. result.add objectNode(cache, t.n)
  161. else:
  162. result.add newNodeI(nkEmpty, info)
  163. else:
  164. if allowRecursion or t.sym == nil:
  165. result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t)
  166. result.add newNodeI(nkEmpty, info)
  167. if t.sons[0] == nil:
  168. result.add newNodeI(nkEmpty, info)
  169. else:
  170. result.add mapTypeToAst(t.sons[0], info)
  171. result.add copyTree(t.n)
  172. else:
  173. result = atomicType(t.sym)
  174. of tyEnum:
  175. result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t)
  176. result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum
  177. for c in t.n.sons:
  178. result.add copyTree(c)
  179. of tyTuple:
  180. if inst:
  181. # only named tuples have a node, unnamed tuples don't
  182. if t.n.isNil:
  183. result = newNodeX(nkTupleConstr)
  184. for subType in t.sons:
  185. result.add mapTypeToAst(subType, info)
  186. else:
  187. result = newNodeX(nkTupleTy)
  188. for s in t.n.sons:
  189. result.add newIdentDefs(s)
  190. else:
  191. result = mapTypeToBracket("tuple", mTuple, t, info)
  192. of tySet: result = mapTypeToBracket("set", mSet, t, info)
  193. of tyPtr:
  194. if inst:
  195. result = newNodeX(nkPtrTy)
  196. result.add mapTypeToAst(t.sons[0], info)
  197. else:
  198. result = mapTypeToBracket("ptr", mPtr, t, info)
  199. of tyRef:
  200. if inst:
  201. result = newNodeX(nkRefTy)
  202. result.add mapTypeToAst(t.sons[0], info)
  203. else:
  204. result = mapTypeToBracket("ref", mRef, t, info)
  205. of tyVar:
  206. if inst:
  207. result = newNodeX(nkVarTy)
  208. result.add mapTypeToAst(t.sons[0], info)
  209. else:
  210. result = mapTypeToBracket("var", mVar, t, info)
  211. of tyLent: result = mapTypeToBracket("lent", mBuiltinType, t, info)
  212. of tySink: result = mapTypeToBracket("sink", mBuiltinType, t, info)
  213. of tySequence: result = mapTypeToBracket("seq", mSeq, t, info)
  214. of tyOpt: result = mapTypeToBracket("opt", mOpt, t, info)
  215. of tyProc:
  216. if inst:
  217. result = newNodeX(nkProcTy)
  218. var fp = newNodeX(nkFormalParams)
  219. if t.sons[0] == nil:
  220. fp.add newNodeI(nkEmpty, info)
  221. else:
  222. fp.add mapTypeToAst(t.sons[0], t.n[0].info)
  223. for i in 1..<t.sons.len:
  224. fp.add newIdentDefs(t.n[i], t.sons[i])
  225. result.add fp
  226. result.add if t.n[0].len > 0: t.n[0][pragmasEffects].copyTree
  227. else: newNodeI(nkEmpty, info)
  228. else:
  229. result = mapTypeToBracket("proc", mNone, t, info)
  230. of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info)
  231. of tyRange:
  232. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  233. result.add atomicType("range", mRange)
  234. if inst:
  235. let rng = newNodeX(nkInfix)
  236. rng.add newIdentNode(getIdent(cache, ".."), info)
  237. rng.add t.n.sons[0].copyTree
  238. rng.add t.n.sons[1].copyTree
  239. result.add rng
  240. else:
  241. result.add t.n.sons[0].copyTree
  242. result.add t.n.sons[1].copyTree
  243. of tyPointer: result = atomicType("pointer", mPointer)
  244. of tyString: result = atomicType("string", mString)
  245. of tyCString: result = atomicType("cstring", mCString)
  246. of tyInt: result = atomicType("int", mInt)
  247. of tyInt8: result = atomicType("int8", mInt8)
  248. of tyInt16: result = atomicType("int16", mInt16)
  249. of tyInt32: result = atomicType("int32", mInt32)
  250. of tyInt64: result = atomicType("int64", mInt64)
  251. of tyFloat: result = atomicType("float", mFloat)
  252. of tyFloat32: result = atomicType("float32", mFloat32)
  253. of tyFloat64: result = atomicType("float64", mFloat64)
  254. of tyFloat128: result = atomicType("float128", mFloat128)
  255. of tyUInt: result = atomicType("uint", mUint)
  256. of tyUInt8: result = atomicType("uint8", mUint8)
  257. of tyUInt16: result = atomicType("uint16", mUint16)
  258. of tyUInt32: result = atomicType("uint32", mUint32)
  259. of tyUInt64: result = atomicType("uint64", mUint64)
  260. of tyVarargs: result = mapTypeToBracket("varargs", mVarargs, t, info)
  261. of tyProxy: result = atomicType("error", mNone)
  262. of tyBuiltInTypeClass:
  263. result = mapTypeToBracket("builtinTypeClass", mNone, t, info)
  264. of tyUserTypeClass, tyUserTypeClassInst:
  265. if t.isResolvedUserTypeClass:
  266. result = mapTypeToAst(t.lastSon, info)
  267. else:
  268. result = mapTypeToBracket("concept", mNone, t, info)
  269. result.add t.n.copyTree
  270. of tyCompositeTypeClass:
  271. result = mapTypeToBracket("compositeTypeClass", mNone, t, info)
  272. of tyAnd: result = mapTypeToBracket("and", mAnd, t, info)
  273. of tyOr: result = mapTypeToBracket("or", mOr, t, info)
  274. of tyNot: result = mapTypeToBracket("not", mNot, t, info)
  275. of tyAnything: result = atomicType("anything", mNone)
  276. of tyInferred: assert false
  277. of tyStatic, tyFromExpr:
  278. if inst:
  279. if t.n != nil: result = t.n.copyTree
  280. else: result = atomicType("void", mVoid)
  281. else:
  282. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  283. result.add atomicType("static", mNone)
  284. if t.n != nil:
  285. result.add t.n.copyTree
  286. of tyOptAsRef: assert(false, "mapTypeToAstX")
  287. proc opMapTypeToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  288. result = mapTypeToAstX(cache, t, info, false, true)
  289. # the "Inst" version includes generic parameters in the resulting type tree
  290. # and also tries to look like the corresponding Nim type declaration
  291. proc opMapTypeInstToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  292. result = mapTypeToAstX(cache, t, info, true, false)
  293. # the "Impl" version includes generic parameters in the resulting type tree
  294. # and also tries to look like the corresponding Nim type implementation
  295. proc opMapTypeImplToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  296. result = mapTypeToAstX(cache, t, info, true, true)