vmdeps.nim 12 KB

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