vmdeps.nim 12 KB

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