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.sons[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.sons[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.sons[i]!=nil: mapTypeToAstX(cache, t.sons[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("expr", mExpr)
  87. of tyTyped: result = atomicType("stmt", 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.sons[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.sons[0].kind == tyRange:
  98. var rng = newNodeX(nkInfix)
  99. rng.add newIdentNode(getIdent(cache, ".."), info)
  100. rng.add t.sons[0].n.sons[0].copyTree
  101. rng.add t.sons[0].n.sons[1].copyTree
  102. result.add rng
  103. else:
  104. result.add mapTypeToAst(t.sons[0], info)
  105. result.add mapTypeToAst(t.sons[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.sons[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.sons[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.sons[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. if t.sym.ast != nil:
  153. result.add t.sym.ast[2][0].copyTree # copy object pragmas
  154. else:
  155. result.add newNodeI(nkEmpty, info)
  156. if t.sons[0] == nil:
  157. result.add newNodeI(nkEmpty, info)
  158. else: # handle parent object
  159. var nn = newNodeX(nkOfInherit)
  160. nn.add mapTypeToAst(t.sons[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.sons[0] == nil:
  171. result.add newNodeI(nkEmpty, info)
  172. else:
  173. result.add mapTypeToAst(t.sons[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.sons[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.sons[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.sons[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 tyOpt: result = mapTypeToBracket("opt", mOpt, t, info)
  218. of tyProc:
  219. if inst:
  220. result = newNodeX(nkProcTy)
  221. var fp = newNodeX(nkFormalParams)
  222. if t.sons[0] == nil:
  223. fp.add newNodeI(nkEmpty, info)
  224. else:
  225. fp.add mapTypeToAst(t.sons[0], t.n[0].info)
  226. for i in 1..<t.sons.len:
  227. fp.add newIdentDefs(t.n[i], t.sons[i])
  228. result.add fp
  229. result.add if t.n[0].len > 0: t.n[0][pragmasEffects].copyTree
  230. else: newNodeI(nkEmpty, info)
  231. else:
  232. result = mapTypeToBracket("proc", mNone, t, info)
  233. of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info)
  234. of tyRange:
  235. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  236. result.add atomicType("range", mRange)
  237. if inst and t.n.len == 2:
  238. let rng = newNodeX(nkInfix)
  239. rng.add newIdentNode(getIdent(cache, ".."), info)
  240. rng.add t.n.sons[0].copyTree
  241. rng.add t.n.sons[1].copyTree
  242. result.add rng
  243. else:
  244. result.add t.n.sons[0].copyTree
  245. if t.n.len > 1:
  246. result.add t.n.sons[1].copyTree
  247. of tyPointer: result = atomicType("pointer", mPointer)
  248. of tyString: result = atomicType("string", mString)
  249. of tyCString: result = atomicType("cstring", mCstring)
  250. of tyInt: result = atomicType("int", mInt)
  251. of tyInt8: result = atomicType("int8", mInt8)
  252. of tyInt16: result = atomicType("int16", mInt16)
  253. of tyInt32: result = atomicType("int32", mInt32)
  254. of tyInt64: result = atomicType("int64", mInt64)
  255. of tyFloat: result = atomicType("float", mFloat)
  256. of tyFloat32: result = atomicType("float32", mFloat32)
  257. of tyFloat64: result = atomicType("float64", mFloat64)
  258. of tyFloat128: result = atomicType("float128", mFloat128)
  259. of tyUInt: result = atomicType("uint", mUInt)
  260. of tyUInt8: result = atomicType("uint8", mUInt8)
  261. of tyUInt16: result = atomicType("uint16", mUInt16)
  262. of tyUInt32: result = atomicType("uint32", mUInt32)
  263. of tyUInt64: result = atomicType("uint64", mUInt64)
  264. of tyVarargs: result = mapTypeToBracket("varargs", mVarargs, t, info)
  265. of tyProxy: result = atomicType("error", mNone)
  266. of tyBuiltInTypeClass:
  267. result = mapTypeToBracket("builtinTypeClass", mNone, t, info)
  268. of tyUserTypeClass, tyUserTypeClassInst:
  269. if t.isResolvedUserTypeClass:
  270. result = mapTypeToAst(t.lastSon, info)
  271. else:
  272. result = mapTypeToBracket("concept", mNone, t, info)
  273. result.add t.n.copyTree
  274. of tyCompositeTypeClass:
  275. result = mapTypeToBracket("compositeTypeClass", mNone, t, info)
  276. of tyAnd: result = mapTypeToBracket("and", mAnd, t, info)
  277. of tyOr: result = mapTypeToBracket("or", mOr, t, info)
  278. of tyNot: result = mapTypeToBracket("not", mNot, t, info)
  279. of tyAnything: result = atomicType("anything", mNone)
  280. of tyInferred: assert false
  281. of tyStatic, tyFromExpr:
  282. if inst:
  283. if t.n != nil: result = t.n.copyTree
  284. else: result = atomicType("void", mVoid)
  285. else:
  286. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  287. result.add atomicType("static", mNone)
  288. if t.n != nil:
  289. result.add t.n.copyTree
  290. of tyOwned: result = mapTypeToBracket("owned", mBuiltinType, t, info)
  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)