vmdeps.nim 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. result.add t.sym.ast[2][0].copyTree # copy object pragmas
  151. if t.sons[0] == nil:
  152. result.add newNodeI(nkEmpty, info)
  153. else: # handle parent object
  154. var nn = newNodeX(nkOfInherit)
  155. nn.add mapTypeToAst(t.sons[0], info)
  156. result.add nn
  157. if t.n.len > 0:
  158. result.add objectNode(cache, t.n)
  159. else:
  160. result.add newNodeI(nkEmpty, info)
  161. else:
  162. if allowRecursion or t.sym == nil:
  163. result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t)
  164. result.add newNodeI(nkEmpty, info)
  165. if t.sons[0] == nil:
  166. result.add newNodeI(nkEmpty, info)
  167. else:
  168. result.add mapTypeToAst(t.sons[0], info)
  169. result.add copyTree(t.n)
  170. else:
  171. result = atomicType(t.sym)
  172. of tyEnum:
  173. result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t)
  174. result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum
  175. for c in t.n.sons:
  176. result.add copyTree(c)
  177. of tyTuple:
  178. if inst:
  179. # only named tuples have a node, unnamed tuples don't
  180. if t.n.isNil:
  181. result = newNodeX(nkTupleConstr)
  182. for subType in t.sons:
  183. result.add mapTypeToAst(subType, info)
  184. else:
  185. result = newNodeX(nkTupleTy)
  186. for s in t.n.sons:
  187. result.add newIdentDefs(s)
  188. else:
  189. result = mapTypeToBracket("tuple", mTuple, t, info)
  190. of tySet: result = mapTypeToBracket("set", mSet, t, info)
  191. of tyPtr:
  192. if inst:
  193. result = newNodeX(nkPtrTy)
  194. result.add mapTypeToAst(t.sons[0], info)
  195. else:
  196. result = mapTypeToBracket("ptr", mPtr, t, info)
  197. of tyRef:
  198. if inst:
  199. result = newNodeX(nkRefTy)
  200. result.add mapTypeToAst(t.sons[0], info)
  201. else:
  202. result = mapTypeToBracket("ref", mRef, t, info)
  203. of tyVar:
  204. if inst:
  205. result = newNodeX(nkVarTy)
  206. result.add mapTypeToAst(t.sons[0], info)
  207. else:
  208. result = mapTypeToBracket("var", mVar, t, info)
  209. of tyLent: result = mapTypeToBracket("lent", mBuiltinType, t, info)
  210. of tySink: result = mapTypeToBracket("sink", mBuiltinType, t, info)
  211. of tySequence: result = mapTypeToBracket("seq", mSeq, t, info)
  212. of tyOpt: result = mapTypeToBracket("opt", mOpt, t, info)
  213. of tyProc:
  214. if inst:
  215. result = newNodeX(nkProcTy)
  216. var fp = newNodeX(nkFormalParams)
  217. if t.sons[0] == nil:
  218. fp.add newNodeI(nkEmpty, info)
  219. else:
  220. fp.add mapTypeToAst(t.sons[0], t.n[0].info)
  221. for i in 1..<t.sons.len:
  222. fp.add newIdentDefs(t.n[i], t.sons[i])
  223. result.add fp
  224. result.add if t.n[0].len > 0: t.n[0][pragmasEffects].copyTree
  225. else: newNodeI(nkEmpty, info)
  226. else:
  227. result = mapTypeToBracket("proc", mNone, t, info)
  228. of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info)
  229. of tyRange:
  230. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  231. result.add atomicType("range", mRange)
  232. if inst:
  233. let rng = newNodeX(nkInfix)
  234. rng.add newIdentNode(getIdent(cache, ".."), info)
  235. rng.add t.n.sons[0].copyTree
  236. rng.add t.n.sons[1].copyTree
  237. result.add rng
  238. else:
  239. result.add t.n.sons[0].copyTree
  240. result.add t.n.sons[1].copyTree
  241. of tyPointer: result = atomicType("pointer", mPointer)
  242. of tyString: result = atomicType("string", mString)
  243. of tyCString: result = atomicType("cstring", mCString)
  244. of tyInt: result = atomicType("int", mInt)
  245. of tyInt8: result = atomicType("int8", mInt8)
  246. of tyInt16: result = atomicType("int16", mInt16)
  247. of tyInt32: result = atomicType("int32", mInt32)
  248. of tyInt64: result = atomicType("int64", mInt64)
  249. of tyFloat: result = atomicType("float", mFloat)
  250. of tyFloat32: result = atomicType("float32", mFloat32)
  251. of tyFloat64: result = atomicType("float64", mFloat64)
  252. of tyFloat128: result = atomicType("float128", mFloat128)
  253. of tyUInt: result = atomicType("uint", mUint)
  254. of tyUInt8: result = atomicType("uint8", mUint8)
  255. of tyUInt16: result = atomicType("uint16", mUint16)
  256. of tyUInt32: result = atomicType("uint32", mUint32)
  257. of tyUInt64: result = atomicType("uint64", mUint64)
  258. of tyVarargs: result = mapTypeToBracket("varargs", mVarargs, t, info)
  259. of tyProxy: result = atomicType("error", mNone)
  260. of tyBuiltInTypeClass:
  261. result = mapTypeToBracket("builtinTypeClass", mNone, t, info)
  262. of tyUserTypeClass, tyUserTypeClassInst:
  263. if t.isResolvedUserTypeClass:
  264. result = mapTypeToAst(t.lastSon, info)
  265. else:
  266. result = mapTypeToBracket("concept", mNone, t, info)
  267. result.add t.n.copyTree
  268. of tyCompositeTypeClass:
  269. result = mapTypeToBracket("compositeTypeClass", mNone, t, info)
  270. of tyAnd: result = mapTypeToBracket("and", mAnd, t, info)
  271. of tyOr: result = mapTypeToBracket("or", mOr, t, info)
  272. of tyNot: result = mapTypeToBracket("not", mNot, t, info)
  273. of tyAnything: result = atomicType("anything", mNone)
  274. of tyInferred: assert false
  275. of tyStatic, tyFromExpr:
  276. if inst:
  277. if t.n != nil: result = t.n.copyTree
  278. else: result = atomicType("void", mVoid)
  279. else:
  280. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  281. result.add atomicType("static", mNone)
  282. if t.n != nil:
  283. result.add t.n.copyTree
  284. of tyOptAsRef: assert(false, "mapTypeToAstX")
  285. proc opMapTypeToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  286. result = mapTypeToAstX(cache, t, info, false, true)
  287. # the "Inst" version includes generic parameters in the resulting type tree
  288. # and also tries to look like the corresponding Nim type declaration
  289. proc opMapTypeInstToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  290. result = mapTypeToAstX(cache, t, info, true, false)
  291. # the "Impl" version includes generic parameters in the resulting type tree
  292. # and also tries to look like the corresponding Nim type implementation
  293. proc opMapTypeImplToAst*(cache: IdentCache; t: PType; info: TLineInfo): PNode =
  294. result = mapTypeToAstX(cache, t, info, true, true)