vmdeps.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 and not allowRecursion and t.sym != nil:
  77. # getTypeInst behavior: return symbol
  78. return atomicType(t.sym)
  79. case t.kind
  80. of tyNone: result = atomicType("none", mNone)
  81. of tyBool: result = atomicType("bool", mBool)
  82. of tyChar: result = atomicType("char", mChar)
  83. of tyNil: result = atomicType("nil", mNil)
  84. of tyExpr: result = atomicType("expr", mExpr)
  85. of tyStmt: result = atomicType("stmt", mStmt)
  86. of tyVoid: result = atomicType("void", mVoid)
  87. of tyEmpty: result = atomicType("empty", mNone)
  88. of tyUncheckedArray:
  89. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  90. result.add atomicType("UncheckedArray", mUncheckedArray)
  91. result.add mapTypeToAst(t.sons[0], info)
  92. of tyArray:
  93. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  94. result.add atomicType("array", mArray)
  95. if inst and t.sons[0].kind == tyRange:
  96. var rng = newNodeX(nkInfix)
  97. rng.add newIdentNode(getIdent(cache, ".."), info)
  98. rng.add t.sons[0].n.sons[0].copyTree
  99. rng.add t.sons[0].n.sons[1].copyTree
  100. result.add rng
  101. else:
  102. result.add mapTypeToAst(t.sons[0], info)
  103. result.add mapTypeToAst(t.sons[1], info)
  104. of tyTypeDesc:
  105. if t.base != nil:
  106. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  107. result.add atomicType("typeDesc", mTypeDesc)
  108. result.add mapTypeToAst(t.base, info)
  109. else:
  110. result = atomicType("typeDesc", mTypeDesc)
  111. of tyGenericInvocation:
  112. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  113. for i in 0 ..< t.len:
  114. result.add mapTypeToAst(t.sons[i], info)
  115. of tyGenericInst:
  116. if inst:
  117. if allowRecursion:
  118. result = mapTypeToAstR(t.lastSon, info)
  119. else:
  120. result = newNodeX(nkBracketExpr)
  121. #result.add mapTypeToAst(t.lastSon, info)
  122. result.add mapTypeToAst(t[0], info)
  123. for i in 1 ..< t.len-1:
  124. result.add mapTypeToAst(t.sons[i], info)
  125. else:
  126. result = mapTypeToAstX(cache, t.lastSon, info, inst, allowRecursion)
  127. of tyGenericBody:
  128. if inst:
  129. result = mapTypeToAstR(t.lastSon, info)
  130. else:
  131. result = mapTypeToAst(t.lastSon, info)
  132. of tyAlias:
  133. result = mapTypeToAstX(cache, t.lastSon, info, inst, allowRecursion)
  134. of tyOrdinal:
  135. result = mapTypeToAst(t.lastSon, info)
  136. of tyDistinct:
  137. if inst:
  138. result = newNodeX(nkDistinctTy)
  139. result.add mapTypeToAst(t.sons[0], info)
  140. else:
  141. if allowRecursion or t.sym == nil:
  142. result = mapTypeToBracket("distinct", mDistinct, t, info)
  143. else:
  144. result = atomicType(t.sym)
  145. of tyGenericParam, tyForward:
  146. result = atomicType(t.sym)
  147. of tyObject:
  148. if inst:
  149. result = newNodeX(nkObjectTy)
  150. if t.sym.ast != nil:
  151. result.add t.sym.ast[2][0].copyTree # copy object pragmas
  152. else:
  153. result.add newNodeI(nkEmpty, info)
  154. if t.sons[0] == nil:
  155. result.add newNodeI(nkEmpty, info)
  156. else: # handle parent object
  157. var nn = newNodeX(nkOfInherit)
  158. nn.add mapTypeToAst(t.sons[0], info)
  159. result.add nn
  160. if t.n.len > 0:
  161. result.add objectNode(cache, t.n)
  162. else:
  163. result.add newNodeI(nkEmpty, info)
  164. else:
  165. if allowRecursion or t.sym == nil:
  166. result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t)
  167. result.add newNodeI(nkEmpty, info)
  168. if t.sons[0] == nil:
  169. result.add newNodeI(nkEmpty, info)
  170. else:
  171. result.add mapTypeToAst(t.sons[0], info)
  172. result.add copyTree(t.n)
  173. else:
  174. result = atomicType(t.sym)
  175. of tyEnum:
  176. result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t)
  177. result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum
  178. for c in t.n.sons:
  179. result.add copyTree(c)
  180. of tyTuple:
  181. if inst:
  182. # only named tuples have a node, unnamed tuples don't
  183. if t.n.isNil:
  184. result = newNodeX(nkTupleConstr)
  185. for subType in t.sons:
  186. result.add mapTypeToAst(subType, info)
  187. else:
  188. result = newNodeX(nkTupleTy)
  189. for s in t.n.sons:
  190. result.add newIdentDefs(s)
  191. else:
  192. result = mapTypeToBracket("tuple", mTuple, t, info)
  193. of tySet: result = mapTypeToBracket("set", mSet, t, info)
  194. of tyPtr:
  195. if inst:
  196. result = newNodeX(nkPtrTy)
  197. result.add mapTypeToAst(t.sons[0], info)
  198. else:
  199. result = mapTypeToBracket("ptr", mPtr, t, info)
  200. of tyRef:
  201. if inst:
  202. result = newNodeX(nkRefTy)
  203. result.add mapTypeToAst(t.sons[0], info)
  204. else:
  205. result = mapTypeToBracket("ref", mRef, t, info)
  206. of tyVar:
  207. if inst:
  208. result = newNodeX(nkVarTy)
  209. result.add mapTypeToAst(t.sons[0], info)
  210. else:
  211. result = mapTypeToBracket("var", mVar, t, info)
  212. of tyLent: result = mapTypeToBracket("lent", mBuiltinType, t, info)
  213. of tySink: result = mapTypeToBracket("sink", mBuiltinType, t, info)
  214. of tySequence: result = mapTypeToBracket("seq", mSeq, t, info)
  215. of tyOpt: result = mapTypeToBracket("opt", mOpt, t, info)
  216. of tyProc:
  217. if inst:
  218. result = newNodeX(nkProcTy)
  219. var fp = newNodeX(nkFormalParams)
  220. if t.sons[0] == nil:
  221. fp.add newNodeI(nkEmpty, info)
  222. else:
  223. fp.add mapTypeToAst(t.sons[0], t.n[0].info)
  224. for i in 1..<t.sons.len:
  225. fp.add newIdentDefs(t.n[i], t.sons[i])
  226. result.add fp
  227. result.add if t.n[0].len > 0: t.n[0][pragmasEffects].copyTree
  228. else: newNodeI(nkEmpty, info)
  229. else:
  230. result = mapTypeToBracket("proc", mNone, t, info)
  231. of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info)
  232. of tyRange:
  233. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  234. result.add atomicType("range", mRange)
  235. if inst:
  236. let rng = newNodeX(nkInfix)
  237. rng.add newIdentNode(getIdent(cache, ".."), info)
  238. rng.add t.n.sons[0].copyTree
  239. rng.add t.n.sons[1].copyTree
  240. result.add rng
  241. else:
  242. result.add t.n.sons[0].copyTree
  243. result.add t.n.sons[1].copyTree
  244. of tyPointer: result = atomicType("pointer", mPointer)
  245. of tyString: result = atomicType("string", mString)
  246. of tyCString: result = atomicType("cstring", mCString)
  247. of tyInt: result = atomicType("int", mInt)
  248. of tyInt8: result = atomicType("int8", mInt8)
  249. of tyInt16: result = atomicType("int16", mInt16)
  250. of tyInt32: result = atomicType("int32", mInt32)
  251. of tyInt64: result = atomicType("int64", mInt64)
  252. of tyFloat: result = atomicType("float", mFloat)
  253. of tyFloat32: result = atomicType("float32", mFloat32)
  254. of tyFloat64: result = atomicType("float64", mFloat64)
  255. of tyFloat128: result = atomicType("float128", mFloat128)
  256. of tyUInt: result = atomicType("uint", mUint)
  257. of tyUInt8: result = atomicType("uint8", mUint8)
  258. of tyUInt16: result = atomicType("uint16", mUint16)
  259. of tyUInt32: result = atomicType("uint32", mUint32)
  260. of tyUInt64: result = atomicType("uint64", mUint64)
  261. of tyVarargs: result = mapTypeToBracket("varargs", mVarargs, t, info)
  262. of tyProxy: result = atomicType("error", mNone)
  263. of tyBuiltInTypeClass:
  264. result = mapTypeToBracket("builtinTypeClass", mNone, t, info)
  265. of tyUserTypeClass, tyUserTypeClassInst:
  266. if t.isResolvedUserTypeClass:
  267. result = mapTypeToAst(t.lastSon, info)
  268. else:
  269. result = mapTypeToBracket("concept", mNone, t, info)
  270. result.add t.n.copyTree
  271. of tyCompositeTypeClass:
  272. result = mapTypeToBracket("compositeTypeClass", mNone, t, info)
  273. of tyAnd: result = mapTypeToBracket("and", mAnd, t, info)
  274. of tyOr: result = mapTypeToBracket("or", mOr, t, info)
  275. of tyNot: result = mapTypeToBracket("not", mNot, t, info)
  276. of tyAnything: result = atomicType("anything", mNone)
  277. of tyInferred: assert false
  278. of tyStatic, tyFromExpr:
  279. if inst:
  280. if t.n != nil: result = t.n.copyTree
  281. else: result = atomicType("void", mVoid)
  282. else:
  283. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  284. result.add atomicType("static", mNone)
  285. if t.n != nil:
  286. result.add t.n.copyTree
  287. of tyOptAsRef: assert(false, "mapTypeToAstX")
  288. proc opMapTypeToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  289. result = mapTypeToAstX(cache, t, info, false, true)
  290. # the "Inst" version includes generic parameters in the resulting type tree
  291. # and also tries to look like the corresponding Nim type declaration
  292. proc opMapTypeInstToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  293. result = mapTypeToAstX(cache, t, info, true, false)
  294. # the "Impl" version includes generic parameters in the resulting type tree
  295. # and also tries to look like the corresponding Nim type implementation
  296. proc opMapTypeImplToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  297. result = mapTypeToAstX(cache, t, info, true, true)