vmdeps.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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; skipAlias = 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; skipAlias = 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 and
  84. not (skipAlias and t.kind == tyAlias):
  85. # getTypeInst behavior: return symbol
  86. return atomicType(t.sym)
  87. case t.kind
  88. of tyNone: result = atomicType("none", mNone)
  89. of tyBool: result = atomicType("bool", mBool)
  90. of tyChar: result = atomicType("char", mChar)
  91. of tyNil: result = atomicType("nil", mNil)
  92. of tyUntyped: result = atomicType("untyped", mExpr)
  93. of tyTyped: result = atomicType("typed", mStmt)
  94. of tyVoid: result = atomicType("void", mVoid)
  95. of tyEmpty: result = atomicType("empty", mNone)
  96. of tyUncheckedArray:
  97. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  98. result.add atomicType("UncheckedArray", mUncheckedArray)
  99. result.add mapTypeToAst(t.elementType, info)
  100. of tyArray:
  101. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  102. result.add atomicType("array", mArray)
  103. if inst and t.indexType.kind == tyRange:
  104. var rng = newNodeX(nkInfix)
  105. rng.add newIdentNode(getIdent(cache, ".."), info)
  106. rng.add t.indexType.n[0].copyTree
  107. rng.add t.indexType.n[1].copyTree
  108. result.add rng
  109. else:
  110. result.add mapTypeToAst(t.indexType, info)
  111. result.add mapTypeToAst(t.elementType, info)
  112. of tyTypeDesc:
  113. if t.base != nil:
  114. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  115. result.add atomicType("typeDesc", mTypeDesc)
  116. result.add mapTypeToAstX(cache, t.base, info, idgen, inst, skipAlias = skipAlias)
  117. else:
  118. result = atomicType("typeDesc", mTypeDesc)
  119. of tyGenericInvocation:
  120. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  121. for a in t.kids:
  122. result.add mapTypeToAst(a, info)
  123. of tyGenericInst:
  124. if inst:
  125. if allowRecursion:
  126. result = mapTypeToAstR(t.skipModifier, info)
  127. # keep original type info for getType calls on the output node:
  128. result.typ() = t
  129. else:
  130. result = newNodeX(nkBracketExpr)
  131. #result.add mapTypeToAst(t.last, info)
  132. result.add mapTypeToAst(t.genericHead, info)
  133. for _, a in t.genericInstParams:
  134. result.add mapTypeToAst(a, info)
  135. else:
  136. result = mapTypeToAstX(cache, t.skipModifier, info, idgen, inst, allowRecursion)
  137. # keep original type info for getType calls on the output node:
  138. result.typ() = t
  139. of tyGenericBody:
  140. if inst:
  141. result = mapTypeToAstR(t.typeBodyImpl, info)
  142. else:
  143. result = mapTypeToAst(t.typeBodyImpl, info)
  144. of tyAlias:
  145. result = mapTypeToAstX(cache, t.skipModifier, info, idgen, inst, allowRecursion, skipAlias = skipAlias)
  146. of tyOrdinal:
  147. result = mapTypeToAst(t.skipModifier, info)
  148. of tyDistinct:
  149. if inst:
  150. result = newNodeX(nkDistinctTy)
  151. result.add mapTypeToAst(t.skipModifier, info)
  152. else:
  153. if allowRecursion or t.sym == nil:
  154. result = mapTypeToBracket("distinct", mDistinct, t, info)
  155. else:
  156. result = atomicType(t.sym)
  157. of tyGenericParam, tyForward:
  158. result = atomicType(t.sym)
  159. of tyObject:
  160. if inst:
  161. result = newNodeX(nkObjectTy)
  162. var objectDef = t.sym.ast[2]
  163. if objectDef.kind == nkRefTy:
  164. objectDef = objectDef[0]
  165. result.add objectDef[0].copyTree # copy object pragmas
  166. if t.baseClass == nil:
  167. result.add newNodeI(nkEmpty, info)
  168. else: # handle parent object
  169. var nn = newNodeX(nkOfInherit)
  170. nn.add mapTypeToAst(t.baseClass, info)
  171. result.add nn
  172. if t.n.len > 0:
  173. result.add objectNode(cache, t.n, idgen)
  174. else:
  175. result.add newNodeI(nkEmpty, info)
  176. else:
  177. if allowRecursion or t.sym == nil:
  178. result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t)
  179. result.add newNodeI(nkEmpty, info)
  180. if t.baseClass == nil:
  181. result.add newNodeI(nkEmpty, info)
  182. else:
  183. result.add mapTypeToAst(t.baseClass, info)
  184. result.add copyTree(t.n)
  185. else:
  186. result = atomicType(t.sym)
  187. of tyEnum:
  188. result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t)
  189. result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum
  190. for c in t.n.sons:
  191. result.add copyTree(c)
  192. of tyTuple:
  193. if inst:
  194. # only named tuples have a node, unnamed tuples don't
  195. if t.n.isNil:
  196. result = newNodeX(nkTupleConstr)
  197. for subType in t.kids:
  198. result.add mapTypeToAst(subType, info)
  199. else:
  200. result = newNodeX(nkTupleTy)
  201. for s in t.n.sons:
  202. result.add newIdentDefs(s)
  203. else:
  204. result = mapTypeToBracket("tuple", mTuple, t, info)
  205. of tySet: result = mapTypeToBracket("set", mSet, t, info)
  206. of tyPtr:
  207. if inst:
  208. result = newNodeX(nkPtrTy)
  209. result.add mapTypeToAst(t.elementType, info)
  210. else:
  211. result = mapTypeToBracket("ptr", mPtr, t, info)
  212. of tyRef:
  213. if inst:
  214. result = newNodeX(nkRefTy)
  215. result.add mapTypeToAst(t.elementType, info)
  216. else:
  217. result = mapTypeToBracket("ref", mRef, t, info)
  218. of tyVar:
  219. if inst:
  220. result = newNodeX(nkVarTy)
  221. result.add mapTypeToAst(t.elementType, info)
  222. else:
  223. result = mapTypeToBracket("var", mVar, t, info)
  224. of tyLent: result = mapTypeToBracket("lent", mBuiltinType, t, info)
  225. of tySink: result = mapTypeToBracket("sink", mBuiltinType, t, info)
  226. of tySequence: result = mapTypeToBracket("seq", mSeq, t, info)
  227. of tyProc:
  228. if inst:
  229. result = newNodeX(if tfIterator in t.flags: nkIteratorTy else: nkProcTy)
  230. var fp = newNodeX(nkFormalParams)
  231. if t.returnType == nil:
  232. fp.add newNodeI(nkEmpty, info)
  233. else:
  234. fp.add mapTypeToAst(t.returnType, t.n[0].info)
  235. for i in FirstParamAt..<t.kidsLen:
  236. fp.add newIdentDefs(t.n[i], t[i])
  237. result.add fp
  238. var prag =
  239. if t.n[0].len > 0:
  240. t.n[0][pragmasEffects].copyTree
  241. else:
  242. newNodeI(nkEmpty, info)
  243. if t.callConv != ccClosure or tfExplicitCallConv in t.flags:
  244. if prag.kind == nkEmpty: prag = newNodeI(nkPragma, info)
  245. prag.add newIdentNode(getIdent(cache, $t.callConv), info)
  246. result.add prag
  247. else:
  248. result = mapTypeToBracket("proc", mNone, t, info)
  249. of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info)
  250. of tyRange:
  251. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  252. result.add atomicType("range", mRange)
  253. if inst and t.n.len == 2:
  254. let rng = newNodeX(nkInfix)
  255. rng.add newIdentNode(getIdent(cache, ".."), info)
  256. rng.add t.n[0].copyTree
  257. rng.add t.n[1].copyTree
  258. result.add rng
  259. else:
  260. result.add t.n[0].copyTree
  261. if t.n.len > 1:
  262. result.add t.n[1].copyTree
  263. of tyPointer: result = atomicType("pointer", mPointer)
  264. of tyString: result = atomicType("string", mString)
  265. of tyCstring: result = atomicType("cstring", mCstring)
  266. of tyInt: result = atomicType("int", mInt)
  267. of tyInt8: result = atomicType("int8", mInt8)
  268. of tyInt16: result = atomicType("int16", mInt16)
  269. of tyInt32: result = atomicType("int32", mInt32)
  270. of tyInt64: result = atomicType("int64", mInt64)
  271. of tyFloat: result = atomicType("float", mFloat)
  272. of tyFloat32: result = atomicType("float32", mFloat32)
  273. of tyFloat64: result = atomicType("float64", mFloat64)
  274. of tyFloat128: result = atomicType("float128", mFloat128)
  275. of tyUInt: result = atomicType("uint", mUInt)
  276. of tyUInt8: result = atomicType("uint8", mUInt8)
  277. of tyUInt16: result = atomicType("uint16", mUInt16)
  278. of tyUInt32: result = atomicType("uint32", mUInt32)
  279. of tyUInt64: result = atomicType("uint64", mUInt64)
  280. of tyVarargs: result = mapTypeToBracket("varargs", mVarargs, t, info)
  281. of tyError: result = atomicType("error", mNone)
  282. of tyBuiltInTypeClass:
  283. result = mapTypeToBracket("builtinTypeClass", mNone, t, info)
  284. of tyUserTypeClass, tyUserTypeClassInst:
  285. if t.isResolvedUserTypeClass:
  286. result = mapTypeToAst(t.last, info)
  287. else:
  288. result = mapTypeToBracket("concept", mNone, t, info)
  289. result.add t.n.copyTree
  290. of tyCompositeTypeClass:
  291. result = mapTypeToBracket("compositeTypeClass", mNone, t, info)
  292. of tyAnd: result = mapTypeToBracket("and", mAnd, t, info)
  293. of tyOr: result = mapTypeToBracket("or", mOr, t, info)
  294. of tyNot: result = mapTypeToBracket("not", mNot, t, info)
  295. of tyIterable: result = mapTypeToBracket("iterable", mIterableType, t, info)
  296. of tyAnything: result = atomicType("anything", mNone)
  297. of tyInferred: result = mapTypeToAstX(cache, t.skipModifier, info, idgen, inst, allowRecursion)
  298. of tyStatic, tyFromExpr:
  299. if inst:
  300. if t.n != nil: result = t.n.copyTree
  301. else: result = atomicType("void", mVoid)
  302. else:
  303. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  304. result.add atomicType("static", mNone)
  305. if t.n != nil:
  306. result.add t.n.copyTree
  307. of tyOwned: result = mapTypeToBracket("owned", mBuiltinType, t, info)
  308. of tyConcept:
  309. result = mapTypeToBracket("concept", mNone, t, info)
  310. result.add t.n.copyTree
  311. proc opMapTypeToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator): PNode =
  312. result = mapTypeToAstX(cache, t, info, idgen, inst=false, allowRecursionX=true)
  313. # the "Inst" version includes generic parameters in the resulting type tree
  314. # and also tries to look like the corresponding Nim type declaration
  315. proc opMapTypeInstToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator; skipAlias = false): PNode =
  316. # skipAlias: skips aliases and typedesc
  317. result = mapTypeToAstX(cache, t, info, idgen, inst=true, allowRecursionX=false, skipAlias = skipAlias)
  318. # the "Impl" version includes generic parameters in the resulting type tree
  319. # and also tries to look like the corresponding Nim type implementation
  320. proc opMapTypeImplToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator): PNode =
  321. result = mapTypeToAstX(cache, t, info, idgen, inst=true, allowRecursionX=true)