vmdeps.nim 13 KB

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