vmdeps.nim 12 KB

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