vmdeps.nim 12 KB

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